Ejemplo n.º 1
0
def imshow_(x, **kwargs):
	if x.ndim == 2:
		plt.imshow(x, interpolation="nearest", **kwargs)
	elif x.ndim == 1:
		plt.imshow(x[:,None].T, interpolation="nearest", **kwargs)
		plt.yticks([])
	plt.axis("tight")
Ejemplo n.º 2
0
    def OnButtonTidyButton(self, event):
        
        # for easy coding
        T = self.TreeCtrlMain
        s = T.GetSelection()
        f = self.GetTreeItemData(s, "figure") 
        w = self.GetTreeItemData(s, "window")
        
        # set the current figure
        pylab.figure(f.number)
        
        # first set the size of the window
        w.SetSize([500,500])
        
        # now loop over all the data and get the range
        lines = f.axes[0].get_lines()
        
        # we want thick lines
        f.axes[0].get_frame().set_linewidth(3.0)

        # get the tick lines in one big list
        xticklines = f.axes[0].get_xticklines()
        yticklines = f.axes[0].get_yticklines()
        
        # set their marker edge width
        pylab.setp(xticklines+yticklines, mew=2.0)
        
        # set what kind of tickline they are (outside axes)
        for l in xticklines: l.set_marker(matplotlib.lines.TICKDOWN)
        for l in yticklines: l.set_marker(matplotlib.lines.TICKLEFT)
        
        # get rid of the top and right ticks
        f.axes[0].xaxis.tick_bottom()
        f.axes[0].yaxis.tick_left()
        
        # we want bold fonts
        pylab.xticks(fontsize=20, fontweight='bold', fontname='Arial')
        pylab.yticks(fontsize=20, fontweight='bold', fontname='Arial')

        # we want to give the labels some breathing room (1% of the data range)
        for label in pylab.xticks()[1]:
            label.set_y(-0.02)
        for label in pylab.yticks()[1]:
            label.set_x(-0.01)
            
        # set the position/size of the axis in the window
        f.axes[0].set_position([0.1,0.1,0.8,0.8])
        
        # set the axis labels
        f.axes[0].set_title('')
        f.axes[0].set_xlabel('')
        f.axes[0].set_ylabel('')

        # set the position of the legend far away
        f.axes[0].legend(loc=[1.2,0])
        
        f.canvas.Refresh()
        
        # autoscale
        self.OnButtonAutoscaleButton(None)
Ejemplo n.º 3
0
Archivo: pca.py Proyecto: id774/sandbox
def test(args):
    data = multivariate_normal([0, 0], [[1, 2], [2, 5]], int(args[1]))
    print(data)
    # PCA
    result = pca(data, base_num=int(args[2]))
    pc_base = result[0]
    print(pc_base)

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

    return 0
Ejemplo n.º 4
0
	def __init__(self, List,Matrix,Artist1="NOTSELECT",Artist2="NOTSELECT"):
		artists = List
		del artists[0]
		self.G = nx.Graph()
		#ノード追加
		for artist in artists:
			self.G.add_node("".join(artist).replace("%20"," "))
		#エッジ追加
		for i, artist in enumerate(artists):
			SimArtist = np.argsort(Matrix[i,:])
			A = artists[SimArtist[len(artists)-2]]
			self.G.add_edge("".join(artist).replace("%20"," "), "".join(A).replace("%20"," "))
		#レイアウト
		pos = nx.spring_layout(self.G)
		#ノード、エッジ調整
		nx.draw_networkx_nodes(self.G, pos, node_size =20, node_color="blue")
		if Artist1 != "NOTSELECT" and Artist2 != "NOTSELECT":
			nx.draw_networkx_nodes(self.G,pos,nodelist=[Artist1,Artist2], node_color= "red" , node_size = 30)
		nx.draw_networkx_edges(self.G, pos, width=1)
		text_items = nx.draw_networkx_labels(self.G, pos, font_size=0.5, font_color="black")
		#フォント調整
		#font_path ="/Users/MASANAOHIROTA/.matplotlib/ipaexm.ttf"
		font_path ="/Library/Fonts/ヒラギノ明朝 Pro W3.otf"
		#font_path="/Library/Fonts/ヒラギノ丸ゴ Pro W4.otf"
		font_prop = fm.FontProperties(fname=font_path)
		for t in text_items.values():
			t.set_fontproperties(font_prop)
		#描画
		plt.xticks([])
		plt.yticks([])
		plt.show()
Ejemplo n.º 5
0
def screeplot(filepath, sigma, comps, div=2, vis=False):
    y = sigma
    x = np.arange(len(y)) + 1

    plt.subplot(2, 1, 1)
    plt.plot(x, y, "o-", ms=2)

    xticks = ["Comp." + str(i) if i%2 == 0 else "" for i in x]

    plt.xticks(x, xticks, rotation=45, fontsize=20)

    # plt.yticks([0, .25, .5, .75, 1], fontsize=20)
    plt.yticks(fontsize=15)
    plt.ylabel("Variance", fontsize=20)
    plt.xlim([0, len(y)])
    plt.title("Plot of the variance of each Singular component", fontsize=25)
    plt.axvspan(10, 11, facecolor='g', alpha=0.5)

    filepath_g = os.path.join(filepath, "graphs")
    if not os.path.exists(filepath_g):
        os.makedirs(filepath_g)

    plt.savefig(filepath_g + "/scree_plot.png", bbox_inches='tight')
    if vis:
        plt.show()
Ejemplo n.º 6
0
def dispcouplets(fname, rows=2, cols=2, size='small',
                 divs=7, normalized=False):
  import numpy as np
  import matplotlib.pylab as plt
  chars=sorted(nletddict(fname, 1))
  sp = nletddict(fname, 2)
  mat = [[sp[ci+cj] for cj in chars] for ci in chars]
  matlab = [[ci+cj for cj in chars] for ci in chars]
  maxcount = max(max(mat))
  l = len(chars)
  pos = np.arange(l)+.5
  for s in range(0, l, rows*cols):
    plt.figure()
    for i in range(rows*cols):
      if i+s<l:
        plt.subplot(rows, cols, i+1)
        plt.barh(pos,mat[i+s],align='center')
        plt.yticks(pos,map(repr,map(second, matlab[i+s])))
        plt.ylabel("couplets")
        plt.xlabel("count")
        if not normalized:
          plt.xticks(np.arange(divs+1)*maxcount/divs, size=size)
        else:
          plt.xticks(size=size)
        plt.title("The %d couplets that begin with %s" % (sum(mat[i+s]), repr(matlab[i+s][0][0])))
  plt.show()
Ejemplo n.º 7
0
def plot_rfs(size, C, Rx, Ry, color='b'):
    radius = np.sqrt(size[...]/np.pi)
    a, w = 0, C.shape[0]
    plt.scatter(Rx, Ry, s=15, color='w', edgecolor='k')
    plt.scatter(C[a:w, 1], C[a:w, 0], s=radius*500, alpha=0.4, color=color)
    plt.xticks([])
    plt.yticks([])
Ejemplo n.º 8
0
def plot_confusion_matrix(cm, title='', cmap=plt.cm.Blues):
    #print cm
    #display vehicle, idle, walking accuracy respectively
    #display overall accuracy
    print type(cm)
   # plt.figure(index
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    #plt.figure("")
    plt.title("Confusion Matrix")
    plt.colorbar()
    tick_marks = [0,1,2]
    target_name = ["driving","idling","walking"]


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

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

    for i in range(0,3):
        for j in range(0,3):
         plt.text(i,j,str(cm[i,j]))
    plt.tight_layout()
    plt.ylabel("Actual Value")
    plt.xlabel("Predicted Outcome")
Ejemplo n.º 9
0
def hist_extraversion():
    '''
    外倾性分数的分布, 及其正态分布曲线
    :return:
    '''
    n_bins = 10
    data = pd.read_csv('data/regress_train_data.txt', sep=' ', header=None)
    ext = data[1]
    mu = ext.mean()
    sigma = ext.std()
    print(mu, sigma)
    fig = plt.figure(figsize=(10, 8))
    # --- for *.eps --- #
    fig.set_rasterized(True)
    # plt.title("The distribution of score on extraversion")
    plt.xlabel("$Score\ on\ extraversion$", fontsize=20)
    plt.ylabel("$Probability$", fontsize=20)
    plt.grid(True)
    plt.hist(ext, n_bins, normed=1, alpha=0.8, rwidth=0.85)
    x = np.linspace(0, 60, 100)
    y = mlab.normpdf(x, mu, sigma)
    plt.xlim(0, 60)
    plt.ylim(0, 0.055)
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.plot(x, y, 'r--')
    # plt.tight_layout()
    plt.savefig('figure/ext_dist.eps', dpi=300)
    plt.show()
Ejemplo n.º 10
0
def show_filters(weights,nweights,d1, d2, nrows, ncols, scale):
    """
    Plots the rows of NumPy 2D array ``weights`` as ``d1`` by ``d2`` images.

    The images are layed out in a ``nrows`` by ``ncols`` grid.

    Option ``scale`` sets the maximum absolute value of elements in ``weights``
    that will be plotted (larger values will be clamped to ``scale``, with the
    right sign).
    """
    perm = range(nweights)
    #random.shuffle(perm)
    image = -scale*numpy.ones((nrows*(d1+1)-1,ncols*(d2+1)-1),dtype=float)
    for i in range(nrows):
        for j in range(ncols):
            image[(i*d1+i):((i+1)*d1+i),(j*d2+j):((j+1)*d2+j)] = -1*weights[perm[i*ncols + j]].reshape(d1,d2)

    for i in range(nrows*(d1+1)-1):
        for j in range(ncols*(d2+1)-1):
            a = image[i,j]
            if a > scale:
                image[i,j] = scale
            if a < -scale:
                image[i,j] = -scale

    bordered_image = scale * numpy.ones((nrows*(d1+1)+1,ncols*(d2+1)+1),dtype=float)

    bordered_image[1:nrows*(d1+1),1:ncols*(d2+1)] = image

    imshow(bordered_image,cmap = cm.Greys,interpolation='nearest')
    xticks([])
    yticks([])
Ejemplo n.º 11
0
def plot_grid_experiment_results(grid_results, params, metrics):
    global plt
    params = sorted(params)
    grid_params = grid_results.grid_params
    plt.figure(figsize=(8, 6))
    for metric in metrics:
        grid_params_shape = [len(grid_params[k]) for k in sorted(grid_params.keys())]
        params_max_out = [(1 if k in params else 0) for k in sorted(grid_params.keys())]
        results = np.array([e.results.get(metric, 0) for e in grid_results.experiments])
        results = results.reshape(*grid_params_shape)
        for axis, included_in_params in enumerate(params_max_out):
            if not included_in_params:
                results = np.apply_along_axis(np.max, axis, results)

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

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

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

        if len(params) == 2:
            plt.xticks(np.arange(len(grid_params[params[1]])), grid_params[params[1]], rotation=45)
        plt.yticks(np.arange(len(grid_params[params[0]])), grid_params[params[0]])
        plt.colorbar()
        plt.show()
Ejemplo n.º 12
0
    def visualization2(self, sp_to_vis=None):
        if sp_to_vis:
            species_ready = list(set(sp_to_vis).intersection(self.all_sp_signatures.keys()))
        else:
            raise Exception('list of driver species must be defined')

        if not species_ready:
            raise Exception('None of the input species is a driver')

        for sp in species_ready:
            # Setting up figure
            plt.figure()
            plt.subplot(313)

            mon_val = OrderedDict()
            signature = self.all_sp_signatures[sp]
            for idx, mon in enumerate(list(set(signature))):
                if mon[0] == 'C':
                    mon_val[self.all_comb[sp][mon] + (-1,)] = idx
                else:
                    mon_val[self.all_comb[sp][mon]] = idx

            mon_rep = [0] * len(signature)
            for i, m in enumerate(signature):
                if m[0] == 'C':
                    mon_rep[i] = mon_val[self.all_comb[sp][m] + (-1,)]
                else:
                    mon_rep[i] = mon_val[self.all_comb[sp][m]]
            # mon_rep = [mon_val[self.all_comb[sp][m]] for m in signature]

            y_pos = numpy.arange(len(mon_val.keys()))
            plt.scatter(self.tspan[1:], mon_rep)
            plt.yticks(y_pos, mon_val.keys())
            plt.ylabel('Monomials', fontsize=16)
            plt.xlabel('Time(s)', fontsize=16)
            plt.xlim(0, self.tspan[-1])
            plt.ylim(0, max(y_pos))

            plt.subplot(312)

            for name in self.model.odes[sp].as_coefficients_dict():
                mon = name
                mon = mon.subs(self.param_values)
                var_to_study = [atom for atom in mon.atoms(sympy.Symbol)]
                arg_f1 = [numpy.maximum(self.mach_eps, self.y[str(va)][1:]) for va in var_to_study]
                f1 = sympy.lambdify(var_to_study, mon)
                mon_values = f1(*arg_f1)
                mon_name = str(name).partition('__')[2]
                plt.plot(self.tspan[1:], mon_values, label=mon_name)
            plt.ylabel('Rate(m/sec)', fontsize=16)
            plt.legend(bbox_to_anchor=(-0.1, 0.85), loc='upper right', ncol=1)

            plt.subplot(311)
            plt.plot(self.tspan[1:], self.y['__s%d' % sp][1:], label=parse_name(self.model.species[sp]))
            plt.ylabel('Molecules', fontsize=16)
            plt.legend(bbox_to_anchor=(-0.15, 0.85), loc='upper right', ncol=1)
            plt.suptitle('Tropicalization' + ' ' + str(self.model.species[sp]))

            # plt.show()
            plt.savefig('s%d' % sp + '.png', bbox_inches='tight', dpi=400)
Ejemplo n.º 13
0
def plot_runtime_results(results):
    plt.rcParams["figure.figsize"] = 7,7
    plt.rcParams["font.size"] = 22
    matplotlib.rc("xtick", labelsize=24)
    matplotlib.rc("ytick", labelsize=24)

    params = {"text.fontsize" : 32,
              "font.size" : 32,
              "legend.fontsize" : 30,
              "axes.labelsize" : 32,
              "text.usetex" : False
              }
    plt.rcParams.update(params)
    
    #plt.semilogx(results[:,0], results[:,3], 'r-x', lw=3)
    #plt.semilogx(results[:,0], results[:,1], 'g-D', lw=3)
    #plt.semilogx(results[:,0], results[:,2], 'b-s', lw=3)

    plt.plot(results[:,0], results[:,3], 'r-x', lw=3, ms=10)
    plt.plot(results[:,0], results[:,1], 'g-D', lw=3, ms=10)
    plt.plot(results[:,0], results[:,2], 'b-s', lw=3, ms=10)

    plt.legend(["Chain", "Tree", "FFT Tree"], loc="upper left")
    plt.xticks([1e5, 2e5, 3e5])
    plt.yticks([0, 60, 120, 180])

    plt.xlabel("Problem Size")
    plt.ylabel("Runtime (sec)")
    return results
Ejemplo n.º 14
0
def show_binary_images(samples, nsamples, d1, d2, nrows, ncols):
    """
    Plots samples in a NumPy 2D array ``samples`` as ``d1`` by ``d2`` images.
    (one sample per row of ``samples``).

    The samples are assumed to be images with binary pixels. The
    images are layed out in a ``nrows`` by ``ncols`` grid.
    """
    perm = range(nsamples)
    #random.shuffle(perm)
    if samples.shape[0] < nrows*ncols:
        samples_padded = numpy.zeros((nrows*ncols,samples.shape[1]))
        samples_padded[:samples.shape[0],:] = samples
        samples = samples_padded

    image = 0.5*numpy.ones((nrows*(d1+1)-1,ncols*(d2+1)-1),dtype=float)
    for i in range(nrows):
        for j in range(ncols):
            image[(i*d1+i):((i+1)*d1+i),(j*d2+j):((j+1)*d2+j)] = (1-samples[perm[i*ncols + j]].reshape(d1,d2))

    bordered_image = 0.5 * numpy.ones((nrows*(d1+1)+1,ncols*(d2+1)+1),dtype=float)

    bordered_image[1:nrows*(d1+1),1:ncols*(d2+1)] = image

    imshow(bordered_image,cmap = cm.Greys,interpolation='nearest')
    xticks([])
    yticks([])
