示例#1
0
    def quiver(self, sparsity=None, scale=None):
        var = self.vars[0]
        mesh = var.getMesh()

        if isinstance(var, FaceVariable):
            N = mesh._getNumberOfFaces() 
            V = mesh._getFaceAreas()
            X, Y = mesh.getFaceCenters()
        elif isinstance(var, CellVariable):
            N = mesh.getNumberOfCells() 
            V = mesh.getCellVolumes()
            X, Y = mesh.getCellCenters()

        if sparsity is not None and N > sparsity:
            self.indices = numerix.random.rand(N) * V
            self.indices = self.indices.argsort()[-sparsity:]
        else:
            self.indices = numerix.arange(N)

        X = numerix.take(X, self.indices)
        Y = numerix.take(Y, self.indices)
        
        U = V = numerix.ones(X.shape)
        
        import pylab
        
        pylab.ion()
        pylab.cla()
        self._quiver = pylab.quiver(X, Y, U, V, scale=scale)
        pylab.ioff()
示例#2
0
文件: kepprf.py 项目: KeplerGO/PyKE
def cmap_plot(cmdLine):

    pylab.figure(figsize=[5,10])
    a=outer(ones(10),arange(0,1,0.01))
    subplots_adjust(top=0.99,bottom=0.00,left=0.01,right=0.8)
    maps=[m for m in cm.datad if not m.endswith("_r")]
    maps.sort()
    l=len(maps)+1
    for i, m in enumerate(maps):
        print m
        subplot(l,1,i+1)
        pylab.setp(pylab.gca(),xticklabels=[],xticks=[],yticklabels=[],yticks=[])
        imshow(a,aspect='auto',cmap=get_cmap(m),origin="lower")
        pylab.text(100.85,0.5,m,fontsize=10)

# render plot

    if cmdLine: 
        pylab.show(block=True)
    else: 
        pylab.ion()
        pylab.plot([])
        pylab.ioff()
	
    status = 1
    return status
示例#3
0
def plot_spectrum():
    #get the data...    
    a_0=struct.unpack('>1024l',fpga.read('even',1024*4,0))
    a_1=struct.unpack('>1024l',fpga.read('odd',1024*4,0))

    interleave_a=[]

    for i in range(1024):
        interleave_a.append(a_0[i])
        interleave_a.append(a_1[i])

    pylab.figure(num=1,figsize=(10,10))
    pylab.ioff()
    pylab.plot(interleave_a)
    #pylab.semilogy(interleave_a)
    pylab.title('Integration number %i.'%prev_integration)
    pylab.ylabel('Power (arbitrary units)')
    pylab.grid()
    pylab.xlabel('Channel')
    pylab.xlim(0,2048)
    pylab.ioff()

    pylab.hold(False)
    pylab.show()
    pylab.draw()
示例#4
0
    def prepare(self):
        def setField(name):
            self.xData = data[name].dimensions[0]
            self.yData = data[name]
            pylab.xlabel(data[name].dimensions[0].label)
            pylab.ylabel(data[name].label)

        pylab.ioff()
        pylab.figure()

        data = self.dataContainer
        if self.dataContainer.numberOfColumns() > 2:
            if u"Smoothed Absorption" in data.longnames.keys():
                setField(u"Smoothed Absorption")
            elif u"Absorption" in data.longnames.keys():
                setField(u"Absorption")
            else:
                self.xData = data[u"Wellenlänge[nm]"]
                self.yData = data[u"ScopRaw[counts]"]
                pylab.ylabel("Scop Raw / a.u.")
        else:
            self.xData = self.dataContainer[0]
            self.yData = self.dataContainer[1]
        for i in xrange(len(self.xData.data)):
            self.ordinates.append(self.yData.data[i])
            self.abscissae.append(self.xData.data[i])
        if u"Minima" in data.longnames.keys():
            mins = data[u"Minima"]
            for i in xrange(len(mins.data)):
                self.mins.append(mins.data[i])

        pylab.xlabel("Wavelength $\lambda$ / nm")
        pylab.title(self.dataContainer.longname)
示例#5
0
 def finalize(self):
     """
     Wraps up plotting by switching off interactive model and showing the
     plot.
     """
     plt.ioff()
     plt.show()
def plotBkMeasure(bk, ek, vk, figurePath):
    #print bk
    #print ek
    #print vk
    
    k = list(range(len(bk)))
    
    #for i,j in enumerate(bk):
    pylab.ioff()
    pylab.figure()
    pylab.plot(k, bk, '.', label='Bk')

    pylab.plot(k, ek, label='E(Bk)')
    #pylab.plot(k, ek+2*np.sqrt(vk), '-.r', label='limit range')
    #pylab.plot(k, ek-2*np.sqrt(vk), '-.r')
    #for i in range(len(ek)):
    pylab.fill_between(k, ek+4*np.sqrt(vk), ek-4*np.sqrt(vk), facecolor='red', interpolate=True )

    # figure setting
    pylab.xlim(2,k[-1])
    pylab.ylim(0,1.0)
    pylab.legend(loc='upper right')
    pylab.xlabel('Number of Clusters')
    pylab.ylabel('Bk')
    # pylab.title('Bk measure between two algorithm')

    # show result
    pylab.savefig(figurePath, format='svg')
示例#7
0
文件: vpca.py 项目: macabot/mlpm_lab
def hinton(W, maxWeight=None):
    """
    Source: http://wiki.scipy.org/Cookbook/Matplotlib/HintonDiagrams
    Draws a Hinton diagram for visualizing a weight matrix.
    Temporarily disables matplotlib interactive mode if it is on,
    otherwise this takes forever.
    """
    reenable = False
    if pl.isinteractive():
        pl.ioff()
    pl.clf()
    height, width = W.shape
    if not maxWeight:
        maxWeight = 2**np.ceil(np.log(np.max(np.abs(W)))/np.log(2))

    pl.fill(np.array([0,width,width,0]),np.array([0,0,height,height]),'gray')
    pl.axis('off')
    pl.axis('equal')
    for x in xrange(width):
        for y in xrange(height):
            _x = x+1
            _y = y+1
            w = W[y,x]
            if w > 0:
                _blob(_x - 0.5, height - _y + 0.5, min(1,w/maxWeight),'white')
            elif w < 0:
                _blob(_x - 0.5, height - _y + 0.5, min(1,-w/maxWeight),'black')
    if reenable:
        pl.ion()
    pl.show()
示例#8
0
文件: Plot.py 项目: airanmehr/popgen
    def plotGeometry(self,SNP,PCAD,title):
        fig=plt.figure(figsize=self.figsize, dpi=self.dpi);plt.ioff()
        SNP.loc['Reference0']=np.zeros(SNP.shape[1],dtype=int)
        plt.subplot(1,2,1)
        D=pd.DataFrame(None,index=SNP.index,columns=SNP.index)
        for i in SNP.index:
            for j in SNP.index:
                D.loc[i,j]= sum(np.logical_xor(SNP.loc[i],SNP.loc[j]))
        D=D.astype(float)
        im=plt.imshow(D,interpolation='nearest',cmap='Reds')
        plt.gca().xaxis.tick_top()
        x=np.arange(D.index.shape[0])
        plt.yticks(x,map(lambda x: x.replace('mdio','') ,D.index.values))
        plt.xticks(x,map(lambda x: x.replace('mdio','') ,D.columns))
        plt.colorbar(im)
        plt.gca().tick_params(axis='both', which='major', labelsize=10)
        plt.title('Pairwise Hamming Distance',y=1.03)

        
        plt.subplot(1,2,0)
        D=PCAD.astype(float)
        im=plt.imshow(D,interpolation='nearest',cmap='Reds')
        plt.gca().xaxis.tick_top()
        x=np.arange(D.index.shape[0])
        plt.yticks(x,map(lambda x: x.replace('mdio','') ,D.index.values))
        plt.xticks(x,map(lambda x: x.replace('mdio','') ,D.columns))
        plt.colorbar(im)
        plt.gca().tick_params(axis='both', which='major', labelsize=10)
        plt.title('Euclidean Distance in PC3',y=1.03)
        plt.suptitle('Figure {}. {}'.format(self.fignumber, title),fontsize=self.titleSizeSup); self.pdf.savefig(fig);self.fignumber+=1
示例#9
0
def saveHintonDiagram(W, directory):
    maxWeight = None
    #print "Weight: ", W
    """
    Draws a Hinton diagram for visualizing a weight matrix. 
    Temporarily disables matplotlib interactive mode if it is on, 
    otherwise this takes forever.
    """
    reenable = False
    if pylab.isinteractive():
        pylab.ioff()
    pylab.clf()
    height, width = W.shape
    if not maxWeight:
        maxWeight = 2**numpy.ceil(numpy.log(numpy.max(numpy.abs(W)))/numpy.log(2))

    pylab.fill(numpy.array([0,width,width,0]),numpy.array([0,0,height,height]),'gray')
    pylab.axis('off')
    pylab.axis('equal')
    for x in xrange(width):
        for y in xrange(height):
            _x = x+1
            _y = y+1
            w = W[y,x]
            if w > 0:
                _blob(_x - 0.5, height - _y + 0.5, min(1,w/maxWeight),'white')
            elif w < 0:
                _blob(_x - 0.5, height - _y + 0.5, min(1,-w/maxWeight),'black')
    if reenable:
        pylab.ion()
    #pylab.show()
    pylab.savefig(directory)
示例#10
0
def init_plots():
    try: matplotlib
    except NameError: import matplotlib
    '''
    Sets plotting defaults to make things pretty
    '''
    pylab.ioff()
    matplotlib.rcParams['lines.markeredgewidth'] = .001
    matplotlib.rcParams['lines.linewidth']=2
    matplotlib.rcParams['patch.edgecolor']='grey'
    matplotlib.rcParams['font.size']=15.0
    matplotlib.rcParams['figure.figsize']=14,12
    matplotlib.rcParams['figure.subplot.left']=.1
    matplotlib.rcParams['figure.subplot.right']=.9
    matplotlib.rcParams['figure.subplot.top']=.92
    matplotlib.rcParams['figure.subplot.bottom']=.1
    matplotlib.rcParams['figure.subplot.wspace']=.2
    matplotlib.rcParams['figure.subplot.hspace']=.2
    matplotlib.rcParams['figure.facecolor']='white'
    matplotlib.rcParams['axes.facecolor']='white'
    matplotlib.rcParams['axes.edgecolor']='black'
    matplotlib.rcParams['axes.linewidth']=1
    matplotlib.rcParams['axes.grid']=False
    matplotlib.rcParams['xtick.major.size']=7
    matplotlib.rcParams['ytick.major.size']=7
    matplotlib.rcParams['xtick.minor.size']=4
    matplotlib.rcParams['ytick.minor.size']=4
示例#11
0
文件: Plot.py 项目: airanmehr/popgen
 def plotSpectrum(self,spectrum,title):
     fig=plt.figure(figsize=self.figsize, dpi=self.dpi);plt.ioff()
     index, bar_width = spectrum.index.values,0.2
     for i in range(spectrum.shape[1]):
         plt.bar(index + i*bar_width, spectrum.icol(i).values, bar_width, color=mpl.cm.jet(1.*i/spectrum.shape[1]), label=spectrum.columns[i])
     plt.xlabel('Allele') ;plt.xticks(index + 3*bar_width, index) ;plt.legend();
     plt.title('Figure {}. {}'.format(self.fignumber, title),fontsize=self.titleSize); self.pdf.savefig(fig);self.fignumber+=1
示例#12
0
def chart(idx, a, b, label, FILE):
    pylab.ioff()
    fig_width_pt = 350 					     # Get this from LaTeX using \showthe\columnwidth
    inches_per_pt = 1.0/72.27                # Convert pt to inch
    golden_mean = ((5**0.5)-1.0)/2.0         # Aesthetic ratio
    fig_width = fig_width_pt*inches_per_pt   # width in inches   
    fig_height = fig_width*golden_mean       # height in inches
    fig_size =  [fig_width*0.42,fig_height]

    params = { 'backend': 'ps',
           'axes.labelsize': 10,
           'text.fontsize': 10,
           'legend.fontsize': 10,
           'xtick.labelsize': 8,
           'ytick.labelsize': 8,
           'text.usetex': True,
           'figure.figsize': fig_size }

    pylab.rcParams.update(params)

    home = '/home/nealbob'
    folder = '/Dropbox/Thesis/IMG/chapter3/'
    img_ext = '.pdf'

    pylab.figure()
    pylab.boxplot(idx, whis=100)
    pylab.ylim(a, b)
    #pylab.ylabel(label)
    pylab.tick_params(axis='x', which = 'both', labelbottom='off')
    pylab.savefig(home + folder + FILE + img_ext)
    pylab.show()
示例#13
0
文件: tools.py 项目: matwid/ElecSus
def plotOutput(X,Y,spectrumLabel,ydata,PBool=True,ydataBool=False,residuals=False,path=False):
    """Plots the program outputs for the user"""
    import matplotlib
    if PBool == False:
        matplotlib.use('Agg') #non-interactive backend
    import pylab as P
    P.ioff() #Ensure interactivity mode is off so that graph does not dissapear immediately
    fig = P.figure()
    maxYval = amax(Y)
    minYval = amin(Y)
    DynamicRange = maxYval - minYval
    if not ydataBool:
        P.plot(X,Y,'g', linewidth = 2.0)
        P.xlabel(r'Detuning (GHz)')
        P.ylabel(spectrumLabel)
        P.xlim(X[0],X[-1])
        P.ylim(minYval-0.02*DynamicRange,maxYval+0.02*DynamicRange)
    else:
        ax1 = fig.add_axes([0.15,0.30,0.75,0.65])
        ax1.plot(X,ydata,'k')
        ax1.plot(X,Y,'r--', linewidth=1.8)
        ax1.set_xlim(X[0],X[-1])
        ax1.set_ylim(minYval-0.02*DynamicRange,maxYval+0.02*DynamicRange)
        ax1.set_xticklabels([])
        P.ylabel(spectrumLabel)
        ax2 = fig.add_axes([0.15,0.10,0.75,0.15])
        ax2.plot(X,residuals*100.0,'k')
        ax2.set_xlim(X[0],X[-1])
        ax2.axhline(color='r', linestyle = '--', linewidth=1.8)
        P.xlabel(r'Detuning (GHz)')
        P.ylabel(r'Residuals $(\times 100)$')
    if path:
        P.savefig(path)
    if PBool:
        P.show()
def train_kmeans(featurelearndata):
    Rinit = numpy.random.permutation(numhid)
    W = featurelearndata[Rinit]
    for epoch in range(10):
        W = online_kmeans.kmeans(featurelearndata, numhid, Winit=W, numepochs=10, learningrate=0.1*0.8**epoch)
        W_ = numpy.dot(pca_forward,W.T).T.reshape(numhid, patchsize, patchsize, numchannels)
        dispims_color.dispims_color(W_)
        pylab.ion()
        pylab.draw()
        pylab.show()
    pylab.ioff()
    print "done"
    allbigramfeatures = []
    print "xxxxx", 
    for i, image in enumerate(allims):
        print "\b\b\b\b\b\b{0:5d}".format(i), 
        image = image.reshape(numchannels, inputdim, inputdim).transpose(1,2,0)
        prange = numpy.arange(patchsize/2, inputdim-patchsize/2)
        meshg = numpy.meshgrid(prange, prange)    
        keypoints = numpy.array([c.flatten() for c in meshg]).T
        patches = crop_patches_color(image, keypoints, patchsize)
        patches -= patches.mean(1)[:,None]
        patches /= patches.std(1)[:,None] + 0.1 * meanstd
        patches = numpy.dot(patches, pca_backward.T).astype("float32")
        if pooling==1:
            allbigramfeatures.append(online_kmeans.assign_triangle(patches, W).mean(0).astype("float32"))
        elif pooling==2:
            quadrants = numpy.array([int(str(int(a[0]>=inputdim/2))+str(int(a[1]>=inputdim/2)), 2) for a in keypoints])
            features = online_kmeans.assign_triangle(patches, W).astype("float32")
            allbigramfeatures.append(numpy.array([(features * (quadrants==i)[:,None]).mean(0) for i in range(4)]).reshape(4*numhid))    
    return allbigramfeatures
def hinton(W, maxWeight=None):
    """
    Draws a Hinton diagram for visualizing a weight matrix. 
    Temporarily disables matplotlib interactive mode if it is on, 
    otherwise this takes forever.
    """
    reenable = False
    if P.isinteractive():
        P.ioff()
    P.clf()
    height, width = W.shape
    if not maxWeight:
        maxWeight = 2**N.ceil(N.log(N.max(N.abs(W)))/N.log(2))

    P.fill(N.array([0,width,width,0]),N.array([0,0,height,height]),'gray')
    P.axis('off')
    P.axis('equal')
    for x in xrange(width):
        for y in xrange(height):
            _x = x+1
            _y = y+1
            w = W[y,x]
            if w > 0:
                _blob(_x - 0.5, height - _y + 0.5, min(1,w/maxWeight),'white')
            elif w < 0:
                _blob(_x - 0.5, height - _y + 0.5, min(1,-w/maxWeight),'black')
    if reenable:
        P.ion()
    P.show()
