Skip to content

Commit

Permalink
Add test and example for plot_residual
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpre committed Aug 26, 2023
1 parent 62684b8 commit 18a019a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
36 changes: 36 additions & 0 deletions examples/model_fitting/plot_residual.py
@@ -0,0 +1,36 @@
"""
Plot Residual
=============
Fit an affine function and plot the residual.
"""

import numpy as np
import hyperspy.api as hs

#%%
# Create a signal:
data = np.arange(1000, dtype=np.int64).reshape((10, 100))
s = hs.signals.Signal1D(data)

#%%
# Add noise:
s.add_poissonian_noise(random_state=0)

#%%
# Create model:
m = s.create_model()
line = hs.model.components1D.Expression("a * x + b", name="Affine")
m.append(line)

#%%
# Fit for all navigation positions:
m.multifit()

#%%
# Plot the fitted model with residual:
m.plot(plot_residual=True)

#%%
# sphinx_gallery_thumbnail_number = 2
5 changes: 4 additions & 1 deletion hyperspy/tests/drawing/test_plot_model1d.py
Expand Up @@ -29,7 +29,7 @@

class TestModelPlot:
def setup_method(self, method):
s = Signal1D(np.arange(1000).reshape((10, 100)))
s = Signal1D(np.arange(1000, dtype=np.int64).reshape((10, 100)))
s.add_poissonian_noise(random_state=0)
m = s.create_model()
line = Expression("a * x", name="line", a=1)
Expand Down Expand Up @@ -64,3 +64,6 @@ def test_default_navigator_plot(self):
def test_no_navigator(self):
self.m.plot(navigator=None)
assert self.m.signal._plot.navigator_plot is None

def test_plot_residual(self):
self.m.plot(plot_residual=True)
1 change: 0 additions & 1 deletion hyperspy/tests/model/test_model.py
Expand Up @@ -23,7 +23,6 @@

import hyperspy.api as hs
from hyperspy.decorators import lazifyTestClass
from hyperspy.misc.test_utils import ignore_warning
from hyperspy.misc.utils import slugify


Expand Down

0 comments on commit 18a019a

Please sign in to comment.