# Path: alphaware/enums.py
# class FreqType(StrEnum):
#     EOD = 'D'
#     EOW = 'W'
#     EOM = 'M'
#     EOQ = 'Q'
#     EOSY = 'S'
#     EOY = 'Y'
# 
# class OutputDataFormat(IntEnum):
#     MULTI_INDEX_DF = 0
#     PITVOT_TABLE_DF = 1
# 
# Path: alphaware/utils/input_validation.py
# @expect_types(date=(str, datetime))
# def ensure_datetime(date, date_format='%Y-%m-%d'):
#     """
#     :param date: str, datetime, 日期
#     :param date_format: str, 时间格式
#     :return: datetime
#     """
#     if isinstance(date, datetime):
#         return date
#     else:
#         return datetime.strptime(date, date_format)
# 
# @expect_types(data=(pd.DataFrame, pd.Series), data_format=OutputDataFormat)
# def ensure_pd_index_names(data, data_format, valid_index):
#     if data_format == OutputDataFormat.MULTI_INDEX_DF:
#         data.index.names = valid_index.full_index
#     else:
#         data.index.name = valid_index.date_index
#     return data
# 
# Path: alphaware/utils/pandas_utils.py
# @expect_types(data=(pd.Series, pd.DataFrame))
# def convert_df_format(data, target_format=OutputDataFormat.MULTI_INDEX_DF, col_name='factor',
#                       multi_index=INDEX_FACTOR):
#     if target_format == OutputDataFormat.MULTI_INDEX_DF:
#         tmp = data.stack()
#         data_ = pd.DataFrame(tmp)
#         data_.index.names = multi_index.full_index
#         data_.columns = [col_name]
#     else:
#         tmp = data.unstack()
#         index = tmp.index
#         columns = tmp.columns.get_level_values(multi_index.sec_index).tolist()
#         data_ = pd.DataFrame(tmp.values, index=index, columns=columns)
# 
#     return data_
# 
# Path: alphaware/const.py
# INDEX_FACTOR = namedtuple('INDEX_FACTOR', ['date_index', 'sec_index', 'full_index', 'col_score'])
# 
# INDEX_INDUSTRY_WEIGHT = namedtuple('INDEX_INDUSTRY_WEIGHT',
#                                    ['date_index', 'industry_index', 'full_index', 'col_name'])


from collections import defaultdict
from toolz.dicttoolz import merge
from xutils import py_assert
from alphaware.enums import (FreqType,
                     OutputDataFormat)
from alphaware.utils import (convert_df_format,
                     ensure_datetime,
                     ensure_pd_index_names)
from alphaware.const import (INDEX_FACTOR,
                     INDEX_INDUSTRY_WEIGHT)
import numpy as np
        if self.property['data_format'] != self.production_data_format:
            if data is not None:
                data = convert_df_format(data, target_format=self.production_data_format)
        return

    def _validate_data_format(self):
        self._ensure_data_format(self.hist_return)
        self._ensure_data_format(self.industry_weight)

    def _ensure_date_format(self, data, multi_index):
        date_format = self.property.get('date_format', '%Y-%m-%d')
        data = ensure_pd_index_names(data, data_format=self.production_data_format, valid_index=multi_index)
        date_index = data.index.get_level_values(multi_index.date_index)
        date_index = [ensure_datetime(date_, date_format) for date_ in date_index]
        data.reset_index(inplace=True)
        data[multi_index.date_index] = date_index
        if self.production_data_format == OutputDataFormat.MULTI_INDEX_DF:
            data.set_index(keys=multi_index.full_index, inplace=True)
            if hasattr(multi_index, 'col_name'):
                data.columns = [multi_index.col_name]
        else:
            data.set_index(keys=multi_index.date_index, inplace=True)
        return

    def _validate_date_format(self):
        self._ensure_date_format(self.hist_return, INDEX_FACTOR)
        self._ensure_date_format(self.industry_weight, INDEX_INDUSTRY_WEIGHT)

    def get_industry_weight_on_date(self, date):
        py_assert(self.industry_weight is not None, ValueError, 'industry weight data is empty')

