def save_fit(self): """Saves fit results to an NXentry""" self.read_parameters() group = NXprocess() group['data'] = self.data for f in self.functions: group[f.name] = self.get_model(f) parameters = NXparameters() for p in f.parameters: parameters[p.name] = NXfield(p.value, error=p.stderr, initial_value=p.init_value, min=str(p.min), max=str(p.max)) group[f.name].insert(parameters) if self.fit is not None: group['program'] = 'lmfit' group['version'] = lmfit.__version__ group['title'] = 'Fit Results' group['fit'] = self.get_model() fit = NXparameters() fit.nfev = self.fit.result.nfev fit.ier = self.fit.result.ier fit.chisq = self.fit.result.chisqr fit.redchi = self.fit.result.redchi fit.message = self.fit.result.message fit.lmdif_message = self.fit.result.lmdif_message group['statistics'] = fit group.note = NXnote( self.fit.result.message, ('%s\n' % self.fit.result.lmdif_message + 'scipy.optimize.leastsq error value = %s\n' % self.fit.result.ier + 'Chi^2 = %s\n' % self.fit.result.chisqr + 'Reduced Chi^2 = %s\n' % self.fit.result.redchi + 'No. of Function Evaluations = %s\n' % self.fit.result.nfev + 'No. of Variables = %s\n' % self.fit.result.nvarys + 'No. of Data Points = %s\n' % self.fit.result.ndata + 'No. of Degrees of Freedom = %s\n' % self.fit.result.nfree + '%s' % self.fit.fit_report())) else: group['title'] = 'Fit Model' group['model'] = self.get_model() if 'w0' not in self.tree: self.tree['w0'] = nxload(self.mainwindow.scratch_file, 'rw') ind = [] for key in self.tree['w0']: try: if key.startswith('f'): ind.append(int(key[1:])) except ValueError: pass if not ind: ind = [0] name = 'f' + str(sorted(ind)[-1] + 1) self.tree['w0'][name] = group
def save_fit(self): """Saves fit results to an NXentry""" self.read_parameters() group = NXprocess() group['data'] = self.data for f in self.functions: group[f.name] = self.get_model(f) parameters = NXparameters() for p in f.parameters: parameters[p.name] = NXfield(p.value, error=p.stderr, initial_value=p.init_value, min=str(p.min), max=str(p.max)) group[f.name].insert(parameters) if self.fit is not None: group['program'] = 'lmfit' group['version'] = lmfit.__version__ group['title'] = 'Fit Results' group['fit'] = self.get_model() fit = NXparameters() fit.nfev = self.fit.result.nfev fit.chisq = self.fit.result.chisqr fit.redchi = self.fit.result.redchi fit.message = self.fit.result.message group['statistics'] = fit group.note = NXnote(self.fit.result.message, ('Chi^2 = %s\n' % self.fit.result.chisqr + 'Reduced Chi^2 = %s\n' % self.fit.result.redchi + 'No. of Function Evaluations = %s\n' % self.fit.result.nfev + 'No. of Variables = %s\n' % self.fit.result.nvarys + 'No. of Data Points = %s\n' % self.fit.result.ndata + 'No. of Degrees of Freedom = %s\n' % self.fit.result.nfree + '%s' % self.fit.fit_report())) else: group['title'] = 'Fit Model' group['model'] = self.get_model() if 'w0' not in self.tree: self.tree['w0'] = nxload(self.mainwindow.scratch_file, 'rw') ind = [] for key in self.tree['w0']: try: if key.startswith('f'): ind.append(int(key[1:])) except ValueError: pass if not ind: ind = [0] name = 'f'+str(sorted(ind)[-1]+1) self.tree['w0'][name] = group
def setup_groups(self): entry = self.configuration_file['entry'] entry['nxreduce'] = NXparameters() entry['instrument'] = NXinstrument() entry['instrument/monochromator'] = NXmonochromator() entry['instrument/goniometer'] = NXgoniometer() entry['instrument/detector'] = NXdetector()
def write_parameters(self): if 'nxreduce' not in self.root['entry']: self.root['entry/nxreduce'] = NXparameters() self.root['entry/nxreduce/threshold'] = self.threshold self.root['entry/nxreduce/first_frame'] = self.first self.root['entry/nxreduce/last_frame'] = self.last self.root['entry/nxreduce/monitor'] = self.monitor self.root['entry/nxreduce/norm'] = self.norm self.root['entry/nxreduce/radius'] = self.radius self.root['entry/nxreduce/qmax'] = self.qmax
def save_fit(self): """Saves fit results to an NXentry""" self.read_parameters() entry = NXentry() entry['data'] = self.data for f in self.functions: entry[f.name] = self.get_model(f) parameters = NXparameters() for p in f.parameters: parameters[p.name] = NXfield(p.value, error=p.stderr, initial_value=p.init_value, min=str(p.min), max=str(p.max)) entry[f.name].insert(parameters) if self.fit is not None: entry['title'] = 'Fit Results' entry['fit'] = self.get_model() fit = NXparameters() fit.nfev = self.fit.result.nfev fit.ier = self.fit.result.ier fit.chisq = self.fit.result.chisqr fit.redchi = self.fit.result.redchi fit.message = self.fit.result.message fit.lmdif_message = self.fit.result.lmdif_message entry['statistics'] = fit else: entry['title'] = 'Fit Model' entry['model'] = self.get_model() if 'w0' not in self.tree.keys(): self.tree.add(NXroot(name='w0')) ind = [] for key in self.tree['w0'].keys(): try: if key.startswith('f'): ind.append(int(key[1:])) except ValueError: pass if not ind: ind = [0] name = 'f' + str(sorted(ind)[-1] + 1) self.tree['w0'][name] = entry
def save(self, x=None): """Save the fit results in a NXprocess group. Parameters ---------- x : ndarray, optional x-values at which to calculate the model. Defaults to `self.x` Returns ------- group : NXprocess NXprocess group that contains the data, models and parameters. """ group = NXprocess(program='lmfit', version=__version__) group['data'] = self.data for f in self.functions: group[f.name] = NXdata(NXfield(self.get_model(x, f), name='model'), NXfield(x, name=self.data.nxaxes[0].nxname), title='Fit Results') parameters = NXparameters() for p in f.parameters: parameters[p.name] = NXfield(p.value, error=p.stderr, initial_value=p.init_value, min=str(p.min), max=str(p.max)) group[f.name]['parameters'] = parameters group['title'] = 'Fit Results' group['fit'] = NXdata(NXfield(self.get_model(x), name='model'), NXfield(x, name=self.data.nxaxes[0].nxname), title='Fit Results') if self.result is not None: fit = NXparameters() fit.nfev = self.result.nfev fit.chisq = self.result.chisqr fit.redchi = self.result.redchi fit.message = self.result.message group['statistics'] = fit group.note = NXnote( self.fit.result.message, f'Chi^2 = {self.fit.result.chisqr}\n' f'Reduced Chi^2 = {self.fit.result.redchi}\n' f'No. of Function Evaluations = {self.fit.result.nfev}\n' f'No. of Variables = {self.fit.result.nvarys}\n' f'No. of Data Points = {self.fit.result.ndata}\n' f'No. of Degrees of Freedom = {self.fit.result.nfree}\n' f'{self.fit.fit_report()}') return group
def save_fit(self): """Saves fit results to an NXentry""" self.read_parameters() entry = NXentry() entry['data'] = self.data for f in self.functions: entry[f.name] = self.get_model(f) parameters = NXparameters() for p in f.parameters: parameters[p.name] = NXfield(p.value, error=p.stderr, initial_value=p.init_value, min=str(p.min), max=str(p.max)) entry[f.name].insert(parameters) if self.fit is not None: entry['title'] = 'Fit Results' entry['fit'] = self.get_model() fit = NXparameters() fit.nfev = self.fit.result.nfev fit.ier = self.fit.result.ier fit.chisq = self.fit.result.chisqr fit.redchi = self.fit.result.redchi fit.message = self.fit.result.message fit.lmdif_message = self.fit.result.lmdif_message entry['statistics'] = fit else: entry['title'] = 'Fit Model' entry['model'] = self.get_model() if 'w0' not in self.tree: self.tree.add(NXroot(name='w0')) ind = [] for key in self.tree['w0']: try: if key.startswith('f'): ind.append(int(key[1:])) except ValueError: pass if not ind: ind = [0] name = 'f'+str(sorted(ind)[-1]+1) self.tree['w0'][name] = entry