def onExportCSV(self, evt=None): group_ids = self.controller.filelist.GetCheckedStrings() savegroups = [] groupnames = [] for checked in group_ids: groupname = self.controller.file_groups[str(checked)] dgroup = self.controller.get_group(groupname) savegroups.append(dgroup) groupnames.append(groupname) if len(savegroups) < 1: Popup(self, "No files selected to export to CSV", "No files selected") return deffile = "%s_%i.csv" % (groupname, len(groupnames)) wcards = 'CSV Files (*.csv)|*.cvs|All files (*.*)|*.*' outfile = FileSave(self, 'Export Selected Groups to CSV File', default_file=deffile, wildcard=wcards) if outfile is None: return groups2csv(savegroups, outfile, x='energy', y='norm', _larch=self.larch)
def onSaveGroupMultiFits(self, evt=None): "Save Data and Best N Fits for the current group" dgroup = self.datagroup nfits = int(self.wids['plot_nchoice'].GetStringSelection()) results = dgroup.lcf_result[:nfits] nresults = len(results) deffile = "%s_LinearFits%i.dat" % (dgroup.filename, nresults) wcards = 'Data Files (*.dat)|*.dat|All files (*.*)|*.*' path = FileSave(self, 'Save Best N Fits', default_file=deffile, wildcard=wcards) if path is None: return form = self.form header = [ 'Larch Linear Arrays for %2.2d best results' % (nresults), 'Dataset filename: %s ' % dgroup.filename, 'Larch group: %s ' % dgroup.groupname, 'Array name: %s' % form['arrayname'], 'Energy fit range: [%f, %f]' % (form['elo'], form['ehi']) ] label = [' energy ', ' data '] label.extend([' fit_%2.2d ' % i for i in range(nresults)]) label = ' '.join(label) out = [results[0].xdata, results[0].ydata] for i, res in enumerate(results): out.append(results[i].yfit) _larch = self.parent.controller.larch write_ascii(path, header=header, label=label, _larch=_larch, *out)
def onExportFitResult(self, event=None): dgroup = self.datagroup deffile = dgroup.filename.replace('.', '_') + '.xdi' wcards = 'All files (*.*)|*.*' outfile = FileSave(self, 'Export Fit Result', default_file=deffile) result = self.get_fitresult() if outfile is not None: i1, i2 = get_xlims(dgroup.xdat, result.user_options['emin'], result.user_options['emax']) x = dgroup.xdat[i1:i2] y = dgroup.ydat[i1:i2] yerr = None if hasattr(dgroup, 'yerr'): yerr = 1.0*dgroup.yerr if not isinstance(yerr, np.ndarray): yerr = yerr * np.ones(len(y)) else: yerr = yerr[i1:i2] export_modelresult(result, filename=outfile, datafile=dgroup.filename, ydata=y, yerr=yerr, x=x)
def onSaveFitResult(self, event=None): deffile = self.datagroup.filename.replace('.', '_') + 'peak.modl' sfile = FileSave(self, 'Save Fit Model', default_file=deffile, wildcard=ModelWcards) if sfile is not None: result = self.get_fitresult() save_modelresult(result, sfile)
def onSaveLarchHistory(self, evt=None): wildcard = 'Larch file (*.lar)|*.lar|All files (*.*)|*.*' path = FileSave(self, message='Save Session History as Larch Script', wildcard=wildcard, default_file='xas_viewer_history.lar') if path is not None: self.larch._larch.input.history.save(path, session_only=True) self.write_message("Wrote history %s" % path, 0)
def onSaveGroupStats(self, evt=None): "Save Statistics and Weights for Best N Fits for the current group" dgroup = self.datagroup nfits = int(self.wids['plot_nchoice'].GetStringSelection()) results = dgroup.lcf_result[:nfits] nresults = len(results) deffile = "%s_LinearStats%i.dat" % (dgroup.filename, nresults) wcards = 'Data Files (*.dat)|*.dat|All files (*.*)|*.*' path = FileSave(self, 'Save Statistics and Weights for Best N Fits', default_file=deffile, wildcard=wcards) if path is None: return form = self.form header = [ 'Larch Linear Fit Statistics for %2.2d best results' % (nresults), 'Dataset filename: %s ' % dgroup.filename, 'Larch group: %s ' % dgroup.groupname, 'Array name: %s' % form['arrayname'], 'Energy fit range: [%f, %f]' % (form['elo'], form['ehi']), 'N_Data: %d' % len(results[0].xdata) ] label = [ 'fit #', 'n_varys', 'n_eval', 'chi2', 'chi2_reduced', 'akaike_info', 'bayesian_info' ] label.extend(form['comp_names']) label.append('Total') for i in range(len(label)): if len(label[i]) < 13: label[i] = (" %s " % label[i])[:13] label = ' '.join(label) out = [] for i, res in enumerate(results): dat = [(i + 1)] for attr in ('nvarys', 'nfev', 'chisqr', 'redchi', 'aic', 'bic'): dat.append(getattr(res.result, attr)) for cname in form['comp_names'] + ['total']: val = 0.0 if cname in res.params: val = res.params[cname].value dat.append(val) out.append(dat) out = np.array(out).transpose() _larch = self.parent.controller.larch write_ascii(path, header=header, label=label, _larch=_larch, *out)
def onSaveFitResult(self, event=None): dgroup = self.controller.get_group() deffile = dgroup.filename.replace('.', '_') + '.modl' outfile = FileSave(self, 'Save Fit Result', default_file=deffile, wildcard=ModelWcards) if outfile is not None: try: self.save_fit_result(dgroup.fit_history[-1], outfile) except IOError: print('could not write %s' % outfile)
def onSaveStats(self, evt=None): "Save Statistics and Weights for Best N Fits" nfits = int(self.wids['plot_nchoice'].GetStringSelection()) deffile = "%s_LinearStats%i.dat" % (dgroup.filename, nfits) wcards = 'Data Files (*.dat)|*.dat|All files (*.*)|*.*' path = FileSave(self, 'Save Statistics and Weights for Best N Fits', default_file=deffile, wildcard=wcards) if path is None: return print("save stats %d ", nfits)
def onSaveAllStats(self, evt=None): "Save All Statistics and Weights " deffile = "LinearFitStats.csv" wcards = 'CVS Files (*.csv)|*.csv|All files (*.*)|*.*' path = FileSave(self, 'Save Statistics Report', default_file=deffile, wildcard=wcards) if path is None: return form = self.form out = [ '# Larch Linear Fit Statistics Report (best results) %s' % time.ctime(), '# Array name: %s' % form['arrayname'], '# Energy fit range: [%f, %f]' % (form['elo'], form['ehi']) ] label = [('Data Set' + ' ' * 25)[:25], 'n_varys', 'chi-square', 'chi-square_red', 'akaike_info', 'bayesian_info'] label.extend(form['comp_names']) label.append('Total') for i in range(len(label)): if len(label[i]) < 12: label[i] = (" %s " % label[i])[:12] label = ', '.join(label) out.append('# %s' % label) for name, dgroup in self.datasets.items(): res = dgroup.lcf_result[0] label = dgroup.filename if len(label) < 25: label = (label + ' ' * 25)[:25] dat = [label] for attr in ('nvarys', 'chisqr', 'redchi', 'aic', 'bic'): dat.append(gformat(getattr(res.result, attr), 12)) for cname in form['comp_names'] + ['total']: val = 0 if cname in res.params: val = res.params[cname].value dat.append(gformat(val, 12)) out.append(', '.join(dat)) out.append('') with open(path, 'w') as fh: fh.write('\n'.join(out))
def save_athena_project(self, filename, grouplist, prompt=True): if len(grouplist) < 1: return savegroups = [self.controller.get_group(gname) for gname in grouplist] deffile = "%s_%i.prj" % (filename, len(grouplist)) wcards = 'Athena Projects (*.prj)|*.prj|All files (*.*)|*.*' outfile = FileSave(self, 'Save Groups to Athena Project File', default_file=deffile, wildcard=wcards) if outfile is None: return aprj = AthenaProject(filename=outfile, _larch=self.larch) for label, grp in zip(grouplist, savegroups): aprj.add_group(grp) aprj.save(use_gzip=True) self.write_message("Saved project file %s" % (outfile))
def onSaveFit(self, evt=None): "Save Fit and Compoents to Data File" print("save fit and components") nfit = self.current_fit dgroup = self.datagroup nfits = int(self.wids['plot_nchoice'].GetStringSelection()) deffile = "%s_LinearFit%i.dat" % (dgroup.filename, nfit + 1) wcards = 'Data Files (*.dat)|*.dat|All files (*.*)|*.*' path = FileSave(self, 'Save Fit and Components to File', default_file=deffile, wildcard=wcards) if path is None: return form = self.form label = [' energy ', ' data ', ' best_fit '] result = dgroup.lcf_result[nfit] header = [ 'Fit #%2.2d' % (nfit + 1), 'Fit arrayname: %s' % form['arrayname'], 'E0: %f ' % form['e0'], 'Energy fit range: [%f, %f]' % (form['elo'], form['ehi']), 'Components: ' ] for key, val in result.weights.items(): header.append(' %s: %f' % (key, val)) report = fit_report(result.result).split('\n') header.extend(report) out = [result.xdata, result.ydata, result.yfit] for compname, compdata in result.ycomps.items(): label.append(' %s' % (compname + ' ' * (max(1, 15 - len(compname))))) out.append(compdata) label = ' '.join(label) _larch = self.parent.controller.larch write_ascii(path, header=header, label=label, _larch=_larch, *out)
def onExportFitResult(self, event=None): dgroup = self.controller.get_group() deffile = dgroup.filename.replace('.', '_') + '_result.xdi' wcards = 'All files (*.*)|*.*' outfile = FileSave(self, 'Export Fit Result', default_file=deffile, wildcard=wcards) if outfile is not None: i1, i2 = self.get_xranges(dgroup.xdat) x = dgroup.xdat[i1:i2] y = dgroup.ydat[i1:i2] yerr = None if hasattr(dgroup, 'yerr'): yerr = 1.0*dgroup.yerr if not isinstance(yerr, np.ndarray): yerr = yerr * np.ones(len(y)) else: yerr = yerr[i1:i2] export_modelresult(dgroup.fit_history[-1], filename=outfile, datafile=dgroup.filename, ydata=y, yerr=yerr, x=x)