MCPcopy Create free account
hub / github.com/FinancialComputingUCL/LOBFrame / __getitem__

Method __getitem__

loaders/custom_dataset.py:266–319  ·  view source on GitHub ↗
(self, index)

Source from the content-addressed store, hash-verified

264 self.cache_indices[self.current_cache_index] = dataset_index
265
266 def __getitem__(self, index):
267 try:
268 dataset_index = 0
269 while index >= self.cumulative_lengths[dataset_index + 1]:
270 dataset_index += 1
271
272 if self.cache_indices[self.current_cache_index] != dataset_index:
273 # Cache the dataset if it's not already cached.
274 self.cache_dataset(dataset_index)
275
276 # Retrieve the un-cumulative index of the current sample.
277 start_index = (
278 index
279 if dataset_index == 0
280 else index - self.cumulative_lengths[dataset_index]
281 )
282
283 if self.lighten:
284 # If the "lighten" option is enabled, we use only the first 5 levels of the orderbook (i.e. 4_level_features * 5_levels = 20_orderbook_features).
285 window_data = self.cache_data[self.current_cache_index][
286 start_index: start_index + self.window_size, :20
287 ]
288 else:
289 # If the "lighten" option is not enabled, we use all the 10 levels of the orderbook (i.e. 4_level_features * 10_levels = 40_orderbook_features).
290 window_data = self.cache_data[self.current_cache_index][
291 start_index: start_index + self.window_size, :40
292 ]
293
294 # Determine the position of the prediction horizon in the list of all horizons.
295 position = next(
296 (
297 index
298 for index, value in enumerate(self.all_horizons)
299 if value == self.prediction_horizon
300 ),
301 None,
302 )
303 # Extract the label from the dataset given its position.
304 label = self.cache_data[self.current_cache_index][
305 start_index + self.window_size, 40:
306 ][position]
307 # Discretize the label using the provided threshold.
308 if self.backtest is False:
309 if label > self.threshold:
310 label = 2
311 elif label < -self.threshold:
312 label = 0
313 else:
314 label = 1
315
316 return torch.tensor(window_data).unsqueeze(0), torch.tensor(label)
317 except Exception as e:
318 print(f"Exception in DataLoader worker: {e}")
319 raise e
320
321
322''&#x27;

Callers

nothing calls this directly

Calls 1

cache_datasetMethod · 0.95

Tested by

no test coverage detected