Ejemplo n.º 1
0
def movie_plotter(components, movies, movie_id="all", x_buffer=3, y_buffer=2):
    if movie_id == "all":
        plt.scatter(components[:,0], components[:,1])
        plt.xlabel("Component 1")
        plt.ylabel("Component 2")
        plt.show()
    else:
        x = components[movie_id][0]
        y = components[movie_id][1]

        xs = [x - x_buffer, x + x_buffer]
        ys = [y - y_buffer, y + y_buffer]

        plt.scatter(components[:,0], components[:,1])
        plt.xlim(xs)
        plt.ylim(ys)
        plt.xlabel("Component 1")
        plt.ylabel("Component 2")

        for x, y, title in zip(components[:,0], components[:,1], movies['movie_title']):
            if x >= xs[0] and x <= xs[1] and y >= ys[0] and y <= ys[1]:
                try:
                    plt.text(x, y, title)
                except:
                    pass
Ejemplo n.º 2
0
	def plotTree(self,dim,substitution_patterns=[],line_kwargs={},marker_kwargs={}, right_to_left = False):
		"""
			Plots a tree of the nodes, using `dim` as the height, if the node contains `dim`.

			Slashes in the model names will be used as the different levels in the tree. The order of the parts between the slashes is ignored for now (3/4 and 4/3 are the same).

			`substitution_patterns` may contain substitution patterns (used to connect nodes) for re.sub as a three tuple (pattern, substitute, color), where color is the color that will be assigned to this connection

			`line_kwargs` and `marker_kwargs` can contain arguments in a dictionary to alter the options to set lines or markers. The dictionary will be passed on to the plot function.

			`right_to_left` determines, whether the plot is plotted from left to right (default) or the other way around (`right_to_left` = True).


		"""
		right_to_left_mult = 1
		if right_to_left:
			right_to_left_mult = -1
		l, model_points = self.getTree(substitution_patterns)
		last_y = 0
		for ll in l:
			if ll[1][0] in self.keys() and ll[1][1] in self.keys():
				plt.plot([right_to_left_mult*ll[0][0],right_to_left_mult*ll[0][1]], [np.mean(self.getNode(ll[1][0])[dim]),np.mean(self.getNode(ll[1][1])[dim])],ll[2],**line_kwargs)
		for ll in model_points:
			if ll[1][0] in self.keys():
				plt.plot(right_to_left_mult*ll[0][0], np.mean(self.getNode(ll[1][0])[dim]),ll[2],**marker_kwargs)
				x = ll[0][0]
				y = np.mean(self.getNode(ll[1][0])[dim])
				if abs(last_y-y) < 0.1:
					y = y + 0.1
				if 'name' in self.getNode(ll[1][0]):
					plt.text(x,y, self.getNode(ll[1][0])['name'],rotation=30,va='bottom',size=9)
				else:
					plt.text(x,y, ll[1][0],rotation=30,va='bottom',size=9)
		plt.title(dim)
Ejemplo n.º 3
0
    def draw(self, description=None, ofile="test.png"):

        plt.clf()

        for f in self.funcs:
            f()

        plt.axis("off")

        ax = plt.gca()
        ax.set_aspect("equal", "datalim")

        f = plt.gcf()
        f.set_size_inches(12.8, 7.2)

        if description is not None:
            plt.text(0.025, 0.05, description, transform=f.transFigure)

        if self.xlim is not None:
            plt.xlim(*self.xlim)

        if self.ylim is not None:
            plt.ylim(*self.ylim)

        plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

        # dpi = 100 for 720p, 150 for 1080p
        plt.savefig(ofile, dpi=150)
Ejemplo n.º 4
0
    def draw_parallel(self, l, color="k", ls="-", label=None):
        """ draw a line of latitude """

        # working in Earth coordinates
        # find the (x, y) of the parallel start and end
        xs = self.x0 - self.R*np.cos(np.radians(l))
        ys = self.y0 + self.R*np.sin(np.radians(l))
        
        xe = self.x0 + self.R*np.cos(np.radians(l))
        ye = self.y0 + self.R*np.sin(np.radians(l))
        

        # since we are tilted, the actual angle in the drawing
        # (from +x) is l + tilt
        angle = self.tilt

        # transform the points into the rotated frame
        ts = sf._rotate((xs, ys), (self.x0, self.y0), np.radians(angle))
        te = sf._rotate((xe, ye), (self.x0, self.y0), np.radians(angle))

        plt.plot([ts[0], te[0]], [ts[1], te[1]], color=color, ls=ls)

        if label is not None:
            if l > 0:
                va = "bottom"
            elif l == 0:
                va = "center"
            else:
                va = "top"

            plt.text(ts[0]-0.01*self.R, ts[1], label, 
                     horizontalalignment="right", verticalalignment=va, color=color)
Ejemplo n.º 5
0
def impact_turning_points(s, pivot_k=1, up_impact=0.01, down_impact=0.01, visible=False):

    import matplotlib.pylab as plt

    peaks, troughs = pivots(s, pivot_k)
    impacts_p_idx, impacts_p, impacts_t_idx, impacts_t = impact(s, peaks, troughs)

    new_peaks = []
    new_troughs = []

    for i, p in enumerate(impacts_p_idx):
        if impacts_p[i] >= down_impact :
            new_peaks.append(p)
    for i, t in enumerate(impacts_t_idx):
        if impacts_t[i] >= up_impact :
            new_troughs.append(t)
            
    peaks, troughs = alternate_pivots(s, new_peaks, new_troughs)
    
    fig = plt.figure(figsize=(8,6))
    s.plot()
    
    for p in peaks:
        plt.plot(s.index[p], s[p], 'ro', markersize=8)
        if visible == True:
            plt.text(s.index[p], s[p], str(p), fontsize=15)
    for t in troughs:
        plt.plot(s.index[t], s[t], 'go', markersize=8)
        if visible == True:
            plt.text(s.index[t], s[t], str(t), fontsize=15)
Ejemplo n.º 6
0
    def test_fit_gaussian_2d(self):
        import matplotlib.pylab as plt
        # Create the gaussian data
        Xin, Yin = plt.mgrid[0:201, 0:201]
        data = self._gaussian(3, 100, 100, 20, 40)(Xin, Yin) + \
            np.random.random(Xin.shape)

        plt.clf()
        ax2 = plt.axes([0, 0.52, 1.0, 0.4])
        ax2.matshow(data, cmap=plt.cm.gist_earth_r)

        params = ap.fitGaussianImage(data)
        fit = self._gaussian(*params)

        ax2.contour(fit(*np.indices(data.shape)), cmap=plt.cm.cool)
        (height, x, y, width_x, width_y) = params

        plt.text(0.95, 0.05, """
        x : %.1f
        y : %.1f
        width_x : %.1f
        width_y : %.1f""" %(x, y, width_x, width_y),
             fontsize=16, horizontalalignment='right',
             verticalalignment='bottom', transform=ax2.transAxes)

        ax1 = plt.axes([0, 0.08, 1.0, 0.4])
Ejemplo n.º 7
0
 def segmentation(self, threshold):
     img = self.spectrogram["data"]
     mask = (img > threshold).astype(np.float)
     hist, bin_edges = np.histogram(img, bins=60)
     bin_centers = 0.5*(bin_edges[:-1] + bin_edges[1:])
     binary_img = mask > 0.5
     plt.figure(figsize=(11,8))
     plt.subplot(131)
     plt.imshow(img)
     plt.axis('off')
     plt.subplot(132)
     plt.plot(bin_centers, hist, lw=2)
     print(threshold)
     plt.axvline(threshold, color='r', ls='--', lw=2)
     plt.text(0.57, 0.8, 'histogram', fontsize=20, transform = plt.gca().transAxes)
     plt.text(0.45, 0.75, 'threshold = '+ str(threshold)[0:5], fontsize=15, transform = plt.gca().transAxes)
     plt.yticks([])
     plt.subplot(133)     
     plt.imshow(binary_img)
     plt.axis('off')
     plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0, right=1)
     plt.show()
     print(img.max())
     print(binary_img.max())
     
     return mask
def difficulty_vs_time(data, the_skill, concepts=False):
    data.filter_data(0, 100)
    pk, level = data.get_skill_id(the_skill)
    data.trim_times()
    data.add_log_response_times()
    m = EloPriorCurrentModel(KC=2, KI=0.5)
    items = data.get_items_df()
    items = items[items["visualization"] != "pairing"]
    items = items.join(get_difficulty(data, m))
    items = items.join(pd.Series(data.get_dataframe_all().groupby(["item"])["log_response_time"].mean(), name="log_response_time_mean"))
    items = items[items["skill_lvl_"+str(level)] == pk]

    if concepts:
        skills = data.get_skills_df()
        skills = skills.join(items.groupby("skill_lvl_3")["difficulty"].mean())
        skills = skills.join(items.groupby("skill_lvl_3")["log_response_time_mean"].mean())
        skills = skills[skills.index.isin(items["skill_lvl_3"].unique())]
        for id, skill in skills.iterrows():
            plt.plot(skill["difficulty"], skill["log_response_time_mean"], "ok")
            plt.text(skill["difficulty"], skill["log_response_time_mean"], skill["name"])
    else:
        colors = "rgbyk"
        visualizations = list(items["visualization"].unique())
        for id, item in items.iterrows():
            plt.plot(item["difficulty"], item["log_response_time_mean"], "o", color=colors[visualizations.index(item["visualization"])])
            plt.text(item["difficulty"], item["log_response_time_mean"], item["name"])
        for i, vis in enumerate(visualizations):
            plt.plot(-1, 2, "o", color=colors[i], label=vis)
    plt.xlabel("difficulty according to " + str(m))
    plt.ylabel("mean of log time")
    plt.legend(loc=0)
    plt.title(the_skill)
Ejemplo n.º 9
0
def item_clustering(data, skill, cluster_number=3, plot=True):
    pk, level = data.get_skill_id(skill)
    items = data.get_items_df()
    items = items[items["skill_lvl_" + str(level)] == pk]
    items = items[items["visualization"] != "pairing"]

    corr = compute_corr(data)
    corr = pd.DataFrame(corr, index=items.index, columns=items.index)

    print("Corr ({}) contain total {} values and from that {} nans".format(corr.shape, corr.size, corr.isnull().sum().sum()))
    corr[corr.isnull()] = 0

    sc = SpectralClusterer(corr, kcut=corr.shape[0] / 2, mutual=True)
    # sc = SpectralClusterer(corr, kcut=30, mutual=True)
    labels = sc.run(cluster_number=cluster_number, KMiter=50, sc_type=2)

    if plot:
        colors = "rgbyk"
        visualizations = list(items["visualization"].unique())

        for i, p in enumerate(corr.columns):
            item = items.loc[p]
            plt.plot(sc.eig_vect[i,1], sc.eig_vect[i,2], "o", color=colors[visualizations.index(item["visualization"])])
            # plt.plot(sc.eig_vect[i, 1], sc.eig_vect[i, 2], "o", color=colors[labels[i]])
            plt.text(sc.eig_vect[i, 1], sc.eig_vect[i, 2], item["name"])

        for i, vis in enumerate(visualizations):
            plt.plot(0, 0, "o", color=colors[i], label=vis)
        plt.title(data)

        plt.legend(loc=3)

    return labels
Ejemplo n.º 10
0
def concept_clustering(data, skill, cluster_number=3, plot=True):
    pk, level = data.get_skill_id(skill)
    items = data.get_items_df()
    items = items[items["skill_lvl_" + str(level)] == pk]
    skills = data.get_skills_df()
    skill_ids = items[~items["skill_lvl_3"].isnull()]["skill_lvl_3"].unique()

    corr = compute_corr(data, merge_skills=True)
    corr = pd.DataFrame(corr, index=skill_ids, columns=skill_ids)
    print("Corr ({}) contain total {} values and from that {} nans".format(corr.shape, corr.size, corr.isnull().sum().sum()))
    corr[corr.isnull()] = 0

    try:
        sc = SpectralClusterer(corr, kcut=corr.shape[0] * 0.5, mutual=True)
        labels = sc.run(cluster_number=cluster_number, KMiter=50, sc_type=2)
    except np.linalg.linalg.LinAlgError:
        sc = SpectralClusterer(corr, kcut=corr.shape[0] * 0.5, mutual=False)
        labels = sc.run(cluster_number=cluster_number, KMiter=50, sc_type=2)

    if plot:
        colors = "rgbyk"
        for i, p in enumerate(corr.columns):
            skill = skills.loc[int(p)]
            plt.plot(sc.eig_vect[i, 1], sc.eig_vect[i, 2], "o", color=colors[labels[i]])
            plt.text(sc.eig_vect[i, 1], sc.eig_vect[i, 2], skill["name"])
        plt.title(data)

    return labels
Ejemplo n.º 11
0
    def test_fit_gaussian_image_l0(self):
        import matplotlib.pylab as plt
        from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
        from mpl_toolkits.axes_grid1.inset_locator import mark_inset
        d1 = ap.catools.caget('LTB-BI:BD1{VF1}Img1:ArrayData')

        self.assertEqual(len(d1), 1220*1620)

        d2 = np.reshape(d1, (1220, 1620))
        params = ap.fitGaussianImage(d2)
        fit = self._gaussian(*params)

        plt.clf()
        extent=(500, 1620, 400, 1220)
        #plt.contour(fit(*np.indices(d2.shape)), cmap=plt.cm.copper)
        plt.imshow(d2, interpolation="nearest", cmap=plt.cm.bwr)
        ax = plt.gca()
        ax.set_xlim(750, 850)
        ax.set_ylim(550, 650)
        (height, y, x, width_y, width_x) = params
        #axins = zoomed_inset_axes(ax, 6, loc=1)
        #axins.contour(fit(*np.indices(d2.shape)), cmap=plt.cm.cool)
        #axins.set_xlim(759, 859)
        #axins.set_ylim(559, 660)
        #mark_inset(ax, axins, loc1=2, loc2=4, fc='none', ec='0.5')

        plt.text(0.95, 0.05, """
        x : %.1f
        y : %.1f
        width_x : %.1f
        width_y : %.1f""" %(x, y, width_x, width_y),
             fontsize=16, horizontalalignment='right',
             verticalalignment='bottom', transform=ax.transAxes)

        plt.savefig(figname("test2.png"))
Ejemplo n.º 12
0
    def plot_uv(self):
        """ Plot UV points. Just because its easy. """
        self.current_plot = 'uv'
        self.ax_zoomed = False
        
        uu = self.uv.d_uv_data['UU']*1e6
        vv = self.uv.d_uv_data['VV']*1e6
        xx = self.uv.d_array_geometry['STABXYZ'][:,0]
        yy = self.uv.d_array_geometry['STABXYZ'][:,1]
        pmax, pmin = np.max([uu, vv])*1.1, np.min([uu,vv])*1.1

        
        fig = self.sp_fig
        plt.subplot(121, aspect='equal')
        ax = plt.plot(xx, yy, 'bo')
        for i in range(len(xx)):
            plt.text(xx[i], yy[i], self.uv.d_array_geometry['ANNAME'][i].strip('Stand').strip('Tile'))

        plt.title("Antenna positions")
        plt.xlabel("X [m]")
        plt.ylabel("Y [m]")
        
        plt.subplot(122, aspect='equal')
        ax = plt.plot(uu, vv, 'b.')
        plt.title("UV data")
        plt.xlabel("UU [$\\mu s$]")
        plt.ylabel("VV [$\\mu s$]")
        plt.xlim(pmin, pmax)
        plt.ylim(pmin, pmax)
        return fig, ax
Ejemplo n.º 13
0
def lattice_plot(component_list, file_path):
    """
    Creates a lattice style plot of all graph components
    """
    graph_fig = plt.figure(figsize=(20, 10))  # Create figure

    # Set the number of rows in the plot based on an odd or
    # even number of components
    num_components = len(component_list)
    if num_components % 2 > 0:
        num_cols = (num_components / 2) + 1
    else:
        num_cols = num_components / 2

    # Plot subgraphs, with centrality annotation
    plot_count = 1
    for G in component_list:
        # Find actor in each component with highest degree
        in_cent = nx.degree(G)
        in_cent = [(b, a) for (a, b) in in_cent.items()]
        in_cent.sort()
        in_cent.reverse()
        high_in = in_cent[0][1]

        # Plot with annotation
        plt.subplot(2, num_cols, plot_count)
        nx.draw_spring(G, node_size=35, with_labels=False)
        plt.text(0, -0.1, "Highest degree: " + high_in, color="darkgreen")
        plot_count += 1

    plt.savefig(file_path)
Ejemplo n.º 14
0
	def plot_data(self,full_fname=None, dpi=300, ys=None, yhat=None):
		name = self.test_name
		if ys is not None : ys_data = ys
		else : ys_data = self.results.series[name]['ys']
		if yhat is not None : yhat_data = yhat
		else: yhat_data = self.results.series[name]['yhat']

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

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

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

		plt.tight_layout()
		if full_fname is not None :
			fig.savefig(full_fname,bbox_inches='tight',dpi=dpi)
			plt.close(fig)
Ejemplo n.º 15
0
def plot_clustering(items, xs, ys, labels=None, texts=None, shapes=None):
    labels = [0] * len(xs) if labels is None else labels
    texts = [''] * len(xs) if texts is None else texts
    shapes = [0] * len(xs) if shapes is None else shapes
    for item, x, y, label, text, shape in zip(items, xs, ys, labels, texts, shapes):
        plt.plot(x, y, markers[shape], color=colors[label])
        plt.text(x, y, text)
Ejemplo n.º 16
0
def plot_prof(obj):
  '''Function that plots a vertical profile of scattering from
  TOC to BOC.
  Input: rt_layer instance object.
  Output: plot of scattering.
  '''
  I = obj.Inodes[:,1,:] \
      / -obj.mu_s
  y = np.linspace(obj.Lc, 0., obj.K+1)
  x = obj.views*180./np.pi
  xm = np.array([])
  for i in np.arange(0,len(x)-1):
    nx = x[i] + (x[i+1]-x[i])/2.
    xm = np.append(xm,nx)
  xm = np.insert(xm,0,0.)
  xm = np.append(xm,180.)
  xx, yy = np.meshgrid(xm, y)
  plt.pcolormesh(xx,yy,I)
  plt.colorbar()
  plt.title('Canopy Fluxes')
  plt.xlabel('Exit Zenith Angle')
  plt.ylabel('Cumulative LAI (0=soil)')
  plt.arrow(135.,3.5,0.,-3.,head_width=5.,head_length=.2,\
      fc='k',ec='k')
  plt.text(140.,2.5,'Downwelling Flux',rotation=90)
  plt.arrow(45.,.5,0.,3.,head_width=5.,head_length=.2,\
      fc='k',ec='k')
  plt.text(35.,2.5,'Upwelling Flux',rotation=270)
  plt.show()
Ejemplo n.º 17
0
def make_corr1d_fig(dosave=False):
    corr = make_corr_both_hemi()
    lw=2; fs=16
    pl.figure(1)#, figsize=(8, 7))
    pl.clf()
    pl.xlim(4,300)
    pl.ylim(-400,+500)    
    lambda_titles = [r'$20 < \lambda < 30$',
                     r'$30 < \lambda < 40$',
                     r'$\lambda > 40$']
    colors = ['blue','green','red']
    for i in range(3):
        corr1d, rcen = corr_1d_from_2d(corr[i])
        ipdb.set_trace()
        pl.semilogx(rcen, corr1d*rcen**2, lw=lw, color=colors[i])
        #pl.semilogx(rcen, corr1d*rcen**2, 'o', lw=lw, color=colors[i])
    pl.xlabel(r'$s (Mpc)$',fontsize=fs)
    pl.ylabel(r'$s^2 \xi_0(s)$', fontsize=fs)    
    pl.legend(lambda_titles, 'lower left', fontsize=fs+3)
    pl.plot([.1,10000],[0,0],'k--')
    s_bao = 149.28
    pl.plot([s_bao, s_bao],[-9e9,+9e9],'k--')
    pl.text(s_bao*1.03, 420, 'BAO scale')
    pl.text(s_bao*1.03, 370, '%0.1f Mpc'%s_bao)
    if dosave: pl.savefig('xi1d_3bin.pdf')
Ejemplo n.º 18
0
def plotBigPsi2():
  '''A function that plots the big psi two angle kernel.
  '''
  n = 16
  view_az = np.ones(n)*0. #1st half 0 then rest pi
  view_ze = np.linspace(0.,np.pi,n)
  view = []
  for a,z in zip(view_az,view_ze):
    view.append((z,a))
  sun_az = 0.
  sun_ze = np.pi
  sun = (sun_ze, sun_az)
  leaf_ze = 0.#np.pi/4.
  y_pos = []
  y_neg = []
  for v in view:
    pos, neg = Big_psi2(v, sun, leaf_ze)
    y_pos.append(pos)
    y_neg.append(neg)
  fig, ax = plt.subplots(1)
  plt.plot(view_ze*180/np.pi,y_pos,'r',label='trans.')
  plt.plot(view_ze*180/np.pi,y_neg,'b',label='refl.')
  plt.legend(loc=7)
  s = '''sun zenith:%.2f, sun azimuth:%.2f,
  view azimuth:%.2f, leaf zenith:%.2f''' % \
      (sun_ze*180/np.pi,sun_az*180/np.pi,\
      view_az[0]*180/np.pi,leaf_ze*180/np.pi)
  props = dict(boxstyle='round',facecolor='wheat',alpha=0.5)
  plt.text(.5,.5, s, bbox = props, transform=ax.transAxes,\
      horizontalalignment='center', verticalalignment='center')
  plt.title(r"$\Psi$' Kernel")
  plt.xlabel(r"$\theta$ (zenith angle)")
  plt.ylabel(r"$\Psi$'")
  plt.show()
Ejemplo n.º 19
0
    def plot_biggest(self,n_plot):
        # plots the n_plot biggest clusters
        s = self.cluster_membership.sum(0)
        order = np.argsort(s)
        
        for i in np.arange(s.size-1,s.size-n_plot-1,-1):
            cluster = order[0,i]
            peaks = np.nonzero(self.cluster_membership.getcol(cluster))[0]
            plt.figure(figsize=(8,8))
            plt.subplot(1,2,1)
            plt.plot(self.peak_data.mass[peaks],self.peak_data.rt[peaks],'ro')
            plt.plot(self.peak_data.transformed[peaks,cluster].toarray(),self.peak_data.rt[peaks],'ko')

            plt.subplot(1,2,2)
            for peak in peaks:
                plt.plot((self.peak_data.mass[peak], self.peak_data.mass[peak]),(0,self.peak_data.intensity[peak]))
                tr = self.peak_data.possible[peak,cluster]-1
                x = self.peak_data.mass[peak] + random.random()
                y = self.peak_data.intensity[peak] + random.random()
                plt.text(x,y,self.peak_data.transformations[tr].name)
                title_string = "Mean RT: " + str(self.cluster_model.cluster_rt_mean[cluster]) + "(" + \
                    str(1.0/self.cluster_model.cluster_rt_prec[cluster]) + ")"                    
                if hasattr(self.cluster_model, 'cluster_mass_mean'):
                    title_string += " Mean Mass: " + str(self.cluster_model.cluster_mass_mean[cluster]) + \
                        "(" + str(1.0/self.cluster_model.cluster_mass_prec[cluster]) + ")"
                plt.title(title_string) 
        plt.show()
Ejemplo n.º 20
0
    def plot_histogram_with_capacity(self, capacity, main=""):
        """Plot histogram of choices and capacities. The number of alternatives is determined
        from the second dimension of probabilities.
        """
        from matplotlib.pylab import bar, xticks, yticks, title, text, axis, figure, subplot

        probabilities = self.get_probabilities()
        if probabilities.ndim < 2:
            raise StandardError, "probabilities must have at least 2 dimensions."
        alts = self.probabilities.shape[1]
        width_par = (1 / alts + 1) / 2.0
        choice_counts = self.get_choice_histogram(0, alts)
        sum_probs = self.get_probabilities_sum()

        subplot(212)
        bar(arange(alts), choice_counts, width=width_par)
        bar(arange(alts) + width_par, capacity, width=width_par, color="r")
        xticks(arange(alts))
        title(main)
        Axis = axis()
        text(
            alts + 0.5,
            -0.1,
            "\nchoices histogram (blue),\ncapacities (red)",
            horizontalalignment="right",
            verticalalignment="top",
        )
Ejemplo n.º 21
0
def tsne_2D(inputs, colors=None, labels=None, initial_dims = 50, perplexity = 30.0):
    """
    Plots in 2D a set of points (the rows of NumPy 2D array
    ``inputs``), using t-SNE.

    A color coding can be specified with option ``colors``
    (e.g. ['b','r','k','k'] would yield one blue, one red and two
    black points). String labels for each data point can also be
    provided. ``initial_dims`` and ``perplexity`` are hyper-parameters
    of t-SNE.

    This function requires t-SNE python code prodived by Laurens van
    der Maaten (see README in mlpython/misc/third_party/tsne/).

    """

    try:
        import mlpython.misc.third_party.tsne.tsne as tsne
        
    except:
        import warnings
        warnings.warn('tsne_2D requires the t-SNE python code prodived by Laurens van der Maaten. See mlpython/misc/third_party/tsne/README for instructions.')
        return

    Y = tsne.tsne(inputs, 2, 50, 20.0);
    if colors is None:
        colors = 'k'
    scatter(Y[:,0], Y[:,1], 20, colors);

    if labels is not None:
        for x,y,l in zip(Y[:,0],Y[:,1],labels):
            text(x,y,l)
Ejemplo n.º 22
0
def _fig_density(sweight, surweight, pval, nlm):
    """
    Plot the histogram of sweight across the image
    and the thresholds implied by the surrogate model (surweight)
    """
    import matplotlib.pylab as mp
    # compute some thresholds
    nlm = nlm.astype('d')
    srweight = np.sum(surweight,1)
    srw = np.sort(srweight)
    nitem = np.size(srweight)
    thf = srw[int((1-min(pval,1))*nitem)]
    mnlm = max(1,nlm.mean())
    imin = min(nitem-1,int((1.-pval/mnlm)*nitem))
    
    thcf = srw[imin]
    h,c = np.histogram(sweight,100)
    I = h.sum()*(c[1]-c[0])
    h = h/I
    h0,c0 = np.histogram(srweight,100)
    I0 = h0.sum()*(c0[1]-c0[0])
    h0 = h0/I0
    mp.figure(1)
    mp.plot(c,h)
    mp.plot(c0,h0)
    mp.legend(('true histogram','surrogate histogram'))
    mp.plot([thf,thf],[0,0.8*h0.max()])
    mp.text(thf,0.8*h0.max(),'p<0.2, uncorrected')
    mp.plot([thcf,thcf],[0,0.5*h0.max()])
    mp.text(thcf,0.5*h0.max(),'p<0.05, corrected')
    mp.savefig('/tmp/histo_density.eps')
    mp.show()
def plot_prob_effector(sens, fpr, xmax=1, baserate=0.1):
    """Plots a line graph of P(effector|positive test) against
    the baserate of effectors in the input set to the classifier.
        
    The baserate argument draws an annotation arrow
    indicating P(pos|+ve) at that baserate
    """
    assert 0.1 <= xmax <= 1, "Max x axis value must be in range [0,1]"
    assert 0.01 <= baserate <= 1, "Baserate annotation must be in range [0,1]"
    baserates = pylab.arange(0, 1.05, xmax * 0.005)  
    probs = [p_correct_given_pos(sens, fpr, b) for b in baserates]
    pylab.plot(baserates, probs, 'r')
    pylab.title("P(eff|pos) vs baserate; sens: %.2f, fpr: %.2f" % (sens, fpr))
    pylab.ylabel("P(effector|positive)")
    pylab.xlabel("effector baserate")
    pylab.xlim(0, xmax)
    pylab.ylim(0, 1)
    # Add annotation arrow
    xpos, ypos = (baserate, p_correct_given_pos(sens, fpr, baserate))
    if baserate < xmax:
        if xpos > 0.7 * xmax:
            xtextpos = 0.05 * xmax
        else:
            xtextpos = xpos + (xmax-xpos)/5.
        if ypos > 0.5:
            ytextpos = ypos - 0.05
        else:
            ytextpos = ypos + 0.05
        pylab.annotate('baserate: %.2f, P(pos|+ve): %.3f' % (xpos, ypos), 
                       xy=(xpos, ypos), 
                       xytext=(xtextpos, ytextpos),
                       arrowprops=dict(facecolor='black', shrink=0.05))
    else:
        pylab.text(0.05 * xmax, 0.95, 'baserate: %.2f, P(pos|+ve): %.3f' %
                   (xpos, ypos))
Ejemplo n.º 24
0
    def plot_histogram(self, main="", numrows=1, numcols=1, fignum=1):
        """Plot a histogram of choices and probability sums. Expects probabilities as (at least) a 2D array.
        """
        from matplotlib.pylab import bar, xticks, yticks, title, text, axis, figure, subplot

        probabilities = self.get_probabilities()
        if probabilities.ndim < 2:
            raise StandardError, "probabilities must have at least 2 dimensions."
        alts = probabilities.shape[1]
        width_par = (1 / alts + 1) / 2.0
        choice_counts = self.get_choice_histogram(0, alts)
        sum_probs = self.get_probabilities_sum()

        subplot(numrows, numcols, fignum)
        bar(arange(alts), choice_counts, width=width_par)
        bar(arange(alts) + width_par, sum_probs, width=width_par, color="g")
        xticks(arange(alts))
        title(main)
        Axis = axis()
        text(
            alts + 0.5,
            -0.1,
            "\nchoices histogram (blue),\nprobabilities sum (green)",
            horizontalalignment="right",
            verticalalignment="top",
        )
def plot_saccades(rows, saccades):
    timestamp = rows['timestamp']
    
    for i in range(len(saccades)):
        if not (timestamp[0] <= saccades[i]['time_middle'] <= timestamp[-1]):
            continue
        # index in rows
        k = find_closest_index(timestamp, saccades[i]['time_middle'])
        
        x = rows['x'][k]
        y = rows['y'][k]
        
        L = 0.03
        orientation_start = np.radians(saccades[i]['orientation_start'])
        orientation_stop = np.radians(saccades[i]['orientation_stop'])
        
        x1 = x + L * np.cos(orientation_stop)
        y1 = y + L * np.sin(orientation_stop)
        x2 = x - L * np.cos(orientation_start)
        y2 = y - L * np.sin(orientation_start)
        
        pylab.plot([x, x1], [y, y1], 'r-')
        pylab.plot([x, x2], [y, y2], 'g-')
        pylab.plot(x, y, 'rx')
        
        pylab.text(x, y, "%d" % i)
Ejemplo n.º 26
0
 def double_label(self, labels, position="horizontal", **kwargs):
     """
     Plots 2 colors in a square:
         
                   vertical
                +-------------+
                |            /|
                |   1st    /  |
                |        /    |
     horizontal |      /      | horizontal
                |    /        |
                |  /   2nd    |
                |/            |
                +-------------+
                    vertical
     """
     assert len(labels) == 2
     if position == 'horizontal':
         x1 = self.x - self.dx*0.25
         x2 = self.x + self.dx*1.25
         y1 = y2 = self.y + self.dy/2
         ha1, ha2 = 'right', 'left'
         va1 = va2 = 'center'
     elif position == 'vertical':
         x1 = x2 = self.x + self.dx/2
         y1 = self.y + self.dy*1.25
         y2 = self.y - self.dy*0.25
         ha1 = ha2 = 'center'
         va1, va2 = 'bottom', 'top'
     else:
         raise ValueError("`position` must be one of:"
                 "'horizontal', 'vertical'")
     fontdict = kwargs.get('fontdict', {})
     plt.text(x1, y1, labels[0], ha=ha1, va=va1, fontdict=fontdict) 
     plt.text(x2, y2, labels[1], ha=ha2, va=va2, fontdict=fontdict) 
Ejemplo n.º 27
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.º 28
0
def mass_flux_plot(*args,**kwargs):
    fltm = idls.read(args[0])
    injm = idls.read(args[1])
    f1 = plt.figure()

    ax1 = f1.add_subplot(211)
    plt.plot(injm.nt_sc,injm.nmf_rscale,'r')
    plt.plot(injm.nt_sc,injm.nmf_zscale,'b')
    plt.plot(injm.nt_sc,injm.nmf_z0scale,'k')
    plt.plot(injm.nt_sc,(injm.nmf_rscale+injm.nmf_zscale),'g')
    plt.axis([0.0,160.0,0.0,3.5e-5])
    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'Time [yr]',labelpad=6)
    plt.ylabel(r'$\dot{\rm M}_{\rm out} [ \rm{M}_{\odot} \rm{yr}^{-1} ]$',labelpad=15)
    
    ax2 = f1.add_subplot(212)
    plt.plot(fltm.nt_sc,fltm.nmf_rscale,'r')
    plt.plot(fltm.nt_sc,fltm.nmf_zscale,'b')
    plt.plot(fltm.nt_sc,fltm.nmf_z0scale,'k')
    plt.plot(fltm.nt_sc,(fltm.nmf_rscale+fltm.nmf_zscale),'g')
    plt.axis([0.0,160.0,0.0,4.0e-5])
    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'Time [yr]',labelpad=6)
    plt.ylabel(r'$\dot{\rm M}_{\rm out} [ \rm {M}_{\odot} \rm{yr}^{-1} ]$',labelpad=15)
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 plotP2():
  '''A function to plot the two-angle Normalized Scattering
  Phase Function. For comparison see Myneni 1988c fig.1. 
  Output: plot of P2 function
  '''
  N = 32
  g_wt = np.array(gauss_wt[str(N)])
  mu_l = np.array(gauss_mu[str(N)])
  zen = np.arccos(mu_l)
  a = 180.*np.pi/180. # view azimuth
  view = []
  for z in zen:
    view.append((z,a))
  sun = (170./180.*np.pi,0./180.*np.pi) # sun zenith, azimuth
  arch = 'u'
  refl = 0.5
  trans = 0.5
  y = []
  for v in view:
    y.append(P2(v, sun, arch, refl, trans))
  fig, ax = plt.subplots(1)
  plt.plot(mu_l,y, 'r--')
  s = '''sun zenith:%.2f, sun azimuth:%.2f, arch:%s
  view azimuth:%.2f, refl:%.2f, trans:%.2f''' % \
      (sun[0]*180/np.pi,sun[1]*180/np.pi, arch,\
      a*180/np.pi, refl, trans)
  props = dict(boxstyle='round',facecolor='wheat',alpha=0.5)
  plt.text(.5,.5, s, bbox = props, transform=ax.transAxes,\
      horizontalalignment='center', verticalalignment='center')
  plt.xlabel(r"$\mu$ (cosine of exit zenith)")
  plt.ylabel(r"P($\Omega$, $\Omega$0)")
  plt.title("P (Normalized Scattering Phase Function)") 
  plt.show()
for datdir in glob.glob("/data/tnk10/phase-field/alloy625/TKR4p158/run*"
                        ):  # {0}".format(j) for j in (2,3,6,12,21,63)]:
    if path.isdir(datdir) and len(glob.glob("{0}/*.xy".format(datdir))) > 0:
        base = path.basename(datdir)
        # Plot phase diagram
        plt.figure(0, figsize=(10, 7.5))  # inches
        plt.plot(XS, YS, "-k")
        plt.plot(X0, Y0, "-k", zorder=1)
        plt.title("Cr-Nb-Ni at %.0f K" % temp, fontsize=18)
        plt.xlabel(r"$x_\mathrm{Nb}$", fontsize=18)
        plt.ylabel(r"$x_\mathrm{Cr}$", fontsize=18)
        plt.xticks(np.linspace(0, 1, 21))
        plt.scatter(Xtick, Ytick, color="black", s=3)
        gann = plt.text(simX(0.010, 0.495),
                        simY(0.495),
                        r"$\gamma$",
                        fontsize=14)
        dann = plt.text(simX(0.230, 0.010),
                        simY(0.010),
                        r"$\delta$",
                        fontsize=14)
        lann = plt.text(simX(0.340, 0.275), simY(0.275), r"L", fontsize=14)

        # Add composition pathways
        fnames = sorted(glob.glob("{0}/*.xy".format(datdir)))
        for file in fnames[::10]:
            try:
                x, xcr, xnb, P = np.loadtxt(file, delimiter=",", unpack=True)
                num = int(re.search("[0-9]{5,16}", file).group(0)) * 7.5e-5
                plt.plot(
                    simX(xnb, xcr),
Ejemplo n.º 32
0
    def plot_microscope(self):
        for i, logdata in enumerate(self.X):
            # sphero_res_learner_1D/log-learner-20150315-233141-eta-0.001000-theta-0.200000-g-0.999000-target-sine.npz
            # sphero_res_learner_1D/log-learner-20150315-230854-eta-0.001000-theta-0.200000-g-0.999000-target-sine.npz
            # sl = slice(70, 140)
            sl = slice(80, 120)
            # sl = slice(0, 1000)
            # spikes = [19, 27, 30, 32]
            spikes = [20, 22, 24]
            # pl.subplot(511)
            # pl.title("x_raw")
            # pl.plot(logdata["x_raw"][sl])
            pl.subplot(111)
            pl.title("Motor signal, performance and weight changes")
            # x = logdata["x"][sl,0]
            x = logdata["e"][sl, 0]
            # dx = np.diff(x, axis=0)
            dx = x + np.roll(x, -1)
            # dx = np.roll(dx, 1)
            # dx[0] = 0
            # for j in spikes:
            #     pl.axvline(x=j+3, ymin=0.45, ymax=0.55, lw=2)
            # x = np.roll(x, -3)
            # recreate perf_lp
            dx_lp = np.zeros_like(dx)
            for j, k in enumerate(dx):
                if j > 1:
                    dx_lp[j] = 0.8 * dx_lp[j - 1] + 0.2 * k
                else:
                    dx_lp[j] = k
            pl.plot(dx * 0.5, lw=2, label="perf")
            # pl.plot(x, lw=2, label="P")
            pl.plot(dx_lp * 0.5, lw=2, label="perf_lp")
            # try:
            #     pl.plot(logdata["t"][sl,0])
            # except Exception, e:
            #     print "fail", e
            # pl.subplot(111)
            # pl.title("z")
            pl.plot(logdata["z"][sl], lw=2, label="Motor")
            zn = logdata["zn"][sl]
            pl.plot(zn, lw=2, label="Noisy motor")
            # pl.plot(logdata["zn_lp"][sl])
            # for j in spikes:
            #     pl.axvline(j, 0.70, 0.85, color="k", linestyle="--", lw=2)
            print x.shape, zn.shape
            xzcorr = np.correlate(zn[:, 0], x, "full")
            print "x-z corr", xzcorr
            # pl.plot(xzcorr)
            # pl.subplot(514)
            # pl.title("err")
            # try:
            #     pl.plot(np.abs(logdata["e"][sl]))
            # except Exception, e:
            #     print "fail", e
            # pl.gca().set_yscale("log")
            # pl.gca().set_xscale("log")
            # pl.subplot(111)
            # pl.title("w")
            w_ = np.linalg.norm(logdata["w"], axis=1)[sl]
            print "w_.shape", w_.shape
            dw = np.diff(logdata["w"], axis=0)
            print "dw.shape", dw.shape
            dw_ = np.linalg.norm(dw[sl], axis=1)
            # dw_ = np.roll(dw_, -3)
            pl.plot(dw_ * 100. - 4., "-o", lw=2, label="|dW|")
            for j in spikes:
                # pl.axvline(j, 0.70, 0.85, color="k", linestyle="--", lw=2)
                # pl.axvline(j+3, 0.05, 0.55, color="k", linestyle="--", lw=2)
                pl.plot([j, j], [zn[j] - 0.5, zn[j] + 0.5],
                        color="k",
                        linestyle="--",
                        lw=2)
                pl.plot([j, j + 3], [zn[j] - 0.5, x[j + 3] + 0.3],
                        color="k",
                        linestyle="--",
                        lw=2)
                pl.plot([j + 3, j + 3], [x[j + 3] + 0.3, -4.],
                        color="k",
                        linestyle="--",
                        lw=2)
                pl.axvline(j + 3, 0.05, 0.55, color="k", linestyle="--", lw=2)
            zwcorr = np.correlate(zn[:, 0], dw_[:, 0], "full")
            print "z-w corr", zwcorr
            pl.text(16, -0.5, "$k$-delay")
            # pl.plot(zwcorr)
            # pl.subplot(511)
            # pl.title("corrs")
            # pl.plot(xzcorr, "k.", label="x/z")
            # pl.twinx()
            # pl.plot(zwcorr, ".", label="z/w")
            pl.ylim((-4.5, 2.))
            pl.ylabel("Mixed")
            pl.xlabel("t [steps]")

            ax = pl.gca()
            box = ax.get_position()
            ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

            # Put a legend to the right of the current axis
            ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

        if self.saveplot:
            pl.gcf().set_size_inches((12, 7))
            pl.gcf().savefig("%s.pdf" % (sys.argv[0][:-3]),
                             dpi=300,
                             bbox_inches="tight")

        pl.show()
map2.drawcoastlines(color='k', linewidth=0.7, zorder=10)
map2.drawcountries(color='k', linewidth=0.5, zorder=10)
# draw the edge of the map projection region (the projection limb)
map2.drawmapboundary()
# draw lat/lon grid lines every 30 degrees.
map2.drawmeridians(np.arange(0, 360, 30), zorder=15)
map2.drawparallels(np.arange(-90, 90, 30), zorder=15)

ncfile = '../../data/trmm_keep_overlapping_tracks_no_jump_cutoff/processed/g20_72h/thresh12/postproc/lpt_track_density__' + season_label + '.nc'

DS = Dataset(ncfile, 'r')

lon = DS['lon'][:]
lat = DS['lat'][:]
density = DS['track_density_rossby_westward_portion'][:]
x1, y1 = np.meshgrid(lon, lat)
x2, y2 = map2(x1 + 1.125, y1 + 1.125)

plt.contour(x2, y2, density, np.arange(2, 72, 2), cmap="jet")

plt.title('Westward LPTs Track Density: 1998 - 2018 (' + season_label.upper() +
          ')')

xt, yt = map2(160, 41)
plt.text(xt, yt, 'Contours every 2\n 2.5 deg. boxes', fontsize=10)

plt.tight_layout()

print(fout)
plt.savefig(fout, dpi=150, bbox_inches='tight')
plt.matshow(avgMatTask, vmin=0.4, vmax=1.0)
plt.colorbar(label='Mean Absolute Error Averaged Over All Subjects')
#plt.colorbar()
plt.title('Nonlinear (Testing)')
plt.xlabel('Models')
plt.ylabel('Data')

#plt.xticks(range(51,102*7, 102), ["EMOTION", "GAMBLING", "LANGUAGE", "MOTOR", "RELATIONAL", "SOCIAL", "WM"])
#plt.yticks(range(51,102*7, 102), ["EMOTION", "GAMBLING", "LANGUAGE", "MOTOR", "RELATIONAL", "SOCIAL", "WM"], rotation =90)
plt.xticks(range(0,len(tasks)), ["E", "G", "L", "M", "R", "S", "W"])
plt.yticks(range(0,len(tasks)), ["E", "G", "L", "M", "R", "S", "W"])
plt.legend()

for asd, cas in enumerate(avgMatTask):
	for sdf, c in enumerate(cas):
			plt.text(sdf-.4, asd+.2, "%.2f" % c)


plt.show()

#######
#######

iFile =  csv.reader(open('minmat_NL_LR_topRL.csv', 'r'))

minMat = []
for l in iFile:
	minMat.append(l)
minMat = np.array(minMat)
minMat = minMat.astype(np.float)
Ejemplo n.º 35
0
def pca(X_std, rep, file_type):
    """pca function that returns PCA scree plots and ..."""
    eig_vecs, eig_vals, v, = np.linalg.svd(X_std.T)
    #test the eig_vecs
    for ev in eig_vecs:
        np.testing.assert_array_almost_equal(1.0, np.linalg.norm(ev))
    print('Everything OK!')

    eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:, i])
                 for i in range(len(eig_vals))]

    #then sort - high to low
    eig_pairs.sort(key=lambda tup: tup[0])
    eig_pairs.reverse()

    print('Eigenvalues in descending order:')
    for i in eig_pairs:
        print(i[0])

    #make plots
    tot = sum(eig_vals)
    var_exp = [(i / tot) * 100 for i in sorted(eig_vals, reverse=True)]
    cum_var_exp = np.cumsum(var_exp)
    #add in cut off for where 95% variance explained
    cut_off = cum_var_exp < 95
    cut_off = np.argwhere(cut_off)
    cut_off = int(cut_off[-1])

    #first make dataframe with all the PCs in
    x = ['PC %s' % i for i in range(1, len(eig_vals))]
    y = var_exp[0:len(eig_vals)]
    z = cum_var_exp[0:len(eig_vals)]
    PC_pairs = [(x[i], y[i], z[i]) for i in range(0, len(eig_vals) - 1)]
    PC_df = pd.DataFrame(data=PC_pairs, columns = ['PC', 'variance_explained', \
                                                'cum_variance_explained'])
    sns.set_style('whitegrid')
    f, (ax1, ax2) = plt.subplots(ncols=2, sharey=True)
    plt.title(rep)
    trace1 = sns.barplot(y='variance_explained', x='PC', data=PC_df, ax=ax1)
    sns.despine()
    ax1.xaxis.set_ticks(np.arange(0, 70, 10))
    ax1.xaxis.set_ticklabels(PC_df['PC'][0:71:10])
    ax1.axes.tick_params(axis='x',
                         rotation=45,
                         direction='in',
                         labelbottom=True)
    ax1.xaxis.axes.set_xlim(left=0, right=70)
    trace2 = sns.stripplot(y='cum_variance_explained',
                           x='PC',
                           data=PC_df,
                           ax=ax2)
    ax2.xaxis.set_ticks(np.arange(0, 70, 10))
    ax2.xaxis.set_ticklabels(PC_df['PC'][0:71:10])
    ax2.axes.tick_params(axis='x',
                         rotation=45,
                         direction='in',
                         labelbottom=True)
    ax2.xaxis.axes.set_xlim(left=0, right=70)
    trace2 = plt.plot([cut_off, cut_off], [0, 95], linewidth=2)
    plt.text(cut_off, 100, str(cut_off))

    sns.despine()
    f.savefig(os.path.join(directory[0:-7], 'Figures',
                           rep + '_PC_variance_explained.' + file_type),
              dpi=400)
    plt.show()
    del x, y, z, tot, var_exp, cum_var_exp, f, ax1, ax2, trace1, trace2

    return eig_vecs, eig_vals, eig_pairs, PC_pairs, PC_df, cut_off
def models_result(minute, closep1, openp, lowp, data, timestamp):
    data = scale_data(data)
    print("ffnn")
    ts_date_f, original_f, predicted_f = ffnn_model(minute, closep1, openp,
                                                    lowp, data, timestamp)
    print("dnn")
    ts_date_d, original_d, predicted_d = dnn_model(minute, closep1, openp,
                                                   lowp, data, timestamp)
    print("rnn")
    ts_date_r, original_r, predicted_r = rnn_model(minute, closep1, openp,
                                                   lowp, data, timestamp)

    minimum, maximum = min_max(closep1)
    pred_f = calculate_actual_values(predicted_f, maximum, minimum)
    actual_f = calculate_actual_values(original_f, maximum, minimum)

    pred_d = calculate_actual_values(predicted_d, maximum, minimum)
    actual_d = calculate_actual_values(original_d, maximum, minimum)
    # converted into actual values
    pred_r = calculate_actual_values(predicted_r, maximum, minimum)
    actual_r = calculate_actual_values(original_r, maximum, minimum)

    mseA = np.round(mean_squared_error(actual_f, pred_f), 4)
    rmsefA = root_mean_square_error_fun(mseA)
    maefA = mean_absolute_error_fun(actual_f, pred_f)
    mape1A = mean_absolute_percentage_error_fun(actual_f, pred_f)
    r_scoreA = r2_score(actual_f, pred_f)

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

    mse_d = np.round(mean_squared_error(actual_d, pred_d), 4)
    rmse_d = root_mean_square_error_fun(mse_d)
    mae_d = mean_absolute_error_fun(actual_d, pred_d)
    mape_d = mean_absolute_percentage_error_fun(actual_d, pred_d)
    r_score_d = r2_score(actual_d, pred_d)

    print("mse:  ", mse_d, "rmse: ", rmse_d, "mae: ", mae_d, "mape: ", mape_d,
          "r2score: ", r_score_d)

    mse_r = np.round(mean_squared_error(actual_r, pred_r), 2)
    rmse_r = root_mean_square_error_fun(mse_r)
    mae_r = mean_absolute_error_fun(actual_r, pred_r)
    mape_r = mean_absolute_percentage_error_fun(actual_r, pred_r)
    r_score_r = r2_score(actual_r, pred_r)

    print("mse:  ", mse_r, "rmse: ", rmse_r, "mae: ", mae_r, "mape: ", mape_r,
          "r2score: ", r_score_r)

    errors_label = ('rmse', 'mae', 'mape')
    y_pos = np.arange(len(errors_label))
    error_values = [rmsefA, maefA, mape1A]
    #error_values_d = np.array([mse_d, rmse_d, mae_d, mape_d])
    error_values_d = [rmse_d, mae_d, mape_d]
    error_values_r = [rmse_r, mae_r, mape_r]
    width = 0.25

    tests = [
        'Hidden Layers: 2', 'neurons: {512,11}',
        'activation: {sigmoid,linear}', 'lr: 0.001'
    ]
    ind = np.arange(3)
    plt.figure(1)
    plt.subplot(211)
    plt.xticks(rotation=30)
    plt.plot(ts_date_f, actual_f, label='close', color='red')
    plt.plot(ts_date_f, pred_f, label="ffnn", color='green')
    plt.plot(ts_date_f, pred_d, label="rnn", color='yellow')
    plt.plot(ts_date_f, pred_r, label="dnn", color='blue')

    plt.grid(True)
    plt.title("Forecast price " + str(minute) + " minutes", fontweight='bold')
    plt.legend(loc=2)  # to left position
    plt.xlabel("Time")
    plt.ylabel("Price of Bitcoin (USD)")
    plt.subplot(212)
    rect1 = plt.bar(ind,
                    error_values,
                    width,
                    align='center',
                    color='r',
                    label='ffnn')
    rect2 = plt.bar(ind + width + 0.00,
                    error_values_d,
                    width,
                    align='center',
                    color='b',
                    label='dnn')
    rect3 = plt.bar(ind + (width * 2) + 0.01,
                    error_values_r,
                    width,
                    align='center',
                    color='g',
                    label='rnn')
    plt.xticks(y_pos, errors_label)
    plt.grid(axis='y')
    plt.legend()
    #plt.legend((error_values[0],error_values_d[0],error_values_r[0]),('FFNN','DNN','RNN'))

    for a, f, d, r in zip(y_pos, error_values, error_values_d, error_values_r):
        plt.text(a, f, str(f))
        plt.text(a + 0.25, d, str(d))
        plt.text(a + .50, r, str(r))
        # plt.annotate(str(b),y_poserror_values=(a,b))

    plt.title('Evaluation Criteria', fontweight='bold')
    plt.xlabel('Errors')
    plt.ylabel('Values of Errors')
    '''
    '''
    plt.subplots_adjust(hspace=0.7, wspace=0.7)

    plt.show()

    return 0
Ejemplo n.º 37
0
import matplotlib.pylab as plt
import numpy as np
plt.rcParams['font.sans-serif'] = "SimHei"  #设置默认字体为黑体
plt.rcParams["axes.unicode_minus"] = False  #正常显示负号
n = 1024
#标准正太分布
x = np.random.normal(0, 1, n)  #生成正太分布x
y = np.random.normal(0, 1, n)
#绘制散点图
plt.scatter(x, y, color="blue", marker='*')  #形状为*
#设置标题
plt.title("标准正态分布", fontsize=20)
#设置文字标签
plt.text(2.5, 2.5, "均值: 0\n标准差:1")  #2.5 2.5表示标签的位置
#设置坐标轴的范围
plt.xlim(-4, 4)
plt.ylim(-4, 4)
#设置坐标轴的标签
plt.xlabel("横坐标x", fontsize="14")
plt.ylabel("纵坐标y", fontsize="14")
plt.show()
Ejemplo n.º 38
0
    kmer_length = int(file_name.split('_')[0].replace('k', ''))
    num_clusters = int(file_name.split('_')[1].replace('m', ''))
    n_fold = int(file_name.split('_')[2].replace('fold', ''))

    plt.hist(phage_data,
             bins,
             histtype='bar',
             color='blue',
             label='Phage',
             alpha=0.8)
    plt.hist(bacteria_data,
             bins,
             histtype='bar',
             color='green',
             label='Bacteria',
             alpha=0.6)
    plt.xlabel('Score: ' r'$log(p-value)$')
    plt.ylabel('Abundance (within %d samples)' % len(phage_data))
    title = '%d-Fold Cross-Validation Score Histogram for Phage Predictiction' % n_fold
    plt.title(title)
    plt.legend(loc=2)
    plt.grid()
    plt.text(-1.8, 112, 'Kmer: %dbp' % kmer_length)
    plt.text(-1.8, 103, '%d k-means clusters' % num_clusters)
    font = {'weight': 'bold', 'size': 12}
    plt.rc('font', **font)
    if x_low:
        plt.xlim([x_low, 0])
    plt.savefig(output_file)
    os.system('open %s' % output_file)