示例#16
0
文件: util2.py 项目: ChrisYang/CRFdet
def drawDef(dfeat,dy,dx,mindef=0.001,distr="father"):
    """
        auxiliary funtion to draw recursive levels of deformation
    """
    from matplotlib.patches import Ellipse
    pylab.ioff()
    if distr=="father":
        py=[0,0,2,2];px=[0,2,0,2]
    if distr=="child":
        py=[0,1,1,2];px=[1,2,0,1]
    ordy=[0,0,1,1];ordx=[0,1,0,1]
    x1=-0.5+dx;x2=2.5+dx
    y1=-0.5+dy;y2=2.5+dy
    if distr=="father":       
        pylab.fill([x1,x1,x2,x2,x1],[y1,y2,y2,y1,y1],"r", alpha=0.15, edgecolor="b",lw=1)    
    for l in range(len(py)):
        aux=dfeat[ordy[l],ordx[l],:].clip(-1,-mindef)
        wh=numpy.exp(-mindef/aux[0])/numpy.exp(1);hh=numpy.exp(-mindef/aux[1])/numpy.exp(1)
        e=Ellipse(xy=[(px[l]+dx),(py[l]+dy)], width=wh, height=hh, alpha=0.35)
        x1=-0.75+dx+px[l];x2=0.75+dx+px[l]
        y1=-0.76+dy+py[l];y2=0.75+dy+py[l]
        col=numpy.array([wh*hh]*3).clip(0,1)
        if distr=="father":
            col[0]=0       
        e.set_facecolor(col)
        pylab.gca().add_artist(e)
        if distr=="father":       
            pylab.fill([x1,x1,x2,x2,x1],[y1,y2,y2,y1,y1],"b", alpha=0.15, edgecolor="b",lw=1)            
示例#17
0
def profile():

    pylab.figure()
    pylab.ion()

    for i, ccol in enumerate(chart):
        R, G, B, tX, tY, tZ = ccol
        tx, ty = toxyY(tX, tY, tZ)

        setAndroidColor(R, G, B)

        X, Y, Z, x, y = getXYZxy()

        print i, len(chart)
        print R, G, B, tX, tY, tZ, X, Y, Z

        pylab.plot(tx, ty, "go")
        pylab.plot(x, y, "rx")
        pylab.xlim([0, 0.8])
        pylab.ylim([0, 0.9])
        pylab.axis("scaled")
        pylab.draw()
        pylab.show()

    pylab.ioff()
    pylab.show()
示例#18
0
  def update(self):
    if callable(self.pre):
      self.pre()
    _p.ioff()
    self.lines=[]
    self.legends=[]
#    self.figure.lines=[]
#    self.figure.patches=[]
#    self.figure.texts=[]
#    self.figure.images = []
    self.figure.legends = []

    if self.lattice:
      self.lattice.patches=[]
      self._lattice(['k0l','kn0l','angle'],"#a0ffa0",'Bend h')
      self._lattice(['ks0l'],"#ffa0a0",'Bend v')
      self._lattice(['kn1l','k1l'],"#a0a0ff",'Quad')
      self._lattice(['hkick'],"#e0a0e0",'Kick h')
      self._lattice(['vkick'],"#a0e0e0",'Kick v')
    if self.left:
      self.left.lines=[]
      for i in self.yl:
        self._column(i,self.left,self.color[i])
    if self.right:
      self.right.lines=[]
      for i in self.yr:
        self._column(i,self.right,self.color[i])
    _p.xlabel(_mylbl(axlabel,self.x))
    self.figure.gca().set_xlim(min(self.xaxis[self.idx]),max(self.xaxis[self.idx]))
    _p.figlegend(self.lines,self.legends,'upper right')
    _p.grid(True)
#    self.figure.canvas.mpl_connect('button_release_event',self.button_press)
#    self.figure.canvas.mpl_connect('pick_event',self.pick)
    _p.ion()
    _p.draw()
示例#19
0
def create_report_var(name, mean, cov, inf, minimum=None, maximum=None):

    report = Node(id=name)
    report.data("covariance", cov)
    report.data("information", inf)
    node_mean = report.data("mean", mean)

    with node_mean.data_file("bounds", "image/png") as filename:
        e = 3 * sqrt(cov.diagonal())
        pylab.ioff()
        f = pylab.figure()
        x = range(len(mean))
        pylab.errorbar(x, mean, yerr=e)
        if minimum is not None:
            pylab.plot(x, minimum, "b-")
        if maximum is not None:
            pylab.plot(x, maximum, "b-")
        pylab.savefig(filename)
        pylab.close(f)

    f = report.figure(name)
    f.sub("mean/bounds", caption="Mean")
    f.sub("covariance", caption="Covariance", display="posneg")
    f.sub("information", caption="Information", display="posneg")

    return report
示例#20
0
文件: orient.py 项目: bkurtz/PyGRBL
def scan():
    pylab.ion()
    pylab.figure(1)
    
    
    with Communicate('', None, debug=True) as serial:
        serial.timeout = 0.0001
        camera = Camera()
        camera.setupmeasure()
        
        controller = Controller(serial)
        controller.setupscan()
        
        out = []
        for x,y in controller.scan():
            camera.update()
            camera.interact()
            
            z = camera.measure()
            out.append([x,y,z])
            
            if camera.status == 'quit':
                break
            camera.show()
            
            if len(out) > 0:
                pylab.cla()
                tmp = zip(*out)
                sc = pylab.scatter(tmp[0],tmp[1],s=tmp[2], c=tmp[2], vmin=0, vmax=400)
                print '{: 8.3f} {: 8.3f} {: 8.3f}'.format(x,y,z)
            
        pylab.ioff()
        pylab.show()
示例#21
0
    def hinton(W, max_weight=None, names=(names, worst_names)):
        """
        Draws a Hinton diagram for visualizing a weight matrix.
        Temporarily disables matplotlib interactive mode if it is on,
        otherwise this takes forever.
        """
        reenable = False
        if P.isinteractive():
            P.ioff()
        P.clf()
        height, width = W.shape
        if not max_weight:
            max_weight = 2**np.ceil(np.log(np.max(np.abs(W)))/np.log(2))

        P.fill(np.array([0, width, width, 0]), np.array([0, 0, height, height]), 'gray')
        P.axis('off')
        P.axis('equal')
        cmap = plt.get_cmap('RdYlGn')
        for x in range(width):
            if names:
                plt.text(-0.5, x, names[0][x], fontsize=7, ha='right', va='bottom')
                plt.text(x, height+0.5, names[1][height-x-1], fontsize=7, va='bottom', rotation='vertical', ha='left')
            for y in range(height):
                _x = x+1
                _y = y+1
                w = W[y, x]
                if w > 0:
                    _blob(_x - 0.5, height - _y + 0.5, min(1, w/max_weight), color=cmap(w/max_weight))
                elif w < 0:
                    _blob(_x - 0.5, height - _y + 0.5, min(1, -w/max_weight), 'black')
        if reenable:
            P.ion()
        P.show()
示例#22
0
  def promt(self, x, y):
    p = Plot()
    p.error(x, y, ecolor='0.3')
    p.make()
    pylab.ion()
    p.show()
    pylab.ioff()
    print(' *** RANGE PICKER for "{}":'.format(self.id))
    if RPicker.storage is not None and self.id in RPicker.storage:
      r = RPicker.storage[self.id]
      print('     previously from {:.5g} to {:.5g}'.format(r[0], r[1]))

    xunit = p._xaxis.sprefunit()
    lower = input('     lower limit ({}) = '.format(xunit))
    upper = input('     upper limit ({}) = '.format(xunit))
    print('')

    lower = float(lower)
    upper = float(upper)

    f = Quantity(xunit) / Quantity(unit=x.uvec)
    f = float(f)
    lower *= f
    upper *= f

    if RPicker.storage is not None:
      RPicker.storage[self.id] = (lower, upper)
      print('     stored...')

    return lower, upper
示例#23
0
    def __call__(self,output_fn,init_time=0,final_time=None,**params):
        p=ParamOverrides(self,params)

        if final_time is None:
            final_time=topo.sim.time()

        attrs = p.attrib_names if len(p.attrib_names)>0 else output_fn.attrib_names
        for a in attrs:
            pylab.figure(figsize=(6,4))
            isint=pylab.isinteractive()
            pylab.ioff()
            pylab.grid(True)
            ylabel=p.ylabel
            pylab.ylabel(a+" "+ylabel)
            pylab.xlabel('Iteration Number')

            coords = p.units if len(p.units)>0 else output_fn.units
            for coord in coords:
                y_data=[y for (x,y) in output_fn.values[a][coord]]
                x_data=[x for (x,y) in output_fn.values[a][coord]]
                if p.raw==True:
                    plot_data=zip(x_data,y_data)
                    pylab.save(normalize_path(p.filename+a+'(%.2f, %.2f)' %(coord[0], coord[1])),plot_data,fmt='%.6f', delimiter=',')


                pylab.plot(x_data,y_data, label='Unit (%.2f, %.2f)' %(coord[0], coord[1]))
                (ymin,ymax)=p.ybounds
                pylab.axis(xmin=init_time,xmax=final_time,ymin=ymin,ymax=ymax)

            if isint: pylab.ion()
            pylab.legend(loc=0)
            p.title=topo.sim.name+': '+a
            p.filename_suffix=a
            self._generate_figure(p)
示例#24
0
文件: Plots.py 项目: airanmehr/bio
    def plotChromosome(DF, fname=None, colors=['black', 'gray'], markerSize=20, ylim=None, show=True, scale=3):
        if not show:
            plt.ioff()
        if 'POS' not in DF.columns:
            df=DF.reset_index()
        else:
            df=DF
        def plotOne(b, d, name):
            a = b.dropna()
            c = d.loc[a.index]
            plt.scatter(a.index, a, s=markerSize, c=c, alpha=0.8, edgecolors='none')
            th = a.mean() + scale * a.std()
            outliers = a[a > th]
            # outliers=findOutliers(a)
            if len(outliers):
                plt.scatter(outliers.index, outliers, s=markerSize, c='r', alpha=0.8, edgecolors='none')
                plt.axhline(th, color='blue')
            plt.axis('tight');
            # plt.xlim(0, a.index[-1]);
            plt.ylabel(name)
            # plt.setp(plt.gca().get_xticklabels(), visible=False)
            if ylim is not None:    plt.ylim(ymin=ylim)

        df['gpos'] = df.POS
        df['color'] = 'gray'
        df.set_index('gpos', inplace=True);
        df.sort_index(inplace=True)
        plt.figure(figsize=(24, 16), dpi=60);
        # plt.subplot(3,1,1)
        df.color='g'
        plotOne(df.icol(1), df.color, 'COMALE')
        df.color='b'
        plotOne(df.icol(2), df.color, 'COMALE')
示例#25
0
    def __call__(self,**params):
        p=ParamOverrides(self,params)
        sheet = p.sheet
        for coordinate in p.coords:
            i_value,j_value=sheet.sheet2matrixidx(coordinate[0],coordinate[1])

            pylab.figure(figsize=(7,7))
            isint=pylab.isinteractive()
            pylab.ioff()

            pylab.ylabel('Response',fontsize='large')
            pylab.xlabel('%s (%s)' % (p.x_axis.capitalize(),p.unit),fontsize='large')
            pylab.title('Sheet %s, coordinate(x,y)=(%0.3f,%0.3f) at time %s' %
                        (sheet.name,coordinate[0],coordinate[1],topo.sim.timestr()))
            p.title='%s: %s Tuning Curve' % (topo.sim.name,p.x_axis.capitalize())

            self.first_curve=True
            for curve_label in sorted(sheet.curve_dict[p.x_axis].keys()):
                x_values,y_values,ticks=self._curve_values(i_value,j_value,sheet.curve_dict[p.x_axis][curve_label])

                x_tick_values,ticks = self._reduce_ticks(ticks)
                labels = [self._format_x_tick_label(x) for x in ticks]
                pylab.xticks(x_tick_values, labels,fontsize='large')
                pylab.yticks(fontsize='large')
                p.plot_type(x_values, y_values, label=curve_label,lw=3.0)
                self.first_curve=False

            if isint: pylab.ion()
            if p.legend: pylab.legend(loc=2)
            self._generate_figure(p)
示例#26
0
def main(argv):
  [ data_file ] = argv
  
  pylab.ioff()

  led_readings = {}

  with open(data_file) as f:
    for line in f:
      record = json.loads(line)

      leds = record['leds']
      if (len(leds) == 1):
        led = leds[0];

        ts = record['timestamp'] / 1000.0
        ts = datetime.fromtimestamp(ts).strftime('%d-%m-%Y %H:%M:%S')

        if led not in led_readings:
          led_readings[led] = {}

        led_readings[led][ts] = record['light']

  for led, readings in sorted(led_readings.items()):
    ts = readings.keys()
    photo_res_1 = [int(photo_res['0']) for photo_res in readings.values()]

    x = range(len(ts))
    pyplot.plot(x, photo_res_1)
    pylab.xticks(x, ts, rotation=45)

    pylab.savefig('/tmp/plots/' + str(led) + '_1.png')
示例#27
0
def arrange_figures(layout=None, screen=2, xgap=10,
                    ygap=30, offset=0, figlist=None):
    """Automatiskt arrangera alla figurer i ett icke overlappande
    monster

       *layout*
            Anvands inte just nu

       *screen* [=2]
            anger vilken skarm man i forsta hand vill ha fonstren pa.

       *xgap*
            Gap i x-led mellan fonster

       *ygap*
            Gap i y-led mellan fonster

       *offset*
            Nar skarmen ar fylld borjar man om fran ovre hogra hornet
            men med en offset i x och y led.

       *figlist*
            Lista med figurnummer som skall arrangeras

    """
    #Hamta information om total skarmbredd over alla anslutna skarmar
    if not is_in_ipython():
        return
#    pylab.show()
    pylab.ioff()
    x0 = 0 + offset
    y0 = 0 + offset
    if screen == 2:
        x0 = (pylab.get_current_fig_manager().window.winfo_screenwidth() +
              offset)
    if figlist is None:
        figlist = sorted([x for x in Gcf.figs.items()])

    x = x0
    y = y0
    maxheight = 0
    while figlist:
        fig = _, f = figlist[0]
        figlist = figlist[1:]
        if fig_fits_w(f, x):
            move_fig(f, x, y)
            x = x + f.window.winfo_width() + xgap
            maxheight = max(maxheight, f.window.winfo_height())
        else:
            x = x0
            y = y + maxheight + ygap
            maxheight = 0
            if fig_fits_h(f, y):
                move_fig(f, x, y)
                x = x + f.window.winfo_width() + xgap
            else:
                arrange_figures(offset=DELTAOFFSET, xgap=xgap, ygap=ygap,
                                screen=screen, figlist=[fig] + figlist)
                break
    pylab.ion()
示例#28
0
def plot_transform(X):
	pylab.ion()
	pylab.figure()
	pylab.imshow(scipy.log(X.T), origin='lower', aspect='auto', interpolation='nearest', norm=matplotlib.colors.Normalize())
	pylab.xlabel('Window index')
	pylab.ylabel('Transform coefficient')
	pylab.ioff()
	def plot(self,pstyle="-"):
		import pylab;
		pfig=XpyFigure(pylab.gcf().number);

		if isstring(pstyle):
			pstyle=XyPlotStyle(pstyle);
		i=0;
		pylab.ioff();
		#print "ioff"
		for k in self.keys():
			p=self[k];
			if self.get('_datobj_title') is not None:
				p['title']=self.get('_datobj_title');
				#print "p type",type(p)
			pstyle['linename']=k;
			if i==0:
				pstyle['showlabels']=True;
			else:
				pstyle['showlabels']=False;
			p.plot(pstyle);
			if i==0:
				pylab.hold(True);
			pstyle.nextplotstyle();
				
			i=i+1;	
		pylab.ion();
		pylab.grid(True);
		stdout( "plotabledataobjtable:plot, done.")
示例#30
0
def plot_graph(gng, iter, fig):
    lines = []
    for e in gng.graph.edges:
        x0, y0 = e.head.data.pos
        c0 = x[int(y0), int(x0)]<0.5 and bg_color or fg_color 
        x1, y1 = e.tail.data.pos
        c1 = x[int(y1), int(x1)]<0.5 and bg_color or fg_color
        # determine edge color
        cline = c0==c1 and c0 or bg_color
        lines.extend(([x0,x1], [y0,y1], cline+'-',
                      [x0,x0], [y0,y0], c0+'.',
                      [x1,x1], [y1,y1], c1+'.'))

    fig.clf()
    pylab.ioff()
    # the order of the axis command is important!
    fig.add_axes([0.01,0.01,0.98,0.98])
    pylab.axis('scaled')
    #pylab.plot(data[:,0], data[:,1], "k.")
    pylab.plot(linewidth=2, ms=14, *lines)
    pylab.axis([0,x.shape[1],0,x.shape[0]])
    pylab.axis('off')
    pylab.draw()
    if save:
        #fig = pylab.gcf()
        fig.frameon = not transparent
        pylab.savefig('animation/img'+('%d'%iter).zfill(4)+'.png',dpi=dpi)
        fig.frameon = True
    pylab.ion()
示例#31
0
def kepsmooth(infile,
              outfile,
              datacol,
              function,
              fscale,
              plot,
              plotlab,
              clobber,
              verbose,
              logfile,
              status,
              cmdLine=False):

    ## startup parameters

    status = 0
    labelsize = 24
    ticksize = 16
    xsize = 18
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

    ## log the call

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = 'KEPSMOOTH -- '
    call += 'infile=' + infile + ' '
    call += 'outfile=' + outfile + ' '
    call += 'datacol=' + str(datacol) + ' '
    call += 'function=' + str(function) + ' '
    call += 'fscale=' + str(fscale) + ' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot=' + plotit + ' '
    call += 'plotlab=' + str(plotlab) + ' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber=' + overwrite + ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose=' + chatter + ' '
    call += 'logfile=' + logfile
    kepmsg.log(logfile, call + '\n', verbose)

    ## start time

    kepmsg.clock('KEPSMOOTH started at', logfile, verbose)

    ## test log file

    logfile = kepmsg.test(logfile)

    ## clobber output file

    if clobber: status = kepio.clobber(outfile, logfile, verbose)
    if kepio.fileexists(outfile):
        message = 'ERROR -- KEPSMOOTH: ' + outfile + ' exists. Use clobber=yes'
        status = kepmsg.err(logfile, message, verbose)

