ChessAnalysisPipeline 0.0.14__py3-none-any.whl → 0.0.16__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 ChessAnalysisPipeline might be problematic. Click here for more details.
- CHAP/__init__.py +1 -1
- CHAP/common/__init__.py +13 -0
- CHAP/common/models/integration.py +29 -26
- CHAP/common/models/map.py +395 -224
- CHAP/common/processor.py +1725 -93
- CHAP/common/reader.py +265 -28
- CHAP/common/writer.py +191 -18
- CHAP/edd/__init__.py +9 -2
- CHAP/edd/models.py +886 -665
- CHAP/edd/processor.py +2592 -936
- CHAP/edd/reader.py +889 -0
- CHAP/edd/utils.py +846 -292
- CHAP/foxden/__init__.py +6 -0
- CHAP/foxden/processor.py +42 -0
- CHAP/foxden/writer.py +65 -0
- CHAP/giwaxs/__init__.py +8 -0
- CHAP/giwaxs/models.py +100 -0
- CHAP/giwaxs/processor.py +520 -0
- CHAP/giwaxs/reader.py +5 -0
- CHAP/giwaxs/writer.py +5 -0
- CHAP/pipeline.py +48 -10
- CHAP/runner.py +161 -72
- CHAP/tomo/models.py +31 -29
- CHAP/tomo/processor.py +169 -118
- CHAP/utils/__init__.py +1 -0
- CHAP/utils/fit.py +1292 -1315
- CHAP/utils/general.py +411 -53
- CHAP/utils/models.py +594 -0
- CHAP/utils/parfile.py +10 -2
- ChessAnalysisPipeline-0.0.16.dist-info/LICENSE +60 -0
- {ChessAnalysisPipeline-0.0.14.dist-info → ChessAnalysisPipeline-0.0.16.dist-info}/METADATA +1 -1
- ChessAnalysisPipeline-0.0.16.dist-info/RECORD +62 -0
- {ChessAnalysisPipeline-0.0.14.dist-info → ChessAnalysisPipeline-0.0.16.dist-info}/WHEEL +1 -1
- CHAP/utils/scanparsers.py +0 -1431
- ChessAnalysisPipeline-0.0.14.dist-info/LICENSE +0 -21
- ChessAnalysisPipeline-0.0.14.dist-info/RECORD +0 -54
- {ChessAnalysisPipeline-0.0.14.dist-info → ChessAnalysisPipeline-0.0.16.dist-info}/entry_points.txt +0 -0
- {ChessAnalysisPipeline-0.0.14.dist-info → ChessAnalysisPipeline-0.0.16.dist-info}/top_level.txt +0 -0
CHAP/utils/models.py
ADDED
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
"""Utils Pydantic model classes."""
|
|
2
|
+
|
|
3
|
+
# Third party imports
|
|
4
|
+
import numpy as np
|
|
5
|
+
from pydantic import (
|
|
6
|
+
BaseModel,
|
|
7
|
+
Field,
|
|
8
|
+
PrivateAttr,
|
|
9
|
+
StrictBool,
|
|
10
|
+
conint,
|
|
11
|
+
conlist,
|
|
12
|
+
confloat,
|
|
13
|
+
constr,
|
|
14
|
+
field_validator,
|
|
15
|
+
)
|
|
16
|
+
from typing import (
|
|
17
|
+
Literal,
|
|
18
|
+
Optional,
|
|
19
|
+
Union,
|
|
20
|
+
)
|
|
21
|
+
from typing_extensions import Annotated
|
|
22
|
+
|
|
23
|
+
# Local modules
|
|
24
|
+
from CHAP.utils.general import not_zero, tiny
|
|
25
|
+
|
|
26
|
+
tiny = np.finfo(np.float64).resolution
|
|
27
|
+
s2pi = np.sqrt(2*np.pi)
|
|
28
|
+
|
|
29
|
+
#def constant(x, c=0.5):
|
|
30
|
+
def constant(x, c=0.0):
|
|
31
|
+
"""Return a linear function.
|
|
32
|
+
|
|
33
|
+
constant(x, c) = c
|
|
34
|
+
|
|
35
|
+
"""
|
|
36
|
+
return c*np.ones((x.size))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
#def linear(x, slope=0.9, intercept=0.1):
|
|
40
|
+
def linear(x, slope=1.0, intercept=0.0):
|
|
41
|
+
"""Return a linear function.
|
|
42
|
+
|
|
43
|
+
linear(x, slope, intercept) = slope * x + intercept
|
|
44
|
+
|
|
45
|
+
"""
|
|
46
|
+
return slope * x + intercept
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
#def quadratic(x, a=0.5, b=0.4, c=0.1):
|
|
50
|
+
def quadratic(x, a=0.0, b=0.0, c=0.0):
|
|
51
|
+
"""Return a parabolic function.
|
|
52
|
+
|
|
53
|
+
parabolic(x, a, b, c) = a * x**2 + b * x + c
|
|
54
|
+
|
|
55
|
+
"""
|
|
56
|
+
return (a*x + b) * x + c
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
#def exponential(x, amplitude=1.0, decay=0.3):
|
|
60
|
+
def exponential(x, amplitude=1.0, decay=1.0):
|
|
61
|
+
"""Return an exponential function.
|
|
62
|
+
|
|
63
|
+
exponential(x, amplitude, decay) = amplitude * exp(-x/decay)
|
|
64
|
+
|
|
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
|
+
"""
|
|
77
|
+
return ((amplitude/(max(tiny, s2pi*sigma)))
|
|
78
|
+
* np.exp(-(x-center)**2 / max(tiny, (2*sigma**2))))
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
#def lorentzian(x, amplitude=0.3, center=0.5, sigma=0.1):
|
|
82
|
+
def lorentzian(x, amplitude=1.0, center=0.0, sigma=1.0):
|
|
83
|
+
"""Return a 1-dimensional Lorentzian function.
|
|
84
|
+
|
|
85
|
+
lorentzian(x, amplitude, center, sigma) =
|
|
86
|
+
(amplitude/(1 + ((1.0*x-center)/sigma)**2)) / (pi*sigma)
|
|
87
|
+
|
|
88
|
+
"""
|
|
89
|
+
return ((amplitude/(1 + ((x-center)/max(tiny, sigma))**2))
|
|
90
|
+
/ max(tiny, (pi*sigma)))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def rectangle(
|
|
94
|
+
x, amplitude=1.0, center1=0.0, sigma1=1.0, center2=1.0,
|
|
95
|
+
sigma2=1.0, form='linear'):
|
|
96
|
+
"""Return a rectangle function.
|
|
97
|
+
|
|
98
|
+
Starts at 0.0, rises to `amplitude` (at `center1` with width `sigma1`),
|
|
99
|
+
then drops to 0.0 (at `center2` with width `sigma2`) with `form`:
|
|
100
|
+
- `'linear'` (default) = ramp_up + ramp_down
|
|
101
|
+
- `'atan'`, `'arctan`' = amplitude*(atan(arg1) + atan(arg2))/pi
|
|
102
|
+
- `'erf'` = amplitude*(erf(arg1) + erf(arg2))/2.
|
|
103
|
+
- `'logisitic'` = amplitude*[1 - 1/(1 + exp(arg1)) - 1/(1+exp(arg2))]
|
|
104
|
+
|
|
105
|
+
where ``arg1 = (x - center1)/sigma1`` and
|
|
106
|
+
``arg2 = -(x - center2)/sigma2``.
|
|
107
|
+
|
|
108
|
+
"""
|
|
109
|
+
arg1 = (x - center1)/max(tiny, sigma1)
|
|
110
|
+
arg2 = (center2 - x)/max(tiny, sigma2)
|
|
111
|
+
|
|
112
|
+
if form == 'erf':
|
|
113
|
+
# Third party modules
|
|
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))/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
|
+
from copy import deepcopy
|
|
143
|
+
|
|
144
|
+
if 'model' in info.data:
|
|
145
|
+
model = info.data['model']
|
|
146
|
+
else:
|
|
147
|
+
model = None
|
|
148
|
+
if model is None or model == 'expression':
|
|
149
|
+
return parameters
|
|
150
|
+
sig = {
|
|
151
|
+
name:par
|
|
152
|
+
for name, par in inspect.signature(models[model]).parameters.items()}
|
|
153
|
+
sig.pop('x')
|
|
154
|
+
|
|
155
|
+
# Check input model parameter validity
|
|
156
|
+
for par in parameters:
|
|
157
|
+
if par.name not in sig:
|
|
158
|
+
raise ValueError('Invalid parameter {par.name} in {model} model')
|
|
159
|
+
|
|
160
|
+
# Set model parameters
|
|
161
|
+
output_parameters = []
|
|
162
|
+
for sig_name, sig_par in sig.items():
|
|
163
|
+
if model == 'rectangle' and sig_name == 'form':
|
|
164
|
+
continue
|
|
165
|
+
for par in parameters:
|
|
166
|
+
if sig_name == par.name:
|
|
167
|
+
break
|
|
168
|
+
else:
|
|
169
|
+
par = FitParameter(name=sig_name)
|
|
170
|
+
if sig_par.default != sig_par.empty:
|
|
171
|
+
par._default = sig_par.default
|
|
172
|
+
output_parameters.append(par)
|
|
173
|
+
|
|
174
|
+
return output_parameters
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class FitParameter(BaseModel):
|
|
178
|
+
"""
|
|
179
|
+
Class representing a specific fit parameter for the fit processor.
|
|
180
|
+
|
|
181
|
+
"""
|
|
182
|
+
name: constr(strip_whitespace=True, min_length=1)
|
|
183
|
+
value: Optional[confloat(allow_inf_nan=False)] = None
|
|
184
|
+
min: Optional[confloat()] = -np.inf
|
|
185
|
+
max: Optional[confloat()] = np.inf
|
|
186
|
+
vary: StrictBool = True
|
|
187
|
+
expr: Optional[constr(strip_whitespace=True, min_length=1)] = None
|
|
188
|
+
_default: float = PrivateAttr()
|
|
189
|
+
_init_value: float = PrivateAttr()
|
|
190
|
+
_prefix: str = PrivateAttr()
|
|
191
|
+
_stderr: float = PrivateAttr()
|
|
192
|
+
|
|
193
|
+
@field_validator('min')
|
|
194
|
+
@classmethod
|
|
195
|
+
def validate_min(cls, value):
|
|
196
|
+
"""Validate the specified min.
|
|
197
|
+
|
|
198
|
+
:param value: Field value to validate (`min`).
|
|
199
|
+
:type value: Union[float, None]
|
|
200
|
+
:return: Lower bound of fit parameter.
|
|
201
|
+
:rtype: float
|
|
202
|
+
"""
|
|
203
|
+
if value is None:
|
|
204
|
+
return -np.inf
|
|
205
|
+
return value
|
|
206
|
+
|
|
207
|
+
@field_validator('max')
|
|
208
|
+
@classmethod
|
|
209
|
+
def validate_max(cls, value):
|
|
210
|
+
"""Validate the specified max.
|
|
211
|
+
|
|
212
|
+
:param value: Field value to validate (`max`).
|
|
213
|
+
:type value: Union[float, None]
|
|
214
|
+
:return: Upper bound of fit parameter.
|
|
215
|
+
:rtype: float
|
|
216
|
+
"""
|
|
217
|
+
if value is None:
|
|
218
|
+
return np.inf
|
|
219
|
+
return value
|
|
220
|
+
|
|
221
|
+
@property
|
|
222
|
+
def default(self):
|
|
223
|
+
"""Return the _default attribute."""
|
|
224
|
+
if hasattr(self, '_default'):
|
|
225
|
+
return self._default
|
|
226
|
+
else:
|
|
227
|
+
return None
|
|
228
|
+
|
|
229
|
+
@property
|
|
230
|
+
def init_value(self):
|
|
231
|
+
"""Return the _init_value attribute."""
|
|
232
|
+
if hasattr(self, '_init_value'):
|
|
233
|
+
return self._init_value
|
|
234
|
+
else:
|
|
235
|
+
return None
|
|
236
|
+
|
|
237
|
+
@property
|
|
238
|
+
def prefix(self):
|
|
239
|
+
"""Return the _prefix attribute."""
|
|
240
|
+
if hasattr(self, '_prefix'):
|
|
241
|
+
return self._prefix
|
|
242
|
+
else:
|
|
243
|
+
return None
|
|
244
|
+
|
|
245
|
+
@property
|
|
246
|
+
def stderr(self):
|
|
247
|
+
"""Return the _stderr attribute."""
|
|
248
|
+
if hasattr(self, '_stderr'):
|
|
249
|
+
return self._stderr
|
|
250
|
+
else:
|
|
251
|
+
return None
|
|
252
|
+
|
|
253
|
+
def set(self, value=None, min=None, max=None, vary=None, expr=None):
|
|
254
|
+
"""
|
|
255
|
+
Set or update FitParameter attributes.
|
|
256
|
+
|
|
257
|
+
:param value: Parameter value.
|
|
258
|
+
:type value: float, optional
|
|
259
|
+
:param min: Lower Parameter value bound. To remove the lower
|
|
260
|
+
bound you must set min to `numpy.inf`.
|
|
261
|
+
:type min: bool, optional
|
|
262
|
+
:param max: Upper Parameter value bound. To remove the lower
|
|
263
|
+
bound you must set max to `numpy.inf`.
|
|
264
|
+
:type max: bool, optional
|
|
265
|
+
:param vary: Whether the Parameter is varied during a fit.
|
|
266
|
+
:type vary: bool, optional
|
|
267
|
+
:param expr: Mathematical expression used to constrain the
|
|
268
|
+
value during the fit. To remove a constraint you must
|
|
269
|
+
supply an empty string.
|
|
270
|
+
:type expr: str, optional
|
|
271
|
+
"""
|
|
272
|
+
if expr is not None:
|
|
273
|
+
if not isinstance(expr, str):
|
|
274
|
+
raise ValueError(f'Invalid parameter expr ({expr})')
|
|
275
|
+
if expr == '':
|
|
276
|
+
expr = None
|
|
277
|
+
self.expr = expr
|
|
278
|
+
if expr is not None:
|
|
279
|
+
self.value = None
|
|
280
|
+
self.min = -np.inf
|
|
281
|
+
self.max = np.inf
|
|
282
|
+
self.vary = False
|
|
283
|
+
return
|
|
284
|
+
if min is not None:
|
|
285
|
+
if not isinstance(min, (int, float)):
|
|
286
|
+
raise ValueError(f'Invalid parameter min ({min})')
|
|
287
|
+
self.min = min
|
|
288
|
+
if max is not None:
|
|
289
|
+
if not isinstance(max, (int, float)):
|
|
290
|
+
raise ValueError(f'Invalid parameter max ({max})')
|
|
291
|
+
self.max = max
|
|
292
|
+
if vary is not None:
|
|
293
|
+
if not isinstance(vary, bool):
|
|
294
|
+
raise ValueError(f'Invalid parameter vary ({vary})')
|
|
295
|
+
self.vary = vary
|
|
296
|
+
if value is not None:
|
|
297
|
+
if not isinstance(value, (int, float)):
|
|
298
|
+
raise ValueError(f'Invalid parameter value ({value})')
|
|
299
|
+
self.value = value
|
|
300
|
+
if self.value > self.max:
|
|
301
|
+
self.value = self.max
|
|
302
|
+
elif self.value < self.min:
|
|
303
|
+
self.value = self.min
|
|
304
|
+
self.expr = None
|
|
305
|
+
|
|
306
|
+
class Constant(BaseModel):
|
|
307
|
+
"""
|
|
308
|
+
Class representing a Constant model component.
|
|
309
|
+
|
|
310
|
+
:ivar model: The model component base name (a prefix will be added
|
|
311
|
+
if multiple identical model components are added).
|
|
312
|
+
:type model: Literal['constant']
|
|
313
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
314
|
+
generated from the function signature (excluding the
|
|
315
|
+
independent variable), defaults to `[]`.
|
|
316
|
+
:type parameters: list[FitParameter], optional
|
|
317
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
318
|
+
:type prefix: str, optional
|
|
319
|
+
"""
|
|
320
|
+
model: Literal['constant']
|
|
321
|
+
parameters: Annotated[
|
|
322
|
+
conlist(item_type=FitParameter),
|
|
323
|
+
Field(validate_default=True)] = []
|
|
324
|
+
prefix: Optional[str] = ''
|
|
325
|
+
|
|
326
|
+
_validate_parameters_parameters = field_validator(
|
|
327
|
+
'parameters')(validate_parameters)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
class Linear(BaseModel):
|
|
331
|
+
"""
|
|
332
|
+
Class representing a Linear model component.
|
|
333
|
+
|
|
334
|
+
:ivar model: The model component base name (a prefix will be added
|
|
335
|
+
if multiple identical model components are added).
|
|
336
|
+
:type model: Literal['linear']
|
|
337
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
338
|
+
generated from the function signature (excluding the
|
|
339
|
+
independent variable), defaults to `[]`.
|
|
340
|
+
:type parameters: list[FitParameter], optional
|
|
341
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
342
|
+
:type prefix: str, optional
|
|
343
|
+
"""
|
|
344
|
+
model: Literal['linear']
|
|
345
|
+
parameters: Annotated[
|
|
346
|
+
conlist(item_type=FitParameter),
|
|
347
|
+
Field(validate_default=True)] = []
|
|
348
|
+
prefix: Optional[str] = ''
|
|
349
|
+
|
|
350
|
+
_validate_parameters_parameters = field_validator(
|
|
351
|
+
'parameters')(validate_parameters)
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
class Quadratic(BaseModel):
|
|
355
|
+
"""
|
|
356
|
+
Class representing a Quadratic model component.
|
|
357
|
+
|
|
358
|
+
:ivar model: The model component base name (a prefix will be added
|
|
359
|
+
if multiple identical model components are added).
|
|
360
|
+
:type model: Literal['quadratic']
|
|
361
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
362
|
+
generated from the function signature (excluding the
|
|
363
|
+
independent variable), defaults to `[]`.
|
|
364
|
+
:type parameters: list[FitParameter], optional
|
|
365
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
366
|
+
:type prefix: str, optional
|
|
367
|
+
"""
|
|
368
|
+
model: Literal['quadratic']
|
|
369
|
+
parameters: Annotated[
|
|
370
|
+
conlist(item_type=FitParameter),
|
|
371
|
+
Field(validate_default=True)] = []
|
|
372
|
+
prefix: Optional[str] = ''
|
|
373
|
+
|
|
374
|
+
_validate_parameters_parameters = field_validator(
|
|
375
|
+
'parameters')(validate_parameters)
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
class Exponential(BaseModel):
|
|
379
|
+
"""
|
|
380
|
+
Class representing an Exponential model component.
|
|
381
|
+
|
|
382
|
+
:ivar model: The model component base name (a prefix will be added
|
|
383
|
+
if multiple identical model components are added).
|
|
384
|
+
:type model: Literal['exponential']
|
|
385
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
386
|
+
generated from the function signature (excluding the
|
|
387
|
+
independent variable), defaults to `[]`.
|
|
388
|
+
:type parameters: list[FitParameter], optional
|
|
389
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
390
|
+
:type prefix: str, optional
|
|
391
|
+
"""
|
|
392
|
+
model: Literal['exponential']
|
|
393
|
+
parameters: Annotated[
|
|
394
|
+
conlist(item_type=FitParameter),
|
|
395
|
+
Field(validate_default=True)] = []
|
|
396
|
+
prefix: Optional[str] = ''
|
|
397
|
+
|
|
398
|
+
_validate_parameters_parameters = field_validator(
|
|
399
|
+
'parameters')(validate_parameters)
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
class Gaussian(BaseModel):
|
|
403
|
+
"""
|
|
404
|
+
Class representing a Gaussian model component.
|
|
405
|
+
|
|
406
|
+
:ivar model: The model component base name (a prefix will be added
|
|
407
|
+
if multiple identical model components are added).
|
|
408
|
+
:type model: Literal['gaussian']
|
|
409
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
410
|
+
generated from the function signature (excluding the
|
|
411
|
+
independent variable), defaults to `[]`.
|
|
412
|
+
:type parameters: list[FitParameter], optional
|
|
413
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
414
|
+
:type prefix: str, optional
|
|
415
|
+
"""
|
|
416
|
+
model: Literal['gaussian']
|
|
417
|
+
parameters: Annotated[
|
|
418
|
+
conlist(item_type=FitParameter),
|
|
419
|
+
Field(validate_default=True)] = []
|
|
420
|
+
prefix: Optional[str] = ''
|
|
421
|
+
|
|
422
|
+
_validate_parameters_parameters = field_validator(
|
|
423
|
+
'parameters')(validate_parameters)
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
class Lorentzian(BaseModel):
|
|
427
|
+
"""
|
|
428
|
+
Class representing a Lorentzian model component.
|
|
429
|
+
|
|
430
|
+
:ivar model: The model component base name (a prefix will be added
|
|
431
|
+
if multiple identical model components are added).
|
|
432
|
+
:type model: Literal['lorentzian']
|
|
433
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
434
|
+
generated from the function signature (excluding the
|
|
435
|
+
independent variable), defaults to `[]`.
|
|
436
|
+
:type parameters: list[FitParameter], optional
|
|
437
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
438
|
+
:type prefix: str, optional
|
|
439
|
+
"""
|
|
440
|
+
model: Literal['lorentzian']
|
|
441
|
+
parameters: Annotated[
|
|
442
|
+
conlist(item_type=FitParameter),
|
|
443
|
+
Field(validate_default=True)] = []
|
|
444
|
+
prefix: Optional[str] = ''
|
|
445
|
+
|
|
446
|
+
_validate_parameters_parameters = field_validator(
|
|
447
|
+
'parameters')(validate_parameters)
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
class Rectangle(BaseModel):
|
|
451
|
+
"""
|
|
452
|
+
Class representing a Rectangle model component.
|
|
453
|
+
|
|
454
|
+
:ivar model: The model component base name (a prefix will be added
|
|
455
|
+
if multiple identical model components are added).
|
|
456
|
+
:type model: Literal['rectangle']
|
|
457
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
458
|
+
generated from the function signature (excluding the
|
|
459
|
+
independent variable), defaults to `[]`.
|
|
460
|
+
:type parameters: list[FitParameter], optional
|
|
461
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
462
|
+
:type prefix: str, optional
|
|
463
|
+
"""
|
|
464
|
+
model: Literal['rectangle']
|
|
465
|
+
parameters: Annotated[
|
|
466
|
+
conlist(item_type=FitParameter),
|
|
467
|
+
Field(validate_default=True)] = []
|
|
468
|
+
prefix: Optional[str] = ''
|
|
469
|
+
|
|
470
|
+
_validate_parameters_parameters = field_validator(
|
|
471
|
+
'parameters')(validate_parameters)
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
class Expression(BaseModel):
|
|
475
|
+
"""
|
|
476
|
+
Class representing an Expression model component.
|
|
477
|
+
|
|
478
|
+
:ivar model: The model component base name (a prefix will be added
|
|
479
|
+
if multiple identical model components are added).
|
|
480
|
+
:type model: Literal['expression']
|
|
481
|
+
:ivar expr: Mathematical expression to represent the model
|
|
482
|
+
component.
|
|
483
|
+
:type expr: str
|
|
484
|
+
:ivar parameters: Function parameters, defaults to those auto
|
|
485
|
+
generated from the model expression (excluding the
|
|
486
|
+
independent variable), defaults to `[]`.
|
|
487
|
+
:type parameters: list[FitParameter], optional
|
|
488
|
+
:ivar prefix: The model prefix, defaults to `''`.
|
|
489
|
+
:type prefix: str, optional
|
|
490
|
+
"""
|
|
491
|
+
model: Literal['expression']
|
|
492
|
+
expr: constr(strip_whitespace=True, min_length=1)
|
|
493
|
+
parameters: Annotated[
|
|
494
|
+
conlist(item_type=FitParameter),
|
|
495
|
+
Field(validate_default=True)] = []
|
|
496
|
+
prefix: Optional[str] = ''
|
|
497
|
+
|
|
498
|
+
_validate_parameters_parameters = field_validator(
|
|
499
|
+
'parameters')(validate_parameters)
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
class Multipeak(BaseModel):
|
|
503
|
+
model: Literal['multipeak']
|
|
504
|
+
centers: conlist(item_type=confloat(allow_inf_nan=False), min_length=1)
|
|
505
|
+
fit_type: Optional[Literal['uniform', 'unconstrained']] = 'unconstrained'
|
|
506
|
+
centers_range: Optional[confloat(allow_inf_nan=False)] = None
|
|
507
|
+
fwhm_min: Optional[confloat(allow_inf_nan=False)] = None
|
|
508
|
+
fwhm_max: Optional[confloat(allow_inf_nan=False)] = None
|
|
509
|
+
peak_models: Literal['gaussian', 'lorentzian'] = 'gaussian'
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
models = {
|
|
513
|
+
'constant': constant,
|
|
514
|
+
'linear': linear,
|
|
515
|
+
'quadratic': quadratic,
|
|
516
|
+
'exponential': exponential,
|
|
517
|
+
'gaussian': gaussian,
|
|
518
|
+
'lorentzian': lorentzian,
|
|
519
|
+
'rectangle': rectangle,
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
model_classes = (
|
|
523
|
+
Constant,
|
|
524
|
+
Linear,
|
|
525
|
+
Quadratic,
|
|
526
|
+
Exponential,
|
|
527
|
+
Gaussian,
|
|
528
|
+
Lorentzian,
|
|
529
|
+
Rectangle,
|
|
530
|
+
)
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
class FitConfig(BaseModel):
|
|
534
|
+
"""
|
|
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 is `'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), defaults to `None`.
|
|
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
|
+
|
|
575
|
+
@field_validator('method')
|
|
576
|
+
@classmethod
|
|
577
|
+
def validate_method(cls, method, info):
|
|
578
|
+
"""Validate the specified method.
|
|
579
|
+
|
|
580
|
+
:param method: The value of `method` to validate.
|
|
581
|
+
:type method: str
|
|
582
|
+
:param info: Pydantic validator info object.
|
|
583
|
+
:type info: pydantic_core._pydantic_core.ValidationInfo
|
|
584
|
+
:return: Fit method.
|
|
585
|
+
:rtype: str
|
|
586
|
+
"""
|
|
587
|
+
code = info.data['code']
|
|
588
|
+
if code == 'lmfit':
|
|
589
|
+
if method not in ('leastsq', 'least_squares'):
|
|
590
|
+
method = 'leastsq'
|
|
591
|
+
elif method == 'least_squares':
|
|
592
|
+
method = 'leastsq'
|
|
593
|
+
|
|
594
|
+
return method
|
CHAP/utils/parfile.py
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
as input
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
|
+
# System modules
|
|
5
6
|
import csv
|
|
6
|
-
import json
|
|
7
7
|
import os
|
|
8
8
|
|
|
9
|
+
# Third party modules
|
|
10
|
+
import json
|
|
11
|
+
|
|
9
12
|
class ParFile():
|
|
10
13
|
"""Representation of a .par file
|
|
11
14
|
|
|
@@ -78,9 +81,12 @@ class ParFile():
|
|
|
78
81
|
:return: a map configuration
|
|
79
82
|
:rtype: CHAP.common.models.map.MapConfig
|
|
80
83
|
"""
|
|
84
|
+
# Third party modules
|
|
81
85
|
import numpy as np
|
|
86
|
+
|
|
87
|
+
# Local modules
|
|
82
88
|
from CHAP.common.models.map import MapConfig
|
|
83
|
-
from
|
|
89
|
+
from chess_scanparsers import SMBScanParser
|
|
84
90
|
|
|
85
91
|
scanparser = SMBScanParser(self.spec_file, 1)
|
|
86
92
|
good_scans = self.good_scan_numbers()
|
|
@@ -156,7 +162,9 @@ class ParFile():
|
|
|
156
162
|
:return: reshaped array of values
|
|
157
163
|
:rtype: np.ndarray
|
|
158
164
|
"""
|
|
165
|
+
# Third party modules
|
|
159
166
|
import numpy as np
|
|
167
|
+
|
|
160
168
|
good_scans = self.good_scan_numbers()
|
|
161
169
|
if len(values) != len(good_scans):
|
|
162
170
|
raise ValueError('number of values provided ({len(values)}) does '
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Valentin Kuznetsov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
Some code in CHAP.utils has been based or taken from the lmfit library whose
|
|
25
|
+
licence is below.
|
|
26
|
+
|
|
27
|
+
Copyright 2022 Matthew Newville, The University of Chicago
|
|
28
|
+
Renee Otten, Brandeis University
|
|
29
|
+
Till Stensitzki, Freie Universitat Berlin
|
|
30
|
+
A. R. J. Nelson, Australian Nuclear Science and Technology Organisation
|
|
31
|
+
Antonino Ingargiola, University of California, Los Angeles
|
|
32
|
+
Daniel B. Allen, Johns Hopkins University
|
|
33
|
+
Michal Rawlik, Eidgenossische Technische Hochschule, Zurich
|
|
34
|
+
|
|
35
|
+
Redistribution and use in source and binary forms, with or without
|
|
36
|
+
modification, are permitted provided that the following conditions are met:
|
|
37
|
+
|
|
38
|
+
1. Redistributions of source code must retain the above copyright notice,
|
|
39
|
+
this list of conditions and the following disclaimer.
|
|
40
|
+
|
|
41
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
42
|
+
notice, this list of conditions and the following disclaimer in the
|
|
43
|
+
documentation and/or other materials provided with the distribution.
|
|
44
|
+
|
|
45
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
46
|
+
contributors may be used to endorse or promote products derived from this
|
|
47
|
+
software without specific prior written permission.
|
|
48
|
+
|
|
49
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
50
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
51
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
52
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
53
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
54
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
55
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
56
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
57
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
58
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
59
|
+
POSSIBILITY OF SUCH DAMAGE.
|
|
60
|
+
|