def onFitPeak(self, evt=None): gname = self.groupname dtext = [] model = self.fit_model.GetStringSelection().lower() dtext.append('Fit Model: %s' % model) bkg = self.fit_bkg.GetStringSelection() if bkg == 'None': bkg = None if bkg is None: dtext.append('No Background') else: dtext.append('Background: %s' % bkg) step = self.fit_step.GetStringSelection().lower() if model in ('step', 'rectangle'): dtext.append('Step form: %s' % step) try: lgroup = getattr(self.larch.symtable, gname) x = lgroup._xdat y = lgroup._ydat except AttributeError: self.write_message('need data to fit!') return if step.startswith('error'): step = 'erf' elif step.startswith('arctan'): step = 'atan' pgroup = fit_peak(x, y, model, background=bkg, step=step, _larch=self.larch) dtext = '\n'.join(dtext) dtext = '%s\n%s\n' % ( dtext, fit_report( pgroup.params, min_correl=0.25, _larch=self.larch)) self.fit_report.SetEditable(True) self.fit_report.SetValue(dtext) self.fit_report.SetEditable(False) lgroup.plot_yarrays = [(lgroup._ydat, PLOTOPTS_1, lgroup.plot_ylabel)] if bkg is None: lgroup._fit = pgroup.fit[:] lgroup.plot_yarrays.append((lgroup._fit, PLOTOPTS_2, 'fit')) else: lgroup._fit = pgroup.fit[:] lgroup._fit_bgr = pgroup.bkg[:] lgroup.plot_yarrays.append((lgroup._fit, PLOTOPTS_2, 'fit')) lgroup.plot_yarrays.append( (lgroup._fit_bgr, PLOTOPTS_2, 'background')) self.plot_group(gname, new=True)
def onFitPeak(self, evt=None): gname = self.groupname dtext = [] model = self.fit_model.GetStringSelection().lower() dtext.append('Fit Model: %s' % model) bkg = self.fit_bkg.GetStringSelection() if bkg == 'None': bkg = None if bkg is None: dtext.append('No Background') else: dtext.append('Background: %s' % bkg) step = self.fit_step.GetStringSelection().lower() if model in ('step', 'rectangle'): dtext.append('Step form: %s' % step) try: lgroup = getattr(self.larch.symtable, gname) x = lgroup._xdat_ y = lgroup._ydat_ except AttributeError: self.write_message('need data to fit!') return if step.startswith('error'): step = 'erf' elif step.startswith('arctan'): step = 'atan' pgroup = fit_peak(x, y, model, background=bkg, step=step, _larch=self.larch) dtext = '\n'.join(dtext) dtext = '%s\n%s\n' % (dtext, fit_report(pgroup.params, min_correl=0.25, _larch=self.larch)) self.fit_report.SetEditable(True) self.fit_report.SetValue(dtext) self.fit_report.SetEditable(False) popts1 = dict(style='solid', linewidth=3, marker='None', markersize=4) popts2 = dict(style='short dashed', linewidth=2, marker='None', markersize=4) lgroup.plot_yarrays = [(lgroup._ydat_, popts1, lgroup.plot_ylabel)] if bkg is None: lgroup._fit = pgroup.fit[:] lgroup.plot_yarrays.append((lgroup._fit, popts2, 'fit')) else: lgroup._fit = pgroup.fit[:] lgroup._fit_bgr = pgroup.bkg[:] lgroup.plot_yarrays.append((lgroup._fit, popts2, 'fit')) lgroup.plot_yarrays.append((lgroup._fit_bgr, popts2, 'background')) self.onPlot()
def xrf_calib_fitrois(mca, _larch=None): """initial calibration step for MCA: find energy locations for all ROIs """ if not isLarchMCAGroup(mca): print('Not a valid MCA') return energy = 1.0 * mca.energy chans = 1.0 * np.arange(len(energy)) counts = mca.counts bgr = getattr(mca, 'bgr', None) if bgr is not None: counts = counts - bgr calib = OrderedDict() for roi in mca.rois: words = roi.name.split() elem = words[0].title() family = 'ka' if len(words) > 1: family = words[1] try: eknown = xray_line(elem, family, _larch=_larch)[0] / 1000.0 except: continue llim = max(0, roi.left - roi.bgr_width) hlim = min(len(chans) - 1, roi.right + roi.bgr_width) fit = fit_peak(chans[llim:hlim], counts[llim:hlim], 'Gaussian', background='constant', _larch=_larch) ccen = fit.params.center.value ecen = ccen * mca.slope + mca.offset fwhm = 2.354820 * fit.params.sigma.value * mca.slope calib[roi.name] = (eknown, ecen, fwhm, ccen, fit) mca.init_calib = calib
def xrf_calib_fitrois(mca, _larch=None): """initial calibration step for MCA: find energy locations for all ROIs """ if not isLarchMCAGroup(mca): print( 'Not a valid MCA') return energy = 1.0*mca.energy chans = 1.0*np.arange(len(energy)) counts = mca.counts bgr = getattr(mca, 'bgr', None) if bgr is not None: counts = counts - bgr calib = OrderedDict() for roi in mca.rois: words = roi.name.split() elem = words[0].title() family = 'ka' if len(words) > 1: family = words[1] try: eknown = xray_line(elem, family, _larch=_larch)[0]/1000.0 except: continue llim = max(0, roi.left - roi.bgr_width) hlim = min(len(chans)-1, roi.right + roi.bgr_width) fit = fit_peak(chans[llim:hlim], counts[llim:hlim], 'Gaussian', background='constant', _larch=_larch) ccen = fit.params['center'].value ecen = ccen * mca.slope + mca.offset fwhm = 2.354820 * fit.params['sigma'].value * mca.slope calib[roi.name] = (eknown, ecen, fwhm, ccen, fit) mca.init_calib = calib