stouputils 1.14.3__py3-none-any.whl → 1.15.1__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.
- stouputils/data_science/config/get.py +51 -51
- stouputils/data_science/data_processing/image/__init__.py +66 -66
- stouputils/data_science/data_processing/image/auto_contrast.py +79 -79
- stouputils/data_science/data_processing/image/axis_flip.py +58 -58
- stouputils/data_science/data_processing/image/bias_field_correction.py +74 -74
- stouputils/data_science/data_processing/image/binary_threshold.py +73 -73
- stouputils/data_science/data_processing/image/blur.py +59 -59
- stouputils/data_science/data_processing/image/brightness.py +54 -54
- stouputils/data_science/data_processing/image/canny.py +110 -110
- stouputils/data_science/data_processing/image/clahe.py +92 -92
- stouputils/data_science/data_processing/image/common.py +30 -30
- stouputils/data_science/data_processing/image/contrast.py +53 -53
- stouputils/data_science/data_processing/image/curvature_flow_filter.py +74 -74
- stouputils/data_science/data_processing/image/denoise.py +378 -378
- stouputils/data_science/data_processing/image/histogram_equalization.py +123 -123
- stouputils/data_science/data_processing/image/invert.py +64 -64
- stouputils/data_science/data_processing/image/laplacian.py +60 -60
- stouputils/data_science/data_processing/image/median_blur.py +52 -52
- stouputils/data_science/data_processing/image/noise.py +59 -59
- stouputils/data_science/data_processing/image/normalize.py +65 -65
- stouputils/data_science/data_processing/image/random_erase.py +66 -66
- stouputils/data_science/data_processing/image/resize.py +69 -69
- stouputils/data_science/data_processing/image/rotation.py +80 -80
- stouputils/data_science/data_processing/image/salt_pepper.py +68 -68
- stouputils/data_science/data_processing/image/sharpening.py +55 -55
- stouputils/data_science/data_processing/image/shearing.py +64 -64
- stouputils/data_science/data_processing/image/threshold.py +64 -64
- stouputils/data_science/data_processing/image/translation.py +71 -71
- stouputils/data_science/data_processing/image/zoom.py +83 -83
- stouputils/data_science/data_processing/image_augmentation.py +118 -118
- stouputils/data_science/data_processing/image_preprocess.py +183 -183
- stouputils/data_science/data_processing/prosthesis_detection.py +359 -359
- stouputils/data_science/data_processing/technique.py +481 -481
- stouputils/data_science/dataset/__init__.py +45 -45
- stouputils/data_science/dataset/dataset.py +292 -292
- stouputils/data_science/dataset/dataset_loader.py +135 -135
- stouputils/data_science/dataset/grouping_strategy.py +296 -296
- stouputils/data_science/dataset/image_loader.py +100 -100
- stouputils/data_science/dataset/xy_tuple.py +696 -696
- stouputils/data_science/metric_dictionnary.py +106 -106
- stouputils/data_science/mlflow_utils.py +206 -206
- stouputils/data_science/models/abstract_model.py +149 -149
- stouputils/data_science/models/all.py +85 -85
- stouputils/data_science/models/keras/all.py +38 -38
- stouputils/data_science/models/keras/convnext.py +62 -62
- stouputils/data_science/models/keras/densenet.py +50 -50
- stouputils/data_science/models/keras/efficientnet.py +60 -60
- stouputils/data_science/models/keras/mobilenet.py +56 -56
- stouputils/data_science/models/keras/resnet.py +52 -52
- stouputils/data_science/models/keras/squeezenet.py +233 -233
- stouputils/data_science/models/keras/vgg.py +42 -42
- stouputils/data_science/models/keras/xception.py +38 -38
- stouputils/data_science/models/keras_utils/callbacks/__init__.py +20 -20
- stouputils/data_science/models/keras_utils/callbacks/colored_progress_bar.py +219 -219
- stouputils/data_science/models/keras_utils/callbacks/learning_rate_finder.py +148 -148
- stouputils/data_science/models/keras_utils/callbacks/model_checkpoint_v2.py +31 -31
- stouputils/data_science/models/keras_utils/callbacks/progressive_unfreezing.py +249 -249
- stouputils/data_science/models/keras_utils/callbacks/warmup_scheduler.py +66 -66
- stouputils/data_science/models/keras_utils/losses/__init__.py +12 -12
- stouputils/data_science/models/keras_utils/losses/next_generation_loss.py +56 -56
- stouputils/data_science/models/keras_utils/visualizations.py +416 -416
- stouputils/data_science/models/sandbox.py +116 -116
- stouputils/data_science/range_tuple.py +234 -234
- stouputils/data_science/utils.py +285 -285
- stouputils/decorators.py +53 -39
- stouputils/decorators.pyi +2 -2
- stouputils/installer/__init__.py +18 -18
- stouputils/installer/linux.py +144 -144
- stouputils/installer/main.py +223 -223
- stouputils/installer/windows.py +136 -136
- stouputils/io.py +16 -9
- stouputils/parallel.py +88 -2
- stouputils/parallel.pyi +21 -1
- stouputils/print.py +229 -2
- stouputils/print.pyi +90 -1
- stouputils/py.typed +1 -1
- {stouputils-1.14.3.dist-info → stouputils-1.15.1.dist-info}/METADATA +1 -1
- {stouputils-1.14.3.dist-info → stouputils-1.15.1.dist-info}/RECORD +80 -80
- {stouputils-1.14.3.dist-info → stouputils-1.15.1.dist-info}/WHEEL +1 -1
- {stouputils-1.14.3.dist-info → stouputils-1.15.1.dist-info}/entry_points.txt +0 -0
|
@@ -1,234 +1,234 @@
|
|
|
1
|
-
"""
|
|
2
|
-
This module contains the RangeTuple class, which provides a named tuple for range parameters.
|
|
3
|
-
|
|
4
|
-
This class contains methods for:
|
|
5
|
-
|
|
6
|
-
- Iterating over range values
|
|
7
|
-
- Accessing range values by index
|
|
8
|
-
- Slicing range values
|
|
9
|
-
- Converting to string representation
|
|
10
|
-
"""
|
|
11
|
-
# pyright: reportUnknownMemberType=false
|
|
12
|
-
# pyright: reportUnknownVariableType=false
|
|
13
|
-
# pyright: reportIncompatibleMethodOverride=false
|
|
14
|
-
|
|
15
|
-
# Imports
|
|
16
|
-
from __future__ import annotations
|
|
17
|
-
|
|
18
|
-
from collections.abc import Generator
|
|
19
|
-
from typing import Any, NamedTuple
|
|
20
|
-
|
|
21
|
-
import numpy as np
|
|
22
|
-
|
|
23
|
-
from .utils import Utils
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
# Create base tuple class
|
|
27
|
-
class _RangeTupleBase(NamedTuple):
|
|
28
|
-
""" Base class for RangeTuple """
|
|
29
|
-
mini: float | None
|
|
30
|
-
""" The minimum value (inclusive) (can be None if default is set) """
|
|
31
|
-
maxi: float | None
|
|
32
|
-
""" The maximum value (exclusive) (can be None if default is set) """
|
|
33
|
-
step: float | None
|
|
34
|
-
""" The step value between elements (can be None if default is set) """
|
|
35
|
-
default: float | None
|
|
36
|
-
""" Optional default value, usually middle of range """
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
# Tuple class for range parameters
|
|
40
|
-
class RangeTuple(_RangeTupleBase):
|
|
41
|
-
""" A named tuple containing range parameters.
|
|
42
|
-
|
|
43
|
-
Attributes:
|
|
44
|
-
mini (float): The minimum value (inclusive) (can be None if default is set)
|
|
45
|
-
maxi (float): The maximum value (exclusive) (can be None if default is set)
|
|
46
|
-
step (float): The step value between elements (can be None if default is set)
|
|
47
|
-
default (float|None): Optional default value, usually middle of range
|
|
48
|
-
|
|
49
|
-
Examples:
|
|
50
|
-
>>> r = RangeTuple(mini=0.0, maxi=1.0, step=0.3)
|
|
51
|
-
>>> print(r)
|
|
52
|
-
mini=0.0, maxi=1.0, step=0.3, default=None
|
|
53
|
-
>>> [int(x*10) for x in r]
|
|
54
|
-
[0, 3, 6, 9]
|
|
55
|
-
>>> len(r)
|
|
56
|
-
4
|
|
57
|
-
>>> r[0]
|
|
58
|
-
0.0
|
|
59
|
-
>>> r[100], r[99] # High indexes will bypass the maximum value
|
|
60
|
-
(30.0, 29.7)
|
|
61
|
-
>>> r[1:3]
|
|
62
|
-
[0.3, 0.6]
|
|
63
|
-
>>> round(r[-2], 1)
|
|
64
|
-
0.6
|
|
65
|
-
>>> r = RangeTuple()
|
|
66
|
-
Traceback (most recent call last):
|
|
67
|
-
...
|
|
68
|
-
ValueError: RangeTuple parameters must not be None
|
|
69
|
-
"""
|
|
70
|
-
def __new__(
|
|
71
|
-
cls,
|
|
72
|
-
mini: float | None = None,
|
|
73
|
-
maxi: float | None = None,
|
|
74
|
-
step: float | None = 1.0,
|
|
75
|
-
default: float | None = None
|
|
76
|
-
) -> RangeTuple:
|
|
77
|
-
if (mini is None or maxi is None):
|
|
78
|
-
if default is None:
|
|
79
|
-
raise ValueError("RangeTuple parameters must not be None")
|
|
80
|
-
else:
|
|
81
|
-
step = None
|
|
82
|
-
return super().__new__(cls, mini, maxi, step, default)
|
|
83
|
-
|
|
84
|
-
def __str__(self) -> str:
|
|
85
|
-
return f"mini={self.mini}, maxi={self.maxi}, step={self.step}, default={self.default}"
|
|
86
|
-
|
|
87
|
-
def __repr__(self) -> str:
|
|
88
|
-
return f"RangeTuple(mini={self.mini!r}, maxi={self.maxi!r}, step={self.step!r}, default={self.default!r})"
|
|
89
|
-
|
|
90
|
-
def __iter__(self) -> Generator[float, Any, Any]:
|
|
91
|
-
""" Iterate over the range values.
|
|
92
|
-
If the range is not initialized (mini or maxi is None), yield the default value.
|
|
93
|
-
Else, yield from np.arange(...)
|
|
94
|
-
|
|
95
|
-
Returns:
|
|
96
|
-
Iterator[float]: Iterator over the range values
|
|
97
|
-
|
|
98
|
-
Examples:
|
|
99
|
-
>>> r = RangeTuple(mini=0.0, maxi=1.0, step=0.5)
|
|
100
|
-
>>> list(r)
|
|
101
|
-
[0.0, 0.5]
|
|
102
|
-
>>> r = RangeTuple(default=1.0)
|
|
103
|
-
>>> list(r)
|
|
104
|
-
[1.0]
|
|
105
|
-
"""
|
|
106
|
-
if self.mini is None or self.maxi is None or self.step is None and self.default is not None:
|
|
107
|
-
yield float(self.default) # pyright: ignore [reportArgumentType]
|
|
108
|
-
else:
|
|
109
|
-
yield from [float(x) for x in np.arange(self.mini, self.maxi, self.step)]
|
|
110
|
-
|
|
111
|
-
def __len__(self) -> int:
|
|
112
|
-
""" Return the number of values in the range.
|
|
113
|
-
|
|
114
|
-
Returns:
|
|
115
|
-
int: Number of values in the range
|
|
116
|
-
|
|
117
|
-
Examples:
|
|
118
|
-
>>> len(RangeTuple(mini=0.0, maxi=1.0, step=0.5))
|
|
119
|
-
3
|
|
120
|
-
>>> len(RangeTuple(default=1.0))
|
|
121
|
-
1
|
|
122
|
-
"""
|
|
123
|
-
if self.mini is None or self.maxi is None or self.step is None:
|
|
124
|
-
return 1
|
|
125
|
-
else:
|
|
126
|
-
return int((self.maxi - self.mini) / self.step) + 1
|
|
127
|
-
|
|
128
|
-
def __getitem__(self, index: int | slice) -> float | list[float]:
|
|
129
|
-
""" Get value(s) at the given index or slice.
|
|
130
|
-
If the range is not initialized, return the default value.
|
|
131
|
-
|
|
132
|
-
Args:
|
|
133
|
-
index (int | slice): Index or slice to get values for
|
|
134
|
-
Returns:
|
|
135
|
-
float | list[float]: Value(s) at the specified index/slice
|
|
136
|
-
|
|
137
|
-
Examples:
|
|
138
|
-
>>> r = RangeTuple(mini=0.0, maxi=1.0, step=0.5)
|
|
139
|
-
>>> r[0]
|
|
140
|
-
0.0
|
|
141
|
-
>>> r[1]
|
|
142
|
-
0.5
|
|
143
|
-
>>> r[-1]
|
|
144
|
-
1.0
|
|
145
|
-
>>> r[0:2]
|
|
146
|
-
[0.0, 0.5]
|
|
147
|
-
>>> r = RangeTuple(default=1.0)
|
|
148
|
-
>>> r[0]
|
|
149
|
-
1.0
|
|
150
|
-
>>> r[1]
|
|
151
|
-
1.0
|
|
152
|
-
"""
|
|
153
|
-
if self.mini is None or self.maxi is None or self.step is None:
|
|
154
|
-
if self.default is not None:
|
|
155
|
-
return self.default
|
|
156
|
-
else:
|
|
157
|
-
raise ValueError("RangeTuple is not initialized")
|
|
158
|
-
else:
|
|
159
|
-
if isinstance(index, slice):
|
|
160
|
-
# Handle None values in slice by using defaults
|
|
161
|
-
start: int = 0 if index.start is None else index.start
|
|
162
|
-
stop: int = len(self) if index.stop is None else index.stop
|
|
163
|
-
step: int = 1 if index.step is None else index.step
|
|
164
|
-
|
|
165
|
-
return [self.mini + i * self.step for i in range(start, stop, step)]
|
|
166
|
-
else:
|
|
167
|
-
while index < 0:
|
|
168
|
-
index = len(self) + index
|
|
169
|
-
return float(self.mini + index * self.step)
|
|
170
|
-
|
|
171
|
-
def __mul__(self, other: float) -> RangeTuple:
|
|
172
|
-
""" Multiply the range by a factor.
|
|
173
|
-
|
|
174
|
-
Args:
|
|
175
|
-
other (float): Factor to multiply by
|
|
176
|
-
Returns:
|
|
177
|
-
RangeTuple: New range with all values multiplied by the factor
|
|
178
|
-
|
|
179
|
-
Examples:
|
|
180
|
-
>>> r = RangeTuple(mini=1.0, maxi=2.0, step=0.5)
|
|
181
|
-
>>> r * 2
|
|
182
|
-
RangeTuple(mini=2.0, maxi=4.0, step=1.0, default=None)
|
|
183
|
-
>>> r = RangeTuple(default=1.0)
|
|
184
|
-
>>> r * 3
|
|
185
|
-
RangeTuple(mini=None, maxi=None, step=None, default=3.0)
|
|
186
|
-
"""
|
|
187
|
-
return RangeTuple(
|
|
188
|
-
mini=Utils.safe_multiply_none(self.mini, other),
|
|
189
|
-
maxi=Utils.safe_multiply_none(self.maxi, other),
|
|
190
|
-
step=Utils.safe_multiply_none(self.step, other),
|
|
191
|
-
default=Utils.safe_multiply_none(self.default, other)
|
|
192
|
-
)
|
|
193
|
-
|
|
194
|
-
def __truediv__(self, other: float) -> RangeTuple:
|
|
195
|
-
""" Divide the range by a factor.
|
|
196
|
-
|
|
197
|
-
Args:
|
|
198
|
-
other (float): Factor to divide by
|
|
199
|
-
Returns:
|
|
200
|
-
RangeTuple: New range with all values divided by the factor
|
|
201
|
-
|
|
202
|
-
Examples:
|
|
203
|
-
>>> r = RangeTuple(mini=2.0, maxi=4.0, step=1.0)
|
|
204
|
-
>>> r / 2
|
|
205
|
-
RangeTuple(mini=1.0, maxi=2.0, step=0.5, default=None)
|
|
206
|
-
>>> r = RangeTuple(default=6.0)
|
|
207
|
-
>>> r / 3
|
|
208
|
-
RangeTuple(mini=None, maxi=None, step=None, default=2.0)
|
|
209
|
-
"""
|
|
210
|
-
return RangeTuple(
|
|
211
|
-
mini=Utils.safe_divide_none(self.mini, other),
|
|
212
|
-
maxi=Utils.safe_divide_none(self.maxi, other),
|
|
213
|
-
step=Utils.safe_divide_none(self.step, other),
|
|
214
|
-
default=Utils.safe_divide_none(self.default, other)
|
|
215
|
-
)
|
|
216
|
-
|
|
217
|
-
def random(self) -> float:
|
|
218
|
-
""" Return a random value from the range.
|
|
219
|
-
If the range is not initialized, return the default value.
|
|
220
|
-
|
|
221
|
-
Returns:
|
|
222
|
-
float: Random value from the range
|
|
223
|
-
|
|
224
|
-
Examples:
|
|
225
|
-
>>> r = RangeTuple(mini=0.0, maxi=1.0, step=1.0)
|
|
226
|
-
>>> 0.0 <= r.random() <= 1.0
|
|
227
|
-
True
|
|
228
|
-
>>> r = RangeTuple(default=1.0)
|
|
229
|
-
>>> r.random()
|
|
230
|
-
1.0
|
|
231
|
-
"""
|
|
232
|
-
index = np.random.randint(0, len(self))
|
|
233
|
-
return self.__getitem__(index) # pyright: ignore [reportReturnType]
|
|
234
|
-
|
|
1
|
+
"""
|
|
2
|
+
This module contains the RangeTuple class, which provides a named tuple for range parameters.
|
|
3
|
+
|
|
4
|
+
This class contains methods for:
|
|
5
|
+
|
|
6
|
+
- Iterating over range values
|
|
7
|
+
- Accessing range values by index
|
|
8
|
+
- Slicing range values
|
|
9
|
+
- Converting to string representation
|
|
10
|
+
"""
|
|
11
|
+
# pyright: reportUnknownMemberType=false
|
|
12
|
+
# pyright: reportUnknownVariableType=false
|
|
13
|
+
# pyright: reportIncompatibleMethodOverride=false
|
|
14
|
+
|
|
15
|
+
# Imports
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from collections.abc import Generator
|
|
19
|
+
from typing import Any, NamedTuple
|
|
20
|
+
|
|
21
|
+
import numpy as np
|
|
22
|
+
|
|
23
|
+
from .utils import Utils
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# Create base tuple class
|
|
27
|
+
class _RangeTupleBase(NamedTuple):
|
|
28
|
+
""" Base class for RangeTuple """
|
|
29
|
+
mini: float | None
|
|
30
|
+
""" The minimum value (inclusive) (can be None if default is set) """
|
|
31
|
+
maxi: float | None
|
|
32
|
+
""" The maximum value (exclusive) (can be None if default is set) """
|
|
33
|
+
step: float | None
|
|
34
|
+
""" The step value between elements (can be None if default is set) """
|
|
35
|
+
default: float | None
|
|
36
|
+
""" Optional default value, usually middle of range """
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# Tuple class for range parameters
|
|
40
|
+
class RangeTuple(_RangeTupleBase):
|
|
41
|
+
""" A named tuple containing range parameters.
|
|
42
|
+
|
|
43
|
+
Attributes:
|
|
44
|
+
mini (float): The minimum value (inclusive) (can be None if default is set)
|
|
45
|
+
maxi (float): The maximum value (exclusive) (can be None if default is set)
|
|
46
|
+
step (float): The step value between elements (can be None if default is set)
|
|
47
|
+
default (float|None): Optional default value, usually middle of range
|
|
48
|
+
|
|
49
|
+
Examples:
|
|
50
|
+
>>> r = RangeTuple(mini=0.0, maxi=1.0, step=0.3)
|
|
51
|
+
>>> print(r)
|
|
52
|
+
mini=0.0, maxi=1.0, step=0.3, default=None
|
|
53
|
+
>>> [int(x*10) for x in r]
|
|
54
|
+
[0, 3, 6, 9]
|
|
55
|
+
>>> len(r)
|
|
56
|
+
4
|
|
57
|
+
>>> r[0]
|
|
58
|
+
0.0
|
|
59
|
+
>>> r[100], r[99] # High indexes will bypass the maximum value
|
|
60
|
+
(30.0, 29.7)
|
|
61
|
+
>>> r[1:3]
|
|
62
|
+
[0.3, 0.6]
|
|
63
|
+
>>> round(r[-2], 1)
|
|
64
|
+
0.6
|
|
65
|
+
>>> r = RangeTuple()
|
|
66
|
+
Traceback (most recent call last):
|
|
67
|
+
...
|
|
68
|
+
ValueError: RangeTuple parameters must not be None
|
|
69
|
+
"""
|
|
70
|
+
def __new__(
|
|
71
|
+
cls,
|
|
72
|
+
mini: float | None = None,
|
|
73
|
+
maxi: float | None = None,
|
|
74
|
+
step: float | None = 1.0,
|
|
75
|
+
default: float | None = None
|
|
76
|
+
) -> RangeTuple:
|
|
77
|
+
if (mini is None or maxi is None):
|
|
78
|
+
if default is None:
|
|
79
|
+
raise ValueError("RangeTuple parameters must not be None")
|
|
80
|
+
else:
|
|
81
|
+
step = None
|
|
82
|
+
return super().__new__(cls, mini, maxi, step, default)
|
|
83
|
+
|
|
84
|
+
def __str__(self) -> str:
|
|
85
|
+
return f"mini={self.mini}, maxi={self.maxi}, step={self.step}, default={self.default}"
|
|
86
|
+
|
|
87
|
+
def __repr__(self) -> str:
|
|
88
|
+
return f"RangeTuple(mini={self.mini!r}, maxi={self.maxi!r}, step={self.step!r}, default={self.default!r})"
|
|
89
|
+
|
|
90
|
+
def __iter__(self) -> Generator[float, Any, Any]:
|
|
91
|
+
""" Iterate over the range values.
|
|
92
|
+
If the range is not initialized (mini or maxi is None), yield the default value.
|
|
93
|
+
Else, yield from np.arange(...)
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
Iterator[float]: Iterator over the range values
|
|
97
|
+
|
|
98
|
+
Examples:
|
|
99
|
+
>>> r = RangeTuple(mini=0.0, maxi=1.0, step=0.5)
|
|
100
|
+
>>> list(r)
|
|
101
|
+
[0.0, 0.5]
|
|
102
|
+
>>> r = RangeTuple(default=1.0)
|
|
103
|
+
>>> list(r)
|
|
104
|
+
[1.0]
|
|
105
|
+
"""
|
|
106
|
+
if self.mini is None or self.maxi is None or self.step is None and self.default is not None:
|
|
107
|
+
yield float(self.default) # pyright: ignore [reportArgumentType]
|
|
108
|
+
else:
|
|
109
|
+
yield from [float(x) for x in np.arange(self.mini, self.maxi, self.step)]
|
|
110
|
+
|
|
111
|
+
def __len__(self) -> int:
|
|
112
|
+
""" Return the number of values in the range.
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
int: Number of values in the range
|
|
116
|
+
|
|
117
|
+
Examples:
|
|
118
|
+
>>> len(RangeTuple(mini=0.0, maxi=1.0, step=0.5))
|
|
119
|
+
3
|
|
120
|
+
>>> len(RangeTuple(default=1.0))
|
|
121
|
+
1
|
|
122
|
+
"""
|
|
123
|
+
if self.mini is None or self.maxi is None or self.step is None:
|
|
124
|
+
return 1
|
|
125
|
+
else:
|
|
126
|
+
return int((self.maxi - self.mini) / self.step) + 1
|
|
127
|
+
|
|
128
|
+
def __getitem__(self, index: int | slice) -> float | list[float]:
|
|
129
|
+
""" Get value(s) at the given index or slice.
|
|
130
|
+
If the range is not initialized, return the default value.
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
index (int | slice): Index or slice to get values for
|
|
134
|
+
Returns:
|
|
135
|
+
float | list[float]: Value(s) at the specified index/slice
|
|
136
|
+
|
|
137
|
+
Examples:
|
|
138
|
+
>>> r = RangeTuple(mini=0.0, maxi=1.0, step=0.5)
|
|
139
|
+
>>> r[0]
|
|
140
|
+
0.0
|
|
141
|
+
>>> r[1]
|
|
142
|
+
0.5
|
|
143
|
+
>>> r[-1]
|
|
144
|
+
1.0
|
|
145
|
+
>>> r[0:2]
|
|
146
|
+
[0.0, 0.5]
|
|
147
|
+
>>> r = RangeTuple(default=1.0)
|
|
148
|
+
>>> r[0]
|
|
149
|
+
1.0
|
|
150
|
+
>>> r[1]
|
|
151
|
+
1.0
|
|
152
|
+
"""
|
|
153
|
+
if self.mini is None or self.maxi is None or self.step is None:
|
|
154
|
+
if self.default is not None:
|
|
155
|
+
return self.default
|
|
156
|
+
else:
|
|
157
|
+
raise ValueError("RangeTuple is not initialized")
|
|
158
|
+
else:
|
|
159
|
+
if isinstance(index, slice):
|
|
160
|
+
# Handle None values in slice by using defaults
|
|
161
|
+
start: int = 0 if index.start is None else index.start
|
|
162
|
+
stop: int = len(self) if index.stop is None else index.stop
|
|
163
|
+
step: int = 1 if index.step is None else index.step
|
|
164
|
+
|
|
165
|
+
return [self.mini + i * self.step for i in range(start, stop, step)]
|
|
166
|
+
else:
|
|
167
|
+
while index < 0:
|
|
168
|
+
index = len(self) + index
|
|
169
|
+
return float(self.mini + index * self.step)
|
|
170
|
+
|
|
171
|
+
def __mul__(self, other: float) -> RangeTuple:
|
|
172
|
+
""" Multiply the range by a factor.
|
|
173
|
+
|
|
174
|
+
Args:
|
|
175
|
+
other (float): Factor to multiply by
|
|
176
|
+
Returns:
|
|
177
|
+
RangeTuple: New range with all values multiplied by the factor
|
|
178
|
+
|
|
179
|
+
Examples:
|
|
180
|
+
>>> r = RangeTuple(mini=1.0, maxi=2.0, step=0.5)
|
|
181
|
+
>>> r * 2
|
|
182
|
+
RangeTuple(mini=2.0, maxi=4.0, step=1.0, default=None)
|
|
183
|
+
>>> r = RangeTuple(default=1.0)
|
|
184
|
+
>>> r * 3
|
|
185
|
+
RangeTuple(mini=None, maxi=None, step=None, default=3.0)
|
|
186
|
+
"""
|
|
187
|
+
return RangeTuple(
|
|
188
|
+
mini=Utils.safe_multiply_none(self.mini, other),
|
|
189
|
+
maxi=Utils.safe_multiply_none(self.maxi, other),
|
|
190
|
+
step=Utils.safe_multiply_none(self.step, other),
|
|
191
|
+
default=Utils.safe_multiply_none(self.default, other)
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
def __truediv__(self, other: float) -> RangeTuple:
|
|
195
|
+
""" Divide the range by a factor.
|
|
196
|
+
|
|
197
|
+
Args:
|
|
198
|
+
other (float): Factor to divide by
|
|
199
|
+
Returns:
|
|
200
|
+
RangeTuple: New range with all values divided by the factor
|
|
201
|
+
|
|
202
|
+
Examples:
|
|
203
|
+
>>> r = RangeTuple(mini=2.0, maxi=4.0, step=1.0)
|
|
204
|
+
>>> r / 2
|
|
205
|
+
RangeTuple(mini=1.0, maxi=2.0, step=0.5, default=None)
|
|
206
|
+
>>> r = RangeTuple(default=6.0)
|
|
207
|
+
>>> r / 3
|
|
208
|
+
RangeTuple(mini=None, maxi=None, step=None, default=2.0)
|
|
209
|
+
"""
|
|
210
|
+
return RangeTuple(
|
|
211
|
+
mini=Utils.safe_divide_none(self.mini, other),
|
|
212
|
+
maxi=Utils.safe_divide_none(self.maxi, other),
|
|
213
|
+
step=Utils.safe_divide_none(self.step, other),
|
|
214
|
+
default=Utils.safe_divide_none(self.default, other)
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
def random(self) -> float:
|
|
218
|
+
""" Return a random value from the range.
|
|
219
|
+
If the range is not initialized, return the default value.
|
|
220
|
+
|
|
221
|
+
Returns:
|
|
222
|
+
float: Random value from the range
|
|
223
|
+
|
|
224
|
+
Examples:
|
|
225
|
+
>>> r = RangeTuple(mini=0.0, maxi=1.0, step=1.0)
|
|
226
|
+
>>> 0.0 <= r.random() <= 1.0
|
|
227
|
+
True
|
|
228
|
+
>>> r = RangeTuple(default=1.0)
|
|
229
|
+
>>> r.random()
|
|
230
|
+
1.0
|
|
231
|
+
"""
|
|
232
|
+
index = np.random.randint(0, len(self))
|
|
233
|
+
return self.__getitem__(index) # pyright: ignore [reportReturnType]
|
|
234
|
+
|