masster 0.2.4__py3-none-any.whl → 0.3.0__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.

Files changed (55) hide show
  1. masster/__init__.py +27 -27
  2. masster/_version.py +17 -17
  3. masster/chromatogram.py +497 -503
  4. masster/data/examples/2025_01_14_VW_7600_LpMx_DBS_CID_2min_TOP15_030msecMS1_005msecReac_CE35_DBS-ON_3.featureXML +199787 -0
  5. masster/data/examples/2025_01_14_VW_7600_LpMx_DBS_CID_2min_TOP15_030msecMS1_005msecReac_CE35_DBS-ON_3.sample5 +0 -0
  6. masster/logger.py +318 -244
  7. masster/sample/__init__.py +9 -9
  8. masster/sample/defaults/__init__.py +15 -15
  9. masster/sample/defaults/find_adducts_def.py +325 -325
  10. masster/sample/defaults/find_features_def.py +366 -366
  11. masster/sample/defaults/find_ms2_def.py +285 -285
  12. masster/sample/defaults/get_spectrum_def.py +314 -318
  13. masster/sample/defaults/sample_def.py +374 -378
  14. masster/sample/h5.py +1321 -1297
  15. masster/sample/helpers.py +833 -364
  16. masster/sample/lib.py +762 -0
  17. masster/sample/load.py +1220 -1187
  18. masster/sample/parameters.py +131 -131
  19. masster/sample/plot.py +1610 -1622
  20. masster/sample/processing.py +1402 -1416
  21. masster/sample/quant.py +209 -0
  22. masster/sample/sample.py +391 -387
  23. masster/sample/sample5_schema.json +181 -181
  24. masster/sample/save.py +737 -719
  25. masster/sample/sciex.py +1213 -0
  26. masster/spectrum.py +1287 -1319
  27. masster/study/__init__.py +9 -9
  28. masster/study/defaults/__init__.py +21 -19
  29. masster/study/defaults/align_def.py +267 -267
  30. masster/study/defaults/export_def.py +41 -40
  31. masster/study/defaults/fill_chrom_def.py +264 -264
  32. masster/study/defaults/fill_def.py +260 -0
  33. masster/study/defaults/find_consensus_def.py +256 -256
  34. masster/study/defaults/find_ms2_def.py +163 -163
  35. masster/study/defaults/integrate_chrom_def.py +225 -225
  36. masster/study/defaults/integrate_def.py +221 -0
  37. masster/study/defaults/merge_def.py +256 -0
  38. masster/study/defaults/study_def.py +272 -269
  39. masster/study/export.py +674 -287
  40. masster/study/h5.py +1398 -886
  41. masster/study/helpers.py +1650 -433
  42. masster/study/helpers_optimized.py +317 -0
  43. masster/study/load.py +1201 -1078
  44. masster/study/parameters.py +99 -99
  45. masster/study/plot.py +632 -645
  46. masster/study/processing.py +1057 -1046
  47. masster/study/save.py +149 -134
  48. masster/study/study.py +606 -522
  49. masster/study/study5_schema.json +247 -241
  50. {masster-0.2.4.dist-info → masster-0.3.0.dist-info}/METADATA +15 -10
  51. masster-0.3.0.dist-info/RECORD +59 -0
  52. {masster-0.2.4.dist-info → masster-0.3.0.dist-info}/licenses/LICENSE +661 -661
  53. masster-0.2.4.dist-info/RECORD +0 -50
  54. {masster-0.2.4.dist-info → masster-0.3.0.dist-info}/WHEEL +0 -0
  55. {masster-0.2.4.dist-info → masster-0.3.0.dist-info}/entry_points.txt +0 -0