Ejemplo n.º 15
0
def plotRocCurves(file_legend):
	pylab.clf()
	pylab.figure(1)
	pylab.xlabel('1 - Specificity', fontsize=12)
	pylab.ylabel('Sensitivity', fontsize=12)
	pylab.title("Need for Referral")
	pylab.grid(True, which='both')
	pylab.xticks([i/10.0 for i in range(1,11)])
	pylab.yticks([i/10.0 for i in range(0,11)])
	pylab.tick_params(axis="both", labelsize=15)

	for file, legend in file_legend:
		points = open(file,"rb").readlines()
		x = [float(p.split()[0]) for p in points]
		y = [float(p.split()[1]) for p in points]
		dev = [float(p.split()[2]) for p in points]
		x = [0.0] + x
		y = [0.0] + y
		dev = [0.0] + dev
	
		auc = np.trapz(y, x) * 100
		aucDev = np.trapz(dev, x) * 100

		pylab.grid()
		pylab.errorbar(x, y, yerr = dev, fmt='-')
		pylab.plot(x, y, '-', linewidth = 1.5, label = legend + u" (AUC = {0:0.1f}% \xb1 {1:0.1f}%)".format(auc,aucDev))

	pylab.legend(loc = 4, borderaxespad=0.4, prop={'size':12})
	pylab.savefig("referral/referral-curves.pdf", format='pdf')
Ejemplo n.º 16
0
def plot(W, idx2term):
    """
    Plot the interpretation of NMF basis vectors on Medlars data set. 
    
    :param W: Basis matrix of the fitted factorization model.
    :type W: `scipy.sparse.csr_matrix`
    :param idx2term: Index-to-term translator.
    :type idx2term: `dict`
    """
    print "Plotting highest weighted terms in basis vectors ..."
    for c in xrange(W.shape[1]):
        if sp.isspmatrix(W):
            top10 = sorted(enumerate(W[:, c].todense().ravel().tolist()[0]), key = itemgetter(1), reverse = True)[:10]
        else:
            top10 = sorted(enumerate(W[:, c].ravel().tolist()[0]), key = itemgetter(1), reverse = True)[:10]
        pos = np.arange(10) + .5
        val = zip(*top10)[1][::-1]
        plb.figure(c + 1)
        plb.barh(pos, val, color = "yellow", align = "center")
        plb.yticks(pos, [idx2term[idx] for idx in zip(*top10)[0]][::-1])
        plb.xlabel("Weight")
        plb.ylabel("Term")
        plb.title("Highest Weighted Terms in Basis Vector W%d" % (c + 1))
        plb.grid(True)
        plb.savefig("documents_basisW%d.png" % (c + 1), bbox_inches = "tight")
    print "... Finished."
Ejemplo n.º 17
0
def plotRocCurves(lesion, lesion_en):
	file_legend = []
	for techniqueMid in techniquesMid:
		for techniqueLow in techniquesLow:
			file_legend.append((directory + techniqueLow + "/" + techniqueMid + "/operating-points-" + lesion + "-scale.dat", "Low-level: " + techniqueLow + ". Mid-level: " + techniqueMid + "."))
			
			pylab.clf()
			pylab.figure(1)
			pylab.xlabel('1 - Specificity', fontsize=12)
			pylab.ylabel('Sensitivity', fontsize=12)
			pylab.title(lesion_en)
			pylab.grid(True, which='both')
			pylab.xticks([i/10.0 for i in range(1,11)])
			pylab.yticks([i/10.0 for i in range(0,11)])
			#pylab.tick_params(axis="both", labelsize=15)
			
			for file, legend in file_legend:
				points = open(file,"rb").readlines()
				x = [float(p.split()[0]) for p in points]
				y = [float(p.split()[1]) for p in points]
				x.append(0.0)
				y.append(0.0)
				
				auc = numpy.trapz(y, x) * -100

				pylab.grid()
				pylab.plot(x, y, '-', linewidth = 1.5, label = legend + u" (AUC = {0:0.1f}%)".format(auc))

	pylab.legend(loc = 4, borderaxespad=0.4, prop={'size':12})
	pylab.savefig(directory + "plots/" + lesion + ".pdf", format='pdf')
Ejemplo n.º 18
0
def plot(frame,dirname,clim=None,axis_limits=None):
    if not os.path.exists('./figures'):
        os.makedirs('./figures')
        
    try:
        sol=Solution(frame,file_format='petsc',read_aux=False,path='./saved_data/'+dirname+'/_p/',file_prefix='claw_p')
    except IOError:
        'Data file not found; please unzip the files in saved_data/.'
        return
    x=sol.state.grid.x.centers; y=sol.state.grid.y.centers
    mx=len(x); my=len(y)
    
    mp=sol.state.num_eqn    
    yy,xx = np.meshgrid(y,x)

    p=sol.state.q[0,:,:]
    if clim is not None:
        pl.pcolormesh(xx,yy,p,cmap=cm.RdBu_r)
    else:
        pl.pcolormesh(xx,yy,p,cmap=cm.Reds)
    pl.title("t= "+str(sol.state.t),fontsize=20)
    pl.xticks(size=20); pl.yticks(size=20)
    cb = pl.colorbar();

    if clim is not None:
        pl.clim(clim[0],clim[1]);
    imaxes = pl.gca(); pl.axes(cb.ax)
    pl.yticks(fontsize=20); pl.axes(imaxes)
    pl.axis('equal')
    if axis_limits is None:
        pl.axis([np.min(x),np.max(x),np.min(y),np.max(y)])
    else:
        pl.axis([axis_limits[0],axis_limits[1],axis_limits[2],axis_limits[3]])
    pl.savefig('./figures/'+dirname+'.png')
    pl.close()
Ejemplo n.º 19
0
	def STAplot(self, option = 0):
		try:
			self.Files.OpenDatabase(self.NAME + '.h5')
			STA_TIME = self.Files.QueryDatabase('STA_Analysis', 'STA_TIME')[0]
			STA_Current = self.Files.QueryDatabase('STA_Analysis', 'STAstim')
			INTSTEP = self.Files.QueryDatabase('DataProcessing', 'INTSTEP')[0][0]
		except:
			print 'Sorry no data found'
		
		X = np.arange(-STA_TIME / INTSTEP, STA_TIME / INTSTEP, dtype=float) * INTSTEP
		
		if option == 1:
			fig = plt.figure()
			ax = fig.add_subplot(111)
			ax.plot(X[0:(STA_TIME/INTSTEP)],STA_Current[0:(STA_TIME/INTSTEP)], 
						linewidth=3, color='k')
			ax.plot(np.arange(-190,-170),np.ones(20)*0.35, linewidth=5,color='k')
			ax.plot(np.ones(200)*-170,np.arange(0.35,0.549,0.001),linewidth=5,color='k')
			ax.plot(np.arange(-200,0),np.zeros(200), 'k--', linewidth=2)
			plt.axis('off')
			plt.show()
			
		
		if option == 0:
			fig = plt.figure(figsize=(12,8))
			ax = fig.add_subplot(111)
			ax.plot(X[0:(STA_TIME / INTSTEP) + 50], STA_Current[0:(STA_TIME / INTSTEP) + 50],
						linewidth=3, color='k')
			plt.xticks(fontsize = 20)
			plt.yticks(fontsize = 20)
			plt.ylabel('current(pA)', fontsize = 20)
			plt.legend(('data'), loc='upper right')
			plt.show()
Ejemplo n.º 20
0
def plot_ea(frame1, filt_df, dst_path, uplift_rate, riv_case):
    f = plt.figure()
    ax = filt_df.plot(x='ApatiteHeAge', y='Elevation', style='o-', ax=f.gca())

    plt.title('Age-Elevation')
    plt.xlabel('ApatiteHeAge [Ma]')
    plt.ylabel('Elevation [Km]')

    #tread line
    sup_age, slope, r_square = find_max_treadline(filt_df, uplift_rate * np.sin(np.deg2rad(60)), riv_case)
    x = filt_df[filt_df['ApatiteHeAge'] < sup_age]['ApatiteHeAge']
    y = filt_df[filt_df['ApatiteHeAge'] < sup_age]['Points:2']
    z = np.polyfit(x, y, 1)
    p = np.poly1d(z)

    # plt.legend(point_lables, loc='best', fontsize=10)
    # n = np.linspace(min(frame1[frame1['Points:2'] > min(frame1['Points:2'])]['ApatiteHeAge']), max(frame1['ApatiteHeAge']), 21)
    n = np.linspace(min(filt_df[filt_df['Points:2'] >= min(filt_df['Points:2'])]['ApatiteHeAge']), max(filt_df['ApatiteHeAge']), 21)
    plt.plot(n, p(n) - min(frame1['Points:2']),'-r')
    ax.text(np.mean(n), np.mean(p(n) - min(frame1['Points:2'])), 'y=%.6fx + b'%(z[0]), fontsize = 20)

    txs = np.linspace(np.round(min(filt_df['Elevation'])), np.ceil(max(filt_df['Elevation'])), 11)
    lebs = ['0'] + [str(i) for i in txs[1:]]
    plt.yticks(txs, list(reversed(lebs)))
    plt.savefig(dst_path)
    plt.close()
    return z[0]
Ejemplo n.º 21
0
def plot_p_leading_order(frame):
    mat = scipy.io.loadmat('sound-speed_2D-wave.mat')
    T=5; nt=T/0.5
    pp=mat['U'][nt,:,:]
    xx=mat['xx']
    yy=mat['yy']

    fig=pl.figure(figsize=(8, 3.5))
    #pl.title("t= "+str(sol.state.t),fontsize=20)
    pl.xticks(size=20); pl.yticks(size=20)
    pl.xlabel('x',fontsize=20); pl.ylabel('y',fontsize=20)
    #pl.pcolormesh(xx,yy,p_subxy,cmap=cm.OrRd)
    pl.pcolormesh(xx,yy,pp,cmap='RdBu_r')
    pl.autoscale(tight=True)
    cb = pl.colorbar(ticks=[0.5,1,1.5,2]);
    
    #pl.clim(ticks=[0.5,1,1.5,2])
    imaxes = pl.gca(); pl.axes(cb.ax)
    pl.yticks(fontsize=20); pl.axes(imaxes)
    #pl.xticks(fontsize=20); pl.axes(imaxes)
    #pl.axis('equal')
    pl.axis('tight')
    fig.tight_layout()
    pl.savefig('./_plots_to_paper/sound-speed_LO_t'+str(frame)+'_pcolor.png')
    pl.close()
Ejemplo n.º 22
0
	def PSTH(self):
	
			
		TimeRes = np.array([0.1,0.25,0.5,1,2.5,5.0,10.0,25.0,50.0,100.0])

		Projection_PSTH = np.zeros((2,len(TimeRes)))
		for i in range(0,len(TimeRes)):
			Data_Hist,STA_Hist,Model_Hist,B = Hist(TimeRes[i])
			data = Data_Hist/np.linalg.norm(Data_Hist)
			sta = STA_Hist/np.linalg.norm(STA_Hist)
			model = Model_Hist/np.linalg.norm(Model_Hist)
			Projection_PSTH[0,i] = np.dot(data,sta)
			Projection_PSTH[1,i] = np.dot(data,model)
			
		import matplotlib.font_manager as fm
		
		plt.figure()
		plt.semilogx(TimeRes,Projection_PSTH[0,:],'gray',TimeRes,Projection_PSTH[1,:],'k',
			     linewidth=3, marker='o', markersize = 12)
		plt.xlabel('Time Resolution, ms',fontsize=25)
		plt.xticks(fontsize=25)
		#plt.axis["right"].set_visible(False)
		plt.ylabel('Projection onto PSTH',fontsize=25)
		plt.yticks(fontsize=25)
		prop = fm.FontProperties(size=20)
		plt.legend(('1D model','2D model'),loc='upper left',prop=prop)
		plt.tight_layout()
		plt.show()
Ejemplo n.º 23
0
def plotDist(subplot, X, Y, label):
    pylab.grid()
    pylab.subplot(subplot)
    pylab.bar(X, Y, 0.05)
    pylab.ylabel(label)
    pylab.xticks(arange(len(X)), X)
    pylab.yticks(arange(0,1,0.1))
Ejemplo n.º 24
0
def plot_p(frame):
    sol=Solution(frame,file_format='petsc',read_aux=False,path='./_output/_p/',file_prefix='claw_p')
    x=sol.state.grid.x.centers; y=sol.state.grid.y.centers
    mx=len(x); my=len(y)
    
    mp=sol.state.num_eqn    
    yy,xx = np.meshgrid(y,x)

    p=sol.state.q[0,:,:]
    fig = pl.figure(figsize=(8, 3.5))
    #pl.title("t= "+str(sol.state.t),fontsize=20)
    pl.xticks(size=20); pl.yticks(size=20)
    pl.xlabel('x',fontsize=20); pl.ylabel('y',fontsize=20)
    #pl.pcolormesh(xx,yy,p_subxy,cmap=cm.OrRd)
    pl.pcolormesh(xx,yy,p,cmap='RdBu_r')
    pl.autoscale(tight=True)
    cb = pl.colorbar(ticks=[0.5,1,1.5,2]);
    
    #pl.clim(ticks=[0.5,1,1.5,2])
    imaxes = pl.gca(); pl.axes(cb.ax)
    pl.yticks(fontsize=20); pl.axes(imaxes)
    #pl.xticks(fontsize=20); pl.axes(imaxes)
    #pl.axis('equal')
    pl.axis('tight')
    fig.tight_layout()
    pl.savefig('./_plots_to_paper/sound-speed_FV_t'+str(frame)+'_pcolor.png')
    pl.close()
Ejemplo n.º 25
0
def plot_hourly_comparisions(s1,s2,s3,s4,s5,s6,s7,s8):
    stages_array=[]    
    stages_array.append(s1)
    stages_array.append(s2)
    stages_array.append(s3)
    stages_array.append(s4)
    stages_array.append(s5)
    stages_array.append(s6)
    stages_array.append(s7)
    stages_array.append(s8)
    i=1
    for i in range(1,11):    
        initial_epoch_value=(i-1)*120
        final_epoch_value=((i-1)*120)+119
        #final_epoch_value2=((i-1)*120)+65
        stages_hourly=stages_array[6][initial_epoch_value:final_epoch_value]
        stages_hourly2=stages_array[7][initial_epoch_value:final_epoch_value]
        #stages_hourly3=stages_array[5][initial_epoch_value:final_epoch_value]
        #stages_hourly4=stages_array[7][initial_epoch_value:final_epoch_value]
        z=len(stages_hourly)
        plt.figure()
        time= np.arange(1,z+1)
        plt.plot(time, stages_hourly,drawstyle='steps',label='Good Sleep')
        plt.plot(time, stages_hourly2,drawstyle='steps',label='Bad Sleep')
        #plt.plot(time, stages_hourly3,drawstyle='steps',label='Subject3')
        #plt.plot(time, stages_hourly4,drawstyle='steps',label='Subject4')
        plt.title('Subject4- Comparision - Hour'+' '+str(i))
        plt.xlabel('Epochs')
        plt.ylabel('Stages')
        plt.yticks(np.arange(-1,9))
        plt.legend(bbox_to_anchor=(0.65, 1), loc=2, borderaxespad=0.)
        plt.show()
        plt.savefig('hour-'+str(i))
