def draw_go_pmid_histogram(go_cats, species, onto='all'):
        
    xaxis = []
    yaxis = []
    for (pmid_list, avg) in go_cats:
        xaxis.append(avg)
        yaxis.append(len(pmid_list))
        
    pylab.bar(xaxis, yaxis, align='center')
    pylab.gcf().subplots_adjust(bottom=0.25)    
    pylab.xticks(xaxis, xaxis, rotation=90)
    
    if onto.lower() == 'all':
        pylab.title("Number of papers per GO Category across all GO subontologies")
    elif onto.lower() == 'mf':
        pylab.title("Number of papers per GO Category across GO Molecular Function")      
    elif onto.lower() == 'bp':
        pylab.title("Number of papers per GO Category across GO Biological Process")
    elif onto.lower() == 'cc':
        pylab.title("Number of papers per GO Category across GO Cellular Component")

    pylab.xlabel("Average number of GO terms")
    pylab.ylabel('Number of PMIDs')        
    pylab.grid(True)
    pylab.tight_layout()
    pylab.savefig(os.path.join(CURR_PATH,"Cohesion/"+species+"/goPmid_"+species+"_"+onto+".png"))
    pylab.close()
Example #2
0
    def draw(self, axes=None, loc=2):
        if axes == None:
            axes = p.gca()

        if self.box != None and self.box in axes.artists:
            del axes.artists[ axes.artists.index(self.box) ]
        
        #key_areas = [ mpl.offsetbox.TextArea(k, textprops=self.textprops) for k in self.datadict.keys() ]
        #val_areas = [ mpl.offsetbox.TextArea(self.datadict[k], textprops=self.textprops) for k in self.datadict.keys() ]
        key_areas = [ mpl.offsetbox.TextArea(k, textprops=self.textprops) for k in list(self.datadict.keys()) ]
        val_areas = [ mpl.offsetbox.TextArea(self.datadict[k], textprops=self.textprops) for k in list(self.datadict.keys()) ]
        
        key_vpack = mpl.offsetbox.VPacker(children=key_areas, align="left", pad=0, sep=0)
        val_vpack = mpl.offsetbox.VPacker(children=val_areas, align="right", pad=0, sep=0)
        hpack = mpl.offsetbox.HPacker(children=[key_vpack, val_vpack], align="top", pad=0,sep=4)
        
        globchildren = []
        if self.title != None:
            #titlearea = mpl.offsetbox.TextArea(self.title, textprops=self.titleprops)
            titlearea = mpl.offsetbox.TextArea(self.title)
            globchildren.append(titlearea)
        
        globchildren.append(hpack)
        globvpack = mpl.offsetbox.VPacker(children=globchildren, align="center", pad=0, sep=1)
        
        self.box = mpl.offsetbox.AnchoredOffsetbox(loc=loc, child=globvpack)
        self.box.patch.set_facecolor(self.facecolor)
        self.box.patch.set_edgecolor(self.edgecolor)
        self.box.patch.set_alpha(self.alpha)
        axes.add_artist( self.box )

        if p.isinteractive():
            p.gcf().canvas.draw()
Example #3
0
def trimmedReadLenHisto(readLenFileName, readLenHistoFileName):
    f = open(readLenFileName)
    title = f.readline()
    data = f.readlines()
    f.close()
    title=title.strip('\n')
    titlelist=title.split('\t')
    column = -1
    for b in range(0,len(titlelist)): 
        if titlelist[b] == 'trimLen':
            column=b
    if column == -1:
        print "ERROR: trimmedReadLenHisto: %s doesn't contain a valid title" % readLenFileName
    a = [float(i.strip().split('\t')[column]) for i in data]
    # establish a dynamic X axis scale based on data size
    mode = findMode(a)
    graph_max_x = math.trunc(mode/100.0+0.5) * 100 + 100
    if graph_max_x < 400:
        graph_max_x = 400
    pylab.clf()
    pylab.gcf().set_size_inches((8.0,4.0))
    pylab.gcf().set_size_inches((8,4))
    pylab.hist(a, bins=graph_max_x, range=(0,graph_max_x),color='#2D4782',histtype='stepfilled')
    pylab.title('Read Length Histogram')
    pylab.xlabel('Read Length')
    pylab.ylabel('Count')
    pylab.savefig(readLenHistoFileName)
Example #4
0
def hpSNR(metrics, charType):
    for k, v in metrics.iteritems():
        title = str(k)
        e = v[charType].strip().split(",")
        hp = []
        snr = []
        for i in e:
            k = i.split(":")
            key = k[0].strip()
            value = k[1].strip()
            hp.append(key)
            snr.append(float(value))
        pylab.clf()
        pylab.xlabel("HP")
        pylab.ylabel("SNR")
        pylab.title("%s_%s" % (charType, title))
        pylab.gcf().set_size_inches((8, 4))
        xaxis = [i for i in range(len(hp))]
        ymax = int(max(snr)) + 1
        xlen = int(len(hp)) + 1
        pylab.axis([0, xlen, 0, ymax])
        pylab.xticks(range(len(hp)))
        pylab.bar(xaxis, snr, width=0.5, linewidth=0, color="r", alpha=0.5, align="center")

        # pylab.legend()
        pylab.savefig(path.join(os.getcwd(), "%s_%s.png" % (charType, title)))
        pylab.clf()
def plot_cumulative_boxes_scale(annotations, dataset_name):

    # model_height = 128
    object_height = 48 # the expected height of the pedestrian inside the detection box
    boxes_scales = []

    for filename, boxes in annotations:
        for box in boxes:
            height = box.max_corner.y - box.min_corner.y
            scale = height / float(object_height)
            boxes_scales.append(scale)


    boxes_log_scales = [math.log(x)/math.log(2) for x in boxes_scales]
    min_log_scale = int(round(min(boxes_log_scales)))
    max_log_scale = int(round(max(boxes_log_scales)))

    if True:
        print("min/max scale in dataset %s == %.4f/%.4f" \
              % (dataset_name, min(boxes_scales), max(boxes_scales)))

    num_boxes_per_scale = pylab.zeros((max_log_scale - min_log_scale)+1)
    log_scales_range = range(min_log_scale, max_log_scale+1)
    for index, log_scale in enumerate(log_scales_range):
        num_boxes_per_scale[index] = len([x for x in boxes_log_scales if round(x) >= log_scale])


    if False:
        # create a new figure
        pylab.figure()
        pylab.gcf().set_facecolor("w") # set white background

        #scales_cumsum = pylab.cumsum(boxes_scales)
        #pylab.plot(scales_cumsum)
        pylab.hist(#boxes_scales,
                   boxes_log_scales,
                   bins=10,
                   #cumulative= True,
                   normed=True,
                   histtype = "stepfilled")
        pylab.title("Cumulative histogram of the annotated windows scales\n Dataset: " + dataset_name)
        pylab.xlabel("Annotated window scale octave")
        pylab.ylabel("Percent of annotated windows")


    # create a new figure
    pylab.figure()
    pylab.gcf().set_facecolor("w") # set white background

    pylab.plot(log_scales_range, num_boxes_per_scale)
    pylab.xlabel("Annotated window scale octave")
    pylab.ylabel("Number of annotated windows")
    pylab.title("Number of annotated windows per scale octave\n Dataset: " + dataset_name)

    pylab.grid(True)
    pylab.xticks(log_scales_range)
    #pylab.yticks(yticks)
    pylab.xlim([log_scales_range[0] - 0.5,  log_scales_range[-1] + 0.5])

    return
Example #6
0
def show_barlines(page):
    import pylab
    for i, barlines in enumerate(page.barlines):
        sd = page.staves.staff_dist[i]
        for j, barline_range in enumerate(barlines):
            barline_x = int(barline_range.mean())
            staff_y = page.staves.staff_y(i, barline_x)
            repeats = page.repeats[i][j]
            if repeats:
                # Draw thick bar
                pylab.fill_between([barline_x - sd/4,
                                    barline_x + sd/4],
                                   staff_y - sd*2,
                                   staff_y + sd*2,
                                   color='g')
                for letter, sign in (('L', -1), ('R', +1)):
                    if letter in repeats:
                        # Draw thin bar
                        bar_x = barline_x + sign * sd/2
                        pylab.plot([bar_x, bar_x],
                                   [staff_y - sd*2,
                                    staff_y + sd*2],
                                   color='g')
                        for y in (-1, +1):
                            circ = pylab.Circle((bar_x + sign*sd/2,
                                                 staff_y + y*sd/2),
                                                sd/4,
                                                color='g')
                            pylab.gcf().gca().add_artist(circ)
            else:
                pylab.plot([barline_x, barline_x],
                           [staff_y - sd*2,
                            staff_y + sd*2],
                           color='g')
Example #7
0
def testeps(d):
    M.clf()
    M.pcolor(d)
    M.axis('tight')
    M.colorbar()
    M.gcf().set_size_inches((7.5,6.))
    M.savefig('test.png',dpi=240)
Example #8
0
    def after_step(self, rbm, trainer, i):
        it = i + 1

        save = it in self.expt.save_after
        display = it in self.expt.show_after

        if save:
            if self.expt.save_particles:
                storage.dump(trainer.fantasy_particles, self.expt.pcd_particles_file(it))
            storage.dump(rbm, self.expt.rbm_file(it))
            if hasattr(trainer, 'avg_rbm'):
                storage.dump(trainer.avg_rbm, self.expt.avg_rbm_file(it))
            storage.dump(time.time() - self.t0, self.expt.time_file(it))

        if 'particles' in self.subset and (save or display):
            fig = rbm_vis.show_particles(rbm, trainer.fantasy_particles, self.expt.dataset, display=display,
                                         figtitle='PCD particles ({} updates)'.format(it))
            if display:
                pylab.gcf().canvas.draw()
            if save:
                misc.save_image(fig, self.expt.pcd_particles_figure_file(it))

        if 'gibbs_chains' in self.subset and (save or display):
            fig = diagnostics.show_chains(rbm, trainer.fantasy_particles, self.expt.dataset, display=display,
                                          figtitle='Gibbs chains (iteration {})'.format(it))
            if save:
                misc.save_image(fig, self.expt.gibbs_chains_figure_file(it))

        if 'objective' in self.subset:
            self.log_prob_tracker.update(rbm, trainer.fantasy_particles)

        if display:
            pylab.gcf().canvas.draw()
Example #9
0
def p_h(data):
    P.gcf().set_size_inches(10, 5)
    x = np.arange(1, len(data[0]) + 1, 1)
    for y in data:
        P.plot(x, y)
    P.ylabel("Number of heads - Number of tails")
    P.xlabel("Number of coin tosses")
Example #10
0
def plot_collinearity(motifs, best_Z):
    """Plot the cooccurrences of motifs.
    """
    import scipy.cluster.hierarchy as hier
    # from scipy.stats import pearsonr
    M = len(motifs)
    cooccurrences = numpy.ones((M, M))
    for m1 in xrange(M):
        for m2 in xrange(M):
            # both = sum(numpy.logical_and(m1seqs, m2seqs))
            # cooccurrences[m1,m2] = both/float(sum(m2seqs))
            cooccurrences[m1, m2] = \
                numpy.sqrt(sum(best_Z[m1] * best_Z[m2])) \
                / numpy.linalg.norm(best_Z[m2])
            # rho, _ = pearsonr(best_Z[m1], best_Z[m2])
            # cooccurrences[m1, m2] = rho
    Y = hier.centroid(cooccurrences)
    index = hier.fcluster(Y, -1) - 1
    cooccurrences = cooccurrences[index, :]
    cooccurrences = cooccurrences[:, index]
    pylab.pcolor(cooccurrences)
    pylab.colorbar()
    ax = pylab.gca()
    ax.set_xticks([])
    # ax.set_xticks(.5 + numpy.arange(M))
    # ax.set_xticklabels(motifs)
    ax.set_yticks(.5 + numpy.arange(M))
    ax.set_yticklabels(numpy.asarray(motifs)[index])
    ax.set_xlim((0, M))
    ax.set_ylim((0, M))
    for line in ax.yaxis.get_ticklines():
        line.set_markersize(0)
    pylab.gcf().subplots_adjust(left=.27, bottom=.02, top=.98, right=.99)
def analyze_year():
    fname = path + "/stretch/data_stretching_oneref_%s_%03d_5swin.npz"
    k = 0
    corrs = []
    datess = []
    stretchs = []
    while k <= 60:
        if not os.path.exists(fname % (station, k)):
            k += 1
            continue
        print "k", k
        npzfile = np.load(fname % (station, k))
        corr, stretch, dates = npzfile["corr"], npzfile["stretch"], npzfile["dates"]
        corr = corr[:, 1]
        stretch = stretch[:, 1]
        corrs.append(corr)
        datess.append(dates)
        stretchs.append(stretch)
        k += 1
    datess = np.hstack(datess)
    corrs = np.hstack(corrs)
    stretchs = np.hstack(stretchs)
    stretchs = smooth2(stretchs, 48 * 3)
    # stretchs, datess = smooth(stretchs, datess, 48)
    plt.plot(datess, stretchs)
    plt.gcf().autofmt_xdate()
    xfmt = mpl.dates.DateFormatter("%y-%m-%d %H:%M")
    plt.gca().xaxis.set_major_formatter(xfmt)
    plt.savefig(path + "/plots/year_%s_TIQ_5swin.pdf" % station)
    plt.show()
Example #12
0
def plotDepth():
    sns.set_style("whitegrid", {"grid.color": "1", 'axes.linewidth': .5, "grid.linewidth": ".09"})
    sns.set_context("notebook", font_scale=1.4, rc={"lines.linewidth": 2.5})
    d = pd.read_pickle(utl.outpath + 'real/CD.F59.df').xs('D', level='READ', axis=1)
    (d.min(1) > 50).sum()

    (d > 50).sum().sum()

    z = pd.Series(np.ndarray.flatten(d.values))
    fontsize = 6
    mpl.rcParams.update({'font.size': fontsize})
    plt.figure(figsize=(6, 4), dpi=300);
    plt.subplot(2, 2, 1);
    z.value_counts().sort_index().plot()
    plt.xlim([0, 200]);
    plt.xlabel('Depth');
    plt.ylabel('Number of Measurments' + '\n (out of {:.1f}M)'.format(z.shape[0] / 1e6));
    plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
    plt.title('Scaled PDF')
    pplt.annotate('(A)', xpad=0.85, ypad=0.45, fontsize=fontsize)
    plt.axvline(50, linestyle='--', linewidth=1, color='k')
    pplt.setSize(plt.gca(), fontsize)
    plt.subplot(2, 2, 2);

    z.value_counts().sort_index().cumsum().plot()
    plt.xlim([0, 200])
    plt.ylim([-3e5, 2.05 * 1e7])
    plt.xlabel('Depth');
    plt.title('Scaled CDF')
    pplt.annotate('(B)', xpad=0.85, ypad=0.45, fontsize=fontsize)
    plt.axvline(50, linestyle='--', linewidth=1, color='k')
    pplt.setSize(plt.gca(), fontsize)
    plt.subplot(2, 2, 3);
    d.min(1).value_counts().sort_index().plot()
    plt.xlim([0, 100]);
    plt.xlabel('Minimum Depth of each Variant');
    plt.ylabel('Number of Variants' + '\n (out of {:.1f}M)'.format(d.shape[0] / 1e6));
    plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
    plt.rc('font', size=fontsize)
    pplt.annotate('(C)', xpad=0.85, ypad=0.45, fontsize=fontsize)
    plt.axvline(50, linestyle='--', linewidth=1, color='k')
    pplt.setSize(plt.gca(), fontsize)
    plt.subplot(2, 2, 4);
    d.min(1).value_counts().sort_index().cumsum().plot()
    plt.xlim([0, 60])
    plt.ylim([0.25 * -1e5, plt.ylim()[1]])
    plt.xlabel('Minimum Depth of each Variant');
    plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
    pplt.annotate('(D)', xpad=0.85, ypad=0.45, fontsize=fontsize)
    plt.axvline(50, linestyle='--', linewidth=1, color='k')
    pplt.setSize(plt.gca(), fontsize)
    plt.gcf().subplots_adjust(bottom=0.15)
    plt.gcf().tight_layout(h_pad=0.1)
    fontsize = 6
    mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern'], 'size': fontsize});
    mpl.rc('text', usetex=True)
    mpl.rcParams.update({'font.size': 1})

    pplt.savefig('depth', 300)
    plt.show()
Example #13
0
def plotDepthHeterogenocity():
    dpi = 300
    sns.set_style("whitegrid", {"grid.color": "0.9", 'axes.linewidth': .5, "grid.linewidth": ".09"})
    _, ax = plt.subplots(2, 2, sharex=True, figsize=(6, 4), dpi=dpi)

    d = pd.read_pickle(utl.outpath + 'real/CD.F59.df').xs('D', level='READ', axis=1)
    std = d.std(1)
    loc = [std.idxmax(), (std == std.quantile(0.52)).replace({False: None}).dropna().index[0],
           (std == std.median()).replace({False: None}).dropna().index[-1],
           (std == std.quantile(0.8)).replace({False: None}).dropna().index[0]]
    ax = ax.reshape(-1)
    fontsize = 6
    for i, pos in enumerate(loc):
        eg = d.loc[pos]
        [eg[r].dropna().plot(marker='o', ax=ax[i], markersize=5) for r in range(3)];
        plt.xticks(d.columns.get_level_values('GEN').unique());
        plt.xlabel('');
        plt.ylabel('')
        print 'position={}:{}'.format(eg.name[0], eg.name[1]), get_axis_limits()

        if i in [0, 2]: ax[i].set_ylabel('Read Depth')
        if i > 1: ax[i].set_xlabel('Generation')
        if i == 0: ax[i].legend(['Replicate 1', 'Replicate 2', 'Replicate 3'], loc='upper center',
                                prop={'size': fontsize})
        yrang = pplt.get_axis_limits(upper=True, ax=ax[i])[1] - pplt.get_axis_limits(upper=False, ax=ax[i])[1]
        ax[i].set_ylim([min(0, ax[i].get_ylim()[0] - 0.05 * yrang), ax[i].get_ylim()[1] + 0.03 * yrang])
        ax[i].set_xlim([-2, 61]);
        ax[i].set_title('{}:{}'.format(eg.name[0], eg.name[1]))
        pplt.setSize(ax[i], fontsize)

    mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']});
    mpl.rc('text', usetex=True)
    plt.gcf().subplots_adjust(bottom=0.15)
    pplt.savefig('depthHetero', dpi)
    plt.show()
Example #14
0
def plot_signal(t, u, fig_title='', file_name=''):
    """
    Plot a signal.

    Parameters
    ----------
    t : ndarray of floats
        Times (in s) at which the signal is defined.
    u : ndarray of floats
        Signal samples.
    fig_title : string
        Plot title.
    file_name : string
        File in which to save the plot.

    """

    p.clf()

    # Set the plot window title:
    p.gcf().canvas.set_window_title(fig_title)
    p.plot(t, u)
    p.xlabel('t (s)')
    p.ylabel('u(t)')
    p.title(fig_title)

    # Make the x axis exactly correspond to the plotted signal's time range:
    p.gca().set_xlim(min(t), max(t)) 
    p.draw_if_interactive()
    if file_name:
        p.savefig(file_name)
def demo_envelopes():

    for i, demo in enumerate(demos):
        try:
            prev = subplot(len(demos), 1, i+1, sharex=prev, sharey=prev)
        except NameError:
            prev = subplot(len(demos), 1, i+1)
        title(repr(demo).strip('{}').replace("'", ""))
        prev.yaxis.set_visible(False)
        prev.xaxis.set_visible(False)

        # Generate carrier of appropriate length for the trial
        #carrier = np.random.normal(size=demo['trial_dur']*fs)
        carrier = np.random.uniform(low=-1, high=1, size=demo['trial_dur']*fs)
        t, waveform, trial_trigs, set_trigs = generate_waveform(carrier, **demo)
        #fill_between(t, waveform, -waveform)
        plot(t, waveform, 'k')
        for trig in set_trigs:
            axvline(trig/fs, color='b', lw=5)
        #for trig in trial_trigs:
        #    axvline(trig/fs, color='b', lw=2.5)
        trig_times = np.true_divide(trial_trigs, fs)
        plot(trig_times, np.ones(len(trig_times)), 'ro')
    prev.xaxis.set_visible(True)
    gcf().subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95,
                          hspace=0.5)
    axis(xmin=-1, ymin=-1.1, ymax=1.1)
    show()
Example #16
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()
Example #17
0
def plot_figure(list, dates, brand):
	
	# Here is for zero padding
	y = []
	#pre_key = sorted(list)[0][0]
	#for key, value in sorted(list):
	#	num_zero = key - pre_key
	#	if num_zero >1 and num_zero <= 30:
	#		for i in xrange(1, num_zero):
	#			y.append(0)
	#	pre_key = key
	#	y.append(value)
	#x = np.arange(0,len(y),1)
	x = [dt.datetime.strptime(d,'%m/%d/%Y').date() for d in dates]
	x = sorted(x)

	for key, value in sorted(list):
		y.append(value)

	pl.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
	pl.gca().xaxis.set_major_locator(mdates.DayLocator(interval=4))
	pl.plot(x,y)
	pl.title("Total Metioned Posts Per Day: " + brand)
	pl.ylabel("Number of Posts")
	pl.gcf().autofmt_xdate()
	pl.show()
Example #18
0
def timeplot(*args,**kwargs):
    import matplotlib.pyplot as plt 
    import matplotlib.ticker 
    
    def HMSFormatter(value, loc): 
        h = value // 3600 
        m = (value - h * 3600) // 60 
        s = value % 60 
        
        return "%02d:%02d:%02d" % (h,m,s) 

    def HMSFormatter2(value, loc): 
        h = value // 3600 
        m = (value - h * 3600) // 60 
        s = value % 60 
        ms=value%1
        
        return "%02d:%02d.%03d" % (m,s,ms*1000) 


    
    t=args[0]
    if max(t)<10:  # use ms
        gca().xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(HMSFormatter2)) 
    else:
        gca().xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(HMSFormatter)) 
    
    
    
    plot(*args,**kwargs) 
    gcf().autofmt_xdate()
Example #19
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 #20
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 #21
0
def show_profile(var='d',func=None,log=True,col=None):
    # pick variable
    if func: data_y = func(data)
    else: data_y = data[var]
    # pick colour
    if col == None: color = color_list[var]
    elif col <= 0: color = color_palette(abs(col))
    else: color = col
    # pick axes
    print "axes = ", pylab.gcf().axes
    if pylab.gcf().axes != []: 
        plot = pylab.gcf().axes[0]
    else: 
        plot = pylab.axes()
        #plot.set_xlabel(legend["r"])
        plot.set_xlabel("r, pc")
        if not func: plot.set_ylabel(legend[var])
    # plot
    rpc = data.points * output.info['unit_length'].express(C.pc)
    if log: plot.semilogy(rpc,data_y,color=color)
    else:   plot.plot(    rpc,data_y,color=color)
    print max(rpc[-1])
    pylab.xlim([0, 8])
    pylab.gcf().canvas.draw()
    return data_y
Example #22
0
def plotNumVariants(depth):
    plt.figure(figsize=(7, 5), dpi=300)
    depth.apply(lambda x: x.dropna().size / 1000.0).unstack("POP").plot.bar(ax=plt.gca())
    plt.gcf().subplots_adjust(bottom=0.30)
    plt.ylabel(r"Num of variants ($\times$1000)")
    renameLegend(ax=plt.gca())
    plt.savefig(pathPlots + "numVariants.pdf")
Example #23
0
def expvaluecollection(evc, show=True, **kwargs):
    """
    Visualize a :class:`pycppqed.expvalues.ExpectationValueCollection`.

    *Usage*
        >>> import numpy as np
        >>> T = np.linspace(0,10)
        >>> d = (np.sin(T), np.cos(T))
        >>> titles = ("<x>", "<y>")
        >>> evc = pycppqed.expvalues.ExpectationValueCollection(d, T, titles)
        >>> evc.plot()

    *Arguments*
        * *evc*
            A :class:`pycppqed.expvalues.ExpectationValueCollection`.

        * *show* (optional):
            If True pylab.show() is called finally. This means a plotting
            window will pop up automatically. (Default is True)

        * Any other arguments that the pylab plotting command can use.
    """
    if evc.subsystems:
        import pylab
        for sysname, data in evc.subsystems.iteritems():
            pylab.figure()
            _expvalues(data.evtrajectories, show=False, **kwargs)
            if hasattr(pylab, "suptitle"): # For old versions not available.
                pylab.suptitle(sysname)
                pylab.gcf().canvas.set_window_title(sysname)
        if show:
            pylab.show()
    else:
        titles = ["(%s) %s" % (i, title) for i, title in enumerate(evc.titles)]
        _expvalues(evc.evtrajectories, titles, show=show, **kwargs)
Example #24
0
def plot_shaded_lines(my_xticks, y1, y2, error1, error2, ylab, xlab, filename):
    plt.figure(figsize=(6,6))
    from matplotlib import rcParams
    rcParams.update({'figure.autolayout': True})

    x = range(0, len(y1))
    plt.plot(x, y1, 'k-', color="blue",  label='men')
    plt.fill_between(x, y1-error1, y1+error1, facecolor='blue', alpha=.2)

    plt.plot(x, y2, 'k-', color="red",  label='women')
    plt.fill_between(x, y2-error2, y2+error2, facecolor='red', alpha=.2)

    #if isinstance(x, (int, long, float, complex)):
    #    plt.xlim(np.min(x), np.max(x))
    plt.gcf().subplots_adjust(bottom=0.3)

    plt.xticks(x, my_xticks)
    plt.xticks(rotation=70, fontsize=14)
    plt.yticks(fontsize=14)
    #plt.setp(ax.get_xticklabels(), rotation='vertical', fontsize=14)
    plt.ylabel(ylab, fontsize=14)
    plt.xlabel(xlab, fontsize=14)
    plt.legend()

    plt.savefig(filename)
Example #25
0
    def acquire_masks(self):
        im1 = self.cam2.get()
        pl.imshow(im1, cmap='gray')
        pl.title('Select Eye')
        pts_eye = pl.ginput(n=0, timeout=0)
        pts_eye = np.array(pts_eye, dtype=np.int32)
        mask_eye = np.zeros(im1.shape, dtype=np.int32)
        cv2.fillConvexPoly(mask_eye, pts_eye, (1,1,1), lineType=cv2.LINE_AA)

        pl.clf()
        
        im2 = self.cam2.get()
        pl.imshow(im2, cmap='gray')
        pl.title('Select Wheel')
        pl.gcf().canvas.draw()
        pts_wheel = pl.ginput(n=0, timeout=0)
        pts_wheel = np.array(pts_wheel, dtype=np.int32)
        mask_wheel = np.zeros(im2.shape, dtype=np.int32)
        cv2.fillConvexPoly(mask_wheel, pts_wheel, (1,1,1), lineType=cv2.LINE_AA)

        pl.close()

        self.mask = np.array([mask_eye, mask_wheel])
        self.mask_flat = self.mask.reshape((2,-1))
        return self.mask
Example #26
0
def compare_expvaluecollections(coll1, coll2, show=True, **kwargs):
    """
    Plot all subsystems of two ExpectationValueCollections.

    *Arguments*
        * *coll1*
            First :class:`pycppqed.expvalues.ExpectationValue.Collection`.

        * *coll2*
            Second :class:`pycppqed.expvalues.ExpectationValue.Collection`.

        * *show* (optional):
            If True pylab.show() is called finally. This means a plotting
            window will pop up automatically. (Default is True)

        * Any other arguments that the pylab plotting command can use.

    This function allows a fast comparison between two sets of expectation
    values that were obtained by different calculations.
    """
    import pylab
    s1 = coll1.subsystems
    s2 = coll2.subsystems
    assert len(s1) == len(s2)
    for i in range(len(s1)):
        pylab.figure()
        _compare_expvaluesubsystems(s1.values()[i], s2.values()[i],
                                        show=False, **kwargs)
        title = "%s vs. %s" % (s1.keys()[i], s2.keys()[i])
        if hasattr(pylab, "suptitle"): # For old versions not available.
            pylab.suptitle(title)
            pylab.gcf().canvas.set_window_title(title)
    if show:
        pylab.show()
Example #27
0
def show_chains(rbm, state, dataset, num_particles=20, num_samples=20, show_every=10, display=True,
                figname='Gibbs chains', figtitle='Gibbs chains'):
    samples = gnp.zeros((num_particles, num_samples, state.v.shape[1]))
    state = state[:num_particles, :, :]

    for i in range(num_samples):
        samples[:, i, :] = rbm.vis_expectations(state.h)
        
        for j in range(show_every):
            state = rbm.step(state)

    npix = dataset.num_rows * dataset.num_cols
    rows = [vm.hjoin([samples[i, j, :npix].reshape((dataset.num_rows, dataset.num_cols)).as_numpy_array()
                      for j in range(num_samples)],
                     normalize=False)
            for i in range(num_particles)]
    grid = vm.vjoin(rows, normalize=False)

    if display:
        pylab.figure(figname)
        pylab.matshow(grid, cmap='gray', fignum=False)
        pylab.title(figtitle)
        pylab.gcf().canvas.draw()

    return grid
Example #28
0
 def plot(self, key, graph_data, num, row, col, ax):
     pylab.subplots_adjust(wspace=.5, hspace=.5)
     pylab.title("Region %s" % str(key))
     pylab.setp(ax.get_yticklabels(), visible=False)
     pylab.setp(ax.get_xticklabels(), visible=False)
     pylab.gcf().set_size_inches((row+col, row+col))
     h = pylab.hist(graph_data, bins=self.bins)
Example #29
0
def cycles_vs_align_len(classData, blastPath, floworder):
    pylab.clf()         
    l = []
    c = []
    for rowcol,leng,dif,seq in classData:
        t = template.deseqify(remove_spaces(seq), floworder)
        l.append(dif)
        c.append(len(t)/4)
    pylab.scatter(l,c)
    if "/" in blastPath:
        p = blastPath.strip().split("/")[-1]
        path = p.strip().split(".")[0]
    else:
        path = blastPath.strip().split(",")[0]
    if 'blastn' in path:
        pretitle = 'Relaxed Blast'
    if 'megablast' in path:
        pretitle = 'Strict Blast'
    pylab.title("%s Number of Flows vs. Alignment Length" % pretitle)
    pylab.xlabel("Length")
    pylab.ylabel("Flows")
    pylab.gcf().set_size_inches((8,4))
    pylab.axis([0,max(l)+10,0,max(c)+10])
    #x = [2.2*i for i in range(max(l))]
    #pylab.plot(x)
    pylab.savefig("alignment_vs_cycles_%s.png" % path)
    pylab.clf()
Example #30
0
def plot_compare(t, u, v, fig_title='', file_name=''):
    """
    Compare two signals and plot the difference between them.

    Parameters
    ----------
    t : ndarray of floats
        Times (s) at which the signal is defined.
    u, v : ndarrays of floats
        Signal samples.
    fig_title : string
        Plot title.
    file_name : string
        File in which to save the plot.

    """

    p.clf()
    p.gcf().canvas.set_window_title(fig_title)
    p.subplot(211)
    p.plot(t, u, 'b', t, v, 'r')
    p.xlabel('t (s)')
    p.ylabel('u(t)')
    p.title(fig_title)
    p.gca().set_xlim(min(t), max(t))
    p.subplot(212)
    p.plot(t, 20*np.log10(abs(u-v)))
    p.xlabel('t (s)')
    p.ylabel('error (dB)')
    p.gca().set_xlim(min(t), max(t))
    p.draw_if_interactive()
    if file_name:
        p.savefig(file_name)
Example #31
0
    if next_event_arg < len(singles):
        collision_disk, direction = singles[next_event_arg]
        vel[collision_disk][direction] *= -1.0
    else:
        a, b = pairs[next_event_arg - len(singles)]
        del_x = [pos[b][0] - pos[a][0], pos[b][1] - pos[a][1]]
        abs_x = math.sqrt(del_x[0] ** 2 + del_x[1] ** 2)
        e_perp = [c / abs_x for c in del_x]
        del_v = [vel[b][0] - vel[a][0], vel[b][1] - vel[a][1]]
        scal = del_v[0] * e_perp[0] + del_v[1] * e_perp[1]
        for k in range(2):
            vel[a][k] += e_perp[k] * scal
            vel[b][k] -= e_perp[k] * scal

pylab.subplots_adjust(left=0.10, right=0.90, top=0.90, bottom=0.10)
pylab.gcf().set_size_inches(6, 6)
img = 0
if not os.path.exists(output_dir): os.makedirs(output_dir)

N=4
histo_data = []

conf_a = ((0.30, 0.30), (0.30, 0.70), (0.70, 0.30), (0.70,0.70))
conf_b = ((0.20, 0.20), (0.20, 0.80), (0.75, 0.25), (0.75,0.75))
conf_c = ((0.30, 0.20), (0.30, 0.80), (0.70, 0.20), (0.70,0.70))
configurations = [conf_a, conf_b, conf_c]
hits = {conf_a: 0, conf_b: 0, conf_c: 0}
del_xy = 0.10

pos = [[0.25, 0.25], [0.75, 0.25], [0.25, 0.75], [0.75, 0.75]]
vel = [[0.21, 0.12], [0.71, 0.18], [-0.23, -0.79], [0.78, 0.1177]]
Example #32
0
PyCont['EQ2'].display((bifPar, 'xB'), axes=(4, 1, 2), stability=True)
PyCont['EQ3'].display((bifPar, 'xB'), axes=(4, 1, 2), stability=True)
plt.title('')
plt.ylabel('$x_B^*$', fontsize=18)
plt.xlabel('')
#plt.xlim([15,35])

PyCont['EQ1'].display((bifPar, 'BM'), axes=(4, 1, 3), stability=True)
PyCont['EQ2'].display((bifPar, 'BM'), axes=(4, 1, 3), stability=True)
PyCont['EQ3'].display((bifPar, 'BM'), axes=(4, 1, 3), stability=True)
plt.title('')
plt.ylabel('$z^*$', fontsize=18)
#plt.xlim([15,35])

PyCont['EQ1'].display((bifPar, 'xM'), axes=(4, 1, 4), stability=True)
PyCont['EQ2'].display((bifPar, 'xM'), axes=(4, 1, 4), stability=True)
PyCont['EQ3'].display((bifPar, 'xM'), axes=(4, 1, 4), stability=True)
plt.title('')
plt.ylabel('$x_M^*$', fontsize=18)
#plt.xlim([15,35])

plt.xlabel('$\\beta_T$', fontsize=18)

PyCont.plot.toggleLabels('off')
PyCont.plot.togglePoints('off')
PyCont.plot.togglePoints(visible='on', bylabel='H1')

