Beispiel #1
0
   def __call__(self, **params):

       p = ParamOverrides(self, params)
       fig = plt.figure(figsize=(5, 5))

       # This one-liner works in Octave, but in matplotlib it
       # results in lines that are all connected across rows and columns,
       # so here we plot each line separately:
       #   plt.plot(x,y,"k-",transpose(x),transpose(y),"k-")
       # Here, the "k-" means plot in black using solid lines;
       # see matplotlib for more info.
       isint = plt.isinteractive() # Temporarily make non-interactive for
       # plotting
       plt.ioff()
       for r, c in zip(p.y[::p.skip], p.x[::p.skip]):
           plt.plot(c, r, "k-")
       for r, c in zip(np.transpose(p.y)[::p.skip],np.transpose(p.x)[::p.skip]):
           plt.plot(c, r, "k-")

       # Force last line avoid leaving cells open
       if p.skip != 1:
           plt.plot(p.x[-1], p.y[-1], "k-")
           plt.plot(np.transpose(p.x)[-1], np.transpose(p.y)[-1], "k-")

       plt.xlabel('x')
       plt.ylabel('y')
       # Currently sets the input range arbitrarily; should presumably figure out
       # what the actual possible range is for this simulation (which would presumably
       # be the maximum size of any GeneratorSheet?).
       plt.axis(p.axis)

       if isint: plt.ion()
       self._generate_figure(p)
       return fig
def plot_movie(x,t,u,NN,**kwargs):
	"""
	     x  | Spatial coordinate vector,  len (Nx)
	     t  | Temporal coordinate vector, len (Nt)
	  data  | The data in matrix form,    len (Nt,Nx)
	    NN  | integer giving interval of plotting.
	
	"""
	N,M = shape(u)
	clf()
	ion()
	line, = plot(x,u[0,:],'k-',label=parse_kwargs('label','$Wave$ $equation:$ $BW$',**kwargs),
		linewidth=2)
	plot(x,u[0,:],color='gray',alpha=0.75)
	line.axes.set_ylim(parse_kwargs('miny',-1,**kwargs),parse_kwargs('maxy',1,**kwargs))
	legend(loc=0)
	xlabel(parse_kwargs('xlabel','$x$',**kwargs))
	ylabel(parse_kwargs('ylabel','$u$',**kwargs))
	grid(True)
	for i in range(0,N,N/NN):
		title('$t={}$'.format(t[i]))
		line.set_ydata(u[i,:])
		plot(x,u[i,:],color='gray',alpha=0.2)
		xlim([min(x),max(x)])
		draw()
	line.set_ydata(u[-1,:])
	
	title('$t={}$'.format(t[-1]))
Beispiel #3
0
def main():
    # u_t = u_xx
    dx = .1
    dt = .5
    timesteps = 100000

    x = np.arange(-10,10,dx)
    m = len(x)
    kappa = 50

    # u''(x) = (u(x + dx) - 2u(x) + u(x - dx)) / dx^2
    ones = lambda x: np.ones(x)
    A = np.diag(ones(m-1),k=-1) + -2*np.diag(ones(m)) + np.diag(ones(m-1),k=1)
    A *= kappa*(dx**2)

    U = 0*ones(m)
    for i in xrange(0,m):
        if x[i] > -2 and x[i] < 2:
            U[i] = 1

    p.ion()
    lines, = p.plot(x,U)
    for n in xrange(0,timesteps):
        U = U + dt*dudt(U,A)
        
        if n % 100 == 0:
            lines.set_ydata(U)
            p.draw()
    p.show()
Beispiel #4
0
def plot_average(filenames, save_plot=True, show_plot=False, dpi=100):

    ''' Plot Signal average from a list of averaged files. '''

    fname = get_files_from_list(filenames)

    # plot averages
    pl.ioff()  # switch off (interactive) plot visualisation
    factor = 1e15
    for fnavg in fname:
        name = fnavg[0:len(fnavg) - 4]
        basename = os.path.splitext(os.path.basename(name))[0]
        print fnavg
        # mne.read_evokeds provides a list or a single evoked based on condition.
        # here we assume only one evoked is returned (requires further handling)
        avg = mne.read_evokeds(fnavg)[0]
        ymin, ymax = avg.data.min(), avg.data.max()
        ymin *= factor * 1.1
        ymax *= factor * 1.1
        fig = pl.figure(basename, figsize=(10, 8), dpi=100)
        pl.clf()
        pl.ylim([ymin, ymax])
        pl.xlim([avg.times.min(), avg.times.max()])
        pl.plot(avg.times, avg.data.T * factor, color='black')
        pl.title(basename)

        # save figure
        fnfig = os.path.splitext(fnavg)[0] + '.png'
        pl.savefig(fnfig, dpi=dpi)

    pl.ion()  # switch on (interactive) plot visualisation
def count_barcodes(dataset, VERBOSE=0):
    '''Count the abundance of each barcode'''

    # Get the read filenames
    data_filenames = get_raw_read_files(dataset)
    datafile = data_filenames['adapter']

    # Count the abundance of each barcode
    bc_counts = defaultdict(int)
    rc = 0
    with open(datafile, 'r') as infile:
        for read in SeqIO.parse(infile, 'fastq'):
            bc_counts[read.seq.tostring()] += 1
            rc += 1
            if rc == maxreads:
                break
    
    print sorted(bc_counts.items(), key=lambda x:x[1], reverse=True)[:20]
    
    # Plot results
    plt.figure()
    ax=plt.subplot(111)
    plt.plot(range(1,len(bc_counts)+1), sorted(bc_counts.values(), reverse=True))
    ax.set_yscale('log')
    ax.set_xscale('log')
    plt.xlabel('barcode rank')
    plt.ylabel('abundance')

    plt.ion()
    plt.show()
Beispiel #6
0
def label_data(prefix, size=100, savename=None):
    from glob import glob
    from os.path import basename
    from PIL import Image
    from os.path import isfile
    if savename==None: savename=labelpath+'label_'+prefix+'.txt'
    # We want to avoid labeling an image twice, so keep track
    # of what we've labeled in previous labeling sessions.
    if isfile(savename):
        fileout = open(savename,'r')
        already_seen = [line.split(',')[0] for line in fileout]
        fileout.close()
    else: already_seen = []
    # Now reopen the file for appending.
    fileout = open(savename,'a')
    pl.ion()
    pl.figure(1,figsize=(9,9))
    files = glob(imgpath+prefix+'*.png')
    for file in np.random.choice(files, size=size, replace=False):
        if basename(file) in already_seen: continue
        pl.clf()
        pl.subplot(1,1,1)
        pl.imshow(np.array(Image.open(file)))
        pl.title(file)
        pl.axis('off')
        pl.draw()
        label = get_one_char()
        if label=='q': break
        fileout.write(basename(file)+','+label+'\n')
        print file,label
    fileout.close()
    return
Beispiel #7
0
def kmr_test_plot(data, k, end_thresh):
    from matplotlib.pylab import ion, figure, draw, ioff, show, plot, cla
    ion()
    fig = figure()
    ax = fig.add_subplot(111)
    ax.grid(True)

    # get k centroids
    kmr = kmeans.kmeans_runner(k, end_thresh)
    kmr.init_data(data)
    print kmr.centroids

    plot(data[:,0], data[:,1], 'o')

    i = 0
    while kmr.stop_flag is False:
        kmr.iterate()
        #print kmr.centroids, kmr.itr_count
        plot(kmr.centroids[:, 0], kmr.centroids[:, 1], 'sr')
        time.sleep(.2)
        draw()
        i += 1

    print "N Iterations: %d" % (i)
    plot(kmr.centroids[:, 0], kmr.centroids[:, 1], 'g^', linewidth=3)

    ioff()
    show()
    print kmr.itr_count, kmr.centroids
Beispiel #8
0
def plot(y, function):
    """ Show an animation of Poincare plot.

    --- arguments ---
    y: A list of initial values
    function: function which is argument of Runge-Kutta solver
    """
    h = dt
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.grid()
    time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
    plt.ion()

    for i in range(nmax + 1):
        for j in range(nstep):
            rk4 = RK.RK4(function)
            y = rk4.solve(y, j * h, h)
            # -pi <= theta <= pi
            while y[0] > pi:
                y[0] = y[0] - 2 * pi
            while y[0] < -pi:
                y[0] = y[0] + 2 * pi

        if ntransient <= i < nmax:          # <-- draw the poincare plots
            plt.scatter(y[0], y[1], s=2.0, marker='o', color='blue')
            time_text.set_text('n = %d' % i)
            plt.draw()

        if i == nmax:                       # <-- to stop the interactive mode
            plt.ioff()
            plt.scatter(y[0], y[1], s=2.0, marker='o', color='blue')
            time_text.set_text('n = %d' % i)
            plt.show()
Beispiel #9
0
def rf_sub_size(Input, net_size, resolution):
    size = np.zeros((net_size*net_size,))
    coms = np.zeros((net_size*net_size, 2))
    R = np.zeros((net_size*resolution, net_size*resolution))
    Z = np.zeros((resolution, resolution))

    scale = 1.0/(resolution**2)

    X, Y = np.meshgrid(np.arange(Z.shape[0]), np.arange(Z.shape[1]))

    count_roi, count_nroi, count_tot = 0, 0, 0
    plt.ion()
    for i in range(net_size):
        for j in range(net_size):
            Z = np.abs(Input[i, j, ...] * (Input[i, j, ...] > 0) +
                       0.0 * (Input[i, j, ...] < 0))

            R[i*resolution:(i+1)*resolution, j*resolution:(j+1)*resolution] = Z
            size[i*net_size+j] = area_of_activation(Z) * scale

            d = np.unravel_index(Z.argmax(), Z.shape)
            Z = np.roll(Z, Z.shape[0]/2-d[0], axis=0)
            Z = np.roll(Z, Z.shape[1]/2-d[1], axis=1)

            xc = ((Z*Y).sum()/Z.sum() - Z.shape[0]/2 + d[0])/float(Z.shape[0])
            yc = ((Z*X).sum()/Z.sum() - Z.shape[1]/2 + d[1])/float(Z.shape[1])

            coms[i*net_size+j, 0] = (xc+1.0) % 1
            coms[i*net_size+j, 1] = (yc+1.0) % 1
    return coms, R, size
Beispiel #10
0
    def do_fit_trans(self):
        f = self.fit_trans
        p0 = self.p0.copy()
        #        p0[:-3]=0.0
        #print p0,"call d0"
        #d0 = f(p0)
        if 0:
         for i in range(len(p0)):
            pt = p0.copy()
            pt[i] = p0[i]+0.001
            from matplotlib.pylab import clf, ion, title, plot, show
            print pt - p0, pt
            ion()
            clf()
            title("%d"%(i))
            plot(d0, f(pt) - d0, ",")
            show()
            if raw_input()[0] != " ": 
                break


        res = scipy.optimize.leastsq( f, p0, full_output=1)
        pfit, pcov, info, errmsg, ier = res
        if ier not in [1,2,3,4]:
            print s_sq, ier, errmsg
        else:
            residu = f(pfit) 
            s_sq = (residu**2).sum()/(len(residu)-len(p0))
        ubi = pfit[:9].reshape(3,3)
        print ("%.6f "*6)%(indexing.ubitocellpars(ubi))
        print pfit[9:12]
        self.g = grain( ubi, pfit[9:12].copy())
Beispiel #11
0
def example():
#    pl.ioff()
    pl.ion()

    import pandas
    from numpy.random import uniform

    n = 25
    m = pandas.DataFrame({
            'x': uniform(-1, 1, size=n),
            'y': uniform(-1, 1, size=n),
            'size': uniform(3, 10, size=n) ** 2,
            'color': uniform(0, 1, size=n),
    })

    # test using a custom index
    m['silly_index'] = ['%sth' % x for x in range(n)]
    m.set_index('silly_index', drop=True, inplace=True, verify_integrity=True)

    print m

    ax = pl.subplot(111)
    plt = ax.scatter(m['x'], m['y'])

    b = LassoBrowser(m, ax)
    print b.idxs

    #from viz.interact.pointbrowser import PointBrowser
    #pb = PointBrowser(m, plot=plt)

    pl.show()

    ip()
Beispiel #12
0
def demo():
    '''
    Load and plot a few CIB spectra.
    '''

    # define ell array.
    l = np.arange(100,4000)

    # get dictionary of CIBxCIB spectra.
    cl_cibcib = get_cl_cibcib(l)

    # plot
    import matplotlib.pylab as pl
    pl.ion()
    lw=2
    fs=18
    leg = []
    pl.clf()
    for band in ['857','545','353']:
        pl.semilogy(l, cl_cibcib['545',band],linewidth=lw)
        leg.append('545 x '+band)
    pl.xlabel(r'$\ell$',fontsize=fs)
    pl.ylabel(r'$C_\ell^{TT, CIB} [\mu K^2]$',fontsize=fs)
    pl.ylim(5e-2,6e3)
    pl.legend(leg, fontsize=fs)
Beispiel #13
0
    def plot_diagnostics(self):
        if 0:
            from matplotlib import pylab as plt
            plt.ion()
            plt.figure()

        _phot = phot.read_fits(self.lcfn,'optimum')
        with self.FigureManager('_0-aperture'):
            plotting.phot.aperture(_phot)

        with self.FigureManager('_1-background'):
            plotting.phot.background(_phot)

        if len(self.dfaper.npix.drop_duplicates()) > 1:
            with self.FigureManager('_2-noise_vs_aperture_size'):
                plotting.pipeline.noise_vs_aperture_size(self)

        with self.FigureManager("_3-fdt_t_roll_2D"):
            plotting.phot.detrend_t_roll_2D(_phot)

        with self.FigureManager("_4-fdt_t_roll_2D_zoom"):
            plotting.phot.detrend_t_roll_2D(_phot,zoom=True)

        with self.FigureManager("_5-fdt_t_rollmed"):
            plotting.phot.detrend_t_rollmed(_phot)
Beispiel #14
0
    def eqDistribution(self, plot=True):
        """ Obtain and plot the equilibrium probabilities of each macrostate

        Parameters
        ----------
        plot : bool, optional, default=True
            Disable plotting of the probabilities by setting it to False

        Returns
        -------
        eq : ndarray
            An array of equilibrium probabilities of the macrostates

        Examples
        --------
        >>> model = Model(data)
        >>> model.markovModel(100, 5)
        >>> model.eqDistribution()
        """
        self._integrityCheck(postmsm=True)
        macroeq = np.ones(self.macronum) * -1
        for i in range(self.macronum):
            macroeq[i] = np.sum(self.msm.stationary_distribution[self.macro_ofmicro == i])

        if plot:
            from matplotlib import pylab as plt
            plt.ion()
            plt.figure()
            plt.bar(range(self.macronum), macroeq)
            plt.ylabel('Equilibrium probability')
            plt.xlabel('Macrostates')
            plt.xticks(np.arange(0.4, self.macronum+0.4, 1), range(self.macronum))
            plt.show()
        return macroeq
    def matrix_plot(self, matrix, figure_name='matrix_plot.pdf'):
        import numpy
        from matplotlib import pylab
        def _blob(x,y,area,colour):
            hs = numpy.sqrt(area) / 2
            xcorners = numpy.array([x - hs, x + hs, x + hs, x - hs])
            ycorners = numpy.array([y - hs, y - hs, y + hs, y + hs])
            pylab.fill(xcorners, ycorners, colour, edgecolor=colour)
        reenable = False
        if pylab.isinteractive():
            pylab.ioff()
        pylab.clf()
        
        maxWeight = 2**numpy.ceil(numpy.log(numpy.max(numpy.abs(matrix)))/numpy.log(2))
        height, width = matrix.shape
        pylab.fill(numpy.array([0,width,width,0]),numpy.array([0,0,height,height]),'white')
        pylab.axis('off')
        pylab.axis('equal')
        for x in xrange(width):
            for y in xrange(height):
                _x = x+1
                _y = y+1
                w = matrix[y,x]
                if w > 0:
                    _blob(_x - 0.5, height - _y + 0.5, 0.2,'#0099CC')
                elif w < 0:
                    _blob(_x - 0.5, height - _y + 0.5, 0.2,'#660000')

        if reenable:
            pylab.ion()
        pylab.savefig(figure_name) 
Beispiel #16
0
def integral_plots(vals, new_vals, n):
    
    """
    Parameters
    -------------------------------------------------------
        vals:   non-normalized values of the table
        
        new_vals: normalized values of the table
                  such that the maximum of each vertical
                  column is 1.
        
        n: number of points in theta and omega arrays
        (so the size of the 2D table is n x n)
        
        
    Return
    -------------------------------------------------------
        Plots a density plot of the table using imshow
        of the normalized table and also plots the integral 
        of the table along each axis (these are the integrals
        of the non-normalized values)
    """

    import matplotlib.gridspec as gridspec
    
    omega_par = np.linspace(0.1,0.4,n)
    theta_par = np.linspace(0.01,1.4,n)
    
    oo = []
    tt = []
    
    # integral in left side (sum of each row)
    for i in range(n):
        oo.append(sum(vals[i]))
    
    # integral in bottom side (sum of columns)
    for i in range(n):
        tt.append(sum(vals.T[i]))

    plt.ion()
    plt.figure(figsize=(11,9))

    gs  = gridspec.GridSpec(2, 2, width_ratios=[1,4],height_ratios=[4,1])
    ax1 = plt.subplot(gs[0])
    ax1.plot(oo,omega_par,  linewidth=2)
    plt.ylabel(r"$\Omega_{||}$", fontsize=20)

    ax2 = plt.subplot(gs[1])
    plt.imshow(new_vals, interpolation='nearest', origin="lower", extent=(theta_par.min(), theta_par.max(), omega_par.min(), omega_par.max()),aspect='auto')
    #plt.imshow(vals, interpolation='nearest', origin="lower", extent=(theta_par.min(), theta_par.max(), omega_par.min(), omega_par.max()),aspect='auto')
    plt.colorbar()

    plt.title("$P(t_s) \propto t_s $, maximum normalized to 1", fontsize=15)


    ax4 = plt.subplot(gs[3])
    ax4.plot(theta_par,tt, linewidth=2)
    plt.xlabel(r"$\theta_{||}$",fontsize=20)
Beispiel #17
0
def plot_pol_info_pat(problem_id, pol, times):
    fig, axes = plt.subplots(nrows=len(times), ncols=1)
    for idx, time in enumerate(times):
        df = pd.DataFrame(pol[:, :, time])
        df.plot(title='{0:s}: Whether to sell, depending on current price; time = {1:d}'.format(problem_id, time),
                     ax=axes[idx])
        axes[idx].set_xlabel('Number of stocks in inventory')
        axes[idx].set_ylabel('1->sell; 0->hold')
    plt.ion()
def assignpeaks( gr, pars, colfile, tol=None, omegatol=0.2 ):
    labels = np.zeros( colfile.nrows )
    cyf = cyfit.cyfit(pars)
    cyf.setscfc( colfile.sc, colfile.fc )
    hkl = np.zeros( cyf.XL.shape, np.float)
    drlv = np.zeros( colfile.nrows, np.float)
    kcalc = np.zeros( cyf.XL.shape, np.float)
    r_omega  = np.array(colfile.omega*np.pi/180.0,np.float)
    cyf.hkl( colfile.sc, colfile.fc, r_omega, gr, hkl, kcalc )
    # Make integer hkl
    wripaca.ih_drlv( hkl, drlv )
    # Now replace omegaobs by omegacalc where this is appropriate
    pre = np.eye(3).ravel()
    posti = np.dot(gv_general.wedgemat(pars.get('wedge')), 
                     gv_general.chimat(pars.get('chi'))).T.ravel()
    axis = np.array([0,0,-1],np.float)
    ub = np.linalg.inv(gr.ubi)
    gcalc = np.dot( ub, hkl.T ).T.copy()
    #print hkl
    romegacalc = np.zeros( r_omega.shape, np.float)
    romegaerr  = np.zeros( r_omega.shape, np.float)
    wripaca.omegacalcclose(gcalc,
                           pre,
                           posti,
                           axis,
                           r_omega,
                           romegacalc,
                           romegaerr,
                           pars.get('wavelength'),
                           )
    # OK, now we will accept anything within omegatol
    if 0:
        pylab.hist(romegaerr, bins=50)
        pylab.figure()
        pylab.plot(r_omega, romegaerr,",")
        pylab.show()
    r_omega = np.where( romegaerr < omegatol*np.pi/180.,  romegacalc, r_omega )
    cyf.hkl( colfile.sc, colfile.fc, r_omega, gr, hkl, kcalc )
    drlv_omegafree = drlv.copy()
    wripaca.ih_drlv( hkl, drlv_omegafree )
    m = drlv < tol
    sc = colfile.sc[m]
    fc = colfile.fc[m]
    omegaobs = colfile.omega[m].astype(np.float)
    omega = colfile.omega[m].astype(np.float)
    hkl = hkl[m]
    print "Got",m.sum(),"peaks",
    if 0:
        pylab.ion()
        pylab.title("tol = %f"%(tol))
        pylab.hist( drlv, bins=np.arange(0,0.05,0.001))
        pylab.hist( drlv_omegafree, bins=np.arange(0,0.05,0.001))
        pylab.show()
        raw_input("OK?")

    return dummycf( sc, fc, omega, omegaobs, hkl )
 def begin_draw():
   pl.ion()
   pl.figure( 1, figsize = ( 20, 20 ), dpi = 50 )
   pl.clf()
   pl.axis( "scaled" )
   pl.xlim( -4, 4 )
   pl.ylim( -2, 6 )
   pl.plot( [d[0] for d in data], [d[1] for d in data], "b." )
   for m in xrange( clusters ):
     plot_gaussian( mu[m], sigma[m], "r" )
Beispiel #20
0
def normalized_contour_max1(ptype, n):
    
    """
    Parameters
    -------------------------------------------------------
        ptype: type of the probability. It can be either
                "Gauss", "one_over_ts" or "ts"
                
        n: number of points in theta and omega arrays
    
    Return
    -------------------------------------------------------
        val:     non-normalized values of the 2D table.
        
        new_val: normalized values fo the 2D table
                 such that the maximum value of each
                 vertical column is 1.
                 
        It also plots the normalized density plot.
    """
    
    omega_par = np.linspace(0.1,0.4,n)
    theta_par = np.linspace(0.01,1.4,n)
    
    
    vals     = np.zeros((n,n))
    Nlist    = []
    new_vals = []


    # calculating the 2D table of distribution
    for i in range(n):
        for j in range(n):
            vals[i][j] = prob_Oparapar(5.,i, j, n, ptype)


    # calculating the normalization A
    for i in range(n):
        xx = (np.max(vals.T[i]))
        Nlist.append(1./xx)

    # calculating the normalized values
    for i in range(n):
        xx = vals.T[i] * Nlist[i]
        new_vals.append(xx)

    new_vals = np.array(new_vals)

    plt.ion()
    plt.imshow(new_vals.T, interpolation='nearest', origin="lower", extent=(theta_par.min(), theta_par.max(), omega_par.min(), omega_par.max()),aspect='auto')

    plt.xlabel(r"$\theta_{||}$",fontsize=20)
    plt.ylabel(r"$\Omega_{||}$", fontsize=20)
    plt.title("$p(t_s) = {0}$.formar(ptype), max of each column is 1", fontsize=15)
    return vals, new_vals
def debug_eep(track, inds=None, ax=None):
    if inds is None:
        inds = track.iptcri[track.iptcri > 0]
    if ax is None:
        plt.ion()
    ax = hrd(track, ax=ax)
    ax = hrd(track, inds=inds, ax=ax)
    annotate_plot(track, ax, logT, logL)

    plt.legend()
    return ax
Beispiel #22
0
    def plot3d(self):

        fsize3d = 16
        plt.ion()
        fig1 = plt.figure('3D plot')
        ax = p3.Axes3D(fig1)
        ax.set_xlabel(self.xlab, fontsize=fsize3d)
        ax.set_ylabel('Time [s]', fontsize=fsize3d)
        
        ax.plot_wireframe(self.m2g.r_new, self.m2g.t_new, self.m2g.g, linewidth=.3, rstride=1, cstride=3)
        plt.draw()
Beispiel #23
0
    def plotTimescales(self, lags=None, units='frames', errors=None, nits=None, results=False, plot=True):
        """ Plot the implied timescales of MSMs of various lag times

        Parameters
        ----------
        lags : list
            The lag times at which to compute the timescales. By default it spreads out 25 lag times linearly from lag
            10 until the mode length of the trajectories.
        units : str
            The units of lag. Can be 'frames' or any time unit given as a string.
        errors : errors
            Calculate errors using Bayes (Refer to pyEMMA documentation)
        nits : int
            Number of implied timescales to calculate. Default: all
        results : bool
            If the method should return the calculated implied timescales
        plot : bool
            If the method should display the plot of implied timescales

        Returns
        -------
        If given `results`=True this method will return the following data
        its : np.ndarray
            The calculated implied timescales. 2D array with dimensions (len(`lags`), `nits`)
        lags : np.ndarray
            A list of the lag times that were used to calculate the implied timescales

        Examples
        --------
        >>> model = Model(data)
        >>> model.plotTimescales()
        >>> model.plotTimescales(lags=list(range(1,100,5)))
        """
        import pyemma.plots as mplt
        import pyemma.msm as msm
        self._integrityCheck()
        if lags is None:
            lags = self._defaultLags()
        else:
            lags = unitconvert(units, 'frames', lags, fstep=self.data.fstep).tolist()

        if nits is None:
            nits = np.min((self.data.K, 20))

        from htmd.config import _config
        its = msm.its(self.data.St.tolist(), lags=lags, errors=errors, nits=nits, n_jobs=_config['ncpus'])
        if plot:
            from matplotlib import pylab as plt
            plt.ion()
            plt.figure()
            mplt.plot_implied_timescales(its, dt=self.data.fstep, units='ns')
            plt.show()
        if results:
            return its.get_timescales(), its.lags
Beispiel #24
0
 def draw_sinus_move(self):
     #Interactive mode ON
     pylab.ion()
     for n in range(25):
         #next frame:
         self.ylist = [math.sin (x + n / 2.0) for x in self.xlist]
         #clear screen
         pylab.clf()
         #plot new info
         pylab.plot (self.xlist, self.ylist)
         #draw
         pylab.draw()
Beispiel #25
0
    def __init__(self, lock, points):
        threading.Thread.__init__(self)
        self.stop = threading.Event()
        plt.ion()

        self.points = points
        self.lock = lock
        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111)
        self.data, = self.ax.plot([0, 0], [0, 0], "o")
        self.ax.axis([0, 1024, 0, 850])
        self.sleepDelay = 0.03
def plot_init(data):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.set_aspect('equal')
    X,Y=np.meshgrid(np.linspace(0., float(DIMENSION-1),DIMENSION), np.linspace(float(DIMENSION-1),0, DIMENSION))
    ax.plot_surface(X, Y, data)
    plt.title(0)
    plt.ylabel("give")
    plt.xlabel("accept")
    plt.ion()
    plt.show()
    return fig ,ax
def plot_pitch_matrix(pitch_matrix, title=None, xlabel=None, ylabel=None):
	fig, ax = plt.subplots(figsize=(21,8))
	heatmap = ax.pcolor(pitch_matrix, cmap=plt.cm.Blues)
	plt.title(title, fontsize = 24)
	plt.ylabel(ylabel, fontsize = 24)
	plt.xlabel(xlabel, fontsize = 24)
	ax = plt.gca()
	ax.set_xlim([0, pitch_matrix.shape[1]])
	ax.set_ylim([0, pitch_matrix.shape[0]])
	
	plt.ion()
	plt.show()
Beispiel #28
0
    def pltciri(self,itx,irx):
        """
        plot channel impulse response interactively.
        display all nodes position of Tx and Rx and choose 
        for which link the CIR is displayed.

        Once self.pltciri(node1,node2) is called : 

        1) Press 't' on the displayed graph to chose the Tx
        2) Press 'x' on the displayed graph to chose the Rx
        3) Press Enter to display CIR betwen Tx-Rx


        Parameters
        ----------
            itx : int
                node number
            irx : int
                node number

        usage
        >>> W=W2()
        >>> W.pltciri(6,1)

        """
        plt.ion()
        fig1 = plt.figure(1)
        ax=fig1.add_subplot(111)
        self.ax2 = fig1.add_subplot(111)
        self.L.showG(fig=fig1,graph='')
        self.S.tx = RadioNode(typ='tx',name=itx)
        self.S.tx.loadini(str(itx)+'.ini',rep=pstruc['DIRNETSAVE'])
        self.S.rx = RadioNode(typ='rx',name=irx)
        self.S.rx.loadini(str(irx)+'.ini',rep=pstruc['DIRNETSAVE'])
        ax.plot(self.S.tx.position[0,:],self.S.tx.position[1,:],'ob')
        ax.plot(self.S.rx.position[0,:],self.S.rx.position[1,:],'or')
        plt.show()
        print( '1. Press \'t\' and click to select a Tx ')
        print( '2. Press \'x\' and click to select a Rx ' ) 
        print( '3. Press Enter to see the associated CIR ' ) 
        self.key=''
        self.x1=''
        self.x2=''
        self.y1=''
        self.y2=''
        self.n1=''
        self.n2=''
        self.pos1=''
        self.pos2=''
        self.c1=[]
        self.c2=[]
        cid=fig1.canvas.mpl_connect('button_press_event', self.onclick)
        cid=fig1.canvas.mpl_connect('key_press_event', self.on_key)
Beispiel #29
0
def run(pixfn, lcfn, transfn, tlimits=[-np.inf,np.inf], tex=None, 
             debug=False, ap_select_tlimits=None):
    """
    Run the pixel decorrelation on pixel file
    """

    pipe = PipelinePixDecor(
           pixfn, lcfn,transfn, tlimits=tlimits, tex=None
           )
    
    pipe.print_parameters()
    pipe.set_lc0('circular',10)
    pipe.set_hyperparameters()
    pipe.reject_outliers()

    dfaper = pipe.get_dfaper_default()
    dfaper = pipe.detrend_dfaper(dfaper)
    dfaper_optimize = pipe.optimize_aperture()
    dfaper = dfaper + dfaper_optimize
    dfaper = pd.DataFrame(dfaper)
    row = dfaper.loc[dfaper.noise.idxmin()].copy()
    row['to_fits'] = True
    row['fits_group'] = 'optimum'
    dfaper = dfaper.append(row, ignore_index=True)
    pipe.dfaper = dfaper
    print dfaper.sort('npix')['fits_group npix noise to_fits'.split()]
    pipe.to_fits(pipe.lcfn)

    if 0:
        from matplotlib import pylab as plt
        plt.ion()
        plt.figure()
        import pdb;pdb.set_trace()

    _phot = phot.read_fits(lcfn,'optimum')
    with pipe.FigureManager('_0-aperture.png'):
        plotting.phot.aperture(_phot)

    with pipe.FigureManager('_1-background.png'):
        plotting.phot.background(_phot)

    with pipe.FigureManager('_2-noise_vs_aperture_size.png'):
        plotting.pipeline.noise_vs_aperture_size(pipe)

    with pipe.FigureManager("_3-fdt_t_roll_2D.png"):
        plotting.phot.detrend_t_roll_2D(_phot)

    with pipe.FigureManager("_4-fdt_t_roll_2D_zoom.png"):
        plotting.phot.detrend_t_roll_2D(_phot,zoom=True)

    with pipe.FigureManager("_5-fdt_t_rollmed.png"):
        plotting.phot.detrend_t_rollmed(_phot)
Beispiel #30
0
def plot_init(data):
    """Initialize plot.

    Args:
        data: team matrix
    """

    # Set figure style
    sns.set_style("dark")

    # Create subplot grid
    fig = gridspec.GridSpec(2, 1)

    # Create subplots
    axarr = [plt.subplot(fig[0, 0]), plt.subplot(fig[1, 0])]

    # Plot data
    img = axarr[0].imshow(data, interpolation = 'nearest', cmap = plt.cm.ocean,
                          extent = (0.5, np.shape(data)[0] + 0.5, 0.5, 
                                    np.shape(data)[1] + 0.5))

    # Display round
    plt.title("Current Round:" + str(0))

    # Set labels
    axarr[0].set_ylabel("Give")
    axarr[0].set_xlabel("Accept")
    axarr[0].set_title("Distribution of Teams")

    # Plot average deal data
    axarr[1].plot(avg_deal_data)

    # Set labels
    axarr[1].set_xlim(0,rounds)
    axarr[1].set_ylabel("Average Cash per Deal")
    axarr[1].set_xlabel("Round Number")

    # Create colorbar for strategy distribution
    plt.colorbar(img, ax=axarr[0], label= "Prevalence vs. Uniform")

    # Interactive mode
    plt.ion()

    # Changed this to use 'normal' instead of 'zoomed' since it didn't work on
    # my system
    mng = plt.get_current_fig_manager()
    mng.window.state('normal')

    # Display everything
    plt.show()

    return fig, axarr
Beispiel #31
0
    def __init__(self, cells, t, solute=None, cmap=default_color_map):
        """
        Creates a new HillPlot on the active figure, showing the state of each layer
         - cells: The a sequence of cmf cells to use in this hill_plot. You can
                  use the whole project if you like
         - t:     Current time step. Needed to retrieve the fluxes
         - solute:The solute concentration to show. If None, the wetness of the 
                  layer will be shown
         - cmap:  a matplotlib colormap (see module cm) for coloring
        """
        was_interactive = pylab.isinteractive()
        if was_interactive:
            pylab.ioff()
        self.cells = cells
        self.layers = cmf.node_list(chain(*[c.layers for c in cells]))
        self.surfacewater = cmf.node_list(c.surfacewater for c in cells)

        x_pos = [self.__x(c.x, c.y) for c in cells]

        self.topline = pylab.plot(x_pos, self.__get_snow_height(), 'k-',
                                  lw=2)[0]

        self.__cells_of_layer = {}
        self.polys = {}

        # Get standard evaluation functions
        if isinstance(solute, cmf.solute):
            self.evalfunction = lambda layer: layer.conc(solute)
        else:
            self.evalfunction = lambda layer: layer.wetness

        self.cmap = cmap
        self.figure = pylab.gcf()

        for i, c in enumerate(cells):
            c_left = cells[i - 1] if i else c
            c_right = cells[i + 1] if i < len(cells) - 1 else c

            for l in c.layers:
                x, z = self.__get_layer_shape(l, c, c_left, c_right)
                self.polys[l.node_id], = pylab.fill(x,
                                                    z,
                                                    fc=self.cmap(
                                                        self.evalfunction(l)),
                                                    ec='none',
                                                    zorder=0)
                self.__cells_of_layer[l.node_id] = (c, c_left, c_right)

        layer_pos = self.layers.get_positions()
        surf_pos = self.surfacewater.get_positions()

        layer_f = self.layers.get_fluxes3d(t)
        surf_f = self.surfacewater.get_fluxes3d(t)
        scale = max(numpy.linalg.norm(surf_f.X), numpy.linalg.norm(
            layer_f.X)) * 10

        layer_x = self.__x(numpy.asarray(layer_pos.X),
                           numpy.asarray(layer_pos.Y))

        surf_x = self.__x(numpy.asarray(surf_pos.X), numpy.asarray(surf_pos.Y))

        self.q_sub = pylab.quiver(layer_x,
                                  layer_pos.Z,
                                  layer_f.X + layer_f.Y,
                                  layer_f.Z,
                                  scale=scale,
                                  minlength=0.1,
                                  pivot='middle',
                                  zorder=1)
        self.q_surf = pylab.quiver(surf_x,
                                   surf_pos.Z,
                                   surf_f.X + surf_f.Y,
                                   surf_f.Z,
                                   color='b',
                                   scale=scale,
                                   minlength=0.1,
                                   pivot='middle',
                                   zorder=1)

        self.title = pylab.title(t)
        if was_interactive:
            pylab.draw()
            pylab.ion()
Beispiel #32
0
def main():
    plt.ion()
    running_reward = 10
    env.reset()
    c1, c2 = [1, 1, 1, 1], [1, 1, 1, 1]
    for i_episode in count(1):
        state = env.reset()
        feedback1 = np.zeros(state.shape)
        feedback2 = np.zeros(state.shape)
        for t in range(10000):  # Don't infinite loop while learning
            # env.render()
            # Begin source coding here
            # encoder:
            encoder_state1 = c1 * state - feedback1
            encoder_state2 = c2 * state - feedback2
            stategy1 = select_strategy1(encoder_state1)  # learn f_encoder
            quantized_state1 = uniform_midtread_quantizer(
                encoder_state1, 1 / 2**stategy1)
            stategy2 = select_strategy2(encoder_state2)  # learn f_encoder
            quantized_state2 = uniform_midtread_quantizer(
                encoder_state2, 1 / 2**stategy2)
            # feedback:
            feedback1 = quantized_state1 + feedback1
            feedback2 = quantized_state2 + feedback2
            # print(feedback)
            control_state = np.stack([feedback1, feedback2]).flatten()
            control_state = np.stack([state, state]).flatten()

            # Controller:
            action = select_action(control_state)  # learn f_decoder
            state, reward, done, _ = env.step(action)
            # reward = reward -(abs(state[2]))
            # if done:
            #     print(reward)
            # finish source coding here
            if args.render:
                env.render()
            rr = reward  #- stategy1*0.1-stategy2*0.1
            policy.rewards.append(rr)
            quantizer1.rewards.append(rr)
            quantizer2.rewards.append(rr)
            if done:
                break

        running_reward = running_reward * 0.99 + t * 0.01

        finish_episode()
        quantizer1_finish_episode()
        quantizer2_finish_episode()

        episode_durations.append(t + 1)
        episode_strategies.append(stategy1 + stategy2)
        plot_strategies()
        plot_durations()  # Plot the durations
        if i_episode % args.log_interval == 0:
            print('Episode {}\tLast length: {:5d}\tAverage length: {:.2f}'.
                  format(i_episode, t, running_reward))
        if running_reward > env.spec.reward_threshold:  #(195)
            print("Solved! Running reward is now {} and "
                  "the last episode runs to {} time steps!".format(
                      running_reward, t))
            break
        if i_episode % 50 == 0:
            folder_path = os.path.join(
                '/home/liang/PycharmProjects/SourceCoding/', 'model')
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)

            torch.save(policy.state_dict(), folder_path + '/policy_state_dict')
            torch.save(quantizer1.state_dict(),
                       folder_path + '/quantizer1_state_dict')
            torch.save(quantizer2.state_dict(),
                       folder_path + '/quantizer2_state_dict')

    torch.save(policy.state_dict(), folder_path + '/policy_state_dict')
    torch.save(quantizer1.state_dict(), folder_path + '/quantizer1_state_dict')
    torch.save(quantizer2.state_dict(), folder_path + '/quantizer2_state_dict')
    print('Complete')
    import pickle
    duration = open('duration2.pickle', 'wb')
    pickle.dump(episode_durations, duration)
    duration.close()
    strategies = open('strategies2.pickle', 'wb')
    pickle.dump(episode_strategies, strategies)
    strategies.close()
    env.close()
    plt.ioff()
    plt.show()
def correlator_online_mp(fileinput='input.txt',
                         dark_file='default',
                         mask_file='default',
                         plot='yes'):
    global nq, n, chn, chn2, rcr, index_in_q, lag, dt, norm, nc, I_avg, I_avg2, lm1, lm2, lm1b, lm2b, ax1, ax1b, ax2, ax2b, nq, detector, ccd_img, flat_field, tot_darks, totmask, ttdata, tcalc_cum, tplot_cum, tread_cum, tI_avg, static_corrected, firstfile, tolerance, I_avgs, xnq, Mythread, l, y, v, input_info, wtotmask, totsaxs, tI_avg, q_2tcf
    time1 = time.time()
    p.rc('image', origin='lower')
    p.rc('image', interpolation='nearest')
    p.close()
    print 'multiprocessor'
    print 'reading input...'
    input_info = get_input(fileinput)
    ##processing input file#####
    dir = input_info['dir']
    dir_dark = input_info['dark dir']
    if dir_dark == 'none':
        dir_dark = dir
    file_prefix = input_info['file_prefix']
    ext = input_info['file_suffix']
    # New version has capabilities of reading also .gz files
    if ext == '.edf.gz':
        dataread = EdfFile.EdfGzipFile
    else:
        dataread = EdfFile.EdfFile
    firstfile = int(input_info['n_first_image'])
    lastfile = int(input_info['n_last_image']) + 1
    firstdark = input_info['n_first_dark']
    if firstdark.lower() != 'none':
        firstdark = int(input_info['n_first_dark'])
        lastdark = int(input_info['n_last_dark']) + 1
    geometry = input_info['geometry'].lower()
    tolerance = float32(float(input_info['tolerance']))
    avgt = input_info['lag time'].lower()
    if avgt == 'auto':
        lagt = []
        lagt1 = 0
        for k in xrange(firstfile + 40, firstfile + 100):
            filename = file_name(dir + file_prefix, ext, k)
            while os.path.exists(filename) is False:
                sys.stdout.write(50 * '\x08')
                sys.stdout.write('file ' + filename + 'still not ready')
                sys.stdout.flush()
                #rint 'file ' ,filename, 'still not ready'
                time.sleep(10)
            f = dataread(filename)
            params = f.GetHeader(0)
            if input_info['detector'] == 'medipix':
                lagt2 = float32(float(params['time_of_frame']))
                lagt.append(lagt2 - lagt1)
                lagt1 = lagt2
#        if (input_info['detector']=='princeton' or input_info['detector']=='andor'):
            else:
                counters = params['counter_mne'].split(' ')
                lagt_ind = counters.index('ccdtavg')
                values = params['counter_pos'].split(' ')
                lagt.append(float32(float(values[lagt_ind])))
        del lagt[0]
        dt = average(array(lagt, dtype=float32))
        print 'lag time =', dt
    else:
        dt = float32(float(input_info['lag time']))
    q_2tcf = (input_info['q for TRC']).lower()
    if q_2tcf != 'none':
        q_2tcf = int(q_2tcf)

    out_dir = get_dir(input_info['output directory'])
    out_prefix = get_prefix(input_info['output filename prefix'])
    out_tot = out_dir + out_prefix
    ##end processing input file#####
    firstname = dir + file_name(file_prefix, ext, firstfile)
    f = dataread(firstname)
    ccd_info = f.GetStaticHeader(0)
    ncol = int(ccd_info['Dim_1'])
    nrows = int(ccd_info['Dim_2'])

    static = out_tot + 'static.edf'
    static = EdfFile.EdfFile(static)
    static_data = asfarray(static.GetData(0), dtype=float32)
    if input_info['n_first_dark'].lower() == 'none':
        print 'not using darks'
        tot_darks = 0 * static_data
    else:
        print 'using darks'
        if dark_file == 'default':
            dark_file = out_tot + 'dark.edf'
        print 'using dark file:', dark_file
        dark = EdfFile.EdfFile(dark_file)
        tot_darks = asfarray(dark.GetData(0), dtype=float32)

    toplot = static_data + .001  #to avoid zeros in plotting logarithm###
    print '...done'
    print '...reading q mask'
    if mask_file == 'default':
        mask_file = out_tot + 'mask.edf'
    print 'using mask file:', mask_file
    tot = EdfFile.EdfFile(mask_file)
    totmask = float32(tot.GetData(0) + tot.GetData(1))
    wtotmask = where(totmask == 0)

    p.ion()
    fileq = out_tot + 'qmask.edf'
    file = EdfFile.EdfFile(fileq)
    q = file.GetData(0)
    maxval = int(amax(q) + 2)
    detector = input_info['detector']
    flatfield_file = input_info['flatfield file']
    if detector == 'medipix':
        flat_field = flatfield(detector, flatfield_file)
    else:
        flat_field = 1.0
    print '...done'
    if geometry == 'saxs':
        print '...correcting static for baseline'
        xbeam = int(input_info['x direct beam'])
        ybeam = int(input_info['y direct beam'])
        static_data = rad_average(static_data, totmask, xbeam, ybeam)

    qaxis_list = []
    npix_per_q = []
    oneq = []
    index_in_q = []
    firstq = float32(float(input_info['first q']))
    deltaq = float32(float(input_info['delta q']))
    stepq = float32(float(input_info['step q']))
    qvalue = firstq + deltaq / 2
    static_corrected = ones(shape(static_data), dtype=float32)
    q *= abs(totmask - 1)
    total_pixels = 0
    for i in range(2, maxval, 2):
        indices = where(q == i)
        index_in_q.append(
            indices
        )  #gives the indices of pixels that are not masked at this q
        if geometry == 'saxs':
            static_corrected[indices] = mean(
                static_data[indices]) / static_data[indices]
        npixel = len(static_data[indices])
        npix_per_q.append(npixel)
        oneq.append(ones((1, npixel)))
        qaxis_list.append(qvalue)
        qvalue += deltaq + stepq
        total_pixels += npixel
    print '...done'
    nq = len(npix_per_q)
    xnq = xrange(nq)
    ncores = 1
    ncores = min(ncores, nq)
    tmp_pix = 0
    q_sec = []
    if nq == 1:
        q_sec.append(0)
    elif ncores >= nq:
        q_sec = range(1, nq)
    else:
        for ii in xnq:
            if tmp_pix < total_pixels / (ncores):
                tmp_pix += npix_per_q[ii]
                if ii == nq - 1:
                    q_sec.append(ii)
            else:
                q_sec.append(ii)
                tmp_pix = 0 + npix_per_q[ii]
    ncores = len(q_sec)
    tmpdat = loadtxt(out_tot + '1Dstatic.dat')
    qaxis = tmpdat[:, 0]
    I_q = tmpdat[:, 1]
    del tmpdat

    ##FINISHED INITIALIZING PART OF THE CODE######
    ##START MAIN PART FOR CORRELATION#####
    chn = 16.
    chn2 = chn / 2
    nfile = lastfile - firstfile
    rch = int(ceil(log(nfile / chn) / log(2)) + 1)
    ###2time
    if q_2tcf != 'none':
        ttdata = zeros((nfile, npix_per_q[q_2tcf - 1]), dtype=float32)
    ###2time
    rcr = chn + chn2 * ceil(log(nfile / chn) / log(2))
    lag = zeros((1, rcr), dtype=float32)
    data_shape = p.shape(toplot)
    smatr = zeros(data_shape, dtype=float32)
    matr = zeros(data_shape, dtype=float32)
    norm = zeros((1, rcr), dtype=float32)
    for ir in xrange(rch):
        if ir == 0:
            lag[0, :chn] = dt * arange(1, chn + 1, 1)
            norm[0, :chn] = 1. / arange(nfile - 2, nfile - chn - 2, -1)
        else:
            lag[0, chn2 * (ir + 1):chn2 *
                (ir + 2)] = (dt * 2**ir) * arange(1 + chn2, chn + 1)
            norm[0, chn2 * (ir + 1):chn2 * (ir + 2)] = 1. / arange(
                (nfile - 1) / (2**ir) - chn2 - 1,
                (nfile - 1) / (2**ir) - chn - 1, -1)
    #END of declaring and initializing variables####
    #READING FILES
    filenames = []
    for k in xrange(firstfile, lastfile):
        filenames.append(file_name(file_prefix, ext, k))
    n = 0
    if plot != 'no':
        ax1 = p.axes([0.11, 0.08, 0.75, 0.57])
        ax1.set_xlabel('t [sec]')
        ax1.set_ylabel('g^2(q,t)')
        ax1b = p.twinx(ax1)
        ax1b.yaxis.tick_right()
        ax2 = p.axes([0.11, 0.73, 0.75, 0.19])
        ax2.xaxis.tick_bottom()
        ax2.set_xlabel('t [sec]')
        ax2.set_ylabel('I(q,t) [a.u.]')

        ax2b = p.gcf().add_axes(ax2.get_position(), frameon=False)
        ax2b.xaxis.tick_top()
        ax2b.yaxis.tick_right()
        ax2b.xaxis.set_label_position('top')
        ax2b.set_xlabel('Image no.')
        label1 = 'q= %2.1e 1/Ang' % qaxis_list[0]
        label2 = 'q= %2.1e 1/Ang' % qaxis_list[nq / 2]
        lm1, = ax1.semilogx((1, ), (1, ), 'ro-', label=label1)
        lm1b, = ax1b.semilogx((1, ), (1, ), 'bo-', label=label2)
        ax1.legend(loc='lower left')
        ax1b.legend(loc=(0.02, 0.1))
        lm2, = ax2.plot((1, ), (1, ), 'r-')
        lm2b, = ax2b.plot((1, ), (1, ), 'b-')
        p.setp(ax1.get_yticklabels(), color='r')
        p.setp(ax1b.get_yticklabels(), color='b')
        p.setp(ax2.get_yticklabels(), color='r')
        p.setp(ax2b.get_yticklabels(), color='b')
    tplot_cum = 0
    tread_cum = 0
    tcalc_cum = 0
    tqueue_cum = 0
    I_avg = zeros((1, nfile), float32)
    I_avg2 = zeros((1, nfile), float32)
    I_avgs = zeros((nfile, nq), float32)
    tI_avg = zeros((1, nfile), float32)
    mon = zeros((1, nfile), int16)

    detector = input_info['detector'].lower()
    Mythread = threading.Thread
    checkfile = os.path.exists
    n = 0
    totsaxs = 0 * static_data
    goodsize = os.path.getsize(dir + filenames[n])
    nnfile = nfile - 1
    #if plot!='no':
    #   tmpf=lambda x : True
    #   thplot=Process(target=tmpf,args=([0]))
    #   thplot.start()
    ######################multiprocessing#######################################################
    qur = []
    qure = []
    pcorr = []
    for i in xrange(ncores):
        qur.append(Queue())
        qure.append(Queue())
    #qur.append(Queue())
    quplot = Queue()
    for i in xrange(ncores):
        if i == 0:
            q_beg = 0
        else:
            q_beg = q_sec[i - 1]
        q_end = q_sec[i]
        if i == ncores - 1:
            q_end = nq
        pcorr.append(
            Process(target=mp_corr,
                    args=(i, nfile, chn, plot, npix_per_q[q_beg:q_end],
                          index_in_q[q_beg:q_end], qur[i], qure[i], quplot)))
    for i in xrange(ncores):
        pcorr[i].start()

    n = 0
    nc = 0
    nnfile = nfile - 1
    if input_info['normalize'].lower() != 'none':
        normalize = input_info['normalize']
        print "normalizing to ", input_info['normalize']
    else:
        print "not normalizing"
    while n < nnfile:
        tread = time.time()
        nc = n + 1
        file = filenames[n]
        tmf = dir + file
        wait = 0
        t0 = time.time()
        stop = 0
        while checkfile(tmf) is False:
            p.draw()
            sys.stdout.write(50 * '\x08')
            sys.stdout.write('waiting for file' + file + '...')
            sys.stdout.flush()
            t1 = time.time()
            wait += t1 - t0
            time.sleep(dt)
            t0 = t1
            if wait > 10 * dt:
                print nfile
                ans = raw_input('\n will this file ever arrive? (y/N)')
                if ans.lower() == 'y':
                    print '\n keep waiting...\n'
                    time.sleep(3 * dt)
                    wait = 0
                else:
                    stop = 1
                    nfile = n + 1
                    break
        if stop == 1:
            break
        if ext == '.edf':
            filesize = os.path.getsize(tmf)
            while filesize != goodsize:
                sys.stdout.write(50 * '\x08')
                sys.stdout.write('file ' + file + 'still not ready...')
                sys.stdout.flush()
                time.sleep(dt)
                filesize = os.path.getsize(tmf)
        f = dataread(tmf)
        dread(f, n, tot_darks, flat_field, static_corrected)
        mon[0, n] = monitor
        #for plot. TO be faster, I only updated plot each chn files.
        jj = 0
        tmp_put = []
        tqueue = time.time()
        for i in xnq:
            if i < q_sec[jj]:
                tmp_put.append(ccd_img[index_in_q[i]])
            elif i == nq - 1:
                tmp_put.append(ccd_img[index_in_q[i]])
                qur[jj].put(tmp_put)
            else:
                qur[jj].put(tmp_put)
                tmp_put = []
                tmp_put.append(ccd_img[index_in_q[i]])
                jj += 1
        tqueue_cum += time.time() - tqueue
        if nc % chn == 0:
            pct = 100.0 * n / nfile
            sys.stdout.write(50 * '\x08')
            sys.stdout.write('read ' + str(int(pct)) + '% of files' + 32 * ' ')
            sys.stdout.flush()
            if plot != 'no':
                #thplot.join()
                xx = quplot.get()
                ttplot(xx[0], xx[1], xx[2], n + 1, I_avg[0, :n + 1],
                       I_avg2[0, :n + 1])
                #thplot=Process(target=ttplot,args=([xx[0],xx[1],xx[2],n+1,I_avg[0,:n+1],I_avg2[0,:n+1]]))
                #thplot.start()
                #thplot.join()
        n += 1
    #if plot!='no':
    #thplot.join()
    sys.stdout.write(50 * '\x08')
    sys.stdout.flush()
    print "read 100% of files"
    ###############################################################################################
    from_proc = []
    for i in xrange(ncores):
        from_proc.append(qure[i].get())
        pcorr[i].join()
        qure[i].close


#############################################################################################
#END OF MAIN LOOP
#calculate 2 times correlation function
    print "saving results..."
    if stop == 1:
        tI_avg = tI_avg[:, :nfile]
        mon = mon[:, :nfile]
        I_avgs = I_avgs[:nfile, :]
        rch = int(ceil(log(nfile / nchannels) / log(2)) + 1)
        for ir in xrange(rch):
            if ir == 0:
                norm[0, :nchannels] = 1. / arange(nfile - 2,
                                                  nfile - nchannels - 2, -1)
            else:
                norm[0, nchannels2 * (ir + 1):nchannels2 *
                     (ir + 2)] = 1. / arange(
                         (nfile - 1) / (2**ir) - nchannels2 - 1,
                         (nfile - 1) / (2**ir) - nchannels - 1, -1)

    #calculate correlation functions
    corf = from_proc[0][0]
    sl = from_proc[0][1]
    sr = from_proc[0][2]
    tcalc_cum = from_proc[0][3]
    for i in xrange(1, ncores):
        corf = concatenate((corf, from_proc[i][0]), axis=0)
        sl = concatenate((sl, from_proc[i][1]), axis=0)
        sr = concatenate((sr, from_proc[i][2]), axis=0)
        tcalc_cum = max(tcalc_cum, from_proc[i][3])

    indt = int(chn + chn2 * log(nfile / chn) / log(2)) - 2
    cc = zeros((indt, nq + 1), float32)
    q_title = '#q values:'
    trace_title = '#file_no. ,   time, monitor,  q values:'
    for cindex in xnq:
        q_title = q_title + ' ' + str(qaxis_list[cindex])
        trace_title = trace_title + ' ' + str(qaxis_list[cindex])
        cc[:,cindex+1]=corf[cindex,:indt]/(sl[cindex,:indt]*sr[cindex,:indt])/\
        norm[0,:indt]
    cc[:, 0] = lag[0, :indt]
    q_title = q_title + '\n'
    trace_title = trace_title + '\n'
    del indt
    f = open(out_tot + 'cf.dat', 'w')
    f.write(q_title)
    savetxt(f, cc)
    f.close()
    del cc
    f = open(out_tot + 'trace.dat', 'w')
    f.write(trace_title)
    traces = zeros((nfile, nq + 3), float32)
    traces[:, 0] = tI_avg / dt + firstfile
    traces[:, 1] = tI_avg
    traces[:, 2] = mon
    traces[:, 3:] = I_avgs
    savetxt(f, traces)
    f.close()
    del traces
    static = out_tot + 'static.edf'
    static = EdfFile.EdfFile(static)
    totsaxs = totsaxs / n - tot_darks
    totsaxs[totsaxs <= 0] = 0
    static.WriteImage({}, totsaxs, 0)
    del static
    print 'correlation functions are saved to ', out_tot + 'cf.dat'
    print 'traces are saved to ', out_tot + 'trace.dat'
    if plot != 'no':
        p.hold(True)
        p.close()
    if q_2tcf != 'none':
        print "calculating time resolved cf and chi4..."
        if nfile > 6000:  #this is for 4 GB RAM PC
            nfile = 6000
            n = 6000
        lind2 = npix_per_q[q_2tcf - 1] / 16
        l = arange(5) * 0
        y = []
        v = []
        for i in range(5):
            y.append([])
            v.append([])
        ib = 0
        for i in xrange(16):
            sys.stdout.write(50 * '\x08')
            sys.stdout.write('done ' + str(int(i / 16. * 100)) + '% of data' +
                             32 * ' ')
            sys.stdout.flush()
            ie = ib + lind2
            y[0].append(trc(ttdata[:n - 1, ib:ie]))
            v[0].append(vartrc(y[0][-1]))
            if l[0] == 1:
                recurf(0)
            else:
                l[0] += 1
            ib += lind2
        vm = []
        for i in range(4, -1, -1):
            vm.append(mean(v[i], 0))
        vm = array(vm)
        del ttdata
        del v
        sys.stdout.write(50 * '\x08')
        sys.stdout.flush()
        file_2times = out_tot + '2times_q_' + str(q_2tcf) + '.edf'
        ytrc.write(file_2times, y[4][0])
        print 'Time resolved CF is saved to ' + out_tot + '2times_q_' + str(
            q_2tcf) + '.edf'
        N = array([[1], [2], [4], [8], [16]]) / float(npix_per_q[q_2tcf - 1])
        data = concatenate((N, vm), 1).T
        #print 'number of pixels ',lind[ttcf_par]
        #print 'q value=', qv[ttcf_par]
        p0 = [0.0, 1.0]
        it = range(len(data[1:, 0]))
        p1 = zeros((len(data[1:, 0]), len(p0) + 1))
        p1[:, 0] = (asfarray(it) + 1.0) * dt
        xdata = data[0, :]
        for i in it:
            ydata = data[i + 1, :]
            p1[i, 1:], success = leastsq(errfunc, p0, args=(xdata, ydata))
        outfile = out_tot + 'fitchi4_q_' + str(q_2tcf) + '.dat'
        f = open(outfile, 'w')
        f.write("#time chi4 error q value:" + str(qaxis_list[q_2tcf - 1]) +
                "\n")
        savetxt(f, p1)
        f.close()
        print 'file is saved to ' + outfile
    print "saving results..."
    time2 = time.time()
    print 'elapsed time', time2 - time1
    print 'elapsed time for plotting', tplot_cum
    print 'elapsed time for reading', tread_cum
    print 'elapsed time for correlating', tcalc_cum
    print 'elapsed time for queueing', tqueue_cum
    print 'used ncores=', ncores
Beispiel #34
0
def main(unused_args):
    tdLoader = TrafficDataLoader(
        'internet-data/data/internet-traffic-11-cities-5min.csv', max_norm=5.)
    tdConfig = TrafficDataConfig()
    tmConfig = TrafficRNNConfig(tdConfig)
    batch_size = tmConfig.batch_size

    seq_input, seq_target = tdLoader.get_rnn_input(tdConfig)

    print seq_input.shape, seq_target.shape
    data = dict()
    data['seq_input'] = seq_input
    data['seq_target'] = seq_target
    data['early_stop'] = tdConfig.batch_size

    is_training = True
    save_graph = False

    with tf.Graph().as_default(), tf.Session() as session:
        model = TrafficRNN(is_training=True, config=tmConfig)

        saver = tf.train.Saver()
        merged = None
        writer = None

        if is_training and save_graph:
            writer = tf.train.SummaryWriter('/tmp/rnn_logs', session.graph_def)

        tf.initialize_all_variables().run()

        decay = .8
        if is_training:
            lr_value = 1e-3
            for epoch in range(tmConfig.max_epoch):
                if epoch > 10:
                    lr_value = 1e-3
                elif epoch > 75:
                    lr_value = 1e-4
                elif epoch > 100:
                    lr_value = 1e-6
                elif epoch > 200:
                    lr_value = 1e-7
                elif epoch > 250:
                    lr_value = 1e-8

                model.assign_lr(session, lr_value)

                net_outs_all = np.array([])

                error, net_outs_all = run_epoch(session, model, data,
                                                model.train_op, tdConfig)
                error, net_outs_all = run_epoch(session, model, data,
                                                tf.no_op(), tdConfig, writer)
                print net_outs_all.shape, seq_target.shape
                print('Epoch %d: %s') % (epoch, error)

                if epoch == 0:
                    plt.figure(1, figsize=(20, 10))
                    plt.ion()
                    plt.ylim([-1, 6])
                    plt.plot(xrange(tdConfig.n_steps), seq_target, 'b-',
                             xrange(tdConfig.n_steps), net_outs_all, 'r-')
                    plt.show()
                    time.sleep(20)
                else:
                    plt.clf()
                plt.ylim([-1, 6])
                plt.plot(xrange(tdConfig.n_steps), seq_target, 'b-',
                         xrange(tdConfig.n_steps), net_outs_all, 'r-')
                img_loc = 'out-img/epoch-%05d.png' % (epoch)
                plt.savefig(img_loc)
                plt.draw()
                time.sleep(.1)

                if epoch > 40 and epoch % 20 == 9:
                    outfile = 'internet-data/saved-models/traffic-rnn-hid-%d-batch-%d-window-%d-lag-%d.chkpnt' % (
                        tmConfig.num_hidden, tdConfig.batch_size,
                        tdConfig.window_size, tdConfig.lag)
                    saver.save(session, outfile, global_step=epoch)
        else:
            saved_vars = 'internet-data/saved-models/traffic-rnn-hid-%d-batch-%d-window-%d-lag-%d.chkpnt-%d' % (
                tmConfig.num_hidden, tdConfig.batch_size, tdConfig.window_size,
                tdConfig.lag, tmConfig.max_epoch - 1)
            saver.restore(session, saved_vars)

        train_error, train_outs_all = run_epoch(session, model, data,
                                                tf.no_op(), tdConfig)

        testDataConfig = TestConfig()
        test_seq_input, test_seq_target = tdLoader.get_rnn_input(
            testDataConfig)

        test_data = dict()
        test_outs_all = np.array([])
        test_data['seq_input'] = test_seq_input
        test_data['seq_target'] = test_seq_target
        test_data['early_stop'] = testDataConfig.batch_size
        test_error, test_outs_all = run_epoch(session, model, test_data,
                                              tf.no_op(), testDataConfig)

        upper_curve = test_outs_all + .1 * test_outs_all
        lower_curve = test_outs_all - .1 * test_outs_all
        shift_left = np.zeros(test_outs_all.shape)
        shift_right = np.zeros(test_outs_all.shape)
        shift_left[:-18] = test_outs_all[18:]
        shift_right[18:] = test_outs_all[:-18]

        curve1 = np.maximum(upper_curve, shift_left)
        curve1 = np.maximum(curve1, shift_left)
        curve1 = np.maximum(curve1, shift_right)
        curve2 = np.minimum(lower_curve, shift_right)
        curve2 = np.minimum(curve2, upper_curve)
        curve2 = np.minimum(curve2, shift_left)

        print test_outs_all.shape

        x = xrange(len(test_outs_all))
        plt.figure(3, figsize=(20, 10))
        plt.ioff()

        plt.plot(x, test_outs_all, 'b-', alpha=1)
        plt.plot(x, test_seq_target, 'g-', alpha=1)
        plt.plot(x, curve1, 'r-', alpha=.1)
        plt.plot(x, curve2, 'r-', alpha=.1)
        plt.fill_between(x, curve1, curve2, color='grey', alpha=.3)
        plt.show()

        print 'Test error: %s' % test_error
        plt.figure(2, figsize=(20, 10))
        plt.plot(xrange(tdConfig.n_steps), seq_target, 'b-',
                 xrange(tdConfig.n_steps), train_outs_all, 'g--')
        plt.plot(
            xrange(tdConfig.n_steps - 24,
                   tdConfig.n_steps + testDataConfig.n_steps - 24),
            test_seq_target, 'b-')
        plt.plot(
            xrange(tdConfig.n_steps - 24,
                   tdConfig.n_steps + testDataConfig.n_steps - 24),
            test_outs_all, 'r--')
        plt.show()
        time.sleep(1)
Beispiel #35
0
else:
    Lite = False

try:
    try:
        import matplotlib.pylab as pylab
    except:
        import matplotlib.matlab as pylab
    # catch old versions which use set instead of setp
    if not hasattr(pylab, 'setp'): pylab.setp = pylab.set
    # gives right aspect ratio in subplots
    pylab.rcParams['image.aspect'] = 'auto'
    # increase vertical gap between plots
    pylab.rcParams['figure.subplot.hspace'] = 0.3
    # turn on interactive mode
    pylab.ion()
    gotMatplotlib = True
except:
    if not Lite:        print '\n ++++ CliMT: WARNING: matplotlib.pylab ' \
+'could not be loaded, so no runtime monitoring !\n'
    gotMatplotlib = False

from numpy import *
from utils import squeeze
from state import KnownFields


def _figureSetUp(FieldKeys, Component):
    """
    Sets up a figure consisting of up to 4 panels (subplots).
    """
Beispiel #36
0
import MEArec as mr
import MEAutility as mu
import matplotlib.pylab as plt
import matplotlib.gridspec as gridspec
from plotting_conventions import *
import numpy as np
import os

save_fig = False

plt.ion()
plt.show()

template_file = 'data/templates/templates_30_tetrode.h5'
recording_file = 'data/recordings/recordings_6cells_tetrode_30.0_10.0uV.h5'

tempgen = mr.load_templates(template_file)
recgen = mr.load_recordings(recording_file)

mea = mu.return_mea(info=recgen.info['electrodes'])

fig = plt.figure(figsize=(12, 11))
gs = gridspec.GridSpec(20, 3)

ax_timeseries = fig.add_subplot(gs[:4, :])
ax_waveforms = fig.add_subplot(gs[5:8, :])
ax_pca = fig.add_subplot(gs[10:, :])

mr.plot_recordings(recgen,
                   ax=ax_timeseries,
                   overlay_templates=True,
Beispiel #37
0
def neu():
    global wa, wb, wa1, wb1, wc1, wa2, wb2, wc2, wconst1, wconst2, wconst3, w13, w23
    wa = random.random()
    wb = random.random()
    wa1 = random.random()
    wb1 = random.random()
    wc1 = random.random()
    wa2 = random.random()
    wb2 = random.random()
    wc2 = random.random()
    w13 = random.random()
    w23 = random.random()
    wconst1 = random.random()
    wconst2 = random.random()
    wconst3 = random.random()
    dwa1 = 0
    dwb1 = 0
    dwc1 = 0
    dwa2 = 0
    dwb2 = 0
    dwc2 = 0
    dw13 = 0
    dw23 = 0
    dwconst1 = 0
    dwconst2 = 0
    dwconst3 = 0

    #-----------------------------------------------

    def neut1(a, b, c, dwa1, dwb1, dwc1, dwconst1):
        global wa1, wb1, wc1, wconst1
        wa1 = wa1 + dwa1
        wb1 = wb1 + dwb1
        wc1 = wc1 + dwc1
        wconst1 = wconst1 + dwconst1
        S = a * wa1 + b * wb1 + c * wc1 + 1 * wconst1
        F = sigmoid(S)
        print(F)
        return F

    def neut2(a, b, c, dwa2, dwb2, dwc2, dwconst2):
        global wa2, wb2, wc2, wconst2
        wa2 = wa2 + dwa2
        wb2 = wb2 + dwb2
        wc2 = wc2 + dwc2
        wconst2 = wconst2 + dwconst2
        S = a * wa2 + b * wb2 + c * wc2 + 1 * wconst2
        F = sigmoid(S)
        print(F)
        return F

    def neut3(f1, f2, dw13, dw23, dwconst3, X):
        global w13, w23, wconst3
        w13 = w13 + dw13
        w23 = w23 + dw23
        wconst3 = wconst3 + dwconst3
        S = f1 * w13 + f2 * w23 + 1 * wconst3
        F = sigmoid(S)
        E = X - F
        print(F)
        return F, w13, w23, E


#---------------------------------------------------

    def neu1(a, b, c):
        global wa1, wb1, wc1, wconst1
        F = sigmoid(a * wa1 + b * wb1 + c * wc1 + wconst1)
        return F

    def neu2(a, b, c):
        global wa2, wb2, wc2, wconst2
        F = sigmoid(a * wa2 + b * wb2 + c * wc2 + wconst2)
        return F

    def neu3(a, b):
        global w13, w23, wconst3
        f3 = sigmoid(a * w13 + b * w23 + wconst3)
        return f3

    print("Training area initialized. Print what u want to do:")
    print("e=exit;l=learn")
    s = input()
    s = "l"
    if s == "e":
        print("See you later!")
    if s == "l":
        print("Lets train something")
        print("Enter values")
        i = 0
        q = 0
        j = 0
        x1 = [1, -1]
        y1 = [0, 0]
        x2 = [0, 0]
        y2 = [1, -1]
        plt.plot(x1, y1)
        plt.plot(x2, y2)
        plt.grid()
        plt.ion()
        while i < 7000:
            #-Тренировочные_данные-\/
            a = [1, 1, 1, 1, 0, 0, 0, 0]
            b = [1, 1, 0, 0, 1, 1, 0, 0]
            c = [1, 0, 1, 0, 1, 0, 1, 0]
            x = [1, 1, 1, 1, 0, 0, 1, 0]
            #-Тренировочные_данные-/\
            ai = a[q]
            bi = b[q]
            ci = c[q]
            xi = x[q]
            f1 = neut1(ai, bi, ci, dwa1, dwb1, dwc1, dwconst1)
            f2 = neut2(ai, bi, ci, dwa2, dwb2, dwc2, dwconst2)
            f3, w13, w23, E = neut3(f1, f2, dw13, dw23, dwconst3, xi)
            E1 = backerror(E, w13)
            E2 = backerror(E, w23)
            dwa1 = E1 * (f1 * (1 - f1)) * ai
            dwb1 = E1 * (f1 * (1 - f1)) * bi
            dwc1 = E1 * (f1 * (1 - f1)) * ci
            dwconst1 = E1 * (f1 * (1 - f1)) * 1
            dwa2 = E2 * (f2 * (1 - f2)) * ai
            dwb2 = E2 * (f2 * (1 - f2)) * bi
            dwc2 = E2 * (f2 * (1 - f2)) * ci
            dwconst2 = E2 * (f2 * (1 - f1)) * 1
            dw13 = E * (f3 * (1 - f3)) * f1
            dw23 = E * (f3 * (1 - f3)) * f2
            dwconst3 = E * (f3 * (1 - f3)) * 1
            print("------------------")
            if q == 6:
                q = 0
            else:
                q = q + 1
            i = i + 1
            try:
                Ap.remove()
            except:
                print("Старт")
            Ap = plt.scatter(E, E)
            plt.draw()
            plt.pause(0.0001)
        plt.ioff()
        plt.show()
        print("Lets train my skills")
        while i < 5:
            i = i + 1
            a = int(input())
            b = int(input())
            c = int(input())
            f1 = neu1(a, b, c)
            f2 = neu2(a, b, c)
            f3 = neu3(f1, f2)
            print(f3)
Beispiel #38
0
    use and modify it however you like). 

"""
# import future syntax as for Python version >= 3.0
from __future__ import division  # such that 0 != 1/2 == 0.5
from __future__ import print_function  # available since 2.6, not needed

from math import log, exp
from random import normalvariate as random_normalvariate
# see randn keyword argument to CMAES.__init__

# Optional imports, can be out-commented, if not available
import sys
try:
    import matplotlib.pylab as pylab
    pylab.ion()  # prevents that execution stops after plotting
    # for plotting, scitools.easyfiz might be an alternative
except ImportError:
    pylab = None
    print('  pylab could not be imported  ')

__version__ = '1.10'
__author__ = 'Nikolaus Hansen'
__docformat__ = 'reStructuredText'


def fmin(objectivefct,
         xstart,
         sigma,
         args=(),
         max_eval='1e3*N**2',
Beispiel #39
0
    def plotTimescales(self,
                       lags=None,
                       units='frames',
                       errors=None,
                       nits=None,
                       results=False,
                       plot=True):
        """ Plot the implied timescales of MSMs of various lag times

        Parameters
        ----------
        lags : list
            The lag times at which to compute the timescales. By default it spreads out 25 lag times linearly from lag
            10 until the mode length of the trajectories.
        units : str
            The units of lag. Can be 'frames' or any time unit given as a string.
        errors : errors
            Calculate errors using Bayes (Refer to pyEMMA documentation)
        nits : int
            Number of implied timescales to calculate. Default: all
        results : bool
            If the method should return the calculated implied timescales
        plot : bool
            If the method should display the plot of implied timescales

        Returns
        -------
        If given `results`=True this method will return the following data
        its : np.ndarray
            The calculated implied timescales. 2D array with dimensions (len(`lags`), `nits`)
        lags : np.ndarray
            A list of the lag times that were used to calculate the implied timescales

        Examples
        --------
        >>> model = Model(data)
        >>> model.plotTimescales()
        >>> model.plotTimescales(lags=list(range(1,100,5)))
        """
        import pyemma.plots as mplt
        import pyemma.msm as msm
        self._integrityCheck()
        if lags is None:
            lags = self._defaultLags()
        else:
            lags = unitconvert(units, 'frames', lags,
                               fstep=self.data.fstep).tolist()

        if nits is None:
            nits = np.min((self.data.K, 20))

        from htmd.config import _config
        its = msm.its(self.data.St.tolist(),
                      lags=lags,
                      errors=errors,
                      nits=nits,
                      n_jobs=_config['ncpus'])
        if plot:
            from matplotlib import pylab as plt
            plt.ion()
            plt.figure()
            mplt.plot_implied_timescales(its, dt=self.data.fstep, units='ns')
            plt.show()
        if results:
            return its.get_timescales(), its.lags
Beispiel #40
0
    def process_data(self, **kwds):
        """Keywords are passed spike.plot_correlogram as style formatting.
        """
        plt.ioff()
        self.figure = {}
        res = self.results
        lags = res['lags']
        C = res['C_overall']
        C_rat = res['C_rat']

        # Set an informative title
        title = "Scan " + res['scan_time'] + ": " + res['query']
        if res['shuffled']:
            title += ' [shuffled]'

        fmt = dict(lw=1)
        fmt.update(kwds)
        corr_args = dict(is_corr=True, norm=False, plot_type="lines", fmt=fmt)

        # Plot the per-rat correlograms normalized in different ways
        C_rat_norm = []
        for norm in 0, 1, 2:
            f = plt.figure(figsize=(8, 7))
            if norm == 0:
                self.figure['rat'] = f
                rat_title = title + ' [per rat]'
            elif norm == 1:
                self.figure['rat_norm_max'] = f
                rat_title = title + ' [per rat] [norm max]'
            elif norm == 2:
                self.figure['rat_norm_zero'] = f
                rat_title = title + ' [per rat] [norm zero]'
            ax = plt.axes()
            for i, psth in enumerate(C_rat.values()):
                if norm == 1:
                    psth /= psth.max()
                    C_rat_norm.append(psth)
                elif norm == 2:
                    psth /= psth[lags==0]
                plot_correlogram((psth, lags), **corr_args)
            ax.set_xlabel('Spike Lag (%s)'%res['units'])
            ax.set_ylabel('Correlation')
            ax.set_title(rat_title)
        # if len(C_rat_norm):
        #     valid = filter(lambda x: np.isfinite(x).all(), C_rat_norm)
        #     C_rat_norm = np.array(C_rat_norm)
        #     mu = C_rat_norm.mean(axis=0)
        #     std = C_rat_norm.std(axis=0)
        #     ci = 1.96*std/np.sqrt(C_rat_norm.shape[0])

        # Plot overal correlogram with min/max per-rat envelope
        self.figure['overall'] = plt.figure(figsize=(8, 7))
        ax = plt.axes()
        fmt['lw'] = 2
        fmt['c'] = 'k'
        corr_args['norm'] = True
        h = plot_correlogram((C, lags), **corr_args)
        h.set_label('Overall Spikes')
        # if len(C_rat_norm):
        #     ax.plot(lags, mu, 'b-', zorder=-1, label='Rat Average')
        #     ax.plot(lags, mu+ci, 'r--', zorder=-2, label='Rat 95% CI')
        #     ax.plot(lags, mu-ci, 'r--', zorder=-2)
        ax.set_xlabel('Spike Lag (%s)'%res['units'])
        ax.set_ylabel('Correlation')
        ax.set_title(title)
        ax.legend(loc=4)

        plt.ion()
        plt.show()
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import time

TIME = 30
path = 'C:/Users/asus/Desktop/0022R.wav'

audio,fs = read_audio_soundfile(path)
# footstep 0007R range[250:280]0 ; gun  0022R range[0:30]  vehicle 0018R range[1020:1050]
audio = audio[fs*0:fs*30]
time = np.arange(0, len(audio)) * (1.0 / fs)
split_len = int(30*fs/1000)
N = int(len(audio)/split_len)
predict_all = []
count = 0

plt.ion() #start interactive mode
# ax = plt.subplot(121)
m = []
acc = []
for i in range(N):
    test_data = audio[i*split_len:(i+1)*split_len]
    feature = mel_f.extract_logmel(test_data,fs)
    feature = avg_frame(feature)
    predict = svm_c.predict_svm([feature],[1])
    if predict == 0:
        count += 1
    predict_point = np.ones(split_len)*predict
    # print(predict_point)
    predict_all.append(predict_point)
    m.append(predict_point)
    acc.append(count/(i+1))
Beispiel #42
0
    def plot(self,
             block=False,
             period=False,
             fap=None,
             gls=True,
             data=True,
             residuals=True):
        """
        Create a plot.

        Parameters
        ----------
        period : boolean
            The periodogram is plotted against log(Period).
        fap : float, list
            Plots the FAP levels.
        gls : boolean
            Plots the GLS periodogram.
        data : boolean
            Plots the data.
        residuals : boolean
            Plots the residuals.

        Returns
        -------
        fig : mpl.figure
            A figure which can be modified.
        """
        try:
            import matplotlib
            import matplotlib.pylab as mpl
        except ImportError:
            raise (ImportError("Could not import matplotlib.pylab."))

        fbest, T0 = self.best["f"], self.best["T0"]

        fig = mpl.figure()
        fig.canvas.set_window_title('GLS periodogram')
        fig.subplots_adjust(hspace=0.05,
                            wspace=0.04,
                            right=0.97,
                            bottom=0.09,
                            top=0.84)
        fs = 10  # fontsize

        nrow = gls + data + residuals
        plt, plt1, plt2, plt3, plt4 = [None] * 5

        if gls:
            # Periodogram
            plt = fig.add_subplot(nrow, 1, 1)
            plt.tick_params(direction='in')
            if period:
                plt.set_xscale("log")
                plt.set_xlabel("Period P")
            else:
                plt.set_xlabel("Frequency $f$")

            plt.set_ylabel(self.label["ylabel"])
            plt.plot(1 / self.f if period else self.f,
                     self.power,
                     'b-',
                     linewidth=.5)
            # mark the highest peak
            plt.plot(1 / fbest if period else fbest,
                     self.power[self.p.argmax()],
                     'r.',
                     label="$1/f = %f$" % (1 / fbest))

            x2tics = 1 / np.array([0.5, 1, 2, 3, 5, 10, 20., 100])
            mx2tics = 1 / np.array([0.75, 1.5, 2.5, 4, 15, 40, 60., 80, 100])

            def tick_function(X):
                return ["%g" % (1 / z) for z in X]

            plt.tick_params(direction='in', which='both', top=True, right=True)
            plt.minorticks_on()
            plt.autoscale(enable=True, axis='x', tight=True)
            if not period:
                ax2 = plt.twiny()
                ax2.tick_params(direction='in', which='both')
                ax2.format_coord = lambda x, y: "x=%g, x2=%g, y=%g" % (x, 1 /
                                                                       x, y)
                ax2.set_xticks(x2tics)
                ax2.set_xticks(mx2tics, minor=True)
                ax2.set_xticklabels(tick_function(x2tics))
                ax2.set_xlim(plt.get_xlim())
                ax2.set_xlabel("Period")
                plt.tick_params(top=False)

            if fap is not None:
                if isinstance(fap, float):
                    fap = [fap]
                n = max(1, len(fap) - 1)  # number of dash types
                for i, fapi in enumerate(fap):
                    plt.axhline(self.powerLevel(fapi),
                                linewidth=0.5,
                                color='r',
                                dashes=(8 + 32 * (n - i) / n, 8 + 32 * i / n),
                                label="FAP = %s%%" % (fapi * 100))
            plt.legend(numpoints=1, fontsize=fs, frameon=False)

        # Data and model
        col = mpl.cm.rainbow(mpl.Normalize()(self.t))

        def plot_ecol(plt, x, y):
            # script for scatter plot with errorbars and time color-coded
            datstyle = dict(color=col,
                            marker='.',
                            edgecolor='k',
                            linewidth=0.5,
                            zorder=2)
            if self.e_y is not None:
                errstyle = dict(yerr=self.e_y,
                                marker='',
                                ls='',
                                elinewidth=0.5)
                if matplotlib.__version__ < '2.':
                    errstyle['capsize'] = 0.
                    datstyle['s'] = 8**2  # requires square size !?
                else:
                    errstyle['ecolor'] = col
                _, _, (c, ) = plt.errorbar(x, y, **errstyle)
                if matplotlib.__version__ < '2.':
                    c.set_color(col)
            plt.scatter(x, y, **datstyle)

        def phase(t):
            #return (t-T0)*fbest % 1
            return (t - T0) % (1 / fbest)

        if data:
            # Time series
            tt = arange(self.t.min(), self.t.max(), 0.01 / fbest)
            ymod = self.sinmod(tt)
            plt1 = fig.add_subplot(nrow, 2, 2 * gls + 1)
            plt1.set_ylabel("Data")
            if residuals:
                mpl.setp(plt1.get_xticklabels(), visible=False)
            else:
                plt1.set_xlabel("Time")
            plot_ecol(plt1, self.t, self.y)
            plt1.plot(tt, ymod, 'k-', zorder=0)

            # Phase folded data
            tt = arange(T0, T0 + 1 / fbest, 0.01 / fbest)
            yy = self.sinmod(tt)
            plt2 = fig.add_subplot(nrow, 2, 2 * gls + 2, sharey=plt1)
            mpl.setp(plt2.get_yticklabels(), visible=False)
            if residuals:
                mpl.setp(plt2.get_xticklabels(), visible=False)
            else:
                plt2.set_xlabel("Phase")
            plot_ecol(plt2, phase(self.t), self.y)
            xx = phase(tt)
            ii = np.argsort(xx)
            plt2.plot(xx[ii], yy[ii], 'k-')
            plt2.format_coord = lambda x, y: "x=%g, x2=%g, y=%g" % (x, x *
                                                                    fbest, y)

        if residuals:
            # Time serie of residuals
            yfit = self.sinmod()
            yres = self.y - yfit
            plt3 = fig.add_subplot(nrow, 2, 2 * (gls + data) + 1, sharex=plt1)
            plt3.set_xlabel("Time")
            plt3.set_ylabel("Residuals")
            plot_ecol(plt3, self.t, yres)
            plt3.plot([self.t.min(), self.t.max()], [0, 0], 'k-')

            # Phase folded residuals
            plt4 = fig.add_subplot(nrow,
                                   2,
                                   2 * (gls + data) + 2,
                                   sharex=plt2,
                                   sharey=plt3)
            plt4.set_xlabel("Phase")
            mpl.setp(plt4.get_yticklabels(), visible=False)
            plot_ecol(plt4, phase(self.t), yres)
            plt4.plot([0, 1 / fbest], [0, 0], 'k-')
            plt4.format_coord = lambda x, y: "x=%g, x2=%g, y=%g" % (x, x *
                                                                    fbest, y)

        for x in fig.get_axes()[2:]:
            x.tick_params(direction='in', which='both', top=True, right=True)
            x.minorticks_on()
            x.autoscale(enable=True, tight=True)

        if hasattr(mpl.get_current_fig_manager(), 'toolbar'):
            # check seems not needed when "TkAgg" is set
            mpl.get_current_fig_manager().toolbar.pan()
        #t = fig.canvas.toolbar
        #mpl.ToggleTool(mpl.wx_ids['Pan'], False)

        fig.tight_layout()  # to get the left margin
        marleft = fig.subplotpars.left * fig.get_figwidth() * fig.dpi / fs

        def tighter():
            # keep margin tight when resizing
            xdpi = fs / (fig.get_figwidth() * fig.dpi)
            ydpi = fs / (fig.get_figheight() * fig.dpi)
            fig.subplots_adjust(bottom=4. * ydpi,
                                top=1 - ydpi - 4 * gls * ydpi,
                                right=1 - 1 * xdpi,
                                wspace=4 * xdpi,
                                hspace=4 * ydpi,
                                left=marleft * xdpi)
            if gls and (residuals or data):
                # gls plot needs additional space for x2axis
                fig.subplots_adjust(top=1 - 8 * ydpi)
                if matplotlib.__version__ < '2.':
                    ax2.set_position(plt.get_position().translated(
                        0, 4 * ydpi))
                plt.set_position(plt.get_position().translated(0, 4 * ydpi))

        #fig.canvas.mpl_connect("resize_event", lambda _: (fig.tight_layout()))
        fig.canvas.mpl_connect("resize_event", lambda _: (tighter()))
        fig.show()
        if block:
            print("Close the plot to continue.")
            # needed when called from shell
            mpl.show()
        else:
            # avoids blocking when: import test_gls
            mpl.ion()
        # mpl.show(block=block) # unexpected keyword argument 'block' in older matplotlib
        return fig
Beispiel #43
0
def test_weathering_model():
    from weathering_model.utils import pack_values
    from weathering_model.muscl import muscl, vanAlbada
    import numpy as np
    nx = 1000
    dx = 0.05
    Xo = 1
    x0 = np.zeros((nx, 2))
    x0[:, 0] = Xo
    t = np.array([0, 2, 4, 6, 8, 10, 12])
    vstar = 1
    r = 0.25
    Yostar = 1

    def bc_function(x):

        return (np.array([[1.0, 1], [0, 0]]), np.array([[1.0, yb], [0, 0]]))

    def flux_function(x):

        v = np.zeros_like(x)
        v[:, 1] = vstar
        return x * v

    def source_function(x):
        X = x[:, 0]
        Y = (x[:, 1] > 0) * x[:, 1]
        s = np.zeros_like(x)
        s[:, 0] = -np.power(Y, r) * np.power(X, 2)
        s[:, 1] = -r * np.power(X, 2) * np.power(Y, r) / Yostar
        return s

    def diffusion_function(x):
        x_d = np.zeros((x0.shape[0] + 2, 2))
        x_d[1:-1, :] = x
        (topBC, bottomBC) = bc_function(x)
        x_d[0, 1] = topBC[0, 1]
        x_d[-1, 1] = bottomBC[0, 1]
        q = np.diff(x_d[:, 1]) / dx
        dxdt = np.zeros_like(x)
        dxdt[:, 1] = vstar * np.diff(q) / dx
        return dxdt

    def prop_speed(x):

        v = np.zeros_like(x)
        v[:, 1] = vstar
        return np.abs(v)

    def to_integrate(t, x):
        x_unpacked = pack_values(x, packing_geometry=x0.shape)
        dxdt_flux = muscl(x_unpacked,
                          dx,
                          bc_function,
                          flux_function,
                          vanAlbada,
                          prop_speed=prop_speed,
                          reconstruction='parabolic')
        dxdt_source = source_function(x_unpacked)
        dxdt_diffusion = diffusion_function(x_unpacked)
        dxdt = dxdt_flux + dxdt_source + dxdt_diffusion
        print(t)
        return pack_values(dxdt, packing_geometry=None)

    from scipy.integrate import solve_ivp
    out = solve_ivp(to_integrate, (np.min(t), np.max(t)),
                    pack_values(x0, packing_geometry=None),
                    method='RK45',
                    t_eval=t)
    y = out.y.T
    import matplotlib.pylab as plt

    plt.ion()

    x_a = np.arange(0, nx * dx, dx)
    for i in range(len(t)):
        this_y = pack_values(y[i], packing_geometry=x0.shape)
        plt.figure(1)
        plt.plot(x_a, this_y[:, 0], '.')
        plt.figure(2)
        plt.plot(x_a, this_y[:, 1], '.')
Beispiel #44
0
    def setupAxes(self):
        if self.showFigs:
            # create figure with axes:

            pylab.ion()  # Force interactive
            plt.close('all')
            ### for 'Qt4Agg' backend maximize figure
            plt.switch_backend('QT5Agg')

            # plt.rc('text', usetex=True)
            plt.rc('font', family='serif')

            self.fig = plt.figure()
            # gs1 = gridspec.GridSpec(1, 2)
            # fig.show()
            # fig.set_tight_layout(True)
            self.figManager = plt.get_current_fig_manager()
            DPI = self.fig.get_dpi()
            self.fig.set_size_inches(800.0 / DPI, 600.0 / DPI)

            gs = gridspec.GridSpec(1, 2)

            self.fig.clf()

            self.FTR.axes = self.fig.add_subplot(gs[0, 0])
            self.FTR.axes.invert_xaxis()
            self.FTR.axes.set_title('$FT(r)$')
            self.FTR.axes.grid(True)

            self.Chi_k.axes = self.fig.add_subplot(gs[0, 1])
            self.Chi_k.axes.invert_xaxis()
            self.Chi_k.axes.set_title('$\chi(k)$')
            self.Chi_k.axes.grid(True)

            self.FTR.axes.set_ylabel('Reletive Intensity (a.u.)',
                                     fontsize=16,
                                     fontweight='bold')
            self.FTR.axes.set_xlabel('$r$ $[\AA]$',
                                     fontsize=16,
                                     fontweight='bold')
            self.Chi_k.axes.set_ylabel('Reletive Intensity (a.u.)',
                                       fontsize=16,
                                       fontweight='bold')
            self.Chi_k.axes.set_xlabel('$k$ $[\AA^{-1}]$',
                                       fontsize=16,
                                       fontweight='bold')

            # Change the axes border width
            for axis in ['top', 'bottom', 'left', 'right']:
                self.FTR.axes.spines[axis].set_linewidth(2)
                self.Chi_k.axes.spines[axis].set_linewidth(2)

            # plt.subplots_adjust(top=0.85)
            # gs1.tight_layout(fig, rect=[0, 0.03, 1, 0.95])
            self.fig.tight_layout(rect=[0.03, 0.03, 1, 0.95], w_pad=1.1)

            # put window to the second monitor
            # figManager.window.setGeometry(1923, 23, 640, 529)
            self.figManager.window.setGeometry(1920, 20, 1920, 1180)

            plt.show()

            self.fig.suptitle(self.graph_title_txt,
                              fontsize=self.suptitle_fontsize,
                              fontweight='normal')

            # put window to the second monitor
            # figManager.window.setGeometry(1923, 23, 640, 529)
            # self.figManager.window.setGeometry(780, 20, 800, 600)
            self.figManager.window.setWindowTitle('Compare xftf')
            self.figManager.window.showMinimized()
def main(conf_file='config.cfg', logfile=None):
    #%% parameters
    print "reading config parameters..."
    config, pars = front_end.parser(conf_file)

    if pars.has_key('logging') and pars['logging']:
        print "recording configuration file..."
        front_end.record_config_file(pars)

        logfile = front_end.make_logfile_name(pars)

    #%% create and initialize the network
    if pars['train_load_net'] and os.path.exists(pars['train_load_net']):
        print "loading network..."
        net = netio.load_network(pars)
        # load existing learning curve
        lc = zstatistics.CLearnCurve(pars['train_load_net'])
    else:
        if pars['train_seed_net'] and os.path.exists(pars['train_seed_net']):
            print "seeding network..."
            net = netio.seed_network(pars, is_seed=True)
        else:
            print "initializing network..."
            net = netio.init_network(pars)
        # initalize a learning curve
        lc = zstatistics.CLearnCurve()

    # show field of view
    print "field of view: ", net.get_fov()
    print "output volume info: ", net.get_outputs_setsz()

    # set some parameters
    print 'setting up the network...'
    vn = utils.get_total_num(net.get_outputs_setsz())
    eta = pars['eta']  #/ vn
    net.set_eta(eta)
    net.set_momentum(pars['momentum'])
    net.set_weight_decay(pars['weight_decay'])

    # initialize samples
    outsz = pars['train_outsz']
    print "\n\ncreate train samples..."
    smp_trn = front_end.CSamples(config, pars, pars['train_range'], net, outsz,
                                 logfile)
    print "\n\ncreate test samples..."
    smp_tst = front_end.CSamples(config, pars, pars['test_range'], net, outsz,
                                 logfile)

    # initialization
    elapsed = 0
    err = 0
    cls = 0

    # interactive visualization
    plt.ion()
    plt.show()

    # the last iteration we want to continue training
    iter_last = lc.get_last_it()

    print "start training..."
    start = time.time()
    print "start from ", iter_last + 1
    for i in xrange(iter_last + 1, pars['Max_iter'] + 1):
        vol_ins, lbl_outs, msks = smp_trn.get_random_sample()

        # forward pass
        vol_ins = utils.make_continuous(vol_ins, dtype=pars['dtype'])

        props = net.forward(vol_ins)

        # cost function and accumulate errors
        props, cerr, grdts = pars['cost_fn'](props, lbl_outs)
        err = err + cerr
        cls = cls + cost_fn.get_cls(props, lbl_outs)

        # mask process the gradient
        grdts = utils.dict_mul(grdts, msks)

        # run backward pass
        grdts = utils.make_continuous(grdts, dtype=pars['dtype'])
        net.backward(grdts)

        if pars['is_malis']:
            malis_weights = cost_fn.malis_weight(props, lbl_outs)
            grdts = utils.dict_mul(grdts, malis_weights)

        if i % pars['Num_iter_per_test'] == 0:
            # test the net
            lc = test.znn_test(net, pars, smp_tst, vn, i, lc)

        if i % pars['Num_iter_per_show'] == 0:
            # anneal factor
            eta = eta * pars['anneal_factor']
            net.set_eta(eta)
            # normalize
            err = err / vn / pars['Num_iter_per_show']
            cls = cls / vn / pars['Num_iter_per_show']
            lc.append_train(i, err, cls)

            # time
            elapsed = time.time() - start
            elapsed = elapsed / pars['Num_iter_per_show']

            show_string = "iteration %d,    err: %.3f,    cls: %.3f,   elapsed: %.1f s/iter, learning rate: %.6f"\
                    %(i, err, cls, elapsed, eta )

            if pars.has_key('logging') and pars['logging']:
                utils.write_to_log(logfile, show_string)
            print show_string

            if pars['is_visual']:
                # show results To-do: run in a separate thread
                front_end.inter_show(start, lc, eta, vol_ins, props, lbl_outs,
                                     grdts, pars)
                if pars['is_rebalance'] and 'aff' not in pars['out_type']:
                    plt.subplot(247)
                    plt.imshow(msks.values()[0][0, 0, :, :],
                               interpolation='nearest',
                               cmap='gray')
                    plt.xlabel('rebalance weight')
                if pars['is_malis']:
                    plt.subplot(248)
                    plt.imshow(malis_weights.values()[0][0, 0, :, :],
                               interpolation='nearest',
                               cmap='gray')
                    plt.xlabel('malis weight (log)')
                plt.pause(2)
                plt.show()
            # reset err and cls
            err = 0
            cls = 0
            # reset time
            start = time.time()

        if i % pars['Num_iter_per_save'] == 0:
            # save network
            netio.save_network(net, pars['train_save_net'], num_iters=i)
            lc.save(pars, elapsed)
Beispiel #46
0
    plotOption = sys.argv[2]
except IndexError:
    print "No plot option provided. Options are: plot and movie"
    sys.exit(1)

if plotOption == "plot":
    for instance in planets_resh:
        try:
            plot(instance[:, 0], instance[:, 1], '*')
            hold('on')
        except:
            print "ValueError!"

# movie:
if plotOption == "movie":
    ion()
    figure()
    colors = ['*r', '*b', '*m']
    min_length = 0
    min_x = 0
    max_x = 0
    min_y = 0
    max_y = 0
    for i in range(len(planets_resh)):
        max_x_tmp = planets_resh[i].max()
        if max_x_tmp > max_x:
            max_x = max_x_tmp
        min_x_tmp = planets_resh[i].min()
        if min_x_tmp < min_x:
            min_x = min_x_tmp
        max_y_tmp = planets_resh[i].max()
Beispiel #47
0
    def move(self):
        """ Move the Person

        """
        if self.pdshow:
            fig = plt.gcf()
            fig, ax = self.L.showG('w',
                                   labels=False,
                                   alphacy=0.,
                                   edges=False,
                                   fig=fig)
            plt.draw()
            plt.ion()
        while True:
            if self.moving:
                if self.sim.verbose:
                    print('meca: updt ag ' + self.ID + ' @ ', self.sim.now())

                # if np.allclose(conv_vecarr(self.destination)[:2],self.L.Gw.pos[47]):
                #     import ipdb
                #     ipdb.set_trace()

                while self.cancelled:
                    yield passivate, self
                    print("Person.move: activated after being cancelled")
                checked = []
                for zone in self.world.zones(self):
                    if zone not in checked:
                        checked.append(zone)
                        zone(self)

                # updating acceleration
                acceleration = self.steering_mind(self)
                acceleration = acceleration.truncate(self.max_acceleration)
                self.acceleration = acceleration

                # updating velocity
                velocity = self.velocity + acceleration * self.interval
                self.velocity = velocity.truncate(self.max_speed)

                if velocity.length() > 0.2:
                    # record direction only when we've really had some
                    self.localy = velocity.normalize()
                    self.localx = vec3(self.localy.y, -self.localy.x)

                # updating position
                self.position = self.position + self.velocity * self.interval
                #                self.update()
                self.position.z = 0
                self.world.update_boid(self)

                self.net.update_pos(self.ID, conv_vecarr(self.position),
                                    self.sim.now())
                p = conv_vecarr(self.position).reshape(3, 1)
                v = conv_vecarr(self.velocity).reshape(3, 1)
                a = conv_vecarr(self.acceleration).reshape(3, 1)

                # fill panda dataframe 2D trajectory
                self.df = self.df.append(
                    pd.DataFrame(
                        {
                            't': pd.Timestamp(self.sim.now(), unit='s'),
                            'x': p[0],
                            'y': p[1],
                            'vx': v[0],
                            'vy': v[1],
                            'ax': a[0],
                            'ay': a[1]
                        },
                        columns=['t', 'x', 'y', 'vx', 'vy', 'ax', 'ay']))

                if self.pdshow:
                    ptmp = np.array([p[:2, 0], p[:2, 0] + v[:2, 0]])

                    if hasattr(self, 'pl'):
                        self.pl[0].set_data(self.df['x'].tail(1),
                                            self.df['y'].tail(1))
                        self.pla[0].set_data(ptmp[:, 0], ptmp[:, 1])
                        circle = plt.Circle(
                            (self.df['x'].tail(1), self.df['y'].tail(1)),
                            radius=self.radius,
                            alpha=0.3)
                        ax.add_patch(circle)
                    else:
                        self.pl = ax.plot(self.df['x'].tail(1),
                                          self.df['y'].tail(1),
                                          'o',
                                          color=self.color,
                                          ms=self.radius * 10)
                        self.pla = ax.plot(ptmp[:, 0], ptmp[:, 1], 'r')
                        circle = plt.Circle(
                            (self.df['x'].tail(1), self.df['y'].tail(1)),
                            radius=self.radius,
                            alpha=0.3)
                        ax.add_patch(circle)
                    # try:
                    #     fig,ax=plu.displot(p[:2],p[:2]+v[:2],'r')
                    # except:
                    #     pass
                    # import ipdb
                    # ipdb.set_trace()
                    plt.draw()
                    plt.pause(0.0001)
                if 'mysql' in self.save:
                    self.db.writemeca(self.ID, self.sim.now(), p, v, a)

                if 'txt' in self.save:
                    pyu.writemeca(self.ID, self.sim.now(), p, v, a)

                # new target when arrived in poi

                if self.arrived and\
                    (self.L.pt2ro(self.position) ==\
                        self.L.Gw.node[self.rooms[1]]['room']):

                    self.arrived = False
                    if self.endpoint:
                        self.endpoint = False
                        self.roomId = self.nextroomId
                        # remove the remaining waypoint which correspond
                        # to current room position
                        del self.waypoints[0]
                        del self.rooms[0]
                        # del self.dlist[0]
                        #
                        # If door lets continue
                        #
                        #
                        # ig destination --> next room
                        #
                        #adjroom  = self.L.Gr.neighbors(self.roomId)
                        #Nadjroom = len(adjroom)
                        if self.cdest == 'random':
                            # self.nextroomId   = int(np.floor(random.uniform(0,self.L.Gr.size())))
                            self.nextroomId = random.sample(
                                self.L.Gr.nodes(), 1)[0]
                            # test 1 ) next != actualroom
                            #      2 ) nextroom != fordiden room
                            #      3 ) room not share without another agent
                            while self.nextroomId == self.roomId or (
                                    self.nextroomId in self.forbidroomId
                            ):  # or (self.nextroomId in self.sim.roomlist):
                                # self.nextroomId   = int(np.floor(random.uniform(0,self.L.Gr.size())))
                                self.nextroomId = random.sample(
                                    self.L.Gr.nodes(), 1)[0]
                        elif self.cdest == 'file':
                            self.room_counter = self.room_counter + 1
                            if self.room_counter >= self.nb_room:
                                self.room_counter = 0
                            self.nextroomId = self.room_seq[self.room_counter]
                            self.wait = self.room_wait[self.room_counter]
                        #self.sim.roomlist.append(self.nextroomId) # list of all destiantion of all nodes in object sim
                        self.rooms, wp = self.L.waypointGw(
                            self.roomId, self.nextroomId)
                        # self.dlist =  [i in self.L.Gw.ldo for i in self.rooms]
                        for tup in wp[1:]:
                            self.waypoints.append(vec3(tup))
                    #nextroom = adjroom[k]
                    #    print "room : ",self.roomId
                    #    print "nextroom : ",self.nextroomId
                    #p_nextroom = self.L.Gr.pos[self.nextroomId]
                    #setdoors1  = self.L.Gr.node[self.roomId]['doors']
                    #setdoors2  = self.L.Gr.node[nextroom]['doors']
                    #doorId     = np.intersect1d(setdoors1,setdoors2)[0]
                    #
                    # coord door
                    #
                    #unode = self.L.Gs.neighbors(doorId)
                    #p1    = self.L.Gs.pos[unode[0]]
                    #p2    = self.L.Gs.pos[unode[1]]
                    #print p1
                    #print p2
                    #pdoor = (np.array(p1)+np.array(p2))/2
                        self.destination = self.waypoints[0]

                        if self.sim.verbose:
                            print('meca: ag ' + self.ID + ' wait ' +
                                  str(self.wait))
                        yield hold, self, self.wait

                    else:
                        del self.waypoints[0]
                        del self.rooms[0]
                        # del self.dlist[0]
                        #print "wp : ", self.waypoints
                        if len(self.waypoints) == 1:
                            self.endpoint = True
                        self.destination = self.waypoints[0]
                    #print "dest : ", self.destination
                else:
                    yield hold, self, self.interval
            else:
                #                self.update()
                self.world.update_boid(self)
                self.net.update_pos(self.ID, conv_vecarr(self.position),
                                    self.sim.now())

                yield hold, self, self.interval
Beispiel #48
0
    if matplotlib_backend in mpl_backends:
        plt.switch_backend(matplotlib_backend)
    else:
        print "Warning: Matplotlib backend", matplotlib_backend, "not available"
        print "Available backends:", mpl_backends
    from matplotlib import pylab as pl
    import matplotlib.ticker as ticker
    from matplotlib import rc, colors
    import matplotlib.patches as mpatches
    import matplotlib.path as mpath
    import matplotlib.cm as cm
    from matplotlib import lines
    from mpl_toolkits.mplot3d import axes3d
    from matplotlib import lines  # for plotting lines in pendulum and PST
    from matplotlib.patches import ConnectionStyle  # for cartpole
    pl.ion()
else:
    print 'matplotlib is not available => No Graphics'

if module_exists('networkx'):
    import networkx as nx
else:
    'networkx is not available => No Graphics on SystemAdmin domain'

if module_exists('sklearn'):
    from sklearn import svm
else:
    'sklearn is not available => No BEBF representation available'
from scipy import stats
from scipy import misc
from scipy import linalg
Beispiel #49
0
    def plot_rssi(self, port):
        self.count = 0
        self.dt = 0.5
        self.tlen = 120
        # Generate mesh for plotting
        Y, X = np.mgrid[slice(0 - .5, 26 + 1.5, 1),
                        slice(0 - self.dt / 2, self.tlen + 1 -
                              self.dt / 2, self.dt)]
        Z = np.zeros_like(X)
        # X and Y are bounds, so Z should be the value *inside* those bounds.
        # Therefore, remove the last value from the Z array.
        Z = Z[:, :-1]
        logging.debug("Creating figure")
        fig = plt.figure()
        pcm = plt.pcolormesh(X,
                             Y,
                             Z,
                             vmin=-128,
                             vmax=0,
                             cmap=plt.cm.get_cmap('jet'))
        plt.xlim([0, self.tlen])
        plt.ylim([0, 26])
        plt.colorbar(label="Measured signal level [dB]")
        plt.ylabel("Channel number")
        plt.xlabel("Time [s]")
        plt.ion()
        logging.debug("Show plot window")
        plt.show()
        ch_min = 26
        ch_max = 0
        last_update = time.time()

        logging.info("Begin collecting data from serial port")
        while True:
            line = port.readline().rstrip()

            pkt_data = re.match(
                r"\[([-+]?\d+),\s*([-+]?\d+),\s*([-+]?\d+)\]\s*(.*)",
                line.decode(errors='replace'))
            if pkt_data:
                now = time.time()
                try:
                    iface_id = int(pkt_data.group(1))
                    timestamp = int(pkt_data.group(2))
                    count = int(pkt_data.group(3))
                    tidx = int(timestamp / (self.dt * 1000000)) % (Z.shape[1])
                except ValueError:
                    continue
                logging.debug("data: tidx=%d if=%d t=%d", tidx, iface_id,
                              timestamp)
                raw = pkt_data.group(4)
                resize = False
                for ch_ed in raw.split(","):
                    try:
                        pair = ch_ed.split(":")
                        ch = int(pair[0])
                        ed = float(pair[1])
                        if ch < ch_min:
                            ch_min = ch
                            resize = True
                        if ch > ch_max:
                            ch_max = ch
                            resize = True
                        Z[ch, tidx] = ed
                    except (ValueError, IndexError):
                        continue
                    #print("ch: %d ed: %d" % (ch, ed))
                if resize:
                    logging.debug("resize: %d %d" % (ch_min, ch_max))
                    plt.ylim([ch_min - .5, ch_max + .5])
                if now > last_update + 1:
                    last_update = now
                    #pcm = plt.pcolormesh(X, Y, Z)
                    pcm.set_array(Z.ravel())
                    pcm.autoscale()
                    pcm.changed()
            plt.pause(0.01)
Beispiel #50
0
def make_waterfall_plots(filenames_list,
                         target,
                         f_start,
                         f_stop,
                         ion=False,
                         epoch=None,
                         local_host='',
                         plot_name='',
                         save_pdf_plot=False,
                         saving_fig=False,
                         **kwargs):
    ''' Makes waterfall plots per group of ON-OFF pairs (up to 6 plots.)
    '''

    matplotlib.rc('font', **font)

    if ion:
        plt.ion()

    min_val = 0
    max_val = 5.
    factor = 1e6
    units = 'Hz'
    print(target)

    n_plots = len(filenames_list)
    fig = plt.subplots(n_plots,
                       sharex=True,
                       sharey=True,
                       figsize=(10, 2 * n_plots))

    #finding plotting values range
    fil = Filterbank(filenames_list[0], f_start=f_start, f_stop=f_stop)
    plot_f, plot_data = fil.grab_data(f_start, f_stop, 0)
    dec_fac_x, dec_fac_y = 1, 1
    if plot_data.shape[0] > MAX_IMSHOW_POINTS[0]:
        dec_fac_x = plot_data.shape[0] / MAX_IMSHOW_POINTS[0]
    if plot_data.shape[1] > MAX_IMSHOW_POINTS[1]:
        dec_fac_y = plot_data.shape[1] / MAX_IMSHOW_POINTS[1]
    plot_data = rebin(plot_data, dec_fac_x, dec_fac_y)

    A1_avg = np.median(plot_data)
    A1_max = plot_data.max()
    A1_std = np.std(plot_data)

    if not epoch:
        epoch = fil.header['tstart']

    labeling = ['A', 'B', 'A', 'C', 'A', 'D']

    #    delta_f = ('%f0.6'%np.abs(f_start-f_stop))
    delta_f = np.abs(f_start - f_stop)
    mid_f = np.abs(f_start + f_stop) / 2.

    for i, filename in enumerate(filenames_list):
        print(filename)
        plt.subplot(n_plots, 1, i + 1)

        fil = Filterbank(filename, f_start=f_start, f_stop=f_stop)
        this_plot = plot_waterfall(fil,
                                   f_start=f_start,
                                   f_stop=f_stop,
                                   vmin=A1_avg - A1_std * min_val,
                                   vmax=A1_avg + max_val * A1_std,
                                   **kwargs)

        if i == 0:
            plt.title(target.replace('HIP', 'HIP '))

        if i < len(filenames_list) - 1:
            plt.xticks(np.arange(f_start, f_stop, delta_f / 4.),
                       ['', '', '', ''])

    #Some plot formatting.
    ax = plt.gca()
    ax.get_xaxis().get_major_formatter().set_useOffset(False)
    print('delta_f', delta_f)
    plt.xticks(np.arange(f_start, f_stop, delta_f / 4.), [
        round(loc_freq) for loc_freq in np.arange(
            (f_start - mid_f), (f_stop - mid_f), delta_f / 4.) * factor
    ])
    plt.xlabel("Relative Frequency [%s] from %f MHz" % (units, mid_f),
               fontdict=font)

    #to plot color bar. for now.
    cax = fig[0].add_axes([0.9, 0.11, 0.03, 0.77])
    fig[0].colorbar(this_plot, cax=cax, label='Power [Arbitrary Units]')

    # Fine-tune figure; make subplots close to each other and hide x ticks for
    # all but bottom plot.
    plt.subplots_adjust(hspace=0, wspace=0)

    if saving_fig:
        if not plot_name:
            plot_name = 'Candidate_waterfall_plots.%s.t%.0f.%s.f%.0f.png' % (
                target, epoch, local_host, mid_f * 1e6)

        print('Saving png figure.')
        plt.savefig(plot_name, bbox_inches='tight')
        if save_pdf_plot:
            print('Saving pdf figure.')
            plt.savefig(plot_name.replace('.png', '') + '.pdf',
                        format='pdf',
                        dpi=300,
                        bbox_inches='tight')
Beispiel #51
0
def plot_waveforms(waveforms,
                   sensors,
                   evt,
                   *,
                   wf_type="PMT",
                   range=(None, ),
                   overlay=False,
                   sum=False,
                   zoomx=False,
                   zoomy=False,
                   dual=False):
    range = slice(*range)

    wfsize = waveforms.shape[2]
    time = np.arange(wfsize).astype(float)
    if wf_type == "PMT": time /= 40
    elif wf_type == "BLR": time /= 40
    elif wf_type == "SiPM": pass
    else:
        raise ValueError("Unrecognized wf type {}. ".format(wf_type) +
                         "Valid options: are 'PMT', 'BLR' and 'SiPM'")

    if sum: wf_type += " SUM"

    gmin, gmax = float("inf"), -float("inf")
    plt.ion()
    ax1 = plt.gca()
    if sum:
        sum_wf = np.zeros(waveforms.shape[2])

    if dual:
        for wf, wf_dual, ID, color in zip(waveforms[0][range],
                                          waveforms[1][range], sensors[range],
                                          _colors):
            ymin, ymax = min(wf_dual), max(wf_dual)
            if ymin < gmin: gmin = ymin
            if ymax > gmax: gmax = ymax

            plt.plot(wf, drawstyle="steps", label=str(ID[0]), c=color)
            plt.plot(wf_dual,
                     drawstyle="steps",
                     label=str(ID[0]),
                     c=next(_colors))

            ylim = (0.99 * ymin, 1.01 * ymax)
            customize_plot(zoomx, zoomy if zoomy else ylim, wf_type, evt,
                           ID[0])
            show_and_wait()
    else:
        for wf, ID, color in zip(waveforms[0][range], sensors[range], _colors):
            ymin, ymax = min(wf), max(wf)
            if ymin < gmin: gmin = ymin
            if ymax > gmax: gmax = ymax

            if sum:
                bls_wf = wf - mode(wf)
                sum_wf = sum_wf + bls_wf * (1 if "SiPM" in wf_type else -1)
            else:
                plt.plot(wf, drawstyle="steps", label=str(ID[0]), c=color)

            if not overlay and not sum:
                ylim = (0.99 * ymin, 1.01 * ymax)
                customize_plot(zoomx, zoomy if zoomy else ylim, wf_type, evt,
                               ID[0])
                show_and_wait()

    if overlay:
        ylim = 0.99 * gmin, 1.01 * gmax
        customize_plot(zoomx, zoomy if zoomy else ylim, wf_type, evt)
        show_and_wait()

    if sum:
        ylim = np.min(sum_wf) - 50, np.max(sum_wf) + 50
        plt.plot(sum_wf, drawstyle="steps", c="k")
        customize_plot(zoomx, zoomy if zoomy else ylim, wf_type, evt)
        show_and_wait()
Beispiel #52
0
def ratdisp(workdir='.', targetdir='.', cams=[0, 1, 2, 3], auto=False):

    # check for non-list camera argument
    if type(cams) is not list:
        cams = [cams]  # convert to list

    pl.ion()  # pylab in interactive mode

    # move to working directory
    start_dir = os.getcwd()
    os.chdir(workdir)

    d = os.getcwd().split('/')[-1]  # name of current directory
    if not auto:
        print "\n* * * displaying science frames in {} for selection * * *".format(
            d)
    else:
        print "\n* * * selecting all science frames in {} * * *".format(d)

    # make target directory if it does not exist
    if not os.path.exists(targetdir):
        print "Creating target directory: ", targetdir
        os.makedirs(targetdir)
    # warn user if previous files may be overwritten
    else:
        print "Warning: Target directory exists. Existing files will be overwritten."
        resp = raw_input("Proceed? (y/n): ")
        if resp.lower() != 'y':
            print "Exiting..."
            os.chdir(start_dir)  # move back to starting directory
            return

    # remove tailing / from target directory name if present
    if targetdir[-1] == '/':
        targetdir = targetdir[:-1]

    # delete existing object and sky lists
    fntemp = glob('{}/{}*.list'.format(targetdir, af.OBJ_NAME))
    for fn in fntemp:
        os.remove(fn)
    fntemp = glob('{}/{}*.list'.format(targetdir, af.SKY_NAME))
    for fn in fntemp:
        os.remove(fn)

    # work on FITs files for specified cameras
    for cam_i in cams:

        # print current camera number
        print "\n* * * CAMERA {} * * *".format(cam_i)

        # CCDs
        if cam_i in [0, 1]:
            fn_list = '{}_{}.list'.format(af.CAM_NAMES[cam_i], d)
            try:
                fin = open(fn_list, 'r')  # open list of FITs files
            except IOError:
                print "Warning: {} not found.  Skipping camera {}.".format(
                    fn_list, cam_i)
            else:

                # look at FITs data sequentially
                for line in fin:

                    fits_fn = line.rstrip(
                    )  # current fits file name with return removed
                    fits_id = fits_fn.split('.')[
                        0]  # fits file name with extention removed
                    print '\n{}'.format(fits_fn)

                    # open data
                    hdulist = pf.open(fits_fn)
                    im = hdulist[0].data
                    h = hdulist[0].header

                    # check for required header keywords
                    if 'PRPSLID' in h:
                        prpslid = h['PRPSLID']
                    else:
                        "ERROR: ratdisp - PRPSLID not found in fits header."
                        os.chdir(start_dir)  # move back to starting directory
                        pl.close('all')  # close image to free memory
                        return
                    if 'VSTID' in h:
                        vstid = h['VSTID']
                    else:
                        "ERROR: ratdisp - VSTID keyword not found in fits header."
                        os.chdir(start_dir)  # move back to starting directory
                        pl.close('all')  # close image to free memory
                        return

                    targname = '{}-vis{}'.format(prpslid, vstid)
                    targname_sky = '{}-sky'.format(targname)

                    # rotate image or not
                    im = np.rot90(im, af.CAM_ROTAT[cam_i])

                    # get image statistics
                    m = np.median(im)
                    s = af.robust_sigma(im)

                    # display image and prompt user
                    if not auto:
                        implot = zoom(
                            im, ZOOM_LVL)  # change image size for display
                        axim = pl.imshow(implot,
                                         vmin=m - 5 * s,
                                         vmax=m + 5 * s,
                                         origin='lower',
                                         cmap=pl.cm.gray)
                        axim.axes.set_xticks(axim.axes.get_xlim())
                        axim.axes.set_xticklabels(['E', 'W'])
                        axim.axes.set_yticks(axim.axes.get_ylim())
                        axim.axes.set_yticklabels(['S', 'N'])
                        pl.title(
                            r"{} band, Median = {}, $\sigma$ = {:.1f}".format(
                                h['FILTER'], int(m), s))

                    # print filter name
                    print '\t* Filter used: {}'.format(h['FILTER'])

                    # query user until valid response is provided
                    valid_entry = False
                    while not valid_entry:

                        # either select all if auto, or have user select
                        if auto:
                            direction = 'y'
                        else:
                            direction = raw_input(
                                "\nType Y for YES, N for NO, Q for QUIT: ")

                        if direction.lower() == 'y':

                            # set keyword values
                            h['PIXSCALE'] = af.CAM_PXSCALE[cam_i]
                            h['WAVELENG'] = 'OPT'
                            h['GAIN'] = (af.CAM_GAIN[cam_i](h['SOFTGAIN']),
                                         'in electrons/DN')
                            h['SATURATE'] = (af.CAM_SATUR[cam_i](
                                h['SOFTGAIN']), 'in electrons/DN')

                            # object frame
                            imfits = '{}/{}_{}_{}.fits'.format(
                                targetdir, fits_id, af.OBJ_NAME, cam_i)
                            h['TARGNAME'] = targname
                            hdulist.writeto(imfits,
                                            clobber=True)  # save object frame
                            fout = open(
                                '{}/{}_{}.list'.format(targetdir, af.OBJ_NAME,
                                                       h['FILTER']),
                                'a')  # append if this filter's list exists
                            fout.write(fits_id +
                                       '\n')  # write new file name to list
                            fout.close()

                            # sky frame
                            skyfits = '{}/{}_{}_{}.fits'.format(
                                targetdir, fits_id, af.SKY_NAME, cam_i)
                            h['TARGNAME'] = targname_sky
                            hdulist.writeto(skyfits,
                                            clobber=True)  # save sky frame
                            fout = open(
                                '{}/{}_{}.list'.format(targetdir, af.SKY_NAME,
                                                       h['FILTER']),
                                'a')  # append if this filter's list exists
                            fout.write(fits_id +
                                       '\n')  # write new file name to list
                            fout.close()

                            valid_entry = True

                        elif direction.lower() == 'q':  # exit function
                            print "Exiting..."
                            os.chdir(
                                start_dir)  # move back to starting directory
                            pl.close('all')  # close image to free memory
                            return

                        elif direction.lower() != 'n':  # invalid case
                            print "'{}' is not a valid entry.".format(
                                direction)

                        else:  # 'N' selected, skip
                            valid_entry = True

                    hdulist.close()  # close FITs file
                    pl.close('all')  # close image to free memory

                # close files
                fin.close()

        # H2RGs
        if cam_i in [2, 3]:
            fn_list = '{}_{}.list'.format(af.CAM_NAMES[cam_i], d)
            try:
                fin = open(fn_list, 'r')  # open list of FITs files
            except IOError:
                print "Warning: {} not found.  Skipping camera {}.".format(
                    fn_list, cam_i)
            else:
                fout_img = [
                    open(
                        '{}/{}_{}.list'.format(targetdir, af.OBJ_NAME,
                                               af.H2RG_FILTERS[cam_i - 2]),
                        'w'),
                    open(
                        '{}/{}_{}.list'.format(targetdir, af.OBJ_NAME,
                                               af.H2RG_FILTERS[cam_i]), 'w')
                ]  # create list files for new img FITs files (e, w)
                fout_sky = [
                    open(
                        '{}/{}_{}.list'.format(targetdir, af.SKY_NAME,
                                               af.H2RG_FILTERS[cam_i - 2]),
                        'w'),
                    open(
                        '{}/{}_{}.list'.format(targetdir, af.SKY_NAME,
                                               af.H2RG_FILTERS[cam_i]), 'w')
                ]  # create list files for new sky FITs files (e, w)

                # look at FITs data sequentially
                for line in fin:

                    fits_fn = line.rstrip(
                    )  # current fits file name with return removed
                    fits_id = fits_fn.split('.')[
                        0]  # fits file name with extention removed
                    print '\n{}'.format(fits_fn)

                    # open data
                    hdulist = pf.open(fits_fn)
                    im = hdulist[0].data
                    h = hdulist[0].header

                    # check for required header keywords
                    if 'PRPSLID' in h:
                        prpslid = h['PRPSLID']
                    else:
                        "ERROR: ratdisp - PRPSLID not found in fits header."
                        os.chdir(start_dir)  # move back to starting directory
                        pl.close('all')  # close image to free memory
                        return
                    if 'VSTID' in h:
                        vstid = h['VSTID']
                    else:
                        "ERROR: ratdisp - VSTID keyword not found in fits header."
                        os.chdir(start_dir)  # move back to starting directory
                        pl.close('all')  # close image to free memory
                        return
                    if af.CENTER_KEY in h:
                        center = h[af.CENTER_KEY].split('center')[0]
                    else:
                        "ERROR: ratdisp - {} keyword not found in fits header.".format(
                            af.CENTER_KEY)
                        os.chdir(start_dir)  # move back to starting directory
                        pl.close('all')
                        return

                    targname = '{}-vis{}'.format(prpslid, vstid)
                    targname_sky = '{}-sky'.format(targname)

                    # rotate FITs data to align with filters (Z/J in E-left, Y/H in W-right)
                    im = np.rot90(im, af.CAM_ROTAT[cam_i])
                    if cam_i == 3:
                        im = np.flipud(im)  # flip C3 about y axis

                    # get image statistics
                    imleft = im[af.H2RG_SLICES[cam_i - 2]]
                    mleft = np.median(imleft)
                    sleft = af.robust_sigma(imleft)
                    imright = im[af.H2RG_SLICES[cam_i]]
                    mright = np.median(imright)
                    sright = af.robust_sigma(imright)

                    # display image and prompt user
                    if not auto:
                        imleft = zoom(
                            imleft, ZOOM_LVL)  # change image size for display
                        pl.subplot(121)
                        pl.subplots_adjust(left=0.125,
                                           bottom=0.1,
                                           right=0.9,
                                           top=0.9,
                                           wspace=0.0,
                                           hspace=0.2)
                        axim = pl.imshow(imleft,
                                         vmin=mleft - 5 * sleft,
                                         vmax=mleft + 5 * sleft,
                                         origin='lower',
                                         cmap=pl.cm.gray)
                        axim.axes.set_xticks([axim.axes.get_xlim()[0]])
                        axim.axes.set_xticklabels(['E'])
                        axim.axes.set_yticks(axim.axes.get_ylim())
                        axim.axes.set_yticklabels(['S', 'N'])
                        pl.title(r"{} band".format(af.H2RG_FILTERS[cam_i -
                                                                   2]) + '\n' +
                                 "Median = {}, $\sigma$ = {:.1f}".format(
                                     int(mleft), sleft))
                        imright = zoom(
                            imright, ZOOM_LVL)  # change image size for display
                        pl.subplot(122)
                        axim = pl.imshow(imright,
                                         vmin=mright - 5 * sright,
                                         vmax=mright + 5 * sright,
                                         origin='lower',
                                         cmap=pl.cm.gray)
                        axim.axes.set_xticks([axim.axes.get_xlim()[1]])
                        axim.axes.set_xticklabels(['W'])
                        axim.axes.set_yticks([])
                        pl.title(r"{} band".format(af.H2RG_FILTERS[cam_i]) +
                                 '\n' +
                                 "Median = {}, $\sigma$ = {:.1f}".format(
                                     int(mright), sright))

                    # print target center for user
                    if center.count(af.H2RG_FILTERS[cam_i]) != 0:
                        print "\t* The target is focused on the {} filter.".format(
                            af.H2RG_FILTERS[cam_i])
                    elif center.count(af.H2RG_FILTERS[cam_i - 2]) != 0:
                        print "\t* The target is focused on the {} filter.".format(
                            af.H2RG_FILTERS[cam_i - 2])
                    else:
                        print "\t* Warning: The target is NOT focused on an H2RG filter. The target is focused on the {} filter.".format(
                            center)

                    # query user until valid response is provided
                    valid_entry = False
                    while not valid_entry:

                        # either select based on center keyword if auto, or have user select
                        if auto:
                            if center.count(af.H2RG_FILTERS[cam_i]) != 0:
                                direction = 'w'
                            elif center.count(af.H2RG_FILTERS[cam_i - 2]) != 0:
                                direction = 'e'
                            else:
                                print "\t* Warning: Skipping frame not centered on H2RG filter."
                                direction = 'n'
                        else:
                            direction = raw_input(
                                "\nType E for EAST, W for WEST, N for NEXT, Q for QUIT: "
                            )

                        if direction.lower() == 'e' or direction.lower(
                        ) == 'w':  # selected
                            h['PIXSCALE'] = af.CAM_PXSCALE[cam_i]
                            h['WAVELENG'] = 'IR'
                            h['GAIN'] = (af.CAM_GAIN[cam_i](h['SOFTGAIN']),
                                         'in electrons/DN')
                            h['SATURATE'] = (af.CAM_SATUR[cam_i](
                                h['SOFTGAIN']), 'in electrons/DN')

                            if direction.lower() == 'e':
                                f_img = cam_i - 2
                                f_sky = cam_i
                            else:
                                f_img = cam_i
                                f_sky = cam_i - 2

                            # filter side with object
                            imfits = '{}/{}_{}_{}.fits'.format(
                                targetdir, fits_id, af.OBJ_NAME,
                                af.H2RG_FILTERS[f_img])
                            im_img = im[af.H2RG_SLICES[f_img]]
                            h['NAXIS1'] = af.H2RG_SLICES[f_img][
                                0].stop - af.H2RG_SLICES[f_img][0].start
                            h['NAXIS2'] = af.H2RG_SLICES[f_img][
                                1].stop - af.H2RG_SLICES[f_img][1].start
                            h['FILTER'] = af.H2RG_FILTERS[f_img]
                            h['TARGNAME'] = targname
                            pf.writeto(imfits, im_img, header=h,
                                       clobber=True)  # save object frame
                            fout_img[0].write('{}_{}_{}\n'.format(
                                fits_id, af.OBJ_NAME, af.H2RG_FILTERS[f_img])
                                              )  # write new file name to list

                            # filter side with sky
                            skyfits = '{}/{}_{}_{}.fits'.format(
                                targetdir, fits_id, af.SKY_NAME,
                                af.H2RG_FILTERS[f_sky])
                            im_sky = im[af.H2RG_SLICES[f_sky]]
                            h['NAXIS1'] = af.H2RG_SLICES[f_sky][
                                0].stop - af.H2RG_SLICES[f_sky][
                                    0].start  # repeat incase filter sizes differ
                            h['NAXIS2'] = af.H2RG_SLICES[f_sky][
                                1].stop - af.H2RG_SLICES[f_sky][1].start
                            h['FILTER'] = af.H2RG_FILTERS[f_sky]
                            h['TARGNAME'] = targname_sky
                            pf.writeto(skyfits, im_sky, header=h,
                                       clobber=True)  # save sky frame
                            fout_sky[0].write('{}_{}_{}\n'.format(
                                fits_id, af.SKY_NAME, af.H2RG_FILTERS[f_sky])
                                              )  # write new file name to list

                            valid_entry = True

                        elif direction.lower() == 'q':  # exit function
                            print "Exiting..."
                            os.chdir(
                                start_dir)  # move back to starting directory
                            pl.close('all')  # close image to free memory
                            return

                        elif direction.lower() != 'n':  # invalid case
                            print "'{}' is not a valid entry.".format(
                                direction)

                        else:  # 'N' selected, skip
                            valid_entry = True

                    hdulist.close()  # close FITs file
                    pl.close('all')  # close image to free memory

                # close files
                fin.close()
                for f in fout_img:
                    f.close()
                for f in fout_sky:
                    f.close()

    # move back to starting directory
    os.chdir(start_dir)
Beispiel #53
0
def run_detect(datasets_dir):
    filenames = [f for f in sorted(os.listdir(datasets_dir)) if f.lower().endswith(".png")]
    for i, filename in enumerate(filenames):
        print i, filename
        filename = os.path.join(datasets_dir, filename)
        # filename = '../samples/aa_test.png'
        image = cv2.imread(filename)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        show("gray", gray)
        
        ret, thresholded = cv2.threshold(gray, 250, 255, cv2.THRESH_BINARY_INV)
        show("thresholded", thresholded)
        toContour = thresholded
        
        # Added by Caryn to erase dots
        # # kernel = np.ones((3,3),np.uint8)
        # kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
        # opened = cv2.morphologyEx(thresholded, cv2.MORPH_OPEN, kernel)
        # show("opened", opened)
        # toContour = opened

        results = cv2.findContours(toContour, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
        if len(results) == 2:
            contours = results[0]
        else:
            contours = results[1]

        draw_image = ~image.copy()
        newimage = np.zeros(thresholded.shape, dtype=np.uint8)

        boxes = [cv2.boundingRect(contour) for contour in contours]
        widths = [box[2] for box in boxes]
        typical_width = np.median(widths)
        merge_width = int(typical_width)

        # merge letters to form word blocks
        for box in boxes:
           if box[2] > 5 * typical_width or box[3] > 5 * typical_width:
                continue
           cv2.rectangle(newimage, (box[0] - merge_width, box[1]), (box[0] + box[2] + merge_width, box[1] + box[3]), 255, -1)

        # refind contours in merged line image
        boximage = newimage.copy()
        results = cv2.findContours(newimage, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
        if len(results) == 2:
            contours = results[0]
        else:
            contours = results[1]
        # make histogram of x coverage of word boxes.
        hist_x1 = np.zeros(image.shape[1])
        for contour in contours:
            box = cv2.boundingRect(contour)
            hist_x1[box[0]:box[0]+box[2]] += 1

        max_x = np.max(hist_x1)
        line_x = np.where(hist_x1 > max_x * 0.6)
        maxtab, mintab = peakdetect(hist_x1, np.max(hist_x1) * 0.2)

        for i, x in maxtab:
            x = int(i) 
            cv2.line(draw_image, (x, 0), (x, 2000), (0, 0, 255), 2)
        draw_image[boximage != 0, 0] = 255

        cv2.imshow("process", draw_image)
        pylab.clf()
        pylab.plot(hist_x1)
        pylab.plot(maxtab[:, 0], maxtab[:, 1], 'o')
        pylab.ion()
        pylab.show()

        cv2.waitKey(0)
Beispiel #54
0
def ratdisp_calib(ftype=af.FLAT_NAME,
                  workdir='.',
                  cams=[0, 1, 2, 3],
                  auto=False,
                  amin=0.1,
                  amax=0.8):

    # check for non-list camera argument
    if type(cams) is not list:
        cams = [cams]  # convert to list

    pl.ion()  # pylab in interactive mode

    # move to working directory
    start_dir = os.getcwd()
    os.chdir(workdir)

    d = os.getcwd().split('/')[-1]  # name of current directory
    if not auto:
        print "\n* * * displaying {} frames in {} for selection * * *".format(
            ftype, d)
    else:
        print "\n* * * automatically selecting {} frames in {} * * *".format(
            ftype, d)

    # delete existing calibration lists
    fntemp = glob(ftype + '*.list')
    for fn in fntemp:
        os.remove(fn)

    # work on FITs files for specified cameras
    for cam_i in cams:

        # print current camera number
        print "\n* * * CAMERA {} * * *".format(cam_i)

        # CCDs
        if cam_i in [0, 1]:

            fn_list = '{}_{}.list'.format(af.CAM_NAMES[cam_i], d)
            try:
                fin = open(fn_list, 'r')  # open list of FITs files
            except IOError:
                print "Warning: {} not found.  Skipping camera {}.".format(
                    fn_list, cam_i)
            else:

                # look at FITs data sequentially
                for line in fin:

                    fits_fn = line.rstrip(
                    )  # current fits file name with return removed
                    fits_id = fits_fn.split('.')[
                        0]  # fits file name with extention removed
                    print '{}'.format(fits_fn)

                    # open data
                    hdulist = pf.open(fits_fn)
                    im = hdulist[0].data
                    h = hdulist[0].header

                    # all bias frames are selected
                    if auto and ftype is af.BIAS_NAME:
                        fout = open('{}_{}.list'.format(ftype, cam_i),
                                    'a')  # append if this filter's list exists
                        fout.write(fits_id +
                                   '\n')  # write new file name to list
                        fout.close()

                    else:

                        # no rotation needed (why is C0 rotated by 270 deg in IDL code?)
                        im = np.rot90(im, af.CAM_ROTAT[cam_i])

                        # Let user determine if the image is good or not
                        implot = zoom(
                            im, ZOOM_LVL)  # change image size for display
                        m = np.median(im)
                        s = af.robust_sigma(im)
                        print '\t* Median is {} counts.'.format(m)

                        # print filter name
                        print '\t* Filter used: {}'.format(h['FILTER'])

                        # select non-saturated flat frames with sufficient counts
                        if auto and ftype is af.FLAT_NAME:

                            # check whether median value is in specified range
                            sat_pt = af.CAM_SATUR[cam_i](h['SOFTGAIN'])
                            vmin = amin * sat_pt
                            vmax = amax * sat_pt
                            if m > vmin and m < vmax:
                                print "\t* Frame selected."
                                fout = open(
                                    '{}_{}.list'.format(ftype, h['FILTER']),
                                    'a')  # append if this filter's list exists
                                fout.write(fits_id +
                                           '\n')  # write new file name to list
                                fout.close()
                            else:
                                if m < vmin:
                                    print "\t* Frame rejected:\tUNDEREXPOSED."
                                else:
                                    print "\t* Frame rejected:\tSATURATED."

                        # display image and prompt user
                        else:
                            axim = pl.imshow(implot,
                                             vmin=m - 5 * s,
                                             vmax=m + 5 * s,
                                             origin='lower',
                                             cmap=pl.cm.gray)
                            pl.title(r"Median = {}, $\sigma$ = {:.1f}".format(
                                int(m), s))

                            # query user until valid response is provided
                            valid_entry = False
                            while not valid_entry:
                                direction = raw_input(
                                    "\nType Y for YES, N for NO, Q for QUIT: ")

                                if direction.lower() == 'y':
                                    fout = open(
                                        '{}_{}.list'.format(
                                            ftype, h['FILTER']), 'a'
                                    )  # append if this filter's list exists
                                    fout.write(
                                        fits_id +
                                        '\n')  # write new file name to list
                                    fout.close()
                                    valid_entry = True

                                elif direction.lower() == 'q':  # exit function
                                    print "Exiting..."
                                    os.chdir(
                                        start_dir
                                    )  # move back to starting directory
                                    pl.close(
                                        'all')  # close image to free memory
                                    return

                                elif direction.lower() != 'n':  # invalid case
                                    print "'{}' is not a valid entry.".format(
                                        direction)

                                else:  # 'N' selected, skip
                                    valid_entry = True

                    hdulist.close()  # close FITs file
                    pl.close('all')  # close image to free memory

                # close files
                fin.close()

        # H2RGs
        if cam_i in [2, 3]:

            fn_list = '{}_{}.list'.format(af.CAM_NAMES[cam_i], d)
            try:
                fin = open(fn_list, 'r')  # open list of FITs files
            except IOError:
                print "Warning: {} not found.  Skipping camera {}.".format(
                    fn_list, cam_i)
            else:
                isfoutopen = False
                # H2RGs do not have bias frames.  inform user
                if auto and ftype is af.BIAS_NAME:
                    print "\t* Warning: H2RG detectors do not have {} frames.  Skipping...".format(
                        af.BIAS_NAME)
                else:
                    fout = [
                        open(
                            '{}_{}.list'.format(ftype,
                                                af.H2RG_FILTERS[cam_i - 2]),
                            'w'),
                        open(
                            '{}_{}.list'.format(ftype, af.H2RG_FILTERS[cam_i]),
                            'w')
                    ]  # create list files for new img FITs files (e, w)
                    isfoutopen = True

                # look at FITs data sequentially
                for line in fin:

                    fits_fn = line.rstrip(
                    )  # current fits file name with return removed
                    fits_id = fits_fn.split('.')[
                        0]  # fits file name with extention removed
                    print '{}'.format(fits_fn)

                    # open data
                    hdulist = pf.open(fits_fn)
                    im = hdulist[0].data
                    h = hdulist[0].header

                    # rotate FITs data to align with filters (Z/J in E-left, Y/H in W-right)
                    im = np.rot90(im, af.CAM_ROTAT[cam_i])
                    if cam_i == 3:
                        im = np.flipud(im)  # flip C3 about y axis

                    # Let user determine if the image is good or not
                    implot = zoom(im,
                                  ZOOM_LVL)  # change image size for display
                    imleft = im[af.H2RG_SLICES[cam_i - 2]]
                    mleft = np.median(imleft)
                    sleft = af.robust_sigma(imleft)
                    imright = im[af.H2RG_SLICES[cam_i]]
                    mright = np.median(imright)
                    sright = af.robust_sigma(imright)
                    print '\t* Median of left side is {} counts'.format(mleft)
                    print '\t* Median of right side is {} counts'.format(
                        mright)

                    # select non-saturated flat frames with sufficient counts
                    if auto and ftype is af.FLAT_NAME:

                        # check whether median values are in specified range
                        sat_pt = af.CAM_SATUR[cam_i](h['SOFTGAIN'])
                        vmin = amin * sat_pt
                        vmax = amax * sat_pt

                        # left side
                        if mleft > vmin and mleft < vmax:
                            print "\t* Left side selected."
                            imfits_left = '{}_{}_{}.fits'.format(
                                fits_id, ftype, af.H2RG_FILTERS[cam_i - 2])
                            h['FILTER'] = af.H2RG_FILTERS[cam_i - 2]
                            pf.writeto(imfits_left,
                                       imleft,
                                       header=h,
                                       clobber=True)  # save left frame
                            fout[0].write('{}_{}_{}\n'.format(
                                fits_id, ftype, af.H2RG_FILTERS[
                                    cam_i - 2]))  # write new file name to list
                        else:
                            if mleft < vmin:
                                print "\t* Left side rejected:\tUNDEREXPOSED."
                            else:
                                print "\t* Left side rejected:\tSATURATED."

                        # right side
                        if mright > vmin and mright < vmax:
                            print "\t* Right side selected."
                            imfits_right = '{}_{}_{}.fits'.format(
                                fits_id, ftype, af.H2RG_FILTERS[cam_i])
                            h['FILTER'] = af.H2RG_FILTERS[cam_i]
                            pf.writeto(imfits_right,
                                       imright,
                                       header=h,
                                       clobber=True)  # save object frame
                            fout[1].write('{}_{}_{}\n'.format(
                                fits_id, ftype, af.H2RG_FILTERS[cam_i])
                                          )  # write new file name to list
                        else:
                            if mleft < vmin:
                                print "\t* Right side rejected:\tUNDEREXPOSED."
                            else:
                                print "\t* Right side rejected:\tSATURATED."

                    # display image and prompt user
                    else:
                        # display image and prompt user
                        pl.subplot(121)
                        pl.subplots_adjust(left=0.125,
                                           bottom=0.1,
                                           right=0.9,
                                           top=0.9,
                                           wspace=0.0,
                                           hspace=0.2)
                        axim = pl.imshow(imleft,
                                         vmin=mleft - 5 * sleft,
                                         vmax=mleft + 5 * sleft,
                                         origin='lower',
                                         cmap=pl.cm.gray)
                        axim.axes.set_xticks([axim.axes.get_xlim()[0]])
                        axim.axes.set_xticklabels(['E'])
                        axim.axes.set_yticks(axim.axes.get_ylim())
                        axim.axes.set_yticklabels(['S', 'N'])
                        pl.title(r"Median = {}, $\sigma$ = {:.1f}".format(
                            int(mleft), sleft))
                        pl.subplot(122)
                        axim = pl.imshow(imright,
                                         vmin=mright - 5 * sright,
                                         vmax=mright + 5 * sright,
                                         origin='lower',
                                         cmap=pl.cm.gray)
                        axim.axes.set_xticks([axim.axes.get_xlim()[1]])
                        axim.axes.set_xticklabels(['W'])
                        axim.axes.set_yticks([])
                        pl.title(r"Median = {}, $\sigma$ = {:.1f}".format(
                            int(mright), sright))

                        # query user until valid response is provided, Z & J face the EASTERN part of the field
                        valid_entry = False
                        while not valid_entry:
                            direction = raw_input(
                                "\nType Y for YES, N for NO, Q for QUIT: ")

                            if direction.lower() == 'y':

                                imfits_left = '{}_{}_{}.fits'.format(
                                    fits_id, ftype, af.H2RG_FILTERS[cam_i - 2])
                                h['FILTER'] = af.H2RG_FILTERS[cam_i - 2]
                                pf.writeto(imfits_left,
                                           imleft,
                                           header=h,
                                           clobber=True)  # save object frame
                                fout[0].write('{}_{}_{}\n'.format(
                                    fits_id, ftype, af.H2RG_FILTERS[
                                        cam_i -
                                        2]))  # write new file name to list

                                imfits_right = '{}_{}_{}.fits'.format(
                                    fits_id, ftype, af.H2RG_FILTERS[cam_i])
                                h['FILTER'] = af.H2RG_FILTERS[cam_i]
                                pf.writeto(imfits_right,
                                           imright,
                                           header=h,
                                           clobber=True)  # save object frame
                                fout[1].write('{}_{}_{}\n'.format(
                                    fits_id, ftype, af.H2RG_FILTERS[cam_i])
                                              )  # write new file name to list

                                valid_entry = True

                            elif direction.lower() == 'q':  # exit function
                                print "Exiting..."
                                os.chdir(start_dir
                                         )  # move back to starting directory
                                pl.close('all')  # close image to free memory
                                return

                            elif direction.lower() != 'n':  # invalid case
                                print "'{}' is not a valid entry.".format(
                                    direction)

                            else:  # 'N' selected, skip
                                valid_entry = True

                    hdulist.close()  # close FITs file
                    pl.close('all')  # close image to free memory

                # close files
                fin.close()
                if isfoutopen:
                    for f in fout:
                        f.close()

    # move back to starting directory
    os.chdir(start_dir)
Beispiel #55
0
def display(q):
    '''
    The worker thread sends audio data this display thread for real-time
    oscilliscope display.
    '''

    pylab.ion()

    fig = pylab.figure(figsize=(12, 6))

    ax1 = fig.add_subplot(211)
    ax1.grid(True)
    ax1.set_xlabel('Time')
    ax1.set_ylabel('Amplitude')

    ax2 = fig.add_subplot(212)
    ax2.grid(True)
    ax2.set_xlabel('Frequency (Hz)')
    ax2.set_ylabel('Amplitude')

    #--------------------------------------------------------------------------
    # Create oscilliscope data and plot axes

    xdata = np.arange(int(DISP_WIDTH * SR + 0.5)) / float(SR)

    n_samples = len(xdata)

    ydata = ns.Buffer.zeros(n_samples)

    ax1.axis([xdata[0], xdata[-1], -0.5, 0.5])

    line1, = ax1.plot(xdata, ydata, rasterized=True)

    # Create spectrogram data and plot axes

    spec_args = (SR, SPEC_WINDOW, 0.010, ns.NUTTALL)

    spec = ns.Spectrogram(ydata, *spec_args)

    spec_xaxis = np.array(spec.getFrequencyAxis())
    spec_yzeros = np.array(spec.computeMagnitude(ydata)[1:])

    line2, = ax2.plot(spec_xaxis, spec_yzeros, rasterized=True)

    ax2.set_xlim(20, SPEC_MAX_FREQ)
    ax2.set_xscale('log')

    ax2.set_ylim(-90, 60)

    fig.canvas.draw()

    # cache the backgrounds
    bg1 = fig.canvas.copy_from_bbox(ax1.bbox)
    bg2 = fig.canvas.copy_from_bbox(ax2.bbox)

    cbuf = ns.CircularBuffer(2 * n_samples)

    d0 = now()

    dcount = 0

    while True:

        try:
            d = q.get(False)
            have_d = True
        except queue.Empty:
            have_d = False

        if not have_d: continue

        if d == "QUIT":
            print "dcount = ", dcount
            return

        elif d == "RESET":
            data = np.zeros(n_samples)
            line1.set_ydata(data)
            line2.set_ydata(spec_yzeros)
            ax1.draw_artist(ax1.patch)
            ax1.draw_artist(line1)
            ax2.draw_artist(ax2.patch)
            ax2.draw_artist(line2)
            fig.canvas.draw()
            fig.canvas.flush_events()
            cbuf.write(ns.Buffer.zeros(2 * n_samples))
            continue

        cbuf.write(d)

        d1 = now()
        dt = d1 - d0

        if dt.total_seconds() >= (1.0 / 30):

            dcount += 1

            data = cbuf.read()

            #------------------------------------------------------------------
            # oscilliscope

            # Search the middle part of the data for the peak value

            n = len(data)

            i0 = n / 4
            i1 = i0 + n_samples / 2

            middle = data[i0:i1]

            imax = i0 + middle.argmax()

            # keep peak at 25 % into the window

            i0 = imax - n_samples / 4
            i1 = i0 + n_samples

            ydata = data[i0:i1]

            #------------------------------------------------------------------
            # spectrogram

            spec_ydata = spec.computeMagnitude(data).getdB()[1:]

            #------------------------------------------------------------------
            # Redraw plot

            line1.set_ydata(ydata)
            line2.set_ydata(spec_ydata)

            # restore background
            fig.canvas.restore_region(bg1)
            fig.canvas.restore_region(bg2)

            ax1.draw_artist(ax1.patch)
            ax1.draw_artist(line1)
            ax2.draw_artist(ax2.patch)
            ax2.draw_artist(line2)

            fig.canvas.blit(ax1.bbox)
            fig.canvas.blit(ax2.bbox)

            # cache the backgrounds
            bg1 = fig.canvas.copy_from_bbox(ax1.bbox)
            bg2 = fig.canvas.copy_from_bbox(ax2.bbox)

            d0 = now()
Beispiel #56
0
def display_video(dataset):
    """Display 3D video."""
    plt.ion()
    for i in range(dataset.shape[2]):
        plt.imshow(dataset[:, i])
        plt.pause(0.05)
Beispiel #57
0
    def plot(self, block=False, period=False):
        """
        Create a plot.
        """
        try:
            import matplotlib
            import matplotlib.pylab as mpl
        except ImportError:
            raise (ImportError("Could not import matplotlib.pylab."))

        fbest, T0 = self.best["f"], self.best["T0"]

        fig = mpl.figure()
        fig.canvas.set_window_title('GLS periodogram')
        fig.subplots_adjust(hspace=0.05,
                            wspace=0.04,
                            right=0.97,
                            bottom=0.09,
                            top=0.84)
        fs = 10  # fontsize

        # Periodogram
        plt = fig.add_subplot(3, 1, 1)
        plt.tick_params(direction='in')
        if period:
            plt.set_xscale("log")
            plt.set_xlabel("Period P")
        else:
            plt.set_xlabel("Frequency f")

        plt.set_ylabel(self.label["ylabel"])
        plt.plot(1 / self.f if period else self.f,
                 self.power,
                 'b-',
                 linewidth=.5)
        # mark the highest peak
        plt.plot(1 / fbest if period else fbest,
                 self.power[self.p.argmax()],
                 'r.',
                 label="1/f = %f" % (1 / fbest))
        plt.legend(numpoints=1, fontsize=fs, frameon=False)

        x2tics = 1 / np.array([0.5, 1, 2, 3, 5, 10, 20., 100])
        mx2tics = 1 / np.array([0.75, 1.5, 2.5, 4, 15, 40, 60., 80, 100])

        def tick_function(X):
            return ["%g" % (1 / z) for z in X]

        plt.tick_params(direction='in', which='both', top=True, right=True)
        plt.minorticks_on()
        plt.autoscale(enable=True, axis='x', tight=True)
        if not period:
            ax2 = plt.twiny()
            ax2.tick_params(direction='in', which='both')
            ax2.format_coord = lambda x, y: "x=%g, x2=%g, y=%g" % (x, 1 / x, y)
            ax2.set_xticks(x2tics)
            ax2.set_xticks(mx2tics, minor=True)
            ax2.set_xticklabels(tick_function(x2tics))
            ax2.set_xlim(plt.get_xlim())
            ax2.set_xlabel("Period")
            plt.tick_params(top=False)

        # Data and model
        col = mpl.cm.rainbow(mpl.Normalize()(self.t))

        def plot_ecol(plt, x, y):
            # script for scatter plot with errorbars and time color-coded
            datstyle = dict(color=col,
                            marker='.',
                            edgecolor='k',
                            linewidth=0.5,
                            zorder=2)
            if self.e_y is not None:
                errstyle = dict(yerr=self.e_y,
                                marker='',
                                ls='',
                                elinewidth=0.5)
                if matplotlib.__version__ < '2.':
                    errstyle['capsize'] = 0.
                    datstyle['s'] = 8**2  # requires square size !?
                else:
                    errstyle['ecolor'] = col
                _, _, (c, ) = plt.errorbar(x, y, **errstyle)
                if matplotlib.__version__ < '2.':
                    c.set_color(col)
            plt.scatter(x, y, **datstyle)

        def phase(t):
            #return (t-T0)*fbest % 1
            return (t - T0) % (1 / fbest)

        # Time series
        tt = arange(self.t.min(), self.t.max(), 0.01 / fbest)
        ymod = self.sinmod(tt)
        yfit = self.sinmod()
        plt1 = fig.add_subplot(3, 2, 3)
        plt1.set_ylabel("Data")
        mpl.setp(plt1.get_xticklabels(), visible=False)
        plot_ecol(plt1, self.t, self.y)
        plt1.plot(tt, ymod, 'k-', zorder=0)

        # Phase folded data
        tt = arange(T0, T0 + 1 / fbest, 0.01 / fbest)
        yy = self.sinmod(tt)
        plt2 = fig.add_subplot(3, 2, 4, sharey=plt1)
        mpl.setp(plt2.get_xticklabels(), visible=False)
        mpl.setp(plt2.get_yticklabels(), visible=False)
        plot_ecol(plt2, phase(self.t), self.y)
        xx = phase(tt)
        ii = np.argsort(xx)
        plt2.plot(xx[ii], yy[ii], 'k-')
        plt2.format_coord = lambda x, y: "x=%g, x2=%g, y=%g" % (x, x * fbest, y
                                                                )

        # Time serie of residuals
        yres = self.y - yfit
        plt3 = fig.add_subplot(3, 2, 5, sharex=plt1)
        plt3.set_xlabel("Time")
        plt3.set_ylabel("Residuals")
        plot_ecol(plt3, self.t, yres)
        plt3.plot([self.t.min(), self.t.max()], [0, 0], 'k-')

        # Phase folded residuals
        plt4 = fig.add_subplot(3, 2, 6, sharex=plt2, sharey=plt3)
        plt4.set_xlabel("Phase")
        mpl.setp(plt4.get_yticklabels(), visible=False)
        plot_ecol(plt4, phase(self.t), yres)
        plt4.plot([0, 1 / fbest], [0, 0], 'k-')
        plt4.format_coord = lambda x, y: "x=%g, x2=%g, y=%g" % (x, x * fbest, y
                                                                )

        for x in [plt1, plt2, plt3, plt4]:
            x.tick_params(direction='in', which='both', top=True, right=True)
            x.minorticks_on()
            x.autoscale(enable=True, tight=True)

        if hasattr(mpl.get_current_fig_manager(), 'toolbar'):
            # check seems not needed when "TkAgg" is set
            mpl.get_current_fig_manager().toolbar.pan()
        #t = fig.canvas.toolbar
        #mpl.ToggleTool(mpl.wx_ids['Pan'], False)
        if block:
            print("Close the plot to continue.")
        else:
            mpl.ion()

        fig.tight_layout()  # to get the left margin
        marleft = fig.subplotpars.left * fig.get_figwidth() * fig.dpi / fs

        def tighter():
            # keep margin tight when resizing
            xdpi = fs / (fig.get_figwidth() * fig.dpi)
            ydpi = fs / (fig.get_figheight() * fig.dpi)
            fig.subplots_adjust(bottom=4. * ydpi,
                                top=1 - 8. * ydpi,
                                right=1 - 1 * xdpi,
                                wspace=4 * xdpi,
                                hspace=4 * ydpi,
                                left=marleft * xdpi)
            if matplotlib.__version__ < '2.':
                ax2.set_position(plt.get_position().translated(0, 4 * ydpi))
            plt.set_position(plt.get_position().translated(0, 4 * ydpi))

        #fig.canvas.mpl_connect("resize_event", lambda _: (fig.tight_layout()))
        fig.canvas.mpl_connect("resize_event", lambda _: (tighter()))
        mpl.show()
        # mpl.show(block=block) # unexpected keyword argument 'block' in older matplotlib
        return mpl
Beispiel #58
0
def test_fps(use_blit=True):

    ax1.cla()
    ax1.set_title('Sensor Input vs. Time -' +
                  'Blit [{0:3s}]'.format("On" if use_blit else "Off"))
    ax1.set_xlabel('Time (s)')
    ax1.set_ylabel('Sensor Input (mV)')

    plt.ion(
    )  # Set interactive mode ON, so matplotlib will not be blocking the window
    plt.show(False)  # Set to false so that the code doesn't stop here

    cur_time = time.time()
    ax1.hold(True)

    x, y = [], []
    times = [time.time() - cur_time]  # Create blank array to hold time values
    y.append(0)

    line1, = ax1.plot(times,
                      y,
                      '.-',
                      alpha=0.8,
                      color="gray",
                      markerfacecolor="red")

    fig.show()
    fig.canvas.draw()

    if use_blit:
        background = fig.canvas.copy_from_bbox(
            ax1.bbox)  # cache the background

    tic = time.time()

    niter = 200
    i = 0
    while i < niter:

        fields = random.random() * 100

        times.append(time.time() - cur_time)
        y.append(fields)

        # this removes the tail of the data so you can run for long hours. You can cache this
        # and store it in a pickle variable in parallel.

        if len(times) > 50:
            del y[0]
            del times[0]

        xmin, xmax, ymin, ymax = [min(times) / 1.05, max(times) * 1.1, -5, 110]

        # feed the new data to the plot and set the axis limits again
        line1.set_xdata(times)
        line1.set_ydata(y)

        plt.axis([xmin, xmax, ymin, ymax])

        if use_blit:
            fig.canvas.restore_region(background)  # restore background
            ax1.draw_artist(line1)  # redraw just the points
            fig.canvas.blit(ax1.bbox)  # fill in the axes rectangle
        else:
            fig.canvas.draw()

        i += 1

    fps = niter / (time.time() - tic)
    print "Blit [{0:3s}] -- FPS: {1:.1f}, time resolution: {2:.4f}s".format(
        "On" if use_blit else "Off", fps, 1 / fps)
    return fps
Beispiel #59
0
    def process_data(self):
        """Create figure plotting distributions of scan-firing measures and
        compute various relevant statistics
        """
        from scanr.tools.stats import smooth_pdf, t_one_tailed
        from scanr.tools.string import snake2title
        from scipy.stats import (ttest_ind as ttest, t as t_dist, ks_2samp as kstest)

        os.chdir(self.datadir)
        self.out.outfd = file('figure.log', 'w')
        self.out.timestamp = False

        self.figure = {}
        self.figure['distros'] = f = plt.figure(figsize=(10, 16))
        f.suptitle('Distributions of LEC/MEC Scan/Non-scan Firing')

        LEC = self.results['LEC']
        MEC = self.results['MEC']

        data_types = LEC.dtype.names
        N = len(data_types)

        def mean_pm_sem(a):
            return '%.4f +/- %.4f'%(a.mean(), a.std()/np.sqrt(a.size))

        plt.ioff()
        kw = dict(lw=2, aa=True)
        for i, data in enumerate(data_types):
            ax = plt.subplot(N, 1, i+1)
            label = snake2title(data)

            kw.update(label='LEC', c='b')
            ax.plot(*smooth_pdf(LEC[data]), **kw)

            kw.update(label='MEC', c='g')
            ax.plot(*smooth_pdf(MEC[data]), **kw)

            ax.axis('tight')
            v = list(ax.axis())
            v[3] *= 1.1
            ax.axis(v)

            med_LEC = np.median(LEC[data])
            med_MEC = np.median(MEC[data])
            ax.plot([med_LEC]*2, [v[2], v[3]], 'b--')
            ax.plot([med_MEC]*2, [v[2], v[3]], 'g--')

            self.out(label.center(50, '-'))
            N_LEC = LEC[data].size
            N_MEC = MEC[data].size
            self.out('Median LEC(%s) = %.4f'%(label, med_LEC))
            self.out('Mean/SEM LEC(%s) = %s'%(label, mean_pm_sem(LEC[data])))
            if data.endswith('norm'):
                self.out('T(LEC > 1) = %.4f, p = %.8f'%t_one_tailed(LEC[data]))
                self.out('T_cstv(LEC > 1) = %.4f, p = %.8f'%t_one_tailed(
                    LEC[data], df=N_LEC/float(5)-1))
            self.out('N LEC cell-sessions = %d'%N_LEC)
            self.out('Median MEC(%s) = %.4f'%(label, med_MEC))
            self.out('Mean/SEM MEC(%s) = %s'%(label, mean_pm_sem(MEC[data])))
            if data.endswith('norm'):
                self.out('T(MEC > 1) = %.4f, p = %.8f'%t_one_tailed(MEC[data]))
                self.out('T_cstv(MEC > 1) = %.4f, p = %.8f'%t_one_tailed(
                    MEC[data], df=N_MEC/float(5)-1))
            self.out('N MEC cell-sessions = %d'%N_MEC)

            t = ttest(LEC[data], MEC[data])
            k = kstest(LEC[data], MEC[data])
            self.out('T-test(%s): t = %.4f, p = %.8f'%(label, t[0], t[1]))
            self.out('KS-test(%s): D = %.4f, p = %.8f'%(label, k[0], k[1]))
            if t[1] < 0.05:
                ax.text(0.025, 0.8, '*t', size='x-large', transform=ax.transAxes)
            if k[1] < 0.05:
                ax.text(0.025, 0.6, '*KS', size='x-large', transform=ax.transAxes)

            ax.set_ylabel('p[ %s ]'%label)

            if i == 0:
                ax.legend(loc=1)

        plt.ion()
        plt.show()
        self.out.outfd.close()
    def plot(self, fig_number=322):
        """plot the stored data in figure `fig_number`.

        Dependencies: `matlabplotlib.pylab`
        """
        from matplotlib import pylab
        from matplotlib.pylab import (
            gcf, gca, figure, plot, xlabel, grid, semilogy, text, draw, show, ion,
            subplot as _subplot, tight_layout, rcParamsDefault, xlim, ylim
            )
        def title_(*args, **kwargs):
            kwargs.setdefault('size', rcParamsDefault['axes.labelsize'])
            pylab.title(*args, **kwargs)
        def subtitle(*args, **kwargs):
            kwargs.setdefault('horizontalalignment', 'center')
            text(0.5 * (xlim()[1] - xlim()[0]), 0.9 * ylim()[1],
                 *args, **kwargs)
        def legend_(*args, **kwargs):
            kwargs.setdefault('framealpha', 0.3)
            kwargs.setdefault('fancybox', True)
            kwargs.setdefault('fontsize', rcParamsDefault['font.size'] - 2)
            pylab.legend(*args, **kwargs)
        def subplot(*args, **kwargs):
            with _warnings.catch_warnings():
                _warnings.simplefilter("ignore")
                _subplot(*args, **kwargs)  # catch unjustified deprecation warning because subplot and add_subplot are not separate
        if isinstance(fig_number, int):
            figure(fig_number)

        dat = self._data  # dictionary with entries as given in __init__
        if not dat or not dat['eval'] or len(dat['eval']) <= 2:
            return
        try:  # a hack to get the presumable population size lambda
            strpopsize = ' (evaluations / %s)' % str(dat['eval'][-2] -
                                                     dat['eval'][-3])
        except IndexError:
            strpopsize = ''

        # plot fit, Delta fit, sigma
        subplot(221)
        gca().clear()
        if dat['fit'][0] is None:  # plot is fine with None, but comput-
            dat['fit'][0] = dat['fit'][1]  # tations need numbers
            # should be reverted later, but let's be lazy
        assert dat['fit'].count(None) == 0
        fmin = min(dat['fit'])
        imin = dat['fit'].index(fmin)
        dat['fit'][imin] = max(dat['fit']) + 1
        fmin2 = min(dat['fit'])
        dat['fit'][imin] = fmin
        semilogy(dat['iter'], [f - fmin if f - fmin > 1e-19 else None
                               for f in dat['fit']],
                 'c', linewidth=1, label='f-min(f)')
        semilogy(dat['iter'], [max((fmin2 - fmin, 1e-19)) if f - fmin <= 1e-19 else None
                               for f in dat['fit']], 'C1*')

        semilogy(dat['iter'], [abs(f) for f in dat['fit']], 'b',
                 label='abs(f-value)')
        semilogy(dat['iter'], dat['sigma'], 'g', label='sigma')
        semilogy(dat['iter'][imin], abs(fmin), 'r*', label='abs(min(f))')
        if dat['more_data']:
            gca().twinx()
            plot(dat['iter'], dat['more_data'])
        grid(True)
        legend_(*[[v[i] for i in [1, 0, 2, 3]]  # just a reordering
                  for v in gca().get_legend_handles_labels()])

        # plot xmean
        subplot(222)
        gca().clear()
        plot(dat['iter'], dat['xmean'])
        for i in range(len(dat['xmean'][-1])):
            text(dat['iter'][0], dat['xmean'][0][i], str(i))
            text(dat['iter'][-1], dat['xmean'][-1][i], str(i))
        subtitle('mean solution')
        grid(True)

        # plot squareroot of eigenvalues
        if dat['D'][-1][0] != dat['D'][-1][-1]:
            subplot(223)
            gca().clear()
            semilogy(dat['iter'], dat['D'], 'm')
            xlabel('iterations' + strpopsize)
            title_('Axis lengths')
            grid(True)

        # plot stds
        subplot(224)
        # if len(gcf().axes) > 1:
        #     sca(pylab.gcf().axes[1])
        # else:
        #     twinx()
        gca().clear()
        semilogy(dat['iter'], dat['stds'])
        for i in range(len(dat['stds'][-1])):
            text(dat['iter'][-1], dat['stds'][-1][i], str(i))
        title_('Coordinate-wise STDs w/o sigma')
        grid(True)
        xlabel('iterations' + strpopsize)
        _stdout.flush()
        tight_layout()  # avoid superfluous padding
        # draw(), show()  # canvas.draw seem to do the job better
        ion()  # may prevent that everything stops until figure is closed?
        # todo: if in the same cell, the subplots are small until the cell is finished
        gcf().canvas.draw()
        CMAESDataLogger.plotted += 1