Skip to content

Commit

Permalink
Merge pull request #2728 from jlaehne/nua_binned_fixes
Browse files Browse the repository at this point in the history
Fixes for binned non_uniform_axes
  • Loading branch information
thomasaarholt committed May 10, 2021
2 parents 3ebb886 + 967fe74 commit a3b7b87
Show file tree
Hide file tree
Showing 42 changed files with 783 additions and 212 deletions.
11 changes: 6 additions & 5 deletions hyperspy/_components/doniach.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,25 @@ 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)]
if only_current is True:
self.centre.value = centre
self.sigma.value = sigma
self.A.value = height * 1.3
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.A.value /= axis.scale
self.A.value /= scaling_factor
return True
else:
if self.A.map is None:
self._create_arrays()
self.A.map['values'][:] = height * 1.3
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.A.map['values'][:] /= axis.scale
self.A.map['values'][:] /= scaling_factor
self.A.map['is_set'][:] = True
self.sigma.map['values'][:] = sigma
self.sigma.map['is_set'][:] = True
Expand Down
13 changes: 7 additions & 6 deletions hyperspy/_components/exponential.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
"""
super(Exponential, self)._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
if not axis.is_uniform and self.binned:
raise NotImplementedError(
"This operation is not implemented for non-uniform axes.")
i1, i2 = axis.value_range_to_indices(x1, x2)
if i1 + 1 == i2:
if i2 < axis.high_index:
Expand Down Expand Up @@ -148,11 +145,15 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
'with a "divide by zero" error (likely log of '
'a zero or negative value).')
return False

if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
A /= axis.scale
if axis.is_uniform:
A /= axis.scale
else:
# using the mean of the gradient for non-uniform axes is a best
# guess to the scaling of binned signals for the estimation
A /= np.mean(np.gradient(axis.axis))
if only_current is True:
self.A.value = A
self.tau.value = t
Expand Down
13 changes: 6 additions & 7 deletions hyperspy/_components/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,27 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):

super(Gaussian, self)._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
if not axis.is_uniform and self.binned:
raise NotImplementedError(
"This operation is not implemented for non-uniform axes.")
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)]
if only_current is True:
self.centre.value = centre
self.sigma.value = sigma
self.A.value = height * sigma * sqrt2pi
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.A.value /= axis.scale
self.A.value /= scaling_factor
return True
else:
if self.A.map is None:
self._create_arrays()
self.A.map['values'][:] = height * sigma * sqrt2pi
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.A.map['values'] /= axis.scale
self.A.map['values'] /= scaling_factor
self.A.map['is_set'][:] = True
self.sigma.map['values'][:] = sigma
self.sigma.map['is_set'][:] = True
Expand Down
12 changes: 7 additions & 5 deletions hyperspy/_components/gaussianhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# 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
Expand Down Expand Up @@ -139,24 +140,25 @@ 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)]
if only_current is True:
self.centre.value = centre
self.fwhm.value = sigma * sigma2fwhm
self.height.value = float(height)
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.height.value /= axis.scale
self.height.value /= scaling_factor
return True
else:
if self.height.map is None:
self._create_arrays()
self.height.map['values'][:] = height
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.height.map['values'][:] /= axis.scale
self.height.map['values'][:] /= scaling_factor
self.height.map['is_set'][:] = True
self.fwhm.map['values'][:] = sigma * sigma2fwhm
self.fwhm.map['is_set'][:] = True
Expand Down
13 changes: 6 additions & 7 deletions hyperspy/_components/lorentzian.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,27 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):

super(Lorentzian, self)._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
if not axis.is_uniform and self.binned:
raise NotImplementedError(
"This operation is not implemented for non-uniform axes.")
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)]
if only_current is True:
self.centre.value = centre
self.gamma.value = gamma
self.A.value = height * gamma * np.pi
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.A.value /= axis.scale
self.A.value /= scaling_factor
return True
else:
if self.A.map is None:
self._create_arrays()
self.A.map['values'][:] = height * gamma * np.pi
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.A.map['values'] /= axis.scale
self.A.map['values'] /= scaling_factor
self.A.map['is_set'][:] = True
self.gamma.map['values'][:] = gamma
self.gamma.map['is_set'][:] = True
Expand Down
16 changes: 8 additions & 8 deletions hyperspy/_components/offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
"""
super(Offset, self)._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
if not axis.is_uniform and self.binned:
raise NotImplementedError(
"This operation is not implemented for non-uniform axes.")
i1, i2 = axis.value_range_to_indices(x1, x2)

# using the mean of the gradient for non-uniform axes is a best guess
# to the scaling of binned signals for the estimation
scaling_factor = axis.scale if axis.is_uniform \
else np.mean(np.gradient(axis.axis))
if only_current is True:
self.offset.value = signal()[i1:i2].mean()
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.offset.value /= axis.scale
self.offset.value /= scaling_factor
return True
else:
if self.offset.map is None:
Expand All @@ -108,10 +108,10 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
gi[axis.index_in_array] = slice(i1, i2)
self.offset.map['values'][:] = dc[tuple(
gi)].mean(axis.index_in_array)
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.offset.map['values'] /= axis.scale
self.offset.map['values'] /= scaling_factor
self.offset.map['is_set'][:] = True
self.fetch_stored_values()
return True
Expand Down
11 changes: 6 additions & 5 deletions hyperspy/_components/pes_voigt.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,24 +296,25 @@ def estimate_parameters(self, signal, E1, E2, only_current=False):
axis = signal.axes_manager.signal_axes[0]
centre, height, sigma = _estimate_gaussian_parameters(signal, E1, E2,
only_current)

