Пример #1
0
def fit_composit(seq, *params, report=False):
    x = np.arange(len(seq))
    y = seq
    amp, cen, wid = params
    cmodel = CompositeModel(Model(gaussian), Model(LognormalModel),
                            ExponentialGaussianModel)
    pars = cmodel.make_params(amplitude=amp, center=cen, sigma=wid, mid=cen)
    # 'mid' and 'center' should be completely correlated, and 'mid' is
    # used as an integer index, so a very poor fit variable:
    pars['mid'].vary = False
    # fit this model to data array y
    result = cmodel.fit(y, params=pars, x=x)
    # limit the amplitude to be in 0-256
    cmodel.set_param_hint('amp', min=0)
    cmodel.set_param_hint('amp', max=256)
    result = gmodel.fit(y, x=x, amp=amp, cen=cen, wid=wid)
    if result.redchi >= 1e3:
        #    if report:
        plt.figure()
        plt.plot(x, y, 'bo')
        #        plt.plot(x, result.init_fit, 'k--')
        plt.plot(x, np.ceil(result.best_fit), 'r-')
        plt.title('chi2: {0:.2e} - red_chi2: {0:.2e}'.format(
            result.chisqr, result.redchi))
        plt.show()
    return result
Пример #2
0
    pad = np.ones(npts)
    tmp = np.concatenate((pad * arr[0], arr, pad * arr[-1]))

    out = np.convolve(tmp, kernel, mode='valid')
    noff = int((len(out) - npts) / 2)
    return out[noff:noff + npts]


model = CompositeModel(Model(irf_gate), Model(QENSmodels.sqwWaterTeixeira),
                       convolve)

print('Names of parameters:', model.param_names)
print('Independent variable(s):', model.independent_vars)

# Define boundaries for parameters to be refined
model.set_param_hint('scale', min=0, max=100)
model.set_param_hint('center', min=-0.1, max=0.1)
model.set_param_hint('D', min=0.05, max=0.25)
model.set_param_hint('resTime', min=0, max=1)
model.set_param_hint('radius', min=0.9, max=1.1)
model.set_param_hint('DR', min=0, max=1)

# Fix some of the parameters
model.set_param_hint('q', vary=False)
model.set_param_hint('spectrum_nb', vary=False)

params = model.make_params()

# Plot of the fitting models without and convoluted with the resolution function
# The values of the parameters are specified below.
# Therefore they could be different from those used in the fitting.