Esempio n. 1
0
def make_gauss(folder):

    df = pd.read_csv(folder + "data.csv")
    print(df)
    # print(df['20.0'])
    df.drop(["Unnamed: 0", "t"], axis=1, inplace=True)

    # print(df)

    xs = np.array([float(x) for x in df.columns])
    ys = np.array([sum(df[x]) for x in df.columns])

    def f(x, sigma, amp, mu, base):
        return base + amp * np.exp(-((x - mu) / sigma)**2 / 2)

    gmodel = models.Model(f)
    # params = gmodel.guess(ys, x=xs)
    params = Parameters()
    params.add_many(('sigma', 5, True, 1), ('amp', 1000, True, 1),
                    ('mu', 35, True, 1), ('base', 200, True, 1))
    fit_result = gmodel.fit(ys, params=params, x=xs)
    print(folder)
    print(fit_result.fit_report())
    print()

    ax.plot(xs, ys, label=folder[:-1])
    ax.plot(xs, fit_result.eval(), label=folder[:-1] + 'fit')
Esempio n. 2
0
#         binwidths.append(round(binwidth, 5))

# print(binwidths)

max_index_ijk = np.argmax(counts_ijk)
max_pulseheight_ijk = pulseheights_ijk[max_index_ijk]

max_pulseheight_ijk_err = max_pulseheight_ijk - pulseheights_ijk[max_index_ijk
                                                                 - 1]

print(
    f'The maximum pulseheight is {max_pulseheight_ijk:.3f} +/- {max_pulseheight_ijk_err:.3f}'
)

f = lambda x, a: a * x
model = models.Model(f, name="linear_origin")

max_energy_alpha = 5.4857

df = pd.DataFrame([
    [max_energy_alpha, max_pulseheight_ijk, max_pulseheight_ijk_err],
],
                  columns=['Energy', 'Pulseheight', 'PH_err'])

fit = model.fit(df['Pulseheight'],
                x=df['Energy'],
                weights=1 / df['PH_err'],
                a=40)
# fit.plot()
# plt.show()
Esempio n. 3
0
def cluster_AP_analysis(progression_df: pd.DataFrame, sil_calc=False, refit=False, **kwargs):
    """
        Wrapper for the AffinityPropagation cluster method from
        scikit-learn.

        The dataframe provided will also receive new columns: Cluster index,
        and Silhouette. The latter corresponds to how likely a sample is
        sandwiched between clusters (0), how squarely it belongs in the
        assigned cluster (+1), or does not belong (-1). The cluster index
        corresponds to which cluster the sample belongs to.

        parameters:
        ---------------
        progression_df - pandas dataframe taken from the result of progression
                         fits
        sil_calc - bool indicating whether silhouettes are calculated
                   after the AP model is fit
        
        returns:
        --------------
        data - dict containing clustered frequencies and associated fits
        ap_obj - AffinityPropagation object containing all the information
                 as attributes.
    """
    ap_options = dict()
    ap_options.update(**kwargs)
    ap_obj = AffinityPropagation(**ap_options)
    # Determine clusters based on the RMS, B, and D similarities
    # Remove occurrences of NaN in the three columns
    progression_df.dropna(subset=["RMS", "B", "D"], inplace=True)
    # Fit a random 40% subset of the B, D, results to the cluster model
    ap_obj.fit(progression_df.sample(frac=0.4)[["RMS", "B", "D"]])
    # Predict the clusters
    progression_df["Cluster indices"] = ap_obj.predict(
        progression_df[["RMS", "B", "D"]]
    )
    # Indicate which progression fits with which cluster
    #progression_df["Cluster indices"] = ap_obj.labels_
    # Calculate the metric for determining how well a sample
    # fits into its cluster
    if sil_calc is True:
        progression_df["Silhouette"] = silhouette_samples(
            progression_df[["RMS", "B", "D"]],
            progression_df["Cluster indices"],
            metric="euclidean"
        )
    data = dict()
    # Loop over each cluster, and aggregate the frequencies together
    for index, label in enumerate(progression_df["Cluster indices"].unique()):
        data[label] = dict()
        cluster_data = ap_obj.cluster_centers_[index]
        slice_df = progression_df.loc[progression_df["Cluster indices"] == label]
        columns = list()
        for col in progression_df:
            try:
                columns.append(int(col))
            except ValueError:
                pass
        unique_frequencies = np.unique(slice_df[columns].values.flatten())
        unique_frequencies = unique_frequencies[~np.isnan(unique_frequencies)]
        data[label]["Frequencies"] = unique_frequencies
        if refit is True:
            # Refit the whole list of frequencies with B and D again
            BJ_model = models.Model(fitting.calc_harmonic_transition)
            params = BJ_model.make_params()
            params["B"].set(
                cluster_data[1],
                min=cluster_data[1]*0.99,
                max=cluster_data[1]*1.01
                )
            params["D"].set(cluster_data[2], vary=False)
            # Get values of J based on B again
            J = (unique_frequencies / cluster_data[1]) / 2
            fit = BJ_model.fit(
                data=unique_frequencies,
                J=J,
                params=params
            )
            # Package results together
            data[label].update(fit.best_values)
            data[label]["oldRMS"] = cluster_data[0]
            data[label]["RMS"] = np.sqrt(np.average(np.square(fit.residual)))
        else:
            # Reuse old RMS
            fit_values = {
                "B": cluster_data[1],
                "D": cluster_data[2],
                "RMS": cluster_data[0]
                }
            data[label].update(fit_values)
    return data, progression_df, ap_obj