Ejemplo n.º 26
0
def plmyfig(df, bgname, dirname, tar, count=10):
    #plot fig!
    print("Starting Plot %s %s" % (dirname, bgname))
    if len(df) > count:
        df = df.head(count)
    pos = plt.arange(len(df)) + 0.5
    ytick = _getTerm(df['Term_description'], df['Term_ID'], bgname)
    xs = [float(n) for n in df[' -log10(pvalue)']]
    ytick.reverse()
    xs.reverse()
    plt.barh(pos, xs, align = 'center', height = 0.5, alpha = 1, color='orange')
    plt.yticks(pos, ytick, size = 'x-small')
    plt.xlabel('$-Log10(pValue)$')
    plt.title('%s' % bgname)
    ax = plt.gca()
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('bottom')
    try:
        plt.tight_layout()
    except ValueError:
        pass
    filename = os.path.join(tar, dirname, dirname + '_' + bgname)
    plt.savefig(filename + '.png', dpi = 72)
    plt.savefig(filename + '.pdf')
    plt.close()
Ejemplo n.º 27
0
def Iris_network(ant, data):
    #G = nx.watts_strogatz_graph(100,3,0.6)
    #G = nx.cubical_graph()
    G = nx.Graph() #無向グラフ

    tmp1 = []
    tmp2 = []
    tmp3 = []
    for i in range(len(data)):
        if data[i][4] == 'setosa':
            tmp1.append(str(i))
        elif data[i][4] == 'versicolor':
            tmp2.append(str(i))
        elif data[i][4] == 'virginica':
            tmp3.append(str(i))

    for i in range(len(data)):
        if len(ant[i].parent) == 0 : pass
        else:
            dest = ant[i].parent[0]
            #G.add_edge(str(ant[i].data), str(ant[dest].data))
            G.add_edge(str(ant[i].Id), str(ant[dest].Id))

    pos = nx.spring_layout(G)

    nx.draw_networkx_nodes(G, pos, nodelist=tmp1, node_size=30, node_color="r")
    nx.draw_networkx_nodes(G, pos, nodelist=tmp2, node_size=30, node_color="w")
    nx.draw_networkx_nodes(G, pos, nodelist=tmp3, node_size=30, node_color="w")
    nx.draw_networkx_edges(G, pos, width=1)
    #nx.draw_networkx_labels(G, pos, font_size=10, font_color="b")
    plt.xticks([])
    plt.yticks([])
    plt.show()
Ejemplo n.º 28
0
def ca_box_plot_shopping():
    # 读取数据
    data1 = pd.read_csv('data/split_class/large_IGNORE_425_shopping_+1.txt', sep=' ', header=None)
    data2 = pd.read_csv('data/split_class/large_IGNORE_425_shopping_-1.txt', sep=' ', header=None)
    col1 = data1[2] / data1[1]
    col2 = data2[2] / data2[1]
    # print(col1.describe())
    # print(col2.describe())
    # col1.to_csv("shopping_+1.txt")
    # col2.to_csv("shopping_-1.txt")
    plt.figure(figsize=(8, 4))
    sns.boxplot(data=[col1, col2], fliersize=0.1, width=0.3)
    # sns.violinplot(data=[col1, col2], fliersize=0.1, width=0.3)

    plt.xticks((0, 1), ('Extroverts', 'Introverts'), fontsize=20)
    # plt.xlim(0.5, 2.5)

    plt.yticks(fontsize=20)
    plt.ylabel("Purchasing Index", fontsize=20)
    plt.ylim(0, 0.12)

    # plt.boxplot(data=[col1, col2], vert=False, sym='k+', showmeans=True, showfliers=True, notch=1)
    # plt.yticks((1, 2), ('Extroverts', 'Introverts'), fontsize=25, rotation=30)
    # plt.ylim(0.5, 2.5)
    #
    # plt.xticks(fontsize=30)
    # plt.xlabel("Purchasing Index", fontsize=30)
    # plt.xlim(0, 0.12)
    plt.savefig('figure/purchase_box.eps', dpi=300)
    plt.show()
Ejemplo n.º 29
0
def minj_mflux(*args,**kwargs):
    Qu = jana.quantities()
    Minj_List =[]
    Minj_MHD_List=[]
    for i in range(len(args[1])):
        Minj_List.append(Qu.Mflux(args[1][i],Mstar=args[0][i],scale=True)['Mfr']+Qu.Mflux(args[1][i],Mstar=args[0][i],scale=True)['Mfz'])
        
    MHD_30minj = Qu.Mflux(args[2],Mstar=30.0,scale=True)['Mfr']+Qu.Mflux(args[2],Mstar=30.0,scale=True)['Mfz']
    for i in args[0]:
        Minj_MHD_List.append(MHD_30minj*np.sqrt(i/30.0))
    
    f1 = plt.figure(num=1)
    ax1 = f1.add_subplot(211)
    plt.axis([10.0,70.0,2.0e-5,7.99e-5])
    plt.plot(args[0],Minj_MHD_List,'k*')
    plt.plot(args[0],Minj_List,'ko')
    plt.minorticks_on()
    locs,labels = plt.yticks()
    plt.yticks(locs, map(lambda x: "%.1f" % x, locs*1e5))
    plt.text(0.0, 1.03, r'$10^{-5}$', transform = plt.gca().transAxes)
    plt.xlabel(r'Stellar Mass : $M_{*} [M_{\odot}]$')
    plt.ylabel(r' $\dot{M}_{\rm vert} + \dot{M}_{\rm rad}\, [M_{\odot}\,\rm yr^{-1}]$')
    
    ax2 = f1.add_subplot(212)
    plt.axis([10.0,70.0,0.0,50.0])
    plt.plot(args[0],100*((np.array(Minj_List)-np.array(Minj_MHD_List))/np.array(Minj_MHD_List)),'k^')
    plt.minorticks_on()
    plt.xlabel(r'Stellar Mass : $M_{*} [M_{\odot}]$')
    plt.ylabel(r'$\%$ Change in Total Mass Outflow Rates')
Ejemplo n.º 30
0
def plot_svc(X, y, mysvc, bounds=None, grid=50):
    if bounds is None:
        xmin = np.min(X[:, 0], 0)
        xmax = np.max(X[:, 0], 0)
        ymin = np.min(X[:, 1], 0)
        ymax = np.max(X[:, 1], 0)
    else:
        xmin, ymin = bounds[0], bounds[0]
        xmax, ymax = bounds[1], bounds[1]
    aspect_ratio = (xmax - xmin) / (ymax - ymin)
    xgrid, ygrid = np.meshgrid(np.linspace(xmin, xmax, grid),
                              np.linspace(ymin, ymax, grid))
    plt.gca(aspect=aspect_ratio)
    plt.xlim(xmin, xmax)
    plt.ylim(ymin, ymax)
    plt.xticks([])
    plt.yticks([])
    plt.hold(True)
    plt.plot(X[y == 1, 0], X[y == 1, 1], 'bo')
    plt.plot(X[y == -1, 0], X[y == -1, 1], 'ro')
    
    box_xy = np.append(xgrid.reshape(xgrid.size, 1), ygrid.reshape(ygrid.size, 1), 1)
    if mysvc is not None:
        scores = mysvc.decision_function(box_xy)
    else:
        print 'You must have a valid SVC object.'
        return None;
    
    CS=plt.contourf(xgrid, ygrid, scores.reshape(xgrid.shape), alpha=0.5, cmap='jet_r')
    plt.contour(xgrid, ygrid, scores.reshape(xgrid.shape), levels=[0], colors='k', linestyles='solid', linewidths=1.5)
    plt.contour(xgrid, ygrid, scores.reshape(xgrid.shape), levels=[-1,1], colors='k', linestyles='dashed', linewidths=1)
    plt.plot(mysvc.support_vectors_[:,0], mysvc.support_vectors_[:,1], 'ko', markerfacecolor='none', markersize=10)
    CB = plt.colorbar(CS)
Ejemplo n.º 31
0
# transport source samples onto target samples
transp_Xs_emd = ot_emd.transform(Xs=Xs)
transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs)
transp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs)


##############################################################################
# Fig 1 : plots source and target samples + matrix of pairwise distance
# ---------------------------------------------------------------------

pl.figure(1, figsize=(10, 10))
pl.subplot(2, 2, 1)
pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples')
pl.xticks([])
pl.yticks([])
pl.legend(loc=0)
pl.title('Source  samples')

pl.subplot(2, 2, 2)
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples')
pl.xticks([])
pl.yticks([])
pl.legend(loc=0)
pl.title('Target samples')

pl.subplot(2, 2, 3)
pl.imshow(M, interpolation='nearest')
pl.xticks([])
pl.yticks([])
pl.title('Matrix of pairwise distances')
Ejemplo n.º 32
0
    sns.set_context('poster')

    plt.subplots(figsize=(20,8))
    #make 5000 noise and 1000 of each x sample
    N_samples=1000
    noise=np.random.randn(5*N_samples, noise_dim).astype('float32')
    x_gen=np.repeat(xgen,1000)
    x_gen=x_gen.reshape(5000,1)
    #plug into posterior
    z_samples=posterior(x_gen,noise)
    z_samples=tf.reshape(z_samples,[xgen.shape[0], N_samples, 2]).eval()
    for i in range(5):
       plt.subplot(2,5,i+1)
       sns.kdeplot(z_samples[i,:,0], z_samples[i,:,1], cmap='Greens')
       plt.axis('square')
       plt.title('q(z|x={})'.format(y[i]))
       plt.xlim([xmin,xmax])
       plt.ylim([xmin,xmax])
       plt.xticks([])
       plt.yticks([]);
       plt.subplot(2,5,5+i+1)
       plt.contour(xrange, xrange, np.exp(logprior+llh[i]).reshape(300,300).T, cmap='Greens')
       plt.axis('square')
       plt.title('p(z|x={})'.format(y[i]))
       plt.xlim([xmin,xmax])
       plt.ylim([xmin,xmax])
       plt.xticks([])
       plt.yticks([])
    plt.savefig('JCADVexp Final Fig')
    plt.close()
def prepare_model(i, label, closep1, tr_date, ts_date, x_train, x_test,
                  y_train, y_test):
    #tr_date, ts_date, x_train, x_test, y_train, y_test = prepare_data()
    #ts_date = timestamp_to_dataconv(ts_date)
    ts_date = pd.to_datetime(ts_date, unit='s')
    print(x_train.shape, y_train.shape, y_test.shape, y_test.shape)
    print(x_train)
    '''
    print(len(X_train),len(Y_train),len(X_test),len(Y_test))
    '''
    try:
        final_model = Sequential()
        final_model.add(Dense(512, input_shape=(len(x_train[0]), )))
        #final_model.add(Dense(11))
        final_model.add(Dense(1))
        # summary of model
        print("summaryof model: ", final_model.summary())
        # plot graph
        # plot_model(final_model, to_file="DeepNeuralnetwork.png")

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

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

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

    print("mse:  ", mseA)
    print("rmse: ", rmsefA)
    print("mae: ", maefA)
    print("mape: ", mape1A)
    print("r2score: ", r_scoreA)
    #print("predicted", predicted, "actual", original)
    #print("predicted1: ", pred1, "actual1: ", actual1)

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

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

    plt.figure(1)
    plt.subplot(211)
    # plt.subplot(221)
    plt.xticks(rotation=30)
    plt.plot(ts_date, actual1, label="actual1", color='green')
    plt.plot(ts_date, pred1, label='predicted1', color='red')
    plt.grid(True)
    # plt.plot(Y_train,label ='y_train',color='yellow')
    # plt.plot(Y_test, label='y_test', color='pink')
    plt.title("(FeedFNN) " + str(i) + str(label), fontweight='bold')
    plt.legend()
    plt.xlabel("Time(s)")
    plt.ylabel("Price of Bitcoin")
    plt.subplots_adjust(hspace=0.4, wspace=0.4)

    plt.subplot(223)
    plt.bar(
        y_pos,
        error_values,
        width,
        align='center',
        alpha=0.5,
        color='red',
    )
    plt.xticks(y_pos, errors_label)
    for a, b in zip(y_pos, error_values):
        plt.text(a, b, str(b))
        # plt.annotate(str(b),y_poserror_values=(a,b))
    plt.title('Evaluation Criteria', fontweight='bold')
    plt.xlabel('Errors')
    plt.ylabel('Values')
    plt.subplot(224)
    # plt.subplot(222)
    # plt.subplot(212)
    plt.title("Architecture", fontsize=14)
    # plt.text(.2,.6,'activation function')
    plt.yticks(np.arange(len(tests)) * -1)
    for i, s in enumerate(tests):
        plt.text(0.1,
                 -i / 2,
                 s,
                 fontsize=12,
                 horizontalalignment='left',
                 backgroundcolor='palegreen',
                 wrap=True)
    plt.subplots_adjust(hspace=0.5, wspace=0.6)

    plt.show()
    #plt.pause(0.01)
    #time.sleep(0.5)
    plt.close()
    return ts_date, actual1, pred1
Ejemplo n.º 34
0
def main(args):
    print("*" * 50)
    print("-" * 20, "Version:{}".format(args.version), "-" * 20)
    print("*" * 50)

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

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

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

    plt.figure()
    plt.subplot(231), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_full, cmap="gray"), plt.title("image_full")
    plt.subplot(232), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_sparse, cmap="gray"), plt.title("image_sparse")
    plt.subplot(233), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_pred, cmap="gray"), plt.title("image_pred")
    plt.subplot(234), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_true, cmap="gray"), plt.title("image_true")
    plt.subplot(235), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_full - image_sparse, cmap="gray"), plt.title("Res image_sparse")
    plt.subplot(236), plt.xticks([]), plt.yticks([]), plt.imshow(
        image_full - image_pred, cmap="gray"), plt.title("Res image_pred")
    plt.show()
    """
    ***********************************************************************************************************
    量化指标
    ***********************************************************************************************************
    """
    ssim_0, mse_0, psnr_0 = ssim_mse_psnr(image_full, image_sparse)
    ssim_1, mse_1, psnr_1 = ssim_mse_psnr(image_full, image_pred)
    print("Sparse Image--> SSIM:{}, MSE:{}, PSNR:{}".format(
        ssim_0, mse_0, psnr_0))
    print("Pred Image--> SSIM:{}, MSE:{}, PSNR:{}".format(
        ssim_1, mse_1, psnr_1))
Ejemplo n.º 35
0
        # add intrinsic tipping points
        plt.plot(np.zeros(len(value_c1)) + c_0_crit,
                 value_c1,
                 '--',
                 color='black',
                 linewidth=1)
        plt.plot(value_c0,
                 np.zeros(len(value_c0)) + c_1_crit,
                 '--',
                 color='black',
                 linewidth=1)

        # axis labels/ ticks and other properties
        plt.xticks(())
        plt.yticks(())

        if count_loop > (
            (value_d1.shape[0] * value_d0.shape[0]) - value_d0.shape[0]):
            plt.xticks(
                major_xticks, fontsize=15
            )  # fontsize = 15 for single, fontsize = 10 for multiple
            plt.xlabel(r"$c_1$" "\n" r"$d_{21} = %s$" % d_0, fontsize=15)

        if count_loop == (1 + (ya * value_d0.shape[0])):
            plt.yticks(major_yticks, fontsize=15)
            plt.ylabel(r"$d_{12} = %s$" "\n" r"$c_2$" % d_1, fontsize=15)
            ya = ya + 1

        plt.gca().set_aspect('equal', adjustable='box')
        for spine in plt.gca().spines.values():
