Skip to content

Commit

Permalink
Merge pull request #76 from ericpre/fix_active_xray_lines
Browse files Browse the repository at this point in the history
`fit_background` fixes
  • Loading branch information
jlaehne committed Sep 11, 2024
2 parents 26cfc27 + 88949ab commit 800b740
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 31 deletions.
62 changes: 31 additions & 31 deletions exspy/models/edsmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ def spectrum(self, value):
"but an object of type %s was provided" % str(type(value))
)

@property
def _active_xray_lines(self):
return [xray_line for xray_line in self.xray_lines if xray_line.active]

def add_family_lines(self, xray_lines="from_elements"):
"""Create the Xray-lines instances and configure them appropiately
Expand Down Expand Up @@ -320,13 +316,15 @@ def fix_background(self):

def enable_xray_lines(self):
"""Enable the X-ray lines components."""
for component in self.xray_lines:
component.active = True
for component in self:
if not component.isbackground:
component.active = True

def disable_xray_lines(self):
"""Disable the X-ray lines components."""
for component in self._active_xray_lines:
component.active = False
for component in self:
if not component.isbackground:
component.active = False

def _make_position_adjuster(self, component, fix_it, show_label):
# Override to ensure formatting of labels of xray lines
Expand Down Expand Up @@ -377,25 +375,31 @@ def fit_background(
if start_energy is None:
start_energy = self.start_energy

signal_range_mask = np.copy(self._channel_switches)

# disactivate line
self.free_background()
with stash_active_state(self):
self.disable_xray_lines()
self.set_signal_range(start_energy, end_energy)
for component in self:
if component.isbackground is False:
self.remove_signal_range(
component.centre.value
- windows_sigma[0] * component.sigma.value,
component.centre.value
+ windows_sigma[1] * component.sigma.value,
)
if kind == "single":
self.fit(**kwargs)
if kind == "multi":
self.multifit(**kwargs)
self.reset_signal_range()
self.fix_background()
with self.suspend_update():
self.free_background()
with stash_active_state(self):
self.disable_xray_lines()
self.set_signal_range(start_energy, end_energy)
for component in self:
if component.isbackground is False:
self.remove_signal_range(
component.centre.value
- windows_sigma[0] * component.sigma.value,
component.centre.value
+ windows_sigma[1] * component.sigma.value,
)
if kind == "single":
self.fit(**kwargs)
if kind == "multi":
self.multifit(**kwargs)

# Reset previous signal range
self.set_signal_range_from_mask(signal_range_mask)
self.fix_background()

def _twin_xray_lines_width(self, xray_lines):
"""
Expand Down Expand Up @@ -910,12 +914,8 @@ def get_lines_intensity(
data_res = data_res[0]
img = self.signal.isig[0:1].integrate1D(-1)
img.data = data_res
img.metadata.General.title = "Intensity of %s at %.2f %s from %s" % (
xray_line,
line_energy,
self.signal.axes_manager.signal_axes[0].units,
self.signal.metadata.General.title,
)
units = self.signal.axes_manager.signal_axes[0].units
img.metadata.General.title = f"{xray_line} at {line_energy:.2f} {units}"
img = img.transpose(signal_axes=[])
if plot_result and img.axes_manager.signal_dimension == 0:
print(
Expand Down
12 changes: 12 additions & 0 deletions exspy/tests/models/test_edsmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ def test_temmodel_store(self):
self.s.set_signal_type("EDS_TEM")
self._check_model_store()

def test_disable_xray_lines(self):
m = s.create_model()
for c in m:
assert c.active
m.disable_xray_lines()
assert m[0].active
for c in m[1:]:
assert not c.active
m.enable_xray_lines()
for c in m:
assert c.active

def test_calibrate_energy_resolution(self):
s = self.s
m = s.create_model()
Expand Down
16 changes: 16 additions & 0 deletions exspy/tests/models/test_linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ def test_fit_fixed_twinned_components_and_std(self):
np.testing.assert_allclose(nonlinear_fit.data, lstsq_fit.data)
np.testing.assert_allclose(nonlinear_std, linear_std)

def test_fit_background(self):
m2 = self.m2
m2.fit_background(optimizer="lstsq")
np.testing.assert_allclose(m2[0].a0.value, 11045209.18)

def test_fit_background_reset_signal_range(self):
m2 = self.m2
m2.remove_signal_range(7.0, 8.0)
np.testing.assert_allclose(m2._channel_switches[:200], True)
np.testing.assert_allclose(m2._channel_switches[301:], True)
np.testing.assert_allclose(m2._channel_switches[200:301], False)
m2.fit_background(optimizer="lstsq")
np.testing.assert_allclose(m2._channel_switches[:200], True)
np.testing.assert_allclose(m2._channel_switches[301:], True)
np.testing.assert_allclose(m2._channel_switches[200:301], False)


class TestWarningSlowMultifit:
def setup_method(self, method):
Expand Down
5 changes: 5 additions & 0 deletions upcoming_changes/76.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:meth:`exspy.models.EDSModel.fit_background` fixes:

- fix error when using linear fitting,
- fix resetting signal range after fitting background,
- suspend plot when fitting background.

0 comments on commit 800b740

Please sign in to comment.