Ejemplo n.º 1
0
def fit_voigt_over_linear(q,
                          I,
                          cen=1,
                          sig=0.002,
                          sigmin=1e-4,
                          sigmax=0.01,
                          amplmin=0,
                          amplmax=500,
                          trim=0.06,
                          plot=False):

    trim = logical_and(q < cen + trim, q > cen - trim)
    q = q[trim]
    I = I[trim]

    mod = LinearModel()
    mod.set_param_hint('slope', value=-20)
    mod.set_param_hint('intercept', value=10)
    lineout = mod.fit(I, x=q)
    pars = lineout.params

    mod += VoigtModel()
    pars.add('center', value=cen)
    pars.add('sigma', value=sig, max=sigmax, min=sigmin)
    pars.add('amplitude',
             value=amplmin / 2 + amplmax / 2,
             min=amplmin,
             max=amplmax)
    out = mod.fit(I, pars, x=q)

    return out
Ejemplo n.º 2
0
    def _setup_model(self):
        lin = LinearModel(prefix='Lin_')

        model = lin

        psi_min, psi_max = self._f.get_domain()[0], self._f.get_domain()[-1]
        slope = (self._f(psi_max) - self._f(psi_min)) / (psi_max - psi_min)

        lin.set_param_hint('intercept', value=self._f(0))
        lin.set_param_hint('slope', value=slope)

        params = lin.make_params()

        return model, params
Ejemplo n.º 3
0
    def _create_model(self, num_comps=1, guess=True):
        lin = LinearModel()
        lin.set_param_hint('slope', vary=False)
        lin.set_param_hint('intercept', vary=False)
        pars = lin.make_params(slope=0, intercept=0)
        mod = lin

        for i in range(num_comps):
            g = MyGaussianModel(prefix='g{}_'.format(i + 1))
            g.set_param_hint('amplitude', min=0.)

            if guess:
                pars.update(g.guess(self['flux'], self['vel'], w=2))
            else:
                pars.update(
                    g.make_params(amplitude=0.15, center=0, sigma=200 / 2.35))

            mod = mod + g

        return mod, pars
Ejemplo n.º 4
0
def CreateBaseModel(varyCont=False, varySlope=False):
    lin = LinearModel()
    lin.set_param_hint('slope', vary=varySlope)
    lin.set_param_hint('intercept', vary=varyCont)
    pars = lin.make_params(slope=0, intercept=0)
    return lin, pars
Ejemplo n.º 5
0
                       index=hall.index)
hall['n'] = pd.Series(
    [field / hall['Rh'].values[i] / e for i in range(len(hall))],
    index=hall.index)

hall.to_csv(output_folder + month[:3] + day + prefix + '_preliminary_hall_' +
            str(int(field * 1000)) + 'mT.csv')

x_fit_min = int(len(hall) / 3)
x_fit_max = int(2 * len(hall) / 3)

x_fit = [hall.index[i] for i in range(x_fit_min, x_fit_max)]
y_fit = [hall.n.values[i] for i in range(x_fit_min, x_fit_max)]

linmod = LinearModel()
linmod.set_param_hint('intercept', value=1e16)
linmod.set_param_hint('slope', value=1e16)
linout = linmod.fit(y_fit, x=x_fit)

print('equation y = ax+b')
print('a = %s 10¹² cm⁻²/V' % np.round(linout.params['slope'].value / 1e16))
print('b = %s 10¹² cm⁻²' % np.round(linout.params['intercept'].value / 1e16))

y_t = np.linspace(int(hall.index.min()), int(hall.index.max()), 10)

plt.figure(figsize=(6, 6), dpi=120)

slope = (linout.params['slope'].value)
intercept = (linout.params['intercept'].value)

plt.plot(hall.index, (hall.n) / 1e16,
Ejemplo n.º 6
0
def fit_scan(scan, model_dict, fit_from=None, fit_to=None):
    if fit_from is None:
        fit_from = -np.inf
    if fit_to is None:
        fit_to = np.inf

    y_fit = scan["counts"]
    x_fit = scan[scan["scan_motor"]]

    # apply fitting range
    fit_ind = (fit_from <= x_fit) & (x_fit <= fit_to)
    y_fit = y_fit[fit_ind]
    x_fit = x_fit[fit_ind]

    model = None
    for model_index, (model_name,
                      model_param) in enumerate(model_dict.items()):
        model_name, _ = model_name.split("-")
        prefix = f"f{model_index}_"

        if model_name == "linear":
            _model = LinearModel(prefix=prefix)
        elif model_name == "gaussian":
            _model = GaussianModel(prefix=prefix)
        elif model_name == "voigt":
            _model = VoigtModel(prefix=prefix)
        elif model_name == "pvoigt":
            _model = PseudoVoigtModel(prefix=prefix)
        else:
            raise ValueError(f"Unknown model name: '{model_name}'")

        _init_guess = _model.guess(y_fit, x=x_fit)

        for param_index, param_name in enumerate(model_param["param"]):
            param_hints = {}
            for hint_name in ("value", "vary", "min", "max"):
                tmp = model_param[hint_name][param_index]
                if tmp is None:
                    param_hints[hint_name] = getattr(
                        _init_guess[prefix + param_name], hint_name)
                else:
                    param_hints[hint_name] = tmp

            if "center" in param_name:
                if np.isneginf(param_hints["min"]):
                    param_hints["min"] = np.min(x_fit)

                if np.isposinf(param_hints["max"]):
                    param_hints["max"] = np.max(x_fit)

            if "sigma" in param_name:
                if np.isposinf(param_hints["max"]):
                    param_hints["max"] = np.max(x_fit) - np.min(x_fit)

            _model.set_param_hint(param_name, **param_hints)

        if model is None:
            model = _model
        else:
            model += _model

    weights = [1 / np.sqrt(val) if val != 0 else 1 for val in y_fit]
    scan["fit"] = model.fit(y_fit, x=x_fit, weights=weights)