code-loader 1.0.190__py3-none-any.whl → 1.0.191.dev0__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.
@@ -277,7 +277,7 @@ class DatasetIntegrationSetup:
277
277
  metrics: List[MetricHandler] = field(default_factory=list)
278
278
  instance_metrics: List[InstanceMetricHandler] = field(default_factory=list)
279
279
  custom_layers: Dict[str, CustomLayerHandler] = field(default_factory=dict)
280
- custom_latent_space: Optional[CustomLatentSpaceHandler] = None
280
+ custom_latent_spaces: Dict[str, CustomLatentSpaceHandler] = field(default_factory=dict)
281
281
  simulations: List[SimulationHandler] = field(default_factory=list)
282
282
 
283
283
 
@@ -289,6 +289,6 @@ class DatasetSample:
289
289
  metadata_is_none: Dict[str, bool]
290
290
  index: Union[int, str]
291
291
  state: DataStateEnum
292
- custom_latent_space: Optional[npt.NDArray[np.float32]] = None
292
+ custom_latent_spaces: Optional[Dict[str, npt.NDArray[np.float32]]] = None
293
293
  instance_masks: Optional[Dict[str, ElementInstance]] = None
294
294
 
@@ -511,18 +511,34 @@ class LeapBinder:
511
511
  """
512
512
  self.setup_container.metadata.append(MetadataHandler(name, function, metadata_type))
513
513
 
514
- def set_custom_latent_space(self, function: SectionCallableInterface) -> None:
514
+ def set_custom_latent_space(self, function: SectionCallableInterface,
515
+ name: Optional[str] = None) -> None:
515
516
  """
516
- Set a custom latent space function.
517
+ Register a custom latent space function.
518
+
519
+ Multiple custom latent spaces may be registered as long as each has a
520
+ unique name. They are stored name-keyed (insertion order preserved), so a
521
+ later registration no longer overrides an earlier one.
517
522
 
518
523
  Args:
519
- function (SectionCallableInterface): The metadata handler function.
524
+ function (SectionCallableInterface): The latent-space handler function.
520
525
  This function receives:
521
526
  subset (PreprocessResponse): The subset of the data.
522
527
  index (int): The index of the sample within the subset.
