Example #1
1
def plot_tracks(src, fakewcs, spa=None, **kwargs):
    # NOTE -- MAGIC 61 = monthly; this is ASSUMEd below.
    tt = np.linspace(2010., 2015., 61)
    t0 = TAITime(None, mjd=TAITime.mjd2k + 365.25*10)
    #rd0 = src.getPositionAtTime(t0)
    #print 'rd0:', rd0
    xx,yy = [],[]
    rr,dd = [],[]
    for t in tt:
        #print 'Time', t
        rd = src.getPositionAtTime(t0 + (t - 2010.)*365.25*24.*3600.)
        ra,dec = rd.ra, rd.dec
        rr.append(ra)
        dd.append(dec)
        ok,x,y = fakewcs.radec2pixelxy(ra,dec)
        xx.append(x - 1.)
        yy.append(y - 1.)

    if spa is None:
        spa = [None,None,None]
    for rows,cols,sub in spa:
        if sub is not None:
            plt.subplot(rows,cols,sub)
        ax = plt.axis()
        plt.plot(xx, yy, 'k-', **kwargs)
        plt.axis(ax)

    return rr,dd,tt
Example #2
0
File: MMOG.py Project: Sura82/colvb
    def plot(self):
        rows = np.sqrt(self.M).round()
        cols = np.ceil(self.M/rows)

        if self.D==1:
            xmin = self.X.min()
            xmax = self.X.max()
            xmin,xmax = xmin-0.1*(xmax-xmin), xmax+0.1*(xmax-xmin)
            Xgrid = np.linspace(xmin,xmax,100)[:,None]
            zz = self.predict(Xgrid)

        if self.D==2:
            xmin,ymin = np.vstack(self.X).min(0)
            xmax,ymax = np.vstack(self.X).max(0)
            xmin,xmax = xmin-0.1*(xmax-xmin), xmax+0.1*(xmax-xmin)
            ymin,ymax = ymin-0.1*(ymax-ymin), ymax+0.1*(ymax-ymin)
            xx,yy = np.mgrid[xmin:xmax:100j,ymin:ymax:100j]
            Xgrid = np.vstack((xx.flatten(),yy.flatten())).T
            zz = self.predict(Xgrid).reshape(100,100,self.M)

        for m in range(self.M):
            pb.subplot(rows,cols,m+1)
            if self.D==1:
                pb.hist(self.X[m,:,0],self.N/10.,normed=True)
                pb.plot(Xgrid,zz[:,m],'r',linewidth=2)
            elif self.D==2:
                pb.plot(self.X[m,:,0],self.X[m,:,1],'rx',mew=2)
                zz_data = self.predict(self.X[m])[:,m]
                pb.contour(xx,yy,zz[:,:,m],[stats.scoreatpercentile(zz_data,5)],colors='r',linewidths=1.5)
                pb.imshow(zz[:,:,m].T,extent=[xmin,xmax,ymin,ymax],origin='lower',cmap=pb.cm.binary,vmin=0.,vmax=zz_data.max())
Example #3
0
    def Xtest3(self):
        """
        Test from Kate Marvel
        As the following code snippet demonstrates, regridding a
        cdms2.tvariable.TransientVariable instance using regridTool='regrid2' 
        results in a new array that is masked everywhere.  regridTool='esmf' 
        and regridTool='libcf' both work as expected.

        This is similar to the original test but we construct our own 
        uniform grid. This should passes.
        """
        import cdms2 as cdms
        import numpy as np

        filename = cdat_info.get_sampledata_path() + '/clt.nc'
        a=cdms.open(filename)
        data=a('clt')[0,...]

        print data.mask #verify this data is not masked

        GRID = cdms.grid.createUniformGrid(-90.0, 23, 8.0, -180.0, 36, 10.0, order="yx", mask=None)

        test_data=data.regrid(GRID,regridTool='regrid2')

        # check that the mask does not extend everywhere...
        self.assertNotEqual(test_data.mask.sum(), test_data.size)
        
        if PLOT:
            pylab.subplot(2, 1, 1)
            pylab.pcolor(data[...])
            pylab.title('data')
            pylab.subplot(2, 1, 2)
            pylab.pcolor(test_data[...])
            pylab.title('test_data (interpolated data)')
            pylab.show()
Example #4
0
def plot_data(kx,omega,F,F_R,F_L,K,O):
    #plt.figure(4)
    #plt.imshow(K,extent=[omega[0],omega[-1],kx[0],kx[-1]],\
    #        interpolation = "nearest", aspect = "auto")
    #plt.xlabel('KX')
    #plt.colorbar()
    
    #plt.figure(5)
    #plt.imshow(O,extent =[omega[0],omega[-1],kx[0],kx[-1]],interpolation="nearest", aspect="auto")
    #plt.xlabel('omega')
    #plt.colorbar()
    
    plt.figure(6)
    pylab.subplot(1,2,1)
    plt.imshow(abs(F_R), extent= [omega[0],omega[-1],kx[0],kx[-1]], interpolation= "nearest", aspect = "auto")
    plt.xlabel('abs FFT_R')
    plt.colorbar()
    plt.subplot(1,2,2)
    plt.imshow(abs(F_L), extent= [omega[0],omega[-1],kx[0],kx[-1]], interpolation= "nearest", aspect = "auto")
    plt.xlabel('abs FFT_L')
    plt.colorbar()
    
    
    plt.figure(7)
    plt.subplot(2,1,1)
    plt.imshow(abs(F_L+F_R),extent=[omega[0],omega[-1],kx[0],kx[-1]],interpolation= "nearest", aspect = "auto")
    plt.xlabel('abs(F_L+F_R)  reconstructed')
    plt.colorbar()
    pylab.subplot(2,1,2)
    plt.imshow(abs(F),extent=[omega[0],omega[-1],kx[0],kx[-1]],interpolation ="nearest",aspect = "auto")
    plt.xlabel('FFT of the original data')
    plt.colorbar()

    #plt.show()
    return
Example #5
0
 def plot_vm(self):
     """Plot Vm for presynaptic compartment and soma - along with
     the same in NEURON simulation if possible."""
     pylab.subplot(211)
     pylab.title('Soma Vm')
     pylab.plot(self.tseries*1e3, self.somaVmTab.vec * 1e3,
                label='Vm (mV) - moose')
     pylab.plot(self.tseries*1e3, self.injectionTab.vec * 1e9,
                label='Stimulus (nA)')
     try:
         nrn_data = np.loadtxt('../nrn/data/%s_soma_Vm.dat' % \
                                   (self.celltype))
         nrn_indices = np.nonzero(nrn_data[:, 0] <= self.tseries[-1]*1e3)[0]                        
         pylab.plot(nrn_data[nrn_indices,0], nrn_data[nrn_indices,1], 
                    label='Vm (mV) - neuron')
     except IOError:
         print 'No neuron data found.'
     pylab.legend()
     pylab.subplot(212)
     pylab.title('Presynaptic Vm')
     pylab.plot(self.tseries*1e3, self.presynVmTab.vec * 1e3,
                label='Vm (mV) - moose')
     pylab.plot(self.tseries*1e3, self.injectionTab.vec * 1e9,
                label='Stimulus (nA)')
     try:
         nrn_data = np.loadtxt('../nrn/data/%s_presynaptic_Vm.dat' % \
                                   (self.celltype))
         nrn_indices = np.nonzero(nrn_data[:, 0] <= self.tseries[-1]*1e3)[0]
         pylab.plot(nrn_data[nrn_indices,0], nrn_data[nrn_indices,1], 
                    label='Vm (mV) - neuron')
     except IOError:
         print 'No neuron data found.'
     pylab.legend()
     pylab.show()
Example #6
0
    def Xtest2(self):
        """
        Test from Kate Marvel
        As the following code snippet demonstrates, regridding a
        cdms2.tvariable.TransientVariable instance using regridTool='regrid2' 
        results in a new array that is masked everywhere.  regridTool='esmf' 
        and regridTool='libcf' both work as expected.

        This passes.
        """
        import cdms2 as cdms
        import numpy as np

        filename = cdat_info.get_sampledata_path() + '/clt.nc'
        a=cdms.open(filename)
        data=a('clt')[0,...]

        print data.mask #verify this data is not masked

        GRID= data.getGrid() # input = output grid, passes

        test_data=data.regrid(GRID,regridTool='regrid2')

        # check that the mask does not extend everywhere...
        self.assertNotEqual(test_data.mask.sum(), test_data.size)
        
        if PLOT:
            pylab.subplot(2, 1, 1)
            pylab.pcolor(data[...])
            pylab.title('data')
            pylab.subplot(2, 1, 2)
            pylab.pcolor(test_data[...])
            pylab.title('test_data (interpolated data)')
            pylab.show()
Example #7
0
    def display(self, xaxis, alpha, new=True):
        """
        E.display(xaxis, alpha = .8)

        :Arguments: xaxis, alpha

        Plots the CI region on the current figure, with respect to
        xaxis, at opacity alpha.

        :Note: The fill color of the envelope will be self.mass
            on the grayscale.
        """
        if new:
            figure()
        if self.ndim == 1:
            if self.mass>0.:
                x = concatenate((xaxis,xaxis[::-1]))
                y = concatenate((self.lo, self.hi[::-1]))
                fill(x,y,facecolor='%f' % self.mass,alpha=alpha, label = ('centered CI ' + str(self.mass)))
            else:
                pyplot(xaxis,self.value,'k-',alpha=alpha, label = ('median'))
        else:
            if self.mass>0.:
                subplot(1,2,1)
                contourf(xaxis[0],xaxis[1],self.lo,cmap=cm.bone)
                colorbar()
                subplot(1,2,2)
                contourf(xaxis[0],xaxis[1],self.hi,cmap=cm.bone)
                colorbar()
            else:
                contourf(xaxis[0],xaxis[1],self.value,cmap=cm.bone)
                colorbar()
Example #8
0
def trace(data, name, format='png', datarange=(None, None), suffix='', path='./', rows=1, columns=1, 
    num=1, last=True, fontmap = None, verbose=1):
    """
    Generates trace plot from an array of data.

    :Arguments:
        data: array or list
            Usually a trace from an MCMC sample.

        name: string
            The name of the trace.
            
        datarange: tuple or list
            Preferred y-range of trace (defaults to (None,None)).

        format (optional): string
            Graphic output format (defaults to png).

        suffix (optional): string
            Filename suffix.

        path (optional): string
            Specifies location for saving plots (defaults to local directory).
            
        fontmap (optional): dict
            Font map for plot.

    """

    if fontmap is None: fontmap = {1:10, 2:8, 3:6, 4:5, 5:4}

    # Stand-alone plot or subplot?
    standalone = rows==1 and columns==1 and num==1

    if standalone:
        if verbose>0:
            print_('Plotting', name)
        figure()

    subplot(rows, columns, num)
    pyplot(data.tolist())
    ylim(datarange)

    # Plot options
    title('\n\n   %s trace'%name, x=0., y=1., ha='left', va='top', fontsize='small')

    # Smaller tick labels
    tlabels = gca().get_xticklabels()
    setp(tlabels, 'fontsize', fontmap[rows/2])

    tlabels = gca().get_yticklabels()
    setp(tlabels, 'fontsize', fontmap[rows/2])

    if standalone:
        if not os.path.exists(path):
            os.mkdir(path)
        if not path.endswith('/'):
            path += '/'
        # Save to file
        savefig("%s%s%s.%s" % (path, name, suffix, format))
def bilinear_interpolation(dataset_name, feat, unfeat, repo, nbt=3):

    tlist=np.linspace(0,1,nbt)
    _, _, test=get_data(dataset_name, repo)
    xtest1, _, _ = test
    n = xtest1.shape[-1]
    N = len(xtest1)
    
    tuple_index=np.random.permutation(N)[:4]
    embeddings = feat.predict(xtest1[tuple_index])
    
    interp_array = np.zeros((nbt, nbt, n, n))
    
    for i in range(nbt):
        for j in range(nbt):
            x = (tlist[i]*embeddings[0] + (1-tlist[0])*embeddings[1])
            y = (tlist[i]*embeddings[2] + (1-tlist[0])*embeddings[3])
            x_interp = unfeat.predict(((tlist[j]*x + (1-tlist[j])*y))[None])
            interp_array[i,j]=x_interp[0,0]
            
    pl.figure(1)
    for i in range(nbt):
        for j in range(nbt):
            nb=i*nbt +j+1
            pl.subplot(nbt*100+(nbt)*10 +nb)
            pl.imshow(interp_array[i,j], cmap='Blues',interpolation='nearest')
def plot_signal(x,y,title,labelx,labley,position):
    pylab.subplot (9, 1, position)
    pylab.plot(x,y)
    pylab.title(title)
    pylab.xlabel(labelx)
    pylab.ylabel(labley)
    pylab.grid(True)
Example #11
0
def plot_samples(samples, no_of_rows, no_of_cols, pno=1, n_samples=100, plot_every=1, img_shp=(28,
    28), axes=None):
    if pno == 0:
        axes = pylab.subplot(no_of_rows, no_of_cols, pno + 1, aspect='equal')
        colored_axis(axes, "red")
        pylab.imshow(samples[pno].reshape(img_shp))
        plot_samples(samples,
                no_of_rows,
                no_of_cols,
                pno=pno + plot_every,
                n_samples=n_samples,
                plot_every=plot_every,
                img_shp=img_shp,
                axes=axes)

    if pno >= n_samples:
       colored_axis(axes, "black")
       return 0
    else:
        plot_no = pno / plot_every
        axes = pylab.subplot(no_of_rows, no_of_cols, plot_no + 1, aspect='equal')
        colored_axis(axes, "black")
        pylab.imshow(samples[pno].reshape(img_shp))
        plot_samples(samples,
                no_of_rows,
                no_of_cols,
                pno=pno + plot_every,
                n_samples=n_samples,
                plot_every=plot_every,
                img_shp=img_shp,
                axes=axes)
Example #12
0
def T2_cpmg_process(folder_to_process,plot='y'):
    """Given a folder of images will process cpmg data and return
    fitted T2 values and associated uncertainties"""
    data=img_roi_signal([folder_to_process],['EchoTime'])
    rois=data[0][0]
    TEs=data[2][0]
    mean_signal_mat=data[3]
    serr_signal_mat=data[4]
        
    if plot=='y':
        plt.figure()
    spin_echo_fits=[]
    for jj in np.arange(len(rois)-2):
        mean_sig=mean_signal_mat[0,jj,:]
        #serr_sig=serr_signal_mat[0,jj,:]
        mean_noise=np.mean(mean_signal_mat[0,-2,:])
        try:
            spin_echo_fit = SE_fit_new( np.array(TEs[0:]), mean_sig[0:], mean_noise, 'n' )
            if plot=='y':
                TE_full=np.arange(0,400,1)
                plt.subplot(4,4,jj+1)
                plt.plot(np.array(TEs[0:]), mean_sig[0:],'o')
                plt.plot(TE_full,spin_echo_fit(TE_full))
            
            spin_echo_fits.append(spin_echo_fit)
        except RuntimeError: 
            print 'RuntimeError'
            spin_echo=fitting.model('M0*exp(-x/T2)+a',{'M0':0,'T2':0,'a':0}) 
            spin_echo_fits.append(spin_echo)
    return spin_echo_fits   
def display_head(set_x, set_y, n = 5):
    '''
    show some figures based on gray image matrixs
    
    @type set_x: TensorSharedVariable, 
    @param set_x: gray level value matrix of the
    
    @type set_y: TensorVariable, 
    @param set_y: label of the figures    
    
    @type n: int, 
    @param n: numbers of figure to be display, less than 10, default 5
    '''
    import pylab
    
    if n > 10: n = 10
    img_x = set_x.get_value()[0:n].reshape(n, 28, 28)
    img_y = set_y.eval()[0:n]
    
    for i in range(n): 
        pylab.subplot(1, n, i+1); 
        pylab.axis('off'); 
        pylab.title(' %d' % img_y[i])
        pylab.gray()
        pylab.imshow(img_x[i])
Example #14
0
            def graphError(predictedPercentChanges, actualPercentChanges, title):
                # considering error and only considering it as error when the signs are different

                def computeSignedError(pred, actual):

                    if (pred > 0 and actual > 0) or (pred < 0 and actual < 0):
                        return 0

                    else:
                        error = abs(pred - actual)
                        # print 'pred: {0}, actual: {1}, error: {2}'.format(pred, actual, error)
                        return error

                signedError = map(
                    lambda pred, actual: computeSignedError(pred, actual), predictedPercentChanges, actualPercentChanges
                )
                pl.figure(2)
                pl.title(title + " Error")
                pl.subplot(211)
                pl.plot(signedError)
                pl.xlabel("Time step")
                pl.ylabel("Error (0 if signs are same and normal error if signs are different)")

                pl.figure(3)
                pl.title(title + " Actual vs Predictions")
                pl.subplot(211)
                pl.plot(
                    range(len(predictedPercentChanges)),
                    predictedPercentChanges,
                    "ro",
                    range(len(actualPercentChanges)),
                    actualPercentChanges,
                    "bs",
                )
Example #15
0
def main():
    amps = [0.167e-9,
            0.25e-9,
            0.333e-9]
    model_dict = setup_model()
    for ii, a in enumerate(amps):
        do_sim(model_dict['stimulus'], a)   
        config.logger.info('##### %d' % (model_dict['tab_vm'].size))
        vm = model_dict['tab_vm'].vector
        inject = model_dict['tab_stim'].vector.copy()
        t = np.linspace(0, simtime, len(vm))
        fname = 'data_fig_a3_%s.txt' % (chr(ord('A')+ii))
        np.savetxt(fname,
                   np.vstack((t, inject, vm)).transpose())
        msg = 'Saved data for %g A current pulse in %s' % (a, fname)
        config.logger.info(msg)
        print(msg)
        pylab.subplot(3,1,ii+1)
        pylab.title('%g nA' % (a*1e9))
        pylab.plot(t, vm, label='soma-Vm (mV)')
        stim_boundary = np.flatnonzero(np.diff(inject))
        pylab.plot((t[stim_boundary[0]]), (vm.min()), 'r^', label='stimulus start')                           
        pylab.plot((t[stim_boundary[-1]]), (vm.min()), 'gv', label='stimulus end')        
        pylab.legend()    
    pylab.savefig('fig_a3.png')
    pylab.show()
Example #16
0
def display_orbit(input, amplitude=0.000001):
    import matplotlib.pyplot
    import pylab

    # Compare the power spectral density functions of the system with and
    #  without the input sequence.
    x = [0.4, 0.6]
    X = []
    for i in range(warmups):
        x = network(x)
        x[0] = x[0] + ( amplitude * input[0][i % len(input[0])] )
        x[1] = x[1] + ( amplitude * input[1][i % len(input[1])] )
    for i in range(measure):
        X.append(x[0])
        x = network(x)
        x[0] = x[0] + ( amplitude * input[0][i % len(input[0])] )
        x[1] = x[1] + ( amplitude * input[1][i % len(input[1])] )
    pylab.subplot(2,1,1)
    matplotlib.pyplot.psd(X,1024,32)

    x = [0.4, 0.6]
    X = []
    for i in range(warmups):
        x = network(x)
        # x[0] = x[0] + ( amplitude * input[i % len(input)] )
    for i in range(measure):
        X.append(x[0])
        x = network(x)
        # x[0] = x[0] + ( amplitude * input[i % len(input)] )
    pylab.subplot(2,1,2)
    matplotlib.pyplot.psd(X,1024,32)

    pylab.show()
Example #17
0
File: pp.py Project: remosu/jobjob
def plot_pp(var):
    jobs_orig.sort(key=lambda job: getattr(job, var))
    pylab.subplot(211)
    for ch in (10, 50, 200):
        xs = [getattr(job, var) for job in jobs_orig if job.ch_objects == ch]
        if xs:
            pylab.plot(xs, 
                       [(job.ef11[-1] + job.ef10[-1]) / job.ef11[0] 
                        for job in jobs_orig if job.ch_objects==ch], 
                       'o-', label=r'$ch=%.0f$'%ch)
            pylab.xscale('log')
    pylab.xlabel(r'$%s$'%(var,))
    pylab.ylabel(r'$(ef_{11}^n+ef_{10}^n)/ef_{11}^0$')
    pylab.legend(loc='best')
    
    pylab.subplot(212)
    for ch in (10, 50, 200):
        xs = [getattr(job, var) for job in jobs_orig if job.ch_objects == ch]
        if xs:
            pylab.plot(xs, 
                       [job.hf_eq11 / job.ef11[-1] for job in jobs_orig if job.ch_objects==ch], 
                       'o-', label=r'$ch=%.0f$'%ch)
            pylab.xscale('log')
    pylab.xlabel(r'$%s$'%(var,))
    pylab.ylabel(r'$hf{11}^n(Q=0) / ef_{11}^n$')
    pylab.legend(loc='best')
    pylab.savefig(var+'.png')
def show_image(*imgs): 
    for idx, img in enumerate(imgs):
        subplot = 101 + len(imgs)*10 +idx
        pl.subplot(subplot)
        pl.imshow(img, cmap=pl.cm.gray)   
        pl.gca().set_axis_off()    
    pl.subplots_adjust(0.02, 0, 0.98, 1, 0.02, 0)        
Example #19
0
    def draw(self):

        print self.edgeno

        pos = 0
        dy = 8
        edgeno = self.edgeno
        edge = self.edges[edgeno]
        edgeprev = self.edges[edgeno-1]
        p = np.round(edge["top"](1024))
        top = min(p+2*dy, 2048)
        bot = min(p-2*dy, 2048)
        self.cutout = self.flat[1][bot:top,:].copy()

        pl.figure(1)
        pl.clf()
        start = 0
        dy = 512
        for i in xrange(2048/dy):
            pl.subplot(2048/dy,1,i+1)
            pl.xlim(start, start+dy)

            if i == 0: pl.title("edge %i] %s|%s" % (edgeno,
                edgeprev["Target_Name"], edge["Target_Name"]))


            pl.subplots_adjust(left=.07,right=.99,bottom=.05,top=.95)

            pl.imshow(self.flat[1][bot:top,start:start+dy], extent=(start,
                start+dy, bot, top), cmap='Greys', vmin=2000, vmax=6000)

            pix = np.arange(start, start+dy)
            pl.plot(pix, edge["top"](pix), 'r', linewidth=1)
            pl.plot(pix, edgeprev["bottom"](pix), 'r', linewidth=1)
            pl.plot(edge["xposs_top"], edge["yposs_top"], 'o')
            pl.plot(edgeprev["xposs_bot"], edgeprev["yposs_bot"], 'o')


            hpp = edge["hpps"]
            pl.axvline(hpp[0],ymax=.5, color='blue', linewidth=5)
            pl.axvline(hpp[1],ymax=.5, color='red', linewidth=5)

            hpp = edgeprev["hpps"]
            pl.axvline(hpp[0],ymin=.5,color='blue', linewidth=5)
            pl.axvline(hpp[1],ymin=.5,color='red', linewidth=5)


            if False:
                L = top-bot
                Lx = len(edge["xposs"])
                for i in xrange(Lx):
                    xp = edge["xposs"][i]
                    frac1 = (edge["top"](xp)-bot-1)/L
                    pl.axvline(xp,ymin=frac1)

                for xp in edgeprev["xposs"]: 
                    frac2 = (edgeprev["bottom"](xp)-bot)/L
                    pl.axvline(xp,ymax=frac2)

            start += dy
def ShowDynamicalResults(indexmap,output):
    import pylab
    pylab.figure()
    for i in range(4):
        pylab.subplot('22'+str(i+1))
        pylab.imshow(indexmap[i],interpolation='nearest')
    pylab.savefig(output+'.png')
Example #21
0
def lookatresults(data, modes, theta=None, vert=False, labels=None):


    P = data[-1][0]
    n = P.shape[0]

    if labels == None:
        labels = [""] * n
    else:
        pass 

    if vert == True:
        subplots = range(n*100+11,n*100+n+11,1)
        figsize = (6, 3*n)
    elif vert == 'four':
        subplots = [221, 222, 223, 224]
        figsize = (10, 10)
    else:
        subplots = range(100+n*10+1,100+n*10+1+n,1)
        figsize = (5*n, 3)

    f = stats.gaussian_kde(data[-1][0])
    int_guess = np.mean(data[-1][0], axis=1)
    modes = minimize(neg, int_guess, args=(f)).x

    thetas = []
    P = data[-1][0]
    labelpad = 20

    for i in xrange(n):
        x = P[i]
        t = r'$\theta_{3:}$ {1:.2f} +{2:.2f}/-{0:.2f}'.format(
            modes[i]-stats.scoreatpercentile(x, 16),
            modes[i],
            stats.scoreatpercentile(x, 84)-modes[i], i+1)

        thetas.append(t)

    if P.shape[1] > 10:
        bins = np.sqrt(P.shape[1])
    else:
        bins=10
    fig = plt.figure(figsize=figsize)
    
    for i in xrange(n):
        print subplots[i]
        plt.subplot(int(subplots[i]))
        #plt.title(thetas[0])
        ker = stats.gaussian_kde(P[i])
        h = plt.hist(P[i], bins=bins, normed=True, alpha=1)
        x = np.linspace(h[1][0],h[1][-1],1000)
        plt.plot(x,ker(x))
        plt.xlabel(labels[i], labelpad=labelpad, fontsize=24)
        if theta != None:
            plt.axvline(theta[0])

    for t in thetas: 
        print t

    return fig
Example #22
0
def main( runTime ):
    try:
        moose.delete('/acc92')
        print("Deleted old model")
    except Exception as  e:
        print("Could not clean. model not loaded yet")

    moose.loadModel('acc92_caBuff.g',loadpath,'gsl')  
    ca = moose.element(loadpath+'/kinetics/Ca')
    pr = moose.element(loadpath+'/kinetics/protein')
    clockdt = moose.Clock('/clock').dts 
    moose.setClock(8, 0.1)#simdt
    moose.setClock(18, 0.1)#plotdt
    print clockdt
    print " \t \t simdt ", moose.Clock('/clock').dts[8],"plotdt ",moose.Clock('/clock').dts[18]
    ori =  ca.concInit
    tablepath = loadpath+'/kinetics/Ca'
    tableele = moose.element(tablepath)
    table = moose.Table2(tablepath+'.con')
    x = moose.connect(table, 'requestOut', tablepath, 'getConc')
    tablepath1 = loadpath+'/kinetics/protein'
    tableele1 = moose.element(tablepath1)
    table1 = moose.Table2(tablepath1+'.con')
    x1 = moose.connect(table1, 'requestOut', tablepath1, 'getConc')

    ca.concInit = ori
    print("[INFO] Running for 4000 with Ca.conc %s " % ca.conc)
    moose.start(4000)

    ca.concInit = 5e-03
    print("[INFO] Running for 20 with Ca.conc %s " % ca.conc)
    moose.start(20)

    ca.concInit = ori
    moose.start( runTime ) #here give the interval time 

    ca.concInit = 5e-03
    print("[INFO] Running for 20 with Ca.conc %s " % ca.conc)
    moose.start(20)

    ca.concInit = ori
    print("[INFO] Running for 2000 with Ca.conc %s " % ca.conc)
    moose.start(2000)

    pylab.figure()
    pylab.subplot(2, 1, 1)
    t = numpy.linspace(0.0, moose.element("/clock").runTime, len(table.vector)) # sec
    pylab.plot( t, table.vector, label="Ca Conc (interval- 8000s)" )
    pylab.legend()
    pylab.subplot(2, 1, 2)
    t1 = numpy.linspace(0.0, moose.element("/clock").runTime, len(table1.vector)) # sec
    pylab.plot( t1, table1.vector, label="Protein Conc (interval- 8000s)" )
    pylab.legend()
    pylab.savefig( os.path.join( dataDir, '%s_%s.png' % (table1.name, runTime) ) )

    print('[INFO] Saving data to csv files in %s' % dataDir)
    tabPath1 = os.path.join( dataDir, '%s_%s.csv' % (table.name, runTime))
    numpy.savetxt(tabPath1, numpy.matrix([t, table.vector]).T, newline='\n')
    tabPath2 = os.path.join( dataDir, '%s_%s.csv' % (table1.name, runTime) )
    numpy.savetxt(tabPath2, numpy.matrix([t1, table1.vector]).T, newline='\n')
Example #23
0
def main():
    star = StarBinary(90.0, 0.5, nside=64, limb_law=-1, limb_coeff=[0.8])
    Flux0 = star.flux(0.0)
    phases = np.linspace(-0.5, 0.5, 100)

    flux1 = np.zeros(len(phases))
    for i in range(len(phases)):
        flux1[i] = star.flux(phases[i]) / Flux0

    # for tt in np.arange(0,360,80):
    # star.makeSpot(0,-45,10.,0.8)
    # star.makeSpot(45,+00,10.,0.8)
    # star.makeSpot(00, -90, 20., 0.)
    # for theta in range(0,360,45):
    #	star.makeSpot(theta,65,10.,0.8)

    # star.makeSpot(00,-90,20.,0.)
    # star.makeSpot(0,+45,10.,0.8)
    # star.makeSpot(270,-10.,10.,0.8)
    # star.makeSpot(180,-45.,10.,0.8)
    #	star.makeSpot(tt,65.,10.,0.5)

    flux2 = np.zeros(len(phases))
    for i in range(len(phases)):
        flux2[i] = star.flux(phases[i]) / Flux0

    H.mollview(star.I, sub=211, rot=(-90, 90))

    #ff = np.loadtxt('/tmp/cl.dat', unpack=True)

    py.subplot(212)
    py.plot(phases, flux1, '-')
    # py.plot(phases,flux2,'-')
    #py.plot(ff[0], ff[1], '.')
    py.show()