# Path: alphaware/enums.py
# class FreqType(StrEnum):
#     EOD = 'D'
#     EOW = 'W'
#     EOM = 'M'
#     EOQ = 'Q'
#     EOSY = 'S'
#     EOY = 'Y'
# 
# class OutputDataFormat(IntEnum):
#     MULTI_INDEX_DF = 0
#     PITVOT_TABLE_DF = 1
# 
# Path: alphaware/utils/input_validation.py
# @expect_types(date=(str, datetime))
# def ensure_datetime(date, date_format='%Y-%m-%d'):
#     """
#     :param date: str, datetime, 日期
#     :param date_format: str, 时间格式
#     :return: datetime
#     """
#     if isinstance(date, datetime):
#         return date
#     else:
#         return datetime.strptime(date, date_format)
# 
# @expect_types(data=(pd.DataFrame, pd.Series), data_format=OutputDataFormat)
# def ensure_pd_index_names(data, data_format, valid_index):
#     if data_format == OutputDataFormat.MULTI_INDEX_DF:
#         data.index.names = valid_index.full_index
#     else:
#         data.index.name = valid_index.date_index
#     return data
# 
# Path: alphaware/utils/pandas_utils.py
# @expect_types(data=(pd.Series, pd.DataFrame))
# def convert_df_format(data, target_format=OutputDataFormat.MULTI_INDEX_DF, col_name='factor',
#                       multi_index=INDEX_FACTOR):
#     if target_format == OutputDataFormat.MULTI_INDEX_DF:
#         tmp = data.stack()
#         data_ = pd.DataFrame(tmp)
#         data_.index.names = multi_index.full_index
#         data_.columns = [col_name]
#     else:
#         tmp = data.unstack()
#         index = tmp.index
#         columns = tmp.columns.get_level_values(multi_index.sec_index).tolist()
#         data_ = pd.DataFrame(tmp.values, index=index, columns=columns)
# 
#     return data_
# 
# Path: alphaware/const.py
# INDEX_FACTOR = namedtuple('INDEX_FACTOR', ['date_index', 'sec_index', 'full_index', 'col_score'])
# 
# INDEX_INDUSTRY_WEIGHT = namedtuple('INDEX_INDUSTRY_WEIGHT',
#                                    ['date_index', 'industry_index', 'full_index', 'col_name'])


# Path: alphaware/preprocess/benchmark.py
from collections import defaultdict
from toolz.dicttoolz import merge
from xutils import py_assert
from alphaware.enums import (FreqType,
                     OutputDataFormat)
from alphaware.utils import (convert_df_format,
                     ensure_datetime,
                     ensure_pd_index_names)
from alphaware.const import (INDEX_FACTOR,
                     INDEX_INDUSTRY_WEIGHT)
import numpy as np

        if self.property['data_format'] != self.production_data_format:
            if data is not None:
                data = convert_df_format(data, target_format=self.production_data_format)
        return

    def _validate_data_format(self):
        self._ensure_data_format(self.hist_return)
        self._ensure_data_format(self.industry_weight)

    def _ensure_date_format(self, data, multi_index):
        date_format = self.property.get('date_format', '%Y-%m-%d')
        data = ensure_pd_index_names(data, data_format=self.production_data_format, valid_index=multi_index)
        date_index = data.index.get_level_values(multi_index.date_index)
        date_index = [ensure_datetime(date_, date_format) for date_ in date_index]
        data.reset_index(inplace=True)
        data[multi_index.date_index] = date_index
        if self.production_data_format == OutputDataFormat.MULTI_INDEX_DF:
            data.set_index(keys=multi_index.full_index, inplace=True)
            if hasattr(multi_index, 'col_name'):
                data.columns = [multi_index.col_name]
        else:
            data.set_index(keys=multi_index.date_index, inplace=True)
        return

    def _validate_date_format(self):
        self._ensure_date_format(self.hist_return, INDEX_FACTOR)
        self._ensure_date_format(self.industry_weight, INDEX_INDUSTRY_WEIGHT)

    def get_industry_weight_on_date(self, date):
        py_assert(self.industry_weight is not None, ValueError, 'industry weight data is empty')

        date = ensure_datetime(date)