Beispiel #1
0
def main():
    import doctest
    doctest.testmod()  # ...

    # ...
    x_blue = np.random.normal(loc=0, size=num_samples)
    y_blue = np.random.normal(loc=0, scale=0.5, size=num_samples)
    x_red  = np.random.normal(loc=2, size=num_samples)
    y_red  = np.random.normal(loc=2, scale=0.5, size=num_samples)
    
    data =  list(zip(zip(x_blue, y_blue), [labels[0]] * num_samples))
    data += zip(zip(x_red,  y_red),  [labels[1]] * num_samples)
    np.random.shuffle(data)  # ...
    
    train_set = data[:train_size]
    test_set  = data[train_size:]
    
    # Matplotlib craziness to be able to update a plot
    plt.ion()
    fig = plt.figure()
    subplot = fig.add_subplot(1,1,1, xlim=(-5,5), ylim=(-5,5))
    subplot.grid()
    subplot.plot(x_blue, y_blue, linestyle='None', marker='o', color='blue')
    subplot.plot(x_red,  y_red,  linestyle='None', marker='o', color='red')

    p = Perceptron(2)
    p.train(train_set, test_set, subplot=subplot, fig=fig)
def task3(fn_t, fn_tmdt, c):
    '''demonstration'''
    
    L = 1000 #length, using boundary condition u(x+L)=u(x)
    dx = 1.
    dt = 0.5
    t_max = 100
    #c = -10
    
    b = (c*dt/dx)**2
    
    #init fields
    x = arange(0,L+dx/2.,dx) #[0, .... , L], consider 0 = L

    #starting conditions
    field_t = fn_t(x,dt) #starting condition at t0
    field_tmdt = fn_tmdt(x,dt) #starting condition at t0-dt
    
    eq1 = wave_eq_simple(field_t, field_tmdt, b)
    
    plt.ion()
    plot, = plt.plot(x, field_t)
    
    for t in arange(0,t_max,dt):
        print 'outer loop, t=',t
        eq1.step()
        plot.set_ydata(eq1.u)
        plt.draw()
Beispiel #3
0
	def __init__(self,name, sim, delay=None, G=None):
		Process.__init__(self, name, sim)
		self.G=G

		if delay:
			self.tstep=delay
		elif not G: #two ways to give delay info. 
			raise Exception('PlotWorld needs information about the delay..')
		else:
			self.tstep=G.plotDelay #this stuff should already have been handled in simextend, but we take care of it anyway.
		if not self.sim.anim and self.tstep < 1e5: #default is like 1e10..
			plt.ion()
		self.fig = plt.figure()
		#look for monitors.
		try:
			self.sim.plotMoni() #will throw an exception if specified since arguments are not given.
			monImpl=False
		except TypeError:
			monImpl=True		
		if self.G.noMonitors or (not monImpl):
			self.ax1 = self.fig.add_subplot(111,aspect='equal', axisbg='#A2CD5A')
			self.ax2=None #not implemented in Simulation. See tools.simextend for instructions.
			self.ax3=None
			self.ax4=None
		else:
			self.ax1 = self.fig.add_subplot(121,aspect='equal', axisbg='#A2CD5A')
			self.ax2 = self.fig.add_subplot(322)
			self.ax3 = self.fig.add_subplot(324)
			self.ax4 = self.fig.add_subplot(326)
		if self.sim.anim:
			if not os.path.exists('animtemp'):
				os.makedirs('animtemp')
Beispiel #4
0
def mask_spectrum(flux_to_fit,interactive=True,mask_lower_limit=None,mask_upper_limit=None):

    """
    Interactively and iteratively creates a Boolean mask for a spectrum.

    """
    if interactive:
      plt.ion()
      continue_parameter = 'no'
      mask_switch = 'yes'
      while mask_switch == 'yes':
        pixel_array = np.arange(len(flux_to_fit))
        plt.figure()
        plt.step(pixel_array,flux_to_fit)
        mask_lower_limit_string = raw_input("Enter mask lower limit (in pixels): ")
        mask_lower_limit = float(mask_lower_limit_string)
        mask_upper_limit_string = raw_input("Enter mask upper limit (in pixels): ")
        mask_upper_limit = float(mask_upper_limit_string)
        mask = (pixel_array >= mask_lower_limit) & (pixel_array <= mask_upper_limit)
        flux_to_fit_masked = np.ma.masked_where(mask,flux_to_fit)
        plt.step(pixel_array,flux_to_fit_masked)
        continue_parameter = raw_input("Happy? (yes or no]): ")
        plt.close()

        if continue_parameter == 'yes':
          mask_switch = 'no'

    else:
      pixel_array = np.arange(len(flux_to_fit))
      mask = (pixel_array >= mask_lower_limit) & (pixel_array <= mask_upper_limit)

    return mask
def task1():
    '''demonstration'''
    
    #TASK 0: Demo
    L = 1000 #length, using boundary condition u(x+L)=u(x)
    dx = 1.
    dt = 0.1
    t_max = 100
    c = +10
    
    b = (c*dt/dx)**2
    
    #init fields
    x = arange(0,L+dx/2.,dx) #[0, .... , L], consider 0 = L

    #starting conditions
    field_t = exp(-(x-10)**2) #starting condition at t0
    field_tmdt = exp(-(x-c*dt-10)**2) #starting condition at t0-dt
    
    eq1 = wave_eq(field_t, field_tmdt, b)
    
    plt.ion()
    plot, = plt.plot(x, field_t)
    
    for t in arange(0,t_max,dt):
        print 'outer loop, t=',t
        eq1.step()
        plot.set_ydata(eq1.u)
        plt.draw()
Beispiel #6
0
 def __init__(self, analogData):
   # set plot to animated
   plt.ion() 
   self.axline, = plt.plot(analogData.ax)
   self.ayline, = plt.plot(analogData.ay)
   self.plt = plt
   plt.ylim([-2,2])
Beispiel #7
0
	def Visualize(self,path=None,filename=None,viz_type='difference'):
		if path is None:
			path = self.result_path
		if filename is None:
			filename = '/results'
		im = []
		if self.n<=1:
			fig = mpl.figure()
			x = np.linspace(0,1,self.m)
			counter = 1
			for step in sorted(glob.glob(path+filename+'*.txt')):
				tmp = np.loadtxt(step)
				if viz_type=='difference':
					im.append(mpl.plot(x,(self.exact(x,np.zeros(self.m),counter*self.dt)-tmp),'b-'))
				else:
					im.append(mpl.plot(x,tmp,'b-'))
				counter += 1
			ani = animation.ArtistAnimation(fig,im)
			mpl.show()
		else:
			X,Y = np.meshgrid(np.linspace(0,1,self.m),np.linspace(0,1,self.n))
			mpl.ion()
			fig = mpl.figure()
			ax = fig.add_subplot(111,projection='3d')
			counter = 1
			for step in sorted(glob.glob(path+filename+'*.txt')):
				tmp = np.loadtxt(step)
				wframe = ax.plot_wireframe(X,Y,(self.exact(X,Y,(counter*self.dt))-tmp))
				mpl.draw()
				if counter==1:
					pass
					# ax.set_autoscaley_on(False)
				ax.collections.remove(wframe)
				counter +=1
Beispiel #8
0
 def _create_figure(self, data=None, x=None, redraw=True, manual=False):
     if self.parent is None:
         pyplot.ion()    # Must be here
         
         kwds = {}
         if self.figsize is not None:
             kwds['figsize'] = self.figsize
         self.figure = pyplot.figure(**kwds)   # num=X
         
         if self.padding is not None:
             self.figure.subplots_adjust(**self.padding)
         
         self.title = self.figure.suptitle(self.title_text)
         if manual == False:
             self.subplot = self.figure.add_subplot(self.pos)
     else:
         self.subplot = self.parent.figure.add_subplot(self.pos)
     
     if self.subplot is not None:
         self.subplot.grid(True)
         self.subplot.set_title(self.sub_title_text)
         if x is None:
             #x = numpy.array([0])
             #if self.x_range is None and data is not None:
             #    self._calc_x_range(data)
             #if self.x_range is not None:
             #    x = numpy.linspace(self.x_range[0], self.x_range[1], self.x_range[1]-self.x_range[0])
             
             #pass
             
             if data is not None:
                 self._calc_x_range(data)
                 x = numpy.linspace(self.x_range[0], self.x_range[1], len(data[0]))
         else:
             self.x_range = (min(x), max(x)) # FIXME: Only if x_range is not None?
         
         #if data is None:
         #    data = numpy.array([0]*len(x))
         
         if not isinstance(data, list):
             data = [data]
         
         if data is not None and x is not None:
             #self.plot, = pyplot.plot(x, data)
             #self.plot, = self.subplot.plot(x, data)
             
             #self.plots += self.subplot.plot([(x, _y) for _y in data])  # FIXME
             for d in data:
                 self.plots += self.subplot.plot(x, d)
         
         # This was moved left one indent level ('_apply_axis_limits' is safe)
         
         #self.plot.axes.grid(True)
         #self.plot.axes.set_title(self.sub_title_text)
     
         #self.plot.axes.set_xlim([min(x),max(x)])
         self._apply_axis_limits()
     
     if redraw:
         self._redraw()
    def plot_goals(self):
        fig = plt.figure()
        #fig = plt.gcf()
        fig.set_size_inches(14,11,forward=True)
        ax = fig.add_subplot(111, projection='3d')
        X  = self.clustered_goal_data[:,0,3]
        Y  = self.clustered_goal_data[:,1,3]
        Z = self.clustered_goal_data[:,2,3]
        #print X,len(X),Y,len(Y),Z,len(Z)
        c  = 'b'
        surf = ax.scatter(X, Y, Z,s=40, c=c,alpha=.5)
        #surf = ax.scatter(X, Y,s=40, c=c,alpha=.6)
        ax.set_xlabel('X Axis')
        ax.set_ylabel('Y Axis')
        ax.set_zlabel('Z Axis')

        ax.set_title(''.join(['Plot of goals from ',self.subject,'. Data: (',str(self.data_start),' - ',str(self.data_finish),')']))

        #fig.colorbar(surf, shrink=0.5, aspect=5)
        rospack = rospkg.RosPack()
        pkg_path = rospack.get_path('hrl_base_selection')
        ax.set_xlim(-.2,.2)
        ax.set_ylim(-.2,.2)
        ax.set_zlim(-.2,.2)
        #plt.savefig(''.join([pkg_path, '/images/goals_plot_',self.model,'_',self.subject,'_numbers_',str(self.data_start),'_',str(self.data_finish),'.png']), bbox_inches='tight')
        
        plt.ion()                                                                                                 
        plt.show()                                                                                                
        ut.get_keystroke('Hit a key to proceed next')
    def doLeutiEvaluation(self, figureId=-1):
        if len(self.plotLeutiDistances) > 0:
            plt.ion()
            plt.show(block=False)
            if figureId == -1:
                figure()
            elif figureId >= 0:
                figure(figureId)
            spacings = self.plotLeutiDistances
            if self.leutiSpacing > 0:
                spacings = np.ones(len(self.plotLeutiDistances)) * self.leutiSpacing
            leutiOutputPos, leutiOutputAtt, leutiOutputYaw, leutiOutputIncl = self.td.computeLeutiScore(
                "pos", "att", "vel", self.tdgt, "pos", "att", self.plotLeutiDistances, spacings, 0.0
            )
            p = np.arange(len(self.plotLeutiDistances))
            if figureId >= -1:
                boxplot(leutiOutputPos, positions=p, widths=0.8)
                ax = axes()
                ax.set_xticklabels(self.plotLeutiDistances)
                ax.set_xlabel("Travelled Distance [m]")
                ax.set_ylabel("Position Error [m]")

            med = np.zeros(len(leutiOutputPos))
            for i in range(len(leutiOutputPos)):
                med[i] = median(leutiOutputPos[i])
            s = np.array(self.plotLeutiDistances) ** 0.5
            score = s.dot(med) / s.dot(s)
            fit = score * s
            if figureId >= -1:
                plot(p, fit)
                title("Error Plot for Position, score = " + str(score))

            return [leutiOutputPos, leutiOutputAtt, leutiOutputYaw, leutiOutputIncl, score]
Beispiel #11
0
def ToSVGString(graph):
    """
    Convert as SVG file.

    Parameters
    ----------
    graph : object
        A Graph or Drawable object.

    Returns a SVG representation as string
    """
    if sys.version_info[0] >= 3:
        output = io.StringIO()
    else:
        output = io.BytesIO()

    # save interactive mode state
    ision = plt.isinteractive()
    plt.ioff()

    view = View(graph)
    view.save(output, format='svg')
    view.close()

    # restore interactive mode state
    if ision:
        plt.ion()

    return output.getvalue()
Beispiel #12
0
 def __init__(self, model, n_rows, n_cols):
     plt.ion()
     self.n_rows, self.n_cols = n_rows, n_cols
     self.n_comp = model.W.shape[1]
     self.sub_rows, self.sub_columns = self.determine_subplots()
     self.figure, self.axes = plt.subplots(self.sub_rows, self.sub_columns)
     self.figure.suptitle(u"Loss and components -- NMF w/ {0}".format(model.loss_name), size=10)
     self.ax_loss = self.axes[0, 0]
     self.ax_loss.set_title(u"Loss", size=8)
     self.lines, = self.ax_loss.plot([], [], u'o')
     self.images = []
     for i in range(self.sub_rows * self.sub_columns - 1):
         sub_i, sub_j = (1 + i) % self.sub_rows, (1 + i) / self.sub_rows
         subplot = self.axes[sub_i, sub_j]
         if i < self.n_comp:
             self.images.append(subplot.imshow(self.prepare_image(model.W[:, i]), cmap=u"Greys"))
             subplot.set_title(u"W[:, %d]" % i, size=8)
             subplot.set_axis_off()
         else:
             # Disable empty subplots
             subplot.set_visible(False)
     self.ax_loss.set_autoscaley_on(True)
     self.ax_loss.set_xlim(0, model.iterations)
     self.ax_loss.grid()
     self.ax_loss.get_xaxis().set_visible(False)
     self.ax_loss.get_yaxis().set_visible(False)
Beispiel #13
0
def main():
    conn = krpc.connect()
    vessel = conn.space_center.active_vessel
    streams = init_streams(conn,vessel)
    print vessel.control.throttle
    plt.axis([0, 100, 0, .1])
    plt.ion()
    plt.show()

    t0 = time.time()
    timeSeries = []
    vessel.control.abort = False
    while not vessel.control.abort:

        t_now = time.time()-t0
        tel = Telemetry(streams,t_now)
        timeSeries.append(tel)
        timeSeriesRecent = timeSeries[-40:]

        plt.cla()
        plt.semilogy([tel.t for tel in timeSeriesRecent], [norm(tel.angular_velocity) for tel in timeSeriesRecent])
        #plt.semilogy([tel.t for tel in timeSeriesRecent[1:]], [quat_diff_test(t1,t2) for t1,t2 in zip(timeSeriesRecent,timeSeriesRecent[1:])])
        #plt.axis([t_now-6, t_now, 0, .1])
        plt.draw()
        plt.pause(0.0000001)
        #time.sleep(0.0001)

    with open('log.json','w') as f:
        f.write(json.dumps([tel.__dict__ for tel in timeSeries],indent=4))

    print 'The End'
Beispiel #14
0
    def plot_tree(self, root=None, **kwargs):
        """
        Draw branches
        """
        if root and not self.root:
            self.set_root(root)

        if self.interactive: pyplot.ioff()

        self.yaxis.set_visible(False)
        self.create_branch_artists()
        self.mark_named()
        ## self.home()

        self.set_name(self.name)
        self.adjust_xspine()

        if self.interactive: pyplot.ion()

        def fmt(x, pos=None):
            if x<0: return ""
            return ""
        #self.yaxis.set_major_formatter(FuncFormatter(fmt))

        return self
Beispiel #15
0
 def wrapper(*args, **kwargs):
     try:
         plt.ion()
         return func(*args, **kwargs)
     finally:
         plt.close("all")
         plt.ioff()
def draw(arrayin):
	array = np.abs(arrayin)
	plt.clf()
	plt.ion()
	plt.imshow(array) #,cmap='Greys_r')
	plt.axis('off')
	plt.draw()
def complexHist(array):
	"""Display the points (array) on a real and imaginary axis."""
	from matplotlib.ticker import NullFormatter
	# scale the amplitudes to 0->1024
	arrayAmp = np.abs(array)/np.max(np.abs(array)) 
	#arrayAmp = arrayAmp - np.min(arrayAmp)
	#arrayAmp = arrayAmp / np.max(arrayAmp)
	arrayAmp = 1000.0*arrayAmp/(1000.0*arrayAmp + 1)
	array2   = arrayAmp * np.exp(-1.0J * np.angle(array))
	x = []
	y = []

	for i in range(1000):
		i = random.randrange(0,array.shape[1])
		j = random.randrange(0,array.shape[0])
		x.append(array2.real[i,j])
		y.append(array2.imag[i,j])

	plt.clf()
	plt.ion()
	rect_scatter = [0.0,0.0,1.0,1.0]
	axScatter = plt.axes(rect_scatter)
	axScatter.scatter(x,y,s=1,c='grey',marker='o')
	axScatter.set_xlim((-1.0,1.0))
	axScatter.set_ylim((-1.0,1.0))
	#plt.plot(x,y,'k,')
	plt.draw()
Beispiel #18
0
def omori_tahir_dist(alpha=.5, beta=.5):
	#
	# plot an omori_x + poisson_tahir type figure
	#
	X=numpy.arange(0., 30., .1)
	norm_fact = alpha+beta
	alpha /= norm_fact
	beta  /= norm_fact
	#
	Y_omori_x   = [omori_x(x) for x in X]		#omori_x(X)
	Y_poisson_x = [poisson_x(x) for x in X]	#	poisson_x(X)
	#
	YY = alpha*numpy.array(Y_omori_x) + beta*numpy.array(Y_poisson_x)
	#
	myplot=plt.loglog
	#
	plt.ion()
	plt.figure(0)
	plt.clf()
	myplot(X, Y_omori_x, label='Aftershock Seismicity', lw=3)
	myplot(X, Y_poisson_x, label='Largest Aftershock', lw=3)
	myplot(X, YY, label = 'combined, $\\alpha = %.2f$, $\\beta = %.2f$' % (alpha, beta), lw=3)
	#
	plt.legend(loc=0, numpoints=1)
	plt.xlabel('\'Normalized\' distance, $r/r_0$', size=16)
	plt.ylabel('Seismic Hazard', size=16)
Beispiel #19
0
def show_vector(dx, dy, arr = None, w=None, h=None, skip=6, holdon=False):
    if w is None or h is None:
        h = dx.shape[0]
        w = dx.shape[1]

    import matplotlib.pyplot as plt
    x, y = np.meshgrid(np.linspace(0, w, w), np.linspace(0, h, h))

    ax = plt.axes(xlim=(0, w), ylim=(0, h))
    line, = plt.plot(0,0,'ro')
    plt.ion()
    plt.ylim([0, h])
    if skip is None:
        ax.quiver(x, y, dx, dy)
    else:
        skip = (slice(None, None, skip), slice(None, None, skip))
        ax.quiver(x[skip], y[skip], dx[skip], dy[skip])
        if arr is not None:
            plt.imshow(arr, cmap=plt.cm.Greys_r)


    if holdon is True:
        plt.draw()
        plt.pause(0.0001)
        return line, plt
    else:
        plt.show()
Beispiel #20
0
def process_statspecs(directive, part=None, designname=None):
    """
    Main processor for the staplestatter directive. Responsible for:
    1) Initialize figure and optionally axes as specified by the directive instructions.
    2) Loop over all statspecs and call process_statspec.
    3) Aggregate and return a list of stats/scores.
    """
    if part is None:
        part = cadnano_api.p()
    if designname is None:
        designname = os.path.splitext(os.path.basename(part.document().controller().filename()))[0]
    print("designname:", designname)

    statspecs = directive['statspecs']
    figspec = directive.get('figure', dict())
    if figspec.get('newfigure', False) or len(pyplot.get_fignums()) < 1:
        fig = pyplot.figure(**figspec.get('figure_kwargs', {}))
    else:
        fig = pyplot.gcf() # Will make a new figure if no figure has been created.
    # Here you can add more "figure/axes" specification logic:
    adjustfuncs = ('title', 'size_inches', 'dpi')
    for cand in adjustfuncs:
        if cand in figspec and figspec[cand]:
            getattr(fig, 'set_'+cand)(figspec[cand]) # equivalent to fig.title(figspec['title'])

    pyplot.ion()
    allscores = list()
    for _, statspec in enumerate(statspecs):
        scores = process_statspec(statspec, part=part, designname=designname, fig=fig)
        allscores.append(scores)
        if 'printspec' in statspec:
            print("Printing highest scores with: statspec['printspec']")
            get_highest_scores(scores, **statspec['printspec']) # This logic is subject to change.
    return dict(figure=fig, scores=allscores)
def makeDisplay():
    plt.ion()
    fig = plt.figure(figsize=(10, 12))

    layout = fig.add_subplot(211)
    plt.ylabel("x position + 10*conc")
    plt.xlabel("y position (microns)")
    timeLabel = plt.text(0, 20, "time = 0")
    layout.set_xlim(-5, 75)
    layout.set_ylim(-20, 25)
    compt = moose.element("/model/chem/compt0")
    pos = compt.voxelMidpoint
    i = len(pos) / 3
    r2 = numpy.sqrt(0.5)
    yp = [-r2 * pos[j] * 1e6 for j in range(i)]
    xp = pos[i : 2 * i] * 1e6 - yp
    # xp = [ pos[i + j] for j in range( i ) ]
    # yp = [ -r2 * pos[j] for j in range( i ) ]
    # line0, = layout.plot( pos[:i], pos[i:2*i] , 'bo' )
    line, = layout.plot(xp, yp, "bo")

    timeSeries = fig.add_subplot(212)
    timeSeries.set_ylim(0, 0.6)
    plt.ylabel("Conc (mM)")
    plt.xlabel("time (seconds)")

    fig.canvas.draw()
    return (timeSeries, fig, line, timeLabel, yp)
Beispiel #22
0
def plotsolution(numnodes,coordinates,routes):
   plt.ion() # interactive mode on
   G=nx.Graph()
   
   nodes = range(1,numnodes+1)
   nodedict = {}
   for i in nodes:
     nodedict[i] = i
   
   nodecolorlist = ['b' for i in nodes]
   nodecolorlist[0] = 'r'
     
   # nodes  
   nx.draw_networkx_nodes(G, coordinates, node_color=nodecolorlist, nodelist=nodes)
   # labels
   nx.draw_networkx_labels(G,coordinates,font_size=9,font_family='sans-serif',labels = nodedict)
   
   edgelist = defaultdict(list)
   
   colors = ['Navy','PaleVioletRed','Yellow','Darkorange','Chartreuse','CadetBlue','Tomato','Turquoise','Teal','Violet','Silver','LightSeaGreen','DeepPink', 'FireBrick','Blue','Green']
   
   for i in (routes):
     edge1 = 1
     for j in routes[i][1:]:
       edge2 = j
       edgelist[i].append((edge1,edge2))
       edge1 = edge2
       nx.draw_networkx_edges(G,coordinates,edgelist=edgelist[i],
                        width=6,alpha=0.5,edge_color=colors[i]) #,style='dashed'
   
   plt.savefig("path.png")

   plt.show()
Beispiel #23
0
	def __init__(self,master,title):
		Toplevel.__init__(self,master)
		self.master = master

		from __init__ import MATPLOTLIB_BACKEND
		if MATPLOTLIB_BACKEND != None:
			print "manipulator: Setting matplotlib backend to \"TkAgg\"."
			
		import matplotlib
		matplotlib.use("TkAgg")

		from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
		from matplotlib.figure import Figure
		from matplotlib import pyplot

		self.title(title)
		self.resizable(True,True)
		self.fig = pyplot.figure()
		pyplot.ion()

		self.canvas = FigureCanvasTkAgg(self.fig, master=self)
		self.canvas.show()
		self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
		self.canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)
		self.update()

		self.experiments = []
def streamVisionSensor(visionSensorName,clientID,pause=0.0001):
    #Get the handle of the vision sensor
    res1,visionSensorHandle=vrep.simxGetObjectHandle(clientID,visionSensorName,vrep.simx_opmode_oneshot_wait)
    #Get the image
    res2,resolution,image=vrep.simxGetVisionSensorImage(clientID,visionSensorHandle,0,vrep.simx_opmode_streaming)
    #Allow the display to be refreshed
    plt.ion()
    #Initialiazation of the figure
    time.sleep(0.5)
    res,resolution,image=vrep.simxGetVisionSensorImage(clientID,visionSensorHandle,0,vrep.simx_opmode_buffer)
    im = I.new("RGB", (resolution[0], resolution[1]), "white")
    #Give a title to the figure
    fig = plt.figure(1)    
    fig.canvas.set_window_title(visionSensorName)
    #inverse the picture
    plotimg = plt.imshow(im,origin='lower')
    #Let some time to Vrep in order to let him send the first image, otherwise the loop will start with an empty image and will crash
    time.sleep(1)
    while (vrep.simxGetConnectionId(clientID)!=-1): 
        #Get the image of the vision sensor
        res,resolution,image=vrep.simxGetVisionSensorImage(clientID,visionSensorHandle,0,vrep.simx_opmode_buffer)
        #Transform the image so it can be displayed using pyplot
        image_byte_array = array.array('b',image)
        im = I.frombuffer("RGB", (resolution[0],resolution[1]), image_byte_array, "raw", "RGB", 0, 1)
        #Update the image
        plotimg.set_data(im)
        #Refresh the display
        plt.draw()
        #The mandatory pause ! (or it'll not work)
        plt.pause(pause)
    print 'End of Simulation'
Beispiel #25
0
def plot_server():
    rospy.init_node('plotter')
    s = rospy.Service('plot', Plot, handle_plot)
    plt.ion()
    print "Ready to plot things!", plt.isinteractive()
    
    rospy.spin()
    def follow_channels(self, channel_list):
        """
        Tracks and plots a specified set of channels in real time.

        Parameters
        ----------
        channel_list : list
            A list of the channels for which data has been requested.
        """
        if not pyplot_available:
            raise ImportError('pyplot needs to be installed for '
                              'this functionality.')
        plt.clf()
        plt.ion()
        while True:
            self.update_channels(channel_list)
            plt.clf()
            for channel_name in self.channels:
                plt.plot(
                    self.channels[channel_name].epoch_record,
                    self.channels[channel_name].val_record,
                    label=channel_name
                )
            plt.legend()
            plt.ion()
            plt.draw()
	def __init__(self):
		self.map = np.loadtxt('wean.dat', delimiter=' ')
		self.occ = np.loadtxt('occu.dat', delimiter=' ')
		self.unocc = np.loadtxt('unoccu.dat', delimiter=' ')
		self.unocc_dict = {tuple(el):1 for el in self.unocc}
		self.unocc_corr = np.loadtxt('unoccu_corr.dat', delimiter=' ')
		#self.X_t = np.loadtxt('part_init.dat', delimiter=' ')
		self.num_p = 1e4
		self.sense = np.loadtxt('sense4.dat', delimiter=' ')
		self.isodom = np.loadtxt('is_odom4.dat', delimiter=' ')
		self.mindist = np.loadtxt('min_d.dat', delimiter=' ')
		self.a = np.array([.01,.01,0.01,0.01])
		self.c_lim = 10
		self.lsr_max = 1000
		self.zmax = 0.25
		self.zrand = 0.25
		self.sig_h = 5
		self.zhit = .75
		self.q = 1
		self.srt = 10
		self.end = 170
		self.step = 10
		plt.imshow(self.map)
		plt.ion()
		self.scat = plt.quiver(0,0,1,0)
Beispiel #28
0
def plot_astcat(infile, astcat, rmarker=10., racol=0, deccol=1, hext=0):

   # Turn on interactive, so that plot occurs first and then query
   plt.ion()

   """ Load fits file """
   fitsim = im.Image(infile)
   hdr = fitsim.hdulist[hext].header

   """ Select astrometric objects within the FOV of the detector """
   mra,mdec,mx,my,astmask = select_good_ast(astcat,hdr,racol,deccol)

   """ Plot the fits file and mark the astrometric objects """
   fig = plt.figure(1)
   fitsim.display(cmap='heat')
   plt.plot(mx,my,'o',ms=rmarker,mec='g',mfc='none',mew=2)
   plt.xlim(0,nx)
   plt.ylim(0,ny)

   plt.draw()
   #cid = fig.canvas.mpl_connect('button_press_event', on_click)

   """ 
   Be careful of the +1 offset between SExtractor pixels and python indices
   """

   return fitsim