Example #24
0
def graphTSPResults(resultsDir,numberOfTours):
    x=[]
    y=[]
    files=[open("%s/objectiveFunctionReport.txt" % resultsDir),
       open("%s/fitnessReport.txt" % resultsDir)]
    for f in files:
        x.append([])
        y.append([])
        i=len(x)-1
        for line in f:
            line=line.split(',')
            if line[0] != "gen":
                x[i].append(int(line[0]))
                y[i].append(float(line[1] if i==1 else line[2]))
            
    ylen=len(y[0])
    pl.subplot(2,1,1)
    pl.plot(x[0],y[0],'bo')
    pl.ylabel('Minimum Distance')
    pl.title("TSP with a %s City Tour" % numberOfTours)
    pl.annotate("{0:,}".format(y[0][0]),xy=(x[0][0],y[0][0]),  xycoords='data',
             xytext=(30, -30), textcoords='offset points',
             arrowprops=dict(arrowstyle="->") )
    pl.annotate("{0:,}".format(y[0][ylen-1]),xy=(x[0][ylen-1],y[0][ylen-1]),  xycoords='data',
             xytext=(-30,30), textcoords='offset points',
             arrowprops=dict(arrowstyle="->") )

    pl.subplot(2,1,2)
    pl.plot(x[1],y[1],'go')
    pl.xlabel('Generation')
    pl.ylabel('Fitness')
    pl.savefig("%s/tsp_result.png" % resultsDir)
    pl.clf()
def plot_Barycenter(dataset_name, feat, unfeat, repo):

    if dataset_name==MNIST:
        _, _, test=get_data(dataset_name, repo, labels=True)
        xtest1,_,_, labels,_=test
    else:
        _, _, test=get_data(dataset_name, repo, labels=False)
        xtest1,_,_ =test
        labels=np.zeros((len(xtest1),))
    # get labels
    def bary_wdl2(index): return _bary_wdl2(index, xtest1, feat, unfeat)
    
    n=xtest1.shape[-1]
    
    num_class = (int)(max(labels)+1)
    barys=[bary_wdl2(np.where(labels==i)) for i in range(num_class)]
    pl.figure(1, (num_class, 1))
    for i in range(num_class):
        pl.subplot(1,10,1+i)
        pl.imshow(barys[i][0,0,:,:],cmap='Blues',interpolation='nearest')
        pl.xticks(())
        pl.yticks(())
        if i==0:
            pl.ylabel('DWE Bary.')
        if num_class >1:
            pl.title('{}'.format(i))
    pl.tight_layout(pad=0,h_pad=-2,w_pad=-2) 
    pl.savefig("imgs/{}_dwe_bary.pdf".format(dataset_name))
Example #26
0
def main():
    src_cv_img_1 = cv.LoadImage("data/gorskaya/images/1/g126.jpg")
    src_cv_img_gray_1 = cv.LoadImage("data/gorskaya/images/1/g126.jpg", 
        cv.CV_LOAD_IMAGE_GRAYSCALE)

    src_cv_img_2 = cv.LoadImage("data/gorskaya/images/1/g127.jpg")
    src_cv_img_gray_2 = cv.LoadImage("data/gorskaya/images/1/g127.jpg", 
        cv.CV_LOAD_IMAGE_GRAYSCALE)

    (keypoints_1, descriptors_1) = \
        cv.ExtractSURF(src_cv_img_gray_1, None, cv.CreateMemStorage(), 
            (0, 30000, 3, 1))
    (keypoints_2, descriptors_2) = \
        cv.ExtractSURF(src_cv_img_gray_2, None, cv.CreateMemStorage(), 
            (0, 30000, 3, 1))

    print("Found {0} and {1} keypoints".format(
        len(keypoints_1), len(keypoints_2)))

    src_arr_1 = array(src_cv_img_1[:, :])[:, :, ::-1]
    src_arr_2 = array(src_cv_img_2[:, :])[:, :, ::-1]

    pylab.rc('image', interpolation='nearest')

    pylab.subplot(121)
    pylab.imshow(src_arr_1)
    pylab.plot(*zip(*[k[0] for k in keypoints_1]), 
        marker='.', color='r', ls='')

    pylab.subplot(122)
    pylab.imshow(src_arr_2)
    pylab.plot(*zip(*[k[0] for k in keypoints_2]), 
        marker='.', color='r', ls='')

    pylab.show()
Example #27
0
def graphSimpleResults(resultsDir):
    x=[]
    y=[]
    files=[open("%s/objectiveFunctionReport.txt" % resultsDir),
       open("%s/fitnessReport.txt" % resultsDir)]
    for f in files:
        x.append([])
        y.append([])
        i=len(x)-1
        for line in f:
            line=line.split(',')
            if line[0] != "gen":
                x[i].append(int(line[0]))
                y[i].append(float(line[1]))

    ylen=len(y[0])
    pl.subplot(2,1,1)
    pl.plot(x[0],y[0],'bo')
    pl.ylabel('Maximum x')
    pl.title('Maximizing x**2 with SGA')
    pl.annotate("{0:,}".format(y[0][0]),xy=(x[0][0],y[0][0]),  xycoords='data',
             xytext=(50, 30), textcoords='offset points',
             arrowprops=dict(arrowstyle="->") )
    pl.annotate("{0:,}".format(y[0][ylen-1]),xy=(x[0][ylen-1],y[0][ylen-1]),  xycoords='data',
             xytext=(-30, -30), textcoords='offset points',
             arrowprops=dict(arrowstyle="->") )

    pl.subplot(2,1,2)
    pl.plot(x[1],y[1],'go')
    pl.xlabel('Generation')
    pl.ylabel('Fitness')
    pl.savefig("%s/simple_result.png" % resultsDir)
Example #28
0
def traceplot(traces, thin, burn):
    '''
    Plot parameter estimates for different levels of the model
    into the same plots. Black lines are individual observers
    and red lines are mean estimates.
    '''
    variables = ['Slope1', 'Slope2', 'Offset', 'Split']
    for i, var in enumerate(variables):
        plt.subplot(2, 2, i + 1)
        vals = get_values(traces, var, thin, burn)
        dim = (vals.min() - vals.std(), vals.max() + vals.std())
        x = plt.linspace(*dim, num=1000)
        for v in vals.T:
            a = gaussian_kde(v)
            y = a.evaluate(x)
            y = y / y.max()
            plt.plot(x, y, 'k', alpha=.5)
        try:
            vals = get_values(traces, 'Mean_' + var, thin, burn)
            a = gaussian_kde(vals)
            y = a.evaluate(x)
            y = y / y.max()
            plt.plot(x, y, 'r', alpha=.75)
        except KeyError:
            pass
        plt.ylim([0, 1.1])
        plt.yticks([0])
        sns.despine(offset=5, trim=True)
        plt.title(var)
Example #29
0
def display_coeff(data=None):
    betaAll,betaErrAll, R2adjAll = measure_stamp_coeff(data = data, zernike_max_order=20)
    ind = np.arange(len(betaAll[0]))
    momname = ('M20','M22.Real','M22.imag','M31.real','M31.imag','M33.real','M33.imag')
    fmtarr = ['bo-','ro-','go-','co-','mo-','yo-','ko-']
    pl.figure(figsize=(17,13))
    for i in range(7):
        pl.subplot(7,1,i+1)
        pl.errorbar(ind,betaAll[i],yerr = betaErrAll[i],fmt=fmtarr[i])
        pl.grid()
        pl.xlim(-1,21)
        if i ==0:
            pl.ylim(-10,65)
        elif i ==1:
            pl.ylim(-5,6)
        elif i ==2:
            pl.ylim(-5,6)
        elif i == 3:
            pl.ylim(-0.1,0.1)
        elif i == 4:
            pl.ylim(-0.1,0.1)
        elif i ==5:
            pl.ylim(-100,100)
        elif i == 6:
            pl.ylim(-100,100)
        pl.xticks(ind,('','','','','','','','','','','','','','','','','','','',''))
        pl.ylabel(momname[i])
    pl.xticks(ind,('0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19'))
    pl.xlabel('Zernike Coefficients')
    return '--- done ! ----'
Example #30
0
def sim_results(obs, modes, stars, model, data):


    synth = model.generate_data(modes)
    synth_stats = model.summary_stats(synth)
    obs_stats = model.summary_stats(obs)


    f = plt.figure(figsize=(15,3))
    plt.suptitle('Obs Cand.:{}; Sim Cand.:{}'.format(obs.size, synth.size))
    plt.rc('legend', fontsize='xx-small', frameon=False)
    plt.subplot(121)
    bins = opt_bin(obs_stats[0],synth_stats[0])
    plt.hist(obs_stats[0], bins=bins, histtype='step', label='Data', lw=2)
    plt.hist(synth_stats[0], bins=bins, histtype='step', label='Simulation', lw=2)
    plt.xlabel(r'$\xi$')
    plt.legend()

    plt.subplot(122)
    bins = opt_bin(obs_stats[1],synth_stats[1])
    plt.hist(obs_stats[1], bins=np.arange(bins.min()-0.5, bins.max()+1.5,
                                          1),
             histtype='step', label='Data', log=True, lw=2)
    plt.hist(synth_stats[1], bins=np.arange(bins.min()-0.5, bins.max()+1.5,
                                            1),
             histtype='step', label='Simulation', log=True, lw=2)
    plt.xlabel(r'$N_p$')
    plt.legend()
Example #31
0
def plot_ts(t, data, names=None, output='show', ax=None, color=None, lw=1.0):
    #print 'total date range: ',t[-1]-t[0]
    #--set up date formatters
    if t[-1] - t[0] < 90:
        majortick = MonthLocator()
        minortick = DayLocator(15)
        minFmt = DateFormatter('%d')
        majFmt = DateFormatter('%b')
    elif t[-1] - t[0] < 365:
        majortick = MonthLocator()
        minortick = DayLocator()
        minFmt = DateFormatter('')
        majFmt = DateFormatter('%b')
    elif t[-1] - t[0] < 5280:
        majortick = YearLocator()
        minortick = MonthLocator((5, 9))
        minFmt = DateFormatter('%b')
        majFmt = DateFormatter('%Y')
    elif t[-1] - t[0] < 3650:
        majortick = YearLocator(2)
        minortick = MonthLocator()
        minFmt = DateFormatter('')
        majFmt = DateFormatter('%Y')
    else:
        majortick = YearLocator(5)
        minortick = YearLocator()
        minFmt = DateFormatter('')
        majFmt = DateFormatter('%Y')

    if ax == None:
        fig = pylab.figure()
        ax = pylab.subplot(111)
    #print data.shape
    try:
        for s in range(0, np.shape(data)[1]):
            if color != None:

                ax.plot(t, data[:, s], label=names[s], color=color[s], lw=lw)
            else:
                ax.plot(t, data[:, s], label=names[s], lw=lw)
    except:
        if color != None:
            ax.plot(t, data, label=names, color=color, lw=lw)
        else:
            ax.plot(t, data, label=names, lw=lw)

    if names != None:
        ax.legend()

    ax.xaxis.set_major_locator(majortick)
    ax.xaxis.set_minor_locator(minortick)
    ax.xaxis.set_major_formatter(majFmt)
    ax.xaxis.set_minor_formatter(minFmt)

    if output == 'show':
        pylab.show()
        return
    elif output == None:
        return ax
    else:
        fmt = output.split('.')[-1]
        pylab.savefig(output, orientation='portrait', format=fmt, dpi=150)
        return


#names,data = load_ssf('flows.out')
#print names,len(data),data[0].shape
#print data[0][0,0],data[0][0,1]
#plot_ts(data[0][:,0],data[0][:,1],names[0])
Example #32
0
 def plot(self, nums, i):
     pylab.subplot(1, 2, i)
     pylab.hist(nums, normed=1)
     pylab.title('{} numbers. M = {:.4f}. D = {:.4f}. K = {:.4f}'.format(
         len(nums), M(nums), D(nums), correlation(nums)))
Example #33
0
if __name__ == "__main__":
    scratch_square = empty(grid_shape)
    grid_square = zeros(grid_shape)
    block_low = int(grid_shape[0] * .4)
    block_high = int(grid_shape[0] * .5)
    grid_square[block_low:block_high, block_low:block_high] = 0.005

    grid_python = 1 - py.imread(
        request.urlopen(
            "http://a4.mzstatic.com/us/r30/Purple4/v4/e8/20/fd/e820fded-8a78-06ac-79d0-f1d140346976/mzl.huoealqj.png"
        )).mean(2)
    grid_python = asarray(grid_python, dtype='float64')
    scratch_python = empty(grid_python.shape)

    py.subplot(3, 2, 1)
    py.imshow(grid_square.copy())
    py.ylabel("t = 0 seconds")
    py.gca().get_xaxis().set_ticks([])
    py.gca().get_yaxis().set_ticks([])
    py.subplot(3, 2, 2)
    py.imshow(grid_python.copy())
    py.gca().get_xaxis().set_ticks([])
    py.gca().get_yaxis().set_ticks([])

    for i in range(500):
        evolve(grid_square, 0.1, scratch_square)
        grid_square, scratch_square = scratch_square, grid_square

        evolve(grid_python, 0.1, scratch_python)
        grid_python, scratch_python = scratch_python, grid_python
