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
masster/study/__init__.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Study module for masster.
|
|
3
|
-
|
|
4
|
-
This module provides the Sample class for handling mass spectrometry data.
|
|
5
|
-
"""
|
|
6
|
-
|
|
7
|
-
from .study import Study
|
|
8
|
-
|
|
9
|
-
__all__ = ["Study"]
|
|
1
|
+
"""
|
|
2
|
+
Study module for masster.
|
|
3
|
+
|
|
4
|
+
This module provides the Sample class for handling mass spectrometry data.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .study import Study
|
|
8
|
+
|
|
9
|
+
__all__ = ["Study"]
|
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
"""Study defaults initialization."""
|
|
2
|
-
|
|
3
|
-
from .align_def import align_defaults
|
|
4
|
-
from .export_def import export_mgf_defaults
|
|
5
|
-
from .
|
|
6
|
-
from .find_consensus_def import find_consensus_defaults
|
|
7
|
-
from .find_ms2_def import find_ms2_defaults
|
|
8
|
-
from .
|
|
9
|
-
from .
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
|
|
1
|
+
"""Study defaults initialization."""
|
|
2
|
+
|
|
3
|
+
from .align_def import align_defaults
|
|
4
|
+
from .export_def import export_mgf_defaults
|
|
5
|
+
from .fill_def import fill_defaults
|
|
6
|
+
from .find_consensus_def import find_consensus_defaults
|
|
7
|
+
from .find_ms2_def import find_ms2_defaults
|
|
8
|
+
from .integrate_def import integrate_defaults
|
|
9
|
+
from .merge_def import merge_defaults
|
|
10
|
+
from .study_def import study_defaults
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"align_defaults",
|
|
14
|
+
"export_mgf_defaults",
|
|
15
|
+
"fill_defaults",
|
|
16
|
+
"find_consensus_defaults",
|
|
17
|
+
"find_ms2_defaults",
|
|
18
|
+
"integrate_defaults",
|
|
19
|
+
"merge_defaults",
|
|
20
|
+
"study_defaults",
|
|
21
|
+
]
|
|
@@ -1,267 +1,267 @@
|
|
|
1
|
-
"""Parameter class for Study align method."""
|
|
2
|
-
|
|
3
|
-
from dataclasses import dataclass, field
|
|
4
|
-
from typing import Any
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
@dataclass
|
|
8
|
-
class align_defaults:
|
|
9
|
-
"""
|
|
10
|
-
Parameter class for Study align method.
|
|
11
|
-
|
|
12
|
-
This class encapsulates parameters for feature alignment across samples,
|
|
13
|
-
including retention time and m/z tolerances, warping parameters, and
|
|
14
|
-
alignment algorithm settings.
|
|
15
|
-
|
|
16
|
-
Attributes:
|
|
17
|
-
rt_max_diff (float): Maximum retention time difference for alignment. Default is 60.0.
|
|
18
|
-
mz_max_diff (float): Maximum m/z difference for alignment. Default is 0.01.
|
|
19
|
-
rt_pair_distance_frac (float): Fraction of RT difference for pair distance. Default is 0.2.
|
|
20
|
-
mz_pair_max_distance (float): Maximum m/z pair distance. Default is 0.01.
|
|
21
|
-
num_used_points (int): Number of points used for alignment. Default is 1000.
|
|
22
|
-
save_features (bool): Whether to save features after alignment. Default is True.
|
|
23
|
-
skip_blanks (bool): Whether to skip blank samples. Default is True.
|
|
24
|
-
"""
|
|
25
|
-
|
|
26
|
-
rt_max_diff: float = 60.0
|
|
27
|
-
mz_max_diff: float = 0.01
|
|
28
|
-
rt_pair_distance_frac: float = 0.2
|
|
29
|
-
mz_pair_max_distance: float = 0.01
|
|
30
|
-
num_used_points: int = 1000
|
|
31
|
-
save_features: bool = True
|
|
32
|
-
skip_blanks: bool = True
|
|
33
|
-
|
|
34
|
-
_param_metadata: dict[str, dict[str, Any]] = field(
|
|
35
|
-
default_factory=lambda: {
|
|
36
|
-
"rt_max_diff": {
|
|
37
|
-
"dtype": float,
|
|
38
|
-
"description": "Maximum retention time difference for alignment (seconds)",
|
|
39
|
-
"default": 60.0,
|
|
40
|
-
"min_value": 1.0,
|
|
41
|
-
"max_value": 300.0,
|
|
42
|
-
},
|
|
43
|
-
"mz_max_diff": {
|
|
44
|
-
"dtype": float,
|
|
45
|
-
"description": "Maximum m/z difference for alignment (Da)",
|
|
46
|
-
"default": 0.01,
|
|
47
|
-
"min_value": 0.001,
|
|
48
|
-
"max_value": 1.0,
|
|
49
|
-
},
|
|
50
|
-
"rt_pair_distance_frac": {
|
|
51
|
-
"dtype": float,
|
|
52
|
-
"description": "Fraction of RT difference for pair distance calculation",
|
|
53
|
-
"default": 0.2,
|
|
54
|
-
"min_value": 0.1,
|
|
55
|
-
"max_value": 1.0,
|
|
56
|
-
},
|
|
57
|
-
"mz_pair_max_distance": {
|
|
58
|
-
"dtype": float,
|
|
59
|
-
"description": "Maximum m/z pair distance (Da)",
|
|
60
|
-
"default": 0.01,
|
|
61
|
-
"min_value": 0.001,
|
|
62
|
-
"max_value": 0.1,
|
|
63
|
-
},
|
|
64
|
-
"num_used_points": {
|
|
65
|
-
"dtype": int,
|
|
66
|
-
"description": "Number of points used for alignment",
|
|
67
|
-
"default": 1000,
|
|
68
|
-
"min_value": 10,
|
|
69
|
-
"max_value": 100000,
|
|
70
|
-
},
|
|
71
|
-
"save_features": {
|
|
72
|
-
"dtype": bool,
|
|
73
|
-
"description": "Whether to save features after alignment",
|
|
74
|
-
"default": True,
|
|
75
|
-
},
|
|
76
|
-
"skip_blanks": {
|
|
77
|
-
"dtype": bool,
|
|
78
|
-
"description": "Whether to skip blank samples during alignment",
|
|
79
|
-
"default": True,
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
repr=False,
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
def get_info(self, param_name: str) -> dict[str, Any]:
|
|
86
|
-
"""
|
|
87
|
-
Get information about a specific parameter.
|
|
88
|
-
|
|
89
|
-
Args:
|
|
90
|
-
param_name: Name of the parameter
|
|
91
|
-
|
|
92
|
-
Returns:
|
|
93
|
-
Dictionary containing parameter metadata
|
|
94
|
-
|
|
95
|
-
Raises:
|
|
96
|
-
KeyError: If parameter name is not found
|
|
97
|
-
"""
|
|
98
|
-
if param_name not in self._param_metadata:
|
|
99
|
-
raise KeyError(f"Parameter '{param_name}' not found")
|
|
100
|
-
return self._param_metadata[param_name]
|
|
101
|
-
|
|
102
|
-
def get_description(self, param_name: str) -> str:
|
|
103
|
-
"""
|
|
104
|
-
Get description for a specific parameter.
|
|
105
|
-
|
|
106
|
-
Args:
|
|
107
|
-
param_name: Name of the parameter
|
|
108
|
-
|
|
109
|
-
Returns:
|
|
110
|
-
Parameter description string
|
|
111
|
-
"""
|
|
112
|
-
return str(self.get_info(param_name)["description"])
|
|
113
|
-
|
|
114
|
-
def validate(self, param_name: str, value: Any) -> bool:
|
|
115
|
-
"""
|
|
116
|
-
Validate a parameter value against its constraints.
|
|
117
|
-
|
|
118
|
-
Args:
|
|
119
|
-
param_name: Name of the parameter
|
|
120
|
-
value: Value to validate
|
|
121
|
-
|
|
122
|
-
Returns:
|
|
123
|
-
True if value is valid, False otherwise
|
|
124
|
-
"""
|
|
125
|
-
if param_name not in self._param_metadata:
|
|
126
|
-
return False
|
|
127
|
-
|
|
128
|
-
metadata = self._param_metadata[param_name]
|
|
129
|
-
expected_dtype = metadata["dtype"]
|
|
130
|
-
|
|
131
|
-
# Type checking
|
|
132
|
-
if expected_dtype is int:
|
|
133
|
-
if not isinstance(value, int):
|
|
134
|
-
try:
|
|
135
|
-
value = int(value)
|
|
136
|
-
except (ValueError, TypeError):
|
|
137
|
-
return False
|
|
138
|
-
elif expected_dtype is float:
|
|
139
|
-
if not isinstance(value, (int, float)):
|
|
140
|
-
try:
|
|
141
|
-
value = float(value)
|
|
142
|
-
except (ValueError, TypeError):
|
|
143
|
-
return False
|
|
144
|
-
elif expected_dtype is bool:
|
|
145
|
-
if not isinstance(value, bool):
|
|
146
|
-
return False
|
|
147
|
-
|
|
148
|
-
# Range validation for numeric types
|
|
149
|
-
if expected_dtype in (int, float) and isinstance(value, (int, float)):
|
|
150
|
-
if "min_value" in metadata and value < metadata["min_value"]:
|
|
151
|
-
return False
|
|
152
|
-
if "max_value" in metadata and value > metadata["max_value"]:
|
|
153
|
-
return False
|
|
154
|
-
|
|
155
|
-
return True
|
|
156
|
-
|
|
157
|
-
def set(self, param_name: str, value: Any, validate: bool = True) -> bool:
|
|
158
|
-
"""
|
|
159
|
-
Set a parameter value with optional validation.
|
|
160
|
-
|
|
161
|
-
Args:
|
|
162
|
-
param_name: Name of the parameter
|
|
163
|
-
value: New value for the parameter
|
|
164
|
-
validate: Whether to validate the value before setting
|
|
165
|
-
|
|
166
|
-
Returns:
|
|
167
|
-
True if parameter was set successfully, False otherwise
|
|
168
|
-
"""
|
|
169
|
-
if not hasattr(self, param_name):
|
|
170
|
-
return False
|
|
171
|
-
|
|
172
|
-
if validate and not self.validate(param_name, value):
|
|
173
|
-
return False
|
|
174
|
-
|
|
175
|
-
# Convert to expected type if needed
|
|
176
|
-
if param_name in self._param_metadata:
|
|
177
|
-
expected_dtype = self._param_metadata[param_name]["dtype"]
|
|
178
|
-
|
|
179
|
-
if expected_dtype is int and not isinstance(value, int):
|
|
180
|
-
try:
|
|
181
|
-
value = int(value)
|
|
182
|
-
except (ValueError, TypeError):
|
|
183
|
-
if validate:
|
|
184
|
-
return False
|
|
185
|
-
elif expected_dtype is float and not isinstance(value, float):
|
|
186
|
-
try:
|
|
187
|
-
value = float(value)
|
|
188
|
-
except (ValueError, TypeError):
|
|
189
|
-
if validate:
|
|
190
|
-
return False
|
|
191
|
-
|
|
192
|
-
setattr(self, param_name, value)
|
|
193
|
-
return True
|
|
194
|
-
|
|
195
|
-
def get(self, param_name: str) -> Any:
|
|
196
|
-
"""
|
|
197
|
-
Get the value of a parameter by name.
|
|
198
|
-
|
|
199
|
-
Args:
|
|
200
|
-
param_name: Name of the parameter
|
|
201
|
-
|
|
202
|
-
Returns:
|
|
203
|
-
Current value of the parameter
|
|
204
|
-
"""
|
|
205
|
-
if not hasattr(self, param_name):
|
|
206
|
-
raise KeyError(f"Parameter '{param_name}' not found")
|
|
207
|
-
return getattr(self, param_name)
|
|
208
|
-
|
|
209
|
-
def set_from_dict(
|
|
210
|
-
self,
|
|
211
|
-
param_dict: dict[str, Any],
|
|
212
|
-
validate: bool = True,
|
|
213
|
-
) -> list[str]:
|
|
214
|
-
"""
|
|
215
|
-
Update multiple parameters from a dictionary.
|
|
216
|
-
|
|
217
|
-
Args:
|
|
218
|
-
param_dict: Dictionary of parameter names and values
|
|
219
|
-
validate: Whether to validate values before setting
|
|
220
|
-
|
|
221
|
-
Returns:
|
|
222
|
-
List of parameter names that could not be set
|
|
223
|
-
"""
|
|
224
|
-
failed_params = []
|
|
225
|
-
|
|
226
|
-
for param_name, value in param_dict.items():
|
|
227
|
-
if not self.set(param_name, value, validate):
|
|
228
|
-
failed_params.append(param_name)
|
|
229
|
-
|
|
230
|
-
return failed_params
|
|
231
|
-
|
|
232
|
-
def to_dict(self) -> dict[str, Any]:
|
|
233
|
-
"""
|
|
234
|
-
Convert parameters to dictionary, excluding metadata.
|
|
235
|
-
|
|
236
|
-
Returns:
|
|
237
|
-
Dictionary of parameter names and values
|
|
238
|
-
"""
|
|
239
|
-
return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}
|
|
240
|
-
|
|
241
|
-
def list_parameters(self) -> list[str]:
|
|
242
|
-
"""
|
|
243
|
-
Get list of all parameter names.
|
|
244
|
-
|
|
245
|
-
Returns:
|
|
246
|
-
List of parameter names
|
|
247
|
-
"""
|
|
248
|
-
return [k for k in self.__dict__.keys() if not k.startswith("_")]
|
|
249
|
-
|
|
250
|
-
def validate_all(self) -> tuple[bool, list[str]]:
|
|
251
|
-
"""
|
|
252
|
-
Validate all parameters in the instance.
|
|
253
|
-
|
|
254
|
-
Returns:
|
|
255
|
-
Tuple of (all_valid, list_of_invalid_params)
|
|
256
|
-
- all_valid: True if all parameters are valid, False otherwise
|
|
257
|
-
- list_of_invalid_params: List of parameter names that failed validation
|
|
258
|
-
"""
|
|
259
|
-
invalid_params = []
|
|
260
|
-
|
|
261
|
-
for param_name in self.list_parameters():
|
|
262
|
-
if param_name in self._param_metadata:
|
|
263
|
-
current_value = getattr(self, param_name)
|
|
264
|
-
if not self.validate(param_name, current_value):
|
|
265
|
-
invalid_params.append(param_name)
|
|
266
|
-
|
|
267
|
-
return len(invalid_params) == 0, invalid_params
|
|
1
|
+
"""Parameter class for Study align method."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass
|
|
8
|
+
class align_defaults:
|
|
9
|
+
"""
|
|
10
|
+
Parameter class for Study align method.
|
|
11
|
+
|
|
12
|
+
This class encapsulates parameters for feature alignment across samples,
|
|
13
|
+
including retention time and m/z tolerances, warping parameters, and
|
|
14
|
+
alignment algorithm settings.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
rt_max_diff (float): Maximum retention time difference for alignment. Default is 60.0.
|
|
18
|
+
mz_max_diff (float): Maximum m/z difference for alignment. Default is 0.01.
|
|
19
|
+
rt_pair_distance_frac (float): Fraction of RT difference for pair distance. Default is 0.2.
|
|
20
|
+
mz_pair_max_distance (float): Maximum m/z pair distance. Default is 0.01.
|
|
21
|
+
num_used_points (int): Number of points used for alignment. Default is 1000.
|
|
22
|
+
save_features (bool): Whether to save features after alignment. Default is True.
|
|
23
|
+
skip_blanks (bool): Whether to skip blank samples. Default is True.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
rt_max_diff: float = 60.0
|
|
27
|
+
mz_max_diff: float = 0.01
|
|
28
|
+
rt_pair_distance_frac: float = 0.2
|
|
29
|
+
mz_pair_max_distance: float = 0.01
|
|
30
|
+
num_used_points: int = 1000
|
|
31
|
+
save_features: bool = True
|
|
32
|
+
skip_blanks: bool = True
|
|
33
|
+
|
|
34
|
+
_param_metadata: dict[str, dict[str, Any]] = field(
|
|
35
|
+
default_factory=lambda: {
|
|
36
|
+
"rt_max_diff": {
|
|
37
|
+
"dtype": float,
|
|
38
|
+
"description": "Maximum retention time difference for alignment (seconds)",
|
|
39
|
+
"default": 60.0,
|
|
40
|
+
"min_value": 1.0,
|
|
41
|
+
"max_value": 300.0,
|
|
42
|
+
},
|
|
43
|
+
"mz_max_diff": {
|
|
44
|
+
"dtype": float,
|
|
45
|
+
"description": "Maximum m/z difference for alignment (Da)",
|
|
46
|
+
"default": 0.01,
|
|
47
|
+
"min_value": 0.001,
|
|
48
|
+
"max_value": 1.0,
|
|
49
|
+
},
|
|
50
|
+
"rt_pair_distance_frac": {
|
|
51
|
+
"dtype": float,
|
|
52
|
+
"description": "Fraction of RT difference for pair distance calculation",
|
|
53
|
+
"default": 0.2,
|
|
54
|
+
"min_value": 0.1,
|
|
55
|
+
"max_value": 1.0,
|
|
56
|
+
},
|
|
57
|
+
"mz_pair_max_distance": {
|
|
58
|
+
"dtype": float,
|
|
59
|
+
"description": "Maximum m/z pair distance (Da)",
|
|
60
|
+
"default": 0.01,
|
|
61
|
+
"min_value": 0.001,
|
|
62
|
+
"max_value": 0.1,
|
|
63
|
+
},
|
|
64
|
+
"num_used_points": {
|
|
65
|
+
"dtype": int,
|
|
66
|
+
"description": "Number of points used for alignment",
|
|
67
|
+
"default": 1000,
|
|
68
|
+
"min_value": 10,
|
|
69
|
+
"max_value": 100000,
|
|
70
|
+
},
|
|
71
|
+
"save_features": {
|
|
72
|
+
"dtype": bool,
|
|
73
|
+
"description": "Whether to save features after alignment",
|
|
74
|
+
"default": True,
|
|
75
|
+
},
|
|
76
|
+
"skip_blanks": {
|
|
77
|
+
"dtype": bool,
|
|
78
|
+
"description": "Whether to skip blank samples during alignment",
|
|
79
|
+
"default": True,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
repr=False,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
def get_info(self, param_name: str) -> dict[str, Any]:
|
|
86
|
+
"""
|
|
87
|
+
Get information about a specific parameter.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
param_name: Name of the parameter
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
Dictionary containing parameter metadata
|
|
94
|
+
|
|
95
|
+
Raises:
|
|
96
|
+
KeyError: If parameter name is not found
|
|
97
|
+
"""
|
|
98
|
+
if param_name not in self._param_metadata:
|
|
99
|
+
raise KeyError(f"Parameter '{param_name}' not found")
|
|
100
|
+
return self._param_metadata[param_name]
|
|
101
|
+
|
|
102
|
+
def get_description(self, param_name: str) -> str:
|
|
103
|
+
"""
|
|
104
|
+
Get description for a specific parameter.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
param_name: Name of the parameter
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
Parameter description string
|
|
111
|
+
"""
|
|
112
|
+
return str(self.get_info(param_name)["description"])
|
|
113
|
+
|
|
114
|
+
def validate(self, param_name: str, value: Any) -> bool:
|
|
115
|
+
"""
|
|
116
|
+
Validate a parameter value against its constraints.
|
|
117
|
+
|
|
118
|
+
Args:
|
|
119
|
+
param_name: Name of the parameter
|
|
120
|
+
value: Value to validate
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
True if value is valid, False otherwise
|
|
124
|
+
"""
|
|
125
|
+
if param_name not in self._param_metadata:
|
|
126
|
+
return False
|
|
127
|
+
|
|
128
|
+
metadata = self._param_metadata[param_name]
|
|
129
|
+
expected_dtype = metadata["dtype"]
|
|
130
|
+
|
|
131
|
+
# Type checking
|
|
132
|
+
if expected_dtype is int:
|
|
133
|
+
if not isinstance(value, int):
|
|
134
|
+
try:
|
|
135
|
+
value = int(value)
|
|
136
|
+
except (ValueError, TypeError):
|
|
137
|
+
return False
|
|
138
|
+
elif expected_dtype is float:
|
|
139
|
+
if not isinstance(value, (int, float)):
|
|
140
|
+
try:
|
|
141
|
+
value = float(value)
|
|
142
|
+
except (ValueError, TypeError):
|
|
143
|
+
return False
|
|
144
|
+
elif expected_dtype is bool:
|
|
145
|
+
if not isinstance(value, bool):
|
|
146
|
+
return False
|
|
147
|
+
|
|
148
|
+
# Range validation for numeric types
|
|
149
|
+
if expected_dtype in (int, float) and isinstance(value, (int, float)):
|
|
150
|
+
if "min_value" in metadata and value < metadata["min_value"]:
|
|
151
|
+
return False
|
|
152
|
+
if "max_value" in metadata and value > metadata["max_value"]:
|
|
153
|
+
return False
|
|
154
|
+
|
|
155
|
+
return True
|
|
156
|
+
|
|
157
|
+
def set(self, param_name: str, value: Any, validate: bool = True) -> bool:
|
|
158
|
+
"""
|
|
159
|
+
Set a parameter value with optional validation.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
param_name: Name of the parameter
|
|
163
|
+
value: New value for the parameter
|
|
164
|
+
validate: Whether to validate the value before setting
|
|
165
|
+
|
|
166
|
+
Returns:
|
|
167
|
+
True if parameter was set successfully, False otherwise
|
|
168
|
+
"""
|
|
169
|
+
if not hasattr(self, param_name):
|
|
170
|
+
return False
|
|
171
|
+
|
|
172
|
+
if validate and not self.validate(param_name, value):
|
|
173
|
+
return False
|
|
174
|
+
|
|
175
|
+
# Convert to expected type if needed
|
|
176
|
+
if param_name in self._param_metadata:
|
|
177
|
+
expected_dtype = self._param_metadata[param_name]["dtype"]
|
|
178
|
+
|
|
179
|
+
if expected_dtype is int and not isinstance(value, int):
|
|
180
|
+
try:
|
|
181
|
+
value = int(value)
|
|
182
|
+
except (ValueError, TypeError):
|
|
183
|
+
if validate:
|
|
184
|
+
return False
|
|
185
|
+
elif expected_dtype is float and not isinstance(value, float):
|
|
186
|
+
try:
|
|
187
|
+
value = float(value)
|
|
188
|
+
except (ValueError, TypeError):
|
|
189
|
+
if validate:
|
|
190
|
+
return False
|
|
191
|
+
|
|
192
|
+
setattr(self, param_name, value)
|
|
193
|
+
return True
|
|
194
|
+
|
|
195
|
+
def get(self, param_name: str) -> Any:
|
|
196
|
+
"""
|
|
197
|
+
Get the value of a parameter by name.
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
param_name: Name of the parameter
|
|
201
|
+
|
|
202
|
+
Returns:
|
|
203
|
+
Current value of the parameter
|
|
204
|
+
"""
|
|
205
|
+
if not hasattr(self, param_name):
|
|
206
|
+
raise KeyError(f"Parameter '{param_name}' not found")
|
|
207
|
+
return getattr(self, param_name)
|
|
208
|
+
|
|
209
|
+
def set_from_dict(
|
|
210
|
+
self,
|
|
211
|
+
param_dict: dict[str, Any],
|
|
212
|
+
validate: bool = True,
|
|
213
|
+
) -> list[str]:
|
|
214
|
+
"""
|
|
215
|
+
Update multiple parameters from a dictionary.
|
|
216
|
+
|
|
217
|
+
Args:
|
|
218
|
+
param_dict: Dictionary of parameter names and values
|
|
219
|
+
validate: Whether to validate values before setting
|
|
220
|
+
|
|
221
|
+
Returns:
|
|
222
|
+
List of parameter names that could not be set
|
|
223
|
+
"""
|
|
224
|
+
failed_params = []
|
|
225
|
+
|
|
226
|
+
for param_name, value in param_dict.items():
|
|
227
|
+
if not self.set(param_name, value, validate):
|
|
228
|
+
failed_params.append(param_name)
|
|
229
|
+
|
|
230
|
+
return failed_params
|
|
231
|
+
|
|
232
|
+
def to_dict(self) -> dict[str, Any]:
|
|
233
|
+
"""
|
|
234
|
+
Convert parameters to dictionary, excluding metadata.
|
|
235
|
+
|
|
236
|
+
Returns:
|
|
237
|
+
Dictionary of parameter names and values
|
|
238
|
+
"""
|
|
239
|
+
return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}
|
|
240
|
+
|
|
241
|
+
def list_parameters(self) -> list[str]:
|
|
242
|
+
"""
|
|
243
|
+
Get list of all parameter names.
|
|
244
|
+
|
|
245
|
+
Returns:
|
|
246
|
+
List of parameter names
|
|
247
|
+
"""
|
|
248
|
+
return [k for k in self.__dict__.keys() if not k.startswith("_")]
|
|
249
|
+
|
|
250
|
+
def validate_all(self) -> tuple[bool, list[str]]:
|
|
251
|
+
"""
|
|
252
|
+
Validate all parameters in the instance.
|
|
253
|
+
|
|
254
|
+
Returns:
|
|
255
|
+
Tuple of (all_valid, list_of_invalid_params)
|
|
256
|
+
- all_valid: True if all parameters are valid, False otherwise
|
|
257
|
+
- list_of_invalid_params: List of parameter names that failed validation
|
|
258
|
+
"""
|
|
259
|
+
invalid_params = []
|
|
260
|
+
|
|
261
|
+
for param_name in self.list_parameters():
|
|
262
|
+
if param_name in self._param_metadata:
|
|
263
|
+
current_value = getattr(self, param_name)
|
|
264
|
+
if not self.validate(param_name, current_value):
|
|
265
|
+
invalid_params.append(param_name)
|
|
266
|
+
|
|
267
|
+
return len(invalid_params) == 0, invalid_params
|