def run_method_usage(methods,cases):
    methods = [m[0] for m in methods]
    # Bootstrap the percentage error bars:
    percents =[]
    for i in range(10000):
        nc = resample(cases)
        percents.append(100*np.sum(nc,axis=0)/len(nc))
    percents=np.array(percents)
    mean_percents = np.mean(percents,axis=0)
    std_percents = np.std(percents,axis=0)*1.96
    inds=np.argsort(mean_percents).tolist()
    inds.reverse()
    avg_usage = np.mean(mean_percents)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    x=np.arange(len(methods))
    ax.plot(x,[avg_usage]*len(methods),'-',color='0.25',lw=1,alpha=0.2)
    ax.bar(x, mean_percents[inds], 0.6, color=paired[0],linewidth=0,
           yerr=std_percents[inds],ecolor=paired[1])
    #ax.set_title('Method Occurrence')
    ax.set_ylabel('Occurrence %',fontsize=30)
    ax.set_xlabel('Method',fontsize=30)
    ax.set_xticks(np.arange(len(methods)))
    ax.set_xticklabels(np.array(methods)[inds],fontsize=8)
    fig.autofmt_xdate()
    fix_axes()
    plt.tight_layout()
    fig.savefig(figure_path+'method_occurrence.pdf', bbox_inches=0)
    fig.show()
    return inds,mean_percents[inds]
Exemple #2
0
    def do_plot_extras(self, extra):
        """ Plot other observed quantities as a function of time.

        Parameters
        ----------
        extra: string
          One of the quantities available in system.extras
        """
        # import pyqtgraph as pg

        colors = 'bgrcmykw' # lets hope for less than 9 data-sets
        t = self.time

        # handle inexistent field
        if extra not in self.extras._fields:
          from shell_colors import red
          msg = red('ERROR: ') + 'The name "%s" is not available in extras.\n' % extra
          clogger.fatal(msg)
          return

        i = self.extras._fields.index(extra) # index corresponding to this quantity

        plt.figure()
        # p = pg.plot()
        plt.plot(t, self.extras[i], 'o', label=extra)
        
        plt.xlabel('Time [days]')
        plt.ylabel(extra + ' []')
        plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05))
        plt.minorticks_on()
        plt.tight_layout()
        plt.show()
Exemple #3
0
    def _plot(self, doFAP=None):
      """
        Create a plot.
      """
      xlabel = 'Period [d]'
      ylabel = 'Power'

      self.fig = plt.figure()
      self.ax = self.fig.add_subplot(1,1,1)
      self.ax.set_title("Normalized periodogram")
      self.ax.set_xlabel(xlabel)
      self.ax.set_ylabel(ylabel)
      self.ax.semilogx(1./self.freq, self.power, 'b-')
      # plot FAPs
      if doFAP is None: 
        pass
      elif doFAP is True: # do default FAPs of 10%, 1% and 0.1%
        pmin = 1./self.freq.min()
        pmax = 1./self.freq.max()
        plvl1 = self.powerLevel(0.1) # 10% FAP
        plvl2 = self.powerLevel(0.01) # 1% FAP
        plvl3 = self.powerLevel(0.001) # 0.1% FAP
        self.ax.semilogx([pmin, pmax],[plvl1, plvl1],'k-')
        self.ax.semilogx([pmin, pmax],[plvl2, plvl2],'k--')
        self.ax.semilogx([pmin, pmax],[plvl3, plvl3],'k:')

      plt.tight_layout()
      plt.show()
Exemple #4
0
    def do_plot_obs(self):
        """ Plot the observed radial velocities as a function of time.
        Data from each file are color coded and labeled.
        """
        # import pyqtgraph as pg

        colors = 'bgrcmykw' # lets hope for less than 9 data-sets
        t, rv, err = self.time, self.vrad, self.error # temporaries
        
        plt.figure()
        # p = pg.plot()
        # plot each files' values
        for i, (fname, [n, nout]) in enumerate(sorted(self.provenance.iteritems())):
            m = n-nout # how many values are there after restriction
            
            # e = pg.ErrorBarItem(x=t[:m], y=rv[:m], \
            #                     height=err[:m], beam=0.5,\
            #                     pen=pg.mkPen(None))
                                # pen={'color': 0.8, 'width': 2})
            # p.addItem(e)
            # p.plot(t[:m], rv[:m], symbol='o')
            plt.errorbar(t[:m], rv[:m], yerr=err[:m], \
                         fmt='o'+colors[i], label=fname)
            t, rv, err = t[m:], rv[m:], err[m:]
        
        plt.xlabel('Time [days]')
        plt.ylabel('RV [km/s]')
        plt.legend()
        plt.tight_layout()
        plt.show()
Exemple #5
0
    def save(self, plot_filename, max_col=2):
        fig = plt.figure()
        fig.suptitle(self.title, fontsize=8)

        if len(self.datastore) <= max_col:
            colsz = len(self.datastore)
            rowsz = 1
            for i, d in enumerate(self.datastore):
                if len(d) == 0:
                    continue  # empty space
                ax = fig.add_subplot(rowsz, colsz, i + 1)
                self._plot(i, ax)
        else:
            sz = len(self.datastore)
            colsz = max_col
            rowsz = sz / max_col
            rowsz = rowsz if sz % max_col == 0 else rowsz + 1
            for i, d in enumerate(self.datastore):
                if len(d) == 0:
                    continue  # empty space
                ax = fig.add_subplot(rowsz, colsz, i + 1)
                self._plot(i, ax)

        #plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
        plt.tight_layout()
        plt.savefig(plot_filename)

        return self
Exemple #6
0
def plot_ss_scatter(steadies):
    """ Plot scatter plots of steady states
    """

    def do_scatter(i, j, ax):
        """ Draw single scatter plot
        """
        xs, ys = utils.extract(i, j, steadies)
        ax.scatter(xs, ys)

        ax.set_xlabel(r"$S_%d$" % i)
        ax.set_ylabel(r"$S_%d$" % j)

        cc = utils.get_correlation(xs, ys)
        ax.set_title(r"Corr: $%.2f$" % cc)

    dim = steadies.shape[1]
    fig, axarr = plt.subplots(1, int((dim ** 2 - dim) / 2), figsize=(20, 5))

    axc = 0
    for i in range(dim):
        for j in range(dim):
            if i == j:
                break
            do_scatter(i, j, axarr[axc])
            axc += 1

    plt.suptitle("Correlation overview")

    plt.tight_layout()
    save_figure("images/correlation_scatter.pdf", bbox_inches="tight")
    plt.close()
Exemple #7
0
def plot(rho, u, uLB, tau, rho_history, zdjecia, image, nx, maxIter ):
#    plt.figure(figsize=(15,15))
#    plt.subplot(4, 1, 1)
#    plt.imshow(u[1,:,0:50],vmin=-uLB*.15, vmax=uLB*.15, interpolation='none')#,cmap=cm.seismic
#    plt.colorbar()
    plt.rcParams["figure.figsize"] = (15,15)
    plt.subplot(5, 1, 1)
    plt.imshow(sqrt(u[0]**2+u[1]**2),vmin=0, vmax=uLB*1.6)#,cmap=cm.seismic
    plt.colorbar()
    plt.title('tau = {:f}'.format(tau))      
    
    plt.subplot(5, 1, 2)
    plt.imshow(u[0,:,:30],  interpolation='none')#,cmap=cm.seismicvmax=uLB*1.6,
    plt.colorbar()
    plt.title('tau = {:f}'.format(tau))  
    
    plt.subplot(5, 1, 3)
    plt.imshow(rho, interpolation='none' )#,cmap=cm.seismic
    plt.title('rho')   
    
    plt.subplot(5, 1,4)
    plt.title(' history rho')
    plt.plot(linspace(0,len(rho_history),len(rho_history)),rho_history)
    plt.xlim([0,maxIter])   
    
    plt.subplot(5, 1,5)
    plt.title(' u0 middle develop')
    plt.plot(linspace(0,nx,len(u[0,20,:])), u[1,20,:])
    plt.tight_layout()                  
    
    plt.savefig(path.join(zdjecia,'f{0:06d}.png'.format(image)))
    plt.clf();
        
Exemple #8
0
def plot_confusion_matrix(cm, title='', cmap=plt.cm.Blues):
    #print cm
    #display vehicle, idle, walking accuracy respectively
    #display overall accuracy
    print type(cm)
   # plt.figure(index
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    #plt.figure("")
    plt.title("Confusion Matrix")
    plt.colorbar()
    tick_marks = [0,1,2]
    target_name = ["driving","idling","walking"]


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

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

    for i in range(0,3):
        for j in range(0,3):
         plt.text(i,j,str(cm[i,j]))
    plt.tight_layout()
    plt.ylabel("Actual Value")
    plt.xlabel("Predicted Outcome")
def plot_batch(color_model, q_ab, X_batch_black, X_batch_color, batch_size, h, w, nb_q, epoch):

    # Format X_colorized
    X_colorized = color_model.predict(X_batch_black / 100.)[:, :, :, :-1]
    X_colorized = X_colorized.reshape((batch_size * h * w, nb_q))
    X_colorized = q_ab[np.argmax(X_colorized, 1)]
    X_a = X_colorized[:, 0].reshape((batch_size, 1, h, w))
    X_b = X_colorized[:, 1].reshape((batch_size, 1, h, w))
    X_colorized = np.concatenate((X_batch_black, X_a, X_b), axis=1).transpose(0, 2, 3, 1)
    X_colorized = [np.expand_dims(color.lab2rgb(im), 0) for im in X_colorized]
    X_colorized = np.concatenate(X_colorized, 0).transpose(0, 3, 1, 2)

    X_batch_color = [np.expand_dims(color.lab2rgb(im.transpose(1, 2, 0)), 0) for im in X_batch_color]
    X_batch_color = np.concatenate(X_batch_color, 0).transpose(0, 3, 1, 2)

    list_img = []
    for i, img in enumerate(X_colorized[:min(32, batch_size)]):
        arr = np.concatenate([X_batch_color[i], np.repeat(X_batch_black[i] / 100., 3, axis=0), img], axis=2)
        list_img.append(arr)

    plt.figure(figsize=(20,20))
    list_img = [np.concatenate(list_img[4 * i: 4 * (i + 1)], axis=2) for i in range(len(list_img) / 4)]
    arr = np.concatenate(list_img, axis=1)
    plt.imshow(arr.transpose(1,2,0))
    ax = plt.gca()
    ax.get_xaxis().set_ticks([])
    ax.get_yaxis().set_ticks([])
    plt.tight_layout()
    plt.savefig("../../figures/fig_epoch%s.png" % epoch)
    plt.clf()
    plt.close()
def plot_ga_cnn(data, save=False, save_dest=None):
    plt.figure()

    # generation and all time best score plot
    # plt.subplot(211)
    # plt.title("GA CNN tuner on dataset D2")
    # plt.plot(data["dataset1"]["gen_best_score"], label="Generation Best")
    # plt.plot(data["dataset1"]["all_time_best_score"], label="All Time best")
    # plt.xlabel("Generation")
    # plt.ylabel("Score")
    # plt.xticks(range(0, 20))
    # plt.xlim([0, 9])
    # plt.ylim([0, 0.01])
    # plt.legend(loc=0)

    # plt.subplot(212)
    # plt.title("GA CNN tuner on dataset D3")
    plt.plot(data["gen_best_score"], label="Generation Best")
    plt.plot(data["all_time_best_score"], label="All Time best")
    plt.xlabel("Generation")
    plt.ylabel("Score")
    plt.xticks(range(0, 20))
    plt.xlim([0, 9])
    plt.ylim([0, 0.01])
    plt.legend(loc=0)

    # show plot or save as picture
    if save is False:
        plt.show()
    else:
        if save_dest is None:
            raise RuntimeError("save_dest not set!!")
        plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
        plt.savefig(save_dest, dpi=200)
