Exemplo n.º 1
0
def doFormating(**formating):
    if 'title' in formating:
        pylab.title(formating['title'])
    if 'xlabel' in formating:
        pylab.xlabel(formating['xlabel'])
    if 'ylabel' in formating:
        pylab.ylabel(formating['ylabel'])
Exemplo n.º 2
0
def disp_results(fig, ax1, ax2, loss_iterations, losses, accuracy_iterations, accuracies, accuracies_iteration_checkpoints_ind, fileName, color_ind=0):
    modula = len(plt.rcParams['axes.color_cycle'])
    acrIterations =[]
    top_acrs={}
    if accuracies.size:
        if 	accuracies.size>4:
		    top_n = 4
        else:
            top_n = accuracies.size -1		
        temp = np.argpartition(-accuracies, top_n)
        result_indexces = temp[:top_n]
        temp = np.partition(-accuracies, top_n)
        result = -temp[:top_n]
        for acr in result_indexces:
            acrIterations.append(accuracy_iterations[acr])
            top_acrs[str(accuracy_iterations[acr])]=str(accuracies[acr])

        sorted_top4 = sorted(top_acrs.items(), key=operator.itemgetter(1))
        maxAcc = np.amax(accuracies, axis=0)
        iterIndx = np.argmax(accuracies)
        maxAccIter = accuracy_iterations[iterIndx]
        maxIter =   accuracy_iterations[-1]
        consoleInfo = format('\n[%s]:maximum accuracy [from 0 to %s ] = [Iteration %s]: %s ' %(fileName,maxIter,maxAccIter ,maxAcc))
        plotTitle = format('max accuracy(%s) [Iteration %s]: %s ' % (fileName,maxAccIter, maxAcc))
        print (consoleInfo)
        #print (str(result))
        #print(acrIterations)
       # print 'Top 4 accuracies:'		
        print ('Top 4 accuracies:'+str(sorted_top4))		
        plt.title(plotTitle)
    ax1.plot(loss_iterations, losses, color=plt.rcParams['axes.color_cycle'][(color_ind * 2 + 0) % modula])
    ax2.plot(accuracy_iterations, accuracies, plt.rcParams['axes.color_cycle'][(color_ind * 2 + 1) % modula], label=str(fileName))
    ax2.plot(accuracy_iterations[accuracies_iteration_checkpoints_ind], accuracies[accuracies_iteration_checkpoints_ind], 'o', color=plt.rcParams['axes.color_cycle'][(color_ind * 2 + 1) % modula])
    plt.legend(loc='lower right') 
Exemplo n.º 3
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")
Exemplo n.º 4
0
def plot_experiment_stats(e):
    sample_data = np.where(e.num_test_genotypes(SAMPLE) > 0)[0]
    c_sample = (100.0 * e.called(SAMPLE)[sample_data]) / e.num_test_genotypes(SAMPLE)[sample_data] + 1e-15
    fill = 100.*e.fill[sample_data]

    snp_data = np.where(e.num_test_genotypes(SNP) > 0)[0]
    c_snp = (100.0 * e.called(SNP)[snp_data]) / e.num_test_genotypes(SNP)[snp_data]
    
    # Call % vs. fill %
    P.figure(1);
    P.clf();
    P.plot(fill, c_sample, 'o')
    P.xlabel('Fill %')
    P.ylabel('Call %')
    P.title('Validation Breakdown by Sample, %.2f%% Deleted. r = %.2f' % 
              (100.0 * e.fraction, np.corrcoef(fill + SMALL_FLOAT, c_sample + SMALL_FLOAT)[0, 1],))

    # Call % vs. SNP
    P.figure(2);
    P.clf();
    P.plot(snp_data, c_snp, 'o')
    P.xlabel('SNP #')
    P.ylabel('Call %')
    P.title('Validation Breakdown by SNP, %.2f%% Deleted' % (100.0 * e.fraction,))
    
    return (np.array([snp_data, c_snp]).transpose(),
            np.array([sample_data, c_sample, fill]).transpose())
Exemplo n.º 5
0
    def plot_trades(self, subset=None):
        if subset is None:
            subset = slice(None, None)
        fr = self.trades.ix[subset]
        le = fr.price[(fr.pos > 0) & (fr.vol > 0)]
        se = fr.price[(fr.pos < 0) & (fr.vol < 0)]
        lx = fr.price[(fr.pos.shift() > 0) & (fr.vol < 0)]
        sx = fr.price[(fr.pos.shift() < 0) & (fr.vol > 0)]

        import matplotlib.pylab as pylab

        pylab.plot(le.index, le.values, '^', color='lime', markersize=12,
                   label='long enter')
        pylab.plot(se.index, se.values, 'v', color='red', markersize=12,
                   label='short enter')
        pylab.plot(lx.index, lx.values, 'o', color='lime', markersize=7,
                   label='long exit')
        pylab.plot(sx.index, sx.values, 'o', color='red', markersize=7,
                   label='short exit')
        eq = self.equity.ix[subset].cumsum()
        ix = eq.index
        (eq + self.ohlc.O[ix[0]]).plot(color='red', style='-')
        # self.ohlc.O.ix[ix[0]:ix[-1]].plot(color='black', label='price')
        self.ohlc.O.ix[subset].plot(color='black', label='price')
        pylab.title('%s\nTrades for %s' % (self, subset))
Exemplo n.º 6
0
    def plot(self, title=None, **kwargs):
        """Generates a pylab plot from the result set.

        ``matplotlib`` must be installed, and in an
        IPython Notebook, inlining must be on::

            %%matplotlib inline

        The first and last columns are taken as the X and Y
        values.  Any columns between are ignored.

        Parameters
        ----------
        title: Plot title, defaults to names of Y value columns

        Any additional keyword arguments will be passsed
        through to ``matplotlib.pylab.plot``.
        """
        import matplotlib.pylab as plt
        self.guess_plot_columns()
        self.x = self.x or range(len(self.ys[0]))
        coords = reduce(operator.add, [(self.x, y) for y in self.ys])
        plot = plt.plot(*coords, **kwargs)
        if hasattr(self.x, 'name'):
            plt.xlabel(self.x.name)
        ylabel = ", ".join(y.name for y in self.ys)
        plt.title(title or ylabel)
        plt.ylabel(ylabel)
        return plot
Exemplo n.º 7
0
def study_redmapper_2d():
    # I just want to know the typical angular separation for RM clusters.
    # I'm going to do this in a lazy way.
    hemi = 'north'
    rm = load_redmapper(hemi=hemi)
    ra = rm['ra']
    dec = rm['dec']
    ncl = len(ra)
    dist = np.zeros((ncl, ncl))
    for i in range(ncl):
        this_ra = ra[i]
        this_dec = dec[i]
        dra = this_ra-ra
        ddec = this_dec-dec
        dxdec = dra*np.cos(this_dec*np.pi/180.)
        dd = np.sqrt(dxdec**2. + ddec**2.)
        dist[i,:] = dd
        dist[i,i] = 99999999.
    d_near_arcmin = dist.min(0)*60.
    pl.clf(); pl.hist(d_near_arcmin, bins=100)
    pl.title('Distance to Nearest Neighbor for RM clusters')
    pl.xlabel('Distance (arcmin)')
    pl.ylabel('N')
    fwhm_planck_217 = 5.5 # arcmin
    sigma = fwhm_planck_217/2.355
    frac_2sigma = 1.*len(np.where(d_near_arcmin>2.*sigma)[0])/len(d_near_arcmin)
    frac_3sigma = 1.*len(np.where(d_near_arcmin>3.*sigma)[0])/len(d_near_arcmin)
    print '%0.3f percent of RM clusters are separated by 2-sigma_planck_beam'%(100.*frac_2sigma)
    print '%0.3f percent of RM clusters are separated by 3-sigma_planck_beam'%(100.*frac_3sigma)    
    ipdb.set_trace()
Exemplo n.º 8
0
    def show(self, x):
        """
        vizualisation of the mm based on the empirical histogram of x

        Parameters
        ----------
        x: array of shape(nbitems): the data to be processed
        """
        step = 3.5*np.std(x)/np.exp(np.log(np.size(x))/3)
        bins = max(10,int((x.max()-x.min())/step))
        h,c = np.histogram(x, bins)
        h = h.astype(np.float)/np.size(x)
        p = self.mixt
        
        dc = c[1]-c[0]
        y = (1-p)*_gaus_dens(self.mean,self.var,c)*dc
        z = np.zeros(np.size(c))
        i = np.ravel(np.nonzero(c>0))
        z = _gam_dens(self.shape,self.scale,c)*p*dc
        
        import matplotlib.pylab as mp
        mp.figure()
        mp.plot(0.5 *(c[1:] + c[:-1]),h)
        mp.plot(c,y,'r')
        mp.plot(c,z,'g')
        mp.plot(c,z+y,'k')
        mp.title('Fit of the density with a Gamma-Gaussians mixture')
        mp.legend(('data','gaussian acomponent','gamma component',
                   'mixture distribution'))
Exemplo n.º 9
0
    def pie(self, key_word_sep=" ", title=None, **kwargs):
        """Generates a pylab pie chart from the result set.

        ``matplotlib`` must be installed, and in an
        IPython Notebook, inlining must be on::

            %%matplotlib inline

        Values (pie slice sizes) are taken from the
        rightmost column (numerical values required).
        All other columns are used to label the pie slices.

        Parameters
        ----------
        key_word_sep: string used to separate column values
                      from each other in pie labels
        title: Plot title, defaults to name of value column

        Any additional keyword arguments will be passsed
        through to ``matplotlib.pylab.pie``.
        """
        self.guess_pie_columns(xlabel_sep=key_word_sep)
        import matplotlib.pylab as plt
        pie = plt.pie(self.ys[0], labels=self.xlabels, **kwargs)
        plt.title(title or self.ys[0].name)
        return pie
Exemplo n.º 10
0
def plot_confusion_matrix(cm, title='', cmap=plt.cm.Blues):
    #print cm
    #display vehicle, idle, walking accuracy respectively
    #display overall accuracy
    print type(cm)
   # plt.figure(index
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    #plt.figure("")
    plt.title("Confusion Matrix")
    plt.colorbar()
    tick_marks = [0,1,2]
    target_name = ["driving","idling","walking"]


    plt.xticks(tick_marks,target_name,rotation=45)

    plt.yticks(tick_marks,target_name,rotation=45)
    print len(cm[0])

    for i in range(0,3):
        for j in range(0,3):
         plt.text(i,j,str(cm[i,j]))
    plt.tight_layout()
    plt.ylabel("Actual Value")
    plt.xlabel("Predicted Outcome")
 def plot_values(self, TITLE, SAVE):
     plot(self.list_of_densities, self.list_of_pressures)
     title(TITLE)
     xlabel("Densities")
     ylabel("Pressure")
     savefig(SAVE)
     show()
def viz_docwordfreq_sidebyside(P1, P2, title1='', title2='', 
                                vmax=None, aspect=None, block=False):
  from matplotlib import pylab
  pylab.figure()

  if vmax is None:
    vmax = 1.0
    P1limit = np.percentile(P1.flatten(), 97)
    if P2 is not None:
      P2limit = np.percentile(P2.flatten(), 97)
    else:
      P2limit = P1limit
    while vmax > P1limit and vmax > P2limit:
      vmax = 0.8 * vmax

  if aspect is None:
    aspect = float(P1.shape[1])/P1.shape[0]
  pylab.subplot(1, 2, 1)
  pylab.imshow(P1, aspect=aspect, interpolation='nearest', vmin=0, vmax=vmax)
  if len(title1) > 0:
    pylab.title(title1)
  if P2 is not None:
    pylab.subplot(1, 2, 2)
    pylab.imshow(P2, aspect=aspect, interpolation='nearest', vmin=0, vmax=vmax)
    if len(title2) > 0:
      pylab.title(title2)
  pylab.show(block=block)
Exemplo n.º 13
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)
Exemplo n.º 14
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")
Exemplo n.º 15
0
def plot_grid_experiment_results(grid_results, params, metrics):
    global plt
    params = sorted(params)
    grid_params = grid_results.grid_params
    plt.figure(figsize=(8, 6))
    for metric in metrics:
        grid_params_shape = [len(grid_params[k]) for k in sorted(grid_params.keys())]
        params_max_out = [(1 if k in params else 0) for k in sorted(grid_params.keys())]
        results = np.array([e.results.get(metric, 0) for e in grid_results.experiments])
        results = results.reshape(*grid_params_shape)
        for axis, included_in_params in enumerate(params_max_out):
            if not included_in_params:
                results = np.apply_along_axis(np.max, axis, results)

        print results
        params_shape = [len(grid_params[k]) for k in sorted(params)]
        results = results.reshape(*params_shape)

        if len(results.shape) == 1:
            results = results.reshape(-1,1)
        import matplotlib.pylab as plt

        #f.subplots_adjust(left=.2, right=0.95, bottom=0.15, top=0.95)
        plt.imshow(results, interpolation='nearest', cmap=plt.cm.hot)
        plt.title(str(grid_results.name) + " " + metric)

        if len(params) == 2:
            plt.xticks(np.arange(len(grid_params[params[1]])), grid_params[params[1]], rotation=45)
        plt.yticks(np.arange(len(grid_params[params[0]])), grid_params[params[0]])
        plt.colorbar()
        plt.show()
Exemplo n.º 16
0
def plot_peaks(spectra, ref, zl, pl, filename=''):
  plt.figure();
  plt.title("Debug: Peak fitting for '%s'" % filename);
  plt.xlabel("y-position [px]");
  plt.ylabel("Intensity");

  Nspectra, Npx = spectra.shape;

  for s in xrange(Nspectra):
    scale = 1./spectra.max();
    offset= -s*0.1;

    # plot data
    plt.plot(spectra[s]*scale + offset,'k',linewidth=2);

    # plot first peak
    p,A,w = zl[s];
    x = np.arange(-2*w, 2*w) + p;
    plt.plot(x,gauss(x,*zl[s])*scale + offset,'r');
  
    # plot second peak
    if ref is not None:
      p,A   = pl[s];
      x = np.arange(len(ref)) - len(ref)/2 + p;
      plt.plot(x,ref/ref.max()*A*scale + offset,'g');
Exemplo n.º 17
0
def fancy_dendrogram(*args, **kwargs):
    '''
    Source: https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/
    '''
    from scipy.cluster import hierarchy
    import matplotlib.pylab as plt
    
    max_d = kwargs.pop('max_d', None)
    if max_d and 'color_threshold' not in kwargs:
        kwargs['color_threshold'] = max_d
    annotate_above = kwargs.pop('annotate_above', 0)

    ddata = hierarchy.dendrogram(*args, **kwargs)

    if not kwargs.get('no_plot', False):
        plt.title('Hierarchical Clustering Dendrogram (truncated)')
        plt.xlabel('sample index or (cluster size)')
        plt.ylabel('distance')
        for i, d, c in zip(ddata['icoord'], ddata['dcoord'], ddata['color_list']):
            x = 0.5 * sum(i[1:3])
            y = d[1]
            if y > annotate_above:
                plt.plot(x, y, 'o', c=c)
                plt.annotate("%.3g" % y, (x, y), xytext=(0, -5),
                             textcoords='offset points',
                             va='top', ha='center')
        if max_d:
            plt.axhline(y=max_d, c='k')
    return ddata
Exemplo n.º 18
0
def viz_birth_proposal_2D(curModel, newModel, ktarget, freshCompIDs,
                          title1='Before Birth',
                          title2='After Birth'):
  ''' Create before/after visualization of a birth move (in 2D)
  '''
  from ..viz import GaussViz, BarsViz
  from matplotlib import pylab

  fig = pylab.figure()
  h1 = pylab.subplot(1,2,1)

  if curModel.obsModel.__class__.__name__.count('Gauss'):
    GaussViz.plotGauss2DFromHModel(curModel, compsToHighlight=ktarget)
  else:
    BarsViz.plotBarsFromHModel(curModel, compsToHighlight=ktarget, figH=h1)
  pylab.title(title1)
    
  h2 = pylab.subplot(1,2,2)
  if curModel.obsModel.__class__.__name__.count('Gauss'):
    GaussViz.plotGauss2DFromHModel(newModel, compsToHighlight=freshCompIDs)
  else:
    BarsViz.plotBarsFromHModel(newModel, compsToHighlight=freshCompIDs, figH=h2)
  pylab.title(title2)
  pylab.show(block=False)
  try: 
    x = raw_input('Press any key to continue >>')
  except KeyboardInterrupt:
    import sys
    sys.exit(-1)
  pylab.close()
Exemplo n.º 19
0
	def plot_cell(self,cell_number=0,label='insert_label'):

		current_cell = self.cell_list[cell_number]
		temp = current_cell.temp
		cd_signal = current_cell.cd_signal
		cd_calc = current_cell.cd_calc()
		
		ax = pylab.gca()

		pylab.plot(temp,cd_signal,'o',color='black')
                pylab.plot(temp,cd_calc,color='black')
		pylab.xlabel(r'Temperature ($^{\circ}$C)')
		pylab.ylabel('mdeg')
		pylab.ylim([-25,-4])
		dH = numpy.round(current_cell.dH, decimals=1)
		Tm = numpy.round(current_cell.Tm-273.15, decimals=1)
		nf = current_cell.nf
		nu = current_cell.nu
		textstr_dH = '${\Delta}H_{m}$ = %.1f kcal/mol' %dH
		textstr_Tm ='$T_{m}$ = %.1f $^{\circ}$C' %Tm
		textstr_nf ='$N_{folded}$ = %d' %nf
		textstr_nu ='$N_{unfolded}$ = %d'%nu
		ax.text(8,-6,textstr_dH, fontsize=16,ha='left',va='top')
		ax.text(8,-7.5,textstr_Tm, fontsize=16,ha='left',va='top')
		ax.text(8,-9,textstr_nf, fontsize=16,ha='left',va='top')
		ax.text(8,-10.5,textstr_nu, fontsize=16,ha='left',va='top')
		pylab.title(label)		
		pylab.show()

		return
    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)
Exemplo n.º 21
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")  
Exemplo n.º 22
0
    def plot_histogram(self, main="", numrows=1, numcols=1, fignum=1):
        """Plot a histogram of choices and probability sums. Expects probabilities as (at least) a 2D array.
        """
        from matplotlib.pylab import bar, xticks, yticks, title, text, axis, figure, subplot

        probabilities = self.get_probabilities()
        if probabilities.ndim < 2:
            raise StandardError, "probabilities must have at least 2 dimensions."
        alts = probabilities.shape[1]
        width_par = (1 / alts + 1) / 2.0
        choice_counts = self.get_choice_histogram(0, alts)
        sum_probs = self.get_probabilities_sum()

        subplot(numrows, numcols, fignum)
        bar(arange(alts), choice_counts, width=width_par)
        bar(arange(alts) + width_par, sum_probs, width=width_par, color="g")
        xticks(arange(alts))
        title(main)
        Axis = axis()
        text(
            alts + 0.5,
            -0.1,
            "\nchoices histogram (blue),\nprobabilities sum (green)",
            horizontalalignment="right",
            verticalalignment="top",
        )
Exemplo n.º 23
0
 def ACF_PACF_plot(self):
     #plot ACF and PACF to find the number of terms needed for the AR and MA in ARIMA
     # ACF finds MA(q): cut off after x lags 
     # and PACF finds AR (p): cut off after y lags 
     # in ARIMA(p,d,q) 
     lag_acf = acf(self.ts_log_diff, nlags=20)
     lag_pacf = pacf(self.ts_log_diff, nlags=20, method='ols')
     
     #Plot ACF:
     ax=plt.subplot(121)
     plt.plot(lag_acf)
     ax.set_xlim([0,5])
     plt.axhline(y=0,linestyle='--',color='gray')
     plt.axhline(y= -1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
     plt.axhline(y= 1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
     plt.title('Autocorrelation Function')
     
     #Plot PACF:
     plt.subplot(122)
     plt.plot(lag_pacf)
     plt.axhline(y=0,linestyle='--',color='gray')
     plt.axhline(y= -1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
     plt.axhline(y=1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
     plt.title('Partial Autocorrelation Function')
     plt.tight_layout()
Exemplo n.º 24
0
    def XXtest5_regrid(self):
        srcF = cdms2.open(sys.prefix + \
                              '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc')
        so = srcF('so')[0, 0, ...]
        clt = cdms2.open(sys.prefix + '/sample_data/clt.nc')('clt')
        dstData = so.regrid(clt.getGrid(), 
                            regridTool = 'esmf', 
                            regridMethod='conserve')

        if self.pe == 0:
            dstDataMask = (dstData == so.missing_value)
            dstDataFltd = dstData * (1 - dstDataMask)
            zeroValCnt = (dstData == 0).sum()
            if so.missing_value > 0:
                dstDataMin = dstData.min()
                dstDataMax = dstDataFltd.max()
            else:
                dstDataMin = dstDataFltd.min()
                dstDataMax = dstData.max()
                zeroValCnt = (dstData == 0).sum()
            print 'Number of zero valued cells', zeroValCnt
            print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax)                   
            self.assertLess(dstDataMax, so.max())
            if False:
                pylab.figure(1)
                pylab.pcolor(so, vmin=20, vmax=40)
                pylab.colorbar()
                pylab.title('so')
                pylab.figure(2)
                pylab.pcolor(dstData, vmin=20, vmax=40)
                pylab.colorbar()
                pylab.title('dstData')
Exemplo n.º 25
0
def EnhanceContrast(g, r=3, op_kernel=15, silence=True):
    
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(op_kernel,op_kernel))
    opening = cv2.morphologyEx(g, cv2.MORPH_OPEN, kernel)
    
    g_copy = np.asarray(np.copy(g), dtype=np.float)

    m_f = np.mean(opening)
        
    u_max = 245; u_min = 10; t_min = np.min(g); t_max = np.max(g)

    idx_gt_mf = np.where(g_copy > m_f)
    idx_lt_mf = np.where(g_copy <= m_f)

    g_copy[idx_gt_mf] = -0.5 * ((u_max-u_min) / (m_f-t_max)**r) * (g_copy[idx_gt_mf]-t_max)**r + u_max
    g_copy[idx_lt_mf] = 0.5 * ((u_max-u_min) / (m_f-t_min)**r) * (g_copy[idx_lt_mf]-t_min)**r + u_min 

    if silence == False:
        plt.subplot(1,2,1)
        plt.imshow(g, cmap='gray')
        plt.title('Original image')
        plt.subplot(1,2,2)
        plt.imshow(g_copy, cmap='gray')
        plt.title('Enhanced image')
        plt.show()
        
    return g_copy
    def static_view(self, m=0, n=1, NS=100):
        """=============================================================
	   Grafica Estatica (m,n) Modo normal:
	    
	    Realiza un grafico de densidad del modo de oscilación (m,n)
	    de la membrana circular en el tiempo t=0

	    ARGUMENTOS:
	      *Numero cuantico angular				m
	      *Numero cuantico radial				n
	      *Resolucion del grid (100 por defecto)		NS
	============================================================="""
        # Grid
        XM = np.linspace(-1 * self.R, 1 * self.R, NS)
        YM = np.linspace(1 * self.R, -1 * self.R, NS)
        # ---------------------------------------------------------------
        Z = np.zeros((NS, NS))
        for i in xrange(0, NS):
            for j in xrange(0, NS):
                xd = XM[i]
                yd = YM[j]
                rd = (xd ** 2 + yd ** 2) ** 0.5
                thd = np.arctan(yd / xd)
                if xd < 0:
                    thd = np.pi + thd
                if rd < self.R:
                    Z[j, i] = self.f(rd, thd, 0, m, n)
                # ---------------------------------------------------------------
        Z[0, 0] = -1
        Z[1, 0] = 1
        plt.xlabel("X (-R,R)")
        plt.ylabel("Y (-R,R)")
        plt.title("Circular Membrane: (%d,%d) mode" % (m, n))
        plt.imshow(Z)
        plt.show()
Exemplo n.º 27
0
	def predict(self,train,test,w,progress=False):
		'''
		1-nearest neighbor classification algorithm using LB_Keogh lower 
		bound as similarity measure. Option to use DTW distance instead
		but is much slower.
		'''
		for ind,i in enumerate(test):
			if progress:
				print str(ind+1)+' points classified'
			min_dist=float('inf')
			closest_seq=[]
	
			for j in train:
				if self.LB_Keogh(i,j[:-1],5)<min_dist:
					dist=self.DTWDistance(i,j[:-1],w)
					if dist<min_dist:
						min_dist=dist
						closest_seq=j
			self.preds.append(closest_seq[-1])
			
			if self.plotter: 
				plt.plot(i)
				plt.plot(closest_seq[:-1])
				plt.legend(['Test Series','Nearest Neighbor in Training Set'])
				plt.title('Nearest Neighbor in Training Set - Prediction ='+str(closest_seq[-1]))
				plt.show()
Exemplo n.º 28
0
def flipPlot(minExp, maxExp):
    """假定minEXPy和maxExp是正整数且minExp<maxExp
    绘制出2**minExp到2**maxExp次抛硬币的结果
    """
    ratios = []
    diffs = []
    aAxis = []
    for i in range(minExp, maxExp+1):
        aAxis.append(2**i)
    for numFlips in aAxis:
        numHeads = 0
        for n in range(numFlips):
            if random.random() < 0.5:
                numHeads += 1
        numTails = numFlips - numHeads
        ratios.append(numHeads/numFlips)
        diffs.append(abs(numHeads-numTails))
    plt.figure()
    ax1 = plt.subplot(121)
    plt.title("Difference Between Heads and Tails")
    plt.xlabel('Number of Flips')
    plt.ylabel('Abs(#Heads - #Tails)')
    ax1.semilogx(aAxis, diffs, 'bo')
    ax2 = plt.subplot(122)
    plt.title("Heads/Tails Ratios")
    plt.xlabel('Number of Flips')
    plt.ylabel("#Heads/#Tails")
    ax2.semilogx(aAxis, ratios, 'bo')
    plt.show()
Exemplo n.º 29
0
def plot_waveforms(time,voltage,APTimes,titlestr):
    """
    plot_waveforms takes four arguments - the recording time array, the voltage
    array, the time of the detected action potentials, and the title of your
    plot.  The function creates a labeled plot showing the waveforms for each
    detected action potential
    """
   
    plt.figure()
   
    ## Your Code Here 
    indices = []
    
    for x in range(len(APTimes)):
        for i in range(len(time)):
            if(time[i]==APTimes[x]):
                indices.append(i)
            

    ##print indices
    Xval = np.linspace(-.003,.003,200)
    print len(Xval)
    for x in range(len(APTimes)):
        plt.plot(Xval, voltage[indices[x]-100:indices[x]+100])
        plt.title(titlestr)
        plt.xlabel('Time (s)')
        plt.ylabel('Voltage (uV)')
        plt.hold(True)

    
    
    plt.show()
Exemplo n.º 30
0
    def plot_histogram_with_capacity(self, capacity, main=""):
        """Plot histogram of choices and capacities. The number of alternatives is determined
        from the second dimension of probabilities.
        """
        from matplotlib.pylab import bar, xticks, yticks, title, text, axis, figure, subplot

        probabilities = self.get_probabilities()
        if probabilities.ndim < 2:
            raise StandardError, "probabilities must have at least 2 dimensions."
        alts = self.probabilities.shape[1]
        width_par = (1 / alts + 1) / 2.0
        choice_counts = self.get_choice_histogram(0, alts)
        sum_probs = self.get_probabilities_sum()

        subplot(212)
        bar(arange(alts), choice_counts, width=width_par)
        bar(arange(alts) + width_par, capacity, width=width_par, color="r")
        xticks(arange(alts))
        title(main)
        Axis = axis()
        text(
            alts + 0.5,
            -0.1,
            "\nchoices histogram (blue),\ncapacities (red)",
            horizontalalignment="right",
            verticalalignment="top",
        )
Exemplo n.º 31
0
    sns.set_context('poster')

    plt.subplots(figsize=(20, 8))
    #make 5000 noise and 1000 of each x sample
    N_samples = 1000
    noise = np.random.randn(5 * N_samples, noise_dim).astype('float32')
    x_gen = np.repeat(xgen, 1000)
    x_gen = x_gen.reshape(5000, 1)
    #plug into posterior
    z_samples = posterior(x_gen, noise)
    z_samples = tf.reshape(z_samples, [xgen.shape[0], N_samples, 2]).eval()
    for i in range(5):
        plt.subplot(2, 5, i + 1)
        sns.kdeplot(z_samples[i, :, 0], z_samples[i, :, 1], cmap='Greens')
        plt.axis('square')
        plt.title('q(z|x={})'.format(y[i]))
        plt.xlim([xmin, xmax])
        plt.ylim([xmin, xmax])
        plt.xticks([])
        plt.yticks([])
        plt.subplot(2, 5, 5 + i + 1)
        plt.contour(xrange,
                    xrange,
                    np.exp(logprior + llh[i]).reshape(300, 300).T,
                    cmap='Greens')
        plt.axis('square')
        plt.title('p(z|x={})'.format(y[i]))
        plt.xlim([xmin, xmax])
        plt.ylim([xmin, xmax])
        plt.xticks([])
        plt.yticks([])
Exemplo n.º 32
0
myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=1)
sd.wait()

sampling_period = 1 / fs
final = lenghtBits / 2
ts = pyl.arange(0, final, sampling_period)

data = cut_unused_data(myrecording)
plot_x = pyl.arange(0, len(data) * sampling_period, sampling_period)

# Demodulation of received data
originalBits = custom_demodulation(ts, data, lenghtBits)

# Plotting received data
pyl.plot(plot_x, data)
pyl.title("Senal recibida por receiver")
pyl.xlabel("Tiempo (s)")
pyl.ylabel("Nivel de senal")
pyl.show()

# delete first bit
originalBits = originalBits[1:]

# split up data
[bits1, bits2] = from_CDMA_to_bits(originalBits)
print(bits1)
print(bits2)

# converting bits to image
bits_to_imageBW(bits1, "result_1")
bits_to_imageBW(bits2, "result_2")
		if (file + ".png") in outs: continue
		if not file.endswith(".wav"): continue
		if count >= threshold: sys.exit(0)
		winsize=512
		shift=256
		fh=600 
		fl=60 
		filename = file
		wavefile = wave.open(path + "/" + dir + "/" + filename, 'r') # open for writing
		nchannels = wavefile.getnchannels()
		sample_width = wavefile.getsampwidth()
		framerate = wavefile.getframerate()
		numframes = wavefile.getnframes()
		# get wav_data
		wav_data = wavefile.readframes(-1)
		wav_data = np.fromstring(wav_data, 'Int16')

		Time=np.linspace(0, len(wav_data)/framerate, num=len(wav_data))

		pl.figure(1)
		pl.title('Signal Wave...')
		pl.plot(Time,wav_data)
		Fs = framerate
		pl.figure(2)
		pl.subplots_adjust(left=0,right=1,bottom=0,top=1)
		pl.specgram(wav_data, NFFT=1024, Fs=Fs, noverlap=512)
		pl.axis('off')
		pl.axis('tight')
		pl.savefig(out + "/" + dir + '/%s.png' % filename)
		count += 1
		print(dir + ": " + count + " for this round")
Exemplo n.º 34
0
            if i == 0:
                pl.text(0.5, 1.2, str(AntNos[j]), transform=ax.transAxes)
            if j == Nant - 1:
                pl.text(1.2, 0.5, str(AntNos[i]), transform=ax.transAxes)

        #Give a sample plot
        pl.subplot(337)
        bl = bls[0]
        i, j = np.argwhere(AntNos == bl[0]).squeeze(), np.argwhere(
            AntNos == bl[1]).squeeze()
        pl.vlines(D_ij[pol][bl] / bl_len[bl], 0, 1.1, color='k')
        pl.vlines((Tau[pol][i] - Tau[pol][j]) / bl_len[bl], 0, 1.1, color='r')
        pl.vlines((-1, 1), 0, 1.1, linestyles='dotted', color='k')
        pl.plot(delays / bl_len[bl], DD[pol][bl], 'b')
        pl.title('Sample baseline, %d_%d' % bl)
        pl.xlim([-1.5, 1.5])
        pl.ylim([0, 1.1])
        pl.xlabel('Delay, baseline lenghth.')
        pl.ylabel('Amplitude')

        pl.subplots_adjust(wspace=0, hspace=0, top=0.95, bottom=0.05)
        pl.draw()
        figcnt += 1

    #Plot a matrix of all delays
    for pol in pols:
        pl.figure(figcnt)
        DDD = np.zeros((Nant, Nant))
        for i, ant1 in enumerate(AntNos):
            for j, ant2 in enumerate(AntNos):
Exemplo n.º 35
0
sol = odeint(satelite,z0,t)
x_s = sol[:,0]
y_s = sol[:,1]
z_s = sol[:,2]


y_1=[-5e6,0,5e6]
eje_y=["-5000","0","5000"]
x_1=[0,18000,36000,54000,72000,90000]
eje_x=["0","5","10","15","20","25"]

plt.figure()

plt.subplot(3,1,1)
plt.title("Posición")
plt.ylabel("X (KM)")

plt.plot(t,x)
plt.plot(t,x_s)
plt.yticks(y_1,eje_y)
plt.xticks(x_1,eje_x)




plt.subplot(3,1,2)
plt.ylabel("Y (KM)")
plt.plot(t,y)
plt.plot(t,y_s)
plt.yticks(y_1,eje_y)
Exemplo n.º 36
0
    def _plot_seasonality(self, alpha: float, plot_kwargs: bool):
        # two_tailed_alpha = int(alpha / 2 * 100)
        periods = list(set([float(i.split("_")[1]) for i in self.seasonality]))

        additive_ts, multiplicative_ts = self._fit_seasonality()

        all_seasonalities = [("additive", additive_ts)]
        if len(self.multiplicative_data):
            all_seasonalities.append(("multiplicative", multiplicative_ts))
        for sn, ts in all_seasonalities:
            if (sn == "multiplicative"
                    and np.sum(ts) == 1) or (sn == "additive"
                                             and np.sum(ts) == 0):
                continue
            ddf = pd.DataFrame(
                np.vstack([
                    np.percentile(ts[:, :, self.skip_first:], 50, axis=-1),
                    np.percentile(ts[:, :, self.skip_first:],
                                  alpha / 2 * 100,
                                  axis=-1),
                    np.percentile(ts[:, :, self.skip_first:],
                                  (1 - alpha / 2) * 100,
                                  axis=-1),
                ]).T,
                columns=[
                    "%s_%s" % (p, l) for l in ["mid", "low", "high"]
                    for p in periods[::-1]
                ],
            )
            ddf.loc[:, "ds"] = self.data["ds"]
            for period in periods:
                if int(period) == 0:
                    step = int(self.data["ds"].diff().mean().total_seconds() //
                               float(period))
                else:
                    step = int(period)
                graph = ddf.head(step)
                if period == 7:
                    ddf.loc[:, "dow"] = [i for i in ddf["ds"].dt.weekday]
                    graph = (ddf[[
                        "dow",
                        "%s_low" % period,
                        "%s_mid" % period,
                        "%s_high" % period,
                    ]].groupby("dow").mean().sort_values("dow"))
                    graph.loc[:, "ds"] = [[
                        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
                    ][i] for i in graph.index]
                    graph = graph.sort_index()
                plt.figure(**plot_kwargs)
                graph.plot(y="%s_mid" % period,
                           x="ds",
                           color="C0",
                           legend=False,
                           ax=plt.gca())
                plt.grid()

                if period == 7:
                    plt.xticks(range(7), graph["ds"].values)
                    plt.fill_between(
                        np.arange(0, 7),
                        graph["%s_low" % period].values.astype(float),
                        graph["%s_high" % period].values.astype(float),
                        alpha=0.3,
                    )
                else:
                    plt.fill_between(
                        graph["ds"].values,
                        graph["%s_low" % period].values.astype(float),
                        graph["%s_high" % period].values.astype(float),
                        alpha=0.3,
                    )

                plt.title("Model Seasonality (%s) for period: %s days" %
                          (sn, period))
                plt.axes().xaxis.label.set_visible(False)
                plt.show()
Exemplo n.º 37
0
print('分类精度r4:%.2f%%'%(r4*100))# 
print('F1:%.2f%%'%(f1*100))     
print('RMSE:', mean_squared_error(y_test, y_pred) ** 0.5)   #迭代寻优后REMS更小                                                
print('AUC:%.2f%%'%(roc_auc * 100))#


#画图roc
plt.figure()  
lw = 2  
plt.figure(figsize=(10,10))  
plt.plot(fpr, tpr, color='darkorange',  
         lw=lw, label='ROC curve (area = %0.2f)' % roc_auc) ###假正率为横坐标,真正率为纵坐标做曲线  
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')  
plt.xlim([0.0, 1.0])  
plt.ylim([0.0, 1.05])  
plt.xlabel('False Positive Rate')  
plt.ylabel('True Positive Rate')  
plt.title('Receiver operating characteristic example')  
plt.legend(loc="lower right")  
plt.show()  

plt.figure() 
lw = 2  
nnn=range(1,99)
plt.plot(nnn,test_RMSE[1:99], color='darkorange',lw=lw,label = 'test') ###假正率为横坐标,真正率为纵坐标做曲线
plt.plot(nnn,train_RMSE[1:99], color='navy', lw=lw, linestyle='--',label = 'train')
plt.xlabel('Iterations')  
plt.ylabel('RMSE')  
plt.title('Receiver operating characteristic example')  
plt.legend(loc='best')
plt.show()  
Exemplo n.º 38
0
from matplotlib import pylab
import matplotlib

legs = [
    'esmf eval',
    'esmf wgts',
]

pylab.loglog(ns, esmf_eval, 'ro', markersize=8)
pylab.loglog(ns, esmf_weights, 'rs', markersize=8)
if args.nprocs > 1:
    pylab.loglog(ns, esmf_eval_par, 'co', markersize=8)
    pylab.loglog(ns, esmf_weights_par, 'cs', markersize=8)
    legs += [
        'esmf eval {}p'.format(args.nprocs),
        'esmf wgts {}p'.format(args.nprocs)
    ]
pylab.legend(legs, loc=4)

pylab.plot(ns, esmf_eval, 'r-')
pylab.plot(ns, esmf_weights, 'r--')
if args.nprocs > 1:
    pylab.plot(ns, esmf_eval_par, 'c-')
    pylab.plot(ns, esmf_weights_par, 'c--')

pylab.xlabel('num src cells * num dst cells')
pylab.ylabel('time [sec]')
pylab.title('conservative interpolation tripolar to rectilinear')
pylab.savefig('run_conserve.png')
pylab.show()
Exemplo n.º 39
0
mu_p_psd_plot = BO2.model.model.likelihood.gp_link.transf(mu_psd_plot)

labels = ['X', 'Y', 'Z']

## In p space
for i in range(3):
    plt.figure()
    plt.plot(x_plot, y_plot[:, i], 'r')
    plt.plot(x_plot, mu_p_plot[:, i], 'b--')
    plt.fill_between(np.squeeze(x_plot),
                     mu_p_msd_plot[:, i],
                     mu_p_psd_plot[:, i],
                     alpha=0.2,
                     facecolor='blue')
    plt.hlines(tgt_1s[i], *domain_alpha)
    plt.title(labels[i] + 'pspace')

#in f space
for i in range(3):
    plt.figure()
    plt.plot(x_plot, fy_plot[:, i], 'r')
    plt.plot(x_plot, mu_plot[:, i], 'b--')
    plt.fill_between(np.squeeze(x_plot),
                     mu_msd_plot[:, i],
                     mu_psd_plot[:, i],
                     alpha=0.2,
                     facecolor='blue')
    plt.hlines(f_tgt_1s[i], *domain_alpha)
    plt.title(labels[i] + 'fspace')

#=======================================#
Exemplo n.º 40
0
    nlp.n_ineq_constraints()))
print("Total number of constraints: {:>17d}".format(nlp.n_constraints()))
print("Number of nnz in Jacobian: {:>20d}".format(nlp.nnz_jacobian()))
print("Number of nnz in hessian of Lagrange: {:>8d}".format(
    nlp.nnz_hessian_lag()))

x = nlp.init_primals().copy()
y = nlp.create_new_vector('duals')
y.fill(1.0)
nlp.set_primals(x)
nlp.set_duals(y)

# Evaluate jacobian of all constraints
jac_full = nlp.evaluate_jacobian()
plt.spy(jac_full)
plt.title('Jacobian of the all constraints\n')
plt.show()

# Evaluate jacobian of the equality constraints
jac = nlp.evaluate_jacobian_eq()
plt.title('Jacobian of the equality constraints\n')
plt.spy(jac)
plt.show()

# Evaluate jacobian of the inequality constraints
jac = nlp.evaluate_jacobian_ineq()
plt.title('Jacobian of the inequality constraints\n')
plt.spy(jac)
plt.show()

# Evaluate hessian of the lagrangian
Exemplo n.º 41
0
def plot_mean_relation_gp(mp, gp, Xs, Ys1, Ys2):

    from examples.kllr import Plot_Fit_Split

    df = pd.DataFrame({'x': Xs[0], 'm': Xs[1], 'stars': Ys1, 'gas': Ys2})

    x1 = np.linspace(-1.0, 0.5, 201)
    x2 = np.array([12.25, 12.75, 13.5])
    X_new = pm.math.cartesian(x1[:, None], x2[:, None])

    plt.figure(figsize=(12, 10))

    ax = plt.subplot(2, 2, 1)
    plt.scatter(Xs[0], Ys1, s=2, color='lightsteelblue', alpha=0.5)
    plot_mean_profile_fit(X_new,
                          mp,
                          gp[0],
                          x2_dim_len=3,
                          ax=ax,
                          color=['darkmagenta', 'black', 'darkgreen'],
                          label=[
                              r'$\log_{10}(M_{200}) = 12.25$',
                              r'$\log_{10}(M_{200}) = 12.75$',
                              r'$\log_{10}(M_{200}) = 13.5$'
                          ])
    plt.xlim([-1.0, 0.5])
    plt.ylim([-3.0, 3.0])
    plt.xticks([-1, -0.5, 0.0, 0.5], 4 * [' '])
    plt.ylabel(r'$\log(\rho_{\rm \star} / 10^{10})$', size=22)
    plt.title('GP Fits', size=20)
    plt.grid()
    plt.legend(loc=3, prop={'size': 12})

    ax = plt.subplot(2, 2, 2)
    plt.scatter(Xs[0], Ys1, s=2, color='lightsteelblue', alpha=0.5)
    ax = Plot_Fit_Split(df,
                        'x',
                        'stars',
                        'm',
                        split_bins=[12, 12.5, 13, 14],
                        show_data=False,
                        nbins=25,
                        xrange=[-1.2, 0.6],
                        split_mode='Data',
                        kernel_type='gaussian',
                        kernel_width=0.15,
                        xlog=False,
                        labels=[r' ', r' ', r'$\log_{10}(M_{200})$'],
                        color=['darkmagenta', 'black', 'darkgreen'],
                        ax=ax)
    plt.legend(loc=3, prop={'size': 12})
    plt.xlim([-1.0, 0.5])
    plt.ylim([-3.0, 3.0])
    plt.xticks([-1, -0.5, 0.0, 0.5], 4 * [' '])
    plt.yticks([-3, -2, -1, 0, 1, 2, 3], 7 * [' '])
    plt.title('KLLR Fits', size=20)

    ax = plt.subplot(2, 2, 3)
    plt.scatter(Xs[0], Ys2, s=2, color='lightcoral', alpha=0.5)
    plot_mean_profile_fit(X_new,
                          mp,
                          gp[1],
                          x2_dim_len=3,
                          ax=ax,
                          color=['darkmagenta', 'black', 'darkgreen'],
                          label=[
                              r'$\log_{10}(M_{200}) = 12.25$',
                              r'$\log_{10}(M_{200}) = 12.75$',
                              r'$\log_{10}(M_{200}) = 13.5$'
                          ])
    plt.xlim([-1.0, 0.5])
    plt.ylim([-2.0, 2.0])
    plt.xticks([-1, -0.5, 0.0, 0.5], ['-1', '-0.5', '0', '0.5'])
    plt.yticks([-2, -1, 0, 1, 2], ['-2', '-1', '0', '1', '2'])

    plt.xlabel(r'$\log_{10}\left(\frac{r}{R_{200}}\right)$', size=22)
    plt.ylabel(r'$\log_{10}(\rho_{\rm gas} / 10^{12})$', size=22)
    plt.grid()
    plt.legend(loc=3, prop={'size': 12})

    ax = plt.subplot(2, 2, 4)
    plt.scatter(Xs[0], Ys2, s=2, color='lightcoral', alpha=0.5)
    ax = Plot_Fit_Split(df,
                        'x',
                        'gas',
                        'm',
                        split_bins=[12, 12.5, 13, 14],
                        show_data=False,
                        nbins=25,
                        xrange=[-1.2, 0.6],
                        split_mode='Data',
                        kernel_type='gaussian',
                        kernel_width=0.15,
                        xlog=False,
                        labels=[r' ', r' ', r'$\log_{10}(M_{200})$'],
                        color=['darkmagenta', 'black', 'darkgreen'],
                        ax=ax)
    plt.legend(loc=3, prop={'size': 12})
    plt.xlim([-1.0, 0.5])
    plt.ylim([-2.0, 2.0])
    plt.xticks([-1, -0.5, 0.0, 0.5], [' ', '-0.5', '0', '0.5'])
    plt.yticks([-2, -1, 0, 1, 2], 5 * [' '])
    plt.xlabel(r'$\log_{10}\left(\frac{r}{R_{200}}\right)$', size=22)

    plt.subplots_adjust(hspace=0.05, wspace=0.05)

    check_directory('./plots/')
    plt.savefig('./plots/inferred_mean_relation_TNG.png', bbox_inches='tight')
    plt.close()
Exemplo n.º 42
0
# 修改x轴信息 并用字符串代取具体的刻度
_x = list(x)
# 实际就是三个列表的累加拼接
_xtick_labels = [
    '10:{}'.format('0{}'.format(i) if i < 9 else '{}'.format(i))
    for i in range(61)
]
_xtick_labels += [
    '11:{}'.format('0{}'.format(i) if i < 9 else '{}'.format(i))
    for i in range(61)
]
_xtick_labels += [
    '12:{}'.format('0{}'.format(i) if i < 9 else '{}'.format(i))
    for i in range(61)
]
# 1分钟一个刻度过于密集 写了3分钟一个刻度

# 修改y轴信息 使y轴数据更加清晰
_y = range(20, 36)
pylab.yticks(_y, ['{}℃'.format(i) for i in _y])

# 刻度尺步长为3 对应的字符串也为3 并且旋转45度
pylab.xticks(_x[::3], _xtick_labels[::3], rotation=45)

# 绘制网格 0.0 - 1.0 清晰度
pylab.grid(alpha=0.1)

pylab.title('2020-10-1号 10:00点--12:00点 三小时温度变化')

# pylab.show()
pylab.savefig('./matp.png')
Exemplo n.º 43
0
pylab.close('all')

fig0 = pylab.figure(figsize=(10, 8))
pylab.subplot(2, 1, 1)

pylab.plot(stock_data['GOOG']['Close'], 'r-', label='GOOG', linewidth=1.5)
pylab.plot(stock_data['YHOO']['Close'], 'b--', label='YHOO', linewidth=1.0)
pylab.plot(stock_data['AAPL']['Close'], 'm-.', label='AAPL', linewidth=2.0)
pylab.plot(stock_data['MSFT']['Close'], 'k-', label='MSFT', linewidth=1.0)
pylab.plot(stock_data['AMZN']['Close'], 'y-', label='AMZN', linewidth=1.0)

pylab.ylabel('Close Price')
pylab.setp(pylab.gca().get_xticklabels(), FontSize=8, rotation=45)
h = pylab.legend(loc='upper left')
pylab.title('IT industry stocks')
pylab.grid('on')

pylab.subplot(2, 1, 2)

pylab.plot(stock_data['JPM']['Close'], 'r-', label='JPM', linewidth=1.5)
pylab.plot(stock_data['BAC']['Close'], 'b-', label='BAC', linewidth=1.0)
pylab.plot(stock_data['HSBC']['Close'], 'm:', label='HSBC', linewidth=2.0)
pylab.plot(stock_data['CIT']['Close'], 'k-', label='CIT', linewidth=2.5)
pylab.plot(stock_data['GS']['Close'], 'y-', label='GS', linewidth=1.0)

pylab.xlabel('Trading time')
pylab.ylabel('Close Price')
pylab.legend(loc='upper left')
pylab.setp(pylab.gca().get_xticklabels(), FontSize=8, rotation=45)
pylab.title('Bank industry stocks')
Exemplo n.º 44
0
print("Function for CPU: \n", Fcpu)
print("Function for RAM: \n", Fram)
print("Function for Flash: \n", Fflash)

# Show results graphically
Fnew = np.linspace(0, 1500, 10)
Zbnew = Fbaseline(Fnew)
Zcnew = Fcpu(Fnew)
Zfnew = Fflash(Fnew)
Zrnew = Fram(Fnew)

# Plot the data points
plt.plot(Frequency, Pbaseline, '.')
plt.plot(Frequency, Pcpu, 'x')
plt.plot(Frequency, Pflash, 'o')
plt.plot(Frequency, Pram, '*')

# Plot the approximated function
plt.plot(Fnew, Zbnew, label='Baseline')
plt.plot(Fnew, Zcnew, label='CPU')
plt.plot(Fnew, Zfnew, label='Flash')
plt.plot(Fnew, Zrnew, label='RAM')

# Plot
title = "Frequenct vs Power at " + str(Tambient) + "C"
pylab.title(title)
plt.xlabel('Frequency')
plt.ylabel('Total Power')
legend = plt.legend(loc='best')
plt.show()
Exemplo n.º 45
0
    # Create data, which are not that well sampled
    dw = np.linspace(5000,5010,200)
    df = np.exp(-(dw-5004.17)**2/(2.*0.1**2))
else:
    # Create the template
    tw = hdu_hr_260[1].data.field('WAVELENGTH')[0]
    tf = hdu_hr_260[1].data.field('FLUX')[0]
    te = hdu_hr_260[1].data.field('ERROR')[0]

    # Create data, which are not that well sampled
    dw = hdu_hr_4521[1].data.field('WAVELENGTH')[0]
    df = hdu_hr_4521[1].data.field('FLUX')[0]
    de = hdu_hr_4521[1].data.field('ERROR')[0]

# Plot template and data
plt.title("Template (blue) and data (red)")
plt.plot(tw, tf, 'b.-')
plt.plot(dw, df, 'r.-')
plt.show()

"""
test = radial_velocity(dw, df, de, # spectral data on your target (uncertainty is NOT SNR)
                       tw, tf, te, # spectral data on a star with known RV, in the same (or at least overlapping) wavelength region
                       'HR_4521', 'HR_260', # their names, for the plots (see point 2 below for details)
                       0.3, 0.01, # the radial velocity of the standard, for the plots
                       1, # for the plots. Should be the same for both.
                       200, # set to 200 for 'default', see point 3 below for what this means
                       0, 0, 0) # set all to zero by default. See point 4 below for what this means

print(test)
"""
Exemplo n.º 46
0
def plot_learning_curve(estimator,
                        title,
                        X,
                        y,
                        ylim=None,
                        n_jobs=1,
                        train_sizes=[100, 300, 600, 1000, 1886],
                        cv=10,
                        verbose=0,
                        plot=True):
    """
    画出data在某模型上的learning curve.
    参数解释
    ----------
    estimator : 你用的分类器。
    title : 表格的标题。
    X : 输入的feature,numpy类型
    y : 输入的target vector
    ylim : tuple格式的(ymin, ymax), 设定图像中纵坐标的最低点和最高点
    cv : 做cross-validation的时候,数据分成的份数,其中一份作为cv集,其余n-1份作为training(默认为3份)
    n_jobs : 并行的的任务数(默认1)
    """
    train_sizes, train_scores, test_scores = learning_curve(
        estimator,
        X,
        y,
        cv=cv,
        n_jobs=n_jobs,
        train_sizes=train_sizes,
        verbose=verbose)

    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)

    if plot:
        plt.figure()
        plt.title(title)
        if ylim is not None:
            plt.ylim(*ylim)
        plt.xlabel(u"train_sample")
        plt.ylabel(u"score")
        plt.gca().invert_yaxis()
        plt.grid()

        plt.fill_between(train_sizes,
                         train_scores_mean - train_scores_std,
                         train_scores_mean + train_scores_std,
                         alpha=0.1,
                         color="b")
        plt.fill_between(train_sizes,
                         test_scores_mean - test_scores_std,
                         test_scores_mean + test_scores_std,
                         alpha=0.1,
                         color="r")
        plt.plot(train_sizes,
                 train_scores_mean,
                 'o-',
                 color="b",
                 label=u"train_score")
        plt.plot(train_sizes,
                 test_scores_mean,
                 'o-',
                 color="r",
                 label=u"cross_validation_score")

        plt.legend(loc="best")

        plt.draw()
        plt.show()
        plt.gca().invert_yaxis()
        plt.savefig("learn_curve.jpg")

    midpoint = ((train_scores_mean[-1] + train_scores_std[-1]) +
                (test_scores_mean[-1] - test_scores_std[-1])) / 2
    diff = (train_scores_mean[-1] + train_scores_std[-1]) - (
        test_scores_mean[-1] - test_scores_std[-1])
    return midpoint, diff
Exemplo n.º 47
0
def ecdf(a, **kwargs):
    sorted=np.sort( a )
    yvals=np.arange(len(sorted))/float(len(sorted))
    plt.plot( sorted, yvals, **kwargs )
    plt.title("$\mu=%.2f, \sigma=%.2f, max=%.2f$" % (np.mean(a), np.std(a), np.max(a)))
Exemplo n.º 48
0
    for V_bias in [50, 150]:
        for n_eff in [1e12, 5e12]:
            E_x, E_y = get_field(x=None,
                                 y=y_1d,
                                 V_bias=V_bias,
                                 n_eff=n_eff,
                                 D=thickness,
                                 is_planar=True)
            plt.plot(
                y_1d,
                E_y,
                linewidth=2.,
                label='$\mathrm{V_{bias} = %d}$ V, $\mathrm{N_{eff} = %1.2e}$'
                % (V_bias, n_eff))

    plt.title('Electric field between planar sensor electrodes')
    plt.xlabel('Position [um]')
    plt.ylabel('Electric field [V/um]')
    plt.legend(loc=0)
    plt.grid()
    plt.savefig('Electric_field_planar_1D.pdf', layout='tight')
    plt.show()

    # Plot planar electric field in 2 dim, full fill factor assumed
    E_x, E_y = get_field(x,
                         y,
                         V_bias=20.,
                         n_eff=1e12,
                         D=thickness,
                         S=None,
                         is_planar=True)
Exemplo n.º 49
0
                                           dtype=float,
                                           delimiter=',')
            testLabel = numpy.argmax(testLabel, axis=1)
            auc = AUC_Calculation(testLabel=testLabel,
                                  probability=probability,
                                  figureFlag=True,
                                  showFlag=False,
                                  legend='Batch-%d' % appoint)
            result, matrix = Result_Calculation(testLabel=testLabel,
                                                probability=probability)
            result.append(auc)
            totalResult.append(result)
            matrixList.append(matrix)
        plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck')
        plt.legend()
        plt.title(savename)
        plt.xlabel('fpr (%)')
        plt.ylabel('tpr (%)')
        plt.savefig(savepath + savename + '.png')
        plt.clf()
        plt.close()
        # plt.show()

        # print(totalResult)
        book = xlwt.Workbook(encoding='utf-8', style_compression=0)
        sheet = book.add_sheet('test', cell_overwrite_ok=True)

        sheet.write_merge(0, 0, 0, 4, savename)
        sheet.write(1, 1, 'Precision')
        sheet.write(1, 2, 'Sensitivity')
        sheet.write(1, 3, 'Specificity')
Exemplo n.º 50
0
                    straighten_worm_intensity = fid.get_node(
                        '/straighten_worm_intensity')[:]
                    if straighten_worm_intensity.shape[0] < 1000:
                        continue

            except:
                bad_f.append(fname)

            s_int = np.median(straighten_worm_intensity,
                              axis=0).astype(np.uint8)
            if row['ventral_orientation'] == 'CCW':
                s_int = s_int[:, ::-1]

            save_name = os.path.join(save_dir, row['base_name'] + '_map.png')
            cv2.imwrite(save_name, s_int)

            s_intensities.append((row['day'], s_int))

        tot = len(s_intensities)
        plt.figure(figsize=(1 * tot, 7))
        for ii, (f, s_int) in enumerate(s_intensities):
            ax = plt.subplot(1, tot, ii + 1)
            plt.imshow(s_int)
            plt.title(f)
            ax.xaxis.set_visible(False)
            ax.yaxis.set_visible(False)

        strT = 'S{}_R{}_W{}'.format(row['strain'], row['replicated_n'],
                                    row['worm_id'])
        plt.suptitle(strT)
        plt.savefig(os.path.join(save_dir_s, strT + '.png'))
Exemplo n.º 51
0
import matplotlib.pylab as plt

plt.figure()
sol = odeint(satelite, z_0, T)
x = sol[:, 0]
y = sol[:, 1]
z = sol[:, 2]

r_atmosfera = (6371 + 80) * 1000
tetha = np.linspace(0, 2 * 3.14, 100)

X = r_atmosfera * np.cos(tetha)
Y = r_atmosfera * np.sin(tetha)

plt.plot(T, y)
plt.hlines(y=radio_tierra, xmin=0, xmax=14000, color="green")
plt.hlines(y=radio_orbita, xmin=0, xmax=14000, color="orange")
plt.hlines(y=-radio_tierra, xmin=0, xmax=14000, color="green")
plt.hlines(y=-radio_orbita, xmin=0, xmax=14000, color="orange")

plt.title("Distancia satelite, tierra y orbita respecto al tiempo.")

plt.ylabel("Posicion (m)")
plt.xlabel("tiempo (s)")
plt.grid(True)
plt.legend(["r(t)", "Radio tierra", "Radio orbita"])
plt.savefig("Distancia satelite, tierra y orbita respecto al tiempo.png")

plt.show()
Exemplo n.º 52
0
def testVisilibity():
    # Define an epsilon value (should be != 0.0)
    epsilon = 0.0000001

    # Define the points which will be the outer boundary of the environment
    # Must be COUNTER-CLOCK-WISE(ccw)
    p1 = vis.Point(0, 0)
    p2 = vis.Point(700, 0)
    p3 = vis.Point(700, 900)
    p4 = vis.Point(0, 900)

    # Load the values of the outer boundary polygon in order to draw it later
    wall_x = [p1.x(), p2.x(), p3.x(), p4.x(), p1.x()]
    wall_y = [p1.y(), p2.y(), p3.y(), p4.y(), p1.y()]

    # Outer boundary polygon must be COUNTER-CLOCK-WISE(ccw)
    # Create the outer boundary polygon
    walls = vis.Polygon([p1, p2, p3, p4])

    # Define the point of the "observer"
    observer = vis.Point(235, 400)

    # Uncomment the following line in order to create a cone polygon
    # walls = create_cone((observer.x(), observer.y()), 500, 270, 30, quality= 3)

    # Walls should be in standard form
    print('Walls in standard form : ', walls.is_in_standard_form())

    # Now we define some holes for our environment. The holes must be inside
    # our outer boundary polygon. A hole blocks the observer vision, it works as
    # an obstacle in his vision sensor.

    # We define some point for a hole. You can add more points in order to get
    # the shape you want.
    # The smalles point should be first
    p2 = vis.Point(100, 300)
    p3 = vis.Point(100, 500)
    p4 = vis.Point(150, 500)
    p1 = vis.Point(150, 300)

    # Load the values of the hole polygon in order to draw it later
    hole_x = [p1.x(), p2.x(), p3.x(), p4.x(), p1.x()]
    hole_y = [p1.y(), p2.y(), p3.y(), p4.y(), p1.y()]

    # Note: The point of a hole must be in CLOCK-WISE(cw) order.
    # Create the hole polygon
    hole = vis.Polygon([p2, p3, p4, p1])

    # Check if the hole is in standard form
    print('Hole in standard form: ', hole.is_in_standard_form())

    # Define another point of a hole polygon
    # Remember: the list of points must be CLOCK-WISE(cw)
    p1 = vis.Point(300, 300)
    p2 = vis.Point(300, 500)
    p3 = vis.Point(400, 550)
    p4 = vis.Point(400, 300)

    # Load the values of the hole polygon in order to draw it later
    hole1_x = [p1.x(), p2.x(), p3.x(), p4.x(), p1.x()]
    hole1_y = [p1.y(), p2.y(), p3.y(), p4.y(), p1.y()]

    # Create the hole polygon
    hole1 = vis.Polygon([p1, p2, p3, p4])

    # Check if the hole is in standard form
    print('Hole in standard form: ', hole1.is_in_standard_form())

    # Define another point of a hole polygon
    # Remember: the list of points must be CLOCK-WISE(cw)
    p2 = vis.Point(90, 700)
    p3 = vis.Point(250, 750)
    p4 = vis.Point(220, 600)
    p1 = vis.Point(150, 600)

    # Load the values of the hole polygon in order to draw it later
    hole2_x = [p1.x(), p2.x(), p3.x(), p4.x(), p1.x()]
    hole2_y = [p1.y(), p2.y(), p3.y(), p4.y(), p1.y()]

    # Create the hole polygon
    hole2 = vis.Polygon([p2, p3, p4, p1])

    # Check if the hole is in standard form
    print('Hole in standard form: ', hole2.is_in_standard_form())

    # Define another point of a hole polygon
    # Remember: the list of points must be CLOCK-WISE(cw)
    p1 = vis.Point(330, 700)
    p2 = vis.Point(330, 800)
    p3 = vis.Point(530, 850)
    p4 = vis.Point(530, 790)

    # Load the values of the hole polygon in order to draw it later
    hole3_x = [p1.x(), p2.x(), p3.x(), p4.x(), p1.x()]
    hole3_y = [p1.y(), p2.y(), p3.y(), p4.y(), p1.y()]

    # Create the hole polygon
    hole3 = vis.Polygon([p1, p2, p3, p4])

    # Check if the hole is in standard form
    print('Hole in standard form: ', hole3.is_in_standard_form())

    # Define another point of a hole polygon
    # Remember: the list of points must be CLOCK-WISE(cw)
    p1 = vis.Point(230, 50)
    p2 = vis.Point(250, 90)
    p3 = vis.Point(390, 90)
    p4 = vis.Point(390, 50)

    # Load the values of the hole polygon in order to draw it later
    hole4_x = [p1.x(), p2.x(), p3.x(), p4.x(), p1.x()]
    hole4_y = [p1.y(), p2.y(), p3.y(), p4.y(), p1.y()]

    # Create the hole polygon
    hole4 = vis.Polygon([p1, p2, p3, p4])

    # Check if the hole is in standard form
    print('Hole in standard form: ', hole4.is_in_standard_form())

    # Create environment, wall will be the outer boundary because
    # is the first polygon in the list. The other polygons will be holes
    env = vis.Environment([walls, hole, hole2, hole1, hole3, hole4])

    # Check if the environment is valid
    print('Environment is valid : ', env.is_valid(epsilon))

    # Define another point, could be used to check if the observer see it, to
    # check the shortest path from one point to the other, etc.
    end = vis.Point(330, 525)

    # Define another point that the 'observer' will see
    end_visible = vis.Point(415, 45)

    # Necessary to generate the visibility polygon
    observer.snap_to_boundary_of(env, epsilon)
    observer.snap_to_vertices_of(env, epsilon)

    # Obtain the visibility polygon of the 'observer' in the environmente
    # previously define
    isovist = vis.Visibility_Polygon(observer, env, epsilon)

    # Uncomment the following line to obtein the visibility polygon
    # of 'end' in the environmente previously define
    # polygon_vis = vis.Visibility_Polygon(end, env, epsilon)

    # Obtein the shortest path from 'observer' to 'end' and 'end_visible'
    # in the environment previously define
    shortest_path = env.shortest_path(observer, end, epsilon)
    shortest_path1 = env.shortest_path(observer, end_visible, epsilon)

    # Print the length of the path
    print("Shortest Path length from observer to end: ", shortest_path.length())
    print("Shortest Path length from observer to end_visible: ", shortest_path1.length())

    # Check if 'observer' can see 'end', i.e., check if 'end' point is in
    # the visibility polygon of 'observer'
    print("Can observer see end? ", end._in(isovist, epsilon))

    print("Can observer see end_visible? ", end_visible._in(isovist, epsilon))

    # Print the point of the visibility polygon of 'observer' and save them
    # in two arrays in order to draw the polygon later
    point_x, point_y = save_print(isovist)

    # Add the first point again because the function to draw, draw a line from
    # one point to the next one and to close the figure we need the last line
    # from the last point to the first one
    point_x.append(isovist[0].x())
    point_y.append(isovist[0].y())

    # Set the title
    p.title('VisiLibity Test')

    # Set the labels for the axis
    p.xlabel('X Position')
    p.ylabel('Y Position')

    # Plot the outer boundary with black color
    p.plot(wall_x, wall_y, 'black')

    # Plot the position of the observer with a green dot ('go')
    p.plot([observer.x()], [observer.y()], 'go')

    # Plot the position of 'end' with a green dot ('go')
    p.plot([end.x()], [end.y()], 'go')

    # Plot the position of 'end_visible' with a green dot ('go')
    p.plot([end_visible.x()], [end_visible.y()], 'go')

    # Plot the visibility polygon of 'observer'
    p.plot(point_x, point_y)

    # Plot the hole polygon with red color
    p.plot(hole_x, hole_y, 'r')

    # Plot the hole polygon with red color
    p.plot(hole1_x, hole1_y, 'r')

    # Plot the hole polygon with red color
    p.plot(hole2_x, hole2_y, 'r')

    # Plot the hole polygon with red color
    p.plot(hole3_x, hole3_y, 'r')

    # Plot the hole polygon with red color
    p.plot(hole4_x, hole4_y, 'r')

    # Example of a cone-shape polygon
    cone_point = vis.Point(440, 420)
    cone = create_cone([cone_point.x(), cone_point.y()], 150, 0, 45, 3)
    cone_x, cone_y = save_print(cone)
    cone_x.append(cone_x[0])
    cone_y.append(cone_y[0])
    p.plot([cone_point.x()], [cone_point.y()], 'go')
    p.plot(cone_x, cone_y)

    # Show the plot
    p.show()
Exemplo n.º 53
0
Arquivo: Test.py Projeto: Wayne0980/AI
# 來看看 mnist 的型態
print(mnist.train.num_examples)
print(mnist.validation.num_examples)
print(mnist.test.num_examples)

train_img = mnist.train.images
train_label = mnist.train.labels
test_img = mnist.test.images
test_label = mnist.test.labels
print
print(" train_img 的 type : %s" % (type(train_img)))
print(" train_img 的 dimension : %s" % (train_img.shape, ))
print(" train_label 的 type : %s" % (type(train_label)))
print(" train_label 的 dimension : %s" % (train_label.shape, ))
print(" test_img 的 type : %s" % (type(test_img)))
print(" test_img 的 dimension : %s" % (test_img.shape, ))
print(" test_label 的 type : %s" % (type(test_label)))
print(" test_label 的 dimension : %s" % (test_label.shape, ))

nsample = 1
randidx = np.random.randint(train_img.shape[0], size=nsample)

for i in [0, 1, 2]:
    curr_img = np.reshape(train_img[i, :], (28, 28))  # 28 by 28 matrix
    curr_label = np.argmax(train_label[i, :])  # Label
    plt.matshow(curr_img, cmap=plt.get_cmap('gray'))
    plt.title("" + str(i + 1) + "th Training Data " + "Label is " +
              str(curr_label))

plt.show()
Exemplo n.º 54
0
def sigmoid(x):
    return 1 / (np.exp(-x) + 1)  # 既可以对数值变量计算,还可以对向量进行计算


x = np.random.randn(1000, 100)  # 1000组数据

node_num = 100

hidden_layer_size = 5

activations = dict()  # 在数字索引时和列表一样

# 开始逐层迭代运算,主要是我对绘图函数还是不熟悉

for i in range(hidden_layer_size):
    if i != 0:
        x = activations[i - 1]
    # w = np.random.randn(node_num,node_num) *1
    w = np.random.randn(node_num, node_num) * 0.01
    z = np.dot(x, w)
    z = sigmoid(z)
    activations[i] = z
# 开始根据activations绘图
plt.rcParams['figure.figsize'] = (10, 5)
#plt.rcParams['figure.dpi'] = 300
for i, a in activations.items():
    plt.subplot(1, len(activations), i + 1)
    plt.title(str(i + 1) + "layer")
    plt.hist(a.flatten(), 30, range=(0, 1))
plt.show()
Exemplo n.º 55
0
import numpy as np
import matplotlib.pylab as plt
from scipy.fftpack import *
##Almacenamiento de los datos##
data1 = np.genfromtxt("signalSuma.dat")
data2 = np.genfromtxt("signal.dat")
#print len(data1[:,1])
N = 2048
dt = abs(data1[1, 0] - data1[2, 0])
##subplots con el grafico para cada senal##
plt.figure()
plt.subplot(2, 1, 1)
plt.title("signalSuma")
plt.xlabel("Tiempo(s)")
plt.ylabel("Amplitud")
plt.plot(data1[:, 0], data1[:, 1])

plt.subplot(2, 1, 2)
plt.title("signal")
plt.xlabel("Tiempo(s)")
plt.ylabel("Amplitud")
plt.plot(data2[:, 0], data2[:, 1])

plt.savefig("signal_subplots.pdf")


##implementacion de la transformada discreta de fourier##
def Fourier(N, data):
    amplitudes = []
    for n in range(N):
        suma = 0
Exemplo n.º 56
0
def cob_health(args):
    log = coblog()
    log(f'\n'
        f'-----------------------------\n'
        f'   Network Health:{args.cob} \n'
        f'-----------------------------\n'
    )
    log(f"\nCreating reports in {os.getcwd()}\n\n")

    cob = co.COB(args.cob)
    if args.out is None:
        args.out = '{}_Health'.format(cob.name)
    log(f'Output prefix: {args.out}')


    if args.edge_zscore_cutoff is not None:
        log("Changing Z-Score cutoff to {}",args.edge_zscore_cutoff)
        cob.set_sig_edge_zscore(args.edge_zscore_cutoff)

    log('Printing Summary ---------------------------------------------------')
    if not path.exists('{}.summary.txt'.format(args.out)):
        with open('{}.summary.txt'.format(args.out),'w') as OUT:
            # Print out the network summary
            cob.summary(file=OUT)
    else:
        log('Skipped summary.')

    log('Plotting Scores ----------------------------------------------------') 
    if not path.exists('{}_CoexPCC_raw.png'.format(args.out)):
        cob.plot_scores(
            '{}_CoexPCC_raw.png'.format(args.out),
            pcc=True
        )
    else:
        log('Skipped Raw.')

    if not path.exists('{}_CoexScore_zscore.png'.format(args.out)):
        cob.plot_scores(
            '{}_CoexScore_zscore.png'.format(args.out),
            pcc=False
        )
    else:
        log('Skipped Norm.')

    log('Plotting Expression ------------------------------------------------')
   #if not path.exists('{}_Expr_raw.png'.format(args.out)):
   #    cob.plot(
   #        '{}_Expr_raw.png'.format(args.out),
   #        include_accession_labels=True,
   #        raw=True,
   #        cluster_method=None
   #    )
   #else:
   #    log('Skipped raw.')
    if not path.exists('{}_Expr_norm.png'.format(args.out)):
        cob.plot_heatmap(
            '{}_Expr_norm.png'.format(args.out),
            include_accession_labels=True,
            raw=False,
            cluster_method='ward',
            cluster_accessions=True
        )
    else:
        log('Skipped norm.')
   #log('Plotting Cluster Expression-----------------------------------------')
   #if not path.exists('{}_Expr_cluster.png'.format(args.out)):
   #    cob.plot(
   #        '{}_Expr_cluster.png'.format(args.out),
   #        include_accession_labels=True,
   #        raw=False,
   #        cluster_accessions=True,
   #        avg_by_cluster=True
   #    )
   #else:
   #    log('Skipped norm.')

    log('Printing QC Statistics ---------------------------------------------')
    if args.refgen is not None:
        if not path.exists('{}_qc_gene.txt'.format(args.out)):
            # Print out the breakdown of QC Values
            refgen = co.RefGen(args.refgen)
            gene_qc = cob._bcolz('qc_gene')
            gene_qc = gene_qc[gene_qc.pass_membership]
            gene_qc['chrom'] = ['chr'+str(refgen[x].chrom) for x in gene_qc.index]
            gene_qc = gene_qc.groupby('chrom').agg(sum,axis=0)
            # Add totals at the bottom
            totals = gene_qc.ix[:,slice(1,None)].apply(sum)
            totals.name = 'TOTAL'
            gene_qc = gene_qc.append(totals)
            gene_qc.to_csv('{}_qc_gene.txt'.format(args.out),sep='\t')
        else:
            log('Skipped QC summary.')

    log('Plotting Degree Distribution ---------------------------------------')
    if not path.exists('{}_DegreeDist.png'.format(args.out)):
        degree = cob.degree['Degree'].values
        # Using powerlaw makes run-time warning the first time you use it.
        # This is still an open issue on the creators github.
        # The creator recommends removing this warning as long as there is a fit.
        np.seterr(divide='ignore', invalid='ignore')
        fit = powerlaw.Fit(degree,discrete=True,xmin=1)
        # get an axis
        ax = plt.subplot()
        # Calculate log ratios
        t2p = fit.distribution_compare('truncated_power_law', 'power_law')
        t2e = fit.distribution_compare('truncated_power_law', 'exponential')
        p2e = fit.distribution_compare('power_law','exponential')
        # Plot!
        emp = fit.plot_ccdf(ax=ax,color='r',linewidth=3, label='Empirical Data')
        pwr = fit.power_law.plot_ccdf(ax=ax,linewidth=2, color='b', linestyle=':', label='Power law')
        tpw = fit.truncated_power_law.plot_ccdf(ax=ax,linewidth=2, color='k', linestyle='-.', label='Truncated Power')
        exp = fit.exponential.plot_ccdf(ax=ax,linewidth=2, color='g', linestyle='--', label='Exponential')
        ####
        ax.set_ylabel("p(Degree≥x)")
        ax.set_xlabel("Degree Frequency")
        ax.legend(
            loc='best'
        )
        plt.title('{} Degree Distribution'.format(cob.name))
        # Save Fig
        try:
            plt.savefig('{}_DegreeDist.png'.format(args.out))
        except FutureWarning as e:
            # This is a matplotlib bug
            pass
    else:
        log('Skipping Degree Dist.')

    if args.go is not None:
        log('Plotting GO --------------------------------------------------------')
        # Set the alpha based on the tails
        if args.two_tailed == True:
            alpha = 0.05 /2
        else:
            alpha = 0.05
        # Generate the GO Table
        if not path.exists('{}_GO.csv'.format(args.out)):
            go = co.GOnt(args.go)
            term_ids = []
            density_emp = []
            density_pvals  = []
            locality_emp = []
            locality_pvals = []
            term_sizes = []
            term_desc = []
            terms_tested = 0
            # max_terms limits the number of GO terms tested (sub-sampling)
            if args.max_terms is not None:
                log('Limiting to {} GO Terms',args.max_terms)
                terms = go.rand(
                    n=args.max_terms,
                    min_term_size=args.min_term_size,
                    max_term_size=args.max_term_size
                )
            else:
                # Else do the whole set (default is terms between 10 and 300 genes)
                terms = go.iter_terms(
                    min_term_size=args.min_term_size,
                    max_term_size=args.max_term_size
                )
            for term in terms:
                # Some terms will lose genes that are not in networks
                term.loci = list(filter(lambda x: x in cob, term.loci))
                # Skip terms that are not an adequate size
                if len(term) < args.min_term_size or len(term) > args.max_term_size:
                    continue
                # set density value for two tailed go so we only test it once
                density = cob.density(term.loci)
                # one tailed vs two tailed test
                density_emp.append(density)
                # 
                term_ids.append(term.id)
                term_sizes.append(len(term))
                term_desc.append(str(term.desc))
                # ------ Density 
                # Calculate PVals
                density_bs = np.array([
                    cob.density(cob.refgen.random_genes(n=len(term.loci))) \
                    for x in range(args.num_bootstraps)
                ])
                if density > 0:
                    pval = sum(density_bs >= density)/args.num_bootstraps
                else:
                    pval = sum(density_bs <= density)/args.num_bootstraps
                density_pvals.append(pval)

                # ------- Locality
                locality = cob.locality(
                    term.loci,include_regression=True
                ).resid.mean()
                locality_emp.append(locality)
                # Calculate PVals
                locality_bs = np.array([
                    cob.locality(
                        cob.refgen.random_genes(n=len(term.loci)),
                        include_regression=True
                    ).resid.mean() \
                    for x in range(args.num_bootstraps)
                ])
                if locality > 0:
                    pval = sum(locality_bs >= locality)/args.num_bootstraps
                else:
                    pval = sum(locality_bs <= locality)/args.num_bootstraps
                locality_pvals.append(pval)
                # -------------
                terms_tested += 1
                if terms_tested % 100 == 0 and terms_tested > 0:
                    log('Processed {} terms'.format(terms_tested)) 
            go_enrichment = pd.DataFrame({
                'GOTerm' : term_ids,
                'desc' : term_desc,
                'size' : term_sizes,
                'density' : density_emp,
                'density_pval' : density_pvals,
                'locality' : locality_emp,
                'locality_pval' : locality_pvals
            })
            # Calculate significance
            go_enrichment['density_significant'] = go_enrichment.density_pval < alpha
            go_enrichment['locality_significant'] = go_enrichment.locality_pval < alpha
            # Calculate bonferonni
            go_enrichment['density_bonferroni'] = go_enrichment.density_pval < (alpha / len(go_enrichment))
            go_enrichment['locality_bonferroni'] = go_enrichment.locality_pval < (alpha / len(go_enrichment))
            # Store the GO results in a CSV
            go_enrichment\
                .sort_values(by='density_pval',ascending=True)\
                .to_csv('{}_GO.csv'.format(args.out),index=False)
            if terms_tested == 0:
                log.warn('No GO terms met your min/max gene criteria!')
        else:
            go_enrichment = pd.read_table('{}_GO.csv'.format(args.out),sep=',')

        if not path.exists('{}_GO.png'.format(args.out)):
            # Convert pvals to log10
            with np.errstate(divide='ignore'):
                # When no bootstraps are more extreme than the term, the minus log pval yields an infinite
                go_enrichment['density_pval'] = -1*np.log10(go_enrichment['density_pval'])
                go_enrichment['locality_pval'] = -1*np.log10(go_enrichment['locality_pval'])
                # Fix the infinites so they are plotted
                max_density = np.max(go_enrichment['density_pval'][np.isfinite(go_enrichment['density_pval'])])
                max_locality = np.max(go_enrichment['locality_pval'][np.isfinite(go_enrichment['locality_pval'])])
                go_enrichment.loc[np.logical_not(np.isfinite(go_enrichment['density_pval'])),'density_pval'] = max_density + 1
                go_enrichment.loc[np.logical_not(np.isfinite(go_enrichment['locality_pval'])),'locality_pval'] = max_locality + 1
            plt.clf()
            # Calculate the transparency based on the number of terms
            if len(go_enrichment) > 20:
                transparency_alpha = 0.05
            else:
                transparency_alpha = 1

            # --------------------------------------------------------------------
            # Start Plotting
            figure,axes = plt.subplots(3,2,figsize=(12,12))
            # -----------
            # Density
            # ----------
            log_alpha = -1 *np.log10(alpha)
            axes[0,0].scatter(
                go_enrichment['density'],
                go_enrichment['density_pval'],
                alpha=transparency_alpha,
                color='blue'
            )
            axes[0,0].set_xlabel('Empirical Density (Z-Score)')
            axes[0,0].set_ylabel('Bootstraped -log10(p-value)')
            fold = sum(np.array(go_enrichment['density_pval'])> log_alpha )/(alpha * len(go_enrichment))
            axes[0,0].axhline(y=-1*np.log10(0.05),color='red')
            axes[0,0].text(
                min(axes[0,0].get_xlim()),
                -1*np.log10(alpha) + 0.1,
                '{:.3g} Fold Enrichement'.format(fold),
                color='red'
            )
            axes[0,0].set_title('Density Health')
            # Plot pvalue by term size
            axes[1,0].scatter(
                go_enrichment['size'],
                go_enrichment['density_pval'],
                alpha=transparency_alpha,
                color='blue'
            )
            axes[1,0].set_ylabel('Bootstrapped -log10(p-value)')
            axes[1,0].set_xlabel('Term Size')
            axes[1,0].axhline(y=-1*np.log10(alpha),color='red')
            axes[2,0].scatter(
                go_enrichment['size'],
                go_enrichment['density'],
                alpha=transparency_alpha,
                color='blue'
            )
            # Plot raw density by term size
            axes[2,0].scatter(
                go_enrichment.query(f'density_pval>{log_alpha}')['size'],
                go_enrichment.query(f'density_pval>{log_alpha}')['density'],
                alpha=transparency_alpha,
                color='r'
            )
            axes[2,0].set_ylabel('Density')
            axes[2,0].set_xlabel('Term Size')
            # ------------
            # Do Locality
            # ------------
            axes[0,1].scatter(
                go_enrichment['locality'],
                go_enrichment['locality_pval'],
                alpha=transparency_alpha,
                color='blue'
            )
            axes[0,1].set_xlabel('Empirical Locality (Residual)')
            axes[0,1].set_ylabel('Bootstraped -log10(p-value)')
            fold = sum(np.array(go_enrichment['locality_pval'])>log_alpha)/(0.05 * len(go_enrichment))
            axes[0,1].axhline(y=-1*np.log10(0.05),color='red')
            axes[0,1].text(
                min(axes[0,1].get_xlim()),
                -1*np.log10(alpha),
                '{:.3g} Fold Enrichement'.format(fold),
                color='red'
            )
            axes[0,1].set_title('Locality Health')
            axes[1,1].scatter(
                go_enrichment['size'],
                go_enrichment['locality_pval'],
                alpha=transparency_alpha,
                color='blue'
            )
            axes[1,1].set_xlabel('Term Size')
            axes[1,1].set_ylabel('Bootstrapped -log10(p-value)')
            axes[1,1].axhline(y=-1*np.log10(0.05),color='red')
            axes[2,1].scatter(
                go_enrichment['size'],
                go_enrichment['locality'],
                alpha=transparency_alpha,
                color='blue' 
            )
            axes[2,1].scatter(
                go_enrichment.query(f'locality_pval>{log_alpha}')['size'],
                go_enrichment.query(f'locality_pval>{log_alpha}')['locality'],
                alpha=transparency_alpha,
                color='r'
            )
            axes[2,1].set_ylabel('Locality')
            axes[2,1].set_xlabel('Term Size')
            # Save Figure
            plt.tight_layout()
            try:
                plt.savefig('{}_GO.png'.format(args.out))
            except FutureWarning as e:
                pass
        else:
            log('Skipping GO Volcano.')
Exemplo n.º 57
0
                           index_col=0,
                           parse_dates=True,
                           squeeze=True)
venda_carros = venda_carros[:-1]
plt.plot(venda_carros)

#Teste de estacionariedade na série temporal
y = venda_carros
kpss_teste(y)
'''H0 (p >= 0.05) = A série é estacionária.
   H1 (p < 0.05) = A série não é estacionária (apresenta raiz unitária).'''

#Aplicando a diferenciação de 1ª Ordem
y_diff = diff(y)

#Comparação entre a série antes e depois da diferenciação de 1 Ordem
plt.figure(figsize=(15, 5))
plt.subplot(2, 1, 1)
plt.title('Antes da diferenciação de 1ª Ordem')
plt.plot(venda_carros)
plt.subplot(2, 1, 2)
plt.title('Depois da diferenciação de 1ª Ordem')
plt.plot(y_diff)
plt.tight_layout()
plt.show()

#Teste de estacionariedade após a diferenciação
kpss_teste(y_diff)
'''Com apenas a diferenciação de 1 ordem, o valor de p é maior do que 0.05,
então a hipótese nula está mantida, só que diferente do Dickey Fuller
a hipótese nula significa que a série é estacionária'''
Exemplo n.º 58
0
import numpy as np
import matplotlib as mpl
import matplotlib.pylab as plt

X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)
plt.title("legend를 표시한 플롯")
plt.plot(X, C, ls="--", label="cosine")
plt.plot(X, S, ls=":", label="sine")
plt.legend(loc=2)
plt.show()
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from CALPHAD_energies import *

labels = [r'$\gamma$', r'$\delta$', 'Laves']
colors = ['red', 'green', 'blue']

tmax = 5625  # 3750
pmax = 0.35
dt = 7.5e-5 * 1000
area = 320. * 192. * (5e-9)**2

# Plot difference in size between delta and Laves precipitates
plt.figure(1, figsize=(10, 7.5))  # inches
plt.title("Cr-Nb-Ni at %.0f K" % temp, fontsize=18)
plt.xlabel(r'$t$ / s', fontsize=18)
plt.ylabel(
    r'Relative Phase Fraction $\frac{\phi_{\delta}-\phi_{\mathrm{L}}}{\phi_{\delta}+\phi_{\mathrm{L}}}$',
    fontsize=18)
plt.ylim([-1, 1])
plt.plot((0, tmax), (0, 0), color='black', zorder=1)

# Generate combined plots (ternary and trajectories); store individual data
datasets = []
fmax = 0.
for datdir in tqdm(
        glob.glob('/data/tnk10/phase-field/alloy625/TKR4p149/run*/')):
    if path.isdir(datdir) and len(glob.glob("{0}/c.log".format(datdir))) > 0:
        base = path.basename(datdir)
        try:
Exemplo n.º 60
0
show_features = [
    'delinq_2yrs', 'fico_range_low', 'fico_range_high', 'inq_last_6mths',
    'mths_since_last_record'
]
pylab.figure(figsize=[8, 5])
for feature in show_features:
    pylab.plot(range(2, month_max + 1),
               PSI_df.loc[feature],
               '-o',
               label=feature)
pylab.legend(loc='upper left')
pylab.ylabel("PSI")
pylab.xlabel("Month")
pylab.ylim([0, 0.01])
pylab.title('Stability of Different Features: Compared with Jan')
pylab.savefig('../output/Stability_compare_with_Jan.png')

print '\n计算各月同上月比较的PSI值:'
PSI_list2 = []
for after_time in range(2, month_max + 1):
    before_time = after_time - 1
    cur_df = df[df['issue_d'].isin([before_time, after_time])]
    PSI_list2.append(get_PSI_features(cur_df['issue_d'], cur_df[feature_cols]))
    print '完成所有特征在' + str(before_time) + '月和' + str(after_time) + '月的比较'
index_names = [
    'PSI_' + str(i - 1) + '_' + str(i) for i in range(2, month_max + 1)
]
PSI_df2 = pd.DataFrame(PSI_list2, columns=feature_cols, index=index_names).T
pylab.figure(figsize=[8, 5])
for feature in show_features: