def plot_coverage(counts, offset_x=0, frags_pos=None, frags_pos_out=None,
                  title=None):
    '''Plot the coverage and the minor allele frequency'''
    cov = counts.sum(axis=1)
    cov_tot = cov.sum(axis=0)

    fig, ax = plt.subplots(1, 1, figsize=(11, 8))
    ax.plot(np.arange(len(cov_tot.T)) + offset_x, cov_tot.T, lw=2, c='k',
            label=read_types)
    ax.set_xlabel('Position [bases]')
    ax.set_ylabel('Coverage')
    ax.grid(True)

    # If the fragments positions are marked, plot them
    # Inner primers
    if frags_pos is not None:
        for i, frag_pos in enumerate(frags_pos.T):
            ax.plot(frag_pos + offset_x, 2 * [(0.97 - 0.03 * (i % 2)) * ax.get_ylim()[1]],
                    c=cm.jet(int(255.0 * i / len(frags_pos.T))), lw=2)

    # Outer primers
    if frags_pos_out is not None:
        for i, frag_pos in enumerate(frags_pos_out.T):
            ax.plot(frag_pos + offset_x, 2 * [(0.96 - 0.03 * (i % 2)) * ax.get_ylim()[1]],
                    c=cm.jet(int(255.0 * i / len(frags_pos_out.T))), lw=2)

    ax.set_xlim(0, 10000)

    if title is not None:
        ax.set_title(title, fontsize=18)

    plt.tight_layout(rect=(0, 0, 1, 0.95)) 
Exemplo n.º 2
0
    def findModDepths(self):
        oldtext = self.setButtonWait( self.findModDepthsPushButton )

        self.imageview.M_ex_rects = []
        self.imageview.M_em_rects = []
        self.imageview.phase_ex_rects = []
        self.imageview.phase_em_rects = []
        self.m.find_modulation_depths_and_phases()
        for s in self.m.validspots:
            color = cm.jet(s.M_ex)
            r = Rectangle( (s.coords[0],s.coords[1]), s.width, s.height, \
                               facecolor=color, edgecolor=color, alpha=1, zorder=7 )
            self.imageview.M_ex_rects.append( r )
            color = cm.jet(s.M_em)
            r = Rectangle( (s.coords[0],s.coords[1]), s.width, s.height, \
                               facecolor=color, edgecolor=color, alpha=1, zorder=7 )
            self.imageview.M_em_rects.append( r )
            color = cm.jet(s.phase_ex/np.pi+.5)
            r = Rectangle( (s.coords[0],s.coords[1]), s.width, s.height, \
                               facecolor=color, edgecolor=color, alpha=1, zorder=7 )
            self.imageview.phase_ex_rects.append( r )
            color = cm.jet(s.phase_em/np.pi+.5)
            r = Rectangle( (s.coords[0],s.coords[1]), s.width, s.height, \
                               facecolor=color, edgecolor=color, alpha=1, zorder=7 )
            self.imageview.phase_em_rects.append( r )
#            self.imageview.axes.add_patch( self.imageview.spot_rects[-1] )
#        self.imageview.show_stuff( what='M_ex' )
        self.imageview.show_stuff(what='M_ex')
        self.showStuffComboBox.setCurrentIndex(1)

        self.unsetButtonWait( self.findModDepthsPushButton, oldtext)
Exemplo n.º 3
0
def makePriorGrid(zs,rs,dr,outfile,rmin=15,rmax=30):
    rMids = np.arange(16.,28.,0.1)
    drs = 0.5*np.ones(len(rMids))
    drs[rMids<23]=0.5
    drs[rMids<20]=1.0
    drs[rMids<19]=2.0
    zEdges=np.arange(-0.075,5.075,0.1)
    zMids = (zEdges[1:]+zEdges[:-1])/2.
    allH = []
    for i in range(len(rMids)):
        #print(rMids[i])
        rMsk = np.ma.masked_outside(rs,rMids[i]-dr,rMids[i]+dr)
        zMsk = np.ma.masked_array(zs,mask=np.ma.getmask(rMsk)).compressed()

        h = np.histogram(zMsk,bins=zEdges)[0]
        kernel=np.ones(5)*(1./5.)
        h2=sig.convolve(h,kernel,mode='same')
        h3=sig.convolve(h2,kernel,mode='same')
        g = interp1d(zMids,h3,bounds_error=False, fill_value=0.0)
        tot = integrate.quad(g,0.,7.)
        h3 = h3/tot[0]
        
        if i%5==0:
            plt.plot(zMids,h3,lw=3,alpha=0.75,color=cm.jet(i/len(rMids)),label='r='+str(rMids[i]))
        else:
            plt.plot(zMids,h3,lw=3,alpha=0.5,color=cm.jet(i/len(rMids)))
        allH.append(h3)
    return([rMids,zMids,np.array(allH)])
Exemplo n.º 4
0
def compSystems(workflow, systems):

    subprocesses = {}
    for system in systems:
        subprocesses[system] = open(
            "C:/cygwin64/home/jrayner/run2/scripts/benchmarking/" + system + workflow, "r"
        )  # open file containt subprocesses

    subprocessNames = {}
    subprocessTimeTaken = {}
    subprocessCompleteTime = {}
    for key in subprocesses:
        rawData = [line.strip().split() for line in subprocesses[key]]
        rawData = np.array(rawData)  # read subplocesses in to numpy array
        times = np.array(rawData[:, 0], dtype=np.float)
        subprocessNames[key] = np.array(rawData[:, 1])
        subprocessTimeTaken[key] = np.array(times) / 60  # read time column into its own array and convert to float
        for i in range(len(times)):
            times[i] = np.sum(times[max([0, i - 1]) : i + 1])  # generate clumaltive time for ploting ticks
        subprocessCompleteTime[key] = times
        subprocesses[key].close()

    lenth = len(subprocessNames[system])
    fig = plt.figure()
    fig2 = plt.figure()
    fig.suptitle(workflow)
    fig2.suptitle(workflow)
    x3 = fig.add_subplot(111)  # plot subprocesses data
    x1 = fig2.add_subplot(111)
    barwidth = 0.5 / len(systems)
    for i, system in enumerate(systems):
        totaltime = np.sum(subprocessTimeTaken[system] / 60)
        x3.bar(
            np.arange(lenth) + barwidth * i,
            subprocessTimeTaken[system] / 60,
            width=barwidth,
            color=cm.jet(1.0 * i / len(systems)),
            label=system,
        )
        x1.bar(
            np.arange(lenth) + barwidth * i,
            subprocessTimeTaken[system] / totaltime / 60,
            width=barwidth,
            color=cm.jet(1.0 * i / len(systems)),
            label=system,
        )
    x3.set_xticks(np.arange(lenth) + 0.25)
    x3.set_xticklabels(subprocessNames[system], rotation=90)
    print(subprocessNames[system])
    x3.set_ylabel("time (minutes)")
    x3.legend(loc="center left", bbox_to_anchor=(1, 0.5))
    x1.set_xticks(np.arange(lenth + 0.25))
    x1.set_xticklabels(subprocessNames[system], rotation=90)
    x1.set_ylabel("time %")
    x1.legend(loc="center left", bbox_to_anchor=(1, 0.5))

    plt.show()
def plot_propagator_theory(xis, t, model='BSC', xlim=[0.03, 0.93], ax=None, logit=False,
                           VERBOSE=0, n=100):
    '''Make and plot BSC propagators for some initial frequencies'''
    from itertools import izip
    from hivwholeseq.theory.propagators import propagator_BSC, propagator_neutral

    if model == 'BSC':
        propagator_fun =  propagator_BSC
    elif model in ('neutral', 'Kingman', 'Kimura'):
        propagator_fun =  lambda x, y: propagator_neutral(x, y, n=n)

    if VERBOSE >= 1:
        print 'Make the propagators'
    xfs = []
    rhos = []
    for i, xi in enumerate(xis): 
        (xf, rho) = propagator_fun(xi, t)
        xfs.append(xf)
        rhos.append(rho)

    if VERBOSE >= 1:
        print 'Plot'
    if ax is None:
        ax_was_none = True
        fig, ax = plt.subplots(figsize=(12, 8))
    else:
        ax_was_none = False

    for i, (xi, xf, rho) in enumerate(izip(xis, xfs, rhos)):
        ind_out1 = (xf < xlim[0])
        ind_in = (xf >= xlim[0]) & (xf <= xlim[1])
        ind_out2 = (xf > xlim[1])
        ax.plot(xf[ind_in], rho[ind_in],
                color=cm.jet(1.0 * i / len(xis)),
                lw=2,
                label='$x_i = '+'{:1.1f}'.format(xi)+'$')
        ax.plot(xf[ind_out1], rho[ind_out1],
                color=cm.jet(1.0 * i / len(xis)),
                lw=2, ls='--', alpha=0.6)
        ax.plot(xf[ind_out2], rho[ind_out2],
                color=cm.jet(1.0 * i / len(xis)),
                lw=2, ls='--', alpha=0.6)

    if ax_was_none:
        ax.set_xlabel('Final frequency')

        if logit:
            ax.set_xlim(10**(-3.1), 1.0 - 10**(-3.1))
            ax.set_xscale('logit')
        else:
            ax.set_xscale('log')

        ax.set_ylim(1e-3, 1e3)
        ax.set_yscale('log')
        ax.set_ylabel('P(x1 | x0)')
        ax.set_title(model+' propagator, t = '+'{:1.1e}'.format(t))
        ax.grid(True)
Exemplo n.º 6
0
def plot_dfcorrections(plotfilename):
    niters= [1,2,3,4,5,10,15,20,25]
    bovy_plot.bovy_print(fig_height=7.,fig_width=8.)
    ii= 0
    # Load DF
    pyplot.subplot(2,1,1)
    dfc= dehnendf(beta=0.,correct=True,niter=niters[ii])
    bovy_plot.bovy_plot(dfc._corr._rs,
                        numpy.log(dfc._corr._corrections[:,0]),
                        '-',gcf=True,color=cm.jet(1.),lw=2.,zorder=1,
                        xrange=[0.,5.],
                        yrange=[-0.25,0.25],
                        ylabel=r'$\ln \Sigma_{\mathrm{out}}(R)-\ln\Sigma_{\mathrm{DF}}(R)$')
    linthresh= 0.0001
    pyplot.yscale('symlog',linthreshy=linthresh)
    for ii,niter in enumerate(niters[1:]):
        dfcn= dehnendf(beta=0.,correct=True,niter=niter)
        dfcp= dehnendf(beta=0.,correct=True,niter=niter-1)
        bovy_plot.bovy_plot(dfc._corr._rs,
                            numpy.log(dfcn._corr._corrections[:,0])-numpy.log(dfcp._corr._corrections[:,0]),
                            '-',overplot=True,
                            color=cm.jet(1.-(ii+1)/float(len(niters))),lw=2.,
                            zorder=ii+2)
    pyplot.fill_between(numpy.linspace(0.,5.,2.),
                        -linthresh*numpy.ones(2),
                         linthresh*numpy.ones(2),color='0.9',
                         zorder=0)
    bovy_plot.bovy_text(4.,-0.00008,r'$\mathrm{linear\ scale}$',
                        backgroundcolor='w',size=16.)
    pyplot.subplot(2,1,2)
    bovy_plot.bovy_plot(dfc._corr._rs,
                        0.5*numpy.log(dfc._corr._corrections[:,1]),
                        '-',gcf=True,color=cm.jet(1.),lw=2.,zorder=1,
                        xrange=[0.,5.],
                        yrange=[-0.25,0.25],
                        xlabel=r'$R/R_0$',
                        ylabel=r'$\ln \sigma_{R,\mathrm{out}}(R)-\ln\sigma_{R,\mathrm{DF}}(R)$')
    pyplot.yscale('symlog',linthreshy=linthresh)
    for ii,niter in enumerate(niters[1:]):
        dfcn= dehnendf(beta=0.,correct=True,niter=niter)
        dfcp= dehnendf(beta=0.,correct=True,niter=niter-1)
        bovy_plot.bovy_plot(dfc._corr._rs,
                            numpy.log(dfcn._corr._corrections[:,1])-numpy.log(dfcp._corr._corrections[:,1]),
                            '-',overplot=True,
                            color=cm.jet(1.-(ii+1)/float(len(niters))),lw=2.,
                            zorder=ii+2)
    pyplot.fill_between(numpy.linspace(0.,5.,2.),
                        -linthresh*numpy.ones(2),
                         linthresh*numpy.ones(2),color='0.9',
                         zorder=0)
    bovy_plot.bovy_text(4.,-0.00008,r'$\mathrm{linear\ scale}$',
                        backgroundcolor='w',size=16.)
    pyplot.tight_layout()
    bovy_plot.bovy_end_print(plotfilename)
    return None
Exemplo n.º 7
0
def create_figure(all_data): # takes in data and title and creates and saves plot

    width = 0.45  # width of the bars (in radians)

    # create the figure, dont change
    fig = plt.figure()
    ax = fig.add_subplot(111, polar=True)

    # angle positions, 0 to 360 with increments of 360/5
    xo = list(range(0, 360, 360 / 5))
    # Convert to radians and subtract half the width
    # of a bar to center it.
    x = [i * pi / 180 for i in xo]

    # set the labels for each bar, do not change
    ax.set_xticks(x)
    ax.set_xticklabels(['Military\nProwess', 'Productivity', 'Resource', 'Self-\nSufficiency', 'Morale'])
    ax.set_thetagrids(xo, frac=1.15) # frac changes distance of label from circumference of circle

    plt.ylim(0, 100) # sets range for radial grid

    fig.suptitle("India \n1993-2012", fontsize=20, y=0.5, x=0.1) # title of plot

    plt.rgrids([20, 40, 60, 80, 100], angle=33, fontsize=10) # the numbers you see along radius, angle changes position

    colorList = [];

    count = -1
    for key in all_data:
        count = count + 1
        data = all_data[key]
        mylist = [item+0.5*(count-len(all_data)/2)/len(all_data) for item in x]
        bars = ax.bar(mylist, data, width=width, align='center') # do the plotting
        i = 0
        for r, bar in zip(data, bars):
            bar.set_facecolor( cm.jet(0.8*count/len(all_data))) # set color for each bar, intensity proportional to height of bar
            colorList.append(cm.jet(0.8*count/len(all_data)))
            #bar.set_alpha(0.2) # make color partly transparent
            
            height = bar.get_height() # this is basically the radial height, or radius of bar

            # write value of each bar inside it
            # first param is angle, second is radius -10 makes it go inside the bar
        
            if i == 3 and count == 0:
                ax.text(mylist[i]-width/4*3, height+5, key, ha='center', va='center', fontsize=11)
            if i == 3 and count == len(all_data)-1:
                ax.text(mylist[i]+width/4*3, height-5, key, ha='center', va='center', fontsize=11)
            i = i + 1

    
    plt.savefig('examples/multiple.png')
 def plotFunc(fig,axes):
     axes.set_xlabel('integration time (s)')
     axes.plot(intTimes,countStds,'k',label=r'total $\sigma$')
     axes.plot(intTimes,countSqrts,'k--',label=r'$\sqrt{med(N)}$')
     nBins = np.shape(spectrumStds)[1]
     for iBin in xrange(nBins):
         axes.plot(intTimes,spectrumStds[:,iBin],
             c=cm.jet((iBin+1.)/nBins),
             label=r'%d-%d $\AA$ $\sigma$'%(rebinnedWvlEdges[iBin],
             rebinnedWvlEdges[iBin+1]))
         axes.plot(intTimes,spectrumSqrts[:,iBin],
             c=cm.jet((iBin+1.)/nBins),linestyle='--')
     axes.legend(loc='upper left')
Exemplo n.º 9
0
def plot_paddle_curve(keys, inputfile, outputfile, format='png',
                      show_fig=False):
    """Plot curves from paddle log and save to outputfile.

    :param keys: a list of strings to be plotted, e.g. AvgCost
    :param inputfile: a file object for input
    :param outputfile: a file object for output
    :return: None
    """
    pass_pattern = r"Pass=([0-9]*)"
    test_pattern = r"Test samples=([0-9]*)"
    if not keys:
        keys = ['AvgCost']
    for k in keys:
        pass_pattern += r".*?%s=([0-9e\-\.]*)" % k
        test_pattern += r".*?%s=([0-9e\-\.]*)" % k
    data = []
    test_data = []
    compiled_pattern = re.compile(pass_pattern)
    compiled_test_pattern = re.compile(test_pattern)
    for line in inputfile:
        found = compiled_pattern.search(line)
        found_test = compiled_test_pattern.search(line)
        if found:
            data.append([float(x) for x in found.groups()])
        if found_test:
            test_data.append([float(x) for x in found_test.groups()])
    x = numpy.array(data)
    x_test = numpy.array(test_data)
    if x.shape[0] <= 0:
        sys.stderr.write("No data to plot. Exiting!\n")
        return
    m = len(keys) + 1
    for i in xrange(1, m):
        pyplot.plot(
            x[:, 0],
            x[:, i],
            color=cm.jet(1.0 * (i - 1) / (2 * m)),
            label=keys[i - 1])
        if (x_test.shape[0] > 0):
            pyplot.plot(
                x[:, 0],
                x_test[:, i],
                color=cm.jet(1.0 - 1.0 * (i - 1) / (2 * m)),
                label="Test " + keys[i - 1])
    pyplot.xlabel('number of epoch')
    pyplot.legend(loc='best')
    if show_fig:
        pyplot.show()
    pyplot.savefig(outputfile, bbox_inches='tight')
    pyplot.clf()
Exemplo n.º 10
0
        def plot_grade(self):
            grade_data = []
            for i in range(len(self.route_waypoints)):
                wp1 = self.route_waypoints[i-1]
                wp2 = self.route_waypoints[i]

                rise = wp2.elevation - wp1.elevation
                run = haversine(wp1, wp2)

                grade_data.append(100 * rise / run)
            pdb.set_trace()

            fig, ax = plt.subplots()
            Ntotal = len(grade_data)
            N, bins, patches = ax.hist(grade_data, Ntotal)

            #I'll color code by height, but you could use any scalar

            # we need to normalize the data to 0..1 for the full
            # range of the colormap
            fracs = N.astype(float)/N.max()
            norm = colors.Normalize(fracs.min(), fracs.max())

            for thisfrac, thispatch in zip(fracs, patches):
                color = cm.jet(norm(thisfrac))
                thispatch.set_facecolor(color)

            plt.show()
Exemplo n.º 11
0
 def visualize_ns_old(self, term, points=200):
     """
     Use randomly selected coordinates instead of most active
     """
     if term in self.no.term:
         term_index = self.no._ns['features_df'].columns.get_loc(term)
         rand_point_inds = np.random.random_integers(0, len(np.squeeze(zip(self.no._ns['mni_coords'].data))), points)
         rand_points = np.squeeze(zip(self.no._ns['mni_coords'].data))[rand_point_inds]
         weights = []
         inds_of_real_points_with_no_fucking_missing_study_ids = []
         for rand_point in range(len(rand_points)):
             if len(self.no.coord_to_ns_act(rand_points[rand_point].astype(list))) > 0:
                 inds_of_real_points_with_no_fucking_missing_study_ids.append(rand_point_inds[rand_point])
                 weights.append(self.no.coord_to_ns_act(rand_points[rand_point].astype(list))[term_index])
         fig = plt.figure()
         ax = fig.add_subplot(111, projection='3d')
         colors = cm.jet(weights/max(weights))
         color_map = cm.ScalarMappable(cmap=cm.jet)
         color_map.set_array(weights)
         fig.colorbar(color_map)
         x = self.no._ns['mni_coords'].data[inds_of_real_points_with_no_fucking_missing_study_ids, 0]
         y = self.no._ns['mni_coords'].data[inds_of_real_points_with_no_fucking_missing_study_ids, 1]
         z = self.no._ns['mni_coords'].data[inds_of_real_points_with_no_fucking_missing_study_ids, 2]
     else:
         raise ValueError('Term '+term + ' has not been initialized. '
                                         'Use get_ns_act(' + term + ')')
     ax.scatter(x, y, z, c=colors, alpha=0.4)
     ax.set_title('Estimation of ' + term)
Exemplo n.º 12
0
def make_matplotlib_icon():
    fig = plt.figure(figsize=(1, 1))
    fig.patch.set_alpha(0.0)
    ax = fig.add_axes([0.025, 0.025, 0.95, 0.95], projection='polar')
    ax.set_axisbelow(True)

    N = 7
    arc = 2 * np.pi
    theta = np.arange(0, arc, arc / N)
    radii = 10 * np.array([0.2, 0.6, 0.8, 0.7, 0.4, 0.5, 0.8])
    width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3])
    bars = ax.bar(theta, radii, width=width, bottom=0.0, linewidth=1,
                  edgecolor='k')

    for r, bar in zip(radii, bars):
        bar.set_facecolor(cm.jet(r/10.))

    ax.tick_params(labelleft=False, labelright=False,
                   labelbottom=False, labeltop=False)
    ax.grid(lw=0.0)

    ax.set_yticks(np.arange(1, 9, 2))
    ax.set_rmax(9)

    return fig
Exemplo n.º 13
0
    def visualize_ge(self, gene, alpha=0.4):
        """
        Generates 3-D heat map of gene expression for a specified gene.

        Parameters
        ----------
        gene : str
            Entrez ID of gene for heat map generation.

        alpha : float
            Sets color density of coordinates used in heat map.
        """
        for e in gene:
            if e in self.no.ge:
                fig = plt.figure()
                ax = fig.add_subplot(111, projection='3d')
                weights = self.no.ge[e]["mean"]['GE']
                colors = cm.jet(weights/max(weights))
                color_map = cm.ScalarMappable(cmap=cm.jet)
                color_map.set_array(weights)
                fig.colorbar(color_map)

                x = self.no._aba['mni_coords'].data[:, 0]
                y = self.no._aba['mni_coords'].data[:, 1]
                z = self.no._aba['mni_coords'].data[:, 2]

                ax.scatter(x, y, z, c=colors, alpha=alpha)
            else:
                raise ValueError("Gene %s has not been initialized. "
                                 "Use self.no.get_aba_ge([%s])" % str(e))
        ax.set_title('Gene Expression of gene ID ' + str(gene))
Exemplo n.º 14
0
def plot_orient_quiver(data, odata, mask=None, imfile='', fps=1, savename='', figsize=None):
    """ plot_orient_quiver(data, odata, mask=None, imfile='')
    """
    import matplotlib.colors as mcolors
    import matplotlib.colorbar as mcolorbar
    pl.figure(tight_layout=False, figsize=figsize)
    if imfile is not None:
        bgimage = Im.open(extdir+prefix+'_0001.tif' if imfile is '' else imfile)
        pl.imshow(bgimage, cmap=cm.gray, origin='upper')
    #pl.quiver(X, Y, U, V, **kw)
    if mask is None:
        try:
            mask = np.all(np.isfinite(odata['orient']), axis=1)
        except ValueError:
            mask = np.isfinite(odata['orient'])

    n = odata.shape[-1] if odata.ndim > 1 else 1
    ndex = np.repeat(np.arange(mask.sum()), n)
    nz = mcolors.Normalize()
    nz.autoscale(data['f'][mask]/fps)
    qq = pl.quiver(
            data['y'][mask][ndex], data['x'][mask][ndex],
            odata['cdisp'][mask][...,1].flatten(), -odata['cdisp'][mask][...,0].flatten(),
            color=cm.jet(nz(data['f'][mask]/fps)),
            scale=1, scale_units='xy')
    #pl.title(', '.join(imfile.split('/')[-1].split('_')[:-1]) if imfile else '')
    cax,_ = mcolorbar.make_axes(pl.gca())
    cb = mcolorbar.ColorbarBase(cax, cmap=cm.jet, norm=nz)
    cb.set_label('time '+('(s)'if fps > 1 else '(frame)'))
    if savename:
        print "saving to", savename
        pl.savefig(savename)
    pl.show()
    return qq, cb
Exemplo n.º 15
0
def plot_orient_location(data,odata,tracks):
    import correlation as corr

    omask = np.isfinite(odata['orient'])
    goodtracks = np.array([78,95,191,203,322])

    ss = 22.

    pl.figure()
    for goodtrack in goodtracks:
        tmask = tracks == goodtrack
        fullmask = np.all(np.asarray(zip(omask,tmask)),axis=1)
        loc_start = (data['x'][fullmask][0],data['y'][fullmask][0])
        orient_start = odata['orient'][fullmask][0]
        sc = pl.scatter(
                (odata['orient'][fullmask] - orient_start + pi) % twopi,
                np.asarray(map(corr.get_norm,
                    zip([loc_start]*fullmask.sum(),
                        zip(data['x'][fullmask],data['y'][fullmask]))
                    ))/ss,
                #marker='*',
                label = 'track {}'.format(goodtrack),
                color = cm.jet(1.*goodtrack/max(tracks)))
                #color = cm.jet(1.*data['f'][fullmask]/1260.))
        print "track",goodtrack
    pl.legend()
    pl.show()
    return True
Exemplo n.º 16
0
    def __init__(self, parent=None, data=None, fnameAbsPath="", enable=True, objectName=""):
        super(lines, self).__init__(parent, data, fnameAbsPath, enable, objectName, pgObject="PlotCurveItem")

        # Choose symbols from preferences file.
        # TODO: read symbols from GUI
        self.symbol = lasHelp.readPreference("symbolOrder")[0]
        self.symbolSize = int(self.parent.markerSize_spinBox.value())
        self.alpha = int(self.parent.markerAlpha_spinBox.value())
        self.lineWidth = int(self.parent.lineWidth_spinBox.value())

        # Add to the imageStackLayers_model which is associated with the points QTreeView
        name = QtGui.QStandardItem(objectName)
        name.setEditable(False)

        # Add checkbox
        thing = QtGui.QStandardItem()
        thing.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsUserCheckable)
        thing.setCheckState(QtCore.Qt.Checked)

        # self.modelItems=(name,thing) #Remove this for now because I have NO CLUE how to get access to the checkbox state
        self.modelItems = name
        self.model = self.parent.points_Model

        self.addToList()

        # Set the colour of the object based on how many items are already present
        number_of_colors = 6
        thisNumber = (self.parent.points_Model.rowCount() - 1) % number_of_colors
        cm_subsection = linspace(0, 1, number_of_colors)
        colors = [cm.jet(x) for x in cm_subsection]
        color = colors[thisNumber]
        self.color = [color[0] * 255, color[1] * 255, color[2] * 255]
Exemplo n.º 17
0
def _cm_ra_plot(data,headings,dates,ave_data,fig_num,colour_col,
             y_col,run_ave_points,n_plot_row,n_plot_col,n_plot_num):
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    
    depth = np.max(data[:,colour_col]) - np.min(data[:,colour_col])
    c_map = cm.jet(np.arange(256))
    
    Z = [[0,0],[0,0]]
    levels = range(int(np.min(data[:,colour_col])),
                    int(np.max(data[:,colour_col])),1)
    mappl = plt.contourf(Z,levels,cmap=cm.jet)      
   
    plt.figure(fig_num)   
 
    plt.subplot(n_plot_row,n_plot_col,n_plot_num)
    for i in range(len(data)):
        plot1 = plt.plot(dates[i],data[i,y_col])
        cm_point = np.floor(((np.max(data[:,colour_col]) - 
                data[i,colour_col]) / depth) * 255)
        plt.setp(plot1,linestyle = 'none',marker = '.')
        plt.setp(plot1,color = c_map[255 - cm_point,])

    plt.grid(True)
    plt.ylabel('%s on %s'%(headings[colour_col],headings[y_col]))
#        plt.axis([None,None,0,100])
    plt.axis([None,None,min(data[:,y_col]),max(data[:,y_col])])
    plt.plot(dates,ave_data[:,y_col],'k-')
    locs, labels = plt.xticks()
    plt.setp(labels, rotation=0)
    plt.colorbar(mappl)
Exemplo n.º 18
0
def add_polar_bar():
    ax = fig.add_axes([0.025, 0.075, 0.2, 0.85], polar=True)

    ax.axesPatch.set_alpha(axalpha)
    ax.set_axisbelow(True)
    N = 7
    arc = 2. * np.pi
    theta = np.arange(0.0, arc, arc/N)
    radii = 10 * np.array([0.2, 0.6, 0.8, 0.7, 0.4, 0.5, 0.8])
    width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3])
    bars = ax.bar(theta, radii, width=width, bottom=0.0)
    for r, bar in zip(radii, bars):
        bar.set_facecolor(cm.jet(r/10.))
        bar.set_alpha(0.6)

    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_visible(False)

    for line in ax.get_ygridlines() + ax.get_xgridlines():
        line.set_lw(0.8)
        line.set_alpha(0.9)
        line.set_ls('-')
        line.set_color('0.5')

    ax.set_yticks(np.arange(1, 9, 2))
    ax.set_rmax(9)
Exemplo n.º 19
0
def plot_m1m2(m1m2_file, plot_pk=None, m1gr=None, m2gr=None, 
              m1gr_err=None, m2gr_err=None,
              m1m2_contour=None,
              pk_label_coord=None,
              xlim=None, ylim=None, plot_inset=False,
              colour=None, line_style=None, m1m2_pbdot_uncorr=None):

     
     n_plot = len(plot_pk)
# Ensure that we are plotting more than 1 parameter:
     if(n_plot < 1):
          print 'plot_m1m2:  Must plot at least one PK parameter. Exiting...'   
     if(colour == None):
          clr = [cm.jet(float(i_plot)/float(n_plot-1)) for i_plot in range(n_plot)]
     else:
          clr = colour
          
     if(line_style == None):
         line_style='-'
 
# Now, read input m1m2.dat-style file:     
     try:
          f_m1m2 = open(m1m2_file, 'r')
     except IOError as (errno, strerror):
          if (errno == 2):  # file not found
               print "IOError ({0}): File".format(errno), m1m2_file, "not found."
          else:
               print "IOError ({0}): {1}".format(errno, strerror)
          return
Exemplo n.º 20
0
def visualize_silhouette_score(X,y_km):

    cluster_labels = np.unique(y_km)
    n_clusters = cluster_labels.shape[0]
    silhouette_vals = metrics.silhouette_samples(X,
                                         y_km,
                                         metric='euclidean')
    y_ax_lower, y_ax_upper = 0, 0
    yticks = []
    for i, c in enumerate(cluster_labels):
        c_silhouette_vals = silhouette_vals[y_km == c]
        c_silhouette_vals.sort()
        y_ax_upper += len(c_silhouette_vals)
        color = cm.jet(i / n_clusters)
        plt.barh(range(y_ax_lower, y_ax_upper),
                c_silhouette_vals,
                height=1.0,
                edgecolor='none',
                color=color)
        yticks.append((y_ax_lower + y_ax_upper) / 2)
        y_ax_lower += len(c_silhouette_vals)

    silhouette_avg = np.mean(silhouette_vals)
    plt.axvline(silhouette_avg,
                color="red",
                linestyle="--")
    plt.yticks(yticks, cluster_labels + 1)
    plt.ylabel('Cluster')
    plt.xlabel('Silhouette coefficient')
    plt.show()
Exemplo n.º 21
0
def compareTestParamsWithOutput( movie, paramfilename ):
    
    import matplotlib.pyplot as plt
    import matplotlib.cm as cmap
    from matplotlib.patches import Rectangle

    params = np.load( paramfilename )

    plt.imshow( params[:,:,0], cmap=cmap.gray )
    ax = plt.gca()

#    mexs = []
    for s in movie.validspots:
#        mexs.append( s.M_ex )
        md_ex = params[s.coords[0],s.coords[1],0]
        s.M_ex_diff = s.M_ex - md_ex
        print 's.M_ex=%f\tmd_ex=%f\tdiff=%f' % (s.M_ex, md_ex, s.M_ex_diff)

        col = cmap.jet(s.M_ex)
        print col
        p = Rectangle((s.coords[0],s.coords[1]), s.width, s.height, \
                          facecolor=col, edgecolor=None, linewidth=0, alpha=.3)
        
        ax.add_patch( p )

    plt.draw()
Exemplo n.º 22
0
  def plot_clusters(self, clusters, cols=None, color=None, alpha=None):
    self.clusters = clusters
    errors = [c.error for c in clusters]
    errors = np.array(errors)
    mean, std = np.mean(errors), np.std(errors)
    if std == 0: std = 1
    errors = (errors - mean) / std

    if not cols:
      cols = [0, 1]
    if isinstance(cols[0], basestring):
      cols = map(clusters.cols.index, cols)

    for idx, cluster in enumerate(clusters):
      tup = tuple(map(list, zip(*cluster.bbox)))
      x, y = tup[cols[0]], tup[cols[1]]
      x, y = self.transform_box(x, y)
      if not self.xbound:
        self.xbound = [x[0], x[1]]
        self.ybound = [y[0], y[1]]
      else:
        self.xbound = r_union(self.xbound, x)
        self.ybound = r_union(self.ybound, y)

      a = alpha or min(1, max(0.1, errors[idx]))
      c = color or cm.jet(errors[idx])
      r = Rect((x[0], y[0]), x[1]-x[0], y[1]-y[0], alpha=a, ec=c, fill=False, lw=1.5)
      self.sub.add_patch(r)

    self.set_lims()
Exemplo n.º 23
0
def GenerateFigure(self, given_list):
    color = []
    for i in range(len(given_list)):
        current = round(given_list[i][2],1)
        float_curr = float(current/1000000)
        color.append(float_curr)
    start = 0
    theta = []
    radii = []
    width = []
    for i in range(len(given_list)):
        theta.append(start)
        if given_list[i][2] == 0:
            radii.append(0)
        else:
            radii.append(given_list[i][2])
        curr_wid = given_list[i][0]/100*2*np.pi
        width.append(curr_wid)
        start = start + curr_wid

    figwidth = 45.0
    figheight = 45.0
    fig = figure(figsize=(figwidth, figheight))
                
    ax = fig.add_axes([0.2,0.2,0.55,0.55], polar = True, aspect = 'equal')
    ax.axis('off')

    for i in range(len(given_list)):
            ax.annotate(given_list[i], xy = [theta[i]+float(width[i]/2), max(radii)], size = float(given_list[i][0]/2)+18.0, rotation = int((theta[i]+float(width[i]/2))/(2*np.pi)*360), va='center', ha='center')
    bars = ax.bar(theta, radii, width=width, bottom =0.0)
    for r,bar in zip(color, bars):
        bar.set_alpha(0.5)
        bar.set_facecolor(cm.jet(r/0.1))
    return fig
Exemplo n.º 24
0
def make_matplotlib_icon():
    fig = plt.figure(figsize=(1, 1))
    fig.patch.set_alpha(0.0)
    ax = fig.add_axes([0.025, 0.025, 0.95, 0.95], projection='polar')
    ax.set_axisbelow(True)

    N = 7
    arc = 2. * np.pi
    theta = np.arange(0.0, arc, arc/N)
    radii = 10 * np.array([0.2, 0.6, 0.8, 0.7, 0.4, 0.5, 0.8])
    width = np.pi / 4 * np.array([0.4, 0.4, 0.6, 0.8, 0.2, 0.5, 0.3])
    bars = ax.bar(theta, radii, width=width, bottom=0.0, linewidth=1,
                  edgecolor='k')

    for r, bar in zip(radii, bars):
        bar.set_facecolor(cm.jet(r/10.))

    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_visible(False)

    for line in ax.get_ygridlines() + ax.get_xgridlines():
        line.set_lw(0.0)

    ax.set_yticks(np.arange(1, 9, 2))
    ax.set_rmax(9)

    return fig
