From 5b06c0aaaa201d43631617044f9c126ef9c2755a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20de=20la=20Pe=C3=B1a?= Date: Mon, 9 Oct 2023 18:00:17 +0200 Subject: [PATCH] Fix tests --- exspy/exspy/test/models/test_eelsmodel.py | 2 +- hyperspy/component.py | 2 +- hyperspy/model.py | 2 +- hyperspy/tests/component/test_component.py | 8 +++---- hyperspy/tests/model/test_fit_component.py | 2 +- hyperspy/tests/model/test_linear_model.py | 4 ++-- hyperspy/tests/model/test_model.py | 28 +++++++++++----------- hyperspy/tests/model/test_model2d.py | 2 +- hyperspy/tests/signals/test_binned.py | 4 ++-- 9 files changed, 27 insertions(+), 27 deletions(-) diff --git a/exspy/exspy/test/models/test_eelsmodel.py b/exspy/exspy/test/models/test_eelsmodel.py index 4c4bb4536d..4ecb019fc6 100644 --- a/exspy/exspy/test/models/test_eelsmodel.py +++ b/exspy/exspy/test/models/test_eelsmodel.py @@ -605,4 +605,4 @@ def test_model_store_restore(self): m = self.m m.store() mc = m.signal.models.a.restore() - assert np.array_equal(m(), mc()) + assert np.array_equal(m._get_current_data(), mc._get_current_data()) diff --git a/hyperspy/component.py b/hyperspy/component.py index 1024e75550..68839c5f53 100644 --- a/hyperspy/component.py +++ b/hyperspy/component.py @@ -1054,7 +1054,7 @@ def _component2plot(self, axes_manager, out_of_range2nans=True): old_axes_manager = self.model.axes_manager self.model.axes_manager = axes_manager self.fetch_stored_values() - s = self.model.__call__(component_list=[self]) + s = self.model._get_current_data(component_list=[self]) if not self.active: s.fill(np.nan) if old_axes_manager is not None: diff --git a/hyperspy/model.py b/hyperspy/model.py index f20d0a7719..0af5a4ec4d 100644 --- a/hyperspy/model.py +++ b/hyperspy/model.py @@ -1212,7 +1212,7 @@ def _get_variance(self, only_current=True): def _calculate_chisq(self): variance = self._get_variance() - d = self(onlyactive=True, binned=self._binned).ravel() - self.signal._get_current_data(as_numpy=True)[ + d = self._get_current_data(onlyactive=True, binned=self._binned).ravel() - self.signal._get_current_data(as_numpy=True)[ np.where(self._channel_switches)] d *= d / (1. * variance) # d = difference^2 / variance. self.chisq.data[self.signal.axes_manager.indices[::-1]] = d.sum() diff --git a/hyperspy/tests/component/test_component.py b/hyperspy/tests/component/test_component.py index eb747ceaed..9a8913c4ec 100644 --- a/hyperspy/tests/component/test_component.py +++ b/hyperspy/tests/component/test_component.py @@ -230,7 +230,7 @@ def setup_method(self, method): self.c = Component(["one", "two"]) c = self.c c.model = mock.MagicMock() - c.model.__call__ = mock.MagicMock() + c.model._get_current_data = mock.MagicMock() c.model._channel_switches = np.array([True, False, True]) c.model.axis.axis = np.array([0.1, 0.2, 0.3]) c.function = mock.MagicMock() @@ -249,7 +249,7 @@ def test_plotting_active_component_notbinned(self): c = self.c c.active = True c.model.signal.axes_manager[-1].is_binned = False - c.model.__call__.return_value = np.array([1.3]) + c.model._get_current_data.return_value = np.array([1.3]) res = c._component2plot(c.model.axes_manager, out_of_range2nans=False) np.testing.assert_array_equal(res, np.array([1.3, ])) @@ -257,7 +257,7 @@ def test_plotting_active_component_binned(self): c = self.c c.active = True c.model.signal.axes_manager[-1].is_binned = True - c.model.__call__.return_value = np.array([1.3]) + c.model._get_current_data.return_value = np.array([1.3]) res = c._component2plot(c.model.axes_manager, out_of_range2nans=False) np.testing.assert_array_equal(res, np.array([1.3, ])) @@ -265,7 +265,7 @@ def test_plotting_active_component_out_of_range(self): c = self.c c.active = True c.model.signal.axes_manager[-1].is_binned = False - c.model.__call__.return_value = np.array([1.1, 1.3]) + c.model._get_current_data.return_value = np.array([1.1, 1.3]) res = c._component2plot(c.model.axes_manager, out_of_range2nans=True) np.testing.assert_array_equal(res, np.array([1.1, np.nan, 1.3])) diff --git a/hyperspy/tests/model/test_fit_component.py b/hyperspy/tests/model/test_fit_component.py index 7d11c60f44..96d7301404 100644 --- a/hyperspy/tests/model/test_fit_component.py +++ b/hyperspy/tests/model/test_fit_component.py @@ -166,7 +166,7 @@ def test_fit_multiple_component(self): m.fit_component(g2, signal_range=(1500, 2200)) m.fit_component(g3, signal_range=(5800, 6150)) np.testing.assert_allclose(self.model.signal.data, - m(), + m._get_current_data(), rtol=self.rtol, atol=10e-3) diff --git a/hyperspy/tests/model/test_linear_model.py b/hyperspy/tests/model/test_linear_model.py index 1e0474190b..08d7dafd10 100644 --- a/hyperspy/tests/model/test_linear_model.py +++ b/hyperspy/tests/model/test_linear_model.py @@ -520,7 +520,7 @@ def test_without_twins(self): np.testing.assert_allclose(gs[0].A.value, 20) np.testing.assert_allclose(gs[1].A.value, -10) np.testing.assert_allclose(gs[2].A.value, 5) - np.testing.assert_allclose(s.data, m()) + np.testing.assert_allclose(s.data, m._get_current_data()) def test_with_twins(self): gs = self.gs @@ -536,7 +536,7 @@ def test_with_twins(self): np.testing.assert_allclose(gs[0].A.value, 20) np.testing.assert_allclose(gs[1].A.value, -10) np.testing.assert_allclose(gs[2].A.value, 5) - np.testing.assert_allclose(s.data, m()) + np.testing.assert_allclose(s.data, m._get_current_data()) def test_compute_constant_term(): diff --git a/hyperspy/tests/model/test_model.py b/hyperspy/tests/model/test_model.py index 7a67185ea9..89a268eb75 100644 --- a/hyperspy/tests/model/test_model.py +++ b/hyperspy/tests/model/test_model.py @@ -99,14 +99,14 @@ def test_call_method_no_convolutions(self): m.convolved = False m[1].active = False - r1 = m() - r2 = m(onlyactive=True) + r1 = m._get_current_data() + r2 = m._get_current_data(onlyactive=True) np.testing.assert_allclose(m[0].function(0) * 2, r1) np.testing.assert_allclose(m[0].function(0), r2) m.convolved = True - r1 = m(non_convolved=True) - r2 = m(non_convolved=True, onlyactive=True) + r1 = m._get_current_data(non_convolved=True) + r2 = m._get_current_data(non_convolved=True, onlyactive=True) np.testing.assert_allclose(m[0].function(0) * 2, r1) np.testing.assert_allclose(m[0].function(0), r2) @@ -124,8 +124,8 @@ def test_call_method_with_convolutions(self): m[2].convolved = False m.convolution_axis = np.array([0.0]) - r1 = m() - r2 = m(onlyactive=True) + r1 = m._get_current_data() + r2 = m._get_current_data(onlyactive=True) np.testing.assert_allclose(m[0].function(0) * 2.3, r1) np.testing.assert_allclose(m[0].function(0) * 1.3, r2) @@ -135,7 +135,7 @@ def test_call_method_binned(self): m.remove(1) m.signal.axes_manager[-1].is_binned = True m.signal.axes_manager[-1].scale = 0.3 - r1 = m() + r1 = m._get_current_data() np.testing.assert_allclose(m[0].function(0) * 0.3, r1) @@ -143,8 +143,8 @@ class TestModelPlotCall: def setup_method(self, method): s = hs.signals.Signal1D(np.empty(1)) m = s.create_model() - m.__call__ = mock.MagicMock() - m.__call__.return_value = np.array([0.5, 0.25]) + m._get_current_data = mock.MagicMock() + m._get_current_data.return_value = np.array([0.5, 0.25]) m.axis = mock.MagicMock() m.fetch_stored_values = mock.MagicMock() m._channel_switches = np.array([0, 1, 1, 0, 0], dtype=bool) @@ -157,16 +157,16 @@ def test_model2plot_own_am(self): np.testing.assert_array_equal( res, np.array([np.nan, 0.5, 0.25, np.nan, np.nan]) ) - assert m.__call__.called - assert m.__call__.call_args[1] == {"non_convolved": False, "onlyactive": True} + assert m._get_current_data.called + assert m._get_current_data.call_args[1] == {"non_convolved": False, "onlyactive": True} assert not m.fetch_stored_values.called def test_model2plot_other_am(self): m = self.model res = m._model2plot(m.axes_manager.deepcopy(), out_of_range2nans=False) np.testing.assert_array_equal(res, np.array([0.5, 0.25])) - assert m.__call__.called - assert m.__call__.call_args[1] == {"non_convolved": False, "onlyactive": True} + assert m._get_current_data.called + assert m._get_current_data.call_args[1] == {"non_convolved": False, "onlyactive": True} assert 2 == m.fetch_stored_values.call_count @@ -627,7 +627,7 @@ def test_binned_uniform(self, binned, uniform): m.signal.axes_manager[-1].scale = 0.3 if uniform: m.signal.axes_manager[-1].convert_to_non_uniform_axis() - np.testing.assert_allclose(m[0].function(0) * 0.3, m()) + np.testing.assert_allclose(m[0].function(0) * 0.3, m._get_current_data()) self.m.print_current_values() diff --git a/hyperspy/tests/model/test_model2d.py b/hyperspy/tests/model/test_model2d.py index 816675691b..c8c4aa9eb0 100644 --- a/hyperspy/tests/model/test_model2d.py +++ b/hyperspy/tests/model/test_model2d.py @@ -104,7 +104,7 @@ def test_fit_no_odr_error(self): def test_call(self): with pytest.raises(ValueError): - self.m(component_list=0) + self.m._get_current_data(component_list=0) def test_Model2D_NotImplementedError_fitting(): diff --git a/hyperspy/tests/signals/test_binned.py b/hyperspy/tests/signals/test_binned.py index b383134383..d8d64b5c10 100644 --- a/hyperspy/tests/signals/test_binned.py +++ b/hyperspy/tests/signals/test_binned.py @@ -48,8 +48,8 @@ def setup_method(self, method): def test_unbinned(self): self.m.signal.axes_manager[-1].is_binned = False - assert self.m() == 1 + assert self.m._get_current_data() == 1 def test_binned(self): self.m.signal.axes_manager[-1].is_binned = True - assert self.m() == 0.1 + assert self.m._get_current_data() == 0.1