def draw_sample_incorrectmatrix(datasettxt, sortedBuckets, incorrectmatix, dataset, cmap=None):
    n_samples = 11
    n_labels = 10

    # size of each sample
    fig = plt.figure(figsize=(n_samples * 1.8, n_labels))
    w_ratios = [1 for n in range(n_samples)]
    w_ratios[:0] = [int(n_samples * 0.8)]
    h_ratios = [1 for n in range(n_labels)]

    # gridspec
    time.sleep(1)  # wait for 1 second for the previous print to appear!
    grid = gridspec.GridSpec(n_labels, n_samples + 1, wspace=0.0, hspace=0.0, width_ratios=w_ratios,
                             height_ratios=h_ratios)
    labelset_pbar = tqdm(range(n_labels), desc=datasettxt, unit='labels')
    for a in labelset_pbar:
        cclassId = incorrectmatrix[sortedBuckets[n_labels - a - 1]]['correct']
        pclassId = incorrectmatrix[sortedBuckets[n_labels - a - 1]]['predicted']
        cdescription = classLabelList[classLabelList.ClassId == cclassId].SignName.to_string(header=False, index=False)
        pdescription = classLabelList[classLabelList.ClassId == pclassId].SignName.to_string(header=False, index=False)
        count = incorrectmatrix[sortedBuckets[n_labels - a - 1]]['count']
        for b in range(n_samples):
            i = a * (n_samples + 1) + b
            ax = plt.Subplot(fig, grid[i])
            if b == 0:
                ax.annotate(
                    'CClassId %d (%d): %s\nPClassId %d: %s' % (cclassId, count, cdescription, pclassId, pdescription),
                    xy=(0, 0), xytext=(0.0, 0.3))
                ax.set_xticks([])
                ax.set_yticks([])
                fig.add_subplot(ax)
            else:
                random_i = random.choice(incorrectmatrix[sortedBuckets[n_labels - a - 1]]['samples'])
                image = dataset[random_i]
                if cmap == None:
                    ax.imshow(image)
                else:
                    # yuv = cv2.split(image)
                    # ax.imshow(yuv[0], cmap=cmap)
                    ax.imshow(image, cmap=cmap)
                ax.set_xticks([])
                ax.set_yticks([])
                fig.add_subplot(ax)

        # We also plot the GT image on the right
        i = a * (n_samples + 1) + n_samples
        ax = plt.Subplot(fig, grid[i])

        img_idx = np.where(y_train == pclassId)
        random_i = random.choice(img_idx[0])
        image = X_train[random_i]
        if cmap == None:
            ax.imshow(image)
        else:
            ax.imshow(image, cmap=cmap)
        ax.set_xticks([])
        ax.set_yticks([])
        fig.add_subplot(ax)

        # hide the borders\
        if a == (n_labels - 1):
            all_axes = fig.get_axes()
            for ax in all_axes:
                for sp in ax.spines.values():
                    sp.set_visible(False)

    plt.show()
pylab.figure(2,figsize=(6,1))
dendrogram_fig = pylab.gcf()

divergence_grid = gridspec.GridSpec(1, 2, width_ratios=[4,1], wspace=0.025)
                 
dendrogram_grid = gridspec.GridSpec(1, 2, width_ratios=[8,1], wspace=0.025)



###################
#
# SNP change panel
#
###################

snp_axis = plt.Subplot(divergence_fig, divergence_grid[0])
divergence_fig.add_subplot(snp_axis)

#snp_axis.set_title("%s %s (%s)" % tuple(species_name.split("_")),fontsize=7)

snp_axis.set_ylabel('Divergence, $d$')
snp_axis.set_xlabel('Ranked host pairs')
snp_axis.set_ylim([1e-06,1e-01])
snp_axis.set_xlim([-512,5])
snp_axis.set_xticks([])

snp_axis.spines['top'].set_visible(False)
snp_axis.spines['right'].set_visible(False)
snp_axis.get_xaxis().tick_bottom()
snp_axis.get_yaxis().tick_left()
예제 #3
0
metapopulation_colors = {
    'nonmutator': figure_utils.nonmutator_group_color,
    'mutator': figure_utils.mutator_group_color
}

####
#
# Set up figure
#
####

fig = plt.figure(figsize=(4, 1.7))

grid = gridspec.GridSpec(1, 2)

dnds_axis = plt.Subplot(fig, grid[0])
fig.add_subplot(dnds_axis)

dnds_axis.spines['top'].set_visible(False)
dnds_axis.spines['right'].set_visible(False)
dnds_axis.get_xaxis().tick_bottom()
dnds_axis.get_yaxis().tick_left()

dnds_axis.set_ylabel('dN/dS')
dnds_axis.set_ylim([0, 3.5])
dnds_axis.set_yticks([0, 1, 2, 3, 4])
dnds_axis.set_xlim([-1, 2])
dnds_axis.set_xticks([0, 1])
dnds_axis.set_xticklabels(['All', 'Fixed'], rotation='vertical')
dnds_axis.plot([-4, 4], [1, 1], '-', linewidth=0.25, color='0.7')
예제 #4
0
파일: csd.py 프로젝트: Neurosim-lab/netpyne
def plotCSD(CSD_data=None,
            LFP_input_data=None,
            overlay=None,
            timeRange=None,
            sampr=None,
            stim_start_time=None,
            spacing_um=None,
            ymax=None,
            dt=None,
            hlines=False,
            layerLines=False,
            layerBounds=None,
            smooth=True,
            fontSize=12,
            figSize=(8, 8),
            dpi=200,
            saveFig=True,
            showFig=True):
    """
    Function to plot CSD values extracted from simulated LFP data 
      
    Parameters
    ----------
    CSD_data : list or array
        CSD_data for plotting.
        **Default:** ``None`` 

    LFP_input_data : list or numpy array 
        LFP data provided by user (mV).  Each element of the list/array must be a list/array containing LFP data for an electrode. 
        **Default:** ``None`` pulls the data from the current NetPyNE sim object.


    overlay : str
        Option to include LFP data overlaid on CSD color map plot. 
        **Default:** ``None`` provides no overlay 
        OPTIONS are 'LFP' or 'CSD'

    timeRange : list
        Time range to plot [start, stop].
        **Default:** ``None`` plots entire time range

    sampr : float
        Sampling rate for data recording (Hz).  
        **Default:** ``None`` uses ``1.0/sim.cfg.recordStep`` from the current NetPyNE sim object. 
      
    stim_start_time : float 
        Time when stimulus is applied (ms). 
        **Default:** ``None`` does not add anything to plot. 
        **Options:** a float adds a vertical dashed line to the plot at stimulus onset. 

    spacing_um : float
        Electrode contact spacing in units of microns.
        **Default:** ``None`` pulls the information from the current NetPyNE sim object.  If the data is empirical, defaults to ``100`` (microns).

    ymax : float
        The upper y-limit.
        **Default:** ``None`` 

    dt : float
        Time between recording points (ms). 
        **Default:** ``None`` uses ``sim.cfg.recordStep`` from the current NetPyNE sim object.

    hlines : bool
        Option to include horizontal lines on plot to indicate electrode positions. 
        **Default:** ``False`` 

    layerLines : bool 
        Whether to plot horizontal lines over CSD plot at layer boundaries. 
        **Default:** ``False`` 

    layerBounds : dict
        Dictionary containing layer labels as keys, and layer boundaries as values, e.g. {'L1':100, 'L2': 160, 'L3': 950, 'L4': 1250, 'L5A': 1334, 'L5B': 1550, 'L6': 2000}
        **Default:** ``None``

    saveFig : bool or str
        Whether and where to save the figure. 
        **Default:** ``True`` autosaves the figure.
        **Options:** ``'/path/filename.ext'`` saves to a custom path and filename, valid file extensions are ``'.png'``, ``'.jpg'``, ``'.eps'``, and ``'.tiff'``.

    showFig : bool
        Whether to show the figure. 
        **Default:** ``True``

    """

    print('Plotting CSD... ')

    # DEFAULT -- CONDITION 1 : GET CSD DATA FROM SIM
    if CSD_data is None:

        from .. import sim
        LFP_data, CSD_data, sampr, spacing_um, dt = getCSD(
            getAllData=True
        )  #getCSD(sampr=sampr, spacing_um=spacing_um, dt=dt, getAllData=True)

        if timeRange is None:
            timeRange = [0, sim.cfg.duration]

        tt = np.arange(timeRange[0], timeRange[1], dt)

        ymax = sim.cfg.recordLFP[-1][1] + spacing_um

        LFP_data = np.array(LFP_data)[int(timeRange[0] / dt):int(timeRange[1] /
                                                                 dt), :]
        CSD_data = CSD_data[:, int(timeRange[0] / dt):int(timeRange[1] / dt)]

        CSD_data_noBandpass = sim.allSimData['CSD'][
            'CSD_data_noBandpass'][:,
                                   int(timeRange[0] / sim.cfg.recordStep
                                       ):int(timeRange[1] /
                                             sim.cfg.recordStep)]
        #CSD_data_noBandpass = CSD_data_noBandpass[:,int(timeRange[0]/dt):int(timeRange[1]/dt)]

        ### The problem with this setup is that it defaults to whatever was saved in .pkl !!
        # sim_data_categories = sim.allSimData.keys()

        # if 'CSD' in sim_data_categories:

        #     if timeRange is None:
        #         timeRange = [0,sim.cfg.duration]

        #     dt = sim.cfg.recordStep
        #     tt = np.arange(timeRange[0],timeRange[1],dt)

        #     spacing_um = sim.allSimData['CSD']['spacing_um']
        #     #spacing_mm = spacing_um/1000

        #     ymax = sim.cfg.recordLFP[-1][1] + spacing_um

        #     # get LFP data
        #     LFP_data = np.array(sim.allSimData['LFP'])[int(timeRange[0]/sim.cfg.recordStep):int(timeRange[1]/sim.cfg.recordStep),:]

        #     # get CSD data
        #     CSD_data = sim.allSimData['CSD']['CSD_data'][:,int(timeRange[0]/sim.cfg.recordStep):int(timeRange[1]/sim.cfg.recordStep)]

        #     # noBandpass trial
        #     CSD_data_noBandpass = sim.allSimData['CSD']['CSD_data_noBandpass'][:,int(timeRange[0]/sim.cfg.recordStep):int(timeRange[1]/sim.cfg.recordStep)]

        # else:
        #     raise Exception('No CSD data in sim.')

    # CONDITION 2 : ARBITRARY CSD DATA
    elif CSD_data is not None:
        if timeRange is None:
            print('MUST PROVIDE TIME RANGE in ms')
        else:
            print('timeRange = ' + str(timeRange))

        if dt is None:
            print('MUST PROVIDE dt in ms')
        else:
            print('dt = ' + str(dt))  # batch0['simConfig']['recordStep']

        if spacing_um is None:
            print('MUST PROVIDE SPACING BETWEEN ELECTRODES in MICRONS')
        else:
            print('spacing_um = ' + str(spacing_um))

        if ymax is None:
            print('MUST PROVIDE YMAX (MAX DEPTH) in MICRONS')
        else:
            print('ymax = ' + str(ymax))

        tt = np.arange(timeRange[0], timeRange[1], dt)
        LFP_data = np.array(LFP_input_data)[int(timeRange[0] /
                                                dt):int(timeRange[1] / dt), :]

    # PLOTTING
    X = np.arange(timeRange[0], timeRange[1], dt)
    Y = np.arange(CSD_data.shape[0])

    # interpolation
    CSD_spline = scipy.interpolate.RectBivariateSpline(Y, X, CSD_data)
    Y_plot = np.linspace(0, CSD_data.shape[0], num=1000)
    Z = CSD_spline(Y_plot, X)

    # plotting options
    plt.rcParams.update({'font.size': fontSize})
    xmin = int(X[0])
    xmax = int(X[-1]) + 1  #int(sim.allSimData['t'][-1])
    ymin = 0
    extent_xy = [xmin, xmax, ymax, ymin]

    # set up figure
    fig = plt.figure(figsize=figSize)

    # create plots w/ common axis labels and tick marks
    axs = []
    numplots = 1
    gs_outer = matplotlib.gridspec.GridSpec(
        1, 1
    )  #(2, 2, figure=fig)#, wspace=0.4, hspace=0.2, height_ratios=[20, 1])

    for i in range(numplots):
        axs.append(plt.Subplot(fig, gs_outer[i * 2:i * 2 + 2]))
        fig.add_subplot(axs[i])
        axs[i].set_xlabel('Time (ms)', fontsize=fontSize)
        axs[i].tick_params(axis='y', which='major', labelsize=fontSize)
        axs[i].tick_params(axis='x', which='major', labelsize=fontSize)

    ## plot interpolated CSD color map
    #if smooth:
    #    Z = scipy.ndimage.filters.gaussian_filter(Z, sigma = 5, mode='nearest')#smooth, mode='nearest')

    spline = axs[0].imshow(Z,
                           extent=extent_xy,
                           interpolation='none',
                           aspect='auto',
                           origin='upper',
                           cmap='jet_r',
                           alpha=0.9)
    axs[0].set_ylabel('Contact depth (um)', fontsize=fontSize)

    # OVERLAY DATA ('LFP', 'CSD', or None) & Set title of plot
    if overlay is None:
        print('No data being overlaid')
        axs[0].set_title('Current Source Density (CSD)', fontsize=fontSize)

    elif overlay is 'CSD' or overlay is 'LFP':
        nrow = LFP_data.shape[1]
        gs_inner = matplotlib.gridspec.GridSpecFromSubplotSpec(
            nrow, 1, subplot_spec=gs_outer[0:2], wspace=0.0, hspace=0.0)
        subaxs = []

        if overlay == 'CSD':
            axs[0].set_title('CSD with time series overlay', fontsize=fontSize)
            for chan in range(nrow):
                subaxs.append(plt.Subplot(fig, gs_inner[chan], frameon=False))
                fig.add_subplot(subaxs[chan])
                subaxs[chan].margins(0.0, 0.01)
                subaxs[chan].get_xaxis().set_visible(False)
                subaxs[chan].get_yaxis().set_visible(False)
                subaxs[chan].plot(X,
                                  CSD_data[chan, :],
                                  color='green',
                                  linewidth=0.3)  # 'blue'

        elif overlay == 'LFP':
            axs[0].set_title('CSD with LFP overlay', fontsize=fontSize)
            for chan in range(nrow):
                subaxs.append(plt.Subplot(fig, gs_inner[chan], frameon=False))
                fig.add_subplot(subaxs[chan])
                subaxs[chan].margins(0.0, 0.01)
                subaxs[chan].get_xaxis().set_visible(False)
                subaxs[chan].get_yaxis().set_visible(False)
                subaxs[chan].plot(X,
                                  LFP_data[:, chan],
                                  color='gray',
                                  linewidth=0.3)

    else:
        print(
            'Invalid option specified for overlay argument -- no data overlaid'
        )
        axs[0].set_title('Current Source Density (CSD)', fontsize=fontSize)

    # add horizontal lines at electrode locations
    if hlines:
        for i in range(len(sim.cfg.recordLFP)):
            axs[0].hlines(sim.cfg.recordLFP[i][1],
                          xmin,
                          xmax,
                          colors='pink',
                          linewidth=1,
                          linestyles='dashed')

    if layerLines:
        if layerBounds is None:
            print(
                'No layer boundaries given -- will not overlay layer boundaries on CSD plot'
            )
        else:
            layerKeys = []
            for i in layerBounds.keys():
                axs[0].hlines(layerBounds[i],
                              xmin,
                              xmax,
                              colors='black',
                              linewidth=1,
                              linestyles='dotted')
                layerKeys.append(
                    i
                )  # makes a list with names of each layer, as specified in layerBounds dict argument

            for n in range(
                    len(layerKeys)
            ):  # label the horizontal layer lines with the proper layer label
                if n == 0:
                    axs[0].text(xmax + 5,
                                layerBounds[layerKeys[n]] / 2,
                                layerKeys[n],
                                color='black',
                                fontsize=fontSize)
                else:
                    axs[0].text(xmax + 5,
                                (layerBounds[layerKeys[n]] +
                                 layerBounds[layerKeys[n - 1]]) / 2,
                                layerKeys[n],
                                color='black',
                                fontsize=fontSize,
                                verticalalignment='center')

    # set vertical line at stimulus onset
    if type(stim_start_time) is int or type(stim_start_time) is float:
        axs[0].vlines(stim_start_time,
                      ymin,
                      ymax,
                      colors='red',
                      linewidth=1,
                      linestyles='dashed')

    # save figure
    if saveFig:
        if isinstance(saveFig, basestring):
            filename = saveFig
        else:
            filename = sim.cfg.filename + '_CSD.png'
        try:
            plt.savefig(filename, dpi=dpi)
        except:
            plt.savefig('CSD_fig.png', dpi=dpi)

    # display figure
    if showFig:
        plt.show()
