masster 0.2.5__py3-none-any.whl → 0.3.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.
Potentially problematic release.
This version of masster might be problematic. Click here for more details.
- masster/__init__.py +27 -27
- masster/_version.py +17 -17
- masster/chromatogram.py +497 -503
- masster/data/examples/2025_01_14_VW_7600_LpMx_DBS_CID_2min_TOP15_030msecMS1_005msecReac_CE35_DBS-ON_3.featureXML +199787 -0
- masster/data/examples/2025_01_14_VW_7600_LpMx_DBS_CID_2min_TOP15_030msecMS1_005msecReac_CE35_DBS-ON_3.sample5 +0 -0
- masster/logger.py +318 -244
- masster/sample/__init__.py +9 -9
- masster/sample/defaults/__init__.py +15 -15
- masster/sample/defaults/find_adducts_def.py +325 -325
- masster/sample/defaults/find_features_def.py +366 -366
- masster/sample/defaults/find_ms2_def.py +285 -285
- masster/sample/defaults/get_spectrum_def.py +314 -318
- masster/sample/defaults/sample_def.py +374 -378
- masster/sample/h5.py +1321 -1297
- masster/sample/helpers.py +833 -364
- masster/sample/lib.py +762 -0
- masster/sample/load.py +1220 -1187
- masster/sample/parameters.py +131 -131
- masster/sample/plot.py +1685 -1622
- masster/sample/processing.py +1402 -1416
- masster/sample/quant.py +209 -0
- masster/sample/sample.py +393 -387
- masster/sample/sample5_schema.json +181 -181
- masster/sample/save.py +737 -736
- masster/sample/sciex.py +1213 -0
- masster/spectrum.py +1287 -1319
- masster/study/__init__.py +9 -9
- masster/study/defaults/__init__.py +21 -19
- masster/study/defaults/align_def.py +267 -267
- masster/study/defaults/export_def.py +41 -40
- masster/study/defaults/fill_chrom_def.py +264 -264
- masster/study/defaults/fill_def.py +260 -0
- masster/study/defaults/find_consensus_def.py +256 -256
- masster/study/defaults/find_ms2_def.py +163 -163
- masster/study/defaults/integrate_chrom_def.py +225 -225
- masster/study/defaults/integrate_def.py +221 -0
- masster/study/defaults/merge_def.py +256 -0
- masster/study/defaults/study_def.py +272 -269
- masster/study/export.py +674 -287
- masster/study/h5.py +1406 -886
- masster/study/helpers.py +1713 -433
- masster/study/helpers_optimized.py +317 -0
- masster/study/load.py +1231 -1078
- masster/study/parameters.py +99 -99
- masster/study/plot.py +632 -645
- masster/study/processing.py +1057 -1046
- masster/study/save.py +161 -134
- masster/study/study.py +612 -522
- masster/study/study5_schema.json +253 -241
- {masster-0.2.5.dist-info → masster-0.3.1.dist-info}/METADATA +15 -10
- masster-0.3.1.dist-info/RECORD +59 -0
- {masster-0.2.5.dist-info → masster-0.3.1.dist-info}/licenses/LICENSE +661 -661
- masster-0.2.5.dist-info/RECORD +0 -50
- {masster-0.2.5.dist-info → masster-0.3.1.dist-info}/WHEEL +0 -0
- {masster-0.2.5.dist-info → masster-0.3.1.dist-info}/entry_points.txt +0 -0
|
@@ -1,318 +1,314 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Get Spectrum Parameters Module
|
|
3
|
-
|
|
4
|
-
This module defines parameters for spectrum retrieval and processing in mass spectrometry data.
|
|
5
|
-
It consolidates all parameters used in the get_spectrum() method with type checking,
|
|
6
|
-
validation, and comprehensive descriptions.
|
|
7
|
-
|
|
8
|
-
Classes:
|
|
9
|
-
get_spectrum_defaults: Configuration parameters for the get_spectrum() method.
|
|
10
|
-
"""
|
|
11
|
-
|
|
12
|
-
from __future__ import annotations
|
|
13
|
-
|
|
14
|
-
from dataclasses import dataclass, field
|
|
15
|
-
from typing import Any
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
@dataclass
|
|
19
|
-
class get_spectrum_defaults:
|
|
20
|
-
"""
|
|
21
|
-
Parameters for spectrum retrieval and processing.
|
|
22
|
-
|
|
23
|
-
This class consolidates all parameters used in the get_spectrum() method including
|
|
24
|
-
scan identification, spectrum processing options, and optional parameters.
|
|
25
|
-
It provides type checking, validation, and comprehensive parameter descriptions.
|
|
26
|
-
|
|
27
|
-
Spectrum Processing Parameters:
|
|
28
|
-
scan: Unique identifier of the scan to retrieve.
|
|
29
|
-
precursor_trim: Value used to trim the precursor m/z for MS2 spectra.
|
|
30
|
-
max_peaks: Maximum number of peaks to retain in the spectrum.
|
|
31
|
-
centroid: Flag indicating whether the spectrum should be centroided.
|
|
32
|
-
deisotope: Flag indicating whether deisotoping should be performed.
|
|
33
|
-
dia_stats: Flag for processing DIA (data-independent acquisition) statistics.
|
|
34
|
-
feature: An optional identifier used when computing DIA statistics.
|
|
35
|
-
label: Optional label to assign to the spectrum.
|
|
36
|
-
centroid_algo: Algorithm to use for centroiding.
|
|
37
|
-
|
|
38
|
-
Available Methods:
|
|
39
|
-
- validate(param_name, value): Validate a single parameter value
|
|
40
|
-
- validate_all(): Validate all parameters at once
|
|
41
|
-
- to_dict(): Convert parameters to dictionary
|
|
42
|
-
- set_from_dict(param_dict, validate=True): Update multiple parameters from dict
|
|
43
|
-
- set(param_name, value, validate=True): Set parameter value with validation
|
|
44
|
-
- get(param_name): Get parameter value
|
|
45
|
-
- get_description(param_name): Get parameter description
|
|
46
|
-
- get_info(param_name): Get full parameter metadata
|
|
47
|
-
- list_parameters(): Get list of all parameter names
|
|
48
|
-
"""
|
|
49
|
-
|
|
50
|
-
scan: list[int] = field(default_factory=lambda: [0])
|
|
51
|
-
precursor_trim: int = -10
|
|
52
|
-
max_peaks: int | None = 50000
|
|
53
|
-
centroid: bool = True
|
|
54
|
-
deisotope: bool = True
|
|
55
|
-
dia_stats: bool | None = False
|
|
56
|
-
feature: int | None = None
|
|
57
|
-
label: str | None = None
|
|
58
|
-
centroid_algo: str | None = "lmp"
|
|
59
|
-
|
|
60
|
-
# Parameter metadata for validation and description
|
|
61
|
-
_param_metadata: dict[str, dict[str, Any]] = field(
|
|
62
|
-
default_factory=lambda: {
|
|
63
|
-
"scan": {
|
|
64
|
-
"dtype": list,
|
|
65
|
-
"description": "List of scan identifiers to retrieve",
|
|
66
|
-
},
|
|
67
|
-
"precursor_trim": {
|
|
68
|
-
"dtype": int,
|
|
69
|
-
"description": "Value used to trim the precursor m/z for MS2 spectra",
|
|
70
|
-
"min_value": -50,
|
|
71
|
-
"max_value": 50,
|
|
72
|
-
},
|
|
73
|
-
"max_peaks": {
|
|
74
|
-
"dtype": "Optional[int]",
|
|
75
|
-
"description": "Maximum number of peaks to retain in the spectrum",
|
|
76
|
-
"min_value": 1,
|
|
77
|
-
"max_value": 100000,
|
|
78
|
-
},
|
|
79
|
-
"centroid": {
|
|
80
|
-
"dtype": bool,
|
|
81
|
-
"description": "Flag indicating whether the spectrum should be centroided",
|
|
82
|
-
},
|
|
83
|
-
"deisotope": {
|
|
84
|
-
"dtype": bool,
|
|
85
|
-
"description": "Flag indicating whether deisotoping should be performed",
|
|
86
|
-
},
|
|
87
|
-
"dia_stats": {
|
|
88
|
-
"dtype": "Optional[bool]",
|
|
89
|
-
"description": "Flag for processing DIA (data-independent acquisition) statistics",
|
|
90
|
-
},
|
|
91
|
-
"feature": {
|
|
92
|
-
"dtype": "Optional[int]",
|
|
93
|
-
"description": "An optional identifier used when computing DIA statistics",
|
|
94
|
-
},
|
|
95
|
-
"label": {
|
|
96
|
-
"dtype": "Optional[str]",
|
|
97
|
-
"description": "Optional label to assign to the spectrum",
|
|
98
|
-
},
|
|
99
|
-
"centroid_algo": {
|
|
100
|
-
"dtype": "Optional[str]",
|
|
101
|
-
"description": "Algorithm to use for centroiding",
|
|
102
|
-
"allowed_values": ["lmp", "cwt", "gaussian"],
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
def get_info(self, param_name: str) -> dict[str, Any]:
|
|
108
|
-
"""
|
|
109
|
-
Get information about a specific parameter.
|
|
110
|
-
|
|
111
|
-
Args:
|
|
112
|
-
param_name: Name of the parameter
|
|
113
|
-
|
|
114
|
-
Returns:
|
|
115
|
-
Dictionary containing parameter metadata
|
|
116
|
-
|
|
117
|
-
Raises:
|
|
118
|
-
KeyError: If parameter name is not found
|
|
119
|
-
"""
|
|
120
|
-
if param_name not in self._param_metadata:
|
|
121
|
-
raise KeyError(f"Parameter '{param_name}' not found")
|
|
122
|
-
return self._param_metadata[param_name]
|
|
123
|
-
|
|
124
|
-
def get_description(self, param_name: str) -> str:
|
|
125
|
-
"""
|
|
126
|
-
Get description for a specific parameter.
|
|
127
|
-
|
|
128
|
-
Args:
|
|
129
|
-
param_name: Name of the parameter
|
|
130
|
-
|
|
131
|
-
Returns:
|
|
132
|
-
Parameter description string
|
|
133
|
-
"""
|
|
134
|
-
return str(self.get_info(param_name)["description"])
|
|
135
|
-
|
|
136
|
-
def validate(self, param_name: str, value: Any) -> bool:
|
|
137
|
-
"""
|
|
138
|
-
Validate a parameter value against its constraints.
|
|
139
|
-
|
|
140
|
-
Args:
|
|
141
|
-
param_name: Name of the parameter
|
|
142
|
-
value: Value to validate
|
|
143
|
-
|
|
144
|
-
Returns:
|
|
145
|
-
True if value is valid, False otherwise
|
|
146
|
-
"""
|
|
147
|
-
if param_name not in self._param_metadata:
|
|
148
|
-
return False
|
|
149
|
-
|
|
150
|
-
metadata = self._param_metadata[param_name]
|
|
151
|
-
expected_dtype = metadata["dtype"]
|
|
152
|
-
|
|
153
|
-
# Handle optional types
|
|
154
|
-
if isinstance(expected_dtype, str) and expected_dtype.startswith("Optional"):
|
|
155
|
-
if value is None:
|
|
156
|
-
return True
|
|
157
|
-
# Extract the inner type for validation
|
|
158
|
-
if "int" in expected_dtype:
|
|
159
|
-
expected_dtype = int
|
|
160
|
-
elif "str" in expected_dtype:
|
|
161
|
-
expected_dtype = str
|
|
162
|
-
elif "bool" in expected_dtype:
|
|
163
|
-
expected_dtype = bool
|
|
164
|
-
|
|
165
|
-
# Type checking
|
|
166
|
-
if expected_dtype is int:
|
|
167
|
-
if not isinstance(value, int):
|
|
168
|
-
try:
|
|
169
|
-
value = int(value)
|
|
170
|
-
except (ValueError, TypeError):
|
|
171
|
-
return False
|
|
172
|
-
elif expected_dtype is float:
|
|
173
|
-
if not isinstance(value, (int, float)):
|
|
174
|
-
try:
|
|
175
|
-
value = float(value)
|
|
176
|
-
except (ValueError, TypeError):
|
|
177
|
-
return False
|
|
178
|
-
elif expected_dtype is bool:
|
|
179
|
-
if not isinstance(value, bool):
|
|
180
|
-
return False
|
|
181
|
-
elif expected_dtype is str:
|
|
182
|
-
if not isinstance(value, str):
|
|
183
|
-
return False
|
|
184
|
-
elif expected_dtype is list:
|
|
185
|
-
if not isinstance(value, list):
|
|
186
|
-
return False
|
|
187
|
-
|
|
188
|
-
# Range validation for numeric types
|
|
189
|
-
if expected_dtype in (int, float) and isinstance(value, (int, float)):
|
|
190
|
-
if "min_value" in metadata and value < metadata["min_value"]:
|
|
191
|
-
return False
|
|
192
|
-
if "max_value" in metadata and value > metadata["max_value"]:
|
|
193
|
-
return False
|
|
194
|
-
|
|
195
|
-
# Allowed values validation for strings
|
|
196
|
-
if expected_dtype is str and "allowed_values" in metadata:
|
|
197
|
-
if value not in metadata["allowed_values"]:
|
|
198
|
-
return False
|
|
199
|
-
|
|
200
|
-
return True
|
|
201
|
-
|
|
202
|
-
def set(self, param_name: str, value: Any, validate: bool = True) -> bool:
|
|
203
|
-
"""
|
|
204
|
-
Set a parameter value with optional validation.
|
|
205
|
-
|
|
206
|
-
Args:
|
|
207
|
-
param_name: Name of the parameter
|
|
208
|
-
value: New value for the parameter
|
|
209
|
-
validate: Whether to validate the value before setting
|
|
210
|
-
|
|
211
|
-
Returns:
|
|
212
|
-
True if parameter was set successfully, False otherwise
|
|
213
|
-
"""
|
|
214
|
-
if not hasattr(self, param_name):
|
|
215
|
-
return False
|
|
216
|
-
|
|
217
|
-
if validate and not self.validate(param_name, value):
|
|
218
|
-
return False
|
|
219
|
-
|
|
220
|
-
# Convert to expected type if needed
|
|
221
|
-
if param_name in self._param_metadata:
|
|
222
|
-
expected_dtype = self._param_metadata[param_name]["dtype"]
|
|
223
|
-
|
|
224
|
-
# Handle optional types
|
|
225
|
-
if (
|
|
226
|
-
isinstance(
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
"""
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
"""
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
if not self.validate(param_name, current_value):
|
|
316
|
-
invalid_params.append(param_name)
|
|
317
|
-
|
|
318
|
-
return len(invalid_params) == 0, invalid_params
|
|
1
|
+
"""
|
|
2
|
+
Get Spectrum Parameters Module
|
|
3
|
+
|
|
4
|
+
This module defines parameters for spectrum retrieval and processing in mass spectrometry data.
|
|
5
|
+
It consolidates all parameters used in the get_spectrum() method with type checking,
|
|
6
|
+
validation, and comprehensive descriptions.
|
|
7
|
+
|
|
8
|
+
Classes:
|
|
9
|
+
get_spectrum_defaults: Configuration parameters for the get_spectrum() method.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class get_spectrum_defaults:
|
|
20
|
+
"""
|
|
21
|
+
Parameters for spectrum retrieval and processing.
|
|
22
|
+
|
|
23
|
+
This class consolidates all parameters used in the get_spectrum() method including
|
|
24
|
+
scan identification, spectrum processing options, and optional parameters.
|
|
25
|
+
It provides type checking, validation, and comprehensive parameter descriptions.
|
|
26
|
+
|
|
27
|
+
Spectrum Processing Parameters:
|
|
28
|
+
scan: Unique identifier of the scan to retrieve.
|
|
29
|
+
precursor_trim: Value used to trim the precursor m/z for MS2 spectra.
|
|
30
|
+
max_peaks: Maximum number of peaks to retain in the spectrum.
|
|
31
|
+
centroid: Flag indicating whether the spectrum should be centroided.
|
|
32
|
+
deisotope: Flag indicating whether deisotoping should be performed.
|
|
33
|
+
dia_stats: Flag for processing DIA (data-independent acquisition) statistics.
|
|
34
|
+
feature: An optional identifier used when computing DIA statistics.
|
|
35
|
+
label: Optional label to assign to the spectrum.
|
|
36
|
+
centroid_algo: Algorithm to use for centroiding.
|
|
37
|
+
|
|
38
|
+
Available Methods:
|
|
39
|
+
- validate(param_name, value): Validate a single parameter value
|
|
40
|
+
- validate_all(): Validate all parameters at once
|
|
41
|
+
- to_dict(): Convert parameters to dictionary
|
|
42
|
+
- set_from_dict(param_dict, validate=True): Update multiple parameters from dict
|
|
43
|
+
- set(param_name, value, validate=True): Set parameter value with validation
|
|
44
|
+
- get(param_name): Get parameter value
|
|
45
|
+
- get_description(param_name): Get parameter description
|
|
46
|
+
- get_info(param_name): Get full parameter metadata
|
|
47
|
+
- list_parameters(): Get list of all parameter names
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
scan: list[int] = field(default_factory=lambda: [0])
|
|
51
|
+
precursor_trim: int = -10
|
|
52
|
+
max_peaks: int | None = 50000
|
|
53
|
+
centroid: bool = True
|
|
54
|
+
deisotope: bool = True
|
|
55
|
+
dia_stats: bool | None = False
|
|
56
|
+
feature: int | None = None
|
|
57
|
+
label: str | None = None
|
|
58
|
+
centroid_algo: str | None = "lmp"
|
|
59
|
+
|
|
60
|
+
# Parameter metadata for validation and description
|
|
61
|
+
_param_metadata: dict[str, dict[str, Any]] = field(
|
|
62
|
+
default_factory=lambda: {
|
|
63
|
+
"scan": {
|
|
64
|
+
"dtype": list,
|
|
65
|
+
"description": "List of scan identifiers to retrieve",
|
|
66
|
+
},
|
|
67
|
+
"precursor_trim": {
|
|
68
|
+
"dtype": int,
|
|
69
|
+
"description": "Value used to trim the precursor m/z for MS2 spectra",
|
|
70
|
+
"min_value": -50,
|
|
71
|
+
"max_value": 50,
|
|
72
|
+
},
|
|
73
|
+
"max_peaks": {
|
|
74
|
+
"dtype": "Optional[int]",
|
|
75
|
+
"description": "Maximum number of peaks to retain in the spectrum",
|
|
76
|
+
"min_value": 1,
|
|
77
|
+
"max_value": 100000,
|
|
78
|
+
},
|
|
79
|
+
"centroid": {
|
|
80
|
+
"dtype": bool,
|
|
81
|
+
"description": "Flag indicating whether the spectrum should be centroided",
|
|
82
|
+
},
|
|
83
|
+
"deisotope": {
|
|
84
|
+
"dtype": bool,
|
|
85
|
+
"description": "Flag indicating whether deisotoping should be performed",
|
|
86
|
+
},
|
|
87
|
+
"dia_stats": {
|
|
88
|
+
"dtype": "Optional[bool]",
|
|
89
|
+
"description": "Flag for processing DIA (data-independent acquisition) statistics",
|
|
90
|
+
},
|
|
91
|
+
"feature": {
|
|
92
|
+
"dtype": "Optional[int]",
|
|
93
|
+
"description": "An optional identifier used when computing DIA statistics",
|
|
94
|
+
},
|
|
95
|
+
"label": {
|
|
96
|
+
"dtype": "Optional[str]",
|
|
97
|
+
"description": "Optional label to assign to the spectrum",
|
|
98
|
+
},
|
|
99
|
+
"centroid_algo": {
|
|
100
|
+
"dtype": "Optional[str]",
|
|
101
|
+
"description": "Algorithm to use for centroiding",
|
|
102
|
+
"allowed_values": ["lmp", "cwt", "gaussian"],
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
def get_info(self, param_name: str) -> dict[str, Any]:
|
|
108
|
+
"""
|
|
109
|
+
Get information about a specific parameter.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
param_name: Name of the parameter
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
Dictionary containing parameter metadata
|
|
116
|
+
|
|
117
|
+
Raises:
|
|
118
|
+
KeyError: If parameter name is not found
|
|
119
|
+
"""
|
|
120
|
+
if param_name not in self._param_metadata:
|
|
121
|
+
raise KeyError(f"Parameter '{param_name}' not found")
|
|
122
|
+
return self._param_metadata[param_name]
|
|
123
|
+
|
|
124
|
+
def get_description(self, param_name: str) -> str:
|
|
125
|
+
"""
|
|
126
|
+
Get description for a specific parameter.
|
|
127
|
+
|
|
128
|
+
Args:
|
|
129
|
+
param_name: Name of the parameter
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
Parameter description string
|
|
133
|
+
"""
|
|
134
|
+
return str(self.get_info(param_name)["description"])
|
|
135
|
+
|
|
136
|
+
def validate(self, param_name: str, value: Any) -> bool:
|
|
137
|
+
"""
|
|
138
|
+
Validate a parameter value against its constraints.
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
param_name: Name of the parameter
|
|
142
|
+
value: Value to validate
|
|
143
|
+
|
|
144
|
+
Returns:
|
|
145
|
+
True if value is valid, False otherwise
|
|
146
|
+
"""
|
|
147
|
+
if param_name not in self._param_metadata:
|
|
148
|
+
return False
|
|
149
|
+
|
|
150
|
+
metadata = self._param_metadata[param_name]
|
|
151
|
+
expected_dtype = metadata["dtype"]
|
|
152
|
+
|
|
153
|
+
# Handle optional types
|
|
154
|
+
if isinstance(expected_dtype, str) and expected_dtype.startswith("Optional"):
|
|
155
|
+
if value is None:
|
|
156
|
+
return True
|
|
157
|
+
# Extract the inner type for validation
|
|
158
|
+
if "int" in expected_dtype:
|
|
159
|
+
expected_dtype = int
|
|
160
|
+
elif "str" in expected_dtype:
|
|
161
|
+
expected_dtype = str
|
|
162
|
+
elif "bool" in expected_dtype:
|
|
163
|
+
expected_dtype = bool
|
|
164
|
+
|
|
165
|
+
# Type checking
|
|
166
|
+
if expected_dtype is int:
|
|
167
|
+
if not isinstance(value, int):
|
|
168
|
+
try:
|
|
169
|
+
value = int(value)
|
|
170
|
+
except (ValueError, TypeError):
|
|
171
|
+
return False
|
|
172
|
+
elif expected_dtype is float:
|
|
173
|
+
if not isinstance(value, (int, float)):
|
|
174
|
+
try:
|
|
175
|
+
value = float(value)
|
|
176
|
+
except (ValueError, TypeError):
|
|
177
|
+
return False
|
|
178
|
+
elif expected_dtype is bool:
|
|
179
|
+
if not isinstance(value, bool):
|
|
180
|
+
return False
|
|
181
|
+
elif expected_dtype is str:
|
|
182
|
+
if not isinstance(value, str):
|
|
183
|
+
return False
|
|
184
|
+
elif expected_dtype is list:
|
|
185
|
+
if not isinstance(value, list):
|
|
186
|
+
return False
|
|
187
|
+
|
|
188
|
+
# Range validation for numeric types
|
|
189
|
+
if expected_dtype in (int, float) and isinstance(value, (int, float)):
|
|
190
|
+
if "min_value" in metadata and value < metadata["min_value"]:
|
|
191
|
+
return False
|
|
192
|
+
if "max_value" in metadata and value > metadata["max_value"]:
|
|
193
|
+
return False
|
|
194
|
+
|
|
195
|
+
# Allowed values validation for strings
|
|
196
|
+
if expected_dtype is str and "allowed_values" in metadata:
|
|
197
|
+
if value not in metadata["allowed_values"]:
|
|
198
|
+
return False
|
|
199
|
+
|
|
200
|
+
return True
|
|
201
|
+
|
|
202
|
+
def set(self, param_name: str, value: Any, validate: bool = True) -> bool:
|
|
203
|
+
"""
|
|
204
|
+
Set a parameter value with optional validation.
|
|
205
|
+
|
|
206
|
+
Args:
|
|
207
|
+
param_name: Name of the parameter
|
|
208
|
+
value: New value for the parameter
|
|
209
|
+
validate: Whether to validate the value before setting
|
|
210
|
+
|
|
211
|
+
Returns:
|
|
212
|
+
True if parameter was set successfully, False otherwise
|
|
213
|
+
"""
|
|
214
|
+
if not hasattr(self, param_name):
|
|
215
|
+
return False
|
|
216
|
+
|
|
217
|
+
if validate and not self.validate(param_name, value):
|
|
218
|
+
return False
|
|
219
|
+
|
|
220
|
+
# Convert to expected type if needed
|
|
221
|
+
if param_name in self._param_metadata:
|
|
222
|
+
expected_dtype = self._param_metadata[param_name]["dtype"]
|
|
223
|
+
|
|
224
|
+
# Handle optional types
|
|
225
|
+
if isinstance(expected_dtype, str) and expected_dtype.startswith("Optional") and value is not None:
|
|
226
|
+
if "int" in expected_dtype and not isinstance(value, int):
|
|
227
|
+
try:
|
|
228
|
+
value = int(value)
|
|
229
|
+
except (ValueError, TypeError):
|
|
230
|
+
if validate:
|
|
231
|
+
return False
|
|
232
|
+
elif "float" in expected_dtype and not isinstance(value, float):
|
|
233
|
+
try:
|
|
234
|
+
value = float(value)
|
|
235
|
+
except (ValueError, TypeError):
|
|
236
|
+
if validate:
|
|
237
|
+
return False
|
|
238
|
+
|
|
239
|
+
setattr(self, param_name, value)
|
|
240
|
+
return True
|
|
241
|
+
|
|
242
|
+
def get(self, param_name: str) -> Any:
|
|
243
|
+
"""
|
|
244
|
+
Get the value of a parameter by name.
|
|
245
|
+
|
|
246
|
+
Args:
|
|
247
|
+
param_name: Name of the parameter
|
|
248
|
+
|
|
249
|
+
Returns:
|
|
250
|
+
Current value of the parameter
|
|
251
|
+
"""
|
|
252
|
+
if not hasattr(self, param_name):
|
|
253
|
+
raise KeyError(f"Parameter '{param_name}' not found")
|
|
254
|
+
return getattr(self, param_name)
|
|
255
|
+
|
|
256
|
+
def set_from_dict(
|
|
257
|
+
self,
|
|
258
|
+
param_dict: dict[str, Any],
|
|
259
|
+
validate: bool = True,
|
|
260
|
+
) -> list[str]:
|
|
261
|
+
"""
|
|
262
|
+
Update multiple parameters from a dictionary.
|
|
263
|
+
|
|
264
|
+
Args:
|
|
265
|
+
param_dict: Dictionary of parameter names and values
|
|
266
|
+
validate: Whether to validate values before setting
|
|
267
|
+
|
|
268
|
+
Returns:
|
|
269
|
+
List of parameter names that could not be set
|
|
270
|
+
"""
|
|
271
|
+
failed_params = []
|
|
272
|
+
|
|
273
|
+
for param_name, value in param_dict.items():
|
|
274
|
+
if not self.set(param_name, value, validate):
|
|
275
|
+
failed_params.append(param_name)
|
|
276
|
+
|
|
277
|
+
return failed_params
|
|
278
|
+
|
|
279
|
+
def to_dict(self) -> dict[str, Any]:
|
|
280
|
+
"""
|
|
281
|
+
Convert parameters to dictionary, excluding metadata.
|
|
282
|
+
|
|
283
|
+
Returns:
|
|
284
|
+
Dictionary of parameter names and values
|
|
285
|
+
"""
|
|
286
|
+
return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}
|
|
287
|
+
|
|
288
|
+
def list_parameters(self) -> list[str]:
|
|
289
|
+
"""
|
|
290
|
+
Get list of all parameter names.
|
|
291
|
+
|
|
292
|
+
Returns:
|
|
293
|
+
List of parameter names
|
|
294
|
+
"""
|
|
295
|
+
return [k for k in self.__dict__.keys() if not k.startswith("_")]
|
|
296
|
+
|
|
297
|
+
def validate_all(self) -> tuple[bool, list[str]]:
|
|
298
|
+
"""
|
|
299
|
+
Validate all parameters in the instance.
|
|
300
|
+
|
|
301
|
+
Returns:
|
|
302
|
+
Tuple of (all_valid, list_of_invalid_params)
|
|
303
|
+
- all_valid: True if all parameters are valid, False otherwise
|
|
304
|
+
- list_of_invalid_params: List of parameter names that failed validation
|
|
305
|
+
"""
|
|
306
|
+
invalid_params = []
|
|
307
|
+
|
|
308
|
+
for param_name in self.list_parameters():
|
|
309
|
+
if param_name in self._param_metadata:
|
|
310
|
+
current_value = getattr(self, param_name)
|
|
311
|
+
if not self.validate(param_name, current_value):
|
|
312
|
+
invalid_params.append(param_name)
|
|
313
|
+
|
|
314
|
+
return len(invalid_params) == 0, invalid_params
|