Skip to content

Instantly share code, notes, and snippets.

@thomasaarholt
Forked from winston-song/Mn_whitelineratio
Created April 21, 2020 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasaarholt/9a123a23bb1ec96f9e42ad2e41483100 to your computer and use it in GitHub Desktop.
Save thomasaarholt/9a123a23bb1ec96f9e42ad2e41483100 to your computer and use it in GitHub Desktop.
Mn L3/L2 white line ratio using gussian component plus H-S step edge
%matplotlib widget
import hyperspy.api as hs
import matplotlib.pyplot as plt
import numpy as np
ll_sum = hs.load('ll_sum.hspy')
s_sum = hs.load('s_sum.hspy')
s_sum.metadata.Acquisition_instrument.TEM.beam_energy=200
s_sum.metadata.Acquisition_instrument.TEM.convergence_angle=22.5
s_sum.metadata.Acquisition_instrument.TEM.Detector.EELS.collection_angle=37.9
s = s_sum.isig[600.:700.]#.remove_background(signal_range=(600.,630.), fast=False)
ll = ll_sum
m = s.create_model(ll = ll, GOS="Hartree-Slater", auto_add_edges=False) # background is automatically added here from auto_background=True.
m.fit_component(m["PowerLaw"], bounded=True, signal_range=[610.,630.], fit_independent=True, only_current=True)
m.assign_current_values_to_all(components_list = [m["PowerLaw"]]) # This gives good (hopefully) initial values to all pixels
# Create and set the 4 components
L3 = hs.model.components1D.EELSCLEdge("Mn_L3", GOS="Hartree-Slater")
L2 = hs.model.components1D.EELSCLEdge("Mn_L2", GOS="Hartree-Slater")
L3_white = hs.model.components1D.Gaussian()
L3_white.name = "Mn_L3 line"
L3_white.centre.bmin = 640.0
L3_white.centre.bmax = 645.0
L3_white.centre.value = (L3_white.centre.bmax + L3_white.centre.bmin) / 2 # Initial value of the centre. Must be set to initialise the twinning below.
L3_white.A.bmin = 0
L3_white.sigma.bmax=3
L2_white = hs.model.components1D.Gaussian()
L2_white.name = "Mn_L2 line"
L2_white.centre.bmin = 650.0
L2_white.centre.bmax = 655.0
L2_white.centre.value = (L2_white.centre.bmax + L2_white.centre.bmin) / 2
L2_white.A.bmin = 0
L2_white.sigma.bmax=3
L3.onset_energy.twin = L3_white.centre
L2.onset_energy.twin = L2_white.centre
# Add all four to the model
m.extend([L3_white, L3, L2_white, L2])
# The following sortof emulates smart_fit, but it is more appropriate than smart_fit,
# because smart_fit doesn't know that it should fit the gaussians between L3 and L2
# We fit "from the left", starting with the lowest energy
m.fit_component(L3_white,fitter="leastsq", signal_range=[L3_white.centre.bmin-3.0, L3_white.centre.bmax+3.0], bounded=True,) # get good values
m.assign_current_values_to_all(components_list = [L3_white]) # apply those good values to all pixels
m.fit_component(L3_white,fitter="leastsq", signal_range=[L3_white.centre.bmin-3.0, L3_white.centre.bmax+3.0], bounded=True, only_current=False)
m.fit_component(L3, bounded=True, signal_range=[636.,646.])
m.assign_current_values_to_all(components_list = [L3])
m.fit_component(L3, bounded=True, signal_range=[636.,646.], only_current=False)
m.fit_component(L2_white, signal_range=[L2_white.centre.bmin-3.0, L2_white.centre.bmax+3.0], bounded=True,)
m.assign_current_values_to_all(components_list = [L2_white])
m.fit_component(L2_white, signal_range=[L2_white.centre.bmin-3.0, L2_white.centre.bmax+3.0], bounded=True, only_current=False)
m.fit_component(L2, bounded=True, signal_range=[649., 659.])
m.assign_current_values_to_all(components_list = [L2])
m.fit_component(L2, bounded=True, signal_range=[649., 659.], only_current=False)
# Repeat this for each component
m.fit_component(L3, bounded=True, signal_range=[636.,646.], only_current=False)
m.fit_component(L3_white, signal_range=[L3_white.centre.bmin-3.0, L3_white.centre.bmax+3.0], bounded=True, only_current=False)
m.fit_component(L2, bounded=True, signal_range=[649., 659.], only_current=False)
m.fit_component(L2_white, signal_range=[L2_white.centre.bmin-3.0, L2_white.centre.bmax+3.0], bounded=True, only_current=False)
m.multifit(bounded=True) # optional final pass may improve fit by letting all components adjust to each other.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment