Пример #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
def fit_gaussian_peak_step_background_2(x, y):
    # create data from broadened step
    npts = 201
    x = np.linspace(0, 10, npts)
    y = step(x, amplitude=12.5, center=4.5, sigma=0.88, form='erf')
    y = y + np.random.normal(size=npts, scale=0.35)
    # create Composite Model using the custom convolution operator
    mod = CompositeModel(Model(jump), Model(gaussian), convolve)
    pars = mod.make_params(amplitude=1, center=3.5, sigma=1.5, mid=5.0)
    # '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 = mod.fit(y, params=pars, x=x)

    print(result.fit_report())

    plot_components = True

    # plot results
    plt.plot(x, y, 'bo')
    if plot_components:
        # generate components
        comps = result.eval_components(x=x)
        plt.plot(x, 10 * comps['jump'], 'k--')
        plt.plot(x, 10 * comps['gaussian'], 'r-')
    else:
        plt.plot(x, result.init_fit, 'k--')
        plt.plot(x, result.best_fit, 'r-')
    plt.show()
    # #<end examples/model_doc3.py>

    return fit_fwhm, fit_center, fwhm_err
    o[imid:] = 1.0
    return o

def convolve(arr, kernel):
    # simple convolution of two arrays
    npts = min(len(arr), len(kernel))
    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]
#
# create Composite Model using the custom convolution operator
mod  = CompositeModel(Model(jump), Model(gaussian), convolve)

pars = mod.make_params(amplitude=1, center=3.5, sigma=1.5, mid=5.0)

# '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 =  mod.fit(y, params=pars, x=x)

print(result.fit_report())

plot_components = False

# plot results
plt.plot(x, y,         'bo')
if plot_components:
Пример #4
0
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.

fig, ax = plt.subplots(1, 2)
# First subplot
for i in range(nb_q_values):
    xx = f_5A['x'][i]
    ax[0].plot(xx,
               QENSmodels.sqwWaterTeixeira(xx,
                                           data_5A['q'][i],
                                           scale=1,
                                           center=0,
                                           D=1,
def convolve(arr, kernel):
    # simple convolution of two arrays
    npts = min(len(arr), len(kernel))
    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]


#
# create Composite Model using the custom convolution operator
mod = CompositeModel(Model(jump), Model(gaussian), convolve)

pars = mod.make_params(amplitude=1, center=3.5, sigma=1.5, mid=5.0)

# '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 = mod.fit(y, params=pars, x=x)

print(result.fit_report())

plot_components = False

# plot results
plt.plot(x, y, 'bo')
if plot_components:
Пример #6
0
    return o


def convolve(arr, kernel):
    """Simple convolution of two arrays."""
    npts = min(arr.size, kernel.size)
    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]


# create Composite Model using the custom convolution operator
mod = CompositeModel(Model(jump), Model(gaussian), convolve)
pars = mod.make_params(amplitude=.01, center=0, sigma=.5, mid=0.5)

# '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 = mod.fit(y, params=pars, x=x)

print(result.fit_report())

# generate components
comps = result.eval_components(x=x)

# plot results
fig, axes = plt.subplots(1, 2, figsize=(12.8, 4.8))