Beispiel #29
0
def plot_dataframe(data, error=False, log=False):
    '''
    plot data frame columns in a long plot
    '''
    try:
        ioff()
        fig, ax = plt.subplots(
            (len(data.columns)), figsize=(10, 50), sharex=True)
        close('all')
        ion()
    except:
        print 'you may be in a non interactive environment'
    if not error:
        for i, j in zip(data.columns, ax):
            if i != 'name':
                j.plot(data.index, data[i].values)
                j.set_ylabel(str(i))
    if error:
        for i, j in zip(data.columns, ax):
            if i != 'name':
                j.errorbar(
                    data.index, data[i].values, yerr=sqrt(data[i].values))
                j.set_ylabel(str(i))
    if log:
        for i, j in zip(data.columns, ax):
            if i != 'name':
                j.set_yscale('log')
    return fig, ax
Beispiel #30
0
def main():
    print """Legend:
        Yellow star\t -\t True position of robot
        Blue arrows\t -\t Particle cloud
        Yellow dots\t -\t Sonar pings
        Green boxes\t -\t Obstacles
        Red star\t -\t Goal"""

    these_parameters = robot.Parameters(vel_max=1
                                  , omega_max=0.1
                                  , displacement_slowdown=5
                                  , avoid_threshold=5
    )
    true_pose = (80, 90, pi)
    this_goal = robot.Goal(location=(20, 20, 0)
                     , radius=3)
    this_map = mapdef()
    this_sonar = ogmap.Sonar(num_theta=20
                             , gauss_var=2
    )

    this_robot = RobotMapper(these_parameters
                       , this_sonar
    )
    this_robot.situate(this_map
                       , true_pose
                       , this_goal
    )

    plt.ion()
    this_robot.automate(num_steps=10)
    if robot.check_success(this_goal, this_robot):
        print "SUCCESS"
    else:
        print "FAILURE"
Beispiel #31
0
def plot_bar_seq_stdev(barcode_frame,
                       notebook_dir,
                       experiment=None,
                       show_plots=True,
                       save_plots=False,
                       count_cutoff=500):

    # Turn interactive plotting on or off depending on show_plots
    if show_plots:
        plt.ion()
    else:
        plt.ioff()

    if save_plots:
        pdf_file = 'barcode read standard deviation plot.pdf'
        pdf = PdfPages(pdf_file)

    data_directory = notebook_dir + "\\barcode_analysis"
    os.chdir(data_directory)

    if experiment is None:
        experiment = get_exp_id(notebook_dir)

    #Plot standard deviation of barcode read fractions (across wells in time point 1) vs mean read fraction
    # data from 2019-10-02: #################################################################################
    x_test = np.asarray([
        0.23776345382258504, 0.21428834768303265, 0.14955568743012018,
        0.10527042635253019, 0.08814193520270863, 0.07140559171457407,
        0.032268913991628186, 0.02486533840744069, 0.009370452839984682,
        0.0021539027931815613, 0.0001936817014361814
    ])
    y_test = np.asarray([
        0.0019726945744597706, 0.0028398295224567756, 0.0027140121666701543,
        0.0016422861817864806, 0.0012364410886752844, 0.0014467832918787287,
        0.0009412184378809117, 0.0007090217957749182, 0.00034552377974558844,
        0.00017198555940160456, 4.958998052635534e-05
    ])
    poisson_err_test = np.asarray([
        0.001391130466104952, 0.001320415964490587, 0.0011032026255463198,
        0.0009247685041703838, 0.0008466282838575875, 0.0007620910541483005,
        0.0005123905962175842, 0.000449754496329767, 0.00027605091052578906,
        0.0001323496187650663, 3.929704870026295e-05
    ])
    #####################################################################################################

    # data from 2019-10-08: #############################################################################
    x_small = np.asarray([
        0.08251274176535274, 0.0962239061597132, 0.08539004578198717,
        0.08675701439383578, 0.07400424816228543, 0.07566109361860245,
        0.0699367739242362, 0.06963680434271374, 0.06384195016208481,
        0.06321931248609224, 0.06334894239678983, 0.02536420185939611,
        0.03923343837910993, 0.020238576239101202
    ])
    y_small = np.asarray([
        0.003020200426682457, 0.003374150359051314, 0.00374541788260866,
        0.0035764736646941536, 0.002598176841078495, 0.003669639858790278,
        0.0021759993522437074, 0.002827475646549457, 0.0038335541520843315,
        0.002201298340428577, 0.008012477386731139, 0.001454772893578839,
        0.0012788004626381614, 0.0021763030793714206
    ])
    poisson_err_small = np.asarray([
        0.0008661333092282185, 0.0009340439480853888, 0.0008821889073372234,
        0.0008856945951456786, 0.000820757229296616, 0.000830315430739499,
        0.0007963057526756344, 0.0007963629310250612, 0.000763102677224598,
        0.0007575749124137182, 0.0007546065015548847, 0.0004797418835729835,
        0.000596486425619687, 0.00042833165436399073
    ])
    ##############################################################################################

    fraction_list = ["fraction_" + w for w in wells_by_column()[:24]]

    plt.rcParams["figure.figsize"] = [16, 8]
    fig, axs = plt.subplots(1, 2)
    #fig.suptitle('First Time Point Only (Plate 2)', fontsize=24, position=(0.5, 0.925))

    axs[0].plot(x_test,
                y_test,
                "o",
                ms=10,
                label="Library Prep Test, 2019-10-02")
    axs[0].plot(x_test, poisson_err_test, c="gray")
    axs[0].plot(x_small,
                y_small,
                "o",
                ms=10,
                label="Small Library Selection, 2019-10-08")
    axs[1].plot(x_test,
                y_test / x_test,
                "o",
                ms=10,
                label="Library Prep Test, 2019-10-02")
    axs[1].plot(x_test, poisson_err_test / x_test, c="gray")
    axs[1].plot(x_small,
                y_small / x_small,
                "o",
                ms=10,
                label="Small Library Selection, 2019-10-08")

    f_data = barcode_frame[barcode_frame["total_counts"] > count_cutoff]

    y = np.asarray(
        [f_data[fraction_list].iloc[i].std() for i in range(len(f_data))])
    x = np.asarray(
        [f_data[fraction_list].iloc[i].mean() for i in range(len(f_data))])
    err_est = np.asarray([
        (f_data[fraction_list].iloc[i].mean()) /
        (np.sqrt(f_data[wells_by_column()[:24]].iloc[i].mean()))
        for i in range(len(f_data))
    ])

    axs[0].plot(x, y, "o", ms=5, label=experiment)
    axs[0].plot(x, err_est, c="darkgreen")
    axs[0].set_ylabel('Stdev(barcode fraction per sample)', size=20)
    axs[0].plot(x, y / x, "o", ms=5, label=experiment)
    axs[0].plot(x, err_est / x, c="darkgreen")
    axs[0].set_ylabel('Relative Stdev(barcode fraction per sample)', size=20)

    for ax in axs.flatten():
        ax.set_xlabel('Mean(barcode fraction per sample)', size=20)
        ax.tick_params(labelsize=16)
        ax.set_xscale("log")
        ax.set_yscale("log")
        leg = ax.legend(loc='upper left',
                        bbox_to_anchor=(0.025, 0.93),
                        ncol=1,
                        borderaxespad=0,
                        frameon=True,
                        fontsize=12)
        leg.get_frame().set_edgecolor('k')
    if save_plots:
        pdf.savefig()
    if not show_plots:
        plt.close(fig)

    if save_plots:
        pdf.close()
from sweeppy import Sweep
import matplotlib.pyplot as plt
import math
import os

DEV = os.environ['SCANSE_SWEEP_PORT']

# Create figure:
fig1 = plt.figure(1)

# Clear figure:
plt.clf()

# Enable interactive mode:
plt.ion()

# Create polar subplot:
sbp = plt.subplot(111, projection='polar')

# Create axis:
ax = fig1.add_axes(sbp)

# Set radius limit:
ax.set_ylim(0, 600)

if __name__ == "__main__":
    with Sweep(DEV) as sweep:
        speed = sweep.get_motor_speed()
        rate = sweep.get_sample_rate()
Beispiel #33
0
    def _ml_t_marginal(self, assign_dates=False):
        """
        Compute the marginal probability distribution of the internal nodes positions by
        propagating from the tree leaves towards the root. The result of
        this operation are the probability distributions of each internal node,
        conditional on the constraints on all leaves of the tree, which have sampling dates.
        The probability distributions are set as marginal_pos_LH attributes to the nodes.

        Parameters
        ----------

         assign_dates : bool, default False
            If True, the inferred dates will be assigned to the nodes as
            :code:`time_before_present' attributes, and their branch lengths
            will be corrected accordingly.
            .. Note::
                Normally, the dates are assigned by running joint reconstruction.

        Returns
        -------

         None
            Every internal node is assigned the probability distribution in form
            of an interpolation object and sends this distribution further towards the
            root.

        """
        def _cleanup():
            for node in self.tree.find_clades():
                try:
                    del node.marginal_pos_Lx
                    del node.subtree_distribution
                    del node.msg_from_parent
                    #del node.marginal_pos_LH
                except:
                    pass

        self.logger(
            "ClockTree - Marginal reconstruction:  Propagating leaves -> root...",
            2)
        # go through the nodes from leaves towards the root:
        for node in self.tree.find_clades(
                order='postorder'):  # children first, msg to parents
            if node.bad_branch:
                # no information
                node.marginal_pos_Lx = None
            else:  # all other nodes
                if node.date_constraint is not None and node.date_constraint.is_delta:  # there is a hard time constraint
                    # initialize the Lx for nodes with precise date constraint:
                    # subtree probability given the position of the parent node
                    # position of the parent node is given by the branch length
                    # distribution attached to the child node position
                    node.subtree_distribution = node.date_constraint
                    bl = node.branch_length_interpolator.x
                    x = bl + node.date_constraint.peak_pos
                    node.marginal_pos_Lx = Distribution(
                        x,
                        node.branch_length_interpolator(bl),
                        min_width=self.min_width,
                        is_log=True)

                else:  # all nodes without precise constraint but positional information
                    # subtree likelihood given the node's constraint and child msg:
                    msgs_to_multiply = [
                        node.date_constraint
                    ] if node.date_constraint is not None else []
                    msgs_to_multiply.extend([
                        child.marginal_pos_Lx for child in node.clades
                        if child.marginal_pos_Lx is not None
                    ])

                    # combine the different msgs and constraints
                    if len(msgs_to_multiply) == 0:
                        # no information
                        node.marginal_pos_Lx = None
                        continue
                    elif len(msgs_to_multiply) == 1:
                        node.subtree_distribution = msgs_to_multiply[0]
                    else:  # combine the different msgs and constraints
                        node.subtree_distribution = Distribution.multiply(
                            msgs_to_multiply)

                    if node.up is None:  # this is the root, set dates
                        node.subtree_distribution._adjust_grid(
                            rel_tol=self.rel_tol_prune)
                        node.marginal_pos_Lx = node.subtree_distribution
                        node.marginal_pos_LH = node.subtree_distribution
                        self.tree.positional_marginal_LH = -node.subtree_distribution.peak_val
                    else:  # otherwise propagate to parent
                        res, res_t = NodeInterpolator.convolve(
                            node.subtree_distribution,
                            node.branch_length_interpolator,
                            max_or_integral='integral',
                            n_grid_points=self.node_grid_points,
                            n_integral=self.n_integral,
                            rel_tol=self.rel_tol_refine)
                        res._adjust_grid(rel_tol=self.rel_tol_prune)
                        node.marginal_pos_Lx = res

        self.logger(
            "ClockTree - Marginal reconstruction:  Propagating root -> leaves...",
            2)
        from scipy.interpolate import interp1d
        for node in self.tree.find_clades(order='preorder'):

            ## The root node
            if node.up is None:
                node.msg_from_parent = None  # nothing beyond the root
            # all other cases (All internal nodes + unconstrained terminals)
            elif node.date_constraint is not None and node.date_constraint.is_delta:
                node.marginal_pos_LH = node.date_constraint
            else:
                parent = node.up
                # messages from the complementary subtree (iterate over all sister nodes)
                complementary_msgs = [
                    sister.marginal_pos_Lx for sister in parent.clades
                    if (sister != node) and (
                        sister.marginal_pos_Lx is not None)
                ]

                # if parent itself got smth from the root node, include it
                if parent.msg_from_parent is not None:
                    complementary_msgs.append(parent.msg_from_parent)

                if len(complementary_msgs):
                    msg_parent_to_node = NodeInterpolator.multiply(
                        complementary_msgs)
                    msg_parent_to_node._adjust_grid(rel_tol=self.rel_tol_prune)
                else:
                    x = [parent.numdate, numeric_date()]
                    msg_parent_to_node = NodeInterpolator(
                        x, [1.0, 1.0], min_width=self.min_width)

                # integral message, which delivers to the node the positional information
                # from the complementary subtree
                res, res_t = NodeInterpolator.convolve(
                    msg_parent_to_node,
                    node.branch_length_interpolator,
                    max_or_integral='integral',
                    inverse_time=False,
                    n_grid_points=self.node_grid_points,
                    n_integral=self.n_integral,
                    rel_tol=self.rel_tol_refine)

                node.msg_from_parent = res
                if node.marginal_pos_Lx is None:
                    node.marginal_pos_LH = node.msg_from_parent
                else:
                    node.marginal_pos_LH = NodeInterpolator.multiply(
                        (node.msg_from_parent, node.subtree_distribution))

                self.logger(
                    'ClockTree._ml_t_root_to_leaves: computed convolution'
                    ' with %d points at node %s' % (len(res.x), node.name), 4)

                if self.debug:
                    tmp = np.diff(res.y - res.peak_val)
                    nsign_changed = np.sum((tmp[1:] * tmp[:-1] < 0) &
                                           (res.y[1:-1] - res.peak_val < 500))
                    if nsign_changed > 1:
                        import matplotlib.pyplot as plt
                        plt.ion()
                        plt.plot(res.x, res.y - res.peak_val, '-o')
                        plt.plot(
                            res.peak_pos - node.branch_length_interpolator.x,
                            node.branch_length_interpolator(
                                node.branch_length_interpolator.x) -
                            node.branch_length_interpolator.peak_val, '-o')
                        plt.plot(
                            msg_parent_to_node.x,
                            msg_parent_to_node.y - msg_parent_to_node.peak_val,
                            '-o')
                        plt.ylim(0, 100)
                        plt.xlim(-0.05, 0.05)
                        import ipdb
                        ipdb.set_trace()

            # assign positions of nodes and branch length only when desired
            # since marginal reconstruction can result in negative branch length
            if assign_dates:
                node.time_before_present = node.marginal_pos_LH.peak_pos
                if node.up:
                    node.clock_length = node.up.time_before_present - node.time_before_present
                    node.branch_length = node.clock_length

            # construct the inverse cumulant distribution to evaluate confidence intervals
            if node.marginal_pos_LH.is_delta:
                node.marginal_inverse_cdf = interp1d(
                    [0, 1],
                    node.marginal_pos_LH.peak_pos * np.ones(2),
                    kind="linear")
            else:
                dt = np.diff(node.marginal_pos_LH.x)
                y = node.marginal_pos_LH.prob_relative(node.marginal_pos_LH.x)
                int_y = np.concatenate(
                    ([0], np.cumsum(dt * (y[1:] + y[:-1]) / 2.0)))
                int_y /= int_y[-1]
                node.marginal_inverse_cdf = interp1d(int_y,
                                                     node.marginal_pos_LH.x,
                                                     kind="linear")
                node.marginal_cdf = interp1d(node.marginal_pos_LH.x,
                                             int_y,
                                             kind="linear")

        if not self.debug:
            _cleanup()

        return