Ejemplo n.º 36
0
    ylabels = ['x', 'y', 'z']
    for dim in [0, 1, 2]:
        pylab.subplots(nrows=N, ncols=1, figsize=(6, 4))
        for n in range(N):
            start = doc_range[n]
            stop = doc_range[n + 1]
            X_n = X[start:stop]
            Z_n = Z[start:stop]

            pylab.subplot(N, 1, n + 1)
            pylab.hold('on')
            for k in range(K):
                Z_n_eq_k = np.flatnonzero(Z_n == k)
                pylab.plot(Z_n_eq_k, X_n[Z_n_eq_k, dim], '.', color=Colors[k])
            pylab.ylim([-2, 2])
            pylab.yticks([-1, 1])
            if n == 0:
                pylab.title(ylabels[dim])
            if n < N - 1:
                pylab.xticks([])
            else:
                pylab.xticks([200, 400, 600, 800])
        pylab.subplots_adjust(bottom=0.14)
        pylab.savefig('DataIllustration-ToyARK13-%s.eps' % (ylabels[dim]),
                      bbox_inches='tight', pad_inches=0)

    N = np.zeros(K)
    for k in range(K):
        N[k] = np.sum(Z == k)
    # print ['%4d ' % (N[k]) for k in xrange(K)]
    pylab.show(block=True)
Ejemplo n.º 37
0
def plot_transcripts(tx_start,
                     transcripts,
                     graph_coords,
                     reverse_minus,
                     font_size,
                     show_gene=False,
                     distance_ratio=0.3):
    """
    [original description]
    draw the gene structure.

    [now]
    due to i changed the mrna class, therefore, this function need be modified

    :param tx_start: the very start of this plot
    :param graph_coords: numpy array, convert the coord of genome to the coord in this plot
    :param reverse_minus:
    :param transcripts: list of Transcript
    :param font_size: the font size of transcript label
    :param show_gene: Boolean value to decide whether to show gene id in this plot
    :param distance_ratio: distance between transcript label and transcript line
    """
    y_loc = 0
    exon_width = .3
    """
    @2018.12.26
    Maybe I'm too stupid for this, using 30% of total length of x axis as the gap between text with axis
    """
    distance = distance_ratio * (max(graph_coords) - min(graph_coords))

    # @2018.12.19
    # @2018.12.21
    # the API of SpliceRegion has changed, the transcripts here should be sorted

    for transcript in transcripts:
        # narrows = math.floor(narrows * (transcript.length / len(graphcoords)))

        # @2018.12.20 add transcript id, based on fixed coordinates
        if show_gene:
            pylab.text(x=-1 * distance,
                       y=y_loc + 0.15,
                       s=transcript.gene,
                       fontsize=font_size)

            pylab.text(x=-1 * distance,
                       y=y_loc - 0.25,
                       s=transcript.transcript,
                       fontsize=font_size)
        else:
            pylab.text(x=-1 * distance,
                       y=y_loc - 0.1,
                       s=transcript.transcript,
                       fontsize=font_size)

        strand = "+"
        # @2018.12.19
        # s and e is the start and end site of single exon
        for exon in transcript.exons:
            s, e, strand = exon.start, exon.end, exon.strand
            s = s - tx_start
            e = e - tx_start
            x = [
                graph_coords[s], graph_coords[e], graph_coords[e],
                graph_coords[s]
            ]
            y = [
                y_loc - exon_width / 2, y_loc - exon_width / 2,
                y_loc + exon_width / 2, y_loc + exon_width / 2
            ]
            pylab.fill(x, y, 'k', lw=.5, zorder=20)

        # @2018.12.21
        # change the intron range
        # Draw intron.
        intron_sites = [
            graph_coords[transcript.start - tx_start],
            graph_coords[transcript.end - tx_start]
        ]
        pylab.plot(intron_sites, [y_loc, y_loc], color='k', lw=0.5)

        # @2018.12.23 fix intron arrows issues
        # Draw intron arrows.
        max_ = graph_coords[transcript.end - tx_start]
        min_ = graph_coords[transcript.start - tx_start]
        length = max_ - min_
        narrows = math.ceil(length / max(graph_coords) * 50)

        spread = .2 * length / narrows

        for i in range(narrows):
            loc = float(i) * length / narrows + graph_coords[transcript.start -
                                                             tx_start]
            if strand == '+' or reverse_minus:
                x = [loc - spread, loc, loc - spread]
            else:
                x = [loc + spread, loc, loc + spread]
            y = [y_loc - exon_width / 5, y_loc, y_loc + exon_width / 5]
            pylab.plot(x, y, lw=.5, color='k')

        y_loc += 1

    pylab.xlim(0, max(graph_coords))
    pylab.ylim(-.5, len(transcripts) + .5)
    pylab.box(on=False)
    pylab.xticks([])
    pylab.yticks([])
Ejemplo n.º 38
0
paramaters = ('Detonation velocity  [m/s]', 'Temperature [K]',
              'Pressure [atm]')
for ax, color, i, par in zip(axes, colors, number, paramaters):
    ax.set_ylabel('%s' % par, color=color)
    ax.tick_params(axis='y', colors=color)
    ax.plot(fi, cj_speed_Phi[:, :], linestyle='-', linewidth=0.7, color=color)
axes[0].set_xlabel('$\Phi$ [-]')
axes[0].xaxis.set_ticks(np.arange(fimin - 0.2, fimax + 1, 1))
axes[0].set_yticks(
    np.linspace(axes[0].get_yticks()[0], axes[0].get_yticks()[-1],
                len(axes[0].get_yticks())))
axes[1].set_yticks(
    np.linspace(axes[1].get_yticks()[0], axes[1].get_yticks()[-1],
                len(axes[0].get_yticks())))
plt.tight_layout()
plt.savefig('phi_plo.pdf')

plt.plot(fi, cj_speed_Phi[:, 1] / 300, linestyle='-', color='black')
plt.yticks(np.arange(0, 17, 1))
plt.ylabel('$T_2/T_1$')
plt.xlabel('$\Phi$')
plt.tight_layout()
plt.savefig('porownanie_temp.pdf')

plt.plot(fi, cj_speed_Phi[:, 2], linestyle='-', color='black')
plt.ylabel('$P_2/P_1$')
plt.xlabel('$\Phi$')
plt.yticks(np.arange(0, 70, 10))
plt.tight_layout()
plt.savefig('porownanie_pres.pdf')
Ejemplo n.º 39
0
Here is the number of events associated with the negative class:

no_CME_data = negative_result[0]
negative_class = negative_result[1]
print("There are", len(no_CME_data), "no-CME events in the negative class.")

## Step 3: Feature selection

Some of the features within a data set may be powerful for distinguishing between the positive and negative class, whereas others may be redundant or irrelevant. To identify features in the former category, we use a univariate feature selection method, which is implemented in the feature selection module of the scikit-learn library, for feature scoring.

To improve the performance of the feature selection algorithm, we'll normalize each feature so that they lie within similar ranges. To do this, we subtract from every feature its median value and divide by its standard deviation.

CME_data = np.array(CME_data)
no_CME_data = np.array(no_CME_data)

def normalize_the_data(flare_data):
    flare_data = np.array(flare_data)
    n_elements = flare_data.shape[0]
    for j in range(flare_data.shape[1]):
        standard_deviation_of_this_feature = np.std(flare_data[:, j])
        median_of_this_feature = np.median(flare_data[:, j])
        for i in range(n_elements):
            flare_data[i, j] = (
                flare_data[i, j] - median_of_this_feature) / (standard_deviation_of_this_feature)
    return flare_data


no_CME_data = normalize_the_data(no_CME_data)
CME_data = normalize_the_data(CME_data)

print("There are", no_CME_data.shape[0], "flares with no associated CMEs.")
print("There are", CME_data.shape[0], "flares with associated CMEs.")

