code-loader 1.0.59__py3-none-any.whl → 1.0.60.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.
@@ -154,7 +154,7 @@ class DatasetBaseHandler:
154
154
  @dataclass
155
155
  class InputHandler(DatasetBaseHandler):
156
156
  shape: Optional[List[int]] = None
157
-
157
+ channel_dim: Optional[int] = -1
158
158
 
159
159
  @dataclass
160
160
  class GroundTruthHandler(DatasetBaseHandler):
@@ -20,6 +20,7 @@ class DatasetBaseSectionInstance:
20
20
  @dataclass
21
21
  class DatasetInputInstance(DatasetBaseSectionInstance):
22
22
  shape: List[int]
23
+ channel_dim: Optional[bool] = -1
23
24
 
24
25
 
25
26
  @dataclass
@@ -181,13 +181,14 @@ class LeapBinder:
181
181
  """
182
182
  self.setup_container.unlabeled_data_preprocess = UnlabeledDataPreprocessHandler(function)
183
183
 
184
- def set_input(self, function: SectionCallableInterface, name: str) -> None:
184
+ def set_input(self, function: SectionCallableInterface, name: str, channel_dim: int = -1) -> None:
185
185
  """
186
186
  Set the input handler function.
187
187
 
188
188
  Args:
189
189
  function (SectionCallableInterface): The input handler function.
190
190
  name (str): The name of the input section.
191
+ channel_dim (int): The dimension of the channels axis
191
192
 
192
193
  Example:
193
194
  def input_encoder(subset: PreprocessResponse, index: int) -> np.ndarray:
@@ -197,10 +198,10 @@ class LeapBinder:
197
198
  img = normalize(img)
198
199
  return img
199
200
 
200
- leap_binder.set_input(input_encoder, name='input_encoder')
201
+ leap_binder.set_input(input_encoder, name='input_encoder', channel_dim=-1)
201
202
  """
202
203
  function = to_numpy_return_wrapper(function)
203
- self.setup_container.inputs.append(InputHandler(name, function))
204
+ self.setup_container.inputs.append(InputHandler(name, function, channel_dim=channel_dim))
204
205
 
205
206
  self._encoder_names.append(name)
206
207
 
@@ -245,14 +245,16 @@ def tensorleap_unlabeled_preprocess():
245
245
  return decorating_function
246
246
 
247
247
 
248
- def tensorleap_input_encoder(name: str):
248
+ def tensorleap_input_encoder(name: str, channel_dim=-1):
249
249
  def decorating_function(user_function: SectionCallableInterface):
250
250
  for input_handler in leap_binder.setup_container.inputs:
251
251
  if input_handler.name == name:
252
252
  raise Exception(f'Input with name {name} already exists. '
253
253
  f'Please choose another')
254
+ if channel_dim < 0 and channel_dim != -1:
255
+ raise Exception(f"Channel dim for input {name} is expected to be either -1 or positive")
254
256
 
255
- leap_binder.set_input(user_function, name)
257
+ leap_binder.set_input(user_function, name, channel_dim=channel_dim)
256
258
 
257
259
  def _validate_input_args(sample_id: Union[int, str], preprocess_response: PreprocessResponse):
258
260
  assert isinstance(sample_id, (int, str)), \
@@ -273,6 +275,8 @@ def tensorleap_input_encoder(name: str):
273
275
  assert result.dtype == np.float32, \
274
276
  (f'tensorleap_input_encoder validation failed: '
275
277
  f'The return type should be a numpy array of type float32. Got {result.dtype}.')
278
+ assert channel_dim - 1 <= len(result.shape), (f'tensorleap_input_encoder validation failed: '
279
+ f'The channel_dim ({channel_dim}) should be <= to the rank of the resulting input rank ({len(result.shape)}).')
276
280
 
277
281
  def inner(sample_id, preprocess_response):
278
282
  _validate_input_args(sample_id, preprocess_response)
code_loader/leaploader.py CHANGED
@@ -221,8 +221,8 @@ class LeapLoader:
221
221
  if global_leap_binder.setup_container.unlabeled_data_preprocess:
222
222
  unlabeled_length = global_leap_binder.setup_container.unlabeled_data_preprocess.data_length
223
223
  dataset_preprocess = DatasetPreprocess(
224
- training_length=setup.preprocess.data_length[DataStateType.training],
225
- validation_length=setup.preprocess.data_length[DataStateType.validation],
224
+ training_length=setup.preprocess.data_length.get(DataStateType.training, 0),
225
+ validation_length=setup.preprocess.data_length.get(DataStateType.validation, 0),
226
226
  test_length=setup.preprocess.data_length.get(DataStateType.test),
227
227
  unlabeled_length=unlabeled_length
228
228
  )
@@ -231,7 +231,7 @@ class LeapLoader:
231
231
  for inp in setup.inputs:
232
232
  if inp.shape is None:
233
233
  raise Exception(f"cant calculate shape for input, input name:{inp.name}")
234
- inputs.append(DatasetInputInstance(name=inp.name, shape=inp.shape))
234
+ inputs.append(DatasetInputInstance(name=inp.name, shape=inp.shape, channel_dim=inp.channel_dim))
235
235
 
236
236
  ground_truths = []
237
237
  for gt in setup.ground_truths:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: code-loader
3
- Version: 1.0.59
3
+ Version: 1.0.60.dev1
4
4
  Summary:
5
5
  Home-page: https://github.com/tensorleap/code-loader
6
6
  License: MIT
@@ -1,11 +1,10 @@
1
1
  LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
2
2
  code_loader/__init__.py,sha256=6MMWr0ObOU7hkqQKgOqp4Zp3I28L7joGC9iCbQYtAJg,241
3
- code_loader/code_inegration_processes_manager.py,sha256=XslWOPeNQk4RAFJ_f3tP5Oe3EgcIR7BE7Y8r9Ty73-o,3261
4
3
  code_loader/contract/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- code_loader/contract/datasetclasses.py,sha256=lFS7_weizsjzx4_tYwYGrrRUj1sgIl010h9FON4brb8,6670
4
+ code_loader/contract/datasetclasses.py,sha256=cd6fRDC4XLrJa7PcrzoTPIKtGwFZq09DGwiSC5BSKvk,6705
6
5
  code_loader/contract/enums.py,sha256=6Lo7p5CUog68Fd31bCozIuOgIp_IhSiPqWWph2k3OGU,1602
7
6
  code_loader/contract/exceptions.py,sha256=jWqu5i7t-0IG0jGRsKF4DjJdrsdpJjIYpUkN1F4RiyQ,51
8
- code_loader/contract/responsedataclasses.py,sha256=w7xVOv2S8Hyb5lqyomMGiKAWXDTSOG-FX1YW39bXD3A,3969
7
+ code_loader/contract/responsedataclasses.py,sha256=1rhpCDSa0yR1DSNi7QpIhYQLCB5X8xgRAxq7fveOwh4,4006
9
8
  code_loader/contract/visualizer_classes.py,sha256=iIa_O2rKvPTwN5ILCTZvRpsGYiiFABKdwQwfIXGigDo,11928
10
9
  code_loader/experiment_api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
10
  code_loader/experiment_api/api.py,sha256=a7wh6Hhe7IaVxu46eV2soSz-yxnmXG3ipU1BBtsEAaQ,2493
@@ -18,13 +17,13 @@ code_loader/experiment_api/types.py,sha256=MY8xFARHwdVA7p4dxyhD60ShmttgTvb4qdp1o
18
17
  code_loader/experiment_api/utils.py,sha256=XZHtxge12TS4H4-8PjV3sKuhp8Ud6ojAiIzTZJEqBqc,3304
19
18
  code_loader/experiment_api/workingspace_config_utils.py,sha256=DLzXQCg4dgTV_YgaSbeTVzq-2ja_SQw4zi7LXwKL9cY,990
20
19
  code_loader/inner_leap_binder/__init__.py,sha256=koOlJyMNYzGbEsoIbXathSmQ-L38N_pEXH_HvL7beXU,99
21
- code_loader/inner_leap_binder/leapbinder.py,sha256=35hyesDdmjOD9wdrTLyayb-vm9aDfmEbMA0c4EQR1LA,25090
22
- code_loader/inner_leap_binder/leapbinder_decorators.py,sha256=uuM_ht9HZ1GH2IabKeGQ_x9NmD3poK_h1Gt0NruwJuY,19704
23
- code_loader/leaploader.py,sha256=POUgD6x1GH_iF_eDGz-VLX4DsIl2kddufKVDdrA_K-U,19491
20
+ code_loader/inner_leap_binder/leapbinder.py,sha256=LVzpynjISO-a774flzGt1yAQPsSYNE8B5V58Hacs7bQ,25216
21
+ code_loader/inner_leap_binder/leapbinder_decorators.py,sha256=6yHYZRFb5wv1dEOV6r75kIN4-cxXtFDbgHL9ciQQ_x4,20131
22
+ code_loader/leaploader.py,sha256=Tpf6A25hYuo4D0umGL3BHNYJhmz_NIwvFveQgAlsSOo,19534
24
23
  code_loader/utils.py,sha256=aw2i_fqW_ADjLB66FWZd9DfpCQ7mPdMyauROC5Nd51I,2197
25
24
  code_loader/visualizers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
26
25
  code_loader/visualizers/default_visualizers.py,sha256=VoqO9FN84yXyMjRjHjUTOt2GdTkJRMbHbXJ1cJkREkk,2230
27
- code_loader-1.0.59.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
28
- code_loader-1.0.59.dist-info/METADATA,sha256=F3b6BUv0E3jFu6ciMsIu0EzSPQMlnP1aKKG7GGSTu0I,888
29
- code_loader-1.0.59.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
30
- code_loader-1.0.59.dist-info/RECORD,,
26
+ code_loader-1.0.60.dev1.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
27
+ code_loader-1.0.60.dev1.dist-info/METADATA,sha256=oturZf0OhySd6ULF2OY6V4iEe4xDCA5V7IphGHsKIJ8,893
28
+ code_loader-1.0.60.dev1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
29
+ code_loader-1.0.60.dev1.dist-info/RECORD,,
@@ -1,83 +0,0 @@
1
- # mypy: ignore-errors
2
- import traceback
3
- from dataclasses import dataclass
4
-
5
- from typing import List, Tuple, Optional
6
-
7
- from multiprocessing import Process, Queue
8
-
9
- from code_loader.leap_loader_parallelized_base import LeapLoaderParallelizedBase
10
- from code_loader.leaploader import LeapLoader
11
- from code_loader.contract.enums import DataStateEnum
12
- from code_loader.metric_calculator_parallelized import MetricCalculatorParallelized
13
- from code_loader.samples_generator_parallelized import SamplesGeneratorParallelized
14
-
15
-
16
- @dataclass
17
- class SampleSerializableError:
18
- state: DataStateEnum
19
- index: int
20
- leap_script_trace: str
21
- exception_as_str: str
22
-
23
-
24
- class CodeIntegrationProcessesManager:
25
- def __init__(self, code_path: str, code_entry_name: str, n_workers: Optional[int] = 2,
26
- max_samples_in_queue: int = 128) -> None:
27
- self.metric_calculator_parallelized = MetricCalculatorParallelized(code_path, code_entry_name)
28
- self.samples_generator_parallelized = SamplesGeneratorParallelized(code_path, code_entry_name)
29
-
30
- def _create_and_start_process(self) -> Process:
31
- process = self.multiprocessing_context.Process(
32
- target=CodeIntegrationProcessesManager._process_func,
33
- args=(self.code_path, self.code_entry_name, self._inputs_waiting_to_be_process,
34
- self._ready_processed_results))
35
- process.daemon = True
36
- process.start()
37
- return process
38
-
39
- def _run_and_warm_first_process(self):
40
- process = self._create_and_start_process()
41
- self.processes = [process]
42
-
43
- # needed in order to make sure the preprocess func runs once in nonparallel
44
- self._start_process_inputs([(DataStateEnum.training, 0)])
45
- self._get_next_ready_processed_result()
46
-
47
- def _operation_decider(self):
48
- if self.metric_calculator_parallelized._ready_processed_results.empty() and not \
49
- self.metric_calculator_parallelized._inputs_waiting_to_be_process.empty():
50
- return 'metric'
51
-
52
- if self.samples_generator_parallelized._ready_processed_results.empty() and not \
53
- self.samples_generator_parallelized._inputs_waiting_to_be_process.empty():
54
- return 'dataset'
55
-
56
-
57
-
58
-
59
- @staticmethod
60
- def _process_func(code_path: str, code_entry_name: str,
61
- samples_to_process: Queue, ready_samples: Queue,
62
- metrics_to_process: Queue, ready_metrics: Queue) -> None:
63
- import os
64
- os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
65
-
66
- leap_loader = LeapLoader(code_path, code_entry_name)
67
- while True:
68
-
69
- # decide on sample or metric to process
70
- state, idx = samples_to_process.get(block=True)
71
- leap_loader._preprocess_result()
72
- try:
73
- sample = leap_loader.get_sample(state, idx)
74
- except Exception as e:
75
- leap_script_trace = traceback.format_exc().split('File "<string>"')[-1]
76
- ready_samples.put(SampleSerializableError(state, idx, leap_script_trace, str(e)))
77
- continue
78
-
79
- ready_samples.put(sample)
80
-
81
- def generate_samples(self, sample_identities: List[Tuple[DataStateEnum, int]]):
82
- return self.start_process_inputs(sample_identities)
83
-