def lmfit(self,maxfev=0,report_fit=True,cpus=1,epsfcn=None,xtol=1.e-7,ftol=1.e-7, workdir=None, verbose=False, **kwargs): """ Calibrate MATK model using lmfit package :param maxfev: Max number of function evaluations, if 0, 100*(npars+1) will be used :type maxfev: int :param report_fit: If True, parameter statistics and correlations are printed to the screen :type report_fit: bool :param cpus: Number of cpus to use for concurrent simulations during jacobian approximation :type cpus: int :param epsfcn: jacobian finite difference approximation increment (single float of list of npar floats) :type epsfcn: float or lst[float] :param xtol: Relative error in approximate solution :type xtol: float :param ftol: Relative error in the desired sum of squares :type ftol: float :param workdir: Name of directory to use for model runs, calibrated parameters will be run there after calibration :type workdir: str :param verbose: If true, print diagnostic information to the screen :type verbose: bool :returns: lmfit minimizer object Additional keyword argments will be passed to scipy leastsq function: http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.optimize.leastsq.html """ try: import lmfit except ImportError as exc: sys.stderr.write("Warning: failed to import lmfit module. ({})".format(exc)) return self.cpus = cpus # Create lmfit parameter object params = lmfit.Parameters() for k,p in self.pars.items(): params.add(k,value=p.value,vary=p.vary,min=p.min,max=p.max,expr=p.expr) out = lmfit.minimize(self.__lmfit_residual, params, args=(cpus,epsfcn,workdir,verbose), maxfev=maxfev,xtol=xtol,ftol=ftol,Dfun=self.__jacobian, **kwargs) # Make sure that self.pars are set to final values of params nm = [params[k].name for k in self.pars.keys()] vs = [params[k].value for k in self.pars.keys()] self.parvalues = dict(zip(nm,vs)) # Run forward model to set simulated values if isinstance( cpus, int): self.forward(workdir=workdir,reuse_dirs=True) elif isinstance( cpus, dict): hostname = cpus.keys()[0] processor = cpus[hostname][0] self.forward(workdir=workdir,reuse_dirs=True, hostname=hostname,processor=processor) else: print 'Error: cpus argument type not recognized' return if report_fit: print lmfit.report_fit(params) print 'SSR: ',self.ssr return out
def lmfit_psf(filename, wsname): a = pd.read_excel(filename, wsname, header=None) x = np.array(map(float, a[0])) data = np.array(map(float, a[1])) # create a set of Parameters params = Parameters() params.add('amp', value=100, min=0.0) params.add('w0', value=0.25, min=0.0) params.add('shift', value=0.35) params.add('offset', value=0.0, min=0.0) # do fit, here with leastsq model result = minimize(fcn2min, params, args=(x, data)) # calculate final result final = data + result.residual # write error report report_fit(params) # try to plot results try: import pylab pylab.plot(x, data, 'k+') pylab.plot(x, final, 'r') pylab.title( '%s w0=%snm' % (os.path.splitext(filename)[0], round(params['w0'].value * 1000))) pylab.draw() except: pass
def Poly_fitting_Er(self): def fcn2min(prms, rs, Es): A = prms['A'] B = prms['B'] C = prms['C'] D = prms['D'] E_calc = [] E_poly = lambda r: A + B * r + C * r**2 + D * r**3 for ri in rs: E_calc.append(E_poly(ri)) return np.array(E_calc) - Es params = Parameters() params.add('A', 0, vary=True) params.add('B', 0, vary=True) params.add('C', 0, vary=True) params.add('D', 0, vary=True) minner = Minimizer(fcn2min, params=params, fcn_args=(self._rexp, self._Eexp)) result = minner.minimize() final = self._Eexp + result.residual report_fit(result) self.__A = result.params['A'].value self.__B = result.params['B'].value self.__C = result.params['C'].value self.__D = result.params['D'].value return final
def fit_nl_orthorhombic_cell(data_df, a, b, c, wavelength, verbose=True): """ perform non-linear fit data_df = data in pandas DataFrame a, b, c = cell parameter wavelength = this ca be replaced with .get_base_ptn_wavelength() """ h = data_df['h'] k = data_df['k'] l = data_df['l'] param = Parameters() param.add('a', value=a, min=0) param.add('b', value=b, min=0) param.add('c', value=c, min=0) twoth_data = data_df['twoth'] minner = Minimizer(fcn2min_orthorhombic, param, fcn_args=(h, k, l, twoth_data, wavelength)) result = minner.minimize() # calculate final result # write error report if verbose: report_fit(result) return result
def fit_phase_tau(self): #fit_range_indices = np.where((self.time>=self.phase_fit_range[0])&(self.time<=self.phase_fit_range[1])) fit_range_indices = np.arange(self.phase_fit_range[0], self.phase_fit_range[1]) fit_time = self.time[fit_range_indices] fit_phase = self.phase[fit_range_indices] self.lmfit_tau_result = minimizer.minimize(fn.phase_tau_func_residual, self.lmfit_init_tau_params, args=(fit_time, fit_phase), nan_policy='propagate', method='nelder') report_fit(self.lmfit_tau_result)
def run(config, input, output): dataFile = os.path.join(config['cwd'], input['fit.datafile'][0][0]) x, data = np.loadtxt(dataFile, usecols=(0, 1), unpack=True) params = Parameters() # Loop over parameters defined in input. Later, we might loop over # all possible parameters and set which ones vary instead. for param in input['fit.parameters']: #minval = param[1]*0.5 if param[1] != 0 else -0.5 #maxval = param[1]*1.5 if param[1] != 0 else 0.5 #params.add(param[0],value=param[1],min=minval,max=maxval) params.add(param[0], value=param[1]) #open('fitconvergence.dat', 'ab') # do fit, here with the default leastsq algorithm minner = Minimizer(Xanes2Min, params, fcn_args=(x, data, input, config, output)) #result = minner.minimize(epsfcn=0.0001,method='differential_evolution',workers=6) result = minner.minimize(epsfcn=0.0001) final = data + result.residual report_fit(result) with open('fit_result.txt', 'w') as fh: fh.write(fit_report(result)) output['fit'] = [ x.tolist(), final.tolist(), data.tolist() ] # For now set this as fit. Later we may want to make a statement about what is implemented and what is not for fit.
def das(tup, x0, from_t=0.4, uniform_fil=None, plot_result=True, fit_kws=None): out = namedtuple('das_result', field_names=[ 'fitter', 'result', 'minimizer']) ti = dv.make_fi(tup.t) if uniform_fil is not None: tupf = filter.uniform_filter(tup, uniform_fil) else: tupf = tup if fit_kws is None: fit_kws = {} import numpy as np #ct = dv.tup(np.hstack((wl, wl)), tup.t[ti(t0):], np.hstack((pa[ti(t0):, :], se[ti(t0):, :]))) ct = dv.tup(tup.wl, tup.t[ti(from_t):], tupf.data[ti(from_t):, :]) f = fitter.Fitter(ct, model_coh=0, model_disp=0) f.lsq_method = 'ridge' kws = dict(full_model=0, lower_bound=0.2, fixed_names=['w']) kws.update(fit_kws) lm = f.start_lmfit(x0, **kws) res = lm.leastsq() import lmfit lmfit.report_fit(res) if plot_result: plt.figure(figsize=(4, 7)) plt.subplot(211) if is_montone(f.wl): monotone = False # Assume wl is repeated N = len(f.wl) else: monotone = True N = len(f.wl) // 2 print(N) l = plt.plot(f.wl[:N], f.c[:N, :], lw=3) if monotone: l2 = plt.plot(f.wl[:N], f.c[N:, :], lw=1) for i, j in zip(l, l2): j.set_color(i.get_color()) plot_helpers.lbl_spec() lbls = ['%.1f' % i for i in f.last_para[1:-1]] + ['const'] plt.legend(lbls) plt.subplot(212) wi = dv.make_fi(tup.wl) for i in range(N)[::6]: l, = plt.plot(tup.t, tupf.data[:, i], '-o', lw=0.7, alpha=0.5, label='%.1f cm-1' % f.wl[i], mec='None', ms=3) plt.plot(f.t, f.model[:, i], lw=3, c=l.get_color()) if monotone: l, = plt.plot(tup.t, tupf.data[:, i+N], '-o', lw=0.7, alpha=0.5, label='%.1f cm-1' % f.wl[i], mec='None', ms=3) plt.plot(f.t, f.model[:, i+N], lw=3, c=l.get_color()) plt.xlim(-1) plt.xscale('symlog', linthreshx=1, linscalex=0.5) plot_helpers.lbl_trans() plt.legend(loc='best') return out(f, res, lm)
def analysis(meas, data=None, fig=None): xs = meas.delays ys, fig = meas.get_ys_fig(data, fig) # fig.axes[0].plot(xs/1e3, ys, 'ks', ms=3) amp0 = (np.max(ys) - np.min(ys)) / 2 fftys = np.abs(np.fft.fft(ys - np.average(ys))) fftfs = np.fft.fftfreq(len(ys), xs[1] - xs[0]) f0 = np.abs(fftfs[np.argmax(fftys)]) print 'Delta f estimate: %.03f kHz' % (f0 * 1e6) params = lmfit.Parameters() params.add('ofs', value=amp0) params.add('amp', value=amp0, min=0) params.add('tau', value=xs[-1], min=10, max=200000) params.add('freq', value=f0, min=0) params.add('phi0', value=0, min=-1.2 * np.pi, max=1.2 * np.pi) result = lmfit.minimize(t2_fit, params, args=(xs, ys)) lmfit.report_fit(params) fig.axes[0].plot(xs / 1e3, -t2_fit(params, xs, 0), label='Fit, tau=%.03f us, df=%.03f kHz' % (params['tau'].value / 1000, params['freq'].value * 1e6)) fig.axes[0].legend() fig.axes[0].set_ylabel('Intensity [AU]') fig.axes[0].set_xlabel('Time [us]') fig.axes[1].plot(xs / 1e3, t2_fit(params, xs, ys), marker='s') fig.canvas.draw() return params
def fit(self, time_range: np.arange = None, parameters: [Parameters, tuple] = None, initial_conditions: list = None, residual=None, verbose: bool = False, data: np.array = None): """ Fit the model based on data in the form np.array([X1,...,Xn]) """ if data is None: if self.data is None: raise ValueError("No data to fit the model on!") data = self.data if initial_conditions is not None: self.initial_conditions = initial_conditions if self.initial_conditions is None: raise ValueError("No initial conditions to fit the model with!") if parameters is None: if self.parameters is None: raise ValueError("No parameters to fit the model with!") parameters = self.parameters if time_range is None: time_range = np.arange(data.shape[0]) if residual is None: residual = self.residual result = minimize(residual, parameters, args=(time_range, data), method=self.fit_method) self.result = result self.fitted_parameters = result.params.valuesdict() if verbose: report_fit(result) return result
def lmfitter(x, y): params = Parameters() params.add('m', value=0.01, vary=True) out = minimize(residual, params, args=(x, y)) report_fit(params) return out.params['m'].value, 0.0, out.params['m'].stderr, 0.0
def fit_my_func(sigma, like): '''Fit my favorite function to the likelihood data''' from lmfit import minimize, Parameters, Parameter, report_fit params = Parameters() params.add('amp', value= 2.5, min=0.0) params.add('beta', value= 8.0,min=0.1) params.add('core', value= 2.0, min=0.1,max=5.0) print params # do fit, here with leastsq model result = minimize(func2min, params, args=(sigma, like)) print params # calculate final result final = like + result.residual # write error report report_fit(params) # try to plot results try: plt.plot(sigma, like, 'k+') plt.plot(sigma, final, 'r') plt.draw() plt.show() print 'sleeping 1 sec' time.sleep(1) except: pass return params
def runFit(self): ## Initialize parameters self.nb_calls = 0 ## Run fit algorithm if self.method=="least_square": out = minimize(self.compute_diff, self.pars) if self.method=="shgo": out = minimize(self.compute_diff, self.pars, method='shgo', sampling_method='sobol', options={"f_tol": 1e-16}, n = 100, iters=20) if self.method=="differential_evolution": out = minimize(self.compute_diff, self.pars, method='differential_evolution') if self.method=="ampgo": out = minimize(self.compute_diff, self.pars, method='ampgo') else: print("This method does not exist in the class") ## Display fit report report_fit(out) ## Export final parameters from the fit for param_name in self.ranges_dict.keys(): if param_name in self.init_member.keys(): self.member[param_name] = out.params[param_name].value elif param_name in self.init_member["band_params"].keys(): self.member["band_params"][param_name] = out.params[param_name].value ## Save BEST member to JSON self.save_member_to_json() ## Compute the FINAL member self.fig_compare(fig_save=True)
def analysis(meas, data=None, fig=None): ys, fig = meas.get_ys_fig(data, fig) xs = meas.angles fig.axes[0].plot(xs, ys, 'ks', ms=3) r = fp.Sine(x_data=xs, y_data=ys) result, params, ys_fit = r.fit(plot=False) txt = 'Frequency = %.03f +- %.03e\nPhase = %.03f +- %.03f' %\ (params['frequency'].value, params['frequency'].stderr, \ params['phase'].value, params['phase'].stderr) fig.axes[0].plot(xs, ys_fit, label=txt) fig.axes[1].plot(xs, ys-ys_fit, marker='s') lmfit.report_fit(params) fig.axes[0].set_ylabel('Intensity [AU]') fig.axes[0].set_xlabel('Rotation (Cycle)') fig.axes[0].legend(loc=0) fig.canvas.draw() return params
def fit_eta(self, line, **run_args): import lmfit def model(v, x): '''Eta orientation function.''' m = v['prefactor'] * (1 - (v['eta']**2)) / (( (1 + v['eta'])**2) - 4 * v['eta'] * (np.square( np.cos((v['symmetry'] / 2.0) * np.radians(x - v['x_center']))))) + v['baseline'] return m def func2minimize(params, x, data): v = params.valuesdict() m = model(v, x) return m - data params = lmfit.Parameters() params.add('prefactor', value=np.max(line.y) * 0.5, min=0) params.add('x_center', value=-90, min=np.min(line.x), max=np.max(line.x)) params.add('eta', value=0.2, min=0, max=1) params.add('symmetry', value=2, min=0.5, max=20, vary=False) params.add('baseline', value=0, min=0, max=np.max(line.y), vary=False) lm_result = lmfit.minimize(func2minimize, params, args=(line.x, line.y)) if run_args['verbosity'] >= 5: print('Fit results (lmfit):') lmfit.report_fit(lm_result.params) fit_x = line.x fit_y = model(lm_result.params.valuesdict(), fit_x) fit_line = DataLine(x=fit_x, y=fit_y, plot_args={ 'linestyle': '-', 'color': 'r', 'marker': None, 'linewidth': 4.0 }) fit_x = np.linspace(np.min(line.x), np.max(line.x), num=1000) fit_y = model(lm_result.params.valuesdict(), fit_x) fit_line_extended = DataLine(x=fit_x, y=fit_y, plot_args={ 'linestyle': '-', 'color': 'r', 'marker': None, 'linewidth': 4.0 }) return lm_result, fit_line, fit_line_extended
def calibrate_long_wG(): long_profile = load_data(calibration_shots_h5, 'wG/wG_profile') magnification = load_data(calibrations_h5, 'magnification/M_bar') def lmfit_gaussian_2d(xdata, ydata, zdata, u_zdata): pars = Parameters() pars.add('Tilt', value=-0.02, min=-0.1, max=0.1, vary=True) pars.add('Amplitude', value=4e4, vary=True) pars.add('wx', value=150, min=2, max=400, vary=True) pars.add('wy', value=150, min=2, max=400, vary=True) pars.add('x0', value=371, vary=True) pars.add('y0', value=181, vary=True) pars.add('offset', value=1e3, vary=True) def residuals(pars, xdata, ydata, zdata, u_zdata): theta = pars['Tilt'] amp = pars['Amplitude'] wx = pars['wx'] wy = pars['wy'] x0 = pars['x0'] y0 = pars['y0'] off = pars['offset'] zmodel = gaussian_2d(xdata, ydata, theta, amp, wx, wy, x0, y0, off) return (zmodel - zdata) / u_zdata return minimize(residuals, pars, args=(xdata, ydata, zdata, u_zdata), method='leastsq', nan_policy='omit') def gaussian_2d(x, y, theta, amp, wx, wy, x0, y0, offset): x = x[np.newaxis, :] y = y[:, np.newaxis] u, v = ((x - x0) * np.cos(theta) - (y - y0) * np.sin(theta)), ((x - x0) * np.sin(theta) + (y - y0) * np.cos(theta)) return amp * np.exp(-(2 * v**2 / wy**2) - (2 * u**2 / wx**2)) + offset x_coords, y_coords = np.linspace(0, long_profile.shape[1], long_profile.shape[1]), np.linspace( 0, long_profile.shape[0], long_profile.shape[0]) result = lmfit_gaussian_2d(x_coords, y_coords, long_profile, 5) parameters = np.array( [result.params[key].value for key in result.params.keys()]) try: u_params = np.sqrt(np.diag(result.covar)) except ValueError: u_params = np.zeros_like(parameters) report_fit(result) x_waist = parameters[2] * pix_size / magnification zR = pi * (x_waist)**2 / (1.064 * um) u_x_waist = u_params[2] * pix_size / magnification u_zR = pi * x_waist * u_x_waist / (1.064 * um) with h5py.File(calibrations_h5) as calibrations: h5_save(calibrations, 'wG/wx0', x_waist) h5_save(calibrations, 'wG/u_wx0', u_x_waist)
def E_total_fitting_Er( self): # fitting DFT data of E_V with the above function def fcn2min(prms, rs, Es): ls = prms['ls'] E_calc = [] for ri in rs: E_calc.append(self.E_total(ri, ls)) return np.array(E_calc) - Es params = Parameters() _last_fitting = "/_last_fitting.json" filename = current_folder + _last_fitting if os.path.exists(filename): with open(filename, 'r') as f: dct = json.load(f) if 'ls' in dct: params.add('ls', dct['ls'], vary=True) else: params.add('ls', 0.5, vary=True) minner = Minimizer(fcn2min, params=params, fcn_args=(self._rexp, self._Eexp)) result = minner.minimize() final = self._Eexp + result.residual _dct = {'ls': result.params['ls'].value} with open(filename, 'w') as f: json.dump(_dct, f) report_fit(result) self.LengthScale = result.params['ls'].value return final
def cov_quasiparticle_occupation_fit_thermal(self, C): n_qp = self.cov_calc_quasiparticle_occupation(C) def objective_function(params, e, n_qp): beta = params['beta'] mu = params['mu'] FD = np.exp(-beta * e + mu) / (1 + np.exp(-beta * e + mu)) return FD - n_qp params = Parameters() params.add('beta', value=3, min=0, max=100) params.add('mu', value=0, min=-10, max=10) minner = Minimizer(objective_function, params, fcn_args=(self.e, n_qp)) result = minner.minimize() final = n_qp + result.residual beta_opt = result.params['beta'].value mu_opt = result.params['mu'].value print "Inverse temp fit: ", beta_opt print "Chemical potential fit fit: ", mu_opt n_qp_thermal = [self.Fermi_Dirac(E, beta_opt, mu_opt) for E in self.e] report_fit(result) return [n_qp, n_qp_thermal, beta_opt, mu_opt]
def __lmfit_fit(residual_function, params, algo_choice, args=None, kws=None, **fit_kws): """Process calls for [lmfit] fitting.""" method = "nelder" if algo_choice == "simplex" else \ "leastsq" if algo_choice == "levmar" else \ "emcee" if algo_choice == "mcmc" else \ "brute" if algo_choice == "grid" else None if method is None: raise ValueError("algo_choice invalid") # Switch output channel to suppress printing during fitting process old_stdout = sys.stdout new_stdout = io.StringIO() sys.stdout = new_stdout # Call [lmfit.minimize()] out = minimize(residual_function, params, method=method, nan_policy='omit', args=args, kws=kws, **fit_kws) # Return to original output channel sys.stdout = old_stdout # Print report of fitting results report_fit(out.params) # Return value bindings return out.params
def analysis(meas, data=None, fig=None, period=None): ys, fig = meas.get_ys_fig(data, fig) xs = meas.areas fig.axes[0].plot(xs, ys, 'ks', ms=3) amp0 = (np.max(ys) - np.min(ys)) / 2 if ys[0] > np.average(ys): amp0 = -amp0 fftys = np.abs(np.fft.fft(ys - np.average(ys))) fftfs = np.fft.fftfreq(len(ys), xs[1] - xs[0]) period0 = 1 / np.abs(fftfs[np.argmax(fftys)]) params = lmfit.Parameters() params.add('ofs', value=np.average(ys)) params.add('amp', value=amp0) if period is not None: params.add('period', value=period, min=0, vary=False) else: params.add('period', value=period0, min=0) params.add('decay', value=20000, min=0) result = lmfit.minimize(fit_timerabi, params, args=(xs, ys)) lmfit.report_fit(params) txt = 'Amp = %.03f +- %.03e\nPeriod = %.03f +- %.03e\nPi area = %.03f' % ( params['amp'].value, params['amp'].stderr, params['period'].value, params['period'].stderr, params['period'].value / 2) fig.axes[0].plot(xs, -fit_timerabi(params, xs, 0), label=txt) fig.axes[0].set_ylabel('Intensity [AU]') fig.axes[0].set_xlabel('Pulse area') fig.axes[0].legend(loc=0) fig.axes[1].plot(xs, fit_timerabi(params, xs, ys), marker='s') fig.canvas.draw() return params
def get_radial_dose(radial_dose_in, dwell_in, point_in): """ Calculate the radial dose function value """ R = pdist([[dwell_in.x, dwell_in.y, dwell_in.z], [point_in.x, point_in.y, point_in.z]]) if R in radial_dose_in.r_cm: return radial_dose_in.gL[radial_dose_in.r_cm.index(R)] elif R < min(radial_dose_in.r_cm): nrVal = find_nearest(np.array(radial_dose_in.r_cm), R) return radial_dose_in.gL[radial_dose_in.r_cm.index(nrVal)] elif R > max(radial_dose_in.r_cm): pw = 2 params = Parameters() params.add('pw', value=pw, vary=False) params.add('adj1', value=1) params.add('adj2', value=1) xf = radial_dose_in.r_cm[-2:] yf = radial_dose_in.gL[-2:] result = minimize(fcn2min, params, args=(xf, yf)) report_fit(result.params) adj1 = result.params['adj1'] adj2 = result.params['adj2'] next_y = adj1 * np.power(R + adj2, pw) return next_y else: return log_interp(radial_dose_in.r_cm, radial_dose_in.gL, R)
def fit_model_with_adapter(method='nelder'): """ Test fit of a bare silicon with oxide. These are the REFL1D results for the same model: Chi2 = 1.307 SiOx thickness 6.7 +- 1.7 SiOx SLD 4.0 +- 0.3 SiOx roughness 1.0 +- 0.7 Si roughness 5.7 +- 2.0 """ # This is how the interface will use its layer model model_adapter = ModelAdapter(bare_silicon_model()) params = model_adapter.params_from_model() # Load the data q, r, dr, _ = np.loadtxt(os.path.join(os.path.dirname(__file__), 'data/Si_137848_1850_800.txt'), unpack=True) # Perform the fit result = minimize(residual_with_adapter, params, args=(q, r, dr, model_adapter), method=method) report_fit(result)
def fit_data_sample(): # create a set of Parameters params = Parameters() params.add('amp', value=10, min=0) params.add('decay', value=0.1) params.add('shift', value=0.0, min=-np.pi / 2., max=np.pi / 2.) params.add('omega', value=3.0) # create data to be fitted x = np.linspace(0, 15, 301) data = (5.0 * np.sin(2.0 * x - 0.1) * np.exp(-x * x * 0.025) + np.random.normal(size=x.size, scale=0.2)) # do fit, here with the default leastsq algorithm minner = Minimizer(fcn2min, params, fcn_args=(x, data)) result = minner.minimize() # calculate final result final = data + result.residual # write error report report_fit(result) # try to plot results try: import matplotlib.pyplot as plt plt.plot(x, data, 'k+') plt.plot(x, final, 'r') plt.show() except ImportError: pass
def sineInfo(s, f = 3., report=True, debugPlot = False): ''' f for repetition in s, input f = fft_f * tSpan return amp, freql, shift, THD real f = freql /tSpan ''' l = len(s) t = array(range(l)) a2 = ppv(s) from lmfit import minimize, Parameters, report_fit def err(params, t, s): amp = params['amp'].value shift = params['shift'].value omega = params['freq'].value model = amp * sin(t * omega * 2*pi + shift) return model - s params = Parameters() params.add('amp', value= a2, min= a2 / 3) params.add('shift', value= 0.) #, min=-pi, max=pi) params.add('freq', value=f/l , min = 0.6*f/l, max = 1.4*f/l) resd = minimize(err, params, args=(t, s)).residual if report: report_fit(params) if debugPlot: figure() plot(s) plot(s+resd) return (params['amp'].value, params['freq'].value * l , params['shift'].value % (2*pi) - pi, sum(resd**2)/sum(s**2))
def report_mcmc_fit(mcmc_res): """Report output of MCMC fit. Code from https://lmfit.github.io/lmfit-py/fitting.html """ # ----------------------- # Median of distribution print("median of posterior probability distribution") print("--------------------------------------------") lmfit.report_fit(mcmc_res.params) p = mcmc_res.params.copy() highest_prob = np.argmax(mcmc_res.lnprob) hp_loc = np.unravel_index(highest_prob, mcmc_res.lnprob.shape) mle_soln = mcmc_res.chain[hp_loc] for i, par in enumerate(p): p[par].value = mle_soln[i] # ----------------------- # Max Likelihood print("\nMaximum Likelihood Estimation from emcee ") print("-------------------------------------------------") print("Parameter MLE Value Median Value Uncertainty") fmt = " {:5s} {:11.5f} {:11.5f} {:11.5f}".format for name, param in p.items(): print( fmt( name, param.value, mcmc_res.params[name].value, mcmc_res.params[name].stderr, )) # ----------------------- # Error Estimate print("\nError Estimates from emcee ") print("------------------------------------------------------") print("Parameter -2sigma -1sigma median +1sigma +2sigma ") for name in p.keys(): quantiles = np.percentile(mcmc_res.flatchain[name], [2.275, 15.865, 50, 84.135, 97.275]) median = quantiles[2] err_m2 = quantiles[0] - median err_m1 = quantiles[1] - median err_p1 = quantiles[3] - median err_p2 = quantiles[4] - median fmt = " {:5s} {:8.4f} {:8.4f} {:8.4f} {:8.4f} {:8.4f}".format print(fmt(name, err_m2, err_m1, median, err_p1, err_p2)) # /for return
def main(): # Read in reference/model spectrum ref = np.loadtxt('ref.txt', unpack=True) # Read in input spectrum to be scaled spec = np.loadtxt(sys.argv[1], unpack=True) region = [[6507, 6512], [6560, 6565]] # Background or continuum region ref_w, ref_f = region_around_line(ref[0], ref[1], region) spec_w, spec_f = region_around_line(spec[0], spec[1], region) divisor = np.max(ref_f) # To normalize spectrum ref_f, spec_f = ref_f / divisor, spec_f / divisor # Initial parameters params = Parameters() params.add('dx', value=0) params.add('sigma', value=1) params.add('amplitude', value=1) # Minimize fitout = minimize(fit_profile_err, params, args=(spec_w, spec_f, ref_f)) fitted_p = fitout.params scaled = fit_profile(fitted_p, spec_w, spec_f) # Best-fit parameters report_fit(fitout) # Save new scaled spectrum save_scaled_spectrum = True if save_scaled_spectrum: ref_center = ref_w[np.argmax(ref_f)] # Model spectrum center new_center = spec_w[np.argmax(scaled)] # This center is from the fit shift = ref_center - new_center # Wavelength shift spec_w_new = shift + spec[0] # Apply shift scaling_factor = simps(ref_f, ref_w) / simps( scaled, spec_w) # To scale the spectrum spec_f_new = spec[1] * scaling_factor # Scaled spec_e_new = spec[2] * scaling_factor # Re-bin spec_f_new = np.interp(ref[0], spec_w_new, spec_f_new) spec_e_new = np.interp(ref[0], spec_w_new, spec_e_new) out_name = os.path.splitext( sys.argv[1])[0].split('.fits')[0] + '_scaled.txt' # Save np.savetxt(out_name, np.transpose([ref[0], spec_f_new, spec_e_new])) plt.plot(ref_w, ref_f) plt.plot(spec_w, scaled) plt.show()
def fit_hawkes(event_times_dict_list, predefined_beta_matrix=None, pool=None): '''Fit a multidimensional Hawkes Process to observed sequences Parameters ---------- event_times_dict_list : list of dict of lists of floats List of sequences of event dicts, with process dimension as key and list of ascending timestamps (floats) as values predefined_beta_matrix : matrix of floats Self-excitation decay per dimension^2. If equals None, then it will be a parameter to fit. Returns ------- fitted_hawkes : MultivariateHawkes New Hawkes Process with fitted parameters ''' parameters = lmfit.Parameters() for dimension_m in event_times_dict_list[0]: parameters.add("mu_{}".format(dimension_m), min=0, value=0.9) for dimension_n in event_times_dict_list[0]: parameters.add("alpha_{}{}".format(dimension_m, dimension_n), min=0, value=0.9) if not predefined_beta_matrix: parameters.add("beta_{}{}".format(dimension_m, dimension_n), min=0, value=0.9) def __extract_parameters(parameters): mu_list = [] alpha_matrix = [] beta_matrix = [] for dim_m in event_times_dict_list[0]: mu_list.append(parameters["mu_{}".format(dim_m)]) alpha_matrix.append([]) beta_matrix.append([]) for dim_n in event_times_dict_list[0]: alpha_matrix[dim_m].append(parameters["alpha_{}{}".format(dim_m, dim_n)]) if not predefined_beta_matrix: beta_matrix[dim_m].append(parameters["beta_{}{}".format(dim_m, dim_n)]) return {"mu": mu_list, "alpha": alpha_matrix, "beta": beta_matrix if not predefined_beta_matrix else predefined_beta_matrix} def log_likelihood_wrapper(parameters, event_times_dict_list, pool): current_hawkes_process = MultivariateHawkes(**__extract_parameters(parameters)) current_hawkes_process.pprint() ''' We compute -1 * log_likelihood, because we use algorithms that minimize, and finding parameters that maximize likelihood is equivalent to finding those that minimize -log_likelihood ''' result = pool.map(current_hawkes_process.log_likelihood, event_times_dict_list) return list(np.array(result) * -1) # return [-1 * current_hawkes_process.log_likelihood(event_times_dict) for event_times_dict in event_times_dict_list] if not pool: pool = multiprocessing.Pool(processes = constants.NUMBER_OF_PROCESSES) minimizer = lmfit.Minimizer(log_likelihood_wrapper, parameters, fcn_args=(event_times_dict_list, pool)) result = minimizer.minimize(method="lbfgsb") lmfit.report_fit(result) return {"parameters": __extract_parameters(parameters), "average_negativeloglikelihood": np.mean(log_likelihood_wrapper(parameters, event_times_dict_list, pool))}
def quadratic_planefit(x, y, z, err=None, pinit=None, method='leastsq', report_fit=False): """ Fits a quadratic bivariate polynomial to 2D data Parameters ---------- x : ndarray array of x values y : ndarray array of y values z : ndarray data to be fit err : ndarray (optional) uncertainties on z data pinit : ndarray (optional) initial guesses for fitting. Format = [mmx, mx, mmy, my, c] method : string (optional) method used for the minimisation (default = leastsq) """ if pinit is None: pars=lmfit.Parameters() pars.add('mmx', value=1.0) pars.add('mx', value=1.0) pars.add('mmy', value=1.0) pars.add('my', value=1.0) pars.add('c', value=1.0) else: pars=lmfit.Parameters() pars.add('mmx', value=pinit[0]) pars.add('mx', value=pinit[1]) pars.add('mmy', value=pinit[2]) pars.add('my', value=pinit[3]) pars.add('c', value=pinit[4]) fitter = lmfit.Minimizer(residual_quadratic_planefit, pars, fcn_args=(x,y), fcn_kws={'data':z, 'err':err}, nan_policy='propagate') result = fitter.minimize(method=method) if report_fit: lmfit.report_fit(result) popt = np.array([result.params['mmx'].value, result.params['mx'].value, result.params['mmy'].value, result.params['my'].value, result.params['c'].value]) perr = np.array([result.params['mmx'].stderr, result.params['mx'].stderr, result.params['mmy'].stderr, result.params['my'].stderr, result.params['c'].stderr]) return popt, perr, result
def prad_fe_fit(): cksbin = cksmet.io.load_table('cksbin-fe') x = log10(cksbin.binc) y = cksbin.fe_mean yerr = cksbin.fe_mean_err import lmfit p0 = lmfit.Parameters() p0.add('p1', 0.1) p0.add('p0', 0.1) def model_linear(params, x): return params['p1'] * x + params['p0'] def model_step(params, x): if isinstance(x, Iterable): out = map(lambda x: model_step(params, x), x) out = np.array(out) return out if x < log10(4): return params['p0'] elif x >= log10(4): return params['p1'] def resid(params, model, x, y, yerr): return (y - model(params, x)) / yerr res_linear = lmfit.minimize(resid, p0, args=( model_linear, x, y, yerr, )) p1_linear = res_linear.params print "linear model" lmfit.report_fit(res_linear) res_step = lmfit.minimize(resid, p0, args=( model_step, x, y, yerr, )) p1_step = res_linear.params print "step model" lmfit.report_fit(res_step) print "BIC(linear) - BIC(step) = {}".format(res_linear.bic - res_step.bic) semilogx() xerr = bins_to_xerr(cksbin.bin0, cksbin.binc, cksbin.bin1) print xerr errorbar(10**x, y, xerr=xerr, yerr=yerr, fmt='o') plot(10**x, model_linear(p1_linear, x)) prad_fe_label() xlim(0.5, 16)
def fitEPSC(self): # create a set of Parameters params = lmfit.Parameters() params.add('amp', value=self.ptreedata.param('Initial Fit Parameters').param( 'amp').value(), min=-self.dmax, max=self.dmax) params.add('tau_rise', value=self.ptreedata.param('Initial Fit Parameters').param( 'taurise').value(), min=1e-4, max=1e-1) params.add('tau_fall', value=self.ptreedata.param('Initial Fit Parameters').param( 'taufall').value(), min=1e-4, max=1e-1) params.add('DC', value=self.ptreedata.param('Initial Fit Parameters').param( 'DC').value(), min=-1e3, max=1e3) dc = np.mean(self.dataY[0:10]) params.add('DC', value=dc, min=dc - dc * 1, max=dc + dc * 1) t0 = self.T0.value() t1 = self.T1.value() it0 = int(t0 / self.dt) it1 = int(t1 / self.dt) if it0 > it1: t = it0 it0 = it1 it1 = t # do fit, here with the default leastsq algorithm time_zero = int(self.time_zero / self.dt) print('timezero: ', time_zero, self.dataX[time_zero]) print(self.dataX[it0:it1] - self.time_zero) print(self.dataY[it0:it1]) minner = lmfit.Minimizer( self._fcn_EPSC, params, fcn_args=(self.dataX[it0:it1] - self.dataX[time_zero], self.dataY[it0:it1])) self.fitresult = minner.minimize(method='least_squares', ) # calculate final result final = self.dataY[it0:it1] + self.fitresult.residual # write error report lmfit.report_fit(self.fitresult) self.updateFit(self.fitresult) self.fitx = self.dataX[it0:it1] self.fity = final self.showFit()
def paramOptimization(self, data, time, ts=None): warnings.filterwarnings("ignore") # set parameters including bounds; you can also fix parameters (use vary=False) S0, E0, I0, R10, R20, D0 = self.y0 _, a0, b0, c0, f0, mu0, xi0 = self.modele.getParam() params = Parameters() params.add('N', value=self.N, vary=False) params.add('E0', value=E0, vary=False) params.add('I0', value=I0, vary=False) params.add('R10', value=R10, vary=False) params.add('R20', value=R20, vary=False) params.add('D0', value=D0, vary=False) params.add('a', value=a0, vary=True, min=0.0, max=0.99) params.add('b', value=b0, vary=True, min=0.0, max=0.99) params.add('c', value=c0, vary=True, min=0.0, max=0.99) params.add('f', value=f0, vary=True, min=0.0, max=0.99) params.add('mu', value=mu0, vary=True, min=0.00001, max=0.15) params.add('xi', value=xi0, vary=True, min=0.000001, max=0.1) # The time delay is only used for the first period (otherwise it is 0 and doesn't need to be estimated) if ts != None: params.add('ts', value=ts, vary=False) else: params.add('ts', value=self.TS, vary=True, min=0, max=len(time) - np.shape(data)[0] - 2) # Fit the model by minimization result = minimize(residual, params, args=(time, data, self, self.indexdata), method='powell') #powell, least_squares if self.verbose > 1: result.params.pretty_print() # display fitted statistics report_fit(result) # Update params with the optimized values self.setParamInit(result.params['N'].value, result.params['E0'].value, result.params['I0'].value, result.params['R10'].value, result.params['R20'].value, result.params['D0'].value) self.modele.setParam( result.params['N'].value, result.params['a'].value, result.params['b'].value, result.params['c'].value, result.params['f'].value, result.params['mu'].value, result.params['xi'].value) self.TS = result.params['ts'].value warnings.filterwarnings("default")
def diag(subf, subs, cat_freqs, cat_K, cat_J, cat_En): ''' vdopler shift! ''' cat_freqs = cat_freqs.copy() xy = [] constants = [] params = Parameters() params.add('w0', 0.0105, min=-0.01, max=0.04) params.add('I0', 0.050, min=0.01, max=10) params.add('I1', 0.050, min=0.01, max=10) params.add('I2', 0.050, min=0.01, max=10) params.add('I3', 0.050, min=0.01, max=10) params.add('I4', 0.050, min=0.01, max=10) params.add('sigma', 0.0001, min=0.00001, max=0.01) minm = minimize(resCH3C2H, params, args=(subf, subs, cat_freqs / 1e9)) lines = fitCH3C2H(minm.params, subf, cat_freqs / 1e9) report_fit(minm.params) I = [] for i in range(len(cat_freqs)): I.append(minm.params.get('I{0}'.format(i)).value) for i in range(len(cat_freqs)): f = cat_freqs[i] / 1e9 par = minm.params.copy() for j in range(len(cat_freqs)): par['I{0}'.format(j)].value = 0 par['I{0}'.format(i)].value = I[i] integ = ((fitCH3C2H(minm.params, subf, cat_freqs / 1e9) > 0) * fitCH3C2H(minm.params, subf, cat_freqs / 1e9)) intl, eu, error = rot(integ.sum(), cat_K[i], cat_J[i], cat_En[i], cat_freqs[i], (subs - lines).std()) xy.append((intl, eu, error)) xy = np.array(xy) xy = np.flipud(xy) fitfunc = lambda x, k, b: -x / k + b def residual(params, x, yd, error): k = params['k'].value b = params['b'].value model = fitfunc(x, k, b) return (yd - model) params = Parameters() params.add('k', value=40) params.add('b', value=-0.8) minm = minimize(residual, params, args=(xy[:-1, 1], xy[:-1, 0], [])) report_fit(minm.params) x = xy[:, 1] y = xy[:, 0] dy = xy[:, 2] Dx = len(x) * ((x**2).sum()) + (x.sum())**2 su = 0 for i in range(len(dy)): su += (dy[i] * (x[i] - x.sum()) / Dx)**2 dB = math.sqrt(su) return (minm.params['k'].value.dB, xy)
def fit_all(t, p): params = lmfit.Parameters() params.add('lnkappa', value=2.) params.add('n_avrami', value=1.) out = lmfit.minimize(residual, params, args=(t, p, )) # lmfit minimizer lmfit.report_fit(params) # return out.params['chi0'].value return out
def polar_plot(dataset, fig_name=None, polar=True, ax=None, reset_scaling=False, fit_mask=np.ones(16, dtype=bool)): if ax is None: fig = plt.figure(fig_name) fig.clf() ax = fig.add_subplot(111, polar=polar) else: fig = ax.figure phi = np.linspace(0, 2 * np.pi, 16, endpoint=False) phi_line = np.linspace(0, 2 * np.pi, 2**10) if reset_scaling: det_factors = dataset.det_factors.copy() dataset.det_factors = np.ones_like(det_factors) auger = dataset.auger_amplitudes.mean(axis=0) photo = dataset.photo_amplitudes.mean(axis=0) if reset_scaling: det_calib = auger.max() / auger ax.plot(phi, auger, 'gx') auger *= det_calib ax.plot(phi, photo, 'rx') photo *= det_calib ax.plot(phi, auger, 'gs', label='auger') ax.plot(phi, photo, 'ro', label='photo') params = cookie_box.initial_params(photo) params['beta'].vary = False params['beta'].value = 2 lmfit.minimize(cookie_box.model_function, params, args=(phi[fit_mask], photo[fit_mask])) lmfit.report_fit(params) ax.plot(phi_line, cookie_box.model_function(params, phi_line), '-m', label='{:.1f} % lin {:.1f} deg'.format( params['linear'].value * 100, np.rad2deg(params['tilt'].value))) ax.grid(True) ax.legend(loc='center', bbox_to_anchor=(0, 0), fontsize='medium') plt.tight_layout() if reset_scaling: dataset.det_factors = det_factors return fig
def fit_peaks(self, line, num_curves=1, **run_args): # Usage: lm_result, fit_line, fit_line_extended = self.fit_peaks(line, **run_args) line_full = line #if 'fit_range' in run_args: #line = line.sub_range(run_args['fit_range'][0], run_args['fit_range'][1]) import lmfit def model(v, x): '''Gaussians with constant background.''' m = v['m']*x + v['b'] for i in range(num_curves): m += v['prefactor{:d}'.format(i+1)]*np.exp( -np.square(x-v['x_center{:d}'.format(i+1)])/(2*(v['sigma{:d}'.format(i+1)]**2)) ) return m def func2minimize(params, x, data): v = params.valuesdict() m = model(v, x) return m - data params = lmfit.Parameters() params.add('m', value=0) params.add('b', value=np.min(line.y), min=0, max=np.max(line.y)) xspan = np.max(line.x) - np.min(line.x) xpeak, ypeak = line.target_y(np.max(line.y)) for i in range(num_curves): #xpos = np.min(line.x) + xspan*(1.*i/num_curves) #xpos, ypos = line.target_x(xpeak*(i+1)) xpos, ypos = xpeak, ypeak params.add('prefactor{:d}'.format(i+1), value=ypos-np.min(line.y), min=0, max=np.max(line.y)*10) params.add('x_center{:d}'.format(i+1), value=xpos, min=np.min(line.x), max=np.max(line.x)) params.add('sigma{:d}'.format(i+1), value=xspan*0.20, min=0, max=xspan*0.50) lm_result = lmfit.minimize(func2minimize, params, args=(line.x, line.y)) if run_args['verbosity']>=5: print('Fit results (lmfit):') lmfit.report_fit(lm_result.params) fit_x = line.x fit_y = model(lm_result.params.valuesdict(), fit_x) fit_line = DataLine(x=fit_x, y=fit_y, plot_args={'linestyle':'-', 'color':'b', 'marker':None, 'linewidth':4.0}) fit_x = np.linspace(np.min(line_full.x), np.max(line_full.x), num=200) fit_y = model(lm_result.params.valuesdict(), fit_x) fit_line_extended = DataLine(x=fit_x, y=fit_y, plot_args={'linestyle':'-', 'color':'b', 'alpha':0.5, 'marker':None, 'linewidth':2.0}) return lm_result, fit_line, fit_line_extended
def fit(self): # set everything up, then call the fit function, finally print out the results in the table self.setParametersFromTable() import Fitting xData,yData = self.getCurrentData() result = Fitting.FitOne(self.getCurrentFunctionName(),self.params,xData,yData) import lmfit lmfit.report_fit(self.params) self.updateParametersTable() self.updatePlot()
def lmfitter(x , y, y_error): params = Parameters() params.add('phiStar', value=0.1, vary=True) params.add('LStar', value=1.0, vary=True) params.add('alpha', value=-1, vary=True) # remove inf values in errors out = minimize(residual, params, args=(x, y, y_error)) report_fit(params) return out
def test_peakfit(): from lmfit.utilfuncs import gaussian def residual(pars, x, data=None): g1 = gaussian(x, pars['a1'].value, pars['c1'].value, pars['w1'].value) g2 = gaussian(x, pars['a2'].value, pars['c2'].value, pars['w2'].value) model = g1 + g2 if data is None: return model return (model - data) n = 601 xmin = 0. xmax = 15.0 noise = np.random.normal(scale=.65, size=n) x = np.linspace(xmin, xmax, n) org_params = Parameters() org_params.add_many(('a1', 12.0, True, None, None, None), ('c1', 5.3, True, None, None, None), ('w1', 1.0, True, None, None, None), ('a2', 9.1, True, None, None, None), ('c2', 8.1, True, None, None, None), ('w2', 2.5, True, None, None, None)) data = residual(org_params, x) + noise fit_params = Parameters() fit_params.add_many(('a1', 8.0, True, None, 14., None), ('c1', 5.0, True, None, None, None), ('w1', 0.7, True, None, None, None), ('a2', 3.1, True, None, None, None), ('c2', 8.8, True, None, None, None)) fit_params.add('w2', expr='2.5*w1') myfit = Minimizer(residual, fit_params, fcn_args=(x,), fcn_kws={'data':data}) myfit.prepare_fit() init = residual(fit_params, x) myfit.leastsq() print(' N fev = ', myfit.nfev) print(myfit.chisqr, myfit.redchi, myfit.nfree) report_fit(fit_params) fit = residual(fit_params, x) check_paras(fit_params, org_params)
def example(): ''' Make a "noisy" hologram. Then fit the noisy hologram. Plot the results. ''' ## Make Noisy Hologram. # Create hologram to be fitted. x,y,z = 0., 0., 100. a_p = 0.5 n_p = 1.5 n_m = 1.339 dim = [201,201] lamb = 0.447 mpp = 0.135 hologram = sph.spheredhm([x,y,z], a_p, n_p, n_m, dim, mpp, lamb) # Add noise. std = 0.03 noise = np.random.normal(size=hologram.shape)*std noisy_hologram = hologram + noise ## Fit the noisy hologram. init_params = {'x':x, 'y':y, 'z':z, 'a_p':a_p, 'n_p':n_p, 'n_m':n_m, 'mpp':mpp, 'lamb':lamb} mie_fit = Mie_Fitter(init_params) result = mie_fit.fit(noisy_hologram) # Calculate the resulting image. residual = result.residual.reshape(*dim) final = hologram + residual # Write error report. report_fit(result) ## Make plots. # Plot images. sns.set(style='white', font_scale=1.4) plt.imshow(np.hstack([hologram, final, residual+1])) plt.title('Image, Fit, Residual') plt.gray() plt.show() # Plot Covariance. f, ax = plt.subplots() #cmap = sns.diverging_palette(220, 10, as_cmap=True) sns.set(font_scale=1.5) plt.title('Log Covariance Matrix') sns.heatmap(np.log(result.covar), cmap='PuBu', square=True, cbar_kws={}, ax=ax) ax.set_xticklabels(['x', 'y', 'z', r'a$_p$', r'n$_p$']) ax.set_yticklabels([r'n$_p$', r'a$_p$', 'z', 'y', 'x']) plt.show()
def run_minimize(paramdic, maxlimitdic, minlimitdic, datadic, extrakeys, extrakeyssolv, rsolvtest, outfile, nptype, mintotalerror): mintotalerror = mintotalerror params = Parameters() for ikey, ivalue in paramdic.iteritems(): maxlimit = maxlimitdic[ikey] minlimit = minlimitdic[ikey] ikey = ikey.replace("@","zzz") #replace @ with zzz because this character is not supported by lmfit library ikey = ikey.replace(".","xxx") #replace . with xxx because this character is not supported by lmfit library #~ params.add(ikey, value=ivalue) #~ if "rczzz" in ikey: #~ params.add(ikey, value=ivalue, min=minlimit, max=maxlimit) #~ else: #~ params.add(ikey, ivalue, False) params.add(ikey, value=ivalue, min=minlimit, max=maxlimit) print params #experimental data datacompoundnamelist = [] for ikey, ivalue in datadic.iteritems(): datacompoundnamelist.append(ikey) #to convert in error function to a dictionary #~ extra_kwargs={} #~ extra_kwargs['epsfcn'] = 0.5 #~ result = minimize(fcn2min, params, args=(extrakeys, extrakeyssolv, rsolvtest, outfile, nptype, datacompoundnamelist), method='lbfgs', **extra_kwargs) #~ extra_kwargs={} #~ extra_kwargs['T'] = 300.0 #~ extra_kwargs['stepsize']=0.1 #~ result = minimize(fcn2min, params, args=(extrakeys, extrakeyssolv, rsolvtest, outfile, nptype, datacompoundnamelist), method='basinhopping', **extra_kwargs) result = minimize(fcn2min, params, args=(extrakeys, extrakeyssolv, outfile, nptype, datacompoundnamelist, limitcycle), method='powell') #~ #calculate final result #~ final = data + result.residual # write error report report_fit(params) #~ # try to plot results #~ try: #~ import pylab #~ pylab.plot(x, data, 'k+') #~ pylab.plot(x, final, 'r') #~ pylab.show() #~ except: #~ pass return datadic
def polar_plot(dataset, fig_name=None, polar=True, ax=None, reset_scaling=False, fit_mask=np.ones(16, dtype=bool)): if ax is None: fig = plt.figure(fig_name) fig.clf() ax = fig.add_subplot(111, polar=polar) else: fig = ax.figure phi = np.linspace(0, 2 * np.pi, 16, endpoint=False) phi_line = np.linspace(0, 2 * np.pi, 2**10) if reset_scaling: det_factors = dataset.det_factors.copy() dataset.det_factors = np.ones_like(det_factors) auger = dataset.auger_amplitudes.mean(axis=0) photo = dataset.photo_amplitudes.mean(axis=0) if reset_scaling: det_calib = auger.max() / auger ax.plot(phi, auger, 'gx') auger *= det_calib ax.plot(phi, photo, 'rx') photo *= det_calib ax.plot(phi, auger, 'gs', label='auger') ax.plot(phi, photo, 'ro', label='photo') params = cookie_box.initial_params(photo) params['beta'].vary = False params['beta'].value = 2 lmfit.minimize(cookie_box.model_function, params, args=(phi[fit_mask], photo[fit_mask])) lmfit.report_fit(params) ax.plot(phi_line, cookie_box.model_function(params, phi_line), '-m', label='{:.1f} % lin {:.1f} deg'.format( params['linear'].value*100, np.rad2deg(params['tilt'].value))) ax.grid(True) ax.legend(loc='center', bbox_to_anchor=(0, 0), fontsize='medium') plt.tight_layout() if reset_scaling: dataset.det_factors = det_factors return fig
def fit_decision_model(df): import lmfit fit_params = lmfit.Parameters() fit_params.add('alpha1', value=.5, min=0, max=1) fit_params.add('alpha2', value=.5, min=0, max=1) fit_params.add('lam', value = .5, min=0, max=1) fit_params.add('W', value = .5, min=0, max=1) fit_params.add('p', value = 0) fit_params.add('B1', value = 3) #fit_params.add('B2', value = 3) out = lmfit.minimize(get_likelihood, fit_params, method = 'lbfgsb', kws={'df': df}) lmfit.report_fit(out) return out.params.valuesdict()
def lmfitter(x , y, y_error): params = Parameters() params.add('a', value=-1., vary=True) params.add('b', value=1., vary=True) params.add('c', value=1., vary=True) params.add('d', value=1., vary=True) params.add('e', value=0.0, vary=False) # remove inf values in errors y_error[y_error == np.float('inf')] = 1.0 out = minimize(residual, params, args=(x, y, y_error)) report_fit(params) return out
def _fit_one_file(self, data_orders, rv_guess=0.0, alpha_guess=1.0): # Create parameters params = lmfit.Parameters() params.add('rv', value=rv_guess, min=-5, max=5) params.add('alpha', value=alpha_guess, min=0, max=3) # Perform the fit result = lmfit.minimize(self._errfcn, params, args=(data_orders, self.telluric_model)) logger = logging.getLogger() if logger.level <= logging.DEBUG: lmfit.report_fit(params) return params
def r_fit(time, R, volume_params,far_field=True): volume_params = volume_params.valuesdict() t_long = volume_params['tau_long'] params = Parameters() params.add('Rinf', value=-0.01, ) params.add('R0', value=-0.05, ) params.add('tau_long', value=t_long, vary=False) result = minimize(residual_r, params, args=(time, R,True,far_field), method='leastsq', maxfev=int(1e5), xtol=1e-9, ftol=1e-9) report_fit(result.params, min_correl=0.5) v = result.params.valuesdict() t = time R_th = residual_r(params, time, R, fit=False) return result.params, R_th
def fit_hi_vel_range(guesses=None, av_image=None, av_image_error=None, hi_cube=None, hi_velocity_axis=None, hi_noise_cube=None, dgr=None): from scipy.optimize import curve_fit from scipy import stats from lmfit import minimize, Parameters, report_fit, report_errors from pprint import pprint params = Parameters() params.add('low_vel', value=guesses[0], min=-100, max=100, vary=True) params.add('high_vel', value=guesses[1], min=-100, max=100, vary=True) result = minimize(calc_model_chisq, params, kws={'av_image': av_image, 'av_image_error': av_image_error, 'hi_cube': hi_cube, 'hi_velocity_axis': hi_velocity_axis, 'hi_noise_cube': hi_noise_cube, 'dgr': dgr}, #method='BFGS', method='anneal', #method='powell', #method='SLSQP', options={'gtol': 1e-6, 'disp': True, 'maxiter' : 1e9} ) report_fit(params) report_errors(params) print result.values print result.errorbars #print(result.__dict__) #print(dir(result)) #print result.vars #print help(result) print result.view_items() print result.Bopt
def fitcurve(val0): if elem.matchX is False: return ydat = measdata.D1complex[:, measdata.findex] data = np.empty(len(ydat) * 2, dtype='float64') data[0::2] = ydat.real data[1::2] = ydat.imag preFit(False) xaxis3 = np.linspace(squid.start, squid.stop, (squid.pt * 2)) # Using standard curve_fit settings # Define fitting parameters params = Parameters() params.add('CapfF', value=squid.Cap*1e15, vary=True, min=30, max=90) params.add('IcuA', value=squid.Ic*1e6, vary=True, min=3.0, max=4.5) params.add('WbpH', value=squid.Wb*1e12, vary=True, min=0, max=1500) params.add('LooppH', value=squid.LOOP*1e12, vary=False, min=0.0, max=100) params.add('alpha', value=squid.ALP, vary=False, min=0.98, max=1.02) params.add('R', value=squid.R, vary=True, min=1, max=20e3) params.add('Z1', value=elem.Z1, vary=False, min=40, max=60) params.add('Z2', value=elem.Z2, vary=False, min=40, max=60) params.add('Z3', value=elem.Z3, vary=False, min=40, max=60) params.add('L2', value=elem.L2, vary=False, min=0.00, max=0.09) # Crop region to fit elem.midx = find_nearest(xaxis3, elem.xmin) elem.madx = find_nearest(xaxis3, elem.xmax) # Do Fit result = minimize(gta1, params, args=(xaxis3[elem.midx:elem.madx], data)) # Present results of fitting print report_fit(result) paramsToMem(result.params) update2(0) preFit(True) # Calculate and plot residual there S11 = getfit() residual = data - S11 plt.figure(4) plt.clf() plt.plot(xaxis3[elem.midx:elem.madx], residual[elem.midx:elem.madx]) plt.axis('tight') plt.draw() print 'Avg-sqr Residuals', abs(np.mean((residual * residual))) * 1e8 return
def sineFITwLINEARbackground(param1, param2, **kwargs): """ Bounded LS minimisation for the fitting of a sinefunction to a given dataset *with a linear background instead of a constant background*. It is assumed that you can represent param2 as a function of param1. The lmfit package is used to perform the bounded LS minimisation. DANGER this routine assumes you have a fixed frequency of 1 [unit**-1]. For example: - if param1 is time and param2 is flux, you will have a sine with a frequency of 1 c/d. - if param1 is position and param2 is flux, you will have a sine with a frequency of 1 c/pix. Returns: The optimal lmfit parameter class. TODO provide options through kwargs to set the boundaries Returns: The optimal lmfit parameter class. @param param1: param1 measurements [???] @type param1: numpy array of length N @param param2: param2 measurements [???] @type param2: numpy array of length N @return: paramSINE @rtype: lmfit parameter class @kwargs: show_ME: Boolean to indicate if you want to see the report_fit - Default is False [Boolean] """ show_ME = kwargs.get('show_ME', False) # Determination of the guesses to start your bounded LS fit with. constantGUESS = np.median(param2) # param2 amplitudeGUESS = np.max(np.abs(constantGUESS-param2))/2. # param2 frequencyGUESS = 1. # DANGER Here is the frequency assumption assumption. phaseGUESS = 0.1 # Using the param1[np.where(param2==np.max(param2))]-param1[0]%1-0.5 is best, when there is no scatter on param2 slopeGUESS, constantGUESS = np.polyfit(param1, param2, 1) paramSINE = Parameters() #Make a params class for lmfit. # (Name, Value, Vary, Min, Max, Expr) paramSINE.add_many(('amplitude', amplitudeGUESS, True, amplitudeGUESS*0.1, amplitudeGUESS*1.2, None), ('frequency', frequencyGUESS, False, frequencyGUESS-0.05, frequencyGUESS+0.05, None), # DANGER Here is the frequency assumption assumption. (It is set to non-vary.) ('constant', constantGUESS, True, -abs(constantGUESS)*1.5, abs(constantGUESS)*1.5, None), ('phase', phaseGUESS, True, 0., 1., None), ('slope', slopeGUESS, True, -2*slopeGUESS, +2*slopeGUESS, None)) resultSIN = minimize(BRITE.fitfunctions.ff_lmfitlmfit_sinslope_vs_data, paramSINE, args=(param1, param2)) if show_ME: report_fit(paramsSIN, show_correl=False) return paramSINE
def optimize(self, optimization_iterations=10, fcn_callback=None): params = Parameters() params.add("density", value=self.density, min=self.density_min, max=self.density_max) params.add("background_scaling", value=self.background_scaling, min=self.bkg_min, max=self.bkg_max) def fcn_optimization(params): density = params['density'].value background_scaling = params['background_scaling'].value self.background_spectrum.scaling = background_scaling calculator = StandardCalculator( original_spectrum=self.original_spectrum, background_spectrum=self.background_spectrum, elemental_abundances=self.elemental_abundances, density=density, r=self.r, interpolation_method=self.interpolation_method, interpolation_parameters=self.interpolation_parameters, use_modification_fcn=self.use_modification_fcn ) calculator.optimize( r=self.minimization_r, iterations=optimization_iterations ) if fcn_callback is not None: fr_spectrum = calculator.calc_fr() gr_spectrum = calculator.calc_gr() fcn_callback(background_scaling, density, fr_spectrum, gr_spectrum) r, fr = calculator.calc_fr(self.minimization_r).data output = (-fr - 4 * np.pi * convert_density_to_atoms_per_cubic_angstrom(self.elemental_abundances, density) * self.minimization_r) ** 2 self.write_output(u'{} X: {:.3f} Den: {:.3f}'.format(self.iteration, np.sum(output)/(r[1]-r[0]), density)) self.iteration+=1 return output self.output_txt.setPlainText('') minimize(fcn_optimization, params) self.write_fit_result(params) report_fit(params)
def volume_fit(time, volumeexp,far_field=True): params = Parameters() params.add('V0', value=-1e-5,) params.add('delta',value=0.0001,min=0) params.add('Vinf', expr='V0-delta') params.add('tau_long', value=0.1) result = minimize(residual_volume, params, args=(time, volumeexp,True,far_field), method='leastsq', maxfev=int(1e5), xtol=1e-9, ftol=1e-9) v = result.params.valuesdict() t = time V_th = residual_volume(params, t, volumeexp, fit=False) # plt.plot(time - t0, volumeexp, 'ro', label="Experimental") # plt.plot(t_th - t0, V_th, 'b-', label="Theoretical") # plt.title("Volume") # plt.legend() # plt.show() report_fit(result.params, min_correl=0.5) return result.params, V_th
def far_field_fit(time, yg, volume_params, R_params, rho, k, g=9.81, mass=0, manip_name=None): # yg = savgol_filter(yg,11,3) volume_params = volume_params.valuesdict() R_params = R_params.valuesdict() Finf = volume_params['Vinf'] * rho * g F0 = volume_params['V0'] * rho * g deltaF = -(Finf - F0) Rinf = R_params['Rinf'] R0 = R_params['R0'] deltaR =-(Rinf - R0) print deltaF params = Parameters() Amin = 0.05 params.add('q0', value=0.1) params.add('q0p', value=0.1) params.add('k', value=8.33) params.add('massTot', value=0.036, min=0.01,max= 1) params.add('deltaprim', value=-0.1) params.add('deltaF', value=deltaF, vary=False) params.add('Finf', value=Finf, vary=False) params.add('deltaR', value=deltaR, vary=False) params.add('Rinf', value=Rinf, vary=False) params.add('tau_v', value=volume_params['tau_long'], vary=False) params.add('tau_R', value=R_params['tau_long'], vary=False) params.add('C1', expr="Finf/k", ) params.add('C2', expr="deltaF/(massTot*((1/tau_v)**2-(2*lamda/tau_v)+omega0**2))") params.add('D1', expr="Rinf" ) params.add('D2', expr="(omega0**2)*deltaR/((1/tau_R)**2-(2*lamda/tau_R)+omega0**2)") params.add('omega0', expr="(k/massTot)**0.5") params.add('lamda', value=1) params.add('deltaprim', expr='lamda**2-omega0**2') result = minimize(residual_far_field, params, args=(time, yg), method='leastsq', maxfev=int(1e5), xtol=1e-9, ftol=1e-9) report_fit(result.params, min_correl=0.5) print result.success v = params.valuesdict() yg_th = residual_far_field(params, time, yg, fit=False) v["added_mass"] =0 v['real_mass'] = mass v["added_mass"] = v['massTot'] - v['real_mass'] # yg_f,ygdot_fa,ygdotdot_f = filterer(yg,win_size,pol_order,double_filter) return params, yg_th
def run_main(): x1, x2, x3 = dw['target fuel_consumptions'].values, dw['target engine_speeds_out'].values, dw.velocities.values data = dw['target engine_coolant_temperatures'].values #.diff().fillna(0) temperature_threshold = 95 temperature_max, initial_engine_temperature = data.max(), data[0] t_init = initial_engine_temperature # do fit, here with leastsq model _t0 = datetime.datetime.now() result = minimize(fcn2min, params, args=(x1, x2, x3, data, t_init)) _t1 = datetime.datetime.now() print("Elapsed time: %s"%(_t1-_t0)) # calculate final result final = fcn(result.params, x1, x2, x3, t_init) # write error report report_fit(result.params) n1, n2, n3 = dn['target fuel_consumptions'].values, dn['target engine_speeds_out'].values, dn.velocities.values nedc_data = dn['target engine_coolant_temperatures'].values nedc_t_init = nedc_data[0] nedc_final = fcn(result.params, n1, n2, n3, nedc_t_init) # try to plot results try: import pylab pylab.plot(data, 'k+') pylab.plot(final, 'r') pylab.plot(nedc_data, 'g+') pylab.plot(nedc_final, 'b') pylab.show() except: pass
def test_1var(): rands = [-0.21698284, 0.41900591, 0.02349374, -0.218552, -0.3513699, 0.33418304, 0.04226855, 0.213303, 0.45948731, 0.33587736] x = numpy.arange(10)+1 y = numpy.arange(10)+1+rands y_errs = numpy.sqrt(y)/2 params = lmfit.Parameters() params.add(name="m", value=1.0, vary=True) params.add(name="c", value=0.0, vary=False) out = lmfit.minimize(linear_chisq, params, args=(x, y)) lmfit.report_fit(out) assert_allclose(params['m'].value, 1.025, rtol=0.02, atol=0.02) assert(len(params)==2) assert(out.nvarys == 1) assert(out.chisqr > 0.01) assert(out.chisqr < 5.00)
def test_simple(): # create data to be fitted np.random.seed(1) x = np.linspace(0, 15, 301) data = (5. * np.sin(2 * x - 0.1) * np.exp(-x*x*0.025) + np.random.normal(size=len(x), scale=0.2) ) # define objective function: returns the array to be minimized def fcn2min(params, x, data): """ model decaying sine wave, subtract data""" amp = params['amp'].value shift = params['shift'].value omega = params['omega'].value decay = params['decay'].value model = amp * np.sin(x * omega + shift) * np.exp(-x*x*decay) return model - data # create a set of Parameters params = Parameters() params.add('amp', value= 10, min=0) params.add('decay', value= 0.1) params.add('shift', value= 0.0, min=-pi / 2., max=pi / 2) params.add('omega', value= 3.0) # do fit, here with leastsq model result = minimize(fcn2min, params, args=(x, data)) # calculate final result final = data + result.residual # write error report print(" --> SIMPLE --> ") print(result.params) report_fit(result.params) #assert that the real parameters are found for para, val in zip(result.params.values(), [5, 0.025, -.1, 2]): check(para, val)
def fit_hi_vel_range( guesses=None, av_image=None, av_image_error=None, hi_cube=None, hi_velocity_axis=None, hi_noise_cube=None, dgr=None ): from scipy.optimize import curve_fit from scipy import stats from lmfit import minimize, Parameters, report_fit, report_errors from pprint import pprint params = Parameters() params.add("low_vel", value=guesses[0], min=-100, max=100, vary=True) params.add("high_vel", value=guesses[1], min=-100, max=100, vary=True) result = minimize( calc_model_chisq, params, kws={ "av_image": av_image, "av_image_error": av_image_error, "hi_cube": hi_cube, "hi_velocity_axis": hi_velocity_axis, "hi_noise_cube": hi_noise_cube, "dgr": dgr, }, # method='BFGS', method="anneal", # method='powell', # method='SLSQP', options={"gtol": 1e-6, "disp": True, "maxiter": 1e9}, ) report_fit(params) report_errors(params) print result.values print result.errorbars # print(result.__dict__) # print(dir(result)) # print result.vars # print help(result) print result.view_items() print result.Bopt
def generate(mod_pars, body_pars, photo_data, rv_data, fit_method, ncores, fname): nbodies, epoch, max_h, orbit_error, rv_body, rv_corr = mod_pars masses, radii, fluxes, u1, u2, a, e, inc, om, ln, ma = body_pars lmparams = lmParameters() # lmparams.add('N', value=N, vary=False) # lmparams.add('epoch', value=epoch, vary=False) # lmparams.add('maxh', value=maxh, vary=False) # lmparams.add('orbit_error', value=orbit_error, vary=False) for i in range(nbodies): lmparams.add('mass_{0}'.format(i), value=masses[i], min=0.0, max=0.1, vary=False) lmparams.add('radius_{0}'.format(i), value=radii[i], min=0.0, max=1.0, vary=False) lmparams.add('flux_{0}'.format(i), value=fluxes[i], min=0.0, max=1.0, vary=False) lmparams.add('u1_{0}'.format(i), value=u1[i], min=0.0, max=1.0, vary=False) lmparams.add('u2_{0}'.format(i), value=u2[i], min=0.0, max=1.0, vary=False) # if i < N-1: # params['flux_{0}'.format(i)].vary = False # params['u1_{0}'.format(i)].vary = False # params['u2_{0}'.format(i)].vary = False if i > 0: lmparams.add('a_{0}'.format(i), value=a[i - 1], min=0.0, max=10.0, vary=False) lmparams.add('e_{0}'.format(i), value=e[i - 1], min=0.0, max=1.0, vary=False) lmparams.add('inc_{0}'.format(i), value=inc[i - 1], min=0.0, max=np.pi, vary=False) lmparams.add('om_{0}'.format(i), value=om[i - 1], min=0.0, max=twopi) lmparams.add('ln_{0}'.format(i), value=ln[i - 1], min=0.0, max=twopi) lmparams.add('ma_{0}'.format(i), value=ma[i - 1], min=0.0, max=twopi) print('Generating maximum likelihood values...') results = minimize(residual, lmparams, args=(mod_pars, photo_data, rv_data, ncores, fname), iter_cb=per_iteration, method=fit_method) # Save the final outputs print "Writing report..." report_fit(results.params) utilfuncs.report_as_input(mod_pars, utilfuncs.get_lmfit_parameters(mod_pars, results.params), fname) # Return best fit values return utilfuncs.get_lmfit_parameters(mod_pars, results.params)
def exp_fit(x, y, start_taus = [1], use_constant=True, amp_max=None, amp_min=None, weights=None, amp_penalty=0, verbose=True): num_exp = len(start_taus) para = lmfit.Parameters() if use_constant: para.add('const', y[-1] ) for i in range(num_exp): para.add('tau' + str(i), start_taus[i], min=0) y_c = y - y[-1] a = y_c[fi(x, start_taus[i])] para.add('amp' + str(i), a) if amp_max is not None: para['amp' + str(i)].max = amp_max if amp_min is not None: para['amp' + str(i)].min = amp_min def fit(p): y_fit = np.zeros_like(y) if use_constant: y_fit += p['const'].value for i in range(num_exp): amp = p['amp'+str(i)].value tau = p['tau'+str(i)].value y_fit += amp * np.exp(-x/tau) + amp_penalty/y.size return y_fit def res(p): if weights is None: return y - fit(p) else: return (y - fit(p)) / weights mini = lmfit.minimize(res, para) if verbose: lmfit.report_fit(mini) y_fit = fit(mini.params) return mini, y_fit
def r_fit(time, R, volume_params,far_field): t_short = volume_params['tau_short'] omega = volume_params['omega'] t_long = volume_params['tau_long'] params = Parameters() params.add('amp', value=-0.02) params.add('tau_short', value=t_short, vary=True) params.add('omega', value=omega, vary=True) # params.add('omega', expr='omega0*sqrt(1-(1/(tau_short*omega0)**2))') params.add('shift', value=1, min=0, max=2 * np.pi) params.add('Rinf', value=-0.01, ) params.add('R0', value=-0.05, ) params.add('tau_long', value=t_long, vary=True) result = minimize(residual_r, params, args=(time, R,far_field), method='leastsq', maxfev=int(5e4), xtol=1e-8, ftol=1e-8) report_fit(result.params, min_correl=0.5) v = result.params.valuesdict() t = time R_th = residual_r(params, time, R, fit=False) return v, R_th
def example(): # create data to be fitted x = np.linspace(0, 15, 301) data = (5. * np.sin(2 * x - 0.1) * np.exp(-x*x*0.025) + np.random.normal(size=len(x), scale=0.2) ) # define objective function: returns the array to be minimized def fcn2min(params, x, data): """ model decaying sine wave, subtract data""" amp = params['amp'].value shift = params['shift'].value omega = params['omega'].value decay = params['decay'].value model = amp * np.sin(x * omega + shift) * np.exp(-x*x*decay) return model - data # create a set of Parameters params = Parameters() params.add('amp', value= 10, min=0) params.add('decay', value= 0.1) params.add('shift', value= 0.0, min=-np.pi/2., max=np.pi/2) params.add('omega', value= 3.0) # do fit, here with leastsq model result = minimize(fcn2min, params, args=(x, data)) # calculate final result final = data + result.residual plt.plot(x,data) #plt.plot(x,final) plt.show() print result.residual resultparams = result.params print resultparams amp = resultparams['amp'].value decay = resultparams['decay'].value shift = resultparams['shift'].value omega = resultparams['omega'].value model = amp * np.sin(x * omega + shift) * np.exp(-x*x*decay) plt.plot(x,model) plt.show() report_fit(result.params)
def lmfit_fit(group, x_fit, y_interp): '''set initial value is important, and more sepcified value can be set down ward''' para = [1]*group #P initial value para.extend([1]*group) #a initial value para.extend([3]*group) #b initial value ################ params = Parameters() for i in np.arange(len(para)/3): p = 'p'+str(i) a = 'a'+str(i) b = 'b'+str(i) if i == 0: params.add(p, value=abs(para[i]), min=10**-1) params.add(a, value=abs(para[i+len(para)/3]), min=0.5) params.add(b, value=0.5, min=0.1, max=1.1) else: params.add(p, value=abs(para[i]), min=10**-10) params.add(a, value=abs(para[i+len(para)/3]), min=10**-5) params.add(b, value=abs(para[i + 2*len(para)/3]), min=0.5, max=5) #using lmfit to get the final result without negtive values result = minimize(residuals, params, args=(x_fit, y_interp), method='leastsq') report_fit(result.params) return result