def alignment_plot(self, yt, pitch, yf): '''Make a pretty, three-panel plot at the end of an auto-alignment''' BMMuser = user_ns['BMMuser'] close_all_plots() fig = plt.figure(tight_layout=True) #, figsize=(9,6)) gs = gridspec.GridSpec(1,3) if self.orientation == 'parallel': motor = 'xafs_y' else: motor = 'xafs_x' t = fig.add_subplot(gs[0, 0]) tt = user_ns['db'][yt].table() yy = tt[motor] signal = tt['It']/tt['I0'] if float(signal[2]) > list(signal)[-2] : ss = -(signal - signal[2]) self.inverted = 'inverted ' else: ss = signal - signal[2] self.inverted = '' mod = StepModel(form='erf') pars = mod.guess(ss, x=numpy.array(yy)) out = mod.fit(ss, pars, x=numpy.array(yy)) t.scatter(yy, out.data) t.plot(yy, out.best_fit, color='red') t.scatter(out.params['center'].value, out.params['amplitude'].value/2, s=120, marker='x', color='green') t.set_xlabel(f'{motor} (mm)') t.set_ylabel(f'{self.inverted}data and error function') p = fig.add_subplot(gs[0, 1]) tp = user_ns['db'][pitch].table() xp = tp['xafs_pitch'] signal = tp['It']/tp['I0'] target = signal.idxmax() p.plot(xp, signal) p.scatter(xp[target], signal.max(), s=120, marker='x', color='green') p.set_xlabel('xafs_pitch (deg)') p.set_ylabel('It/I0') p.set_title(f'alignment of spinner {self.current()}') f = fig.add_subplot(gs[0, 2]) tf = user_ns['db'][yf].table() yy = tf[motor] signal = (tf[BMMuser.xs1] + tf[BMMuser.xs2] + tf[BMMuser.xs3] + tf[BMMuser.xs4]) / tf['I0'] #if BMMuser.element in ('Zr', 'Sc', 'Nb'): # com = signal.idxmax() # centroid = yy[com] #else: com = int(center_of_mass(signal)[0])+1 centroid = yy[com] f.plot(yy, signal) f.scatter(centroid, signal[com], s=120, marker='x', color='green') f.set_xlabel(f'{motor} (mm)') f.set_ylabel('If/I0') fig.canvas.draw() fig.canvas.flush_events() plt.show()
def align_linear(self, force=False, drop=None): '''Fit an error function to the linear scan against It. Plot the result. Move to the centroid of the error function.''' if self.orientation == 'parallel': motor = user_ns['xafs_liny'] else: motor = user_ns['xafs_linx'] yield from linescan(motor, 'it', -2.3, 2.3, 51, pluck=False) close_last_plot() table = user_ns['db'][-1].table() yy = table[motor.name] signal = table['It']/table['I0'] if drop is not None: yy = yy[:-drop] signal = signal[:-drop] if float(signal[2]) > list(signal)[-2] : ss = -(signal - signal[2]) self.inverted = 'inverted ' else: ss = signal - signal[2] self.inverted = '' mod = StepModel(form='erf') pars = mod.guess(ss, x=numpy.array(yy)) out = mod.fit(ss, pars, x=numpy.array(yy)) print(whisper(out.fit_report(min_correl=0))) target = out.params['center'].value yield from mv(motor, target) self.y_plot(yy, out)
def align_y(self, force=False, drop=None): '''Fit an error function to the xafs_y scan against It. Plot the result. Move to the centroid of the error function.''' xafs_y = user_ns['xafs_y'] db = user_ns['db'] yield from linescan(xafs_y, 'it', -1, 1, 31, pluck=False) close_last_plot() table = db[-1].table() yy = table['xafs_y'] signal = table['It'] / table['I0'] if drop is not None: yy = yy[:-drop] signal = signal[:-drop] if float(signal[2]) > list(signal)[-2]: ss = -(signal - signal[2]) self.inverted = 'inverted ' else: ss = signal - signal[2] self.inverted = '' mod = StepModel(form='erf') pars = mod.guess(ss, x=numpy.array(yy)) out = mod.fit(ss, pars, x=numpy.array(yy)) print(whisper(out.fit_report(min_correl=0))) self.y_plot(yy, out) target = out.params['center'].value yield from mv(xafs_y, target)
def wafer_edge(motor='x'): '''Fit an error function to the linear scan against It. Plot the result. Move to the centroid of the error function.''' if motor == 'x': motor = user_ns['xafs_linx'] else: motor = user_ns['xafs_liny'] yield from linescan(motor, 'it', -2, 2, 41, pluck=False) close_last_plot() table = user_ns['db'][-1].table() yy = table[motor.name] signal = table['It'] / table['I0'] if float(signal[2]) > list(signal)[-2]: ss = -(signal - signal[2]) else: ss = signal - signal[2] mod = StepModel(form='erf') pars = mod.guess(ss, x=numpy.array(yy)) out = mod.fit(ss, pars, x=numpy.array(yy)) print(whisper(out.fit_report(min_correl=0))) out.plot() target = out.params['center'].value yield from mv(motor, target) yield from resting_state_plan() print( f'Edge found at X={user_ns["xafs_x"].position} and Y={user_ns["xafs_y"].position}' )
def fitlogistic(x, y, dias): # fit a logistic function model = StepModel(form="logistic") # parameters to fit guesses by lmfit parameters = model.guess(y, x=x) output = model.fit(y, parameters, x=x) amplitude = output.params["amplitude"].value amplitude = math.floor(amplitude) center = output.params["center"].value sigma = output.params["sigma"].value fit = [] xfit = [] cumulative = [] for i in range(61, dias): if i == 61: xfit.append(i) alpha = (i - center) / sigma value = amplitude * (1 - (1 / (1 + math.exp(alpha)))) fit.append(value) cumulative.append(0) else: xfit.append(i) alpha = (i - center) / sigma value = amplitude * (1 - (1 / (1 + math.exp(alpha)))) fit.append(value) c = value - fit[i - 62] cumulative.append(c) return amplitude, center, sigma, xfit, fit, cumulative, output.fit_report()
def get_zero_model(): xdata = np.linspace(-100, 200, 30) ydata = np.zeros(301) model = StepModel(form='linear', prefix='step_') zero_model = model.fit(ydata, x=xdata) return zero_model
def predictive_model(data: pd.DataFrame, interesting_rows, day_zero_n_patients: int = 20, days_in_future: int = 30, aggregated: bool = False): data = data[interesting_rows].iloc[:, :] from lmfit.models import StepModel, ExponentialModel fig = plt.figure(figsize=(10, 5)) for c in range(len(data.index)): if aggregated: values = data.values[c, 4:][data.iloc[c, 4:] > day_zero_n_patients] else: values = np.concatenate( ([0], np.diff( data.values[c, 4:][data.iloc[c, 4:] > day_zero_n_patients]))) n = values.shape[0] x = np.asarray(range(values.shape[0]), dtype='float64') y = np.asarray(values, dtype='float64') if len(x) == 0: continue label = "{}-{}".format(data.values[c, 0], data.values[c, 1]) plt.plot(x, y, label=label) if data.values[c, 1] in ["China", "US"]: continue try: model_step = StepModel() model_exp = ExponentialModel() params_step = model_step.guess(y, x=x) params_exp = model_exp.guess(y, x=x) result_step = model_step.fit(y, params_step, x=x) result_exp = model_exp.fit(y, params_exp, x=x) except Exception: continue x_pred = np.asarray(range(days_in_future)) plt.plot(x_pred, model_step.eval(result_step.params, x=x_pred), ':', label='fit-{}'.format(label)) plt.plot(x_pred, model_exp.eval(result_exp.params, x=x_pred), '.', label='fit-{}'.format(label)) # print(result.fit_report()) # result.plot_fit() plt.legend(prop={"size": 7}) plt.yscale('log') plt.xticks(rotation=45) plt.grid(which='both') now = datetime.now() dt_string = now.strftime("%d%m%Y-%H%M%S")
def get_fit(df, country): x, y = df[df[country] > 0][country].index.values, df[ df[country] > 0][country].values mod = StepModel(form='logistic') pars = mod.guess(y, x=x) # Give no weight # fit = mod.fit(y, pars, x=x) # Give weight to highest points # fit = mod.fit(y, pars, x=x, weights=(1 / (y + 1e-3))[::-1]) # Or give weight to newest points fit = mod.fit(y, pars, x=x, weights=(1 / (x + 1e-3))[::-1]) # Or give weight to least and highest points using sech # y_max = y.max() # coe = 10 / y_max # fit = mod.fit(y, pars, x=x, weights=(1 - 1/np.cosh(coe*(y - y_max / 2)))) # Or give weight to least and highest points using polynomial # y_max = y.max() # fit = mod.fit(y, pars, x=x, weights=pow(y - y_max / 2, 4) / pow(y_max / 2, 4)) return fit
def vec_latency_VX_vs_T(traces, participants, conditions, journal, sw_c=['NS', 'NS'], sw_e=['PS', 'AS'], adj_axis=300, crit=0.01, Out_crit=1.5, close_policy=False, fig_width=12): def nan_helper(y): """ Helper to handle indices and logical indices of NaNs. Main reason of that code ? => Avoid errors when using lmfit see use below for an example """ return np.isnan(y), lambda z: z.nonzero()[0] for cond in conditions: # excluded participants if cond != 'Healthy': subjs = participants['ctl'][cond][participants['ctl'][cond] != 2] else: subjs = participants['ctl'][cond] for s in subjs: fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width / 1.6180)) for c, switches, col_code, VA_col in zip( ['ctl', 'exp'], [sw_c, sw_e], [['turquoise', 'b'], ['r', 'g']], ['b', 'k']): latencies = [] for side, col, l_style in zip(['left', 'right'], col_code, ['-', '--']): Y = np.nanmean(np.concatenate( (traces[c][cond][s][switches[0]][side], traces[c][cond][s][switches[1]][side]), axis=0), axis=0) X = np.arange(Y.size) - adj_axis # Y interpolation of NaNs nans, x = nan_helper(Y) Y[nans] = np.interp(x(nans), x(~nans), Y[~nans]) #_ = ax.plot(X, Y, color=col, linestyle=l_style, # linewidth=3, label='mean {} side velocity'.format(side)) #error_vec = Out_crit*np.nanstd(np.concatenate((traces[c][cond][s][switches[0]][side], # traces[c][cond][s][switches[1]][side]), axis=0), axis=0) # _ = ax.fill_between(X, Y-error_vec, Y+error_vec, facecolor=col, alpha=0.3) # Mean trace smoothing using Levenberg–Marquardt algorithm mod = StepModel(form='erf') pars = mod.guess(Y, x=X) out = mod.fit(Y, pars, x=X) ax.plot(np.asarray(X), out.best_fit, color=col) current = np.concatenate( (traces[c][cond][s][switches[0]][side], traces[c][cond][s][switches[1]][side]), axis=0).shape[0] sum_of = np.concatenate( (traces[c][cond][s][switches[0]]['left'], traces[c][cond][s][switches[1]]['left']), axis=0).shape[0] + np.concatenate( (traces[c][cond][s][switches[0]]['right'], traces[c][cond][s][switches[1]]['right']), axis=0).shape[0] perc_side = current / sum_of list_l = [] for tps in range(len(X)): if perc_side * abs(out.best_fit[tps]) > crit: list_l.append(X[tps]) if len(list_l) != 0: latencies.append(list_l[0]) else: ax.text(adj_axis, 1.5, "NO LATENCY FOUND !", color='r', fontsize=15) ax.axvline(np.mean(latencies), color=VA_col, linewidth=3, label='latency {} = {} ms'.format( c, np.mean(latencies))) ax.set_ylabel('Smooth eye velocity (°/s)', fontsize=14) ax.set_xlabel('Time', fontsize=11) ax.set_xlim([-100, 1000]) ax.set_ylim([-8, 8]) _ = ax.set_title('{} {} dep rule = {}'.format( cond, int(s), journal[c][cond][s][:, 1][0][0])) _ = ax.legend(bbox_to_anchor=(1.01, 1), loc=2, borderaxespad=0.) if close_policy: plt.close('all')
def get_county_fit(df, tp): x, y = df.index.values, df[tp].values mod = StepModel(form='logistic') pars = mod.guess(y, x=x) fit = mod.fit(y, pars, x=x, weights=(1 / (x + 1e-3))[::-1]) return fit
spark_prices = spark_prices[0:50] utilities = utilities[0:50] # In[46]: df = pd.DataFrame({"sparks": spark_prices, "utilities": utilities}) df = df.sort_values(by="sparks") df = df.round(2) spark_prices = list(df["sparks"]) utilities = list(df["utilities"]) # In[47]: model = StepModel(form='linear', prefix='step_') fitted_model = model.fit(utilities, x=spark_prices) # print results # plot data and best-fit fitted_model.plot() # In[64]: lmodel = Model(two_lines) # In[74]: params = lmodel.make_params(offset1=0, slope1=0, offset2=0, slope2=20,