Esempio n. 1
0
def slide_phantom(shot, camera='phantom2', sub=20, blur=3, interval=50, 
                  pixel_t_hist=None):
    """
    Slide through Phantom frames while displaying last closed flux surface and 
    relevant timeseries plots.
    Parameters
        shot: int, shot number
        camera: string e.g. 'phantom' (outboard midplane GPI),
                'phantom2' (X-point GPI)
        sub: number of frames to use in average subtraction. 20 works well.
        blur: extent of Gaussian blur, 1, 2, or 3 advisable
        interval: delay in ms between frames during play
        pixel_t_hist: (x,y) to show time history of that pixel instead of 
        H-alpha.
    """
    time = acquire.gpi_series(shot, camera, 'time')
    frames = acquire.video(shot, camera)
    frame_count = frames.shape[0]
    
    # Plot GPI and LCFS 
    gs = gridspec.GridSpec(3, 1, height_ratios=[3, 1, 1])
    fig, ax = plt.subplots()
    plt.subplots_adjust(bottom=0.25, hspace=.43, top=.96)
    plt.subplot(gs[0])
    im = plt.imshow(frames[0], origin='lower', cmap=plt.cm.gist_heat)
    plt.colorbar()
    plt.axis('off')

    # Plot H_alpha or pixel timeseries
    plt.subplot(gs[1]).locator_params(axis='y', nbins=4)
    if pixel_t_hist:
        plt.title('Pixel time history')
        plt.plot(time, 
                 frames[:, pixel_t_hist[0], pixel_t_hist[1]].swapaxes(0, 1))
    else: 
        plt.title('H-alpha')
        time_ha2, ha2 = process.time_crop(acquire.time_ha2(shot), time)
        plt.plot(time_ha2, ha2)
    vl1 = plt.axvline(time[0], color='r')
    plt.xlim([time[0], time[-1]])

    # Plot line average density 
    time_dens, dens = process.time_crop(acquire.time_dens(shot), time) 
    plt.subplot(gs[2]).locator_params(axis='y', nbins=4)
    plt.title('Line Average Density')
    plt.plot(time_dens, dens)
    vl2 = plt.axvline(time[0], color='r')
    plt.xlabel('Time (s)')
    plt.xlim([time[0], time[-1]])

    curr_frame = 0

    def update(val):
        global frames, curr_frame
        val = int(val)
        curr_frame = val 
        curr_time = time[val]
        slider.valtext.set_text('%d (t=%.5f s)' % (val, curr_time))
        if val < frame_count: 
            im.set_array(frames[val])
            vl1.set_xdata(curr_time)
            vl2.set_xdata(curr_time)
        fig.canvas.draw_idle()
 
    # Slider settings
    slide_area = plt.axes([0.10, 0.1, 0.65, 0.03])
    slider = Slider(slide_area, 'Frame', 0, frame_count-1, valinit=0)
    slider.drawon = True
    slider.valfmt = '%d'
    slider.on_changed(update)

    def init():
        global curr_frame
        curr_frame = int(curr_frame)
        start_frame = curr_frame
        im.set_data(frames[curr_frame])
        vl1.set_xdata(time[curr_frame])
        vl2.set_xdata(time[curr_frame])
        for i in xrange(curr_frame, curr_frame + 200, 1): 
            slider.set_val(i)
            vl1.set_xdata(time[i])
            vl2.set_xdata(time[i])
        slider.set_val(start_frame)
        return [im, vl1, vl2]

    def animate(i):
        im.set_array(frames[i])
        vl1.set_xdata(time[i])
        vl2.set_xdata(time[i])
        return [im, vl1, vl2]
    
    def play(event):
        anim = animation.FuncAnimation(fig, animate, init_func=init,
                                       frames=100, interval=interval, 
                                       blit=False)

    def forward(event):
        global curr_frame
        slider.set_val(curr_frame + 1)

    def backward(event):
        global curr_frame
        slider.set_val(curr_frame - 1)

    # Time button settings
    play_button_area = plt.axes([0.45, 0.025, 0.1, 0.04])
    play_button = Button(play_button_area, 'Play')
    play_button.on_clicked(play)
    forward_button_area = plt.axes([0.56, 0.05, 0.04, 0.025])
    forward_button = Button(forward_button_area, '>')
    forward_button.on_clicked(forward)
    back_button_area = plt.axes([0.56, 0.015, 0.04, 0.025])
    back_button = Button(back_button_area, '<')
    back_button.on_clicked(backward)

    def apply_filter(label):
        global frames
        if label == 'Orig': 
            frames = acquire.video(shot, camera)
        elif label == 'Sub %d' % sub:
            frames = acquire.video(shot, camera, sub=sub)
        elif label == 'Blur %d' % blur:
            frames = acquire.video(shot, camera, sub=sub, blur=blur)
        elif label == 'Sobel':
            frames = acquire.video(shot, camera, sub=sub, blur=blur, 
                                   sobel=True)
        update(curr_frame)
        im.autoscale()

    def recolor(event):
        im.autoscale()

    def cmap_change(label):
        global curr_frame
        if label == 'Red': im.cmap = plt.cm.gist_heat
        if label == 'Gray': im.cmap = plt.cm.gray
        update(curr_frame)
        im.autoscale()

    # Image button settings
    left = .3
    bottom = .79
    filter_radio_area = plt.axes([left, bottom, .1, .12])
    filter_radio = RadioButtons(filter_radio_area, ('Orig', 'Sub %d' % sub, 
                                                    'Blur %d' % blur, 
                                                    'Sobel'))
    filter_radio.on_clicked(apply_filter)
    cmap_radio_area = plt.axes([left, bottom-.08, .1, .07])
    cmap_radio = RadioButtons(cmap_radio_area, ('Red', 'Gray'))
    cmap_radio.on_clicked(cmap_change)
    recolor_button_area = plt.axes([left, bottom-.13, 0.1, 0.04])
    recolor_button = Button(recolor_button_area, 'Recolor')
    recolor_button.on_clicked(recolor)

    # Black magic to fix strange global variable error
    apply_filter('Orig')

    plt.show()