Beispiel #34
0
# package tests.beta scope global params
import os
import matplotlib.pyplot as plt
plt.ion() # all beta/test_ plot disappear automatically

# MTPY_ROOT='/Softlab/Githubz/mtpy'    # source code root dir
MTPY_ROOT='/g/data/ha3/fxz547/Githubz/mtpyclone'    # source code root dir
# MTPY_ROOT='E:/Githubz/mtpy'    # source code root dir

EDI_DATA_DIR = os.path.join(MTPY_ROOT,'examples/data/edi_files')
EDI_DATA_DIR2 = os.path.join(MTPY_ROOT,'examples/data/edi_files_2')

AUS_TOPO_FILE = os.path.join(MTPY_ROOT,'examples/data/AussieContinent_etopo1.asc')

# path to directory containing model input files - samples reference for compare
SAMPLE_DIR = os.path.join(MTPY_ROOT,'examples/model_files') # r'E:\Githubz\mtpy\examples\model_files'

# test runs output directory where to save plots/files to
TEMP_OUT_DIR = os.path.join(MTPY_ROOT,'temp/beta_out_dir')  # r'E:\Githubz\mtpy\temp'
print('\nSecond dose administered per day(avg) :','{:,}'.format(math.ceil(secondDose/(len(IndiaVaccineData)-28))))

cur.execute('''insert into dose_distribution (state, first_dose, second_dose) 
values ((%s),(%s),(%s)) on conflict(state) do update set first_dose = (%s), second_dose = (%s)''',
('Puducherry', firstDose, secondDose, firstDose, secondDose,))
conn.commit()

xPoints = ['First dose', 'Second dose']
yPoints = [firstDose, secondDose]
values = pd.DataFrame({'xPoints':xPoints, 'yPoints':yPoints})
pyplot.figure(figsize = (9,4))
pyplot.pie(yPoints, explode = (0,0.1), labels = xPoints, colors = ['yellow', 'orange'], autopct = '%1.1f%%', shadow = True)
pyplot.axis('equal')
pyplot.title('Doses administered')
pyplot.savefig(r'C:\Users\Mittu\Desktop\CovidDataAnalysis\Covid-Data-Analysis\Graphs\PuducherryDoses.png')
pyplot.ion()
pyplot.close()

xPoints = list(range(1,96))
yPoints = IndiaVaccineData['First Dose Administered'].sub(IndiaVaccineData['First Dose Administered'].shift())
yPoints = yPoints[1:]
pyplot.plot(xPoints, yPoints/1000)
pyplot.title('Daily administration of first dose')
pyplot.ylabel('First Dose Administered in thousands')
pyplot.xlabel('Days')
pyplot.savefig(r'C:\Users\Mittu\Desktop\CovidDataAnalysis\Covid-Data-Analysis\Graphs\PuducherryFirstDose.png')
pyplot.ion()
pyplot.close()

xPoints = list(range(1,96))
yPoints = IndiaVaccineData['Second Dose Administered'].sub(IndiaVaccineData['Second Dose Administered'].shift())
        if j == i:
            continue
        result *= (x - x_vec[j]) / (x_vec[i] - x_vec[j])
    return result


#the entire lagrangian
def L(x, x_vec, f_vec):
    result = 0
    for i in range(0, len(x_vec)):
        result += f_vec[i] * l(i, x, x_vec)
    return result


##############################  GENERATING THE PLOT ############################
plt.ion()  #turn interactive mode on
FIG = plt.figure("Slider Bar Lagrangian Plot Window")  #create a global figure
AXES = FIG.add_subplot(111)  #add a plot to it (because it was empty)
#and create a global axis handle
AXES.set_xlim(-10, 10)
AXES.set_ylim(-2, 2)
FINE = np.linspace(-10, 10, 200)
ARCTAN, = AXES.plot(FINE, [arctan(x) for x in FINE], 'k-', label='arctan(x)')
PRESETS = linspace(-10, 10, 6)
PRESET_F = [arctan(x) for x in PRESETS]
SAMPLE, = AXES.plot(PRESETS, [arctan(x) for x in PRESETS],
                    'ro',
                    label='sample')
LAGRANGIAN, = AXES.plot(FINE, [L(x, PRESETS, PRESET_F) for x in FINE],
                        'b-',
                        label='lagrangian')
Beispiel #37
0
def plot_barcode_fitness(barcode_frame,
                         notebook_dir,
                         experiment=None,
                         show_plots=True,
                         save_plots=False,
                         inducer_conc_list=None,
                         plot_range=None,
                         inducer="IPTG"):

    if plot_range is not None:
        barcode_frame = barcode_frame.iloc[plot_range[0]:plot_range[1]]

    if experiment is None:
        experiment = get_exp_id(notebook_dir)

    if inducer_conc_list is None:
        inducer_conc_list = [0, 2]
        for i in range(10):
            inducer_conc_list.append(2 * inducer_conc_list[-1])

    # Turn interactive plotting on or off depending on show_plots
    if show_plots:
        plt.ion()
    else:
        plt.ioff()

    if save_plots:
        pdf_file = 'barcode fitness plots.pdf'
        pdf = PdfPages(pdf_file)

    data_directory = notebook_dir + "\\barcode_analysis"
    os.chdir(data_directory)

    #plot fitness curves
    plt.rcParams["figure.figsize"] = [12, 8 * (len(barcode_frame))]
    fig, axs = plt.subplots(len(barcode_frame), 1)
    x = inducer_conc_list
    linthreshx = min([i for i in inducer_conc_list if i > 0])

    plot_colors = sns.color_palette()

    for (index, row), ax in zip(barcode_frame.iterrows(),
                                axs):  # iterate over barcodes
        for initial in ["b", "e"]:
            y = row[f"fitness_0_estimate_{initial}"]
            s = row[f"fitness_0_err_{initial}"]
            fill_style = "full" if initial == "b" else "none"
            ax.errorbar(x,
                        y,
                        s,
                        marker='o',
                        ms=10,
                        color=plot_colors[0],
                        fillstyle=fill_style)
            y = row[f"fitness_tet_estimate_{initial}"]
            s = row[f"fitness_tet_err_{initial}"]
            ax.errorbar(x,
                        y,
                        s,
                        marker='^',
                        ms=10,
                        color=plot_colors[1],
                        fillstyle=fill_style)

            if initial == "b":
                barcode_str = str(index) + ', '
                barcode_str += str(row['total_counts']) + ", "
                barcode_str += row['RS_name'] + ": "
                barcode_str += row['forward_BC'] + ", "
                barcode_str += row['reverse_BC']
                ax.text(x=0.0,
                        y=1.03,
                        s=barcode_str,
                        horizontalalignment='left',
                        verticalalignment='top',
                        transform=ax.transAxes,
                        fontsize=12)
                ax.set_xscale('symlog', linthreshx=linthreshx)
                ax.set_xlim(-linthreshx / 10, 2 * max(x))
                ax.set_xlabel(f'[{inducer}] (umol/L)', size=20)
                ax.set_ylabel('Fitness (log(10)/plate)', size=20)
                ax.tick_params(labelsize=16)

    if save_plots:
        pdf.savefig()
    if not show_plots:
        plt.close(fig)

    if save_plots:
        pdf.close()
Beispiel #38
0
def plot_bar_seq_read_fractions(barcode_frame,
                                notebook_dir,
                                experiment=None,
                                show_plots=True,
                                save_plots=False,
                                num_to_plot=None):

    # Turn interactive plotting on or off depending on show_plots
    if show_plots:
        plt.ion()
    else:
        plt.ioff()

    if save_plots:
        pdf_file = 'barcode read fraction plots.pdf'
        pdf = PdfPages(pdf_file)

    data_directory = notebook_dir + "\\barcode_analysis"
    os.chdir(data_directory)

    if experiment is None:
        experiment = get_exp_id(notebook_dir)

    #Plot read fraction across all samples for first several barcodes
    plt.rcParams["figure.figsize"] = [16, 6 * num_to_plot]
    fig, axs = plt.subplots(num_to_plot, 1)

    f_data = barcode_frame[:num_to_plot]

    plot_colors = sns.hls_palette(12, l=.4, s=.8)

    plot_colors12 = []
    for c in plot_colors:
        for i in range(8):
            plot_colors12.append(c)

    for index, row in f_data.iterrows():
        y = []
        x = []
        y_for_scale = []
        for i, t in enumerate(wells_by_column()):
            y.append(row["fraction_" + t])
            x.append(i + 1)
            if (row["fraction_" + t]) > 0:
                y_for_scale.append(row["fraction_" + t])

        axs[index].scatter(x, y, c=plot_colors12, s=70)
        axs[index].set_ylim(0.5 * min(y_for_scale), 2 * max(y))
        axs[index].set_yscale("log")
        barcode_str = str(index) + ', '
        if row['RS_name'] != "": barcode_str += row['RS_name'] + ", "
        barcode_str += row['forward_BC'] + ', ' + row['reverse_BC']
        axs[index].text(x=0.05,
                        y=0.95,
                        s=barcode_str,
                        horizontalalignment='left',
                        verticalalignment='top',
                        transform=axs[index].transAxes,
                        fontsize=14)

        for i in range(13):
            axs[index].plot([i * 8 + 0.5, i * 8 + 0.5],
                            [0.6 * min(y_for_scale), 1.2 * max(y)],
                            color='gray')
    axs[0].set_title("Read Fraction Per Barcode", fontsize=32)
    if save_plots:
        pdf.savefig()
    if not show_plots:
        plt.close(fig)

    if save_plots:
        pdf.close()
Beispiel #39
0
    def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(n_feature, n_hidden)  # hidden layer
        self.out = torch.nn.Linear(n_hidden, n_output)  # output layer

    def forward(self, x):
        x = F.relu(self.hidden(x))  # activation function for hidden layer
        x = self.out(x)
        return x


net = Net(n_feature=2, n_hidden=10, n_output=2)
optimizer = torch.optim.SGD(net.parameters(), lr=0.1)
loss_func = torch.nn.CrossEntropyLoss()

plt.ion()  # something about plotting

for t in range(100):
    out = net(x)  # input x and predict based on x
    loss = loss_func(
        out, y
    )  # must be (1. nn output, 2. target), the target label is NOT one-hotted

    optimizer.zero_grad()  # clear gradients for next train
    loss.backward()  # backpropagation, compute gradients
    optimizer.step()  # apply gradients

    if t % 2 == 0:
        # plot and show learning process
        plt.cla()
        prediction = torch.max(out, 1)[1]
Beispiel #40
0
def bar_seq_threshold_plot(notebook_dir,
                           experiment=None,
                           save_plots=False,
                           cutoff=None,
                           hist_bin_max=None,
                           num_bins=50,
                           barcode_file=None):

    # Turn interactive plotting on or off depending on show_plots
    plt.ion()

    if save_plots:
        pdf_file = 'barcode histogram plot.pdf'
        pdf = PdfPages(pdf_file)

    #os.chdir(notebook_dir)

    if experiment is None:
        experiment = get_exp_id(notebook_dir)

    print(
        f"Importing BarSeq count data and plotting histogram for thresholding for experiment: {experiment}"
    )

    data_directory = notebook_dir + "\\barcode_analysis"
    os.chdir(data_directory)

    if barcode_file is None:
        barcode_file = glob.glob("*.sorted_counts.csv")[0]
    print(f"Importing BarSeq count data from file: {barcode_file}")
    barcode_frame_0 = pd.read_csv(barcode_file, skipinitialspace=True)

    barcode_frame_0.sort_values('total_counts', ascending=False, inplace=True)
    barcode_frame_0.reset_index(drop=True, inplace=True)

    if hist_bin_max is None:
        hist_bin_max = barcode_frame_0[int(len(barcode_frame_0) /
                                           50):int(len(barcode_frame_0) / 50) +
                                       1]["total_counts"].values[0]

    #Allow user to replot with different hist_bin_max
    interact_hist = interact.options(manual=True,
                                     manual_name="(re)plot histogram")

    @interact_hist()
    def plot_histogram(hist_max=str(hist_bin_max)):
        #Plot histogram of Barcode counts to enable decision about threshold

        plt.rcParams["figure.figsize"] = [16, 8]
        fig, axs = plt.subplots(1, 2)
        try:
            hist_bin_max = float(hist_max)
            bins = np.linspace(-0.5, hist_bin_max + 0.5, num_bins)
            for ax in axs.flatten():
                ax.hist(barcode_frame_0['total_counts'], bins=bins)
                ax.set_xlabel('Barcode Count', size=20)
                ax.set_ylabel('Number of Barcodes', size=20)
                ax.tick_params(labelsize=16)
            axs[0].hist(barcode_frame_0['total_counts'],
                        bins=bins,
                        histtype='step',
                        cumulative=-1)
            axs[0].set_yscale('log')
            axs[1].set_yscale('log')
            axs[1].set_xlim(0, hist_bin_max / 3)
        except:
            print("hist_max needs to be a number")

    if save_plots:
        pdf.savefig()

    if save_plots:
        pdf.close()

    return barcode_frame_0
