load feature This function is responsible for loading feature/expression based on the expression engine. The concrete implementation will be separated into two parts: 1) caching data, handle errors. - This part is shared by all the expressions and implemented
(self, instrument, start_index, end_index, *args)
| 140 | return Or(other, self) |
| 141 | |
| 142 | def load(self, instrument, start_index, end_index, *args): |
| 143 | """load feature |
| 144 | This function is responsible for loading feature/expression based on the expression engine. |
| 145 | |
| 146 | The concrete implementation will be separated into two parts: |
| 147 | |
| 148 | 1) caching data, handle errors. |
| 149 | |
| 150 | - This part is shared by all the expressions and implemented in Expression |
| 151 | 2) processing and calculating data based on the specific expression. |
| 152 | |
| 153 | - This part is different in each expression and implemented in each expression |
| 154 | |
| 155 | Expression Engine is shared by different data. |
| 156 | Different data will have different extra information for `args`. |
| 157 | |
| 158 | Parameters |
| 159 | ---------- |
| 160 | instrument : str |
| 161 | instrument code. |
| 162 | start_index : str |
| 163 | feature start index [in calendar]. |
| 164 | end_index : str |
| 165 | feature end index [in calendar]. |
| 166 | |
| 167 | *args may contain following information: |
| 168 | 1) if it is used in basic expression engine data, it contains following arguments |
| 169 | freq: str |
| 170 | feature frequency. |
| 171 | |
| 172 | 2) if is used in PIT data, it contains following arguments |
| 173 | cur_pit: |
| 174 | it is designed for the point-in-time data. |
| 175 | period: int |
| 176 | This is used for query specific period. |
| 177 | The period is represented with int in Qlib. (e.g. 202001 may represent the first quarter in 2020) |
| 178 | |
| 179 | Returns |
| 180 | ---------- |
| 181 | pd.Series |
| 182 | feature series: The index of the series is the calendar index |
| 183 | """ |
| 184 | from .cache import H # pylint: disable=C0415 |
| 185 | |
| 186 | # cache |
| 187 | cache_key = str(self), instrument, start_index, end_index, *args |
| 188 | if cache_key in H["f"]: |
| 189 | return H["f"][cache_key] |
| 190 | if start_index is not None and end_index is not None and start_index > end_index: |
| 191 | raise ValueError("Invalid index range: {} {}".format(start_index, end_index)) |
| 192 | try: |
| 193 | series = self._load_internal(instrument, start_index, end_index, *args) |
| 194 | except Exception as e: |
| 195 | get_module_logger("data").debug( |
| 196 | f"Loading data error: instrument={instrument}, expression={str(self)}, " |
| 197 | f"start_index={start_index}, end_index={end_index}, args={args}. " |
| 198 | f"error info: {str(e)}" |
| 199 | ) |