fig = plt.gcf()
fig.set_size_inches(3, 5)
plt.tight_layout()
#plt.savefig("met-bif-bT-mixed.pdf")
Example #33
0
        else:
            fmt = "%.3f"

        numpy.savetxt(opts["--output"], mat.filled(0), fmt=fmt)
    else:
        font_kwargs = {
            "family": opts["--font-family"],
            "size": float(opts["--font-size"])
        }

        init_pylab(font_kwargs)

        # hide all the spines i.e. no axes are drawn
        init_spines(hidden=["top", "bottom", "left", "right"])

        pylab.gcf().set_figwidth(float(opts["--width-inches"]))
        pylab.gcf().set_figheight(float(opts["--height-inches"]))

        pylab.xlim(0, len(residues))
        pylab.ylim(0, len(residues))

        pylab.xlabel(opts["--xlabel"])
        pylab.ylabel(opts["--ylabel"])

        ax, fig = pylab.gca(), pylab.gcf()

        if opts["--show-frame"]:
            init_spines(hidden=[])

        if opts["cmap"] or opts["hbmap"]:
            map_obj = pylab.pcolormesh(mat,
Example #34
0
def diagnose_interactions(df):
    print("\n=== Interactions table, original shape={} ===\n".format(df.shape))
    df = df.copy()
    df['ITEM_ID'] = df['ITEM_ID'].astype(str)
    df['USER_ID'] = df['USER_ID'].astype(str)
    df.index = df["TIMESTAMP"].values.astype("datetime64[s]")

    na_rate = df[INTERACTIONS_REQUIRED_FIELDS].isnull().any(axis=1).mean()
    print("missing rate in fields", INTERACTIONS_REQUIRED_FIELDS, na_rate)
    if na_rate > NA_RATE_THRESHOLD:
        warnings.warn(
            "High data missing rate for required fields ({:.1%})!".format(
                na_rate))
    df = df.dropna(subset=INTERACTIONS_REQUIRED_FIELDS)
    print("dropna shape", df.shape)

    dup_rate = (df.groupby(INTERACTIONS_REQUIRED_FIELDS).size() -
                1.0).sum() / df.shape[0]
    print("duplication rate", dup_rate)
    if dup_rate > DUP_RATE_THRESHOLD:
        warnings.warn("""
        High duplication rate ({:.1%})!
        Only one event can be taken at the same (user,item,timestamp) index.
        """.format(dup_rate))
    df = df.drop_duplicates(subset=INTERACTIONS_REQUIRED_FIELDS)
    print("drop_duplicates shape", df.shape)

    repeat_rate = (df.groupby(["USER_ID", "ITEM_ID"]).size() -
                   1.0).sum() / df.shape[0]
    print("user item repeat rate", repeat_rate)
    if repeat_rate > REPEAT_RATE_THRESHOLD:
        warnings.warn("""
        High rate of repeated consumptions ({:.1%})!
        We would not do anything, but it may beneficial to
        (1) consider keeping only the last interaction between the same user-item pair,
        (2) consider if the ITEM_IDs have collisions, and/or
        (3) use high-order hierarchical models.
        """.format(repeat_rate))

    summary = describe_dataframe(df, 'interactions table')

    print("\n=== Hourly activity pattern ===")
    print(df.groupby(df.index.hour).size())

    print("\n=== Day of week activity pattern ===")
    print(df.groupby(df.index.dayofweek).size())

    plot_patterns = {
        "date": df.index.date,
        "hour": df.index.hour,
        "dayofweek": df.index.dayofweek
    }

    for k, v in plot_patterns.items():
        pl.plot(df.groupby(v).size(), '.-')
        pl.gcf().autofmt_xdate()
        pl.title("Activity pattern by %s" % k)
        pl.grid()
        pl.show()

    print("\n=== Temporal shift analysis ===\n")
    print(
        "Sorting and removing repeated user-items for temporal shift analysis..."
    )
    df.sort_index(inplace=True, kind='mergesort')
    df_dedup = df.drop_duplicates(['USER_ID', 'ITEM_ID'], keep='last')

    print("\n=== Temporal shift - retrain frequency ===\n")

    for method in TEMPORAL_LOSS_METHODS:
        bootstrap_avg = []
        past_fut_avg = []
        for freq in RETRAIN_FREQUENCY:
            _, _, _bs_avg, loss_fmt = compute_bootstrap_loss(
                df_dedup, freq, method)
            _, _, _ts_avg, loss_fmt = compute_temporal_loss(
                df_dedup, freq, method, 1)
            bootstrap_avg.append(_bs_avg)
            past_fut_avg.append(_ts_avg)
        pl.plot(RETRAIN_FREQUENCY,
                bootstrap_avg,
                '.--',
                label='same-period bootstrap')
        pl.plot(RETRAIN_FREQUENCY,
                past_fut_avg,
                '.-',
                label='lagged popularity')
        pl.legend()
        pl.xlabel('retrain frequency')
        pl.title(method + ' loss at different frequencies')
        pl.grid()
        pl.gca().yaxis.set_major_formatter(
            pl.FuncFormatter(lambda y, _: loss_fmt.format(y)))
        pl.show()

    print("\n=== Temporal shift - history cutoffs ===\n")

    for method in TEMPORAL_LOSS_METHODS:
        for freq in TEMPORAL_FREQUENCY:
            bootstrap_loss, _, avg_loss, loss_fmt = compute_bootstrap_loss(
                df_dedup, freq, method)
            pl.plot(bootstrap_loss.iloc[-TEMPORAL_PLOT_LIMIT:],
                    '.--',
                    label='boostrap baseline={}'.format(
                        loss_fmt.format(avg_loss)))

            for hist_len in ROLLING_HISTORY_LEN:
                temporal_loss, df_wgt, avg_loss, loss_fmt = compute_temporal_loss(
                    df_dedup, freq, method, hist_len)

                pl.plot(temporal_loss.iloc[-TEMPORAL_PLOT_LIMIT:],
                        '.-',
                        label='hist={} * {}, avg={}'.format(
                            hist_len, freq, loss_fmt.format(avg_loss)))

            pl.gca().yaxis.set_major_formatter(
                pl.FuncFormatter(lambda y, _: loss_fmt.format(y)))

            pl.title('{} {} from rolling history (lower is better)'.format(
                freq, method))
            pl.grid()
            pl.gcf().autofmt_xdate()
            pl.legend(loc='upper left')
            pl.twinx()
            pl.plot(df_wgt.iloc[-TEMPORAL_PLOT_LIMIT:],
                    color='grey',
                    lw=3,
                    ls='--',
                    alpha=0.5)
            pl.legend(['activity density'], loc='upper right')
            pl.show()

    print("\n=== session time delta describe ===")

    user_time_delta = df.groupby('USER_ID')["TIMESTAMP"].transform(
        pd.Series.diff).dropna()
    user_time_delta.sort_values(ascending=False, inplace=True)
    print(user_time_delta.describe())
    plot_loglog(user_time_delta, 'session time delta', show=False)
    for k, v in TIMEDELTA_REFERENCES:
        if pl.ylim()[0] < v < pl.ylim()[1]:
            pl.plot(pl.xlim(), [v, v], '--')
            pl.text(pl.xlim()[0], v, k)
    pl.show()

    user_time_span = df.groupby('USER_ID')["TIMESTAMP"].apply(
        lambda x: max(x) - min(x))
    user_time_span.sort_values(ascending=False, inplace=True)
    print("=== user time span describe ===")
    print(user_time_span.describe())
    plot_loglog(user_time_span, 'user time span', show=False)
    for k, v in TIMEDELTA_REFERENCES:
        if pl.ylim()[0] < v < pl.ylim()[1]:
            pl.plot(pl.xlim(), [v, v], '--')
            pl.text(pl.xlim()[0], v, k)
    pl.show()
Example #35
0
def analyze_boundaries(annot_dir, trim, annotators):
    """Analyzes the annotated boundaries.

    Parameters
    ----------
    annot_dir: str
        Path to the annotations directory where all the jams file reside.
    trim: boolean
        Whether to trim the first and last boundaries.
    annotators: dict
        Dictionary containing the names and e-mail addresses of the 5
        different annotators.
    """
    jams_files = glob.glob(os.path.join(annot_dir, "*.jams"))
    dtype = [('P3', float), ('R3', float), ('F3', float), ('P05', float),
             ('R05', float), ('F05', float), ('D', float), ('DevA2E', float),
             ('DevE2A', float), ('track_id', '<U400')]
    track_ids = get_track_ids(annot_dir)
    mma_humans_file = "mma_experiment_humans.pk"
    mgp_humans_file = "mgp_experiment_humans.pk"

    # Compute the MMA human results
    logging.info("Computing the MMA...")
    #mma_results = []
    #for jam_file in jams_files:
    #mma_file = compute_mma_results(jam_file, annotators, trim, 250)
    #mma_results.append(np.mean(mma_file, axis=0))
    #for i in xrange(len(track_ids)):
    #mma_results[i] = mma_results[i].tolist()
    #mma_results[i].append(track_ids[i])
    #mma_results[i] = tuple(mma_results[i])
    #mma_results = np.asarray(mma_results, dtype=dtype)
    #pickle.dump(mma_results, open(mma_humans_file, "w"))
    mma_results = pickle.load(open(mma_humans_file, "r"))

    # Compute the MGP human results (not really necessary)
    #logging.info("Computing the MGP...")
    #mgp_results = compute_mgp(jams_files, annotators, trim)
    #mgp_results = mgp_results.tolist()
    #for i in xrange(len(track_ids)):
    #mgp_results[i].append(track_ids[i])
    #mgp_results[i] = tuple(mgp_results[i])
    #mgp_results = np.asarray(mgp_results, dtype=dtype)
    #pickle.dump(mgp_results, open(mgp_humans_file, "w"))
    #mgp_results = pickle.load(open(mgp_humans_file, "r"))

    # Get MGP machine results
    mgp_results_machine = pickle.load(
        open("../notes/mgp_experiment_machine.pk", "r"))

    mgp_results_machine = np.sort(mgp_results_machine, order="track_id")
    mma_results = np.sort(mma_results, order='track_id')

    for mgp_res, mma_res in zip(mgp_results_machine, mma_results):
        print mgp_res["F3"], mma_res["F3"], mgp_res["track_id"], \
            mma_res["track_id"]

    # Plot results
    figsize = (4, 4)
    plt.figure(1, figsize=figsize, dpi=120, facecolor='w', edgecolor='k')
    plt.scatter(mgp_results_machine["F3"], mma_results["F3"])
    plt.plot([0, 1], [0, 1])
    plt.gca().set_ylabel("Human MMA$_{F3}$ results")
    plt.gca().set_xlabel("Machine MGP$_{F3}$ results")
    plt.gca().set_xticks(np.arange(0, 1.1, .1))
    plt.gca().set_yticks(np.arange(0, 1.1, .1))
    plt.gca().set_xlim(0, 1)
    plt.gca().set_ylim(0, 1)
    plt.gcf().subplots_adjust(bottom=0.12, left=0.15)

    alpha = 0.3
    # Best-humans, worse-machines
    circle = mpatches.Circle([0.3, 0.79],
                             0.1,
                             ec="none",
                             color="g",
                             alpha=alpha)
    plt.gca().add_artist(circle)

    # Best-humans, worse-machines
    ellipse = mpatches.Ellipse([0.84, 0.84],
                               0.1,
                               0.26,
                               ec="none",
                               color="r",
                               alpha=alpha)
    plt.gca().add_artist(ellipse)

    # Worse-humans, worse-machines
    rectangle = mpatches.Rectangle([0.2, 0.3],
                                   0.2,
                                   0.2,
                                   ec="none",
                                   color="m",
                                   alpha=alpha)
    plt.gca().add_artist(rectangle)

    plt.show()
Example #36
0
def normaliza(wave, flux):
    """
    Normaliza
    =====
    La función hace un ploteo interactivo de wave (eje x) y flux (eje y). En ese ploteo se pueden marcar
    puntos que conformarán el continuum de nuestro espectro, normalizar el espectro a partir del continuum
    y guardar en un archivo txt los datos del eje x e y del espectrograma normalizado

    Parameters
    ----------
    -wave: array de la longitud de onda de nuestro espectro

    -flux: array de la luminosidad de nuestro espectro

    Returns
    ----------
    Archivo .txt con los datos de longitud de onda y luminosidad

    Notes
    ---------
    Estos arrays deben ser del mismo tamaño.

    """
    if wave.shape != flux.shape:
        raise Exception(
            "Los arrays de longitud de onda y de luminosidad deben tener el mismo tamaño"
        )

    def onclick(event):
        """
        Donde el usuario haga click con el boton izquierdo del raton, 
        se ploteara un pto del continuum de su eleccion. 

        El tamaño del punto esta determinado por ms.

        Picker es un parámetro de área que se usa en la función onpick
        """
        if event.button == 1:
            plt.plot(event.xdata,
                     event.ydata,
                     'r.',
                     ms=10,
                     picker=5,
                     label='nuevoPunto')
        plt.draw()

    def onpick(event):
        """
        Si el usuario hace click con el boton derecho del raton
        cerca (rango determinado por el parámetro picker de onclick) 
        de un punto creado anteriormente, se eliminará el mismo 
        (o los que estén en el rango)
        
        """
        if event.mouseevent.button == 3:
            if hasattr(
                    event.artist,
                    'get_label') and event.artist.get_label() == 'nuevoPunto':
                event.artist.remove()

    def ontype(event):
        """
        Cuando el usuario pulse "enter" tras haber seleccionado los puntos
        deseados de continuum, se creará un spline de tercer grado que representa
        el continuum. Se pueden añadir más puntos después y al volver a pulsar 
        "enter" se actualizará el spline.

        Cuando pulse "n" se realizará la normalización respecto del spline o continuum.

        Si pulsa "g" los valores de Luminosidad relativa y longitud de onda se guardarán en un
        archivo .txt de nombre normalizacion.

        Si pulsa "r" se volverá a la forma original del espectro eliminando los puntos y spline.

        Si pulsa "a" el programa hace la normalización automáticamente, sun necesidad de crear
        manualmente el continuum.

        Si pulsa "t" el programa realiza la normalización de forma teórica con la función de Plank
        para cuerpos oscuros.

        """

        # Ploteo Continuum
        if event.key == 'enter':
            new_pto_coord = []
            for artist in plt.gca().get_children():
                if hasattr(artist,
                           'get_label') and artist.get_label() == 'nuevoPunto':
                    new_pto_coord.append(artist.get_data())
                elif hasattr(
                        artist,
                        'get_label') and artist.get_label() == 'continuum':
                    artist.remove()
            new_pto_coord = np.array(new_pto_coord)[..., 0]
            sort_array = np.argsort(new_pto_coord[:, 0])
            x, y = new_pto_coord[sort_array].T
            spline = splrep(x, y, k=3)
            continuum = splev(wave, spline)
            plt.plot(wave, continuum, 'r-', lw=2, label='continuum')

            # Temperatura de la estrella. Obtenida con la Ley de Wien para cuerpos negros
            max_lux = np.amax(continuum)
            wave_index_max = np.where(continuum == np.amax(continuum))

            temperatura = 29000000.0 / float(wave[wave_index_max])
            print("Temperatura aproximada de la estrella (K) =", temperatura)

        # Normalizacion
        elif event.key == 'n':
            continuum = None
            for artist in plt.gca().get_children():
                if hasattr(artist,
                           'get_label') and artist.get_label() == 'continuum':
                    continuum = artist.get_data()[1]
                    break
            if continuum is not None:
                plt.cla()
                plt.plot(wave,
                         flux / continuum,
                         'k-',
                         label='normalizacion',
                         lw=0.3)

            # Guardado de "seguridad" por si el usuario no guarda la normalización
            for artist in plt.gca().get_children():
                if hasattr(
                        artist,
                        'get_label') and artist.get_label() == 'normalizacion':
                    datos_normalizar = np.array(artist.get_data())
                    np.savetxt("normalizacion.txt", datos_normalizar.T)
                    break

        # Normalizado Teórico
        elif event.key == 't':
            data = np.zeros_like((wave, flux))
            data[0, :], data[1, :] = flux, wave
            auxList = []

            # Temperatura de la estrella. Obtenida con la Ley de Wien para cuerpos negros
            pseudocontinuum = medfilt(data[0, :], 301)
            pseudocontinuumNoError = pseudocontinuum[50:-50]
            max_lux = max(pseudocontinuum)
            wave_index_max = np.amin(np.where(pseudocontinuum == max_lux))
            wave_max = data[1, wave_index_max]
            temperatura = 29000000.0 / float(wave_max)
            print("Temperatura aproximada de la estrella (K) =", temperatura)

            # Obtención del espectro de cuerpo oscuro y noramliación de la función.
            continuum = plank(data[1, :], temperatura - 100)
            continuum_noError = continuum[50:-50]
            normData = data[0, 50:-50] / pseudocontinuumNoError
            plt.cla()
            plt.plot(data[1, 50:-50],
                     normData,
                     'k-',
                     lw=0.3,
                     label='normalizacion')

            # Guardado de "seguridad" por si el usuario no guarda la normalización
            for artist in plt.gca().get_children():
                if hasattr(
                        artist,
                        'get_label') and artist.get_label() == 'normalizacion':
                    datos_normalizar = np.array(artist.get_data())
                    np.savetxt("normalizacion.txt", datos_normalizar.T)
                    break

        # Normalizado Automático
        elif event.key == 'a':
            data = np.zeros_like((wave, flux))
            data[0, :], data[1, :] = flux, wave
            auxList = []

            pseudocontinuum = medfilt(data[0, :], 301) + 0.06
            pseudocontinuumNoError = pseudocontinuum[50:-50]
            normData = data[0, 50:-50] / pseudocontinuumNoError
            plt.cla()
            plt.plot(data[1, 50:-50],
                     normData,
                     'k-',
                     lw=0.3,
                     label='normalizacion')

            # Temperatura de la estrella. Obtenida con la Ley de Wien para cuerpos negros
            max_lux = max(pseudocontinuum)
            wave_index_max = np.amin(np.where(pseudocontinuum == max_lux))
            wave_max = data[1, wave_index_max]
            temperatura = 29000000.0 / float(wave_max)
            print("Temperatura aproximada de la estrella (K) =", temperatura)

            # Guardado de "seguridad" por si el usuario no guarda la normalización
            for artist in plt.gca().get_children():
                if hasattr(
                        artist,
                        'get_label') and artist.get_label() == 'normalizacion':
                    datos_normalizar = np.array(artist.get_data())
                    np.savetxt("normalizacion.txt", datos_normalizar.T)
                    break

        # Guardado del espectrograma normalizado.
        elif event.key == 'g':
            for artist in plt.gca().get_children():
                if hasattr(
                        artist,
                        'get_label') and artist.get_label() == 'normalizacion':
                    datos_normalizar = np.array(artist.get_data())
                    np.savetxt("normalizacion.txt", datos_normalizar.T)
                    print(
                        'Se ha guardado la normalización en el archivo normalizacion.txt'
                    )
                    break

        # Reinicio del proceso, vuelve a plotear el espectrograma original
        elif event.key == 'r':
            plt.cla()
            plt.plot(wave, flux, 'k-', lw=0.3)

        plt.draw()

    #Conectamos las funciones interactivas con sus respectivos eventos

    plt.gcf().canvas.mpl_connect('key_press_event', ontype)
    plt.gcf().canvas.mpl_connect('button_press_event', onclick)
    plt.gcf().canvas.mpl_connect('pick_event', onpick)

    spectrum = plt.plot(wave, flux, 'k-', lw=0.3)
    plt.show()

    return
haploid_color = '#08519c'

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

# When it 1 then 2
#pylab.figure(1,figsize=(4.5, 2))
#fig = pylab.gcf()
#outer_grid  = gridspec.GridSpec(1,2, width_ratios=[3,1.3],wspace=0.4)
#right_grid = gridspec.GridSpecFromSubplotSpec(2,1, height_ratios=[1,1],hspace=0.5,subplot_spec=outer_grid[1])

pylab.figure(1,figsize=(5, 3))
fig = pylab.gcf()
outer_grid  = gridspec.GridSpec(1,1)

pylab.figure(2,figsize=(5, 3))
fig2 = pylab.gcf()
outer_grid2  = gridspec.GridSpec(1,1)


###########################################
#
# Do calculations and plotting for panel D (dN/dS vs dS)
#
###########################################


syn_differences = {}
Example #38
0
def plot_hodograph(data, parameters, fname=None, web=False, fixed=False, archive=False):
    img_title = "%s VWP valid %s" % (data.rid, data['time'].strftime("%d %b %Y %H%M UTC"))
    if fname is not None:
        img_file_name = fname
    else:
        img_file_name = "%s_vad.png" % data.rid

    u, v = vec2comp(data['wind_dir'], data['wind_spd'])

    sat_age = 6 * 3600
    if fixed or len(u) == 0:
        # TJT
        ctr_u, ctr_v = 20, 0
        #ctr_u, ctr_v = 20, 20
        size = 120
        #size = 100
    else:
        ctr_u = u.mean()
        ctr_v = v.mean()
        size = max(u.max() - u.min(), v.max() - v.min()) + 20
        size = max(120, size)

    min_u = ctr_u - size / 2
    max_u = ctr_u + size / 2
    min_v = ctr_v - size / 2
    max_v = ctr_v + size / 2

    now = datetime.utcnow()
    img_age = now - data['time']
    age_cstop = min(_total_seconds(img_age) / sat_age, 1) * 0.4
    age_color = mpl.cm.get_cmap('hot')(age_cstop)[:-1]

    age_str = "Image created on %s (%s old)" % (now.strftime("%d %b %Y %H%M UTC"), _fmt_timedelta(img_age))

    pylab.figure(figsize=(10, 7.5), dpi=150)
    fig_wid, fig_hght = pylab.gcf().get_size_inches()
    fig_aspect = fig_wid / fig_hght

    axes_left = 0.05
    axes_bot = 0.05
    axes_hght = 0.9
    axes_wid = axes_hght / fig_aspect
    pylab.axes((axes_left, axes_bot, axes_wid, axes_hght))

    _plot_background(min_u, max_u, min_v, max_v)
    _plot_data(data, parameters)
    _plot_param_table(parameters, web=web)

    pylab.xlim(min_u, max_u)
    pylab.ylim(min_v, max_v)
    pylab.xticks([])
    pylab.yticks([])

    if not archive:
        pylab.title(img_title, color=age_color)
        pylab.text(0., -0.01, age_str, transform=pylab.gca().transAxes, ha='left', va='top', fontsize=9, color=age_color)
    else:
        pylab.title(img_title)

    if web:
        web_brand = "http://www.autumnsky.us/vad/"
        pylab.text(1.0, -0.01, web_brand, transform=pylab.gca().transAxes, ha='right', va='top', fontsize=9)

    pylab.savefig(img_file_name, dpi=pylab.gcf().dpi)
    pylab.close()

    if web:
        bounds = {'min_u':min_u, 'max_u':max_u, 'min_v':min_v, 'max_v':max_v}
        print(json.dumps(bounds)) 
Example #39
0
P = []
for i in range(0, swarmsize):
    P.append(Particle())

gBest_fitness = float('inf')
gBest_position = [None] * len(P[0].getCurrPosition())

# matplotlib inline
import time
import pylab as pl
from IPython import display

if (D == 1):
    pl.plot(function_to_predict["X"], function_to_predict["Y"])
    display.clear_output(wait=True)
    display.display(pl.gcf())
    time.sleep(1.0)

if (D == 2):
    ax = plt.axes(projection='3d')
    ax.plot3D(
        function_to_predict["X"], function_to_predict["Y"],
        function_to_predict["Z"], 'o'
    )  #,function_to_predict["X"], function_to_predict["Y"], function_to_predict["Z"], '-o')
    display.clear_output(wait=True)
    display.display(pl.gcf())
    time.sleep(1.0)

#PSO

old_y = []
def main():
    assert args.train_filename is not None

    # モデル
    model = ithmm.model()
    if model.load(os.path.join(args.model, "ithmm.model")) == False:
        raise Exception("モデルが見つかりません.")

    # 単語辞書
    dictionary = ithmm.dictionary()
    dictionary.load(os.path.join(args.model, "ithmm.dict"))

    tagger = treetaggerwrapper.TreeTagger(TAGLANG="en")
    all_types_of_pos = set()
    state_sequence_array_true = []  # 真の品詞列
    state_sequence_array_viterbi = []  # ビタビアルゴリズムによる推定

    def append(f):
        for i, line in enumerate(f):
            line = line.strip()  # 改行を消す
            results = tagger.tag_text(line)  # 形態素解析
            word_ids = []
            state_sequence_true = []
            for result_str in results:
                pos, word = parse_tagger_result_str(result_str)
                all_types_of_pos.add(pos)
                word_ids.append(dictionary.string_to_word_id(word))
                state_sequence_true.append(pos)
            state_sequence_array_true.append(state_sequence_true)
            state_sequence_viterbi = model.viterbi_decode(word_ids)
            assert len(state_sequence_viterbi) == len(state_sequence_true)
            state_sequence_array_viterbi.append(state_sequence_viterbi)

    # 読み込み
    if args.train_filename.endswith(".txt"):
        with codecs.open(args.train_filename, "r", "utf-8") as f:
            append(f)
    else:
        train_dir = args.train_filename
        files = os.listdir(train_dir)
        for filename in files:
            with codecs.open(os.path.join(train_dir, filename), "r",
                             "utf-8") as f:
                append(f)

    # モデルの予測品詞と正解品詞の対応関係
    num_occurrence_of_pos_for_tag = {}
    for state_sequence_true, state_sequence_viterbi in zip(
            state_sequence_array_true, state_sequence_array_viterbi):
        for pos_true, tag_viterbi in zip(state_sequence_true,
                                         state_sequence_viterbi):
            if tag_viterbi not in num_occurrence_of_pos_for_tag:
                num_occurrence_of_pos_for_tag[tag_viterbi] = {}
            if pos_true not in num_occurrence_of_pos_for_tag[tag_viterbi]:
                num_occurrence_of_pos_for_tag[tag_viterbi][pos_true] = 0
            num_occurrence_of_pos_for_tag[tag_viterbi][pos_true] += 1

    tags = model.get_all_states()

    # 存在しない部分を0埋め
    for tag, occurrence in num_occurrence_of_pos_for_tag.items():
        for pos in all_types_of_pos:
            if pos not in occurrence:
                occurrence[pos] = 0

    for tag in tags:
        if tag not in num_occurrence_of_pos_for_tag:
            num_occurrence_of_pos_for_tag[tag] = {}
            for pos in all_types_of_pos:
                num_occurrence_of_pos_for_tag[tag][pos] = 0

    # 正解品詞ごとに正規化
    for pos in all_types_of_pos:
        z = 0
        for tag, occurrence in num_occurrence_of_pos_for_tag.items():
            z += occurrence[pos]
        if z > 0:
            for tag, occurrence in num_occurrence_of_pos_for_tag.items():
                occurrence[pos] = float(occurrence[pos]) / float(z)

    fig = pylab.gcf()
    fig.set_size_inches(len(tags) + 3, len(all_types_of_pos))
    pylab.clf()
    dataframe = pd.DataFrame(num_occurrence_of_pos_for_tag)
    ax = sns.heatmap(dataframe, annot=False, fmt="f", linewidths=0)
    ax.tick_params(labelsize=20)
    plt.yticks(rotation=0)
    plt.xticks(rotation=90)
    plt.xlabel(u"予測タグ")
    plt.ylabel(u"正解品詞")
    heatmap = ax.get_figure()
    heatmap.savefig("{}/pos.png".format(args.model))
Example #41
0
def curvedEarthAxes(rect=111,
                    fig=None,
                    minground=0.,
                    maxground=2000,
                    minalt=0,
                    maxalt=500,
                    Re=6371.,
                    nyticks=5,
                    nxticks=4):
    """ Create curved axes in ground-range and altitude
      
  **Args**: 
      * [**rect**]: subplot spcification
      * [**fig**]: A pylab.figure object (default to gcf)
      * [**maxground**]: maximum ground range [km]
      * [**minalt**]: lowest altitude limit [km]
      * [**maxalt**]: highest altitude limit [km]
      * [**Re**]: Earth radius
  **Returns**:
      * **ax**: matplotlib.axes object containing formatting
      * **aax**: matplotlib.axes object containing data
  **Example**:
      ::

          import numpy as np
          from utils import plotUtils
          ax, aax = plotUtils.curvedEarthAxes()
          th = np.linspace(0, ax.maxground/ax.Re, 50)
          r = np.linspace(ax.Re+ax.minalt, ax.Re+ax.maxalt, 20)
          Z = exp( -(r - 300 - ax.Re)**2 / 100**2 ) * np.cos(th[:, np.newaxis]/th.max()*4*np.pi)
          x, y = np.meshgrid(th, r)
          im = aax.pcolormesh(x, y, Z.T)
          ax.grid()

          
  written by Sebastien, 2013-04
  """
    from matplotlib.transforms import Affine2D, Transform
    import mpl_toolkits.axisartist.floating_axes as floating_axes
    from matplotlib.projections import polar
    from mpl_toolkits.axisartist.grid_finder import FixedLocator, DictFormatter
    import numpy as np
    from pylab import gcf

    ang = maxground / Re
    minang = minground / Re
    angran = ang - minang
    angle_ticks = [(0, "{:.0f}".format(minground))]
    while angle_ticks[-1][0] < angran:
        tang = angle_ticks[-1][0] + 1. / nxticks * angran
        angle_ticks.append((tang, "{:.0f}".format((tang - minang) * Re)))
    grid_locator1 = FixedLocator([v for v, s in angle_ticks])
    tick_formatter1 = DictFormatter(dict(angle_ticks))

    altran = maxalt - minalt
    alt_ticks = [(minalt + Re, "{:.0f}".format(minalt))]
    while alt_ticks[-1][0] < Re + maxalt:
        alt_ticks.append(
            (1. / nyticks * altran + alt_ticks[-1][0],
             "{:.0f}".format(altran * 1. / nyticks + alt_ticks[-1][0] - Re)))
    _ = alt_ticks.pop()
    grid_locator2 = FixedLocator([v for v, s in alt_ticks])
    tick_formatter2 = DictFormatter(dict(alt_ticks))

    tr_rotate = Affine2D().rotate(np.pi / 2 - ang / 2)
    tr_shift = Affine2D().translate(0, Re)
    tr = polar.PolarTransform() + tr_rotate

    grid_helper = floating_axes.GridHelperCurveLinear(
        tr,
        extremes=(0, angran, Re + minalt, Re + maxalt),
        grid_locator1=grid_locator1,
        grid_locator2=grid_locator2,
        tick_formatter1=tick_formatter1,
        tick_formatter2=tick_formatter2,
    )

    if not fig: fig = gcf()
    ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)

    # adjust axis
    ax1.axis["left"].label.set_text(r"ALt. [km]")
    ax1.axis["bottom"].label.set_text(r"Ground range [km]")
    ax1.invert_xaxis()

    ax1.minground = minground
    ax1.maxground = maxground
    ax1.minalt = minalt
    ax1.maxalt = maxalt
    ax1.Re = Re

    fig.add_subplot(ax1, transform=tr)

    # create a parasite axes whose transData in RA, cz
    aux_ax = ax1.get_aux_axes(tr)

    aux_ax.patch = ax1.patch  # for aux_ax to have a clip path as in ax
    ax1.patch.zorder = 0.9  # but this has a side effect that the patch is
    # drawn twice, and possibly over some other
    # artists. So, we decrease the zorder a bit to
    # prevent this.

    return ax1, aux_ax
