Well let's see here - Re-acquainting myself with the code it seems that fitSpectrum takes a spectrum with detector and composition information and uses a non-linear algorithm to fit a continuum model and Gaussian peak profiles to the spectrum data.
spec is a spectrum object. The easiest way to create a spectrum object is to read one from disk and then use the "ls()" function to determine its Python alias. A spectrum alias is a variable "s?" where ? is an integer index.
Since the spectrum must be associated with a detector, make sure that you have defined a suitable detector in "File -> Properties" and that it is selected as the "Default Detector" on the left side of the "Spectrum" panel. (This needs to be done before reading in the spectrum.)
Since the spectrum must also be associated with a composition so the fitting algorithm knows which elements to fit, select the spectrum in the "Spectrum List" and use the "Tools -> Assign material..." menu item to specify the approximate composition (guess if you don't know.)
Now simply
1> ls()
Name Spectrum
s1 FeNiCrSS
2> r=fitSpectrum(s1) # Performs the fit assigning the result to r
3> display(r["Brem"]) # Displays the fit continuum spectrum
4> display(r["Char"]) # Displays the fit characteristic portion of the spectrum
5> report(r["Result].toHTML()) # Outputs a result table on the Report tab
This is the Python code behind it all which calls Java code in the EPQ library.
def fitSpectrum(spec, nIter=5, poly=3, fn="mostRecentFit.csv"):
"""fitSpectrum(spec, nIter=5)
Fit spec, an ISpectrumData object, with a series of Gaussian's centered on the characteristic x-ray energies. spec must define a detector and a StandardComposition in it's properties."""
sp = spec.getProperties()
sf = epq.SpectrumFitter8(sp.getDetector(), sp.getCompositionProperty(epq.SpectrumProperties.StandardComposition), spec)
rois = sf.getROIS()
props = sp.getDetector().getCalibration().getProperties()
# If there is an extended range of energies with characteristic peaks,
# we should increase the number of fit parameters.
coeffs = (
props.getNumericWithDefault(epq.SpectrumProperties.EnergyOffset, 0.0),
props.getNumericWithDefault(epq.SpectrumProperties.EnergyScale, 10.0),
props.getNumericWithDefault(epq.SpectrumProperties.EnergyQuadratic, 0.0),
0.0,
0.0)
sf.setEnergyScale(sf.EnergyScaleFunction(coeffs, poly))
# Fit both Fano factor and noise...
sf.setResolution(sf.FanoNoiseWidth(6.0))
# First fit using table weights...
sf.setMultiLineset(sf.buildWeighted(rois))
mrFitsFile = None
try:
results = sf.compute()
elmFits = jio.File(App.getReport().getBasePath(), fn)
mrFitsFile = jio.FileWriter(elmFits, elmFits.exists())
# mrFitsFile.write(results.tabulateResults())
for i in range(0, nIter):
# Refit by adjusting the line weights...
results = sf.recompute(10.0, 0.3)
try:
mrFitsFile.write(results.tabulateResults())
mrFitsFile.write(results.tabulateResults(sp))
mrFitsFile.flush()
except jl.Exception, e1:
e1.printStackTrace()
finally:
if mrFitsFile:
mrFitsFile.close()
result = { }
result["Result"] = results
result["Spectrum"] = spec
result["Fit"] = sf.getBestFit()
result["Char"] = sf.getCharacteristicSpectrum()
result["Brem"] = sf.getBremsstrahlungSpectrum()
return result