qlib icon indicating copy to clipboard operation
qlib copied to clipboard

qrun TRA model error

Open stockcoder opened this issue 3 years ago • 5 comments

🐛 Bug Description

To Reproduce

Steps to reproduce the behavior:

  1. qrun examples/benchmarks/TRA/workflow_config_tra_Alpha158.yaml

Expected Behavior

ERROR - qlib.workflow - [utils.py:41] - An exception has been raised[AttributeError: 'list' object has no attribute 'start'].

Screenshot

Environment

Note: User could run cd scripts && python collect_info.py all under project directory to get system information and paste them here directly.

  • Qlib version:
  • Python version:
  • OS (Windows, Linux, MacOS):
  • Commit number (optional, please provide it if you are using the dev version):

Additional Notes

stockcoder avatar Apr 18 '22 14:04 stockcoder

I also found the error. You can edit the file: anaconda3\Lib\site-packages\qlib\contrib\data\dataset.py, line 196 of function _prepare_seg, and add the following code:

if not isinstance(slc, slice):
    slc = slice(*slc)

I wish it can be fixed in the next version. By the way, type of segments in class DatasetH and MTSDatasetH are different. You may need transform the type of segments in MTSDatasetH by pd.to_datetime().

polaris-gogh avatar Apr 19 '22 12:04 polaris-gogh

I also found the error. You can edit the file: anaconda3\Lib\site-packages\qlib\contrib\data\dataset.py, line 196 of function _prepare_seg, and add the following code:

if not isinstance(slc, slice):
    slc = slice(*slc)

I wish it can be fixed in the next version. By the way, type of segments in class DatasetH and MTSDatasetH are different. You may need transform the type of segments in MTSDatasetH by pd.to_datetime().

Thanks for your reply. I added your code, but it is not work. Can you give me more details?

stockcoder avatar Apr 20 '22 15:04 stockcoder

I also found the error. You can edit the file: anaconda3\Lib\site-packages\qlib\contrib\data\dataset.py, line 196 of function _prepare_seg, and add the following code:

if not isinstance(slc, slice):
    slc = slice(*slc)

I wish it can be fixed in the next version. By the way, type of segments in class DatasetH and MTSDatasetH are different. You may need transform the type of segments in MTSDatasetH by pd.to_datetime().

Thanks for your reply. I added your code, but it is not work. Can you give me more details?

can you paste the error message?

polaris-gogh avatar Apr 22 '22 05:04 polaris-gogh

I also found the error. You can edit the file: anaconda3\Lib\site-packages\qlib\contrib\data\dataset.py, line 196 of function _prepare_seg, and add the following code:

if not isinstance(slc, slice):
    slc = slice(*slc)

I wish it can be fixed in the next version. By the way, type of segments in class DatasetH and MTSDatasetH are different. You may need transform the type of segments in MTSDatasetH by pd.to_datetime().

Thanks for your reply. I added your code, but it is not work. Can you give me more details?

I think the reason you can't run the code is that the type of segments in class DatasetH and MTSDatasetH are different. You may run code in a py file instead of using qrun. Here is my py file:

import ruamel.yaml as yaml
import shutil
from pathlib import Path
from loguru import logger
from qlib import init_from_yaml_conf
from qlib.utils import init_instance_by_config, flatten_dict
from qlib.workflow import R
from qlib.workflow.record_temp import SignalRecord, PortAnaRecord, SigAnaRecord
from qlib.contrib.report import analysis_position
import pandas as pd

path = 'workflow_config_tra_Alpha360.yaml'

def change_to_datetime(config):
    df = pd.DataFrame(config['task']['dataset']['kwargs']['segments'])
    df = df.applymap(lambda x: pd.to_datetime(x))
    config['task']['dataset']['kwargs']['segments'] = df.to_dict(orient='list')
    return config



if __name__ == '__main__':
    if Path('mlruns/').exists():
        logger.info('delete existed mlruns...')
        shutil.rmtree(Path('mlruns/'))

    with open(path, 'r') as f:
        config = yaml.safe_load(f)

    print(config['task']['dataset']['kwargs']['segments'])
    config = change_to_datetime(config)
    print(config['task']['dataset']['kwargs']['segments'])

    init_from_yaml_conf(None, **config['qlib_init'])

    model = init_instance_by_config(config['task']["model"])
    dataset = init_instance_by_config(config['task']["dataset"])
    print(dataset)
    df_test = dataset.prepare('test')
    print('df_test:', df_test)

    with R.start(experiment_name="workflow"):
        # train
        R.log_params(**flatten_dict(config['task']))
        model.fit(dataset)
        R.save_objects(**{"params.pkl": model})

        # prediction
        recorder = R.get_recorder()
        sr = SignalRecord(model, dataset, recorder)
        sr.generate()

        # Signal Analysis
        sar = SigAnaRecord(recorder)
        sar.generate()

        # backtest
        par = PortAnaRecord(recorder, config['port_analysis_config'], "day")
        par.generate()

    # recorder = R.get_recorder(recorder_name='mlflow_recorder', experiment_id='1')
    print('recorder:',recorder)
    report_normal_df = recorder.load_object("portfolio_analysis/report_normal_1day.pkl")
    print(report_normal_df)
    figure_list = analysis_position.report_graph(report_normal_df, show_notebook=False)
    for _fig in figure_list:
        _fig.show()

    analysis_df = recorder.load_object("portfolio_analysis/port_analysis_1day.pkl")
    figure_list = analysis_position.risk_analysis_graph(analysis_df, report_normal_df, show_notebook=False)
    for _fig in figure_list:
        _fig.show()

    pred_df = recorder.load_object("pred.pkl")
    print(pred_df)
    pred_df = pred_df[['score','label']]
    # 对于MTSDatasetH,不能通过常规的prepare取数据
    # label_df = dataset.handler.fetch(col_set="label")
    figure_list = analysis_position.score_ic_graph(pred_df, show_notebook=False)
    for _fig in figure_list:
        _fig.show()

polaris-gogh avatar Apr 22 '22 05:04 polaris-gogh

Is this issue fixed by https://github.com/microsoft/qlib/pull/1050 ?

you-n-g avatar May 06 '22 09:05 you-n-g