Esempio n. 1
0
def _sp(data):
    """
    Generate plots of convergence criteria, and energy vs. optimization cycles

    :job: ccdata object, or file
    :returns: TODO

    """
    # TODO scfenergies, scfvalues, scftargets vs. scf cycles
    print("\n\n")
    #print("Optimization Converged: ", data.optdone)
    criteria = [0, 0, 0]
    criteria[0] = [x[0] for x in data.scfvalues]
    criteria[1] = [x[1] for x in data.scfvalues]
    criteria[2] = [x[2] for x in data.scfvalues]
    idx = np.arange(len(criteria[0]))

    # Plot Geometry Optimization Criteria for Convergence over opt cycles
    plt.plot(idx, criteria[0], label='Criteria 1')
    plt.plot(idx, criteria[1], label='Criteria 2')
    plt.plot(idx, criteria[2], label='Criteria 3')

    # Plot target criteria for convergence
    plt.axhline(y=data.scftargets[0])
    plt.yscale('log')

    plt.title("SCF Convergence Analysis")
    plt.xlabel("SCF Cycle")
    plt.legend()

    print(idx, criteria, data.scftargets)

    plt.show()
def create_plots(iterations, data, M, step_size):
    NN_b, NN_a, E = steepest_decent_training(iterations, data, M, step_size)

    X = data[:,0]
    X_cont = np.arange(-10,10,0.1)
    
    t = data[:,1]
    Y = [NN_a(np.array([[x],[1]]))[0] for x in X_cont]
    f = np.vectorize(lambda(x): sin(x) / x)

    plt.figure(1)
    plt.plot(X_cont,Y,label='Neural network \n (M = %d)' % M)
    plt.plot(X_cont,f(X_cont),label='sinc(x)')
    plt.legend()
    plt.savefig('images/nn_vs_real_%d_%d.%s' % (iterations,M,img_format), format=img_format)

    plt.figure(2)
    plt.plot(X_cont,Y,label='Neural network \n (M = %d)' % M)
    plt.scatter(X,t,color='red',label='Training data')
    plt.legend()
    plt.savefig('images/nn_vs_training_%d_%d.%s' % (iterations,M,img_format), format=img_format)

    plt.figure(3)
    plt.plot(E, label='Error (M = %d)' % M)
    plt.yscale('log')
    plt.legend()
    plt.savefig('images/error_%d_%d.%s' % (iterations,M,img_format), format=img_format)
    
    plt.show()
Esempio n. 3
0
def plot_figure_commute(file_name):
    f = open(file_name)
    lines = f.readlines()
    lines[0] = lines[0][:-1]
    f.close()

    probabilities = lines[0].split(" ")
    xx = xrange(len(probabilities))
    probabilities = [float(x) for x in probabilities]

    plt.figure(figsize=(12, 10))
    plt.plot(xx, probabilities, label="D(degree > x)")
    plt.ylabel('Percentage of vertices which degree> x')
    plt.xlabel('Degree')
    plt.title("Cumulative function for degrees")
    plt.legend(loc='upper right')

    name = "results_graphs/"
    if file_name == "temp_degrees/cumulative_temp.txt":
        name += "ful_cumulative"
    else:
        name += "good_cumulative"

    plt.savefig(name + '.png')

    plt.figure(figsize=(12, 10))
    plt.plot(xx, probabilities, label="log(D(degree > x))")
    plt.legend(loc='upper right')
    plt.title("Cumulative function for degrees in log/log coords")
    plt.ylabel('Log of percentage of vertices which degree> x')
    plt.xlabel('Log of degrees')
    plt.xscale('log')
    plt.yscale('log')

    plt.savefig(name + '_log' + '.png')
Esempio n. 4
0
def setDiffsPlot(CF,d0,ySym = True):
    CFtext = [str(j)+',' for j in CF]
    bText = [CFtext[0],r'...,$a_i$+c,...',CFtext[-1]]
    CFtext = ''.join(CFtext)
    bText= ''.join(bText)
    CFtext = '[' + CFtext[:-1] + ']'
    bText = '[' + bText[:-1] + ']'
    print(CFtext)

    plt.ylabel(r'$d^{crit}_b - d^{crit}_a$',fontsize=20)
    plt.xlabel(r'Element changed',fontsize=20)
    xmin, xmax, ymin, ymax = plt.axis()
    if ySym:
        plt.yscale('symlog',linthreshy=1e-15)
        yLoc = [y*ymax for y in [.1, .01, .001]]
    else:
        yLoc = [y*(ymax-ymin)+ymin for y in [0.95, 0.85, 0.75]]
    plt.plot([0,xmax],[0,0],'k--',label='_')
    plt.text((xmax-xmin)*0.15,yLoc[0],r'$a = [a_i] =$'+CFtext,fontsize=15)
    plt.text((xmax-xmin)*0.15,yLoc[1],r'$b_i = $'+bText,fontsize=15)
    plt.text((xmax-xmin)*0.15,yLoc[2],r'$d_a^{crit} = $'+str(float(d0)),fontsize=15)
    plt.legend(loc='best')
    plt.xscale('symlog',linthreshx=1e-14)
    # plt.yscale('log')
    plt.show()
Esempio n. 5
0
def plot_max_avg_Rho_lev012(lev0,lev1,lev2, ic, spath):
    '''lev -- TimeProfQs() object, whose lev.convert() attribute has been called'''
    #avgRho
    Tratio = lev0.t / ic.tCr
    fig, ax = plt.subplots()
    #initial
    plt.hlines(ic.rho0, Tratio.min(),Tratio.max(), colors="magenta", linestyles='dashed',label="initial")
    #lev0
    plt.plot(Tratio,lev0.maxRho,ls="-",c="black",lw=2.,label="max Lev0")
    plt.plot(Tratio,lev0.minRho,ls="-",c="green",lw=2.,label="min Lev0")
    plt.plot(Tratio,lev0.avgRho,ls="-",c="blue",lw=2.,label="avg Lev0")
    plt.plot(Tratio,lev0.avgRho_HiPa,ls="-",c="red",lw=2.,label=r"avg ($\rho > \rho_0$)")
    #lev1
    plt.plot(Tratio,lev1.maxRho,ls="--",c="black",lw=2.,label="max Lev1")
    plt.plot(Tratio,lev1.minRho,ls="--",c="green",lw=2.,label="min Lev1")
    plt.plot(Tratio,lev1.avgRho,ls="--",c="blue",lw=2.,label="avg Lev1")
    plt.plot(Tratio,lev1.avgRho_HiPa,ls="--",c="red",lw=2.,label=r"avg Lev1 ($\rho > \rho_0$)")
    #lev2
    plt.plot(Tratio,lev2.maxRho,ls=":",c="black",lw=2.,label="max Lev2")
    plt.plot(Tratio,lev2.minRho,ls=":",c="green",lw=2.,label="min Lev2")
    plt.plot(Tratio,lev2.avgRho,ls=":",c="blue",lw=2.,label="avg Lev2")
    plt.plot(Tratio,lev0.avgRho_HiPa,ls=":",c="red",lw=2.,label=r"avg Lev1 ($\rho > \rho_0$)")
    #finish
    plt.yscale("log")
    plt.xlabel("t / t_cross")
    plt.ylabel("Densities [g/cm^3]")
    plt.title(r"Max & Avg Densities, Lev0")
    py.legend(loc=4, fontsize="small")
    name = spath+"max_avg_Rho_Lev012.pdf"
    plt.savefig(name,format="pdf")
    plt.close()
Esempio n. 6
0
def _scatter(actual, prediction, args):
    plt.figure()
    plt.plot(actual, prediction, 'b'+args['plot_scatter_marker'])
    xmin=min(actual)
    xmax=max(actual)
    ymin=min(prediction)
    ymax=max(prediction)
    diagxmin=min(math.fabs(x) for x in actual)
    diagymin=min(math.fabs(y) for y in prediction)
    diagpmin=min(diagxmin,diagymin)
    pmin=min(xmin,ymin)
    pmax=max(xmax,ymax)
    plt.plot([diagpmin,pmax],[diagpmin,pmax],'k-')
    if args['plot_identifier'] != 'NoName':
        plt.title(args['plot_identifier'])
    plt.xlabel('Observed')
    plt.ylabel('Modeled')
    if args['plot_performance_log'] == True:
        plt.yscale('log')
        plt.xscale('log')
    if args['plot_scatter_free'] != True:
        plt.axes().set_aspect('equal')
    if args['plot_dump'] == True:
        pfname=os.path.join(args['plot_dir'],args['plot_identifier']+'_eiger_scatter.pdf')
        plt.savefig(pfname,format="pdf")
    else:
        plt.show()
Esempio n. 7
0
 def plot_probability_calibration_curves(self):
 
     """ Compute true and predicted probabilities for a calibration plot 
         fraction_of_positives - The true probability in each bin (fraction of positives).
         mean_predicted_value - The mean predicted probability in each bin.
     """
     
     fig = plt.figure()
     ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
     ax2 = plt.subplot2grid((3, 1), (2, 0), rowspan=2)
     
     ax1.set_ylabel("Fraction of positives")
     ax1.set_ylim([-0.05, 1.05])
     ax1.legend(loc="lower right")
     ax1.set_title('Calibration plots  (reliability curve) ' + self.description)
 
     ax2.set_xlabel("Mean predicted value")
     ax2.set_ylabel("Count")
     ax2.legend(loc="upper center", ncol=2)
     
     clf_score = brier_score_loss(self.y_true, self.y_pred, pos_label=1)
     
     
     fraction_of_positives, mean_predicted_value = calibration_curve(self.y_true, self.y_pred, n_bins=50)
     
     ax1.plot(mean_predicted_value, fraction_of_positives, "s-", color="#660066",  alpha = 0.6, label="%s (%1.3f)" % (self.description, clf_score))
     ax2.hist(self.y_pred, range=(0, 1), bins=50, color="#660066", linewidth=2.0 , alpha = 0.6, label="%s (%1.3f)" % (self.description, clf_score), histtype="step", lw=2)
     plt.yscale('log')
     return
Esempio n. 8
0
def Config(**options):
    """Configures the plot.

    Pulls options out of the option dictionary and passes them to
    title, xlabel, ylabel, xscale, yscale, xticks, yticks, axis, legend,
    and loc.
    """
    title = options.get('title', '')
    pyplot.title(title)

    xlabel = options.get('xlabel', '')
    pyplot.xlabel(xlabel)

    ylabel = options.get('ylabel', '')
    pyplot.ylabel(ylabel)

    if 'xscale' in options:
        pyplot.xscale(options['xscale'])

    if 'xticks' in options:
        pyplot.xticks(options['xticks'])

    if 'yscale' in options:
        pyplot.yscale(options['yscale'])

    if 'yticks' in options:
        pyplot.yticks(options['yticks'])

    if 'axis' in options:
        pyplot.axis(options['axis'])

    loc = options.get('loc', 0)
    legend = options.get('legend', True)
    if legend:
        pyplot.legend(loc=loc)
def main():

    sample='q'
    sm_bin='10.0_10.5'
    catalogue = 'sm_9.5_s0.2_sfr_c-0.75_250'

    #load in fiducial mock
    filepath = './'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'_cov.npy'
    cov = np.matrix(np.load(filepath+filename))
    diag = np.diagonal(cov)
    filepath = cu.get_output_path() + 'analysis/central_quenching/observables/'
    filename = 'sm_9.5_s0.2_sfr_c-0.8_Chinchilla_250_wp_fiducial_'+sample+'_'+sm_bin+'.dat'
    data = ascii.read(filepath+filename)
    rbins = np.array(data['r'])
    mu = np.array(data['wp'])
    
    #load in comparison mock
    
    
    
    
    plt.figure()
    plt.errorbar(rbins, mu, yerr=np.sqrt(np.diagonal(cov)), color='black')
    plt.plot(rbins, wp,  color='red')
    plt.xscale('log')
    plt.yscale('log')
    plt.show()
    
    inv_cov = cov.I
    Y = np.matrix((wp-mu))
    
    X = Y*inv_cov*Y.T
    
    print(X)
Esempio n. 10
0
def pareto_graph(database, objectives=None):
	'''Constructs a visualization of the movement of the best front over generations

	The argument 'objectives' must be a list indicating the indices of the fitness values to be used.
	The first two will be consumed. If the list has less than two elements, or if is not given, the
	graph will be produced using the first two fitness values.
	'''
	if objectives is None or len(objectives) < 2:
		objectives = [0, 1]

	generations = []
	if database.properties['highest_population'] < FRONT_COUNT:
		generations = list(range(1, database.properties['highest_population'] + 1))
	else:
		step = database.properties['highest_population'] / FRONT_COUNT
		generations = [round(i * step) for i in range(1, FRONT_COUNT + 1)]

	for i, gen in enumerate(generations, start=1):
		database.select(gen)
		individual_data = [val for key, val in database.load_report().items()
			if key.startswith('I') and val['rank'] == 1]
		x_values = [val['fitness'][objectives[0]] for val in individual_data]
		y_values = [val['fitness'][objectives[1]] for val in individual_data]
		plt.plot(x_values, y_values,
			color=str((FRONT_COUNT - i) / FRONT_COUNT),
			linestyle='None',
			marker='o',
			markeredgecolor='white')

	plt.title('Movement of best front')
	plt.xscale('log')
	plt.yscale('log')
	plt.xlabel(database.properties['objective_names'][objectives[0]])
	plt.ylabel(database.properties['objective_names'][objectives[1]])
	plt.show()
def bar_graph_dict(dict_to_plot, plot_title="", xlab="", ylab="", log_scale=False, col="#71cce6", sort_key_list=None,
                   min_count=1):
    """
    Plots a bar graph of the provided dictionary.
    Params:
    dict_to_plot (dict): should have the format {'label': count}
    plot_title (str), xlab (str), ylab (str), log_scale (bool): fairly self-explanatory plot customization
    col (str): colour for the bars
    sort_key_list (list): the keys of the dictionary are assumed to match based its first item and reordered as such
    min_count (int): do not plot items with less than this in the count
    """
    # Sort dictionary & convert to list using custom keys if needed
    if not sort_key_list:
        list_to_plot = sorted(dict_to_plot.items())
    else:
        list_to_plot = sorted(dict_to_plot.items(), key=lambda x: sort_key_list.index(x[0]))

    # Remove list items with less than min_count
    if min_count != 1:
        list_to_plot = [dd for dd in list_to_plot if dd[1] >= min_count]

    # Bar plot of secondary structure regions containing mutants in each mutant Cas9
    bar_width = 0.45
    plt.bar(np.arange(len(list_to_plot)), [dd[1] for dd in list_to_plot], width=bar_width, align='center', color=col)
    plt.xticks(range(len(list_to_plot)), [dd[0] for dd in list_to_plot], rotation=45, ha='right')
    plt.title(plot_title)
    plt.xlabel(xlab)
    plt.ylabel(ylab)
    if log_scale:
        plt.yscale('log')
        plt.ylim(min_count-0.1)  # Show values with just the minimum count
    plt.show()
Esempio n. 12
0
    def _init_ipython_plot(self,
                           plot_data,
                           legend_lst=[],
                           title_str=' ',
                           axis=None,
                           plot_relative=False):
        """ Private member function that loads ipython plotting libraries 

        Parameters
        ----------
        plot_data : lst of lst
            A list of lists that stores the data series to be plotted.
        legend : lst
            A list of strings for the legend.
        axis : bool
           Indicates whether there is a user specified axis.
        plot_relative : bool
           Indicates whether the relative residuals should be plotted.
        """
        import matplotlib
        import numpy as np
        import matplotlib.pyplot as plt       
        for data_set in plot_data:
            if plot_relative == True:
                max_term = max(data_set)
                for i,term in enumerate(data_set):
                    data_set[i] = data_set[i] / max_term
            plt.plot(data_set)
        plt.yscale("log")
        plt.legend(legend_lst)
        plt.title(title_str)
        if axis is not None:
            plt.xlim(axis[0],axis[1])
        plt.show() 
Esempio n. 13
0
def plottamelo3():
    a = np.arange(0,30)
    nw = NWsc(matr1,True)
    db = DBsc(matr1,True)
    dbPR = productDBsc(matr1,True)
    INsc = INsc1(matr1,True)
    plt.yscale('log')
    plt.plot(range(0,nw.iter),nw.res, color='blue', lw=2, label="Newton Sclaled")
    #plt.plot(range(0,db.iter),db.res, color='red', lw=2, label="DB Sclaled")
    #plt.plot(range(0,dbPR.iter),dbPR.res, color='green', lw=2, label="DB Product Sclaled")
    #plt.plot(range(0,INsc.iter),INsc.res, color='orange', lw=2, label="IN Sclaled")
    nw = newton(matr1,True)
    db = DB(matr1,True)
    dbPR = productDB(matr1,True)
    IN = INiteration(matr1,True)
    CR = CRiteration(matr1,True)
    plt.yscale('log')
    print len(range(0,nw.iter))
    print len(nw.res)
    plt.plot(range(0,nw.iter+1),nw.res, color='blue', lw=2, label="Newton")
    #plt.plot(range(0,db.iter+1),db.res, color='red', lw=2, label="DB")
    #plt.plot(range(0,dbPR.iter+1),dbPR.res, color='green', lw=2, label="DB Product")
    #plt.plot(range(0,IN.iter+1),IN.res, color='orange', lw=2, label="IN")
    #plt.plot(range(0,CR.iter+1),CR.res, color='brown', lw=2, label="CR")

    plt.legend(loc='upper right')
    plt.show()