Exemplo n.º 25
0
    def _plot2(pdobj, title):
        fig = plt.figure()
        ax = fig.add_subplot(111)

        if isinstance(pdobj, pd.Series):
            _pdobj = pd.DataFrame(pdobj)
        else:
            _pdobj = pdobj

        size, grps = _pdobj.shape
        ticks = np.arange(size)
        width = .9 / grps

        bars = []
        for i in range(grps):
            bars.append(ax.bar(ticks+i*width, _pdobj.iloc[:, i], width))
        for i in range(grps):
            for rect in bars[i]:
                rect.set_facecolor(cm.jet(i*1.0/grps))
                rect.set_alpha(0.6)

        ax.set_xticks(ticks + grps*width/2)
        ax.set_xticklabels([dt.strftime('%Y%m%d') for dt in pdobj.index], rotation=90)

        ax.set_title(title)
        if not isinstance(pdobj, pd.Series):
            ax.legend([bar[0] for bar in bars], list(pdobj.columns))
        return fig
def plot_quality_along_reads(data_folder, adaID, title, quality, VERBOSE=0, savefig=False):
    """Plot the results of the quality scores along reads"""

    import matplotlib.pyplot as plt
    from matplotlib import cm

    fig, axs = plt.subplots(1, 2, figsize=(16, 9))
    for i, (ax, qual) in enumerate(izip(axs, quality)):
        for j, qpos in enumerate(qual):
            x = qpos
            y = np.linspace(0, 1, len(x))[::-1]
            ax.plot(x, y, color=cm.jet(int(255.0 * j / len(qual))), alpha=0.5, lw=2)
        ax.set_xlabel("Phred quality", fontsize=14)
        ax.set_ylabel("Fraction of bases above quality x", fontsize=14)
        ax.set_title("Read" + str(i + 1), fontsize=16)
        ax.text(2, 0.03, "blue to red: 0 to " + str(len(qual)) + " base", fontsize=18)

    fig.suptitle(title, fontsize=20)

    if savefig:
        from hivwholeseq.utils.generic import mkdirs
        from hivwholeseq.sequencing.filenames import get_figure_folder, get_quality_along_reads_filename

        fig_folder = get_figure_folder(data_folder, adaID)
        fig_filename = get_quality_along_reads_filename(data_folder, adaID)
        mkdirs(fig_folder)
        fig.savefig(fig_filename)

    else:
        plt.tight_layout()
        plt.ion()
        plt.show()
Exemplo n.º 27
0
def print_clusters(pp, clusters, tuples=[], title=''):
    fig = plt.figure(figsize=(8, 8))
    sub = fig.add_subplot(111)
    clusters.sort(key=lambda c: c.error)

    for cluster in clusters:
      x, y = tuple(map(list, zip(*cluster.bbox)))[:2]
      x[0] = max(0, x[0])
      x[1] = min(100, x[1])
      y[0] = max(0, y[0])
      y[1] = min(100, y[1])
      c = cm.jet(cluster.error)
      r = Rect((x[0], y[0]), x[1]-x[0], y[1]-y[0], alpha=max(0.1,cluster.error), ec=c, fill=False, lw=1.5)
      sub.add_patch(r)

    if tuples:
      cols = zip(*tuples)
      xs, ys, cs = cols[0], cols[1], cols[-1]
      sub.scatter(ys, xs, c=cs, alpha=0.5, lw=0)


    sub.set_ylim(-5, 105)
    sub.set_xlim(-5, 105)
    sub.set_title(title)
    plt.savefig(pp, format='pdf')
Exemplo n.º 28
0
def plotHyper(X,W,b):
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    import numpy as np
    import matplotlib.pyplot as plt
    
    point = np.array([1, 2, 3])
    normal = np.array([1, 1, 2])
    
    # a plane is a*x+b*y+c*z+d=0
    # [a,b,c] is the normal. Thus, we have to calculate
    # d and we're set
    d = -point.dot(normal)
    
    # create x,y
    xx, yy = np.meshgrid(range(10), range(10))
    
    # calculate corresponding z
    z = (-normal[0] * xx - normal[1] * yy - d) * 1. / normal[2]
    
    # plot the surface
    plt3d = plt.figure().gca(projection='3d')
    plt.scatter(X[:,0],X[:,1])

    
    Gx, Gy = np.gradient(xx * yy)  # gradients with respect to x and y
    G = (Gx ** 2 + Gy ** 2) ** .5  # gradient magnitude
    N = G / G.max()  # normalize 0..1
    
    plt3d.plot_surface(xx, yy, z, rstride=1, cstride=1,
                       facecolors=cm.jet(N),
                       linewidth=0, antialiased=False, shade=False
    )
    plt.show()
Exemplo n.º 29
0
def scatter(frame, var1, var2, var3=None, reg=False, **args):
    import matplotlib.cm as cm

    if type(frame) is copper.Dataset:
        frame = frame.frame
    x = frame[var1]
    y = frame[var2]

    if var3 is None:
        plt.scatter(x.values, y.values, **args)
    else:
        options = list(set(frame[var3]))
        for i, option in enumerate(options):
            f = frame[frame[var3] == option]
            x = f[var1]
            y = f[var2]
            c = cm.jet(i/len(options),1)
            plt.scatter(x, y, c=c, label=option, **args)
            plt.legend()

    if reg:
        slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
        line = slope * x + intercept # regression line
        plt.plot(x, line, c='r')

    plt.xlabel(var1)
    plt.ylabel(var2)
Exemplo n.º 30
0
def graph(size, kind, name, results, labels):
  N = len(labels)
  M = len(containers)
  ind = numpy.arange(N)
  border = 0.05
  width = (1.0 - border * 2) / M
  fig, ax = pyplot.subplots()

  legend_ids = []
  legend_names = []
  rects_list = []
  n = 0
  for container in containers:
    times = results[container]
    rects = ax.bar(ind + width * n + border, times, width, color=cm.jet(n / M))
    legend_ids.append(rects[0])
    legend_names.append(container)
    rects_list.append(rects)
    n += 1

  ax.set_xlabel('Container size')
  ax.set_ylabel('Milliseconds')
  ax.set_yscale('log')
  ax.set_title('%s of std::uint%d_t' % (name, size))
  ax.set_xticks(ind + width * M / 2 + border)
  ax.set_xticklabels(labels)
  ax.legend(legend_ids, legend_names, loc='upper left')

  pyplot.savefig('%s/%s-%s-%d.svg' % (output_dir,
                                   name.lower().replace(' ', '_'),
                                   kind,
                                   size))
  pyplot.close()
Exemplo n.º 31
0
def plot_breathing_rate(subj_id,
                        subj_data,
                        interval,
                        variable='breathing_rate',
                        show=True,
                        ax_given=None,
                        save=False,
                        path=None):
    """ Plot the breathing rate of subject *subj_id* between the interval specified.
    """

    # Interpolate missing values.
    subj_data = subj_data.interpolate(method='linear', axis=0)

    if interval[0] == 'start':
        start = 0
    else:
        start = [i for i, x in enumerate(subj_data.index == interval[0])
                 if x][0]
    if interval[1] == 'end':
        end = subj_data.shape[0]
    else:
        end = [i for i, x in enumerate(subj_data.index == interval[1]) if x][0]

    plot_data = subj_data.iloc[start:end, :]

    fig, ax = plt.subplots(figsize=(10, 5))

    ax = ax_given or plt.gca()

    if ax_given is not None:
        predefined_ax = True
    else:
        predefined_ax = False

    color_data = plot_data['activity_type'].replace(-1, 3)

    # Plot data coloured by activity type.
    # plot_colourline(plot_data.index.values, plot_data[variable].values, plot_data['activity_type'], ax=ax)
    plot_colourline(plot_data.index.values,
                    plot_data[variable].values,
                    color_data,
                    ax=ax)

    if variable == 'breathing_rate':
        # Plot standard error of the breathing rate.
        ax.fill_between(plot_data.index.values,
                        plot_data['breathing_rate'] + plot_data['sd_br'],
                        plot_data['breathing_rate'] - plot_data['sd_br'],
                        alpha=0.2)

    # Format axes.
    if predefined_ax == False:
        xformatter = md.DateFormatter('%H:%M')
        xlocator = md.MinuteLocator(byminute=[0, 15, 30, 45], interval=1)
        ax.xaxis.set_major_locator(xlocator)
        plt.gcf().axes[0].xaxis.set_major_formatter(xformatter)
        fig.autofmt_xdate()
        plt.xlabel('Time')

    if variable == 'breathing_rate':
        plt.ylabel('Breathing rate per minute')
    elif variable == 'smoothed_br':
        plt.ylabel('Smoothed breathing rate')
    elif variable == 'sd_br':
        plt.ylabel('Standard deviation of breathing rate')
    elif variable == 'rolling_sd_br':
        plt.ylabel('Rolling mean st. dev. of breathing rate')
    elif variable == 'activity_level':
        plt.ylabel('Activity level per minute')
    elif variable == 'smoothed_al':
        plt.ylabel('Smoothed activity level')

    # Plot legend.
    custom_lines = [
        Line2D([0], [0], color=cm.jet(0.), lw=2),
        Line2D([0], [0], color=cm.jet(1 / 3), lw=2),
        Line2D([0], [0], color=cm.jet(2 / 3), lw=2),
        Line2D([0], [0], color=cm.jet(1.), lw=2)
    ]
    lgd = ax.legend(custom_lines, [
        'Sitting/standing', 'Walking', 'Lying down',
        "Wrong orientation/undetermined"
    ],
                    loc='center left',
                    bbox_to_anchor=(1.01, 0.5))

    if save == True:
        if path == None:
            raise ValueError(
                "A path needs to be specified to save the figure to.")
        else:
            plt.savefig(path,
                        bbox_extra_artists=(lgd, ),
                        bbox_inches='tight',
                        dpi=300)

    if show == True:
        plt.show()
Exemplo n.º 32
0
        localvol.append(min([0.2+5*np.log(100./ls_K[i])**2+0.1*np.exp(-(ls_T[j])), 0.6]))
        ls_prix[i].append(BlackScholes("Call",S0,ls_K[i],r,localvol[i],ls_T[j]))

ls_prix = np.array(ls_prix)
        
liste_sigma = [[] for i in range(len(ls_K))]
for i in range(len(ls_K)):
    for j in range(len(ls_T)):
        liste_sigma[i].append(dicho("Call",S0, ls_K[i], r, ls_T[j], 0.5, 0, np.sqrt(0.25), 0.01, 0.6, ls_prix[i,j]))

liste_sigma = np.array(liste_sigma)
ls_T = np.array(ls_T)
ls_K = np.array(ls_K)

ax = Axes3D(plt.figure())


ls_T,ls_K = np.meshgrid(ls_T,ls_K)

Gx, Gy = np.gradient(liste_sigma) # gradients with respect to x and y
G = (Gx**2+Gy**2)**.5  # gradient magnitude
N = G/G.max()  # normalize 0..1
#ax.plot_wireframe(ls_T,ls_K,liste_sigma, rstride=1, cstride=1)

ax.plot_surface(ls_T,ls_K,liste_sigma, rstride=1, cstride=1, facecolors=cm.jet(N), linewidth=0, antialiased=False, shade=False)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.title("Surface de volatilité")


ax.view_init(17, 30)
Exemplo n.º 33
0
cluster_labels = np.unique(y)
n_clusters = cluster_labels.shape[0]

# initializing variables for creating silhouettes of clusters
silhouette_vals = silhouette_samples(X_pca, y, metric='euclidean')
y_ax_lower, y_ax_upper = 0, 0
yticks = []

# generate silhouette for each cluster
for i, c in enumerate(cluster_labels):
    c_silhouette_vals = silhouette_vals[
        y == c]  # store silhouette vals for each cluster
    c_silhouette_vals.sort()
    y_ax_upper += len(c_silhouette_vals)
    color = cm.jet(float(i) / n_clusters)  # assign different color each loop
    plt.barh(
        range(y_ax_lower, y_ax_upper),  # plot silhouette for each cluster
        c_silhouette_vals,
        height=1.0,
        edgecolor='none',
        color=color)
    yticks.append((y_ax_lower + y_ax_upper) / 2.)
    y_ax_lower += len(c_silhouette_vals)
silhouette_avg = np.mean(silhouette_vals)  # avg for plotting

# plot silhouette coefficients (labels)
plt.axvline(silhouette_avg, color="red", linestyle="--")
plt.yticks(yticks, cluster_labels + 1)
plt.ylabel('Cluster')
plt.xlabel('Silhouette coefficient')
Exemplo n.º 34
0
subplot(132)
plot(auto_rem[set1], alpha=0.8, color='red')
plot(auto_rem[set2], alpha=0.8, color='grey')
subplot(133)
plot(auto_sws[set1], alpha=0.8, color='red')
plot(auto_sws[set2], alpha=0.8, color='grey')

figure()
for i, k in enumerate(accel_curves.columns[order]):
    subplot(5, 8, i + 1)
    plot(accel_curves[k])

from matplotlib import cm
times = np.sort(list(speed_curves_shifted.keys()))
norm = matplotlib.colors.Normalize(vmin=times[0], vmax=times[-1])
rgba_color = cm.jet(norm(400), bytes=True)

figure()
for i, n in enumerate(order):
    subplot(5, 8, i + 1)
    name = session.split("/")[1] + "_" + str(n)
    plot(speed_curves[name], color='black')
    for t in np.sort(list(speed_curves_shifted.keys())):
        plot(speed_curves_shifted[t][name],
             color=cm.jet(norm(t)),
             linewidth=0.8,
             alpha=0.7)

