def plotResults(results): plt.interactive(True) plt.subplot(131) plt.plot(results.xpos, color='b',label='X Position') plt.plot(results.ypos, linestyle='--', color='r', label='Y Position') plt.plot(results.zpos, linestyle='-', color='y', label='Z Position') plt.xlabel('Time') plt.ylabel('Distance') plt.title('Distance Traveled') plt.legend() plt.show() ts = range(0,len(results),6) plt.subplot(131) pt = plt.plot(0,results.zpos[0], 'ro', markersize=4) for t in ts: plt.subplot(131) pt[0].set_ydata(results.zpos[t]) pt[0].set_xdata(t) ax1 = plt.subplot(132) ax1.clear() plt.bar(range(6), results[t].lowerleglinearmag) ax2 = plt.subplot(133) ax2.clear() plt.bar(range(6), results[t].upperleglinearmag) plt.draw() time.sleep(0.01)
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 lvmTwoDPlot(X, lbl=None, symbol=None): """Helper function for plotting the labels in 2-D. Description: lvmTwoDPlot(X, lbl, symbol) helper function for plotting an embedding in 2-D with symbols. Arguments: X - the data to plot. lbl - the labels of the data point. symbol - the symbols to use for the different labels. See also lvmScatterPlot, lvmVisualise Copyright (c) 2004, 2005, 2006, 2008, 2009 Neil D. Lawrence """ if lbl=='connect': connect = True lbl = None else: connect = False if symbol is None: if lbl is None: symbol = ndlutil.getSymbols(1) else: symbol = ndlutil.getSymbols(lbl.shape[1]) axisHand = pp.gca() returnVal = [] holdState = axisHand.ishold() intState = pp.isinteractive() pp.interactive(False) for i in range(X.shape[0]): if i == 1: axisHand.hold(True) if lbl is not None: labelNo = np.flatnonzero(lbl[i]) else: labelNo = 0 try: returnVal.append(axisHand.plot([X[i, 0]], [X[i, 1]], symbol[labelNo], markersize=10, linewidth=2)) if connect: if i>0: axisHand.plot([X[i-1, 0], X[i, 0]], [X[i-1, 1], X[i, 1]], 'r') except(NotImplementedError): raise NotImplementedError('Only '+ str(len(symbol)) + ' labels supported (it''s easy to add more!)') axisHand.hold(holdState) if intState: pp.show() pp.interactive(intState) return returnVal
def plot_gamma_1_storage(): plt.figure() ts=get_mismatch(1.0) dummy,storage_capacity,storage_level=get_policy_2_storage(ts,return_storage_filling_time_series=True) plt.plot(storage_level) plt.interactive(1) plt.show()
def plot_path2d(data1, data2, data3, data4): x1=[x for [x, y, z] in data1] y1=[y for [x, y, z] in data1] z1=[z for [x, y, z] in data1] x2=[x for [x, y, z] in data2] y2=[y for [x, y, z] in data2] z2=[z for [x, y, z] in data2] mx1=[x for [x, y, z] in data3] my1=[y for [x, y, z] in data3] mz1=[z for [x, y, z] in data3] mx2=[x for [x, y, z] in data4] my2=[y for [x, y, z] in data4] mz2=[z for [x, y, z] in data4] pltxy=plt.plot(x1,y1, 'ro-',label='xy') pltuv=plt.plot(x2,y2, 'bs-',label='uv') mpltxy=plt.plot(mx1,my1, 'go-',label='cut_xy') mpltuv=plt.plot(mx2,my2, 'ks-',label='cut_uv') plt.legend() plt.axis('equal') plt.axis([min([min(x1),min(x2),min(mx1),min(mx2)]),\ max([max(x1),max(x2),max(mx1),max(mx2)]),\ min([min(y1),min(y2),min(my1),min(my2)]),\ max([max(y1),max(y2),max(my1),max(my2)])]) plt.grid(True) plt.interactive(True) plt.show(block=False) # plt.show() plt.hold(True)
def cosp_plot_column_2D(fnc, varname='equivalent_reflectivity_factor', level=0, column = 0, time = 0): """ Function that plots one column of lat/lon data. """ plt.interactive(True) fig=plt.figure() ax = fig.add_subplot(111) # Read cube z=iris.load(fnc) z=z[0] # Get coords c = z.coord('column') t = z.coord('time') # Select time and column y=z.extract(iris.Constraint(column=c.points[column])) y=y.extract(iris.Constraint(time=t.points[time])) # Select level. Not managed to make constrain with 'atmospheric model level' y=y[level] color_map = mpl_cm.get_cmap('Paired') qplt.pcolormesh(y,cmap=color_map,vmin=-20,vmax=20) plt.gca().coastlines() return
def plot_data_dict(data_dict, plots = None, mode = 'static', hang = True, figure = None, size = None, **plot_preference_kwargs): """ Make a plot of data in the format defined in data_dict :param data_dict: dict<str: plottable_data> :param plots: Optionally, a dict of <key: IPlot> identifying the plot objects to use (keys should be the same as those in data_dict). :return: The plots (same ones you provided if you provided them) """ assert mode in ('live', 'static') if isinstance(data_dict, list): assert all(len(d) == 2 for d in data_dict), "You can provide data as a list of 2 tuples of (plot_name, plot_data)" data_dict = OrderedDict(data_dict) if plots is None: plots = {k: get_plot_from_data(v, mode = mode, **plot_preference_kwargs) for k, v in data_dict.items()} if figure is None: if size is not None: from pylab import rcParams rcParams['figure.figsize'] = size figure = plt.figure() n_rows, n_cols = vector_length_to_tile_dims(len(data_dict)) for i, (k, v) in enumerate(data_dict.items()): plt.subplot(n_rows, n_cols, i + 1) plots[k].update(v) plots[k].plot() plt.title(k, fontdict = {'fontsize': 8}) oldhang = plt.isinteractive() plt.interactive(not hang) plt.show() plt.interactive(oldhang) return figure, plots
def plot_paths(results, which_to_label=None): import matplotlib import matplotlib.pyplot as plt plt.clf() interactive_state = plt.isinteractive() xvalues = -np.log(results.lambdas[1:]) for index, path in enumerate(results.coefficients): if which_to_label and results.indices[index] in which_to_label: if which_to_label[results.indices[index]] is None: label = "$x_{%d}$" % results.indices[index] else: label = which_to_label[results.indices[index]] else: label = None if which_to_label and label is None: plt.plot(xvalues, path[1:], ':') else: plt.plot(xvalues, path[1:], label=label) plt.xlim(np.amin(xvalues), np.amax(xvalues)) if which_to_label is not None: plt.legend(loc='upper left') plt.title('Regularization paths ($\\rho$ = %.2f)' % results.balance) plt.xlabel('$-\log(\lambda)$') plt.ylabel('Value of regression coefficient $\hat{\\beta}_i$') plt.show() plt.interactive(interactive_state)
def get_windows(image): """Display the given image and record user selected points. Parameters ---------- image : M,N,3 ndarray The image to be displayed. Returns ------- array : n_points,2 An array of coordinates in the image. Each row corresponds to the x, y coordinates of one point. If an odd number of points are specified, the last one will be discarded. """ plt.interactive(True) plt.imshow(image) plt.show() crop = plt.ginput(0) plt.close() plt.interactive(False) # remove last point if an odd number selected crop = crop[:-1] if np.mod(len(crop), 2) else crop return np.vstack(crop).astype('int')[:, [1, 0]]
def createHistogramOfOrientedGradientFeatures(sourceImage, numOrientations, pixelsPerCell): # Returns an nxd matrix, n pixels and d the HOG vector length. # H is a matrix NBLOCKS_Y x NBLOCKS_X x CPB_Y x CPB_X x ORIENTATIONS # Here CPB == 1 H,Himg = myhog.hog( sourceImage, numOrientations, pixelsPerCell, cells_per_block=(1,1), flatten=False, visualise=True ) hog_image_rescaled = skimage.exposure.rescale_intensity( Himg )#, in_range=(0, 0.2)) plt.interactive(True) plt.figure() plt.subplot(1,2,1) plt.imshow(sourceImage) plt.subplot(1,2,2) plt.imshow( hog_image_rescaled, cmap=plt.cm.gray ) plt.title('HOG') plt.waitforbuttonpress() # Reduce to non-singleton dimensions, BY x BX x ORIENT H = H.squeeze() assert H.ndim == 3 assert H.max() <= 1.0 # resize to image pixels rather than grid blocks hogImg = np.zeros( ( sourceImage.shape[0], sourceImage.shape[1], numOrientations ), dtype=float ) for o in range(numOrientations): hogPerOrient = H[:,:,o].astype(np.float32) hpoAsPil = pil.fromarray( hogPerOrient, mode='F' ) hogImg[:,:,o] = np.array( hpoAsPil.resize( (sourceImage.shape[1], sourceImage.shape[0]), pil.NEAREST ) ) return hogImg.reshape( ( sourceImage.shape[0]*sourceImage.shape[1], numOrientations ) )
def train_agent(mdp, agent, max_episodes, epsilon_decay=0.9, plot=False): ''' Trains an agent on the given MDP for the specified number of episodes. :param mdp: The mdp which implements the domain :param agent: The RL agent to train :param max_episodes: The maximum number of episodes to run :param epsilon_decay: The per-episode decay rate of the epsilon parameter :param plot: If true, plot the reward results online. ''' episode_rewards = [] for i in range(max_episodes): episode_rewards.append(run_episode(mdp, agent, kbd_ctl=False)) if i % 1 == 0: agent.epsilon *= epsilon_decay if plot: plt.interactive(True) plt.clf() plt.ylabel('Reward per episodes') plt.xlabel('Episodes') plt.plot(episode_rewards) plt.draw() print "[episode %d] episode reward: %f. Epsilon now: %f" %\ (i, episode_rewards[-1], agent.epsilon) return episode_rewards
def main(argv=None): # Permit interactive use if argv is None: argv = sys.argv # Parse and check incoming command line arguments outsuffix = None try: try: opts, args = getopt.getopt(argv[1:], "h", ["help"]) except getopt.error as msg: raise Usage(msg) for o, a in opts: if o in ("-h", "--help"): print(__doc__) return 0 elif o == "-o": outsuffix = a except Usage as err: print(err.msg, file=sys.stderr) return 2 # Push interactive mode off (in case we get used from IPython) was_interactive = plt.isinteractive() plt.interactive(False) # If not saving, then display. if not outsuffix: plt.show() # Pop interactive mode plt.interactive(was_interactive)
def draw_2D_slice_interactive(self, p_vals, x_variable, y_variable, range_x, range_y, slider_ranges, **kwargs): previous = plt.isinteractive() plt.ioff() number_of_sliders = len(slider_ranges) slider_block = 0.03*number_of_sliders fig = plt.figure() plt.clf() ax = plt.axes([0.1, 0.2+slider_block, 0.8, 0.7-slider_block]) c_axs = list() cdict = dict() if 'color_dict' in kwargs: cdict.update(kwargs['color_dict']) j = 0 sliders = dict() for i in slider_ranges: slider_ax = plt.axes([0.1, 0.1+j*0.03, 0.8, 0.02]) slider = Slider(slider_ax, i, log10(slider_ranges[i][0]), log10(slider_ranges[i][1]), valinit=log10(p_vals[i]), color='#AAAAAA' ) j += 1 sliders[i] = slider update = SliderCallback(self, sliders, c_axs, cdict, ax, p_vals, x_variable, y_variable, range_x, range_y, **kwargs) update(1) for i in sliders: sliders[i].on_changed(update) plt.show() plt.interactive(previous)
def __init__(self, client_pars=None, plot_template=None, interactive=True, **kwargs): self.client=Client(client_pars) self.connect() # initialize data containers self.pr=u.Param() self.ob=u.Param() self.err=[] self.ferr=None # initialize the plotter from matplotlib import pyplot self.interactive = interactive self.pp = pyplot pyplot.interactive(interactive) #self.template_default = default_template self.templates = templates #self.template = u.Param() self.p = u.Param(DEFAULT) #self.update_plot_layout(plot_template=plot_template) # save as 'cmd': tuple(ticket,buffer,key) # will call get cmds with 'cmd', save the ticket in <ticket> and # save the resulting data in buffer[key] self.cmd_dct = {} self.server_dcts={}
def generate_single_funnel_test_data( excitation_angles, emission_angles, \ md_ex=0, md_fu=1, \ phase_ex=0, phase_fu=0, \ gr=1.0, et=1.0 ): ex, em = np.meshgrid( excitation_angles, emission_angles ) alpha = 0.5 * np.arccos( .5*(((gr+2)*md_ex)-gr) ) ph_ii_minus = phase_ex - alpha ph_ii_plus = phase_ex + alpha print ph_ii_minus print ph_ii_plus Fnoet = np.cos( ex-ph_ii_minus )**2 * np.cos( em-ph_ii_minus )**2 Fnoet += gr*np.cos( ex-phase_ex )**2 * np.cos( em-phase_ex )**2 Fnoet += np.cos( ex-ph_ii_plus )**2 * np.cos( em-ph_ii_plus )**2 Fnoet /= (2+gr) Fet = .25 * (1+md_ex*np.cos(2*(ex-phase_ex))) \ * (1+md_fu*np.cos(2*(em-phase_fu-phase_ex))) Fem = et*Fet + (1-et)*Fnoet import matplotlib.pyplot as plt plt.interactive(True) plt.matshow( Fem, origin='bottom' ) plt.colorbar()
def interactive(b): b_prev = plt.isinteractive() plt.interactive(b) try: yield finally: plt.interactive(b_prev)
def plot_patches(patches, fignum=None, low=0, high=0): """ Given a stack of 2D patches indexed by the first dimension, plot the patches in subplots. 'low' and 'high' are optional arguments to control which patches actually get plotted. 'fignum' chooses the figure to plot in. """ try: istate = plt.isinteractive() plt.ioff() if fignum is None: fig = plt.gcf() else: fig = plt.figure(fignum) if high == 0: high = len(patches) pmin, pmax = patches.min(), patches.max() dims = np.ceil(np.sqrt(high - low)) for idx in xrange(high - low): spl = plt.subplot(dims, dims, idx + 1) ax = plt.axis('off') im = plt.imshow(patches[idx], cmap=matplotlib.cm.gray) cl = plt.clim(pmin, pmax) plt.show() finally: plt.interactive(istate)
def show_result(lr, title): fig, axarr = plt.subplots(2, 1) fig.suptitle(title, fontsize=16) # Draw line num_samples = 1000 x = lr.features[:, :-1] min_val_x, max_val_x = np.floor(np.amin(x)), np.ceil(np.amax(x)) x_continue = np.linspace(min_val_x, max_val_x, num_samples).reshape(num_samples, 1) features = np.hstack((x_continue, np.ones((num_samples, 1)))) y_continue = lr.hypothesise(features) axarr[0].set_title("Draw line.") axarr[0].plot(lr.features[:, :-1], lr.labels, 'bo') axarr[0].plot(x_continue, y_continue, 'r-') axarr[0].axis([min_val_x, max_val_x, np.amin(y_continue), np.amax(y_continue)]) # History of the Cost num_iters = len(lr.loss_history) axarr[1].set_title("History of the Cost") axarr[1].plot(np.linspace(1, num_iters, num_iters), lr.loss_history, 'b-') axarr[1].axis([0, num_iters, 0, np.max(lr.loss_history)]) plt.interactive(False) plt.show(block=True) plt.show()
def __init__(self, i): #TO-DO: work the i argument plt.interactive(True) self._figure = plt.figure() ax = f.add_subplot() #<- TO-DO: depending on intended axes and geometries '''
def main(): plt.interactive(False) #analyseSimpleBatchGradientDescent() #analyseSimpleBatchGradientDescentRegularization() #analyseSimpleBatchGradientDescentMNIST() analyseBatchVersusMiniBatch() #analyseOneVersusAll() return
def rotating_sequence(show_frames=True, save_frames=True, save_ani=False, show_ani=False, ani_path='./puffer.mp4', frames_basename='./puffer_frame_', airtemp_cubes=None, precip_cubes=None, n_steps_round=20, tilt_angle=21.7 ): plt.interactive(show_frames) figure = make_puffersphere_figure() # per_image_artists = [] for (i_plt, lon_rotate) in enumerate(np.linspace(0.0, 360.0, n_steps_round, endpoint=False)): print 'rotate=', lon_rotate,' ...' axes = make_puffersphere_axes( projection_kwargs={'central_longitude': lon_rotate, 'central_latitude': tilt_angle}) image = axes.stock_img() coast = axes.coastlines() # data = istk.global_pp() data = airtemp_cubes[i_plt] transparent_blue = (0.0, 0.0, 1.0, 0.25) transparent_red = (1.0, 0.0, 0.0, 0.25) cold_thresh = -10.0 cold_fill = iplt.contourf( data, levels=[cold_thresh, cold_thresh], colors=[transparent_blue], extend='min') cold_contour = iplt.contour( data, levels=[cold_thresh], colors=['blue'], linestyles=['solid']) data = precip_cubes[i_plt] precip_thresh = 0.0001 precip_fill = iplt.contourf( data, levels=[precip_thresh, precip_thresh], colors=[transparent_red], extend='max') precip_contour = iplt.contour( data, levels=[precip_thresh], colors=['red'], linestyles=['solid']) gridlines = draw_gridlines(n_meridians=6) # artists = [] # artists += [coast] # artists += [image] # artists += cold_fill.collections # artists += precip_fill.collections # for gridline in gridlines: # artists += gridline if show_frames: plt.draw() if save_frames: save_path = frames_basename+str(i_plt)+'.png' save_figure_for_puffersphere(figure, save_path) # per_image_artists.append(artists) print ' ..done.'
def plot(self): plt.interactive(True) min_stamp = None max_stamp = None no_valid_data = True for abag in self.bag_file: with rosbag.Bag(abag) as bag: info = yaml.load(bag._get_yaml_info()) message_num = sum([topic["messages"] for topic in info["topics"] if topic["topic"] in self.all_topics]) widgets = [Fore.GREEN + "%s: " % (abag) + Fore.RESET, Percentage(), Bar()] pbar = ProgressBar(maxval=message_num, widgets=widgets).start() counter = 0 for topic, msg, timestamp in bag.read_messages(topics=self.all_topics): pbar.update(counter) for topic_data in self.topic_data: topic_data.addValue(topic, msg) no_valid_data = False if min_stamp: if min_stamp > msg.header.stamp: min_stamp = msg.header.stamp else: min_stamp = msg.header.stamp if max_stamp: if max_stamp < msg.header.stamp: max_stamp = msg.header.stamp else: max_stamp = msg.header.stamp counter = counter + 1 pbar.finish() if no_valid_data: print Fore.RED + "Cannot find valid data in bag files, valid topics are:\n%s" % ", ".join(self.all_topics) + Fore.RESET return title = ("""Plot from [%s] to [%s] (%d secs)""" % (str(time.ctime(min_stamp.to_sec())), str(time.ctime(max_stamp.to_sec())), (max_stamp - min_stamp).to_sec())) start_time = rospy.Duration(self.start_time) + min_stamp if self.duration: end_time = start_time + rospy.Duration(self.duration) else: end_time = max_stamp for topic_data in self.topic_data: topic_data.filter(start_time, end_time) fig = plt.figure(facecolor="1.0") self.fig = fig fig.suptitle(title) self.show_legend = True fig.canvas.mpl_connect('key_press_event', self.keyPress) # Compute layout self.start_time = start_time self.plotAll(fig, start_time, self.show_legend) self.printUsage() self.runp = True while self.runp: plt.pause(1)
def display(self, covariances=None, logalpha=None, name=None, figurenum=1): """Display covariances and alphas with matplotlib.""" if PLOTTING_AVAILABLE: try: if covariances == None: covariances = self._covariances if logalpha == None: logalpha = self._logalpha if name == None: name = "mixture" # Save state of interactive mode and turn it off. istate = pyplot.isinteractive() pyplot.ioff() # Create figure instance. pyplot.figure(figurenum) pyplot.clf() # Calculate necessary subplot dimensions for a roughly square rows = np.floor(np.sqrt(self._ncomponent())) cols = np.ceil(np.sqrt(self._ncomponent())) # Add one more row if we have a perfect square, for the # preferences bar chart while rows * cols <= self._ncomponent(): rows = rows + 1 for clust in xrange(self._ncomponent()): pyplot.subplot(rows, cols, clust + 1) self._display_covariance_matrix(clust) title = "Covariance for %s component %d" % (name, clust + 1) pyplot.title(title, fontsize="small") pyplot.colorbar() # Plot a bar graph of the mixing proportions in an extra subplot pyplot.subplot(rows, cols, self._ncomponent() + 1) # Do the bar graph and make it look reasonable pyplot.bar( bottom=np.zeros(self._ncomponent()), left=np.arange(1, self._ncomponent() + 1) - 0.5, height=np.exp(self._logalpha), width=1, ) # xticks for each bar in the bar graph. pyplot.xticks(np.arange(1, self._ncomponent() + 1)) pyplot.title("Mixing proportions") # Display figure. pyplot.show() finally: # Restore previous interactivity state. pyplot.interactive(istate)
def draw_plot(self, x_label, y_label, x_data, y_data): x_data.sort() y_data.sort() import matplotlib.pyplot as plt plt.xlabel(x_label) plt.ylabel(y_label) title = '{0} {1} RELATIONSHIP'.format(x_label, y_label) plt.title(title) plt.interactive(False) plt.plot(x_data, y_data, label="") plt.show()
def plot_em(T): problem=r'2b: Plot lnp' w=W(T) lnp=[probit(w,t,Xtrain, ytrain) for t in range(T)] x=[t+1 for t in range(T)] plt.plot(x, lnp) plt.title(problem) plt.xlabel(r't') plt.ylabel(r'ln[p(w_t, y | X)]') plt.interactive(True) plt.savefig(problem.replace(':','').replace(' ', '_'))
def plot_map2d(k, coordinates, unit_statistic_homogeneous_significant, vmin=None, vmax=None): """Plots 2d sensor (unit) map related to simulate_2d() data. """ map2d = np.zeros((k, k)) for i, (x, y) in enumerate(coordinates): map2d[x, y] = unit_statistic_homogeneous_significant[i] plt.interactive(True) plt.figure() plt.imshow(map2d, interpolation='nearest', vmin=vmin, vmax=vmax) plt.colorbar()
def plot_data(dist, fset, dset, qset): plt.interactive(False) plt.rcParams['font.size'] = 17.0 dist_params = pdata.dist_map[dist] ext = dist_params["ext"] typ = dset["type"] emp_key = "emp_" + typ emp_file = fset[emp_key] dist_key = ext + "_" + typ dist_file = fset[dist_key] p1 = read_data(emp_file) p2 = read_data(dist_file) xlabel = dset["xlabel"] ylabel = dset["ylabel"] l1 = dset["legend1"] l2 = dset["legend2"].replace(pdata.dist_replace_string, dist_params["legend"]) loc = dset["loc"] if ( typ == "cdf" ): plt.plot(p1[:,0], p1[:,1], 'k-', label=l1) plt.plot(p2[:,0], p2[:,1], 'k--', label=l2) else: plt.loglog(p1[:,0], p1[:,1], 'k-', label=l1) plt.loglog(p2[:,0], p2[:,1], 'k--', label=l2) if "xlim" in dset: plt.xlim(dset["xlim"]) if "ylim" in dset: plt.ylim(dset["ylim"]) if "xticks" in dset: plt.xticks(dset["xticks"]) if "yticks" in dset: plt.yticks(dset["yticks"]) plt.grid() plt.xlabel(xlabel) plt.ylabel(ylabel) plt.legend(loc=loc, frameon=False) dir = qset["dir"] fname = qset["file"] eps_file = dset["tag"].lower() + "_" + typ + "_" + ext + ".eps" plot_dir = os.path.join(dir, pdata.plot_dir) image_file = os.path.join(plot_dir, eps_file) prepare_file(image_file) plt.savefig(image_file) print "\t Created plot", image_file plt.close()
def plot_uncertainty_bounds_s(self, multiplier =200, *args, **kwargs): ''' Plots complex uncertainty bounds plot on smith chart. This function plots the complex uncertainty of a NetworkSet as circles on the smith chart. At each frequency a circle with radii proportional to the complex standard deviation of the set at that frequency is drawn. Due to the fact that the `markersize` argument is in pixels, the radii can scaled by the input argument `multiplier`. default kwargs are { 'marker':'o', 'color':'b', 'mew':0, 'ls':'', 'alpha':.1, 'label':None, } Parameters ------------- multipliter : float controls the circle sizes, by multiples of the standard deviation. ''' default_kwargs = { 'marker':'o', 'color':'b', 'mew':0, 'ls':'', 'alpha':.1, 'label':None, } default_kwargs.update(**kwargs) if plb.isinteractive(): was_interactive = True plb.interactive(0) else: was_interactive = False [self.mean_s[k].plot_s_smith(*args, ms = self.std_s[k].s_mag*multiplier, **default_kwargs) for k in range(len(self[0]))] if was_interactive: plb.interactive(1) plb.draw() plb.show()
def compare_sarsa_qlearning(render): ''' The top-level method for running RL experiments. Performs runs on the two defined agents, and plots the results. :param render: If true, render while training ''' sarsa_rewards = run_experiment(SARSAAgent, render) qlearning_rewards = run_experiment(QLearningAgent, render) plt.interactive(False) plot_sarsa_vs_qlearning(sarsa_rewards, qlearning_rewards)
def main(): plt.interactive(False) iris = sklearn.datasets.load_iris() data = iris['data'] labels = iris['target'] pca = sklearn.decomposition.PCA(2) data_pca = pca.fit_transform(data) estimated_labels, centers = kmeans(data_pca, 3, 0.001) plot_groups(data_pca, estimated_labels, {0: 'o', 1: 's', 2: '^'}, figsize=(4, 4)) plt.show()
def density_plot(x): p = sns.kdeplot(x) p = plt.show(block=True) p = plt.interactive(False) return p
# Run redmonster on a cluster using multiprocessesing # # Tim Hutchinson, University of Utah, August 2014 # [email protected] from time import gmtime, strftime import numpy as n import matplotlib.pyplot as p p.interactive(True) import multiprocessing as mp from astropy.io import fits from redmonster.sandbox import yanny as y from redmonster.datamgr import spec, io from redmonster.physics import zfinder, zfitter, zpicker def parallel_rm(xxx_todo_changeme): (plate, mjd, fiberid) = xxx_todo_changeme specs = spec.Spec(plate=plate, mjd=mjd, fiberid=fiberid) zssp = zfinder.ZFinder(fname='ndArch-ssp_em_galaxy-v000.fits', npoly=4, zmin=-0.01, zmax=1.2) zssp.zchi2(specs.flux, specs.loglambda, specs.ivar) # Write chi2 file with zbase prihdu = fits.PrimaryHDU(zssp.zchi2arr) col1 = fits.Column(name='ZBASE', format='E', array=zssp.zbase) cols = fits.ColDefs([col1])
def show_model_output(model, dataset, indices, num_rows): plt.interactive(False) num_cols = 3 plt.rcParams.update({'font.size': 8}) for i_sample, index in enumerate(indices): sample = dataset.__getitem__((index, True)) x, y, question, _, full_sequence = sample if question != -1: y_pred, latent_stuff = model(torch.unsqueeze(x, 0), torch.unsqueeze(question, 0)) else: y_pred, latent_stuff = model(torch.unsqueeze(x, 0), torch.Tensor([-1])) y_pred = torch.sigmoid(y_pred) to_pil = transforms.ToPILImage() f, axes = plt.subplots(num_rows, num_cols) # f.suptitle("\nSample {}, question: {}".format(i_sample, question), fontsize=16) f.suptitle("\nQuestion: {}".format(question), fontsize=16) if num_rows > 1: for i_frame in range(num_rows): # Plot ground truth axes[i_frame, 0].imshow(to_pil(y[i_frame]), cmap='gray') # Plot prediction axes[i_frame, 1].imshow(to_pil(y_pred[0, i_frame]), cmap='gray') # Plot Deviation diff = abs(y_pred[0, i_frame] - y[i_frame]) axes[i_frame, 2].imshow(to_pil(diff), cmap='gray') else: for i_frame in range(num_rows): # Plot ground truth if question != -1: im = torch.zeros_like(full_sequence[0]) if question > 0: for tmp_im in full_sequence[:question.int()]: tmp_im[tmp_im > 0.5] = 1 tmp_im[tmp_im < 0.5] = 0 im += tmp_im im = im.clamp(0, 0.5) im = (im + full_sequence[question.int()]).clamp(0, 1) axes[0].imshow(to_pil(im), cmap='gray') else: axes[0].imshow(to_pil(y), cmap='gray') # Plot prediction axes[1].imshow(to_pil(y_pred[0]), cmap='gray') # Plot Deviation diff = abs(y_pred[0] - y) axes[2].imshow(to_pil(diff), cmap='gray') # Remove axis ticks for ax in axes.reshape(-1): ax.get_xaxis().set_visible(False) ax.get_yaxis().set_tick_params(which='both', length=0, labelleft=False) # Label rows labels = {0: 'Ground truth', 1: 'Prediction', 2: 'Deviation'} for i_col in range(num_cols): if num_rows > 1: plt.sca(axes[0, i_col]) axes[0, i_col].set_title(labels[i_col], rotation=0, size=14) else: plt.sca(axes[i_col]) axes[i_col].set_title(labels[i_col], rotation=0, size=14) f.tight_layout() plt.show(block=True)
def plot_joint_distribution_3dbar_chart(joint_data, X_field, Y_field, chart_field, lower_color=(0., 0., .85, 1.), greater_color=(.85, 0., 0., 1.), neutral_color=(0., .25, .0, 0.25), add_x_label=False, add_y_label=False): plt.interactive(True) fig_3d = plt.figure(figsize=(10, 10)) ax_3d = fig_3d.add_subplot(111, projection='3d') joint_data.sort_values([Y_field, chart_field, X_field], ascending=[True, False, True], inplace=True) x_values = list(joint_data[X_field].drop_duplicates()) joint_data[X_field + '_idx'] = joint_data[X_field].apply(lambda x: x_values.index(x)) y_values = list(joint_data[Y_field].drop_duplicates()) joint_data[Y_field + '_idx'] = joint_data[Y_field].apply(lambda y: y_values.index(y)) x_data = joint_data[X_field + '_idx'].as_matrix() y_data = joint_data[Y_field + '_idx'].as_matrix() z_data = joint_data[chart_field].as_matrix() colors = joint_data['favored hypothesis'].apply( lambda x: greater_color if x == 'significantly_greater' else lower_color if x == 'significantly_lower' else neutral_color) joint_data['color'] = colors ax_3d.bar3d(x_data, y_data, np.zeros(len(joint_data)), 1, 1, z_data, color=colors, linewidth=1.) _, x_labels_texts = plt.xticks(range(len(x_values)), x_values, rotation=90, fontsize=8) _, y_labels_texts = plt.yticks(range(len(y_values)), y_values, rotation=-90, fontsize=8) for x_index, x_label_text in enumerate(x_labels_texts): avg_color_of_value = tuple( map( np.mean, zip(*joint_data[joint_data[X_field].astype(str) == x_label_text._text]['color']))) x_label_text.set_color(avg_color_of_value) for y_index, y_label_text in enumerate(y_labels_texts): avg_color_of_value = tuple( map( np.mean, zip(*joint_data[joint_data[Y_field].astype(str) == y_label_text._text]['color']))) y_label_text.set_color(avg_color_of_value) if add_x_label: ax_3d.set_xlabel(X_field) if add_y_label: ax_3d.set_ylabel(Y_field) plt.show()
import matplotlib import pandas from matplotlib import pyplot from matplotlib2tikz import save as tikz_save from psutil import virtual_memory import numpy matplotlib.style.use('ggplot') pyplot.interactive(False) def to_min_secs(x, pos): x = int(x) minutes = x // 60 seconds = x % 60 return '{:02d}:{:02d}'.format(minutes, seconds) def system_performance(): total_RAM_mb = virtual_memory().total / (1024 * 1024) c = [ 'timestamp', 'escrituras hdd', 'lecturas hdd', 'total I/o', '% freq procesador', '% rendimiento procesador', '% de uso procesador', '% tiempo procesador', 'MB disponibles RAM' ] df = pandas.read_csv('SystemStats.csv', encoding='ISO-8859-1') df.columns = c df['timestamp'] = pandas.to_datetime(df['timestamp']) starttime = df['timestamp'].min()
from scipy.signal import convolve2d from matplotlib.path import Path from matplotlib.patches import PathPatch import matplotlib.colors as mcolors import matplotlib.cm as cm from matplotlib.colors import Normalize import matplotlib.patches as patches import math import copy import pickle import os import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap import matplotlib.pylab as py import mpl_toolkits.mplot3d.art3d as art3d plt.interactive(True); from mpl_toolkits.axes_grid1 import make_axes_locatable from mpl_toolkits.axes_grid1.inset_locator import inset_axes import datetime from descartes.patch import PolygonPatch import matplotlib.patheffects as PathEffects import matplotlib.gridspec as gridspec from matplotlib import rcParams import h5py import matplotlib.pyplot as plt import matplotlib.pylab as pl import scipy def MFP_process (component, init, NSAMP, win_size, XINIT, YINIT, GRDX, GRDY, XEND, YEND, DEPTHS, MFP, rx, ry, VEL, depth_start, sampl_freq): # load init data
import re import sys import numpy as np import lsst.afw.geom as afwGeom import lsst.afw.cameraGeom as afwCG import lsst.geom as geom import lsst.daf.persistence as dafPersist try: plt except NameError: import matplotlib.pyplot as plt from matplotlib.patches import Circle, PathPatch from matplotlib.path import Path plt.interactive(1) def main(butler, visits, fields, fieldRadius, showCCDs=False, aitoff=False, alpha=0.2, byFilter=False, byVisit=False, title="", verbose=False): ra, dec = [], [] filters = {}
def plotMap(map2d_, path_, title_ =''): '''Plots a map (image) of a 2d matrix with a path from start point to the goal point. cells with a value of 0: Free cell; -1: Obstacle; -2: Start point; -3: Goal point; Parameters: ----------- map2d_ : array-like an array with Real Numbers path_ : list of lists a list of the points (2d corrdinates) in the format of [[x0, x1, x2, x3 ...], [y0, y1, y2, y3 ...]] title_ : string information/description of the plot Returns: -------- ''' import matplotlib.cm as cm plt.interactive(False) greennumber = map2d_.max() #greennumber = len(np.unique(map2d_)) #print(greennumber) colors = cm.winter(np.linspace(0, 1, greennumber)) colorsMap2d = [[[] for x in range(map2d_.shape[1])] for y in range(map2d_.shape[0])] # Assign RGB Val for starting point and ending point locStart, locEnd = np.where(map2d_ == -2), np.where(map2d_ == -3) colorsMap2d[locStart[0][0]][locStart[1][0]] = [.0, .0, .0, 1.0] # black colorsMap2d[locEnd[0][0]][locEnd[1][0]] = [.0, .0, .0, 1.0] # white # Assign RGB Val for obstacle locObstacle = np.where(map2d_ == -1) for iposObstacle in range(len(locObstacle[0])): colorsMap2d[locObstacle[0][iposObstacle]][locObstacle[1][iposObstacle]] = [1.0, .0, .0, 1.0] # Assign 0 locZero = np.where(map2d_ == 0) for iposZero in range(len(locZero[0])): colorsMap2d[locZero[0][iposZero]][locZero[1][iposZero]] = [1.0, 1.0, 1.0, 1.0] # Assign Expanded nodes locExpand = np.where(map2d_>0) for iposExpand in range(len(locExpand[0])): colorsMap2d[locExpand[0][iposExpand]][locExpand[1][iposExpand]] = colors[int(map2d_[locExpand[0][iposExpand]][locExpand[1][iposExpand]])-1] for irow in range(len(colorsMap2d)): for icol in range(len(colorsMap2d[irow])): if colorsMap2d[irow][icol] == []: colorsMap2d[irow][icol] = [1.0, 0.0, 0.0, 1.0] fig = plt.figure() plt.title(title_) plt.imshow(colorsMap2d, interpolation='nearest') plt.colorbar() plt.plot(path_[:][0],path_[:][1], color='magenta',linewidth=2.5) plt.ylim(0,map2d_.shape[0]) plt.xlim(0,map2d_.shape[1]) plt.draw() #plt.savefig() #plt.show() ## Example ## Map description ## 0 - Free cell ## -1 - Obstacle ## -2 - Start point ## -3 - Goal point #_map_ = generateMap2d([60,60]) #plt.clf() #plt.imshow(_map_) # map with rotated H shape obstacle and obstacles randomly distributed #map_h_object, info = generateMap2d_obstacle([60,60]) #plt.clf() #plt.imshow(map_h_object) ## solved_map description ## 0 - unexpanded cell ## -1 - obstacle ## -2 - start point ## -3 - goal point ## positive_number - one of the values described in lab2 description (heuristic cost, travel cost, cell total cost,...) #example_solved_map = map_h_object #x_corr, y_corr = range(30), range(30)[::-1] #example_solved_path = [x_corr, y_corr] #pp.plotMap(example_solved_map,example_solved_path)
def mk_plots( res, event=None, issrs=True, Q="auto", drms=None, inc0rb=True, fmt="pdf", onepdf=True, layout=(2, 3), figsize=(11, 8.5), showall=None, showboth=False, cases=None, direc="srs_plots", tight_layout_args=None, plot=plt.plot, show_figures=False, ): """ Make SRS or response history plots Parameters ---------- res : :class:`DR_Results` instance Subclass of dict containing categories with results (see :class:`DR_Results`). For example, ``results['SC_atm'].ext``. event : string or None; optional String for plot titles and file names (eg: 'Liftoff'). If None, `event` is determined from `res[drm].event`. issrs : bool; optional True if plotting SRS data; False otherwise. Q : scalar or iterable or 'auto'; optional The Q value(s) to plot. If 'auto', all the Q values for each category are plotted. Must be a scalar if `showall` is True (see below). drms : list of data recovery categories or None; optional Data recovery categories to plot. If None, plot all available. See also input `inc0rb`. inc0rb : bool; optional If True, the '_0rb' versions of each data recovery category are automatically included. fmt : string or None; optional If `fmt` == "pdf", all plots are written to one PDF file, unless `onepdf` is set to False. If `fmt` is some other string, it is used as the `format` parameter in :func:`matplotlib.pyplot.savefig`. If None, no figures will be saved. Typical values for `fmt` are (from ``fig.canvas.get_supported_filetypes()``):: 'eps': 'Encapsulated Postscript', 'jpeg': 'Joint Photographic Experts Group', 'jpg': 'Joint Photographic Experts Group', 'pdf': 'Portable Document Format', 'pgf': 'PGF code for LaTeX', 'png': 'Portable Network Graphics', 'ps': 'Postscript', 'raw': 'Raw RGBA bitmap', 'rgba': 'Raw RGBA bitmap', 'svg': 'Scalable Vector Graphics', 'svgz': 'Scalable Vector Graphics', 'tif': 'Tagged Image File Format', 'tiff': 'Tagged Image File Format' File naming conventions: if 'SC_atm' is a category, then example output filenames could be:: 'SC_atm_srs.pdf' 'SC_atm_eqsine.pdf' 'SC_atm_srs_0.png', 'SC_atm_srs_1.png', ... 'SC_atm_eqsine_0.png', 'SC_atm_eqsine_1.png', ... onepdf : bool or string; optional If `onepdf` evaluates to True and `fmt` is 'pdf', all plots are written to one PDF file where the name is: ======== ======================================= `onepdf` PDF file name ======== ======================================= string All plots saved in: `onepdf` + ".pdf" True If `issrs`: all plots saved in: `event` + "_srs.pdf" True If not `issrs`: all plots saved in: `event` + "_??.pdf", where "??" is either 'hist', 'psd', or 'frf' as appropriate. ======== ======================================= If False, each figure is saved to its own file named as described above (see `fmt`). layout : 2-element tuple/list; optional Subplot layout, eg: (2, 3) for 2 rows by 3 columns. See also `figsize`. figsize : 2-element tuple/list; optional Define page size in inches. See also `layout`. showall : bool or None; optional If True, show all SRS curves for all cases; otherwise just plot envelope. If None and `showboth` is True, `showall` is set to True. showboth : bool; optional If True, shows all SRS curves and the envelope; otherwise just plot which ever `showall` indicates. direc : string; optional Directory name to put all output plot files; will be created if it doesn't exist. cases : tuple/list of case names to plot or None; optional If None, all cases are plotted. This option is ignored if plotting SRS curves and `showall` is True. tight_layout_args : dict or None; optional Arguments for :func:`matplotlib.pyplot.tight_layout`. If None, defaults to:: {'pad': 3.0, 'w_pad': 2.0, 'h_pad': 2.0, 'rect': (0.3 / figsize[0], 0.3 / figsize[1], 1.0 - 0.3 / figsize[0], 1.0 - 0.3 / figsize[1])} plot : function; optional The function that will draw each curve. Defaults to :func:`matplotlib.pyplot.plot`. As an example, for a plot with a linear X-axis but a log Y-axis, set `plot` to :func:`matplotlib.pyplot.semilogy`. You can also use a custom function of your own devising, but it is expected to accept the same arguments as :func:`matplotlib.pyplot.plot`. show_figures : bool; optional If True, plot figures will be displayed on the screen for interactive viewing. Warning: there may be many figures. Notes ----- Set the `onepdf` parameter to a string to specify the name of the PDF file. Used by :func:`DR_Results.srs_plots` and :func:`DR_Results.resp_plots` for plot generation. """ if tight_layout_args is None: tight_layout_args = { "pad": 3.0, "w_pad": 2.0, "h_pad": 2.0, "rect": ( 0.3 / figsize[0], 0.3 / figsize[1], 1.0 - 0.3 / figsize[0], 1.0 - 0.3 / figsize[1], ), } if showboth and showall is None: showall = True if not os.path.exists(direc): os.mkdir(direc) rows = layout[0] cols = layout[1] perpage = rows * cols # The following removed in v0.95.5: # orientation = "landscape" if figsize[0] > figsize[1] else "portrait" if drms is None: alldrms = sorted(res) else: alldrms = copy.copy(drms) if inc0rb: for name in drms: if name + "_0rb" in res: alldrms.append(name + "_0rb") pdffile = None imode = plt.isinteractive() plt.interactive(show_figures) cur_fig = None try: for name in alldrms: if name not in res: raise ValueError(f"category {name} does not exist.") if issrs: if "srs" not in res[name].__dict__: if drms and name in drms: warnings.warn(f"no SRS data for {name}", RuntimeWarning) continue Qs = _get_Qs(Q, res, name, showall) if Qs is None: continue x = res[name].srs.frq y = None xlab = "Frequency (Hz)" ylab = None ptype = "srs" else: if "hist" in res[name].__dict__: x = res[name].time y = res[name].hist xlab = "Time (s)" ylab = "Response" ptype = "hist" elif "psd" in res[name].__dict__: x = res[name].freq y = res[name].psd xlab = "Frequency (Hz)" ylab = "PSD" ptype = "psd" elif "frf" in res[name].__dict__: x = res[name].freq y = abs(res[name].frf) xlab = "Frequency (Hz)" ylab = "FRF" ptype = "frf" else: if drms and name in drms: warnings.warn(f"no response data for {name}", RuntimeWarning) continue (labels, rowpv, maxlen, sname, srstype, lbl, units, _cases) = _set_vars( issrs, res, name, event, showall, showboth, cases ) if fmt == "pdf" and onepdf and pdffile is None: if isinstance(onepdf, str): fname = os.path.join(direc, onepdf) if not fname.endswith(".pdf"): fname = fname + ".pdf" else: fname = os.path.join(direc, f"{sname}_{ptype}.pdf") pdffile = PdfPages(fname) filenum = 0 uj = 0 # units index nplots = len(rowpv) maxcol = cols if nplots > cols else nplots sub = perpage prefix = None leg_info = [None, 0.0] for j in range(nplots): sub, filenum, prefix, cur_fig = _prep_subplot( rows, cols, sub, perpage, filenum, nplots, fmt, name, lbl, sname, figsize, prefix, onepdf, show_figures, cur_fig, ) label = " ".join(labels[j].split()) if issrs: for q in Qs: if showall: _plot_all( issrs, plot, res[name], q, x, y, showboth, _cases, sub, cols, maxcol, name, label, maxlen, sname, rowpv, j, leg_info, figsize, tight_layout_args, ) else: _plot_ext( plot, res[name], q, Qs, x, sub, cols, maxcol, name, label, maxlen, sname, rowpv, j, ) else: _plot_all( issrs, plot, res[name], None, x, y, showboth, _cases, sub, cols, maxcol, name, label, maxlen, sname, rowpv, j, leg_info, figsize, tight_layout_args, ) _add_xy_labels(issrs, units, uj, xlab, ylab, nplots, sub, srstype) if j + 1 == nplots or (j + 1) % perpage == 0: _legend_layout(leg_info, tight_layout_args) if fmt == "pdf" and onepdf: pdffile.savefig() # orientation=orientation, # papertype='letter') elif fmt: fname = os.path.join(direc, prefix + "." + fmt) # The following removed in v0.95.5: # if fmt != "pdf": # kwargs = dict( # orientation=orientation, dpi=200, bbox_inches="tight" # ) # else: # kwargs = {} plt.savefig(fname, format=fmt) # , **kwargs) if not show_figures: plt.close(cur_fig) cur_fig = None finally: plt.interactive(imode) if pdffile: pdffile.close() if cur_fig: plt.close(cur_fig)
def interactive_matplotlib_context(on=True): old_mode = plt.isinteractive() plt.interactive(on) yield plt.interactive(old_mode)
#para la clasificacion en redes neuronales es recomendable codificar las clases con una neurona de salida #por clase, esto sucede porque se usa la funcion de activacion SoftMaxLayer que lo requiere de esa manera 1 neurona por salida trndata._convertToOneOfMany() tstdata._convertToOneOfMany() validata._convertToOneOfMany() print trndata.indim, trndata.outdim, tstdata.indim, tstdata.outdim #configuracion de la red neuronal para este caso es una red neuronal difusa o FNN con 4 entradas #3 neuronas ocultas y 3 neuronas de salida. net = buildNetwork(4, 5, 3, outclass=SoftmaxLayer) #y tenemos que configurar el algoritmo de entrenamiento backpropagation trainer = BackpropTrainer(net, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01) #ahora para entrenar lo que inicializamos utilizamos la siguiente linea trnerr, valerr = trainer.trainUntilConvergence(dataset=trndata, maxEpochs=100) pl.interactive(False) pl.plot(trnerr, 'b', valerr, 'r') pl.show() out = net.activateOnDataset(tstdata) out = np.argmax(out, axis=1) output = np.array([net.activate(x) for x, _ in validata]) output = output.argmax(axis=1) eror = percentError(output, validata['class']) print 100 - eror print output print validata['class'] print type(validata)
import os import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg plt.interactive("True") # Parse the testSet and dbSet script_dir = os.path.dirname(__file__) dbSet = open(os.path.join(script_dir, "db.txt"), 'r').read().split("\n") testSet = open(os.path.join(script_dir, "test.txt"), 'r').read().split("\n") dbSet = [data.split(";") for data in dbSet] dbSet = dbSet[0:len(dbSet) - 1] testSet = [data.split(";") for data in testSet] testSet = testSet[0:len(testSet) - 1] # Parse matches.txt matches = open(os.path.join(script_dir, "matches.txt"), 'r').read().split("\n") matches = [data.split(":") for data in matches] # print(dbSet[0:10]) # print(testSet[0:10]) # print(matches) imagePaths = [] for match in matches: imagePaths.append([testSet[int(match[0])][0], dbSet[int(match[1])][0]]) # print(imagePaths) fig = plt.figure() for imagePath in imagePaths:
Script uses microphone to receive audio input stream. """ # TO DO: convert to use wave # TO DO: convert to input .wav file # TO DO: extract MFCC from FFT on a .wav file # https://people.csail.mit.edu/hubert/pyaudio/docs/ import time import pyaudio import struct # allows conversion of audio data to ints import numpy as np import matplotlib.pyplot as plt from scipy.fftpack import fft plt.interactive(True) # ensures matplotlib opens a new window # format constants CHUNK = 1024 * 2 FORMAT = pyaudio.paInt16 CHANNELS = 1 # MONO RATE = 44100 # Hz # instance of pyaudio p = pyaudio.PyAudio() # audio input stream object stream = p.open( format=FORMAT, channels=CHANNELS, rate=RATE,
def show_result_jupyter(input1, input2, variable_file=False): fasit, long_results, short_results, df_tot, chosen_p, chosen_r2, r2_modelled, r2_modelled_long, prediction, tipping_ps, short_period, nb_weeks_tipping = input1 fasit_key, ant_kandidater, max_p, reg_end, read_start = input2 """This function prints out and plots the results from the regression.""" plt.interactive(False) reg_start = (pd.to_datetime(time.strftime(reg_end), format="%Y.%m.%d") - Timedelta(days=short_period * 7)).strftime('%Y.%m.%d') print( '\n-----------------------------------------------------------------------' ) print('RESULTATER FOR %s\n' % fasit_key) print('Regresjonsperiode brukt til setup for siste tipping: %s til: %s.' % (read_start, reg_end)) if variable_file: print( 'Input variablene (reg_period og ant_kandidater) ble hentet fra: ', variable_file) else: print( 'Input variablene (reg_period og ant_kandidater) ble funnet ved tuning.' ) print( 'Regresjonsperiode brukt på modellen for siste tipping: %s til: %s, satt til %d uker.' % (reg_start, reg_end, int(short_period))) print( 'Valgte %d kandidater til regresjonen utifra korrelasjon med fasitserien.' % (int(ant_kandidater))) print('Valgte så ut de med p-value < %.5f, som var %i stk.' % (max_p, len(long_results.pvalues))) print('R2 for regresjonen (kort periode): %.5f' % r2_modelled) print('R2 mellom fasit og tipping: %.5f\n' % (reg.calc_R2(fasit[fasit_key].loc[tipping_ps.index[0]:], tipping_ps[:fasit[fasit_key].index[-1]]))) print('Fasit:\n', fasit[fasit_key][-4:]) print('\nModdelert/Tippet:\n', tipping_ps[-5:]) if fasit_key[-3:] == '105': color_tipping = 'blue' elif fasit_key[-3:] == '132': color_tipping = 'lightblue' else: sys.exit("The fasit key should end with 105 or 132") # Plot with regression: plt.figure(figsize=(16, 10)) if (0 <= today.weekday() <= 1) or (today.weekday() == 2 and today.hour < 14): # True for tipping plt.plot(fasit[fasit_key].loc[:reg_end], color='k', linewidth=2.0, label='fasit') else: plt.plot(fasit[fasit_key].loc[:], color='k', linewidth=2.0, label='fasit') plt.plot(short_results.predict(df_tot[chosen_p].loc[reg_start:reg_end]), color='orange', label='regresjon på historie(kort periode)') plt.plot(long_results.predict(df_tot[chosen_p].loc[:reg_start]), color='cyan', label='regresjon på historie (lang periode)') plt.plot(short_results.predict(df_tot[chosen_p].loc[:reg_start]), color='deeppink', label='modell på historie (kort periode)') plt.plot(tipping_ps, label='tipping', color=color_tipping) # , marker='o') plt.title('Regresjon for: %s' % fasit_key) plt.legend() # Plot just prediction: plt.figure(figsize=(16, 10)) plt.plot(fasit[fasit_key].loc[tipping_ps.index[0]:], color='k', linewidth=2.0, label='fasit') plt.plot(tipping_ps, label='tipping', color=color_tipping) # , marker='o') plt.title('Tipping for: %s' % fasit_key) plt.legend() # Plot input series: plt.figure(figsize=(16, 10)) plt.plot(fasit[fasit_key], color='k', linewidth=3.0, label='fasit') for key in chosen_p: if fasit_key[-3:] == '105': sfac = df_tot[fasit_key].mean() / df_tot[key].mean() plt.plot(df_tot[key] * sfac, label=key) # , marker='o') elif fasit_key[-3:] == '132': plt.plot(df_tot[key], label=key) # , marker='o') plt.plot(tipping_ps, label='tipping', color=color_tipping) # , marker='o') plt.title('Regresjonsserier for: %s' % fasit_key) plt.legend() plt.show()
def animate(self, nframes=100, duration=10000, line_styles=None): """ Return animation of one or more evolutionary processes. If the `nframes` is 0, then a static figure is returned instead of an animation. """ if nframes is 0: stride = None else: if nframes > len(self): nframes = len(self) stride = len(self) // nframes if duration < nframes: duration = nframes interval = round(duration / nframes) processes = self.processes labels = [str(p) for p in processes] if line_styles is None: line_styles = ['-' for p in processes] g = [p.growth_rates() for p in processes] p = [processes[:], [p.normalized() for p in processes]] n = len(self) if not stride is None: for i in range(2): p[i] = [ np.concatenate((y[:n:stride], [y[n - 1]])) for y in p[i] ] # for y in p[i]: # y[y == 0] = np.nan n_frames = len(p[0][0]) lines = np.empty((2, len(p[0])), dtype=object) is_interactive = plt.isinteractive() plt.interactive(False) fig, ax = plt.subplots(2, sharex=True) for n in range(2): for i in range(len(g)): w = p[n][i][-1] > 0 lines[n][i], = ax[n].plot(g[i][w], p[n][i][-1][w], label=labels[i], ls=line_styles[i], lw=2, zorder=10) ################################################################################ for n in range(2): for i in range(len(g)): w = p[n][i][0] > 0 ax[n].plot(g[i][w], p[n][i][0][w], c='black', lw=0.5, alpha=0.5, zorder=3) w = p[n][i][-1] > 0 ax[n].plot(g[i][w], p[n][i][-1][w], c=lines[n][i].get_c(), lw=1, alpha=0.7) fig.suptitle('Evolution for {0} Years{1}'.format( len(self) - 1, self.subtitle)) ax[1].set_xlabel('Malthusian Growth Factor') ax[0].set_yscale('log') ax[0].set_ylabel('Frequency') ax[1].set_ylabel('Proportion') ax[0].legend(loc='best') plt.interactive(is_interactive) def initializer(): for n in range(2): for line, x, y in zip(lines[n], g, p[n]): line.set_xdata(x[y[0] > 0]) line.set_ydata(y[0][y[0] > 0]) line.set_zorder(2) return lines.flatten() def animator(i): for n in range(2): for line, x, y in zip(lines[n], g, p[n]): line.set_xdata(x[y[i] > 0]) line.set_ydata(y[i][y[i] > 0]) if i == 1: line.set_zorder(10) return lines.flatten() if stride is None: out = fig else: out = animation.FuncAnimation(fig, animator, init_func=initializer, frames=n_frames, interval=interval, blit=True, repeat_delay=2000) return out
def DisplayLearningCurve(plot): plt.plot(plot) plt.interactive(False) plt.show(block=True)
Required packages: 1. Numpy 2. Scipy 3. Matplotlib Required modules: 1. f_stft 2. f_istft """ import numpy as np from f_stft import f_stft from f_istft import f_istft import matplotlib.pyplot as plt plt.interactive('True') def rpca_ss(x, fs, specparam=None, mask=False, maskgain=0): """ The function rpca_ss uses the RPCA method to decompose the power spectral density of a mixture into its background (assumed to be low-rank) and foreground (assumed to be sparse). A binary mask is formed using the results of RPCA and applied to the mixture to perform background/foreground separation. x: M by N numpy array containing M channel mixtures, each of length N time samples fs: sampling frequency of the audio signal specparam(optional): list containing do_STFT parameters including in order, the window length, window type, overlap in # of samples, and # of fft points.
import pandas as pd from pandas import DataFrame import numpy as np from matplotlib import pyplot as plt plt.interactive(True) ReadCsv = pd.read_csv(r'num_videos_per_gloss.csv', sep=';', header='infer') df = DataFrame(ReadCsv) df.to_csv('num_videos_per_gloss_3.csv', sep=',', index=False)
#!/usr/bin/env python # Copyright (c) 2011-2020, wradlib developers. # Distributed under the MIT License. See LICENSE.txt for more info. import sys import tempfile import unittest import numpy as np import matplotlib.pyplot as pl pl.interactive(True) # noqa from wradlib import georef, util, vis cartopy = util.import_optional('cartopy') class PolarPlotTest(unittest.TestCase): def setUp(self): img = np.zeros((360, 10), dtype=np.float32) img[2, 2] = 10 # isolated pixel img[5, 6:8] = 10 # line img[20, :] = 5 # spike img[60:120, 2:7] = 11 # precip field self.r = np.arange(0, 100000, 10000) self.az = np.arange(0, 360) self.el = np.arange(0, 90) self.th = np.zeros_like(self.az) self.az1 = np.ones_like(self.el) * 225 self.img = img self.proj = georef.create_osr("dwd-radolan")
def preprocess(X, Y, PLOT_COV = False, PLOT_COV_CAT = False, PRINT_MISSING_DATA = True, SIMPLIFY_FEATURES=True, percentage_variance_explained_PCA=1, high_feature_correlation = 0.8, low_output_correlation = 0.01, kernel_pca_components=0, kernel_for_pca="linear"): if PRINT_MISSING_DATA: print_misssing(X) categorical_data = categorical_encoding(X, 5) if PLOT_COV_CAT: plt.interactive(True) plt.matshow(categorical_data.corr()) plt.ioff() plt.show() plt.interactive(False) X = normalize(X, categorical_data) corr_X = X.corr() if PLOT_COV: plt.interactive(True) plt.matshow(corr_X) plt.ioff() plt.show() plt.interactive(False) corr = pd.concat([Y, X], axis=1).corr().values corr_X = corr[1:, 1:] corr_Y = corr[1:, 0] if SIMPLIFY_FEATURES: n_high_corr_X = ((corr_X>high_feature_correlation).sum() - corr_X.shape[0])/2 print("Number of features with correlation of %d or more: %d\n\n" % (high_feature_correlation, n_high_corr_X)) n_low_corr_Y = ((low_output_correlation > corr_Y) & (corr_Y > -low_output_correlation)) print("Number of features with %d output correlation: %d\n\n" % (low_output_correlation, n_low_corr_Y.sum())) corr_X_upper_tri = np.triu(corr_X, 1) delete_features_corr_X = (corr_X_upper_tri>high_feature_correlation).sum(axis=0) != 0 delete_features = np.logical_or(n_low_corr_Y, delete_features_corr_X) for i in range(X.shape[1]-len(delete_features)): delete_features = np.append(delete_features, False) X_new = np.array(X)[:, np.invert(delete_features)] print("%d features were deleted." % (X.shape[1]-X_new.shape[1])) X = X_new pca = 0 if percentage_variance_explained_PCA != 1: X, pca = pca_function(X, percentage_variance_explained_PCA) kernel_pca = 0 if kernel_pca_components != 0: kernel_pca = KernelPCA(n_components=kernel_pca_components, kernel=kernel_for_pca) X = kernel_pca.fit_transform(X) return X, delete_features, pca, kernel_pca
if not args.json: print("Need output filename!") sys.exit(1) g = get_subgraph_for_node(args.single_sankey_json) yang_dict = {} with open(args.dict_file, "r") as df: for line in df: yang_model, yang_auth_email = line.partition(":")[::2] yang_dict[yang_model.strip()] = yang_auth_email print_dependency_tree_as_json(graph=g, filename=args.json, yang_dict=yang_dict) elif args.graph: # Set matplotlib into non-interactive mode plt.interactive(False) ng = prune_standalone_nodes() plt.figure(1, figsize=(20, 20)) print('Plotting the overall dependency graph...') plot_module_dependency_graph(ng) plt.savefig("modules.png") print(' Done.') plt.show() else: plot_num = 2 for node in args.sub_graphs: # Set matplotlib into non-interactive mode plt.interactive(False) plt.figure(plot_num, figsize=(20, 20)) plot_num += 1
def plot(self, obstype='ADT'): IDS=np.unique(self.OBSID) plt.interactive(True) fig = plt.figure(num=1, figsize=(16,14), facecolor='w') for ids in IDS: #I = np.where( (self.OBSID==ids) & (self.qc==1) & (np.abs(self.lat)<90.0) & (np.abs(self.lev)<300.0) ) #I = np.where( (self.OBSID==ids) & (np.abs(self.lev)<2000.0) & (np.abs(self.lev)>0.0) & (np.abs(self.omf)<100.0) & (np.abs(self.lat)<90.0) ) I = np.where( (self.OBSID==ids) & (np.abs(self.lev)<300.0) & (self.fcst!=0.0) & (np.abs(self.lat)<90.0) & (np.abs(self.omf)<10.0)) if ( (ids == 3073) | (ids == 5521) ): if ids==3073: vmin=-3. vmax=3. #ax = fig.add_subplot(411) #ax2=ax.twinx() plt.subplot(611) plt.plot_date(self.date, np.mean(np.abs(self.omf[I])),'ok',markersize=8, alpha=self.alpha, marker=self.marker) plt.plot_date(self.date, np.mean(np.abs(self.oma[I])),'or',markersize=8, alpha=self.alpha, marker=self.marker) #plt.plot_date(self.date, np.sqrt(np.mean((self.omf[I])**2)),'k',markersize=8, alpha=self.alpha, marker=self.marker) #plt.plot_date(self.date, np.mean(np.abs(self.oma[I])),'r',markersize=8, alpha=self.alpha, marker=self.marker) #ax2.plot_date(self.date, len(I[0]),marker='.',color='gray',markersize=8) plt.grid(True) #ax2.grid(False) #ax2.locator_params(axis='y',nbins=6) print 'Nobs=',len(I[0]) #plt.grid(True) #plt.subplot(812) #plt.plot_date(self.date, np.mean(self.omf[I]),'ok',markersize=8) #plt.grid(True) if ids==5521: vmin=-1 vmax=1 plt.subplot(612) plt.plot_date(self.date, np.mean(np.abs(self.omf[I])),'ok',markersize=8, alpha=self.alpha, marker=self.marker) plt.plot_date(self.date, np.mean(np.abs(self.oma[I])),'or',markersize=8, alpha=self.alpha, marker=self.marker) plt.grid(True) #plt.subplot(814) #plt.plot_date(self.date, np.mean(self.omf[I]),'ok',markersize=8) #plt.grid(True) #plt.plot(self.omf[I], -self.lev[I],'.k',alpha=0.2) #plt.xlim((vmin,vmax)) #plt.ylim((-3000,0)) if (ids == 5525): if ids==5525: vmin=-1. vmax=1. if ids==5351: vmin=-.2 vmax=.2 plt.subplot(613) plt.plot_date(self.date, np.mean(np.abs(self.omf[I])),'ok',markersize=8, alpha=self.alpha, marker=self.marker) plt.plot_date(self.date, np.mean(np.abs(self.oma[I])),'or',markersize=8, alpha=self.alpha, marker=self.marker) plt.grid(True) plt.title(obsid_dict[ids],fontweight='bold') #plt.subplot(816) #plt.plot_date(self.date, np.mean(self.omf[I]),'ok',markersize=8) #plt.grid(True) if (ids == 5351): plt.subplot(614) plt.plot_date(self.date, np.mean(np.abs(self.omf[I])),'ok',markersize=8, alpha=self.alpha, marker=self.marker) plt.plot_date(self.date, np.mean(np.abs(self.oma[I])),'or',markersize=8, alpha=self.alpha, marker=self.marker) plt.grid(True) plt.title(obsid_dict[ids],fontweight='bold') if (ids == 6000): IN = np.where( (self.OBSID==ids) & (self.lat>55.0) )#& (np.abs(self.lev)<2000.0) & (self.fcst!=0.0) & (self.lat>55.0) & (np.abs(self.omf)<40.0)) plt.subplot(615) plt.plot_date(self.date, np.mean(np.abs(self.omf[IN])),'ok',markersize=8, alpha=self.alpha, marker=self.marker) plt.plot_date(self.date, np.mean(np.abs(self.oma[IN])),'or',markersize=8, alpha=self.alpha, marker=self.marker) plt.grid(True) plt.title('Arctic '+obsid_dict[ids],fontweight='bold') IS = np.where( (self.OBSID==ids) & (self.lat<-55.0) )#& (np.abs(self.lev)<2000.0) & (self.fcst!=0.0) & (self.lat<-55.0) & (np.abs(self.omf)<40.0)) plt.subplot(616) plt.plot_date(self.date, np.mean(np.abs(self.omf[IS])),'ok',markersize=8, alpha=self.alpha, marker=self.marker) plt.plot_date(self.date, np.mean(np.abs(self.oma[IS])),'or',markersize=8, alpha=self.alpha, marker=self.marker) plt.grid(True) plt.title('Antarctic '+obsid_dict[ids],fontweight='bold') #plt.subplot(818) #plt.plot_date(self.date, np.mean(self.omf[I]),'ok',markersize=8) #plt.grid(True) '''
def plot_mb_group_activity_healthy_vs_mb(marray_data, cat): """ Grouped bar chart showing the absolute expression levels of marker genes in the healthy samples and later passage MB xenografts. :param marray_data: :param cat: :return: """ from matplotlib import pyplot as plt import seaborn as sns plt.interactive(True) sns.set_style('white') marray_ann = add_gene_symbol_column(marray_data, cat) REF_GROUPS = ( ( 'WNT', ('WIF1', 'TNC', 'GAD1', 'DKK2', 'EMX2'), ), ( 'SHH', ('PDLIM3', 'EYA1', 'HHIP', 'ATOH1', 'SFRP1'), ), ( 'Group C', ('IMPG2', 'GABRA5', 'EGFL11', 'NRL', 'MAB21L2', 'NPR3'), ), # EYS = EGFL11 ('Group D', ('KCNA1', 'EOMES', 'KHDRBS2', 'RBM24', 'UNC5D', 'OAS1')), ) CEREBELLUM_SAMPLE_NAMES = [ 'NT-1197', 'NCb-1', 'NCb-2', 'A911105', 'A508112', 'A508285', ] CEREBELLUM_SAMPLE_NAMES += [t + '-R' for t in CEREBELLUM_SAMPLE_NAMES] XENOGRAFT_SAMPLE_NAMES = [ 'ICb1299-III', 'ICb1299-IV', ] XENOGRAFT_SAMPLE_NAMES += [t + '-R' for t in XENOGRAFT_SAMPLE_NAMES] # add new grouping column marray_ann['mb_group'] = [np.nan] * len(marray_ann.index) for grp, arr in REF_GROUPS: marray_ann.loc[marray_ann.gene_symbol.isin(arr), 'mb_group'] = grp # compare cerebellum sample median with passage III and IV grp_data = marray_ann.groupby(['gene_symbol', 'mb_group']) xeno_data = collections.defaultdict(dict) heal_data = collections.defaultdict(dict) for (gene, grp), arr in grp_data: xeno_data[grp][gene] = np.nanmedian( arr.loc[:, XENOGRAFT_SAMPLE_NAMES].values) heal_data[grp][gene] = np.nanmedian( arr.loc[:, CEREBELLUM_SAMPLE_NAMES].values) fig, axs = plt.subplots(ncols=len(xeno_data), sharey=True, figsize=(10, 5)) WIDTH = 0.8 MAX_N_X = max([len(b) for a, b in heal_data.items()]) dw = WIDTH / 2. # each slot has 2 bars for i in range(len(xeno_data)): grp = REF_GROUPS[i][0] x = xeno_data[grp] h = heal_data[grp] ax = axs[i] x0 = np.arange(len(h)) l0, y0 = zip(*h.items()) ax.bar(x0, y0, dw, facecolor='k', edgecolor='none') x1 = x0 + dw y1 = [x[l] for l in l0] ax.bar(x1, y1, dw, facecolor='b', edgecolor='none') ax.set_xticks(x1) ax.set_xticklabels(l0, rotation=90) ax.set_xlabel(grp) ax.xaxis.set_label_coords(0.5, -.21) ax.set_xlim([WIDTH - 1, MAX_N_X]) axs[-1].legend(['Healthy cerebellum', 'MB']) ylim = list(axs[-1].get_ylim()) ylim[0] = -10 axs[-1].set_ylim(ylim) plt.subplots_adjust(left=0.1, right=0.99, bottom=0.2, top=0.95, wspace=0.08, hspace=0.) return fig # useful for programmatic saving
def visualise_engine(__C): # Load parameters dataset = DatasetLoader(__C).DataSet() if __C.CKPT_PATH is not None: print('Warning: you are now using CKPT_PATH args, ' 'CKPT_VERSION and CKPT_EPOCH will not work') path = __C.CKPT_PATH else: path = __C.CKPTS_PATH + \ '/ckpt_' + __C.CKPT_VERSION + \ '/epoch' + str(__C.CKPT_EPOCH) + '.pkl' print('Loading ckpt from: {}'.format(path)) state_dict = torch.load(path)['state_dict'] print('Finish!') if __C.N_GPU > 1: state_dict = ckpt_proc(state_dict) # Store the prediction list # qid_list = [ques['question_id'] for ques in dataset.ques_list] ans_ix_list = [] pred_list = [] data_size = dataset.data_size token_size = dataset.token_size ans_size = dataset.ans_size pretrained_emb = dataset.pretrained_emb net = ModelLoader(__C).Net( __C, pretrained_emb, token_size, ans_size, dataset.token_to_ix ) net.cuda() net.eval() if __C.N_GPU > 1: net = nn.DataParallel(net, device_ids=__C.DEVICES) net.load_state_dict(state_dict) batch_size = 1 dataloader = Data.DataLoader( dataset, batch_size=batch_size, shuffle=True, num_workers=__C.NUM_WORKERS, pin_memory=__C.PIN_MEM ) for step, ( frcn_feat_iter, grid_feat_iter, bbox_feat_iter, ques_ix_iter, ans_iter, image_id, question, words, target_ans ) in enumerate(dataloader): print("\rEvaluation: [step %4d/%4d]" % ( step, int(data_size / __C.EVAL_BATCH_SIZE), ), end=' ') frcn_feat_iter = frcn_feat_iter.cuda() grid_feat_iter = grid_feat_iter.cuda() bbox_feat_iter = bbox_feat_iter.cuda() ques_ix_iter = ques_ix_iter.cuda() pred, img_attention_map, txt_attention_map = net( frcn_feat_iter, grid_feat_iter, bbox_feat_iter, ques_ix_iter ) img_attention_map = img_attention_map[:, :, :, 1:] txt_attention_map = txt_attention_map[:, :, :, 1:len(words) + 1] pred_np = pred.cpu().data.numpy() pred_argmax = np.argmax(pred_np, axis=1) ans = dataset.ix_to_ans[pred_argmax[0]] plt.interactive(False) visualise_img(question['image_filename'][0], question['question'][0], img_attention_map.cpu().data.numpy()[0], ans, target_ans[0]) visualise_txt(words, txt_attention_map.cpu().data.numpy()[0])
def setPlotFile(fileName): global plotFile plotFile = PdfPages(fileName) plt.interactive(False)
def __init__(self, normalized_worm, motion_mode=None, interactive=False): """ Initialize the animation of the worm's attributes. Parameters --------------------------------------- normalized_worm: NormalizedWorm the NormalizedWorm object to be plotted. motion_mode: 1-dimensional numpy array (optional) The motion mode of the worm over time. Must have length = normalized_worm.num_frames interactive: boolean (optional) if interactive is set to False, then: suppress the drawing of the figure, until an explicit plt.show() is called. this allows WormPlotter to be instantiated without just automatically being displayed. Instead, the user must call WormPlotter.show() to have plt.show() be called. Notes --------------------------------------- To initialize the animation, we must do six things: 1. set up the data to be used from the normalized_worm 2. create the figure 3. create subplots in the figure, assigning them Axis handles 4. create Artist objects for all objects in the subplots 5. assign the Artist objects to the correct Axis handle 6. call the base class __init__ """ # A WormPlotter instance can be instantiated to be interactive, # or by default it is set to NOT interactive, which means that # WormPlotter.show() must be called to get it to display. plt.interactive(interactive) # 1. set up the data to be used self.motion_mode = motion_mode self.motion_mode_options = {-1: 'backward', 0: 'paused', 1: 'forward'} self.motion_mode_colours = { -1: 'b', # blue 0: 'r', # red 1: 'g' } # green self.normalized_worm = normalized_worm # TODO: eventually we'll put this in a nicer place self.vulva_contours = self.normalized_worm.data_dict['vulva_contours'] self.non_vulva_contours = self.normalized_worm.data_dict[ 'non_vulva_contours'] self.skeletons = self.normalized_worm.data_dict['skeletons'] self.skeletons_centred = self.normalized_worm.translate_to_centre() self.skeleton_centres = self.normalized_worm.centre() self.orientation = self.normalized_worm.angle() self.skeletons_rotated = self.normalized_worm.rotate_and_translate() # 2. create the figure fig = plt.figure(figsize=(5, 5)) # We have blit=True, so the animation only redraws the elements that have # changed. This means that if the window is resized, everything other # than the plot area will be black. To fix this, here we have matplotlib # explicitly redraw everything if a resizing event occurs. # DEBUG: this actually doesn't appear to work. def refresh_plot(event): fig.canvas.draw() self.refresh_connection_id = \ fig.canvas.mpl_connect('resize_event', refresh_plot) fig.suptitle('C. elegans attributes', fontsize=20) # 3. add the subplots ax1 = plt.subplot2grid((3, 3), (0, 0), rowspan=2, colspan=2) ax1.set_title('Position') ax1.set_xlabel('x') ax1.set_ylabel('y') ax1.set_xlim(self.normalized_worm.position_limits(0)) ax1.set_ylim(self.normalized_worm.position_limits(1)) #ax1.set_aspect(aspect='equal', adjustable='datalim') #ax1.set_autoscale_on() self.annotation1a = ax1.annotate("(motion mode data not available)", xy=(-10, 10), xycoords='axes points', horizontalalignment='right', verticalalignment='top', fontsize=10) self.annotation1b = ax1.annotate("bottom right (points)", xy=(-10, 10), xycoords='axes points', horizontalalignment='right', verticalalignment='bottom', fontsize=10) # DEBUG: this doesn't appear to do anything, #it was an attempt to get it to refresh with diff titles #self.annotation1.animated = True ax2 = plt.subplot2grid((3, 3), (2, 0)) ax2.set_title('Morphology') ax2.set_xlim( (-500, 500)) # DON'T USE set_xbound, it changes dynmically ax2.set_ylim((-500, 500)) ax2.set_aspect(aspect='equal', adjustable='datalim') self.annotation2 = ax2.annotate("Worm head", xy=(0, 0), xycoords='data', xytext=(10, 10), textcoords='data', arrowprops=dict( arrowstyle="fancy", connectionstyle="arc3,rad=.2")) ax3 = plt.subplot2grid((3, 3), (2, 1)) ax3.set_title('Orientation-free') ax3.set_xlim( (-500, 500)) # DON'T USE set_xbound, it changes dynmically ax3.set_ylim((-500, 500)) ax3.set_aspect(aspect='equal', adjustable='datalim') ax4 = plt.subplot2grid((3, 3), (2, 2)) #self.annotation4 = ax4.annotate("Segmentation status: ", # xy=(0,0), xycoords='data', # xytext=(10, 10), textcoords='data', # arrowprops=dict(arrowstyle="fancy", # connectionstyle="arc3,rad=.2")) # 4. create Artist objects self.line1W = Line2D([], [], color='green', linestyle='point marker', marker='o', markersize=5) self.line1W_head = Line2D([], [], color='red', linestyle='point marker', marker='o', markersize=7) self.line1C = Line2D([], [], color='yellow', linestyle='point marker', marker='o', markersize=5) self.patch1E = Ellipse(xy=(0, 0), width=1000, height=500, angle=0, alpha=0.3) self.line2W = Line2D([], [], color='black', marker='o', markersize=5) self.line2W_head = Line2D([], [], color='red', linestyle='point marker', marker='o', markersize=7) self.line2C = Line2D([], [], color='blue') self.line2C2 = Line2D([], [], color='orange') self.line3W = Line2D([], [], color='black', marker='o', markersize=5) self.line3W_head = Line2D([], [], color='red', linestyle='point marker', marker='o', markersize=7) self.artists_to_be_drawn = \ [self.line1W, self.line1C, self.line1W_head, self.patch1E, self.annotation1a, self.annotation1b, self.line2W, self.line2C, self.line2C2, self.line2W_head, self.annotation2, self.line3W, self.line3W_head] # 5. assign Artist objects to the relevant subplot ax1.add_line(self.line1W) ax1.add_line(self.line1W_head) ax1.add_line(self.line1C) ax1.add_artist(self.patch1E) ax2.add_line(self.line2W) ax2.add_line(self.line2W_head) ax2.add_line(self.line2C) ax2.add_line(self.line2C2) ax3.add_line(self.line3W) ax3.add_line(self.line3W_head) # So labels don't overlap: plt.tight_layout() # 6. call the base class __init__ # TimedAnimation draws a new frame every *interval* milliseconds. # so this is how we convert from FPS to interval: interval = 1000 / config.FPS return animation.TimedAnimation.__init__(self, fig, interval=interval, blit=True)
def closePlotFile(): global plotFile if plotFile is not None: plotFile.close() plotFile = None plt.interactive(True)
def set_mpl_interactive(): '''Ensure matplotlib is in interactive mode.''' import matplotlib.pyplot as plt if not plt.isinteractive(): plt.interactive(True)
def rptpct1( mxmn1, mxmn2, filename, *, title="PERCENT DIFFERENCE REPORT", names=("Self", "Reference"), desc=None, filterval=None, labels=None, units=None, ignorepv=None, uf_reds=None, use_range=True, numform=None, prtbad=None, prtbadh=None, prtbadl=None, flagbad=None, flagbadh=None, flagbadl=None, dohistogram=True, histogram_inc=1.0, domagpct=True, magpct_options=None, doabsmax=False, shortabsmax=False, roundvals=-1, rowhdr="Row", deschdr="Description", maxhdr="Maximum", minhdr="Minimum", absmhdr="Abs-Max", perpage=-1, tight_layout_args=None, show_figures=False, align_by_label=True, ): """ Write a percent difference report between 2 sets of max/min data Parameters ---------- mxmn1 : 2d array_like or SimpleNamespace The max/min data to compare to the `mxmn2` set. If 2-column array_like, its columns are: [max, min]. If SimpleNamespace, it must be as defined in :class:`DR_Results` and have these members: .. code-block:: none .ext = [max, min] .drminfo = SimpleNamespace which has (at least): .desc = one line description of category .filterval = the filter value; (see `filterval` description below) .labels = a list of descriptions; one per row .ignorepv = these rows will get 'n/a' for % diff .units = string with units .uf_reds = uncertainty factors Note that the inputs `desc`, `labels`, etc, override the values above. mxmn2 : 2d array_like or SimpleNamespace The reference set of max/min data. Format is the same as `mxmn1`. .. note:: If both `mxmn1` and `mxmn2` are SimpleNamespaces and have the ``.drminfo.labels`` attribute, this routine will, by default, use the labels to align the data sets for comparison. To prevent this, set the `align_by_label` parameter to False. filename : string or file_like or 1 or None Either a name of a file, or is a file_like object as returned by :func:`open` or :class:`io.StringIO`. Input as integer 1 to write to stdout. Can also be the name of a directory or None; in these cases, a GUI is opened for file selection. title : string; must be named; optional Title for the report names : list/tuple; must be named; optional Two (short) strings identifying the two sets of data desc : string or None; must be named; optional A one line description of the table. Overrides `mxmn1.drminfo.desc`. If neither are input, 'No description provided' is used. filterval : scalar, 1d array_like or None; must be named; optional Numbers with absolute value <= than `filterval` will get a 'n/a' % diff. If vector, length must match number of rows in `mxmn1` and `mxmn2` data. Overrides `mxmn1.drminfo.filterval`. If neither are input, `filterval` is set to 1.e-6. labels : list or None; must be named; optional A list of strings briefly describing each row. Overrides `mxmn1.drminfo.labels`. If neither are input, ``['Row 1','Row 2',...]`` is used. units : string or None; must be named; optional Specifies the units. Overrides `mxmn1.drminfo.units`. If neither are input, 'Not specified' is used. ignorepv : 1d array or None; must be named; optional 0-offset index vector specifying which rows of `mxmn1` to ignore (they get the 'n/a' % diff). Overrides `mxmn1.drminfo.ignorepv`. If neither are input, no rows are ignored (though `filterval` is still used). .. note:: `ignorepv` applies *before* any alignment by labels is done (when `align_by_label` is True, which is the default). uf_reds : 1d array or None; must be named; optional Uncertainty factors: [rigid, elastic, dynamic, static]. Overrides `mxmn1.drminfo.uf_reds`. If neither is input, 'Not specified' is used. use_range : bool; must be named, optional If True, the denominator of the % diff calc for both the max & min for each row is the absolute maximum of the reference max & min for that row. If False, the denominator is the applicable reference max or min. A quick example shows why ``use_range=True`` might be useful: .. code-block:: none If [max1, min1] = [12345, -10] and [max2, min2] = [12300, 50] Then: % diff = [0.37%, 0.49%] if use_range is True % diff = [0.37%, 120.00%] if use_range is False Note that the sign of the % diff is defined such that a positive % diff means an exceedance: where ``max1 > max2`` or ``min1 < min2``. `use_range` is ignored if `doabsmax` is True. numform : string or None; must be named; optional Format of the max & min numbers. If None, it is set internally to be 13 chars wide and depends on the range of numbers to print: - if range is "small", numform='{:13.xf}' where "x" ranges from 0 to 7 - if range is "large", numform='{:13.6e}' prtbad : scalar or None; must be named; optional Only print rows where ``abs(%diff) > prtbad``. For example, to print rows off by more than 5%, use ``prtbad=5``. `prtbad` takes precedence over `prtbadh` and `prtbadl`. prtbadh : scalar or None; must be named; optional Only print rows where ``%diff > prtbadh``. Handy for showing just the exceedances. `prtbadh` takes precedence over `prtbadl`. prtbadl : scalar or None; must be named; optional Only print rows where ``%diff < prtbadl``. Handy for showing where reference rows are higher. flagbad : scalar or None; must be named; optional Flag % diffs where ``abs(%diff) > flagbad``. Works similar to `prtbad`. The flag is an asterisk (*). flagbadh : scalar or None; must be named; optional Flag % diffs where ``%diff > flagbadh``. Works similar to `prtbadh`. Handy for flagging exceedances. `flagbadh` takes precedence over `flagbadl`. flagbadl : scalar or None; must be named; optional Flag % diffs where ``%diff < flagbadl``. Works similar to `prtbadl`. dohistogram : bool; must be named; optional If True, plot the histograms. Plots will be written to "`filename`.histogram.png". histogram_inc : scalar; must be named; optional The histogram increment; defaults to 1.0 (for 1%). domagpct : bool; must be named; optional If True, plot the percent differences versus magnitude via :func:`magpct`. Plots will be written to "`filename`.magpct.png". Filtering for the "magpct" plot is controlled by the ``magpct_options['filterval']`` and ``magpct_options['symlogy']`` options. By default, all percent differences are shown, but the larger values (according to the `filterval` filter) are emphasized by using a mixed linear/log y-axis. The percent differences for the `ignorepv` rows are not plotted. magpct_options : None or dict; must be named; optional If None, it is internally reset to:: magpct_options = {'filterval': 'filterval'} Use this parameter to provide any options to :func:`magpct` but note that the `filterval` option for :func:`magpct` is treated specially. Here, in addition to any of the values that :func:`magpct` accepts, it can also be set to the string "filterval" as in the default case shown above. In that case, ``magpct_options['filterval']`` gets internally reset to the initial value of `filterval` (which is None by default). .. note:: The call to :func:`magpct` is *after* applying `ignorepv` and doing any data aligning by labels. .. note:: The two filter value options (`filterval` and ``magpct_options['filterval']``) have different defaults: None and 'filterval`, respectively. They also differ on how the ``None`` setting is used: for `filterval`, None is replaced by 1.e-6 while for `magpct_filterval`, None means that the "magpct" plot will not have any filters applied at all. .. note:: The above means that, if you accept the default values for `filterval` and for ``magpct_options['filterval']``, then tables and the histogram plots will use a `filterval` of 1.e-6 while the "magpct" plots will use no filter (it compares everything except perfect zeros). doabsmax : bool; must be named; optional If True, compare only absolute maximums. shortabsmax : bool; must be named; optional If True, set ``doabsmax=True`` and do not print the max1 and min1 columns. roundvals : integer; must be named; optional Round max & min numbers at specified decimal. If negative, no rounding. rowhdr : string; must be named; optional Header for row number column deschdr : string; must be named; optional Header for description column maxhdr : string; must be named; optional Header for the column 1 data minhdr : string; must be named; optional Header for the column 2 data absmhdr : string; must be named; optional Header for abs-max column perpage : integer; must be named; optional The number of lines to write perpage. If < 1, there is no limit (one page). tight_layout_args : dict or None; must be named; optional Arguments for :func:`matplotlib.pyplot.tight_layout`. If None, defaults to ``{'pad': 3.0}``. show_figures : bool; must be named; optional If True, plot figures will be displayed on the screen for interactive viewing. Warning: there may be many figures. align_by_label : bool; must be named; optional If True, use labels to align the two sets of data for comparison. See note above under the `mxmn2` option. Returns ------- pdiff_info : dict Dictionary with 'amx' (abs-max), 'mx' (max), and 'mn' keys: .. code-block:: none <class 'dict'>[n=3] 'amx': <class 'dict'>[n=5] 'hsto' : float64 ndarray 33 elems: (11, 3) 'mag' : [n=2]: (float64 ndarray: (100,), ... 'pct' : float64 ndarray 100 elems: (100,) 'prtpv': bool ndarray 100 elems: (100,) 'spct' : [n=100]: [' -2.46', ' -1.50', ... 'mn' : <class 'dict'>[n=5] 'hsto' : float64 ndarray 33 elems: (11, 3) 'mag' : [n=2]: (float64 ndarray: (100,), ... 'pct' : float64 ndarray 100 elems: (100,) 'prtpv': bool ndarray 100 elems: (100,) 'spct' : [n=100]: [' 1.55', ' 1.53', ... 'mx' : <class 'dict'>[n=5] 'hsto' : float64 ndarray 27 elems: (9, 3) 'mag' : [n=2]: (float64 ndarray: (100,), ... 'pct' : float64 ndarray 100 elems: (100,) 'prtpv': bool ndarray 100 elems: (100,) 'spct' : [n=100]: [' -2.46', ' -1.50', ... Where: .. code-block:: none 'hsto' : output of :func:`histogram`: [center, count, %] 'mag' : inputs to :func:`magpct` 'pct' : percent differences 'prtpv' : rows to print partition vector 'spct' : string version of 'pct' Examples -------- >>> import numpy as np >>> from pyyeti import cla >>> ext1 = [[120.0, -8.0], ... [8.0, -120.0]] >>> ext2 = [[115.0, -5.0], ... [10.0, -125.0]] Run :func:`rptpct1` multiple times to get a more complete picture of all the output (the table is very wide). Also, the plots will be turned off for this example. First, the header: >>> opts = {'domagpct': False, 'dohistogram': False} >>> dct = cla.rptpct1(ext1, ext2, 1, **opts) # doctest: +ELLIPSIS PERCENT DIFFERENCE REPORT <BLANKLINE> Description: No description provided Uncertainty: Not specified Units: Not specified Filter: 1e-06 Notes: % Diff = +/- abs(Self-Reference)/max(abs(Reference... Sign set such that positive % differences indicate... Date: ... ... Then, the max/min/absmax percent difference table in 3 calls: >>> dct = cla.rptpct1(ext1, ext2, 1, **opts) # doctest: +ELLIPSIS PERCENT DIFFERENCE REPORT ... Self Reference ... Row Description Maximum Maximum % Diff ... ------- ----------- ------------- ------------- ------- ... 1 Row 1 120.00000 115.00000 4.35 ... 2 Row 2 8.00000 10.00000 -1.60 ... ... >>> dct = cla.rptpct1(ext1, ext2, 1, **opts) # doctest: +ELLIPSIS PERCENT DIFFERENCE REPORT ... ... Self Reference ... Row Description ... Minimum Minimum % Diff ... ------- ----------- ...------------- ------------- ------- ... 1 Row 1 ... -8.00000 -5.00000 2.61 ... 2 Row 2 ... -120.00000 -125.00000 -4.00 ... ... >>> dct = cla.rptpct1(ext1, ext2, 1, **opts) # doctest: +ELLIPSIS PERCENT DIFFERENCE REPORT ... ... Self Reference Row Description ... Abs-Max Abs-Max % Diff ------- ----------- ...------------- ------------- ------- 1 Row 1 ... 120.00000 115.00000 4.35 2 Row 2 ... 120.00000 125.00000 -4.00 ... Finally, the histogram summaries: >>> dct = cla.rptpct1(ext1, ext2, 1, **opts) # doctest: +ELLIPSIS PERCENT DIFFERENCE REPORT ... No description provided - Maximum Comparison Histogram <BLANKLINE> % Diff Count Percent -------- -------- ------- -2.00 1 50.00 4.00 1 50.00 <BLANKLINE> 0.0% of values are within 1% 50.0% of values are within 2% 100.0% of values are within 5% <BLANKLINE> % Diff Statistics: [Min, Max, Mean, StdDev] = [-1.60, 4.35,... <BLANKLINE> <BLANKLINE> No description provided - Minimum Comparison Histogram <BLANKLINE> % Diff Count Percent -------- -------- ------- -4.00 1 50.00 3.00 1 50.00 <BLANKLINE> 0.0% of values are within 1% 100.0% of values are within 5% <BLANKLINE> % Diff Statistics: [Min, Max, Mean, StdDev] = [-4.00, 2.61,... <BLANKLINE> <BLANKLINE> No description provided - Abs-Max Comparison Histogram <BLANKLINE> % Diff Count Percent -------- -------- ------- -4.00 1 50.00 4.00 1 50.00 <BLANKLINE> 0.0% of values are within 1% 100.0% of values are within 5% <BLANKLINE> % Diff Statistics: [Min, Max, Mean, StdDev] = [-4.00, 4.35,... """ if tight_layout_args is None: tight_layout_args = {"pad": 3.0} if magpct_options is None: magpct_options = {"filterval": "filterval"} else: magpct_options = magpct_options.copy() # magpct_options['filterval'] get special treatment: magpct_filterval = magpct_options["filterval"] del magpct_options["filterval"] if isinstance(magpct_filterval, str): if magpct_filterval != "filterval": raise ValueError("``magpct_options['filterval']`` is an invalid " f"string: {magpct_filterval!r} (can only " "be 'filterval' if a string)") # copy the initial `filterval` setting: magpct_filterval = filterval infovars = ( "desc", "filterval", "magpct_filterval", "labels", "units", "ignorepv", "uf_reds", ) dct = locals() infodct = {n: dct[n] for n in infovars} del dct # check mxmn1: if isinstance(mxmn1, SimpleNamespace): sns = mxmn1.drminfo for key, value in infodct.items(): if value is None: infodct[key] = getattr(sns, key, None) del sns mxmn1 = mxmn1.ext else: mxmn1 = np.atleast_2d(mxmn1) row_number = np.arange(1, mxmn1.shape[0] + 1) # check mxmn2: if isinstance(mxmn2, SimpleNamespace) and getattr(mxmn2, "drminfo", None): labels2 = mxmn2.drminfo.labels mxmn2 = mxmn2.ext if align_by_label: # use labels and labels2 to align data; this is in case # the two sets of results recover some of the same items, # but not all mxmn1, mxmn2, row_number = _align_mxmn(mxmn1, mxmn2, labels2, row_number, infodct) else: mxmn2 = np.atleast_2d(mxmn2) desc = infodct["desc"] if desc is None: desc = "No description provided" R = mxmn1.shape[0] if R != mxmn2.shape[0]: raise ValueError( f"`mxmn1` and `mxmn2` have a different number of rows: " f"{R} vs {mxmn2.shape[0]} for category with `desc` = {desc}") filterval = infodct["filterval"] magpct_filterval = infodct["magpct_filterval"] labels = infodct["labels"] units = infodct["units"] ignorepv = infodct["ignorepv"] uf_reds = infodct["uf_reds"] del infodct if filterval is None: filterval = 1.0e-6 filterval = _proc_filterval(filterval, R, "filterval") magpct_filterval = _proc_filterval(magpct_filterval, R, "magpct_options['filterval']") if labels is None: labels = [f"Row {i + 1:6d}" for i in range(R)] elif len(labels) != R: raise ValueError("length of `labels` does not match number" f" of rows in `mxmn1`: {len(labels)} vs {R} for " f"category with `desc` = {desc}") if units is None: units = "Not specified" if numform is None: numform = _get_numform(mxmn1) pdhdr = "% Diff" nastring = "n/a " comppv = np.ones(R, bool) if ignorepv is not None: comppv[ignorepv] = False # for row labels: w = max(11, len(max(labels, key=len))) frm = f"{{:{w}}}" # start preparing for writer.formheader: print_info = SimpleNamespace( headers1=["", ""], headers2=[rowhdr, deschdr], formats=["{:7d}", frm], printargs=[row_number, labels], widths=[7, w], seps=[0, 2], justs=["c", "l"], ) if shortabsmax: doabsmax = True if doabsmax: use_range = False if roundvals > -1: mxmn1 = np.round(mxmn1, roundvals) mxmn2 = np.round(mxmn2, roundvals) prtbads = (prtbad, prtbadh, prtbadl) flagbads = (flagbad, flagbadh, flagbadl) # compute percent differences pctinfo = {} kwargs = dict( names=names, mxmn1=mxmn1, comppv=comppv, histogram_inc=histogram_inc, numform=numform, prtbads=prtbads, flagbads=flagbads, maxhdr=maxhdr, minhdr=minhdr, absmhdr=absmhdr, pdhdr=pdhdr, nastring=nastring, doabsmax=doabsmax, shortabsmax=shortabsmax, print_info=print_info, ) with warnings.catch_warnings(): warnings.filterwarnings("ignore", r"All-NaN (slice|axis) encountered") mx1 = np.nanmax(abs(mxmn1), axis=1) mx2 = np.nanmax(abs(mxmn2), axis=1) if not doabsmax: max1, min1 = mxmn1[:, 0], mxmn1[:, 1] max2, min2 = mxmn2[:, 0], mxmn2[:, 1] mxmn_b = mxmn2 if use_range else None prtpv = np.zeros(R, bool) for i in zip( ("mx", "mn", "amx"), (max1, min1, mx1), (max2, min2, mx2), (True, False, True), (maxhdr, minhdr, absmhdr), ): lbl, ext1, ext2, ismax, valhdr = i pctinfo[lbl] = _proc_pct( ext1, ext2, filterval, magpct_filterval, mxmn_b=mxmn_b, ismax=ismax, valhdr=valhdr, **kwargs, ) prtpv |= pctinfo[lbl]["prtpv"] prtpv &= comppv else: pctinfo["amx"] = _proc_pct( mx1, mx2, filterval, magpct_filterval, mxmn_b=None, ismax=True, valhdr=absmhdr, **kwargs, ) prtpv = pctinfo["amx"]["prtpv"] hu, frm = writer.formheader( [print_info.headers1, print_info.headers2], print_info.widths, print_info.formats, sep=print_info.seps, just=print_info.justs, ) # format page header: misc = _get_filtline(filterval) + _get_noteline(use_range, names, prtbads, flagbads) hdrs = _get_rpt_headers(desc=desc, uf_reds=uf_reds, units=units, misc=misc) header = title + "\n\n" + hdrs + "\n" imode = plt.isinteractive() plt.interactive(show_figures) try: if domagpct: _plot_magpct( pctinfo, names, desc, doabsmax, filename, magpct_options, use_range, maxhdr, minhdr, absmhdr, show_figures, tight_layout_args, ) if dohistogram: _plot_histogram( pctinfo, names, desc, doabsmax, filename, histogram_inc, maxhdr, minhdr, absmhdr, show_figures, tight_layout_args, ) finally: plt.interactive(imode) # write results @guitools.write_text_file def _wtcmp(f, header, hu, frm, printargs, perpage, prtpv, pctinfo, desc): prtpv = prtpv.nonzero()[0] if perpage < 1: # one additional in case size is zero perpage = prtpv.size + 1 pages = (prtpv.size + perpage - 1) // perpage if prtpv.size < len(printargs[0]): for i, item in enumerate(printargs): printargs[i] = [item[j] for j in prtpv] tabhead = header + hu pager = "\n" # + chr(12) for p in range(pages): if p > 0: f.write(pager) f.write(tabhead) b = p * perpage e = b + perpage writer.vecwrite(f, frm, *printargs, so=slice(b, e)) f.write(pager) for lbl, hdr in zip(("mx", "mn", "amx"), (maxhdr, minhdr, absmhdr)): if lbl in pctinfo: f.write(_get_histogram_str(desc, hdr, pctinfo[lbl])) _wtcmp(filename, header, hu, frm, print_info.printargs, perpage, prtpv, pctinfo, desc) return pctinfo
def plot_qbc_example(): dpi = 100 color_palette = ("tab:blue", "tab:orange", "tab:green", "tab:red", "tab:purple", "tab:brown") plot_path = os.path.join(dr(dr(dr(abspath(__file__)))), 'results', 'plots', 'for_ppt') plt.interactive(True) # prepare figure figure = plt.subplots(nrows=2, ncols=2, figsize=(12, 6), dpi=dpi) ax_all = figure[0].axes[0] ax_gt = figure[0].axes[1] ax_random = figure[0].axes[2] ax_active = figure[0].axes[3] # random points, [0, :] for x; [1, :] for y, sorted for x random_all = np.random.rand(2, 200) random_all[0, :] = np.sort(random_all[0, :]) random_all[1, :] = random_all[1, np.argsort(random_all[0, :])] # plot random unlabelled ax_all.scatter(x=random_all[0, :], y=random_all[1, :], c='#606060', marker='x') ax_all.set_title('Unlabeled data', fontsize=16) ax_all.set_autoscale_on(False) # make it pretty # plot all labelled idx_cat1 = np.argwhere(random_all[0, :] < 0.5) idx_cat2 = np.argwhere(random_all[0, :] > 0.5) # give overlap idx_temp = idx_cat1[-6:-1].copy() idx_cat1[-6:-1] = idx_cat2[0:5] idx_cat2[0:5] = idx_temp.copy() # plot ax_gt.scatter(x=random_all[0, idx_cat1], y=random_all[1, idx_cat1], c=color_palette[0], marker="o") ax_gt.scatter(x=random_all[0, idx_cat2], y=random_all[1, idx_cat2], c=color_palette[1], marker="s") # plot boundary ax_gt.set_autoscale_on(False) # ax_gt.axvline(x=0.5, c=color_palette[2]) ax_gt.set_title('If All Data Labeled', fontsize=16) # make it pretty # plot random training, skew towards cat 1 skewer_cat1 = idx_cat1[np.intersect1d(np.argwhere(random_all[0, idx_cat1] > 0.35), np.argwhere(random_all[1, idx_cat1] < 0.25))].squeeze() skewer_cat2 = idx_cat2[np.intersect1d(np.argwhere(random_all[0, idx_cat2] < 0.65), np.argwhere(random_all[1, idx_cat2] > 0.75))].squeeze() idx_cat1_random = np.random.randint(low=0, high=idx_cat1.max(), size=30-min(skewer_cat1.size, 8)) idx_cat2_random = np.random.randint(low=idx_cat2.min(), high=idx_cat2.max(), size=30-min(skewer_cat2.size, 8)) idx_cat1_rand = np.concatenate([idx_cat1_random, skewer_cat1[0:(min(skewer_cat1.size, 8))]]) # 5 skewer data idx_cat2_rand = np.concatenate([idx_cat2_random, skewer_cat2[0:(min(skewer_cat2.size, 8))]]) # 5 skewer data # plot unlabeled ax_random.scatter(x=random_all[0, np.setdiff1d(range(200), [idx_cat1_rand, idx_cat2_rand])], y=random_all[1, np.setdiff1d(range(200), [idx_cat1_rand, idx_cat2_rand])], c='#606060', marker='x') # plot labeled ax_random.scatter(x=random_all[0, idx_cat1_rand], y=random_all[1, idx_cat1_rand], c=color_palette[0], marker="o") ax_random.scatter(x=random_all[0, idx_cat2_rand], y=random_all[1, idx_cat2_rand], c=color_palette[1], marker="s") # plot boundary ax_random.set_autoscale_on(False) # ax_random.plot([0.55, 0.45], [-0.1, 1.1], c=color_palette[2]) ax_random.set_title('Trained with random', fontsize=16) # make it pretty # plot active learning idx_al1 = idx_cat1[-30:].squeeze() idx_al2 = idx_cat2[0:30].squeeze() # plot unlabeled ax_active.scatter(x=random_all[0, np.setdiff1d(range(200), [idx_al1, idx_al2])], y=random_all[1, np.setdiff1d(range(200), [idx_al1, idx_al2])], c='#606060', marker='x') # plot labeled ax_active.scatter(x=random_all[0, idx_al1], y=random_all[1, idx_al1], c=color_palette[0], marker="o") ax_active.scatter(x=random_all[0, idx_al2], y=random_all[1, idx_al2], c=color_palette[1], marker="s") # plot boundary ax_active.set_autoscale_on(False) # ax_active.plot([0.49, 0.51], [-0.1, 1.1], c=color_palette[2]) ax_active.set_title('Trained with active learning', fontsize=16) # make it pretty plt.tight_layout() ax_all.tick_params(axis='both', which='both', bottom=False, left=False, labelbottom=False, labelleft=False) ax_gt.tick_params(axis='both', which='both', bottom=False, left=False, labelbottom=False, labelleft=False) ax_random.tick_params(axis='both', which='both', bottom=False, left=False, labelbottom=False, labelleft=False) ax_active.tick_params(axis='both', which='both', bottom=False, left=False, labelbottom=False, labelleft=False) # save fig name = 'random_vs_al_example' file_name = os.path.join(plot_path, (name + '.png')) plt.savefig(file_name, format='png', dpi=300)