Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix estimate_parameters for binned and non uniform axis #2743

Merged
4 changes: 2 additions & 2 deletions hyperspy/_components/arctan.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, A=1., k=1., x0=1., module=["numpy", "scipy"], **kwargs):
# Not to break scripts once we remove the legacy Arctan
if "minimum_at_zero" in kwargs:
del kwargs["minimum_at_zero"]
super(Arctan, self).__init__(
super().__init__(
expression="A * arctan(k * (x - x0))",
name="Arctan",
A=A,
Expand All @@ -65,4 +65,4 @@ def __init__(self, A=1., k=1., x0=1., module=["numpy", "scipy"], **kwargs):
module=module,
autodoc=False,
**kwargs,
)
)
18 changes: 9 additions & 9 deletions hyperspy/_components/bleasdale.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@
class Bleasdale(Expression):

r"""Bleasdale function component.

Also called the Bleasdale-Nelder function. Originates from the description of the yield-density relationship in crop growth.

.. math::

f(x) = \left(a+b\cdot x\right)^{-1/c}

Parameters
-----------
a : Float

b : Float

c : Float

**kwargs
Extra keyword arguments are passed to the ``Expression`` component.

For :math:`(a+b\cdot x)\leq0`, the component will be set to 0.
"""

def __init__(self, a=1., b=1., c=1., module="numexpr", **kwargs):
super(Bleasdale, self).__init__(
super().__init__(
expression="where((a + b * x) > 0, (a + b * x) ** (-1 / c), 0)",
name="Bleasdale",
a=a,
Expand Down Expand Up @@ -84,5 +84,5 @@ def grad_c(self, x):
a = self.a.value
b = self.b.value
c = self.c.value
return np.where((a + b * x) > 0, np.log(a + b * x) / (c ** 2. *
return np.where((a + b * x) > 0, np.log(a + b * x) / (c ** 2. *
(b * x + a) ** (1. / c)), 0)
7 changes: 4 additions & 3 deletions hyperspy/_components/doniach.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import math
import numpy as np

from hyperspy.component import _get_scaling_factor
from hyperspy._components.expression import Expression
from hyperspy._components.gaussian import _estimate_gaussian_parameters
from hyperspy.misc.utils import is_binned # remove in v2.0
Expand Down Expand Up @@ -82,7 +83,7 @@ class Doniach(Expression):

def __init__(self, centre=0., A=1., sigma=1., alpha=0.5,
module=["numpy", "scipy"], **kwargs):
super(Doniach, self).__init__(
super().__init__(
expression="A*cos(0.5*pi*alpha+\
((1.0 - alpha) * arctan( (x-centre+offset)/sigma) ) )\
/(sigma**2 + (x-centre+offset)**2)**(0.5 * (1.0 - alpha));\
Expand Down Expand Up @@ -145,8 +146,8 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
axis = signal.axes_manager.signal_axes[0]
centre, height, sigma = _estimate_gaussian_parameters(signal, x1, x2,
only_current)
scaling_factor = axis.scale if axis.is_uniform \
else np.gradient(axis.axis)[axis.value2index(centre)]
scaling_factor = _get_scaling_factor(signal, axis, centre)

if only_current is True:
self.centre.value = centre
self.sigma.value = sigma
Expand Down
2 changes: 1 addition & 1 deletion hyperspy/_components/eels_arctan.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(self, A=1., k=1., x0=1., module=["numpy", "scipy"], **kwargs):
# Not to break scripts once we remove the legacy Arctan
if "minimum_at_zero" in kwargs:
del kwargs["minimum_at_zero"]
super(EELSArctan, self).__init__(
super().__init__(
expression="A * (pi /2 + arctan(k * (x - x0)))",
name="Arctan",
A=A,
Expand Down
16 changes: 8 additions & 8 deletions hyperspy/_components/eels_double_power_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ class DoublePowerLaw(Expression):
the component will return 0.
"""

def __init__(self, A=1e-5, r=3., origin=0., shift=20., ratio=1.,
left_cutoff=0.0, module="numexpr", compute_gradients=False,
def __init__(self, A=1e-5, r=3., origin=0., shift=20., ratio=1.,
left_cutoff=0.0, module="numexpr", compute_gradients=False,
**kwargs):
super(DoublePowerLaw, self).__init__(
super().__init__(
expression="where(x > left_cutoff, \
A * (ratio * (x - origin - shift) ** -r \
+ (x - origin) ** -r), 0)",
Expand Down Expand Up @@ -106,25 +106,25 @@ def grad_A(self, x):
return self.function(x) / self.A.value

def grad_r(self, x):
return np.where(x > self.left_cutoff.value, -self.A.value *
return np.where(x > self.left_cutoff.value, -self.A.value *
self.ratio.value * (x - self.origin.value -
self.shift.value) ** (-self.r.value) *
np.log(x - self.origin.value - self.shift.value) -
self.A.value * (x - self.origin.value) **
self.A.value * (x - self.origin.value) **
(-self.r.value) * np.log(x - self.origin.value), 0)

def grad_origin(self, x):
return np.where(x > self.left_cutoff.value, self.A.value * self.r.value
* self.ratio.value * (x - self.origin.value - self.shift.value)
* self.ratio.value * (x - self.origin.value - self.shift.value)
** (-self.r.value - 1) + self.A.value * self.r.value
* (x - self.origin.value) ** (-self.r.value - 1), 0)

def grad_shift(self, x):
return np.where(x > self.left_cutoff.value, self.A.value * self.r.value
* self.ratio.value * (x - self.origin.value -
* self.ratio.value * (x - self.origin.value -
self.shift.value) ** (-self.r.value - 1), 0)

def grad_ratio(self, x):
return np.where(x > self.left_cutoff.value, self.A.value *
(x - self.origin.value - self.shift.value) **
(x - self.origin.value - self.shift.value) **
(-self.r.value), 0)
14 changes: 7 additions & 7 deletions hyperspy/_components/error_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ class Erf(Expression):

.. math::

f(x) = \frac{A}{2}~\mathrm{erf}\left[\frac{(x - x_0)}{\sqrt{2}
f(x) = \frac{A}{2}~\mathrm{erf}\left[\frac{(x - x_0)}{\sqrt{2}
\sigma}\right]


============== =============
Variable Parameter
Variable Parameter
============== =============
:math:`A` A
:math:`\sigma` sigma
:math:`x_0` origin
:math:`A` A
:math:`\sigma` sigma
:math:`x_0` origin
============== =============

Parameters
Expand All @@ -48,13 +48,13 @@ class Erf(Expression):
origin : float
Position of the zero crossing.
"""

def __init__(self, A=1., sigma=1., origin=0., module=["numpy", "scipy"],
**kwargs):
if LooseVersion(sympy.__version__) < LooseVersion("1.3"):
raise ImportError("The `ErrorFunction` component requires "
"SymPy >= 1.3")
super(Erf, self).__init__(
super().__init__(
expression="A * erf((x - origin) / sqrt(2) / sigma) / 2",
name="Erf",
A=A,
Expand Down
4 changes: 2 additions & 2 deletions hyperspy/_components/exponential.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Exponential(Expression):
"""

def __init__(self, A=1., tau=1., module="numexpr", **kwargs):
super(Exponential, self).__init__(
super().__init__(
LMSC-NTappy marked this conversation as resolved.
Show resolved Hide resolved
expression="A * exp(-x / tau)",
name="Exponential",
A=A,
Expand Down Expand Up @@ -86,7 +86,7 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
bool

"""
super(Exponential, self)._estimate_parameters(signal)
super()._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
i1, i2 = axis.value_range_to_indices(x1, x2)
if i1 + 1 == i2:
Expand Down
9 changes: 5 additions & 4 deletions hyperspy/_components/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import numpy as np
import dask.array as da

from hyperspy.component import _get_scaling_factor
from hyperspy._components.expression import Expression
from hyperspy.misc.utils import is_binned # remove in v2.0

Expand Down Expand Up @@ -101,7 +102,7 @@ class Gaussian(Expression):
"""

def __init__(self, A=1., sigma=1., centre=0., module="numexpr", **kwargs):
super(Gaussian, self).__init__(
super().__init__(
expression="A * (1 / (sigma * sqrt(2*pi))) * exp(-(x - centre)**2 \
/ (2 * sigma**2))",
name="Gaussian",
Expand Down Expand Up @@ -160,12 +161,12 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
>>> g.estimate_parameters(s, -10, 10, False)
"""

super(Gaussian, self)._estimate_parameters(signal)
super()._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
centre, height, sigma = _estimate_gaussian_parameters(signal, x1, x2,
only_current)
scaling_factor = axis.scale if axis.is_uniform \
else np.gradient(axis.axis)[axis.value2index(centre)]
scaling_factor = _get_scaling_factor(signal, axis, centre)

if only_current is True:
self.centre.value = centre
self.sigma.value = sigma
Expand Down
2 changes: 1 addition & 1 deletion hyperspy/_components/gaussian2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Gaussian2D(Expression):

def __init__(self, A=1., sigma_x=1., sigma_y=1., centre_x=0.,
centre_y=0, module="numexpr", **kwargs):
super(Gaussian2D, self).__init__(
super().__init__(
expression="A * (1 / (sigma_x * sigma_y * 2 * pi)) * \
exp(-((x - centre_x) ** 2 / (2 * sigma_x ** 2) \
+ (y - centre_y) ** 2 / (2 * sigma_y ** 2)))",
Expand Down
10 changes: 5 additions & 5 deletions hyperspy/_components/gaussianhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
# along with HyperSpy. If not, see <http://www.gnu.org/licenses/>.

import math
import numpy as np

from hyperspy._components.expression import Expression
from hyperspy._components.gaussian import _estimate_gaussian_parameters
from hyperspy.component import _get_scaling_factor
from hyperspy.misc.utils import is_binned # remove in v2.0

sqrt2pi = math.sqrt(2 * math.pi)
Expand Down Expand Up @@ -78,7 +78,7 @@ class GaussianHF(Expression):

def __init__(self, height=1., fwhm=1., centre=0., module="numexpr",
**kwargs):
super(GaussianHF, self).__init__(
super().__init__(
expression="height * exp(-(x - centre)**2 * 4 * log(2)/fwhm**2)",
name="GaussianHF",
height=height,
Expand Down Expand Up @@ -136,12 +136,12 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
>>> g.estimate_parameters(s, -10, 10, False)
"""

super(GaussianHF, self)._estimate_parameters(signal)
super()._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
centre, height, sigma = _estimate_gaussian_parameters(signal, x1, x2,
only_current)
scaling_factor = axis.scale if axis.is_uniform \
else np.gradient(axis.axis)[axis.value2index(centre)]
scaling_factor = _get_scaling_factor(signal, axis, centre)

if only_current is True:
self.centre.value = centre
self.fwhm.value = sigma * sigma2fwhm
Expand Down
3 changes: 1 addition & 2 deletions hyperspy/_components/heaviside.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class HeavisideStep(Expression):

def __init__(self, A=1., n=0., module="numpy", compute_gradients=True,
**kwargs):
super(HeavisideStep, self).__init__(
super().__init__(
expression="A*heaviside(x-n,0.5)",
name="HeavisideStep",
A=A,
Expand All @@ -71,4 +71,3 @@ def __init__(self, A=1., n=0., module="numpy", compute_gradients=True,

self.isbackground = True
self.convolved = False

20 changes: 10 additions & 10 deletions hyperspy/_components/logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ class Logistic(Expression):
r"""Logistic function (sigmoid or s-shaped curve) component.

.. math::
f(x) = \frac{a}{1 + b\cdot \mathrm{exp}\left[-c

f(x) = \frac{a}{1 + b\cdot \mathrm{exp}\left[-c
\left((x - x_0\right)\right]}

============== =============
Variable Parameter
Variable Parameter
============== =============
:math:`A` a
:math:`b` b
:math:`c` c
:math:`x_0` origin
:math:`A` a
:math:`b` b
:math:`c` c
:math:`x_0` origin
============== =============


Parameters
-----------
a : Float
The curve's maximum y-value,
The curve's maximum y-value,
:math:`\mathrm{lim}_{x\to\infty}\left(y\right) = a`
b : Float
Additional parameter:
Additional parameter:
b>1 shifts origin to larger values;
0<b<1 shifts origin to smaller values;
b<0 introduces an asymptote
Expand All @@ -57,7 +57,7 @@ class Logistic(Expression):
"""

def __init__(self, a=1., b=1., c=1., origin=0., module="numexpr", **kwargs):
super(Logistic, self).__init__(
super().__init__(
expression="a / (1 + b * exp(-c * (x - origin)))",
name="Logistic",
a=a,
Expand Down
9 changes: 5 additions & 4 deletions hyperspy/_components/lorentzian.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import numpy as np
import dask.array as da

from hyperspy.component import _get_scaling_factor
from hyperspy._components.expression import Expression
from hyperspy.misc.utils import is_binned # remove in v2.0

Expand Down Expand Up @@ -96,7 +97,7 @@ class Lorentzian(Expression):
def __init__(self, A=1., gamma=1., centre=0., module="numexpr", **kwargs):
# We use `_gamma` internally to workaround the use of the `gamma`
# function in sympy
super(Lorentzian, self).__init__(
super().__init__(
expression="A / pi * (_gamma / ((x - centre)**2 + _gamma**2))",
name="Lorentzian",
A=A,
Expand Down Expand Up @@ -160,12 +161,12 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
>>> g.estimate_parameters(s, -10, 10, False)
"""

super(Lorentzian, self)._estimate_parameters(signal)
super()._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
centre, height, gamma = _estimate_lorentzian_parameters(signal, x1, x2,
only_current)
scaling_factor = axis.scale if axis.is_uniform \
else np.gradient(axis.axis)[axis.value2index(centre)]
scaling_factor = _get_scaling_factor(signal, axis, centre)

if only_current is True:
self.centre.value = centre
self.gamma.value = gamma
Expand Down