예제 #5
0
def test(epoch, plot_intv=25):
    model.eval()
    loss = 0.
    for batch_idx, (input, target) in enumerate(test_loader):
        input, target = input.to(device), target.to(device)
        with th.no_grad():
            output = model(input)
        loss += F.mse_loss(output, target, size_average=False).item()

        # plot predictions
        if epoch % plot_intv == 0 and batch_idx == len(test_loader) - 1:
            n_samples = 4
            idx = th.LongTensor(
                np.random.choice(args.n_test, n_samples, replace=False))

            for i in range(n_samples):
                x = x_test[[idx[i]]]
                samples_target = y_test[idx[i]]
                x_tensor = (th.FloatTensor(x)).to(device)
                y_hat = model(x_tensor)
                samples_output = y_hat[0].data.cpu().numpy()
                samples_err = samples_target - samples_output
                samples = np.vstack(
                    (samples_target, samples_output, samples_err))

                Nout = y_test.shape[1]
                c_max = np.full((Nout * 3), 0.0)
                for l in range(Nout * 3):
                    if l < Nout:
                        c_max[l] = np.max(samples[l])
                    elif Nout <= l < 2 * Nout:
                        c_max[l] = np.max(samples[l])
                        if c_max[l] > c_max[l - Nout]:
                            c_max[l - Nout] = c_max[l]
                        else:
                            c_max[l] = c_max[l - Nout]
                    else:
                        c_max[l] = np.max(np.abs(samples[l]))

                LetterId = ([
                    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm'
                ])
                ylabel = ([
                    '$\mathbf{y}$', '$\hat{\mathbf{y}}$',
                    '$\mathbf{y}-\hat{\mathbf{y}}$'
                ])
                fig = plt.figure(figsize=(4 * 4 - 0.5, 10))
                outer = gridspec.GridSpec(2, 1, wspace=0.01, hspace=0.06)
                nl = 40
                m = 0
                samp_id = [[0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19],
                           [4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23]]
                for j in range(2):
                    inner = gridspec.GridSpecFromSubplotSpec(
                        3, 4, subplot_spec=outer[j], wspace=0.2, hspace=0.08)
                    l = 0
                    for k in range(3 * 4):
                        ax = plt.Subplot(fig, inner[k])
                        ax.set_aspect('equal')
                        ax.set_xticks([])
                        ax.set_yticks([])
                        s_id = samp_id[j][k]
                        if k < 2 * 4:
                            cax = ax.contourf(
                                samples[s_id],
                                np.arange(0.0,
                                          c_max[s_id] + c_max[s_id] / nl * 1,
                                          c_max[s_id] / nl),
                                cmap='jet')
                            fig.add_subplot(ax)
                        else:
                            cax = ax.contourf(
                                samples[s_id],
                                np.arange(
                                    0.0 - c_max[s_id] - c_max[s_id] / nl * 1,
                                    c_max[s_id] + c_max[s_id] / nl * 1,
                                    c_max[s_id] / nl),
                                cmap='jet',
                                extend='both')
                            fig.add_subplot(ax)
                        ax.spines['left'].set_color('white')
                        ax.spines['right'].set_color('white')
                        ax.spines['bottom'].set_color('white')
                        ax.spines['top'].set_color('white')
                        cbar = plt.colorbar(cax,
                                            ax=ax,
                                            fraction=0.021,
                                            pad=0.04,
                                            format=ticker.FuncFormatter(
                                                lambda x, pos: "%.3f" % x))
                        cbar.ax.tick_params(labelsize=10)

                        if k < 4:
                            if j == 1 and k == 3:
                                ax.text(2,
                                        33,
                                        '$({})$ head [L]'.format(LetterId[m]),
                                        fontsize=14,
                                        color='white')
                            else:
                                ax.text(2,
                                        33,
                                        '$({})\ t={}$ [T]'.format(
                                            LetterId[m], (m + 1) * 2),
                                        fontsize=14,
                                        color='white')
                            m = m + 1
                        if np.mod(k, 4) == 0:
                            if j == 0:
                                ax.set_ylabel(ylabel[l], fontsize=14)
                                l = 1 + l
                            else:
                                ax.set_ylabel(ylabel[l], fontsize=14)
                                l = 1 + l

                plt.savefig(output_dir +
                            '/epoch_{}_output_{}.png'.format(epoch, idx[i]),
                            bbox_inches='tight',
                            dpi=400)
                plt.close(fig)
                print("epoch {}, done with printing sample output {}".format(
                    epoch, idx[i]))

    rmse_test = np.sqrt(loss / n_out_pixels_test)
    r2_score = 1 - loss / y_test_var
    print("epoch: {}, test r2-score:  {:.6f}".format(epoch, r2_score))
    return r2_score, rmse_test
예제 #6
0
def make_ticklabels_invisible(fig):
    for i, ax in enumerate(fig.axes):
        ax.text(0.5, 0.5, "ax%d" % (i+1), va = "center", ha = "center")
        ax.tick_params(labelbottom = False, labelleft = False)


# gridspec inside gridspec

f = plt.figure()

gs0 = gridspec.GridSpec(1, 2)

gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec = gs0[0])

ax1 = plt.Subplot(f, gs00[:-1, :])
f.add_subplot(ax1)
ax2 = plt.Subplot(f, gs00[-1, :-1])
f.add_subplot(ax2)
ax3 = plt.Subplot(f, gs00[-1, -1])
f.add_subplot(ax3)


gs01 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec = gs0[1])

ax4 = plt.Subplot(f, gs01[:, :-1])
f.add_subplot(ax4)
ax5 = plt.Subplot(f, gs01[:-1, -1])
f.add_subplot(ax5)
ax6 = plt.Subplot(f, gs01[-1, -1])
f.add_subplot(ax6)
예제 #7
0
pred_images = get_images(os.getcwd() + "/archive/seg_pred/")

fig = plt.figure(figsize=(30, 30))
outer = gridspec.GridSpec(5, 5, wspace=0.2, hspace=0.2)

for i in range(25):
    inner = gridspec.GridSpecFromSubplotSpec(2,
                                             1,
                                             subplot_spec=outer[i],
                                             wspace=0.1,
                                             hspace=0.1)
    rnd_number = randint(0, len(pred_images))
    pred_image = np.array([pred_images[rnd_number]])
    pred_class = get_classlabel(model.predict_classes(pred_image)[0])
    # pred_class = np.argmax(model.predict(pred_image), axis=-1)
    pred_prob = model.predict(pred_image).reshape(6)
    for j in range(2):
        if (j % 2) == 0:
            ax = plt.Subplot(fig, inner[j])
            ax.imshow(pred_image[0])
            ax.set_title(pred_class)
            ax.set_xticks([])
            ax.set_yticks([])
            fig.add_subplot(ax)
        else:
            ax = plt.Subplot(fig, inner[j])
            ax.bar([0, 1, 2, 3, 4, 5], pred_prob)
            fig.add_subplot(ax)

fig.show()
plt.savefig(os.getcwd() + '/vgg16_prediction_result.png')
예제 #8
0
파일: paper.py 프로젝트: abonaca/gd1_spur
def param_search():
    """Visualize the process and results of the parameter search"""

    # data
    g = Table.read('../data/members.fits')

    p = np.load('/home/ana/projects/GD1-DR2/output/polytrack.npy')
    poly = np.poly1d(p)
    phi2_mask_data = np.abs(g['phi2'] - poly(g['phi1'])) < 0.5

    bx = np.linspace(-60, -20, 30)
    bc = 0.5 * (bx[1:] + bx[:-1])
    Nb = np.size(bc)
    density = False

    h_data, be = np.histogram(g['phi1'][phi2_mask_data],
                              bins=bx,
                              density=density)
    yerr_data = np.sqrt(h_data)

    # data tophat
    phi1_edges = np.array([-55, -45, -35, -25, -43, -37])
    base_mask = ((bc > phi1_edges[0]) &
                 (bc < phi1_edges[1])) | ((bc > phi1_edges[2]) &
                                          (bc < phi1_edges[3]))
    hat_mask = (bc > phi1_edges[4]) & (bc < phi1_edges[5])
    data_base = np.median(h_data[base_mask])
    data_hat = np.median(h_data[hat_mask])

    gap = np.load('../data/gap_properties.npz')
    phi1_edges = gap['phi1_edges']
    gap_position = gap['position']
    gap_width = gap['width']
    gap_yerr = gap['yerr']

    ytop_data = tophat(bc, 1, 0, gap_position, gap_width)
    ytop_data = tophat(bc, data_base, data_hat, gap_position, gap_width)

    # spur spline
    sp = np.load('../data/spur_track.npz')
    spx = sp['x']
    spy = sp['y']

    # lists with data points
    gap_data = [[bc, h_data]]
    gap_model = [[bc, ytop_data]]
    spur_data = [[g['phi1'], g['phi2']]]
    loops = []
    pfid = []

    ids = [-1, 15, 48]
    for i in ids:
        pkl = pickle.load(
            open('../data/predictions/model_{:03d}.pkl'.format(i), 'rb'))
        cg = pkl['stream']

        gap_data += [[pkl['bincen'], pkl['nbin']]]
        gap_model += [[pkl['bincen'], pkl['nexp']]]
        spur_data += [[cg.phi1.wrap_at(wangle), cg.phi2]]
        loops += [pkl['all_loop']]

        # parameters
        x = pkl['x']
        bnorm = np.sqrt(x[1]**2 + x[2]**2)
        vnorm = np.sqrt(x[3]**2 + x[4]**2)
        pfid += [[x[0], bnorm, vnorm, x[6], np.log10(x[5])]]

    colors = ['k', 'orange', 'deepskyblue', 'limegreen']
    accent_colors = ['k', 'orangered', 'navy', 'forestgreen']
    colors = ['k', '#ff6600', '#2ca02c', '#37abc8']
    accent_colors = ['k', '#aa4400', '#165016', '#164450']
    colors = ['k', '#2c89a0', '#37abc8', '#5fbcd3']
    accent_colors = ['k', '#164450', '#216778', '#2c89a0']
    sizes = [1, 4, 4, 4]
    labels = ['Data', 'Model A (fiducial)', 'Model B', 'Model C']

    plt.close()
    fig = plt.figure(figsize=(13, 6.5))

    gs0 = mpl.gridspec.GridSpec(1,
                                2,
                                left=0.07,
                                right=0.97,
                                bottom=0.15,
                                top=0.95,
                                wspace=0.25)

    gs_left = mpl.gridspec.GridSpecFromSubplotSpec(4,
                                                   2,
                                                   subplot_spec=gs0[0],
                                                   width_ratios=[2, 1],
                                                   wspace=0.4,
                                                   hspace=0.1)
    gs_right = mpl.gridspec.GridSpecFromSubplotSpec(4,
                                                    4,
                                                    subplot_spec=gs0[1],
                                                    hspace=0.1,
                                                    wspace=0.1)

    # show gap and spur profiles
    for e in range(4):
        ax = plt.Subplot(fig, gs_left[e, 1])
        ax1 = fig.add_subplot(ax)

        plt.plot(gap_data[e][0], gap_data[e][1], 'o', ms=6, color=colors[e])
        #plt.errorbar(bc, h_data, yerr=yerr_data, fmt='none', color='k', label='')
        plt.plot(gap_model[e][0],
                 gap_model[e][1],
                 '-',
                 color='k',
                 alpha=0.5,
                 lw=3)

        plt.xlim(-55, -25)
        #plt.ylim(-0.5, 1.5)
        if e < 3:
            plt.setp(plt.gca().get_xticklabels(), visible=False)
        else:
            plt.xlabel('$\phi_1$ [deg]')
        plt.ylabel('Number')

        ax = plt.Subplot(fig, gs_left[e, 0])
        ax2 = fig.add_subplot(ax)

        plt.plot(spur_data[e][0],
                 spur_data[e][1],
                 'o',
                 ms=sizes[e],
                 color=colors[e])
        if e > 0:
            ind = loops[e - 1]
            #print(ind)
            plt.plot(spur_data[e][0][ind],
                     spur_data[e][1][ind],
                     'o',
                     ms=1.2 * sizes[e],
                     color=accent_colors[e],
                     mec=accent_colors[e],
                     mew=1)

        plt.plot(spx, spy, '-', color='k', alpha=0.5, lw=3)

        plt.xlim(-55, -25)
        plt.ylim(-4, 4)
        txt = plt.text(0.07,
                       0.75,
                       labels[e],
                       transform=plt.gca().transAxes,
                       fontsize='small')
        txt.set_bbox(dict(facecolor='w', alpha=0.8, ec='none'))

        if e < 3:
            plt.setp(plt.gca().get_xticklabels(), visible=False)
        else:
            plt.xlabel('$\phi_1$ [deg]')
        plt.ylabel('$\phi_2$ [deg]')

    #########################
    # right side: corner plot

    hull = np.load('../data/hull_points_v500w200.npz')
    vertices = hull['vertices']
    pids = hull['panel']

    Nvar = 5
    params = [
        'T [Gyr]', 'b [pc]', 'V [km s$^{-1}$]', '$r_s$ [pc]', 'log M/M$_\odot$'
    ]
    lims = [[0., 2], [1, 70], [10, 500], [1, 40], [5, 8.6]]
    lims = [[0., 4.5], [0, 145], [0, 700], [0, 99], [4.5, 9]]
    ax = [[[] * 4] * 4] * 4

    symbols = ['*', 'o', 's']
    sizes = [15, 8, 7]
    labels = ['Model A (fiducial)', 'Model B', 'Model C']

    for i in range(0, Nvar - 1):
        for j in range(i + 1, Nvar):
            ax_ = plt.Subplot(fig, gs_right[j - 1, i])
            ax[j - 1][i] = fig.add_subplot(ax_)

            vert_id = (pids[:, 0] == i) & (pids[:, 1] == j)
            xy_vert = vertices[vert_id]
            p = mpl.patches.Polygon(xy_vert,
                                    closed=True,
                                    lw=2,
                                    ec='0.8',
                                    fc='0.9',
                                    zorder=0,
                                    label='Consistent with data')
            #plt.gca().add_artist(p)
            patch = plt.gca().add_patch(p)

            for e in range(3):
                plt.plot(pfid[e][i],
                         pfid[e][j],
                         symbols[e],
                         ms=sizes[e],
                         mec=accent_colors[e + 1],
                         mew=1.5,
                         color=colors[e + 1],
                         label=labels[e])

            plt.xlim(lims[i])
            plt.ylim(lims[j])

            if j - 1 < 3:
                plt.setp(plt.gca().get_xticklabels(), visible=False)
            else:
                plt.xlabel(params[i])

            if i > 0:
                plt.setp(plt.gca().get_yticklabels(), visible=False)
            else:
                plt.ylabel(params[j])

    plt.sca(ax[3][2])
    # sort legend entries
    handles, labels = plt.gca().get_legend_handles_labels()
    labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0]))
    plt.legend(handles,
               labels,
               frameon=False,
               loc=2,
               fontsize='medium',
               bbox_to_anchor=(-0.2, 4.3))
    #plt.legend(frameon=False, loc=2, fontsize='medium', bbox_to_anchor=(-0.2,4.3))

    plt.savefig('../paper/param_search.pdf')
예제 #9
0
def main(input_h5_file, output_pdf_file):
    ''' This program composes vespagrams to identify RF converted phases and their multiples
        please refer to Tian et al. GRL 2005 VOL. 32, L08301, doi:10.1029/2004GL021885 for good examples

        input  - H5 file with receiver functions
        output - PDF files to print

        Dependencies - rf and obspy packages beside other standard python packages
        The image is composed using triangulation. It gives good results but block median or mean must
        be implemented at some stage to reduce size of PDF.
    '''

    stream = rf.read_rf(input_h5_file, 'H5')
    rf_type = 'LQT-Q '
    filter_type = 'bandpass'
    freqmin = 0.03
    freqmax = 0.5

    # we use a zero-phase-shift band-pass filter using 2 corners. This is done in two runs forward and backward,
    # so we end up with 4 corners de facto.
    # Lets assume we have LQT orientation
    selected_stream = stream.select(component='Q').filter(
        filter_type,
        freqmin=freqmin,
        freqmax=freqmax,
        corners=2,
        zerophase=True).interpolate(10)

    # if none lets try ZRT
    if not selected_stream:
        selected_stream = stream.select(component='R').filter(
            filter_type,
            freqmin=freqmin,
            freqmax=freqmax,
            corners=2,
            zerophase=True).interpolate(10)
        rf_type = 'ZRT-R '
    # end if

    if not selected_stream:
        print("Tried Q and R components but neither found, quitting...")
        exit(-1)
    # end if

    station_list = []

    # here we collect station names but maybe ID is more appropriate in case of having the same station names
    # in different deployments

    for tr in selected_stream:
        station_list.append(tr.stats.station)
        net = tr.stats.network
    # end for

    pdf = PdfPages(output_pdf_file)
    case_description = rf_type + filter_type + ' ' + str(freqmin) + '-' + str(
        freqmax) + ' Hz'
    pdf.attach_note(case_description)
    d = pdf.infodict()
    d['Title'] = rf_type + 'RF vespagrams of ' + net + ' network'
    d['Keywords'] = case_description

    station_list = np.unique(np.array(station_list))
    print("Gathered ", len(station_list), " stations")

    # Define layout of the page outer_grid
    columns = 3
    rows = 2
    frame = 0
    figure = 1

    # ------------------------------------------
    # Main loop here over all stations
    for i, station in enumerate(station_list):
        if frame == 0:
            printed = False
            fig = plt.figure(figsize=(11.69, 8.27))
            outer_grid = gridspec.GridSpec(columns,
                                           rows,
                                           wspace=0.2,
                                           hspace=0.2)
        # end if

        print("Station ", station, i + 1, " of ", station_list.shape[0])
        traces = selected_stream.select(station=station)
        print('Contains: ', len(traces), ' events')

        # we choose short RF to simplify and speed up the processing
        # from -5 to 20 seconds and slowness range from 5 to 9 s/deg
        # its enough to see multiples and possible LAB conversion at ~19 sec (~160km)

        traces = traces.trim2(-5, 20, 'onset')
        moved = []
        slow = []

        for tr in traces:
            tr.normalize()

            # This 'if' block is designed to check correct data placement on vespagram to
            # trace the logic (debugging purposes)
            DEBUG_PLACEMENT = False
            if DEBUG_PLACEMENT and (tr.stats.slowness >
                                    6.) and (tr.stats.slowness < 7.):
                print('altered')
                data = tr.data.copy()
                print(data.shape, tr.stats.delta)
                # 500 below corresponds to 0 with sampling rate of 100Hz
                # TODO: !Change these array indices to be computed, not magic numbers!
                data[500:800] = 1.
                moved.append(data)
            else:
                moved.append(tr.data.copy() / np.max(np.abs(tr.data)))
                slow.append(tr.stats.slowness)
            # end if
        # end for

        print("Slowness min and max: ", np.min(slow), np.max(slow))
        slow.append(np.min(slow) - 0.1)
        moved.append(np.zeros(traces[0].data.shape))
        slow.append(np.max(slow) + 0.1)
        moved.append(np.zeros(traces[0].data.shape))

        slow = np.array(slow)
        idx = np.argsort(slow)
        moved = np.nan_to_num(np.array(moved))
        # moved = np.array(moved)
        slow = slow[idx]
        moved = moved[idx, :]
        z = moved.copy()

        # Some simple stacking to reduce data size on the image, this block can be safely commented out
        idx = []
        idx.append(True)  # first row with zeroes
        slo_cum = 0.
        elements = 1
        for j in xrange(1, slow.shape[0] - 2):
            if np.abs(slow[j + 1] - slow[j]) < 0.1 and slo_cum < 0.2:
                slow[j + 1] = (slow[j] + slow[j + 1]) / 2.
                moved[j, :] = moved[j, :] * elements
                moved[j + 1, :] = np.sum(moved[j:j + 2, :],
                                         axis=0) / (elements + 1)
                elements = elements + 1
                idx.append(False)
                slo_cum = slo_cum + np.abs(slow[j + 1] - slow[j])
            else:
                idx.append(True)
                slo_cum = 0
                elements = 1
            # end if
        # end for

        idx.append(True)  # before last
        idx.append(True)  # last row with zeroes
        idx = np.array(idx)
        print(idx.shape, slow.shape, moved.shape)

        slow = slow[idx]
        moved = moved[idx, :]
        z = moved.copy()
        # ------------------------------ end of stacking ------------------------

        # print('minmax',np.min(z),np.max(z))
        x = np.array(list(range(moved.shape[1]))) * traces[0].stats.delta - 5.
        x = np.repeat([x], moved.shape[0], axis=0)
        y = np.ones((moved.shape[0], moved.shape[1]))

        phase_Ps = []
        phase_Pms = []
        phase_PpPmS = []
        phase_PpSmS = []
        phase_slow = []
        # basin part
        phase_Pbs = []
        phase_PpPbs = []

        for j in xrange(slow.shape[0]):
            y[j, :] = y[j, :] * slow[j]
            phase_Ps.append(
                simple_model.calculate_delay_times(slow[j], phase='PS'))
            phase_Pms.append(
                simple_model.calculate_delay_times(slow[j], phase='PmS'))
            phase_PpPmS.append(
                simple_model.calculate_delay_times(slow[j], phase='PpPmS'))
            phase_PpSmS.append(
                simple_model.calculate_delay_times(slow[j], phase='PpSmS'))
            phase_slow.append(np.ones(phase_Ps[-1].shape[0]) * slow[j])

            # basin, we will use reflection at the top layer only
            if zb.size > 0:
                phase_Pbs.append(
                    basin_model.calculate_delay_times(slow[j], phase='PS'))
                phase_PpPbs.append(
                    basin_model.calculate_delay_times(slow[j], phase='PpPmS'))
            # end if
        # end for

        xi = np.linspace(-5, 20, 200)
        yi = np.linspace(0, 9, 400)

        # Gridding the data using triangulation. standard gridding doesn't work well here
        triang = tri.Triangulation(x.flatten(), y.flatten())
        interpolator = tri.LinearTriInterpolator(triang, z.flatten())
        xi, yi = np.meshgrid(xi, yi)
        zi = interpolator(xi, yi)

        # Define two plots as inner_grid to place them inside one cell of outer_grid
        inner_grid = gridspec.GridSpecFromSubplotSpec(
            2, 1, subplot_spec=outer_grid[frame], wspace=0.5, hspace=0.)
        ax1 = plt.Subplot(fig, inner_grid[0])
        ax2 = plt.Subplot(fig, inner_grid[1], sharex=ax1)
        lim = np.max(np.abs(zi[zi < 0]) * 0.5)
        levels = np.linspace(-lim, lim, 15)
        # print("Levels ",-lim,lim)
        cmap = plt.cm.jet
        cs = ax1.contourf(xi, yi, zi, levels=levels, extend='both', cmap=cmap)
        cs.cmap.set_under('k')
        cs.cmap.set_over('k')
        ax1.set_ylim(5, 9)
        ax1.set_xlim(-5, 20)
        ax1.plot(phase_Ps, slow,
                 color='black')  # direct conversion, positive amplitude
        ax1.plot(phase_PpPmS, slow,
                 color='crimson')  # multiples,         positive amplitude
        ax1.plot(phase_PpSmS, slow,
                 color='purple')  # multiples,         negative amplitude

        ax1.annotate('Pms',
                     xy=(phase_Ps[-1][0], 9.1),
                     xycoords='data',
                     ha='center',
                     va='bottom',
                     rotation=0.,
                     annotation_clip=False,
                     fontsize=7)
        ax1.annotate('Ps LAB',
                     xy=(phase_Ps[-1][-1], 9.1),
                     xycoords='data',
                     ha='center',
                     va='bottom',
                     rotation=0.,
                     annotation_clip=False,
                     fontsize=7)

        if phase_Pbs:
            ax1.annotate('Pbs',
                         xy=(phase_Pbs[-1][0], 9.1),
                         xycoords='data',
                         ha='center',
                         va='bottom',
                         rotation=0.,
                         annotation_clip=False,
                         fontsize=7)
            ax1.plot(phase_Pbs, slow, color='black')
            ax1.plot(phase_PpPbs, slow, color='crimson')
        # end if

        ax1.spines['bottom'].set_visible(False)
        ax1.tick_params(labelbottom='off')
        ax1.spines['bottom'].set_visible(False)
        ax1.yaxis.tick_right()
        ax1.yaxis.set_label_position("right")
        xlabels = ax1.get_xticklabels()
        ylabels = ax1.get_yticklabels()
        for label in xlabels:
            label.set_rotation(90)
            label.set_fontsize(7)
        # end for
        for label in ylabels:
            label.set_rotation(90)
            label.set_fontsize(7)
        # end for

        ax1.annotate(station,
                     xy=(-0.08, 0),
                     ha='left',
                     va='center',
                     xycoords='axes fraction',
                     textcoords='offset points',
                     rotation=90.)

        start, end = ax1.get_ylim()
        ax1.yaxis.set_ticks(np.arange(start + 1, end + 1, 1))

        cs = ax2.contourf(xi,
                          -1. * yi,
                          zi,
                          levels=levels,
                          extend='both',
                          cmap=cmap)
        cs.cmap.set_under('k')
        cs.cmap.set_over('k')
        ax2.spines['top'].set_visible(False)
        ax2.set_ylim(-9, -5)
        ax2.set_xlim(-5, 20)
        ax2.yaxis.tick_right()
        ax2.yaxis.set_label_position("right")
        ylabels = ax2.get_yticklabels()
        ax2.plot(phase_Ps, -slow, color='black')
        ax2.plot(phase_PpPmS, -slow, color='crimson')
        ax2.plot(phase_PpSmS, -slow, color='purple')

        ax2.annotate('+PpPms',
                     xy=(phase_PpPmS[-1][0], -9.1),
                     xycoords='data',
                     ha='center',
                     va='top',
                     rotation=0.,
                     annotation_clip=False,
                     fontsize=7,
                     color='crimson')
        ax2.annotate('-PpSms',
                     xy=(phase_PpSmS[-1][0], -9.1),
                     xycoords='data',
                     ha='center',
                     va='top',
                     rotation=0.,
                     annotation_clip=False,
                     fontsize=7,
                     color='purple')

        if phase_PpPbs:
            ax2.annotate('+PpPbs',
                         xy=(phase_PpPbs[-1][0], -9.1),
                         xycoords='data',
                         ha='center',
                         va='top',
                         rotation=0.,
                         annotation_clip=False,
                         fontsize=7,
                         color='crimson')

            ax2.plot(phase_PpPbs, -slow, color='crimson')
            ax2.plot(phase_Pbs, -slow, color='black')
        # end if

        for label in ylabels:
            label.set_rotation(90)
            label.set_fontsize(7)
        # end for

        if frame > 3:
            xlabels = ax2.get_xticklabels()
            for label in xlabels:
                label.set_rotation(90)
                label.set_fontsize(7)
            ax2.set_xlabel('Time (sec.)')
        else:
            ax2.set_xticklabels([])
        # end if

        if (frame % 2) != 0:
            ax2.annotate('Slowness s/deg',
                         xy=(1.2, 1),
                         ha='left',
                         va='center',
                         xycoords='axes fraction',
                         textcoords='offset points',
                         rotation=90.)
        # end if

        start, end = ax2.get_ylim()
        ax2.yaxis.set_ticks(np.arange(start, end, 1))
        traces.moveout()
        x = np.array(list(range(
            traces[0].data.shape[0]))) * traces[0].stats.delta - 5.
        y = traces.stack()

        # Some amplitude scaling to have nice plot
        y = y[0].data / 1.5 - 5.
        ax2.plot(x, y, clip_on=False, linewidth=3, color='white')
        ax2.plot(x, y, clip_on=False, linewidth=1)

        fig.add_subplot(ax1)
        fig.add_subplot(ax2)

        frame = frame + 1
        print('frame', frame)
        if frame >= rows * columns:
            cb_ax = fig.add_axes([0.25, 0.98, 0.5, 0.02])
            labels = fig.colorbar(cs,
                                  cax=cb_ax,
                                  ticks=[np.min(zi), 0,
                                         np.max(zi)],
                                  orientation='horizontal',
                                  extend='neither',
                                  extendfrac=0.00001,
                                  extendrect=True,
                                  drawedges=False)
            # labels.set_ticks([np.min(zi), 0, np.max(zi)])
            # labels.set_ticklabels(['-', '0', '+'])
            cb_ax.set_xticks([np.min(zi), 0, np.max(zi)])
            cb_ax.set_xticklabels(['-', '0', '+'])
            # labels.ax.set_yticklabels(['-', '0', '+'])
            pdf.savefig()
            figure += 1
            frame = 0
            printed = True
            # plt.show()
            plt.close()
        # end if

    # end for

    if not printed:
        cb_ax = fig.add_axes([0.25, 0.95, 0.5, 0.02])
        labels = fig.colorbar(cs,
                              cax=cb_ax,
                              ticks=[-1, 0, 1],
                              orientation='horizontal',
                              extend='neither',
                              extendfrac=0.00001,
                              extendrect=True,
                              drawedges=False)
        labels.ax.set_xticklabels(['-', '0', '+', ''])
        pdf.savefig()
        plt.close()
    # end if

    pdf.close()
예제 #10
0
    def pixel_by_pixel(self,
                       colrange=None,
                       rowrange=None,
                       flux_type="corrected",
                       mask=None):
        """
        Creates a pixel-by-pixel light curve using the corrected flux.
        
        Parameters
        ---------- 
        colrange : np.array, optional
             A list of start column and end column you're interested in 
             zooming in on.
        rowrange : np.array, optional
             A list of start row and end row you're interested in zooming
             in on.
        flux_type : str, optional
             The type of flux used. Either: 'raw' or 'corrected'. If not,
             default set to 'corrected'.
        mask : np.array, optional
             Specifies the cadences used in the light curve. If not, default
             set to good quality cadences.
        """
        if colrange is None:
            colrange = [0, self.dimensions[0]]

        if rowrange is None:
            rowrange = [0, self.dimensions[1]]

        nrows = int(np.round(colrange[1] - colrange[0]))
        ncols = int(np.round(rowrange[1] - rowrange[0]))

        if (colrange[1] > self.dimensions[1]) or (rowrange[1] >
                                                  self.dimensions[0]):
            raise ValueError(
                "Asking for more pixels than available in the TPF.")

        figure = plt.figure(figsize=(20, 8))
        outer = gridspec.GridSpec(1, 2, width_ratios=[1, 4])

        inner = gridspec.GridSpecFromSubplotSpec(nrows,
                                                 ncols,
                                                 hspace=0.1,
                                                 wspace=0.1,
                                                 subplot_spec=outer[1])

        i, j = rowrange[0], colrange[0]

        if mask is None:
            q = self.obj.quality == 0
        else:
            q = mask == 0

        for ind in range(int(nrows * ncols)):
            ax = plt.Subplot(figure, inner[ind])

            flux = self.flux[:, i, j]

            if flux_type.lower() == 'corrected':
                flux = self.obj.corrected_flux(flux=flux)

            flux = flux[q] / np.nanmedian(flux[q])
            ax.plot(self.obj.time[q], flux, 'k')

            j += 1
            if j == colrange[1]:
                i += 1
                j = colrange[0]

            ax.set_ylim(np.percentile(flux, 1), np.percentile(flux, 99))

            ax.set_xlim(
                np.min(self.obj.time[q]) - 0.1,
                np.max(self.obj.time[q]) + 0.1)

            ax.set_xticks([])
            ax.set_yticks([])

            figure.add_subplot(ax)

        ax = plt.subplot(outer[0])
        c = ax.imshow(self.flux[0, colrange[0]:colrange[1],
                                rowrange[0]:rowrange[1]],
                      vmax=np.percentile(self.flux[0], 95))
        divider = make_axes_locatable(ax)
        cax = divider.append_axes('right', size='5%', pad=0.15)
        plt.colorbar(c, cax=cax, orientation='vertical')

        figure.show()
    # First do figure 1
    colorVal = pi_scalarMap.to_rgba(numpy.log10(pi))

    pylab.figure(1)
    pylab.semilogy(bin_locations + normal(0, 1) *
                   (bin_locations[1] - bin_locations[0]) * 0.1,
                   sfs,
                   '.-',
                   alpha=0.5,
                   color=colorVal)

    # Then do figure 2
    pylab.figure(2)

    axis = plt.Subplot(fig, outer_grid[i])
    fig.add_subplot(axis)
    #axis.set_xlim([0,0.5])
    #axis.set_xticks(numpy.arange(0,11)*0.05)

    axis.set_xticks(numpy.arange(0, 21) * 0.05)
    axis.set_xlim([0.05, 0.95])

    axis.set_xticklabels([])
    #axis.set_ylim([0,counts.max()+1])

    bin_idxs = (bin_locations > 0.05) * (bin_locations < 0.95)

    axis.plot(bin_locations[bin_idxs],
              counts[bin_idxs],
              label=('%d: pi=%g, D=%g' % (i, pi, D)))
# gridspec inside gridspec
outer_grid = gridspec.GridSpec(3, 3, wspace=0.05, hspace=0.05)