Esempio n. 14
0
 def check_hod(self, z, prop):
     data = np.genfromtxt(LOCATION + "/data/" + prop + "z" + str(z))
     if prop == "ncen":
         if PLOT:
             plt.clf()
             plt.plot(self.hod.hmf.M,
                      self.hod.n_cen,
                      label="mine")
             plt.plot(data[:, 0] * self.hod.cosmo.h, data[:, 1], label="charles")
             plt.legend()
             plt.xscale('log')
             plt.yscale('log')
             plt.savefig(join(pref, "ncen" + prop + "z" + str(z) + ".pdf"))
         assert max_diff_rel(self.hod.n_cen, data[:, 1], 0.01)
     elif prop == "nsat":
         if PLOT:
             plt.clf()
             plt.plot(self.hod.hmf.M,
                      self.hod.n_sat,
                      label="mine")
             plt.plot(data[:, 0] * self.hod.cosmo.h, data[:, 1], label="charles")
             plt.legend()
             plt.xscale('log')
             plt.yscale('log')
             plt.savefig(join(pref, "nsat" + prop + "z" + str(z) + ".pdf"))
         assert max_diff_rel(self.hod.n_sat, data[:, 1], 0.01)
Esempio n. 15
0
 def plot_samples(self):
     """ Plot the samples requested for each arm """
     plt.clf()
     plt.scatter(range(0, self.K), self.sample_size)
     plt.yscale('log')
     plt.ioff()
     plt.show()
Esempio n. 16
0
def plot(objects, xscales={}, yscales={}, title=""):
    from matplotlib.pyplot import plot, show, close, subplot,\
        xscale, yscale, gcf
    '''
    Plots current state of objects in subplots.
    Define xscales and yscales as dict of indexes.
    '''
    if not isinstance(objects, list):
            objects = [objects]

    l = len(objects)
    first = round(l / 2.0) + 1
    second = round(l / 2.0)
    for i in range(0, l):
        subplot(first, second, i + 1)
        if i in xscales:
            xscale(xscales[i])
        if i in yscales:
            yscale(yscales[i])
        fig = gcf()
        fig.suptitle(title, fontsize="x-large")

        values = objects[i].get_y_values()
        x, y = values.shape

        for j in range(x):
            plot(objects[i].get_t_values(), values[j, :])

    show()
    close()
Esempio n. 17
0
def saskia_plot_pulseheight( data_file_name, station_number=501, detector=0, number_bins=200, range_start=0., range_end=4500 ):
    
    # If the plot exist we skip the plotting
    if os.path.isfile('./img/pulseheigt_histogram_%d_detector_%d.pdf' % (station_number, detector)):
        # Say if the plot is present
        print "Plot already present for station %d" % station_number

    # If there is no plot we make it
    else:
   
        # Now transform the ROOT histogram to a python figure
        rootpy.plotting.root2matplotlib.hist(ph_histo)
    
        # Setting the limits on the axis
        plt.ylim((pow(10,-1),pow(10,7)))
        plt.xlim((range_start, range_end))
        plt.yscale('log')
    
        # Setting the plot labels and title
        plt.xlabel("Pulseheight [ADC]")
        plt.ylabel("Counts")
        plt.title("Pulseheight histogram (log scale) for station (%d)" %station_number)
    
        # Saving them Pica
        plt.savefig(
            './img/pulseheigt_histogram_%d_detector_%d.pdf' % (station_number, detector) ,    # Name of the file
            bbox_inches='tight')                        # Use less whitespace
Esempio n. 18
0
def plot_roc_one_vs_rest(labels, predictions_dict, weights=None, physics_notion=False, predictions_dict_comparison=None, separate_particles=False, algorithms_name=('MVA', 'baseline')):
    """
    Plot roc curves one versus rest.
    
    :param array labels: labels form 0 to 5
    :param dict(array) predictions_dict: dict of label/predictions
    :param array weights: sample weights
    """
    if separate_particles:
        plt.figure(figsize=(22, 22))
    else:
        plt.figure(figsize=(10, 8))
    for label, name in labels_names_correspondence.items():
        if separate_particles:
            plt.subplot(3, 2, label + 1)
        for preds, prefix in zip([predictions_dict, predictions_dict_comparison], algorithms_name):
            if preds is None:
                continue
            fpr, tpr, _ = roc_curve(labels == label, preds[label], sample_weight=weights)
            auc = roc_auc_score(labels == label, preds[label], sample_weight=weights)
            if physics_notion:
                plt.plot(tpr * 100, fpr * 100, label='{}, {}, AUC={:1.5f}'.format(prefix, name, auc), linewidth=2)
                plt.yscale('log', nonposy='clip')
            else:
                plt.plot(tpr, 1-fpr, label='{}, AUC={:1.5f}'.format(name, auc), linewidth=2)
        if physics_notion:
            plt.xlabel('Efficiency', fontsize=22)
            plt.ylabel('Overall MisID Efficiency', fontsize=22)
        else:
            plt.xlabel('Signal efficiency', fontsize=22)
            plt.ylabel('Background rejection', fontsize=22)
        plt.legend(loc='best', fontsize=18)
def PlotEDepSummary(gFiles,nFiles,figureName='EDepSummary.png',tParse=GetThickness,
  histKey='eDepHist'):
  """ PlotEDepSummary
  Plotss the energy deposition summary
  """
  # Extrating the average values
  gT = list()
  gDep = list()
  gDepError = list()
  nT = list()
  nDep = list()
  nDepError = list()
  for fname in gFiles:
    f = TFile(fname,'r')
    hist = f.Get(histKey)
    gT.append(GetThickness(fname))
    gDep.append(hist.GetMean())
    gDepError.append(hist.GetMeanError())
  for fname in nFiles:
    f = TFile(fname,'r')
    hist = f.Get(histKey)
    nT.append(GetThickness(fname))
    nDep.append(hist.GetMean())
    nDepError.append(hist.GetMeanError())
  # Plotting
  plt.errorbar(gT,gDep,yerr=gDepError,fmt='r+')
  plt.hold(True)
  plt.errorbar(nT,nDep,yerr=nDepError,fmt='go')
  plt.xlabel("Thickness (mm)")
  plt.ylabel("Average Energy Deposition (MeV)")
  plt.legend(["Co-60","Cf-252"])
  plt.xscale("log")
  plt.yscale("log")
  plt.grid(True)
  plt.savefig(figureName)
Esempio n. 20
0
def Validation():
  numSamples = 1000000
  
  theta = np.random.rand(numSamples)*np.pi
  ECo60 = np.array([1.117,1.332])
  Ef0,Ee0 = Compton(ECo60[0],theta)
  Ef1,Ee1 = Compton(ECo60[1],theta)
  dSdE0 = diffXSElectrons(ECo60[0],theta)
  dSdE1 = diffXSElectrons(ECo60[1],theta)

  # Sampling Values
  values = list()
  piMax = np.max([dSdE0,dSdE1])
  while (len(values) < numSamples):
    values.append(SampleRejection(piMax,ComptonScattering))
  # Binning the data
  bins = np.logspace(-3,0.2,100)
  counts = np.histogram(values,bins)
  counts = counts[0]/float(len(values))
  binCenters = 0.5*(bins[1:]+bins[:-1])
  
  # Plotting
  plt.figure()
  plt.plot(binCenters,counts,ls='steps')
  #plt.bar(binCenters,counts,align='center')
  plt.grid(True)
  plt.xlim((1E-3,1.4))
  plt.xlabel('Electron Energy (MeV)')
  plt.ylabel('Frequency per Photon')
  plt.yscale('log')
  plt.xscale('log')
  plt.savefig('ValComptonScatteringXS.png')
Esempio n. 21
0
    def plot(self, debug = False):
	"""plot figures for population, nuisance parameters"""
	# first figure out what scheme is used
	self.list_scheme()

	# next get MABR sampling done
	self.MBAR_analysis()

	# load in precomputed P and dP from MBAR analysis
        pops0, pops1   = self.P_dP[:,0], self.P_dP[:,self.K-1]
        dpops0, dpops1 = self.P_dP[:,self.K], self.P_dP[:,2*self.K-1]
	t0 = self.traj[0]
    	t1 = self.traj[self.K-1]

        # Figure Plot SETTINGS
        label_fontsize = 12
        legend_fontsize = 10
        fontfamily={'family':'sans-serif','sans-serif':['Arial']}
        plt.rc('font', **fontfamily)

        # determine number of row and column
        if (len(self.scheme)+1)%2 != 0:
            c,r = 2, (len(self.scheme)+2)/2
    	else:
            c,r = 2, (len(self.scheme)+1)/2
    	plt.figure( figsize=(4*c,5*r) )
    	# Make a subplot in the upper left
    	plt.subplot(r,c,1)
    	plt.errorbar( pops0, pops1, xerr=dpops0, yerr=dpops1, fmt='k.')
    	plt.hold(True)
    	plt.plot([1e-6, 1], [1e-6, 1], color='k', linestyle='-', linewidth=2)
    	plt.xlim(1e-6, 1.)
    	plt.ylim(1e-6, 1.)
    	plt.xlabel('$p_i$ (exp)', fontsize=label_fontsize)
    	plt.ylabel('$p_i$ (sim+exp)', fontsize=label_fontsize)
    	plt.xscale('log')
    	plt.yscale('log')
    	# label key states
    	plt.hold(True)
    	for i in range(len(pops1)):
        	if (i==0) or (pops1[i] > 0.05):
            		plt.text( pops0[i], pops1[i], str(i), color='g' )
    	for k in range(len(self.scheme)):
        	plt.subplot(r,c,k+2)
        	plt.step(t0['allowed_'+self.scheme[k]], t0['sampled_'+self.scheme[k]], 'b-')
        	plt.hold(True)
        	plt.xlim(0,5)
        	plt.step(t1['allowed_'+self.scheme[k]], t1['sampled_'+self.scheme[k]], 'r-')
        	plt.legend(['exp', 'sim+exp'], fontsize=legend_fontsize)
        	if self.scheme[k].find('cs') == -1:
            		plt.xlabel("$\%s$"%self.scheme[k], fontsize=label_fontsize)
            		plt.ylabel("$P(\%s)$"%self.scheme[k], fontsize=label_fontsize)
            		plt.yticks([])
        	else:
            		plt.xlabel("$\sigma_{%s}$"%self.scheme[k][6:],fontsize=label_fontsize)
            		plt.ylabel("$P(\sigma_{%s})$"%self.scheme[k][6:],fontsize=label_fontsize)
            		plt.yticks([])

    	plt.tight_layout()
    	plt.savefig(self.picfile)
Esempio n. 22
0
def avgDegree(G):
  print "Nodes: ",
  print G.number_of_nodes()
  print "Edges: ",
  print G.number_of_edges()

  # avg degree
  degrees = defaultdict(int)
  total = 0
  for node in G.nodes():
    neighbors = G.neighbors(node)
    degrees[len(neighbors)] += 1
    total += len(neighbors)

  max_degree = max(degrees.keys())
  degrees_arr = (max_degree+1) * [0]
  for index, count in degrees.iteritems():
    degrees_arr[index] = count

  plt.plot(range(max_degree+1), degrees_arr, '.')
  plt.xscale('log', basex=2)
  plt.xlabel('degree')
  plt.yscale('log', basex=2)
  plt.ylabel('# of people')
  plt.savefig('degree_distribution.png')
  plt.close()
def plot_gradient_over_time(points, get_grad_over_time):
    fig = plt.figure(figsize=(6.5, 4))
    # Remove the plot frame lines. They are unnecessary chartjunk.    
    ax = plt.subplot(111)    
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    # ax.xaxis.set_major_locator(plt.MultipleLocator(1.0))
    # ax.xaxis.set_minor_locator(plt.MultipleLocator(0.1))
    # ax.yaxis.set_major_locator(plt.MultipleLocator(1.0))
    # ax.yaxis.set_minor_locator(plt.MultipleLocator(0.1))
    # ax.grid(which='major', axis='x', linewidth=0.75, linestyle='-', color='0.75')
    # ax.grid(which='minor', axis='x', linewidth=0.25, linestyle='-', color='0.75')
    # ax.grid(which='major', axis='y', linewidth=0.75, linestyle='-', color='0.75')
    # ax.grid(which='minor', axis='y', linewidth=0.25, linestyle='-', color='0.75')
    ax.grid(b=True, which='major', linewidth=0.75, linestyle=':', color='0.75')
    ax.yaxis.set_ticks_position('none')
    ax.xaxis.set_ticks_position('none')
    for wx, wRec, c in points:
        grad_over_time = get_grad_over_time(wx, wRec)
        x = np.arange(1, grad_over_time.shape[1]+1, 1)
        plt.plot(x, np.sum(grad_over_time, axis=0), c+'.-', label='({0}, {1})'.format(wx, wRec), linewidth=1, markersize=8)
    plt.xlim(1, grad_over_time.shape[1])
    plt.xticks(x)
    plt.gca().invert_xaxis()
    plt.yscale('symlog')
    plt.yticks([10**8, 10**6, 10**4, 10**2, 0, -10**2, -10**4, -10**6, -10**8])
    plt.xlabel('time k')
    plt.ylabel('gradient ')
    plt.title('Unstability of gradient in backward propagation.\n(backpropagate from left to right)')
    leg = plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), frameon=False, numpoints=1)
    leg_font = FontProperties()
    leg_font.set_size('x-large')
    leg.set_title('$(w_x, w_{rec})$', prop=leg_font)
Esempio n. 24
0
def plot_citation_graph(citation_graph, filename, plot_title):
    # find the indegree_distribution
    indeg_dist = in_degree_distribution(citation_graph)    
    # sort freq by keys
    number_citations = sorted(indeg_dist.keys())
    indeg_freq = [indeg_dist[n] for n in number_citations]

    # normalize
    total = sum(indeg_freq)
    indeg_freq_norm = [freq / float(total) for freq in indeg_freq]
    
    # calculate log/log, except for the first one (0)
    #log_number_citations = [math.log10(x) for x in number_citations[1:]]
    #log_indeg_freq_norm = [math.log10(x) for x in indeg_freq_norm[1:]]
    
    plot(number_citations[1:], indeg_freq_norm[1:], 'o')
    
    xscale("log")
    yscale("log")
    
    xlabel("log10 #citations")
    ylabel("log10 Norm.Freq.")
    title(plot_title)
    grid(True)
    savefig(filename)
    show()
Esempio n. 25
0
def plotCurves(losses,rateOfExceedance,return_periods,lossLevels):

    plt.figure(1, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
    plt.scatter(losses,rateOfExceedance,s=20)
    if len(return_periods) > 0:
        annual_rate_exc = 1.0/np.array(return_periods)
        for rate in annual_rate_exc:
            if rate > min(rateOfExceedance):
                plt.plot([min(losses),max(losses)],[rate,rate],color='red') 
                plt.annotate('%.6f' % rate,xy=(max(losses),rate),fontsize = 12)

    plt.yscale('log')
    plt.xscale('log')
    plt.ylim([min(rateOfExceedance),1])
    plt.xlim([min(losses),max(losses)])
    plt.xlabel('Losses', fontsize = 16)
    plt.ylabel('Annual rate of exceedance', fontsize = 16)

    setReturnPeriods = 1/rateOfExceedance
    plt.figure(2, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
    plt.scatter(setReturnPeriods,losses,s=20)
    if len(return_periods) > 0:
        for period in return_periods:
            if period < max(setReturnPeriods):
                plt.plot([period,period],[min(losses),max(losses)],color='red') 
                plt.annotate(str(period),xy=(period,max(losses)),fontsize = 12)

    plt.xscale('log')
    plt.xlim([min(setReturnPeriods),max(setReturnPeriods)])
    plt.ylim([min(losses),max(losses)])
    plt.xlabel('Return period (years)', fontsize = 16)
    plt.ylabel('Losses', fontsize = 16)
Esempio n. 26
0
    def __save(self, n, plot, sfile):
        p.figure(figsize=sfile)

        p.xlabel(plot.xlabel)
        p.ylabel(plot.ylabel)
        p.xscale(plot.xscale)
        p.yscale(plot.yscale)
        p.grid()
        for curvetype, args, kwargs in plot.curves:
            if curvetype == "plot":
                p.plot(*args, **kwargs)
            elif curvetype == "imshow":
                p.imshow(*args, **kwargs)
            elif curvetype == "hist":
                p.hist(*args, **kwargs)
            elif curvetype == "bar":
                p.bar(*args, **kwargs)

        p.axes().set_aspect(plot.aspect)
        if plot.legend:
            p.legend(shadow=0, loc=plot.loc)

        if not os.path.isdir(plot.dir):
            os.mkdir(plot.dir)
        if plot.pgf:
            p.savefig(plot.dir + plot.name + ".pgf")
            print(plot.name + ".pgf")
        if plot.pdf:
            p.savefig(plot.dir + plot.name + ".pdf", bbox_inches="tight")
            print(plot.name + ".pdf")

        p.close()
Esempio n. 27
0
def plot():
    plt.plot(x_large, y_large, label=u'grafo de tamaño grande')
    plt.plot(x_medium, y_medium, label=u'grafo de tamaño medio')
    plt.yscale('log')
    plt.legend()
    plt.xlabel(u'iteración')
    plt.ylabel(u'diferencia en la norma del vector resultado entre sucesivas iteraciones')
Esempio n. 28
0
def create_time_plot(data, times, ylabel, title=None, filename=None,
                     xlims=None, ylims=None, log=False):
    plt.close('all')
    fig, ax = plt.subplots()
    if type(data) == dict:
        for lab in data.keys():
            plt.plot(times[lab], data[lab], 'x', label=lab)
        # plt.legend(loc='upper left')
        plt.legend()
    else:
        plt.plot(times, data, 'x')

    plt.grid()
    plt.xlabel("Received Time")
    plt.ylabel(ylabel)
    if title is not None:
        plt.title(title)
    fig.autofmt_xdate()
    if xlims is not None:
        plt.xlim(xlims)
    if ylims is not None:
        plt.ylim(ylims)
    if log:
        plt.yscale('log')
    if filename is not None:
        plt.tight_layout()
        plt.savefig(filename, fmt='pdf')
    else:
        plt.show()
def make_plot():
    # Set up figure
    fig = plot.figure(figsize = (700 / my_dpi, 600 / my_dpi), dpi = my_dpi)

    ### Plot ###

    for i, mode in enumerate(default_modes):
        alpha = 0.4
        if mode == 1:
            alpha = 1.0
        if mode == 3 or mode == 5:
            alpha = 0.7
        plot.plot(frame_range, modes_over_time[i, :], linewidth = linewidth, alpha = alpha, label = "%d" % mode)

    # Axis
    plot.xlim(0, frame_range[-1])
    plot.ylim(10**(-3.5), 10**(0.0))
    plot.yscale("log")

    # Annotate
    this_title = readTitle()
    plot.xlabel("Number of Planet Orbits", fontsize = fontsize)
    plot.ylabel("Vortensity Mode Amplitudes", fontsize = fontsize)
    plot.title("%s" % (this_title), fontsize = fontsize + 1)

    plot.legend(loc = "upper right", bbox_to_anchor = (1.2, 1.0)) # outside of plot

    # Save and Close
    plot.savefig("fft_vortensity_modes.png", bbox_inches = 'tight', dpi = my_dpi)
    plot.show()
    plot.close(fig) # Close Figure (to avoid too many figures)
Esempio n. 30
0
def make_plot(filename, title, arguments, methods, scale):
    if not support_plots:
        return

    is_linear = (scale == 'linear')

    plot_size = LINEAR_PLOT_SIZE if is_linear else OTHER_PLOT_SIZE
    plt.figure(figsize=plot_size)

    for name, func, measures in methods:
        plt.plot(arguments, measures, 'o-', label=name, markersize=3)

    if is_linear:
        axis = plt.axis()
        plt.axis((0, axis[1], 0, axis[3]))

    plt.xscale(scale)
    plt.yscale(scale)

    plt.xticks(fontsize=NORMAL_FONT_SIZE)
    plt.yticks(fontsize=NORMAL_FONT_SIZE)

    plt.grid(True)

    plt.title(title, fontsize=LABEL_FONT_SIZE)
    plt.xlabel('Argument', fontsize=NORMAL_FONT_SIZE)
    plt.ylabel('Time (seconds)', fontsize=NORMAL_FONT_SIZE)
    plt.legend(loc='upper left', fontsize=NORMAL_FONT_SIZE)

    plt.tight_layout(0.2)

    path = os.path.join(PLOTS_DIR, filename)
    plt.savefig(path)
    print '[*] Saved plot "%s"' % path
Esempio n. 31
0
def get_radial_profile(img, xoyo, nbin, disp=0):
    ''' Computes the mean radial profile of the image.
    
        img:
            2D image.
        xoyo:
            (xo,yo) center for the annuli.
        nbin:
            width of the annuli in pixels
        disp:
            optional key word for displaying the images.
            Its value will serve as the window number that will be created.
    '''

    (xo, yo) = xoyo
    (nx, ny) = img.shape
    r = get_r_dist(nx, ny, xo, yo)

    r_max = np.max(r)  # radius of the image
    r_max = np.max(r[xo, :])

    npts = int(r_max / nbin)
    O = np.ones((nx, ny))
    Z = np.zeros((nx, ny))
    Profile = np.zeros(npts)

    if disp != 0:
        plt.figure(disp)
        plt.clf()
        plt.subplot(121)
        plt.plot(xo, yo, 'xw')
        plt.title('PSF')
        plt.title('Averaged radial profile')
        plt.xlabel('Distance from center (pixels)')
        plt.imshow(img, interpolation='none')
        plt.colorbar()

        val_min = np.min(img)
        val_max = np.max(img)

        for k in range(0, npts - 1, 1):
            M = np.where(r > nbin * k, O, Z) * np.where(
                r < nbin * (k + 1), O, Z)
            Profile[k] = np.sum(img * M) / np.sum(M)

            plt.figure(disp)
            plt.subplot(121)
            plt.imshow(img, interpolation='none')
            plt.pause(.005)
            plt.imshow(img * M,
                       interpolation='none',
                       vmin=val_min,
                       vmax=val_max)
            plt.pause(.005)
            plt.subplot(122)
            plt.plot(Profile, 'rx')
            plt.yscale('log')

        plt.plot(Profile, 'r')
        plt.subplot(121)
        plt.imshow(img, interpolation='none', vmin=val_min, vmax=val_max)

    for k in range(0, npts - 1, 1):
        M = np.where(r > nbin * k, O, Z) * np.where(r < nbin * (k + 1), O, Z)
        Profile[k] = np.sum(img * M) / np.sum(M)

    return Profile
# Convert to numpy
np_wilayah = np.array(list_wilayah)
np_jokowi= np.array(jokowi)
np_prabowo= np.array(prabowo)

# plot data
fig,ax = plt.subplots(figsize=(10,5))
# fig,ax = plt.subplots()
# print(ax)
pos = list(range(len(np_jokowi)))
width = 0.25

# print(ind-width/2)

ax.bar(pos,np_jokowi,width,color='green',label='Jokowi')
ax.bar([p + width for p in pos],np_prabowo,width,color='red',label='Prabowo')
# ax.set_xticks(ind)
ax.set_xticks([p + 0.5 * width for p in pos])
ax.set_xticklabels(np_wilayah)
# # Naming label
plt.xlabel('provinsi')
plt.ylabel('perolehan suara')

# # styling x,y value
plt.yticks(np.arange(np_jokowi.min(),np_jokowi.max(),4000000))
plt.xticks(rotation='vertical',ha='right')
plt.legend(loc='upper right')
plt.yscale('linear')

plt.show()
        h = scipy.optimize.curve_fit(fitfunc_gnfs,
                                     np.array([i[0] for i in timestamps]),
                                     np.array([i[1] for i in timestamps]),
                                     p0=p0_gnfs)
        plt.plot(range_gnfs,
                 fitfunc_gnfs(range_gnfs, *h[0]),
                 color='green',
                 linestyle='--')
    elif enable_fits:
        print("Too few points for fitting NFS")
except TypeError:
    print("Error encountered when doing the curve fit")

plt.grid()
plt.yscale(
    "log",
    subsy=3600 *
    np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]))
plt.yticks(YTICK_POSITION, YTICK_LABELS)
plt.xlabel("log10(n)")

if not ZOOM:
    plt.xticks(XTICK_POSITION, XTICK_LABELS)
    plt.xlim(80, None)
    plt.ylim(60, None)
else:
    plt.xlim(*ZOOM_X_LIMITS)
    plt.ylim(*ZOOM_Y_LIMITS)

if SAVE_FIGURE:
    plt.savefig(FILENAME, dpi=DPI)
Esempio n. 34
0
]
#'darkolivegreen','darkmagenta','aquamarine','coral','burlywood',
#'beige','darkorange','crimson','darkcyan','bisque'
Ncolor = len(colo)
colo = colo * 10
styl = ['-'] * Ncolor + ['--'] * Ncolor + [':'] * Ncolor + ['-.'] * Ncolor * 7
widt = [2] * Ncolor * 10

#================== temperature-pressure structure ====================
fig, ax = plt.subplots()
plt.plot(Tg, press / bar, lw=4)
plt.xlabel(r'$T\ \mathrm{[K]}$', fontsize=20)
plt.ylabel(r'$p\ \mathrm{[bar]}$', fontsize=20)
plt.xlim(Tmin, Tmax)
plt.ylim(pmin, pmax)
if (pmax > pmin * 5): plt.yscale('log')
plt.tick_params(axis='both', labelsize=15)
plt.tick_params('both', length=6, width=1.5, which='major')
plt.tick_params('both', length=3, width=1, which='minor')
minorLocator = MultipleLocator(sep)
ax.xaxis.set_minor_locator(minorLocator)
cAl2O3 = [
    14.1611, -2.8238E+04, -7.3843E-01, -3.7413E-07, 2.2086E-11, 2421, 3253
]
cSiO2 = [
    -378.5210, 6.5473E+03, 1.3150E+02, -3.5774E-02, 3.4220E-06, 1883, 2503
]
cH2O = [29.8605, -3.1522E+03, -7.3037E+00, 2.4247E-09, 1.8090E-06, 273, 647]
cFe = [11.5549, -1.9538E+04, -6.2549E-01, -2.7182E-09, 1.9086E-13, 1808, 3008]
cNH3 = [37.1575, -2.0277E+03, -1.1601E+01, 7.4625E-03, -9.5811E-12, 195, 406]
cCO2 = [35.0187, -1.5119E+03, -1.1335E+01, 9.3383E-03, 7.7626E-10, 216, 305]
def process_input(para):
    #dir_prefix = os.getcwd()
    #os.chdir(dir_prefix + '/Box Sync/python_garage')
    t_start_reading = os.times()[-1]
    plt.close("all")
    print('='*50)
    plt.ioff()
    
    # ==============================================================
    # ============= parameter initialization =======================
    # ==============================================================
    
    Q1, Q2, bound, beta, J21, J12 = 100000.0, 100000.0, 2e-4, 5e-3, 0.0001, 0.001   # default values
    gain, omega2 = 1e3, 1.1    # default values
    Q1, Q2= 122680.0, 86505.0
    omega2 = 1.0539 * 3  # here is to the 1:3 model
    bound  = 1e-4    # this is 'optimized' value, given Q1, Q2, and omega2
    J21    = 7.6e-4  # this is 'optimized' value, given Q1, Q2, and omega2, bound
    
    save_file = 0
    save_file_FFT = 0
    doRTSA = 0
    doPlot = 1
    #para_list = np.logspace(-4,-2,50)
    #para_list = [0.0001]

    reference = int(max(Q1, Q2))
    time = np.linspace(0, 5*reference, 50*reference+1)
    #time = np.linspace(0, 1000, 10000+1);  # for debugging purpose

    
    # ============ coupled oscillator case ============ 
    

#    print('='*50)
    print('Starting with parameter = {}'.format(para)); 
    # ==============================================================
    # ============= parameter setting! =============================
    # ==============================================================
    J12 = para
    J21 = para
    temp_folder = 'PythonParallelTemp_1_to_3_Js'
    if not os.path.exists(temp_folder):
            os.makedirs(temp_folder)
    temp_file = os.path.join(os.getcwd(),temp_folder) + '/ss_Js_' + str(para) + '.png' 
    # ==============================================================
    # ============================================================== 
    x1Int, v1Int, x2Int, v2Int = 0.1, 0, 0, 0
    t_start_ss = os.times()[-1]
    Assol = sp.integrate.odeint(solvr_nonlinear_osc_2modes, [x1Int, v1Int, x2Int, v2Int], time,
                                args = (Q1, Q2, omega2, beta, gain, bound, J21, J12)); #   solve the equations!
    AsFinal = Assol[-1]
    
    if doPlot:
      plt.figure(1)
      plt.subplot(211)
      plt.plot(time[::100], Assol[::100,0], 'b-')
      plt.xlabel('time'); plt.ylabel('displacement'); plt.title('mode 1')
      plt.subplot(212); 
      plt.plot(time[::100], Assol[::100,2], 'g-')
      plt.xlabel('time'); plt.ylabel('displacement'); plt.title('mode 2')
      #plt.show()
      plt.savefig(temp_file)
      plt.close(1)
    
    
    freq1, fft_out1 = myFFT(time[-int(0.1*len(time)):], Assol[-int(0.4*len(time)):,0]); 
    # only last 10% of the time-domain data, which assuming to have reached steady-state
    
    freq2, fft_out2 = myFFT(time[-int(0.1*len(time)):], Assol[-int(0.4*len(time)):,2]); 
    # only last 10% of the time-domain data, which assuming to have reached steady-state
        
    if doPlot:    
      plt.figure(11)
      #plt.plot(2*np.pi*freq1, abs(fft_out1),'b-')
      plt.plot(2*np.pi*freq1, fft_out1.real,'bo-', 2*np.pi*freq2, fft_out2.real,'go-')
      plt.xlim([1.0, 1.1])
      plt.grid(b=True, which='both', color='0.65',linestyle='--')
      plt.title('Steady-state')
      plt.xlabel('Frequency (Hz)'); 
      plt.ylabel('FFT amplitude (a.u.)');    plt.yscale('log')
      #plt.show()
      plt.savefig(os.path.join(os.getcwd(),temp_folder) + '/ss_FFT_Js_' + str(para) + '.png')
      
      #print; print 'The FFT resolution for steady-state is {} Hz'.format(2*np.pi*mode(np.diff(freq2))[0][0])
      #print; print 'The maximum in FFT for steady-state happens at {} Hz'.format(2*np.pi*freq2[fft_out2.argmax()]); 
            
    if save_file == 1:
        np.savetxt(os.path.join(os.getcwd(),temp_folder) + '/ss_Js_' + str(para) + '.txt',
                   np.hstack([time[:,np.newaxis], Assol]))
    #if save_file_FFT == 1:
    #    if not os.path.exists(temp_folder):
    #        os.makedirs(temp_folder)
    #    idx_low, idx_high = np.argmax(2*np.pi*freq1 > 0.5), np.argmin(2*np.pi*freq1 < 1.7)
    #    freq1, fft_out1, fft_out2 = freq1[idx_low:idx_high], fft_out1[idx_low:idx_high], fft_out2[idx_low:idx_high]
    #    to_save = np.vstack([2*np.pi*freq1, np.log(fft_out1.real), np.log(fft_out2.real), para*np.ones(freq1.shape[0])])
    #    to_save = np.transpose(to_save)
    #    with open(os.path.join(os.getcwd(),temp_folder) + '/' + str(para) + '.txt','w') as FFT_file:
    #        np.savetxt(FFT_file, to_save, fmt='%10.6f')
    #    print; print 'Saved data with parameter = {}'.format(para)
    #    #return to_save
    
    print; print('elasped time for calculating single steady-state is {} seconds'.format(os.times()[-1] - t_start_ss)); 
    

    
    
    # ===================================================================
    # ================= Below I will start the ringdown =================
    # ===================================================================
    t_start_ss = os.times()[-1]
    #time = np.linspace(0,5001,10*5001)
    Ringdown = sp.integrate.odeint(solvr_nonlinear_osc_2modes, AsFinal, time,
                                args = (Q1, Q2, omega2, beta, gain, 0.0, J21, J12)); #   solve the ringdown where bound is set to zero
    
    if doPlot:
      plt.figure(3)
      plt.subplot(211)
      plt.plot(time[::100], Ringdown[::100,0], 'b-', time[::100], max(Ringdown[:100,0])*np.exp(-time[::100]/2/Q1),'r--')
      plt.xlabel('time'); plt.ylabel('displacement'); plt.title('Ringdown, mode 1')
      #plt.yscale('log')
      plt.subplot(212); 
      plt.plot(time[::100], Ringdown[::100,2], 'g-',time[::100], max(Ringdown[:100,2])*np.exp(-time[::100]/2/Q2),'r--')
      plt.xlabel('time'); plt.ylabel('displacement'); plt.title('Ringdown, mode 2')
      #plt.yscale('log')
      #plt.show()
      
      temp_file_2 = os.path.join(os.getcwd(),temp_folder) + '/rd_Js_' + str(para) + '.png' 
      plt.savefig(temp_file_2)
      plt.close(3)

    
    
    #idx_temp = np.argmax(time>5000)
    ##idx_temp = time.shape[0]
    #freq1, fft_out1 = myFFT(time[:idx_temp], Ringdown[:idx_temp,0]); 
    ## only the first 5000 seconds
    #
    #freq2, fft_out2 = myFFT(time[:idx_temp], Ringdown[:idx_temp,2])
    ## only the first 5000 seconds
    #
    #plt.figure(31)
    ##plt.plot(2*np.pi*freq1, abs(fft_out1),'b-')
    #plt.plot(2*np.pi*freq1, fft_out1.real,'bo-', 2*np.pi*freq2, fft_out2.real,'go-')
    #plt.xlim([0.8, 2])
    #plt.grid(b=True, which='both', color='0.65',linestyle='--')
    #plt.title('Ringdown...')
    #plt.xlabel('Frequency (Hz)'); 
    #plt.ylabel('FFT amplitude (a.u.)');plt.yscale('log')
    #
    #plt.show()
    #print; print 'The FFT resolution for ringdown is {} Hz'.format(2*np.pi*mode(np.diff(freq2))[0][0])
    #
    #pass
        
    #plt.figure(2)
    #plt.plot(time, Assol[:,2])
    #plt.xlabel('time'); plt.ylabel('displacement'); plt.title('mode 2'); 
    #plt.show()
    
    if save_file == 1:
        np.savetxt(os.path.join(os.getcwd(),temp_folder) + '/rd_Js_' + str(para) + '.txt'
                   ,np.hstack([time[:,np.newaxis], Ringdown]))
    #print; print 'elasped time for total is {} seconds'.format(os.times()[-1] - t_start_reading); 
    
    if doRTSA:
      RTSA(time, Ringdown[:,0], 100)
      prettify_figure('Frequency', 'time', 'real-time spectrum, ringdown, mode 1')
      plt.ylim([1.0, 1.1])
      plt.yscale('linear')
      plt.savefig(os.path.join(os.getcwd(),temp_folder) + '/RTSA_mode1_Js_' + str(para) + '.png', dpi = 400)
      
      RTSA(time, Ringdown[:,2], 200)
      prettify_figure('Frequency', 'time', 'real-time spectrum, ringdown, mode 2')
      plt.ylim([1.0, 1.1])  
      plt.yscale('linear')  
      plt.savefig(os.path.join(os.getcwd(),temp_folder) + '/RTSA_mode2_Js_' + str(para) + '.png', dpi = 400)
    print('elasped time for calculating single ringdown is {} seconds'.format(os.times()[-1] - t_start_ss));
