Exemplo n.º 1
0
    def plot_filter_characteristics(self):
        w, h = freqz(self.freq_filter.num, self.freq_filter.denom)
        plt.figure(1)
        plt.subplot(2,1,1)
        plt.hold(True)
        powa = plt.plot((self.filter_parameters.sample_rate*0.5/pi)*w, abs(h),'b-', label = 'Char. amplitudowa')
        plt.title('Charakterystyki filtru')
        plt.xlabel('Czestotliwosc [Hz]')
        plt.ylabel('Amplituda')


        plt.twinx(ax=None)
        angles = unwrap(angle(h))
        plt.znie = plot((self.filter_parameters.sample_rate*0.5/pi)*w,angles, 'g-', label = 'Char. fazowa')
        plt.ylabel('Faza')

        plt.grid()
        tekst = powa + znie
        wybierz = [l.get_label() for l in tekst]

        plt.legend(tekst, wybierz, loc='best')
    ########################################################################################################################
        plt.subplot(2,1,2)

        w2, gd = group_delay((num, denom))

        plt.plot((sample_rate*0.5/pi)*w2, gd)
        plt.grid()
        plt.xlabel('Czestotliwosc [Hz]')
        plt.ylabel('Opoznienie grupowe [probki]')
        plt.title('Opoznienie grupowe filtru')

        plt.show()
Exemplo n.º 2
0
def log_plot():
    start = int(np.floor(np.log(min(median)) / np.log(10))) + 3
    end = int(np.ceil(np.log(max(median)) / np.log(10))) + 3

    xs = []
    ticks = []
    for i in range(start, end + 1):
        xs.append(10 ** (i - 3))

        if i % 3 == 0:
            ticks.append('{}s'.format(prefix[i / 3]))
        else:
            ticks.append(str(10 ** (i % 3)))

    plt.barh(pos, median, align='center', height=0.25, left=1e-3,
             color=bar_color, lw=0)
    plt.errorbar(median, pos, ecolor=error_bar_color, fmt=None, xerr=err)

    plt.grid(True)
    plt.xlabel('Time')
    plt.xlim(min(xs), max(xs))
    plt.xscale('log')
    plt.xticks(xs, ticks)
    plt.ylim(ymax=size)
    plt.yticks(pos, language)

    plt.twinx()
    plt.ylim(ymax=size)
    plt.yticks(pos, relative)

    plt.savefig('plots/{}.png'.format(pid), bbox_inches='tight')
    plt.clf()
Exemplo n.º 3
0
Arquivo: util.py Projeto: sao-eht/eat
def timeseries(bs, dt=1):
    if not hasattr(bs, '__iter__'):
        bs = [bs,]
    nrow = len(bs)
    for (i, b) in enumerate(bs):
        p = params(b)
        plt.subplot(nrow, 1, 1+i)
        v = pop212(b).sum(axis=1) # stack over channels
        nt = len(v)
        dt = min(dt, nt)
        nt = nt - np.fmod(nt, dt) # fit time segments after decimation
        v = v[:nt].reshape((nt/dt, -1)).sum(axis=1) # clip to multiple of dt and stack
        t = p.dtvec[:nt].reshape((-1, dt)).mean(axis=1) + p.T/2.
        amp = np.abs(v)
        phase = np.angle(v)
        plt.plot(t, amp, 'b.-')
        plt.ylim(0, plt.ylim()[1])
        plt.gca().set_yticklabels([])
        plt.twinx()
        plt.plot(t, phase, 'r.-')
        plt.ylim(-np.pi, np.pi)
        plt.gca().set_yticklabels([])
        putil.rmgaps(1e6, 2.0)
        plt.xlim(0, p.T)
        plt.gca().add_artist(AnchoredText(p.baseline, loc=1, frameon=False, borderpad=0))
    plt.setp(plt.gcf(), figwidth=8, figheight=2+nrow)
    plt.tight_layout()
    plt.subplots_adjust(hspace=0)
Exemplo n.º 4
0
def plt_twin( axis, tick0=None, tick=None ) : 
	'''
	Add x-top or y-right axis

	axis:
		['x' | 'y']

	tick0:
		Must between [0, 1]
	'''
	if (str(axis).lower() not in ['x', 'y']) : Raise(Warning, "axis='"+str(axis)+"' not in ['x', 'y']. Do nothing !")
	axis = str(axis).lower()
	#--------------------------------------------------
	if (tick0 is not None and tick is not None) : 
		tick0 = npfmt(tick0, float)
		if (tick0.min()<0 or tick0.max()>1) : 
			Raise(Warning, 'tick0.(min,max)=(%.1f, %.1f) out of [0, 1]. Do nothing !' % (tick0.min(), tick0.max()))
		else : 
			if   (axis == 'x') : 
				plt.twiny()
				plt.xticks(tick0, tick)
			elif (axis == 'y') : 
				plt.twinx()
				plt.yticks(tick0, tick)
	#--------------------------------------------------
	elif (tick0 is None and tick is None) : 
		if   (axis == 'x') : plt.tick_params(axis='x', which='both', labeltop='on', labelbottom='on')
		elif (axis == 'y') : plt.tick_params(axis='y', which='both', labelleft='on', labelright='on')
	#--------------------------------------------------
	else : Raise(Warning, 'tick0, tick must both ==None or !=None, now one is None but the other is not. Do nothing !')
Exemplo n.º 5
0
 def pcs(self):
     self.pesobj = PES.PES(self.X,self.Y,self.S,self.D,self.lb.flatten(),self.ub.flatten(),self.para['kindex'],self.para['mprior'],self.para['sprior'],DH_SAMPLES=self.para['DH_SAMPLES'], DM_SAMPLES=self.para['DM_SAMPLES'], DM_SUPPORT=self.para['DM_SUPPORT'],DM_SLICELCBPARA=self.para['DM_SLICELCBPARA'],mode=self.para['SUPPORT_MODE'])
     xmin = self.reccomend()
     plt.figure(1)
     plt.plot(xmin[0],xmin[1],'r.')
     print xmin
     plt.figure(2)
     plt.subplot(4,1,1)
     ns = 6000
     sup = sp.linspace(-1,1,ns)
     for i in xrange(2):
         X = sp.vstack([xmin for k in xrange(ns)])
         print X.shape
         for j in xrange(ns):
             X[j,i] = sup[j]
         [m,v] = self.pesobj.G.infer_diag_post(X,[[sp.NaN]]*ns)
         s = sp.sqrt(v)
         plt.subplot(4,1,2*i+1)
         plt.fill_between(sup,(m-2*s).flatten(),(m+2*s).flatten(), facecolor='lightblue',edgecolor='lightblue')
         plt.plot(sup,m.flatten())
         [m,v] = self.pesobj.G.infer_diag_post(X,[[i]]*ns)
         s = sp.sqrt(v)
         plt.subplot(4,1,2*i+2)
         plt.fill_between(sup,(m-2*s).flatten(),(m+2*s).flatten(), facecolor='lightblue',edgecolor='lightblue')
         plt.plot(sup,m.flatten(),'r')
         p = sp.exp(-0.5*(m**2)/v)
         
         
         
         plt.twinx().plot(sup,p.flatten(),'g')
     return
Exemplo n.º 6
0
def main():

    # switch the commented lines here to alternate between CV testing and making kaggle submission
    x_train, x_test, y_train, y_test = load_data_cv('data/train_32.npy')
    #x_train, y_train, x_test = load_data_test('data/train_32.npy', 'data/test_32.npy')

    model = build_model()

    print("Starting training")
    # batch iterator with 300 epochs
    train_loss = []
    valid_loss = []
    valid_acc = []
    for i in range(300):
        loss = batch_iterator(x_train, y_train, 128, model)
        train_loss.append(loss)
        valid_avg = model.evaluate(x_test, y_test, show_accuracy = True, verbose = 0)
        valid_loss.append(valid_avg[0])
        valid_acc.append(valid_avg[1])
        print 'epoch:', i, 'train loss:', np.round(loss, decimals = 4), 'valid loss:', np.round(valid_avg[0], decimals = 4), 'valid acc:', np.round(valid_avg[1], decimals = 4)

    train_loss = np.array(train_loss)
    valid_loss = np.array(valid_loss)
    valid_acc = np.array(valid_acc)
    sns.set_style("whitegrid")
    pyplot.plot(train_loss, linewidth = 3, label = 'train loss')
    pyplot.plot(valid_loss, linewidth = 3, label = 'valid loss')
    pyplot.legend(loc = 2)
    pyplot.ylim([0,4.5])
    pyplot.twinx()
    pyplot.plot(valid_acc, linewidth = 3, label = 'valid accuracy', color = 'r')
    pyplot.grid()
    pyplot.ylim([0,1])
    pyplot.legend(loc = 1)
    pyplot.show()
def fidelity_vs_power(folder='', x_axis_par='par_ro_Ex_power'):
    
    if folder == '':
        folder = os.getcwd()

    allfiles = os.listdir(folder)
    fidfiles = [f for f in allfiles if ('totalfid' in f and '.dat' in f)]

    x_axis = [] #in this case powers
    maxfid = []
    maxfid_t = []

    for f in fidfiles:
        fn, ext = os.path.splitext(f)
        idx = int(fn[fn.find('+_')+2:])
        basepath = os.path.join(folder, PREFIX+'-'+str(idx))
        parfile = basepath+'_'+PARAMS_SUFFIX+'.dat'  
        x_axis.append(loadtxt(parfile)[get_param_column(parfile,x_axis_par)]*1e9) #pwr in nW
        
        fiddat = loadtxt(f)
        maxidx = argmax(fiddat[1,:])
        maxfid.append(fiddat[1,maxidx])
        maxfid_t.append(fiddat[0,maxidx])
    
    
    fig = plt.figure()
    plt.plot(x_axis, maxfid, 'ro', label='max F')
    plt.xlabel('P [nW]')
    plt.ylabel('max. F')
    plt.legend()
    
    plt.twinx()
    plt.plot(x_axis, maxfid_t, 'bo')
    plt.ylabel('best ro-time')
    plt.savefig('fidelity_vs_power.png')
Exemplo n.º 8
0
def laserPlot(filenameLst):
	'''
	Plot the temporal and spectral profile of a
	mode-locked fiber laser simulation
	'''

	nbrPlots = len(filenameLst)
	for i in arange(len(filenameLst)):
		results = load(filenameLst[i])
		t = results['t']
		nt = results['nt']
		T = results['T']
		archivePass = results['archivePass']
		nu_inst_out3 = results['nu_inst_out3']
		nu_inst_out4 = results['nu_inst_out4']
		spectre_out = results['spectre_out']
		wavelength = results['wavelength']


		# Graph
		plt.figure(figsize=(12,9))

		ax3 = plt.subplot(221)
		plt.plot(t, pow(abs(archivePass[0]),2), color="black")
		plt.ylabel("$|u(z,T)|^2$ [W]")
		plt.xlabel("$T/T_0$")
		plt.xlim([-T/2,T/2])
		plt.grid(True)
		ax4 = plt.twinx()
		plt.plot(t[0:nt-1], nu_inst_out3)
		plt.ylabel("Chirp")
		ax4.yaxis.tick_right()
		plt.ylim([-1.5,1.5])

		ax5 = plt.subplot(223)
		plt.semilogy(t, pow(abs(archivePass[0]),2), color="black")
		plt.ylabel("$|u(z,T)|^2$ [dBm]")
		plt.xlabel("$T/T_0$")
		plt.xlim([-T/2,T/2])
		plt.grid(True)
		ax4 = plt.twinx()
		plt.plot(t[0:nt-1], nu_inst_out3)
		plt.ylabel("Chirp")
		ax4.yaxis.tick_right()
		plt.ylim([-1.5,1.5])

		ax7 = plt.subplot(222)
	 	plt.plot(wavelength, spectre_out, color="black")
		plt.xlabel("Wavelength [nm]")
		plt.grid(True)

		ax8 = plt.subplot(224)
	 	plt.semilogy(wavelength, spectre_out, color="black")
		plt.xlabel("$T/T_0$")
		plt.xlabel("Wavelength [nm]")
		plt.grid(True)

	plt.show()
def makeFig():
    plt.title('recycle')
    plt.plot(xArray, '-ro', label='paper')
    plt.legend(loc='upper left')
    plt2=plt.twinx()
    plt2.plot(yArray, 'b^-', label='plastic')
    plt2.legend(loc='upper center')
    plt3=plt.twinx()
    plt3.plot(zArray, 'gD-', label='can')
    plt3.legend(loc='lower left')
Exemplo n.º 10
0
def plot_label_by_success(setup_name, school, context_name=None, term_type=None, legend=True, linetype='-', show_data_size=True, set_order=None):
    data = load_ratings_with_contexts()
    if set_order is not None:
        data = data[data['practice_set_order'] == set_order]
    data = data[(data['experiment_setup_name'] == setup_name)]
    if context_name is not None:
        data = data[data['context_name'] == context_name]
    if term_type is not None:
        data = data[data['term_type'] == term_type]
    if school is not None:
        school_usage = load_school_usage().reset_index().rename(columns={'ip_address': 'school', 'user_id': 'user'})
        data = pandas.merge(data, school_usage, on='user', how='inner')
        data = data[data['school'] == school]
    data = data[data['error_rate'].apply(lambda x: x % 10 == 0)]

    def _apply(group):
        result = []
        for label in group['label'].unique():
            mean = binomial_confidence_mean(group['label'] == label)
            result.append({
                'label': label,
                'learners': 100 * mean[0],
                'learners_min': 100 * mean[1][0],
                'learners_max': 100 * mean[1][1],
            })
        return pandas.DataFrame(result)
    to_plot = data.groupby(['experiment_setup_name', 'error_rate']).apply(_apply).reset_index().sort_values(by=['label', 'error_rate'])
    for i, (label, label_data) in enumerate(to_plot.groupby('label')):
        plt.plot(
            label_data['error_rate'],
            label_data['learners'],
            linetype,
            label=label.split('-')[-1],
            color=output.palette()[i],
            marker='.',
            markersize=20
        )
        plt.fill_between(
            label_data['error_rate'],
            label_data['learners_min'],
            label_data['learners_max'],
            color=output.palette()[i], alpha=0.35
        )
    if legend:
        plt.legend(ncol=3, loc='upper left', frameon=True)
    plt.ylabel('Label (%)')
    plt.xlabel('Real error rate')
    plt.gca().xaxis.grid(True)
    plt.gca().yaxis.grid(True)
    if show_data_size:
        plt.twinx()
        size = data.groupby('error_rate').apply(len).reset_index().rename(columns={0: 'size'})
        plt.plot(size['error_rate'], size['size'], '.-', color='gray')
        plt.ylabel('Data size')
    plt.ylim(0, 70)
Exemplo n.º 11
0
def trialPlot(dm, soa, _show=show, err=True, minSmp=200, suffix='', padding=0,
	diff=True):

	"""
	A pupil-trace plot for the full trial epoch.

	Arguments:
	dm				--	A DataMatrix.
	soa				--	The SOA to select.

	Keyword arguments:
	_show			--	Indicates whether the plot should be shown.
						(default=True)
	err				--	Indicates whether error bars should be drawn.
						(default=True)
	suffix			--	A suffix to identify the trace. (default='')
	padding			--	A padding time to be added to the traceLen. (default=0)
	diff			--	Indicates whether the difference trace should be plotted
						as well. (default=True)
	"""

	assert(soa in dm.unique('soa'))
	if _show:
		Plot.new(size=Plot.ws)
		plt.title('SOA: %d ms' % (soa+55))
	plt.axhline(1, linestyle='--', color='black')
	dm = dm.select('soa == %d' % soa)
	# Determine the trace length and create the trace plot
	traceLen = soa + 105 + padding
	traceParams = trialParams.copy()
	traceParams['traceLen'] = traceLen
	tracePlot(dm, traceParams=traceParams, err=err, suffix='.%d%s' % (soa, \
		suffix), minSmp=minSmp)
	# Cue
	plt.axvspan(0, cueDur, color=blue[1], alpha=.2)
	# Target. Take into account to cue duration in determining the target onset.
	targetOnset = soa+55
	plt.axvspan(targetOnset, targetOnset+targetDur, color=blue[1], alpha=.2)
	plt.xlim(0, 2550)
	plt.legend(frameon=False)
	plt.xlabel('Time since cue onset (ms)')
	plt.ylabel('Pupil size (norm.)')
	plt.yticks([1,1.025, 1.05])
	plt.xticks(range(0, 2501, 500))
	if diff:
		plt.ylim(.92, 1.07)
		plt.axhline(diffY, linestyle='--', color='black')
		plt.twinx()
		plt.tick_params(axis="y")
		plt.ylim(.92, 1.07)
		plt.yticks([.925,.95, .975], [-.025, 0, .025])
	else:
		plt.ylim(.98, 1.07)
	if _show:
		Plot.save('trialPlot.%d' % soa, 'trialPlot', show=show)
def fidelity_vs_power(folder='',sweep_param='Ex_RO_amplitude'):
    
    if folder == '':
        folder = os.getcwd()

    allfiles = os.listdir(folder)
    fidfiles = [f for f in allfiles if ('totalfid' in f and '.dat' in f)]

    pow = []
    maxfid = []
    maxfid_t = []

    
    for f in fidfiles:
        fn, ext = os.path.splitext(f)
        idx = int(fn[fn.find('+_')+2:])-1
        print idx
        
        if idx < 10:
            SUFFIX = '-00'+str(idx)
        elif idx == 10:
            SUFFIX = '-0'+str(idx)
        elif idx < 100:
            SUFFIX = '-0'+str(idx)

        basepath = os.path.join(folder, PREFIX+SUFFIX)
        parfile = basepath+'_parameters_dict.npz'  
        param_dict=load(parfile)
        #pow.append(loadtxt(parfile)[get_param_column(parfile,'par_ro_Ex_power')]*1e9)
        #pow.append(int(idx/2)*1.)
        pow.append(param_dict[sweep_param])
        param_dict.close
        fiddat = loadtxt(f)
        maxidx = argmax(fiddat[1,:])
        maxfid.append(fiddat[1,maxidx])
        maxfid_t.append(fiddat[0,maxidx])
        #fiddat.close() 
    
    fig = plt.figure()
    plt.plot(pow, maxfid, 'ro', label='max F')
    plt.xlabel('P [nW]')
    plt.ylabel('max. F')
    plt.ylim(ymax=1.2*max(maxfid))
    plt.title('Maximum SSRO Fidelity and Optimal readout time vs' + sweep_param)
    plt.text(0.01*(max(pow)+min(pow)),1.15*max(maxfid),folder,fontsize='x-small')
    plt.legend()
    
    plt.twinx()
    plt.plot(pow, maxfid_t, 'bo')
    plt.ylabel('best ro-time')
    plt.ylim(ymax=1.2*max(maxfid_t))
    plt.savefig('fidelity_vs_power.png')
	def sampleExponentials(self, n_samples = 1000):
		la = np.load("results/fitDistLog.npy")
		x = np.linspace(0, 1, 100)
		av = np.average(la[:, :2], axis=0)
		cov = np.cov(la[:, :2].T)
		s = np.random.multivariate_normal(av, cov, size=n_samples)
		vals = s[:, 0]*np.exp(np.outer(x, s[:, 1]))
		print vals.shape
		bins, _, _ = self.getBinnedData(2, 100)
		'''for i in xrange(100):
			if len(bins[i]) != 0:
				plt.hist(bins[i], bins=50, alpha=0.6, normed=True)
			plt.hist(vals[i], bins=50, alpha=0.6, color='r', normed=True)
			plt.savefig("results/comparefullhist{0:03d}.pdf".format(i))
			plt.clf()'''
		plt.subplot(231)
		hist, xedges, yedges = np.histogram2d(la[:, 0], la[:, 1], bins=20, normed=True)
		plt.imshow(hist.T, interpolation = 'nearest', cmap=cm.Blues, aspect = 'auto',
				extent=[np.min(xedges), np.max(xedges), np.min(yedges), np.max(yedges)],
				origin='lower')
		plt.subplot(232)
		samples = np.random.multivariate_normal(av, cov, size=10*n_samples)
		hist_sp, xedges_sp, yedges_sp = np.histogram2d(samples[ :, 0], 
									samples[ :, 1], bins=50, normed=True)
		plt.imshow(hist_sp.T, interpolation = 'bicubic', cmap=cm.Reds, aspect = 'auto',
				extent=[np.min(xedges_sp), np.max(xedges_sp), np.min(yedges_sp), np.max(yedges_sp)],
				origin='lower')
		plt.subplot(233)
		plt.pcolor(np.linspace(np.min(xedges), np.max(xedges), len(xedges)),
				np.linspace(np.min(yedges), np.max(yedges), len(yedges)), hist.T,
				alpha=0.8, cmap=cm.Blues)
		plt.pcolor(np.linspace(np.min(xedges_sp), np.max(xedges_sp), len(xedges_sp)),
				np.linspace(np.min(yedges_sp), np.max(yedges_sp), len(yedges_sp)), hist_sp.T,
				alpha=0.5, cmap=cm.Reds)
		plt.subplot(234)
		plt.hist2d(la[:, 0], la[:, 1], bins=20, cmap=cm.Blues)
		plt.subplot(235)
		_, bx, _ = plt.hist(la[:, 1], 20, alpha=0.6, normed=True)
		plt.hist(samples[:, 1], 20, color='r', alpha=0.6, normed=True)
		xp = np.linspace(np.min(bx), np.max(bx), 100)
		p = scipy.stats.norm.pdf(xp, loc=av[1], scale=np.sqrt(cov[1,1]))
		plt.plot(xp, p)
		plt.subplot(236)
		nx, bx, _ = plt.hist(la[:, 0], 20, alpha=0.6, normed=True)
		plt.hist(samples[:, 0], 20, color='r', alpha=0.6, normed=True)
		xp = np.linspace(np.min(bx), np.max(bx), 100)
		p = scipy.stats.norm.pdf(xp, loc=av[0], scale=np.sqrt(cov[0,0]))
		plt.plot(xp, p)
		plt.twinx()
		plt.plot(bx[1:]-np.diff(bx), np.cumsum(nx)*np.diff(bx), color='g')
		plt.savefig("results/comparehistdist.pdf")
		plt.clf()
def makeFig(): #Create a function that makes the desired plot
    gs = gridspec.GridSpec(3, 3) #gridspec is created 3x3
    #First plot fig 1
    #Plot 1
    plt.subplot(gs[0, :])                                   #subplot position atributes
    plt.ylim([-30,50])                                      #Set y min and max values
    plt.title('Temperatura em Graus C')                     #Plot the title
    plt.grid(True)                                          #Turn on the grid
    plt.ylabel('Temp-1 c')                                  #Set ylabels
    plt.plot(tempF, 'ro-', label='temperatura em graus')    #plot the temperature
    plt.legend(loc='upper left')                            #plot the legend
    plt2=plt.twinx()                                        #Create a second y axis
    plt.ylim(-30,50)                                        #Set limits of second y axis- adjust to readings you are getting
    plt2.plot(tempF2, 'b^-', label='Temp-2 c')              #plot temperature array
    plt2.set_ylabel('Temp-2 c')                             #label second y axis
    plt2.ticklabel_format(useOffset=False)                  #Force matplotlib to NOT autoscale y axis
    plt2.legend(loc='upper right')                          #plot the legend
    #second plot same figure (same window)
    #Plot 2
    plt.subplot(gs[1, :])                                   #subplot position attributes
    plt.ylim([0,100])                                       #Set y min and max values
    plt.title('Humidade do Ar em Percentagem')              #Plot the title
    plt.grid(True)                                          #Turn on the grid
    plt.ylabel('Himidade-1 %')                              #Set ylabels
    plt.plot(humF1, 'ro-', label='Humidade')                #plot the temperature
    plt.legend(loc='upper left')                            #plot the legend
    plt2=plt.twinx()                                        #Create a second y axis
    plt.ylim(0,100)                                         #Set limits of second y axis- adjust to readings you are getting
    plt2.plot(humF2, 'b^-', label='Humidade-2 %')           #plot temperature array
    plt2.set_ylabel('Humidade-2 %')                         #label second y axis
    plt2.ticklabel_format(useOffset=False)                  #Force matplotlib to NOT autoscale y axis
    plt2.legend(loc='upper right')                          #plot the legend
    #Third plot same figure (same window)
    #Plot 3
    plt.subplot(gs[-1,0])                                   #subplot position atributes
    plt.ylim([0,100])                                       #Set y min and max values
    plt.title('Humidade do solo')                           #Plot the title
    plt.grid(True)                                          #Turn on the grid
    plt.ylabel('Himidade %')                                #Set ylabels
    plt.plot(moist, 'ro-', label='Humidade')                #plot the temperature
    plt.legend(loc='upper left')                            #plot the legend
    #Fourth plot same figure (same window)
    #Plot 4
    plt.subplot(gs[-1,-1])                                  #subplot position atributes
    plt.ylim([0,2000])                                      #Set y min and max values
    plt.title('Luminosidade')                               #Plot the title
    plt.grid(True)                                          #Turn on the grid
    plt.ylabel('Luminosidade (lux)')                        #Set ylabels
    plt.plot(lum, 'ro-', label='Luminosidade')              #plot the temperature
    plt.legend(loc='upper left')                            #plot the legend
Exemplo n.º 15
0
 def plotsigma(self,emit=2.5e-6/7000*0.938,deltap=1.1e-4,**nargs):
   self.sigx =sqrt(self.betx*emit)*1000
   self.sigy =sqrt(self.bety*emit)*1000
   self.sigdx=self.dx*deltap*1000
   self.plot('sigx sigy sigdx',**nargs)
   ya,yb=pl.ylim()
   pl.twinx()
   bmax=max(self.betx.max(),self.bety.max())
   rng=range(0,int(_n.ceil(_n.log10(bmax)))+1)
   bval=_n.array([n*10**dd for dd in rng for n in [1,2,5] ])
   bval=bval[bval<bmax]
   pl.ylim(ya,yb)
   self._plot=_p.gcf()
   return t
Exemplo n.º 16
0
    def payload_loss_figure(self):
        ps = []
        rs = []
        rs2 = []
        Omegas = [i * 0.001 for i in range(1, 201)]
        last_p = 0
        for Omega in Omegas:
            self.udp.Omega = Omega
            p, r = self.udp.get_payload_size()
            if p != last_p:
                print "Omega=%s, p=%s, r=%s" % (Omega, p, r)
                last_p = p
            ps.append(p)
            rs.append(r)

            r2 = self.udp.get_ratio_lower(p=4096)
            rs2.append(r2 / r)

        plt.clf()
        plt.cla()
        l, = plt.plot(Omegas, ps)
        plt.legend([l], ["Optimal\nPayload Size"], loc="upper left", prop={"size": 18})
        # print ps
        # plt.ylim(ymin=0.0, ymax=1)
        plt.ticklabel_format(style="sci", axis="x", scilimits=(3, 3))
        plt.ticklabel_format(style="sci", axis="y", scilimits=(-2, 0))

        plt.grid(True)
        plt.xlabel("Packet Loss Rate")
        plt.ylabel("Optimal Payload Size per Chunk")

        plt.twinx()
        #         l1, = plt.plot(Omegas, rs, "g")
        #         l2, = plt.plot(Omegas, rs2, "r")
        #         plt.ylabel("Max Goodput-to-Throughput Ratio")
        #         plt.legend([l1, l2], ["Max G2T Ratio", "Ratio of 4096-to-LB"], loc="upper right",  prop={'size':18})
        #
        l1, = plt.plot(Omegas, rs, "g")
        # l2, = plt.plot(Omegas, rs2, "r")
        plt.ylabel("Max Goodput-to-Throughput Ratio")
        plt.legend([l1], ["Max G2T Ratio"], loc="upper right", prop={"size": 18})

        plt.title("UDP: $\Delta$=%s, M=%s" % (self.udp.Delta, self.udp.M))

        name = "payload-loss-M%s-Delta%s" % (self.udp.M, self.udp.Delta)
        name.replace(".", "")
        plt.savefig(self.udp.out_dir + "/" + name + ".pdf")

        print "%s.pdf ends" % (name)
Exemplo n.º 17
0
def agdd_plots(nplots, iplot, tbase, tmax, t_range, temp, agdd):
    """This function does the AGDD plots in a nplots vertical stack of plots.
    iplot is the current plot (from top to bottom, starting at 1), tbase and
    tmax are the values used for AGDD calculations. t_range is a temporal range
    (usually DoY) and temp and AGDD are extracted 2m temperature and AGDD"""
    plt.subplot(nplots, 1, iplot)
    # Put a grey area for the AGDD calculation bounds
    plt.axhspan(tbase, tmax, xmin=0, xmax=366, color="0.9")
    # Plot temperature
    plt.plot(t_range, temp, "-r", label="Tm")
    plt.ylabel("Mean Temp [degC]")
    plt.grid(True)
    plt.twinx()
    plt.plot(t_range, agdd, "-g", label="AGDD")
    plt.ylabel("AGDD [degC]")
Exemplo n.º 18
0
Arquivo: curve.py Projeto: ralesi/rex
    def ex_twinplot(self, X, Y, **kwargs):
#        go_with_x()
#        ax = gca()

        # If type is tuple, use secondary verticle axis, otherwise plot multiple values on same scale
        if type(Y) == tuple:
            if len(Y)>2:
                raise Exception, "twinx plots only takes two arguments"
            else:
                for i in range(len(Y)):
                    self.ex_plot(X, Y[i], **kwargs)
                    twinx()
        else:
            for i in range(len(Y)):
                self.ex_plot(X, Y[i], **kwargs)
Exemplo n.º 19
0
def plot_data(lines, save_loc = ["plot_output.pdf",], **kwargs):
    '''Plots a series of data lines
    Each line is (x_list, y_list, properties_dict)
    '''
    fig = pyplot.figure()

    #Set up legend area
    if kwargs.get('legend_outside'): 
        ax = fig.add_axes([0.1, 0.1, 0.55, 0.8])
    else: 
        ax = fig.add_subplot(111)

    if kwargs.get('plot_title'): pyplot.title(kwargs.get('plot_title'))

    legend_text = []; legend_lines = []

    #Plot passed in points.
    for line in lines:

        title = line[2].pop('title', None)
        title_on_plot = line[2].pop('title_on_plot', False)

        plot_function = ax.loglog if kwargs.get('log_plot', None) else ax.plot
        plotted = plot_function(line[0],line[1],**(line[2] if len(line) > 2 else {}))

        if title and not title_on_plot:
            legend_lines.append(plotted)
            legend_text.append(title)

    for t in kwargs.get('text_labels', []):
        ax.text(**t)

    if kwargs.get('axis_v'): ax.axis(kwargs.get('axis_v'))
    elif kwargs.get('zero_yaxis'): 
        ax.set_ylim((0.0, ax.get_ylim()[1]))

    if kwargs.get('x_tick_freq'): ax.xaxis.set_major_locator(MultipleLocator(kwargs.get('x_tick_freq')))
    if kwargs.get('y_tick_freq'): ax.yaxis.set_major_locator(MultipleLocator(kwargs.get('y_tick_freq')))

    if kwargs.get('x_tick_func'): ax.xaxis.set_major_formatter(FuncFormatter(kwargs.get('x_tick_func')))
    if kwargs.get('y_tick_func'): ax.yaxis.set_major_formatter(FuncFormatter(kwargs.get('y_tick_func')))
       
    if legend_lines:
        if kwargs.get('legend_outside'): fig.legend(legend_lines, legend_text, loc = kwargs.get('legend_placement', 'best'), numpoints=1)
        else: ax.legend(legend_lines, legend_text, loc = kwargs.get('legend_placement', 'best'), numpoints=1)

    if kwargs.get('x_title'): ax.set_xlabel(kwargs.get('x_title'))
    if kwargs.get('y_title'): ax.set_ylabel(kwargs.get('y_title'))

    #Draw a second scale
    if kwargs.get('y2_transform') is not None:
        ax2 = pyplot.twinx(ax = ax)
        ax2.yaxis.set_view_interval(kwargs.get('y2_transform')(ax.yaxis.get_view_interval()[0]), kwargs.get('y2_transform')(ax.yaxis.get_view_interval()[1]))
        if kwargs.get('y2_title'): ax2.yaxis.set_label_text(kwargs.get('y2_title'))

    if save_loc is not None:
        for l in save_loc:
            pyplot.savefig(l, format=os.path.splitext(l)[1][1:])

    if kwargs.get('show_plot'): show()
Exemplo n.º 20
0
def plot_mfc_budget(mfc_budget, index, year, legend=True,
                    legend_kw={'fontsize' : 9, 'loc' : 'upper left',
                               'handlelength' : 2.5},
                    dashes=[6, 2], netprecip=False):
    ts = mfc_budget.sel(year=year)
    ind = index.sel(year=year)
    days = ts['day'].values
    styles = {'PRECTOT' : {'color' : 'k', 'linestyle' : '--', 'dashes' : dashes},
              'EVAP' : {'color' : 'k'},
              'MFC' : {'color' : 'k', 'linewidth' : 2},
              'dw/dt' : {'color' : '0.7', 'linewidth' : 2}}
    if netprecip:
        styles['P-E'] = {'color' : 'b', 'linewidth' : 2}
    for nm in styles:
        plt.plot(days, ts[nm], label=nm, **styles[nm])
    plt.axvline(ind['onset'], color='k')
    plt.axvline(ind['retreat'], color='k')
    plt.xlabel('Day of Year')
    plt.ylabel('mm/day')
    ax1 = plt.gca()
    ax2 = plt.twinx()
    plt.sca(ax2)
    plt.plot(days, ind['tseries'], 'r', alpha=0.6, linewidth=2, label='CMFC')
    atm.fmt_axlabels('y', 'mm', color='r', alpha=0.6)
    if legend:
        atm.legend_2ax(ax1, ax2, **legend_kw)
    return ax1, ax2
Exemplo n.º 21
0
def hist_plot_two_y(m, n, p, mv, nv, pv, filename):
    x = np.arange(0,len(m))
    ax = plt.subplot(111)
    common_params = dict(bins=len(m), range=(0,len(m)), normed=False)

    patterns = ('//', '', '\\')
    _, _, patches = plt.hist((x,x,x), weights=(m,n,p), \
            label=["Autocorrelation", "HOS", "PCS-based HOS"], **common_params)
    for patch,pattern in zip(patches, patterns):
        [k.set_hatch(pattern) for k in patch]

    ax2 = plt.twinx()
    ax2.plot(map(lambda i:i+0.5, x), np.array(mv)/np.array(m), label="Autocorrelation", linestyle=':', color='b', marker='v', linewidth=1.5)
    ax2.plot(map(lambda i:i+0.5, x), np.array(nv)/np.array(n), label="HOS", linestyle='-.', color='g', marker='^', linewidth=1.5)
    ax2.plot(map(lambda i:i+0.5, x), np.array(pv)/np.array(p), label="PCS-based HOS", linestyle='-', color='r', marker='s', linewidth=1.5)
    formatter = FuncFormatter(to_percent)
    ax2.yaxis.set_major_formatter(formatter)
    ax2.yaxis.label.set_fontsize(15)
    ax2.set_ylabel("Ratio between variance and amplitude", labelpad=10)

    names = ["lag#0","lag#1","lag#2","lag#3","lag#4","lag#5","lag#6"]
    ax.xaxis.label.set_fontsize(15)
    ax.yaxis.label.set_fontsize(15)
    ax.set_xticks([k+0.5 for k in range(7)])
    #ax.set_xticklabels(names,rotation=30, rotation_mode="anchor", ha="right")
    ax.set_xticklabels(names)
    ax.set_ylabel("Amplitude of cumulants", labelpad=10)
    ax.set_xlabel("Lags of the model", labelpad=10)
    plt.tight_layout()
    plt.grid(axis='y')
    plt.legend(loc=9, fancybox=True, shadow=True)

    plt.savefig(filename, format='pdf')
    plt.show()
Exemplo n.º 22
0
def assess_flux_stability(samplename='Glassy_Carbon'):
    ip = get_ipython()
    f = plt.figure()
    ax1 = f.add_subplot(1, 1, 1)
    plt.xlabel('Date of exposure')
    plt.ylabel('Beam flux (photon/sec), continuous lines')
    ax2 = plt.twinx()
    plt.ylabel('Vacuum pressure (mbar), dotted lines')
    plt.title('Beam flux stability')
    samplenames = sorted([sn_ for sn_ in ip.user_ns['_headers_sample'] if samplename in sn_])
    linestyles = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
    lines = []
    for sn, ls in zip(samplenames, linestyles):
        print(sn)
        heds = ip.user_ns['_headers_sample'][sn]
        allheds = []
        for k in heds.keys():
            allheds.extend(heds[k])
        allheds = sorted(allheds, key=lambda x: x.fsn)
        flux = np.array([float(h.flux) for h in allheds])
        dates = [h.date for h in allheds]
        lines.extend(ax1.plot(dates, flux, ls + 'o', label='Flux (%s)' % sn))
        vacuums = np.array([float(h.vacuum) for h in allheds])
        lines.extend(ax2.plot(dates, vacuums, ls + 's', label='Vacuum (%s)' % sn, lw=2))
        print('  Measurement duration: %.2f h' % ((dates[-1] - dates[0]).total_seconds() / 3600.))
        print('  Mean flux: ', flux.mean(), '+/-', flux.std(), 'photons/sec')
        print('  RMS variation of flux: ', flux.std() / flux.mean() * 100, '%')
        print('  P-P variation of flux: ', flux.ptp() / flux.mean() * 100, '%')
    ax1.legend(lines, [l.get_label() for l in lines], loc='best')
    plt.show()
Exemplo n.º 23
0
def graph(fil):
    heap, time , free = np.loadtxt(fil, delimiter=',', unpack=True)

    fig,ax1 = plt.subplots(dpi=120, figsize=(7,7))
    ax2 = plt.twinx()

    ax1.set_ylabel("Allocation time ($ms$)",color = 'blue')
    ax1.set_xlabel("Initial heap size ($MB$)")

    ax2.set_ylabel("Free space on heap ($MB$)",color = 'green')
    ax2.set_xlabel("Initial heap size ($MB$)")


    p1,= ax1.plot(heap,time, label='Time taken to allocate large array')
    p2,= ax2.plot(heap,free , label='Free space on heap' ,color = 'green')


    plt.title('Scala Fragmentation tolerance')

    from matplotlib.font_manager import FontProperties
    fontP = FontProperties()
    fontP.set_size('small')


    lines =[p1,p2]

    plt.legend(lines, [l.get_label() for l in lines],prop = fontP ,loc =9)

    name =  fil.split('.')[0]
    name = name +".png"

    plt.savefig(name)
Exemplo n.º 24
0
def hist(nndist, **kwds):
    if 'output' in kwds:
        import matplotlib
        matplotlib.use('Agg')
    from matplotlib import pyplot as plt

    # handle formatting keywords
    linewidth = kwds.get('linewidth', 3)
    #plt.rc('text', usetex=True)
    plt.rc('font', family='serif')
    n, bins, patches = plt.hist(nndist, normed=True, bins=100)
    width = bins[1] - bins[0]
    ax1 = plt.gca()
    ax2 = plt.twinx()
    ax2.plot(bins[:-1], width*np.cumsum(n), 'r-', linewidth=linewidth)
    ax2.set_ylim(top=1.0)
    tics = ax1.get_yticks(); ax1.set_yticks(tics[1:])
    tics = ax2.get_yticks(); ax2.set_yticks(tics[1:])
    ax1.set_xlabel(r"d ($\mu$m)")
    ax1.set_ylabel(r"PDF")
    ax2.set_ylabel(r"CDF", color='r')
    for tl in ax2.get_yticklabels():
        tl.set_color('r')
    # check scalar descriptors
    median_dist = np.median(nndist)
    ax1.axvline(median_dist, color='gray', linewidth=linewidth)
    # handle keywords
    if 'title' in kwds:
        plt.title(kwds['title'])
    if 'output' in kwds:
        plt.draw()
        plt.savefig(kwds['output'])
    else:
        plt.show()
Exemplo n.º 25
0
def plotter (kind, experimental, steps):

    #print "maxdist_data :", len(maxdist_data)
    #print "experimental :", len(experimental)

    data_axes = ['8B','16B','32B','64B','128B','256B', '512B', '1KB', '2KB', '4KB', '8KB', '16KB','32KB','64KB','128KB','256KB', '512KB', '1MB',
                 '2MB', '4MB', '8MB', '16MB','32MB']

    model1         = plt.plot(maxdist_data, model_no_congestion(steps) ,  '--gD',  label="Analytical model (link)")
    model1         = plt.plot(maxdist_data, model_node(steps) ,  '--bD',  label="Analytical model (node)")
    #model2         = plt.plot(maxdist_data, model_with_congestion(steps) ,  '--yD',  label="Analytical model (with congestion)")
    line           = plt.plot(maxdist_data, experimental , '--r*', label=kind)
    plt.xticks(maxdist_data, data_axes, rotation=45)
    plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.5)
    #duplicate_err = plt.plot(maxdist_data, err(experimental, [no_congestion_model(d, steps) for d in maxdist_data]) , '-bs', label="Error %")
    plt.ylabel("Time to completion in us (log2 scale)")
    plt.xlabel("Data per Halo exchange in Bytes(log2 scale)")

    err_plot       = plt.twinx()

    rects1 = plt.bar(maxdist_data,
                     err(experimental, [node_model(d, steps) for d in maxdist_data]),
                     0.35,
                     alpha=0.2,
                     color='r',
                     label='Error %')
    #err_plot.plot(maxdist_data, err(experimental, [no_congestion_model(d, steps) for d in maxdist_data]) , '-bs', label="Error %")
    #err_plot.plot(maxdist_data, err(experimental, [congestion_model(d, steps) for d in maxdist_data]) , 'b-')
    #err_plot.plot(maxdist_data, err(linear_data, [no_congestion_model(d, steps) for d in maxdist_data]) , 'y-')
    plt.title('Time to complete halo exchange - 512 Nodes, RPN 16, 5D application matrix')

    err_plot.set_ylabel('Error %')
    plt.show()
Exemplo n.º 26
0
    def plot_error_time(self):
        '''Plot the speed and follow error on one figure, using two axes.'''
        for times, follow, speed, _ in self.each_condition('error'):
            ax = pl.gca()
            n = np.sqrt(len(follow))

            m = follow.mean(axis=0)
            e = follow.std(axis=0) / n
            lf = ax.plot(times, m, color='b')
            ax.fill_between(times, m - e, m + e, color='b', alpha=0.3, lw=0)
            ax.set_ylabel('Follow Error (m)')
            ax.set_ylim(*eval(self.opts.follow))
            ax.set_xlim(times[0], times[-1])

            ax = pl.twinx()

            m = speed.mean(axis=0)
            e = speed.std(axis=0) / n
            ls = ax.plot(times, m, color='g')
            ax.fill_between(times, m - e, m + e, color='g', alpha=0.3, lw=0)
            ax.set_ylabel('Speed Error (m/s)')
            ax.set_ylim(*eval(self.opts.speed))
            ax.set_xlim(times[0], times[-1])
            ax.set_xlabel('Time (s)')

            pl.grid(True)
            pl.legend((lf, ls), ('Follow', 'Speed'), loc='lower right')
Exemplo n.º 27
0
	def get(self):
		db = self.application.database
		epoc=datetime(2016,1,9)
		t = list()
		vbat = list()
		vsol = list()
		temp = list()
		for r in db.records.find():
			if r.has_key("content"):
				c=json.loads(r["content"])
				t.append((r["timestamp"]-epoc).total_seconds()/3600.0)
				vbat.append(c["vbat"])
				vsol.append(c["vsol"])
				temp.append(c["temperature"])
		fig = plt.figure()
		ax1 = fig.add_subplot(111)
		l1 = ax1.plot(t, vsol, '-', linewidth=1, label="Vsol (mV)")
		l2 = ax1.plot(t, vbat, '-', linewidth=1, label="Vbat (mV)")
		ax2 = plt.twinx()
		l3 = ax2.plot(t, temp, '-', linewidth=1, label=u"Temperature (°C)", color="r")
		ax1.legend(loc=0)
		ax2.legend()

		buf = io.BytesIO()
		plt.savefig(buf, format='png')
		self.set_header("Content-Type", "image/png")
		self.write(buf.getvalue())
		buf.close()
Exemplo n.º 28
0
def plot_DE_results( OFE_budgets, Fmin_values, N_values, Cr_values, F_values ):
    pyplot.figure()
    pyplot.subplot(1,2,1)
    _plot_pareto_front( OFE_budgets, Fmin_values )
    pyplot.subplot(1,2,2)
    line_Cr = pyplot.semilogx(OFE_budgets, Cr_values, 'b^')[0]
    line_F  = pyplot.semilogx(OFE_budgets, F_values, 'rx')[0]
    pyplot.ylabel('Cr, F')
    pyplot.twinx()
    line_N = pyplot.semilogx(OFE_budgets, N_values, 'go')[0]
    pyplot.ylim( 0, max(N_values)*1.1)
    pyplot.ylabel('N')
    pyplot.legend([line_Cr,line_F,line_N], ['Cr','F','N'], loc='upper center')
    pyplot.xlim(min(OFE_budgets),max(OFE_budgets))
    pyplot.xlabel('OFE budget')
    pyplot.title('Optimal CPVs for different OFE budgets')
Exemplo n.º 29
0
def plot_CMD_CDD_diff(CMD_file_name, CCD_file_name, init_dis, CCD_start_num=0, CMD_start_num=0, factor=10000):
    '''
    ��ȡ����λ�ú�CCDʵ�ʲ��λ�������ļ�������CCD�ļ��е����ݽ��н�ȡ��Ȼ�����߶Աȣ��õ����
    CMD_file_name��CMD�������ļ�
    CCD_file_name��CCD�������ļ�
    init_dis��CCD�ɼ�ʱ�����ڵļ��
    CCD_start_num��CCD�ɼ���������Ч���ݵ���ʼ��ַ����һ�����ݵ��start_numΪ0
    CMD_start_num��PMAC������������Ч���ݵ���ʼ��ַ,��һ�����ݵ��start_numΪ0
    factor��CCD���ݲɼ���λ��PMAC�ĵ�λת�����ӣ���1mmת��0.1umΪ10000.
    '''
    CMD_data = get_data_from_file(CMD_file_name)[CMD_start_num:-1]
    # CMD_data = CCD_data_process(CMD_file_name, init_dis=0, start_num=CMD_start_num, factor=1)
    CCD_data = CCD_data_process(CCD_file_name, init_dis, CCD_start_num, len(CMD_data))
    CCD_line, = plt.plot(CCD_data, label='CCD')
    CMD_line, = plt.plot(CMD_data, label='CMD')
    plt.xlabel(r't/442us')
    plt.ylabel(r'y/cts')
    print CCD_data[-1] - CMD_data[-1]
    print len(CCD_data), len(CMD_data)
    logging.warning("dd")
    par2 = plt.twinx()
    fe_line, = par2.plot(CMD_data - CCD_data, label='fe')
    par2.set_ylabel('following error')
    fe_line.set_color('red')
    plt.legend([CCD_line, CMD_line, fe_line], ["CCD", "CMD", "fe"])
    plt.grid()
    plt.show()
    pass
Exemplo n.º 30
0
def plotVectors(json, coincidence=0, name="", minTime=30, maxTime=1700,
                labels=False):
    delims = readDelims()

    macAddresses = {}
    json["packets"].apply(lambda row: addPacket(row, macAddresses), axis=1)

    sum = np.zeros(json["last"], dtype=np.int)
    for val in macAddresses.values():
        diff = val[1] - val[0]
        if ((diff > minTime) and (diff < maxTime) and (val[2] >= coincidence)):
            sum[val[0]: val[1]] += 1

    plot.xlabel('Seconds since start')
    plot.ylabel('Devices', color='b')
    plot.step(range(int(json["last"])), sum.tolist())

    actual = [stop['actual'] for stop in delims]
    stopx = [stop['start'] for stop in delims]
    ax2 = plot.twinx()
    ax2.step(stopx, actual, 'r', where="post")
    (t1.set_color for t1 in ax2.get_yticklabels())
    ax2.set_ylabel('Riders', color='r')
    plot.ylim(0, 30)
    plot.xlim(0, json["last"])

    if(labels):
        for stop in delims:
            annotate(stop["code"], stop["start"], stop["actual"], 10, 10)

    makeWidePlot("bus", "vectors")

    plot.show()
                                     rec=rec, FS = FS, filename_h5 = spec_data)
    model_results['run_model'].append('spec2m_simple')




# plotting section
# what will be plottet
nr_p = plot_rates + plot_adapt + plot_input
fig = plt.figure(); pidx = 1
if plot_input:
    ax_mu = fig.add_subplot(nr_p, 1, pidx)
    plt.plot(t_ext, mu_ext_orig, color = 'k', lw=1.5) if filter_mean else 0
    line_mu_final = plt.plot(t_ext, ext_input0[0], color = 'm', lw=1.5, label='$\mu_\mathrm{final}$')
    plt.ylabel('$\mu_{ext}$ [mV/ms]', fontsize=15)
    ax_sig = plt.twinx()    
    plt.plot(t_ext, sigma_ext_orig, color = 'g', lw=1.5) if filter_std else 0
    line_sig_final = plt.plot(t_ext, ext_input0[1], color = 'b', lw=1.5, label='$\sigma_\mathrm{final}$')
    plt.ylabel('$\sigma_{ext}$ [$\sqrt{mV}$/ms]', fontsize=15)
    plt.legend([line_mu_final[0], line_sig_final[0]], 
               [line_mu_final[0].get_label(), line_sig_final[0].get_label()])
    pidx +=1

if plot_rates:
    ax_rate = fig.add_subplot(nr_p, 1, pidx, sharex=ax_mu)
    for model in model_results['run_model']:
        color = params['color'][model]
        lw = params['lw'][model]
        key = 'results_{}'.format(model)
        time = model_results[key]['t']
        rates = model_results[key]['r']
Exemplo n.º 32
0
    def something(rundir, file_no):

        my_path = os.getcwd()
        #print(my_path)
        working_dir = my_path + '/' + rundir
        #print(working_dir)
        efield_dir = working_dir + '/MS/FLD/e1/'
        laser_dir = working_dir + '/MS/FLD/e2/'
        eden_dir = working_dir + '/MS/DENSITY/electrons/charge/'
        iden_dir = working_dir + '/MS/DENSITY/ions/charge/'
        phase_space_dir = working_dir + '/MS/PHA/p1x1/ions/'
        p1x1_dir = working_dir + '/MS/PHA/p1x1/electrons/'

        efield_prefix = 'e1-'
        laser_prefix = 'e2-'
        phase_prefix = 'p1x1-ions-'
        p1x1_prefix = 'p1x1-electrons-'
        eden_prefix = 'charge-electrons-'
        iden_prefix = 'charge-ions-'
        fig = plt.figure(figsize=(12, 16))

        filename1 = phase_space_dir + phase_prefix + repr(file_no).zfill(
            6) + '.h5'
        filename2 = eden_dir + eden_prefix + repr(file_no).zfill(6) + '.h5'
        filename3 = efield_dir + efield_prefix + repr(file_no).zfill(6) + '.h5'
        filename4 = laser_dir + laser_prefix + repr(file_no).zfill(6) + '.h5'
        filename5 = p1x1_dir + p1x1_prefix + repr(file_no).zfill(6) + '.h5'
        filename6 = iden_dir + iden_prefix + repr(file_no).zfill(6) + '.h5'

        #print(filename1)
        #print(filename2)

        phase_space = np.abs(osh5io.read_h5(filename1))
        # print(repr(phase_space))
        eden = osh5io.read_h5(filename2)
        ex = osh5io.read_h5(filename3)
        ey = osh5io.read_h5(filename4)
        p1x1 = np.abs(osh5io.read_h5(filename5))
        iden = osh5io.read_h5(filename6)

        phase_plot = plt.subplot(325)
        #print(repr(phase_space.axes[0].min))
        #print(repr(phase_space.axes[1].min))
        title = phase_space.data_attrs['LONG_NAME']
        time = phase_space.run_attrs['TIME'][0]

        fig.suptitle('Time = ' + repr(time) + '$\omega_p^{-1}$', fontsize=24)
        ext_stuff = [
            phase_space.axes[1].min, phase_space.axes[1].max,
            phase_space.axes[0].min, phase_space.axes[0].max
        ]
        data_max = max(np.abs(np.amax(phase_space)), 100)
        #print(repr(data_max))
        phase_contour = plt.contourf(
            np.abs(phase_space + 0.000000001),
            levels=[
                0.00001 * data_max, 0.0001 * data_max, 0.001 * data_max,
                0.01 * data_max, 0.05 * data_max, 0.1 * data_max,
                0.2 * data_max, 0.5 * data_max
            ],
            extent=ext_stuff,
            cmap='Spectral',
            vmin=1e-5 * data_max,
            vmax=1.5 * data_max,
            norm=colors.LogNorm(vmin=0.00001 * data_max, vmax=1.5 * data_max))
        phase_plot.set_title('Ion P1X1 Phase Space')
        phase_plot.set_xlabel('Position [$c / \omega_{p}$]')
        phase_plot.set_ylabel('Proper Velocity $\gamma v_1$ [ c ]')
        second_x = plt.twinx()
        second_x.plot(ex.axes[0], ex, 'g', linestyle='-.')

        #plt.colorbar()
        #osh5vis.oscontour(phase_space,levels=[10**-5,10**-3,10**-1,1,10,100],colors='black',linestyles='dashed',vmin=1e-5,vmax=1000)
        # plt.contour(np.abs(phase_space+0.000001),levels=[0.0001,0.001,0.01,0.05,0.1,0.2,0.5,1],extent=ext_stuff,colors='black',linestyles='dashed')
        plt.colorbar(phase_contour)

        den_plot = plt.subplot(323)
        osh5vis.osplot(eden, title='Electron Density')

        iden_plot = plt.subplot(324)
        osh5vis.osplot(iden, title='Ion Density')

        # for i in range(ex.shape[0]-2,-1,-1):
        #     ex[i]=ex[i+1] + ex.axes[0].increment*ex[i]
        ex_plot = plt.subplot(322)

        osh5vis.osplot(ex, title='Wake E-field ', ylabel='$E_1 [m_e c^2/e]$')

        ey_plot = plt.subplot(321)

        osh5vis.osplot(ey, title='Laser Electric Field')
Exemplo n.º 33
0
def tenpar_plot():
    import os
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_pdf import PdfPages

    import pandas as pd
    from pyemu import Pst
    d = os.path.join("smoother","10par_xsec")
    pst = Pst(os.path.join(d,"10par_xsec.pst"))
    plt_dir = os.path.join(d,"plot")
    if not os.path.exists(plt_dir):
        os.mkdir(plt_dir)


    par_files = [os.path.join(d,f) for f in os.listdir(d) if "parensemble." in f
                 and ".png" not in f]
    par_dfs = [pd.read_csv(par_file,index_col=0).apply(np.log10) for par_file in par_files]
    par_names = list(par_dfs[0].columns)
    mx = (pst.parameter_data.loc[:,"parubnd"] * 1.1).apply(np.log10)
    mn = (pst.parameter_data.loc[:,"parlbnd"] * 0.9).apply(np.log10)

    obj_df = pd.read_csv(os.path.join(d,"10par_xsec.pst.iobj.csv"),index_col=0)
    real_cols = [col for col in obj_df.columns if col.startswith("0")]
    obj_df.loc[:,real_cols] = obj_df.loc[:,real_cols].apply(np.log10)
    obj_df.loc[:,"mean"] = obj_df.loc[:,"mean"].apply(np.log10)
    obj_df.loc[:, "std"] = obj_df.loc[:, "std"].apply(np.log10)

    fig = plt.figure(figsize=(20, 10))
    ax = plt.subplot(111)
    axt = plt.twinx()
    obj_df.loc[:, real_cols].plot(ax=ax, lw=0.5, color="0.5", alpha=0.5, legend=False)
    ax.plot(obj_df.index, obj_df.loc[:, "mean"], 'b', lw=2.5,marker='.',markersize=5)
    #ax.fill_between(obj_df.index, obj_df.loc[:, "mean"] - (1.96 * obj_df.loc[:, "std"]),
    #                obj_df.loc[:, "mean"] + (1.96 * obj_df.loc[:, "std"]),
    #                facecolor="b", edgecolor="none", alpha=0.25)
    axt.plot(obj_df.index,obj_df.loc[:,"lambda"],"k",dashes=(2,1),lw=2.5)
    ax.set_ylabel("log$_10$ phi")
    axt.set_ylabel("lambda")
    ax.set_title("total runs:{0}".format(obj_df.total_runs.max()))
    plt.savefig(os.path.join(plt_dir,"iobj.pdf"))
    plt.close()

    with PdfPages(os.path.join(plt_dir,"parensemble.pdf")) as pdf:

        for par_file,par_df in zip(par_files,par_dfs):
            print(par_file)
            fig = plt.figure(figsize=(20,10))

            plt.figtext(0.5,0.975,par_file,ha="center")
            axes = [plt.subplot(2,6,i+1) for i in range(len(par_names))]
            for par_name,ax in zip(par_names,axes):
                mean = par_df.loc[:,par_name].mean()
                std = par_df.loc[:,par_name].std()
                par_df.loc[:,par_name].hist(ax=ax,edgecolor="none",
                                            alpha=0.5,grid=False)
                ax.set_yticklabels([])
                ax.set_title("{0}, {1:6.2f}".\
                             format(par_name,10.0**mean))
                ax.set_xlim(mn[par_name],mx[par_name])
                ylim = ax.get_ylim()
                if "stage" in par_name:
                    val = np.log10(1.5)
                else:
                    val = np.log10(2.5)
                ticks = ["{0:2.1f}".format(x) for x in 10.0**ax.get_xticks()]
                ax.set_xticklabels(ticks,rotation=90)
                ax.plot([val,val],ylim,"k-",lw=2.0)

                ax.plot([mean,mean],ylim,"b-",lw=1.5)
                ax.plot([mean+(2.0*std),mean+(2.0*std)],ylim,"b--",lw=1.5)
                ax.plot([mean-(2.0*std),mean-(2.0*std)],ylim,"b--",lw=1.5)
            pdf.savefig()
            plt.close()




    obs_files = [os.path.join(d,f) for f in os.listdir(d) if "obsensemble." in f
                 and ".png" not in f]
    obs_dfs = [pd.read_csv(obs_file) for obs_file in obs_files]
    #print(obs_files)
    #mx = max([obs_df.obs.max() for obs_df in obs_dfs])
    #mn = min([obs_df.obs.min() for obs_df in obs_dfs])
    #print(mn,mx)
    obs_names = ["h01_04","h01_06","h01_08","h02_08"]
    #print(obs_files)
    obs_dfs = [obs_df.loc[:,obs_names] for obs_df in obs_dfs]
    mx = {obs_name:max([obs_df.loc[:,obs_name].max() for obs_df in obs_dfs]) for obs_name in obs_names}
    mn = {obs_name:min([obs_df.loc[:,obs_name].min() for obs_df in obs_dfs]) for obs_name in obs_names}

    with PdfPages(os.path.join(plt_dir,"obsensemble.pdf")) as pdf:
        for obs_file,obs_df in zip(obs_files,obs_dfs):
            fig = plt.figure(figsize=(10,10))
            plt.figtext(0.5,0.975,obs_file,ha="center")
            print(obs_file)
            axes = [plt.subplot(2,2,i+1) for i in range(len(obs_names))]
            for ax,obs_name in zip(axes,obs_names):
                mean = obs_df.loc[:,obs_name].mean()
                std = obs_df.loc[:,obs_name].std()
                obs_df.loc[:,obs_name].hist(ax=ax,edgecolor="none",
                                            alpha=0.5,grid=False)
                ax.set_yticklabels([])
                #print(ax.get_xlim(),mn[obs_name],mx[obs_name])
                ax.set_title("{0}, {1:6.2f}:{2:6.2f}".format(obs_name,mean,std))
                #ax.set_xlim(mn[obs_name],mx[obs_name])
                ax.set_xlim(0.0,20.0)
                ylim = ax.get_ylim()
                oval = pst.observation_data.loc[obs_name,"obsval"]
                ax.plot([oval,oval],ylim,"k-",lw=2)
                ax.plot([mean,mean],ylim,"b-",lw=1.5)
                ax.plot([mean+(2.0*std),mean+(2.0*std)],ylim,"b--",lw=1.5)
                ax.plot([mean-(2.0*std),mean-(2.0*std)],ylim,"b--",lw=1.5)
            pdf.savefig()
            plt.close()
Exemplo n.º 34
0
def ts_fit_graph(ts, params):
    """Create graphic showing an histogram of the data and the distribution fitted to it.

    The graphic contains one panel per watershed.

    Parameters
    ----------
    ts : xr.DataArray
      Stream flow time series with dimensions (time, nbasins).
    params : xr.DataArray
      Fitted distribution parameters returned by `xclim.land.fit` indicator.

    Returns
    -------
    fig
      Figure showing a histogram and the parameterized pdf.
    """
    from xclim.indices.stats import get_dist

    n = ts.nbasins.size
    dist = params.attrs["scipy_dist"]

    fig, axes = plt.subplots(n, figsize=(10, 6), squeeze=False)

    for i in range(n):
        ax = axes.flat[i]
        ax2 = plt.twinx(ax)
        p = params.isel(nbasins=i)
        t = ts.isel(nbasins=i).dropna(dim="time")

        # Plot histogram of time series as density then as a normal count.
        density, bins, patches = ax.hist(
            t,
            alpha=0.5,
            density=True,
            bins="auto",
            label="__nolabel__",
        )
        ax2.hist(
            t,
            bins=bins,
            facecolor=(1, 1, 1, 0.01),
            edgecolor="gray",
            linewidth=1,
        )

        # Plot pdf of distribution
        dc = get_dist(dist)(*params.isel(nbasins=i))
        mn = dc.ppf(0.01)
        mx = dc.ppf(0.99)
        q = np.linspace(mn, mx, 200)
        pdf = dc.pdf(q)

        ps = ", ".join([f"{x:.1f}" for x in p.values])
        ax.plot(q,
                pdf,
                "-",
                label="{}({})".format(params.attrs["scipy_dist"], ps))

        # Labels
        ax.set_xlabel(f"{ts.long_name} (${units2pint(ts.units):~P}$)")
        ax.set_ylabel("Probability density")
        ax2.set_ylabel("Histogram count")

        ax.legend(frameon=False)

    plt.tight_layout()
    return fig
Exemplo n.º 35
0
    safety_factor = 0.001

    # dt_arr = np.load('dt_arr.npy')
    # print(dt_arr)
    # let gas cool at constant density
    data = evolve_constant_density(fc,
                                   final_time=final_time,
                                   safety_factor=safety_factor)

    p1, = pyplot.loglog(data["time"].to("Myr"),
                        data["temperature"],
                        color="black",
                        label="T")
    pyplot.xlabel("Time [Myr]")
    pyplot.ylabel("T [K]")

    data["mu"] = data["temperature"] / \
        (data["energy"] * (my_chemistry.Gamma - 1.) *
         fc.chemistry_data.temperature_units)
    pyplot.twinx()
    p2, = pyplot.semilogx(data["time"].to("Myr"),
                          data["mu"],
                          color="red",
                          label="$\\mu$")
    pyplot.ylabel("$\\mu$")
    pyplot.legend([p1, p2], ["T", "$\\mu$"], fancybox=True, loc="center left")
    pyplot.savefig("cooling_cell.png")

    # save data arrays as a yt dataset
    yt.save_as_dataset({}, "cooling_cell.h5", data)
Exemplo n.º 36
0
            if test_img_group_manual_single_target is not None:
                print()
                print(img_name, " => ", empties_cnt)
                print()

    if test_img_group_manual_single_target is None:
        num_changed_list = np.array(num_changed_list)
        np.save(os.path.join(out_path, "LeNet5__num_changed.npy"),
                num_changed_list)
        print(num_changed_list)

    # plot
    if test_img_group_manual_single_target is None:
        plt.close("all")
        fig, ax_violin = plt.subplots()
        ax_unsolved = plt.twinx(ax_violin)
        changed_num_reshaped = np.array(num_changed_list).reshape(5, -1)
        plot_values = []
        plot_unsolved_ratio = []
        for view_idx in range(5):
            _plot_val = changed_num_reshaped[view_idx]
            plot_val = _plot_val[np.where(_plot_val < 10)]
            plot_values.append(plot_val)
            plot_unsolved_ratio.append(100 - 100. * plot_val.size / _plot_val.size)
        violin = ax_violin.violinplot(plot_values, positions=np.arange(5),
                                      showmeans=True, showextrema=True, showmedians=True)
        ratio_line = ax_unsolved.plot(plot_unsolved_ratio, marker="o", color="orange",
                                      label="Unsolved Boards Ratio")
        # handle legends
        label_flag = []
        label_str = []
Exemplo n.º 37
0
def bin_woe_iv(s, target, desc='Fineclass'):
    '''
    Pass the feature and target series in (both should have the same index)
    Return the dataframe of woe IV stats and plots
    params:
        desc: by default 'Fineclass', can set as Coarse class with customised description
    '''
    eval_df = pd.DataFrame({'feat': s, 'target': target})
    eval_df.sort_values(by='feat')
    eval_df['good'] = (eval_df['target'] == 0)
    eval_df['bad'] = (eval_df['target'] == 1)
    eval_df_summary = eval_df.groupby('feat')[['good', 'bad'
                                               ]].sum().rename(columns={
                                                   'good': 'N_good',
                                                   'bad': 'N_bad'
                                               }).reset_index()
    #get the numeric order of num part so 10. is not ranked before 2.
    eval_df_summary['feat_num_index'] = pd.to_numeric(
        eval_df_summary['feat'].map(lambda x: x.split('.')[0]),
        errors='coerce')
    eval_df_summary.sort_values(by=['feat_num_index', 'feat'], inplace=True)
    eval_df_summary = eval_df_summary.reset_index(drop=True)
    eval_df_summary[
        'N_count'] = eval_df_summary['N_good'] + eval_df_summary['N_bad']
    num_good_total = eval_df_summary['N_good'].sum()
    num_bad_total = eval_df_summary['N_bad'].sum()

    eval_df_summary['dist_good'] = eval_df_summary['N_good'] / num_good_total
    eval_df_summary['dist_bad'] = eval_df_summary['N_bad'] / num_bad_total
    eval_df_summary['bin_count_perc'] = eval_df_summary['N_count'] / (
        num_good_total + num_bad_total)
    eval_df_summary['woe'] = np.log(eval_df_summary['dist_good'] /
                                    eval_df_summary['dist_bad']).replace(
                                        np.inf, np.nan)
    eval_df_summary['logodds'] = np.log(eval_df_summary['N_good'] /
                                        eval_df_summary['N_bad']).replace(
                                            np.inf, np.nan)
    eval_df_summary['p_bad'] = (eval_df_summary['N_bad'] /
                                eval_df_summary['N_count']).replace(
                                    np.inf, np.nan)
    eval_df_summary['iv'] = (
        eval_df_summary['dist_good'] -
        eval_df_summary['dist_bad']) * eval_df_summary['woe']

    s_total = eval_df_summary.sum().drop(
        ['feat_num_index', 'woe', 'logodds', 'p_bad'])
    s_total.iloc[0] = 'Total'

    eval_df_summary = eval_df_summary.append(s_total, ignore_index=True)
    eval_df_summary['var'] = s.name
    eval_df_summary['desc'] = desc

    print(desc)
    print('IV is ' + str(eval_df_summary['iv'].sum()))
    #     if eval_df_summary['N_count'].sum()==((target==0)|(target==1)).sum():
    #         print('total number of rows match')
    #     else:
    #         print('total number of rows DOES NOT match')

    eval_df_summary_forplot = eval_df_summary.loc[
        eval_df_summary['feat'] != 'Total']
    plt.plot(eval_df_summary_forplot['woe'], label='woe', marker='.')
    plt.title(s.name + ' woe')
    plt.xticks(ticks=eval_df_summary_forplot.index.values,
               labels=eval_df_summary_forplot['feat'].values)
    plt.xticks(rotation=60)
    plt.legend(loc='upper left')
    plt.twinx()
    plt.bar(eval_df_summary_forplot.index,
            eval_df_summary_forplot['bin_count_perc'],
            alpha=0.1,
            label='bin_vol%')
    plt.legend(loc='upper right')
    plt.show()
    return eval_df_summary
Exemplo n.º 38
0
def loadRunSTGNeuroML_L123(filename):
    'Loads and runs the pyloric rhythm generator from NeuroML files.'
    # for graded synapses, else NeuroML event-based are used
    from load_synapses import load_synapses
    moose.Neutral('/library')
    # set graded to False to use event based synapses
    #  if False, neuroml event-based synapses get searched for and loaded
    # True to load graded synapses
    graded_syn = True
    #graded_syn = False
    if graded_syn:
        load_synapses()

    neuromlR = NeuroML()
    ## readNeuroMLFromFile below returns:
    # This returns
    # populationDict = {
    #     'populationname1':('cellName',{('instanceid1'):moosecell, ... })
    #     , ...
    #     }
    # (cellName and instanceid are strings, mooosecell is a moose.Neuron object instance)
    # and
    # projectionDict = {
    #     'projName1':('source','target',[('syn_name1','pre_seg_path','post_seg_path')
    #     ,...])
    #     , ...
    #     }
    populationDict, projectionDict = \
        neuromlR.readNeuroMLFromFile(filename)
    soma1_path = populationDict['AB_PD'][1][0].path + '/Soma_0'
    soma1Vm = setupTable('somaVm', moose.Compartment(soma1_path), 'Vm')
    soma2_path = populationDict['LP'][1][0].path + '/Soma_0'
    soma2Vm = setupTable('somaVm', moose.Compartment(soma2_path), 'Vm')
    soma3_path = populationDict['PY'][1][0].path + '/Soma_0'
    soma3Vm = setupTable('somaVm', moose.Compartment(soma3_path), 'Vm')

    # monitor channel current
    channel_path = soma1_path + '/KCa_STG'
    channel_Ik = setupTable('KCa_Ik', moose.element(channel_path), 'Ik')
    # monitor Ca
    capool_path = soma1_path + '/CaPool_STG'
    capool_Ca = setupTable('CaPool_Ca', moose.element(capool_path), 'Ca')

    # monitor synaptic current
    soma2 = moose.element(soma2_path)
    print "Children of", soma2_path, "are:"
    for child in soma2.children:
        print child.className, child.path
    if graded_syn:
        syn_path = soma2_path + '/DoubExpSyn_Ach__cells-0-_AB_PD_0-0-_Soma_0'
        syn = moose.element(syn_path)
    else:
        syn_path = soma2_path + '/DoubExpSyn_Ach'
        syn = moose.element(syn_path)
    syn_Ik = setupTable('DoubExpSyn_Ach_Ik', syn, 'Ik')

    print "Reinit MOOSE ... "
    resetSim(['/elec', cells_path], simdt, plotdt, simmethod='hsolve')

    print "Using graded synapses? = ", graded_syn
    print "Running model filename = ", filename, " ... "
    moose.start(runtime)
    tvec = np.arange(0.0, runtime + 2 * plotdt, plotdt)
    tvec = tvec[:soma1Vm.vector.size]

    fig = plt.figure(facecolor='w', figsize=(10, 6))
    axA = plt.subplot2grid((3, 2), (0, 0), rowspan=3, colspan=1, frameon=False)
    img = plt.imread('STG.png')
    imgplot = axA.imshow(img)
    for tick in axA.get_xticklines():
        tick.set_visible(False)
    for tick in axA.get_yticklines():
        tick.set_visible(False)
    axA.set_xticklabels([])
    axA.set_yticklabels([])
    ax = plt.subplot2grid((3, 2), (0, 1), rowspan=1, colspan=1)
    ax.plot(tvec,
            soma1Vm.vector * 1000,
            label='AB_PD',
            color='g',
            linestyle='solid')
    ax.set_xticklabels([])
    ax.set_ylabel('AB_PD (mV)')
    ax = plt.subplot2grid((3, 2), (1, 1), rowspan=1, colspan=1)
    ax.plot(tvec,
            soma2Vm.vector * 1000,
            label='LP',
            color='r',
            linestyle='solid')
    ax.set_xticklabels([])
    ax.set_ylabel('LP (mV)')
    ax = plt.subplot2grid((3, 2), (2, 1), rowspan=1, colspan=1)
    ax.plot(tvec,
            soma3Vm.vector * 1000,
            label='PY',
            color='b',
            linestyle='solid')
    ax.set_ylabel('PY (mV)')
    ax.set_xlabel('time (s)')
    fig.tight_layout()

    fig = plt.figure(facecolor='w')
    plt.plot(tvec,
             soma2Vm.vector * 1000,
             label='LP',
             color='r',
             linestyle='solid')
    plt.plot(tvec,
             soma3Vm.vector * 1000,
             label='PY',
             color='b',
             linestyle='solid')
    plt.legend()
    plt.xlabel('time (s)')
    plt.ylabel('Soma Vm (mV)')

    plt.figure(facecolor='w')
    plt.plot(tvec, channel_Ik.vector, color='b', linestyle='solid')
    plt.title('KCa current; Ca conc')
    plt.xlabel('time (s)')
    plt.ylabel('Ik (Amp)')
    plt.twinx()
    plt.plot(tvec, capool_Ca.vector, color='r', linestyle='solid')
    plt.ylabel('Ca (mol/m^3)')

    plt.figure(facecolor='w')
    plt.plot(tvec, syn_Ik.vector, color='b', linestyle='solid')
    plt.title('Ach syn current in ' + soma2_path)
    plt.xlabel('time (s)')
    plt.ylabel('Isyn (S)')
    print "Showing plots ..."

    plt.show()
Exemplo n.º 39
0
    def fit(self, train, test=None, fname=None):
        self.lr_hist = []
        self.e = 1
        input_shape = self.arch['input_shape']
        output_shape = self.arch['output_shape']
        offset_max = train.shape[2] - (input_shape[1] + output_shape[1])
        output_idx = self.arch['output_idx']

        def get_xy(b):
            offset = np.random.randint(0, offset_max, len(b))
            x, y = [], []
            for i, _offset in enumerate(offset):
                x.append(b[i][:, _offset:_offset + input_shape[1]])
                _offset += input_shape[1]
                _b = b[i][output_idx]
                y.append(_b[:, _offset:_offset + output_shape[1]])
            return np.array(x), np.array(y)

        if not test is None:
            x_test, y_test = get_xy(test)
            if not self._scaling is None:
                x_test, y_test = self._scaling_xy(x_test, y_test)

        def get_time(_sec):
            _day, _hour, _min = 0, 0, 0
            _1day = 3600 * 24
            if _sec > _1day:
                _day = _sec // _1day
                _sec -= _day * _1day
            if _sec > 3600:
                _hour = _sec // 3600
                _sec -= _hour * 3600
            if _sec > 60:
                _min = _sec // 60
                _sec -= _min * 60
            return int(_day), int(_hour), int(_min), int(_sec)

        history, _time = [], []
        print_str = ''
        while self.e <= self.epoch:
            if self.e in self.anneal:
                self.opt._lr /= 2
            self.lr_hist.append(self.opt._lr)
            s = time()
            x_train, y_train = get_xy(train)
            if not self._scaling is None:
                x_train, y_train = self._scaling_xy(x_train, y_train)
            perm = np.random.permutation(len(x_train))

            if 0:
                print(input_shape, output_shape)
                print(x_train.shape, y_train.shape)

            _train = self.forward(x_train, perm, y=y_train, opt=self.opt)
            if not test is None:
                perm = np.arange(len(x_test))
                _test = self.forward(x_test, perm, y=y_test)
            _cp = time() - s
            _time.append(_cp)
            print_str = ' ' * len(print_str)
            print(print_str, end='\r')
            print_str = f'{self.e:05d}/{self.epoch:05d} {_train.mean():.6f}'
            if not test is None:
                print_str += f' {_test.mean():.6f}'
            print_str += f' @ {_cp:.2f}sec'
            _sec = np.array(_time).mean() * (self.epoch - self.e)
            _day, _hour, _min, _sec = get_time(_sec)
            if _day > 0:
                print_str += f' / {_day:d}d-'
            else:
                print_str += ' / '
            print_str += f'{_hour:02d}:{_min:02d}:{_sec:02d}'
            print(print_str, end='\r', flush=True)
            if test is None:
                history.append([self.e, _train.mean(), _cp])
            else:
                history.append([self.e, _train.mean(), _test.mean(), _cp])
            self.e += 1
        _sec = np.array(_time).sum()
        _day, _hour, _min, _sec = get_time(_sec)
        print(f'\nFinished at {_day:d}d-{_hour:02d}:{_min:02d}:{_sec:02d}')
        if fname:
            history = np.array(history)
            plt.plot(history[:, 0], history[:, 1], label='train', alpha=.6)
            if not x_test is None:
                plt.plot(history[:, 0], history[:, 2], label='test', alpha=.6)
            plt.legend()
            plt.xscale('log')
            plt.yscale('log')
            plt.xlabel('epoch')
            plt.ylabel('Error')
            plt.grid()
            plt.twinx()
            plt.plot(history[:, 0], self.lr_hist, 'k', lw=1)
            plt.ylabel('learning rate')
            plt.yscale('log')
            plt.tight_layout()
            plt.savefig(fname)
            plt.clf()
            plt.close()
Exemplo n.º 40
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description='SER example: MLP')
    parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)')
    parser.add_argument('--layer', type=int, default=1, help='number of layes for turn')
    parser.add_argument('--unit',  type=int, default=500, help='number of units for turn')
    parser.add_argument('--batchsize', '-b', type=int, default=100, help='number of images in each mini-batch')
    parser.add_argument('--dropout', '-d', type=float, default=0.4, help='value of dropout rate')
    parser.add_argument('--weightdecay', default=0.001, type=float, help='value of weight decay rate')
    parser.add_argument('--epoch', '-e', type=int, default=100, help='number of sweeps over the dataset to train')
    parser.add_argument('--train', default='datasets/signate/smile/train.txt', type=str, help='training file (.txt)')
    parser.add_argument('--valid', default='datasets/signate/smile/valid.txt', type=str, help='validation file (.txt)')
    parser.add_argument('--out', '-o', default='result-mlp_cw', help='directory to output the result')
    parser.add_argument('--noplot', action='store_true', help='disable PlotReport extension')
    parser.add_argument('--optim',  default='adam', choices=['adam', 'adadelta'], help='type of optimizer')
    parser.add_argument('--cw', default='none', choices=['none', 'sum', 'norm'], help='type of class weight')
    args = parser.parse_args()
    # args = parser.parse_args(args=[])
    print(json.dumps(args.__dict__, indent=2))
    sys.stdout.flush()

    seed = 123
    os.environ['PYTHONHASHSEED'] = str(seed)
    random.seed(seed)
    np.random.seed(seed)

    if args.gpu >= 0:
        chainer.backends.cuda.get_device_from_id(args.gpu).use()
        cuda.check_cuda_available()
        cuda.cupy.random.seed(seed)

    model_dir = args.out
    if not os.path.exists(model_dir):
        os.mkdir(model_dir)

    # データの読み込み
    labels = {'neu': 0, 'ang': 1, 'sad': 2, 'hap': 3}
    # labels = {'MA_CH': 0, 'FE_AD': 1, 'MA_AD': 2, 'FE_EL': 3, 'FE_CH': 4, 'MA_EL': 5}

    X_train, y_train, labels = load_data(args.train, labels=labels)
    X_eval,  y_eval,  labels = load_data(args.valid, labels=labels)

    n_class = len(labels)

    print('# train X: {}, y: {}, dim: {}, counts: {}'.format(len(X_train), len(y_train), len(X_train[0]), [y_train.count(x) for x in sorted(labels.values())]))
    print('# eval  X: {}, y: {}, dim: {}, counts: {}'.format(len(X_eval),  len(y_eval),  1,               [y_eval.count(x)  for x in sorted(labels.values())]))
    print('# class: {}, labels: {}'.format(n_class, labels))

    class_weight = None
    class_count = np.array([y_train.count(x) for x in sorted(labels.values())], 'f')
    if args.cw == 'sum':
        class_weight = np.sum(class_count) / class_count
    elif args.cw == 'norm':
        class_weight = np.sum(class_count) / class_count
        class_weight = class_weight / np.max(class_weight)
    print('# class_weight: {}'.format(class_weight))
    sys.stdout.flush()

    if args.gpu >= 0:
        class_weight = cuda.to_gpu(class_weight)

    with open(os.path.join(args.out, 'labels.pkl'), 'wb') as f:
        pickle.dump(labels, f)

    model = SER(n_layers=args.layer, n_outputs=n_class, n_units=args.unit, dropout_rate=args.dropout, class_weight=class_weight)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    # 学習率
    lr = 0.001

    # 学習率の減衰
    lr_decay = 0.99

    # 勾配上限
    grad_clip = 3

    # Setup optimizer (Optimizer の設定)
    if args.optim == 'adam':
        optimizer = chainer.optimizers.Adam(alpha=lr, beta1=0.9, beta2=0.999, weight_decay_rate=args.weightdecay, eps=1e-8)
    elif args.optim == 'adadelta':
        optimizer = chainer.optimizers.AdaDelta()
    else:
        raise ValueError("Only support adam or adadelta.")

    # optimizer.add_hook(chainer.optimizer.GradientClipping(grad_clip))
    optimizer.setup(model)

    # プロット用に実行結果を保存する
    train_loss = []
    train_accuracy1 = []
    train_accuracy2 = []
    test_loss = []
    test_accuracy1 = []
    test_accuracy2 = []

    min_loss = float('inf')
    best_accuracy = .0

    start_at = time.time()
    cur_at = start_at

    # Learning loop
    for epoch in range(1, args.epoch + 1):

        # training
        train_iter = batch_iter([(x, t) for x, t in zip(X_train, y_train)], args.batchsize, shuffle=True)
        sum_train_loss = 0.
        sum_train_accuracy1 = 0.
        sum_train_accuracy2 = 0.
        K = 0

        for X, t in train_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            t = to_device(args.gpu, np.asarray(t, 'i'))

            # 勾配を初期化
            model.cleargrads()

            # 順伝播させて誤差と精度を算出
            loss, accuracy = model(x, t)
            sum_train_loss += float(loss.data) * len(t)
            sum_train_accuracy1 += float(accuracy.data) * len(t)
            sum_train_accuracy2 += .0
            K += len(t)

            # 誤差逆伝播で勾配を計算
            loss.backward()
            optimizer.update()

        # 訓練データの誤差と,正解精度を表示
        mean_train_loss = sum_train_loss / K
        mean_train_accuracy1 = sum_train_accuracy1 / K
        mean_train_accuracy2 = sum_train_accuracy2 / K
        train_loss.append(mean_train_loss)
        train_accuracy1.append(mean_train_accuracy1)
        train_accuracy2.append(mean_train_accuracy2)
        now = time.time()
        train_throughput = now - cur_at
        cur_at = now

        # evaluation
        test_iter = batch_iter([(x, t) for x, t in zip(X_eval, y_eval)], args.batchsize, shuffle=False)
        sum_test_loss = 0.
        sum_test_accuracy1 = 0.
        sum_test_accuracy2 = 0.
        K = 0

        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            y_true = []
            y_pred = []
            for X, t in test_iter:
                x = to_device(args.gpu, np.asarray(X, 'f'))
                t = to_device(args.gpu, np.asarray(t, 'i'))

                # 順伝播させて誤差と精度を算出
                loss, accuracy = model(x, t)
                sum_test_loss += float(loss.data) * len(t)
                sum_test_accuracy1 += float(accuracy.data) * len(t)
                sum_test_accuracy2 += .0
                K += len(t)

                y = model.predict(x)
                y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
                y_true += t.tolist()

        cm = confusion_matrix(y_true, y_pred)
        cm2 = cm / np.sum(cm, axis=1)
        uar = np.mean(np.diag(cm2))
        sum_test_accuracy2 += uar * K

        # テストデータでの誤差と正解精度を表示
        mean_test_loss = sum_test_loss / K
        mean_test_accuracy1 = sum_test_accuracy1 / K
        mean_test_accuracy2 = sum_test_accuracy2 / K
        test_loss.append(mean_test_loss)
        test_accuracy1.append(mean_test_accuracy1)
        test_accuracy2.append(mean_test_accuracy2)
        now = time.time()
        test_throughput = now - cur_at
        cur_at = now

        logger.info(''
                    '[{:>3d}] '
                    'T/loss={:.6f} '
                    'T/acc1={:.6f} '
                    'T/acc2={:.6f} '
                    'T/sec= {:.6f} '
                    'D/loss={:.6f} '
                    'D/acc1={:.6f} '
                    'D/acc2={:.6f} '
                    'D/sec= {:.6f} '
                    'rate={:.6f}'
                    ''.format(
            epoch,
            mean_train_loss,
            mean_train_accuracy1,
            mean_train_accuracy2,
            train_throughput,
            mean_test_loss,
            mean_test_accuracy1,
            mean_test_accuracy2,
            test_throughput,
            optimizer.rho if args.optim == 'adadelta' else optimizer.lr
        )
        )
        sys.stdout.flush()

        # model と optimizer を保存する
        if args.gpu >= 0: model.to_cpu()
        if mean_test_loss < min_loss:
            min_loss = mean_test_loss
            print('saving early-stopped model (loss) at epoch {}'.format(epoch))
            chainer.serializers.save_npz(os.path.join(model_dir, 'early_stopped-loss.model'), model)
        if mean_test_accuracy2 > best_accuracy:
            best_accuracy = mean_test_accuracy2
            print('saving early-stopped model (uar) at epoch {}'.format(epoch))
            chainer.serializers.save_npz(os.path.join(model_dir, 'early_stopped-uar.model'), model)
        # print('saving final model at epoch {}'.format(epoch))
        chainer.serializers.save_npz(os.path.join(model_dir, 'final.model'), model)
        chainer.serializers.save_npz(os.path.join(model_dir, 'final.state'), optimizer)
        if args.gpu >= 0: model.to_gpu()
        sys.stdout.flush()

        # if args.optim == 'adam':
        #     optimizer.alpha *= lr_decay

        # 精度と誤差をグラフ描画
        if not args.noplot:
            ylim1 = [min(train_loss + test_loss), max(train_loss + test_loss)]
            # ylim2 = [min(train_accuracy1 + test_accuracy1 + train_accuracy2 + test_accuracy2), max(train_accuracy1 + test_accuracy1 + train_accuracy2 + test_accuracy2)]
            ylim2 = [0., 1.]

            # グラフ左
            plt.figure(figsize=(10, 10))

            plt.subplot(1, 2, 1)
            plt.ylim(ylim1)
            plt.plot(range(1, len(train_loss) + 1), train_loss, color='C1', marker='x')
            # plt.grid()
            plt.ylabel('loss')
            plt.legend(['train loss'], loc="lower left")
            plt.twinx()
            plt.ylim(ylim2)
            plt.plot(range(1, len(train_accuracy1) + 1), train_accuracy1, color='C0', marker='x')
            # plt.plot(range(1, len(train_accuracy2) + 1), train_accuracy2, color='C2', marker='x')
            plt.yticks(np.arange(ylim2[0], ylim2[1], .1))
            plt.grid(True)
            # plt.ylabel('accuracy')
            plt.legend(['train acc'], loc="upper right")
            plt.title('Loss and accuracy of train.')

            # グラフ右
            plt.subplot(1, 2, 2)
            plt.ylim(ylim1)
            plt.plot(range(1, len(test_loss) + 1), test_loss, color='C1', marker='x')
            # plt.grid()
            # plt.ylabel('loss')
            plt.legend(['test loss'], loc="lower left")
            plt.twinx()
            plt.ylim(ylim2)
            plt.plot(range(1, len(test_accuracy1) + 1), test_accuracy1, color='C0', marker='x')
            plt.plot(range(1, len(test_accuracy2) + 1), test_accuracy2, color='C2', marker='x')
            plt.yticks(np.arange(ylim2[0], ylim2[1], .1))
            plt.grid(True)
            plt.ylabel('accuracy')
            plt.legend(['test acc', 'test uar'], loc="upper right")
            plt.title('Loss and accuracy of test.')

            plt.savefig('{}.png'.format(args.out))
            # plt.savefig('{}-train.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        cur_at = now

    index2label = {v: k for k, v in labels.items()}
    sorted_labels = [k for k, _ in sorted(labels.items(), key=lambda x: x[1], reverse=False)]

    # test (early_stopped model by loss)
    chainer.serializers.load_npz(os.path.join(model_dir, 'early_stopped-loss.model'), model)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    test_iter = batch_iter([(x, t) for x, t in zip(X_eval, y_eval)], args.batchsize, shuffle=False)

    with chainer.no_backprop_mode(), chainer.using_config('train', False):
        y_true = []
        y_pred = []

        for X, t in test_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            y = model.predict(x)
            y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
            y_true += t

        print("\n==== Confusion matrix 1 (early_stopped-loss) ====\n")
        cm = confusion_matrix([index2label[x] for x in y_true], [index2label[x] for x in y_pred], labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))

        print("\n==== Confusion matrix 2 (early_stopped-loss) ====\n")
        cm2 = cm / np.sum(cm, axis=1).reshape(4, 1)
        uar = np.mean(np.diag(cm2))

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm2):
            print("{}\t{}".format(label, "\t".join(map(lambda x: "%.2f" % x, counts))))

        print("\nUAR = {:.6f}".format(float(uar)))
        sys.stdout.flush()

        # グラフ描画
        if not args.noplot:
            plt.figure()
            plt.imshow(cm2, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm2.shape[0]):
                for j in range(cm2.shape[1]):
                    plt.text(j, i, "{:.2f}".format(cm2[i, j]), horizontalalignment="center", color="white" if cm2[i, j] > cm2.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-cm-early_stopped-loss.png'.format(args.out))
            # plt.savefig('{}-train_cm.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        print("\n==== Classification report (early_stopped-loss) ====\n")
        print(classification_report(
            [sorted_labels[x] for x in y_true],
            [sorted_labels[x] for x in y_pred]
        ))
        sys.stdout.flush()

    # test (early_stopped model by uar)
    chainer.serializers.load_npz(os.path.join(model_dir, 'early_stopped-uar.model'), model)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    test_iter = batch_iter([(x, t) for x, t in zip(X_eval, y_eval)], args.batchsize, shuffle=False)

    with chainer.no_backprop_mode(), chainer.using_config('train', False):
        y_true = []
        y_pred = []

        for X, t in test_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            y = model.predict(x)
            y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
            y_true += t

        print("\n==== Confusion matrix 1 (early_stopped-uar) ====\n")
        cm = confusion_matrix([index2label[x] for x in y_true], [index2label[x] for x in y_pred], labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))

        print("\n==== Confusion matrix 2 (early_stopped-uar) ====\n")
        cm2 = cm / np.sum(cm, axis=1).reshape(4, 1)
        uar = np.mean(np.diag(cm2))

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm2):
            print("{}\t{}".format(label, "\t".join(map(lambda x: "%.2f" % x, counts))))

        print("\nUAR = {:.6f}".format(float(uar)))
        sys.stdout.flush()

        # グラフ描画
        if not args.noplot:
            plt.figure()
            plt.imshow(cm2, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm2.shape[0]):
                for j in range(cm2.shape[1]):
                    plt.text(j, i, "{:.2f}".format(cm2[i, j]), horizontalalignment="center", color="white" if cm2[i, j] > cm2.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-cm-early_stopped-uar.png'.format(args.out))
            # plt.savefig('{}-train_cm.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        print("\n==== Classification report (early_stopped-uar) ====\n")
        print(classification_report(
            [sorted_labels[x] for x in y_true],
            [sorted_labels[x] for x in y_pred]
        ))
        sys.stdout.flush()

    # test (final model)
    chainer.serializers.load_npz(os.path.join(model_dir, 'final.model'), model)
    if args.gpu >= 0:
        model.to_gpu(args.gpu)

    test_iter = batch_iter([(x, t) for x, t in zip(X_eval, y_eval)], args.batchsize, shuffle=False)

    with chainer.no_backprop_mode(), chainer.using_config('train', False):
        y_true = []
        y_pred = []

        for X, t in test_iter:
            x = to_device(args.gpu, np.asarray(X, 'f'))
            y = model.predict(x)
            y_pred += np.argmax(cuda.to_cpu(y.data), axis=1).tolist()
            y_true += t

        print("\n==== Confusion matrix 1 (final model) ====\n")
        cm = confusion_matrix([index2label[x] for x in y_true], [index2label[x] for x in y_pred], labels=sorted_labels)

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm):
            print("{}\t{}".format(label, "\t".join(map(str, counts))))

        print("\n==== Confusion matrix 2 (final model) ====\n")
        cm2 = cm / np.sum(cm, axis=1).reshape(4, 1)
        uar = np.mean(np.diag(cm2))

        print("\t{}".format("\t".join(sorted_labels)))
        for label, counts in zip(sorted_labels, cm2):
            print("{}\t{}".format(label, "\t".join(map(lambda x: "%.2f" % x, counts))))

        print("\nUAR = {:.6f}".format(float(uar)))
        sys.stdout.flush()

        # グラフ描画
        if not args.noplot:
            plt.figure()
            plt.imshow(cm2, interpolation='nearest', cmap=plt.cm.Blues)
            for i in range(cm2.shape[0]):
                for j in range(cm2.shape[1]):
                    plt.text(j, i, "{:.2f}".format(cm2[i, j]), horizontalalignment="center", color="white" if cm2[i, j] > cm2.max() / 2 else "black")
            plt.title('Confusion matrix')
            plt.colorbar()
            tick_marks = np.arange(len(sorted_labels))
            plt.xticks(tick_marks, sorted_labels, rotation=45)
            plt.yticks(tick_marks, sorted_labels)
            plt.tight_layout()
            plt.ylabel('True label')
            plt.xlabel('Predicted label')
            plt.savefig('{}-cm-final.png'.format(args.out))
            # plt.savefig('{}-cm-final.png'.format(os.path.splitext(os.path.basename(__file__))[0]))
            # plt.show()
            plt.close()

        print("\n==== Classification report (final model) ====\n")
        print(classification_report(
            [sorted_labels[x] for x in y_true],
            [sorted_labels[x] for x in y_pred]
        ))
        sys.stdout.flush()
Exemplo n.º 41
0
    np.ctypeslib.ndpointer(dtype=data_t, ndim=1, flags='C_CONTIGUOUS'),
]
lib.stalta.restype = C.c_int


def stalta(data, nsta, nlta):
    # initialize C struct / numpy structed array
    head = np.empty(1, dtype=head_t)
    head[:] = (len(data), nsta, nlta)
    # ensure correct type and countiguous of data
    data = np.require(data, dtype=data_t, requirements=['C_CONTIGUOUS'])
    # all memory should be allocated by python
    charfct = np.empty(len(data), dtype=data_t)
    # run and check the errorcode
    errcode = lib.stalta(head, data, charfct)
    if errcode != 0:
        raise Exception('stalta exited with error code %d' % errcode)
    return charfct


if __name__ == '__main__':
    from obspy.core import read
    import matplotlib.pyplot as plt

    tr = read("/path/to/loc_RJOB20050831023349.z")[0]
    charfct = stalta(tr.data, 400, 2000)
    
    plt.plot(tr.data)
    plt.twinx().plot(charfct, 'r')
    plt.show()
Exemplo n.º 42
0
		# turn on grid
		if opts.is_grid == True:
			plt.grid(True)

		axbot = plt.twiny()
		axbot.xaxis.tick_bottom()
		axbot.xaxis.set_label_position("bottom")
		if opts.is_period_doubled and bin_start == 0 and bin_end == opts.nbins - 1:
			axbot.set_xlim(xmin=opts.phase_start, xmax=2.*opts.phase_end)
		else:
			axbot.set_xlim(xmin=opts.phase_start, xmax=opts.phase_end)
        	for label in axbot.get_xticklabels(): label.set_fontsize(opts.fs)
        	plt.xlabel("Pulse phase", fontsize=opts.fs)
		plt.gca().minorticks_on()

		ayleft = plt.twinx()
		ayleft.yaxis.tick_left()
		ayleft.yaxis.set_label_position("left")
		if (dump_pulses > 0 or dump_time > 0) and not opts.is_events:
			ayleft.set_ylim(ymin=0.0, ymax=ncount*dump_time)
		else:
			ayleft.set_ylim(ymin=0.0, ymax=opts.window_time)
        	for label in ayleft.get_yticklabels(): label.set_fontsize(opts.fs)
        	plt.ylabel("Observing time (s)", fontsize=opts.fs)
		plt.gca().minorticks_on()

		# Determining the main (top/right) axes
		if opts.is_period_doubled and bin_start == 0 and bin_end == opts.nbins - 1:
			ax.set_xlim(xmin=bin_start, xmax=opts.nbins + bin_end-0.1)
		else:
			ax.set_xlim(xmin=bin_start, xmax=bin_end-0.1)
Exemplo n.º 43
0
# Display the new image with 'gray' color map
plt.subplot(2, 1, 1)
plt.title('Equalized image')
plt.axis('off')
plt.imshow(new_image, cmap='gray')

# generate a hist of new pixels
plt.subplot(2, 1, 2)
pdf = plt.hist(new_pixels,
               bins=64,
               range=(0, 256),
               normed=False,
               color='red',
               alpha=0.4)
plt.grid('off')

plt.twinx()
plt.xlim((0, 256))
plt.grid('off')
plt.title('PDF & CDF (equalized image)')

# generate a cumulative hist of new pixels
plt.hist(new_pixels,
         bins=64,
         range=(0, 256),
         normed=True,
         color='blue',
         alpha=.4,
         cumulative=True)
plt.show()
Exemplo n.º 44
0
'''
    Tow y axis in plot with different scale
'''
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

x = np.arange(2, 20, 1)
y1 = x * x
y2 = np.log(x)

plt.plot(x, y1)

plt.twinx()  # add more y axis
plt.plot(x, y2, 'r')

plt.show()
Exemplo n.º 45
0
results

Rec = []
Precision = []
Recall = []
for i in range(0, 9):
    Rec.append(results[i]['K'])
    Precision.append(results[i]['Precision'])
    Recall.append(results[i]['Recall'])

from matplotlib import pyplot as plt
plt.plot(Rec, Precision)
plt.xlabel('# of Recommendations')
plt.ylabel('Precision')
plt2 = plt.twinx()
plt2.plot(Rec, Recall, 'r')
plt.ylabel('Recall')
for tl in plt2.get_yticklabels():
    tl.set_color('r')

#data = Dataset.load_builtin('ml-100k')
trainset = data.build_full_trainset()  #Build on entire data set
algo = SVD(n_factors=factors,
           n_epochs=epochs,
           lr_all=lr_value,
           reg_all=reg_value)
algo.fit(trainset)

