Ejemplo n.º 1
0
    def save_image_model_data(self, output_filename):
        """

        :param output_filename:
        :return:
        """
        output_dict = self.model.to_dict()
        file_io.dict_2_h5(output_filename, output_dict)
Ejemplo n.º 2
0
def check_finesse(folder, recover=False):
    data = h5_2_dict(join(folder, 'input_finesse.h5'))
    ix = data['fit_ix']
    Lpost, dpost = read_Ld_results(data['Ld_folder'])

    i = np.random.choice(len(Lpost))
    L = Lpost[i]
    d = dpost[i]
    r = data['r']
    sig = data['sig']

    analyzer = pymultinest.Analyzer(5,
                                    outputfiles_basename=join(
                                        folder, "finesse_"))
    modes = analyzer.get_mode_stats()
    #F, A, Arel, Ti, offset = modes['modes'][0]['mean']
    F, A, Arel, Ti = modes['modes'][0]['mean']
    post = analyzer.get_equal_weighted_posterior()
    w0 = [487.873302, 487.98634]
    mu = [232.03806, 39.948]

    Lpost = Lpost[::30]
    dpost = dpost[::30]

    Fpost = post[::30, 0]
    Apost = post[::30, 1]
    Arelpost = post[::30, 2]
    Tipost = post[::30, 3]
    #offsetpost = post[::30, 4]

    #offset_mean = np.mean(post[:, 4])
    #offset_sd = np.std(post[:, 4])
    print('F: {0:f} +/- {1:f}'.format(np.mean(post[:, 0]), np.std(post[:, 0])))
    print('A: {0:e} +/- {1:e}'.format(np.mean(post[:, 1]), np.std(post[:, 1])))
    print('Arel: {0:f} +/- {1:f}'.format(np.mean(post[:, 2]), np.std(post[:,
                                                                          2])))
    print('Ti Ar: {0:f} +/- {1:f}'.format(np.mean(post[:, 3]),
                                          np.std(post[:, 3])))
    #print('offset: {0:e} +/- {1:e}'.format(offset_mean, offset_sd))

    if recover:
        try:
            post_dict = h5_2_dict(join(folder, "finesse_solver_model_post.h5"))
            sig_post = post_dict["signal post"]
            new_r = post_dict['new_r']
        except IOError, e:
            print(
                "Can't recover finesse solver q posterior. Calculating from scratch."
            )
            #sig_post = calculate_signal_post(r[ix], Lpost, dpost, Fpost, Apost, Arelpost, Tipost, offsetpost, w0, mu)
            new_r = np.linspace(0, 900, 1000)
            # sig_post = calculate_signal_post(r[ix], Lpost, dpost, Fpost, Apost, Arelpost, Tipost, w0, mu)
            sig_post = calculate_signal_post(new_r, Lpost, dpost, Fpost, Apost,
                                             Arelpost, Tipost, w0, mu)
            dict_2_h5(join(folder, "finesse_solver_model_post.h5"), {
                'signal post': sig_post,
                'new_r': new_r
            })
Ejemplo n.º 3
0
def check_solution(folder,
                   Ld_dir,
                   finesse_dir,
                   recover=False,
                   w0=487.98634,
                   mu=39.948):
    print("I'm here!")
    data = h5_2_dict(join(folder, 'input_plasma_data.h5'))

    ix = data['fit_ix']
    r = data['r']
    sig = data['sig']

    Lpost, dpost = read_Ld_results(Ld_dir)
    Fpost, _, _, _ = read_finesse_results(finesse_dir)
    nL = len(Lpost)
    nF = len(Fpost)
    i = np.random.choice(nL)
    j = np.random.choice(nF)

    L = Lpost[i]
    d = dpost[i]
    F = Fpost[j]

    Lstep = 100
    Fstep = 100
    Tistep = 100
    analyzer = pymultinest.Analyzer(3,
                                    outputfiles_basename=join(folder, "Ti_"))
    modes = analyzer.get_mode_stats()
    Ti, V, A = modes['modes'][0]['mean']
    print(modes['modes'][0]['sigma'])
    print(Ti, V, A)
    post = analyzer.get_equal_weighted_posterior()

    Tipost = post[::, 0]
    Vpost = post[::, 1]
    Apost = post[::, 2]

    if recover:
        try:
            post_dict = h5_2_dict(join(folder, "Ti_solver_model_post.h5"))
            sig_post = post_dict["signal post"]
        except:
            print(
                "Can't recover Ti solver q posterior.  Calculating from scratch."
            )
            sig_post = calculate_signal_post(r[ix],
                                             Lpost[::Lstep],
                                             dpost[::Lstep],
                                             Fpost[::Fstep],
                                             Tipost[::Tistep],
                                             Vpost[::Tistep],
                                             Apost[::Tistep],
                                             w0,
                                             mu,
                                             nprocs=32)
            dict_2_h5(join(folder, "Ti_solver_model_post.h5"),
                      {"signal post": sig_post})
    else:
        sig_post = calculate_signal_post(r[ix],
                                         Lpost[::Lstep],
                                         dpost[::Lstep],
                                         Fpost[::Fstep],
                                         Tipost[::Tistep],
                                         Vpost[::Tistep],
                                         Apost[::Tistep],
                                         w0,
                                         mu,
                                         nprocs=32)
        dict_2_h5(join(folder, "Ti_solver_model_post.h5"),
                  {"signal post": sig_post})

    sig_mean = np.mean(sig_post, axis=1)
    percentiles = calculate_percentile_ranges(sig_post)
    #vals = forward_model(r[ix], L, d, F, w0, mu, A, Ti, V, sm_ang=False, nlambda=1024)

    fig, ax = plt.subplots(figsize=(3.5, 3.5 / 1.61))
    # ax.plot(r[ix], sig[ix], 'C1', alpha=0.5, label='Data')
    ax.plot(r[ix], (sig[ix] - 3000.0) / 100.0, 'C1', alpha=0.5, label='Data')
    #ax.plot(r[ix], vals, 'r')
    alphas = [0.8, 0.5, 0.2]
    keys = [68, 95, 99]
    for alpha, per in zip(alphas, keys):
        if per == 99:
            # ax.fill_between(r[ix], percentiles[per][0], percentiles[per][1], color='C3', alpha=alpha, label='Fit')
            ax.fill_between(r[ix],
                            percentiles[per][0] / 100.0,
                            percentiles[per][1] / 100.0,
                            color='C3',
                            alpha=alpha,
                            label='Fit')
        else:
            # ax.fill_between(r[ix], percentiles[per][0], percentiles[per][1], color='C3', alpha=alpha)
            ax.fill_between(r[ix],
                            percentiles[per][0] / 100.0,
                            percentiles[per][1] / 100.0,
                            color='C3',
                            alpha=alpha)
    fig.legend(frameon=False,
               fontsize=8,
               loc='upper right',
               bbox_to_anchor=(0.5, 0.5))
    ax.set_xlabel("R (px)", fontsize=8, labelpad=-1)
    ax.set_ylabel("Counts (Hundreds)", fontsize=8, labelpad=-1)
    ax.tick_params(labelsize=8)
    #fig.tight_layout()
    fig.savefig(join(folder, "Ti_Ar_fit.png"), dpi=400)
    plt.show(block=False)

    axis_labels = ["Ti (eV)", "V (m/s)", "A (Counts)"]
    ylabels = ["P(Ti)", "P(V)", "P(A)"]
    # fig, ax = plt.subplots(3, figsize=(6, 15))
    fig, ax = plt.subplots(2, figsize=(3.5, 2 * 3.5 / 1.61))
    # for n in range(3):
    for n in range(2):
        my_hist(ax[n], post[:, n])
        ax[n].set_xlabel(axis_labels[n], fontsize=8, labelpad=-1)
        ax[n].set_ylabel(ylabels[n], fontsize=8, labelpad=-1)
        ax[n].tick_params(labelsize=8)
    #fig.tight_layout()
    fig.savefig(join(folder, "Ti_solver_histograms.png"), dpi=400)
    plt.show()