Beispiel #41
0
def barplot(labels, data):
    pos = np.arange(len(data))
    plt.ion()
    plt.xticks(pos + 0.4, labels)
    plt.bar(pos, data)
    plt.grid('on')
Beispiel #42
0
def total_plate_2_and_plot_bar_seq_quality(barcode_frame,
                                           notebook_dir,
                                           experiment=None,
                                           show_plots=True,
                                           save_plots=False,
                                           cutoff=None,
                                           num_to_plot=None,
                                           export_trimmed_file=False,
                                           trimmed_export_file=None):

    # Turn interactive plotting on or off depending on show_plots
    if show_plots:
        plt.ion()
    else:
        plt.ioff()

    if save_plots:
        pdf_file = 'barcode quality plots.pdf'
        pdf = PdfPages(pdf_file)

    ref_seq_file = "reference_sequences.csv"
    ref_seq_file_found = False
    top_directory = notebook_dir
    while not ref_seq_file_found:
        find_result = top_directory.rfind("\\")
        if find_result == -1:
            break
        else:
            top_directory = top_directory[:find_result]
            os.chdir(top_directory)
            ref_seq_file_found = os.path.isfile(ref_seq_file)

    if ref_seq_file_found:
        ref_seq_frame = pd.read_csv(ref_seq_file, skipinitialspace=True)
    else:
        ref_seq_frame = None

    data_directory = notebook_dir + "\\barcode_analysis"
    os.chdir(data_directory)

    if experiment is None:
        experiment = get_exp_id(notebook_dir)

    if cutoff is None:
        hist_bin_max = barcode_frame[int(len(barcode_frame) /
                                         50):int(len(barcode_frame) / 50) +
                                     1]["total_counts"].values[0]
        cutoff = int(hist_bin_max / 10)
    print(f"Barcode frequency cutoff: {cutoff}")

    #drop_list = list(barcode_frame[barcode_frame["total_counts"]<cutoff].index)
    #barcode_frame.drop(drop_list, inplace=True)

    barcode_frame = barcode_frame[
        barcode_frame["total_counts"] > cutoff].copy()
    barcode_frame.reset_index(drop=True, inplace=True)
    if export_trimmed_file:
        if trimmed_export_file is None:
            trimmed_export_file = f"{experiment}.trimmed_sorted_counts.csv"
        print(
            f"Exporting trimmed barcode counts data to: {trimmed_export_file}")
        barcode_frame.to_csv(trimmed_export_file)

    for w in wells():
        label = 'fraction_' + w
        barcode_frame[label] = barcode_frame[w] / barcode_frame[w].sum()

    barcode_frame['fraction_total'] = barcode_frame[
        'total_counts'] / barcode_frame['total_counts'].sum()

    plot_colors = sns.hls_palette(12, l=.4, s=.8)

    plot_colors12 = []
    for c in plot_colors:
        for i in range(8):
            plot_colors12.append(c)

    BC_totals = []
    index_list = []
    for i, w in enumerate(wells()):
        BC_totals.append(barcode_frame[w].sum())
        index_list.append(i + 1)

    BC_total_arr = []
    for r in rows():
        subarr = []
        for c in columns():
            subarr.append(barcode_frame[r + str(c)].sum())
        BC_total_arr.append(subarr)

    #Plot barcode read counts across plate
    plt.rcParams["figure.figsize"] = [12, 16]
    fig, axs = plt.subplots(2, 1)

    r12 = np.asarray(np.split(np.asarray(BC_totals), 8)).transpose().flatten()

    axs[0].scatter(index_list, r12, c=plot_colors12, s=70)
    for i in range(13):
        axs[0].plot([i * 8 + 0.5, i * 8 + 0.5],
                    [min(BC_totals), max(BC_totals)],
                    color='gray')
    axs[0].set_title("Total Read Counts Per Sample", fontsize=32)
    #axs[0].set_yscale('log');

    axs[0].set_xlim(0, 97)
    axs[0].set_xlabel('Sample Number', size=20)
    axs[0].set_ylabel('Total Reads per Sample', size=20)
    axs[0].tick_params(labelsize=16)

    axs[1].matshow(BC_total_arr, cmap="inferno")
    axs[1].grid(b=False)
    axs[1].set_xticklabels([i + 1 for i in range(12)], size=16)
    axs[1].set_xticks([i for i in range(12)])
    axs[1].set_yticklabels([r + " " for r in rows()[::-1]], size=16)
    axs[1].set_yticks([i for i in range(8)])
    axs[1].set_ylim(-0.5, 7.5)
    axs[1].tick_params(length=0)
    if save_plots:
        pdf.savefig()
    if not show_plots:
        plt.close(fig)

    name_list = [""] * len(barcode_frame)
    barcode_frame["RS_name"] = name_list

    if ref_seq_frame is not None:
        for index, row in ref_seq_frame.iterrows():
            display_frame = barcode_frame[
                barcode_frame["forward_BC"].str.contains(
                    row["forward_lin_tag"])]
            display_frame = display_frame[
                display_frame["reverse_BC"].str.contains(
                    row["reverse_lin_tag"])]
            display_frame = display_frame[[
                "RS_name", "forward_BC", "reverse_BC", "total_counts"
            ]]
            if len(display_frame) > 0:
                display_frame["RS_name"].iloc[0] = row["RS_name"]
                barcode_frame.loc[display_frame.index[0],
                                  "RS_name"] = row["RS_name"]
            display(display_frame)

        total_reads = barcode_frame["total_counts"].sum()
        print(f"total reads: {total_reads}")
        total_RS_reads = barcode_frame[
            barcode_frame["RS_name"] != ""]["total_counts"].sum()
        print(f"reference sequence reads: {total_RS_reads}")

    total = []
    for index, row in barcode_frame[wells_by_column()[:24]].iterrows():
        counts = 0
        for t in wells_by_column()[:24]:
            counts += row[t]
        total.append(counts)
    barcode_frame['total_counts_plate_2'] = total
    barcode_frame['fraction_total_p2'] = barcode_frame[
        'total_counts_plate_2'] / barcode_frame['total_counts_plate_2'].sum()

    #Plot Barcode fraction for each well in time point 1 vs. mean fraction in time point 1
    plt.rcParams["figure.figsize"] = [16, 16]
    fig, axs = plt.subplots(2, 2)
    if num_to_plot is None:
        f_data = barcode_frame
    else:
        f_data = barcode_frame[:num_to_plot]

    f_x = f_data['fraction_total_p2']
    for ax in axs.flatten()[:2]:
        ax.plot([0, .125], [0, .125], color='k')
    for ax in axs.flatten()[2:4]:
        ax.plot([0, .125], [0, 0], color='k')
    for i, w in enumerate(wells_by_column()[:24]):
        c = [(plot_colors * 8)[i]] * len(f_data)
        for ax in axs.flatten()[:2]:
            ax.scatter(f_x, f_data['fraction_' + w], c=c)
        for ax in axs.flatten()[2:4]:
            ax.scatter(f_x, (f_data['fraction_' + w] - f_x) * 100, c=c)

    axs.flatten()[1].set_xscale("log")
    axs.flatten()[1].set_yscale("log")
    axs.flatten()[1].set_xlim(0.01, 0.125)
    axs.flatten()[1].set_ylim(0.01, 0.125)

    axs.flatten()[3].set_xscale("log")
    axs.flatten()[3].set_xlim(0.01, 0.125)
    fig.suptitle('Fraction from Each Dual Barcode (Plate 2)',
                 fontsize=24,
                 position=(0.5, 0.905))

    for ax in axs.flatten()[:2]:
        ax.set_xlabel('Fraction Total', size=20)
        ax.set_ylabel('Fraction per Sample', size=20)
        ax.tick_params(labelsize=16)

    for ax in axs.flatten()[2:4]:
        ax.set_xlabel('Fraction Total', size=20)
        ax.set_ylabel('Fraction per Sample - Fraction Total (%)', size=20)
        ax.tick_params(labelsize=16)
    if save_plots:
        pdf.savefig()
    if not show_plots:
        plt.close(fig)

    return barcode_frame
Beispiel #43
0
def piechart(labels, data):
    plt.ion()
    fig = plt.figure(figsize=(7, 7))
    plt.pie(data, labels=labels, autopct='%1.2f%%')
    plt.draw()
    return fig
Beispiel #44
0
print("loading test_loader")
folder_test_input = folder2.ImageFolder(root=dir_test_input,
                                        transform=transforms.ToTensor(),
                                        loader=folder2.pil_loader)
test_loader_input = torch.utils.data.DataLoader(folder_test_input,
                                                batch_size=2,
                                                shuffle=False)

folder_test_output = folder2.ImageFolder(root=dir_test_output,
                                         transform=transforms.ToTensor(),
                                         loader=folder2.pil_loader)
test_loader_output = torch.utils.data.DataLoader(folder_test_output,
                                                 batch_size=2,
                                                 shuffle=False)
print("loading complete")
"""
# show image
f, a = plt.subplots(3, N_TEST_IMG, figsize=(5,3))
plt.ion()

for i in range(N_TEST_IMG):
    a[0][i].imshow(view_data_in.data.cpu().numpy()[i].reshape(512, 350), cmap='gray')
    a[0][i].set_xticks(()) 
    a[0][i].set_yticks(())
    a[1][i].imshow(view_data_out.data.cpu().numpy()[i].reshape(512, 350), cmap='gray')
    a[1][i].set_xticks(())
    a[1][i].set_yticks(())
"""
for epoch in range(EPOCH):
    train_input_iter = iter(train_loader_input)
    train_output_iter = iter(train_loader_output)
Beispiel #45
0
def output(vsd=0.1,
           vb_low=0.0,
           vb_high=80.0,
           v_step=4,
           step=100,
           delay=100,
           pair='0.5um'):

    vsd = float(vsd)
    vb_low = float(vb_low)
    vb_high = float(vb_high)

    #    temp_A = lakeshore.ask('KRDG? A')
    #    T =float(temp_A.split('+')[1])

    sd_sweep(start=0.0, end=-vsd, step=1000, delay=100)
    bg_sweep(start=0.0, end=vb_low, step=4000, delay=100)

    Range = 1.1 * (math.fabs(vsd))
    source.write(':SOUR:VOLT:RANG ' + str(Range))
    source.write(':SOUR:DEL ' + str(delay / 1000))
    stage = 2 * vsd / step
    v_stage = (vb_high - vb_low) / v_step

    #real-time plotting
    plt.ion()

    for j in range(v_step + 1):

        clear_data()

        filename = time.strftime("%Y%m%d", time.localtime()) +'-'+'output'+'-'+'pair'+pair+'-'+'vsd'+'-'+str(vsd*1000)+'mV' \
                   +'-'+'vbg'+'-'+ str(vb_low+j*v_stage)+ 'V' + '-'  + '20K'

        if filename[-4:] != '.csv':
            filename += '.csv'

        #plt.figure(vb_low + j * v_stage)

        for i in range(step + 1):

            Vs = -vsd + stage * i
            source.write(':source:volt %s' % Vs)
            #source.write('read?')

            data_bg = backgate.read("TRACE:DATA")
            data_sd = source.read("TRACE:DATA")
            # temp_A = lakeshore.ask('KRDG? A')

            Ig = float(data_bg.split(',')[1]) * 1E9
            Vg = float(vb_low + j * v_stage)
            Is = float(data_sd.split(',')[1])
            Vs = float(Vs)
            Rs = math.fabs(Vs / Is)
            # T = float(temp_A.split('+')[1])

            Vg_list.append(Vg)
            Ig_list.append(Ig)
            Vsd_list.append(Vs)
            Isd_list.append(Is)
            Rsd_list.append(Rs)
            #T_list.append(T)

            plt.subplot(2, 2, 1)
            plt.plot(Vs, Is, 'b.')
            plt.xlabel('Vsd (V)')
            plt.ylabel('Isd (A)')
            plt.grid(True)

            plt.subplot(2, 2, 2)
            if Is > 0:
                plt.semilogy(Vs, Is, 'b.')
                plt.xlabel('Vsd (V)')
                plt.ylabel('Isd (A)')
                plt.grid(True)

            plt.subplot(2, 2, 3)
            plt.plot(Vs, Rs, 'b.')
            plt.xlabel('Vsd (V)')
            plt.ylabel('Rsd (Ohm)')
            plt.grid(True)

            plt.subplot(2, 2, 4)
            plt.plot(Vs, Ig, 'r.')
            plt.xlabel('Vsd (V)')
            plt.ylabel('Ig (nA)')
            plt.grid(True)

            plt.tight_layout()
            plt.pause(0.0001)

            savefig(filename[:-4] + '.png')
            plt.ioff()

            csv_out = open(filename, 'wb')
            mywriter = csv.writer(csv_out)
            mywriter.writerow(('Vg', 'Ig(nA)', 'Vsd', 'Isd', 'Rsd'))

            for row in zip(Vg_list, Ig_list, Vsd_list, Isd_list, Rsd_list):
                mywriter.writerow(row)

            csv_out.close()

        if j != v_step:
            bg_sweep(start=vb_low + j * v_stage,
                     end=vb_low + (j + 1) * v_stage,
                     step=4000,
                     delay=100)
            sd_sweep(start=vsd, end=-vsd, step=1000, delay=100)

    bg_sweep(start=vb_high, end=0.0, step=4000, delay=100)
    sd_sweep(start=vsd, end=0.0, step=1000, delay=100)
