code-loader 1.0.48__py3-none-any.whl → 1.0.49.dev1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,8 +32,18 @@ class PreprocessResponse:
32
32
  }
33
33
  response = PreprocessResponse(length=len(preprocessed_data), data=preprocessed_data)
34
34
  """
35
- length: int
36
- data: Any
35
+ length: Optional[int] = None # Deprecated. Please use sample_ids instead
36
+ data: Any = None
37
+ sample_ids: Optional[Union[List[str], List[int]]] = None
38
+ state: Optional[DataStateType] = None
39
+
40
+ def __post_init__(self):
41
+ if self.length is not None and self.sample_ids is None:
42
+ self.sample_ids = [i for i in range(self.length)]
43
+ elif self.length is None and self.sample_ids is not None:
44
+ self.length = len(self.sample_ids)
45
+ else:
46
+ raise Exception("length is deprecated. Please use sample_ids instead.")
37
47
 
38
48
 
39
49
  SectionCallableInterface = Callable[[int, PreprocessResponse], npt.NDArray[np.float32]]
@@ -389,17 +389,36 @@ class LeapBinder:
389
389
  if preprocess is None:
390
390
  raise Exception("Please make sure you call the leap_binder.set_preprocess method")
391
391
  preprocess_results = preprocess.function()
392
- preprocess_result_dict = {
393
- DataStateEnum(i): preprocess_result
394
- for i, preprocess_result in enumerate(preprocess_results)
395
- }
392
+ preprocess_result_dict = {}
393
+ for i, preprocess_result in enumerate(preprocess_results):
394
+ if preprocess_result.state is None:
395
+ state_enum = DataStateEnum(i)
396
+ preprocess_result.state = DataStateType(state_enum.name)
397
+ else:
398
+ state_enum = DataStateEnum[preprocess_result.state.name]
396
399
 
397
- unlabeled_preprocess = self.setup_container.unlabeled_data_preprocess
398
- if unlabeled_preprocess is not None:
399
- preprocess_result_dict[DataStateEnum.unlabeled] = unlabeled_preprocess.function()
400
+ if state_enum in preprocess_result_dict:
401
+ raise Exception(f"Duplicate state {state_enum.name} in preprocess results")
402
+ preprocess_result_dict[state_enum] = preprocess_result
403
+
404
+ if DataStateEnum.unlabeled not in preprocess_result_dict:
405
+ preprocess_unlabeled_result = self.get_preprocess_unlabeled_result()
406
+ if preprocess_unlabeled_result is not None:
407
+ preprocess_result_dict[DataStateEnum.unlabeled] = preprocess_unlabeled_result
408
+
409
+ if DataStateEnum.training not in preprocess_result_dict:
410
+ raise Exception("Training data is required")
411
+ if DataStateEnum.validation not in preprocess_result_dict:
412
+ raise Exception("Validation data is required")
400
413
 
401
414
  return preprocess_result_dict
402
415
 
416
+ def get_preprocess_unlabeled_result(self) -> Optional[PreprocessResponse]:
417
+ unlabeled_preprocess = self.setup_container.unlabeled_data_preprocess
418
+ if unlabeled_preprocess is not None:
419
+ return unlabeled_preprocess.function()
420
+ return None
421
+
403
422
  def _get_all_dataset_base_handlers(self) -> List[Union[DatasetBaseHandler, MetadataHandler]]:
404
423
  all_dataset_base_handlers: List[Union[DatasetBaseHandler, MetadataHandler]] = []
405
424
  all_dataset_base_handlers.extend(self.setup_container.inputs)
code_loader/leaploader.py CHANGED
@@ -2,6 +2,7 @@
2
2
  import importlib.util
3
3
  import io
4
4
  import sys
5
+ import time
5
6
  from contextlib import redirect_stdout
6
7
  from functools import lru_cache
7
8
  from pathlib import Path
@@ -23,10 +24,14 @@ from code_loader.utils import get_root_exception_file_and_line_number
23
24
 
24
25
 
25
26
  class LeapLoader:
26
- def __init__(self, code_path: str, code_entry_name: str):
27
+ def __init__(self, code_path: str, code_entry_name: str, streaming_mode: bool = False):
27
28
  self.code_entry_name = code_entry_name
28
29
  self.code_path = code_path
29
30
 
31
+ self.streaming_mode = streaming_mode
32
+ self._preprocess_result_cached = None
33
+ self._last_updated_unlabeled_preprocess = None
34
+
30
35
  @lru_cache()
31
36
  def exec_script(self) -> None:
32
37
  try:
@@ -279,10 +284,34 @@ class LeapLoader:
279
284
  ]
280
285
  return ModelSetup(custom_layer_instances)
281
286
 
282
- @lru_cache()
283
287
  def _preprocess_result(self) -> Dict[DataStateEnum, PreprocessResponse]:
284
288
  self.exec_script()
285
- return global_leap_binder.get_preprocess_result()
289
+
290
+ if self._preprocess_result_cached is None:
291
+ self._preprocess_result_cached = global_leap_binder.get_preprocess_result()
292
+ self._last_updated_unlabeled_preprocess = time.time()
293
+
294
+ if self.streaming_mode and global_leap_binder.setup_container.unlabeled_data_preprocess is None:
295
+ raise Exception("unlabeled_data_preprocess is not defined in the dataset script and it "
296
+ "most be defined in production monitoring.")
297
+
298
+ update_unlabeled_preprocess_interval = 120
299
+ if (self.streaming_mode and
300
+ time.time() - self._last_updated_unlabeled__preprocess > update_unlabeled_preprocess_interval):
301
+ self._preprocess_result_cached[
302
+ DataStateEnum.unlabeled] = global_leap_binder.get_preprocess_unlabeled_result()
303
+ self._last_updated_unlabeled_preprocess = time.time()
304
+
305
+ return self._preprocess_result_cached
306
+
307
+ def get_preprocess_sample_ids(self) -> Dict[DataStateEnum, Union[List[int], List[str]]]:
308
+ preprocess_result = self._preprocess_result()
309
+ sample_ids = {}
310
+ for state, preprocess_response in preprocess_result.items():
311
+ sample_ids[state] = preprocess_response.sample_ids
312
+
313
+ return sample_ids
314
+
286
315
 
287
316
  def _get_dataset_handlers(self, handlers: Iterable[DatasetBaseHandler],
288
317
  state: DataStateEnum, idx: int) -> Dict[str, npt.NDArray[np.float32]]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: code-loader
3
- Version: 1.0.48
3
+ Version: 1.0.49.dev1
4
4
  Summary:
5
5
  Home-page: https://github.com/tensorleap/code-loader
6
6
  License: MIT
@@ -1,7 +1,7 @@
1
1
  LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
2
2
  code_loader/__init__.py,sha256=6MMWr0ObOU7hkqQKgOqp4Zp3I28L7joGC9iCbQYtAJg,241
3
3
  code_loader/contract/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- code_loader/contract/datasetclasses.py,sha256=81TmCcVol7768lzKUp70MatLLipR3ftcR9jgE1r8Yqo,5698
4
+ code_loader/contract/datasetclasses.py,sha256=ooGKDBix0ZEmTqjgpYLedbu09NhiHOdhr3oJb-KRZG4,6237
5
5
  code_loader/contract/enums.py,sha256=6Lo7p5CUog68Fd31bCozIuOgIp_IhSiPqWWph2k3OGU,1602
6
6
  code_loader/contract/exceptions.py,sha256=jWqu5i7t-0IG0jGRsKF4DjJdrsdpJjIYpUkN1F4RiyQ,51
7
7
  code_loader/contract/responsedataclasses.py,sha256=w7xVOv2S8Hyb5lqyomMGiKAWXDTSOG-FX1YW39bXD3A,3969
@@ -17,12 +17,12 @@ code_loader/experiment_api/types.py,sha256=MY8xFARHwdVA7p4dxyhD60ShmttgTvb4qdp1o
17
17
  code_loader/experiment_api/utils.py,sha256=XZHtxge12TS4H4-8PjV3sKuhp8Ud6ojAiIzTZJEqBqc,3304
18
18
  code_loader/experiment_api/workingspace_config_utils.py,sha256=DLzXQCg4dgTV_YgaSbeTVzq-2ja_SQw4zi7LXwKL9cY,990
19
19
  code_loader/inner_leap_binder/__init__.py,sha256=koOlJyMNYzGbEsoIbXathSmQ-L38N_pEXH_HvL7beXU,99
20
- code_loader/inner_leap_binder/leapbinder.py,sha256=CDiz8AWgNW-TgSPspllUUxy2Q-9V2JcavPJ0CZOAUnU,23888
21
- code_loader/leaploader.py,sha256=E1DHtRDzz0hyb3kAMl6S_JDbjGxJPR_bZHU77Af4rR4,17521
20
+ code_loader/inner_leap_binder/leapbinder.py,sha256=m-9eNKYqK71irfEd4T1bNyM9t8mh_utdUDPYvbK34UY,24902
21
+ code_loader/leaploader.py,sha256=tE1XVLc2odt30KSUwDSrn86Dq53F5bQgMWv-p8w0Kd0,18906
22
22
  code_loader/utils.py,sha256=TZAoUbA2pE8eK3Le3s5Xr4eRaYdeDMQtxotx6rh-5oE,2185
23
23
  code_loader/visualizers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
24
  code_loader/visualizers/default_visualizers.py,sha256=VoqO9FN84yXyMjRjHjUTOt2GdTkJRMbHbXJ1cJkREkk,2230
25
- code_loader-1.0.48.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
26
- code_loader-1.0.48.dist-info/METADATA,sha256=s65JNpeKwOhpvCS8TY1RFkfSEUTDxGCfiBBjy-b7sh8,888
27
- code_loader-1.0.48.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
28
- code_loader-1.0.48.dist-info/RECORD,,
25
+ code_loader-1.0.49.dev1.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
26
+ code_loader-1.0.49.dev1.dist-info/METADATA,sha256=l0my0SEFGL5Nzwh0SW0MJhkCaX9Xiz4eqzSrQ1xnC74,893
27
+ code_loader-1.0.49.dev1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
28
+ code_loader-1.0.49.dev1.dist-info/RECORD,,