Example #42
0
for i in range(len(names))[::-1]:
    axes[i].set_position([l0, b0 + hh * i, ww, hh])

# Resize labels/ticks
for i in range(len(axes)):
    ax = axes[i]

    ax.tick_params(axis='both',
                   which='major',
                   labelsize=20,
                   size=8.,
                   width=1.5,
                   pad=8.)
    ax.tick_params(axis='both',
                   which='minor',
                   labelsize=20,
                   size=5.,
                   width=1.5)

    if i != 0: ax.tick_params(axis='x', which='major', labelbottom='off')

axes[0].set_xlabel(r"$k \,[\mathrm{Mpc}^{-1}]$",
                   fontdict={'fontsize': 'xx-large'},
                   labelpad=10.)
#ax.set_ylabel(r"$P(k)$", fontdict={'fontsize':'20'})

# Set size
P.gcf().set_size_inches(8.5, 10.)
P.savefig('ska-fbao.pdf', transparent=True)
P.show()
            if row[3] != '0':
                gene_number.append(row[0])
                expression_in_exp_form.append(float(row[1]))
                total_intron_length.append(float(row[3]))
                transcript_lengths.append(float(row[4]))

### Setting font size for all graphs in this program
size_font = 16

### Combined control data and intron length data for density operation
### and compute the density values as variable z
comb = [expression_in_exp_form, total_intron_length]
z = gaussian_kde(comb)(comb)