Esempio n. 4
0
wav_e_HeII = 1640.4
z = 2.923  #estimate from literature
g_cen = wav_e_HeII * (1. + z)
g_blue = 6414.  #estimate from visual look at spectrum

pars = Parameters()

pars.add_many( \
 ('g_cen1', g_blue, True, g_blue-10., g_blue+25.),\
 ('amp1', 2.e3, True, 0.),\
 ('wid1', 12., True, 0., 50.),\
 ('g_cen2', g_cen, True, g_cen-1., g_cen+3.),\
 ('amp2', 2.e4, True, 0.),\
 ('wid2', 10., True, 0.))

mod = lm.Model(dgauss_nocont)

fit = mod.fit(flux, pars, x=wav, weights=inv_noise)

print fit.fit_report()

res = fit.params

g_blue = fit.params['g_cen1'].value
amp_blue = fit.params['amp1'].value
wid_blue = fit.params['wid1'].value

wav_o_HeII = fit.params['g_cen2'].value
wav_o_HeII_err = fit.params['g_cen2'].stderr
amp_HeII = fit.params['amp2'].value
amp_HeII_err = fit.params['amp2'].stderr
Esempio n. 5
0
# print(center_na_2.value, center_na_2.stderr)

df = pd.DataFrame([
    [0.5110, center_na_1.value, center_na_1.stderr],
    [0.6617, center_cs.value, center_cs.stderr],
    [1.2746, center_na_2.value, center_na_2.stderr],
], columns=['Energy', 'Pulseheight', 'PH_err'])

model2 = models.LinearModel()
fit4 = model2.fit(df['Pulseheight'], x=df['Energy'], weights=1/df['PH_err'], slope=2000)
# fit4.plot()
# plt.xlim(0, 1.5)
# plt.ylim(0, 4000)

f = lambda x, a: a*x
model3 = models.Model(f, name="linear_origin")

fit5 = model3.fit(df['Pulseheight'], x=df['Energy'], weights=1/df['PH_err'], a=2000)
# fit5.plot()
# plt.xlim(0, 1.5)
# plt.ylim(0, 4000)
# plt.show()

slope = fit4.params['slope']
intercept = fit4.params['intercept']

spectrum_cs['energy'] = (spectrum_cs['pulseheight'] - intercept)/slope
spectrum_na['energy'] = (spectrum_na['pulseheight'] - intercept)/slope