# i = 33
# n = order[i]
# name = session.split("/")[1]+"_"+str(n)
def convection_onset_statistics(cwv, precip, test, output_path, sites):
    # Create CWV bins
    number_of_bins = 28  # default = 28
    cwv_max = 70  # default = 70 (in mm)
    cwv_min = 28  # default = 28 (in mm)
    bin_width = 1.5  # default = 1.5
    bin_center = np.arange((cwv_min + (bin_width / 2)),
                           (cwv_max - (bin_width / 2)) + bin_width, bin_width)

    # Define precip threshold
    precip_threshold = 0.5
    # default 0.5 (in mm/hr)

    # Define variables for binning
    bin_index = np.zeros([number_of_bins, cwv.size])
    precip_binned = np.empty([number_of_bins, cwv.size]) * np.nan
    precip_counts = np.zeros([number_of_bins, cwv.size])

    # In[264]:

    # Bin the data by CWV value as specified above
    for i in range(0, number_of_bins):
        for j in range(0, cwv.size):
            if cwv[j] > (cwv_min + (i * bin_width)):
                bin_index[i, j] = 1
            if cwv[j] > (cwv_min + (i * bin_width) + bin_width):
                bin_index[i, j] = 0
            if bin_index[i, j] == 1:
                precip_binned[i, j] = precip[j]
            else:
                precip_binned[i, j] = np.nan
            if precip_binned[i, j] >= precip_threshold:
                precip_counts[i, j] = 1
            if np.isnan(precip_binned[i, j]):
                precip_counts[i, j] = np.nan

    # In[265]:

    # Create binned arrays
    hist_cwv = np.empty([number_of_bins, 1]) * np.nan
    hist_precip_points = np.empty([number_of_bins, 1]) * np.nan
    pr_binned_mean = np.empty([number_of_bins, 1]) * np.nan
    pr_binned_var = np.empty([number_of_bins, 1]) * np.nan
    pr_binned_std = np.empty([number_of_bins, 1]) * np.nan
    pr_probability = np.empty([number_of_bins, 1]) * np.nan
    errorbar_hist_precip_points = np.empty([number_of_bins, 1]) * np.nan
    std_error_precip = np.empty([number_of_bins, 1]) * np.nan
    pdf_cwv = np.empty([number_of_bins, 1]) * np.nan
    pdf_precipitating_points = np.empty([number_of_bins, 1]) * np.nan

    # Fill binned arrays
    hist_cwv = bin_index.sum(axis=1)
    hist_precip_points = np.nansum(precip_counts, axis=1)
    pr_binned_mean = np.nanmean(precip_binned, axis=1)
    pr_binned_var = np.nanvar(precip_binned, axis=1)
    pr_binned_std = np.nanstd(precip_binned, axis=1)
    r = np.empty([1, number_of_bins]) * np.nan
    r = np.sum(~np.isnan(precip_counts), axis=1)
    pr_probability = np.nansum(precip_counts, axis=1) / r
    pdf_cwv = hist_cwv / bin_width
    pdf_precipitating_points = hist_precip_points / bin_width
    for i in range(0, number_of_bins):
        std_error_precip[i] = pr_binned_std[i] / math.sqrt(hist_cwv[i])
        errorbar_hist_precip_points[i] = math.sqrt(
            hist_precip_points[i]) / bin_width

    # In[266]:

    # create color map
    # choose from maps here:
    # http://matplotlib.org/examples/color/colormaps_reference.html
    scatter_colors = cm.jet(
        np.linspace(0, 1, number_of_bins + 1, endpoint=True))
    # scatter_colors = cm.plasma(numpy.linspace(0,1,number_of_bins+1,endpoint=True))

    # In[267]:

    axes_fontsize = 12  # size of font in all plots
    legend_fontsize = 9
    marker_size = 40  # size of markers in scatter plots
    xtick_pad = 10  # padding between x tick labels and actual plot

    # create figure canvas
    fig = mp.figure(figsize=(8, 2.5))

    # create figure 1
    ax1 = fig.add_subplot(131)
    ax1.set_xlim(25, 72)
    ax1.set_ylim(0, 6)
    ax1.set_xticks([30, 40, 50, 60, 70])
    ax1.set_yticks([0, 1, 2, 3, 4, 5, 6])
    ax1.tick_params(labelsize=axes_fontsize)
    ax1.tick_params(axis='x', pad=10)
    error = [std_error_precip, std_error_precip]
    ax1.errorbar(bin_center,
                 pr_binned_mean,
                 xerr=0,
                 yerr=error,
                 ls='none',
                 color='black')
    ax1.scatter(bin_center,
                pr_binned_mean,
                edgecolor='none',
                facecolor=scatter_colors,
                s=marker_size,
                clip_on=False,
                zorder=3)
    ax1.set_ylabel('Precip (mm hr $^-$ $^1$)', fontsize=axes_fontsize)
    ax1.set_xlabel('CWV (mm)', fontsize=axes_fontsize)
    ax1.text(0.05,
             0.95,
             sites[0],
             transform=ax1.transAxes,
             fontsize=12,
             verticalalignment='top')
    ax1.text(0.05,
             0.85,
             test,
             transform=ax1.transAxes,
             fontsize=12,
             verticalalignment='top')
    #ax1.grid()
    ax1.set_axisbelow(True)

    # create figure 2 (probability pickup)
    ax2 = fig.add_subplot(132)
    ax2.set_xlim(25, 72)
    ax2.set_ylim(0, 1)
    ax2.set_xticks([30, 40, 50, 60, 70])
    ax2.set_yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
    ax2.tick_params(labelsize=axes_fontsize)
    ax2.tick_params(axis='x', pad=xtick_pad)
    ax2.scatter(bin_center,
                pr_probability,
                marker='d',
                s=marker_size,
                edgecolor='none',
                facecolor='steelblue',
                clip_on=False,
                zorder=3)
    ax2.set_ylabel('Probability of Precip', fontsize=axes_fontsize)
    ax2.set_xlabel('CWV (mm)', fontsize=axes_fontsize)
    #ax2.grid()
    ax2.set_axisbelow(True)

    # create figure 3 (non-normalized PDF)
    ax3 = fig.add_subplot(133)
    ax3.set_yscale('log')
    ax3.set_ylim(5e-1, 5e5)
    ax3.set_xlim(25, 72)
    ax3.set_xticks([30, 40, 50, 60, 70])
    ax3.set_yticks(10**np.array((0, 1, 2, 3, 4, 5)))
    ax3.tick_params(labelsize=axes_fontsize)
    ax3.tick_params(axis='x', pad=xtick_pad)
    # yscale is log scale, so throw out any zero values
    pdf_precipitating_points[pdf_precipitating_points == 0] = np.nan
    error = [errorbar_hist_precip_points, errorbar_hist_precip_points]
    ax3.errorbar(bin_center,
                 pdf_precipitating_points,
                 xerr=0,
                 yerr=error,
                 ls='none',
                 color='black')
    ax3.scatter(bin_center,
                pdf_precipitating_points,
                edgecolor='none',
                facecolor='b',
                s=marker_size,
                clip_on=False,
                zorder=3,
                label='precip $>$ 0.5 mm hr $^{\minus 1}$')
    ax3.scatter(bin_center, pdf_cwv, marker='x', color='0', label='all')
    ax3.set_ylabel('Freq Density', fontsize=axes_fontsize)
    ax3.set_xlabel('CWV (mm)', fontsize=axes_fontsize)
    #ax3.grid()
    ax3.set_axisbelow(True)

    # create legend
    legend_handles, legend_labels = ax3.get_legend_handles_labels()
    ax3.legend(legend_handles,
               legend_labels,
               loc='upper left',
               bbox_to_anchor=(0.1, 0.95),
               fontsize=legend_fontsize,
               scatterpoints=1,
               handlelength=0,
               labelspacing=0,
               borderpad=0,
               borderaxespad=0,
               frameon=False)

    # set layout to tight (so that space between figures is minimized)
    mp.tight_layout()
    # save figure
    #mp.savefig('conv_diagnostics_example_kas_new.pdf', transparent=True, bbox_inches='tight')
    mp.savefig(output_path + '/figures/conv_diagnostics_' + test + '_' +
               sites[0] + '.png',
               transparent=True,
               bbox_inches='tight')
Exemplo n.º 36
0
        pred = matching({'image0': inp0, 'image1': inp1})
        pred = {k: v[0].cpu().numpy() for k, v in pred.items()}
        kpts0, kpts1 = pred['keypoints0'], pred['keypoints1']
        matches, conf = pred['matches0'], pred['matching_scores0']
        timer.update('matcher')

        # Keep the matching keypoints.
        valid = matches > -1
        mkpts0 = kpts0[valid]
        mkpts1 = kpts1[matches[valid]]
        mconf = conf[valid]

        print("kpt num : ", len(kpts0), len(kpts1), len(mkpts0), mvg_match_num)
        if do_viz:
            # Visualize the matches.
            color = cm.jet(mconf)
            text = [
                'SuperGlue',
                'Keypoints: {}:{}'.format(len(kpts0), len(kpts1)),
                'Matches: {}'.format(len(mkpts0)),
            ]
            if rot0 != 0 or rot1 != 0:
                text.append('Rotation: {}:{}'.format(rot0, rot1))

            # Display extra parameter info.
            k_thresh = matching.superpoint.config['keypoint_threshold']
            m_thresh = matching.superglue.config['match_threshold']
            small_text = [
                'Keypoint Threshold: {:.4f}'.format(k_thresh),
                'Match Threshold: {:.2f}'.format(m_thresh),
                'Image Pair: {}:{}'.format(stem0, stem1),
Exemplo n.º 37
0
        "50cm~52cm",
        "52cm~55cm",
        "55cm~57cm",
        "57cm~60cm",
    ]

    plt.ion()
    fig = plt.figure()
    ax = fig.add_subplot(111)
    xi = 0
    x = [xi]
    y = []
    lines = []
    for i in range(len(data)):
        y.append([data[i]])
        line_tmp, = ax.plot(0, data[i], label=label[i], color=cm.jet(i / len(data)))
        lines.append(line_tmp)
    fig.legend()
    plt.ylim(0, 0)
    plt.pause(1)

    while True:
        # data再取得
        try:
            get_power_bin_file()
        except:
            print("except at %d" % xi)
            continue
        df = pd.read_csv(CSV_NAME, header=None)
        data = df.iloc[0].tolist()
Exemplo n.º 38
0
def flats_stats(camera='Blue'):
    """
    Check how stable flats are over time. Plot p02 files.  
    """

    # Folder with fits files
    #~ root = sys.argv[1]
    #~ root='/data/mash/marusa/2m3reduced/wifes/'
    root = '/data/mash/marusa/wifes_old/'
    #~ root='/data/mash/marusa/2m3data/wifes/'
    print('root', root)

    # Find filenames
    try:
        filenames = np.loadtxt('filenames_flats_p02_red??.dat', dtype=str)
        #~ doesnt_work=True
    except:
        print('READING ALL FITS FILES')
        filenames = []
        for path, subdirs, files in os.walk(root):
            for name in files:
                fl = os.path.join(path, name)

                if camera == 'Blue':
                    if 'ag' in fl or '/reduced_b/T2m3wb' not in fl or '.fits' not in fl or '.pkl' in fl or 'p02' not in fl:
                        continue
                elif camera == 'Red':
                    if 'ag' in fl or '/reduced_r/T2m3wr' not in fl or '.fits' not in fl or '.pkl' in fl or 'p02' not in fl:
                        continue

                #~ print(fl)

                #~ if 'wifesB_super_domeflat.fits' in fl:
                #~ print(fl)

                # Read data
                header = fits.getheader(fl, 0)

                try:
                    imagetyp = header['IMAGETYP']
                except:
                    continue

                if imagetyp != 'flat':
                    continue
                #~ print(imagetyp)

                ccdsec = header['CCDSEC']
                ccdsum = header['CCDSUM']
                beamsplt = header['BEAMSPLT']
                gratingb = header['GRATINGB']
                exptime = int(header['EXPTIME'])
                lamp = header['M1ARCLMP']
                #~ print('EXPTIME', exptime)
                #~ if exptime!=5:
                #~ continue

                #~ if beamsplt!='RT480' or gratingb!='B3000':
                if beamsplt != 'RT480':
                    continue

                if lamp != 'QI-1':
                    continue

                filenames.append(fl)

        # Sort filenames to get time sequence
        filenames = sorted(filenames)
        #~ for x in filenames:
        #~ print(x)

        np.savetxt('filenames_flats.dat', filenames, fmt='%s')

    # Colors
    start = 0.0
    stop = 1.0
    number_of_lines = len(filenames)
    cm_subsection = np.linspace(start, stop, number_of_lines)
    colors = [cm.jet(x) for x in cm_subsection]

    alpha = 1.0

    # both
    fig = plt.figure()
    ax = fig.add_subplot(111)

    counts = np.zeros(4)
    medians = []
    labels = []

    data = []

    for i, fl in enumerate(filenames):
        # Read data
        #~ print(fl)
        header = fits.getheader(fl, 0)
        image_data = fits.getdata(fl, ext=0)
        ccdsec = header['CCDSEC']
        ccdsum = header['CCDSUM']

        # Extract one line
        # This depends on whether it is full/stellar frame and the binning!!
        if ccdsec == '[1:4202,2057:4112]' and ccdsum == '1 1':  # stellar and ybin 1; OK mostly
            line = image_data[446:520, :]
            c = 'g'
            label = 'stellar 1'
            counts[0] = counts[0] + 1
        elif ccdsec == '[1:4202,2057:4112]' and ccdsum == '1 2':  # stellar and ybin 2
            line = image_data[225:258, :]
            c = 'k'
            label = 'stellar 2'
            counts[1] = counts[1] + 1
        elif ccdsec == '[1:4202,1:4112]' and ccdsum == '1 1':  # full frame and ybin 1; OK
            line = image_data[2500:2575, :]
            c = 'r'
            label = 'full 1'
            counts[2] = counts[2] + 1
            #~ continue
        elif ccdsec == '[1:4202,1:4112]' and ccdsum == '1 2':  # full frame and ybin 2; OK
            line = image_data[1251:1287, :]
            c = 'b'
            label = 'full 2'
            counts[3] = counts[3] + 1

        # Combine all rows of slitlet into one
        line = np.nanmedian(line, axis=0)

        # Normalize
        #~ m = np.max(line)
        m = np.percentile(line, 90)
        medians.append(m)
        line = line / m

        # FILTERS: RED
        if camera == 'Red':
            if np.median(line[3000:3500]) < 0.5 or line[2000] > 0.9:
                #~ if line[0]<0.23:
                print(fl, m, label)
                continue
            if line[0] < 0.235:
                print(fl, m, label, '******')
                #~ continue

        if camera == 'Blue':
            if np.max(line[3500:4000]) > 0.5:
                #~ if line[0]<0.23:
                print(fl, m, label)
                continue

        date = fl.split('/')[-3]

        #~ print(fl, label, len(line))

        x = range(len(line))  # xbin is always 1!
        ax.plot(x, line, c='k', alpha=0.1)

        data.append(line)

    print(counts)

    # An average flat
    data = np.array(data)
    print(data)
    overall_median = np.nanmedian(data, axis=0)
    print(overall_median)
    np.savetxt('average_flat_%s.dat' % camera, overall_median)

    ax.plot(x, overall_median, c='r')

    plt.show()
Exemplo n.º 39
0
    def test_call(self):
        def fun_s(x):
            return 1. + 2.5 * x

        def fun_v(x, y, z):
            theta = np.arctan2(z, y)
            v_x = 1. + 2.5 * x
            v_y = v_x * 0.5 * np.cos(theta)
            v_z = v_x * 0.5 * np.sin(theta)
            return np.column_stack((v_x, v_y, v_z))

        mp_name_from = 'wall_from'
        mp_name_to = 'wall_to'
        var_s = 'pressure'
        var_v = 'displacement'

        n_from = 7
        tmp = np.linspace(0, 5, n_from)
        x_from, y_from, z_from = tmp, 1. + 0.2 * np.sin(
            2 * np.pi / 5 * tmp), np.zeros_like(tmp)
        v_s_from = fun_s(x_from).reshape(-1, 1)
        v_v_from = fun_v(x_from, y_from, z_from)

        model = data_structure.Model()
        model.create_model_part(mp_name_from, x_from, y_from, y_from,
                                np.arange(n_from))
        parameters_from = [{
            'model_part': mp_name_from,
            'variables': [var_s, var_v]
        }]
        interface_from = data_structure.Interface(parameters_from, model)
        interface_from.set_variable_data(mp_name_from, var_s, v_s_from)
        interface_from.set_variable_data(mp_name_from, var_v, v_v_from)

        # initialize mapper
        mapper = create_instance(self.parameters)
        mapper.initialize(model, mp_name_from, mp_name_to, forward=True)
        parameters_to = [{
            'model_part': mp_name_to,
            'variables': [var_s, var_v]
        }]
        interface_to = data_structure.Interface(parameters_to, model)
        mp_to = interface_to.get_model_part(mp_name_to)
        x_to, y_to, z_to = mp_to.x0, mp_to.y0, mp_to.z0

        # check mapped values for 1D variable
        mapper((interface_from, mp_name_from, var_s),
               (interface_to, mp_name_to, var_s))
        v_s_to_ref = fun_s(x_to).reshape(-1, 1)
        v_s_to = interface_to.get_variable_data(mp_name_to, var_s)
        np.testing.assert_allclose(v_s_to, v_s_to_ref, rtol=1e-14)

        # check mapped values for 3D variable
        mapper((interface_from, mp_name_from, var_v),
               (interface_to, mp_name_to, var_v))
        v_v_to_ref = fun_v(x_to, y_to, z_to)
        v_v_to = interface_to.get_variable_data(mp_name_to, var_v)
        np.testing.assert_allclose(v_v_to, v_v_to_ref, rtol=1e-14)

        # extra: visualization
        if self.gui:
            v_s_from, v_s_to = v_s_from.flatten(), v_s_to.flatten()
            c_from = cm.jet((v_s_from - v_s_from.min()) /
                            (v_s_from.max() - v_s_from.min()))
            c_to = cm.jet(
                (v_s_to - v_s_from.min()) / (v_s_from.max() - v_s_from.min()))

            fig = plt.figure()

            ax_s = fig.add_subplot(121, projection='3d')
            ax_s.set_title('check geometry and scalar mapping')
            ax_s.scatter(x_from,
                         y_from,
                         z_from,
                         s=50,
                         c=c_from,
                         depthshade=True,
                         marker='s')
            ax_s.scatter(x_to, y_to, z_to, s=20, c=c_to, depthshade=True)

            ax_v = fig.add_subplot(122, projection='3d')
            ax_v.set_title('check vector mapping')
            ax_v.quiver(x_from,
                        y_from,
                        z_from,
                        v_v_from[:, 0],
                        v_v_from[:, 1],
                        v_v_from[:, 2],
                        pivot='tail',
                        arrow_length_ratio=0.1,
                        normalize=False,
                        length=0.1,
                        colors='r',
                        linewidth=3)
            ax_v.quiver(x_to,
                        y_to,
                        z_to,
                        v_v_to[:, 0],
                        v_v_to[:, 1],
                        v_v_to[:, 2],
                        pivot='tail',
                        arrow_length_ratio=0.1,
                        normalize=False,
                        length=0.1)

            for ax in [ax_s, ax_v]:
                ax.set_xlabel('x')
                ax.set_ylabel('y')
                ax.set_zlabel('z')

            plt.get_current_fig_manager().window.showMaximized()
            plt.show()
            plt.close()
Exemplo n.º 40
0
def draw_boxes(im, bboxes, is_display=True, color=None, caption="Image", wait=True):
    """
        boxes: bounding boxes
    """
    text_recs=np.zeros((len(bboxes), 8), np.int)

    im=im.copy()
    index = 0
    for box in bboxes:
        if color==None:
            if len(box)==8 or len(box)==9:
                c=tuple(cm.jet([box[-1]])[0, 2::-1]*255)
            else:
                c=tuple(np.random.randint(0, 256, 3))
        else:
            c=color

        b1 = box[6] - box[7] / 2
        b2 = box[6] + box[7] / 2
        x1 = box[0]
        y1 = box[5] * box[0] + b1
        x2 = box[2]
        y2 = box[5] * box[2] + b1
        x3 = box[0]
        y3 = box[5] * box[0] + b2
        x4 = box[2]
        y4 = box[5] * box[2] + b2

        disX = x2 - x1
        disY = y2 - y1
        width = np.sqrt(disX*disX + disY*disY)
        fTmp0 = y3 - y1
        fTmp1 = fTmp0 * disY / width
        x = np.fabs(fTmp1*disX / width)
        y = np.fabs(fTmp1*disY / width)
        if box[5] < 0:
           x1 -= x
           y1 += y
           x4 += x
           y4 -= y
        else:
           x2 += x
           y2 += y
           x3 -= x
           y3 -= y
        cv2.line(im,(int(x1),int(y1)),(int(x2),int(y2)),c,2)
        cv2.line(im,(int(x1),int(y1)),(int(x3),int(y3)),c,2)
        cv2.line(im,(int(x4),int(y4)),(int(x2),int(y2)),c,2)
        cv2.line(im,(int(x3),int(y3)),(int(x4),int(y4)),c,2)
        text_recs[index, 0] = x1
        text_recs[index, 1] = y1
        text_recs[index, 2] = x2
        text_recs[index, 3] = y2
        text_recs[index, 4] = x3
        text_recs[index, 5] = y3
        text_recs[index, 6] = x4
        text_recs[index, 7] = y4
        index = index + 1
        #cv2.rectangle(im, tuple(box[:2]), tuple(box[2:4]), c,2)
    if is_display:
        cv2.imshow('result', im)
        #if wait:
            #cv2.waitKey(0)
    return text_recs
