Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qrun TRA model error #1062

Closed
stockcoder opened this issue Apr 18, 2022 · 5 comments
Closed

qrun TRA model error #1062

stockcoder opened this issue Apr 18, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@stockcoder
Copy link

stockcoder commented Apr 18, 2022

🐛 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 stockcoder added the bug Something isn't working label Apr 18, 2022
@polaris-gogh
Copy link

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().

@stockcoder
Copy link
Author

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?

@polaris-gogh
Copy link

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
Copy link

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()

@you-n-g
Copy link
Collaborator

you-n-g commented May 6, 2022

Is this issue fixed by #1050 ?

HyeongminMoon added a commit to HyeongminMoon/qlib that referenced this issue Jun 16, 2022
solve issue "qrun TRA model error (microsoft#1062)"
HyeongminMoon pushed a commit to HyeongminMoon/qlib that referenced this issue Jun 28, 2022
HyeongminMoon added a commit to HyeongminMoon/qlib that referenced this issue Jun 28, 2022
you-n-g pushed a commit that referenced this issue Jul 7, 2022
* fix bug on TRA dataset

solve issue "qrun TRA model error (#1062)"

* apply black pylint
qianyun210603 pushed a commit to qianyun210603/qlib that referenced this issue Mar 23, 2023
* fix bug on TRA dataset

solve issue "qrun TRA model error (microsoft#1062)"

* apply black pylint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants