Skip to content

Commit

Permalink
more tweaks to XRF fitting
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Jul 12, 2024
1 parent 3844bf0 commit bacce3a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions larch/wxlib/xrfdisplay_fitpeaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def VarChoice(p, default=0, size=(90, -1)):


DEF_CONFIG = { "mca_name": "", "escape_use": True, "escape_amp": 0.25,
"pileup_use": True, "pileup_amp": 0.1, "escape_amp_vary":
"pileup_use": True, "pileup_amp": 0.2, "escape_amp_vary":
True, "pileup_amp_vary": True, "cal_slope": 0.01,
"cal_offset": 0, "cal_vary": True, "det_mat": "Si", "det_thk":
0.4, "det_noise_vary": True,
Expand Down Expand Up @@ -228,12 +228,13 @@ def __init__(self, parent, size=(750, 850)):
action=self.onShowModel)
self.wids['btn_fit'] = Button(pan, 'Fit Model', size=(150, -1),
action=self.onFitModel)
self.wids['fit_message'] = SimpleText(pan, ' ', size=(300, -1), style=LEFT)

pan.AddText(" XRF Spectrum: ", colour='#880000')
pan.Add(self.wids['mca_name'], dcol=3)
pan.Add(self.wids['btn_calc'], newrow=True)
pan.Add(self.wids['btn_fit'])

pan.Add(self.wids['fit_message'])
self.panels = {}
self.panels['Beam & Detector'] = self.beamdet_page
self.panels['Filters & Matrix'] = self.materials_page
Expand Down Expand Up @@ -371,11 +372,11 @@ def beamdet_page(self, **kws):
cal_offset = getattr(mca, 'offset', 0)
cal_slope = getattr(mca, 'slope', 0.010)
det_noise = getattr(mca, 'det_noise', 0.035)
escape_amp = getattr(mca, 'escape_amp', 0.10)
escape_amp = getattr(mca, 'escape_amp', 0.25)

if not hasattr(self.mca, 'pileup_scale'):
self.mca.predict_pileup()
pileup_amp = max(0.001, getattr(mca, 'pileup_scale', 0.05))
pileup_amp = max(0.001, getattr(mca, 'pileup_scale', 0.25))

wids = self.wids
pdet = GridPanel(self, itemstyle=LEFT)
Expand Down Expand Up @@ -1322,11 +1323,13 @@ def plot_model(self, model_spectrum=None, init=False, with_comps=False,
def onShowModel(self, event=None):
self.build_model()
self.plot_model(init=True, with_comps=False)
self.wids['fit_message'].SetLabel("Initial Model Built")

def onFitIteration(self, iter=0, pars=None):
# print("XRF Fit iteration %d" % iter)
# self.wids['fit_message'].SetLabel("Fit iteration %d" % iter)
pass
if iter % 10 == 0:
nvar = len([p for p in pars.values() if p.vary])
print(f"XRF Fit iteration {iter}, {nvar} variables")
# pass


def onFitModel(self, event=None):
Expand All @@ -1352,7 +1355,13 @@ def onFitModel(self, event=None):
dgroup = self._larch.symtable.get_group(self.mcagroup)
self.xrfresults = self._larch.symtable.get_symbol(XRFRESULTS_GROUP)

xrfresult = self.xrfresults[0]
try:
xrfresult = self.xrfresults[0]
self.wids['fit_message'].SetLabel("Fit Complete")
except:
self.wids['fit_message'].SetLabel("fit failed, cannot get result")
return

xrfresult.script = "%s\n%s" % (self.model_script, fit_script)
xrfresult.label = "fit %d" % (len(self.xrfresults))
self.plot_model(init=True, with_comps=False)
Expand Down
2 changes: 1 addition & 1 deletion larch/xrf/xrf_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def __resid(self, params, data, index):
self.fit_iter += 1
model = self.calc_spectrum(self.best_en, params=params)
if callable(self.iter_callback):
self.iter_callback(iter=self.fit_iter, pars=pars)
self.iter_callback(iter=self.fit_iter, pars=params)
return ((data - model) * self.fit_weight)[self.imin:self.imax]

def set_fit_weight(self, energy, counts, emin, emax, ewid=0.050):
Expand Down

0 comments on commit bacce3a

Please sign in to comment.