## open input file

    if status == 0:
        instr, status = kepio.openfits(infile, 'readonly', logfile, verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(
            instr, infile, logfile, verbose, status)
        if cadence == 0.0:
            tstart, tstop, ncad, cadence, status = kepio.cadence(
                instr, infile, logfile, verbose, status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

## fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr, file, logfile, verbose)

## read table structure

    if status == 0:
        table, status = kepio.readfitstab(infile, instr[1], logfile, verbose)

# read time and flux columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile, table, logfile, verbose)
    if status == 0:
        flux, status = kepio.readfitscol(infile, instr[1].data, datacol,
                                         logfile, verbose)

# filter input data table

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
            naxis2 = 0
            for i in range(len(table.field(0))):
                if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i])
                        and flux[i] != 0.0):
                    table[naxis2] = table[i]
                    naxis2 += 1
            instr[1].data = table[:naxis2]
            comment = 'NaN cadences removed from data'
            status = kepkey.new('NANCLEAN', True, comment, instr[1], outfile,
                                logfile, verbose)

## read table columns

    if status == 0:
        try:
            intime = instr[1].data.field('barytime')
        except:
            intime, status = kepio.readfitscol(infile, instr[1].data, 'time',
                                               logfile, verbose)
        indata, status = kepio.readfitscol(infile, instr[1].data, datacol,
                                           logfile, verbose)
    if status == 0:
        intime = intime + bjdref
        indata = indata / cadenom

## smooth data

    if status == 0:
        outdata = kepfunc.smooth(indata, fscale / (cadence / 86400), function)

## comment keyword in output file

    if status == 0:
        status = kepkey.history(call, instr[0], outfile, logfile, verbose)

## clean up x-axis unit

    if status == 0:
        intime0 = float(int(tstart / 100) * 100.0)
        if intime0 < 2.4e6: intime0 += 2.4e6
        ptime = intime - intime0
        xlab = 'BJD $-$ %d' % intime0

## clean up y-axis units

    if status == 0:
        pout = indata * 1.0
        pout2 = outdata * 1.0
        nrm = len(str(int(numpy.nanmax(pout)))) - 1
        pout = pout / 10**nrm
        pout2 = pout2 / 10**nrm
        ylab = '10$^%d$ %s' % (nrm, re.sub('_', '-', plotlab))

        ## data limits

        xmin = numpy.nanmin(ptime)
        xmax = numpy.nanmax(ptime)
        ymin = numpy.min(pout)
        ymax = numpy.nanmax(pout)
        xr = xmax - xmin
        yr = ymax - ymin
        ptime = insert(ptime, [0], [ptime[0]])
        ptime = append(ptime, [ptime[-1]])
        pout = insert(pout, [0], [0.0])
        pout = append(pout, 0.0)
        pout2 = insert(pout2, [0], [0.0])
        pout2 = append(pout2, 0.0)

## plot light curve

    if status == 0 and plot:
        try:
            params = {
                'backend': 'png',
                'axes.linewidth': 2.5,
                'axes.labelsize': labelsize,
                'axes.font': 'sans-serif',
                'axes.fontweight': 'bold',
                'text.fontsize': 12,
                'legend.fontsize': 12,
                'xtick.labelsize': ticksize,
                'ytick.labelsize': ticksize
            }
            rcParams.update(params)
        except:
            print('ERROR -- KEPSMOOTH: install latex for scientific plotting')
            status = 1
    if status == 0 and plot:
        pylab.figure(1, figsize=[xsize, ysize])

        # delete any fossil plots in the matplotlib window

        pylab.clf()

        # position axes inside the plotting window

        ax = pylab.subplot(111)
        pylab.subplots_adjust(0.06, 0.1, 0.93, 0.88)

        # force tick labels to be absolute rather than relative

        pylab.gca().xaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(
            pylab.ScalarFormatter(useOffset=False))

        # rotate y labels by 90 deg

        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90)

        pylab.plot(ptime[1:-1],
                   pout[1:-1],
                   color='#ff9900',
                   linestyle='-',
                   linewidth=lwidth)
        fill(ptime, pout, color=fcolor, linewidth=0.0, alpha=falpha)
        pylab.plot(ptime,
                   pout2,
                   color=lcolor,
                   linestyle='-',
                   linewidth=lwidth * 4.0)
        pylab.xlabel(xlab, {'color': 'k'})
        pylab.ylabel(ylab, {'color': 'k'})
        xlim(xmin - xr * 0.01, xmax + xr * 0.01)
        if ymin >= 0.0:
            ylim(ymin - yr * 0.01, ymax + yr * 0.01)
        else:
            ylim(1.0e-10, ymax + yr * 0.01)
        pylab.grid()

# render plot

    if cmdLine:
        pylab.show()
    else:
        pylab.ion()
        pylab.plot([])
        pylab.ioff()

## write output file

    if status == 0:
        for i in range(len(outdata)):
            instr[1].data.field(datacol)[i] = outdata[i]
        instr.writeto(outfile)

## close input file

    if status == 0:
        status = kepio.closefits(instr, logfile, verbose)

## end time

    if (status == 0):
        message = 'KEPSMOOTH completed at'
    else:
        message = '\nKEPSMOOTH aborted at'
    kepmsg.clock(message, logfile, verbose)
     "NP2003": (0.693, 0.703),
     "NP2004": (0.599, 0.591),
     "TD2003": (0.404, 0.398),
     "TD2004": (0.469, 0.461),
 }
 #defaults = {
 #    "HP2003":[0.674],
 #    "HP2004":[0.629],
 #    "NP2003":[0.693],
 #    "NP2004":[0.599],
 #    "TD2003":[0.404],
 #    "TD2004":[0.469],
 #}
 fig = P.figure(figsize=(5.0, 4.0))
 P.clf()
 P.ioff()
 cindex = 0
 lindex = 0
 legenddone = False
 datas = datas[:-1]
 for data in datas:
     if args.plotallusers:
         cindex = 0
         lindex = 0
         P.title(data)
         defaultindex = 0
         for default in defaults[data]:
             seq = defaultlines[defaultindex]
             l = P.axhline(y=default,
                           color="gray",
                           label=defaultlabels[defaultindex])
示例#33
0
    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()

specs = scipy.array(specs)
wspecs = scipy.array(wspecs)
pitches = scipy.array(pitches)[:, 0]
saliencies = scipy.array(saliencies)[:, 0]
frameCount = specs.shape[0] - 1

if plot:
    pylab.figure()
    pylab.subplot(211)
    pylab.plot(pitches[:, 0])

    pylab.subplot(212)
    pylab.plot(saliencies[:, 0])
    pylab.show()
示例#34
0
def orthview(
    map=None,
    fig=None,
    rot=None,
    coord=None,
    unit="",
    xsize=800,
    half_sky=False,
    title="Orthographic view",
    nest=False,
    min=None,
    max=None,
    flip="astro",
    remove_dip=False,
    remove_mono=False,
    gal_cut=0,
    format="%g",
    format2="%g",
    cbar=True,
    cmap=None,
    badcolor="gray",
    bgcolor="white",
    notext=False,
    norm=None,
    hold=False,
    margins=None,
    sub=None,
    return_projected_map=False,
):
    """Plot a healpix map (given as an array) in Orthographic projection.
    
    Parameters
    ----------
    map : float, array-like or None
      An array containing the map.
      If None, will display a blank map, useful for overplotting.
    fig : int or None, optional
      The figure number to use. Default: create a new figure
    rot : scalar or sequence, optional
      Describe the rotation to apply.
      In the form (lon, lat, psi) (unit: degrees) : the point at
      longitude *lon* and latitude *lat* will be at the center. An additional rotation
      of angle *psi* around this direction is applied.
    coord : sequence of character, optional
      Either one of 'G', 'E' or 'C' to describe the coordinate
      system of the map, or a sequence of 2 of these to rotate
      the map from the first to the second coordinate system.
    half_sky : bool, optional
      Plot only one side of the sphere. Default: False
    unit : str, optional
      A text describing the unit of the data. Default: ''
    xsize : int, optional
      The size of the image. Default: 800
    title : str, optional
      The title of the plot. Default: 'Orthographic view'
    nest : bool, optional
      If True, ordering scheme is NESTED. Default: False (RING)
    min : float, optional
      The minimum range value
    max : float, optional
      The maximum range value
    flip : {'astro', 'geo'}, optional
      Defines the convention of projection : 'astro' (default, east towards left, west towards right)
      or 'geo' (east towards roght, west towards left)
    remove_dip : bool, optional
      If :const:`True`, remove the dipole+monopole
    remove_mono : bool, optional
      If :const:`True`, remove the monopole
    gal_cut : float, scalar, optional
      Symmetric galactic cut for the dipole/monopole fit.
      Removes points in latitude range [-gal_cut, +gal_cut]
    format : str, optional
      The format of the scale label. Default: '%g'
    format2 : str, optional
      Format of the pixel value under mouse. Default: '%g'
    cbar : bool, optional
      Display the colorbar. Default: True
    notext : bool, optional
      If True, no text is printed around the map
    norm : {'hist', 'log', None}
      Color normalization, hist= histogram equalized color mapping,
      log= logarithmic color mapping, default: None (linear color mapping)
    cmap : a color map
       The colormap to use (see matplotlib.cm)
    badcolor : str
      Color to use to plot bad values
    bgcolor : str
      Color to use for background
    hold : bool, optional
      If True, replace the current Axes by an OrthographicAxes.
      use this if you want to have multiple maps on the same
      figure. Default: False
    sub : int, scalar or sequence, optional
      Use only a zone of the current figure (same syntax as subplot).
      Default: None
    margins : None or sequence, optional
      Either None, or a sequence (left,bottom,right,top)
      giving the margins on left,bottom,right and top
      of the axes. Values are relative to figure (0-1).
      Default: None
    return_projected_map : bool
      if True returns the projected map in a 2d numpy array
    
    See Also
    --------
    mollview, gnomview, cartview, azeqview
    """
    # Create the figure
    import pylab

    # Ensure that the nside is valid
    nside = pixelfunc.get_nside(map)
    pixelfunc.check_nside(nside, nest=nest)

    if not (hold or sub):
        f = pylab.figure(fig, figsize=(8.5, 5.4))
        extent = (0.02, 0.05, 0.96, 0.9)
    elif hold:
        f = pylab.gcf()
        left, bottom, right, top = np.array(f.gca().get_position()).ravel()
        extent = (left, bottom, right - left, top - bottom)
        f.delaxes(f.gca())
    else:  # using subplot syntax
        f = pylab.gcf()
        if hasattr(sub, "__len__"):
            nrows, ncols, idx = sub
        else:
            nrows, ncols, idx = sub // 100, (sub % 100) // 10, (sub % 10)
        if idx < 1 or idx > ncols * nrows:
            raise ValueError("Wrong values for sub: %d, %d, %d" %
                             (nrows, ncols, idx))
        c, r = (idx - 1) % ncols, (idx - 1) // ncols
        if not margins:
            margins = (0.01, 0.0, 0.0, 0.02)
        extent = (
            c * 1.0 / ncols + margins[0],
            1.0 - (r + 1) * 1.0 / nrows + margins[1],
            1.0 / ncols - margins[2] - margins[0],
            1.0 / nrows - margins[3] - margins[1],
        )
        extent = (
            extent[0] + margins[0],
            extent[1] + margins[1],
            extent[2] - margins[2] - margins[0],
            extent[3] - margins[3] - margins[1],
        )
        # extent = (c*1./ncols, 1.-(r+1)*1./nrows,1./ncols,1./nrows)
    # f=pylab.figure(fig,figsize=(8.5,5.4))

    # Starting to draw : turn interactive off
    wasinteractive = pylab.isinteractive()
    pylab.ioff()
    try:
        if map is None:
            map = np.zeros(12) + np.inf
            cbar = False
        ax = PA.HpxOrthographicAxes(f,
                                    extent,
                                    coord=coord,
                                    rot=rot,
                                    format=format2,
                                    flipconv=flip)
        f.add_axes(ax)
        if remove_dip:
            map = pixelfunc.remove_dipole(map,
                                          gal_cut=gal_cut,
                                          nest=nest,
                                          copy=True,
                                          verbose=True)
        elif remove_mono:
            map = pixelfunc.remove_monopole(map,
                                            gal_cut=gal_cut,
                                            nest=nest,
                                            copy=True,
                                            verbose=True)
        img = ax.projmap(
            map,
            nest=nest,
            xsize=xsize,
            half_sky=half_sky,
            coord=coord,
            vmin=min,
            vmax=max,
            cmap=cmap,
            badcolor=badcolor,
            bgcolor=bgcolor,
            norm=norm,
        )
        if cbar:
            im = ax.get_images()[0]
            b = im.norm.inverse(np.linspace(0, 1, im.cmap.N + 1))
            v = np.linspace(im.norm.vmin, im.norm.vmax, im.cmap.N)
            if matplotlib.__version__ >= "0.91.0":
                cb = f.colorbar(
                    im,
                    ax=ax,
                    orientation="horizontal",
                    shrink=0.5,
                    aspect=25,
                    ticks=PA.BoundaryLocator(),
                    pad=0.05,
                    fraction=0.1,
                    boundaries=b,
                    values=v,
                    format=format,
                )
            else:
                # for older matplotlib versions, no ax kwarg
                cb = f.colorbar(
                    im,
                    orientation="horizontal",
                    shrink=0.5,
                    aspect=25,
                    ticks=PA.BoundaryLocator(),
                    pad=0.05,
                    fraction=0.1,
                    boundaries=b,
                    values=v,
                    format=format,
                )
            cb.solids.set_rasterized(True)
        ax.set_title(title)
        if not notext:
            ax.text(
                0.86,
                0.05,
                ax.proj.coordsysstr,
                fontsize=14,
                fontweight="bold",
                transform=ax.transAxes,
            )
        if cbar:
            cb.ax.text(
                0.5,
                -1.0,
                unit,
                fontsize=14,
                transform=cb.ax.transAxes,
                ha="center",
                va="center",
            )
        f.sca(ax)
    finally:
        pylab.draw()
        if wasinteractive:
            pylab.ion()
            # pylab.show()
    if return_projected_map:
        return img
示例#35
0
def gnomview(
    map=None,
    fig=None,
    rot=None,
    coord=None,
    unit="",
    xsize=200,
    ysize=None,
    reso=1.5,
    title="Gnomonic view",
    nest=False,
    remove_dip=False,
    remove_mono=False,
    gal_cut=0,
    min=None,
    max=None,
    flip="astro",
    format="%.3g",
    cbar=True,
    cmap=None,
    badcolor="gray",
    bgcolor="white",
    norm=None,
    hold=False,
    sub=None,
    margins=None,
    notext=False,
    return_projected_map=False,
    no_plot=False,
):
    """Plot a healpix map (given as an array) in Gnomonic projection.

    Parameters
    ----------
    map : array-like
      The map to project, supports masked maps, see the `ma` function.
      If None, use a blank map, useful for
      overplotting.
    fig : None or int, optional
      A figure number. Default: None= create a new figure
    rot : scalar or sequence, optional
      Describe the rotation to apply.
      In the form (lon, lat, psi) (unit: degrees) : the point at
      longitude *lon* and latitude *lat* will be at the center. An additional rotation
      of angle *psi* around this direction is applied.
    coord : sequence of character, optional
      Either one of 'G', 'E' or 'C' to describe the coordinate
      system of the map, or a sequence of 2 of these to rotate
      the map from the first to the second coordinate system.
    unit : str, optional
      A text describing the unit of the data. Default: ''
    xsize : int, optional
      The size of the image. Default: 200
    ysize : None or int, optional
      The size of the image. Default: None= xsize
    reso : float, optional
      Resolution (in arcmin). Default: 1.5 arcmin
    title : str, optional
      The title of the plot. Default: 'Gnomonic view'
    nest : bool, optional
      If True, ordering scheme is NESTED. Default: False (RING)
    min : float, scalar, optional
      The minimum range value
    max : float, scalar, optional
      The maximum range value
    flip : {'astro', 'geo'}, optional
      Defines the convention of projection : 'astro' (default, east towards left, west towards right)
      or 'geo' (east towards roght, west towards left)
    remove_dip : bool, optional
      If :const:`True`, remove the dipole+monopole
    remove_mono : bool, optional
      If :const:`True`, remove the monopole
    gal_cut : float, scalar, optional
      Symmetric galactic cut for the dipole/monopole fit.
      Removes points in latitude range [-gal_cut, +gal_cut]
    format : str, optional
      The format of the scale label. Default: '%g'
    cmap : a color map
       The colormap to use (see matplotlib.cm)
    badcolor : str
      Color to use to plot bad values
    bgcolor : str
      Color to use for background
    hold : bool, optional
      If True, replace the current Axes by a MollweideAxes.
      use this if you want to have multiple maps on the same
      figure. Default: False
    sub : int or sequence, optional
      Use only a zone of the current figure (same syntax as subplot).
      Default: None
    margins : None or sequence, optional
      Either None, or a sequence (left,bottom,right,top)
      giving the margins on left,bottom,right and top
      of the axes. Values are relative to figure (0-1).
      Default: None
    notext: bool, optional
      If True: do not add resolution info text. Default=False
    return_projected_map : bool, optional
      if True returns the projected map in a 2d numpy array
    no_plot : bool, optional
      if True no figure will be created      

    See Also
    --------
    mollview, cartview, orthview, azeqview
    """
    import pylab

    # Ensure that the nside is valid
    nside = pixelfunc.get_nside(map)
    pixelfunc.check_nside(nside, nest=nest)

    if not (hold or sub):
        f = pylab.figure(fig, figsize=(5.8, 6.4))
        if not margins:
            margins = (0.075, 0.05, 0.075, 0.05)
        extent = (0.0, 0.0, 1.0, 1.0)
    elif hold:
        f = pylab.gcf()
        left, bottom, right, top = np.array(pylab.gca().get_position()).ravel()
        if not margins:
            margins = (0.0, 0.0, 0.0, 0.0)
        extent = (left, bottom, right - left, top - bottom)
        f.delaxes(pylab.gca())
    else:  # using subplot syntax
        f = pylab.gcf()
        if hasattr(sub, "__len__"):
            nrows, ncols, idx = sub
        else:
            nrows, ncols, idx = sub // 100, (sub % 100) // 10, (sub % 10)
        if idx < 1 or idx > ncols * nrows:
            raise ValueError("Wrong values for sub: %d, %d, %d" %
                             (nrows, ncols, idx))
        c, r = (idx - 1) % ncols, (idx - 1) // ncols
        if not margins:
            margins = (0.01, 0.0, 0.0, 0.02)
        extent = (
            c * 1.0 / ncols + margins[0],
            1.0 - (r + 1) * 1.0 / nrows + margins[1],
            1.0 / ncols - margins[2] - margins[0],
            1.0 / nrows - margins[3] - margins[1],
        )
    extent = (
        extent[0] + margins[0],
        extent[1] + margins[1],
        extent[2] - margins[2] - margins[0],
        extent[3] - margins[3] - margins[1],
    )
    # f=pylab.figure(fig,figsize=(5.5,6))

    # Starting to draw : turn interactive off
    wasinteractive = pylab.isinteractive()
    pylab.ioff()
    try:
        if map is None:
            map = np.zeros(12) + np.inf
            cbar = False
        map = pixelfunc.ma_to_array(map)
        ax = PA.HpxGnomonicAxes(f,
                                extent,
                                coord=coord,
                                rot=rot,
                                format=format,
                                flipconv=flip)
        f.add_axes(ax)
        if remove_dip:
            map = pixelfunc.remove_dipole(map,
                                          gal_cut=gal_cut,
                                          nest=nest,
                                          copy=True)
        elif remove_mono:
            map = pixelfunc.remove_monopole(map,
                                            gal_cut=gal_cut,
                                            nest=nest,
                                            copy=True)
        img = ax.projmap(
            map,
            nest=nest,
            coord=coord,
            vmin=min,
            vmax=max,
            xsize=xsize,
            ysize=ysize,
            reso=reso,
            cmap=cmap,
            norm=norm,
            badcolor=badcolor,
            bgcolor=bgcolor,
        )
        if cbar:
            im = ax.get_images()[0]
            b = im.norm.inverse(np.linspace(0, 1, im.cmap.N + 1))
            v = np.linspace(im.norm.vmin, im.norm.vmax, im.cmap.N)
            if matplotlib.__version__ >= "0.91.0":
                cb = f.colorbar(
                    im,
                    ax=ax,
                    orientation="horizontal",
                    shrink=0.5,
                    aspect=25,
                    ticks=PA.BoundaryLocator(),
                    pad=0.08,
                    fraction=0.1,
                    boundaries=b,
                    values=v,
                    format=format,
                )
            else:
                cb = f.colorbar(
                    im,
                    orientation="horizontal",
                    shrink=0.5,
                    aspect=25,
                    ticks=PA.BoundaryLocator(),
                    pad=0.08,
                    fraction=0.1,
                    boundaries=b,
                    values=v,
                    format=format,
                )
            cb.solids.set_rasterized(True)
        ax.set_title(title)
        if not notext:
            ax.text(
                -0.07,
                0.02,
                "%g '/pix,   %dx%d pix" % (
                    ax.proj.arrayinfo["reso"],
                    ax.proj.arrayinfo["xsize"],
                    ax.proj.arrayinfo["ysize"],
                ),
                fontsize=12,
                verticalalignment="bottom",
                transform=ax.transAxes,
                rotation=90,
            )
            ax.text(
                -0.07,
                0.6,
                ax.proj.coordsysstr,
                fontsize=14,
                fontweight="bold",
                rotation=90,
                transform=ax.transAxes,
            )
            lon, lat = np.around(ax.proj.get_center(lonlat=True),
                                 ax._coordprec)
            ax.text(
                0.5,
                -0.03,
                "(%g,%g)" % (lon, lat),
                verticalalignment="center",
                horizontalalignment="center",
                transform=ax.transAxes,
            )
        if cbar:
            cb.ax.text(
                1.05,
                0.30,
                unit,
                fontsize=14,
                fontweight="bold",
                transform=cb.ax.transAxes,
                ha="left",
                va="center",
            )
        f.sca(ax)
    finally:
        pylab.draw()
        if wasinteractive:
            pylab.ion()
            # pylab.show()
        if no_plot:
            pylab.close(f)
            f.clf()
            ax.cla()
    if return_projected_map:
        return img
