def fit_gaussian(data, constrained=False): illusion_strength = compute_illusion_strength(data) noise_frequencies = np.unique(data.noise_freq) n_reps = illusion_strength.shape[1] # fit a horizontal line if the data do not have any real dip # approximate baseline as 3rd largest value baseline = np.sort(illusion_strength.mean(1))[-2] minimum = illusion_strength.mean(1).min() if baseline - minimum < illusion_strength.mean(1).max() - baseline: model = lmfit.Model(lambda x, baseline: baseline) params = lmfit.Parameters() params.add('baseline', value=baseline) else: params = lmfit.Parameters() #params.add('center', value=3) params.add('center', value=noise_frequencies[np.argmin( illusion_strength.mean(1))], max=9, min=.5) params.add('width', value=2) if constrained: params.add('baseline', value=baseline) params.add('minimum', value=minimum, min=minimum - 1, max=baseline + 1) else: params.add('baseline', value=5) params.add('minimum', value=-2) model = lmfit.Model(inverted_gaussian) result = model.fit(illusion_strength.ravel(), x=noise_frequencies.repeat(n_reps), params=params) return result
def fit_gaussian(data, constrained=False): illusion_strength = compute_illusion_strength(data) noise_frequencies = np.unique(data.noise_freq) n_reps = illusion_strength.shape[1] # fit a horizontal line if the data do not have any real dip if data.vp[0] in ['dbm', 'odog'] or (data.vp[0] == 'biwam' and data.grating_freq[0] == .2): model = lmfit.Model(lambda x, baseline: baseline) params = lmfit.Parameters() params.add('baseline', value=5) else: params = lmfit.Parameters() #params.add('center', value=3) params.add('center', value=noise_frequencies[np.argmin( illusion_strength.mean(1))], max=9, min=.01) params.add('width', value=3) if constrained: minimum = illusion_strength.mean(1).min() params.add('baseline', value=5, max=8.8) params.add('minimum', value=-minimum, min=-6, max=-minimum + 1) else: params.add('baseline', value=5) params.add('minimum', value=5) model = lmfit.Model(inverted_gaussian) result = model.fit(illusion_strength.ravel(), x=noise_frequencies.repeat(n_reps), params=params) return result
def __init__(self, amplitude, sigma, skew, baseline, floor, ceiling, null, peak_limit_frc=.2): # initialise null model null_model = lmfit.models.ConstantModel() null_params = lmfit.Parameters() null_params['c'] = null # initialise peak model peak_model = lmfit.Model(skewed_gaussian) peak_params = lmfit.Parameters() peak_params['center'] = lmfit.Parameter(value=0, vary=False) peak_params['amplitude'] = amplitude peak_params['sigma'] = sigma peak_params['skew'] = skew peak_params['baseline'] = baseline peak_params['ceiling'] = ceiling peak_params['floor'] = floor # initialise flank model half_peak_model = lmfit.Model(gaussian) super(SkewedGaussianPeakFitter, self).__init__( null_model=null_model, null_params=null_params, peak_model=peak_model, peak_params=peak_params, half_peak_model=half_peak_model, peak_limit_frc=peak_limit_frc )
def prepare_fitting(self): self.fit_dicts = OrderedDict() dac_vals = self.proc_data_dict['dac_values'] freq_vals = self.proc_data_dict['fit_frequencies'] if max(freq_vals) < 1e9: freq_vals *= 1e9 guess = self.options_dict.get('fit_guess', {}) f_q = guess.get( 'f_max_qubit', self.options_dict.get('f_max_qubit', None)) guess['f_max_qubit'] = f_q ext = f_q is not None if self.is_spectroscopy: fitmod = lmfit.Model(Qubit_dac_to_freq) fitmod.guess = Qubit_dac_arch_guess.__get__( fitmod, fitmod.__class__) else: if f_q is None and self.verbose: print('Specify f_max_qubit in the options_dict to obtain a better fit!') # Todo: provide alternative fit? fitmod = lmfit.Model(Resonator_dac_to_freq) fitmod.guess = Resonator_dac_arch_guess.__get__( fitmod, fitmod.__class__) # fit_result = fitmod.fit(freq_vals, dac_voltage=dac_vals) self.fit_dicts['dac_arc'] = { 'model': fitmod, 'fit_xvals': {'dac_voltage': dac_vals}, 'fit_yvals': {'data': freq_vals}, 'guessfn_pars': {'values': guess} }
def curv_fit(x=None, y=None, model=None): x = np.array(x) y = np.array(y) params = lmfit.Parameters() if model == 'gaussian': mod = lmfit.models.GaussianModel() params = mod.guess(y, x=x) out = mod.fit(y, params, x=x) r_sq = 1 - out.residual.var() / np.var(y) elif model == '4PL': mod = lmfit.Model(logistic_4p) params.add('la', value=1.0) params.add('gr', value=120.0, vary=False) params.add('ce', value=150.0) params.add('ua', value=3.0) out = mod.fit(y, params, x=x) r_sq = 1 - out.residual.var() / np.var(y) elif model == '5PL': mod = lmfit.Model(logistic_5p) params.add('la', value=1.0) params.add('gr', value=1.0) params.add('ce', value=1.0) params.add('ua', value=1.0) params.add('sy', value=1.0) out = mod.fit(y, params, x=x) r_sq = 1 - out.residual.var() / np.var(y) out.R_sq = r_sq return out
def lmfit_voigt_fit(data, startparams, costrains): # startparams:XCEN,YCEN, model = lmfit.Model(func=constant_func, independent_vars=['x', 'y'], prefix='const_') for i in range(len(startparams)): mod = lmfit.Model(func=pseudo_voigt_2d, independent_vars=['x', 'y'], prefix='model' + str(i) + '_') model += mod params = model.make_params() params['const_c'].set(value=np.min(data[2]), vary=False) for i in range(len(startparams)): for j in values: ii = 'model' + str(i) + '_' params[ii + j].set(value=startparams.at[ii, j], max=costrains.at[j, 'max'], min=costrains.at[j, 'min'], vary=costrains.at[j, 'vary']) output = model.fit(data=data[2].ravel(), params=params, x=data[0].ravel(), y=data[1].ravel()) return output
def Obtain_Initial_Phase(tpf, corrected_lc, frequency_list): flux = corrected_lc.flux.value times = corrected_lc.time.value - np.mean(corrected_lc.time.value) pg = corrected_lc.to_periodogram(frequency=np.append( [0.0001], frequency_list), ls_method='slow') initial_flux = np.asarray(pg.power[1:]) initial_phase = np.zeros(len(frequency_list)) def lc_model(time, amp, freq, phase): return amp * np.sin(2 * np.pi * freq * time + phase) def background_model(time, height): return np.ones(len(time)) * height for j in np.arange(len(frequency_list)): for i in np.arange(len(frequency_list)): if (i == 0): model = lm.Model(lc_model, independent_vars=['time'], prefix='f{0:d}'.format(i)) model += lm.Model(background_model, independent_vars=['time']) else: model += lm.Model(lc_model, independent_vars=['time'], prefix='f{0:d}'.format(i)) model.set_param_hint('f{0:d}phase'.format(i), min=-np.pi, max=np.pi, value=initial_phase[i], vary=False) model.set_param_hint('f{0:d}amp'.format(i), value=initial_flux[i], vary=False) model.set_param_hint('height', value=np.mean(flux), vary=False) model.set_param_hint('f{0:d}freq'.format(i), value=frequency_list[i], vary=False) params = model.make_params() params['f{0:d}phase'.format(j)].set(vary=True) params['f{0:d}phase'.format(j)].set(value=initial_phase[j]) params['f{0:d}phase'.format(j)].set(brute_step=np.pi / 10) result = model.fit(corrected_lc.flux.value, params, time=times, method='brute') initial_phase[j] = result.best_values['f{0:d}phase'.format(j)] return initial_phase
def __init__(self, amp, tau): self.amp_s = amp self.tau_s = tau self.amp_k = self.amp_s/(self.amp_s-1.) self.tau_k = (1.-self.amp_s)*self.tau_s self.step_func = lambda t: 1.-self.amp_s*np.exp(-t/self.tau_s) self.kernel_step_func = lambda t: 1.+self.amp_k*np.exp(-t/self.tau_k) self.step_model = lmfit.Model(self.step_func) self.kernel_step_model = lmfit.Model(self.kernel_step_func)
def fitBandy2(et, M, dM=None, mode="logM", start=None, lb=None, ub=None): def residual(pars, x, data=None, eps=None): # unpack parameters: # extract .value attribute for each parameter parvals = pars.valuesdict() period = parvals["period"] shift = parvals["shift"] decay = parvals["decay"] if abs(shift) > pi / 2: shift = shift - sign(shift) * pi if abs(period) < 1.0e-10: period = sign(period) * 1.0e-10 model = parvals["amp"] * sin(shift + x / period) * exp( -x * x * decay * decay) if data is None: return model if eps is None: return model - data return (model - data) / eps pars = lmfit.Parameters() pars.add("t", value=start[0], min=lb[0], max=ub[0], vary=1) pars.add("g", value=start[1], min=lb[1], max=ub[1], vary=1) if dM is not None: wgt = 1.0 / dM**2 else: wgt = np.ones_like(M) if mode == "M": mod = lmfit.Model(bandy_M) elif mode == "logM": mod = lmfit.Model(bandy_logM) elif mode == "logMa" or mode == "logMa_off": mod = lmfit.Model(bandy_logMa) if "off" in mode: pars.add("a", value=0, min=0, vary=1) else: pars.add("a", value=0, vary=0) pars.add("b", value=start[2], min=lb[2], max=ub[2], vary=1) elif mode == "logV2a": mod = lmfit.Model(bandy_V2a) wgt = np.log10(wgt) pars.add("a", value=0, vary=0) pars.add("b", value=start[2], min=lb[2], max=ub[2], vary=1) if "log" in mode: M = np.log10(M) if dM is not None: wgt = 1.0 / np.log10(dM)**2 return mod.fit(M, pars, x=et, weights=wgt)
def fitBandy2(et, M, dM=None, mode='logM', start=None, lb=None, ub=None): def residual(pars, x, data=None, eps=None): # unpack parameters: # extract .value attribute for each parameter parvals = pars.valuesdict() period = parvals['period'] shift = parvals['shift'] decay = parvals['decay'] if abs(shift) > pi / 2: shift = shift - sign(shift) * pi if abs(period) < 1.e-10: period = sign(period) * 1.e-10 model = parvals['amp'] * sin(shift + x / period) * exp( -x * x * decay * decay) if data is None: return model if eps is None: return (model - data) return (model - data) / eps pars = lmfit.Parameters() pars.add('t', value=start[0], min=lb[0], max=ub[0], vary=1) pars.add('g', value=start[1], min=lb[1], max=ub[1], vary=1) if dM is not None: wgt = 1. / dM**2 else: wgt = np.ones_like(M) if mode == 'M': mod = lmfit.Model(bandy_M) elif mode == 'logM': mod = lmfit.Model(bandy_logM) elif mode == 'logMa' or mode == 'logMa_off': mod = lmfit.Model(bandy_logMa) if 'off' in mode: pars.add('a', value=0, min=0, vary=1) else: pars.add('a', value=0, vary=0) pars.add('b', value=start[2], min=lb[2], max=ub[2], vary=1) elif mode == 'logV2a': mod = lmfit.Model(bandy_V2a) wgt = np.log10(wgt) pars.add('a', value=0, vary=0) pars.add('b', value=start[2], min=lb[2], max=ub[2], vary=1) if 'log' in mode: M = np.log10(M) if dM is not None: wgt = (1. / np.log10(dM)**2) return mod.fit(M, pars, x=et, weights=wgt)
def get_optimal_amp(qbc, qbt, soft_sweep_points, timestamp=None, classified_ro=False, tangent_fit=False, parfit=False, analysis_object=None, **kw): if analysis_object is None: if classified_ro: channel_map = {qb.name: [vn + ' ' + qb.instr_acq() for vn in qb.int_avg_classif_det.value_names] for qb in [qbc, qbt]} else: channel_map = {qb.name: [vn + ' ' + qb.acq_instr() for vn in qb.int_avg_det.value_names] for qb in [qbc, qbt]} tdma = tda.CPhaseLeakageAnalysis( t_start=timestamp, qb_names=[qbc.name, qbt.name], options_dict={'TwoD': True, 'plot_all_traces': False, 'plot_all_probs': False, 'delegate_plotting': False, 'channel_map': channel_map}) else: tdma = analysis_object cphases = tdma.proc_data_dict[ 'analysis_params_dict'][f'cphase_{qbt.name}']['val'] sweep_pts = list(soft_sweep_points.values())[0]['values'] if tangent_fit: fit_res = lmfit.Model(lambda x, m, b: m*np.tan(x/2-np.pi/2) + b).fit( x=cphases, data=sweep_pts, m=(max(sweep_pts)-min(sweep_pts))/((max(cphases)-min(cphases))), b=np.min(sweep_pts)) elif parfit: fit_res = lmfit.Model(lambda x, m, b, c: m*x + c*x**2 + b).fit( x=cphases, data=sweep_pts, m=(max(sweep_pts)-min(sweep_pts))/((max(cphases)-min(cphases))), c=0.001, b=np.min(sweep_pts)) else: fit_res = lmfit.Model(lambda x, m, b: m*x + b).fit( x=cphases, data=sweep_pts, m=(max(sweep_pts)-min(sweep_pts))/((max(cphases)-min(cphases))), b=np.min(sweep_pts)) plot_and_save_cz_amp_sweep(cphases=cphases, timestamp=timestamp, soft_sweep_params_dict=soft_sweep_points, fit_res=fit_res, save_fig=True, plot_guess=False, qbc_name=qbc.name, qbt_name=qbt.name, **kw) return fit_res
def test_live_fit_plot(fresh_RE): RE = fresh_RE try: import lmfit except ImportError: raise pytest.skip('requires lmfit') def gaussian(x, A, sigma, x0): return A * np.exp(-(x - x0)**2 / (2 * sigma**2)) model = lmfit.Model(gaussian) init_guess = { 'A': 2, 'sigma': lmfit.Parameter('sigma', 3, min=0), 'x0': -0.2 } livefit = LiveFit(model, 'det', {'x': 'motor'}, init_guess, update_every=50) lfplot = LiveFitPlot(livefit, color='r') lplot = LivePlot('det', 'motor', ax=plt.gca(), marker='o', ls='none') RE(scan([det], motor, -1, 1, 50), [lplot, lfplot]) expected = {'A': 1, 'sigma': 1, 'x0': 0} for k, v in expected.items(): assert np.allclose(livefit.result.values[k], v, atol=1e-6)
def test_live_fit_multidim(fresh_RE): RE = fresh_RE try: import lmfit except ImportError: raise pytest.skip('requires lmfit') motor1._fake_sleep = 0 motor2._fake_sleep = 0 det4.exposure_time = 0 def gaussian(x, y, A, sigma, x0, y0): return A * np.exp(-((x - x0)**2 + (y - y0)**2) / (2 * sigma**2)) model = lmfit.Model(gaussian, ['x', 'y']) init_guess = { 'A': 2, 'sigma': lmfit.Parameter('sigma', 3, min=0), 'x0': -0.2, 'y0': 0.3 } cb = LiveFit(model, 'det4', { 'x': 'motor1', 'y': 'motor2' }, init_guess, update_every=50) RE(outer_product_scan([det4], motor1, -1, 1, 10, motor2, -1, 1, 10, False), cb) expected = {'A': 1, 'sigma': 1, 'x0': 0, 'y0': 0} for k, v in expected.items(): assert np.allclose(cb.result.values[k], v, atol=1e-6)
def test_live_fit_plot(RE, hw): try: import lmfit except ImportError: raise pytest.skip("requires lmfit") def gaussian(x, A, sigma, x0): return A * np.exp(-(x - x0)**2 / (2 * sigma**2)) model = lmfit.Model(gaussian) init_guess = { "A": 2, "sigma": lmfit.Parameter("sigma", 3, min=0), "x0": -0.2, } livefit = LiveFit(model, "det", {"x": "motor"}, init_guess, update_every=50) lfplot = LiveFitPlot(livefit, color="r") lplot = LivePlot("det", "motor", ax=plt.gca(), marker="o", ls="none") RE(scan([hw.det], hw.motor, -1, 1, 50), [lplot, lfplot]) expected = {"A": 1, "sigma": 1, "x0": 0} for k, v in expected.items(): assert np.allclose(livefit.result.values[k], v, atol=1e-6)
def train(): body = request.json df=pd.DataFrame(body) N=body["init"].N #data=np.array([body["confirmed"],body["active"],body["recovered"],body["deaths"]]) data=np.array([body["confirmed"],body["active"]]) x =df.index # plt.plot(x,data[1]) # plt.show() print(len(data.T),len(x)) mod = lmfit.Model(mdl) mod.set_param_hint("beta", value=body["init"].beta, vary=True,min=0, max=6) mod.set_param_hint("gamma", value=body["init"].gamma, vary=True,min=0, max=4,) mod.set_param_hint("sigma", value=body["init"].sigma, vary=True,min=0, max=4) mod.set_param_hint("N", value=N, vary=False) params = mod.make_params() result = mod.fit(data, params, method="leastsq", x=x) #result.plot_fit(datafmt="-") #plt.show() print(result.fit_report()) # a=requests.get(apiUrl+"dayone/country/"+body["country"]) # print(a.content) return dict(result.params.valuesdict())
def test_coercion_of_input_data(peakdata, input_dtype, expected_dtype): """Test for coercion of 'data' and 'independent_vars'. - 'data' should become 'float64' or 'complex128' - dtype for 'indepdendent_vars' is only changed when the input is a list, tuple, numpy.ndarray, or pandas.Series """ x, y = peakdata model = lmfit.Model(gaussian) pars = model.make_params() if (not lmfit.minimizer.HAS_PANDAS and input_dtype in ['pandas-real', 'pandas-complex']): return elif input_dtype == 'pandas-real': result = model.fit(lmfit.model.Series(y, dtype=np.float32), pars, x=lmfit.model.Series(x, dtype=np.float32)) elif input_dtype == 'pandas-complex': result = model.fit(lmfit.model.Series(y, dtype=np.complex64), pars, x=lmfit.model.Series(x, dtype=np.complex64)) elif input_dtype == 'list': result = model.fit(y.tolist(), pars, x=x.tolist()) elif input_dtype == 'tuple': result = model.fit(tuple(y), pars, x=tuple(x)) else: result = model.fit(np.asarray(y, dtype=input_dtype), pars, x=np.asarray(x, dtype=input_dtype)) assert result.__dict__['userkws']['x'].dtype == expected_dtype assert result.__dict__['userargs'][0].dtype == expected_dtype
def fit(self): mod = lmfit.Model(self.fitter) self.kabko.apply_params(mod) outbreak_shift = self.kabko.outbreak_shift( self.kabko.params["incubation_period"].init ) days = self.kabko.data_days(outbreak_shift) mod.set_param_hint("days", value=days, vary=False) params = mod.make_params() x_data = np.linspace(0, days - 1, days, dtype=int) y_data = util.shift_array( self.kabko.dead, outbreak_shift ) result = mod.fit(y_data, params, method="least_squares", x=x_data) #result.plot_fit(datafmt="-"); return result
def tip_line_fit(point1, point2, image, length_spacing, line_thickness, width_spacing): """Fit the tip of a line to a complementary error function, 1 - erf(x). Args: point1: 1D array or float. point2: 1D array or float. image: 2D array. length_spacing: float, `get_thick_line()`. line_thickness: float, `get_thick_line()`. width_spacing: float, `get_thick_line()`. """ profile_parameters = {} profile_parameters["length_spacing"] = length_spacing # pixel profile_parameters["line_thickness"] = line_thickness # pixel profile_parameters["width_spacing"] = width_spacing # pixel profile_parameters["normalized_intensities"] = True x_profile, y_profile = line_profile(image, point1, point2, **profile_parameters) def errorfunction(x, mu, sigma, mt, bg): # pylint: disable=no-member return bg + (0.5 * mt * special.erfc((x - mu) / (np.sqrt(2) * sigma))) model = lmfit.Model(errorfunction) fit_params = {} fit_params["mu"] = lmfit.Parameter("mu", value=x_profile[-1] / 2, min=0, max=x_profile[-1]) fit_params["sigma"] = lmfit.Parameter("sigma", value=1, vary=True, min=0, max=x_profile[-1]) fit_params["mt"] = lmfit.Parameter("mt", value=1, vary=True, min=0) fit_params["bg"] = lmfit.Parameter("bg", value=1, vary=True, min=0) fit_result = model.fit(y_profile.copy(), x=x_profile.copy(), **fit_params) return x_profile, y_profile, fit_result, errorfunction
def setup(): fig, ax = plt.subplots() self.ax = ax fig.canvas.set_window_title('Peakup') self.ax.clear() # Setup LiveCallbacks self.live_plot = LivePlot(scaler, dcm_c2_pitch_name, linestyle='', marker='*', color='C0', label='raw', ax=self.ax, use_teleporter=False) # Setup LiveFit # f_gauss(x, A, sigma, x0, y0, m) model = lmfit.Model(f_gauss, ['x']) init_guess = { 'A': lmfit.Parameter('A', 100000, min=0), 'sigma': lmfit.Parameter('sigma', 0.001, min=0), 'x0': lmfit.Parameter('x0', pitch_guess), 'y0': lmfit.Parameter('y0', 0, min=0), 'm': lmfit.Parameter('m', 0, vary=False) } self.lf = LiveFit(model, scaler, {'x': dcm_c2_pitch_name}, init_guess) self.lpf = LiveFitPlot(self.lf, ax=self.ax, color='r', use_teleporter=False, label='Gaussian fit')
def __init__(self, n_peaks, verbose=False, model='Lorentz'): self.mkey = model _func = { 'Lorentz': makefunc_Lorentz_cmplx, 'Voigt': makefunc_Voigt }[model] self.n = n_peaks self.model = lmfit.Model(_func(n_peaks)) self.params = self.model.make_params(verbose=False) # This loop initializes all parameters by 1 and sets standard boundaries depending on the respective parameterset for k in self.params.keys(): self.set_initial_values(k, 1) if k[0] == 'a': self.set_boundaries(k, 0, np.inf) self.set_initial_values(k, 1e6) elif k[0] == 'l': self.set_boundaries(k, 0, np.inf) self.set_initial_values(k, 1e-2) elif k[0] == 'p': self.set_boundaries(k, -180, 180) elif k[0] == 's': self.set_boundaries(k, 0, np.inf) self.set_initial_values(k, 1e-2) else: pass if verbose: self.params.pretty_print()
def simplefit(t, G, err, kappa = 5, triplet = 1): #0 = triplet off, 1 = triplet on # 1-comp diffusion, triplet considered only if selected A0_guess = np.mean(G[:5]) arg = abs(G - A0_guess/2) ind = np.argmin(arg) tau_guess = t[ind] print('Guessing tau = %f ms' %(tau_guess)) model = lmfit.Model(ff.triplet_diff3d) params = model.make_params(A0=A0_guess, tau_diff=tau_guess) if triplet == 1: params['T'].set(min=0, max=1, value=0.1) params['tau_trip'].set(min=1E-9, max=0.05, value=10e-4) else: params['T'].set(value=0, vary=False) params['tau_trip'].set(value=1e-4, vary=False) params['A0'].set(min=0.00001, value=A0_guess) params['tau_diff'].set(min=1e-4, value=tau_guess) params['Ginf'].set(value=0, vary = True) params['kappa'].set(value=kappa, vary=False) # 3D model only weights = 1/err t_max = 1e7 fitres = model.fit(G[t < t_max], timelag=t[t<t_max], params=params, method='least_squares', weights=weights[t<t_max]) print('\nList of fitted parameters for %s: \n' % model.name) fitres.params.pretty_print(colwidth=10, columns=['value', 'stderr', 'min', 'max']) print(fitres.fit_report()) return fitres
def simplefit2(t, G, err, kappa = 5, tau_diff2_fix_value = 0.050): # tau_diff2_fix_value = 0.036 # kappa = 5.9 # two component diffusion model = lmfit.Model(ff.twocomp_diff3d) A0_guess = np.mean(G[:5]) arg = abs(G - A0_guess/2) ind = np.argmin(arg) tau_guess = t[ind] print('Guessing tau = %f ms' %(tau_guess)) params = model.make_params(A0=A0_guess) params['A0'].set(min=0.00001, value = A0_guess) # params['tau_diff1'].set(min=2*tau_diff2_fix_value, value = tau_guess) #slow component params['tau_diff1'].set(min=1e-6, value = tau_guess) #slow component params['tau_diff2'].set(min=1e-6, value = tau_diff2_fix_value, vary = False) #fast component, usually fixed params['p1'].set(min = 0, max = 1, value = 0.5) #fraction slow params['Ginf'].set(value=0, vary = True) params['kappa'].set(value=kappa, vary=False) # 3D model only # weights = np.ones_like(avg_G) weights = 1/err t_max = 1e7 fitres = model.fit(G[t < t_max], timelag=t[t<t_max], params=params, method='least_squares', weights=weights[t<t_max]) print('\nList of fitted parameters for %s: \n' % model.name) fitres.params.pretty_print(colwidth=10, columns=['value', 'stderr', 'min', 'max']) print(fitres.fit_report()) return fitres
def constrained_fitter(minicube, xax, input_parameters, par_minima=None, par_maxima=None, **model_kwargs): """ input_parameters should be a dict """ model = lmfit.Model(multicomp_minicube_model_generator(**model_kwargs), independent_vars=['xax']) params = model.make_params() for par in params: params[par].value = input_parameters[par] if 'amp' in par and par[3] != 'd': params[par].min = 0 elif 'sigma' in par and par[5] != 'd': params[par].min = 0 if par_minima is not None: for mpar in par_minima: if mpar not in params: raise ValueError("Parameter {0} is not in params".format(mpar)) params[mpar].min = par_minima[mpar] if par_maxima is not None: for mpar in par_maxima: if mpar not in params: raise ValueError("Parameter {0} is not in params".format(mpar)) params[mpar].max = par_maxima[mpar] result = model.fit(minicube, xax=xax, params=params) return result
def fit_to_exp(self, rem = 0, plot_rem = False): model = lmf.Model(self.decay_exp) params = model.make_params(amp = 1.0, delta = 0.5) params.add('c', value = 0, vary = False) # res_exp = model.fit(self.Fs, params, x = self.Ls) mod = lmf.models.LinearModel() pars = mod.guess(np.log(self.Fs[:self.Len-rem]), x=self.Ls[:self.Len-rem]) scaling_fit = mod.fit(np.log(self.Fs)[:self.Len-rem] ,pars, x=self.Ls[:self.Len-rem]) fig, ax = plt.subplots(figsize = (6, 3.6)) ax.plot(self.Ls, self.Fs, "o") ax.plot(self.Ls, np.exp(scaling_fit.values["intercept"])*np.exp(scaling_fit.values["slope"]*self.Ls),"C1-") ax.annotate("$\propto exp({:.2f}{})$".format(scaling_fit.values["slope"], self.L_label), (self.Ls[0]*0.1+self.Ls[1]*0.9, self.Fs[0]*0.1 + self.Fs[1]*0.9)) ax.set_xlabel("{}".format(self.L_label)) ax.set_ylabel("{}".format(self.F_label)) ax.tick_params(axis = "y") ax.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.1e')) ax.locator_params(nbins=5) axin = ax.inset_axes([0.35,0.35,0.6,0.6]) scaling_fit.plot_fit(ax = axin, xlabel = "{}".format(self.L_label), ylabel = "log({})".format(self.F_label)) if plot_rem: axin.plot(self.Ls[-3:],np.log(np.abs(self.Fs[-rem:])),"C0o") axin.set_title("") axin.set_xticklabels([]) axin.set_yticklabels([]) axin.tick_params(axis='both', which='both', length=0) axin.get_legend().remove() plt.show()
def read_mu_x(self): self.debug_stream("Calculating the center of the peak in the x axis") def gaussian_paula(x, mu, A, sigma, c): return A * np.exp(-(x - mu)**2 / (2 * sigma**2)) + c mod = lm.Model(gaussian_paula) self.parsx = lm.Parameters() real_data = np.array(self.image_proxy.read().value) self.x_axis = np.mean(real_data, axis=0) #self.x_axis = np.mean(real_data[self.__xposi:self.__xposi+self.__xwind,:], axis = 0) self.x_max = np.max(self.x_axis) self.x_min = np.min(self.x_axis) self.mu = self.x_axis.argmax() self.parsx.add('mu', value=self.mu) self.parsx.add('A', value=self.x_max - self.x_min) self.parsx.add('c', value=self.x_min) self.parsx.add('sigma', value=self.sigma_try) self.debug_stream('parameters fitting x axis data') self.out_gaussx = mod.fit(self.x_axis, self.parsx, x=self.x2) self.debug_stream('fitting x axis done succesfully') self.a = self.out_gaussx.best_values['sigma'] return self.out_gaussx.best_values['mu']
def sinefit(self, dataIn: DataDictBase = None): if dataIn is None: return None # this is just a ghetto example: only support very simple datasets naxes = len(dataIn.axes()) ndeps = len(dataIn.dependents()) if not (naxes == 1 and ndeps == 1): return dict(dataOut=dataIn) # getting the data axname = dataIn.axes()[0] x = dataIn.data_vals(axname) y = dataIn.data_vals(dataIn.dependents()[0]) # try to fit sinemodel = lmfit.Model(sinefunc) p0 = sinemodel.make_params(amp=1, freq=self.frequencyGuess, phase=0) result = sinemodel.fit(y, p0, x=x) # if the fit works, add the fit result to the output dataOut = dataIn.copy() if result.success: dataOut['fit'] = dict(values=result.best_fit, axes=[ axname, ]) dataOut.add_meta('info', result.fit_report()) else: dataOut.add_meta('info', 'Could not fit sine.') return dict(dataOut=dataOut)
def fit_to_pow(self): model = lmf.Model(self.decay_pow) params = model.make_params(amp = 1.0, delta = 0.5) params.add('c', value = 0, vary = False) res_pow = model.fit(self.Fs, params, x = self.Ls) mod = lmf.models.LinearModel() pars = mod.guess(np.log(self.Fs), x=np.log(self.Ls)) scaling_fit = mod.fit(np.log(self.Fs),pars, x=np.log(self.Ls)) fig, ax = plt.subplots(figsize = (6, 3.6)) ax.plot(self.Ls, self.Fs, "o") ax.plot(self.Ls, np.exp(scaling_fit.values["intercept"])*self.Ls**scaling_fit.values["slope"],"C1-") ax.annotate("$\propto {}^{{{:.2f}}}$".format(self.L_label,scaling_fit.values["slope"]), (self.Ls[2]/2+self.Ls[3]/2, self.Fs[2]/2 + self.Fs[3]/2)) ax.set_xlabel("{}".format(self.L_label)) ax.set_ylabel("{}".format(self.F_label)) ax.locator_params(nbins=5) ax.tick_params(axis = "y", labelleft = False) axin = ax.inset_axes([0.4,0.4,0.6,0.6]) scaling_fit.plot_fit(ax = axin, xlabel = "log({})".format(self.L_label), ylabel = "log({})".format(self.F_label)) axin.set_title("") axin.set_xticklabels([]) axin.set_yticklabels([]) axin.tick_params(axis='both', which='both', length=0) axin.get_legend().remove() plt.show()
def test_OptimizerCallback(self): def rosen(params, a=1, b=100, noise=0): """ Rosenbrock function """ v = (a - params[0])**2 + b * (params[1] - params[0]**2)**2 v += noise * (np.random.rand() - .5) return v def objective(x): return rosen(x, 0.01, 1) oc = OptimizerCallback(show_progress=False) result = scipy.optimize.minimize(objective, [.5, .9], callback=oc.scipy_callback) self.assertEqual(result.success, True) self.assertEqual(oc.number_of_evaluations(), result.nit) self.assertIsInstance(oc.data, pandas.DataFrame) oc.clear() lmfit_model = lmfit.Model(linear_function, independent_vars=['x']) xdata = np.linspace(0, 10, 40) data = 10 + 5 * xdata lmfit_model.fit(data, x=xdata, a=1, b=1, iter_cb=oc.lmfit_callback) plt.figure(100) plt.clf() oc.plot(logy=True) plt.close(100)
def fit_to_expstr(self): model = lmf.Model(self.decay_expstr) params = model.make_params(amp = 1.0) params.add('c', value = 0, vary = False) params.add('delta', value = 0.5, min = 0.1) res_expstr = model.fit(self.Fs, params, x = self.Ls) mod = lmf.models.LinearModel() pars = mod.guess(np.log(self.Fs), x=np.sqrt(self.Ls)) scaling_fit = mod.fit(np.log(self.Fs)[:] ,pars, x=np.sqrt(self.Ls[:])) fig, ax = plt.subplots(figsize = (6, 3.6)) ax.plot(self.Ls, self.Fs, "o") ax.plot(self.Ls, np.exp(scaling_fit.values["intercept"])*np.exp(scaling_fit.values["slope"]*np.sqrt(self.Ls)),"C1-") ax.annotate("$\propto exp({:.2f}\sqrt{{{}}})$".format(scaling_fit.values["slope"], self.L_label), (self.Ls[2]*0.1+self.Ls[3]*0.9, self.Fs[2]*0.1 + self.Fs[3]*0.9)) ax.set_xlabel("{}".format(self.L_label)) ax.set_ylabel("{}".format(self.F_label)) ax.locator_params(nbins=5) axin = ax.inset_axes([0.35,0.35,0.6,0.6]) scaling_fit.plot_fit(ax = axin, xlabel = "$\sqrt{{{}}}$".format(self.L_label), ylabel = "log({})".format(self.F_label)) # axin.plot(self.Ls[-3:],np.log(self.Fs[-3:]),"C0o") axin.set_title("") axin.set_xticklabels([]) axin.set_yticklabels([]) axin.tick_params(axis='both', which='both', length=0) axin.get_legend().remove() plt.show()
def prepare_fitting(self): self.fit_dicts = OrderedDict() states = ['0', '1', '+'] for i, state in enumerate(states): yvals = self.proc_data_dict['yvals_{}'.format(state)] xvals = self.proc_data_dict['xvals'] mod = lmfit.Model(fit_mods.idle_error_rate_exp_decay) mod.guess = fit_mods.idle_err_rate_guess.__get__( mod, mod.__class__) # Done here explicitly so that I can overwrite a specific guess guess_pars = mod.guess(N=xvals, data=yvals) vary_N2 = self.options_dict.get('vary_N2', True) if not vary_N2: guess_pars['N2'].value = 1e21 guess_pars['N2'].vary = False # print(guess_pars) self.fit_dicts['fit {}'.format(states[i])] = { 'model': mod, 'fit_xvals': { 'N': xvals }, 'fit_yvals': { 'data': yvals }, 'guess_pars': guess_pars }