### Plot Data
plt.figure(5, figsize=(9, 6.75))
fig = pylab.gcf()
plt.scatter(expression_in_exp_form,
            total_intron_length,
            c=z,
            s=50,
            edgecolors='')
fig.canvas.set_window_title('')
plt.title('C. Elegan - Total Intron Length vs Gene Expression',
          fontsize=size_font)
plt.ylabel('Total Intron Length (nucleotides)', fontsize=size_font)
plt.xlabel('Expression (number of expression sequence tag hits)',
           fontsize=size_font)
plt.grid()
plt.show(block=True)
Example #44
0
def _get_fig(fi=None):
    if fi is not None:
        myfig = PL.figure(fi)
    else:
        myfig = PL.gcf()
    return myfig
Example #45
0
            plot_stiffness_field(k_cart_list[-1],plottitle='end')


    str_parts = fname.split('.')
    if d.has_key('strategy'):
        addon = ''
        if opt.xy:
            addon = '_xy'
        if opt.xz:
            addon = '_xz'
        fig_name = str_parts[0]+'_'+d['strategy']+addon+'.png'
    else:
        fig_name = str_parts[0]+'_res.png'

    if expt_plot:
        f = pl.gcf()
        curr_size = f.get_size_inches()
        f.set_size_inches(curr_size[0]*2,curr_size[1]*2)
        f.savefig(fig_name)

    if show_fig:
        pl.show()
    else:
        print '################################'
        print 'show_fig is FALSE'
        if not(expt_plot):
            pl.savefig(fig_name)

    if xyz_flag:
        plot_cartesian(traj, xaxis=0,yaxis=1,zaxis=2)
        mlab.show()
Example #46
0
def mollview(map=None,
             fig=None,
             rot=None,
             coord=None,
             unit='',
             xsize=800,
             title='Mollweide view',
             nest=False,
             min=None,
             max=None,
             flip='astro',
             remove_dip=False,
             remove_mono=False,
             gal_cut=0,
             format='%g',
             format2='%g',
             cbar=True,
             cmap=None,
             notext=False,
             norm=None,
             hold=False,
             margins=None,
             sub=None):
    """Plot an healpix map (given as an array) in Mollweide projection.
    
    Input:
      - map : an ndarray containing the map
              if None, use map with inf value (white map), useful for
              overplotting
    Parameters:
      - fig: a figure number. Default: create a new figure
      - rot: rotation, either 1,2 or 3 angles describing the rotation
             Default: None
      - coord: either one of 'G', 'E' or 'C' to describe the coordinate
               system of the map, or a sequence of 2 of these to make
               rotation from the first to the second coordinate system.
               Default: None
      - unit: a text describing the unit. Default: ''
      - xsize: the size of the image. Default: 800
      - title: the title of the plot. Default: 'Mollweide view'
      - nest: if True, ordering scheme is NEST. Default: False (RING)
      - min: the minimum range value
      - max: the maximum range value
      - flip: 'astro' (default, east towards left, west towards right) or 'geo'
      - remove_dip: if True, remove the dipole+monopole
      - remove_mono: if True, remove the monopole
      - gal_cut: galactic cut for the dipole/monopole fit
      - format: the format of the scale label. Default: '%g'
      - format2: format of the pixel value under mouse. Default: '%g'
      - cbar: display the colorbar. Default: True
      - notext: if True, no text is printed around the map
      - norm: color normalization, hist= histogram equalized color mapping, log=
              logarithmic color mapping, default: None (linear color mapping)
      - hold: if True, replace the current Axes by a MollweideAxes.
              use this if you want to have multiple maps on the same
              figure. Default: False
      - sub: use a part of the current figure (same syntax as subplot).
             Default: None
      - margins: either None, or a sequence (left,bottom,right,top)
                 giving the margins on left,bottom,right and top
                 of the axes. Values are relative to figure (0-1).
                 Default: None
    """
    # Create the figure
    if not (hold or sub):
        f = pylab.figure(fig, figsize=(8.5, 5.4))
        extent = (0.02, 0.05, 0.96, 0.9)
    elif hold:
        f = pylab.gcf()
        left, bottom, right, top = npy.array(f.gca().get_position()).ravel()
        extent = (left, bottom, right - left, top - bottom)
        f.delaxes(f.gca())
    else:  # using subplot syntax
        f = pylab.gcf()
        if hasattr(sub, '__len__'):
            nrows, ncols, idx = sub
        else:
            nrows, ncols, idx = sub / 100, (sub % 100) / 10, (sub % 10)
        if idx < 1 or idx > ncols * nrows:
            raise ValueError('Wrong values for sub: %d, %d, %d' %
                             (nrows, ncols, idx))
        c, r = (idx - 1) % ncols, (idx - 1) / ncols
        if not margins:
            margins = (0.01, 0.0, 0.0, 0.02)
        extent = (c * 1. / ncols + margins[0],
                  1. - (r + 1) * 1. / nrows + margins[1],
                  1. / ncols - margins[2] - margins[0],
                  1. / nrows - margins[3] - margins[1])
        extent = (extent[0] + margins[0], extent[1] + margins[1],
                  extent[2] - margins[2] - margins[0],
                  extent[3] - margins[3] - margins[1])
        #extent = (c*1./ncols, 1.-(r+1)*1./nrows,1./ncols,1./nrows)
    #f=pylab.figure(fig,figsize=(8.5,5.4))

    # Starting to draw : turn interactive off
    wasinteractive = pylab.isinteractive()
    pylab.ioff()
    try:
        if map is None:
            map = npy.zeros(12) + npy.inf
            cbar = False
        ax = PA.HpxMollweideAxes(f,
                                 extent,
                                 coord=coord,
                                 rot=rot,
                                 format=format2,
                                 flipconv=flip)
        f.add_axes(ax)
        if remove_dip:
            map = pixelfunc.remove_dipole(map,
                                          gal_cut=gal_cut,
                                          nest=nest,
                                          copy=True,
                                          verbose=True)
        elif remove_mono:
            map = pixelfunc.remove_monopole(map,
                                            gal_cut=gal_cut,
                                            nest=nest,
                                            copy=True,
                                            verbose=True)
        ax.projmap(map,
                   nest=nest,
                   xsize=xsize,
                   coord=coord,
                   vmin=min,
                   vmax=max,
                   cmap=cmap,
                   norm=norm)
        if cbar:
            im = ax.get_images()[0]
            b = im.norm.inverse(npy.linspace(0, 1, im.cmap.N + 1))
            v = npy.linspace(im.norm.vmin, im.norm.vmax, im.cmap.N)
            if matplotlib.__version__ >= '0.91.0':
                cb = f.colorbar(ax.get_images()[0],
                                ax=ax,
                                orientation='horizontal',
                                shrink=0.5,
                                aspect=25,
                                ticks=PA.BoundaryLocator(),
                                pad=0.05,
                                fraction=0.1,
                                boundaries=b,
                                values=v,
                                format=format)
            else:
                # for older matplotlib versions, no ax kwarg
                cb = f.colorbar(ax.get_images()[0],
                                orientation='horizontal',
                                shrink=0.5,
                                aspect=25,
                                ticks=PA.BoundaryLocator(),
                                pad=0.05,
                                fraction=0.1,
                                boundaries=b,
                                values=v,
                                format=format)
        ax.set_title(title)
        if not notext:
            ax.text(0.86,
                    0.05,
                    ax.proj.coordsysstr,
                    fontsize=14,
                    fontweight='bold',
                    transform=ax.transAxes)
        if cbar:
            cb.ax.text(0.5,
                       -1.0,
                       unit,
                       fontsize=14,
                       transform=cb.ax.transAxes,
                       ha='center',
                       va='center')
        f.sca(ax)
    finally:
        pylab.draw()
        if wasinteractive:
            pylab.ion()
Example #47
0
        seg = segs[ii]
        if seg.timebase[0] > start_time:
            #            print("normalise = %s" % normalise)
            if (normalise != 0) and (normalise != '0'):
                # the old code used to change seg, even at the beginning of a long chain.
                seg_proc = seg.subtract_mean().normalise(normalise, separate)
            else:
                seg_proc = seg.subtract_mean()

            outsvd = seg_proc.svd()
            ax_list = outsvd.svdplot(hold=hold, init_but_list=init_but_list)

            try:  # plotting mag
                if verbose: print(outsvd.history)
                if plot_mag and (seg_proc.scales is not None):
                    fig = pl.gcf()
                    oldtop = fig.subplotpars.top
                    fig.subplots_adjust(top=0.65)
                    ax = pl.axes([0.63, 0.75, 0.35, 0.15])
                    #                    ax=pl.subplot(8,2,-2) # try to put plot on top: doesn't work in new version
                    xticks = range(len(seg_proc.scales))
                    if pyfusion.VERBOSE > 3:
                        print('scales', len(seg_proc.scales), seg_proc.scales)
                    pl.bar(xticks, seg_proc.scales, align='center')
                    ax.set_xticks(xticks)
                    # still confused - sometimes the channels are the names bdb
                    try:
                        seg_proc.channels[0].name
                        names = [sgch.name for sgch in seg_proc.channels]
                    except:
                        names = seg_proc.channels
Example #48
0
def scaling():

    procs_mpi = numpy.zeros(4, numpy.int)
    avg_mpi = numpy.zeros(4, numpy.float64)

    procs_omp6 = numpy.zeros(4, numpy.int)
    avg_omp6 = numpy.zeros(4, numpy.float64)

    procs_omp12 = numpy.zeros(4, numpy.int)
    avg_omp12 = numpy.zeros(4, numpy.float64)

    # all the following is 768^3 timings, with one block per MPI task

    # pure MPI
    procs_mpi[:] = [1728, 4096, 13824, 32768]
    avg_mpi[:] = [39.6, 21.1, 16.3, 30.3]

    # 6 OpenMP threads per MPI task
    procs_omp6[:] = [1296, 3072, 10368, 24576]
    avg_omp6[:] = [60.1, 26.7, 9.9, 6.5]

    # 12 OpenMP threads per MPI task
    procs_omp12[:] = [2592, 6144, 20736, 49152]
    avg_omp12[:] = [57.3, 22.5, 8.3, 4.9]

    ax = pylab.subplot(111)
    ax.set_xscale('log')
    ax.set_yscale('log')

    pylab.plot(procs_mpi,
               avg_mpi,
               ms=6,
               mew=0,
               marker="s",
               c="r",
               ls="solid",
               label=r"pure MPI")

    ptemp = numpy.arange(2) * procs_mpi[len(procs_mpi) - 1] + float(
        procs_mpi[0])
    pylab.plot(ptemp,
               avg_mpi[0] * (procs_mpi[0] / ptemp),
               "r:",
               label="_nolegend_")

    pylab.plot(procs_omp6,
               avg_omp6,
               ms=6,
               mew=0,
               marker="o",
               c="b",
               ls="solid",
               label=r"6 OpenMP threads / MPI task")

    ptemp = numpy.arange(2) * procs_omp6[len(procs_omp6) - 1] + float(
        procs_omp6[0])
    pylab.plot(ptemp,
               avg_omp6[0] * (procs_omp6[0] / ptemp),
               "b:",
               label="_nolegend_")

    pylab.plot(procs_omp12,
               avg_omp12,
               ms=6,
               mew=0,
               marker="^",
               c="g",
               ls="solid",
               label=r"12 OpenMP threads / MPI task")

    ptemp = numpy.arange(2) * procs_omp12[len(procs_omp12) - 1] + float(
        procs_omp12[0])
    pylab.plot(ptemp,
               avg_omp12[0] * (procs_omp12[0] / ptemp),
               "g:",
               label="_nolegend_")

    pylab.xlabel("# of processors")
    pylab.ylabel("time to advance timestep")

    leg = pylab.legend(loc=3)
    ltext = leg.get_texts()
    pylab.setp(ltext, fontsize='small')
    leg.draw_frame(0)

    pylab.axis([1000, 100000, 1, 100])

    pylab.title(r"MAESTRO WD convection (768^3) scaling on Jaguar XT5",
                fontsize=11)

    f = pylab.gcf()
    f.set_size_inches(6.0, 6.0)

    pylab.savefig("jaguar_xt5_scaling.eps")
    # Fix ticks
    if j == 1:
        axes[i].yaxis.tick_right()
        axes[i].yaxis.set_label_position("right")
    if i % 2 == 1:
        for tick in axes[i].xaxis.get_major_ticks():
            tick.label1.set_visible(False)
    if i % 2 == 1: j += 1

    # Hide alternating ticks
    kk = 0
    for tick in axes[i].yaxis.get_major_ticks():
        if kk % 2 == 1:
            tick.label1.set_visible(False)
            tick.label2.set_visible(False)
        kk += 1