示例#36
0
Tanto `pdf` quanto `eps` são formatos vetoriais de arquivo, o que significa que podemos ampliar a imagem sem que ela perca a qualidade (as linhas ainda serão nítidas). Formatos de arquivos como `png`, `jpg`, `gif`, `tif` e `bmp` salvam a imagem em formato de mapa de bits (*bitmap*), ou seja, uma matriz de valores de cor, que aparecerá desfocada ou pixelizada ao ser ampliada ou impressa em alta resolução.

### Modo interativo

O Matplotlib pode ser executado de dois modos:

   - não interativo (padrão)
   
   - interativo.
   
No modo não interativo, nenhum plot será exibido até que o comando `show()` tenha sido chamado. Neste modo, o comando `show()` deve ser a última declaração do seu programa. 

No modo interativo, os plots serão imediatamente mostrados depois que o comando de plotagem tiver sido chamado. 

Pode-se ativar o modo interativo com o comando `pylab.ion()` e desativá-lo com o comando `pylab.ioff()`. O comando mágico `%matplotlib` do IPython também habilita o modo interativo.

### Lidando com os detalhes de sua plotagem

O Matplotlib permite-lhe fazer o "ajuste fino" de suas plotagens nos mínimos detalhes. Aqui está um exemplo:

import pylab
import numpy as N

x = N.arange(-3.14, 3.14, 0.01)
y1 = N.sin(x)
y2 = N.cos(x)
pylab.figure(figsize =(5 , 5))
pylab.plot(x, y1, label='sin(x)')
pylab.plot(x, y2, label='cos(x)')
pylab.legend()
示例#37
0
                          verbose=True,
                          weightdecay=0.01)
ticks = arange(-3., 6., 0.2)
X, Y = meshgrid(ticks, ticks)
# need column vectors in dataset, not arrays
griddata = ClassificationDataSet(2, 1, nb_classes=3)
for i in xrange(X.size):
    griddata.addSample([X.ravel()[i], Y.ravel()[i]], [0])
griddata._convertToOneOfMany(
)  # this is still needed to make the fnn feel comfy
for i in range(20):
    trnresult = percentError(trainer.testOnClassData(), trndata['class'])
    tstresult = percentError(trainer.testOnClassData(dataset=tstdata),
                             tstdata['class'])
    out = fnn.activateOnDataset(griddata)
    out = out.argmax(axis=1)  # the highest output activation gives the class
    out = out.reshape(X.shape)
    figure(1)
    ioff()  # interactive graphics off
    clf()  # clear the plot
    hold(True)  # overplot on
    for c in [0, 1, 2]:
        here, _ = where(tstdata['class'] == c)
        plot(tstdata['input'][here, 0], tstdata['input'][here, 1], 'o')
        if out.max() != out.min():  # safety check against flat field
            contourf(X, Y, out)  # plot the contour
    ion()  # interactive graphics on
    draw()  # update the plot
ioff()
show()
示例#38
0
        name = target['name']
        if opts.name is not None and opts.name != name: continue
        try: title = TITLES[name]
        except KeyError: title = name
        distance_modulus = ugali.utils.projector.distanceToDistanceModulus(target['distance'])
        idx = numpy.abs(distance_modulus_array - distance_modulus).argmin()
        print(title, distance_modulus, distance_modulus_array[idx])

        if target['coord'] == 'CEL':
            glon, glat = ugali.utils.projector.celToGal(target['lon'],target['lat'])
        elif target['coord'] == 'GAL':
            glon, glat = target['lon'],target['lat']
        else:
            raise Exception('...')

        plt.ioff()
        fig = plt.figure(figsize=(6,6))
        xsize = 1000
        reso = 60. * 2. * target['radius'] / xsize # Deg to arcmin

        label_kwargs = dict(xy=(0.05,0.05),xycoords='axes fraction', xytext=(0, 0), 
                            textcoords='offset points',ha='left', va='bottom',size=10,
                            bbox={'boxstyle':"round",'fc':'1'}, zorder=10)

        ts_map   = copy.copy(blank); 
        ts_map[pix]   = ts[idx] if ts.ndim > 1 else ts
        
        stellar_map = copy.copy(blank); 
        stellar_map[pix] = stellar[idx] if stellar.ndim > 1 else stellar
        ##frac_map = copy.copy(blank); frac_map[pix] = frac[idx]
        slices = [(ts_map,'TS'),(stellar_map,'Stellar'),(maglim_map,'Maglim (g)'),(density_map,'Density')]
示例#39
0
def MultiBuildBatteryBarChart(report_list,
                              interactive=False,
                              autoscale=False,
                              legenddata=()):
    """Create a bar chart given a list of battery life reports."""
    # same as above.
    import pylab
    from matplotlib.font_manager import FontProperties
    from matplotlib import colors
    if not interactive:
        pylab.ioff()

    N = len(report_list)
    barwidth = 1.0 / N + 0.1

    thefig = pylab.gcf()
    thefig.set_size_inches((11, 6))
    thefig.set_dpi(80.0)
    pylab.subplots_adjust(left=0.07, bottom=0.05, right=0.75, top=0.95)

    max_lifetime = 0.0
    patches = []
    labels = []
    pylab.title("Battery comparison: %s" %
                (report_list[0].metadata.GetStateString(*legenddata)),
                fontsize="small")
    for index, battery_report in enumerate(report_list):
        lifetime = float(battery_report.lifetime.hours)
        max_lifetime = max(lifetime, max_lifetime)
        blue = aid.IF(battery_report.byvoltage, 0.0, 0.5)
        color = colors.rgb2hex(
            aid.IF(battery_report.estimated, ((index * 0.1) % 1.0, 0.5, blue),
                   ((index * 0.1) % 1.0, 1.0, blue)))
        label = "(%s) %s (%.2f h)" % (index + 1,
                                      battery_report.metadata.build.id,
                                      battery_report.lifetime.hours)
        patchlist = pylab.bar(index,
                              lifetime,
                              width=barwidth,
                              color=color,
                              label=label)
        labels.append(label)
        patches.extend(patchlist)
    if autoscale:
        pylab.ylim(0.0, max_lifetime + 20.0)
    else:
        pylab.ylim(0.0, 200.0)
    pylab.xlim(-barwidth, N)
    # labels are position offset by +1.
    pylab.xticks(pylab.arange(N) + (barwidth / 2.0),
                 map(str,
                     pylab.arange(N) + 1),
                 fontsize=10)

    pylab.ylabel('Time (h)')
    if legenddata:
        font = FontProperties(size=9)
        pylab.figlegend(patches, labels, "upper right", prop=font)

    if interactive:
        pylab.show()
    else:
        fname = "batterylife_builds-%s.png" % (
            report_list[0].metadata.timestamp.strftime("%m%d%H%M%S"))
        pylab.savefig(fname, format="png")
        return fname
示例#40
0
def plotgauge(gaugeno, plotdata, verbose=False):
    #==========================================
    """
    Plot all requested plots for a single gauge from the computation.
    The plots are requested by setting attributes of plotdata
    to ClawPlotFigure objects with plot_type="each_gauge".

    """

    if verbose:
        gaugesoln = plotdata.getgauge(gaugeno)
        print '    Plotting gauge %s  at x = %s, y = %s ... '  \
                 % (gaugeno, gaugesoln.location[0], gaugesoln.location[1])

    if plotdata.mode() == 'iplotclaw':
        pylab.ion()

    try:
        plotfigure_dict = plotdata.plotfigure_dict
    except:
        print '*** Error in plotgauge: plotdata missing plotfigure_dict'
        print '*** This should not happen'
        return None

    if len(plotfigure_dict) == 0:
        print '*** Warning in plotgauge: plotdata has empty plotfigure_dict'
        print '*** Apparently no figures to plot'

    # initialize current_data containing data that will be passed
    # to beforegauge, aftergauge, afteraxes commands
    current_data = clawdata.ClawData()
    current_data.add_attribute("user", {})  # for user specified attributes
    # to avoid potential conflicts
    current_data.add_attribute('plotdata', plotdata)
    current_data.add_attribute('gaugeno', gaugeno)

    # call beforegauge if present, which might define additional
    # attributes in current_data or otherwise set up plotting for this
    # gauge.

    beforegauge = getattr(plotdata, 'beforegauge', None)
    if beforegauge:
        if isinstance(beforegauge, str):
            # a string to be executed
            exec(beforegauge)
        else:
            # assume it's a function
            try:
                output = beforegauge(current_data)
                if output: current_data = output
            except:
                print '*** Error in beforegauge ***'
                raise

    # iterate over each single plot that makes up this gauge:
    # -------------------------------------------------------

    if plotdata._mode == 'iplotclaw':
        gaugesoln = plotdata.getgauge(gaugeno)
        print '    Plotting Gauge %s  at x = %s, y = %s ... '  \
                 % (gaugeno, gaugesoln.location[0], gaugesoln.location[1])
        requested_fignos = plotdata.iplotclaw_fignos
    else:
        requested_fignos = plotdata.print_fignos
    plotted_fignos = []

    plotdata = set_show(plotdata)  # set _show attributes for which figures
    # and axes should be shown.

    # loop over figures to appear for this gauge:
    # -------------------------------------------

    for figname in plotdata._fignames:
        plotfigure = plotdata.plotfigure_dict[figname]
        if (not plotfigure._show) or (plotfigure.type != 'each_gauge'):
            continue  # skip to next figure

        figno = plotfigure.figno
        if requested_fignos != 'all':
            if figno not in requested_fignos:
                continue  # skip to next figure

        plotted_fignos.append(figno)

        if not plotfigure.kwargs.has_key('facecolor'):
            # use Clawpack's default bg color (tan)
            plotfigure.kwargs['facecolor'] = '#ffeebb'

        # create figure and set handle:
        plotfigure._handle = pylab.figure(num=figno, **plotfigure.kwargs)

        pylab.ioff()
        if plotfigure.clf_each_gauge:
            pylab.clf()

        try:
            plotaxes_dict = plotfigure.plotaxes_dict
        except:
            print '*** Error in plotgauge: plotdata missing plotaxes_dict'
            print '*** This should not happen'
            return None

        if (len(plotaxes_dict) == 0) or (len(plotfigure._axesnames) == 0):
            print '*** Warning in plotgauge: plotdata has empty plotaxes_dict'
            print '*** Apparently no axes to plot in figno ', figno

        # loop over axes to appear on this figure:
        # ----------------------------------------

        for axesname in plotfigure._axesnames:
            plotaxes = plotaxes_dict[axesname]
            if not plotaxes._show:
                continue  # skip this axes if no items show

            # create the axes:
            axescmd = getattr(plotaxes, 'axescmd', 'subplot(1,1,1)')
            axescmd = 'plotaxes._handle = pylab.%s' % axescmd
            exec(axescmd)
            pylab.hold(True)

            # loop over items:
            # ----------------

            for itemname in plotaxes._itemnames:

                plotitem = plotaxes.plotitem_dict[itemname]
                outdir = plotitem.outdir
                if outdir is None:
                    outdir = plotdata.outdir
                gaugesoln = plotdata.getgauge(gaugeno, outdir)

                current_data.add_attribute('gaugesoln', gaugesoln)
                current_data.add_attribute('q', gaugesoln.q)
                current_data.add_attribute('t', gaugesoln.t)

                if plotitem._show:
                    try:
                        output = plotgauge1(gaugesoln, plotitem, current_data)
                        if output: current_data = output
                        if verbose:
                            print '      Plotted  plotitem ', itemname
                    except:
                        print '*** Error in plotgauge: problem calling plotgauge1'
                        traceback.print_exc()
                        return None

            # end of loop over plotitems

        for itemname in plotaxes._itemnames:
            plotitem = plotaxes.plotitem_dict[itemname]

        pylab.title("%s at gauge %s" % (plotaxes.title, gaugeno))

        # call an afteraxes function if present:
        afteraxes = getattr(plotaxes, 'afteraxes', None)
        if afteraxes:
            if isinstance(afteraxes, str):
                # a string to be executed
                exec(afteraxes)
            else:
                # assume it's a function
                try:
                    current_data.add_attribute("plotaxes", plotaxes)
                    current_data.add_attribute("plotfigure",
                                               plotaxes._plotfigure)
                    output = afteraxes(current_data)
                    if output: current_data = output
                except:
                    print '*** Error in afteraxes ***'
                    raise

        if plotaxes.scaled:
            pylab.axis('scaled')

        # set axes limits:
        if (plotaxes.xlimits is not None) & (type(plotaxes.xlimits)
                                             is not str):
            try:
                pylab.xlim(plotaxes.xlimits[0], plotaxes.xlimits[1])
            except:
                pass  # let axis be set automatically
        if (plotaxes.ylimits is not None) & (type(plotaxes.ylimits)
                                             is not str):
            try:
                pylab.ylim(plotaxes.ylimits[0], plotaxes.ylimits[1])
            except:
                pass  # let axis be set automatically

            # end of loop over plotaxes

        # end of loop over plotfigures

    # call an aftergauge function if present:
    aftergauge = getattr(plotdata, 'aftergauge', None)
    if aftergauge:
        if isinstance(aftergauge, str):
            # a string to be executed
            exec(aftergauge)
        else:
            # assume it's a function
            try:
                output = aftergauge(current_data)
                if output: current_data = output
            except:
                print '*** Error in aftergauge ***'
                raise

    if plotdata.mode() == 'iplotclaw':
        pylab.ion()
    for figno in plotted_fignos:
        pylab.figure(figno)
        pylab.draw()

    if verbose:
        print '    Done with plotgauge for gauge %i' % (gaugeno)

    # print the figure(s) to file(s) if requested:
    if (plotdata.mode() != 'iplotclaw') & plotdata.printfigs:
        # iterate over all figures that are to be printed:
        for figno in plotted_fignos:
            printfig(gaugeno=gaugeno, figno=figno, \
                    format=plotdata.print_format, plotdir=plotdata.plotdir,\
                    verbose=verbose)

    return current_data
示例#41
0
def PlotHistogram(filenames,
                  timemarks=None,
                  columns=None,
                  bins=2000,
                  interactive=False,
                  legenddata=(),
                  autoscale=False):
    import pylab
    from matplotlib.font_manager import FontProperties
    if not interactive:
        pylab.ioff()
    try:
        bins = int(bins)
        brange = (-0.05, 2.5)
        binname = str(bins)
    except ValueError:
        binname = bins.upper()
        if binname.startswith("D"):  # detailed
            bins = (list(aid.frange(0.0, 0.05, 0.0005)) +
                    list(aid.frange(0.05, 1.0, 0.001)) +
                    list(aid.frange(1.0, 2.6, 0.002)))
            brange = (-0.05, 2.5)
        elif binname.startswith("M"):  # medium detail
            bins = (list(aid.frange(0.0, 0.05, 0.0005)) +
                    list(aid.frange(0.05, 1.1, 0.001)))
            brange = (-0.05, 1.0)
        elif binname.startswith("Z"):  # zoom at lower end
            bins = (list(aid.frange(0.0, 0.05, 0.0005)) +
                    list(aid.frange(0.05, 0.51, 0.001)))
            brange = (-0.05, 0.5)
        elif binname.startswith("S"):  # super zoom at lower end
            bins = (list(aid.frange(0.0, 0.05, 0.0005)) +
                    list(aid.frange(0.05, 0.16, 0.001)))
            brange = (-0.05, 0.15)
    ts = None
    unit = None
    for filename in filenames:
        metadata = datafile.DecodeFullPathName(filename)
        if ts is None:
            ts = metadata.timestamp.strftime("%m%d%H%M%S")
        header, measurements = ReadArray(filename)
        if unit is None:
            unit = HEADER_RE.search(header[1]).group(2)
        measurements = NormalizeTime(measurements)
        if timemarks:
            timemarks = TimeMarksGenerator(timemarks)
            measurements = TimeSlice(measurements, timemarks.next(),
                                     timemarks.next())
        datacolumns = measurements.transpose()
        datacols, headers = _GetColumns(datacolumns, header, columns)
        for col, colname, color in itertools.izip(datacols, headers,
                                                  color_cycler):
            label = "%s-%s" % (colname, metadata.GetStateString(*legenddata))
            hist, edges = numpy.histogram(col, bins, brange, False)
            pylab.plot(edges, hist, color=color, label=label)  # plot it

    pylab.axis(xmin=-0.005, ymin=-100)
    pylab.setp(pylab.gcf(), dpi=100, size_inches=(9, 6))
    title = "histogram-%s-%s" % (ts, "bins%s" % binname)
    pylab.title(title, fontsize="x-small")
    if unit is not None:
        pylab.xlabel(unit)
    if legenddata:
        font = FontProperties(size="x-small")
        pylab.legend(prop=font)
    if interactive:
        pylab.show()
        fname = None
    else:
        fname = "%s.%s" % (title, "png")
        pylab.savefig(fname, format="png")
        pylab.cla()
    return fname
示例#42
0
def mlda(data, K, num_itr=epoch_num, save_dir="model", load_dir=None):
    pylab.ion()

    # 尤度のリスト
    liks = []

    M = len(data)  # モダリティの数

    dims = []
    for m in range(M):
        if data[m] is not None:
            dims.append(len(data[m][0]))
            D = len(data[m])  # 物体の数
        else:
            dims.append(0)

    # data内の単語を一列に並べる(計算しやすくするため)
    docs_mdn = [[None for i in range(D)] for m in range(M)]
    topics_mdn = [[None for i in range(D)] for m in range(M)]
    for d in range(D):
        for m in range(M):
            if data[m] is not None:
                docs_mdn[m][d] = conv_to_word_list(data[m][d])
                topics_mdn[m][d] = np.random.randint(0, K, len(
                    docs_mdn[m][d]))  # 各単語にランダムでトピックを割り当てる

    # LDAのパラメータを計算
    n_dz, n_mzw, n_mz = calc_lda_param(docs_mdn, topics_mdn, K, dims)

    # 認識モードでは学習したパラメータを読み込む
    if load_dir:
        n_mzw, n_mz = load_model(load_dir)

    for it in range(num_itr):
        # メイン処理
        for d in range(D):
            for m in range(M):
                if data[m] is None:
                    continue

                N = len(docs_mdn[m][d])  # 物体dのモダリティmに含まれる特徴数
                for n in range(N):
                    w = docs_mdn[m][d][n]  # 特徴のインデックス
                    z = topics_mdn[m][d][n]  # 特徴に割り当てられているカテゴリ

                    # データを取り除きパラメータの更新
                    n_dz[d][z] -= 1

                    if not load_dir:
                        n_mzw[m][z][w] -= 1
                        n_mz[m][z] -= 1

                    # サンプリング
                    z = sample_topic(d, w, n_dz, n_mzw[m], n_mz[m], K, dims[m])

                    # データをサンプリングされたクラスに追加してパラメータを更新
                    topics_mdn[m][d][n] = z
                    n_dz[d][z] += 1

                    if not load_dir:
                        n_mzw[m][z][w] += 1
                        n_mz[m][z] += 1

        lik = 0
        for m in range(M):
            if data[m] is not None:
                lik += calc_liklihood(data[m], n_dz, n_mzw[m], n_mz[m], K,
                                      dims[m])
        liks.append(lik)
        plot(n_dz, liks, D, K)

    save_model(save_dir, n_dz, n_mzw, n_mz, M, dims)

    pylab.ioff()
    pylab.show()
示例#43
0
def extract_plr(t, dr, flash_time, scan_id='1234', which_eye='LEFT'):
    # Extract PLR parameters from provided time series.
    flash_time = flash_time[1]
    t = t - (flash_time - 1)
    flash_time = 1.0

    # Find valid points
    idx = q(t > 0)
    t = t[idx]
    dr = dr[idx] * 1000
    t_start = t.min()
    t -= t_start
    dr = taut_string(dr, 0.01)

    # Fit with cubic splines
    t_mn = 0
    t_mx = 5

    idx = q((t > t_mn) * (t < t_mx))
    t_ = t[idx]
    dr_ = dr[idx]
    f = Fit(t_, 100, basis_type='cubic-spline', reg_coefs=[0, 1e-2, 9e-3])
    r = f.fit(dr_)

    # Grab the latency time.
    try:
        roc = 1 / ((2 + 1 + (r.dy)**2)**(3 / 2) / np.abs(r.d2y))
    except:
        roc = inf

    roc = taut_string(roc, 0.10)
    g = Fit(t_, 100, basis_type='cubic-spline', reg_coefs=[0, 1e-2, 1e-3])
    rc = g.fit(roc)
    roc = rc.y

    peaks = []
    for itr, val in enumerate(roc):
        if (itr < 1) or (itr == len(roc) - 1):
            continue
        if val > roc[itr - 1] and (val > roc[itr + 1]):
            peaks.append(t_[itr])

    # Light flash time.
    closest_index = np.abs(r.x - flash_time)
    flash_idx = np.argmin(closest_index)

    # Starting amplitude.
    starting_amplitude = r.y[flash_idx]

    # Find the peak in the curvature.
    peaks = np.array(peaks)
    peaks -= flash_time
    peaks = peaks[peaks > 0.150]
    peak_time = peaks[0] + flash_time
    med_amplitude = np.median(dr_)
    std_amplitude = dr_.std()
    roc -= mean(roc)
    roc = roc * roc.std() * std_amplitude / 2
    roc += med_amplitude
    latency = peak_time - flash_time
    print('Latency is {:0.3f}'.format(latency))

    # Minimum amplitude.
    idx_min, idx_max, t_min, t_max, v_min, v_max =\
            extrema_in_range(r.x, r.y, t_mn, t_mx-2)
    minimum_amplitude = v_min
    minimum_time = t_min
    delta_amplitude = starting_amplitude - minimum_amplitude
    average_speed = (delta_amplitude) / (minimum_time - flash_time)

    # Find the recovery time.
    recovery_amplitude = 0.75 * delta_amplitude + minimum_amplitude
    idx_after_min = q(t > minimum_time)
    t_after_min = t[idx_after_min]
    amp_after_min = dr[idx_after_min]
    recovery_idx = q(amp_after_min >= recovery_amplitude)[0]
    if recovery_idx:
        recovery_time = t_after_min[recovery_idx] - flash_time
        time_of_recovery = t_after_min[recovery_idx]
    else:
        recovery_time = -1
        time_of_recovery = 5

    package = {}
    package['latency'] = latency
    package['starting_diameter'] = starting_amplitude
    package['minimum_diameter'] = minimum_amplitude
    package['absolute_diameter_change'] = delta_amplitude
    package['relative_diameter_change'] = delta_amplitude / starting_amplitude
    package['average_speed'] = average_speed
    package['recovery_time'] = recovery_time

    plt.ioff()
    plt.close('all')
    plt.plot(r.x, r.y, color='#c62828', linewidth=3)
    plt.plot(t_, roc, color='#305580', linewidth=3, alpha=0.5)
    y_lim = plt.ylim(
        [med_amplitude - 3 * std_amplitude, med_amplitude + 2 * std_amplitude])
    plt.plot([0, 5], [starting_amplitude, starting_amplitude],
             ':',
             color='#2f2f2f')
    plt.plot([0, 5], [minimum_amplitude, minimum_amplitude],
             ':',
             color='#2f2f2f')
    plt.plot([time_of_recovery, time_of_recovery], [0, 20],
             '--',
             color='#c62828')
    plt.plot([flash_time, flash_time], [0, 20], '--', color='#000000')
    plt.plot([peak_time, peak_time], [0, 20], '--', color='#4caf50')
    plt.xlim([t_mn, t_mx])
    plt.xlabel('Time (Seconds)')
    plt.ylabel('Pupil Diameter (mm)')
    location = './plots'
    plt.savefig('{}/{}_{}.png'.format(location, scan_id, which_eye))
    return package
示例#44
0
def MultiBatteryBarChart(report_list,
                         interactive=False,
                         legenddata=(),
                         title=""):
    """Create a bar chart with that places similar conditions together for
  comparison.

  Reports with matching metadata should be grouped together in the list.
  """
    import pylab
    from matplotlib.font_manager import FontProperties
    from matplotlib import colors
    if not interactive:
        pylab.ioff()
    build = report_list[0].metadata.build

    N = len(report_list)

    barwidth = 1.0 / 8.0

    patches = []
    labels = []
    thefig = pylab.gcf()
    thefig.set_size_inches((11, 7))
    thefig.set_dpi(80.0)
    pylab.title("Battery Life (%s) %s" % (build.id, title))
    pylab.subplots_adjust(left=0.07, bottom=0.05, right=0.55, top=0.95)

    group = 0
    last_report = None
    for index, battery_report in enumerate(report_list):
        lifetime = float(battery_report.lifetime.hours)
        blue = aid.IF(battery_report.byvoltage, 0.0, 0.5)
        color = colors.rgb2hex(
            (aid.IF(battery_report.estimated, 1.0,
                    0.0), (index * 0.05) % 1.0, (group * 0.1) % 1.0))
        if last_report and \
            last_report.metadata.CompareData(battery_report.metadata,
            legenddata, missingok=True):
            label = "(%s) - (%.2f h)" % (index + 1, lifetime)
        else:
            label = "(%s) %s (%.2f h)" % (
                index + 1, battery_report.metadata.GetStateString(*legenddata),
                lifetime)
            group += 1
            #pylab.bar(index + group, 0, width=barwidth) # spacer
        patchlist = pylab.bar(index + group,
                              lifetime,
                              width=barwidth,
                              color=color,
                              label=label)
        labels.append(label)
        patches.extend(patchlist)
        last_report = battery_report

    pylab.ylim(0.0, 200.0)
    pylab.xlim(-barwidth, N)
    # label index is origin 1.
    x_range = pylab.arange(N)
    pylab.xticks(x_range + (barwidth / 2.0),
                 map(str, x_range + 1),
                 fontsize=10)
    pylab.ylabel('Time (h)')
    if legenddata:
        font = FontProperties(size=9)
        pylab.figlegend(patches, labels, "upper right", prop=font)

    if interactive:
        pylab.show()
    else:
        fname = "batterylife_group-%s.png" % (
            report_list[0].metadata.timestamp.strftime("%m%d%H%M%S"))
        pylab.savefig(fname, format="png")
        return fname
示例#45
0
def plot_task_hierarchy_a_b(data_HP, data_PFC,experiment_aligned_HP, experiment_aligned_PFC,\
                        beh = True, block = False, raw_data = False, HP = True,start = 0, end = 25,\
                        region = 'HP', plot = True,\
                        a_b_in_task = False, plot_block_in_task = True, a_b_all_tasks = False, plot_block_all_tasks = False, perm = True,\
                            plot_all = False):

     a_a_matrix_t_1_list, b_b_matrix_t_1_list,\
     a_a_matrix_t_2_list, b_b_matrix_t_2_list,\
     a_a_matrix_t_3_list, b_b_matrix_t_3_list,\
     block_1_t1, block_2_t1, block_3_t1,block_4_t1,\
     block_1_t2, block_2_t2, block_3_t2, block_4_t2,\
     block_1_t3, block_2_t3, block_3_t3, block_4_t3 =  hieararchies_extract(data_HP, data_PFC,experiment_aligned_HP, \
     experiment_aligned_PFC, beh = beh, block = block, raw_data = raw_data, HP = HP, start = start, end = end)
   
    
     if HP == True:
         a_list,  b_list, rew_list,  no_rew_list = find_rewards_choices(data_HP, experiment_aligned_HP)
         if plot_block_all_tasks == False:
             ind_above_chance = perm_time_in_block(data_HP, data_PFC,experiment_aligned_HP, \
     experiment_aligned_PFC, beh = beh, block = block, raw_data = raw_data, HP = HP, start = start, end = end, perm = perm)
         elif  plot_block_all_tasks ==  True:
            ind_above_chance,per = perm_test_time(data_HP, data_PFC,experiment_aligned_HP, \
     experiment_aligned_PFC, beh = beh, block = block, raw_data = raw_data, HP = HP, start = start, end = end, perm = perm)
          
     elif HP == False:
        a_list,  b_list, rew_list,  no_rew_list = find_rewards_choices(data_PFC, experiment_aligned_PFC)
        if plot_block_all_tasks == False:
            ind_above_chance = perm_time_in_block(data_HP, data_PFC,experiment_aligned_HP, \
     experiment_aligned_PFC, beh = beh, block = block, raw_data = raw_data, HP = HP, start = start, end = end, perm = perm)
        elif  plot_block_all_tasks ==  True:
           ind_above_chance, per = perm_test_time(data_HP, data_PFC,experiment_aligned_HP, \
     experiment_aligned_PFC, beh = beh, block = block, raw_data = raw_data, HP = HP, start = start, end = end, perm = perm)
          
                       
     # a_blocks_1_2 = np.hstack((a_a_matrix_t_1_list,a_a_matrix_t_2_list, a_a_matrix_t_3_list))
     # b_blocks_1_2 =  np.hstack((b_b_matrix_t_1_list,b_b_matrix_t_2_list, b_b_matrix_t_3_list))
     # stack_all_blocks = np.concatenate((a_blocks_1_2,b_blocks_1_2),1)
     # c = palettable.scientific.sequential.LaPaz_5.mpl_colormap

     # np.arange(a_a_matrix_t_1_list.shape[1])
     # corr = np.corrcoef(stack_all_blocks.T)
     # plt.ion()
     # plt.figure()
     # swtich = a_a_matrix_t_1_list.shape[1]/2
     # ticks = [0,swtich,swtich*2,swtich*3,swtich*4,swtich*5,swtich*6,swtich*7,swtich*8,swtich*9,swtich*10,swtich*11]
     # tick_n = [' A 1 T1 ', ' A 2 T1 ', 'B 1 T1', 'B 2 T1', ' A 1 T2 ', ' A 2 T2 ', 'B 1 T2', 'B 2 T2',\
     #           ' A 1 T3 ', ' A 2 T3 ', 'B 1 T3', 'B 2 T3']
     
     # # plt.ioff()
     # plt.imshow(corr, cmap = c)
     # plt.xticks(ticks, tick_n, rotation = 90)
     # plt.yticks(ticks, tick_n)

     # plt.colorbar()
     switch = int(a_a_matrix_t_1_list.shape[1]/2)
     
     a_s_1 = np.mean([a_a_matrix_t_1_list[:,:switch],a_a_matrix_t_1_list[:,switch:]],0)
     b_s_1 = np.mean([b_b_matrix_t_1_list[:,:switch],b_b_matrix_t_1_list[:,switch:]],0)
     a_s_1_1 = a_a_matrix_t_1_list[:,:switch] 
     a_s_1_2 = a_a_matrix_t_1_list[:,switch:]
     b_s_1_1 = b_b_matrix_t_1_list[:,:switch]
     b_s_1_2 = b_b_matrix_t_1_list[:,switch:]
     
   
     a_s_2 = np.mean([a_a_matrix_t_2_list[:,:switch],a_a_matrix_t_2_list[:,switch:]],0)
     b_s_2 = np.mean([b_b_matrix_t_2_list[:,:switch],b_b_matrix_t_2_list[:,switch:]],0)
     a_s_2_1 = a_a_matrix_t_2_list[:,:switch]
     a_s_2_2 = a_a_matrix_t_2_list[:,switch:]
     b_s_2_1 = b_b_matrix_t_2_list[:,:switch]
     b_s_2_2 = b_b_matrix_t_2_list[:,switch:]
  
     a_s_3 = np.mean([a_a_matrix_t_3_list[:,:switch],a_a_matrix_t_3_list[:,switch:]],0)
     b_s_3 = np.mean([b_b_matrix_t_3_list[:,:switch],b_b_matrix_t_3_list[:,switch:]],0)
     
     a_s_3_1 = a_a_matrix_t_3_list[:,:switch]
     a_s_3_2 = a_a_matrix_t_3_list[:,switch:]
     b_s_3_1 = b_b_matrix_t_3_list[:,:switch]
     b_s_3_2 = b_b_matrix_t_3_list[:,switch:]
     
     # if raw_data == True:
     #     a_s_1_1 = a_s_1_1 - np.mean(a_s_1_1,1)
     #     a_s_1_2 = a_s_1_2 - np.mean(a_s_1_2,1)
     #     a_s_1 = np.mean([a_s_1_1,a_s_1_2],0)

     #     b_s_1_1 = b_s_1_1 - np.mean(b_s_1_1,1)
     #     b_s_1_2 = b_s_1_2 - np.mean(b_s_1_2,1)
     #     b_s_1 = np.mean([b_s_1_1,b_s_1_2],0)

     #     a_s_2_1 = a_s_2_1 - np.mean(a_s_2_1,1)
     #     a_s_2_2 = a_s_2_2 - np.mean(a_s_2_2,1)
     #     a_s_2 = np.mean([a_s_2_1,a_s_2_2],0)

     #     b_s_2_1 = b_s_2_1 - np.mean(b_s_2_1,1)
     #     b_s_2_2 = b_s_2_2 - np.mean(b_s_2_2,1)
     #     b_s_2 = np.mean([b_s_2_1,b_s_2_2],0)

     #     a_s_3_1 = a_s_3_1 - np.mean(a_s_3_1,1)
     #     a_s_3_2 = a_s_3_2 - np.mean(a_s_3_2,1)
     #     a_s_3 = np.mean([a_s_3_1,a_s_3_2],0)

     #     b_s_3_1 = b_s_3_1 - np.mean(b_s_3_1,1)
     #     b_s_3_2 = b_s_3_2 - np.mean(b_s_3_2,1)
     #     b_s_3 = np.mean([b_s_3_1,b_s_3_2],0)

     #     stack_all_blocks = np.concatenate((a_s_1,a_s_2, a_s_3, b_s_1, b_s_2,b_s_3),1)
   
     stack_all_blocks = np.concatenate((a_s_1,a_s_2, a_s_3, b_s_1, b_s_2,b_s_3),1)
     c = palettable.scientific.sequential.LaPaz_5.mpl_colormap

     corr = np.corrcoef(stack_all_blocks.T)
     plt.figure()
     swtich = a_s_1.shape[1]
     ticks = [0,swtich,swtich*2,swtich*3,swtich*4, swtich*5]
     tick_n = [' A 1 ', ' A 2 ', 'A 3', 'B 1 ', ' B 2 ', ' B 3']
     
     plt.ion()
     plt.figure()
     plt.imshow(corr, cmap = c)
     plt.xticks(ticks, tick_n, rotation = 90)
     plt.yticks(ticks, tick_n)

     
     isl_1 =  wes.Moonrise1_5.mpl_colors
     isl  = wes.Royal3_5.mpl_colors
     if plot == True:
         pdf = PdfPages('/Users/veronikasamborska/Desktop/time/'+ region +'A vs B block 1 vs 2.pdf')
         plt.ioff()
         count = 0
         plot_new = True
       
         #plt.savefig('/Users/veronikasamborska/Desktop/time/'+ region +'corr_time_in_block.pdf')
         switch = int(a_a_matrix_t_1_list.shape[1]/2)
         neuron_count = 0
         for i,m in enumerate(a_a_matrix_t_1_list): 
            count +=1
            neuron_count += 1
            if count == 7:
                plot_new = True
                count = 1
            if plot_new == True:
                pdf.savefig()      
                plt.clf()
                plt.figure()
                plot_new = False
            plt.subplot(3,4, count)
           
            #blocks_mean_b = np.mean([b_b_matrix_t_1_list[i],b_b_matrix_t_2_list[i], b_b_matrix_t_3_list[i]],0)
            #blocks_std_b = np.std([b_b_matrix_t_1_list[i],b_b_matrix_t_2_list[i], b_b_matrix_t_3_list[i]],0)/(np.sqrt(3))
            
            #blocks_mean_a = np.mean([a_a_matrix_t_1_list[i],a_a_matrix_t_2_list[i], a_a_matrix_t_3_list[i]],0)
           # blocks_std_a = np.std([a_a_matrix_t_1_list[i],a_a_matrix_t_2_list[i], a_a_matrix_t_3_list[i]],0)/(np.sqrt(3))
            grand_bud = wes.GrandBudapest1_4.mpl_colors
            grand_bud_1 = wes.GrandBudapest2_4.mpl_colors
            mend = wes.Mendl_4.mpl_colors
            if raw_data == True:
                
                a_1 = (a_s_1_1[i]- np.mean(a_s_1_1[i]))
                a_2 = (a_s_1_2[i]- np.mean(a_s_1_2[i]))
                a_3 = (a_s_2_1[i]- np.mean(a_s_2_1[i]))
                a_4 = (a_s_2_2[i]- np.mean(a_s_2_2[i]))
                a_5 = (a_s_3_1[i]- np.mean(a_s_3_1[i]))
                a_6 = (a_s_3_2[i]- np.mean(a_s_3_2[i]))

                b_1 = (b_s_1_1[i]- np.mean(b_s_1_1[i]))
                b_2 = (b_s_1_2[i]- np.mean(b_s_1_2[i]))
                b_3 = (b_s_2_1[i]- np.mean(b_s_2_1[i]))
                b_4 = (b_s_2_2[i]- np.mean(b_s_2_2[i]))
                b_5 = (b_s_3_1[i]- np.mean(b_s_3_1[i]))
                b_6 = (b_s_3_2[i]- np.mean(b_s_3_2[i]))
            else:
                    
                a_1 = a_s_1_1[i]#- np.mean(a_s_1_1[i])
                a_2 = a_s_1_2[i]#- np.mean(a_s_1_2[i])
                a_3 = a_s_2_1[i]#- np.mean(a_s_2_1[i])
                a_4 = a_s_2_2[i]#- np.mean(a_s_2_2[i])
                a_5 = a_s_3_1[i]#- np.mean(a_s_3_1[i])
                a_6 = a_s_3_2[i]#- np.mean(a_s_3_2[i])
    
                b_1 = b_s_1_1[i]#- np.mean(b_s_1_1[i])
                b_2 = b_s_1_2[i]#- np.mean(b_s_1_2[i])
                b_3 = b_s_2_1[i]#- np.mean(b_s_2_1[i])
                b_4 = b_s_2_2[i]#- np.mean(b_s_2_2[i])
                b_5 = b_s_3_1[i]#- np.mean(b_s_3_1[i])
                b_6 = b_s_3_2[i]#- np.mean(b_s_3_2[i])

            if a_b_in_task == True: 
                
                plt.plot(a_1, color = grand_bud[1], label = 'A 1')
                           
                plt.plot(a_2, color = grand_bud_1[0], label = 'A 2')
    
                plt.plot(a_3, color = isl[1], label = 'A 3')
    
                plt.plot(a_4, color = isl_1[0], label = 'B 1')
    
                plt.plot(a_5, color = mend[1], label = 'B 2')
    
                plt.plot(a_6, color = mend[3], label = 'B 3')
            
            if plot_block_in_task == True:
            
                blocks_mean_task_1 =  np.mean([a_1,a_2, b_1,b_2],0)
                blocks_std_task_1 = np.std([a_1,a_2, b_1,b_2],0)/(np.sqrt(4))

                blocks_mean_task_2 =  np.mean([a_3,a_4, b_3,b_4],0)
                blocks_std_task_2 = np.std([a_3,a_4, b_3,b_4],0)/(np.sqrt(4))

                blocks_mean_task_3 =  np.mean([a_5,a_6, b_5,b_6],0)
                blocks_std_task_3 = np.std([a_5,a_6, b_5,b_6],0)/(np.sqrt(4))
 
                plt.plot(blocks_mean_task_1, color = isl_1[0], label = 'Task 1')
                plt.fill_between(np.arange(len(blocks_mean_task_1)), blocks_mean_task_1-blocks_std_task_1, blocks_mean_task_1+blocks_std_task_1, alpha=0.2, color = isl_1[0])

                plt.plot(blocks_mean_task_2, color = mend[1], label = 'Task 2')
                plt.fill_between(np.arange(len(blocks_mean_task_2)), blocks_mean_task_2 - blocks_std_task_2, blocks_mean_task_2 + blocks_std_task_2, alpha=0.2, color = mend[1])

                plt.plot(blocks_mean_task_3, color = mend[2], label = 'Task 3')
                plt.fill_between(np.arange(len(blocks_mean_task_3)), blocks_mean_task_3 - blocks_std_task_3, blocks_mean_task_3 + blocks_std_task_3, alpha=0.2, color = mend[2])
                
            elif plot_block_all_tasks:
                 
               
                
                 blocks_all_tasls =  np.mean([a_1,a_2,a_3,a_4,a_5,a_6,b_1,b_2,b_3,b_4,b_5,b_6],0)
                 std_blocks_all_tasls =  np.std([a_1,a_2,a_3,a_4,a_5,a_6,b_1,b_2,b_3,b_4,b_5,b_6],0)/(np.sqrt(12))

                 plt.plot(blocks_all_tasls, color = isl_1[0], label = 'All Tasks Block Time')
                 plt.fill_between(np.arange(len(blocks_all_tasls)), blocks_all_tasls-std_blocks_all_tasls, blocks_all_tasls+std_blocks_all_tasls, alpha=0.2, color = isl_1[0])
 
            elif a_b_all_tasks == True:
                
               

                
                blocks_mean_a = np.mean([a_1,a_2,a_3,a_4,a_5,a_6],0)
                blocks_std_a = np.std([a_1,a_2,a_3,a_4,a_5,a_6],0)/(np.sqrt(6))
                
                blocks_mean_b = np.mean([b_1,b_2,b_3,b_4,b_5,b_6],0)
                blocks_std_b = np.std([b_1,b_2,b_3,b_4,b_5,b_6],0)/(np.sqrt(6))
               
                
                # blocks_mean_a = np.mean([a_s_1[i], a_s_2[i],a_s_3[i]],0)
                # blocks_std_a = np.std([a_s_1[i], a_s_2[i],a_s_3[i]],0)/(np.sqrt(3))
                
                # blocks_mean_b = np.mean([b_s_1[i], b_s_2[i], b_s_3[i]],0)
                # blocks_std_b = np.std([b_s_1[i], b_s_2[i], b_s_3[i]],0)/(np.sqrt(3))
               
                plt.plot(blocks_mean_b, color = isl_1[0], label = 'B')
                plt.fill_between(np.arange(len(blocks_mean_b)), blocks_mean_b-blocks_std_b, blocks_mean_b+blocks_std_b, alpha=0.2, color = isl_1[0])
    
                plt.plot(blocks_mean_a, color = isl_1[1], label = 'A')
                plt.fill_between(np.arange(len(blocks_mean_a)), blocks_mean_a-blocks_std_a, blocks_mean_a+blocks_std_a, alpha=0.2, color = isl_1[1])
    
                #plt.vlines(switch, np.min([np.min(blocks_mean_a),np.min(blocks_mean_b)]), np.max([np.max(blocks_mean_a),np.max(blocks_mean_b)]), linestyle = ':', color = 'grey', label = 'Switch')
            if plot_all == True:
                stack = np.vstack((a_1,a_2,a_3,a_4,a_5,a_6,b_1, b_2,b_3,b_4,b_5,b_6))
                
                labels = ['A 1 ', ' A 2 ', 'A 3', 'A 4', ' A 5 ', 'A 6', 'B 1 ', ' B 2 ', ' B 3', 'B 4 ', ' B 5 ', ' B 6']
                for s,ss in enumerate(stack):
                    plt.plot(ss)
                    
            plt.tight_layout()
            
            if count == 1:
                plt.legend()
            if  (neuron_count-1) in ind_above_chance:
                plt.title('Significant')
            else:
                plt.title(str(count))


            plt.subplot(3,4, count+6)
            plt.plot(a_list[i], color = isl[1], label = 'A')
            plt.plot(b_list[i], color = isl[2], label = 'B')
            plt.plot(rew_list[i], color = isl[3], label = 'Reward')
            plt.plot(no_rew_list[i], color = isl[4], label = 'No Rew')
            if  (neuron_count-1) in ind_above_chance:
                plt.title('Significant')
            else:
                plt.title(str(count))
            plt.vlines([25,36,43], np.min([np.min(a_list[i]),np.min(b_list[i]),np.min(rew_list[i]),np.min(no_rew_list[i])]),\
                                        np.max([np.max(a_list[i]),np.max(b_list[i]),np.max(rew_list[i]),np.max(no_rew_list[i])]),linestyle= '--', color = 'pink')

           
            if count == 1:
                plt.legend()
           
         pdf.savefig()      
         pdf.close()
         
         return ind_above_chance,neuron_count
示例#46
0
def MakeCharts(dataset,
               timemarks="0s,9d",
               ylim=None,
               columns=None,
               autoscale=False,
               events=None,
               interactive=False):
    # import pylab here since this is a very large library that you might
    # not want loaded and initialized everytime you use this module. Also,
    # if you are using one of the GTK based backends for matplotlib, and you
    # do not have DISPLAY set (e.g. running from a remote shell), then you
    # will see this exception raised when you import this module:
    # RuntimeError: could not open display
    # This is not in my, or matplotlib's, control.
    import pylab
    from matplotlib.font_manager import FontProperties
    pylab.ioff()
    names = []
    if type(events) is DataSet:
        events.NormalizeTime(dataset.measurements[0][0])
    dataset.NormalizeTime()
    unit = dataset.unit

    for subset, start, end in dataset.GetTimeSlices(timemarks):
        if len(subset) > 0:
            x_time, datacols, labels = subset.GetColumns(columns)
            if len(x_time) < 100:
                mrk = "."
                ls = "-"
            else:
                mrk = ","
                ls = "None"
            for col, label, color in itertools.izip(datacols, labels,
                                                    color_cycler):
                pylab.plot(x_time,
                           col,
                           color=color,
                           label=label,
                           ls=ls,
                           marker=mrk)
            pylab.setp(pylab.gcf(), dpi=100, size_inches=(9, 6))
            pylab.xlabel("Time (s)")
            if not autoscale:
                _SetYLimit(pylab.gca(), ylim, unit)
            pylab.ylabel(unit)

            if events is not None:
                ax = pylab.gca()
                for row in events:
                    ax.axvline(row[0], color="rgbymc"[int(row[1]) % 6])

            metadata = subset.metadata
            title = "%s-%s-%s-%ss-%ss" % (
                metadata.testcase, metadata.timestamp.strftime("%m%d%H%M%S"),
                "-".join(labels), int(start), int(end))
            pylab.title(title, fontsize="x-small")
            font = FontProperties(size="x-small")
            pylab.legend(prop=font)

            if not interactive:
                fname = "%s.%s" % (title, "png")
                pylab.savefig(fname, format="png")
                names.append(fname)
                pylab.cla()
        else:
            break
    return names
示例#47
0
def orig_adv_dist(orig_img=None,
                  target_img=None,
                  plot=False,
                  bestC=None,
                  iteration=1):
    if orig_img is None:
        orig_img = np.random.randint(0, len(test_x))
    if target_img is None:
        target_label = test_y[orig_img]
        while target_label == test_y[orig_img]:
            target_img = np.random.randint(0, len(test_x))
            target_label = test_y[target_img]

    noise_dist = []
    orig_dist = []
    adv_dist = []
    target_recon_dist = []
    recon_dist = []
    orig_target_dist = []
    orig_target_recon_dist = []
    target_orig_recon_dist = []

    C = np.logspace(-5, 20, 50, base=2, dtype=np.float32)

    for c in C:
        noise, od, ad, ore, tre, recd, otd, otrd, tord = adv_test(
            orig_img, target_img, C=c, plot=False, iteration=iteration)
        noise_dist.append(noise)
        orig_dist.append(od)
        adv_dist.append(ad)
        target_recon_dist.append(tre)
        recon_dist.append(recd)
        orig_target_dist.append(otd)
        orig_target_recon_dist.append(otrd)
        target_orig_recon_dist.append(tord)

    noise_dist = np.array(noise_dist)
    orig_dist = np.array(orig_dist)
    adv_dist = np.array(adv_dist)
    target_recon_dist = np.array(target_recon_dist)
    recon_dist = np.array(recon_dist)
    orig_target_dist = np.array(orig_target_dist)
    orig_target_recon_dist = np.array(orig_target_recon_dist)
    target_orig_recon_dist = np.array(target_orig_recon_dist)

    if bestC is None:
        bestC = C[np.argmax(adv_dist - orig_dist >= 0) - 1]

    print(orig_img, target_img, bestC)

    best_noise, best_orig_dist, best_adv_dist, orig_reconstruction_dist, target_reconstruction_dist, _, _, _, _ = adv_test(
        orig_img, target_img, C=bestC, plot=plot, iteration=iteration)

    plt.ioff()
    if plot:
        fig = plt.figure()
        plt.axhline(y=target_reconstruction_dist,
                    linewidth=2,
                    color='cyan',
                    label="Dist(Target reconstruction - Target)")
        plt.axvline(x=orig_reconstruction_dist,
                    linewidth=2,
                    color='DarkOrange',
                    label="Dist(Original reconstruction - Original)")
        plt.scatter(orig_dist, adv_dist)
        plt.scatter([best_orig_dist], [best_adv_dist], color="red")
        plt.xlabel("Dist(reconstructed Adversarial image - Original image)")
        plt.ylabel("Dist(reconstructed Adversarial image - Target image)")
        plt.legend()
        plt.plot()
        #output_dir = '/Users/rishikaagarwal/Desktop/cs597/adv_vae-master/results/' + model_filename + '/'
        output_dir = 'results/compare_attacks/' + model_filename + '/'
        #os.path.join(output_dir, {}/exp_'+ str(iteration)+ '.png')
        fig.savefig(
            os.path.join(output_dir, ('exp_' + str(iteration) + 'graph1.png')))
        plt.close(fig)

        fig = plt.figure()
        plt.axhline(y=target_reconstruction_dist,
                    linewidth=2,
                    color='cyan',
                    label="Dist(Target reconstruction - Target)")
        plt.axvline(x=np.linalg.norm(test_x[orig_img] - test_x[target_img]),
                    linewidth=2,
                    color='DarkOrange',
                    label="Dist(Original - Target)")
        plt.scatter(noise_dist, adv_dist)
        plt.scatter([best_noise], [best_adv_dist], color="red")
        plt.ylabel("Dist(reconstructed Adversarial image - Target image)")
        plt.xlabel("Dist(noise)")
        plt.legend()
        fig.savefig(
            os.path.join(output_dir, ('exp_' + str(iteration) + 'graph2.png')))
        plt.plot()
        plt.close(fig)

        bestCind = np.where(C == bestC)
        print('orig_img : ', orig_img)
        print('target_img : ', target_img)
        print('orig_reconstruction_dist : ', orig_reconstruction_dist)
        print('target_reconstruction_dist : ', target_reconstruction_dist)
        print('original_target_dist : ', orig_target_dist[bestCind])
        print('orig_target_recon_dist : ', orig_target_recon_dist[bestCind])
        print('target_orig_recon_dist : ', target_orig_recon_dist[bestCind])

        print()
        print('bestC : ', bestC)
        print('adv_adv_recon_dist : ', recon_dist[bestCind])
        print('best noise_dist :  ', noise_dist[bestCind])
        print('best orig_dist :  ', orig_dist[bestCind])
        print('best adv_dist : ', adv_dist[bestCind])
        print()

    df = pd.DataFrame({
        'orig_img': orig_img,
        'target_img': target_img,
        'bestC': bestC,
        'orig_reconstruction_dist': orig_reconstruction_dist,
        'target_reconstruction_dist': target_reconstruction_dist,
        'noise_dist': noise_dist,
        'orig_dist': orig_dist,
        'adv_dist': adv_dist,
        'target_recon_dist': target_recon_dist,
        'recon_dist': recon_dist,
        'orig_target_dist': orig_target_dist,
        'orig_target_recon_dist': orig_target_recon_dist,
        'target_orig_recon_dist': target_orig_recon_dist,
        'C': C
    })

    return df