Let's look at the distribution of one feature for the active regions that both flared and produced a CME (green) and for the active regions that flared but did not produce a CME (red). You can change the value of `i` in the code block below to see that some features are totally useless as there is barely any difference in the distributions for the positive and negative class. As such, we can throw such features out of our sample. It's a good idea to do some feature selection before running the SVM, so as to reduce noise (in this case, with only 18 features, there's not too much noise to begin with). 

sharps = ['Total unsigned flux', 'Mean gradient of total field',
          'Mean current helicity (Bz contribution)', 'Mean photospheric magnetic free energy',
          'Fraction of Area with Shear > 45 deg', 'Total unsigned current helicity',
          'Mean gradient of horizontal field', 'Mean characteristic twist parameter, alpha',
          'Mean angle of field from radial', 'Mean gradient of vertical field',
          'Mean vertical current density', 'Total unsigned vertical current',
          'Sum of the modulus of the net current per polarity',
          'Total photospheric magnetic free energy density', 'Mean shear angle',
          'Area of strong field pixels in the active region', 'Sum of flux near polarity inversion line',
          'Absolute value of the net current helicity']

i = 2

# For the positive class (green)
mu_fl = np.mean(CME_data[:, i])
sigma_fl = np.std(CME_data[:, i])
num_bins = 15
n_fl, bins_fl, patches_fl = plt.hist(
    CME_data[:, i], num_bins, normed=1, facecolor='green', alpha=0.5)
y_fl = scipy.stats.norm.pdf(bins_fl, mu_fl, sigma_fl)
plt.plot(bins_fl, y_fl, 'g--', label='positive class')

# For the negative class (red)
mu_nofl = np.mean(no_CME_data[:, i])
sigma_nofl = np.std(no_CME_data[:, i])
n_nofl, bins_nofl, patches_nofl = plt.hist(
    no_CME_data[:, i], num_bins, normed=1, facecolor='red', alpha=0.5)
y_nofl = scipy.stats.norm.pdf(bins_nofl, mu_nofl, sigma_nofl)
plt.plot(bins_nofl, y_nofl, 'r--', label='negative class')

text_style = dict(fontsize=16, fontdict={'family': 'monospace'})
plt.xlabel('Normalized '+sharps[i], **text_style)
plt.ylabel('Number (normalized)', labelpad=20, **text_style)
fig = plt.gcf()
fig.set_size_inches(10, 5)
fig.savefig('fscore_tmp.png', bbox_inches='tight')
legend = plt.legend(loc='upper right', fontsize=12, framealpha=0.0, title='')
legend.get_frame().set_linewidth(0.0)

Now we will compute the Univariate F-score for feature selection. It is a very simple method: the F-score measures the distance between the two distributions for a given feature (inter-class distance), divided by the sum of the variances for this feature (intra-class distance). We can use the `sklearn.feature_selection` module to do this:

# import the feature selection method
from sklearn.feature_selection import SelectKBest, f_classif
# select the number of features
N_features = 18
Nfl = CME_data.shape[0]
Nnofl = no_CME_data.shape[0]
yfl = np.ones(Nfl)
ynofl = np.zeros(Nnofl)
# k is the number of features
selector = SelectKBest(f_classif, k=N_features)
selector.fit(np.concatenate((CME_data, no_CME_data), axis=0),
             np.concatenate((yfl, ynofl), axis=0))
scores = selector.scores_
print(scores)

It's not easy to interpret the scores in this fashion, so let's plot the results. The higher the Univariate Fisher Score, the more predictive the feature.

plt.clf()
order = np.argsort(scores)
orderedsharps = [sharps[i] for i in order]
y_pos2 = np.arange(18)
plt.barh(y_pos2, sorted(scores/np.max(scores)))
plt.ylim((-1, 19))
plt.yticks(y_pos2, orderedsharps, fontsize=12)
plt.xlabel('Normalized Fisher Score', fontsize=15)
plt.title('Normalized Univariate Fisher Score Per Feature', fontsize=15)
plt.subplots_adjust(left=0.5, right=1.0)
fig = plt.gcf()
fig.set_size_inches(9, 5)
plt.show()

## Step 4: The support vector machine

Now we initialize the support vector machine on the data. The SVM uses non-linear decision functions to map the feature space into a higher-dimensional space, where the positive and negative examples can be separated linearly by a hyperplane. <br>

This is incredibly non-intuitive. But we can think of a simpler example. Suppose we had two classes: CME-producing and non-CME producing active regions. And suppose we had two features: the total flux in these regions, and the total area of these regions. We could construct a two-dimentional feature space, where we plot the flux against the area of each active region. Positive examples could be indicated by an X and negatives ones by an O. In theory, if our data behaved well, we could draw a line between these classess. <br>

Since we have 18 features, the SVM constructs an 18-dimensional feature space. In this feature space, the decision boundary separating the positive and negative examples may be non-linear. As such, the algorithm then enlarges this 18-dimensional feature space (using the function indicated by the `kernel` parameter in the [`svm.SVC`](http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html) function) into a higher-dimensional feature space wherein it is possible to linearly separate the positive and negatives classes. There are lots of people trying to work on how to [visualize these multi-dimensional feature spaces](https://github.com/tmadl/highdimensional-decision-boundary-plot), which is an active area of research.

number_of_examples = Nfl + Nnofl
C = 4.0
gamma = 0.075
class_weight = {1: 6.5}
clf = svm.SVC(C=C, gamma=gamma, kernel='rbf', class_weight=class_weight,
              cache_size=500, max_iter=-1, shrinking=True, tol=1e-8, probability=True)

## Step 5: Stratified k-folds cross-validation

Now we run and evaluate the performance of the SVM. There are lots of different ways to evaluate the performance of a classifier, which we discuss in Section 4 of [Bobra & Couvidat (2015)](https://arxiv.org/abs/1411.1405). We're going to choose a metric called the True Skill Score, or the TSS, which we can calculate from four quantities: true positives, true negatives, false positives, and false negatives. We prefer the TSS to all the other metrics as it is insensitive to the class imbalance ratio and thus best for comparison to other groups. The TSS is symmetrically distributed about 0: i.e., it goes from [-1, 1] where 0 represents no skill and a negative value represents a perverse prediction. Thus we are able to predict CMEs in a fashion better than randomly guessing. Here we define a confusion table to measure the performance of our binary classification: <br>

def confusion_table(pred, labels):
    """
    computes the number of TP, TN, FP, FN events given the arrays with predictions and true labels
    and returns the true skill score

    Args:
    pred: np array with predictions (1 for flare, 0 for nonflare)
    labels: np array with true labels (1 for flare, 0 for nonflare)

    Returns: true negative, false positive, true positive, false negative
    """
    Nobs = len(pred)
    TN = 0.
    TP = 0.
    FP = 0.
    FN = 0.
    for i in range(Nobs):
        if (pred[i] == 0 and labels[i] == 0):
            TN += 1
        elif (pred[i] == 1 and labels[i] == 0):
            FP += 1
        elif (pred[i] == 1 and labels[i] == 1):
            TP += 1
        elif (pred[i] == 0 and labels[i] == 1):
            FN += 1
        else:
            print("Error! Observation could not be classified.")
    return TN, FP, TP, FN

Now we run the SVM on our data and cross-validate our results. In our case, the positive sample size is quite small (both objectively and compared to the negative sample size). Therefore, we use a stratified k-folds cross-validation method, which makes k partitions of the data set and uses k-1 folds for training the SVM and 1 fold for testing the trained SVM. The stratification preserves the ratio of positive to negative examples per fold. Then we can permute over the partitions such that each partition eventually makes its way into the testing set. For each individual testing set, we can calculate a skill score. Then we can average the skill scores over the total number of testing sets. 

To compute the TSS, we must first select a value of k. k can be arbitrarily defined and take any value between 2 and `number_of_examples`, so we can explore this parameter space. As k approaches `number_of_examples`, the k-fold method reduces to the Leave One Out method, in which only one example is in the testing set and all other examples are in the training set. The literature suggests this method is not the best, so we can stray away from high values of k. Many studies (e.g. [Kohavi, 1995](http://web.cs.iastate.edu/~jtian/cs573/Papers/Kohavi-IJCAI-95.pdf)) recommend the stratified 10-fold cross-validation to reduce variance and bias. Here, we test their recommendation by computing the TSS using 50 k values, ranging from 2 to 52. 

# lists to hold the TSS and standard deviation of the TSS
array_of_avg_TSS = np.ndarray([50])
array_of_std_TSS = np.ndarray([50])

# xdata are the examples
# ydata are the labels
xdata = np.concatenate((CME_data, no_CME_data), axis=0)
ydata = np.concatenate((np.ones(Nfl), np.zeros(Nnofl)), axis=0)

# mdata contain metadata about the active region that will be useful
# when we interpret the results using LIME
mdata = np.concatenate((positive_class, negative_class), axis=0)

# compute the TSS for a variety of k ranging from 2 to 52
# this is to see how the TSS varies as a function of k, and to test if k=10 really makes sense
for k in range(2, 52):
    skf = StratifiedKFold(n_splits=k, shuffle=True)
    these_TSS_for_this_k = []
    for train_index, test_index in skf.split(xdata, ydata):
        # xtrain are the examples in the training set
        xtrain = xdata[train_index]
        # ytrain are the labels in the training set
        ytrain = ydata[train_index]
        # xtest are the examples in the testing set
        xtest = xdata[test_index]
        ytest = ydata[test_index]    # ytest are the labels in the testing set
        # metadata useful for interpreting with LIME
        mtrain = mdata[train_index]
        # metadata useful for interpreting with LIME
        mtest = mdata[test_index]
        clf.fit(xtrain, ytrain)
        TN, FP, TP, FN = confusion_table(clf.predict(xtest), ytest)
        if (((TP+FN) == 0.0) or (FP+TN) == 0.0):
            these_TSS_for_this_k.append(np.nan)
            continue
        else:
            these_TSS_for_this_k.append(TP/(TP+FN) - FP/(FP+TN))
    TSS_k = np.array(these_TSS_for_this_k)
    array_of_avg_TSS[k-2] = np.mean(TSS_k)
    array_of_std_TSS[k-2] = np.std(TSS_k)

Now we can plot the mean TSS per k, using the standard deviation as the error in the TSS. We see that for high values of k, the standard deviation in the TSS can be greater than the mean. These points are indicated in blue.

fig, ax = plt.subplots(figsize=(10, 8))      # define the size of the figure
orangered = (1.0, 0.27, 0, 1.0)              # create an orange-red color
cornblue = (0.39, 0.58, 0.93, 1.0)           # create a cornflower-blue color

# define some style elements
marker_style_red = dict(linestyle='', markersize=8,
                        fillstyle='full', color=orangered, markeredgecolor=orangered)
marker_style_blue = dict(linestyle='', markersize=8,
                         fillstyle='full', color=cornblue, markeredgecolor=cornblue)
text_style = dict(fontsize=16, fontdict={'family': 'monospace'})

# ascribe the data to the axes
k = np.arange(50)+2
for i in range(50):
    if (array_of_avg_TSS[i] > array_of_std_TSS[i]):
        ax.errorbar(k[i], array_of_avg_TSS[i],
                    yerr=array_of_std_TSS[i], linestyle='', color=orangered)
        ax.plot(k[i], array_of_avg_TSS[i], 'o', **marker_style_red)
    if (array_of_avg_TSS[i] <= array_of_std_TSS[i]):
        ax.errorbar(k[i], array_of_avg_TSS[i],
                    yerr=array_of_std_TSS[i], linestyle='', color=cornblue)
        ax.plot(k[i], array_of_avg_TSS[i], 'o', **marker_style_blue)

# set plot limits
plt.xlim(xmax=52, xmin=0)
plt.ylim(ymax=1.1, ymin=0)

# label the axes and the plot
ax.set_xlabel('k', **text_style)
ax.set_ylabel('TSS', labelpad=20, **text_style)
plt.title(r'TSS per k using stratified k-fold cross-validation', **text_style)
fig = plt.gcf()
fig.set_size_inches(10, 5)

As such, we confirm that high k-values result in a high variance. We find it reasonable to use the stratified 10-fold cross-validation method to compute the TSS and will follow this recommendation. Therefore we report this score as our final result:

print("The TSS equals", array_of_avg_TSS[9],
      "plus or minus", array_of_std_TSS[9], ".")

## Addendum : Local Interpretable Model-Agnostic Explanations (LIME)

Machine-learning is a powerful technique that can help us predict CMEs. However, our goal is not only to predict CMEs, but also to quantitatively understand which signatures indicate the imminent eruption of a CME. But the practical successes of machine-learning algorithms are often not matched by successes in understanding, and this has become an issue within the machine-learning community ([Rahimi and Recht, 2017](http://www.argmin.net/2017/12/11/alchemy-addendum/)).

The SVM is a good model to start with, because it is (relatively) simple and we can use the Univariate Fisher Score to identify the most predictive features. But it would also be useful to figure out why each individual active region was classed as positive or negative. To do this, we can use a tool called [LIME](https://github.com/marcotcr/lime) (or Local Interpretable Model-Agnostic Explanations). <br>

First, we initialize the LIME explainer:

explainer = lime.lime_tabular.LimeTabularExplainer(
    xtrain, feature_names=sharps, class_names=['CME', 'no CME'], discretize_continuous=True)

Then we use the explainer to explain its choice for a particular active region. To do this, the LIME module generates neighborhood data by randomly perturbing the values of the features associated with this active region. If, for any given feature, this perturbation does not change the outcome of the prediction, this feature isn't useful along the perturbed dimension. If, for any given feature, the perturbation does change the outcome of the prediction, this feature is useful along the perturbed dimension. Thus the explainer can determine which features are useful under which conditions.

i = np.random.randint(0, xtest.shape[0])
exp = explainer.explain_instance(xtest[i], clf.predict_proba, num_features=8)

Now we can visualize the results. The bottom left panel shows the probabilities assigned to this particular example (which are computed by the SVM via the `probability=True` parameter). The right panel plots the weights per feature (and indicates the values of these weights at the end of each horizontal bar). The text describes the conditions under which this feature  is predictive. 

print("Here is the prediction explanation for NOAA Active Region",
      mtest[i][1], "(HARPNUM ", mtest[i][0], "),\n which produced a", mtest[i][2], "class flare on", mtest[i][3], ".")
exp.show_in_notebook(show_table=False, show_all=False)

Here is the same information in words:

explained_list = exp.as_list()
for i in range(len(explained_list)):
    if (explained_list[i][1]) < 0:
        feature_sign = 'no CME'
    else:
        feature_sign = 'CME'
    print("The following condition:", explained_list[i][0], "\n predicts",
          feature_sign, "with a model weight of", abs(explained_list[i][1]), ".")
Ejemplo n.º 40
0
    score = accuracy_score(y_test, y_pred)
    print score
    scores.append(score)
print 'mean', np.mean(scores)
print 'std', np.std(scores)

for i in range(len(y_test)):

    if y_test[i] > 2:
        y_test[i] = 3

    if y_pred[i] > 2:
        y_pred[i] = 3

cm = confusion_matrix(y_test, y_pred)

from sklearn import preprocessing

cm = preprocessing.normalize(cm.astype('float32'), axis=1)

plt.figure(2)
plt.matshow(cm)
plt.xticks(np.arange(4), ['R-H', 'L-H', 'Feet', 'Rest'])
plt.yticks(np.arange(4), ['R-H', 'L-H', 'Feet', 'Rest'])
# plt.xticks(np.arange(4), ['Rest', 'R-H', 'L-H', 'Feet'])
# plt.yticks(np.arange(4), ['Rest', 'R-H', 'L-H', 'Feet'])
plt.xlabel('True')
plt.ylabel('Predcited')
plt.colorbar()
plt.show()
Ejemplo n.º 41
0
                r2, p2 = pearsonr(lr_supp_z[i, :], mean_supp_z[i, :])
                print('r/lr: %.4f' % r2)
                corr_means_lr[ilamb, i] = r2

    # boxplot
    plt.figure()
    corrs = np.vstack((corr_means_lr[0, :], corr_means_lr_ae))
    plt.boxplot(corrs.T)
    plt.ylabel('correlation r')
    plt.title('Support Recovery: normal versus low-rank logistic regression\n'
              '%i components' % n_comp)
    tick_strs = [u'normal'] + [u'low-rank lambda=%.2f' % val for val in lambs]
    plt.xticks(np.arange(6) + 1, tick_strs, rotation=320)
    plt.ylim(0, 1.0)
    plt.yticks(np.linspace(0, 1., 11), np.linspace(0, 1., 11))
    plt.tight_layout()

    out_path = op.join(WRITE_DIR, 'supp_recov_comp=%i.png' % n_comp)
    plt.savefig(out_path)

    # barplot
    plt.figure()
    ind = np.arange(5)
    width = 1.
    colors = [
        (242., 62., 22.),  #(7., 196., 255.),
        (7., 176., 242.),
        (7., 136., 217.),
        (7., 40., 164.),
        (1., 4., 64.)
Ejemplo n.º 42
0
        _perf = performance[np.where(a_list == a)]
        plt.errorbar(
            np.arange(20),
            1 - _perf.mean(0),
            # yerr=[performance.std(0), performance.std(0)],
            fmt='--o',
            label=a)

# 4. adjust figure parameters and save
fig_lettersize = 17
plt.legend(loc='best',
           frameon=False,
           title='Alphabet size',
           fontsize=fig_lettersize,
           title_fontsize=fig_lettersize)
plt.xlabel(r'$t_{p}$', fontsize=fig_lettersize)
plt.ylabel('Error', fontsize=fig_lettersize)
plt.xlim([0, 20])
plt.ylim([0, 1])
plt.xticks([0, 5, 10, 15, 20], fontsize=fig_lettersize - 2)
plt.yticks([0, 0.5, 1], ['$0\%$', '$50\%$', '$100\%$'],
           fontsize=fig_lettersize - 2)
plt.tight_layout()

if SAVE_PLOT:
    plots_dir = 'plots/RandomSequenceTask/'
    if not os.path.exists(plots_dir):
        os.makedirs(plots_dir)
    plt.savefig('{}FM_N{}.pdf'.format(plots_dir, NETWORK_SIZE), format='pdf')
plt.show()
#plt.vlines(81,0,1,color='red',linestyle='dotted',linewidth=0.5)

#plt.xticks(np.array([1,40,80,120,160,200,240]),[1,40,80,120,160,200,240],fontsize=9)

delayarray = np.array([
    0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300,
    320, 340, 360
])
timearray = (delayarray * 1e5) / (1e6)
timelist = list(timearray.astype(int))
plt.xticks(timearray, timelist, fontsize=8)
plt.xlabel('Delay Time [Myr]', fontsize=11)

#ax1.set_xticklabels([])
plt.yticks(np.array(
    [0.0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.5]),
           [0.0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.5],
           fontsize=9)
plt.ylabel('Statistical Complexity', fontsize=9)
#plt.xlim(1,499)
plt.ylim(0.15, 0.4)
leg = plt.legend(loc='lower right', fontsize=3, frameon=False, handlelength=5)
leg.set_title('Analysis Length', prop={'size': 5})

savefilename = 'M9_SC_recordlengthvariation_type32i_et.png'
savefile = os.path.normpath(datadir + savefilename)
plt.savefig(savefile, dpi=300, facecolor='w', edgecolor='k')
"""
fig=plt.figure(num=2,figsize=(4,3),dpi=300,facecolor='w',edgecolor='k')
left  = 0.16  # the left side of the subplots of the figure
right = 0.94    # the right side of the subplots of the figure
bottom = 0.2  # the bottom of the subplots of the figure
Ejemplo n.º 44
0
plt.fill_between(result.index.year,
                 result['min_rcp45'],
                 result['max_rcp45'],
                 color=color[1],
                 alpha=.2)
plt.fill_between(result.index.year,
                 result['min_rcp85'],
                 result['max_rcp85'],
                 color=color[2],
                 alpha=.2)
plt.legend(loc="upper left", markerscale=1., scatterpoints=1, fontsize=20)

#ax.set_xlim(result.index.year[0], result.index.year[-1])
plt.xticks(range(result.index.year[0] - 1, result.index.year[-1] + 1, 10),
           fontsize=14)
plt.yticks(fontsize=14)
plt.tick_params(axis='y', which='both', labelleft='on', labelright='on')

ax.grid(axis="x", linestyle="--", color='black', linewidth=0.25, alpha=0.5)
ax.grid(axis="y", linestyle="--", color='black', linewidth=0.25, alpha=0.5)
# Show the minor grid lines with very faint and almost transparent grey lines
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)

xposition = [1970, 2000, 2010, 2040, 2070]
for xc in xposition:
    plt.axvline(x=xc, color='k', linestyle='--')
for tick in ax.xaxis.get_major_ticks():
    tick.label.set_fontsize(20)
plt.axhline(y=0, color='k')
    v: V,
    w: W,
    'TIPO': TIPO,
}

datos = pd.DataFrame(data,
                     columns=[
                         "Estrella", a, b, c, d, e, f, g, h, ii, j, Kk, m, n,
                         o, p, q, r, s, t, u, v, w, "TIPO"
                     ])

datosCorr = datos.corr()
df = datosCorr
datos.to_csv(
    "/home/bleon/Documents/TESIS_FILES/Codigos/DATOS/datos_Final_entrenamiento.csv"
)

f = plt.figure(figsize=(8, 8))
plt.matshow(datosCorr, fignum=f.number, cmap="RdBu")
plt.xticks(range(df.shape[1]), df.columns, fontsize=14, rotation=90)
plt.yticks(range(df.shape[1]), df.columns, fontsize=14)
plt.plot([5.5, 5.5], [-0.5, 21.5], c="red")
plt.plot([-0.5, 21.5], [5.5, 5.5], c="red")
cb = plt.colorbar()
cb.ax.tick_params(labelsize=14)

ts_cor = datetime.datetime.now().timestamp()
print("Correlacion listo:" + str(ts_cor - ts1) + "Segundos")
# plt.savefig("/home/bleon/Documents/TESIS_FILES/Codigos/DATOS/Matriz_Correlacion.png")
plt.show()
Ejemplo n.º 46
0
m.contourf(lon, lat, tab.squeeze(), contours, norm=norm)
plt.colorbar(shrink=0.45,
             ticks=[1.0, 1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 1.07, 1.08])

# x axis
locs, labels = plt.xticks()
newlocs = npy.array(
    [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360], 'f')
newlabels = npy.array(
    [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 0], 'i')
plt.xticks(newlocs, newlabels)
plt.xlabel('Longitude', fontsize=14)
#
# y axis
locsY, labelsy = plt.yticks()
newlocsy = npy.array([
    -80, -70, -60, -50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80
], 'f')
newlabelsy = npy.array([
    -80, -70, -60, -50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80
], 'i')
plt.yticks(newlocsy, newlabelsy)
plt.ylabel('Latitude', fontsize=14)

plt.title(
    'Multiplicative ratio applied on ERAinterim longwave radiation\n (values equal to unity are masked)',
    fontsize=16)
plt.savefig('./smoothed_ratio_lw.png')
plt.close()
Ejemplo n.º 47
0
            target='reflective',
            integration_method=integration_method,
            grid_size=grid_size))

    grad_Fzlist.append(grad_Fz * 10**4)

plt.figure(13)
plt.plot(w / rho, grad_Fzlist, lw=2, c="c", label="rho_0x/w = 0.5")

print((w / (rho))[np.argmin(abs(
    np.array(grad_Fzlist)))])  #print the inflection point

new_ticks1 = np.linspace(0.6, 1.5, 10)  # plot axis
print(new_ticks1)
plt.xticks(new_ticks1, fontsize=20)
plt.yticks(np.linspace(-2, 5, 8), fontsize=20)

plt.rc('xtick', labelsize=20)
plt.rc('ytick', labelsize=20)
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0.6))
ax.spines['bottom'].set_position(('data', 0))

plt.legend(loc=1, fontsize=15)

plt.xlabel('w/rho', fontsize=20)
plt.ylabel('grad_Fz(10^(-4)N/m)', fontsize=20)
Ejemplo n.º 48
0
fig, ax = plt.subplots(figsize=(8, 5))
Cert = np.asarray([.1, .6, .85, .4, .1])
Prob = np.asarray([.6, .2, .1, .05, .05])
objects = ('Worse alot', 'Worse a little', 'Same', 'Better a little',
           'Better alot')
N = len(objects)
y_pos = len(objects) - np.arange(len(objects)) - 1
colors = rvb(1 - Cert)

# Add colormap
y = np.unique(Cert)
newcmp = ListedColormap(rvb(1 - y))
plot = plt.scatter(y, y, c=y, cmap=newcmp, alpha=0.5)
plt.clf()
plt.colorbar(plot)

#! Barplot
plt.barh(y_pos, Prob, align='center', alpha=0.5, color=colors)
plt.yticks(y_pos, objects)
plt.xlabel('Probability')
plt.title('HAB Condition Forecast')

# Turns off grid on the left Axis.
ax.grid(False)

#! Add current date
sns.despine(ax=ax, offset=10)
fig.tight_layout()
plt.show()
plt.savefig("./PNG/Fig_prediction.png", dpi=600)
#plt.vlines(85,0,1,color='red',linestyle='dotted',linewidth=0.5)
delayarray = np.array([
    0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160,
    170, 180, 190, 200, 210, 220, 230, 240, 250
])
delayarray = np.array([
    0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300,
    320, 340, 360, 380, 400, 420, 440, 460, 480, 500
])
timearray = (delayarray * 1e5) / (1e6)
timelist = list(timearray.astype(int))
#plt.xticks(np.array([1,20,40,60,80,100,120,140,160,180,200,220,240]),[1,20,40,60,80,100,120,140,160,180,200,220,240],fontsize=9)

plt.xticks(timearray, timelist, fontsize=12)
plt.yticks(np.array([0.0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40]),
           [0.0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40],
           fontsize=12)
plt.xlabel(r'$\tau_s$ [Myr]', fontsize=15)
#plt.xlabel('Delay Steps',fontsize=9)
ax1.set_xticklabels([])
plt.ylabel(r'$C$', fontsize=15)
plt.xlim(0, 40)
plt.ylim(0.11, 0.4)
#plt.legend(loc='lower right',fontsize=12,frameon=False,handlelength=5,numpoints=3)
plt.text(0.07,
         0.92,
         '(a)',
         horizontalalignment='center',
         verticalalignment='center',
         transform=ax1.transAxes,
         fontsize=16)
Ejemplo n.º 50
0
# Author: Dr. Enrico Corsaro
 def change_fonts(current_data):
     pylab.xticks(fontsize=15)
     pylab.yticks(fontsize=15)
Ejemplo n.º 52
0
def doPosPlots(posDict,
               settings,
               extraTitle,
               selectedCount,
               extraSettings,
               structDict2=None,
               trajectory=None):

    fileName = standardFileName(SCRIPT_DIR, settings.mySeq, extraTitle,
                                settings.trials)

    length = len(settings.mySeq) + 1

    # Make a grid...
    nrows, ncols = settings.cutOff, length
    image = np.zeros(nrows * ncols)

    for i in range(nrows):
        for j in range(ncols):
            image[i + nrows * j] = -990.0

    myMax = -5.0
    myMin = myMax - 7.0
    corrector = 1.0

    goodPosDict = dict(posDict)

    if ("winprob" in extraSettings):
        myMax = np.log10(np.float(2.0))
        myMin = myMax - 1.5

    if ("binaryProb" in extraSettings):
        myMax = 0.1
        myMin = -2.0

    if ("M1" in extraSettings):
        extraTitle = "-ModeOne-" + extraTitle
        corrector = settings.trials

    for pos, val in goodPosDict.iteritems():

        value = 0.0

        if (val > 0.0):
            value = np.log10(val / corrector)
        else:
            value = -99.9

        image[pos.posX + ncols * pos.posY] = np.power(value, 1.0)

    # Reshape things into a grid
    image = image.reshape((nrows, ncols))

    row_labels = range(nrows)
    col_labels = range(ncols)
    plt.matshow(image, cmap=plt.cm.gray_r, vmin=myMin, vmax=myMax)

    if ("M1" in extraSettings):

        #         if not "test" in extraTitle:
        mostFreq, maxX = plotMostFrequentStructure(goodPosDict, length)

        if not (trajectory == None):
            plotFirstTrajectory(fileName, trajectory, length)

    plt.title(
        standardTitle(settings.mySeq, extraTitle, selectedCount,
                      settings.trials))

    ax = plt.gca()

    ax.xaxis.set_ticks_position('bottom')
    ax.invert_yaxis()

    ax.set_ylabel('Basepairs within strands')
    ax.set_xlabel('Basepairs between strands')

    plt.xticks(range(ncols), col_labels)
    plt.yticks(range(nrows), row_labels)
    plt.colorbar(shrink=0.75)  # orientation='Horizontal'

    plt.savefig(fileName + '.pdf')
    plt.close()

    # Now generate a new plot for probability of success of the most common structures

    if ("M1" in extraSettings and (Options.STR_SUCCESS in extraTitle
                                   or Options.STR_FAILURE in extraTitle)
            and not "test" in extraTitle):

        fileName = standardFileName(SCRIPT_DIR, settings.mySeq,
                                    extraTitle + "", settings.trials)

        winProb = list()
        plotRange = range(1, maxX)

        f2 = open(fileName + "-mostPopularStructs.txt", 'w')

        for x in plotRange:

            y = mostFreq[x][0]
            myPos = position(x, y)

            winProb.append(100.0 *
                           computeWinProb(f2, myPos, structDict2, settings))

        f2.close()

        fig = plt.figure()

        plt.fill_between(plotRange,
                         0.0,
                         winProb,
                         facecolor='orange',
                         edgecolor='orange',
                         alpha=0.5)
        plt.savefig(fileName + '-structSample.pdf')
#%% plot phase
axis = np.arange(theta.shape[0])

fig = plt.figure(figsize=(20, 8))
gs = fig.add_gridspec(Nosc, 1)
plt.subplots_adjust(wspace=0.4, hspace=0.0)

for n in range(Nosc):
    #####################
    ax11 = fig.add_subplot(gs[n, 0])
    plt.plot(axis, theta[:, n])

    plt.ylabel('$\\phi_{%d}$' % (n + 1))
    plt.xticks(np.arange(0, Nt + 1, int(
        Nt / 2)))  # plt.xticks(np.arange(0, Nt+1, int(Nt/3)))
    plt.yticks([0, np.pi, 2 * np.pi], labels=['0', '$\\pi$', '$2 \\pi$'])
    plt.grid()

plt.xlabel('# sample')

plt.show()
#%% plot phase dynamics
fig = plt.figure(figsize=(20, 4))
plt.plot(axis, phase_dynamics)
plt.title('synthetic data')
plt.legend(bbox_to_anchor=(1.05, 1),
           labels=['oscillator 1', 'oscillator 2', 'oscillator 3'],
           loc='upper left',
           borderaxespad=0,
           fontsize=26)
plt.xticks(np.arange(0, Nt + 1,
    def buildFullPlots(self):
        """
		Plotting the final figure with firing rates, concentrations and hypnogram
		"""

        plt.figure(figsize=(14, 7))
        supTitle = "State dependency of neural populations activities and neurotransmitters concentrations"
        plt.suptitle(supTitle, weight="bold", size=14)

        plt.subplot(311)
        plt.plot(self.Time,
                 self.f_W_alt,
                 label="Wake-promoting",
                 color="darkorange",
                 linewidth=self.lw)
        plt.plot(self.Time,
                 self.f_N_alt,
                 label="NREM-promoting",
                 color="limegreen",
                 linewidth=self.lw)
        plt.plot(self.Time,
                 self.f_R_alt,
                 label="REM-promoting",
                 color="darkblue",
                 linewidth=self.lw)
        plt.plot(self.Time,
                 self.f_W_ctrl,
                 color="darkorange",
                 linewidth=self.lw,
                 alpha=self.alphaVal,
                 linestyle='--')
        plt.plot(self.Time,
                 self.f_N_ctrl,
                 color="limegreen",
                 linewidth=self.lw,
                 alpha=self.alphaVal,
                 linestyle='--')
        plt.plot(self.Time,
                 self.f_R_ctrl,
                 color="darkblue",
                 linewidth=self.lw,
                 alpha=self.alphaVal,
                 linestyle='--')
        plt.ylabel("Population activity (Hz)", size=11, weight="bold")
        plt.title(self.alterationTitle, weight="bold", size=13)
        plt.legend(loc=6, prop={'size': 13})
        plt.xticks(self.hoursNb, self.hoursId)
        plt.yticks(size=12, weight="bold")

        plt.subplot(312)
        plt.plot(self.Time,
                 self.c_WXi_alt,
                 label="WXi",
                 color="sandybrown",
                 linewidth=self.lw)
        plt.plot(self.Time,
                 self.c_NXi_alt,
                 label="NXi",
                 color="chartreuse",
                 linewidth=self.lw)
        plt.plot(self.Time,
                 self.c_RXe_alt,
                 label="RXe",
                 color="blue",
                 linewidth=self.lw)
        plt.plot(self.Time,
                 self.h_alt,
                 label="Homeostatic Force",
                 color="violet",
                 linewidth=self.lw)
        plt.plot(self.Time,
                 self.c_WXi_ctrl,
                 color="sandybrown",
                 linewidth=self.lw,
                 alpha=self.alphaVal,
                 linestyle='--')
        plt.plot(self.Time,
                 self.c_NXi_ctrl,
                 color="chartreuse",
                 linewidth=self.lw,
                 alpha=self.alphaVal,
                 linestyle='--')
        plt.plot(self.Time,
                 self.c_RXe_ctrl,
                 color="blue",
                 linewidth=self.lw,
                 alpha=self.alphaVal,
                 linestyle='--')
        plt.plot(self.Time,
                 self.h_ctrl,
                 color="violet",
                 linewidth=self.lw,
                 alpha=self.alphaVal,
                 linestyle='--')
        plt.ylabel("Concentration (aU)", size=11, weight="bold")
        plt.legend(loc=6, prop={'size': 13})
        plt.xticks(self.hoursNb, self.hoursId)
        plt.yticks(size=12, weight="bold")

        plt.subplot(313)
        plt.plot(self.Time,
                 self.hypno_alt,
                 label="Hypnogram",
                 color="black",
                 linewidth=2)
        plt.plot(self.Time,
                 self.hypno_ctrl,
                 color="black",
                 linewidth=self.lw / 2,
                 alpha=self.alphaVal,
                 linestyle='--')
        plt.legend(loc=6, prop={'size': 13})
        plt.xticks(self.hoursNb, self.hoursId, size=12, weight="bold")
        plt.yticks([1, 2, 3], ["NREM", "REM", "Wake"], size=11, weight="bold")
        plt.xlabel("Time (h)", size=12, weight="bold")

        plt.subplots_adjust(left=0.05,
                            bottom=0.07,
                            right=0.99,
                            top=0.92,
                            wspace=0.19,
                            hspace=0.05)

        if (self.toShow == "y"):
            plt.savefig(self.dataDirName + "/Activities_Alterations_" +
                        self.dirName + "_" + self.alterationSite + "_sim" +
                        str(self.simNumber) + ".jpg",
                        dpi=(500))
            plt.show()
        elif (self.toShow == "n"):
            plt.savefig(self.dataDirName + "/Activities_Alterations_" +
                        self.dirName + "_" + self.alterationSite + "_sim" +
                        str(self.simNumber) + ".jpg",
                        dpi=(500))
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns

sns.set()


def return_intersection(hist_1, hist_2):
    minima = np.minimum(hist_1, hist_2)
    intersection = np.true_divide(np.sum(minima), np.sum(hist_2))
    return intersection


data_1 = pd.read_csv("resources/results_swapped.csv", sep=",", index_col=0)
data_1.sort_index(axis=1, inplace=True)
corr_1 = data_1.corr()

data_2 = pd.read_csv("resources/Python_user_time.csv", sep=",", index_col=0)
data_2.sort_index(axis=1, inplace=True)
corr_2 = data_2.corr()

plt.figure(figsize=(18, 12))
plt.title('User performance spearman', size=15)
sns.heatmap(corr_2, square=True, center=0)
plt.xticks(rotation=90)
plt.yticks(rotation=0)
plt.show()
Ejemplo n.º 56
0
    def REM_pressure_laser_onset(self, dur = 5*60):
        """Plots REM pressure association with the delay between the end of a REM period and the next laser onset

        Keyword Arguments:
            dur {int or float (usually int)} -- duration of laser stimulation used in simulation of model above (run_mi_model)

        """

        #find delay between end of REM period and next laser onset, grab REM pressure at laser onset
        onset = []
        pressure = []
        REM_induced_onset = []
        REM_induced_dur = []
        record_time = False
        delay = 0
        for i in range(1, len(H[0])):
            #start recording time if REM period ends
            if H[0][i-1] == 1 and H[0][i] != 1:
                record_time = True
            #if time is being recorded, add timestep for each iteration
            if record_time:
                delay += 0.05
                #stop recording time and grab data upon laser onset
                if X[i-1, -1] == 0 and X[i, -1] != 0:
                    record_time = False
                    onset.append(delay)
                    delay = 0
                    pressure.append(X[i, -5])
                    if H[0][i] == 1:
                        REM_induced_onset.append(1)
                    else:
                        REM_induced_onset.append(0)
                    REM_in_dur = False
                    for j in range(int(dur / 0.05)):
                        if (i + j) >= len(H[0]):
                            continue
                        if H[0][i + j] == 1:
                            REM_in_dur = True
                    if REM_in_dur:
                        REM_induced_dur.append(1)
                    else:
                        REM_induced_dur.append(0)

        for i in range(len(onset)):
            if i >= len(onset):
                continue
            #remove outliers
            if onset[i] >= 4000:
                del onset[i]
                del pressure[i]
                del REM_induced_onset[i]
                del REM_induced_dur[i]
                        

        #trendline and r^2
        m, b, r, p, std = stats.linregress(onset, pressure)
                    
        #plot data in scatter plot
        plt.figure()
        plt.scatter(onset, pressure)
        plt.plot(onset, np.multiply(m,onset) + b, color = 'red')
        plt.xlabel('Time Between End of REM Period and Next Laser Onset (s)')
        plt.ylabel('REM Pressure')
        plt.title('REM Pressure Upon Laser Stimulation After REM Period')
        plt.text(max(onset), m * max(onset) + b, f'R^2: {round(r**2, 2)}', fontsize = 12)
        plt.show()

        plt.figure()
        plt.scatter(onset, REM_induced_onset)
        plt.ylim([-0.5, 1.5])
        plt.yticks([0, 1], ['No', 'Yes'])
        plt.xlabel('Time Between End of REM Period and Next Laser Onset (s)')
        plt.ylabel('REM Successfully Induced?')
        plt.title('Ability to Laser-Induce REM After End of Previous REM Period on Laser Onset')
        plt.show()     

        plt.figure()
        plt.scatter(onset, REM_induced_dur)
        plt.ylim([-0.5, 1.5])
        plt.yticks([0, 1], ['No', 'Yes'])
        plt.xlabel('Time Between End of REM Period and Next Laser Onset (s)')
        plt.ylabel('REM Successfully Induced?')
        plt.title('Ability to Laser-Induce REM After End of Previous REM Period Over Laser Duration')
        plt.show()
Ejemplo n.º 57
0
def plot_strategy_hist_clust():
    file = pickle.load(
        open(
            'results/results_cust_all_only_strat31jan/strategy_history_clusterd_diff_strat.p',
            'rb'))

    STRATEGIES = ["0.00", "0.25", "0.50", "0.75", "1.00"]
    data = {}
    for strat in STRATEGIES:
        data[strat] = []
    for line in file:

        for strat in STRATEGIES:

            # Get last value in the list
            data[strat].append(line.iloc[RUNS - 1][strat])

    x_pos = np.arange(len(STRATEGIES))
    values = data.values()
    values = list(values)

    total = []
    # total.append([float(i)/max(values[0]) for i in values[0]])
    # total.append([float(i)/max(values[1]) for i in values[1]])
    # total.append([float(i)/max(values[2]) for i in values[2]])
    # total.append([float(i)/max(values[3]) for i in values[3]])
    # total.append([float(i)/max(values[4]) for i in values[4]])

    total.append(values[0])
    total.append(values[1])
    total.append(values[2])
    total.append(values[3])
    total.append(values[4])

    # fill with colors
    colors = [
        "lightgreen", "lightblue", "lightgreen", "lightblue", "lightgreen",
        "lightblue", "lightgreen", "lightblue", "lightgreen", "lightblue"
    ]
    ticks = STRATEGIES
    fig, axes = plt.subplots()

    boxplot = axes.boxplot(total, patch_artist=True, widths=0.8)
    plt.xticks(x_pos + 1, STRATEGIES)
    plt.yticks(np.arange(0, 100, 20))

    colors = ['lightblue', 'lightblue', 'lightblue', "lightblue", "lightblue"]

    for patch, color in zip(boxplot['boxes'], colors):
        patch.set_facecolor(color)

    # adding horizontal grid lines
    axes.yaxis.grid(True)
    axes.set_title(
        "Ratio of people with a specific strategy at the end of a run")
    axes.set_xlabel('Strategy')
    axes.set_ylabel('Percentage of people')
    plt.show()

    total2 = []

    total2.append(round(np.mean(values[0]), 1))
    total2.append(round(np.mean(values[1]), 1))
    total2.append(round(np.mean(values[2]), 1))
    total2.append(round(np.mean(values[3]), 1))
    total2.append(round(np.mean(values[4]), 1))

    plt.title("Ratio of strategies at end of run")
    plt.pie(total2, autopct='%1.1f%%')
    plt.legend(STRATEGIES)
    plt.show()
Ejemplo n.º 58
0
#!/usr/bin/env python
# coding: utf8
# author: youdi

import numpy as np
import cv2
from matplotlib import pylab as plt
img = cv2.imread('caiying.png', 0)
plt.imshow(img, cmap='gray', interpolation='bicubic')
plt.xticks([]), plt.yticks([])  # to hiede tick values on x anfd Y axis
plt.show()
Ejemplo n.º 59
0
plt.subplots_adjust(left=left,
                    bottom=bottom,
                    right=right,
                    top=top,
                    wspace=wspace,
                    hspace=hspace)

ax1 = plt.subplot(1, 1, 1)
hist = plt.hist(
    peak_sep_arr_norm,
    bins=binarr,
    weights=np.ones(len(peak_sep_arr_norm)) / len(peak_sep_arr_norm),
    density=False
)  #note weights and density arguement make total of y values 1, regardless of bin values
plt.xticks(binarr, fontsize=12)
plt.yticks(fontsize=12)
plt.ylim(0, 0.5)
plt.xlabel(r'Period/$\tau_{dyn}^{' + galaxyname + '}$', fontsize=15)
plt.ylabel('Fraction of Orbits', fontsize=15)
plt.title(galaxyname + ' - ' + str(len(peak_seps)) + ' counted of ' +
          str(num_orbits) + ' total orbits')
plt.vlines(mean_sep,
           0,
           1,
           color='red',
           label='Mean=' + str(np.round(mean_sep, 2)))
leg = plt.legend(loc='upper left', fontsize=12, frameon=False)

savefilename = galaxyname + '_Type1_radialoscillationperiod_histogram_abscorr.png'
savefile = os.path.normpath(datadir + savefilename)
plt.savefig(savefile, dpi=600, facecolor='w', edgecolor='k')
Ejemplo n.º 60
0
def select_windows(data_trace,
                   synthetic_trace,
                   event_latitude,
                   event_longitude,
                   event_depth_in_km,
                   station_latitude,
                   station_longitude,
                   minimum_period,
                   maximum_period,
                   min_cc=0.10,
                   max_noise=0.10,
                   max_noise_window=0.4,
                   min_velocity=2.4,
                   threshold_shift=0.30,
                   threshold_correlation=0.75,
                   min_length_period=1.5,
                   min_peaks_troughs=2,
                   max_energy_ratio=10.0,
                   min_envelope_similarity=0.2,
                   verbose=False,
                   plot=False):
    """
    Window selection algorithm for picking windows suitable for misfit
    calculation based on phase differences.

    Returns a list of windows which might be empty due to various reasons.

    This function is really long and a lot of things. For a more detailed
    description, please see the LASIF paper.

    :param data_trace: The data trace.
    :type data_trace: :class:`~obspy.core.trace.Trace`
    :param synthetic_trace: The synthetic trace.
    :type synthetic_trace: :class:`~obspy.core.trace.Trace`
    :param event_latitude: The event latitude.
    :type event_latitude: float
    :param event_longitude: The event longitude.
    :type event_longitude: float
    :param event_depth_in_km: The event depth in km.
    :type event_depth_in_km: float
    :param station_latitude: The station latitude.
    :type station_latitude: float
    :param station_longitude: The station longitude.
    :type station_longitude: float
    :param minimum_period: The minimum period of the data in seconds.
    :type minimum_period: float
    :param maximum_period: The maximum period of the data in seconds.
    :type maximum_period: float
    :param min_cc: Minimum normalised correlation coefficient of the
        complete traces.
    :type min_cc: float
    :param max_noise: Maximum relative noise level for the whole trace.
        Measured from maximum amplitudes before and after the first arrival.
    :type max_noise: float
    :param max_noise_window: Maximum relative noise level for individual
        windows.
    :type max_noise_window: float
    :param min_velocity: All arrivals later than those corresponding to the
        threshold velocity [km/s] will be excluded.
    :type min_velocity: float
    :param threshold_shift: Maximum allowable time shift within a window,
        as a fraction of the minimum period.
    :type threshold_shift: float
    :param threshold_correlation: Minimum normalised correlation coeeficient
        within a window.
    :type threshold_correlation: float
    :param min_length_period: Minimum length of the time windows relative to
        the minimum period.
    :type min_length_period: float
    :param min_peaks_troughs: Minimum number of extrema in an individual
        time window (excluding the edges).
    :type min_peaks_troughs: float
    :param max_energy_ratio: Maximum energy ratio between data and
        synthetics within a time window. Don't make this too small!
    :type max_energy_ratio: float
    :param min_envelope_similarity: The minimum similarity of the envelopes of
        both data and synthetics. This essentially assures that the
        amplitudes of data and synthetics can not diverge too much within a
        window. It is a bit like the inverse of the ratio of both envelopes
        so a value of 0.2 makes sure neither amplitude can be more then 5
        times larger than the other.
    :type min_envelope_similarity: float
    :param verbose: No output by default.
    :type verbose: bool
    :param plot: Create a plot of the algortihm while it does its work.
    :type plot: bool
    """
    # Shortcuts to frequently accessed variables.
    data_starttime = data_trace.stats.starttime
    data_delta = data_trace.stats.delta
    dt = data_trace.stats.delta
    npts = data_trace.stats.npts
    synth = synthetic_trace.data
    data = data_trace.data
    times = data_trace.times()

    # Fill cache if necessary.
    if not TAUPY_MODEL_CACHE:
        from obspy.taup import TauPyModel  # NOQA
        TAUPY_MODEL_CACHE["model"] = TauPyModel("AK135")
    model = TAUPY_MODEL_CACHE["model"]

    # -------------------------------------------------------------------------
    # Geographical calculations and the time of the first arrival.
    # -------------------------------------------------------------------------
    dist_in_deg = geodetics.locations2degrees(station_latitude,
                                              station_longitude,
                                              event_latitude, event_longitude)
    dist_in_km = geodetics.calcVincentyInverse(
        station_latitude, station_longitude, event_latitude,
        event_longitude)[0] / 1000.0

    # Get only a couple of P phases which should be the first arrival
    # for every epicentral distance. Its quite a bit faster than calculating
    # the arrival times for every phase.
    # Assumes the first sample is the centroid time of the event.
    tts = model.get_travel_times(source_depth_in_km=event_depth_in_km,
                                 distance_in_degree=dist_in_deg,
                                 phase_list=["ttp"])
    # Sort just as a safety measure.
    tts = sorted(tts, key=lambda x: x.time)
    first_tt_arrival = tts[0].time

    # -------------------------------------------------------------------------
    # Window settings
    # -------------------------------------------------------------------------
    # Number of samples in the sliding window. Currently, the length of the
    # window is set to a multiple of the dominant period of the synthetics.
    # Make sure it is an uneven number; just to have a trivial midpoint
    # definition and one sample does not matter much in any case.
    window_length = int(round(float(2 * minimum_period) / dt))
    if not window_length % 2:
        window_length += 1

    # Use a Hanning window. No particular reason for it but its a well-behaved
    # window and has nice spectral properties.
    taper = np.hanning(window_length)

    # =========================================================================
    # check if whole seismograms are sufficiently correlated and estimate
    # noise level
    # =========================================================================

    # Overall Correlation coefficient.
    norm = np.sqrt(np.sum(data**2)) * np.sqrt(np.sum(synth**2))
    cc = np.sum(data * synth) / norm
    if verbose:
        _log_window_selection(data_trace.id,
                              "Correlation Coefficient: %.4f" % cc)

    # Estimate noise level from waveforms prior to the first arrival.
    idx_end = int(np.ceil((first_tt_arrival - 0.5 * minimum_period) / dt))
    idx_end = max(10, idx_end)
    idx_start = int(np.ceil((first_tt_arrival - 2.5 * minimum_period) / dt))
    idx_start = max(10, idx_start)

    if idx_start >= idx_end:
        idx_start = max(0, idx_end - 10)

    abs_data = np.abs(data)
    noise_absolute = abs_data[idx_start:idx_end].max()
    noise_relative = noise_absolute / abs_data.max()

    if verbose:
        _log_window_selection(data_trace.id,
                              "Absolute Noise Level: %e" % noise_absolute)
        _log_window_selection(data_trace.id,
                              "Relative Noise Level: %e" % noise_relative)

    # Basic global rejection criteria.
    accept_traces = True
    if (cc < min_cc) and (noise_relative > max_noise / 3.0):
        msg = "Correlation %.4f is below threshold of %.4f" % (cc, min_cc)
        if verbose:
            _log_window_selection(data_trace.id, msg)
        accept_traces = msg

    if noise_relative > max_noise:
        msg = "Noise level %.3f is above threshold of %.3f" % (noise_relative,
                                                               max_noise)
        if verbose:
            _log_window_selection(data_trace.id, msg)
        accept_traces = msg

    # Calculate the envelope of both data and synthetics. This is to make sure
    # that the amplitude of both is not too different over time and is
    # used as another selector. Only calculated if the trace is generally
    # accepted as it is fairly slow.
    if accept_traces is True:
        data_env = obspy.signal.filter.envelope(data)
        synth_env = obspy.signal.filter.envelope(synth)

    # -------------------------------------------------------------------------
    # Initial Plot setup.
    # -------------------------------------------------------------------------
    # All the plot calls are interleaved. I realize this is really ugly but
    # the alternative would be to either have two functions (one with plots,
    # one without) or split the plotting function in various subfunctions,
    # neither of which are acceptable in my opinion. The impact on
    # performance is minimal if plotting is turned off: all imports are lazy
    # and a couple of conditionals are cheap.
    if plot:
        import matplotlib.pylab as plt  # NOQA
        import matplotlib.patheffects as PathEffects  # NOQA

        if accept_traces is True:
            plt.figure(figsize=(18, 12))
            plt.subplots_adjust(left=0.05,
                                bottom=0.05,
                                right=0.98,
                                top=0.95,
                                wspace=None,
                                hspace=0.0)
            grid = (31, 1)

            # Axes showing the data.
            data_plot = plt.subplot2grid(grid, (0, 0), rowspan=8)
        else:
            # Only show one axes it the traces are not accepted.
            plt.figure(figsize=(18, 3))

        # Plot envelopes if needed.
        if accept_traces is True:
            plt.plot(times,
                     data_env,
                     color="black",
                     alpha=0.5,
                     lw=0.4,
                     label="data envelope")
            plt.plot(synthetic_trace.times(),
                     synth_env,
                     color="#e41a1c",
                     alpha=0.4,
                     lw=0.5,
                     label="synthetics envelope")

        plt.plot(times, data, color="black", label="data", lw=1.5)
        plt.plot(synthetic_trace.times(),
                 synth,
                 color="#e41a1c",
                 label="synthetics",
                 lw=1.5)

        # Symmetric around y axis.
        middle = data.mean()
        d_max, d_min = data.max(), data.min()
        r = max(d_max - middle, middle - d_min) * 1.1
        ylim = (middle - r, middle + r)
        xlim = (times[0], times[-1])
        plt.ylim(*ylim)
        plt.xlim(*xlim)

        offset = (xlim[1] - xlim[0]) * 0.005
        plt.vlines(first_tt_arrival, ylim[0], ylim[1], colors="#ff7f00", lw=2)
        plt.text(first_tt_arrival + offset,
                 ylim[1] - (ylim[1] - ylim[0]) * 0.02,
                 "first arrival",
                 verticalalignment="top",
                 horizontalalignment="left",
                 color="#ee6e00",
                 path_effects=[
                     PathEffects.withStroke(linewidth=3, foreground="white")
                 ])

        plt.vlines(first_tt_arrival - minimum_period / 2.0,
                   ylim[0],
                   ylim[1],
                   colors="#ff7f00",
                   lw=2)
        plt.text(first_tt_arrival - minimum_period / 2.0 - offset,
                 ylim[0] + (ylim[1] - ylim[0]) * 0.02,
                 "first arrival - min period / 2",
                 verticalalignment="bottom",
                 horizontalalignment="right",
                 color="#ee6e00",
                 path_effects=[
                     PathEffects.withStroke(linewidth=3, foreground="white")
                 ])

        for velocity in [6, 5, 4, 3, min_velocity]:
            tt = dist_in_km / velocity
            plt.vlines(tt, ylim[0], ylim[1], colors="gray", lw=2)
            if velocity == min_velocity:
                hal = "right"
                o_s = -1.0 * offset
            else:
                hal = "left"
                o_s = offset
            plt.text(tt + o_s,
                     ylim[0] + (ylim[1] - ylim[0]) * 0.02,
                     str(velocity) + " km/s",
                     verticalalignment="bottom",
                     horizontalalignment=hal,
                     color="0.15")
        plt.vlines(dist_in_km / min_velocity + minimum_period / 2.0,
                   ylim[0],
                   ylim[1],
                   colors="gray",
                   lw=2)
        plt.text(dist_in_km / min_velocity + minimum_period / 2.0 - offset,
                 ylim[1] - (ylim[1] - ylim[0]) * 0.02,
                 "min surface velocity + min period / 2",
                 verticalalignment="top",
                 horizontalalignment="right",
                 color="0.15",
                 path_effects=[
                     PathEffects.withStroke(linewidth=3, foreground="white")
                 ])

        plt.hlines(noise_absolute,
                   xlim[0],
                   xlim[1],
                   linestyle="--",
                   color="gray")
        plt.hlines(-noise_absolute,
                   xlim[0],
                   xlim[1],
                   linestyle="--",
                   color="gray")
        plt.text(offset,
                 noise_absolute + (ylim[1] - ylim[0]) * 0.01,
                 "noise level",
                 verticalalignment="bottom",
                 horizontalalignment="left",
                 color="0.15",
                 path_effects=[
                     PathEffects.withStroke(linewidth=3, foreground="white")
                 ])
        plt.legend(loc="lower right",
                   fancybox=True,
                   framealpha=0.5,
                   fontsize="small")
        plt.gca().xaxis.set_ticklabels([])

        # Plot the basic global information.
        ax = plt.gca()
        txt = (
            "Total CC Coeff: %.4f\nAbsolute Noise: %e\nRelative Noise: %.3f" %
            (cc, noise_absolute, noise_relative))
        ax.text(0.01,
                0.95,
                txt,
                transform=ax.transAxes,
                fontdict=dict(fontsize="small", ha='left', va='top'),
                bbox=dict(boxstyle="round", fc="w", alpha=0.8))
        plt.suptitle("Channel %s" % data_trace.id, fontsize="larger")

        # Show plot and return if not accepted.
        if accept_traces is not True:
            txt = "Rejected: %s" % (accept_traces)
            ax.text(0.99,
                    0.95,
                    txt,
                    transform=ax.transAxes,
                    fontdict=dict(fontsize="small", ha='right', va='top'),
                    bbox=dict(boxstyle="round", fc="red", alpha=1.0))
            plt.show()
    if accept_traces is not True:
        return []

    # Initialise masked arrays. The mask will be set to True where no
    # windows are chosen.
    time_windows = np.ma.ones(npts)
    time_windows.mask = False
    if plot:
        old_time_windows = time_windows.copy()

    # Elimination Stage 1: Eliminate everything half a period before or
    # after the minimum and maximum travel times, respectively.
    # theoretical arrival as positive.
    min_idx = int((first_tt_arrival - (minimum_period / 2.0)) / dt)
    max_idx = int(
        math.ceil((dist_in_km / min_velocity + minimum_period / 2.0) / dt))
    time_windows.mask[:min_idx + 1] = True
    time_windows.mask[max_idx:] = True
    if plot:
        plt.subplot2grid(grid, (8, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="TRAVELTIME ELIMINATION")
        old_time_windows = time_windows.copy()

    # -------------------------------------------------------------------------
    # Compute sliding time shifts and correlation coefficients for time
    # frames that passed the traveltime elimination stage.
    # -------------------------------------------------------------------------
    # Allocate arrays to collect the time dependent values.
    sliding_time_shift = np.ma.zeros(npts, dtype="float32")
    sliding_time_shift.mask = True
    max_cc_coeff = np.ma.zeros(npts, dtype="float32")
    max_cc_coeff.mask = True

    for start_idx, end_idx, midpoint_idx in _window_generator(
            npts, window_length):
        if not min_idx < midpoint_idx < max_idx:
            continue

        # Slice windows. Create a copy to be able to taper without affecting
        # the original time series.
        data_window = data[start_idx:end_idx].copy() * taper
        synthetic_window = \
            synth[start_idx: end_idx].copy() * taper

        # Elimination Stage 2: Skip windows that have essentially no energy
        # to avoid instabilities. No windows can be picked in these.
        if synthetic_window.ptp() < synth.ptp() * 0.001:
            time_windows.mask[midpoint_idx] = True
            continue

        # Calculate the time shift. Here this is defined as the shift of the
        # synthetics relative to the data. So a value of 2, for instance, means
        # that the synthetics are 2 timesteps later then the data.
        cc = np.correlate(data_window, synthetic_window, mode="full")

        time_shift = cc.argmax() - window_length + 1
        # Express the time shift in fraction of the minimum period.
        sliding_time_shift[midpoint_idx] = (time_shift * dt) / minimum_period

        # Normalized cross correlation.
        max_cc_value = cc.max() / np.sqrt(
            (synthetic_window**2).sum() * (data_window**2).sum())
        max_cc_coeff[midpoint_idx] = max_cc_value

    if plot:
        plt.subplot2grid(grid, (9, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="NO ENERGY IN CC WINDOW")
        # Axes with the CC coeffs
        plt.subplot2grid(grid, (15, 0), rowspan=4)
        plt.hlines(0, xlim[0], xlim[1], color="lightgray")
        plt.hlines(-threshold_shift,
                   xlim[0],
                   xlim[1],
                   color="gray",
                   linestyle="--")
        plt.hlines(threshold_shift,
                   xlim[0],
                   xlim[1],
                   color="gray",
                   linestyle="--")
        plt.text(5,
                 -threshold_shift - (2) * 0.03,
                 "threshold",
                 verticalalignment="top",
                 horizontalalignment="left",
                 color="0.15",
                 path_effects=[
                     PathEffects.withStroke(linewidth=3, foreground="white")
                 ])
        plt.plot(times,
                 sliding_time_shift,
                 color="#377eb8",
                 label="Time shift in fraction of minimum period",
                 lw=1.5)
        ylim = plt.ylim()
        plt.yticks([-0.75, 0, 0.75])
        plt.xticks([300, 600, 900, 1200, 1500, 1800])
        plt.ylim(ylim[0], ylim[1] + ylim[1] - ylim[0])
        plt.ylim(-1.0, 1.0)
        plt.xlim(xlim)
        plt.gca().xaxis.set_ticklabels([])
        plt.legend(loc="lower right",
                   fancybox=True,
                   framealpha=0.5,
                   fontsize="small")

        plt.subplot2grid(grid, (10, 0), rowspan=4)
        plt.hlines(threshold_correlation,
                   xlim[0],
                   xlim[1],
                   color="0.15",
                   linestyle="--")
        plt.hlines(1, xlim[0], xlim[1], color="lightgray")
        plt.hlines(0, xlim[0], xlim[1], color="lightgray")
        plt.text(5,
                 threshold_correlation + (1.4) * 0.01,
                 "threshold",
                 verticalalignment="bottom",
                 horizontalalignment="left",
                 color="0.15",
                 path_effects=[
                     PathEffects.withStroke(linewidth=3, foreground="white")
                 ])
        plt.plot(times,
                 max_cc_coeff,
                 color="#4daf4a",
                 label="Maximum CC coefficient",
                 lw=1.5)
        plt.ylim(-0.2, 1.2)
        plt.yticks([0, 0.5, 1])
        plt.xticks([300, 600, 900, 1200, 1500, 1800])
        plt.xlim(xlim)
        plt.gca().xaxis.set_ticklabels([])
        plt.legend(loc="lower right",
                   fancybox=True,
                   framealpha=0.5,
                   fontsize="small")

    # Elimination Stage 3: Mark all areas where the normalized cross
    # correlation coefficient is under threshold_correlation as negative
    if plot:
        old_time_windows = time_windows.copy()
    time_windows.mask[max_cc_coeff < threshold_correlation] = True
    if plot:
        plt.subplot2grid(grid, (14, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="CORRELATION COEFF THRESHOLD ELIMINATION")

    # Elimination Stage 4: Mark everything with an absolute travel time
    # shift of more than # threshold_shift times the dominant period as
    # negative
    if plot:
        old_time_windows = time_windows.copy()
    time_windows.mask[np.ma.abs(sliding_time_shift) > threshold_shift] = True
    if plot:
        plt.subplot2grid(grid, (19, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="TIME SHIFT THRESHOLD ELIMINATION")

    # Elimination Stage 5: Mark the area around every "travel time shift
    # jump" (based on the traveltime time difference) negative. The width of
    # the area is currently chosen to be a tenth of a dominant period to
    # each side.
    if plot:
        old_time_windows = time_windows.copy()
    sample_buffer = int(np.ceil(minimum_period / dt * 0.1))
    indices = np.ma.where(np.ma.abs(np.ma.diff(sliding_time_shift)) > 0.1)[0]
    for index in indices:
        time_windows.mask[index - sample_buffer:index + sample_buffer] = True
    if plot:
        plt.subplot2grid(grid, (20, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="TIME SHIFT JUMPS ELIMINATION")

    # Clip both to avoid large numbers by division.
    stacked = np.vstack([
        np.ma.clip(synth_env,
                   synth_env.max() * min_envelope_similarity * 0.5,
                   synth_env.max()),
        np.ma.clip(data_env,
                   data_env.max() * min_envelope_similarity * 0.5,
                   data_env.max())
    ])
    # Ratio.
    ratio = stacked.min(axis=0) / stacked.max(axis=0)

    # Elimination Stage 6: Make sure the amplitudes of both don't vary too
    # much.
    if plot:
        old_time_windows = time_windows.copy()
    time_windows.mask[ratio < min_envelope_similarity] = True
    if plot:
        plt.subplot2grid(grid, (25, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="ENVELOPE AMPLITUDE SIMILARITY ELIMINATION")

    if plot:
        plt.subplot2grid(grid, (21, 0), rowspan=4)
        plt.hlines(min_envelope_similarity,
                   xlim[0],
                   xlim[1],
                   color="gray",
                   linestyle="--")
        plt.text(5,
                 min_envelope_similarity + (2) * 0.03,
                 "threshold",
                 verticalalignment="bottom",
                 horizontalalignment="left",
                 color="0.15",
                 path_effects=[
                     PathEffects.withStroke(linewidth=3, foreground="white")
                 ])
        plt.plot(times,
                 ratio,
                 color="#9B59B6",
                 label="Envelope amplitude similarity",
                 lw=1.5)
        plt.yticks([0, 0.2, 0.4, 0.6, 0.8, 1.0])
        plt.ylim(0.05, 1.05)
        plt.xticks([300, 600, 900, 1200, 1500, 1800])
        plt.xlim(xlim)
        plt.gca().xaxis.set_ticklabels([])
        plt.legend(loc="lower right",
                   fancybox=True,
                   framealpha=0.5,
                   fontsize="small")

    # First minimum window length elimination stage. This is cheap and if
    # not done it can easily destabilize the peak-and-trough marching stage
    # which would then have to deal with way more edge cases.
    if plot:
        old_time_windows = time_windows.copy()
    min_length = \
        min(minimum_period / dt * min_length_period, maximum_period / dt)
    for i in flatnotmasked_contiguous(time_windows):
        # Step 7: Throw away all windows with a length of less then
        # min_length_period the dominant periodele
        if (i.stop - i.start) < min_length:
            time_windows.mask[i.start:i.stop] = True
    if plot:
        plt.subplot2grid(grid, (26, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="MINIMUM WINDOW LENGTH ELIMINATION 1")

    # -------------------------------------------------------------------------
    # Peak and trough marching algorithm
    # -------------------------------------------------------------------------
    final_windows = []
    for i in flatnotmasked_contiguous(time_windows):
        # Cut respective windows.
        window_npts = i.stop - i.start
        synthetic_window = synth[i.start:i.stop]
        data_window = data[i.start:i.stop]

        # Find extrema in the data and the synthetics.
        data_p, data_t = find_local_extrema(data_window)
        synth_p, synth_t = find_local_extrema(synthetic_window)

        window_mask = np.ones(window_npts, dtype="bool")

        closest_peaks = find_closest(data_p, synth_p)
        diffs = np.diff(closest_peaks)

        for idx in np.where(diffs == 1)[0]:
            if idx > 0:
                start = synth_p[idx - 1]
            else:
                start = 0
            if idx < (len(synth_p) - 1):
                end = synth_p[idx + 1]
            else:
                end = -1
            window_mask[start:end] = False

        closest_troughs = find_closest(data_t, synth_t)
        diffs = np.diff(closest_troughs)

        for idx in np.where(diffs == 1)[0]:
            if idx > 0:
                start = synth_t[idx - 1]
            else:
                start = 0
            if idx < (len(synth_t) - 1):
                end = synth_t[idx + 1]
            else:
                end = -1
            window_mask[start:end] = False

        window_mask = np.ma.masked_array(window_mask, mask=window_mask)

        if window_mask.mask.all():
            continue

        for j in flatnotmasked_contiguous(window_mask):
            final_windows.append((i.start + j.start, i.start + j.stop))

    if plot:
        old_time_windows = time_windows.copy()
    time_windows.mask[:] = True
    for start, stop in final_windows:
        time_windows.mask[start:stop] = False
    if plot:
        plt.subplot2grid(grid, (27, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="PEAK AND TROUGH MARCHING ELIMINATION")

    # Loop through all the time windows, remove windows not satisfying the
    # minimum number of peaks and troughs per window. Acts mainly as a
    # safety guard.
    old_time_windows = time_windows.copy()
    for i in flatnotmasked_contiguous(old_time_windows):
        synthetic_window = synth[i.start:i.stop]
        data_window = data[i.start:i.stop]
        data_p, data_t = find_local_extrema(data_window)
        synth_p, synth_t = find_local_extrema(synthetic_window)
        if np.min([len(synth_p), len(synth_t), len(data_p), len(data_t)]) < \
                min_peaks_troughs:
            time_windows.mask[i.start:i.stop] = True
    if plot:
        plt.subplot2grid(grid, (28, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="PEAK/TROUGH COUNT ELIMINATION")

    # Second minimum window length elimination stage.
    if plot:
        old_time_windows = time_windows.copy()
    min_length = \
        min(minimum_period / dt * min_length_period, maximum_period / dt)
    for i in flatnotmasked_contiguous(time_windows):
        # Step 7: Throw away all windows with a length of less then
        # min_length_period the dominant period.
        if (i.stop - i.start) < min_length:
            time_windows.mask[i.start:i.stop] = True
    if plot:
        plt.subplot2grid(grid, (29, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="MINIMUM WINDOW LENGTH ELIMINATION 2")

    # Final step, eliminating windows with little energy.
    final_windows = []
    for j in flatnotmasked_contiguous(time_windows):
        # Again assert a certain minimal length.
        if (j.stop - j.start) < min_length:
            continue

        # Compare the energy in the data window and the synthetic window.
        data_energy = (data[j.start:j.stop]**2).sum()
        synth_energy = (synth[j.start:j.stop]**2).sum()
        energies = sorted([data_energy, synth_energy])
        if energies[1] > max_energy_ratio * energies[0]:
            if verbose:
                _log_window_selection(
                    data_trace.id,
                    "Deselecting window due to energy ratio between "
                    "data and synthetics.")
            continue

        # Check that amplitudes in the data are above the noise
        if noise_absolute / data[j.start: j.stop].ptp() > \
                max_noise_window:
            if verbose:
                _log_window_selection(
                    data_trace.id,
                    "Deselecting window due having no amplitude above the "
                    "signal to noise ratio.")
        final_windows.append((j.start, j.stop))

    if plot:
        old_time_windows = time_windows.copy()
    time_windows.mask[:] = True
    for start, stop in final_windows:
        time_windows.mask[start:stop] = False

    if plot:
        plt.subplot2grid(grid, (30, 0), rowspan=1)
        _plot_mask(time_windows,
                   old_time_windows,
                   name="LITTLE ENERGY ELIMINATION")

    if verbose:
        _log_window_selection(
            data_trace.id, "Done, Selected %i window(s)" % len(final_windows))

    # Final step is to convert the index value windows to actual times.
    windows = []
    for start, stop in final_windows:
        start = data_starttime + start * data_delta
        stop = data_starttime + stop * data_delta
        windows.append((start, stop))

    if plot:
        # Plot the final windows to the data axes.
        import matplotlib.transforms as mtransforms  # NOQA
        ax = data_plot
        trans = mtransforms.blended_transform_factory(ax.transData,
                                                      ax.transAxes)
        for start, stop in final_windows:
            ax.fill_between([start * data_delta, stop * data_delta],
                            0,
                            1,
                            facecolor="#CDDC39",
                            alpha=0.5,
                            transform=trans)

        plt.show()

    return windows