-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalytical_convolution.py
More file actions
496 lines (400 loc) · 16.3 KB
/
Copy pathanalytical_convolution.py
File metadata and controls
496 lines (400 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# SPDX-FileCopyrightText: 2026 EasyScience contributors <https://github.com/easyscience>
# SPDX-License-Identifier: BSD-3-Clause
from typing import ClassVar
import numpy as np
from scipy.special import voigt_profile
from easydynamics.convolution.convolution_base import ConvolutionBase
from easydynamics.sample_model import DeltaFunction
from easydynamics.sample_model import Gaussian
from easydynamics.sample_model import Lorentzian
from easydynamics.sample_model import Voigt
from easydynamics.sample_model.component_collection import ComponentCollection
from easydynamics.sample_model.components.model_component import ModelComponent
class AnalyticalConvolution(ConvolutionBase):
"""
Analytical convolution of a ModelComponent or ComponentCollection with a ResolutionModel.
Possible analytical convolutions are any combination of delta functions, Gaussians, Lorentzians
and Voigt profiles.
"""
# Mapping of supported component type pairs to convolution methods.
# Delta functions are handled separately.
_CONVOLUTIONS: ClassVar[dict[str, object]] = {
('Gaussian', 'Gaussian'): '_convolute_gaussian_gaussian',
('Gaussian', 'Lorentzian'): '_convolute_gaussian_lorentzian',
('Gaussian', 'Voigt'): '_convolute_gaussian_voigt',
('Lorentzian', 'Lorentzian'): '_convolute_lorentzian_lorentzian',
('Lorentzian', 'Voigt'): '_convolute_lorentzian_voigt',
('Voigt', 'Voigt'): '_convolute_voigt_voigt',
}
def convolution(
self,
) -> np.ndarray:
"""
Convolve sample with resolution analytically if possible.
Accepts ComponentCollection or single ModelComponent for each. Possible analytical
convolutions are any combination of delta functions, Gaussians, Lorentzians and Voigt
profiles.
Returns
-------
np.ndarray
The convolution of the sample_components and resolution_components values evaluated at
self.energy.
"""
total = np.zeros_like(self.energy.values, dtype=float)
for sample_component in self.sample_components:
# Go through resolution components,
# adding analytical contributions
for resolution_component in self.resolution_components:
contrib = self._convolute_analytic_pair(
sample_component=sample_component,
resolution_component=resolution_component,
)
total += contrib
return total
def _convolute_analytic_pair(
self,
sample_component: ModelComponent,
resolution_component: ModelComponent,
) -> np.ndarray:
r"""
Analytic convolution for component pair (sample_component, resolution_component).
The convolution of two Gaussian components results in another Gaussian component with width
$\sqrt{w_1^2 + w_2^2}$.
The convolution of two Lorentzian components results in another Lorentzian component with
width $w_1 + w_2$.
The convolution of a Gaussian and a Lorentzian results in a Voigt profile.
The convolution of a Gaussian and a Voigt profile results in another Voigt profile, with
the Lorentzian width unchanged and the Gaussian widths summed in quadrature.
The convolution of a Lorentzian and a Voigt profile results in another Voigt profile, with
the Gaussian width unchanged and the Lorentzian widths summed.
The convolution of two Voigt profiles results in another Voigt profile, with the Gaussian
widths summed in quadrature and the Lorentzian widths summed.
The convolution of a delta function with any component or ComponentCollection results in
the same component or ComponentCollection shifted by the delta center.
All areas are multiplied in the convolution.
Parameters
----------
sample_component : ModelComponent
The sample component to be convolved.
resolution_component : ModelComponent
The resolution component to convolve with.
Raises
------
ValueError
If the component pair cannot be handled analytically.
Returns
-------
np.ndarray
The convolution result.
"""
if isinstance(resolution_component, DeltaFunction):
raise ValueError(
'Analytical convolution with a delta function \
in the resolution model is not supported.'
)
# Delta function + anything -->
# anything, shifted by delta center with area A1 * A2
if isinstance(sample_component, DeltaFunction):
return self._convolute_delta_any(
sample_component,
resolution_component,
)
pair = (type(sample_component).__name__, type(resolution_component).__name__)
swapped = False
if pair not in self._CONVOLUTIONS:
# Try reversing the pair
pair = (
type(resolution_component).__name__,
type(sample_component).__name__,
)
swapped = True
func_name = self._CONVOLUTIONS.get(pair)
if func_name is None:
raise ValueError(
f'Analytical convolution not supported for component pair: '
f'{type(sample_component).__name__}, {type(resolution_component).__name__}'
)
# Call the corresponding method
if swapped:
return getattr(self, func_name)(resolution_component, sample_component)
return getattr(self, func_name)(sample_component, resolution_component)
def _convolute_delta_any(
self,
sample_component: DeltaFunction,
resolution_components: ComponentCollection | ModelComponent,
) -> np.ndarray:
"""
Convolution of delta function with any ModelComponent or ComponentCollection results in the
same component or ComponentCollection shifted by the delta center. The areas are
multiplied.
Parameters
----------
sample_component : DeltaFunction
The sample component to be convolved.
resolution_components : ComponentCollection | ModelComponent
The resolution model to convolve with.
Returns
-------
np.ndarray
The evaluated convolution values at self.energy.
"""
return sample_component.area.value * resolution_components.evaluate(
self.energy_with_offset.values - sample_component.center.value
)
def _convolute_gaussian_gaussian(
self,
sample_component: Gaussian,
resolution_component: Gaussian,
) -> np.ndarray:
r"""
Convolution of two Gaussian components results in another Gaussian component with width
$\sqrt{w_1^2 + w_2^2}$. The areas are multiplied.
Parameters
----------
sample_component : Gaussian
The sample Gaussian component to be convolved.
resolution_component : Gaussian
The resolution Gaussian component to convolve with.
Returns
-------
np.ndarray
The evaluated convolution values at self.energy.
"""
width = np.sqrt(sample_component.width.value**2 + resolution_component.width.value**2)
area = sample_component.area.value * resolution_component.area.value
center = sample_component.center.value + resolution_component.center.value
return self._gaussian_eval(area=area, center=center, width=width)
def _convolute_gaussian_lorentzian(
self,
sample_component: Gaussian,
resolution_component: Lorentzian,
) -> np.ndarray:
"""
Convolution of a Gaussian and a Lorentzian results in a Voigt profile. The areas are
multiplied.
Parameters
----------
sample_component : Gaussian
The sample Gaussian component to be convolved.
resolution_component : Lorentzian
The resolution Lorentzian component to convolve with.
Returns
-------
np.ndarray
The evaluated convolution values at self.energy.
"""
center = sample_component.center.value + resolution_component.center.value
area = sample_component.area.value * resolution_component.area.value
return self._voigt_eval(
area=area,
center=center,
gaussian_width=sample_component.width.value,
lorentzian_width=resolution_component.width.value,
)
def _convolute_gaussian_voigt(
self,
sample_component: Gaussian,
resolution_component: Voigt,
) -> np.ndarray:
"""
Convolution of a Gaussian and a Voigt profile results in another Voigt profile. The
Lorentzian width remains unchanged, while the Gaussian widths are summed in quadrature. The
areas are multiplied.
Parameters
----------
sample_component : Gaussian
The sample Gaussian component to be convolved.
resolution_component : Voigt
The resolution Voigt component to convolve with.
Returns
-------
np.ndarray
The evaluated convolution values at self.energy.
"""
area = sample_component.area.value * resolution_component.area.value
center = sample_component.center.value + resolution_component.center.value
gaussian_width = np.sqrt(
sample_component.width.value**2 + resolution_component.gaussian_width.value**2
)
lorentzian_width = resolution_component.lorentzian_width.value
return self._voigt_eval(
area=area,
center=center,
gaussian_width=gaussian_width,
lorentzian_width=lorentzian_width,
)
def _convolute_lorentzian_lorentzian(
self,
sample_component: Lorentzian,
resolution_component: Lorentzian,
) -> np.ndarray:
r"""
Convolution of two Lorentzian components results in another Lorentzian component with width
$w_1 + w_2$. The areas are multiplied.
Parameters
----------
sample_component : Lorentzian
The sample Lorentzian component to be convolved.
resolution_component : Lorentzian
The resolution Lorentzian component to convolve with.
Returns
-------
np.ndarray
The evaluated convolution values at self.energy.
"""
area = sample_component.area.value * resolution_component.area.value
center = sample_component.center.value + resolution_component.center.value
width = sample_component.width.value + resolution_component.width.value
return self._lorentzian_eval(area=area, center=center, width=width)
def _convolute_lorentzian_voigt(
self,
sample_component: Lorentzian,
resolution_component: Voigt,
) -> np.ndarray:
"""
Convolution of a Lorentzian and a Voigt profile results in another Voigt profile.
The Gaussian width remains unchanged, while the Lorentzian widths are summed.
The areas are multiplied.
Parameters
----------
sample_component : Lorentzian
The sample Lorentzian component to be convolved.
resolution_component : Voigt
The resolution Voigt component to convolve with.
Returns
-------
np.ndarray
The evaluated convolution values at self.energy.
"""
area = sample_component.area.value * resolution_component.area.value
center = sample_component.center.value + resolution_component.center.value
gaussian_width = resolution_component.gaussian_width.value
lorentzian_width = (
sample_component.width.value + resolution_component.lorentzian_width.value
)
return self._voigt_eval(
area=area,
center=center,
gaussian_width=gaussian_width,
lorentzian_width=lorentzian_width,
)
def _convolute_voigt_voigt(
self,
sample_component: Voigt,
resolution_component: Voigt,
) -> np.ndarray:
"""
Convolution of two Voigt profiles results in another Voigt profile.
The Gaussian widths are summed in quadrature, while the Lorentzian widths are summed. The
areas are multiplied.
Parameters
----------
sample_component : Voigt
The sample Voigt component to be convolved.
resolution_component : Voigt
The resolution Voigt component to convolve with.
Returns
-------
np.ndarray
The evaluated convolution values at self.energy.
"""
area = sample_component.area.value * resolution_component.area.value
center = sample_component.center.value + resolution_component.center.value
gaussian_width = np.sqrt(
sample_component.gaussian_width.value**2 + resolution_component.gaussian_width.value**2
)
lorentzian_width = (
sample_component.lorentzian_width.value + resolution_component.lorentzian_width.value
)
return self._voigt_eval(
area=area,
center=center,
gaussian_width=gaussian_width,
lorentzian_width=lorentzian_width,
)
def _gaussian_eval(
self,
area: float,
center: float,
width: float,
) -> np.ndarray:
r"""
Evaluate a Gaussian function.
$$ I(x) = \frac{A}{\sigma \sqrt{2\pi}} \exp\left( -\frac{1}{2} \left(\frac{x -
x_0}{\sigma}\right)^2 \right) $$
where $A$ is the area, $x_0$ is the center, and $\sigma$ is the width.
All checks are handled in the calling function.
Parameters
----------
area : float
The area under the Gaussian curve.
center : float
The center of the Gaussian.
width : float
The width (sigma) of the Gaussian.
Returns
-------
np.ndarray
The evaluated Gaussian values at self.energy.
"""
normalization = 1 / (np.sqrt(2 * np.pi) * width)
exponent = -0.5 * ((self.energy_with_offset.values - center) / width) ** 2
return area * normalization * np.exp(exponent)
def _lorentzian_eval(self, area: float, center: float, width: float) -> np.ndarray:
r"""
Evaluate a Lorentzian function.
$$ I(x) = \frac{A}{\\pi} \frac{\Gamma}{(x - x_0)^2 + \Gamma^2}, $$
where $A$ is the area, $x_0$ is the center, and $\\Gamma$ is the half width at half maximum
(HWHM).
All checks are handled in the calling function.
Parameters
----------
area : float
The area under the Lorentzian.
center : float
The center of the Lorentzian.
width : float
The width (HWHM) of the Lorentzian.
Returns
-------
np.ndarray
The evaluated Lorentzian values at self.energy.
"""
normalization = width / np.pi
denominator = (self.energy_with_offset.values - center) ** 2 + width**2
return area * normalization / denominator
def _voigt_eval(
self,
area: float,
center: float,
gaussian_width: float,
lorentzian_width: float,
) -> np.ndarray:
"""
Evaluate a Voigt profile function using scipy's voigt_profile.
Parameters
----------
area : float
The area under the Voigt profile.
center : float
The center of the Voigt profile.
gaussian_width : float
The Gaussian width (sigma) of the Voigt profile.
lorentzian_width : float
The Lorentzian width (HWHM) of the Voigt profile.
Returns
-------
np.ndarray
The evaluated Voigt profile values at self.energy.
"""
return area * voigt_profile(
self.energy_with_offset.values - center, gaussian_width, lorentzian_width
)
def __repr__(self) -> str:
return (
f'{self.__class__.__name__}('
f'display_name={self.display_name!r}, '
f'unique_name={self.unique_name!r}, '
f'unit={self._unit}, '
f'energy_len={len(self.energy)})'
)