Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
def gauss_step_const(signal, guess):
    """
    Fits high contrast data very well
    """
    if guess == False:
        return [0, 0]
    else:
        amp, centre, stdev, offset = guess
        
        data = np.array([range(len(signal)), signal]).T
        X = data[:,0]
        Y = data[:,1]

#         gauss_mod = Model(gaussian)
        gauss_mod = Model(gaussian)
        const_mod = ConstantModel()
        step_mod = StepModel(prefix='step')
        
        pars = gauss_mod.make_params(height=amp, center=centre, width=stdev / 3., offset=offset)
#         pars = gauss_mod.make_params(amplitude=amp, center=centre, sigma=stdev / 3.)
        gauss_mod.set_param_hint('sigma', value = stdev / 3., min=stdev / 2., max=stdev)
        pars += step_mod.guess(Y, x=X, center=centre)

        pars += const_mod.guess(Y, x=X)
    
        
        mod = const_mod + gauss_mod + step_mod
        result = mod.fit(Y, pars, x=X)
        # write error report
        #print result.fit_report()
        print "contrast fit", result.redchi
    
    return X, result.best_fit, result.redchi
Ejemplo n.º 3
0
 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)
Ejemplo n.º 5
0
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}'
    )
Ejemplo n.º 6
0
def find_fit_sigmoid(x, y):
    model_gompertz = lm.models.Model(gompertz)
    params_gompertz = lm.Parameters()
    params_gompertz.add('asymptote', value=1E-3, min=1E-8)
    params_gompertz.add('displacement', value=1E-3, min=1E-8)
    params_gompertz.add('step_center', value=1E-3, min=1E-8)

    result_gompertz = model_gompertz.fit(y, params_gompertz, x=x)

    step_mod = StepModel(form='erf', prefix='step_')
    line_mod = LinearModel(prefix='line_')

    params_stln = line_mod.make_params(intercept=y.min(), slope=0)
    params_stln += step_mod.guess(y, x=x, center=90)

    model_stln = step_mod + line_mod
    result_stln = model_stln.fit(y, params_stln, x=x)

    ret_result = None
    ret_model = None

    if result_stln.chisqr < result_gompertz.chisqr:
        ret_result = result_stln
        ret_model = model_stln
    else:
        ret_result = result_gompertz
        ret_model = model_gompertz

    return ret_result, ret_model
Ejemplo n.º 7
0
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()
Ejemplo n.º 8
0
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")
Ejemplo n.º 9
0
def test_stepmodel_erf():
    x, y = get_data()
    stepmod = StepModel(form='linear')
    const = ConstantModel()
    pars = stepmod.guess(y, x)
    pars = pars + const.make_params(c=3*y.min())
    mod = stepmod + const

    out = mod.fit(y, pars, x=x)

    assert(out.nfev > 5)
    assert(out.nvarys == 4)
    assert(out.chisqr > 1)
    assert(out.params['c'].value > 3)
    assert(out.params['center'].value > 1)
    assert(out.params['center'].value < 4)
    assert(out.params['amplitude'].value > 50)
    assert(out.params['sigma'].value > 0.2)
    assert(out.params['sigma'].value < 1.5)
Ejemplo n.º 10
0
def test_stepmodel_erf():
    x, y = get_data()
    stepmod = StepModel(form='linear')
    const = ConstantModel()
    pars = stepmod.guess(y, x)
    pars = pars + const.make_params(c=3 * y.min())
    mod = stepmod + const

    out = mod.fit(y, pars, x=x)

    assert (out.nfev > 5)
    assert (out.nvarys == 4)
    assert (out.chisqr > 1)
    assert (out.params['c'].value > 3)
    assert (out.params['center'].value > 1)
    assert (out.params['center'].value < 4)
    assert (out.params['amplitude'].value > 50)
    assert (out.params['sigma'].value > 0.2)
    assert (out.params['sigma'].value < 1.5)
Ejemplo n.º 11
0
def Step(signal, guess):
    
    if guess == False:
        return [0, 0, 0]
    else:
        amp, centre, stdev, offset = guess
        
        data = np.array([range(len(signal)), signal]).T
        X = data[:,0]
        Y = data[:,1]

        step_mod = StepModel(prefix='step')
        const_mod = ConstantModel(prefix='const_')
        
        pars = step_mod.guess(Y, x=X, center=centre)
        pars += const_mod.guess(Y, x=X)

        mod = step_mod + const_mod
        result = mod.fit(Y, pars, x=X)
        # write error report
        #print result.fit_report()
        
    return X, result.best_fit, result.redchi, 0
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
def GaussStepConst(signal, guess):
    """
    Fits high contrast data very well
    """
    if guess == False:
        return [0, 0, 0]
    else:
        amp, centre, stdev, offset = guess
        
        data = np.array([range(len(signal)), signal]).T
        X = data[:,0]
        Y = data[:,1]

#         gauss_mod = Model(gaussian)
        gauss_mod = Model(gaussian)
        const_mod = ConstantModel()
        step_mod = StepModel(prefix='step')
        
        gauss_mod.set_param_hint('width', value = stdev / 2., min=stdev / 3., max=stdev)
        gauss_mod.set_param_hint('fwhm', expr='2.3548*width')
        pars = gauss_mod.make_params(height=amp, center=centre, width=stdev / 2., offset=offset)
        
        pars += step_mod.guess(Y, x=X, center=centre)

        pars += const_mod.guess(Y, x=X)
        
        pars['width'].vary = False
        
        mod = const_mod + gauss_mod + step_mod
        result = mod.fit(Y, pars, x=X)
        # write error report
        #print result.fit_report()
        
        fwhm = result.best_values['width'] * 2.3548
        
    return X, result.best_fit, result.redchi, fwhm
# <examples/doc_builtinmodels_stepmodel.py>
import matplotlib.pyplot as plt
import numpy as np

from lmfit.models import LinearModel, StepModel

x = np.linspace(0, 10, 201)
y = np.ones_like(x)
y[:48] = 0.0
y[48:77] = np.arange(77-48)/(77.0-48)
np.random.seed(0)
y = 110.2 * (y + 9e-3*np.random.randn(x.size)) + 12.0 + 2.22*x

step_mod = StepModel(form='erf', prefix='step_')
line_mod = LinearModel(prefix='line_')

pars = line_mod.make_params(intercept=y.min(), slope=0)
pars += step_mod.guess(y, x=x, center=2.5)

mod = step_mod + line_mod
out = mod.fit(y, pars, x=x)

print(out.fit_report())

plt.plot(x, y, 'b')
plt.plot(x, out.init_fit, 'k--', label='initial fit')
plt.plot(x, out.best_fit, 'r-', label='best fit')
plt.legend(loc='best')
plt.show()
# <end examples/doc_builtinmodels_stepmodel.py>
Ejemplo n.º 15
0
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
Ejemplo n.º 16
0
# <examples/doc_builtinmodels_stepmodel.py>
import matplotlib.pyplot as plt
import numpy as np

from lmfit.models import LinearModel, StepModel

x = np.linspace(0, 10, 201)
y = np.ones_like(x)
y[:48] = 0.0
y[48:77] = np.arange(77-48)/(77.0-48)
np.random.seed(0)
y = 110.2 * (y + 9e-3*np.random.randn(len(x))) + 12.0 + 2.22*x

step_mod = StepModel(form='erf', prefix='step_')
line_mod = LinearModel(prefix='line_')

pars = line_mod.make_params(intercept=y.min(), slope=0)
pars += step_mod.guess(y, x=x, center=2.5)

mod = step_mod + line_mod
out = mod.fit(y, pars, x=x)

print(out.fit_report())

plt.plot(x, y, 'b')
plt.plot(x, out.init_fit, 'k--')
plt.plot(x, out.best_fit, 'r-')
plt.show()
# <end examples/doc_builtinmodels_stepmodel.py>
Ejemplo n.º 17
0
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')