# Predict ratings for all pairs (u, i) that are NOT in the training set.
testset = trainset.build_anti_testset()
Exemplo n.º 46
0
    def summary_plot(self):
        '''
            Plot a summary plot for the chemical evolution model.
            The star formation rate, inflow rate, SNIa and SNII rates at the
            solar radius are plotted along with observational constraints from
            Guesten & Mezger et al. (1982) (for the SFR)
            Portinari et al. (1998) (for the inflow)
            and Li et al. (2011) (for the SN rates)
        '''
        plt.figure(figsize=[3., 4.])
        plt.subplot(2, 1, 1)
        plt.plot(self.t, self.SFR, color='k', label='SFR')
        l, = plt.plot(self.t,
                      self.Inflow,
                      color='k',
                      ls='dashed',
                      label='Inflow')
        l.set_dashes((2, 1))
        plt.legend(loc='upper left', bbox_to_anchor=(0.1, 1.0))
        plt.xlabel(r'$t/\,\mathrm{Gyr}$')
        plt.ylabel(
            r'Rate /$\,\mathrm{M}_\odot\,\mathrm{pc}^{-2}\,\mathrm{Gyr}^{-1}$')
        plt.ylim(0., np.max(self.SFR) * 1.4)

        sfr = [6., 4.]  # Guesten & Mezger et al. (1982)
        inf = [0.9, 0.6]  # Portinari et al. (1998)
        plt.errorbar([11.8], [sfr[0]],
                     yerr=[sfr[1]],
                     color=sns.color_palette()[2],
                     fmt='*',
                     markersize=4)
        plt.errorbar([11.8], [inf[0]],
                     yerr=[inf[1]],
                     color=sns.color_palette()[2],
                     fmt='*',
                     markersize=4)

        ax = plt.twinx()
        plt.plot(self.t, self.SNIa, color='k', ls='dotted', label='SNIa')
        l, = plt.plot(self.t, self.SNII, color='k', ls='dashed', label='SNII')
        l.set_dashes((4, 1))
        plt.ylabel(r'Rate /$\,\mathrm{pc}^{-2}\,\mathrm{Gyr}^{-1}$')
        plt.legend(loc='upper right', bbox_to_anchor=(0.9, 1.0))
        plt.ylim(0., plt.ylim()[1] * 1.4)

        typeII = [1.555, 0.285]  # Li et al. (2011)
        typeIa = [0.54, 0.11]  # Li et al. (2011)
        ## Assuming 15kpc disc
        conv = 10. / np.pi / 15.**2
        plt.errorbar([12.2], [typeIa[0] * conv],
                     yerr=[typeIa[1] * conv],
                     color=sns.color_palette()[1],
                     fmt='o',
                     markersize=2)
        plt.errorbar([12.2], [typeII[0] * conv],
                     yerr=[typeII[1] * conv],
                     color=sns.color_palette()[1],
                     fmt='o',
                     markersize=2)
        plt.xlim(0., self.t[-1] * 1.05)
        plt.subplot(2, 1, 2)
        plt.plot(self.R, self.Mstar[-1], color='k', label='Stars')
        l, = plt.plot(self.R, self.Mgas[-1], color='k', ls='', label='Gas')
        l.set_dashes((2, 1))
        plt.legend(loc='upper right', bbox_to_anchor=(0.9, 1.0))
        plt.errorbar([SolarRadius, SolarRadius], [43., 13.],
                     yerr=[5., 3.],
                     fmt='o',
                     markersize=2)
        plt.xlim(2., 16.)
        plt.semilogy()
        plt.xlabel(r'$R/\,\mathrm{kpc}$')
        plt.ylabel(r'Surface Density$/\,\mathrm{M}_\odot\,\mathrm{pc}^{-2}$')
        plt.tight_layout()
Exemplo n.º 47
0
# 设置中文格式为黑体
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False  #解决负号显示为方块的问题

# 读取数据
data = pd.read_excel('data.xlsx')

#  把日期由string字符串格式转为timestamp时间戳格式,方便坐标轴显示
"""
1.先创建空集用于放置转换后的日期
2.
"""
d = []
for i in range(len(data)):
    d.append(datetime.datetime.strptime(data['date'][i], '%Y-%m-%d'))
data['date'] = d  # 将原来的date那一列数据换成新生成的时间戳格式日期

#  数据可视化并设置双坐标轴
plt.plot(data['date'], data['score'], linestyle='--', label='评分')
plt.xticks(rotation=45)  # 设置x轴刻度显示角度
plt.legend(loc='upper left')  # 分数的图例设置在左上角
plt.twinx()  # 设置双坐标轴
plt.plot(data['date'], data['price'], color='red', label='股价')
plt.xticks(rotation=45)
plt.legend(loc='upper right')

plt.gcf().autofmt_xdate()  #自动调整角度

plt.show()
Exemplo n.º 48
0
def gen_svg_tiles(path_dest,
                  catalog,
                  last_hours=(72, 48, 24),
                  color=COLOR_TILES):
    """
    Generate tiles (svg evolution plots of sensor variables) for enerpiweb

    :param str path_dest: IMG_TILES_BASEPATH = os.path.join(BASE_PATH, '..', 'enerpiweb', 'static', 'img', 'generated')
    :param HDFTimeSeriesCatalog catalog: ENERPI data catalog
    :param tuple last_hours: with hour intervals to plot. Default 72, 48, 24 h
    :param tuple color: color for lines in tiles (default: white, (1, 1, 1))
    :return: ok
    :rtype: bool

    """
    def _cut_axes_and_save_svgs(figure,
                                axes,
                                x_lim,
                                delta_total,
                                data_name,
                                tile_gradient_end,
                                tile_gradient_st,
                                preserve_ratio=False):
        for i, lh in enumerate(last_hours):
            file = os.path.join(
                path_dest,
                'tile_{}_{}_last_{}h.svg'.format('enerpi_data', data_name, lh))
            axes.set_xlim(
                (x_lim[0] + delta_total * (1 - lh / total_hours), x_lim[1]))
            figure.set_figwidth(_tile_figsize(lh / total_hours)[0])
            write_fig_to_svg(figure,
                             name_img=file,
                             preserve_ratio=preserve_ratio)
            # if (i + 1 == len(last_hours)):
            if EXPORT_PNG_TILES and (i + 1 == len(last_hours)):
                path_png = file[:-3] + 'png'
                base_path, name_png = os.path.split(path_png)
                figure.set_dpi(216)
                canvas = FigureCanvas(figure)
                canvas.draw()
                # Fusion data + bg:
                data_img = Image.frombytes('RGBA', (900, 600),
                                           canvas.buffer_rgba())
                png_img = _get_png_tile_background(
                    os.path.join(base_path, 'fondo_' + name_png),
                    (data_img.width, data_img.height), tile_gradient_end,
                    tile_gradient_st)
                png_img.paste(data_img, (0, 0), data_img)
                png_img.save(path_png)

    fig = ax = None
    total_hours = last_hours[0]
    last_data, last_data_c = catalog.get(last_hours=last_hours[0],
                                         with_summary=True)
    if (last_data is not None) and (len(last_data) > 2):
        ahora = dt.datetime.now().replace(second=0, microsecond=0)
        xlim = mpd.date2num(ahora - dt.timedelta(
            hours=last_hours[0])), mpd.date2num(ahora)
        delta = xlim[1] - xlim[0]

        for c in SENSORS.columns_sensors_rms:
            fig, ax = _plot_sensor_tile(catalog.resample_data(last_data[c],
                                                              rs_data='5min'),
                                        barplot=False,
                                        ax=ax,
                                        fig=fig,
                                        color=color)
            _cut_axes_and_save_svgs(fig, ax, xlim, delta, c,
                                    SENSORS[c].tile_gradient_end,
                                    SENSORS[c].tile_gradient_st)
            plt.cla()
            fig.set_figwidth(_tile_figsize()[0])

        for c in SENSORS.columns_sensors_mean:
            fig, ax = _plot_sensor_tile(catalog.resample_data(last_data[c],
                                                              rs_data='30s',
                                                              use_median=True),
                                        barplot=False,
                                        ax=ax,
                                        fig=fig,
                                        color=color)
            _cut_axes_and_save_svgs(fig, ax, xlim, delta, c,
                                    SENSORS[c].tile_gradient_end,
                                    SENSORS[c].tile_gradient_st)
            plt.cla()
            fig.set_figwidth(_tile_figsize()[0])

        # (DEBUG Control decay) svg de 'ref'
        # c = SENSORS.ref_rms
        # fig, ax = _plot_sensor_tile(catalog.resample_data(last_data[c], rs_data='30s'),
        #                             barplot=False, ax=ax, fig=fig, color=(0.5922, 0.149, 0.1451))
        # _cut_axes_and_save_svgs(fig, ax, xlim, delta, c, preserve_ratio=True)
        # plt.cla()
        # fig.set_figwidth(_tile_figsize()[0])

        # Consumption tile (€ & kWh)
        if len(last_data_c) > 1:
            fig, ax = _plot_sensor_tile(last_data_c.kWh,
                                        barplot=True,
                                        ax=ax,
                                        fig=fig,
                                        color=color)

            # Append cost data:
            fact = FacturaElec(consumo=last_data_c.kWh)
            last_data_coste = fact.reparto_coste().tz_localize(None)
            last_data_coste.index = last_data_coste.index + dt.timedelta(
                minutes=30)
            coste_max = last_data_coste.max()
            step_c = 0.05
            c_max = max(3 * step_c, np.ceil(coste_max * 100) / 100.)
            yticks = list(np.arange(start=0, stop=c_max, step=step_c))[1:-2]
            ax_2 = plt.twinx(ax)
            ax_2.plot(last_data_coste, color=color, lw=2, ls='-.')
            ax_2.tick_params(axis='y',
                             direction='in',
                             pad=-40,
                             length=3,
                             width=.5,
                             labelsize=FONTSIZE_TILE,
                             labelright=True,
                             colors='k')
            ax_2.set_ylim((0, c_max))
            ax_2.set_yticks(yticks)
            ax_2.set_yticklabels(['{:.02f} €'.format(t) for t in yticks])

            ax.annotate('{:.1f}kWh'.format(last_data_c.kWh.iloc[-25:-1].sum()),
                        (.1, .7),
                        xycoords='axes fraction',
                        verticalalignment='top',
                        alpha=.8,
                        bbox={
                            'edgecolor': color,
                            'pad': 2,
                            'linewidth': 1,
                            'facecolor': color,
                            'alpha': 0.1
                        },
                        horizontalalignment='left',
                        size=3 * FONTSIZE,
                        color='k')
            ax.annotate('{:.2f}€'.format(last_data_coste.iloc[-25:-1].sum()),
                        (.9, .7),
                        xycoords='axes fraction',
                        verticalalignment='top',
                        alpha=.8,
                        bbox={
                            'edgecolor': color,
                            'pad': 2,
                            'linewidth': 1,
                            'facecolor': color,
                            'alpha': 0.1
                        },
                        horizontalalignment='right',
                        size=3 * FONTSIZE,
                        color='k')
            _cut_axes_and_save_svgs(fig, ax, xlim, delta, last_data_c.kWh.name,
                                    (191, 160, 245, 0.27),
                                    (140, 39, 211, 0.83))

        if fig is not None:
            plt.close(fig)
        return True
    else:
        return False
Exemplo n.º 49
0
    tick.set_fontsize(12)
for tick in plt.gca().get_yticklabels():
    tick.set_fontname("Calibri")
    tick.set_fontsize(12)

#Axis labels and formats

# axis 1
color = 'tab:red'
plt.xlabel("Date", fontsize=12)
fig.autofmt_xdate()
plt.ylabel(str(ticker1), color='black', fontsize=12)
plt.plot(x_values_selected, y_values_selected, color=color)
plt.tick_params(axis='y', labelcolor=color)

ax2 = plt.twinx()  # instantiate a second axes that shares the same x-axis

#axis 2
color = 'tab:blue'
ax2.set_ylabel(str(ticker2), color='black',
               fontsize=12)  # we already handled the x-label with ax1
ax2.plot(x_values_selected, ticker_2.get_shareprice_data_y(), color=color)
ax2.tick_params(axis='y', labelcolor=color)

#remove borders
plt.gca().spines['top'].set_visible(False)

#Chart title
plt.title(str(ticker1 + " & " + ticker2 + " share price chart"), fontsize=14)

#Show chart
Exemplo n.º 50
0
        monitor.join()

        with open('feed_forward_data', 'w') as outfile:
            json.dump(dynamics, outfile)
    else:
        with open('feed_forward_data', 'r') as outfile:
            dynamics = json.load(outfile)

    plt.figure()
    fig, ax1 = plt.subplots()
    ax1.set_ylabel('CPU Load (%)', color='b')
    ax1.plot(dynamics['time'], dynamics['cpu'], 'b-')
    ax1.set_xlabel('Time [ms]')
    ax1.grid(True)
    ax1.set_ylim([0, 100])
    for tl in ax1.get_yticklabels():
        tl.set_color('b')

    ax2 = plt.twinx()
    ax2.set_ylabel('Input Load', color='r')
    cpuInput = [
        100 * inverse_cpu_model(x) for x in dynamics['sleepTimeTarget']
    ]
    ax2.plot(dynamics['time'], cpuInput, 'r-')
    ax2.grid(True)
    ax2.set_ylim([0, 100])
    for tl in ax2.get_yticklabels():
        tl.set_color('r')
    plt.savefig('FeedForward.png', dpi=100)
    plt.close()
def gains_chart(loss,
                score,
                num_bins=10,
                title='Gains Chart',
                return_table=True,
                include_scores=True):
    """Show gains chart of binned scores along with KS and Gini."""
    n = len(loss)
    df = pd.DataFrame({'loss': loss, 'score': score})
    df = df.sort_values(by='score')

    # Initialize arrays that store aggregated values
    bin_scores = np.zeros(num_bins)
    bin_losses = np.zeros(num_bins)
    bin_sizes = np.zeros(num_bins)
    virtual_bins = num_bins

    min_score = np.min(score)
    max_score = np.max(score)
    raw_step_size = (max_score - min_score) / num_bins
    break_start = 0
    break_end = num_bins

    breaks = np.linspace(min_score, max_score, virtual_bins + 1)

    for i in np.arange(break_start, break_end):
        bin_scores[i] = np.mean(df['score'][(df['score'] > breaks[i])
                                            & (df['score'] <= breaks[i + 1])])
        bin_losses[i] = np.mean(df['loss'][(df['score'] > breaks[i])
                                           & (df['score'] <= breaks[i + 1])])
        bin_sizes[i] = np.sum((df['score'] > breaks[i])
                              & (df['score'] <= breaks[i + 1]))

    # Convert gains chart table to DataFrame
    gains_chart_df = pd.DataFrame(
        {
            'bin': np.arange(1, num_bins + 1),
            'count': bin_sizes,
            'avg_score': bin_scores,
            'avg_loss': bin_losses
        },
        index=np.arange(1, num_bins + 1))

    gains_chart_df = gains_chart_df[['bin', 'count', 'avg_score', 'avg_loss']]
    #    gains_chart_df['rl_avg_loss'] = gains_chart_df['avg_loss'] / np.mean(df['loss'])
    #    gains_chart_df['rl_avg_score'] = gains_chart_df['avg_score'] / np.mean(df['score'])

    gini = ks_gini(loss, score)
    gini = "{:.3f}".format(gini['gini'])

    if include_scores:
        chart_min = min(np.min(gains_chart_df['avg_score']),
                        np.min(gains_chart_df['avg_loss'])) - .1
        chart_max = max(np.max(gains_chart_df['avg_score']),
                        np.max(gains_chart_df['avg_loss'])) + .1
    else:
        chart_min = np.min(gains_chart_df['avg_loss']) - .1
        chart_max = np.max(gains_chart_df['avg_loss']) + .1

    # Create the plot
    plt.ylim(min(chart_min, 0.0), max(1.0, chart_max))
    plt.title(title + ', Gini: ' + gini)
    plt.grid()
    plt.xticks(np.arange(1, 11))
    plt.plot(gains_chart_df['bin'], gains_chart_df['avg_loss'], zorder=15)
    plt.scatter(gains_chart_df['bin'],
                gains_chart_df['avg_loss'],
                s=15,
                label='_nolegend_',
                zorder=14)
    if include_scores:
        plt.plot(gains_chart_df['bin'], gains_chart_df['avg_score'], zorder=8)
        plt.scatter(gains_chart_df['bin'],
                    gains_chart_df['avg_score'],
                    s=15,
                    label='_nolegend',
                    zorder=7)
    plt.legend(bbox_to_anchor=(1.15, 1), loc=2, borderaxespad=0.)
    plt.ylabel('Avg Actual')
    plt.xlabel('Bin')
    axes2 = plt.twinx()
    axes2.set_ylabel('Count')
    axes2.bar(gains_chart_df['bin'],
              gains_chart_df['count'],
              zorder=2,
              color='grey')
    axes2.set_ylim(0, 3 * np.max(gains_chart_df['count']))

    plt.show()
    plt.close()

    if return_table:
        return gains_chart_df
    return
Exemplo n.º 52
0
    def plot_back_test(self, ma_days):
        for ii in range(len(self.user_stock_code_list)):
            plt.figure(ii)
            ax = plt.gca()

            std = []
            negetive_std = []
            for jj in range(len(self.diviation_from_ma_list[ii])):
                temp_std = np.std(
                    self.diviation_from_ma_list[ii][max(jj - ma_days +
                                                        1, 0):jj + 1])
                std.append(temp_std)
                negetive_std.append(-1.0 * temp_std)

            plt.plot(self.user_stock_uniform_price_list[ii],
                     color="k",
                     linewidth=2.5,
                     label="Price")
            #plt.plot(self.back_test_info[5][ii],color = "b",linewidth =2.5,label = "Return")
            plt.scatter(self.back_test_info[1][ii],
                        self.back_test_info[2][ii],
                        c="r",
                        s=150,
                        marker="s",
                        label="Long")
            plt.scatter(self.back_test_info[3][ii],
                        self.back_test_info[4][ii],
                        c="g",
                        s=150,
                        marker="s",
                        label="Short/Sell")
            plt.xlabel("Time /day")
            plt.ylabel("Return")
            plt.legend(loc="upper left")

            ax_2 = plt.twinx()
            ax_2.plot(self.diviation_from_ma_list[ii],
                      color="y",
                      linewidth=2.5,
                      label="Diviation From MA")
            #ax_2.plot(self.diviation_from_ma_std_list[ii],color = "r",linewidth = 2.5,label= "Positive Std")
            #ax_2.plot(negetive_std,color = "g",linewidth = 2.5,label = "Negative Std")

            #for jj in range(len(self.ma_days_list)):
            ax_2.plot(self.diviation_ma_list[ii],
                      color="c",
                      linewidth=2.5,
                      label=str(self.ma_dict[self.ma_days]) +
                      " MA of Diviation")

            ax_2.legend(loc="upper right")
            ax_2.set_ylabel("Diviation From MA")

            #plt.ylim(0)
            plt.xlim(0)

            xticks = ax.get_xticks()
            offset = xticks[1] - xticks[0]
            date_time = []
            for jj in range(len(xticks)):
                if jj * int(offset) >= len(self.user_stock_datetime_list[ii]):
                    break
                date_time.append(
                    self.user_stock_datetime_list[ii][jj * int(offset)])
            ax.set_xticklabels(date_time, rotation=30)

            if self.short_flag == 1:
                plt.title(self.user_stock_name_list[ii] +
                          "long/short return " + str(ma_days) + " MA")
            else:
                plt.title(self.user_stock_name_list[ii] + "long/hold return " +
                          str(ma_days) + " MA")
            plt.grid(True)
            fig = plt.gcf()
            fig.set_size_inches(24, 15)
            fig.savefig("../input/DeviationFromMA/png_" + str(ma_days) + "/" +
                        self.user_stock_code_list[ii] + "_return.png",
                        dpi=100)
            plt.close(fig)
Exemplo n.º 53
0
    # Lines of constant Jeans Length
    plt.plot(rho, temp_jeans_length(rho, 1e1), color='green', linestyle='-')
    plt.plot(rho, temp_jeans_length(rho, 1e2), color='green', linestyle='-')
    plt.plot(rho, temp_jeans_length(rho, 1e3), color='green', linestyle='-')
    plt.plot(rho, temp_jeans_length(rho, 1e4), color='green', linestyle='-')
    anns = {
        r"$\lambda_J = 10 pc$": (7e1, 1e2),
        r"$\lambda_J = 100 pc$": (7e-1, 1e2),
        r"$\lambda_J = 10^3 pc$": (7e-3, 1e2),
        r"$\lambda_J = 10^4 pc$": (7e-5, 1e2)
    }
    annotate_lines(anns, 1, 'g')

    # Show sound speed as second y axis
    psound = plt.twinx()
    psound.set_ylabel('$c_s (km/s)$')
    temp_dyn_range = temp_range[1] - temp_range[0]
    temp_cs_vals = (np.log10(
        temp_sound_speed(np.array([3e0, 1e1, 3e1, 1e2, 3e2, 1e3, 3e3]))) -
                    temp_range[0]) / temp_dyn_range
    psound.set_yticks(temp_cs_vals)
    psound.set_yticklabels(['3', '10', '30', '100', '300', '1,000', '3,000'])

    # Show free-fall time as second x axis
    pfall = plt.twiny()
    pfall.set_xlabel('$t_{ff} (Myr)$')
    rho_dyn_range = rho_range[1] - rho_range[0]
    rho_ff_vals = (
        np.log10(rho_freefall(np.array([1e0, 1e1, 1e2, 1e3, 1e4, 1e5]))) -
        rho_range[0]) / rho_dyn_range
Exemplo n.º 54
0
    "Running Sim II"
]

plt.ylim(0, 16)
plt.xlim(0, len(reference))
plt.xticks(np.arange(len(reference)), reference)
plt.xticks(rotation=-30, ha="left")
plt.grid(True)
plt.ylabel("Total RAM Memory [GiB]")

plt.legend(lines, lines_legend)
first_legend = plt.legend(lines, lines_legend)
second_legend = plt.legend(patches, patches_legend, loc="center left")
plt.gca().add_artist(first_legend)

second_ax = plt.twinx()
second_ax.set_ylabel("Total RAM Memory [%]")
second_ax.set_ylim(0, 100)
second_ax.set_zorder(0)

mng = plt.get_current_fig_manager()
mng.window.showMaximized()
plt.tight_layout()

plt.savefig(plot_file("AllTotalRAM.png"), bbox_inches='tight')

#%% COMPARE SIMULATIONS I AND II

crossed_reference = [
    "Inicial", "mp.Simulation", "Flujo", "sim.init_sim()", "load_midflux()",
    "Flujo negado", "Principio", "Mitad", "Final"
Exemplo n.º 55
0
def find_saddle(data,first_guess=240,nbins=50,xlims=[100,320],method='fmin',\
                addtests=True,showplot=False,figd=False):
    '''Function to find the saddle between to peaks in a bimodal distribution.
       Usage: Best to plot a histogram of data first in order to make a good
              first guess
              The implemented deldxmin method has OLR typical proximity
              thresholds relative to first_guess value.
    '''
    import scipy.interpolate as spi
    data = np.nan_to_num(data)
    #if showplot:
    #    f,[ax1,ax2] = plt.subplots(2,1)
    #    plt.axes(ax1)
    plt.figure(num='hist')
    hy, hx, hbars = plt.hist(data.ravel(),
                             bins=nbins,
                             normed=True,
                             color='0.5',
                             ec='k')
    plt.xlim(xlims)
    plt.ylabel('Raw Data')
    plt.yticks(np.arange(0.002, 0.016, 0.004))
    Interpolator = spi.interp1d(hx[1:], hy, kind='cubic')
    if method == 'deldxmin':
        hires_x = np.linspace(xlims[0], xlims[1], 2000)
        y = Interpolator(hires_x)
        dx = y[1:] - y[:-1]
        ddx = dx[1:] - dx[:-1]
        if showplot:
            plt.axes(ax2)
            plt.plot(hires_x[:-1], dx, 'b')
            plt.plot(hires_x[:-1], np.abs(dx), 'g')
            plt.plot([hires_x[0], hires_x[-1]], [0, 0], 'b--')
            plt.twinx()
            plt.plot(hires_x[1:-1], ddx, 'r')
            plt.plot([hires_x[0], hires_x[-1]], [0, 0], 'r--')
            plt.axes(ax1)
        iddx_mask = ddx <= 0
        data1, data2 = first_guess - 30, first_guess + 30
        proximity_mask = (hires_x[1:-1] < data1) | (hires_x[1:-1] > data2)
        msk = iddx_mask | proximity_mask
        idx_masked = np.ma.MaskedArray(np.abs(dx[1:]), mask=msk)
        idx_zero = idx_masked.argmin()
        olr_saddle = hires_x[idx_zero]
    elif method == 'fmin':
        import scipy.optimize as spo
        [olr_saddle] = spo.fmin(Interpolator,
                                first_guess,
                                full_output=0,
                                disp=0)
        olr_saddle = round(olr_saddle)
    if addtests:
        lower = olr_saddle - 5
        upper = olr_saddle + 5
    if showplot:
        plt.plot(olr_saddle, 0, 'k^', markersize=30)
        if addtests:
            plt.plot((lower, lower), (0, 0.014), 'k', lw=2)
            plt.plot((upper, upper), (0, 0.014), 'k', lw=2)
        figname = figd + 'olrhist_autothresh_' + str(olr_saddle) + '.png'
        plt.savefig(figname, dpi=150)
        plt.close()
    else:
        plt.close()
    return olr_saddle
Exemplo n.º 56
0
plt.subplot(2, 3, 4)
plt.plot(np.fft.rfftfreq(len(beam_x), d=1.), np.abs(np.fft.rfft(beam_x)))
plt.ylabel('Amplitude')
plt.xlabel('Qx')
plt.subplot(2, 3, 5)
plt.plot(np.fft.rfftfreq(len(beam_y), d=1.), np.abs(np.fft.rfft(beam_y)))
plt.ylabel('Amplitude')
plt.xlabel('Qy')
plt.subplot(2, 3, 6)
plt.plot(np.fft.rfftfreq(len(beam_z), d=1.), np.abs(np.fft.rfft(beam_z)))
plt.xlim(0, 0.1)
plt.ylabel('Amplitude')
plt.xlabel('Qz')

fig, axes = plt.subplots(3, figsize=(16, 8), tight_layout=True)
twax = [plt.twinx(ax) for ax in axes]
axes[0].plot(sx, label=r'$\sigma_x$')
twax[0].plot(epsx, '-g', label=r'$\varepsilon_x$')
axes[0].set_xlabel('Turns')
axes[0].set_ylabel(r'$\sigma_x$')
twax[0].set_ylabel(r'$\varepsilon_x$')
axes[1].plot(sy, label=r'$\sigma_y$')
twax[1].plot(epsy, '-g', label=r'$\varepsilon_y$')
axes[1].set_xlabel('Turns')
axes[1].set_ylabel(r'$\sigma_y$')
twax[1].set_ylabel(r'$\varepsilon_y$')
axes[2].plot(sz, label=r'$\sigma_z$')
twax[2].plot(epsz, '-g', label=r'$\varepsilon_z$')
axes[2].set_xlabel('Turns')
axes[2].set_ylabel(r'$\sigma_z$')
twax[2].set_ylabel(r'$\varepsilon_z$')
## Berm dates
plt.axvline(datetime.datetime(2019, 9, 5), color='k', alpha=.8, ls=':')
plt.axvline(x=datetime.datetime(2019, 12, 4),
            ymin=0,
            ymax=.83,
            color='k',
            alpha=.8,
            ls=':')
plt.axvline(x=datetime.datetime(2019, 12, 4),
            ymin=0.96,
            ymax=1,
            color='k',
            alpha=.8,
            ls=':')

ax2 = plt.twinx(plt.gca())
plt.plot(flow.index,
         flow.logflow,
         lw=1.75,
         color='#00264d',
         label='Discharge',
         zorder=1)  ## Scott
plt.ylabel('Discharge (log$_{10}$ft$^3$/s)',
           rotation=270,
           ha='center',
           va='baseline',
           rotation_mode='anchor')
plt.ylim(1, 3)

plt.xlim(dr[0], dr[-1])
offset = 7
def make_plot(Path, Parameters):

    #create blank figure
    plt.figure(1, figsize=(12, 6))
    ax1 = plt.subplot(111)
    ax2 = plt.twinx()

    OffsetX = [0, 500, 800, 0, 600, 200, 350, 900, 0]
    OffsetZ = [50, 70, 90, 0, 30, 40, 80, 30, 120]

    for i in range(0, len(Parameters[0])):

        #Declare filenames
        MorphologyFileName = Path + "ShoreProfile_G1_T_" + str(
            Parameters[0][i]) + "_H_" + str(Parameters[1][i]) + "_W_" + str(
                Parameters[2][i]) + "_R_" + str(
                    Parameters[3][i]) + "_Br_1_Bo_1.xz"
        ConcentrationsFileName = Path + "Concentrations_G1_T_" + str(
            Parameters[0][i]) + "_H_" + str(Parameters[1][i]) + "_W_" + str(
                Parameters[2][i]) + "_R_" + str(
                    Parameters[3][i]) + "_Br_1_Bo_1.xn"

        #First plot the morphology through time
        # declare the file and the axis
        f = open(MorphologyFileName, 'r')
        Lines = f.readlines()
        NoLines = len(Lines)
        EndTime = float(Lines[-1].strip().split(" ")[0])

        # Get info on vertical from header
        Header = np.array(Lines[0].strip().split(" "), dtype=np.float)
        CliffHeight = Header[0]
        dz = Header[1]

        Line = (Lines[-1].strip().split(" "))
        Time = float(Line[0])

        #Read morphology
        X = np.array(Line[1:], dtype="float64") + OffsetX[i]
        NValues = len(X)
        Z = np.arange(0, NValues) * -dz + CliffHeight + OffsetZ[i]

        #First plot the morphology through time
        # declare the file and the axis
        f = open(ConcentrationsFileName, 'r')
        Lines = f.readlines()
        NoLines = len(Lines)
        dx = float(Lines[1].strip().split()[0])
        print dx
        #Read concentrations
        Line = (Lines[-1].strip().split(" "))
        N = np.array(Line[1:], dtype="float64") + OffsetZ[i] * 1000
        Xn = np.arange(0, len(N)) * dx + OffsetX[i]
        Mask = N != N[-1]
        N = N[Mask]
        Xn = Xn[Mask]

        ax1.plot(X, Z, 'k-', lw=1.5)
        ax1.plot([X[0], X[-1]], [OffsetZ[i], OffsetZ[i]], 'b-')
        ax2.plot(Xn, N, 'r-', lw=1.5)

        ax1.text(
            X[-1] + 20,
            Z[-1] - 12,
            ("Tr = " + str(Parameters[0][i]) + " m\n" + "H = " +
             str(Parameters[1][i]) + " m\n" + "W = " + str(Parameters[2][i]) +
             "\n" + "R = " + str(Parameters[3][i])),
            fontsize=8)

    # tweak the plot
    ax1.set_xlabel("Distance (m)")
    ax1.set_ylabel("Relative Elevation (m)")
    ax1.set_xlim(-100, 1100)
    ax1.set_ylim(-25, 150)
    ax2.set_ylim(-25000, 150000)
    ax2.set_ylabel("Relative Concentration (atoms g$^{-1}$)")
    plt.tight_layout()
    plt.savefig("CRN_ensemble.png")
    plt.savefig("CRN_ensemble.pdf")
Exemplo n.º 59
0
x = time
y = b_field_freq
yerr = 1/np.sqrt(4*100)


params = lmfit.Parameters()

params.add('A', value = 0.5)
params.add('freq', value = 1/(3600*12), vary = False)
params.add('phase', value = 0.0)
params.add('offset', value = 0.0)

result = lmfit.minimize(cosine_fit, params, args = (x, y, yerr))

fit_values  = y + result.residual

lmfit.report_errors(params)

x_plot = np.linspace(x.min(),x.max(),1000)

figure = pyplot.figure(1)
figure.clf()

pyplot.plot(x,(b_field-np.average(b_field))*1000,'o')
ax2 = pyplot.twinx()
ax2.plot(x,b_field_freq*1000,'o', markersize = 0.0)
ax2.plot(x_plot,cosine_model(params,x_plot)*1000,linewidth = 3.0)

pyplot.show()
Exemplo n.º 60
0
def create_plot_image(mark_inst, work_point=None):

    truncate_koef = 1.2

    path1 = '/static/images/pq_and_eff.png'
    path2 = '/static/images/npsh.png'
    path3 = '/static/images/p2.png'

    curves = Curves(mark_inst, interplolate_curves=True)

    plt.clf()  # clean plots

    # plot H-Q curve
    plt.plot(curves.q_points, curves.h_points)

    # plot workpoint and load H-Q curve
    if work_point:

        curves.compute_work_parameters(work_point)

        # plot work_point
        plt.plot(curves.work_point[0], curves.work_point[1], 'ro')

        if curves.q_wp:

            q_points = curves.q_points
            # compute index to truncate load curve
            i = 0
            while q_points[i] < curves.q_wp:
                i += 1
            i = round(i * truncate_koef)
            # plot curve truncated
            plt.plot(q_points[:i], curves.h_load_points[:i])
            plt.plot(curves.q_wp, curves.h_wp, 'ro')

    # plot eff-Q curve
    ax_eff = plt.twinx()
    ax_eff.plot(curves.q_points, curves.eff_points)
    # get efficiency max value
    eff_max = max(curves.eff_points)
    # set plot y lim(it
    ax_eff.set_ylim((0, eff_max * 3))
    plt.savefig('Main/' + path1)

    # plot npsh-Q curve
    plt.clf()
    plt.plot(curves.q_points, curves.npsh_points)
    plt.savefig('Main/' + path2)

    # plot p2-Q curve
    plt.clf()
    plt.plot(curves.q_points, curves.p2_points)
    plt.savefig('Main/' + path3)

    curves_data = {'img1': path1, 'img2': path2, 'img3': path3}

    if curves.q_wp != None:

        extra = {
            'q_wp': formatted(curves.q_wp),
            'h_wp': formatted(curves.h_wp),
            'eff_wp': formatted(curves.eff_wp),
            'npsh_wp': formatted(curves.npsh_wp),
            'p2_wp': formatted(curves.p2_wp)
        }

        curves_data.update(extra)

    return curves_data