Example #34
0
File: mpl.py Project: yokeldd/vaex
def histogram(self, x=None, what="count(*)", grid=None, shape=64, facet=None, limits=None, figsize=None, f="identity", n=None, normalize_axis=None,
              xlabel=None, ylabel=None, label=None,
              selection=None, show=False, tight_layout=True, hardcopy=None,
              progress=None,
              **kwargs):
    """Plot a histogram.

    Example:

    >>> df.histogram(df.x)
    >>> df.histogram(df.x, limits=[0, 100], shape=100)
    >>> df.histogram(df.x, what='mean(y)', limits=[0, 100], shape=100)

    If you want to do a computation yourself, pass the grid argument, but you are responsible for passing the
    same limits arguments:

    >>> counts = df.mean(df.y, binby=df.x, limits=[0, 100], shape=100)/100.
    >>> df.histogram(df.x, limits=[0, 100], shape=100, grid=means, label='mean(y)/100')

    :param x: Expression to bin in the x direction
    :param what: What to plot, count(*) will show a N-d histogram, mean('x'), the mean of the x column, sum('x') the sum
    :param grid: If the binning is done before by yourself, you can pass it
    :param facet: Expression to produce facetted plots ( facet='x:0,1,12' will produce 12 plots with x in a range between 0 and 1)
    :param limits: list of [xmin, xmax], or a description such as 'minmax', '99%'
    :param figsize: (x, y) tuple passed to pylab.figure for setting the figure size
    :param f: transform values by: 'identity' does nothing 'log' or 'log10' will show the log of the value
    :param n: normalization function, currently only 'normalize' is supported, or None for no normalization
    :param normalize_axis: which axes to normalize on, None means normalize by the global maximum.
    :param normalize_axis:
    :param xlabel: String for label on x axis (may contain latex)
    :param ylabel: Same for y axis
    :param: tight_layout: call pylab.tight_layout or not
    :param kwargs: extra argument passed to pylab.plot
    :return:
    """

    import pylab
    f = _parse_f(f)
    n = _parse_n(n)
    if type(shape) == int:
        shape = (shape,)
    binby = []
    x = _ensure_strings_from_expressions(x)
    for expression in [x]:
        if expression is not None:
            binby = [expression] + binby
    limits = self.limits(binby, limits)
    if figsize is not None:
        pylab.figure(num=None, figsize=figsize, dpi=80, facecolor='w', edgecolor='k')
    fig = pylab.gcf()
    import re
    if facet is not None:
        match = re.match("(.*):(.*),(.*),(.*)", facet)
        if match:
            groups = match.groups()
            facet_expression = groups[0]
            facet_limits = [ast.literal_eval(groups[1]), ast.literal_eval(groups[2])]
            facet_count = ast.literal_eval(groups[3])
            limits.append(facet_limits)
            binby.append(facet_expression)
            shape = (facet_count,) + shape
        else:
            raise ValueError("Could not understand 'facet' argument %r, expected something in form: 'column:-1,10:5'" % facet)

    if grid is None:
        if what:
            if isinstance(what, (vaex.stat.Expression)):
                grid = what.calculate(self, binby=binby, limits=limits, shape=shape, selection=selection)
            else:
                what = what.strip()
                index = what.index("(")
                import re
                groups = re.match("(.*)\((.*)\)", what).groups()
                if groups and len(groups) == 2:
                    function = groups[0]
                    arguments = groups[1].strip()
                    functions = ["mean", "sum", "std", "count"]
                    if function in functions:
                        # grid = getattr(self, function)(arguments, binby, limits=limits, shape=shape, selection=selection)
                        grid = getattr(vaex.stat, function)(arguments).calculate(self, binby=binby, limits=limits, shape=shape, selection=selection, progress=progress)
                    elif function == "count" and arguments == "*":
                        grid = self.count(binby=binby, shape=shape, limits=limits, selection=selection, progress=progress)
                    elif function == "cumulative" and arguments == "*":
                        # TODO: comulative should also include the tails outside limits
                        grid = self.count(binby=binby, shape=shape, limits=limits, selection=selection, progress=progress)
                        grid = np.cumsum(grid)
                    else:
                        raise ValueError("Could not understand method: %s, expected one of %r'" % (function, functions))
                else:
                    raise ValueError("Could not understand 'what' argument %r, expected something in form: 'count(*)', 'mean(x)'" % what)
        else:
            grid = self.histogram(binby, size=shape, limits=limits, selection=selection)
    fgrid = f(grid)
    if n is not None:
        # ngrid = n(fgrid, axis=normalize_axis)
        ngrid = fgrid / fgrid.sum()
    else:
        ngrid = fgrid
        # reductions = [_parse_reduction(r, colormap, colors) for r in reduce]
        # rgrid = ngrid * 1.
        # for r in reduce:
        #   r = _parse_reduction(r, colormap, colors)
        #   rgrid = r(rgrid)
        # grid = self.reduce(grid, )
    xmin, xmax = limits[-1]
    if facet:
        N = len(grid[-1])
    else:
        N = len(grid)
    xexpression = binby[0]
    xar = np.arange(N + 1) / (N - 0.) * (xmax - xmin) + xmin
    label = str(label or selection or x)
    if facet:
        import math
        rows, columns = int(math.ceil(facet_count / 4.)), 4
        values = np.linspace(facet_limits[0], facet_limits[1], facet_count + 1)
        for i in range(facet_count):
            ax = pylab.subplot(rows, columns, i + 1)
            value = ax.plot(xar, ngrid[i], drawstyle="steps-mid", label=label, **kwargs)
            v1, v2 = values[i], values[i + 1]
            pylab.xlabel(xlabel or x)
            pylab.ylabel(ylabel or what)
            ax.set_title("%3f <= %s < %3f" % (v1, facet_expression, v2))
            if self.iscategory(xexpression):
                labels = self.category_labels(xexpression)
                step = len(labels) // max_labels
                pylab.xticks(range(len(labels))[::step], labels[::step], size='small')
    else:
        # im = pylab.imshow(rgrid, extent=np.array(limits[:2]).flatten(), origin="lower", aspect=aspect)
        pylab.xlabel(xlabel or self.label(x))
        pylab.ylabel(ylabel or what)
        # print(xar, ngrid)
        # repeat the first element, that's how plot/steps likes it..
        g = np.concatenate([ngrid[0:1], ngrid])
        value = pylab.plot(xar, g, drawstyle="steps-pre", label=label, **kwargs)
        if self.iscategory(xexpression):
            labels = self.category_labels(xexpression)
            step = len(labels) // max_labels
            pylab.xticks(range(len(labels))[::step], labels[::step], size='small')
    if tight_layout:
        pylab.tight_layout()
    if hardcopy:
        pylab.savefig(hardcopy)
    if show:
        pylab.show()
    return value
