Skip to content

Commit 0edf7d2

Browse files
author
Angelus Vincent
committed
Added new Basic Model Fitting Example and updated upcoming_changelod entry
1 parent 1c83d4c commit 0edf7d2

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

examples/data_handling/energy_conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99

1010
# %%
11-
# Load exemplementary data
11+
# Load examplary data
1212
import lumispy as lum
1313
import numpy as np
1414

examples/data_handling/map_function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import numpy as np
1414

1515
# %%
16-
# Loading Exemplementary Data
16+
# Loading examplary Data
1717
# ---------------------------
1818
#
1919
# Choosing the spectrum at the navigation coordinates ``(15, 12)`` to see a spike

examples/data_handling/peak_analysis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""
1111

1212
# %%
13-
# Load exemplementary data
13+
# Load examplary data
1414
import lumispy as lum
1515

1616
s = lum.data.asymmetric_peak_map()

examples/model_fitting/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Model Fitting
2+
=============
3+
4+
Gallery of Examples for model fitting in lumispy/hyperspy.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Basic Model Fitting
3+
====================
4+
In this Example we will give a brief introduction to model fitting in Lumispy/Hyperspy.
5+
"""
6+
7+
# %%
8+
# Load examplary data
9+
import hyperspy.api as hs
10+
import lumispy as lum
11+
12+
s = lum.data.asymmetric_peak_map()
13+
14+
# %%
15+
# First we need to initialize the model:
16+
m = s.create_model()
17+
18+
# %%
19+
# A Hyperspy model can be composed of multiple components (functions).
20+
# To take a look at the components of the model we can use ``m.components``.
21+
m.components
22+
23+
# %%
24+
# Since we dont hav any yet, we need to create some components and add them to the model.
25+
# Since we have an asymmetric peak, we will use a single SkewNormal component.
26+
# For successful Fit we need to pass the center wavelength ``x0=650nm`` [#f1]_.
27+
g1 = hs.model.components1D.SkewNormal(x0=650)
28+
# Alternative way to set the start value of x0:
29+
# g1.x0.value = 650
30+
m.append(g1)
31+
m.components
32+
33+
# %%
34+
# To see the parameters of our components and their values, we can print all parameter values:
35+
m.print_current_values()
36+
37+
# %%
38+
# To apply the fit, we can use the ``fit()`` function. In this case it will only be using the current index for fitting.
39+
# The fit for the other spectra will be the same. If you want to apply the fit to all spectra of the map, you can use ``multifit()`` instead.
40+
m.fit()
41+
42+
# %%
43+
# We can now plot the model together with the data:
44+
m.plot()
45+
46+
# %%
47+
# After we applied the fit, we can again print the fitted parameter at the current index:
48+
m.print_current_values()
49+
50+
# %%
51+
# If we used ``multifit()``, we could aswell return the fitted parameter as Signal objects.
52+
m.multifit()
53+
g1.A.as_signal().plot(cmap="viridis")
54+
55+
# %%
56+
# sphinx_gallery_thumbnail_number = 2
57+
#
58+
# .. [#f1] Note that Hyperspy has a range of built-in functions covering most needs that can be addedd as components to a model. However, it aslo has an intuitive mechanism to define custom functions.

upcoming_changes/253.new.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Indexing, Map Function, Energy Conversion, ROI, Peak Analysis Example for the Gallery of Examples
1+
Indexing, Map Function, Energy Conversion, ROI, Peak Analysis, Basic Model Fitting Example for the Gallery of Examples

0 commit comments

Comments
 (0)