@@ -1,256 +1,256 @@
1
- """Parameter class for Study find_consensus method."""
2
-
3
- from dataclasses import dataclass, field
4
- from typing import Any
5
-
6
-
7
- @dataclass
8
- class find_consensus_defaults:
9
- """
10
- Parameter class for Study find_consensus method.
11
-
12
- This class encapsulates parameters for consensus feature detection across samples,
13
- including algorithm selection, grouping tolerances, and minimum sample requirements.
14
-
15
- Attributes:
16
- algorithm (str): Feature grouping algorithm. Default is "qt".
17
- min_samples (int): Minimum number of samples for a consensus feature. Default is 1.
18
- link_ms2 (bool): Whether to link MS2 spectra to consensus features. Default is True.
19
- mz_tol (float): m/z tolerance for grouping (Da). Default is 0.01.
20
- rt_tol (float): RT tolerance for grouping (seconds). Default is 1.0.
21
- """
22
-
23
- algorithm: str = "qt"
24
- min_samples: int = 1
25
- link_ms2: bool = True
26
- mz_tol: float = 0.01
27
- rt_tol: float = 1.0
28
-
29
- _param_metadata: dict[str, dict[str, Any]] = field(
30
- default_factory=lambda: {
31
- "algorithm": {
32
- "dtype": str,
33
- "description": "Feature grouping algorithm",
34
- "default": "qt",
35
- "allowed_values": ["qt", "kd", "unlabeled", "sequential"],
36
- },
37
- "min_samples": {
38
- "dtype": int,
39
- "description": "Minimum number of samples for a consensus feature",
40
- "default": 1,
41
- "min_value": 1,
42
- },
43
- "link_ms2": {
44
- "dtype": bool,
45
- "description": "Whether to link MS2 spectra to consensus features",
46
- "default": True,
47
- },
48
- "mz_tol": {
49
- "dtype": float,
50
- "description": "m/z tolerance for grouping (Da)",
51
- "default": 0.01,
52
- "min_value": 0.001,
53
- "max_value": 1.0,
54
- },
55
- "rt_tol": {
56
- "dtype": float,
57
- "description": "RT tolerance for grouping (seconds)",
58
- "default": 1.0,
59
- "min_value": 0.1,
60
- "max_value": 60.0,
61
- },
62
- },
63
- repr=False,
64
- )
65
-
66
- def get_info(self, param_name: str) -> dict[str, Any]:
67
- """
68
- Get information about a specific parameter.
69
-
70
- Args:
71
- param_name: Name of the parameter
72
-
73
- Returns:
74
- Dictionary containing parameter metadata
75
-
76
- Raises:
77
- KeyError: If parameter name is not found
78
- """
79
- if param_name not in self._param_metadata:
80
- raise KeyError(f"Parameter '{param_name}' not found")
81
- return self._param_metadata[param_name]
82
-
83
- def get_description(self, param_name: str) -> str:
84
- """
85
- Get description for a specific parameter.
86
-
87
- Args:
88
- param_name: Name of the parameter
89
-
90
- Returns:
91
- Parameter description string
92
- """
93
- return str(self.get_info(param_name)["description"])
94
-
95
- def validate(self, param_name: str, value: Any) -> bool:
96
- """
97
- Validate a parameter value against its constraints.
98
-
99
- Args:
100
- param_name: Name of the parameter
101
- value: Value to validate
102
-
103
- Returns:
104
- True if value is valid, False otherwise
105
- """
106
- if param_name not in self._param_metadata:
107
- return False
108
-
109
- metadata = self._param_metadata[param_name]
110
- expected_dtype = metadata["dtype"]
111
-
112
- # Type checking
113
- if expected_dtype is int:
114
- if not isinstance(value, int):
115
- try:
116
- value = int(value)
117
- except (ValueError, TypeError):
118
- return False
119
- elif expected_dtype is float:
120
- if not isinstance(value, (int, float)):
121
- try:
122
- value = float(value)
123
- except (ValueError, TypeError):
124
- return False
125
- elif expected_dtype is bool:
126
- if not isinstance(value, bool):
127
- return False
128
- elif expected_dtype is str:
129
- if not isinstance(value, str):
130
- return False
131
-
132
- # Range validation for numeric types
133
- if expected_dtype in (int, float) and isinstance(value, (int, float)):
134
- if "min_value" in metadata and value < metadata["min_value"]:
135
- return False
136
- if "max_value" in metadata and value > metadata["max_value"]:
137
- return False
138
-
139
- # Allowed values validation for strings
140
- if expected_dtype is str and "allowed_values" in metadata:
141
- if value not in metadata["allowed_values"]:
142
- return False
143
-
144
- return True
145
-
146
- def set(self, param_name: str, value: Any, validate: bool = True) -> bool:
147
- """
148
- Set a parameter value with optional validation.
149
-
150
- Args:
151
- param_name: Name of the parameter
152
- value: New value for the parameter
153
- validate: Whether to validate the value before setting
154
-
155
- Returns:
156
- True if parameter was set successfully, False otherwise
157
- """
158
- if not hasattr(self, param_name):
159
- return False
160
-
161
- if validate and not self.validate(param_name, value):
162
- return False
163
-
164
- # Convert to expected type if needed
165
- if param_name in self._param_metadata:
166
- expected_dtype = self._param_metadata[param_name]["dtype"]
167
-
168
- if expected_dtype is int and not isinstance(value, int):
169
- try:
170
- value = int(value)
171
- except (ValueError, TypeError):
172
- if validate:
173
- return False
174
- elif expected_dtype is float and not isinstance(value, float):
175
- try:
176
- value = float(value)
177
- except (ValueError, TypeError):
178
- if validate:
179
- return False
180
-
181
- setattr(self, param_name, value)
182
- return True
183
-
184
- def get(self, param_name: str) -> Any:
185
- """
186
- Get the value of a parameter by name.
187
-
188
- Args:
189
- param_name: Name of the parameter
190
-
191
- Returns:
192
- Current value of the parameter
193
- """
194
- if not hasattr(self, param_name):
195
- raise KeyError(f"Parameter '{param_name}' not found")
196
- return getattr(self, param_name)
197
-
198
- def set_from_dict(
199
- self,
200
- param_dict: dict[str, Any],
201
- validate: bool = True,
202
- ) -> list[str]:
203
- """
204
- Update multiple parameters from a dictionary.
205
-
206
- Args:
207
- param_dict: Dictionary of parameter names and values
208
- validate: Whether to validate values before setting
209
-
210
- Returns:
211
- List of parameter names that could not be set
212
- """
213
- failed_params = []
214
-
215
- for param_name, value in param_dict.items():
216
- if not self.set(param_name, value, validate):
217
- failed_params.append(param_name)
218
-
219
- return failed_params
220
-
221
- def to_dict(self) -> dict[str, Any]:
222
- """
223
- Convert parameters to dictionary, excluding metadata.
224
-
225
- Returns:
226
- Dictionary of parameter names and values
227
- """
228
- return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}
229
-
230
- def list_parameters(self) -> list[str]:
231
- """
232
- Get list of all parameter names.
233
-
234
- Returns:
235
- List of parameter names
236
- """
237
- return [k for k in self.__dict__.keys() if not k.startswith("_")]
238
-
239
- def validate_all(self) -> tuple[bool, list[str]]:
240
- """
241
- Validate all parameters in the instance.
242
-
243
- Returns:
244
- Tuple of (all_valid, list_of_invalid_params)
245
- - all_valid: True if all parameters are valid, False otherwise
246
- - list_of_invalid_params: List of parameter names that failed validation
247
- """
248
- invalid_params = []
249
-
250
- for param_name in self.list_parameters():
251
- if param_name in self._param_metadata:
252
- current_value = getattr(self, param_name)
253
- if not self.validate(param_name, current_value):
254
- invalid_params.append(param_name)
255
-
256
- return len(invalid_params) == 0, invalid_params
1
+ """Parameter class for Study find_consensus method."""
2
+
3
+ from dataclasses import dataclass, field
4
+ from typing import Any
5
+
6
+
7
+ @dataclass
8
+ class find_consensus_defaults:
9
+ """
10
+ Parameter class for Study find_consensus method.
11
+
12
+ This class encapsulates parameters for consensus feature detection across samples,
13
+ including algorithm selection, grouping tolerances, and minimum sample requirements.
14
+
15
+ Attributes:
16
+ algorithm (str): Feature grouping algorithm. Default is "qt".
17
+ min_samples (int): Minimum number of samples for a consensus feature. Default is 1.
18
+ link_ms2 (bool): Whether to link MS2 spectra to consensus features. Default is True.
19
+ mz_tol (float): m/z tolerance for grouping (Da). Default is 0.01.
20
+ rt_tol (float): RT tolerance for grouping (seconds). Default is 1.0.
21
+ """
22
+
23
+ algorithm: str = "qt"
24
+ min_samples: int = 1
25
+ link_ms2: bool = True
26
+ mz_tol: float = 0.01
27
+ rt_tol: float = 1.0
28
+
29
+ _param_metadata: dict[str, dict[str, Any]] = field(
30
+ default_factory=lambda: {
31
+ "algorithm": {
32
+ "dtype": str,
33
+ "description": "Feature grouping algorithm",
34
+ "default": "qt",
35
+ "allowed_values": ["qt", "kd", "unlabeled", "sequential"],
36
+ },
37
+ "min_samples": {
38
+ "dtype": int,
39
+ "description": "Minimum number of samples for a consensus feature",
40
+ "default": 1,
41
+ "min_value": 1,
42
+ },
43
+ "link_ms2": {
44
+ "dtype": bool,
45
+ "description": "Whether to link MS2 spectra to consensus features",
46
+ "default": True,
47
+ },
48
+ "mz_tol": {
49
+ "dtype": float,
50
+ "description": "m/z tolerance for grouping (Da)",
51
+ "default": 0.01,
52
+ "min_value": 0.001,
53
+ "max_value": 1.0,
54
+ },
55
+ "rt_tol": {
56
+ "dtype": float,
57
+ "description": "RT tolerance for grouping (seconds)",
58
+ "default": 1.0,
59
+ "min_value": 0.1,
60
+ "max_value": 60.0,
61
+ },
62
+ },
63
+ repr=False,
64
+ )
65
+
66
+ def get_info(self, param_name: str) -> dict[str, Any]:
67
+ """
68
+ Get information about a specific parameter.
69
+
70
+ Args:
71
+ param_name: Name of the parameter
72
+
73
+ Returns:
74
+ Dictionary containing parameter metadata
75
+
76
+ Raises:
77
+ KeyError: If parameter name is not found
78
+ """
79
+ if param_name not in self._param_metadata:
80
+ raise KeyError(f"Parameter '{param_name}' not found")
81
+ return self._param_metadata[param_name]
82
+
83
+ def get_description(self, param_name: str) -> str:
84
+ """
85
+ Get description for a specific parameter.
86
+
87
+ Args:
88
+ param_name: Name of the parameter
89
+
90
+ Returns:
91
+ Parameter description string
92
+ """
93
+ return str(self.get_info(param_name)["description"])
94
+
95
+ def validate(self, param_name: str, value: Any) -> bool:
96
+ """
97
+ Validate a parameter value against its constraints.
98
+
99
+ Args:
100
+ param_name: Name of the parameter
101
+ value: Value to validate
102
+
103
+ Returns:
104
+ True if value is valid, False otherwise
105
+ """
106
+ if param_name not in self._param_metadata:
107
+ return False
108
+
109
+ metadata = self._param_metadata[param_name]
110
+ expected_dtype = metadata["dtype"]
111
+
112
+ # Type checking
113
+ if expected_dtype is int:
114
+ if not isinstance(value, int):
115
+ try:
116
+ value = int(value)
117
+ except (ValueError, TypeError):
118
+ return False
119
+ elif expected_dtype is float:
120
+ if not isinstance(value, (int, float)):
121
+ try:
122
+ value = float(value)
123
+ except (ValueError, TypeError):
124
+ return False
125
+ elif expected_dtype is bool:
126
+ if not isinstance(value, bool):
127
+ return False
128
+ elif expected_dtype is str:
129
+ if not isinstance(value, str):
130
+ return False
131
+
132
+ # Range validation for numeric types
133
+ if expected_dtype in (int, float) and isinstance(value, (int, float)):
134
+ if "min_value" in metadata and value < metadata["min_value"]:
135
+ return False
136
+ if "max_value" in metadata and value > metadata["max_value"]:
137
+ return False
138
+
139
+ # Allowed values validation for strings
140
+ if expected_dtype is str and "allowed_values" in metadata:
141
+ if value not in metadata["allowed_values"]:
142
+ return False
143
+
144
+ return True
145
+
146
+ def set(self, param_name: str, value: Any, validate: bool = True) -> bool:
147
+ """
148
+ Set a parameter value with optional validation.
149
+
150
+ Args:
151
+ param_name: Name of the parameter
152
+ value: New value for the parameter
153
+ validate: Whether to validate the value before setting
154
+
155
+ Returns:
156
+ True if parameter was set successfully, False otherwise
157
+ """
158
+ if not hasattr(self, param_name):
159
+ return False
160
+
161
+ if validate and not self.validate(param_name, value):
162
+ return False
163
+
164
+ # Convert to expected type if needed
165
+ if param_name in self._param_metadata:
166
+ expected_dtype = self._param_metadata[param_name]["dtype"]
167
+
168
+ if expected_dtype is int and not isinstance(value, int):
169
+ try:
170
+ value = int(value)
171
+ except (ValueError, TypeError):
172
+ if validate:
173
+ return False
174
+ elif expected_dtype is float and not isinstance(value, float):
175
+ try:
176
+ value = float(value)
177
+ except (ValueError, TypeError):
178
+ if validate:
179
+ return False
180
+
181
+ setattr(self, param_name, value)
182
+ return True
183
+
184
+ def get(self, param_name: str) -> Any:
185
+ """
186
+ Get the value of a parameter by name.
187
+
188
+ Args:
189
+ param_name: Name of the parameter
190
+
191
+ Returns:
192
+ Current value of the parameter
193
+ """
194
+ if not hasattr(self, param_name):
195
+ raise KeyError(f"Parameter '{param_name}' not found")
196
+ return getattr(self, param_name)
197
+
198
+ def set_from_dict(
199
+ self,
200
+ param_dict: dict[str, Any],
201
+ validate: bool = True,
202
+ ) -> list[str]:
203
+ """
204
+ Update multiple parameters from a dictionary.
205
+
206
+ Args:
207
+ param_dict: Dictionary of parameter names and values
208
+ validate: Whether to validate values before setting
209
+
210
+ Returns:
211
+ List of parameter names that could not be set
212
+ """
213
+ failed_params = []
214
+
215
+ for param_name, value in param_dict.items():
216
+ if not self.set(param_name, value, validate):
217
+ failed_params.append(param_name)
218
+
219
+ return failed_params
220
+
221
+ def to_dict(self) -> dict[str, Any]:
222
+ """
223
+ Convert parameters to dictionary, excluding metadata.
224
+
225
+ Returns:
226
+ Dictionary of parameter names and values
227
+ """
228
+ return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}
229
+
230
+ def list_parameters(self) -> list[str]:
231
+ """
232
+ Get list of all parameter names.
233
+
234
+ Returns:
235
+ List of parameter names
236
+ """
237
+ return [k for k in self.__dict__.keys() if not k.startswith("_")]
238
+
239
+ def validate_all(self) -> tuple[bool, list[str]]:
240
+ """
241
+ Validate all parameters in the instance.
242
+
243
+ Returns:
244
+ Tuple of (all_valid, list_of_invalid_params)
245
+ - all_valid: True if all parameters are valid, False otherwise
246
+ - list_of_invalid_params: List of parameter names that failed validation
247
+ """
248
+ invalid_params = []
249
+
250
+ for param_name in self.list_parameters():
251
+ if param_name in self._param_metadata:
252
+ current_value = getattr(self, param_name)
253
+ if not self.validate(param_name, current_value):
254
+ invalid_params.append(param_name)
255
+
256
+ return len(invalid_params) == 0, invalid_params