Example #35
0
File: mpl.py Project: yokeldd/vaex
def heatmap(self, x=None, y=None, z=None, what="count(*)", vwhat=None, reduce=["colormap"], f=None,
            normalize="normalize", normalize_axis="what",
            vmin=None, vmax=None,
            shape=256, vshape=32, limits=None, grid=None, colormap="afmhot",  # colors=["red", "green", "blue"],
            figsize=None, xlabel=None, ylabel=None, aspect="auto", tight_layout=True, interpolation="nearest", show=False,
            colorbar=True,
            colorbar_label=None,
            selection=None, selection_labels=None, title=None,
            background_color="white", pre_blend=False, background_alpha=1.,
            visual=dict(x="x", y="y", layer="z", fade="selection", row="subspace", column="what"),
            smooth_pre=None, smooth_post=None,
            wrap=True, wrap_columns=4,
            return_extra=False, hardcopy=None):
    """Viz data in a 2d histogram/heatmap.

    Declarative plotting of statistical plots using matplotlib, supports subplots, selections, layers.

    Instead of passing x and y, pass a list as x argument for multiple panels. Give what a list of options to have multiple
    panels. When both are present then will be origanized in a column/row order.

    This methods creates a 6 dimensional 'grid', where each dimension can map the a visual dimension.
    The grid dimensions are:

     * x: shape determined by shape, content by x argument or the first dimension of each space
     * y:   ,,
     * z:  related to the z argument
     * selection: shape equals length of selection argument
     * what: shape equals length of what argument
     * space: shape equals length of x argument if multiple values are given

     By default, this its shape is (1, 1, 1, 1, shape, shape) (where x is the last dimension)

    The visual dimensions are

     * x: x coordinate on a plot / image (default maps to grid's x)
     * y: y   ,,                         (default maps to grid's y)
     * layer: each image in this dimension is blended togeher to one image (default maps to z)
     * fade: each image is shown faded after the next image (default mapt to selection)
     * row: rows of subplots (default maps to space)
     * columns: columns of subplot (default maps to what)

    All these mappings can be changes by the visual argument, some examples:

    >>> df.plot('x', 'y', what=['mean(x)', 'correlation(vx, vy)'])

    Will plot each 'what' as a column.

    >>> df.plot('x', 'y', selection=['FeH < -3', '(FeH >= -3) & (FeH < -2)'], visual=dict(column='selection'))

    Will plot each selection as a column, instead of a faded on top of each other.





    :param x: Expression to bin in the x direction (by default maps to x), or list of pairs, like [['x', 'y'], ['x', 'z']], if multiple pairs are given, this dimension maps to rows by default
    :param y:                          y           (by default maps to y)
    :param z: Expression to bin in the z direction, followed by a :start,end,shape  signature, like 'FeH:-3,1:5' will produce 5 layers between -10 and 10 (by default maps to layer)
    :param what: What to plot, count(*) will show a N-d histogram, mean('x'), the mean of the x column, sum('x') the sum, std('x') the standard deviation, correlation('vx', 'vy') the correlation coefficient. Can also be a list of values, like ['count(x)', std('vx')], (by default maps to column)
    :param reduce:
    :param f: transform values by: 'identity' does nothing 'log' or 'log10' will show the log of the value
    :param normalize: normalization function, currently only 'normalize' is supported
    :param normalize_axis: which axes to normalize on, None means normalize by the global maximum.
    :param vmin: instead of automatic normalization, (using normalize and normalization_axis) scale the data between vmin and vmax to [0, 1]
    :param vmax: see vmin
    :param shape: shape/size of the n-D histogram grid
    :param limits: list of [[xmin, xmax], [ymin, ymax]], or a description such as 'minmax', '99%'
    :param grid: if the binning is done before by yourself, you can pass it
    :param colormap: matplotlib colormap to use
    :param figsize: (x, y) tuple passed to pylab.figure for setting the figure size
    :param xlabel:
    :param ylabel:
    :param aspect:
    :param tight_layout: call pylab.tight_layout or not
    :param colorbar: plot a colorbar or not
    :param interpolation: interpolation for imshow, possible options are: 'nearest', 'bilinear', 'bicubic', see matplotlib for more
    :param return_extra:
    :return:
    """
    import pylab
    import matplotlib
    n = _parse_n(normalize)
    if type(shape) == int:
        shape = (shape,) * 2
    binby = []
    x = _ensure_strings_from_expressions(x)
    y = _ensure_strings_from_expressions(y)
    for expression in [y, x]:
        if expression is not None:
            binby = [expression] + binby
    fig = pylab.gcf()
    if figsize is not None:
        fig.set_size_inches(*figsize)
    import re

    what_units = None
    whats = _ensure_list(what)
    selections = _ensure_list(selection)
    selections = _ensure_strings_from_expressions(selections)

    if y is None:
        waslist, [x, ] = vaex.utils.listify(x)
    else:
        waslist, [x, y] = vaex.utils.listify(x, y)
        x = list(zip(x, y))
        limits = [limits]

    # every plot has its own vwhat for now
    vwhats = _expand_limits(vwhat, len(x))  # TODO: we're abusing this function..
    logger.debug("x: %s", x)
    limits, shape = self.limits(x, limits, shape=shape)
    shape = shape[0]
    logger.debug("limits: %r", limits)

    # mapping of a grid axis to a label
    labels = {}
    shape = _expand_shape(shape, 2)
    vshape = _expand_shape(shape, 2)
    if z is not None:
        match = re.match("(.*):(.*),(.*),(.*)", z)
        if match:
            groups = match.groups()
            import ast
            z_expression = groups[0]
            logger.debug("found groups: %r", list(groups))
            z_limits = [ast.literal_eval(groups[1]), ast.literal_eval(groups[2])]
            z_shape = ast.literal_eval(groups[3])
            # for pair in x:
            x = [[z_expression] + list(k) for k in x]
            limits = np.array([[z_limits] + list(k) for k in limits])
            shape = (z_shape,) + shape
            vshape = (z_shape,) + vshape
            logger.debug("x = %r", x)
            values = np.linspace(z_limits[0], z_limits[1], num=z_shape + 1)
            labels["z"] = list(["%s <= %s < %s" % (v1, z_expression, v2) for v1, v2 in zip(values[:-1], values[1:])])
        else:
            raise ValueError("Could not understand 'z' argument %r, expected something in form: 'column:-1,10:5'" % facet)
    else:
        z_shape = 1

    # z == 1
    if z is None:
        total_grid = np.zeros((len(x), len(whats), len(selections), 1) + shape, dtype=float)
        total_vgrid = np.zeros((len(x), len(whats), len(selections), 1) + vshape, dtype=float)
    else:
        total_grid = np.zeros((len(x), len(whats), len(selections)) + shape, dtype=float)
        total_vgrid = np.zeros((len(x), len(whats), len(selections)) + vshape, dtype=float)
    logger.debug("shape of total grid: %r", total_grid.shape)
    axis = dict(plot=0, what=1, selection=2)
    xlimits = limits

    grid_axes = dict(x=-1, y=-2, z=-3, selection=-4, what=-5, subspace=-6)
    visual_axes = dict(x=-1, y=-2, layer=-3, fade=-4, column=-5, row=-6)
    # visual_default=dict(x="x", y="y", z="layer", selection="fade", subspace="row", what="column")
    # visual: mapping of a plot axis, to a grid axis
    visual_default = dict(x="x", y="y", layer="z", fade="selection", row="subspace", column="what")

    def invert(x): return dict((v, k) for k, v in x.items())
    # visual_default_reverse = invert(visual_default)
    # visual_ = visual_default
    # visual = dict(visual) # copy for modification
    # add entries to avoid mapping multiple times to the same axis
    free_visual_axes = list(visual_default.keys())
    # visual_reverse = invert(visual)
    logger.debug("1: %r %r", visual, free_visual_axes)
    for visual_name, grid_name in visual.items():
        if visual_name in free_visual_axes:
            free_visual_axes.remove(visual_name)
        else:
            raise ValueError("visual axes %s used multiple times" % visual_name)
    logger.debug("2: %r %r", visual, free_visual_axes)
    for visual_name, grid_name in visual_default.items():
        if visual_name in free_visual_axes and grid_name not in visual.values():
            free_visual_axes.remove(visual_name)
            visual[visual_name] = grid_name
    logger.debug("3: %r %r", visual, free_visual_axes)
    for visual_name, grid_name in visual_default.items():
        if visual_name not in free_visual_axes and grid_name not in visual.values():
            visual[free_visual_axes.pop(0)] = grid_name

    logger.debug("4: %r %r", visual, free_visual_axes)

    visual_reverse = invert(visual)
    # TODO: the meaning of visual and visual_reverse is changed below this line, super confusing
    visual, visual_reverse = visual_reverse, visual
    # so now, visual: mapping of a grid axis to plot axis
    # visual_reverse: mapping of a grid axis to plot axis
    move = {}
    for grid_name, visual_name in visual.items():
        if visual_axes[visual_name] in visual.values():
            index = visual.values().find(visual_name)
            key = visual.keys()[index]
            raise ValueError("trying to map %s to %s while, it is already mapped by %s" % (grid_name, visual_name, key))
        move[grid_axes[grid_name]] = visual_axes[visual_name]

    # normalize_axis = _ensure_list(normalize_axis)

    fs = _expand(f, total_grid.shape[grid_axes[normalize_axis]])
    # assert len(vwhat)
    # labels["y"] = ylabels
    what_labels = []
    if grid is None:
        grid_of_grids = []
        for i, (binby, limits) in enumerate(zip(x, xlimits)):
            grid_of_grids.append([])
            for j, what in enumerate(whats):
                if isinstance(what, vaex.stat.Expression):
                    grid = what.calculate(self, binby=binby, shape=shape, limits=limits, selection=selections, delay=True)
                else:
                    what = what.strip()
                    index = what.index("(")
                    import re
                    groups = re.match("(.*)\((.*)\)", what).groups()
                    if groups and len(groups) == 2:
                        function = groups[0]
                        arguments = groups[1].strip()
                        if "," in arguments:
                            arguments = arguments.split(",")
                        functions = ["mean", "sum", "std", "var", "correlation", "covar", "min", "max", "median_approx"]
                        unit_expression = None
                        if function in ["mean", "sum", "std", "min", "max", "median"]:
                            unit_expression = arguments
                        if function in ["var"]:
                            unit_expression = "(%s) * (%s)" % (arguments, arguments)
                        if function in ["covar"]:
                            unit_expression = "(%s) * (%s)" % arguments
                        if unit_expression:
                            unit = self.unit(unit_expression)
                            if unit:
                                what_units = unit.to_string('latex_inline')
                        if function in functions:
                            grid = getattr(self, function)(arguments, binby=binby, limits=limits, shape=shape, selection=selections, delay=True)
                        elif function == "count":
                            grid = self.count(arguments, binby, shape=shape, limits=limits, selection=selections, delay=True)
                        else:
                            raise ValueError("Could not understand method: %s, expected one of %r'" % (function, functions))
                    else:
                        raise ValueError("Could not understand 'what' argument %r, expected something in form: 'count(*)', 'mean(x)'" % what)
                if i == 0:  # and j == 0:
                    what_label = str(whats[j])
                    if what_units:
                        what_label += " (%s)" % what_units
                    if fs[j]:
                        what_label = fs[j] + " " + what_label
                    what_labels.append(what_label)
                grid_of_grids[-1].append(grid)
        self.execute()
        for i, (binby, limits) in enumerate(zip(x, xlimits)):
            for j, what in enumerate(whats):
                grid = grid_of_grids[i][j].get()
                total_grid[i, j, :, :] = grid[:, None, ...]
        labels["what"] = what_labels
    else:
        dims_left = 6 - len(grid.shape)
        total_grid = np.broadcast_to(grid, (1,) * dims_left + grid.shape)

    #           visual=dict(x="x", y="y", selection="fade", subspace="facet1", what="facet2",)
    def _selection_name(name):
        if name in [None, False]:
            return "selection: all"
        elif name in ["default", True]:
            return "selection: default"
        else:
            return "selection: %s" % name
    if selection_labels is None:
        labels["selection"] = list([_selection_name(k) for k in selections])
    else:
        labels["selection"] = selection_labels

    # visual_grid = np.moveaxis(total_grid, move.keys(), move.values())
    # np.moveaxis is in np 1.11 only?, use transpose
    axes = [None] * len(move)
    for key, value in move.items():
        axes[value] = key
    visual_grid = np.transpose(total_grid, axes)

    logger.debug("grid shape: %r", total_grid.shape)
    logger.debug("visual: %r", visual.items())
    logger.debug("move: %r", move)
    logger.debug("visual grid shape: %r", visual_grid.shape)

    xexpressions = []
    yexpressions = []
    for i, (binby, limits) in enumerate(zip(x, xlimits)):
        xexpressions.append(binby[0])
        yexpressions.append(binby[1])
    if xlabel is None:
        xlabels = []
        ylabels = []
        for i, (binby, limits) in enumerate(zip(x, xlimits)):
            if z is not None:
                xlabels.append(self.label(binby[1]))
                ylabels.append(self.label(binby[2]))
            else:
                xlabels.append(self.label(binby[0]))
                ylabels.append(self.label(binby[1]))
    else:
        Nl = visual_grid.shape[visual_axes['row']]
        xlabels = _expand(xlabel, Nl)
        ylabels = _expand(ylabel, Nl)
    #labels[visual["x"]] = (xlabels, ylabels)
    labels["x"] = xlabels
    labels["y"] = ylabels

    # grid = total_grid
    # print(grid.shape)
    # grid = self.reduce(grid, )
    axes = []
    # cax = pylab.subplot(1,1,1)

    background_color = np.array(matplotlib.colors.colorConverter.to_rgb(background_color))

    # if grid.shape[axis["selection"]] > 1:#  and not facet:
    #   rgrid = vaex.image.fade(rgrid)
    #   finite_mask = np.any(finite_mask, axis=0) # do we really need this
    #   print(rgrid.shape)
    # facet_row_axis = axis["what"]
    import math
    facet_columns = None
    facets = visual_grid.shape[visual_axes["row"]] * visual_grid.shape[visual_axes["column"]]
    if visual_grid.shape[visual_axes["column"]] == 1 and wrap:
        facet_columns = min(wrap_columns, visual_grid.shape[visual_axes["row"]])
        wrapped = True
    elif visual_grid.shape[visual_axes["row"]] == 1 and wrap:
        facet_columns = min(wrap_columns, visual_grid.shape[visual_axes["column"]])
        wrapped = True
    else:
        wrapped = False
        facet_columns = visual_grid.shape[visual_axes["column"]]
    facet_rows = int(math.ceil(facets / facet_columns))
    logger.debug("facet_rows: %r", facet_rows)
    logger.debug("facet_columns: %r", facet_columns)
    # if visual_grid.shape[visual_axes["row"]] > 1: # and not wrap:
    #   #facet_row_axis = axis["what"]
    #   facet_columns = visual_grid.shape[visual_axes["column"]]
    # else:
    #   facet_columns = min(wrap_columns, facets)
    # if grid.shape[axis["plot"]] > 1:#  and not facet:

    # this loop could be done using axis arguments everywhere
    # assert len(normalize_axis) == 1, "currently only 1 normalization axis supported"
    grid = visual_grid * 1.
    fgrid = visual_grid * 1.
    ngrid = visual_grid * 1.
    # colorgrid = np.zeros(ngrid.shape + (4,), float)
    # print "norma", normalize_axis, visual_grid.shape[visual_axes[visual[normalize_axis]]]
    vmins = _expand(vmin, visual_grid.shape[visual_axes[visual[normalize_axis]]], type=list)
    vmaxs = _expand(vmax, visual_grid.shape[visual_axes[visual[normalize_axis]]], type=list)
    # for name in normalize_axis:
    visual_grid
    if smooth_pre:
        grid = vaex.grids.gf(grid, smooth_pre)
    if 1:
        axis = visual_axes[visual[normalize_axis]]
        for i in range(visual_grid.shape[axis]):
            item = [slice(None, None, None), ] * len(visual_grid.shape)
            item[axis] = i
            item = tuple(item)
            f = _parse_f(fs[i])
            with np.errstate(divide='ignore', invalid='ignore'):  # these are fine, we are ok with nan's in vaex
                fgrid.__setitem__(item, f(grid.__getitem__(item)))
            # print vmins[i], vmaxs[i]
            if vmins[i] is not None and vmaxs[i] is not None:
                nsubgrid = fgrid.__getitem__(item) * 1
                nsubgrid -= vmins[i]
                nsubgrid /= (vmaxs[i] - vmins[i])
            else:
                nsubgrid, vmin, vmax = n(fgrid.__getitem__(item))
                vmins[i] = vmin
                vmaxs[i] = vmax
            # print "    ", vmins[i], vmaxs[i]
            ngrid.__setitem__(item, nsubgrid)

    if 0:  # TODO: above should be like the code below, with custom vmin and vmax
        grid = visual_grid[i]
        f = _parse_f(fs[i])
        fgrid = f(grid)
        finite_mask = np.isfinite(grid)
        finite_mask = np.any(finite_mask, axis=0)
        if vmin is not None and vmax is not None:
            ngrid = fgrid * 1
            ngrid -= vmin
            ngrid /= (vmax - vmin)
            ngrid = np.clip(ngrid, 0, 1)
        else:
            ngrid, vmin, vmax = n(fgrid)
            # vmin, vmax = np.nanmin(fgrid), np.nanmax(fgrid)
    # every 'what', should have its own colorbar, check if what corresponds to
    # rows or columns in facets, if so, do a colorbar per row or per column

    rows, columns = int(math.ceil(facets / float(facet_columns))), facet_columns
    colorbar_location = "individual"
    if visual["what"] == "row" and visual_grid.shape[1] == facet_columns:
        colorbar_location = "per_row"
    if visual["what"] == "column" and visual_grid.shape[0] == facet_rows:
        colorbar_location = "per_column"
    # values = np.linspace(facet_limits[0], facet_limits[1], facet_count+1)
    logger.debug("rows: %r, columns: %r", rows, columns)
    import matplotlib.gridspec as gridspec
    column_scale = 1
    row_scale = 1
    row_offset = 0
    if facets > 1:
        if colorbar_location == "per_row":
            column_scale = 4
            gs = gridspec.GridSpec(rows, columns * column_scale + 1)
        elif colorbar_location == "per_column":
            row_offset = 1
            row_scale = 4
            gs = gridspec.GridSpec(rows * row_scale + 1, columns)
        else:
            gs = gridspec.GridSpec(rows, columns)
    facet_index = 0
    fs = _expand(f, len(whats))
    colormaps = _expand(colormap, len(whats))

    # row
    for i in range(visual_grid.shape[0]):
        # column
        for j in range(visual_grid.shape[1]):
            if colorbar and colorbar_location == "per_column" and i == 0:
                norm = matplotlib.colors.Normalize(vmins[j], vmaxs[j])
                sm = matplotlib.cm.ScalarMappable(norm, colormaps[j])
                sm.set_array(1)  # make matplotlib happy (strange behavious)
                if facets > 1:
                    ax = pylab.subplot(gs[0, j])
                    colorbar = fig.colorbar(sm, cax=ax, orientation="horizontal")
                else:
                    colorbar = fig.colorbar(sm)
                if "what" in labels:
                    label = labels["what"][j]
                    if facets > 1:
                        colorbar.ax.set_title(label)
                    else:
                        colorbar.ax.set_ylabel(colorbar_label or label)

            if colorbar and colorbar_location == "per_row" and j == 0:
                norm = matplotlib.colors.Normalize(vmins[i], vmaxs[i])
                sm = matplotlib.cm.ScalarMappable(norm, colormaps[i])
                sm.set_array(1)  # make matplotlib happy (strange behavious)
                if facets > 1:
                    ax = pylab.subplot(gs[i, -1])
                    colorbar = fig.colorbar(sm, cax=ax)
                else:
                    colorbar = fig.colorbar(sm)
                label = labels["what"][i]
                colorbar.ax.set_ylabel(colorbar_label or label)

            rgrid = ngrid[i, j] * 1.
            # print rgrid.shape
            for k in range(rgrid.shape[0]):
                for l in range(rgrid.shape[0]):
                    if smooth_post is not None:
                        rgrid[k, l] = vaex.grids.gf(rgrid, smooth_post)
            if visual["what"] == "column":
                what_index = j
            elif visual["what"] == "row":
                what_index = i
            else:
                what_index = 0

            if visual[normalize_axis] == "column":
                normalize_index = j
            elif visual[normalize_axis] == "row":
                normalize_index = i
            else:
                normalize_index = 0
            for r in reduce:
                r = _parse_reduction(r, colormaps[what_index], [])
                rgrid = r(rgrid)

            row = facet_index // facet_columns
            column = facet_index % facet_columns

            if colorbar and colorbar_location == "individual":
                # visual_grid.shape[visual_axes[visual[normalize_axis]]]
                norm = matplotlib.colors.Normalize(vmins[normalize_index], vmaxs[normalize_index])
                sm = matplotlib.cm.ScalarMappable(norm, colormaps[what_index])
                sm.set_array(1)  # make matplotlib happy (strange behavious)
                if facets > 1:
                    ax = pylab.subplot(gs[row, column])
                    colorbar = fig.colorbar(sm, ax=ax)
                else:
                    colorbar = fig.colorbar(sm)
                label = labels["what"][what_index]
                colorbar.ax.set_ylabel(colorbar_label or label)

            if facets > 1:
                ax = pylab.subplot(gs[row_offset + row * row_scale:row_offset + (row + 1) * row_scale, column * column_scale:(column + 1) * column_scale])
            else:
                ax = pylab.gca()
            axes.append(ax)
            logger.debug("rgrid: %r", rgrid.shape)
            plot_rgrid = rgrid
            assert plot_rgrid.shape[1] == 1, "no layers supported yet"
            plot_rgrid = plot_rgrid[:, 0]
            if plot_rgrid.shape[0] > 1:
                plot_rgrid = vaex.image.fade(plot_rgrid[::-1])
            else:
                plot_rgrid = plot_rgrid[0]
            extend = None
            if visual["subspace"] == "row":
                subplot_index = i
            elif visual["subspace"] == "column":
                subplot_index = j
            else:
                subplot_index = 0
            extend = np.array(xlimits[subplot_index][-2:]).flatten()
            #   extend = np.array(xlimits[i]).flatten()
            logger.debug("plot rgrid: %r", plot_rgrid.shape)
            plot_rgrid = np.transpose(plot_rgrid, (1, 0, 2))
            im = ax.imshow(plot_rgrid, extent=extend.tolist(), origin="lower", aspect=aspect, interpolation=interpolation)
            # v1, v2 = values[i], values[i+1]

            def label(index, label, expression):
                if label and _issequence(label):
                    return label[i]
                else:
                    return self.label(expression)
            if visual_reverse["x"] =='x':
                labelsx = labels['x']
                pylab.xlabel(labelsx[subplot_index])
            if visual_reverse["x"] =='x':
                labelsy = labels['y']
                pylab.ylabel(labelsy[subplot_index])
            if visual["z"] in ['row']:
                labelsz = labels['z']
                ax.set_title(labelsz[i])
            if visual["z"] in ['column']:
                labelsz = labels['z']
                ax.set_title(labelsz[j])

            max_labels = 10
            xexpression = xexpressions[subplot_index]
            if self.iscategory(xexpression):
                labels = self.category_labels(xexpression)
                step = max(len(labels) // max_labels, 1)
                pylab.xticks(np.arange(len(labels))[::step], labels[::step], size='small')
            yexpression = yexpressions[subplot_index]
            if self.iscategory(yexpression):
                labels = self.category_labels(yexpression)
                step = max(len(labels) // max_labels, 1)
                pylab.yticks(np.arange(len(labels))[::step], labels[::step], size='small')
            facet_index += 1
    if title:
        fig.suptitle(title, fontsize="x-large")
    if tight_layout:
        if title:
            pylab.tight_layout(rect=[0, 0.03, 1, 0.95])
        else:
            pylab.tight_layout()
    if hardcopy:
        pylab.savefig(hardcopy)
    if show:
        pylab.show()
    if return_extra:
        return im, grid, fgrid, ngrid, rgrid
    else:
        return im
Example #36
0
C, _C, _Cx = {}, {}, {}
for k in days:
    I[k], _I[k], _Ix[k] = {}, {}, {}
    C[k], _C[k], _Cx[k] = {}, {}, {}
    for bl in x[k]:
        C[k][bl] = cov(x[k][bl])
        I[k][bl] = n.identity(C[k][bl].shape[0])
        #C[k][bl] = C[k][bl] + 1*I[k][bl] #C+NI noise
        U, S, V = n.linalg.svd(C[k][bl].conj())  #singular value decomposition
        _C[k][bl] = n.einsum('ij,j,jk', V.T, 1. / S, U.T)
        _I[k][bl] = n.identity(_C[k][bl].shape[0])
        _Cx[k][bl] = n.dot(_C[k][bl], x[k][bl])
        _Ix[k][bl] = x[k][bl].copy()
        if PLOT and True:
            #p.plot(S); p.show()
            p.subplot(311)
            capo.arp.waterfall(x[k][bl], mode='real')
            p.title('Data x')
            p.subplot(323)
            capo.arp.waterfall(C[k][bl])
            p.title('C')
            p.subplot(324)
            p.plot(n.einsum('ij,jk', n.diag(S), V).T.real)
            p.subplot(313)
            capo.arp.waterfall(_Cx[k][bl], mode='real')
            p.title('C^-1 x')
            p.suptitle('%d_%d' % a.miriad.bl2ij(bl) + ' ' + k)
            #p.figure(2); p.plot(n.diag(S))
            p.tight_layout()
            p.show()
Example #37
0
    def plot(self,fn=None,idens=0,functions='all',properties=None,fignumber=1):
        """Plot density statistics against rho_cut for reference (black) and density 0 (red).

        plot(filename,properties=<dict of dicts>)

        Plot various functions of the density cut-off rho_cut. Current
        functions are 'sites', 'volume', 'occupancy', or 'all'.

        Plots can be customized by using the properties dict. To
        change the ylim and add an title to the sites graph, use

           properties = {'sites': {ylim':(0,220),'title':'number of sites'}}

        :Arguments:
        fn            file name for the file; default is scan.pdf. Suffix determines file type.
        idens         number of density plot; the first one is 0 in self.scanarrays[].
        functions     list of function names or 'all'
        properties    dict1 of dicts; keys1: sites, volume, occupancy;
                      keys2: any matplotlib settable property, values2: appropriate values
        fignumber     pylab figure number
        """
        import pylab
        available_functions = ['sites', 'volume', 'occupancy']
        plot_functions = []
        if functions is 'all':
            plot_functions = available_functions
        else:
            for f in functions:
                if f in available_functions:
                    plot_functions.append(f)
                else:
                    raise ValueError('No function '+str(f)+' is available, only '+
                                     str(available_functions))
        props = self.__plot_properties_default.copy()
        if type(properties) is dict:
            for k,userparams in properties.items():
                try:
                    props[k].update(userparams)
                except KeyError:
                    props[k] = userparams

        r = self.scanarrays['reference']
        d = self.scanarrays[idens]

        pylab.figure(fignumber)
        pylab.clf()
        pylab.axes(axisbg='w',frameon=False)

        par = props['sites']
        pylab.subplot(221)
        pylab.plot(r.rho_cut,r.N_equivalence_sites,'ko',label="N_equiv")
        pylab.plot(r.rho_cut,r.N_sites,'kx',label="N_sites")
        pylab.plot(r.rho_cut,d.N_sites,'rx')
        pylab.ylim(par['ylim'])
        #pylab.legend()

        # refine err bar plots
        alpha = 1  # acts on the line not on the errorbar markers
        capsize=0

        par = props['volume']
        pylab.subplot(222)
        pylab.errorbar(r.rho_cut,r.site_volume_avg,yerr=r.site_volume_std,
                       color='k',capsize=capsize,alpha=alpha,label="volume")
        pylab.errorbar(r.rho_cut,d.site_volume_avg,yerr=d.site_volume_std,
                       color='r',capsize=capsize,alpha=alpha,label="volume")
        pylab.ylim(par['ylim'])
        pylab.ylabel(par['ylabel'])
        # [pylab.set(pylab.gca(), k, v) for k,v in par.items()]

        par = props['occupancy']
        pylab.subplot(223)
        pylab.errorbar(r.rho_cut,r.site_occupancy_rho_avg,yerr=r.site_occupancy_rho_std,
                       color='k',capsize=capsize,alpha=alpha)
        pylab.errorbar(r.rho_cut,d.site_occupancy_rho_avg,yerr=d.site_occupancy_rho_std,
                       color='r',capsize=capsize,alpha=alpha)
        #pylab.legend()
        pylab.ylim(par['ylim'])
        pylab.xlabel(par['xlabel'])
        pylab.ylabel(par['ylabel'])

        pylab.savefig(fn)
        msg(1, "Graph saved to %s\n" % fn)

        pylab.close(fignumber)
Example #38
0
                                              noise_independent_smooth=1.5,
                                              noise_common_n=1,
                                              noise_common_std=3)

    # just a little helper
    def get2d(ds):
        return dss[0].a.mapper.reverse(ds)

    import pylab as pl
    pl.clf()
    DS = dsvstack(dss)
    # Sample plots
    for s in [0, 1]:
        ds2 = get2d(dss[0])
        for r in [0, 1]:
            pl.subplot(3, 3, 1 + r + s * 3)
            pl.imshow(ds2[ds2.sa.chunks == r].samples[0],
                      interpolation='nearest')
            pl.ylabel('subj%d' % s)
            pl.xlabel('run1')
        pl.subplot(3, 3, 3 + s * 3)
        pl.imshow(get2d(mean_group_sample(['dissimilarity'
                                           ])(dss[0]).samples)[0],
                  interpolation='nearest')
        pl.xlabel('mean')

    ds = dsvstack(dss)
    ds.a['mapper'] = dss[0].a.mapper
    ds_mean = mean_group_sample(['dissimilarity', 'chunks'])(ds)
    for r in [0, 1]:
        ds_mean_run0 = ds.a.mapper.reverse(ds_mean[ds_mean.chunks == r])
Example #39
0
def allele_plot(filename, normalize=False, alleles=None, generations=None):
    """Plot the alleles from each generation from the individuals file.
    
    This function creates a plot of the individual allele values as they
    change through the generations. It creates three subplots, one for each
    of the best, median, and average individual. The best and median 
    individuals are chosen using the fitness data for each generation. The 
    average individual, on the other hand, is actually an individual created
    by averaging the alleles within a generation. This function requires the 
    pylab library.

    .. note::
    
       This function only works for single-objective problems.

    .. figure:: _static/allele_plot.png
        :alt: Example allele plot
        :align: center
        
        An example image saved from the ``allele_plot`` function.
    
    Arguments:
    
    - *filename* -- the name of the individuals file produced by the file_observer 
    - *normalize* -- Boolean value stating whether allele values should be
      normalized before plotting (default False)
    - *alleles* -- a list of allele index values that should be plotted
      (default None)
    - *generations* -- a list of generation numbers that should be plotted
      (default None)

    If *alleles* is ``None``, then all alleles are plotted. Similarly, if 
    *generations* is ``None``, then all generations are plotted.

    """
    import pylab

    generation_data = []
    reader = csv.reader(open(filename))
    for row in reader:
        g = int(row[0])
        row[3] = row[3].replace('[', '')
        row[-1] = row[-1].replace(']', '')
        individual = [float(r) for r in row[3:]]
        individual.append(float(row[2]))
        try:
            generation_data[g]
        except IndexError:
            generation_data.append([])
        generation_data[g].append(individual)
    for gen in generation_data:
        gen.sort(key=lambda x: x[-1])
        for j, g in enumerate(gen):
            gen[j] = g[:-1]

    best = []
    median = []
    average = []
    for gen in generation_data:
        best.append(gen[0])
        plen = len(gen)
        if plen % 2 == 1:
            med = gen[(plen - 1) // 2]
        else:
            med = []
            for a, b in zip(gen[plen // 2 - 1], gen[plen // 2]):
                med.append(float(a + b) / 2)
        median.append(med)
        avg = [0] * len(gen[0])
        for individual in gen:
            for i, allele in enumerate(individual):
                avg[i] += allele
        for i, a in enumerate(avg):
            avg[i] /= float(len(gen))
        average.append(avg)

    for plot_num, (data, title) in enumerate(
            zip([best, median, average], ["Best", "Median", "Average"])):
        if alleles is None:
            alleles = list(range(len(data[0])))
        if generations is None:
            generations = list(range(len(data)))
        if normalize:
            columns = list(zip(*data))
            max_col = [max(c) for c in columns]
            min_col = [min(c) for c in columns]
            for dat in data:
                for i, d in enumerate(dat):
                    dat[i] = (d - min_col[i]) / float(max_col[i] - min_col[i])
        plot_data = []
        for g in generations:
            plot_data.append([data[g][a] for a in alleles])
        sub = pylab.subplot(3, 1, plot_num + 1)
        pylab.pcolor(pylab.array(plot_data))
        pylab.colorbar()
        step_size = max(len(generations) // 7, 1)
        ytick_locs = list(range(step_size, len(generations), step_size))
        ytick_labs = generations[step_size::step_size]
        pylab.yticks(ytick_locs, ytick_labs)
        pylab.ylabel('Generation')
        if plot_num == 2:
            xtick_locs = list(range(len(alleles)))
            xtick_labs = alleles
            pylab.xticks(xtick_locs, xtick_labs)
            pylab.xlabel('Allele')
        else:
            pylab.setp(sub.get_xticklabels(), visible=False)
        pylab.title(title)
    pylab.show()
Example #40
0
########################################
## Load reconstructed signals
with open("op.pkl", "rb") as f:
    params = pickle.load(f)

y0 = np.array(params["y0"], dtype=np.float64)
theta = np.array(params["theta"], dtype=np.float64)

########################################
## Reconstruct signal
Y0_rec = y0
W_rec, K_rec = convert_theta(len(y0), theta)

Y0_rec, W_rec, K_rec = Y0_rec[:N], W_rec[:N], K_rec[:N, :N]
y_rec = compute_kuramoto(ts, Y0_rec, W_rec, K_rec)

########################################
## Plot for each oscillator
for osc in range(N):
    plt.subplot(N, 2, 2 * osc + 1)
    plt.plot(ts, y_in[osc], 'g')
    plt.plot(ts, y_rec[osc], 'r')
    plt.title("Osc " + str(osc + 1))

    plt.subplot(N, 2, 2 * osc + 2)
    plt.plot(ts, y_in[osc] - y_rec[osc], 'g')
    plt.title("Pointwise diff")

plt.tight_layout()
plt.show()
Example #41
0
if interactivePlot:
    pylab.ion()
    pylab.figure()
    pylab.title('Interactive plot of the FFT vs LPC frequency response')
    pylab.gca().set_ylim([-100, 40])
    pylab.gca().set_autoscale_on(False)

for frame in stream:
    fft = ffter.process(windower.process(frame))
    spec = loudia.magToDb(abs(fft))

    wspec = whitening.process(spec)
    pitch, saliency = pitchSaliency.process(wspec)

    if interactivePlot:
        pylab.subplot(211)
        pylab.hold(False)
        pylab.plot(spec[0, :plotSize])

        pylab.subplot(212)
        pylab.hold(False)
        pylab.plot(result[0, :], label='Noise Suppressed Spectrum')

    specs.append(spec[0, :plotSize])
    wspecs.append(wspec[0, :plotSize])
    pitches.append(pitch)
    saliencies.append(saliency)

if interactivePlot:
    pylab.ioff()
Example #42
0
def regression_gaussian_process_modelselection (n=100, n_test=100, \
  x_range=5, x_range_test=10, noise_var=0.4):

    from modshogun import RealFeatures, RegressionLabels
    from modshogun import GaussianKernel
    from modshogun import GradientModelSelection, ModelSelectionParameters
    from modshogun import GaussianLikelihood, ZeroMean, \
     ExactInferenceMethod, GaussianProcessRegression, GradientCriterion, \
     GradientEvaluation

    # easy regression data: one dimensional noisy sine wave
    X_train = random.rand(1, n) * x_range
    X_test = array([[float(i) / n_test * x_range_test for i in range(n_test)]])

    y_test = sin(X_test)
    y_train = sin(X_train) + random.randn(n) * noise_var

    # shogun representation
    labels = RegressionLabels(y_train[0])
    feats_train = RealFeatures(X_train)
    feats_test = RealFeatures(X_test)

    # GP specification
    kernel = GaussianKernel(10, 0.05)

    mean = ZeroMean()

    likelihood = GaussianLikelihood(0.8)

    inf = ExactInferenceMethod(kernel, feats_train, mean, labels, likelihood)
    inf.set_scale(2.5)

    gp = GaussianProcessRegression(inf)

    means = gp.get_mean_vector(feats_test)
    variances = gp.get_variance_vector(feats_test)

    # plot results
    figure()

    subplot(2, 1, 1)
    title('Initial parameter\'s values')

    plot(X_train[0], y_train[0], 'bx')  # training observations

    plot(X_test[0], y_test[0], 'g-')  # ground truth of test
    plot(X_test[0], means, 'r-')  # mean predictions of test

    fill_between(X_test[0],
                 means - 1.96 * sqrt(variances),
                 means + 1.96 * sqrt(variances),
                 color='grey')

    legend(["training", "ground truth", "mean predictions"])

    # evaluate our inference method for its derivatives
    grad = GradientEvaluation(gp, feats_train, labels, GradientCriterion(),
                              False)
    grad.set_function(inf)

    # handles all of the above structures in memory
    grad_search = GradientModelSelection(grad)

    # search for best parameters
    best_combination = grad_search.select_model(True)

    # outputs all result and information
    best_combination.apply_to_machine(gp)

    means = gp.get_mean_vector(feats_test)
    variances = gp.get_variance_vector(feats_test)

    # plot results
    subplot(2, 1, 2)
    title('Selected by gradient search parameter\'s values')

    plot(X_train[0], y_train[0], 'bx')  # training observations

    plot(X_test[0], y_test[0], 'g-')  # ground truth of test
    plot(X_test[0], means, 'r-')  # mean predictions of test

    fill_between(X_test[0],
                 means - 1.96 * sqrt(variances),
                 means + 1.96 * sqrt(variances),
                 color='grey')

    legend(["training", "ground truth", "mean predictions"])

    show()
Example #43
0
depthFrameData.load(srcVideoPath)

srcVideoPath = join(videoDirectory,firstFile+colorVideoSuffix+videoFilenameExtension)
colorFrameData = VideoFrameData()
colorFrameData.load(srcVideoPath)

i = 0
resultImages = []

depthRetval,depthFrame = depthFrameData.readFrame()
colorRetval,colorFrame = colorFrameData.readFrame()
labelRetval,labelFrame = labelFrameData.readFrame()
skeletonRetval,skeletonFrame = skeletonFrameData.readFrame()
if not depthRetval or not colorRetval or not labelRetval or not skeletonRetval:
    exit

encodedFrame = FrameConverter().encode(depthFrame, colorFrame, labelFrame, skeletonFrame)

plt.subplot(1,2,1), plt.imshow(depthFrame)
plt.subplot(1,2,2), plt.imshow(colorFrame)
plt.show()

argc = len(sys.argv)
if argc > 1:
    host = sys.argv[1]
else:
    host = 'localhost'

client = HandShapeClient(host, port)
client.send_data(encodedFrame)
Example #44
0
    if res_fname.find('NV') > 0:
        x = [i for g in sx for i in g]
        y = [i for g in data[cl] for i in g]
    else:
        x = [
            i for k, g in enumerate(sx)
            if my_groups[k] in ['persistent', 'remission'] for i in g
        ]
        y = [
            i for k, g in enumerate(data[cl])
            if my_groups[k] in ['persistent', 'remission'] for i in g
        ]

    slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
    if not quiet:
        pl.subplot(nrows, ncols, cnt)
        # make the scatterplot first
        pl.plot(x, y, '.b', ms=10)
        line = slope * np.array(x) + intercept
        pl.plot(x, line, 'r-', linewidth=5)
        pl.title('r = %.2f, p < %.2f, cluster %d' % (r_value, p_value, cl + 1))
        pl.xlabel('symptoms')
        pl.ylabel('zscores')
        ax = pl.gca()
        ax.yaxis.labelpad = -5
        pl.axis('tight')
        cnt += 1

        # now do the barplot
        pl.subplot(nrows, ncols, cnt)
        ybars = [np.mean(data[cl][i]) for i in range(len(my_groups))]
Example #45
0
p  #delta=-1 # Extinsion de las presas y luego de los depredadores
x[:, 0] = [a / b * (1 + delta),
           c / d * (1 - delta)]  #fijo condiciones iniciales

#ya podemos integrar de forma muy parecida a una variable

for i in range(len(t) - 1):
    x[:, i + 1] = int_rk4(
        derivada, x[:, i], dt,
        params)  #ahora donde ponía números pongo vectorcitos en las x

#veo como dio

x0 = x[0, :]  #resultados de la primer especio
x1 = x[1, :]  #resultados de la segunda especie

#grafiquemos
plb.subplot(211)
plb.plot(t, x0, label='Presas')  #le pongo leyendas a cada especie
plb.plot(t, x1, label='Deredadores')
plb.xlabel('Tiempo')
plb.ylabel('Población')
plb.title('Modelo Lotka-Volterra')
plb.legend(loc='upper right', fontsize=10)  #le digo dónde poner las leyendas
plb.show(
)  #le digo que muestre las leyendas (sin este comando no van a aparecer)      plb.subplot(121)
plb.subplot(212)
plb.plot(x0, x1)  #le pongo leyendas a cada especie
plb.xlabel('Presas')
plb.ylabel('Depredadores')
all_movs,shifts,corss,_=all_movs.motion_correct(template=None,max_shift_w=45, max_shift_h=45)
#%%
template=np.median(all_movs[:],axis=0)
np.save(base_folder+'template_total',template)
pl.imshow(template,cmap=pl.cm.gray,vmax=120)
#%%
all_movs.play(backend='opencv',gain=10,fr=10)
#%%
t1 = time()
file_res=cb.motion_correct_parallel(fnames,30,template=template,margins_out=0,max_shift_w=45, max_shift_h=45,client=c,remove_blanks=False)
t2=time()-t1
print(t2)
#%%
for f in  file_res:
    with np.load(f+'npz') as fl:
        pl.subplot(2,2,1)
        pl.imshow(fl['template'],cmap=pl.cm.gray,vmin=np.percentile(fl['template'],1),vmax=np.percentile(fl['template'],99))

        pl.subplot(2,2,3)
        pl.plot(fl['xcorrs'])  
        pl.subplot(2,2,2)
        pl.plot(fl['shifts'])
        pl.pause(0.1)
        pl.cla()

print((time() - t1 - 200))
#%%
all_movs=[]
for f in  glob.glob(base_folder+'*.hdf5'):
    print(f)
    with np.load(f[:-4]+'npz') as fl:
Example #47
0
pylab.legend()

#### Dois (ou mais gráficos) em uma janela de figura

O comando `pylab.subplot` permite-lhe organizar vários gráficos dentro da mesma janela. A sintaxe geral é

    subplot(numRows, numCols, plotNum)

Aqui está um exemplo completo de plotagem das curvas seno e cosseno em dois gráficos alinhados um embaixo do outro na mesma janela:

import numpy as N
t = N.arange (0 , 2 * N . pi , 0.01)

import pylab

pylab.subplot(2, 1, 1)
pylab.plot(t, N.sin(t))
pylab.xlabel('t')
pylab.ylabel('sen(t)')

pylab.subplot(2, 1, 2)
pylab.plot(t, N.cos(t))
pylab.xlabel('t')
pylab.ylabel('cos(t)')

import pylab
pylab.figure(1)
pylab.plot(range(10),'o')

pylab.figure(2)
pylab.plot(range(100),'x')
}

benchs = list(json.loads(Path("ultraopt.json").read_text()).keys())

cols = int(np.sqrt(len(benchs)))
rows = int(np.ceil(len(benchs) / cols))
plt.rcParams['font.family'] = 'YaHei Consolas Hybrid'  # 设置字体样式
plt.rcParams['figure.figsize'] = (15, 12)
# plt.suptitle("对比")
# log_scale=True

for log_scale in [True, False]:
    plt.close()
    index = 1
    for bench in benchs:
        plt.subplot(rows, cols, index)
        for name, (color,) in info.items():
            mean_std = json.loads(Path(f"{name}.json").read_text())[bench]
            mean = np.array(mean_std["mean"])
            std = np.array(mean_std["std"])
            iters = range(len(mean))
            if not log_scale:
                plt.fill_between(
                    iters, mean - std, mean + std, alpha=0.1,
                    color=color
                )
            plt.plot(
                iters, mean, color=color, label=name, alpha=0.9
            )
        plt.title(bench)
        index += 1
Example #49
0
    bins = 20
    xlabel = 'Ground state probability'
    ylabel = 'Population'
    xlim = [0, 1]
    ylim = [0, sims]
    labelfsize = 16
    normed = False
    fig = pl.figure(figsize=(8, 9))
    fig.suptitle(r'$N_{qubits} = ' + str(n) + '$,  $T_{final} = ' + str(time) +
                 '$,  $P = 3$,  $G = N-n/2N$',
                 fontsize=14)
    pl.grid(True)

    # Plot the stuff
    pl.subplot(3, 1, 1)
    pl.title('Hebb rule')
    pl.xlim(xlim)
    pl.ylim(ylim)
    pl.hist(x=hebbData, bins=bins, range=(0, 1), normed=normed)

    pl.subplot(3, 1, 2)
    pl.title('Storkey rule')
    pl.ylabel(ylabel, fontweight='bold', fontsize=labelfsize)
    pl.xlim(xlim)
    pl.ylim(ylim)
    pl.hist(x=storkData, bins=bins, range=(0, 1), normed=normed)

    pl.subplot(3, 1, 3)
    pl.title('Projection rule')
    pl.xlabel(xlabel, fontweight='bold', fontsize=labelfsize)
    f.close()

    rot = []
    cofx = []
    cofy = []
    coa = []
    for line in lines:
        words = line.split()
        rot.append(float(words[1]))
        cofx.append(float(words[2]))
        cofy.append(float(words[3]))
        coa.append(float(words[4]))

#	pylab.figure(figsize=(10,14))
    pylab.figure()
    pylab.subplot(221)
    pylab.plot(rot)
    pylab.title("rot")

    pylab.subplot(222)
    pylab.plot(cofx)
    pylab.title('cofx')

    pylab.subplot(223)
    pylab.plot(cofy)
    pylab.title('cofy')

    pylab.subplot(224)
    pylab.plot(coa)
    pylab.title('coa')
Example #51
0
            i = i + 1
            #print(array)

        # ADC_output_code = MCP3201.readADC_LSB()
        # ADC_voltage = MCP3201.convert_to_voltage(ADC_output_code)
        # print("MCP3201 output code (LSB-mode): %d" % ADC_output_code)
        # print("MCP3201 voltage: %0.2f V" % ADC_voltage)
        #print()

        #sleep(0.1)

        write_data(array)
        fft_function()
    except (KeyboardInterrupt):
        print('\n', "Exit on Ctrl-C: Good bye!")

    except:
        print("Other error or exception occurred!")
        raise

    finally:
        fft_signal = fft_function(array)
        plt.subplot(221)
        plt.plot(time, array)

        plt.subplot(222)
        plt.plot(20 * log10(abs(fft_signal)))
        #plt.xlim(0,25)
        plt.show()
        print()
Example #52
0
    def VibrationSpecgram(self, event):
        import pylab as pl

        pipeline = self.visFr.pipeline

        x = pipeline['x']
        y = pipeline['y']

        x -= x.mean()
        y -= y.mean()

        xStd = []
        yStd = []
        window = 80

        for i in range(len(x) - window):
            xStd.append(x[i:i + window].std())
            yStd.append(y[i:i + window].std())

        xDrift = abs(x[0:window].mean() - x[-window - 1:-1].mean())
        yDrift = abs(y[0:window].mean() - y[-window - 1:-1].mean())

        ### create Spectrogram data and axes bounds
        # set parameters for specgram
        NFFT = 1024
        noverlap = int(NFFT * .9)
        Fs = 1 / pipeline.mdh.getEntry('Camera.CycleTime')

        # create specgram data
        pl.figure('Specgram for X and Y', figsize=(16, 6))
        xData, xFreq, xBins, a = pl.specgram(x,
                                             NFFT=NFFT,
                                             noverlap=noverlap,
                                             Fs=Fs,
                                             detrend=pl.detrend_linear)
        yData, yFreq, yBins, b = pl.specgram(y,
                                             NFFT=NFFT,
                                             noverlap=noverlap,
                                             Fs=Fs,
                                             detrend=pl.detrend_linear)
        pl.clf()

        # convert to dB
        xData = 10 * np.log10(xData)
        yData = 10 * np.log10(yData)

        # create upper limit for colorbar
        ColorbarMax = max(25, xData.max(), yData.max())

        ### plot spectrograms
        # X
        pl.subplot(121)
        pl.imshow(xData,
                  aspect='auto',
                  clim=[-15, ColorbarMax],
                  origin='lower',
                  interpolation='nearest',
                  extent=[xBins[0], xBins[-1], xFreq[0], xFreq[-1]])
        pl.title('X')
        pl.ylabel('frequency [Hz]')
        #pl.yticks(freqLabels[0], freqLabels[1])
        pl.xlabel(
            'framebin [s]\nx-drift(min-max): %d nm, vibration-std(80fr): %d nm'
            % (xDrift, np.average(xStd)))
        #pl.xticks(binLabels[0], binLabels[1])
        pl.colorbar()

        # Y
        pl.subplot(122)
        pl.imshow(yData,
                  aspect='auto',
                  clim=[-15, ColorbarMax],
                  origin='lower',
                  interpolation='nearest',
                  extent=[xBins[0], xBins[-1], xFreq[0], xFreq[-1]])
        pl.title('Y')
        pl.ylabel('frequency [Hz]')
        #pl.yticks(freqLabels[0], freqLabels[1])
        pl.xlabel(
            'framebin [s]\ny-drift(min-max): %d nm, vibration-std(80fr): %d nm'
            % (yDrift, np.average(yStd)))
        #pl.xticks(binLabels[0], binLabels[1])
        pl.colorbar()

        # plot x, y vs time to visualize drift
        pl.figure('x- and y-drift visualization')
        pl.plot(pipeline['t'] / Fs, x)
        pl.xlabel('Time [s]')
        pl.plot(pipeline['t'] / Fs, y)
        pl.ylabel('relative position [nm]')
def main(argv=None):
    """script main.

    parses command line options in sys.argv, unless *argv* is given.
    """

    if argv is None:
        argv = sys.argv

    parser = E.OptionParser(
        version=
        "%prog version: $Id: plot_matrix.py 2782 2009-09-10 11:40:29Z andreas $"
    )

    parser.add_option("-c",
                      "--columns",
                      dest="columns",
                      type="string",
                      help="columns to take from table.")

    parser.add_option("-a",
                      "--hardcopy",
                      dest="hardcopy",
                      type="string",
                      help="write hardcopy to file.",
                      metavar="FILE")

    parser.add_option("-f",
                      "--file",
                      dest="input_filename",
                      type="string",
                      help="filename with table data.",
                      metavar="FILE")

    parser.add_option("-p",
                      "--plot",
                      dest="plot",
                      type="string",
                      help="plots to plot.",
                      action="append")

    parser.add_option("-t",
                      "--threshold",
                      dest="threshold",
                      type="float",
                      help="min threshold to use for counting method.")

    parser.add_option("-o",
                      "--colours",
                      dest="colours",
                      type="int",
                      help="column with colour information.")

    parser.add_option("-l",
                      "--plot-labels",
                      dest="labels",
                      type="string",
                      help="column labels for x and y in matched plots.")

    parser.add_option("-e",
                      "--header-names",
                      dest="headers",
                      action="store_true",
                      help="headers are supplied in matrix.")

    parser.add_option("--no-headers",
                      dest="headers",
                      action="store_false",
                      help="headers are not supplied in matrix.")

    parser.add_option("--normalize",
                      dest="normalize",
                      action="store_true",
                      help="normalize matrix.")

    parser.add_option("--palette",
                      dest="palette",
                      type="choice",
                      choices=("rainbow", "gray", "blue-white-red", "autumn",
                               "bone", "cool", "copper", "flag", "gray", "hot",
                               "hsv", "jet", "pink", "prism", "spring",
                               "summer", "winter", "spectral", "RdBu", "RdGy",
                               "BrBG", "BuGn", "Blues", "Greens", "Reds",
                               "Oranges", "Greys"),
                      help="colour palette [default=%Default]")

    parser.add_option("--reverse-palette",
                      dest="reverse_palette",
                      action="store_true",
                      help="reverse the palette [default=%default].")

    parser.add_option("",
                      "--xrange",
                      dest="xrange",
                      type="string",
                      help="xrange.")

    parser.add_option("",
                      "--yrange",
                      dest="yrange",
                      type="string",
                      help="yrange.")

    parser.add_option("",
                      "--zrange",
                      dest="zrange",
                      type="string",
                      help="zrange.")

    parser.add_option("",
                      "--xticks",
                      dest="xticks",
                      type="string",
                      help="xticks.")

    parser.add_option("",
                      "--yticks",
                      dest="yticks",
                      type="string",
                      help="yticks.")

    parser.add_option("--bar-format",
                      dest="bar_format",
                      type="string",
                      help="format for ticks on colourbar.")

    parser.add_option("--title",
                      dest="title",
                      type="string",
                      help="title to use.")

    parser.add_option("--missing-value",
                      dest="missing",
                      type="float",
                      help="value to use for missing data.")

    parser.add_option(
        "--subplots",
        dest="subplots",
        type="string",
        help=
        "split matrix into several subplots. Supply number of rows and columns separated by a comma."
    )

    parser.set_defaults(hardcopy=None,
                        input_filename="-",
                        columns="all",
                        statistics=[],
                        plot=[],
                        threshold=0.0,
                        labels="x,y",
                        colours=None,
                        xrange=None,
                        yrange=None,
                        zrange=None,
                        palette=None,
                        reverse_palette=False,
                        xticks=None,
                        yticks=None,
                        normalize=False,
                        bar_format="%1.1f",
                        headers=True,
                        missing=None,
                        title=None,
                        subplots=None)

    (options, args) = E.start(parser)

    # import matplotlib/pylab. Has to be done here
    # for batch scripts without GUI.
    import matplotlib
    if options.hardcopy:
        matplotlib.use("cairo")
    import pylab

    if len(args) > 0:
        options.input_filename = ",".join(args)

    if options.xticks:
        options.xticks = options.xticks.split(",")
    if options.yticks:
        options.yticks = options.yticks.split(",")

    if options.xrange:
        options.xrange = list(map(float, options.xrange.split(",")))
    if options.yrange:
        options.yrange = list(map(float, options.yrange.split(",")))

    if options.columns != "all":
        options.columns = [int(x) - 1 for x in options.columns.split(",")]

    filenames = options.input_filename.split(",")

    if len(filenames) > 1:
        nsubrows = (len(filenames) / 3) + 1
        nsubcols = 3
    elif options.subplots:
        nsubrows, nsubcols = [int(x) for x in options.subplots.split(",")]
    else:
        nsubrows, nsubcols = 1, 1

    nsubplots = nsubrows * nsubcols

    # Setting up color maps
    if options.palette:
        if options.palette == "gray":
            _gray_data = {
                'red': ((0., 1, 1), (1., 0, 0)),
                'green': ((0., 1, 1), (1., 0, 0)),
                'blue': ((0., 1, 1), (1., 0, 0))
            }

            LUTSIZE = pylab.rcParams['image.lut']
            colors_gray = matplotlib.colors.LinearSegmentedColormap(
                'gray', _gray_data, LUTSIZE)

    plot_id = 0
    for filename in filenames:

        plot_id += 1
        pylab.subplot(nsubrows, nsubcols, plot_id)

        if filename == "-":
            infile = sys.stdin
        else:
            infile = IOTools.open_file(filename, "r")

        matrix, row_headers, col_headers = MatlabTools.readMatrix(
            infile,
            numeric_type=numpy.float32,
            take=options.columns,
            headers=options.headers,
            missing=options.missing)

        if min(matrix.flat) == max(matrix.flat):
            options.stderr.write("matrix is uniform - no plotting done.\n")
            sys.exit(0)

        if options.normalize:
            v = max(matrix.flat)
            matrix = matrix / v

        if options.zrange:
            options.zrange = GetRange(matrix, options.zrange)

        nrows, ncols = matrix.shape

        if options.palette:
            if options.palette == "gray":
                color_scheme = colors_gray
            else:
                if options.reverse_palette:
                    color_scheme = eval("pylab.cm.%s_r" % options.palette)
                else:
                    color_scheme = eval("pylab.cm.%s" % options.palette)
        else:
            color_scheme = None

        if options.zrange:
            vmin, vmax = options.zrange
            matrix[matrix < vmin] = vmin
            matrix[matrix > vmax] = vmax
        else:
            vmin, vmax = None, None

        if options.subplots:

            if nsubcols > 1:
                increment_x = int(float(nrows + 1) / nsubcols)
                increment_y = nrows

                x = 0
                y = 0
                for n in range(nsubplots):
                    pylab.subplot(nsubrows, nsubcols, plot_id)
                    plot_id += 1

                    print(n, "rows=", nsubrows, "cols=", nsubcols, y,
                          y + increment_y, x, x + increment_x)
                    print(matrix[y:y + increment_y, x:x + increment_x].shape)
                    print(matrix.shape)
                    plotMatrix(matrix[y:y + increment_y, x:x + increment_x],
                               color_scheme, row_headers[y:y + increment_y],
                               col_headers[x:x + increment_x], 0, 100, options)

                x += increment_x

            elif nsubrows > 1:
                increment_x = int(float(ncols + 1) / nsubrows)

                x = 0
                for n in range(nsubplots):
                    pylab.subplot(nsubrows, nsubcols, plot_id)
                    plot_id += 1
                    plotMatrix(matrix[0:nrows,
                                      x:x + increment_x], color_scheme,
                               row_headers, col_headers[x:x + increment_x],
                               vmin, vmax, options)

                    x += increment_x
        else:
            plotMatrix(matrix, color_scheme, row_headers, col_headers, vmin,
                       vmax, options)

        if options.xrange:
            pylab.xlim(options.xrange)

        if options.yrange:
            pylab.ylim(options.yrange)

        if options.labels:
            xlabel, ylabel = options.labels.split(",")
            pylab.xlabel(xlabel)
            pylab.ylabel(ylabel)

        if not options.subplots:
            pylab.colorbar(format=options.bar_format)

        if options.title is None or options.title != "":
            pylab.title(filename)

    if options.hardcopy:
        pylab.savefig(os.path.expanduser(options.hardcopy))
    else:
        pylab.show()

    E.stop()
Example #54
0
File: FFT.py Project: 86023/Glab
# coding=utf-8
from matplotlib.font_manager import FontProperties
import pylab as pl
import numpy as np
chinese_font = FontProperties(fname='/usr/share/fonts/truetype/wqy/wqy-microhei.ttc')
sampling_rate = 8000
fft_size = 512
t = np.arange(0, 1.0, 1.0/sampling_rate)
x = np.sin(2*np.pi*156.25*t)  + 2*np.sin(2*np.pi*234.375*t)
xs = x[:fft_size]
xf = np.fft.rfft(xs)/fft_size
freqs = np.linspace(0, sampling_rate/2, fft_size/2+1)
xfp = 20*np.log10(np.clip(np.abs(xf), 1e-20, 1e100))
pl.figure(figsize=(8,4))
pl.subplot(211)
pl.plot(t[:fft_size], xs)
pl.xlabel(u"时间(秒)",fontproperties=chinese_font)
pl.title(u"156.25Hz和234.375Hz的波形和频谱", fontproperties=chinese_font)
pl.subplot(212)
pl.plot(freqs, xfp)
pl.xlabel(u"频率(Hz)",fontproperties=chinese_font)
pl.subplots_adjust(hspace=0.4)
pl.show()
Example #55
0
plt.title(' Расстояние от центра Земли от времени', size=15)
plot(list(m1[:, 1] / 1000), list(m1[:, 0]), "-*k", markersize=0.1)
plt.ylabel('Расстояние, км ')
plt.xlabel('Время, 10^3 c')
plt.grid()
show()

plt.title(' Cкорость от времени', size=15)
plot(list(m2[:, 1] / 1000), list(m2[:, 0]), "-*k", markersize=0.1)
plt.ylabel('Скорость, км/с ')
plt.xlabel('Время, 10^3 с')
plt.grid()
show()

i = 0
while i < 460000:
    pylab.subplot(131)
    plt.scatter(m[i][0], m[i][1], color='black')
    # y(x)
    pylab.subplot(132)
    plt.scatter(m2[i][1] / 1000, m2[i][0], color='black')
    # r(t)
    pylab.subplot(133)
    plt.scatter(m1[i][1] / 1000, m1[i][0], color='black')
    # v(t)
    i += 1000
    plt.pause(0.001)
plt.pause(10)
output.close
output2.close
output3.close
Example #56
0
            splits=splits,
            num_splits_to_process=num_splits_to_process,
            num_iter=num_iter,
            template=new_templ,
            shifts_opencv=shifts_opencv,
            save_movie=save_movie)
        new_templ = total_template_wls

    if len(num_splits_to_process_list) > 1:
        num_splits_to_process_list = [None]

    total_shifts_els.append([x_shifts_els, y_shifts_els])
    templates_all_els.append(total_template_wls)

#%%
pl.subplot(2, 1, 1)
pl.plot(np.concatenate([shfts[0] for shfts in total_shifts_els], 0))
pl.subplot(2, 1, 2)
pl.plot(np.concatenate([shfts[1] for shfts in total_shifts_els], 0))
#%%
border_to_0 = np.ceil(np.max(np.array(total_shifts_els))).astype(np.int)
#%%
fnames_map = [os.path.abspath(flfl) for flfl in glob.glob('*.mmap')]
fnames_map.sort()
print(fnames_map)

#%%
# add_to_movie=np.nanmin(templates_rig)+1# the movie must be positive!!!
downsample_factor = 1  # use .2 or .1 if file is large and you want a quick answer
idx_xy = None
base_name = 'Yr'
Example #57
0
    pos = util.get_realdata(True)
    neg = util.get_realdata(False)
    traindatList[i] = concatenate((pos, neg), axis=1)
    trainfeatList[i] = util.get_realfeatures(pos, neg)
    trainlabsList[i] = util.get_labels(True)
    trainlabList[i] = util.get_labels()
    kernelList[i] = GaussianKernel(trainfeatList[i], trainfeatList[i], width)
    svmList[i] = LibSVM(10, kernelList[i], trainlabList[i])

for i in range(num_svms):
    print "Training svm nr. %d" % (i)
    currentSVM = svmList[i]
    currentSVM.train()
    print currentSVM.get_num_support_vectors()
    print "Done."
    x, y, z = util.compute_output_plot_isolines(currentSVM, kernelList[i],
                                                trainfeatList[i])
    subplot(num_svms / 2, 2, i + 1)
    pcolor(x, y, z, shading='interp')
    contour(x, y, z, linewidths=1, colors='black', hold=True)
    scatter(traindatList[i][0, :],
            traindatList[i][1, :],
            s=20,
            marker='o',
            c=trainlabsList[i],
            hold=True)
    axis('tight')

connect('key_press_event', util.quit)
show()
Example #58
0
pylab.plot(range(training_epochs), d_da3)
pylab.xlabel('iterations')
pylab.ylabel('cross-entropy')
pylab.title('Cost Entropy of Denoising Auto-encoder 3')
pylab.savefig(method + '_figure_cross_entropyda3.png')
pylab.show()

H3_new, Z_2 = test_da3(H2_new)

pylab.figure()
# Plots of first 100 weights in encoding layer 1
w1 = W1.get_value()
pylab.figure()
pylab.gray()
for i in range(100):
    pylab.subplot(10, 10, i + 1)
    pylab.axis('off')
    pylab.imshow(w1[:, i].reshape(28, 28))
pylab.savefig(method + '_figure_w1.png')

# Plots of first 100 weights in encoding layer 2
w2 = W2.get_value()
pylab.figure()
pylab.gray()
for i in range(100):
    pylab.subplot(10, 10, i + 1)
    pylab.axis('off')
    pylab.imshow(w2[:, i].reshape(30, 30))
pylab.savefig(method + '_figure_w2.png')

# Plots of first 100 weights in encoding layer 3
Example #59
0
#!/usr/bin/env python

from tabulate_like import *
import pylab, cPickle, scipy
print "starting"
G = cPickle.load(open('grids/combined.pickle'))

if False:
    for i, gf in enumerate(G.grid):
        pylab.figure()
        pylab.subplot(1, 2, 1)
        pylab.imshow(gf[0], extent=(-1, 1, -1, 1))
        pylab.colorbar()
        pylab.subplot(1, 2, 2)
        pylab.imshow(gf[0] - G.grid[0][0], extent=(-1, 1, -1, 1))
        pylab.colorbar()
        pylab.savefig('fig%i.pdf' % (i))

diffB = (G.grid[3][0] - G.grid[0][0])
#d2=(G.grid[3][0]+G.grid[4][0]-2*G.grid[0][0])/0.02
diffA = (G.grid[2][0] - G.grid[0][0])
s = 0.005
d1 = (8 * diffA - diffB) / (6 * s)
d3 = (diffB - 2 * diffA) / (6 * s**3)

pylab.figure()

pylab.subplot(2, 2, 1)
pylab.imshow(d1, extent=(-1, 1, -1, 1))
pylab.colorbar()
pylab.subplot(2, 2, 2)
Example #60
0
    r, g, b
])  # Reconstruction image format RGB       ou np.concatenate((b,g,r),axis=1)
imgRGB = cv2.cvtColor(
    img_C, cv2.COLOR_BGR2RGB
)  # Passage BGR --> RGB via cv2.cvtColor (non utilisé par la suite)

# 2.1.1 Affichage image via opencv --> affichage format BGR
cv2.namedWindow("mon image BGR",
                cv2.WINDOW_NORMAL)  # Pour dimensionner la fenetre d'affichage
cv2.imshow("mon image BGR", img_C)
cv2.namedWindow("mon image RGB", cv2.WINDOW_NORMAL)
cv2.imshow("mon image RGB", img_C2)

# Affichage image via plt  --> affichage format RGB
plt.figure()
plt.subplot(121)
plt.imshow(img_C)
plt.xticks([]), plt.yticks([])
plt.title(" Mon image BGR")
plt.subplot(122)
plt.imshow(img_C2)
plt.xticks([]), plt.yticks([])
plt.title(" Mon image RGB")
plt.show()

# 2.1.2 Affichage des trois canaux séparéments
plt.figure()
plt.subplot(131)
plt.imshow(b, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.title(" Mon image B")