Ejemplo n.º 39
0
def run(appfolder, stationName, data):
    try:
        Data={}
        for k, v in data.iteritems():
            Data[int(k)] = {}
            for a, b in v.iteritems():
                Data[int(k)][int(a)] = map(lambda x: x if x!=None else 0,b)

        print "DATA TO RUN...", Data
        Years = Data.keys() # My Index with Years
        #-------------------------------------------------------------------------------
        # Calculation for Thronwaite Potential Evapotranspiration Formula
        # FinalData:  Data = {Year:Month:[Rain,Temp,i_index,PET}
        #-------------------------------------------------------------------------------
        #Calculation i_index
        for Year in Years:
            for i in [1,2,3,4,5,6,7,8,9,10,11,12]:
                if Data[Year][i][1]>0:
                    i_index = 0.09*(Data[Year][i][1])**1.514
                else: #if Data[Year][i][1]<=0:
                    i_index = 0
                Data[Year][i].append(i_index)
        #Daylight Hour  (Just for Now) Constant =12 hour <----- THIS WILL CHANGE!!!
        N=12
        for Year in Years:
            sum_I=0
            for i in [1,2,3,4,5,6,7,8,9,10,11,12]:
                sum_I = sum_I + Data[Year][i][2]
            for i in [1,2,3,4,5,6,7,8,9,10,11,12]:
                DaysPerMonth = [31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
                if calendar.isleap(Year) == True:
                    DaysPerMonth[1]=29
                N=12
                if Data[Year][i][1] == 0:
                    PET = 0
                else:
                    if sum_I != 0: #prevent division by zero
                        PET  = 16 * (10 * (Data[Year][i][1])/sum_I)* ( DaysPerMonth[i-1] * N/360)
                    else:
                        print stationName, Data
                        PET = 0
                if PET <= 0 :
                    Data[Year][i].append(0)
                else:
                    Data[Year][i].append(PET)
        #-------------------------------------------------------------------------------
        # FINAL DATASET IS READY FOR CALC SPI AND RDI
        #-------------------------------------------------------------------------------
        # Get Special Rain and Rain/PET  DataSet :
        rain1_data = []
        rain3_data = []
        rain6_data = []
        rain12_data = []
        rainpet1 = []
        rainpet3 = []
        rainpet6 = []
        rainpet12 = []

        for Year in Years:
            #Rain1
            for i in [1,2,3,4,5,6,7,8,9,10,11,12]:
                rain1_data.append(Data[Year][i][0])
                if Data[Year][i][3]==0:
                    rainpet1.append(0)
                else:
                    rainpet1.append(float(Data[Year][i][0])/Data[Year][i][3])
            #Rain3 Selected Months
            Months= [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] # Every 3 Months Selections
            for month in Months:
                sum_rain3 = 0 # Loop for Summing
                sum_RP3 = 0
                for i in [0,1,2]:
                    sum_rain3 = sum_rain3+Data[Year][month[i]][0] # Selected Values
                    if Data[Year][month[i]][3] != 0:
                        sum_RP3 = sum_RP3 +float(Data[Year][month[i]][0])/(Data[Year][month[i]][3])
                    else:
                        sum_RP3 = sum_RP3 + 0
                rain3_data.append(sum_rain3)
                rainpet3.append(sum_RP3)
            #Rain 6 Selected
            Months= [[1,2,3,4,5,6],[7,8,9,10,11,12]]
            for month in Months:
                sum_rain6 = 0
                sum_RP6 = 0
                for i in [0,1,2,3,4,5]:
                    sum_rain6 = sum_rain6+Data[Year][month[i]][0]
                    if Data[Year][month[i]][3] !=0:
                        sum_RP6 = sum_RP6 +float(Data[Year][month[i]][0])/(Data[Year][month[i]][3])
                    else:
                        sum_RP6 = sum_RP6 + 0
                rain6_data.append(sum_rain6)
                rainpet6.append(sum_RP6)
            #Rain12 Selected not so pythonic way
            Months= [[1,2,3,4,5,6,7,8,9,10,11,12]]
            for month in Months:
                sum_rain12 = 0
                sum_RP12 = 0
                for i in [0,1,2,3,4,5,6,7,8,9,10,11]:
                    sum_rain12 = sum_rain12+Data[Year][month[i]][0]
                    if Data[Year][month[i]][3] !=0:
                        sum_RP12 = sum_RP12 +float(Data[Year][month[i]][0])/(Data[Year][month[i]][3])
                    else:
                       sum_RP12 = sum_RP12 + 0
                rain12_data.append(sum_rain12)
                rainpet12.append(sum_RP12)
        #-------------------------------------------------------------------------------------------
        # Datasets For Spi Analusis  - YOU NEED TO TEST FOR RANDOM NULL VALUES!!!
        dataspi = {"Spi1":rain1_data,"Spi3":rain3_data,"Spi6":rain6_data,"Spi12":rain12_data}
        datardi = {"Rdi1":rainpet1,"Rdi3":rainpet3,"Rdi6":rainpet6,"Rdi12":rainpet12}

        #-------------------------------------------------------------------------------------------
        #         RESULTS
        # Note : Problem with RDI (Cant figure out yet)
        #-------------------------------------------------------------------------------------------
        #THIS THE OUTPUT YOU NEED FOR THE WEB APP -- Spi diffferent time Span Resulsv
        spiresults = {"Spi1": spi_calc(dataspi["Spi1"]),"Spi3": spi_calc(dataspi["Spi3"]),"Spi6": spi_calc(dataspi["Spi6"]),"Spi12": spi_calc(dataspi["Spi12"])}
        rdiresults = {"Rdi1": rdi_calc(datardi["Rdi1"]),"Rdi3": rdi_calc(datardi["Rdi3"]),"Rdi6": rdi_calc(datardi["Rdi6"]),"Rdi12": rdi_calc(datardi["Rdi12"])}

        #-------------------------------------------------------------------------------------------
        #          Charts
        #-------------------------------------------------------------------------------------------
        stationname = stationName
        minyear=min(Years)
        maxyear=max(Years)
        keysspi = spiresults.keys()
        keysrdi = rdiresults.keys()
        # Spi1 and Rdi1 Chart-------------------------------------------------------------------------------------------
        plt.figure(figsize=(6*3.13,4*3.13))
        plt.subplot(211)
        plt.axhspan(0, -0.8, facecolor='0.5', alpha=0.5,color ='yellow',linewidth = True)
        plt.axhspan(-0.8, -1.3, facecolor='0.5', alpha=0.5,color ='orange',linewidth = True)
        plt.axhspan(-1.3, -1.6, facecolor='0.5', alpha=0.5,color ='orangered',linewidth = True)
        plt.axhspan(-1.6, -2, facecolor='0.5', alpha=0.5,color ='r',linewidth = True)
        plt.axhspan(-2, -3, facecolor='0.5', alpha=0.5,color ='maroon',linewidth = True)
        Spi1= spiresults["Spi1"]['Spi_result']
        Rdi1= rdiresults["Rdi1"]['Rdi_result']
        n=len(Spi1)
        ind = np.arange(n)
        width = 0.40
        rects1 = plt.bar(ind, Spi1, width, color='b', label='SPI ')
        rects2 = plt.bar(ind+width, Rdi1, width, color='lightblue', label='RDI ')
        plt.ylabel('Drought Index - std units')
        plt.title('{} {} Drought Propagation Chart for The Period {} - {}'.format('[1 month]', stationname,minyear,maxyear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks = range(0,n,12)
        plt.xticks(myTicks,xYears,rotation ="vertical")
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax = n)
        plt.ylim([-3,3])
        plt.text( len(Spi1), 2.5, 'Wet Conditions', rotation=90)
        plt.text( len(Spi1), -0.5, 'Dry Conditions', rotation=90)
        plt.subplot(212)
        plt.scatter(Spi1,rain1_data,label='Spi ')
        plt.scatter(Rdi1,rain1_data,c="lightblue",label='Rdi ')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        #plt.show()
        #return [os.path.join(os.getcwd(), f) for f in os.listdir(os.getcwd())]

        plt.savefig(appfolder+'/static/charts'
                            '/'+stationName+'_1month.png')
        # <--------------------------------------------PIC ANTONIS
        plt.close()
        # Note some info inside the chart will be added later
        # Spi3 and Rdi3 Chart-------------------------------------------------------------------------------------------
        Spi3= spiresults["Spi3"]['Spi_result']
        Rdi3= rdiresults["Rdi3"]['Rdi_result']
        plt.figure(figsize=(6*3.13,4*3.13))
        plt.subplot(211)
        plt.axhspan(0, -0.8, facecolor='0.5', alpha=0.5,color ='yellow',linewidth = True)
        plt.axhspan(-0.8, -1.3, facecolor='0.5', alpha=0.5,color ='orange',linewidth = True)
        plt.axhspan(-1.3, -1.6, facecolor='0.5', alpha=0.5,color ='orangered',linewidth = True)
        plt.axhspan(-1.6, -2, facecolor='0.5', alpha=0.5,color ='r',linewidth = True)
        plt.axhspan(-2, -3, facecolor='0.5', alpha=0.5,color ='maroon',linewidth = True)
        n=len(Spi3)
        ind = np.arange(n)
        width = 0.40
        rects1 = plt.bar(ind, Spi3, width, color='b', label='SPI ')
        rects2 = plt.bar(ind + width, Rdi3, width, color='lightblue', label='RDI ')
        plt.ylabel('Drought Index - std units')
        plt.title('{} {} Drought Propagation Chart for The Period {} - {}'.format('[3 Month]', stationname,minyear,maxyear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks = range(0,n,4)
        plt.xticks(myTicks,xYears,rotation='vertical')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax = n)
        plt.ylim([-3,3])
        plt.text( len(Spi3), 2.5, 'Wet Conditions', rotation=90)
        plt.text( len(Spi3), -0.5, 'Dry Conditions', rotation=90)
        plt.grid()
        #plt.show()
        plt.subplot(212)
        plt.scatter(Spi3,rain3_data,label='SPI ')
        plt.scatter(Rdi3,rain3_data,c="lightblue",label='RDI ')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        plt.savefig(appfolder+'static/charts'
                            '/'+stationName+'_3month.png')
        plt.close()

        # Spi6 and Rdi6 Chart-------------------------------------------------------------------------------------------
        plt.figure(figsize=(6*3.13,4*3.13))
        plt.subplot(211)
        Spi6= spiresults["Spi6"]['Spi_result']
        Rdi6= rdiresults["Rdi6"]['Rdi_result']
        plt.axhspan(0, -0.8, facecolor='0.5', alpha=0.5,color ='yellow',linewidth = True)
        plt.axhspan(-0.8, -1.3, facecolor='0.5', alpha=0.5,color ='orange',linewidth = True)
        plt.axhspan(-1.3, -1.6, facecolor='0.5', alpha=0.5,color ='orangered',linewidth = True)
        plt.axhspan(-1.6, -2, facecolor='0.5', alpha=0.5,color ='r',linewidth = True)
        plt.axhspan(-2, -3, facecolor='0.5', alpha=0.5,color ='maroon',linewidth = True)
        n=len(Spi6)
        ind = np.arange(n)
        width = 0.40
        rects1 = plt.bar(ind, Spi6, width, color='b', label='SPI')
        rects2 = plt.bar(ind + width, Rdi6, width, color='lightblue', label='RDI')
        plt.ylabel('Drought Index - std units')
        plt.title('{} {} Drought Propagation Chart for The Period {} - {}'.format('[6 Month]', stationname,minyear,maxyear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks = range(0,n,2)
        plt.xticks(myTicks,xYears,rotation='vertical')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax = n)
        plt.ylim([-3,3])
        plt.grid()
        plt.text( len(Spi6) , 2.5, 'Wet Conditions', rotation=90)
        plt.text( len(Spi6) , -0.5, 'Dry Conditions', rotation=90)
        #plt.show()
        plt.subplot(212)
        plt.scatter(Spi6,rain6_data,label='SPI')
        plt.scatter(Rdi6,rain6_data,c="r",label='RDI ')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        plt.savefig(appfolder+'static/charts'
                            '/'+stationName+'_6month.png')
        plt.close()
        # Spi12 and Rdi12 Chart-------------------------------------------------------------------------------------------
        Spi12= spiresults["Spi12"]['Spi_result']
        Rdi12= rdiresults["Rdi12"]['Rdi_result']
        plt.figure(figsize=(6*3.13,4*3.13))
        plt.subplot(211)
        n=len(Spi12)
        ind = np.arange(n)
        width = 0.40
        plt.axhspan(0, -0.8, facecolor='0.5', alpha=0.5,color ='yellow',linewidth = True)
        plt.axhspan(-0.8, -1.3, facecolor='0.5', alpha=0.5,color ='orange',linewidth = True)
        plt.axhspan(-1.3, -1.6, facecolor='0.5', alpha=0.5,color ='orangered',linewidth = True)
        plt.axhspan(-1.6, -2, facecolor='0.5', alpha=0.5,color ='r',linewidth = True)
        plt.axhspan(-2, -3, facecolor='0.5', alpha=0.5,color ='maroon',linewidth = True)
        rects1 = plt.bar(ind, Spi12, width, color='b', label='Spi ')
        rects2 = plt.bar(ind + width, Rdi12, width, color='lightblue', label='Rdi ')
        plt.ylabel('Drought Index - std units')
        plt.title('{} {} Drought Propagation Chart for The Period {} - {}'.format('[12 Month]', stationname,minyear,maxyear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks=range(0,n)
        plt.xticks(myTicks,xYears,rotation='vertical')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax = n)
        plt.ylim([-3,3])
        plt.grid()
        plt.text( len(Spi12), 2.5, 'Wet Conditions', rotation=90)
        plt.text( len(Spi12), -0.5, 'Dry Conditions', rotation=90)
        #plt.show()
        plt.subplot(212)
        plt.scatter(Spi12,rain12_data,label='SPI ')
        plt.scatter(Rdi12,rain12_data,c="lightblue",label='RDI')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        plt.savefig(appfolder+'static/charts'
                            '/'+stationName+'_12month.png')
        plt.close()
        print stationName, "Charts Update run completed... OK"
    except Exception, e:
        print "Drought Error for", stationName,":", e.message, e.args
Ejemplo n.º 40
0
    plt.title(rep)
    trace1 = sns.barplot(y='variance_explained',
                         x='PC',
                         data=PC_df[rep],
                         ax=ax1)
    sns.despine()
    ax1.xaxis.set_ticks(np.arange(0, 50, 10))
    ax1.xaxis.set_ticklabels(PC_df[rep]['PC'][0:51:10])
    trace2 = sns.stripplot(y='cum_variance_explained',
                           x='PC',
                           data=PC_df[rep],
                           ax=ax2)
    ax2.xaxis.set_ticks(np.arange(0, 50, 10))
    ax2.xaxis.set_ticklabels(PC_df[rep]['PC'][0:51:10])
    trace2 = plt.plot([cut_off[rep], cut_off[rep]], [0, 95], linewidth=2)
    plt.text(cut_off[rep], 100, str(cut_off[rep]))

    sns.despine()
    f.savefig(os.path.join(directory[0:-7], 'Figures',
                           rep + '_PC_variance_explained.tif'),
              dpi=400)
    f.show()
    del x, y, z, tot, var_exp, cum_var_exp, f, ax1, ax2, trace1, trace2

#basically need 35 eigenvectors to explain 95% of the variance - this was for only one experiment.

#%% now analyse how the features contribute to the eigenplanes
#see how the features contribute to the first 10 eigenvectors

#for each rep - do combined later
PC_feats = {}
Ejemplo n.º 41
0
                plt.plot(new_x,
                         sssp_utils.birch_murnaghan(new_x, E0_wien2k,
                                                    V0_wien2k,
                                                    B0_wien2k / 160.2176487,
                                                    B1_wien2k),
                         color='black',
                         label='WIEN2k')
                #plt.text(max(volumes)-1.0,max(energies)-0.010, 'Avg. total magnetization = {} Bohr mag. / cell \n'
                #         'Delta factor = {} meV/atom'.format(round(avg_magnetization,3),
                #        [p.get_dict()['delta'] for p in infos_for_pseudo if p.get_dict()['delta'] != None][0]),
                #         verticalalignment='center', horizontalalignment='center',fontsize=12)
                try:
                    plt.text(
                        reduce(lambda x, y: x + y, volumes) / len(volumes),
                        reduce(lambda x, y: x + y, energies) / len(energies),
                        '$\Delta$ = {} meV/atom'.format(
                            round(delta, 3), delta_units),
                        verticalalignment='center',
                        horizontalalignment='center',
                        fontsize=14)
                except KeyError:
                    pass

                plt.xlabel('Volume [A$^3$/atom]')
                plt.ylabel('Energy [eV/atom]')
                plt.title('EOS for ' + element + ' (' +
                          pseudo_dict[pseudo_family] + ')')
                plt.legend()
                plt.savefig(element + '_' + pseudo_dict[pseudo_family] +
                            '_eos.png')
                plt.close()
Ejemplo n.º 42
0
 def subtitle(*args, **kwargs):
     kwargs.setdefault('horizontalalignment', 'center')
     text(0.5 * (xlim()[1] - xlim()[0]), 0.9 * ylim()[1], *args,
          **kwargs)
Ejemplo n.º 43
0
    def plot(self, fig_number=322):
        """plot the stored data in figure `fig_number`.

        Dependencies: `matlabplotlib.pylab`
        """
        from matplotlib import pylab
        from matplotlib.pylab import (gcf, gca, figure, plot, xlabel, grid,
                                      semilogy, text, draw, show, ion, subplot
                                      as _subplot, tight_layout,
                                      rcParamsDefault, xlim, ylim)

        def title_(*args, **kwargs):
            kwargs.setdefault('size', rcParamsDefault['axes.labelsize'])
            pylab.title(*args, **kwargs)

        def subtitle(*args, **kwargs):
            kwargs.setdefault('horizontalalignment', 'center')
            text(0.5 * (xlim()[1] - xlim()[0]), 0.9 * ylim()[1], *args,
                 **kwargs)

        def legend_(*args, **kwargs):
            kwargs.setdefault('framealpha', 0.3)
            kwargs.setdefault('fancybox', True)
            kwargs.setdefault('fontsize', rcParamsDefault['font.size'] - 2)
            pylab.legend(*args, **kwargs)

        def subplot(*args, **kwargs):
            with _warnings.catch_warnings():
                _warnings.simplefilter("ignore")
                _subplot(
                    *args, **kwargs
                )  # catch unjustified deprecation warning because subplot and add_subplot are not separate

        if isinstance(fig_number, int):
            figure(fig_number)

        dat = self._data  # dictionary with entries as given in __init__
        if not dat or not dat['eval'] or len(dat['eval']) <= 2:
            return
        try:  # a hack to get the presumable population size lambda
            strpopsize = ' (evaluations / %s)' % str(dat['eval'][-2] -
                                                     dat['eval'][-3])
        except IndexError:
            strpopsize = ''

        # plot fit, Delta fit, sigma
        subplot(221)
        gca().clear()
        if dat['fit'][0] is None:  # plot is fine with None, but comput-
            dat['fit'][0] = dat['fit'][1]  # tations need numbers
            # should be reverted later, but let's be lazy
        assert dat['fit'].count(None) == 0
        fmin = min(dat['fit'])
        imin = dat['fit'].index(fmin)
        dat['fit'][imin] = max(dat['fit']) + 1
        fmin2 = min(dat['fit'])
        dat['fit'][imin] = fmin
        semilogy(dat['iter'],
                 [f - fmin if f - fmin > 1e-19 else None for f in dat['fit']],
                 'c',
                 linewidth=1,
                 label='f-min(f)')
        semilogy(dat['iter'], [
            max((fmin2 - fmin, 1e-19)) if f - fmin <= 1e-19 else None
            for f in dat['fit']
        ], 'C1*')

        semilogy(dat['iter'], [abs(f) for f in dat['fit']],
                 'b',
                 label='abs(f-value)')
        semilogy(dat['iter'], dat['sigma'], 'g', label='sigma')
        semilogy(dat['iter'][imin], abs(fmin), 'r*', label='abs(min(f))')
        if dat['more_data']:
            gca().twinx()
            plot(dat['iter'], dat['more_data'])
        grid(True)
        legend_(*[
            [v[i] for i in [1, 0, 2, 3]]  # just a reordering
            for v in gca().get_legend_handles_labels()
        ])

        # plot xmean
        subplot(222)
        gca().clear()
        plot(dat['iter'], dat['xmean'])
        for i in range(len(dat['xmean'][-1])):
            text(dat['iter'][0], dat['xmean'][0][i], str(i))
            text(dat['iter'][-1], dat['xmean'][-1][i], str(i))
        subtitle('mean solution')
        grid(True)

        # plot squareroot of eigenvalues
        if dat['D'][-1][0] != dat['D'][-1][-1]:
            subplot(223)
            gca().clear()
            semilogy(dat['iter'], dat['D'], 'm')
            xlabel('iterations' + strpopsize)
            title_('Axis lengths')
            grid(True)

        # plot stds
        subplot(224)
        # if len(gcf().axes) > 1:
        #     sca(pylab.gcf().axes[1])
        # else:
        #     twinx()
        gca().clear()
        semilogy(dat['iter'], dat['stds'])
        for i in range(len(dat['stds'][-1])):
            text(dat['iter'][-1], dat['stds'][-1][i], str(i))
        title_('Coordinate-wise STDs w/o sigma')
        grid(True)
        xlabel('iterations' + strpopsize)
        _stdout.flush()
        tight_layout()  # avoid superfluous padding
        # draw(), show()  # canvas.draw seem to do the job better
        ion()  # may prevent that everything stops until figure is closed?
        # todo: if in the same cell, the subplots are small until the cell is finished
        gcf().canvas.draw()
        CMAESDataLogger.plotted += 1
Ejemplo n.º 44
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 period.
        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
Ejemplo n.º 45
0
p0err=np.sqrt( covar[0][0] )
p1err= np.sqrt( covar[1][1] )

pfinal

Y=[]    ##Tiene problemitas el jupyter
for i in logx:
    Y.append(pfinal[0]+pfinal[1]*i)
plt.plot(logx, logy, "ro", label="Data")
plt.plot(logx, Y, "k-", label="Ajuste")
plt.scatter(np.log10(K_in[1][2:]),np.log10(K_in[0][1:]), label="Bineo lineal")
plt.xlabel("Logaritmo del grado")
plt.legend()
plt.ylabel("Logaritmo del número de ocurrencias")
plt.title('Gráfico 7: Distribución de grado y su ajuste lineal ')
plt.text(0, -0, 'Coeficiente ppal = %5.2f +/- %5.2f' % (pfinal[1], p1err))
plt.text(0, -.5, 'Ordenada = %5.2f +/- %5.2f' % (pfinal[0], p0err))
plt.show()
#%%Matriz de adyacencia
M=nx.adjacency_matrix(G_en_antes) ##el type es scipy.sparse.csr.csr_matrix
#Nos podemos mover fácilmente por los vecinos out de un nodo
M.indices

#%%
#def NodoGrado(Grafo):
#    Nodos=[] #lista con grado-nodo
#    for n in Grafo.nodes():
#        Nodos.append([Grafo.in_degree(n),n])
#    sorted(Nodos)
#    return Nodos 
Ejemplo n.º 46
0
from ctapipe import io
from ctapipe.visualization import ArrayDisplay
import matplotlib.pylab as plt

if __name__ == '__main__':

    plt.style.use("ggplot")
    layout = io.get_array_layout("hess")
    X = layout['POSX']
    Y = layout['POSY']
    A = layout['MIRAREA']
    A[:] = 132

    ad = ArrayDisplay(X, Y, A, title="HESS")

    # label them
    for tel in layout:
        name = "CT{tid}:{tclass}".format(tid=tel['TELID'],
                                         tclass=io.tel_class_name(
                                             tel['CLASS']))
        plt.text(tel['POSX'], tel['POSY'], name)

    ad.axes.set_xlim(-300, 300)
    ad.axes.set_ylim(-300, 300)
    plt.show()
Ejemplo n.º 47
0
def get_app_phot(coords, image, plot_only=False, store=True, wcsin="world", fwhm=2.5, plotdir=None, box=15, arcsecpix=0.394):
    '''
    coords: files: 
    wcsin: can be "world", "logic"
    '''
    # Load packages; splot is in the onedspec package, which is in noao. 
    # The special keyword _doprint=0 turns off displaying the tasks 
    # when loading a package. 
    
    from pyraf import iraf 

    if (not plot_only):
        iraf.noao(_doprint=0)
        iraf.digiphot(_doprint=0)
        iraf.apphot(_doprint=0)
        iraf.unlearn("apphot")

    imdir = os.path.dirname(image)
    imname = os.path.basename(image)
    
    if (plotdir is None):
        plotdir = os.path.join(imdir, "photometry")
    
    if not os.path.isdir(plotdir):
        os.makedirs(plotdir)
        
    out_name = os.path.join(plotdir, imname +  ".seq.mag")
    clean_name = os.path.join(plotdir, imname +  ".app.mag")

    print "Will create output files", out_name, clean_name

    # Read values from .ec file
    
    fwhm_value = fwhm/arcsecpix
    
    if (fitsutils.has_par(image, 'FWHM')):
        fwhm_value = fitsutils.get_par(image, 'FWHM')/arcsecpix
    elif (fwhm is None):
        fwhm_value=3.5/arcsecpix
    if (fitsutils.has_par(image, 'AIRMASS')):
        airmass_value = fitsutils.get_par(image, 'AIRMASS')
    else:
	airmass_value = 1.3
 
    exptime = fitsutils.get_par(image, 'EXPTIME')
    gain = fitsutils.get_par(image, 'GAIN')
    noise = fitsutils.get_par(image, 'RDNOISE')

    
    print "FWHM: %.1f pixels, %.1f arcsec"%(fwhm_value, fwhm_value*arcsecpix)
    aperture_rad = math.ceil(float(fwhm_value)*1.5)      # Set aperture radius to three times the PSF radius
    sky_rad= math.ceil(aperture_rad)*4
    

    if (not plot_only):

        iraf.noao.digiphot.apphot.qphot(image = image,\
        cbox = box ,\
        annulus = sky_rad ,\
        dannulus = 20. ,\
        aperture = str(aperture_rad),\
        coords = coords ,\
        output = out_name ,\
        plotfile = "" ,\
        zmag = 0. ,\
        exposure = "exptime",\
        airmass = "airmass" ,\
        filter = "filter" ,\
        obstime = "DATE" ,\
        #fwhm = fwhm_value,\
        epadu = gain ,\
        interactive = "no" ,\
        radplots = "yes" ,\
        verbose = "no" ,\
        graphics = "stdgraph" ,\
        display = "stdimage" ,\
        icommands = "" ,\
        wcsin = wcsin,
        wcsout = "logical",
        gcommands = "") 
        
         
        #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name)
        iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,cier,rapert,sum,area,nsky,flux,itime,mag,merr", "yes", Stdout=clean_name)
        
    
    ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"),  ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("msky","<f4"), ("stdev","<f4"),\
        ("flags", np.int), ("rapert", "<f4"), ("sum", "<f4"), ("area", "<f4"), ("nsky","<f4") , ("flux", "<f4"), ("itime", "<f4"), ("fit_mag","<f4"), ("fiterr","<f4")])
    if (ma.size > 0):    
        m = ma[~np.isnan(ma["fit_mag"])]
    else:
        print "Only one object found!"
        m = np.array([ma])
        
    hdulist = pf.open(image)
    prihdr = hdulist[0].header
    img = hdulist[0].data * 1.
    nx, ny = img.shape

    
    
    dimX = int(np.floor(np.sqrt(len(m))))
    dimY = int(np.ceil(len(m)*1./dimX))
    outerrad = sky_rad+10
    cutrad = outerrad + 15
    
    print "Cutrad %.1f"%cutrad

    plt.suptitle("FWHM=%.2f arcsec. %d stars"%(fwhm_value*arcsecpix, len(m)))
    for i in np.arange(dimX):
        for j in np.arange(dimY):
            if ( i*dimY + j < len(m)):
                k = i*dimY + j
		#print dimX, dimY, i, j, k
                ax = plt.subplot2grid((dimX,dimY),(i, j))
                y1, y2, x1, x2 = m[k]["X"]-cutrad, m[k]["X"]+cutrad, m[k]["Y"]-cutrad, m[k]["Y"]+cutrad
                y1, y2, x1, x2 = int(y1), int(y2), int(x1), int(x2)
                y1 = np.maximum(y1, 0); y2=np.maximum(y2, 0); x1=np.maximum(x1, 0); x2 = np.maximum(x2, 0)
                try:
                    zmin, zmax = zscale.zscale(img[x1:x2,y1:y2], nsamples=1000, contrast=0.25)
                except ValueError:
		    print y1, y2, x1, x2 
		    print img[x1:x2,y1:y2]
                    sh= img[x1:x2,y1:y2].shape
                    if sh[0]>0 and sh[1]>0:
                        zmin = np.nanmin(img[x1:x2,y1:y2])
                        zmax = np.nanmax(img[x1:x2,y1:y2])
                ax.imshow(img[x1:x2,y1:y2], aspect="equal", extent=(-cutrad, cutrad, -cutrad, cutrad), origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=zmin, vmax=zmax)
                c1 = plt.Circle( (0, 0), edgecolor="r", facecolor="none", radius=aperture_rad)
                c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad)
                c3 = plt.Circle( (0, 0), edgecolor="yellow", facecolor="none", radius=sky_rad+20)
                plt.gca().add_artist(c1)
                plt.gca().add_artist(c2)
                plt.gca().add_artist(c3)
                ax.set_xticks([])
                ax.set_yticks([])
        
                plt.text(+5, +5, "%d"%m[k]["id"])
                plt.text(-cutrad, -cutrad, "%.2f$\pm$%.2f"%(m[k]["fit_mag"], m[k]["fiterr"]), color="b")
		
    plt.tight_layout()
    plt.savefig(os.path.join(plotdir, imname + "plot.png"))
    plt.clf()
#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)/100.0
#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(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/\tau_{Dyn}^{M-}$',fontsize=15)
#plt.xlabel('Delay Steps',fontsize=9)
ax1.set_xticklabels([])
plt.ylabel(r'$C$',fontsize=15)
plt.xlim(0,0.25)
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)

#savefilename='SC_galpy0718_1000timesteps_3000_orbits.png'
#savefilename='SC_galpy0718_1000timesteps_all_orbits.png'
#savefile = os.path.normpath(datadir+savefilename)
#plt.savefig(savefile,dpi=300,facecolor='w',edgecolor='k')


#fig=plt.figure(num=2,figsize=(5,3.5),dpi=300,facecolor='w',edgecolor='k')
#plt.subplots_adjust(left=left, bottom=bottom, right=right, top=top, wspace=wspace, hspace=hspace)

plt.rc('lines',markersize=8,markeredgewidth=0.0,linewidth=2.0)
ax1=plt.subplot(2,1,2)
plt.plot(timeindex_normM4[:SCsCR4_endarray-1],PEsCR4[1:SCsCR4_endarray],marker=points[1],markevery=50,linestyle='dashed',color='blue',label='M4')
plt.plot(timeindex_normM5,PEsCR5[1:],marker=points[1],markevery=50,color='red',linestyle='solid',label='M5')
plt.plot(timeindex_normM6,PEsCR6[1:],marker=points[1],markevery=50,color='green',linestyle='dotted',label='M6')
Ejemplo n.º 49
0
indexes = []
values = []
dates = []
names = df2.Name
print(names)
for date2 in df2.Date:
    print(df1.index[df1.Date == date2], df1.index[df1.Date == date2][0])
    indexes.append(df1.index[df1.Date == date2][0])
    values.append(df1["Close"][df1.index[df1.Date == date2][0]])
    dates.append(df1["Date"][df1.index[df1.Date == date2][0]])
print(indexes, values)




mean = df1["Close"].mean()

plt.figure("Apple Stock and Iphone Launch")

plt.plot(df1["Date"], df1["Close"], 'r-', linewidth=0.6, label="Stock price, mean="+str(mean))
#plt.plot(df1["Date"], df1["Close"], '-o', ms=7, markevery=indexes, linewidth=0, label="Iphone launch date")
plt.plot(dates, values, 'o', label="Iphone launch date")
# put the values
for i in range(len(dates)):
    plt.text(dates[i], values[i], str(int(values[i])))
plt.xlabel("Dates")
plt.legend(loc="upper left")



plt.show()
Ejemplo n.º 50
0
ax = pl.axes([0.07, 0.15, 0.95 - 0.1, 0.95 - 0.1])
im = ax.imshow(spindens, cmap='RdBu_r')
pl.yticks([0, 20, 40, 60, 80])
pl.xlabel('$y$\,[$\mu$m]')
pl.ylabel('$x$\,[$\mu$m]')
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.1)
#pl.yticks([0,20,40,60,80])
#ax = pl.gca()
#ax.xaxis.set_label_coords(0.5, -0.05)
#pl.text(0.85, -0.55, r'$n\times 10^{17}$', fontsize=8, transform = pl.gca().transAxes)
#pl.colorbar(orientation='horizontal')
#pl.colorbar()
cbar = pl.colorbar(im, cax, ticks=[-0.3, 0, 0.3])
cbar.ax.set_yticklabels(['-1', '0', '1'])
pl.text(xshift, 0.85, r'$n$ [a.u.]', fontsize=10, transform=pl.gca().transAxes)
fig.savefig('images/spinsplitme.pdf', transparent='true')
# -------------------------------------
fig_width_pt = 448.13095  # Get this from LaTeX using \showthe\columnwidth
fig_height_pt = 150  # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0 / 72.27  # Convert pt to inch
#golden_mean = (np.sqrt(5)-1.0)/2.0         # Aesthetic ratio
fig_width = fig_width_pt * inches_per_pt  # width in inches
fig_height = fig_height_pt * inches_per_pt  # width in inches
#fig_height = fig_width*golden_mean      # height in inches
fig_size = [fig_width, fig_height]
params = {
    'backend': 'pdf',
    'font.family': 'serif',
    'font.serif': 'computer modern roman',
    'axes.labelsize': 10,
Ejemplo n.º 51
0
# A is better than B. Plots true positive rate, (tpr) vs false positive
# rate, (fpr).

import numpy as np
import matplotlib.pylab as pl

fA = np.vectorize(lambda x: x**(1.0 / 3))
fB = np.vectorize(lambda x: x**(2.0 / 3))
x = np.arange(0, 1, 0.01)

pl.plot(x, fA(x), 'r-')
pl.plot(x, fB(x), 'b-')
pl.fill_between(x, fB(x), 0, facecolor='blue')
pl.plot(x, 1 - x, 'k-')

inter_a = 0.3177
# found using scipy.optimize.fsolve(x**(1.0/3)+x-1, 0)
inter_b = 0.4302
# found using scipy.optimize.fsolve(x**(2.0/3)+x-1, 0)

pl.plot(inter_a, fA(inter_a), 'ro')
pl.plot(inter_b, fB(inter_b), 'bo')

pl.text(inter_a, fA(inter_a) + 0.1, 'A', color='red', size='x-large')
pl.text(inter_b, fB(inter_b) + 0.1, 'B', color='blue', size='x-large')

pl.xlabel('FPR', size='xx-large')
pl.ylabel('TPR', size='xx-large')
pl.savefig('ROChand.png')
pl.show()
Ejemplo n.º 52
0
def plot_wavelet(array,
                 shotdata,
                 Bpwr,
                 Bscalespec,
                 wvfreq,
                 Bfft,
                 fftfreq,
                 time,
                 timerange=[0.0, 100.0],
                 pwr_lims=[-40, -10],
                 min_freq=500e3,
                 showPlot=True,
                 savePlot=True):
    plt.ioff(
    )  #Turn interactive mode off so that plots don't appear automatically without plt.show()

    #Create figure
    #facecolor = white, edgecolor = black
    fig = plt.figure(num=1,
                     figsize=(9.25, 4.6),
                     dpi=200,
                     facecolor='w',
                     edgecolor='k')

    #Create axes for 2D plot
    #ax=plt.axes([fromleft,frombottom,width,height])
    ax = plt.axes([0.3, 0.105, 0.68, 0.55])
    plt.xlabel(r't [$\mu$s]', fontsize=12)
    plt.ylabel(r'$f$ [Hz]', fontsize=12)

    #prepare 2D array for plotting (flip and take log)
    plotcwt = Bpwr[0:-1]
    logplotcwt = np.log10(np.flipud(plotcwt))

    #create 2D image

    if not pwr_lims:
        print('Min max range')
        vmin = logplotcwt.min()
        vmax = logplotcwt.max()
        print(vmin)
        print(vmax)
        im = plt.imshow(logplotcwt,
                        extent=[time[0], time[-1], wvfreq[0], wvfreq[-1]],
                        vmin=vmin,
                        vmax=vmax,
                        aspect='auto')
    if pwr_lims:
        print('user range')
        im = plt.imshow(logplotcwt,
                        extent=[time[0], time[-1], wvfreq[0], wvfreq[-1]],
                        vmin=pwr_lims[0],
                        vmax=pwr_lims[1],
                        aspect='auto')

    #modify axis settings
    ax.set_yscale('log')
    plt.yticks(fontsize=6)
    plt.ylim(min_freq, wvfreq[0])

    #Create axes for time series plot (with x axis shared with 2Dplot)
    ax2 = plt.axes([0.3, 0.7, 0.68, 0.25], sharex=ax)

    #create time series image
    plt.plot(time, array, linewidth=0.5, color='blue')

    #modify axis settings
    plt.ylabel('B-dot', fontsize=12)
    plt.yticks(fontsize=6)
    plt.xticks(np.arange(0, 110, 10), fontsize=6)
    plt.xlim(timerange[0], timerange[1])

    #create total power spectrum comparison axes
    ax3 = plt.axes([0.03, 0.105, 0.2, 0.75])

    #plot total power spectra
    plt.loglog(fftfreq, Bfft, color='orange', linewidth=0.5, label='FFT')
    plt.loglog(wvfreq, Bscalespec, color='red', linewidth=1, label='Wavelet')

    #modify axis settings
    plt.xlabel(r'$f$ [Hz]', fontsize=12)
    plt.xticks(fontsize=6)
    plt.yticks(fontsize=6)
    plt.xlim(fftfreq[0], 5.0 * fftfreq[-1])

    #make simple legend
    plt.legend(loc='lower left', fontsize=7)

    #label plot
    day = shotdata[0]
    nn = shotdata[1]  #shot number
    axis_label = shotdata[2]  #axis (r,t,z)
    channel = shotdata[3]  #probe channel number
    plt.text(0.15,
             0.92,
             "Shot " + str(nn) + axis_label + " Chan:" + str(channel),
             fontsize=10,
             bbox=dict(facecolor='green', alpha=0.2),
             transform=fig.transFigure,
             horizontalalignment='center')
    plt.text(0.30,
             0.01,
             "Data Date: " + day,
             fontsize=6,
             transform=fig.transFigure,
             horizontalalignment='center')

    if showPlot and not savePlot: plt.show()
    if savePlot:
        process_dir = 'C:/Users/David Schaffner/Documents/ssxpython/plots/WaveletOutputDatabase/'  #run073013_1mwb_single/chan1/'
        filename = 'wavelet_' + day + '_shot' + str(
            nn) + '_B' + axis_label[5] + '_chan' + str(channel) + '.png'
        savefile = os.path.normpath(process_dir + filename)
        #save figure with facecolor=white, edgecolor = black
        plt.savefig(savefile, dpi=150, facecolor='w', edgecolor='k')
        plt.clf()
        plt.close(fig)
Ejemplo n.º 53
0
plt.subplots_adjust(left=0.1,
                    bottom=0.05,
                    right=0.9,
                    top=0.95,
                    wspace=0,
                    hspace=0)
plt.subplot(gs[0, 0:3])
plt.plot(time, sst, 'k')
plt.xlim(xlim[:])
plt.xlabel('Time (year)')
plt.ylabel('NINO3 SST (\u00B0C)')
plt.title('a) NINO3 Sea Surface Temperature (seasonal)')

plt.text(time[-1] + 35,
         0.5,
         'Wavelet Analysis\nC. Torrence & G.P. Compo\n' +
         'http://paos.colorado.edu/\nresearch/wavelets/',
         horizontalalignment='center',
         verticalalignment='center')

#--- Contour plot wavelet power spectrum
# plt3 = plt.subplot(3, 1, 2)
plt3 = plt.subplot(gs[1, 0:3])
levels = [0, 0.5, 1, 2, 4, 999]
CS = plt.contourf(time, period, power, len(levels))  #*** or use 'contour'
im = plt.contourf(CS,
                  levels=levels,
                  colors=['white', 'bisque', 'orange', 'orangered', 'darkred'])
plt.xlabel('Time (year)')
plt.ylabel('Period (years)')
plt.title('b) Wavelet Power Spectrum (contours at 0.5,1,2,4\u00B0C$^2$)')
plt.xlim(xlim[:])
Ejemplo n.º 54
0
        # If there are only 3 landmarks don't need user input
        simplices = np.array([valid_lms])
        notHappy = False
        break
    else:
        triangles = Delaunay(last_lms_valid, incremental=True)
        simplices = triangles.simplices  # Indices of the points in each triangulation
        # Remap simplices to valid landmark ids
        remap = lambda x: valid_lms_arr[x]
        simplices = np.apply_along_axis(remap, 0, simplices)

    # Visual check for triangulation
    plt.gca().clear()
    plt.triplot(last_lms[:, 0], last_lms[:, 1], simplices.copy())
    for i in valid_lms:
        plt.text(*last_lms[i, :], s=str(i))
    plt.draw()
    plt.pause(0.01)

    if check_triangulation:
        remove_str = input("Enter the IDs of landmarks to be removed: ")
        try:
            remove = ast.literal_eval(remove_str)
        except Exception as e:
            print("Error understanding input:", e)
            remove = ()
            notHappy = False

        # If only one number entered
        if type(remove) is int:
            remove = (remove, )
Ejemplo n.º 55
0
             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.text(
             -60, 20,
             'JSD Loss: %f, NELBO: %f, KL0: %f, KL1: %f, KL2: %f, KL3: %f, KL4: %f'
             % (dl, NELBO, KL0, KL1, KL2, KL3, KL4))
         plt.text(-28, -7, 'KLAVG: %f' % (KLAVG))
         plt.savefig('FiguresJCADV\Fig %i' % (j))
         plt.close()
 plt.subplot(2, 1, 1)
 plt.plot(KLAVGBRUH)
 plt.xlabel('Iterations (x100)')
 plt.ylabel('Avg KL Div')
 plt.subplot(2, 1, 2)
 plt.plot(ISTHISLOSS)
 plt.xlabel('Iterations (x100)')
 plt.ylabel('JSD Loss')
 plt.subplots_adjust(top=0.95, bottom=0.15, right=0.95, hspace=0.4)
 plt.savefig('FiguresJCADV\Diag Plot', bbox_inches='tight')
 plt.close()
def genmprim_unicycle(outfilename, visualize=False, separate_plots=False):
    visualize = matplotlib_found and visualize  # Plot the primitives

    # Local Variables: basemprimendpts22p5_c, endtheta_c, endx_c, baseendpose_c, additionalactioncostmult, fout, numofsamples, basemprimendpts45_c, primind, basemprimendpts0_c, rv, angle, outfilename, numberofangles, startpt, UNICYCLE_MPRIM_16DEGS, sidestepcostmult, rotation_angle, basemprimendpts_c, forwardandturncostmult, forwardcostmult, turninplacecostmult, endpose_c, backwardcostmult, interpfactor, S, R, tvoverrv, dtheta, intermcells_m, tv, dt, currentangle, numberofprimsperangle, resolution, currentangle_36000int, l, iind, errorxy, interind, endy_c, angleind, endpt
    # Function calls: plot, cos, pi, grid, figure, genmprim_unicycle, text, int2str, basemprimendpts33p75_c, pause, axis, sin, pinv, basemprimendpts11p25_c, fprintf, fclose, rem, zeros, fopen, round, size
    #%
    #%generates motion primitives and saves them into file
    #%
    #%written by Maxim Likhachev
    #%---------------------------------------------------
    #%
    #%defines
    UNICYCLE_MPRIM_16DEGS = 1.0
    if UNICYCLE_MPRIM_16DEGS == 1.0:
        resolution = 0.05
        numberofangles = 16
        #%preferably a power of 2, definitely multiple of 8
        numberofprimsperangle = 7
        #%multipliers (multiplier is used as costmult*cost)
        forwardcostmult = 1.0
        backwardcostmult = 40.0
        forwardandturncostmult = 2.0
        sidestepcostmult = 10.0
        turninplacecostmult = 20.0
        #%note, what is shown x,y,theta changes (not absolute numbers)
        #%0 degreees
        basemprimendpts0_c = np.zeros((numberofprimsperangle, 4))
        #%x,y,theta,costmult
        #%x aligned with the heading of the robot, angles are positive
        #%counterclockwise
        #%0 theta change
        basemprimendpts0_c[0, :] = np.array(
            np.hstack((1.0, 0.0, 0.0, forwardcostmult)))
        basemprimendpts0_c[1, :] = np.array(
            np.hstack((8.0, 0.0, 0.0, forwardcostmult)))
        basemprimendpts0_c[2, :] = np.array(
            np.hstack((-1.0, 0.0, 0.0, backwardcostmult)))
        #%1/16 theta change
        basemprimendpts0_c[3, :] = np.array(
            np.hstack((8.0, 1.0, 1.0, forwardandturncostmult)))
        basemprimendpts0_c[4, :] = np.array(
            np.hstack((8.0, -1.0, -1.0, forwardandturncostmult)))
        #%turn in place
        basemprimendpts0_c[5, :] = np.array(
            np.hstack((0.0, 0.0, 1.0, turninplacecostmult)))
        basemprimendpts0_c[6, :] = np.array(
            np.hstack((0.0, 0.0, -1.0, turninplacecostmult)))
        #%45 degrees
        basemprimendpts45_c = np.zeros((numberofprimsperangle, 4))
        #%x,y,theta,costmult (multiplier is used as costmult*cost)
        #%x aligned with the heading of the robot, angles are positive
        #%counterclockwise
        #%0 theta change
        basemprimendpts45_c[0, :] = np.array(
            np.hstack((1.0, 1.0, 0.0, forwardcostmult)))
        basemprimendpts45_c[1, :] = np.array(
            np.hstack((6.0, 6.0, 0.0, forwardcostmult)))
        basemprimendpts45_c[2, :] = np.array(
            np.hstack((-1.0, -1.0, 0.0, backwardcostmult)))
        #%1/16 theta change
        basemprimendpts45_c[3, :] = np.array(
            np.hstack((5.0, 7.0, 1.0, forwardandturncostmult)))
        basemprimendpts45_c[4, :] = np.array(
            np.hstack((7.0, 5.0, -1.0, forwardandturncostmult)))
        #%turn in place
        basemprimendpts45_c[5, :] = np.array(
            np.hstack((0.0, 0.0, 1.0, turninplacecostmult)))
        basemprimendpts45_c[6, :] = np.array(
            np.hstack((0.0, 0.0, -1.0, turninplacecostmult)))
        #%22.5 degrees
        basemprimendpts22p5_c = np.zeros((numberofprimsperangle, 4))
        #%x,y,theta,costmult (multiplier is used as costmult*cost)
        #%x aligned with the heading of the robot, angles are positive
        #%counterclockwise
        #%0 theta change
        basemprimendpts22p5_c[0, :] = np.array(
            np.hstack((2.0, 1.0, 0.0, forwardcostmult)))
        basemprimendpts22p5_c[1, :] = np.array(
            np.hstack((6.0, 3.0, 0.0, forwardcostmult)))
        basemprimendpts22p5_c[2, :] = np.array(
            np.hstack((-2.0, -1.0, 0.0, backwardcostmult)))
        #%1/16 theta change
        basemprimendpts22p5_c[3, :] = np.array(
            np.hstack((5.0, 4.0, 1.0, forwardandturncostmult)))
        basemprimendpts22p5_c[4, :] = np.array(
            np.hstack((7.0, 2.0, -1.0, forwardandturncostmult)))
        #%turn in place
        basemprimendpts22p5_c[5, :] = np.array(
            np.hstack((0.0, 0.0, 1.0, turninplacecostmult)))
        basemprimendpts22p5_c[6, :] = np.array(
            np.hstack((0.0, 0.0, -1.0, turninplacecostmult)))
    else:
        print('ERROR: undefined mprims type\n')
        return []

    fout = open(outfilename, 'w')
    #%write the header
    fout.write('resolution_m: %f\n' % (resolution))
    fout.write('numberofangles: %d\n' % (numberofangles))
    fout.write('totalnumberofprimitives: %d\n' %
               (numberofprimsperangle * numberofangles))
    #%iterate over angles
    for angleind in np.arange(1.0, (numberofangles) + 1):
        currentangle = ((angleind - 1) * 2.0 * np.pi) / numberofangles
        currentangle_36000int = np.round(
            (angleind - 1) * 36000.0 / numberofangles)
        if visualize:
            if separate_plots:
                fig = plt.figure(angleind)
                plt.title('angle {:2.0f} (= {:3.1f} degrees)'.format(
                    angleind - 1, currentangle_36000int / 100.0))
            else:
                fig = plt.figure(1)

            plt.axis('equal')
            plt.axis([
                -10 * resolution, 10 * resolution, -10 * resolution,
                10 * resolution
            ])
            ax = fig.add_subplot(1, 1, 1)
            major_ticks = np.arange(-8 * resolution, 9 * resolution,
                                    4 * resolution)
            minor_ticks = np.arange(-8 * resolution, 9 * resolution,
                                    resolution)
            ax.set_xticks(major_ticks)
            ax.set_xticks(minor_ticks, minor=True)
            ax.set_yticks(major_ticks)
            ax.set_yticks(minor_ticks, minor=True)
            ax.grid(which='minor', alpha=0.5)
            ax.grid(which='major', alpha=0.9)

        #%iterate over primitives
        for primind in np.arange(1.0, (numberofprimsperangle) + 1):
            fout.write('primID: %d\n' % (primind - 1))
            fout.write('startangle_c: %d\n' % (angleind - 1))
            #%current angle
            #%compute which template to use
            if (currentangle_36000int % 9000) == 0:
                basemprimendpts_c = basemprimendpts0_c[int(primind) - 1, :]
                angle = currentangle
            elif (currentangle_36000int % 4500) == 0:
                basemprimendpts_c = basemprimendpts45_c[int(primind) - 1, :]
                angle = currentangle - 45.0 * np.pi / 180.0

            elif ((currentangle_36000int - 7875) % 9000) == 0:
                basemprimendpts_c = (
                    1 * basemprimendpts33p75_c[primind, :]
                )  # 1* to force deep copy to avoid reference update below
                basemprimendpts_c[0] = basemprimendpts33p75_c[primind, 1]
                #%reverse x and y
                basemprimendpts_c[1] = basemprimendpts33p75_c[primind, 0]
                basemprimendpts_c[2] = -basemprimendpts33p75_c[primind, 2]
                #%reverse the angle as well
                angle = currentangle - (78.75 * np.pi) / 180.0
                print('78p75\n')

            elif ((currentangle_36000int - 6750) % 9000) == 0:
                basemprimendpts_c = (
                    1 * basemprimendpts22p5_c[int(primind) - 1, :]
                )  # 1* to force deep copy to avoid reference update below
                basemprimendpts_c[0] = basemprimendpts22p5_c[int(primind) - 1,
                                                             1]
                #%reverse x and y
                basemprimendpts_c[1] = basemprimendpts22p5_c[int(primind) - 1,
                                                             0]
                basemprimendpts_c[2] = -basemprimendpts22p5_c[int(primind) - 1,
                                                              2]
                #%reverse the angle as well
                # print('%d : %d %d %d onto %d %d %d\n'%(primind-1,basemprimendpts22p5_c[int(primind)-1,0], basemprimendpts22p5_c[int(primind)-1,1], basemprimendpts22p5_c[int(primind)-1,2], basemprimendpts_c[0], basemprimendpts_c[1], basemprimendpts_c[2]))
                angle = currentangle - (67.5 * np.pi) / 180.0
                print('67p5\n')

            elif ((currentangle_36000int - 5625) % 9000) == 0:
                basemprimendpts_c = (
                    1 * basemprimendpts11p25_c[primind, :]
                )  # 1* to force deep copy to avoid reference update below
                basemprimendpts_c[0] = basemprimendpts11p25_c[primind, 1]
                #%reverse x and y
                basemprimendpts_c[1] = basemprimendpts11p25_c[primind, 0]
                basemprimendpts_c[2] = -basemprimendpts11p25_c[primind, 2]
                #%reverse the angle as well
                angle = currentangle - (56.25 * np.pi) / 180.0
                print('56p25\n')

            elif ((currentangle_36000int - 3375) % 9000) == 0:
                basemprimendpts_c = basemprimendpts33p75_c[int(primind), :]
                angle = currentangle - (33.75 * np.pi) / 180.0
                print('33p75\n')

            elif ((currentangle_36000int - 2250) % 9000) == 0:
                basemprimendpts_c = basemprimendpts22p5_c[int(primind) - 1, :]
                angle = currentangle - (22.5 * np.pi) / 180.0
                print('22p5\n')

            elif ((currentangle_36000int - 1125) % 9000) == 0:
                basemprimendpts_c = basemprimendpts11p25_c[int(primind), :]
                angle = currentangle - (11.25 * np.pi) / 180.0
                print('11p25\n')

            else:
                print('ERROR: invalid angular resolution. angle = %d\n' %
                      currentangle_36000int)
                return []

            #%now figure out what action will be
            baseendpose_c = basemprimendpts_c[0:3]
            additionalactioncostmult = basemprimendpts_c[3]
            endx_c = np.round((baseendpose_c[0] * np.cos(angle)) -
                              (baseendpose_c[1] * np.sin(angle)))
            endy_c = np.round((baseendpose_c[0] * np.sin(angle)) +
                              (baseendpose_c[1] * np.cos(angle)))
            endtheta_c = np.fmod(angleind - 1 + baseendpose_c[2],
                                 numberofangles)
            endpose_c = np.array(np.hstack((endx_c, endy_c, endtheta_c)))
            print("endpose_c=", endpose_c)
            print(('rotation angle=%f\n' % (angle * 180.0 / np.pi)))
            # if np.logical_and(baseendpose_c[1] == 0., baseendpose_c[2] == 0.):
            #%fprintf(1, 'endpose=%d %d %d\n', endpose_c(1), endpose_c(2), endpose_c(3));

            #%generate intermediate poses (remember they are w.r.t 0,0 (and not
            #%centers of the cells)
            numofsamples = 10
            intermcells_m = np.zeros((numofsamples, 3))
            if UNICYCLE_MPRIM_16DEGS == 1.0:
                startpt = np.array(np.hstack((0.0, 0.0, currentangle)))
                endpt = np.array(
                    np.hstack((
                        (endpose_c[0] * resolution),
                        (endpose_c[1] * resolution),
                        (((np.fmod(angleind - 1 + baseendpose_c[2],
                                   numberofangles)) * 2.0 * np.pi) /
                         numberofangles),
                    )))

                print("startpt =", startpt)
                print("endpt   =", endpt)
                intermcells_m = np.zeros((numofsamples, 3))
                if np.logical_or(np.logical_and(endx_c == 0.0, endy_c == 0.0),
                                 baseendpose_c[2] == 0.0):
                    #%turn in place or move forward
                    for iind in np.arange(1.0, (numofsamples) + 1):
                        fraction = float(iind - 1) / (numofsamples - 1)
                        intermcells_m[int(iind) - 1, :] = np.array((
                            startpt[0] + (endpt[0] - startpt[0]) * fraction,
                            startpt[1] + (endpt[1] - startpt[1]) * fraction,
                            0,
                        ))
                        rotation_angle = baseendpose_c[2] * (2.0 * np.pi /
                                                             numberofangles)
                        intermcells_m[int(iind) - 1, 2] = np.fmod(
                            startpt[2] + rotation_angle * fraction,
                            (2.0 * np.pi))
                        # print " ",iind,"  of ",numofsamples," fraction=",fraction," rotation=",rotation_angle

                else:
                    #%unicycle-based move forward or backward  (http://sbpl.net/node/53)
                    R = np.array(
                        np.vstack((
                            np.hstack((np.cos(startpt[2]),
                                       np.sin(endpt[2]) - np.sin(startpt[2]))),
                            np.hstack(
                                (np.sin(startpt[2]),
                                 -np.cos(endpt[2]) + np.cos(startpt[2]))),
                        )))

                    S = np.dot(
                        np.linalg.pinv(R),
                        np.array(
                            np.vstack((endpt[0] - startpt[0],
                                       endpt[1] - startpt[1]))))
                    l = S[0]
                    tvoverrv = S[1]
                    rv = (baseendpose_c[2] * 2.0 * np.pi /
                          numberofangles) + l / tvoverrv
                    tv = tvoverrv * rv

                    # print "R=\n",R
                    # print "Rpi=\n",np.linalg.pinv(R)
                    # print "S=\n",S
                    # print "l=",l
                    # print "tvoverrv=",tvoverrv
                    # print "rv=",rv
                    # print "tv=",tv

                    if l < 0.0:
                        print((
                            'WARNING: l = %f < 0 -> bad action start/end points\n'
                            % (l)))
                        l = 0.0

                    #%compute rv
                    #%rv = baseendpose_c(3)*2*pi/numberofangles;
                    #%compute tv
                    #%tvx = (endpt(1) - startpt(1))*rv/(sin(endpt(3)) - sin(startpt(3)))
                    #%tvy = -(endpt(2) - startpt(2))*rv/(cos(endpt(3)) - cos(startpt(3)))
                    #%tv = (tvx + tvy)/2.0;
                    #%generate samples
                    for iind in np.arange(1, numofsamples + 1):
                        dt = (iind - 1) / (numofsamples - 1)
                        #%dtheta = rv*dt + startpt(3);
                        #%intermcells_m(iind,:) = [startpt(1) + tv/rv*(sin(dtheta) - sin(startpt(3))) ...
                        #%                        startpt(2) - tv/rv*(cos(dtheta) - cos(startpt(3))) ...
                        #%                        dtheta];
                        if (dt * tv) < l:
                            intermcells_m[int(iind) - 1, :] = np.array(
                                np.hstack((
                                    startpt[0] + dt * tv * np.cos(startpt[2]),
                                    startpt[1] + dt * tv * np.sin(startpt[2]),
                                    startpt[2],
                                )))
                        else:
                            dtheta = rv * (dt - l / tv) + startpt[2]
                            intermcells_m[int(iind) - 1, :] = np.array(
                                np.hstack((
                                    startpt[0] + l * np.cos(startpt[2]) +
                                    tvoverrv *
                                    (np.sin(dtheta) - np.sin(startpt[2])),
                                    startpt[1] + l * np.sin(startpt[2]) -
                                    tvoverrv *
                                    (np.cos(dtheta) - np.cos(startpt[2])),
                                    dtheta,
                                )))

                    #%correct
                    errorxy = np.array(
                        np.hstack((
                            endpt[0] - intermcells_m[int(numofsamples) - 1, 0],
                            endpt[1] - intermcells_m[int(numofsamples) - 1, 1],
                        )))
                    # print('l=%f errx=%f erry=%f\n'%(l, errorxy[0], errorxy[1]))
                    interpfactor = np.array(
                        np.hstack((np.arange(0.0, 1.0 + (1.0 / (numofsamples)),
                                             1.0 / (numofsamples - 1)))))

                    # print "intermcells_m=",intermcells_m
                    # print "interp'=",interpfactor.conj().T

                    intermcells_m[:, 0] = intermcells_m[:, 0] + errorxy[
                        0] * interpfactor.conj().T
                    intermcells_m[:, 1] = intermcells_m[:, 1] + errorxy[
                        1] * interpfactor.conj().T

            #%write out
            fout.write('endpose_c: %d %d %d\n' %
                       (endpose_c[0], endpose_c[1], endpose_c[2]))
            fout.write('additionalactioncostmult: %d\n' %
                       (additionalactioncostmult))
            fout.write('intermediateposes: %d\n' %
                       (matrix_size(intermcells_m, 1.0)))
            for interind in np.arange(1.0,
                                      (matrix_size(intermcells_m, 1.0)) + 1):
                fout.write('%.4f %.4f %.4f\n' % (
                    intermcells_m[int(interind) - 1, 0],
                    intermcells_m[int(interind) - 1, 1],
                    intermcells_m[int(interind) - 1, 2],
                ))

            if visualize:
                plt.plot(intermcells_m[:, 0],
                         intermcells_m[:, 1],
                         linestyle="-",
                         marker="o")
                plt.text(endpt[0], endpt[1], '{:2.0f}'.format(endpose_c[2]))
                plt.hold(True)
        # if (visualize):
        #    plt.waitforbuttonpress()  # uncomment to plot each primitive set one at a time

    fout.close()
    if visualize:
        #    plt.waitforbuttonpress()  # hold until buttom pressed
        plt.show()  # Keep windows open until the program is terminated
    return []
Ejemplo n.º 57
0
def prepare_model(volume1, openp, lowp, data, timestamp):
    data = scale_data(data)
    p = int(len(lowp) * 0.70)
    openp = openp[p:]
    lowp = lowp[p:]
    tr_date, ts_date, x_train, x_test, y_train, y_test = prepare_data(
        data, timestamp)
    #ts_date = timestamp_to_dataconv(ts_date)
    print(x_train.shape, y_train.shape, y_test.shape, y_test.shape)
    #print(x_train)
    try:
        final_model = Sequential()
        final_model.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.01)
        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(volume1)
    pred1 = calculate_actual_values(predicted, maximum, minimum)
    actual1 = calculate_actual_values(original, maximum, minimum)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    plt.show()

    print("ended model process")
    return ts_date, pred1, actual1
           fontsize=12)
plt.xlabel(r'$\tau_s/\tau_{dyn}$', fontsize=18)
#plt.xlabel('Delay Steps',fontsize=9)
ax1.set_xticklabels([])
plt.ylabel(r'$C$', fontsize=18)
plt.xlim(0, 0.25)
plt.ylim(0.11, 0.4)
plt.legend(loc='lower right',
           fontsize=12,
           frameon=False,
           handlelength=5,
           numpoints=2)
plt.text(0.04,
         0.95,
         '(a)',
         fontsize=16,
         horizontalalignment='center',
         verticalalignment='center',
         transform=ax1.transAxes)

#savefilename='SC_galpy0718_1000timesteps_3000_orbits.png'
#savefilename='SC_galpy0718_1000timesteps_all_orbits.png'
#savefile = os.path.normpath(datadir+savefilename)
#plt.savefig(savefile,dpi=300,facecolor='w',edgecolor='k')

#fig=plt.figure(num=2,figsize=(5,3.5),dpi=300,facecolor='w',edgecolor='k')
#plt.subplots_adjust(left=left, bottom=bottom, right=right, top=top, wspace=wspace, hspace=hspace)

ax1 = plt.subplot(2, 1, 2)
plt.plot(timeindex_normM6,
         PEs1[1:500],
Ejemplo n.º 59
0
plt.ylim(-ylim, ylim)
plt.xlim(left_window_border, right_window_border)

re = Rectangle((start, plt.ylim()[0]),
               end - start,
               plt.ylim()[1] - plt.ylim()[0],
               color="blue",
               alpha=0.25,
               zorder=-5)
plt.gca().add_patch(re)
plt.text(x=end - 0.02 * (end - start),
         y=plt.ylim()[1] - 0.01 * (plt.ylim()[1] - plt.ylim()[0]),
         s="Chosen window",
         color="0.2",
         fontweight=900,
         horizontalalignment="right",
         verticalalignment="top",
         size="small",
         multialignment="right")

pretty_colors = ["#5B76A1", "#619C6F", "#867CA8", "#BFB281", "#74ACBD"]

_i = 0
for key, value in srcs:
    _i += 1
    plt.subplot(len(srcs) + 1, 1, _i + 1)

    adj_src = pyadjoint.calculate_adjoint_source(key,
                                                 obs,
                                                 syn,
def prepare_model(closep1, openp, lowp, data, timestamp):
    data = scale_data(data)
    p = int(len(lowp) * 0.70)
    openp = openp[p:]
    lowp = lowp[p:]
    tr_date, ts_date, x_train, x_test, y_train, y_test = prepare_data(
        data, timestamp)

    print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)
    print("xlentrain: ", len(x_train))
    print("xlentest: ", len(x_test))
    #x_train = x_train.reshape(x_train.shape[0],1,x_train.shape[1])
    #x_test = x_test.reshape(x_test.shape[0],1,x_test.shape[1])
    # X_train = X_train.reshape((1,-1))

    # reshape input to be [samples, time steps, features]
    #x_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
    #x_test = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))
    print(x_train.shape, x_train.shape, x_test.shape, y_test.shape)

    model = Sequential()
    model.add(
        Conv1D(6, kernel_size=3, activation='relu',
               input_shape=(1, 1, 5)))  # input_shape=()
    model.add(MaxPooling1D(pool_size=3))
    model.add(Conv1D(12, kernel_size=3, activation='relu'))
    model.add(MaxPooling1D(pool_size=3))
    model.add(Flatten())
    model.add(Dense(20, activation='tanh'))
    model.add(Dense(1, activation='linear'))

    model.compile(loss='mse', optimizer=Adam(), metrics=['mae'])

    model.fit(x_train, y_train, epochs=5, verbose=2)

    pred_data = model.predict(x_test)

    predicted = pred_data
    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)
    # writing the data in csv

    # 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)

    write_predicted_data(ts_date, actual1, pred1)
    write_errors_in_file(rmsefA, maefA, mape1A)

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

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

    plt.figure(1)
    # plt.subplot(211)
    plt.subplot(221)
    plt.xticks(rotation=30)
    plt.plot(ts_date, actual1, label="Close Price", color='green')
    plt.plot(ts_date, pred1, label='Predicted', color='red')
    plt.grid(True)
    plt.title("Bitcoin Price (CNN)", fontweight='bold')
    plt.legend(loc=2)
    plt.xlabel("Time")
    plt.ylabel("Price of Bitcoin (USD)")
    # plt.subplots_adjust(hspace=0.4, wspace=0.4)
    plt.subplot(222)
    # plt.subplot(223)
    plt.bar(y_pos, error_values, width, align='center', alpha=0.5, color='red')
    plt.xticks(y_pos, errors_label)
    for a, b in zip(y_pos, error_values):
        plt.text(a, b, str(b))
        # plt.annotate(str(b),y_poserror_values=(a,b))

    plt.title('Evaluation Criteria', fontweight='bold')
    plt.xlabel('Errors')
    plt.ylabel('Values')
    plt.subplot(212)
    plt.title("Architecture of Model", fontsize=14)
    plt.text(0.025, 0.8, "Layers :- ", fontweight='bold')
    plt.text(0.2, 0.8, "Two layers: {11,1}", fontsize=10)

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

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

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

    plt.subplots_adjust(hspace=0.6,
                        wspace=0.5,
                        left=0.125,
                        bottom=0.1,
                        right=None,
                        top=None)
    '''
    # Save Figure
    plt.savefig("foo.png")
    # Save Transparent Figure
    plt.savefig("foo.png", transparent=True)
    '''
    plt.show()
    return ts_date, pred1, actual1