# spectrum_cs.plot.scatter('energy', 'counts', yerr='y_err')
# spectrum_na.plot.scatter('energy', 'counts', yerr='y_err')
e_g = geometrisch(0.015, 0.15)
e_g_err = geometrisch_err(0.015, 0.15, 0.001, 0.001)

df_1['intrinsiek'] = df_1['efficientie'] / e_g
df_1['intrinsiek_err'] = np.sqrt((df_1['efficientie_err'] / e_g)**2 +
                                 (-(df_1['efficientie'] * e_g_err) /
                                  e_g**2)**2)

sel_1 = df_1[[
    'GM', 'efficientie', 'efficientie_err', 'intrinsiek', 'intrinsiek_err'
]]
sel_1.to_csv('efficienties.csv', index=False)

constant = lambda x, a: a
constant_model = models.Model(constant, name='constant')

fit = constant_model.fit(df_1['strontium'],
                         x=df_1['GM'],
                         weights=1 / df_1['strontium_err'],
                         a=1300)

# df_1.plot.scatter('GM', 'strontium', yerr='strontium_err')
# plt.show()

# print(lmfit.report_fit(fit))

# print(fit.redchi)
## referentie: https://stackoverflow.com/questions/43381833/lmfit-extract-fit-statistics-parameters-after-fitting
# print(fit.params['c'].value)
Esempio n. 7
0
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from lmfit import models
import lmfit

df = pd.read_csv('Radon220V2.csv')
df['N1_err'] = np.sqrt(df['N1'])
df['N2_err'] = np.sqrt(df['N2'])
df['N3_err'] = np.sqrt(df['N3'])

sel = df.query('(t >= 63)')

f = lambda t, N_0, l, Bv: N_0 * np.exp(-(l * t)) + Bv
mod_halfwaarde = models.Model(f, name='intensiteit')

fit = mod_halfwaarde.fit(sel['N1'],
                         t=sel['t'],
                         weights=1 / sel['N1_err'],
                         N_0=400,
                         l=0.01,
                         Bv=20)

sel.plot.scatter('t', 'N1', yerr='N1_err')
fit.plot(ylabel='N', xlabel='t (s)')
sel.plot.scatter('t', 'N2', yerr='N2_err')
sel.plot.scatter('t', 'N3', yerr='N3_err')
plt.show()

print(lmfit.report_fit(fit))
df['err_U'] = .002
df['err_R'] = .01 * df['R'] + 25e-3

df['inv_U'] = 1 / df['U']
df['inv_R'] = 1 / df['R']

df['err_inv_U'] = df['err_U'] / df['U'] * df['inv_U']
df['err_inv_R'] = df['err_R'] / df['R'] * df['inv_R']

df['I'] = df['U'] / df['R']
df['err_I'] = df['I'] * np.sqrt((df['err_U'] / df['U']) ** 2 + (df['err_R'] / df['R']) ** 2)

print(df.head())

f = lambda R, R_u, U_0: R / (R_u + R) * U_0
mod_spanning = models.Model(f, name="Spanningsdeler")

mod_linear = models.LinearModel()
fit = mod_linear.fit(df['I'], x=df['U'], weights=1/df['err_I'])
#fit = mod_linear.fit(df['inv_U'], x=df['inv_R'], weights=1/df['err_inv_U'])

#fit = mod_spanning.fit(df['U'], R=df['R'], weights=1/df['err_U'], R_u=1, U_0=1)

#df.plot.scatter('R', 'U', xerr='err_R', yerr='err_U')
#fit.plot(ylabel='U (V)', xlabel='R ($\Omega$)')
#plt.xlim(0, 350)
#plt.ylim(0, 0.7)
#plt.xlabel('R ($\Omega$)')
#plt.ylabel('U (V)')

df.plot.scatter('inv_R', 'inv_U', xerr='err_inv_R', yerr='err_inv_U')