Skip to content

Commit

Permalink
Set explicit fit('leastsq') on other tests, fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasaarholt committed Jul 23, 2018
1 parent efccaff commit 187da73
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 80 deletions.
2 changes: 1 addition & 1 deletion examples/hyperspy_as_library/curve_fitting_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

m = s.create_model(ll=ll)
m.enable_fine_structure()
m.multifit(kind="smart")
m.multifit(fitter='leastsq', kind="smart")
m.plot()

plt.savefig("model_original_spectrum_plot.png")
3 changes: 2 additions & 1 deletion hyperspy/_components/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ class docstring.
name, sympy.latex(_parse_substitutions(expression)))

for para in self.parameters:
para._is_linear = check_parameter_linearity(expression, para.name)
para._is_linear = check_parameter_linearity(self._parsed_expr, para.name)

def compile_function(self, module="numpy", position=False):
expr = _parse_substitutions(self._str_expression)
self._parsed_expr = expr
# Extract x
x, = [symbol for symbol in expr.free_symbols if symbol.name == "x"]
# Extract y
Expand Down
2 changes: 1 addition & 1 deletion hyperspy/datasets/artificial_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def get_core_loss_eels_model():
"""
s = get_core_loss_eels_signal()
m = s.create_model(auto_background=False, GOS='hydrogenic')
m.fit()
m.fit(fitter='leastsq')
return m


Expand Down
4 changes: 2 additions & 2 deletions hyperspy/tests/drawing/test_plot_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def set_gaussian(gaussian, centre, sigma):
sigma_value_gaussian):
set_gaussian(gaussian, centre, sigma)

m.fit()
m.fit(fitter='leastsq')
m.plot(plot_components=plot_component)

def A_value(s, component, binned):
Expand Down Expand Up @@ -145,6 +145,6 @@ def test_fit_EELS_convolved(convolved):
cl.metadata.General.title = 'Convolved: {}'.format(convolved)
ll = hs.load(os.path.join(dname, 'Cr_L_ll.hspy')) if convolved else None
m = cl.create_model(auto_background=False, ll=ll, GOS='hydrogenic')
m.fit(kind='smart')
m.fit(fitter='leastsq', kind='smart')
m.plot(plot_components=True)
return m._plot.signal_plot.figure
12 changes: 6 additions & 6 deletions hyperspy/tests/model/test_chi_squared.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_chisq_with_fit(self):
m = self.model
g = Gaussian()
m.append(g)
m.fit()
m.fit(fitter='leastsq')
assert np.allclose(m.chisq(), 7.78966223)

def test_dof_with_fit(self):
Expand All @@ -47,14 +47,14 @@ def test_dof_with_fit(self):
g1 = Gaussian()
m.extend((g, g1))
g1.set_parameters_not_free('A')
m.fit()
m.fit(fitter='leastsq')
assert np.equal(m.dof(), 5)

def test_red_chisq_with_fit(self):
m = self.model
g = Gaussian()
m.append(g)
m.fit()
m.fit(fitter='leastsq')
assert np.allclose(m.red_chisq(), 1.55793245)

def test_chisq(self):
Expand Down Expand Up @@ -94,7 +94,7 @@ def test_chisq_in_range(self):
g = Gaussian()
m.append(g)
m.set_signal_range(1, 7)
m.fit()
m.fit(fitter='leastsq')
assert np.allclose(m.red_chisq(), 2.87544335)

def test_chisq_with_inactive_components(self):
Expand All @@ -104,7 +104,7 @@ def test_chisq_with_inactive_components(self):
m.append(ga)
m.append(gin)
gin.active = False
m.fit()
m.fit(fitter='leastsq')
assert np.allclose(m.chisq(), 7.78966223)

def test_dof_with_inactive_components(self):
Expand All @@ -114,5 +114,5 @@ def test_dof_with_inactive_components(self):
m.append(ga)
m.append(gin)
gin.active = False
m.fit()
m.fit(fitter='leastsq')
assert np.equal(m.dof(), 3)
13 changes: 7 additions & 6 deletions hyperspy/tests/model/test_component.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np

from hyperspy.component import Component
from hyperspy.axes import AxesManager
from hyperspy.axes import AxesManager, DataAxis
from unittest import mock


Expand Down Expand Up @@ -202,17 +202,18 @@ def setup_method(self, method):
c.model = mock.MagicMock()
c.model.__call__ = mock.MagicMock()
c.model.channel_switches = np.array([True, False, True])
c.model.axis.axis = np.array([0.1, 0.2, 0.3])
c.model.axes_manager.signal_axes = [DataAxis(size=3, offset=0.1, scale=0.1)]
c.model.axis.axis = c.model.axes_manager.signal_axes[0].axis
c.function = mock.MagicMock()
c.function.return_value = np.array([1.3, ])
c.function.return_value = np.array([1.3, 1.3, 1.3])
c.model.signal.axes_manager.signal_axes = [mock.MagicMock(), ]
c.model.signal.axes_manager.signal_axes[0].scale = 2.

def test_call(self):
c = self.c
assert 1.3 == c()
np.testing.assert_array_equal(c.function.call_args[0][0],
np.array([0.1, 0.3]))
assert (1.3 == c()).all()
np.testing.assert_array_almost_equal(c.function.call_args[0][0],
np.array([0.1, 0.2, 0.3]))

def test_plotting_not_active_component(self):
c = self.c
Expand Down
8 changes: 4 additions & 4 deletions hyperspy/tests/model/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def test_both_unbinned(self):
m.append(fp)
with ignore_warning(message="invalid value encountered in sqrt",
category=RuntimeWarning):
m.fit()
m.fit(fitter='leastsq')
assert abs(fp.yscale.value - 100) <= 0.1

def test_both_binned(self):
Expand All @@ -302,7 +302,7 @@ def test_both_binned(self):
m.append(fp)
with ignore_warning(message="invalid value encountered in sqrt",
category=RuntimeWarning):
m.fit()
m.fit(fitter='leastsq')
assert abs(fp.yscale.value - 100) <= 0.1

def test_pattern_unbinned_signal_binned(self):
Expand All @@ -315,7 +315,7 @@ def test_pattern_unbinned_signal_binned(self):
m.append(fp)
with ignore_warning(message="invalid value encountered in sqrt",
category=RuntimeWarning):
m.fit()
m.fit(fitter='leastsq')
assert abs(fp.yscale.value - 1000) <= 1

def test_pattern_binned_signal_unbinned(self):
Expand All @@ -328,7 +328,7 @@ def test_pattern_binned_signal_unbinned(self):
m.append(fp)
with ignore_warning(message="invalid value encountered in sqrt",
category=RuntimeWarning):
m.fit()
m.fit(fitter='leastsq')
assert abs(fp.yscale.value - 10) <= .1


Expand Down
18 changes: 9 additions & 9 deletions hyperspy/tests/model/test_edsmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def setup_method(self, method):
def test_fit(self):
s = self.s
m = s.create_model()
m.fit()
m.fit(fitter='leastsq')
np.testing.assert_allclose([i.data for i in
m.get_lines_intensity()],
[[0.5], [0.2], [0.3]], atol=10 - 4)
Expand Down Expand Up @@ -84,8 +84,8 @@ def test_temmodel_store(self):
def test_calibrate_energy_resolution(self):
s = self.s
m = s.create_model()
m.fit()
m.fit_background()
m.fit(fitter='leastsq')
m.fit_background(fitter='leastsq')
reso = s.metadata.Acquisition_instrument.TEM.Detector.EDS.\
energy_resolution_MnKa,
s.set_microscope_parameters(energy_resolution_MnKa=150)
Expand All @@ -97,7 +97,7 @@ def test_calibrate_energy_resolution(self):
def test_calibrate_energy_scale(self):
s = self.s
m = s.create_model()
m.fit()
m.fit(fitter='leastsq')
ax = s.axes_manager[-1]
scale = ax.scale
ax.scale += 0.0004
Expand All @@ -107,7 +107,7 @@ def test_calibrate_energy_scale(self):
def test_calibrate_energy_offset(self):
s = self.s
m = s.create_model()
m.fit()
m.fit(fitter='leastsq')
ax = s.axes_manager[-1]
offset = ax.offset
ax.offset += 0.04
Expand All @@ -117,7 +117,7 @@ def test_calibrate_energy_offset(self):
def test_calibrate_xray_energy(self):
s = self.s
m = s.create_model()
m.fit()
m.fit(fitter='leastsq')
m['Fe_Ka'].centre.value = 6.39
m.calibrate_xray_lines(calibrate='energy', xray_lines=['Fe_Ka'],
bound=100)
Expand All @@ -135,7 +135,7 @@ def test_calibrate_xray_weight(self):
'offset': 4.9})
s = (s + s1 / 50)
m = s.create_model()
m.fit()
m.fit(fitter='leastsq')
with assert_warns(message='The X-ray line expected to be in the model '
'was not found'):
m.calibrate_xray_lines(calibrate='sub_weight',
Expand All @@ -147,7 +147,7 @@ def test_calibrate_xray_weight(self):
def test_calibrate_xray_width(self):
s = self.s
m = s.create_model()
m.fit()
m.fit(fitter='leastsq')
sigma = m['Fe_Ka'].sigma.value
m['Fe_Ka'].sigma.value = 0.065
m.calibrate_xray_lines(calibrate='energy', xray_lines=['Fe_Ka'],
Expand Down Expand Up @@ -205,7 +205,7 @@ def setup_method(self, method):
def test_lines_intensity(self):
s = self.s
m = s.create_model()
m.multifit() # m.fit() is just too inaccurate
m.multifit(fitter='leastsq') # m.fit() is just too inaccurate
ws = np.array([0.5, 0.7, 0.3, 0.5])
w = np.zeros((4,) + self.mix.shape)
for x in range(self.mix.shape[0]):
Expand Down
6 changes: 3 additions & 3 deletions hyperspy/tests/model/test_eelsmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ def setup_method(self, method):
self.m.append(hs.model.components1D.Offset())

def test_fit_background_B_C(self):
self.m.fit_background()
self.m.fit_background(fitter='leastsq')
assert_allclose(self.m["Offset"].offset.value,
1)
assert self.m["B_K"].active
assert self.m["C_K"].active

def test_fit_background_C(self):
self.m["B_K"].active = False
self.m.fit_background()
self.m.fit_background(fitter='leastsq')
assert_allclose(self.m["Offset"].offset.value,
1.71212121212)
assert not self.m["B_K"].active
Expand All @@ -193,7 +193,7 @@ def test_fit_background_C(self):
def test_fit_background_no_edge(self):
self.m["B_K"].active = False
self.m["C_K"].active = False
self.m.fit_background()
self.m.fit_background(fitter='leastsq')
assert_allclose(self.m["Offset"].offset.value,
2.13567839196)
assert not self.m["B_K"].active
Expand Down
2 changes: 1 addition & 1 deletion hyperspy/tests/model/test_fancy_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def setup_method(self, method):
m.append(g)
g.active_is_multidimensional = True
for index in m.axes_manager:
m.fit()
m.fit(fitter='leastsq')
self.model = m

def test_model_signal_indexer_slice(self):
Expand Down
16 changes: 8 additions & 8 deletions hyperspy/tests/model/test_fit_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ def test_fit_component(self):

g1 = Gaussian()
m.append(g1)
m.fit_component(g1, signal_range=(4000, 6000))
m.fit_component(g1, signal_range=(4000, 6000), fitter='leastsq')
np.testing.assert_allclose(self.g.function(axis),
g1.function(axis),
rtol=self.rtol,
atol=10e-3)

def test_component_not_in_model(self):
with pytest.raises(ValueError):
self.model.fit_component(self.g)
self.model.fit_component(self.g, fitter='leastsq')


class TestFitSeveralComponent:
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_fit_component_active_state(self):
g3 = self.g3
g2.active = True
g3.active = False
m.fit_component(g1, signal_range=(4500, 5200), fit_independent=True)
m.fit_component(g1, signal_range=(4500, 5200), fit_independent=True, fitter='leastsq')
np.testing.assert_allclose(self.gs1.function(axis),
g1.function(axis),
rtol=self.rtol,
Expand All @@ -123,7 +123,7 @@ def test_fit_component_free_state(self):
g3 = self.g3
g2.A.free = False
g2.sigma.free = False
m.fit_component(g1, signal_range=(4500, 5200))
m.fit_component(g1, signal_range=(4500, 5200), fitter='leastsq')
np.testing.assert_allclose(self.gs1.function(axis),
g1.function(axis),
rtol=self.rtol,
Expand All @@ -146,9 +146,9 @@ def test_fit_multiple_component(self):
g1 = self.g1
g2 = self.g2
g3 = self.g3
m.fit_component(g1, signal_range=(4500, 5200))
m.fit_component(g2, signal_range=(1500, 2200))
m.fit_component(g3, signal_range=(5800, 6150))
m.fit_component(g1, signal_range=(4500, 5200), fitter='leastsq')
m.fit_component(g2, signal_range=(1500, 2200), fitter='leastsq')
m.fit_component(g3, signal_range=(5800, 6150), fitter='leastsq')
np.testing.assert_allclose(self.model.signal.data,
m(),
rtol=self.rtol,
Expand All @@ -168,7 +168,7 @@ def setup_method(self, method):
def test_fit_spectrum_image(self):
m = self.model
G = self.G
m.fit_component(G, signal_range=(2,7), only_current=False)
m.fit_component(G, signal_range=(2,7), only_current=False, fitter='leastsq')
m.axes_manager.indices = (0,0)
A = G.A.value
m.axes_manager.indices = (1,1)
Expand Down

0 comments on commit 187da73

Please sign in to comment.