forked from hyperspy/hyperspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skew_normal.py
269 lines (230 loc) · 9.31 KB
/
skew_normal.py
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
# -*- coding: utf-8 -*-
# Copyright 2007-2023 The HyperSpy developers
#
# This file is part of HyperSpy.
#
# HyperSpy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# HyperSpy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with HyperSpy. If not, see <https://www.gnu.org/licenses/#GPL>.
import dask.array as da
import numpy as np
from hyperspy.component import _get_scaling_factor
from hyperspy._components.expression import Expression
sqrt2pi = np.sqrt(2 * np.pi)
def _estimate_skewnormal_parameters(signal, x1, x2, only_current):
axis = signal.axes_manager.signal_axes[0]
i1, i2 = axis.value_range_to_indices(x1, x2)
X = axis.axis[i1:i2]
if only_current is True:
data = signal()[i1:i2]
X_shape = (len(X),)
i = 0
x0_shape = (1,)
else:
i = axis.index_in_array
data_gi = [slice(None), ] * len(signal.data.shape)
data_gi[axis.index_in_array] = slice(i1, i2)
data = signal.data[tuple(data_gi)]
X_shape = [1, ] * len(signal.data.shape)
X_shape[axis.index_in_array] = data.shape[i]
x0_shape = list(data.shape)
x0_shape[i] = 1
a1 = np.sqrt(2 / np.pi)
b1 = (4 / np.pi - 1) * a1
m1 = np.sum(X.reshape(X_shape) * data, i) / np.sum(data, i)
m2 = abs(np.sum((X.reshape(X_shape) - m1.reshape(x0_shape)) ** 2 * data, i)
/ np.sum(data, i))
m3 = abs(np.sum((X.reshape(X_shape) - m1.reshape(x0_shape)) ** 3 * data, i)
/ np.sum(data, i))
x0 = m1 - a1 * (m3 / b1) ** (1 / 3)
scale = np.sqrt(m2 + a1 ** 2 * (m3 / b1) ** (2 / 3))
delta = np.sqrt(1 / (a1**2 + m2 * (b1 / m3) ** (2 / 3)))
shape = delta / np.sqrt(1 - delta**2)
iheight = np.argmin(abs(X.reshape(X_shape) - x0.reshape(x0_shape)), i)
# height is the value of the function at x0, shich has to be computed
# differently for dask array (lazy) and depending on the dimension
if isinstance(data, da.Array):
x0, iheight, scale, shape = da.compute(x0, iheight, scale, shape)
if only_current is True or signal.axes_manager.navigation_dimension == 0:
height = data.vindex[iheight].compute()
elif signal.axes_manager.navigation_dimension == 1:
height = data.vindex[np.arange(signal.axes_manager.navigation_size),
iheight].compute()
else:
height = data.vindex[(*np.indices(signal.axes_manager.navigation_shape),
iheight)].compute()
else:
if only_current is True or signal.axes_manager.navigation_dimension == 0:
height = data[iheight]
elif signal.axes_manager.navigation_dimension == 1:
height = data[np.arange(signal.axes_manager.navigation_size),
iheight]
else:
height = data[(*np.indices(signal.axes_manager.navigation_shape),
iheight)]
return x0, height, scale, shape
class SkewNormal(Expression):
r"""Skew normal distribution component.
| Asymmetric peak shape based on a normal distribution.
| For definition see
https://en.wikipedia.org/wiki/Skew_normal_distribution
| See also http://azzalini.stat.unipd.it/SN/
|
.. math::
f(x) &= 2 A \phi(x) \Phi(x) \\
\phi(x) &= \frac{1}{\sqrt{2\pi}}\mathrm{exp}{\left[
-\frac{t(x)^2}{2}\right]} \\
\Phi(x) &= \frac{1}{2}\left[1 + \mathrm{erf}\left(\frac{
\alpha~t(x)}{\sqrt{2}}\right)\right] \\
t(x) &= \frac{x-x_0}{\omega}
============== =============
Variable Parameter
============== =============
:math:`x_0` x0
:math:`A` A
:math:`\omega` scale
:math:`\alpha` shape
============== =============
Parameters
-----------
x0 : float
Location of the peak position (not maximum, which is given by
the `mode` property).
A : float
Height parameter of the peak.
scale : float
Width (sigma) parameter.
shape: float
Skewness (asymmetry) parameter. For shape=0, the normal
distribution (Gaussian) is obtained. The distribution is
right skewed (longer tail to the right) if shape>0 and is
left skewed if shape<0.
**kwargs
Extra keyword arguments are passed to the
:py:class:`~._components.expression.Expression` component.
Notes
-----
The properties `mean` (position), `variance`, `skewness` and `mode`
(position of maximum) are defined for convenience.
"""
def __init__(self, x0=0., A=1., scale=1., shape=0.,
module=['numpy', 'scipy'], **kwargs):
# We use `_shape` internally because `shape` is already taken in sympy
# https://github.com/sympy/sympy/pull/20791
super().__init__(
expression="2 * A * normpdf * normcdf;\
normpdf = exp(- t ** 2 / 2) / sqrt(2 * pi);\
normcdf = (1 + erf(_shape * t / sqrt(2))) / 2;\
t = (x - x0) / scale",
name="SkewNormal",
x0=x0,
A=A,
scale=scale,
shape=shape,
module=module,
autodoc=False,
rename_pars={"_shape": "shape"},
**kwargs,
)
# Boundaries
self.A.bmin = 0.
self.scale.bmin = 0
self.isbackground = False
self.convolved = True
def estimate_parameters(self, signal, x1, x2, only_current=False):
"""Estimate the skew normal distribution by calculating the momenta.
Parameters
----------
signal : Signal1D instance
x1 : float
Defines the left limit of the spectral range to use for the
estimation.
x2 : float
Defines the right limit of the spectral range to use for the
estimation.
only_current : bool
If False estimates the parameters for the full dataset.
Returns
-------
bool
Notes
-----
Adapted from Lin, Lee and Yen, Statistica Sinica 17, 909-927 (2007)
https://www.jstor.org/stable/24307705
Examples
--------
>>> g = hs.model.components1D.SkewNormal()
>>> x = np.arange(-10, 10, 0.01)
>>> data = np.zeros((32, 32, 2000))
>>> data[:] = g.function(x).reshape((1, 1, 2000))
>>> s = hs.signals.Signal1D(data)
>>> s.axes_manager._axes[-1].offset = -10
>>> s.axes_manager._axes[-1].scale = 0.01
>>> g.estimate_parameters(s, -10, 10, False)
"""
super()._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
x0, height, scale, shape = _estimate_skewnormal_parameters(
signal, x1, x2, only_current
)
scaling_factor = _get_scaling_factor(signal, axis, x0)
if only_current is True:
self.x0.value = x0
self.A.value = height * sqrt2pi
self.scale.value = scale
self.shape.value = shape
if axis.is_binned:
self.A.value /= scaling_factor
return True
else:
if self.A.map is None:
self._create_arrays()
self.A.map['values'][:] = height * sqrt2pi
if axis.is_binned:
self.A.map['values'] /= scaling_factor
self.A.map['is_set'][:] = True
self.x0.map['values'][:] = x0
self.x0.map['is_set'][:] = True
self.scale.map['values'][:] = scale
self.scale.map['is_set'][:] = True
self.shape.map['values'][:] = shape
self.shape.map['is_set'][:] = True
self.fetch_stored_values()
return True
@property
def mean(self):
"""Mean (position) of the component."""
delta = self.shape.value / np.sqrt(1 + self.shape.value**2)
return self.x0.value + self.scale.value * delta * np.sqrt(2 / np.pi)
@property
def variance(self):
"""Variance of the component."""
delta = self.shape.value / np.sqrt(1 + self.shape.value**2)
return self.scale.value**2 * (1 - 2 * delta**2 / np.pi)
@property
def skewness(self):
"""Skewness of the component."""
delta = self.shape.value / np.sqrt(1 + self.shape.value**2)
return (4 - np.pi)/2 * (delta * np.sqrt(2/np.pi))**3 / (1 -
2 * delta**2 / np.pi)**(3/2)
@property
def mode(self):
"""Mode (position of maximum) of the component."""
delta = self.shape.value / np.sqrt(1 + self.shape.value**2)
muz = np.sqrt(2 / np.pi) * delta
sigmaz = np.sqrt(1 - muz**2)
if self.shape.value == 0:
return self.x0.value
else:
m0 = muz - self.skewness * sigmaz / 2 - np.sign(self.shape.value) \
/ 2 * np.exp(- 2 * np.pi / abs(self.shape.value))
return self.x0.value + self.scale.value * m0