# Manually add shared x label
P.figtext(0.5, 0.02, "$z$", fontdict={'size': 'xx-large'})

# Legend
P.legend(bbox_to_anchor=[0.93, -0.04], frameon=False)

# Set size
P.gcf().set_size_inches(10., 7.)
if not PLOT_DIFFERENT_MEASURES:
    #pass
    P.savefig('fig06-zfns.pdf', transparent=True)
    #P.savefig('BINGO-zfns.pdf', transparent=True)
P.show()
Example #50
0
ax.text(max(ticks_array) + offset_border, -7.5, "IL", ha='left', fontsize=6)
ax.text(max(ticks_array) + offset_border, -15, "RL", ha='left', fontsize=6)
ax.text(max(ticks_array) + offset_border, -23, "reward", ha='left', fontsize=6)
pl.axvline(max(ticks_array) + offset_border - 0.32,
           ymin=-0.25,
           ymax=0,
           color='k',
           clip_on=False,
           lw=0.5)
# ax.set_xticks(x_pos + bar_width / dist_bars)
# ax.set_xticklabels(model_names, rotation=40)
ax.set_ylabel("ratio in [\%]")
ax.legend((success_bars[0], timeout_bars[0], crash_bars[0]),
          ("success", "timeout", "crash"),
          ncol=1,
          fancybox=True,
          framealpha=0.5,
          bbox_to_anchor=(0.15, 1.7))

# pl.tight_layout()
pl.gcf().subplots_adjust(bottom=0.17,
                         top=0.65,
                         left=0.065,
                         right=0.95,
                         hspace=0.12)
# data.plot.bar(stacked=True)

pl.show(block=False)

fig.savefig("figures/test_map_eval.pdf")
Example #51
0
        else:
            while data_lengths[-i] < L_series[L_series_index]:
                L_series_index += 1
                data_lengths_binned.append(0)
            data_lengths_binned[-1] += data_non_cumul[-i]
    for i in range(1, len(L_series)):
        data_lengths_binned[i] *= 1.0 / (total_length *
                                         (-L_series[i] + L_series[i - 1]))
    data_lengths_binned[0] *= 1.0 / total_length
    #    print 'total length:', total_length
    return data_lengths_binned, total_length


YRI_lengths_binned, total_length = bin_dataset(open(tractfile))
plt.loglog(L_series, YRI_lengths_binned, label=tractfile)
plt.loglog(L_series[:-1], y5, label='Inferred history')
plt.legend(loc='lower left')

plt.xlabel('IBS tract length (L)')

plt.ylim(ymin=10**(-16))
plt.xlim(xmin=1)
plt.xlim(xmax=10**6)
plt.xscale('log')
plt.yscale('log')

F = pylab.gcf()
F.set_size_inches(6.83, 7)
rc('font', **{'family': 'sans-serif', 'sans-serif': ['Arial'], 'size': 5})
plt.savefig(output_file, dpi=300, format='pdf')
Example #52
0
    return (dataNorth, dataSouth)


if __name__ == '__main__':

    (path, run, t0, t1) = parseArgs()

    (dataNorth, dataSouth) = extractQuantities(path, run, t0, t1)

    # --- Dump a  pickle!
    print(
        'Serializing pyLTR.TimeSeries object of MIX data using the Python Pickle package.'
    )
    filename = os.path.join(path, 'mixSparTimeSeries.pkl')
    print(('Writing ' + filename))
    fh = open(filename, 'wb')
    pickle.dump([dataNorth, dataSouth], fh, protocol=2)
    fh.close()

    # --- Make a plot of everything
    print('Creating summary plot of MIX time series data.')
    filename = os.path.join(path, 'mixSparTimeSeries.png')
    print(('Writing ' + filename))
    pyLTR.Graphics.TimeSeries.MultiPlotN([dataNorth, dataSouth], 'datetime',
                                         ['ispar', 'hp', 'aspar'], ['r', 'b'],
                                         ['North', 'South'])
    pylab.gcf().autofmt_xdate()
    pylab.title(os.path.join(path, run))
    pylab.savefig(filename, dpi=1200)
def do_plot_final_hist(Controller, xTandemInput, xtPpmNew, isFinalPlots):
    lims = Controller.updatedSettings['plottingPars']['plotting range, ppm']
    lims = tuple([float(i) for i in lims.split(',')])
    binSize = float(Controller.updatedSettings['plottingPars']['histogram bin size, ppm'])

    dSetName = Controller.updatedSettings['spectra dataset']
    dirName = Controller.updatedSettings['spectra directory']
    pathName = os.path.join(dirName, dSetName) + '_HIST.png'

    ppmInd = list(xTandemInput[0]).index('massErrorPpm')
    xtPpmOld = array([float(i[ppmInd]) for i in xTandemInput[1:]])

    bins = arange(lims[0], lims[1] + binSize, binSize)

    if isFinalPlots:
        figure(num=None, figsize=(10, 5))
        subplots_adjust(top=0.875, bottom=0.175, left=0.1, right=0.925, wspace=0.1)

    # some info in the bottom left corner
    fineprint = 'dataset: %s\ntotal number of identifications: %s\nbin size: %s ppm' \
                % (dSetName, len(xTandemInput), binSize)
    t = gcf().text(0.05, 0.075, fineprint,
                   horizontalalignment='left',
                   verticalalignment='top',
                   fontproperties=FontProperties(size=6))

    # go through NEW ppm error
    mean1, stdev1, portion1, mean2, stdev2 = do_em_norm_n_fixed_norm(xtPpmNew)
    dist1 = portion1 * dnorm(bins, mean1, stdev1)
    dist2 = (1 - portion1) * dnorm(bins, mean2, stdev2)
    total = binSize * len(xtPpmNew) * (dist1 + dist2)
    #
    meanMAD = median(xtPpmNew)
    stdevMAD = 1.4826 * median(abs(xtPpmNew - meanMAD))
    totalMAD = binSize * len(xtPpmNew) * dnorm(bins, meanMAD, stdevMAD)
    # plot histogram with NEW
    yLim = None
    statParsNew = dict(zip(['Exp.Max.', 'Robust'],
                           [dict(zip(['mean', 'stdev'], [mean1, stdev1])),
                            dict(zip(['mean', 'stdev'], [meanMAD, stdevMAD]))]
                           )
                       )
    totalEMNew, totalMADNew = total, totalMAD
    if isFinalPlots:
        yLim, binCountNew = subHist(122, xtPpmNew, bins, lims, total, mean1, stdev1, totalMAD, meanMAD, stdevMAD,
                                    'refined', yLim)

    # go through OLD ppm error
    mean1, stdev1, portion1, mean2, stdev2 = do_em_norm_n_fixed_norm(xtPpmOld)
    dist1 = portion1 * dnorm(bins, mean1, stdev1)
    dist2 = (1 - portion1) * dnorm(bins, mean2, stdev2)
    total = binSize * len(xtPpmOld) * (dist1 + dist2)
    #
    meanMAD = median(xtPpmOld)
    stdevMAD = 1.4826 * median(abs(xtPpmOld - meanMAD))
    totalMAD = binSize * len(xtPpmOld) * dnorm(bins, meanMAD, stdevMAD)
    # plot histogram with OLD
    statParsOri = dict(zip(['Exp.Max.', 'Robust'],
                           [dict(zip(['mean', 'stdev'], [mean1, stdev1])),
                            dict(zip(['mean', 'stdev'], [meanMAD, stdevMAD]))]
                           )
                       )
    if isFinalPlots:
        totalEMOri, totalMADOri = total, totalMAD
        yLim, binCountOri = subHist(121, xtPpmOld, bins, lims, total, mean1, stdev1, totalMAD, meanMAD, stdevMAD,
                                    'original', yLim)

    if isFinalPlots:
        savefig(str(pathName))
        clf()

    # KEEP GOING AND MAKE RESIDUAL ERROR
    # SCATTER PLOTS
    if isFinalPlots:
        dimensions = Controller.allDimensions
        for dim in dimensions:
            ind = list(xTandemInput[0]).index(dim)
            par = array([float(i[ind]) for i in xTandemInput[1:]])
            #
            # some info in the bottom left corner
            fineprint = 'dataset: %s' \
                        % (dSetName)
            t = gcf().text(0.05, 0.075, fineprint,
                           horizontalalignment='left',
                           verticalalignment='top',
                           fontproperties=FontProperties(size=6))
            #
            bothScatterPlots(par, xtPpmOld, xtPpmNew, lims, dim)
            #
            pathName = os.path.join(dirName, dSetName) + '_%s.png' % dim
            savefig(str(pathName))
            clf()

    # pass the statistics
    statPars = {}
    statPars['new'] = statParsNew
    statPars['ori'] = statParsOri
    Controller.statPars = statPars

    # save the histograms as text
    # bins - array
    # binCountNew - array
    # binCountOri - array
    # totalEMNew, totalMADNew 
    # totalEMOri, totalMADOri
    if isFinalPlots:
        # The binCountOri and binCountNew arrays are likely 1 item shorter than bins
        # The while loops correct for this

        while len(binCountOri) < len(bins):
            binCountOri = append(binCountOri, [0])

        while len(binCountNew) < len(bins):
            binCountNew = append(binCountNew, [0])

        data = vstack((bins, binCountOri, totalEMOri, totalMADOri, binCountNew, totalEMNew, totalMADNew)).T
        data = around(data, 1)
        titles = ['MassErrorBin (ppm)', 'Original', 'OriginalFitEM', 'OriginalFitRobust',
                  'Refined', 'RefinedFitEM', 'RefinedFitRobust']
        data = vstack((titles, data))
        histTxtPath = os.path.join(dirName, dSetName) + '_HIST.txt'
        savetxt(histTxtPath, data, fmt='%s', delimiter='\t')
xp, yp = map(lon, lat)

s = pylab.text(xp, yp, "+Y", color="b", zorder=10)

# <<< point on +x >>>
x = 1
y = 0
z = 0

(lon, lat) = convert_xyz_to_lonlat(x, y, z)

xp, yp = map(lon, lat)

s = pylab.text(xp, yp, "+X", color="b", zorder=10)

f = pylab.gcf()
f.set_size_inches(6.0, 8.0)

pylab.savefig("test_sphere_proj_octant.png")

#-----------------------------------------------------------------------------
# full sphere data
#-----------------------------------------------------------------------------

pylab.clf()

# see http://www.scipy.org/Cookbook/Matplotlib/Maps

# here, the lat_0 and lon_0 specify the point directly under
# the viewer (only needed for some projections)
Example #55
0
    
    print labels[k]
    for i in range(len(err)):
        print "%3.3f %4.4f" % (zc[i], err[i])

    
# Subplot labels
P.gca().tick_params(axis='both', which='major', labelsize=18, width=1.5, size=8., pad=7.)
P.gca().tick_params(axis='both', which='minor', labelsize=18, width=1.5, size=5.)

# Set axis limits
P.xlim((-0.02, 2.5))
P.ylim((0., 0.045))

P.xlabel('$z$', labelpad=10., fontdict={'fontsize':'xx-large'})
P.ylabel("$\sigma_{D_V}/D_V$", labelpad=15., fontdict={'fontsize':'xx-large'})
    
## Set tick locations
P.gca().yaxis.set_major_locator(matplotlib.ticker.MultipleLocator(0.01))
P.gca().yaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(0.005))

leg = P.legend(loc='upper right', prop={'size':'medium'}, frameon=True, ncol=2)
leg.get_frame().set_alpha(0.8)
leg.get_frame().set_edgecolor('w')

# Set size
P.gcf().set_size_inches(9.5, 6.8)
P.tight_layout()
P.savefig(filename, transparent=True)
P.show()
        ]
        fileroot = [
            "evol_std_solar_m", "evol_std_.2solar_m", "evol_std_2solar_m"
        ]
        filemass = massarr[imass]

        filename = location[metalicity] + fileroot[metalicity] + filemass

        DataIn = np.genfromtxt(filename, dtype="float", unpack=True)

        Teff = 10**DataIn[6, :]
        L = DataIn[5, :]

        pylab.plot(Teff / 10**4, L, color='black')
        pylab.xlim([7, 0])

    pylab.title(titles[metalicity])
    #show all of the tracks that have been plotted
    #then save the image

    pylab.ylabel("Log[L]")
    #pylab.title("Evolutionary tracks for solar metalicity stars")
    if metalicity == 0:
        pylab.xlabel(r"T$_{\rm eff}$  [x10$^5$]")
pylab.gcf().subplots_adjust(bottom=0.27)
saveLoc = "../../Images/"
saveName = "Evol_solar.png"

pylab.savefig("OldTracks.ps")
#pylab.show()
Example #57
0
import pylab as pl
import matplotlib.pyplot as plt
import numpy as np
import random
import math

