code-loader 1.0.47__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]]
@@ -42,7 +42,7 @@ class LogExternalEpochDataRequest:
42
42
  experimentId: str
43
43
  epoch: int
44
44
  metrics: ApiMetrics
45
- force: bool = False
45
+ override: bool = False
46
46
 
47
47
  @dataclass
48
48
  class TagModelRequest:
@@ -91,13 +91,14 @@ class Epoch:
91
91
  tags=tags
92
92
  ))
93
93
 
94
- def log(self, modelFilePath: Optional[str] = None, tags: Optional[List[str]] = None) -> None:
94
+ def log(self, modelFilePath: Optional[str] = None, tags: Optional[List[str]] = None, override: bool = False) -> None:
95
95
  """
96
96
  Logs the epoch with optional model file and tags.
97
97
 
98
98
  Args:
99
99
  modelFilePath (Optional[str]): The path to the model file. Defaults to None.
100
100
  tags (Optional[List[str]]): A list of tags to associate with the epoch model. Will always include the default tag. Unless the default tag is set to '', all previous epoch model with the same tag will be removed
101
+ override (bool): Whether to override the existing epoch model. Defaults to False.
101
102
  """
102
103
  if tags is None:
103
104
  tags = []
@@ -119,7 +120,8 @@ class Epoch:
119
120
  experimentId=self.ctx.experiment_id,
120
121
  projectId=self.ctx.project_id,
121
122
  epoch=self.epoch,
122
- metrics=api_metrics
123
+ metrics=api_metrics,
124
+ override=override
123
125
  ))
124
126
  if modelFilePath is not None and len(tags) > 0:
125
127
  self._tag_model(tags)
@@ -41,7 +41,7 @@ class Experiment:
41
41
  """
42
42
  return Epoch(self.ctx, epoch, self.default_epoch_tag)
43
43
 
44
- def log_epoch(self, epoch: int, metrics: Optional[Metrics] = None, model_path: Optional[str] = None, tags: Optional[List[str]] = None) -> None:
44
+ def log_epoch(self, epoch: int, metrics: Optional[Metrics] = None, model_path: Optional[str] = None, tags: Optional[List[str]] = None, override: bool = False) -> None:
45
45
  """
46
46
  Logs an epoch with optional metrics, model path, and tags.
47
47
 
@@ -50,11 +50,12 @@ class Experiment:
50
50
  metrics (Optional[Metrics]): The metrics to log for the epoch. Defaults to None.
51
51
  model_path (Optional[str]): The path to the model file. Defaults to None.
52
52
  tags (Optional[List[str]]): A list of tags to associate with the epoch model. Will always include the default tag. Unless the default tag is set to '', all previous epoch model with the same tag will be removed.
53
+ override (bool): Whether to override the epoch if it already exists. Defaults to False.
53
54
  """
54
55
  epoch_o = self.init_epoch(epoch)
55
56
  if metrics is not None:
56
57
  epoch_o.set_metrics(metrics)
57
- epoch_o.log(model_path, tags)
58
+ epoch_o.log(model_path, tags, override)
58
59
 
59
60
  def set_properties(self, properties: Dict[str, Any]) -> None:
60
61
  """
@@ -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.47
3
+ Version: 1.0.49.dev1
4
4
  Summary:
5
5
  Home-page: https://github.com/tensorleap/code-loader
6
6
  License: MIT
@@ -1,28 +1,28 @@
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
8
8
  code_loader/contract/visualizer_classes.py,sha256=Ka8fJSVKrOeZ12Eg8-dOBGMW0UswYCIkuhnNMd-7z9s,22948
9
9
  code_loader/experiment_api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
- code_loader/experiment_api/api.py,sha256=p_III5t9WCpvd1REq9L-pF3r1RhjDlxMCBBK-Sn0B1I,2490
10
+ code_loader/experiment_api/api.py,sha256=a7wh6Hhe7IaVxu46eV2soSz-yxnmXG3ipU1BBtsEAaQ,2493
11
11
  code_loader/experiment_api/cli_config_utils.py,sha256=n6JMyNrquxql3KKxHhAP8jAzezlRT-PV2KWI95kKsm0,1140
12
12
  code_loader/experiment_api/client.py,sha256=8kqLWh2Zg7mNA5E9Q0v0qRuPm3OE3Rs7lfGAcXOucU4,1428
13
- code_loader/experiment_api/epoch.py,sha256=q46cxVfpautynKL-ju0YS83vYhpuZuscLA0nff748y0,4700
14
- code_loader/experiment_api/experiment.py,sha256=oVi6GAJ1e1kOygnOquTvb6SgepMhTlrpH0uVqCwviYM,6349
13
+ code_loader/experiment_api/epoch.py,sha256=mcrdgwbUgG8hcq6Nthp7bNl5KAVZ5727_ICasJp7thQ,4849
14
+ code_loader/experiment_api/experiment.py,sha256=vyE2zyY9JcVKuHcIA8Njr5DWlmbP2cyJHnGAsnaOots,6483
15
15
  code_loader/experiment_api/experiment_context.py,sha256=kdzUbuzXo1pMVslOC3TKeJwW8sx_qWkxDVrswjduH0A,194
16
16
  code_loader/experiment_api/types.py,sha256=MY8xFARHwdVA7p4dxyhD60ShmttgTvb4qdp1oEB_NPg,485
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.47.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
26
- code_loader-1.0.47.dist-info/METADATA,sha256=bDWLdxS53o7nfne8b-M9OOGy5Rf-AqnKshHY6XtNYTM,888
27
- code_loader-1.0.47.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
28
- code_loader-1.0.47.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,,