Esempio n. 2
0
slbutton = Button(ax9, 'RWD <<', color='magenta', hovercolor='0.975')
slbutton.on_clicked(sl_onClick)
ax10 = plt.axes([0.47+0.2, 0.76, 0.05, 0.04])
readybutton = Button(ax10, 'Set label \nstart time', color='yellow', hovercolor='0.975')
readybutton.on_clicked(set_label_start)
ax11 = plt.axes([0.47+0.2, 0.71, 0.05, 0.04])
logbutton = Button(ax11, 'Stop time \nLog label', color='green', hovercolor='0.975')
logbutton.on_clicked(log_label)
ax12 = plt.axes([0.87, 0.81, 0.05, 0.09])
savebutton = Button(ax12, 'Save labels\nto file', color='cyan', hovercolor='0.975')
savebutton.on_clicked(save_label)
startax = plt.axes([0.15, 0.625, 0.25, 0.03])
stopax = plt.axes([0.55, 0.625, 0.25, 0.03])
start = Slider(startax, 'Label Start \nTimestamp', data.index[0].value//10**3, data.index[-1].value//10**3, valinit=data.index[0].value//10**3)
stop = Slider(stopax, 'Label Stop\nTimestamp', data.index[0].value//10**3, data.index[-1].value//10**3, valinit=data.index[0].value//10**3)
start.valfmt = '%i'
stop.valfmt = '%i'
rangeax = plt.axes([0.47+0.2, 0.67, 0.18, 0.03])
drange = Slider(rangeax,'Constant\ntimedelta(s)', -100, 100, valinit=0)
drange.valfmt = '%i'
rax = plt.axes([0.53+0.2, 0.71, 0.06, 0.19])
radio = RadioButtons(rax, labels, active=0)
#radio = CheckButtons(rax, labels[1:], [False]*len(labels[1:]))
rax1 = plt.axes([0.60+0.2, 0.71, 0.06, 0.19])
radio1 = RadioButtons(rax1, ['NA']*(num_class+1), active=0)
#radio1 = CheckButtons(rax1, range(num_class), [False]*num_class)
radio.on_clicked(r_label)
radio1.on_clicked(r1_label)
def itergen():
	global press_delta
	i_max = data.shape[0]
Esempio n. 3
0
def slide_corr(frames, pixel, other_pixels=None):
    """
    Display correlation values for a pixel with a slider for lag time.
    Parameters
        frames: NumPy array of correlations with dimension (no. lags, x pixels,
                y pixels)
        pixel: (x, y) to mark with a circle
        other_pixels: array of (x, y) values of pixels to mark
    """
    frame_count = frames.shape[0]

    # Initial plotting
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    plt.subplots_adjust(bottom=0.25)
    im = plt.imshow(frames[frame_count/2], origin='bottom', 
                    cmap=plt.cm.RdBu_r, vmin=-1, vmax=1)
    circ = plt.Circle(pixel[::-1], radius=1, edgecolor='r', fill=False)
    ax.add_patch(circ)
    if other_pixels: 
        t_hists_r = [o[0] for o in other_pixels]
        t_hists_z = [o[1] for o in other_pixels]
    else:
        t_hists_r = acquire.gpi_series(1150611004, 'phantom2', 'hist_xpix')
        t_hists_z = acquire.gpi_series(1150611004, 'phantom2', 'hist_ypix')
    for pos in zip(t_hists_r, t_hists_z): 
        ax.add_patch(plt.Circle(pos, radius=1, edgecolor='b', fill=False))
    plt.colorbar()
    plt.title('Correlation for pixel (%d, %d)' % pixel)
    plt.xlabel('Pixel y coordinate')
    plt.ylabel('Pixel x coordinate')

    # Slider and button settings
    slide_area = plt.axes([0.10, 0.1, 0.65, 0.03])
    slider = Slider(slide_area, 'Lag', -frame_count/2, frame_count/2, valinit=0)
    slider.drawon = True
    slider.valfmt = '%d'
    play_button_area = plt.axes([0.45, 0.025, 0.1, 0.04])
    play_button = Button(play_button_area, 'Play')

    curr_frame = 0

    def update(val):
        global curr_frame
        curr_frame = val + frame_count/2
        slider.valtext.set_text('%d' % val)
        if curr_frame < frame_count: 
            im.set_array(frames[curr_frame])
        fig.canvas.draw_idle()

    slider.on_changed(update)

    def init():
        global curr_frame
        curr_frame = int(curr_frame)
        im.set_data(frames[curr_frame])
        for i in xrange(curr_frame, curr_frame + 200, 2): 
            slider.set_val(i)
        return [im]

    def animate(i):
        im.set_array(frames[i])
        return [im]
    
    def play(event):
        anim = animation.FuncAnimation(fig, animate, init_func=init, frames=
                                       frame_count, interval=0, blit=False)

    play_button.on_clicked(play)
    plt.show()
Esempio n. 4
0
def slide_correlation(shot, fl_sav):
    """
    Slide through Phantom camera frames using given synthetic field line images 
    to create a reconstruction of the original using cross-correlation to find
    the best match.
    
    Args:
        shot: [int] shot number
        fl_sav: [scipy.io.idl.readsav object] containing field line images
    """
    time = acquire.gpi_series(shot, 'phantom2', 'time')
    frames = acquire.video(shot, 'phantom2', sub=20, sobel=False)
    frame_index = 0
    fls = fl_sav.fl_image
    fl_r = fl_sav.fieldline_r
    fl_z = fl_sav.fieldline_z
    rlcfs, zlcfs = acquire.lcfs_rz(shot)
    efit_times, flux, flux_extent = acquire.time_flux_extent(shot)
    efit_t_index = process.find_nearest(efit_times, time[0], ordered=True)
    machine_x, machine_y = acquire.machine_cross_section()

    # Find cross-correlation scores between frames and field line images
    xcorrs = np.zeros(fls.shape[0])
    for i, fl in enumerate(fls):
        xcorrs[i] = signals.cross_correlation(frames[frame_index], fl) 
    indices = np.argsort(xcorrs)

    # Interpolate field line cross-correlation scores over R, Z grid
    r_space = np.linspace(min(fl_r), max(fl_r), 100)
    z_space = np.linspace(min(fl_z), max(fl_z), 100)
    r_grid, z_grid = np.meshgrid(r_space, z_space)
    xcorr_grid = matplotlib.mlab.griddata(fl_r, fl_z, xcorrs, r_grid, z_grid, 
                                          interp='linear')

    # Plot camera image with field line overlay
    fig, ax = plt.subplots()
    fig.suptitle('Shot {}'.format(shot))

    plt.subplot(121)
    plt.title('Divertor camera view')
    plasma_image = plt.imshow(frames[frame_index], cmap=plt.cm.gray, 
                              origin='bottom')
    overlay_cmap = make_colormap([(1., 0., 0., 0.), (1., 0., 0., 1.)])
    fl_image = plt.imshow(fls[indices[-1]], cmap=overlay_cmap, origin='bottom',
                          alpha=0.8)
    plt.axis('off')

    # Plot field line R, Z data in context of machine
    plt.subplot(122)
    plt.title('Toroidal cross section')
    xcorr_image = plt.pcolormesh(r_grid, z_grid, xcorr_grid)
    colorbar = plt.colorbar()
    colorbar.set_label('Cross-correlation')
    plt.plot(machine_x, machine_y, color='gray')
    l, = plt.plot(rlcfs[efit_t_index], zlcfs[efit_t_index], color='fuchsia')
    plt.axis('equal')
    plt.xlim([.49, .62])
    plt.ylim([-.50, -.33])
    plt.xlabel('R (m)')
    plt.ylabel('Z (m)')
    f, = plt.plot(fl_r[indices[-1]], fl_z[indices[-1]], 'ro')
    plt.contour(flux[efit_t_index], 100, extent=flux_extent)
    
    # Slider and button settings
    fl_slide_area = plt.axes([0.20, 0.02, 0.60, 0.03])
    fl_slider = Slider(fl_slide_area, 'Correlation rank', 0, len(fls)-1, 
                       valinit=0)
    fl_slider.valfmt = '%d'
    phantom_slide_area = plt.axes([0.20, 0.06, 0.60, 0.03])
    phantom_slider = Slider(phantom_slide_area, 'Camera frame', 0, len(frames)-1,
                        valinit=0)
    phantom_slider.valfmt = '%d'
    forward_button_area = plt.axes([0.95, 0.06, 0.04, 0.04])
    forward_button = Button(forward_button_area, '>')
    back_button_area = plt.axes([0.95, 0.01, 0.04, 0.04])
    back_button = Button(back_button_area, '<')

    def update_data(val):
        global frame_index, indices, xcorr_grid
        frame_index = int(val)
        for i, fl in enumerate(fls):
            xcorrs[i] = signals.cross_correlation(frames[frame_index], fl) 
        xcorr_grid = matplotlib.mlab.griddata(fl_r, fl_z, xcorrs, r_grid, 
                                              z_grid, interp='linear')
        indices = np.argsort(xcorrs)[::-1]
        update_images(0)
    
    def update_images(val):
        global frame_index, indices, xcorr_grid
        val = int(val)
        efit_t_index = process.find_nearest(efit_times, time[frame_index], ordered=True)
        plasma_image.set_array(frames[frame_index])
        plasma_image.autoscale()
        fl_image.set_array(fls[indices[val]])
        fl_image.autoscale()
        f.set_xdata(fl_r[indices[val]])
        f.set_ydata(fl_z[indices[val]])
        l.set_xdata(rlcfs[efit_t_index])
        l.set_ydata(zlcfs[efit_t_index])
        xcorr_image.set_array(xcorr_grid[:-1, :-1].ravel())
        fig.canvas.draw_idle()

    def forward(event):
        global frame_index
        frame_index += 1
        update_data(frame_index)

    def backward(event):
        global frame_index
        frame_index -= 1
        update_data(frame_index)
    
    fl_slider.on_changed(update_images)
    phantom_slider.on_changed(update_data)
    forward_button.on_clicked(forward)
    back_button.on_clicked(backward)

    plt.tight_layout(rect=(0, .1, 1, .9))
    plt.show()
Esempio n. 5
0
def slide_reconstruction(shot, smoothing_param=100, save=False):
    """
    Slide through Phantom camera frames and their reconstructions from
    synthetic field line images calculated externally with Julia.
    
    Args:
        shot: [int] shot number
    """
    # Read cache files broken up by EFIT time segment
    old_working_dir = os.getcwd()
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    emissivity_files = sorted(glob.glob('../cache/fl_emissivities_Xpt_{}_sp{}_*'.format(shot, smoothing_param)))
    fl_data_files = sorted(glob.glob('../cache/fl_data_Xpt_{}_*'.format(shot)))
    fl_image_files = sorted(glob.glob('../cache/fl_images_Xpt_{}_*'.format(shot)))
    if len(emissivity_files) == 0 or len(fl_data_files) == 0 or len(fl_image_files) == 0:
        raise Exception('Could not find files necessary to view reconstruction')
    emissivities = [np.load(f) for f in emissivity_files]
    fl_data = [scipy.io.idl.readsav(f) for f in fl_data_files]
    fl_images = [np.load(f) for f in fl_image_files]
    os.chdir(old_working_dir)

    frame_index = 0
    times = acquire.gpi_series(shot, 'phantom2', 'time')
    frames = acquire.video(shot, 'phantom2', sub=20, sobel=False)
    fl_rs = [f.fieldline_r for f in fl_data]
    fl_zs = [f.fieldline_z for f in fl_data]
    rlcfs, zlcfs = acquire.lcfs_rz(shot)
    efit_times, flux, flux_extent = acquire.time_flux_extent(shot)
    machine_x, machine_y = acquire.machine_cross_section()
    efit_t_index = process.find_nearest(efit_times, times[0], ordered=True)
    efit_phantom_start_index = efit_t_index
    previous_segments_frame_count = [sum([len(e) for e in emissivities[:t_index]]) for t_index in range(len(emissivities))]

    geomatrices = [np.transpose(np.array([fl.flatten() for fl in fl_image_set])) for fl_image_set in fl_images]
    reconstructed = geomatrices[0].dot(emissivities[0][0])
    reconstructed = reconstructed.reshape((64,64))

    fl_r_all = np.concatenate(fl_rs)
    fl_z_all = np.concatenate(fl_zs)
    r_space = np.linspace(np.min(fl_r_all), np.max(fl_r_all), 100)
    z_space = np.linspace(np.min(fl_z_all), np.max(fl_z_all), 100)
    r_grid, z_grid = np.meshgrid(r_space, z_space)
    emissivity_grid = make_grid(fl_rs[0], fl_zs[0], r_grid, z_grid, emissivities[0][0])
    
    # Draw figure using first frame
    fig, ax = plt.subplots()
    title = plt.suptitle('Shot {} frame {}'.format(shot, 0))
    plt.tight_layout(rect=(0, .1, 1, .9))

    plt.subplot(221)
    plt.title('Divertor camera view')
    plasma_image = plt.imshow(frames[0], cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    #plt.colorbar()
    
    plt.subplot(222)
    plt.title('Reconstruction')
    reconstruction_image = plt.imshow(reconstructed, cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    #plt.colorbar()
    
    plt.subplot(223)
    plt.title('Original minus reconstruction')
    error_image = plt.imshow(frames[0] - reconstructed, cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    #plt.colorbar()
    
    plt.subplot(224)
    plt.title('Toroidal cross section')
    emissivity_image = plt.pcolormesh(r_grid, z_grid, emissivity_grid, cmap=plt.cm.plasma)
    #colorbar = plt.colorbar()
    #colorbar.set_label('Relative emissivity')
    plt.axis('equal')
    plt.plot(machine_x, machine_y, color='gray')
    l, = plt.plot(rlcfs[efit_phantom_start_index], 
                  zlcfs[efit_phantom_start_index], color='orange', alpha=0.5)
    plt.xlim([.49, .62])
    plt.ylim([-.50, -.33])
    plt.xlabel('R (m)')
    plt.ylabel('Z (m)')
    plt.contour(flux[efit_t_index], 300, extent=flux_extent, alpha=0.5)
    plt.gca().set_axis_bgcolor('black')

    if not save:
        phantom_slide_area = plt.axes([0.20, 0.02, 0.60, 0.03])
        phantom_slider = Slider(phantom_slide_area, 'Camera frame', 0, len(frames)-1,
                            valinit=0)
        phantom_slider.valfmt = '%d'
        cutoff_slider_area = plt.axes([0.20, 0.05, 0.60, 0.03])
        cutoff_slider = Slider(cutoff_slider_area, 'Cutoff', 0, 100, valinit=0)
        cutoff_slider.valfmt = '%d%%'
        forward_button_area = plt.axes([0.95, 0.06, 0.04, 0.04])
        forward_button = Button(forward_button_area, '>')
        back_button_area = plt.axes([0.95, 0.01, 0.04, 0.04])
        back_button = Button(back_button_area, '<')

    def update_frame(val):
        global efit_t_index, frame_index
        frame_index = int(val)
        cutoff = cutoff_slider.val

        # Recalculate things
        efit_t_index = process.find_nearest(efit_times, times[frame_index], ordered=True)
        efit_rel_index = efit_t_index - efit_phantom_start_index
        frame_rel_index = frame_index - previous_segments_frame_count[efit_rel_index]
        emissivity_cutoff = cutoff_array(emissivities[efit_rel_index][frame_rel_index], cutoff)
        emissivity_grid = make_grid(fl_rs[efit_rel_index], fl_zs[efit_rel_index], r_grid, z_grid, emissivity_cutoff) 
        reconstructed = geomatrices[efit_rel_index].dot(emissivity_cutoff).reshape((64,64))

        # Update canvas
        plasma_image.set_array(frames[frame_index])
        plasma_image.autoscale()
        reconstruction_image.set_array(reconstructed)
        reconstruction_image.autoscale()
        emissivity_image.set_array(emissivity_grid[:-1, :-1].ravel())
        emissivity_image.autoscale()
        error_image.set_array(frames[frame_index] - reconstructed)
        error_image.autoscale()
        l.set_xdata(rlcfs[efit_t_index])
        l.set_ydata(zlcfs[efit_t_index])
        title.set_text('Shot {} frame {}'.format(shot, frame_index))

    def forward(event):
        global frame_index
        phantom_slider.set_val(frame_index + 1)

    def backward(event):
        global frame_index
        phantom_slider.set_val(frame_index - 1)

    def update_cutoff(val):
        global frame_index
        update_frame(frame_index)

    if save:
        FFMpegWriter = animation.writers['ffmpeg']
        writer = FFMpegWriter(fps=15, bitrate=5000)
        anim = animation.FuncAnimation(fig, update_frame, frames=1000, interval=5, blit=False)
        anim.save('{}_sp{}.avi'.format(shot, smoothing_param), writer=writer)
        plt.close(fig)
    else:
        phantom_slider.on_changed(update_frame)
        cutoff_slider.on_changed(update_cutoff)
        forward_button.on_clicked(forward)
        back_button.on_clicked(backward)
        plt.show()
Esempio n. 6
0
def slide_reconstruct(shot, fl_sav, smoothing_param=100):
    """
    Slide through Phantom camera frames using given synthetic field line images 
    to create a reconstruction of the original using non-negative least squares
    (NNLS) fitting. The NNLS method used here is slow, so there are long delays 
    between clicks and interface updates.
    
    Args:
        shot: [int] shot number
        fl_sav: [scipy.io.idl.readsav object] containing field line images
        smoothing_param: [float] least-squares smoothing parameter
    """
    time = acquire.gpi_series(shot, 'phantom2', 'time')
    frames = acquire.video(shot, 'phantom2', sub=20, sobel=False)
    frame_index = 0
    fl_images = fl_sav.fl_image
    fl_r = fl_sav.fieldline_r
    fl_z = fl_sav.fieldline_z
    rlcfs, zlcfs = acquire.lcfs_rz(shot)
    efit_times, flux, flux_extent = acquire.time_flux_extent(shot)
    efit_t_index = process.find_nearest(efit_times, time[0], ordered=True)
    machine_x, machine_y = acquire.machine_cross_section()

    inversion_func = scipy.optimize.nnls
    geomatrix = np.transpose(np.array([fl.flatten() for fl in fl_images]))
    geomatrix_smooth = np.concatenate((geomatrix, 
                                       smoothing_param*np.identity(len(fl_images))))
    target = frames[0]
    target_smooth = np.concatenate((target.flatten(), np.zeros(len(fl_images))))
    inv = inversion_func(geomatrix_smooth, target_smooth)

    r_space = np.linspace(min(fl_r), max(fl_r), 100)
    z_space = np.linspace(min(fl_z), max(fl_z), 100)
    r_grid, z_grid = np.meshgrid(r_space, z_space)
    emissivity_grid = matplotlib.mlab.griddata(fl_r, fl_z, inv[0], r_grid,
                                               z_grid, interp='linear') 

    reconstructed = geomatrix.dot(inv[0])
    reconstructed = reconstructed.reshape((64,64))
    target = target.reshape((64,64))
    fig, ax = plt.subplots()
    
    plt.subplot(221)
    plt.title('Divertor camera view')
    plasma_image = plt.imshow(target, cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    plt.colorbar()
    
    plt.subplot(222)
    plt.title('Reconstruction')
    reconstruction_image = plt.imshow(reconstructed, cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    plt.colorbar()
    
    plt.subplot(223)
    plt.title('Reconstruction minus original')
    error_image = plt.imshow(target - reconstructed, cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    plt.colorbar()
    
    plt.subplot(224)
    plt.title('Toroidal cross section')
    emissivity_image = plt.pcolormesh(r_grid, z_grid, emissivity_grid)
    colorbar = plt.colorbar()
    colorbar.set_label('Relative emissivity')
    plt.axis('equal')
    plt.plot(machine_x, machine_y, color='gray')
    l, = plt.plot(rlcfs[efit_t_index], zlcfs[efit_t_index], color='fuchsia')
    plt.xlim([.49, .62])
    plt.ylim([-.50, -.33])
    plt.xlabel('R (m)')
    plt.ylabel('Z (m)')
    plt.contour(flux[efit_t_index], 100, extent=flux_extent)

    phantom_slide_area = plt.axes([0.20, 0.02, 0.60, 0.03])
    phantom_slider = Slider(phantom_slide_area, 'Camera frame', 0, len(frames)-1,
                        valinit=0)
    phantom_slider.valfmt = '%d'
    forward_button_area = plt.axes([0.95, 0.06, 0.04, 0.04])
    forward_button = Button(forward_button_area, '>')
    back_button_area = plt.axes([0.95, 0.01, 0.04, 0.04])
    back_button = Button(back_button_area, '<')

    def update_data(val):
        global frame_index, emissivity_grid, reconstructed
        frame_index = int(val)
        target_smooth = np.concatenate((frames[frame_index].flatten(), np.zeros(len(fl_images))))
        inv = inversion_func(geomatrix_smooth, target_smooth)
        # inv = inversion_func(geomatrix, target)
        reconstructed = geomatrix.dot(inv[0]).reshape((64,64))
        emissivity_grid = matplotlib.mlab.griddata(fl_r, fl_z, inv[0], r_grid, 
                                              z_grid, interp='linear')
        plasma_image.set_array(frames[frame_index])
        reconstruction_image.set_array(reconstructed)
        emissivity_image.set_array(emissivity_grid[:-1, :-1].ravel())
        error_image.set_array(frames[frame_index]-reconstructed)
        error_image.autoscale()
        plasma_image.autoscale()
        reconstruction_image.autoscale()
        emissivity_image.autoscale()
        efit_t_index = process.find_nearest(efit_times, time[frame_index], ordered=True)
        l.set_xdata(rlcfs[efit_t_index])
        l.set_ydata(zlcfs[efit_t_index])
        fig.canvas.draw_idle()

    def forward(event):
        global frame_index
        frame_index += 1
        update_data(frame_index)

    def backward(event):
        global frame_index
        frame_index -= 1
        update_data(frame_index)

    phantom_slider.on_changed(update_data)
    forward_button.on_clicked(forward)
    back_button.on_clicked(backward)

    plt.tight_layout(rect=(0, .1, 1, .9))
    plt.show()
Esempio n. 7
0
                    fontsize=10)

#initiate speed meter
ax4 = plt.subplot(gs[5:9, 2])
smeter, = plt.bar(0, fuel, facecolor='#54dd5b', edgecolor='black')
plt.axis([0, fmeter.get_width() / 3, 0, yspeed])
ax4.set_xticks([0.5 * fmeter.get_width()])
ax4.set_xticklabels(['Speed'], color='white')
ax4.set_ylim([0, 15])
ax4.set_yticks([0])
ax4.set_yticklabels(["%2.1f m/s" % yspeed], color='white', fontsize=10)
ax4.yaxis.tick_right()

axThrust = plt.axes([0.18, 0.025, 0.65, 0.04])
thrust = Slider(axThrust, 'Thrust', 0.0, maxThrust, color='#8602e5')
thrust.valfmt = '%.f N'

ctypes.windll.user32.MessageBoxW(0, openMessage, 'Welcome!', 1)

fig.canvas.mpl_connect('key_press_event', press)

timeStamps = np.array([0])
heightStamps = np.array([height])

t = 0
del_t = 0

while height > 0:

    if len(plt.get_fignums()) == 0:
        sys.exit("Program closed")