Skip to content

Instantly share code, notes, and snippets.

@thomasaarholt
Created January 22, 2018 12:40
Show Gist options
  • Save thomasaarholt/ac7763f03f319a48109c3dd9a96a154d to your computer and use it in GitHub Desktop.
Save thomasaarholt/ac7763f03f319a48109c3dd9a96a154d to your computer and use it in GitHub Desktop.
Trick for saving individual components from a model fro plotting
# See below for use
def get_individual_model_components(m):
"""
Exports the signals from the individual components present in the model
Returns a list of signals, from the current position in the model.
"""
def get_A(component):
return component.A.value
def set_A(component, value):
component.A.value = value
def set_A_zero(component):
if "Gaussian" in str(type(component)):
component.A.value = 0
elif "EELS" in str(type(component)):
component.intensity.value = 0
else:
component.A.value = 0
def set_all_A_zero(model):
for component in model:
set_A_zero(component)
m = m.inav[m.axes_manager.coordinates]
original_values = []
#individuals = []
individuals = [m.signal] # Get data
whole = m.as_signal()
whole.metadata.General.title = "Fitting"
individuals.append(whole)
for i in range(len(m)):
original_values.append(get_A(m[i]))
for i in range(len(m)):
set_all_A_zero(m)
set_A(m[i], original_values[i])
m.store_current_values()
component_signal = m.as_signal()
component_signal.metadata.General.title = m[i].name
individuals.append(component_signal)
for i in range(len(m)):
set_A(m[i], original_values[i])
m.store_current_values()
return individuals
# This is how you use it
spectra = get_individual_model_components(m)
titles = [spec.metadata.General.title for spec in spectra]
hs.plot.plot_spectra(spectra, legend=titles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment