def plot_tuning_curves(direction_rates, title):
    """
    This function takes the x-values and the y-values  in units of spikes/s 
    (found in the two columns of direction_rates) and plots a histogram and 
    polar representation of the tuning curve. It adds the given title.
    """
    x = direction_rates[:,0]
    y = direction_rates[:,1]
    plt.figure()
    plt.subplot(2,2,1)
    plt.bar(x,y,width=45,align='center')
    plt.xlim(-22.5,337.5)
    plt.xticks(x)
    plt.xlabel('Direction of Motion (degrees)')
    plt.ylabel('Firing Rate (spikes/s)')
    plt.title(title)   
        
        
    
    plt.subplot(2,2,2,polar=True)
    r = np.append(y,y[0])
    theta = np.deg2rad(np.append(x, x[0]))
    plt.polar(theta,r,label='Firing Rate (spikes/s)')
    plt.legend(loc=8)
    plt.title(title)
Example #2
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")
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)
def plot_degreeRate(db, keynames, save_path):
	degRate_x_name = 'degRateDistr_x'
	degRate_y_name = 'degRateDistr_y'

	plt.clf()
	plt.figure(figsize = (8, 5))

	plt.subplot(1, 2, 1)
	plt.plot(db[keynames['mog']][degRate_x_name], db[keynames['mog']][degRate_y_name], 'b-', lw=5, label = 'fairyland')
	plt.plot(db[keynames['mblg']][degRate_x_name], db[keynames['mblg']][degRate_y_name], 'r:', lw=5, label = 'twitter')
	plt.plot(db[keynames['im']][degRate_x_name], db[keynames['im']][degRate_y_name], 'k--', lw=5, label = 'yahoo')
	plt.xscale('log')
	plt.grid(True)
	plt.title('interaction')
	plt.legend(('fairyland', 'twitter', 'yahoo'), loc = 4, prop = {'size': 10})
	plt.xlabel('In-degree to Out-degree Ratio')
	plt.ylabel('CDF')

	plt.subplot(1, 2, 2)
	plt.plot(db[keynames['mogF']][degRate_x_name], db[keynames['mogF']][degRate_y_name], 'b-', lw=5, label = 'fairyland')
	plt.plot(db[keynames['mblgF']][degRate_x_name], db[keynames['mblgF']][degRate_y_name], 'r:', lw=5, label = 'twitter')
	#plt.plot(db[keynames['imF']][degRate_x_name], db[keynames['imF']][degRate_y_name], 'k--', lw=5, label = 'yahoo')
	plt.xscale('log')
	plt.grid(True)
	plt.title('ally')
	plt.xlabel('In-degree to Out-degree Ratio')
	plt.ylabel('CDF')

	plt.savefig(os.path.join(save_dir, save_path))
Example #5
0
    def __init__(self, fig, gs, num_plots, rows=None, cols=None):
        if cols is None:
            cols = int(np.floor(np.sqrt(num_plots)))
        if rows is None:
            rows = int(np.ceil(float(num_plots)/cols))
        assert num_plots <= rows*cols, 'Too many plots to put into gridspec.'

        self._fig = fig
        self._gs = gridspec.GridSpecFromSubplotSpec(8, 1, subplot_spec=gs)
        self._gs_legend = self._gs[0:1, 0]
        self._gs_plot   = self._gs[1:8, 0]

        self._ax_legend = plt.subplot(self._gs_legend)
        self._ax_legend.get_xaxis().set_visible(False)
        self._ax_legend.get_yaxis().set_visible(False)

        self._gs_plots = gridspec.GridSpecFromSubplotSpec(rows, cols, subplot_spec=self._gs_plot)
        self._axarr = [plt.subplot(self._gs_plots[i], projection='3d') for i in range(num_plots)]
        self._lims = [None for i in range(num_plots)]
        self._plots = [[] for i in range(num_plots)]

        for ax in self._axarr:
            ax.tick_params(pad=0)
            ax.locator_params(nbins=5)
            for item in (ax.get_xticklabels() + ax.get_yticklabels() + ax.get_zticklabels()):
                item.set_fontsize(10)

        self._fig.canvas.draw()
        self._fig.canvas.flush_events()   # Fixes bug with Qt4Agg backend
Example #6
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()
Example #7
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 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",
        )
Example #9
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 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()
Example #11
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",
        )
Example #12
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()
Example #13
0
def test_likelihood_evaluator3():
    
    tr = template.TemplateRenderCircleBorder()
    tr.set_params(14, 6, 4)

    t1 = tr.render(0, np.pi/2)
    img = np.zeros((240, 320), dtype=np.uint8)

    env = util.Environmentz((1.5, 2.0), (240, 320))
    
    le2 = likelihood.LikelihoodEvaluator3(env, tr)

    img[(120-t1.shape[0]/2):(120+t1.shape[0]/2), 
        (160-t1.shape[1]/2):(160+t1.shape[1]/2)] += t1 *255
    pylab.subplot(1, 2, 1)
    pylab.imshow(img, interpolation='nearest', cmap=pylab.cm.gray)

    state = np.zeros(1, dtype=util.DTYPE_STATE)

    xvals = np.linspace(0, 2.,  100)
    yvals = np.linspace(0, 1.5, 100)
    res = np.zeros((len(yvals), len(xvals)), dtype=np.float32)
    for yi, y in enumerate(yvals):
        for xi, x in enumerate(xvals):
            state[0]['x'] = x
            state[0]['y'] = y
            state[0]['theta'] = np.pi / 2. 
            res[yi, xi] =     le2.score_state(state, img)
    pylab.subplot(1, 2, 2)
    pylab.imshow(res)
    pylab.colorbar()
    pylab.show()
Example #14
0
def find_params():

    FRAMES =  np.arange(30)*100

    frame_images = organizedata.get_frames(ddir("bukowski_04.W2"), FRAMES)
    print "DONE READING DATA"

    CLUST_EPS = np.linspace(0, 0.5, 10)
    MIN_SAMPLES = [2, 3, 4, 5]
    MIN_DISTS = [2, 3, 4, 5, 6]
    THOLD = 240

    fracs_2 = np.zeros((len(CLUST_EPS), len(MIN_SAMPLES), len(MIN_DISTS)))

    for cei, CLUST_EP in enumerate(CLUST_EPS):
        for msi, MIN_SAMPLE in enumerate(MIN_SAMPLES):
            for mdi, MIN_DIST in enumerate(MIN_DISTS):
                print cei, msi, mdi
                numclusters = np.zeros(len(FRAMES))
                for fi, im in enumerate(frame_images):
                    centers = frame_clust_points(im, THOLD, MIN_DIST, 
                                                 CLUST_EP, MIN_SAMPLE)
                    # cluster centers
                    numclusters[fi] = len(centers)
                fracs_2[cei, msi, mdi] = float(np.sum(numclusters == 2))/len(numclusters)
    pylab.figure(figsize=(12, 8))
    for mdi, MIN_DIST in enumerate(MIN_DISTS):
        pylab.subplot(len(MIN_DISTS), 1, mdi+1)

        for msi in range(len(MIN_SAMPLES)):
            pylab.plot(CLUST_EPS, fracs_2[:, msi, mdi], label='%d' % MIN_SAMPLES[msi])
        pylab.title("min_dist= %3.2f" % MIN_DIST)
    pylab.legend()
    pylab.savefig('test.png', dpi=300)
Example #15
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
Example #16
0
def plot_beta_dist( ctr, trials, success, alphas, betas, turns ):
	"""
	Pass in the ctr, trials and success, alphas, betas returned
	by the `experiment` function and the number of turns 
	and plot the beta distribution for all the arms in that turn
	"""
	subplot_num = len(turns) / 2
	x = np.linspace( 0.001, .999, 200 )
	fig = plt.figure( figsize = ( 14, 7 ) ) 

	for idx, turn in enumerate(turns):

		plt.subplot( subplot_num, 2, idx + 1 )

		for i in range( len(ctr) ):
			y = beta( alphas[i] + success[ turn, i ], 
					  betas[i] + trials[ turn, i ] - success[ turn, i ] ).pdf(x)
			line = plt.plot( x, y, lw = 2, label = "arm {}".format( i + 1 ) )
			color = line[0].get_color()
			plt.fill_between( x, 0, y, alpha = 0.2, color = color )
			plt.axvline( x = ctr[i], color = color, linestyle = "--", lw = 2 )
			plt.title("Posteriors After {} turns".format(turn) )
			plt.legend( loc = "upper right" )

	return fig
Example #17
0
def test():
	## Load files
    s = load_spectrum('ring28yael')
    w = linspace(1510e-9,1600e-9,len(s))
    
	## Process
    mins = find_minima(s)
    w_p = 1510e-9 + array(mins) * 90.e-9/len(w)
    ww = 2 * pi * 3e8/w_p   
    
	## Plot
    pl.plot(w,s)
    pl.plot(w_p,s[mins],'o')
    pl.show()
    
    beta2 = -1./(112e-6*2*pi)*diff(diff(ww))/(diff(ww)[:-1]**3)
    p = polyfit(w_p[1:-1], beta2, 1)
    
    savetxt('ring28yael-p.txt', w_p)
    
    pl.subplot(211)
    pl.plot(w,s)
    pl.plot(w_p,s[mins],'o')
    
    pl.subplot(212)
    pl.plot(w_p[1:-1]*1e6, beta2)
    pl.plot(w_p[1:-1]*1e6, p[1]+ p[0]*w_p[1:-1], label="q=%.2e"%p[0])
    pl.legend()
        
    pl.show()
Example #18
0
def plot_imgs(imgs, ratios=[1, 1]):
    plt.gray()
    gs = gridspec.GridSpec(1, len(imgs), width_ratios=ratios)
    for i in range(len(imgs)):
        plt.subplot(gs[i])
        plt.imshow(imgs[i])
    return gs
Example #19
0
def experiment_plot( ctr, trials, success ):
	"""
	Pass in the ctr, trials and success returned
	by the `experiment` function and plot
	the Cumulative Number of Turns For Each Arm and
	the CTR's Convergence Plot side by side
	"""
	T, K = trials.shape
	n = np.arange(T) + 1
	fig = plt.figure( figsize = ( 14, 7 ) )

	plt.subplot(121)	
	for i in range(K):
		plt.loglog( n, trials[ :, i ], label = "arm {}".format(i + 1) )

	plt.legend( loc = "upper left" )
	plt.xlabel("Number of turns")
	plt.ylabel("Number of turns/arm")
	plt.title("Cumulative Number of Turns For Each Arm")

	plt.subplot(122)
	for i in range(K):
		plt.semilogx( n, np.zeros(T) + ctr[i], label = "arm {}'s CTR".format( i + 1 ) )

	plt.semilogx( n, ( success[ :, 0 ] + success[ :, 1 ] ) / n, label = "CTR at turn t" )

	plt.axis([ 0, T, 0, 1 ] )
	plt.legend( loc = "upper left" )
	plt.xlabel("Number of turns")
	plt.ylabel("CTR")
	plt.title("CTR's Convergence Plot")

	return fig
Example #20
0
def plot_all_dep(data, with_subplots=True, remove_weekday=False):
    country_codes = list(data.country_code.unique())
    channels = list(data.marketing_channel.unique())
    ny = len(country_codes)
    nx = len(channels)
    plot_num = 1

    abrevs = channel_abbrevs()

    for channel in channels:
        for country_code in country_codes:
            data_one = data[(data.country_code == country_code)
                            & (data.marketing_channel == channel)]
            if len(data_one) > 2:
                if with_subplots:
                    plt.subplot(nx, ny, plot_num)
                dates = data_one.date
                y = data_one.user_visits
                if remove_weekday:
                    y = remove_weekday_seasonality(dates, y)
                plt.plot(dates, y)
                if with_subplots:
                    title = abrevs[channel] + '_' + country_code
                    plt.title(title)
                    plot_num += 1
Example #21
0
 def test2_varRegrid(self):
     print
     print 'test2_varRegrid'
     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')
     diag = {'srcAreas': None, 'dstAreas': None,
             'srcAreaFractions': None, 'dstAreaFractions': None}
     soInterp = so.regrid(clt.getGrid(), 
                          regridTool = 'esmf', 
                          regridMethod='conserve', 
                    	     diag = diag)
     if self.pe == 0:
         totSrcArea = diag['srcAreas'].sum()
         totDstArea = diag['dstAreas'].sum()
         totSrcFrac = diag['srcAreaFractions'].sum()
         self.assertEqual(numpy.isnan(totSrcFrac).sum(), 0)
         self.assertLess(abs(totSrcArea - 4*pi)/(4*pi), 0.02)
         self.assertLess(abs(totDstArea - 4*pi)/(4*pi), 0.01)
         soMass = (so*diag['srcAreas']).sum()
         inMass = (soInterp*diag['dstAreas']).sum()
         print soMass, inMass
         diff = abs(soMass - inMass)/soMass
         self.assertLess(diff, 7.e-7)
     if False:
         pylab.subplot(1, 2, 1)
         pylab.pcolor(so, vmin = 20, vmax = 40)
         pylab.colorbar()
         pylab.title('so')
         pylab.subplot(1, 2, 2)
         pylab.pcolor(soInterp, vmin = 20, vmax = 40)
         pylab.colorbar()
         pylab.title('soInterp')
def chooseDegree(npts, mindegree=0, maxdegree=20, filename=None):
    """Gets noisy data, uses cross validation to estimate error, and fits new data with best model."""
    x, y = bv.noisyData(npts)
    degrees = numpy.arange(mindegree,maxdegree+1)
    errs = numpy.zeros_like(degrees,dtype=numpy.float)
    for i,d in enumerate(degrees):
        errs[i] = estimateError(x, y, d)

    plt.subplot(1,2,1)
    plt.plot(degrees,errs,'bo-')
    plt.xlabel("Degree")
    plt.ylabel("CV Error")

    besti = numpy.argmin(errs)
    bestdegree = degrees[besti]

    plt.subplot(1,2,2)
    x2, y2 = bv.noisyData(npts)
    plt.plot(x2,y2,'ro')
    xs = numpy.linspace(min(x),max(x),150)
    fitf = numpy.poly1d(numpy.polyfit(x2,y2,bestdegree))
    plt.plot(xs,fitf(xs),'g-',lw=2)
    plt.xlim((bv.MIN,bv.MAX))
    plt.ylim((-2.,2.))
    plt.suptitle('Selected Degree '+str(bestdegree))
    bv.outputPlot(filename)
Example #23
0
 def plot(x,y,field,filename,c=200):
     plt.figure()
     # define grid.
     xi = np.linspace(min(x),max(x),100)
     yi = np.linspace(min(y),max(y),100)
     # grid the data.
     si_lin = griddata((x, y), field, (xi[None,:], yi[:,None]), method='linear')
     si_cub = griddata((x, y), field, (xi[None,:], yi[:,None]), method='linear')
     print np.min(field)
     print np.max(field)
     plt.subplot(211)
     # contour the gridded data, plotting dots at the randomly spaced data points.
     CS = plt.contour(xi,yi,si_lin,c,linewidths=0.5,colors='k')
     CS = plt.contourf(xi,yi,si_lin,c,cmap=plt.cm.jet)
     plt.colorbar() # draw colorbar
     # plot data points.
     #    plt.scatter(x,y,marker='o',c='b',s=5)
     plt.xlim(min(x),max(x))
     plt.ylim(min(y),max(y))
     plt.title('Lineaarinen interpolointi')
     #plt.tight_layout()
     plt.subplot(212)
     # contour the gridded data, plotting dots at the randomly spaced data points.
     CS = plt.contour(xi,yi,si_cub,c,linewidths=0.5,colors='k')
     CS = plt.contourf(xi,yi,si_cub,c,cmap=plt.cm.jet)
     plt.colorbar() # draw colorbar
     # plot data points.
     #    plt.scatter(x,y,marker='o',c='b',s=5)
     plt.xlim(min(x),max(x))
     plt.ylim(min(y),max(y))
     plt.title('Kuubinen interpolointi')
     plt.savefig(filename)
def PlotMtxError(Corr_w):
    max_val = 1
    min_val = -0.1

    AvCorr = np.sum(Corr_w, axis=0)
    dCorr = Corr_w - AvCorr
    errCorr = np.log10(np.sqrt(np.einsum("i...,i...", dCorr, dCorr)) / np.absolute(AvCorr) / np.sqrt(Corr_w.shape[0]))
    # print errCorr.shape
    # print errCorr

    plt.rcParams.update({"font.size": 6, "font.weight": "bold"})
    for i in xrange(errCorr.shape[0]):
        plt.subplot(2, 7, i + 1)
        plt.title("SITE " + str(i + 1) + ":: \nHistogram of errors in corr. mtx.")
        plt.hist(errCorr[0, :, :].flatten(), 256, range=(min_val, max_val))
        plt.xlabel("log_10(sigma)")
        plt.ylabel("Count")

        plt.subplot(2, 7, i + 7 + 1)
        plt.imshow(errCorr[0, :, :], vmin=min_val, vmax=max_val)
        cbar = plt.colorbar(shrink=0.25, aspect=40)
        cbar.set_label("log_10(sigma)")
        plt.set_cmap("gist_yarg")
        plt.title("SITE " + str(i + 1) + ":: \nError in corr. matx. values")
        plt.xlabel("Site i")
        plt.ylabel("Site j")
    plt.show()
Example #25
0
def plot_hyperplane(X, Y, model, K, plot_id, d = 500):
    I0 = np.where(Y==-1)[0]
    I1 = np.where(Y==1)[0]

    plt.subplot(plot_id)

    plt.plot(X[I1, 0], X[I1, 1], 'og')
    plt.plot(X[I0, 0], X[I0, 1], 'xb')

    min_val = np.min(X, 0)
    max_val = np.max(X, 0)

    clf = model()
    clf.train(X, Y, K)

    x0_plot = np.linspace(min_val[0, 0], max_val[0, 0], d)
    x1_plot = np.linspace(min_val[0, 1], max_val[0, 1], d)

    [x0, x1] = plt.meshgrid(x0_plot, x1_plot);

    Y_all = np.matrix(np.zeros([d, d]))

    for i in range(d):
        X_all = np.matrix(np.zeros([d, 2]))
        X_all[:, 0] = np.matrix(x0[:, i]).T
        X_all[:, 1] = np.matrix(x1[:, i]).T
        Y_all[:, i] = clf.predict(X_all)

    plt.contour(np.array(x0), np.array(x1), np.array(Y_all), levels = [0.0], colors = 'red')
Example #26
0
File: mf.py Project: 4everer/ml
    def train(self, ratings, model_path):
 
        self.mu = ratings.mean
        self.P = 0.001 * np.matrix(np.random.randn(len(ratings.rows), self.num_factor))
        self.bu = 0.001 * np.matrix(np.random.randn(len(ratings.rows), 1))
        self.Q = 0.001 * np.matrix(np.random.randn(len(ratings.cols), self.num_factor))
        self.bi = 0.001 * np.matrix(np.random.randn(len(ratings.cols), 1))
        
        self.rows = dict(ratings.rows)
        self.cols = dict(ratings.cols)       

        if self.validate > 0:
            T = ratings.kv_dict.items()
            random.shuffle(T)
            k = len(T) / self.validate
            self.L_validate = T[0 : k]
            self.L_train = T[k :]
        else:
            self.L_train = ratings.kv_dict.items()

        rmse_train = [0.0] * self.max_iter
        rmse_validate = [0.0] * self.max_iter       
 
        for s in range(self.max_iter):

            random.shuffle(self.L_train)
            self.current_sample = 0
            self.sqr_err = 0.0

            self.threads = [ParallelSGD('Thread_%d' % n, self) for n in range(self.num_thread)]
            
            start = time.time()
            for t in self.threads:
                t.start()
                t.join()
            terminal = time.time()

            duration = terminal - start

            rmse_train[s] = math.sqrt(self.sqr_err / len(ratings.kv_dict))
    
            if self.validate > 0:
                m = SparseMatrix()
                m.kv_dict = {k : v for (k, v) in self.L_validate}
                rmse_validate[s] = float(self.test(m))
            
            sys.stderr.write('Iter: %4.4i' % (s + 1))
            sys.stderr.write('\t[Train RMSE] = %f' % rmse_train[s])
            if self.validate > 0:
                sys.stderr.write('\t[Validate RMSE] = %f' % rmse_validate[s])
            sys.stderr.write('\t[Duration] = %f' % duration)
            sys.stderr.write('\t[Samples] = %d\n' % len(self.L_train))

            self.dump_model(model_path + '/' + 'model_%4.4i' % (s + 1))
            self.dump_raw_model(model_path + '/' + 'model_%4.4i.raw_model' % (s + 1))

        plt.subplot(111)
        plt.plot(range(self.max_iter), rmse_train, '-og')
        plt.plot(range(self.max_iter), rmse_validate, '-xb')
        plt.show()
Example #27
0
def plot_reconstruction_result(res):
    """ Plot original and reconstructed graph plus time series
    """
    fig = plt.figure(figsize=(32, 8))
    gs = mpl.gridspec.GridSpec(1, 4)

    # original graph
    orig_ax = plt.subplot(gs[0])
    plot_graph(nx.from_numpy_matrix(res.A.orig), orig_ax)
    orig_ax.set_title('Original graph')

    # time series
    ax = plt.subplot(gs[1:3])
    sns.tsplot(
        time='time', value='theta',
        unit='source', condition='oscillator',
        estimator=np.mean, legend=False,
        data=compute_solutions(res),
        ax=ax)
    ax.set_title(r'$A_{{err}} = {:.2}, B_{{err}} = {:.2}$'.format(*compute_error(res)))

    # reconstructed graph
    rec_ax = plt.subplot(gs[3])
    tmp = res.A.rec
    tmp[abs(tmp) < 1e-1] = 0
    plot_graph(nx.from_numpy_matrix(tmp), rec_ax)
    rec_ax.set_title('Reconstructed graph')

    plt.tight_layout()
    save(fig, 'reconstruction_overview')
def fit_calib_auto(X_bh, X_bg_gh, do_l1=False):
    """
Does auto-regression on the 6-DOF variables : x,y,z,r,p,y.
"""
    assert X_bh.shape[0]==X_bg_gh.shape[0]==12, "calib data has unknown shape."
    
    #X_bh = X_bh[:,500:1000]
    #X_bg_gh = X_bg_gh[:,500:1000]
    
    axlabels = ['x','y','z','roll','pitch','yaw']
    for k in xrange(1,100, 10):
        print "order : k=", k
        plt.clf()
        W = np.empty((6, 2*k+1))
        for i in xrange(6):
            j = i+3 if i > 2 else i
            Ai, bi, W[i,:] = fit_auto(X_bh[j,:], X_bg_gh[j,:], k, do_l1)
            est = Ai.dot(W[i,:])
            print " norm err : ", np.linalg.norm(bi - est)
    
            plt.subplot(3,2,i+1)
            plt.plot(bi, label='pr2')
            plt.plot(X_bh[j,k-1:], label='hydra')
            plt.plot(est, label='estimate')
            plt.ylabel(axlabels[i])
            plt.legend()
        plt.show()
Example #29
0
 def sanity_example2(self):
   """
     Checking Kepler orbit calculation example.
   """
   import numpy
   from PyAstronomy import pyasl
   import matplotlib.pylab as plt
   
   # Instantiate a Keplerian elliptical orbit with
   # semi-major axis of 1.3 length units,
   # period of 2 time units, eccentricity of 0.5, and
   # longitude of ascending node of 70 degrees.
   ke = pyasl.KeplerEllipse(1.3, 2., e=0.5, Omega=70.)
   
   # Get a time axis
   t = numpy.linspace(0, 6.5, 200)
   
   # Calculate the orbit position at the given points
   # in a Cartesian coordinate system.
   pos = ke.xyzPos(t)
   print "Shape of output array: ", pos.shape
   
   # x, y, and z coordinates for 50th time point
   print "x, y, z for 50th point: ", pos[50, ::]
   
   # Calculate orbit radius as a function of the
   radius = ke.radius(t)
   
   # Plot x and y coordinates of the orbit
   plt.subplot(2,1,1)
   plt.plot(pos[::,0], pos[::,1], 'bp')
   # Plot orbit radius as a function of time
   plt.subplot(2,1,2)
   plt.plot(t, radius, 'bp')
Example #30
0
def plot_env(env, growth_data=None, log_pop_size=True):
    """
    Plot environment and the population growth (if given).
    """
    if growth_data is None:
        df = env.as_df(melted=True)
        g = sns.factorplot(x="index", y="value", hue="nutrient", data=df)
        return g
    # get un-melted dataframe
    df = env.as_df(melted=False)
    df["t"] = growth_data["t"].t
    plt.subplot(2, 1, 1)
    df["pop_size"] = np.exp(growth_data["log_pop_size"])
    for nutr in env.nutrs:
        plt.plot(df["t"], df[nutr], label=nutr)
    plt.legend()
    plt.xlabel("Time")
    plt.subplot(2, 1, 2)
    plt.xlabel("Time")
    if log_pop_size:
        plt.plot(df["t"], np.log2(df["pop_size"]))
        plt.ylabel("Pop. size (log$_\mathrm{2}$)")
    else:
        plt.plot(df["t"], df["pop_size"])
        plt.ylabel("Pop. size")
Example #31
0
    # Compute mean squared error with regularization with optimal lambda
    Error_train_rlr[k] = np.square(y_train - X_train @ w_rlr[:, k]).sum(
        axis=0) / y_train.shape[0]
    Error_test_rlr[k] = np.square(y_test - X_test @ w_rlr[:, k]).sum(
        axis=0) / y_test.shape[0]

    m = lm.LinearRegression().fit(X_train, y_train)
    Error_train[k] = np.square(y_train -
                               m.predict(X_train)).sum() / y_train.shape[0]
    Error_test[k] = np.square(y_test -
                              m.predict(X_test)).sum() / y_test.shape[0]
    #Gen_error[k] = Error_test[k] - Error_train[k]
    # Display the results for the last cross-validation fold
    if k == K - 1:
        figure(k, figsize=(10, 5))
        subplot(1, 2, 1)
        semilogx(lambdas, mean_w_vs_lambda.T[:, 1:],
                 '.-')  # Don't plot the bias term
        xlabel('Regularization factor')
        ylabel('Mean Coefficient Values')
        grid()

        subplot(1, 2, 2)
        title('Optimal lambda: 1e{0}'.format(np.log10(opt_lambda)))
        loglog(lambdas, train_err_vs_lambda.T, 'b.-', lambdas,
               test_err_vs_lambda.T, 'r.-')
        xlabel('Regularization factor')
        ylabel('Squared error (crossvalidation)')
        legend(['Train error', 'Validation error'])
        grid()
        plt.savefig('reg.png')
test_stationarity(ts_log_ewma_diff)

ts_log_diff = ts_log - ts_log.shift()
plt.plot(ts_log_diff)

ts_log_diff.dropna(inplace=True)
test_stationarity(ts_log_diff)

from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(ts_log)

trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

plt.subplot(411)
plt.plot(ts_log, label='Original')
plt.legend(loc='best')
plt.subplot(412)
plt.plot(trend, label='Trend')
plt.legend(loc='best')
plt.subplot(413)
plt.plot(seasonal, label='Seasonality')
plt.legend(loc='best')
plt.subplot(414)
plt.plot(residual, label='Residuals')
plt.legend(loc='best')
plt.tight_layout()

ts_log_decompose = residual
ts_log_decompose.dropna(inplace=True)
Example #33
0
		print("Estimated time left: %0.1f" % time_left)
		print("CFL: %s" %CFL)



print('done')
#def sdat(c, F):
   # print("Saving in:", F)
   # np.savez(F,u = c)
   # return 0

#sdat( u, "data/zu_N" + str(N2) + "_" + str(L) + "_" + str(C) + "_" + str(E) + ".dat")

# %%
%matplotlib inline
plt.subplot(3, 1, 1)
plt.contourf(  u[:, 0] )
plt.colorbar()
plt.subplot(3, 1, 2)
plt.contourf( u[:, 1] )
plt.colorbar()
plt.subplot(3, 1, 3 )
plt.plot( np.mean( u[:, 0], axis = 0) )
plt.plot( np.mean( u[:, 1], axis = 0) )
plt.grid()
plt.show()
#
#
# plt.contourf(np.fft.irfft2(psic_1[0,:,:]))
# plt.colorbar()
# plt.show()
weight = table.col_values(4)[1:]

#对数据中男女进行分类
for index, value in enumerate(sex):
    if value == 1:
        sex_men.append(int(index))
        height_men.append(height[index])
        weight_men.append(weight[index])
    else:
        sex_women.append(int(index))
        height_women.append(height[index])
        weight_women.append(weight[index])

#数据可视化(直方图)
plt.figure('Height Distribution')       
plb.subplot(1, 2, 1) 
plt.hist(height_men,len(set(height_men)))
plt.title('Height Histogram(men)')
plt.xlabel('height(cm)')
plt.ylabel('number')
plb.subplot(1, 2, 2)
plt.hist(height_women,len(set(height_women)))
plt.title('Height Histogram(women)')
plt.xlabel('height(cm)')
plt.ylabel('number')
plt.savefig('height histogram.jpg')
plt.figure('Weight Distribution') 
plb.subplot(1, 2, 1) 
plt.hist(weight_men,len(set(weight_men)))
plt.title('Weight Histogram(men)')
plt.xlabel('weight(kg)')
Example #35
0
def crb_compare(state0,
                samples0,
                state1,
                samples1,
                crb0=None,
                crb1=None,
                zlayer=None,
                xlayer=None):
    """
    To run, do:

    s,h = pickle...
    s1,h1 = pickle...
        i.e. /media/scratch/bamf/vacancy/vacancy_zoom-1.tif_t002.tif-featured-v2.pkl
        i.e. /media/scratch/bamf/frozen-particles/0.tif-featured-full.pkl
    crb0 = diag_crb_particles(s); crb1 = diag_crb_particles(s1)
    crb_compare(s,h[-25:],s1,h1[-25:], crb0, crb1)
    """
    s0 = state0
    s1 = state1
    h0 = np.array(samples0)
    h1 = np.array(samples1)

    slicez = zlayer or s0.image.shape[0] / 2
    slicex = xlayer or s0.image.shape[2] / 2
    slicer1 = np.s_[slicez, s0.pad:-s0.pad, s0.pad:-s0.pad]
    slicer2 = np.s_[s0.pad:-s0.pad, s0.pad:-s0.pad, slicex]
    center = (slicez, s0.image.shape[1] / 2, slicex)

    mu0 = h0.mean(axis=0)
    mu1 = h1.mean(axis=0)

    std0 = h0.std(axis=0)
    std1 = h1.std(axis=0)

    mask0 = (s0.state[s0.b_typ] == 1.) & (analyze.trim_box(
        s0, mu0[s0.b_pos].reshape(-1, 3)))
    mask1 = (s1.state[s1.b_typ] == 1.) & (analyze.trim_box(
        s1, mu1[s1.b_pos].reshape(-1, 3)))
    active0 = np.arange(s0.N)[mask0]  #s0.state[s0.b_typ]==1.]
    active1 = np.arange(s1.N)[mask1]  #s1.state[s1.b_typ]==1.]

    pos0 = mu0[s0.b_pos].reshape(-1, 3)[active0]
    pos1 = mu1[s1.b_pos].reshape(-1, 3)[active1]
    rad0 = mu0[s0.b_rad][active0]
    rad1 = mu1[s1.b_rad][active1]

    link = analyze.nearest(pos0, pos1)
    dpos = pos0 - pos1[link]
    drad = rad0 - rad1[link]

    drift = dpos.mean(axis=0)
    print 'drift', drift

    dpos -= drift

    fig = pl.figure(figsize=(24, 10))

    #=========================================================================
    #=========================================================================
    gs0 = ImageGrid(fig,
                    rect=[0.02, 0.4, 0.4, 0.60],
                    nrows_ncols=(2, 3),
                    axes_pad=0.1)

    lbl(gs0[0], 'A')
    for i, slicer in enumerate([slicer1, slicer2]):
        ax_real = gs0[3 * i + 0]
        ax_fake = gs0[3 * i + 1]
        ax_diff = gs0[3 * i + 2]

        diff0 = s0.get_model_image() - s0.image
        diff1 = s1.get_model_image() - s1.image
        a = (s0.image - s1.image)
        b = (s0.get_model_image() - s1.get_model_image())
        c = (diff0 - diff1)

        ptp = 0.7 * max([np.abs(a).max(), np.abs(b).max(), np.abs(c).max()])
        cmap = pl.cm.RdBu_r
        ax_real.imshow(a[slicer], cmap=cmap, vmin=-ptp, vmax=ptp)
        ax_real.set_xticks([])
        ax_real.set_yticks([])
        ax_fake.imshow(b[slicer], cmap=cmap, vmin=-ptp, vmax=ptp)
        ax_fake.set_xticks([])
        ax_fake.set_yticks([])
        ax_diff.imshow(c[slicer], cmap=cmap, vmin=-ptp,
                       vmax=ptp)  #cmap=pl.cm.RdBu, vmin=-1.0, vmax=1.0)
        ax_diff.set_xticks([])
        ax_diff.set_yticks([])

        if i == 0:
            ax_real.set_title(r"$\Delta$ Confocal image", fontsize=24)
            ax_fake.set_title(r"$\Delta$ Model image", fontsize=24)
            ax_diff.set_title(r"$\Delta$ Difference", fontsize=24)
            ax_real.set_ylabel('x-y')
        else:
            ax_real.set_ylabel('x-z')

    #=========================================================================
    #=========================================================================
    gs1 = GridSpec(1,
                   3,
                   left=0.05,
                   bottom=0.125,
                   right=0.42,
                   top=0.37,
                   wspace=0.15,
                   hspace=0.05)

    spos0 = std0[s0.b_pos].reshape(-1, 3)[active0]
    spos1 = std1[s1.b_pos].reshape(-1, 3)[active1]
    srad0 = std0[s0.b_rad][active0]
    srad1 = std1[s1.b_rad][active1]

    def hist(ax, vals, bins, *args, **kwargs):
        y, x = np.histogram(vals, bins=bins)
        x = (x[1:] + x[:-1]) / 2
        y /= len(vals)
        ax.plot(x, y, *args, **kwargs)

    def pp(ind, tarr, tsim, tcrb, var='x'):
        bins = 10**np.linspace(-3, 0.0, 30)
        bin2 = 10**np.linspace(-3, 0.0, 100)
        bins = np.linspace(0.0, 0.2, 30)
        bin2 = np.linspace(0.0, 0.2, 100)
        xlim = (0.0, 0.12)
        #xlim = (1e-3, 1e0)
        ylim = (1e-2, 30)

        ticks = ticker.FuncFormatter(
            lambda x, pos: '{:0.0f}'.format(np.log10(x)))
        scaler = lambda x: x  #np.log10(x)

        ax_crb = pl.subplot(gs1[0, ind])
        ax_crb.hist(scaler(np.abs(tarr)),
                    bins=bins,
                    normed=True,
                    alpha=0.7,
                    histtype='stepfilled',
                    lw=1)
        ax_crb.hist(scaler(np.abs(tcrb)).ravel(),
                    bins=bin2,
                    normed=True,
                    alpha=1.0,
                    histtype='step',
                    ls='solid',
                    lw=1.5,
                    color='k')
        ax_crb.hist(scaler(np.abs(tsim).ravel()),
                    bins=bin2,
                    normed=True,
                    alpha=1.0,
                    histtype='step',
                    lw=3)
        ax_crb.set_xlabel(r"$\Delta = |%s(t_1) - %s(t_0)|$" % (var, var),
                          fontsize=24)
        #ax_crb.semilogx()
        ax_crb.set_xlim(xlim)
        #ax_crb.semilogy()
        #ax_crb.set_ylim(ylim)
        #ax_crb.xaxis.set_major_formatter(ticks)
        ax_crb.grid(b=False, which='both', axis='both')

        if ind == 0:
            lbl(ax_crb, 'B')
            ax_crb.set_ylabel(r"$P(\Delta)$")
        else:
            ax_crb.set_yticks([])

        ax_crb.locator_params(axis='x', nbins=3)

    f, g = 1.5, 1.95
    sim = f * sim_crb_diff(spos0[:, 1], spos1[:, 1][link])
    crb = g * sim_crb_diff(crb0[0][:, 1][active0], crb1[0][:,
                                                           1][active1][link])
    pp(0, dpos[:, 1], sim, crb, 'x')

    sim = f * sim_crb_diff(spos0[:, 0], spos1[:, 0][link])
    crb = g * sim_crb_diff(crb0[0][:, 0][active0], crb1[0][:,
                                                           0][active1][link])
    pp(1, dpos[:, 0], sim, crb, 'z')

    sim = f * sim_crb_diff(srad0, srad1[link])
    crb = g * sim_crb_diff(crb0[1][active0], crb1[1][active1][link])
    pp(2, drad, sim, crb, 'a')

    #ax_crb_r.locator_params(axis='both', nbins=3)
    #gs1.tight_layout(fig)

    #=========================================================================
    #=========================================================================
    gs2 = GridSpec(2,
                   2,
                   left=0.48,
                   bottom=0.12,
                   right=0.99,
                   top=0.95,
                   wspace=0.35,
                   hspace=0.35)

    ax_hist = pl.subplot(gs2[0, 0])
    ax_hist.hist(std0[s0.b_pos],
                 bins=np.logspace(-3.0, 0, 50),
                 alpha=0.7,
                 label='POS',
                 histtype='stepfilled')
    ax_hist.hist(std0[s0.b_rad],
                 bins=np.logspace(-3.0, 0, 50),
                 alpha=0.7,
                 label='RAD',
                 histtype='stepfilled')
    ax_hist.set_xlim((10**-3.0, 1))
    ax_hist.semilogx()
    ax_hist.set_xlabel(r"$\bar{\sigma}$")
    ax_hist.set_ylabel(r"$P(\bar{\sigma})$")
    ax_hist.legend(loc='upper right')
    lbl(ax_hist, 'C')

    imdiff = ((s0.get_model_image() - s0.image) /
              s0._sigma_field)[s0.image_mask == 1.].ravel()
    mu = imdiff.mean()
    #sig = imdiff.std()
    #print mu, sig
    x = np.linspace(-5, 5, 10000)

    ax_diff = pl.subplot(gs2[0, 1])
    ax_diff.plot(x,
                 1.0 / np.sqrt(2 * np.pi) * np.exp(-(x - mu)**2 / 2),
                 '-',
                 alpha=0.7,
                 color='k',
                 lw=2)
    ax_diff.hist(imdiff, bins=1000, histtype='step', alpha=0.7, normed=True)
    ax_diff.semilogy()
    ax_diff.set_ylabel(r"$P(\delta)$")
    ax_diff.set_xlabel(r"$\delta = (M_i - d_i)/\sigma_i$")
    ax_diff.locator_params(axis='x', nbins=5)
    ax_diff.grid(b=False, which='minor', axis='y')
    ax_diff.set_xlim(-5, 5)
    ax_diff.set_ylim(1e-4, 1e0)
    lbl(ax_diff, 'D')

    pos = mu0[s0.b_pos].reshape(-1, 3)
    rad = mu0[s0.b_rad]
    mask = analyze.trim_box(s0, pos)
    pos = pos[mask]
    rad = rad[mask]

    gx, gy = analyze.gofr(pos,
                          rad,
                          mu0[s0.b_zscale][0],
                          resolution=5e-2,
                          mask_start=0.5)
    mask = gx < 5
    gx = gx[mask]
    gy = gy[mask]
    ax_gofr = pl.subplot(gs2[1, 0])
    ax_gofr.plot(gx, gy, '-', lw=1)
    ax_gofr.set_xlabel(r"$r/a$")
    ax_gofr.set_ylabel(r"$g(r/a)$")
    ax_gofr.locator_params(axis='both', nbins=5)
    #ax_gofr.semilogy()
    lbl(ax_gofr, 'E')

    gx, gy = analyze.gofr(pos, rad, mu0[s0.b_zscale][0], method='surface')
    mask = gx < 5
    gx = gx[mask]
    gy = gy[mask]
    gy[gy <= 0.] = gy[gy > 0].min()
    ax_gofrs = pl.subplot(gs2[1, 1])
    ax_gofrs.plot(gx, gy, '-', lw=1)
    ax_gofrs.set_xlabel(r"$r/a$")
    ax_gofrs.set_ylabel(r"$g_{\rm{surface}}(r/a)$")
    ax_gofrs.locator_params(axis='both', nbins=5)
    ax_gofrs.grid(b=False, which='minor', axis='y')
    #ax_gofrs.semilogy()
    lbl(ax_gofrs, 'F')

    ylim = ax_gofrs.get_ylim()
    ax_gofrs.set_ylim(gy.min(), ylim[1])
Example #36
0
def pretty_summary(state, samples, zlayer=None, xlayer=None, vertical=False):
    s = state
    h = np.array(samples)

    slicez = zlayer or s.image.shape[0] / 2
    slicex = xlayer or s.image.shape[2] / 2
    slicer1 = np.s_[slicez, s.pad:-s.pad, s.pad:-s.pad]
    slicer2 = np.s_[s.pad:-s.pad, s.pad:-s.pad, slicex]
    center = (slicez, s.image.shape[1] / 2, slicex)

    if vertical:
        fig = pl.figure(figsize=(12, 24))
    else:
        fig = pl.figure(figsize=(24, 8))

    #=========================================================================
    #=========================================================================
    if vertical:
        gs1 = ImageGrid(fig,
                        rect=[0.02, 0.55, 0.99, 0.40],
                        nrows_ncols=(2, 3),
                        axes_pad=0.1)
    else:
        gs1 = ImageGrid(fig,
                        rect=[0.02, 0.0, 0.4, 1.00],
                        nrows_ncols=(2, 3),
                        axes_pad=0.1)

    for i, slicer in enumerate([slicer1, slicer2]):
        ax_real = gs1[3 * i + 0]
        ax_fake = gs1[3 * i + 1]
        ax_diff = gs1[3 * i + 2]

        diff = s.get_model_image() - s.image
        ax_real.imshow(s.image[slicer], cmap=pl.cm.bone_r)
        ax_real.set_xticks([])
        ax_real.set_yticks([])
        ax_fake.imshow(s.get_model_image()[slicer], cmap=pl.cm.bone_r)
        ax_fake.set_xticks([])
        ax_fake.set_yticks([])
        ax_diff.imshow(diff[slicer], cmap=pl.cm.RdBu, vmin=-1.0, vmax=1.0)
        ax_diff.set_xticks([])
        ax_diff.set_yticks([])

        if i == 0:
            ax_real.set_title("Confocal image", fontsize=24)
            ax_fake.set_title("Model image", fontsize=24)
            ax_diff.set_title("Difference", fontsize=24)
            ax_real.set_ylabel('x-y')
        else:
            ax_real.set_ylabel('x-z')

    #=========================================================================
    #=========================================================================
    mu = h.mean(axis=0)
    std = h.std(axis=0)

    if vertical:
        gs2 = GridSpec(2,
                       2,
                       left=0.10,
                       bottom=0.10,
                       right=0.99,
                       top=0.52,
                       wspace=0.45,
                       hspace=0.45)
    else:
        gs2 = GridSpec(2,
                       2,
                       left=0.50,
                       bottom=0.12,
                       right=0.95,
                       top=0.95,
                       wspace=0.35,
                       hspace=0.35)

    ax_hist = pl.subplot(gs2[0, 0])
    ax_hist.hist(std[s.b_pos],
                 bins=np.logspace(-2.5, 0, 50),
                 alpha=0.7,
                 label='POS',
                 histtype='stepfilled')
    ax_hist.hist(std[s.b_rad],
                 bins=np.logspace(-2.5, 0, 50),
                 alpha=0.7,
                 label='RAD',
                 histtype='stepfilled')
    ax_hist.set_xlim((10**-2.4, 1))
    ax_hist.semilogx()
    ax_hist.set_xlabel(r"$\bar{\sigma}$")
    ax_hist.set_ylabel(r"$P(\bar{\sigma})$")
    ax_hist.legend(loc='upper right')

    ax_diff = pl.subplot(gs2[0, 1])
    ax_diff.hist((s.get_model_image() - s.image)[s.image_mask == 1.].ravel(),
                 bins=1000,
                 histtype='stepfilled',
                 alpha=0.7)
    ax_diff.semilogy()
    ax_diff.set_ylabel(r"$P(\delta)$")
    ax_diff.set_xlabel(r"$\delta = M_i - d_i$")
    ax_diff.locator_params(axis='x', nbins=5)

    pos = mu[s.b_pos].reshape(-1, 3)
    rad = mu[s.b_rad]
    mask = analyze.trim_box(s, pos)
    pos = pos[mask]
    rad = rad[mask]

    gx, gy = analyze.gofr(pos,
                          rad,
                          mu[s.b_zscale][0],
                          resolution=5e-2,
                          mask_start=0.5)
    mask = gx < 5
    gx = gx[mask]
    gy = gy[mask]
    ax_gofr = pl.subplot(gs2[1, 0])
    ax_gofr.plot(gx, gy, '-', lw=1)
    ax_gofr.set_xlabel(r"$r/d$")
    ax_gofr.set_ylabel(r"$g(r/d)$")
    ax_gofr.locator_params(axis='both', nbins=5)

    gx, gy = analyze.gofr(pos, rad, mu[s.b_zscale][0], method='surface')
    mask = gx < 5
    gx = gx[mask]
    gy = gy[mask]
    gy[gy <= 0.] = gy[gy > 0].min()
    ax_gofrs = pl.subplot(gs2[1, 1])
    ax_gofrs.plot(gx, gy, '-', lw=1)
    ax_gofrs.set_xlabel(r"$r/d$")
    ax_gofrs.set_ylabel(r"$g_{\rm{surface}}(r/d)$")
    ax_gofrs.locator_params(axis='both', nbins=5)
    ax_gofrs.grid(b=False, which='minor', axis='y')
    #ax_gofrs.semilogy()

    ylim = ax_gofrs.get_ylim()
    ax_gofrs.set_ylim(gy.min(), ylim[1])
def prepare_model():
    tr_date, ts_date, x_train, x_test, y_train, y_test = prepare_data()
    ts_date = timestamp_to_dataconv(ts_date)
    print(x_train.shape, y_train.shape, y_test.shape, y_test.shape)
    print(x_train)
    '''
    print(len(X_train),len(Y_train),len(X_test),len(Y_test))
    '''
    try:
        final_model= Sequential()
        final_model.add(Dense(512, input_shape=(len(x_train[0]),), kernel_initializer='normal', kernel_constraint=maxnorm(3)))
        #final_model.add(Dense(11))
        #final_model.add(Dropout(0.5))
        final_model.add(Dense(1))
        '''
        main_input = Input(shape=(len(x_train[0]),), name='main_input')
        l = Dense(512, activation='sigmoid')(main_input)
        #l = Dense(64, activation='sigmoid')(l)
        output = Dense(1, activation="linear", name="output")(l)
        final_model = Model(inputs=main_input, outputs=output)
        '''
        # summary of model
        print("summaryof model: ", final_model.summary())
        # plot graph
        # plot_model(final_model, to_file="DeepNeuralnetwork.png")

        opt = Adam(lr=0.001)
        final_model.compile(optimizer=opt, loss=losses.logcosh)
        # fit the model
        final_model.fit(x_train, y_train, epochs=5, batch_size=256, verbose=0
                        )  # validation_data=(X_test, Y_test)
        final_model.evaluate(x_test, y_test, batch_size=256, verbose=0)
        print("fit end")
        #print("weights:  ", final_model.get_weights())
        #print("param:  ",final_model.count_params())
        #print(final_model.__reduce__())
        #print(final_model.legacy_get_config())
        pred = final_model.predict(x_test)

    except:
        print("something is wrong in model")

    predicted = pred
    predicted = predicted.ravel()
    original = y_test
    # actual converted values after model
    minimum, maximum = min_max(closep1)
    pred1 = calculate_actual_values(predicted, maximum, minimum)
    actual1 = calculate_actual_values(original, maximum, minimum)
    #print("prediii: ", len(pred1), "actual: ", len(actual1))
    # actual values
    mseA = mean_squared_error(actual1, pred1)
    rmsefA = root_mean_square_error_fun(mseA)
    maefA = mean_absolute_error_fun(actual1, pred1)
    mape1A = mean_absolute_percentage_error_fun(actual1, pred1)
    r_scoreA = r2_score(actual1, pred1)

    print("mse:  ", mseA)
    print("rmse: ", rmsefA)
    print("mae: ", maefA)
    print("mape: ", mape1A)
    print("r2score: ", r_scoreA)

    errors_label = ('rmse', 'mae', 'mape')
    y_pos = np.arange(len(errors_label))
    error_values = np.array([rmsefA, maefA, mape1A])
    width = 0.75

    tests = ['Layer: 1', 'neurons: {512}', 'activation: {sigmoid,linear}',
             'lr: 0.001']

    plt.figure(1)
    # plt.subplot(211)
    plt.subplot(221)
    plt.xticks(rotation=30)
    plt.plot(ts_date, actual1, label="Close ", color='green')
    plt.plot(ts_date, pred1, label='Predicted', color='red')
    plt.grid(True)
    plt.title("Bitcoin Price (FFNN)", fontweight='bold')
    plt.legend(loc=2)
    plt.xlabel("Date")
    plt.ylabel("Price of Bitcoin")
    # plt.subplots_adjust(hspace=0.4, wspace=0.4)
    plt.subplot(222)
    # plt.subplot(223)
    plt.bar(y_pos, error_values, width, align='center', alpha=0.5, color='red',
            )

    plt.xticks(y_pos, errors_label)
    for a, b in zip(y_pos, error_values):
        plt.text(a, b, str(b))
        # plt.annotate(str(b),y_poserror_values=(a,b))

    plt.title('Evaluation Criteria', fontweight='bold')
    plt.xlabel('Errors')
    plt.ylabel('Values')
    plt.subplot(212)
    # plt.subplot(222)
    # plt.subplot(212)
    plt.title("Architecture of Model", fontsize=14)
    # plt.yticks(np.arange(len(tests)) * -1)
    # for i, s in enumerate(tests):
    #   plt.text(0.1,-i/2, s,fontsize=12,horizontalalignment='left',
    #           backgroundcolor='palegreen',wrap=True)
    plt.text(0.025, 0.8, "Layers :- ", fontweight='bold')
    plt.text(0.2, 0.8, "Two layers", fontsize=10)

    plt.text(0.025, 0.6, "Activation Fun :- ", fontweight='bold')
    plt.text(0.2, 0.6, "{ sigmoid, linear }")

    plt.text(0.025, 0.4, "Data_Set :- ", fontweight='bold')
    plt.text(0.2, 0.4, "Training: 70% and Testing: 30%, inputs: 6 ")

    plt.text(0.025, 0.2, "Features :- ", fontweight='bold')
    plt.text(0.2, 0.2, "batchsize: 256,  epochs: 5,  lr: 0.01, Optimizer: Adam, Indicator: MA")

    plt.subplots_adjust(hspace=0.4, wspace=0.4, left=0.125, bottom=0.1, right=None, top=None)
    '''
    # Save Figure
    plt.savefig("foo.png")
    # Save Transparent Figure
    plt.savefig("foo.png", transparent=True)
    '''
    plt.show()
Example #38
0
    Zseq = Zseq[:maxT]

    # Plot X, colored by segments Z
    changePts = np.flatnonzero(np.abs(np.diff(Zseq)))
    changePts = np.hstack([0, changePts + 1])
    for ii, loc in enumerate(changePts[:-1]):
        nextloc = changePts[ii + 1]
        ts = np.arange(loc, nextloc)
        xseg = Xseq[loc:nextloc]
        kseg = int(Zseq[loc])

        color = GaussViz.Colors[kseg % len(GaussViz.Colors)]
        pylab.plot(ts, xseg, '.-', color=color, markersize=8)
        pylab.plot(
            [nextloc - 1, nextloc], [Xseq[nextloc - 1], Xseq[nextloc]], 'k:')
    pylab.ylim([-2, 14])

if __name__ == "__main__":
    from matplotlib import pylab
    pylab.figure()
    plot_true_clusters()
    Data = get_data(nObsTotal=5000)

    W = 10
    H = 15
    pylab.subplots(nrows=Data.nDoc, ncols=1, figsize=(W, H))
    for n in xrange(Data.nDoc):
        pylab.subplot(Data.nDoc, 1, n + 1)
        plot_sequence(n, Data)
    pylab.show(block=True)
Example #39
0
i1 = np.loadtxt("impli1.dat")
i2 = np.loadtxt("impli2.dat")
i3 = np.loadtxt("impli3.dat")
e1 = np.loadtxt("euler1.dat")
e2 = np.loadtxt("euler2.dat")
e3 = np.loadtxt("euler3.dat")
x1 = e1[:, 0]
y1 = e1[:, 1]
x2 = e2[:, 0]
y2 = e2[:, 1]
x3 = e3[:, 0]
y3 = e3[:, 1]

x4 = i1[:, 0]
y4 = i1[:, 1]
x5 = i2[:, 0]
y5 = i2[:, 1]
x6 = i3[:, 0]
y6 = i3[:, 1]

plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.plot(x3, y3)
plt.subplot(1, 2, 2)
plt.plot(x4, y4)
plt.plot(x5, y5)
plt.plot(x6, y6)
plt.savefig("primergrado.png")
def main(argv):
    # load preprocessed data samples
    print 'loading data...\t',
    #data_train, data_test = genData() #load('../data/vanhateren.npz')
    # data_train_0,data_train_1,data_train_2, data_test = load_data_BS("data/changedetection\intermitentmotion\streetlight/orignal_color.pkl")
    img = Image.open("data/changedetection\ptz\continuous/gt000962.png")
    print "Doie : ", (asarray(img)[:, :]).shape
    groundtruth = (((asarray(img)[:, :]) / 255.0 > 0.5) * 1).flatten()

    # #groundtruth = (((asarray(img)[:,:])/255.0 > 0.5) * 1).flatten()
    # print '[DONE]'
    batches = 3

    # remove DC component (first component)
    # data_train = data['train'][1:, :]
    # data_test = data['test'][1:, :]

    # create 1st layer
    dbn = DBN(GaussianRBM(num_visibles=x_ * y_, num_hiddens=15))
    dbn1 = DBN(GaussianRBM(num_visibles=x_ * y_, num_hiddens=15))
    dbn2 = DBN(GaussianRBM(num_visibles=x_ * y_, num_hiddens=15))

    dbn[0].learning_rate = 0.0001
    dbn1[0].learning_rate = 0.0001
    dbn2[0].learning_rate = 0.0001

    # train 1st layer
    print 'training...\t',
    for k in range(2):
        print "epoc :", k
        for i in range(batches):
            global batch_no
            batch_no = i
            os.path.walk(
                'C:\work\\backgdSubt\dataset\changedetection\ptz\continuous\\orignal',
                load_batch, 0)
            global train_set_x0
            global train_set_x1
            global train_set_x2

            dbn.train(train_set_x0.T,
                      num_epochs=1,
                      batch_size=1,
                      shuffle=False)
            dbn1.train(train_set_x1.T,
                       num_epochs=1,
                       batch_size=1,
                       shuffle=False)
            dbn2.train(train_set_x2.T,
                       num_epochs=1,
                       batch_size=1,
                       shuffle=False)

    print '[DONE]'

    os.path.walk(
        'C:\work\\backgdSubt\dataset\changedetection\ptz\continuous\\test',
        load_test_batch, 0)

    data_test_0 = ((data_test.T)[:, ::3]).T
    data_test_1 = ((data_test.T)[:, 1::3]).T
    data_test_2 = ((data_test.T)[:, 2::3]).T

    Ndat = 25  #data_test_0.shape[1]
    Nsteps = 5
    # evaluate 1st layer
    print 'evaluating 1...\t',
    dataout = zeros(x_ * y_)
    # #datasub = zeros(x_*y_)
    for point in xrange(Ndat):
        #X = asmatrix(data_test_0[:,point]).T
        X = asmatrix(data_test_0[:, -1]).T
        #dataout = vstack((dataout,X.flatten()))
        #print "testing:", X.shape
        for recstep in xrange(Nsteps):
            Y = dbn[0].forward(X)  # self.activ(1)
            X = dbn[0].backward(Y, X)
        #print "S hsape:", X.shape
        #dataout = vstack((dataout,X.flatten()))
        dataout = vstack((dataout,
                          subtract(asarray(X), data_test_0[:, -1],
                                   asarray(dbn[0].vsigma), point + 1)))

    print 'evaluating 2...\t',
    dataout1 = zeros(x_ * y_)
    # #datasub = zeros(x_*y_)
    for point in xrange(Ndat):
        #X = asmatrix(data_test_1[:,point]).T
        X = asmatrix(data_test_1[:, -1]).T
        #dataout1 = vstack((dataout1,X.flatten()))
        #print "testing:", X.shape
        for recstep in xrange(Nsteps):
            Y = dbn1[0].forward(X)  # self.activ(1)
            X = dbn1[0].backward(Y, X)
        #print "S hsape:", X.shape
        #dataout1 = vstack((dataout1,X.flatten()))
        dataout1 = vstack((dataout1,
                           subtract(asarray(X), data_test_1[:, -1],
                                    asarray(dbn1[0].vsigma), point + 1)))

    print 'evaluating 3...\t',
    dataout2 = zeros(x_ * y_)
    # #datasub = zeros(x_*y_)
    for point in xrange(Ndat):
        #X = asmatrix(data_test_2[:,point]).T
        X = asmatrix(data_test_2[:, -1]).T
        #dataout2 = vstack((dataout2,X.flatten()))
        #print "testing:", X.shape
        for recstep in xrange(Nsteps):
            Y = dbn2[0].forward(X)  # self.activ(1)
            X = dbn2[0].backward(Y, X)
        #print "S hsape:", X.shape
        #dataout2 = vstack((dataout2,X.flatten()))
        dataout2 = vstack((dataout2,
                           subtract(asarray(X), data_test_2[:, -1],
                                    asarray(dbn2[0].vsigma), point + 1)))

    # plt.imshow((reshape(data_test[::3,5],(x_,y_))), cmap = cm.Greys_r, interpolation ="nearest")
    # plt.axis('off')
    # plt.show()

    plt.figure(1)
    for i in range(Ndat):
        plt.subplot(5, 5, i + 1)
        d = multiply(asarray(dataout[i + 1, :]), asarray(dataout1[i + 1, :]),
                     asarray(dataout2[i + 1, :]))
        d = mod(d + 1, 2)
        #print "Image Example Fmeaure: ",i," : ", f_measure(d,groundtruth) * 100
        # d[0::3] = asarray(dataout[i+1,:])
        # d[1::3] = asarray(dataout1[i+1,:])
        # d[2::3] = asarray(dataout2[i+1,:])
        # d[:,:,0] = (reshape(asarray(dataout[i+1,:]),(x_,y_)))
        # d[:,:,1] = (reshape(asarray(dataout1[i+1,:]),(x_,y_)))
        # d[:,:,2] = (reshape(asarray(dataout2[i+1,:]),(x_,y_)))
        plt.imshow(reshape(d, (x_, y_)),
                   cmap=cm.Greys_r,
                   interpolation="nearest")
        plt.axis('off')
    plt.figure(2)

    for k in range(15):
        plt.subplot(5, 3, k + 1)
        d = zeros((x_ * y_ * 3))
        d[0::3] = asarray(dbn[0].W[:, k].flatten())
        d[1::3] = asarray(dbn1[0].W[:, k].flatten())
        d[2::3] = asarray(dbn2[0].W[:, k].flatten())
        plt.imshow(reshape(d[0::3], (x_, y_)),
                   cmap=cm.Greys_r,
                   interpolation="nearest")
        plt.axis('off')
    # plt.figure()
    # plt.imshow((reshape(dbn[0].vsigma[:19200],(x_,y_))))

    # plt.figure(2)
    # plt.imshow((reshape(dbn[0].vsigma[19200:19200*2],(x_,y_))))

    # plt.figure(3)
    # plt.imshow((reshape(dbn[0].vsigma[19200*2:19200*3],(x_,y_))))

    plt.figure(3)
    print type(dbn[0].vsigma)
    plt.imshow(reshape(asarray(dbn[0].vsigma), (x_, y_)))
    plt.show()

    print dbn[0].vsigma
    p.show()
Example #41
0
        I = stokeswf(wf, "I")
        Q = stokeswf(wf, "Q")
        U = stokeswf(wf, "U")
        V = stokeswf(wf, "V")
    elif 'foldspec' in sys.argv[1]:
        I = stokes_liam(f, ic, "I")
        Q = stokes_liam(f, ic, "Q")
        U = stokes_liam(f, ic, "U")
        V = stokes_liam(f, ic, "V")

    Ucomp = U + 1j * V  # complex U, will look at this below

    # plots of Stokes Parameters vs Frequency
    if '--SvF' in sys.argv:
        plt.suptitle('Stokes Parameters for B0531+21 %s' % title)
        plt.subplot(221)
        plt.plot(I, 'm')
        plt.title('I')
        plt.ylabel('Intensity')
        plt.xlim(0, 1024)
        plt.xticks(np.linspace(0, 1024, 5), np.linspace(400, 800, 5))
        plt.subplot(222)
        plt.plot(Q, 'm')
        plt.title('Q')
        plt.xlim(0, 1024)
        plt.xticks(np.linspace(0, 1024, 5), np.linspace(400, 800, 5))
        plt.subplot(223)
        plt.plot(U, 'm')
        plt.title('U')
        plt.ylabel('Intensity')
        plt.xlabel('Frequency (MHz)')
W20=DATOS[20000:40000][:,3]
W2f=DATOS[20000:40000][:,4]

W30=DATOS[40000:60000][:,3]
W3f=DATOS[40000:60000][:,4]

W40=DATOS[60000:80000][:,3]
W4f=DATOS[60000:80000][:,4]

W50=DATOS[80000:100000][:,3]
W5f=DATOS[80000:100000][:,4]
#grafica. la leyenda molesta con 20 colores, se agrupan con un criterio arbitrario en edificios pequeños medianos y altos.
plt.figure(figsize=(20,20))
for n in range(20):
    plt.subplot(5,2,1)
    plt.title("Amplitud en primer piso para w=0.5")
    plt.xlabel("t")
    plt.ylabel("Amplitud")
    if(n<10):
        plt.plot(t,W10[1000*n:1000*(n+1)],"r")
    elif(n<17):
        plt.plot(t,W10[1000*n:1000*(n+1)],"b")
    else:
        plt.plot(t,W10[1000*n:1000*(n+1)],"g")
    
    plt.subplot(5,2,2)
    plt.title("Amplitud en ultimo piso para w=0.5")
    plt.xlabel("t")
    plt.ylabel("Amplitud")
    if(n<10):
Example #43
0
        gmm = GaussianMixture(n_components=K,
                              covariance_type=covar_type,
                              n_init=reps).fit(X_train)

        # compute negative log likelihood of X_test
        CVE[t] += -gmm.score_samples(X_test).sum()

# Plot results

figure(1)
plot(KRange, BIC, '-*b')
plot(KRange, AIC, '-xr')
plot(KRange, 2 * CVE, '-ok')
legend(['BIC', 'AIC', 'Crossvalidation'])
xlabel('K')
show()

x = np.linspace(-10, 10, 50)
# Compute kernel density estimate
kde = gaussian_kde(X.ravel())
xe = np.linspace(-10, 10, 100)
# Plot kernel density estimate
figure(figsize=(6, 7))
subplot(2, 1, 1)
hist(X, x)
title('Data histogram')
subplot(2, 1, 2)
plot(xe, kde.evaluate(xe))
title('Kernel density estimate')
show()
    
    mlp = MLPClassifier(hidden_layer_sizes=(50), activation='logistic',
                        max_iter=100, alpha=0.1,
                        solver='lbfgs', verbose=False, tol=1e-4, random_state=1,
                        learning_rate_init=.1)
    mlp.fit(X_train, Y_train)
    print("Training Accurancy : {:<10}".format(mlp.score(X_train, Y_train)))
    print('x-validation Accurancy: {:<10}'.format(mlp.score(X_xv, Y_xv)))
    NN_scores.append(mlp.score(X_xv, Y_xv))
    print('Time spent for NN: {:6.3f}s'.format(time.time() - start))
    print('The logloss is: {}'.format(logloss(xv_sub['result'], 
          mlp.predict_proba(xv_sub[features])[:, 1])))
    
import matplotlib.pylab as plt
plt.figure()
plt.subplot(2,1,1)
plt.title('Scores of Random Forest')
if RF:
  plt.plot(RF_para, RF_scores, 'o-')
plt.subplot(2,1,2)
plt.title('Scores of Neural Network')
if NN:
  plt.semilogx(NN_para, NN_scores, 'o-')
plt.show()

# =============================================================================
# =============================== prediction ==================================
# =============================================================================
# now let's predict the 2013 ~ 2016 Tourney
if predict:
  if prediction_csv_file:
Example #45
0
data = ts_log
# create class
model = SimpleExpSmoothing(data)
'''

#ACF and PACF plots:

from statsmodels.tsa.stattools import acf, pacf

ts_log_diff = ts_log - ts_log.shift()
ts_log_diff.dropna(inplace=True)

lag_acf = acf(ts_log_diff, nlags=20)
lag_pacf = pacf(ts_log_diff, nlags=20, method='ols')
#Plot ACF:
plt.subplot(121)
plt.plot(lag_acf)
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()
plt.show()
Example #46
0
         'X',
         size=20,
         color='r')
ax1.set_xlabel('Users')
ax1.set_ylabel('Tweet Frequency')
plt.title("Tweet Frequency Scatterplot", fontsize=17)
plt.legend()
plt.savefig("tweet_frequency" + ".png", bbox_inches='tight')

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

realTemp = list(realUsers.values())
ax = plt.subplot(111)
x = np.sort(realTemp)
n = x.size
y = np.arange(1, n + 1) / n

ax.scatter(x=x, y=y)

fakeTemp = list(fakeUsers.values())
x = np.sort(fakeTemp)
n = x.size
y = np.arange(1, n + 1) / n

ax.scatter(x=x, y=y)

ax.legend(('Real', 'Fake'))
ax.set_xlabel('Tweet Frequency')
Example #47
0
    def show_frequency_distribution(self):
        plt.figure(3, figsize=(20, 35))
        plt.subplot(6, 2, 1)
        plt.hist([self.winddata.simulations['df']['WS-92'], self.winddata.measurements['df']['WS-92']],
                 bins=np.arange(30), label=['NEWA', 'Measurements'])
        plt.legend()
        plt.title('Wind Speed Frequency Distribution 2012 - 2015')
        plt.xlabel('Wind Speed [m/s]')
        plt.ylabel('Frequency')

        plt.subplot(6, 2, 2)
        plt.hist([self.winddata.simulations['removed']['WS-92']], bins=np.arange(30))
        plt.title('Wind Speed Frequency Distribution of Removed NEWA Data due to Measurement Gaps')
        plt.xlabel('Wind Speed [m/s]')
        plt.ylabel('Frequency')

        plt.subplot(6, 2, 3)
        plt.hist([self.winddata.simulations['df']['WS-92'][self.winddata.simulations['df'].index.year == yr] for yr in
                  self.years], bins=np.arange(30), label=self.years)
        plt.legend()
        plt.title('Yearly Wind Speed Frequency Distribution NEWA')
        plt.xlabel('Wind Speed [m/s]')
        plt.ylabel('Frequency')

        plt.subplot(6, 2, 4)
        plt.hist([self.winddata.measurements['df']['WS-92'][self.winddata.measurements['df'].index.year == yr] for yr in
                  self.years], bins=np.arange(30), label=self.years)
        plt.legend()
        plt.title('Yearly Wind Speed Frequency Distribution Measurements')
        plt.xlabel('Wind Speed [m/s]')
        plt.ylabel('Frequency')

        for i, yr in enumerate(self.years):
            con1_sim = (self.winddata.simulations['df'].index.year == yr)
            con1_meas = (self.winddata.measurements['df'].index.year == yr)

            plt.subplot(6, 2, 5 + 2 * i)
            plt.hist([self.winddata.simulations['df']['WS-92'][con1_sim & (self.winddata.simulations['df']['season'] ==
                                                                           sn)] for sn in self.seasons],
                     bins=np.arange(30), label=self.seasons)
            plt.legend()
            plt.title(f'Seasonal Wind Speed Frequency Distribution NEWA {yr}')
            plt.xlabel('Wind Speed [m/s]')
            plt.ylabel('Frequency')
            plt.ylim(0, 650)

            plt.subplot(6, 2, 5 + 2 * i + 1)
            plt.hist(
                [self.winddata.measurements['df']['WS-92'][con1_meas & (self.winddata.measurements['df']['season'] ==
                                                                        sn)] for sn in self.seasons],
                bins=np.arange(30), label=self.seasons)
            plt.legend()
            plt.title(f'Seasonal Wind Speed Frequency Distribution Measurements {yr}')
            plt.xlabel('Wind Speed [m/s]')
            plt.ylabel('Frequency')
            plt.ylim(0, 650)
        plt.show()
Example #48
0
def main(args):
    print("*" * 50)
    print("-" * 15, "Iter:{}  Version:{}".format(args.iter, args.version),
          "-" * 15)
    print("*" * 50)

    index = np.random.randint(low=0, high=223)

    geo_full = build_geo(args.full_view)
    geo_sparse = build_geo(args.sparse_view)
    pre_trans_img = [Transpose(), TensorFlip(0), TensorFlip(1)]
    datasets_v = {
        "train":
        BuildDataSet(args.data_root_path, args.train_folder, geo_full,
                     geo_sparse, pre_trans_img, "train"),
        "val":
        BuildDataSet(args.data_root_path, args.val_folder, geo_full,
                     geo_sparse, None, "val"),
        "test":
        BuildDataSet(args.data_root_path, args.test_folder, geo_full,
                     geo_sparse, None, "test")
    }

    sample = datasets_v["test"][index]
    image_true = sample["image_true"]
    image_full = sample["image_full"][0].numpy()

    image_sparse_0 = sample["image_sparse_0"]
    image_sparse_1 = sample["image_sparse_1"]
    image_sparse_2 = sample["image_sparse_2"]
    image_sparse = sample["image_sparse"][0].numpy()
    """
    ***********************************************************************************************************
    Show Loss
    ***********************************************************************************************************
    """
    TrainBestEpoch, ValBestEpoch = read_loss(loss_name="min_loss_epoch",
                                             loss_path=args.loss_path)
    TrainBestLoss, ValBestLoss = read_loss(loss_name="min_loss",
                                           loss_path=args.loss_path)
    print("TrainBestEpoch:{}, ValBestEpoch:{}".format(TrainBestEpoch,
                                                      ValBestEpoch))
    print("TrainBestLoss:{:.6f}, ValBestLoss:{:.6f}".format(
        TrainBestLoss, ValBestLoss))
    show_loss(loss_name="losses", loss_path=args.loss_path)
    """
    ***********************************************************************************************************
    Test model
    ***********************************************************************************************************
    """
    modelparser = ModelInit()
    model = ResUnet(modelparser)
    model = model_updata(model,
                         model_old_name=args.model_name +
                         "{}_{}_Best".format(ValBestEpoch, "val"),
                         model_old_path=args.model_path)
    print("Load Modle...")
    # print(args.root_path + "/results/Iter_{}/{}/model/IterDa_E{}_val_Best.pth".format(args.iter, args.version,ValBestEpoch))
    # model =  torch.load(args.root_path + "/results/Iter_{}/{}/model/IterDa_E{}_val_Best.pth".format(args.iter, args.version,ValBestEpoch),
    #                     map_location=torch.device('cpu'))
    image_pred = pred_sample(image_sparse, model)
    """
    ***********************************************************************************************************
    Show images
    ***********************************************************************************************************
    """
    plt.figure()
    plt.subplot(231), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_true, cmap="gray"), plt.title("image_true")
    plt.subplot(232), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_full, cmap="gray"), plt.title("image_full")
    plt.subplot(233), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_sparse_0, cmap="gray"), plt.title("image_sparse_0")
    plt.subplot(234), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_sparse_1, cmap="gray"), plt.title("image_sparse_1")
    plt.subplot(235), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_sparse_2, cmap="gray"), plt.title("image_sparse_2")
    plt.subplot(236), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_pred, cmap="gray"), plt.title("image_pred")
    plt.show()

    plt.figure()
    plt.subplot(241), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_sparse_0, cmap="gray"), plt.title("image_sparse_0")
    plt.subplot(242), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_sparse_1, cmap="gray"), plt.title("image_sparse_1")
    plt.subplot(243), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_sparse_2, cmap="gray"), plt.title("image_sparse_2")
    plt.subplot(244), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_pred, cmap="gray"), plt.title("image_pred")
    plt.subplot(245), plt.xticks([]), plt.yticks(
        []), plt.imshow(image_full - image_sparse_0,
                        cmap="gray"), plt.title("Res image_sparse_0")
    plt.subplot(246), plt.xticks([]), plt.yticks(
        []), plt.imshow(image_full - image_sparse_1,
                        cmap="gray"), plt.title("Res image_sparse_1")
    plt.subplot(247), plt.xticks([]), plt.yticks(
        []), plt.imshow(image_full - image_sparse_2,
                        cmap="gray"), plt.title("Res image_sparse_2")
    plt.subplot(248), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_full - image_pred, cmap="gray"), plt.title("Res image_pred")
    plt.show()
    """
    ***********************************************************************************************************
    量化指标
    ***********************************************************************************************************
    """
    ssim_0, mse_0, psnr_0 = ssim_mse_psnr(image_full, image_sparse_0)
    ssim_1, mse_1, psnr_1 = ssim_mse_psnr(image_full, image_sparse_1)
    ssim_2, mse_2, psnr_2 = ssim_mse_psnr(image_full, image_sparse_2)
    ssim_3, mse_3, psnr_3 = ssim_mse_psnr(image_full, image_pred)
    print("Sparse Image--> SSIM:{}, MSE:{}, PSNR:{}".format(
        ssim_0, mse_0, psnr_0))
    print("Iter_1 Image--> SSIM:{}, MSE:{}, PSNR:{}".format(
        ssim_1, mse_1, psnr_1))
    print("Iter_2 Image--> SSIM:{}, MSE:{}, PSNR:{}".format(
        ssim_2, mse_2, psnr_2))
    print("Pred Image--> SSIM:{}, MSE:{}, PSNR:{}".format(
        ssim_3, mse_3, psnr_3))
Example #49
0
# print(xseries)

series1 = []
series2 = []
series3 = plt.sin(xseries)

maxvalues_xaxis = plt.arange(plt.pi / 2, 30, plt.pi)
maxvalues_yaxis = plt.sin(maxvalues_xaxis)

for i in xseries:
    series1.append(i)
    series2.append(i / 2)

# Create individual figures using subplot()
plt.subplot(211)  # xyz -> row, column, position
plt.plot(xseries, series1, label="Faster Linear Progression")
plt.plot(xseries, series2, "y:", label="Slower Linear Progression")
plt.legend()

plt.subplot(212)  # xyz -> row, column, position
# style(color(r, g, b, c, m, y, k), line(:, --), marker(^))
plt.plot(xseries, series3, "k--", label="sin() function")
plt.plot(maxvalues_xaxis,
         maxvalues_yaxis,
         "r^",
         label="Extreme Values of sin()")
# plt.text() is used to put text inside the graph
for i in range(len(maxvalues_xaxis)):
    plt.text(maxvalues_xaxis[i], maxvalues_yaxis[i], str(maxvalues_yaxis[i]))
plt.ylim(-2, 4)
Example #50
0
import netCDF4 as nc4

pl.close('all')


class Read_stat:
    def __init__(self, file_name):
        f = nc4.Dataset(file_name)
        for v in f.variables:
            setattr(self, v, f.variables[v][:])


s = Read_stat('ls_force.default.0000000.nc')

pl.figure()
pl.subplot(231)
pl.plot(s.t, s.phh[:, 0])
pl.ylabel('Surface pressure (Pa)')
pl.xlabel('time (s)')

pl.subplot(232)
pl.plot(s.t, s.thlflux[:, 0])
pl.ylabel('wthl_s (K m s-1)')
pl.xlabel('time (s)')

pl.subplot(233)
pl.plot(s.thl[0, :], s.z, label='t={}'.format(s.t[0]))
pl.plot(s.thl[18, :], s.z, label='t={}'.format(s.t[18]), dashes=[4, 2])
pl.plot(s.thl[-1, :], s.z, label='t={}'.format(s.t[-1]))
pl.legend()
pl.xlabel('thl (K)')
Example #51
0
def plotTS_NWP_Comparison(sFileName,
                          oTimeRun,
                          oRain_OBS,
                          oRain_NWP_DET,
                          oRain_NWP_PROB,
                          oDischarge_MOD_NWP_DET,
                          oDischarge_MOD_NWP_PROB,
                          oRain_Accumulated_OBS,
                          oRain_Accumulated_NWP_DET,
                          oSoilMoisture_OBS,
                          oDischarge_OBS=None,
                          oDischarge_MOD_OBS=None,
                          sTypeRun='NWP_Comparison',
                          iFigDPI=120,
                          bOutlierDetection=False):

    # -------------------------------------------------------------------------------------
    # Check function
    try:

        # -------------------------------------------------------------------------------------
        # Adjust data in a compatible format
        oRain_OBS = configGraphIdxDF_TS(oRain_OBS)[0]
        oRain_Accumulated_OBS = configGraphIdxDF_TS(oRain_Accumulated_OBS)[0]

        oRain_NWP_DET_TYPE1 = configGraphIdxDF_TS(oRain_NWP_DET)[0]
        oRain_NWP_DET_TYPE2 = configGraphIdxDF_TS(oRain_NWP_DET)[1]
        oRain_NWP_PROB_TYPE1 = configGraphIdxDF_TS(oRain_NWP_PROB)[0]
        oRain_NWP_PROB_TYPE2 = configGraphIdxDF_TS(oRain_NWP_PROB)[1]
        oRain_Accumulated_NWP_DET_TYPE1 = configGraphIdxDF_TS(
            oRain_Accumulated_NWP_DET)[0]
        oRain_Accumulated_NWP_DET_TYPE2 = configGraphIdxDF_TS(
            oRain_Accumulated_NWP_DET)[1]

        oDischarge_OBS = configGraphIdxDF_TS(oDischarge_OBS)[0]
        oDischarge_MOD_OBS = configGraphIdxDF_TS(oDischarge_MOD_OBS)[0]

        oDischarge_MOD_NWP_DET_TYPE1 = configGraphIdxDF_TS(
            oDischarge_MOD_NWP_DET)[0]
        oDischarge_MOD_NWP_DET_TYPE2 = configGraphIdxDF_TS(
            oDischarge_MOD_NWP_DET)[1]
        oDischarge_MOD_NWP_PROB_TYPE1 = configGraphIdxDF_TS(
            oDischarge_MOD_NWP_PROB)[0]
        oDischarge_MOD_NWP_PROB_TYPE2 = configGraphIdxDF_TS(
            oDischarge_MOD_NWP_PROB)[1]

        oSoilMoisture_OBS = configGraphIdxDF_TS(oSoilMoisture_OBS)[0]
        # -------------------------------------------------------------------------------------

        # -------------------------------------------------------------------------------------
        # Get data values and metadata
        oRain_OBS_VAL, oRain_OBS_META = configGraphData_TS(oRain_OBS)
        oRain_NWP_DET_VAL_TYPE1, oRain_NWP_DET_META_TYPE1 = configGraphData_TS(
            oRain_NWP_DET_TYPE1)
        oRain_NWP_DET_VAL_TYPE2, oRain_NWP_DET_META_TYPE2 = configGraphData_TS(
            oRain_NWP_DET_TYPE2)
        oRain_NWP_PROB_VAL_TYPE1, oRain_NWP_PROB_META_TYPE1 = configGraphData_TS(
            oRain_NWP_PROB_TYPE1)
        oRain_NWP_PROB_VAL_TYPE2, oRain_NWP_PROB_META_TYPE2 = configGraphData_TS(
            oRain_NWP_PROB_TYPE2)

        oRain_ACC_OBS_VAL, oRain_ACC_OBS_META = configGraphData_TS(
            oRain_Accumulated_OBS)
        oRain_ACC_NWP_DET_VAL_TYPE1, oRain_ACC_NWP_DET_META_TYPE1 = configGraphData_TS(
            oRain_Accumulated_NWP_DET_TYPE1)
        oRain_ACC_NWP_DET_VAL_TYPE2, oRain_ACC_NWP_DET_META_TYPE2 = configGraphData_TS(
            oRain_Accumulated_NWP_DET_TYPE2)

        oDischarge_OBS_VAL, oDischarge_OBS_META = configGraphData_TS(
            oDischarge_OBS)
        oDischarge_MOD_OBS_VAL, oDischarge_MOD_OBS_META = configGraphData_TS(
            oDischarge_MOD_OBS)

        oDischarge_MOD_NWP_DET_VAL_TYPE1, oDischarge_MOD_NWP_DET_META_TYPE1 = configGraphData_TS(
            oDischarge_MOD_NWP_DET_TYPE1)
        oDischarge_MOD_NWP_PROB_VAL_TYPE2, oDischarge_MOD_NWP_PROB_META_TYPE2 = configGraphData_TS(
            oDischarge_MOD_NWP_PROB_TYPE2)
        oDischarge_MOD_NWP_PROB_VAL_TYPE1, oDischarge_MOD_NWP_PROB_META_TYPE1 = configGraphData_TS(
            oDischarge_MOD_NWP_PROB_TYPE1)
        oDischarge_MOD_NWP_DET_VAL_TYPE2, oDischarge_MOD_NWP_DET_META_TYPE2 = configGraphData_TS(
            oDischarge_MOD_NWP_DET_TYPE2)

        oDischarge_MOD_NWP_PROB_META_MERGED = mergeDataRegistry(
            oDischarge_MOD_NWP_PROB_META_TYPE1,
            oDischarge_MOD_NWP_PROB_META_TYPE2)

        oSoilMoisture_OBS_VAL, oSoilMoisture_OBS_META = configGraphData_TS(
            oSoilMoisture_OBS)

        oDischarge_MOD_OBS_VAL = filterVarLimit(oDischarge_MOD_OBS_VAL,
                                                oDischarge_MOD_OBS_META,
                                                dVar_MAX_OUTLIER=10000)
        oDischarge_MOD_NWP_DET_VAL_TYPE1 = filterVarLimit(
            oDischarge_MOD_NWP_DET_VAL_TYPE1,
            oDischarge_MOD_NWP_DET_META_TYPE1,
            dVar_MAX_OUTLIER=10000)
        oDischarge_MOD_NWP_DET_VAL_TYPE2 = filterVarLimit(
            oDischarge_MOD_NWP_DET_VAL_TYPE2,
            oDischarge_MOD_NWP_DET_META_TYPE2,
            dVar_MAX_OUTLIER=10000)
        oDischarge_MOD_NWP_PROB_VAL_TYPE1 = filterVarLimit(
            oDischarge_MOD_NWP_PROB_VAL_TYPE1,
            oDischarge_MOD_NWP_PROB_META_TYPE1,
            dVar_MAX_OUTLIER=10000)
        oDischarge_MOD_NWP_PROB_VAL_TYPE2 = filterVarLimit(
            oDischarge_MOD_NWP_PROB_VAL_TYPE2,
            oDischarge_MOD_NWP_PROB_META_TYPE2,
            dVar_MAX_OUTLIER=10000)

        if hasattr(oDischarge_MOD_NWP_PROB_VAL_TYPE1, 'min'):
            del oDischarge_MOD_NWP_PROB_VAL_TYPE1.min
        if hasattr(oDischarge_MOD_NWP_PROB_VAL_TYPE1, 'max'):
            del oDischarge_MOD_NWP_PROB_VAL_TYPE1.max
        if hasattr(oDischarge_MOD_NWP_PROB_VAL_TYPE2, 'min'):
            del oDischarge_MOD_NWP_PROB_VAL_TYPE2.min
        if hasattr(oDischarge_MOD_NWP_PROB_VAL_TYPE2, 'max'):
            del oDischarge_MOD_NWP_PROB_VAL_TYPE2.max

        if bOutlierDetection:
            oDischarge_MOD_NWP_PROB_TYPE1, bVarData_OUTLIER_TYPE1 = detectVarOutlier(
                oDischarge_MOD_NWP_PROB_VAL_TYPE1)
            oDischarge_MOD_NWP_PROB_TYPE2, bVarData_OUTLIER_TYPE2 = detectVarOutlier(
                oDischarge_MOD_NWP_PROB_VAL_TYPE2)
            if bVarData_OUTLIER_TYPE1:
                oDischarge_MOD_NWP_PROB_FILTER_TYPE1 = filterVarOutlier(
                    oDischarge_MOD_NWP_PROB_TYPE1)
            else:
                oDischarge_MOD_NWP_PROB_FILTER_TYPE1 = oDischarge_MOD_NWP_PROB_TYPE1
            if bVarData_OUTLIER_TYPE2:
                oDischarge_MOD_NWP_PROB_FILTER_TYPE2 = filterVarOutlier(
                    oDischarge_MOD_NWP_PROB_TYPE2)
            else:
                oDischarge_MOD_NWP_PROB_FILTER_TYPE2 = oDischarge_MOD_NWP_PROB_TYPE2
        else:
            oDischarge_MOD_NWP_PROB_FILTER_TYPE1 = oDischarge_MOD_NWP_PROB_VAL_TYPE1
            oDischarge_MOD_NWP_PROB_FILTER_TYPE1.iloc[-1] = np.nan
            oDischarge_MOD_NWP_PROB_FILTER_TYPE2 = oDischarge_MOD_NWP_PROB_VAL_TYPE2
            oDischarge_MOD_NWP_PROB_FILTER_TYPE2.iloc[-1] = np.nan

        # Compute nwp probabilistic peak(s)
        oDischarge_MOD_NWP_PROB_PEAKS_TYPE1 = computeVarPeaks(
            oDischarge_MOD_NWP_PROB_FILTER_TYPE1, dVar_PEAK_MIN=0)
        oDischarge_MOD_NWP_PROB_PEAKS_TYPE2 = computeVarPeaks(
            oDischarge_MOD_NWP_PROB_FILTER_TYPE2, dVar_PEAK_MIN=0)

        # Compute nwp probabilistic quantile(s)
        oDischarge_MOD_NWP_PROB_TYPE1 = computeVarQuantile(
            oDischarge_MOD_NWP_PROB_FILTER_TYPE1,
            sVarData_QTL='qtls',
            oVarData_QTL=[0, 0.25, 0.5, 0.75, 1],
            iVarAxis=1)
        oDischarge_MOD_NWP_PROB_TYPE2 = computeVarQuantile(
            oDischarge_MOD_NWP_PROB_FILTER_TYPE2,
            sVarData_QTL='qtls',
            oVarData_QTL=[0, 0.25, 0.5, 0.75, 1],
            iVarAxis=1)

        # Get time information
        [oVarTick_TimePeriod, oVarTick_TimeIdx,
         oVarTick_TimeLabels] = configGraphTime_TS(oRain_OBS_VAL)
        sTimeRun = oTimeRun.strftime(sTimeFormat)
        sTimeNWP_TYPE1 = oDischarge_MOD_NWP_DET_META_TYPE1['time_dataset']
        sTimeNWP_TYPE2 = oDischarge_MOD_NWP_DET_META_TYPE2['time_dataset']

        # Get header information
        oVarHeader = configGraphHeader_TS(oRain_OBS_META)

        # Set graph range (rain and discharge)
        dRain_MIN_GRAPH, dRain_MAX_GRAPH = setGraphRange_Rain(oRain_OBS_META)
        dDischarge_MIN_GRAPH, dDischarge_MAX_GRAPH = setGraphRange_Discharge(
            oDischarge_MOD_OBS_META,
            dVar_MIN_OUTLIER=0,
            dVar_MAX_OUTLIER=10000)

        if sTimeNWP_TYPE1 is None:
            sTimeNWP_TYPE1 = "NA"
            oTimeNWP_TYPE1 = None
        else:
            oTimeNWP_TYPE1 = pd.Timestamp(sTimeNWP_TYPE1)
        if sTimeNWP_TYPE2 is None:
            sTimeNWP_TYPE2 = "NA"
            oTimeNWP_TYPE2 = None
        else:
            oTimeNWP_TYPE2 = pd.Timestamp(sTimeNWP_TYPE2)
        # -------------------------------------------------------------------------------------

        # -------------------------------------------------------------------------------------
        # Open figure
        fig = plt.figure(figsize=(17, 11))
        fig.autofmt_xdate()

        mpl.rcParams['hatch.linewidth'] = 0.01
        mpl.rcParams['hatch.color'] = '#0000FF'

        # Subplot 1
        ax1 = plt.subplot(3, 1, 1)
        p11 = ax1.bar(oRain_OBS_VAL.index,
                      oRain_OBS_VAL.values,
                      color='#33A1C9',
                      alpha=1,
                      width=0.025,
                      align='edge')
        p12 = ax1.bar(oRain_NWP_DET_VAL_TYPE1.index,
                      oRain_NWP_DET_VAL_TYPE1.values,
                      edgecolor='#0000FF',
                      color='None',
                      alpha=1,
                      width=0.025,
                      align='edge')  # hatch="/"
        p13 = ax1.bar(oRain_NWP_DET_VAL_TYPE2.index,
                      oRain_NWP_DET_VAL_TYPE2.values,
                      edgecolor='#003666',
                      color='None',
                      alpha=1,
                      width=0.025,
                      align='edge')  # hatch="/"

        ax1.set_xticks(oVarTick_TimeIdx)
        ax1.set_xticklabels([])

        ax1.set_xlim(oVarTick_TimePeriod[0], oVarTick_TimePeriod[-1])
        ax1.set_ylabel('rain [mm]', color='#000000')
        ax1.set_ylim(dRain_MIN_GRAPH, dRain_MAX_GRAPH)
        ax1.grid(b=True)

        p14 = ax1.axvline(oTimeRun, color='#000000', linestyle='--', lw=2)
        if oTimeNWP_TYPE1:
            p15 = ax1.axvline(oTimeNWP_TYPE1,
                              color='#FF5733',
                              linestyle='--',
                              lw=2)
        if oTimeNWP_TYPE2:
            p16 = ax1.axvline(oTimeNWP_TYPE2,
                              color='#FF8970',
                              linestyle='-',
                              lw=2)

        ax1.set_title('Time Series \n Section: ' + oVarHeader['section_name'] +
                      ' == Basin: ' + oVarHeader['section_basin'] +
                      ' == Area [Km^2]: ' + oVarHeader['section_area'] +
                      ' \n  TypeRun: ' + sTypeRun + ' == Time_Run: ' +
                      sTimeRun + ' Time_NWP_M1: ' + sTimeNWP_TYPE1 +
                      ' Time_NWP_M2: ' + sTimeNWP_TYPE2)

        ax3 = ax1.twinx()
        p31 = ax3.plot(oRain_ACC_OBS_VAL, color='#33A1C9', linestyle='-', lw=1)
        p32 = ax3.plot(oRain_ACC_NWP_DET_VAL_TYPE1,
                       color='#0000FF',
                       linestyle='-',
                       lw=1)
        p33 = ax3.plot(oRain_ACC_NWP_DET_VAL_TYPE2,
                       color='#003666',
                       linestyle='-',
                       lw=1)

        ax3.set_ylabel('rain accumulated [mm]', color='#000000')
        ax3.set_ylim(0, 500)

        ax3.set_xticks(oVarTick_TimeIdx)
        ax3.set_xticklabels([])
        ax3.set_xlim(oVarTick_TimePeriod[0], oVarTick_TimePeriod[-1])

        legend = ax1.legend((p11, p12, p13, p31[0], p32[0], p33[0]), (
            oRain_OBS_META['var_appearance'],
            oRain_NWP_DET_META_TYPE1['var_appearance'],
            oRain_NWP_DET_META_TYPE2['var_appearance'],
            oRain_ACC_OBS_META['var_appearance'],
            oRain_ACC_NWP_DET_META_TYPE1['var_appearance'],
            oRain_ACC_NWP_DET_META_TYPE2['var_appearance'],
        ),
                            frameon=False,
                            loc=2)

        ax1.add_artist(legend)

        # Subplot 2
        ax2 = plt.subplot(3, 1, (2, 3))

        p21 = ax2.plot(oDischarge_OBS_VAL,
                       color='#000000',
                       linestyle='--',
                       lw=1,
                       marker='o',
                       ms=4)
        p22 = ax2.plot(oDischarge_MOD_OBS_VAL,
                       color='#0000FF',
                       linestyle='-',
                       lw=1)
        # START NWP 1
        p23 = ax2.plot(oDischarge_MOD_NWP_DET_VAL_TYPE1,
                       color='#0F4610',
                       linestyle='-',
                       lw=1)
        ax2.fill_between(oDischarge_MOD_NWP_PROB_TYPE1.index,
                         oDischarge_MOD_NWP_PROB_TYPE1['qtls0.0'].values,
                         oDischarge_MOD_NWP_PROB_TYPE1['qtls0.25'].values,
                         where=oDischarge_MOD_NWP_PROB_TYPE1['qtls0.25'] >
                         oDischarge_MOD_NWP_PROB_TYPE1['qtls0.0'],
                         facecolor='#D3D3D3',
                         alpha=10,
                         interpolate=False)
        p24 = Rectangle((0, 0), 1, 1, fc='#D3D3D3')
        ax2.fill_between(oDischarge_MOD_NWP_PROB_TYPE1.index,
                         oDischarge_MOD_NWP_PROB_TYPE1['qtls0.25'].values,
                         oDischarge_MOD_NWP_PROB_TYPE1['qtls0.75'].values,
                         where=oDischarge_MOD_NWP_PROB_TYPE1['qtls0.75'] >=
                         oDischarge_MOD_NWP_PROB_TYPE1['qtls0.25'],
                         facecolor='#A9A9A9',
                         alpha=10,
                         interpolate=False)
        p25 = Rectangle((0, 0), 1, 1, fc='#A9A9A9')
        ax2.fill_between(oDischarge_MOD_NWP_PROB_TYPE1.index,
                         oDischarge_MOD_NWP_PROB_TYPE1['qtls0.75'].values,
                         oDischarge_MOD_NWP_PROB_TYPE1['qtls1.0'].values,
                         where=oDischarge_MOD_NWP_PROB_TYPE1['qtls1.0'] >=
                         oDischarge_MOD_NWP_PROB_TYPE1['qtls0.75'],
                         facecolor='#D3D3D3',
                         alpha=10,
                         interpolate=False)
        p26 = Rectangle((0, 0), 1, 1, fc='#D3D3D3')

        p27 = ax2.plot(pd.DatetimeIndex(
            oDischarge_MOD_NWP_PROB_PEAKS_TYPE1.time),
                       oDischarge_MOD_NWP_PROB_PEAKS_TYPE1.peak,
                       color='#0F4610',
                       linestyle='-',
                       lw=0,
                       marker='*',
                       ms=4)
        p28 = Rectangle((0, 0), 1, 1, fc='#0000FF')
        # END NWP 1 #9C4293
        # START NWP 2
        p29 = ax2.plot(oDischarge_MOD_NWP_DET_VAL_TYPE2,
                       color='#9C4293',
                       linestyle='-',
                       lw=1)
        ax2.fill_between(oDischarge_MOD_NWP_PROB_TYPE2.index,
                         oDischarge_MOD_NWP_PROB_TYPE2['qtls0.0'].values,
                         oDischarge_MOD_NWP_PROB_TYPE2['qtls0.25'].values,
                         where=oDischarge_MOD_NWP_PROB_TYPE2['qtls0.25'] >
                         oDischarge_MOD_NWP_PROB_TYPE2['qtls0.0'],
                         facecolor='#F7F8A4',
                         alpha=10,
                         interpolate=False)
        p210 = Rectangle((0, 0), 1, 1, fc='#F7F8A4')
        ax2.fill_between(oDischarge_MOD_NWP_PROB_TYPE2.index,
                         oDischarge_MOD_NWP_PROB_TYPE2['qtls0.25'].values,
                         oDischarge_MOD_NWP_PROB_TYPE2['qtls0.75'].values,
                         where=oDischarge_MOD_NWP_PROB_TYPE2['qtls0.75'] >=
                         oDischarge_MOD_NWP_PROB_TYPE2['qtls0.25'],
                         facecolor='#C6C80F',
                         alpha=10,
                         interpolate=False)
        p211 = Rectangle((0, 0), 1, 1, fc='#C6C80F')
        ax2.fill_between(oDischarge_MOD_NWP_PROB_TYPE2.index,
                         oDischarge_MOD_NWP_PROB_TYPE2['qtls0.75'].values,
                         oDischarge_MOD_NWP_PROB_TYPE2['qtls1.0'].values,
                         where=oDischarge_MOD_NWP_PROB_TYPE2['qtls1.0'] >=
                         oDischarge_MOD_NWP_PROB_TYPE2['qtls0.75'],
                         facecolor='#F7F8A4',
                         alpha=10,
                         interpolate=False)
        p212 = Rectangle((0, 0), 1, 1, fc='#F7F8A4')

        p213 = ax2.plot(pd.DatetimeIndex(
            oDischarge_MOD_NWP_PROB_PEAKS_TYPE2.time),
                        oDischarge_MOD_NWP_PROB_PEAKS_TYPE2.peak,
                        color='#9C4293',
                        linestyle='-',
                        lw=0,
                        marker='*',
                        ms=4)
        p214 = Rectangle((0, 0), 1, 1, fc='#9C4293')
        # END NWP2

        ax2.set_xlabel('time [hour]', color='#000000')
        ax2.set_xlim(oVarTick_TimePeriod[0], oVarTick_TimePeriod[-1])
        ax2.set_ylabel('discharge [m^3/s]', color='#000000')
        ax2.set_ylim(dDischarge_MIN_GRAPH, dDischarge_MAX_GRAPH)

        ax2.grid(b=True)

        p215 = ax2.axvline(oTimeRun,
                           color='#000000',
                           linestyle='--',
                           lw=2,
                           label='time run')

        p216 = ax2.axhline(float(oVarHeader['section_q_thr1']),
                           color='#FFA500',
                           linestyle='--',
                           linewidth=2,
                           label='discharge thr alarm')
        p217 = ax2.axhline(float(oVarHeader['section_q_thr2']),
                           color='#FF0000',
                           linestyle='--',
                           linewidth=2,
                           label='discharge thr alert')

        if oTimeNWP_TYPE1:
            p218 = ax2.axvline(oTimeNWP_TYPE1,
                               color='#FF5733',
                               linestyle='--',
                               lw=2,
                               label='time nwp m1')
        if oTimeNWP_TYPE2:
            p219 = ax2.axvline(oTimeNWP_TYPE2,
                               color='#FF8970',
                               linestyle='-',
                               lw=2,
                               label='time nwp m2')

        ax2.set_xticks(oVarTick_TimeIdx)
        ax2.set_xticklabels(oVarTick_TimeLabels, rotation=45, fontsize=8)

        ax4 = ax2.twinx()
        p41 = ax4.plot(oSoilMoisture_OBS_VAL,
                       color='#DA70D6',
                       linestyle='--',
                       lw=2)

        ax4.set_ylabel('soil moisture [-]', color='#000000')
        ax4.set_ylim(0, 1)
        ax4.set_xticks(oVarTick_TimeIdx)
        ax4.set_xticklabels(oVarTick_TimeLabels, rotation=45, fontsize=8)

        legend1 = ax2.legend((p21[0], p22[0], p23[0], p29[0], p41[0]), (
            oDischarge_OBS_META['var_appearance'],
            oDischarge_MOD_OBS_META['var_appearance'],
            oDischarge_MOD_NWP_DET_META_TYPE1['var_appearance'],
            oDischarge_MOD_NWP_DET_META_TYPE2['var_appearance'],
            oSoilMoisture_OBS_META['var_appearance'],
        ),
                             frameon=False,
                             ncol=2,
                             loc=0)

        legend2 = ax2.legend(
            (p24, p210, p25, p211, p216, p217),
            ('< 25% -- 75% > m1', '< 25% -- 75% > m2', '25% -- 75% m1',
             '25% -- 75% m2', 'discharge thr alarm', 'discharge thr alert'),
            frameon=False,
            ncol=4,
            loc=9,
            bbox_to_anchor=(0.5, -0.2))

        ax2.add_artist(legend1)
        ax2.add_artist(legend2)

        # plt.show()
        if not os.path.exists(sFileName):
            createFolderByFile(sFileName)
        fig.savefig(sFileName, dpi=iFigDPI)

        plt.close()
        # -------------------------------------------------------------------------------------

        # -------------------------------------------------------------------------------------
        # Write ancillary file in json format (to configure png attributes)
        if os.path.exists(sFileName):
            sFileRoot = os.path.splitext(sFileName)[0]
            writeFileGraph_JSON(sFileRoot + '.json',
                                oDischarge_MOD_NWP_PROB_META_MERGED)
        # -------------------------------------------------------------------------------------

        # -------------------------------------------------------------------------------------
        # Exit with success
        oGraphStatus = True
        # -------------------------------------------------------------------------------------

    except BaseException:

        raise IOError

        # -------------------------------------------------------------------------------------
        # Exit with warning
        Exc.getExc(
            ' =====> WARNING: graph ' + sTypeRun + ' in ' + sFileName +
            ' failed! Error(s) occurred in plot generation! ', 2, 1)
        oGraphStatus = False
        # -------------------------------------------------------------------------------------

    # -------------------------------------------------------------------------------------
    # Return code
    return oGraphStatus
Example #52
0
def prepare_model(minute, closep1, openp, lowp, data, timestamp):
    data = scale_data(data)
    p = int(len(lowp) * 0.70)
    openp = openp[p:]
    lowp = lowp[p:]
    tr_date, ts_date, x_train, x_test, y_train, y_test = prepare_data(
        minute, data, timestamp)
    #ts_date = timestamp_to_dataconv(ts_date)
    print(x_train.shape, y_train.shape, y_test.shape, y_test.shape)
    #print(x_train)
    try:
        final_model = Sequential()
        #final_model.load_weights('ann_models/feedForwardNetwork/ffnn-weights',by_name=True)

        # final_model.add(Dropout(0.2, input_shape=(len(x_train[0]),)))
        final_model.add(
            Dense(112,
                  input_shape=(len(x_train[0]), ),
                  kernel_initializer='uniform',
                  activation='linear',
                  kernel_constraint=maxnorm(3)))
        # final_model.add(Dense(112, input_shape=(len(x_train[0]),)))
        final_model.add(
            Dense(1,
                  kernel_initializer='uniform',
                  kernel_constraint=maxnorm(3),
                  activation="linear"))
        # summary of model
        # print("summaryof model: ", final_model.summary())
        # plot graph
        # plot_model(final_model, to_file="DeepNeuralnetwork.png")
        # Compile model
        sgd = SGD(lr=0.01, momentum=0.9, decay=0.0, nesterov=False)
        opt = Adam(lr=0.01)
        #final_model.compile(optimizer=sgd, loss=losses.logcosh)
        final_model.compile(loss='binary_crossentropy',
                            optimizer='adam',
                            metrics=['accuracy'])

        # fit the model
        final_model.fit(x_train,
                        y_train,
                        epochs=5,
                        batch_size=256,
                        verbose=0,
                        validation_data=(x_test, y_test))
        scores = final_model.evaluate(x_train, y_train, verbose=0)
        #scores = model.evaluate(X, Y, verbose=0)
        print("%s: %.2f%%" % (final_model.metrics_names[1], scores[1] * 100))
        print("fit end")
        # print("weights:  ", final_model.get_weights())
        # print("param:  ",final_model.count_params())
        # print(final_model.__reduce__())
        # print(final_model.legacy_get_config())
        pred = final_model.predict(x_test)

    except:
        print("something is wrong in model")

    predicted = pred
    predicted = predicted.ravel()
    original = y_test
    # actual converted values after model
    minimum, maximum = min_max(closep1)
    pred1 = calculate_actual_values(predicted, maximum, minimum)
    actual1 = calculate_actual_values(original, maximum, minimum)

    mseA = mean_squared_error(actual1, pred1)
    rmsefA = root_mean_square_error_fun(mseA)
    maefA = mean_absolute_error_fun(actual1, pred1)
    mape1A = mean_absolute_percentage_error_fun(actual1, pred1)
    r_scoreA = r2_score(actual1, pred1)

    # writing data in csv
    # write_predicted_data(ts_date, actual1, pred1)
    #write_errors_in_file(rmsefA, maefA, mape1A)

    print("mse:  ", mseA)
    print("rmse: ", rmsefA)
    print("mae: ", maefA)
    print("mape: ", mape1A)
    print("r2score: ", r_scoreA)

    errors_label = ('rmse', 'mae', 'mape')
    y_pos = np.arange(len(errors_label))
    error_values = np.array([rmsefA, maefA, mape1A])
    width = 0.75

    plt.figure(1)
    # plt.subplot(211)
    plt.subplot(221)
    plt.xticks(rotation=30)

    plt.plot(ts_date, actual1, label="Close Price", color='green')
    #plt.plot(ts_date, lowp, label='Low', color='blue')
    #plt.plot(ts_date, openp, label='Open', color='yellow')
    plt.plot(ts_date, pred1, label='Predicted', color='red')

    plt.grid(True)
    plt.title("Bitcoin forecast for " + str(minute) + " minutes (FFNN)",
              fontweight='bold')
    plt.legend(loc=2)
    plt.xlabel("Time")
    plt.ylabel("Price of Bitcoin (USD)")
    # plt.subplots_adjust(hspace=0.4, wspace=0.4)
    plt.subplot(222)
    # plt.subplot(223)
    plt.bar(
        y_pos,
        error_values,
        width,
        align='center',
        alpha=0.5,
        color='red',
    )

    plt.xticks(y_pos, errors_label)
    for a, b in zip(y_pos, error_values):
        plt.text(a, b, str(b))
        # plt.annotate(str(b),y_poserror_values=(a,b))

    plt.title('Evaluation Criteria', fontweight='bold')
    plt.xlabel('Errors')
    plt.ylabel('Values')
    plt.subplot(212)
    # plt.subplot(222)
    # plt.subplot(212)
    plt.title("Architecture of Model", fontsize=14)
    # plt.yticks(np.arange(len(tests)) * -1)
    # for i, s in enumerate(tests):
    #   plt.text(0.1,-i/2, s,fontsize=12,horizontalalignment='left',
    #           backgroundcolor='palegreen',wrap=True)
    plt.text(0.025, 0.8, "Layers :- ", fontweight='bold')
    plt.text(0.2, 0.8, "Two layers", fontsize=10)

    plt.text(0.025, 0.6, "Activation Fun :- ", fontweight='bold')
    plt.text(0.2, 0.6, "{ sigmoid, linear }")

    plt.text(0.025, 0.4, "Data_Set :- ", fontweight='bold')
    plt.text(0.2, 0.4, "Training: 70% and Testing: 30%, inputs: 5 ")

    plt.text(0.025, 0.2, "Features :- ", fontweight='bold')
    plt.text(0.2, 0.2,
             "batchsize: 256,  epochs: 5,  lr: 0.01, Optimizer: Adam")

    plt.subplots_adjust(hspace=0.6,
                        wspace=0.5,
                        left=0.125,
                        bottom=0.1,
                        right=None,
                        top=None)

    # Save Figure
    #plt.savefig("foo.png")
    # Save Transparent Figure
    #plt.savefig("foo.png", transparent=True)

    plt.show()

    print("ended model process")
    return ts_date, pred1, actual1
Example #53
0
		thrust[0,i] = prob['TubeAndPod.pod.nozzle.Fg']-prob['TubeAndPod.pod.inlet.F_ram']
	    print(i)

	np.savetxt('../../../paper/images/data_files/mach_trades/M_pod.txt', M_pod, fmt = '%f', delimiter = '\t', newline = '\r\n')
	np.savetxt('../../../paper/images/data_files/mach_trades/Re.txt', Re, fmt = '%f', delimiter = '\t', newline = '\r\n')
	np.savetxt('../../../paper/images/data_files/mach_trades/A_tube.txt', A_tube, fmt = '%f', delimiter = '\t', newline = '\r\n')
	np.savetxt('../../../paper/images/data_files/mach_trades/T_tunnel.txt', T_tunnel, fmt = '%f', delimiter = '\t', newline = '\r\n')
	np.savetxt('../../../paper/images/data_files/mach_trades/L_pod.txt', L_pod, fmt = '%f', delimiter = '\t', newline = '\r\n')
	np.savetxt('../../../paper/images/data_files/mach_trades/comp_power.txt', power, fmt = '%f', delimiter = '\t', newline = '\r\n')
	np.savetxt('../../../paper/images/data_files/mach_trades/vac_power.txt', steady_vac, fmt = '%f', delimiter = '\t', newline = '\r\n')
	np.savetxt('../../../paper/images/data_files/mach_trades/total_energy.txt', total_energy, fmt = '%f', delimiter = '\t', newline = '\r\n')
	np.savetxt('../../../paper/images/data_files/mach_trades/thrust.txt', thrust, fmt = '%f', delimiter = '\t', newline = '\r\n')

	plt.figure(1)
	plt.hold(True)
	plt.subplot(211)
	plt.plot(M_pod, A_tube[0,:], 'b-', linewidth = 2.0)
	# plt.xlabel('Mach Number', fontsize = 12, fontweight = 'bold')
	plt.ylabel('Tube Area (m^2)', fontsize = 12, fontweight = 'bold')
	# plt.plot(M_pod, steady_vac[0,:], 'r-', linewidth = 2.0)
	# plt.xlabel('Mach Number', fontsize = 16, fontweight = 'bold')
	# plt.ylabel('Vacuum Power (hp)', fontsize = 16, fontweight = 'bold')
	# plt.show()
	plt.subplot(212)
	plt.plot(M_pod, total_energy[0,:]/(1.0e6), 'r-', linewidth = 2.0)
	plt.xlabel('Mach Number', fontsize = 12, fontweight = 'bold')
	plt.ylabel('Yearly Energy Cost (Million USD)', fontsize = 12, fontweight = 'bold')
	plt.ylim(25,35)
	plt.show()
	# plt.plot(M_pod, power[0,:])
	# plt.show()
Example #54
0
mov_inp = mov_inp.permute(0,2,3,1)
ref_inp = ref_inp.permute(0,2,3,1)
deformed = deformed.permute(0,2,3,1)
ref_def = ref_def.permute(0,2,3,1)

deformed, ref_def, grid, grid_inv = deformed.data.cpu().numpy(), ref_def.data.cpu().numpy(), grid.data.cpu().numpy(), grid_inv.data.cpu().numpy()


###PLOT####
xx, yy = np.meshgrid(range(mov_inp.shape[1]), range(mov_inp.shape[2]))
dx, dy = np.squeeze(grid[:,0,:,:]) + xx, np.squeeze(grid[:,1,:,:]) + yy
dxi, dyi = np.squeeze(grid_inv[:,0,:,:]) + xx, np.squeeze(grid_inv[:,1,:,:]) + yy


plt.figure(figsize=(10,4))
plt.subplot(1, 4, 1)
plt.imshow(np.squeeze(mov_inp.data.cpu().numpy()),cmap='gray')
plt.title('Moving Image')
plt.subplot(1, 4, 2)
plt.imshow(np.squeeze(deformed),cmap='gray')
plt.contour(dx, 50, alpha=0.5, linewidths=0.5)
plt.contour(dy, 50, alpha=0.5, linewidths=0.5)
plt.title('Forward Deformation \n applied on Moving Image')
plt.subplot(1, 4, 3)
plt.imshow(np.squeeze(ref_def),cmap='gray')
plt.contour(dxi, 50, alpha=0.5, linewidths=0.5)
plt.contour(dyi, 50, alpha=0.5, linewidths=0.5)
plt.title('Inverse Deformation \n applied on Deformed Image')
plt.subplot(1, 4, 4)
plt.imshow(np.squeeze(ref_inp.data.cpu().numpy()),cmap='gray')
plt.title('Reference Image')
Example #55
0
cat_w51e = ascii.read(
    '/Users/josh/GitHub/W51/data/W51-E.sw.sources.fin.ok_dec2020.cat',
    data_start=0,
    format='commented_header',
    header_start=110,
    comment="!")
cat_w51irs2 = ascii.read(
    '/Users/josh/GitHub/W51/data/W51-IRS2.sw.sources.fin.ok_dec2020.cat',
    data_start=0,
    format='commented_header',
    header_start=110,
    comment="!")
parmap = fits.open('/Users/josh/GitHub/W51/data/par_maps.fits')
ww = wcs.WCS(parmap[0].header).celestial
plt.figure(figsize=(20, 20))
plt.subplot(projection=ww)
ax = plt.gca()
plt.imshow(parmap[0].data[0])
plt.plot(cat_w51e['WCS_ACOOR'],
         cat_w51e['WCS_DCOOR'],
         'w.',
         transform=ax.get_transform('world'))
plt.plot(cat_w51irs2['WCS_ACOOR'],
         cat_w51irs2['WCS_DCOOR'],
         'r.',
         transform=ax.get_transform('world'))
xpix, ypix = ww.wcs_world2pix(cat_w51e['WCS_ACOOR'], cat_w51e['WCS_DCOOR'], 0)
temperatures_w51e = parmap[0].data[0][ypix.astype('int'), xpix.astype('int')]
xpix, ypix = ww.wcs_world2pix(cat_w51irs2['WCS_ACOOR'],
                              cat_w51irs2['WCS_DCOOR'], 0)
temperatures_irs2 = parmap[0].data[0][ypix.astype('int'), xpix.astype('int')]
Example #56
0
def plotTS_OBS(sFileName,
               oTimeRun,
               oRain_OBS,
               oRain_Accumulated_OBS,
               oSoilMoisture_OBS,
               oDischarge_OBS=None,
               oDischarge_MOD_OBS=None,
               sTypeRun='OBS_WeatherStations',
               iFigDPI=120):

    # -------------------------------------------------------------------------------------
    # Check function
    try:

        # -------------------------------------------------------------------------------------
        # Adjust data in a compatible format
        oRain_OBS = configGraphIdxDF_TS(oRain_OBS)[0]
        oRain_Accumulated_OBS = configGraphIdxDF_TS(oRain_Accumulated_OBS)[0]

        oDischarge_OBS = configGraphIdxDF_TS(oDischarge_OBS)[0]
        oDischarge_MOD_OBS = configGraphIdxDF_TS(oDischarge_MOD_OBS)[0]

        oSoilMoisture_OBS = configGraphIdxDF_TS(oSoilMoisture_OBS)[0]
        # -------------------------------------------------------------------------------------

        # -------------------------------------------------------------------------------------
        # Get data values and metadata
        oRain_OBS_VAL, oRain_OBS_META = configGraphData_TS(oRain_OBS)
        oRain_ACC_OBS_VAL, oRain_ACC_OBS_META = configGraphData_TS(
            oRain_Accumulated_OBS)

        oDischarge_OBS_VAL, oDischarge_OBS_META = configGraphData_TS(
            oDischarge_OBS)
        oDischarge_MOD_OBS_VAL, oDischarge_MOD_OBS_META = configGraphData_TS(
            oDischarge_MOD_OBS)

        oSoilMoisture_OBS_VAL, oSoilMoisture_OBS_META = configGraphData_TS(
            oSoilMoisture_OBS)

        # Get time information
        [oVarTick_TimePeriod, oVarTick_TimeIdx,
         oVarTick_TimeLabels] = configGraphTime_TS(oRain_OBS_VAL)
        sTimeRun = oTimeRun.strftime(sTimeFormat)

        # Get header information
        oVarHeader = configGraphHeader_TS(oRain_OBS_META)

        # Set graph range (rain and discharge)
        dRain_MIN_GRAPH, dRain_MAX_GRAPH = setGraphRange_Rain(oRain_OBS_META)
        dDischarge_MIN_GRAPH, dDischarge_MAX_GRAPH = setGraphRange_Discharge(
            oDischarge_MOD_OBS_META)
        # -------------------------------------------------------------------------------------

        # -------------------------------------------------------------------------------------
        # Open figure
        fig = plt.figure(figsize=(17, 11))
        fig.autofmt_xdate()

        # Subplot 1
        ax1 = plt.subplot(3, 1, 1)
        p11 = ax1.bar(oRain_OBS_VAL.index,
                      oRain_OBS_VAL.values,
                      color='#33A1C9',
                      alpha=1,
                      width=0.025,
                      align='edge')

        ax1.set_xticks(oVarTick_TimeIdx)
        ax1.set_xticklabels([])

        ax1.set_xlim(oVarTick_TimePeriod[0], oVarTick_TimePeriod[-1])
        ax1.set_ylabel('rain [mm]', color='#000000')
        ax1.set_ylim(dRain_MIN_GRAPH, dRain_MAX_GRAPH)
        ax1.grid(b=True)

        p13 = ax1.axvline(oTimeRun, color='#000000', linestyle='--', lw=2)

        ax3 = ax1.twinx()
        p31 = ax3.plot(oRain_ACC_OBS_VAL, color='#33A1C9', linestyle='-', lw=1)

        ax3.set_ylabel('rain accumulated [mm]', color='#000000')
        ax3.set_ylim(0, 500)

        ax3.set_xticks(oVarTick_TimeIdx)
        ax3.set_xticklabels([])
        ax3.set_xlim(oVarTick_TimePeriod[0], oVarTick_TimePeriod[-1])

        # legend = ax1.legend(p11, [oRain_OBS_META['var_appearance']], frameon=False, loc=2)

        legend = ax1.legend((p11[0], p31[0]), (
            oRain_OBS_META['var_appearance'],
            oRain_ACC_OBS_META['var_appearance'],
        ),
                            frameon=False,
                            loc=2)

        ax1.add_artist(legend)

        ax1.set_title('Time Series \n Section: ' + oVarHeader['section_name'] +
                      ' == Basin: ' + oVarHeader['section_basin'] +
                      ' == Area [Km^2]: ' + oVarHeader['section_area'] +
                      ' \n  TypeRun: ' + sTypeRun + ' == Time_Run: ' +
                      sTimeRun)

        # Subplot 2
        ax2 = plt.subplot(3, 1, (2, 3))

        p21 = ax2.plot(oDischarge_OBS_VAL,
                       color='#000000',
                       linestyle='--',
                       lw=1,
                       marker='o',
                       ms=4)
        p22 = ax2.plot(oDischarge_MOD_OBS_VAL,
                       color='#0000FF',
                       linestyle='-',
                       lw=1)

        ax2.set_xlabel('time [hour]', color='#000000')
        ax2.set_xlim(oVarTick_TimePeriod[0], oVarTick_TimePeriod[-1])
        ax2.set_ylabel('discharge [m^3/s]', color='#000000')
        ax2.set_ylim(dDischarge_MIN_GRAPH, dDischarge_MAX_GRAPH)

        ax2.grid(b=True)

        p27 = ax2.axvline(oTimeRun,
                          color='#000000',
                          linestyle='--',
                          lw=2,
                          label='time run')
        p28 = ax2.axhline(float(oVarHeader['section_q_thr1']),
                          color='#FFA500',
                          linestyle='--',
                          linewidth=2,
                          label='discharge thr alarm')
        p29 = ax2.axhline(float(oVarHeader['section_q_thr2']),
                          color='#FF0000',
                          linestyle='--',
                          linewidth=2,
                          label='discharge thr alert')

        ax2.set_xticks(oVarTick_TimeIdx)
        ax2.set_xticklabels(oVarTick_TimeLabels, rotation=45, fontsize=8)

        ax4 = ax2.twinx()
        p41 = ax4.plot(oSoilMoisture_OBS_VAL,
                       color='#DA70D6',
                       linestyle='--',
                       lw=2)

        ax4.set_ylabel('soil moisture [-]', color='#000000')
        ax4.set_ylim(0, 1)
        ax4.set_xticks(oVarTick_TimeIdx)
        ax4.set_xticklabels(oVarTick_TimeLabels, rotation=45, fontsize=8)

        legend1 = ax2.legend((p21[0], p22[0], p41[0]),
                             (oDischarge_OBS_META['var_appearance'],
                              oDischarge_MOD_OBS_META['var_appearance'],
                              oSoilMoisture_OBS_META['var_appearance']),
                             frameon=False,
                             ncol=2,
                             loc=0)
        legend2 = ax2.legend((p28, p29),
                             ('discharge thr alarm', 'discharge thr alert'),
                             frameon=False,
                             ncol=4,
                             loc=9,
                             bbox_to_anchor=(0.5, -0.2))

        ax2.add_artist(legend1)
        ax2.add_artist(legend2)

        # plt.show()
        if not os.path.exists(sFileName):
            createFolderByFile(sFileName)
        fig.savefig(sFileName, dpi=iFigDPI)

        plt.close()
        # -------------------------------------------------------------------------------------

        # -------------------------------------------------------------------------------------
        # Write ancillary file in json format (to configure png attributes)
        if os.path.exists(sFileName):
            sFileRoot = os.path.splitext(sFileName)[0]
            writeFileGraph_JSON(sFileRoot + '.json', oDischarge_MOD_OBS_META)
        # -------------------------------------------------------------------------------------

        # -------------------------------------------------------------------------------------
        # Exit with success
        oGraphStatus = True
        # -------------------------------------------------------------------------------------

    except BaseException:

        # -------------------------------------------------------------------------------------
        # Exit with warning
        Exc.getExc(
            ' =====> WARNING: graph ' + sTypeRun + ' in ' + sFileName +
            ' failed! Error(s) occurred in plot generation! ', 2, 1)
        oGraphStatus = False
        # -------------------------------------------------------------------------------------

    # -------------------------------------------------------------------------------------
    # Return code
    return oGraphStatus
Example #57
0
        # y = np.sin(t[1:] * freq / (2 * np.pi)).reshape((n_batch, output_size))

    return (X_batch, y_batch)


# print X.shape
# print y.shape

# check data
for k in range(3):
    X, y = gen_data3(k=k,
                     seq_length=seq_len,
                     seq_width=input_size,
                     batch_size=batch_size,
                     freq=0.1)
    pl.subplot(3, 1, k + 1)
    for b in xrange(len(X)):
        print "b", b
        # t = np.linspace(k*batch_size, (k+1)*batch_size+1, batch_size+1, endpoint=False)
        # t = range(k*batch_size, (k+1)*batch_size)
        # print len(X)
        t = np.arange(b * seq_len, (b + 1) * seq_len)
        print t.shape, X[b].shape
        print t.shape, y[b].shape
        pl.plot(t, X[b], "b-", label="x")
        pl.plot(t, y[b], "g-", label="y")
    pl.legend()
pl.show()

# sys.exit()
ds.config["filtering"]["y_pos min"] = 0   # the y_pos limits have to be changed if the channel was off-center
ds.config["filtering"]["y_pos max"] = 25
ds.apply_filter()  # this step is important!
ds_child = dclab.new_dataset(ds) #dataset filtered according to the set filter
ds_child.apply_filter() ### will change ds_child when the filters for ds are changed
counts = (len(ds), len(ds_child))

images_from_rtdc_dataset, images_from_rtdc_dataset_masks = framecapture_notflat(ds_child)
images_from_rtdc_dataset = images_from_rtdc_dataset[0]
images_from_rtdc_dataset_masks = images_from_rtdc_dataset_masks[0]
images_from_rtdc_dataset_segmented = images_from_rtdc_dataset * images_from_rtdc_dataset_masks


################################################ PLOTTING #######################################################
figure = plt.figure(figsize=(20,5))
ax = plt.subplot(1,5,1, xlabel = 'Area [$\mu$m$^2$]', xlim = (10,300), ylabel = 'Deformation [a.u.]', ylim = (0, 0.4))
density_scatter(ds_child["area_um"], ds_child["deform"], bins = [1000,100], ax = ax)
ax = plt.subplot(1,5,2, xlabel = 'Area [$\mu$m$^2$]', xlim = (10,120), ylabel = 'Brightness average [a.u.]', ylim = (80, 150))
density_scatter(ds_child["area_um"], ds_child["bright_avg"], bins = [1000,100], ax = ax)
ax = plt.subplot(1,5,3, xlabel = 'Area [$\mu$m$^2$]', xlim = (10,120), ylabel = 'Brightness SD [a.u.]', ylim = (0, 30))
density_scatter(ds_child["area_um"], ds_child["bright_sd"], bins = [1000,100], ax = ax)
ax = plt.subplot(1,5,4, xlabel = 'Brightness average [a.u.]', xlim = (80, 150), ylabel = 'Deformation [a.u.]', ylim = (0, 0.4))
density_scatter(ds_child["bright_avg"], ds_child["deform"], bins = [1000,100], ax = ax)
ax = plt.subplot(1,5,5, xlabel = 'Brightness average [a.u.]', xlim = (80, 150), ylabel = 'Brightness SD [a.u.]', ylim = (0, 30))
density_scatter(ds_child["bright_avg"], ds_child["bright_sd"], bins = [1000,100], ax = ax)
figure.canvas.mpl_connect('pick_event', onpick)



#%%
Example #59
0
def plot_3d(params_dir):
    N = 2  # bins for colormap
    model_dirs = [
        name for name in os.listdir(params_dir)
        if os.path.isdir(os.path.join(params_dir, name))
    ]

    colors = plt.get_cmap('plasma')
    plt.figure(figsize=(20, 10))
    ax = plt.subplot(111, projection='3d')
    ax.set_xlabel('Momentum')
    ax.set_ylabel('Learning Rate')
    ax.zaxis.set_rotate_label(False)  # disable automatic rotation
    ax.set_zlabel('Training error rate', rotation=270)
    ax.set_xticks(np.arange(0, 1.2, 0.2))
    ax.set_yticks(np.arange(0, 0.011, 0.002))
    ax.set_zticks(np.arange(0, 0.9, 0.1))
    #ax.set_xticklabels(('No', 'Yes'))
    #ax.set_zticklabels(('0','0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8'))
    ax.invert_yaxis()  # invert y axis
    ax.invert_xaxis()  # invert x axis
    #ax.view_init(azim=-178, elev=32)

    i = 0
    for model_dir in model_dirs:
        model_df = pd.DataFrame()
        for param_path in glob.glob(
                os.path.join(params_dir, model_dir) + '/*.h5'):
            param = dd.io.load(param_path)
            gd = {
                'learning rate': param['hyperparameters']['learning_rate'],
                'momentum': param['hyperparameters']['momentum'],
                'dropout': param['hyperparameters']['dropout'],
                'val. objective': param['best_epoch']['validate_objective']
            }
            model_df = model_df.append(pd.DataFrame(gd, index=[0]),
                                       ignore_index=True)
        if i != len(model_dirs) - 1:
            ax.scatter(
                model_df['momentum'],
                model_df['learning rate'],
                model_df['val. objective'],
                s=128,
                marker=(i + 3, 0),
                label=model_dir,
                #    c=model_df['val. objective'],
                c=model_df['dropout'],
                cmap=discrete_cmap(N, 'jet'))
        else:
            im = ax.scatter(
                model_df['momentum'],
                model_df['learning rate'],
                model_df['val. objective'],
                s=128,
                marker=(i + 4, 0),
                label=model_dir,
                #    c=model_df['val. objective'],
                c=model_df['dropout'],
                cmap=discrete_cmap(N, 'jet'))
        i += 1

    cbar = plt.colorbar(im, label='Dropout', ticks=range(N))
    cbar.ax.set_yticklabels(['No', 'Yes'])
    cbar.set_label('Dropout', rotation=270)
    #plt.legend()
    plt.title('Adult dataset', weight='bold')
    plt.show()
    plt.savefig('{}.eps'.format(
        os.path.join(IMAGES_DIRECTORY, 'params3d_adult')),
                format='eps',
                dpi=1000)
    plt.close()
Example #60
0
def run(config_path):
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    config = neat.Config(neat.iznn.IZGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         config_path)

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = neat.population.Population(config)

    # Add a stdout reporter to show progress in the terminal.
    pop.add_reporter(neat.StdOutReporter(True))
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)

    pe = neat.ParallelEvaluator(multiprocessing.cpu_count(), eval_genome)
    winner = pop.run(pe.evaluate, 3000)

    # Display the winning genome.
    print('\nBest genome:\n{!s}'.format(winner))

    node_names = {-1:'A', -2: 'B'}
    visualize.draw_net(config, winner, True, node_names=node_names)
    visualize.plot_stats(stats, ylog=False, view=True)
    visualize.plot_species(stats, view=True)

    # Show output of the most fit genome against training data, and create
    # a plot of the traces out to the max time for each set of inputs.
    print('\nBest network output:')
    plt.figure(figsize=(12, 12))
    sum_square_error, simulated = simulate(winner, config)
    for r, (inputData, outputData, t0, t1, v0, v1, neuron_data) in enumerate(simulated):
        response = compute_output(t0, t1)
        print("{0!r} expected {1:.3f} got {2:.3f}".format(inputData, outputData, response))

        axes = plt.subplot(4, 1, r + 1)
        plt.title("Traces for XOR input {{{0:.1f}, {1:.1f}}}".format(*inputData), fontsize=12)
        for i, s in neuron_data.items():
            if i in [0, 1]:
                t, I, v, u, fired = zip(*s)
                plt.plot(t, v, "-", label="neuron {0:d}".format(i))

        # Circle the first peak of each output.
        circle0 = patches.Ellipse((t0, v0), 1.0, 10.0, color='r', fill=False)
        circle1 = patches.Ellipse((t1, v1), 1.0, 10.0, color='r', fill=False)
        axes.add_artist(circle0)
        axes.add_artist(circle1)

        plt.ylabel("Potential (mv)", fontsize=10)
        plt.ylim(-100, 50)
        plt.tick_params(labelsize=8)
        plt.grid()

    plt.xlabel("Time (in ms)", fontsize=10)
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    plt.savefig("traces.png", dpi=90)
    plt.show()