Example #1
0
def albedo_parameter_plots(imarr, darr, params=None, plot_params=True,
                           ylabel='Reflectance', visible_only=True,
                           figsize=(12,7)):
    # from matplotlib import style
    # style.use('ggplot')
    if params is None:
        params = est_curve_params(darr, imarr)
    if visible_only:
        fig, axs = subplots(2, 3, figsize=figsize, sharey=False, sharex=True)
    else:
        fig, axs = subplots(2, 4, figsize=figsize, sharey=False, sharex=True)

    for i, ax in enumerate(axs.ravel()):
        if i >= imarr.shape[-1]:
            # This means I've got more axes than image bands so I'll skip plotting
            continue
        ax.scatter(darr.compressed(),imarr[...,i].compressed(), c='gold', alpha=0.2, edgecolor='none')
        cp = params[i]
        plotz = np.arange(darr.min(), darr.max(), 0.2)
        if plot_params:
            ax.plot(plotz, myR0(plotz, *cp), c='brown')
        ax.set_xlabel('Depth (m)')
        ax.set_ylabel(ylabel)
        btxt = "Band{b} $R_\infty = {R:.2f}$\n$A^{{toa}} = {A:.2f}$, $K_g = {Kg:.2f}$ "\
                .format(b=i+1, R=cp[0], A=cp[1], Kg=cp[2])
        ax.set_title(btxt)
    tight_layout()
    return fig
Example #2
0
def analyse_results(k,n,outpath=None):
    """Summarise multiple results"""

    if outpath != None:
        os.chdir(outpath)
    #add mirbase info
    k = k.merge(mirbase,left_on='name',right_on='mature1')
    ky1 = 'unique reads'
    ky2 = 'read count' #'RC'
    cols = ['name','freq','mean read count','mean_norm','total','perc','mirbase_id']
    print
    print ('found:')
    idcols,normcols = get_column_names(k)
    final = filter_expr_results(k,freq=.8,meanreads=200)
    print (final[cols])
    print ('-------------------------------')
    print ('%s total' %len(k))
    print ('%s with >=10 mean reads' %len(k[k['mean read count']>=10]))
    print ('%s found in 1 sample only' %len(k[k['freq']==1]))
    print ('top 10 account for %2.2f' %k['perc'][:10].sum())

    fig,ax = plt.subplots(nrows=1, ncols=1, figsize=(8,6))
    k.set_index('name')['total'][:10].plot(kind='barh',colormap='Spectral',ax=ax,log=True)
    plt.tight_layout()
    fig.savefig('srnabench_top_known.png')
    #fig = plot_read_count_dists(final)
    #fig.savefig('srnabench_known_counts.png')
    fig,ax = plt.subplots(figsize=(10,6))
    k[idcols].sum().plot(kind='bar',ax=ax)
    fig.savefig('srnabench_total_persample.png')
    print
    k.to_csv('srnabench_known_all.csv',index=False)
    return k
Example #3
0
def plot_4_4_cdfs(df, param_name, teff_categories, age_categories, title='Somechart', figsize=(16, 4)):
    """

    """
    # sequential colors..
    colors = ['#fecc5c', '#fd8d3c', '#f03b20', '#bd0026']

    fig1, axes1 = plt.subplots(nrows=1, ncols=4, sharex=True, sharey=True, figsize=figsize)
    fig2, axes2 = plt.subplots(nrows=1, ncols=4, sharex=True, sharey=True, figsize=figsize)
    fig3, axes3 = plt.subplots(nrows=1, ncols=4, sharex=True, sharey=True, figsize=figsize)
    for teff_bin in xrange(len(teff_categories)):
        ax = axes1[teff_bin]
        ax.set_title("{} K".format(teff_categories[teff_bin]))

        serieses = []
        for age_bin in xrange(len(age_categories)):
            series = df[(df.teff_bins == teff_bin+1) & (df.age_bins == age_bin+1)][param_name]
            s = series.copy()
            s.sort()
            serieses.append(s)
            ax.plot(np.arange(s.size)/float(s.size), s, label=age_categories[age_bin], color=colors[age_bin])

        # plot the KS-score for each age-group (4choose2 graphs)
        plot_ks_scores(serieses, axes2[teff_bin])
        plot_ks_scores(serieses, axes3[teff_bin], log=True)

    axes2[0].axes.get_yaxis().set_visible(False)
    axes3[0].axes.get_yaxis().set_visible(False)
    add_colorful_yticks(axes2[0], colors, len(age_categories))
    fig1.suptitle(title, size='xx-large', y=1.08)
    fig3.suptitle("Distribution-Pairs KS scores", size='xx-large', y=1.00)
    axes1[-1].legend(loc='upper center', bbox_to_anchor=(-1.3, -0.05), fancybox=True, shadow=True, ncol=4)
    fig1.tight_layout()
    fig2.tight_layout()
    return fig1, fig2, fig3
def iterTestRun(m, tspan):
    '''
    Method to perform iterations and generate plots
    '''
        
    # Test 16 random conditions - plot fft and phase diagrams
    fig, axes = subplots(4, 4)
    fig, axes2 = subplots(4, 4)
    for i in range(0, 4):
        for j in range(0, 4):
            # Run model
            yout = m.runModel(tspan, True)
            
            # Plot quiver diagram
            m.plotQuiver(yout, axes[i, j])
            
            # Plot Fourier transform
            freqs, aMags, rMags = m.calcFourier(yout, tspan)
            
            # Plot FFT
            axes2[i, j].plot(freqs, aMags, freqs, rMags)
            if i == 0:
                xlabel('Frequency (s^-1)')
            if j == 3:
                ylabel('Magnitude')
            
    legend(['Activator', 'Repressor'])
def plot_pis(indices,it):
    f,ax=pl.subplots(len(indices),sharex=True)
    for a in range(len(indices)):
        k=people[a]
        x=[]
        y=[]
        for j in range(settings.nlabels):
            for l in range(settings.nscores):
                x.append(l+j*settings.nlabels-0.4)
                y.append(pi(k,j,l))
        ax[a].bar(x,y)
        ax[a].get_yaxis().set_ticks([])
        ax[a].set_ylim(0,1)
    ax[0].set_xlim(-0.5,len(y)-0.5)
    pl.savefig('D:\Documents\images\pi_%03d.png' %it)
    
    f,ax=pl.subplots(len(indices),sharex=True)
    for a in range(len(indices)):
        i=indices[a]
        x=[]
        y=[]
        for j in range(settings.nlabels):
            x.append(j-0.4)
            y.append(results[i][j])
        ax[a].bar(x,y)
        ax[a].get_yaxis().set_ticks([])
        ax[a].set_ylim(0,1)
    ax[0].set_xlim(-0.5,len(y)-0.5)
    pl.savefig('D:\Documents\images\\result_%03d.png' %it)
    pl.close('all')
    def plot_network_representation(self):
        '''
            Plot the response of the network
        '''

        if self.W_type == 'dirichlet':
            # Sorting that emphasis balance
            balanced_indices_neurons = self.number_connections[:,0].argsort()[::-1]
        else:
            balanced_indices_neurons = np.arange(self.M)

        # Plot the population response
        plot_separation_y = 0.3*(np.max(self.network_representations) - np.min(self.network_representations))

        fig1, ax1 = plt.subplots(1)

        for r in xrange(self.R):
            ax1.plot(self.network_representations[r, :, balanced_indices_neurons] + np.arange(self.K)*plot_separation_y + r*(self.K+0.5)*plot_separation_y)

        ax1.autoscale(tight=True)

        # Plot Hinton graphs
        sf, ax = plt.subplots(self.R, 1)

        for r in xrange(self.R):
            hinton(self.W[r, balanced_indices_neurons].T, ax=ax[r])
def TablePlot():
    index_time=0
#    print [x[2] for x in RouteTablewithSeq]
        
    pl.ion()
    fig,ay=pl.subplots()
    fig,ax=pl.subplots()
    fig.set_tight_layout(True)
    
    idx_row = Index(np.arange(0,nNodes))
    idx_col = Index(np.arange(0,nPackets))
    df = DataFrame(cache_matrix[0,:,:], index=idx_row, columns=idx_col)
#    print df
    normal = pl.Normalize(0, 1)
    for index_time in range(len(RouteTablewithSeq_time)):
	vals=cache_matrix[index_time,:,:30]

#    fig = pl.figure(figsize=(15,8))
#    ax = fig.add_subplot(111, frameon=True, xticks=[], yticks=[])
#	print vals.shape
    	the_table=pl.table(cellText=vals, rowLabels=df.index, colLabels=df.columns, colWidths = [0.03]*vals.shape[1], loc='center', cellColours=pl.cm.hot(normal(vals)), fontsize=3)
	the_table.alpha=0
	for i in range(index_time+1):
		for j in range(vals.shape[0]):
			if (vals[j,i]==1):
				the_table._cells[(j+1, i)]._text.set_color('white')

	pl.title("Table at time: "+str(cache_time[index_time])+" Packet: "+str(index_time)+" Probability: "+str(p) )
    	pl.show()
	pl.pause(.0005)
	pl.clf()
Example #8
0
def analyse_diff_res2(res1, res2, res3, res4):
    plt.figure(95)
    plt.clf()
    #plt.title('Energy vs time')
    f, (ax1, ax2) = plt.subplots(2, 1)
    f.subplots_adjust(hspace=0.25)
    ax1.plot(res3['sim'][0] / 86400, np.array(res3['sim'][4]) / 1e15, 
             label='Eulerian')
    ax1.plot(res4['sim'][0] / 86400, np.array(res4['sim'][4]) / 1e15, 
             label='semi-Lagrangian')
    ax1.set_xlabel('time (days)')
    ax1.set_ylabel('energy (PJ)')
    ax1.legend(loc='lower right')

    ax2.plot(res3['sim'][0] / 86400, np.array(res3['sim'][4]) / 1e15, 
             label='Eulerian')
    ax2.plot(res4['sim'][0] / 86400, np.array(res4['sim'][4]) / 1e15, 
             label='semi-Lagrangian')
    ax2.set_xlim((60, 100))
    ax2.set_ylim((2.65, 2.75))
    ax2.set_xlabel('time (days)')
    ax2.set_ylabel('energy (PJ)')

    print('{}: energy_diff={}'.format(res3['res'], res3['energy_diff']))
    print('{}: energy_diff={}'.format(res4['res'], res4['energy_diff']))

    pd1 = (np.array(res3['sim'][4]).max() - res3['sim'][4][-1] ) / np.array(res3['sim'][4]).max() * 100
    pd2 = (np.array(res4['sim'][4]).max() - res4['sim'][4][-1] ) / np.array(res4['sim'][4]).max() * 100
    print('% diff 1 {}'.format(pd1))
    print('% diff 2 {}'.format(pd2))

    plt.savefig('writeup/figures/task_d_energy.png')

    f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
    f.subplots_adjust(hspace=0.25, wspace=0.25, right=0.8)

    X, Y = res1['grid']
    ax1.set_title('(a) $\eta$ after 1 day - Eul.')
    ax1.contourf(X/1e3, Y/1e3, res1['sim'][1], 100, vmin=-0.01, vmax=0.01)
    ax2.set_title('(b) $\eta$ after 1 day - S-L')
    cf2 = ax2.contourf(X/1e3, Y/1e3, res2['sim'][1], 100, vmin=-0.01, vmax=0.01)

    cbar_ax1 = f.add_axes([0.85, 0.55, 0.05, 0.4])
    f.colorbar(cf2, cax=cbar_ax1)

    ax3.set_title('(c) $\eta$ after 100 days - Eul.')
    ax3.contourf(X/1e3, Y/1e3, res3['sim'][1], 100, vmin=-0.15, vmax=0.2)
    ax4.set_title('(d) $\eta$ after 100 days - S-L')
    cf4 = ax4.contourf(X/1e3, Y/1e3, res4['sim'][1], 100, vmin=-0.15, vmax=0.2)

    ax3.set_xlabel('x (km)') 
    ax4.set_xlabel('x (km)') 
    ax1.set_ylabel('y (km)') 
    ax3.set_ylabel('y (km)') 

    cbar_ax2 = f.add_axes([0.85, 0.05, 0.05, 0.4])
    f.colorbar(cf4, cax=cbar_ax2)
    plt.savefig('writeup/figures/task_d_eta.png')
Example #9
0
def create_fig( file_name,
                small=True,
                marker=False,
                figsize=None,
                nrows=1,
                ncols=1,
                sharex=False,
                sharey=False ):
    if not isinstance( file_name, list ):
        file_name = [file_name]
    defaults = plot_defaults._get_defaults(small)
    params = plot_defaults._get_params( small )
    pylab.rcParams.update( params )
    pylab.rc('font', **defaults[ 'font' ])
    if figsize:
        fig, axs = pylab.subplots( nrows=nrows, ncols=ncols,
                                   sharex=sharex, sharey=sharey,
                                   figsize=figsize)
    else:
        fig, axs = pylab.subplots( nrows=nrows, ncols=ncols,
                                   sharex=sharex, sharey=sharey )
    if not isinstance(axs, list) and not isinstance(axs, ndarray):
        axs = [axs]
    for i, f_name in enumerate(file_name):
        curves = _input_from_txt(file_name = f_name, small=False)
        labels = curves.keys()
        labels.sort()
        for label in labels:
            curve = curves[ label ]
            number = label.split('_')[0]
            label  = '_'.join( label.split('_')[1:] )
            number = int(number)
            curr_defaults = plot_defaults._cycle_defaults( number, small=False )
            def current_default( key ):
                if key in curve.keys():
                    return curve[ key ]
                else:
                    if key in curr_defaults.keys():
                        return curr_defaults[key]
                    else:
                        if marker:
                            key += '_marker'
                            if key in curr_defaults.keys():
                                return curr_defaults[key]
                            else:
                                return None
            axs[i].plot( curve['x'], curve['y'],
                  color           = current_default( 'color'     ),
                  label           = label,
                  linewidth       = current_default( 'linewidth' ),
                  linestyle       = current_default( 'linestyle' ),
                  marker          = current_default( 'marker'    ),
                  markerfacecolor = current_default( 'markerfacecolor' ),
                  markeredgecolor = current_default( 'markeredgecolor' ),
                  markeredgewidth = current_default( 'markeredgewidth' ),
                  markersize      = current_default( 'markersize' )       )

    return fig
Example #10
0
def analyze(X, Y, data, classifier, label, outDir):
    #store the means for our ROC curve
    meanTPRate = 0.0
    meanFPRate = np.linspace(0, 1, 100)

    #start with the subplot
    pl.subplots()

    #now lets analyze the data
    for i, (train, test) in enumerate(data):
        #grab the data sets for training and testing
        xTrain, xTest, yTrain, yTest = X[train], X[test], Y[train], Y[test]
        #print xTrain
        #print yTrain

        #train the model
        classifier.fit(xTrain, yTrain)

        #now predict on the hold out
        predictions = classifier.predict(xTest)
        probabilities = classifier.predict_proba(xTest)

        #compute ROC and AUC
        fpr, tpr, thresholds = roc_curve(yTest, probabilities[:, 1])
        meanTPRate += interp(meanFPRate, fpr, tpr)
        meanTPRate[0] = 0.0
        rocAUC = auc(fpr,tpr)

        #now plot it out
        pl.plot(fpr, tpr, lw=1, label='ROC Iter %d (area = %0.2f)' % (i+1, rocAUC))

        #print "P: %s\nA: %s\n" % (predictions, yTest)

    #now plot the random chance line
    pl.plot([0,1], [0,1], '--', color=(0.6,0.6,0.6), label='Random Chance')

    #generate some stats for the mean plot
    meanTPRate /= len(data)
    meanTPRate[-1] = 1.0
    meanAUC = auc(meanFPRate, meanTPRate)

    #plot the average line
    pl.plot(meanFPRate, meanTPRate, 'k--', label='Mean ROC (area = %0.2f)' % (meanAUC), lw=2)

    #add some other plot parameters
    pl.xlim([-0.05, 1.05])
    pl.ylim([-0.05, 1.05])
    pl.xlabel('False Positive Rate')
    pl.ylabel('True Positive Rate')
    pl.title('ROC Curve (%s)' % (label))
    pl.legend(loc="lower right")

    pl.savefig('%s/%s.png' % (outDir, label))
    pl.close()
    logging.info("%s\t%f" % (label, meanAUC))
Example #11
0
def events_per_sim_cycle_histograms():
    data = np.loadtxt("analysisData/eventsAvailableBySimCycle.csv", dtype=np.intc, delimiter = ",", skiprows=2)
    trimmedData = reject_first_last_outliers(data)
    fig, ax1 = pylab.subplots()
    outFile = outDir + 'eventsAvailableBySimCycle-histogram-std'
    pylab.title('Total LPs: %s; ' % "{:,}".format(total_lps) +
                'Total Sim Cycles: %s. '% "{:,}".format(len(trimmedData)))
    ax1.hist(trimmedData, bins=100, histtype='stepfilled')
    ax1.set_xlabel('Number of Events')
    ax1.set_ylabel('Number of Simulation Cycles')
    ax2=ax1.twinx()
    ax2.hist(trimmedData, bins=100, histtype='stepfilled')
    ax2.set_ylabel('Percent of Simulation Cycles')
    ax2.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(toPercentOfTotalSimCycles))
    display_graph(outFile)

    # ok, so now let's build a histogram of the % of LPs with active events.

    fig, ax1 = pylab.subplots()
    outFile = outDir + 'percentOfLPsWithAvailableEvents'
    pylab.title('Percent of LPs w/ Available Events as a Percentage of the Total Sim Cycles')
    
    ax1.hist(trimmedData.astype(float)/float(total_lps), bins=100, histtype='stepfilled')
    ax1.set_xlabel('Number of Events as a percentage of Total LPs')
    ax1.set_ylabel('Number of Sim Cycles said Percentage Occurs')
#    ax1 = pylab.gca()
    ax1.get_xaxis().set_major_formatter(mpl.ticker.FuncFormatter(toPercent))
#    ax.get_yaxis().set_major_formatter(mpl.ticker.FormatStrFormatter('%.1f%%'))
    ax2=ax1.twinx()
    ax2.hist(trimmedData, bins=100, histtype='stepfilled')
    ax2.set_ylabel('Percent of Simulation Cycles')
    ax2.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(toPercentOfTotalSimCycles))
    display_graph(outFile)

    # ok, let's present the histogram data using pandas series/value_counts.  much nicer plot.
    fig, ax1 = pylab.subplots()
    outFile = outDir + 'eventsAvailableBySimCycle-histogram-dual'
    pylab.title('Total LPs: %s; ' % "{:,}".format(total_lps) +
                'Total Sim Cycles: %s. '% "{:,}".format(len(trimmedData)))
    setNumOfSimCycles(len(data)+1)
    mean_events_available = np.mean(trimmedData)
    data = pd.Series(trimmedData).value_counts()
    data = data.sort_index()
    x_values = np.array(data.keys())
    y_values = np.array(data)
    ax1.plot(x_values, y_values)
    ax1.set_xlabel('Number of Events (Ave=%.2f)' % mean_events_available)
    ax1.set_ylabel('Number of Simulation Cycles')
    ax2=ax1.twinx()
    ax2.plot(x_values, y_values)
    ax2.set_ylabel('Percent of Simulation Cycles')
    ax2.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(toPercentOfTotalSimCycles))
    display_graph(outFile)

    return
Example #12
0
def analyse_isomirs(iso,outpath=None):
    """Analyse isomiR results in detail"""

    if iso is None:
        return
    if outpath != None:
        os.chdir(outpath)
    subcols = ['name','read','isoClass','NucVar','total','freq']
    iso = iso.sort_values('total', ascending=False)
    #filter very low abundance reads
    iso = iso[(iso.total>10) & (iso.freq>0.5)]
    top = get_top_isomirs(iso)
    top.to_csv('srnabench_isomirs_dominant.csv',index=False)
    print ('top isomiRs:')
    print (top[:20])
    print ('%s/%s with only 1 isomir' %(len(top[top.domisoperc==1]),len(top)))
    print ('different dominant isomir:', len(top[top.variant!='exact'])/float(len(top)))
    print ('mean dom isomir perc:', top.domisoperc.mean())
    print
    #stats
    fig,ax = plt.subplots(1,1)
    top.plot('isomirs','total',kind='scatter',logy=True,logx=True,alpha=0.8,s=50,ax=ax)
    ax.set_title('no. isomiRs per miRNA vs total adundance')
    ax.set_xlabel('no. isomiRs')
    ax.set_ylabel('total reads')
    fig.savefig('srnabench_isomir_counts.png')
    fig,ax = plt.subplots(1,1)
    #top.hist('domisoperc',bins=20,ax=ax)
    try:
        base.sns.distplot(top.domisoperc,bins=15,ax=ax,kde_kws={"lw": 2})
    except:
        pass
    fig.suptitle('distribution of dominant isomiR share of reads')
    fig.savefig('srnabench_isomir_domperc.png')

    x = iso[iso.name.isin(iso.name[:28])]
    bins=range(15,30,1)
    ax = x.hist('length',bins=bins,ax=ax,by='name',sharex=True,alpha=0.9)
    ax[-1,-1].set_xlabel('length')
    fig.suptitle('isomiR length distributions')
    fig.savefig('srnabench_isomir_lengths.png')
    plt.close('all')

    c=iso.variant.value_counts()
    #c=c[c>10]
    fig,ax = plt.subplots(1,1,figsize=(8,8))
    c.plot(kind='pie',colormap='Spectral',ax=ax, labels=None,legend=True,
             startangle=0,pctdistance=1.1,autopct='%.1f%%',fontsize=10)
    ax.set_title('isomiR class distribution')
    plt.tight_layout()
    fig.savefig('srnabench_isomir_classes.png')

    iso.to_csv('srnabench_isomirs_all.csv',index=False)
    return top
Example #13
0
def GenomeChromosomewise(df, candSNPs=None, genes=None, axes=None,outliers=None):
    markerSize = 6
    fontsize = 6
    chrsize = df.reset_index().groupby('CHROM').POS.max()
    if axes is None:
        if chrsize.shape[0]>1:
            _, axes = plt.subplots(int(np.ceil(chrsize.shape[0] / 2.)), 2, sharey=True, dpi=200, figsize=(10, 6));
            ax = axes.reshape(-1)
        else:
            ax = [plt.subplots(1,1, sharey=True, dpi=200, figsize=(10, 6))[1]]

    for j, (chrom, a) in enumerate(df.groupby(level=0)):
        if candSNPs is not None:
            try:
                candSNPs.loc[chrom]
                for pos in candSNPs.loc[chrom].index.values:
                    ax[j].axvline(pos, color='r', linewidth=0.5, alpha=0.5)
                    ax[j].annotate(
                        '{:.0f},{:.2f}'.format(candSNPs['rank'].loc[(chrom, pos)], candSNPs.nu0.loc[(chrom, pos)]),
                        xy=(pos, a.max()), xytext=(pos, a.max()), fontsize=fontsize - 2)

            except:
                pass

        if genes is not None:
            try:
                X = genes.loc[chrom]
                if len(genes.loc[chrom].shape) == 1:
                    X = pd.DataFrame(X).T
                for _, row in X.iterrows():
                    ax[j].fill_between([row.start, row.end], a.min(), a.max(), color='r')
                    ax[j].annotate(row['name'], xy=(row.start, a.max()), xytext=(row.start, a.max()),
                                   fontsize=fontsize - 2)


            except:
                pass

        ax[j].scatter(a.loc[chrom].index, a.loc[chrom], s=markerSize, alpha=0.8, edgecolors='none')

        if outliers is not None:
            try:
                ax[j].scatter(outliers.loc[chrom].index, outliers.loc[chrom], s=markerSize, c='r', alpha=0.8, edgecolors='none')
            except:
                pass

        setSize(ax[j], fontsize)
        ax[j].set_xlim([-1000, chrsize[chrom] + 1000])
        # ax[j].set_title(chrom, fontsize=fontsize+2)
        annotate(chrom, ax=ax[j],fontsize=fontsize+4)
        ax[j].locator_params(axis='x', nbins=10)
    plt.tight_layout(pad=0.1)
    plt.gcf().subplots_adjust(bottom=0.1)
Example #14
0
def PCA(df, variable_col, sample_col, value_col, pcx=1, pcy=2, point_label=False, plot_style="ggplot", **kwargs):
    """
    Plot a the frequence of contribution to the variance of the principal components from a dataframe in “stacked” or “record” format.
    Use matplotlib PCA implementation
    *  df
        A pandas dataframe with a least the 3 following columns:
        - variable_col = column containing variable identifiers (such as gene identifiers)
        - sample_col = column containing sample identifiers (such as experimental condition)
        - value_col = column containing quantitative numeric values
    *  variable_col
        Name or index of the column containing variable identifiers
    *  sample_col
        Name or index of the column containing sample identifiers
    *  value_col
        Name or index of the column containing values
    *  pcx
        Number of the component to plot on the x axis [ DEFAULT: 1 ]
    *  pcy
        Number of the component to plot on the y axis [ DEFAULT: 2 ]
    * point_label
        If True the points will be labelled with their names
    *  plot_style
        Default plot style for pyplot ('grayscale'|'bmh'|'ggplot'|'dark_background'|'classic'|'fivethirtyeight'...)[ DEFAULT: "ggplot" ]
    *  kwargs
        Additional parameters for plot appearance derived from pylab basic plot arguments such as: title, color, alpha, fontsize, fontname, linewidths
    """
    # pivot data to reorganize df
    df = df.pivot(index=variable_col, columns=sample_col, values=value_col)
    
    # Perform PCA analysis
    pca_res = pl.mlab.PCA(df, standardize=True)
    
    # Prepare plotting area and parameters
    figsize = kwargs["figsize"] if "figsize" in kwargs else [10, 10]
    fig, ax = pl.subplots(figsize=figsize)
    pl.style.use(plot_style)
    fontsize = kwargs["fontsize"] if "fontsize" in kwargs else 10
    fontname = kwargs["fontname"] if "fontname" in kwargs else "Sans"
    color =  kwargs["color"] if "color" in kwargs else "black"
    alpha = kwargs["alpha"] if "alpha" in kwargs else 1
    linewidths = kwargs["linewidths"] if "linewidths" in kwargs else 3
    
    # Extract data and plot it
    df_res = pd.DataFrame(data=np.array(pca_res.Wt), index=pca_res.a.columns, columns=range(1, len(pca_res.a.columns)+1))
    ax.scatter(df_res[pcx], df_res[pcy], linewidths=linewidths, edgecolor=color, color=color, alpha=alpha)
    
    if point_label:
        for name, val in df_res.iterrows():
            ax.text(x=val[pcx], y=val[pcy], s=name, horizontalalignment="center", fontsize = fontsize-2, fontname=fontname)
    
    ax.set_xlabel("PC{} ({}%)".format(pcx, round(pca_res.fracs[pcx-1]*100, 2)))
    ax.set_ylabel("PC{} ({}%)".format(pcy, round(pca_res.fracs[pcy-1]*100, 2)))
    ax.set_title(kwargs["title"] if "title" in kwargs else "Principal component PC{}/PC{}".format(pcx, pcy))
    
    # Fontsize and fontname
    for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()):
        item.set_fontsize(fontsize)
        item.set_fontname(fontname)

    return (df_res)
  def plot_network_activity(self,
                            stimulus_input=None,
                            nb_stddev=1.,
                            ax_handle=None):
    '''
            Plot the activity of the network to a specific stimulus.

            TODO This really should depend on the type of the population code...
        '''

    if stimulus_input is None:
      stimulus_input = self.default_stimulus_input

    # Get the network response
    activity = self.get_network_response(stimulus_input)

    # Plot it
    if ax_handle is None:
      f, ax_handle = plt.subplots()

    ax_handle.plot(activity)
    ax_handle.set_xlabel('Neuron')
    ax_handle.set_ylabel('Activity')

    plt.show()
Example #16
0
def matshowN(array, axis=None, titles=None, matshow_kw=dict(), subplot_kw=dict(frame_on=False), **kwargs):
    ''' matshowN(array, axis=None, titles=None, subplot_kw=dict(frame_on=False), **kwargs)
        Ex: f=matshowN(angle(kum.iG.cint[:]))
            f.tight_layout()
            basic.graphics.matshowN((residual/stdpha)**2., matshow_kw={'vmin':0, 'vmax':1})
            
        axis: If not specified, uses the axis with minimum elements.
            axis=argmin(array.shape)
    '''          
    if axis is None:
      axis=np.argmin(array.shape);
    num=array.shape[axis];
    lw=int(np.ceil(np.sqrt(num)));
    f,axarr=P.subplots(lw,lw,sharex='col',sharey='row', subplot_kw=subplot_kw, **kwargs)
    array=np.rollaxis(array, axis, 0);
    k=0;
    for ax in axarr.ravel():
      if k >= num:
        ax.set_axis_off();    
        continue
      ax.matshow(array[k,:,:], **matshow_kw);
      if titles is None:
        ax.set_title(str('%d'%k));ax.set_axis_off();
      else:
        ax.set_title(titles[k]);ax.set_axis_off();
      k=k+1;
    return f;
Example #17
0
def group_plots(ylist, ncols, x = None,
		titles = None,
		suptitle = None,
		ylabels = None,
		figsize = None,
		sameyscale = True,
                order='C',
		imkw={}):
    import pylab as pl
    nrows = np.ceil(len(ylist)/float(ncols))
    figsize = ifnot(figsize, (2*ncols,2*nrows))
    fh, axs = pl.subplots(int(nrows), int(ncols),
                          sharex=True,
                          sharey=bool(sameyscale),
                          figsize=figsize)
    ymin,ymax = data_range(ylist)
    axlist = axs.ravel(order=order)
    for i,f in enumerate(ylist):
	x1 = ifnot(x, range(len(f)))
        _im = axlist[i].plot(x1,f,**imkw)
	if titles is not None:
            pl.setp(axlist[i], title = titles[i])
	if ylabels is not None:
            pl.setp(axlist[i], ylabel=ylabels[i])
    if suptitle:
        pl.suptitle(suptitle)
    return
Example #18
0
def printHeatMap(marginals, words, outFile):
    N = len(words)
    words_uni = [i.decode('UTF-8') for i in words]
    heatmap = np.zeros((N+1, N+1))
    for chart in marginals:
        heatmap[chart[0], chart[1]] = math.log(marginals[chart])
    fig, ax = plt.subplots()    
    mask = np.tri(heatmap.shape[0], k=0)
    heatmap = np.ma.array(heatmap, mask=mask)
    cmap = plt.cm.get_cmap('RdBu')
    cmap.set_bad('w')
    im = ax.pcolor(heatmap, cmap=cmap, alpha=0.8)
    font = mpl.font_manager.FontProperties(fname='/usr0/home/avneesh/spectral-scfg/data/wqy-microhei.ttf')
    ax.grid(True)
    ax.set_ylim([0,N])
    ax.invert_yaxis()
    ax.set_yticks(np.arange(heatmap.shape[1]-1)+0.5, minor=False)
    ax.set_yticklabels(words_uni, minor=False, fontproperties=font)
    ax.set_xticks(np.arange(heatmap.shape[0])+0.5, minor=True)
    ax.set_xticklabels(np.arange(heatmap.shape[0]), minor=True)
    ax.set_xticks([])
    cbar = fig.colorbar(im, use_gridspec=True)
    cbar.set_label('ln(sum)')
    ax.set_xlabel('Span End')
    ax.xaxis.set_label_position('top')
    ax.xaxis.tick_top()
    plt.ylabel('Span starting at word: ')
    plt.tight_layout()
    #ax.set_title('CKY Heat Map: Node Marginals')
    fig.savefig(outFile)    
Example #19
0
def plotPowerCLR(recompute=False):
    if recompute:
        mc = pd.read_pickle('{}ROC/{}'.format(utl.outpath, 'MarkovChain'))
        hmm = f(pd.read_pickle('{}ROC/{}'.format(utl.outpath, 'HMM')))
        a = pd.concat([mc, hmm]);
        print a
        a = a[a.index.get_level_values('coverage') != np.inf]
        df = pd.DataFrame(a.groupby(level=range(6)).apply(lambda x: x[x >= x.quantile(Qcoverage[x.name[0]])].mean()))[0]
        # df = pd.DataFrame(a.groupby(level=range(6)).apply(lambda x: x[x >= x.quantile(0.99)].mean()))
        df = getPower(df, groupbyLevels=range(4))
        df.to_pickle(utl.outpath + 'ROC/PowerCLR.df')
    else:
        df = pd.read_pickle(utl.outpath + 'ROC/PowerCLR.df')
        reload(pplt)
    info = pplt.getNameColorMarker(df)
    info.loc[info.index.get_level_values('method') == 'HMM', 'marker'] = '--o'
    info.loc[info.index.get_level_values('method') == 'MarkovChain', 'marker'] = '--s'
    info.loc[info.index.get_level_values('method') == 'HMM', 'color'] = 'r'
    info.loc[info.index.get_level_values('method') == 'MarkovChain', 'color'] = 'darkblue'
    # info.loc[info.index.get_level_values('q')==0.99,'color']='r'
    # info.loc[info.index.get_level_values('q')==1,'color']='darkblue'
    fig, axes = plt.subplots(2, 3, sharey=True, sharex=True, figsize=(6, 2.5), dpi=dpi);
    pplt.setStyle(lw=1);
    pplt.plotOnePower(df.xs(0.005, level='nu0'), info, axes[0], legendSubplot=0, ylabel='Hard');
    pplt.plotOnePower(df.xs(0.1, level='nu0'), info, axes[1], ylabel='Soft');
    [pplt.annotate('({})'.format(list('ABCDEF')[j]), ax=x, fontsize=7) for j, x in enumerate(axes.reshape(-1))]
    plt.gcf().subplots_adjust(bottom=0.15)
    pplt.savefig('powerCLR', dpi=dpi)
    plt.show()
def visData(dF="dannyFull.db"):
    f, ax = pl.subplots()
    m = visBg(ax)
    D = sq.connect(dF)
    cur = D.cursor()
    A = cur.execute(
        """select clg,clat,cluster
	from meta
	group by clg,clat,cluster"""
    )
    for w in A:
        clg = w[0]
        clat = w[1]
        x, y = m(clg, clat)
        cluster = w[2]
        ax.plot(x, y, "s", ms=10, color=pickColor(cluster))
    A = cur.execute(
        """select slg,slat
	from meta
	group by slg,slat"""
    )
    for w in A:
        slg = w[0]
        slat = w[1]
        x, y = m(slg, slat)
        ax.plot(x, y, "b*", ms=20)
    ax.plot(-float("inf"), -float("inf"), "b*", label="Servers")
    ax.plot(-float("inf"), -float("inf"), "bs", label="Clients")
    ax.legend(loc=4)
    pl.show()
Example #21
0
File: LD.py Project: airanmehr/bio
def plotScalingFactor():
    r=2*1e-8
    l = 5e4
    dpi = 300
    j = 0
    for nu0 in [0.005, 0.1]:
        for s in [0.025, 0.1]:
            t = np.arange(0, 2 * (utl.logit(0.995) - utl.logit(nu0)) / s + 1., 1)
            fig, ax = plt.subplots(2, 1, figsize=(5.5, 2.5), dpi=dpi, sharex=True);
            nu(t, s=s, nu0=nu0).plot(color='k', legend=False, ax=ax[0])
            pplt.annotate(r'$s$={}, $\nu_0=${} ({} Sweep)'.format(s, nu0, ('Soft', 'Hard')[nu0 == 0.005]), fontsize=7,
                          ax=ax[0])
            pplt.setSize(ax=ax[0], fontsize=6)
            ax[0].set_ylabel(r'$\nu_t$')
            #
            H0 = H(t[0], s=s, nu0=nu0)
            Ht = H(t, s=s, nu0=nu0)
            df = pd.DataFrame([np.log(Ht / H0), -2 * r * t * l], columns=t, index=['log(Growth)', r'log(Decay)']).T
            df['log(Growth) + log(Decay)'] = df.sum(1)
            df.plot(ax=ax[1], grid=True, linewidth=2);
            ax[1].set_xlabel('Generations');
            ax[1].set_ylabel('Log(Scaling Factor)')
            ax[1].axvline(df.iloc[1:, 2].abs().idxmin(), color='k', linestyle='--', linewidth=0.5)
            # if j != 3:
            #     ax[1].legend_.remove()
            # else:
            ax[1].legend(['log(Growth)', r'log(Decay)', 'log(Growth) + log(Decay)'], bbox_to_anchor=(1.45, .75),
                         prop={'size': 6})
            pplt.setSize(ax[1], fontsize=6)

            plt.tight_layout(pad=0.1, rect=[0, 0, 0.7, 1])
            plt.gcf().subplots_adjust(bottom=0.15)
            pplt.savefig('decayFactors{}'.format(j), dpi=dpi)
            j += 1
Example #22
0
 def plot(self, theta_a, theta_e, sigma_e, y, klic, N, T, D):
     # Set up an output figure
     fig, ax = pylab.subplots(3, 1)
     colours = ['b', 'g', 'r', 'c', 'm', 'y']
     # Plot smoothed densities
     for i in xrange(D):
         # Plot original theta values
         ax[0].plot(theta_a[:, i], ls='--', c=colours[i%len(colours)])
         # Plot data-estimated theta values
         ax[0].plot(theta_e[:,i], ls='-', c=colours[i%len(colours)])
         # Plot data-estimated confidence intervals
         ax[0].fill_between(numpy.arange(T),
             theta_e[:,i] - 2*numpy.sqrt(sigma_e[:,i,i]),
             theta_e[:,i] + 2*numpy.sqrt(sigma_e[:,i,i]),
             color=colours[i%len(colours)], alpha=.25)
         ax[1].plot(y[:,i], c=colours[i%len(colours)], ls='-')
     # Set axes labels and legends
     ax[0].set_ylabel('Theta')
     ax[0].set_title('Smoothed densities')
     ax[1].set_title('Observed pattern rates')
     ax[1].set_ylabel('Rate (patterns/second)')
     ax[2].set_title('KL Divergence')
     ax[2].set_ylabel('Bits')
     ax[2].plot(klic)
     ax[2].set_xlabel('Time (ms)')
     fig.tight_layout()
     pylab.show()
def errorVis(e, t):
    f, ax = pl.subplots(1)
    ax.boxplot(e, widths=0.15)
    ax.set_xticks([1, 2])
    ax.set_xticklabels(t)
    ax.set_ylabel("Error in km", fontsize=20)
    pl.show()
Example #24
0
def help_dcols():
	""" Show a plot demonstrating all the colours """
	from pylab import figure,clf,subplots,show
	from matplotlib.patches import Rectangle
	fig, axes = subplots(4,3)
	i=0
	print(axes)
	for axA in axes:
		for ax in axA:
			patch = Rectangle([0,0],1,1,color=cols[i])
			ax.add_patch(patch)
			ax.set_xticks([])
			ax.set_yticks([])
			ax.set_xticklabels([])
			ax.set_yticklabels([])			
			if colname[i] is not 'd_black':
				ax.text(0.2,0.4,colname[i],color='k')
			else:
				ax.text(0.2,0.4,colname[i],color='w')
			i+=1
	fig.subplots_adjust(bottom=0.25,left=0.03,right=0.98,top=0.98,wspace=0.05,hspace=0.05)
	fig.text(0.08,0.18,'To use: from durhamcolours import *')
	fig.text(0.08,0.12,'then e.g. ax.plot(x,y,colour=d_olive)')
	fig.text(0.08,0.06,"Note: no parentheses i.e. d_olive not 'd_olive'")
	
	fig.savefig('durhamcolors.png')
	fig.savefig('durhamcolors.pdf')
	
	show()
Example #25
0
    def plot_calibration(self):
        """Plots the calibration function and data
        Arguments
        ------------
            cal: calibration instance

        Returns
        ------------
            figure
            axes
            calibration data graph
            calibration function graph
        """
        cal_function = self.calibrationFunction
        bin_no = np.logspace(np.log10(self.data.bin_no.min()), np.log10(self.data.bin_no.max()), 500)
        d = cal_function(bin_no)

        f, a = plt.subplots()

        cal_data, = a.plot(self.data.d,  self.data.bin_no, 'o', label='data',)
        cal_func, = a.plot(d, bin_no, label='function')

        a.loglog()

        a.set_xlim(0.9*self.data.d.min(), 1.1*self.data.d.max())
        a.set_xlabel('Diameter (nm)')

        a.set_ylim(0.9*self.data.bin_no.min(), 1.1*self.data.bin_no.max())
        a.set_ylabel('bin number')

        a.set_title('Calibration curve')
        a.legend(loc = 2)
        return f, a, cal_data, cal_func
Example #26
0
def plotBottleneck(maxGen=None,obs=False,mean=True,color='blue'):
    exit()

    def plotOne(df, ax, method):
        m=df.mean(1)
        s=df.std(1)
        # plt.locator_params(nbins=4);
        m.plot(ax=ax, legend=False, linewidth=3, color=color)
        x=m.index.values
        m=m.values;s=s.values
        ax.fill_between(x, m - 2 * s, m + 2 * s, color=color, alpha=0.3)
        ax.set_ylabel(method.strip())
        ax.set_ylim([-0.1, ax.get_ylim()[1]])

        pplt.setSize(ax)

    dfn = \
        pd.read_pickle(path + 'nu{}.s{}.df'.format(0.005, 0.0))
    fig, ax = plt.subplots(3, 1, sharex=True, figsize=(4, 3), dpi=300)
    plotOne(dfn['tajimaD'], ax[0], "Tajima's $D$");
    plt.xlabel('Generations')
    plotOne(dfn['HAF'], ax[1], "Fay Wu's $H$");
    plt.xlabel('Generations')
    plotOne(dfn['SFSelect'], ax[2], 'SFSelect');
    plt.xlabel('Generations')
    plt.gcf().subplots_adjust(bottom=0.25)
    mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']});
    mpl.rc('text', usetex=True)
    pplt.savefig('bottleneck', 300)
    plt.show()
Example #27
0
 def plot_basis_from_samp(self, b_samp, ax=None):
   """  approximate log like is just independent poissons """
   B = self._beta_to_basis(b_samp) 
   #B /= self._dx
   #B, W = self._vec_to_basis_weights(th)
   if self._xdim==1: 
     if ax is None: 
       f = plt.figure()
       plt.plot(self._grids[0], B.T)
       plt.title('Basis')
       plt.xlabel('space')
     else: 
       ax.plot(self._grids[0], B.T)
       ax.set_title("Basis")
       ax.set_xlabel("X space")
   elif self._xdim==2:
     if ax is None:
       fig, ax = plt.subplots(self._K)
     for k in range(self._K): 
       ax[k].imshow( B[k].reshape(self._xgrid_dims).T,  
                     origin = 'lower', 
                     extent = self._xbbox[0]+self._xbbox[1] )
       ax[k].set_title("Spatial basis, $B_%d$"%k)
   else: 
     raise NotImplementedError
Example #28
0
def game():
    tick = 0
    state = [[(list(), list()) for x in range(3)] for y in range(3)]

    red_hq = HQ(TEAM.RED, 1, 0)
    blue_hq = HQ(TEAM.BLUE, 1, 2)

    state[0][1][TEAM.RED].append(red_hq)
    state[2][1][TEAM.BLUE].append(blue_hq)

    red_player_ai = RedPlayerAI(red_hq)
    blue_player_ai = BluePlayerAI(blue_hq)

    fig, (state_view, progress) = pl.subplots(2, 1)

    while True:
        if red_player_ai.hq.hp <= 0 and blue_player_ai.hq.hp <= 0:
            return RESULT.DRAW
        elif red_player_ai.hq.hp <= 0:
            return RESULT.BLUE_WIN
        elif blue_player_ai.hq.hp <= 0:
            return RESULT.RED_WIN

        print "R", red_player_ai.money, red_hq.hp
        print "B", blue_player_ai.money, blue_hq.hp
        state = red_player_ai.act(state)
        state = blue_player_ai.act(state)
        state = group_ai(state)
        state = update(state)
        red_player_ai.money += 1
        blue_player_ai.money += 1
        state = update(state)
        draw(state_view, progress, state, tick)
        tick += 1
Example #29
0
	def get_canva_genome(self) :
		# plot_canva
		figure, ax = plt.subplots()
		addValue = 0
		xticks_positions = []
		chronames = []
		for chromosome, dataPosi in sorted(self.data.iteritems(), key = lambda x : (len(x[0]), x[0])) :
			chronames.append(chromosome)
			x, y = [], []
			maxposi = 0
			for posi, snp in sorted(dataPosi.iteritems(), key = lambda x : x[0]) :
				genome_posi = posi + addValue
				snp.set_genome_posi(genome_posi)
				x.append(genome_posi)
				y.append(snp.abhet)
				maxposi = posi if posi > maxposi else posi

			ax.plot(x, y, 'o', markersize=4)
			xticks_positions.append((addValue * 2 + maxposi) / 2.0)
			addValue += maxposi

		ax.set_xlim([0, addValue])
		ax.set_xticks(xticks_positions)
		ax.set_xticklabels(chronames)
		canva = MplCanva(self, None, figure, ax)
		canva.mpl_connect('button_press_event', self.click_pressed_genome)
		self.adjust_layout()
		return canva
def plotFitResult(fit, show_legend=True, show_plots=True, save_to_file=False,
                  foldername='', filename='', filetype='png'):

    xvals = fit.xvals
    yvals = fit.yvals

    fit = fit.fitValues(xvals)

    fig, ax = plt.subplots(1)

    ax.plot(xvals, yvals, label='histogram', linewidth=3)

    for n, f in enumerate(fit):
        ax.plot(xvals, f, label='peak %i' % (n + 1), linewidth=6)

    l2 = ax.legend(loc='upper center', bbox_to_anchor=(0.7, 1.05),
                   ncol=3, fancybox=True, shadow=True)
    l2.set_visible(show_legend)

    plt.xlabel('pixel value')
    plt.ylabel('number of pixels')

    if save_to_file:
        p = PathStr(foldername).join(filename).setFiletype(filetype)
        plt.savefig(p)
        with open(PathStr(foldername).join('%s_params.csv' % filename), 'w') as f:
            f.write('#x, #y, #fit\n')
            for n, (x, y, ys) in enumerate(zip(xvals, yvals)):
                fstr = ', '.join(str(f[n]) for f in fit)
                f.write('%s, %s, %s\n' % (x, y, fstr))

    if show_plots:
        plt.show()
Example #31
0
cy, cx = np.where(img_45ghz == np.max(img_45ghz))
cy += 1
cx += 0  #Small adjustments
print(
    'Image 45 GHz --> center cy,cx: ({}, {})'.format(cy, cx) +
    ', position angle:', (90 + angle.value % 360) * u.deg)

xx = np.linspace(0, 25, 1000)
yy = xx**2 / 17

xx_, yy_ = np.dot([[np.cos(angle), np.sin(angle)],
                   [-np.sin(angle), np.cos(angle)]], [xx, yy])
xx2_, yy2_ = np.dot([[np.cos(angle), np.sin(angle)],
                     [-np.sin(angle), np.cos(angle)]], [-xx, yy])

fig, ax = pl.subplots(ncols=2, figsize=(10, 4))
ax[0].set_title('Model - 45 GHz')
ax[0].imshow(img_45ghz, origin='lower', interpolation='none')
ax[0].plot(xx_ + cx, yy_ + cy, linewidth=0.5, color='w', linestyle='--')
ax[0].plot(xx2_ + cx, yy2_ + cy, linewidth=0.5, color='w', linestyle='--')

data_on_path = img_45ghz[(yy_ + cy).astype('int'), (xx_ + cx).astype('int')]
data_on_path2 = img_45ghz[(yy2_ + cy).astype('int'), (xx2_ + cx).astype('int')]
prj_dist = (xx**2 + yy**2)**0.5

ax[1].plot(prj_dist, data_on_path, label='left arm')
ax[1].plot(prj_dist, data_on_path2, label='right arm')
core = data_on_path[:10].mean()
"""
profile = (core*(prj_dist/prj_dist[250])**-0.1 * (prj_dist >= prj_dist[250])/10.
           + core * (1-(prj_dist/prj_dist[580])**2) * (prj_dist<=prj_dist[580])
Example #32
0
def parse_sub_skyline(skyline_sub_df,
                      sample,
                      q_value=0.05,
                      isotope='light',
                      IO=True,
                      output_file=None):
    '''
    parse_sub_skyline is a helper function to parse a sample and protein filtered skyline dataframe such that a properly 
    sorted and labeled dataframe is output
    
    :param skyline_sub_df: pandas dataframe with only one sample injection and only those proteins of interest
    :param sample: string of injection sample name
    :param q_value: float with a optional q_value cutoff to filter your data (default 0.05)
    :param isotope: string for filtering output dataframe such that columns from a particular isotope type are included (default 'light')
    :param IO: bool determining if you want to display output (number of peptides, plots of filtering)
    :param output_file: string determining if you want to write the dataframe to disk (default None won't write to disk)
    
    :return: a pandas dataframe with no index, columns of peptide_modified_sequence, rt_start (seconds), rt_end (seconds), charge, 
    mz [note that this may only be approxiamate for modified peptides], protein_IDs
    '''
    if IO:
        print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
        print('reading peptides from skyline dataframe ' + sample)

    #generate pandas file, filter, parse peptides, save encyc_rts values, save charge values, calculate mz, save proteins
    filt_entries = skyline_sub_df.loc[skyline_sub_df[
        isotope + ' ' + sample + ' ' + DETECT_Q_VALUE_FIELD] < q_value]
    peptides = [i for i in filt_entries[PEPTIDE_MOD_SEQ_FIELD]]
    rts_start = [
        round(t, 2)
        for t in filt_entries[isotope + ' ' + sample + ' ' + RT_START_FIELD]
    ]
    #print(isotope+' '+sample+' '+RT_START_FIELD)
    rts_end = [
        round(t, 2)
        for t in filt_entries[isotope + ' ' + sample + ' ' + RT_END_FIELD]
    ]
    zs = [i for i in filt_entries[PEPTIDE_CHARGE_FIELD]]
    prot_names = [i for i in filt_entries[PROTEIN_FULL_NAME_FIELD]]
    mzs = [mz for mz in filt_entries[isotope + ' ' + ISOTOPE_FIND_FIELD]]

    #calc number of used peptides, and make sure all the arrays are the same size
    used_peptides = len(peptides)
    assert used_peptides == len(rts_start)
    assert used_peptides == len(rts_end)
    assert used_peptides == len(zs)
    assert used_peptides == len(prot_names)
    assert used_peptides == len(mzs)

    #make a dataframe with your newly calculated entries
    pandas_dataframe = pd.DataFrame({
        peptides[i]: {
            'peptide_modified_sequence': peptides[i],
            'rt_start': rts_start[i],
            'rt_end': rts_end[i],
            'charge': zs[i],
            'mz': mzs[i],
            'protein_IDs': prot_names[i]
        }
        for i in range(used_peptides)
    }).T
    pandas_dataframe = pandas_dataframe.sort_values(by=['rt_start'])
    # show plots for q-value criteria
    if IO:
        skyline_sub_df.rename(columns={
            isotope + ' ' + sample + ' ' + DETECT_Q_VALUE_FIELD:
            'q-value'
        },
                              inplace=True)

        print(str(skyline_sub_df.shape[0]) + ' peptides identified')
        print(str(len(peptides)) + ' peptides meet q-value criteria')
        print(
            str(skyline_sub_df.shape[0] - len(peptides)) +
            ' peptides fail q-value criteria')

        fig, ax = pylab.subplots(
        )  # create a new figure with a default 111 subplot
        ax.hist(skyline_sub_df['q-value'].dropna(), bins=100)
        ax.vlines([q_value],
                  0,
                  ax.get_ylim()[1],
                  colors='red',
                  linestyle='dashed',
                  label='q-cuttoff')
        #axins = zoomed_inset_axes(ax, 2, loc=1) # zoom-factor: 2.5, location: upper-left
        #axins.hist(skyline_sub_df['q-value'].dropna(), bins=100)
        #axins.set_xlim(0, q_value*2) # apply the x-limits
        #axins.set_ylim(0, ax.get_ylim()[1]/4) # apply the x-limits
        #axins.vlines([q_value], 0, ax.get_ylim()[1]/4, colors='red', linestyle='dashed', label='q-cuttoff')
        #mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
        ax.set_xlabel('q-value')
        ax.set_ylabel('peptides')
        pylab.show()
        print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')

    #return the sorted dataframe
    return pandas_dataframe
Example #33
0
def anaRes(boardID, runstart, runstop):

    # Read file
    a = np.loadtxt('caliboutput_b{0}_R{1}R{2}.txt'.format(
        boardID, runstart, runstop))
    att = a[0, :]
    mm = np.empty([len(att), 3])
    emm = np.empty([len(att), 3])
    for k in range(3):
        mm[:, k] = a[2 * k + 1, :]
        emm[:, k] = a[2 * k + 2, :]

    sel = np.where((att > 100) & (att < 200))  #Fit range
    a = 0.25
    b = -63.5 - 7  #2*3.3dB attenuator insertion loss
    attdB = att * a + b  # ideal: att = 0dB for att=127 on both attenuators
    vin = 0.262 / 2 * pow(
        10, attdB / 20
    )  # 10^(attdB/20) attenuation & factor 1/2 because of 3 channels split
    fig, (ax1, ax2) = pl.subplots(2, 1)
    for k in range(3):
        #pl.subplot(211)
        ax1.plot(attdB,
                 mm[:, k],
                 'o',
                 markersize=6,
                 label='Channel {0}'.format(k))
        #z = np.polyfit(att[sel],mm[sel,k][0],1)  # Linear fit
        #print 'Channel',k,', slope=',z[0],'LSB/dB Att coef'
        #yth = att*z[0]+z[1]
        #pl.plot(att,yth,'y--')
        #pl.subplot(2,1,2)
        ax2.plot(attdB,
                 emm[:, k],
                 'o',
                 markersize=6,
                 label='Channel {0}'.format(k))
    #pl.subplot(2,1,1)
    ax1.grid(True)
    #ax1.set_xlabel('$\Sigma$attenuation indexes')
    ax1.set_xlabel('Signal attenuation (dB)')
    ax1.set_ylabel('Mean output level (V)')
    ax1.legend(loc='best')
    #ax2.subplot(2,1,2)
    ax2.grid(True)
    ax2.set_xlabel('Signal attenuation (dB)')
    #ax2.set_xlabel('$\Sigma$attenuation indexes')
    ax2.set_ylabel('StdDev (V)')
    ax2.legend(loc='best')

    # Now load external calib data Inout = 66MHz+100mV sine wave + att = 30dB + splitter
    if 0:
        f = np.loadtxt('calibExt.txt')
        runex = f[:, 0]
        boardex = f[:, 1]
        selex = np.where(boardex == int(boardID))[0]
        Vinex = f[selex, 2] * 0.001  # mV==>V
        Vindaq = Vinex * pow(
            10, -30. / 20) * 0.5  # Now apply attenuation (attenuator+splitter)

        mex = np.empty([len(selex), 3])
        emex = np.empty([len(selex), 3])
        for k in range(3):
            mex[:, k] = f[selex, 3 + 2 * k]
            emex[:, k] = f[selex, 3 + 2 * k + 1]
        for k in range(3):
            z = np.polyfit(attdB[sel], mm[sel, k][0], 1)  # Linear fit
            yth = attdB * z[0] + z[1]
            #pl.subplot(2,1,1)
            fig = pl.figure(12)
            pl.errorbar(attdB,
                        mm[:, k],
                        yerr=emm[:, k],
                        lw=2,
                        label='Channel {0}'.format(k))
            pl.plot(attdB, yth, 'y')
            print('Channel', k, ', slope=', z[0], 'V/dB')
            fig3 = pl.figure(13)
            sind = k + 1
            sbp2 = pl.subplot(3, 1, sind)
            sbp2.set_xscale('log')
            pl.errorbar(vin,
                        mm[:, k],
                        yerr=emm[:, k],
                        lw=2,
                        label='Channel {0}'.format(k))
            pl.errorbar(Vindaq,
                        mex[:, k],
                        yerr=emex[:, k],
                        lw=2,
                        label='ExtSin - Channel {0}'.format(k))
            pl.grid(True)
            if k == 2:
                pl.xlabel('Signal amplitude @ channel input [Vpp]')
            pl.ylabel('Mean output level [V]')
            pl.legend(loc='best')

    pl.figure(12)
    pl.title('Board {0} R{1}-{2}'.format(boardID, runstart, runstop))
    pl.xlabel('Quartz signal attenuation [dB]')
    pl.ylabel('Mean output level [V]')
    pl.legend(loc='best')
    pl.figure(12)
    pl.title('Board {0} R{1}-{2}'.format(boardID, runstart, runstop))
    pl.show()
Example #34
0
import pylab as pl

iris = datasets.load_iris()
km = KMeans(n_clusters=3)
km.fit(iris.data)

predictions = km.predict(iris.data)

colors = cycle('rgb')
labels = ["Cluster 1", "Cluster 2", "Cluster 3"]
targets = range(len(labels))

feature_index = range(len(iris.feature_names))
feature_names = iris.feature_names
combs = combinations(feature_index, 2)

f, axarr = pl.subplots(3, 2)
axarr_flat = axarr.flat

for comb, axflat in zip(combs, axarr_flat):
    for target, color, label in zip(targets, colors, labels):
        feature_index_x = comb[0]
        feature_index_y = comb[1]
        axflat.scatter(iris.data[predictions == target, feature_index_x],
                       iris.data[predictions == target, feature_index_y],
                       c=color,
                       label=label)
        axflat.set_xlabel(feature_names[feature_index_x])
        axflat.set_ylabel(feature_names[feature_index_y])
pl.show()
Example #35
0
    drt_l_mean = np.array(drt_l_mean)
    drt_u_std = np.array(drt_u_std)
    drt_l_std = np.array(drt_l_std)
    residuals_u = (drt_u_mean - drt_in) / drt_u_std
    residuals_l = (drt_l_mean - drt_in) / drt_l_std

    ##- Setup figures
    plt.ion()
    plt.rcParams.update({'font.size': 14})
    colors = [
        '#396AB1', '#DA7C30', '#3E9651', '#CC2529', '#535154', '#6B4C9A',
        '#922428', '#948B3D'
    ]

    ##- Plot bias figure
    fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(8, 8))
    fig.subplots_adjust(hspace=0.5)
    ax[0].set_title(
        fr'$\Delta r$, 1000000 realisations, rt={rt_in}, rp={rp_in}')
    ax[0].plot(drt_in, drt_in, color=colors[4], ls=':')
    ax[0].errorbar(drt_in,
                   drt_u_mean,
                   yerr=drt_u_std,
                   color=colors[0],
                   marker='o',
                   markeredgecolor=colors[0],
                   fmt='.',
                   capsize=5,
                   elinewidth=2,
                   markeredgewidth=2,
                   label=r'Unlensed $\xi$')
Example #36
0
###############################################################################
# Plot pseudosection for 10 Hz
import reda.plotters.pseudoplots as PS
data_10hz = g.get_group(10)
fig, ax, cb = PS.plot_pseudosection_type2(data_10hz, column='r', log10=True)
fig, ax, cb = PS.plot_pseudosection_type2(data_10hz, column='rpha')

###############################################################################
# Plot pseudosections of all frequencies
import reda.plotters.pseudoplots as PS
import pylab as plt
with reda.CreateEnterDirectory('output_radic'):
    fig, axes = plt.subplots(7,
                             2,
                             figsize=(15 / 2.54, 25 / 2.54),
                             sharex=True,
                             sharey=True)
    for ax, (key, item) in zip(axes.flat, g):
        fig, ax, cb = PS.plot_pseudosection_type2(item,
                                                  ax=ax,
                                                  column='r',
                                                  log10=True)
        ax.set_title('f: {} Hz'.format(key))
    fig.subplots_adjust(
        hspace=1,
        wspace=0.5,
        right=0.9,
        top=0.95,
    )
    fig.savefig('pseudosections_radic.pdf')
Example #37
0
    def plot(self,
             offset=[0, 0, 0, 0],
             airmassfct=True,
             move_max=True,
             legend=True,
             all_on_one_axis=False,
             additional_axes=False,
             errors=False,
             rayleigh=True):
        """plots ... sorry, but this is a messi function. Things should have been done different, e.g too much data
         processing whith the data not put out ... need fixn
        Arguments
        ---------
        offset: list
        airmassfct: bool.
            If the airmass factor is included or not.
            True: naturally the air-mass factor is included in the data, so this does nothing.
            False: data is corrected to correct for the slant angle
        rayleigh: bool or the aod part of the output of miniSASP.simulate_from_size_dist_LS.
            make sure there is no airmassfkt included in this!!
        all_on_one_axis: bool or axes instance
            if True all is plotted in one axes. If axes instances this axis is used.
        """

        m_size = 5
        m_ewidht = 1.5
        l_width = 2
        gridspec_kw = {'wspace': 0.05}
        no_axes = 4
        if all_on_one_axis:
            no_axes = 1
        if additional_axes:
            no_axes = no_axes + additional_axes

        if type(all_on_one_axis).__name__ == 'AxesSubplot':
            a = all_on_one_axis
            f = a.get_figure()
        else:
            f, a = plt.subplots(1, no_axes, gridspec_kw=gridspec_kw)
        columns = [
            '460.3', '460.3 max', '550.4', '550.4 max', '671.2', '671.2 max',
            '860.7', '860.7 max'
        ]
        # peaks_max = [460.3, '460.3 max', 550.4, '550.4 max', 860.7, '860.7 max', 671.2,
        #        '671.2 max']
        if not all_on_one_axis:
            f.set_figwidth(15)
        #################
        for i in range(int(len(columns) / 2)):
            col = plt_tools.wavelength_to_rgb(columns[i * 2]) * 0.8
            intens = self.data[columns[i * 2]].dropna(
            )  # .plot(ax = a, style = 'o', label = '%s nm'%colums[i*2])
            x = intens.index.get_level_values(1)
            if type(rayleigh) == bool:
                if rayleigh:
                    rayleigh_corr = 0
            else:
                # print('mach ick')
                aodt = rayleigh[float(columns[i * 2])].loc[:, ['rayleigh']]
                intenst = intens.copy()
                intenst.index = intenst.index.droplevel(
                    ['Time', 'Sunelevation'])
                aodt_sit = pd.concat([aodt,
                                      intenst]).sort_index().interpolate()
                aodt_sit = aodt_sit.groupby(aodt_sit.index).mean().reindex(
                    intenst.index)
                rayleigh_corr = aodt_sit.rayleigh.values / np.sin(
                    intens.index.get_level_values(2))
                # return aodt

            if not airmassfct:
                amf_corr = np.sin(intens.index.get_level_values(2))
            else:
                amf_corr = 1
            if not all_on_one_axis:
                atmp = a[i]
            else:
                atmp = a

            y = (offset[i] - np.log(intens) - rayleigh_corr) * amf_corr
            g, = atmp.plot(y, x)
            g.set_label('%s nm' % columns[i * 2])
            g.set_linestyle('')
            g.set_marker('o')
            #         g = a.get_lines()[-1]
            g.set_markersize(m_size)
            g.set_markeredgewidth(m_ewidht)
            g.set_markerfacecolor('None')
            g.set_markeredgecolor(col)

            if move_max:
                #             sun_intensities.data.iloc[:,i*2+1].dropna().plot(ax = a)
                intens = self.data[columns[i * 2 + 1]].dropna(
                )  # .plot(ax = a, style = 'o', label = '%s nm'%colums[i*2])
                x = intens.index.values

                g, = a[i].plot(offset[i] - np.log(intens), x)
                #             g = a.get_lines()[-1]
                g.set_color(col)
                #             g.set_solid_joinstyle('round')
                g.set_linewidth(l_width)
                g.set_label(None)

            if i != 0 and not all_on_one_axis:
                atmp.set_yticklabels([])

            if i == 4:
                break
        if all_on_one_axis:
            a.legend()
        else:
            if legend:
                for aa in a:
                    aa.legend()
        if not airmassfct:
            txt = 'OD'
        else:
            txt = 'OD * (air-mass factor)'
        if all_on_one_axis:
            atmp = a
        else:
            atmp = a[0]
        atmp.set_xlabel(txt)
        if not all_on_one_axis:
            atmp.xaxis.set_label_coords(2.05, -0.07)
        atmp.set_ylabel('Altitude (m)')
        return a
man.parman.modify_polygon(pid_mag, polygon, 3)

fig, ax = man.show_parset(pid_mag)
fig.savefig('model_magnitude.jpg')
fig, ax = man.show_parset(pid_pha)
fig.savefig('model_phase.jpg')

man.configs.gen_dipole_dipole(skipc=0)
man.configs.gen_dipole_dipole(skipc=1)
man.configs.gen_dipole_dipole(skipc=2)

import pylab as plt
# conduct forward modeling
rmag_rpha_mod = man.measurements()

fig, axes = plt.subplots(2, 1)
ax = axes[0]
ax.hist(rmag_rpha_mod[:, 0], 100)
ax.set_xlabel('magnitudes')
ax = axes[1]
ax.hist(rmag_rpha_mod[:, 1], 100)
ax.set_xlabel('phases')
fig.savefig('modeled_data.jpg')

# now add syntehtic noise
# TODO

man.save_measurements('volt.dat')
tdman = crtomo.tdMan(grid=grid)
tdman.read_voltages('volt.dat')
tdman.invert()
Example #39
0
v_k = -100
v_na = 50
v_l = -67
i_ext = 0.3
t_final = 300
dt = 0.02

#  AMPA-like synaptic input pulse
tau_r = 0.5
tau_peak = 0.5
tau_d = 2.0
g_syn = 12

if __name__ == "__main__":

    fig, ax = plt.subplots(2, figsize=(7, 5), sharex=True)

    tau_d_q = tau_d_q_function(tau_d, tau_r, tau_peak, dt)
    x0 = initial_condition(-77.71)
    t = np.arange(0, t_final, dt)
    sol = rk4_integrator(x0, ode, t, dt)
    v = sol[:, 0]

    ax[0].plot(t, v, lw=3, c="b", alpha=0.5)
    ax[1].plot(t, v, lw=3, c="b", alpha=0.5)
    ax[0].set_xlim(min(t), max(t))
    ax[0].set_ylim(-100, 50)
    ax[1].set_xlabel("time [ms]")
    ax[0].set_ylabel("v [mV]")
    ax[0].set_yticks(range(-100, 100, 50))
    for i in range(2):
o_masl_v = obsgrid_nc.variables['ZS']

o_ta_v = obsgrid_nc.variables['Tair']
o_rr_v = obsgrid_nc.variables['Rainf']
o_sf_v = obsgrid_nc.variables['Snowf']
o_time_v = obsgrid_nc.variables['time']

o_t = o_time_v[:]  #netCDF4.num2date(o_time_v[:], o_time_v.units)
o_ta = o_ta_v[:, N]
o_rr = o_rr_v[:, N]
o_sf = o_sf_v[:, N]

#t_width = datetime.timedelta(minutes=30)
width = 0.25

f, axarr = plt.subplots(3, sharex=True)
plt.hold(True)
axarr[0].axhline(273.65, color='k', linestyle="--")
axarr[0].plot(o_t, o_ta, color='r')
axarr[0].plot(a_t, a_ta, color='b')
axarr[0].set_title("OBSGRID coords: {0}, {1}\nAROME coords: {2}, {3}".format(
    o_lat_v[N], o_lon_v[N], a_lat_v[N], a_lon_v[N]))
axarr[0].set_ylabel("Temperature")

axarr[1].bar(o_t + width, o_rr, width=width, color='r')
axarr[1].bar(a_t, a_rr, width=width, color='b')
axarr[1].set_ylabel("Rainfall rate")

axarr[2].bar(o_t + width, o_sf, width=width, color='r')
axarr[2].bar(a_t, a_sf, width=width, color='b')
axarr[2].set_ylabel("Snowfall rate")
Example #41
0
# In[ ]:

# Instantiate solver
solver = TSPSolver.from_data(cities.X, cities.Y, norm="EUC_2D")

t = time.time()
tour_data = solver.solve(
    time_bound=60.0, verbose=True, random_seed=42
)  # solve() doesn't seem to respect time_bound for certain values?
print(time.time() - t)
print(tour_data.found_tour)

# In[ ]:

pd.DataFrame({
    'Path': np.append(tour_data.tour, [0])
}).to_csv('submission.csv', index=False)

# In[ ]:

# Plot tour
lines = [[(cities.X[tour_data.tour[i]], cities.Y[tour_data.tour[i]]),
          (cities.X[tour_data.tour[i + 1]], cities.Y[tour_data.tour[i + 1]])]
         for i in range(0,
                        len(cities) - 1)]
lc = mc.LineCollection(lines, linewidths=2)
fig, ax = pl.subplots(figsize=(20, 20))
ax.set_aspect('equal')
ax.add_collection(lc)
ax.autoscale()
Example #42
0
output = signal.convolve(sig.prueba_Dataset, lowpass_coef, mode='same')
###Filtro pasa banda###
f1, f2 = 8, 19
bandpass_coef = signal.firwin(19, [f1, f2],
                              pass_zero=False,
                              nyq=100,
                              window='nuttall')  #pasa banda
output_pb = signal.convolve(sig.prueba_Dataset, bandpass_coef, mode='same')

###Filtro rechaza banda###
f1, f2 = 8, 19
bandpass_coef = signal.firwin(19, [f1, f2],
                              pass_zero=True,
                              nyq=100,
                              window='nuttall')  #pasa banda
output_rb = signal.convolve(sig.prueba_Dataset, bandpass_coef, mode='same')

##Graficas filtros
f, plt_arr = plt.subplots(5, sharex=True)
plt_arr[0].plot(sig.prueba_Dataset, color='blue')
plt_arr[0].set_title('señal de entrada', color='blue')
plt_arr[1].plot(output, color='red')
plt_arr[1].set_title('señal filtrada pasa bajos', color='red')
plt_arr[2].plot(output_pa, color='green')
plt_arr[2].set_title('señal filtrada pasa altos', color='green')
plt_arr[3].plot(output_pb, color='black')
plt_arr[3].set_title('Señal filtrada pasa banda', color='black')
plt_arr[4].plot(output_rb, color='navy')
plt_arr[4].set_title('Señal filtrada rechaza banda', color='navy')
plt.show()
Example #43
0
def dovis(my_data, n):

    pylab.clf()

    pylab.rc("font", size=10)

    u = my_data.get_var("x-velocity")
    v = my_data.get_var("y-velocity")

    myg = my_data.grid

    fig, axes = pylab.subplots(nrows=2, ncols=2, num=1)
    pylab.subplots_adjust(hspace=0.25)

    # x-velocity
    ax = axes.flat[0]

    img = ax.imshow(numpy.transpose(u[myg.ilo:myg.ihi + 1,
                                      myg.jlo:myg.jhi + 1]),
                    interpolation="nearest",
                    origin="lower",
                    extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax])

    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_title("x-velocity")

    pylab.colorbar(img, ax=ax)

    # y-velocity
    ax = axes.flat[1]

    img = ax.imshow(numpy.transpose(v[myg.ilo:myg.ihi + 1,
                                      myg.jlo:myg.jhi + 1]),
                    interpolation="nearest",
                    origin="lower",
                    extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax])

    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_title("y-velocity")

    pylab.colorbar(img, ax=ax)

    # vorticity
    ax = axes.flat[2]

    vort = myg.scratch_array()
    vort[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
        0.5*(v[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
             v[myg.ilo-1:myg.ihi,myg.jlo:myg.jhi+1])/myg.dx - \
         0.5*(u[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
              u[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi])/myg.dy

    img = ax.imshow(numpy.transpose(vort[myg.ilo:myg.ihi + 1,
                                         myg.jlo:myg.jhi + 1]),
                    interpolation="nearest",
                    origin="lower",
                    extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax])

    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_title(r"$\nabla \times U$")

    pylab.colorbar(img, ax=ax)

    # div U
    ax = axes.flat[3]

    divU = myg.scratch_array()

    divU[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
        0.5*(u[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
             u[myg.ilo-1:myg.ihi,myg.jlo:myg.jhi+1])/myg.dx + \
        0.5*(v[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
             v[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi])/myg.dy

    img = ax.imshow(numpy.transpose(divU[myg.ilo:myg.ihi + 1,
                                         myg.jlo:myg.jhi + 1]),
                    interpolation="nearest",
                    origin="lower",
                    extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax])

    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_title(r"$\nabla \cdot U$")

    pylab.colorbar(img, ax=ax)

    pylab.figtext(0.05, 0.0125, "t = %10.5f" % my_data.t)

    pylab.draw()
Example #44
0
    def plot_torque_speed_curve(self,
                                highlight_power=None,
                                highlight_torque=None,
                                highlight_rpm=None,
                                embed=False,
                                full_title=True):
        x = np.linspace(self.min_rpm, self.max_rpm / self.gear_ratio,
                        100)  # * ureg.tpm
        # y1 = np.vectorize(m.torque_continuous)(x)
        # y2 = np.vectorize(m.torque_intermittent)(x)
        # y1 = [m.torque_continuous(x_) for x_ in x]
        # y2 = [m.torque_intermittent(x_) for x_ in x]
        y1 = np.array([
            self.torque_continuous(x_).to(ureg.newton * ureg.meter).magnitude
            for x_ in x
        ]) * (ureg.newton * ureg.meter)
        y2 = np.array([
            self.torque_intermittent(x_).to(ureg.newton * ureg.meter).magnitude
            for x_ in x
        ]) * (ureg.newton * ureg.meter)

        fig, ax1 = pylab.subplots()

        if full_title:
            ax1.set_title(self.name + " Torque, Power vs. Speed", fontsize=16.)
        else:
            ax1.set_title("Torque, Power vs. Speed", fontsize=16.)

        ax1.set_xlabel("Speed [RPM]", fontsize=12)
        ax1.set_ylabel("Torque [N m]", fontsize=12)
        xmin = x[0].magnitude
        xmax = x[-1].magnitude
        if highlight_rpm is not None:
            xmin = min(xmin, highlight_rpm.m * 0.9)
        if highlight_rpm is not None:
            xmax = max(xmax, highlight_rpm.m * 1.1)
        ax1.set_xlim([xmin, xmax])

        ax2 = ax1.twinx()
        ax2.set_ylabel("Power [W]", fontsize=12)

        colors = [
            '#aa0000ee', '#00aa00ee', '#ff0000ee', '#00ff00ee', '#aaaa00ee',
            '#aa00aaee', '#00aaaaee'
        ]

        lns = []
        lns2 = []

        lns += ax1.plot(x.magnitude,
                        y1.magnitude,
                        color=colors[0],
                        label='Continuous T')
        lns2 += ax2.plot(x.magnitude, (y1 * x / 9.5488).magnitude,
                         color=colors[1],
                         label='Continuous P')

        if self.torque_intermittent_define:
            lns += ax1.plot(x.magnitude,
                            y2.magnitude,
                            color=colors[2],
                            label='Intermittent T')
            lns2 += ax2.plot(x.magnitude, (y2 * x / 9.5488).magnitude,
                             color=colors[3],
                             label='Intermittent P')

        #if highlight_power is not None:
        #    lns += [ax2.axhline(highlight_power.to('watt').magnitude, color=colors[4], label='Requested P')]


#
#        if highlight_torque is not None:
#            lns += [ax2.axhline(highlight_rpm.magnitude, color=colors[5], label='Requested T')]
#
#        if highlight_rpm is not None:
#            lns += [ax2.axvline(highlight_rpm.magnitude, color=colors[6], label='Requested RPM')]

        if highlight_rpm is not None and highlight_power is not None:
            ax2.scatter([highlight_rpm.magnitude],
                        [highlight_power.to('watt').magnitude],
                        label='Requested RPM,P')
            ax2.scatter([highlight_rpm.magnitude * .90],
                        [highlight_power.to('watt').magnitude * .90],
                        label='90% Requested RPM,P')
            ax2.scatter([highlight_rpm.magnitude * 1.10],
                        [highlight_power.to('watt').magnitude * 1.10],
                        label='110% Requested RPM,P')

        ax1.set_ylim(bottom=0)
        ax2.set_ylim(bottom=0)

        # labs = [l.get_label() for l in lns]
        # ax1.legend(lns, labs, loc='upper left')
        # labs = [l.get_label() for l in lns]
        ax1.legend(loc='upper left')
        ax2.legend(loc='upper right')

        fig.tight_layout()
        if not embed:
            pylab.show()
            pylab.close()
            return None
        else:
            import io
            pylab.show()
            imgdata = io.BytesIO()
            pylab.savefig(imgdata, format='png', bbox_inches='tight')
            imgdata.seek(0)
            img_str = imgdata.getvalue()
            pylab.close()
            return img_str
Example #45
0
for i in range(len(P)):
    iself = int(ids[i])
    corr = 0.
    im1 = iself - 1
    if im1 < 0:
        im1 = len(P) - 1
        corr = 1.
    ip1 = iself + 1
    if ip1 == len(P):
        ip1 = 0
        corr = 1.
    idxp1 = ids_reverse[ip1]
    idxm1 = ids_reverse[im1]
    gradP[i] = (P[idxp1] - P[idxm1]) / (coords[idxp1, 0] - coords[idxm1, 0])

fig, ax = pl.subplots(2, 2, sharex=True)

ax[0][0].plot(coords[:, 0], rho, ".", label="gizmo-mfm")
ax[0][0].plot(x, rho_x, "-", label="stable solution")
ax[0][0].set_ylabel("$\\rho{}$ (code units)")
ax[0][0].legend(loc="best")

ax[0][1].plot(x, a, "-", label="$\\nabla{}\\Phi{}$ external")
ax[0][1].plot(coords[:, 0],
              gradP / rho,
              ".",
              label="$\\nabla{}P/\\rho{}$ gizmo-mfm")
ax[0][1].set_ylabel("force (code units)")
ax[0][1].legend(loc="best")

ax[1][0].axhline(y=uconst, label="isothermal $u$", color="k", linestyle="--")
Example #46
0
import os
#from gatspy.periodic import LombScargleFast
import json
s = json.load(open("fbb_matplotlibrc.json"))
pl.rcParams.update(s)

impath = os.getenv("UIdata") + "/groundtest1/ESB_s119.75*"
img = glob.glob(impath + "*0000.raw")[0]

nrow, ncol = 1530, 2444
#= findsize(img)
nband = 3
print(nrow, ncol)

print(impath)
flist = glob.glob(impath + "*213949*.raw")
print(len(flist))
flist11975 = np.array(flist)[np.argsort(flist)]
print(len(flist))
#print (flist11975)
for i, f in enumerate(flist11975[50:150]):

    fig, axs = pl.subplots(1, 1, figsize=(15, 15))
    axs.axis('off')

    axs.imshow(np.fromfile(f, dtype=np.uint8).reshape(nrow, ncol, nband),
               interpolation='none')
    axs.set_xlim(0, axs.get_xlim()[1])
    axs.set_ylim(axs.get_ylim()[0], 0)
    pl.savefig("%s_%02d.png" % (impath[:-1], i), bbox_inches='tight')
Example #47
0
def do_solve(maxIter, solver, display, test_interval, test_iters):

    # SET PLOTS DATA
    train_loss_C = zeros(maxIter / display)
    train_top1 = zeros(maxIter / display)
    # train_top5 = zeros(maxIter/display)

    val_loss_C = zeros(maxIter / test_interval)
    val_top1 = zeros(maxIter / test_interval)
    # val_top5 = zeros(maxIter/test_interval)

    it_axes = (arange(maxIter) * display) + display
    it_val_axes = (arange(maxIter) * test_interval) + test_interval

    _, ax1 = subplots()
    ax2 = ax1.twinx()
    ax1.set_xlabel('iteration')
    ax1.set_ylabel('train loss C (r), val loss C (y)')
    ax2.set_ylabel(
        'train TOP1 (b), val TOP1 (g)')  # train TOP-5 (c), val TOP-5 (k)')
    ax2.set_autoscaley_on(False)
    ax2.set_ylim([0, 1])

    lossC = np.zeros(maxIter)
    acc1 = np.zeros(maxIter)
    acc5 = np.zeros(maxIter)

    #RUN TRAINING
    for it in range(niter):
        #st = time.time()
        solver.step(1)  # run a single SGD step in Caffepy()
        #en = time.time()
        #print "Time step: " + str((en-st))

        #PLOT
        if it % display == 0 or it + 1 == niter:
            lossC[it] = solver.net.blobs['loss3/loss3'].data.copy()
            acc1[it] = solver.net.blobs['loss3/top-1'].data.copy()
            # acc5[it] = solver.net.blobs['loss3/top-5'].data.copy()

            loss_disp = 'loss3C= ' + str(lossC[it]) + '  top-1= ' + str(
                acc1[it])

            print '%3d) %s' % (it, loss_disp)

            train_loss_C[it / display] = lossC[it]
            train_top1[it / display] = acc1[it]
            # train_top5[it / display] = acc5[it]

            ax1.plot(it_axes[0:it / display], train_loss_C[0:it / display],
                     'r')
            ax2.plot(it_axes[0:it / display], train_top1[0:it / display], 'b')
            # ax2.plot(it_axes[0:it / display], train_top5[0:it / display], 'c')

            #ax1.set_ylim([0, 10])
            plt.title(training_id)
            plt.ion()
            plt.grid(True)
            plt.show()
            plt.pause(0.001)

        #VALIDATE
        if it % test_interval == 0 and it > 0:
            loss_val_C = 0
            top1_val = 0
            # top5_val = 0
            for i in range(test_iters):
                solver.test_nets[0].forward()
                loss_val_C += solver.test_nets[0].blobs['loss3/loss3'].data
                top1_val += solver.test_nets[0].blobs['loss3/top-1'].data
                # top5_val += solver.test_nets[0].blobs['loss3/top-5'].data

            loss_val_C /= test_iters
            top1_val /= test_iters
            # top5_val /= test_iters

            print("Val loss C: {:.3f}".format(loss_val_C))

            val_loss_C[it / test_interval - 1] = loss_val_C
            val_top1[it / test_interval - 1] = top1_val
            # val_top5[it / test_interval - 1] = top5_val

            ax1.plot(it_val_axes[0:it / test_interval],
                     val_loss_C[0:it / test_interval], 'y')
            ax2.plot(it_val_axes[0:it / test_interval],
                     val_top1[0:it / test_interval], 'g')
            # ax2.plot(it_val_axes[0:it / test_interval], val_top5[0:it / test_interval], 'k')

            #ax1.set_ylim([0, 10])
            plt.title(training_id)
            plt.ion()
            plt.grid(True)
            plt.show()
            plt.pause(0.001)
            title = '../../../datasets/EmotionDataset/models/training/' + training_id + str(
                it) + '.png'  # Save graph to disk
            savefig(title, bbox_inches='tight')

    return
Example #48
0
import statsmodels.formula.api as smf

mod = smf.ols(formula='starbucks_count ~ new_homes + hotels + HC03_VC88', data=df)
res = mod.fit()
print res.summary()

############################## False positives for logit regression

logit = sm.Logit(sample['has_starbucks'], sample[['new_homes','hotels','HC03_VC88','shopping_centers']])
# fit the model
result = logit.fit()
print result.summary()

sample['predict'] = result.predict(sample[['new_homes','hotels','HC03_VC88','shopping_centers']])
predict = sample[['new_homes','hotels','HC03_VC88', 'HC03_VC96', 'shopping_centers', 'has_starbucks', 'predict', 'zip']]
predict[(predict.has_starbucks==0)&(predict.predict>0.55)].sort_values(['predict'], ascending=[0])

## Correlation matrix

import seaborn as sns
corr = df[['sq_miles', u'HC03_VC96', 'HC03_VC88', 
           'shopping_centers', 'new_homes', 'hotels', 
           'population_density', 'starbucks_count', 
           'airport_related', 'colleges', 'HC01_VC118', 
           u'HC01_VC03', u'HC01_VC103',]].corr()

plt.subplots(figsize=(13,10)) 
sns.heatmap(corr, 
            xticklabels=corr.columns.values,
            yticklabels=corr.columns.values)
	"GNU GPL*": 10,
	"ISC": 2,
	"MIT*": 215,
	"MIT NoPerm": 4,
	"MIT-variant": 9,
	"custom C": 2,
	"other (long)": 42,
	"other (short)": 82
}
pcts = collections.OrderedDict(sorted(pcts.items()))

from cycler import cycler
colors = pylab.cm.gray(np.linspace(0.2,0.8,5))
pylab.rcParams['axes.prop_cycle'] = cycler(color=colors)

fig, ax = pylab.subplots(figsize=(6, 4.5), subplot_kw=dict(aspect="equal"))

def labelling(val):
	a = int(val / 100 * sum(list(pcts.values())))
	if val > 4:
		lbl = "{:d}\n[{:.1f}%]".format(a, val)
	elif a >= 3:
		lbl = "{:d}".format(a)
	else:
		lbl = ""
	print(val, a, lbl)
	return lbl

wedges, texts, lbls = ax.pie(list(pcts.values()), wedgeprops=dict(width=0.5), startangle=30, autopct=labelling, pctdistance=0.75)

bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
import numpy as np
import matplotlib

matplotlib.use("TkAgg")  # This program works with Qt only


def exp_sin(x, A, f, z, p):
    return A * np.sin(2 * np.pi * f * x + p) * np.exp(z * x)


import pylab as pl

fig, ax = pl.subplots()

x = np.linspace(1e-6, 1, 500)
pars = {"A": 1.0, "f": 2, "z": -0.2, "p": 0}
y = exp_sin(x, **pars)

line, = pl.plot(x, y)


def update(**kw):
    y = exp_sin(x, **kw)
    line.set_data(x, y)
    ax.relim()
    ax.autoscale_view()
    fig.canvas.draw_idle()


from scpy2.matplotlib.gui_panel import TkSliderPanel
Example #51
0
#fwd = mne.make_forward_solution(info, trans=trans, src=src, bem=bem, fname='fsaverage-fwd.fif', meg=False, eeg=True, mindist=5.)
fwd = mne.read_forward_solution(
    r'C:\Users\nsmetanin\PycharmProjects\nfb\tests\sloreta\fsaverage-fwd-1005-2.fif',
    surf_ori=True)

# inverse
from mne.minimum_norm.inverse import _assemble_kernel
inv = mne.minimum_norm.make_inverse_operator(info,
                                             fwd,
                                             noise_cov,
                                             loose=0.2,
                                             depth=0.8,
                                             fixed=True)

lambdas = [1000, 100, 10, 1, 0.1, 0.05, 0.01, 0.005, 0.001, 0.0001]
f, axes = plt.subplots(2, len(lambdas))
f.set_size_inches(10, 3.5)
label_name = [
    'caudalanteriorcingulate', 'lateraloccipital', 'posteriorcingulate'
][0]
for j, lambda2 in enumerate(lambdas):
    for k, add_label in enumerate(['lh', 'rh']):
        # setup roi
        area = True
        labels = mne.read_labels_from_annot('fsaverage', parc='aparc')
        print([label.name for label in labels])
        roi_label = labels[[label.name for label in labels
                            ].index('{}-{}'.format(label_name, add_label))]
        arg = None

        # prepare inv
Example #52
0
File: plot.py Project: jcoady/PIC
import pylab as pl
import numpy as np
import csv

with open('results.csv', 'r') as f:
    reader = csv.reader(f)
    entries = list(reader)

#convert data to float, skip first line which contains headers
data = [[float(c) for c in r] for r in entries[1:]]

#transpose into a numpy array
data = np.transpose(np.array(data))

#create a new figure
fig, ax1 = pl.subplots(figsize=(7, 4))
ax2 = ax1.twinx()

#plot column 0 (x) vs. column 1 (phi)
ax1.plot(data[0],
         data[1],
         lineWidth=2,
         color='black',
         marker='o',
         markerFaceColor='white',
         markerSize=8)

ax2.plot(data[0], data[3], '--', lineWidth=2, color='gray')

ax1.set_xlabel('x (m)')
ax1.set_ylabel('phi (V)')
Example #53
0
# lagged mutual information
T_L = np.append(T_R, np.arange(76,300)*0.001)
T_L_ms = T_L * 1000
lagged_MI = np.load('{}/analysis/{}/lagged_MI.npy'.format(CODE_DIR, recorded_system))
tau_L = plots.get_T_avg(T_L_ms, lagged_MI, T_0_ms)

"""Plotting"""
rc('text', usetex=True)
matplotlib.rcParams['font.size'] = '15.0'
matplotlib.rcParams['xtick.labelsize'] = '15'
matplotlib.rcParams['ytick.labelsize'] = '15'
matplotlib.rcParams['legend.fontsize'] = '15'
matplotlib.rcParams['axes.linewidth'] = 0.6

fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(nrows= 2, ncols = 2 , figsize=(5.8, 3.7))

# Colors
main_red = sns.color_palette("RdBu_r", 15)[12]
main_blue = sns.color_palette("RdBu_r", 15)[1]
soft_red = sns.color_palette("RdBu_r", 15)[10]
soft_blue = sns.color_palette("RdBu_r", 15)[4]
violet = sns.cubehelix_palette(8)[4]
green = sns.cubehelix_palette(8, start=.5, rot=-.75)[3]

##########################################
########## Simulated Conventional ########
##########################################

##### x-axis ####
# unset borders
Example #54
0
        rows = {
            key: uvtbl[(uvtbl['region'] == key) & (uvtbl['band'] == band)]
            for key in sorted(uvtbl['region'])
        }
        stats = [{
            "label": key,
            "med": row['50%'][0],
            "q1": row['25%'][0],
            "q3": row['75%'][0],
            "whislo": row['5%'][0],
            "whishi": row['95%'][0],
            "fliers": [],
        } for key, row in rows.items() if len(row) > 0][::-1]

        fig, axes = pl.subplots(nrows=1,
                                ncols=1,
                                figsize=(12, 12),
                                sharey=True)
        axes.bxp(stats, vert=False)
        #axes.set_title(f'{band} UV distribution overview', fontsize=fontsize)
        axes.set_xlabel("Angular Scale (\")", fontsize=fontsize)
        axes.set_xlim(0.1, 15)
        rad_to_as = u.radian.to(u.arcsec)

        def fcn(x):
            return rad_to_as / x / 1000

        ax1t = axes.secondary_xaxis('top', functions=(fcn, fcn))
        ax1t.set_xlabel("Baseline Length (k$\lambda$)")
        ax1t.set_ticks([15, 20, 30, 50, 100, 400])
        savefig(
            f'/orange/adamginsburg/ALMA_IMF/2017.1.01355.L/paper_figures/uvhistograms/{band}_summary_uvdistribution.pdf',
Example #55
0
    E[i] = [float(v) for v in output.split()]
    print("%3d  %.6e %.6e" % (n, E[i, 0], E[i, 1]))

for s in range(h.size - 1):

    print("rate = %.2f  %.2f" %
          (np.polyfit(np.log10(h[s:]), np.log10(E[s:, 0]), 1)[0],
           np.polyfit(np.log10(h[s:]), np.log10(E[s:, 1]), 1)[0]))

pad = 0.05
lbl = 0.19
dh = np.log10(h[0]) - np.log10(h[-1])
dh *= (1. + 2 * pad + lbl)
hL = 10**(np.log10(h[-1]) - pad * dh)
hR = 10**(np.log10(h[0]) + (pad + lbl) * dh)
h0 = 10**(np.log10(h[0]) + 0.02 * dh)

fig, ax = plt.subplots(tight_layout=True)
ax.loglog(h, E[:, 0], '-o', ms=4)
ax.loglog(h, E[:, 1], '-^', ms=4)
tx, ty = _buildTriangle(2.0, 0.05, h, E[:, 0])
ax.loglog(tx, ty, '-k')
ax.text(tx[1], ty[1], "2 ", ha="right", va="bottom")
ax.text(h0, E[0, 0], r"$|||p-p_h|||$")
ax.text(h0, E[0, 1], r"$|||\mathbf{u}-\mathbf{u}_h|||$")
ax.set_xlim(hL, hR)
ax.set_xlabel("Mesh size, $h$")
ax.set_ylabel("Norm of Error")
fig.savefig("convergence_%d_%d%s.png" %
            (d, args.problem, "_x" if args.perturb else ""))
Example #56
0
        * ptime[iproc][3]
        / (ptime[iproc][2] - ptime[iproc][1])
    )
    tconv[iproc] = 1.0 / (tmax[iproc] - tmin[iproc])
    tconv_in_s[iproc] = (
        (tmax[iproc] - tmin[iproc])
        * ptime[iproc][3]
        / (ptime[iproc][2] - ptime[iproc][1])
    )

ttot_min = tmin_in_s.min()
ttot_max = tmax_in_s.max()

## make the plot

fig, ax = pl.subplots(1, 1, sharex=True)

ax.set_xlim(
    ttot_min - 0.05 * (ttot_max - ttot_min),
    ttot_max + 0.05 * (ttot_max - ttot_min),
)
ax.axvline(x=ttot_min, linestyle="--", color="k", linewidth=0.8)
ax.axvline(x=ttot_max, linestyle="--", color="k", linewidth=0.8)

# first plot the MPI communications (if requested)
if args.messagefile:
    mname = args.messagefile

    # load the MPI communication data dump
    mdata = np.loadtxt(mname)
Example #57
0
    tools.showProbeSignals(probes, -1.1, 1.1, dt)

    # Исключение отраженных волн из датчика падающей волны с помощью временного окна
    # Временное окно равно длительности импульса + времени, необходимому волне для прохождения
    # расстояния от левого края до диэлектрика (в вакууме)
    probes[1].E[int(T + lay_st * dx / (300000000 * dt)):] = 0

    # Формирование спектра падающей и отраженной волн
    spectr1 = tools.Spectrum(probes[0].E, dt, 30e9)
    spectr1.fourierTransform()
    spectr2 = tools.Spectrum(probes[1].E, dt, 30e9)
    spectr2.fourierTransform()

    # Вывод спектров падающей и отраженной волн, нормированных к максимуму спектра падающей волны
    max_Norm = max(spectr2.PF)
    fig, ax = pylab.subplots()
    ax.plot(spectr1.f, spectr1.PF / max_Norm)
    ax.plot(spectr1.f, spectr2.PF / max_Norm)
    ax.set_xlim(0, spectr1.xMax)
    ax.set_xlabel('f, Гц')
    ax.set_ylabel(r'|$\frac{P}{P_{max}}$|')
    ax.grid()
    pylab.show()

    # Вывод модуля коэффициента отражения в полосе частот Fmin, Fmax
    Fmin = 5e9
    Fmax = 15e9
    fig, ax = pylab.subplots()
    ax.plot(spectr1.f, spectr1.PF / spectr2.PF)
    ax.set_xlim(Fmin, Fmax)
    ax.set_ylim(0, 1)
                    ignore_index=True)

import pylab as plt

cm = dict(
    zip(['FB0', 'FB250', 'FB500', 'FBMock'],
        ['#3CB4E8', '#438BA8', '#002A3B', '#FE4A49']))

for comp_with in ['FB0', 'FB250', 'FB500', 'FBMock']:

    contrasts = [
        '{} - {}'.format(fb_type, comp_with)
        for fb_type in ['FB0', 'FB250', 'FB500', 'FBMock']
        if fb_type != comp_with
    ]
    fig, axes = plt.subplots(1, 4, sharey='all', sharex='all', figsize=(8, 3))

    plt.suptitle('Comparison with {} condition'.format(comp_with))
    plt.subplots_adjust(wspace=0, right=0.84, bottom=0.2, top=0.8)
    for j_metric_type, metric_type in enumerate(
        ['magnitude', 'n_spindles', 'amplitude', 'duration']):
        axes[j_metric_type].set_title(r'{}, log($\beta_1$)'.format(
            metric_type) if (LOG and 'ude' in metric_type
                             ) else r'{}, $\beta_1$'.format(metric_type))
        for contrast in contrasts:
            fb1_type, fb2_type = contrast.split(' - ')
            data = t_stat_df.query('Contrast=="{}" & metric_type=="{}"'.format(
                contrast, metric_type)).copy()
            if len(data) == 0:
                data = t_stat_df.query(
                    'Contrast=="{}" & metric_type=="{}"'.format(
Example #59
0
    layer = L1Norm_layer(input_shape=inpt.shape)

    # FORWARD

    layer.forward(inpt)
    forward_out = layer.output
    print(layer)

    # BACKWARD

    delta = np.zeros(shape=inpt.shape, dtype=float)
    layer.backward(delta, copy=True)

    # Visualizations

    fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(10, 5))
    fig.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.15)

    fig.suptitle('L1Normalization Layer')

    ax1.imshow(float_2_img(inpt[0]))
    ax1.set_title('Original image')
    ax1.axis('off')

    ax2.imshow(float_2_img(forward_out[0]))
    ax2.set_title("Forward")
    ax2.axis("off")

    ax3.imshow(float_2_img(delta[0]))
    ax3.set_title('Backward')
    ax3.axis('off')
Example #60
0
def scan_eddym(data,
               lon,
               lat,
               levels,
               date,
               areamap,
               mask='',
               destdir='',
               physics='',
               eddycenter='masscenter',
               maskopt='contour',
               preferences=None,
               mode='gaussian',
               basemap=False,
               checkgauss=True,
               areaparms=None,
               usefullfit=False,
               diagnostics=False,
               plotdata=False,
               debug=False):
    '''scan_eddym wraps the identification and Gaussian fit functions.

    Function to identify each eddy using closed contours,
    also this function checks if the elipse adjusted have
    a consistent eccentricity, vorticty and other parameters.

    Parameters
    ----------
    args:
        data: array
            Sea Surface Height in cm.
        lon: array|list
            Longitude of data field.
        lat: array|list 
            Latitude of data field.
        levels: array|list
            Discrete levels where the code will find the closed contours.
        date: int | datetime 
            Date in julian days.
        areamap: list
            Section of interest [lon0,lon1,lat0,lat1].
        mask: np.mask 
            Continent mask.
    
    Returns
    -------
    eddys: dict
        OrderedDict of identified eddies.
    check: boolean
        Status of identification.
    total_contours: int
        Total number of contours analysed.

    Example
    -------
    data: 
        Read netCDF4 data with mask or create a mask for your data.
    lon: 
        Import your longitude coordinates, if your grid is regular, you can use a linspace instead.
    lat: 
        Import your latitude coordinates (same as above).
    levels: 
        List of the levels in which ones you want to find the eddies.
    date: 
        Date as Julian Days.
    areamap: 
        array([[0,len(lon)],[0,len(lat)]]) Array with the index 
        of your area of interest.
    
    I used some auxilar functions, each one has his respective author.
    Author: Josue Martinez Moreno, 2017
    '''

    # Defining lists inside dictionary
    ellipse_path = []
    contour_path = []
    mayoraxis_eddy = []
    minoraxis_eddy = []
    gaussianfitdict = []
    gaussfit2d = []
    # Data shape
    shapedata = np.shape(data)
    # Diagnostics to list, which allows to print multiple diagnostics at the same time.
    # (Be carefull because it uses a lot of memory)
    if type(diagnostics) != list:
        diagnostics = [diagnostics]
    # Check if data is masked
    elif data is ma.masked:
        print('Invalid data data, must be masked')
        return
    # Make sure the shape of the data is identical to the grid.
    elif shapedata == [len(lat), len(lon)]:
        print('Invalid  data size, should be [length(lat) length(lon]')
        return
    #Check that area to analyze is valid
    elif np.shape(areamap) == shapedata:
        if np.shape(areamap) == [1, 1] | len(areamap) != len(lat):
            print('Invalid areamap, using NaN for eddy surface area')
        return
    #Check the number of levels is valid.
    elif len(levels) != 2:
        print(
            'Invalid len of levels, please use the function for multiple levels or use len(levels)==2'
        )
        return
    #Saving mask for future post-processing.
    if mask != '':
        data = np.ma.masked_array(data, mask)
    datanan = data.filled(np.nan)
    # Obtain the contours of a surface (contourf), this aproach is better than the contour.
    if len(np.shape(lon)) == 1 and len(np.shape(lat)) == 1:
        Lon, Lat = np.meshgrid(lon, lat)
    else:
        Lon, Lat = lon, lat
    # Extract min value and max value from the coordinates.
    min_x = Lon[0, 0]
    min_y = Lat[0, 0]
    max_x = Lon[-1, -1]
    max_y = Lat[-1, -1]
    # Plot contours according to the data.
    if len(shapedata) == 3:
        CS=plt.contourf(lon[areamap[0,0]:areamap[0,1]],lat[areamap[1,0]:areamap[1,1]],\
                datanan[date,areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]],levels=levels)
    else:
        CS=plt.contourf(lon[areamap[0,0]:areamap[0,1]],lat[areamap[1,0]:areamap[1,1]],\
                datanan[areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]],levels=levels)
    if preferences == None:
        preferences = {'ellipse': 0.85, 'eccentricity': 0.85, 'gaussian': 0.8}
    # Close the contour plot.
    plt.close()
    # Extracting detected contours.
    CONTS = CS.allsegs[:][:]
    # Define variables used in the main loop.
    total_contours = 0
    eddyn = 0
    threshold = 7
    threshold2D = 20
    numverlevels = np.shape(CONTS)[0]
    fiteccen = 1
    areachecker = np.inf
    ellipsarea = np.inf
    center_eddy = [np.nan, np.nan]
    center_extrem = [np.nan, np.nan]
    contarea = np.inf
    checkm = False
    checkM = False
    gaussarea = [False, 0]
    gausssianfitp = [0, 0, 0, 0, 0, 0]
    xx = np.nan
    yy = np.nan
    areastatus = {'check': None, 'contour': None, 'ellipse': None}
    # Loop in contours of the levels defined.
    for ii in range(0, numverlevels):
        if debug == True:
            print("\n *******EDDY******")
            pdb.set_trace()
        CONTSlvls = CONTS[ii]
        numbereddies = np.shape(CONTSlvls)[0]
        # Loop over all the close contours.
        for jj in range(0, numbereddies):
            if debug == True:
                print("\n ******* EDDY N ******")
                pdb.set_trace()
            check = False
            CONTeach = CONTSlvls[jj]

            #Relevant contour values
            xidmin, xidmax = find2l(lon, lon, CONTeach[:, 0].min(),
                                    CONTeach[:, 0].max())
            yidmin, yidmax = find2l(lat, lat, CONTeach[:, 1].min(),
                                    CONTeach[:, 1].max())

            if xidmin <= threshold - 1:
                xidmin = +threshold - 1
            elif xidmax >= len(lon) - threshold:
                xidmax = len(lon) - threshold
            if yidmin <= threshold - 1:
                yidmin = threshold - 1
            elif yidmax >= len(lat) - threshold:
                yidmax = len(lat) - threshold
            lon_contour = lon[xidmin - threshold + 1:xidmax + threshold]
            lat_contour = lat[yidmin - threshold + 1:yidmax + threshold]

            cmindex = find(CONTeach[:, 1], CONTeach[:, 1].max())
            xmindex, ymindex = find2l(lon, lat, CONTeach[cmindex, 0],
                                      CONTeach[cmindex, 1])
            centertop = [
                ymindex - yidmin + threshold - 2,
                xmindex - xidmin + threshold - 1
            ]

            if len(shapedata) == 3:
                data4gauss = datanan[date,
                                     yidmin - threshold + 1:yidmax + threshold,
                                     xidmin - threshold + 1:xidmax + threshold]
                data_in_contour = insideness_contour(data4gauss * 1,
                                                     centertop,
                                                     levels,
                                                     maskopt=maskopt,
                                                     diagnostics=diagnostics)
            else:
                data4gauss = datanan[yidmin - threshold + 1:yidmax + threshold,
                                     xidmin - threshold + 1:xidmax + threshold]
                data_in_contour = insideness_contour(data4gauss * 1,
                                                     centertop,
                                                     levels,
                                                     maskopt=maskopt,
                                                     diagnostics=diagnostics)

            checkcontour = check_closecontour(CONTeach, lon_contour,
                                              lat_contour, data4gauss)

            if checkcontour == False:
                xx = np.nan
                yy = np.nan
                center = [np.nan, np.nan]
            else:
                checke = False
                ellipse, status, r2 = fit_ellipse(CONTeach[:, 0],
                                                  CONTeach[:, 1],
                                                  diagnostics=diagnostics)
                if status == True and r2 >= preferences[
                        'ellipse'] and preferences['ellipse'] < 1:
                    checke = True
                if status == True:
                    ellipseadjust,checke=ellipsefit(CONTeach[:,1],ellipse['ellipse'][1],\
                                              ellipsrsquarefit=preferences['ellipse'],\
                                              diagnostics=diagnostics)
                if checke == True:

                    center = [ellipse['X0_in'], ellipse['Y0_in']]
                    phi = ellipse['phi']
                    axes = [ellipse['a'], ellipse['b']]
                    R = np.arange(0, 2.1 * np.pi, 0.1)
                    a, b = axes
                    eccen = eccentricity(a, b)

                    if eccen <= preferences['eccentricity']:
                        #Ellipse coordinates.
                        xx = ellipse['ellipse'][0]
                        yy = ellipse['ellipse'][1]

                        mayoraxis = ellipse['majoraxis']
                        minoraxis = ellipse['minoraxis']

                        areastatus=checkscalearea(areaparms,lon_contour,lat_contour,\
                                                    xx,yy,CONTeach[:,0],CONTeach[:,1])

                        if eddycenter == 'maximum':
                            center_eddy=contourmaxvalue(data_in_contour,lon_contour,\
                                                 lat_contour,levels,date,threshold)
                            center_eddy[
                                3] = center_eddy[3] + xidmin - threshold + 1
                            center_eddy[
                                4] = center_eddy[4] + yidmin - threshold + 1

                            center_extrem = center_eddy

                        elif eddycenter == 'masscenter':
                            center_eddy=centroidvalue(CONTeach[:,0],CONTeach[:,1],\
                                                data_in_contour,lon_contour,\
                                                lat_contour,levels,date,threshold)
                            center_extrem=contourmaxvalue(data_in_contour,lon_contour,\
                                                        lat_contour,levels,date)
                            center_extrem[
                                3] = center_extrem[3] + xidmin - threshold + 1
                            center_extrem[
                                4] = center_extrem[4] + yidmin - threshold + 1
                        checkM = False
                        checkm = False

                        if areastatus['status']:
                            if checkgauss == True:
                                if len(shapedata) == 3:
                                    profile,checkM=extractprofeddy(mayoraxis,\
                                                   data4gauss,lon_contour,lat_contour,50,\
                                                   gaus='One',kind='linear',\
                                                   gaussrsquarefit=preferences['gaussian'],\
                                                   diagnostics=diagnostics)
                                    if checkM == True:
                                        profile,checkm=extractprofeddy(minoraxis,\
                                                       data4gauss,lon_contour,\
                                                       lat_contour,50,\
                                                       gaus='One',kind='linear',\
                                                       gaussrsquarefit=preferences['gaussian'],\
                                                       diagnostics=diagnostics)
                                else:
                                    profile,checkM=extractprofeddy(mayoraxis,\
                                                   data4gauss,lon_contour,lat_contour,50,\
                                                   gaus='One',kind='linear',\
                                                   gaussrsquarefit=preferences['gaussian'],\
                                                   diagnostics=diagnostics)
                                    if checkM == True:
                                        profile,checkm=extractprofeddy(minoraxis,\
                                                       data4gauss,lon_contour,\
                                                       lat_contour,50,\
                                                       gaus='One',kind='linear',\
                                                       gaussrsquarefit=preferences['gaussian'],\
                                                       diagnostics=diagnostics)
                                #print(checkM,checkm)
                                if checkM == True and checkm == True:
                                    if levels[0] > 0:
                                        level = levels[0]
                                        extremvalue = np.nanmax(
                                            data_in_contour)
                                    else:
                                        level = levels[1]
                                        extremvalue = np.nanmin(
                                            data_in_contour)

                                    initial_guess = [a, b, phi, 0, 0, 0]
                                    #print("\n ---pre fit---")
                                    #pdb.set_trace()
                                    fixvalues=[lon_contour,lat_contour,extremvalue,\
                                               center_extrem[0],center_extrem[1]]
                                    gausssianfitp,R2=fit2Dcurve(data_in_contour,\
                                                  fixvalues,\
                                                  level,initial_guess=initial_guess,date='',\
                                                  mode=mode,diagnostics=diagnostics)
                                    #gausssianfitp=initial_guess
                                    # Buf fix for anomalous big Gaussians
                                    #print('++++++++++',abs(gausssianfitp[0]) < 2*np.pi*(xx.max()-xx.min()))
                                    if abs(gausssianfitp[0]) < 2 * np.pi * (
                                            xx.max() - xx.min()
                                    ) or abs(gausssianfitp[1]) < 2 * np.pi * (
                                            xx.max() - xx.min()):
                                        fiteccen = eccentricity(
                                            gausssianfitp[0], gausssianfitp[1])
                                        gausscheck2D = checkgaussaxis2D(a,b,\
                                                                        gausssianfitp[0],\
                                                                        gausssianfitp[1])

                                        #print('=======',gausscheck2D,fiteccen)
                                        if fiteccen <= preferences[
                                                'eccentricity'] and gausscheck2D == True:
                                            if xidmin <= threshold2D:
                                                xidmin = threshold2D
                                            elif xidmax >= len(
                                                    lon) - threshold2D:
                                                xidmax = len(lon) - threshold2D
                                            if yidmin <= threshold2D:
                                                xidmin = threshold2D
                                            elif yidmax >= len(
                                                    lat) - threshold2D:
                                                yidmax = len(lat) - threshold2D
                                            fixvalues[0] = lon[xidmin -
                                                               threshold2D +
                                                               1:xidmax +
                                                               threshold2D]
                                            fixvalues[1] = lat[yidmin -
                                                               threshold2D +
                                                               1:yidmax +
                                                               threshold2D]
                                            gaussarea= gaussareacheck(fixvalues,level,\
                                                                      areaparms,gausssianfitp,\
                                                                      areastatus['contour'])
                                            if gaussarea[0] == True:
                                                check = True
                            else:
                                print(
                                    'Checkgauss need to be True to reconstruct the field.'
                                )
                    if check == True:
                        if usefullfit == False and mode == 'gaussian':
                            gausssianfitp[-1] = 0
                            gausssianfitp[-2] = 0
                            gausssianfitp[-3] = 0
                        ellipse_path.append([xx, yy])
                        contour_path.append([CONTeach[:, 0], CONTeach[:, 1]])
                        mayoraxis_eddy.append([mayoraxis[0], mayoraxis[1]])
                        minoraxis_eddy.append([minoraxis[0], minoraxis[1]])
                        gaussianfitdict.append([gausssianfitp])
                        gaussfit2d.append([R2])
                        #Switch from the ellipse center to the position of
                        #the maximum value inside de contour
                        if eddyn == 0:
                            position_selected = [center_eddy]
                            position_max = [center_extrem]
                            position_ellipse = [center]
                            total_eddy = [[eddyn]]
                            area = [[
                                areastatus['contour'], areastatus['check'],
                                gaussarea[1]
                            ]]
                            angle = [phi]
                            if CS.levels[0] > 0:
                                level = CS.levels[0]
                            else:
                                level = CS.levels[1]
                            levelm = [[level]]
                        else:
                            position_selected = np.vstack(
                                (position_selected, center_eddy))
                            position_max = np.vstack(
                                (position_max, center_extrem))
                            position_ellipse = np.vstack(
                                (position_ellipse, center))
                            total_eddy = np.vstack((total_eddy, eddyn))
                            area = np.vstack((area, [
                                areastatus['contour'], areastatus['check'],
                                gaussarea[1]
                            ]))
                            angle = np.vstack((angle, phi))

                            if CS.levels[0] > 0:
                                levelprnt = CS.levels[0]
                                levelm = np.vstack((levelm, levelprnt))
                            else:
                                levelprnt = CS.levels[1]
                                levelm = np.vstack((levelm, levelprnt))
                        eddyn = eddyn + 1
                    #diagnostics=True
                    if ("ellipse"
                            in diagnostics) or ("all" in diagnostics) or (
                                True in diagnostics):  # and  check == True:

                        print("Eddy Number (No time tracking):", eddyn)
                        print("Ellipse parameters")
                        print("Ellipse center = ", center)
                        print("Mass center = ", center_eddy)
                        print("angle of rotation = ", phi)
                        print("axes (a,b) = ", axes)
                        print("Eccentricity (ellips,gauss) = ", eccen,
                              fiteccen)
                        print("Area (rossby,cont,ellips,gauss) = ",
                              areastatus['check'], areastatus['contour'],
                              areastatus['ellipse'], gaussarea[1])
                        print("Ellipse adjust = ", ellipseadjust, checke)
                        print("Mayor Gauss fit = ", checkM)
                        print("Minor Gauss fit = ", checkm)
                        print("2D Gauss fit (Fitness, R^2)=", gausssianfitp,
                              gaussfit2d)
                        print(
                            "Conditions | Area | Ellipse | Eccen | Gaussians ")
                        if areastatus['ellipse'] == None or areastatus[
                                'check'] == None or areastatus[
                                    'contour'] == None:
                            print("           | ", False," | ", checke ,"| ",\
                                  eccen <= preferences['eccentricity'] and fiteccen <= preferences['eccentricity'] ,\
                                  " | ", checkM and checkm)
                        else:
                            print("           | ", areastatus['ellipse'] < areastatus['check'] and areastatus['contour'] < areastatus['check'] and  gaussarea[0],\
                              " | ", checke ,"| ",
                                  eccen <= preferences['eccentricity'] and fiteccen <= preferences['eccentricity'] ,\
                              " | ", checkM and checkm)

                    if ("all" in diagnostics) or (
                            True in diagnostics):  #and plotdata == True:
                        f, (ax1, ax2) = plt.subplots(1, 2, figsize=(13, 6))
                        if len(shapedata) == 3:
                            ax1.contourf(lon[areamap[0,0]:areamap[0,1]],lat[areamap[1,0]:areamap[1,1]],\
                                data[date,areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]])
                            cc=ax2.pcolormesh(lon[areamap[0,0]:areamap[0,1]],\
                                              lat[areamap[1,0]:areamap[1,1]],\
                              data[date,areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]],\
                              vmin=data[date,areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]].min(),\
                              vmax=data[date,areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]].max())
                            cca=ax2.contour(lon[areamap[0,0]:areamap[0,1]],\
                                            lat[areamap[1,0]:areamap[1,1]],\
                                            data[date,areamap[1,0]:areamap[1,1],\
                                            areamap[0,0]:areamap[0,1]],levels=levels,cmap='jet')
                            ax2.clabel(cca, fontsize=9, inline=1)
                        else:
                            cca=ax1.contourf(lon[areamap[0,0]:areamap[0,1]],\
                                             lat[areamap[1,0]:areamap[1,1]],\
                                             datanan[areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]],\
                                             levels=levels)
                            ax1.plot(CONTeach[:, 0], CONTeach[:, 1], '-r')
                            ax2.plot(CONTeach[:, 0], CONTeach[:, 1], '-r')
                            ax2.pcolormesh(lon[areamap[0,0]:areamap[0,1]],\
                                           lat[areamap[1,0]:areamap[1,1]],\
                                           datanan[areamap[1,0]:areamap[1,1],\
                                           areamap[0,0]:areamap[0,1]],vmin=-20,vmax=20)
                            plt.show()
                            f, (ax1, ax2) = plt.subplots(1, 2, figsize=(13, 6))
                            ax1.contourf(lon[areamap[0,0]:areamap[0,1]],\
                                         lat[areamap[1,0]:areamap[1,1]],\
                                         data[areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]])
                            cc=ax2.pcolormesh(lon[areamap[0,0]:areamap[0,1]],\
                                              lat[areamap[1,0]:areamap[1,1]],\
                              data[areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]],\
                              vmin=data[areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]].min(),\
                              vmax=data[areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]].max())
                            cca=ax2.contour(lon[areamap[0,0]:areamap[0,1]],\
                                            lat[areamap[1,0]:areamap[1,1]],\
                                            data[areamap[1,0]:areamap[1,1],areamap[0,0]:areamap[0,1]],\
                                            levels=levels,cmap='jet')
                            ax2.clabel(cca, fontsize=9, inline=1)
                        ax1.plot(CONTeach[:, 0], CONTeach[:, 1], '*r')
                        ax1.plot(xx, yy, '-b')
                        ax1.plot(center[0], center[1], 'ob')
                        f.subplots_adjust(right=0.8)
                        cbar_ax = f.add_axes([0.85, 0.15, 0.05, 0.7])
                        f.colorbar(cc, cax=cbar_ax)
                        ax2.plot(CONTeach[:, 0], CONTeach[:, 1], '-r')
                        ax2.plot(xx, yy, '-b')
                        ax2.plot(center[0], center[1], 'ob')
                        idxelipcheck, idyelipcheck = find2l(
                            lon, lat, center[0], center[1])
                        ax2.plot(lon[idxelipcheck], lat[idyelipcheck], 'om')
                        ax2.plot(center_eddy[0], center_eddy[1], 'oc')
                        ax2.plot(center_extrem[0], center_extrem[1], '*g')
                        ax2.set_ylim(
                            [CONTeach[:, 1].min(), CONTeach[:, 1].max()])
                        ax2.set_xlim(
                            [CONTeach[:, 0].min(), CONTeach[:, 0].max()])
                        plt.show()
                        plt.close()

                total_contours = total_contours + 1
        try:
            position_selected = np.array(position_selected)
            position_max = np.array(position_max)
            position_ellipse = np.array(position_ellipse)
            area = np.array(area)
            levelm = np.array(levelm)
            mayoraxis_eddy = np.array(mayoraxis_eddy)
            minoraxis_eddy = np.array(minoraxis_eddy)
            gaussianfitdict = np.array(gaussianfitdict)
            gaussfit2d = np.array(gaussfit2d)
            eddys=dict_eddym(contour_path,ellipse_path,position_selected,\
                             position_max,position_ellipse,\
                             mayoraxis_eddy,minoraxis_eddy,\
                             area,angle,total_eddy,levelm,gaussianfitdict,gaussfit2d)
            check = True

        except:
            check = False
            eddys = 0
        #if destdir!='':
        #    save_data(destdir+'day'+str(date)+'_one_step_cont'+str(total_contours)+'.dat', variable)
    return eddys, check, total_contours