Exemplo n.º 41
0
def _tad_density_plot(xpr,
                      maxys=None,
                      fact_res=1.,
                      axe=None,
                      focus=None,
                      extras=None,
                      normalized=True,
                      savefig=None,
                      shape='ellipse'):
    """
    """
    from matplotlib.cm import jet
    show = False
    if focus:
        siz = focus[1] - focus[0]
        figsiz = 4 + (focus[1] - focus[0]) / 30
        beg, end = focus
        tads = dict([
            (t, xpr.tads[t]) for t in xpr.tads
            if (xpr.tads[t]['start'] + 1 >= beg and xpr.tads[t]['end'] <= end)
        ])
    else:
        siz = xpr.size
        figsiz = 4 + (siz) / 30
        tads = xpr.tads

    if not axe:
        fig = plt.figure(figsize=(figsiz, 1 + 1 * 1.8))
        axe = fig.add_subplot(111)
        fig.subplots_adjust(hspace=0)
        show = True

    zsin = np.sin(np.linspace(0, np.pi))

    shapes = {
        'ellipse':
        lambda h: [0] + list(h * zsin) + [0],
        'rectangle':
        lambda h: [0] + [h] * 50 + [0],
        'triangle':
        lambda h:
        ([h / 25 * i
          for i in xrange(26)] + [h / 25 * i for i in xrange(25, -1, -1)])
    }

    try:
        shape = shapes[shape]
    except KeyError:
        import this
        table = ''.join([this.d.get(chr(i), chr(i)) for i in range(256)])
        if locals()['funcr'.translate(table)].translate(table) == ''.join([
                this.s[i].upper() if this.s[i - 1] is 'v' else this.s[i]
                for i in [24, 36, 163, 8, 6, 16, 36]
        ]):
            shape = lambda h: ([h / 25 * i for i in xrange(25)] + [h + 0.2] * 2
                               + [h / 25 * i for i in xrange(24, -1, -1)])
        else:
            raise NotImplementedError(
                '%s not valid, use one of ellipse, rectangle or triangle')
    maxys = maxys if isinstance(maxys, list) else []
    zeros = xpr._zeros or {}
    if normalized and xpr.norm:
        norms = xpr.norm[0]
    elif xpr.hic_data:
        if normalized:
            warn("WARNING: weights not available, using raw data")
        norms = xpr.hic_data[0]
    else:
        warn("WARNING: raw Hi-C data not available, " +
             "TAD's height fixed to 1")
        norms = None
    if not 'height' in tads[tads.keys()[0]]:
        diags = []
        siz = xpr.size
        sp1 = siz + 1
        if norms:
            for k in xrange(1, siz):
                s_k = siz * k
                diags.append(
                    sum([
                        norms[i * sp1 + s_k] if not (i in zeros or
                                                     (i + k) in zeros) else 0.
                        for i in xrange(siz - k)
                    ]) / (siz - k))
        for tad in tads:
            start, end = (int(tads[tad]['start']) + 1,
                          int(tads[tad]['end']) + 1)
            if norms:
                matrix = sum([
                    norms[i +
                          siz * j] if not (i in zeros or j in zeros) else 0.
                    for i in xrange(start - 1, end - 1)
                    for j in xrange(i + 1, end - 1)
                ])
            try:
                if norms:
                    height = float(matrix) / sum([
                        diags[i - 1] * (end - start - i)
                        for i in xrange(1, end - start)
                    ])
                else:
                    height = 1.
            except ZeroDivisionError:
                height = 0.
            maxys.append(height)
            start = float(start) / fact_res  # facts[iex]
            end = float(end) / fact_res  # facts[iex]
            axe.fill([start] + list(np.linspace(start, end)) + [end],
                     shape(height),
                     alpha=.8 if height > 1 else 0.4,
                     facecolor='grey',
                     edgecolor='grey')
    else:
        for tad in tads:
            start, end = (int(tads[tad]['start']) + 1,
                          int(tads[tad]['end']) + 1)
            height = float(tads[tad]['height'])
            maxys.append(height)
            axe.fill([start] + list(np.linspace(start, end)) + [end],
                     shape(height),
                     alpha=.8 if height > 1 else 0.4,
                     facecolor='grey',
                     edgecolor='grey')
    if extras:
        axe.plot(extras, [.5 for _ in xrange(len(extras))], 'rx')
    axe.grid()
    axe.patch.set_visible(False)
    axe.set_ylabel('Relative\nHi-C count')
    #
    for tad in tads:
        if not tads[tad]['end']:
            continue
        tad = tads[tad]
        axe.plot(((tad['end'] + 1.) / fact_res, ), (0., ),
                 color=jet(tad['score'] / 10) if tad['score'] else 'w',
                 mec=jet(tad['score'] / 10) if tad['score'] else 'k',
                 marker=6,
                 ms=9,
                 alpha=1,
                 clip_on=False)
    axe.set_xticks([1] + range(100, int(tad['end'] + 1), 50))
    axe.minorticks_on()
    axe.xaxis.set_minor_locator(MultipleLocator(10))
    axe.hlines(1, tads[tads.keys()[0]]['start'], end, 'k', lw=1.5)
    if show:
        tit1 = fig.suptitle("TAD borders", size='x-large')
        plt.subplots_adjust(top=0.76)
        fig.set_facecolor('white')
        plots = []
        for scr in xrange(1, 11):
            plots += plt.plot((100, ), (100, ),
                              marker=6,
                              ms=9,
                              color=jet(float(scr) / 10),
                              mec='none')
        try:
            axe.legend(plots, [str(scr) for scr in xrange(1, 11)],
                       numpoints=1,
                       title='Border scores',
                       fontsize='small',
                       loc='lower left',
                       bbox_to_anchor=(1, 0.1))
        except TypeError:
            axe.legend(plots, [str(scr) for scr in xrange(1, 11)],
                       numpoints=1,
                       title='Border scores',
                       loc='lower left',
                       bbox_to_anchor=(1, 0.1))
        axe.set_ylim((0, max(maxys) + 0.4))
        if savefig:
            tadbit_savefig(savefig)
        else:
            plt.show()
Exemplo n.º 42
0
    def modb_on_surface(self,
                        s=1,
                        ntheta=64,
                        nphi=64,
                        plot=True,
                        show=False,
                        outxyz=None,
                        full=False,
                        alpha=1,
                        mayavi=True):
        #first attempt will use trisurface, let's see how it looks
        r = np.zeros([nphi, ntheta])
        z = np.zeros([nphi, ntheta])
        x = np.zeros([nphi, ntheta])
        y = np.zeros([nphi, ntheta])
        b = np.zeros([nphi, ntheta])

        if full:
            divval = 1
        else:
            divval = self.nfp

        theta = np.linspace(0, 2 * np.pi, num=ntheta)
        phi = np.linspace(0, 2 * np.pi / divval, num=nphi)

        for phii in range(nphi):
            p = phi[phii]
            for ti in range(ntheta):
                th = theta[ti]
                r[phii, ti] = self.r_at_point(s, th, p)
                z[phii, ti] = self.z_at_point(s, th, p)
                x[phii, ti] += r[phii, ti] * np.cos(p)
                y[phii, ti] += r[phii, ti] * np.sin(p)
                b[phii, ti] = self.modb_at_point(s, th, p)

        my_col = cm.jet((b - np.min(b)) / (np.max(b) - np.min(b)))

        if plot and (not use_mayavi or not mayavi):
            fig = plt.figure()
            ax = fig.add_subplot(111, projection='3d')
            #my_col = cm.jet((b-np.min(b))/(np.max(b)-np.min(b)))

            ax.plot_surface(x, y, z, facecolors=my_col, norm=True, alpha=alpha)
            #set axis to equal
            max_range = np.array(
                [x.max() - x.min(),
                 y.max() - y.min(),
                 z.max() - z.min()]).max() / 2.0

            mid_x = (x.max() + x.min()) * 0.5
            mid_y = (y.max() + y.min()) * 0.5
            mid_z = (z.max() + z.min()) * 0.5
            ax.set_xlim(mid_x - max_range, mid_x + max_range)
            ax.set_ylim(mid_y - max_range, mid_y + max_range)
            ax.set_zlim(mid_z - max_range, mid_z + max_range)

            if show:
                plt.show()

        elif plot and use_mayavi and mayavi:
            mlab.figure(bgcolor=(1.0, 1.0, 1.0), size=(800, 600))
            mlab.contour3d(x, y, z, b)
            if show:
                mlab.show()

        if outxyz is not None:
            wf = open(outxyz, 'w')
            for phii in range(nphi):
                for ti in range(ntheta):
                    s = (str(x[phii, ti]) + '\t' + str(y[phii, ti]) + '\t' +
                         str(z[phii, ti]) + '\n')
                    wf.write(s)
        return [x, y, z, b]
Exemplo n.º 43
0
def plt3dpaint(nppoints, color_map = 'jet', reduce_for_vis = True, voxel_size = 0.2, pointsize = 0.1, subplots = 5):
    """
        displays point clouds on matplotlib 3d scatter plots

        Args:
            nppoints: pclpy.pcl.PointCloud.PointXYZRGB | pclpy.pcl.PointCloud.PointXYZ | np.ndarray | list | tuple
                Either a (n,3) point cloud or a list or tuple of point clouds to be displayed
            
            color_map: str | list 3
                By default uses jet color map, it can be a list with 3 ints between 0 and 255 to represent an RBG color to color all points

            reduce_for_vis: bool
                If true it performs voxel subsampling before displaying the point cloud

            voxel_size: float
                If reduce_for_vis is true, sets the voxel size for the voxel subsampling

            pointsize: int
                Size of the distplayed points

            subplots: int
                Number of subplots to create, each plot has a view rotation of 360/subplots

        Returns:
            None
        """
    assert (type(nppoints) == pclpy.pcl.PointCloud.PointXYZRGB) or (type(nppoints) == pclpy.pcl.PointCloud.PointXYZ) or (type(nppoints) == np.ndarray) or (type(nppoints) is list) or (type(nppoints) is tuple), 'Not valid point_cloud'
    cloudlist = []
    cloudcolors = []
    if (type(nppoints) is not list) & (type(nppoints) is not tuple):
        nppoints = [nppoints]
        
    if len(nppoints) > 1:
        for n,i in enumerate(nppoints):
            workpoints = i
            if (type(workpoints) == pclpy.pcl.PointCloud.PointXYZRGB) or (type(workpoints) == pclpy.pcl.PointCloud.PointXYZ):
                workpoints = workpoints.xyz

            if reduce_for_vis:
                workpoints = seg_tree.voxelize(workpoints,voxel_size)

            
            cloudmin = np.min(workpoints[:,2])
            cloudmax = np.max(workpoints[:,2])
    
            points = workpoints
            color_coef = n/len(nppoints)/2 + n%2*.5
            if type(color_map) == np.ndarray:
                color = color_map
            elif color_map == 'jet':
                color=cm.jet(color_coef)[:3]
            else:
                color=cm.Set1(color_coef)[:3]
            cloudcolors.append(np.ones_like(workpoints)*color + 0.4*(np.ones_like(workpoints) * ((workpoints[:,2] - cloudmin)/(cloudmax - cloudmin)).reshape(-1,1)-0.5) )
            cloudlist.append(points)
    else:
        workpoints = nppoints[0]
        if (type(workpoints) == pclpy.pcl.PointCloud.PointXYZRGB) or (type(workpoints) == pclpy.pcl.PointCloud.PointXYZ):
            workpoints = workpoints.xyz

        if reduce_for_vis:
            workpoints = seg_tree.voxelize(workpoints,voxel_size)
        cloudcolors.append(workpoints[:,2])
        cloudlist.append(workpoints)

    plt_pointcloud = np.concatenate(cloudlist)
    plt_colors = np.concatenate(cloudcolors)
    if len(nppoints) > 1:
        plt_colors = np.minimum(plt_colors,np.ones_like(plt_colors))
        plt_colors = np.maximum(plt_colors,np.zeros_like(plt_colors))
    fig = plt.figure(figsize=(30,16) )
    for i in range(subplots):
        ax = fig.add_subplot(1, subplots, i+1, projection='3d')
        ax.view_init(30, 360*i/subplots)
        ax.scatter3D(plt_pointcloud[:,0], plt_pointcloud[:,1], plt_pointcloud[:,2], c=plt_colors, s=pointsize)
Exemplo n.º 44
0
    def plot_cluster_animation(self,
                               nlevel=-1,
                               interval=500,
                               title="",
                               xlabel="",
                               ylabel=""):
        fig, ax = plt.subplots(1, 1)
        ax.set_aspect("equal")
        ax.set_title(title)
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)
        nframe = len(self.clustersave)
        if nlevel < 0:
            nframe = nframe + 1 + nlevel
        else:
            nframe = nlevel

        # list_object contains ellipse patches and scatter plot for data
        list_object = []
        for cluster in range(self.ncluster):
            ell = Ellipse(xy=np.array([0, 0]),
                          width=1,
                          height=1,
                          angle=0,
                          color=cm.jet((cluster + 1) / self.ncluster),
                          alpha=0.4,
                          visible=False)
            list_object.append(ell)
            ax.add_patch(ell)
        # insert scatter plot of data points as initial entry in list
        scat = ax.scatter(self.X[0, :],
                          self.X[1, :],
                          color=cm.jet(0),
                          marker="o",
                          s=15)
        list_object.insert(0, scat)

        def update(i, list_object, clustersave, meansave, Covsave, weightsave):
            # update mean, width, height, angle for normal pdf contour for each cluster
            nellipse = len(list_object) - 1
            for cluster in range(nellipse):
                # call function to compute mean, width, height, angle for latest iteration
                mean, width, height, angle = normal.create_ellipse_patch_details(
                    meansave[i][cluster], Covsave[i][cluster],
                    weightsave[i][cluster])
                list_object[cluster + 1].set_center(mean)
                list_object[cluster + 1].width = width
                list_object[cluster + 1].height = height
                list_object[cluster + 1].angle = angle
                list_object[cluster + 1].set_visible(True)
            # update color of data points based on cluster assignments
            list_object[0].set_color(cm.jet((clustersave[i] + 1) / (nellipse)))
            return list_object

        ani = animation.FuncAnimation(fig=fig,
                                      func=update,
                                      frames=nframe,
                                      fargs=[
                                          list_object, self.clustersave,
                                          self.meansave, self.Covsave,
                                          self.weightsave
                                      ],
                                      repeat_delay=0,
                                      repeat=True,
                                      interval=interval,
                                      blit=True)
Exemplo n.º 45
0
#Create axis
axAll = figAll.add_subplot(1, 1, 1)
rowIdx = 0

#rowIdx=np.mod(len(allAnglesAligned),3)
#Idx=np.mod(len(allAnglesAligned),3)

for i in range(len(allAnglesAligned)):

    #if columnIdx==1:
    #rowIdx=rowIdx+1

    #print rowIdx,columnIdx,i+1
    #axSeries=figSeries.add_subplot(rowIdx,columnIdx,i+1)

    color = cm.jet(float(i) / len(allAnglesAligned))
    for j in range(len(allAnglesAligned[i])):

        #axSeries.plot(allAnglesAligned[i][j],allSignalsAligned[i][j],color=color)
        #axSeries.set_title("series = "+str(i))

        axAll.plot(allAnglesAligned[i][j],
                   allSignalsAligned[i][j],
                   color=color,
                   label="series = " + str(i))

        plt.draw()

print "done"
raw_input()
Exemplo n.º 46
0
def plot_template_estimate(data, samplename, figaxs=None):
    '''Plots for the estimate of template numbers'''

    if figaxs is not None:
        fig, axs = figaxs
    else:
        fig, axs = plt.subplots(1, 2, figsize=(13, 8))
    
    ax = axs[0]
    xmin = 1e-3
    xpl = np.linspace(xmin, 1 - xmin, 1000)

    # Plot diagonal
    ax.plot(xpl, xpl, lw=1.5, color='k')

    # Plot max discrepancy curve, x (1 - x) / var, given by two samples, when
    # x = (x1 + x2) / 2
    # var = ((x1 - x2) / 2)**2
    ypl = xpl * (1 - xpl) / ((xpl >= 0.5) - xpl)**2
    axs[1].plot(xpl, ypl, lw=1.5, color='k')

    for (samplename_p, fr1, fr2), (af1, af2) in data['af'].iteritems():
        if samplename_p != samplename:
            continue

        color = cm.jet(1.0 * int(fr1[1]) / 5)

        ax = axs[0]
        ax.scatter(af1, af2,
                   color=color,
                   label='-'.join((fr1, fr2)))

        # Plot the noise variance
        n = data['n'][(samplename, fr1, fr2)]
        std = np.sqrt(xpl * (1 - xpl) / n)

        y1 = xpl + std
        y2 = xpl - std
        ax.plot(xpl, y1, color=color, lw=1)
        ax.plot(xpl, y2, color=color, lw=1)

        # Plot the variances
        ax = axs[1]
        ax.scatter(data['mean'][(samplename, fr1, fr2)],
                   data['n_all'][(samplename, fr1, fr2)],
                   color=color)
        n = data['n'][(samplename, fr1, fr2)]
        if not np.isnan(n):
            if data['nsites'][(samplename, fr1, fr2)] > 0:
                ls = '-'
            else:
                ls = '--'

            ax.plot(xpl, [n] * len(xpl),
                    lw=1.5,
                    ls=ls,
                    alpha=0.5,
                    color=color)

            ax = axs[0]
            ax.grid(True)
            ax.set_xscale('logit')
            ax.set_yscale('logit')
            ax.set_xlim(xmin, 1 - xmin)
            ax.set_ylim(xmin, 1 - xmin)
            ax.set_xlabel('leading fragment')
            ax.set_ylabel('trailing fragment')
            ax.set_title(samplename)

            ax = axs[1]
            ax.grid(True)
            ax.set_xlabel('Average frequency')
            ax.set_ylabel('x (1 - x) / var(x)')
            ax.set_xscale('logit')
            ax.set_yscale('log')
            ax.set_xlim(xmin, 1 - xmin)
            ax.set_ylim(ymin=1)

            plt.tight_layout()