Exemple #11
0
 def ACF_PACF_plot(self):
     #plot ACF and PACF to find the number of terms needed for the AR and MA in ARIMA
     # ACF finds MA(q): cut off after x lags 
     # and PACF finds AR (p): cut off after y lags 
     # in ARIMA(p,d,q) 
     lag_acf = acf(self.ts_log_diff, nlags=20)
     lag_pacf = pacf(self.ts_log_diff, nlags=20, method='ols')
     
     #Plot ACF:
     ax=plt.subplot(121)
     plt.plot(lag_acf)
     ax.set_xlim([0,5])
     plt.axhline(y=0,linestyle='--',color='gray')
     plt.axhline(y= -1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
     plt.axhline(y= 1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
     plt.title('Autocorrelation Function')
     
     #Plot PACF:
     plt.subplot(122)
     plt.plot(lag_pacf)
     plt.axhline(y=0,linestyle='--',color='gray')
     plt.axhline(y= -1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
     plt.axhline(y=1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
     plt.title('Partial Autocorrelation Function')
     plt.tight_layout()
Exemple #12
0
def plotRttDistribution(rttEstimates, ip, filename, nbBins=500, logscale=False):
    """Plot the RTT distribution of an IP address

    :rttEstimates: pandas DataFrame containing the RTT estimations
    :ip: IP address to plot
    :filename: Filename for the plot
    :nbBins: Number of bins in the histogram
    :logscale: Plot RTTs in logscale if set to True
    :returns: None

    """

    if logscale:
        data = np.log10(rttEstimates[rttEstimates.index == ip].rtt)
    else:
        data = rttEstimates[rttEstimates.index == ip].rtt

    h, b=np.histogram(data, nbBins, normed=True)
    plt.figure(1, figsize=(9, 3))
    plt.clf()
    ax = plt.subplot()
    x = b[:-1]
    ax.plot(x, h, "k")
    ax.grid(True)
    plt.title("%s (%s RTTs)" % (ip, len(data)))
    if logscale:
        plt.xlabel("log10(RTT)")
    else:
        plt.xlabel("RTT")
    plt.ylabel("pdf")
    minorLocator = mpl.ticker.MultipleLocator(10)
    ax.xaxis.set_minor_locator(minorLocator)
    plt.tight_layout()
    plt.savefig(filename)
Exemple #13
0
def test(args):
    data = multivariate_normal([0, 0], [[1, 2], [2, 5]], int(args[1]))
    print(data)
    # PCA
    result = pca(data, base_num=int(args[2]))
    pc_base = result[0]
    print(pc_base)

    # Plotting
    fig = plt.figure()
    fig.add_subplot(1, 1, 1)
    plt.axvline(x=0, color="#000000")
    plt.axhline(y=0, color="#000000")
    # Plot data
    plt.scatter(data[:, 0], data[:, 1])
    # Draw the 1st principal axis
    pc_line = sp.array([-3.0, 3.0]) * (pc_base[1] / pc_base[0])
    plt.arrow(0, 0, -pc_base[0] * 2, -pc_base[1] * 2, fc="r", width=0.15, head_width=0.45)
    plt.plot([-3, 3], pc_line, "r")
    # Settings
    plt.xticks(size=15)
    plt.yticks(size=15)
    plt.xlim([-3, 3])
    plt.tight_layout()
    plt.show()
    plt.savefig("image.png")

    return 0
Exemple #14
0
    def plot_corner_posteriors(self, savefile=None, labels=["T1", "R1", "Av", "T2", "R2"]):
        '''
        Plots the corner plot of the MCMC results.
        '''
        ndim = len(self.sampler.flatchain[0,:])
        chain = self.sampler
        samples = chain.flatchain
        
        samples = samples[:,0:ndim]  
        plt.figure(figsize=(8,8))
        fig = corner.corner(samples, labels=labels[0:ndim])
        plt.title("MJD: %.2f"%self.mjd)
        name = self._get_save_path(savefile, "mcmc_posteriors")
        plt.savefig(name)
        plt.close("all")
        

        plt.figure(figsize=(8,ndim*3))
        for n in range(ndim):
            plt.subplot(ndim,1,n+1)
            chain = self.sampler.chain[:,:,n]
            nwalk, nit = chain.shape
            
            for i in np.arange(nwalk):
                plt.plot(chain[i], lw=0.1)
                plt.ylabel(labels[n])
                plt.xlabel("Iteration")
        name_walkers = self._get_save_path(savefile, "mcmc_walkers")
        plt.tight_layout()
        plt.savefig(name_walkers)
        plt.close("all")  
Exemple #15
0
	def PSTH(self):
	
			
		TimeRes = np.array([0.1,0.25,0.5,1,2.5,5.0,10.0,25.0,50.0,100.0])

		Projection_PSTH = np.zeros((2,len(TimeRes)))
		for i in range(0,len(TimeRes)):
			Data_Hist,STA_Hist,Model_Hist,B = Hist(TimeRes[i])
			data = Data_Hist/np.linalg.norm(Data_Hist)
			sta = STA_Hist/np.linalg.norm(STA_Hist)
			model = Model_Hist/np.linalg.norm(Model_Hist)
			Projection_PSTH[0,i] = np.dot(data,sta)
			Projection_PSTH[1,i] = np.dot(data,model)
			
		import matplotlib.font_manager as fm
		
		plt.figure()
		plt.semilogx(TimeRes,Projection_PSTH[0,:],'gray',TimeRes,Projection_PSTH[1,:],'k',
			     linewidth=3, marker='o', markersize = 12)
		plt.xlabel('Time Resolution, ms',fontsize=25)
		plt.xticks(fontsize=25)
		#plt.axis["right"].set_visible(False)
		plt.ylabel('Projection onto PSTH',fontsize=25)
		plt.yticks(fontsize=25)
		prop = fm.FontProperties(size=20)
		plt.legend(('1D model','2D model'),loc='upper left',prop=prop)
		plt.tight_layout()
		plt.show()
    def plot_graph(self):
        '''
        plots a matplotlib graph from the stocks data. Dates on the x axis and
        Closing Prices on the y axis. Then adds it to the graph_win in the
        display frame as a tk widget()
        '''
        x_axis = [
			dt.datetime.strptime(self.daily_data[day][0], '%Y-%m-%d')
            for day in range(1, len(self.daily_data) - 1)
		]
        y_axis = [
			self.daily_data[cls_adj][-1]
            for cls_adj in range(1, len(self.daily_data) - 1)
		]
        fig = plt.figure()
        ax = fig.add_subplot("111")
        ax.plot(
			x_axis,
            y_axis,
            marker='h',
            linestyle='-.',
            color='r',
            label='Daily Adjusted Closing Prices'
		)
        labels = ax.get_xticklabels()
        for label in labels:
            label.set_rotation(15)
        plt.xlabel('Dates')
        plt.ylabel('Close Adj')
        plt.legend()
        plt.tight_layout()  # adjusts the graph to fit in the space its limited to
        self.data_plot = FigureCanvasTkAgg(fig, master=self.display)
        self.data_plot.show()
        self.graph_win = self.data_plot.get_tk_widget()
Exemple #17
0
	def behavioral_analysis(self):
		"""some analysis of the behavioral data, such as mean percept duration, 
		dominance ratio etc"""
		self.assert_data_intern()
		# only do anything if this is not a no report trial
		if 'RP' in self.file_alias:
			all_percepts_and_durations = [[],[]]
		else:
			all_percepts_and_durations = [[],[],[]]
		if not 'NR' in self.file_alias: #  and not 'RP' in self.file_alias
			for x in range(len(self.trial_indices)):
				if len(self.events) != 0:
					events_this_trial = self.events[(self.events['EL_timestamp'] > self.timestamps_pt[x][0]) & (self.events['EL_timestamp'] < self.timestamps_pt[x][-1])]
					for sc, scancode in enumerate(self.scancode_list):
						percept_start_indices = np.arange(len(events_this_trial))[np.array(events_this_trial['scancode'] == scancode)]
						percept_end_indices = percept_start_indices + 1
						
						# convert to times
						start_times = np.array(events_this_trial['EL_timestamp'])[percept_start_indices] - self.timestamps_pt[x,0]
						if len(start_times) > 0:
							if percept_end_indices[-1] == len(events_this_trial):
								end_times = np.array(events_this_trial['EL_timestamp'])[percept_end_indices[:-1]] - self.timestamps_pt[x,0]
								end_times = np.r_[end_times, len(self.from_zero_timepoints)]
							else:
								end_times = np.array(events_this_trial['EL_timestamp'])[percept_end_indices] - self.timestamps_pt[x,0]

							these_raw_event_times = np.array([start_times + self.timestamps_pt[x,0], end_times + self.timestamps_pt[x,0]]).T
							these_event_times = np.array([start_times, end_times]).T + x * self.trial_duration * self.sample_rate
							durations = np.diff(these_event_times, axis = -1)

							all_percepts_and_durations[sc].append(np.hstack((these_raw_event_times, these_event_times, durations)))

			self.all_percepts_and_durations = [np.vstack(apd) for apd in all_percepts_and_durations]

			# last element is duration, sum inclusive and exclusive of transitions
			total_percept_duration = np.concatenate([apd[:,-1] for apd in self.all_percepts_and_durations]).sum()
			total_percept_duration_excl = np.concatenate([apd[:,-1] for apd in [self.all_percepts_and_durations[0], self.all_percepts_and_durations[-1]]]).sum()

			self.ratio_transition = 1.0 - (total_percept_duration_excl / total_percept_duration)
			self.ratio_percept_red = self.all_percepts_and_durations[0][:,-1].sum() / total_percept_duration_excl

			self.red_durations = np.array([np.mean(self.all_percepts_and_durations[0][:,-1]), np.median(self.all_percepts_and_durations[0][:,-1])])
			self.green_durations = np.array([np.mean(self.all_percepts_and_durations[-1][:,-1]), np.median(self.all_percepts_and_durations[-1][:,-1])])
			self.transition_durations = np.array([np.mean(self.all_percepts_and_durations[1][:,-1]), np.median(self.all_percepts_and_durations[1][:,-1])])

			self.ratio_percept_red_durations = self.red_durations / (self.red_durations + self.green_durations)
			plot_mean_or_median = 0 # mean

			f = pl.figure(figsize = (8,4))
			s = f.add_subplot(111)
			for i in range(len(self.colors)):
				pl.hist(self.all_percepts_and_durations[i][:,-1], bins = 20, color = self.colors[i], histtype='step', lw = 3.0, alpha = 0.4, label = ['Red', 'Trans', 'Green'][i])
			pl.hist(np.concatenate([self.all_percepts_and_durations[0][:,-1], self.all_percepts_and_durations[-1][:,-1]]), bins = 20, color = 'k', histtype='step', lw = 3.0, alpha = 0.4, label = 'Percepts')
			pl.legend()
			s.set_xlabel('time [ms]')
			s.set_ylabel('count')
			sn.despine(offset=10)
			s.annotate("""ratio_transition: %1.2f, \nratio_percept_red: %1.2f, \nduration_red: %2.2f,\nduration_green: %2.2f, \nratio_percept_red_durations: %1.2f"""%(self.ratio_transition, self.ratio_percept_red, self.red_durations[plot_mean_or_median], self.green_durations[plot_mean_or_median], self.ratio_percept_red_durations[plot_mean_or_median]), (0.5,0.65), textcoords = 'figure fraction')
			pl.tight_layout()
			pl.savefig(os.path.join(self.analyzer.fig_dir, self.file_alias + '_dur_hist.pdf'))
Exemple #18
0
    def plot_abc(self, uarg, param):
        """Plot a() and b() functions on the same plot.

        """
        plt.figure(figsize=(8, 4))

        plt.subplot(1, 3, 1)
        plt.plot(uarg, self.afun(uarg, param))
        plt.axhline(0)
        plt.axvline(0)
        plt.ylabel('$a(u)$')
        plt.xlabel('$u$')

        plt.subplot(1, 3, 2)
        plt.plot(uarg, self.bfun(uarg, param))
        plt.axhline(0)
        plt.axvline(0)
        plt.ylabel('$b(u)$')
        plt.xlabel('$u$')

        plt.subplot(1, 3, 3)
        plt.plot(uarg, self.cfun(uarg, param))
        plt.axhline(0)
        plt.axvline(0)
        plt.ylabel('$c(u)$')
        plt.xlabel('$u$')

        plt.tight_layout()
        plt.show()
Exemple #19
0
def plmyfig(df, bgname, dirname, tar, count=10):
    #plot fig!
    print("Starting Plot %s %s" % (dirname, bgname))
    if len(df) > count:
        df = df.head(count)
    pos = plt.arange(len(df)) + 0.5
    ytick = _getTerm(df['Term_description'], df['Term_ID'], bgname)
    xs = [float(n) for n in df[' -log10(pvalue)']]
    ytick.reverse()
    xs.reverse()
    plt.barh(pos, xs, align = 'center', height = 0.5, alpha = 1, color='orange')
    plt.yticks(pos, ytick, size = 'x-small')
    plt.xlabel('$-Log10(pValue)$')
    plt.title('%s' % bgname)
    ax = plt.gca()
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('bottom')
    try:
        plt.tight_layout()
    except ValueError:
        pass
    filename = os.path.join(tar, dirname, dirname + '_' + bgname)
    plt.savefig(filename + '.png', dpi = 72)
    plt.savefig(filename + '.pdf')
    plt.close()
Exemple #20
0
    def plot(self,file):
        cds = CaseDataset(file, 'bson')
        data = cds.data.driver('driver').by_variable().fetch()
        cds2 = CaseDataset('../output/therm_mc_20141110173851.bson', 'bson')
        data2 = cds2.data.driver('driver').by_variable().fetch()
        
        #temp
        temp_boundary_k = data['hyperloop.temp_boundary']
        temp_boundary_k.extend(data2['hyperloop.temp_boundary'])
        temp_boundary = [((x-273.15)*1.8 + 32) for x in temp_boundary_k]
        #histogram
        n, bins, patches = plt.hist(temp_boundary, 100, normed=1, histtype='stepfilled')
        plt.setp(patches, 'facecolor', 'b', 'alpha', 0.75)

        #stats
        mean = np.average(temp_boundary)
        std = np.std(temp_boundary)
        percentile = np.percentile(temp_boundary,99.5)
        print "mean: ", mean, " std: ", std, " 99.5percentile: ", percentile
        x = np.linspace(50,170,150)
        plt.plot(x,mlab.normpdf(x,mean,std), color='black', lw=2)
        plt.xlim([60,160])
        plt.ylabel('Probability', fontsize=18)
        plt.xlabel(u'Equilibrium Temperature, \N{DEGREE SIGN}F', fontsize=18)
        #plt.show()
        plt.tight_layout()
        plt.savefig('../output/histo.pdf', dpi=300)
Exemple #21
0
def plot_reconstruction_result(res):
    """ Plot original and reconstructed graph plus time series
    """
    fig = plt.figure(figsize=(32, 8))
    gs = mpl.gridspec.GridSpec(1, 4)

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

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

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

    plt.tight_layout()
    save(fig, 'reconstruction_overview')
Exemple #22
0
    def save(self, out_path):
        '''Saves a figure for the monitor
        
        Args:
            out_path: str
        '''
        
        plt.clf()
        np.set_printoptions(precision=4)
        font = {
            'size': 7
        }
        matplotlib.rc('font', **font)
        y = 2
        x = ((len(self.d) - 1) // y) + 1
        fig, axes = plt.subplots(y, x)
        fig.set_size_inches(20, 8)

        for j, (k, v) in enumerate(self.d.iteritems()):
            ax = axes[j // x, j % x]
            ax.plot(v, label=k)
            if k in self.d_valid.keys():
                ax.plot(self.d_valid[k], label=k + '(valid)')
            ax.set_title(k)
            ax.legend()

        plt.tight_layout()
        plt.savefig(out_path, facecolor=(1, 1, 1))
        plt.close()
Exemple #23
0
    def plot(self, show=True, filename=None):
        """
        Plots the windows for the object's channel.

        :param show: Calls ``plt.show()`` before returning if True.
        :param filename: If given, a picture will be saved to this path.

        :return: The possibly created axes object.
        """
        if self.comm is None:
            raise ValueError("Operation only possible with an active "
                             "communicator instance.")

        import matplotlib.pylab as plt

        ax = self.comm.visualizations.plot_data_and_synthetics(
            self.event, self.iteration, self.channel_id, show=False)
        ylim = ax.get_ylim()

        st = self.event["origin_time"]
        for window in self.windows:
            ax.fill_between((window.starttime - st, window.endtime - st),
                            [ylim[0] - 10] * 2, [ylim[1] + 10] * 2,
                            alpha=0.3, lw=0, zorder=-10)
        ax.set_ylim(*ylim)
        if show:
            plt.tight_layout()
            plt.show()
            plt.close()
        elif filename:
            plt.tight_layout()
            plt.savefig(filename)
            plt.close()

        return ax
Exemple #24
0
def zsview(im, cmap=pl.cm.gray, figsize=(8,5), contours=False, ccolor='r'):
    z1, z2 = zscale(im)
    pl.figure(figsize=figsize)
    pl.imshow(im, vmin=z1, vmax=z2, origin='lower', cmap=cmap, interpolation='none')
    if contours:
        pl.contour(im, levels=[z2], origin='lower', colors=ccolor)
    pl.tight_layout()
Exemple #25
0
def expt3():
	"""
	Experiment 2: Chooses the result files and generates figures
	"""
	result_file = "./expt3.txt"
	input_threads, input_sizes, throughputs, resp_times \
		= parse_output(result_file) 

	throughputs_MiB = [tp/2**20 for tp in throughputs]

	fig1, (ax0, ax1) = pl.subplots(ncols=2, figsize=(6, 3))
	fig1.set_tight_layout(True)

	ax0.plot(input_threads, throughputs_MiB, 
			'bo-', ms=MARKER_SIZE, mew=0, mec='b')
	ax0.set_xlabel("threads")
	ax0.set_ylabel("throughput (MiB/sec)")
	ax0.set_xlim(0,25)
	ax0.text(1, 85, "(A)")

	ax1.plot(input_threads, resp_times, 
			'mo-', ms=MARKER_SIZE, mew=0, mec='m')
	ax1.set_xlabel("threads")
	ax1.set_ylabel("response time (sec)")
	ax1.set_xlim(0,25)
	ax1.set_ylim(0,400)
	ax1.text(1, 375, "(B)")

	pl.tight_layout()
	pl.savefig("./figures/%s" % result_file.replace(".txt", ".pdf"))
def plot_ra(s1, s2, idxs=None, epsilon=0.25, fig=None):
    """Computes the RA plot of two groups of samples"""

    ## compute log2 values
    l1 = np.log2(s1 + epsilon)
    l2 = np.log2(s2 + epsilon)

    ## compute A and R
    r = l1 - l2
    a = (l1 + l2) * 0.5

    fig = pl.figure() if fig is None else fig
    pl.figure(fig.number)

    if idxs is None:
        pl.plot(a, r, '.k', markersize=2)
    else:
        pl.plot(a[~idxs], r[~idxs], '.k', markersize=2)
        pl.plot(a[idxs], r[idxs], '.r')

    pl.axhline(0, linestyle='--', color='k')

    pl.xlabel('(log2 sample1 + log2 sample2) / 2')
    pl.ylabel('log2 sample1 - log2 sample2')

    pl.tight_layout()
Exemple #27
0
 def test_pix_positions(self):
     image.sex(os.path.join(self.args.output_path, 'output.fits'))
     inputcat=catalog.read(os.path.join(self.args.tmp_path, 'ccd_1.cat'))
     outputcat=catalog.readfits(os.path.join(self.args.output_path, 'output.cat'))
     ouputcat=outputcat[outputcat["FLAGS"]==0]
     mergedcat=catalog.mergecatpix(inputcat,outputcat, delta=3, poskeys1=['X_IMAGE','Y_IMAGE'], poskeys2=['X_IMAGE','Y_IMAGE'])
     catalog.add_diff_column(mergedcat,'X_IMAGE_2','X_IMAGE_1',outputfield='DELTAX')
     catalog.add_diff_column(mergedcat,'Y_IMAGE_2','Y_IMAGE_1',outputfield='DELTAY')
     
     #p.figure()
     #p.scatter(mergedcat['DELTAX'],mergedcat['DELTAY'])
     #p.xlabel('Delta X (pixels)')
     #p.ylabel('Delta Y (pixels)')
     
     catalog.scattercols(mergedcat,'DELTAX','DELTAY',xlab='Delta X (pixels)',ylab='Delta Y (pixels)',show=False)
     
     p.grid()
     p.legend()
     p.tight_layout()
     p.savefig(os.path.join(self.figdir,"wcs_test_pix_positions_1.png"))
     
     p.figure()
     f, axarr = p.subplots(2, sharex=True)
     axarr[0].hist(mergedcat['DELTAX'],bins=np.linspace(-0.5,0.5,101))
     axarr[0].set_title('Delta X (pixels)')
     axarr[1].hist(mergedcat['DELTAY'],bins=np.linspace(-0.5,0.5,101))
     axarr[1].set_title('Delta Y (pixels)')
     
     p.grid()
     p.legend()
     p.tight_layout()
     p.savefig(os.path.join(self.figdir,"wcs_test_pix_positions_2.png"))
     tol = 0.15
     assert (np.mean(mergedcat['DELTAX']) < tol) and (np.mean(mergedcat['DELTAY']) < tol)
Exemple #28
0
	def plot_data(self,full_fname=None, dpi=300, ys=None, yhat=None):
		name = self.test_name
		if ys is not None : ys_data = ys
		else : ys_data = self.results.series[name]['ys']
		if yhat is not None : yhat_data = yhat
		else: yhat_data = self.results.series[name]['yhat']

		fig = plt.figure()
		ax = fig.add_subplot(111)

		plt.plot(ys_data)
		plt.plot(yhat_data)

		if ys is None :
			fig.suptitle("%s | wp:%s,lc:%s" % (name,self.tm.winners_percent,self.tm.loopy_cols))
			y = 0.998
			for m in ['mape','nll', 'mae', 'rmse', 'r2'] :
				metric = "%s: %.3f" % (str(m).upper(), self.results.metrics[name][m])
				plt.text(0.998,y, metric, horizontalalignment='right',  verticalalignment='top', transform=ax.transAxes)
				y -= 0.03

		plt.tight_layout()
		if full_fname is not None :
			fig.savefig(full_fname,bbox_inches='tight',dpi=dpi)
			plt.close(fig)
Exemple #29
0
def measure_psf(vignet, pixscale=1., show=False, mask_value=None):
    y, x = np.mgrid[-vignet.shape[0]/2:vignet.shape[0]/2, -vignet.shape[1]/2:vignet.shape[1]/2]*pixscale
    if mask_value :
        vignet = ma.masked_values(vignet, mask_value).filled(0)
    # Fit the data using astropy.modeling
    p_init=models.Gaussian2D(amplitude=vignet.max(), x_mean=0., y_mean=0.,
        x_stddev=2*pixscale, y_stddev=2*pixscale, theta=0, cov_matrix=None)
    fit_p = fitting.LevMarLSQFitter()

    p = fit_p(p_init, x, y, vignet)
    barycenter=measure_barycenter(vignet, pixscale=pixscale)
    
    # Plot the data with the best-fit model
    P.figure(figsize=(8, 2.5))
    P.subplot(1, 3, 1)
    P.imshow(vignet, origin='lower', interpolation='nearest', vmin=vignet.min(), vmax=vignet.max())
    P.title("Data")
    P.subplot(1, 3, 2)
    P.imshow(p(x, y), origin='lower', interpolation='nearest', vmin=vignet.min(), vmax=vignet.max())
    P.scatter(vignet.shape[0]/2, vignet.shape[1]/2,marker="+")
    P.annotate("({:.3f},{:.3f})".format(*barycenter), (vignet.shape[0]/3, vignet.shape[1]/3))
    P.title("Model - psf = {:.2f}".format(2.3548*np.mean([p.x_stddev.value, p.y_stddev.value])))
    P.subplot(1, 3, 3)
    P.imshow(vignet - p(x, y), origin='lower', interpolation='nearest', vmin=-vignet.max()/10,vmax=vignet.max()/10)
    P.title("Residual")
    P.tight_layout()
    if show :
        P.show()
    
    return p
Exemple #30
0
    def plot_fitted_model(self, sample, data, fig=None, xmin=-1, xmax=12, npoints=1000, nbins=100, epsilon=0.25):
        """Plot fitted model"""

        # fetch group
        group = [i for i, item in enumerate(data.groups.items()) if sample in item[1]][0]

        # fetch data
        counts = data.counts_norm[sample].values.astype('float')
        counts[counts < 1] = epsilon
        counts = np.log(counts)

        # compute fitted model
        x = np.reshape(np.linspace(xmin, xmax, npoints), (-1, 1))
        xx = np.exp(x)
        loglik = _compute_loglik(xx, self.log_phi, self.log_mu, self.beta[self.z[group]])
        y = xx * np.exp(loglik) / self.nfeatures

        # plot
        fig = pl.figure() if fig is None else fig
        pl.figure(fig.number)

        pl.hist(counts, nbins, histtype='stepfilled', linewidth=0, normed=True, color='gray')
        pl.plot(x, np.sum(y, 1), 'r')

        pl.grid()
        pl.xlabel('log counts')
        pl.ylabel('density')
        pl.legend(['model', 'data'], loc=0)
        pl.tight_layout()
Exemple #31
0
    def plot(self, **kwargs):
        """
        plot artifact rejection  averaged signals for ECG and EOG onsets

        Parameters
        ----------
        raw :
        raw_clean :
        ch_name :
        events :
        event_id :
        picks :
        tmin :
        tmax :
        title :
        colors :
        alpha :
        grid :
        scale :
        offset :
        fontsize :

        Returns
        -------

        """

        self._update_from_kwargs(**kwargs)
        title = kwargs.get("title")
        evt = kwargs.get("events")
        counts = evt.shape[0]

        # init figure

        sig_raw, sig_clean, range, t = self._calc_data(self.raw,
                                                       self.raw_clean,
                                                       evt,
                                                       event_id=self.event_id,
                                                       tmin=self.tmin,
                                                       tmax=self.tmax,
                                                       picks=self.picks)
        #--- ref channel e.g.: ECG
        sig_picks = self.labels2picks(labels=self.ch_name)
        sig_ref, _, _ = self._calc_signal(self.raw,
                                          evt,
                                          event_id=self.event_id,
                                          tmin=self.tmin,
                                          tmax=self.tmax,
                                          picks=sig_picks)

        if not isinstance(self.figures, (list)):
            self.figures = []

        # if self.idx == 1:
        if not self._fig:
            self.figures.append(plt.figure(self.fig_nr))
            self._fig = self.figures[-1]
            if title:
                self.figure.suptitle(title, fontsize=12)
            suptitle = kwargs.get("suptitle")
            self.suptitle = suptitle
        else:
            self._fig = plt.figure(self.fig_nr)

    #--- subplot(nrows,ncols,idx)
        ax1 = plt.subplot(self.n_rows, self.n_cols, self.idx)
        #--- sig raw
        scl = self.scale.get("raw")
        ylim = self._calc_ylimits(ranges=range,
                                  factor=scl.get("factor"),
                                  offset=self.offset)
        self._plot(ax1, t, sig_raw * scl.get("factor"), scl.get("unit"),
                   "black")

        #--- sig clean
        ax2 = plt.subplot(self.n_rows, self.n_cols, self.idx + self.n_cols)
        self._plot(ax2, t, sig_clean * scl.get("factor"), scl.get("unit"),
                   "black")
        #---
        scl = self.scale.get("ref")
        ax3 = ax1.twinx(
        )  # instantiate a second axes that shares the same x-axis
        color = 'tab:blue'
        self._plot(ax3, t, sig_ref * scl.get("factor"), scl.get("unit"), "red")
        ax3.tick_params(axis='y', labelcolor=color)
        ax3.legend([self.ch_name + " cnts {}".format(counts)],
                   loc=2,
                   prop={'size': 8})

        ax4 = ax2.twinx(
        )  # instantiate a second axes that shares the same x-axis
        color = 'tab:blue'
        self._plot(ax4, t, sig_ref * scl.get("factor"), scl.get("unit"),
                   "green")
        ax4.tick_params(axis='y', labelcolor=color)
        ax4.legend(["Clean " + self.ch_name + " cnts {}".format(counts)],
                   loc=2,
                   prop={'size': 8})
        try:
            ax1.set_ylim(ylim[0], ylim[1])
            ax2.set_ylim(ylim[0], ylim[1])
        except:
            ax1.set_ylim(-1.0, 1.0)
            ax2.set_ylim(-1.0, 1.0)
            logger.error(
                "ERROR in performance plot : can not set ylim : {}".format(
                    title))

        plt.tight_layout()
Exemple #32
0
def sample_of_trajectories(model, path, agent_ids=range(100), secveg_dynamics=False):

    fig, (ax1, ax2, ax3) = plt.subplots(3, sharex='all', figsize=(6, 6))

    ax1b = ax2.twinx()

    if agent_ids == "all":
        agent_ids = range(model.no_agents)

    for inspect_id in agent_ids:

        print("plotting trajectory %d" % inspect_id )
        # print(sv_traj[0:dim])
        plot_P, plot_qp, plot_k, plot_F, plot_S, plot_qs, *aux_vars = np.copy(
            model.sv_traj.T[inspect_id * model.dim:(inspect_id + 1) * model.dim])

        ax1.plot(model.t, plot_F, color='darkgreen', linewidth=.5, label='F', alpha=0.3)
        ax1.plot(model.t, plot_P, color='lawngreen', linewidth=.5, label='P', alpha=0.3)
        ax1.plot(model.t, plot_S, color='m', linewidth=.5, label='S', alpha=0.3)

        ax2.plot(model.t, plot_qp, color='saddlebrown', linewidth=.5, label='q', alpha=0.3)
        if secveg_dynamics:
            ax2.plot(model.t, plot_qs, color='m', linewidth=.5, label=r'qs', alpha=0.3)
        ax1b.plot(model.t, plot_k, 'b', linewidth=.5, label=r'$k$', alpha=0.3)  # , marker='o', fillstyle = 'none')

        plot_d, plot_a, plot_r, plot_l, plot_m = np.copy(
            model.cv_traj.T[inspect_id * model.no_controls:(inspect_id + 1) * model.no_controls])

        ax3.plot(model.t, plot_d, label='d', color='b', linewidth=.5, alpha=0.3)
        ax3.plot(model.t, plot_a, label='a', color='r', linewidth=.5, alpha=0.3)
        ax3.plot(model.t, plot_r, label='r', color='g', linewidth=.5, alpha=0.3)
        ax3.plot(model.t, plot_l, label='l', color='m', linewidth=.5, alpha=0.3)
        ax3.plot(model.t, plot_m, label='m', color='c', linewidth=.5, alpha=0.3)

    ax1.set_ylabel(r'$F, P, S$', fontsize=16)
    if not model.pars["absolute_area"]:
        ax1.set_ylim([-0.1, 1.1])

    handles, labels = ax1.get_legend_handles_labels()
    ax1.legend(handles[0:3], labels[0:3], loc='upper left')

    ax2.set_ylabel(r'$q$', fontsize=16, color='saddlebrown')
    for tl in ax2.get_yticklabels():
        tl.set_color('saddlebrown')

    ax1b.set_ylabel(r'$k$', color='b', fontsize=16)
    for tick_label in ax1b.get_yticklabels():
        tick_label.set_color('b')

    ax3.set_xlabel(r't', fontsize=16)

    ax3.set_ylim([-0.2, 1])

    handles, labels = ax3.get_legend_handles_labels()
    ax3.legend(handles[0:5], labels[0:5], loc='upper left')

    ax3.text(-1., -0.1, 'strategy', horizontalalignment='right',
             verticalalignment='center')

    plt.tight_layout()

    fig.savefig(path, dpi=model.plot_pars["dpi_saving"])

    fig.clf()
    plt.close(fig)

    return 0
Exemple #33
0
def plot_single_agent(model, path, node_id=1,
                      plot_areas=True,
                      plot_qk=True,
                      plot_controls=False,
                      plot_aux=False,
                      secveg_dynamics=False,
                      ):

    axis_label_fontsize = 14

    # print(sv_traj[0:dim])
    plot_p, plot_qp, plot_k, plot_f, plot_s, plot_qs, *aux_vars = np.copy(model.sv_traj.T[node_id * model.dim:(node_id + 1) * model.dim])

    no_plots = sum([plot_areas, plot_qk,  plot_controls, plot_aux])

    fig, ax = plt.subplots(no_plots, sharex='all', figsize=(6, no_plots * 2 + 1))

    if no_plots == 1:
        ax = [ax]

    # current axis
    axis_counter = 0
    # plot labels
    plot_labels = ["(a)", "(b)", "(c)", "(d)", "(e)", "(f)"]
    annotation_pos = (-0.15, 1)
    annotation_offset = (1, -1)

    if plot_areas:
        cax = ax[axis_counter]
        cax.plot(model.t, plot_f, color='darkgreen', linewidth=2., label='Forest F')
        cax.plot(model.t, plot_p, color='lawngreen', linewidth=2., label='Pasture P')
        cax.plot(model.t, plot_s, color='m', linewidth=2., label='Sec. Vegetation S')
        cax.set_ylabel(r'$F, P, S$ [ha]', fontsize=axis_label_fontsize)
        if not model.pars["absolute_area"]:
            cax.set_ylim([-0.1, 1.1])
        cax.legend(loc='right')

        cax.annotate(plot_labels[axis_counter], xy=annotation_pos, xycoords='axes fraction', fontsize=axis_label_fontsize,
                     xytext=annotation_offset, textcoords='offset points',
                     horizontalalignment='left', verticalalignment='top')
        axis_counter += 1

    if plot_qk:
        cax = ax[axis_counter]
        cax.plot(model.t, plot_qp, color='saddlebrown', linewidth=2., label='pasture productivity q')
        if secveg_dynamics:
            cax.plot(model.t, plot_qs, color='m', linewidth=2., label='soil quality (on S) v')

        if secveg_dynamics:
            cax.set_ylabel(r'$q, v$ [a.u.]', fontsize=axis_label_fontsize, color='k')
        else:
            cax.set_ylabel(r'$q$ [a.u.]', fontsize=axis_label_fontsize, color='saddlebrown')
            for tl in cax.get_yticklabels():
                tl.set_color('saddlebrown')

        #cax.set_zorder(100)

        axt = cax.twinx()
        if max(plot_k) > 10000000:
            rescale_k = 1000000
        elif max(plot_k) > 10000:
            rescale_k = 1000
        else:
            rescale_k = 1

        axt.plot(model.t, plot_k/rescale_k, 'b', linewidth=2., label=r'savings $k$')  # , marker='o', fillstyle = 'none')
        if rescale_k == 1:
            axt.set_ylabel(r'savings [BRL]', color='b', fontsize=axis_label_fontsize)
        else:
            axt.set_ylabel(r'savings [{} BRL]'.format(rescale_k), color='b', fontsize=axis_label_fontsize)
        for tick_label in axt.get_yticklabels():
            tick_label.set_color('b')

        if secveg_dynamics:
            cax.legend(loc='lower right')  # loc='upper/lower right'

        cax.annotate(plot_labels[axis_counter], xy=annotation_pos, xycoords='axes fraction', fontsize=axis_label_fontsize,
                     xytext=annotation_offset, textcoords='offset points',
                     horizontalalignment='left', verticalalignment='top')
        axis_counter += 1

    if plot_controls:
        cax = ax[axis_counter]
        plot_d, plot_a, plot_r, plot_l, plot_m = np.copy(model.cv_traj.T[node_id * model.no_controls:(node_id + 1)
                                                         * model.no_controls])

        cax.plot(model.t, plot_d, label='d', color='b')
        cax.plot(model.t, plot_a, label='a', color='r')
        cax.plot(model.t, plot_r, label='r', color='g')
        cax.plot(model.t, plot_l, label='l', color='m')
        cax.plot(model.t, plot_m, label='m', color='c')

        max_plot = np.array([plot_d, plot_a, plot_r, plot_l, plot_m]).max().max()
        cax.set_ylim([-0.2*max_plot, 1.1 * max_plot])

        # print(strategy_traj.T[inspect_id])
        # visualize strategy
        for i, strategy_t in enumerate(model.strategy_traj.T[node_id]):
            if strategy_t == 0:
                color = 'g'
            else:
                color = 'r'
            cax.plot((i - 0.5, i + 0.5), (-0.1*max_plot, -0.1*max_plot), linewidth=10, color=color, solid_capstyle='butt')

        cax.legend(loc='upper right')
        cax.text(-1., -0.1*max_plot, 'strategy', horizontalalignment='right',
                 verticalalignment='center')

        cax.annotate(plot_labels[axis_counter], xy=annotation_pos, xycoords='axes fraction', fontsize=axis_label_fontsize,
                     xytext=annotation_offset, textcoords='offset points',
                     horizontalalignment='left', verticalalignment='top')
        axis_counter += 1

    if plot_aux:
        cax = ax[axis_counter]
        for i, aux_var in enumerate(aux_vars):
            cax.plot(model.t, aux_var, label=model.state_variables[model.dim - model.no_aux_vars + i])

        cax.legend(loc='upper right')

        cax.annotate(plot_labels[axis_counter], xy=annotation_pos, xycoords='axes fraction', fontsize=axis_label_fontsize,
                     xytext=annotation_offset, textcoords='offset points',
                     horizontalalignment='left', verticalalignment='top')
        axis_counter += 1

    cax.set_xlabel(r'time [years]', fontsize=axis_label_fontsize)

    plt.tight_layout()

    fig.savefig(path, dpi=model.plot_pars["dpi_saving"])

    fig.clf()
    plt.close(fig)

    return 0
Exemple #34
0
def plot_trajectory_stat(
        stats_df,
        path,
        measure="median",
        bounds=None,
        percentiles=None,
        plot_areas=True,
        plot_controls=False,
        plot_strategy=True,
        plot_active_ranches=False,
        plot_qk=True,
        plot_price=False,
        plot_gini=False,
        secveg_dynamics=False,
        t_min=None,
        dpi=150,
        ensemble_stats=False):

    axis_label_fontsize = 14
    # show also standard deviation/percentiles of aggregate variables
    # with the following alpha
    range_alpha = 0.2
    t = stats_df.index.values
    if t_min is None:
        t_min = min(t)
    t_max = max(t)

    t = t[t_min:t_max]

    no_plots = sum([plot_areas, plot_controls, plot_strategy, plot_qk, plot_price, plot_gini])

    fig, ax = plt.subplots(no_plots, sharex='all', figsize=(6, no_plots * 2 + 1))

    # plot labels
    plot_labels = ["(a)", "(b)", "(c)", "(d)", "(e)", "(f)"]
    annotation_pos = (-0.19, 1)
    annotation_offset = (1, -1)

    # current axis
    axis_counter = 0

    if bounds is 'percentiles':
        if type(percentiles) is not list:
            print("Error: Need percentiles for plotting!")
            return 1

        lb_label = "perc" + str(percentiles[0])
        ub_label = "perc" + str(percentiles[1])

    elif bounds is 'minmax':
        lb_label = 'min'
        ub_label = 'max'

    elif bounds is 'std' or bounds is None:
        pass
    else:
        print("No valid bounds chosen for plotting")

    if plot_areas:
        cax = ax[axis_counter]

        if bounds is 'std':
            cax.fill_between(t, stats_df['F', measure][t_min:t_max]
                             - stats_df['F', 'std'][t_min:t_max],
                             stats_df['F', measure][t_min:t_max]
                             + stats_df['F', 'std'][t_min:t_max],
                             color='darkgreen',
                             alpha=range_alpha)
            cax.fill_between(t, stats_df['P', measure][t_min:t_max]
                             - stats_df['P', 'std'][t_min:t_max],
                             stats_df['P', measure][t_min:t_max]
                             + stats_df['P', 'std'][t_min:t_max], color='lawngreen',
                             alpha=range_alpha)
            cax.fill_between(t, stats_df['S', measure][t_min:t_max]
                             - stats_df['S', 'std'][t_min:t_max],
                             stats_df['S', measure][t_min:t_max]
                             + stats_df['S', 'std'][t_min:t_max], color='m', alpha=range_alpha)

        elif bounds in ['percentiles', 'minmax']:

            cax.fill_between(t, stats_df['F', lb_label][t_min:t_max],
                             stats_df['F', ub_label][t_min:t_max], color='darkgreen',
                             alpha=range_alpha)
            cax.fill_between(t,  stats_df['P', lb_label][t_min:t_max],
                             stats_df['P', ub_label][t_min:t_max], color='lawngreen',
                             alpha=range_alpha)
            cax.fill_between(t,  stats_df['S', lb_label][t_min:t_max],
                             stats_df['S', ub_label][t_min:t_max], color='m', alpha=range_alpha)

        cax.plot(t, stats_df['F', measure][t_min:t_max], color='darkgreen',
                 linewidth=2., label='Forest F')
        cax.plot(t, stats_df['P', measure][t_min:t_max], color='lawngreen',
                 linewidth=2., label='Pasture P')
        cax.plot(t, stats_df['S', measure][t_min:t_max], color='m',
                 linewidth=2., label='Sec. vegetation S')

        cax.set_ylabel(r'$F, P, S$ [ha]', fontsize=axis_label_fontsize)

        if stats_df['F', measure].max() < 1:
            cax.set_ylim([-0.1, 1.1])
        cax.set_xlim([t_min, t_max])
        cax.legend(loc='right')

        cax.annotate(plot_labels[axis_counter], xy=annotation_pos, xycoords='axes fraction', fontsize=axis_label_fontsize,
                     xytext=annotation_offset, textcoords='offset points',
                     horizontalalignment='left', verticalalignment='top')
        axis_counter += 1

    if plot_qk:
        cax = ax[axis_counter]
        cax.plot(t, stats_df['q', measure][t_min:t_max], color='saddlebrown',
                 linewidth=2., label='pasture productivity q')

        if bounds is 'std':
            cax.fill_between(t, stats_df['q', measure][t_min:t_max]
                             - stats_df['q', 'std'][t_min:t_max],
                             stats_df['q', measure][t_min:t_max]
                             + stats_df['q', 'std'][t_min:t_max], color='saddlebrown',
                             alpha=range_alpha)
        elif bounds in ['percentiles', 'minmax']:
            cax.fill_between(t,  stats_df['q', lb_label][t_min:t_max],
                             stats_df['q', ub_label][t_min:t_max], color='saddlebrown',
                             alpha=range_alpha)

        if secveg_dynamics:
            cax.plot(t, stats_df['v', measure][t_min:t_max], color='m',
                     linewidth=2., label='soil quality on S v')

            if bounds is 'std':
                cax.fill_between(t, stats_df['v', measure][t_min:t_max]
                                 - stats_df['v', 'std'][t_min:t_max],
                                 stats_df['v', measure][t_min:t_max]
                                 + stats_df['v', 'std'][t_min:t_max], color='m',
                                 alpha=range_alpha)
            elif bounds in ['percentiles', 'minmax']:
                cax.fill_between(t, stats_df['v', lb_label][t_min:t_max],
                                 stats_df['v', ub_label][t_min:t_max], color='m',
                                 alpha=range_alpha)

            cax.set_ylabel(r'$q$, $v$ [a.u.]',
                           fontsize=axis_label_fontsize, color='k')

        else:
            cax.set_ylabel(r'$q$ [a.u.]', fontsize=axis_label_fontsize,
                           color='saddlebrown')

        if not secveg_dynamics:
            for tl in cax.get_yticklabels():
                tl.set_color('saddlebrown')

        # plot k

        if max(stats_df['k', measure][t_min:t_max]) > 500000:
            rescale_k = 1000000
            rescale_k_label = "million "
        elif max(stats_df['k', measure][t_min:t_max]) > 1000:
            rescale_k = 1000
            rescale_k_label = "1000 "
        else:
            rescale_k = 1
            rescale_k_label = ""

        cax2 = cax.twinx()
        cax2.set_xlim([t_min, t_max])
        cax2.plot(t, stats_df['k', measure][t_min:t_max]/rescale_k, 'b', linewidth=2., label=r'savings $k$')
        # , marker='o', fillstyle = 'none')


        label_str = 'savings\n[{}BRL]'.format(rescale_k_label)
        cax2.set_ylabel(label_str, color='b', fontsize=axis_label_fontsize)

        if bounds is 'std':
            cax2.fill_between(t, (stats_df['k', measure][t_min:t_max]
                                  - stats_df['k', 'std'][t_min:t_max])/rescale_k,
                              (stats_df['k', measure][t_min:t_max]
                               + stats_df['k', 'std'][t_min:t_max])/rescale_k,
                              color='b', alpha=range_alpha)
        elif bounds in ['percentiles', 'minmax']:
            cax2.fill_between(t,  stats_df['k', lb_label][t_min:t_max]/rescale_k,
                              stats_df['k', ub_label][t_min:t_max]/rescale_k, color='b', alpha=range_alpha)

        for tick_label in cax2.get_yticklabels():
            tick_label.set_color('b')

        if secveg_dynamics:
            cax.legend(loc='lower right')  # loc='upper right')

        cax.annotate(plot_labels[axis_counter], xy=annotation_pos, xycoords='axes fraction', fontsize=axis_label_fontsize,
                     xytext=annotation_offset, textcoords='offset points',
                     horizontalalignment='left', verticalalignment='top')
        axis_counter += 1

    if plot_strategy:
        cax = ax[axis_counter]

        if ensemble_stats:

            if bounds is 'std':
                cax.fill_between(t,  stats_df["strategy", measure][t_min:t_max]
                                 - stats_df["strategy", "std"][t_min:t_max],
                                 stats_df["strategy", measure][t_min:t_max]
                                 + stats_df["strategy", "std"][t_min:t_max],
                                 color='k', alpha=range_alpha)
            elif bounds in ['percentiles', 'minmax']:
                cax.fill_between(t,  stats_df["strategy", lb_label][t_min:t_max],
                                 stats_df["strategy", ub_label][t_min:t_max], color='k', alpha=range_alpha)

            cax.plot(t, stats_df["strategy", measure][t_min:t_max], linewidth=2., color='k')

        else:
            cax.plot(t, stats_df["strategy", "mean"][t_min:t_max], linewidth=2., color='k')

        cax.set_ylim([0., 1.])
        cax.set_ylabel(r'intensification', fontsize=axis_label_fontsize)

        if plot_active_ranches:
            cax.plot(t, stats_df["active_ranches", "mean"][t_min:t_max], linewidth=2., color='b')

        cax.annotate(plot_labels[axis_counter], xy=annotation_pos, xycoords='axes fraction', fontsize=axis_label_fontsize,
                     xytext=annotation_offset, textcoords='offset points',
                     horizontalalignment='left', verticalalignment='top')

        axis_counter += 1

    if plot_price:
        cax = ax[axis_counter]

        if ensemble_stats:

            if bounds is 'std':
                cax.fill_between(t,  (stats_df["cattle_price", measure][t_min:t_max]
                                 - stats_df["cattle_price", "std"][t_min:t_max])/1000,
                                 (stats_df["cattle_price", measure][t_min:t_max]
                                 + stats_df["cattle_price", "std"][t_min:t_max])/1000,
                                 color='k', alpha=range_alpha)
            elif bounds in ['percentiles', 'minmax']:
                cax.fill_between(t,  stats_df["cattle_price", lb_label][t_min:t_max]/1000,
                                 stats_df["cattle_price", ub_label][t_min:t_max]/1000, color='k', alpha=range_alpha)

            cax.plot(t, stats_df["cattle_price", measure][t_min:t_max]/1000, linewidth=2., color='k')

        else:
            cax.plot(t, stats_df["cattle_price", "mean"][t_min:t_max]/1000, linewidth=2., color='k')

        cax.set_ylabel("cattle price\n[1000 BRL]", fontsize=axis_label_fontsize)
        cax.set_ylim(bottom=0.9, top=3.7)

        cax2 = cax.twinx()

        if stats_df["cattle_quantity", measure][t_min:t_max].max() > 500000:
            rescale_cattle_quantity = 1000000
            print_rescale_cattle_quantity = "million"
        elif stats_df["cattle_quantity", measure][t_min:t_max].max() > 1000:
            rescale_cattle_quantity = 1000
            print_rescale_cattle_quantity = "1000"
        else:
            rescale_cattle_quantity = 1
            print_rescale_cattle_quantity = ""

        if ensemble_stats:

            if bounds is 'std':
                cax2.fill_between(t,  (stats_df["cattle_quantity", measure][t_min:t_max]
                                       - stats_df["cattle_quantity", "std"][t_min:t_max])
                                  / rescale_cattle_quantity,
                                  (stats_df["cattle_quantity", measure][t_min:t_max]
                                   + stats_df["cattle_quantity", "std"][t_min:t_max])
                                  / rescale_cattle_quantity,
                                  color='r', alpha=range_alpha)
            elif bounds in ['percentiles', 'minmax']:
                cax2.fill_between(t,  stats_df["cattle_quantity", lb_label][t_min:t_max]/rescale_cattle_quantity,
                                  stats_df["cattle_quantity", ub_label][t_min:t_max]/rescale_cattle_quantity,
                                  color='r', alpha=range_alpha)

            cax2.plot(t, stats_df["cattle_quantity", measure][t_min:t_max]/rescale_cattle_quantity, linewidth=2., color='r')

        else:
            cax2.plot(t, stats_df["cattle_quantity", "mean"][t_min:t_max], linewidth=2., color='r')

        if rescale_cattle_quantity == 1:
            cax2.set_ylabel('cattle production\n[heads]', fontsize=axis_label_fontsize, color='r')
        else:
            cax2.set_ylabel('cattle production\n[{} heads]'.format(print_rescale_cattle_quantity), fontsize=axis_label_fontsize, color='r')
        for tl in cax2.get_yticklabels():
            tl.set_color('r')

        cax.annotate(plot_labels[axis_counter], xy=annotation_pos, xycoords='axes fraction', fontsize=axis_label_fontsize,
                     xytext=annotation_offset, textcoords='offset points',
                     horizontalalignment='left', verticalalignment='top')
        axis_counter += 1

    if plot_controls:
        cax = ax[axis_counter]

        cax.plot(t, stats_df['d', measure][t_min:t_max],
                 label=r'$\left\langle d \right\rangle$',
                 color='b')
        cax.plot(t, stats_df['a', measure][t_min:t_max],
                 label=r'$\left\langle a \right\rangle$',
                 color='r')
        cax.plot(t, stats_df['r', measure][t_min:t_max],
                 label=r'$\left\langle r \right\rangle$',
                 color='g')
        # ax4.plot(t, plot_l, label=r'$\left\langle l \right\rangle$',
        # color='m')
        cax.plot(t, stats_df['m', measure][t_min:t_max],
                 label=r'$\left\langle m \right\rangle$',
                 color='c')
        cax.set_xlim([t_min, t_max])

        cax.legend(loc='upper right')

        cax.annotate(plot_labels[axis_counter], xy=annotation_pos, xycoords='axes fraction', fontsize=axis_label_fontsize,
                     xytext=annotation_offset, textcoords='offset points',
                     horizontalalignment='left', verticalalignment='top')
        axis_counter += 1

    cax.set_xlim([t_min, t_max])
    cax.set_xlabel(r'time [years]', fontsize=axis_label_fontsize)
    plt.tight_layout()

    if type(path) is not list:
        path = [path]

    for p in path:
        fig.savefig(p, dpi=dpi)

    fig.clf()
    plt.close(fig)

    return 0
Exemple #35
0
def plot_data(data,
              name='',
              date=[0, 0],
              smas=var.default_smas,
              emas=var.default_emas,
              entry_points=None,
              exit_points=None,
              market_name='',
              to_file=False,
              show_smas=False,
              show_emas=False,
              show_bbands=False):
    '''
    Plots selected data.
    entry_points is a tuple of lists: (entry_points_x,entry_points_y)
    '''
    #plt.clf()

    # For when it's called outside backtest.
    if date != [0, 0]:
        if len(data) != date[1] - date[0]:
            data = data[date[0]:date[1]]

    f, (ax1, ax2, ax3) = plt.subplots(3,
                                      sharex=True,
                                      figsize=(9, 4),
                                      gridspec_kw={'height_ratios': [3, 1, 1]})

    ax1.grid(True)
    ax2.grid(True)
    ax3.grid(True)

    # var date is causing conflicts. using name date.
    if date[1] == 0:
        end_date = len(data)
    else:
        end_date = date[1]

    x = range(date[0], end_date)

    ax1.plot(x, data.Last, color='black', linewidth=1, alpha=0.65)

    if show_bbands:
        bb_upper, bb_lower, bb_sma = bollinger_bands(data.Last, 10, 2)
        #ax1.plot(x, bb_upper, color='red', linestyle='none', linewidth=1)
        #ax1.plot(x, bb_lower, color='green', linestyle='none', linewidth=1)

        ax1.fill_between(x, bb_sma, bb_upper, color='green', alpha=0.3)
        ax1.fill_between(x, bb_lower, bb_sma, color='red', alpha=0.3)

    if show_smas:
        for sma in smas:
            ax1.plot(x, data.Last.rolling(sma).mean())

    if show_emas:
        for ema in emas:
            ax1.plot(x, data.Last.ewm(ema).mean())

    if entry_points:
        ax1.plot(entry_points[0],
                 entry_points[1],
                 marker='o',
                 linestyle='None',
                 color='green',
                 alpha=0.75)

    if exit_points:
        ax1.plot(exit_points[0],
                 exit_points[1],
                 marker='o',
                 linestyle='None',
                 color='red',
                 alpha=0.75)

    ax2.bar(x, data.BaseVolume.iloc[:], 1, color='black', alpha=0.55)

    try:
        ax3.plot(x, data.OpenSell.iloc[:])
    except Exception as e:
        ax3.plot(x, data.High.iloc[:])

    plt.xlim(date[0], end_date)
    plt.tight_layout()
    f.subplots_adjust(hspace=0)
    if to_file:
        if not name:
            name = 'fig_test' + str(time())
        f.savefig(var.fig_dir + name + '.pdf', bbox_inches='tight')
        plt.close(f)
    #plt.show()

    return True
Exemple #36
0
def make_model_evaluation(df_nonnan, model_path, ls_pred_dt, cfg_tds, cfg_op):
    X_test_ls = []
    y_test_ls = []
    cmap_pred_dt = plt.cm.get_cmap('viridis_r')

    ## Import dictionary with selected models:
    train_path_name = os.path.join(
        model_path, "model_dict_t0diff_maxdepth6_selfeat_gain.pkl")
    with open(train_path_name, "rb") as file:
        dict_sel_model = pickle.load(file)

    plt.close()
    fig = plt.figure(num=1, figsize=(7, 6))

    ## Loop over lead times:
    for i, pred_dt in enumerate(ls_pred_dt):

        if i == 0:
            xgb_model_ls = []
            pred_model_ls = []
            Rank_obs_ls = []
            top_features_ls = []
            df_param_ls_diff = []
            df_param_ls_rank = []
            df_param_ls_rank_PM = []
            df_param_ls_rank_pers = []
            Rank_pred_XGB_ls = []
            Rank_pred_XGB_PM_ls = []

        if len(X_test_ls) == len(ls_pred_dt) and len(y_test_ls) == len(
                ls_pred_dt):
            X_test = X_test_ls[i]
            y_test = y_test_ls[i]
        else:
            if i == 0:
                X_test_ls = []
                y_test_ls = []
            X_train, X_test, y_train, y_test = ipt.get_model_input(
                df_nonnan,
                del_TRTeqZero_tpred=True,
                split_Xy_traintest=True,
                X_normalise=False,
                pred_dt=pred_dt,
                check_for_nans=False,
                verbose=True)
            del (X_train, y_train)
            X_test_ls.append(X_test)
            y_test_ls.append(y_test)

        ## Load XGB model fitted to all features:
        with open(
                os.path.join(model_path,
                             "model_%i_t0diff_maxdepth6.pkl" % pred_dt),
                "rb") as file:
            xgb_model_feat = pickle.load(file)
        xgb_model_ls.append(xgb_model_feat)

        top_features = pd.DataFrame.from_dict(
            xgb_model_feat.get_booster().get_score(importance_type='gain'),
            orient="index",
            columns=["F_score"]).sort_values(by=['F_score'], ascending=False)
        top_features_ls.append(top_features)

        ## Get specific predictive model for this leadtime:
        pred_model = dict_sel_model["pred_mod_%i" % pred_dt]
        pred_model_ls.append(pred_model)

        ## Check that features agree:
        features_pred_model = pred_model.get_booster().feature_names
        n_features = len(features_pred_model)
        if set(features_pred_model) != set(top_features.index[:n_features]):
            raise ValueError(
                "Features of predictive model and top features of model fitted with all features do not agree"
            )

        ## Make prediction of TRT Rank differences:
        TRT_diff_pred = pred_model.predict(X_test[features_pred_model])

        ## Get set of different TRT Rank predictions:
        Rank_obs, Rank_pred_XGB, Rank_pred_XGB_PM, Rank_pred_pers, Rank_pred_pers_PM, \
            Rank_pred_diff, Diff_pred_XGB = get_obs_fcst_TRT_Rank(X_test["TRT_Rank|0"], TRT_diff_pred, y_test, X_test["TRT_Rank|-5"])
        Rank_obs_ls.append(Rank_obs)
        Rank_pred_XGB_ls.append(Rank_pred_XGB)
        Rank_pred_XGB_PM_ls.append(Rank_pred_XGB_PM)

        ## Plot scatterplots obs vs. predicted:
        plot_pred_vs_obs_core(y_test,
                              Diff_pred_XGB.values,
                              pred_dt,
                              "_XGB%i" % n_features,
                              cfg_tds,
                              outtype="TRT_Rank_diff")
        plot_pred_vs_obs_core(Rank_obs,
                              Rank_pred_XGB.values,
                              pred_dt,
                              "_XGB%i" % n_features,
                              cfg_tds,
                              outtype="TRT_Rank")
        plot_pred_vs_obs_core(Rank_obs,
                              Rank_pred_XGB_PM.values,
                              pred_dt,
                              "_XGB%i-ProbMatch" % n_features,
                              cfg_tds,
                              outtype="TRT_Rank")
        plot_pred_vs_obs_core(Rank_obs,
                              Rank_pred_pers.values,
                              pred_dt,
                              "_Pers",
                              cfg_tds,
                              outtype="TRT_Rank")
        plot_pred_vs_obs_core(Rank_obs,
                              Rank_pred_pers_PM.values,
                              pred_dt,
                              "_Pers-ProbMatch",
                              cfg_tds,
                              outtype="TRT_Rank")
        plot_pred_vs_obs_core(Rank_obs,
                              Rank_pred_diff.values,
                              pred_dt,
                              "_ConstDiff",
                              cfg_tds,
                              outtype="TRT_Rank")

        ## Calculate different term elements for R^2 / Brier Score calculation:
        df_param_ls_diff.append(
            get_R2_param(y_test.values, Diff_pred_XGB.values))
        df_param_ls_rank.append(
            get_R2_param(Rank_obs.values, Rank_pred_XGB.values))
        df_param_ls_rank_PM.append(
            get_R2_param(Rank_obs.values, Rank_pred_XGB_PM.values))
        df_param_ls_rank_pers.append(
            get_R2_param(Rank_obs.values, Rank_pred_pers.values))

        ## Calculate statistics for Taylor Diagram:
        stat_pred_XGB = sm.taylor_statistics(predicted=Rank_pred_XGB.values,
                                             reference=Rank_obs.values)
        stat_pred_XGB_PM = sm.taylor_statistics(
            predicted=Rank_pred_XGB_PM.values, reference=Rank_obs.values)
        stat_pred_pred_pers = sm.taylor_statistics(
            predicted=Rank_pred_pers.values, reference=Rank_obs.values)
        stat_pred_pred_diff = sm.taylor_statistics(
            predicted=Rank_pred_diff.values, reference=Rank_obs.values)
        stat_pred_pred_pers_PM = sm.taylor_statistics(
            predicted=Rank_pred_pers_PM.values, reference=Rank_obs.values)

        sdev = np.array([
            stat_pred_XGB['sdev'][0], stat_pred_XGB['sdev'][1],
            stat_pred_XGB_PM['sdev'][1], stat_pred_pred_pers['sdev'][1]
        ])
        crmsd = np.array([
            stat_pred_XGB['crmsd'][0], stat_pred_XGB['crmsd'][1],
            stat_pred_XGB_PM['crmsd'][1], stat_pred_pred_pers['crmsd'][1]
        ])
        ccoef = np.array([
            stat_pred_XGB['ccoef'][0], stat_pred_XGB['ccoef'][1],
            stat_pred_XGB_PM['ccoef'][1], stat_pred_pred_pers['ccoef'][1]
        ])
        #sdev  = np.array([stat_pred_XGB['sdev'][0], stat_pred_XGB['sdev'][1], stat_pred_XGB_PM['sdev'][1], stat_pred_pred_pers['sdev'][1], stat_pred_pred_diff['sdev'][1]])
        #crmsd = np.array([stat_pred_XGB['crmsd'][0], stat_pred_XGB['crmsd'][1], stat_pred_XGB_PM['crmsd'][1], stat_pred_pred_pers['crmsd'][1], stat_pred_pred_diff['crmsd'][1]])
        #ccoef = np.array([stat_pred_XGB['ccoef'][0], stat_pred_XGB['ccoef'][1], stat_pred_XGB_PM['ccoef'][1], stat_pred_pred_pers['ccoef'][1], stat_pred_pred_diff['ccoef'][1]])

        ## Plot Taylor Diagram:
        col_point = cmap_pred_dt(float(i) / len(ls_pred_dt))
        col_point = (col_point[0], col_point[1], col_point[2], 0.8)

        plot_markerLabel = ["Obs", "+%imin" % pred_dt, "", ""]
        plot_markerLabelColor = "black"
        if i == 0:
            plot_markerLegend = 'on'
            plot_overlay = 'off'
        else:
            plot_markerLegend = "on"
            plot_overlay = 'on'
            #plot_markerLabelColor = None
            if i == len(ls_pred_dt) - 1:
                plot_markerLabelColor = None
                plot_markerLabel = ["Obs", "XGB", "XGB (PM)", "Persistance"]

        sm.taylor_diagram(
            sdev / sdev[0],
            crmsd,
            ccoef,
            styleOBS='-',
            colOBS='darkred',
            markerobs='o',
            titleOBS='Obs',
            markerLabel=plot_markerLabel,
            markerLabelColor=plot_markerLabelColor,
            alpha=0.1,
            markerColor=col_point,
            markerLegend=plot_markerLegend,
            axismax=1.2,
            markerSize=5,
            colRMS='grey',
            styleRMS='--',
            widthRMS=0.8,
            rincRMS=0.25,
            tickRMS=np.arange(0.25, 1.5, 0.25),  #titleRMSangle = 110,
            colSTD='grey',
            styleSTD='-.',
            widthSTD=0.8,
            colCOR='grey',
            styleCOR=':',
            widthCOR=0.8,
            overlay=plot_overlay)

    ## Save Taylor Diagram:
    get_time_delta_colorbar(fig, ls_pred_dt, cmap_pred_dt,
                            [0.7, 0.5, 0.05, 0.3])
    plt.savefig(
        os.path.join(cfg_tds["fig_output_path"], "Taylor_Diagram_cmap.pdf"))
    plt.close()

    ## Plot histogram showing the effect of probability matching:
    print(
        "Save dataframe with observed, predicted, and predicted & PM TRT Ranks"
    )
    Rank_obs_df = pd.concat(Rank_obs_ls, axis=1, sort=True)
    Rank_obs_df.columns = [
        "TRT_Rank_obs|%i" % pred_dt for pred_dt in ls_pred_dt
    ]
    Rank_pred_XGB_df = pd.concat(Rank_pred_XGB_ls, axis=1, sort=True)
    Rank_pred_XGB_df.columns = [
        "TRT_Rank_pred|%i" % pred_dt for pred_dt in ls_pred_dt
    ]
    Rank_pred_XGB_PM_df = pd.concat(Rank_pred_XGB_PM_ls, axis=1, sort=True)
    Rank_pred_XGB_PM_df.columns = [
        "TRT_Rank_pred_PM|%i" % pred_dt for pred_dt in ls_pred_dt
    ]
    #plot_hist_probmatch(Rank_pred_XGB_df, Rank_pred_XGB_PM_df)
    Rank_obs_pred_df = pd.concat(
        [Rank_obs_df, Rank_pred_XGB_df, Rank_pred_XGB_PM_df],
        axis=1,
        sort=True)

    ## Get dataframe with observed, predicted, and predicted & PM TRT Ranks for operational PM:
    op_path_name = os.path.join(cfg_op["XGB_model_path"],
                                "TRT_Rank_obs_pred.pkl")
    with open(op_path_name, "wb") as file:
        pickle.dump(Rank_obs_pred_df, file, protocol=2)
    print("  saved dict to 'XGB_model_path' location:\n    %s" % op_path_name)
    prt_txt = """
    ---------------------------------------------------------------------------------
        The file 'TRT_Rank_obs_pred.pkl' in the
        directory '%s'
        is now used for the operational probability matching procedure, be aware of
        that!
    ---------------------------------------------------------------------------------\n""" % (
        cfg_op["XGB_model_path"])
    print(prt_txt)

    ## Plot skill scores as function of lead-time:
    df_R2_param_rank = pd.concat(df_param_ls_rank,
                                 axis=0).set_index(np.array(ls_pred_dt))
    df_R2_param_rank_PM = pd.concat(df_param_ls_rank_PM,
                                    axis=0).set_index(np.array(ls_pred_dt))
    df_R2_param_diff = pd.concat(df_param_ls_diff,
                                 axis=0).set_index(np.array(ls_pred_dt))
    df_R2_param_rank_pers = pd.concat(df_param_ls_rank_pers,
                                      axis=0).set_index(np.array(ls_pred_dt))
    plot_stats(df_R2_param_rank, "TRT_Rank", cfg_tds)
    plot_stats(df_R2_param_diff, "TRT_Rank_diff", cfg_tds)
    plot_stats_nice(df_R2_param_rank, "TRT_Rank", cfg_tds)
    plot_stats_nice(df_R2_param_diff, "TRT_Rank_diff", cfg_tds)
    plot_stats_nice(df_R2_param_rank_pers, "TRT_Rank_pers", cfg_tds)
    plot_stats_nice(df_R2_param_rank_PM, "TRT_Rank_PM", cfg_tds)

    ## Print IDs of long TRT cells in testing dataset:
    print(
        "\nThese are the IDs of long TRT cells (>25 time steps) in the testing dataset:"
    )
    TRT_ID = X_test_ls[-1].index
    TRT_ID = [TRT_ID_i[13:] for TRT_ID_i in TRT_ID.values]
    TRT_ID_count = Counter(TRT_ID)
    TRT_ID_count_sort = [
        (k, TRT_ID_count[k])
        for k in sorted(TRT_ID_count, key=TRT_ID_count.get, reverse=True)
    ]
    TRT_ID_count_sort_pd = pd.DataFrame(np.array(TRT_ID_count_sort),
                                        columns=["TRT_ID", "Count"])
    TRT_ID_count_sort_pd["Count"] = TRT_ID_count_sort_pd["Count"].astype(
        np.uint16, inplace=True)
    TRT_ID_long = TRT_ID_count_sort_pd.loc[TRT_ID_count_sort_pd["Count"] > 25]
    print(TRT_ID_long)

    TRT_ID_casestudy = [
        "2018080721250094", "2018080721300099", "2018080711400069",
        "2018080710200036"
    ]
    print("  Making analysis for TRT IDs (hardcoded!): %s" % TRT_ID_casestudy)

    TRT_ID_long_sel = TRT_ID_long.loc[TRT_ID_long['TRT_ID'].isin(
        TRT_ID_casestudy)]
    df_feature_ts_plot = pd.DataFrame.from_dict({
        "Radar":
        ["CZC_lt57dBZ|-45|SUM", "CZC_lt57dBZ|-45|SUM", "CZC_lt57dBZ|-45|SUM"],
        "Satellite": [
            "IR_097_stat|-20|PERC05", "IR_097_stat|-15|PERC01",
            "IR_097_stat|-20|MIN"
        ],
        "COSMO": [
            "CAPE_MU_stat|-10|PERC50", "CAPE_MU_stat|-5|PERC75",
            "CAPE_ML_stat|0|SUM"
        ],
        "Lightning": [
            "THX_densIC_stat|-30|SUM", "THX_curr_pos_stat|-40|SUM",
            "THX_curr_pos_stat|-30|SUM"
        ]
    })
    for i_sel in range(len(TRT_ID_long_sel)):
        print("    Working on cell %s" % TRT_ID_long_sel.iloc[i_sel]["TRT_ID"])
        plot_pred_time_series(TRT_ID_long_sel.iloc[i_sel], df_nonnan,
                              Rank_pred_XGB_ls, ls_pred_dt, cfg_tds)
        plot_pred_time_series(TRT_ID_long_sel.iloc[i_sel],
                              df_nonnan,
                              Rank_pred_XGB_PM_ls,
                              ls_pred_dt,
                              cfg_tds,
                              path_addon="PM",
                              title_addon=" (PM)")

        plot_var_time_series_dt0_multiquant(TRT_ID_long_sel.iloc[i_sel],
                                            df_nonnan, cfg_tds)

        for i_pred_dt, pred_dt in enumerate([10, 20, 30]):
            fig = plt.figure(figsize=[10, 6])
            ax_rad = fig.add_subplot(2, 2, 1)
            ax_sat = fig.add_subplot(2, 2, 2)
            ax_cos = fig.add_subplot(2, 2, 3)
            ax_thx = fig.add_subplot(2, 2, 4)
            ax_ls = [ax_rad, ax_sat, ax_cos, ax_thx]
            #fig, axes = plt.subplots(2,2)
            #fig.set_size_inches(8,6)
            for i_source, source in enumerate(
                ["Radar", "Satellite", "COSMO", "Lightning"]):
                ls_feat_param = df_feature_ts_plot[source].iloc[
                    i_pred_dt].split("|")
                past_dt = np.arange(-45, 0,
                                    5) if int(ls_feat_param[1]) != 0 else [0]
                ax_ls[i_source] = plot_var_time_series(
                    TRT_ID_long_sel.iloc[i_sel],
                    df_nonnan,
                    ls_feat_param[0],
                    ls_feat_param[2],
                    past_dt=past_dt,
                    dt_highlight=int(ls_feat_param[1]),
                    ax=ax_ls[i_source])
            plt.tight_layout()
            plt.savefig(
                os.path.join(
                    cfg_tds["fig_output_path"], "Feat_series_%i_%s.pdf" %
                    (pred_dt, TRT_ID_long_sel.iloc[i_sel]["TRT_ID"])))
            plt.close()
Exemple #37
0
ax2.errorbar(xlocs,
             binned_correlation_times,
             yerr=2 * errs,
             marker='o',
             markersize=5)
#ax6.plot(xlocs,binned_correlation_times,marker='o',markersize=4)
ax1.set_xlabel(r'Cell speed ($\mu$m/min)')
ax1.set_ylabel('Persistence time (min)')
ax4.set_ylabel('Persistence time (min)')
ax1.set_title('Zebrafish T cells', fontsize=10)
ax3.set_xlabel(r'Cell speed ($\mu$m/min)')
ax4.set_xlabel(r'Cell speed ($\mu$m/min)')
ax5.set_xlabel(r'Cell speed ($\mu$m/min)')
ax6.set_xlabel(r'Cell speed ($\mu$m/min)')
ax2.set_xlabel(r'Cell speed ($\mu$m/min)')
ax3.set_title('UPT', fontsize=10)
ax4.set_title('UPT with noise', fontsize=10)
ax5.set_title('S and P uncorrelated', fontsize=10)
ax6.set_title('S and P uncorrelated with noise', fontsize=10)
ax2.set_title('SPC with noise', fontsize=10)

ax1.text(-.1, 1.1, 'A', fontsize=12, transform=ax1.transAxes)
ax2.text(-.1, 1.1, 'B', fontsize=12, transform=ax2.transAxes)
ax3.text(-.1, 1.1, 'C', fontsize=12, transform=ax3.transAxes)
ax4.text(-.1, 1.1, 'D', fontsize=12, transform=ax4.transAxes)
ax5.text(-.1, 1.1, 'E', fontsize=12, transform=ax5.transAxes)
ax6.text(-.1, 1.1, 'F', fontsize=12, transform=ax6.transAxes)

pt.tight_layout()
pt.savefig(
    '/Users/ejerison/Dropbox/imaging_data/figures/empirical_sims_v2.pdf')
Exemple #38
0
def flat_corr_matrix(df,
                     pdf=None,
                     tight=False,
                     labels=None,
                     label_size=None,
                     size=12,
                     n_labels=3,
                     fontsize='auto',
                     draw_cbar=False,
                     tick_label_rotation=45,
                     formatter='%.2e',
                     label_rotation=45,
                     cmap='PiYG'):
    """ Draws a flat correlation matrix of df

    Args:
        df:
        pdf:
        tight:
        col_numbers:
        labels:
        label_size:
        size:
        n_labels:
        fontsize:
        draw_cbar:
        rotation:
        formatter:

    Returns:

    """

    assert isinstance(
        df, pd.DataFrame), 'Argument of wrong type! Needs pd.DataFrame'

    n_vars = np.shape(df)[1]

    fontsize = np.interp(n_vars, (0, 10),
                         (22, 10)) if fontsize is 'auto' else fontsize

    if labels is None:
        labels = df.columns
    else:
        assert len(labels) == len(
            df.columns
        ), "Numbers of labels not matching the numbers of coulums in the df"
    im = None
    fig, axes = plt.subplots(nrows=n_vars, ncols=n_vars, figsize=(size, size))

    # Plotting the matrix, iterate over the columns in 2D
    for i, row in zip(range(n_vars), axes):
        for j, ax in zip(range(n_vars), row):
            if i is j - 1000:
                plt.sca(ax)
                ax.hist(df.iloc[:, i].values, label='data', color='gray')
                ax.set_yticklabels([])
            else:
                im = flat_correlation(df.iloc[:, j],
                                      df.iloc[:, i],
                                      ax=ax,
                                      draw_labels=False,
                                      get_im=True,
                                      cmap=cmap)
            ax.xaxis.set_major_locator(plt.NullLocator())
            ax.yaxis.set_major_locator(plt.NullLocator())

    if tight:
        plt.tight_layout()

    # Common outer label
    for i, row in zip(range(n_vars), axes):
        for j, ax in zip(range(n_vars), row):
            if i == n_vars - 1:
                if label_size is not None:
                    set_flat_labels(ax,
                                    df.iloc[:, j],
                                    axis=1,
                                    n_labels=n_labels,
                                    labelsize=label_size,
                                    rotation=90 if tick_label_rotation is 0
                                    else tick_label_rotation,
                                    formatter=formatter)

                ax.set_xlabel(labels[j],
                              fontsize=fontsize,
                              rotation=label_rotation,
                              ha='right',
                              va='top')
            if j == 0:
                if label_size is not None:
                    set_flat_labels(ax,
                                    df.iloc[:, i],
                                    axis=0,
                                    n_labels=n_labels,
                                    labelsize=label_size,
                                    rotation=tick_label_rotation,
                                    formatter=formatter)
                ax.set_ylabel(labels[i],
                              fontsize=fontsize,
                              rotation=label_rotation,
                              ha='right',
                              va='bottom')

    if pdf is None:
        # plt.show()
        pass
    else:
        pdf.savefig()
        plt.close()

    if draw_cbar:
        cbar_ax = fig.add_axes([0.92, 0.15, 0.02, 0.7])
        cbar = plt.colorbar(
            im,
            cax=cbar_ax,
        )
        cbar.ax.set_ylabel('$\sigma$',
                           rotation=0,
                           fontsize=fontsize * 1.2,
                           va='center')
        cbar.ax.tick_params(labelsize=fontsize)
def plot_intrarater_stats(dbcon, savedir: str, clsgroup: str):
    """"""
    _maybe_mkdir(savedir)
    _maybe_mkdir(opj(savedir, 'csv'))
    _maybe_mkdir(opj(savedir, 'plots'))

    # read intrarater stats
    evalsets = ['B-control', 'E']
    stats = read_sql_query(f"""
        SELECT "participant", "second_evalset" AS "evalset"
             , "detection_and_classification", "classification"
             , "n_anchors_second_evalset" AS "n_anchors"
             , "n_clicks_second_evalset" AS "n_clicks"
        FROM "intra-rater_{clsgroup}ClassGroup"
        WHERE "participant" IN ({ir._get_sqlite_usrstr_for_who('All')})
          AND "first_evalset" = "U-control"
          AND "second_evalset" IN ({ir._get_sqlitestr_for_list(evalsets)})
    ;""", dbcon)
    stats.loc[:, 'psegmented'] = stats.loc[
        :, 'n_clicks'] / stats.loc[:, 'n_anchors']

    # to save raw values for calculating p-values later
    overalldf = []

    # reorder evalsets
    tmp = []
    for evalset in ir.EVALSET_NAMES:
        tmp.append(stats.loc[stats.loc[:, 'evalset'] == evalset, :])
    stats = concat(tmp, axis=0)

    # organize canvas and plot
    nperrow = 3
    nrows = 1
    fig, ax = plt.subplots(nrows, nperrow, figsize=(5 * nperrow, 5.5 * nrows))
    scprops = {'alpha': 0.7, 's': 7 ** 2, 'edgecolor': 'k'}
    axno = -1
    metrics = ['psegmented', 'detection_and_classification', 'classification']
    mdict = {
        'psegmented': 'N. segmentations / N. anchors',
        'detection_and_classification': "Kappa vs U-control (det. & classif.)",
        'classification': "Kappa vs U-control (classif.)",
    }
    for axis in ax.ravel():
        axno += 1
        metric = metrics[axno]

        dfslice = stats.copy()
        dfslice.index = dfslice.loc[:, 'participant']
        dfslice.loc[:, 'who'] = 'NPs'
        for who in ['JPs', 'SPs']:
            for p in dfslice.index:
                if p in ir.who[who]:
                    dfslice.loc[p, 'who'] = who
        dfslice.loc[:, 'swho'] = dfslice.loc[:, 'who'].copy()
        dfslice.loc[dfslice.loc[:, 'swho'] == 'SPs', 'swho'] = 'Ps'
        dfslice.loc[dfslice.loc[:, 'swho'] == 'JPs', 'swho'] = 'Ps'
        dfslice = dfslice.loc[:, ['evalset', metric, 'who', 'swho']]
        overalldf.append(dfslice)

        # annotate agreement ranges
        if axno > 0:
            _annotate_krippendorph_ranges(
                axis=axis, minx=0, maxx=2, shades=False)

        # main boxplots
        bppr = {'alpha': 0.5}
        sns.boxplot(
            ax=axis, data=dfslice, x='evalset', y=metric, hue='swho',
            palette=[ir.PARTICIPANT_STYLES[who]['c'] for who in ['Ps', 'NPs']],
            boxprops=bppr, whiskerprops=bppr, capprops=bppr, medianprops=bppr,
            showfliers=False, notch=False, bootstrap=5000)

        # scatter each participant group
        for who in ['NPs', 'JPs', 'SPs']:

            pstyle = ir.PARTICIPANT_STYLES[who]
            scprops.update({k: pstyle[k] for k in ['c', 'marker']})
            plotme = dfslice.loc[dfslice.loc[:, 'who'] == who, :].copy()
            offset = -0.2 if who in ['JPs', 'SPs'] else 0.2
            plotme.loc[:, 'x'] = plotme.loc[:, 'evalset'].apply(
                lambda x: evalsets.index(x) + offset)
            plotme = np.array(plotme.loc[:, ['x', metric]])

            # add jitter
            plotme[:, 0] += 0.05 * np.random.randn(plotme.shape[0])

            # now scatter
            axis.scatter(
                plotme[:, 0], plotme[:, 1], label=f'{who}', **scprops)

        axis.set_ylim(0., 1.)
        axis.set_title(mdict[metric], fontsize=14, fontweight='bold')
        axis.set_ylabel(metric.capitalize(), fontsize=11)
        axis.legend()

    plt.tight_layout(pad=0.3, w_pad=0.5, h_pad=0.3)
    savename = f'intra-rater_comparison'
    plt.savefig(opj(savedir, 'plots', savename + '.svg'))
    plt.close()

    # save raw numbers
    overalldf = concat(overalldf, axis=0)
    overalldf.to_csv(opj(savedir, 'csv', savename + '.csv'))
Exemple #40
0
                plt.plot(Utils.movingAverage(average),
                         label=str(2 * numOptionsToUse) + ' opt.',
                         color=Utils.colors[color_idx])
            else:
                plt.plot(Utils.movingAverage(average),
                         label=str(numOptionsToUse) + ' opt.',
                         color=Utils.colors[color_idx])

            plt.fill_between(range(len(Utils.movingAverage(average))),
                             Utils.movingAverage(minConfInt),
                             Utils.movingAverage(maxConfInt),
                             alpha=0.5,
                             color=Utils.colors[color_idx])

        plt.legend(loc='upper left', prop={'size': 10}, bbox_to_anchor=(1, 1))
        plt.tight_layout(pad=7)
        plt.show()

    elif taskToPerform == 7:  #Solve for a given goal w/ primitive actions (q-learning)
        #following discovered AND loaded options This one is for comparison.
        numOptionsLoadedToConsider = 4
        numOptionsDiscoveredToConsider = 128

        returnsEvalPrimitive1, returnsEvalDiscovered, totalOptionsToUseDiscovered = qLearningWithOptions(
            env=env,
            alpha=0.1,
            gamma=0.9,
            options_eps=0.0,
            epsilon=1.0,
            nSeeds=num_seeds,
            maxLengthEp=max_length_episode,
def eval(**kwargs):

    # Roll out the parameters
    batch_size = kwargs["batch_size"]
    generator = kwargs["generator"]
    model_name = kwargs["model_name"]
    image_data_format = kwargs["image_data_format"]
    img_dim = kwargs["img_dim"]
    cont_dim = (kwargs["cont_dim"],)
    cat_dim = (kwargs["cat_dim"],)
    noise_dim = (kwargs["noise_dim"],)
    bn_mode = kwargs["bn_mode"]
    noise_scale = kwargs["noise_scale"]
    dset = kwargs["dset"]
    eval_epoch = kwargs["eval_epoch"]

    # Setup environment (logging directory etc)
    general_utils.setup_logging(**kwargs)

    # Load and rescale data
    if dset == "RGZ":
        X_real_train = data_utils.load_RGZ(img_dim, image_data_format)
    if dset == "mnist":
        X_real_train, _, _, _ = data_utils.load_mnist(image_data_format)
    img_dim = X_real_train.shape[-3:]

    # Load generator model
    generator_model = models.load("generator_%s" % generator,
                                  cat_dim,
                                  cont_dim,
                                  noise_dim,
                                  img_dim,
                                  bn_mode,
                                  batch_size,
                                  dset=dset)

    # Load colorization model
    generator_model.load_weights("../../models/%s/gen_weights_epoch%05d.h5" %
                                 (model_name, eval_epoch))

    X_plot = []
    # Vary the categorical variable
    for i in range(cat_dim[0]):
        X_noise = data_utils.sample_noise(noise_scale, batch_size, noise_dim)
        X_cont = data_utils.sample_noise(noise_scale, batch_size, cont_dim)
        X_cont = np.repeat(X_cont[:1, :], batch_size, axis=0)  # fix continuous noise
        X_cat = np.zeros((batch_size, cat_dim[0]), dtype='float32')
        X_cat[:, i] = 1  # always the same categorical value

        X_gen = generator_model.predict([X_cat, X_cont, X_noise])
        X_gen = data_utils.inverse_normalization(X_gen)

        if image_data_format == "channels_first":
            X_gen = X_gen.transpose(0,2,3,1)

        X_gen = [X_gen[i] for i in range(len(X_gen))]
        X_plot.append(np.concatenate(X_gen, axis=1))
    X_plot = np.concatenate(X_plot, axis=0)

    plt.figure(figsize=(8,10))
    if X_plot.shape[-1] == 1:
        plt.imshow(X_plot[:, :, 0], cmap="gray")
    else:
        plt.imshow(X_plot)
    plt.xticks([])
    plt.yticks([])
    plt.ylabel("Varying categorical factor", fontsize=28, labelpad=60)

    plt.annotate('', xy=(-0.05, 0), xycoords='axes fraction', xytext=(-0.05, 1),
                 arrowprops=dict(arrowstyle="-|>", color='k', linewidth=4))
    plt.tight_layout()
    plt.savefig(os.path.join("../../figures", model_name, "varying_categorical.png"))
    plt.clf()
    plt.close()

    # Vary the continuous variables
    X_plot = []
    # First get the extent of the noise sampling
    x = np.ravel(data_utils.sample_noise(noise_scale, batch_size * 20000, cont_dim))
    # Define interpolation points
    x = np.linspace(x.min(), x.max(), num=batch_size)
    for i in range(batch_size):
        X_noise = data_utils.sample_noise(noise_scale, batch_size, noise_dim)
        X_cont = np.concatenate([np.array([x[i], x[j]]).reshape(1, -1) for j in range(batch_size)], axis=0)
        X_cat = np.zeros((batch_size, cat_dim[0]), dtype='float32')
        X_cat[:, 1] = 1  # always the same categorical value

        X_gen = generator_model.predict([X_cat, X_cont, X_noise])
        X_gen = data_utils.inverse_normalization(X_gen)
        if image_data_format == "channels_first":
            X_gen = X_gen.transpose(0,2,3,1)
        X_gen = [X_gen[i] for i in range(len(X_gen))]
        X_plot.append(np.concatenate(X_gen, axis=1))
    X_plot = np.concatenate(X_plot, axis=0)

    plt.figure(figsize=(10,10))
    if X_plot.shape[-1] == 1:
        plt.imshow(X_plot[:, :, 0], cmap="gray")
    else:
        plt.imshow(X_plot)
    plt.xticks([])
    plt.yticks([])
    plt.ylabel("Varying continuous factor 1", fontsize=28, labelpad=60)
    plt.annotate('', xy=(-0.05, 0), xycoords='axes fraction', xytext=(-0.05, 1),
                 arrowprops=dict(arrowstyle="-|>", color='k', linewidth=4))
    plt.xlabel("Varying continuous factor 2", fontsize=28, labelpad=60)
    plt.annotate('', xy=(1, -0.05), xycoords='axes fraction', xytext=(0, -0.05),
                 arrowprops=dict(arrowstyle="-|>", color='k', linewidth=4))
    plt.tight_layout()
    plt.savefig(os.path.join("../../figures", model_name, "varying_continuous.png"))
    plt.clf()
    plt.close()
Exemple #42
0
def make_all_plots(df_plotting_container,
                   specific_plots=[],
                   backend='matplotlib',
                   grid_of_plots=(2, 2),
                   kde=False,
                   plot_params=None,
                   figsize=(9, 7),
                   norm_hist=False,
                   xlabelfontsize=12,
                   ignorePID=False,
                   stacked=False,
                   alpha=0.5,
                   decay=None,
                   tag='default'):

    if plot_params is None:
        plot_params = get_variable_parameters_for_plotting()

    sps_in_container = list(df_plotting_container.keys())
    print(sps_in_container)
    tempdf = df_plotting_container[sps_in_container[0]]['df']
    tempnames = None
    if type(tempdf) is list:
        tempnames = tempdf[0].columns
    else:
        tempnames = tempdf.columns
    #tempnames = df_plotting_container[sps_in_container[0]]['dfs'][0].columns

    names = []
    if ignorePID:
        for name in tempnames:
            if name.find('Is') >= 0 or name.find('BDT') >= 0 or name.find(
                    'KM') >= 0:
                1
            else:
                names.append(name)

    nplots = len(names)

    nplots_per_figure = grid_of_plots[0] * grid_of_plots[1]

    grid_count = 0
    i = 0
    if len(specific_plots) == 0:
        specific_plots = list(names)

    figcount = 0

    sps_for_labels = []

    #for icount,name in enumerate(names):
    for icount, name in enumerate(specific_plots):

        #if len(specific_plots)>0 and name not in specific_plots:
        if name not in names:
            continue

        if i % nplots_per_figure == 0:
            plt.figure(figsize=figsize)
            grid_count = 0

        plt.subplot(grid_of_plots[0], grid_of_plots[1], grid_count + 1)

        plotrange = None
        xlabel = None
        bins = 100
        if name in plot_params.keys():
            xlabel = plot_params[name]['xlabel']
            plotrange = plot_params[name]['range']
            if 'bins' in list(plot_params[name].keys()):
                bins = plot_params[name]['bins']

        if name.find('Is') >= 0 or name.find('BDT') >= 0 or name.find(
                'KM') >= 0:
            plotrange = (0, 1)
            bins = 2

        ########################################################################
        '''
        if backend=='seaborn':
            if plotrange is not None:
                for j,df in enumerate(dfs):
                    label = None
                    if labels is not None:
                        label = labels[j]
                    weight=np.ones(len(df[name]))
                    if type(weights) == list:
                        weight *= weights[j]

                    sns.distplot(df[name],bins=bins,hist_kws={"range": plotrange, "weights":weight, "stacked":stacked,'alpha':alpha},kde=kde,norm_hist=norm_hist,label=label)
                    plt.xlim(plotrange[0],plotrange[1])
            else:
                for j,df in enumerate(dfs):
                    label = None
                    if labels is not None:
                        label = labels[j]
                    weight=np.ones(len(df[name]))
                    if type(weights) == list:
                        weight *= weights[j]

                    sns.distplot(df[name],bins=bins,kde=kde,norm_hist=norm_hist,label=label,hist_kws={"weights":weight, "stacked":stacked})

            if xlabel is not None:
                plt.xlabel(xlabel,fontsize=xlabelfontsize)
            else:
                plt.xlabel(name,fontsize=xlabelfontsize)

            if labels is not None:
                plt.legend(fontsize=8)

        '''
        ########################################################################
        if backend == 'matplotlib':
            #if plotrange is not None:
            allvals = []
            allweights = []
            #last_df = len(dfs)

            backgroundMC = {
                'values': [],
                'weights': [],
                'colors': [],
                'labels': []
            }
            signalMC = {
                'values': [],
                'weights': [],
                'colors': [],
                'labels': []
            }
            data = {'values': [], 'weights': [], 'colors': [], 'labels': []}

            totweights = 0
            for sp in sps_in_container:
                c = df_plotting_container[sp]
                if c['isSignalMC'] is True:
                    signalMC['values'] = c['df'][name]
                    signalMC['colors'] = c['color']
                    signalMC['weights'] = np.ones(len(
                        c['df'][name])) * c['weights']
                    signalMC['labels'] = c['label']
                elif c['isData'] is True:
                    data['values'] = c['df'][name]
                    data['colors'] = c['color']
                    data['weights'] = np.ones(len(
                        c['df'][name])) * c['weights']
                    data['labels'] = c['label']
                else:
                    backgroundMC['values'].append(c['df'][name])
                    backgroundMC['colors'].append(c['color'])
                    backgroundMC['weights'].append(
                        np.ones(len(c['df'][name])) * c['weights'])
                    backgroundMC['labels'].append(c['label'])
                    totweights += c['weights'] * np.sum(len(c['df'][name]))

            #print(backgroundMC)
            #print(backgroundMC['values'])
            #print(backgroundMC['weights'])
            #print(len(backgroundMC['values']))
            #print(len(backgroundMC['weights']))

            # Background
            #print(backgroundMC['colors'])
            #print(backgroundMC['labels'])
            plt.hist(backgroundMC['values'],
                     bins=bins,
                     range=plotrange,
                     weights=backgroundMC['weights'],
                     stacked=stacked,
                     alpha=alpha,
                     label=backgroundMC['labels'],
                     color=backgroundMC['colors'],
                     histtype='stepfilled',
                     density=norm_hist)

            # Signal
            if len(signalMC['values']) > 0:
                nentries = len(signalMC['values'])
                #print(f"totweights: {totweights}")
                signalMC['weights'] = np.ones(nentries) * (totweights /
                                                           nentries) * 0.15
                plt.hist(signalMC['values'],
                         bins=bins,
                         range=plotrange,
                         weights=signalMC['weights'],
                         lw=2,
                         ls='--',
                         alpha=1.0,
                         label=signalMC['labels'],
                         color='b',
                         histtype='step',
                         density=norm_hist)

            # Data
            if len(data['values']) > 0:
                hist_with_errors(data['values'],
                                 bins=bins,
                                 range=plotrange,
                                 label='Data')

            if plotrange is not None:
                plt.xlim(plotrange[0], plotrange[1])

            if xlabel is not None:
                plt.xlabel(xlabel, fontsize=xlabelfontsize)
            else:
                plt.xlabel(name, fontsize=xlabelfontsize)

            #plt.legend(fontsize=8,loc='upper left')
            plt.legend(fontsize=8, loc='best')

        ########################################################################
        if i % nplots_per_figure == nplots_per_figure - 1 or i == nplots - 1:
            print("Here!")
            plt.tight_layout()
            filename = 'plots/stacked_cut_summary_files_{0}_{1}_{2}.png'.format(
                decay, tag, figcount)
            plt.savefig(filename)
            figcount += 1

        i += 1
        plt.tight_layout()
        grid_count += 1

        filename = 'plots/stacked_cut_summary_files_{0}_{1}_{2}.png'.format(
            decay, tag, figcount)
        plt.savefig(filename)
Exemple #43
0
    def combine_all(self,
                    filepath,
                    savename,
                    directory,
                    skiprows,
                    column_names,
                    show=True):
        dir = directory + '/%s' % (savename.strip('.yaml'))
        if not os.path.exists(dir):
            os.makedirs(dir)

        if filepath.endswith('.yaml') == True:
            with open(filepath, 'r') as stream:
                data_file = yaml.load(stream)
                stream.close()
                data = data_file['data']
                # parameters = data_file['parameters']
        else:
            # parameters = open(filepath, "r").readlines()[:33]
            data = np.loadtxt(filepath, delimiter=',', skiprows=skiprows)
        length = len(data[0]) - (len(column_names) + 3)
        L_length = int(data[0][-1])
        R_length = int(data[0][-2])

        for i in range(R_length):
            column_names = np.append(column_names, "R_%i" % (i + 1))
        for i in range(L_length):
            column_names = np.append(column_names, "L_%i" % (i + 1))
        column_names = np.append(column_names, "Accepted")
        column_names = np.append(column_names, "Rayleigh_length")
        column_names = np.append(column_names, "Love_length")

        par = Get_Paramters()
        REAL = par.get_unkown()

        df = pd.DataFrame(data, columns=column_names)
        df_select = df[["Epi", "Depth", "Strike", "Dip", "Rake", "M0"]]
        fig = plt.figure(figsize=(20, 20))
        ax1 = plt.subplot2grid((5, 2), (0, 0), colspan=2)
        # ax1.axhline(y = REAL['epi'] ,linewidth = 0.3 , linestyle =':', color = 'b')
        ax1.plot(df_select['Epi'], label="Epi", c='b')
        ax1.tick_params("y", colors='b')
        ax1.set_ylabel('Epicentral distance [degree]', color='b')
        ax2 = ax1.twinx()
        ax2.plot(df_select['Depth'], label="Depth", c='r')
        # ax2.axhline(y=REAL['depth_s'], linewidth=0.3, linestyle=':', color='r')
        ax2.tick_params('y', colors='r')
        ax2.set_ylabel('Depth [m]', color='r')
        plt.tight_layout()
        ax3 = plt.subplot2grid((5, 2), (1, 0), colspan=2)
        ax3.plot(df_select['Strike'], label="Strike", color="b")
        # ax3.axhline(y=REAL['strike'], linewidth=0.3, linestyle=':', color='b')
        # ax3.axhline(y=REAL['dip'], linewidth=0.3, linestyle=':', color='g')
        # ax3.axhline(y=REAL['rake'], linewidth=0.3, linestyle=':', color='r')
        ax3.plot(df_select['Dip'], label="Dip", color='g')
        ax3.plot(df_select['Rake'], label="Rake", color='r')
        ax3.set_ylabel('Moment tensor angle [degree]', color='k')
        plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
        plt.tight_layout()
        axes = ax3.twinx()
        axes.plot(df_select['M0'], label="M0", c='m')
        # ax2.axhline(y=REAL['depth_s'], linewidth=0.3, linestyle=':', color='r')
        axes.tick_params('y', colors='m')
        axes.set_ylabel('M0', color='r')
        plt.yscale('log')
        plt.tight_layout()

        R_select = df.filter(like='R_')
        L_select = df.filter(like='L_')
        df_select_xi = df[[
            "Total_misfit", "S_z", "S_r", "S_t", "P_z", "P_r", "BW_misfit",
            "Rtot", "Ltot"
        ]]
        ax4 = plt.subplot2grid((5, 2), (2, 0), colspan=2)
        ax4.plot(df_select_xi['Total_misfit'], label="Total_misfit", c='r')
        # ax4.plot(df_select_xi['S_z'], label = "S_z")
        # ax4.plot(df_select_xi['S_r'], label = "S_r")
        # ax4.plot(df_select_xi['S_t'], label = "S_t")
        # ax4.plot(df_select_xi['P_z'], label = "P_z")
        # ax4.plot(df_select_xi['P_r'], label = "P_r")
        ax4.plot(df_select_xi['BW_misfit'], label="BW_tot")
        ymin, ymax = ax4.get_ylim()
        plt.yscale('log')
        # plt.ylim((pow(10, 0), pow(10, 3)))
        plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
        plt.tight_layout()
        ax5 = plt.subplot2grid((5, 2), (3, 0), colspan=2)
        # for i in R_select:
        #     ax5.plot(R_select[i],label = i)
        ax5.plot(df_select_xi['Rtot'], label="Rtot")
        plt.yscale('log')
        # plt.ylim((pow(10, 0), pow(10, 4)))
        plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
        plt.tight_layout()
        ax6 = plt.subplot2grid((5, 2), (4, 0), colspan=2)
        # for i in L_select:
        #     ax6.plot(L_select[i],label = i)
        ax6.plot(df_select_xi['Ltot'], label="Ltot")
        plt.yscale('log')

        # plt.ylim((pow(10, 0), pow(10, 4)))
        plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
        plt.tight_layout()
        plt.savefig(dir + '/combined_all_par.pdf')
        plt.close()
Exemple #44
0
def createPlot(name, xlabel, ylabel, xlist, \
        list1, list1_5, list1_95, label1, \
        list2, list2_5, list2_95, label2, \
        list3, list3_5, list3_95, label3, \
        list4, list4_5, list4_95, label4, \
        list5, list5_5, list5_95, label5, \
        list6, list6_5, list6_95, label6, \
        list7, list7_5, list7_95, label7, \
        list8, list8_5, list8_95, label8):

    # collecting the data separately in case there are all timeouts for one
    # particular algorithm for one case
    xlist_list7, new_list7, new_list7_5, new_list7_95 = [], [], [], []
    xlist_list8, new_list8, new_list8_5, new_list8_95 = [], [], [], []
    for i, n in enumerate(xlist):
        if not list7[i] == -1:
            xlist_list7.append(n)
            new_list7.append(list7[i])
            new_list7_5.append(list7_5[i])
            new_list7_95.append(list7_95[i])
        if not list8[i] == -1:
            xlist_list8.append(n)
            new_list8.append(list8[i])
            new_list8_5.append(list8_5[i])
            new_list8_95.append(list8_95[i])

    list7 = new_list7
    list7_5 = new_list7_5
    list7_95 = new_list7_95
    list8 = new_list8
    list8_5 = new_list8_5
    list8_95 = new_list8_95

    matplotlib.rcParams.update({'font.size': 14})
    plt.figure(facecolor='w', edgecolor='k', figsize=(7, 5))
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    xlist = np.array(xlist)

    # curve for median best fit
    score1CV, _ = curve_fit(func2D, xlist, list1)
    score2CV, _ = curve_fit(func2D, xlist, list2)
    score3CV, _ = curve_fit(func2D, xlist, list3)
    score4CV, _ = curve_fit(func2D, xlist, list4)
    score5CV, _ = curve_fit(func2D, xlist, list5)
    score6CV, _ = curve_fit(func2D, xlist, list6)
    score7CV, _ = curve_fit(func2D, xlist_list7, list7)
    score8CV, _ = curve_fit(func2D, xlist_list8, list8)

    # curve for 5% CI best fit
    score1_5_CV, _ = curve_fit(func2D, xlist, list1_5)
    score2_5_CV, _ = curve_fit(func2D, xlist, list2_5)
    score3_5_CV, _ = curve_fit(func2D, xlist, list3_5)
    score4_5_CV, _ = curve_fit(func2D, xlist, list4_5)
    score5_5_CV, _ = curve_fit(func2D, xlist, list5_5)
    score6_5_CV, _ = curve_fit(func2D, xlist, list6_5)
    score7_5_CV, _ = curve_fit(func2D, xlist_list7, list7_5)
    score8_5_CV, _ = curve_fit(func2D, xlist_list8, list8_5)

    # curve for 95% CI best fit
    score1_95_CV, _ = curve_fit(func2D, xlist, list1_95)
    score2_95_CV, _ = curve_fit(func2D, xlist, list2_95)
    score3_95_CV, _ = curve_fit(func2D, xlist, list3_95)
    score4_95_CV, _ = curve_fit(func2D, xlist, list4_95)
    score5_95_CV, _ = curve_fit(func2D, xlist, list5_95)
    score6_95_CV, _ = curve_fit(func2D, xlist, list6_95)
    score7_95_CV, _ = curve_fit(func2D, xlist_list7, list7_95)
    score8_95_CV, _ = curve_fit(func2D, xlist_list8, list8_95)

    # confidence interval
    # plt.fill_between(xlist, func2D(xlist, score2_5_CV[0], score2_5_CV[1], score2_5_CV[2]), \
    #                         func2D(xlist, score2_95_CV[0], score2_95_CV[1], score2_95_CV[2]), color='blue', alpha=.5)
    # plt.fill_between(xlist, func2D(xlist, score3_5_CV[0], score3_5_CV[1], score3_5_CV[2]), \
    #                         func2D(xlist, score3_95_CV[0], score3_95_CV[1], score3_95_CV[2]), color='seagreen', alpha=.5)
    # plt.fill_between(xlist, func2D(xlist, score1_5_CV[0], score1_5_CV[1], score1_5_CV[2]), \
    #                         func2D(xlist, score1_95_CV[0], score1_95_CV[1], score1_95_CV[2]), color='orangered', alpha=.5)
    # plt.fill_between(xlist, func2D(xlist, score4_5_CV[0], score4_5_CV[1], score4_5_CV[2]), \
    #                         func2D(xlist, score4_95_CV[0], score4_95_CV[1], score4_95_CV[2]), color='skyblue', alpha=.5)
    # plt.fill_between(xlist, func2D(xlist, score5_5_CV[0], score5_5_CV[1], score5_5_CV[2]), \
    #                         func2D(xlist, score5_95_CV[0], score5_95_CV[1], score5_95_CV[2]), color='gold', alpha=.5)
    # plt.fill_between(xlist, func2D(xlist, score6_5_CV[0], score6_5_CV[1], score6_5_CV[2]), \
    #                         func2D(xlist, score6_95_CV[0], score6_95_CV[1], score6_95_CV[2]), color='purple', alpha=.5)

    newx = np.linspace(10, 1000, 250)
    # best fit lines
    plt.plot(newx,
             func2D(np.array(newx), score3CV[0], score3CV[1], score3CV[2]),
             '-',
             color='darkgreen')
    plt.plot(newx,
             func2D(np.array(newx), score6CV[0], score6CV[1], score6CV[2]),
             '-',
             color='navy')
    plt.plot(newx,
             func2D(np.array(newx), score2CV[0], score2CV[1], score2CV[2]),
             '-',
             color='mediumseagreen')
    plt.plot(newx,
             func2D(np.array(newx), score5CV[0], score5CV[1], score5CV[2]),
             '-',
             color='royalblue')
    plt.plot(newx,
             func2D(np.array(newx), score1CV[0], score1CV[1], score1CV[2]),
             '-',
             color='lightgreen')
    plt.plot(newx,
             func2D(np.array(newx), score4CV[0], score4CV[1], score4CV[2]),
             '-',
             color='skyblue')
    plt.plot(newx,
             func2D(np.array(newx), score7CV[0], score7CV[1], score7CV[2]),
             '-',
             color='orange')
    if not paper:
        plt.plot(newx,
                 func2D(np.array(newx), score8CV[0], score8CV[1], score8CV[2]),
                 '-',
                 color='red')

    # points
    plt.plot(xlist, list3, "s", markersize=4, label=label3, color='darkgreen')
    plt.plot(xlist, list6, "d", markersize=4, label=label6, color='navy')
    plt.plot(xlist,
             list2,
             'o',
             markersize=4,
             label=label2,
             color='mediumseagreen')
    plt.plot(xlist, list5, '^', markersize=4, label=label5, color='royalblue')
    plt.plot(xlist, list1, '+', markersize=4, label=label1, color='lightgreen')
    plt.plot(xlist, list4, '*', markersize=4, label=label4, color='skyblue')
    plt.plot(xlist_list7,
             list7,
             '3',
             markersize=4,
             label=label7,
             color='orange')
    if not paper:
        plt.plot(xlist_list8,
                 list8,
                 '4',
                 markersize=4,
                 label=label8,
                 color='red')

    handles, labels = plt.gca().get_legend_handles_labels()
    order = [4, 2, 0, 5, 3, 1, 6]
    if not paper:
        order = [4, 2, 0, 5, 3, 1, 6, 7]
    plt.xlim(xmin=0, xmax=1000)
    plt.ylim(ymin=0)
    plt.grid()

    plt.legend([handles[idx] for idx in order], [labels[idx] for idx in order])
    # plt.legend()
    plt.tight_layout()
    filename = dir_name + "/" + name + ".pdf"
    if paper:
        filename = dir_name + "/paper_" + name + ".pdf"
    plt.savefig(filename)
    plt.close()
    plt.rcParams.update(plt.rcParamsDefault)
Exemple #45
0
def createHistograms():

    # mean num optimal matchings per optimal matching type
    fig, ax = plt.subplots()
    ind = np.arange(6)
    width = 0.2
    exps = ['S100', 'S400', 'S700', 'S1000']
    exp_bars = []

    for i, exp in enumerate(exps):
        numMinRegret = float(d[exp + 'Av_MinRegret_num'][0])
        numEgalitarian = float(d[exp + 'Av_Egalitarian_num'][0])
        numBalanced = float(d[exp + 'Av_Balanced_num'][0])
        numSeqEqual = float(d[exp + 'Av_SexEqual_num'][0])
        numRegretEqual = float(d[exp + 'Av_RegretEqual_num'][0])
        numMinSumRegret = float(d[exp + 'Av_MinSumRegret_num'][0])

        av_counts = [
            numBalanced, numSeqEqual, numEgalitarian, numMinRegret,
            numRegretEqual, numMinSumRegret
        ]
        exp_bars.append(
            ax.bar(ind + i * width, av_counts, width, color=blues[i]))

    objects = [
        'Balanced', 'Sex-equal', 'Egalitarian', 'Minimum\nregret',
        'Regret-\nequal', 'Min-regret\nsum'
    ]
    y_pos = np.arange(len(objects))
    plt.grid()
    plt.xticks(y_pos + 0.3, objects)
    plt.ylabel('Mean number of stable matchings')
    plt.xlabel('Type of optimal stable matching')
    plt.ylim(ymin=0.7)
    plt.ylim(ymax=100)
    plt.yscale('log')
    ax.legend(exp_bars, exps)
    plt.tight_layout()

    plt.savefig(dir_name + "/paper_bar_optimal_nums.pdf")
    plt.close()

    # average number of matchings which exhibit different numbers of optimal
    # matchings
    fig, ax = plt.subplots()
    ind = np.arange(7)
    width = 0.2
    exps = ['S100', 'S400', 'S700', 'S1000']
    exp_bars = []

    for i, exp in enumerate(exps):
        num0 = float(d[exp + 'Av_0_matchingCount'][0])
        num1 = float(d[exp + 'Av_1_matchingCount'][0])
        num2 = float(d[exp + 'Av_2_matchingCount'][0])
        num3 = float(d[exp + 'Av_3_matchingCount'][0])
        num4 = float(d[exp + 'Av_4_matchingCount'][0])
        num5 = float(d[exp + 'Av_5_matchingCount'][0])
        num6 = float(d[exp + 'Av_6_matchingCount'][0])

        av_counts = [num0, num1, num2, num3, num4, num5, num6]
        exp_bars.append(
            ax.bar(ind + i * width, av_counts, width, color=blues[i]))

    objects = ['0', '1', '2', '3', '4', '5', '6']
    y_pos = np.arange(len(objects))
    plt.grid()
    plt.xticks(y_pos + 0.3, objects)
    plt.ylabel('Mean number of stable matchings')
    plt.xlabel('Number of optimal matching criteria satisfied')
    plt.ylim(ymin=0.07)
    plt.ylim(ymax=1300)
    plt.yscale('log')
    ax.legend(exp_bars, exps)
    plt.tight_layout()

    plt.savefig(dir_name + "/paper_bar_num_matchings_num_opt.pdf")
    plt.close()
Exemple #46
0
    def trace_density(self,
                      filepath,
                      savename,
                      directory,
                      skiprows,
                      column_names,
                      show=True):
        dir = directory + '/%s' % (savename.strip('.yaml'))
        if not os.path.exists(dir):
            os.makedirs(dir)

        if filepath.endswith('.yaml') == True:
            with open(filepath, 'r') as stream:
                data_file = yaml.load(stream)
                stream.close()
                data = data_file['data']
                # parameters = data_file['parameters']
        else:
            # parameters = open(filepath, "r").readlines()[:33]
            data = np.loadtxt(filepath, delimiter=',', skiprows=skiprows)

        length = len(data[0]) - (len(column_names) + 3)
        L_length = 4  #int(data[0][-1])
        R_length = 4  #int(data[0][-2])

        for i in range(R_length):
            column_names = np.append(column_names, "R_%i" % (i + 1))
        for i in range(L_length):
            column_names = np.append(column_names, "L_%i" % (i + 1))
        column_names = np.append(column_names, "Accepted")
        # column_names = np.append(column_names,"Rayleigh_length")
        # column_names = np.append(column_names,"Love_length")

        params = {
            'legend.fontsize': 'x-large',
            'figure.figsize': (15, 15),
            'axes.labelsize': 25,
            'axes.titlesize': 'x-large',
            'xtick.labelsize': 25,
            'ytick.labelsize': 25
        }
        pylab.rcParams.update(params)
        #
        df = pd.DataFrame(data, columns=column_names)
        df_select = df[["Epi", "Depth", "Strike", "Dip", "Rake"]]
        par = Get_Paramters()
        REAL = par.get_unkown()
        real_v = np.array([
            REAL['epi'], REAL['depth_s'], REAL['strike'], REAL['dip'],
            REAL['rake']
        ])
        # real_v=np.array([35.2068855191,16848.1405882,99.88000245897199, 77.02994881296266,68.80551508147109])
        # real_v=np.array([73.8059395565,26247.7456326,158.92744919732135, 47.46909146946276,-45.69139707143311])
        strike, dip, rake = aux_plane(REAL['strike'], REAL['dip'],
                                      REAL['rake'])
        # strike,dip,rake = aux_plane(99.88000245897199,77.02994881296266,68.80551508147109)
        # strike,dip,rake = aux_plane(158.92744919732135, 47.46909146946276,-45.69139707143311)

        fig = plt.figure(figsize=(20, 20))
        row = 0

        for i in df_select:
            ax1 = plt.subplot2grid((5, 2), (row, 0))
            ax1.plot(df_select[i], label=i)
            # ax1.axhline(y=REAL[df_select], linewidth=0.3, linestyle=':')
            ax1.set_title("Trace %s" % i, color='b', fontsize=20)
            ax1.set_xlabel("Iteration")
            # ax1.set_ylabel("Epicentral ")
            plt.tight_layout()
            ax2 = plt.subplot2grid((5, 2), (row, 1))
            plt.hist(df_select[i], bins=100)
            ymin, ymax = ax2.get_ylim()
            plt.vlines(df_select[i][1],
                       ymin=ymin,
                       ymax=ymax,
                       colors='r',
                       linewidth=3)
            plt.vlines(real_v[row],
                       ymin=ymin,
                       ymax=ymax,
                       colors='k',
                       linewidth=3)
            if i == 'Strike':
                plt.vlines(strike,
                           ymin=ymin,
                           ymax=ymax,
                           colors='g',
                           linewidth=3)
            if i == 'Dip':
                plt.vlines(dip, ymin=ymin, ymax=ymax, colors='g', linewidth=3)
            if i == 'Rake':
                plt.vlines(rake, ymin=ymin, ymax=ymax, colors='g', linewidth=3)

            # y, binEdges = np.histogram(df_select[i], bins=100)
            # bincenters = 0.5 * (binEdges[1:] + binEdges[:-1])
            # pylab.plot(bincenters, y, '-',label = "%s" % i)
            ax2.set_title("Density %s" % i, color='b', fontsize=20)
            ax2.set_xlabel("N=%i" % (len(df_select[i])))
            plt.tight_layout()
            row += 1
        plt.savefig(dir + '/Trace_density.pdf')
Exemple #47
0
def visualize_stats(data, out):
    f, axs = plt.subplots(2, 2, sharey=False, sharex=False)

    X = plt.arange(data.shape[0])
    width = 0.4
    title_fontsize = 10
    label_fontsize = 6

    # number of detected clusters
    bars3dsize = axs[0][0].bar(X, data[:, 1], width, color=COLOR1)
    bars1dsize = axs[0][0].bar(X + width, data[:, 2], width, color=COLOR2)
    axs[0][0].legend((bars3dsize[0], bars1dsize[0]), ('3D gene clusters', \
            '1D gene clusters'), fontsize=title_fontsize)
    axs[0][0].set_xticks(X + width / 2)
    axs[0][0].set_xticklabels(map(int, data[:, 0]))
    axs[0][0].set_ylabel('count')
    #    axs[0][0].set_xlabel(r'$\delta$')
    axs[0][0].set_title('number of discovered clusters',
                        fontsize=title_fontsize)

    # average size of detected clusters
    bars3dsize = axs[0][1].bar(X, data[:, 3], width, color=COLOR1)
    bars1dsize = axs[0][1].bar(X + width, data[:, 4], width, color=COLOR2)
    axs[0][1].legend((bars3dsize[0], bars1dsize[0]), ('3D gene clusters', \
            '1D gene clusters'), fontsize=title_fontsize)
    axs[0][1].set_xticks(X + width / 2)
    axs[0][1].set_xticklabels(map(int, data[:, 0]))
    axs[0][1].set_ylabel('avg. size')
    #    axs[0][1].set_xlabel(r'$\delta$')
    axs[0][1].set_title('average size of discovered clusters',
                        fontsize=title_fontsize)
    axs[0][1].set_yscale('log')

    # runtime of GraphTeams
    axs[1][1].scatter(X + width / 2, data[:, 5], color=COLOR1)
    #    axs[1][0].legend(('1D gene clusters', ), fontsize=title_fontsize)
    axs[1][1].set_xticks(X + width / 2)
    axs[1][1].set_xticklabels(map(int, data[:, 0]))
    axs[1][1].set_ylim([0, max(data[:, 5]) + 10])
    axs[1][1].set_ylabel('time in mins')
    axs[1][1].set_xlabel(r'$\delta$')
    axs[1][1].set_title('computation time of 3D clusters',
                        fontsize=title_fontsize)

    # spatial gain 3D clusters
    axs[1][0].bar(X + (width / 2), data[:, 6], width, color=COLOR1)
    #    axs[1][1].legend(('3D gene clusters', ), fontsize=title_fontsize)
    axs[1][0].set_xticks(X + width / 2)
    axs[1][0].set_xticklabels(map(int, data[:, 0]))
    axs[1][0].set_ylabel('avg. number of gained genes')
    axs[1][0].set_xlabel(r'$\delta$')
    axs[1][0].set_title('spatial gain of 3D clusters', fontsize=title_fontsize)
    axs[1][0].set_yscale('log')

    for axsx in axs:
        for axsy in axsx:
            for label in axsy.get_xmajorticklabels():
                label.set_rotation(90)
                label.set_fontsize(label_fontsize)
                #label.set_horizontalalignment('right')
            for label in axsy.get_ymajorticklabels():
                label.set_fontsize(label_fontsize)

    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    f.savefig(out, format='pdf')
Exemple #48
0
                    obs_odd_mean + obs_odd_sem,
                    color=odd_colour,
                    alpha=0.2,
                    linewidth=0)
ax_obs.axhline(0, linewidth=0.5, color="black")
ax_obs.axvline(0, linestyle="--", linewidth=0.5, color="black")
plt.title("Observation phase")
plt.legend(loc=1)
plt.xlabel("Time[s]")
plt.ylabel("fT")
plt.ylim(-10, 10)
plt.xlim(-0.1, 1.0)

ax_subset = figure.add_subplot(gs[1])

mne.viz.plot_topomap(np.zeros(274),
                     info,
                     cmap="Greys",
                     vmin=0,
                     vmax=0,
                     mask=ch_selection,
                     mask_params=mask_params,
                     axes=ax_subset,
                     show=False)
plt.title("Channel subset")

plt.tight_layout(w_pad=0.2, h_pad=0.2)

out_path = op.join(img_save, "ERF_ttest_{}.svg".format(key))
plt.savefig(out_path, bbox_inches="tight")
plt.show()
Exemple #49
0
def plot_markers():

    from matplotlib.colors import LogNorm
    SMALL_SIZE = 15
    MEDIUM_SIZE = 22
    plt.rc('font', size=MEDIUM_SIZE)  # controls default text sizes
    plt.rc('axes', titlesize=MEDIUM_SIZE)  # fontsize of the axes title
    plt.rc('axes', labelsize=MEDIUM_SIZE)  # fontsize of the x and y labels
    plt.rc('xtick', labelsize=SMALL_SIZE)  # fontsize of the tick labels
    plt.rc('ytick', labelsize=SMALL_SIZE)  # fontsize of the tick labels
    from matplotlib import rcParams
    rcParams['font.family'] = 'STIXGeneral'  # 'Times New Roman'
    rcParams['mathtext.fontset'] = 'custom'
    rcParams['mathtext.rm'] = 'Bitstream Vera Sans'
    # mpl.rcParams['mathtext.it'] = 'Bitstream Vera Sans:italic'
    rcParams['mathtext.bf'] = 'Bitstream Vera Sans:bold'

    # with open(LCmapFile, 'rb') as handle:
    #     LCmap = pickle.load(handle)
    # xs = [65, 23, 80, 23]
    # ys = [64, 106, 66, 42]
    xs = [65, 25, 80, 23]
    ys = [64, 103, 66, 42]
    fig = plt.figure(figsize=(20, 10))

    # im_ax = fig.add_subplot(1,2,1)
    im_ax = fig.add_axes([0.00, 0.25, 0.35, 0.55])
    interval_map = np.sum(LCmap[:, :, :1], axis=2)
    im = im_ax.imshow(np.abs(interval_map) + 1e-9,
                      interpolation='none',
                      origin='lower',
                      vmin=1,
                      norm=LogNorm(),
                      cmap="inferno")
    plt.colorbar(im)
    # annotate_axis(im, im_ax, interval_map.shape[0])
    im_ax.axis('off')
    im_ax.text(3, 121, 'a', color='w', fontweight='bold', fontsize='30')
    labels = ['b', 'c', 'd', 'e']
    axes = []
    subposes = [[0.1, 0.4, 0.4, 0.4], [0.07, 0.4, 0.4, 0.4],
                [0.55, 0.4, 0.4, 0.4], [0.55, 0.4, 0.4, 0.4]]
    for i in [2, 3, 5, 6]:
        axes.append(fig.add_subplot(2, 3, i))
    intses = []
    for ia, (ix, iy) in enumerate(zip(xs, ys)):
        circle = plt.Circle((ix, iy),
                            radius=4,
                            color='w',
                            fill=False,
                            linewidth=2)
        im_ax.add_artist(circle)
        im_ax.text(ix - 2,
                   iy + 6,
                   labels[ia],
                   color='w',
                   fontweight='bold',
                   fontsize=25)  #fontsize is newly added

        ints = LCmap[iy, ix]
        intses.append(ints)
        ID = pipe.get_intensity_dist(ints)
        bincent = (ID['binsS'] + np.roll(ID['binsS'], 1)) / 2.
        bincent = np.array(bincent)[1:]
        guessIc = np.mean(ints) * 0.7
        guessIs = np.mean(ints) * 0.3
        barwidth = (bincent[-1] - bincent[0]) / len(bincent)
        popt, _ = curve_fit(MR, bincent, ID['histS'], p0=[guessIc, guessIs])
        axes[ia].bar(bincent,
                     ID['histS'],
                     width=barwidth,
                     edgecolor='k',
                     facecolor='lightgrey')
        axes[ia].plot(bincent, MR(bincent, *popt), 'r--', linewidth=2)
        # Imean = np.mean(ints)
        # Istd = np.std(ints)
        # Ic= np.sqrt(Imean ** 2 - Istd ** 2)
        # Is = Imean - Ic
        # axes[ia].plot(bincent, MR(bincent, Ic, Is), 'r--')
        axes[ia].set_xlabel('Intensity (counts)')
        axes[ia].tick_params(direction='in',
                             which='both',
                             right=True,
                             top=True)
        r = popt[0] / popt[1]
        print r
        axes[ia].set_ylim([0, 0.165])
        axes[ia].text(0.03,
                      0.9,
                      labels[ia],
                      transform=axes[ia].transAxes,
                      fontweight='bold')
        props = dict(boxstyle='square', facecolor='w')
        axes[ia].text(0.97,
                      0.9,
                      r'$I_C=$%.1f     $I_S=$%.1f      $I_C/I_S=$%.1f' %
                      (popt[0], popt[1], r),
                      transform=axes[ia].transAxes,
                      ha='right',
                      bbox=props)

        print 'in order to put the data at the top in a square make 3  text instances with ha = left center and right. Then do plt.Rectangle around all 3. You will need to add some hspace too after you call tight_layout'
        # axes[ia].text(1.1,0.5, r'$I_C=$%.1f      $I_S=$%.1f      $I_C/I_S=$%.1f' % (popt[0],popt[1],r), transform=axes[ia].transAxes, ha='center', bbox=props)

        # if ia <2: axes[ia].text(subposes[ia][0],0.25, r'$\frac{I_C}{I_S}=$%.1f' % r, transform=axes[ia].transAxes)
        # else: axes[ia].text(subposes[ia][0] + subposes[ia][3] ,0.25, r'$\frac{I_C}{I_S}=$%.1f' % r, ha='right', transform=axes[ia].transAxes)
        # axes[ia].legend([popt[0], popt[1], r], ['$I_C=$%.1f', '$I_S=$%.1f',r'$\frac{I_C}{I_S}=$%.1f'], loc=3)
        if ia == 1:
            axes[ia].set_xlim([20, 110])
    plt.tight_layout()
    for ia in range(4):
        subax1 = add_subplot_axes(axes[ia], subposes[ia])
        subax1.plot(np.linspace(0, 2, len(ints)), intses[ia])
        subax1.set_xlabel('Time (s)')

    # plt.subplots_adjust(left=0.11, right=0.99, top=0.98, bottom=0.14, wspace=0.2)
    plt.show()
Exemple #50
0
def createDurationPlot(xlist, xlabel, ylabel, \
        re_alg_duration_median, re_alg_duration_5, re_alg_duration_95, alg_label, \
        re_algAlt_duration_median, re_algAlt_duration_5, re_algAlt_duration_95, algAlt_label, \
        re_enum_duration_median, re_enum_duration_5, re_enum_duration_95, enum_label):

    xlist_alg, xlist_algAlt, xlist_enum = [], [], []
    new_re_alg_duration_median, new_re_alg_duration_5, new_re_alg_duration_95 = [], [], []
    new_re_algAlt_duration_median, new_re_algAlt_duration_5, new_re_algAlt_duration_95 = [], [], []
    new_re_enum_duration_median, new_re_enum_duration_5, new_re_enum_duration_95 = [], [], []

    # if paper:
    #     matplotlib.rcParams.update({'font.size': 14})

    # collecting the data separately in case we have all timeouts on one particular algorithm
    for i, n in enumerate(xlist):
        if not re_alg_duration_median[i] == -1:
            xlist_alg.append(n)
            new_re_alg_duration_median.append(re_alg_duration_median[i])
            new_re_alg_duration_5.append(re_alg_duration_5[i])
            new_re_alg_duration_95.append(re_alg_duration_95[i])
        if not re_algAlt_duration_median[i] == -1:
            xlist_algAlt.append(n)
            new_re_algAlt_duration_median.append(re_algAlt_duration_median[i])
            new_re_algAlt_duration_5.append(re_algAlt_duration_5[i])
            new_re_algAlt_duration_95.append(re_algAlt_duration_95[i])
        if not re_enum_duration_median[i] == -1:
            xlist_enum.append(n)
            new_re_enum_duration_median.append(re_enum_duration_median[i])
            new_re_enum_duration_5.append(re_enum_duration_5[i])
            new_re_enum_duration_95.append(re_enum_duration_95[i])

    re_alg_duration_median = new_re_alg_duration_median
    re_algAlt_duration_median = new_re_algAlt_duration_median
    re_enum_duration_median = new_re_enum_duration_median
    re_alg_duration_5 = new_re_alg_duration_5
    re_algAlt_duration_5 = new_re_algAlt_duration_5
    re_enum_duration_5 = new_re_enum_duration_5
    re_alg_duration_95 = new_re_alg_duration_95
    re_algAlt_duration_95 = new_re_algAlt_duration_95
    re_enum_duration_95 = new_re_enum_duration_95

    # starting the plotting
    plt.figure()
    plt.figure(facecolor='w', edgecolor='k', figsize=(7, 5))
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)

    xlist_alg = np.array(xlist_alg)
    xlist_algAlt = np.array(xlist_algAlt)
    xlist_enum = np.array(xlist_enum)

    # curves
    algCV, _ = curve_fit(func2D, np.log(xlist_alg),
                         np.log(re_alg_duration_median))
    algAltCV, _ = curve_fit(func2D, np.log(xlist_algAlt),
                            np.log(re_algAlt_duration_median))
    enumCV, _ = curve_fit(func2D, np.log(xlist_enum),
                          np.log(re_enum_duration_median))
    alg_5_CV, _ = curve_fit(func2D, np.log(xlist_alg),
                            np.log(re_alg_duration_5))
    algAlt_5_CV, _ = curve_fit(func2D, np.log(xlist_algAlt),
                               np.log(re_algAlt_duration_5))
    enum_5_CV, _ = curve_fit(func2D, np.log(xlist_enum),
                             np.log(re_enum_duration_5))
    alg_95_CV, _ = curve_fit(func2D, np.log(xlist_alg),
                             np.log(re_alg_duration_95))
    algAlt_95_CV, _ = curve_fit(func2D, np.log(xlist_algAlt),
                                np.log(re_algAlt_duration_95))
    enum_95_CV, _ = curve_fit(func2D, np.log(xlist_enum),
                              np.log(re_enum_duration_95))

    newx_alg = np.logspace(1, 3, 250)
    newx_algAlt = np.logspace(1, 3, 250)
    newx_algenum = np.logspace(1, 3, 250)

    if paper:
        # plot the best fit curves
        plt.plot(newx_alg,
                 np.exp(func2D(np.log(newx_alg), algCV[0], algCV[1],
                               algCV[2])),
                 '-',
                 color=blues[1])
        plt.plot(newx_algenum,
                 np.exp(
                     func2D(np.log(newx_algenum), enumCV[0], enumCV[1],
                            enumCV[2])),
                 '-',
                 color=blues[3])

        # plot the points
        plt.plot(xlist_alg,
                 re_alg_duration_median,
                 'o',
                 markersize=4,
                 label=alg_label,
                 color=blues[1])
        plt.plot(xlist_enum,
                 re_enum_duration_median,
                 's',
                 markersize=4,
                 label=enum_label,
                 color=blues[3])

        # plot the confidence intervals
        plt.fill_between(newx_alg, np.exp(func2D(np.log(newx_alg), alg_5_CV[0], alg_5_CV[1], alg_5_CV[2])), \
                                np.exp(func2D(np.log(newx_alg), alg_95_CV[0], alg_95_CV[1], alg_95_CV[2])), color=blues[0], alpha=0.8)
        plt.fill_between(newx_algenum, np.exp(func2D(np.log(newx_algenum), enum_5_CV[0], enum_5_CV[1], enum_5_CV[2])), \
                                np.exp(func2D(np.log(newx_algenum), enum_95_CV[0], enum_95_CV[1], enum_95_CV[2])), color=blues[2], alpha=0.8)

    else:
        # plot the best fit curves
        plt.plot(newx_alg,
                 np.exp(func2D(np.log(newx_alg), algCV[0], algCV[1],
                               algCV[2])),
                 '-',
                 color='skyblue')
        plt.plot(newx_algAlt,
                 np.exp(
                     func2D(np.log(newx_algAlt), algAltCV[0], algAltCV[1],
                            algAltCV[2])),
                 '-',
                 color='orangered')
        plt.plot(newx_algenum,
                 np.exp(
                     func2D(np.log(newx_algenum), enumCV[0], enumCV[1],
                            enumCV[2])),
                 '-',
                 color='seagreen')

        # plot the points
        plt.plot(xlist_alg,
                 re_alg_duration_median,
                 'o',
                 markersize=4,
                 label=alg_label,
                 color='skyblue')
        plt.plot(xlist_algAlt,
                 re_algAlt_duration_median,
                 '*',
                 markersize=4,
                 label=algAlt_label,
                 color='orangered')
        plt.plot(xlist_enum,
                 re_enum_duration_median,
                 's',
                 markersize=4,
                 label=enum_label,
                 color='seagreen')

        plt.plot([0, 1000], [timeout_ms, timeout_ms],
                 "--",
                 label="Timeout",
                 color='red')

        # plot the confidence intervals
        plt.fill_between(newx_alg, np.exp(func2D(np.log(newx_alg), alg_5_CV[0], alg_5_CV[1], alg_5_CV[2])), \
                                np.exp(func2D(np.log(newx_alg), alg_95_CV[0], alg_95_CV[1], alg_95_CV[2])), color='skyblue', alpha=.5)
        plt.fill_between(newx_algAlt, np.exp(func2D(np.log(newx_algAlt), algAlt_5_CV[0], algAlt_5_CV[1], algAlt_5_CV[2])), \
                                    np.exp(func2D(np.log(newx_algAlt), algAlt_95_CV[0], algAlt_95_CV[1], algAlt_95_CV[2])), color='orangered', alpha=.5)
        plt.fill_between(newx_algenum, np.exp(func2D(np.log(newx_algenum), enum_5_CV[0], enum_5_CV[1], enum_5_CV[2])), \
                                np.exp(func2D(np.log(newx_algenum), enum_95_CV[0], enum_95_CV[1], enum_95_CV[2])), color='seagreen', alpha=.5)

    # plt.plot([100, 1000], [100, 1000000], "--", label="Gradient 4n")

    plt.xlim(xmin=0, xmax=1000)
    plt.ylim(ymin=100)
    plt.yscale('log')
    plt.grid()

    plt.legend(loc='upper left')
    plt.tight_layout()
    filename = dir_name + "/duration.pdf"
    if paper:
        filename = dir_name + "/paper_duration.pdf"
    plt.savefig(filename)
    plt.close()
    plt.rcParams.update(plt.rcParamsDefault)
from  matplotlib import pylab

if __name__ == '__main__':
    fh = open('read_lengths.txt')
    L = [int(x.strip()) for x in fh]
    fh.close()
    pylab.hist(L,120, histtype='bar')
    pylab.tight_layout();
    pylab.show()
def plot(index):
    ts = 1e-12  # time resolution
    tos = 19e-9
    tend = 30e-9
    wz1 = -2.0 * np.pi * 0.55e9
    wp1 = -2.0 * np.pi * 1.2e9
    wp2 = -2.0 * np.pi * 3.4e9
    gain = -1.0 * wp1 * wp2 / wz1

    if index == 1:
        simout_filename = 'eq_out1.txt'
        gs_filename = 'ctle_pulse_response_1.eps'
    elif index == 2:
        simout_filename = 'eq_out2.txt'
        gs_filename = 'ctle_pulse_response_2.eps'
    elif index == 3:
        simout_filename = 'eq_out3.txt'
        gs_filename = 'ctle_pulse_response_3.eps'

    # load verilog simulation data and get 1d interpolator
    data1 = np.loadtxt(simout_filename)
    t = data1[:, 0]
    yv = data1[:, 1]
    fn_vlog = interp1d(t, yv)
    time = np.arange(t[0], t[-1], ts)
    time2 = np.arange(t[0], tend, ts)
    time = time[:-1]
    time2 = time2[:-1]
    no_sample = len(time2) - len(time)

    xdata = np.loadtxt('input.txt')
    xt = np.append(xdata[:, 0], t[-1])
    xy = np.append(xdata[:, 1], xdata[:, 1][-1])
    fn_x = interp1d(xt, xy)
    x = fn_x(time)
    if no_sample > 0:
        x2 = np.array(list(x) + no_sample * [x[-1]])
    else:
        x2 = x[:no_sample]

    # ideal transfer function in s-domain
    t0, y0, x0 = lsim(zpk2tf([wz1], [wp1, wp2], gain), x2, time2)

    # residual error
    y_vlog = fn_vlog(time)
    if no_sample > 0:
        y_vlog = np.array(list(y_vlog) + no_sample * [y_vlog[-1]])
    else:
        y_vlog = y_vlog[:no_sample]

    err = y_vlog - y0
    print 'Max. residual error = %.1f [mV]' % (max(abs(err[t0 > tos])) * 1000)

    # plot time, value pairs at which events occur
    plt.subplot(2, 1, 1)
    plt.plot(1e9 * t[t > tos],
             yv[t > tos],
             'or',
             label='Verilog',
             markersize=5)
    plt.plot(1e9 * t0[t0 > tos], y0[t0 > tos], label='Analytic expression')
    plt.xlabel('Time [nsec]')
    plt.ylabel('Output [V]')
    plt.axis([tos * 1e9, tend * 1e9, -0.20, 0.20])
    #plt.title('Pulse response of CTLE ('+r'$e_{tol}$'+'=1 [mV])')
    plt.legend(loc=0)

    plt.subplot(2, 1, 2)
    plt.plot(1e9 * t0[t0 > tos], 1000 * err[t0 > tos], 'r')
    plt.xlabel('Time [nsec]')
    plt.ylabel('Residual error [mV]')
    plt.axis([tos * 1e9, tend * 1e9, -20, 20])
    plt.tight_layout()
    plt.savefig(gs_filename)
    plt.close()
Exemple #53
0
plt.plot(ts, label = 'Original')
plt.legend(loc = 'best')

plt.subplot(4, 1, 2)
plt.plot(tendencia, label = 'Tendencia')
plt.legend(loc = 'best')

plt.subplot(4, 1, 3)
plt.plot(sazonal, label = 'Sazionalidade')
plt.legend(loc = 'best')

plt.subplot(4, 1, 4)
plt.plot(aleatorio, label = 'Aleatorio')
plt.legend(loc = 'best')

plt.tight_layout() #para mostrar o legends

# =============================================================================
# Previsão com media
# =============================================================================

ts.mean()

# =============================================================================
# Média do ultimo ano
# =============================================================================

ts['1960-01-01':'1960-12-01'].mean()

# =============================================================================
# Media movel
Exemple #54
0
import numpy as np
import matplotlib.pylab as plt
import pylab
from kernel import KernelBasisDE
from scipy.special import legendre
params = {
    'backend': 'ps',
    'axes.labelsize': 18,
    'text.fontsize': 18,
    'axes.titlesize': 18,
    'legend.fontsize': 18,
    'xtick.labelsize': 18,
    'ytick.labelsize': 18,
    'text.usetex': True,
}
pylab.rcParams.update(params)

def find_zeros(x, y):
    r = []
    for i in range(len(y) - 1):
        if y[i] * y[i + 1] < 0:
            zero = (x[i] * np.abs(y[i + 1]) + x[i + 1] * np.abs(y[i])) / (
                np.abs(y[i]) + np.abs(y[i + 1]))
            r.append(zero)
    return np.array(r)

results = []
N = 1001
il_list = [0, 1, 2]
marker_list = ['o', 'x', '+', 'v']
Exemple #55
0
def quicklook_im(image,
                 logAmp=False,
                 show=True,
                 vmin=None,
                 vmax=None,
                 axis=False,
                 anno=None,
                 annos=None,
                 title=None,
                 pupil=False,
                 colormap="YlGnBu_r",
                 mark_star=False,
                 label=None):
    # MEDIUM_SIZE = 27
    # plt.rc('font', size=MEDIUM_SIZE)  # controls default text sizes

    # mgr = plt.get_current_fig_manager()
    # mgr.full_screen_toggle()
    # py = mgr.canvas.height()
    # px = mgr.canvas.width()
    # dprint((py,px))
    # mgr.window.close()

    if pupil:
        import Analysis.phot
        image = image * Analysis.phot.aperture(
            tp.grid_size / 2, tp.grid_size / 2, tp.grid_size / 2)

    if show != 'continuous':
        fig = plt.figure()
    else:
        fig = sp.fig
        vmax = sp.vmax
        vmin = sp.vmin
    if title == None:
        title = r'  $I / I^{*}$'

    ax = fig.add_subplot(111)
    if logAmp:
        if np.min(image) <= 0:

            cax = ax.imshow(image,
                            interpolation='none',
                            origin='lower',
                            vmin=vmin,
                            vmax=vmax,
                            norm=SymLogNorm(linthresh=1e-5),
                            cmap="YlGnBu_r")
        else:
            cax = ax.imshow(image,
                            interpolation='none',
                            origin='lower',
                            vmin=vmin,
                            vmax=vmax,
                            norm=LogNorm(),
                            cmap="YlGnBu_r")

    else:
        cax = ax.imshow(image,
                        interpolation='none',
                        origin='lower',
                        vmin=vmin,
                        vmax=vmax,
                        cmap=colormap)
    if axis:
        annotate_axis(cax, ax, image.shape[0])
    if show == 'continuous':
        fig.canvas.draw()
        if not sp.cbar:
            sp.cbar = plt.colorbar(
                cax)  #norm=LogNorm(vmin=cax.min(), vmax=cax.max()))
            clims = sp.cbar.get_clim()
            sp.vmax = clims[1]
            sp.vmin = clims[0]
        plt.show(block=False)
        # import time
        # time.sleep(5.0)
    else:
        # cb = plt.colorbar(cax, format=ticker.FuncFormatter(fmt))
        cb = plt.colorbar(cax)

    if axis == None:
        ax.axis('off')
        # ax.text(0.84, 0.9, '0.2"', transform=ax.transAxes, fontweight='bold', color='w', ha='center', fontsize=14, family='serif')
        # ax.plot([0.78, 0.9], [0.87, 0.87],transform=ax.transAxes, color='w', linestyle='-', linewidth=3)
        # cb.ax.set_title(title, fontsize=16)
        # if anno:
        #     ax.text(0.05, 0.05, anno, transform=ax.transAxes, fontweight='bold', color='w', fontsize=22)
    if anno:
        props = dict(boxstyle='square', facecolor='k', alpha=0.3)
        ax.text(0.05,
                0.05,
                anno,
                transform=ax.transAxes,
                fontweight='bold',
                color='w',
                fontsize=22,
                bbox=props)
    if label:
        props = dict(boxstyle='square', facecolor='k', alpha=0.3)
        ax.text(0.05,
                0.9,
                label,
                transform=ax.transAxes,
                fontweight='bold',
                color='w',
                fontsize=22,
                family='serif',
                bbox=props)
    # ax.text(0.84, 0.9, '0.5"', transform=ax.transAxes, fontweight='bold', color='w', ha='center', fontsize=14, family='serif')
    # ax.plot([0.78, 0.9], [0.87, 0.87],transform=ax.transAxes, color='w', linestyle='-', linewidth=3)
    if mark_star:
        ax.plot(image.shape[0] / 2, image.shape[1] / 2, marker='*', color='r')

    plt.tight_layout()

    # # For plotting on the leftmost screen
    # figManager = plt.get_current_fig_manager()
    # # if px=0, plot will display on 1st screen
    # figManager.window.move(-1920, 0)
    # figManager.window.setFocus()

    if show == True:
        plt.show(block=True)
vi = 100 * 1000 / 3600.
z0 = sp.array([0, 0, vi, vi])

# Viento:
V = 0  #[m/s]

while V <= 20:

    sol = odeint(bala, z0, t)

    x = sol[:, 0]
    y = sol[:, 1]

    plt.figure(1)
    plt.plot(x, y)

    V += 10

# Grafico:
plt.legend(['V=0 m/s', 'V=10 m/s', 'V=20 m/s'])
plt.title('Trayectoria para distintos vientos')
plt.xlabel('X (m)')
plt.ylabel('Y (m)')
plt.axis([0, 150, 0, 50])
plt.grid(True)

plt.tight_layout()

# Guardar grafico como png sin abrir una ventana de visualizacion
plt1.savefig('Trayectoria para distintos vientos')
Exemple #57
0
def view_datacube(datacube,
                  show=True,
                  logAmp=False,
                  axis=True,
                  vmin=None,
                  vmax=None,
                  width=5):
    '''axis = anno/None/True'''
    fig = plt.figure(figsize=(14, 7))
    colors = len(datacube)
    print(colors, width)
    height = int(np.ceil(colors / float(width)))
    print(height)
    peak = np.max(datacube)
    trough = np.min(datacube)
    # print peak, trough, 'max'

    if vmin != None:
        if peak >= vmin:
            vmax = peak
    else:
        vmin = np.min(datacube)
    dprint((vmin, peak))
    for w in range(colors):
        ax = fig.add_subplot(height, width, w + 1)
        if logAmp:
            if vmin <= 0:
                # datacube[w] = np.abs(datacube[w] + 1e-9)
                im = ax.imshow(datacube[w],
                               interpolation='none',
                               origin='lower',
                               vmin=vmin,
                               vmax=vmax,
                               norm=SymLogNorm(linthresh=1e-5),
                               cmap="YlGnBu_r")
            else:
                im = ax.imshow(datacube[w],
                               interpolation='none',
                               origin='lower',
                               vmin=vmin,
                               vmax=peak,
                               norm=LogNorm(),
                               cmap="YlGnBu_r")
        else:
            im = ax.imshow(datacube[w],
                           interpolation='none',
                           origin='lower',
                           vmin=vmin,
                           vmax=peak,
                           cmap="YlGnBu_r")
        if axis == 'anno':
            annotate_axis(im, ax, datacube.shape[1])
        if axis == None:
            plt.axis('off')
        # ax.grid(color='w', linestyle='--')

    if axis:
        # fig.subplots_adjust(bottom=0.1)
        cbar_ax = fig.add_axes([0.45, 0.15, 0.4, 0.05])
        fig.colorbar(im, cax=cbar_ax, orientation='horizontal')

    # # For plotting on the leftmost screen
    # figManager = plt.get_current_fig_manager()
    # # if px=0, plot will display on 1st screen
    # figManager.window.move(-1920, 0)
    # figManager.window.setFocus()

    if show == True:
        plt.tight_layout()
        plt.show(block=True)
X_ticks[12] = "Lack of spontaneity (N6)"

cl_mean = np.mean(X_org[X_cl_labels == 0], axis=0)
n_subs = np.sum(X_cl_labels == 0)
ax1 = sns.barplot(X_ticks, cl_mean, palette=my_palette, ax=axarr[0])
ax1.xaxis.set_ticks_position('top')
rotateTickLabels(ax1, 45, 'x')
ax1.xaxis.set_ticklabels(X_ticks, fontsize=12)
ax1.set_xlabel('%i patients' % n_subs, fontsize=16)
ax1.set_ylabel("Group 1", fontsize=16)

cl_mean = np.mean(X_org[X_cl_labels == 1], axis=0)
n_subs = np.sum(X_cl_labels == 1)
ax2 = sns.barplot(X_ticks, cl_mean, palette=my_palette, ax=axarr[1])
ax2.set_xlabel('%i patients' % n_subs, fontsize=16)
ax2.set_ylabel("Group 2", fontsize=16)

cl_mean = np.mean(X_org[X_cl_labels == 2], axis=0)
n_subs = np.sum(X_cl_labels == 2)
ax3 = sns.barplot(X_ticks, cl_mean, palette=my_palette, ax=axarr[2])
ax3.set_xlabel('%i patients' % n_subs, fontsize=16)
ax3.set_ylabel("Group 3", fontsize=16)

ax3.tick_params(axis='x', labelbottom='off')
# sns.despine(bottom=True)
plt.setp(f.axes, ylim=[0, 5.])
# plt.xticks(rotation= 90, fontsize=8)
plt.tight_layout(h_pad=3)
# plt.savefig('plots/kmeans_3cl_noYaxis.png')
plt.show()
Exemple #59
0
axs[3].plot(mjd, df['par_2'], 'o', mec='black')
axs[3].set_ylabel('Disk blackbody\nkT (keV)')
axs[3].set_ylim(0.15, 0.18)

axs[4].plot(mjd, np.sqrt(df['par_3']), 'o', mec='black')
axs[4].set_ylabel('Inner disk radius\n'
                  r'R$_{in}d_{10 kpc}/\sqrt{\cos {\theta}}$ (km)')
axs[4].set_ylim(380, 900)

axs[5].plot(mjd, df['par_4'], 'o', mec='black', mfc='orange')
axs[5].set_ylabel('Power-law \nPhoton index')

axs[6].plot(mjd, df['par_7'] / 100.0, 'o', mec='black', mfc='orange')
axs[6].set_ylabel(
    'Unabsorb PL 2-10 keV flux \n($10^{-10}$ erg s$^{-1}$ cm$^{-2}$) ')
axs[-1].set_xlabel('MJD')
for ax in axs:
    ax.label_outer()
    #ax.minorticks_on()
    ax.xaxis.grid(True)
    ax.xaxis.grid(which='major', linestyle='--', color='#000000')
    ax.xaxis.grid(which='minor', linestyle='-.')
    ax.tick_params(axis="both", which='major', direction='in', length=5)
    ax.tick_params(axis="both", which='minor', direction='in', length=3)
outpdf = 'test.pdf'
fig.align_ylabels(axs)
plt.tight_layout(pad=2)
plt.rcParams["font.family"] = "serif"
plt.rcParams["mathtext.fontset"] = "dejavuserif"
plt.savefig(outpdf)
def process(f, i):
    path = 'time_series_images/' + os.path.basename(f) + '.png'
    if os.path.exists(path):
        print('Exists, skipping ...')
        return

    j = json.loads(open(f).read())

    p = j['features'][0]['properties']

    # fr = p['water_area_filled_fraction']

    t = p['water_area_time']
    v1 = p['water_area_value']
    v2 = p['water_area_filled']

    t_jrc = p['water_area_time_jrc']
    v_jrc = p['water_area_value_jrc']

    filled_fr = list(zip(v1, v2))
    filled_fr = [(o[1] - o[0]) / o[1] for o in filled_fr]

    mask = ma.masked_greater_equal(filled_fr, 0.5)

    # t = list(ma.masked_array(t, mask).compressed())
    # v1 = list(ma.masked_array(v1, mask).compressed())
    # v2 = list(ma.masked_array(v2, mask).compressed())

    if not len(t):
        print('Empty, skipping ...')
        return

    years = mdates.YearLocator()  # every year

    v2_filtered = savitzky_golay(np.array(v2), window_size=15, order=4)
    # v2_filtered = signal.medfilt(v2, 7)
    # v2_filtered = lowess(v2, t)
    # v2_filtered = lowess(v2, t, frac=1./50)

    t = [datetime.datetime.fromtimestamp(tt / 1000) for tt in t]
    t_jrc = [
        datetime.datetime.fromtimestamp(tt_jrc / 1000) for tt_jrc in t_jrc
    ]

    s_scale = 'Scale: {:.2f}'.format(p['scale']) + '$m$'
    s_area = 'Area: {:.2f}'.format(
        p['area'] /
        (1000 * 1000)) + '$km^2$, ' + '{:.2f}'.format(100 * p['area'] /
                                                      (1000 * 1000)) + '$ha$'
    title = s_scale + ', ' + s_area

    fig = plt.figure(figsize=(11, 4))
    ax = fig.add_subplot(111)
    ax.xaxis.set_major_locator(years)

    # fig.autofmt_xdate()
    ax.set_xlim([datetime.date(1985, 1, 1), datetime.date(2019, 1, 1)])

    ax.grid(color='k', linestyle='-', linewidth=1, alpha=0.2)

    plt.title(title)

    plt.xticks(rotation=90)

    ax.plot(t_jrc,
            v_jrc,
            marker='.',
            c='r',
            markersize=2,
            linewidth=0,
            alpha=0.05)

    ax.plot(t, v1, marker='.', c='b', markersize=2, linewidth=0, alpha=0.05)

    ax.plot(t, v2, marker='.', c='k', markersize=3, linewidth=0, alpha=0.8)

    # for SG
    if len(t) != len(v2_filtered):
        print('Bad, shapes are not equal, skipping line plotting ...')
    else:
        ax.plot(t,
                v2_filtered,
                marker='.',
                c='k',
                markersize=0,
                linewidth=2,
                alpha=0.1)

    # for LOWESS
    # v2_filtered_t = [datetime.datetime.fromtimestamp(t / 1000) for t in v2_filtered[:, 0]]
    # ax.plot(v2_filtered_t, v2_filtered[:, 1], marker='.', c='k', markersize=0, linewidth=2, alpha=0.1)

    path = 'time_series_images/' + os.path.basename(f) + '.png'
    print(str(i) + ' ' + path)
    plt.tight_layout()
    plt.savefig(path, dpi=150)
    plt.close()