code-loader 1.0.94.dev11__py3-none-any.whl → 1.0.153.dev3__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.
Potentially problematic release.
This version of code-loader might be problematic. Click here for more details.
- code_loader/__init__.py +6 -0
- code_loader/contract/datasetclasses.py +30 -11
- code_loader/contract/mapping.py +11 -0
- code_loader/inner_leap_binder/leapbinder.py +21 -3
- code_loader/inner_leap_binder/leapbinder_decorators.py +1368 -147
- code_loader/leaploader.py +65 -25
- code_loader/leaploaderbase.py +30 -5
- code_loader/mixpanel_tracker.py +230 -0
- code_loader/plot_functions/__init__.py +0 -0
- code_loader/plot_functions/plot_functions.py +444 -0
- code_loader/plot_functions/visualize.py +22 -0
- code_loader/utils.py +6 -6
- code_loader/visualizers/default_visualizers.py +3 -0
- {code_loader-1.0.94.dev11.dist-info → code_loader-1.0.153.dev3.dist-info}/METADATA +6 -3
- {code_loader-1.0.94.dev11.dist-info → code_loader-1.0.153.dev3.dist-info}/RECORD +17 -13
- {code_loader-1.0.94.dev11.dist-info → code_loader-1.0.153.dev3.dist-info}/LICENSE +0 -0
- {code_loader-1.0.94.dev11.dist-info → code_loader-1.0.153.dev3.dist-info}/WHEEL +0 -0
code_loader/__init__.py
CHANGED
|
@@ -2,3 +2,9 @@ from code_loader.leaploader import LeapLoader
|
|
|
2
2
|
from code_loader.inner_leap_binder import global_leap_binder as leap_binder
|
|
3
3
|
from code_loader.experiment_api.experiment import init_experiment
|
|
4
4
|
from code_loader.experiment_api.client import Client
|
|
5
|
+
|
|
6
|
+
try:
|
|
7
|
+
from code_loader.mixpanel_tracker import track_code_loader_loaded
|
|
8
|
+
track_code_loader_loaded({'event_type': 'module_import'})
|
|
9
|
+
except Exception:
|
|
10
|
+
pass
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import warnings
|
|
1
2
|
from dataclasses import dataclass, field
|
|
2
|
-
from typing import Any, Callable, List, Optional, Dict, Union, Type
|
|
3
|
+
from typing import Any, Callable, List, Optional, Dict, Union, Type, Literal
|
|
3
4
|
import re
|
|
4
5
|
import numpy as np
|
|
5
6
|
import numpy.typing as npt
|
|
@@ -41,11 +42,7 @@ class PreprocessResponse:
|
|
|
41
42
|
sample_ids_to_instance_mappings: Optional[Dict[str, List[str]]] = None # in use only for element instance
|
|
42
43
|
instance_to_sample_ids_mappings: Optional[Dict[str, str]] = None # in use only for element instance
|
|
43
44
|
|
|
44
|
-
|
|
45
45
|
def __post_init__(self) -> None:
|
|
46
|
-
def is_valid_string(s: str) -> bool:
|
|
47
|
-
return bool(re.match(r'^[A-Za-z0-9_]+$', s))
|
|
48
|
-
|
|
49
46
|
assert self.sample_ids_to_instance_mappings is None, f"Keep sample_ids_to_instance_mappings None when initializing PreprocessResponse"
|
|
50
47
|
assert self.instance_to_sample_ids_mappings is None, f"Keep instance_to_sample_ids_mappings None when initializing PreprocessResponse"
|
|
51
48
|
|
|
@@ -59,10 +56,20 @@ class PreprocessResponse:
|
|
|
59
56
|
if self.sample_id_type == str:
|
|
60
57
|
for sample_id in self.sample_ids:
|
|
61
58
|
assert isinstance(sample_id, str), f"Sample id should be of type str. Got: {type(sample_id)}"
|
|
62
|
-
if not is_valid_string(sample_id):
|
|
63
|
-
raise Exception(f"Sample id should contain only letters (A-Z, a-z), numbers or '_'. Got: {sample_id}")
|
|
64
59
|
else:
|
|
65
|
-
raise Exception("length is deprecated.")
|
|
60
|
+
raise Exception("length is deprecated, please use sample_ids instead.")
|
|
61
|
+
|
|
62
|
+
if self.state is None:
|
|
63
|
+
from code_loader.inner_leap_binder.leapbinder_decorators import store_warning_by_param
|
|
64
|
+
store_warning_by_param(
|
|
65
|
+
param_name="PreprocessResponse.state",
|
|
66
|
+
user_func_name="tensorleap_preprocess",
|
|
67
|
+
default_value=str("specific order"),
|
|
68
|
+
link_to_docs="https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code/preprocess-function",
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
else:
|
|
72
|
+
assert isinstance(self.state, DataStateType), f"PreprocessResponse.state must be of type {DataStateType.__name__} but got {type(self.state)}"
|
|
66
73
|
|
|
67
74
|
def __hash__(self) -> int:
|
|
68
75
|
return id(self)
|
|
@@ -75,9 +82,13 @@ class PreprocessResponse:
|
|
|
75
82
|
class ElementInstance:
|
|
76
83
|
name: str
|
|
77
84
|
mask: npt.NDArray[np.float32]
|
|
85
|
+
# instance_metadata: Dict[str, Union[Optional[str], int, bool, Optional[float]]]
|
|
86
|
+
|
|
78
87
|
|
|
79
88
|
SectionCallableInterface = Callable[[Union[int, str], PreprocessResponse], npt.NDArray[np.float32]]
|
|
80
|
-
InstanceCallableInterface = Callable[[Union[int, str], PreprocessResponse],
|
|
89
|
+
InstanceCallableInterface = Callable[[Union[int, str], PreprocessResponse, int], Optional[ElementInstance]]
|
|
90
|
+
InstanceLengthCallableInterface = Callable[[Union[int, str], PreprocessResponse], int]
|
|
91
|
+
|
|
81
92
|
|
|
82
93
|
MetadataSectionCallableInterface = Union[
|
|
83
94
|
Callable[[Union[int, str], PreprocessResponse], int],
|
|
@@ -216,11 +227,17 @@ class MetadataHandler:
|
|
|
216
227
|
metadata_type: Optional[Union[DatasetMetadataType, Dict[str, DatasetMetadataType]]] = None
|
|
217
228
|
|
|
218
229
|
|
|
230
|
+
@dataclass
|
|
231
|
+
class CustomLatentSpaceHandler:
|
|
232
|
+
function: SectionCallableInterface
|
|
233
|
+
name: str = 'custom_latent_space'
|
|
234
|
+
|
|
219
235
|
@dataclass
|
|
220
236
|
class PredictionTypeHandler:
|
|
221
237
|
name: str
|
|
222
238
|
labels: List[str]
|
|
223
|
-
channel_dim: int
|
|
239
|
+
channel_dim: Union[int, Literal["tl_default_value"]]= "tl_default_value"
|
|
240
|
+
|
|
224
241
|
|
|
225
242
|
|
|
226
243
|
@dataclass
|
|
@@ -245,6 +262,7 @@ class DatasetIntegrationSetup:
|
|
|
245
262
|
custom_loss_handlers: List[CustomLossHandler] = field(default_factory=list)
|
|
246
263
|
metrics: List[MetricHandler] = field(default_factory=list)
|
|
247
264
|
custom_layers: Dict[str, CustomLayerHandler] = field(default_factory=dict)
|
|
265
|
+
custom_latent_space: Optional[CustomLatentSpaceHandler] = None
|
|
248
266
|
|
|
249
267
|
|
|
250
268
|
@dataclass
|
|
@@ -255,5 +273,6 @@ class DatasetSample:
|
|
|
255
273
|
metadata_is_none: Dict[str, bool]
|
|
256
274
|
index: Union[int, str]
|
|
257
275
|
state: DataStateEnum
|
|
258
|
-
|
|
276
|
+
custom_latent_space: Optional[npt.NDArray[np.float32]] = None
|
|
277
|
+
instance_masks: Optional[Dict[str, ElementInstance]] = None
|
|
259
278
|
|
code_loader/contract/mapping.py
CHANGED
|
@@ -21,12 +21,22 @@ class NodeMappingType(Enum):
|
|
|
21
21
|
Prediction1 = 'Prediction1'
|
|
22
22
|
Prediction2 = 'Prediction2'
|
|
23
23
|
Prediction3 = 'Prediction3'
|
|
24
|
+
Prediction4 = 'Prediction4'
|
|
25
|
+
Prediction5 = 'Prediction5'
|
|
26
|
+
Prediction6 = 'Prediction6'
|
|
27
|
+
Prediction7 = 'Prediction7'
|
|
28
|
+
Prediction8 = 'Prediction8'
|
|
29
|
+
Prediction9 = 'Prediction9'
|
|
24
30
|
Input0 = 'Input0'
|
|
25
31
|
Input1 = 'Input1'
|
|
26
32
|
Input2 = 'Input2'
|
|
27
33
|
Input3 = 'Input3'
|
|
28
34
|
Input4 = 'Input4'
|
|
29
35
|
Input5 = 'Input5'
|
|
36
|
+
Input6 = 'Input6'
|
|
37
|
+
Input7 = 'Input7'
|
|
38
|
+
Input8 = 'Input8'
|
|
39
|
+
Input9 = 'Input9'
|
|
30
40
|
PredictionLabels = 'PredictionLabels'
|
|
31
41
|
|
|
32
42
|
|
|
@@ -39,6 +49,7 @@ class NodeMapping:
|
|
|
39
49
|
arg_names: Optional[List[str]] = None
|
|
40
50
|
|
|
41
51
|
|
|
52
|
+
|
|
42
53
|
@dataclass
|
|
43
54
|
class NodeConnection:
|
|
44
55
|
node: NodeMapping
|
|
@@ -11,7 +11,7 @@ from code_loader.contract.datasetclasses import SectionCallableInterface, InputH
|
|
|
11
11
|
CustomCallableInterfaceMultiArgs, ConfusionMatrixCallableInterfaceMultiArgs, LeapData, \
|
|
12
12
|
CustomMultipleReturnCallableInterfaceMultiArgs, DatasetBaseHandler, custom_latent_space_attribute, \
|
|
13
13
|
RawInputsForHeatmap, VisualizerHandlerData, MetricHandlerData, CustomLossHandlerData, SamplePreprocessResponse, \
|
|
14
|
-
ElementInstanceMasksHandler, InstanceCallableInterface
|
|
14
|
+
ElementInstanceMasksHandler, InstanceCallableInterface, CustomLatentSpaceHandler
|
|
15
15
|
from code_loader.contract.enums import LeapDataType, DataStateEnum, DataStateType, MetricDirection, DatasetMetadataType
|
|
16
16
|
from code_loader.contract.mapping import NodeConnection, NodeMapping, NodeMappingType
|
|
17
17
|
from code_loader.contract.responsedataclasses import DatasetTestResultPayload
|
|
@@ -25,6 +25,9 @@ from code_loader.visualizers.default_visualizers import DefaultVisualizer, \
|
|
|
25
25
|
default_image_mask_visualizer, default_text_mask_visualizer, default_raw_data_visualizer, default_video_visualizer
|
|
26
26
|
|
|
27
27
|
|
|
28
|
+
mapping_runtime_mode_env_var_mame = '__MAPPING_RUNTIME_MODE__'
|
|
29
|
+
|
|
30
|
+
|
|
28
31
|
class LeapBinder:
|
|
29
32
|
"""
|
|
30
33
|
Interface to the Tensorleap platform. Provides methods to set up preprocessing,
|
|
@@ -46,6 +49,7 @@ class LeapBinder:
|
|
|
46
49
|
self._extend_with_default_losses()
|
|
47
50
|
|
|
48
51
|
self.mapping_connections: List[NodeConnection] = []
|
|
52
|
+
self.integration_test_func: Optional[Callable[[str, PreprocessResponse], Any]] = None
|
|
49
53
|
|
|
50
54
|
self.batch_size_to_validate: Optional[int] = None
|
|
51
55
|
|
|
@@ -417,6 +421,19 @@ class LeapBinder:
|
|
|
417
421
|
"""
|
|
418
422
|
self.setup_container.metadata.append(MetadataHandler(name, function, metadata_type))
|
|
419
423
|
|
|
424
|
+
def set_custom_latent_space(self, function: SectionCallableInterface) -> None:
|
|
425
|
+
"""
|
|
426
|
+
Set a custom latent space function.
|
|
427
|
+
|
|
428
|
+
Args:
|
|
429
|
+
function (SectionCallableInterface): The metadata handler function.
|
|
430
|
+
This function receives:
|
|
431
|
+
subset (PreprocessResponse): The subset of the data.
|
|
432
|
+
index (int): The index of the sample within the subset.
|
|
433
|
+
This function should numpy float32 array contains the latent space vec of the sample.
|
|
434
|
+
"""
|
|
435
|
+
self.setup_container.custom_latent_space = CustomLatentSpaceHandler(function)
|
|
436
|
+
|
|
420
437
|
def set_custom_layer(self, custom_layer: Type[Any], name: str, inspect_layer: bool = False,
|
|
421
438
|
kernel_index: Optional[int] = None, use_custom_latent_space: bool = False) -> None:
|
|
422
439
|
"""
|
|
@@ -530,9 +547,10 @@ class LeapBinder:
|
|
|
530
547
|
for i, (single_metadata_name, single_metadata_result) in enumerate(raw_result.items()):
|
|
531
548
|
metadata_test_result = metadata_test_result_payloads[i]
|
|
532
549
|
|
|
533
|
-
if not isinstance(single_metadata_result, (int, str, bool, float, np.unsignedinteger,
|
|
550
|
+
if not isinstance(single_metadata_result, (int, str, bool, float, np.unsignedinteger,
|
|
551
|
+
np.signedinteger, np.bool_, np.floating, type(None))):
|
|
534
552
|
raise Exception(f"Unsupported return type of metadata {single_metadata_name}."
|
|
535
|
-
f"The return type should be one of [int, float, str, bool]. Got {type(single_metadata_result)}")
|
|
553
|
+
f"The return type should be one of [int, float, str, bool, None]. Got {type(single_metadata_result)}")
|
|
536
554
|
|
|
537
555
|
metadata_type = None
|
|
538
556
|
if single_metadata_result is None:
|