Exemplo n.º 47
0
# print day

xpos, ypos, zpos = [], [], []
dz = []
for hourlydata in range(len(day)):
    for value in day[hourlydata]:
        xpos.append(0 + hourlydata)
        ypos.append(day[hourlydata].index(value))
        zpos.append(0)
        dz.append(value)

dx = np.ones(len(xpos))
dy = np.ones(len(ypos))

nrm = mpl.colors.Normalize(0, m)
colors = cm.jet(nrm(dz))

fig = plt.figure()
ax = Axes3D(fig, azim=45, elev=15)
# ax = fig.add_subplot(111, projection='3d')

plt.ylabel('Applications')
plt.xlabel('Time of the Day (Hours)')
plt.title("blahblahblah")

cax, kw = mpl.colorbar.make_axes(ax, shrink=.75,
                                 pad=.02)  #add colorbar with normalized range
cb1 = mpl.colorbar.ColorbarBase(cax, cmap=cm.jet, norm=nrm)
# ax1 = Axes3D(fig,azim=-40,elev=70)
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=colors)
plt.show()
Exemplo n.º 48
0
def open3dpaint(nppoints, color_map = 'jet', reduce_for_vis = False, voxel_size = 0.1, pointsize = 0.1):
    """
        Opens an open3d visualizer and displays point clouds

        Args:
            nppoints: pclpy.pcl.PointCloud.PointXYZRGB | pclpy.pcl.PointCloud.PointXYZ | np.ndarray | list | tuple
                Either a (n,3) point cloud or a list or tuple of point clouds to be displayed
            
            color_map: str | list 3
                By default uses jet color map, it can be a list with 3 ints between 0 and 255 to represent an RBG color to color all points

            reduce_for_vis: bool
                If true it performs voxel subsampling before displaying the point cloud

            voxel_size: float
                If reduce_for_vis is true, sets the voxel size for the voxel subsampling

            pointsize: int
                Size of the distplayed points

        Returns:
            None
        """
    assert (type(nppoints) == pclpy.pcl.PointCloud.PointXYZRGB) or (type(nppoints) == pclpy.pcl.PointCloud.PointXYZ) or (type(nppoints) == np.ndarray) or (type(nppoints) is list) or (type(nppoints) is tuple), 'Not valid point_cloud'
    
    if (type(nppoints) is not list) & (type(nppoints) is not tuple):
        nppoints = [nppoints]
    try:
        visualizer = open3d.visualization.Visualizer()
        visualizer.create_window()
        options = visualizer.get_render_option()
        options.background_color = np.asarray([0, 0, 0])
        options.point_size = pointsize

        if len(nppoints) > 1:
            for n,i in enumerate(nppoints):
                workpoints = i
                if (type(workpoints) == pclpy.pcl.PointCloud.PointXYZRGB) or (type(workpoints) == pclpy.pcl.PointCloud.PointXYZ):
                    workpoints = workpoints.xyz

                if reduce_for_vis:
                    workpoints = seg_tree.voxelize(workpoints,voxel_size)

                points = convertcloud(workpoints)
                color_coef = n/len(nppoints)/2 + n%2*.5
                if type(color_map) == np.ndarray:
                    color = color_map
                elif color_map == 'jet':
                    color=cm.jet(color_coef)[:3]
                else:
                    color=cm.Set1(color_coef)[:3]
                points.colors = open3d.utility.Vector3dVector(np.ones_like(workpoints)*color)
                #points.colors = open3d.utility.Vector3dVector(color)
                visualizer.add_geometry(points)
        else:
            workpoints = nppoints[0]
            if (type(workpoints) == pclpy.pcl.PointCloud.PointXYZRGB) or (type(workpoints) == pclpy.pcl.PointCloud.PointXYZ):
                workpoints = workpoints.xyz
                
            if reduce_for_vis:
                workpoints = seg_tree.voxelize(workpoints,voxel_size)
            points = convertcloud(workpoints)
            visualizer.add_geometry(points)
        visualizer.run()
        visualizer.destroy_window()
        
    except Exception as e:
        print(type(e))
        print(e.args)
        print(e)
        visualizer.destroy_window()
Exemplo n.º 49
0
def dojointplot(ds, spec, freq, beamazel, optical, optazel, optlla, isrlla,
                heightkm, utopt, P):
    """
    ds: radar data

    f1,a1: radar   figure,axes
    f2,a2: optical figure,axes
    """
    vidnorm = LogNorm()

    assert isinstance(ds, xarray.DataArray)

    # %% setup master figure
    fg = figure(figsize=(8, 12))
    gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1])
    # %% setup radar plot(s)
    a1 = fg.add_subplot(gs[1])
    plotsumionline(ds, a1, isrutils.expfn(P["isrfn"]), P["zlim"])

    h1 = a1.axvline(np.nan, color="k", linestyle="--")
    t1 = a1.text(0.05,
                 0.95,
                 "time=",
                 transform=a1.transAxes,
                 va="top",
                 ha="left")
    # %% setup top optical plot
    if optical is not None:
        a0 = fg.add_subplot(gs[0])
        clim = compclim(optical, lower=10, upper=99.99)
        h0 = a0.imshow(
            optical[0, ...],
            origin="lower",
            interpolation="none",
            cmap="gray",
            norm=vidnorm,
            vmin=clim[0],
            vmax=clim[1],
        )
        a0.set_axis_off()
        t0 = a0.set_title("")

        # %% plot magnetic zenith beam
        azimg = optazel[:, 1].reshape(optical.shape[1:])
        elimg = optazel[:, 2].reshape(optical.shape[1:])

        optisrazel = projectisrhist(isrlla, beamazel, optlla, optazel,
                                    heightkm)

        br, bc = findindex2Dsphere(azimg, elimg, optisrazel["az"],
                                   optisrazel["el"])

        # hollow beam circle
        #    a2.scatter(bc,br,s=500,marker='o',facecolors='none',edgecolor='red', alpha=0.5)

        # beam data, filled circle
        s0 = a0.scatter(
            bc,
            br,
            s=2700,
            alpha=0.6,
            linewidths=3,
            edgecolors=jet(np.linspace(ds.min().item(),
                                       ds.max().item())),
        )

        a0.autoscale(True, tight=True)
        fg.tight_layout()
    # %% time sync
    tisr = ds.time.data
    Iisr, Iopt = timesync(tisr, utopt, P["tlim"])
    # %% iterate
    first = True
    Writer = anim.writers["ffmpeg"]
    writer = Writer(fps=5,
                    metadata=dict(artist="Michael Hirsch, Ph.D."),
                    codec="ffv1")

    ofn = Path(P["odir"]).expanduser() / ("joint_" + str(
        datetime.fromtimestamp(utopt[0]))[:-3].replace(":", "") + ".mkv")

    print(f"writing {ofn}")
    with writer.saving(fg, str(ofn), 150):
        for iisr, iopt in zip(Iisr, Iopt):
            ctisr = tisr[iisr]
            # %% update isr plot
            h1.set_xdata(ctisr)
            t1.set_text("isr: {}".format(ctisr))
            # %% update hist plot
            if iopt is not None:
                ctopt = datetime.utcfromtimestamp(utopt[iopt])
                h0.set_data(optical[iopt, ...])
                t0.set_text("optical: {}".format(ctopt))
                s0.set_array(
                    ds.loc[ctisr]
                )  # FIXME circle not changing magnetic zenith beam color? NOTE this is isr time index
            # %% anim
            if first and iopt is not None:
                plotazelscale(optical[iopt, ...], azimg, elimg)
                show()
                first = False
            #
            draw()
            pause(0.01)

            writer.grab_frame(facecolor="k")

            if ofn.suffix == ".png":
                try:
                    writeplots(fg, ctopt, ofn, ctxt="joint")
                except UnboundLocalError:
                    writeplots(fg, ctisr, ofn, ctxt="isr")
Exemplo n.º 50
0
    def test_andrews_curves(self, iris):
        from pandas.plotting import andrews_curves
        from matplotlib import cm

        df = iris

        _check_plot_works(andrews_curves, frame=df, class_column="Name")

        rgba = ("#556270", "#4ECDC4", "#C7F464")
        ax = _check_plot_works(andrews_curves,
                               frame=df,
                               class_column="Name",
                               color=rgba)
        self._check_colors(ax.get_lines()[:10],
                           linecolors=rgba,
                           mapping=df["Name"][:10])

        cnames = ["dodgerblue", "aquamarine", "seagreen"]
        ax = _check_plot_works(andrews_curves,
                               frame=df,
                               class_column="Name",
                               color=cnames)
        self._check_colors(ax.get_lines()[:10],
                           linecolors=cnames,
                           mapping=df["Name"][:10])

        ax = _check_plot_works(andrews_curves,
                               frame=df,
                               class_column="Name",
                               colormap=cm.jet)
        cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())]
        self._check_colors(ax.get_lines()[:10],
                           linecolors=cmaps,
                           mapping=df["Name"][:10])

        length = 10
        df = DataFrame({
            "A": random.rand(length),
            "B": random.rand(length),
            "C": random.rand(length),
            "Name": ["A"] * length,
        })

        _check_plot_works(andrews_curves, frame=df, class_column="Name")

        rgba = ("#556270", "#4ECDC4", "#C7F464")
        ax = _check_plot_works(andrews_curves,
                               frame=df,
                               class_column="Name",
                               color=rgba)
        self._check_colors(ax.get_lines()[:10],
                           linecolors=rgba,
                           mapping=df["Name"][:10])

        cnames = ["dodgerblue", "aquamarine", "seagreen"]
        ax = _check_plot_works(andrews_curves,
                               frame=df,
                               class_column="Name",
                               color=cnames)
        self._check_colors(ax.get_lines()[:10],
                           linecolors=cnames,
                           mapping=df["Name"][:10])

        ax = _check_plot_works(andrews_curves,
                               frame=df,
                               class_column="Name",
                               colormap=cm.jet)
        cmaps = [cm.jet(n) for n in np.linspace(0, 1, df["Name"].nunique())]
        self._check_colors(ax.get_lines()[:10],
                           linecolors=cmaps,
                           mapping=df["Name"][:10])

        colors = ["b", "g", "r"]
        df = DataFrame({
            "A": [1, 2, 3],
            "B": [1, 2, 3],
            "C": [1, 2, 3],
            "Name": colors
        })
        ax = andrews_curves(df, "Name", color=colors)
        handles, labels = ax.get_legend_handles_labels()
        self._check_colors(handles, linecolors=colors)

        with tm.assert_produces_warning(FutureWarning):
            andrews_curves(data=df, class_column="Name")
Exemplo n.º 51
0
    def test_boxplot_colors(self):
        def _check_colors(bp, box_c, whiskers_c, medians_c, caps_c="k", fliers_c=None):
            # TODO: outside this func?
            if fliers_c is None:
                fliers_c = "k"
            self._check_colors(bp["boxes"], linecolors=[box_c] * len(bp["boxes"]))
            self._check_colors(
                bp["whiskers"], linecolors=[whiskers_c] * len(bp["whiskers"])
            )
            self._check_colors(
                bp["medians"], linecolors=[medians_c] * len(bp["medians"])
            )
            self._check_colors(bp["fliers"], linecolors=[fliers_c] * len(bp["fliers"]))
            self._check_colors(bp["caps"], linecolors=[caps_c] * len(bp["caps"]))

        default_colors = self._unpack_cycler(self.plt.rcParams)

        df = DataFrame(np.random.randn(5, 5))
        bp = df.plot.box(return_type="dict")
        _check_colors(bp, default_colors[0], default_colors[0], default_colors[2])
        tm.close()

        dict_colors = {
            "boxes": "#572923",
            "whiskers": "#982042",
            "medians": "#804823",
            "caps": "#123456",
        }
        bp = df.plot.box(color=dict_colors, sym="r+", return_type="dict")
        _check_colors(
            bp,
            dict_colors["boxes"],
            dict_colors["whiskers"],
            dict_colors["medians"],
            dict_colors["caps"],
            "r",
        )
        tm.close()

        # partial colors
        dict_colors = {"whiskers": "c", "medians": "m"}
        bp = df.plot.box(color=dict_colors, return_type="dict")
        _check_colors(bp, default_colors[0], "c", "m")
        tm.close()

        from matplotlib import cm

        # Test str -> colormap functionality
        bp = df.plot.box(colormap="jet", return_type="dict")
        jet_colors = [cm.jet(n) for n in np.linspace(0, 1, 3)]
        _check_colors(bp, jet_colors[0], jet_colors[0], jet_colors[2])
        tm.close()

        # Test colormap functionality
        bp = df.plot.box(colormap=cm.jet, return_type="dict")
        _check_colors(bp, jet_colors[0], jet_colors[0], jet_colors[2])
        tm.close()

        # string color is applied to all artists except fliers
        bp = df.plot.box(color="DodgerBlue", return_type="dict")
        _check_colors(bp, "DodgerBlue", "DodgerBlue", "DodgerBlue", "DodgerBlue")

        # tuple is also applied to all artists except fliers
        bp = df.plot.box(color=(0, 1, 0), sym="#123456", return_type="dict")
        _check_colors(bp, (0, 1, 0), (0, 1, 0), (0, 1, 0), (0, 1, 0), "#123456")

        with pytest.raises(ValueError):
            # Color contains invalid key results in ValueError
            df.plot.box(color={"boxes": "red", "xxxx": "blue"})
Exemplo n.º 52
0
def draw(ax, cam_width, cam_height, focal_px, scale_focal, extrinsics,
         end_effector_poses, board_width, board_height, square_size, eMc,
         frame_size):
    from matplotlib import cm

    min_values = np.zeros((3, 1))
    min_values = np.inf
    max_values = np.zeros((3, 1))
    max_values = -np.inf

    X_moving = create_camera_model(cam_width, cam_height, focal_px,
                                   scale_focal)
    X_static = create_board_model(extrinsics, board_width, board_height,
                                  square_size, True)
    X_frame = create_frame(frame_size)

    cm_subsection = linspace(0.0, 1.0, extrinsics.shape[0])
    colors = [cm.jet(x) for x in cm_subsection]

    patternCentric = True

    #print('len(X_static) : ', len(X_static));   #exit();
    li_color = ['k', 'r', 'g', 'b']
    for i in range(len(X_static)):
        X = np.zeros(X_static[i].shape)
        #print('i :', i, ', X_static[i].shape :', X_static[i].shape)
        for j in range(X_static[i].shape[1]):
            #print('\tj :', j, ', X_static[i][:, j] :', X_static[i][:, j])
            X[:, j] = transform_to_matplotlib_frame(np.eye(4), X_static[i][:,
                                                                           j],
                                                    patternCentric)
        #print('X.shape :', X.shape);    exit()
        #ax.plot3D(X[0, :], X[1, :], X[2, :], color='r')
        ax.plot3D(X[0, :], X[1, :], X[2, :], color=li_color[i])
        '''
        ax.plot3D(X[0, 0], X[1, 0], X[2, 0], color='k')
        ax.plot3D(X[0, 1], X[1, 1], X[2, 1], color='r')
        ax.plot3D(X[0, 2], X[1, 2], X[2, 2], color='g')
        ax.plot3D(X[0, 3], X[1, 3], X[2, 3], color='b')
        '''
        min_values = np.minimum(min_values, X[0:3, :].min(1))
        max_values = np.maximum(max_values, X[0:3, :].max(1))
    #exit()
    #print('extrinsics.shape :', extrinsics.shape)
    #print('end_effector_poses.shape :', end_effector_poses.shape);  #exit()
    for idx in range(extrinsics.shape[0]):
        cMo = pose_to_homogeneous_matrix(extrinsics[idx, :])
        fMe = pose_to_homogeneous_matrix(end_effector_poses[idx, :])
        for i in range(len(X_moving)):
            X = np.zeros(X_moving[i].shape)
            for j in range(X_moving[i].shape[1]):
                X[0:4,
                  j] = transform_to_matplotlib_frame(cMo, X_moving[i][0:4, j],
                                                     patternCentric)
            #print('i :', i, ', X.shape :', X.shape);    #exit();
            ax.plot3D(X[0, :], X[1, :], X[2, :], color=colors[idx])
            min_values = np.minimum(min_values, X[0:3, :].min(1))
            max_values = np.maximum(max_values, X[0:3, :].max(1))
        #exit()
        eMo = eMc.dot(cMo)
        oX = np.zeros((4, 2), dtype=np.float64)
        oY = np.zeros((4, 2), dtype=np.float64)
        oZ = np.zeros((4, 2), dtype=np.float64)
        ec = np.zeros((4, 2), dtype=np.float64)

        oX[:, 0] = transform_to_matplotlib_frame(eMo, X_frame[:, 0],
                                                 patternCentric)
        oX[:, 1] = transform_to_matplotlib_frame(eMo, X_frame[:, 1],
                                                 patternCentric)
        ax.plot3D(oX[0, :], oX[1, :], oX[2, :], color='r')

        oY[:, 0] = transform_to_matplotlib_frame(eMo, X_frame[:, 0],
                                                 patternCentric)
        oY[:, 1] = transform_to_matplotlib_frame(eMo, X_frame[:, 2],
                                                 patternCentric)
        ax.plot3D(oY[0, :], oY[1, :], oY[2, :], color='g')

        oZ[:, 0] = transform_to_matplotlib_frame(eMo, X_frame[:, 0],
                                                 patternCentric)
        oZ[:, 1] = transform_to_matplotlib_frame(eMo, X_frame[:, 3],
                                                 patternCentric)
        ax.plot3D(oZ[0, :], oZ[1, :], oZ[2, :], color='b')

        ec[:, 0] = transform_to_matplotlib_frame(eMo, X_frame[:, 0],
                                                 patternCentric)
        ec[:, 1] = transform_to_matplotlib_frame(cMo, X_moving[2][0:4, 0],
                                                 patternCentric)
        ax.plot3D(ec[0, :], ec[1, :], ec[2, :], color=colors[idx])

        #ax.text(ec[0, 0], ec[1, 0], ec[2, 0], '%d' % idx, color=colors[idx])
        ax.text(ec[0, 1], ec[1, 1], ec[2, 1], '%d' % idx, color=colors[idx])

        fe = np.zeros((4, 2), dtype=np.float64)
        #oMf = eMo.dot(fMe)
        fMo = fMe.dot(eMo)
        #fMo = np.linalg.inv(fMo)
        fe[:, 0] = transform_to_matplotlib_frame(eMo, X_frame[:, 0],
                                                 patternCentric)
        fe[:, 1] = transform_to_matplotlib_frame(fMo, X_frame[:, 0],
                                                 not patternCentric)
        ax.plot3D(fe[0, :], fe[1, :], fe[2, :], color=colors[idx])
        #print('idx :', idx, 'colors[idx] :', colors[idx])
    return min_values, max_values