Ejemplo n.º 4
0
        fname = abspath(join(args.folder, "input_plasma_data.h5"))
        basename = abspath(join(args.folder, 'Ti_'))
        org_fname = abspath(join(args.folder, 'ringsum.h5'))

        if not isfile(fname) or args.overwrite:
            if isfile(org_fname):
                data = h5_2_dict(org_fname)
                r = data['r']
                sig = data['sig']
                error = data['sig_sd']
                fit_ix, _ = get_fitting_region(r, sig, error)
                maxval = np.max(sig[fit_ix])
                amp_lim = [0.5 * maxval, 5.0 * maxval]
                dict_2_h5(fname, {
                    'r': r,
                    'sig': sig,
                    'fit_ix': fit_ix,
                    'error': error
                })
            else:
                raise ValueError('{0} does not exist!'.format(org_fname))
        else:
            a = h5_2_dict(fname)
            r = a['r']
            sig = a['sig']
            fit_ix = a['fit_ix']
            error = a['error']
            maxval = np.max(sig[fit_ix])
            amp_lim = [0.5 * maxval, 5.0 * maxval]

        inputs = {
            'error_per': args.error,
Ejemplo n.º 5
0
            #sig_post = calculate_signal_post(r[ix], Lpost, dpost, Fpost, Apost, Arelpost, Tipost, offsetpost, w0, mu)
            new_r = np.linspace(0, 900, 1000)
            # sig_post = calculate_signal_post(r[ix], Lpost, dpost, Fpost, Apost, Arelpost, Tipost, w0, mu)
            sig_post = calculate_signal_post(new_r, Lpost, dpost, Fpost, Apost,
                                             Arelpost, Tipost, w0, mu)
            dict_2_h5(join(folder, "finesse_solver_model_post.h5"), {
                'signal post': sig_post,
                'new_r': new_r
            })
    else:
        new_r = np.linspace(0, 900, 1000)
        #sig_post = calculate_signal_post(r[ix], Lpost, dpost, Fpost, Apost, Arelpost, Tipost, w0, mu)
        sig_post = calculate_signal_post(new_r, Lpost, dpost, Fpost, Apost,
                                         Arelpost, Tipost, w0, mu)
        dict_2_h5(join(folder, "finesse_solver_model_post.h5"), {
            'signal post': sig_post,
            'new_r': new_r
        })

    sig_mean = np.mean(sig_post, axis=1)
    sig_std = np.std(sig_post, axis=1)
    percentiles = calculate_percentile_ranges(sig_post)

    fig, ax = plt.subplots()
    ax.plot(r[ix], sig[ix], 'C0', alpha=0.7)
    #ax.plot(r[ix], sig_mean, 'C3')
    alphas = [0.8, 0.5, 0.2]
    #alphas = [0.9, 0.7, 0.5]
    keys = [68, 95, 99]
    for alpha, per in zip(alphas, keys):
        #ax.fill_between(r[ix], percentiles[per][0], percentiles[per][1], color='C3', alpha=alpha)
        ax.fill_between(new_r,
Ejemplo n.º 6
0
    #i1 = np.abs(r - r1).argmin()
    #i2 = np.abs(r - r2).argmin()+1
    #sl = slice(i1, i2)
    #data['sig_sd'][sl] = np.sqrt(data['sig_sd'][sl]**2 - (0.01*data['sig'][sl])**2 + (0.02*data['sig'][sl])**2)

    #min_loc = np.argmin(sig)
    # data['sig'] -= sig.min()
    # data['sig'] -= 0.8 * sig.min()
    # data['sig_sd'] = np.sqrt(data['sig_sd']**2 + data['sig_sd'][min_loc]**2) # Adding in the offset error

    ix = get_fitting_region(r, sig, data['sig_sd'], plot_fit_region=True)

    data['fit_ix'] = ix

    fig, ax = plt.subplots()
    ax.plot(r**2, data['sig'])
    ax.plot(r[ix['0']]**2, data['sig'][ix['0']])
    ax.fill_between(r**2,
                    data['sig'] - data['sig_sd'],
                    data['sig'] + data['sig_sd'],
                    alpha=0.5)
    plt.show()

    fig, ax = plt.subplots()
    ax.plot(r[ix['0']], data['sig_sd'][ix['0']] / data['sig'][ix['0']])
    plt.show()

    #dict_2_h5(join(folder, 'finesse_input.h5'), data)
    # dict_2_h5(join(folder, 'test_finesse_input.h5'), data)
    dict_2_h5(join(folder, args.filename), data)
Ejemplo n.º 7
0
        if args.overwrite:
            resume = False
        else:
            resume = True
        resume = False 
        if not isfile(fname) or args.overwrite:
            if isfile(org_fname):
                data = h5_2_dict(org_fname)
                r = data['r']
                sig = data['sig']
                sig_sd = data['sig_sd']
                pk_dir = join(args.folder,'multinest_peaks/')
                prep_folder(pk_dir)
                peaks, peaks_sd, orders = get_pk_locations(r, sig, sig_sd, args.wavelengths, pk_dir, pkerr=args.pkerr, pkthresh=args.pkthresh)
                image_name = data['fname']
                dict_2_h5(fname, {'r':r, 'sig':sig, 'peaks':peaks, 'orders':orders, 'peaks_sd':peaks_sd})
            else:
                raise ValueError('{0} does not exist!'.format(org_fname))
        else:
           a = h5_2_dict(fname)
           peaks = a['peaks']
           orders = a['orders']
           peaks_sd = a['peaks_sd']

        inputs = {'L_lim':args.L_lim, 'd_lim':args.d_lim, 'livepoints':args.livepoints, 'pkerr':args.pkerr}
        dict_2_h5(fname, inputs, append=True)

        if args.no_solve:
            solver_in = None
        else:
            solver_in = {'peaks':peaks, 'orders':orders, 'basename':basename, 'peaks_sd':peaks_sd,
Ejemplo n.º 8
0
def main(fname,
         bgfname=None,
         color='b',
         binsize=0.1,
         xguess=None,
         yguess=None,
         block_center=False,
         click_center=True,
         find_center=True,
         sub_prof=False,
         plotit=False,
         write=None,
         npix=1,
         return_tiff_mean=True,
         tiff_image_idx=None):

    bgdata = None
    data = images.get_data(fname,
                           color=color,
                           return_mean=return_tiff_mean,
                           image_index=tiff_image_idx)

    if npix > 1:
        data = ringsum.super_pixelate(data, npix=npix)

    if find_center:
        if click_center:
            xguess, yguess = plotting.center_plot(data)

        x0, y0 = ringsum.locate_center(data,
                                       xguess=xguess,
                                       yguess=yguess,
                                       block_center=block_center,
                                       binsize=0.1,
                                       plotit=True,
                                       printit=True)

        if plotit:
            fig, ax = plt.subplots(figsize=(10, 8))
            plotting.ring_plot(data, fax=(fig, ax))
            ax.axhline(y0, color='b')
            ax.axvline(x0, color='r')
            fig.savefig('/home/milhone/fp_shot_2332.png', transparent=True)
            plt.show()
    else:
        if xguess is None:
            x0 = data.shape[1] / 2.
        else:
            x0 = xguess
        if yguess is None:
            y0 = data.shape[0] / 2.
        else:
            y0 = yguess

    print('Performing Annual Sum...')
    r, sig0, sig0_sd = ringsum.ringsum(data,
                                       x0,
                                       y0,
                                       use_weighted=False,
                                       quadrants=False,
                                       binsize=binsize,
                                       remove_hot_pixels=True)

    if bgfname is not None:
        print('Removing background...')
        if bgdata is None:
            bgdata = images.get_data(bgfname,
                                     color=color,
                                     return_mean=return_tiff_mean,
                                     image_index=tiff_image_idx)
            if npix > 1:
                bgdata = ringsum.super_pixelate(bgdata, npix=npix)
        _, bg, bg_sd = ringsum.ringsum(bgdata,
                                       x0,
                                       y0,
                                       use_weighted=False,
                                       binsize=binsize,
                                       remove_hot_pixels=True)
        sig = sig0 - bg
        sig_sd = np.sqrt(sig0_sd**2 + bg_sd**2)
    else:
        sig, sig_sd = sig0, sig0_sd

    dic = {
        'fname': abspath(fname),
        'color': color,
        'center': (x0, y0),
        'binsize': binsize,
        'r': r,
        'sig': sig,
        'sig_sd': sig_sd
    }

    if bgfname is not None:
        dic['bg_fname'] = abspath(bgfname)

    if sub_prof:
        dic['pk_guess'] = pk_guess

    print('done!')

    if write is not None:
        file_io.prep_folder(write)
        file_io.dict_2_h5(join(write, 'ringsum.h5'), dic)

    if write is not None or plotit:
        fig, axs = plt.subplots(2, 1, figsize=(12, 8))

        axs[0].errorbar(r,
                        sig,
                        yerr=sig_sd,
                        fmt='-',
                        errorevery=5,
                        color='r',
                        lw=2,
                        zorder=10,
                        ecolor='b')
        axs[0].axhline(0, color='k', alpha=0.7)
        axs[0].set_title('binsize={0}'.format(binsize))
        axs[1].errorbar(r,
                        sig0,
                        yerr=sig0_sd,
                        fmt='-',
                        errorevery=5,
                        lw=2,
                        label='raw',
                        zorder=10,
                        color='C0',
                        ecolor='C1')
        axs[1].axhline(0, color='k', alpha=0.7)

        if bgfname is not None:
            axs[1].errorbar(r,
                            bg,
                            yerr=bg_sd,
                            fmt='-',
                            errorevery=5,
                            lw=2,
                            label='background',
                            zorder=10,
                            color='C2',
                            ecolor='C3')

        axs[1].set_xlabel('R (px)')
        axs[1].legend()

        if write:
            file_io.prep_folder(join(write, 'plots'))
            plt.savefig(join(write, 'plots', 'ringsum.png'),
                        bbox_inches='tight')
        if plotit:
            plt.show()
        else:
            plt.close()

    fig, ax = plt.subplots()
    #ax.plot(r**2, sig, 'C1')
    ax.errorbar(r, sig / 1000, yerr=sig_sd / 1000, color='C1')
    #ax.plot(r, sig/1000.0, color='C1')
    #ax.ticklabel_format(style='sci', axis='both', scilimits=(0,0))
    #ax.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
    #ax.set_xlabel(r"R${}^2$ (px${}^2$)", fontsize=18)
    ax.set_xlabel("R (px)", fontsize=18)
    ax.set_ylabel("Counts ($10^3$)", fontsize=18)
    ax.tick_params(labelsize=18)
    fig.tight_layout()
    #fig.savefig('fp_ringsum_2332.pdf', transparent=True)
    plt.show()
    #
    fig, ax = plt.subplots()
    ax.plot(r, sig_sd / sig)
    plt.show()
    #check_fwhm(r, sig-sig.min())
    return dic
Ejemplo n.º 9
0
def main(w,
         mass,
         temp,
         v_outer,
         r_outer,
         impact_factor,
         Lnu,
         fname,
         rmax=42.0):
    warr, spectrum = plasma.calculate_pcx_chord_emission(impact_factor,
                                                         temp,
                                                         w,
                                                         mass,
                                                         Lnu,
                                                         v_outer,
                                                         rmax=rmax,
                                                         nr=500,
                                                         nlambda=2000,
                                                         Lne=4.0,
                                                         R_outer=r_outer)

    L = 150.0 / 0.004
    d = 0.88
    F = 20.7

    print('Impact Factor', impact_factor)
    print('Ti: ', temp)
    print('w0: ', w)
    print('mu: ', mass)
    print('Lnu: ', Lnu)
    print('Vouter: ', v_outer)
    print('Router: ', r_outer)
    print('Rmax: ', rmax)
    print('L: ', L)
    print('d: ', d)
    print('F: ', F)

    r, theta, x = plasma.calculate_r_theta_x_from_impact_factor(impact_factor,
                                                                rmax=rmax,
                                                                npts=500)
    vel = plasma.pcx_velocity_profile(r, Lnu, r_outer, v_outer)
    plt.plot(r, vel)
    plt.show()

    raw_input('pausing...')
    nx = 6000
    ny = 4000

    data = np.zeros((ny, nx))
    x = np.arange(1, nx + 1, 1)
    y = np.arange(1, ny + 1, 1)

    x0 = 3000
    y0 = 2000

    XX, YY = np.meshgrid(x, y)
    R = np.sqrt((XX - x0)**2 + (YY - y0)**2)

    nprocs = 32

    split_r = np.array_split(R.flatten(), nprocs)
    results = [0.0 for _ in split_r]
    t0 = time.time()
    with concurrent.futures.ProcessPoolExecutor() as executor:
        fut_to_spl = dict()
        for idx, split in enumerate(split_r):
            future = executor.submit(models.general_model, split, L, d, F,
                                     warr, spectrum)
            fut_to_spl[future] = idx
        for future in concurrent.futures.as_completed(fut_to_spl):
            current_idx = fut_to_spl[future]
            output = future.result()
            results[current_idx] = output

    results = np.concatenate(results)
    results.shape = (ny, nx)

    results *= 1000.0 / results.max()

    t1 = time.time()
    print(t1 - t0, 'seconds')
    fig, ax = plt.subplots()
    im = ax.imshow(results)
    plt.colorbar(im, ax=ax)
    plt.show()

    x0, y0 = ringsum.locate_center(results, xguess=3000, yguess=2000)
    rr, ss, _ = ringsum.ringsum(results, x0, y0)

    rrr = np.linspace(1, 1750, 5000)
    actual = models.general_model(rrr, L, d, F, warr, spectrum)

    fig, ax = plt.subplots()
    ax.plot(rr, ss / ss.max(), 'C0', label='ringsum')
    ax.plot(rrr, actual / actual.max(), 'C1', label='actual')
    ax.legend()
    plt.show()

    #results = results.astype(int)
    #results = np.asarray(results, dtype=np.int16)
    image_data = {
        'image': results,
        'L': L,
        'd': d,
        'F': F,
        'Vouter': v_outer,
        'R_outer': r_outer,
        'Ti': temp,
        'w0': w,
        'mu': mass,
        'Lnu': Lnu,
        'impact_factor': impact_factor
    }
    file_io.dict_2_h5(fname, image_data)
Ejemplo n.º 10
0
    Ti = 0.5
    Vouter = 1000.0
    Router = 35.0
    Rmax = 42.0
    impact_fac = 35.0
    ne = 2e17
    nn = 8e17
    # mom_dif_length = 20.0
    mom_dif_length = 100.0 * plasma.Lnu(ne * nn, Ti, mu=40.0)
    fname = 'pcx_vel_profile_{0:2.0f}.h5'.format(impact_fac)
    print(fname)

    main(w0,
         mu,
         Ti,
         Vouter,
         Router,
         impact_fac,
         mom_dif_length,
         fname,
         rmax=Rmax)
    data = file_io.h5_2_dict(fname)
    data['image'] = add_noise(data['image'])

    file_io.dict_2_h5(fname, data)

    # fig, ax = plt.subplots()
    # im = ax.imshow(data['image'])
    # plt.colorbar(im)
    # plt.show()