523
- This function should numpy float32 array contains the latent space vec of the sample.
528
+ This function should return a numpy float32 array containing the latent
529
+ space vec of the sample.
530
+ name (Optional[str]): Unique name for this custom latent space. Defaults to
531
+ the reserved single-LS name for backward compatibility.
524
532
  """
525
- self.setup_container.custom_latent_space = CustomLatentSpaceHandler(function)
533
+ if name is None:
534
+ name = custom_latent_space_attribute
535
+ if name in self.setup_container.custom_latent_spaces:
536
+ raise Exception(
537
+ f"A custom latent space named '{name}' is already registered. Each "
538
+ f"@tensorleap_custom_latent_space must have a unique name "
539
+ f"(pass name='...' to distinguish them)."
540
+ )
541
+ self.setup_container.custom_latent_spaces[name] = CustomLatentSpaceHandler(function, name)
526
542
 
527
543
  def set_custom_layer(self, custom_layer: Type[Any], name: str, inspect_layer: bool = False,
528
544
  kernel_index: Optional[int] = None, use_custom_latent_space: bool = False) -> None:
@@ -1568,8 +1568,9 @@ def tensorleap_metadata(
1568
1568
  return decorating_function
1569
1569
 
1570
1570
 
1571
- def tensorleap_custom_latent_space():
1571
+ def tensorleap_custom_latent_space(name: Optional[str] = None):
1572
1572
  def decorating_function(user_function: SectionCallableInterface):
1573
+ ls_name = name if name is not None else user_function.__name__
1573
1574
  def _validate_input_args(sample_id: Union[int, str], preprocess_response: PreprocessResponse):
1574
1575
  assert isinstance(sample_id, (int, str)), \
1575
1576
  (f'tensorleap_custom_latent_space validation failed: '
@@ -1589,9 +1590,9 @@ def tensorleap_custom_latent_space():
1589
1590
  if result.ndim > 1:
1590
1591
  flat_dim = int(np.prod(result.shape))
1591
1592
  store_general_warning(
1592
- key=("tensorleap_custom_latent_space_flatten", tuple(result.shape)),
1593
+ key=("tensorleap_custom_latent_space_flatten", ls_name, tuple(result.shape)),
1593
1594
  message=(
1594
- f"tensorleap_custom_latent_space returned per-sample shape {tuple(result.shape)} "
1595
+ f"tensorleap_custom_latent_space '{ls_name}' returned per-sample shape {tuple(result.shape)} "
1595
1596
  f"(ndim={result.ndim}). Tensorleap assumes per-sample shape (d, ...) and will "
1596
1597
  f"flatten to ({flat_dim},) before downstream visualization and clustering. "
1597
1598
  f"If you want a different aggregation (e.g. global average pooling), do it "
@@ -1610,7 +1611,7 @@ def tensorleap_custom_latent_space():
1610
1611
 
1611
1612
  return result
1612
1613
 
1613
- leap_binder.set_custom_latent_space(inner_without_validate)
1614
+ leap_binder.set_custom_latent_space(inner_without_validate, ls_name)
1614
1615
 
1615
1616
  def inner(sample_id, preprocess_response):
1616
1617
  if os.environ.get(mapping_runtime_mode_env_var_mame):
code_loader/leaploader.py CHANGED
@@ -251,11 +251,7 @@ class LeapLoader(LeapLoaderBase):
251
251
 
252
252
  metadata, metadata_is_none = self.get_metadata(state, sample_id)
253
253
 
254
- custom_latent_space = None
255
- if global_leap_binder.setup_container.custom_latent_space is not None:
256
- custom_latent_space = global_leap_binder.setup_container.custom_latent_space.function(sample_id,
257
- preprocess_result[
258
- state])
254
+ custom_latent_spaces = self._get_custom_latent_spaces(sample_id, preprocess_result[state])
259
255
  instance_mask = self._get_instances_masks(state, sample_id, instance_id)
260
256
  sample = DatasetSample(inputs=self._get_inputs(state, sample_id),
261
257
  gt=None if state == DataStateEnum.unlabeled else self._get_gt(state, sample_id),
@@ -263,7 +259,7 @@ class LeapLoader(LeapLoaderBase):
263
259
  metadata_is_none=metadata_is_none,
264
260
  index=sample_id,
265
261
  state=state,
266
- custom_latent_space=custom_latent_space,
262
+ custom_latent_spaces=custom_latent_spaces,
267
263
  instance_masks=instance_mask)
268
264
  return sample
269
265
 
@@ -294,11 +290,7 @@ class LeapLoader(LeapLoaderBase):
294
290
  handler.name, handler_result
295
291
  )
296
292
 
297
- custom_latent_space = None
298
- if global_leap_binder.setup_container.custom_latent_space is not None:
299
- custom_latent_space = global_leap_binder.setup_container.custom_latent_space.function(
300
- original_sample_id, preprocess
301
- )
293
+ custom_latent_spaces = self._get_custom_latent_spaces(original_sample_id, preprocess)
302
294
 
303
295
  return DatasetSample(
304
296
  inputs=inputs,
@@ -307,7 +299,7 @@ class LeapLoader(LeapLoaderBase):
307
299
  metadata_is_none=metadata_is_none,
308
300
  index=synthetic_index,
309
301
  state=DataStateEnum.additional,
310
- custom_latent_space=custom_latent_space,
302
+ custom_latent_spaces=custom_latent_spaces,
311
303
  instance_masks=None,
312
304
  )
313
305
 
@@ -874,10 +866,29 @@ class LeapLoader(LeapLoaderBase):
874
866
 
875
867
  return id_type
876
868
 
869
+ @staticmethod
870
+ def _get_custom_latent_spaces(
871
+ sample_id: Union[int, str],
872
+ preprocess: "PreprocessResponse") -> Optional[Dict[str, npt.NDArray[np.float32]]]:
873
+ handlers = global_leap_binder.setup_container.custom_latent_spaces
874
+ if not handlers:
875
+ return None
876
+ return {name: handler.function(sample_id, preprocess) for name, handler in handlers.items()}
877
+
877
878
  @lru_cache()
878
879
  def has_custom_latent_space_decorator(self) -> bool:
879
880
  self.exec_script()
880
- return global_leap_binder.setup_container.custom_latent_space is not None
881
+ return len(global_leap_binder.setup_container.custom_latent_spaces) > 0
882
+
883
+ @lru_cache()
884
+ def get_custom_latent_space_names(self) -> Tuple[str, ...]:
885
+ """Ordered names of all registered custom latent spaces.
886
+
887
+ Registration order is the canonical index mapping consumed by the engine
888
+ (name i -> user_custom_i). Returns a tuple so the lru_cache value is hashable.
889
+ """
890
+ self.exec_script()
891
+ return tuple(global_leap_binder.setup_container.custom_latent_spaces.keys())
881
892
 
882
893
  def get_instances_data(self, state: DataStateEnum) -> Tuple[Dict[str, List[str]], Dict[str, str]]:
883
894
  preprocess_result = self._preprocess_result()
@@ -149,6 +149,10 @@ class LeapLoaderBase:
149
149
  def has_custom_latent_space_decorator(self) -> bool:
150
150
  pass
151
151
 
152
+ @abstractmethod
153
+ def get_custom_latent_space_names(self) -> Tuple[str, ...]:
154
+ pass
155
+
152
156
  @abstractmethod
153
157
  def get_heatmap_visualizer_raw_vis_input_arg_name(self, visualizer_name: str) -> Optional[str]:
154
158
  pass
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: code-loader
3
- Version: 1.0.190
3
+ Version: 1.0.191.dev0
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=outxRQ0M-zMfV0QGVJmAed5qWfRmyD0TV6-goEGAzBw,406
3
3
  code_loader/contract/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- code_loader/contract/datasetclasses.py,sha256=RYZcnX7vLMYhyQtCbX2PscVFNAYULxgAA9U4DRRfLqA,10456
4
+ code_loader/contract/datasetclasses.py,sha256=obtB9Zhzyw241NxerCDn2PkF2MBvcaSZdwHaSMBoM-g,10493
5
5
  code_loader/contract/enums.py,sha256=2q-IV_5g9lLE306DIbWA1c0tn5IhDtxsKxyV1x_Lreg,1671
6
6
  code_loader/contract/exceptions.py,sha256=jWqu5i7t-0IG0jGRsKF4DjJdrsdpJjIYpUkN1F4RiyQ,51
7
7
  code_loader/contract/mapping.py,sha256=sWJhpng-IkOzQnWQdMT5w2ZZ3X1Z_OOzSwCLXIS7oxE,1446
@@ -21,10 +21,10 @@ code_loader/experiment_api/types.py,sha256=MY8xFARHwdVA7p4dxyhD60ShmttgTvb4qdp1o
21
21
  code_loader/experiment_api/utils.py,sha256=XZHtxge12TS4H4-8PjV3sKuhp8Ud6ojAiIzTZJEqBqc,3304
22
22
  code_loader/experiment_api/workingspace_config_utils.py,sha256=DLzXQCg4dgTV_YgaSbeTVzq-2ja_SQw4zi7LXwKL9cY,990
23
23
  code_loader/inner_leap_binder/__init__.py,sha256=koOlJyMNYzGbEsoIbXathSmQ-L38N_pEXH_HvL7beXU,99
24
- code_loader/inner_leap_binder/leapbinder.py,sha256=PAX9cmT1QQV7PWsbY9rMRWktpM7KryfFlwBiSwSskzg,42654
25
- code_loader/inner_leap_binder/leapbinder_decorators.py,sha256=k7_kJaEHEuoKWAGRUDXqlZmR1C228xA_Ny9mmliYKys,122098
26
- code_loader/leaploader.py,sha256=9bUl69X5pk6prgFMCnaoA4MhRoacDb9Ddv3CWgG1F0Y,45968
27
- code_loader/leaploaderbase.py,sha256=l36qDA00GhZEG5NLKpEtAXgWJA-UQQIhNFGxywK7mUA,6530
24
+ code_loader/inner_leap_binder/leapbinder.py,sha256=t51C-6yH-n8X7VG5YCEPjAreKmfdk1kdseUmspC1KW8,43533
25
+ code_loader/inner_leap_binder/leapbinder_decorators.py,sha256=as8zMdVuKFvWVCDSDi6E1IDj6PU4MbnwuHcYoSUNgn0,122225
26
+ code_loader/leaploader.py,sha256=VGzHpAF0X171Oo3uUlhZ4CbLwu99Ve5brcs3R55XLmI,46293
27
+ code_loader/leaploaderbase.py,sha256=sFMvh7axgoRycLTUWFHsF7Yq20nTLl_k6hgWXe95lg8,6628
28
28
  code_loader/mixpanel_tracker.py,sha256=rNwRmFifNbdUoqLQvvhhgpKczWpWiEmd8MfyJe27sxw,9131
29
29
  code_loader/plot_functions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
30
30
  code_loader/plot_functions/plot_functions.py,sha256=2DC-zlVaN13P4VNx5d8csgs80C6SisaeP1-Kq2LW7iM,16075
@@ -32,7 +32,7 @@ code_loader/plot_functions/visualize.py,sha256=gsBAYYkwMh7jIpJeDMPS8G4CW-pxwx6Lz
32
32
  code_loader/utils.py,sha256=YecipkdTA-VcE9F0RQcY9cFnY8P3AksPnHM2Db7xUSk,3972
33
33
  code_loader/visualizers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
34
34
  code_loader/visualizers/default_visualizers.py,sha256=onRnLE_TXfgLN4o52hQIOOhUcFexGlqJ3xSpQDVLuZM,2604
35
- code_loader-1.0.190.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
36
- code_loader-1.0.190.dist-info/METADATA,sha256=Pl7b1dTzYCBphl0mxWCitL0RZ3FnlPfxJhNjEJkcP3k,1090
37
- code_loader-1.0.190.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
38
- code_loader-1.0.190.dist-info/RECORD,,
35
+ code_loader-1.0.191.dev0.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
36
+ code_loader-1.0.191.dev0.dist-info/METADATA,sha256=OoBB3IpPxf_hKlJTCWdZ3C-HtEH1wZ6Z_5_P_joci5c,1095
37
+ code_loader-1.0.191.dev0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
38
+ code_loader-1.0.191.dev0.dist-info/RECORD,,