Beispiel #46
0
def scatterplot(x, y):
    plt.ion()
    plt.plot(x, y, 'b.')
    plt.xlim(min(x) - 1, max(x) + 1)
    plt.ylim(min(y) - 1, max(y) + 1)
    plt.draw()
    def on_plot_salinity(self):
        logger.debug("plot salinity")

        with rc_context(self.rc_context):

            plt.ion()

            # figure and canvas
            plt.close("Plot Salinity")
            fig = plt.figure("Plot Salinity", figsize=self.f_sz, dpi=self.f_dpi)
            fig.patch.set_alpha(0.0)
            ax = fig.add_subplot(111)
            ax.invert_yaxis()

            y_min = None
            y_max = None
            x_min = None
            x_max = None

            # actually do the export
            for pk in self._pks:

                success = self.lib.load_profile(pk, skip_atlas=True)
                if not success:

                    # noinspection PyCallByClass
                    QtGui.QMessageBox.warning(self, "Database", "Unable to load profile #%02d!" % pk, QtGui.QMessageBox.Ok)
                    continue

                _x_min = self.lib.cur.proc.sal[self.lib.cur.proc_valid].min()
                if x_min is None:
                    x_min = _x_min
                else:
                    x_min = min(x_min, _x_min)

                _x_max = self.lib.cur.proc.sal[self.lib.cur.proc_valid].max()
                if x_max is None:
                    x_max = _x_max
                else:
                    x_max = max(x_max, _x_max)

                _y_min = self.lib.cur.proc.depth[self.lib.cur.proc_valid].min()
                if y_min is None:
                    y_min = _y_min
                else:
                    y_min = min(y_min, _y_min)

                _y_max = self.lib.cur.proc.depth[self.lib.cur.proc_valid].max()
                if y_max is None:
                    y_max = _y_max
                else:
                    y_max = max(y_max, _y_max)

                _, = ax.plot(
                    self.lib.cur.proc.sal[self.lib.cur.proc_valid],
                    self.lib.cur.proc.depth[self.lib.cur.proc_valid],
                    label='#%03d' % pk
                )

            logger.debug("x: min %.2f, max %.2f" % (x_min, x_max))
            logger.debug("y: min %.2f, max %.2f" % (y_min, y_max))

            x_range = x_max - x_min
            y_range = y_max - y_min

            ax.legend(loc='lower left')
            plt.xlabel('Salinity [PSU]', fontsize=8)
            plt.ylabel('Depth [m]', fontsize=8)
            fig.get_axes()[0].set_xlim(x_min - 0.1*x_range, x_max + 0.1*x_range)
            fig.get_axes()[0].set_ylim(y_max + 0.15*y_range, y_min - 0.05*y_range)
            plt.grid()
            plt.show()

        self.accept()
Beispiel #48
0
def lockin_probe(vb_low=0.0, vb_high=80.0, step=100, delay=100):

    vb_low = float(vb_low)
    vb_high = float(vb_high)

    filename = time.strftime("%Y%m%d", time.localtime(
    )) + '-' + 'lockin-transfer' + '-' + 'sd-vxx' + '-' + 'vbg' + '-' + str(
        vb_low) + '-' + str(vb_high) + 'V' + '-' + '100K'

    if filename[-4:] != '.csv':
        filename += '.csv'

    bg_sweep(start=0.0, end=vb_low, step=1000, delay=100)

    Range = 1.1 * (math.fabs(vb_high) if math.fabs(vb_high) > math.fabs(vb_low)
                   else math.fabs(vb_low))
    backgate.write(':SOUR:VOLT:RANG ' + str(Range))
    backgate.write(':SOUR:DEL ' + str(delay / 1000))
    stage = (vb_high - vb_low) / step

    clear_data()

    #real-time plotting
    plt.ion()

    for i in range(step + 1):
        Vg = vb_low + stage * i
        backgate.write(':source:volt %s' % Vg)

        # backgate.write('read?')
        # data = inst.read(':CURR')

        data_bg = backgate.read("TRACE:DATA")
        temp_A = lakeshore.ask('KRDG? A')

        T = float(temp_A.split('+')[1])

        Ig = float(data_bg.split(',')[1])
        Vg = round(Vg, 3)

        X1 = math.fabs(float(lockin_1.ask('OUTP? 1')))
        X1 = math.fabs(float(lockin_1.ask('OUTP? 1')))
        Y1 = float(lockin_1.ask('OUTP? 2'))
        Y1 = float(lockin_1.ask('OUTP? 2'))
        R1 = float(lockin_1.ask('OUTP? 3'))
        R1 = float(lockin_1.ask('OUTP? 3'))
        theta1 = float(lockin_1.ask('OUTP? 4'))
        theta1 = float(lockin_1.ask('OUTP? 4'))

        X2 = math.fabs(float(lockin_2.ask('OUTP? 1')))
        X2 = math.fabs(float(lockin_2.ask('OUTP? 1')))
        Y2 = float(lockin_2.ask('OUTP? 2'))
        Y2 = float(lockin_2.ask('OUTP? 2'))
        R2 = float(lockin_2.ask('OUTP? 3'))
        R2 = float(lockin_2.ask('OUTP? 3'))
        theta2 = float(lockin_1.ask('OUTP? 4'))
        theta2 = float(lockin_1.ask('OUTP? 4'))

        X3 = math.fabs(float(lockin_3.ask('OUTP? 1')))
        X3 = math.fabs(float(lockin_3.ask('OUTP? 1')))
        Y3 = float(lockin_3.ask('OUTP? 2'))
        Y3 = float(lockin_3.ask('OUTP? 2'))
        R3 = float(lockin_3.ask('OUTP? 3'))
        R3 = float(lockin_3.ask('OUTP? 3'))
        theta3 = float(lockin_3.ask('OUTP? 4'))
        theta3 = float(lockin_3.ask('OUTP? 4'))

        R_2p = math.fabs(X1 / X3)
        R_4p = math.fabs(X2 / X3)
        Rc = (R_2p - R_4p * 1 / 1) / 2  # ohm

        T_list.append(T)

        Vg_list.append(Vg)
        Ig_list.append(Ig)

        X1_list.append(X1)
        Y1_list.append(Y1)
        R1_list.append(R1)
        theta1_list.append(theta1)

        X2_list.append(X2)
        Y2_list.append(Y2)
        R2_list.append(R2)
        theta2_list.append(theta2)

        X3_list.append(X3)
        Y3_list.append(Y3)
        R3_list.append(R3)
        theta3_list.append(theta3)

        R2p_list.append(R_2p)
        R4p_list.append(R_4p)
        Rc_list.append(Rc)

        plt.subplot(2, 5, 1)
        plt.plot(Vg_list, X1_list, r'b-D')
        plt.xlabel('Vbg (V)')
        plt.ylabel('V2p (V)')
        plt.grid(True)

        plt.subplot(2, 5, 3)
        plt.plot(Vg_list, X3_list, r'b-D')
        plt.xlabel('Vbg (V)')
        plt.ylabel('Isd (A)')
        plt.grid(True)

        plt.subplot(2, 5, 2)
        if R_2p > 0:
            plt.semilogy(Vg_list, R2p_list, r'b-D')
            plt.xlabel('Vbg (V)')
            plt.ylabel('R2p (Ohm)')
            plt.grid(True)

        plt.subplot(2, 5, 4)
        plt.plot(Vg_list, theta1_list, r'b-D')
        plt.xlabel('Vbg (V)')
        plt.ylabel('Theta_2p (degree)')
        plt.grid(True)

        plt.subplot(2, 5, 5)
        plt.plot(Vg_list, Ig_list, r'b-D')
        plt.xlabel('Vbg (V)')
        plt.ylabel('Ig (A)')
        plt.grid(True)

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

        plt.subplot(2, 5, 6)
        plt.plot(Vg_list, X2_list, r'b-D')
        plt.xlabel('Vbg (V)')
        plt.ylabel('V4p (V)')
        plt.grid(True)

        plt.subplot(2, 5, 8)
        if Rc > 0:
            plt.semilogy(Vg_list, Rc_list, r'b-D')
            plt.xlabel('Vbg (V)')
            plt.ylabel('Rc (ohm)')
            plt.grid(True)

        plt.subplot(2, 5, 7)
        if R_4p > 0:
            plt.semilogy(Vg_list, R4p_list, r'b-D')
            plt.xlabel('Vbg (V)')
            plt.ylabel('R4p (Ohm)')
            plt.grid(True)

        plt.subplot(2, 5, 9)
        plt.plot(Vg_list, theta2_list, r'b-D')
        plt.xlabel('Vbg (V)')
        plt.ylabel('Theta_4p (degree)')
        plt.grid(True)

        plt.subplot(2, 5, 10)
        plt.plot(Vg_list, T_list, r'b-D')
        plt.xlabel('Vbg (V)')
        plt.ylabel('T (K)')
        plt.grid(True)

        plt.tight_layout()

        plt.pause(0.0001)

        if i == step:
            savefig(filename[:-4] + '.png')
            plt.ioff()
            #plt.close()

    csv_out = open(filename, 'wb')
    mywriter = csv.writer(csv_out)
    mywriter.writerow(('Vg', 'V_2p', 'theta_2p', 'V_4p', 'theta_4p', 'R_2p',
                       'R_4p', 'Rc', 'Isd', 'Ig', 'T'))

    for row in zip(Vg_list, X1_list, Y1_list, X2_list, Y2_list, R2p_list,
                   R4p_list, Rc_list, X3_list, Ig_list, T_list):
        mywriter.writerow(row)

    csv_out.close()

    bg_sweep(start=vb_high, end=0.0, step=1000, delay=100)
Beispiel #49
0
from __future__ import print_function, division

import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import os
import copy

plt.ion()  # interactive mode

# Data augmentation and normalization for training
# Just normalization for validation
data_transforms = {
    'train':
    transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'val':
    transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
plt.show()

axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(-50, +50)
line, = axes.plot(xdata, ydata, 'ro')

for i in range(100):
    xdata.append(i)
    ydata.append(ysample[i])
    #xdata = i
    #ydata = ysample[i]
    line.set_xdata(xdata)
    line.set_ydata(ydata)
    plt.draw()
    plt.pause(1e-17)
    time.sleep(0.01)

# add this if you don't want the window to disappear at the end
plt.show()


'''
plt.ion()

for i in range(10):
    scat = plt.scatter(i, i)
    plt.pause(0.5)
    scat.remove()
'''
Beispiel #51
0
    # 通过print可以直接打印网络的结构
    # print(net)  # net 的结构
    # Net(
    #   (hidden): Linear(in_features=2, out_features=10, bias=True)
    #   (predict): Linear(in_features=10, out_features=2, bias=True)
    # )

    # optimizer 是训练的工具
    optimizer = torch.optim.SGD(net.parameters(), lr=0.02)
    # 传入 net 的所有参数, 学习率
    loss_func = torch.nn.CrossEntropyLoss()
    # 误差的计算方式,这里选用的交叉熵损失
    # 交叉熵损失是分类中常用的一种损失函数,表示数据的不确定程度,大概可以这么理解,越合理的分类结果(视觉上就是成堆聚在一块的分成一类),其交叉熵值越小,反之,则越大

    plt.ion()   # 画图
    plt.show()

    for t in range(100):
        res = net(x)  # 喂给 net 训练数据 x, 输出分析值

        # if t==90:
        #     print(res)

        loss = loss_func(res, y)  # 计算两者的误差

        optimizer.zero_grad()  # 清空上一步的残余更新参数值
        loss.backward()  # 误差反向传播, 计算参数更新值
        optimizer.step()  # 将参数更新值施加到 net 的 parameters 上

        # 接着上面来
Beispiel #52
0
 def __init__(self):
     plt.ion()
def train_policy_gradient_agent(num_episodes, max_episode_length, batch_size,
        learning_rate, gamma, regularisation, reward_to_exit=500, monitor=False,
        showPlot=True):
    # Initialise theta
    theta = np.random.randn(1,4)

    all_rewards = []
    if showPlot:
        plt.ion()
        fig = plt.figure()
        ax1 = fig.add_subplot(1,1,1)

    velocity = np.zeros((1,4), dtype='float32')

    last_avg_reward = 0

    if monitor:
        env.monitor.start('/tmp/cartpole-experiment-2')
    for i_episode in range(num_episodes):
        policy_gradients = []
        policy_gradient = 0
        batch_rewards = []
        for i_batch in range(batch_size):
            episode_rewards, episode_actions, episode_observations = run_episode(theta, max_episode_length)
            batch_rewards.append(sum(episode_rewards))

            # Now compute the policy gradient
            batch_i_gradient = compute_policy_gradient(episode_rewards,
                episode_actions, episode_observations, theta)

            policy_gradients.append(batch_i_gradient)

        mean_reward = np.mean(batch_rewards)
        policy_gradient = 0
        for i_batch in range(batch_size):
            policy_gradients[i_batch] = policy_gradients[i_batch] / float(batch_rewards[i_batch]) * (batch_rewards[i_batch] - mean_reward)
            policy_gradient = policy_gradient + 1.0 / batch_size * policy_gradients[i_batch]

        # If we are above reward_to_exit for two episodes, then stop training
        if (last_avg_reward > reward_to_exit) and (mean_reward >
                reward_to_exit):
            return theta
        last_avg_reward = mean_reward

        # Print the total reward for the episode
        if i_episode % 50 == 0:
            print('Reward', mean_reward, ', episode:', i_episode)

        if showPlot:
            all_rewards.append(mean_reward)
            ax1.clear()
            ax1.plot(all_rewards)
            plt.pause(0.0001)

        # Apply regularisation
        policy_gradient = policy_gradient - regularisation * sum(theta*theta)

        alpha = 30*learning_rate / (30 + i_episode)
        velocity = gamma * velocity + alpha * policy_gradient
        theta = theta + velocity

    if monitor:
        env.monitor.close()
    plt.show()

    # Finally, return the theta we find
    return theta
Beispiel #54
0
def socket_csi():
    """
        parameter:
        -channel 6
        -BW 20
        -package len 274
        -[0 1 2 3 ] magic bytes 0x11111111              4
        -[4 5 6 7 8 9 ] source mac addr                 6
        -[10 11 ] sequence number                       2
        -[12 13 ] <core and spatial number>             2
        -[14 15 ]  chanspec    channel/BW                2
        -[16 17 ] chip version                          2
        -[18 274 ]  real data                          256

 
    """
    #1 0100  0000 0001  0001 1000  0001 0011

    #10100               11000       10011

    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
    udp_socket.bind(("", 5500))
    global count
    count = 0
    print('[===============START===============]')

    # plt interactive
    plt.ion()
    # plt.rcParams['figure.figsize'] = (10, 10)
    # plt.rcParams['font.sans-serif'] = ['SimHei']
    # plt.rcParams['axes.unicode_mius'] = False
    # plt.rcParams['lines.linewidth'] = 1

    global compare_list
    compare_list = []

    global var_list_7
    var_list_7 = []

    global var_list_5
    var_list_5 = []

    global var_list_3
    var_list_3 = []

    global var_list_2
    var_list_2 = []

    compare_value = np.arange(100)
    global fig
    global ax
    fig, ax = plt.subplots()
    while 1:
        data, addr = udp_socket.recvfrom(1024)
        np_data = np.frombuffer(data, dtype=np.int16, offset=18)
        # print("np_data",np_data)
        print('-----------------')
        magic_bytes = []
        source_mac = []
        sequence_number = []
        core_spatial_number = []
        chan_spec = []
        chip_version = []
        real_data = []
        if len(data) != 274:
            continue
        # magic_bytes = [x for x in data[:4]]
        # print("magic_bytes:", magic_bytes, [hex(x) for x in magic_bytes])
        # source_mac = [x for x in data[4:10]]
        # print("source_mac:",source_mac,[hex(x) for x in source_mac])
        # sequence_number = [x for x in data[10:12]]
        # print("sequence_number:",sequence_number, [hex(x) for x in sequence_number])
        # core_spatial_number = [x for x in data[12:14]]
        # print("core_spatial_number:",core_spatial_number)
        # chanspec = [x for x in data[14:16]]
        # print("chanspec:",chanspec)
        # chip_version = [x for x in data[16:18]]
        # print("chip_version",chip_version)
        # real_data = [x for x in data[18:]]
        # print("real_data:",real_data,len(real_data))

        count += 1
        # if count >= 200:
        #     break

        treat_one_package(np_data, count)

        #control package number

    udp_socket.close()
    plt.ioff()
    plt.show()
Beispiel #55
0
Box(Point3D(-50, -50, 50), Point3D(50, 50, 50.1), world, material=Checkerboard(4, d65_white, d65_white, 0.001, 0.002))
# Box(Point(-100, -100, -100), Point(100, 100, 100), world, material=UniformSurfaceEmitter(d65_white, 0.001))

# OBSERVER --------------------------------------------------------------------

#import cProfile
#
# def profile_test(n=25000):
#     r = Ray(origin=Point(0.0, 0, 0), min_wavelength=526, max_wavelength=532, num_samples=100)
#     for i in range(n):
#         r.trace(world)
#
# cProfile.run("profile_test()", sort="tottime")

ion()

r = Ray(origin=Point3D(0.5, 0, -2.5), min_wavelength=440, max_wavelength=740, bins=800)
s = r.trace(world)
plot(s.wavelengths, s.samples)

r = Ray(origin=Point3D(0.5, 0, -2.5), min_wavelength=440, max_wavelength=740, bins=1600)
s = r.trace(world)
plot(s.wavelengths, s.samples)
show()

camera = PinholeCamera((128, 128), parent=world, transform=translate(0, 0, -2.5))
camera.spectral_rays = 1
camera.spectral_bins = 21
camera.pixel_samples = 50
    def train(self):
        ### Inicializando os pesos
        self.A = np.random.rand(self.Ns, self.Nh) * self.Aini

        ### Inicializando os centros
        self.t = np.zeros((1, self.Nh))

        idx = np.random.permutation(self.Nh)
        for j in xrange(self.Nh):
            self.t[0,j] = self.d[idx[j]]
        
        ### Inicializando as larguras
        self.R = abs(np.max(self.t) - np.min(self.t)) / 2

        self.tat = np.ones(self.t.shape) * self.delta0
        self.taA = np.ones(self.A.shape) * self.delta0
        grt_ant = grR_ant = grA_ant = 0

        MSE = np.zeros(self.epoch_max)
        plt.ion()

        for epoca in xrange(self.epoch_max):
            z = np.zeros(self.N)
            E = np.zeros(self.N)
            deltat = np.ones(self.t.shape)
            deltaA = np.ones(self.A.shape)
            gradt = gradR = gradA = 0

            index = np.random.permutation(self.N)

            for i in index:
                xi = self.X_train[i]#np.array([self.X_train[i]]).reshape(1, -1)
                theta = (xi - self.t) / self.R
                yj = self.sig_dev2(theta)
                z[i] = np.dot(self.A, yj.T)[0][0]

                e = self.d[i] - z[i]
                #self.A = self.A + (self.eta * e * yj)
                #self.t = self.t - (self.eta * e * self.A / self.R * self.sig_dev3(theta))
                #self.R = self.R - (((self.eta * e * self.A * (xi - self.t)) / self.R**2) * self.sig_dev3(theta))
                gradA += (-e * yj)
                gradt += (e * self.A / self.R * self.sig_dev3(theta))
                self.R = self.R - (self.delta0 * (e * self.A * (xi - self.t)) / self.R**2) * self.sig_dev3(theta)
                
                #self.R -= (self.delta0 * gradR)

                E[i] = 0.5 * e**2

            grt = np.sign(gradt)
            grA = np.sign(gradA)

            if epoca == 0:
                self.t += (-self.delta0 * gradt)
                self.R += (-self.delta0 * gradR)
                self.A += (-self.delta0 * gradA)
            else:
                Dt = grt * grt_ant
                DA = grA * grA_ant

                sizet = Dt.shape
                sizeA = DA.shape

                for i in xrange(sizet[0]):
                    for j in xrange(sizet[1]):
                        if (Dt[i,j] > 0):
                            self.tat[i,j] = min(self.tat[i,j] * self.eta_plus, self.delta_max)
                        elif (Dt[i,j] < 0):
                            self.tat[i,j] = max(self.tat[i,j] * self.eta_less, self.delta_min)

                        if (grt[i,j] > 0):
                            deltat[i,j] = -self.tat[i,j]
                        elif (grt[i,j] < 0):
                            deltat[i,j] = self.tat[i,j]
                
                for i in xrange(sizeA[0]):
                    for j in xrange(sizeA[1]):
                        if (DA[i,j] > 0):
                            self.taA[i,j] = min(self.taA[i,j] * self.eta_plus, self.delta_max)
                        elif (DA[i,j] < 0):
                            self.taA[i,j] = max(self.taA[i,j] * self.eta_less, self.delta_min)

                        if (grA[i,j] > 0):
                            deltaA[i,j] = -self.taA[i,j]
                        elif (grA[i,j] < 0):
                            deltaA[i,j] = self.taA[i,j]

                self.t += deltat
                self.A += deltaA
            
            grt_ant = grt
            grA_ant = grA

            MSE[epoca] = np.sum(E) / self.N

            if (epoca % 200 == 0 or epoca == self.epoch_max - 1):
                if (epoca != 0):
                    plt.cla()
                    plt.clf()
                
                self.plot(z, epoca)
        
        print MSE[-1]

        return MSE
Beispiel #57
0
def plot(st, inv=None, event=None, zoom=1, yinfo=False, stationlabel=True, epidistances=None, markphases=None, phaselabel=True, phaselabelclr='red',
        norm=False, clr=None, clrtrace=None, newfigure=True, savefig=False, dpi=400, xlabel=None, ylabel=None, yticks=False, t_axis=None,
        fs=15, tw=None, time_shift=None, verbose=False, kind='classic', labelfs=20, ylimit=None):
    """
    Alpha Version!

    Needs inventory and event of catalog for yinfo using

    param st: 	stream
    type  st:	obspy.core.stream.Stream

    param inv: inventory
    type  inv:

    param event: event of the seismogram
    type  event:

    param zoom: zoom factor of the traces
    type  zoom:	float

    param yinfo:	Plotting with y info as distance of traces
    type  yinfo:		bool

    param markphases: Phases, that should be marked in the plot, default is "None"
    type  markphases: list

    param norm: Depiction of traces; unprocessed or normalized. Normalization options are:
                all - normalized on biggest value of all traces
                trace - each trace is normalized on its biggest value
    type  norm: string or bool

    param clr: Color of plot
    type  clr: string

    param clrtrace: dict containing tracenumber and color, e.g.:
                        >>>		clrtrace = {1: 'red', 7: 'green'}
                        >>>		trace 1 in red
                        >>>		trace 7 in green
                    or
                    attribute a trace, so only traces with that attribute are plot in red, e.g.:
                        clrtrace='pocs', clrtrace='empty'

    type  clrtrace: list

    """

    #check for Data input
    if not isinstance(st, Stream):
        if not isinstance(st, Trace):
            try:
                if isinstance(yinfo,bool):
                    yinfo = 1
                plot_data(st, zoom=zoom, y_dist=yinfo, clr=clr, newfigure=newfigure, savefig=savefig, dpi=dpi, xlabel=xlabel, ylabel=ylabel, t_axis=t_axis, fs=fs)
                return
            except:
                msg = "Wrong data input, must be Stream or Trace"
                raise TypeError(msg)
    if newfigure:
        # Set axis information and bools.
        fig, ax = plt.subplots()
        if xlabel:
            ax.set_xlabel(xlabel, fontsize=fs)
        else:
            ax.set_xlabel("Time(s)", fontsize=fs)
        if ylabel:
            ax.set_ylabel(ylabel, fontsize=fs)

        ax.tick_params(axis='both', which='major', labelsize=fs)
        clr  = 'black'
        cclr = 'black'

    else:
        ax 	= plt.gca()
        fig = plt.gcf()
        if not clr:
            clr  = 'blue'
            cclr = 'blue'


    if isinstance(st, Stream):

        # Check if just specific timewindow should be plotted.
        try:
            tw = np.array(tw)
            twdelta = tw.max() - tw.min()
            t_axis = np.linspace(tw.min(),tw.max(), twdelta/float(st[0].stats.delta))
            npts_min = int(tw.min()/float(st[0].stats.delta))
            npts_max = int(tw.max()/float(st[0].stats.delta))
        except:
            t_axis_max = st[0].stats.delta * st[0].stats.npts
            t_axis = np.linspace(0,t_axis_max, st[0].stats.npts)
            tw = np.array([0, t_axis_max])
            npts_min = 0
            npts_max = st[0].stats.npts

        data = stream2array(st)

        if time_shift:
            data = np.roll(data, time_shift*int(st[0].stats.sampling_rate))

        spacing=2.
        ax.set_xlim(tw.min(), tw.max())
        isinv = False
        isevent = False


        # Check if inventory and catalog is input, then calculate distances etc.
        if isinstance(inv, Inventory): isinv   = True
        if isinstance(event,Event):    isevent = True

        if isinv:
            # Calculates y-axis info using epidistance information of the stream.
            # Check if there is a network entry
            attach_network_to_traces(st,inv)
            attach_coordinates_to_traces(st, inv, event)
        else:
            for trace in st:
                try:
                    if not trace.stats.distance:
                        isinv = False
                        break
                    else:
                        isinv = True
                except:
                        isinv = False
                        break

        try:
            depth = event.origins[0]['depth']/1000.
            isevent = True
        except AttributeError:
            try:
                depth = st[0].stats.depth
                isevent = True
            except AttributeError:
                isevent = False

        yold=0

        # Normalize Data, if set to 'all'
        if norm in ['all']:
            data = data/data.max()

        if yinfo:
            ymin = st[0].stats.distance
            ymax = st[0].stats.distance

        if markphases and isinv and isevent:
            try:
                origin = event.origins[0]['time']
                depth = event.origins[0]['depth']/1000.
            except:
                origin = st[0].stats.origin
                depth = st[0].stats.depth

            m = TauPyModel('ak135')



        if kind in ('classic', 'Classic'):
            for j, trace in enumerate(data):

                # Normalize trace, if set to 'trace'
                if norm in ['trace']:
                    trace = trace/trace.max()

                try:
                    y_dist = st[j].stats.distance
                except:
                    y_dist = yold + 1

                if markphases and isinv and isevent:

                    arrivals = m.get_travel_times(depth, y_dist, phase_list=markphases)
                    timetable = [ [], [] ]
                    for k, phase in enumerate(arrivals):
                        phase_name = phase.name
                        t = phase.time
                        phase_time = origin + t - st[j].stats.starttime
                        Phase_npt = int(phase_time/st[j].stats.delta)
                        Phase = Phase_npt * st[j].stats.delta

                        if Phase < t_axis.min() or Phase > t_axis.max():
                            continue
                        else:
                            timetable[0].append(phase_name)
                            timetable[1].append(Phase)

                    if not timetable[0] or not timetable[1]:
                        print('Phases not in Seismogram')
                        plt.close('all')
                        return

                    if yinfo:
                        if not ylabel: ax.set_ylabel("Distance(deg)", fontsize=fs)

                        if st[j].stats.distance < ymin: ymin = st[j].stats.distance
                        if st[j].stats.distance > ymax: ymax = st[j].stats.distance

                        try:
                            if j in clrtrace:
                                cclr = clrtrace[j]
                            else:
                                cclr = clr
                        except TypeError:
                            if clrtrace in st[j].stats:
                                cclr = 'red'
                            else:
                                cclr = clr
                        except:
                            cclr = clr

                        if stationlabel:
                            ax.annotate('%s' % st[j].stats.station, xy=(1 + tw.min(),y_dist+0.1))

                        ax.plot(t_axis,zoom*trace[npts_min: npts_max]+ y_dist, color=cclr)
                        ax.plot( (timetable[1],timetable[1]),(-1+y_dist,1+y_dist), color=phaselabelclr )
                        if verbose:
                            print(timetable[1] + st[j].stats.shifttime)
                            ax.plot( (timetable[1] + st[j].stats.shifttime, timetable[1] + st[j].stats.shifttime), \
                                    (-1+y_dist,1+y_dist), color=phaselabelclr )

                        if phaselabel:
                            for time, key in enumerate(timetable[0]):
                                ax.annotate('%s' % key, xy=(timetable[1][time],y_dist))
                                if verbose:
                                    ax.annotate('%s' % key, xy=(timetable[1][time] + st[j].stats.shifttime, y_dist))
                        else:
                            continue

                    else:
                        if not ylabel: ax.set_ylabel("No. of trace", fontsize=fs)

                        try:
                            if j in clrtrace:
                                cclr = clrtrace[j]
                            else:
                                cclr = clr
                        except TypeError:
                            if clrtrace in st[j].stats:
                                cclr = 'red'
                            else:
                                cclr = clr
                        except:
                            cclr = clr

                        fig.gca().yaxis.set_major_locator(plt.NullLocator())

                        if stationlabel:
                            ax.annotate('%s' % st[j].stats.station, xy=(1 + tw.min(),spacing*j+0.1))

                        ax.plot(t_axis,zoom*trace[npts_min: npts_max]+ spacing*j, color=cclr)
                        ax.plot( (timetable[1],timetable[1]),(-1+spacing*j,1+spacing*j), color=phaselabelclr )
                        if verbose:
                            print(st[j].stats.shifttime)
                            print(timetable[1] + st[j].stats.shifttime)

                            ax.plot( (timetable[1] + st[j].stats.shifttime, timetable[1] + st[j].stats.shifttime), \
                                    (-1+spacing*j,1+spacing*j), color=phaselabelclr )

                        if phaselabel:
                            for time, key in enumerate(timetable[0]):
                                ax.annotate('%s' % key, xy=(timetable[1][time],spacing*j))
                                if verbose:
                                    ax.annotate('%s' % key, xy=(timetable[1][time] + st[j].stats.shifttime, spacing*j))
                        else:
                            continue

                elif markphases and not isinv:
                    msg='markphases needs Inventory Information, not found.'
                    raise IOError(msg)

                elif markphases and not isevent:
                    msg='markphases needs Event Information, not found.'
                    raise IOError(msg)

                elif type(epidistances) == numpy.ndarray or type(epidistances)==list:
                    y_dist = epidistances
                    if not ylabel: ax.set_ylabel("Distance(deg)", fontsize=fs)
                    try:
                        if j in clrtrace:
                            cclr = clrtrace[j]
                        else:
                            cclr = clr
                    except:
                        cclr = clr

                    if stationlabel:
                        ax.annotate('%s' % st[j].stats.station, xy=(1 + tw.min(),y_dist[j]+0.1))

                    ax.plot(t_axis, zoom*trace[npts_min: npts_max] + y_dist[j], color=cclr)

                else:
                    if yinfo:

                        try:
                            if not ylabel: ax.set_ylabel("Distance(deg)", fontsize=fs)

                            try:
                                if j in clrtrace:
                                    cclr = clrtrace[j]
                                else:
                                    cclr = clr
                            except TypeError:
                                if clrtrace in st[j].stats:
                                    cclr = 'red'
                                else:
                                    cclr = clr
                            except:
                                cclr = clr

                        except:
                            msg='Oops, something not found.'
                            raise IOError(msg)

                        if st[j].stats.distance < ymin: ymin = st[j].stats.distance
                        if st[j].stats.distance > ymax: ymax = st[j].stats.distance

                        if stationlabel:
                            ax.annotate('%s' % st[j].stats.station, xy=(1 + tw.min(),y_dist+0.1))

                        ax.plot(t_axis,zoom*trace[npts_min: npts_max]+ y_dist, color=cclr)

                    else:
                        if not ylabel: ax.set_ylabel("No. of trace", fontsize=fs)
                        try:
                            if j in clrtrace:
                                cclr = clrtrace[j]
                            else:
                                cclr = clr
                        except:
                            cclr = clr

                        if stationlabel:
                            ax.annotate('%s' % st[j].stats.station, xy=(1 + tw.min(),spacing*j+0.1))

                        ax.plot(t_axis,zoom*trace[npts_min: npts_max]+ spacing*j, color=cclr)

                yold = y_dist

            if not yticks:
                fig.gca().yaxis.set_major_locator(plt.NullLocator())

        elif kind in ['contour', 'Contour']:
            yrange = np.arange(len(st))

            try:
                cax = ax.imshow(zoom*data[tw.min():tw.max()], aspect='auto', extent=(tw.min(), tw.max(), yrange.min(), yrange.max()),
                                origin='lower', cmap='seismic')
            except:
                cax = ax.imshow(zoom*data, aspect='auto', extent=(t_axis.min(), t_axis.max(), yrange.min(), yrange.max()),
                                origin='lower', cmap='seismic')


            if markphases and isinv and isevent:

                stats = np.arange(len(st))
                for j in stats:

                    arrivals = m.get_travel_times(depth, st[j].stats.distance, phase_list=markphases)
                    timetable = [ [], [] ]

                    for phase in arrivals:
                        t = phase.time
                        phase_time = origin + t - st[j].stats.starttime
                        Phase_npt = int(phase_time / st[j].stats.delta)
                        tPhase = Phase_npt * st[j].stats.delta
                        name = phase.name

                        if tPhase > t_axis.max() or tPhase < t_axis.min():
                            continue

                        ax.autoscale(False)

                        ax.plot( (tPhase, tPhase), (-labelfs/100. +j, labelfs/100.+j), color='red')

                        if j == stats.max():
                            if name in [u'P^220P']:
                                ax.annotate('$P^{220}P$', xy=(tPhase, j), xytext=(tPhase + 1, j+1.2),
                                            fontsize=labelfs, color='black')

                            elif name in [u'P^410P']:
                                ax.annotate('$P^{410}P$', xy=(tPhase, j), xytext=(tPhase + 1, j+1.2),
                                            fontsize=labelfs, color='black')

                            elif name in [u'P^660P']:
                                ax.annotate('$P^{660}P$', xy=(tPhase, j), xytext=(tPhase + 1, j+1.2),
                                            fontsize=labelfs, color='black')

                            else:
                                ax.annotate('$%s$' % name, xy=(tPhase, j), xytext=(tPhase + 1, j+1.2),
                                            fontsize=labelfs, color='black')

            cbar = fig.colorbar(cax, format='%.1f')
            cbar.set_clim(-1, 1)
            cbar.ax.tick_params(labelsize=fs)
            cbar.ax.set_ylabel('A', fontsize=fs)

        if yinfo:
            ylim = (ymin-1, ymax+1)
            ax.set_ylim(ylim)

        if savefig:
            fig.set_size_inches(8, 7)
            fig.savefig(savefig, dpi=dpi)
            plt.close("all")
        else:
            plt.ion()
            plt.draw()
            plt.show()
            plt.ioff()

    elif isinstance(st, Trace):

        t_axis = np.linspace(0, st.stats.delta * st.stats.npts, st.stats.npts)
        data = st.data.copy()

        if norm in ['all', 'All', 'trace', 'Trace']:
            data = data/data.max()
        try:
            y_dist = st.stats.distance
        except:
            print("No distance information attached to trace, no phases are calculated!")
            markphases=False

        if ylimit:
            ax.set_ylim(ylimit)

        if markphases:
            try:
                origin = event.origins[0]['time']
                depth = event.origins[0]['depth']/1000.
            except:
                origin = st.stats.origin
                depth = st.stats.depth

            m = TauPyModel('ak135')
            arrivals = m.get_travel_times(depth, y_dist, phase_list=markphases)
            timetable = [ [], [] ]
            for k, phase in enumerate(arrivals):
                phase_name = phase.name
                t = phase.time
                phase_time = origin + t - st.stats.starttime
                Phase_npt = int(phase_time/st.stats.delta)
                Phase = Phase_npt * st.stats.delta

                if Phase < t_axis.min() or Phase > t_axis.max():
                    continue
                else:
                    timetable[0].append(phase_name)
                    timetable[1].append(Phase)

            if not ylabel: ax.set_ylabel("Amplitude", fontsize=fs)
            title = st.stats.network+'.'+st.stats.station+'.'+st.stats.location+'.'+st.stats.channel
            ax.set_title(title, fontsize=fs)
            #plt.gca().yaxis.set_major_locator(plt.NullLocator())
            ax.plot(t_axis,zoom*data, color=clr)
            ax.plot( (timetable[1],timetable[1]),(-0.5,0.5), color=phaselabelclr )
            if phaselabel:
                for time, key in enumerate(timetable[0]):
                    ax.annotate('%s' % key, xy=(timetable[1][time]+5,0.55))

        else:
            if not ylabel: ax.set_ylabel("Amplitude", fontsize=fs)
            title = st.stats.network+'.'+st.stats.station+'.'+st.stats.location+'.'+st.stats.channel
            ax.set_title(title, fontsize=fs)
            ax.plot(t_axis, zoom*data, color=clr)



        if savefig:
            plt.savefig(savefig)
            plt.close("all")
        else:
            plt.ion()
            plt.draw()
            plt.show()
            plt.ioff()
Beispiel #58
0
def chanvese(I,
             init_mask,
             max_its=200,
             alpha=0.2,
             thresh=0,
             color='r',
             display=False):
    I = I.astype(np.float)

    # Create a signed distance map (SDF) from mask
    phi = mask2phi(init_mask)

    if display:
        plt.ion()
        fig, axes = plt.subplots(ncols=2)
        show_curve_and_phi(fig, I, phi, color)
        plt.savefig('levelset_start.png', bbox_inches='tight')

    # Main loop
    its = 0
    stop = False
    prev_mask = init_mask
    c = 0

    while (its < max_its and not stop):
        # Get the curve's narrow band
        idx = np.flatnonzero(np.logical_and(phi <= 1.2, phi >= -1.2))

        if len(idx) > 0:
            # Intermediate output
            if display:
                if np.mod(its, 50) == 0:
                    print('iteration: {0}'.format(its))
                    show_curve_and_phi(fig, I, phi, color)
            else:
                pass
                #if np.mod(its, 10) == 0:
                #print('iteration: {0}'.format(its))

            # Find interior and exterior mean
            upts = np.flatnonzero(phi <= 0)  # interior points
            vpts = np.flatnonzero(phi > 0)  # exterior points
            u = np.sum(I.flat[upts]) / (len(upts) + eps)  # interior mean
            v = np.sum(I.flat[vpts]) / (len(vpts) + eps)  # exterior mean

            # Force from image information
            #F = (I.flat[idx] - u)**2 - (I.flat[idx] - v)**2
            partial_seg = phi <= 0
            F = 2 * (I - partial_seg.astype(np.float64)).flat[idx]

            # Force from curvature penalty
            curvature = get_curvature(phi, idx)

            # Gradient descent to minimize energy
            dphidt = F / np.max(np.abs(F)) + alpha * curvature

            # Maintain the CFL condition
            dt = 0.45 / (np.max(np.abs(dphidt)) + eps)

            # Evolve the curve
            phi.flat[idx] += dt * dphidt

            # Keep SDF smooth
            phi = sussman(phi, 0.5)

            new_mask = phi <= 0
            c = convergence(prev_mask, new_mask, thresh, c)

            if c <= 5:
                its = its + 1
                prev_mask = new_mask
            else:
                stop = True

        else:
            break

    # Final output
    if display:
        show_curve_and_phi(fig, I, phi, color)
        plt.savefig('levelset_end.png', bbox_inches='tight')

    # Make mask from SDF
    seg = phi <= 0  # Get mask from levelset

    return seg, phi, its
Beispiel #59
0
def dcg_solver(A, b, mu,niter,x0=None):
    """
    Damped conjugate gradient solver for Ax = b lstsqs problems, as shown in Tomographic
    inversion via the conjugate gradient method, Scales, J. 1987
    Expect a mxn Matrix A, a rhs b and an optional startvalue x0

    minimizes the following problem by applying CGLS to:

                            || (   A  )  	  ( b )	||^{2}
                        min || (mu * I) *x 	- ( 0 )	||_{2}

                ==>		min || G * m - d || ^{2}_{2}

    """
    print("--- Using dCG-method --- \n \nInitiating matrices... \n \n")

    # Create G matrix and d Vector.

    I = mu**2. * sparse.identity(A.shape[0])

    G = sparse.lil_matrix(np.vstack((A.toarray(), I.toarray())))
    G = G.tocsc()

    d = np.hstack( (b, np.zeros(I.shape[0])) )


    # Initialize startvalues.
    try:
        if not x0:
            m = np.zeros(A.shape[1])
            s = -d
    except ValueError:
        if x0.any():
            m = x0
            s = G.dot(m) - d
    except:
        msg = 'No valid input'
        raise IOError(msg)


    beta_old 	= 0.
    r 			= G.transpose().conjugate().dot(s)
    p_old 		= np.zeros(r.shape)

    print("Starting iterations. \n \n")

    cont = True
    k = 1
    while cont:

        p 		= -r + beta_old * p_old
        alpha 	= np.linalg.norm(r,2)**2. /  p.dot(G.transpose().conjugate().toarray()).dot(G.dot(p))
        m_new 	= m + alpha * p
        s_new 	= s + alpha * G.dot(p)
        r_new	= G.transpose().conjugate().dot(s_new)
        beta	= np.linalg.norm(r_new,2)**2. / np.linalg.norm(r,2)**2.

        m			= m_new.copy()
        s			= s_new.copy()
        r			= r_new.copy()
        beta_old	= beta.copy()
        p_old		= p.copy()

        misfit 	= np.linalg.norm(G.dot(m) - d, 2)
        rnorm 	= np.linalg.norm(m,2)**2.

        plt.ion()
        plt.figure()
        plt.imshow(abs(m.reshape(20,300)),aspect='auto', interpolation='none')
        plt.show()

        if k == niter: cont=False
        k +=1


    x = m.copy()
    return x
Beispiel #60
0
            nn.Linear(128, 28 * 28),
            nn.Sigmoid(),
        )

    def forward(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return encoded, decoded


autoencoder = AutoEncoder()

optimizer = torch.optim.Adam(autoencoder.parameters(), lr=LR)
loss_func = nn.MSELoss()
f, a = plt.subplots(2, N_TEST_IMG, figsize=(5, 2))
plt.ion()  # continuously plot

# original data (first row) for viewing
view_data = train_data.train_data[:N_TEST_IMG].view(-1, 28 * 28).type(
    torch.FloatTensor) / 255.
for i in range(N_TEST_IMG):
    a[0][i].imshow(np.reshape(view_data.data.numpy()[i], (28, 28)),
                   cmap='gray')
    a[0][i].set_xticks(())
    a[0][i].set_yticks(())

for epoch in range(EPOCH):
    for step, (x, b_label) in enumerate(train_loader):
        b_x = x.view(-1, 28 * 28)  # batch x, shape (batch, 28*28)
        b_y = x.view(-1, 28 * 28)  # batch y, shape (batch, 28*28)