Esempio n. 36
0
    def both_hazard(self, true, fitted, coefs):
        """
        Plots the hazard function
        :param true: pickle                 True hazard file (*.pickle)
        :param fitted: pickle               Fitted hazard file (*.pickle)
        :param coefs: pickle                Coefficients of fitted hazard
        :return: figure object
        """
        hazard_fit = fitted["hazard_fit"]
        hazard_s = fitted["s"]
        hazard_T = fitted["T"]
        im, s, apoe = true

        fig, ax = plt.subplots(figsize=(6, 4), dpi=100)
        if self.period:
            for i in range(len(self.period)):
                # ID corresponding to the period
                idx = int(self.period[i] * 10)
                # Labels
                labelAdd = f"PSHA, T={self.period[i]:.2f}s" if self.period[
                    i] > 0.0 else "PSHA, PGA"
                labelAdd_2nd = r"Sa(%.2fs), $2^{nd}$ order fit" % self.period[i] if self.period[i] > 0.0 \
                    else r"PGA, $2^{nd}$ order fit"
                labelAdd_1st = r"Sa(%.2fs), $1^{st}$ order fit" % self.period[i] if self.period[i] > 0.0 \
                    else r"PGA, $1^{st}$ order fit"

                # Tag for the hazard to select
                if self.period[i] > 0.0:
                    try:
                        hazardTag = f"SA({self.period[i]:.2f})"
                        apoeFit = np.array(hazard_fit[hazardTag])
                        coef = coefs[hazardTag]
                    except:
                        hazardTag = f"SA({self.period[i]:.1f})"
                        apoeFit = np.array(hazard_fit[hazardTag])
                        coef = coefs[hazardTag]
                else:
                    hazardTag = "PGA"
                    apoeFit = np.array(hazard_fit[hazardTag])
                    coef = coefs[hazardTag]

                # 1st order fits
                h1, hx, saT1x = self.linearFit(coef, hazard_s)

                # Plotting
                plt.scatter(s[idx],
                            apoe[idx],
                            color=self.color_grid[i],
                            label=labelAdd,
                            marker=self.markers[i])
                plt.loglog(hazard_s,
                           apoeFit,
                           color=self.color_grid[i],
                           label=labelAdd_2nd)
                plt.loglog(hazard_s,
                           h1,
                           color=self.color_grid[i],
                           label=labelAdd_1st,
                           ls="--",
                           lw=0.8)
                if i == 0:
                    labelScatter = "Fitting points"
                else:
                    labelScatter = None
                plt.scatter(saT1x,
                            hx,
                            marker='x',
                            c='k',
                            zorder=10,
                            s=40,
                            label=labelScatter)

        if self.rp:
            for i in self.rp:
                plt.plot([0.01, 10.1], [1 / i, 1 / i],
                         ls='--',
                         color=self.gray)
                self.add_text(ax,
                              0.011,
                              1.5 / i,
                              f'Return period: {i} years',
                              color=self.gray,
                              ha='left')

        plt.ylabel('Annual probability of\n exceedance, ' + r'$H$',
                   fontsize=12)
        plt.xlabel(r'Intensity, $s$ [g]', fontsize=12)
        plt.ylim(10e-6, 1)
        plt.xlim(0.01, 10.1)
        plt.xticks([])
        plt.xticks(np.array([0.01, 0.1, 1.0, 10]))
        plt.rc('xtick', labelsize=14)
        plt.rc('ytick', labelsize=14)
        plt.legend(frameon=False,
                   loc='upper right',
                   fontsize=12,
                   bbox_to_anchor=(1.55, 1))

        plt.xscale("log")
        plt.yscale("log")
        plt.grid(True, which="major", ls="--", lw=0.4, dashes=(5, 10))

        return fig
    sig_ = -sigma_data[:, 1]
    Vg_true.append(V_)

avg_tflux = numpy.array(avg_tflux)

fig = plt.figure(figsize=(4, 4))
plt.style.use("science")

for i, conc in enumerate(concentrations):
    plt.plot(Vg_true[i],
             avg_tflux[i, :],
             "-o",
             markersize=5,
             label="{} mol/L$^3$".format(conc))

plt.yscale("log")
plt.xlabel("True $V_{g}$ (V)")
plt.xlim(0, 1.25)
plt.ylabel("Average total flux (mol/(m$^{-2}$*s))")
plt.legend()

plt.savefig(os.path.join(plot_path, "rect_Vg_no_correction.svg"))

# d_debye = Debye_length(numpy.array(concentrations)) / 1e-9
# rect = 1 - avg_tflux[:, 7] / avg_tflux[:, 0]

# plt.figure(figsize=(4, 4))
# plt.plot(d_debye, rect, "-s", markersize=5)
# plt.xlabel("Debye Length (nm)")
# plt.ylabel("Rectification ratio")
# plt.savefig(os.path.join(plot_path, "rect_debye_.pdf"))
Esempio n. 38
0
	w=[]
	x=[]
	with open(directory[0]+"/res/"+directory[1]) as f:
		for j in range(30):
			v=[]
			for i in range(30):
				v.append(float(f.readline().split()[3]))
			f.readline()
			w.append(v)
	arr=np.array(w)
	plt.plot(range(1000,31000,1000),
					 np.mean(arr,axis=1),
					 linewidth=2,color=trans[directory[0]][directory[1][-2:]])
	labels.append(directory[0]+" d="+directory[1][-2:]+"%")
	
