@@ -86,10 +86,61 @@ def begin_task_train(task_config: dict, experiment_name: str, recorder_name: str
8686 return R .get_recorder ()
8787
8888
89+ def get_item_from_obj (config : dict , name_path : str ) -> object :
90+ """
91+ Follow the name_path to get values from config
92+ For example:
93+ If we follow the example in in the Parameters section,
94+ Timestamp('2008-01-02 00:00:00') will be returned
95+
96+ Parameters
97+ ----------
98+ config : dict
99+ e.g.
100+ {'dataset': {'class': 'DatasetH',
101+ 'kwargs': {'handler': {'class': 'Alpha158',
102+ 'kwargs': {'end_time': '2020-08-01',
103+ 'fit_end_time': '<dataset.kwargs.segments.train.1>',
104+ 'fit_start_time': '<dataset.kwargs.segments.train.0>',
105+ 'instruments': 'csi100',
106+ 'start_time': '2008-01-01'},
107+ 'module_path': 'qlib.contrib.data.handler'},
108+ 'segments': {'test': (Timestamp('2017-01-03 00:00:00'),
109+ Timestamp('2019-04-08 00:00:00')),
110+ 'train': (Timestamp('2008-01-02 00:00:00'),
111+ Timestamp('2014-12-31 00:00:00')),
112+ 'valid': (Timestamp('2015-01-05 00:00:00'),
113+ Timestamp('2016-12-30 00:00:00'))}}
114+ }}
115+ name_path : str
116+ e.g.
117+ "dataset.kwargs.segments.train.1"
118+
119+ Returns
120+ -------
121+ object
122+ the retrieved object
123+ """
124+ cur_cfg = config
125+ for k in name_path .split ("." ):
126+ if isinstance (cur_cfg , dict ):
127+ cur_cfg = cur_cfg [k ]
128+ elif k .isdigit ():
129+ cur_cfg = cur_cfg [int (k )]
130+ else :
131+ raise ValueError (f"Error when getting { k } from cur_cfg" )
132+ return cur_cfg
133+
134+
89135def fill_placeholder (config : dict , config_extend : dict ):
90136 """
91137 Detect placeholder in config and fill them with config_extend.
92138 The item of dict must be single item(int, str, etc), dict and list. Tuples are not supported.
139+ There are two type of variables:
140+ - user-defined variables :
141+ e.g. when config_extend is `{"<MODEL>": model, "<DATASET>": dataset}`, "<MODEL>" and "<DATASET>" in `config` will be replaced with `model` `dataset`
142+ - variables extracted from `config` :
143+ e.g. the variables like "<dataset.kwargs.segments.train.0>" will be replaced with the values from `config`
93144
94145 Parameters
95146 ----------
@@ -122,8 +173,13 @@ def fill_placeholder(config: dict, config_extend: dict):
122173 if isinstance (now_item [key ], list ) or isinstance (now_item [key ], dict ):
123174 item_queue .append (now_item [key ])
124175 tail += 1
125- elif isinstance (now_item [key ], str ) and now_item [key ] in config_extend .keys ():
126- now_item [key ] = config_extend [now_item [key ]]
176+ elif isinstance (now_item [key ], str ):
177+ if now_item [key ] in config_extend .keys ():
178+ now_item [key ] = config_extend [now_item [key ]]
179+ else :
180+ m = re .match (r"<(?P<name_path>[^<>]+)>" , now_item [key ])
181+ if m is not None :
182+ now_item [key ] = get_item_from_obj (config , m .groupdict ()["name_path" ])
127183 return config
128184
129185
0 commit comments