for i in range(9):
    dist, hieroglyph = which_hiero(test_hiero[i + 5], dico_hiero)
    print("True Hieroglyph : ", labels_true[ntrain + i + 5], "// Predicted : ",
          hieroglyph, "dist : ", dist)

    inner_grid = gridspec.GridSpecFromSubplotSpec(1,
                                                  2,
                                                  subplot_spec=outer_grid[i],
                                                  wspace=0.0,
                                                  hspace=0.0)

    ax = plt.Subplot(fig, inner_grid[0])
    ax.imshow(test_data[i + 5].reshape(img_height, img_width), cmap='gray')
    ax.set_xticks([])
    ax.set_yticks([])
    fig.add_subplot(ax)
    ax = plt.Subplot(fig, inner_grid[1])
    index = dico_hiero[hieroglyph][1][1]
    ax.imshow(train_data[0][index].reshape(img_height, img_width), cmap='gray')
    ax.set_xticks([])
    ax.set_yticks([])
    ax.text(-32,
            -8,
            'Dissimilarity : {:.2f}'.format(dist),
            style='italic',
            bbox={
                'facecolor': 'white',
예제 #13
0
        del atoms[atoms.positions[:, 0] >= centreofmass[0] + 8.10]
        del atoms[atoms.positions[:, 0] <= centreofmass[0] - 8.10]
        del atoms[atoms.positions[:, 1] >= centreofmass[1] + 7.8]
        del atoms[atoms.positions[:, 1] <= centreofmass[1] - 7.10]
    # left side cover
    if (j == 6):
        del atoms[atoms.positions[:, 0] >= centreofmass[0] + 5.10]
        del atoms[atoms.positions[:, 0] <= centreofmass[0] - 11.10]
        del atoms[atoms.positions[:, 1] >= centreofmass[1] + 7.8]
        del atoms[atoms.positions[:, 1] <= centreofmass[1] - 7.10]

    colorlenth = len(atoms)
    #view(atoms)
    cell = atoms.get_cell()
    # 0 0
    ax = plt.Subplot(fig, inner[0])
    img = atoms.copy()
    plot_conf(ax, img, colorlenth)

    ax.set_xlim([centreofmass[0] - 7.50, centreofmass[0] + 7.50])
    if (j == 6):
        ax.set_xlim([centreofmass[0] - 10.50, centreofmass[0] + 4.50])
    ax.set_ylim([10.7, 20.0])
    ax.set_yticks([])
    ax.set_xticks([])
    ax.set(aspect=1)
    fig.add_subplot(ax)
    #    if (j ==0):
    #       name2 ='Pt$_7$O$_1$ Lowest Isomers'
    #       ax.set_title(name2)
예제 #14
0
def pixel_by_pixel(box, colrange=None, rowrange=None, cmap='viridis', mask=None,
                    xlim=None,ylim=None, color_by_pixel=True, 
                    freq_range=[1/20., 1/0.1]):
    
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    """
    Creates a pixel-by-pixel light curve using the corrected flux.
    Contribution from Oliver Hall.
    Parameters
    ----------
    colrange : np.array, optional
         A list of start column and end column you're interested in
         zooming in on.
    rowrange : np.array, optional
         A list of start row and end row you're interested in zooming
         in on.
    cmap : str, optional
         Name of a matplotlib colormap. Default is 'viridis'.
    #data_type : str, optional
         The type of flux used. Either: 'raw', 'corrected', 'amplitude',
         or 'periodogram'. If not, default set to 'corrected'.
    mask : np.array, optional
         Specifies the cadences used in the light curve. If not, default
         set to good quality cadences.
    xlim : np.array, optional
         Specifies the xlim on the subplots. If not, default is set to 
         the entire light curve.
    ylim : np.array, optional
         Specifies the ylim on the subplots, If not, default is set to 
         the entire light curve flux range.
    color_by_pixel : bool, optional
         Colors the light curve given the color of the pixel. If not,
         default is set to True.
    #freq_range : list, optional
         List of minimum and maximum frequency to search in Lomb Scargle
         periodogram. Only used if data_type = 'periodogram'. If None,
         default = [1/20., 1/0.1].
    """
    if colrange is None:
        colrange = [0, np.shape(box)[-1]]
    
    if rowrange is None:
        rowrange = [0, np.shape(box)[-2]]
        
    nrows = int(np.round(colrange[1]-colrange[0]))
    ncols = int(np.round(rowrange[1]-rowrange[0]))
    
      
    fig = plt.figure(figsize=(20,8))
    outer = gridspec.GridSpec(1,2, width_ratios=[1,4])
    
    inner = gridspec.GridSpecFromSubplotSpec(ncols, nrows, hspace=0.1, wspace=0.1,
                                             subplot_spec=outer[1])
    
    i, j = rowrange[0], colrange[0]
    
    #    if mask is None:
    #        q = self.obj.quality == 0
    #    else:
    #        q = mask == 0
    
    
    ## PLOTS TARGET PIXEL FILE ##
    ax = plt.subplot(outer[0])
    
    c = ax.imshow(np.nanmedian(box[:,rowrange[0]:rowrange[1],colrange[0]:colrange[1]],0),cmap=cmap)
    divider = make_axes_locatable(ax)
    cax = divider.append_axes('right', size='5%', pad=0.15)
    plt.colorbar(c, cax=cax, orientation='vertical')
    
    ## PLOTS PIXEL LIGHT CURVES ##
    for ind in range( int(nrows * ncols) ):
        print(ind)
        ax = plt.Subplot(fig, inner[ind])
    
        flux = box[:,i,j]
        y = flux
        x = np.arange(len(y))
    #        time = self.obj.time
    #        corr_flux = self.obj.corrected_flux(flux=flux)
    
    #        if data_type.lower() == 'corrected':
    #            y = corr_flux[q]/np.nanmedian(corr_flux[q])
    #            x = time[q]
    
    #        elif data_type.lower() == 'amplitude':
    #            lc = lk.LightCurve(time=time, flux=corr_flux)
    #            pg = lc.normalize().to_periodogram()
    #            x = pg.frequency.value
    #            y = pg.power.value
    #    
    #        elif data_type.lower() == 'raw':
    #            y = flux[q]/np.nanmedian(flux[q])
    #            x = time[q]
    #        
    #        elif data_type.lower() == 'periodogram':
    #            freq, power = LombScargle(time, corr_flux).autopower(minimum_frequency=freq_range[0],
    #                                                                 maximum_frequency=freq_range[1],
    #                                                                 method='fast')
    #            y = power
    #            x = 1/freq
    
        if color_by_pixel is False:
            color = 'k'
        else:
            rgb = c.cmap(c.norm(np.nanmedian(box,0)[i,j]))
            color = mpl.colors.rgb2hex(rgb)
    
        ax.plot(x, y, c=color)
    
        j += 1
        if j == colrange[1]:
            i += 1
            j  = colrange[0]
    
        if ylim is None:
            ax.set_ylim(np.percentile(y, 1), np.percentile(y, 99))
        else:
            ax.set_ylim(ylim[0], ylim[1])
    
        if xlim is None:
            ax.set_xlim(np.min(x)-0.1, np.max(x)+0.1)
        else:
            ax.set_xlim(xlim[0], xlim[1])
    
#        if data_type.lower() == 'amplitude':
#            ax.set_yscale('log')
#            ax.set_xscale('log')
#            ax.set_ylim(y.min(), y.max())
#            ax.set_xlim(np.min(x),
#                        np.max(x))
    
        ax.set_xticks([])
        ax.set_yticks([])
    
        fig.add_subplot(ax)
    plt.savefig('pixbypix_X%iby%i_Y%iby%i.png'%(rowrange[0],rowrange[1],colrange[0],colrange[1]),dpi=400,bbox_inches='tight')
    return fig
예제 #15
0
def contrastplot_test(data,
                      x,
                      y,
                      idx=None,
                      alpha=0.75,
                      axis_title_size=None,
                      barWidth=5,
                      contrastShareY=True,
                      contrastEffectSizeLineStyle='solid',
                      contrastEffectSizeLineColor='black',
                      contrastYlim=None,
                      contrastZeroLineStyle='solid',
                      contrastZeroLineColor='black',
                      effectSizeYLabel="Effect Size",
                      figsize=None,
                      floatContrast=True,
                      floatSwarmSpacer=0.2,
                      heightRatio=(1, 1),
                      idcol=None,
                      lineWidth=2,
                      legend=True,
                      legendFontSize=14,
                      legendFontProps={},
                      paired=False,
                      pal=None,
                      rawMarkerSize=8,
                      rawMarkerType='o',
                      reps=3000,
                      showGroupCount=True,
                      show95CI=False,
                      showAllYAxes=False,
                      showRawData=True,
                      smoothboot=False,
                      statfunction=None,
                      summaryBar=False,
                      summaryBarColor='grey',
                      summaryBarAlpha=0.25,
                      summaryColour='black',
                      summaryLine=True,
                      summaryLineStyle='solid',
                      summaryLineWidth=0.25,
                      summaryMarkerSize=10,
                      summaryMarkerType='o',
                      swarmShareY=True,
                      swarmYlim=None,
                      tickAngle=45,
                      tickAlignment='right',
                      violinOffset=0.375,
                      violinWidth=0.2,
                      violinColor='k',
                      xticksize=None,
                      yticksize=None,
                      **kwargs):
    '''Takes a pandas dataframe and produces a contrast plot:
    either a Cummings hub-and-spoke plot or a Gardner-Altman contrast plot.
    -----------------------------------------------------------------------
    Description of flags upcoming.'''

    # Check that `data` is a pandas dataframe
    if 'DataFrame' not in str(type(data)):
        raise TypeError(
            "The object passed to the command is not not a pandas DataFrame.\
         Please convert it to a pandas DataFrame.")

    # Get and set levels of data[x]
    if idx is None:
        widthratio = [1]
        allgrps = np.sort(data[x].unique())
        if paired:
            # If `idx` is not specified, just take the FIRST TWO levels alphabetically.
            tuple_in = tuple(allgrps[0:2], )
        else:
            # No idx is given, so all groups are compared to the first one in the DataFrame column.
            tuple_in = (tuple(allgrps), )
            if len(allgrps) > 2:
                floatContrast = False

    else:
        if all(isinstance(element, str) for element in idx):
            # if idx is supplied but not a multiplot (ie single list or tuple)
            tuple_in = (idx, )
            widthratio = [1]
            if len(idx) > 2:
                floatContrast = False
        elif all(isinstance(element, tuple) for element in idx):
            # if idx is supplied, and it is a list/tuple of tuples or lists, we have a multiplot!
            tuple_in = idx
            if (any(len(element) > 2 for element in tuple_in)):
                # if any of the tuples in idx has more than 2 groups, we turn set floatContrast as False.
                floatContrast = False
            # Make sure the widthratio of the seperate multiplot corresponds to how
            # many groups there are in each one.
            widthratio = []
            for i in tuple_in:
                widthratio.append(len(i))
        else:
            raise TypeError(
                "The object passed to `idx` consists of a mixture of single strings and tuples. \
                Please make sure that `idx` is either a tuple of column names, or a tuple of tuples for plotting."
            )

    # initialise statfunction
    if statfunction == None:
        statfunction = np.mean

    # Create list to collect all the contrast DataFrames generated.
    contrastList = list()
    contrastListNames = list()
    # # Calculate the bootstraps according to idx.
    # for ix, current_tuple in enumerate(tuple_in):
    #     bscontrast=list()
    #     for i in range (1, len(current_tuple)):
    #     # Note that you start from one. No need to do auto-contrast!
    #         tempbs=bootstrap_contrast(
    #             data=data,
    #             x=x,
    #             y=y,
    #             idx=[current_tuple[0], current_tuple[i]],
    #             statfunction=statfunction,
    #             smoothboot=smoothboot,
    #             reps=reps)
    #         bscontrast.append(tempbs)
    #         contrastList.append(tempbs)
    #         contrastListNames.append(current_tuple[i]+' vs. '+current_tuple[0])

    # Setting color palette for plotting.
    if pal is None:
        if 'hue' in kwargs:
            colorCol = kwargs['hue']
            colGrps = data[colorCol].unique()
            nColors = len(colGrps)
        else:
            colorCol = x
            colGrps = data[x].unique()
            nColors = len([element for tupl in tuple_in for element in tupl])
        plotPal = dict(zip(colGrps, sns.color_palette(n_colors=nColors)))
    else:
        plotPal = pal

    # Ensure summaryLine and summaryBar are not displayed together.
    if summaryLine is True and summaryBar is True:
        summaryBar = True
        summaryLine = False
    # Turn off summary line if floatContrast is true
    if floatContrast:
        summaryLine = False

    if swarmYlim is None:
        # get range of _selected groups_.
        u = list()
        for t in idx:
            for i in np.unique(t):
                u.append(i)
        u = np.unique(u)
        tempdat = data[data[x].isin(u)]
        swarm_ylim = np.array([np.min(tempdat[y]), np.max(tempdat[y])])
    else:
        swarm_ylim = np.array([swarmYlim[0], swarmYlim[1]])

    if contrastYlim is not None:
        contrastYlim = np.array([contrastYlim[0], contrastYlim[1]])

    barWidth = barWidth / 1000  # Not sure why have to reduce the barwidth by this much!
    if showRawData is True:
        maxSwarmSpan = 0.25
    else:
        maxSwarmSpan = barWidth

    # Expand the ylim in both directions.
    ## Find half of the range of swarm_ylim.
    swarmrange = swarm_ylim[1] - swarm_ylim[0]
    pad = 0.1 * swarmrange
    x2 = np.array([swarm_ylim[0] - pad, swarm_ylim[1] + pad])
    swarm_ylim = x2

    # plot params
    if axis_title_size is None:
        axis_title_size = 25
    if yticksize is None:
        yticksize = 18
    if xticksize is None:
        xticksize = 18

    # Set clean style
    sns.set(style='ticks')

    axisTitleParams = {'labelsize': axis_title_size}
    xtickParams = {'labelsize': xticksize}
    ytickParams = {'labelsize': yticksize}
    svgParams = {'fonttype': 'none'}

    rc('axes', **axisTitleParams)
    rc('xtick', **xtickParams)
    rc('ytick', **ytickParams)
    rc('svg', **svgParams)

    if figsize is None:
        if len(tuple_in) > 2:
            figsize = (12, (12 / np.sqrt(2)))
        else:
            figsize = (8, (8 / np.sqrt(2)))

    # Initialise figure, taking into account desired figsize.
    fig = plt.figure(figsize=figsize)

    # Initialise GridSpec based on `tuple_in` shape.
    gsMain = gridspec.GridSpec(
        1,
        np.shape(tuple_in)[0],
        # 1 row; columns based on number of tuples in tuple.
        width_ratios=widthratio,
        wspace=0)

    for gsIdx, current_tuple in enumerate(tuple_in):
        #### FOR EACH TUPLE IN IDX
        plotdat = data[data[x].isin(current_tuple)]
        plotdat[x] = plotdat[x].astype("category")
        plotdat[x].cat.set_categories(current_tuple,
                                      ordered=True,
                                      inplace=True)
        plotdat.sort_values(by=[x])
        # Drop all nans.
        plotdat = plotdat.dropna()

        # Calculate summaries.
        summaries = plotdat.groupby([x], sort=True)[y].apply(statfunction)

        if floatContrast is True:
            # Use fig.add_subplot instead of plt.Subplot
            ax_raw = fig.add_subplot(gsMain[gsIdx], frame_on=False)
            ax_contrast = ax_raw.twinx()
        else:
            # Create subGridSpec with 2 rows and 1 column.
            subGridSpec = gridspec.GridSpecFromSubplotSpec(
                2, 1, subplot_spec=gsMain[gsIdx], wspace=0)
            # Use plt.Subplot instead of fig.add_subplot
            ax_raw = plt.Subplot(fig, subGridSpec[0, 0], frame_on=False)
            ax_contrast = plt.Subplot(fig,
                                      subGridSpec[1, 0],
                                      sharex=ax_raw,
                                      frame_on=False)
        # Calculate the boostrapped contrast
        bscontrast = list()
        for i in range(1, len(current_tuple)):
            # Note that you start from one. No need to do auto-contrast!
            tempbs = bootstrap_contrast(
                data=data,
                x=x,
                y=y,
                idx=[current_tuple[0], current_tuple[i]],
                statfunction=statfunction,
                smoothboot=smoothboot,
                reps=reps)
            bscontrast.append(tempbs)
            contrastList.append(tempbs)
            contrastListNames.append(current_tuple[i] + ' vs. ' +
                                     current_tuple[0])

        #### PLOT RAW DATA.
        if showRawData is True:
            # Seaborn swarmplot doc says to set custom ylims first.
            ax_raw.set_ylim(swarm_ylim)
            sw = sns.swarmplot(data=plotdat,
                               x=x,
                               y=y,
                               order=current_tuple,
                               ax=ax_raw,
                               alpha=alpha,
                               palette=plotPal,
                               size=rawMarkerSize,
                               marker=rawMarkerType,
                               **kwargs)

        if summaryBar is True:
            bar_raw = sns.barplot(x=summaries.index.tolist(),
                                  y=summaries.values,
                                  facecolor=summaryBarColor,
                                  ax=ax_raw,
                                  alpha=summaryBarAlpha)

        if floatContrast:
            # Get horizontal offset values.
            maxXBefore = max(sw.collections[0].get_offsets().T[0])
            minXAfter = min(sw.collections[1].get_offsets().T[0])
            xposAfter = maxXBefore + floatSwarmSpacer
            xAfterShift = minXAfter - xposAfter
            # shift the swarmplots
            offsetSwarmX(sw.collections[1], -xAfterShift)

            ## get swarm with largest span, set as max width of each barplot.
            for i, bar in enumerate(bar_raw.patches):
                x_width = bar.get_x()
                width = bar.get_width()
                centre = x_width + (width / 2.)
                if i == 0:
                    bar.set_x(centre - maxSwarmSpan / 2.)
                else:
                    bar.set_x(centre - xAfterShift - maxSwarmSpan / 2.)
                bar.set_width(maxSwarmSpan)

            ## Set the ticks locations for ax_raw.
            ax_raw.xaxis.set_ticks((0, xposAfter))
            firstTick = ax_raw.xaxis.get_ticklabels()[0].get_text()
            secondTick = ax_raw.xaxis.get_ticklabels()[1].get_text()
            ax_raw.set_xticklabels(
                [
                    firstTick,  #+' n='+count[firstTick],
                    secondTick
                ],  #+' n='+count[secondTick]],
                rotation=tickAngle,
                horizontalalignment=tickAlignment)

        if summaryLine is True:
            for i, m in enumerate(summaries):
                ax_raw.plot(
                    (i - summaryLineWidth,
                     i + summaryLineWidth),  # x-coordinates
                    (m, m),
                    color=summaryColour,
                    linestyle=summaryLineStyle)

        if show95CI is True:
            sns.barplot(data=plotdat, x=x, y=y, ax=ax_raw, alpha=0, ci=95)

        ax_raw.set_xlabel("")
        if floatContrast is False:
            fig.add_subplot(ax_raw)

        #### PLOT CONTRAST DATA.
        if len(current_tuple) == 2:
            # Plot the CIs on the contrast axes.
            plotbootstrap(sw.collections[1],
                          bslist=tempbs,
                          ax=ax_contrast,
                          violinWidth=violinWidth,
                          violinOffset=violinOffset,
                          markersize=summaryMarkerSize,
                          marker=summaryMarkerType,
                          offset=floatContrast,
                          color=violinColor,
                          linewidth=1)
            if floatContrast:
                # Set reference lines
                ## First get leftmost limit of left reference group
                xtemp, _ = np.array(sw.collections[0].get_offsets()).T
                leftxlim = xtemp.min()
                ## Then get leftmost limit of right test group
                xtemp, _ = np.array(sw.collections[1].get_offsets()).T
                rightxlim = xtemp.min()

                ## zero line
                ax_contrast.hlines(
                    0,  # y-coordinates
                    leftxlim,
                    3.5,  # x-coordinates, start and end.
                    linestyle=contrastZeroLineStyle,
                    linewidth=0.75,
                    color=contrastZeroLineColor)

                ## effect size line
                ax_contrast.hlines(
                    tempbs['summary'],
                    rightxlim,
                    3.5,  # x-coordinates, start and end.
                    linestyle=contrastEffectSizeLineStyle,
                    linewidth=0.75,
                    color=contrastEffectSizeLineColor)

                ## If the effect size is positive, shift the right axis up.
                if float(tempbs['summary']) > 0:
                    rightmin = ax_raw.get_ylim()[0] - float(tempbs['summary'])
                    rightmax = ax_raw.get_ylim()[1] - float(tempbs['summary'])
                ## If the effect size is negative, shift the right axis down.
                elif float(tempbs['summary']) < 0:
                    rightmin = ax_raw.get_ylim()[0] + float(tempbs['summary'])
                    rightmax = ax_raw.get_ylim()[1] + float(tempbs['summary'])

                ax_contrast.set_ylim(rightmin, rightmax)

                if gsIdx > 0:
                    ax_contrast.set_ylabel('')

                align_yaxis(ax_raw, tempbs['statistic_ref'], ax_contrast, 0.)

            else:
                # Set bottom axes ybounds
                if contrastYlim is not None:
                    ax_contrast.set_ylim(contrastYlim)

                # Set xlims so everything is properly visible!
                swarm_xbounds = ax_raw.get_xbound()
                ax_contrast.set_xbound(
                    swarm_xbounds[0] - (summaryLineWidth * 1.1),
                    swarm_xbounds[1] + (summaryLineWidth * 1.1))

        else:
            # Plot the CIs on the bottom axes.
            plotbootstrap_hubspoke(bslist=bscontrast,
                                   ax=ax_contrast,
                                   violinWidth=violinWidth,
                                   violinOffset=violinOffset,
                                   markersize=summaryMarkerSize,
                                   marker=summaryMarkerType,
                                   linewidth=lineWidth)

        if floatContrast is False:
            fig.add_subplot(ax_contrast)

        if gsIdx > 0:
            ax_raw.set_ylabel('')
            ax_contrast.set_ylabel('')

    # Turn contrastList into a pandas DataFrame,
    contrastList = pd.DataFrame(contrastList).T
    contrastList.columns = contrastListNames

    ########
    axesCount = len(fig.get_axes())

    ## Loop thru SWARM axes for aesthetic touchups.
    for i in range(0, axesCount, 2):
        axx = fig.axes[i]

        if i != axesCount - 2 and 'hue' in kwargs:
            # If this is not the final swarmplot, remove the hue legend.
            axx.legend().set_visible(False)

        if floatContrast is False:
            axx.xaxis.set_visible(False)
            sns.despine(ax=axx, trim=True, bottom=False, left=False)
        else:
            sns.despine(ax=axx, trim=True, bottom=True, left=True)

        if showAllYAxes is False:
            if i in range(2, axesCount):
                axx.yaxis.set_visible(showAllYAxes)
            else:
                # Draw back the lines for the relevant y-axes.
                # Not entirely sure why I have to do this.
                drawback_y(axx)

        # Add zero reference line for swarmplots with bars.
        if summaryBar is True:
            axx.add_artist(
                Line2D((axx.xaxis.get_view_interval()[0],
                        axx.xaxis.get_view_interval()[1]), (0, 0),
                       color='black',
                       linewidth=0.75))

        # I don't know why the swarm axes controls the contrast axes ticks....
        if showGroupCount:
            count = data.groupby(x).count()[y]
            newticks = list()
            for ix, t in enumerate(axx.xaxis.get_ticklabels()):
                t_text = t.get_text()
                nt = t_text + ' n=' + str(count[t_text])
                newticks.append(nt)
            axx.xaxis.set_ticklabels(newticks)

        if legend is False:
            axx.legend().set_visible(False)
        else:
            if i == axesCount - 2:  # the last (rightmost) swarm axes.
                axx.legend(loc='top right',
                           bbox_to_anchor=(1.1, 1.0),
                           fontsize=legendFontSize,
                           **legendFontProps)

    ## Loop thru the CONTRAST axes and perform aesthetic touch-ups.
    ## Get the y-limits:
    for j, i in enumerate(range(1, axesCount, 2)):
        axx = fig.get_axes()[i]

        if floatContrast is False:
            xleft, xright = axx.xaxis.get_view_interval()
            # Draw zero reference line.
            axx.hlines(y=0,
                       xmin=xleft - 1,
                       xmax=xright + 1,
                       linestyle=contrastZeroLineStyle,
                       linewidth=0.75,
                       color=contrastZeroLineColor)
            # reset view interval.
            axx.set_xlim(xleft, xright)
            # # Draw back x-axis lines connecting ticks.
            # drawback_x(axx)

            if showAllYAxes is False:
                if i in range(2, axesCount):
                    axx.yaxis.set_visible(False)
                else:
                    # Draw back the lines for the relevant y-axes.
                    # Not entirely sure why I have to do this.
                    drawback_y(axx)

            sns.despine(ax=axx,
                        top=True,
                        right=True,
                        left=False,
                        bottom=False,
                        trim=True)

            # Rotate tick labels.
            rotateTicks(axx, tickAngle, tickAlignment)

        else:
            # Re-draw the floating axis to the correct limits.
            lower = np.min(contrastList.ix['diffarray', j])
            upper = np.max(contrastList.ix['diffarray', j])
            meandiff = contrastList.ix['summary', j]

            ## Make sure we have zero in the limits.
            if lower > 0:
                lower = 0.
            if upper < 0:
                upper = 0.

            ## Get the tick interval from the left y-axis.
            leftticks = fig.get_axes()[i - 1].get_yticks()
            tickstep = leftticks[1] - leftticks[0]

            ## First re-draw of axis with new tick interval
            axx.yaxis.set_major_locator(MultipleLocator(base=tickstep))
            newticks1 = axx.get_yticks()

            ## Obtain major ticks that comfortably encompass lower and upper.
            newticks2 = list()
            for a, b in enumerate(newticks1):
                if (b >= lower and b <= upper):
                    # if the tick lies within upper and lower, take it.
                    newticks2.append(b)
            # if the meandiff falls outside of the newticks2 set, add a tick in the right direction.
            if np.max(newticks2) < meandiff:
                ind = np.where(newticks1 == np.max(newticks2))[0][
                    0]  # find out the max tick index in newticks1.
                newticks2.append(newticks1[ind + 1])
            elif meandiff < np.min(newticks2):
                ind = np.where(newticks1 == np.min(newticks2))[0][
                    0]  # find out the min tick index in newticks1.
                newticks2.append(newticks1[ind - 1])
            newticks2 = np.array(newticks2)
            newticks2.sort()

            ## Second re-draw of axis to shrink it to desired limits.
            axx.yaxis.set_major_locator(FixedLocator(locs=newticks2))

            ## Despine the axes.
            sns.despine(ax=axx,
                        trim=True,
                        bottom=False,
                        right=False,
                        left=True,
                        top=True)

    # Normalize bottom/right Contrast axes to each other for Cummings hub-and-spoke plots.
    if (axesCount > 2 and contrastShareY is True and floatContrast is False):

        # Set contrast ylim as max ticks of leftmost swarm axes.
        if contrastYlim is None:
            lower = list()
            upper = list()
            for c in range(0, len(contrastList.columns)):
                lower.append(np.min(contrastList.ix['bca_ci_low', c]))
                upper.append(np.max(contrastList.ix['bca_ci_high', c]))
            lower = np.min(lower)
            upper = np.max(upper)
        else:
            lower = contrastYlim[0]
            upper = contrastYlim[1]

        normalizeContrastY(fig,
                           contrast_ylim=contrastYlim,
                           show_all_yaxes=showAllYAxes)

    # if (axesCount==2 and
    #     floatContrast is False):
    #     drawback_x(fig.get_axes()[1])
    #     drawback_y(fig.get_axes()[1])

    # if swarmShareY is False:
    #     for i in range(0, axesCount, 2):
    #         drawback_y(fig.get_axes()[i])

    # if contrastShareY is False:
    #     for i in range(1, axesCount, 2):
    #         if floatContrast is True:
    #             sns.despine(ax=fig.get_axes()[i],
    #                        top=True, right=False, left=True, bottom=True,
    #                        trim=True)
    #         else:
    #             sns.despine(ax=fig.get_axes()[i], trim=True)

    # Zero gaps between plots on the same row, if floatContrast is False
    if (floatContrast is False and showAllYAxes is False):
        gsMain.update(wspace=0.)

    else:
        # Tight Layout!
        gsMain.tight_layout(fig)

    # And we're all done.
    rcdefaults()  # restore matplotlib defaults.
    sns.set()  # restore seaborn defaults.
    return fig, contrastList
예제 #16
0
#
####################################################
# This figure spreads them all out

pylab.figure(1,figsize=(5,1.7))
fig = pylab.gcf()
# make three panels panels
outer_grid  = gridspec.GridSpec(1,2, width_ratios=[2, 1], wspace=0.3)

##############################################################################
#
# Panel (a). Pooled SFS as function of minor allele freq
#
##############################################################################

sfs_axis = plt.Subplot(fig, outer_grid[0])
fig.add_subplot(sfs_axis)

sfs_axis.set_xlabel('Minor allele freq in largest clade, $f$')
sfs_axis.set_ylabel('Scaled fraction of sites, $f(1-f) P(f)$')
sfs_axis.set_xlim([0,0.5])
sfs_axis.spines['top'].set_visible(False)
sfs_axis.spines['right'].set_visible(False)
sfs_axis.get_xaxis().tick_bottom()
sfs_axis.get_yaxis().tick_left()

##############################################################################
#
# Panel (b). Comparison of 1D and 4D for minor allele *count* 
#
##############################################################################
예제 #17
0
파일: visualize.py 프로젝트: ckm3/eleanor
    def pixel_by_pixel(self,
                       colrange=None,
                       rowrange=None,
                       cmap='viridis',
                       data_type="corrected",
                       mask=None,
                       xlim=None,
                       ylim=None,
                       color_by_pixel=False,
                       freq_range=[1 / 20., 1 / 0.1]):
        """
        Creates a pixel-by-pixel light curve using the corrected flux.
        Contribution from Oliver Hall.

        Parameters
        ----------
        colrange : np.array, optional
             A list of start column and end column you're interested in
             zooming in on.
        rowrange : np.array, optional
             A list of start row and end row you're interested in zooming
             in on.
        cmap : str, optional
             Name of a matplotlib colormap. Default is 'viridis'.
        data_type : str, optional
             The type of flux used. Either: 'raw', 'corrected', 'amplitude',
             or 'periodogram'. If not, default set to 'corrected'.
        mask : np.array, optional
             Specifies the cadences used in the light curve. If not, default
             set to good quality cadences.
        xlim : np.array, optional
             Specifies the xlim on the subplots. If not, default is set to
             the entire light curve.
        ylim : np.array, optional
             Specifies the ylim on the subplots, If not, default is set to
             the entire light curve flux range.
        color_by_pixel : bool, optional
             Colors the light curve given the color of the pixel. If not,
             default is set to False.
        freq_range : list, optional
             List of minimum and maximum frequency to search in Lomb Scargle
             periodogram. Only used if data_type = 'periodogram'. If None,
             default = [1/20., 1/0.1].
        """
        if colrange is None:
            colrange = [0, self.dimensions[1]]

        if rowrange is None:
            rowrange = [0, self.dimensions[0]]

        nrows = int(np.round(colrange[1] - colrange[0]))
        ncols = int(np.round(rowrange[1] - rowrange[0]))

        if (colrange[1] > self.dimensions[1]) or (rowrange[1] >
                                                  self.dimensions[0]):
            raise ValueError(
                "Asking for more pixels than available in the TPF.")

        figure = plt.figure(figsize=(20, 8))
        outer = gridspec.GridSpec(1, 2, width_ratios=[1, 4])

        inner = gridspec.GridSpecFromSubplotSpec(ncols,
                                                 nrows,
                                                 hspace=0.1,
                                                 wspace=0.1,
                                                 subplot_spec=outer[1])

        i, j = rowrange[0], colrange[0]

        if mask is None:
            q = self.obj.quality == 0
        else:
            q = mask == 0

        ## PLOTS TARGET PIXEL FILE ##
        ax = plt.subplot(outer[0])

        c = ax.imshow(self.flux[100, rowrange[0]:rowrange[1],
                                colrange[0]:colrange[1]],
                      vmax=np.percentile(self.flux[100], 95),
                      cmap=cmap)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes('right', size='5%', pad=0.15)
        plt.colorbar(c, cax=cax, orientation='vertical')

        ## PLOTS PIXEL LIGHT CURVES ##
        for ind in range(int(nrows * ncols)):
            ax = plt.Subplot(figure, inner[ind])

            flux = self.flux[:, i, j]
            time = self.obj.time
            corr_flux = self.obj.corrected_flux(flux=flux)

            if data_type.lower() == 'corrected':
                y = corr_flux[q] / np.nanmedian(corr_flux[q])
                x = time[q]

            elif data_type.lower() == 'amplitude':
                lc = lk.LightCurve(time=time, flux=corr_flux)
                pg = lc.normalize().to_periodogram()
                x = pg.frequency.value
                y = pg.power.value

            elif data_type.lower() == 'raw':
                y = flux[q] / np.nanmedian(flux[q])
                x = time[q]

            elif data_type.lower() == 'periodogram':
                freq, power = LombScargle(time, corr_flux).autopower(
                    minimum_frequency=freq_range[0],
                    maximum_frequency=freq_range[1],
                    method='fast')
                y = power
                x = 1 / freq

            if color_by_pixel is False:
                color = 'k'
            else:
                rgb = c.cmap(c.norm(self.flux[100, i, j]))
                color = matplotlib.colors.rgb2hex(rgb)

            ax.plot(x, y, c=color)

            j += 1
            if j == colrange[1]:
                i += 1
                j = colrange[0]

            if ylim is None:
                ax.set_ylim(np.percentile(y, 1), np.percentile(y, 99))
            else:
                ax.set_ylim(ylim[0], ylim[1])

            if xlim is None:
                ax.set_xlim(np.min(x) - 0.1, np.max(x) + 0.1)
            else:
                ax.set_xlim(xlim[0], xlim[1])

            if data_type.lower() == 'amplitude':
                ax.set_yscale('log')
                ax.set_xscale('log')
                ax.set_ylim(y.min(), y.max())
                ax.set_xlim(np.min(x), np.max(x))
#                ax.set_xticks([])
#                ax.set_yticks([])

            ax.set_xticks([])
            ax.set_yticks([])

            figure.add_subplot(ax)

        return figure
예제 #18
0
        shot_zones_O, player)

    shot_zones_d_on = calculate_shot_frequency(shot_zones_d_on)
    shot_zones_d_off = calculate_shot_frequency(shot_zones_d_off)
    shot_zones_o_on = calculate_shot_frequency(shot_zones_o_on)
    shot_zones_o_off = calculate_shot_frequency(shot_zones_o_off)

    shot_zones_d_comb = combine_on_off(shot_zones_d_on, shot_zones_d_off)
    shot_zones_o_comb = combine_on_off(shot_zones_o_on, shot_zones_o_off)

    fig = plt.figure(figsize=(16, 6))
    fig.suptitle("{} \n On-Off Shot Frequency Charts".format(player),
                 fontsize=12)
    gs0 = gridspec.GridSpec(1, 2)

    ax1 = plt.Subplot(fig, gs0[0, 0])
    fig.add_subplot(ax1)

    ####
    #### Offensive Shot Profile
    ####
    ax1.set_xlim(-250, 250)
    ax1.set_ylim(-47.5, 422.5)
    ax1 = drawCourt.draw_shot_chart_court_with_zones(ax=ax1, outer_lines=True)
    ax1.get_xaxis().set_visible(False)
    ax1.get_yaxis().set_visible(False)
    ax1.xaxis.label.set_visible(False)
    ax1.yaxis.label.set_visible(False)
    ax1.set_title("Offensive Shot Frequency", fontsize=12)

    ax1 = plot_on_off_shot_chart(ax1, shot_zones_o_comb, shot_frequency, -4, 4,
예제 #19
0
    def __init__(self,
                 xlims=None,
                 ylims=None,
                 d=.015,
                 tilt=45,
                 subplot_spec=None,
                 fig=None,
                 despine=True,
                 *args,
                 **kwargs):
        """Creates a grid of axes that act like a single broken axes

        Parameters
        ----------
        xlims, ylims: (optional) None or tuple of tuples, len 2
            Define the ranges over which to plot. If `None`, the axis is left
            unsplit.
        d: (optional) double
            Length of diagonal split mark used to indicate broken axes
        tilt: (optional) double
            Angle of diagonal split mark
        subplot_spec: (optional) None or Gridspec.subplot_spec
            Defines a subplot
        fig: (optional) None or Figure
            If no figure is defined, `plt.gcf()` is used
        despine: (optional) bool
            Get rid of right and top spines. Default: True
        wspace, hspace: (optional) bool
            Change the size of the horizontal or vertical gaps
        args, kwargs: (optional)
            Passed to gridspec.GridSpec

        Notes
        -----
        The broken axes effect is achieved by creating a number of smaller axes
        and setting their position and data ranges. A "big_ax" is used for
        methods that need the position of the entire broken axes object, e.g.
        `set_xlabel`.
        """

        self.despine = despine
        if fig is None:
            self.fig = plt.gcf()
        else:
            self.fig = fig

        if xlims:
            width_ratios = [i[1] - i[0] for i in xlims]
        else:
            width_ratios = [1]

        if ylims:
            height_ratios = [i[1] - i[0] for i in ylims[::-1]]
        else:
            height_ratios = [1]

        ncols, nrows = len(width_ratios), len(height_ratios)

        kwargs.update(ncols=ncols,
                      nrows=nrows,
                      height_ratios=height_ratios,
                      width_ratios=width_ratios)
        if subplot_spec:
            gs = gridspec.GridSpecFromSubplotSpec(subplot_spec=subplot_spec,
                                                  *args,
                                                  **kwargs)
            self.big_ax = plt.Subplot(self.fig, subplot_spec)
        else:
            gs = gridspec.GridSpec(*args, **kwargs)
            self.big_ax = plt.Subplot(self.fig, gridspec.GridSpec(1, 1)[0])

        [sp.set_visible(False) for sp in self.big_ax.spines.values()]
        self.big_ax.set_xticks([])
        self.big_ax.set_yticks([])
        self.big_ax.patch.set_facecolor('none')

        self.axs = []
        for igs in gs:
            ax = plt.Subplot(self.fig, igs)
            self.fig.add_subplot(ax)
            self.axs.append(ax)
        self.fig.add_subplot(self.big_ax)

        for i, ax in enumerate(self.axs):
            if ylims is not None:
                ax.set_ylim(ylims[::-1][i // ncols])
            if xlims is not None:
                ax.set_xlim(xlims[i % ncols])
        self.standardize_ticks()
        if d:
            self.draw_diags(d, tilt)
        if despine:
            self.set_spines()
예제 #20
0
    def _plot_overview(self, fig, outer_grid=None, time_step=-1, plot_agent_status=True, plot_path=True,
                       plot_input=False, plot_info=False, title=''):
        """
        Plot a map all agents are included
        :param fig: matplotlib figure
        :param outer_grid: none or matplotlib grid this map should be plotted in
        :param time_step: time step to be plotted
        :param plot_agent_status: plot agents status (not implemented yet)
        :param plot_path: plot a line from start via each step to the current position
        :param plot_input: unused parameter. Just here to make list of parameters equal to _plot_all()
        :param plot_info: plot some information below the map
        :param title: title of plot
        :return: matplotlib figure
        """
        if outer_grid is None:
            outer_grid = gridspec.GridSpec(1, 1, wspace=0, hspace=0)[0]
            grid = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=outer_grid,
                                                    wspace=0.1, hspace=0.1, width_ratios=[1],
                                                    height_ratios=[0, 5, 1 if plot_info else 0])
        else:
            grid = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=outer_grid,
                                                    wspace=0.1, hspace=0.1, width_ratios=[1],
                                                    height_ratios=[1 if plot_info else 0, 3, 1 if plot_info else 0])
        ax = plt.Subplot(fig, grid[1])

        # Obstacles
        for x, y in self._obstacle_pos:
            self._plot_rect_at_pos(ax, x, y, 'black')

        # Add maps of all agents to the plot
        for i_agent in range(self._agent_count):
            if self._view_reduced:
                start_pos = [self._current_pos[0, i_agent]]
                cur_pos = [self._current_pos[time_step, i_agent]]
                aim_pos = [self._aim_pos[time_step, i_agent]]
            else:
                start_pos = np.argwhere(self._current_maps[0, i_agent])
                cur_pos = np.argwhere(self._current_maps[time_step, i_agent])
                aim_pos = np.argwhere(self._aim_maps[time_step, i_agent])

            # nxt_map = ... TODO: Next step

            color = self._get_plot_color(i_agent, next_step=False)
            # color_next = self._get_plot_color(i_agent, next_step=True)  # TODO: Next step

            # Plot next position
            # TODO: Next step
            # if self._next_step:
            #     self._plot_map(ax, nxt_map, color_next)

            # Plot current position
            for x, y in cur_pos:
                self._plot_rect_at_pos(ax, x, y, color)

            # Plot path
            if plot_path:
                hist = np.where(self._current_maps[0:time_step + 1, i_agent])
                offset = (1 / (self._agent_count + 1) * (i_agent + 1) * 0.5) - 0.25
                x = hist[2] + 0.5 + offset
                y = self._map_size_x - hist[1] - 0.5 + offset
                ax.plot(x, y, '-', color=color, zorder=0)

            # Plot start position
            for x, y in start_pos:
                self._plot_label(ax, x - 0.15, y + 0.2, "S", color)

            # Plot aim position
            for x, y in aim_pos:
                self._plot_label(ax, x - 0.15, y + 0.2, "E", color)

            # # Plot agent status  # TODO: Agent status
            # if plot_agent_status:
            #     for status, symbol in zip(['a', 's', '3', 't'], ['\u2713', '\u2717', '\u2717', '\u2717']):  # \u2620
            #         if self._agents_conditions[i_agent] == status:
            #             for x, y in self._current_maps[time_step, i_agent]:
            #                 self._plot_label(ax, x - 0.15, y + 0.2, symbol, 'black')

        # Plot Border
        self._plot_map_border(ax)

        ax.set_ylim(0, self._map_size_x)
        ax.set_xlim(0, self._map_size_y)
        ax.set_aspect('equal')
        ax.set_xticks([])
        ax.set_yticks([])
        ax.axis('off')
        ax.set_title(title, fontsize=15)
        fig.add_subplot(ax)

        # Add info box below if wanted
        if plot_info:
            ax = plt.Subplot(fig, grid[2])
            self._plot_info(ax, time_step)
            fig.add_subplot(ax)

        return fig
예제 #21
0
def display_code_marginal_densities(codes,
                                    num_hist_bins,
                                    lines=True,
                                    overlaid=False,
                                    plot_title=""):
    """
  Estimate the marginal density of the coefficients of a code over some dataset

  Parameters
  ----------
  codes : ndarray(float32, size=(s, D))
      The codes for a dataset of size D. These are the vectors x for each sample
      from the dataset. The value s is the dimensionality of the code
  num_hist_bins : int
      The number of bins to use when we make a histogram estimate of the
      empirical density.
  lines : bool, optional
      If true, plot the binned counts using a line rather than bars. This
      can make it a lot easier to compare multiple datasets at once but
      can look kind of jagged if there aren't many samples
  overlaid : bool, optional
      If true, then make a single plot with the marginal densities all overlaid
      on top of eachother. This gets messy for more than a few coefficients.
      Alteratively, display the densities in their own separate plots.
      Default False.
  plot_title : str, optional
      The title of the plot. Default ""

  Returns
  -------
  code_density_figs : list
      A list containing pyplot figures. Can be saved separately, or whatever
      from the calling function

  """
    if overlaid:
        # there's just a single plot
        fig = plt.figure(figsize=(15, 15))
        fig.suptitle(plot_title, fontsize=15)
        ax = plt.subplot(1, 1, 1)
        blue = plt.get_cmap('Blues')
        cmap_indeces = np.linspace(0.25, 1.0, codes.shape[0])

        histogram_min = np.min(codes)
        histogram_max = np.max(codes)
        histogram_bin_edges = np.linspace(histogram_min, histogram_max,
                                          num_hist_bins + 1)
        histogram_bin_centers = (histogram_bin_edges[:-1] +
                                 histogram_bin_edges[1:]) / 2
        for de_idx in range(codes.shape[0]):
            counts, _ = np.histogram(codes[de_idx], histogram_bin_edges)
            empirical_density = counts / np.sum(counts)
            if lines:
                ax.plot(histogram_bin_centers,
                        empirical_density,
                        color=blue(cmap_indeces[de_idx]),
                        linewidth=2,
                        label='Coeff idx ' + str(de_idx))
            else:
                ax.bar(histogram_bin_centers,
                       empirical_density,
                       align='center',
                       color=blue(cmap_indeces[de_idx]),
                       width=histogram_bin_centers[1] -
                       histogram_bin_centers[0],
                       alpha=0.4,
                       label='Coeff idx ' + str(de_idx))
        ax.legend(fontsize=10)
        de_figs = [fig]

    else:
        # every coefficient gets its own subplot
        max_de_per_fig = 80 * 80  # max 80x80 {d}ictionary {e}lements displayed each fig
        assert np.sqrt(max_de_per_fig) % 1 == 0, 'please pick a square number'
        num_de = codes.shape[0]
        num_de_figs = int(np.ceil(num_de / max_de_per_fig))
        # this determines how many dictionary elements are aranged in a square
        # grid within any given figure
        if num_de_figs > 1:
            de_per_fig = max_de_per_fig
        else:
            squares = [
                x**2 for x in range(1,
                                    int(np.sqrt(max_de_per_fig)) + 1)
            ]
            de_per_fig = squares[bisect.bisect_left(squares, num_de)]
        plot_sidelength = int(np.sqrt(de_per_fig))

        de_idx = 0
        de_figs = []
        for in_de_fig_idx in range(num_de_figs):
            fig = plt.figure(figsize=(15, 15))
            fig.suptitle(
                plot_title +
                ', fig {} of {}'.format(in_de_fig_idx + 1, num_de_figs),
                fontsize=15)
            subplot_grid = gridspec.GridSpec(plot_sidelength,
                                             plot_sidelength,
                                             wspace=0.35,
                                             hspace=0.35)

            fig_de_idx = de_idx % de_per_fig
            while fig_de_idx < de_per_fig and de_idx < num_de:
                if de_idx % 100 == 0:
                    print('plotted', de_idx, 'of', num_de, 'code coefficients')
                ax = plt.Subplot(fig, subplot_grid[fig_de_idx])
                histogram_min = min(codes[de_idx, :])
                histogram_max = max(codes[de_idx, :])
                histogram_bin_edges = np.linspace(histogram_min, histogram_max,
                                                  num_hist_bins + 1)
                histogram_bin_centers = (histogram_bin_edges[:-1] +
                                         histogram_bin_edges[1:]) / 2
                counts, _ = np.histogram(codes[de_idx, :], histogram_bin_edges)
                empirical_density = counts / np.sum(counts)
                max_density = np.max(empirical_density)
                variance = np.var(codes[de_idx, :])
                hist_kurtosis = kurtosis(empirical_density, fisher=False)

                if lines:
                    ax.plot(histogram_bin_centers,
                            empirical_density,
                            color='k',
                            linewidth=1)
                else:
                    ax.bar(histogram_bin_centers,
                           empirical_density,
                           align='center',
                           color='k',
                           width=histogram_bin_centers[1] -
                           histogram_bin_centers[0])

                ax.yaxis.set_major_formatter(FormatStrFormatter('%0.1f'))
                ax.xaxis.set_major_formatter(FormatStrFormatter('%0.1f'))
                ax.tick_params(axis='both', which='major', labelsize=5)
                ax.set_xticks([histogram_min, 0., histogram_max])
                ax.set_yticks([0., max_density])
                ax.spines['right'].set_visible(False)
                ax.spines['top'].set_visible(False)
                # ax.set_yscale('log')

                ax.text(0.1,
                        0.97,
                        '{:.2f}'.format(variance),
                        horizontalalignment='left',
                        verticalalignment='top',
                        transform=ax.transAxes,
                        color='b',
                        fontsize=5)
                ax.text(0.1,
                        0.8,
                        '{:.2f}'.format(hist_kurtosis),
                        horizontalalignment='left',
                        verticalalignment='top',
                        transform=ax.transAxes,
                        color='r',
                        fontsize=5)

                fig.add_subplot(ax)
                fig_de_idx += 1
                de_idx += 1
            de_figs.append(fig)

    return de_figs
예제 #22
0
    def _plot_all(self, fig, time_step=-1, plot_agent_status=True, plot_path=True, plot_input=False,
                  plot_info=False, overview_title='Overview'):
        """
        Plot a visualisation with a big overview map and
        small maps for all agents for all types of object in the environment
        :param fig: matplotlib figure
        :param time_step: time step to be plotted
        :param plot_agent_status: plot agents status (not implemented yet)
        :param plot_path: plot a line from start via each step to the current position
        :param plot_input: show also heatmaps to visualize network input
        :param plot_info: plot some information below the overview map
        :param overview_title: title of overview map
        :return: matplotlib figure
        """
        # Create outer grid
        outer = gridspec.GridSpec(1, 2, wspace=0.1, hspace=0.1, width_ratios=[0.382, 0.618])
        outer.update(left=0.01, right=0.99, top=0.95, bottom=0.01)

        # Plot overview (and information box) on the left side
        self._plot_overview(fig, outer[0], time_step=time_step, plot_agent_status=plot_agent_status,
                            plot_path=plot_path, plot_info=plot_info, title=overview_title)

        # Define titles of the small maps
        if self._next_step:
            nr_maps = 6
            maps_names = ['Obstacles', 'Aim', 'Agent\'s\nCurrent Pos.', 'Agent\'s\nNext Pos.',
                          'Others\nCurrent Pos.', 'Others\nNext Pos.']
        else:
            nr_maps = 4
            maps_names = ['Obstacles', 'Aim', 'Agent\'s\nPosition', 'Others\nPosition']
        if plot_input:
            nr_maps += 2
            maps_names.append('Full Map\nNet Input')
            maps_names.append('Reduced\nNet Input')

        # Create the right grid for the small maps
        agents_grid = gridspec.GridSpecFromSubplotSpec(self._agent_count, nr_maps, subplot_spec=outer[1],
                                                       wspace=0.1, hspace=0.1)

        # Plot small maps
        for i_agent in range(self._agent_count):
            maps = self.get_maps_for_agent(time_step=time_step, agent=i_agent, plot_input=plot_input)
            for i_map, map_ in enumerate(maps):
                i_grid = i_agent * nr_maps + i_map
                ax = plt.Subplot(fig, agents_grid[i_grid])
                if plot_input and i_map + 2 >= nr_maps:
                    self._plot_heatmap(ax, map_)
                else:
                    color = self._get_plot_color(i_agent)
                    if self._view_reduced:
                        self._plot_map(ax, map_, color, plot_view_filed=True,
                                       curr_pos=self._current_pos[time_step, i_agent])
                    else:
                        self._plot_map(ax, map_, color)

                # add map titles
                if ax.is_first_row():
                    ax.set_xlabel(maps_names[i_map], fontsize=15)
                    ax.xaxis.set_label_position('top')

                # add agent titles
                if ax.is_first_col():
                    ax.set_ylabel('Agent {}'.format(i_agent), fontsize=15)

                fig.add_subplot(ax)

        plt.subplots_adjust(wspace=0, hspace=0)

        return fig
sys.stderr.write("Postprocessing %d species...\n" % len(species_names))

####################################################
#
# Set up Figure (1 panels, arranged in 1x1 grid)
#
####################################################

haploid_color = '#08519c'

pylab.figure(1, figsize=(4, 1))
fig = pylab.gcf()
# make three panels panels
outer_grid = gridspec.GridSpec(1, 1)

divergence_axis = plt.Subplot(fig, outer_grid[0])
fig.add_subplot(divergence_axis)

divergence_axis.set_ylabel('Divergence, $d$')
divergence_axis.set_ylim([1e-06, 1e-01])
divergence_axis.set_xlim([-1, len(species_names)])

xticks = numpy.arange(0, len(species_names))
xticklabels = [
    "%s (%d)" % (species_names[i], sample_sizes[i])
    for i in xrange(0, len(sample_sizes))
]
#xticklabels = ["%s" % (species_names[i]) for i in xrange(0,len(sample_sizes))]

divergence_axis.set_xticks(xticks)
divergence_axis.set_xticklabels(xticklabels, rotation='vertical', fontsize=4)
def placeAxesOnGrid(fig,
                    dim=[1, 1],
                    xspan=[0, 1],
                    yspan=[0, 1],
                    wspace=None,
                    hspace=None,
                    sharex=False,
                    sharey=False):
    '''
    Takes a figure with a gridspec defined and places an array of sub-axes on a portion of the gridspec

    Takes as arguments:
        fig: figure handle - required
        dim: number of rows and columns in the subaxes - defaults to 1x1
        xspan: fraction of figure that the subaxes subtends in the x-direction (0 = left edge, 1 = right edge)
        yspan: fraction of figure that the subaxes subtends in the y-direction (0 = top edge, 1 = bottom edge)
        wspace and hspace: white space between subaxes in vertical and horizontal directions, respectively

    returns:
        subaxes handles
    '''
    import matplotlib.gridspec as gridspec

    outer_grid = gridspec.GridSpec(100, 100)
    inner_grid = gridspec.GridSpecFromSubplotSpec(
        dim[0],
        dim[1],
        subplot_spec=outer_grid[int(100 * yspan[0]):int(100 * yspan[1]),
                                int(100 * xspan[0]):int(100 * xspan[1])],
        wspace=wspace,
        hspace=hspace)

    # NOTE: A cleaner way to do this is with list comprehension:
    # inner_ax = [[0 for ii in range(dim[1])] for ii in range(dim[0])]
    inner_ax = dim[0] * [
        dim[1] * [fig]
    ]  # filling the list with figure objects prevents an error when it they are later replaced by axis handles
    inner_ax = np.array(inner_ax)
    idx = 0
    for row in range(dim[0]):
        for col in range(dim[1]):
            if row > 0 and sharex == True:
                share_x_with = inner_ax[0][col]
            else:
                share_x_with = None

            if col > 0 and sharey == True:
                share_y_with = inner_ax[row][0]
            else:
                share_y_with = None

            inner_ax[row][col] = plt.Subplot(fig,
                                             inner_grid[idx],
                                             sharex=share_x_with,
                                             sharey=share_y_with)
            fig.add_subplot(inner_ax[row, col])
            idx += 1

    inner_ax = np.array(
        inner_ax).squeeze().tolist()  # remove redundant dimension
    return inner_ax
예제 #25
0
def drawPlots(imagefile, sampleTitle, orient, pix_per_cell, cell_per_block,
              trainScore, testScore, carimage, carhog, notcarimage, notcarhog,
              deltaTime):
    print("saving sample image and hogs to ", imagefile)
    # Setup plot
    fig = plt.figure(figsize=(10, 3))
    w_ratios = [1 for n in range(5)]
    h_ratios = [1 for n in range(1)]
    grid = gridspec.GridSpec(1,
                             5,
                             wspace=0.0,
                             hspace=0.0,
                             width_ratios=w_ratios,
                             height_ratios=h_ratios)
    i = 0

    # draw the images
    # next image
    sampleTitleWScores = '%s\n Orientation: %d\n Pix_per_cell: %d\n Cell_per_block: %d\n Train Accuracy:\n  %10.9f\n Test Accuracy:\n  %10.9f\n Decision Time:\n  %10.9f' % (
        sampleTitle, orient, pix_per_cell, cell_per_block, trainScore,
        testScore, deltaTime)
    ax = plt.Subplot(fig, grid[i])
    ax.text(0.1, 0.4, sampleTitleWScores, fontsize=8)
    ax.set_xticks([])
    ax.set_yticks([])
    for sp in ax.spines.values():
        sp.set_visible(False)
    fig.add_subplot(ax)
    i += 1

    ax = plt.Subplot(fig, grid[i])
    ax.imshow(carimage)
    if i == 1:
        ax.set_title('Sample Car Image', size=8)
    ax.set_xticks([])
    ax.set_yticks([])
    fig.add_subplot(ax)
    i += 1

    ax = plt.Subplot(fig, grid[i])
    ax.imshow(carhog, cmap='gray')
    if i == 2:
        ax.set_title('Sample Car HOG', size=8)
    ax.set_xticks([])
    ax.set_yticks([])
    fig.add_subplot(ax)
    i += 1

    ax = plt.Subplot(fig, grid[i])
    ax.imshow(notcarimage)
    if i == 3:
        ax.set_title('Sample Noncar Image', size=8)
    ax.set_xticks([])
    ax.set_yticks([])
    fig.add_subplot(ax)
    i += 1

    ax = plt.Subplot(fig, grid[i])
    ax.imshow(notcarhog, cmap='gray')
    if i == 4:
        ax.set_title('Sample Noncar HOG', size=8)

    ax.set_xticks([])
    ax.set_yticks([])
    fig.add_subplot(ax)
    i += 1

    plt.savefig(imagefile)
예제 #26
0
#
# Set up figure
#
####

fig = plt.figure(figsize=(7, 3))

outer_grid = gridspec.GridSpec(2, 1, height_ratios=[0.7, 1], hspace=0.4)

inner_grid = gridspec.GridSpecFromSubplotSpec(1,
                                              2,
                                              width_ratios=[1, 1],
                                              subplot_spec=outer_grid[1],
                                              wspace=0.1)  #, hspace=0.08)

trafic_axis = plt.Subplot(fig, outer_grid[0])
fig.add_subplot(trafic_axis)

trafic_axis.spines['top'].set_visible(False)
trafic_axis.spines['right'].set_visible(False)
trafic_axis.get_xaxis().tick_bottom()
trafic_axis.get_yaxis().tick_left()

trafic_axis.set_ylabel('Allele frequency, $f(t)$')
trafic_axis.set_xlabel('Generation, $t$')

trafic_axis.get_yaxis().set_tick_params(direction='out', length=3, pad=1)
trafic_axis.get_xaxis().set_tick_params(direction='out', length=3, pad=1)

trafic_axis.set_ylim([0, 1.05])
trafic_axis.set_xlim([0, 60000])
예제 #27
0

def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2 * np.pi, 0.05)):
    return np.sin(i * a) * np.cos(i * b), np.sin(i * c) * np.cos(i * d)


fig11 = plt.figure(figsize=(8, 8), constrained_layout=False)

# gridspec inside gridspec
outer_grid = fig11.add_gridspec(4, 4, wspace=0.0, hspace=0.0)

for i in range(16):
    inner_grid = outer_grid[i].subgridspec(3, 3, wspace=0.0, hspace=0.0)
    a, b = int(i / 4) + 1, i % 4 + 1
    for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
        ax = plt.Subplot(fig11, inner_grid[j])
        ax.plot(*squiggle_xy(a, b, c, d))
        ax.set_xticks([])
        ax.set_yticks([])
        fig11.add_subplot(ax)

all_axes = fig11.get_axes()

# show only the outside spines
for ax in all_axes:
    for sp in ax.spines.values():
        sp.set_visible(False)
    if ax.is_first_row():
        ax.spines['top'].set_visible(True)
    if ax.is_last_row():
        ax.spines['bottom'].set_visible(True)
예제 #28
0
    def _plot_sample(self,
                     fig,
                     inner_grid,
                     num_inner_plots,
                     ind,
                     inputs,
                     outputs=None,
                     predictions=None,
                     sample_ids=None,
                     is_one_hot=None):
        """Implementation of abstract method
        :meth:`data.dataset.Dataset._plot_sample`.

        Args:
            (....): See docstring of method
                :meth:`data.dataset.Dataset._plot_sample`.
            sample_ids (numpy.ndarray): See option ``sample_ids`` of method
                :meth:`get_out_pattern_bounds`. Only required if
                ``predictions`` is not ``None`` but provided as a sequence of
                labels (note, this method will consider the label at the end
                of the input sequence as predicted label).
            is_one_hot (bool, optional): Whether ``outputs`` and ``predictions``
                are provided as 1-hot encodings. If not specified, we will
                assume the value specified by attribute
                :attr:`data.dataset.Dataset.is_one_hot`.
        """
        if is_one_hot is None:
            is_one_hot = self.is_one_hot

        # Bring the data into a proper form.
        x = self._flatten_array(inputs,
                                ts_dim_first=True,
                                reverse=True,
                                feature_shape=self.in_shape)

        # Sanity check.
        if sample_ids is not None:
            eod = np.argwhere(x[:, 0, 3])
            assert eod.size == 1 and eod.squeeze() == \
                self.get_in_seq_lengths(sample_ids[[ind]]).squeeze() - 1

        if outputs is not None:
            # Note, the base class already removed 1-hot encoding from ground-
            # truth data.
            t = self._flatten_array(outputs,
                                    ts_dim_first=True,
                                    reverse=True,
                                    feature_shape=[1])
            if t.shape[0] > 1:  # Multiple timesteps.
                # Note, the label should be the same across all timesteps,
                # as this is a ground-truth output.
                t = t[0, :, :]
        if predictions is not None:
            fs = [self.num_classes] if is_one_hot else [1]
            y = self._flatten_array(predictions,
                                    ts_dim_first=True,
                                    reverse=True,
                                    feature_shape=fs)
            if y.shape[0] > 1:  # Multiple timesteps.
                # Note, we consider the correct label the one that is predicted
                # at the end of the input sequence.
                if sample_ids is None:
                    raise ValueError('Option "sample_ids" must be specified ' +
                                     'when providing timeseries predictions.')
                sl = self.get_in_seq_lengths(sample_ids[[ind]])
                y = y[int(sl[0]) - 1, :, :]

        ax = plt.Subplot(fig, inner_grid[0])

        if outputs is None:
            ax.set_title("SMNIST Sample")
        else:
            assert (np.size(t) == 1)
            label = np.asscalar(t)

            if predictions is None:
                ax.set_title('SMNIST sample with\nlabel: %d' % label)
            else:
                if np.size(y) == self.num_classes:
                    pred_label = np.argmax(y)
                else:
                    pred_label = np.asscalar(y)

                ax.set_title('SMNIST sample with\nlabel: %d (prediction: %d)' %
                             (label, pred_label))

        # Build image from stroke data.
        image = np.zeros((28, 28))
        eos = True
        for i in range(x.shape[0]):
            if x[i, 0, 3] == 1:  # end-of-digit
                break

            if eos:
                eos = False
                x_idx = int(x[i, 0, 0]) - 1
                y_idx = int(x[i, 0, 1]) - 1
            else:
                x_idx += int(x[i, 0, 0])
                y_idx += int(x[i, 0, 1])

            # This doesn't seem to matter. Seems only the first position is
            # absolute.
            #if x[i, 0, 2] == 1: # end-of-stroke
            #    eos = True

            x_idx = 0 if x_idx < 0 else x_idx
            y_idx = 0 if y_idx < 0 else y_idx
            x_idx = 27 if x_idx > 27 else x_idx
            y_idx = 27 if y_idx > 27 else y_idx
            image[x_idx, y_idx] = 255

        ax.set_axis_off()
        ax.imshow(image.transpose())
        fig.add_subplot(ax)

        if num_inner_plots == 2:
            ax = plt.Subplot(fig, inner_grid[1])
            ax.set_title('Predictions')
            bars = ax.bar(range(self.num_classes), np.squeeze(y))
            ax.set_xticks(range(self.num_classes))
            if outputs is not None:
                bars[int(label)].set_color('r')
            fig.add_subplot(ax)
def create_frame(frame, data, frame_idx, start_frame, end_frame):
    print_names = data["names"]
    loss_names = data["loss_names"]
    plt.style.use('dark_background')
    fig = plt.figure(figsize=(10, 4.5), dpi=100)
    canvas = FigureCanvas(fig)
    main_fig = gridspec.GridSpec(2,
                                 1,
                                 wspace=0.0,
                                 hspace=0.0,
                                 height_ratios=[6, 1])

    # figure handle for the video frames
    ax = plt.Subplot(fig, main_fig[0])
    ax.imshow(frame)
    ax.set_xticks([])
    ax.set_yticks([])
    fig.add_subplot(ax)

    # frame_fig = gridspec.GridSpecFromSubplotSpec(
    #     1, 1, subplot_spec=main_fig[0], wspace=0.0, hspace=0.0)

    # ax = plt.Subplot(fig, frame_fig[0])
    # ax.imshow(frame)
    # ax.set_xticks([])
    # ax.set_yticks([])
    # fig.add_subplot(ax)

    # create the handles for the ethogram style plots
    inner = gridspec.GridSpecFromSubplotSpec(len(print_names),
                                             1,
                                             subplot_spec=main_fig[1],
                                             wspace=0.0,
                                             hspace=0.0)

    label_colors = [
        'cyan', 'yellow', 'lime', 'red', 'magenta', 'lavenderblush'
        # 'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown'
    ]
    label_names = ["Lift", "Hand", "Grab", "Supinate", "At Mouth", "Chew"]

    # ylabels = ["ground truth", "wasserstein"]
    bar_height = 0.01
    ylabels = print_names
    # data_mat = [data["gt"], data["pred"]]
    num_frames = data["gt"].shape[0]
    # for j in range(len(print_names)):
    for j in range(1):
        loss_key = loss_names[j]

        ax = plt.Subplot(fig, inner[j])
        # create the prediction bar
        for k in range(num_frames):
            if any(data[loss_key][k, :] > 0):
                idx = numpy.argmax(data[loss_key][k, :])
                # ax.plot([k, k], [0, 1], color=(0,1.0,0.0,1.0))
                # try:
                ax.plot([k, k], [0, bar_height], label_colors[idx])
                # except:
                #     import pdb; pdb.set_trace()

        # plot frame indicator
        ax.plot([start_frame + frame_idx, start_frame + frame_idx],
                [0, bar_height], 'snow')
        # [0, bar_height], '0.6')
        # ax.plot(data["lift"][:, j+1])
        # ax.set_ylabel(ylabels[j])
        ax.set_ylim([0, bar_height])
        # ax.set_xlim([0, num_frames])
        ax.set_xlim([start_frame, end_frame])
        ax.set_xticks([])
        # if j != len(print_names) - 1:
        #     ax.set_xticks([])
        ax.set_yticks([])
        fig.add_subplot(ax)

    plt.tight_layout()
    # main_fig.update(wspace=0.05, hspace=0.05)
    canvas.draw()  # draw the canvas, cache the renderer
    s, (width, height) = canvas.print_to_buffer()

    # Option 2a: Convert to a NumPy array.
    X = numpy.fromstring(s, numpy.uint8).reshape((height, width, 4))
    plt.close('all')

    # ... bgr ...
    bgr_frame = X[:, :, :3].copy()  # copy and get rid of alpha chan
    bgr_frame[:, :, 0] = X[:, :, 2]
    bgr_frame[:, :, 2] = X[:, :, 0]

    # add the time to the frame.
    # import pdb; pdb.set_trace()
    font = cv2.FONT_HERSHEY_SIMPLEX
    seconds_count = (frame_idx / 500.0)
    # temp = numpy.floor(seconds_count / 10)
    temp = numpy.floor(frame_idx / 10)
    offset = 0
    while temp > 0:
        offset = offset + 1
        temp = numpy.floor(temp / 10)

    # xoffset = 870 - offset * 20
    # cv2.putText(bgr_frame, "%.2f" % seconds_count, (xoffset, 360), font, 1, (255, 255, 0), 2)
    xoffset = 930 - offset * 15
    # cv2.putText(bgr_frame, "%.3f" % seconds_count, (xoffset, 360), font, 0.75, (255, 255, 255), 1)
    cv2.putText(bgr_frame, "%d" % frame_idx, (xoffset, 360), font, 0.75,
                (255, 255, 255), 1)

    # add label texts?
    # cv2.putText(bgr_frame, "GT", (270, 290), font, 0.5, (255, 255, 255), 1)
    # cv2.putText(bgr_frame, "Wasserstein", (200, 310), font, 0.5, (255, 255, 255), 1)
    # cv2.putText(bgr_frame, "Matching", (220, 330), font, 0.5, (255, 255, 255), 1)
    # cv2.putText(bgr_frame, "MSE", (255, 350), font, 0.5, (255, 255, 255), 1)
    cv2.putText(bgr_frame, "GT", (270, 350), font, 0.5, (255, 255, 255), 1)

    # for each label, add the label names
    base_x = 310
    y_coords = [290, 310, 330, 350]
    y_coords = [350, 310, 330, 350]
    x_offsets = [310, 345, 395, 440, 515, 595]
    label_bgr = [(255, 255, 0), (0, 255, 255), (0, 255, 0), (0, 0, 255),
                 (255, 0, 255), (245, 240, 255)]
    alpha = 0

    # for i in range(len(y_coords)):
    for i in range(1):
        loss_key = loss_names[i]
        for j in range(len(label_colors)):
            # for the loss, find the closest label idx from the current
            # frame.
            label_idxs = numpy.argwhere(data[loss_key][:, j]).flatten()
            min_dist = numpy.inf
            for k in range(len(label_idxs)):
                if numpy.abs((frame_idx + start_frame) -
                             label_idxs[k]) < min_dist:
                    min_dist = numpy.abs((frame_idx + start_frame) -
                                         label_idxs[k])
            alpha = 1 - numpy.min([30, min_dist]) / 30

            color = list(label_bgr[j])
            for k in range(len(color)):
                color[k] = int(color[k] * alpha)

            color = tuple(color)
            # if min_dist < 30:
            #     import pdb; pdb.set_trace()
            # import pdb; pdb.set_trace()
            if alpha > 0:
                cv2.putText(bgr_frame, label_names[j],
                            (x_offsets[j], y_coords[i]), font, 0.5, color, 1)

    return bgr_frame