plt.yscale('log')
plt.xticks(range(0,31000,10000))
plt.xlabel('N')
plt.ylabel('time(s)')
plt.grid(False)
plt.subplots_adjust(bottom=0.15,right=0.65)
#plt.legend(labels,loc=4)
plt.legend(labels,loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()

labels=[]
for directory in product(lista,files):
	w=[]
	x=[]
	with open(directory[0]+"/res/"+directory[1]) as f:
		for j in range(30):
Esempio n. 39
0
#error_avg[r - loop_start + 1] = np.append(error_avg[r - loop_start + 1], sim_env.error / testing)
# error_avg[r - loop_start + 2] = [sim_env.error/testing] * episodes
# error_optimum_avg_all = np.sum(sim_env.loss) / testing_comps
# print(error_avg[r - loop_start +2])

for i in range(loop_end - loop_start + 1):
    plt.figure(i)
    #plt.plot(error_optimum[0], '-b.',error_optimum[1], '-r.', error_optimum[2], '-g.', markersize=1)
    plt.plot(error, 'b.', markersize=1.5)
    plt.xlabel('Cycles')
    plt.ylabel('eps_RL')
    plt.title('Absolute Error probability'.format(loop_start + i))
    #plt.legend(['K=2', 'K=3', 'K=4'])
    plt.axis([0, episodes * testing, 0, 10**2])
    #ax1.set_yscale('symlog', nonposy='clip', linthreshy=0.01)
    plt.yscale('symlog', nonposy='clip', linthreshy=10**-8)
    plt.grid()

plt.figure(loop_end - loop_start + 2)
#plt.plot(error_optimum[0], '-b.',error_optimum[1], '-r.', error_optimum[2], '-g.', markersize=1)
plt.plot(loss_overall, '-b.', markersize=1.5)
plt.xlabel('Episodes')
plt.ylabel('Loss')
plt.title('Average Loss for 250 training cycles')
#plt.legend(['K=2', 'K=3', 'K=4'])
#plt.axis([0, episodes*testing, 0, 10**2])
#ax1.set_yscale('symlog', nonposy='clip', linthreshy=0.01)
#plt.yscale('symlog', nonposy='clip', linthreshy=10**-8)
plt.grid()

plt.figure(loop_end - loop_start + 4)
Esempio n. 40
0
def fit_airy_2D(img, disp=0):
    ''' Fits a 2D Airy pattern on the image.
        
        Returns the best fit parameters of the Airy pattern.
        
        See twoD_Airy(xy, amplitude, xo, yo, sigma_x, sigma_y, theta, offset)
    '''

    nx = img.shape[1]
    ny = img.shape[0]
    x = np.linspace(0, nx - 1, nx)
    y = np.linspace(0, ny - 1, ny)
    x, y = np.meshgrid(x, y)
    xy = (x, y)

    init_xmax = np.unravel_index(img.argmax(), img.shape)[1]
    init_ymax = np.unravel_index(img.argmax(), img.shape)[0]
    initial_guess = (img.max(), init_xmax, init_ymax, .4)
    #initial_guess = (img[init_xmax, init_ymax]  , init_xmax, init_ymax, .6)
    #plt.figure(27)
    #plt.imshow(twoD_Airy(xy, img[init_xmax, init_ymax]  , init_xmax, \
    #        init_ymax, .6).reshape(nx,ny))

    popt, pcov = opt.curve_fit(twoD_Airy, xy, img.ravel(), p0=initial_guess)

    if disp != 0:
        data_fitted = twoD_Airy(xy, *popt)
        #offset=np.min(img)
        plt.figure(disp)
        plt.clf()
        plt.subplot(1, 3, 1)
        plt.imshow(data_fitted.reshape(nx,ny), interpolation='none', \
                cmap=cm.Greys_r)
        plt.colorbar()
        plt.subplot(1, 3, 2)
        plt.plot(img[popt[1], :])
        plt.plot(data_fitted.reshape(nx, ny)[popt[1], :], 'r--')
        plt.yscale('log')
        plt.subplot(1, 3, 3)
        plt.plot(img[:, popt[2]])
        plt.plot(data_fitted.reshape(nx, ny)[:, popt[2]], 'r--')
        plt.yscale('log')

        plt.figure(disp + 1)
        plt.clf()
        plt.subplot(121)
        plt.imshow(data_fitted.reshape(nx,ny), interpolation='none', \
                cmap=cm.Greys_r)
        plt.colorbar()
        P_fit=get_radial_profile(data_fitted.reshape(nx,ny), (popt[1], \
                popt[2]), 1, disp=10)
        P_mes = get_radial_profile(img, (popt[1], popt[2]), 1, disp=0)
        plt.subplot(122)
        plt.plot(P_mes, 'b')
        plt.plot(P_fit, 'r--')
        plt.yscale('log')

        print('\n--- Airy disk fit results ---')
        print('Amplitude =' + str(popt[0]))
        print('Position of the maximum: \nxo=' + str(popt[1]) + ' nyo=' +
              str(popt[2]))
        print('F factor=' + str(popt[3]))
        print('-----------------------------')

    return popt
Esempio n. 41
0
def modelling(datadir):
    """ modelling """
    edi_file = os.path.join(datadir, r"synth02.edi")
    save_path = os.path.join(datadir, 'TM')

    allfiles = glob.glob(save_path + '\\*.*')
    for i in allfiles:
        os.remove(i)
    try:
        os.remove(save_path + '\\Model1D')
        os.remove(save_path + '\\OccamStartup1D')
    except:
        pass

    d1 = occam1d.Data()
    d1.write_data_file(edi_file=edi_file,
                       mode='TE',
                       save_path=save_path,
                       res_err='data',
                       phase_err='data',
                       res_errorfloor=4.,
                       phase_errorfloor=2.,
                       remove_outofquadrant=True)

    m1 = occam1d.Model(target_depth=40000,
                       n_layers=100,
                       bottom_layer=100000,
                       z1_layer=10)
    m1.write_model_file(save_path=d1.save_path)

    #--> make a startup file
    s1 = occam1d.Startup(data_fn=d1.data_fn,
                         model_fn=m1.model_fn,
                         max_iter=200,
                         target_rms=1.0)

    s1.write_startup_file()

    #--> run occam1d from python
    occam_path = r"C:\Work\Programming\pygmi\pygmi\bin\occam1d.exe"
    occam1d.Run(s1.startup_fn, occam_path, mode='TM')

    #--plot the L2 curve
    #    l2 = occam1d.PlotL2(d1.save_path, m1.model_fn,
    #                        fig_dpi=100,
    #                        fig_size=(2,2)
    #                        )

    #--> see that iteration 7 is the optimum model to plot

    iterfn = os.path.join(save_path, 'TM_005.iter')
    respfn = os.path.join(save_path, 'TM_005.resp')

    p1 = occam1d.Plot1DResponse(
        data_te_fn=d1.data_fn,
        model_fn=m1.model_fn,
        iter_te_fn=iterfn,
        resp_te_fn=respfn,
        #                                depth_limits=(0,20),
        fig_dpi=100,
        fig_size=(2, 2))

    oc1m = occam1d.Model(model_fn=m1.model_fn)

    allfiles = glob.glob(save_path + '\*.iter')

    roughness = []
    rms = []
    for iterfn in allfiles:
        oc1m.read_iter_file(iterfn)
        roughness.append(float(oc1m.itdict['Roughness Value']))
        rms.append(float(oc1m.itdict['Misfit Value']))

    roughness.pop(0)
    rms.pop(0)

    plt.plot(roughness, rms)
    plt.xlabel('Roughness')
    plt.ylabel('RMS')
    plt.show()
    plt.plot(rms)
    plt.xlabel('Iteration')
    plt.ylabel('RMS')
    plt.show()

    iterfn = os.path.join(save_path, 'TM_005.iter')
    respfn = os.path.join(save_path, 'TM_005.resp')

    oc1m.read_iter_file(iterfn)

    oc1d = occam1d.Data(data_fn=d1.data_fn)
    oc1d.read_resp_file(respfn)

    depths = []
    res = []

    for i, val in enumerate(oc1m.model_res[:, 1]):
        if i == 0:
            continue
        if i > 1:
            depths.append(-oc1m.model_depth[i - 1])
            res.append(val)

        depths.append(-oc1m.model_depth[i])
        res.append(val)

    plt.plot(res, depths)

    plt.xlabel('Res')
    plt.ylabel('Depth')

    plt.show()

    plt.plot(1 / oc1d.freq, oc1d.data['resxy'][0], 'bs')
    plt.plot(1 / oc1d.freq, oc1d.data['resxy'][2], 'r')
    plt.xscale('log')
    plt.yscale('log')
    plt.grid(True)
    plt.show()

    plt.plot(1 / oc1d.freq, oc1d.data['phasexy'][0], 'bs')
    plt.plot(1 / oc1d.freq, oc1d.data['phasexy'][2], 'r')
    plt.xscale('log')
    plt.yscale('log')
    plt.grid(True)
    plt.show()

    breakpoint()
Esempio n. 42
0
 norm_labels = {'L2': '$L_2$', 'Linf': '$L_\infty$'}
 for field_name in field_names:
     for norm in norms:
         differences = [
             case.get_difference(exact, field_name, mask=mask, norm=norm)
             for case in simulations
         ]
         ax.plot(grid_spacings,
                 differences,
                 label='{} - {}-norm'.format(field_name, norm_labels[norm]),
                 marker='o',
                 zorder=10)
 ax.legend()
 ax.set_xlim(0.1 * min(grid_spacings), 10.0 * max(grid_spacings))
 pyplot.xscale('log')
 pyplot.yscale('log')
 ax.axis('equal')
 # save and display
 if save_name:
     print('[info] saving figure ...')
     if not os.path.isdir(save_directory):
         os.makedirs(save_directory)
     time_step = simulations[0].fields[field_names[0]].time_step
     pyplot.savefig(os.path.join(
         save_directory, '{}{:0>7}.{}'.format(save_name, time_step, fmt)),
                    bbox_inches='tight',
                    dpi=dpi,
                    format=fmt)
 if show:
     print('[info] displaying figure ...')
     pyplot.show()
Esempio n. 43
0
def draw_dynamic_compare():
    '''
    draw the comparison of DLP and ICDE'14
    '''
    g_names = []
    dlp_time = []
    dlp_mem = []
    icde14_time = []
    icde14_mem = []
    # fill the data
    # load data
    for data in datasets:
        g_names.append(data_names[data])
        dlp_data = parse_dynamic_local_pushfile(DYNAMIC_LOCAL_PUSH_DIR + data + ".txt")
        icde_file = ICDE14_DIR + data + ".txt"
        icde14_data = {"time": 0, "mem": 0}
        if os.path.exists(icde_file):
            icde14_data = parse_icde14_file(icde_file)
        print(dlp_data, icde14_data)
        dlp_time.append(dlp_data["time"])
        dlp_mem.append(dlp_data["mem"])
        icde14_time.append(icde14_data["time"])
        icde14_mem.append(icde14_data["mem"])

    # draw the cpu figures
    size_of_fig = (FIG_SIZE_MULTIPLE)
    fig, ax = plt.subplots()
    N = len(g_names)
    width = 0.35  # the width of the bars
    ind = 1.2 * np.arange(N)  # the x locations for the groups
    dlp_cpu_bar = ax.bar(ind, dlp_time, width, hatch="\\", label="DLP", fill=False)
    icde14_cpu_bar = ax.bar(ind + width, icde14_time, width, hatch="-", label="Inc-SR", fill=False)
    plt.yscale('log')  # log scale of y
    # plt.ylim(ymin=-2)  # this line
    ax.set_xticks(ind + width / 2)
    ax.set_xticklabels(g_names, fontsize=LABEL_SIZE)
    ax.set_ylabel("Avg Update Time(s)", fontsize=LABEL_SIZE)
    plt.xticks(fontsize=TICK_SIZE)
    plt.yticks(fontsize=TICK_SIZE)

    fig.set_size_inches(*size_of_fig)  # set ratio
    plt.legend(prop={'size': LEGEND_SIZE, "weight": "bold"}, loc=2)
    fig.savefig("./figures/" + 'dynamic_cpu_compare.pdf', bbox_inches='tight', dpi=300)

    # draw the memory figure
    fig, ax = plt.subplots()
    N = len(g_names)
    width = 0.35  # the width of the bars
    ind = 1.2 * np.arange(N)  # the x locations for the groups
    dlp_mem_bar = ax.bar(ind, np.array(dlp_mem), width, \
                         hatch="\\", label="DLP", fill=False)
    icde14_mem_bar = ax.bar(ind + width, np.array(icde14_mem), width, \
                            hatch="-", label="Inc-SR", fill=False)
    plt.yscale('log')  # log scale of y
    # plt.ylim(ymin=-2)  # this line
    ax.set_xticks(ind + width / 2)
    ax.set_xticklabels(g_names, fontsize=LABEL_SIZE)
    ax.set_ylabel("Memory Usage(MB)", fontsize=LABEL_SIZE)
    plt.xticks(fontsize=TICK_SIZE)
    plt.yticks(fontsize=TICK_SIZE)

    fig.set_size_inches(*size_of_fig)  # set ratio
    plt.legend(prop={'size': LEGEND_SIZE, "weight": "bold"}, loc=2)
    fig.savefig("./figures/" + 'dynamic_mem_compare.pdf', bbox_inches='tight', dpi=300)
    return
Esempio n. 44
0
	p.figure(1, (6,6))
	p.axes([0.2,0.18,0.75,0.75])
	p.plot(xf,phi_h(10**xf,z_mean), c='cyan' , ls='dashed', lw=2, label='Ai15')#Aird 2-10 keV LADE')
	#p.fill_between(xf, y1=(nall)*(1-(nallN)**(-0.5)), y2=(nall)*(1+(nallN)**(-0.5)), color='r', alpha=0.7, label='Soft mock')# 0.5-2 keV')
	if len(Lx_c[ok_hasinger])>0:
		p.fill_between(Lx_c[ok_hasinger], y1=phi[ok_hasinger]*(1-(Nobj[ok_hasinger])**(-0.5)), y2=phi[ok_hasinger]*(1+(Nobj[ok_hasinger])**(-0.5)), color='G', alpha=0.7, label='Ha05', lw=2)
	if z_mean[0]>1 and z_mean[0]<1.5:
		p.fill_between(log_LX_hard, y1=10**log_dndv_low, y2=10**log_dndv_up, color='g', alpha=0.7, label='Bu15', lw=2)

	log_LX_hard, log_dndv_up,  log_dndv_low
	#nall, nallN, vol = get_nzs(LX_soft, redshift_R, fbins, zmin, zmax, simulation_area)
	#p.fill_between(xf,  y1=(nall)*(1-(nallN)**(-0.5)), y2=(nall)*(1+(nallN)**(-0.5)), color='g', alpha=0.7, label='Mock', lw=2)# 2-10  keV')

	nall, nallN, vol = get_nzs(data_RXS.data['RXS_LOGLX'][data_RXS.observed], data_RXS.data['Z_BEST'][data_RXS.observed], fbins, zmin, zmax, area_eboss_dr16)
	p.fill_between(xf,  y1=(nall)*(1-(nallN)**(-0.5)), y2=(nall)*(1+(nallN)**(-0.5)), color='r', alpha=0.7, label='2RXS', lw=2)# 2-10  keV')

	nall, nallN, vol = get_nzs(data_XMM.data['XMMSL2_LOGLX_FULL'][data_XMM.observed], data_XMM.data['Z_BEST'][data_XMM.observed], fbins, zmin, zmax, area_eboss_dr16)
	p.fill_between(xf,  y1=(nall)*(1-(nallN)**(-0.5)), y2=(nall)*(1+(nallN)**(-0.5)), color='b', alpha=0.7, label='XMMSL2', lw=2)# 2-10  keV')

	p.xlabel(r'$\log_{10}(L_X/[erg/s])$')
	p.ylabel(r'$\Phi$=dN/dlogL/dV [1/Mpc$^3$/dex]')
	#if zmin<0.1:
	p.legend(loc=3, fontsize=14)
	p.yscale('log')
	p.xlim((42., 47.5))
	p.ylim((1/(2*vol),1e-3))
	p.title(str(np.round(zmin,2))+"<z<"+str(np.round(zmax,2)))
	p.grid()
	p.savefig(os.path.join(figure_dir, "XLF_soft_"+str(np.round(zmin,2))+"_"+str(np.round(zmax,2))+".png"))
	p.clf()
Esempio n. 45
0
    def plot_current_field(self,
                           xlim=None,
                           flim=None,
                           log=False,
                           wrap_order=0):
        beam = self
        # XXX Not sure why I have to copy, I suspect the fft
        e = np.copy(beam.e[int(self.Nt / 2), :, :])
        I = beam.intensity_from_field(e)
        If = abs(fftshift(beam.fft(e)))**2
        fx, fy = beam.get_f()
        fx = fftshift(fx)
        fy = fftshift(fy)
        phase = np.angle(e)

        # Images
        X = beam.X
        Y = beam.Y
        ext = [-X / 2, X / 2, -Y / 2, Y / 2]
        extf = [fx[0], fx[-1], fy[0], fy[-1]]
        plt.figure(figsize=(16, 4), dpi=150)
        plt.subplot(131)
        plt.imshow(beam.prep_data(I),
                   aspect='auto',
                   extent=ext,
                   cmap='viridis')
        cb = plt.colorbar()
        cb.set_label(r'Intensity ($10^{14}$ W/cm^2)')
        plt.xlabel(r'$x$ (um)')
        plt.ylabel(r'$y$ (um)')
        if xlim != None:
            plt.xlim(xlim)
            plt.ylim(xlim)

        if wrap_order == 0:
            axis0 = 0
            axis1 = 1
        elif wrap_order == 1:
            axis0 = 1
            axis1 = 0
        plt.subplot(132)
        plt.imshow(np.unwrap(np.unwrap(beam.prep_data(phase), axis=axis0),
                             axis=axis1),
                   aspect='auto',
                   extent=ext,
                   cmap='viridis')
        cb = plt.colorbar()
        cb.set_label(r'Phase (rad)')
        plt.xlabel(r'$x$ (um)')
        plt.ylabel(r'$y$ (um)')
        if xlim != None:
            plt.xlim(xlim)
            plt.ylim(xlim)

        plt.subplot(133)
        plt.imshow(beam.prep_data(If),
                   aspect='auto',
                   extent=extf,
                   cmap='viridis')
        cb = plt.colorbar()
        cb.set_label(r'Intensity (arb unit)')
        plt.xlabel(r'$f_x$ (um$^{-1}$)')
        plt.ylabel(r'$f_y$ (um$^{-1}$)')
        if flim != None:
            plt.xlim(flim)
            plt.ylim(flim)

        plt.tight_layout()
        plt.show()
        # Lineouts
        # We've already taken the transpose so y is the first index
        indy = int(beam.Ny / 2)
        indx = int(beam.Nx / 2)
        x = beam.x
        y = beam.y
        plt.figure(figsize=(16, 4), dpi=150)
        plt.subplot(131)
        plt.plot(x, I[:, indy], label='y')
        plt.plot(y, I[indx, :], 'm--', label='x')
        plt.legend()
        plt.xlabel(r'$x$ (um)')
        plt.ylabel(r'Intensity ($10^{14}$ W/cm^2)')
        if xlim != None:
            plt.xlim(xlim)

        plt.subplot(132)
        plt.plot(x, np.unwrap(phase[:, indy]), label='x')
        plt.plot(y, np.unwrap(phase[indx, :]), 'm--', label='y')
        plt.legend()
        plt.xlabel(r'$x$ (um)')
        plt.ylabel(r'Phase (rad)')
        if xlim != None:
            plt.xlim(xlim)

        plt.subplot(133)
        plt.plot(fx, If[:, indy], label='x')
        plt.plot(fy, If[indx, :], 'm--', label='y')
        plt.legend()
        plt.xlabel(r'$f_x$ (um$^{-1}$)')
        plt.ylabel(r'Intensity (arb unit)')
        if flim != None:
            plt.xlim(flim)

        plt.tight_layout()
        plt.show()

        if log == True:
            # Lineouts
            plt.figure(figsize=(16, 4), dpi=150)
            plt.subplot(131)
            plt.plot(x, I[:, indy], label='x')
            plt.plot(y, I[indx, :], 'm--', label='y')
            plt.legend()
            plt.xlabel(r'$x$ (um)')
            plt.ylabel(r'Intensity ($10^{14}$ W/cm^2)')
            plt.yscale('log')
            if xlim != None:
                plt.xlim(xlim)

            plt.subplot(132)
            plt.plot(x, np.unwrap(phase[:, indy]), label='x')
            plt.plot(y, np.unwrap(phase[indx, :]), 'm--', label='y')
            plt.legend()
            plt.xlabel(r'$x$ (um)')
            plt.ylabel(r'Phase (rad)')
            plt.yscale('log')
            if xlim != None:
                plt.xlim(xlim)

            plt.subplot(133)
            plt.plot(fx, If[:, indy], label='x')
            plt.plot(fy, If[indx, :], 'm--', label='y')
            plt.legend()
            plt.xlabel(r'$f_x$ (um$^{-1}$)')
            plt.ylabel(r'Intensity (arb unit)')
            plt.yscale('log')
            if flim != None:
                plt.xlim(flim)

            plt.tight_layout()
            plt.show()
Esempio n. 46
0
    output_num_units=10,
    output_nonlinearity=lasagne.nonlinearities.softmax,
    update=nesterov_momentum,
    update_learning_rate=0.01,
    update_momentum=0.9,
    regression=False,
    max_epochs=10,
    verbose=1,
)

X, y = load()  # load 2-d data
net2.fit(X, y)

# Training for 1000 epochs will take a while.  We'll pickle the
# trained model so that we can load it back later:
import cPickle as pickle
with open('net2.pickle', 'wb') as f:
    pickle.dump(net2, f, -1)

train_loss = np.array([i["train_loss"] for i in net2.train_history_])
valid_loss = np.array([i["valid_loss"] for i in net2.train_history_])
pyplot.plot(train_loss, linewidth=3, label="train")
pyplot.plot(valid_loss, linewidth=3, label="valid")
pyplot.grid()
pyplot.legend()
pyplot.xlabel("epoch")
pyplot.ylabel("loss")
pyplot.ylim(0, 1)
pyplot.yscale("log")
pyplot.show()
Esempio n. 47
0
def gfl_cvx_2():
    '''   Graph Fused Lasso   '''
    numNode = 100
    data = np.zeros((numNode, 2))
    for k in range(11):
        data[k] = data[k] + np.array([1, 1])
        data[k + 11] = data[k + 11] + np.array([-1, 1])
        data[k + 22] = data[k + 22] + np.array([2, 2])
        data[k + 33] = data[k + 33] + np.array([-1, -1])

    np.random.seed(1)
    noise = 0.5 * np.random.normal(0, 1, (numNode, 2))
    data = data + noise

    graph = nx.grid_2d_graph(10, 10)
    graph = nx.convert_node_labels_to_integers(graph)

    print('graph:', list(graph.edges()))

    temp_edges = []
    for k in range(0, numNode):
        for j in graph.neighbors(k):
            if j == k + 1:
                temp_edges.append([k, j])

    graph0_edges = []
    graph0_edges.append(temp_edges[0])
    for edge in temp_edges:
        if (edge[0] != graph0_edges[-1][1]) and (edge != graph0_edges[-1]):
            graph0_edges.append(edge)

    graph1_edges = [
        edge for edge in list(graph.edges()) if edge not in graph0_edges
    ]

    graph1_edges.sort()

    graph0_nodes = np.unique(np.array(graph0_edges))
    graph1_nodes = np.unique(np.array(graph1_edges))

    graph0 = nx.Graph()
    graph1 = nx.Graph()
    graph0.add_nodes_from(graph0_nodes)
    graph1.add_nodes_from(graph1_nodes)
    graph0.add_edges_from(graph0_edges)
    graph1.add_edges_from(graph1_edges)

    print("graph0:", graph0_edges, "graph1:", graph1_edges)

    # edgelist = [[k, k + 1] for k in range(numNode - 1)]

    # graph = nx.Graph()
    # graph.add_edges_from(edgelist)
    # coloredge(graph)
    # graph0, graph1 = decompose_graph(graph)

    runtime = []
    objerror = []
    # rho_set = [2**k for k in range(7)]
    rho_set = [1]

    x_hat1, obj1, _, _ = solve_GroupFL(y=data,
                                       G=graph,
                                       G0=graph0,
                                       G1=graph1,
                                       maxsteps=20,
                                       rho=1,
                                       lam=1)
    optobj = obj1[-1]
    print(obj1[:20])

    for rho in rho_set:
        clock_time = time.time()
        x_hat2, tevolution, obj, _ = solve_GroupFL_pool(y=data,
                                                        G=graph,
                                                        G0=graph0,
                                                        G1=graph1,
                                                        maxsteps=20,
                                                        rho=rho,
                                                        lam=1)
        print('GraphLasso_pool:', time.time() - clock_time, ',rho:', rho)
        objerror.append([i - optobj for i in obj])
        runtime.append(tevolution)
        print(obj[:20])
        print((x_hat1 - x_hat2)[:10])
        print([obj1[i] - obj[i] for i in range(len(obj))])

    # plotting
    plt.switch_backend('TkAgg')
    plt.figure()
    plt.plot(runtime[0], objerror[0], label=r'$\rho=2^0$')
    # plt.plot(runtime[1], objerror[1], label=r'$\rho=2^1$')
    # plt.plot(runtime[2], objerror[2], label=r'$\rho=2^2$')
    # plt.plot(runtime[3], objerror[3], label=r'$\rho=2^3$')
    # plt.plot(runtime[4], objerror[4], label=r'$\rho=2^4$')
    # plt.plot(runtime[5], objerror[5], label=r'$\rho=2^5$')
    # plt.plot(runtime[6], objerror[6], label=r'$\rho=2^6$')
    plt.legend()
    plt.xlim((0, 50))
    plt.ylim((1e-5, 1e2))
    plt.title('Graph Fused Lasso Algorithm (cvxpy)')
    plt.xlabel('Running time (seconds)', fontsize=12)
    plt.ylabel('Objective Value Error', fontsize=12)
    plt.yscale('log')
Esempio n. 48
0
def draw_static_compare():
    '''
    draw the static graph of comparison
    draw two figures: one for time, one for memory
    '''
    # load data
    c = 0.6
    epsilon = 0.01
    g_names = []
    rlp_time = []
    flp_time = []
    pcg_time = []
    rlp_mem = []
    flp_mem = []
    pcg_mem = []
    lin_time = []
    lin_mem = []
    cloudwalker_time = []
    cloudwalker_mem = []
    for data in datasets:
        g_names.append(data_names[data])
        rlp_data = parse_exp_file(RLP_get_data_file_base(data, c, epsilon) + ".meta")
        flp_data = parse_exp_file(FLP_get_data_file_base(data, c, epsilon) + ".meta")
        lin_data = parse_lin_file(get_lineard_file(data))
        cloudwalker_data = parse_lin_file(get_cloudwalker_file(data))
        tkde_data = {}
        print(rlp_data["time"], flp_data["time"])
        print(rlp_data["mem"], flp_data["mem"])
        # get tkde data
        tkde_file = TKDE17_get_data_file_base(data, c, epsilon) + ".meta"
        if (os.path.isfile(tkde_file)):
            with open(tkde_file, "r") as f:
                lines = f.readlines()
                tkde_data["time"] = float(lines[0].strip())
                tkde_data["mem"] = float(lines[1].strip())
        else:
            tkde_data["time"] = 0
            tkde_data["mem"] = 0
        print(rlp_data["time"], flp_data["time"], tkde_data["time"])
        print(rlp_data["mem"], flp_data["mem"], tkde_data["mem"])
        # file the data into the list
        rlp_time.append(rlp_data["time"])
        rlp_mem.append(rlp_data["mem"])
        flp_time.append(flp_data["time"])
        flp_mem.append(flp_data["mem"])
        pcg_time.append(tkde_data["time"])
        pcg_mem.append(tkde_data["mem"])
        lin_time.append(lin_data["time"])
        lin_mem.append(lin_data["mem"])
        cloudwalker_time.append(cloudwalker_data["time"])
        cloudwalker_mem.append(cloudwalker_data["mem"])

    # draw the cpu figures
    size_of_fig = (FIG_SIZE_MULTIPLE)
    fig, ax = plt.subplots()
    N = len(g_names)
    width = 0.2  # the width of the bars
    ind = 1.2 * np.arange(N)  # the x locations for the groups
    rlp_time_bar = ax.bar(ind, rlp_time, width, hatch="//", label="Opt-LP", fill=False)
    flp_time_bar = ax.bar(ind + width, flp_time, width, hatch=".", label="FLP", fill=False)
    lin_time_bar = ax.bar(ind + 3 * width, lin_time, width, hatch='x', label="LIN", fill=False)
    cloudwalker_time_bar = ax.bar(ind + 2 * width, cloudwalker_time, width, hatch='-', label="MCAP", fill=False)
    pcg_time_bar = ax.bar(ind + 4 * width, pcg_time, width, hatch='+', label="PCG", fill=False)

    plt.yscale('log')  # log scale of y
    # plt.ylim(ymin=-2)  # this line
    ax.set_xticks(ind + 2 * width)
    ax.set_xticklabels(g_names, fontsize=LABEL_SIZE)
    ax.set_ylabel("CPU Time(s)", fontsize=LABEL_SIZE)
    plt.xticks(fontsize=TICK_SIZE)
    plt.yticks(fontsize=TICK_SIZE)

    fig.set_size_inches(*size_of_fig)  # set ratio
    plt.legend(prop={'size': LEGEND_SIZE, "weight": "bold"}, loc="upper left", ncol=5)  # number of algorithms
    fig.savefig("./figures/" + 'static_cpu_compare.pdf', bbox_inches='tight', dpi=300)

    # draw the memory figure
    fig, ax = plt.subplots()
    N = len(g_names)
    ind = 1.2 * np.arange(N)  # the x locations for the groups
    rlp_mem_bar = ax.bar(ind, np.array(rlp_mem) / 1000, width, \
                         hatch="//", label="Opt-LP", fill=False)
    flp_mem_bar = ax.bar(ind + width, np.array(flp_mem) / 1000, width, \
                         hatch=".", label="FLP", fill=False)
    lin_mem_bar = ax.bar(ind + 3 * width, np.array(lin_mem) / 1000, width, hatch='x', label="LIN", fill=False)
    cloudwalker_mem_bar = ax.bar(ind + 2 * width, np.array(cloudwalker_mem) / 1000, width, hatch='-', label="MCAP",
                                 fill=False)
    pcg_mem_bar = ax.bar(ind + 4 * width, np.array(pcg_mem) / 1000, width, \
                         hatch='+', label="PCG", fill=False)

    plt.yscale('log')  # log scale of y
    # plt.ylim(ymin=-2)  # this line
    ax.set_xticks(ind + 2 * width)
    ax.set_xticklabels(g_names, fontsize=LABEL_SIZE)
    ax.set_ylabel("Memory Usage(MB)", fontsize=LABEL_SIZE)
    plt.xticks(fontsize=TICK_SIZE)
    plt.yticks(fontsize=TICK_SIZE)

    fig.set_size_inches(*size_of_fig)  # set ratio
    plt.legend(prop={'size': LEGEND_SIZE, "weight": "bold"}, loc="upper left", ncol=5)
    fig.savefig("./figures/" + 'static_mem_compare.pdf', bbox_inches='tight', dpi=300)
    return
        def plot_1d_hists(dim):

            plot_label = dimension_labels[dim]

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

            plt.suptitle('step %d - Parameter - %s' % (e, plot_label))

            plt.subplot(3, 3, 1)
            #inital real
            plt.title('Inital unseen test input', fontsize='x-small')
            plt.hist(sample_to_test[:, 0, dim],
                     bins=50,
                     range=[
                         np.amin(sample_to_test[:, 0, dim]),
                         np.amax(sample_to_test[:, 0, dim])
                     ])
            plt.subplot(3, 3, 2)
            #final real
            plt.title('GEANT4 output', fontsize='x-small')
            plt.hist(sample_to_test[:, 1, dim],
                     bins=50,
                     range=[
                         np.amin(sample_to_test[:, 1, dim]),
                         np.amax(sample_to_test[:, 1, dim])
                     ],
                     color='g')

            plt.subplot(3, 3, 4)
            #inital fake
            plt.title('Exact same test input', fontsize='x-small')
            plt.hist(synthetic_test_output[:, 0, dim],
                     bins=50,
                     range=[
                         np.amin(sample_to_test[:, 0, dim]),
                         np.amax(sample_to_test[:, 0, dim])
                     ])
            plt.subplot(3, 3, 5)
            #final fake
            plt.title('GAN output - same range', fontsize='x-small')
            plt.hist(synthetic_test_output[:, 1, dim],
                     bins=50,
                     range=[
                         np.amin(sample_to_test[:, 1, dim]),
                         np.amax(sample_to_test[:, 1, dim])
                     ],
                     color='r')
            plt.subplot(3, 3, 7)
            #final fake - full range
            plt.title('GAN output - full range', fontsize='x-small')
            plt.hist(synthetic_test_output[:, 1, dim], bins=50, color='r')

            plt.subplot(3, 3, 8)
            #final fake
            plt.title('Output overlays', fontsize='x-small')
            plt.hist(
                [sample_to_test[:, 1, dim], synthetic_test_output[:, 1, dim]],
                histtype='step',
                bins=50,
                label=['GEANT4', 'GAN'],
                range=[
                    np.amin(sample_to_test[:, 1, dim]),
                    np.amax(sample_to_test[:, 1, dim])
                ])
            plt.legend(loc='upper right')
            plt.tick_params(axis='y', which='both', labelsize=5)
            plt.tick_params(axis='x', which='both', labelsize=5)

            plt.subplot(3, 3, 9)
            #final fake
            plt.title('Output overlays', fontsize='x-small')
            plt.hist(
                [sample_to_test[:, 1, dim], synthetic_test_output[:, 1, dim]],
                histtype='step',
                bins=50,
                label=['GEANT4', 'GAN'],
                range=[
                    np.amin(sample_to_test[:, 1, dim]),
                    np.amax(sample_to_test[:, 1, dim])
                ])
            plt.legend(loc='upper right')
            plt.yscale('log')
            plt.tick_params(axis='y', which='both', labelsize=5)
            plt.tick_params(axis='x', which='both', labelsize=5)

            plt.savefig('%s1D_hist_%d.png' % (output_location, dim),
                        bbox_inches='tight')
            plt.savefig('%s%d/1D_hist_%d.png' % (output_location, dim, e),
                        bbox_inches='tight')

            plt.close('all')
Esempio n. 50
0
    def do_fit(self, ):

        # run emcee:
        sampler = self.run_emcee()

        # plot chains:
        fig, axes = plt.subplots(self.ndim, figsize=(10, 7), sharex=True)
        samples = sampler.get_chain()
        labels = ["Fvb", "vb", "p", "log(f)"]
        for i in range(self.ndim):
            ax = axes[i]
            ax.plot(samples[:, :, i], "k", alpha=0.3)
            ax.set_xlim(0, len(samples))
            ax.set_ylabel(labels[i])
            ax.yaxis.set_label_coords(-0.1, 0.5)

        axes[-1].set_xlabel("step number")

        # plot 2D parameter posterior distributions:
        burnin = self.burnin
        flat_samples = sampler.get_chain(discard=burnin, thin=15, flat=True)
        print(flat_samples.shape)
        fig = corner.corner(flat_samples, labels=labels)
        fig.savefig(f'{self.name}_2dposteriors.pdf')
        print('----------------------------------------------------------')
        print('MCMC results:')

        # get autocorrelation time:
        try:
            tau = sampler.get_autocorr_time()

        except:
            print(
                '**Warning** The chain is shorter than 50 times the integrated autocorrelation time for 4 parameter(s). Use this estimate with caution and run a longer chain!'
            )
            tau = np.inf

        print(
            f'The autocorrelation time is {tau}. You should run the chains for at least 10 x steps as this.'
        )

        # extract p plus uncertainties:
        results = []
        results_up = []
        results_low = []
        print('The MCMC fit parameters are:')
        for i in range(self.ndim):
            mcmc = np.percentile(flat_samples[:, i], [16, 50, 84])
            q = np.diff(mcmc)
            txt = "\mathrm{{{3}}} = {0:.3f}_{{-{1:.3f}}}^{{{2:.3f}}}"
            txt = txt.format(mcmc[1], q[0], q[1], labels[i])
            results.append(mcmc[1])
            results_up.append(q[1])
            results_low.append(q[0])
            display(Math(txt))

        print('----------------------------------------------------------')

        # plot the observed and model spectra:

        # plot flux density SED:

        f = plt.figure(figsize=(8, 8))

        vs = np.linspace(0, 30, 100)

        emcee_flux = self.powerlaw(vs, results[0], results[1], results[2])
        emcee_flux_up = self.powerlaw(vs, results_up[0], results_up[1],
                                      results_up[2])
        emcee_flux_low = self.powerlaw(vs, results_low[0], results_low[1],
                                       results_low[2])

        plt.scatter(self.frequency, self.flux_emission)
        plt.errorbar(
            self.frequency,
            self.flux_emission,
            yerr=[self.fd_err_up, self.fd_err_low],
            fmt='.',
            capsize=2,
        )

        plt.plot(vs, emcee_flux)
        plt.plot(vs, emcee_flux + emcee_flux_up, color='grey', alpha=0.5)
        plt.plot(vs, emcee_flux - emcee_flux_low, color='grey', alpha=0.5)
        # plt.plot(frequency[5:], 4*frequency[5:]**-3)

        # plt.axhline(y=np.max(flux_emission),label=r'F_p')

        plt.xscale('log')
        plt.yscale('log')

        # plt.axis([1, 30, 0.5e-2,12e-2])

        plt.xlabel('Frequency (GHz)')
        plt.ylabel('Flux Density (mJy)')

        plt.axvline(x=vs[np.where(emcee_flux == np.max(emcee_flux))],
                    ls='--',
                    color='grey')
        plt.axhline(y=np.max(emcee_flux), ls='--', color='grey')

        plt.savefig(f'{self.name}_model_spectrum.pdf')

        # print peak flux, peak frequency, and p of spectrum:
        print('----------------------------------------------------------')
        print('The peak flux, peak frequency, and p of the spectrum are:')
        up = emcee_flux_up[np.where(emcee_flux == np.max(emcee_flux))]
        low = emcee_flux_low[np.where(emcee_flux == np.max(emcee_flux))]
        Ferror = (up + low) / 2
        print(f'Fp = {np.max(emcee_flux):.2f} +/- {Ferror[0]:.2f} mJy')
        print(
            f'vp = {vs[np.where(emcee_flux == np.max(emcee_flux))][0]:2f} GHz')
        print(
            f'p = {results[2]:.2f} +{results_up[2]:.2f} - {results_low[2]:.2f} '
        )
        print('----------------------------------------------------------')
        Fvb = results[0]
        Fvb_u = (results_up[0] + results_low[0]) / 2
        vb = results[1]
        vb_u = (results_up[1] + results_low[1]) / 2
        p = results[2]
        p_u = (results_up[2] + results_low[2]) / 2
        Fp = np.max(emcee_flux)
        Fp_u = Ferror[0]
        vp = vs[np.where(emcee_flux == np.max(emcee_flux))][0]
        return Fvb, vb, p, Fp, vp, Fvb_u, vb_u, p_u, Fp_u
Esempio n. 51
0
    def __get_aligned_nr_pn_amp_phase__(this,
                                        lm,
                                        kind,
                                        plot=False,
                                        verbose=False):
        '''
        Given optimal hybrid params for l=m=2, begin to estimate optimal phase alignment for PN
        '''
        # Import usefuls
        from numpy import argmax, array, unwrap, angle, pi, mean, exp, arange
        # Unpack l and m
        l, m = lm
        # Create shorthand for useful information
        k0 = this.optimal_hybrid_params['k0']
        dt = this.optimal_hybrid_params['dt']
        mask = this.optimal_hybrid_params['mask']
        T1 = this.optimal_hybrid_params['T1']
        T2 = this.optimal_hybrid_params['T2']
        phi22 = this.optimal_hybrid_params['phi0_22']
        # Find the peak location for l=m=2 to be used for masking below
        k_amp_max_22 = argmax(this.__unpacklm__((2, 2), kind)[1])
        # Get the format aligned data for this multipole
        t, nr_y, pn_y = this.__unpacklm__(lm, kind)
        # Apply optimal time shift, NOTE the rescaling of phi22
        pn_y, phi0 = this.__apply_hyb_params_to_pn__(pn_y,
                                                     nr_y,
                                                     k0 * dt,
                                                     MSK=mask,
                                                     TSMETH='index',
                                                     phi0=m * phi22 / 2)
        # Begin phase alignment
        __getphi__ = lambda X: unwrap(angle(X))

        # Get phases aligned in mask region
        def get_aligned_phi(a, b, __mask__):
            '''Get phases aligned in mask region'''
            A = __getphi__(a)
            B = __getphi__(b)
            L = pi / 2
            n = round(mean(B[__mask__] - A[__mask__]) / L)
            B -= n * L
            return A, B

        # Get NR phase
        nr_phi = __getphi__(nr_y)
        # Find a mask for the smoothest part
        nr_smoothest_mask_amp = smoothest_part(abs(nr_y)[:k_amp_max_22])
        nr_smoothest_mask_pha = smoothest_part(nr_phi[:k_amp_max_22])
        # nr_smoothest_mask_pha = nr_smoothest_mask_amp
        # Take the union of the amplitude and phase masks
        nr_smoothest_mask = arange(
            min(min(nr_smoothest_mask_amp), min(nr_smoothest_mask_pha)),
            min(max(nr_smoothest_mask_amp), max(nr_smoothest_mask_pha)))
        # Get the aligned phases (NOTE that nr_phi should not have changed)
        # NOTE that below we wish to use T1 as the start of the alignment region if the entire NR waveform is erroneously deemed smooth (e.g. due to settings in smoothest_part)
        nr_phi, pn_phi = get_aligned_phi(
            nr_y, pn_y, nr_smoothest_mask[:10] if t[nr_smoothest_mask][0] > T1
            else nr_smoothest_mask[t[nr_smoothest_mask] >= T1][:10])
        # Get Amplitudes
        nr_amp, pn_amp = abs(nr_y), abs(pn_y)
        # --
        # Impose scaling of PN amplitude to match NR to accomodate limitations of PN
        # NOTE that this is an agressive approach
        if this.aggressive:
            # >>>
            T1k = max(t[nr_smoothest_mask[0]], T1)
            T2k = T1k + (T2 - T1) / this.optimal_hybrid_params['hybrid_cycles']
            k = (t >= T1k) & (t <= T2k)
            scale_factor = mean(nr_amp[k] / pn_amp[k])
            if this.aggressive == 2:
                warning(
                    'A scale factor of %f is applied to the PN amplitude.' %
                    scale_factor,
                    verbose=this.verbose)
                pn_amp *= scale_factor
            # >>>
            phase_shift = mean(nr_phi[k] - pn_phi[k])
            warning('The PN phase will be shifted by %f (rad).' % phase_shift)
            pn_phi += phase_shift
            # >>>
        # --
        # Plotting
        if plot:
            # Import usefuls
            from matplotlib.pyplot import figure, figaspect, plot, xlim, ylim, xscale, subplot, show
            from matplotlib.pyplot import yscale, axvline, axvspan, xlabel, ylabel, title, yscale, legend
            # Create figure
            fig = figure(figsize=3 * figaspect(4.0 / 7))
            # Plot phases
            subplot(2, 1, 2)
            if this.aggressive:
                # Hilight the aggressive alignment region
                axvspan(T1k, T2k, color='lawngreen', alpha=0.2)
                axvline(T1k, color='g', ls='--')
                axvline(T2k, color='g', ls='--')
            plot(t[nr_smoothest_mask],
                 nr_phi[nr_smoothest_mask],
                 color='k',
                 lw=3,
                 alpha=0.4,
                 ls='--')
            plot(t, nr_phi)
            plot(t, pn_phi, lw=1)
            # Set plot limits
            ylim(min(pn_phi), max(lim(nr_phi[nr_smoothest_mask], dilate=0.1)))
            xlim(0, max(t[nr_smoothest_mask]))
            # Highlight the hybridization region
            axvspan(T1, T2, alpha=0.15, color='cyan')
            axvline(T1, color='c', ls='--')
            axvline(T2, color='c', ls='--')
            # Set axis labels
            xlabel(r'$t/M$')
            ylabel(r'$\phi_{%i%i}=\arg(\psi_{%i%i})$' % (l, m, l, m))
            # Plot amplitudes
            subplot(2, 1, 1)
            if this.aggressive:
                # Hilight the aggressive alignment region
                axvspan(T1k,
                        T2k,
                        color='lawngreen',
                        alpha=0.2,
                        label='Aggressive alignment region')
                axvline(T1k, color='g', ls='--')
                axvline(T2k, color='g', ls='--')
            # Add amplitude lines
            yscale('log')
            plot(t[nr_smoothest_mask],
                 nr_amp[nr_smoothest_mask],
                 color='k',
                 lw=3,
                 alpha=0.4,
                 ls='--')
            plot(t, nr_amp)
            plot(t, pn_amp, lw=1)
            # Set plot limits
            ylim(min(pn_amp[pn_amp > 0]),
                 max(lim(nr_amp[nr_smoothest_mask], dilate=0.1)))
            xlim(0, max(t[nr_smoothest_mask]))
            # Highlight the hybridization region
            axvspan(T1,
                    T2,
                    alpha=0.15,
                    color='cyan',
                    label='Final hybridization region')
            axvline(T1, color='c', ls='--')
            axvline(T2, color='c', ls='--')
            # Set axis labels
            ylabel(r'$|\psi_{%i%i}|$' % (l, m))
            legend()
            # xlabel(r'$t/M$')
            # show()
        # Package output
        foo = {}
        foo['nr_amp'] = nr_amp
        foo['nr_phi'] = nr_phi
        foo['pn_amp'] = pn_amp
        foo['pn_phi'] = pn_phi
        foo['nr_smoothest_mask'] = nr_smoothest_mask
        foo['t'] = t
        # Return aligned phases
        return foo
        def plot_p_against_pt(geant_array, gan_array):

            geant_array = np.swapaxes(geant_array, 0, 1)[1]
            gan_array = np.swapaxes(gan_array, 0, 1)[1]

            geant_array = np.swapaxes(geant_array, 0, 1)
            gan_array = np.swapaxes(gan_array, 0, 1)

            trans_geant = np.sqrt(np.add(geant_array[2]**2, geant_array[3]**2))
            trans_gan = np.sqrt(np.add(gan_array[2]**2, gan_array[3]**2))

            total_mom_geant = np.add(trans_geant, geant_array[4])
            total_mom_gan = np.add(trans_gan, gan_array[4])

            plt.figure(figsize=(8, 8))
            plt.suptitle('step %d' % (e))

            plt.subplot(2, 2, 1)
            plt.title('p vs pt GEANT4', fontsize='x-small')
            plt.hist2d(total_mom_geant,
                       trans_geant,
                       bins=50,
                       norm=LogNorm(),
                       cmap=plt.cm.CMRmap)
            plt.xlabel('P (GeV)')
            plt.ylabel('P_t (GeV)')

            plt.subplot(2, 2, 2)
            plt.title('p vs pt GAN', fontsize='x-small')
            plt.hist2d(total_mom_gan,
                       trans_gan,
                       bins=50,
                       norm=LogNorm(),
                       cmap=plt.cm.CMRmap)
            plt.xlabel('P (GeV)')
            plt.ylabel('P_t (GeV)')

            plt.subplot(2, 2, 3)
            plt.title('p overlay', fontsize='x-small')
            plt.hist([total_mom_geant, total_mom_gan],
                     bins=50,
                     histtype='step',
                     label=['GEANT4', 'GAN'])
            plt.xlabel('P (GeV)')
            plt.yscale('log')
            plt.legend(loc='upper right')

            plt.subplot(2, 2, 4)
            plt.title('p_t overlay', fontsize='x-small')
            plt.hist([trans_geant, trans_gan],
                     bins=50,
                     histtype='step',
                     label=['GEANT4', 'GAN'])
            plt.xlabel('P_t (GeV)')
            plt.yscale('log')
            plt.legend(loc='upper right')

            plt.savefig('%smomentum/momentum_%d.png' % (output_location, e),
                        bbox_inches='tight')
            plt.savefig('%scurrent_momentum.png' % output_location,
                        bbox_inches='tight')
            plt.close('all')
Esempio n. 53
0
    def __calc_single_multipole_hybrid__(this, lm, kind, plot=False):

        # Import usefuls
        from numpy import zeros_like, argmax, arange, diff, roll, unwrap, mod, exp

        # Create shorthand for useful information
        l, m = lm
        T1 = this.optimal_hybrid_params['T1']
        T2 = this.optimal_hybrid_params['T2']

        # Get the time and phase aligned waveforms
        foo = this.__get_aligned_nr_pn_amp_phase__(lm, kind, plot=plot)
        nr_amp = foo['nr_amp']
        nr_phi = foo['nr_phi']
        pn_amp = foo['pn_amp']
        pn_phi = foo['pn_phi']
        t = foo['t']
        pn_t_amp_max = t[argmax(pn_amp)]

        #
        stride = 4 * (T2 - T1)
        pnmask = (t >= max(T1 - stride, 0)) & (t <= T2)
        nrmask = (t >= t[foo['nr_smoothest_mask']][0]) & (t <= min(
            pn_t_amp_max, t[foo['nr_smoothest_mask']][0] + stride))

        # Deermine if a bridge model is needed
        # NOTE that a bridge model (between PN and NR regions over the noisey NR) is only needed if the smooth NR data does not contain the region bounded by [T1,T2].
        BRIDGE = t[nrmask][0] > T1

        #
        if BRIDGE:

            # Create training data for bridge models
            __bridge_t = t.copy()
            __bridge_t = __bridge_t[pnmask | nrmask]
            #
            __bridge_amp = zeros_like(t)
            __bridge_amp[pnmask] = pn_amp[pnmask]
            __bridge_amp[nrmask] = nr_amp[nrmask]
            __bridge_amp = __bridge_amp[pnmask | nrmask]
            #
            __bridge_phi = zeros_like(t)
            __bridge_phi[pnmask] = pn_phi[pnmask]
            __bridge_phi[nrmask] = nr_phi[nrmask]
            __bridge_phi = __bridge_phi[pnmask | nrmask]

            # Model the AMPLITUDE with fixed symbols; TODO -- check for poles which seem to appear when both NR and PN are poor
            rope = gmvrfit(__bridge_t, __bridge_amp)
            # rope = mvrfit( __bridge_t, __bridge_amp, numerator_symbols=[('00')], denominator_symbols=['00','000'] )

            # Model the PHASE with fixed symbols
            plank = mvpolyfit(__bridge_t,
                              __bridge_phi,
                              basis_symbols=['K', '0', '000'])
            # plank = gmvpfit( __bridge_t, __bridge_phi ) # NOTE -- this works but is slower

            # Create blending window towards connecting models with NR
            bridge_amp = rope.eval(t)
            bridge_phi = plank.eval(t)
            bridge_state = lim(arange(len(t))[nrmask])
            window = maketaper(t, bridge_state)

            # Extend NR towards PN using models
            u = window
            extended_nr_amp = (1 - u) * bridge_amp + u * nr_amp
            extended_nr_phi = (1 - u) * bridge_phi + u * nr_phi

        else:

            #
            warning(
                'The NR data appares to be sufficiently smooth. No bridge model will be used.',
                verbose=this.verbose)

            # No extension to the left (i.e. bridge) is required
            extended_nr_amp = nr_amp
            extended_nr_phi = nr_phi

        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- #
        # Create final hybrid multipole by repeating the process above for the extended nr and pn
        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- #
        # Select hybridization region & make related taper
        mask = (t >= T1) & (t <= T2)
        hybrid_state = lim(arange(len(t))[mask])
        u = maketaper(t, hybrid_state)
        # Currently, the taper removes PN data that may be to the right of NR data due to wrapping around when time shifting, so we correct for this by turning the taper off
        k_pn_start = int(mod(this.optimal_hybrid_params['k0'], len(t)))
        u[k_pn_start:] = 0
        hybrid_amp = (1 - u) * pn_amp + u * extended_nr_amp
        hybrid_phi = (1 - u) * pn_phi + u * extended_nr_phi
        # hybrid_amp = roll(hybrid_amp,k_pn_start)
        # hybrid_phi = unwrap(roll(hybrid_phi,k_pn_start))

        # Plotting
        fig, (ax1, ax2) = None, (None, None)
        if plot:
            # Import usefuls
            from matplotlib.pyplot import figure, figaspect, plot, xlim, ylim, xscale, subplot, show
            from matplotlib.pyplot import yscale, axvline, axvspan, xlabel, ylabel, title, yscale, legend
            # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- #
            # Informative plots of intermediate information
            # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- #
            # Create figure
            fig = figure(figsize=3 * figaspect(4.0 / 7))
            #
            ax1 = subplot(2, 1, 1)
            # Show training regions
            if BRIDGE:
                axvspan(min(t[pnmask]),
                        max(t[pnmask]),
                        color='cyan',
                        alpha=0.15,
                        label='Bridge Training Region')
                axvspan(min(t[nrmask]),
                        max(t[nrmask]),
                        color='cyan',
                        alpha=0.15)
                axvline(min(t[pnmask]), color='c', ls='--')
                axvline(max(t[pnmask]), color='c', ls='--')
                axvline(min(t[nrmask]), color='c', ls='--')
                axvline(max(t[nrmask]), color='c', ls='--')
            # Plot amplitudes
            plot(t, nr_amp, label='NR', alpha=0.2, lw=4, ls='--')
            if BRIDGE: plot(t, rope.eval(t), lw=2, label='Bridge Model')
            plot(t, hybrid_amp, '-', label='Hybrid')
            plot(t,
                 pn_amp,
                 label='PN',
                 linewidth=2,
                 ls='--',
                 color='k',
                 alpha=0.5)
            # Set plot limits
            yscale('log')
            ylim(min(pn_amp[pn_amp > 0]),
                 max(lim(nr_amp[foo['nr_smoothest_mask']], dilate=0.1)))
            xlim(0, max(t[foo['nr_smoothest_mask']]))
            # Set axis labels and legend
            ylabel(r'$|\psi_{%i%i}|$' % (l, m))
            legend(frameon=True, framealpha=1, edgecolor='k', fancybox=False)
            #
            ax2 = subplot(2, 1, 2)
            # Show training regions
            if BRIDGE:
                axvspan(min(t[pnmask]),
                        max(t[pnmask]),
                        color='cyan',
                        alpha=0.15,
                        label='Bridge Training Region')
                axvspan(min(t[nrmask]),
                        max(t[nrmask]),
                        color='cyan',
                        alpha=0.15)
                axvline(min(t[pnmask]), color='c', ls='--')
                axvline(max(t[pnmask]), color='c', ls='--')
                axvline(min(t[nrmask]), color='c', ls='--')
                axvline(max(t[nrmask]), color='c', ls='--')
            # Plot phases
            plot(t, nr_phi, label='NR', alpha=0.2, lw=4, ls='--')
            if BRIDGE: plot(t, plank.eval(t), lw=2, label='Bridge Model')
            plot(t, hybrid_phi, '-')
            plot(t,
                 pn_phi,
                 label='PN',
                 linewidth=2,
                 ls='--',
                 color='k',
                 alpha=0.5)
            # Set plot limits
            ylim(min(pn_phi),
                 max(lim(nr_phi[foo['nr_smoothest_mask']], dilate=0.1)))
            xlim(0, max(t[foo['nr_smoothest_mask']]))
            # plot( t, window*diff(ylim())+min(ylim()), color='k',alpha=0.3 )
            # plot( t, (1-window)*diff(ylim())+min(ylim()), color='k',alpha=0.3 )
            # Set axis labels
            xlabel(r'$t/M$')
            ylabel(r'$\phi_{%i%i}=\arg(\psi_{%i%i})$' % (l, m, l, m))
            # # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- #
            # # Informative plots of final hybrid and original pn+nr
            # # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- #
            # # Create figure
            # fig = figure( figsize=1.5*figaspect(4.0/7) )
            # #
            # ax1 = subplot(2,1,1)
            # # plot( t, nr_amp, label='NR', alpha = 0.5 )
            # plot( t, roll(hybrid_amp,int(this.optimal_hybrid_params['k0'])), label='Hybrid' )
            # plot( t, roll(pn_amp,int(this.optimal_hybrid_params['k0'])), label='PN' )
            # # Set plot limits
            # yscale('log')
            # # ylim( min(pn_amp[pn_amp>0]),max(lim( nr_amp[foo['nr_smoothest_mask']], dilate=0.1)) )
            # # xlim( 0,max(t[foo['nr_smoothest_mask']]) )
            # # Set axis labels and legend
            # ylabel(r'$|\psi_{%i%i}|$'%(l,m))
            # legend()
            # #
            # ax2 = subplot(2,1,2)
            # # plot( t, nr_phi, label='NR', alpha = 0.5 )
            # plot( t, unwrap(roll(hybrid_phi,int(this.optimal_hybrid_params['k0']))), label='Hybrid' )
            # plot( t, unwrap(roll(pn_phi,int(this.optimal_hybrid_params['k0']))), label='PN' )
            # # Set plot limits
            # # ylim( min(pn_phi),max(lim( nr_phi[foo['nr_smoothest_mask']], dilate=0.1)) )
            # # xlim( 0,max(t[foo['nr_smoothest_mask']]) )
            # # Set axis labels
            # xlabel(r'$t/M$'); ylabel(r'$\phi_{%i%i}=\arg(\psi_{%i%i})$'%(l,m,l,m))

        # Output data for further processing
        return t, hybrid_amp, hybrid_phi
Esempio n. 54
0
]

barWidth = 0.2
opacity = 0.4

index = np.arange(15) + 1

plt.bar(index - 0.1,
        pythonResults,
        barWidth,
        alpha=opacity,
        color='r',
        label='python')
plt.bar(index + 0.1,
        numpyResults,
        barWidth,
        alpha=opacity,
        color='g',
        label='numpy')

plt.yscale('symlog')

# ax.yaxis.set_ticks(np.arange(min(npy), max(npy)*0.02, 0.02))

plt.ylabel('Running time in seconds')

plt.title("Running time comparison between python and numpy")
plt.legend()

plt.show()
def plot_MVAoutput(y_truth, y_score, out_path, label='', nbins=100):
    """
    Plots the MVA output as histogram and returns the underlying
    distributions of the positive and the negative class.
    """

    print('::   Creating MVA output plot...')

    
    y_score_truePos = y_score[np.array(y_truth==1)]
    y_score_trueNeg = y_score[np.array(y_truth==0)]
    y_score_trueBG  = y_score[np.array(y_truth==99)]

    
    plt.figure()
    n_col = 2

    n_total, bins_total, patches_total = \
        plt.hist(y_score,
                 bins=nbins,
                 range=(0., 1.),
                 alpha=.25,
                 color='black',
                 label='S+B')
   
    if y_score_truePos.shape[0] > 0:
        n_truePos, bins_truePos, patches_truePos = \
            plt.hist(y_score_truePos,
                     bins=nbins,
                     range=(0., 1.),
                     alpha=0.5,
                     color='green',
                     label='Sig')
        n_col = n_col + 1
    else:
        n_truePos = None
        
    n_trueNeg, bins_trueNeg, patches_trueNeg = \
        plt.hist(y_score_trueNeg,
                 bins=nbins,
                 range=(0., 1.),
                 alpha=0.5,
                 color='#dd0000',
                 label='Bg')
     
    if y_score_trueBG.shape[0] > 0:
        n_trueBG, bins_trueBG, patches_trueBG = \
            plt.hist(y_score_trueBG,
                     bins=nbins,
                     range=(0., 1.),
                     alpha=0.5,
                     color='yellow',
                     label='Generated Bg')
        n_trueNeg = n_trueNeg+n_trueBG
        n_col = n_col + 1

    if n_col == 3:
        x_txt = 0.546
        leg_loc = 1
    else:
        x_txt = 0.03
        leg_loc = 2

    #plt.title('MVA output distribution (positive class)')
    plt.xlim(-0.05, 1.05)
    plt.xlabel('MVA output', fontsize=17)
    plt.ylabel('Entries', fontsize=17)
    plt.legend(loc=leg_loc, ncol=n_col,fontsize=12)
    plt.xticks(fontsize=11)
    plt.yticks(fontsize=11)
    plt.annotate('ALICE simulation, this work\nPythia-8 MBR {}\n{} TeV'.format(
        r'$\varepsilon=0.104$', r'$\sqrt{s} = 13$'), 
        xy=(x_txt,0.74),
        xycoords='axes fraction', 
        fontsize=12, 
        bbox=dict(boxstyle="round", fc="w", ec="0.5", alpha=0.4))

    plt.tight_layout()
    plt.savefig(out_path + 'MVAoutput_distr_' + label + '.png')
    plt.savefig(out_path + 'MVAoutput_distr_' + label + '.pdf')

    plt.yscale('log')
    plt.savefig(out_path + 'MVAoutput_distr_' + label + '_log.png')
    plt.savefig(out_path + 'MVAoutput_distr_' + label + '_log.pdf')    
    
    return n_truePos, n_trueNeg
Esempio n. 56
0
    def __calc_l2m2_hybrid_params__(this, plot=False):

        # Import usefuls
        from scipy.optimize import minimize
        from numpy import pi, linspace, mean, std, unwrap, angle, exp

        # Setup plotting
        __plot__ = plot
        if __plot__:
            # Import usefuls
            from matplotlib.pyplot import plot,ylim,xlim,xlabel,ylabel,\
            figure,figaspect,yscale,xscale,axvline,axhline,show,title
            alert('Plots will be generated.', verbose=this.verbose)

        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        # Extract domain and range values of interest
        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        kind = this.__kind__
        pno = this.__pn__
        nro = this.gwylmo
        pn_t, pn_y, nr_t, nr_y = pno.pn_gwylmo.t, pno.pn_gwylmo[
            2, 2][kind].y, nro.t, nro[2, 2][kind].y
        alert(
            'The l=m=2 %s will be used for determining (initial) optimal hybrid params.'
            % cyan(kind),
            verbose=this.verbose)

        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        # Format data in a common way
        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        alert('Aligning formats of NR and PN data', verbose=this.verbose)
        t, pn_y, nr_y = format_align(pn_t,
                                     pn_y,
                                     nr_t,
                                     nr_y,
                                     center_domains=True)
        alert('Storing hybrid time series.', verbose=this.verbose)
        this.t = t

        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        # Align data for initial guesses
        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        alert('Using cross-correlation to estimate optimal params.',
              verbose=this.verbose)
        t, pn_, t, nr_, foo = corr_align(t, abs(pn_y), t, abs(nr_y))

        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        # Calculate time resolution
        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        dt = t[1] - t[0]
        this.dt = dt

        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        # Define limits of fitting region
        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        N = 3  # Width of hybrid region in number of cycles (approx)
        alert('We will use %i cycles for the hybridization region\'s width.' %
              N,
              verbose=this.verbose)
        T1 = (nro.t - nro.t[0])[nro.startindex]
        T2 = (nro.t - nro.t[0])[nro.startindex + int(N * (2 * pi) /
                                                     (nro.wstart_pn / 2))]
        mask = (t >= T1) & (t <= T2)

        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        # Define methods for additional alignment
        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        # Method: Time shift the PN data
        pn_time_shift = lambda TSHIFT, PN=pn_y, METH=None: tshift(
            t, PN, TSHIFT, method=METH)  # roll( pn_y, int(t0/dt) )

        # Method: Align in phase using average phase difference
        def pn_phi_align(PN, NR, return_phi=False, MSK=mask, phi0=None):
            # Apply an optimal phase shift
            getphi = lambda X: unwrap(angle(X))
            if phi0 == None: phi0 = mean((getphi(PN) - getphi(NR))[MSK])
            if return_phi:
                return PN * exp(-1j * phi0), phi0
            else:
                return PN * exp(-1j * phi0)

        # Method: Calculate estimator
        estimartor_fun = lambda PN, NR=nr_y, MSK=mask: abs(
            std(PN[MSK] - NR[MSK]) / std(NR[MSK]))
        # Store usefuls
        alert('Storing shifting functions for future reference.',
              verbose=this.verbose)
        this.__pn_time_shift__ = pn_time_shift
        this.__estimator_fun__ = estimartor_fun
        this.__pn_phi_align__ = pn_phi_align

        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        # Define work function for optimizer
        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        def work(t0):
            # Shift the PN waveform by t0
            pn_y_ = pn_time_shift(t0)
            # Apply an optimal phase shift
            pn_y_ = pn_phi_align(pn_y_, nr_y)
            # Compute a residual error statistic
            frmse = estimartor_fun(pn_y_)
            # Return estimator
            return frmse

        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        # Numerically Determine Optimal Tims Shift
        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #

        alert('Finding optimal time-shift using scipy.optimize.minimize',
              verbose=this.verbose)
        t0_guess = len(t) - foo['domain_shift']
        bear = minimize(work, t0_guess)
        est0 = bear.fun
        t0 = bear.x[0]
        k0 = round(t0 / dt)

        if __plot__:
            t0_space = linspace(t0 - 200 * dt, t0 + 200 * dt, 21)
            figure()
            plot(t0_space, map(work, t0_space))
            xlim(lim(t0_space))
            axvline(t0, color='r', ls='--')
            axvline(t0_guess, color='k', ls='-', alpha=0.3)
            title('(%f,%f)' % (t0, t0_guess))

        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #
        # Apply optimal parameters
        # ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ #

        # Define general functoin for applying hybrid params
        def apply_hyb_params_to_pn(PN,
                                   NR,
                                   T0,
                                   MSK=mask,
                                   TSMETH=None,
                                   phi0=None):
            # Apply optimal time shift
            PN_ = this.__pn_time_shift__(T0, PN=PN, METH=TSMETH)
            # Compute and apply an optimal phase shift
            PN_, phi0 = pn_phi_align(PN_,
                                     NR,
                                     MSK=mask,
                                     phi0=phi0,
                                     return_phi=True)
            # Return deliverables
            return PN_, phi0

        # Store method
        this.__apply_hyb_params_to_pn__ = apply_hyb_params_to_pn

        # Apply optimal time shift
        pn_y_, phi0_22 = this.__apply_hyb_params_to_pn__(pn_y,
                                                         nr_y,
                                                         t0,
                                                         MSK=mask)

        if __plot__:
            figure(figsize=1 * figaspect(1.0 / 7))
            plot(t, pn_y_.real)
            plot(t, nr_y.real)
            plot(t, abs(nr_y))
            plot(t, abs(pn_y_))
            xlim([100, 2500])
            axvline(T1, color='b')
            axvline(T2, color='b')
            yscale('log', nonposy='clip')
            ylim([1e-6, 5e-3])

        # Store optimals
        alert('Storing optimal params to this.optimal_hybrid_params',
              verbose=this.verbose)
        this.optimal_hybrid_params = {
            't0': t0,
            'dt': dt,
            'k0': k0,
            'phi0_22': phi0_22,
            'mask': mask,
            'T1': T1,
            'T2': T2,
            'hybrid_cycles': N
        }
        if this.verbose: print this.optimal_hybrid_params
def plot_smoothed(data,
                  N,
                  observable,
                  method,
                  t_odes,
                  fit_range=None,
                  plot=True,
                  mags=[]):
    mag_vals = {
        0.0: 0.33332738590880195,
        0.025: 0.33307956586891685,
        0.05: 0.3326564798699867,
        0.07500000000000001: 0.3320883979754039,
        0.1: 0.3313545522832395,
        0.125: 0.3302569822117752,
        0.15000000000000002: 0.3290837899991117,
        0.17500000000000002: 0.327625550776773,
        0.2: 0.32559085983104563,
        0.225: 0.32329927512893614
    }
    megam = [0, 25, 50, 75, 100, 125, 150, 175, 200, 225]
    multiplier = 1

    plt.xscale("log")
    plt.yscale("log")
    plt.xlabel(r"$\delta$ = 1 + $\epsilon$")
    plt.ylabel("Thermalization time ")
    cmap = plt.get_cmap('tab10')
    therm_sequence = data[observable][N]
    prefix = r"$T_{th}$ from $\overline{\mathcal{O}(t)}, $" + " {}, method: {}".format(
        observable, method)
    """Getting time at which average goes over threshold"""
    mean_therm_times = []
    hams = []
    deltas = list(t_odes[N].keys())
    deltas.sort()
    for delta in deltas:
        if delta > 900:
            continue
        t_therm = 0
        for megamacek in megam:
            if type(therm_sequence[1000 * delta + megamacek]) == np.float64:
                continue

            if observable == "bondz_mean":
                limit = (1 - 0.001 * delta) / 3
            else:
                limit = mag_vals[return_closest(megamacek / 1000,
                                                mag_vals.keys())]
            if method[:3] == "rel":
                threshold = limit * float(method[3:])
            else:
                threshold = limit - float(method[3:])
            if threshold > 0:
                if therm_sequence[1000 * delta + megamacek][-1] > threshold:
                    index = np.where(
                        therm_sequence[1000 * delta +
                                       megamacek] > threshold)[0][0]
                    mag_factor = len([
                        x for x in mags[N][delta]
                        if megamacek == return_closest(1000 * x, megam)
                    ]) / len(mags[N][delta])
                    t_therm += t_odes[N][delta] * (index) / len(
                        therm_sequence[delta]) * mag_factor
                else:
                    t_therm += np.inf
        mean_therm_times.append(t_therm)
        if delta > 1000:
            hams.append(2 - 0.001 * delta)
        else:
            hams.append(0.001 * delta)
        temp_sum = 0
    hams_log = np.log(np.array(hams))
    mean_therm_times_log = np.log(mean_therm_times)
    if plot:
        if fit_range:

            start, end = fit_range
            coefficients, residuals, a, b, c = np.polyfit(
                hams_log[start:end],
                mean_therm_times_log[start:end],
                1,
                full=True)
            print(N, "Thermalization time residuals are ", residuals[0])
            polynomial = np.poly1d(coefficients)
            ys = polynomial(hams_log)
            plt.plot(hams[start:end],
                     multiplier * np.exp(ys[start:end]),
                     color=cmap.colors[1])
            #, label = "N=" + str(N)+ ",exponent = " + str(coefficients[0])[:7])
            plt.scatter(hams,
                        multiplier * np.array(mean_therm_times),
                        label=prefix + ", exp:" + str(coefficients[0])[:5] +
                        ", res: " + str(residuals[0])[:6])
        else:
            plt.scatter(hams,
                        mean_therm_times,
                        label=prefix + " N = " + str(N))
    return dict(zip(hams, mean_therm_times))
def plot_feature(x_sig, x_bg, out_path, **kwargs):
    """
    Plots the features comparing signal and background as histogram 
    """
    x_total = np.concatenate([x_sig, x_bg])
    n_unique = np.unique(x_total).shape[0]
    if n_unique == 0:
        n_unique = 1
        hist_range = (x_total.min()-1., x_total.max()+1)
    else:
        hist_range = np.percentile(np.hstack([x_total]), [0.01, 99.99])

    if n_unique < 40:
        nbins = n_unique*2
    else:
        nbins = 100
    
    plt.figure()

    title = kwargs.pop('title', None)
    xlabel = kwargs.pop('xlabel', 'x')
    xlabel = xlabel.replace('_', ' ')
    sig_label = kwargs.pop('sig_label', 'Signal')
    bg_label = kwargs.pop('bg_label', 'BG')
    combined_label = kwargs.pop('combined_label', 'S+B')
    normed = kwargs.pop('normed', None)

    if kwargs:
        raise TypeError('Invalid kwargs passed: {}'.format(kwargs))

    # #### Kolmogorov-Smirnov test seems not to work at the moment ####
    # h_sig_norm = np.histogram(x_sig, nbins, density=True)[0]
    # h_bg_norm  = np.histogram(x_bg, nbins, density=True)[0] 

    # ks_p_val = stats.ks_2samp(h_sig_norm, h_bg_norm)[1]
    # plt.plot([], [], ' ', label='KS p-value: {:.3f}'.format(ks_p_val))

    n_total, bins_total, patches_total = \
        plt.hist(x_total,
                 bins=nbins,
                 range=hist_range,
                 normed=normed,
                 alpha=.25,
                 color='black',
                 label=combined_label)
    
    n_trueNeg, bins_trueNeg, patches_trueNeg = \
        plt.hist(x_bg,
                 bins=nbins,
                 range=hist_range,
                 normed=normed,
                 alpha=0.5,
                 color='#dd0000',
                 label=bg_label)
    
    n_truePos, bins_truePos, patches_truePos = \
        plt.hist(x_sig,
                 bins=nbins,
                 range=hist_range,
                 normed=normed,
                 alpha=0.5,
                 color='green',
                 label=sig_label)

    # currently not working -> some binning problem 
    # zeros_in_neg = np.where(n_trueNeg==0)[0].tolist()
    # zeros_in_pos = np.where(n_truePos==0)[0].tolist()
    # zeros_in_both = sorted(list(set(zeros_in_pos).intersection(zeros_in_neg)))
    # all_idx = list(range(n_trueNeg.shape[0]))
    # non_zero_lst = [x for x in all_idx if x not in zeros_in_both]
    # ks_p_val = stats.ks_2samp(n_trueNeg[non_zero_lst], n_truePos[non_zero_lst])[1]
    # plt.plot([], [], ' ', label='KS p-value: {:.3f}'.format(ks_p_val))
    
    # plt.title(title, fontsize=18)
    # plt.xlim(-0.05, 1.05)
    plt.xlabel(xlabel, fontsize=17)
    plt.ylabel('Entries', fontsize=17)
    plt.legend(loc='best',fontsize=13)
    plt.xticks(fontsize=11)
    plt.yticks(fontsize=11)
    # plt.annotate('ALICE simulation, this work\nPythia-8 MBR {}\n{} TeV'.format(
    #     r'$\varepsilon=0.104$', r'$\sqrt{s} = 13$'), 
    #     xy=(0.5,0.81),
    #     xycoords='axes fraction', 
    #     fontsize=13, 
    #     bbox=dict(boxstyle="round", fc="w", ec="0.5", alpha=0.5))

    plt.tight_layout()
    plt.savefig(out_path + 'feature_' + title + '_' + xlabel + '.png')
    plt.savefig(out_path + 'feature_' + title + '_' + xlabel + '.pdf')

    plt.yscale('log')
    plt.savefig(out_path + 'feature_' + title + '_' + xlabel + '_log.png')
    plt.savefig(out_path + 'feature_' + title + '_' + xlabel + '_log.pdf')
    
    return n_truePos, n_trueNeg
Esempio n. 59
0
    def plot_simlum(self,isofile='lumfuncs/initial.dat',linestyle='solid',overlay=False,pop='multiple'):
                
        def apparent(M,dguess):
            m = M + 5*np.log10(dguess/10)
            return m
        
        def merr(dist,derr):
            
            m=5*np.log10(dist/10)-5*np.log10((dist-derr)/10)
            
            return m
        def mag(a,b):
            c=np.sqrt(a**2+b**2)
            return c
        
        self.merr=merr
        self.mag=mag
        
        dists=[(680000,30000),(630000,30000),(820000,30000),(785000,25000),(0,0)]
        
        for i in range(len(self.galaxies)):
            
            if self.galaxy==self.galaxies[i]:
                
                distance=dists[i][0]
                disterr=dists[i][1]
                
                break
        
        names=['age','z','magbin','mbolmag','zmag','ymag','jmag','hmag','kmag']
        
        iso=ascii.read(isofile,names=names)
        
        iso=iso.to_pandas()
        

        
        iso.magbin=apparent(iso.magbin,distance)
        

            #label=8 is the AGB phase in padova isochrones
            

        #same method for seperating out the different isochrones in the set
        #as in self.isoplot
        

        
        xdata=iso.magbin
        ydata=iso.kmag
        
        indices=[]
        
        for i in range(1,len(iso.age)):
            if iso.age[i]!=iso.age[i-1] or iso.z[i]!=iso.z[i-1]:
                indices.append(i)
 
        if pop!='single':
        
        
            plt.plot(xdata[:indices[0]],ydata[:indices[0]],linestyle=linestyle,label='log(t) = ' + str(iso.age[0]) + ', Z = ' +str(iso.z[0]))
            print(xdata[:indices[0]])
            print(ydata[:indices[0]])
            for i in range(len(indices)):
                if i==(len(indices)-1):
                    plt.plot(xdata[indices[i]:],ydata[indices[i]:],linestyle=linestyle,label='log(t) = ' + str(iso.age[indices[i]]) + ', Z = ' +str(iso.z[indices[i]]))
                    break
                else:
                    plt.plot(xdata[indices[i]:indices[i+1]],ydata[indices[i]:indices[i+1]],linestyle=linestyle,label='log(t) = ' + str(iso.age[indices[i]]) + ', Z = ' +str(iso.z[indices[i]]))
        
        else:
            
            plt.plot(xdata,ydata,linestyle=linestyle,marker='o',markersize='1',label='log(t) = ' + str(iso.age[0]) + ', Z = ' + str(iso.z[0]))
        
        if overlay==False:
            

                plt.ylabel('N_stars')
                plt.xlabel('$K_0$')
                
                plt.yscale('log')
                
        plt.gca().invert_xaxis()
        plt.legend()            
Esempio n. 60
0
      

    for g in range(len(c2f)):
       cl2.append(c2f[g] - (1.0/(2*g+1)*((abs(alm2[g]))**2)))
    #print(alm2)
    for z in range(len(cl2)):
      c2[z] = c2[z] + cl2[z]   
for c in range(len(cl)):
    c2[c] = c2[c] / (1.0*events2)


for i in range(len(cl)):
   cl[i] = cl[i] - c2[i]
'''
'''
plt.yscale('log')
plt.plot(c)
plt.savefig("powerspect_AMPT.png")
'''
'''
b11 = math.sqrt((2*1+1)/(4*math.pi)*1/math.factorial(2))* -1.56727

b00 = math.sqrt((2*0+1)/(4*math.pi)*1/math.factorial(0))* 1.96962

b22 = math.sqrt((2*2+1)/(4*math.pi)*1/math.factorial(4))* 3.99862

b33 = math.sqrt((2*3+1)/(4*math.pi)*1/math.factorial(6))* -17.6705