Esempio n. 1
0
	def feature_label(self, data):
		f = data[0]
		s = data[1]
		base_x = 0.88
		base_y = 0.74
		for i in range(0, len(data[0])):
			 pl.figtext(base_y, base_x-0.05*i, '{} -> {}'.format(f[i], s[i]))
Esempio n. 2
0
def main(args):
    if len(args) != 3:
        print "Usage: python plots.py <results file> <start time> <end time>"
        sys.exit()
    fh = open(args[0], "r")
    l = fh.readline().split()
    N = int(l[0])
    T = int(l[1])
    step = int(l[2])
    mean_c = float(l[3])
    ts = int(args[1]) / step
    td = int(args[2]) / step + step
    x = range(0, T + step, step)
    cl = []
    for line in fh.readlines():
        c = float(line)
        cl.append(c)
    fh.close()

    # Time evolution of the fraction of cooperators.
    pylab.figure(1, figsize = (7, 4.5), dpi = 500)
    pylab.xlabel(r"$t$")
    pylab.ylabel(r"$x$")
    pylab.plot(x[ts:td], cl[ts:td], "#000000", alpha = 0.6, linewidth = 2.0)
    pylab.figtext(0.82, 0.85, r"$x_\infty = %4.3f$" %(mean_c), 
                  ha = 'center', va = 'center', 
                  bbox = dict(facecolor = 'white', edgecolor = 'black'))
    pylab.xlim(int(args[1]), int(args[2]))
    pylab.ylim(0, 1)
    ax = pylab.gca()
    ax.xaxis.major.formatter.set_powerlimits((0,0))
    pylab.savefig("plot.pdf", format = "pdf")
    pylab.close(1)
Esempio n. 3
0
	def add_label(self, feature_result, min_feature_label):
		base_x = 0.88
		base_y = 0.74
		for i in range(0, len(feature_result)):
			x1 = feature_result[i]
			x2 = min_feature_label[i]
			pl.figtext(base_y, base_x-0.05*i, '{} -> [{}]'.format(x1, x2))
Esempio n. 4
0
def plotData():
    marr = fetchData()
    textsize = 18

    yticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], xt, size=textsize)
    ylabel("Periode [s]", size=textsize)
    xticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], yt, size=textsize)
    xlabel("Ausstattung [%]", size=textsize)
    title(
        "Abweichung der Geschwindigkeit zwischen FCD und des simulierten Verkehrs", size=textsize)
    # title("Relative Anzahl erfasster Kanten", size=textsize)
    figtext(0.7865, 0.92, '[%]', size=textsize)

    # levels=arange(mmin-mmin*.1, mmax+mmax*.1, (mmax-mmin)/10.))
    contourf(marr, 50)
    # set fontsize and ticks for the colorbar:
    if showVal == EDGENO:
        cb = colorbar(ticks=[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
    else:
        # cb = colorbar(ticks=range(17))
        cb = colorbar()
    for t in cb.ax.get_yticklabels():  # set colorbar fontsize of each tick
        t.set_fontsize(textsize)

    show()
Esempio n. 5
0
def myplot_setlabel(xlabel=None,ylabel=None,title=None, label=None, xy=(0,0), ax=None, labsize=15,rightticks=False):
    import matplotlib as mpl
    mpl.rcParams['font.size'] = labsize+0.
#    mpl.rcParams['font.family'] = 'serif'# New Roman'
    mpl.rcParams['font.serif'] = 'Bitstream Vera Serif'

    mpl.rcParams['axes.labelsize'] = labsize+1.
    print labsize+1
    mpl.rcParams['xtick.labelsize'] = labsize+0.
    mpl.rcParams['ytick.labelsize'] = labsize+0.

    if label:
        print "######################## LABELS HERE##########################"
        
        if xy==(0,0):
            xy=(0.3,0.90)
        if not ax:
            print "WARNING: no axix, cannot place label"
            pl.figtext(xy[0],xy[1],label, fontsize=labsize)
        else:
            pl.text(xy[0],xy[1],label, transform=ax.transAxes, fontsize=labsize)
            if rightticks:
                ax.yaxis.tick_right()
    pl.xlabel(xlabel, fontsize=labsize+1)
    pl.ylabel(ylabel, fontsize=labsize+1)
    if title:
        pl.title(title)
Esempio n. 6
0
    def display_settings(self):
        from pylab import figtext
        assert self.search

        search = self.search
        figtext(.01,.99,search.str_graph_fixed(), va='top',fontsize='medium')
        figtext(.35, .99, search.str_graph_used(), va='top',fontsize='medium')
    def buildAtmos(self, secz, xlim=[300, 1100], doPlot=False):
        """Generate the total atmospheric transmission profile at this airmass, using the coefficients C."""
        # Burke paper says atmosphere put together as 
        # Trans_total (alt/az/time) = Tgray * (e^-Z*tau_aerosol(alt/az/t)) * 
        #         * (1 - C_mol * BP(t)/BPo * A_mol(Z))  -- line 2
        #         * (1 - sqrt(C_mol * BP(t)/BPo) * A_mol(Z))  -- 3
        #         * (1 - C_O3 * A_O3(A) )
        #         * (1 - C_H2O(alt/az/time) * A_H2O(Z))
        # Tau_aerosol = trans['aerosol'] ... but replace with power law (because here all 1's)
        #  typical power law index is about tau ~ lambda^-1
        # A_mol = trans['O2']
                
        # secz = secz of this observation
        # wavelen / atmo_templates == building blocks of atmosphere, with seczlist / atmo_ind keys
        # C = coeffsdictionary = to, t1, t2, alpha0 (for aerosol), C_mol, BP, C_O3, C_H2O  values    
        if (abs(secz - self.secz) > interplimit):
            print "Generating interpolated atmospheric absorption profiles for this airmass %f" %(secz)
            self.interpolateSecz(secz)

        BP0 = 782 # mb
        # set aerosol appropriately with these coefficients
        self.atmo_abs['aerosol'] = 1.0 - numpy.exp(-secz * (self.C['t0'] + self.C['t1']*0.0 + self.C['t2']*0.0) 
                                                   * (self.wavelen/675.0)**self.C['alpha'])
        # set total transmission, with appropriate coefficients
        self.trans_total = numpy.ones(len(self.wavelen), dtype='float')
        self.trans_total = self.trans_total * (1.0 - self.C['mol'] * self.C['BP']/BP0 * self.atmo_abs['rayleigh'])  \
                      * ( 1 - numpy.sqrt(self.C['mol'] * self.C['BP']/BP0) * self.atmo_abs['O2']) \
                      * ( 1 - self.C['O3'] * self.atmo_abs['O3']) \
                      * ( 1 - self.C['H2O'] * self.atmo_abs['H2O']) \
                      * ( 1 - self.atmo_abs['aerosol'])
        # now we can plot the atmosphere
        if doPlot:
            pylab.figure()
            pylab.subplot(212)
            colorindex = 0
            for comp in self.atmo_ind:
                pylab.plot(self.wavelen, self.atmo_abs[comp], colors[colorindex], label='%s' %(comp))
                colorindex = self._next_color(colorindex)        
            leg =pylab.legend(loc=(0.88, 0.3), fancybox=True, numpoints=1, shadow=True)
            ltext = leg.get_texts()
            pylab.setp(ltext, fontsize='small')
            coefflabel = ""
            for comp in ('mol', 't0', 'alpha', 'O3', 'H2O'):
                coefflabel = coefflabel + "C[%s]:%.2f  " %(comp, self.C[comp])
                if (comp=='alpha') | (comp=='mol'):
                    coefflabel = coefflabel + "\n"
            pylab.figtext(0.2, 0.35, coefflabel, fontsize='small')
            pylab.xlim(xlim[0], xlim[1])
            pylab.ylim(0, 1.0)
            pylab.xlabel("Wavelength (nm)")
            pylab.subplot(211)
            pylab.plot(self.wavelen, self.atmo_trans[self.seczToString(1.2)]['comb'], 'r-', label='Standard X=1.2 (no aerosols)')
            pylab.plot(self.wavelen, self.trans_total, 'k-', label='Observed')
            leg = pylab.legend(loc=(0.12, 0.05), fancybox=True, numpoints=1, shadow=True)
            ltext = leg.get_texts()
            pylab.setp(ltext, fontsize='small')
            pylab.xlim(xlim[0], xlim[1])
            pylab.ylim(0, 1.0)
            pylab.title("Example Atmosphere at X=%.2f" %(secz))
        return
def do_panel(condition, refcondition, figtitle, cursor, band='r'):
    cmd = "select a.BT, f.flag, a.r_bulge,  a.n_bulge, a.ba_bulge, a.ba_disk, c.z from CAST as c, Flags_catalog as f, {band}_band_serexp as a, gz2_flags as z where a.galcount = c.galcount and a.galcount = f.galcount and a.galcount = z.galcount and f.band='{band}' and f.model='serexp' and f.ftype ='u' and {condition};"
#a.r_bulge,
    BT, flags, r_bulge, n_bulge,ba_bulge,ba_disk, zspec =cursor.get_data(cmd.format(condition=condition, band=band))
    ref_BT,ref_flag, ref_rb, ref_nb, ref_ba,ref_badisk, ref_zspec = cursor.get_data(cmd.format(condition=refcondition, band=band))
    BT = np.array(BT)
    flags = np.array(flags, dtype=int)
    r_bulge = np.array(r_bulge)
    n_bulge= np.array(n_bulge)
    ba_bulge= np.array(ba_bulge)
    ba_disk= np.array(ba_disk)
    zspec = np.array(zspec)

    ref_BT = np.array(ref_BT)
    ref_flag = np.array(ref_flag, dtype=int)
    ref_rb = np.array(ref_rb)
    ref_nb= np.array(ref_nb)
    ref_ba= np.array(ref_ba)
    ref_badisk= np.array(ref_badisk)
    ref_zspec = np.array(ref_zspec)


    panel_plot(BT, flags, r_bulge, n_bulge,ba_bulge,ba_disk, 
               ref_BT,ref_flag, ref_rb, ref_nb, ref_ba,ref_badisk, 
               zspec, ref_zspec)
    pl.figtext(0.5, 0.95, figtitle)
    print figtitle+' ',BT.size, ' objects' 
    pl.savefig('bar_params_serexp_%s.eps' %band)
    #pl.savefig('bar_z_serexp.eps')
    return
Esempio n. 9
0
 def showImage(self, xlim=None, ylim=None, clims=None, cmap=None, copy=False, stats=True):
     if copy:
         # useful if you're going to apply a hanning filter or something later, but
         # want to keep an imge of the original
         image = numpy.copy(self.image)
     else:
         image = self.image
     pylab.figure()
     pylab.title('Image')
     if xlim == None:
         x0 = 0
         x1 = self.nx
     else:
         x0 = xlim[0]
         x1 = xlim[1]
     if ylim == None:
         y0 = 0
         y1 = self.ny
     else:
         y0 = ylim[0]
         y1 = ylim[1]
     if clims == None:
         pylab.imshow(image, origin='lower', cmap=cmap)
     else:
         pylab.imshow(image, origin='lower', vmin=clims[0], vmax=clims[1], cmap=cmap)
     pylab.xlabel('X')
     pylab.ylabel('Y')
     cb = pylab.colorbar()
     clims = cb.get_clim()
     pylab.xlim(x0, x1)
     pylab.ylim(y0, y1)
     if stats:
         statstxt = 'Mean/Stdev/Min/Max:\n %.2f/%.2f/%.2f/%.2f' %(numpy.mean(image), numpy.std(image), image.min(), image.max())
         pylab.figtext(0.75, 0.03, statstxt)
     return clims
Esempio n. 10
0
    def test_varying_inclination(self):
        #""" Test that the waveform is consistent for changes in inclination
        #"""
        sigmas = []
        incs = numpy.arange(0, 21, 1.0) * lal.PI / 10.0

        for inc in incs:
            # WARNING: This does not properly handle the case of SpinTaylor*
            # where the spin orientation is not relative to the inclination
            hp, hc = get_waveform(self.p, inclination=inc)
            s = sigma(hp, low_frequency_cutoff=self.p.f_lower)        
            sigmas.append(s)
         
        f = pylab.figure()
        pylab.axes([.1, .2, 0.8, 0.70])   
        pylab.plot(incs, sigmas)
        pylab.title("Vary %s inclination, $\\tilde{h}$+" % self.p.approximant)
        pylab.xlabel("Inclination (radians)")
        pylab.ylabel("sigma (flat PSD)")
        
        info = self.version_txt
        pylab.figtext(0.05, 0.05, info)
        
        if self.save_plots:
            pname = self.plot_dir + "/%s-vary-inclination.png" % self.p.approximant
            pylab.savefig(pname)

        if self.show_plots:
            pylab.show()
        else:
            pylab.close(f)

        self.assertAlmostEqual(sigmas[-1], sigmas[0], places=7)
        self.assertAlmostEqual(max(sigmas), sigmas[0], places=7)
        self.assertTrue(sigmas[0] > sigmas[5])
Esempio n. 11
0
def regressionANN(mypathforResults,predicted,observed,regress,variable_to_fill, Site_ID,units,list_out,index_str):    
    for index, item in enumerate(list_out):
	fig=pl.figure(4, figsize=(16, 12), dpi=80, facecolor='w', edgecolor='k')
	ANN_label=str(item+"_NN") 
        graphtext1=str('slope      ' + str("{0:.2f}".format(regress[index][0])) +'\n' +
                       'intercept  ' + str("{0:.2f}".format(regress[index][1])) +'\n' +
                       'r-value    ' + str("{0:.2f}".format(regress[index][2])) +'\n' +
                       'p-value    ' + str("{0:.2f}".format(regress[index][3])) +'\n' +
                       'slope SE   ' + str("{0:.2f}".format(regress[index][4])) +'\n' +
                       'estim. SE  ' + str("{0:.2f}".format(regress[index][5]))         )  
        pl.figtext(0.7,0.6,graphtext1, bbox=dict())
        pl.plot(observed[:,index], predicted[:,index], 'o', label='targets vs. outputs')    
        slope = regress[index][0]; intercept = regress[index][1]
    
        x = np.linspace(min(observed[:,index]),max(observed[:,index]))
        y = slope * x + intercept
        pl.plot(x, y, linewidth = 2, label = 'regression line')
        pl.legend()
        pl.title('Tower vs ANN for '+item+' at ' +Site_ID+ ' index '+index_str)
        pl.xlabel('Tower ' + '('+units+')')
        pl.ylabel('ANN ' + '('+units+')')
        pl.legend(shadow=True, fancybox=True,loc='best')
        
        pl.savefig(mypathforResults+'/'+'Tower vs ANN for '+item+' at ' +Site_ID+ ' index '+index_str)
        #pl.show()
        pl.close(4)    
	time.sleep(2)
Esempio n. 12
0
    def OnCalcShift(self, event):
        if (len(self.PSFLocs) > 0):
            import pylab
            
            x,y,z = self.PSFLocs[0]
            
            z_ = numpy.arange(self.image.data.shape[2])*self.image.mdh['voxelsize.z']*1.e3
            z_ -= z_.mean()
            
            pylab.figure()
            p_0 = 1.0*self.image.data[x,y,:,0].squeeze()
            p_0  -= p_0.min()
            p_0 /= p_0.max()

            #print (p_0*z_).sum()/p_0.sum()
            
            p0b = numpy.maximum(p_0 - 0.5, 0)
            z0 = (p0b*z_).sum()/p0b.sum()
            
            p_1 = 1.0*self.image.data[x,y,:,1].squeeze()
            p_1 -= p_1.min()
            p_1 /= p_1.max()
            
            p1b = numpy.maximum(p_1 - 0.5, 0)
            z1 = (p1b*z_).sum()/p1b.sum()
            
            dz = z1 - z0

            print(('z0: %f, z1: %f, dz: %f' % (z0,z1,dz)))
            
            pylab.plot(z_, p_0)
            pylab.plot(z_, p_1)
            pylab.vlines(z0, 0, 1)
            pylab.vlines(z1, 0, 1)
            pylab.figtext(.7,.7, 'dz = %3.2f' % dz)
Esempio n. 13
0
 def gen_hrf(self):
     m_A, q_Z, mu_k, m_H, sigma_k, width, height, hrf0 = self.analy.Vbjde() 
     fgs = self.ConditionalNRLHist(m_A, q_Z)
     MMin = -1.0  # Y.min()
     MMax = 1.0  # Y.max()
     pas = (MMax - MMin) / 100
     xx = arange(MMin, MMax, pas)
     nf = 1
     g0 = self.gaussian(xx, mu_k[0][0], sigma_k[0][0])
     g1 = self.gaussian(xx, mu_k[0][1], sigma_k[0][1])
     print (g0, g1)
     fgs.insert(0, figure((self.nf + 1) * 123))
     title("Fonction de reponse", fontsize='xx-large')
     figtext(0.2, 0.04,
             'bande = ' + str(self.bande)+
             ' beta =' + str(self.beta) +
             ' sigma = ' + str(self.sigmaH) +
             ' pl = ' + str(self.pl) +
             ' dt = ' + str(self.dt) +
             ' thrf = ' + str(self.Thrf),
             #'mu_k = '+ str(self.mu_k) +
             #'sigma_k = '+ str(self.sigma_k),
            fontsize='x-large')
     plot(m_H)
     if self.shower == 1:
         show()
     return fgs
Esempio n. 14
0
    def dovis(self):
        """
        Do runtime visualization. 
        """

        pylab.clf()

        phi = self.cc_data.get_var("phi")

        myg = self.cc_data.grid

        pylab.imshow(numpy.transpose(phi[myg.ilo:myg.ihi+1,
                                         myg.jlo:myg.jhi+1]), 
                     interpolation="nearest", origin="lower",
                     extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax])

        pylab.xlabel("x")
        pylab.ylabel("y")
        pylab.title("phi")

        pylab.colorbar()
        
        pylab.figtext(0.05,0.0125, "t = %10.5f" % self.cc_data.t)

        pylab.draw()
Esempio n. 15
0
    def plot_Rnl(self,filename=None):
        """ Plot radial wave functions with matplotlib.
        
        filename:  output file name + extension (extension used in matplotlib)
        """
        if pl==None:
            raise AssertionError('pylab could not be imported')
        rmax = data[self.symbol]['R_cov']/0.529177*3
        ri = np.where( self.rgrid<rmax )[0][-1]
        states=len(self.list_states())
        p = np.ceil(np.sqrt(states)) #p**2>=states subplots
        
        fig=pl.figure()        
        i=1
        # as a function of grid points
        for n,l,nl in self.list_states():
            ax=pl.subplot(2*p,p,i)
            pl.plot(self.Rnlg[nl])
            pl.yticks([],[])
            pl.xticks(size=5)
            
            # annotate
            c = 'k'
            if nl in self.valence: 
                c='r'
            pl.text(0.5,0.4,r'$R_{%s}(r)$' %nl,transform=ax.transAxes,size=15,color=c)
            if ax.is_first_col():
                pl.ylabel(r'$R_{nl}(r)$',size=8)
            i+=1
            
        # as a function of radius
        i = p**2+1
        for n,l,nl in self.list_states():
            ax=pl.subplot(2*p,p,i)
            pl.plot(self.rgrid[:ri],self.Rnlg[nl][:ri])
            pl.yticks([],[])
            pl.xticks(size=5)
            if ax.is_last_row():
                pl.xlabel('r (Bohr)',size=8)

            c = 'k'
            if nl in self.valence: 
                c='r'
            pl.text(0.5,0.4,r'$R_{%s}(r)$' %nl,transform=ax.transAxes,size=15,color=c)
            if ax.is_first_col():
                pl.ylabel(r'$R_{nl}(r)$',size=8)
            i+=1
        

        file = '%s_KSAllElectron.pdf' %self.symbol
        #pl.rc('figure.subplot',wspace=0.0,hspace=0.0)
        fig.subplots_adjust(hspace=0.2,wspace=0.1)
        s=''
        if self.confinement!=None:
            s='(confined)'
        pl.figtext(0.4,0.95,r'$R_{nl}(r)$ for %s-%s %s' %(self.symbol,self.symbol,s))
        if filename is not None:
            file = filename
        pl.savefig(file)
Esempio n. 16
0
 def putText( self, text ):
   """
   Puts some text into an otherwise empty plot.
   @param text: text to put in the empty plot
   """
   newText = ''
   for i in range( int(len(text)/60.0)+1):
     newText+=text[60*i:60*i+60]+'\n'
   figtext(0.15,0.15, newText)
Esempio n. 17
0
    def test_varying_orbital_phase(self):
        #"""Check that the waveform is consistent under phase changes
        #"""
        
        if self.p.approximant in td_approximants():
            sample_attr = 'sample_times'
        else:
            sample_attr = 'sample_frequencies'   
            
        f = pylab.figure()
        pylab.axes([.1, .2, 0.8, 0.70])
        hp_ref, hc_ref = get_waveform(self.p, coa_phase=0)
        pylab.plot(getattr(hp_ref, sample_attr), hp_ref.real(), label="phiref")
       
        hp, hc = get_waveform(self.p, coa_phase=lal.PI/4)
        m, i = match(hp_ref, hp)
        self.assertAlmostEqual(1, m, places=2)
        o = overlap(hp_ref, hp)
        pylab.plot(getattr(hp, sample_attr), hp.real(), label="$phiref \pi/4$")
        
        hp, hc = get_waveform(self.p, coa_phase=lal.PI/2)
        m, i = match(hp_ref, hp)
        o = overlap(hp_ref, hp)
        self.assertAlmostEqual(1, m, places=7)
        self.assertAlmostEqual(-1, o, places=7)
        pylab.plot(getattr(hp, sample_attr), hp.real(), label="$phiref \pi/2$")
        
        hp, hc = get_waveform(self.p, coa_phase=lal.PI)
        m, i = match(hp_ref, hp)
        o = overlap(hp_ref, hp)
        self.assertAlmostEqual(1, m, places=7)
        self.assertAlmostEqual(1, o, places=7)
        pylab.plot(getattr(hp, sample_attr), hp.real(), label="$phiref \pi$")
        
        pylab.xlim(min(getattr(hp, sample_attr)), max(getattr(hp, sample_attr)))
        pylab.title("Vary %s oribital phiref, h+" % self.p.approximant)
        
        if self.p.approximant in td_approximants():
            pylab.xlabel("Time to coalescence (s)")
        else:
            pylab.xlabel("GW Frequency (Hz)") 

        pylab.ylabel("GW Strain (real part)")
        pylab.legend(loc="upper left")
        
        info = self.version_txt
        pylab.figtext(0.05, 0.05, info)
        
        if self.save_plots:
            pname = self.plot_dir + "/%s-vary-phase.png" % self.p.approximant
            pylab.savefig(pname)
        if self.show_plots:
            pylab.show()
        else:
            pylab.close(f)
Esempio n. 18
0
def get_GB_cls_metrics(data_fh,info):
    """
    Get the metrics of Gradient Boost classification models

    :param data_classi_fh: path to file containing Classification training data
    """
    from pylab import figtext
    try:
        dpkl=read_pkl(data_fh)
    except:
        return False
    if not 'gs_cv' in dpkl.keys():
        return False
    dXy=dpkl['dXy_final']
    ycol=dpkl['ycol']
    gs_cv=dpkl['gs_cv']
    feat_imp = dpkl['feat_imp']

    Xcols=[c for c in dXy.columns.tolist() if c!=ycol]
    est=gs_cv.best_estimator_
    X=dXy.loc[:,Xcols].as_matrix()
    y=dXy.loc[:,ycol].as_matrix()        

    #partial dep 
    plot_type='partial_dep'
    plot_fh='%s/data_ml/%s.%s.pdf' % (info.prj_dh,plot_type,basename(data_fh))
    if not exists(plot_fh):
        feats_indi=[s for s in dpkl['feat_imp'].head(6).index.tolist() if not ((') ' in s) and (' (' in s))]
        features=[Xcols.index(f) for f in feats_indi]
        feature_names=linebreaker(Xcols)
        from sklearn.ensemble.partial_dependence import plot_partial_dependence
        fig, axs = plot_partial_dependence(est, X, features,#[[features[1],features[2]]],
                                           feature_names=feature_names,
                                           n_jobs=int(info.cores), grid_resolution=50,
                                           n_cols=2,
                                           line_kw={'color':'r'},
                                          figsize=[7,9])
        figtext(0.9,-0.2,'AUC = %.2f' % gs_cv.best_score_,ha='right',color='b')
        saveplot(plot_fh,form='pdf',tight_layout=False)
    
    #relimp
    plot_type='featimps'
    plot_fh='%s/data_ml/%s.%s.pdf' % (info.prj_dh,plot_type,basename(data_fh))
    if not exists(plot_fh):
        featst=10
        fig=plt.figure(figsize=(3,featst*0.75))
        # fig = plt.figure(figsize=(8,featst*0.25))#figsize=(11,5))
        ax=plt.subplot(111)
        feat_imp=feat_imp.sort_values(by='Feature importance',ascending=True)
        feat_imp.index=linebreaker(feat_imp.index, break_pt=30)
        feat_imp.tail(featst).plot(kind='barh',ax=ax, color='red')
        ax.set_xlabel('Feature Importance')
        ax.legend([])    
        figtext(0.9,-0.2,'AUC = %.2f' % gs_cv.best_score_,ha='right',color='b')
        saveplot(plot_fh,form='pdf',tight_layout=False)
Esempio n. 19
0
def anneal_w_graphics(n=11, depth=10):
    """ Make an animation of the BDST chain walking on an nxn grid and play it
    """
    ni = 5
    nj = 100
    nk = 5

    beta = mc.Uninformative('beta', value=1.)

    G = nx.grid_graph([n, n])
    G.orig_pos = dict([[v, v] for v in G.nodes_iter()])
    G.pos = dict([[v, v] for v in G.nodes_iter()])

    root = (5,5)
    bdst = BDST(G, root, depth, beta)

    mod_mc = mc.MCMC([beta, bdst])
    mod_mc.use_step_method(STMetropolis, bdst)
    mod_mc.use_step_method(mc.NoStepper, beta)

    for i in range(ni):
        beta.value = i*5
        for j in range(nj):
            mod_mc.sample(1)
            T = bdst.value
            
            for k in range(nk):
                if random.random() < .95:
                    delta_pos = nx.spring_layout(T, pos=G.pos, fixed=[root], iterations=1)
                else:
                    delta_pos = G.orig_pos
                eps=.01
                my_avg = lambda x, y: (x[0]*(1.-eps) + y[0]*eps, x[1]*(1.-eps)+y[1]*eps)
                for v in G.pos:
                    G.pos[v] = my_avg(G.pos[v], delta_pos[v])
                views.plot_graph_and_tree(G, T, time=1.*k/nk)
                str = ''
                str += ' beta: %.1f\n' % beta.value
                str += ' cur depth: %d (target: %d)\n' % (T.depth, depth)
                sm = mod_mc.step_method_dict[bdst][0]
                str += ' accepted: %d of %d\n' % (sm.accepted, sm.accepted + sm.rejected)
                pl.figtext(0, 0, str)
                pl.figtext(1, 0, 'healthyalgorithms.wordpress.com \n', ha='right')
                pl.axis([-1, n, -1, n])
                pl.axis('off')
                pl.subplots_adjust(0, 0, 1, 1)
                pl.savefig('bdst%06d.png' % (i*nj*nk + j*nk + k))
            print 'accepted:', mod_mc.step_method_dict[bdst][0].accepted

    import subprocess
    subprocess.call('mencoder mf://bdst*.png -mf w=800:h=600 -ovc x264 -of avi -o bdst_G_%d_d_%d.avi' % (n, depth), shell=True)
    subprocess.call('mplayer -loop 0 bdst_G_%d_d_%d.avi' % (n, depth), shell=True)
    subprocess.call('rm bdst*.png')

    return bdst.value
Esempio n. 20
0
def showdp(fsz=16):
    """Utility function to set default parameters for DOS plots."""
    pl.xlabel("E-E$_\mathrm{f}$ (eV)", size=fsz)
    pl.figtext(0.03, 0.45, "LDOS", rotation="vertical", size=fsz)
    loc, lab = pl.xticks()
    lab.set_size = fsz
    loc, lab = pl.yticks()
    lab.set_size = fsz
    pl.legend()
    pl.subplots_adjust(hspace=0.0)
    pl.show()
Esempio n. 21
0
def plot_peak_offsets(offsets, filebase, doshow=False):
    """Plots the results of the peak offsets calculated."""
    from pylab import figure, clf, xlim, ylim, savefig, plot, text, show, figtext, subplot, title
    #@type inst Instrument
    inst = instrument.inst
    numperpage = 6

    # Initialize some stats
    rms = 0
    rms_wl = 0
    #@type po PeakOffset
    for po in offsets:
        #Square of the error distance
        error = (po.measured[0]- po.predicted[0])**2 + (po.measured[1]- po.predicted[1])**2
        rms += error
        error = (po.wavelength_measured - po.wavelength_predicted)**2
        rms_wl += error
    # Now do the root-mean
    rms = (rms/ len(offsets))**0.5
    rms_wl = (rms_wl/ len(offsets))**0.5
    print "Peak offsets RMS error is ", rms
    print "Peak offsets RMS wavelength error is ", rms_wl

    #@type det FlatDetector
    for (det_num, det) in enumerate(inst.detectors):
        if det_num % numperpage == 0:
            figure(det_num/numperpage, figsize=[8, 10])
            clf()
            figtext(0.5, 0.95, "Offset (black line) from predicted peak positions (red dot); wavelength in angstroms", horizontalalignment='center', verticalalignment='top')
        ax = subplot(3, 2, det_num % numperpage+1)
        #Set the axes font sizes
        for xlabel_i in ax.get_xticklabels() + ax.get_yticklabels() :
            xlabel_i.set_fontsize(10)
        #@type po PeakOffset
        for po in offsets:
            if po.det_num == det_num:
                x = [po.measured[0], po.predicted[0]]
                y = [po.measured[1], po.predicted[1]]
                plot(po.predicted[0], po.predicted[1], 'r.')
                plot(x, y, '-k')
                text(po.predicted[0], po.predicted[1], ' %.1f' % po.wavelength_measured, size=5, verticalalignment='center')
        xlim( -det.width/2, det.width/2)
        ylim( -det.height/2, det.height/2)
        #axis('equal')
        title('Detector %s' % det.name)
    #-- Save to files --
    for i in xrange((len(inst.detectors) + numperpage-1) / numperpage):
        figure(i)
        savefig( filebase + "_%d.pdf" % i, papertype="letter")
    #-- combine --
    os.system("pdftk %s_*.pdf cat output %s.pdf" % (filebase, filebase))

    if doshow:
        show()
Esempio n. 22
0
def gen_plots():
    """
    Create plots and save them in plot.pdf file, and print the mean fraction 
    of cooperators over the last 10 percent of the generations to STDOUT.
    """

    # Open and load the stuff from the results file.
    fh = open("results.pkl", "r")
    params = pickle.load(fh)
    population = pickle.load(fh)
    fh.close()

    # generations axis ticks.
    x = [i * params["report_freq"] \
             for i in range(0, params["generations"] / \
                                params["report_freq"] + 1)]
    
    y1 = []
    fixation_time_in_ticks = len(population[0].get_trait_list()) - 1
    fixation_time = fixation_time_in_ticks * params["report_freq"]
    fixated_to_c = False
    for t in range(0, params["generations"] / params["report_freq"] + 1):
        freq_c = 0
        if t <= fixation_time_in_ticks:
            for p in population:
                if p.get_trait_list()[t] == Player.C:
                    freq_c += 1
            if freq_c == params["population"]:
                fixated_to_c = True
        else:
            if fixated_to_c:
                freq_c = params["population"]
        freq_c = 1.0 * freq_c / params["population"]
        y1.append(freq_c)
    last = int(params["generations"] * 0.1)
    i = (params["generations"] / params["report_freq"]) - \
        (last / params["report_freq"])
    l = y1[i:]
    avg_c = sum(l) / len(l) 

    # Plot 1 (generations versus frequency of C players).
    pylab.figure(1, figsize = (7, 4.5), dpi = 500)
    pylab.xlabel(r"$t$")
    pylab.ylabel(r"$x$")
    pylab.plot(x, y1, "#000000", alpha = 0.6, linewidth = 2.0)
    pylab.figtext(0.82, 0.85, r"$x_\infty = %4.3f$" %(avg_c), 
                  ha = 'center', va = 'center', 
                  bbox = dict(facecolor = 'white', edgecolor = 'black'))
    pylab.ylim(0.0, 1.0)
    ax = pylab.gca()
    ax.xaxis.major.formatter.set_powerlimits((0,0))
    pylab.savefig("plot.pdf", format = "pdf")
    pylab.close(1)
Esempio n. 23
0
    def display_fail(self, message):
        #? If there is a way to stretch this an inch as well, it would
        # help with text overlap.  not sure if it is possible without
        # making a subplot, and corresponding strange boxes
        from pylab import savefig, close, axes, figtext

        axes([0,0,.01,.01])
        figtext(.5, .5, message, ha='center',va='center',size='x-large')
        self.display_settings()
        savefig(self.image_dst + ".png")
        close()
        return False
Esempio n. 24
0
    def savefig(filename, *args, **kwargs):
        if ScriptFlags.MREORG_SAVEFIGADDINFO:
            F = pylab.gcf()
            x,y = F.get_size_inches()
            txt = 'Size: x=%2.2f y=%2.2f (inches)' % (x,y) 
            txt += '\n' + filename.split('/')[-1]
            pylab.figtext(0.0, 0.5, txt, backgroundcolor='white')


        if ScriptFlags.MREORG_AUTOMAKEDIRS:
            mreorg.ensure_directory_exists(filename)
        return orig_mplsavefig(filename, *args, **kwargs)
Esempio n. 25
0
def psfSizeCCD(ccd=None,filter='r',seeing=0,theta=0., zenith = 0., x=0., y=0.,z=0.):
    res = genImgV(Nstar=1, ccd = ccd,seeing=seeing,theta=theta,zenith=zenith,x=x,y=y,z=z)
    imgV = res[0]
    size = np.sqrt(len(imgV[0][2:]))
    img = imgV[0][2:].reshape(size,size)        
    xcen = res[1][0]['xcen']
    ycen = res[1][0]['ycen']
    height, x, y, width_x, width_y = fitgaussian(img)
    disImgCCD(imgV,ccd)
    pl.figtext(0.2,0.85,'CCD: '+ccd[0], color='r')
    pl.figtext(0.2,0.8,'Filter: '+filter, color='r')
    return width_x, width_y
Esempio n. 26
0
    def dovis(self):
        """ 
        Do runtime visualization
        """
        pylab.clf()

        pylab.rc("font", size=10)

        u = self.cc_data.get_var("x-velocity")
        v = self.cc_data.get_var("y-velocity")

        myg = self.cc_data.grid

        vort = myg.scratch_array()
        divU = myg.scratch_array()

        vort[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
             0.5*(v[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
                  v[myg.ilo-1:myg.ihi,myg.jlo:myg.jhi+1])/myg.dx - \
             0.5*(u[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
                  u[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi])/myg.dy

        divU[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
            0.5*(u[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
                 u[myg.ilo-1:myg.ihi,myg.jlo:myg.jhi+1])/myg.dx + \
            0.5*(v[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
                 v[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi])/myg.dy

        fig, axes = pylab.subplots(nrows=2, ncols=2, num=1)
        pylab.subplots_adjust(hspace=0.25)

        fields = [u, v, vort, divU]
        field_names = ["u", "v", r"$\nabla \times U$", r"$\nabla \cdot U$"]
    
        for n in range(4):
            ax = axes.flat[n]
    
            f = fields[n]
            img = ax.imshow(np.transpose(f[myg.ilo:myg.ihi+1,
                                           myg.jlo:myg.jhi+1]), 
                            interpolation="nearest", origin="lower",
                            extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax])

            ax.set_xlabel("x")
            ax.set_ylabel("y")
            ax.set_title(field_names[n])

            pylab.colorbar(img, ax=ax)


        pylab.figtext(0.05,0.0125, "t = %10.5f" % self.cc_data.t)

        pylab.draw()
Esempio n. 27
0
def plot(cluster, filename="plot.png", func=lambda a: a.id, 
         plot_title='', cmap='Paired', filter=lambda a: True, 
         draw_legend=False, radius='2.25', sym=None):
    ac = rc.load(cluster)
    clusters = rc.load_clusters(cluster)

    p = get_population()
    pops = [0 for i in range(30000)]
    cluster_pops = [] 
    cluster_pop_max = []
    for clust in range(len(clusters)):
        cluster_pops.append(pops[:])
        cluster_pop_max.append([0,0,-1,0])

    for clust,agents in enumerate(clusters):
        for agent in agents:
            a = Agent(agent)
            for i in range(a.birth, a.death):
                cluster_pops[clust][i] += 1
                if cluster_pop_max[clust][2] == -1:
                    cluster_pop_max[clust][2] = i
                if i > cluster_pop_max[clust][3]:
                    cluster_pop_max[clust][3] = i
                if cluster_pops[clust][i] > cluster_pop_max[clust][0]:
                    cluster_pop_max[clust][0] = cluster_pops[clust][i]
                    cluster_pop_max[clust][1] = i
    
    lines=[]
    for i,clust in enumerate(cluster_pops):
        lines.append(pylab.plot(range(30000),clust, 
                         label=("%d: k=%d" % (i, len(clusters[i]))),
                         color=pylab.cm.Paired(float(i)/len(clusters))))

    if draw_legend:
        pylab.figlegend(lines, ["%d: k=%d" % (i, len(clust))
                                    for i,clust in enumerate(clusters)], 
                        'center right', 
                        ncol=((len(clusters)/35)+1), prop=dict(size=6))
    else:
        print "should not draw!!!"

    title = r"Cluster Population ($\epsilon$ = %s, %d clusters)" % (radius, len(clusters))
    pylab.title(title, weight='black')
    pylab.xlabel("Time", weight='bold')
    pylab.ylabel("Population Size", weight='bold')
    if sym is not None:
        pylab.figtext(0,.954, '(%s)' % sym, size=6, weight='black')

    pylab.savefig(filename, dpi=300)

    print 'cluster, totalPop, start, peak, stop, maxPop'
    for clust,agents in enumerate(clusters):
        print clust, len(agents), cluster_pop_max[clust][2], cluster_pop_max[clust][1], cluster_pop_max[clust][3]+1, cluster_pop_max[clust][0]
Esempio n. 28
0
File: plot.py Progetto: Aerobota/rce
def main(fileNames, ylog, remote, plotStyle):
    if ylog:
        pylab.subplot(111, xscale='log', yscale='log')
    else:
        pylab.subplot(111, xscale='log')

    for fileName in fileNames:
        with open(fileName, 'r') as f:
            size = numpy.array(json.loads(f.readline())[1:])
            benchmark = map(json.loads, f.readlines())

        for test in benchmark:
            if not remote and test['label'].endswith('R-to-C'):
                continue

            rawData = test.pop('data')
            data = [d for d in rawData if -1.0 not in d]
            length = len(data)

            if length != len(rawData):
                print('Invalid measurement encountered in '
                      '{0}.'.format(test['label']))

            data = numpy.array(data)

            if length > 1:
                mean = numpy.mean(data, 0)[1:]
                var = numpy.var(data, 0)[1:]
                minVal = numpy.min(data, 0)[1:]
                maxVal = numpy.max(data, 0)[1:]
            elif length == 1:
                mean = numpy.array(data[0][1:])
                var = None
            else:
                print('No valid measurements for {0}.'.format(test['label']))
                continue

            if plotStyle == 'minmax':
                pylab.errorbar(size, mean, numpy.array([minVal, maxVal]),
                               **test)
            elif plotStyle == 'variance':
                pylab.errorbar(size, mean, var, **test)
            else:
                pylab.errorbar(size, mean, **test)

    if 'paper.data' in fileNames:
        pylab.figtext(0.8, 0.8, 'green: paper measurements', ha='right')

    pylab.xlabel('number of characters')
    pylab.ylabel('time [ms]')
    pylab.legend(loc='upper left')
    pylab.show()
Esempio n. 29
0
def plot_F_and_pi(F, pi, causes, title=""):
    N, T, J = F.shape
    pl.figure(figsize=(0.5 * T, 1.0 * J))

    left = 2.0 / (T + 5.0)
    right = 1 - 0.05 / T
    bottom = 2.0 / (T + 5.0)
    top = 1 - 0.05 / T

    xmax = F.max()

    dj = (top - bottom) / J
    dt = (right - left) / T

    ax = {}
    for jj, j in enumerate(sorted(range(J), key=lambda j: pi[:, :, j].mean())):
        for t in range(T):
            pl.axes([left + t * dt, bottom + jj * dj, dt, dj])
            pl.plot(pl.randn(N), F[:, t, j], "b.", alpha=0.5, zorder=-100)
            pl.plot(0.5 * pl.randn(N), pi[:N, t, j], "g.", alpha=0.5, zorder=100)

            # pi[:,t,j].sort()
            # below = pi[:, t, j].mean() - pi[:,t,j][.025*N]
            # above = pi[:,t,j][.975*N] - pi[:, t, j].mean()
            # pl.errorbar([0], pi[:, t, j].mean(), [[below], [above]],
            #            fmt='gs', ms=10, mew=1, mec='white', linewidth=3, capsize=10,
            #            zorder=100)
            pl.text(
                -2.75,
                xmax * 0.9,
                "%.0f\n%.0f\n%.0f"
                % (
                    100 * F[:, t, j].mean(),
                    100 * pi[:, t, j].mean(),
                    100 * pi[:, t, j].mean() - 100 * F[:, t, j].mean(),
                ),
                va="top",
                ha="left",
            )
            pl.xticks([])
            pl.yticks([0.25, 0.5, 0.75])
            if jj == 0:
                pl.xlabel("%d\n%.0f" % (t + 1980, 100 * F[:, t, :].sum() / N))

            if t > 0:
                pl.yticks([])
            else:
                pl.ylabel(causes[j])

            pl.axis([-3, 3, 0, xmax])
    if title:
        pl.figtext(0.01, 0.99, title, va="top", ha="left")
def do3DSimulation():

	fig = plt.figure()
	ax = fig.gca(projection='3d')

	mCoeff1List = np.linspace(0,1,12)
	mCoeff2List = np.linspace(0,1,12)
        #mCoeffList = [1]       
        err1List = []
        err2List = []
        #print mCoeffList

	X, Y = np.meshgrid(mCoeff1List, mCoeff2List)
	z1s = np.array([calculateError3Comps(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])
	z2s = np.array([calculateRMSError3Comps(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])
	#R = np.sqrt(X**2 + Y**2)
	#Z = np.sin(R)
	Z1 = z1s.reshape(X.shape)
	Z2 = z2s.reshape(X.shape)
	#surf = ax.plot_surface(X, Y, Z1)
	#surf = ax.plot_surface(X, Y, Z2)
	#surf = ax.plot_surface(X, Y, Z1, rstride=1, cstride=1, cmap=cm.afmhot,linewidth=0, antialiased=False)
	surf = ax.plot_surface(X, Y, Z1, rstride=1, cstride=1, cmap=cm.afmhot)
	ax.set_zlim(0, 1.8)
	ax.view_init(elev=25,azim=-25)

	ax.zaxis.set_major_locator(LinearLocator(10))
	ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
	ax.set_xticks([0,0.2,0.8,1.0])
	ax.set_yticks([0,0.2,0.8,1.0])
	ax.set_zticks([0,0.5,1.0,1.8])
	#ax.set_xticks([0,0.2,0.4,0.6,0.8,1.0])
	#fig.colorbar(surf, shrink=0.5, aspect=5)

	for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(15) 
	for tick in ax.yaxis.get_major_ticks():
                tick.label.set_fontsize(15)
	for tick in ax.zaxis.get_major_ticks():
                tick.label.set_fontsize(15)
	ax.grid(color='black',linestyle='-', linewidth=1)

	ax.set_xlabel("$m_{f_{1}f_{2}}$",fontsize=28)
	ax.set_ylabel("$m_{f_{1}f_{3}}$",fontsize=28)
	ax.set_zlabel("Overall error",fontsize=24)
	plt.figtext(0.25, 0.8, "$|E_{f_{1}}| = |E_{f_{2}}| = |E_{f_{3}}| = 1.0$",fontsize=24)
	#plt.title("Variation of overall error with MIC between 3 error components",fontsize=24)
	plt.savefig("simu3d.eps",format="eps",dpi=800,bbox_inches="tight")	
        	
        plt.show()	
Esempio n. 31
0
color = '#114568'
xsize = 800
figpat = '../doc/source/images/sonat-{xsize}-{bgcolor}{shadow}.svg'

aspect = 1 / 4.5

xisize = 1. * xsize / dpi
yisize = xisize * aspect

sonat_size = 145. * xisize / 8.
o_size = 50 * xisize / 8.

figfiles = []
P.figure(figsize=(xisize, yisize), dpi=dpi)
kw = dict(ha='center', va='center', color=color, weight='bold')
ts = P.figtext(.5, .41, 'SONAT', size=sonat_size, **kw)
to = P.figtext(.302, .49, 'O', size=o_size, **kw)
shadow = ''

bgcolor = 'transp'
figfiles.append(figpat.format(**locals()))
checkdir(figfiles[-1], asfile=True)
P.savefig(figfiles[-1], transparent=True)

bgcolor = 'white'
figfiles.append(figpat.format(**locals()))
P.savefig(figfiles[-1])

bgcolor = 'blue'
figfiles.append(figpat.format(**locals()))
P.savefig(figfiles[-1], facecolor="#2882B9")
Esempio n. 32
0
print("Shape")
print(data_new.shape)

pylab.figure()

pylab.title('PC2 VS PC1')

pylab.xlabel('PC1')
pylab.ylabel('PC2')

graph_dir = '~/deep_learning_microbiome/analysis/kmers_rel_abundance'

pylab.scatter(data_new[:, 0],data_new[:, 1])
pylab.gca().set_position((.1, .4, .8, .6))
pylab.figtext(0.02, .24, 'This graph shows the relative abundance data plotted with 2-component PCA')
pylab.savefig(os.path.expanduser(graph_dir + '/pca_two_components' + '.pdf'), bbox_inches='tight')



pylab.figure()
pylab.title('Explained Variance vs Number of Components')
pylab.xlabel('Components')
pylab.ylabel('Total Variance')

nComponents = 10
fileInfo = '_totalnumcomponents-{}'.format(nComponents)

total_variance = 0
for i in range(1, nComponents):
    pca = PCA(n_components=i)
Esempio n. 33
0
def buildADS1115Graph(password, myGraphSampleCount, graphNumber):
    		print('buildADS1115Graph%d - The time is: %s' % (graphNumber, datetime.now()))

		# open database
		con1 = mdb.connect('localhost', 'root', password, 'DataLogger' )
		# now we have to get the data, stuff it in the graph 

    		mycursor = con1.cursor()

		print myGraphSampleCount
		query = '(SELECT timestamp, deviceid, channel0_voltage, channel0_raw, channel1_voltage, channel1_raw, channel2_voltage, channel2_raw, channel3_voltage, channel3_raw, id FROM '+ADS1115tableName+' ORDER BY id DESC LIMIT '+ str(myGraphSampleCount) + ') ORDER BY id ASC' 

		print "query=", query
		try:
			mycursor.execute(query)
			result = mycursor.fetchall()
		except:
			e=sys.exc_info()[0]
			print "Error: %s" % e


		print result[0]
		t = []   # time
		u = []   # channel 1 - Current 
		averageCurrent = 0.0
 		currentCount = 0
		for record in result:
			t.append(record[0])

			# adjust according to graphNumber
			if (graphNumber == 0):
				addValue = record[graphNumber*2+3] 

			if (graphNumber == 1):
        			# O2 Sensor
        			sensorVoltage = record[graphNumber*2+2]*(5.0/6.144)
        			AMP  = 121
        			K_O2  = 7.43
        			sensorVoltage = sensorVoltage/AMP*10000.0
        			Value_O2 = sensorVoltage/K_O2
				addValue =  Value_O2 - 1.05

			if (graphNumber == 2):
				addValue = record[graphNumber*2+2] 

			if (graphNumber == 3):
				addValue = record[graphNumber*2+2] 


			u.append(addValue)
		
			averageCurrent = averageCurrent+addValue
			currentCount=currentCount+1

		averageCurrent = averageCurrent/currentCount
		
		print ("count of t=",len(t))
                #x1 = [datetime.strptime(d, '%Y-%m-%d %H:%M:%S',) for d in t]
                x1 = [d for d in t]

                fds = dates.date2num(x1) # converted
		# matplotlib date format object
		hfmt = dates.DateFormatter('%H:%M:%S')
		#hfmt = dates.DateFormatter('%m/%d-%H')

		fig = pyplot.figure()
		fig.set_facecolor('white')
		ax = fig.add_subplot(111,facecolor = 'white')
		ax.vlines(fds, -200.0, 1000.0,colors='w')



		#ax.xaxis.set_major_locator(dates.MinuteLocator(interval=1))
		ax.xaxis.set_major_formatter(hfmt)
		if (graphNumber == 0):
			ax.set_ylim(bottom = 0.0)
			pyplot.xticks(rotation='45')
			pyplot.subplots_adjust(bottom=.3)
			pylab.plot(fds, u, color='r',label="Air Quality Sensor",linestyle="-",marker=".")

		if (graphNumber == 1):
			ax.set_ylim(bottom = 0.0)
			pyplot.xticks(rotation='45')
			pyplot.subplots_adjust(bottom=.3)
			pylab.plot(fds, u, color='r',label="Oxygen (O2) Sensor ",linestyle="-",marker=".")

		if (graphNumber == 2):
			ax.set_ylim(bottom = 0.0)
			pyplot.xticks(rotation='45')
			pyplot.subplots_adjust(bottom=.3)
			pylab.plot(fds, u, color='r',label="Light Sensor",linestyle="-",marker=".")

		if (graphNumber == 3):
			ax.set_ylim(bottom = -200.0)
			pyplot.xticks(rotation='45')
			pyplot.subplots_adjust(bottom=.3)
			pylab.plot(fds, u, color='r',label="Voltage Divider ",linestyle="-",marker=".")

		pylab.xlabel("Seconds")

		pylab.legend(loc='lower center')
		if (graphNumber == 0):
			pylab.axis([min(fds), max(fds), 0, max(u)+1000])
			pylab.ylabel("Raw Data")

		if (graphNumber == 1):
			pylab.axis([min(fds), max(fds), 0, max(u)+2])
			pylab.ylabel("Percent (%)")

		if (graphNumber == 2):
			pylab.axis([min(fds), max(fds), 0, max(u)+2])
			pylab.ylabel("Voltage (V)")

		if (graphNumber == 3):
			pylab.axis([min(fds), max(fds), 0, max(u)+2])
			pylab.ylabel("Voltage Divider (V)")


		if (graphNumber == 0):
			pylab.figtext(.5, .05, ("Average Air Quality %6.2f\n%s") %(averageCurrent, datetime.now()),fontsize=18,ha='center')

		if (graphNumber == 1):
			pylab.figtext(.5, .05, ("Average O2 %6.2f %%\n%s") %(averageCurrent, datetime.now()),fontsize=18,ha='center')

		if (graphNumber == 2):
			pylab.figtext(.5, .05, ("Average Light Sensor %6.2f V\n%s") %(averageCurrent, datetime.now()),fontsize=18,ha='center')

		if (graphNumber == 3):
			pylab.figtext(.5, .05, ("Average Voltage Divider %6.2f V\n%s") %(averageCurrent, datetime.now()),fontsize=18,ha='center')

		pylab.grid(True)

		pyplot.show()
		pyplot.savefig("/var/www/html/ADS1115DataLoggerGraph"+str(graphNumber)+".png", facecolor=fig.get_facecolor())	



		mycursor.close()       	 
		con1.close()

		fig.clf()
		pyplot.close()
		pylab.close()
		gc.collect()
		print "------ADS1115Graph"+str(graphNumber)+" finished now"
            ecart.append(abs(predic_score - score_spkshow[spk_test]))
        val.append(round(np.mean(ecart), 3))
        if round(np.mean(ecart), 3) < lower_ecart[0]:
            lower_ecart = [round(np.mean(ecart), 3), round(predic_score, 2)]
    print 'spkshow lower ecart', lower_ecart
    fig1 = plt.figure()
    fig1.suptitle('ecart/prediction baseline spkshow',
                  fontsize=14,
                  fontweight='bold')
    plt.ylabel('ecart')
    plt.xlabel('prediction')

    plt.plot(np.arange(0, 1.01, 0.05), val)
    plt.ylim([0, 1])
    plt.figtext(
        .01, .01, "plus petit ecart : " + str(lower_ecart[0]) +
        " pour prediction=" + str(lower_ecart[1]))
    fig1.savefig('spkshow_base_line')
    plt.show()

    score_spkseg = {}
    lower_ecart = [1.0, 0.0]
    for line in open(l_spkseg_file):
        l = line[:-1].split(' ')
        score_spkseg.setdefault(l[0] + '#' + l[3], {})
        score_spkseg[l[0] + '#' + l[3]][l[1] + ' ' + l[2]] = []

    for line in open(score_file_spkseg['f']):
        l = line[:-1].split(' ')
        spk = l[0] + '#' + l[3]
        seg = str(float(l[1])) + ' ' + str(float(l[2]))
def TemperatureHumidityGraph(source, days, delay):

    print("TemperatureHumidityGraph source:%s days:%s" % (source, days))
    print("sleeping seconds:", delay)
    time.sleep(delay)
    print("TemperatureHumidityGraph running now")

    # blink GPIO LED when it's run
    GPIO.setup(18, GPIO.OUT)
    GPIO.output(18, True)
    time.sleep(0.2)
    GPIO.output(18, False)

    # now we have get the data, stuff it in the graph

    try:
        print("trying database")
        db = mdb.connect('localhost', 'root', DATABASEPASSWORD, 'WeatherPi')

        cursor = db.cursor()

        query = "SELECT TimeStamp, bmp180Temperature,  outsideTemperature, outsideHumidity, insideHumidity FROM WeatherData where  now() - interval %i hour < TimeStamp" % (
            days * 24)

        print "query=", query
        cursor.execute(query)
        result = cursor.fetchall()

        t = []
        u = []
        v = []
        x = []
        z = []

        fig = pyplot.figure()

        for record in result:
            t.append(record[0])
            u.append(record[1])
            v.append(record[2])
            x.append(record[3])
            z.append(record[4])

        print("count of t=", len(t))
        if (len(t) == 0):
            return

        #dts = map(datetime.datetime.fromtimestamp, s)
        #fds = dates.date2num(dts) # converted
        # matplotlib date format object
        hfmt = dates.DateFormatter('%m/%d-%H')

        ax = fig.add_subplot(111)
        ax.xaxis.set_major_locator(dates.HourLocator(interval=6))
        ax.xaxis.set_major_formatter(hfmt)
        pylab.xticks(rotation='vertical')

        pyplot.subplots_adjust(bottom=.3)
        pylab.plot(t,
                   v,
                   color='g',
                   label="Outside Temp (C)",
                   linestyle="-",
                   marker=".")
        pylab.plot(t,
                   u,
                   color='r',
                   label="Inside Temp (C)",
                   linestyle="-",
                   marker=".")
        pylab.xlabel("Hours")
        pylab.ylabel("degrees C")
        pylab.legend(loc='upper left')
        pylab.axis([min(t), max(t), 0, 40])
        ax2 = pylab.twinx()
        pylab.ylabel("% ")
        pylab.plot(t,
                   x,
                   color='y',
                   label="Outside Hum %",
                   linestyle="-",
                   marker=".")
        pylab.plot(t,
                   z,
                   color='b',
                   label="Inside Hum %",
                   linestyle="-",
                   marker=".")
        pylab.axis([min(t), max(t), 0, 100])
        pylab.legend(loc='lower left')
        pylab.figtext(.5,
                      .05, ("Environmental Statistics Last %i Days" % days),
                      fontsize=18,
                      ha='center')

        #pylab.grid(True)

        pyplot.setp(ax.xaxis.get_majorticklabels(), rotation=70)
        ax.xaxis.set_major_formatter(dates.DateFormatter('%m/%d-%H'))
        pyplot.show()
        pyplot.savefig(
            "/home/pi/WeatherPiRasPiConnectServer/static/TemperatureHumidityGraph.png"
        )

    except mdb.Error, e:

        print "Error %d: %s" % (e.args[0], e.args[1])
Esempio n. 36
0
pl.hist(whk1124a, bins=20, alpha=0.5)
pl.xlabel('Whiker Length [adaptive moments]')
pl.vlines(0.2, 0, 10, color='r', lw=2)
pl.title('11/24/2012')
pl.grid()

pl.subplot(3, 3, 9)
pl.hist(whk1202a, bins=20, alpha=0.5)
pl.xlabel('Whiker Length [adaptive moments]')
pl.vlines(0.2, 0, 10, color='r', lw=2)
pl.ylim(0, 10)
pl.title('12/02/2012')
pl.grid()
pl.figtext(0.35,
           0.95,
           'Distribution of Mean Whisker Length',
           color='b',
           fontsize=18)

pl.savefig('whisker_robust_mean_summary.png')

#----median fwhm distribution  ----
pl.figure(figsize=(15, 15))
pl.subplot(3, 3, 1)
pl.hist(fwhm1123w, bins=20, alpha=0.5)
pl.xlabel('Whiker Length [weighed moments]')
pl.vlines(0.9, 0, 10, color='r', lw=2)
pl.ylim(0, 10)
pl.xlim(0.6, 2.0)
pl.title('11/23/2012')
pl.grid()
Esempio n. 37
0
    def aspcap_residue_plot(self,
                            test_predictions,
                            test_labels,
                            test_pred_error=None,
                            test_labels_err=None):
        """
        NAME:
            aspcap_residue_plot
        PURPOSE:
            plot aspcap residue
        INPUT:
            test_predictions (ndarray): Test result from neural network
            test_labels (ndarray): Gound truth for tests result
            test_pred_error (ndarray): (Optional) 1-sigma error for tests result from Baysian neural network.
            test_labels_err (ndarray): (Optional) Gound truth for tests result
        OUTPUT:
            None, just plots to be saved
        HISTORY:
            2018-Jan-28 - Written - Henry Leung (University of Toronto)
        """

        import pylab as plt
        import numpy as np
        import seaborn as sns

        print("Start plotting residues")

        resid = test_predictions - test_labels

        # Some plotting variables for asthetics
        plt.rcParams['axes.facecolor'] = 'white'
        sns.set_style("ticks")
        plt.rcParams['axes.grid'] = True
        plt.rcParams['grid.color'] = 'gray'
        plt.rcParams['grid.alpha'] = '0.4'

        x_lab = 'ASPCAP'
        y_lab = 'astroNN'
        fullname = self.targetname

        aspcap_residue_path = os.path.join(self.fullfilepath, 'ASPCAP_residue')

        if not os.path.exists(aspcap_residue_path):
            os.makedirs(aspcap_residue_path)

        mad_labels = np.zeros(test_labels.shape[1])

        for i in range(test_labels.shape[1]):
            not9999_index = np.where(test_labels[:, i] != MAGIC_NUMBER)
            mad_labels[i] = mad((test_labels[:, i])[not9999_index], axis=0)

        if test_pred_error is None:
            # To deal with prediction from non-Bayesian Neural Network
            test_pred_error = np.zeros(test_predictions.shape)

        for i in range(self._labels_shape):
            plt.figure(figsize=(15, 11), dpi=200)
            plt.axhline(0, ls='--', c='k', lw=2)
            not9999 = np.where(test_labels[:, i] != -9999.)[0]
            plt.errorbar((test_labels[:, i])[not9999], (resid[:, i])[not9999],
                         yerr=(test_pred_error[:, i])[not9999],
                         markersize=2,
                         fmt='o',
                         ecolor='g',
                         capthick=2,
                         elinewidth=0.5)

            plt.xlabel('ASPCAP ' + target_name_conversion(fullname[i]),
                       fontsize=25)
            plt.ylabel('$\Delta$ ' + target_name_conversion(fullname[i]) +
                       '\n(' + y_lab + ' - ' + x_lab + ')',
                       fontsize=25)
            plt.tick_params(labelsize=20, width=1, length=10)
            if self._labels_shape == 1:
                plt.xlim([
                    np.min((test_labels[:])[not9999]),
                    np.max((test_labels[:])[not9999])
                ])
            else:
                plt.xlim([
                    np.min((test_labels[:, i])[not9999]),
                    np.max((test_labels[:, i])[not9999])
                ])
            ranges = (np.max((test_labels[:, i])[not9999]) - np.min(
                (test_labels[:, i])[not9999])) / 2
            plt.ylim([-ranges, ranges])
            bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=2)
            bias = np.median((resid[:, i])[not9999], axis=0)
            scatter = mad((resid[:, i])[not9999], axis=0)
            plt.figtext(0.6,
                        0.75,
                        '$\widetilde{m}$=' + '{0:.3f}'.format(bias) +
                        ' $\widetilde{s}$=' +
                        '{0:.3f}'.format(scatter / float(mad_labels[i])) +
                        ' s=' + '{0:.3f}'.format(scatter),
                        size=25,
                        bbox=bbox_props)
            plt.tight_layout()
            plt.savefig(aspcap_residue_path + f'/{fullname[i]}_test.png')
            plt.close('all')
            plt.clf()

        if test_labels_err is not None:
            for i in range(self._labels_shape):
                plt.figure(figsize=(15, 11), dpi=200)
                plt.axhline(0, ls='--', c='k', lw=2)
                not9999 = np.where(test_labels[:, i] != -9999.)[0]

                plt.scatter((test_labels_err[:, i])[not9999],
                            (resid[:, i])[not9999],
                            s=0.7)
                plt.xlabel('ASPCAP Error of ' +
                           target_name_conversion(fullname[i]),
                           fontsize=25)
                plt.ylabel('$\Delta$ ' + target_name_conversion(fullname[i]) +
                           '\n(' + y_lab + ' - ' + x_lab + ')',
                           fontsize=25)
                plt.tick_params(labelsize=20, width=1, length=10)
                if self._labels_shape == 1:
                    plt.xlim([
                        np.percentile((test_labels_err[:])[not9999], 5),
                        np.percentile((test_labels_err[:])[not9999], 95)
                    ])
                else:
                    plt.xlim([
                        np.min((test_labels_err[:, i])[not9999]),
                        np.percentile((test_labels_err[:, i])[not9999], 90)
                    ])
                ranges = (np.percentile(
                    (resid[:, i])[not9999], 5) - np.percentile(
                        (resid[:, i])[not9999], 95))
                plt.ylim([-ranges, ranges])

                plt.tight_layout()
                plt.savefig(aspcap_residue_path +
                            f'/{fullname[i]}_test_err.png')
                plt.close('all')
                plt.clf()

        print("Finished plotting residues")
Esempio n. 38
0
def myplot(path='.',output=-1,sink=False,center=[0.5,0.5,0.5],los='x',size=[0.5,0.5], \
           minimum=None,maximum=None,typlot='rho',record=False,igrp=-1,plot_velocity=False,slice=False):

    filerep = path
    if (record):
        savedir = os.path.join(filerep, "results")
        if not os.path.exists(savedir):
            os.makedirs(savedir)
        #mu,alpha,lmax = parseDirSink(directory)
        #savedir       = checkDir(savedir, "mu" + mu)
        #savedir       = checkDir(savedir, "lmax" + lmax)

    # if output=-1, take last output
    if output == -1:
        outputname = np.sort(glob.glob(os.path.join(filerep,
                                                    "output_?????")))[-1]
        output = int(outputname.split('_')[-1])

    # Define the file
    ro = pm.RamsesOutput(filerep, output)

    if igrp != -1 and (igrp > ro.info["ngrp"] or igrp < 0):
        print(bold + 'PROBLEM:' + reset + ' igrp=' + str(igrp) +
              '  but simulation with ngrp=' + str(ro.info["ngrp"]))
        sys.exit(1)

    # Define the output data structure (to use with pymses5)
    ro.define_amr_scalar_field("hydro", "rho", 0)
    ro.define_amr_vector_field("hydro", "vel", [1, 2, 3])
    ro.define_amr_vector_field("hydro", "Bl", [4, 5, 6])
    ro.define_amr_vector_field("hydro", "Br", [7, 8, 9])
    ro.define_amr_scalar_field("hydro", "P", 10)
    ro.define_amr_multivalued_field("hydro", "Er", 11, ro.info["ngrp"])
    ro.define_amr_vector_field(
        "hydro", "J",
        [11 + ro.info["ngrp"], 12 + ro.info["ngrp"], 13 + ro.info["ngrp"]])
    ro.define_amr_scalar_field("hydro", "eint", 14 + ro.info["ngrp"])
    if ro.info["eos"]:
        ro.define_amr_scalar_field("hydro", "T", 15 + ro.info["ngrp"])
    ro.define_amr_vector_field("grav", "g", [0, 1, 2])

    time = ro.info["time"] * ro.info["unit_time"].express(ct.kyr)
    if (sink):
        # Read sink particles
        sink_filename = os.path.join(
            filerep, 'output_%05d' % output + '/sink_%05d' % output + '.csv')
        print("Reading sinks     : " + reset + sink_filename)
        sinkp = np.loadtxt(sink_filename,
                           dtype={
                               'names':
                               ('Id', 'mass', 'x', 'y', 'z', 'vx', 'vy', 'vz',
                                'rot_period', 'lx', 'ly', 'lz', 'acc_rate',
                                'acc_lum', 'age', 'int_lum', 'Teff'),
                               'formats':
                               ('i', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f',
                                'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f')
                           },
                           delimiter=',')
        sinkp = np.atleast_1d(sinkp)  # to work even if only 1 sink
        nsink = sinkp.size

        # Define the accretion radius
        if ("ncell_racc" in ro.info):
            ncell_racc = ro.info['ncell_racc']
        else:
            ncell_racc = 4

        dxmin = 0.5**ro.info['levelmax'] * ro.info["unit_length"].express(
            ct.au)
        r_acc = dxmin * ncell_racc
        #area  = np.pi*r_acc**2

        xc, yc, zc = sinkp['x'] / ro.info['boxlen'], sinkp['y'] / ro.info[
            'boxlen'], sinkp['z'] / ro.info['boxlen']
    else:
        nsink = 0

    if nsink == 0:
        nsink = 1
        # Center
        xc, yc, zc = np.atleast_1d(center[0]), np.atleast_1d(
            center[1]), np.atleast_1d(center[2])

    cmap = pl.cm.get_cmap('seismic', 100)
    if typlot == 'rho':
        cmap = pl.cm.get_cmap('jet', 100)
    elif typlot == 'Tr':
        cmap = pl.cm.get_cmap('hot', 100)
    elif typlot == 'B':
        cmap = pl.cm.get_cmap('PuOr', 100)
    elif typlot == 'beta_plasma':
        cmap = pl.cm.get_cmap('seismic', 100)
    else:
        cmap = pl.cm.get_cmap('jet', 100)

    # Width of the region to plot
    xr, yr = size[0], size[1]
    xbl = (-xr / 2. * ro.info["unit_length"]).express(ct.au)
    xbr = (+xr / 2. * ro.info["unit_length"]).express(ct.au)
    ybl = (-yr / 2. * ro.info["unit_length"]).express(ct.au)
    ybr = (+yr / 2. * ro.info["unit_length"]).express(ct.au)
    extent = [xbl, xbr, ybl, ybr]

    # Define camera
    if los == 'x':
        upvec = "z"
        vx = 1
        vy = 2
        labelx = 'Y (AU)'
        labely = 'Z (AU)'
        plane = 'yz'
    elif los == 'y':
        upvec = "x"
        vx = 2
        vy = 0
        labelx = 'Z (AU)'
        labely = 'X (AU)'
        plane = 'zx'
    elif los == 'z':
        upvec = "y"
        vx = 0
        vy = 1
        labelx = 'X (AU)'
        labely = 'Y (AU)'
        plane = 'xy'

    def plot_func(dset):
        if typlot == 'rho':
            return dset['rho'] * ro.info["unit_density"].express(
                ct.g / ct.cm**3)
        if typlot == 'eint':
            return dset['eint'] * (ro.info["unit_density"] *
                                   ro.info["unit_velocity"]**2).express(
                                       ct.erg / ct.cm**3)
        if typlot == 'T':
            if ro.info["eos"]:
                return dset['T']
            else:
                return dset['P'] / dset['rho'] * ro.info[
                    "unit_temperature"].express(ct.K) * ro.info["mu_gas"]
        if typlot == 'B':
            B = 1./4*( (dset['Bl'][:,0]+dset['Br'][:,0])**2  \
                      +(dset['Bl'][:,1]+dset['Br'][:,1])**2  \
                      +(dset['Bl'][:,2]+dset['Br'][:,2])**2)
            return B
        # To use with pymses5
        if typlot == 'Er' or typlot == 'Tr':
            if igrp == -1:
                Er = np.sum(dset['Er'], axis=-1) * (
                    ro.info["unit_density"] *
                    ro.info["unit_velocity"]**2).express(ct.erg / ct.cm**3)
            else:
                Er = dset['Er'][:, igrp - 1] * (
                    ro.info["unit_density"] *
                    ro.info["unit_velocity"]**2).express(ct.erg / ct.cm**3)

            if typlot == 'Er':
                return Er
            else:
                return (Er / ct.a_R.express(ct.erg / ct.cm**3 / ct.K**4))**0.25

        if typlot == 'entropy':
            return dset['P'] / dset['rho']**ro.hydro_info["gamma"]

        if typlot == 'Pth_Pdyn':
            return dset['P'] / (0.5 * dset['rho'] *
                                np.sum(dset['vel']**2, axis=-1))

        if typlot == 'beta_plasma':
            B = 1./4.*( (dset['Bl'][:,0]+dset['Br'][:,0])**2  \
                      +(dset['Bl'][:,1]+dset['Br'][:,1])**2  \
                      +(dset['Bl'][:,2]+dset['Br'][:,2])**2)
            if igrp == -1:
                Er = np.sum(
                    dset['Er'], axis=-1
                )  #*(ro.info["unit_density"]*ro.info["unit_velocity"]**2).express(ct.erg/ct.cm**3)
            else:
                Er = dset[
                    'Er'][:, igrp -
                          1]  #*(ro.info["unit_density"]*ro.info["unit_velocity"]**2).express(ct.erg/ct.cm**3)

            return dset['P'] / (B)  #/dset['rho'])

        else:
            print(bold + 'Problem in typlot')
            sys.exit()

    # Fields to be read
    fields_to_read = []
    if (not ro.info["eos"] and typlot == 'T') or typlot == 'entropy':
        fields_to_read = ["rho", "P"]
    elif typlot == 'Tr':
        fields_to_read = ["Er"]
    elif typlot == 'Pth_Pdyn':
        fields_to_read = ["P", "rho", "vel"]
    elif typlot == 'B':
        fields_to_read = ["Bl", "Br"]
    elif typlot == 'beta_plasma':
        fields_to_read = ["Bl", "Br", "rho", "P", "Er"]
    else:
        fields_to_read = [typlot]
    if (plot_velocity):
        fields_to_read.append("vel")
    if not slice:
        fields_to_read.append("rho")

    fields_to_read = list(set(fields_to_read))  #to remove duplicates

    if slice:
        source = ro.amr_source(fields_to_read)
        varplot_op = ScalarOperator(plot_func)
    else:
        rt = raytracing.RayTracer(ro, fields_to_read)

        if typlot == 'rho':
            func = lambda dset: plot_func(dset) * ro.info["unit_length"
                                                          ].express(ct.cm)
            varplot_op = ScalarOperator(func)
        else:
            up_func = lambda dset: (dset["rho"] * ro.info["unit_density"].
                                    express(ct.g / ct.cm**3) * plot_func(dset))
            down_func = lambda dset: (dset["rho"] * ro.info["unit_density"].
                                      express(ct.g / ct.cm**3))
            varplot_op = FractionOperator(up_func, down_func)

    for i in range(nsink):
        fig = pl.figure()
        ax = pl.subplot(111)

        pl.xlabel(labelx)
        pl.ylabel(labely)

        cam = Camera(center=[xc[i], yc[i], zc[i]],line_of_sight_axis=los,region_size=[xr, yr] \
                         , up_vector=upvec,map_max_size=512, log_sensitive=True)

        if slice:
            mapx = SliceMap(source, cam, varplot_op, z=0.)
        else:
            # WARNING! surf_qty=True to get an integral over dz and not dSdz
            # so that if typlot=='rho', get surface density map
            # and if not, get a density-weighted map (and not a mass-weighted map)
            mapx = rt.process(varplot_op, cam, surf_qty=True)

        if typlot == 'Pth_Pdyn':
            imx    = pl.imshow(mapx.transpose(), extent = extent, origin='lower' \
                                   , vmin = minimum, vmax = maximum,cmap=cmap)
            #imx    = pl.contourf(mapx.transpose(), extent = extent, origin='lower' \
            #                       , vmin = minimum, vmax = maximum)
            cbar = pl.colorbar()
            cbar.set_label(typlot)
        else:
            imx    = pl.imshow(np.log10(mapx.transpose()), extent = extent, origin='lower' \
                                   , vmin = minimum, vmax = maximum,cmap=cmap)
            #imx    = pl.contourf(np.log10(mapx.transpose()), extent = extent, origin='lower' \
            #                       , vmin = minimum, vmax = maximum)
            cbar = pl.colorbar()

            #            cmap = pl.cm.get_cmap('jet',100)

            if typlot == 'rho':
                cbar.set_label(r'$\mathrm{log(}\rho)$', fontsize=15)
            elif typlot == 'Tr':
                cbar.set_label(r'$\mathrm{log(T_r)}$', fontsize=15)
            elif typlot == 'Pth_Pdyn':
                cbar.set_label('Pth/Pdyn')
            elif typlot == 'B':
                cbar.set_label('log(|B|)')
            elif typlot == 'beta_plasma':
                cbar.set_label(r'$\mathrm{log(}\beta)$', fontsize=15)
            else:
                cbar.set_label('Log(' + typlot + ')')

        if (sink):
            for isink in range(nsink):
                # Plot sinks position
                mass = sinkp[isink]["mass"]
                col = cmap(int(mass * 100))
                xp = (sinkp[isink]['x'] / ro.info['boxlen'] -
                      xc[i]) * ro.info["unit_length"].express(ct.au)
                yp = (sinkp[isink]['y'] / ro.info['boxlen'] -
                      yc[i]) * ro.info["unit_length"].express(ct.au)
                zp = (sinkp[isink]['z'] / ro.info['boxlen'] -
                      zc[i]) * ro.info["unit_length"].express(ct.au)

                if los == 'x':
                    ax.add_patch(
                        pl.Circle((yp, zp),
                                  radius=r_acc,
                                  fc='white',
                                  alpha=0.65))
                    ax.add_patch(
                        pl.Circle((yp, zp),
                                  radius=(r_acc / ncell_racc),
                                  fc=col,
                                  ec=col))
                #pl.plot(yp,zp,'k+',mew=2,ms=10)
                elif los == 'y':
                    ax.add_patch(
                        pl.Circle((zp, xp),
                                  radius=r_acc,
                                  fc='white',
                                  alpha=0.65))
                    ax.add_patch(
                        pl.Circle((zp, xp),
                                  radius=(r_acc / ncell_racc),
                                  fc=col,
                                  ec=col))
                #pl.plot(zp,xp,'k+',mew=2,ms=10)
                elif los == 'z':
                    ax.add_patch(
                        pl.Circle((xp, yp),
                                  radius=r_acc,
                                  fc='white',
                                  alpha=0.65))
                    ax.add_patch(
                        pl.Circle((xp, yp),
                                  radius=(r_acc / ncell_racc),
                                  fc=col,
                                  ec=col))
                #pl.plot(xp,yp,'k+',mew=2,ms=10)

        # Plot velocity field
        if (slice and plot_velocity):
            p = cam.get_slice_points(0.0)
            nx, ny = cam.get_map_size()
            dset = pm.analysis.sample_points(source, p)
            vel = dset["vel"] * ro.info["unit_velocity"].express(ct.km / ct.s)
            rs = 32
            x = np.linspace(xbl, xbr, nx)
            y = np.linspace(ybl, ybr, ny)
            u, v = np.zeros((nx, ny)), np.zeros((nx, ny))
            mask = np.zeros((nx, ny))
            for ii in range(nx):
                for jj in range(ny):
                    if (ii % rs == 0 and jj % rs == 0):
                        u[ii, jj] = vel[:, vx].reshape(nx, ny)[ii, jj]
                        v[ii, jj] = vel[:, vy].reshape(nx, ny)[ii, jj]
                    else:
                        u[ii, jj] = 'Inf'
                        v[ii, jj] = 'Inf'
                        mask[ii, jj] = 1

            u2 = u[::rs, ::rs]
            v2 = v[::rs, ::rs]
            x2 = x[::rs]
            y2 = y[::rs]

            vel_mean = np.mean(np.sqrt(u2**2 + v2**2))
            vel_max = np.max(np.sqrt(u2**2 + v2**2))

            #pl.quiver(x,y,u.transpose(),v.transpose(),scale=20*nx)
            #Q=pl.quiver(x,y,u.transpose(),v.transpose(),scale=100,pivot='mid')
            #u_masked=np.ma.masked_array(u,mask=mask)
            #v_masked=np.ma.masked_array(v,mask=mask)
            #vel_mean=np.mean(np.sqrt(u_masked**2+v_masked**2))

            Q = pl.quiver(x2,
                          y2,
                          u2.transpose(),
                          v2.transpose(),
                          scale=100,
                          pivot='mid')
            #pl.quiverkey(Q,0.7,0.92,vel_mean,r'%.2f km/s'%vel_mean,coordinates='figure')
            pl.quiverkey(Q,
                         0.7,
                         0.92,
                         vel_max,
                         r'%.2f km/s' % vel_max,
                         coordinates='figure')
            pl.axis(extent)
            del (u, v, x, y, u2, v2, x2, y2)

            if (sink):
                # Print the mass of the sinks
                mass = 0
                for j in range(nsink):
                    mass = mass + sinkp["mass"][j]

                pl.figtext(0.01,
                           00.01,
                           "$\mathrm{M}_*=$" + "%4.1f " % mass + "$M_\odot$",
                           fontsize=15)

        pl.suptitle("%.3f kyr" % time)

        if (record):
            for format in ("png", "eps", "pdf"):
                if slice:
                    namefig = os.path.join(
                        savedir, "slice_" + typlot + "_" + plane + "_eps" +
                        "_%.3f" % size[0] + "_out" + "_%05d" % output +
                        "_isink_%05d." % sinkp['Id'][i] + format)
                else:
                    namefig = os.path.join(
                        savedir, "proj_" + typlot + "_" + plane + "_eps" +
                        "_%.3f" % size[0] + "_out" + "_%05d" % output +
                        "_isink_%05d." % sinkp['Id'][i] + format)
                print(bold + "Save figure: " + reset + namefig)
                pl.savefig(namefig)
            pl.close()
        else:
            pl.ion()
            pl.show()

    if (sink):
        # Print the mass of the sinks
        for i in range(nsink):
            mass = sinkp["mass"][i]
            print(bold + "Mass(" + str(i) + "): " + reset + "%.2e Msun" % mass)
Esempio n. 39
0
P.xlabel(r'$S_{\rm area}$ $[{\rm deg}^2]$', labelpad=10., fontdict={'fontsize':'xx-large'})
P.ylabel('$\sigma(w_0)$', labelpad=15., fontdict={'fontsize':'xx-large'})

# Set tick locations
P.gca().xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(1000))
#P.yscale('log')

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

P.ylim((0., YMAX))

print enames
print exptnames

# Experiment names
t1 = P.figtext(0.25, 0.88, enames[1], transform=P.gca().transAxes, fontsize=18, color=colours[1])
t2 = P.figtext(0.25, 0.82, enames[0], transform=P.gca().transAxes, fontsize=18, color=colours[0])

t1.set_bbox(dict(color='w', alpha=0.7))
t2.set_bbox(dict(color='w', alpha=0.7))

# Set size
P.tight_layout()
#P.gcf().set_size_inches(8.4, 7.8)
#P.gcf().set_size_inches(9.5, 6.8)
P.savefig(fname, transparent=True)
P.show()
Esempio n. 40
0
def CreateFrames(path, runName, runExt, t0, t1, plane, configFile):
    assert ((plane == 'xzplane') | (plane == 'xyplane'))

    planeSelect = {'xzplane': 'xzplane', 'xyplane': 'xyplane'}[plane]

    # Make sure the output directory exisits if not make it
    dirname = os.path.join(path, 'figs', plane)
    if not os.path.exists(dirname):
        os.makedirs(dirname)

    print(('Rendering ' + planeSelect + 'plane, storing frames at ' + dirname))
    #Now check to make sure the files are correct
    data = pyLTR.Models.LFM(path, runName, ext=runExt)
    modelVars = data.getVarNames()
    timeRange = data.getTimeRange()
    if len(timeRange) == 0:
        raise Exception((
            'No data files found.  Are you pointing to the correct run directory?'
        ))

    index0 = 0
    if t0:
        for i, t in enumerate(timeRange):
            if t0 >= t:
                index0 = i

    index1 = len(timeRange) - 1
    if t1:
        for i, t in enumerate(timeRange):
            if t1 >= t:
                index1 = i

    print(
        ('Extracting LFM MAG quantities for time series over %d time steps.' %
         (index1 - index0)))

    # Output a status bar displaying how far along the computation is.
    progress = pyLTR.StatusBar(0, index1 - index0)
    progress.start()

    # Deal with the plot options
    if (configFile == None):
        vxOpts = {'min': -800., 'max': 800., 'colormap': 'RdBu_r'}
        vyOpts = {'min': -100., 'max': 100., 'colormap': 'RdBu_r'}
        vzOpts = {'min': -100., 'max': 100., 'colormap': 'RdBu_r'}
        bxOpts = {'min': -10., 'max': 10.}
        byOpts = {'min': -10., 'max': 10.}
        bzOpts = {'min': -10., 'max': 10.}
        rhoOpts = {'min': 0, 'max': 50}
        optsObject = {
            'vx': vxOpts,
            'vy': vyOpts,
            'vz': vzOpts,
            'bx': bxOpts,
            'by': byOpts,
            'bz': bzOpts,
            'rho': rhoOpts
        }
        configFilename = os.path.join(dirname, 'MHDPlanes.config')
        print(("Writing plot config file at " + configFilename))
        f = open(configFilename, 'w')
        f.write(pyLTR.yaml.safe_dump(optsObject, default_flow_style=False))
        f.close()
    else:
        f = open(configFile, 'r')
        optsDict = pyLTR.yaml.safe_load(f.read())
        f.close()
        if ('vx' in optsDict):
            vxOpts = optsDict['vx']
        else:
            vxOpts = {'min': -800., 'max': 800., 'colormap': 'RdBu_r'}
        if ('vy' in optsDict):
            vyOpts = optsDict['vy']
        else:
            vyOpts = {'min': -100., 'max': 100., 'colormap': 'RdBu_r'}
        if ('vz' in optsDict):
            vzOpts = optsDict['vz']
        else:
            vzOpts = {'min': -100., 'max': 100., 'colormap': 'RdBu_r'}
        if ('bx' in optsDict):
            bxOpts = optsDict['bx']
        else:
            bxOpts = {'min': -10., 'max': 10.}
        if ('by' in optsDict):
            byOpts = optsDict['by']
        else:
            byOpts = {'min': -10., 'max': 10.}
        if ('bz' in optsDict):
            bzOpts = optsDict['bz']
        else:
            bzOpts = {'min': -10., 'max': 10.}
        if ('rho' in optsDict):
            bzOpts = optsDict['rho']
        else:
            rhoOpts = {'min': 0., 'max': 50.}

    vals = data.getEqSlice('X_grid', timeRange[0]) / 6.38e8
    xeq = {'data': vals, 'name': r'$X$', 'units': r'$R_E$'}
    vals = data.getEqSlice('Y_grid', timeRange[0]) / 6.38e8
    yeq = {'data': vals, 'name': r'$X$', 'units': r'$R_E$'}
    vals = data.getEqSlice('Z_grid', timeRange[0]) / 6.38e8
    zeq = {'data': vals, 'name': r'$X$', 'units': r'$R_E$'}
    vals = data.getMerSlice('X_grid', timeRange[0]) / 6.38e8
    xmer = {'data': vals, 'name': r'$X$', 'units': r'$R_E$'}
    vals = data.getMerSlice('Y_grid', timeRange[0]) / 6.38e8
    ymer = {'data': vals, 'name': r'$X$', 'units': r'$R_E$'}
    vals = data.getMerSlice('Z_grid', timeRange[0]) / 6.38e8
    zmer = {'data': vals, 'name': r'$X$', 'units': r'$R_E$'}
    for i, time in enumerate(timeRange[index0:index1]):
        try:
            #first read the data
            vals = data.getEqSlice('vx_', time) / 1000.0
            vx = {'data': vals, 'name': r'$V_X$', 'units': r'km/s'}
            vals = data.getEqSlice('vy_', time) / 1000.0
            vy = {'data': vals, 'name': r'$V_Y$', 'units': r'km/s'}
            vals = data.getEqSlice('vz_', time) / 1000.0
            vz = {'data': vals, 'name': r'$V_Z$', 'units': r'km/s'}
            vals = data.getEqSlice('bx_', time)
            bx = {'data': vals, 'name': r'$B_X$', 'units': r'nT'}
            vals = data.getEqSlice('by_', time)
            by = {'data': vals, 'name': r'$B_Y$', 'units': r'nT'}
            vals = -1.0 * data.getEqSlice('bz_', time)
            bz = {'data': vals, 'name': r'$B_Z$', 'units': r'S'}
            # Now onto the plot
            tt = time.timetuple()
            p.figure(figsize=(16, 12))
            p.figtext(0.5,
                      0.92,
                      'LFM ' + '\n%4d-%02d-%02d  %02d:%02d:%02d' %
                      (tt.tm_year, tt.tm_mon, tt.tm_mday, tt.tm_hour,
                       tt.tm_min, tt.tm_sec),
                      fontsize=14,
                      multialignment='center')
            ax = p.subplot(231)
            x = xeq['data']
            y = yeq['data']
            pyLTR.Graphics.CutPlane.CutPlaneDict(x, y, vx, vxOpts, userAxes=ax)
            ax = p.subplot(234)
            pyLTR.Graphics.CutPlane.CutPlaneDict(x, y, vy, vyOpts, userAxes=ax)
            ax = p.subplot(232)
            pyLTR.Graphics.CutPlane.CutPlaneDict(x, y, vz, vzOpts, userAxes=ax)
            ax = p.subplot(235)
            pyLTR.Graphics.CutPlane.CutPlaneDict(x, y, bx, bxOpts, userAxes=ax)
            ax = p.subplot(233)
            pyLTR.Graphics.CutPlane.CutPlaneDict(x, y, by, byOpts, userAxes=ax)
            ax = p.subplot(236)
            pyLTR.Graphics.CutPlane.CutPlaneDict(x, y, bz, bzOpts, userAxes=ax)
            savefigName = os.path.join(path, 'figs', plane,
                                       'frame_%05d.png' % i)
            p.savefig(savefigName, dpi=100)
            p.close()
            progress.increment()
        except KeyboardInterrupt:
            # Exit when the user hits CTRL+C.
            progress.stop()
            progress.join()
            print('Exiting.')
            import sys
            sys.exit(0)
        except:
            # Cleanup progress bar if something bad happened.
            progress.stop()
            progress.join()
            raise
    progress.stop()
    progress.join()

    return os.path.join(path, 'figs', plane)
print "Sensors names : %s" % significant_sensors_names

###############################################################################
# View location of significantly active sensors
import pylab as pl

# load sensor layout
from mne.layouts import read_layout
layout = read_layout('Vectorview-grad')

# Extract mask and indices of active sensors in layout
idx_of_sensors = [layout.names.index(name)
                  for name in significant_sensors_names
                  if name in layout.names]
mask_significant_sensors = np.zeros(len(layout.pos), dtype=np.bool)
mask_significant_sensors[idx_of_sensors] = True
mask_non_significant_sensors = mask_significant_sensors == False

# plot it
pl.figure(facecolor='k')
pl.axis('off')
pl.axis('tight')
pl.scatter(layout.pos[mask_significant_sensors, 0],
           layout.pos[mask_significant_sensors, 1], s=50, c='r')
pl.scatter(layout.pos[mask_non_significant_sensors, 0],
           layout.pos[mask_non_significant_sensors, 1], c='w')
title = 'MNE sample data (Left auditory between 40 and 60 ms)'
pl.figtext(0.03, 0.93, title, color='w', fontsize=18)
pl.show()
pl.show()
Esempio n. 42
0
def visualize_single_step(mod, i, alpha=0., description_str=''):
    """ Show how a random walk in a two dimensional space has
    progressed up to step i"""

    X = mod.X.trace()

    pl.clf()

    sq_size = .3

    # show 2d trace
    pl.axes([.05, .05, sq_size, sq_size])

    pl.plot(X[:i, 0], X[:i, 1], 'b.-', alpha=.1)

    Y = alpha * X[i, :] + (1 - alpha) * X[i - 1, :]
    pl.plot([Y[0], Y[0]], [Y[1], 2.], 'k-', alpha=.5)
    pl.plot([Y[0], 2], [Y[1], Y[1]], 'k-', alpha=.5)
    pl.plot(Y[0], Y[1], 'go')

    if hasattr(mod, 'shape'):
        pl.fill(mod.shape[:, 0], mod.shape[:, 1], color='b', alpha=.2)
    if hasattr(mod, 'plot_distribution'):
        mod.plot_distribution()

    pl.axis([-1.1, 1.1, -1.1, 1.1])
    pl.xticks([])
    pl.yticks([])

    # show 1d marginals

    ## X[0] is horizontal position
    pl.axes([.05, .05 + sq_size, sq_size, 1. - .1 - sq_size])
    pl.plot(X[:(i + 1), 0], i + 1 - pl.arange(i + 1), 'k-')
    pl.axis([-1.1, 1.1, 0, 1000])
    pl.xticks([])
    pl.yticks([])
    pl.text(-1, .1, '$X_0$')

    ## X[1] is vertical position
    pl.axes([.05 + sq_size, .05, 1. - .1 - sq_size, sq_size])
    pl.plot(i + 1 - pl.arange(i + 1), X[:(i + 1), 1], 'k-')
    pl.axis([0, 1000, -1.1, 1.1])
    pl.xticks([])
    pl.yticks([])
    pl.text(10, -1., '$X_1$')

    ## show X[i, j] acorr
    N, D = X.shape
    if i > 250:
        for j in range(D):
            pl.axes([
                1 - .1 - 1.5 * sq_size * (1 - j * D**-1.),
                1. - .1 - 1.5 * sq_size * D**-1, 1.5 * sq_size * D**-1.,
                1.5 * sq_size * D**-1.
            ])
            pl.acorr(X[(i / 2.):i:10, j], detrend=pl.mlab.detrend_mean)
            pl.xlabel('$X_%d$' % j)
            if j == 0:
                pl.ylabel('autocorr')
            pl.xticks([])
            pl.yticks([])
            pl.axis([-10, 10, -.1, 1])
    ## show X[1] acorr

    ## textual information
    str = ''
    str += 't = %d\n' % i
    str += 'acceptance rate = %.2f\n\n' % (
        1. - pl.mean(pl.diff(X[(i / 2.):i, 0]) == 0.))

    str += 'mean(X) = %s' % pretty_array(X[(i / 2.):i, :].mean(0))
    if hasattr(mod, 'true_mean'):
        str += ' / true mean = %s\n' % pretty_array(mod.true_mean)
    else:
        str += '\n'

    if i > 10:
        iqr = pl.sort(X[(i / 2.):i, :],
                      axis=0)[[.25 * (i / 2.), .75 * (i / 2.)], :].T

        for j in range(D):
            str += 'IQR(X[%d]) = (%.2f, %.2f)' % (j, iqr[j, 0], iqr[j, 1])
            if hasattr(mod, 'true_iqr'):
                str += ' / true IQR = %s\n' % mod.true_iqr[j]
            else:
                str += '\n'
    pl.figtext(.05 + .01 + sq_size,
               .05 + .01 + sq_size,
               str,
               va='bottom',
               ha='left')

    pl.figtext(sq_size + .5 * (1. - sq_size),
               .96,
               description_str,
               va='top',
               ha='center',
               size=32)

    pl.figtext(.95, .05, 'healthyalgorithms.wordpress.com', ha='right')
Esempio n. 43
0
def apogee_model_eval(h5name=None,
                      folder_name=None,
                      check_cannon=None,
                      test_noisy=None):
    """
    NAME: apogee_model_eval
    PURPOSE: To test the model and generate plots
    INPUT:
        h5name = Name of the h5 data set
        folder_name = the folder name contains the model
        check_cannon = check cannon result or not
        test_noist = whether test noisy training data or not (both adding noise and transolational shift)
    OUTPUT: target and normalized data
    HISTORY:
        2017-Oct-14 Henry Leung
    """

    # prevent Tensorflow taking up all the GPU memory
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    set_session(tf.Session(config=config))

    h5name_check(h5name)

    if test_noisy is None:
        test_noisy = False

    h5test = h5name + '_test.h5'
    traindata = h5name + '_train.h5'

    currentdir = os.getcwd()
    fullfolderpath = currentdir + '/' + folder_name
    print(fullfolderpath)
    mean_and_std = np.load(fullfolderpath + '/meanstd.npy')
    spec_meanstd = np.load(fullfolderpath + '/spectra_meanstd.npy')
    target = np.load(fullfolderpath + '/targetname.npy')
    modelname = '/model_{}.h5'.format(folder_name[-11:])
    model = load_model(os.path.normpath(fullfolderpath + modelname))

    mean_labels = mean_and_std[0]
    std_labels = mean_and_std[1]
    num_labels = mean_and_std.shape[1]

    # ensure the file will be cleaned up
    with h5py.File(h5test) as F:
        i = 0
        index_not9999 = []
        for tg in target:
            temp = np.array(F['{}'.format(tg)])
            temp_index = np.where(temp != -9999)
            if i == 0:
                index_not9999 = temp_index
                i += 1
            else:
                index_not9999 = reduce(np.intersect1d,
                                       (index_not9999, temp_index))

        test_spectra = np.array(F['spectra'])
        test_spectra = test_spectra[index_not9999]
        test_spectra -= spec_meanstd[0]
        test_spectra /= spec_meanstd[1]

        i = 0
        test_labels = np.array((test_spectra.shape[1]))
        for tg in target:  # load data
            temp = np.array(F['{}'.format(tg)])
            temp = temp[index_not9999]
            if i == 0:
                test_labels = temp[:]
                if len(target) == 1:
                    test_labels = test_labels.reshape((len(test_labels), 1))
                i += 1
            else:
                test_labels = np.column_stack((test_labels, temp[:]))
        apogee_index = np.array(F['index'])[index_not9999]

    print('Test set contains ' + str(len(test_spectra)) + ' stars')

    time1 = time.time()
    test_predictions = batch_predictions(model, test_spectra, 500, num_labels,
                                         std_labels, mean_labels)
    print("{0:.2f}".format(time.time() - time1) + ' seconds to make ' +
          str(len(test_spectra)) + ' predictions')

    resid = test_predictions - test_labels
    bias = np.median(resid, axis=0)
    scatter = mad_std(resid, axis=0)

    # Some plotting variables for asthetics
    plt.rcParams['axes.facecolor'] = 'white'
    sns.set_style("ticks")
    plt.rcParams['axes.grid'] = True
    plt.rcParams['grid.color'] = 'gray'
    plt.rcParams['grid.alpha'] = '0.4'

    x_lab = 'ASPCAP'
    y_lab = 'astroNN'
    for i in range(num_labels):
        plt.figure(figsize=(15, 11), dpi=200)
        plt.axhline(0, ls='--', c='k', lw=2)
        plt.scatter(test_labels[:, i], resid[:, i], s=3)
        fullname = target_name_conversion(target[i])
        plt.xlabel('ASPCAP ' + fullname, fontsize=25)
        plt.ylabel('$\Delta$ ' + fullname + '\n(' + y_lab + ' - ' + x_lab +
                   ')',
                   fontsize=25)
        plt.tick_params(labelsize=20, width=1, length=10)
        if num_labels == 1:
            plt.xlim([np.min(test_labels[:]), np.max(test_labels[:])])
        else:
            plt.xlim([np.min(test_labels[:, i]), np.max(test_labels[:, i])])
        ranges = (np.max(test_labels[:, i]) - np.min(test_labels[:, i])) / 2
        plt.ylim([-ranges, ranges])
        bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=2)
        plt.figtext(0.6,
                    0.75,
                    '$\widetilde{m}$=' + '{0:.3f}'.format(bias[i]) +
                    ' $\widetilde{s}$=' +
                    '{0:.3f}'.format(scatter[i] / std_labels[i]) + ' s=' +
                    '{0:.3f}'.format(scatter[i]),
                    size=25,
                    bbox=bbox_props)
        plt.tight_layout()
        plt.savefig(fullfolderpath + '/{}_test.png'.format(target[i]))
        plt.close('all')
        plt.clf()

    if traindata is not None:
        with h5py.File(traindata) as F:
            index_not9999 = []
            for counter, tg in enumerate(target):
                temp = np.array(F['{}'.format(tg)])
                temp_index = np.where(temp != -9999)
                if counter == 0:
                    index_not9999 = temp_index
                else:
                    index_not9999 = reduce(np.intersect1d,
                                           (index_not9999, temp_index))

            train_spectra = np.array(F['spectra'])
            train_spectra = train_spectra[index_not9999]
            sigma = 0.08**2
            train_spectra_noisy = train_spectra + np.random.poisson(
                sigma, train_spectra.shape)
            train_spectra -= spec_meanstd[0]
            train_spectra /= spec_meanstd[1]
            train_spectra_noisy -= spec_meanstd[0]
            train_spectra_noisy /= spec_meanstd[1]
            random_num_color = np.array([])
            for index in range(train_spectra_noisy.shape[0]):
                # make sure no 0 pixel shift
                while True:
                    random_temp = np.random.randint(-7, 7)
                    if random_temp != 0:
                        break
                random_num_color = np.append(random_num_color, random_temp)
                train_spectra_noisy[index] = np.roll(
                    train_spectra_noisy[index], random_temp)
            train_labels = np.array((train_spectra.shape[1]))
            for counter, tg in enumerate(target):  # load data
                temp = np.array(F['{}'.format(tg)])
                temp = temp[index_not9999]
                if counter == 0:
                    train_labels = temp[:]
                    if len(target) == 1:
                        train_labels = train_labels.reshape(
                            (len(train_labels), 1))
                else:
                    train_labels = np.column_stack((train_labels, temp[:]))

        if test_noisy is True:
            train_noisy_predictions = batch_predictions(
                model, train_spectra_noisy, 500, num_labels, std_labels,
                mean_labels)
            train_predictions = batch_predictions(model, train_spectra, 500,
                                                  num_labels, std_labels,
                                                  mean_labels)
            resid_noisy = train_noisy_predictions - train_labels
            resid_train = train_predictions - train_labels
            bias_train = np.median(resid_train, axis=0)
            scatter_train = mad_std(resid_train, axis=0)
            bias_noisy = np.median(resid_noisy, axis=0)
            scatter_noisy = mad_std(resid_noisy, axis=0)

            # Some plotting variables for aesthetics
            plt.rcParams['axes.facecolor'] = 'white'
            sns.set_style("ticks")
            plt.rcParams['axes.grid'] = True
            plt.rcParams['grid.color'] = 'gray'
            plt.rcParams['grid.alpha'] = '0.4'

            x_lab = 'ASPCAP'
            y_lab = 'astroNN'
            trainplot_noisy_fullpath = os.path.join(fullfolderpath,
                                                    'Noisy_TrainData_Plots/')
            trainplot_noisy_2_fullpath = os.path.join(
                fullfolderpath, 'Noisy_TrainData_Plots_02/')
            trainplot_fullpath = os.path.join(fullfolderpath,
                                              'TrainData_Plots/')

            # check folder existence
            if not os.path.exists(trainplot_fullpath):
                os.makedirs(trainplot_fullpath)
            if not os.path.exists(trainplot_noisy_fullpath):
                os.makedirs(trainplot_noisy_fullpath)
            if not os.path.exists(trainplot_noisy_2_fullpath):
                os.makedirs(trainplot_noisy_2_fullpath)

            for i in range(num_labels):
                plt.figure(figsize=(15, 11), dpi=200)
                plt.axhline(0, ls='--', c='k', lw=2)
                plt.scatter(train_labels[:, i], resid_train[:, i], s=3)
                fullname = target_name_conversion(target[i])
                plt.xlabel('ASPCAP ' + fullname, fontsize=25)
                plt.ylabel('$\Delta$ ' + fullname + '\n(' + y_lab + ' - ' +
                           x_lab + ')',
                           fontsize=25)
                plt.tick_params(labelsize=20, width=1, length=10)
                if num_labels == 1:
                    plt.xlim(
                        [np.min(train_labels[:]),
                         np.max(train_labels[:])])
                else:
                    plt.xlim([
                        np.min(train_labels[:, i]),
                        np.max(train_labels[:, i])
                    ])
                ranges = (np.max(train_labels[:, i]) -
                          np.min(train_labels[:, i])) / 2
                plt.ylim([-ranges, ranges])
                bbox_props = dict(boxstyle="square,pad=0.3",
                                  fc="w",
                                  ec="k",
                                  lw=2)
                plt.figtext(
                    0.6,
                    0.75,
                    '$\widetilde{m}$=' + '{0:.3f}'.format(bias_train[i]) +
                    ' $\widetilde{s}$=' +
                    '{0:.3f}'.format(scatter_train[i] / std_labels[i]) +
                    ' s=' + '{0:.3f}'.format(scatter_train[i]),
                    size=25,
                    bbox=bbox_props)
                plt.tight_layout()
                plt.savefig(trainplot_fullpath +
                            '{}_train_data.png'.format(target[i]))
                plt.close('all')
                plt.clf()

                plt.figure(figsize=(15, 11), dpi=200)
                plt.axhline(0, ls='--', c='k', lw=2)
                plt.scatter(train_labels[:, i],
                            resid_noisy[:, i],
                            c=random_num_color,
                            s=3,
                            cmap='gray')
                fullname = target_name_conversion(target[i])
                plt.xlabel('ASPCAP ' + fullname, fontsize=25)
                plt.ylabel('$\Delta$ ' + fullname + '\n(' + y_lab + ' - ' +
                           x_lab + ')',
                           fontsize=25)
                plt.tick_params(labelsize=20, width=1, length=10)
                if num_labels == 1:
                    plt.xlim(
                        [np.min(train_labels[:]),
                         np.max(train_labels[:])])
                else:
                    plt.xlim([
                        np.min(train_labels[:, i]),
                        np.max(train_labels[:, i])
                    ])
                ranges = (np.max(train_labels[:, i]) -
                          np.min(train_labels[:, i])) / 2
                plt.ylim([-ranges, ranges])
                bbox_props = dict(boxstyle="square,pad=0.3",
                                  fc="w",
                                  ec="k",
                                  lw=2)
                plt.figtext(
                    0.5,
                    0.85,
                    '$\widetilde{m}$=' + '{0:.3f}'.format(bias_noisy[i]) +
                    ' $\widetilde{s}$=' +
                    '{0:.3f}'.format(scatter_noisy[i] / std_labels[i]) +
                    ' s=' + '{0:.3f}'.format(scatter_noisy[i]),
                    size=25,
                    bbox=bbox_props)
                cbar = plt.colorbar()
                cbar.ax.tick_params(labelsize=25, width=1, length=10)
                plt.tight_layout()
                plt.savefig(trainplot_noisy_fullpath +
                            '{}_noisytrain_data.png'.format(target[i]))
                plt.close('all')
                plt.clf()
    if check_cannon is True:
        astroNN.apogee.cannon.cannon_plot(apogee_index,
                                          std_labels,
                                          target,
                                          folder_name=folder_name,
                                          aspcap_answer=test_labels)

    return None
    inf_high.append(msims[scen].results['new_infections'].high[wd])

epsx = 0.003
llpad = 0.01

lockdown1 = [sim.day('2020-03-23'), sim.day('2020-05-31')]
lockdown2 = [sim.day('2020-11-05'), sim.day('2020-12-03')]
lockdown3 = [sim.day('2021-01-04'), sim.day('2021-02-08')]

for nc in range(ncols):
    pl.figtext(xgapl + (dx + xgapm) * nc + epsx,
               ygapb + dy * nrows + ygapm * (nrows - 1) + llpad,
               labels[nc],
               fontsize=36,
               fontweight='bold',
               bbox={
                   'edgecolor': 'none',
                   'facecolor': 'white',
                   'alpha': 0.5,
                   'pad': 4
               })

for pn in range(nplots):
    ax[pn] = pl.axes([
        xgapl + (dx + xgapm) * (pn % ncols),
        ygapb + (ygapm + dy) * (pn // ncols), dx, dy
    ])
    print([
        xgapl + (dx + xgapm) * (pn % ncols),
        ygapb + (ygapm + dy) * (pn // ncols)
    ])
Esempio n. 45
0
def CreateFrames(path, runName, runExt, t0, t1, hemisphere, configFile):
    assert ((hemisphere == 'north') | (hemisphere == 'south'))

    hemiSelect = {'north': 'north', 'south': 'south'}[hemisphere]

    # Make sure the output directory exisits if not make it
    dirname = os.path.join(path, 'figs', hemisphere)
    if not os.path.exists(dirname):
        os.makedirs(dirname)

    print(('Rendering ' + hemiSelect + 'ern hemisphere, storing frames at ' +
           dirname))
    #Now check to make sure the files are correct
    data = pyLTR.Models.LFMION(path, runName, runExt)
    modelVars = data.getVarNames()
    for v in [
            'x_interp', 'y_interp', 'potnorth', 'potsouth', 'curnorth',
            'cursouth', 'SigmaP_north', 'SigmaP_south', 'SigmaH_north',
            'SigmaH_south', 'avE_north', 'avE_south', 'fluxnorth', 'fluxsouth'
    ]:
        assert (v in modelVars)

    timeRange = data.getTimeRange()
    if len(timeRange) == 0:
        raise Exception((
            'No data files found.  Are you pointing to the correct run directory?'
        ))

    index0 = 0
    if t0:
        for i, t in enumerate(timeRange):
            if t0 >= t:
                index0 = i

    index1 = len(timeRange) - 1
    if t1:
        for i, t in enumerate(timeRange):
            if t1 >= t:
                index1 = i

    print(
        ('Extracting LFM ION quantities for time series over %d time steps.' %
         (index1 - index0)))

    # Output a status bar displaying how far along the computation is.
    progress = pyLTR.StatusBar(0, index1 - index0)
    progress.start()

    # Pre-compute r and theta
    x = data.read('x_interp', timeRange[index0])
    y = data.read('y_interp', timeRange[index0])
    theta = n.arctan2(y, x)
    theta[theta < 0] = theta[theta < 0] + 2 * n.pi
    # plotting routines now rotate local noon to point up
    #theta=theta+n.pi/2 # to put noon up
    r = n.sqrt(x**2 + y**2)
    # plotting routines now expect longitude and colatitude, in radians, stored in dictionaries
    longitude = {'data': theta, 'name': r'\phi', 'units': r'rad'}
    colatitude = {'data': n.arcsin(r), 'name': r'\theta', 'units': r'rad'}

    # Deal with the plot options
    if (configFile == None):
        potOpts = {'min': -100., 'max': 100., 'colormap': 'RdBu_r'}
        facOpts = {'min': -1., 'max': 1., 'colormap': 'RdBu_r'}
        pedOpts = {'min': 1., 'max': 20.}
        halOpts = {'min': 1., 'max': 20.}
        engOpts = {'min': 0., 'max': 20.}
        flxOpts = {'min': 0., 'max': 1.0e9, 'format_str': '%.1e'}
        optsObject = {
            'pot': potOpts,
            'fac': facOpts,
            'ped': pedOpts,
            'hall': halOpts,
            'energy': engOpts,
            'flux': flxOpts
        }
        configFilename = os.path.join(dirname, 'IonSum.config')
        print(("Writing plot config file at " + configFilename))
        f = open(configFilename, 'w')
        f.write(pyLTR.yaml.safe_dump(optsObject, default_flow_style=False))
        f.close()
    else:
        f = open(configFile, 'r')
        optsDict = pyLTR.yaml.safe_load(f.read())
        f.close()
        if ('pot' in optsDict):
            potOpts = optsDict['pot']
        else:
            potOpts = {'min': -100., 'max': 100., 'colormap': 'RdBu_r'}
        if ('fac' in optsDict):
            facOpts = optsDict['fac']
        else:
            facOpts = {'min': -1., 'max': 1., 'colormap': 'RdBu_r'}
        if ('ped' in optsDict):
            pedOpts = optsDict['ped']
        else:
            pedOpts = {'min': 1., 'max': 8.}
        if ('hall' in optsDict):
            halOpts = optsDict['hall']
        else:
            halOpts = {'min': 1., 'max': 8.}
        if ('energy' in optsDict):
            engOpts = optsDict['energy']
        else:
            engOpts = {'min': 0., 'max': 20.}
        if ('flux' in optsDict):
            flxOpts = optsDict['flux']
        else:
            flxOpts = {'min': 0., 'max': 1.0e9, 'format_str': '%.1e'}

    for i, time in enumerate(timeRange[index0:index1]):
        try:
            #first read the data
            vals = data.read('pot' + hemiSelect, time) / 1000.0
            psi = {'data': vals, 'name': r'$\Phi$', 'units': r'kV'}
            vals = data.read('cur' + hemiSelect, time) * 1.0e6
            fac = {
                'data': vals,
                'name': r'$J_{||}$',
                'units': r'$\mu\mathrm{A/m^2}$'
            }
            vals = data.read('avE_' + hemiSelect, time)
            eng = {'data': vals, 'name': r'Energy', 'units': r'keV'}
            vals = data.read('flux' + hemiSelect, time)
            flx = {
                'data': vals,
                'name': r'Flux',
                'units': r'$\mathrm{1/cm^2s}$'
            }
            vals = data.read('SigmaP_' + hemiSelect, time)
            ped = {'data': vals, 'name': r'$\Sigma_{P}$', 'units': r'S'}
            vals = -1.0 * data.read('SigmaH_' + hemiSelect, time)
            hal = {'data': vals, 'name': r'$\Sigma_{H}$', 'units': r'S'}
            # Now onto the plot
            tt = time.timetuple()
            p.figure(figsize=(16, 12))
            p.figtext(
                0.5,
                0.92,
                'LFM ION ' + hemiSelect + '\n%4d-%02d-%02d  %02d:%02d:%02d' %
                (tt.tm_year, tt.tm_mon, tt.tm_mday, tt.tm_hour, tt.tm_min,
                 tt.tm_sec),
                fontsize=14,
                multialignment='center')
            ax = p.subplot(231, polar=True)
            pyLTR.Graphics.PolarPlot.BasicPlotDict(longitude,
                                                   colatitude,
                                                   psi,
                                                   potOpts,
                                                   userAxes=ax,
                                                   useMesh=True)
            ax = p.subplot(234, polar=True)
            pyLTR.Graphics.PolarPlot.BasicPlotDict(longitude,
                                                   colatitude,
                                                   fac,
                                                   facOpts,
                                                   userAxes=ax,
                                                   useMesh=True)
            ax = p.subplot(232, polar=True)
            pyLTR.Graphics.PolarPlot.BasicPlotDict(longitude,
                                                   colatitude,
                                                   ped,
                                                   pedOpts,
                                                   userAxes=ax,
                                                   useMesh=True)
            ax = p.subplot(235, polar=True)
            pyLTR.Graphics.PolarPlot.BasicPlotDict(longitude,
                                                   colatitude,
                                                   hal,
                                                   halOpts,
                                                   userAxes=ax,
                                                   useMesh=True)
            ax = p.subplot(233, polar=True)
            pyLTR.Graphics.PolarPlot.BasicPlotDict(longitude,
                                                   colatitude,
                                                   eng,
                                                   engOpts,
                                                   userAxes=ax,
                                                   useMesh=True)
            ax = p.subplot(236, polar=True)
            pyLTR.Graphics.PolarPlot.BasicPlotDict(longitude,
                                                   colatitude,
                                                   flx,
                                                   flxOpts,
                                                   userAxes=ax,
                                                   useMesh=True)
            savefigName = os.path.join(path, 'figs', hemisphere,
                                       'frame_summary_%05d.png' % i)
            p.savefig(savefigName, dpi=100)
            p.close()
            progress.increment()
        except KeyboardInterrupt:
            # Exit when the user hits CTRL+C.
            progress.stop()
            progress.join()
            print('Exiting.')
            import sys
            sys.exit(0)
        except:
            # Cleanup progress bar if something bad happened.
            progress.stop()
            progress.join()
            raise
    progress.stop()
    progress.join()

    return os.path.join(path, 'figs', hemisphere)
Esempio n. 46
0
def plot_calibration(sims, date, do_save=0):

    sim = sims[0] # For having a sim to refer to

    # Draw plots
    fig1_path = f'calibration_{date}_fig1.png'
    fig2_path = f'calibration_{date}_fig2.png'
    fig_args    = sc.mergedicts({'figsize': (16, 14)})
    axis_args   = sc.mergedicts({'left': 0.10, 'bottom': 0.05, 'right': 0.95, 'top': 0.93, 'wspace': 0.25, 'hspace': 0.40})

    # Handle input arguments -- merge user input with defaults
    low_q = 0.1
    high_q = 0.9

    # Figure 1: Calibration
    pl.figure(**fig_args)
    pl.subplots_adjust(**axis_args)
    pl.figtext(0.42, 0.95, 'Model calibration', fontsize=30)


    #%% Figure 1, panel 1
    ax = pl.subplot(4,1,1)
    format_ax(ax, sim)
    plotter('new_tests', sims, ax, calib=True, label='Number of tests per day', ylabel='Tests')
    plotter('new_diagnoses', sims, ax, calib=True, label='Number of diagnoses per day', ylabel='Tests')


    #%% Figure 1, panel 2
    ax = pl.subplot(4,1,2)
    format_ax(ax, sim)
    plotter('cum_diagnoses', sims, ax, calib=True, label='Cumulative diagnoses', ylabel='People')


    #%% Figure 1, panel 3
    ax = pl.subplot(4,1,3)
    format_ax(ax, sim)
    plotter('cum_deaths', sims, ax, calib=True, label='Cumulative deaths', ylabel='Deaths')


    #%% Figure 1, panels 4A and 4B

    agehists = []

    for s,sim in enumerate(sims):
        agehist = sim['analyzers'][0]
        if s == 0:
            age_data = agehist.data
        agehists.append(agehist.hists[-1])

    x = age_data['age'].values
    pos = age_data['cum_diagnoses'].values
    death = age_data['cum_deaths'].values

    # From the model
    mposlist = []
    mdeathlist = []
    for hists in agehists:
        mposlist.append(hists['diagnosed'])
        mdeathlist.append(hists['dead'])
    mposarr = np.array(mposlist)
    mdeatharr = np.array(mdeathlist)

    mpbest = pl.median(mposarr, axis=0)
    mplow  = pl.quantile(mposarr, q=low_q, axis=0)
    mphigh = pl.quantile(mposarr, q=high_q, axis=0)
    mdbest = pl.median(mdeatharr, axis=0)
    mdlow  = pl.quantile(mdeatharr, q=low_q, axis=0)
    mdhigh = pl.quantile(mdeatharr, q=high_q, axis=0)

    # Plotting
    w = 4
    off = 2
    bins = x.tolist() + [100]

    ax = pl.subplot(4,2,7)
    c1 = [0.3,0.3,0.6]
    c2 = [0.6,0.7,0.9]
    xx = x+w-off
    pl.bar(x-off,pos, width=w, label='Data', facecolor=c1)
    pl.bar(xx, mpbest, width=w, label='Model', facecolor=c2)
    for i,ix in enumerate(xx):
        pl.plot([ix,ix], [mplow[i], mphigh[i]], c='k')
    ax.set_xticks(bins[:-1])
    pl.title('Diagnosed cases by age')
    pl.xlabel('Age')
    pl.ylabel('Cases')
    pl.legend()

    ax = pl.subplot(4,2,8)
    c1 = [0.5,0.0,0.0]
    c2 = [0.9,0.4,0.3]
    pl.bar(x-off,death, width=w, label='Data', facecolor=c1)
    pl.bar(x+w-off, mdbest, width=w, label='Model', facecolor=c2)
    for i,ix in enumerate(xx):
        pl.plot([ix,ix], [mdlow[i], mdhigh[i]], c='k')
    ax.set_xticks(bins[:-1])
    pl.title('Deaths by age')
    pl.xlabel('Age')
    pl.ylabel('Deaths')
    pl.legend()

    # Tidy up
    if do_save:
        cv.savefig(fig1_path)


    # Figure 2: Projections
    pl.figure(**fig_args)
    pl.subplots_adjust(**axis_args)
    pl.figtext(0.42, 0.95, 'Model estimates', fontsize=30)

    #%% Figure 2, panel 1
    ax = pl.subplot(4,1,1)
    format_ax(ax, sim)
    plotter('cum_infections', sims, ax,calib=True, label='Cumulative infections', ylabel='People')
    plotter('cum_recoveries', sims, ax,calib=True, label='Cumulative recoveries', ylabel='People')

    #%% Figure 2, panel 2
    ax = pl.subplot(4,1,2)
    format_ax(ax, sim)
    plotter('n_infectious', sims, ax,calib=True, label='Number of active infections', ylabel='People')
    plot_intervs(sim, labels=True)

    #%% Figure 2, panel 3
    ax = pl.subplot(4,1,3)
    format_ax(ax, sim)
    plotter('new_infections', sims, ax,calib=True, label='Infections per day', ylabel='People')
    plotter('new_recoveries', sims, ax,calib=True, label='Recoveries per day', ylabel='People')
    plot_intervs(sim)

    #%% Figure 2, panels 4
    ax = pl.subplot(4,1,4)
    format_ax(ax, sim)
    plotter('r_eff', sims, ax, calib=True, label='Effective reproductive number', ylabel=r'$R_{eff}$')

    ylims = [0,4]
    pl.ylim(ylims)
    xlims = pl.xlim()
    pl.plot(xlims, [1, 1], 'k')
    plot_intervs(sim)

    # Tidy up
    if do_save:
        cv.savefig(fig2_path)

    return
Esempio n. 47
0
def buildWXLINKGraphSolarPower(password, myGraphSampleCount):
    print('buildWXLINKGraphSolarPower - The time is: %s' %
          datetime.now(timezone('US/Pacific')))

    # open database
    con1 = mdb.connect('localhost', 'root', password, 'DataLogger')
    # now we have to get the data, stuff it in the graph

    mycursor = con1.cursor()

    print myGraphSampleCount
    query = '(SELECT timestamp, deviceid, Outdoor_Temperature, OutDoor_Humidity, Battery_Voltage, Battery_Current, Solar_Panel_Voltage, Solar_Panel_Current,  Load_Current, id FROM ' + WXLINKtableName + ' ORDER BY id DESC LIMIT ' + str(
        myGraphSampleCount) + ') ORDER BY id ASC'

    print "query=", query
    try:
        mycursor.execute(query)
        result = mycursor.fetchall()
    except:
        e = sys.exc_info()[0]
        print "Error: %s" % e

    t = []  # time
    u = []  # Battery_Voltage
    v = []  # Battery_Current
    x = []  # Solar_Panel_Voltage
    y = []  # Solar_Panel_Current
    z = []  # Load_Current

    sp = []  # Solar Power
    bp = []  # Battery Power
    lp = []  # Load Power

    averagePowerIn = 0.0
    averagePowerOut = 0.0
    currentCount = 0

    for record in result:
        t.append(record[0])
        u.append(record[4])
        v.append(record[5])
        x.append(record[6])
        y.append(record[7])
        z.append(record[8])

        sp.append(record[6] * record[7])
        bp.append(record[4] * record[5])
        lp.append(5.0 * record[8])  # assume 5V nominal output

    print("count of t=", len(t))

    lastSampleTime = t[-1]

    fds = dates.date2num(t)  # converted
    # matplotlib date format object
    hfmt = dates.DateFormatter('%H:%M:%S')
    #hfmt = dates.DateFormatter('%m/%d-%H')

    fig = pyplot.figure()
    fig.set_facecolor('white')
    ax = fig.add_subplot(111, axisbg='white')
    ax.vlines(fds, -200.0, 1000.0, colors='w')

    ax2 = fig.add_subplot(111, axisbg='white')

    ax.xaxis.set_major_formatter(hfmt)
    pyplot.xticks(rotation='45')
    pyplot.subplots_adjust(bottom=.3)
    pylab.plot(t,
               sp,
               color='green',
               label="Solar Power (mW) ",
               linestyle="-",
               marker=".")
    pylab.plot(t,
               bp,
               color='red',
               label="Battery Power (mW) ",
               linestyle="-",
               marker=".")
    pylab.plot(t,
               lp,
               color='black',
               label="Load Power (mW) ",
               linestyle="-",
               marker=".")
    pylab.xlabel("Time")
    pylab.ylabel("milli Watts (mW)")
    pylab.legend(loc='upper left', fontsize='x-small')
    fullpower = []
    fullpower.extend(sp)
    fullpower.extend(bp)
    fullpower.extend(lp)
    pylab.axis([min(t), max(t), min(fullpower) - 100, max(fullpower) + 100])

    pylab.figtext(.5,
                  .01,
                  ("System Power Performance WXLink\n%s\nLast Sample: %s") %
                  (datetime.now(timezone('US/Pacific')), lastSampleTime),
                  fontsize=18,
                  ha='center')
    pylab.grid(True)

    pyplot.show()
    pyplot.savefig("/var/www/html/WXLINKDataLoggerGraphPower.png",
                   facecolor=fig.get_facecolor())

    mycursor.close()
    con1.close()

    fig.clf()
    pyplot.close()
    pylab.close()
    gc.collect()
    print "------WXLINKGraphPower finished now"
Esempio n. 48
0
    def plot(self, filename=None):
        """ Plot the Slater-Koster table with matplotlib. 
        
        parameters:
        ===========
        filename:     for graphics file
        """
        try:
            import pylab as pl
        except:
            raise AssertionError('pylab could not be imported')
        fig = pl.figure()
        fig.subplots_adjust(hspace=0.0001, wspace=0.0001)
        mx = max(1, self.tables[0].max())
        if self.nel == 2:
            mx = max(mx, self.tables[1].max())
        for i in range(10):
            name = integrals[i]
            ax = pl.subplot(5, 2, i + 1)
            for p, (e1, e2) in enumerate(self.pairs):
                s1, s2 = e1.get_symbol(), e2.get_symbol()
                if p == 0:
                    s = '-'
                    lw = 1
                    alpha = 1.0
                else:
                    s = '--'
                    lw = 4
                    alpha = 0.2
                if np.all(abs(self.tables[p][:, i]) < 1E-10):
                    ax.text(0.03,
                            0.02 + p * 0.15,
                            'No %s integrals for <%s|%s>' % (name, s1, s2),
                            transform=ax.transAxes,
                            size=10)
                    if not ax.is_last_row():
                        pl.xticks([], [])
                    if not ax.is_first_col():
                        pl.yticks([], [])

                else:
                    pl.plot(self.Rgrid,
                            self.tables[p][:, i],
                            c='r',
                            ls=s,
                            lw=lw,
                            alpha=alpha)
                    pl.plot(self.Rgrid,
                            self.tables[p][:, i + 10],
                            c='b',
                            ls=s,
                            lw=lw,
                            alpha=alpha)
                    pl.axhline(0, c='k', ls='--')
                    pl.title(name, position=(0.9, 0.8))
                    if ax.is_last_row():
                        pl.xlabel('r (Bohr)')
                    else:
                        pl.xticks([], [])
                    if not ax.is_first_col():
                        pl.yticks([], [])
                    pl.ylim(-mx, mx)
                    pl.xlim(0)

        pl.figtext(0.3, 0.95, 'H', color='r', size=20)
        pl.figtext(0.34, 0.95, 'S', color='b', size=20)
        pl.figtext(0.38, 0.95, ' Slater-Koster tables', size=20)
        e1, e2 = self.ela.get_symbol(), self.elb.get_symbol()
        pl.figtext(0.3,
                   0.92,
                   '(thin solid: <%s|%s>, wide dashed: <%s|%s>)' %
                   (e1, e2, e2, e1),
                   size=10)

        file = '%s_%s_slako.pdf' % (e1, e2)
        if filename != None:
            file = filename
        pl.savefig(file)
def  PowerVoltageGraph(source,days,delay):


	
	print("PowerVoltageGraph source:%s days:%s delay:%i" % (source,days,delay))
	print("sleeping :",delay)
	time.sleep(delay)
	print("PowerVoltageGraph running now")
	
        # blink GPIO LED when it's run
	GPIO.setup(18, GPIO.OUT)
        GPIO.output(18, True)
        time.sleep(0.2)
        GPIO.output(18, False)

	# now we have get the data, stuff it in the graph 

	try:
		print("trying database")
    		db = mdb.connect('localhost', 'root', config.MySQL_Password, 'ProjectCuracao2');

    		cursor = db.cursor()

		query = "SELECT TimeStamp, solarVoltage, batteryVoltage, loadVoltage FROM PowerSystem where  now() - interval %i hour < TimeStamp" % (days*24)
		cursor.execute(query)
		result = cursor.fetchall()

		t = []
		s = []
		u = []
		v = []
		#x = []
		
		for record in result:
  			t.append(record[0])
  			s.append(record[1])
  			u.append(record[2])
  			v.append(record[3])
  			#x.append(record[4])
		fig = pyplot.figure()

		print ("count of t=",len(t))
		#print (t)
		if (len(t) == 0):
			return
		#dts = map(datetime.datetime.fromtimestamp, t)
		#print dts
		#fds = dates.date2num(t) # converted
		# matplotlib date format object
		hfmt = dates.DateFormatter('%m/%d-%H')

		fig = pyplot.figure()
		fig.set_facecolor('white')
		ax = fig.add_subplot(111,axisbg = 'white')
		#ax.vlines(fds, -200.0, 1000.0,colors='w')

		ax.xaxis.set_major_locator(dates.HourLocator(interval=6))
		ax.xaxis.set_major_formatter(hfmt)
		ax.set_ylim(bottom = -200.0)
		pyplot.xticks(rotation='vertical')
		pyplot.subplots_adjust(bottom=.3)
		pylab.plot(t, s, color='b',label="Solar",linestyle="-",marker=".")
		pylab.plot(t, u, color='r',label="Battery",linestyle="-",marker=".")
		pylab.plot(t, v, color='g',label="Load",linestyle="-",marker=".")
		#pylab.plot(t, x, color='m',label="Power Eff",linestyle="-",marker=".")
		pylab.xlabel("Hours")
		pylab.ylabel("Voltage V")
		pylab.legend(loc='upper left')

		if (max(u) > max(s)):
			myMax = max(u)+ 100.0
		else:
			myMax = max(s)
		pylab.axis([min(t), max(t), min(u), myMax])
		pylab.figtext(.5, .05, ("ProjectCuracao2 Power Voltage Last %i Days" % days),fontsize=18,ha='center')
		pyplot.setp( ax.xaxis.get_majorticklabels(), rotation=70)

		pylab.grid(True)

		pyplot.show()
		try:
			pyplot.savefig("/home/pi/RasPiConnectServer/static/PowerVoltageGraph.png",facecolor=fig.get_facecolor())	
		except:
			pyplot.savefig("/home/pi/SDL_Pi_ProjectCuracao2/static/PowerVoltageGraph.png",facecolor=fig.get_facecolor())	


	except mdb.Error, e:
  
    		print "Error %d: %s" % (e.args[0],e.args[1])
    data_normalized_projected3 = pca3.inverse_transform(data_normalized_pca3)
    data_normalized_projected4 = pca4.inverse_transform(data_normalized_pca4)
    loss3 = ((data_normalized3 - data_normalized_projected3)**2).mean()
    loss4 = ((data_normalized4 - data_normalized_projected4)**2).mean()
#    print(str(i) + ": 3mer, " + str(loss3))
#    print(str(i) + ": 5mer, " + str(loss5))
    pylab.scatter(i, loss3, color = 'b')
    pylab.scatter(i, loss4, color = 'g')


#Figure text
axes = pylab.gca()
axes.set_ylim([0,0.00000018])

pylab.gca().set_position((.1, .4, .8, .6))
pylab.figtext(0.02, .24, 'This graph plots the mean squared loss with increasing number of PCA components.')
pylab.figtext(0.02, .2, 'Maximum number of components: {}'.format(no_components))
pylab.figtext(0.02, .16, '314 hmp files evaluated for each kmer size')


#####2 component
data_normalized3 = normalize(data3, axis = 1, norm = 'l1')
data_normalized4 = normalize(data4, axis = 1, norm = 'l1')

pca3 = PCA(n_components=2)
pca3.fit(data_normalized3)
PCA(copy=True, iterated_power = 'auto', n_components=2, random_state=None,
    svd_solver = 'auto', tol=0.0, whiten = False)
data_new3 = pca3.transform(data_normalized3)

pca4 = PCA(n_components=2)
def plot_uvj_vs_icd():
    galaxies = pickle.load(open('galaxies.pickle', 'rb'))
    galaxies = filter(lambda galaxy: galaxy.ICD_IH != None, galaxies)
    galaxies = filter(lambda galaxy: galaxy.sersic != None and \
            galaxy.ston_I > 10, galaxies)

    #Upper and Lower limit arrow verts
    arrowup_verts = [[0., 0.], [-1., -1], [0., 0.], [0., -2.], [0., 0.],
                     [1, -1]]
    #arrowdown_verts = [[0.,0.], [-1., 1], [0.,0.],
    #    [0.,2.], [0.,0.], [1, 1]]

    F = pyl.figure(1, figsize=(8, 3))
    grid = AxesGrid(F,
                    111,
                    nrows_ncols=(1, 4),
                    axes_pad=0.1,
                    add_all=True,
                    aspect=False,
                    share_all=False)

    ax1 = grid[0]
    ax2 = grid[1]
    ax3 = grid[2]
    ax4 = grid[3]

    for galaxy in galaxies:
        if galaxy.sersic < 1.:
            if galaxy.ICD_IH * 100 < 50:
                col1 = ax1.scatter(galaxy.Mass,
                                   galaxy.ICD_IH * 100.,
                                   s=25,
                                   c='0.8',
                                   edgecolor='0.8')
            else:
                col1 = ax1.scatter(galaxy.Mass,
                                   50,
                                   marker=None,
                                   s=100,
                                   verts=arrowup_verts)
        if 1. < galaxy.sersic < 2.:
            if galaxy.ICD_IH * 100 < 50:
                col2 = ax2.scatter(galaxy.Mass,
                                   galaxy.ICD_IH * 100.,
                                   s=25,
                                   c='0.8',
                                   edgecolor='0.8')
            else:
                col2 = ax2.scatter(galaxy.Mass,
                                   50,
                                   marker=None,
                                   s=100,
                                   verts=arrowup_verts)
        if 2. < galaxy.sersic < 3.:
            if galaxy.ICD_IH * 100 < 50:
                col3 = ax3.scatter(galaxy.Mass,
                                   galaxy.ICD_IH * 100.,
                                   s=25,
                                   c='0.8',
                                   edgecolor='0.8')
            else:
                col3 = ax3.scatter(galaxy.Mass,
                                   50,
                                   marker=None,
                                   s=100,
                                   verts=arrowup_verts)
        if 3. < galaxy.sersic:
            if galaxy.ICD_IH * 100 < 50:
                col4 = ax4.scatter(galaxy.Mass,
                                   galaxy.ICD_IH * 100.,
                                   s=25,
                                   c='0.8',
                                   edgecolor='0.8')
            else:
                col4 = ax4.scatter(galaxy.Mass,
                                   50,
                                   marker=None,
                                   s=100,
                                   verts=arrowup_verts)

    # Add the box and whiskers
    galaxies1 = filter(lambda galaxy: galaxy.ston_I > 10. and \
        galaxy.sersic < 1, galaxies)
    galaxies1 = pyl.asarray(galaxies1)
    galaxies2 = filter(lambda galaxy: galaxy.ston_I > 10. and \
        1 < galaxy.sersic < 2, galaxies)
    galaxies2 = pyl.asarray(galaxies2)
    galaxies3 = filter(lambda galaxy: galaxy.ston_I > 10. and \
        2 < galaxy.sersic < 3, galaxies)
    galaxies3 = pyl.asarray(galaxies3)
    galaxies4 = filter(lambda galaxy: galaxy.ston_I > 10. and \
        3 < galaxy.sersic, galaxies)
    galaxies4 = pyl.asarray(galaxies4)

    x1 = [galaxy.Mass for galaxy in galaxies1]
    x2 = [galaxy.Mass for galaxy in galaxies2]
    x3 = [galaxy.Mass for galaxy in galaxies3]
    x4 = [galaxy.Mass for galaxy in galaxies4]

    grid1 = []
    grid2 = []
    grid3 = []
    grid4 = []

    from boxplot_percentile_width import percentile_box_plot as pbp

    bins_x = pyl.array([8.5, 9., 9.5, 10., 11])
    for i in range(bins_x.size - 1):
        xmin = bins_x[i]
        xmax = bins_x[i + 1]
        cond = [cond1 and cond2 for cond1, cond2 in zip(x1 >= xmin, x1 < xmax)]
        grid1.append(galaxies1.compress(cond))

    icd1 = []
    for i in range(len(grid1)):
        icd1.append([galaxy.ICD_IH * 100 for galaxy in grid1[i]])
    width = pyl.diff(bins_x)
    index = pyl.delete(bins_x, -1) + 0.25
    index[-1] = index[-1] + 0.25
    bp1 = pbp(ax1, icd1, indexer=list(index), width=width)

    bins_x = pyl.array([8.5, 9., 9.5, 10., 10.5, 11.5])
    for i in range(bins_x.size - 1):
        xmin = bins_x[i]
        xmax = bins_x[i + 1]
        cond = [cond1 and cond2 for cond1, cond2 in zip(x2 >= xmin, x2 < xmax)]
        grid2.append(galaxies2.compress(cond))
    icd2 = []
    for i in range(len(grid2)):
        icd2.append([galaxy.ICD_IH * 100 for galaxy in grid2[i]])
    width = pyl.diff(bins_x)
    index = pyl.delete(bins_x, -1) + 0.25
    index[-1] = index[-1] + 0.25
    bp2 = pbp(ax2, icd2, indexer=list(index), width=width)

    bins_x = pyl.array([8.5, 9.5, 10., 11.])
    for i in range(bins_x.size - 1):
        xmin = bins_x[i]
        xmax = bins_x[i + 1]
        cond = [cond1 and cond2 for cond1, cond2 in zip(x3 >= xmin, x3 < xmax)]
        grid3.append(galaxies3.compress(cond))
    icd3 = []
    for i in range(len(grid3)):
        icd3.append([galaxy.ICD_IH * 100 for galaxy in grid3[i]])
    width = pyl.diff(bins_x)
    index = pyl.delete(bins_x, -1) + 0.25
    index[-1] = index[-1] + 0.25
    index[0] = index[0] + 0.25
    bp3 = pbp(ax3, icd3, indexer=list(index), width=width)

    bins_x = pyl.array([8.5, 9., 9.5, 10., 10.5, 11., 12.])
    for i in range(bins_x.size - 1):
        xmin = bins_x[i]
        xmax = bins_x[i + 1]
        cond = [cond1 and cond2 for cond1, cond2 in zip(x4 >= xmin, x4 < xmax)]
        grid4.append(galaxies4.compress(cond))
    icd4 = []
    for i in range(len(grid4)):
        icd4.append([galaxy.ICD_IH * 100 for galaxy in grid4[i]])
    width = pyl.diff(bins_x)
    index = pyl.delete(bins_x, -1) + 0.25
    index[-1] = index[-1] + 0.25
    print 'ajsdf'
    bp4 = pbp(ax4, icd4, indexer=list(index), width=width)

    ax1.set_xticks([8, 9, 10, 11])
    ax2.set_xticks([8, 9, 10, 11, 12])
    ax3.set_xticks([8, 9, 10, 11])
    ax4.set_xticks([8, 9, 10, 11, 12])

    ax1.set_ylim(0, 50)
    ax2.set_ylim(0, 50)
    ax3.set_ylim(0, 50)
    ax4.set_ylim(0, 50)

    ax1.set_xlim(8, 12.5)
    ax2.set_xlim(8, 12.5)
    ax3.set_xlim(8, 12.5)
    ax4.set_xlim(8, 12.5)

    ax1.set_ylabel(r'$\xi[i_{775},H_{160}]$ (%)')
    ax1.set_title('n < 1')
    ax2.set_title('1 < n < 2')
    ax3.set_title('2 < n < 3')
    ax4.set_title('3 < n')

    pyl.figtext(.5,
                .05,
                r'Log Mass $(M_{\odot})$',
                fontsize=18,
                horizontalalignment='center')
    ax1.axhline(0, lw=2, zorder=0)
    ax2.axhline(0, lw=2, zorder=0)
    ax3.axhline(0, lw=2, zorder=0)
    ax4.axhline(0, lw=2, zorder=0)

    import matplotlib.font_manager
    line1 = pyl.Line2D([], [],
                       marker='o',
                       mfc='0.8',
                       mec='0.8',
                       markersize=8,
                       linewidth=0)
    line2 = pyl.Line2D([], [],
                       marker='s',
                       mec='#348ABD',
                       mfc='None',
                       markersize=10,
                       linewidth=0,
                       markeredgewidth=2)
    line3 = pyl.Line2D([], [], color='#A60628', linewidth=2)
    prop = matplotlib.font_manager.FontProperties(size='small')
    ax3.legend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'),
               loc='upper center',
               prop=prop,
               ncol=1)

    pyl.tight_layout()
    pyl.subplots_adjust(bottom=0.21, left=0.11)
    pyl.show()
Esempio n. 52
0
for i,tmp in enumerate(xs):
    pl.subplot(520+3+i*2)
    pl.plot(tmp, label="分段%s" % (i+1))
    pl.gca().set_yticklabels([])
    pl.gca().set_xticklabels([])
    pl.legend()
    pl.subplot(520+3+i*2+1)
    tmp = np.convolve(tmp, h)
    result.append(tmp)
    pl.plot(tmp, label="分段卷积%s" % (i+1))
    pl.gca().set_yticklabels([])
    pl.gca().set_xticklabels([])  
    pl.axvspan(i*100,i*100+200,alpha=0.3,facecolor="g")
    pl.legend()

pl.subplot(529)
pl.plot(np.convolve(x,h), label="原始信号卷积")
pl.gca().set_yticklabels([])
pl.gca().set_xticklabels([])  
pl.legend()

pl.subplot(5,2,10)
pl.plot(np.sum(result, axis=0), label="分段卷积和")
pl.gca().set_yticklabels([])
pl.gca().set_xticklabels([]) 
pl.legend()

pl.subplots_adjust(hspace=0.05, wspace=0.03, top=0.95, bottom=0.01,left=0.03,right=0.97)
pl.figtext(0.5, 0.965,  "分段卷积演示",
           ha='center', color='black', weight='bold', size='large')
pl.show()
Esempio n. 53
0
def RMIrichness(ra=None,
                dec=None,
                photoz=None,
                cat=None,
                plot=True,
                err=True,
                rw=True,
                bcg=True):
    fra = cat.field('ra')
    fdec = cat.field('dec')
    imag = cat.field('imag')
    rmi = cat.field('model_mag')[:, 2] - cat.field('model_mag')[:, 3]
    rmierr = np.sqrt(
        cat.field('model_magerr')[:, 2]**2 +
        cat.field('model_magerr')[:, 3]**2)
    depth = 12
    h = es.htm.HTM(depth)
    srad = np.rad2deg(1. / Da(0, photoz))
    m1, m2, d12 = h.match(ra, dec, fra, fdec, srad, maxmatch=50000)
    r12 = np.deg2rad(d12) * Da(0, photoz)
    cimag = imag[m2[0]]
    crmi = rmi[m2[0]]
    if bcg is True:
        indices = (imag[m2] <= limi0_2(photoz)) * (imag[m2] > cimag)
    else:
        indices = (imag[m2] <= limi0_2(photoz))
    ntot = len(m2[indices])
    if ntot <= 10:
        return 'not enough galaxy brighter than 0.2 L*'
    alpha = np.array([0.5, 0.5])
    mu = np.array([
        sts.scoreatpercentile(rmi[m2[indices]], per=70),
        sts.scoreatpercentile(rmi[m2[indices]], per=40)
    ])
    sigma = np.array([0.04, 0.3])
    if err is True:
        if rw is False:
            aic2 = gmm.aic_ecgmm(rmi[m2[indices]], rmierr[m2[indices]], alpha,
                                 mu, sigma)
            aic1 = gmm.wstat(rmi[m2[indices]], rmierr[m2[indices]])[3]
        else:
            aic2, alpha, mu, sigma = rwgmm.aic2EM(rmi[m2[indices]],
                                                  rmierr[m2[indices]],
                                                  r12[indices], alpha, mu,
                                                  sigma)
            aic1 = rwgmm.aic1EM(rmi[m2[indices]], rmierr[m2[indices]],
                                r12[indices])[0]
    else:
        aic2 = gmm.aic_ecgmm(rmi[m2[indices]],
                             aalpha=alpha,
                             mmu=mu,
                             ssigma=sigma)
        aic1 = gmm.wstat(rmi[m2[indices]])[3]
    srt = np.argsort(sigma)
    alpha = alpha[srt]
    mu = mu[srt]
    sigma = sigma[srt]
    z = rmiz(mu[0])
    if plot == True:
        pl.figure(figsize=(12, 6))
        pl.subplot(1, 2, 1)
        hh = pl.hist(rmi[m2[indices]],
                     bins=50,
                     normed=True,
                     facecolor='green',
                     alpha=0.3,
                     range=[-1, 3])
        pl.vlines(crmi,
                  0,
                  hh[0].max() + 0.5,
                  color='red',
                  lw=2,
                  linestyle='dashed')
        pl.grid()
        x = np.arange(-1, 3, 0.01)
        t = gmm.ecgmmplot(x, alpha, mu, sigma)
        richness = ntot * alpha[0]
        pl.xlabel('r - i')
        pl.figtext(0.61, 0.85, 'Relative Weights: ' + str(np.round(alpha, 4)))
        pl.figtext(0.61, 0.8, 'Mean Colors: ' + str(np.round(mu, 4)))
        pl.figtext(0.61, 0.75, 'Mean Color Widths: ' + str(np.round(sigma, 4)))
        pl.figtext(0.61, 0.68, 'Richness: ' + str(np.round(richness, 2)))
        pl.figtext(0.61, 0.61, r'$AIC_1$: ' + str(aic1))
        pl.figtext(0.61, 0.54, r'$AIC_2$: ' + str(aic2))
        pl.figtext(0.61, 0.47, 'Test Photoz: ' + str(photoz))
        pl.figtext(0.61, 0.4, 'Ridgeline Photoz: ' + str(round(z, 3)))
        pl.figtext(0.61, 0.33,
                   'R200: ' + str(round(0.09 * richness**0.798, 2)) + ' Mpc')
        pl.figtext(
            0.61, 0.25, 'M200: ' + str(round(8.8 * (richness / 19.)**1.7, 2)) +
            'x10^13 Solar Mass')
        pl.title('Total # of galaxies: ' + str(ntot))
    return richness, aic1, aic2, crmi, alpha, mu, sigma, z
Esempio n. 54
0
def gaia_model_eval(h5name=None, folder_name=None):
    """
    NAME: apogee_model_eval
    PURPOSE: To test the model and generate plots
    INPUT:
        h5name = Name of the h5 data set
        folder_name = the folder name contains the model
    OUTPUT: target and normalized data
    HISTORY:
        2017-Oct-14 Henry Leung
    """

    # prevent Tensorflow taking up all the GPU memory
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    set_session(tf.Session(config=config))

    h5name_check(h5name)

    h5test = h5name + '_test.h5'
    traindata = h5name + '_train.h5'

    currentdir = os.getcwd()
    fullfolderpath = currentdir + '/' + folder_name
    print(fullfolderpath)
    mean_and_std = np.load(fullfolderpath + '/meanstd.npy')
    spec_meanstd = np.load(fullfolderpath + '/spectra_meanstd.npy')
    modelname = '/model_{}.h5'.format(folder_name[-11:])
    model = load_model(os.path.normpath(fullfolderpath + modelname))

    mean_labels = mean_and_std[0]
    std_labels = mean_and_std[1]

    # ensure the file will be cleaned up
    with h5py.File(h5test) as F:
        test_spectra = np.array(F['spectra'])
        test_spectra -= spec_meanstd[0]
        test_spectra /= spec_meanstd[1]
        absmag = np.array(F['absmag'])

    print('Test set contains ' + str(len(test_spectra)) + ' stars')

    time1 = time.time()
    test_predictions = batch_predictions(model, test_spectra, 500, 1,
                                         std_labels, mean_labels)
    print("{0:.2f}".format(time.time() - time1) + ' seconds to make ' +
          str(len(test_spectra)) + ' predictions')

    resid = test_predictions.flatten() - absmag
    bias = np.median(resid)
    scatter = mad_std(resid)

    # Some plotting variables for asthetics
    plt.rcParams['axes.facecolor'] = 'white'
    sns.set_style("ticks")
    plt.rcParams['axes.grid'] = True
    plt.rcParams['grid.color'] = 'gray'
    plt.rcParams['grid.alpha'] = '0.4'

    x_lab = 'Gaia'
    y_lab = 'astroNN'
    plt.figure(figsize=(15, 11), dpi=200)
    plt.axhline(0, ls='--', c='k', lw=2)
    plt.scatter(absmag, resid, s=3)
    plt.xlabel('Gaia Abs Mag', fontsize=25)
    plt.ylabel('$\Delta$ Abs Mag' + '\n(' + y_lab + ' - ' + x_lab + ')',
               fontsize=25)
    plt.tick_params(labelsize=20, width=1, length=10)
    plt.xlim([np.min(absmag), np.max(absmag)])
    ranges = (np.max(absmag) - np.min(absmag)) / 2
    plt.ylim([-ranges, ranges])
    bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=2)
    plt.figtext(0.6,
                0.75,
                '$\widetilde{m}$=' + '{0:.3f}'.format(float(bias)) +
                ' $\widetilde{s}$=' +
                '{0:.3f}'.format(float(scatter / std_labels)) + ' s=' +
                '{0:.3f}'.format(float(scatter)),
                size=25,
                bbox=bbox_props)
    plt.tight_layout()
    plt.savefig(fullfolderpath + '/absmag_test.png')
    plt.close('all')
    plt.clf()

    if traindata is not None:
        with h5py.File(traindata) as F:
            train_spectra = np.array(F['spectra'])
            train_spectra -= spec_meanstd[0]
            train_spectra /= spec_meanstd[1]
            absmag = np.array(F['absmag'])

        time1 = time.time()
        test_predictions = batch_predictions(model, train_spectra, 500, 1,
                                             std_labels, mean_labels)
        print("{0:.2f}".format(time.time() - time1) + ' seconds to make ' +
              str(len(train_spectra)) + ' predictions')

        resid = test_predictions.flatten() - absmag
        bias = np.median(resid)
        scatter = mad_std(resid)

        # Some plotting variables for asthetics
        plt.rcParams['axes.facecolor'] = 'white'
        sns.set_style("ticks")
        plt.rcParams['axes.grid'] = True
        plt.rcParams['grid.color'] = 'gray'
        plt.rcParams['grid.alpha'] = '0.4'

        x_lab = 'Gaia'
        y_lab = 'astroNN'
        plt.figure(figsize=(15, 11), dpi=200)
        plt.axhline(0, ls='--', c='k', lw=2)
        plt.scatter(absmag, resid, s=3)
        plt.xlabel('Gaia Abs Mag', fontsize=25)
        plt.ylabel('$\Delta$ Abs Mag' + '\n(' + y_lab + ' - ' + x_lab + ')',
                   fontsize=25)
        plt.tick_params(labelsize=20, width=1, length=10)
        plt.xlim([np.min(absmag), np.max(absmag)])
        ranges = (np.max(absmag) - np.min(absmag)) / 2
        plt.ylim([-ranges, ranges])
        bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=2)
        plt.figtext(0.6,
                    0.75,
                    '$\widetilde{m}$=' + '{0:.3f}'.format(float(bias)) +
                    ' $\widetilde{s}$=' +
                    '{0:.3f}'.format(float(scatter / std_labels)) + ' s=' +
                    '{0:.3f}'.format(float(scatter)),
                    size=25,
                    bbox=bbox_props)
        plt.tight_layout()
        plt.savefig(fullfolderpath + '/absmag_train.png')
        plt.close('all')
        plt.clf()

    return None
Esempio n. 55
0
 def multilabels(self, xtext, ytext, title=None):
     plt.subplots_adjust(bottom=0.2)
     plt.figtext(0.5, 0.07, xtext, ha='center')
     plt.figtext(0.05, 0.5, ytext, rotation='vertical', va='center')
     if title is not None: plt.suptitle(title)
Esempio n. 56
0
def fitDecay(colourFilter, metadata, channame='', i=0):
    #get frames in which events occured and convert into seconds
    t = colourFilter['t'].astype('f') * metadata.getEntry('Camera.CycleTime')

    n, bins = np.histogram(t, 100)

    b1 = bins[:-1]
    Nm = n.max()

    res = FitModel(e2mod, [Nm * 2, 15, -3, Nm * 3, n[1] / 1], n[1:], b1[1:],
                   Nm)
    #mse = (res[2]['fvec']**2).mean()
    #ch2 = chi2(res, n[1:])
    #print ch2
    ch2, mse = chi2_mse(e2mod, n[1:], res, b1[1:], Nm)

    PL.AddRecord('/Photophysics/Decay/e2mod',
                 munge_res(e2mod, res, mse=mse, ch2=ch2))

    res2 = FitModelPoisson(emod, [Nm * 2, 15, -3, Nm * 3, n[1] / 2], n[1:],
                           b1[1:], Nm)  #[0]
    ch2, mse = chi2_mse(hmod, n[1:], res2, b1[1:], Nm)

    PL.AddRecord('/Photophysics/Decay/emod',
                 munge_res(emod, res2, mse=mse, ch2=ch2))

    res3 = FitModelPoisson(hmod, [Nm * 2, 15, -3, Nm * 3, n[1] / 2], n[1:],
                           b1[1:], Nm)  #[0]
    ch2, mse = chi2_mse(hmod, n[1:], res3, b1[1:], Nm)

    PL.AddRecord('/Photophysics/Decay/hmod',
                 munge_res(hmod, res3, mse=mse, ch2=ch2))

    r4 = FitModelPoisson(e2mod, [Nm * 2, 15, -3, Nm * 3, n[1] / 2], n[1:],
                         b1[1:], Nm)
    ch2, mse = chi2_mse(e2mod, n[1:], r4, b1[1:], Nm)
    PL.AddRecord('/Photophysics/Decay/e2mod_p',
                 munge_res(e2mod, r4, mse=mse, ch2=ch2))

    if USE_GUI:
        pylab.bar(b1 / 60,
                  n,
                  width=(b1[1] - b1[0]) / 60,
                  alpha=0.4,
                  fc=colours[i])
        pylab.plot(b1 / 60, e2mod(res[0], b1, Nm), colours[i], lw=3)
        pylab.plot(b1 / 60, emod(res2[0], b1, Nm), colours[i], lw=2, ls='--')
        pylab.plot(b1 / 60, hmod(res3[0], b1, Nm), colours[i], lw=2, ls=':')
        pylab.plot(b1 / 60, e2mod(r4[0], b1, Nm), colours[i], lw=1)
        pylab.ylim(0, 1.2 * n.max())
        pylab.ylabel('Events')
        pylab.xlabel('Acquisition Time [mins]')
        pylab.title('Event Rate')

        b = 0.5 * (1 + erf(res[0][2])) * Nm

        pylab.figtext(.4,
                      .8 - .05 * i,
                      channame + '\t$\\tau = %3.2fs,\\;b = %3.2f$' %
                      (res[0][1], b / res[0][0]),
                      size=18,
                      color=colours[i])

    return 0
    '_bias-', bias
) + '{}{}'.format('_numEpochs-', numEpochs) + '{}{}'.format(
    '_batch-', batchSize
) + '_encActFunc-' + encoded_activation + '_decActFunc-' + decoded_activation + '_lossFunc-' + loss + '_backend-' + backend

# how does the loss change as the fraction of data in the test vs training change?
pylab.figure()
for hs in allHistories:
    pylab.plot(hs.history['val_loss'])
pylab.title('model loss by epochs and number of training samples')
pylab.ylabel('test loss')
pylab.xlabel('epoch')
pylab.legend(['300', '600', '900', '1200', '1500'], loc='upper left')
pylab.gca().set_position((.1, .6, .8, .6))
pylab.figtext(
    0.02, .4,
    'This graph shows how loss changes with number of epochs for different splits between training and test data.'
)
pylab.figtext(0.02, .32, 'Backend: ' + backend)
pylab.figtext(0.02, .28, 'Loss function: ' + loss)
pylab.figtext(0.02, .24,
              'Number of encoding dimensions: {}'.format(encoding_dim))
pylab.figtext(0.02, .2, 'Use bias: {}'.format(bias))
pylab.figtext(0.02, .16, 'Number of epochs of training: {}'.format(numEpochs))
pylab.figtext(0.02, .12,
              'Batch size used during training: {}'.format(batchSize))
pylab.figtext(0.02, .08,
              'Activation function used for encoding: ' + encoded_activation)
pylab.figtext(0.02, .04,
              'Activation function used for decoding: ' + decoded_activation)
pylab.savefig(
    os.path.expanduser(graph_dir + '/epoch_vs_loss_varied_training_samples' +
Esempio n. 58
0
def fitFluorBrightnessT(colourFilter, metadata, channame='', i=0, rng=None):
    #nPh = (colourFilter['A']*2*math.pi*(colourFilter['sig']/(1e3*metadata.getEntry('voxelsize.x')))**2)
    #nPh = nPh*metadata.getEntry('Camera.ElectronsPerCount')/metadata.getEntry('Camera.TrueEMGain')
    #from mpl_toolkits.mplot3d import Axes3D

    nPh = getPhotonNums(colourFilter, metadata)
    t = (colourFilter['t'].astype('f') - metadata['Protocol.DataStartsAt']
         ) * metadata.getEntry('Camera.CycleTime')
    NEvents = len(t)

    if rng is None:
        rng = nPh.mean() * 3

    Nco = nPh.min()

    n, xbins, ybins = np.histogram2d(
        nPh, t, [np.linspace(0, rng, 50),
                 np.linspace(0, t.max(), 20)])
    bins = xbins[:-1]

    xb = xbins[:-1][:, None] * np.ones([1, ybins.size - 1])
    yb = ybins[:-1][None, :] * np.ones([xbins.size - 1, 1])

    res0 = FitModel(
        fITmod2,
        [n.max() * 3, 1, np.median(nPh), 20, 1e2, 1e2, 100], n, xb, yb, Nco)
    print((res0[0]))

    PL.AddRecord('/Photophysics/FluorBrightness/fITmod2',
                 munge_res(fITmod2, res0))

    A, Ndet, lamb, tauI, a, Acrit, bg = res0[0]
    #Ndet = Ndet**2
    #NDetM = NDetM**2
    #Acrit = Acrit**2
    #a = (1+erf(a))/2

    Ndet = np.sqrt(Ndet**2 + 1) - 1  #+ve
    bg = np.sqrt(bg**2 + 1) - 1
    Acrit = np.sqrt(Acrit**2 + 1) - 1
    a = (1 + erf(a)) / 2  # [0,1]
    #bg = sqrt(bg**2 + 1) - 1
    #k = sqrt(k**2 + 1) - 1

    NDetM = bg

    rr = fITmod2(res0[0], xb, yb, Nco)
    if USE_GUI:

        pylab.figure()
        pylab.subplot(131)
        pylab.imshow(n, interpolation='nearest')
        pylab.colorbar()

        pylab.subplot(132)
        pylab.imshow(rr, interpolation='nearest')
        pylab.colorbar()

        pylab.subplot(133)
        pylab.imshow(n - rr, interpolation='nearest')
        pylab.colorbar()

        pylab.title(channame)

        pylab.figure()

        t_ = np.linspace(t[0], t[-1], 100)

        #sc = (lamb/(ybins[1] - ybins[0]))
        #sc = len(ybins)
        sc = 1. / (1 - np.exp(-(ybins[1] - ybins[0]) / lamb))
        print(('sc = ', sc))
        y1 = sc * A / ((t_ / tauI)**a + 1)
        pylab.plot(t_, y1)
        pylab.plot(t_, sc * (Ndet / ((t_ / tauI)**a + 1) + NDetM))

        pylab.bar(ybins[:-1], n.sum(0), width=ybins[1] - ybins[0], alpha=0.5)
        pylab.plot(ybins[:-1], rr.sum(0), lw=2)

        pylab.title(channame)
        pylab.xlabel('Time [s]')

        pylab.figtext(
            .2,
            .7,
            '$A = %3.0f\\;N_{det} = %3.2f\\;\\lambda = %3.0f\\;\\tau = %3.0f$\n$\\alpha = %3.3f\\;A_{crit} = %3.2f\\;N_{det_0} = %3.2f$'
            % (A, Ndet, lamb, tauI, a, Acrit, NDetM),
            size=18)

    return [channame, lamb, NEvents]
Esempio n. 59
0
def dmmProcessData(data, mRange):
    # Display graph if we received data with plot
    if len(data) == 361:

        # Scope screen has 320 i.e. 40pix/div points on x axis
        x = numpy.linspace(0, 8 * timebase[data[13]][0], 320)

        # Scope screen has 128 (-64:64) i.e. 16pix/div points on y axis [x/myInt for x in myList]
        const = float(1) / 64 * mRange[0] * 4
        point = data[12] if data[12] < 127 else -(255 - data[12])
        offset = float(point) * const
        offsety = [0 for point in data[40:360]]
        iy = [
            point if point < 127 else -(255 - point) for point in data[40:360]
        ]
        y = [(float(point) * const) for point in iy]

        # Trigger time -76:76 (-160:160)
        trigx = numpy.linspace(-64 * const - offset, 64 * const - offset, 128)
        tpointx = data[14] if data[14] < 127 else -(255 - data[14])
        tpointx = (4 * timebase[data[13]][0]) + (tpointx * 8 *
                                                 timebase[data[13]][0] / 160)
        trigxx = [tpointx for point in trigx]

        # Trigger level -60:60 (-64:64)
        tpointy = data[16] if data[16] < 127 else -(255 - data[16])
        tpointy = float(tpointy) * const - offset
        trigy = [tpointy for point in data[40:360]]

        if options.cont_cap:
            logging.info(
                'MeasuredData %s TotalTime: %d %s TotalRange: %d .. %d %s TrigTime: %.2f %s TrigValue: %.2f %s BaseOffset: %.2f %s Min: %.2f %s Max: %.2f %s',
                y, 8 * timebase[data[13]][0], timebase[data[13]][1],
                -mRange[0] * 4 - offset, mRange[0] * 4 - offset, mRange[1],
                tpointx, timebase[data[13]][1], tpointy, mRange[1], offset,
                mRange[1], min(y[1:]), mRange[1], max(y[1:]), mRange[1])
        else:
            logging.info(
                'TotalTime: %d %s TotalRange: %d .. %d %s TrigTime: %.2f %s TrigValue: %.2f %s BaseOffset: %.2f %s Min: %.2f %s Max: %.2f %s',
                8 * timebase[data[13]][0], timebase[data[13]][1],
                -mRange[0] * 4 - offset, mRange[0] * 4 - offset, mRange[1],
                tpointx, timebase[data[13]][1], tpointy, mRange[1], offset,
                mRange[1], min(y[1:]), mRange[1], max(y[1:]), mRange[1])
            # http://matplotlib.org/users/pyplot_tutorial.html
            pylab.figure(1, figsize=(6, 6))

            # Plot the complete image
            pylab.plot(x, y, x, trigy, 'r--', trigxx, trigx, 'r--', x, offsety,
                       'g--')

            # Scope screen shows 8 timebase segments
            pylab.axis([
                0, 8 * timebase[data[13]][0], -mRange[0] * 4 - offset,
                mRange[0] * 4 - offset
            ])
            pylab.xlabel(timebase[data[13]][1])
            pylab.ylabel(mRange[1])
            pylab.grid(True)
            fig = pylab.gcf()
            fig.canvas.set_window_title('Scopemeter UT81B')

            if data[17] == 2:
                TrigMode = 'SHOT'
            elif data[17] == 1:
                TrigMode = 'NORM'
            else:
                TrigMode = 'AUTO'
            TrigEdge = 'Rising' if data[15] == 0 else 'Falling'
            TrigText = 'Slope: ' + TrigEdge + ' Mode: ' + TrigMode
            pylab.figtext(
                0.13, 0.91, mRange[2] + ' ' + str(mRange[0]) + mRange[1] +
                '  ' + str(timebase[data[13]][0]) + timebase[data[13]][1] +
                '  ' + TrigText)
            pylab.show()

    elif len(data) == 41:
        time.sleep(timeout)
    else:
        logging.info('Malformed data')
Esempio n. 60
0
def main():
    """
    NAME 
        core_depthplot.py

    DESCRIPTION
        plots various measurements versus core_depth or age.  plots data flagged as 'FS-SS-C' as discrete samples.  

    SYNTAX
        core_depthplot.py [command line optins]

    OPTIONS
        -h prints help message and quits
        -f FILE: specify input magic_measurments format file from magi
        -fsum FILE: specify input LIMS database (IODP) core summary csv file
        -fwig FILE: specify input depth,wiggle to plot, in magic format with sample_core_depth key for depth
        -fsa FILE: specify input er_samples format file from magic for depth
        -fa FILE: specify input er_ages format file from magic for age
              NB: must have either -fsa OR -fa (not both)
        -fsp FILE sym size: specify input zeq_specimen format file from magic, sym and size
              NB: PCAs will have specified color, while fisher means will be white with specified color as the edgecolor
        -fres FILE specify input pmag_results file from magic, sym and size
        -LP [AF,T,ARM,IRM, X] step [in mT,C,mT,mT, mass/vol] to plot 
        -S do not plot blanket treatment data (if this is set, you don't need the -LP)
        -sym SYM SIZE, symbol, size for continuous points (e.g., ro 5, bs 10, g^ 10 for red dot, blue square, green triangle), default is blue dot at 5 pt
        -D do not plot declination
        -M do not plot magnetization
        -log  plot magnetization  on a log scale
        -L do not connect dots with a line
        -I do not plot inclination
        -d min max [in m] depth range to plot
        -n normalize by weight in er_specimen table
        -Iex: plot the expected inc at lat - only available for results with lat info in file
        -ts TS amin amax: plot the GPTS for the time interval between amin and amax (numbers in Ma)
           TS: [ck95, gts04, gts12] 
        -ds [mbsf,mcd] specify depth scale, mbsf default 
        -fmt [svg, eps, pdf, png] specify output format for plot (default: svg)
        -sav save plot silently

     DEFAULTS:
         Measurements file: magic_measurements.txt
         Samples file: er_samples.txt
         NRM step
         Summary file: none
    """
    meas_file = 'magic_measurements.txt'
    intlist = [
        'measurement_magnitude', 'measurement_magn_moment',
        'measurement_magn_volume', 'measurement_magn_mass'
    ]
    samp_file = 'er_samples.txt'
    depth_scale = 'sample_core_depth'
    wt_file = ''
    verbose = pmagplotlib.verbose
    width = 10
    sym, size = 'bo', 5
    Ssym, Ssize = 'cs', 5
    method, fmt = "LT-NO", '.svg'
    step = 0
    pcol = 3
    pel = 3
    pltD, pltI, pltM, pltL, pltS = 1, 1, 1, 1, 1
    logit = 0
    maxInt = -1000
    minInt = 1e10
    maxSuc = -1000
    minSuc = 10000
    plotexp, pTS = 0, 0
    dir_path = "."
    sum_file = ""
    suc_file = ""
    age_file = ""
    spc_file = ""
    res_file = ""
    ngr_file = ""
    wig_file = ""
    title, location = "", ""
    if '-WD' in sys.argv:
        ind = sys.argv.index('-WD')
        dir_path = sys.argv[ind + 1]
    norm = 0
    if '-h' in sys.argv:
        print main.__doc__
        sys.exit()
    if '-L' in sys.argv:
        pltL = 0
    if '-S' in sys.argv: pltS = 0  # don't plot the bulk measurements at all
    if '-D' in sys.argv:
        pltD = 0
        pcol -= 1
        pel -= 1
        width -= 2
    if '-I' in sys.argv:
        pltI = 0
        pcol -= 1
        pel -= 1
        width -= 2
    if '-M' in sys.argv:
        pltM = 0
        pcol -= 1
        pel -= 1
        width -= 2
    if '-log' in sys.argv: logit = 1
    if '-ds' in sys.argv and 'mcd' in sys.argv:
        depth_scale = 'sample_composite_depth'
    if '-sym' in sys.argv:
        ind = sys.argv.index('-sym')
        sym = sys.argv[ind + 1]
        size = float(sys.argv[ind + 2])
    if '-f' in sys.argv:
        ind = sys.argv.index('-f')
        meas_file = sys.argv[ind + 1]
    if '-fsa' in sys.argv:
        ind = sys.argv.index('-fsa')
        samp_file = sys.argv[ind + 1]
        if '-fa' in sys.argv:
            print main.__doc__
            print 'only -fsa OR -fa - not both'
            sys.exit()
    elif '-fa' in sys.argv:
        ind = sys.argv.index('-fa')
        age_file = sys.argv[ind + 1]
    if '-fsp' in sys.argv:
        ind = sys.argv.index('-fsp')
        spc_file = dir_path + '/' + sys.argv[ind + 1]
        spc_sym = sys.argv[ind + 2]
        spc_size = float(sys.argv[ind + 3])
    if '-fres' in sys.argv:
        ind = sys.argv.index('-fres')
        res_file = dir_path + '/' + sys.argv[ind + 1]
        res_sym = sys.argv[ind + 2]
        res_size = float(sys.argv[ind + 3])
    if '-fwig' in sys.argv:
        ind = sys.argv.index('-fwig')
        wig_file = dir_path + '/' + sys.argv[ind + 1]
        pcol += 1
        width += 2
    if '-fsum' in sys.argv:
        ind = sys.argv.index('-fsum')
        sum_file = dir_path + '/' + sys.argv[ind + 1]
    if '-fmt' in sys.argv:
        ind = sys.argv.index('-fmt')
        fmt = '.' + sys.argv[ind + 1]
    if '-sav' in sys.argv:
        plots = 1
        verbose = 0
    if '-LP' in sys.argv:
        ind = sys.argv.index('-LP')
        meth = sys.argv[ind + 1]
        if meth == "AF":
            step = round(float(sys.argv[ind + 2]) * 1e-3, 6)
            method = 'LT-AF-Z'
        elif meth == 'T':
            step = round(float(sys.argv[ind + 2]) + 273, 6)
            method = 'LT-T-Z'
        elif meth == 'ARM':
            method = 'LT-AF-I'
            step = round(float(sys.argv[ind + 2]) * 1e-3, 6)
        elif meth == 'IRM':
            method = 'LT-IRM'
            step = round(float(sys.argv[ind + 2]) * 1e-3, 6)
        elif meth == 'X':
            method = 'LP-X'
            pcol += 1
            if sys.argv[ind + 2] == 'mass':
                suc_key = 'measurement_chi_mass'
            elif sys.argv[ind + 2] == 'vol':
                suc_key = 'measurement_chi_volume'
            else:
                print 'error in susceptibility units'
                sys.exit()
        else:
            print 'method not supported'
            sys.exit()
    if '-n' in sys.argv:
        ind = sys.argv.index('-n')
        wt_file = dir_path + '/' + sys.argv[ind + 1]
        norm = 1
    dmin, dmax = -1, -1
    if '-d' in sys.argv:
        ind = sys.argv.index('-d')
        dmin = float(sys.argv[ind + 1])
        dmax = float(sys.argv[ind + 2])
    if '-ts' in sys.argv:
        ind = sys.argv.index('-ts')
        ts = sys.argv[ind + 1]
        amin = float(sys.argv[ind + 2])
        amax = float(sys.argv[ind + 3])
        pTS = 1
        pcol += 1
        width += 2
    #
    #
    # get data read in
    meas_file = dir_path + '/' + meas_file
    if age_file == "":
        samp_file = dir_path + '/' + samp_file
        Samps, file_type = pmag.magic_read(samp_file)
    else:
        depth_scale = 'age'
        age_file = dir_path + '/' + age_file
        Samps, file_type = pmag.magic_read(age_file)
        age_unit = ""
    if spc_file != "": Specs, file_type = pmag.magic_read(spc_file)
    if res_file != "": Results, file_type = pmag.magic_read(res_file)
    if norm == 1:
        ErSpecs, file_type = pmag.magic_read(wt_file)
        print len(ErSpecs), ' specimens read in from ', wt_file
    Cores = []
    core_depth_key = "Top depth cored CSF (m)"
    if sum_file != "":
        input = open(sum_file, 'rU').readlines()
        if "Core Summary" in input[0]:
            headline = 1
        else:
            headline = 0
        keys = input[headline].replace('\n', '').split(',')
        if "Core Top (m)" in keys: core_depth_key = "Core Top (m)"
        if "Core Label" in keys: core_label_key = "Core Label"
        if "Core label" in keys: core_label_key = "Core label"
        for line in input[2:]:
            if 'TOTALS' not in line:
                CoreRec = {}
                for k in range(len(keys)):
                    CoreRec[keys[k]] = line.split(',')[k]
                Cores.append(CoreRec)
        if len(Cores) == 0:
            print 'no Core depth information available: import core summary file'
            sum_file = ""
    Data = []
    if depth_scale == 'sample_core_depth':
        ylab = "Depth (mbsf)"
    elif depth_scale == 'age':
        ylab = "Age"
    else:
        ylab = "Depth (mcd)"
    # collect the data for plotting declination
    Depths, Decs, Incs, Ints = [], [], [], []
    SDepths, SDecs, SIncs, SInts = [], [], [], []
    SSucs = []
    samples = []
    methods, steps, m2 = [], [], []
    if pltS:  # plot the bulk measurement data
        Meas, file_type = pmag.magic_read(meas_file)
        meas_key = 'measurement_magn_moment'
        print len(Meas), ' measurements read in from ', meas_file
        for m in intlist:  # find the intensity key with data
            meas_data = pmag.get_dictitem(
                Meas, m, '', 'F')  # get all non-blank data for this specimen
            if len(meas_data) > 0:
                meas_key = m
                break
        m1 = pmag.get_dictitem(Meas, 'magic_method_codes', method,
                               'has')  # fish out the desired method code
        if method == 'LT-T-Z':
            m2 = pmag.get_dictitem(m1, 'treatment_temp', str(step),
                                   'eval')  # fish out the desired step
        elif 'LT-AF' in method:
            m2 = pmag.get_dictitem(m1, 'treatment_ac_field', str(step), 'eval')
        elif 'LT-IRM' in method:
            m2 = pmag.get_dictitem(m1, 'treatment_dc_field', str(step), 'eval')
        elif 'LT-X' in method:
            m2 = pmag.get_dictitem(m1, suc_key, '', 'F')
        if len(m2) > 0:
            for rec in m2:  # fish out depths and weights
                D = pmag.get_dictitem(Samps, 'er_sample_name',
                                      rec['er_sample_name'], 'T')
                if not D:  # if using an age_file, you may need to sort by site
                    D = pmag.get_dictitem(Samps, 'er_site_name',
                                          rec['er_site_name'], 'T')
                depth = pmag.get_dictitem(D, depth_scale, '', 'F')
                if len(depth) > 0:
                    if ylab == 'Age':
                        ylab = ylab + ' (' + depth[0][
                            'age_unit'] + ')'  # get units of ages - assume they are all the same!

                    rec['core_depth'] = float(depth[0][depth_scale])
                    rec['magic_method_codes'] = rec[
                        'magic_method_codes'] + ':' + depth[0][
                            'magic_method_codes']
                    if norm == 1:
                        specrecs = pmag.get_dictitem(ErSpecs,
                                                     'er_specimen_name',
                                                     rec['er_specimen_name'],
                                                     'T')
                        specwts = pmag.get_dictitem(specrecs,
                                                    'specimen_weight', "", 'F')
                        if len(specwts) > 0:
                            rec['specimen_weight'] = specwts[0][
                                'specimen_weight']
                            Data.append(
                                rec
                            )  # fish out data with core_depth and (if needed) weights
                    else:
                        Data.append(
                            rec
                        )  # fish out data with core_depth and (if needed) weights
                    if title == "":
                        pieces = rec['er_sample_name'].split('-')
                        location = rec['er_location_name']
                        title = location

        SData = pmag.sort_diclist(Data, 'core_depth')
        for rec in SData:  # fish out bulk measurement data from desired depths
            if dmax == -1 or float(rec['core_depth']) < dmax and float(
                    rec['core_depth']) > dmin:
                Depths.append((rec['core_depth']))
                if method == "LP-X":
                    SSucs.append(float(rec[suc_key]))
                else:
                    if pltD == 1: Decs.append(float(rec['measurement_dec']))
                    if pltI == 1: Incs.append(float(rec['measurement_inc']))
                    if norm == 0 and pltM == 1:
                        Ints.append(float(rec[meas_key]))
                    if norm == 1 and pltM == 1:
                        Ints.append(
                            float(rec[meas_key]) /
                            float(rec['specimen_weight']))
            if len(SSucs) > 0:
                maxSuc = max(SSucs)
                minSuc = min(SSucs)
            if len(Ints) > 1:
                maxInt = max(Ints)
                minInt = min(Ints)
        if len(Depths) == 0:
            print 'no bulk measurement data matched your request'
    SpecDepths, SpecDecs, SpecIncs = [], [], []
    FDepths, FDecs, FIncs = [], [], []
    if spc_file != "":  # add depths to spec data
        print 'spec file found'
        BFLs = pmag.get_dictitem(
            Specs, 'magic_method_codes', 'DE-BFL',
            'has')  # get all the discrete data with best fit lines
        for spec in BFLs:
            if location == "":
                location = spec['er_location_name']
            samp = pmag.get_dictitem(Samps, 'er_sample_name',
                                     spec['er_sample_name'], 'T')
            if len(samp) > 0 and depth_scale in samp[0].keys(
            ) and samp[0][depth_scale] != "":
                if ylab == 'Age':
                    ylab = ylab + ' (' + samp[0][
                        'age_unit'] + ')'  # get units of ages - assume they are all the same!
                if dmax == -1 or float(samp[0][depth_scale]) < dmax and float(
                        samp[0][depth_scale]) > dmin:  # filter for depth
                    SpecDepths.append(float(
                        samp[0][depth_scale]))  # fish out data with core_depth
                    SpecDecs.append(float(
                        spec['specimen_dec']))  # fish out data with core_depth
                    SpecIncs.append(float(
                        spec['specimen_inc']))  # fish out data with core_depth
            else:
                print 'no core_depth found for: ', spec['er_specimen_name']
        FMs = pmag.get_dictitem(
            Specs, 'magic_method_codes', 'DE-FM',
            'has')  # get all the discrete data with best fit lines
        for spec in FMs:
            if location == "":
                location = spec['er_location_name']
            samp = pmag.get_dictitem(Samps, 'er_sample_name',
                                     spec['er_sample_name'], 'T')
            if len(samp) > 0 and depth_scale in samp[0].keys(
            ) and samp[0][depth_scale] != "":
                if ylab == 'Age':
                    ylab = ylab + ' (' + samp[0][
                        'age_unit'] + ')'  # get units of ages - assume they are all the same!
                if dmax == -1 or float(samp[0][depth_scale]) < dmax and float(
                        samp[0][depth_scale]) > dmin:  # filter for depth
                    FDepths.append(float(
                        samp[0][depth_scale]))  # fish out data with core_depth
                    FDecs.append(float(
                        spec['specimen_dec']))  # fish out data with core_depth
                    FIncs.append(float(
                        spec['specimen_inc']))  # fish out data with core_depth
            else:
                print 'no core_depth found for: ', spec['er_specimen_name']
    ResDepths, ResDecs, ResIncs = [], [], []
    if 'age' in depth_scale:  # set y-key
        res_scale = 'average_age'
    else:
        res_scale = 'average_height'
    if res_file != "":  #creates lists of Result Data
        for res in Results:
            meths = res['magic_method_codes'].split(":")
            if 'DE-FM' in meths:
                if dmax == -1 or float(res[res_scale]) < dmax and float(
                        res[res_scale]) > dmin:  # filter for depth
                    ResDepths.append(float(
                        res[res_scale]))  # fish out data with core_depth
                    ResDecs.append(float(
                        res['average_dec']))  # fish out data with core_depth
                    ResIncs.append(float(
                        res['average_inc']))  # fish out data with core_depth
                    Susc, Sus_depths = [], []
    if dmin == -1:
        if len(Depths) > 0: dmin, dmax = Depths[0], Depths[-1]
        if len(FDepths) > 0: dmin, dmax = Depths[0], Depths[-1]
        if pltS == 1 and len(SDepths) > 0:
            if SDepths[0] < dmin: dmin = SDepths[0]
            if SDepths[-1] > dmax: dmax = SDepths[-1]
        if len(SpecDepths) > 0:
            if min(SpecDepths) < dmin: dmin = min(SpecDepths)
            if max(SpecDepths) > dmax: dmax = max(SpecDepths)
        if len(ResDepths) > 0:
            if min(ResDepths) < dmin: dmin = min(ResDepths)
            if max(ResDepths) > dmax: dmax = max(ResDepths)
    if suc_file != "":
        sucdat = open(suc_file, 'rU').readlines()
        keys = sucdat[0].replace('\n', '').split(',')  # splits on underscores
        for line in sucdat[1:]:
            SucRec = {}
            for k in range(len(keys)):
                SucRec[keys[k]] = line.split(',')[k]
            if float(SucRec['Top Depth (m)']) < dmax and float(
                    SucRec['Top Depth (m)']
            ) > dmin and SucRec['Magnetic Susceptibility (80 mm)'] != "":
                Susc.append(float(SucRec['Magnetic Susceptibility (80 mm)']))
                if Susc[-1] > maxSuc: maxSuc = Susc[-1]
                if Susc[-1] < minSuc: minSuc = Susc[-1]
                Sus_depths.append(float(SucRec['Top Depth (m)']))
    WIG, WIG_depths = [], []
    if wig_file != "":
        wigdat, file_type = pmag.magic_read(wig_file)
        swigdat = pmag.sort_diclist(wigdat, depth_scale)
        keys = wigdat[0].keys()
        for key in keys:
            if key != depth_scale:
                plt_key = key
                break
        for wig in swigdat:
            if float(wig[depth_scale]) < dmax and float(
                    wig[depth_scale]) > dmin:
                WIG.append(float(wig[plt_key]))
                WIG_depths.append(float(wig[depth_scale]))
    tint = 4.5
    plt = 1
    if len(Decs) > 0 and len(Depths) > 0 or (
            len(SpecDecs) > 0 and len(SpecDepths) > 0
    ) or (len(ResDecs) > 0
          and len(ResDepths) > 0) or (len(SDecs) > 0 and len(SDepths) > 0) or (
              len(SInts) > 0 and len(SDepths) > 0) or (len(SIncs) > 0
                                                       and len(SDepths) > 0):
        pylab.figure(1, figsize=(width, 8))
        version_num = pmag.get_version()
        pylab.figtext(.02, .01, version_num)
        if pltD == 1:
            ax = pylab.subplot(1, pcol, plt)
            if pltL == 1:
                pylab.plot(Decs, Depths, 'k')
            if len(Decs) > 0:
                pylab.plot(Decs, Depths, sym, markersize=size)
            if len(Decs) == 0 and pltL == 1 and len(SDecs) > 0:
                pylab.plot(SDecs, SDepths, 'k')
            if len(SDecs) > 0:
                pylab.plot(SDecs, SDepths, Ssym, markersize=Ssize)
            if spc_file != "":
                pylab.plot(SpecDecs, SpecDepths, spc_sym, markersize=spc_size)
            if spc_file != "" and len(FDepths) > 0:
                pylab.scatter(FDecs,
                              FDepths,
                              marker=spc_sym[-1],
                              edgecolor=spc_sym[0],
                              facecolor='white',
                              s=spc_size**2)
            if res_file != "":
                pylab.plot(ResDecs, ResDepths, res_sym, markersize=res_size)
            if sum_file != "":
                for core in Cores:
                    depth = float(core[core_depth_key])
                    if depth > dmin and depth < dmax:
                        pylab.plot([0, 360.], [depth, depth], 'b--')
                        if pel == plt:
                            pylab.text(360, depth + tint, core[core_label_key])
            if pel == plt:
                pylab.axis([0, 400, dmax, dmin])
            else:
                pylab.axis([0, 360., dmax, dmin])
            pylab.xlabel('Declination')
            pylab.ylabel(ylab)
            plt += 1
            pmagplotlib.delticks(ax)  # dec xticks are too crowded otherwise
    if pltI == 1:
        pylab.subplot(1, pcol, plt)
        if pltL == 1: pylab.plot(Incs, Depths, 'k')
        if len(Incs) > 0: pylab.plot(Incs, Depths, sym, markersize=size)
        if len(Incs) == 0 and pltL == 1 and len(SIncs) > 0:
            pylab.plot(SIncs, SDepths, 'k')
        if len(SIncs) > 0: pylab.plot(SIncs, SDepths, Ssym, markersize=Ssize)
        if spc_file != "" and len(SpecDepths) > 0:
            pylab.plot(SpecIncs, SpecDepths, spc_sym, markersize=spc_size)
        if spc_file != "" and len(FDepths) > 0:
            pylab.scatter(FIncs,
                          FDepths,
                          marker=spc_sym[-1],
                          edgecolor=spc_sym[0],
                          facecolor='white',
                          s=spc_size**2)
        if res_file != "":
            pylab.plot(ResIncs, ResDepths, res_sym, markersize=res_size)
        if sum_file != "":
            for core in Cores:
                depth = float(core[core_depth_key])
                if depth > dmin and depth < dmax:
                    if pel == plt:
                        pylab.text(90, depth + tint, core[core_label_key])
                    pylab.plot([-90, 90], [depth, depth], 'b--')
        pylab.plot([0, 0], [dmax, dmin], 'k-')
        if pel == plt:
            pylab.axis([-90, 110, dmax, dmin])
        else:
            pylab.axis([-90, 90, dmax, dmin])
        pylab.xlabel('Inclination')
        pylab.ylabel('')
        plt += 1
    if pltM == 1 and len(Ints) > 0 or len(SInts) > 0:
        pylab.subplot(1, pcol, plt)
        for pow in range(-10, 10):
            if maxInt * 10**pow > 1: break
        if logit == 0:
            for k in range(len(Ints)):
                Ints[k] = Ints[k] * 10**pow
            for k in range(len(SInts)):
                SInts[k] = SInts[k] * 10**pow
            if pltL == 1 and len(Ints) > 0: pylab.plot(Ints, Depths, 'k')
            if len(Ints) > 0: pylab.plot(Ints, Depths, sym, markersize=size)
            if len(Ints) == 0 and pltL == 1 and len(SInts) > 0:
                pylab.plot(SInts, SDepths, 'k-')
            if len(SInts) > 0:
                pylab.plot(SInts, SDepths, Ssym, markersize=Ssize)
            if sum_file != "":
                for core in Cores:
                    depth = float(core[core_depth_key])
                    pylab.plot([0, maxInt * 10**pow + .1], [depth, depth],
                               'b--')
                    if depth > dmin and depth < dmax:
                        pylab.text(maxInt * 10**pow - .2 * maxInt * 10**pow,
                                   depth + tint, core[core_label_key])
            pylab.axis([0, maxInt * 10**pow + .1, dmax, dmin])
            if norm == 0:
                pylab.xlabel('%s %i %s' % ('Intensity (10^-', pow, ' Am^2)'))
            else:
                pylab.xlabel('%s %i %s' %
                             ('Intensity (10^-', pow, ' Am^2/kg)'))
        else:
            if pltL == 1: pylab.semilogx(Ints, Depths, 'k')
            if len(Ints) > 0:
                pylab.semilogx(Ints, Depths, sym, markersize=size)
            if len(Ints) == 0 and pltL == 1 and len(SInts) > 0:
                pylab.semilogx(SInts, SDepths, 'k')
            if len(Ints) == 0 and pltL == 1 and len(SInts) > 0:
                pylab.semilogx(SInts, SDepths, 'k')
            if len(SInts) > 0:
                pylab.semilogx(SInts, SDepths, Ssym, markersize=Ssize)
            if sum_file != "":
                for core in Cores:
                    depth = float(core[core_depth_key])
                    pylab.semilogx([minInt, maxInt], [depth, depth], 'b--')
                    if depth > dmin and depth < dmax:
                        pylab.text(maxInt - .2 * maxInt, depth + tint,
                                   core[core_label_key])
            pylab.axis([0, maxInt, dmax, dmin])
            if norm == 0:
                pylab.xlabel('Intensity (Am^2)')
            else:
                pylab.xlabel('Intensity (Am^2/kg)')
        plt += 1
    if suc_file != "" or len(SSucs) > 0:
        pylab.subplot(1, pcol, plt)
        if len(Susc) > 0:
            if pltL == 1: pylab.plot(Susc, Sus_depths, 'k')
            if logit == 0: pylab.plot(Susc, Sus_depths, sym, markersize=size)
            if logit == 1:
                pylab.semilogx(Susc, Sus_depths, sym, markersize=size)
        if len(SSucs) > 0:
            if logit == 0: pylab.plot(SSucs, SDepths, sym, markersize=size)
            if logit == 1: pylab.semilogx(SSucs, SDepths, sym, markersize=size)
        if sum_file != "":
            for core in Cores:
                depth = float(core[core_depth_key])
                if logit == 0:
                    pylab.plot([minSuc, maxSuc], [depth, depth], 'b--')
                if logit == 1:
                    pylab.semilogx([minSuc, maxSuc], [depth, depth], 'b--')
        pylab.axis([minSuc, maxSuc, dmax, dmin])
        pylab.xlabel('Susceptibility')
        plt += 1
    if wig_file != "":
        pylab.subplot(1, pcol, plt)
        pylab.plot(WIG, WIG_depths, 'k')
        if sum_file != "":
            for core in Cores:
                depth = float(core[core_depth_key])
                pylab.plot([WIG[0], WIG[-1]], [depth, depth], 'b--')
        pylab.axis([min(WIG), max(WIG), dmax, dmin])
        pylab.xlabel(plt_key)
        plt += 1
    if pTS == 1:
        ax1 = pylab.subplot(1, pcol, plt)
        ax1.axis([-.25, 1.5, amax, amin])
        plt += 1
        TS, Chrons = pmag.get_TS(ts)
        X, Y, Y2 = [0, 1], [], []
        cnt = 0
        if amin < TS[1]:  # in the Brunhes
            Y = [amin, amin]  # minimum age
            Y1 = [TS[1], TS[1]]  # age of the B/M boundary
            ax1.fill_between(X, Y, Y1,
                             facecolor='black')  # color in Brunhes, black
        for d in TS[1:]:
            pol = cnt % 2
            cnt += 1
            if d <= amax and d >= amin:
                ind = TS.index(d)
                Y = [TS[ind], TS[ind]]
                Y1 = [TS[ind + 1], TS[ind + 1]]
                if pol:
                    ax1.fill_between(
                        X, Y, Y1,
                        facecolor='black')  # fill in every other time
        ax1.plot([0, 1, 1, 0, 0], [amin, amin, amax, amax, amin], 'k-')
        ax2 = ax1.twinx()
        pylab.ylabel("Age (Ma): " + ts)
        for k in range(len(Chrons) - 1):
            c = Chrons[k]
            cnext = Chrons[k + 1]
            d = cnext[1] - (cnext[1] - c[1]) / 3.
            if d >= amin and d < amax:
                ax2.plot([1, 1.5], [c[1], c[1]],
                         'k-')  # make the Chron boundary tick
                ax2.text(1.05, d, c[0])  #
        ax2.axis([-.25, 1.5, amax, amin])
    figname = location + '_m:_' + method + '_core-depthplot' + fmt
    pylab.title(location)
    if verbose:
        pylab.draw()
        ans = raw_input("S[a]ve plot? ")
        if ans == 'a':
            pylab.savefig(figname)
            print 'Plot saved as ', figname
    elif plots:
        pylab.savefig(figname)
        print 'Plot saved as ', figname
    sys.exit()