def write(self): self.writePreHook() for cut in self.contourData.filteredCombinedData: # Add cut to the output filename (outputFilename, ext) = os.path.splitext(self.outputFilename) outputFilename = "{0}_{1}_{2:.0f}{3}".format(outputFilename, cut.key, cut.value, ext) plt.ioff() plt.figure(figsize=(8,6)) #plt.yscale('log') plt.ylim([0,1.0]) plt.figtext(0.05, 0.96, 'Cut: {0} = {1:.0f}'.format(cut.key, cut.value), color='grey', size='small') plt.figtext(0.05, 0.93, 'Plot written at {:%d/%m/%Y %H:%M}'.format(datetime.datetime.now()), color='grey', size='small') for f in self.contourData.contributingRegionsFiltered[cut]: data = self.contourData.filteredData[f][cut] label = filterLabel(f, self.gridConfig) if not cut.isSimple(): plt.xticks( np.arange(len(data)), ["%d_%d" % (x[self.gridConfig.x], x[self.gridConfig.y]) for x in data.values()]) xLabel = "Grid point" else: var = self.gridConfig.x if cut.key == var: var = self.gridConfig.y xLabel = var plt.xticks( np.arange(len(data)), ["%d" % (x[var]) for x in data.values()]) print [x[self.contourData.combineOn] for x in data.values()] plt.plot( [x[self.contourData.combineOn] for x in data.values()], label=label ) plt.plot( [0.05 for x in data.values()], color='r', linestyle='--', linewidth=2)
def plot_hist(S,mu,sig,crit_t,year,d): if len(S)<=1: log.info(" plot_hist: 1 or less values in S for year "+str(year)+", skipping histogram ...") return S=S.reset_index(drop=True) x_low=S.min()-0.1*S.min() x_high=S.max()+0.1*S.max() x=np.linspace(x_low,x_high,100) if d["show_plots"]: plt.ion() else: plt.ioff() fig=plt.figure(figsize=(12,8)) #fig.patch.set_facecolor('white') plt.hist(S,normed=True) plt.plot(x,mlab.normpdf(x,mu,sig),color='red',linewidth=2.5,label='Gaussian PDF') plt.xlim(x_low,x_high) plt.xlabel(r'u* ($m\/s^{-1}$)',fontsize=16) plt.axvline(x=mu-sig*crit_t,color='black',linestyle='--') plt.axvline(x=mu+sig*crit_t,color='black',linestyle='--') plt.axvline(x=mu,color='black',linestyle='dotted') props = dict(boxstyle='round,pad=1', facecolor='white', alpha=0.5) txt='mean u*='+str(mu) ax=plt.gca() plt.text(0.4,0.1,txt,bbox=props,fontsize=12,verticalalignment='top',transform=ax.transAxes) plt.legend(loc='upper left') plt.title(str(year)+'\n') plot_out_name=os.path.join(d["plot_path"],d["site_name"]+'_CPD_'+str(year)+'.png') fig.savefig(plot_out_name) if d["show_plots"]: plt.draw() plt.ioff() else: plt.ion()
def _plot_histogram(self, data, number_of_devices=1, preamp_timeout=1253): if number_of_devices == 0: return data = np.array(data) plt.figure(3) plt.ioff() plt.get_current_fig_manager().window.wm_geometry("800x550+700+25") plt.clf() if number_of_devices == 1: plt.hist(data[0,:], bins=preamp_timeout, range=(1, preamp_timeout-1), color='b') elif number_of_devices == 2: plt.hist(data[0,:], bins=preamp_timeout, range=(1, preamp_timeout-1), color='r', label='JPM A') plt.hist(data[1,:], bins=preamp_timeout, range=(1, preamp_timeout-1), color='b', label='JPM B') plt.legend() elif number_of_devices > 2: raise Exception('Histogram plotting for more than two ' + 'devices is not implemented.') plt.xlabel('Timing Information [Preamp Time Counts]') plt.ylabel('Counts') plt.xlim(0, preamp_timeout) plt.draw() plt.pause(0.05)
def print_all(fnames, freq, spec1, spec2, rms1, rms2, bw=(0,1600)): ''' Print all power spectra to PDF files ''' # Construct path for saving figures and notify base_dir = os.path.join(os.getcwd(), 'figures') if not os.path.exists(base_dir): os.mkdir(base_dir) print('\nsaving figures to {s} ... '.format(s=base_dir), flush=True) # Plot and save figures for each channel's spectrum on = plt.isinteractive() plt.ioff() for chan in range(spec1.shape[-1]): fig = comp_spectra(freq, spec1, spec2, channel=chan, bw=bw) plt.title('Channel {0:d}'.format(chan)) plt.xlabel('Frequency (Hz)') plt.ylabel('Power') plt.legend(('Before', 'After')) print('figure {:02d}'.format(chan), flush=True) plt.savefig(os.path.join(base_dir, 'channel{0:02d}.png'.format(chan)), format='png') plt.close(fig) # Plot and save figure showing RMS ratio fig = plt.figure() plt.plot(rms2 / rms1, 'o') plt.title('RMS ratio (after / before)') plt.xlabel('Channel') plt.savefig(os.path.join(base_dir, 'rms_ratio.png'), format='png') plt.close(fig) # Notify plt.interactive(on) print('done.')
def main(args): # Load parameters from yaml param_path = 'params.yaml' # rospy.get_param("~param_path") f = open(param_path,'r') params_raw = f.read() f.close() params = yaml.load(params_raw) occupancy_map = np.array(params['occupancy_map']) world_map = np.array(params['world_map']) pos_init = np.array(params['pos_init']) pos_goal = np.array(params['pos_goal']) max_vel = params['max_vel'] max_omega = params['max_omega'] t_cam_to_body = np.array(params['t_cam_to_body']) x_spacing = params['x_spacing'] y_spacing = params['y_spacing'] # Intialize the RobotControl object robotControl = RobotControl(world_map, occupancy_map, pos_init, pos_goal, max_vel, max_omega, x_spacing, y_spacing, t_cam_to_body) # TODO for student: Comment this when running on the robot # Run the simulation while not robotControl.robot_sim.done and plt.get_fignums(): robotControl.process_measurements() robotControl.robot_sim.update_frame() plt.ioff() plt.show() # TODO for student: Use this to run the interface on the robot # Call process_measurements at 60Hz """r = rospy.Rate(60)
def oystek_start(fname): global num_oysters plt.ioff() # interactive # initialize oystek_reset(fname) while (True): print '\nfetching oyster ', num_oysters+1, ' ...' if (skip_to_image() != True): break if (fetch_1_oyster() != True): break num_oysters += 1 prepare_scatter_images() log_to_file() print_measurement() #plot_oyster_images() plot_oyster_images_AiO() plot_oyster_3Dimages() # clean up before exit fin.close() fout.close() print '\ntotal # oysters: ', num_oysters return
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()
def end(self): """End plot""" # Make sure that the window does not close before we wan't it to plt.ioff() plt.show()
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
def wrapper(*args, **kwargs): try: plt.ion() return func(*args, **kwargs) finally: plt.close("all") plt.ioff()
def run (self, gamma, ELA, dt, dx): for t in range(len(self.timestep)): self.dqdx=glacier_model.dqdx_func(dx) self.dhdt=glacier_model.b_func(gamma, ELA)-self.dqdx self.h+=self.dhdt*dt for i in range(0, len(self.h)): #make sure bottom limit of z does not go below zb if self.h[i]<0: self.h[i]=0 self.z=self.h+self.zb if self.timestep[t] % 10 ==0: self.figure.clear() plt.title('Glacier Accumulation & Ablation Over Time') plt.xlabel('Distance (m)') plt.ylabel('Elevation (m)') self.figure.set_ylim(2000, 4000) #make sure y axis doesn't change self.figure.set_xlim(0, 15000) #make sure x axis doesn't change plt.text(12000, 3500, 'Time [yrs]: %d\nELA=%d m' % (self.timestep[t], ELA)) self.figure.plot(self.spacestep, self.z, label='Glacier Height', color='b') self.figure.plot(self.spacestep, self.zb, label='Bedrock Height', color='k') self.figure.plot(self.spacestep, (np.zeros(len(self.spacestep))+ELA), '--r', label='ELA') self.figure.fill_between(self.spacestep, self.zb, self.z, color ='b', interpolate=True) self.figure.fill_between(self.spacestep, 0, self.zb, color='k', interpolate=True) self.figure.legend() plt.pause(0.00001) plt.ioff()
def plot_samples(self): """ Plot the samples requested for each arm """ plt.clf() plt.scatter(range(0, self.K), self.sample_size) plt.yscale('log') plt.ioff() plt.show()
def hovmoellerPlot(varlist, clevs=None,cbls=None,title='',subplot=(),slices={},figargs={'figsize':(8,8)},**plotargs): import matplotlib.pylab as pyl from pygeode.axis import XAxis, YAxis, TAxis from plotting.misc import multiPlot, sharedColorbar if not isinstance(varlist,list): varlist = [varlist] if not isinstance(clevs,list): clevs = [clevs] if not isinstance(cbls,list): cbls = [cbls] pyl.ioff() # non-interactive mode # construct figure and axes f = pyl.figure(**figargs) f.clf() # create zonal-mean variables titles = [var.name for var in varlist] plotlist = [var(**slices).mean(XAxis).transpose(YAxis,TAxis) for var in varlist] # latitude sliced # organize colorbar (cleanup arguments) colorbar = plotargs.pop('colorbar',{}) manualCbar = colorbar.pop('manual',False) if manualCbar: cbar = False else: cbar = colorbar # set default margins defaultMargins = {'left':0.065,'right':0.975,'bottom':0.05,'top':0.95,'wspace':0.05,'hspace':0.1} defaultMargins.update(plotargs.pop('margins',{})) ## make subplots (f,cf,subplot) = multiPlot(f=f,varlist=plotlist,titles=titles,clevs=clevs,cbls=cbls,subplot=subplot, # colorbar=cbar,margins=defaultMargins,**plotargs) if title: f.suptitle(title,fontsize=14) ## add common colorbar if manualCbar: f = sharedColorbar(f, cf, clevs, colorbar, cbls, subplot, defaultMargins) # finalize pyl.draw(); # pyl.ion(); return f
def storeHistogram(whitened, codebook, data_name, save_loc): plt.ioff() plt.clf() plt.hist(whitened, label=data_name) plt.hist(codebook, label="clusters") plt.legend() plt.savefig(save_loc)
def generate_image(self, filepath, smoothness=7, size=400): x = self.average_hist[:, 1] y = self.average_hist[:, 0] x -= 50 y = np.concatenate((y[-50:], y[:-50])) #The following is for plotting xticks at proper peak locations locs = np.arange(0, 1200, 100) labels = ["Sa", "Ri1", "Ri2/Ga1", "Ri3/Ga2", "Ga3", "Ma1", "Ma2", "Pa", "Da1", "Da2/Ni1", "Da3/Ni2", "Ni3"] label_map = {} for i in xrange(len(locs)): label_map[locs[i]] = labels[i] dobj = pypeaks.Data(x, y, smoothness=7) dobj.get_peaks(peak_amp_thresh=6e-04) actual_locs = np.sort(dobj.peaks["peaks"][0]) for i in xrange(len(actual_locs)): actual_locs[i] = locs[self.find_nearest_index(locs, actual_locs[i])] actual_labels = [label_map[i] for i in actual_locs] y = gaussian_filter(y, smoothness) plt.ioff() fig = plt.figure() fig.set_size_inches(2, 2) fig.set_dpi(300) plt.plot(x, y, "k-") plt.xlim(-50, 1150) plt.xticks(actual_locs, actual_labels, fontsize=6) plt.yticks([]) plt.savefig(filepath, bbox_inches="tight") plt.close(fig)
def rotate_window(maze, pt, yaw, window_size=(64, 64)): wall, route = np.max(maze), 0 h_maze, w_maze = maze.shape # x, y = pt h_slide, w_slide = window_size # expected rect top, bottom, left, right = y - h_slide // 2, y + h_slide // 2, x - w_slide // 2, x + w_slide // 2 # valid rect v_top, v_bottom, v_left, v_right = max(top, 0), min(bottom, h_maze), max(left, 0), min(right, w_maze) # generate slide window sw = np.ones([h_slide, w_slide], dtype=np.float32) * wall sw[v_top - top:h_slide - bottom + v_bottom, v_left - left:w_slide - right + v_right] = \ maze[v_top:v_bottom,v_left:v_right] # rotation rr, cc = skimage.draw.circle(31, 31, 32) # circle = np.zeros_like(sw, dtype=np.bool) # circle[rr, cc] = True # circle = np.bitwise_not(circle) # sw = np.multiply(sw, circle) rw = np.ones_like(sw) rw[rr, cc] = sw[rr, cc] rw = skimage.transform.rotate(rw, yaw) # plt.ioff() plt.imshow(rw, cmap='Greys') plt.draw() plt.pause(0.1)
def get_isobath(lon, lat, topo, iso): """ USAGE ----- lon_isob, lat_isob = get_isobath(lon_topo, lat_topo, topo, iso) Retrieves the 'lon_isob','lat_isob' coordinates of a wanted 'iso' isobath from a topography array 'topo', with 'lon_topo','lat_topo' coordinates. """ lon, lat, topo = map(np.asanyarray, (lon, lat, topo)) plt.ioff() fig, ax = plt.subplots() cs = ax.contour(lon, lat, topo, [iso]) coll = cs.collections[0] ## Test all lines to find thel ongest one. ## This is assumed to be the wanted isobath. ncoll = len(coll.get_paths()) siz = np.array([]) for n in xrange(ncoll): path = coll.get_paths()[n] siz = np.append(siz, path.vertices.shape[0]) f = siz.argmax() xiso = coll.get_paths()[f].vertices[:, 0] yiso = coll.get_paths()[f].vertices[:, 1] del(fig, ax, cs) plt.ion() return xiso, yiso
def gearth_fig(llcrnrlon, llcrnrlat, urcrnrlon, urcrnrlat, pixels=1024): """ Return a Matplotlib `fig` and `ax` handles for a Google-Earth Image. TJL - Obtained from http://ocefpaf.github.io/python4oceanographers/blog/2014/03/10/gearth/ """ aspect = np.cos(np.mean([llcrnrlat, urcrnrlat]) * np.pi/180.0) xsize = np.ptp([urcrnrlon, llcrnrlon]) * aspect ysize = np.ptp([urcrnrlat, llcrnrlat]) aspect = ysize / xsize if aspect > 1.0: figsize = (10.0 / aspect, 10.0) else: figsize = (10.0, 10.0 * aspect) if False: plt.ioff() # Make `True` to prevent the KML components from popping-up. fig = plt.figure(figsize=figsize, frameon=False, dpi=pixels//10) # KML friendly image. If using basemap try: `fix_aspect=False`. ax = fig.add_axes([0, 0, 1, 1]) ax.set_xlim(llcrnrlon, urcrnrlon) ax.set_ylim(llcrnrlat, urcrnrlat) return fig, ax
def go(self, start, end): start = (start / self.batchSize) * self.batchSize end = (end / self.batchSize + 1) * self.batchSize cur = 0 # skip to the start for _ in range(start / self.batchSize): cur += self.batchSize self.parser.getNextBatch() for _ in range((end - start) / self.batchSize): dens, _, _ = self.parser.getNextBatch() r, theta = np.meshgrid(self.params['radialIntervals'], self.params['thetaIntervals']) plt.ioff() #-- Plot... ------------------------------------------------ for i in range(len(dens)): fig = plt.figure() ax = plt.subplot(111, polar=True) ax.contourf(theta, r, np.log(dens[i]).transpose(), cmap=plt.cm.afmhot) ax.scatter([self.secondaryTheta[cur]], [self.secondaryRadius[cur]], s=150) ax.set_rmax(1.5) #ax.set_title(r"$\theta_{sec}=" + "{0:.2f}$ rad".format(self.secondaryTheta[cur] % 6.283), va='bottom') plt.savefig(self.outputDir + "/figs/dens" + str(cur) + ".png") plt.close(fig) cur += 1
def bb_map(lons, lats, projection='merc', resolution='i', drawparallels=True, drawmeridians=True, ax=plt.gca()): """ USAGE ----- m = bb_map(lons, lats, **kwargs) Returns a Basemap instance with lon,lat bounding limits inferred from the input arrays `lons`,`lats`. Coastlines, countries, states, parallels and meridians are drawn, and continents are filled. """ lons,lats = map(np.asanyarray, (lons,lats)) lonmin,lonmax = lons.min(),lons.max() latmin,latmax = lats.min(),lats.max() m = Basemap(llcrnrlon=lonmin, urcrnrlon=lonmax, llcrnrlat=latmin, urcrnrlat=latmax, projection=projection, resolution=resolution, ax=ax) plt.ioff() # Avoid showing the figure. m.fillcontinents(color='0.9', zorder=9) m.drawcoastlines(zorder=10) m.drawstates(zorder=10) m.drawcountries(linewidth=2.0, zorder=10) m.drawmapboundary(zorder=9999) if drawmeridians: m.drawmeridians(np.arange(np.floor(lonmin), np.ceil(lonmax), 1), linewidth=0.15, labels=[1, 0, 1, 0], zorder=12) if drawparallels: m.drawparallels(np.arange(np.floor(latmin), np.ceil(latmax), 1), linewidth=0.15, labels=[1, 0, 0, 0], zorder=12) plt.ion() return m
def q_test(weight): plt.ion() time_step = 1 # Discrete approximation time step for the continuous game exploration_rate = 0.1 # Exploration rate discount_factor = 0.9 # Discounted factor test_iter = 5 # Number of training iterations for i in range(test_iter): # x, y, angle = -4, 0, 0 # Robot state x, y, angle = random_state() cumulative_reward, discount = 0, 1 while True: show_state(x, y, angle) feature = state_to_feature(x, y, angle) q_value = get_q_value(feature, weight) max_q_value, max_action = get_max_q_action(q_value) action = explore_exploit(max_action, exploration_rate) velocity, angle_speed = action_to_physics(action) x, y, angle = update_state(x, y, angle, velocity, angle_speed, time_step) if state_is_end(x, y, angle): reward = -10 else: reward = get_reward(x, y, angle, velocity, time_step) cumulative_reward += discount * reward discount *= discount_factor if state_is_end(x, y, angle): break logging.info('iteration: %d, exploration rate: %.4f, cumulative reward: %.4f', i, exploration_rate, cumulative_reward) plt.ioff() plt.show()
def show_q_value(weight): plt.ion() time_step = 1 discount_factor = 0.9 # x, y, angle = -4, 0, 0 # Robot state x, y, angle = random_state() cumulative_reward, discount = 0, 1 while True: show_state(x, y, angle) feature = state_to_feature(x, y, angle) q_value = get_q_value(feature, weight) logging.info('q_value: [%.4f, %.4f]', q_value[0], q_value[1]) input("Press Enter to continue...\n") max_q_value, max_action = get_max_q_action(q_value) action = explore_exploit(max_action, 0.1) velocity, angle_speed = action_to_physics(action) x, y, angle = update_state(x, y, angle, velocity, angle_speed, time_step) if state_is_end(x, y, angle): reward = -10 else: reward = get_reward(x, y, angle, velocity, time_step) cumulative_reward += discount * reward discount *= discount_factor if state_is_end(x, y, angle): break logging.info('cumulative reward: %.4f', cumulative_reward) plt.ioff() plt.show()
def Main(): args=ParseArg() data=np.loadtxt(args.input,delimiter='\t',dtype=float) min_x=int(args.xlim[0]) max_x=int(args.xlim[1]) start=int(data[0,0]) peak=data[:,1].argmax() plt.ioff() plt.plot(np.array(range(min_x,max_x)),data[(min_x-start):(max_x-start),1],color='r',label="real_count") if args.distogram: plt.annotate('local max: '+str(peak+start)+"bp",xy=(peak+start,data[peak,1]),xytext=(peak+start+30,0.8*data[peak,1]),) # arrowprops=dict(facecolor='black', shrink=0.05)) # smoth the plot xnew=np.linspace(min_x,max_x,(max_x-min_x)/5) smooth=spline(np.array(range(min_x,max_x)),data[(min_x-start):(max_x-start),1],xnew) plt.plot(xnew,smooth,color='g',label='smooth(5bp)') max_y=max(data[(min_x-start):(max_x-start),1]) min_y=min(data[(min_x-start):(max_x-start),1]) plt.xlabel("Distance") plt.ylabel("Counts") plt.xlim(min_x,max_x) plt.ylim(min_y*0.9,max_y*1.1) plt.title(os.path.basename(args.input).split("_"+str(start))[0]) plt.legend() plt.savefig(os.path.basename(args.input).split("_"+str(start))[0]+"_%d~%dbp."%(min_x,max_x)+args.output) print >>sys.stderr,"output figure file generated!!"
def open_data(env=makeEnviron3(), filename="out.dat"): r = Robot_Sim(None, None, None, None, None) f = open(filename, "r") plt.ion() plt.show() for line in f: if len(line) < 2: break try: parse = line.split(";") r.x = pose_from_str(parse[0]) r.Z = measurement_from_str(parse[1]) # print(r.x) # print(r.Z) P = [part_from_str(p) for p in parse[2:]] except: break # print([str(p) for p in P]) plt.clf() env.plot(plt) r.plot(plt) for p in P: p.plot(plt, p_rgb) plt.draw() f.close() plt.ioff() plt.show()
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
def testdriver2(): env = makeEnviron2() P = [(0.5, 1.5), (2.5, 1.5), (2.5, 0.5), (3.5, 0.5), (3.5, 3.5), (1.5, 3.5), (1.5, 1.5), (0.5, 1.5)] plt.ion() env.plot(plt) plt.draw() mot = makeMotion1() odom = Robot_Odometry_Model() meas = Robot_Measurement_Model(measure_count=4, fov=pi / 8) start_pose = Pose(0.5, 1.5, 0) r = Robot_Sim(env, mot, odom, meas, start_pose) # print(r) driver = Robot_Driver(r, P, v_max=5, w_max=6) while not driver.finished: u = driver.next_control() # print(u) r.tick(u, driver.dt) r.plot(plt) plt.draw() # print(driver.count) plt.ioff() plt.show()
def testmotion2(u=(5, 3)): env = Environment([Rect(-10, -10, 20, 20)]) # for reference env.plot(plt) mot = makeMotion1() odom = Robot_Odometry_Model() meas = Robot_Measurement_Model(measure_count=0, fov=pi / 8) start_pose = Pose(0.5, 1.5, 0.5) p1 = Particle(env, mot, meas, start_pose) p1.plot(plt) P0 = [] for i in range(10): p_temp = p1.sample_mov(u, 0.2) p_temp.plot(plt) P0.append(p_temp) P = [P0] for i in range(10): P_temp = [] for p in P[i]: p_temp = p.sample_mov(u, 0.1) p_temp.plot(plt) P_temp.append(p_temp) P.append(P_temp) plt.ioff() plt.show()
def get_legend_size(self): """ Determine the size of the legend by building a dummy figure and extracting its properties """ if len(self.leg_items) > 0 and self.leg_on: now = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S') mplp.ioff() fig = mpl.pyplot.figure(dpi=self.dpi) ax = fig.add_subplot(111) lines = [] for i in self.leg_items: lines += ax.plot([1,2,3]) leg = mpl.pyplot.legend(lines, list(self.leg_items), title=self.leg_title, numpoints=self.leg_points, prop={'size':self.leg_font_size}) fig.canvas.draw() mpl.pyplot.savefig('dummy_legend_%s.png' % now) self.leg_h = leg.get_window_extent().height + self.leg_border self.leg_w = leg.get_window_extent().width + self.leg_border mpl.pyplot.close(fig) os.remove('dummy_legend_%s.png' % now) self.leg_w = max(self.leg_w, self.ax_leg_fig_ws)
def VerifyCurrent(Status): dirdata = "CompressedData/" AllVectors = {} if os.path.exists(dirdata) is False: return False files = os.listdir(dirdata) # Begin loading the data x is like "132-1" for x in files: pid, state = x.split('-') pid = int(pid) state = int(state) try: fp = open("%s%s"%(dirdata, x), 'r') except: print 'Open file error' sf = cPickle.load(fp) AllVectors[(pid,state)] = sf[115,:].tolist() fp.close() vector, SelectedVectors =GetCurrent(Status,AllVectors) PCAclass = calib.PCA.PCAClass() DeVector = PCAclass.PCA(np.matrix(vector).transpose(), Status) plt.ioff() dims = len(AllVectors[(0, 1)]) for pid in SelectedVectors: fig=plt.figure() plt.bar(np.arange(0,dims,1),DeVector[pid],0.3) plt.bar(np.arange(0.3,dims+0.3,1),SelectedVectors[pid],0.3,color='r') plt.show()
def q_explore_simulation(): plt.ion() time_step = 1 # Discrete approximation time step for the continuous game exploration_rate = 0.5 # Exploration rate discount_factor = 0.9 # Discounted factor for iteration in range(5): logging.info('starting iteration %d', iteration) x, y, angle = -4, 0, 0 # Robot state weight = initialize_param(3 + 1, 2) cumulative_reward, discount = 0, 1 for i in range(10): show_state(x, y, angle) feature = state_to_feature(x, y, angle) q_value = get_q_value(feature, weight) max_q_value, max_action = get_max_q_action(q_value) action = explore_exploit(max_action, exploration_rate) velocity, angle_speed = action_to_physics(action) x, y, angle = update_state(x, y, angle, velocity, angle_speed, time_step) reward = get_reward(x, y, angle, velocity, time_step) cumulative_reward += discount * reward discount *= discount_factor logging.info('cumulative reward: %.4f', cumulative_reward) if state_is_end(x, y, angle): break plt.ioff() plt.show()
def process(input_dir, output_dir, overwrite=False): items = 0 created = 0 found = 0 date_mult = {"08_08": 2, "08_11": 2, "08_14": 2} plt.ioff() for date in [x for x in os.listdir(input_dir) if re.match(pattern, x)]: multiplier = 1 if date in date_mult: multiplier = date_mult[date] date_path = os.path.join(input_dir, date) o_date_path = os.path.join(output_dir, date) make_dir(o_date_path) for label in [d for d in os.listdir(date_path) if d in VALID_LABELS]: label_path = os.path.join(date_path, label) o_label_path = os.path.join(o_date_path, label) make_dir(o_label_path) print("\tProcessing: {}".format(label_path)) print("\tTime: {}".format(curr_time())) for channel in [ d for d in os.listdir(label_path) if d.startswith("ch") ]: voice = False ch = int(channel[2:]) if ch == 4 or ch >= 9: voice = True channel_path = os.path.join(label_path, channel) o_channel_path = os.path.join(o_label_path, channel) make_dir(o_channel_path) channel_num = channel[-1] for file in [ f for f in os.listdir(channel_path) if f.endswith(".wav") ]: items += 1 wavpath = os.path.join(channel_path, file) imgpath = os.path.join(o_channel_path, file[:-4] + IMG_EXT) if overwrite or not os.path.isfile(imgpath): created += 1 if items % VERBOSITY == 0: print("\t\tCreated {}th image".format(items)) sample_rate, samples = wavfile.read(wavpath) samples = preprocess(samples, sample_rate, multiplier) # freqs, times, spectrogram = signal.spectrogram(samples, sample_rate) if voice: S = librosa.feature.melspectrogram(samples, sr=sample_rate, n_mels=128) else: S = librosa.feature.melspectrogram(samples, sr=sample_rate, n_mels=128, fmax=512) log_S = librosa.power_to_db(S, ref=np.max) fig = plt.figure(figsize=(1.28, 1.28), dpi=100, frameon=False) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) plt.axis('off') librosa.display.specshow(log_S) plt.savefig(imgpath) plt.close() else: found += 1 if items % VERBOSITY == 0: print("\t\tFound {}th image".format(items)) print("\tFound:\t\t{}\n\tCreated:\t{}".format(found, created)) plt.ion()
def train(sess, env, actor, critic, actor_noise, buffer_size, min_batch, ep): sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(max_to_keep=5) # Initialize target network weights actor.update_target_network() critic.update_target_network() # Initialize replay memory replay_buffer = ReplayBuffer(buffer_size, 0) max_episodes = ep max_steps = 1000 score_list = [] plt.ion() for i in range(max_episodes): state = env.reset() score = 0 for j in range(max_steps): action = actor.predict(np.reshape(state, (1, actor.s_dim))) + actor_noise() next_state, reward, done, info = env.step(action[0]) replay_buffer.add( np.reshape(state, (actor.s_dim,)), np.reshape(action, (actor.a_dim,)), reward, done, np.reshape(next_state, (actor.s_dim,)), ) # updating the network in batch if replay_buffer.size() < min_batch: continue states, actions, rewards, dones, next_states = replay_buffer.sample_batch( min_batch ) target_q = critic.predict_target( next_states, actor.predict_target(next_states) ) y = [] for k in range(min_batch): y.append(rewards[k] + critic.gamma * target_q[k] * (1 - dones[k])) # Update the critic given the targets predicted_q_value, _ = critic.train( states, actions, np.reshape(y, (min_batch, 1)) ) # Update the actor policy using the sampled gradient a_outs = actor.predict(states) grads = critic.action_gradients(states, a_outs) actor.train(states, grads[0]) # Update target networks actor.update_target_network() critic.update_target_network() state = next_state score += reward env.render() if done: print("Reward: {} | Episode: {}/{}".format(int(score), i, max_episodes)) break score_list.append(score) plt.cla() plt.plot(score_list) plt.pause(0.0001) avg = np.mean(score_list[-200:]) print("Average of last 200 episodes: {0:.2f} \n".format(avg)) if avg > 200: print("Task Completed") print("The last episode ran for {} time steps!".format((j + 1))) save_model( saver, sess, fname="model_checkpoints/lunarLander", steps=i, write_meta_graph=True, ) break if i % 10 == 0: save_model(saver, sess, fname="model_checkpoints/lunarLander", steps=i) plt.ioff() plt.show() plt.savefig("lunarLander-ddpg.png") return score_list
for vp in violin_parts['bodies']: vp.set_edgecolor('#3B1255') vp.set_facecolor('#9675AB') vp.set_linewidth(1.0) vp.set_alpha(1.0) plt.xticks(range(len(y_data)), dimensions) plt.ylim(ylims[problem_str]) # plt.yscale('log') # plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0)) plt.ylabel(r'Fitness, $\log(f(x) + 1)$') plt.xlabel(r'Dimensions') plt.title(r'' + 'Cardinality: ' + cardinality, loc='center') plt.ioff() if cardinality == '1': plt.legend([Line2D([0], [0], color='#AC4C3D', lw=3), Line2D([0], [0], color='#285C6B', lw=3)], ['Mean', 'Median'], frameon=False, loc="upper left", borderaxespad=0, ncol=1) # bbox_to_anchor=(0, 0.9, 1, 0.2), file_name = '{}'.format(problem_str) plt.tight_layout() if is_saving: plt.savefig(folder_name + 'vp' + file_name + '.eps', format='eps', dpi=1000) print(file_name + ' Saved!') plt.show() # %% PLOT FITNESS PER STEP
def analyze_wobble(config): """ Extracts the theta2 plot of a dataset taken with wobble observations Parameters ---------- config_file """ fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 8)) n_points = config['analysis']['parameters']['n_points'] theta2_cut = config['analysis']['selection']['theta2'][0] LOGGER.info("Running wobble analysis with %s off-source observation points", n_points) LOGGER.info("Analyzing runs %s", config['analysis']['runs']) observation_time, data = merge_dl2_runs(config['input']['data_tag'], config['analysis']['runs'], config['input']['columns_to_read']) LOGGER.debug('\nPreselection:\n%s',config['preselection']) for key, value in config['preselection'].items(): LOGGER.debug('\nParameter: %s, range: %s, value type: %s', key, value, type(value)) selected_data = filter_events(data, config['preselection']) # Add theta2 to selected data true_source_position = extract_source_position(selected_data, config['input']['observed_source']) plotting.plot_wobble(true_source_position, n_points, ax1) named_datasets = [] named_datasets.append(('ON data', np.array(compute_theta2(selected_data, true_source_position)), 1)) n_on = np.sum(named_datasets[0][1] < theta2_cut) n_off = 0 rotation_angle = 360./n_points origin_x = selected_data['reco_src_x'] origin_y = selected_data['reco_src_y'] for off_point in range(1, n_points): t_off_data = selected_data.copy() off_xy = rotate(tuple(zip(origin_x, origin_y)), rotation_angle * off_point) t_off_data['reco_src_x'] = [xy[0] for xy in off_xy] t_off_data['reco_src_y'] = [xy[1] for xy in off_xy] named_datasets.append((f'OFF {rotation_angle * off_point}', np.array(compute_theta2(t_off_data, true_source_position)), 1)) n_off += np.sum(named_datasets[-1][1] < theta2_cut) stat = WStatCountsStatistic(n_on, n_off, 1./(n_points - 1)) # API change for attributes significance and excess in the new gammapy version: https://docs.gammapy.org/dev/api/gammapy.stats.WStatCountsStatistic.html lima_significance = stat.sqrt_ts.item() lima_excess = stat.n_sig LOGGER.info('Observation time %s', observation_time) LOGGER.info('Number of "ON" events %s', n_on) LOGGER.info('Number of "OFF" events %s', n_off) LOGGER.info('ON/OFF observation time ratio %s', 1./(n_points - 1)) LOGGER.info('Excess is %s', lima_excess) LOGGER.info('Li&Ma significance %s', lima_significance) plotting.plot_1d_excess(named_datasets, lima_significance, r'$\theta^2$ [deg$^2$]', theta2_cut, ax2) if config['output']['interactive'] is True: LOGGER.info('Interactive mode ON, plots will be only shown, but not saved') plt.show() else: LOGGER.info('Interactive mode OFF, no plots will be displayed') plt.ioff() plt.savefig(f"{config['output']['directory']}/wobble.png") plt.close()
def analyze_on_off(config): """ Extracts the theta2 plot of a dataset taken with ON/OFF observations Parameters ---------- config_file """ fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 8)) LOGGER.info("Running ON/OFF analysis") LOGGER.info("ON data runs: %s", config['analysis']['runs_on']) observation_time_on, data_on = merge_dl2_runs(config['input']['data_tag'], config['analysis']['runs_on'], config['input']['columns_to_read'], 4) LOGGER.info("ON observation time: %s", observation_time_on) LOGGER.info("OFF data runs: %s", config['analysis']['runs_off']) observation_time_off, data_off = merge_dl2_runs(config['input']['data_tag'], config['analysis']['runs_off'], config['input']['columns_to_read'], 4) LOGGER.info("OFF observation time: %s", observation_time_off) #observation_time_ratio = observation_time_on / observation_time_off #LOGGER.info('Observation time ratio %s', observation_time_ratio) selected_data_on = filter_events(data_on, config['preselection']) selected_data_off = filter_events(data_off, config['preselection']) theta2_on = np.array(compute_theta2(selected_data_on, (0, 0))) theta2_off = np.array(compute_theta2(selected_data_off, (0, 0))) theta2_cut = config['analysis']['selection']['theta2'][0] n_on = np.sum(theta2_on < theta2_cut) n_off = np.sum(theta2_off < theta2_cut) LOGGER.info('Number of observed ON and OFF events are:\n %s, %s', n_on, n_off) theta2_norm_min = config['analysis']['selection']['theta2'][1] theta2_norm_max = config['analysis']['selection']['theta2'][2] n_norm_on = np.sum((theta2_on > theta2_norm_min) & (theta2_on < theta2_norm_max)) n_norm_off = np.sum((theta2_off > theta2_norm_min) & (theta2_off < theta2_norm_max)) lima_norm = n_norm_on / n_norm_off stat = WStatCountsStatistic(n_on, n_off, lima_norm) lima_significance = stat.sqrt_ts.item() lima_excess = stat.n_sig LOGGER.info('Excess is %s', lima_excess) LOGGER.info('Excess significance is %s', lima_significance) plotting.plot_1d_excess([('ON data', theta2_on, 1), (f'OFF data X {lima_norm:.2f}', theta2_off, lima_norm)], lima_significance, r'$\theta^2$ [deg$^2$]', theta2_cut, ax1) # alpha analysis LOGGER.info('Perform alpha analysis') alpha_on = np.array(compute_alpha(selected_data_on)) alpha_off = np.array(compute_alpha(selected_data_off)) alpha_cut = config['analysis']['selection']['alpha'][0] n_on = np.sum(alpha_on < alpha_cut) n_off = np.sum(alpha_off < alpha_cut) LOGGER.info('Number of observed ON and OFFevents are:\n %s, %s', n_on, n_off) alpha_norm_min = config['analysis']['selection']['alpha'][1] alpha_norm_max = config['analysis']['selection']['alpha'][2] n_norm_on = np.sum((alpha_on > alpha_norm_min) & (alpha_on < alpha_norm_max)) n_norm_off = np.sum((alpha_off > alpha_norm_min) & (alpha_off < alpha_norm_max)) lima_norm = n_norm_on / n_norm_off stat = WStatCountsStatistic(n_on, n_off, lima_norm) lima_significance = stat.sqrt_ts.item() lima_excess = stat.n_sig LOGGER.info('Excess is %s', lima_excess) LOGGER.info('Excess significance is %s', lima_significance) plotting.plot_1d_excess([('ON data', alpha_on, 1), (f'OFF data X {lima_norm:.2f}', alpha_off, lima_norm)], lima_significance, r'$\alpha$ [deg]', alpha_cut, ax2, 0, 90, 90) if config['output']['interactive'] is True: LOGGER.info('Interactive mode ON, plots will be only shown, but not saved') plt.show() else: LOGGER.info('Interactive mode OFF, no plots will be displayed') plt.ioff() plt.savefig(f"{config['output']['directory']}/on_off.png") plt.close()
def run_crophealth_app(ds, lat, lon, buffer): """ Plots an interactive map of the crop health case-study area and allows the user to draw polygons. This returns a plot of the average NDVI value in the polygon area. Last modified: January 2020 Parameters ---------- ds: xarray.Dataset data set containing combined, masked data Masked values are set to 'nan' lat: float The central latitude corresponding to the area of loaded ds lon: float The central longitude corresponding to the area of loaded ds buffer: The number of square degrees to load around the central latitude and longitude. For reasonable loading times, set this as `0.1` or lower. """ # Suppress warnings warnings.filterwarnings('ignore') # Update plotting functionality through rcParams mpl.rcParams.update({'figure.autolayout': True}) # Define polygon bounds latitude = (lat - buffer, lat + buffer) longitude = (lon - buffer, lon + buffer) # Define the bounding box that will be overlayed on the interactive map # The bounds are hard-coded to match those from the loaded data geom_obj = { "type": "Feature", "properties": { "style": { "stroke": True, "color": 'red', "weight": 4, "opacity": 0.8, "fill": True, "fillColor": False, "fillOpacity": 0, "showArea": True, "clickable": True } }, "geometry": { "type": "Polygon", "coordinates": [ [ [ longitude[0], latitude[0] ], [ longitude[1], latitude[0] ], [ longitude[1], latitude[1] ], [ longitude[0], latitude[1] ], [ longitude[0], latitude[0] ] ] ] } } # Create a map geometry from the geom_obj dictionary # center specifies where the background map view should focus on # zoom specifies how zoomed in the background map should be loadeddata_geometry = ogr.CreateGeometryFromJson(str(geom_obj['geometry'])) loadeddata_center = [ loadeddata_geometry.Centroid().GetY(), loadeddata_geometry.Centroid().GetX() ] loadeddata_zoom = 16 # define the study area map studyarea_map = Map( center=loadeddata_center, zoom=loadeddata_zoom, basemap=basemaps.Esri.WorldImagery ) # define the drawing controls studyarea_drawctrl = DrawControl( polygon={"shapeOptions": {"fillOpacity": 0}}, marker={}, circle={}, circlemarker={}, polyline={}, ) # add drawing controls and data bound geometry to the map studyarea_map.add_control(studyarea_drawctrl) studyarea_map.add_layer(GeoJSON(data=geom_obj)) # Index to count drawn polygons polygon_number = 0 # Define widgets to interact with instruction = widgets.Output(layout={'border': '1px solid black'}) with instruction: print("Draw a polygon within the red box to view a plot of " "average NDVI over time in that area.") info = widgets.Output(layout={'border': '1px solid black'}) with info: print("Plot status:") fig_display = widgets.Output(layout=widgets.Layout( width="50%", # proportion of horizontal space taken by plot )) with fig_display: plt.ioff() fig, ax = plt.subplots(figsize=(8, 6)) ax.set_ylim([0, 1]) colour_list = plt.rcParams['axes.prop_cycle'].by_key()['color'] # Function to execute each time something is drawn on the map def handle_draw(self, action, geo_json): nonlocal polygon_number # Execute behaviour based on what the user draws if geo_json['geometry']['type'] == 'Polygon': info.clear_output(wait=True) # wait=True reduces flicker effect # Save geojson polygon to io temporary file to be rasterized later jsonData = json.dumps(geo_json) binaryData = jsonData.encode() io = BytesIO(binaryData) io.seek(0) # Read the polygon as a geopandas dataframe gdf = gpd.read_file(io) gdf.crs = "EPSG:4326" # Convert the drawn geometry to pixel coordinates xr_poly = xr_rasterize(gdf, ds.NDVI.isel(time=0), crs='EPSG:6933') # Construct a mask to only select pixels within the drawn polygon masked_ds = ds.NDVI.where(xr_poly) masked_ds_mean = masked_ds.mean(dim=['x', 'y'], skipna=True) colour = colour_list[polygon_number % len(colour_list)] # Add a layer to the map to make the most recently drawn polygon # the same colour as the line on the plot studyarea_map.add_layer( GeoJSON( data=geo_json, style={ 'color': colour, 'opacity': 1, 'weight': 4.5, 'fillOpacity': 0.0 } ) ) # add new data to the plot xr.plot.plot( masked_ds_mean, marker='*', color=colour, ax=ax ) # reset titles back to custom ax.set_title("Average NDVI from Sentinel-2") ax.set_xlabel("Date") ax.set_ylabel("NDVI") # refresh display fig_display.clear_output(wait=True) # wait=True reduces flicker effect with fig_display: display(fig) with info: print("Plot status: polygon sucessfully added to plot.") # Iterate the polygon number before drawing another polygon polygon_number = polygon_number + 1 else: info.clear_output(wait=True) with info: print("Plot status: this drawing tool is not currently " "supported. Please use the polygon tool.") # call to say activate handle_draw function on draw studyarea_drawctrl.on_draw(handle_draw) with fig_display: # TODO: update with user friendly something display(widgets.HTML("")) # Construct UI: # +-----------------------+ # | instruction | # +-----------+-----------+ # | map | plot | # | | | # +-----------+-----------+ # | info | # +-----------------------+ ui = widgets.VBox([instruction, widgets.HBox([studyarea_map, fig_display]), info]) display(ui)
def create_manhattan_plot(twopoint, multipoint, args): """Creates the manhattan plot from marker data. Args: twopoint (pandas.DataFrame): the two point data (``None`` if not available). multipoint (pandas.DataFrame): the multipoint data (``None`` if not available). args (argparse.Namespace): the options and arguments of the program. Creates manhattan plots from two point or multipoint data. Two point results are shown in a manhattan plot using points (different color for each of the chromosomes). Multi point results are shown using lines. If both two and mutli point data are available, multi point results are shown above two point data. """ import matplotlib as mpl from _tkinter import TclError if args.no_annotation: mpl.use("Agg") import matplotlib.pyplot as plt if args.no_annotation: plt.ioff() # The available chromosomes available_chrom = [] if args.twopoint is not None: available_chrom.append(sorted(twopoint.chrom.unique())) if args.multipoint is not None: available_chrom.append(sorted(multipoint.chrom.unique())) if len(available_chrom) == 1: available_chrom = available_chrom[0] else: if available_chrom[0] != available_chrom[1]: raise ProgramError("chromosomes are not the same for twopoint and " "multipoint data") available_chrom = available_chrom[0] # Creating the figure figure = None try: figure = plt.figure(figsize=(args.graph_width, args.graph_height), frameon=True) except TclError: raise ProgramError("There is no available display, but annotation has " "been asked for... Try using the --no_annotation " "option.") # Getting the maximum and minimum of the confidence value conf_min = [0.0] conf_max = [] if args.twopoint is not None: conf_min.append(twopoint.conf.min()) conf_max.append(twopoint.conf.max()) if args.multipoint is not None: conf_min.append(multipoint.conf.min()) conf_max.append(multipoint.conf.max()) conf_min = min(conf_min) conf_max = max(conf_max) if args.max_ylim is not None: conf_max = args.max_ylim if args.min_ylim is not None: conf_min = args.min_ylim if args.no_negative_values or args.use_pvalues_flag: conf_min = 0.0 # The chromosome spacing chrom_spacing = 25.0 if args.phys_pos_flag: chrom_spacing = 25000000 # Creating the ax and modify it ax = figure.add_subplot(111) ax.xaxis.set_ticks_position("none") ax.yaxis.set_ticks_position("left") ax.set_ylabel("LOD") ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.set_xticks([]) ax.set_xticklabels([]) if args.use_pvalues_flag: ax.set_ylabel(r'$-\log_{10}$ (p value)', fontsize=args.label_text_size) else: ax.set_ylabel(args.graph_y_label, fontsize=args.label_text_size) ax.set_xlabel(args.graph_x_label, fontsize=args.label_text_size) ax.set_title(args.graph_title, fontsize=16, weight="bold") # Now plotting for each of the chromosome starting_pos = 0 annots = [] ticks = [] for i, chrom in enumerate(available_chrom): chrom_twopoint = None chr_multipoint = None max_pos = [] if args.twopoint is not None: chrom_twopoint = twopoint[twopoint.chrom == chrom] max_pos.append(chrom_twopoint.pos.max()) if args.multipoint is not None: chr_multipoint = multipoint[multipoint.chrom == chrom] max_pos.append(chr_multipoint.pos.max()) max_pos = max(max_pos) # The color of the points color = args.even_chromosome_color if i % 2 == 0: color = args.odd_chromosome_color multipoint_color = color # The box xmin = starting_pos - (chrom_spacing / 2) xmax = max_pos + starting_pos + (chrom_spacing / 2) if i % 2 == 1: ax.axvspan(xmin=xmin, xmax=xmax, color=args.chromosome_box_color) # The chromosome label ticks.append((xmin + xmax) / 2) # Plotting the twopoint if args.twopoint is not None: ax.plot(chrom_twopoint.pos + starting_pos, chrom_twopoint.conf, marker="o", ms=args.point_size, mfc=color, mec=color, ls="None") multipoint_color = args.multipoint_color # Plotting the multipoint if args.multipoint is not None: ax.plot(chr_multipoint.pos + starting_pos, chr_multipoint.conf, ls="-", color=multipoint_color, lw=1.2) # Plotting the abline for abline_position in args.abline: ax.axhline(y=abline_position, color="black", ls="--", lw=1.2) if conf_min < 0: ax.axhline(y=0, color="black", ls="-", lw=1.2) # Plotting the significant markers if args.twopoint is not None: sig_mask = chrom_twopoint.conf >= args.significant_threshold ax.plot(chrom_twopoint.pos[sig_mask] + starting_pos, chrom_twopoint.conf[sig_mask], marker="o", ls="None", ms=args.significant_point_size, mfc=args.significant_color, mec=args.significant_color) # If we want annotation if not args.no_annotation: for m_index, m in chrom_twopoint[sig_mask].iterrows(): # The confidence to write the_conf = "{:.3f}".format(m.conf) if args.use_pvalues_flag: the_conf = str(10**(-1 * m.conf)) # The label of the annotation label = "\n".join([m.snp, the_conf]) annot = ax.annotate( label, xy=(m.pos + starting_pos, m.conf), xycoords="data", size=10, xytext=(m.pos + starting_pos, conf_max), va="center", bbox=dict(boxstyle="round", fc="white", ec="black"), textcoords="data", arrowprops=dict(arrowstyle="->", shrinkA=6, shrinkB=5), ) annots.append(annot) # Changing the starting point for the next chromosome starting_pos = max_pos + starting_pos + chrom_spacing # Make the annotation draggable drs = [] for annot in annots: dr = DraggableAnnotation(annot) dr.connect() drs.append(dr) # Setting the limits padding = 0.39 if args.no_y_padding: padding = 0 ax.set_ylim(conf_min - padding, conf_max + padding) ax.set_xlim(0 - chrom_spacing, starting_pos + chrom_spacing) # Putting the xticklabels ax.set_xticks(ticks) ax.set_xticklabels(available_chrom) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(args.axis_text_size) for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(args.chr_text_size) # Saving or plotting the figure mpl.rcParams['savefig.dpi'] = args.dpi mpl.rcParams['ps.papersize'] = "auto" mpl.rcParams['savefig.orientation'] = "landscape" if args.no_annotation or (args.twopoint is None): # Annotation is for two-point only, se we save the figure plt.savefig(args.outFile_name + "." + args.graph_format, bbox_inches="tight") if args.graph_format != "png": plt.savefig(args.outFile_name + ".png", bbox_inches="tight") if args.web: print(args.outFile_name + ".png") else: # There is some two-point data and annotation is asked, se we show # the figure plt.show()
#import pdb; pdb.set_trace() df_welfare = pd.read_csv(folder_WS + '/df_welfare_withparameters.csv', index_col=[0], parse_dates=True) df_welfare['u_change'] = (df_welfare['LEM_u'] - df_welfare['LEM_cost']) - ( df_welfare['fixed_u'] - df_welfare['fixed_cost']) #import pdb; pdb.set_trace() print('Average utility change') print(df_welfare['u_change'].mean()) print('Total utility change') print(df_welfare['u_change'].sum()) #Histogram utility change fig = ppt.figure(figsize=(6, 4), dpi=150) ppt.ioff() ax = fig.add_subplot(111) lns = ppt.scatter(df_welfare['beta'], df_welfare['u_change']) #ax.set_ylim(0,75) # if df_welfare['u_change'].min() > 0.0: # ax.set_xlim(0,df_welfare['u_change'].max()*1.05) # else: # ax.vlines(0,0,max_y,'k',lw=1) # ax.set_xlabel('Utility change [USD]') # if max_y > 0.0: # ax.set_ylim(0,max_y) # ax.set_ylabel('Number of houses') ppt.savefig(folder_WS + '/beta_uchange_' + str(ind_WS) + '.png', bbox_inches='tight') ppt.savefig(folder_WS + '/beta_uchange_' + str(ind_WS) + '.pdf', bbox_inches='tight')
def Motif_Features(self,features,indices,sequences,directory_results,sub_dir,kernel, motif_seq,make_seq_logos=True,color_scheme='weblogo_protein', logo_file_format='.eps'): dir = os.path.join(directory_results,'Motifs',sub_dir) if os.path.exists(dir): shutil.rmtree(dir) os.makedirs(dir) keep_idx = np.sum(self.predicted,-1)!=0 predicted = self.predicted[keep_idx] features = features[keep_idx] indices = indices[keep_idx] sequences = sequences[keep_idx] Y = self.Y[keep_idx] # corr = np.zeros([features.shape[1],self.predicted.shape[1]]) # for ii,f in enumerate(features.T,0): # for jj,p in enumerate(self.predicted.T,0): # corr[ii,jj],_ = spearmanr(f,p) corr = np.zeros([features.shape[1],predicted.shape[1]]) LR = LinearRegression() for jj, p in enumerate(predicted.T, 0): LR.fit(features,p) corr[:,jj] = LR.coef_ for zz,c in enumerate(self.lb.classes_,0): dir = os.path.join(directory_results,'Motifs',sub_dir,c) if os.path.exists(dir): shutil.rmtree(dir) os.makedirs(dir) corr_temp = corr[:,zz] idx = np.flip(np.argsort(corr_temp)) for jj,ft in enumerate(idx,0): idx_sort = np.flip(np.argsort(predicted[:,zz])) ind_sort = indices[idx_sort,ft] seq_sort = sequences[idx_sort] label_sort = Y[idx_sort,zz] ind_sort = ind_sort[label_sort==1] seq_sort = seq_sort[label_sort==1] motifs = [] motifs_logo = [] for ii,(s,i) in enumerate(zip(seq_sort,ind_sort),0): motif = s[int(i):int(i)+kernel] if len(motif) < kernel: motif = motif + 'X' * (kernel - len(motif)) motifs_logo.append(motif) motif = motif.lower() motif = SeqRecord(Seq(motif, IUPAC.protein), str(ii)) motifs.append(motif) if ii > motif_seq-2: break mag_write = str(np.around(corr[ft,zz], 3)) SeqIO.write(motifs, os.path.join(directory_results, 'Motifs', sub_dir, c, str(jj)+'_'+mag_write + '_feature_' + str(ft) + '.fasta'),'fasta') if make_seq_logos: plt.ioff() df_out = Get_Logo_df(motifs_logo, kernel) if df_out.shape[1] >= 1: ax = logomaker.Logo(df_out, color_scheme=color_scheme) ax.style_spines(spines=['top', 'right', 'left', 'bottom'], visible=False) ax.ax.set_xticks([]) ax.ax.set_yticks([]) ax.fig.savefig(os.path.join(directory_results, 'Motifs', sub_dir, c, str(jj)+'_'+mag_write + '_feature_' + str(ft) + logo_file_format)) plt.close() out = pd.DataFrame(corr) out.columns = self.lb.classes_ return out
for t in range(100): ... loss.backward() optimizer.step() # 接着上面来 if t % 2 == 0: plt.cla() # 过了一道 softmax 的激励函数后的最大概率才是预测值 prediction = torch.max(F.softmax(out), 1)[1] pred_y = prediction.data.numpy().squeeze() target_y = y.data.numpy() plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=pred_y, s=100, lw=0, cmap='RdYlGn') accuracy = sum(pred_y == target_y) / 200. # 预测中有多少和真实值一样 plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy, fontdict={ 'size': 20, 'color': 'red' }) plt.pause(0.1) plt.ioff() # 停止画图 plt.show()
def plot(self, figaxs=None, title='', heatmap=True, marker='o', start_nu=True, **kwargs): '''Plot the propagator. Parameters **kwargs: passed down to plot function ''' import matplotlib.pyplot as plt plt.ioff() if figaxs is None: if heatmap: fig, axs = plt.subplots(1, 2, figsize=(16, 8)) else: fig, axs = plt.subplots(1, 1, figsize=(12, 8)) axs = [axs] else: (fig, axs) = figaxs if not heatmap: axs = [axs] # Normalize histogram z = self.histogram z = (1.0 * z.T / z.sum(axis=1)).T z /= self.binsyw # Plot with lines ax = axs[0] for iz, zi in enumerate(z): xi = self.binsxc[iz] # Do not take the first and last final frequency bins, they include the # extremes (loss and fixation) and behave specially xf = self.binsyc[1:-1] y = zi[1:-1] xf0 = 1.2e-3 xf1 = 1.0 - 1.2e-3 ax.plot(xf, y, lw=2, c=cm.jet(1.0 * iz / z.shape[0]), label='$x_i = '+'{:1.1e}'.format(xi)+'$', **kwargs) ax.scatter(xf0, zi[0], s=80, facecolor='none', lw=2, edgecolor=cm.jet(1.0 * iz / z.shape[0]), marker=marker) ax.scatter(xf1, zi[-1], s=80, facecolor='none', lw=2, edgecolor=cm.jet(1.0 * iz / z.shape[0]), marker=marker) if start_nu: ax.axvline(xi, color=cm.jet(1.0 * iz / z.shape[0]), lw=0.5, alpha=0.5, ls='-') if self.use_logit: ax.set_xscale('logit') ax.set_xlim(1e-3, 1 - 1e-3) else: ax.set_xscale('log') ax.set_xlim(1e-3, 1.5) ax.grid(True) ax.set_ylabel('P(x1 | x0)') ax.set_xlabel('Final frequency') ax.set_ylim(1e-3, 1e3) ax.set_yscale('log') #ax.legend(loc=3, fontsize=10, title='Initial frequency:', ncol=2) # Plot with heatmap if heatmap: ax = axs[1] # Do not take the first and last final frequency bins, they include the # extremes (loss and fixation) and behave specially z1 = np.log10(z[:, 1:-1]) im = ax.imshow(z1.T, interpolation='nearest', aspect='auto') ax.set_xlabel('Initial freq') ax.set_ylabel('Final freq') ax.set_xticks(np.arange(len(self.binsx)) - 0.5) ax.set_xticklabels(map('{:1.2e}'.format, self.binsx), rotation=45, fontsize=10) ax.set_yticks(np.arange(len(self.binsy) - 2) - 0.5) ax.set_yticklabels(map('{:1.2e}'.format, self.binsy[1:-1]), rotation=45, fontsize=10) # Reverse the y axis (by default image y coordinates are top to bottom) ax.set_ylim(*(ax.get_ylim()[::-1])) cb = plt.colorbar(im) cb.set_label('log10 P(x1 | x0)', labelpad=30, rotation=270, fontsize=12) if title: if heatmap: fig.suptitle(title, fontsize=16) else: axs[0].set_title(title, fontsize=16) plt.tight_layout(rect=(0, 0, 1, 0.94))
def draw(self, pos=[], figsize=[36, 20], node_size=10000, node_fone_size=8, link_fone_size=9, node_shape='H', path=''): #画图,根据资源占用比显示10级别色差 node_labels = {} # nx.get_node_attributes(G) edge_label = {} # nx.get_edge_attributes(G) node_colors = [] edge_colors = [] node_sizes = [] color_list = [] color_list1 = [(200, 0, 0), (255, 0, 0), (255, 30, 30), (255, 60, 60), (255, 90, 90), (255, 120, 120), (255, 150, 150), (255, 180, 180), (255, 210, 210), (255, 240, 240)] for data in color_list1: color_list.append((data[0] / 255, data[1] / 255, data[2] / 255)) for node in self.G.nodes: color = 0 strs = 'ID: ' + str(node.get_id()) + '\n' if (node.is_access() == False): str1 = ('%-6s ' % ('att')) str2 = ('%-6s ' % ('all')) vnf_strs = [] for vnf in node.get_vnfs(): vnf_strs.append('%-6s ' % (str(vnf.get_name()))) for key in node.get_atts(): if key != 'access': str1 = str1 + ('%-8s' % (str(key))) stra = ('%.3g/%.3g' % (node.get_remain_resource()[key], node.get_atts()[key])) color += node.get_remain_resource( )[key] / node.get_atts()[key] str2 = str2 + ('%-8s' % (stra)) i = 0 for vnf in node.get_vnfs(): stra = ('%.3g/%.3g' % (vnf.get_remain_resource()[key], +vnf.get_atts()[key])) vnf_strs[i] = vnf_strs[i] + ('%-8s' % (stra)) i += 1 color = int(round(10 * color / (len(node.get_atts()) - 1)) - 1) if (color < 0): color = 0 node_colors.append(color_list[color]) node_sizes.append(node_size) strs = strs + str1 + '\n' + str2 for vnf_str in vnf_strs: strs = strs + '\n' + vnf_str else: node_colors.append('red') node_sizes.append(18000) node_labels[node] = strs.rstrip('\n') for edge in self.G.edges: strs = ' ' color = 0 for key in self.G.edges[edge]: if key == 'bandwidth': stra = ('%.3g/%.3g' % (self.G.edges[edge]['remain_bandwidth'], self.G.edges[edge][key])) color = int( round(10 * self.G.edges[edge]['remain_bandwidth'] / self.G.edges[edge][key]) - 1) if (color < 0): color = 0 strs = strs + 'BW' + ':' + stra + '\n' elif key != 'remain_bandwidth': strs = strs + key + ':' + str( self.G.edges[edge][key]) + '\n' edge_label[edge] = strs.rstrip('\n') edge_colors.append(color_list[color]) plt.figure(figsize=figsize) plt.ioff() if (path != ''): fig = plt.gcf() nx.draw(self.G, pos=pos, node_size=node_sizes, node_color=node_colors, width=4, edge_color=edge_colors, node_shape=node_shape) nx.draw_networkx_labels(self.G, pos=pos, labels=node_labels, font_size=node_fone_size) nx.draw_networkx_edge_labels(self.G, pos=pos, edge_labels=edge_label, font_size=link_fone_size) plt.show() plt.close() if (path != ''): fig.savefig(path)
def kd_tree(): try: from kdtree import kdtree from kdnode import kdnode print("Create Kd tree") global xs global ys global points points = [] for i in range(len(xs)): newpoint = point(xs[i], ys[i]) points.append(newpoint) maxbinsz = int(input("Enter max bin size (integer): ")) emax = 100 tree = kdtree(maxbinsz, emax) tree.timer = 0.05 ## Plotting ## minx, miny, maxx, maxy = -2, -2, 12, 12 ## -------- ## root = tree.makeTree(points, minx, miny, maxx, maxy) print("finished") ch = 0 prev = 0 rectangles = 0 while True: print("1. for nearest neighbor") print("0. Exit") try: ch = int(input()) except ValueError: print("That's not an int!") continue if ch == 1: print("Enter: x y NN") i = input() i = i.split(' ') try: query = [float(i[0]), float(i[1])] except ValueError: print("Could not convert string to float") continue if (prev != 0): ## Plotting ## prev.set_color('gray') prev.set_alpha(0.4) for rect in rectangles: rect.set_color('black') rect.set_alpha(0.05) ## -------- ## prev, rectangles = tree.queuryNNwrap(query, int(i[2])) print("finished") elif ch == 0: plt.ioff() points = [] break except: plt.ioff() plt.close("all")
def gift_wrapping(): try: import vector import time print("Convex Hull using gift wrapping method") global xs global ys stepmode = True # 1. Start by finding coordinate with the smallest x value "minind" minval = xs[0] minind = 0 for i in range(len(xs)): if (xs[i] < minval): minind = i minval = xs[i] # 2. Find the point "vstart" making the greatest angle with "minind" maxangle = -9999 vstart = -1 for i in range(len(xs)): if (i != minind): angle = (ys[i] - ys[minind]) / (xs[i] - xs[minind]) if (angle > maxangle): maxangle = angle vstart = i ## Plotting ## plt.ion() fig, ax = plt.subplots() plt.ylim([-2, 12]) plt.xlim([-2, 12]) plt.scatter(xs, ys) plt.plot([xs[minind], xs[vstart]], [ys[minind], ys[vstart]]) fig.canvas.draw() fig.canvas.flush_events() plt.show() if stepmode: a = input("Press Enter to step or c to continue:") if a == 'c': stepmode = False print("finishing...") else: time.sleep(0.01) ## -------- ## # v1 is the vector from vstart->prevstart prevstart = minind v1 = [xs[minind] - xs[vstart], ys[minind] - ys[vstart]] # v2 is the vector from vstart->v2end # 3. iterate through all the v2ends and choose the one that makes the greatest angle with v1. Set v1 to v2end_final->vstart, and repeat until we have wrapped back around to the starting index. while (vstart != minind): maxangle = -9999 v2end_final = -1 save = 0 for v2end in range(len(xs)): if (v2end != vstart and v2end != prevstart): v2 = [xs[v2end] - xs[vstart], ys[v2end] - ys[vstart]] ## Plotting ## lines = plt.plot([xs[v2end], xs[vstart]], [ys[v2end], ys[vstart]]) plt.show() fig.canvas.draw() fig.canvas.flush_events() if stepmode: a = input("Press Enter to step or c to continue:") if a == 'c': stepmode = False print("finishing...") else: time.sleep(0.01) lines.pop(0).remove() plt.show() ## -------- ## angle = vector.angle(v1, v2) if (angle > maxangle): ## Plotting ## if (save != 0): save.pop(0).remove() save = plt.plot([xs[v2end], xs[vstart]], [ys[v2end], ys[vstart]]) plt.show() ## -------- ## maxangle = angle v2end_final = v2end plt.plot([xs[v2end_final], xs[vstart]], [ys[v2end_final], ys[vstart]]) plt.show() v1 = [xs[vstart] - xs[v2end_final], ys[vstart] - ys[v2end_final]] prevstart = vstart vstart = v2end_final stepmode = True print("finished") plt.ioff() display_plot() except: plt.ioff() plt.close("all")
KEYPRESS4 = False KEYPRESS5 = False KEYPRESS6 = False KEYPRESS7 = False KEYPRESS8 = False KEYPRESS9 = False KEYPRESS10 = False KEYPRESS11 = False KEYPRESS12 = False KEYPRESS13 = False KEYPRESS14 = False KEYPRESS15 = False KEYPRESS16 = False KEYPRESS17 = False KEYPRESS18 = False KEYPRESS19 = False KEYPRESS20 = False KEYPRESS21 = False KEYPRESS22 = False KEYPRESS23 = False KEYPRESS24 = False my_line.set_ydata(outputg) #update the plot pyplot.ioff() # Close audio stream stream.stop_stream() stream.close() stream2.stop_stream() stream2.close() p.terminate()
def grahams_scan(): try: import vector import time print("Convex Hull using gift Graham's Scan") global xs global ys global points points = [] stepmode = True # 1. Find leftmost minval = xs[0] minind = 0 for i in range(len(xs)): if (xs[i] < minval): minind = i minval = xs[i] leftest = point(xs[minind], ys[minind]) # 2. Sort points by polar angle for i in range(len(xs)): if (i != minind): newpoint = point(xs[i], ys[i]) newpoint.angle = (ys[i] - ys[minind]) / (xs[i] - xs[minind]) points.append(newpoint) points.sort(key=lambda x: x.angle, reverse=False) points.append(leftest) ## Plotting ## plt.ion() fig, ax = plt.subplots() plt.ylim([-2, 12]) plt.xlim([-2, 12]) plt.scatter(xs, ys) plt.plot([points[len(points) - 1].x, points[0].x], [points[len(points) - 1].y, points[0].y]) fig.canvas.draw() fig.canvas.flush_events() plt.show() if stepmode: a = input("Press Enter to step or c to continue:") if a == 'c': stepmode = False print("finishing...") else: time.sleep(0.01) ## -------- ## # 3. While end point doesn't equal initial start point end = 0 lines = [] while end != len(points) - 1: # Get next point ## Plotting ## lines.append( plt.plot([points[end].x, points[end + 1].x], [points[end].y, points[end + 1].y])) plt.show() fig.canvas.draw() fig.canvas.flush_events() if stepmode: a = input("Press Enter to step or c to continue:") if a == 'c': stepmode = False else: time.sleep(0.01) ## -------- ## # 4. (end-1) <-v1- (end) -v2-> (end+1) v1 = [ points[end].x - points[end - 1].x, points[end].y - points[end - 1].y ] v2 = [ points[end].x - points[end + 1].x, points[end].y - points[end + 1].y ] angle = vector.fullangle(v2, v1) while angle <= 0.0: points.pop(end) end -= 1 ## Plotting ## if len(lines) > 1: lines[len(lines) - 1].pop(0).remove() lines[len(lines) - 2].pop(0).remove() lines.pop(len(lines) - 1) lines.pop(len(lines) - 1) lines.append( plt.plot([points[end].x, points[end + 1].x], [points[end].y, points[end + 1].y])) plt.show() fig.canvas.draw() fig.canvas.flush_events() if stepmode: a = input("Press Enter to step or c to continue:") if a == 'c': stepmode = False print("finishing...") else: time.sleep(0.01) ## -------- ## v1 = [ points[end].x - points[end - 1].x, points[end].y - points[end - 1].y ] v2 = [ points[end].x - points[end + 1].x, points[end].y - points[end + 1].y ] angle = vector.fullangle(v2, v1) # Until the angle between this point and previous point is less than or equal to 180 # Move previous point back by one end += 1 stepmod = True print("finished") plt.ioff() points = [] except: plt.ioff() plt.close("all")
def plot_1Dspecs( filelist, plotname='./tdose_1Dspectra.pdf', colors=None, labels=None, plotSNcurve=False, tdose_wavecol='wave', tdose_fluxcol='flux', tdose_errcol='fluxerror', simsources=None, simsourcefile='/Users/kschmidt/work/TDOSE/mock_cube_sourcecat161213_all.fits', sim_cube_dim=None, comparisonspecs=None, comp_colors=['blue'], comp_labels=None, comp_wavecol='WAVE_AIR', comp_fluxcol='FLUX', comp_errcol='FLUXERR', xrange=None, yrange=None, showspecs=False, shownoise=True, skyspecs=None, sky_colors=['red'], sky_labels=['sky'], sky_wavecol='lambda', sky_fluxcol='data', sky_errcol='stat', showlinelists=None, linelistcolors=['gray'], smooth=0, ylog=False, plotratio=False, verbose=True, pubversion=False): """ Plots of multiple 1D spectra --- INPUT --- filelist List of spectra filenames to plot plotname Name of plot to generate colors Colors of the spectra in filelist to use labels Labels of the spectra in filelist to use plotSNcurve Show signal-to-noise curve instead of flux spectra tdose_wavecol Wavelength column of the spectra in filelist tdose_fluxcol Flux column of the spectra in filelist tdose_errcol Flux error column of the spectra in filelist simsources To plot simulated sources provide ids here simsourcefile Source file with simulated sources to plot sim_cube_dim Dimensions of simulated cubes comparisonspecs To plot comparison spectra provide the filenames of those here comp_colors Colors of the spectra in comparisonspecs list to use comp_labels Labels of the spectra in comparisonspecs list to use comp_wavecol Wavelength column of the spectra in comparisonspecs list comp_fluxcol Flux column of the spectra in comparisonspecs list comp_errcol Flux error column of the spectra in comparisonspecs list xrange Xrange of plot yrange Yrange of plot showspecs To show plot instead of storing it to disk set showspecs=True shownoise To add noise envelope around spectrum set shownoise=True skyspecs To plot sky spectra provide the filenames of those here sky_colors Colors of the spectra in skyspecs list to use sky_labels Labels of the spectra in skyspecs list to use sky_wavecol Wavelength column of the spectra in skyspecs list sky_fluxcol Flux column of the spectra in skyspecs list sky_errcol Flux error column of the spectra in skyspecs list showlinelists To show line lists provide a list of arrays of dimension (Nlines,2) where each row in the arrays contains [waveobs, name], where 'waveobs' is the observed wavelengths and 'name' is a string with the name of each of the Nlines postions to mark on the spectrum. linelistcolors List of colors for line lists provided in showlinelists smooth To smooth the spectra, provide sigma of the 1D gaussian smoothing kernel to apply. For smooth = 0, no smoothing is performed. ylog To plot y-axis in log scale set to true plotratio To plot the ratio between the main spectrum and the comparison spectra instead of the actual spectra, set this keyword to true. verbose Toggle verbosity pubversion Generate more publication friendly version of figure """ if len(filelist) == 1: if verbose: print ' - Plotting data from ' + filelist[0] else: if verbose: print ' - Plotting data from filelist ' if pubversion: fig = plt.figure(figsize=(6, 3)) fig.subplots_adjust(wspace=0.1, hspace=0.1, left=0.15, right=0.95, bottom=0.18, top=0.83) Fsize = 12 else: fig = plt.figure(figsize=(10, 3)) fig.subplots_adjust(wspace=0.1, hspace=0.1, left=0.06, right=0.81, bottom=0.15, top=0.95) Fsize = 10 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Looking for flux units in spectra bunit = 'BUNIT FLUX' # Default BUNIT for unitspec in filelist: if bunit == 'BUNIT FLUX': try: sourcecubehdr = afits.open(unitspec)['SOURCECUBE'].header bunit = sourcecubehdr['BUNIT'] except: try: # Backwards compatibility to TDOSE v2.0 extractions sourcecubehdr = afits.open(unitspec)[2].header bunit = sourcecubehdr['BUNIT'] except: pass if bunit == 'BUNIT FLUX': if verbose: print( ' - Did not find BUNIT in SOURCECUBE header for any spectra in filelist - are they not from TDOSE?' ) if bunit == '10**(-20)*erg/s/cm**2/Angstrom': # Making bunit LaTeXy for MUSE-Wide BUNIT format bunit = '1e-20 erg/s/cm$^2$/\AA' else: bunit = '$' + bunit + '$' # minimizing pronlems with LaTeXing plot axes # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - lthick = 1 plt.rc('text', usetex=True) plt.rc('font', family='serif', size=Fsize) plt.rc('xtick', labelsize=Fsize) plt.rc('ytick', labelsize=Fsize) plt.clf() plt.ioff() #plt.title(plotname.split('TDOSE 1D spectra'),fontsize=Fsize) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for ff, specfile in enumerate(filelist): specdat = pyfits.open(specfile)[1].data if colors is None: spec_color = None else: spec_color = colors[ff] if labels is None: spec_label = specfile else: spec_label = labels[ff] if xrange is not None: goodent = np.where((specdat[tdose_wavecol] > xrange[0]) & (specdat[tdose_wavecol] < xrange[1]))[0] if goodent == []: if verbose: print ' - The chosen xrange is not covered by the input spectrum. Plotting full spectrum' goodent = np.arange(len(specdat[tdose_wavecol])) else: goodent = np.arange(len(specdat[tdose_wavecol])) if plotSNcurve: try: s2ndat = specdat['s2n'][goodent] except: s2ndat = specdat[tdose_fluxcol][goodent] / specdat[ tdose_errcol][goodent] if smooth > 0: s2ndat = snf.gaussian_filter(s2ndat, smooth) if not plotratio: plt.plot(specdat[tdose_wavecol][goodent], s2ndat, color=spec_color, lw=lthick, label=spec_label) ylabel = 'S/N' else: plt.plot(specdat[tdose_wavecol][goodent], s2ndat / s2ndat, color=spec_color, lw=lthick, label=None) ylabel = 'S/N ratio' #plotname = plotname.replace('.pdf','_S2N.pdf') else: fillalpha = 0.30 fluxdat = specdat[tdose_fluxcol][goodent] errlow = specdat[tdose_fluxcol][goodent] - specdat[tdose_errcol][ goodent] errhigh = specdat[tdose_fluxcol][goodent] + specdat[tdose_errcol][ goodent] if smooth > 0: fluxdat = snf.gaussian_filter(fluxdat, smooth) errlow = snf.gaussian_filter(errlow, smooth) errhigh = snf.gaussian_filter(errhigh, smooth) if smooth > 0: fluxdat = snf.gaussian_filter(fluxdat, smooth) if not plotratio: if shownoise: plt.fill_between(specdat[tdose_wavecol][goodent], errlow, errhigh, alpha=fillalpha, color=spec_color) plt.plot(specdat[tdose_wavecol][goodent], fluxdat, color=spec_color, lw=lthick, label=spec_label) ylabel = tdose_fluxcol else: plt.plot(specdat[tdose_wavecol][goodent], fluxdat / fluxdat, color=spec_color, lw=lthick, label=None) ylabel = tdose_fluxcol + ' ratio ' # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if simsources is not None: sim_total = np.zeros(len(specdat[tdose_wavecol])) for sourcenumber in simsources: sourcedat = pyfits.open(simsourcefile)[1].data xpos = sourcedat['xpos'][sourcenumber] ypos = sourcedat['ypos'][sourcenumber] fluxscale = sourcedat['fluxscale'][sourcenumber] sourcetype = sourcedat['sourcetype'][sourcenumber] spectype = sourcedat['spectype'][sourcenumber] sourcecube = tbmc.gen_source_cube([ypos, xpos], fluxscale, sourcetype, spectype, cube_dim=sim_cube_dim, verbose=verbose, showsourceimgs=False) simspec = np.sum(np.sum(sourcecube, axis=1), axis=1) sim_total = sim_total + simspec if smooth > 0: simspec = snf.gaussian_filter(simspec, smooth) plt.plot(specdat[tdose_wavecol], simspec, '--', color='black', lw=lthick) plt.plot(specdat[tdose_wavecol], sim_total, '--', color='black', lw=lthick, label='Sim. spectrum: \nsimsource=' + str(simsources)) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if comparisonspecs is not None: for cc, comparisonspec in enumerate(comparisonspecs): compdat = pyfits.open(comparisonspec)[1].data if xrange is not None: goodent = np.where((compdat[comp_wavecol] > xrange[0]) & (compdat[comp_wavecol] < xrange[1]))[0] if goodent == []: if verbose: print ' - The chosen xrange is not covered by the comparison spectrum. Plotting full spectrum' goodent = np.arange(len(compdat[comp_wavecol])) else: goodent = np.arange(len(compdat[comp_wavecol])) if comp_colors is None: comp_color = None else: comp_color = comp_colors[cc] if comp_labels is None: comp_label = comparisonspec else: comp_label = comp_labels[cc] if plotSNcurve: s2ncompdat = compdat[comp_fluxcol][goodent] / compdat[ comp_errcol][goodent] if smooth > 0: s2ncompdat = snf.gaussian_filter(s2ncompdat, smooth) if not plotratio: plt.plot(compdat[comp_wavecol][goodent], s2ncompdat, color=comp_color, lw=lthick, label=comp_label) else: plt.plot(compdat[comp_wavecol][goodent], s2ndat / s2ncompdat, color=comp_color, lw=lthick, label=comp_label) else: fillalpha = 0.30 fluxcompdat = compdat[comp_fluxcol][goodent] errlow = compdat[comp_fluxcol][goodent] - compdat[comp_errcol][ goodent] errhigh = compdat[comp_fluxcol][goodent] + compdat[ comp_errcol][goodent] if smooth > 0: fluxcompdat = snf.gaussian_filter(fluxcompdat, smooth) errlow = snf.gaussian_filter(errlow, smooth) errhigh = snf.gaussian_filter(errhigh, smooth) if not plotratio: if shownoise: plt.fill_between(compdat[comp_wavecol][goodent], errlow, errhigh, alpha=fillalpha, color=comp_color) plt.plot(compdat[comp_wavecol][goodent], fluxcompdat, color=comp_color, lw=lthick, label=comp_label) else: plt.plot(compdat[comp_wavecol][goodent], fluxdat / fluxcompdat, color=comp_color, lw=lthick, label=comp_label) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if skyspecs is not None: for ss, skyspec in enumerate(skyspecs): skydat = pyfits.open(skyspec)[1].data if xrange is not None: goodent = np.where((skydat[sky_wavecol] > xrange[0]) & (skydat[sky_wavecol] < xrange[1]))[0] if goodent == []: if verbose: print ' - The chosen xrange is not covered by the sky spectrum. Plotting full spectrum' goodent = np.arange(len(skydat[sky_wavecol])) else: goodent = np.arange(len(skydat[sky_wavecol])) if sky_colors is None: sky_color = None else: sky_color = sky_colors[ss] if sky_labels is None: sky_label = skyspec else: sky_label = sky_labels[ss] if plotSNcurve: s2nsky = skydat[sky_fluxcol][goodent] / skydat[sky_errcol][ goodent] if smooth > 0: s2nsky = snf.gaussian_filter(s2nsky, smooth) plt.plot(skydat[sky_wavecol][goodent], s2nsky, color=sky_color, lw=lthick, label=sky_label) else: fillalpha = 0.30 fluxsky = skydat[sky_fluxcol][goodent] errlow = skydat[sky_fluxcol][goodent] - skydat[sky_errcol][ goodent] errhigh = skydat[sky_fluxcol][goodent] + skydat[sky_errcol][ goodent] if smooth > 0: fluxsky = snf.gaussian_filter(fluxsky, smooth) errlow = snf.gaussian_filter(errlow, smooth) errhigh = snf.gaussian_filter(errhigh, smooth) if shownoise: plt.fill_between(skydat[sky_wavecol][goodent], errlow, errhigh, alpha=fillalpha, color=sky_color) plt.plot(skydat[sky_wavecol][goodent], fluxsky, color=sky_color, lw=lthick, label=sky_label) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if xrange is None: xvals = [4800, 9300] else: xvals = xrange plt.plot(xvals, [0, 0], '--k', lw=lthick) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - plt.xlabel('Wavelength [\AA]', fontsize=Fsize) if pubversion: if plotSNcurve: ylabel = 'Signal-to-Noise' else: ylabel = 'Flux [' + str(bunit) + ']' if plotratio: ylabel = ylabel + ' ratio' plt.ylabel(ylabel, fontsize=Fsize) if ylog: plt.yscale('log') if yrange is not None: plt.ylim(yrange) if xrange is not None: plt.xlim(xrange) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if showlinelists is not None: for sl, showlinelist in enumerate(showlinelists): ymin, ymax = plt.ylim() xmin, xmax = plt.xlim() for ww, wave in enumerate(showlinelist[:, 0]): wave = float(wave) if (wave < xmax) & (wave > xmin): plt.plot([wave, wave], [ymin, ymax], linestyle='--', color=linelistcolors[sl], lw=lthick) plt.text(wave, ymin + 1.03 * np.abs([ymax - ymin]), showlinelist[:, 1][ww], color=linelistcolors[sl], fontsize=Fsize - 2., ha='center') # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if pubversion: leg = plt.legend(fancybox=True, loc='upper center', prop={'size': Fsize - 2}, ncol=4, numpoints=1, bbox_to_anchor=(0.44, 1.27)) # add the legend else: leg = plt.legend(fancybox=True, loc='upper right', prop={'size': Fsize}, ncol=1, numpoints=1, bbox_to_anchor=(1.25, 1.03)) # add the legend leg.get_frame().set_alpha(0.7) if showspecs: if verbose: print ' Showing plot (not saving to file)' plt.show() else: if verbose: print ' Saving plot to', plotname plt.savefig(plotname) plt.clf() plt.close('all')
def train_model(dataset, model, loss, task=None, loss_task=None, options={}, plot_options={}, save_with={}): # Global training parameters name = options.get('name', 'model') epochs = options.get('epochs', 10000) save_epochs = options.get('save_epochs', 2000) best_save_epochs = options.get('best_save_epochs', save_epochs) plot_epochs = options.get('plot_epochs', 100) batch_size = options.get('batch_size', 64) image_export = options.get('image_export', False) nb_reconstructions = options.get('nb_reconstructions', 3) save_threshold = options.get('save_threshold', 100) remote = options.get('remote', None) if loss_task is None: loss_task = task if not task is None else None # Setting results & plotting directories results_folder = options.get('results_folder', 'saves/' + name) figures_folder = options.get('figures_folder', results_folder + '/figures') if not os.path.isdir(results_folder): os.makedirs(results_folder) if not os.path.isdir(figures_folder): os.makedirs(figures_folder) # Init training epoch = -1 min_test_loss = np.inf best_model = None reconstruction_ids = np.random.permutation( len(dataset))[:nb_reconstructions**2] best_model = None # Start training! while epoch < epochs: print('-----EPOCH %d' % epoch) epoch += 1 loader = DataLoader(dataset, batch_size=batch_size, partition='train', task=task) # train phase batch = 0 current_loss = 0 train_losses = None model.train() for x, y in loader: x = model.format_input_data(x) y = model.format_label_data(y) if loss_task == task: y_task = y else: if not loss_task is None: plabel = {'dim': dataset.classes[loss_task]['_length']} y_task = model.format_label_data( dataset.metadata.get(loss_task)[loader.current_ids], plabel=plabel) else: y_task = None out = model.forward(x, y=y) batch_loss, losses = loss.loss(model, out, x=x, y=y_task, epoch=epoch) if train_losses is None: train_losses = losses else: train_losses += losses model.step(batch_loss) print("epoch %d / batch %d / losses : %s " % (epoch, batch, loss.get_named_losses(losses))) current_loss += batch_loss batch += 1 current_loss /= batch print('--- FINAL LOSS : %s' % current_loss) loss.write('train', train_losses) ## test_phase with torch.no_grad(): model.eval() test_data = model.format_input_data(dataset['test'][:]) test_metadata = model.format_label_data(dataset.metadata[task][ dataset.partitions['test']]) if not task is None else None out = model.forward(test_data, y=test_metadata) if loss_task == task: y_task = y else: if not loss_task is None: plabel = {'dim': dataset.classes[loss_task]['_length']} test_ids = dataset.partitions.get('test') y_task = model.format_label_data( dataset.metadata.get(loss_task)[test_ids], plabel=plabel) else: y_task = None test_loss, losses = loss.loss(model, out, x=test_data, y=y_task) loss.write('test', losses) if test_loss < min_test_loss and epoch > save_threshold: min_test_loss = test_loss print('-- saving best model at %s' % 'results/%s/%s_%d.t7' % (results_folder, name, epoch)) model.save('%s/%s_best.t7' % (results_folder, name), loss=loss, epoch=epoch, partitions=dataset.partitions) model.schedule(test_loss) plt.ioff() # Save models if epoch % save_epochs == 0: print('-- saving model at %s' % 'results/%s/%s_%d.t7' % (results_folder, name, epoch)) model.save('%s/%s_%d.t7' % (results_folder, name, epoch), loss=loss, epoch=epoch, partitions=dataset.partitions, **save_with) # Make plots if epoch % plot_epochs == 0: plt.close('all') n_points = plot_options.get('plot_npoints', min(dataset.data.shape[0], 5000)) plot_tasks = plot_options.get('plot_tasks', dataset.tasks) plot_dimensions = plot_options.get( 'plot_dimensions', list(range(model.platent[-1]['dim']))) plot_layers = plot_options.get('plot_layers', list(range(len(model.platent)))) if plot_options.get('plot_reconstructions', True): print('plotting reconstructions...') lplt.plot_reconstructions(dataset, model, label=task, out=figures_folder + '/reconstructions_%d.svg' % epoch) if plot_options.get('plot_latentspace', True): transformation = PCA(n_components=3) print('plotting latent spaces...') lplt.plot_latent3(dataset, model, transformation, label=task, tasks=plot_tasks, layers=plot_layers, n_points=n_points, out=figures_folder + '/latent_%d' % epoch) if plot_options.get('plot_statistics', True): print('plotting latent statistics...') lplt.plot_latent_stats(dataset, model, label=task, tasks=plot_tasks, layers=plot_layers, legend=True, n_points=n_points, balanced=True, out=figures_folder + '/statistics_%d' % epoch) if plot_options.get('plot_distributions', True): print('plotting latent distributions...') lplt.plot_latent_dists(dataset, model, label=task, tasks=plot_tasks, n_points=n_points, out=figures_folder + '/dists_%d' % epoch, dims=plot_dimensions, split=False, legend=True, bins=10, relief=True) if plot_options.get('plot_losses', True): print('plotting losses...') lplt.plot_class_losses(dataset, model, loss, label=task, tasks=plot_tasks, loss_task=loss_task, out=figures_folder + '/losses') if not remote is None: print('scp -r %s %s:' % (figures_folder, remote)) os.system('scp -r %s %s:' % (figures_folder, remote)) if image_export: images = dataset[reconstruction_ids] if not task is None: metadata = dataset.metadata[task][reconstruction_ids] else: metadata = None out = model.pinput[0]['dist']( *model.forward(images, y=metadata)['x_params'][0]).mean torchvision.utils.save_image(out.reshape(out.size(0), 1, 28, 28), figures_folder + 'grid_%d.png' % epoch, nrow=nb_reconstructions) model.save('%s/%s_final.t7' % (results_folder, name), loss=loss, epoch=epoch, partitions=dataset.partitions, **save_with)
def blockplt(): ''' block plt''' plt.ioff() plt.show()
def runSimulation( trials=30, imagePath=None, usePerfectResponses=False, stimuli={ 'minContrast': 0.01, 'maxContrast': 1, 'contrastResolution': 24, 'minFrequency': .2, 'maxFrequency': 36, 'frequencyResolution': 20, }, parameters={ 'truePeakSensitivity': 18, 'truePeakFrequency': 11, 'trueBandwidth': 12, 'trueBandwidth': 11, }, ): logger.info('Starting simulation') numpy.random.seed() if imagePath is not None: pathlib.Path(imagePath).mkdir(parents=True, exist_ok=True) stimulusSpace = numpy.array([ QuickCSF.makeContrastSpace(stimuli['minContrast'], stimuli['maxContrast'], stimuli['contrastResolution']), QuickCSF.makeFrequencySpace(stimuli['minFrequency'], stimuli['maxFrequency'], stimuli['frequencyResolution']) ]) unmappedTrueParams = numpy.array([[ parameters['truePeakSensitivity'], parameters['truePeakFrequency'], parameters['trueBandwidth'], parameters['trueBandwidth'], ]]) qcsf = QuickCSF.QuickCSFEstimator(stimulusSpace) graph = plot(qcsf, unmappedTrueParams=unmappedTrueParams) # Trial loop for i in range(trials): # Get the next stimulus stimulus = qcsf.next() newStimValues = numpy.array([[stimulus.contrast, stimulus.frequency]]) # Simulate a response if usePerfectResponses: logger.debug('Simulating perfect response') frequency = newStimValues[:, 1] trueSens = numpy.power( 10, QuickCSF.csf_unmapped(unmappedTrueParams, numpy.array([frequency]))) testContrast = newStimValues[:, 0] testSens = 1 / testContrast response = trueSens > testSens else: logger.debug('Simulating human response response') p = qcsf._pmeas(unmappedTrueParams) response = numpy.random.rand() < p qcsf.markResponse(response) # Update the plot graph.clear() graph.set_title(f'Estimated Contrast Sensitivity Function ({i+1})') plot(qcsf, graph, unmappedTrueParams) if imagePath is not None: plt.savefig( pathlib.Path(imagePath + '/%f.png' % time.time()).resolve()) logger.info('Simulation complete') print('******* History *******') for record in qcsf.responseHistory: print(f'\tf={record[0][1]},c={record[0][0]},r={record[1]}') print('***********************') paramEstimates = qcsf.getResults() logger.info('Results: ' + str(paramEstimates)) trueParams = QuickCSF.mapCSFParams(unmappedTrueParams, True).T print('******* Results *******') print(f'\tEstimates = {paramEstimates}') print(f'\tActuals = {trueParams}') print('***********************') plt.ioff() plt.show()
def check_background_SN(ramp_file='id1ketvxq_ramp.dat', show=False): """ Check how S/N evolves with adding reads with increasing background """ from astropy.table import Table import matplotlib.pyplot as plt import numpy as np time, bg = np.loadtxt(ramp_file, unpack=True) t0 = np.diff(time)[0] time, bg = time[1:], bg[1:] # skip first 2.9 s read if len(bg) <= 2: return [] dt = np.append(t0, np.diff(time)) s = 1. # test count rate so = np.argsort(bg) NREAD = len(bg) reads = np.arange(NREAD) + 1 fluence = np.cumsum((s * dt)[so]) rms = np.sqrt(np.cumsum((bg * dt)[so])) rms_optimal = np.sqrt(np.cumsum((bg[so][2] * dt)[so])) max_ix = np.argmax(fluence / rms) if max_ix < (NREAD - 1): pop_reads = list(reads[so][max_ix + 1:]) else: pop_reads = [] ### where observed rms < expected RMS for flat background other_bad = list(reads[so][rms / rms_optimal > 1.25]) pop_reads = np.cast[int](np.unique(np.hstack((pop_reads, other_bad)))) pop_reads = list(pop_reads) if len(pop_reads) > 0: # np.savetxt(ramp_file.replace('ramp.dat', 'ramp.pop.png'), pop_reads, # fmt='%d') fp = open('/tmp/' + ramp_file.replace('ramp.dat', 'ramp.pop.dat'), 'w') for r in pop_reads: fp.write('%d\n' % (r)) fp.close() if show: plt.ioff() fig = plt.figure(figsize=[6, 3]) ax = fig.add_subplot(111) ax.plot(time, bg, color='0.8', linewidth=6, zorder=1) si = 60 ax.scatter(time, bg, color='w', s=si, zorder=8) ax.scatter(time, bg, color='k', s=0.5 * si, zorder=9) if len(pop_reads) > 0: ix = np.array(pop_reads) ax.scatter(time[ix - 1], bg[ix - 1], color='r', zorder=10, s=0.5 * si) ax.grid() ax.set_title(ramp_file) ax.set_xlabel('time'), ax.set_ylabel('background') fig.tight_layout(pad=0.1) fig.savefig('/tmp/' + ramp_file.replace('.dat', '.pop.png')) plt.close() return pop_reads
def plots(outimage, imageprm={}, filename=None, angunit=None, uvunit=None, plotargs={ 'ms': 1., }): isinteractive = plt.isinteractive() backend = matplotlib.rcParams["backend"] if angunit is None: angunit = outimage.angunit if isinteractive: plt.ioff() matplotlib.use('Agg') nullfmt = NullFormatter() # Label if uvunit.lower().find("l") == 0: unitlabel = r"$\lambda$" elif uvunit.lower().find("kl") == 0: unitlabel = r"$10^3 \lambda$" elif uvunit.lower().find("ml") == 0: unitlabel = r"$10^6 \lambda$" elif uvunit.lower().find("gl") == 0: unitlabel = r"$10^9 \lambda$" elif uvunit.lower().find("m") == 0: unitlabel = "m" elif uvunit.lower().find("km") == 0: unitlabel = "km" else: print("Error: uvunit=%s is not supported" % (unit2)) return -1 # Get statistics stats = statistics(outimage, **imageprm) # Open File if filename is not None: pdf = PdfPages(filename) # Save Image if filename is not None: util.matplotlibrc(nrows=1, ncols=1, width=600, height=600) else: matplotlib.rcdefaults() plt.figure() outimage.imshow(angunit=angunit) if filename is not None: pdf.savefig() plt.close() # fcv if stats["isfcv"] == True: table = imageprm["vistable"] if uvunit is None: uvunit = table.uvunit # Get model data model = table.eval_image(imfits=outimage, mask=None, amptable=False, istokes=0, ifreq=0) resid = table.residual_image(imfits=outimage, mask=None, amptable=False, istokes=0, ifreq=0) if filename is not None: util.matplotlibrc(nrows=3, ncols=1, width=600, height=200) else: matplotlib.rcdefaults() fig, axs = plt.subplots(nrows=3, ncols=1, sharex=True) plt.subplots_adjust(hspace=0) ax = axs[0] plt.sca(ax) table.radplot(uvunit=uvunit, datatype="amp", color="black", **plotargs) model.radplot(uvunit=uvunit, datatype="amp", color="red", errorbar=False, **plotargs) plt.xlabel("") ax = axs[1] plt.sca(ax) table.radplot(uvunit=uvunit, datatype="phase", color="black", **plotargs) model.radplot(uvunit=uvunit, datatype="phase", color="red", errorbar=False, **plotargs) plt.xlabel("") ax = axs[2] plt.sca(ax) resid.radplot(uvunit=uvunit, datatype="real", normerror=True, errorbar=False, color="blue", **plotargs) resid.radplot(uvunit=uvunit, datatype="imag", normerror=True, errorbar=False, color="red", **plotargs) plt.axhline(0, color="black", ls="--") plt.ylabel("Normalized Residuals") plt.xlabel(r"Baseline Length (%s)" % (unitlabel)) plt.legend(ncol=2) divider = make_axes_locatable(ax) # Histgram cax = divider.append_axes("right", size="10%", pad=0.05) normresidr = resid["amp"] * np.cos(np.deg2rad( resid["phase"])) / resid["sigma"] normresidi = resid["amp"] * np.sin(np.deg2rad( resid["phase"])) / resid["sigma"] normresid = np.concatenate([normresidr, normresidi]) N = len(normresid) ymin, ymax = ax.get_ylim() y = np.linspace(ymin, ymax, 1000) x = 1 / np.sqrt(2 * np.pi) * np.exp(-y * y / 2.) cax.hist(normresid, bins=np.int(np.sqrt(N)), normed=True, orientation='horizontal') cax.plot(x, y, color="red") cax.set_ylim(ax.get_ylim()) cax.axhline(0, color="black", ls="--") cax.yaxis.set_major_formatter(nullfmt) cax.xaxis.set_major_formatter(nullfmt) if filename is not None: pdf.savefig() plt.close() if stats["isamp"] == True: table = imageprm["amptable"] if uvunit is None: uvunit = table.uvunit # Get model data model = table.eval_image(imfits=outimage, mask=None, amptable=True, istokes=0, ifreq=0) resid = table.residual_image(imfits=outimage, mask=None, amptable=True, istokes=0, ifreq=0) if filename is not None: util.matplotlibrc(nrows=2, ncols=1, width=600, height=300) else: matplotlib.rcdefaults() fig, axs = plt.subplots(nrows=2, ncols=1, sharex=True) plt.subplots_adjust(hspace=0) ax = axs[0] plt.sca(ax) table.radplot(uvunit=uvunit, datatype="amp", color="black", **plotargs) model.radplot(uvunit=uvunit, datatype="amp", color="red", errorbar=False, **plotargs) plt.xlabel("") ax = axs[1] plt.sca(ax) resid.radplot(uvunit=uvunit, datatype="amp", normerror=True, errorbar=False, color="black", **plotargs) plt.axhline(0, color="black", ls="--") ymin = np.min(resid["amp"] / resid["sigma"]) * 1.1 plt.ylim(ymin, ) plt.ylabel("Normalized Residuals") plt.xlabel(r"Baseline Length (%s)" % (unitlabel)) divider = make_axes_locatable(ax) # Histgram cax = divider.append_axes("right", size="10%", pad=0.05) normresid = resid["amp"] / resid["sigma"] N = len(normresid) ymin, ymax = ax.get_ylim() y = np.linspace(ymin, ymax, 1000) x = 1 / np.sqrt(2 * np.pi) * np.exp(-y * y / 2.) cax.hist(normresid, bins=np.int(np.sqrt(N)), normed=True, orientation='horizontal') cax.plot(x, y, color="red") cax.set_ylim(ax.get_ylim()) cax.axhline(0, color="black", ls="--") cax.yaxis.set_major_formatter(nullfmt) cax.xaxis.set_major_formatter(nullfmt) if filename is not None: pdf.savefig() plt.close() # Closure Amplitude if stats["isca"] == True: table = imageprm["catable"] if uvunit is None: uvunit = table.uvunit # Get model data model = table.eval_image(imfits=outimage, mask=None, istokes=0, ifreq=0) resid = table.residual_image(imfits=outimage, mask=None, istokes=0, ifreq=0) if filename is not None: util.matplotlibrc(nrows=2, ncols=1, width=600, height=300) else: matplotlib.rcdefaults() fig, axs = plt.subplots(nrows=2, ncols=1, sharex=True) plt.subplots_adjust(hspace=0) ax = axs[0] plt.sca(ax) table.radplot(uvunit=uvunit, uvdtype="ave", color="black", log=True, **plotargs) model.radplot(uvunit=uvunit, uvdtype="ave", color="red", log=True, errorbar=False, **plotargs) plt.xlabel("") ax = axs[1] plt.sca(ax) resid.radplot(uvunit=uvunit, uvdtype="ave", log=True, normerror=True, errorbar=False, color="black", **plotargs) plt.axhline(0, color="black", ls="--") plt.ylabel("Normalized Residuals") plt.xlabel(r"Baseline Length (%s)" % (unitlabel)) divider = make_axes_locatable(ax) # Histgram cax = divider.append_axes("right", size="10%", pad=0.05) normresid = resid["logamp"] / resid["logsigma"] N = len(normresid) ymin, ymax = ax.get_ylim() xmin = np.min(normresid) xmax = np.max(normresid) y = np.linspace(ymin, ymax, 1000) x = 1 / np.sqrt(2 * np.pi) * np.exp(-y * y / 2.) cax.hist(normresid, bins=np.int(np.sqrt(N)), normed=True, orientation='horizontal') cax.plot(x, y, color="red") cax.set_ylim(ax.get_ylim()) cax.axhline(0, color="black", ls="--") cax.yaxis.set_major_formatter(nullfmt) cax.xaxis.set_major_formatter(nullfmt) if filename is not None: pdf.savefig() plt.close() # Closure Phase if stats["iscp"] == True: table = imageprm["bstable"] if uvunit is None: uvunit = table.uvunit # Get model data model = table.eval_image(imfits=outimage, mask=None, istokes=0, ifreq=0) resid = table.residual_image(imfits=outimage, mask=None, istokes=0, ifreq=0) if filename is not None: util.matplotlibrc(nrows=2, ncols=1, width=600, height=300) else: matplotlib.rcdefaults() fig, axs = plt.subplots(nrows=2, ncols=1, sharex=True) plt.subplots_adjust(hspace=0) ax = axs[0] plt.sca(ax) table.radplot(uvunit=uvunit, uvdtype="ave", color="black", **plotargs) model.radplot(uvunit=uvunit, uvdtype="ave", color="red", errorbar=False, **plotargs) plt.xlabel("") ax = axs[1] plt.sca(ax) resid.radplot(uvunit=uvunit, uvdtype="ave", normerror=True, errorbar=False, color="black", **plotargs) plt.axhline(0, color="black", ls="--") normresid = resid["phase"] / (np.rad2deg( resid["sigma"] / resid["amp"])) N = len(normresid) ymin = np.min(normresid) * 1.1 ymax = np.max(normresid) * 1.1 plt.ylim(ymin, ymax) plt.ylabel("Normalized Residuals") plt.xlabel(r"Baseline Length (%s)" % (unitlabel)) del ymin, ymax divider = make_axes_locatable(ax) # Histgram cax = divider.append_axes("right", size="10%", pad=0.05) ymin, ymax = ax.get_ylim() y = np.linspace(ymin, ymax, 1000) x = 1 / np.sqrt(2 * np.pi) * np.exp(-y * y / 2.) cax.hist(normresid, bins=np.int(np.sqrt(N)), normed=True, orientation='horizontal') cax.plot(x, y, color="red") cax.set_ylim(ax.get_ylim()) cax.axhline(0, color="black", ls="--") cax.yaxis.set_major_formatter(nullfmt) cax.xaxis.set_major_formatter(nullfmt) if filename is not None: pdf.savefig() plt.close() # Close File if filename is not None: pdf.close() else: plt.show() # Reset rcsetting matplotlib.rcdefaults() if isinteractive: plt.ion() matplotlib.use(backend)
def plot_result(img, results, bbox_ML, cts, num, meta, directory, save=False, plot=True): if plot == True: fig, ax = plt.subplots(1, 1, figsize=(10, 10)) ax.imshow(np.amax(img, axis=0), vmax=img.max()) for blob in results: z, x, y, s = blob loci = ax.scatter(y, x, s=20, facecolors='none', edgecolors='y') for coord, val, cell in zip(bbox_ML, cts, num): if val == 0: circles1 = patches.Circle((coord[0] + 30, coord[1] + 30), 30, linewidth=3, edgecolor='r', facecolor='none', alpha=0.2) elif val > 0: circles1 = patches.Circle((coord[0] + 30, coord[1] + 30), 30, linewidth=3, edgecolor='r', facecolor='none') # Add the patch to the Axes ax.add_patch(circles1) ax.text(coord[0] + 15, coord[1], "Cell_{}".format(str(cell)), color='r', weight='bold') ax.text(coord[0] + 15, coord[1] + 35, "{} COSA-1".format(str(val)), color='w', weight='bold') plt.legend([circles1, loci], ["Nucleus found with ML", "FOCI"], loc=0, fontsize='small') if save: try: filename = meta['Name'] + '.pdf' plt.savefig(directory + '/' + filename, transparent=True) except FileNotFoundError: plt.savefig(filename, transparent=True) elif plot == False: plt.ioff() fig, ax = plt.subplots(1, 1, figsize=(10, 10)) ax.imshow(np.amax(img, axis=0), vmax=img.max()) for blob in results: z, x, y, s = blob loci = ax.scatter(y, x, s=10, facecolors='none', edgecolors='y') for coord, val, cell in zip(bbox_ML, cts, num): if val == 0: circles1 = patches.Circle((coord[0] + 30, coord[1] + 30), 30, linewidth=3, edgecolor='r', facecolor='none', alpha=0.2) elif val > 0: circles1 = patches.Circle((coord[0] + 30, coord[1] + 30), 30, linewidth=3, edgecolor='r', facecolor='none') # Add the patch to the Axes ax.add_patch(circles1) ax.text(coord[0] + 15, coord[1], "Cell_{}".format(str(cell)), color='r', weight='bold') ax.text(coord[0] + 15, coord[1] + 35, "{} COSA-1".format(str(val)), color='w', weight='bold') plt.legend([circles1, loci], ["Nucleus found with ML", "FOCI"], loc=0, fontsize='small') if save: try: filename = meta['Name'] + '.pdf' plt.savefig(directory + '/' + filename, transparent=True) except FileNotFoundError: plt.savefig(filename, transparent=True) plt.close(fig)
def show_2D(mag_x, mag_y, mag_z=None, a=15, l=None, w=None, title=None, color=False, hsv=True, origin='upper', save=None, GUI_handle=False, GUI_color_array=None): """ Display a 2D vector arrow plot. Displays an an arrow plot of a vector field, with arrow length scaling with vector magnitude. If color=True, a colormap will be displayed under the arrow plot. If mag_z is included and color=True, a spherical colormap will be used with color corresponding to in-plane and white/black to out-of-plane vector orientation. Args: mag_x (2D array): x-component of magnetization. mag_y (2D array): y-component of magnetization. mag_z (2D array): optional z-component of magnetization. a (int): Number of arrows to plot along the x and y axes. Default 15. l (float): Scale factor of arrows. Larger l -> shorter arrows. Default None guesses at a good value. None uses matplotlib default. w (float): Width scaling of arrows. None uses matplotlib default. title (str): (`optional`) Title for plot. Default None. color (bool): (`optional`) Whether or not to show a colormap underneath the arrow plot. Color image is made from colorwheel.color_im(). hsv (bool): (`optional`) Only relevant if color == True. Whether to use an hsv or 4-fold color-wheel in the color image. origin (str): (`optional`) Control image orientation. save (str): (`optional`) Path to save the figure. GUI_handle (bool): ('optional') Handle for indicating if using GUI. Default is False. GUI_color_array (2D array): ('optional') The colored image array passed from the GUI, it is for creating the overlaying the arrows without using color_im(). Returns: fig: Returns the figure handle. """ a = ((mag_x.shape[0] - 1) // a) + 1 dimy, dimx = mag_x.shape X = np.arange(0, dimx, 1) Y = np.arange(0, dimy, 1) U = mag_x V = mag_y sz_inches = 8 if not GUI_handle or save is not None: if color: rad = mag_x.shape[0] // 16 rad = max(rad, 16) pad = 10 # pixels width = np.shape(mag_y)[1] + 2 * rad + pad aspect = dimy / width else: aspect = dimy / dimx if GUI_handle and save is None: fig, ax = plt.subplots(figsize=(10, 10)) plt.ioff() ax.set_aspect('equal', adjustable='box') else: fig, ax = plt.subplots() ax.set_aspect(aspect) if color: if not GUI_handle or save is not None: from colorwheel import color_im im = ax.matshow(color_im(mag_x, mag_y, mag_z, hsvwheel=hsv, rad=rad), cmap='gray', origin=origin) else: im = ax.matshow(GUI_color_array, cmap='gray', origin=origin, aspect='equal') arrow_color = 'white' plt.axis('off') else: arrow_color = 'black' if GUI_handle and save is None: white_array = np.zeros([dimy, dimx, 3], dtype=np.uint8) white_array.fill(255) im = ax.matshow(white_array, cmap='gray', origin=origin, aspect='equal') plt.axis('off') elif GUI_handle and save: white_array = np.zeros([dimy, dimx, 3], dtype=np.uint8) white_array.fill(255) im = ax.matshow(white_array, cmap='gray', origin=origin) fig.tight_layout(pad=0) ax.xaxis.set_major_locator(mpl.ticker.NullLocator()) ax.yaxis.set_major_locator(mpl.ticker.NullLocator()) plt.axis('off') ashift = (dimx - 1) % a // 2 q = ax.quiver(X[ashift::a], Y[ashift::a], U[ashift::a, ashift::a], V[ashift::a, ashift::a], units='xy', scale=l, scale_units='xy', width=w, angles='xy', pivot='mid', color=arrow_color) if not color: if not GUI_handle: qk = ax.quiverkey(q, X=0.95, Y=0.98, U=1, label=r'$Msat$', labelpos='S', coordinates='axes') qk.text.set_backgroundcolor('w') if origin == 'upper': ax.invert_yaxis() if title is not None: tr = False ax.set_title(title) else: tr = True plt.tick_params(axis='x', labelbottom=False, bottom=False, top=False) plt.tick_params(axis='y', labelleft=False, left=False, right=False) # ax.set_aspect(aspect) if not GUI_handle: plt.show() if save is not None: if not color: tr = False fig.set_size_inches(8, 8 / aspect) print(f'Saving: {save}') plt.axis('off') # sets dpi to 5 times original image dpi so arrows are reasonably sharp dpi2 = max(dimy, dimx) * 5 / sz_inches plt.savefig(save, dpi=dpi2, bbox_inches='tight', transparent=tr) if GUI_handle: return fig, ax else: return
def train(self, datahr=None, datalr=None, epochs=1, batch_size=16, callbacks=None, save_interval=1, bench_idx=None, verbose=False): #datahr = self.datahr if datahr is None else datahr #datalr = self.datalr if datalr is None else datalr running_loss = [] # TODO: This just throws away the remaining expamples if not batch_size not divisor of num_train num_train = len(self.datahr) if batch_size == 1: num_batches = 1 else: num_batches = num_train / batch_size if num_train % batch_size == 0 else num_train / batch_size - 1 num_batches = int(num_batches) # if save_interval > 0: grab and hold a random lr input for benchmarking if save_interval > 0: if bench_idx is None: bench_idx = np.random.randint(1, num_train - 1, 1) else: bench_idx = np.array([ bench_idx, ]) bench_lr = self.datalr[bench_idx, :, :, :] bench_hr = self.datahr[bench_idx, :, :, :] for epoch in range(epochs): # Shuffle the indices TODO: Shuffle batch in place if batch_size != 1: idx = np.random.permutation(list(range(num_train - 1))) # Gives error # ValueError: If your data is in the form of symbolic tensors, # you should specify the `steps` argument #(instead of the `batch_size` argument, #because symbolic tensors are expected to #produce batches of input data). # #datahr = tf.gather(self.datahr,idx) #datalr = tf.gather(self.datalr,idx) #self.datalr = tf.random.shuffle(self.datalr) #self.datahr = tf.random.shuffle(self.datahr) epoch_start_time = datetime.datetime.now() # Grab batch_size images from training data both lr and hr if batch_size == 1: batches = 1 else: batches = int(num_batches / 2) for batch_idx in range(batches): # Take 2 batches per round # generate fake hr images with generator.predict(lr_imgs) size of batch #batch_lr = datalr[bix_begin:bix_end,:,:,:] #batch_hr = datahr[bix_begin:bix_end,:,:,:] if batch_size == 1: batch_lr = self.datalr batch_hr = self.datahr else: bix_begin = batch_idx * batch_size bix_end = bix_begin + batch_size ti = datetime.datetime.now() if verbose: print('Start Shuffling data...') batch_lr = np.array([ self.datalr[i, :, :, :] for i in idx[bix_begin:bix_end] ]) batch_hr = np.array([ self.datahr[i, :, :, :] for i in idx[bix_begin:bix_end] ]) if verbose: print('Done shuffling. Time: {0}'.format( datetime.datetime.now() - ti)) if verbose: print('Making Predictions...') ti = datetime.datetime.now() print(batch_lr.shape) x_gen = self.gen.predict(batch_lr) if verbose: print('Done predicting. Time: {0}'.format( datetime.datetime.now() - ti)) x_tr = batch_hr #x_tr = np.concatenate((batch_hr,x_gen)) # TODO: Randomly flip some of these a la https://github.com/utkd/gans/blob/master/cifar10dcgan.ipynb noise_prop = 0.05 # Randomly flip 5% of labels # Prepare labels for real data y_tr = np.zeros( (len(x_gen), ) + (4, 4, 1)) + np.random.uniform( low=0.0, high=0.1, size=((len(x_gen), ) + (4, 4, 1))) flipped_idx = np.random.choice(np.arange(len(y_tr)), size=int(noise_prop * len(y_tr))) y_tr[flipped_idx] = 1 - y_tr[flipped_idx] # Prepare labels for fake data y_gen = np.ones( (len(x_gen), ) + (4, 4, 1)) - np.random.uniform( low=0.0, high=0.1, size=((len(x_gen), ) + (4, 4, 1))) flipped_idx = np.random.choice(np.arange(len(y_gen)), size=int(noise_prop * len(y_gen))) if verbose: print('Training Discriminator...') ti = datetime.datetime.now() # Train the discriminator alone d_loss_hr = self.disc_model.train_on_batch(x_tr, y_tr) d_loss_gen = self.disc_model.train_on_batch(x_gen, y_gen) if verbose: print('Done training. Time: {0}'.format( datetime.datetime.now() - ti)) # Grab another batch_size of images just for end to end training for adversarial model self.adv_model if batch_size != 1: bix_begin = bix_end bix_end = bix_begin + batch_size #batch_lr = datalr[bix_begin:bix_end,:,:,:] batch_lr = np.array([ self.datalr[i, :, :, :] for i in idx[bix_begin:bix_end] ]) y_tr = np.ones((batch_size, ) + (4, 4, 1)) x_tr = batch_lr #print('GAN Train Size: {0}'.format(x_tr.shape)) if verbose: print('Training GAN...') ti = datetime.datetime.now() a_loss = self.adv_model.train_on_batch(x_tr, y_tr) if verbose: print('Done training GAN. Time: {0}'.format( datetime.datetime.now() - ti)) log_mesg = "%d:%d: [D loss_hr: %f, acc_hr: %f, loss_gen: %f, acc_gen: %f]" % \ (epoch, batch_idx, d_loss_hr[0], d_loss_hr[1], d_loss_gen[0], d_loss_gen[1]) log_mesg = "%s [A loss: %f, acc: %f]" % (log_mesg, a_loss[0], a_loss[1]) # Print loss, call callbacks, save benchmarks if interval, etc... #print(log_mesg) running_loss.append([epoch] + [batch_idx] + list(d_loss_hr) + list(d_loss_gen) + list(a_loss)) np.save(self.dir_pfx + 'loss_logging/loss_log.npy', arr=np.array(running_loss)) #if batch_idx % save_interval: print('Finished Epoch {0}... Time: {1}'.format( epoch, datetime.datetime.now() - epoch_start_time)) print(log_mesg) # If save, save a pic if save_interval > 0: img = self.gen.predict(bench_lr).squeeze() img = (img + 1) / 2 plt.ioff() plt.figure().suptitle( 'HR + Prediction: Epoch {0}'.format(epoch), fontsize=20) plt.subplot(1, 2, 1) output_hr = (bench_hr.squeeze() + 1) / 2 plt.imshow(output_hr) plt.subplot(1, 2, 2) plt.imshow(img) plt.savefig('{0}images/bench_epoch_{1}'.format( self.dir_pfx, epoch)) if epoch % 10 == 0: print('Epoch: {0}'.format(epoch)) self.gen.save(self.dir_pfx + 'weights/generator_weights.h5') self.disc.save(self.dir_pfx + 'weights/discriminator_weights.h5')
def main(unused_argv): data_dir = "ycb-data-cropped" class_to_name = {} for object_dir in os.listdir(data_dir): if not object_dir.startswith("."): class_to_name[int(object_dir[:3]) - 1] = object_dir classes = list(class_to_name.keys()) classes.sort() classifier = tf.estimator.Estimator(model_fn=cnn_ycb_v1.cnn_model_fn, model_dir="every_image_model_v1") cap = cv2.VideoCapture(0) while True: ret, image = cap.read() if ret: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = image.astype('float32') # image = cv2.normalize(image, None, 0.0, 1.0, cv2.NORM_MINMAX) image_resized = cv2.resize(image, (28, 28), interpolation=cv2.INTER_AREA) predict_input_fn = tf.estimator.inputs.numpy_input_fn( x={"x": np.expand_dims(image_resized.flatten(), axis=0)}, num_epochs=1, shuffle=False) predictions = list(classifier.predict(input_fn=predict_input_fn)) template = '{} ({:.1f}%)' for prediction in predictions: class_id = prediction["classes"] probability = prediction["probabilities"][class_id] image_to_display = image_resized.astype('uint8') image_to_display = cv2.cvtColor(image_to_display, cv2.COLOR_RGB2BGR) image_to_display = cv2.resize(image_to_display, (500, 500)) plt.ion() # plt.hist(class_to_name.keys(), 10, weights=prediction["probabilities"]) plt.bar(x=[x + 1 for x in classes], height=prediction["probabilities"]) names = list(class_to_name.values()) names.sort() plt.text(x=8, y=0.2, s="\n".join(names)) plt.xticks([x + 1 for x in classes]) plt.draw() plt.ioff() plt.cla() cv2.putText( image_to_display, template.format(class_to_name[class_id], 100 * probability), (10, 490), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) cv2.imshow('image', image_to_display) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def plot_histograms(datavectors, plotname='./tdose_cubehist.pdf', colors=None, labels=None, bins=None, xrange=None, yrange=None, verbose=True, norm=True, ylog=True): """ Plot histograms of a set of data vectors. --- INPUT --- datavectors Set of data vectors to plot histograms of plotname Name of plot to generate colors Colors to use for histograms labels Labels for the data vectors bins Bins to use for histograms. Can be generated with np.arange(minval,maxval+binwidth,binwidth) xrange Xrange of plot yrange Yrange of plot verbose Toggle verbosity norm Noramlize the histograms ylog Use a logarithmic y-axes when plotting """ Ndat = len(datavectors) if verbose: print ' - Plotting histograms of N = ' + str(Ndat) + ' data vectors' if colors is None: colors = ['blue'] * Ndat if labels is None: labels = ['data vector no. ' + str(ii + 1) for ii in np.arange(Ndat)] if bins is None: bins = np.arange(-100, 102, 2) fig = plt.figure(figsize=(10, 3)) fig.subplots_adjust(wspace=0.1, hspace=0.1, left=0.08, right=0.81, bottom=0.1, top=0.95) Fsize = 10 lthick = 1 plt.rc('text', usetex=True) plt.rc('font', family='serif', size=Fsize) plt.rc('xtick', labelsize=Fsize) plt.rc('ytick', labelsize=Fsize) plt.clf() plt.ioff() #plt.title(plotname.split('TDOSE 1D spectra'),fontsize=Fsize) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for dd, datavec in enumerate(datavectors): hist = plt.hist(datavec[~np.isnan(datavec)], color=colors[dd], bins=bins, histtype="step", lw=lthick, label=labels[dd], normed=norm) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if yrange is None: yvals = [1e-5, 1e8] else: yvals = yrange plt.plot([0, 0], yvals, '--k', lw=lthick) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - plt.xlabel('', fontsize=Fsize) plt.ylabel('\#', fontsize=Fsize) if yrange is not None: plt.ylim(yrange) if xrange is not None: plt.xlim(xrange) if ylog: plt.yscale('log') leg = plt.legend(fancybox=True, loc='upper right', prop={'size': Fsize}, ncol=1, numpoints=1, bbox_to_anchor=(1.25, 1.03)) # add the legend leg.get_frame().set_alpha(0.7) if verbose: print ' Saving plot to', plotname plt.savefig(plotname) plt.clf() plt.close('all') # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def close(self): plt.ioff() plt.close()
for i in range(1, n + 1): fx_expanded_form = derivate(fx, i).evalf(subs={x: 0}) * (x ** i) / np.math.factorial(i) item_first = item_first + fx_expanded_form return item_first x = sp.Symbol('x') fx = sp.sin(x) for j in range(1, 51): plt.clf() # 在画板上清除图像 y = fx_expanded_form_N(fx, j) # 第j阶的Taloy展开式 print(j, y) xn = np.arange(-20, 20, 0.1) yn = [] for i in xn: yn.append(y.evalf(subs={x: i})) # 得到j阶的Taloy展开式中y的列表 plt.ion() # 开启交互模式,这样每个绘图命令之后添加自动图形更新 '''设置x的刻度值''' x_major_locator = MultipleLocator(1) ax = plt.gca() ax.xaxis.set_major_locator(x_major_locator) plt.xlabel("x") plt.ylabel("sin(x)") plt.ylim(-2, 2) # y轴的范围 plt.grid() # 在背景中加上方格 plt.plot(xn, yn) plt.pause(0.1) # 当前图像悬停一会 plt.ioff() # 要关闭交互模式,否则图像会一闪而过 plt.show()
channels = CHANNELS, rate = RATE, input = True, output = True) # Read microphone, plot audio signal for i in range(K): # Read audio input stream input_bytes = stream.read(BLOCKLEN) # In case of run-time errors, try using instead: # input_bytes = stream.read(BLOCKLEN, exception_on_overflow = False) signal_block = struct.unpack('h' * BLOCKLEN, input_bytes) # Convert g1.set_ydata(input_bytes) # Update y-data of plot g2.set_ydata(input_bytes) pyplot.pause(0.0001) stream.stop_stream() stream.close() p.terminate() pyplot.ioff() # Turn off interactive mode pyplot.show() # Keep plot showing at end of program pyplot.close() print('* Finished')