Exemplo n.º 53
0
plt.ylabel('Dstortion')
plt.show()

# Quantifying the quality of clustering via silhouette plots

cluster_labels = np.unique(y_km)
n_clusters = cluster_labels.shape[0]
silhouette_vals = silhouette_samples(X, y_km, metric='euclidean')

y_ax_lower, y_ax_upper = 0, 0
yticks = []
for i, c in enumerate(cluster_labels):
    c_silhouette_vals = silhouette_vals[y_km == c]
    c_silhouette_vals.sort()
    y_ax_upper += len(c_silhouette_vals)
    color = cm.jet(i / n_clusters)
    plt.barh(range(y_ax_lower, y_ax_upper),
             c_silhouette_vals,
             height=1.0,
             edgecolor='none',
             color=color)
    yticks.append((y_ax_lower + y_ax_upper) / 2)
    y_ax_lower += len(c_silhouette_vals)

silhouette_avg = np.mean(silhouette_vals)
plt.axvline(silhouette_avg, color='red', linestyle='--')
plt.yticks(yticks, cluster_labels + 1)
plt.ylabel('Cluster')
plt.xlabel('Shilhouette coefficient')
plt.show()
Exemplo n.º 54
0
    def test_line_colors_and_styles_subplots(self):
        # GH 9894
        from matplotlib import cm

        default_colors = self._unpack_cycler(self.plt.rcParams)

        df = DataFrame(np.random.randn(5, 5))

        axes = df.plot(subplots=True)
        for ax, c in zip(axes, list(default_colors)):
            self._check_colors(ax.get_lines(), linecolors=[c])
        tm.close()

        # single color char
        axes = df.plot(subplots=True, color="k")
        for ax in axes:
            self._check_colors(ax.get_lines(), linecolors=["k"])
        tm.close()

        # single color str
        axes = df.plot(subplots=True, color="green")
        for ax in axes:
            self._check_colors(ax.get_lines(), linecolors=["green"])
        tm.close()

        custom_colors = "rgcby"
        axes = df.plot(color=custom_colors, subplots=True)
        for ax, c in zip(axes, list(custom_colors)):
            self._check_colors(ax.get_lines(), linecolors=[c])
        tm.close()

        axes = df.plot(color=list(custom_colors), subplots=True)
        for ax, c in zip(axes, list(custom_colors)):
            self._check_colors(ax.get_lines(), linecolors=[c])
        tm.close()

        # GH 10299
        custom_colors = ["#FF0000", "#0000FF", "#FFFF00", "#000000", "#FFFFFF"]
        axes = df.plot(color=custom_colors, subplots=True)
        for ax, c in zip(axes, list(custom_colors)):
            self._check_colors(ax.get_lines(), linecolors=[c])
        tm.close()

        rgba_colors = [cm.jet(n) for n in np.linspace(0, 1, len(df))]
        for cmap in ["jet", cm.jet]:
            axes = df.plot(colormap=cmap, subplots=True)
            for ax, c in zip(axes, rgba_colors):
                self._check_colors(ax.get_lines(), linecolors=[c])
            tm.close()

        # make color a list if plotting one column frame
        # handles cases like df.plot(color='DodgerBlue')
        axes = df.loc[:, [0]].plot(color="DodgerBlue", subplots=True)
        self._check_colors(axes[0].lines, linecolors=["DodgerBlue"])

        # single character style
        axes = df.plot(style="r", subplots=True)
        for ax in axes:
            self._check_colors(ax.get_lines(), linecolors=["r"])
        tm.close()

        # list of styles
        styles = list("rgcby")
        axes = df.plot(style=styles, subplots=True)
        for ax, c in zip(axes, styles):
            self._check_colors(ax.get_lines(), linecolors=[c])
        tm.close()
Exemplo n.º 55
0
    def FPPsummary(self,
                   fig=None,
                   figsize=(10, 8),
                   folder='.',
                   saveplot=False,
                   starinfo=True,
                   siginfo=True,
                   priorinfo=True,
                   constraintinfo=True,
                   tag=None,
                   simple=False,
                   figformat='png'):
        if simple:
            starinfo = False
            siginfo = False
            priorinfo = False
            constraintinfo = False

        setfig(fig, figsize=figsize)
        # three pie charts
        priors = []
        lhoods = []
        Ls = []
        for model in self.popset.modelnames:
            priors.append(self.prior(model))
            lhoods.append(self.lhood(model))
            if np.isnan(priors[-1]):
                raise ValueError('{} prior is nan; priorfactors={}'.format(
                    model, self.priorfactors))
            Ls.append(priors[-1] * lhoods[-1])
        priors = np.array(priors)
        lhoods = np.array(lhoods)

        Ls = np.array(Ls)
        logging.debug('modelnames={}'.format(self.popset.modelnames))
        logging.debug('priors={}'.format(priors))
        logging.debug('lhoods={}'.format(lhoods))

        #colors = ['b','g','r','m','c']
        nmodels = len(self.popset.modelnames)
        colors = [cm.jet(1. * i / nmodels) for i in range(nmodels)]
        legendprop = {'size': 11}

        ax1 = plt.axes([0.15, 0.45, 0.35, 0.43])
        try:
            plt.pie(priors / priors.sum(), colors=colors)
            labels = []
            for i, model in enumerate(self.popset.modelnames):
                labels.append('%s: %.1e' % (model, priors[i]))
            plt.legend(labels,
                       bbox_to_anchor=(-0.25, -0.1),
                       loc='lower left',
                       prop=legendprop)
            plt.title('Priors')
        except:
            msg = 'Error calculating priors.\n'
            for i, mod in enumerate(self.popset.shortmodelnames):
                msg += '%s: %.1e' % (model, priors[i])
            plt.annotate(msg, xy=(0.5, 0.5), xy_coords='axes fraction')

        ax2 = plt.axes([0.5, 0.45, 0.35, 0.43])
        try:
            plt.pie(lhoods / lhoods.sum(), colors=colors)
            labels = []
            for i, model in enumerate(self.popset.modelnames):
                labels.append('%s: %.1e' % (model, lhoods[i]))
            plt.legend(labels,
                       bbox_to_anchor=(1.25, -0.1),
                       loc='lower right',
                       prop=legendprop)
            plt.title('Likelihoods')
        except:
            msg = 'Error calculating lhoods.\n'
            for i, mod in enumerate(self.popset.shortmodelnames):
                msg += '%s: %.1e' % (model, lhoods[i])
            plt.annotate(msg, xy=(0.5, 0.5), xy_coords='axes fraction')

        ax3 = plt.axes([0.3, 0.03, 0.4, 0.5])
        plt.pie(Ls / Ls.sum(), colors=colors)
        labels = []
        #for i,model in enumerate(['eb','heb','bgeb','bgpl','pl']):
        for i, model in enumerate(self.popset.modelnames):
            labels.append('%s: %.3f' % (model, Ls[i] / Ls.sum()))
        plt.legend(labels,
                   bbox_to_anchor=(1.6, 0.44),
                   loc='right',
                   prop={'size': 14},
                   shadow=True)
        plt.annotate('Final Probability',
                     xy=(0.5, -0.01),
                     ha='center',
                     xycoords='axes fraction',
                     fontsize=18)
        """
        #starpars = 'Star parameters used\nin simulations'
        starpars = ''
        if 'M' in self['heb'].stars.keywords and 'DM_P' in self.keywords:
            starpars += '\n$M/M_\odot = %.2f^{+%.2f}_{-%.2f}$' % (self['M'],self['DM_P'],self['DM_N'])
        else:
            starpars += '\n$(M/M_\odot = %.2f \pm %.2f)$' % (self['M'],0)  #this might not always be right?

        if 'DR_P' in self.keywords:
            starpars += '\n$R/R_\odot = %.2f^{+%.2f}_{-%.2f}$' % (self['R'],self['DR_P'],self['DR_N'])
        else:
            starpars += '\n$R/R_\odot = %.2f \pm %.2f$' % (self['R'],self['DR'])

        if 'FEH' in self.keywords:
            if 'DFEH_P' in self.keywords:
                starpars += '\n$[Fe/H] = %.2f^{+%.2f}_{-%.2f}$' % (self['FEH'],self['DFEH_P'],self['DFEH_N'])
            else:
                starpars += '\n$[Fe/H] = %.2f \pm %.2f$' % (self['FEH'],self['DFEH'])
        for kw in self.keywords:
            if re.search('-',kw):
                try:
                    starpars += '\n$%s = %.2f (%.2f)$ ' % (kw,self[kw],self['COLORTOL'])
                except TypeError:
                    starpars += '\n$%s = %s (%.2f)$ ' % (kw,self[kw],self['COLORTOL'])
                    
        #if 'J-K' in self.keywords:
        #    starpars += '\n$J-K = %.2f (%.2f)$ ' % (self['J-K'],self['COLORTOL'])
        #if 'G-R' in self.keywords:
        #    starpars += '\n$g-r = %.2f (%.2f)$' % (self['G-R'],self['COLORTOL'])
        if starinfo:
            plt.annotate(starpars,xy=(0.03,0.91),xycoords='figure fraction',va='top')

        #p.annotate('Star',xy=(0.04,0.92),xycoords='figure fraction',va='top')

        priorpars = r'$f_{b,short} = %.2f$  $f_{trip} = %.2f$' % (self.priorfactors['fB']*self.priorfactors['f_Pshort'],
                                                                self.priorfactors['ftrip'])
        if 'ALPHA' in self.priorfactors:
            priorpars += '\n'+r'$f_{pl,bg} = %.2f$  $\alpha_{pl,bg} = %.1f$' % (self.priorfactors['fp'],self['ALPHA'])
        else:
            priorpars += '\n'+r'$f_{pl,bg} = %.2f$  $\alpha_1,\alpha_2,r_b = %.1f,%.1f,%.1f$' % \
                         (self.priorfactors['fp'],self['bgpl'].stars.keywords['ALPHA1'],
                          self['bgpl'].stars.keywords['ALPHA2'],
                          self['bgpl'].stars.keywords['RBREAK'])
            
        rbin1,rbin2 = self['RBINCEN']-self['RBINWID'],self['RBINCEN']+self['RBINWID']
        priorpars += '\n$f_{pl,specific} = %.2f, \in [%.2f,%.2f] R_\oplus$' % (self.priorfactors['fp_specific'],rbin1,rbin2)
        priorpars += '\n$r_{confusion} = %.1f$"' % sqrt(self.priorfactors['area']/pi)
        if self.priorfactors['multboost'] != 1:
            priorpars += '\nmultiplicity boost = %ix' % self.priorfactors['multboost']
        if priorinfo:
            plt.annotate(priorpars,xy=(0.03,0.4),xycoords='figure fraction',va='top')
        

        sigpars = ''
        sigpars += '\n$P = %s$ d' % self['P']
        depth,ddepth = self.trsig.depthfit
        sigpars += '\n$\delta = %i^{+%i}_{-%i}$ ppm' % (depth*1e6,ddepth[1]*1e6,ddepth[0]*1e6)
        dur,ddur = self.trsig.durfit
        sigpars += '\n$T = %.2f^{+%.2f}_{-%.2f}$ h' % (dur*24.,ddur[1]*24,ddur[0]*24)
        slope,dslope = self.trsig.slopefit
        sigpars += '\n'+r'$T/\tau = %.1f^{+%.1f}_{-%.1f}$' % (slope,dslope[1],dslope[0])
        sigpars += '\n'+r'$(T/\tau)_{max} = %.1f$' % (self.trsig.maxslope)
        if siginfo:
            plt.annotate(sigpars,xy=(0.81,0.91),xycoords='figure fraction',va='top')
        
            #p.annotate('${}^a$Not used for FP population simulations',xy=(0.02,0.02),
            #           xycoords='figure fraction',fontsize=9)
        """

        constraints = 'Constraints:'
        for c in self.popset.constraints:
            try:
                constraints += '\n  %s' % self['heb'].constraints[c]
            except KeyError:
                constraints += '\n  %s' % self['beb'].constraints[c]
        if constraintinfo:
            plt.annotate(constraints,
                         xy=(0.03, 0.22),
                         xycoords='figure fraction',
                         va='top',
                         color='red')

        odds = 1. / self.FPP()
        if odds > 1e6:
            fppstr = 'FPP: < 1 in 1e6'
        else:
            fppstr = 'FPP: 1 in %i' % odds

        plt.annotate('$f_{pl,V} = %.3f$\n%s' % (self.fpV(), fppstr),
                     xy=(0.7, 0.02),
                     xycoords='figure fraction',
                     fontsize=16,
                     va='bottom')

        plt.suptitle(self.trsig.name, fontsize=22)

        #if not simple:
        #    plt.annotate('n = %.0e' % self.n,xy=(0.5,0.85),xycoords='figure fraction',
        #                 fontsize=14,ha='center')

        if saveplot:
            if tag is not None:
                plt.savefig('%s/FPPsummary_%s.%s' % (folder, tag, figformat))
            else:
                plt.savefig('%s/FPPsummary.%s' % (folder, figformat))
            plt.close()
Exemplo n.º 56
0
Y_lm = sph_harmonic(2, 0, theta, phi).real
Y_sup = np.sqrt(3./8)*sph_harmonic(2,2,theta,phi) + np.sqrt(3./8)*sph_harmonic(2,-2,theta,phi) -\
0.5*sph_harmonic(2,0,theta,phi)


##################################################################################################
r = np.abs(Y_sup)
# The Cartesian coordinates of the spheres
x = r * np.sin(theta) * np.cos(phi)
y = r * np.sin(theta) * np.sin(phi)
z = r * np.cos(theta)

N = r/r.max() # Color scaling for mapping on to the harmonics
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'), figsize=(12,10))
im = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.jet(N))
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(r)    # Assign the unnormalized data array to the mappable
                  #so that the scale corresponds to the values of R
fig.colorbar(m, shrink=0.8);
ax.set_axis_off()

plt.show()
##################################################################################################
# Calculate the spherical harmonic Y(l,m) and normalize to [0,1]
fcolors = Y_sup.real
fmax, fmin = fcolors.max(), fcolors.min()
fcolors = (fcolors - fmin)/(fmax - fmin)