scaling_factor = axis.scale if axis.is_uniform \
else np.gradient(axis.axis)[axis.value2index(centre)]
if only_current is True:
self.centre.value = centre
self.FWHM.value = sigma * sigma2fwhm
self.area.value = height * sigma * sqrt2pi
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.area.value /= axis.scale
self.area.value /= scaling_factor
return True
else:
if self.area.map is None:
self._create_arrays()
self.area.map['values'][:] = height * sigma * sqrt2pi
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.area.map['values'][:] /= axis.scale
self.area.map['values'][:] /= scaling_factor
self.area.map['is_set'][:] = True
self.FWHM.map['values'][:] = sigma * sigma2fwhm
self.FWHM.map['is_set'][:] = True
Expand Down
16 changes: 8 additions & 8 deletions hyperspy/_components/polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,21 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
"""
super()._estimate_parameters(signal)

axis = signal.axes_manager.signal_axes[0]
if not axis.is_uniform and self.binned:
raise NotImplementedError(
"This operation is not implemented for non-uniform axes.")
i1, i2 = axis.value_range_to_indices(x1, x2)
# using the mean of the gradient for non-uniform axes is a best guess
# to the scaling of binned signals for the estimation
scaling_factor = axis.scale if axis.is_uniform \
else np.mean(np.gradient(axis.axis))
if only_current is True:
estimation = np.polyfit(axis.axis[i1:i2],
signal()[i1:i2],
self.get_polynomial_order())
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
for para, estim in zip(self.parameters[::-1], estimation):
para.value = estim / axis.scale
para.value = estim / scaling_factor
else:
for para, estim in zip(self.parameters[::-1], estimation):
para.value = estim
Expand All @@ -127,11 +127,11 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
cmap_shape = nav_shape + (self.get_polynomial_order() + 1, )
fit = fit.reshape(cmap_shape)

if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
for i, para in enumerate(self.parameters[::-1]):
para.map['values'][:] = fit[..., i] / axis.scale
para.map['values'][:] = fit[..., i] / scaling_factor
para.map['is_set'][:] = True
else:
for i, para in enumerate(self.parameters[::-1]):
Expand Down
15 changes: 8 additions & 7 deletions hyperspy/_components/polynomial_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,19 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
"""
super(Polynomial, self)._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
if not axis.is_uniform and self.binned:
raise NotImplementedError(
"This operation is not implemented for non-uniform axes.")
i1, i2 = axis.value_range_to_indices(x1, x2)
# using the mean of the gradient for non-uniform axes is a best guess
# to the scaling of binned signals for the estimation
scaling_factor = axis.scale if axis.is_uniform \
else np.mean(np.gradient(axis.axis))
if only_current is True:
estimation = np.polyfit(axis.axis[i1:i2],
signal()[i1:i2],
self.get_polynomial_order())
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.coefficients.value = estimation / axis.scale
self.coefficients.value = estimation / scaling_factor
else:
self.coefficients.value = estimation
return True
Expand All @@ -159,10 +160,10 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):
# Shape needed to fit coefficients.map:
cmap_shape = nav_shape + (self.get_polynomial_order() + 1, )
self.coefficients.map['values'][:] = cmaps.reshape(cmap_shape)
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.coefficients.map["values"] /= axis.scale
self.coefficients.map["values"] /= scaling_factor
self.coefficients.map['is_set'][:] = True
self.fetch_stored_values()
return True
Expand Down
9 changes: 6 additions & 3 deletions hyperspy/_components/scalable_fixed_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,13 @@ def _function(self, x, xscale, yscale, shift):
result = yscale * self.f(x * xscale - shift)
else:
result = yscale * self.signal.data
if is_binned(self.signal) is True:
if is_binned(self.signal):
# in v2 replace by
#if self.signal.axes_manager.signal_axes[0].is_binned is True:
return result / self.signal.axes_manager.signal_axes[0].scale
#if self.signal.axes_manager.signal_axes[0].is_binned:
if self.signal.axes_manager.signal_axes[0].is_uniform:
return result / self.signal.axes_manager.signal_axes[0].scale
else:
return result / np.gradient(self.signal.axes_manager.signal_axes[0].axis)
else:
return result

Expand Down
13 changes: 6 additions & 7 deletions hyperspy/_components/skew_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,30 +207,29 @@ def estimate_parameters(self, signal, x1, x2, only_current=False):

super(SkewNormal, self)._estimate_parameters(signal)
axis = signal.axes_manager.signal_axes[0]
if not axis.is_uniform and self.binned:
raise NotImplementedError(
"This operation is not implemented for non-uniform axes.")
x0, height, scale, shape = _estimate_skewnormal_parameters(signal, x1,
x2, only_current)
scaling_factor = axis.scale if axis.is_uniform \
else np.gradient(axis.axis)[axis.value2index(x0)]
if only_current is True:
self.x0.value = x0
self.A.value = height * sqrt2pi
self.scale.value = scale
self.shape.value = shape
if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.A.value /= axis.scale
self.A.value /= scaling_factor
return True
else:
if self.A.map is None:
self._create_arrays()
self.A.map['values'][:] = height * sqrt2pi

if is_binned(signal) is True:
if is_binned(signal):
# in v2 replace by
#if axis.is_binned:
self.A.map['values'] /= axis.scale
self.A.map['values'] /= scaling_factor
self.A.map['is_set'][:] = True
self.x0.map['values'][:] = x0
self.x0.map['is_set'][:] = True
Expand Down

0 comments on commit a3b7b87

Please sign in to comment.