Esempio n. 1
0
def threeD_gridplot(nodes, save=False, savefile=''):
    r"""Function to plot in 3D a series of grid points.

    :type nodes: list of tuples
    :param nodes: List of tuples of the form (lat, long, depth)
    :type save: bool
    :param save: if True will save without plotting to screen, if False \
        (default) will plot to screen but not save
    :type savefile: str
    :param savefile: required if save=True, path to save figure to.
    """
    lats = []
    longs = []
    depths = []
    for node in nodes:
        lats.append(float(node[0]))
        longs.append(float(node[1]))
        depths.append(float(node[2]))
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(lats, longs, depths)
    ax.set_ylabel("Latitude (deg)")
    ax.set_xlabel("Longitude (deg)")
    ax.set_zlabel("Depth(km)")
    ax.get_xaxis().get_major_formatter().set_scientific(False)
    ax.get_yaxis().get_major_formatter().set_scientific(False)
    if not save:
        plt.show()
        plt.close()
    else:
        plt.savefig(savefile)
    return
Esempio n. 2
0
    def plot_field(self,x,y,u=None,v=None,F=None,contour=False,outdir=None,plot='quiver',figname='_field',format='eps'):
        outdir = self.set_dir(outdir)
        p = 64
        if F is None: F=self.calc_F(u,v)

        plt.close('all')
        plt.figure()
        #fig, axes = plt.subplots(nrows=1)
        if contour:
            plt.hold(True)
            plt.contourf(x,y,F)
        if plot=='quiver':
            plt.quiver(x[::p],y[::p],u[::p],v[::p],scale=0.1)
        
        if plot=='pcolor':
            plt.pcolormesh(x[::4],y[::4],F[::4],cmap=plt.cm.Pastel1)
            plt.colorbar()

        if plot=='stream':
            speed = F[::16]
            plt.streamplot(x[::16], y[::16], u[::16], v[::16], density=(1,1),color='k')

        plt.xlabel('$x$ (a.u.)')
        plt.ylabel('$y$ (a.u.)')
        
        plt.savefig(os.path.join(outdir,figname+'.'+format),format=format,dpi=320,bbox_inches='tight')
        plt.close()
Esempio n. 3
0
def ploter(X,n,name,path,real_ranges = None):
	'''
	Graph plotting module. Very basic, so feel free to modify it.
	'''
	try: import matplotlib.pylab as plt
	except ImportError: 
		print '\nMatplotlib is not installed! Either install it, or deselect graph option.\n'	
		return 1

	lll = 1+len(X)/n
	fig = plt.figure(figsize=(16,14),dpi=100)
	for x in xrange(0,len(X),n):
		iii = 1+x/n
		ax = fig.add_subplot(lll,1,iii)
       
		if real_ranges != None:
			for p in real_ranges:
				if p[0] in range(x,x+n) and p[1] in range(x,x+n): ax.axvspan(p[0]%(n), p[1]%(n), facecolor='g', alpha=0.5)
				elif p[0] in range(x,x+n) and p[1] > x+n: ax.axvspan(p[0]%(n), n, facecolor='g', alpha=0.5)
				elif p[0] < x and p[1] in range(x,x+n): ax.axvspan(0, p[1]%(n), facecolor='g', alpha=0.5)
				elif p[0] < x and p[1] > x+n: ax.axvspan(0, n, facecolor='g', alpha=0.5)

		ax.plot(X[x:x+n],'r-')

		ax.set_xlim(0.,n)
		ax.set_ylim(0.,1.)
		ax.set_xticklabels(range(x,x+750,100)) #(x,x+n/5,x+2*n/5,x+3*n/5,x+4*n/5,x+5*n/5) )
                
	plt.savefig(path+'HMM_'+name+'.png')
	plt.close()
Esempio n. 4
0
def triple_plot(cccsum, cccsum_hist, trace, threshold, save=False,
                savefile=''):
    r"""Main function to make a triple plot with a day-long seismogram, \
    day-long correlation sum trace and histogram of the correlation sum to \
    show normality.

    :type cccsum: numpy.ndarray
    :param cccsum: Array of the cross-channel cross-correlation sum
    :type cccsum_hist: numpy.ndarray
    :param cccsum_hist: cccsum for histogram plotting, can be the same as \
        cccsum but included if cccsum is just an envelope.
    :type trace: obspy.Trace
    :param trace: A sample trace from the same time as cccsum
    :type threshold: float
    :param threshold: Detection threshold within cccsum
    :type save: bool, optional
    :param save: If True will svae and not plot to screen, vice-versa if False
    :type savefile: str, optional
    :param savefile: Path to save figure to, only required if save=True
    """
    if len(cccsum) != len(trace.data):
        print('cccsum is: ' +
              str(len(cccsum))+' trace is: '+str(len(trace.data)))
        msg = ' '.join(['cccsum and trace must have the',
                        'same number of data points'])
        raise ValueError(msg)
    df = trace.stats.sampling_rate
    npts = trace.stats.npts
    t = np.arange(npts, dtype=np.float32) / (df * 3600)
    # Generate the subplot for the seismic data
    ax1 = plt.subplot2grid((2, 5), (0, 0), colspan=4)
    ax1.plot(t, trace.data, 'k')
    ax1.axis('tight')
    ax1.set_ylim([-15 * np.mean(np.abs(trace.data)),
                  15 * np.mean(np.abs(trace.data))])
    # Generate the subplot for the correlation sum data
    ax2 = plt.subplot2grid((2, 5), (1, 0), colspan=4, sharex=ax1)
    # Plot the threshold values
    ax2.plot([min(t), max(t)], [threshold, threshold], color='r', lw=1,
             label="Threshold")
    ax2.plot([min(t), max(t)], [-threshold, -threshold], color='r', lw=1)
    ax2.plot(t, cccsum, 'k')
    ax2.axis('tight')
    ax2.set_ylim([-1.7 * threshold, 1.7 * threshold])
    ax2.set_xlabel("Time after %s [hr]" % trace.stats.starttime.isoformat())
    # ax2.legend()
    # Generate a small subplot for the histogram of the cccsum data
    ax3 = plt.subplot2grid((2, 5), (1, 4), sharey=ax2)
    ax3.hist(cccsum_hist, 200, normed=1, histtype='stepfilled',
             orientation='horizontal', color='black')
    ax3.set_ylim([-5, 5])
    fig = plt.gcf()
    fig.suptitle(trace.id)
    fig.canvas.draw()
    if not save:
        plt.show()
        plt.close()
    else:
        plt.savefig(savefile)
    return
Esempio n. 5
0
def display_grid(grid, **kwargs):
    fig = plt.figure()
    plt.axes().set_aspect('equal')

    if kwargs.get('mark_core_cells', True):
        core_cell_coords = grid._cell_nodes[1:-1, 1:-1]
        cellx, celly = core_cell_coords[:, :, 0], core_cell_coords[:, :, 1]
        plt.plot(cellx, celly, '-o', np.transpose(cellx), np.transpose(celly), '-o', color='red')

    if kwargs.get('mark_boundary_cells', True):
        boundary_cell_coords = grid._cell_nodes[0, :], \
                               grid._cell_nodes[-1, :], \
                               grid._cell_nodes[1:-1, 0], \
                               grid._cell_nodes[1:-1, -1]

        for coords in boundary_cell_coords:
            plt.plot(coords[:, 0], coords[:, 1], '-x', color='blue')

    if kwargs.get('show', False):
        plt.show()

    f = BytesIO()
    plt.savefig(f)

    return f
Esempio n. 6
0
def showKernel(dataOrMatrix, fileName = None, useLabels = True, **args) :
 
    labels = None
    if hasattr(dataOrMatrix, 'type') and dataOrMatrix.type == 'dataset' :
	data = dataOrMatrix
	k = data.getKernelMatrix()
	labels = data.labels
    else :
	k = dataOrMatrix
	if 'labels' in args :
	    labels = args['labels']

    import matplotlib

    if fileName is not None and fileName.find('.eps') > 0 :
        matplotlib.use('PS')
    from matplotlib import pylab

    pylab.matshow(k)
    #pylab.show()

    if useLabels and labels.L is not None :
	numPatterns = 0
	for i in range(labels.numClasses) :
	    numPatterns += labels.classSize[i]
	    #pylab.figtext(0.05, float(numPatterns) / len(labels), labels.classLabels[i])
	    #pylab.figtext(float(numPatterns) / len(labels), 0.05, labels.classLabels[i])
	    pylab.axhline(numPatterns, color = 'black', linewidth = 1)
	    pylab.axvline(numPatterns, color = 'black', linewidth = 1)
    pylab.axis([0, len(labels), 0, len(labels)])
    if fileName is not None :
        pylab.savefig(fileName)
	pylab.close()
Esempio n. 7
0
def plot_feat_hist(data_name_list, filename=None):
    if len(data_name_list)>1:
        assert filename is not None

    pylab.figure(num=None, figsize=(8, 6))
    num_rows = 1 + (len(data_name_list) - 1) / 2
    num_cols = 1 if len(data_name_list) == 1 else 2
    pylab.figure(figsize=(5 * num_cols, 4 * num_rows))

    for i in range(num_rows):
        for j in range(num_cols):
            pylab.subplot(num_rows, num_cols, 1 + i * num_cols + j)
            x, name = data_name_list[i * num_cols + j]
            pylab.title(name)
            pylab.xlabel('Value')
            pylab.ylabel('Fraction')
            # the histogram of the data
            max_val = np.max(x)
            if max_val <= 1.0:
                bins = 50
            elif max_val > 50:
                bins = 50
            else:
                bins = max_val
            n, bins, patches = pylab.hist(
                x, bins=bins, normed=1, facecolor='blue', alpha=0.75)

            pylab.grid(True)

    if not filename:
        filename = "feat_hist_%s.png" % name.replace(" ", "_")

    pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
Esempio n. 8
0
def make_plot_mG_nH(mgrpArr_all, denArr_all, over_density2, fignamestr, ngrp=15):
	fig = plt.figure(3)
	plt.clf()
	ax = fig.add_subplot(111)

	idArr, bins = pTdf.group_by_prop(mgrpArr_all, ngrp=ngrp, takelog=True, fixed_interval=True)
	ngrp = len(bins) - 1
	bins = 10.**bins
		
	f_nHArr = N.zeros(len(bins)-1)

	rho_mean = cTdf.mean_hydrogen_numden_z(simI["zcol"])
	for igrp in xrange(ngrp):
		isame = N.where(idArr == igrp)[0]
		print "ptcls in [%.2e]: %d" % (bins[igrp], len(isame))
		if len(isame) > 0:
			ihigh = N.where(denArr_all[isame, izcol] >= over_density2*rho_mean)[0]
			f_nHArr[igrp] = 1.*len(ihigh)/(1.*len(isame))

	print "x({:d}): {:}".format(len(bins[:-1]+bins[1:]), (bins[:-1]+bins[1:])/2.)
	print "y({:d}): {:}".format(len(f_nHArr), f_nHArr)

	ax.plot((bins[:-1]+bins[1:])/2., f_nHArr)
	ax.set_xlabel(r"$M_h$") 
	ax.set_ylabel(r"$f_{n_H > %d \bar{n_H}}(%.1f)$" % (over_density2, simI["zcol"]))
	ax.set_xscale("log")
	ax.set_xlim(bins[0], bins[-1]); ax.set_ylim(0., 1.1)
	#plt.setp(ax.get_xticklabels(), fontsize=fs["tc"])
	#plt.setp(ax.get_yticklabels(), fontsize=fs["tc"])

	figname = "mgrp-fnH_%s_ov%d_vr%d.%s" % (fignamestr[1], int(over_density2), simI["virial_radius"], figext)
	plt.savefig(os.path.join(fignamestr[2], figname))
Esempio n. 9
0
def make_corr1d_fig(dosave=False):
    corr = make_corr_both_hemi()
    lw=2; fs=16
    pl.figure(1)#, figsize=(8, 7))
    pl.clf()
    pl.xlim(4,300)
    pl.ylim(-400,+500)    
    lambda_titles = [r'$20 < \lambda < 30$',
                     r'$30 < \lambda < 40$',
                     r'$\lambda > 40$']
    colors = ['blue','green','red']
    for i in range(3):
        corr1d, rcen = corr_1d_from_2d(corr[i])
        ipdb.set_trace()
        pl.semilogx(rcen, corr1d*rcen**2, lw=lw, color=colors[i])
        #pl.semilogx(rcen, corr1d*rcen**2, 'o', lw=lw, color=colors[i])
    pl.xlabel(r'$s (Mpc)$',fontsize=fs)
    pl.ylabel(r'$s^2 \xi_0(s)$', fontsize=fs)    
    pl.legend(lambda_titles, 'lower left', fontsize=fs+3)
    pl.plot([.1,10000],[0,0],'k--')
    s_bao = 149.28
    pl.plot([s_bao, s_bao],[-9e9,+9e9],'k--')
    pl.text(s_bao*1.03, 420, 'BAO scale')
    pl.text(s_bao*1.03, 370, '%0.1f Mpc'%s_bao)
    if dosave: pl.savefig('xi1d_3bin.pdf')
 def plot_values(self, TITLE, SAVE):
     plot(self.list_of_densities, self.list_of_pressures)
     title(TITLE)
     xlabel("Densities")
     ylabel("Pressure")
     savefig(SAVE)
     show()
Esempio n. 11
0
 def test_simple_gen(self):
     self_con = .8
     other_con = 0.05
     g = self.gen.gen_stoch_blockmodel(min_degree=1, blocks=5, self_con=self_con, other_con=other_con,
                                       powerlaw_exp=2.1, degree_seq='powerlaw', num_nodes=1000, num_links=3000)
     deg_hist = vertex_hist(g, 'total')
     res = fit_powerlaw.Fit(g.degree_property_map('total').a, discrete=True)
     print 'powerlaw alpha:', res.power_law.alpha
     print 'powerlaw xmin:', res.power_law.xmin
     if len(deg_hist[0]) != len(deg_hist[1]):
         deg_hist[1] = deg_hist[1][:len(deg_hist[0])]
     print 'plot degree dist'
     plt.plot(deg_hist[1], deg_hist[0])
     plt.xscale('log')
     plt.xlabel('degree')
     plt.ylabel('#nodes')
     plt.yscale('log')
     plt.savefig('deg_dist_test.png')
     plt.close('all')
     print 'plot graph'
     pos = sfdp_layout(g, groups=g.vp['com'], mu=3)
     graph_draw(g, pos=pos, output='graph.png', output_size=(800, 800),
                vertex_size=prop_to_size(g.degree_property_map('total'), mi=2, ma=30), vertex_color=[0., 0., 0., 1.],
                vertex_fill_color=g.vp['com'],
                bg_color=[1., 1., 1., 1.])
     plt.close('all')
     print 'init:', self_con / (self_con + other_con), other_con / (self_con + other_con)
     print 'real:', gt_tools.get_graph_com_connectivity(g, 'com')
Esempio n. 12
0
def PlotTurbulenceIllustr(a):

    """
    Can generate the grid with
    g=kolmogorovutils.GenerateKolmogorov3D( 1025, 129, 129)
    a=kolmogorovutils.GridToNumarray(g)

    """

    for x in [1,10,100]:

        suba= numarray.sum(a[:,:,0:x], axis=2)

        suba.transpose()
        pylab.clf()
        pylab.matshow(suba)
        pylab.savefig("temp/turb3d-sum%03i.eps" % x)

    for x in [1,10,100]:

        for j in [0,1,2]:

            suba= numarray.sum(a[:,:200,j*x:(j+1)*x], axis=2)

            suba.transpose()
            pylab.clf()
            pylab.matshow(suba)
            pylab.savefig("temp/turb3d-sum%03i-s%i.eps" % (x,j))        
Esempio n. 13
0
def Animate(g):
    for i in range(1,64,5):
        pylab.clf()
        x= g[0:i,:,:]
        y= numarray.sum(x, axis=0)
        pylab.matshow( y)
        pylab.savefig("temp/3dturb-%03i.png" % i)
Esempio n. 14
0
    def visualization2(self, sp_to_vis=None):
        if sp_to_vis:
            species_ready = list(set(sp_to_vis).intersection(self.all_sp_signatures.keys()))
        else:
            raise Exception('list of driver species must be defined')

        if not species_ready:
            raise Exception('None of the input species is a driver')

        for sp in species_ready:
            # Setting up figure
            plt.figure()
            plt.subplot(313)

            mon_val = OrderedDict()
            signature = self.all_sp_signatures[sp]
            for idx, mon in enumerate(list(set(signature))):
                if mon[0] == 'C':
                    mon_val[self.all_comb[sp][mon] + (-1,)] = idx
                else:
                    mon_val[self.all_comb[sp][mon]] = idx

            mon_rep = [0] * len(signature)
            for i, m in enumerate(signature):
                if m[0] == 'C':
                    mon_rep[i] = mon_val[self.all_comb[sp][m] + (-1,)]
                else:
                    mon_rep[i] = mon_val[self.all_comb[sp][m]]
            # mon_rep = [mon_val[self.all_comb[sp][m]] for m in signature]

            y_pos = numpy.arange(len(mon_val.keys()))
            plt.scatter(self.tspan[1:], mon_rep)
            plt.yticks(y_pos, mon_val.keys())
            plt.ylabel('Monomials', fontsize=16)
            plt.xlabel('Time(s)', fontsize=16)
            plt.xlim(0, self.tspan[-1])
            plt.ylim(0, max(y_pos))

            plt.subplot(312)

            for name in self.model.odes[sp].as_coefficients_dict():
                mon = name
                mon = mon.subs(self.param_values)
                var_to_study = [atom for atom in mon.atoms(sympy.Symbol)]
                arg_f1 = [numpy.maximum(self.mach_eps, self.y[str(va)][1:]) for va in var_to_study]
                f1 = sympy.lambdify(var_to_study, mon)
                mon_values = f1(*arg_f1)
                mon_name = str(name).partition('__')[2]
                plt.plot(self.tspan[1:], mon_values, label=mon_name)
            plt.ylabel('Rate(m/sec)', fontsize=16)
            plt.legend(bbox_to_anchor=(-0.1, 0.85), loc='upper right', ncol=1)

            plt.subplot(311)
            plt.plot(self.tspan[1:], self.y['__s%d' % sp][1:], label=parse_name(self.model.species[sp]))
            plt.ylabel('Molecules', fontsize=16)
            plt.legend(bbox_to_anchor=(-0.15, 0.85), loc='upper right', ncol=1)
            plt.suptitle('Tropicalization' + ' ' + str(self.model.species[sp]))

            # plt.show()
            plt.savefig('s%d' % sp + '.png', bbox_inches='tight', dpi=400)
Esempio n. 15
0
    def test_flux(self):
        tol = 150.
        inputcat = catalog.read(os.path.join(self.args.tmp_path, 'ccd_1.cat'))
        pixradius = 3*self.target["psf"]/self.instrument["PIXEL_SCALE"]
        positions = list(zip(inputcat["X_IMAGE"]-1, inputcat["Y_IMAGE"]-1))
        fluxes = image.simple_aper_phot(self.im[1], positions, pixradius)
        sky_background = image.annulus_photometry(self.im[1], positions,
        	pixradius+5, pixradius+8)

        total_bg_pixels = np.shape(image.build_annulus_mask(pixradius+5, pixradius+8, positions[0]))[1]
        total_source_pixels = np.shape(image.build_circle_mask(pixradius,
        	positions[0]))[1]

        estimated_fluxes = fluxes - sky_background*1./total_bg_pixels*total_source_pixels

        estimated_magnitude = image.flux2mag(estimated_fluxes,
        	self.im[1].header['SIMMAGZP'], self.target["exptime"])

        expected_flux = image.mag2adu(17.5, self.target["zeropoint"][0],
        	exptime=self.target["exptime"])

        p.figure()
        p.hist(fluxes, bins=50)
        p.title('Expected flux: {:0.2f}, mean flux: {:1.2f}'.format(expected_flux, np.mean(estimated_fluxes)))
        p.savefig(os.path.join(self.figdir,'Fluxes.png'))

        assert np.all(np.abs(fluxes-expected_flux) < tol)
Esempio n. 16
0
def plot_feat_hist(data_name_list, filename=None):
    pylab.clf()
    # import pdb;pdb.set_trace()
    num_rows = 1 + (len(data_name_list) - 1) / 2
    num_cols = 1 if len(data_name_list) == 1 else 2
    pylab.figure(figsize=(5 * num_cols, 4 * num_rows))

    for i in range(num_rows):
        for j in range(num_cols):
            pylab.subplot(num_rows, num_cols, 1 + i * num_cols + j)
            x, name = data_name_list[i * num_cols + j]
            pylab.title(name)
            pylab.xlabel('Value')
            pylab.ylabel('Density')
            # the histogram of the data
            max_val = np.max(x)
            if max_val <= 1.0:
                bins = 50
            elif max_val > 50:
                bins = 50
            else:
                bins = max_val
            n, bins, patches = pylab.hist(
                x, bins=bins, normed=1, facecolor='green', alpha=0.75)

            pylab.grid(True)

    if not filename:
        filename = "feat_hist_%s.png" % name

    pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plotHist(outBaseStr,outData,motifID):
    """Plot histograms for each motif results."""
    def _chooseBins():
        """Uses one of a number of heuristics to 
        choose the bins for each hist.
        
        More discussed here:
        http://en.wikipedia.org/wiki/Histogram#Number_of_bins_and_width"""
        # sqrtChoice
        # k = int(sqrt(n))+1
        return int(math.sqrt(len(ctrls)))*3 

        
    outFile = '%s_%s_hist.pdf' % (outBaseStr,motifID)
    ctrls   = [x for x in outData[motifID][1:]]
    yLab    = 'Controls [n=%s]' % (len(ctrls))
    xLab    = 'Enrichment [p-value]'
    
    fig  = pl.figure()
    ax = fig.add_subplot(111)
    ax.set_ylabel(yLab)
    ax.set_xlabel(xLab)
    ax.set_xscale('log')
    
    bins = _chooseBins()
    hData = ax.hist(ctrls, bins=bins,histtype='stepfilled',color='grey',cumulative=1)
    ax.axvline(x=outData[motifID][0] ,linewidth=2, color='r')
    pl.savefig(outFile)
Esempio n. 18
0
 def show_CMB_T_map(self,Tmap=None, max=100, title = "CMB graviational potential fluctuations as seen from inside the LSS", from_perspective_of = "observer", cmap=None):
     if Tmap is None:
         self.NSIDE = 256
         self.Tmap = hp.alm2map(self.alm,self.NSIDE)
     else:
         self.Tmap = Tmap
         
     if from_perspective_of == "observer":
         
         dpi = 300
         figsize_inch = 60, 40
         fig = plt.figure(figsize=figsize_inch, dpi=dpi)
         # Sky map:
         hp.mollview(self.Tmap, rot=(-90,0,0),  min=-max, max=max,  title=title + ", $\ell_{max}=$%d " % self.truncated_lmax, cmap=cmap, unit="$\mu$K")
         
         plt.savefig(title+".png", dpi=dpi, bbox_inches="tight")
     
     else:
         # Interactive "external" view ([like this](http://zonca.github.io/2013/03/interactive-3d-plot-of-sky-map.html))            pass
         #   beatbox.zoncaview(self.Tmap)
         # This did not work, sadly. Maybe we can find a 3D
         # spherical surface plot routine using matplotlib? For
         # now, just use the healpix vis.
         R = (0.0,0.0,0.0) # (lon,lat,psi) to specify center of map and rotation to apply
         hp.orthview(self.Tmap,rot=R,flip='geo',half_sky=True,title="CMB graviational potential fluctuations as seen from outside the LSS, $\ell_{max}$=%d" % self.truncated_lmax)
         print "Ahem - we can't visualize maps on the surface of the sphere yet, sorry."
     return
Esempio n. 19
0
    def plot_corner_posteriors(self, savefile=None, labels=["T1", "R1", "Av", "T2", "R2"]):
        '''
        Plots the corner plot of the MCMC results.
        '''
        ndim = len(self.sampler.flatchain[0,:])
        chain = self.sampler
        samples = chain.flatchain
        
        samples = samples[:,0:ndim]  
        plt.figure(figsize=(8,8))
        fig = corner.corner(samples, labels=labels[0:ndim])
        plt.title("MJD: %.2f"%self.mjd)
        name = self._get_save_path(savefile, "mcmc_posteriors")
        plt.savefig(name)
        plt.close("all")
        

        plt.figure(figsize=(8,ndim*3))
        for n in range(ndim):
            plt.subplot(ndim,1,n+1)
            chain = self.sampler.chain[:,:,n]
            nwalk, nit = chain.shape
            
            for i in np.arange(nwalk):
                plt.plot(chain[i], lw=0.1)
                plt.ylabel(labels[n])
                plt.xlabel("Iteration")
        name_walkers = self._get_save_path(savefile, "mcmc_walkers")
        plt.tight_layout()
        plt.savefig(name_walkers)
        plt.close("all")  
    def handle(self, *args, **options):
        try:
            from matplotlib import pylab as pl
            import numpy as np
        except ImportError:
            raise Exception('Be sure to install requirements_scipy.txt before running this.')

        all_names_and_counts = RawCommitteeTransactions.objects.all().values('attest_by_name').annotate(total=Count('attest_by_name')).order_by('-total')
        all_names_and_counts_as_tuple_and_sorted = sorted([(row['attest_by_name'], row['total']) for row in all_names_and_counts], key=lambda row: row[1])
        print "top ten attestors:  (name, number of transactions they attest for)"
        for row in all_names_and_counts_as_tuple_and_sorted[-10:]:
            print row

        n_bins = 100
        filename = 'attestor_participation_distribution.png'

        x_max = all_names_and_counts_as_tuple_and_sorted[-31][1]  # eliminate top outliers from hist
        x_min = all_names_and_counts_as_tuple_and_sorted[0][1]

        counts = [row['total'] for row in all_names_and_counts]
        pl.figure(1, figsize=(18, 6))
        pl.hist(counts, bins=np.arange(x_min, x_max, (float(x_max)-x_min)/100) )
        pl.title('Histogram of Attestor Participation in RawCommitteeTransactions')
        pl.xlabel('Number of transactions a person attested for')
        pl.ylabel('Number of people')
        pl.savefig(filename)
Esempio n. 21
0
def _fig_density(sweight, surweight, pval, nlm):
    """
    Plot the histogram of sweight across the image
    and the thresholds implied by the surrogate model (surweight)
    """
    import matplotlib.pylab as mp
    # compute some thresholds
    nlm = nlm.astype('d')
    srweight = np.sum(surweight,1)
    srw = np.sort(srweight)
    nitem = np.size(srweight)
    thf = srw[int((1-min(pval,1))*nitem)]
    mnlm = max(1,nlm.mean())
    imin = min(nitem-1,int((1.-pval/mnlm)*nitem))
    
    thcf = srw[imin]
    h,c = np.histogram(sweight,100)
    I = h.sum()*(c[1]-c[0])
    h = h/I
    h0,c0 = np.histogram(srweight,100)
    I0 = h0.sum()*(c0[1]-c0[0])
    h0 = h0/I0
    mp.figure(1)
    mp.plot(c,h)
    mp.plot(c0,h0)
    mp.legend(('true histogram','surrogate histogram'))
    mp.plot([thf,thf],[0,0.8*h0.max()])
    mp.text(thf,0.8*h0.max(),'p<0.2, uncorrected')
    mp.plot([thcf,thcf],[0,0.5*h0.max()])
    mp.text(thcf,0.5*h0.max(),'p<0.05, corrected')
    mp.savefig('/tmp/histo_density.eps')
    mp.show()
Esempio n. 22
0
def plotFeaturePDF(ift, pft, outbase, fmin=0.0, fmax=1.0, fstep=0.01):
    """
    Plot a comparison between the input feature distribution and the 
    feature distribution of the predicted halos
    """
    plt.clf()
    nfbins = ( fmax - fmin ) / fstep
    fbins = np.logspace( fmin, fmax, nfbins )
    fcen = ( fbins[:-1] + fbins[1:] ) / 2

    plt.xscale( 'log', nonposx='clip' )
    plt.yscale( 'log', nonposy='clip' )
    
    ic, e, p = plt.hist( ift, fbins, label='Original Halos', alpha=0.5, normed=True )
    pc, e, p = plt.hist( pft, fbins, label='Added Halos', alpha=0.5, normed=True )

    plt.legend()
    plt.xlabel( r'$\delta$' )
    plt.savefig( outbase+'_fpdf.png' )

    fdtype = np.dtype( [ ('fcen', float), ('ifcounts', float), ('pfcounts', float) ] )
    fd = np.ndarray( len(fcen), dtype = fdtype )
    fd[ 'mcen' ] = fcen
    fd[ 'imcounts' ] = ic
    fd[ 'pmcounts' ] = pc

    fitsio.write( outbase+'_fpdf.fit', fd )
Esempio n. 23
0
def plot_part2(filename):
	"""
	Plots the result of count ones test
	"""
	fig1 = pl.figure()
	iterations, runtimes, fvals = extract(filename)
	algos = ["SA", "GA", "MIMIC"]
	iters_sa, iters_ga, iters_mimic = [np.array(iterations[a]) for a in algos]
	runtime_sa, runtime_ga, runtime_mimic = [np.array(runtimes[a]) for a in algos]
	fvals_sa, fvals_ga, fvals_mimic = [np.array(fvals[a]) for a in algos]

	plotfunc = getattr(pl, "loglog")
	plotfunc(runtime_sa, fvals_sa, "bs", mew=0)
	plotfunc(runtime_ga, fvals_ga, "gs", mew=0)
	plotfunc(runtime_mimic, fvals_mimic, "rs", mew=0)

	# plotfunc(iters_sa, fvals_sa/(runtime_sa * iters_sa), "bs", mew=0)
	# plotfunc(iters_ga, fvals_ga/(runtime_ga * iters_ga), "gs", mew=0)
	# plotfunc(iters_mimic, fvals_mimic/(runtime_mimic * iters_mimic), "rs", mew=0)

	pl.xlabel("Runtime (seconds)")
	pl.ylabel("Objective function value")
	pl.ylim([min(fvals_sa) / 2, max(fvals_mimic) * 2])
	pl.legend(["SA", "GA", "MIMIC"], loc=4)

	pl.savefig(filename.replace(".csv", ".png"), bbox_inches="tight") 
Esempio n. 24
0
def plot_size_of_c(size_of_c, path):
    xlabel('|C|')
    ylabel('Max model size |Ci|')
    grid(True)
    plot([x+1 for x in range(len(size_of_c))], size_of_c)
    savefig(os.path.join(path, 'size_of_c.png'))
    close()
def plot_q(frame,file_prefix='claw',file_format='petsc',path='./_output/',plot_pcolor=True,plot_slices=True,slices_xlimits=None):
    import sys
    sys.path.append('.')
    import gaussian_1d

    sol=Solution(frame,file_format=file_format,read_aux=False,path=path,file_prefix=file_prefix)
    x=sol.state.grid.x.centers
    mx=len(x)

    bathymetry = 0.5
    eta=sol.state.q[0,:] + bathymetry

    if frame < 10:
        str_frame = "00"+str(frame)
    elif frame < 100:
        str_frame = "0"+str(frame)
    else:
        str_frame = str(frame)

    fig = pl.figure(figsize=(40,10))
    ax = fig.add_subplot(111)
    ax.set_aspect(aspect=1)
    ax.plot(x,eta)
    #pl.title("t= "+str(sol.state.t),fontsize=20)
    #pl.xticks(size=20); pl.yticks(size=20)
    #pl.xlim([0, gaussian_1d.Lx])
    pl.ylim([0.5, 1.0])
    pl.xlim([0., 4.0])
    #pl.axis('equal')
    pl.savefig('./_plots/eta_'+str_frame+'_slices.png')
    pl.close()
Esempio n. 26
0
def plot_heuristic(heuristic, path):
    xlabel('|C|')
    ylabel('h')
    grid(True)
    plot(heuristic)
    savefig(os.path.join(path, 'heuristic.png'))
    close()
Esempio n. 27
0
def plot_running_time(running_time, path):
    xlabel('|C|')
    ylabel('MTV iteration in secs.')
    grid(True)
    plot([x for x in range(len(running_time))], running_time)
    savefig(os.path.join(path, 'running_time.png'))
    close()
Esempio n. 28
0
    def plot_knowledge_count(agent_network, filename):

        word_dict = dict()
        agent_list = agent_network.get_all_agents()
        for agent_item in agent_list:
            for word in agent_item.knowledge:
                if word not in word_dict:
                    word_dict[word] = 0
                word_dict[word] = word_dict[word] + 1

        word_count_tuple_list = word_dict.items()
        word_count_tuple_list = sorted(word_count_tuple_list, key=itemgetter(1))
        print word_count_tuple_list

        x = list()
        y = list()

        for item in word_count_tuple_list:
            word = item[0]
            count = item[1]
            x.append(word)
            y.append(count)

        plt.scatter(x, y, s=30, vmin = 0, vmax= 100, alpha=0.5)
        plt.savefig(filename)
Esempio n. 29
0
def plot_BIC_score(BIC_SCORE, path):
    xlabel('|C|')
    ylabel('BIC score')
    grid(True)
    plot(BIC_SCORE)
    savefig(os.path.join(path, 'BIC.png'))
    close()
Esempio n. 30
0
def plotMassFunction(im, pm, outbase, mmin=9, mmax=13, mstep=0.05):
    """
    Make a comparison plot between the input mass function and the 
    predicted projected correlation function
    """
    plt.clf()

    nmbins = ( mmax - mmin ) / mstep
    mbins = np.logspace( mmin, mmax, nmbins )
    mcen = ( mbins[:-1] + mbins[1:] ) /2
    
    plt.xscale( 'log', nonposx = 'clip' )
    plt.yscale( 'log', nonposy = 'clip' )
    
    ic, e, p = plt.hist( im, mbins, label='Original Halos', alpha=0.5, normed = True)
    pc, e, p = plt.hist( pm, mbins, label='Added Halos', alpha=0.5, normed = True)
    
    plt.legend()
    plt.xlabel( r'$M_{vir}$' )
    plt.ylabel( r'$\frac{dN}{dM}$' )
    #plt.tight_layout()
    plt.savefig( outbase+'_mfcn.png' )
    
    mdtype = np.dtype( [ ('mcen', float), ('imcounts', float), ('pmcounts', float) ] )
    mf = np.ndarray( len(mcen), dtype = mdtype )
    mf[ 'mcen' ] = mcen
    mf[ 'imcounts' ] = ic
    mf[ 'pmcounts' ] = pc

    fitsio.write( outbase+'_mfcn.fit', mf )
Esempio n. 31
0
def plot_bf_distributions():

    # Bulge-fraction distribution
    btf = genfromtxt('bt_distr_mstar.txt')

    cmap = get_cmap('coolwarm')
    labs = [
        r"$9<\log\,M_\ast<9.25$", r"$9.25<\log\,M_\ast<9.5$",
        r"$9.5<\log\,M_\ast<9.75$", r"$9.75<\log\,M_\ast<10$",
        r"$10<\log\,M_\ast<10.25$", r"$10.25<\log\,M_\ast<10.5$",
        r"$10.5<\log\,M_\ast<10.75$", r"$10.75<\log\,M_\ast<11$",
        r"$\log\,M_\ast>11$"
    ]

    fig = plt.figure()
    ax = fig.add_subplot(111)
    for i in range(2, 11):
        c = linspace(0.01, 0.99, num=9)[i - 2]
        ax.bar(btf[:, 0],
               btf[:, i],
               width=btf[:, 1] - btf[:, 0],
               color='none',
               edgecolor=cmap(c),
               lw=3)
        ax.text(0.35,
                linspace(0.3, 0.7, num=9)[i - 2],
                labs[i - 2],
                fontsize=16,
                color=cmap(c),
                transform=ax.transAxes)

    ax.axvline(0.2, ymin=0, ymax=1, ls='--', c='k')
    ax.axvline(0.5, ymin=0, ymax=.26, ls='--', c='k')
    ax.axvline(0.5, ymin=0.8, ymax=1, ls='--', c='k')
    ax.axvline(0.8, ymin=0, ymax=1, ls='--', c='k')
    ax.text(0.025,
            0.9,
            r"$\rm Sc-Sb$",
            fontsize=18,
            color='#151AB0',
            transform=ax.transAxes)
    ax.text(0.325,
            0.9,
            r"$\rm Sa$",
            fontsize=18,
            color='#008540',
            transform=ax.transAxes)
    ax.text(0.625,
            0.9,
            r"$\rm S0$",
            fontsize=18,
            color='#AB9700',
            transform=ax.transAxes)
    ax.text(0.875,
            0.9,
            r"$\rm Es$",
            fontsize=18,
            color='#BD000D',
            transform=ax.transAxes)

    ax.set_xlabel(r"$\rm B/T$", fontsize=18)
    ax.set_ylabel(r"$\rm P(B/T)$", fontsize=18)
    ax.set_xlim([0, 1])
    plt.savefig("bt_hist_mstar.pdf", bbox_inches='tight')
    plt.show()
Esempio n. 32
0
def plot_results(filename, ccdnr=1, pngfile=None, panels='residual'):
    #
    #
    #
    if (not os.path.isfile(filename)):
        raise FileNotFoundError
    #
    with fits.open(filename) as hdu:
        resid = hdu['RESIDUALS'].data[ccdnr - 1, :, :]
        errors = hdu['ERRORS'].data[ccdnr - 1, :, :]
        chi2r = hdu['CHI2_R'].data[ccdnr - 1, :, :]
        rev_start = hdu[0].header['REV0']
        rev_end = hdu[0].header['REV1']
    #
    # calculate sigma_clipped statistics
    #
    xstat = stats.sigma_clipped_stats(resid, sigma=3, maxiters=3)
    #
    rawy_array = np.arange(20, 200, 20)
    if (panels == 'all'):
        fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(10, 10), sharex=True)
        im = ax[0].imshow(resid,
                          vmin=-100,
                          vmax=100.0,
                          origin='lower',
                          cmap=plt.get_cmap('bwr'))
        div = make_axes_locatable(ax[0])
        cax = div.append_axes("right", size="5%", pad=0.1)
        cbar = plt.colorbar(im, cax=cax)
        cbar.set_label('E - 8040 (eV)')
        ax[0].set_title(f'CCD: {ccdnr}, revs in [{rev_start},{rev_end}]')
        ax[0].set_xlabel(
            fr'mean={xstat[0]:.1f} eV, st.dev.={xstat[2]:.1f} eV (3-$\sigma$ clipped)'
        )
        #ax[0].set_xticks(rawy_array)
        #
        im = ax[1].imshow(errors,
                          vmin=0.0,
                          vmax=20.0,
                          origin='lower',
                          cmap=plt.get_cmap('bwr'))
        div = make_axes_locatable(ax[1])
        cax = div.append_axes("right", size="5%", pad=0.1)
        cbar = plt.colorbar(im, cax=cax)
        cbar.set_label('Error (eV)')
        #ax[1].set_title(f'CCD: {ccd}')
        #ax[1].set_xticks(rawy_array)
        #
        im = ax[2].imshow(chi2r,
                          vmin=1.0,
                          vmax=2.0,
                          origin='lower',
                          cmap=plt.get_cmap('bwr'))
        div = make_axes_locatable(ax[2])
        cax = div.append_axes("right", size="5%", pad=0.1)
        cbar = plt.colorbar(im, cax=cax)
        cbar.set_label('Chi2_r')
        #ax[2].set_title(f'CCD: {ccd}')
        ax[2].set_xticks(rawy_array)
        #
        if (pngfile is not None):
            plt.savefig(pngfile, dpi=100)
        plt.show()
    else:
        fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 6), sharex=True)
        im = ax.imshow(resid,
                       vmin=-100,
                       vmax=100.0,
                       origin='lower',
                       cmap=plt.get_cmap('bwr'))
        div = make_axes_locatable(ax)
        cax = div.append_axes("right", size="5%", pad=0.1)
        cbar = plt.colorbar(im, cax=cax)
        cbar.set_label('E - 8040 (eV)')
        ax.set_xticks(rawy_array)
        ax.set_xlabel('RAWY')
        ax.set_ylabel('RAWX')
        ax.set_title(
            f'CCD: {ccdnr}, revs in [{rev_start},{rev_end}]\n' +
            fr'mean={xstat[0]:.1f} eV, st.dev.={xstat[2]:.1f} eV (3-$\sigma$ clipped)'
        )
        #plt.title(fr'mean={xstat[0]:.1f} eV, st.dev.={xstat[2]:.1f} eV (3-$\sigma$ clipped)',ha='right',fontsize=16)
        if (pngfile is not None):
            plt.savefig(pngfile, dpi=100)
        plt.show()
    return
    m = Basemap(llcrnrlon=-15.5,
                llcrnrlat=-25.5,
                urcrnrlon=15.5,
                urcrnrlat=-4.5,
                projection='merc',
                resolution='l')
    #Move to image directory
    os.chdir(directory)
    #Triplots for each file
    print 'Making plots for %s...' % f
    plt.figure('')
    plt.clf()
    cloud.five_plot(data='Nd')
    fig = plt.gcf()
    fig.set_size_inches(13.33, 7.5)
    plt.savefig('%s_%s_%s_%s_Nd' % (year, month, day, cloud.time), dpi=150)
    print 'Done!\n'

#
###Terra ACAERO
#
print 'Checking for new Terra ACAERO data...'
fdir = '/Users/michaeldiamond/Documents/oracles_files/terra/%s' % jday
os.chdir(fdir)
current_files = os.listdir(fdir)
new_files = []
path = 'allData/6/MOD06ACAERO/%s/%s/' % (year, jday)
files = []

#Try to access
ftp_worked = False
Esempio n. 34
0
ax.set_ylim(0,8)
ax.tick_params(axis=u'both', which=u'both',length=0)
ax.set_xlabel('Measurement environment',fontsize=14)
ax.set_ylabel('Evolution environment',fontsize=14)
pt.plot([0,8],[8,0],'--',color='Gray',linewidth=.5)
for i in range(8):
	for j in range(8):
		if fitness_table[i,7-j] < 0 and fitness_table[i,7-j] > -.1: ###This will get printed as -0.0, so print 0.0 instead
			ax.text(7-j+.5,7-i+.5,abs(numpy.round(fitness_table[i,7-j],1)),color='Gray',fontsize=7,horizontalalignment='center',verticalalignment='center')
		elif i==7-j:
			ax.text(7-j+.5,7-i+.5,numpy.round(fitness_table[i,7-j],1),color='k',fontsize=7,horizontalalignment='center',verticalalignment='center')

		else:
			ax.text(7-j+.5,7-i+.5,numpy.round(fitness_table[i,7-j],1),color='Gray',fontsize=7,horizontalalignment='center',verticalalignment='center')
		
pt.savefig('figures/median_fitness_heatmap_check.pdf',bbox_inches='tight')

##Plot the table with clones that lost the virus removed

pt.figure()

pt.pcolor( fitness_table_vplus[::-1], cmap = 'RdBu', vmin=-5,vmax=5,edgecolors='w' )
pt.colorbar(label='Median fitness (%)')
ax = pt.gca()
ax.set_xticks(numpy.arange(.5,8.7,1))
ax.set_yticks(numpy.arange(.5,8.7,1))
ax.set_xticklabels(evolution_env_labels,fontsize=8)
ax.set_yticklabels(measurement_env_labels[::-1],fontsize=8)
ax.set_xlim(0,8)
ax.set_ylim(0,8)
ax.tick_params(axis=u'both', which=u'both',length=0)
Esempio n. 35
0
    for word in model.vocab:
        vocabulary.append(word)
        vec.append(model[word])
    vecLength = len(vec)
    vec = np.array(vec)[:int((dataRatio * vecLength))]
    vocabulary = vocabulary[:int((dataRatio * vecLength))]

    tsne = TSNE(n_components=2, random_state=0)
    yTransform = tsne.fit_transform(vec)
    print("New Size: " + str(model.vectors.shape))

    tags = set(['JJ', 'NNP', 'NN', 'NNS'])
    puncts = ["'", ".", ":", ";", ",", "?", "!", u"’"]
    plt.figure()
    texts = []
    nltk.download(
        ['averaged_perceptron_tagger', 'maxent_treebank_pos_tagger', 'punkt'])

    for i, label in enumerate(vocabulary):
        pos = nltk.pos_tag([label])
        if (label[0].isupper() and len(label) > 1 and pos[0][1] in tags
                and all(c not in label for c in puncts)):
            x, y = yTransform[i, :]
            texts.append(plt.text(x, y, label))
            plt.scatter(x, y)

    adjust_text(texts, arrowprops=dict(arrowstyle='-', color='k', lw=1.5))

    plt.savefig('hp_result.png', dpi=600)
    #plt.show()
Esempio n. 36
0

#%%
# d_sim = percept_loss.forward(resz_ref_img, resz_out_img)
zcode, fitimg = img_backproj(target_img, percept_loss)
# zcode, fitimg = img_backproj(target_img, L1loss)

#%%
for nsteps in [1000]:  #%150, 250, 500,
    zcode, fitimg = img_backproj(target_img, percept_loss, nsteps=nsteps)
    label = "alexPL_step%d" % (nsteps)
    plt.figure(figsize=[4, 4.5])
    plt.imshow(fitimg.cpu().numpy())
    plt.title(label)
    plt.axis("off")
    plt.savefig(join(savedir, "Embed_%s.jpg" % label))
    plt.show()

#%%
percept_net = models.PerceptualLoss(model='net-lin',
                                    net='vgg',
                                    use_gpu=1,
                                    gpu_ids=[0])


def percept_loss(img, target, net=percept_net):
    return net.forward(
        img.unsqueeze(0).permute([0, 3, 1, 2]),
        target.unsqueeze(0).permute([0, 3, 1, 2]))

Esempio n. 37
0
from sklearn.naive_bayes import MultinomialNB
from os.path import join
# %pylab inline

plt.rcParams['text.usetex'] = True

pos = norm(loc=3, scale=2.5)
neg = norm(loc=-0.5, scale=0.75)
plt.clf()
for ins, color in zip([pos, neg], ['b', 'r']):
    x = np.linspace(ins.ppf(0.001), ins.ppf(0.999))
    plt.plot(x, ins.pdf(x), color)
plt.legend(['Positive', 'Negative'])
plt.grid()
plt.tight_layout()
plt.savefig('two_normals.png', dpi=300)

_min = neg.ppf(0.05)
_max = neg.ppf(0.95)
D = [(x, 0) for x in neg.rvs(100) if x >= _min and x <= _max]
D += [(x, 1) for x in pos.rvs(1000) if x < _min or x > _max]

plt.clf()
plt.plot([x for x, k in D if k == 1], pos.pdf([x for x, k in D if k == 1]),
         'b.')
plt.plot([x for x, k in D if k == 0], neg.pdf([x for x, k in D if k == 0]),
         'r.')
plt.grid()
plt.legend(['Positive', 'Negative'])
plt.tight_layout()
plt.savefig('two_normal_samples.png', dpi=300)
Esempio n. 38
0
 for stock in stocks:
     print(stock)
     dir_path = './result/%s' % stock
     line = '頻度_rate, 日数, 高騰正解数, 高騰正答数, 下落正解数, 下落正答数, 高騰正答率(%), 下落正答率(%)\n'
     for l in day_length_list:
         with open('%s/result_%dd.csv' % (dir_path, l), 'r') as f:
             lines = [line.strip() for line in f][1:]
         actual_list = [float(line.split(',')[1]) for line in lines]
         pred_list = [float(line.split(',')[2]) for line in lines]
         plt.figure()
         plt.title('stock_close/topix_close')
         plt.plot(actual_list, label='actual')
         plt.plot(pred_list, label='predict')
         plt.legend()
         filename = '%s/result_graph%dd.png' % (dir_path, l)
         plt.savefig(filename)
         values = [
             (actual_list[idx] - actual_list[idx - l]) / actual_list[idx]
             for idx in range(l, len(actual_list))
         ]
         r_corrects = [1 if val > rate else 0 for val in values]
         values = [(pred_list[idx] - pred_list[idx - l]) / pred_list[idx]
                   for idx in range(l, len(pred_list))]
         prediction = [1 if val > rate else 0 for val in values]
         #            for idx in range(len(prediction)): print('%d, %d'%(r_corrects[idx], prediction[idx]))
         raise_correct = sum(item for item in r_corrects)
         count_raise = sum(1 if r_corrects[idx] == prediction[idx] else 0
                           for idx in range(len(prediction)))
         values = [
             (actual_list[idx] - actual_list[idx - l]) / actual_list[idx]
             for idx in range(l, len(actual_list))
Esempio n. 39
0
import numpy as np
import matplotlib.pylab as plt

Datos = np.genfromtxt("datos.dat", delimiter=";")
x = Datos[:, 0]
a = Datos[:, 1]
Datos2 = np.genfromtxt("datos2.dat", delimiter=";")
x2 = Datos2[:, 0]
a2 = Datos2[:, 1]
Datos3 = np.genfromtxt("datos3.dat", delimiter=";")
x3 = Datos3[:, 0]
a3 = Datos3[:, 1]
plt.figure()
plt.plot(x, a, label="Inicial", c="red")
plt.plot(x2, a2, label="arrAf", c="blue")
plt.plot(x3, a3, label="arrf", c="green")
plt.xlabel("X")
plt.ylabel("A")
plt.legend()
plt.savefig("ArevaloSergioS6C2.png")
Esempio n. 40
0
def plot_angular_momentum_size_velocity():

    size = 100000
    # Planck parameters
    h, Om, OL = 0.677, 0.31, 0.69

    # uniform halo mass function
    mhalo = array(10.**uniform(10.75, 14.5, size), dtype=float32)

    # read halo mass-function
    # mhf = genfromtxt('mVector_PLANCK-SMT_11-13.txt')
    # mhf = genfromtxt('mVector_PLANCK-SMT_10.5-13.txt')
    mhf = genfromtxt('mVector_PLANCK-SMT_11-14.5.txt')
    # mhalo = array(choice(mhf[:, 0], p=mhf[:, 5]/sum(mhf[:, 5]), size=size), dtype=float32)
    '''
    Ms = Mstar(0., mode='low', model='B10')
    mstar = []

    print "generating mstar..."
    for m in mhalo:
        mstar.append(Ms(m))
    mstar = array(mstar)
    '''

    mstar = Mstar_vU(mhalo, mode='TF')

    ms_lowlim, ms_highlim = 7., 11.75
    mhalo = mhalo[(log10(array(mstar)) > ms_lowlim)
                  & (log10(array(mstar)) < ms_highlim)]
    mstar = mstar[(log10(array(mstar)) > ms_lowlim)
                  & (log10(array(mstar)) < ms_highlim)]

    # DM halo spin parameter distribution
    # from Maccio', Dutton & van den Bosch (2008)
    lambda_halo = 10.**normal(-1.466, 0.253, len(mhalo))

    # bt = get_bt_distribution_from_SDSS(mstar)
    bt = get_bt_distribution_from_meert14(mstar)
    # bt, mstar, mhalo, lambda_halo = get_bt_distribution_from_lambda(mstar, mhalo, lambda_halo)

    # Dutton & Maccio' (2014)
    # b = -0.101 + 0.026 * array([0., 1.])
    # a = 0.52 + (0.905 - 0.52) * exp(-0.617 * array([0., 1.]) ** 1.21)
    b = -0.097 + 0.024 * array([0., 1.])
    a = 0.537 + (1.025 - 0.537) * exp(-0.718 * array([0., 1.])**1.08)
    # scatter 0.11dex
    cvir = []
    print "generating concentrations..."
    for m in mhalo:
        cvir.append(10.**normal(a[0] + b[0] * log10(m / (1e12 / h)), 0.11,
                                1)[0])
    cvir = array(cvir)

    # Circular velocity for NFW haloes
    G = 4.302e-3 * 1e-6  # Mpc Mo^-1 (km/s)^2
    f = lambda x: log(1. + asarray(x)) - asarray(x) / (1. + asarray(x))
    rho_c = 3. * (h * 100.)**2 / (8. * pi * G)
    # Bryan & Norman (1998)
    Oz = lambda z: Om * (1 + z)**3 / (Om * (1. + z)**3 + OL)
    Delta_c = lambda z: 18. * pi**2 + 82. * (Oz(z) - 1.) - 39. * (Oz(z) - 1.
                                                                  )**2
    rho_hat = 4. / 3. * pi * Delta_c(0.) * rho_c

    vc = sqrt(G * f(2.15) / f(cvir) * cvir / 2.15 *
              pow(rho_hat, 1. / 3.)) * power(mhalo / h, 1. / 3.)

    # print "%e %e" % (f(10.) * 2.15 / (f(2.15) * 10.), sqrt(2. * f(10.) * 2.15 / (f(2.15) * 10.)))

    # Kravtsov 2013
    # rho_200 = 4. / 3. * pi * 200. * rho_c
    r200 = 1e3 * power(mhalo / rho_hat, 1. / 3.)
    rs = []
    for r in r200:
        rs.append(10.**normal(log10(0.015 * r), 0.25, 1)[0])
    rs = array(rs)
    js = rs * vc

    # jhalo - mhalo
    fe = cvir / 2. * (1. - 1. / power(1. + cvir, 2) - 2. * log(1. + cvir) /
                      (1. + cvir)) / power(cvir /
                                           (1. + cvir) - log(1. + cvir), 2)
    # fe = f(2.15) / f(cvir) * cvir / 2.15
    j_halo = sqrt(2. / fe) * lambda_halo * sqrt(G * 1e3 * mhalo / r200) * r200
    # j_halo da Romanowsky & Fall (2012) eq. (14)
    # j_halo = 4.23e4 * lambda_halo * power(mhalo / 1e12, 2./3.)

    # bt_discs, bt_spirals, bt_ltg, bt_lents, bt_ells = 0.1, 0.2, 0.5, 0.8, 0.95
    bt_discs, bt_spirals, bt_ltg, bt_lents, bt_ells = 0.2, 0.4, 0.5, 0.65, 0.8

    # fj(Mhalo)
    def jstar_FR(mass, bf):
        if (bf >= 0.) & (bf <= bt_discs):
            # Sc
            j0, alpha, s = 3.29, 0.55, 0.18
        elif (bf > bt_discs) & (bf <= bt_spirals):
            # Sb
            j0, alpha, s = 3.21, 0.68, 0.15
        elif (bf > bt_spirals) & (bf <= bt_ltg):
            # Sa
            j0, alpha, s = 3.02, 0.64, 0.12
        elif (bf > bt_ltg) & (bf <= bt_lents):
            # S0
            j0, alpha, s = 3.05, 0.8, 0.22
        elif (bf > bt_lents) & (bf <= bt_ells):
            # fE
            j0, alpha, s = 2.875, 0.6, 0.2
        elif (bf > bt_ells) & (bf <= 1.):
            # sE
            j0, alpha, s = 2.73, 0.6, 0.2
        else:
            print bf
            raise ValueError("Problem in bt not in ]0,1]")

        return 10.**normal(j0 + alpha * (mass - 11.), s, 1)[0]

    # fitting function to jb/jd estimated from Romanowsky & Fall (2012) data
    # jb/jd = 0.025 + B/T^2
    # jd = j_star / (1 + (jb/jd-1)B/T)
    # --> here jdisc = jstar / fb_func
    jb_over_jd_fit = lambda fb: 0.025 + fb**2
    fb_func = lambda fb: 1. + (jb_over_jd_fit(fb) - 1.) * fb

    jstar, jdisc, jbulge = [], [], []
    print "generating jstar..."
    for i in range(len(mstar)):
        jstar.append(jstar_FR(log10(mstar[i]), bt[i]))
        jdisc.append(jstar[i] / fb_func(bt[i]))
        jbulge.append(jdisc[i] * jb_over_jd_fit(bt[i]))
    jstar, jdisc, jbulge = array(jstar), array(jdisc), array(jbulge)
    fj = jstar / j_halo  # jstar_FR(log10(0.158 * mhalo)) / j_halo

    # Kravtsov
    Mstar_TF = lambda t: 10.**(-0.61 + 4.93 * asarray(t)
                               )  # McGaugh & Schombert (2015), sTF @ 3.6um
    vstar_TF = lambda t: 10.**(0.61 / 4.93 + 1. / 4.93 * asarray(t))

    sstar_FJ = lambda t: 10.**(2.054 + 0.286 * asarray(t - 10.))
    Mstar_FJ = lambda t: 10.**(-2.054 / 0.286 + 10. + 1. / 0.286 * asarray(t))

    # Re = jdisc / (vstar_TF(log10(mstar)))
    # Re[bt > 0.8] = jbulge[bt > 0.8] / (0.7 * vstar_TF(log10(mstar[bt > 0.8])))
    # Re[bt > 0.8] = jbulge[bt > 0.8] / (10.**(-1.06 + 0.32*log10(mstar[bt > 0.8])))
    Re = jstar / vc  # vstar_TF(log10(mstar))
    # Re[bt > bt_ltg] = jstar[bt > bt_ltg] / (1.65 * sstar_FJ(log10(mstar[bt > bt_ltg])))

    # Mo, Mao & White (1998)
    # lambda_halo_prime = lambda_halo * jdisc / j_halo  # fj
    lambda_halo_prime = lambda_halo * fj  # fj
    cvir = array(cvir)
    fac_sis_nfw = 1. / sqrt(
        cvir / 2. * (1. - 1. / power(1. + cvir, 2) - 2. * log(1. + cvir) /
                     (1. + cvir)) / power(cvir /
                                          (1. + cvir) - log(1. + cvir), 2))
    # md = mstar / mhalo
    # fac_sis_nfw *= pow(lambda_halo_prime / 0.1, -0.0 + 2.71 * md + 0.0047 / lambda_halo_prime) * \
    #                (1-3.*md+5.2*md*md) * (1-0.019*cvir * 0.00025*cvir*cvir + 0.52 / cvir)
    Rd = 1. / sqrt(2.) * lambda_halo_prime * r200 * fac_sis_nfw
    """
    HDF5 file output
    """
    f = h5py.File("jstar-jhalo.hdf5", 'w')

    group = f.create_group("columns")

    dset_lambda = group.create_dataset("Lambda_Halo", data=lambda_halo)
    dset_mhalo = group.create_dataset("Halo_Mass", data=mhalo)
    dset_mstar = group.create_dataset("Galaxy_Mass", data=mstar)
    dset_jhalo = group.create_dataset("Halo_Specific_Angular_Momentum",
                                      data=j_halo)
    dset_jstar = group.create_dataset("Galaxy_Specific_Angular_Momentum",
                                      data=jstar)
    dset_cvir = group.create_dataset("Halo_Concentration", data=cvir)
    dset_fj = group.create_dataset("Retained_fraction_of_j", data=fj)
    dset_bt = group.create_dataset("Bulge_Fraction", data=bt)
    dset_vc = group.create_dataset("Circular_Velocity", data=vc)
    dset_vc = group.create_dataset("Velocity_Dispersion", data=vc / 1.1)
    dset_Re = group.create_dataset("Effective_Radius", data=Re)
    dset_Rd = group.create_dataset("Disc_Scale_Radius", data=Rd)
    """
    Plots
    """

    # plt.plot(log10(mstar * bt), log10(jbulge), 'r.', alpha=0.005)
    # plt.plot(log10(mstar * bt * (1./bt - 1.)), log10(jdisc), 'b.', alpha=0.005)
    # plt.plot(linspace(9, 12), 3.28+0.67*(linspace(9, 12)-11.), 'k-', lw=3)
    # plt.plot(linspace(9, 12), 2.75+0.67*(linspace(9, 12)-11.), 'k--', lw=3)
    # plt.show()

    size_hist = 20
    sigs = [68., 90.]
    save_plots = False
    '''
    ----------- Mstar-Mhalo
    '''
    # Dutton et al. 2010 relations
    d10_msmh_ltg = lambda x: 10.**1.61 * (asarray(x) / 10.**10.4)**-0.5 * (
        0.5 + 0.5 * (asarray(x) / 10.**10.4))**0.5
    d10_msmh_etg = lambda x: 10.**1.97 * (asarray(x) / 10.**10.8)**-0.15 * (
        0.5 + 0.5 * (asarray(x) / 10.**10.8)**2)**0.5
    d10_ms_ltg = logspace(9.35, 11)
    d10_ms_etg = logspace(9.85, 11.4)

    fig = plt.figure()
    ax = fig.add_subplot(211)
    ax.set_ylabel(r"$\log\rm\,M_\ast/M_\odot$", fontsize=16)
    # ax.xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16)
    ax.axes.get_xaxis().set_visible(False)
    ax.set_ylim([8, 11.75])
    ax.set_xlim([10.75, 13.5])

    x, y, H, lev, _, _, _ = bins_and_hist(mhalo, mstar, bt > 0, 30, sigs)
    # plt.plot(log10(d10_msmh_ltg(d10_ms_ltg) * d10_ms_ltg), log10(d10_ms_ltg), 'b-')
    # plt.plot(log10(d10_msmh_etg(d10_ms_etg) * d10_ms_etg), log10(d10_ms_etg), 'r-')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Greys'),
                 alpha=0.75)
    plt.xlim()

    ax2 = fig.add_subplot(212, sharex=ax)
    ax2.set_ylabel(r"$\log\rm\,M_\ast/M_\odot$", fontsize=16)
    ax2.set_xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16)
    ax2.set_xlim([10.75, 13.5])
    fig.subplots_adjust(hspace=0)

    x, y, H, lev, _, _, _ = bins_and_hist(mhalo, mstar / mhalo, bt > 0, 30,
                                          sigs)
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Greys'),
                 alpha=0.75)

    if save_plots:
        plt.savefig('SHMR_all_ref.pdf', bbox_inches='tight')
    '''
    ----------- lambda-Mhalo
    '''
    fig = plt.figure()
    plt.xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16)
    plt.ylabel(r"$\log\rm\lambda_{halo}$", fontsize=16)
    plt.plot(log10(mhalo), log10(lambda_halo), 'b.', alpha=0.0025)
    x, y, H, lev, _, _, _ = bins_and_hist(mhalo, lambda_halo, bt > 0,
                                          size_hist, sigs)
    plt.contour(x, y, log10(H).T, levels=lev, colors='#151AB0')
    '''
    ----------- j_halo - M_halo
    '''
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plt.xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16)
    plt.ylabel(r"$\log\rm\,j_h/km\,s^{-1}\,kpc$", fontsize=16)
    x, y, H, lev, _, _, _ = bins_and_hist(mhalo, j_halo, bt > 0, size_hist,
                                          sigs)
    # ax.contour(x, y, log10(H).T, levels=lev, colors='#151AB0')
    ax.contourf(x,
                y,
                log10(H).T,
                levels=append(lev,
                              log10(H).max()),
                cmap=plt.get_cmap('Greys'),
                alpha=0.75)

    ax.plot(linspace(10.75, 13),
            3.28 + 0.67 * (linspace(10.75, 13) - 11.),
            'k--',
            lw=3)
    # plt.plot([12.5, log10(0.158 * 10.**12.5)], [3.505, 3.7481], 'm-', lw=3)
    # ax.arrow(12.5, 3.505, -12.5+log10(0.08 * 10.**12.5), -3.505+3.28+0.67*(log10(0.08 * 10.**12.5)-11.)-0.045,
    #          head_width=0.075, head_length=0.05, fc='m', ec='m', lw=3)
    ax.arrow(12.,
             3.1704,
             -12. + log10(0.08 * 10.**12.),
             -3.1704 + 3.28 + 0.67 * (log10(0.08 * 10.**12.) - 11.) - 0.045,
             head_width=0.075,
             head_length=0.05,
             fc='m',
             ec='m',
             lw=3)

    ax.text(11.15,
            4.,
            r"$f_{\rm baryon}\simeq 0.16$" + "\n" + r"$M_\ast=M_{\rm bar}/2$",
            color='m',
            fontsize=18)

    if save_plots:
        plt.savefig('jh-Mh_all_ref.pdf', bbox_inches='tight')
    '''
    ----------- fj(M_halo)
    '''
    fig = plt.figure()
    plt.xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16)
    plt.ylabel(r"$\log\rm\,f_j(M_h)\equiv j_\ast / j_h$", fontsize=16)
    ax = fig.add_subplot(111)
    ax.text(0.1,
            0.9,
            r"$\rm Sc-Sb$",
            fontsize=18,
            color='#151AB0',
            transform=ax.transAxes)
    ax.text(0.1,
            0.85,
            r"$\rm Sa-S0$",
            fontsize=18,
            color='#009603',
            transform=ax.transAxes)
    ax.text(0.1,
            0.8,
            r"$\rm Es$",
            fontsize=18,
            color='#BD000D',
            transform=ax.transAxes)

    # spirals
    w = bt < bt_spirals
    # plt.plot(log10(mhalo[w]), log10(fj[w]), 'b.', alpha=0.0025)
    x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(
        mhalo, fj, w, size_hist, sigs)
    # plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='b', fmt='o')
    # plt.contour(x, y, log10(H).T, levels=lev, colors='#151AB0')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Blues'),
                 alpha=0.75)

    # Sa and lenticulars
    w = (bt >= bt_spirals) & (bt < bt_lents)
    # plt.plot(log10(mhalo[w]), log10(fj[w]), 'b.', alpha=0.0025)
    x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(
        mhalo, fj, w, size_hist, sigs)
    # plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='g', fmt='o')
    # plt.contour(x, y, log10(H).T, levels=lev, colors='#AB9700')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Greens'),
                 alpha=0.75)

    # ellipticals
    w = bt > bt_lents
    # plt.plot(log10(mhalo[w]), log10(fj[w]), 'r.', alpha=0.0025)
    x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(
        mhalo, fj, w, size_hist, sigs)
    # plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='r', fmt='o')
    # plt.contour(x, y, log10(H).T, levels=lev, colors='#BD000D')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Reds'),
                 alpha=0.75)
    plt.xlim([10.7, 13.5])

    # ------ Median relation
    x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(mhalo,
                                                              fj,
                                                              bt > 0,
                                                              size_hist,
                                                              sigs,
                                                              bin_size=30)
    plt.errorbar(log10(mhalo_bin),
                 log10(fj_bin),
                 yerr=abs(log10(fj_bin) - log10(e_fj_bin)),
                 c='k',
                 fmt='o')

    if save_plots:
        plt.savefig('fj-Mh_all_ref.pdf', bbox_inches='tight')
    '''
    ----------- Kravtsov
    '''
    fig = plt.figure()
    plt.ylabel(r"$\log\rm\,R_e/kpc$", fontsize=16)
    plt.xlabel(r"$\log\rm\,r_{\rm vir}/kpc$", fontsize=16)
    ax = fig.add_subplot(111)
    ax.text(0.1,
            0.9,
            r"$\rm Sc-Sb$",
            fontsize=18,
            color='#151AB0',
            transform=ax.transAxes)
    ax.text(0.1,
            0.85,
            r"$\rm Sa-S0$",
            fontsize=18,
            color='#009603',
            transform=ax.transAxes)
    ax.text(0.1,
            0.8,
            r"$\rm Es$",
            fontsize=18,
            color='#BD000D',
            transform=ax.transAxes)
    # ax.text(0.4, 0.15, r"$\rm Kravtsov Plot$", fontsize=20, transform=ax.transAxes)

    # spirals
    w = bt < bt_spirals
    # plt.plot(log10(r200)[w], log10(Re)[w], 'b.', alpha=0.0075)
    x, y, H, lev, _, _, _ = bins_and_hist(r200, Re, w, size_hist, sigs)
    # plt.contour(x, y, log10(H).T, levels=lev, colors='#151AB0')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Blues'),
                 alpha=0.75)

    # Sa and lenticulars
    w = (bt >= bt_spirals) & (bt < bt_lents)
    # plt.plot(log10(r200)[w], log10(Re)[w], 'b.', alpha=0.0075)
    x, y, H, lev, _, _, _ = bins_and_hist(r200, Re, w, size_hist, sigs)
    # plt.contour(x, y, log10(H).T, levels=lev, colors='#AB9700')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Greens'),
                 alpha=0.75)

    # ellipticals
    w = bt > bt_lents
    # plt.plot(log10(r200)[w], log10(Re)[w], 'r.', alpha=0.0075)
    x, y, H, lev, _, _, _ = bins_and_hist(r200, Re, w, size_hist, sigs)
    # plt.contour(x, y, log10(H).T, levels=lev, colors='#BD000D')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Reds'),
                 alpha=0.75)

    plt.plot(log10(linspace(110, 800)),
             log10(0.015 * linspace(110, 800)),
             'k-',
             lw=2)
    plt.plot(log10(linspace(110, 800)),
             log10(0.015 * linspace(110, 800)) + 0.5,
             'k--',
             lw=2)
    plt.plot(log10(linspace(110, 800)),
             log10(0.015 * linspace(110, 800)) - 0.5,
             'k--',
             lw=2)
    plt.xlim([log10(110.), log10(800.)])

    # ----- Median relation
    x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(r200,
                                                             Re,
                                                             bt > 0,
                                                             size_hist,
                                                             sigs,
                                                             bin_size=30)
    plt.errorbar(log10(r200_bin),
                 log10(Rd_bin),
                 yerr=abs(log10(Rd_bin) - log10(e_Rd_bin)),
                 c='k',
                 fmt='o')

    if save_plots:
        plt.savefig('Re-r200_all_ref.pdf', bbox_inches='tight')
    '''
    ----------- Mo, Mao & White Rd-r200
    '''
    fig = plt.figure()
    plt.ylabel(r"$\log\rm\,R_d/kpc$", fontsize=16)
    plt.xlabel(r"$\log\rm\,r_{\rm vir}/kpc$", fontsize=16)
    ax = fig.add_subplot(111)
    ax.text(0.1,
            0.9,
            r"$\rm Sc-Sb$",
            fontsize=18,
            color='#151AB0',
            transform=ax.transAxes)
    ax.text(0.1,
            0.85,
            r"$\rm Sa-S0$",
            fontsize=18,
            color='#009603',
            transform=ax.transAxes)
    # ax.text(0.4, 0.15, r"$\rm Mo, Mao & White Plot$", fontsize=20, transform=ax.transAxes)

    # spirals
    w = bt < bt_spirals
    # plt.plot(log10(r200[w]), log10(Rd[w]), 'b.', alpha=0.01)
    x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(
        r200, Rd, w, size_hist, sigs)
    # plt.errorbar(log10(r200_bin), log10(Rd_bin), yerr=abs(log10(Rd_bin)-log10(e_Rd_bin)), c='b', fmt='o')
    # plt.contour(x, y, log10(H).T, levels=lev, colors='#151AB0')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Blues'),
                 alpha=0.75)

    # Sa and lenticulars
    w = (bt >= bt_spirals) & (bt < bt_lents)
    # plt.plot(log10(r200[w]), log10(Rd[w]), 'y.', alpha=0.01)
    x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(
        r200, Rd, w, size_hist, sigs)
    # plt.errorbar(log10(r200_bin), log10(Rd_bin), yerr=abs(log10(Rd_bin)-log10(e_Rd_bin)), c='g', fmt='o')
    # plt.contour(x, y, log10(H).T, levels=lev, colors='#AB9700')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Greens'),
                 alpha=0.75)
    plt.plot(log10(linspace(110, 800)),
             log10(0.0112 * linspace(110, 800)),
             'k-',
             lw=2)
    plt.xlim([log10(110.), log10(800.)])

    x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(r200,
                                                             Rd,
                                                             bt < bt_lents,
                                                             size_hist,
                                                             sigs,
                                                             bin_size=30)
    plt.errorbar(log10(r200_bin),
                 log10(Rd_bin),
                 yerr=abs(log10(Rd_bin) - log10(e_Rd_bin)),
                 c='k',
                 fmt='o')

    if save_plots:
        plt.savefig('Rd-r200_ScS0_ref.pdf', bbox_inches='tight')
    '''
    ----------- sTFR
    '''
    fig = plt.figure()
    plt.ylabel(r"$\log\rm\,M_\ast/M_\odot$", fontsize=16)
    plt.xlabel(r"$\log\rm\,V_{max}/km\,s^{-1}$", fontsize=16)
    plt.ylim(8, 11.5)
    ax = fig.add_subplot(111)
    ax.text(0.1,
            0.9,
            r"$\rm Sc-Sb$",
            fontsize=18,
            color='#151AB0',
            transform=ax.transAxes)

    # spirals
    w = bt < bt_spirals
    # plt.plot(log10(vc[w]), log10(mstar[w]), 'b.', alpha=0.0025)
    x, y, H, lev, vc_bin, mstar_bin, e_mstar_bin = bins_and_hist(
        vc, mstar, w, size_hist, sigs)
    # plt.errorbar(log10(vc_bin), log10(mstar_bin), yerr=abs(log10(mstar_bin)-log10(e_mstar_bin)), c='b', fmt='o')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Blues'))
    plt.plot(linspace(1.9, 2.4),
             log10(Mstar_TF(linspace(1.9, 2.4))),
             'k-',
             lw=3)
    plt.plot(linspace(1.9, 2.4),
             log10(Mstar_TF(linspace(1.9, 2.4))) + 0.15,
             'k--',
             lw=3)
    plt.plot(linspace(1.9, 2.4),
             log10(Mstar_TF(linspace(1.9, 2.4))) - 0.15,
             'k--',
             lw=3)

    if save_plots:
        plt.savefig('sTF_Sc_ref.pdf', bbox_inches='tight')
    '''
    ----------- Faber-Jackson
    '''
    fig = plt.figure()
    plt.ylabel(r"$\log\rm\,M_\ast/M_\odot$", fontsize=16)
    plt.xlabel(r"$\log\rm\,\sigma/km\,s^{-1}$", fontsize=16)
    plt.ylim(9, 11.75)
    ax = fig.add_subplot(111)
    ax.text(0.1,
            0.9,
            r"$\rm Es$",
            fontsize=18,
            color='#BD000D',
            transform=ax.transAxes)

    factor = 1.1  # V_circ(R_50) := factor * sigma(R_50)
    # ellipticals
    w = bt > bt_lents
    # turn vc to sigma for ellipticals
    vc[w] /= factor
    # Dutton et al. 2010 correction
    # (vc[w])[log10(mstar[w]) > 10.5] *= 10.**(0.1-0.3*(log10(mstar[w])[log10(mstar[w]) > 10.5]-10.5))
    # (vc[w])[log10(mstar[w]) <= 10.5] *= 10.**0.1
    # plt.plot(log10(mstar[w]), log10(vc[w]), 'r.', alpha=0.0025)
    x, y, H, lev, vc_bin, mstar_bin, e_mstar_bin = bins_and_hist(
        vc, mstar, w, size_hist, sigs)
    # plt.errorbar(log10(vc_bin), log10(mstar_bin), yerr=abs(log10(mstar_bin)-log10(e_mstar_bin)), c='r', fmt='o')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Reds'))
    plt.plot(linspace(1.8, 2.6),
             log10(Mstar_FJ(linspace(1.8, 2.6))),
             'k-',
             lw=3)
    plt.plot(linspace(1.8, 2.6),
             log10(Mstar_FJ(linspace(1.8, 2.6))) + 0.1,
             'k--',
             lw=3)
    plt.plot(linspace(1.8, 2.6),
             log10(Mstar_FJ(linspace(1.8, 2.6))) - 0.1,
             'k--',
             lw=3)

    if save_plots:
        plt.savefig('FJ_Es_ref.pdf', bbox_inches='tight')
    '''
    ----------- FP
    '''
    fig = plt.figure()
    plt.xlabel(r"$\log\rm\,M_\ast/kpc$", fontsize=16)
    plt.ylabel(r"$\rm\,10.6+2\log\,\sigma/130\,km\,s^{-1}+\log\,R_e/2\,kpc$",
               fontsize=16)
    plt.xlim(9, 11.75)
    ax = fig.add_subplot(111)
    ax.text(0.1,
            0.825,
            r"$\rm Es$",
            fontsize=18,
            color='#BD000D',
            transform=ax.transAxes)

    # etg
    w = bt > bt_lents
    # fp
    fp = 10.6 + 2. * log10(vc[w] / factor / 130.) + log10(Re[w] / 2.)
    x, y, H, lev, mstar_bin, fp_bin, e_fp_bin = bins_and_hist(
        mstar[w], 10.**fp, mstar[w] > 0, size_hist, sigs)
    # plt.errorbar(log10(mstar_bin), fp_bin, yerr=0.5, c='r', fmt='o')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Reds'))
    plt.plot(linspace(9, 12), linspace(9, 12), 'k-', lw=3)
    plt.plot(linspace(9, 12), linspace(9, 12) + 0.06, 'k--', lw=3)
    plt.plot(linspace(9, 12), linspace(9, 12) - 0.06, 'k--', lw=3)

    if save_plots:
        plt.savefig('FP_Es_ref.pdf', bbox_inches='tight')
    '''
    ----------- Mass - size relation
    '''
    fig = plt.figure()
    plt.xlabel(r"$\log\rm\,M_\ast/kpc$", fontsize=16)
    plt.ylabel(r"$\log\rm\,R_e/kpc$", fontsize=16)
    plt.xlim(8, 11.75)
    ax = fig.add_subplot(111)
    ax.text(0.1,
            0.9,
            r"$\rm Sc-Sb-Sa$",
            fontsize=18,
            color='#151AB0',
            transform=ax.transAxes)
    ax.text(0.1,
            0.825,
            r"$\rm S0-Es$",
            fontsize=18,
            color='#BD000D',
            transform=ax.transAxes)

    # spirals
    w = bt < bt_ltg
    # plt.plot(log10(r200)[w], log10(Re)[w], 'b.', alpha=0.0075)
    x, y, H, lev, mstar_bin, Re_bin, e_Re_bin = bins_and_hist(
        mstar, Re, w, size_hist, sigs)
    # plt.errorbar(log10(mstar_bin), log10(Re_bin), yerr=abs(log10(Re_bin)-log10(e_Re_bin)), c='b', fmt='o')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Blues'),
                 alpha=0.75)

    # ellipticals
    w = bt > bt_ltg
    # plt.plot(log10(r200)[w], log10(Re)[w], 'r.', alpha=0.0075)
    x, y, H, lev, mstar_bin, Re_bin, e_Re_bin = bins_and_hist(
        mstar, Re, w, size_hist, sigs)
    # plt.errorbar(log10(mstar_bin), log10(Re_bin), yerr=abs(log10(Re_bin)-log10(e_Re_bin)), c='r', fmt='o')
    plt.contourf(x,
                 y,
                 log10(H).T,
                 levels=append(lev,
                               log10(H).max()),
                 cmap=plt.get_cmap('Reds'),
                 alpha=0.75)

    mass_size_etg = lambda mass: -5.54061 + 0.56 * log10(mass)
    mass_size_ltg = lambda mass: -1. + 0.14 * log10(mass) + 0.25 * log10(
        1. + mass / 3.98e10)
    plt.plot(linspace(9, 11.75), mass_size_etg(logspace(9, 11.75)), 'r-', lw=2)
    plt.plot(linspace(9, 11.75),
             mass_size_ltg(logspace(9, 11.75)),
             'b--',
             lw=2)

    if save_plots:
        plt.savefig('Re-Ms_all_ref.pdf', bbox_inches='tight')

    plt.show()
Esempio n. 41
0
plt.subplot(3, 1, 2)
plt.plot(t, y)
plt.plot(t, y_sol)
plt.xticks(x1, xticks)
plt.yticks(y1, yticks)
plt.ylabel("Y [Km]")

plt.subplot(3, 1, 3)
plt.plot(t, z)
plt.plot(t, z_sol)
plt.xticks(x1, xticks)
plt.yticks(y1, yticks)
plt.ylabel("Z [Km]")

plt.title("Posición en los ejes de coordenadas")
plt.xlabel("Tiempo, t [horas]")

plt.tight_layout()
plt.savefig('grafico_xyz.png')
plt.show()

# Grafico 2
plt.plot(t, lista)

plt.title("Distancia entre posicion real y predicha, δ = 597.9 [km]")
plt.xlabel('Tiempo, t [h]')
plt.ylabel("Deriva [Km]")
plt.xticks(x1, xticks)
plt.savefig('grafico_deriva.png')
plt.show()
Esempio n. 42
0
import numpy as np
import matplotlib.pylab as plt

#import the data
data = np.transpose(np.loadtxt('data.txt', skiprows=1))

LL = data[0]
energy = data[0] / 8 * .662
counts = data[2]

#----- Plot Lower level vs Counts -----
plt.plot(data[0], counts, 'bo')
plt.xlabel("Lower Level +/- .02")
plt.ylabel("counts +/- 1")
plt.title("CS 137 Radiation Distrobution")
plt.savefig("LL_v_Counts.png")
plt.close()
#----------

#----- plot Energy vs counts -----
plt.plot(energy, counts, 'bo')
plt.xlabel("Energy +/- .003 (MeV)")
plt.ylabel("counts +/- 1")
plt.title("CS 137 Radiation Distrobution")
plt.savefig("Energy_v_Counts.png")
plt.close()
#----------

#computed fit parameters
a1 = 1545
a2 = .658
areas = [17, 18, 19, 37, 20]

# separate plot for each category
for cid, category in enumerate(categories):

    print 'Drawing "%s" ...' % category
    importance = np.load('%s/%s' % (INDIR, 'FT_feature_importances_ctg%d.npy' % cid))
    successful_probes = np.load('%s/%s' % (INDIR, 'FT_successful_probes_ctg%d.npy' % cid))
    successful_mnis = np.load('%s/%s' % (INDIR, 'FT_successful_mnis_ctg%d.npy' % cid))
    successful_areas = np.load('%s/%s' % (INDIR, 'FT_successful_areas_ctg%d.npy' % cid))

    # drop 0,0,0 electrode if it creeps in
    drop_idx = np.unique(np.where(successful_mnis == [0, 0, 0])[0])
    importance = np.delete(importance, drop_idx, 0)
    successful_probes = np.delete(successful_probes, drop_idx, 0)
    successful_mnis = np.delete(successful_mnis, drop_idx, 0)
    successful_areas = np.delete(successful_areas, drop_idx, 0)

    # Mean over each BA
    for ba in areas:
        if importance[successful_areas == ba, :, :].shape[0] > 0:
            fig = plt.figure(figsize=(10, 8), dpi=300);
            title = 'Temporal profile of importance for "%s"\nmean over %d probes in BA%d' % (categories[cid], np.sum(successful_areas == ba), ba)
            panel_time_profile(np.sum(np.mean(importance[successful_areas == ba, :, :], 0), 0), title, True)
            plt.savefig('%s/time_profile_%d_%s_BA%d.png' % (OUTDIR, cid, category, ba), bbox_inches='tight');
            plt.clf();
            plt.close(fig);

    
        
Esempio n. 44
0
global data
data = load_images('train-images.idx3-ubyte')


def pca(i):
    means = mean(data, axis=0)
    new_data = data - means
    covMat = np.cov(new_data.T)
    eigVals, eigVects = np.linalg.eig(covMat)
    n_eigValIndice = argsort(-eigVals)
    selectedfeature = np.matrix(eigVects.T[n_eigValIndice[:i]])
    finalData = new_data * selectedfeature.T
    finalData = finalData.real
    reconMat = (finalData * selectedfeature) + means
    return reconMat, new_data


mse = []
length = 1
for j in range(1, 784, length):
    rec, data_aftermean = pca(j)
    MSE = mean_squared_error(data_aftermean, np.real(rec))
    mse.append(MSE)
plt.plot(mse)
plt.xlabel('d')
plt.ylabel('MSE')
plt.title('MSE against d')
plt.savefig('3c')
plt.show()
Esempio n. 45
0
               ypt + .001,
               name,
               size=5 + 2 * int(np.round(np.log10(flx))),
               color=opts.src_color)
if not opts.nobar: p.colorbar(shrink=.5, format='%.2f')
else: p.subplots_adjust(.05, .05, .95, .95)


def mk_arr(val, dtype=np.double):
    if type(val) is np.ndarray: return val.astype(dtype)
    return np.array(val, dtype=dtype).flatten()


if opts.outfile != '':
    print('Saving to', opts.outfile)
    p.savefig(opts.outfile)
else:
    # Add right-click functionality for finding locations/strengths in map.
    cnt = 1

    def click(event):
        global cnt
        if event.button == 3:
            lon, lat = map(event.xdata, event.ydata, inverse=True)
            if opts.osys == 'eq': lon = (360 - lon) % 360
            lon *= a.img.deg2rad
            lat *= a.img.deg2rad
            ra, dec = ephem.hours(lon), ephem.degrees(lat)
            x, y, z = a.coord.radec2eq((ra, dec))
            flx = h[(x, y, z)]
            print('#%d (RA,DEC): (%s, %s), Jy: %f' % (cnt, ra, dec, flx))
def plot_gaussian(mu,sigma):
    x=[]
    y=[]
    for i in range(0,mu*3,1):
        x.append(i)
        y.append(probability_function(i,mu,sigma))
           
    pl.plot(x,y,label='Gaussian'+str(mu))

#Poisson
def poisson(n,mu):
    return(math.pow(mu,n)*math.exp(-1*mu)/math.factorial(n))

def plot_poisson(mu):
    x=[]
    y=[]
    for i in range(mu*3):
        x.append(i)
        y.append(poisson(i,mu))
    pl.plot(x,y,label='Poisson'+str(mu))

mu=[1,5,10,20,30]
fig=pl.figure()
pl.xlim(0,60)
for i in mu:
    plot_gaussian(i,math.sqrt(i))
    plot_poisson(i)
pl.legend()
pl.savefig("Poisson vs Gaussian.pdf")
pl.show()
Esempio n. 47
0
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_wireframe(grid[0],
                  grid[1],
                  mu.reshape(grid[0].shape),
                  alpha=0.5,
                  color='g',
                  label='our estimation')
#ax.plot_wireframe(grid[0], grid[1], reference.reshape(grid[0].shape), alpha=0.5, color='b', label='py reference')
ax.plot_wireframe(grid[0],
                  grid[1],
                  original,
                  alpha=0.5,
                  color='Orange',
                  label='real function')
#ax.plot_wireframe(grid[0], grid[1], mu.reshape(grid[0].shape) - reference.reshape(grid[0].shape), alpha=0.5, color='Orange', label='diff mu-reference')
plt.legend()

plt.savefig('fig.png')

# plt.clf()
#plt.imshow((original.flatten()-mu).reshape(24,24), cmap='hot', interpolation='nearest')
#plt.colorbar()
#plt.show()

print("np.linalg.norm(reference-mu) = %lf" % np.linalg.norm(mu - reference))
print(
    "np.linalg.norm(original-mu)=%lf, np.linalg.norm(original-reference)=%lf" %
    (np.linalg.norm(original.flatten() - mu),
     np.linalg.norm(original.flatten() - reference)))
Esempio n. 48
0
		year_path = constants.TASK_1_PATH + str(year) + '/'
		create_clean_folders([year_path])

		graph, starting_node, colors, labels = graph_to_save_year[year]
		pos = nx.spring_layout(graph, k=0.9, iterations=100)  # positions for all nodes)
		graph: nx.Graph
		print(starting_node)

		ec = nx.draw_networkx_edges(graph.to_undirected(), pos, alpha=0.2)
		nc = nx.draw_networkx_nodes(graph, pos, nodelist=graph.nodes, node_color=colors,
									with_labels=True, node_size=100, cmap=cmap, vmin=vmin, vmax=vmax)
		lc = nx.draw_networkx_labels(graph, pos, labels, font_size=5)
		cb = plt.colorbar(nc)
		cb.set_label('Spreading of influence iteration (-1 if not influenced)')
		plt.axis('off')
		plt.savefig(year_path + str(starting_node), dpi=500)
		plt.close()
		influenced_path = year_path + 'spreading of influence/'
		create_clean_folders([influenced_path])
		for el in top_results_year:
			topic = el[0]
			score = el[1]
			topic_path = influenced_path + str(score) + '_' + topic + '/'
			create_clean_folders([topic_path])
			infl_nodes = influenced_nodes[year][str(year) + '_' + topic]
			f = open(topic_path + 'influenced_nodes.txt', "a")
			f.write(str(infl_nodes))
			f.close()

	'''########## SAVE TOPIC MERGES ##########'''
	for year in merged_topics_year.keys():
Esempio n. 49
0
def run_main():
    # 1. 准备数据
    # 指定股票分析开始日期
    start_date = datetime.datetime(2007, 1, 1)
    # 指定股票分析截止日期
    end_date = datetime.datetime(2017, 3, 1)
    # 股票代码
    stock_code = '600519.SS'  # 沪市贵州茅台

    stock_df = pandas_datareader.data.DataReader(stock_code, 'yahoo',
                                                 start_date, end_date)
    # 预览数据
    print(stock_df.head())
    # 2. 可视化数据
    plt.plot(stock_df['Close'])
    plt.title('股票每日收盘价')
    plt.show()

    # 按周重采样
    stock_s = stock_df['Close'].resample('W-MON').mean()
    stock_train = stock_s['2014':'2016']
    plt.plot(stock_train)
    plt.title('股票周收盘价均值')
    plt.show()

    # 分析 ACF
    acf = plot_acf(stock_train, lags=20)
    plt.title("股票指数的 ACF")
    acf.show()

    # 分析 PACF
    pacf = plot_pacf(stock_train, lags=20)
    plt.title("股票指数的 PACF")
    pacf.show()

    # 3. 处理数据,平稳化数据
    # 这里只是简单第做了一节差分,还有其他平稳化时间序列的方法
    # 可以查询资料后改进这里的平稳化效果
    stock_diff = stock_train.diff()
    diff = stock_diff.dropna()
    print(diff.head())
    print(diff.dtypes)

    plt.figure()
    plt.plot(diff)
    plt.title('一阶差分')
    plt.show()

    acf_diff = plot_acf(diff, lags=20)
    plt.title("一阶差分的 ACF")
    acf_diff.show()

    pacf_diff = plot_pacf(diff, lags=20)
    plt.title("一阶差分的 PACF")
    pacf_diff.show()

    # 4. 根据ACF和PACF定阶并建立模型
    model = ARIMA(stock_train, order=(1, 1, 1), freq='W-MON')
    # 拟合模型
    arima_result = model.fit()
    print(arima_result.summary())

    # 5. 预测
    pred_vals = arima_result.predict('20170102',
                                     '20170301',
                                     dynamic=True,
                                     typ='levels')
    print(pred_vals)

    # 6. 可视化预测结果
    stock_forcast = pd.concat([stock_s, pred_vals],
                              axis=1,
                              keys=['original', 'predicted'])

    plt.figure()
    plt.plot(stock_forcast)
    plt.title('真实值vs预测值')
    plt.savefig('./stock_pred.png', format='png')
    plt.show()
Esempio n. 50
0
reflectance4 = band4.ReadAsArray(0, 0, cols, rows)
reflectance5 = band5.ReadAsArray(0, 0, cols, rows)
reflectance6 = band6.ReadAsArray(0, 0, cols, rows)
reflectance7 = band7.ReadAsArray(0, 0, cols, rows)

reflectance1 = np.array(reflectance1)
reflectance2 = np.array(reflectance2)
reflectance3 = np.array(reflectance3)
reflectance4 = np.array(reflectance4)
reflectance5 = np.array(reflectance5)
reflectance6 = np.array(reflectance6)
reflectance7 = np.array(reflectance7)

plt.imshow(reflectance1, origin='lower left')
plt.colorbar()
plt.savefig('D:\\REFLECTANCE1.jpg')  #save the figure
plt.show()
plt.clf()
#read angle data
fn_angle = 'H:/2014MOD09GA006/angle_fin/A2014001_angle_Grid_2D.tif'
ds_angle = gdal.Open(fn_angle, GA_ReadOnly)
#angle1=solar zenith angle, angle2=view zenith angle, angle3=relative azimuth angle
band8 = ds_angle.GetRasterBand(1)
band9 = ds_angle.GetRasterBand(2)
band10 = ds_angle.GetRasterBand(3)

angle1 = band8.ReadAsArray(0, 0, cols, rows)
angle2 = band9.ReadAsArray(0, 0, cols, rows)
angle3 = band10.ReadAsArray(0, 0, cols, rows)

angle1 = np.array(angle1)
Esempio n. 51
0
    def plot_maps(self, df, file_name):
        cmap = plt.cm.coolwarm
        if self.scenario == 1:
            fig, axes = plt.subplots(nrows=2, ncols=1)

            # plotting class labels
            class_numbers = np.array(df['Class Number'])
            width = int(np.sqrt(len(class_numbers)))
            class_numbers = np.concatenate(
                (class_numbers,
                 75 * np.ones(width - len(class_numbers) % width))).reshape(
                     (-1, width))
            label_dict = dict([(label, i)
                               for i, label in enumerate(self.labels)])
            label_dict['empty'] = len(label_dict)
            rcParams['axes.prop_cycle'] = cycler(
                color=cmap(np.linspace(0, 1, len(label_dict))))
            axes[0].imshow(class_numbers, cmap=cmap)
            # axes[0].legend(handles=patches, loc=2, bbox_to_anchor=(1.01, 1))
            axes[0].tick_params(axis='x',
                                which='both',
                                bottom=False,
                                top=False,
                                labelbottom=False)
            axes[0].tick_params(axis='y',
                                which='both',
                                left=False,
                                right=False,
                                labelleft=False)

            # plotting tags
            tags = df['Tag']
            tags_enum = []
            tags_dict = dict([(tag, i)
                              for i, tag in enumerate(np.unique(self.tags))])
            tags_dict['empty'] = len(tags_dict)
            for tag in tags:
                tags_enum.append(tags_dict[tag])
            tags_enum = np.array(tags_enum)
            tags_enum = np.concatenate(
                (tags_enum, (len(tags_dict) - 1) *
                 np.ones(width - len(tags_enum) % width))).reshape((-1, width))
            rcParams['axes.prop_cycle'] = cycler(
                color=cmap(np.linspace(0, 1, len(tags_dict))))
            axes[1].imshow(tags_enum, cmap=cmap)
            # axes[1].legend(handles=patches, loc=2, bbox_to_anchor=(1.01, 1))
            axes[1].tick_params(axis='x',
                                which='both',
                                bottom=False,
                                top=False,
                                labelbottom=False)
            axes[1].tick_params(axis='y',
                                which='both',
                                left=False,
                                right=False,
                                labelleft=False)
        else:
            fig, axes = plt.subplots(1, 2, figsize=(6, 12))
            class_numbers = np.array(df['Class Number'])
            label_dict = dict([(label, i)
                               for i, label in enumerate(self.labels)])
            label_dict['empty'] = len(label_dict)
            width = int(np.sqrt(len(class_numbers)))
            class_numbers = np.concatenate(
                (class_numbers, (len(label_dict) - 1) *
                 np.ones(width - len(class_numbers) % width))).reshape(
                     (-1, width))
            rcParams['axes.prop_cycle'] = cycler(
                color=cmap(np.linspace(0, 1, len(label_dict))))
            axes[0].imshow(class_numbers, cmap=cmap)
            cmaplist = [cmap(int(i * 3.1)) for i in range(len(label_dict))]
            cmaplist[len(label_dict) - 1] = [1.0, 1.0, 1.0, 1.0]
            cmap = mpl.colors.LinearSegmentedColormap.from_list(
                'Custom cmap', cmaplist, len(label_dict))
            axes[0].imshow(class_numbers, cmap=cmap)
            patches = [
                mpatches.Patch(color=cmap(v), label=k.upper())
                for k, v in label_dict.items() if v in class_numbers
            ]
            axes[1].legend(handles=patches, ncol=len(patches) // 20 + 1)
            plt.tick_params(axis='x',
                            which='both',
                            bottom=False,
                            top=False,
                            labelbottom=False)
            plt.tick_params(axis='y',
                            which='both',
                            left=False,
                            right=False,
                            labelleft=False)
        plt.savefig(str(file_name))
        plt.cla()
        return
test_loss, = ax1.plot(test_log['#Iters'], test_log['TestLoss'], linewidth=2, color='green')
ax1.set_ylim(ymin=0, ymax=1)
ax1.set_xlabel('Iterations', fontsize=15)
ax1.set_ylabel('Loss', fontsize=15)
ax1.tick_params(labelsize=15)
#Plotting test accuracy
ax2 = ax1.twinx()
test_accuracy, = ax2.plot(test_log['#Iters'], test_log['TestAccuracy'], linewidth=2, color='blue')
ax2.set_ylim(ymin=0, ymax=1)
ax2.set_ylabel('Accuracy', fontsize=15)
ax2.tick_params(labelsize=15)
#Adding legend
plt.legend([train_loss, test_loss, test_accuracy], ['Training Loss', 'Test Loss', 'Test Accuracy'],  bbox_to_anchor=(1, 0.8))
plt.title('Training Curve', fontsize=18)
#Saving learning curve
plt.savefig(learning_curve_path)

'''
Deleting training and test logs
'''
command = 'rm ' + train_log_path
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()

command = command = 'rm ' + test_log_path
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()



Esempio n. 53
0
def plot_kin_models(galaxy,
                    vmode,
                    vel_ha,
                    R,
                    Vrot,
                    Vrad,
                    Vtan,
                    VSYS,
                    MODEL,
                    ext,
                    plot=0,
                    save=1):

    mask_MODEL = np.divide(MODEL, MODEL)
    fig = plt.figure(figsize=(6, 2))
    gs2 = GridSpec(1, 3)
    gs2.update(left=0.06,
               right=0.62,
               top=0.83,
               hspace=0.01,
               bottom=0.15,
               wspace=0.)

    ax = plt.subplot(gs2[0, 0])
    ax1 = plt.subplot(gs2[0, 1])
    ax2 = plt.subplot(gs2[0, 2])

    im0 = ax.imshow(vel_ha - VSYS,
                    cmap=califa,
                    origin="lower",
                    vmin=-250,
                    vmax=250,
                    aspect="auto",
                    extent=ext,
                    interpolation="nearest")
    im2 = ax1.imshow(MODEL,
                     cmap=califa,
                     origin="lower",
                     aspect="auto",
                     vmin=-250,
                     vmax=250,
                     extent=ext,
                     interpolation="nearest")

    residual = (vel_ha * mask_MODEL - VSYS) - MODEL
    im2 = ax2.imshow(residual,
                     cmap=califa,
                     origin="lower",
                     aspect="auto",
                     vmin=-50,
                     vmax=50,
                     extent=ext,
                     interpolation="nearest")

    AXIS(ax, tickscolor="k")
    AXIS(ax1, tickscolor="k", remove_yticks=True)
    AXIS(ax2, tickscolor="k", remove_yticks=True)

    ax.set_ylabel(r'$\Delta$ Dec (pix)', fontsize=8, labelpad=0)
    ax.set_xlabel(r'$\Delta$ RA (pix)', fontsize=8, labelpad=0)
    ax1.set_xlabel(r'$\Delta$ RA (pix)', fontsize=8, labelpad=0)
    ax2.set_xlabel(r'$\Delta$ RA (pix)', fontsize=8, labelpad=0)

    ax.text(0.05, 0.9, "VLOS", fontsize=7, transform=ax.transAxes)
    ax1.text(0.05, 0.9, "MODEL", fontsize=7, transform=ax1.transAxes)
    ax2.text(0.05, 0.9, "RESIDUAL", fontsize=7, transform=ax2.transAxes)

    ax.set_facecolor('#e8ebf2')
    ax1.set_facecolor('#e8ebf2')
    ax2.set_facecolor('#e8ebf2')

    gs2 = GridSpec(1, 1)
    gs2.update(left=0.68, right=0.995, top=0.83, bottom=0.15)
    ax3 = plt.subplot(gs2[0, 0])

    ax3.plot(R,
             Vrot,
             color="k",
             linestyle='-',
             alpha=0.6,
             label="V$_\mathrm{circ}$")
    ax3.scatter(R, Vrot, color="k", s=5, marker="s")
    #ax3.errorbar(R,Vrot, yerr=e_vr, fmt='s', color = "k",markersize = 3, label = "V_\mathrm{circ}")

    ax3.plot(R,
             Vrad,
             color="orange",
             linestyle='-',
             alpha=0.6,
             label="V$_\mathrm{rad}$")
    ax3.scatter(R, Vrad, color="orange", s=5, marker="s")
    #ax3.errorbar(R,Vrad, yerr=e_Vrad, fmt='s', color = "orange",markersize = 3)

    ax3.plot(R,
             Vtan,
             color="skyblue",
             linestyle='-',
             alpha=0.6,
             label="V$_\mathrm{tan}$")
    ax3.scatter(R, Vtan, color="skyblue", s=5, marker="s")
    #ax3.errorbar(R,Vtan, yerr=e_Vtan, fmt='s', color = "skyblue",markersize = 3)

    ax3.legend(loc="center",
               fontsize=6.5,
               bbox_to_anchor=(0, 1, 1, 0.1),
               ncol=3,
               frameon=False)

    vels = [Vrot, Vrad, Vtan]
    max_vel, min_vel = int(np.nanmax(vels)), int(np.nanmin(vels))

    ax3.set_ylim(min_vel - 50, max_vel + 50)
    ax3.plot([0, np.nanmax(R)], [0, 0], color="k", linestyle='-', alpha=0.6)
    ax3.set_xlabel('r (arcsec)', fontsize=8, labelpad=0)
    ax3.set_ylabel('V$_\mathrm{ROT}$ (km/s)', fontsize=8, labelpad=0)
    ax3.set_facecolor('#e8ebf2')

    AXIS(ax3, tickscolor="k")

    cb(im0,
       ax,
       orientation="horizontal",
       colormap=califa,
       bbox=(0, 1.12, 1, 1),
       width="100%",
       height="5%",
       label_pad=-23,
       label="(km/s)",
       font_size=7)
    cb(im2,
       ax2,
       orientation="horizontal",
       colormap=califa,
       bbox=(0, 1.12, 1, 1),
       width="100%",
       height="5%",
       label_pad=-23,
       label="(km/s)",
       font_size=7)

    if save == 1 and plot == 1:
        plt.savefig("./plots/kin_%s_model_%s.png" % (vmode, galaxy), dpi=300)
        plt.show()
        plt.clf()
    else:

        if plot == 1:
            plt.show()
            plt.clf()
        if save == 1:
            plt.savefig("./plots/kin_%s_model_%s.png" % (vmode, galaxy),
                        dpi=300)
            plt.clf()
Esempio n. 54
0
    BT = BT[:,3,3]
    gd = BT > 0

    fig, ax = plt.subplots(2, 2) 
    ax[0,0].plot(BT[gd],'.',markersize=0.5, label='BT (from ME)')
    ax[0,0].plot(BT_MMD[gd],'.',markersize=0.5, label='BT (from MMD)')
    ax[0,0].legend(markerscale=20, scatterpoints=5, fontsize=8)
    ax[0,0].set_ylim(200,320)
    ax[0,0].set_xlabel('MM')
    ax[0,0].set_ylabel('BT')
    ax[1,0].plot(BT_MMD[gd], BT_MMD[gd]-BT[gd],'.',markersize=0.5)
    ax[1,0].set_xlim(200,320)
    ax[1,0].set_ylim(-1,1)
    ax[1,0].set_xlabel('BT_MMD')
    ax[1,0].set_ylabel('BT(MMD)-BT(ME)')
#    ax[0,1].hist((BT[gd]-float(BT[gd].mean())),bins=100, label='d_BT (from ME)')
#    ax[0,1].hist((BT_MMD[gd]-float(BT_MMD[gd].mean())),bins=100, label='d_BT (from MMD)')
#    ax[0,1].legend(markerscale=20, scatterpoints=5, fontsize=8)
    ax[0,1].hist((BT_MMD[gd]-BT[gd]),bins=100)
    ax[0,1].set_xlabel('BT(MMD)-BT(ME)')
    ax[1,1].set_ylabel('count (nbins=100)')
    ax[1,1].plot(BT_MMD[gd], BT_MMD[gd], '.',markersize=0.5)
    ax[1,1].set_xlim(200,320)
    ax[1,1].set_ylim(200,320)
    ax[1,1].set_xlabel('BT_MMD')
    ax[1,1].set_ylabel('BT(ME)')
    plt.tight_layout()
    plt.savefig('BT_v_BT_MMD.png')
    
    print('** END')
Esempio n. 55
0
def plot_weights(model,
                 side='decoder',
                 title='',
                 xlabel='',
                 comp=None,
                 rotation=15,
                 fig_path=None):
    try:
        model.n_channels = model.n_datasets
    except:
        pass
    if not hasattr(model, 'ch_name'):
        model.ch_name = [f'Ch.{_}' for _ in range(model.n_channels)]
    fig, axs = plt.subplots(model.n_channels, 1)
    if comp is None:
        suptitle = 'Model Weights\n({})'.format(side)
    else:
        suptitle = 'Model Weights\n({}, comp. {})'.format(side, comp)
    plt.suptitle(title + suptitle)
    for ch in range(model.n_channels):
        ax = axs if model.n_channels == 1 else axs[
            ch]  # 'AxesSubplot' object does not support indexing
        x = np.arange(model.n_feats[ch])
        if side == 'encoder':
            y = model.vae[ch].W_mu.weight.detach().numpy().T.copy()
        else:
            y = model.vae[ch].W_out.weight.detach().numpy().copy()
            try:  # In case of bernoulli features
                if model.bern_feats is not None:
                    bidx = model.bern_feats[ch]
                    y[bidx, :] = sigmoid(y[bidx, :])
            except:
                pass
        if y.shape[0] > 200:
            pass
        else:
            if comp is not None:
                y = y[:, comp]
            # axs[ch].plot(y)
            if model.lat_dim == 1 or comp is not None:
                ax.bar(x, y.reshape(-1), width=0.25)
            else:
                ax.plot(x, y)
            ax.set_ylabel(model.ch_name[ch], rotation=45, fontsize=14)
            if comp is None:
                ax.legend(['comp. ' + str(c) for c in range(model.lat_dim)])
            ax.axhline(y=0, ls="--", c=".3")
            try:
                tick_marks = np.arange(len(model.varname[ch]))
                ax.set_xticks(tick_marks)
                ax.set_xticklabels(model.varname[ch],
                                   rotation=rotation,
                                   fontsize=20)
            except:
                pass
            plt.xlabel(xlabel)

    if fig_path is not None:
        pickle.dump(fig, open(fig_path + '.pkl', 'wb'))
        fsch = 4 * model.n_channels
        fsfeat = 3 * max(model.n_feats)
        plt.rcParams['figure.figsize'] = (fsfeat, fsch)
        plt.tight_layout()
        plt.savefig(fig_path, bbox_inches='tight')
        plt.close()
Esempio n. 56
0
mpl.rcParams['xtick.major.width'] = 5
mpl.rcParams['xtick.minor.size'] = 10
mpl.rcParams['xtick.minor.width'] = 5
plt.rcParams['xtick.labelsize'] = 20

mpl.rcParams['ytick.major.size'] = 10
mpl.rcParams['ytick.major.width'] = 5
mpl.rcParams['ytick.minor.size'] = 10
mpl.rcParams['ytick.minor.width'] = 5
plt.rcParams['ytick.labelsize'] = 20

fig = plt.figure(figsize=(10, 10))
thefile = "One_Bar_Dynamics_Dynamic_Vibration.h5.feioutput"
finput = h5py.File(thefile)

# Read the time and displacement
times = finput["time"][:]
disp = finput["/Model/Nodes/Generalized_Displacements"][5, :]

# Configure the figure filename, according to the input filename.
outfig = thefile.replace("_", "-")
outfigname = outfig.replace("h5.feioutput", "pdf")

# Plot the figure. Add labels and titles.
plt.plot(times, disp, Linewidth=4)
plt.grid()
plt.xlabel("Time [s] ")
plt.ylabel("Displacement [m]  ")
plt.savefig(outfigname, bbox_inches='tight')
plt.show()
]

plt.xlabel('Epochs')
plt.ylabel('CTC Loss')

x_axis = list(range(1, 21))

plt.plot(x_axis, t_loss_1, 'b', label='LSTM-50 training loss')
plt.plot(x_axis, t_loss_2, 'g', label='GRU-50 training loss')
plt.plot(x_axis, v_loss_1, 'b-.', label='LSTM-50 validation loss')
plt.plot(x_axis, v_loss_2, 'g-.', label='GRU-50 validation loss')
plt.legend()
plt.xticks(x_axis)

#plt.show()
plt.savefig('50neurons-loss.png', dpi=600)

#100units

#lstm
t_loss_1 = [
    307, 197, 187, 193, 208, 208, 207, 206, 206, 204, 196, 183, 176, 174, 167,
    161, 156, 154, 149, 159
]
v_loss_1 = [
    303, 289, 249, 278, 278, 278, 276, 277, 279, 276, 259, 287, 245, 236, 225,
    230, 232, 212, 214, 230
]

#gru
t_loss_2 = [
Esempio n. 58
0
def plot_latent_space(model,
                      data=None,
                      qzx=None,
                      classificator=None,
                      text=None,
                      uncertainty=True,
                      comp=None,
                      fig_path=None):
    if data is None:
        try:
            data = model.data
        except:
            pass

    channels = model.n_channels
    if not hasattr(model, 'ch_name'):
        model.ch_name = [f'Ch.{_}' for _ in range(channels)]

    comps = model.lat_dim
    # output = model(data)
    # qzx = output['qzx']
    if qzx is None:
        qzx = model.encode(data)

    if classificator is not None:
        groups = np.unique(classificator)
        if not groups.dtype == np.dtype('O'):
            # remove nans if groups are not objects (strings)
            groups = groups[~np.isnan(groups)]

    # One figure per latent component
    #  Linear relationships expected between channels
    if comp is not None:
        itercomps = comp if isinstance(comp, list) else [comp]
    else:
        itercomps = range(comps)

    # One figure per channel
    #  Uncorrelated relationsips expected between latent components
    for comp in itercomps:
        fig, axs = plt.subplots(channels, channels)
        fig.suptitle(r'$z_{' + str(comp) + '}$', fontsize=30)
        for i, j in itertools.product(range(channels), range(channels)):
            if i == j:
                axs[j, i].text(0.5,
                               0.5,
                               f'z|{model.ch_name[i]}',
                               horizontalalignment='center',
                               verticalalignment='center',
                               fontsize=20)
                axs[j, i].axis('off')
            elif i > j:
                xi = qzx[i].loc.detach().numpy()[:, comp]
                xj = qzx[j].loc.detach().numpy()[:, comp]
                si = np.exp(0.5 * qzx[i].scale.detach().numpy()[:, comp])
                sj = np.exp(0.5 * qzx[j].scale.detach().numpy()[:, comp])
                ells = [
                    Ellipse(xy=[xi[p], xj[p]],
                            width=2 * si[p],
                            height=2 * sj[p]) for p in range(len(xi))
                ]
                if classificator is not None:
                    for g in groups:
                        g_idx = classificator == g
                        axs[j, i].plot(xi[g_idx],
                                       xj[g_idx],
                                       '.',
                                       alpha=0.5,
                                       markersize=15)
                        if uncertainty:
                            color = ax.get_lines()[-1].get_color()
                            for idx in np.where(g_idx)[0]:
                                axs[j, i].add_artist(ells[idx])
                                ells[idx].set_alpha(0.1)
                                ells[idx].set_facecolor(color)
                else:
                    axs[j, i].plot(xi, xj, '.')
                    if uncertainty:
                        for e in ells:
                            axs[j, i].add_artist(e)
                            e.set_alpha(0.1)
                if text is not None:
                    [axs[j, i].text(*item) for item in zip(xi, xj, text)]
                # Bisettrice
                lox, hix = axs[j, i].get_xlim()
                loy, hiy = axs[j, i].get_ylim()
                lo, hi = np.min([lox, loy]), np.max([hix, hiy])
                axs[j, i].plot([lo, hi], [lo, hi], ls="--", c=".3")
            else:
                axs[j, i].axis('off')

        if classificator is not None:
            [axs[-1, 0].plot(0, 0) for g in groups]
            legend = [
                '{} (n={})'.format(g, len(classificator[classificator == g]))
                for g in groups
            ]
            axs[-1, 0].legend(legend)
            try:
                axs[-1, 0].set_title(classificator.name)
            except AttributeError:
                axs[-1, 0].set_title('Groups')

    if fig_path is not None:
        pickle.dump(fig, open(fig_path + '.pkl', 'wb'))
        fs = 2 * channels + 1
        plt.rcParams['figure.figsize'] = (fs, fs)
        # plt.tight_layout()
        plt.savefig(fig_path, bbox_inches='tight')
        plt.close()

    return fig, axs
Esempio n. 59
0
# Output information
print 'TTV amplitude =', numpy.amax(ttv_array), \
    '[min] = ', numpy.amax(ttv_array) * 60, '[sec]'
print 'TDV amplitude =', numpy.amax(tdv_array), \
    '[min] = ', numpy.amax(tdv_array) * 60, '[sec]'

ax = plt.axes()
plt.plot(ttv_array, tdv_array, color='k')
plt.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
plt.rc('text', usetex=True)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.xlabel('transit timing variation [minutes]', fontsize=16)
plt.ylabel('transit duration variation [minutes]', fontsize=16)
ax.tick_params(direction='out')
plt.ylim([numpy.amin(tdv_array) * 1.2, numpy.amax(tdv_array) * 1.2])
plt.xlim([numpy.amin(ttv_array) * 1.2, numpy.amax(ttv_array) * 1.2])
plt.plot((0, 0), (numpy.amax(tdv_array) * 10., numpy.amin(tdv_array) * 10.),
         'k',
         linewidth=0.5)
plt.plot((numpy.amin(ttv_array) * 10., numpy.amax(ttv_array) * 10.), (0, 0),
         'k',
         linewidth=0.5)

# Fix axes for comparison with eccentric moon
plt.xlim(-0.15, +0.15)
plt.ylim(-0.65, +0.65)
plt.annotate(r"4:2:1", xy=(-0.145, +0.55), size=16)

plt.savefig("fig_system_8.eps", bbox_inches='tight')
Esempio n. 60
0
	if hzdict[label]==t0:
		ax.set_xlim(min(times)-(max(times)-min(times))*0.05,max(times)+(max(times)-min(times))*0.05)
	elif hzdict[label]==t1:
		ax.set_xlim(dt.datetime.strptime(max_times_str,date_fmt)-dt.timedelta(minutes=hzdict[label])-dt.timedelta(minutes=hzdict[label])*0.05,dt.datetime.strptime(max_times_str,date_fmt)+dt.timedelta(minutes=hzdict[label]*0.05))
	else:
		ax.set_xlim(dt.datetime.strptime(max_times_str,date_fmt)-dt.timedelta(hours=hzdict[label])-dt.timedelta(hours=hzdict[label])*0.05,dt.datetime.strptime(max_times_str,date_fmt)+dt.timedelta(hours=hzdict[label]*0.05))
	plt.draw()
radio.on_clicked(hzfunc)
for k in range(6):
    ax.plot(times,pressures[k],label=labels[k+1]+' %.2e mbar'%pressures[k][-1],color=col[k],marker='.',ms=mars,ls=lines)
''' plots vertical lines when label is changed '''
"""for i in range(len(events)):
    ax.plot([events[i],events[i]], [1e-12,1e2], 'k', lw = 1)
    ax.text(events[i],1e1,'Label changed')""" 

ax.set_yscale('log')
ax.set_ylim(1e-12,1e2)
ax.grid(True,which='both',ls='-',color='0.45')
fig.autofmt_xdate()
ax.format_xdata = mdate.DateFormatter('%d')
ax.xaxis.set_major_formatter(myFMT)


ax.set_ylabel('Pressure [mbar]')
lgnd=ax.legend(loc=3)
for i in range(6):
    lgnd.legendHandles[i]._legmarker.set_markersize(5)
datenow = str(dt.datetime.now().strftime(date_fmt))
plt.savefig('pressure - %s.png'%datenow.replace(':','-'))
plt.show()