示例#48
0
    def close(self, s):
        if self.type == 'randomOutput':
            print('\nClosing random output virtual arm...')

        if self.type == 'dummyArm':
            if s.explorMovs == 1:  # remove explor movs related noise to cells
                for imus in range(s.nMuscles):
                    IDSCgids = array(s.motorCmdCellRange[self.randMus]) - int(
                        s.popGidStart[s.EDSC]) + int(s.popGidStart[s.IDSC])
                    for (i, x) in enumerate(s.backgroundsources):
                        if s.backgroundgid[i] in s.motorCmdCellRange[
                                imus] + list(IDSCgids):
                            x.interval = 0.0001**-1 * 1e3
                            x.noise = s.backgroundnoise  # Fractional noise in timing
            elif s.explorMovs == 2:  # remove explor movs related noise to cells
                for (i, x) in enumerate(s.backgroundsources
                                        ):  # for all input background netstims
                    if s.backgroundgid[
                            i] in self.targetCells:  # for the rest of cells
                        x.interval = s.backgroundrate**-1 * 1e3  # set to normal level

            if s.trialReset:
                s.timeoflastreset = 0
                self.initArmMovement = int(s.initArmMovement)

            if s.rank == 0:
                print('\nClosing dummy virtual arm ...')

                if self.anim:
                    ioff()  # turn interactive mode off
                    close(self.fig)  # close arm animation graph
                if self.graphs:  # plot graphs
                    self.plotTraj(s.filename)
                    #self.plotAngs()
                    #self.plotMotorCmds()
                    #self.plotRL()

        if self.type == 'musculoskeletal':
            if s.explorMovs == 1:  # remove explor movs related noise to cells
                for imus in range(s.nMuscles):
                    IDSCgids = array(s.motorCmdCellRange[self.randMus]) - int(
                        s.popGidStart[s.EDSC]) + int(s.popGidStart[s.IDSC])
                    for (i, x) in enumerate(s.backgroundsources):
                        if s.backgroundgid[i] in s.motorCmdCellRange[
                                imus] + list(IDSCgids):
                            x.interval = 0.0001**-1 * 1e3
                            x.noise = s.backgroundnoise  # Fractional noise in timing
            elif s.explorMovs == 2:  # remove explor movs related noise to cells
                for (i, x) in enumerate(s.backgroundsources
                                        ):  # for all input background netstims
                    if s.backgroundgid[
                            i] in self.targetCells:  # for the rest of cells
                        x.interval = s.backgroundrate**-1 * 1e3  # set to normal level

            if s.trialReset:
                s.timeoflastreset = 0
                self.initArmMovement = int(s.initArmMovement)

            if s.rank == 0:
                print('\nClosing dummy virtual arm ...')
                arminterface.closeSavePlot(self.duration / 1000.0,
                                           self.interval)

                if self.graphs:  # plot graphs
                    self.plotTraj(s.filename)
                    #self.plotAngs()
                    self.plotMotorCmds()
                    self.plotRL()
示例#49
0
def cartview(map=None,
             fig=None,
             rot=None,
             zat=None,
             coord=None,
             unit='',
             xsize=800,
             ysize=None,
             lonra=None,
             latra=None,
             title='Cartesian view',
             nest=False,
             remove_dip=False,
             remove_mono=False,
             gal_cut=0,
             min=None,
             max=None,
             flip='astro',
             format='%.3g',
             cbar=True,
             cmap=None,
             norm=None,
             aspect=None,
             hold=False,
             sub=None,
             margins=None,
             notext=False,
             return_projected_map=False):
    """Plot an healpix map (given as an array) in Cartesian projection.

    Parameters
    ----------
    map : float, array-like or None
      An array containing the map, 
      supports masked maps, see the `ma` function.
      If None, will display a blank map, useful for overplotting.
    fig : int or None, optional
      The figure number to use. Default: create a new figure
    rot : scalar or sequence, optional
      Describe the rotation to apply.
      In the form (lon, lat, psi) (unit: degrees) : the point at
      longitude *lon* and latitude *lat* will be at the center. An additional rotation
      of angle *psi* around this direction is applied.
    coord : sequence of character, optional
      Either one of 'G', 'E' or 'C' to describe the coordinate
      system of the map, or a sequence of 2 of these to rotate
      the map from the first to the second coordinate system.
    unit : str, optional
      A text describing the unit of the data. Default: ''
    xsize : int, optional
      The size of the image. Default: 800
    lonra : sequence, optional
      Range in longitude. Default: [-180,180]
    latra : sequence, optional
      Range in latitude. Default: [-90,90]
    title : str, optional
      The title of the plot. Default: 'Mollweide view'
    nest : bool, optional
      If True, ordering scheme is NESTED. Default: False (RING)
    min : float, optional
      The minimum range value
    max : float, optional
      The maximum range value
    flip : {'astro', 'geo'}, optional
      Defines the convention of projection : 'astro' (default, east towards left, west towards right)
      or 'geo' (east towards roght, west towards left)
    remove_dip : bool, optional
      If :const:`True`, remove the dipole+monopole
    remove_mono : bool, optional
      If :const:`True`, remove the monopole
    gal_cut : float, scalar, optional
      Symmetric galactic cut for the dipole/monopole fit.
      Removes points in latitude range [-gal_cut, +gal_cut]
    format : str, optional
      The format of the scale label. Default: '%g'
    cbar : bool, optional
      Display the colorbar. Default: True
    notext : bool, optional
      If True, no text is printed around the map
    norm : {'hist', 'log', None}, optional
      Color normalization, hist= histogram equalized color mapping,
      log= logarithmic color mapping, default: None (linear color mapping)
    hold : bool, optional
      If True, replace the current Axes by a CartesianAxes.
      use this if you want to have multiple maps on the same
      figure. Default: False
    sub : int, scalar or sequence, optional
      Use only a zone of the current figure (same syntax as subplot).
      Default: None
    margins : None or sequence, optional
      Either None, or a sequence (left,bottom,right,top)
      giving the margins on left,bottom,right and top
      of the axes. Values are relative to figure (0-1).
      Default: None
    return_projected_map : bool
      if True returns the projected map in a 2d numpy array

    See Also
    --------
    mollview, gnomview, orthview, azeqview
    """
    import pylab
    if not (hold or sub):
        f = pylab.figure(fig, figsize=(8.5, 5.4))
        if not margins:
            margins = (0.075, 0.05, 0.075, 0.05)
        extent = (0.0, 0.0, 1.0, 1.0)
    elif hold:
        f = pylab.gcf()
        left, bottom, right, top = np.array(pylab.gca().get_position()).ravel()
        if not margins:
            margins = (0.0, 0.0, 0.0, 0.0)
        extent = (left, bottom, right - left, top - bottom)
        f.delaxes(pylab.gca())
    else:  # using subplot syntax
        f = pylab.gcf()
        if hasattr(sub, '__len__'):
            nrows, ncols, idx = sub
        else:
            nrows, ncols, idx = sub // 100, (sub % 100) // 10, (sub % 10)
        if idx < 1 or idx > ncols * nrows:
            raise ValueError('Wrong values for sub: %d, %d, %d' %
                             (nrows, ncols, idx))
        c, r = (idx - 1) % ncols, (idx - 1) // ncols
        if not margins:
            margins = (0.01, 0.0, 0.0, 0.02)
        extent = (c * 1. / ncols + margins[0],
                  1. - (r + 1) * 1. / nrows + margins[1],
                  1. / ncols - margins[2] - margins[0],
                  1. / nrows - margins[3] - margins[1])
    extent = (extent[0] + margins[0], extent[1] + margins[1],
              extent[2] - margins[2] - margins[0],
              extent[3] - margins[3] - margins[1])

    #f=pylab.figure(fig,figsize=(5.5,6))
    # Starting to draw : turn interactive off
    wasinteractive = pylab.isinteractive()
    pylab.ioff()
    try:
        if map is None:
            map = np.zeros(12) + np.inf
            cbar = False
        map = pixelfunc.ma_to_array(map)
        if zat and rot:
            raise ValueError('Only give rot or zat, not both')
        if zat:
            rot = np.array(zat, dtype=np.float64)
            rot.resize(3)
            rot[1] -= 90
        ax = PA.HpxCartesianAxes(f,
                                 extent,
                                 coord=coord,
                                 rot=rot,
                                 format=format,
                                 flipconv=flip)
        f.add_axes(ax)
        if remove_dip:
            map = pixelfunc.remove_dipole(map,
                                          gal_cut=gal_cut,
                                          nest=nest,
                                          copy=True)
        elif remove_mono:
            map = pixelfunc.remove_monopole(map,
                                            gal_cut=gal_cut,
                                            nest=nest,
                                            copy=True)
        img = ax.projmap(map,
                         nest=nest,
                         coord=coord,
                         vmin=min,
                         vmax=max,
                         xsize=xsize,
                         ysize=ysize,
                         lonra=lonra,
                         latra=latra,
                         cmap=cmap,
                         norm=norm,
                         aspect=aspect)
        if cbar:
            im = ax.get_images()[0]
            b = im.norm.inverse(np.linspace(0, 1, im.cmap.N + 1))
            v = np.linspace(im.norm.vmin, im.norm.vmax, im.cmap.N)
            if matplotlib.__version__ >= '0.91.0':
                cb = f.colorbar(im,
                                ax=ax,
                                orientation='horizontal',
                                shrink=0.5,
                                aspect=25,
                                ticks=PA.BoundaryLocator(),
                                pad=0.08,
                                fraction=0.1,
                                boundaries=b,
                                values=v,
                                format=format)
            else:
                cb = f.colorbar(im,
                                orientation='horizontal',
                                shrink=0.5,
                                aspect=25,
                                ticks=PA.BoundaryLocator(),
                                pad=0.08,
                                fraction=0.1,
                                boundaries=b,
                                values=v,
                                format=format)
            cb.solids.set_rasterized(True)
        ax.set_title(title)
        if not notext:
            ax.text(-0.07,
                    0.6,
                    ax.proj.coordsysstr,
                    fontsize=14,
                    fontweight='bold',
                    rotation=90,
                    transform=ax.transAxes)
        if cbar:
            cb.ax.text(1.05,
                       0.30,
                       unit,
                       fontsize=14,
                       fontweight='bold',
                       transform=cb.ax.transAxes,
                       ha='left',
                       va='center')
        f.sca(ax)
    finally:
        if wasinteractive:
            pylab.ion()
            pylab.draw()
            #pylab.show()
    if return_projected_map:
        return img
示例#50
0
def plot_task_hierarchy(data_HP, data_PFC,experiment_aligned_HP, experiment_aligned_PFC,\
                        beh = True, block = False, raw_data = False, HP = True, start = 0, end = 62, region = 'HP', plot = True):

     a_a_matrix_t_1_list, b_b_matrix_t_1_list,\
     a_a_matrix_t_2_list, b_b_matrix_t_2_list,\
     a_a_matrix_t_3_list, b_b_matrix_t_3_list,\
     block_1_t1, block_2_t1, block_3_t1,block_4_t1,\
     block_1_t2, block_2_t2, block_3_t2, block_4_t2,\
     block_1_t3, block_2_t3, block_3_t3, block_4_t3 =  hieararchies_extract(data_HP, data_PFC,experiment_aligned_HP, \
     experiment_aligned_PFC, beh = beh, block = block, raw_data = raw_data, HP = HP, start = start, end = end)
   
    
     if HP == True:
         a_list,  b_list, rew_list,  no_rew_list = find_rewards_choices(data_HP, experiment_aligned_HP)
     elif HP == False:
         a_list,  b_list, rew_list,  no_rew_list = find_rewards_choices(data_PFC, experiment_aligned_PFC)
                               
     isl = wes.FantasticFox2_5.mpl_colors
     plt.ioff()

     blocks_t1 = np.hstack((block_1_t1,block_2_t1,block_3_t1, block_4_t1))
     blocks_t2 = np.hstack((block_1_t2,block_2_t2,block_3_t2, block_4_t2))
     blocks_t3 = np.hstack((block_1_t3,block_2_t3,block_3_t3, block_4_t3))
     stack_all_blocks = np.concatenate((blocks_t1,blocks_t2,blocks_t3),1)
     c = palettable.scientific.sequential.LaPaz_5.mpl_colormap
     switch = block_1_t1.shape[1]

     ticks = [0,switch,switch*2,switch*3,switch*4,switch*5,switch*6,switch*7,switch*8,switch*9,switch*10,switch*11]
     tick_n = ['1 T1 ', '2 T1 ', '3 T1', '4 T1', '1 T2 ', '2 T2 ', '3 T2', '4 T2',\
               ' 1 T3 ', ' 2 T3 ', '3 T3', '4 T3']
  
     corr = np.corrcoef(stack_all_blocks.T)
     plt.figure()

     plt.imshow(corr, cmap = c)
     plt.colorbar()
     plt.xticks(ticks, tick_n, rotation = 90)
     plt.yticks(ticks, tick_n)
     isl_1 =  wes.Moonrise1_5.mpl_colors
     grand_bud = wes.GrandBudapest1_4.mpl_colors

     if plot == True:
         pdf = PdfPages('/Users/veronikasamborska/Desktop/time/'+ region +'time_in_block.pdf')
         plt.ioff()
         count = 0
         plot_new = True
       
         for i,m in enumerate(blocks_t1): 
             
            count +=1
            if count == 7:
                plot_new = True
                count = 1
            if plot_new == True:
                pdf.savefig()      
                plt.clf()
                plt.figure()
                plot_new = False
            plt.subplot(3,4, count)
           
            blocks_mean = np.mean([blocks_t1[i], blocks_t2[i], blocks_t3[i]],0)
            blocks_std = np.std([blocks_t1[i], blocks_t2[i], blocks_t3[i]],0)/(np.sqrt(4))
            
            plt.plot(blocks_mean, color = grand_bud[0], label = 'Task Av')
            plt.fill_between(np.arange(len(blocks_mean)), blocks_mean-blocks_std, blocks_mean+blocks_std, alpha=0.2, color = grand_bud[0])
        
            #plt.plot(blocks_t1[i], color = isl_1[1], label = 'Task 1')
            #plt.plot(blocks_t2[i], color = isl_1[2], label = 'Task 2')
            #plt.plot(blocks_t3[i], color = isl_1[4], label = 'Task 3')

            plt.vlines([switch,switch*2,switch*3], np.min(blocks_mean), np.max(blocks_mean), linestyle = ':', color = 'grey', label = 'Switch')
            
            plt.title(str(count))
            plt.tight_layout()
            
            if count == 1:
                plt.legend()
                
            plt.subplot(3,4, count+6)
            plt.plot(a_list[i], color = isl[1], label = 'A')
            plt.plot(b_list[i], color = isl[2], label = 'B')
            plt.plot(rew_list[i], color = isl[3], label = 'Reward')
            plt.plot(no_rew_list[i], color = isl[4], label = 'No Rew')
            plt.title(str(count))
            plt.vlines([25,36,43], np.min([np.min(a_list[i]),np.min(b_list[i]),np.min(rew_list[i]),np.min(no_rew_list[i])]),\
                                        np.max([np.max(a_list[i]),np.max(b_list[i]),np.max(rew_list[i]),np.max(no_rew_list[i])]),linestyle= '--', color = 'pink')

            if count == 1:
                plt.legend()
         pdf.savefig()      
         pdf.close()
示例#51
0
def signal_handler(signal, frame):
    plt.ioff()
    sys.exit(0)
示例#52
0
文件: tools.py 项目: Desolnir/PZ3
    def stop(self):

        # Остановить анимацию

        pylab.ioff()
示例#53
0
文件: firn.py 项目: JacobDowns/cslvr
    'density': True,
    'velocity': True,
    'age': False
}

mom = MomentumFirn(model)
nrg = EnergyFirn(model)
plt = FirnPlot(model, plot_cfg)


def cb():
    model.update_height_history()
    theta_exp.t = model.t
    rho_exp.t = model.t
    w_exp.t = model.t
    #bdotNew     = (w_exp.adot * model.rhoi(0)) / model.spy(0)
    #model.assign_variable(model.bdot, bdotNew)
    plt.update()


dt_list = [dt1, dt2]
#dt_list = None

model.transient_solve(mom, nrg, t0, tm, tf, dt1, dt_list=dt_list, callback=cb)

p.ioff()
p.show()

# plot the surface height trend :
#plt.plot_height(model.times, model.ht, model.origHt)
示例#54
0
import numpy as np
import json
import os
import radio_beam
import reproject
from astropy import constants, units as u, table, stats, coordinates, wcs, log
from astropy.io import fits
from spectral_cube import SpectralCube, wcs_utils, tests, Projection, OneDSpectrum
from astropy.nddata import Cutout2D
from parse_contdotdat import parse_contdotdat

import tempfile

import pylab as pl
pl.ioff()

overwrite=False



# for zarr storage
os.environ['TMPDIR'] = '/blue/adamginsburg/adamginsburg/tmp'

if __name__ == "__main__":
    # need to be in main block for dask to work
    from dask.distributed import Client
    if os.getenv('SLURM_MEM_PER_NODE'):
        memlim_total = int(os.getenv('SLURM_MEM_PER_NODE')) / 1024 # GB
        ntasks = int(os.getenv('SLURM_NTASKS'))
        memlim = memlim_total / ntasks
    else:
示例#55
0
def VOCprlist(gtImages, detlist, show=False, usetr=True, usedf=False, ovr=0.5):
    """
        calculate the precision recall curve
    """
    #detf=open(detfile,"r")
    #detect=detf.readlines()
    imname = []
    cnt = 0
    #ovr=0.49
    #print trPosImages.getTotal()
    tp = []
    fp = []
    tot = 0
    for idx in range(gtImages.getTotal()):
        print gtImages.getImageName(idx)
        if show:
            img = gtImages.getImage(idx)
            pylab.figure(1)
            pylab.clf()
            pylab.imshow(img)
        #pyr=HOGcompute.HOGcrop(img,interv=interv)
        #pyr.pad()
        #pyr.pad()
        #pyr.contrast()
        rect = gtImages.getBBox(idx, usetr=usetr, usedf=usedf)
        print rect
        if show:
            for r in rect:
                pylab.figure(1)
                pylab.ioff()
                box(r[0], r[1], r[2], r[3], 'b', lw=1.5)
                #raw_input()
        tot = tot + len(rect)
        #print len(rect),rect
        #print rect
        for l in detlist:
            data = l  #.split(" ")
            if data[0] == gtImages.getImageName(idx).split("/")[-1].split(
                    ".")[0]:
                notfound = True
                rb = [
                    float(data[3]),
                    float(data[2]),
                    float(data[5]),
                    float(data[4])
                ]
                if show:
                    pylab.ioff()
                    pylab.text(rb[1], rb[0], data[1])
                for id, r in enumerate(rect):
                    #pylab.figure(1)
                    #box(r[0],r[1],r[2],r[3],'b',lw=1.5)
                    #print "entered",data
                    #rb=[float(data[3]),float(data[2]),float(data[5]),float(data[4])]
                    #print rb,r,overlap(rb,r)
                    #pylab.text(rb[1],rb[0],data[1])
                    if overlap(rb, r) >= ovr:
                        if show:
                            pylab.ioff()
                            box(rb[0], rb[1], rb[2], rb[3], 'g', lw=1.5)
                        del rect[id]
                        tp.append(float(data[1]))
                        notfound = False
                        break
                if notfound == True:
                    if show:
                        pylab.ioff()
                        box(rb[0], rb[1], rb[2], rb[3], 'r', lw=1)
                    fp.append(float(data[1]))
                #print len(tp),len(fp),tot
            #break
        if show:
            pylab.figure(1)
            pylab.show()
            pylab.draw()
        #raw_input()
    return tp, fp, tot
示例#56
0
 def stop(self):
     '''
     Остановить анимацию
     '''
     pylab.ioff()
示例#57
0
def viewSortDet(gtImages,
                detlist,
                numim=numpy.inf,
                opt="all",
                usetr=True,
                usedf=False,
                ovr=0.5):
    dimg = {}
    tot = 0
    for idx in range(min(gtImages.getTotal(), numim)):
        rect = gtImages.getBBox(idx)
        if rect != []:
            #print gtImages.getImageName(idx).split("/")[-1].split(".")[0]
            dimg[gtImages.getImageName(idx).split("/")[-1].split(".")
                 [0]] = rect
        tot = tot + len(rect)
    imname = []
    cnt = 0
    tp = numpy.zeros(len(detlist))
    fp = numpy.zeros(len(detlist))
    thr = numpy.zeros(len(detlist))
    detlist.sort(cmpscore)
    for idx, detbb in enumerate(detlist):
        #print detbb[1]
        found = False
        if dimg.has_key(detbb[0]):
            rect = dimg[detbb[0]]
            found = False
            for r in rect:
                rb = (float(detbb[3]), float(detbb[2]), float(detbb[5]),
                      float(detbb[4]))
                #print "GT:",r
                #print "DET:",rb
                if overlap(rb, r) >= ovr:
                    dimg[detbb[0]].remove(r)
                    found = True
                    break
        if found:
            tp[idx] = 1
        else:
            fp[idx] = 1
        thr[idx] = detbb[1]
        if show:
            pylab.ioff()
            prec = numpy.sum(tp) / float(numpy.sum(tp) + numpy.sum(fp))
            rec = numpy.sum(tp) / tot
            print "Scr:", detbb[1], "Prec:", prec, "Rec:", rec
            img = gtImages.getImageByName2(detbb[0])
            pylab.figure(1)
            pylab.clf()
            pylab.imshow(img)
            rb = (float(detbb[3]), float(detbb[2]), float(detbb[5]),
                  float(detbb[4]))
            for r in rect:
                pylab.figure(1)
                pylab.ioff()
                box(r[0], r[1], r[2], r[3], 'b', lw=1.5)
            if found:
                box(rb[0], rb[1], rb[2], rb[3], 'g', lw=1.5)
            else:
                box(rb[0], rb[1], rb[2], rb[3], 'r', lw=1.5)
            pylab.draw()
            pylab.show()
            rect = []
            raw_input()

    return tp, fp, thr, tot
def orig_adv_dist(orig_img=None,
                  target_img=None,
                  plot=False,
                  bestC=None,
                  iteration=1):
    if orig_img is None:
        orig_img = np.random.randint(0, len(test_x))
        #orig_img = np.random.randint(0, len(test_x_app))
    if target_img is None:
        target_label = test_y[orig_img]
        #target_label = test_y_app[orig_img]
        while target_label == test_y[orig_img]:
            #while target_label == test_y_app[orig_img]:
            target_img = np.random.randint(0, len(test_x))
            target_label = test_y[target_img]
            #target_img = np.random.randint(0, len(test_x_app))
            #target_label = test_y_app[target_img]

    noise_dist = []
    orig_dist = []
    adv_dist = []
    target_recon_dist = []
    recon_dist = []
    orig_target_dist = []
    orig_target_recon_dist = []
    target_orig_recon_dist = []

    C = np.logspace(-20, 20, 100, base=2, dtype=np.float32)

    for c in C:
        noise, od, ad, ore, tre, recd, otd, otrd, tord = adv_test(orig_img,
                                                                  target_img,
                                                                  C=c,
                                                                  plot=False)
        noise_dist.append(noise)
        orig_dist.append(od)
        adv_dist.append(ad)
        target_recon_dist.append(tre)
        recon_dist.append(recd)
        orig_target_dist.append(otd)
        orig_target_recon_dist.append(otrd)
        target_orig_recon_dist.append(tord)

    noise_dist = np.array(noise_dist)
    orig_dist = np.array(orig_dist)
    adv_dist = np.array(adv_dist)
    target_recon_dist = np.array(target_recon_dist)
    recon_dist = np.array(recon_dist)
    orig_target_dist = np.array(orig_target_dist)
    orig_target_recon_dist = np.array(orig_target_recon_dist)
    target_orig_recon_dist = np.array(target_orig_recon_dist)

    if bestC is None:
        bestC = C[np.argmax(adv_dist - orig_dist >= 0) - 1]

    #print (orig_img, target_img, bestC)

    best_noise, best_orig_dist, best_adv_dist, orig_reconstruction_dist, target_reconstruction_dist, _, _, _, _ = adv_test(
        orig_img, target_img, C=bestC, plot=True)

    plt.ioff()
    if plot:
        fig = plt.figure()
        plt.axhline(y=target_reconstruction_dist,
                    linewidth=2,
                    color='cyan',
                    label="Dist(Target reconstruction - Target)")
        plt.axvline(x=orig_reconstruction_dist,
                    linewidth=2,
                    color='DarkOrange',
                    label="Dist(Original reconstruction - Original)")
        plt.scatter(orig_dist, adv_dist)
        plt.scatter([best_orig_dist], [best_adv_dist], color="red")
        plt.xlabel("Dist(recon Adversarial image, Original image)")
        plt.ylabel("Dist(recon Adversarial image, Target image)")
        plt.legend()
        plt.plot()
        #output_dir = '/Users/rishikaagarwal/Desktop/cs597/adv_vae-master/results/' + model_filename + '/'
        output_dir = 'results/' + model_filename + '/'
        #os.path.join(output_dir, {}/exp_'+ str(iteration)+ '.png')
        fig.savefig(
            os.path.join(output_dir, ('exp_' + str(iteration) + 'graph1.png')))
        plt.close(fig)

        fig = plt.figure()
        plt.axhline(y=target_reconstruction_dist,
                    linewidth=2,
                    color='cyan',
                    label="Dist(Target reconstruction - Target)")
        plt.axvline(x=np.linalg.norm(test_x[orig_img] - test_x[target_img]),
                    linewidth=2,
                    color='DarkOrange',
                    label="Dist(Original - Target)")
        plt.scatter(noise_dist, adv_dist)
        plt.scatter([best_noise], [best_adv_dist], color="red")
        plt.ylabel("Dist(reconAdversarial image, Target image)")
        plt.xlabel("noise")
        plt.legend()
        fig.savefig(
            os.path.join(output_dir, ('exp_' + str(iteration) + 'graph2.png')))
        plt.plot()
        plt.close(fig)

        #compute AUDDC

    #orig_img : index of original image
    #target_img : index of target image
    #bestC:
    #orig_reconstruction_dist : distance b/w original image and its recons
    #target_rec_dist :  distance b/w original image and its recons
    #noise dist :
    #orig_dist :
    #adv_dist :

    xs = noise_dist / orig_target_dist
    zs = ((adv_dist - target_recon_dist) /
          (orig_target_recon_dist - target_recon_dist))
    idx = np.argsort(xs)
    xs = xs[idx]
    zs = zs[idx]
    (xs, zs) = zip(*[(x, z) for (x, z) in zip(xs, zs) if x >= 0 and x <= 1])
    points = (xs, zs)
    limits = (1.0, 1.0, 0)
    AUDDC = metrics_auc(points, limits)

    bestCind = np.where(C == bestC)

    print('orig_img : ', orig_img)
    print('target_img : ', target_img)
    #print('orig_reconstruction_dist : ', orig_reconstruction_dist)
    #print('target_reconstruction_dist : ',target_reconstruction_dist)
    #print('original_target_dist : ', orig_target_dist[bestCind])
    #print('orig_target_recon_dist : ', orig_target_recon_dist[bestCind])
    #print('target_orig_recon_dist : ', target_orig_recon_dist[bestCind])

    print()
    print('bestC : ', bestC)
    print('adv_adv_recon_dist : ', recon_dist[bestCind])
    print('best noise_dist :  ', noise_dist[bestCind])
    print('best orig_dist :  ', orig_dist[bestCind])
    print('best adv_dist : ', adv_dist[bestCind])
    print('AUDDC: ', AUDDC)
    print()

    df = pd.DataFrame({
        'orig_img': orig_img,
        'target_img': target_img,
        'bestC': bestC,
        'orig_reconstruction_dist': orig_reconstruction_dist,
        'target_reconstruction_dist': target_reconstruction_dist,
        'noise_dist': noise_dist,
        'orig_dist': orig_dist,
        'adv_dist': adv_dist,
        'target_recon_dist': target_recon_dist,
        'recon_dist': recon_dist,
        'orig_target_dist': orig_target_dist,
        'orig_target_recon_dist': orig_target_recon_dist,
        'target_orig_recon_dist': target_orig_recon_dist,
        'C': C,
        'AUDDC': AUDDC,
        'best_noise': best_noise
    })

    return df
示例#59
0
def VOCprRecord_wrong(gtImages,
                      detlist,
                      show=False,
                      usetr=True,
                      usedf=False,
                      ovr=0.5):
    """
        calculate the precision recall curve
    """
    dimg = {}
    tot = 0
    for idx in range(len(gtImages)):
        rect = gtImages[idx]["bbox"][:]
        #if idx>288:
        #    print idx,rect
        if rect != []:
            #print gtImages.getImageName(idx).split("/")[-1].split(".")[0]
            dimg[gtImages[idx]["name"].split("/")[-1].split(".")[0]] = {
                "bbox": rect,
                "det": [False] * len(rect)
            }
        tot = tot + len(rect)
    imname = []
    cnt = 0
    tp = numpy.zeros(len(detlist))
    fp = numpy.zeros(len(detlist))
    thr = numpy.zeros(len(detlist))
    detlist.sort(cmpscore)
    for idx, detbb in enumerate(detlist):
        #print detbb[1]
        found = False
        maxovr = 0
        #gtdet=[False]
        gt = 0
        if dimg.has_key(detbb[0]):
            rect = dimg[detbb[0]]["bbox"]
            found = False
            for ir, r in enumerate(rect):
                #gtdet.append(False)
                rb = (float(detbb[3]), float(detbb[2]), float(detbb[5]),
                      float(detbb[4]))
                #print "GT:",r
                #print "DET:",rb
                covr = overlap(rb, r)
                if covr >= maxovr:
                    maxovr = covr
                    gt = ir
                    #dimg[detbb[0]].remove(r)
                    #found=True
                    #break
        if maxovr > ovr:
            #if not(dimg[detbb[0]]["det"][gt]):
            tp[idx] = 1
            #dimg[detbb[0]]["det"][gt]=True
            #else:
            #    fp[idx]=1
        else:
            fp[idx] = 1
        thr[idx] = detbb[1]
        if show:
            prec = numpy.sum(tp) / float(numpy.sum(tp) + numpy.sum(fp))
            rec = numpy.sum(tp) / tot
            print "Scr:", detbb[1], "Prec:%.3f" % prec, "Rec:%.3f" % rec
            ss = raw_input()
            if ss == "s" or not (found):
                pylab.ioff()
                img = gtImages.getImageByName2(detbb[0])
                pylab.figure(1)
                pylab.clf()
                pylab.imshow(img)
                rb = (float(detbb[3]), float(detbb[2]), float(detbb[5]),
                      float(detbb[4]))
                for r in rect:
                    pylab.figure(1)
                    pylab.ioff()
                    box(r[0], r[1], r[2], r[3], 'b', lw=1.5)
                if found:
                    box(rb[0], rb[1], rb[2], rb[3], 'g', lw=1.5)
                else:
                    box(rb[0], rb[1], rb[2], rb[3], 'r', lw=1.5)
                pylab.draw()
                pylab.show()
                rect = []

    return tp, fp, thr, tot
示例#60
0
def viewDet(gtImages,
            detfile,
            opt="all",
            usetr=True,
            usedf=False,
            stop=True,
            t=0.5):
    detf = open(detfile, "r")
    detect = detf.readlines()
    detlst = numpy.zeros((len(detect), 5))
    namelst = []
    pylab.ioff()
    for id, el in enumerate(detect):
        aux = el.split()
        namelst.append(aux[0])
        detlst[id, :] = aux[1:]
    srt = numpy.argsort(-detlst[:, 0])
    imname = []
    cnt = 0
    ovr = 0.49
    #print trPosImages.getTotal()
    tp = []
    fp = []
    tot = 0
    pylab.figure()
    bb = numpy.zeros((4))
    for id in range(detlst.shape[0]):
        pylab.ioff()
        abb = detlst[srt[id]]
        conf = abb[0]
        bb[0] = abb[2]
        bb[1] = abb[1]
        bb[2] = abb[4]
        bb[3] = abb[3]
        pylab.clf()
        img = gtImages.getImageByName2(namelst[srt[id]])
        gtbb = gtImages.getBBoxByName(namelst[srt[id]],
                                      usetr=usetr,
                                      usedf=usedf)
        found = False
        for l in range(len(gtbb)):
            pylab.imshow(img)
            pylab.title("%s Confidence: %f" % (namelst[srt[id]], float(conf)))
            #box(gtbb[l][0],gtbb[l][1],gtbb[l][2],gtbb[l][3],col='b',lw="2")
            print overlap(bb[:], gtbb[l][:4])
            if overlap(bb[:], gtbb[l][:4]) > 0:
                if overlap(bb[:], gtbb[l][:4]) > ovr:
                    box(gtbb[l][0],
                        gtbb[l][1],
                        gtbb[l][2],
                        gtbb[l][3],
                        col='y',
                        lw="2")
                    box(bb[0], bb[1], bb[2], bb[3], col='g', lw="2")
                    pylab.show()
                    pylab.draw()
                    if stop:
                        raw_input()
                    else:
                        time.sleep(t)
                    found = True
                else:
                    box(gtbb[l][0],
                        gtbb[l][1],
                        gtbb[l][2],
                        gtbb[l][3],
                        col='y',
                        lw="1")
                    #box(bb[0],bb[1],bb[2],bb[3],col='g',lw="2")
                    #raw_input()
            else:
                pass
                #pylab.imshow(img)
                #box(bb[0],bb[1],bb[2],bb[3],col='r',lw="2")
        if not (found):
            pylab.imshow(img)
            box(bb[0], bb[1], bb[2], bb[3], col='r', lw="2")
            pylab.show()
            pylab.draw()
            if stop:
                raw_input()
            else:
                time.sleep(t)