#passed_species = species_phylogeny_utils.sort_phylogenetically(passed_species)
num_passed_species = len(passed_species)

####################################################
#
# Set up Figure (3 panels, arranged in 3x1 grid)
#
####################################################

pylab.figure(3,figsize=(7,1.25))

ld_fig = pylab.gcf()
# make three panels panels
ld_grid  = gridspec.GridSpec(1,3,width_ratios=[1,1,1],wspace=0.25)

ld_axis_1 = plt.Subplot(ld_fig, ld_grid[0])
ld_fig.add_subplot(ld_axis_1)

ld_axis_1.set_title(figure_utils.get_pretty_species_name('Bacteroides_fragilis_54507'), fontsize=5)
ld_axis_1.set_ylabel('Linkage disequilibrium, $\sigma^2_d$')
ld_axis_1.set_xlabel('Distance between SNPs, $\ell$')
ld_axis_1.spines['top'].set_visible(False)
ld_axis_1.spines['right'].set_visible(False)
ld_axis_1.spines['bottom'].set_zorder(22)
ld_axis_1.get_xaxis().tick_bottom()
ld_axis_1.get_yaxis().tick_left()  
ld_axis_1.set_xlim([2,1e04])
ld_axis_1.set_ylim([1e-02,1])
ld_axis_1.text(6e03,3.5e-03,'Genome-\nwide', horizontalalignment='center',fontsize='5')