示例#1
0
    header = "Powers, Gaussian Amplitudes, Gaussian centres, Gaussian widths"
    np.savetxt(data_name, data, header=header)
print(len(fit_data))
prd_plots.ggplot()
size = 4
fig1 = plt.figure('fig1', figsize=(size * np.sqrt(2), size))
ax1 = fig1.add_subplot(1, 1, 1)
fig1.patch.set_facecolor(cs['mnk_dgrey'])
ax1.set_xlabel('Wavelength (λ - nm)')
ax1.set_ylabel('Counts')
ax1.set_title('Labelled spectrum with fits')
ax1.plot(xs0, ys0, '.', markersize=2,
         alpha=0.5, color=cs['gglred'], label='')

pk_ys = [ys0[i] for i in pk_idxs]
for i0, val0 in enumerate(fit_data):
    pk_x = fit_data[i0][1][1]
    pk_y = prd_maths.Gaussian_1D(pk_x,
                                 fit_data[i0][0][1],
                                 fit_data[i0][1][1],
                                 fit_data[i0][2][1])
    ax1.plot(pk_x, pk_y, '.',
             mfc=cs['ggblue'],
             mec=cs['ggblue'],
             label='peak ' + str(i0))
    ax1.text(pk_x, pk_y, ' peak ' + str(i0))

fig1.tight_layout()
plt.show()
prd_plots.PPT_save_2d(fig1, ax1, 'peak labels.png')
    x_fit = np.linspace(min(x_roi), max(x_roi), 1000)

    popt, pcov = curve_fit(prd_maths.Gaussian_1D,
                           x_roi, y_roi, p0=[popt])

    # Final plots
    prd_plots.ggplot()

    colors = plt.cm.viridis(np.linspace(0, 1, len(λs)))
    ax1.plot(x - popt[2], y, '--', alpha=0.5,
             color=colors[i1],
             label='',
             lw=0)

    plt.plot(x_roi - popt[2], y_roi, '.',
             c=colors[i1],
             alpha=0.3)
    plt.plot(x_fit - popt[2], prd_maths.Gaussian_1D(
        x_fit - popt[2], *popt),
        label='fit',
        c=colors[i1],
        lw=0.5)

    # plt.xlim((x[idx_pk - int(0.3 * roi)], x[idx_pk + int(0.3 * roi)]))
ax1.set_title('spectra with fits')
fig1.tight_layout()
plt.show()

ax1.figure.savefig('spectra with fits dark' + '.png')
prd_plots.PPT_save_2d(fig1, ax1, 'spectra with fits')
fig2.patch.set_facecolor(cs['mnk_dgrey'])
ax2.set_xlabel('Wavelength (λ) / nm')
ax2.set_ylabel('Residuals')


x = λ
y = cts
# Final plots
prd_plots.ggplot()

ax1.plot(x_roi, y_roi, '.',
         label='data',
         color=cs['ggred'],
         alpha=1)

ax1.plot(x_fit, prd_maths.Gaussian_1D(
    x_fit, *popt_G),
    color=cs['ggblue'],
    label='Gaussian fit',
    lw=0.5)
ax1.plot(x_fit, prd_maths.Lorentzian_1D(
    x_fit, *popt_L),
    color=cs['ggpurple'],
    label='Lorentzian fit',
    lw=0.5)
ax1.plot(x_fit, prd_maths.Voigt_1D(
    x_fit, *popt_V),
    color=cs['ggyellow'],
    label='Voigt fit',
    lw=0.5)