x = np.sin(theta) * np.cos(phi)
y = np.sin(theta) * np.sin(phi)
Exemplo n.º 57
0
def stampa(img, soglia_sup, soglia_inf, sf, graph_results_date, x, y, vx, vy):
    soglia_sup = int(soglia_sup)
    soglia_inf = int(soglia_inf)

    H = img.RasterYSize
    W = img.RasterXSize

    #data=np.loadtxt(risultati,skiprows=1)

    modulo_spostamenti = np.sqrt((vx**2) * sf * sf + (vy**2) * sf * sf)

    #rimuovo dal grafico le deformazioni con modulo inferiore/superiore al valore della soglia
    limite_inf = np.where(modulo_spostamenti > soglia_sup)[0]
    modulo_spostamenti = np.delete(modulo_spostamenti, limite_inf, axis=0)

    limite_sup = np.where(modulo_spostamenti < soglia_inf)[0]
    modulo_spostamenti = np.delete(modulo_spostamenti, limite_sup, axis=0)

    x = np.delete(x, limite_inf, axis=0)
    x = np.delete(x, limite_sup, axis=0)

    y = np.delete(y, limite_inf, axis=0)
    y = np.delete(y, limite_sup, axis=0)

    vx = np.delete(vx, limite_inf, axis=0)
    vx = np.delete(vx, limite_sup, axis=0)

    vy = np.delete(vy, limite_inf, axis=0)
    vy = np.delete(vy, limite_sup, axis=0)

    nz = mcolors.Normalize(vmin=soglia_inf, vmax=soglia_sup)

    fig = plt.figure(str(data[0]) + " - " + str(data[1]))
    ax = fig.add_subplot(111)
    #ax.set_prop_cycle(['red', 'black', 'yellow'])
    #ax.set_color_cycle(['red', 'black', 'yellow'])

    plt.gca().set_aspect('equal', adjustable='box')
    plt.ylabel('Northing (m)')
    plt.xlabel('Easting (m)')

    img_band = img.GetRasterBand(1).ReadAsArray(0, 0, W, H)
    img_band_int = np.int_(img_band)

    gt = img.GetGeoTransform()

    extent = (gt[0], gt[0] + img.RasterXSize * gt[1],
              gt[3] + img.RasterYSize * gt[5], gt[3])
    plt.imshow(img_band_int, extent=extent, origin='upper', cmap=plt.cm.gray)

    #ipdb.set_trace()
    plt.quiver(x,
               y, (vx * sf) / modulo_spostamenti,
               (sf * vy) / modulo_spostamenti,
               angles='xy',
               scale=50,
               color=cm.jet(nz(modulo_spostamenti)))
    plt.ylim([4742500, 4760000])  #intervallo sulle y
    plt.xlim([613000, 637000])  #intervallo sulle x

    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    cb = mcolorbar.ColorbarBase(cax, cmap=cm.jet, norm=nz)
    cb.set_clim(soglia_inf, soglia_sup)  #
    cb.set_label('meters/month')

    plt.savefig(graph_results_date, dpi=250, bbox_inches='tight')

    fig.clear()
    plt.close()
Exemplo n.º 58
0
def convection_onset_statistics(precip_threshold, cwv_max, cwv_min, bin_width,
                                cwv, precip, test, output_path, sites,
                                sitename):

    # Create CWV bins
    #number_of_bins = 38 # default = 28
    #cwv_max = 85 # default = 70 (in mm)
    #cwv_min = 28 # default = 28 (in mm)
    #bin_width = 1.5 # default = 1.5
    number_of_bins = int(np.ceil((cwv_max - cwv_min) / bin_width))
    #print('cwv_max',cwv_max)
    #print(number_of_bins)
    bin_center = np.arange((cwv_min + (bin_width / 2)),
                           (cwv_max - (bin_width / 2)) + bin_width, bin_width)
    #print('bin_center',bin_center,'bin_width',bin_width)
    if len(bin_center) != number_of_bins:
        bin_center = np.arange((cwv_min + (bin_width / 2)),
                               (cwv_max - (bin_width / 2)), bin_width)

    # Define precip threshold
    #precip_threshold = 0.5 # default 0.5 (in mm/hr)
    avg_interval = 1  # averaged within 1 hr
    # Define variables for binning
    bin_index = np.zeros([number_of_bins, cwv.size])
    precip_binned = np.empty([number_of_bins, cwv.size]) * np.nan
    precip_counts = np.zeros([number_of_bins, cwv.size])

    np.warnings.filterwarnings('ignore')
    # Bin the data by CWV value as specified above
    for i in range(0, number_of_bins):
        tmp1 = np.where(cwv > cwv_min + (i * bin_width))
        bin_index[i, tmp1] = 1
        tmp2 = np.where(cwv > cwv_min + (i * bin_width) + bin_width)
        bin_index[i, tmp2] = 0

    for i in range(0, number_of_bins):
        tmp1 = np.where(bin_index[i, :] == 1)
        precip_binned[i, tmp1] = precip[tmp1]
        tmp2 = np.where(bin_index[i, :] != 1)
        precip_binned[i, tmp2] = np.nan

    for i in range(0, number_of_bins):
        tmp1 = np.where(precip_binned[i, :] >= precip_threshold)
        precip_counts[i, tmp1] = 1
        for j in range(0, cwv.size):
            #if cwv[j] > (cwv_min+(i*bin_width)):
            #    bin_index[i,j] = 1
            #if cwv[j] > (cwv_min+(i*bin_width)+bin_width):
            #    bin_index[i,j] = 0
            #if bin_index[i,j] == 1:
            #    precip_binned[i,j] = precip[j]
            #else:
            #    precip_binned[i,j] = np.nan
            #if precip_binned[i,j] >= precip_threshold:
            #    precip_counts[i,j] = 1
            if np.isnan(precip_binned[i, j]):
                precip_counts[i, j] = np.nan

    # Create binned arrays
    hist_cwv = np.empty([number_of_bins, 1]) * np.nan
    hist_precip_points = np.empty([number_of_bins, 1]) * np.nan
    pr_binned_mean = np.empty([number_of_bins, 1]) * np.nan
    pr_binned_var = np.empty([number_of_bins, 1]) * np.nan
    pr_binned_std = np.empty([number_of_bins, 1]) * np.nan
    pr_probability = np.empty([number_of_bins, 1]) * np.nan
    errorbar_precip_points = np.empty([number_of_bins, 1]) * np.nan
    errorbar_precip = np.empty([number_of_bins, 1]) * np.nan
    std_error_precip = np.empty([number_of_bins, 1]) * np.nan
    pdf_cwv = np.empty([number_of_bins, 1]) * np.nan
    pdf_precipitating_points = np.empty([number_of_bins, 1]) * np.nan

    ###
    errorbar_precip_binom = np.empty([number_of_bins, 2]) * np.nan

    # Fill binned arrays
    hist_cwv = bin_index.sum(axis=1)
    hist_cwv[hist_cwv <= 1] = 0
    hist_precip_points = np.nansum(precip_counts, axis=1)
    hist_precip_points[hist_precip_points <= 1] = 0
    pr_binned_mean = np.nanmean(precip_binned, axis=1)
    #print('pr_binned_mean',pr_binned_mean)
    pr_binned_var = np.nanvar(precip_binned, axis=1)
    pr_binned_std = np.nanstd(precip_binned, axis=1)
    r = np.empty([1, number_of_bins]) * np.nan
    r = np.sum(~np.isnan(precip_counts), axis=1)
    pr_probability = np.nansum(precip_counts, axis=1) / r
    freq_cwv = (hist_cwv / bin_width) / np.nansum(hist_cwv)
    pdf_cwv = (hist_cwv / bin_width) / np.nansum(hist_cwv / bin_width)
    freq_precipitating_points = hist_precip_points / bin_width / np.nansum(
        hist_cwv)
    pdf_precipitating_points = (hist_precip_points / bin_width) / np.nansum(
        hist_cwv / bin_width)

    for i in range(0, number_of_bins):
        errorbar_precip[i] = pr_binned_std[i] / math.sqrt(hist_cwv[i])
        errorbar_precip_points[i] = math.sqrt(
            hist_precip_points[i]) / np.nansum(
                hist_cwv / bin_width) / bin_width
        z = .675
        p = hist_precip_points[i] / hist_cwv[i]
        NT = hist_cwv[i]
        phat = hist_precip_points[i] / hist_cwv[i]
        errorbar_precip_binom[i, 0] = z * math.sqrt(phat *
                                                    (1 - phat) / hist_cwv[i])
        errorbar_precip_binom[i, 1] = z * math.sqrt(phat *
                                                    (1 - phat) / hist_cwv[i])

    scatter_colors = cm.jet(np.linspace(0, 1, number_of_bins, endpoint=True))
    axes_fontsize = 12  # size of font in all plots
    legend_fontsize = 9
    marker_size = 40  # size of markers in scatter plots
    xtick_pad = 10  # padding between x tick labels and actual plot
    bin_width = (np.max(bin_center) - np.min(bin_center)) / number_of_bins

    # create figure canvas
    fig = mp.figure(figsize=(8, 2.5))

    # create figure 1
    ax1 = fig.add_subplot(131)
    xulim = 5 * np.ceil(np.max(np.round(bin_center + bin_width / 2)) / 5)
    xllim = 5 * np.floor(np.min(np.round(bin_center - bin_width / 2)) / 5)
    ax1.set_xlim(xllim - 10, xulim + 15)
    ax1.set_ylim(0, 5)
    ax1.set_xticks(
        np.arange(
            np.ceil(xllim / 10) * 10 - 10,
            np.ceil(xulim / 10) * 10 + 15, 15))
    #print(np.arange(np.ceil(xllim/10)*10-10,np.ceil(xulim/10)*10+15,15))
    #print(np.ceil(xllim/10)*10)
    #print(np.ceil(xulim/10)*10)
    ulim = np.nanmax(pr_binned_mean)
    #print('max precip',ulim)
    #ax1.set_yticks(np.arange(0,np.ceil(ulim)))
    ax1.set_yticks(np.arange(0, 5))
    #ax1.set_xlim(25,72)
    #ax1.set_ylim(0,6)
    #ax1.set_xticks([30,40,50,60,70])
    #ax1.set_yticks([0,1,2,3,4,5,6])
    ax1.tick_params(labelsize=axes_fontsize)
    ax1.tick_params(axis='x', pad=10)
    error = [errorbar_precip, errorbar_precip]
    #print(bin_center.shape)
    #print(pr_binned_mean.shape)
    #ax1.errorbar(bin_center, pr_binned_mean, xerr=None, yerr=np.asarray(error), ls='none', color='black')
    ax1.errorbar(bin_center,
                 pr_binned_mean,
                 xerr=None,
                 yerr=errorbar_precip.squeeze(),
                 ls='none',
                 color='black')
    ax1.scatter(bin_center,
                pr_binned_mean,
                edgecolor='none',
                facecolor=scatter_colors,
                s=marker_size,
                clip_on=False,
                zorder=3)
    ax1.set_ylabel('Precip (mm/hr)', fontsize=axes_fontsize)
    ax1.set_xlabel('CWV (mm)', fontsize=axes_fontsize)
    #ax1.text(0.05, 0.95, sites[0], transform=ax1.transAxes, fontsize=12, verticalalignment='top')
    #ax1.text(0.05, 0.85, test, transform=ax1.transAxes, fontsize=12, verticalalignment='top')
    #ax1.grid()
    ax1.set_axisbelow(True)

    # create figure 2 (probability pickup)
    ax2 = fig.add_subplot(132)
    xulim = 5 * np.ceil(np.max(np.round(bin_center + bin_width / 2)) / 5)
    xllim = 5 * np.floor(np.min(np.round(bin_center - bin_width / 2)) / 5)
    ax2.set_xlim(xllim - 10, xulim + 15)
    ax2.set_xticks(
        np.arange(
            np.ceil(xllim / 10) * 10 - 10,
            np.ceil(xulim / 10) * 10 + 15, 15))
    #ax2.set_xlim(25,72)
    ax2.set_ylim(0, 1)
    #ax2.set_xticks([30,40,50,60,70])
    ax2.set_yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
    ax2.tick_params(labelsize=axes_fontsize)
    ax2.errorbar(bin_center,
                 pr_probability,
                 xerr=None,
                 yerr=errorbar_precip_binom.T,
                 fmt="none",
                 color='black')
    ax2.tick_params(axis='x', pad=xtick_pad)
    ax2.scatter(bin_center,
                pr_probability,
                marker='d',
                s=marker_size,
                edgecolor='none',
                facecolor='steelblue',
                clip_on=False,
                zorder=3)
    ax2.set_ylabel('Probability of Precip.', fontsize=axes_fontsize)
    ax2.set_xlabel('CWV (mm)', fontsize=axes_fontsize)
    #ax2.grid()
    ax2.set_axisbelow(True)

    # create figure 3 (non-normalized PDF)
    ax3 = fig.add_subplot(133)
    ax3.set_yscale('log')

    xulim = 5 * np.ceil(np.max(np.round(bin_center + bin_width / 2)) / 5)
    xllim = 5 * np.floor(np.min(np.round(bin_center - bin_width / 2)) / 5)
    ax3.set_xlim(xllim - 10, xulim + 15)
    ax3.set_xticks(
        np.arange(
            np.ceil(xllim / 10) * 10 - 10,
            np.ceil(xulim / 10) * 10 + 15, 15))

    #low_lim = np.floor(np.log10(np.min(freq_precipitating_points[freq_precipitating_points>0])))
    low_lim = -6.0
    up_lim = np.ceil(np.log10(np.max(freq_cwv)))
    ax3.set_ylim(10**low_lim, 100)
    #print('low_lim',low_lim)
    #print('y_min',10**low_lim)
    #ax3.set_ylim(5e-1, 5e5)
    #ax3.set_xlim(25,72)
    #ax3.set_xticks([30,40,50,60,70])
    ax3.set_yticks(10**np.arange(low_lim, 2, dtype='float64'))
    #ax3.set_yticks(10**np.array((0,1,2,3,4,5)))
    ax3.tick_params(labelsize=axes_fontsize)
    ax3.tick_params(axis='x', pad=xtick_pad)
    # yscale is log scale, so throw out any zero values
    freq_precipitating_points[freq_precipitating_points == 0] = np.nan
    freq_cwv[freq_cwv == 0] = np.nan

    #pdf_precipitating_points[pdf_precipitating_points==0] = np.nan
    error = [errorbar_precip_points, errorbar_precip_points]
    ax3.errorbar(bin_center,
                 freq_precipitating_points,
                 xerr=None,
                 yerr=errorbar_precip_points.squeeze(),
                 ls='none',
                 color='black')
    ax3.scatter(bin_center, freq_cwv, color='b', label='all')
    ax3.scatter(bin_center,
                freq_precipitating_points,
                edgecolor='none',
                facecolor='steelblue',
                s=marker_size,
                clip_on=False,
                zorder=3,
                label='precip $>$ 0.5 mm/hr ')
    #ax3.scatter(bin_center, pdf_cwv, marker='x', color='0', label='all')

    ax3.set_ylabel('PDF', fontsize=axes_fontsize)
    ax3.set_xlabel('CWV (mm)', fontsize=axes_fontsize)
    #ax3.grid()
    ax3.set_axisbelow(True)

    # create legend
    legend_handles, legend_labels = ax3.get_legend_handles_labels()
    ax3.legend(legend_handles,
               legend_labels,
               loc='upper left',
               bbox_to_anchor=(0.1, 0.95),
               fontsize=legend_fontsize,
               scatterpoints=1,
               handlelength=0,
               labelspacing=0,
               borderpad=0,
               borderaxespad=0,
               frameon=False)

    # set layout to tight (so that space between figures is minimized)
    mp.tight_layout()
    mp.suptitle(test + ' at ' + sitename + ' Averaged over ' +
                str(avg_interval) + ' hrs',
                y=1.08,
                fontweight='bold')

    # save figure
    #mp.savefig('conv_diagnostics_example_kas_new.pdf', transparent=True, bbox_inches='tight')
    mp.savefig(output_path + '/conv_diagnostics_' + test + '_' + sites[0] +
               '.png',
               transparent=False,
               bbox_inches='tight')
Exemplo n.º 59
0
            axis.set_title('Relative difference in linear power of cases 4-7 to case 0',size=18)
            tight_layout()
            savefig('D:\dropbox\Dropbox\plots\case4\linpowrel4.png')  
            show()           


    if 2 in subcase:
        print shape(tp)
        figure('powercycle5')
        plt.clf()
        axis=plt.subplot(111)
        
        ph=[0]*29
        ii=[0,6,8,11,14,17,20,23,26,29]
        for i in range(len(ii)):
            c = cm.jet((i)/len(ii),1)
            ph[i],=step(z_mesh,Stepper(LinPow(tp,5,ii[i]),0)/1000,color=c,where='post',label='{} EFPD'.format(ts[ii[i]]))
        axis.yaxis.grid(True,'major',linewidth=1,color=[0.7,0.7,0.7])
        axis.xaxis.grid(True,'major',linewidth=1,color=[0.7,0.7,0.7])
        axis.set_ylabel('linear Power, [kW/m]' ,size=14)
        axis.set_xlabel('axial position, [m]',size=14) 
          
        l=legend(loc=8,ncol=2)
        axis.set_xlim([0,3.6576])
        tight_layout()
        #savefig('V:\\master report\\figures\\powercycle{0}.pdf'.format(C))
        #axis.set_title('Linear Power development with burnup \n case {0}'.format(C),size=18)   
        tight_layout()    
        savefig('D:/dropbox/Dropbox/plots/spring2013/powercycle_case5.pdf'.format(C))
        #axis.set_ylim([500,1200])    
        show()     
Exemplo n.º 60
0
# 查看好友所在地区分布情况
cityStat = memberList.groupby(by=['City'])['City'].agg({
    '计数': len
}).reset_index().sort_values(by='计数', ascending=False)
cityStat = cityStat[:20]  # 取top-20
# 查看好友所在身份分布情况
provinceStat = memberList.groupby(by=['Province'])['Province'].agg({
    '计数': len
}).reset_index().sort_values(by='计数', ascending=False)
provinceStat = provinceStat[:20]  # 取top-20
# 绘图(好友省份分布)
fig = plt.figure(2)
plt.subplot(2, 1, 1)
plt.title('Top20-好友省份分布')
axis1 = numpy.arange(len(provinceStat))
color = cm.jet(axis1 / max(axis1))
plt.bar(axis1, provinceStat['计数'], color=color)
province = []
for p in provinceStat['Province']:
    if len(p) == 0:
        p = '未知'
    province.append(p)
plt.grid(color='#95a5a6', axis='y', linestyle='--', linewidth=1, alpha=0.4)
plt.ylabel('好友数量')
plt.xticks(axis1, province)
# 绘图(好友城市分布)
plt.subplot(2, 1, 2)
axis2 = numpy.arange(len(cityStat))
plt.rc('font', family='simhei', size=9)
plt.title('Top20-好友城市分布')
cityStat = cityStat.sort_values('计数')