rnumber = random.randint(0, 100)
X = np.linspace(rnumber - 10, rnumber + 10, 360, endpoint=True)
Y = np.sin(X)
plt.xlabel('Number generated: ' + str(rnumber))
plt.ylabel('Sinus for Number')
plt = pl.gcf()
plt.canvas.set_window_title('Random Number Sinus Plotter')
pl.plot(rnumber, math.sin(rnumber), 'o')
pl.plot(X, Y)
pl.show()
clip.plot.scatter(c='trailingWorstDrawdown',
                  y='futureMaxLoss',
                  ax=axlo,
                  **same)
clip.plot.scatter(c='trailingWorstDrawdown',
                  y='futureMaxGain',
                  ax=axhi,
                  **same)
axlo.tick_params(axis='x', bottom=True, labelbottom=True)
axlo.set_xlabel('Trailing {}-session pct change'.format(trailingShort))  # ??
axlo.set_ylabel('Future {}-session lowest pct change'.format(future))
axhi.set_ylabel('Future {}-session highest pct change'.format(future))
axhi.set_title('S&P500, 1928–present (data: Yahoo Finance)')
[
    x.set_ylabel('Trailing {}-session worst drawdown'.format(trailingLong))
    for x in plt.gcf().get_axes()[2:]
]
axlo.tick_params(axis='x', bottom=True, labelbottom=True)
axlo.xaxis.get_label().set_visible(True)

# clip.plot.hexbin(x='trailingPctChange', y='futureMaxLoss')

# Plotting the worst loss over a period (for potential put strategy)
predWindows = [21]
futureWindow = 9 + 1
worstdf, predcols, current = makePredictorDataframeMaxLoss(
    spx, predWindows, futureWindow, rollworst)
bestdf, _, _ = makePredictorDataframeMaxLoss(spx, predWindows, futureWindow,
                                             rollbest)
print('CURRENTLY', current)
Example #59
0
def cartview(map=None,
             fig=None,
             rot=None,
             zat=None,
             coord=None,
             unit='',
             xsize=800,
             ysize=None,
             lonra=None,
             latra=None,
             title='Cartesian view',
             nest=False,
             remove_dip=False,
             remove_mono=False,
             gal_cut=0,
             min=None,
             max=None,
             flip='astro',
             format='%.3g',
             cbar=True,
             cmap=None,
             norm=None,
             aspect=None,
             hold=False,
             sub=None,
             margins=None,
             notext=False):
    """Plot an healpix map (given as an array) in Cartesian projection.

    Input:
      - map : an ndarray containing the map.
              if None, use map with inf value (white map), useful for
              overplotting
    Parameters:
      - fig: a figure number. Default: create a new figure
      - rot: rotation, either 1,2 or 3 angles describing the rotation
             Default: None
      - coord: either one of 'G', 'E' or 'C' to describe the coordinate
               system of the map, or a sequence of 2 of these to make
               rotation from the first to the second coordinate system.
               Default: None
      - unit: a text describing the unit. Default: ''
      - xsize: the size of the image. Default: 200
      - lonra: range in longitude. Default: [-180,180]
      - latra: range in latitude. Default: [-90,90]
      - title: the title of the plot. Default: 'Mollweide view'
      - nest: if True, ordering scheme is NEST. Default: False (RING)
      - min: the minimum range value
      - max: the maximum range value
      - flip: 'astro' (default, east towards left, west towards right) or 'geo'
      - remove_dip: if True, remove the dipole+monopole
      - remove_mono: if True, remove the monopole
      - gal_cut: galactic cut for the dipole/monopole fit
      - format: the format of the scale. Default: '%.3g'
      - hold: if True, replace the current Axes by a MollweideAxes.
              use this if you want to have multiple maps on the same
              figure. Default: False
      - sub: use a part of the current figure (same syntax as subplot).
             Default: None
      - margins: either None, or a sequence (left,bottom,right,top)
                 giving the margins on left,bottom,right and top
                 of the axes. Values are relative to figure (0-1).
                 Default: None
      - notext: True: do not add resolution info text
                Default=False
    """
    if not (hold or sub):
        f = pylab.figure(fig, figsize=(8.5, 5.4))
        if not margins:
            margins = (0.075, 0.05, 0.075, 0.05)
        extent = (0.0, 0.0, 1.0, 1.0)
    elif hold:
        f = pylab.gcf()
        left, bottom, right, top = npy.array(
            pylab.gca().get_position()).ravel()
        if not margins:
            margins = (0.0, 0.0, 0.0, 0.0)
        extent = (left, bottom, right - left, top - bottom)
        f.delaxes(pylab.gca())
    else:  # using subplot syntax
        f = pylab.gcf()
        if hasattr(sub, '__len__'):
            nrows, ncols, idx = sub
        else:
            nrows, ncols, idx = sub / 100, (sub % 100) / 10, (sub % 10)
        if idx < 1 or idx > ncols * nrows:
            raise ValueError('Wrong values for sub: %d, %d, %d' %
                             (nrows, ncols, idx))
        c, r = (idx - 1) % ncols, (idx - 1) / ncols
        if not margins:
            margins = (0.01, 0.0, 0.0, 0.02)
        extent = (c * 1. / ncols + margins[0],
                  1. - (r + 1) * 1. / nrows + margins[1],
                  1. / ncols - margins[2] - margins[0],
                  1. / nrows - margins[3] - margins[1])
    extent = (extent[0] + margins[0], extent[1] + margins[1],
              extent[2] - margins[2] - margins[0],
              extent[3] - margins[3] - margins[1])

    #f=pylab.figure(fig,figsize=(5.5,6))
    # Starting to draw : turn interactive off
    wasinteractive = pylab.isinteractive()
    pylab.ioff()
    try:
        if map is None:
            map = npy.zeros(12) + npy.inf
            cbar = False
        if zat and rot:
            raise ValueError('Only give rot or zat, not both')
        if zat:
            rot = npy.array(zat, dtype=npy.float64)
            rot.resize(3)
            rot[1] -= 90
        ax = PA.HpxCartesianAxes(f,
                                 extent,
                                 coord=coord,
                                 rot=rot,
                                 format=format,
                                 flipconv=flip)
        f.add_axes(ax)
        if remove_dip:
            map = pixelfunc.remove_dipole(map,
                                          gal_cut=gal_cut,
                                          nest=nest,
                                          copy=True)
        elif remove_mono:
            map = pixelfunc.remove_monopole(map,
                                            gal_cut=gal_cut,
                                            nest=nest,
                                            copy=True)
        ax.projmap(map,
                   nest=nest,
                   coord=coord,
                   vmin=min,
                   vmax=max,
                   xsize=xsize,
                   ysize=ysize,
                   lonra=lonra,
                   latra=latra,
                   cmap=cmap,
                   norm=norm,
                   aspect=aspect)
        if cbar:
            if matplotlib.__version__ >= '0.91.0':
                cb = f.colorbar(ax.get_images()[0],
                                ax=ax,
                                orientation='horizontal',
                                shrink=0.5,
                                aspect=25,
                                ticks=PA.BoundaryLocator(),
                                pad=0.08,
                                fraction=0.1,
                                format=format)
            else:
                cb = f.colorbar(ax.get_images()[0],
                                orientation='horizontal',
                                shrink=0.5,
                                aspect=25,
                                ticks=PA.BoundaryLocator(),
                                pad=0.08,
                                fraction=0.1,
                                format=format)
        ax.set_title(title)
        if not notext:
            ax.text(-0.07,
                    0.6,
                    ax.proj.coordsysstr,
                    fontsize=14,
                    fontweight='bold',
                    rotation=90,
                    transform=ax.transAxes)
        if cbar:
            cb.ax.text(1.05,
                       0.30,
                       unit,
                       fontsize=14,
                       fontweight='bold',
                       transform=cb.ax.transAxes,
                       ha='left',
                       va='center')
        f.sca(ax)
    finally:
        if wasinteractive:
            pylab.ion()
            pylab.draw()
Example #60
0
def plot_ndpanel(panel,
                 func=None,
                 xlim='auto',
                 ylim='auto',
                 row_labels='auto',
                 col_labels='auto',
                 row_name='auto',
                 col_name='auto',
                 pass_slicing_meta_to_func=False,
                 subplot_xlabel=None,
                 subplot_ylabel=None,
                 row_name_pad=40.0,
                 col_name_pad=40.0,
                 hspace=0,
                 wspace=0,
                 hide_tick_labels=True,
                 hide_tick_lines=False,
                 legend=None,
                 legend_title=None,
                 grid_kwargs={},
                 **kwargs):
    """Use to visualize mutli-dimensional data stored in N-dimensional pandas panels.

    Given an nd-panel of shape (.., .., .., rows, cols), the function creates a 2d grid of subplot
    of shape (rows, cols). subplot i, j calls func parameter with an (n-2) nd panel
    that corresponds to (..., .., .., i, j).

    Parameters
    ---------------
    panel : pandas Panel (3d-5d.. indexing is hard coded at the moment)
        items : time series generated along this axis
        major : rows
        minor : cols
    func : function that accepts a slice of a panel (two dimensions less than input panel)
    {_graph_grid_layout}

    pass_slicing_meta_to_func : [False | True]
        Changes the arguments that are passed to the provided function.
        If False: func(data_slice, **kwargs) (Default)
        If True: func(data_slice, row=row, col=col, row_value=row_value, col_value=col_value, **kwargs)

    grid_kwargs : dict
        kwargs to be passed to the create_grid_layout method. See its documentation for further details.
    legend : None, tuple
        If provided as tuple, must be a 2-d tuple corresponding to a subplot position.
        If legend=(2, 4), then the legend will drawn using the labels of the lines provided
        in subplot in 2nd row and 4th column.
        A better name could be subplot_source_for_legend?
    legend_title : str, None
        If provided, used as title for the legend.

    Returns
    ---------------

    Reference to main axis and to subplot axes.

    Examples
    ----------------

    if a is a panel:

    plot_panel(a, func=plot, marker='o');

    Code that could be useful
    ---------------------------

    # Checks number of arguments function accepts
    if func.func_code.co_argcount == 1:
        func(data)
    else:
        func(data, ax)

    """
    auto_col_name, auto_col_labels, auto_row_name, auto_row_labels = extract_annotation(
        panel)
    shape = panel.values.shape
    rowNum, colNum = shape[-2], shape[
        -1]  # Last two are used for setting up the size
    ndim = len(shape)

    if ndim < 2 or ndim > 5:
        raise Exception('Only dimensions between 2 and 5 are supported')

    if row_labels == 'auto':
        row_labels = auto_row_labels
    if col_labels == 'auto':
        col_labels = auto_col_labels

    # Figure out xlimits and y limits
    axis = ''  # used below to autoscale subplots

    if xlim == 'auto':
        xlim = None
        axis += 'x'
    if ylim == 'auto':
        ylim = None
        axis += 'y'

    ax_main, ax_subplots = create_grid_layout(
        rowNum=rowNum,
        colNum=colNum,
        row_labels=row_labels,
        col_labels=col_labels,
        xlabel=subplot_xlabel,
        ylabel=subplot_ylabel,
        hide_tick_labels=hide_tick_labels,
        hide_tick_lines=hide_tick_lines,
        xlim=xlim,
        ylim=ylim,
        hspace=hspace,
        wspace=wspace,
        **grid_kwargs)

    nrange = arange(ndim)
    nrange = list(
        nrange[(nrange - 2) %
               ndim])  # Moves the last two dimensions to the first two

    if not isinstance(panel, pandas.DataFrame):
        panel = panel.transpose(*nrange)

    for (row, col), ax in numpy.ndenumerate(ax_subplots):
        plt.sca(ax)

        data_slice = panel.iloc[row].iloc[col]
        row_value = panel.axes[0][row]
        col_value = panel.axes[1][col]

        if pass_slicing_meta_to_func:
            func(data_slice,
                 row=row,
                 col=col,
                 row_value=row_value,
                 col_value=col_value,
                 **kwargs)
        else:
            func(data_slice, **kwargs)

    autoscale_subplots(ax_subplots, axis)

    plt.sca(ax_main)

    if legend is not None:
        items, labels = ax_subplots[legend].get_legend_handles_labels()

        # lines = ax_subplots[legend].lines
        # l = pl.legend(lines , map(lambda x : x.get_label(), lines),
        l = pl.legend(items,
                      labels,
                      bbox_to_anchor=(0.9, 0.5),
                      bbox_transform=pl.gcf().transFigure,
                      loc='center left',
                      numpoints=1,
                      frameon=False)
        if legend_title is not None:
            l.set_title(legend_title)

    if row_name == 'auto':
        row_name = auto_row_name
    if col_name == 'auto':
        col_name = auto_col_name
    if row_name is not None:
        pl.xlabel(col_name, labelpad=col_name_pad)
    if col_name is not None:
        pl.ylabel(row_name, labelpad=row_name_pad)

    #####
    # Placing ticks on the top left subplot
    ax_label = ax_subplots[0, -1]
    pl.sca(ax_label)

    if subplot_xlabel:
        xticks = numpy.array(pl.xticks()[0], dtype=object)
        xticks[1::2] = ''
        ax_label.set_xticklabels(xticks, rotation=90, size='small')

    if subplot_ylabel:
        yticks = numpy.array(pl.yticks()[0], dtype=object)
        yticks[1::2] = ''
        ax_label.set_yticklabels(yticks, rotation=0, size='small')

    pl.sca(ax_main)

    return ax_main, ax_subplots