ax2.plot(x, y - prd_maths.Gaussian_1D(
示例#4
0
def spec_seq_Gauss_fit_20190611(d, popt, idx_pk, roi, pk_lb, Ps):
    print(d)
    λs, ctss, lbs = prd_file_import.load_spec_dir(d)
    prd_plots.ggplot()
    cs = prd_plots.palette()
    size = 4
    colors = plt.cm.viridis(np.linspace(0, 1, len(λs[0:])))

    fig1 = plt.figure('fig1', figsize=(size * np.sqrt(2), size))
    ax1 = fig1.add_subplot(1, 1, 1)
    fig1.patch.set_facecolor(cs['mnk_dgrey'])
    ax1.set_xlabel('Wavelength shifted with excitation power')
    ax1.set_ylabel('Counts')
    ax1.set_title('Spectra with fits - ' + pk_lb)
    fig1.tight_layout()

    fig2 = plt.figure('fig2', figsize=(size * np.sqrt(2), size))
    ax2 = fig2.add_subplot(1, 1, 1)
    fig2.patch.set_facecolor(cs['mnk_dgrey'])
    ax2.set_xlabel('Power (μW)')
    ax2.set_ylabel('Fit amplitude')
    ax2.set_title('Fit amplitude with power - ' + pk_lb)
    fig2.tight_layout()

    fig3 = plt.figure('fig3', figsize=(size * np.sqrt(2), size))
    ax3 = fig3.add_subplot(1, 1, 1)
    fig3.patch.set_facecolor(cs['mnk_dgrey'])
    ax3.set_xlabel('Power (μW)')
    ax3.set_ylabel('Peak location, (λ$_c$ - nm)')
    ax3.set_title('Peak location with power - ' + pk_lb)
    fig3.tight_layout()

    fig4 = plt.figure('fig4', figsize=(size * np.sqrt(2), size))
    ax4 = fig4.add_subplot(1, 1, 1)
    fig4.patch.set_facecolor(cs['mnk_dgrey'])
    ax4.set_xlabel('Power (P - μW)')
    ax4.set_ylabel('Peak width (σ - nm)')
    ax4.set_title('Peak widths with power - ' + pk_lb)
    fig4.tight_layout()

    μs = []
    σs = []
    As = []

    for i0, val0 in enumerate(λs[0:]):
        P = Ps[i0]
        x = λs[i0]
        y = ctss[i0]
        x_roi = x[int(idx_pk - roi / 2):int(idx_pk + roi / 2)]
        y_roi = y[int(idx_pk - roi / 2):int(idx_pk + roi / 2)]

        x_fit = np.linspace(min(x_roi), max(x_roi), 1000)

        popt, pcov = curve_fit(prd_maths.Gaussian_1D, x_roi, y_roi, p0=[popt])
        As.append(popt[0])
        μs.append(popt[1])
        σs.append(popt[2])

        # Final plots
        prd_plots.ggplot()

        colors = plt.cm.viridis(np.linspace(0, 1, len(λs)))
        ax1.plot(x - popt[1] + i0,
                 y,
                 '--',
                 alpha=0.5,
                 color=colors[i0],
                 label='',
                 lw=0)

        ax1.plot(x_roi - popt[1] + i0, y_roi, '.', c=colors[i0], alpha=0.3)

        ax1.plot(x_fit - popt[1] + i0,
                 prd_maths.Gaussian_1D(x_fit - popt[1] + i0,
                                       *(popt[0], i0, popt[2], popt[3])),
                 label='fit',
                 c=colors[i0],
                 lw=0.5)
        ax1.set_xlim((x[idx_pk - int(0.3 * roi)], x[idx_pk + int(0.3 * roi)]))
        ax1.set_xlim((-1, 12))
        fig1.tight_layout()

        ax2.plot(P, popt[0], 'o', c=colors[i0])
        fig2.tight_layout()

        ax3.plot(P, popt[1], 'o', c=colors[i0])
        fig3.tight_layout()

        ax4.plot(P, popt[2], 'o', c=colors[i0])
        fig4.tight_layout()

    plt.show()

    # ax1.figure.savefig(pk_lb + ' ' + ax1.get_title() + ' dark' + '.png')
    # ax2.figure.savefig(pk_lb + ' ' + ax2.get_title() + ' dark' + '.png')
    # ax3.figure.savefig(pk_lb+ ax3.get_title() + ' dark' + '.png')
    # ax4.figure.savefig(pk_lb + ' ' + ax4.get_title() + ' dark' + '.png')

    prd_plots.PPT_save_2d(fig1, ax1, pk_lb + ' ' + ax1.get_title())
    prd_plots.PPT_save_2d(fig2, ax2, pk_lb + ' ' + ax2.get_title())
    prd_plots.PPT_save_2d(fig3, ax3, pk_lb + ' ' + ax3.get_title())
    prd_plots.PPT_save_2d(fig4, ax4, pk_lb + ' ' + ax4.get_title())

    ax1.cla()
    ax2.cla()
    ax3.cla()
    ax4.cla()
    return As, μs, σs