ChessAnalysisPipeline 0.0.17.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.
- CHAP/TaskManager.py +216 -0
- CHAP/__init__.py +27 -0
- CHAP/common/__init__.py +57 -0
- CHAP/common/models/__init__.py +8 -0
- CHAP/common/models/common.py +124 -0
- CHAP/common/models/integration.py +659 -0
- CHAP/common/models/map.py +1291 -0
- CHAP/common/processor.py +2869 -0
- CHAP/common/reader.py +658 -0
- CHAP/common/utils.py +110 -0
- CHAP/common/writer.py +730 -0
- CHAP/edd/__init__.py +23 -0
- CHAP/edd/models.py +876 -0
- CHAP/edd/processor.py +3069 -0
- CHAP/edd/reader.py +1023 -0
- CHAP/edd/select_material_params_gui.py +348 -0
- CHAP/edd/utils.py +1572 -0
- CHAP/edd/writer.py +26 -0
- CHAP/foxden/__init__.py +19 -0
- CHAP/foxden/models.py +71 -0
- CHAP/foxden/processor.py +124 -0
- CHAP/foxden/reader.py +224 -0
- CHAP/foxden/utils.py +80 -0
- CHAP/foxden/writer.py +168 -0
- CHAP/giwaxs/__init__.py +11 -0
- CHAP/giwaxs/models.py +491 -0
- CHAP/giwaxs/processor.py +776 -0
- CHAP/giwaxs/reader.py +8 -0
- CHAP/giwaxs/writer.py +8 -0
- CHAP/inference/__init__.py +7 -0
- CHAP/inference/processor.py +69 -0
- CHAP/inference/reader.py +8 -0
- CHAP/inference/writer.py +8 -0
- CHAP/models.py +227 -0
- CHAP/pipeline.py +479 -0
- CHAP/processor.py +125 -0
- CHAP/reader.py +124 -0
- CHAP/runner.py +277 -0
- CHAP/saxswaxs/__init__.py +7 -0
- CHAP/saxswaxs/processor.py +8 -0
- CHAP/saxswaxs/reader.py +8 -0
- CHAP/saxswaxs/writer.py +8 -0
- CHAP/server.py +125 -0
- CHAP/sin2psi/__init__.py +7 -0
- CHAP/sin2psi/processor.py +8 -0
- CHAP/sin2psi/reader.py +8 -0
- CHAP/sin2psi/writer.py +8 -0
- CHAP/tomo/__init__.py +15 -0
- CHAP/tomo/models.py +210 -0
- CHAP/tomo/processor.py +3862 -0
- CHAP/tomo/reader.py +9 -0
- CHAP/tomo/writer.py +59 -0
- CHAP/utils/__init__.py +6 -0
- CHAP/utils/converters.py +188 -0
- CHAP/utils/fit.py +2947 -0
- CHAP/utils/general.py +2655 -0
- CHAP/utils/material.py +274 -0
- CHAP/utils/models.py +595 -0
- CHAP/utils/parfile.py +224 -0
- CHAP/writer.py +122 -0
- MLaaS/__init__.py +0 -0
- MLaaS/ktrain.py +205 -0
- MLaaS/mnist_img.py +83 -0
- MLaaS/tfaas_client.py +371 -0
- chessanalysispipeline-0.0.17.dev3.dist-info/LICENSE +60 -0
- chessanalysispipeline-0.0.17.dev3.dist-info/METADATA +29 -0
- chessanalysispipeline-0.0.17.dev3.dist-info/RECORD +70 -0
- chessanalysispipeline-0.0.17.dev3.dist-info/WHEEL +5 -0
- chessanalysispipeline-0.0.17.dev3.dist-info/entry_points.txt +2 -0
- chessanalysispipeline-0.0.17.dev3.dist-info/top_level.txt +2 -0
CHAP/utils/models.py
ADDED
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
"""Utils Pydantic model classes."""
|
|
2
|
+
|
|
3
|
+
# System modules
|
|
4
|
+
from typing import (
|
|
5
|
+
Literal,
|
|
6
|
+
Optional,
|
|
7
|
+
Union,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
# Third party imports
|
|
11
|
+
from pydantic import (
|
|
12
|
+
Field,
|
|
13
|
+
PrivateAttr,
|
|
14
|
+
StrictBool,
|
|
15
|
+
conint,
|
|
16
|
+
conlist,
|
|
17
|
+
confloat,
|
|
18
|
+
constr,
|
|
19
|
+
field_validator,
|
|
20
|
+
)
|
|
21
|
+
from typing_extensions import Annotated
|
|
22
|
+
import numpy as np
|
|
23
|
+
|
|
24
|
+
# Local modules
|
|
25
|
+
from CHAP.models import CHAPBaseModel
|
|
26
|
+
from CHAP.utils.general import not_zero, tiny
|
|
27
|
+
|
|
28
|
+
# pylint: disable=no-member
|
|
29
|
+
tiny = np.finfo(np.float64).resolution
|
|
30
|
+
# pylint: enable=no-member
|
|
31
|
+
s2pi = np.sqrt(2*np.pi)
|
|
32
|
+
|
|
33
|
+
#def constant(x, c=0.5):
|
|
34
|
+
def constant(x, c=0.0):
|
|
35
|
+
"""Return a linear function.
|
|
36
|
+
|
|
37
|
+
constant(x, c) = c
|
|
38
|
+
"""
|
|
39
|
+
return c*np.ones((x.size))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
#def linear(x, slope=0.9, intercept=0.1):
|
|
43
|
+
def linear(x, slope=1.0, intercept=0.0):
|
|
44
|
+
"""Return a linear function.
|
|
45
|
+
|
|
46
|
+
linear(x, slope, intercept) = slope * x + intercept
|
|
47
|
+
"""
|
|
48
|
+
return slope * x + intercept
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
#def quadratic(x, a=0.5, b=0.4, c=0.1):
|
|
52
|
+
def quadratic(x, a=0.0, b=0.0, c=0.0):
|
|
53
|
+
"""Return a parabolic function.
|
|
54
|
+
|
|
55
|
+
parabolic(x, a, b, c) = a * x**2 + b * x + c
|
|
56
|
+
"""
|
|
57
|
+
return (a*x + b) * x + c
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
#def exponential(x, amplitude=1.0, decay=0.3):
|
|
61
|
+
def exponential(x, amplitude=1.0, decay=1.0):
|
|
62
|
+
"""Return an exponential function.
|
|
63
|
+
|
|
64
|
+
exponential(x, amplitude, decay) = amplitude * exp(-x/decay)
|
|
65
|
+
"""
|
|
66
|
+
return amplitude * np.exp(-x/not_zero(decay))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
#def gaussian(x, amplitude=0.25, center=0.5, sigma=0.1):
|
|
70
|
+
def gaussian(x, amplitude=1.0, center=0.0, sigma=1.0):
|
|
71
|
+
"""Return a 1-dimensional Gaussian function.
|
|
72
|
+
|
|
73
|
+
gaussian(x, amplitude, center, sigma) =
|
|
74
|
+
(amplitude/(s2pi*sigma)) * exp(-(x-center)**2 / (2*sigma**2))
|
|
75
|
+
"""
|
|
76
|
+
return ((amplitude/(max(tiny, s2pi*sigma)))
|
|
77
|
+
* np.exp(-(x-center)**2 / max(tiny, (2*sigma**2))))
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
#def lorentzian(x, amplitude=0.3, center=0.5, sigma=0.1):
|
|
81
|
+
def lorentzian(x, amplitude=1.0, center=0.0, sigma=1.0):
|
|
82
|
+
"""Return a 1-dimensional Lorentzian function.
|
|
83
|
+
|
|
84
|
+
lorentzian(x, amplitude, center, sigma) =
|
|
85
|
+
(amplitude/(1 + ((1.0*x-center)/sigma)**2)) / (pi*sigma)
|
|
86
|
+
"""
|
|
87
|
+
return ((amplitude/(1 + ((x-center)/max(tiny, sigma))**2))
|
|
88
|
+
/ max(tiny, (np.pi*sigma)))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def rectangle(
|
|
92
|
+
x, amplitude=1.0, center1=0.0, sigma1=1.0, center2=1.0,
|
|
93
|
+
sigma2=1.0, form='linear'):
|
|
94
|
+
"""Return a rectangle function.
|
|
95
|
+
|
|
96
|
+
Starts at 0.0, rises to `amplitude` (at `center1` with width
|
|
97
|
+
`sigma1`), then drops to 0.0 (at `center2` with width `sigma2`)
|
|
98
|
+
with `form`:
|
|
99
|
+
- `'linear'` (default) = ramp_up + ramp_down
|
|
100
|
+
- `'atan'`, `'arctan`' = amplitude*(atan(arg1) + atan(arg2))/pi
|
|
101
|
+
- `'erf'` = amplitude*(erf(arg1) + erf(arg2))/2.
|
|
102
|
+
- `'logisitic'` = amplitude*[1 - 1/(1 + exp(arg1)) -
|
|
103
|
+
1/(1+exp(arg2))]
|
|
104
|
+
|
|
105
|
+
where ``arg1 = (x - center1)/sigma1`` and
|
|
106
|
+
``arg2 = -(x - center2)/sigma2``.
|
|
107
|
+
"""
|
|
108
|
+
arg1 = (x - center1)/max(tiny, sigma1)
|
|
109
|
+
arg2 = (center2 - x)/max(tiny, sigma2)
|
|
110
|
+
|
|
111
|
+
if form == 'erf':
|
|
112
|
+
# Third party modules
|
|
113
|
+
# pylint: disable=no-name-in-module
|
|
114
|
+
from scipy.special import erf
|
|
115
|
+
|
|
116
|
+
rect = 0.5*(erf(arg1) + erf(arg2))
|
|
117
|
+
elif form == 'logistic':
|
|
118
|
+
rect = 1. - 1./(1. + np.exp(arg1)) - 1./(1. + np.exp(arg2))
|
|
119
|
+
elif form in ('atan', 'arctan'):
|
|
120
|
+
rect = (np.arctan(arg1) + np.arctan(arg2))/np.pi
|
|
121
|
+
elif form == 'linear':
|
|
122
|
+
rect = 0.5*(np.minimum(1, np.maximum(-1, arg1))
|
|
123
|
+
+ np.minimum(1, np.maximum(-1, arg2)))
|
|
124
|
+
else:
|
|
125
|
+
raise ValueError(f'Invalid parameter form ({form})')
|
|
126
|
+
|
|
127
|
+
return amplitude*rect
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def validate_parameters(parameters, info):
|
|
131
|
+
"""Validate the parameters.
|
|
132
|
+
|
|
133
|
+
:param parameters: Fit model parameters.
|
|
134
|
+
:type parameters: list[FitParameter]
|
|
135
|
+
:param info: Pydantic validator info object.
|
|
136
|
+
:type info: pydantic_core._pydantic_core.ValidationInfo
|
|
137
|
+
:return: List of fit model parameters.
|
|
138
|
+
:rtype: list[FitParameter]
|
|
139
|
+
"""
|
|
140
|
+
# System imports
|
|
141
|
+
import inspect
|
|
142
|
+
|
|
143
|
+
if 'model' in info.data:
|
|
144
|
+
model = info.data['model']
|
|
145
|
+
else:
|
|
146
|
+
model = None
|
|
147
|
+
if model is None or model == 'expression':
|
|
148
|
+
return parameters
|
|
149
|
+
sig = dict(inspect.signature(models[model]).parameters.items())
|
|
150
|
+
sig.pop('x')
|
|
151
|
+
|
|
152
|
+
# Check input model parameter validity
|
|
153
|
+
for par in parameters:
|
|
154
|
+
if par.name not in sig:
|
|
155
|
+
raise ValueError('Invalid parameter {par.name} in {model} model')
|
|
156
|
+
|
|
157
|
+
# Set model parameters
|
|
158
|
+
output_parameters = []
|
|
159
|
+
for sig_name, sig_par in sig.items():
|
|
160
|
+
if model == 'rectangle' and sig_name == 'form':
|
|
161
|
+
continue
|
|
162
|
+
for par in parameters:
|
|
163
|
+
if sig_name == par.name:
|
|
164
|
+
break
|
|
165
|
+
else:
|
|
166
|
+
par = FitParameter(name=sig_name)
|
|
167
|
+
if sig_par.default != sig_par.empty:
|
|
168
|
+
par._default = sig_par.default
|
|
169
|
+
output_parameters.append(par)
|
|
170
|
+
|
|
171
|
+
return output_parameters
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class FitParameter(CHAPBaseModel):
|
|
175
|
+
"""Class representing a specific fit parameter for the fit
|
|
176
|
+
processor.
|
|
177
|
+
"""
|
|
178
|
+
name: constr(strip_whitespace=True, min_length=1)
|
|
179
|
+
value: Optional[confloat(allow_inf_nan=False)] = None
|
|
180
|
+
min: Optional[confloat()] = -np.inf
|
|
181
|
+
max: Optional[confloat()] = np.inf
|
|
182
|
+
vary: StrictBool = True
|
|
183
|
+
expr: Optional[constr(strip_whitespace=True, min_length=1)] = None
|
|
184
|
+
_default: float = PrivateAttr()
|
|
185
|
+
_init_value: float = PrivateAttr()
|
|
186
|
+
_prefix: str = PrivateAttr()
|
|
187
|
+
_stderr: float = PrivateAttr()
|
|
188
|
+
|
|
189
|
+
@field_validator('min')
|
|
190
|
+
@classmethod
|
|
191
|
+
def validate_min(cls, value):
|
|
192
|
+
"""Validate the specified min.
|
|
193
|
+
|
|
194
|
+
:param value: Field value to validate (`min`).
|
|
195
|
+
:type value: Union[float, None]
|
|
196
|
+
:return: Lower bound of fit parameter.
|
|
197
|
+
:rtype: float
|
|
198
|
+
"""
|
|
199
|
+
if value is None:
|
|
200
|
+
return -np.inf
|
|
201
|
+
return value
|
|
202
|
+
|
|
203
|
+
@field_validator('max')
|
|
204
|
+
@classmethod
|
|
205
|
+
def validate_max(cls, value):
|
|
206
|
+
"""Validate the specified max.
|
|
207
|
+
|
|
208
|
+
:param value: Field value to validate (`max`).
|
|
209
|
+
:type value: Union[float, None]
|
|
210
|
+
:return: Upper bound of fit parameter.
|
|
211
|
+
:rtype: float
|
|
212
|
+
"""
|
|
213
|
+
if value is None:
|
|
214
|
+
return np.inf
|
|
215
|
+
return value
|
|
216
|
+
|
|
217
|
+
@property
|
|
218
|
+
def default(self):
|
|
219
|
+
"""Return the _default attribute."""
|
|
220
|
+
if hasattr(self, '_default'):
|
|
221
|
+
return self._default
|
|
222
|
+
return None
|
|
223
|
+
|
|
224
|
+
@property
|
|
225
|
+
def init_value(self):
|
|
226
|
+
"""Return the _init_value attribute."""
|
|
227
|
+
if hasattr(self, '_init_value'):
|
|
228
|
+
return self._init_value
|
|
229
|
+
return None
|
|
230
|
+
|
|
231
|
+
@property
|
|
232
|
+
def prefix(self):
|
|
233
|
+
"""Return the _prefix attribute."""
|
|
234
|
+
if hasattr(self, '_prefix'):
|
|
235
|
+
return self._prefix
|
|
236
|
+
return None
|
|
237
|
+
|
|
238
|
+
@property
|
|
239
|
+
def stderr(self):
|
|
240
|
+
"""Return the _stderr attribute."""
|
|
241
|
+
if hasattr(self, '_stderr'):
|
|
242
|
+
return self._stderr
|
|
243
|
+
return None
|
|
244
|
+
|
|
245
|
+
def set(self, value=None, min=None, max=None, vary=None, expr=None):
|
|
246
|
+
"""Set or update FitParameter attributes.
|
|
247
|
+
|
|
248
|
+
:param value: Parameter value.
|
|
249
|
+
:type value: float, optional
|
|
250
|
+
:param min: Lower Parameter value bound. To remove the lower
|
|
251
|
+
bound you must set min to `numpy.inf`.
|
|
252
|
+
:type min: bool, optional
|
|
253
|
+
:param max: Upper Parameter value bound. To remove the lower
|
|
254
|
+
bound you must set max to `numpy.inf`.
|
|
255
|
+
:type max: bool, optional
|
|
256
|
+
:param vary: Whether the Parameter is varied during a fit.
|
|
257
|
+
:type vary: bool, optional
|
|
258
|
+
:param expr: Mathematical expression used to constrain the
|
|
259
|
+
value during the fit. To remove a constraint you must
|
|
260
|
+
supply an empty string.
|
|
261
|
+
:type expr: str, optional
|
|
262
|
+
"""
|
|
263
|
+
if expr is not None:
|
|
264
|
+
if not isinstance(expr, str):
|
|
265
|
+
raise ValueError(f'Invalid parameter expr ({expr})')
|
|
266
|
+
if expr == '':
|
|
267
|
+
expr = None
|
|
268
|
+
self.expr = expr
|
|
269
|
+
if expr is not None:
|
|
270
|
+
self.value = None
|
|
271
|
+
self.min = -np.inf
|
|
272
|
+
self.max = np.inf
|
|
273
|
+
self.vary = False
|
|
274
|
+
return
|
|
275
|
+
if min is not None:
|
|
276
|
+
if not isinstance(min, (int, float)):
|
|
277
|
+
raise ValueError(f'Invalid parameter min ({min})')
|
|
278
|
+
self.min = min
|
|
279
|
+
if max is not None:
|
|
280
|
+
if not isinstance(max, (int, float)):
|
|
281
|
+
raise ValueError(f'Invalid parameter max ({max})')
|
|
282
|
+
self.max = max
|
|
283
|
+
if vary is not None:
|
|
284
|
+
if not isinstance(vary, bool):
|
|
285
|
+
raise ValueError(f'Invalid parameter vary ({vary})')
|
|
286
|
+
self.vary = vary
|
|
287
|
+
if value is not None:
|
|
288
|
+
if not isinstance(value, (int, float)):
|
|
289
|
+
raise ValueError(f'Invalid parameter value ({value})')
|
|
290
|
+
self.value = value
|
|
291
|
+
if self.value > self.max:
|
|
292
|
+
self.value = self.max
|
|
293
|
+
elif self.value < self.min:
|
|
294
|
+
self.value = self.min
|
|
295
|
+
self.expr = None
|
|
296
|
+
|
|
297
|
+
class Constant(CHAPBaseModel):
|
|
298
|
+
"""Class representing a Constant model component.
|
|
299
|
+
|
|
300
|
+
:ivar model: The model component base name (a prefix will be added
|
|
301
|
+
if multiple identical model components are added).
|
|
302
|
+
:type model: Literal['constant']
|
|
303
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
304
|
+
generated from the function signature (excluding the
|
|
305
|
+
independent variable), defaults to `[]`.
|
|
306
|
+
:type parameters: list[FitParameter], optional
|
|
307
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
308
|
+
:type prefix: str, optional
|
|
309
|
+
"""
|
|
310
|
+
model: Literal['constant']
|
|
311
|
+
parameters: Annotated[
|
|
312
|
+
conlist(item_type=FitParameter),
|
|
313
|
+
Field(validate_default=True)] = []
|
|
314
|
+
prefix: Optional[str] = ''
|
|
315
|
+
|
|
316
|
+
_validate_parameters_parameters = field_validator(
|
|
317
|
+
'parameters')(validate_parameters)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
class Linear(CHAPBaseModel):
|
|
321
|
+
"""Class representing a Linear model component.
|
|
322
|
+
|
|
323
|
+
:ivar model: The model component base name (a prefix will be added
|
|
324
|
+
if multiple identical model components are added).
|
|
325
|
+
:type model: Literal['linear']
|
|
326
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
327
|
+
generated from the function signature (excluding the
|
|
328
|
+
independent variable), defaults to `[]`.
|
|
329
|
+
:type parameters: list[FitParameter], optional
|
|
330
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
331
|
+
:type prefix: str, optional
|
|
332
|
+
"""
|
|
333
|
+
model: Literal['linear']
|
|
334
|
+
parameters: Annotated[
|
|
335
|
+
conlist(item_type=FitParameter),
|
|
336
|
+
Field(validate_default=True)] = []
|
|
337
|
+
prefix: Optional[str] = ''
|
|
338
|
+
|
|
339
|
+
_validate_parameters_parameters = field_validator(
|
|
340
|
+
'parameters')(validate_parameters)
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
class Quadratic(CHAPBaseModel):
|
|
344
|
+
"""Class representing a Quadratic model component.
|
|
345
|
+
|
|
346
|
+
:ivar model: The model component base name (a prefix will be added
|
|
347
|
+
if multiple identical model components are added).
|
|
348
|
+
:type model: Literal['quadratic']
|
|
349
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
350
|
+
generated from the function signature (excluding the
|
|
351
|
+
independent variable), defaults to `[]`.
|
|
352
|
+
:type parameters: list[FitParameter], optional
|
|
353
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
354
|
+
:type prefix: str, optional
|
|
355
|
+
"""
|
|
356
|
+
model: Literal['quadratic']
|
|
357
|
+
parameters: Annotated[
|
|
358
|
+
conlist(item_type=FitParameter),
|
|
359
|
+
Field(validate_default=True)] = []
|
|
360
|
+
prefix: Optional[str] = ''
|
|
361
|
+
|
|
362
|
+
_validate_parameters_parameters = field_validator(
|
|
363
|
+
'parameters')(validate_parameters)
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
class Exponential(CHAPBaseModel):
|
|
367
|
+
"""Class representing an Exponential model component.
|
|
368
|
+
|
|
369
|
+
:ivar model: The model component base name (a prefix will be added
|
|
370
|
+
if multiple identical model components are added).
|
|
371
|
+
:type model: Literal['exponential']
|
|
372
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
373
|
+
generated from the function signature (excluding the
|
|
374
|
+
independent variable), defaults to `[]`.
|
|
375
|
+
:type parameters: list[FitParameter], optional
|
|
376
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
377
|
+
:type prefix: str, optional
|
|
378
|
+
"""
|
|
379
|
+
model: Literal['exponential']
|
|
380
|
+
parameters: Annotated[
|
|
381
|
+
conlist(item_type=FitParameter),
|
|
382
|
+
Field(validate_default=True)] = []
|
|
383
|
+
prefix: Optional[str] = ''
|
|
384
|
+
|
|
385
|
+
_validate_parameters_parameters = field_validator(
|
|
386
|
+
'parameters')(validate_parameters)
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
class Gaussian(CHAPBaseModel):
|
|
390
|
+
"""Class representing a Gaussian model component.
|
|
391
|
+
|
|
392
|
+
:ivar model: The model component base name (a prefix will be added
|
|
393
|
+
if multiple identical model components are added).
|
|
394
|
+
:type model: Literal['gaussian']
|
|
395
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
396
|
+
generated from the function signature (excluding the
|
|
397
|
+
independent variable), defaults to `[]`.
|
|
398
|
+
:type parameters: list[FitParameter], optional
|
|
399
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
400
|
+
:type prefix: str, optional
|
|
401
|
+
"""
|
|
402
|
+
model: Literal['gaussian']
|
|
403
|
+
parameters: Annotated[
|
|
404
|
+
conlist(item_type=FitParameter),
|
|
405
|
+
Field(validate_default=True)] = []
|
|
406
|
+
prefix: Optional[str] = ''
|
|
407
|
+
|
|
408
|
+
_validate_parameters_parameters = field_validator(
|
|
409
|
+
'parameters')(validate_parameters)
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
class Lorentzian(CHAPBaseModel):
|
|
413
|
+
"""Class representing a Lorentzian model component.
|
|
414
|
+
|
|
415
|
+
:ivar model: The model component base name (a prefix will be added
|
|
416
|
+
if multiple identical model components are added).
|
|
417
|
+
:type model: Literal['lorentzian']
|
|
418
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
419
|
+
generated from the function signature (excluding the
|
|
420
|
+
independent variable), defaults to `[]`.
|
|
421
|
+
:type parameters: list[FitParameter], optional
|
|
422
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
423
|
+
:type prefix: str, optional
|
|
424
|
+
"""
|
|
425
|
+
model: Literal['lorentzian']
|
|
426
|
+
parameters: Annotated[
|
|
427
|
+
conlist(item_type=FitParameter),
|
|
428
|
+
Field(validate_default=True)] = []
|
|
429
|
+
prefix: Optional[str] = ''
|
|
430
|
+
|
|
431
|
+
_validate_parameters_parameters = field_validator(
|
|
432
|
+
'parameters')(validate_parameters)
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
class Rectangle(CHAPBaseModel):
|
|
436
|
+
"""Class representing a Rectangle model component.
|
|
437
|
+
|
|
438
|
+
:ivar model: The model component base name (a prefix will be added
|
|
439
|
+
if multiple identical model components are added).
|
|
440
|
+
:type model: Literal['rectangle']
|
|
441
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
442
|
+
generated from the function signature (excluding the
|
|
443
|
+
independent variable), defaults to `[]`.
|
|
444
|
+
:type parameters: list[FitParameter], optional
|
|
445
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
446
|
+
:type prefix: str, optional
|
|
447
|
+
"""
|
|
448
|
+
model: Literal['rectangle']
|
|
449
|
+
parameters: Annotated[
|
|
450
|
+
conlist(item_type=FitParameter),
|
|
451
|
+
Field(validate_default=True)] = []
|
|
452
|
+
prefix: Optional[str] = ''
|
|
453
|
+
|
|
454
|
+
_validate_parameters_parameters = field_validator(
|
|
455
|
+
'parameters')(validate_parameters)
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
class Expression(CHAPBaseModel):
|
|
459
|
+
"""Class representing an Expression model component.
|
|
460
|
+
|
|
461
|
+
:ivar model: The model component base name (a prefix will be added
|
|
462
|
+
if multiple identical model components are added).
|
|
463
|
+
:type model: Literal['expression']
|
|
464
|
+
:ivar expr: Mathematical expression to represent the model
|
|
465
|
+
component.
|
|
466
|
+
:type expr: str
|
|
467
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
468
|
+
generated from the model expression (excluding the
|
|
469
|
+
independent variable), defaults to `[]`.
|
|
470
|
+
:type parameters: list[FitParameter], optional
|
|
471
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
472
|
+
:type prefix: str, optional
|
|
473
|
+
"""
|
|
474
|
+
model: Literal['expression']
|
|
475
|
+
expr: constr(strip_whitespace=True, min_length=1)
|
|
476
|
+
parameters: Annotated[
|
|
477
|
+
conlist(item_type=FitParameter),
|
|
478
|
+
Field(validate_default=True)] = []
|
|
479
|
+
prefix: Optional[str] = ''
|
|
480
|
+
|
|
481
|
+
_validate_parameters_parameters = field_validator(
|
|
482
|
+
'parameters')(validate_parameters)
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
class Multipeak(CHAPBaseModel):
|
|
486
|
+
"""Class representing a multipeak model.
|
|
487
|
+
|
|
488
|
+
:ivar model: The model component base name (a prefix will be added
|
|
489
|
+
if multiple identical model components are added).
|
|
490
|
+
:type model: Literal['expression']
|
|
491
|
+
:ivar centers: Peak centers.
|
|
492
|
+
:type center: list[float]
|
|
493
|
+
:ivar centers_range: Range of peak centers around their centers.
|
|
494
|
+
:type centers_range: float, optional
|
|
495
|
+
:ivar fit_type: Type of fit, defaults to `'unconstrained'`.
|
|
496
|
+
:type fit_type: Literal['uniform', 'unconstrained'], optional.
|
|
497
|
+
:ivar fwhm_min: Lower limit of the fwhm of the peaks.
|
|
498
|
+
:type fwhm_min: float, optional
|
|
499
|
+
:ivar fwhm_max: Upper limit of the fwhm of the peaks.
|
|
500
|
+
:type fwhm_max: float, optional
|
|
501
|
+
:ivar peak_models: Type of peaks, defaults to `'gaussian'`.
|
|
502
|
+
:type peak_models: Literal['gaussian', 'lorentzian'], optional.
|
|
503
|
+
"""
|
|
504
|
+
model: Literal['multipeak']
|
|
505
|
+
centers: conlist(item_type=confloat(allow_inf_nan=False), min_length=1)
|
|
506
|
+
centers_range: Optional[confloat(allow_inf_nan=False)] = None
|
|
507
|
+
fit_type: Optional[Literal['uniform', 'unconstrained']] = 'unconstrained'
|
|
508
|
+
fwhm_min: Optional[confloat(allow_inf_nan=False)] = None
|
|
509
|
+
fwhm_max: Optional[confloat(allow_inf_nan=False)] = None
|
|
510
|
+
peak_models: Literal['gaussian', 'lorentzian'] = 'gaussian'
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
models = {
|
|
514
|
+
'constant': constant,
|
|
515
|
+
'linear': linear,
|
|
516
|
+
'quadratic': quadratic,
|
|
517
|
+
'exponential': exponential,
|
|
518
|
+
'gaussian': gaussian,
|
|
519
|
+
'lorentzian': lorentzian,
|
|
520
|
+
'rectangle': rectangle,
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
model_classes = (
|
|
524
|
+
Constant,
|
|
525
|
+
Linear,
|
|
526
|
+
Quadratic,
|
|
527
|
+
Exponential,
|
|
528
|
+
Gaussian,
|
|
529
|
+
Lorentzian,
|
|
530
|
+
Rectangle,
|
|
531
|
+
)
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
class FitConfig(CHAPBaseModel):
|
|
535
|
+
"""Class representing the configuration for the fit processor.
|
|
536
|
+
|
|
537
|
+
:ivar code: Specifies is lmfit is used to perform the fit or if
|
|
538
|
+
the scipy fit method is called directly, default to `'lmfit'`.
|
|
539
|
+
:type code: Literal['lmfit', 'scipy'], optional
|
|
540
|
+
:ivar parameters: Fit model parameters in addition to those
|
|
541
|
+
implicitly defined through the build-in model functions,
|
|
542
|
+
defaults to `[]`'
|
|
543
|
+
:type parameters: list[FitParameter], optional
|
|
544
|
+
:ivar models: The component(s) of the (composite) fit model.
|
|
545
|
+
:type models: Union[Constant, Linear, Quadratic, Exponential,
|
|
546
|
+
Gaussian, Lorentzian, Rectangle, Expression, Multipeak]
|
|
547
|
+
:ivar rel_height_cutoff: Relative peak height cutoff for
|
|
548
|
+
peak fitting (any peak with a height smaller than
|
|
549
|
+
`rel_height_cutoff` times the maximum height of all peaks
|
|
550
|
+
gets removed from the fit model).
|
|
551
|
+
:type rel_height_cutoff: float, optional
|
|
552
|
+
:ivar num_proc: The number of processors used in fitting a map
|
|
553
|
+
of data, defaults to `1`.
|
|
554
|
+
:type num_proc: int, optional
|
|
555
|
+
:ivar plot: Weather a plot of the fit result is generated,
|
|
556
|
+
defaults to `False`.
|
|
557
|
+
:type plot: bool, optional.
|
|
558
|
+
:ivar print_report: Weather to generate a fit result printout,
|
|
559
|
+
defaults to `False`.
|
|
560
|
+
:type print_report: bool, optional.
|
|
561
|
+
"""
|
|
562
|
+
code: Literal['lmfit', 'scipy'] = 'scipy'
|
|
563
|
+
parameters: conlist(item_type=FitParameter) = []
|
|
564
|
+
models: conlist(item_type=Union[
|
|
565
|
+
Constant, Linear, Quadratic, Exponential, Gaussian, Lorentzian,
|
|
566
|
+
Rectangle, Expression, Multipeak], min_length=1)
|
|
567
|
+
method: Literal[
|
|
568
|
+
'leastsq', 'trf', 'dogbox', 'lm', 'least_squares'] = 'leastsq'
|
|
569
|
+
rel_height_cutoff: Optional[
|
|
570
|
+
confloat(gt=0, lt=1.0, allow_inf_nan=False)] = None
|
|
571
|
+
num_proc: conint(gt=0) = 1
|
|
572
|
+
plot: StrictBool = False
|
|
573
|
+
print_report: StrictBool = False
|
|
574
|
+
memfolder: str = 'joblib_memmap'
|
|
575
|
+
|
|
576
|
+
@field_validator('method')
|
|
577
|
+
@classmethod
|
|
578
|
+
def validate_method(cls, method, info):
|
|
579
|
+
"""Validate the specified method.
|
|
580
|
+
|
|
581
|
+
:param method: The value of `method` to validate.
|
|
582
|
+
:type method: str
|
|
583
|
+
:param info: Pydantic validator info object.
|
|
584
|
+
:type info: pydantic_core._pydantic_core.ValidationInfo
|
|
585
|
+
:return: Fit method.
|
|
586
|
+
:rtype: str
|
|
587
|
+
"""
|
|
588
|
+
code = info.data['code']
|
|
589
|
+
if code == 'lmfit':
|
|
590
|
+
if method not in ('leastsq', 'least_squares'):
|
|
591
|
+
method = 'leastsq'
|
|
592
|
+
elif method == 'least_squares':
|
|
593
|
+
method = 'leastsq'
|
|
594
|
+
|
|
595
|
+
return method
|