自定义模块教程-选择列

自定义模块
标签: #<Tag:0x00007fcc049a1fa0>

(iQuant) #1

参考 自定义模块教程-开发一个修改数据列名的模块为例-超级详细版 开发自定义模块

克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-539:input_1","SourceOutputPortId":"-185:data"},{"DestinationInputPortId":"-23:input_ds","SourceOutputPortId":"-185:data"}],"ModuleNodes":[{"Id":"-539","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# 修改数据列名\ndef bigquant_run(input_ds, columns, reverse_select):\n columns = columns.strip().split(',')\n print('列:', columns)\n # 输出数据源\n dataset_ds = DataSource()\n output_store = dataset_ds.open_df_store()\n\n for key, df in input_ds.iter_df():\n curr_column_set = set(df.columns)\n for col in columns:\n if col not in curr_column_set:\n print('警告:列 %s 不存在' % col)\n if reverse_select:\n columns_set = set(columns)\n columns = [c for c in df.columns if c not in columns_set]\n df = df[columns]\n df.to_hdf(output_store, key)\n row_count = len(df)\n print('%s: %s' % (key, len(df)))\n\n dataset_ds.close_df_store()\n return Outputs(data=dataset_ds)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"input_ds","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{\n 'columns': 'deal_number,close',\n 'reverse_select': false\n}\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"data","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-539"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-539"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-539"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-539","OutputType":null},{"Name":"data_2","NodeId":"-539","OutputType":null},{"Name":"data_3","NodeId":"-539","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":19,"Comment":"","CommentCollapsed":true},{"Id":"-185","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"2018-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2018-02-01","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-185"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-185"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-185","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-23","ModuleId":"BigQuantSpace.select_columns.select_columns-v2","ModuleParameters":[{"Name":"columns","Value":"date,close","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"reverse_select","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-23"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-23","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='-539' Position='1292,119,200,200'/><NodePosition Node='-185' Position='1235,-3,200,200'/><NodePosition Node='-23' Position='1021.3095703125,264.7436218261719,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [9]:
    # 本代码由可视化策略环境自动生成 2018年8月4日 00:12
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m4 = M.use_datasource.v1(
        datasource_id='bar1d_CN_STOCK_A',
        start_date='2018-01-01',
        end_date='2018-02-01'
    )
    
    # 修改数据列名
    def m19_run_bigquant_run(input_ds, columns, reverse_select):
        columns = columns.strip().split(',')
        print('列:', columns)
        # 输出数据源
        dataset_ds = DataSource()
        output_store = dataset_ds.open_df_store()
    
        for key, df in input_ds.iter_df():
            curr_column_set = set(df.columns)
            for col in columns:
                if col not in curr_column_set:
                    print('警告:列 %s 不存在' % col)
            if reverse_select:
                columns_set = set(columns)
                columns = [c for c in df.columns if c not in columns_set]
            df = df[columns]
            df.to_hdf(output_store, key)
            row_count = len(df)
            print('%s: %s' % (key, len(df)))
    
        dataset_ds.close_df_store()
        return Outputs(data=dataset_ds)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m19_post_run_bigquant_run(outputs):
        return outputs
    
    m19 = M.cached.v3(
        input_1=m4.data,
        run=m19_run_bigquant_run,
        post_run=m19_post_run_bigquant_run,
        input_ports='input_ds',
        params="""{
        'columns': 'deal_number,close',
        'reverse_select': false
    }
    """,
        output_ports='data'
    )
    
    m1 = M.select_columns.v2(
        input_ds=m4.data,
        columns='date,close',
        reverse_select=False
    )
    
    [2018-08-04 00:12:44.810100] INFO: bigquant: use_datasource.v1 开始运行..
    [2018-08-04 00:12:44.816291] INFO: bigquant: 命中缓存
    [2018-08-04 00:12:44.817747] INFO: bigquant: use_datasource.v1 运行完成[0.007664s].
    [2018-08-04 00:12:44.823876] INFO: bigquant: cached.v3 开始运行..
    [2018-08-04 00:12:44.830291] INFO: bigquant: 命中缓存
    [2018-08-04 00:12:44.831651] INFO: bigquant: cached.v3 运行完成[0.007814s].
    [2018-08-04 00:12:44.835039] INFO: bigquant: select_columns.v2 开始运行..
    [2018-08-04 00:12:44.839420] INFO: bigquant: 命中缓存
    [2018-08-04 00:12:44.840623] INFO: bigquant: select_columns.v2 运行完成[0.005563s].
    
    In [10]:
    m19.data.read_df().head()
    
    Out[10]:
    deal_number close
    0 12795 42.973991
    1 54576 9.993798
    2 5385 234.071014
    3 9120 129.114807
    4 6859 269.347412