def field(domain, z, title, clim = None, path=None, save=True, show = False, ics=1, ext='.png', cmap=plt.cm.jet): """ Given a domain, plot the nodal value z :param domain: :class:`~polyadcirc.run_framework.domain` :param z: :class:`np.array` :param string title: plot title :param clim: :class:`np.clim` :type path: string or None :param path: directory to store plots :type save: boolean :param save: flag for whether or not to save plots :type show: boolean :param show: flag for whether or not to show plots :param int ics: polar coordinate option (1 = cart coords, 2 = polar coords) :param string ext: file extension """ if path == None: path = os.getcwd() plt.figure() plt.tripcolor(domain.triangulation, z, shading='gouraud', cmap=cmap) plt.gca().set_aspect('equal') plt.autoscale(tight=True) if clim: plt.clim(clim[0], clim[1]) add_2d_axes_labels(ics) colorbar() #plt.title(title) save_show(path+'/figs/'+title, save, show, ext)
def plotISVar(): plt.figure() plt.title('Variance minimization problem (call).\nVertical lines mark the minima.') for K in [0.6, 0.8, 1.0, 1.2]: theta = np.linspace(-0.6, 2) var = [BS.exactCallVar(K*s0, theta) for theta in theta] minth = theta[np.argmin(var)] line, = plt.plot(theta, var, label=str(K)) plt.axvline(minth, color=line.get_color()) plt.xlabel(r'$\theta$') plt.ylabel('call variance') plt.legend(title=r'$K/s_0$', loc='upper left') plt.autoscale(tight=True) plt.figure() plt.title('Variance minimization problem (put).\nVertical lines mark the minima.') for K in [0.8, 1.0, 1.2, 1.4]: theta = np.linspace(-2, 0.5) var = [BS.exactPutVar(K*s0, theta) for theta in theta] minth = theta[np.argmin(var)] line, = plt.plot(theta, var, label=str(K)) plt.axvline(minth, color=line.get_color()) plt.xlabel(r'$\theta$') plt.ylabel('put variance') plt.legend(title=r'$K/s_0$', loc='upper left') plt.autoscale(tight=True)
def plot_models(x, y, models, fname, mx=None, ymax=None, xmin=None): plt.clf() plt.scatter(x, y, s=10) plt.title("Web traffic over the last month") plt.xlabel("Time") plt.ylabel("Hits/hour") plt.xticks([w * 7 * 24 for w in range(10)], ["week %i" % w for w in range(10)]) if models: if mx is None: mx = sp.linspace(0, x[-1], 1000) for model, style, color in zip(models, linestyles, colors): # print "Model:",model # print "Coeffs:",model.coeffs plt.plot(mx, model(mx), linestyle=style, linewidth=2, c=color) plt.legend(["d=%i" % m.order for m in models], loc="upper left") plt.autoscale(tight=True) plt.ylim(ymin=0) if ymax: plt.ylim(ymax=ymax) if xmin: plt.xlim(xmin=xmin) plt.grid(True, linestyle="-", color="0.75")
def plot_k_walls(k_walls, plot_range=None, plot_data_points=False,): """ Plot K-walls for debugging purpose. """ pyplot.figure() pyplot.axes().set_aspect('equal') for k_wall in k_walls: xs = k_wall.get_xs() ys = k_wall.get_ys() pyplot.plot(xs, ys, '-', label=k_wall.identifier) if(plot_data_points == True): pyplot.plot(xs, ys, 'o', color='k', markersize=4) if plot_range is None: pyplot.autoscale(enable=True, axis='both', tight=None) else: [[x_min, x_max], [y_min, y_max]] = plot_range pyplot.xlim(x_min, x_max) pyplot.ylim(y_min, y_max) mpldatacursor.datacursor( formatter='{label}'.format, hover=True, ) pyplot.show()
def plotSpectogramF0Segments(x, fs, w, N, H, f0, segments): """ Code for plotting the f0 contour on top of the spectrogram """ # frequency range to plot maxplotfreq = 1000.0 fontSize = 16 fig = plt.figure() ax = fig.add_subplot(111) mX, pX = stft.stftAnal(x, fs, w, N, H) #using same params as used for analysis mX = np.transpose(mX[:,:int(N*(maxplotfreq/fs))+1]) timeStamps = np.arange(mX.shape[1])*H/float(fs) binFreqs = np.arange(mX.shape[0])*fs/float(N) plt.pcolormesh(timeStamps, binFreqs, mX) plt.plot(timeStamps, f0, color = 'k', linewidth=5) for ii in range(segments.shape[0]): plt.plot(timeStamps[segments[ii,0]:segments[ii,1]], f0[segments[ii,0]:segments[ii,1]], color = '#A9E2F3', linewidth=1.5) plt.autoscale(tight=True) plt.ylabel('Frequency (Hz)', fontsize = fontSize) plt.xlabel('Time (s)', fontsize = fontSize) plt.legend(('f0','segments')) xLim = ax.get_xlim() yLim = ax.get_ylim() ax.set_aspect((xLim[1]-xLim[0])/(2.0*(yLim[1]-yLim[0]))) plt.autoscale(tight=True) plt.show()
def plot(self, y, *args, **kwargs): ''' Plot something vs this frequency This plots whatever is given vs. `self.f_scaled` and then calls `labelXAxis`. ''' try: if len(shape(y))>2: # perhapsthe dimensions are empty, try to squeeze it down y= y.squeeze() if len(shape(y))>2: # the dimensions are full, so lets loop and plot each for m in range(shape(y)[1]): for n in range(shape(y)[2]): self.plot(y[:,m,n], *args, **kwargs) return if len(y)==len(self): pass else: raise IndexError(['thing to plot doesn\'t have same' ' number of points as f']) except(TypeError): y = y*npy.ones(len(self)) plot(self.f_scaled, y,*args, **kwargs) autoscale(axis='x', tight=True) self.labelXAxis()
def imshow_active_cells(grid, values, var_name=None, var_units=None, grid_units=(None, None), symmetric_cbar=False, cmap='pink'): """ .. deprecated:: 0.6 Use :meth:`imshow_active_cell_grid`, above, instead. """ data = values.view() data.shape = (grid.shape[0]-2, grid.shape[1]-2) y = np.arange(data.shape[0]) - grid.dx * .5 x = np.arange(data.shape[1]) - grid.dx * .5 if symmetric_cbar: (var_min, var_max) = (data.min(), data.max()) limit = max(abs(var_min), abs(var_max)) limits = (-limit, limit) else: limits = (None, None) plt.pcolormesh(x, y, data, vmin=limits[0], vmax=limits[1], cmap=cmap) plt.gca().set_aspect(1.) plt.autoscale(tight=True) plt.colorbar() plt.xlabel('X (%s)' % grid_units[1]) plt.ylabel('Y (%s)' % grid_units[0]) if var_name is not None: plt.title('%s (%s)' % (var_name, var_units)) plt.show()
def plot_hmm(self, container): from leaparticulator.notebooks.StreamlinedDataAnalysisGhmm import plot_hmm, unpickle_results indexes = self.log_selection.selectedRows() phase = self.log_model.itemFromIndex(indexes[0]).text() unit = container.cmbUnit.currentText() xlabel, ylabel, process_data = self._format_axes_and_labels(unit, reversed=container.chkReversed.isChecked(), multivariate=container.chkMultivariate.isChecked()) ax = container.figure.gca() if container.chkClear.isChecked(): ax.hold(False) # create an axis ax = container.figure.add_subplot(111) ax.cla() # which phase are we interested in? # first, ask the user which HMM they want to plot import fnmatch fname_to_logid = lambda x: os.path.split(x)[-1].split(".exp.log")[0] print self.current_fname, fname_to_logid(self.current_fname) matches = [] base_id = fname_to_logid(self.current_fname) query = '%s*phase%d*.%s.hmms' % (base_id, int(phase), unit) print "Searching for filename format: %s" % query self.update_status("Searching %s recursively for HMM files associated with this trajectory..." % self.dir) for root, dirnames, filenames in os.walk(self.dir, followlinks=True): for filename in fnmatch.filter(filenames, query): matches.append(os.path.join(root, filename)) hmm_file = self.pick_hmm_file(matches) # unpickle the HMMs self.update_status("Unpickling the HMMs...") results = unpickle_results(hmm_file) hmm = results.hmms[0] print "Initial BIC:", hmm.bic,"in %d HMMs" % len(results.hmms) for hmm_ in results.hmms: if hmm.bic > hmm_.bic: hmm = hmm_ # hmm = results.hmms[2] print "Final BIC:", hmm.bic # # now for the actual plotting # # create an axis # ax = container.figure.add_subplot(111) # # # discards the old graph ax.hold(True) r = plot_hmm(hmm.means, hmm.transmat, hmm.variances, hmm.initProb, axes=ax, clr=constants.kelly_colors, prob_lists=container.chkAnnotate.isChecked(), transition_arrows=container.chkArrows.isChecked()) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) plt.autoscale(True) container.canvas.draw() container.hmm = hmm container.hmm_file = hmm_file self.update_status("Loaded HMMS from %s" % hmm_file) return r
def draw_methods(argmts,da_method): methods,ys,yerrs,x,lookfor_pair = argmts fig, ax = plt.subplots(figsize=(12,8)) index = np.arange(len(x)) markers = ['.','x']*(len(methods)/2) i = 0 # print index,[len(y) for y in ys] for y in ys: #yerr=yerrs[i] plt.errorbar(index,y,marker= markers[i],alpha=opacity,label=convert(methods[i]),mew=3,linewidth=3.0,markersize=10) i += 1 plt.xticks(index,x) plt.title(lookfor_pair+': '+da_method,size=22) plt.xlabel('$\\lambda$',size=22) plt.ylabel('Accuracy',size=22) # bottom box # box = ax.get_position() # ax.set_position([box.x0, box.y0 + box.height * 0.1,box.width, box.height * 0.9]) # ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1), # fancybox=True, shadow=True, ncol=5) # plt.show() plt.autoscale() plt.ylim([57,90]) plt.savefig('%s:%s-acc.png'%(lookfor_pair,da_method)) pass
def plot_subj_mean(self, x, y, out_fname='', density_cmap=cm.spectral_r, density_alpha=0.5): pyplot.autoscale(enable=False) self.fig = plt.figure(figsize=(3., 3.), dpi=150.) self.ax = self.fig.add_axes([0.1, 0.125, 0.75, 0.75])#, axisbg='w') self.ax_cb = self.fig.add_axes([0.87, 0.15, 0.025, 0.72]) self.ax.xaxis.set_ticks_position('bottom') self.ax.yaxis.set_ticks_position('left') this_max = N.max(N.array(N.max(x.reshape(-1)), N.max(y.reshape(-1)))) this_min = N.min(N.array(N.min(x.reshape(-1)), N.min(y.reshape(-1)))) self.ax.plot(N.arange(this_min,this_max), N.arange(this_min,this_max), 'b:') #add 1 to 1 line self.ax.plot(N.arange(this_min,this_max), self.odr_slope*N.arange(this_min,this_max)+self.odr_intercept, 'g--', lw=1.5, alpha=0.9) #plot regression self.ax.plot(N.arange(this_min,this_max), N.zeros(len(N.arange(this_min,this_max))), 'k--', lw=0.5, alpha=0.9) #add y = 0 self.ax.plot(N.zeros(len(N.arange(this_min,this_max))), N.arange(this_min,this_max), 'k--', lw=0.5, alpha=0.9) #add x = 0 #plot 2d-density cmap_min = 1. #cmap_min = N.min(sorted_map_improve_array) cmap_max = 100. #cmap_max = N.max(sorted_map_improve_array) cmap_range = cmap_max - cmap_min cmap_norm = mpl.colors.Normalize(vmin=cmap_min, vmax=cmap_max) cmap = density_cmap #cm.spectral_r self.ax.hexbin(x.reshape(-1), y.reshape(-1), norm=cmap_norm, cmap=density_cmap, alpha=density_alpha, mincnt=1) #vmin=1, vmax=100, self.cb = mpl.colorbar.ColorbarBase(self.ax_cb, cmap=cmap, norm=cmap_norm, orientation='vertical') odr_txt_summary = 'odr slope: %.3f, se: %.3f \nodr intercept: %.3f, se: %.3f' % (self.odr_slope, self.odr_out.sd_beta[0], self.odr_intercept, self.odr_out.sd_beta[1]) odr_fp = FontProperties(family='Arial', weight='normal', size=8) #tick properties # self.fig.text(0.3, 0.2, odr_txt_summary, fontproperties=odr_fp) #write ODR summary #import pdb; pdb.set_trace() if len(out_fname)>0: self.fig.savefig(out_fname, dpi=300) print("Results have been printed to "+ out_fname) else: self.fig.show()
def plot_different_versions(repo_name, pre_fetch_size, distance_to_fetch, fig_name=None): """Sample plot of several different repos.""" x = range(100) legend = [] fig, ax = plt.subplots() for version in version_color: csv_reader = _file_to_csv( version=version, repo_name=repo_name, pre_fetch_size=pre_fetch_size, distance_to_fetch=distance_to_fetch) y = get_column(csv_reader, 'hit_rate') if y is not None: plt.plot(x, y, color=version_color[version]) line = mlines.Line2D( [], [], label=version, color=version_color[version], linewidth=2) legend.append(line) plt.title('Different version outputs of %s.git' % (repo_name,), y=1.02) plt.legend(handles=legend, loc=4) plt.ylabel('hit rate') plt.xlabel('cache size (%)') legend_text = 'pfs = %s, dtf = %s\ncommit_num = %s' % ( pre_fetch_size, distance_to_fetch, REPO_DATA[repo_name]['commit_num']) text(0.55, 0.07, legend_text, ha='center', va='center', transform=ax.transAxes, multialignment='left', bbox=dict(alpha=1.0, boxstyle='square', facecolor='white')) plt.grid(True) plt.autoscale(False) plt.show()
def kmeans_plot(): global g_train_set global g_test_set global g_centroids # 以0,1,3特征绘制3D图 my_set = g_train_set; plot.scatter(my_set[0 :25,0],my_set[0: 25,1],marker="x",color="k") plot.scatter(my_set[25:50,0],my_set[25:50,1],marker="x",color="m") plot.scatter(my_set[50:75,0],my_set[50:75,1],marker="x",color="c") plot.scatter(my_set[0 :25,2],my_set[0: 25,3],marker="o",color="k") plot.scatter(my_set[25:50,2],my_set[25:50,3],marker="o",color="m") plot.scatter(my_set[50:75,2],my_set[50:75,3],marker="o",color="c") my_set = g_test_set; plot.scatter(my_set[0 :25,0],my_set[0: 25,1],marker="x",color="k") plot.scatter(my_set[25:50,0],my_set[25:50,1],marker="x",color="m") plot.scatter(my_set[50:75,0],my_set[50:75,1],marker="x",color="c") plot.scatter(my_set[0 :25,2],my_set[0: 25,3],marker="o",color="k") plot.scatter(my_set[25:50,2],my_set[25:50,3],marker="o",color="m") plot.scatter(my_set[50:75,2],my_set[50:75,3],marker="o",color="c") plot.xlabel(Config.LABEL_NAMES[0]+"/"+Config.LABEL_NAMES[2]) plot.ylabel(Config.LABEL_NAMES[1]+"/"+Config.LABEL_NAMES[3]) plot.autoscale(tight=True) plot.grid() plot.show()
def plot_layer(self, layer): layer = {k: v for k, v in layer.iteritems() if k in self.VALID_AES} layer.update(self.manual_aes) x = layer.pop('x') if 'weight' not in layer: counts = pd.value_counts(x) labels = counts.index.tolist() weights = counts.tolist() else: weights = layer.pop('weight') if not isinstance(x[0], Timestamp): labels = x else: df = pd.DataFrame({'weights':weights, 'timepoint': pd.to_datetime(x)}) df = df.set_index('timepoint') ts = pd.TimeSeries(df.weights, index=df.index) ts = ts.resample('W', how='sum') ts = ts.fillna(0) weights = ts.values.tolist() labels = ts.index.to_pydatetime().tolist() indentation = np.arange(len(labels)) + 0.2 width = 0.35 idx = np.argsort(labels) labels, weights = np.array(labels)[idx], np.array(weights)[idx] labels = sorted(labels) plt.bar(indentation, weights, width, **layer) plt.autoscale() return [ {"function": "set_xticks", "args": [indentation+width/2]}, {"function": "set_xticklabels", "args": [labels]} ]
def plot_bar_chart(x_axis,freq,unit,category,rota,font,path): plt.figure(figsize=(26,15)) plt.bar(range(len(freq)),freq, align='center',width=1,color='r') plt.xticks(range(len(x_axis)),x_axis,size='small',fontsize=font,rotation=rota)#size='small' plt.yticks(fontsize=font) if unit=="Ratio": plt.xlabel("VIDEO") plt.ylabel('RATIO (%)') axes = plt.gca() axes.set_ylim([0,100]) else: plt.xlabel(unit) plt.ylabel('Frequency (# Videos)') plt.title("Histogram of "+category+" : "+unit) plt.autoscale(True) plt.get_scale_names() plt.grid(True) figManager = plt.get_current_fig_manager() figManager.window.showMaximized() plt.savefig(path,papertype='letter',orientation='landscape') plt.show() plt.close() return
def create_2_plots_v(mic1, mic2, title1, title2, xlabel1, ylabel1, xlabel2, ylabel2, colormap): fig, axes = plt.subplots(2, sharex=True) mic_array = array(mic1) dimensions = mic_array.shape nx = dimensions[0] ny = dimensions[1] axes[0].imshow(mic1, extent=(0, nx, ny, 0),cmap=cm.get_cmap(colormap)) axes[0].set_title(title1) axes[0].set_xlabel(xlabel1) axes[0].set_ylabel(ylabel1) axes[0].set_ylim([0,ny]) mic_array = array(mic2) dimensions = mic_array.shape nx = dimensions[0] ny = dimensions[1] print(nx, ny) axes[1].imshow(mic2, extent=(0, nx, ny, 0),cmap=cm.get_cmap(colormap)) axes[1].set_title(title2) axes[1].set_xlabel(xlabel2) axes[1].set_ylabel(ylabel2) axes[1].set_ylim([0,ny]) plt.axis('tight') plt.autoscale(enable=True, axis='y', tight=True) plt.show()
def plot_forecasts(data, legend_data, save=False, filename=None): '''Plot data using predefined styles suitable for black and white document data - list of pandas Series representing the forecasts and/or actual data legend_data - list of strings representing the display names for the elements of data. save(boolean) - if True then plot is saved as filename filename(string) - absolute path to name of file where plot is saved Note: data and legend_data must be ordered such that legend_data[i] represents the display name of data[i]''' if len(data) == 2: plot_styles = ['ks--', 'ko-'] elif len(data) == 3: plot_styles = ['ko-.', 'ks--', 'ko-'] elif len(data) == 4: plot_styles = ['ko-', 'ks--', 'ko-.', 'ko:'] for ds, sty, name in zip(data, plot_styles, legend_data): ds.plot(style=sty, markersize=3.0, label=name) legend(loc='best') plt.autoscale() plt.grid(False) plt.ylabel("System Price (EUR/MWh)") if save is True: plt.savefig(filename) else: plt.show()
def generate_graph(): with open('../../data/netinterface.dat', 'r') as csvfile: data_source = csv.reader(csvfile, delimiter=' ', skipinitialspace=True) for row in data_source: # [0] column is a time column # Convert to datetime data type a = datetime.strptime((row[0]),'%H:%M:%S') x.append((a)) # The remaining columns contain data r_kb.append(row[4]) s_kb.append(row[5]) # Plot lines plt.plot(x,r_kb, label='Kilobytes received per second', color='#009973', antialiased=True) plt.plot(x,s_kb, label='Kilobytes sent per second', color='#b3b300', antialiased=True) # Graph properties plt.xlabel('Time',fontstyle='italic') plt.ylabel('Kb/s',fontstyle='italic') plt.title('Network statistics') plt.grid(linewidth=0.4, antialiased=True) plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.18), ncol=2, fancybox=True, shadow=True) plt.autoscale(True) # Graph saved to PNG file plt.savefig('../../graphs/netinterface.png', bbox_inches='tight')
def _prepare_world(self): world = self.simulation.reality.world self.g = nx.Graph() g = self.g g.add_nodes_from(world.points) edgelist = [ (edge.a_end.point, edge.b_end.point, min(edge.pheromone_level(), 1)) for edge in world.edges ] g.add_weighted_edges_from(edgelist) if self.fig is not None: self.fig.clear() else: self.fig = plt.figure(figsize=(24, 13.5), dpi=100) # figsize is in inches. x, y. plt.autoscale(enable=True, axis='both', tight=True) normal_points = {point: point.coordinates for point in world.points if not point.is_anthill() and not point.is_foodpoint()} nx.draw_networkx_nodes(g, pos=normal_points, nodelist=normal_points.keys(), node_color='w', node_size=self.NODE_SIZE) food_points = {point: point.coordinates for point in world.get_food_points()} nx.draw_networkx_nodes(g, pos=food_points, nodelist=food_points.keys(), node_color='g', node_size=self.NODE_SIZE, label='food') anthills = {point: point.coordinates for point in world.get_anthills()} nx.draw_networkx_nodes(g, pos=anthills, nodelist=anthills.keys(), node_color='b', node_size=self.NODE_SIZE, label='anthill') self.all_points = {point: point.coordinates for point in world.points}
def timePlt (X,x1=None, x2=None, y1=None, y2=None,srate=None): """ this is a plotter for the time domain ---------------- Variables: X = incomming array of amplitude data srate = the sample rate from the data being alalysed, for accurate time ---------------- Returns : none, it just plots the data ---------------- """ if (srate == None): srate = 44100 figsize = (12,4) seconds = (len(X)/srate) time = np.linspace(0, seconds, num=len(X)) plt.plot(time,X) plt.title("Sample's Waveform") plt.autoscale(tight=True) plt.ylabel('Amplitude') plt.xlabel('Seconds') plt.axis([x1,x2,y1,y2]) plt.show() return 0
def graph_samples(data): # no idea how to read the data so lets graph them # Ok, channel zero looks like a ecg :) plt.plot(range(len(data)), data) plt.xlabel('time in 1/256 s (3.90625 milliseconds )') plt.autoscale(enable=True) plt.show()
def animate(i): global xar, yar, ymin, ymax #gets the course currencies.refresh() y = currencies.get_rate() x = datetime.datetime.now() #makes the y axis larger if needed if float(y) > ymax: ymax = float(y)+0.0005 if float(y) < ymin: ymin = float(y) - 0.0005 print str(x) +": " + str(y) xar.append(x) yar.append(float(y)) if len(xar) > 240: del xar[0] del yar[0] ax1.clear() ax1.yaxis.grid(True) ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H.%M.%S')) ax1.plot(xar,yar) plt.autoscale('both') plt.gcf().autofmt_xdate() ax1.set_ylim([ymin,ymax]) plt.title("Course right now: "+str(y))
def construct_operators(n): normalized_differences = get_eigenvalue_differences(n) plt.hist(normalized_differences, color="red", bins=100, lw=5, histtype='step', edgecolor="red", normed=1) plt.autoscale() plt.xlim(0, 3) plt.gca().xaxis.set_minor_locator(MultipleLocator(0.1)) plt.gca().yaxis.set_minor_locator(MultipleLocator(0.1)) plt.tick_params(which='major', width=5, length=25, labelsize=70) plt.tick_params(which='minor', width=3, length=15) plt.xlabel("Normalized Zero Difference", labelpad=50) plt.ylabel("Normalized Frequency", labelpad=50) plt.gcf().set_size_inches(30, 24, forward=1) plt.savefig("plots/qm.pdf") plt.clf() return normalized_differences
def plot_compare_paths(pos_rigid, pos_inertial): fig, ax = plt.subplots(1) plt.plot(*zip(*pos_rigid), color='r', label='rigid', alpha=0.8) plt.plot(*zip(*pos_inertial), color='g', label='inertial', alpha=0.8) ax.legend(loc='upper right') p1 = Circle(pos_rigid[0], radius=ax.get_ylim()[0] / 50., fill=True, color='r', alpha=0.8) p2 = Circle(pos_inertial[0], radius=ax.get_ylim()[0] / 50., fill=True, color='g', alpha=0.8) for p in [p1, p2]: ax.add_patch(p) plt.title("Both paths") plt.axis('equal') plt.xlabel('x') plt.ylabel('y') plt.autoscale() if Plotter.live is True: plt.show() else: plt.savefig('plot/path compare.png') plt.close()
def output(portfolio): """ Output """ printer={} for key, value in portfolio.items(): X=[] for subkey, subvalue in value.items(): X.append(subvalue) std = np.std(X) #standard deviation mean = np.mean(X) #average printer[key]=std, mean plt.figure() plt.hist(X, 100, range=[-1,1]) plt.xlabel("The probability to win or lose") plt.title('thk301 - The histogram of the result $%s' %key) plt.ylabel('The number of trials') plt.autoscale(False, tight=True) if key==1000: plt.savefig('histogram_1000_pos.png') elif key==100: plt.savefig('histogram_0100_pos.png') elif key==10: plt.savefig('histogram_0010_pos.png') elif key==1: plt.savefig('histogram_0001_pos.png') else: plt.savefig('histogram_%s_pos.png' %key) file = open("results.txt", "w") #save the "printer" dictionary for key, value in printer.items(): file.write( "$%d\n" %key) file.write("Standard Deviation:%f\n"%value[0]) file.write( "Mean:%f\n\n"%value[1])
def _fractions_grid(data, dom_x, dom_y, title, case_id, plot_dir): ''' Plot diagnostic plots of fraction variables ''' # ---------------------------------------------------------------- # # Plot Fractions pfname = _make_filename(title, case_id, plot_dir) mask = data <= 0.0 data = np.ma.array(data, mask=mask) cmap = matplotlib.cm.cool cmap.set_bad(color='w') fig = plt.figure() plt.pcolormesh(data, cmap=cmap) plt.autoscale(tight=True) plt.axis('tight') plt.colorbar() plt.title(title) plt.xlabel('x') plt.ylabel('y') plt.ylim([0, dom_y.shape[0]]) plt.xlim([0, dom_x.shape[1]]) fig.savefig(pfname) plt.close() # ---------------------------------------------------------------- # return pfname
def plot_m(mesh, npy, comp='x', based=None): if comp == 'x': cmpi = 0 elif comp == 'y': cmpi = 1 elif comp == 'z': cmpi = 2 else: raise Exception('Seems the given component is wrong!!!') data = np.load(npy) if based is not None: data = data - based data.shape = (-1, 3) m = data[:,cmpi] nx = mesh.nx ny = mesh.ny nz = mesh.nz m.shape = (nz, ny, nx) m2 = m[0,:,:] fig = plt.figure() # norm=color.Normalize(-1,1) plt.imshow(m2, aspect=1, cmap=plt.cm.coolwarm, origin='lower', interpolation='none') plt.autoscale(False) plt.xticks([]) plt.yticks([]) fig.savefig('%s_%s.png' % (npy[:-4], comp))
def _show_order_info(problem, mesh_sizes, stabilization): """Performs consistency check for the given problem/method combination and show some information about it. Useful for debugging. """ errors, hmax = _compute_errors(problem, mesh_sizes, stabilization) order = helpers.compute_numerical_order_of_convergence(hmax, errors) # Print the data print() print("hmax ||u - u_h|| conv. order") print("{:e} {:e}".format(hmax[0], errors[0])) for j in range(len(errors) - 1): print(32 * " " + "{:2.5f}".format(order[j])) print("{:e} {:e}".format(hmax[j + 1], errors[j + 1])) # Plot the actual data. plt.loglog(hmax, errors, "-o") # Compare with order curves. plt.autoscale(False) e0 = errors[0] for order in range(4): plt.loglog( [hmax[0], hmax[-1]], [e0, e0 * (hmax[-1] / hmax[0]) ** order], color="0.7" ) plt.xlabel("hmax") plt.ylabel("||u-u_h||") plt.show() return
def plot_measurements(xs, ys=None, color='k', lw=2, label='Measurements', lines=False, **kwargs): """ Helper function to give a consistant way to display measurements in the book. """ plt.autoscale(tight=True) '''if ys is not None: plt.scatter(xs, ys, marker=marker, c=c, s=s, label=label, alpha=alpha) if connect: plt.plot(xs, ys, c=c, lw=1, alpha=alpha) else: plt.scatter(range(len(xs)), xs, marker=marker, c=c, s=s, label=label, alpha=alpha) if connect: plt.plot(range(len(xs)), xs, lw=1, c=c, alpha=alpha)''' if lines: if ys is not None: plt.plot(xs, ys, color=color, lw=lw, ls='--', label=label, **kwargs) else: plt.plot(xs, color=color, lw=lw, ls='--', label=label, **kwargs) else: if ys is not None: plt.scatter(xs, ys, edgecolor=color, facecolor='none', lw=2, label=label, **kwargs) else: plt.scatter(range(len(xs)), xs, edgecolor=color, facecolor='none', lw=2, label=label, **kwargs)
def quick_plt_loc(robot, map): plt.imshow(map, cmap='gray') plt.autoscale(False) a = np.asarray(robot.past_loc) plt.scatter(a[:,1], a[:,0]) plt.plot(a[:,1], a[:,0]) plt.show()
def plot_min_eigenvalue_vs_N(): N_s = [] minimum_eigenvalues = [] for i in range(5, 101): N_s.append(i) minimum_eigenvalues.append( min([ l for l in np.abs(get_eigenvalues(i)) if l > 1e-5 ] ) ) plt.plot(N_s, minimum_eigenvalues, lw=5, color="green") plt.xlabel("Matrix Size, $N$", labelpad=30, fontsize=70) plt.ylabel("Min. Eigenvalue", labelpad=30, fontsize=70) plt.autoscale() plt.gca().xaxis.set_minor_locator(MultipleLocator(10)) plt.gca().yaxis.set_minor_locator(MultipleLocator(0.05)) plt.tick_params(which='major', width=5, length=25, labelsize=70) plt.tick_params(which='minor', width=3, length=15) plt.gcf().set_size_inches(30, 24, forward=1) plt.savefig("plots/qm_min_eigenvalues.pdf") plt.clf()
def FitSP_Peak(h, b1, c, ext, ppos): global cf a0 = float(h.split()[5]) a1 = float(h.split()[6]) #cf=a1 a2 = float(h.split()[7]) if "spe" in ext: b = b1 else: b = [return_energy(b1[i], a0, a1, a2) for i in range(len(b1))] print "pars: ", a0, " ", a1, " ", a2 b2 = numpy.array(b) c2 = numpy.array(c) cond = (b2 >= ppos - 70) & (b2 <= ppos + 70) #b = b2[ cond ] #c = c2[ cond ] ofilename = OUTDIR + "/" + filename_data + ".png" sh_marker = [ "+", ".", "^", "*", "p", "s", "x", "D", "h", "o", "+", ".", "^", "*", "p", "s", "x", "D", "h", "o", "+", ".", "^", "*", "p", "s" ] sh_color = [ 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'orange', 'darkblue', 'darkgreen', 'darkred', 'darkcyan', 'darkmagenta', 'deeppink', 'firebrick', 'gold', 'darkviolet', 'lavenderblush', 'dodgerblue', 'indigo', 'limegreen' ] #gmodel = Model(gaussian) + Model(line) #gmodel = Model(gaussian) + Model(expo) gmodel = Model(fit_function) gmodel.set_param_hint('A', value=10000) gmodel.set_param_hint('beta', value=0.001) gmodel.set_param_hint('B', value=500) gmodel.set_param_hint('mu', value=ppos) gmodel.set_param_hint('sigma', value=20) pars = gmodel.make_params() result = gmodel.fit(c, pars, x=b) print(result.fit_report()) #print result.params cfo = ppos * a1 / result.params["mu"].value mval = result.params["mu"].value sval = result.params["sigma"].value print "%6.2f %6.4f %8.6f -> %8.6f" % (mval, sval, a1, ppos * a1 / result.params["mu"].value) if bFix: cf = cfo else: cf = a1 print "%6.2f %6.4f %8.6f -> %8.6f : %8.6f (%4.2f %% off)" % ( mval, sval, a1, cf, cfo, 100 * (cf - cfo) / cfo) energy = [return_energy(b1[i], a0, cf, a2) for i in range(len(b1))] fig = plt.gcf() plt.title(h, fontsize=14, fontweight='bold') if "spe" in ext: plt.xlabel('Energy ', fontsize=14, fontweight='bold') else: plt.xlabel('Bins ', fontsize=14, fontweight='bold') plt.ylabel('Counts ', fontsize=14, fontweight='bold') plt.tick_params(labelsize=18) #plt.text(energyBins[binid][0]+10, 0.8*maxv2,strv,fontsize=14,fontweight='bold') plt.plot(b, c, marker=sh_marker[0], linestyle='None', color=sh_color[0], label=filename_data) plt.plot(b, result.best_fit, 'r-', label="fit %6.2f %6.4f" % (mval, sval)) plt.xlim([mval - 5 * sval, mval + 5 * sval]) legend = plt.legend(loc='upper right', shadow=True) frame = legend.get_frame() frame.set_facecolor('0.90') for label in legend.get_texts(): label.set_fontsize('large') for label in legend.get_lines(): label.set_linewidth(1.5) # the legend line width plt.grid(True) #plt.yscale('log') plt.autoscale(enable=True, axis='y') fig.set_size_inches(16, 12) plt.savefig(ofilename, dpi=100) print "Output file ->", ofilename plt.show() return energy
class Robot: def __init__(self, x, y, t): self.x = x self.y = y self.t = t def mover(self, d): self.x += d * math.cos(self.t) self.y += d * math.sin(self.t) def girar(self, dt): self.t += (dt * math.pi) / 180.0 def dibujar(self): plot.scatter(self.x, self.y) r = Robot(10, 2, 0) plot.ion() while True: #plot.clf() plot.axis([-100, 100, -100, 100]) plot.autoscale(False) r.mover(1) r.girar(2) r.dibujar() plot.pause(0.001) #r.log()
# calculate segment endpoints of Poisson line process xx1 = xx0 + p * cos_theta + q * sin_theta yy1 = yy0 + p * sin_theta - q * cos_theta xx2 = xx0 + p * cos_theta - q * sin_theta yy2 = yy0 + p * sin_theta + q * cos_theta ###END Simulate a Poisson line process on a disk END### ### START Plotting ###START # draw circle t = np.linspace(0, 2 * np.pi, 200) xp = xx0 + r * np.cos(t) yp = yy0 + r * np.sin(t) fig, ax = plt.subplots() ax.plot(xp, yp, color='k') plt.xlabel('x') plt.ylabel('y') plt.axis('equal') plt.autoscale(enable=True, axis='x', tight=True) plt.axis('off') # plot segments of Poisson line process # need to create a list to plot the segments (probably a better way to do this) segments = [] # initialize list segments = [[(xx1[i], yy1[i]), (xx2[i], yy2[i])] for i in range(numbLines)] lc = mc.LineCollection(segments, colors='b') ax.add_collection(lc) # plot segments plt.show() ###END Plotting END###
def sineModelAnalEnhanced( inputFile='../../sounds/sines-440-602-transient.wav'): """ Input: inputFile (string): wav file including the path Output: tStamps: A Kx1 numpy array of time stamps at which the frequency components were estimated tfreq: A Kx2 numpy array of frequency values, one column per component """ phaseDevThres = 1e-2 # Allowed deviation in phase M = 2047 # window size N = 4096 # FFT size t = -80 # threshold in negative dB H = 128 # hop-size window = 'blackman' # window type fs, x = UF.wavread(inputFile) # Read input file w = get_window(window, M) # Get the window hM1 = int(np.floor( (w.size + 1) / 2)) # half analysis window size by rounding hM2 = int(np.floor(w.size / 2)) # half analysis window size by floor x = np.append( np.zeros(hM2), x) # add zeros at beginning to center first window at sample 0 x = np.append(x, np.zeros(hM2)) # add zeros at the end to analyze last sample pin = hM1 # initialize sound pointer in middle of analysis window pend = x.size - hM1 # last sample to start a frame tStamps = np.arange(pin, pend, H) / float(fs) # Generate time stamps w = w / sum(w) # normalize analysis window tfreq = np.array([]) while pin < pend: # while input sound pointer is within sound x1 = x[pin - hM1:pin + hM2] # select frame mX, pX = SM.DFT.dftAnal(x1, w, N) # compute dft ploc = UF.peakDetection(mX, t) # detect locations of peaks ###### CODE DIFFERENT FROM sineModelAnal() ######### # Phase based mainlobe tracking plocSelMask = np.zeros(len(ploc)) for pindex, p in enumerate(ploc): if p > 2 and p < ( len(pX) - 2 ): # Peaks at either end of the spectrum are not processed if selectFlatPhasePeak( pX, p, phaseDevThres ): # Select the peak if the phase spectrum around the peak is flat plocSelMask[pindex] = 1 else: plocSelMask[pindex] = 1 plocSel = ploc[plocSelMask.nonzero()[0]] # Select the ones chosen if len(plocSel ) != 2: # Ignoring frames that don't return two selected peaks ipfreq = [0.0, 0.0] else: iploc, ipmag, ipphase = UF.peakInterp( mX, pX, plocSel ) # Only selected peaks to refine peak values by interpolation ipfreq = fs * iploc / float(N) # convert peak locations to Hertz ###### CODE DIFFERENT FROM sineModelAnal() ######### if pin == hM1: # if first frame initialize output frequency track tfreq = ipfreq else: # rest of frames append values to frequency track tfreq = np.vstack((tfreq, ipfreq)) pin += H # Plot the estimated frequency tracks mX, pX = stft.stftAnal(x, w, N, H) maxplotfreq = 1500.0 binFreq = fs * np.arange(N * maxplotfreq / fs) / N numFrames = int(mX[:, 0].size) frmTime = H * np.arange(numFrames) / float(fs) plt.pcolormesh(frmTime, binFreq, np.transpose(mX[:, :N * maxplotfreq / fs + 1]), cmap='hot_r') plt.plot(tStamps, tfreq[:, 0], color='y', linewidth=2.0) plt.plot(tStamps, tfreq[:, 1], color='c', linewidth=2.0) plt.legend(('Estimated f1', 'Estimated f2')) plt.xlabel('Time (s)') plt.ylabel('Frequency (Hz)') plt.autoscale(tight=True) return tStamps, tfreq
k1 = 200 # [number of feature, maximum size of the feature] real_pram = [15, 15] obstacle_pram = [45, 30] fake_pram = [7, 15] # 5.5 = 2000 pixel at dpi = 200. May scale up as demanded myDpi = 400 pic_edge_len = 5.5 mpl.rcParams['figure.dpi'] = myDpi fig = plt.figure(figsize=(pic_edge_len, pic_edge_len), dpi=myDpi, facecolor='w') ax = fig.add_axes([0, 0, 1, 1]) plt.autoscale(False, tight=True) plt.xlim(0, k1) plt.ylim(0, k1) def load_feature(feature, shape='circle', target=None, feature_type='gradient'): if target is None: target = real() for dot in feature: if feature_type == 'gradient': num_steps = dot[2] * 2 # We used equal stride for the gradient in this case.
datapoints[360.0] = datapoints[0.0] channels = [37, 38, 39] angles = sorted(list(datapoints.keys())) distances = sorted(list(datapoints[0.0].keys())) ids = __get_ids(datapoints) avgs = [[[[ st.mean([ dp["rssi"] for dp in datapoints[angle][dist] if dp["channel"] == ch and dp["tag_id"] == id ]) for angle in angles ] for ch in channels] for dist in distances] for id in ids] plt.subplots_adjust(left=0.1, right=0.95, top=0.93, bottom=0.1) for id in avgs: for dist in id: for ch in dist: plt.plot(angles, ch) plt.title("Signal Strength by Channel and Distance") plt.xlabel("Angle (degrees)") plt.ylabel("RSSI (dBm)") plt.margins(y=0.1) # plt.legend(["Tag {}, {} cm, ch {}".format(i, d, c) for i in ids for d in distances for c in channels], # loc="center left", bbox_to_anchor=(1, 0.5), fontsize=8) plt.autoscale(tight=True) for angle in [0, 90, 180, 270, 360]: plt.axvline(x=angle, color='grey') plt.savefig(args.chart) # plt.show()
def plotAshInv(json_data, unit="tg", colormap='default', usetex=True, plotsum=True, prune=True, orientation='vertical', axis_date_format="%d %b\n%H:%M", dpi=200, fig_width=6, fig_height=4, vmax=None, r_vmax=2.0, **kwargs): def npTimeToDatetime(np_time): return datetime.datetime.utcfromtimestamp((np_time - np.datetime64('1970-01-01T00:00')) / np.timedelta64(1, 's')) def npTimeToStr(np_time, fmt="%Y-%m-%d %H:%M"): return npTimeToDatetime(np_time).strftime(fmt) if (unit == 'tg'): pass elif (unit == 'kg/(m*s)'): # The setup is to emit 1 tg over three hours for each level # Converting this to kg / (m * s) duration = np.diff(json_data['emission_times'])/np.timedelta64(1, 's') assert(np.all(duration == duration[0])) duration = duration[0] json_data['a_priori'] = json_data['a_priori']/(json_data['level_heights'][:,None]*duration)*1.0e9 json_data['a_posteriori'] = json_data['a_posteriori']/(json_data['level_heights'][:,None]*duration)*1.0e9 else: raise "Unknown unit {:s}".format(unit) colors = [ (0.0, (1.0, 1.0, 0.8)), (0.05, (0.0, 1.0, 0.0)), (0.4, (0.9, 1.0, 0.2)), (0.6, (1.0, 0.0, 0.0)), (1.0, (0.6, 0.2, 1.0)) ] if (colormap == 'alternative'): colors = [ (0.0, (1.0, 1.0, 0.6)), (0.4, (0.9, 1.0, 0.2)), (0.6, (1.0, 0.8, 0.0)), (0.7, (1.0, 0.4, 0.0)), (0.8, (1.0, 0.0, 0.0)), (0.9, (1.0, 0.2, 0.6)), (1.0, (0.6, 0.2, 1.0)) ] elif (colormap == 'birthe'): colors = [ ( 0/35, ("#ffffff")), ( 4/35, ("#b2e5f9")), (13/35, ("#538fc9")), (18/35, ("#47b54c")), (25/35, ("#f5e73c")), (35/35, ("#df2b24")) ] elif (colormap == 'stohl'): colors = [ ( 0.00/10, ("#ffffff")), ( 0.35/10, ("#ffe5e2")), ( 0.60/10, ("#b1d9e6")), ( 1.00/10, ("#98e8a8")), ( 2.00/10, ("#fffc00")), ( 5.00/10, ("#ff0d00")), (10.00/10, ("#910000")) ] cm = LinearSegmentedColormap.from_list('ash', colors, N=256) cm.set_bad(alpha = 0.0) if (orientation == 'horizontal'): fig, axs = plt.subplots(nrows=1, ncols=4, figsize=(4*fig_width,fig_height), dpi=dpi) elif (orientation == 'vertical'): fig, axs = plt.subplots(nrows=4, ncols=1, figsize=(fig_width,4*fig_height), dpi=dpi) #Create x ticks and y ticks x_ticks = np.arange(0, json_data['emission_times'].size) x_labels = [npTimeToStr(t, fmt=axis_date_format) for t in json_data['emission_times']] y_ticks = np.arange(-0.5, json_data['level_heights'].size+0.5) y_labels = ["{:.0f}".format(a) for a in np.cumsum(np.concatenate(([json_data['volcano_altitude']], json_data['level_heights'])))] #Subsample x ticks / y ticks x_ticks = x_ticks[3::8] x_labels = x_labels[3::8] y_ticks = y_ticks[::2] y_labels = y_labels[::2] y_max = max(1.0e-10, 1.3*max(json_data['a_priori'].sum(axis=0).max(), json_data['a_posteriori'].sum(axis=0).max())) if (vmax is None): vmax = max(json_data['a_priori'].max(), json_data['a_posteriori'].max()) # First subfigure (a priori) plt.sca(axs[0]) plt.title("A priori ({:s})".format(unit)) plt.imshow(json_data['a_priori'], aspect='auto', interpolation='none', origin='lower', cmap=cm, vmin=0.0, vmax=vmax) plt.colorbar(orientation='horizontal', pad=0.15) plt.xticks(ticks=x_ticks, labels=x_labels, rotation=0, horizontalalignment='center', usetex=usetex) plt.yticks(ticks=y_ticks, labels=y_labels, usetex=usetex) if (plotsum): plt.sca(axs[0].twinx()) plt.autoscale(False) plt.plot(json_data['a_priori'].sum(axis=0), 'kx--', linewidth=2, alpha=0.5, label='A priori') plt.ylim(0, y_max) plt.grid() plt.legend() #Second subfigure (a posteriori) plt.sca(axs[1]) plt.title("Inverted ({:s})".format(unit)) plt.imshow(json_data['a_posteriori'], aspect='auto', interpolation='none', origin='lower', cmap=cm, vmin=0.0, vmax=vmax) plt.colorbar(orientation='horizontal', pad=0.15) plt.xticks(ticks=x_ticks, labels=x_labels, rotation=0, horizontalalignment='center', usetex=usetex) plt.yticks(ticks=y_ticks, labels=y_labels, usetex=usetex) if (plotsum): plt.sca(axs[1].twinx()) plt.autoscale(False) plt.plot(json_data['a_priori'].sum(axis=0), 'kx--', linewidth=2, alpha=0.5, label='A priori') plt.plot(json_data['a_posteriori'].sum(axis=0), 'ko-', fillstyle='none', label='Inverted') plt.ylim(0, y_max) plt.grid() plt.legend() #Third subfigure (difference) norm = TwoSlopeNorm(vmin=-1.0, vcenter=0, vmax=r_vmax) #norm = SymLogNorm(linthresh=0.1, vmin=-1.0, vmax=r_vmax, base=10) #norm = MidpointLogNorm(lin_thres=0.01, lin_scale=1.0, vmin=-1.0, vmax=10, base=10) diff = (json_data['a_posteriori']-json_data['a_priori']) / json_data['a_priori'] plt.sca(axs[2]) plt.title("(Inverted - A priori) / A priori") plt.imshow(diff, aspect='auto', interpolation='none', origin='lower', cmap='bwr', norm=norm) plt.colorbar(orientation='horizontal', pad=0.15) plt.xticks(ticks=x_ticks, labels=x_labels, rotation=0, horizontalalignment='center', usetex=usetex) plt.yticks(ticks=y_ticks, labels=y_labels, usetex=usetex) #Fourth subfigure (convergence) plt.sca(axs[3]) plt.title("Convergence / residual") plt.plot(json_data['convergence'], 'r-', linewidth=2, label='Convergence') plt.xlabel("Iteration") plt.sca(axs[3].twinx()) plt.plot(json_data['residual'], 'b-', linewidth=2, label='Residual') plt.xlabel("Iteration") plt.legend() #Set tight layout to minimize overlap #plt.tight_layout() #plt.subplots_adjust(top=0.9) return fig
def tracking_analysis_for_attribute_cutoff_bootstrapped(cycle_stats_df, category_stats, attribute, cutoff_criteria, cutoff, n_bootstrapping, save_dir): ''' Function that computes tracking analysis per group, based on bootstrapping It computes the Kolmogorov-Smirnov tests between group distributions It computes the likelihood in low, mid and high extremes of the metric Input: cycle_stats_df: pandas dataframe, with information about user's cycle statistics category_stats: pandas dataframe, with information about user's tracking statistics for a given category attribute: what specific tracking attribute to study: i.e., concatenation of the metric and the symptom to analyze cutoff_criteria: what statistic to use for separating users into groups ('cycle_lengths' for paper) cutoff: what statistic cutoff value to use for separating users into groups (9 for paper) n_bootstrapping: Number of bootstrapped samples to use for the analysis save_dir: path where to save plot Output: true_statistics: Dictionary with statistics for the observed cohort: {'KS', 'p_val', 'prob_values_high', 'prob_values_low', 'ratios'} bootstrap_statistics: Dictionary with statistics for the bootstrapped cohort: {'KS': mean of the bootstrapped KS values, 'KS_0025': 2.5 percentile of the bootstrapped KS values, 'KS_0975': 97.5 percentile of the bootstrapped KS values, 'p_val': mean of the bootstrapped p_val values, 'p_val_0025': 2.5 percentile of the bootstrapped p_val values, 'p_val_0975': 97.5 percentile of the bootstrapped p_val values, 'prob_values_high': mean of the boostrapped probability values for the high volatility group, 'prob_values_high_0025': 2.5 percentile of the boostrapped probability values for the high volatility group, 'prob_values_high_0975': 97.5 percentile of the boostrapped probability values for the high volatility group, 'prob_values_low': mean of the boostrapped probability values for the low volatility group, 'prob_values_low_0025': 2.5 percentile of the boostrapped probability values for the low volatility group, 'prob_values_low_0975': 97.5 percentile of the boostrapped probability values for the low volatility group, 'ratios': mean of the bootstrapped ratios for the high to low volability groups 'ratios_0025': 2.5 percentile of the bootstrapped ratios for the high to low volability groups 'ratios_0975': 97.5 percentile of the bootstrapped ratios for the high to low volability groups} ''' ### Define # Extreme likelihood ranges extreme_bins=np.array([0,0.05,0.95,1]) # Histogram type, color and labels hist_type='step' colors = ['orange', 'c'] labels=['Highly variable', 'NOT highly variable'] # True separation of users into groups all_users=np.unique(cycle_stats_df['user_id']) true_users_greater_than_cutoff = np.unique(cycle_stats_df[cycle_stats_df[cutoff_criteria] > cutoff]['user_id']) true_users_less_than_cutoff = np.unique(cycle_stats_df[cycle_stats_df[cutoff_criteria] <= cutoff]['user_id']) n_users_greater_than_cutoff=true_users_greater_than_cutoff.size n_users_less_than_cutoff=true_users_less_than_cutoff.size true_category_stats_users_greater_than_cutoff = category_stats[category_stats['user_id'].isin(true_users_greater_than_cutoff)] true_category_stats_users_less_than_cutoff = category_stats[category_stats['user_id'].isin(true_users_less_than_cutoff)] # Analysis for proportion of cycles metric if attribute.startswith('proportion_cycles_'): ########### TRUE OBSERVERD STATISTICS ########## # KS true_KS, true_p_val = stats.ks_2samp(true_category_stats_users_greater_than_cutoff[attribute].dropna(), true_category_stats_users_less_than_cutoff[attribute].dropna()) # Counts on extremes true_extreme_counts_greater_than_cutoff, bins_greater_than_cutoff = np.histogram(true_category_stats_users_greater_than_cutoff[attribute].dropna(), bins=extreme_bins, density=True) true_extreme_counts_less_than_cutoff, bins_less_than_cutoff = np.histogram(true_category_stats_users_less_than_cutoff[attribute].dropna(), bins=extreme_bins, density=True) # Probability values true_prob_values_high=np.array([(true_extreme_counts_greater_than_cutoff[0]*0.05), (true_extreme_counts_greater_than_cutoff[1]*0.9), (true_extreme_counts_greater_than_cutoff[2]*0.05)]) true_prob_values_low=np.array([(true_extreme_counts_less_than_cutoff[0]*0.05), (true_extreme_counts_less_than_cutoff[1]*0.9), (true_extreme_counts_less_than_cutoff[2]*0.05)]) # Ratios true_ratios=np.array([true_prob_values_high[0]/true_prob_values_low[0], true_prob_values_high[1]/true_prob_values_low[1], true_prob_values_high[2]/true_prob_values_low[2]]) # CDF # Auto bins based on integer range of values counts_greater_than_cutoff, bins_greater_than_cutoff = np.histogram(true_category_stats_users_greater_than_cutoff[attribute].dropna(), bins='auto', density=True) counts_less_than_cutoff, bins_less_than_cutoff = np.histogram(true_category_stats_users_less_than_cutoff[attribute].dropna(), bins='auto', density=True) all_bins=np.setdiff1d(bins_less_than_cutoff,bins_greater_than_cutoff) true_counts_greater_than_cutoff, bins_greater_than_cutoff = np.histogram(true_category_stats_users_greater_than_cutoff[attribute].dropna(), bins=all_bins, density=True) true_counts_less_than_cutoff, bins_less_than_cutoff = np.histogram(true_category_stats_users_less_than_cutoff[attribute].dropna(), bins=all_bins, density=True) ########### BOOTSTRAP BASED STATISTICS ########## # Computed suff statistics bootstrapped_KS=np.zeros(n_bootstrapping) bootstrapped_p_val=np.zeros(n_bootstrapping) bootstrapped_prob_values_high=np.zeros((n_bootstrapping, extreme_bins.size-1)) bootstrapped_prob_values_low=np.zeros((n_bootstrapping, extreme_bins.size-1)) bootstrapped_ratios=np.zeros((n_bootstrapping, extreme_bins.size-1)) bootstrapped_counts_greater_than_cutoff=np.zeros((n_bootstrapping, all_bins.size-1)) bootstrapped_counts_less_than_cutoff=np.zeros((n_bootstrapping, all_bins.size-1)) for n_bootstrap in np.arange(n_bootstrapping): #print('Sample={}/{}'.format(n_bootstrap,n_bootstrapping)) # Bootstrapped sample indicators users_greater_than_cutoff=np.random.choice(true_users_greater_than_cutoff,n_bootstrapping) users_less_than_cutoff=np.random.choice(true_users_less_than_cutoff,n_bootstrapping) # Bootstrapped data category_stats_users_greater_than_cutoff = category_stats[category_stats['user_id'].isin(users_greater_than_cutoff)] category_stats_users_less_than_cutoff = category_stats[category_stats['user_id'].isin(users_less_than_cutoff)] # KS bootstrapped_KS[n_bootstrap], bootstrapped_p_val[n_bootstrap] = stats.ks_2samp(category_stats_users_greater_than_cutoff[attribute].dropna(), category_stats_users_less_than_cutoff[attribute].dropna()) # Counts on extremes counts_greater_than_cutoff, bins_greater_than_cutoff = np.histogram(category_stats_users_greater_than_cutoff[attribute].dropna(), bins=extreme_bins, density=True) counts_less_than_cutoff, bins_less_than_cutoff = np.histogram(category_stats_users_less_than_cutoff[attribute].dropna(), bins=extreme_bins, density=True) # Probability values bootstrapped_prob_values_high[n_bootstrap]=np.array([(counts_greater_than_cutoff[0]*0.05), (counts_greater_than_cutoff[1]*0.9), (counts_greater_than_cutoff[2]*0.05)]) bootstrapped_prob_values_low[n_bootstrap]=np.array([(counts_less_than_cutoff[0]*0.05), (counts_less_than_cutoff[1]*0.9), (counts_less_than_cutoff[2]*0.05)]) # Ratios bootstrapped_ratios[n_bootstrap]=bootstrapped_prob_values_high[n_bootstrap]/bootstrapped_prob_values_low[n_bootstrap] # CDF, based on same bins as for true CDF bootstrapped_counts_greater_than_cutoff[n_bootstrap], bins_greater_than_cutoff = np.histogram(category_stats_users_greater_than_cutoff[attribute].dropna(), bins=all_bins, density=True) bootstrapped_counts_less_than_cutoff[n_bootstrap], bins_less_than_cutoff = np.histogram(category_stats_users_less_than_cutoff[attribute].dropna(), bins=all_bins, density=True) else: raise ValueError('Analysis for attribute {} not implemented'.format(attribute)) # Print bootstrap results print('*************************************************************************') print('******** {0} KS={1:.3f} (p={2}) ***********'.format( attribute, true_KS, true_p_val )) print('******** {0} Bootstrapped KS={1:.3f}+/-{2:.3f} (p={3} (+/-{4}))***********'.format( attribute, bootstrapped_KS.mean(), bootstrapped_KS.std(), bootstrapped_p_val.mean(), bootstrapped_p_val.std() )) print('******** {0} Bootstrapped KS={1:.3f}({2:.3f},{3:.3f}) p={4} ({5},{6}))***********'.format( attribute, bootstrapped_KS.mean(), np.percentile(bootstrapped_KS, 2.5, axis=0), np.percentile(bootstrapped_KS, 97.5, axis=0), bootstrapped_p_val.mean(), np.percentile(bootstrapped_p_val, 2.5, axis=0), np.percentile(bootstrapped_p_val, 97.5, axis=0) )) print('Bins \t\t\t & p < 0.05 \t\t & 0.05 \leq p < 0.95 \t & 0.95 \leq 1') print('True ratio \t\t & {0:.3f} \t\t & {1:.3f} \t\t & {2:.3f}'.format(true_ratios[0],true_ratios[1],true_ratios[2])) print('Bootstrapped ratio \t & {0:.3f}+/-{1:.3f} \t & {2:.3f}+/-{3:.3f} \t & {4:.3f}+/-{5:.3f}'.format( bootstrapped_ratios.mean(axis=0)[0],bootstrapped_ratios.std(axis=0)[0], bootstrapped_ratios.mean(axis=0)[1],bootstrapped_ratios.std(axis=0)[1], bootstrapped_ratios.mean(axis=0)[2],bootstrapped_ratios.std(axis=0)[2] )) print('Bootstrapped ratio \t & {0:.3f} ({1:.3f}, {2:.3f}) \t & {3:.3f} ({4:.3f}, {5:.3f}) \t & {6:.3f} ({7:.3f}, {8:.3f})'.format( bootstrapped_ratios.mean(axis=0)[0], np.percentile(bootstrapped_ratios[:,0], 2.5, axis=0), np.percentile(bootstrapped_ratios[:,0], 97.5, axis=0), bootstrapped_ratios.mean(axis=0)[1], np.percentile(bootstrapped_ratios[:,1], 2.5, axis=0), np.percentile(bootstrapped_ratios[:,1], 97.5, axis=0), bootstrapped_ratios.mean(axis=0)[2], np.percentile(bootstrapped_ratios[:,2], 2.5, axis=0), np.percentile(bootstrapped_ratios[:,2], 97.5, axis=0) )) # CDF plotting # First normalize counts bootstrapped_pdf_greater_than_cutoff=bootstrapped_counts_greater_than_cutoff/bootstrapped_counts_greater_than_cutoff.sum(axis=1, keepdims=True) bootstrapped_pdf_less_than_cutoff=bootstrapped_counts_less_than_cutoff/bootstrapped_counts_less_than_cutoff.sum(axis=1, keepdims=True) # Plot (no bootstrap) # True plt.hist(all_bins[:-1], all_bins, weights=true_counts_greater_than_cutoff, density=True, cumulative=True, color=colors[0], histtype=hist_type) plt.hist(all_bins[:-1], all_bins, weights=true_counts_less_than_cutoff, density=True, cumulative=True, color=colors[1], histtype=hist_type) # Polish and close plt.autoscale(enable=True, tight=True) plt.xlabel('$\lambda_s$') plt.ylabel('$P(\lambda_s \leq \lambda)$') # Custom legend custom_lines = [Line2D([0], [0], color=colors[0], lw=2), Line2D([0], [0], color=colors[1], lw=2), Line2D([0], [0], color='w', lw=4)] plt.legend(custom_lines, ['Highly variable', 'NOT highly variable', 'KS = {:.3f}'.format(true_KS)], loc='upper left') filename = '{}/{}_cdf.pdf'.format(save_dir, attribute) plt.savefig(filename, format='pdf', bbox_inches='tight') plt.close() # Plot (with bootstrap) # True plt.hist(all_bins[:-1], all_bins, weights=true_counts_greater_than_cutoff, density=True, cumulative=True, color=colors[0], histtype=hist_type) plt.hist(all_bins[:-1], all_bins, weights=true_counts_less_than_cutoff, density=True, cumulative=True, color=colors[1], histtype=hist_type) # Bootstrapped plt.plot(all_bins[:-1], bootstrapped_pdf_greater_than_cutoff.cumsum(axis=1).mean(axis=0), ':', color=colors[0]) plt.plot(all_bins[:-1], bootstrapped_pdf_less_than_cutoff.cumsum(axis=1).mean(axis=0), ':', color=colors[1]) plt.fill_between(all_bins[:-1], np.percentile(bootstrapped_pdf_greater_than_cutoff.cumsum(axis=1), 2.5, axis=0), np.percentile(bootstrapped_pdf_greater_than_cutoff.cumsum(axis=1), 97.5, axis=0), alpha=0.3, facecolor=colors[0]) plt.fill_between(all_bins[:-1], np.percentile(bootstrapped_pdf_less_than_cutoff.cumsum(axis=1), 2.5, axis=0), np.percentile(bootstrapped_pdf_less_than_cutoff.cumsum(axis=1), 97.5, axis=0), alpha=0.3, facecolor=colors[1]) # Polish and close plt.autoscale(enable=True, tight=True) plt.xlabel('$\lambda_s$') plt.ylabel('$P(\lambda_s \leq \lambda)$') # Custom legend custom_lines = [Line2D([0], [0], color=colors[0], lw=2), Line2D([0], [0], color=colors[1], lw=2), Line2D([0], [0], color='w', lw=4)] plt.legend(custom_lines, ['Highly variable', 'NOT highly variable', 'KS = {:.3f}'.format(true_KS)], loc='upper left') filename = '{}/{}_bcdf.pdf'.format(save_dir, attribute) plt.savefig(filename, format='pdf', bbox_inches='tight') plt.close() # True statistics true_statistics={'KS':true_KS, 'p_val':true_p_val, 'prob_values_high':true_prob_values_high, 'prob_values_low':true_prob_values_low, 'ratios':true_ratios} bootstrap_statistics={ 'KS':bootstrapped_KS.mean(), 'KS_0025':np.percentile(bootstrapped_KS, 2.5, axis=0), 'KS_0975':np.percentile(bootstrapped_KS, 97.5, axis=0), 'p_val':bootstrapped_p_val.mean(), 'p_val_0025':np.percentile(bootstrapped_p_val, 2.5, axis=0), 'p_val_0975':np.percentile(bootstrapped_p_val, 97.5, axis=0), 'prob_values_high':bootstrapped_prob_values_high.mean(axis=0), 'prob_values_high_0025':np.percentile(bootstrapped_prob_values_high, 2.5, axis=0), 'prob_values_high_0975':np.percentile(bootstrapped_prob_values_high, 97.5, axis=0), 'prob_values_low':bootstrapped_prob_values_low.mean(axis=0), 'prob_values_low_0025':np.percentile(bootstrapped_prob_values_low, 2.5, axis=0), 'prob_values_low_0975':np.percentile(bootstrapped_prob_values_low, 97.5, axis=0), 'ratios':bootstrapped_ratios.mean(axis=0), 'ratios_0025':np.percentile(bootstrapped_ratios, 2.5, axis=0), 'ratios_0975':np.percentile(bootstrapped_ratios, 97.5, axis=0) } return true_statistics, bootstrap_statistics
def plot_depths(self, row=None, col=None, ax=None): """ Plot the depths of a model grid along a row or column transect. If the bathymetry is known, it is plotted also. Parameters ---------- row : int, optional The row number to plot col : int, optional The column number to plot ax : matplotlib.axes, optional The axes to use for the figure Returns ------- ax : matplotlib.axes The axes containing the plot """ import matplotlib.pyplot as plt # Create the axes if we don't have any if not ax: fig = plt.figure() ax = fig.add_subplot(111) # ax.set_bg_color('darkseagreen') # Get the data if row: sz = np.s_[:, row, :] s = np.s_[row, :] x = self.lon_rho[s] label = "Longitude" elif col: sz = np.s_[:, :, col] s = np.s_[:, col] x = self.lat_rho[s] label = "Latitude" else: warn("You must specify a row or column") return # If it is ROMS, we should plot the top and bottom of the cells if self._isroms: sr, csr = seapy.roms.stretching(self.vstretching, self.theta_s, self.theta_b, self.hc, self.n, w_grid=True) dep = np.ma.masked_where( seapy.adddim(self.mask_rho[s], self.n + 1) == 0, seapy.roms.depth(self.vtransform, self.h[s], self.hc, sr, csr, w_grid=True)) else: dep = np.ma.masked_where( seapy.adddim(self.mask_rho[s], self.n) == 0, self.depth_rho[sz]) h = -self.h[s] # Begin with the bathymetric data ax.fill_between(x, h, np.min(h), facecolor="darkseagreen", interpolate=True) # Plot the layers ax.plot(x, dep.T, color="grey") # Labels ax.set_xlabel(label + " [deg]") ax.set_ylabel("Depth [m]") # Make it tight plt.autoscale(ax, tight=True) return ax
def main(inputFile=demo_sound_path('bendir.wav'), window='hamming', M=2001, N=2048, t=-80, minSineDur=0.02, maxnSines=150, freqDevOffset=10, freqDevSlope=0.001, stocf=0.2, interactive=True, plotFile=False): """ inputFile: input sound file (monophonic with sampling rate of 44100) window: analysis window type (rectangular, hanning, hamming, blackman, blackmanharris) M: analysis window size; N: fft size (power of two, bigger or equal than M) t: magnitude threshold of spectral peaks; minSineDur: minimum duration of sinusoidal tracks maxnSines: maximum number of parallel sinusoids freqDevOffset: frequency deviation allowed in the sinusoids from frame to frame at frequency 0 freqDevSlope: slope of the frequency deviation, higher frequencies have bigger deviation stocf: decimation factor used for the stochastic approximation """ # size of fft used in synthesis Ns = 512 # hop size (has to be 1/4 of Ns) H = 128 # read input sound (fs, x) = audio.read_wav(inputFile) # compute analysis window w = get_window(window, M) # perform sinusoidal+sotchastic analysis tfreq, tmag, tphase, stocEnv = sps.from_audio(x, fs, w, N, H, t, minSineDur, maxnSines, freqDevOffset, freqDevSlope, stocf) # synthesize sinusoidal+stochastic model y, ys, yst = sps.to_audio(tfreq, tmag, tphase, stocEnv, Ns, H, fs) # output sound file (monophonic with sampling rate of 44100) baseFileName = strip_file(inputFile) outputFileSines, outputFileStochastic, outputFile = [ 'output_sounds/%s_spsModel%s.wav' % (baseFileName, i) for i in ('_sines', '_stochastic', '') ] # write sounds files for sinusoidal, residual, and the sum audio.write_wav(ys, fs, outputFileSines) audio.write_wav(yst, fs, outputFileStochastic) audio.write_wav(y, fs, outputFile) # create figure to plot plt.figure(figsize=(12, 9)) # frequency range to plot maxplotfreq = 10000.0 # plot the input sound plt.subplot(3, 1, 1) plt.plot(np.arange(x.size) / float(fs), x) plt.axis([0, x.size / float(fs), min(x), max(x)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('input sound: x') plt.subplot(3, 1, 2) numFrames = int(stocEnv.shape[0]) sizeEnv = int(stocEnv.shape[1]) frmTime = H * np.arange(numFrames) / float(fs) binFreq = (.5 * fs) * np.arange(sizeEnv * maxplotfreq / (.5 * fs)) / sizeEnv plt.pcolormesh( frmTime, binFreq, np.transpose(stocEnv[:, :sizeEnv * maxplotfreq / (.5 * fs) + 1])) plt.autoscale(tight=True) # plot sinusoidal frequencies on top of stochastic component if (tfreq.shape[1] > 0): sines = tfreq * np.less(tfreq, maxplotfreq) sines[sines == 0] = np.nan numFrames = int(sines.shape[0]) frmTime = H * np.arange(numFrames) / float(fs) plt.plot(frmTime, sines, color='k', ms=3, alpha=1) plt.xlabel('time(s)') plt.ylabel('Frequency(Hz)') plt.autoscale(tight=True) plt.title('sinusoidal + stochastic spectrogram') # plot the output sound plt.subplot(3, 1, 3) plt.plot(np.arange(y.size) / float(fs), y) plt.axis([0, y.size / float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() if interactive: plt.show() if plotFile: plt.savefig('output_plots/%s_sps_model.png' % files.strip_file(inputFile))
def SpectrumComparisonNGeometryComparison(rownum, colnum, Figsize, model_name, boundary=[-1, 1, -1, 1]): """ Read the Prediction files and plot the spectra comparison plots :param SubplotArray: 2x2 array indicating the arrangement of the subplots :param Figsize: the size of the figure :param Figname: the name of the figures to save :param model_name: model name (typically a list of numebr containing date and time) """ mpl = ImportColorBarLib() #import lib Xtruth, Xpred, Ytruth, Ypred, Ymae, Ymse = RetrieveFeaturePredictionNMse( model_name) #retrieve features print("Ymse shape:", Ymse.shape) print("Xpred shape:", Xpred.shape) print("Xtrth shape:", Xtruth.shape) #Plotting the spectrum comaprison f = plt.figure(figsize=Figsize) fignum = rownum * colnum for i in range(fignum): ax = plt.subplot(rownum, colnum, i + 1) plt.ylabel('Transmission rate') plt.xlabel('frequency') plt.plot(Ytruth[i], label='Truth', linestyle='--') plt.plot(Ypred[i], label='Prediction', linestyle='-') plt.legend() plt.ylim([0, 1]) f.savefig('Spectrum Comparison_{}'.format(model_name)) """ Plotting the geometry comparsion, there are fignum points in each plot each representing a data point with a unique marker 8 dimension therefore 4 plots, 2x2 arrangement """ #for j in range(fignum): pointnum = fignum #change #fig to #points in comparison f = plt.figure(figsize=Figsize) ax0 = plt.gca() for i in range(4): truthmarkers = UniqueMarkers() #Get some unique markers predmarkers = UniqueMarkers() #Get some unique markers ax = plt.subplot(2, 2, i + 1) #plt.xlim([29,56]) #setting the heights limit, abandoned because sometime can't see prediciton #plt.ylim([41,53]) #setting the radius limits for j in range(pointnum): #Since the colored scatter only takes 2+ arguments, plot 2 same points to circumvent this problem predArr = [[Xpred[j, i], Xpred[j, i]], [Xpred[j, i + 4], Xpred[j, i + 4]]] predC = [Ymse[j], Ymse[j]] truthplot = plt.scatter(Xtruth[j, i], Xtruth[j, i + 4], label='Xtruth{}'.format(j), marker=next(truthmarkers), c='m', s=40) predplot = plt.scatter(predArr[0], predArr[1], label='Xpred{}'.format(j), c=predC, cmap='jet', marker=next(predmarkers), s=60) plt.xlabel('h{}'.format(i)) plt.ylabel('r{}'.format(i)) rect = mpl.patches.Rectangle((boundary[0], boundary[2]), boundary[1] - boundary[0], boundary[3] - boundary[2], linewidth=1, edgecolor='r', facecolor='none', linestyle='--', label='data region') ax.add_patch(rect) plt.autoscale() plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), mode="expand", ncol=6, prop={'size': 5}) #, bbox_to_anchor=(1,0.5)) cb_ax = f.add_axes([0.93, 0.1, 0.02, 0.8]) cbar = f.colorbar(predplot, cax=cb_ax) #f.colorbar(predplot) f.savefig('Geometry Comparison_{}'.format(model_name))
def vis_one_image(im, im_name, output_dir, boxes, segms=None, keypoints=None, thresh=0.9, kp_thresh=2, dpi=200, box_alpha=0.0, dataset=None, show_class=False, ext='pdf', thresh_lo=None, range_thresh=False, scale=None, classes=None): """Visual debugging of detections. :param range_thresh: :param thresh_lo: """ output_name = os.path.basename(im_name) + '.' + ext fig_path = os.path.join(output_dir, '{}'.format(output_name)) if os.path.isfile(fig_path): return if not os.path.exists(output_dir): try: os.makedirs(output_dir) except: pass if isinstance(boxes, list): boxes, segms, keypoints, classes = convert_from_cls_format( boxes, segms, keypoints) if range_thresh: if boxes is None or boxes.shape[0] == 0 or ( max(boxes[:, 4]) <= thresh_lo and min(boxes[:, 4]) > thresh): return elif boxes is None or boxes.shape[0] == 0 or max(boxes[:, 4]) < thresh: return dataset_keypoints, _ = keypoint_utils.get_keypoints() if segms is not None: masks = mask_util.decode(segms) if scale: im_size_max = max(masks.shape) if np.round(scale * im_size_max) > cfg.TRAIN.MAX_SIZE: scale = float(cfg.TRAIN.MAX_SIZE) / float(im_size_max) masks = cv2.resize(masks, None, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR) if len(masks.shape) == 2: masks = np.expand_dims(masks, -1) boxes = boxes * scale color_list = colormap(rgb=True) / 255 kp_lines = kp_connections(dataset_keypoints) cmap = plt.get_cmap('rainbow') colors = [cmap(i) for i in np.linspace(0, 1, len(kp_lines) + 2)] fig = plt.figure(frameon=False) fig.set_size_inches(im.shape[1] / dpi, im.shape[0] / dpi) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.axis('off') fig.add_axes(ax) ax.imshow(im) # Display in largest to smallest order to reduce occlusion areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) sorted_inds = np.argsort(-areas) mask_color_id = 0 for i in sorted_inds: bbox = boxes[i, :4] score = boxes[i, -1] if range_thresh: if not (thresh_lo < score <= thresh): continue elif score < thresh: continue # show box (off by default) ax.add_patch( plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False, edgecolor='b', linewidth=0.2, alpha=0.3)) if show_class: ax.text(bbox[0], bbox[1] - 2, get_class_string(classes[i], score, dataset), fontsize=2, family='serif', bbox=dict(facecolor='g', alpha=0.2, pad=0, edgecolor='none'), color='white') # show mask if segms is not None and len(segms) > i: img = np.ones(im.shape) color_mask = color_list[mask_color_id % len(color_list), 0:3] mask_color_id += 1 w_ratio = .1 for c in range(3): color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio for c in range(3): img[:, :, c] = color_mask[c] e = masks[:, :, i] _, contour, hier = cv2.findContours(e.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) for c in contour: polygon = Polygon(c.reshape((-1, 2)), fill=True, facecolor=color_mask, edgecolor='b', linewidth=0.2, alpha=0.2) ax.add_patch(polygon) # show keypoints if keypoints is not None and len(keypoints) > i: kps = keypoints[i] plt.autoscale(False) for l in range(len(kp_lines)): i1 = kp_lines[l][0] i2 = kp_lines[l][1] if kps[2, i1] > kp_thresh and kps[2, i2] > kp_thresh: x = [kps[0, i1], kps[0, i2]] y = [kps[1, i1], kps[1, i2]] line = plt.plot(x, y) plt.setp(line, color=colors[l], linewidth=1.0, alpha=0.7) if kps[2, i1] > kp_thresh: plt.plot(kps[0, i1], kps[1, i1], '.', color=colors[l], markersize=3.0, alpha=0.7) if kps[2, i2] > kp_thresh: plt.plot(kps[0, i2], kps[1, i2], '.', color=colors[l], markersize=3.0, alpha=0.7) # add mid shoulder / mid hip for better visualization mid_shoulder = ( kps[:2, dataset_keypoints.index('right_shoulder')] + kps[:2, dataset_keypoints.index('left_shoulder')]) / 2.0 sc_mid_shoulder = np.minimum( kps[2, dataset_keypoints.index('right_shoulder')], kps[2, dataset_keypoints.index('left_shoulder')]) mid_hip = (kps[:2, dataset_keypoints.index('right_hip')] + kps[:2, dataset_keypoints.index('left_hip')]) / 2.0 sc_mid_hip = np.minimum( kps[2, dataset_keypoints.index('right_hip')], kps[2, dataset_keypoints.index('left_hip')]) if (sc_mid_shoulder > kp_thresh and kps[2, dataset_keypoints.index('nose')] > kp_thresh): x = [mid_shoulder[0], kps[0, dataset_keypoints.index('nose')]] y = [mid_shoulder[1], kps[1, dataset_keypoints.index('nose')]] line = plt.plot(x, y) plt.setp(line, color=colors[len(kp_lines)], linewidth=1.0, alpha=0.7) if sc_mid_shoulder > kp_thresh and sc_mid_hip > kp_thresh: x = [mid_shoulder[0], mid_hip[0]] y = [mid_shoulder[1], mid_hip[1]] line = plt.plot(x, y) plt.setp(line, color=colors[len(kp_lines) + 1], linewidth=1.0, alpha=0.7) fig.savefig(fig_path, dpi=dpi) plt.close('all')
def get_cone(self, radius_mask, output_filename=None, cone_filename=None): """find the center of a camera's pixel. The pixel image along with important parameter can be save for future use using the cone_filename parameter. Parameter --------- radius_mask : radius of the mask used for extracting pixels from image (see plot_cones()) output_filename : optional, path where to put the resulting cone image. cone_filename : optional, name of the fits file where to save the cone image along with important parameters. """ if type(radius_mask) is not float: raise AttributeError('radius_mask must be a float.') mask_fft = self.get_fft_mask(radius=radius_mask) image_cones = np.real(np.fft.ifft2(self.fft_image_cones * mask_fft)) center_image = (np.array(image_cones.shape[::-1]) - 1) / 2 if self.center_fitted is None: center_tested = center_image else: center_tested = self.center_fitted points = np.array((center_tested + self.r1, center_tested + self.r2, center_tested + self.r3, center_tested - self.r1, center_tested - self.r2, center_tested - self.r3)) bounds = ((np.min(points[:, 0]), np.max(points[:, 0])), (np.min(points[:, 1]), np.max(points[:, 1]))) offsets = (0 * self.r1, (self.r1 + self.r2) / 3, self.r1, self.r2, (self.r2 + self.r3) / 3, self.r3, (self.r3 - self.r1) / 3, -self.r1, (-self.r1 - self.r2) / 3, -self.r2, (-self.r2 - self.r3) / 3, -self.r3, (-self.r3 + self.r1) / 3) results = [] for i, offset in enumerate(offsets): res = optimize.minimize(get_neg_hexagonalicity_with_mask, center_tested + offset, args=(image_cones, self.r1, self.r2, (60, 300)), bounds=bounds, method='TNC', options={ 'disp': False, 'eps': 1, 'xtol': 1e-1, 'maxiter': 200 }) results.append(res) print('fit', i + 1, '/', len(offsets), 'at', res.x, 'done: hex=', -res.fun) if -res.fun > 0.85: break print('refine fit') hex_results = np.array([-res.fun for res in results]) pos_results = np.array([res.x for res in results]) # fine fit for best result res = optimize.minimize(get_neg_hexagonalicity_with_mask, pos_results[np.argmax(hex_results)], args=(image_cones, 6 * self.r1, 6 * self.r2, (60, 120, 180, 240, 300)), bounds=bounds, method='TNC', options={ 'disp': False, 'eps': .1, 'xtol': 1e-5, 'maxiter': 2000 }) if -res.fun > 0.75: self.center_fitted = res.x hex_result = -res.fun else: print('WARNING: hexagonalicity is small.') results.append(res) hex_results = np.array([-res.fun for res in results]) pos_results = np.array([res.x for res in results]) self.center_fitted = pos_results[np.argmax(hex_results)] hex_result = hex_results[np.argmax(hex_results)] print('pixel center found: ', self.center_fitted, 'with hex=', hex_result) print(self.center_fitted - center_image, 'px from center') points_fitted = points - center_tested + np.array(self.center_fitted) pixels_x_min = int(np.floor(np.min(points_fitted[:, 0]))) pixels_x_max = int(np.ceil(np.max(points_fitted[:, 0]))) + 1 pixels_y_min = int(np.floor(np.min(points_fitted[:, 1]))) pixels_y_max = int(np.ceil(np.max(points_fitted[:, 1]))) + 1 # plot_image(image_cones) image_crop = image_cones[pixels_y_min:pixels_y_max, pixels_x_min:pixels_x_max] # plot_image(image_crop) center_crop = (np.array(self.center_fitted) - np.array( (pixels_x_min, pixels_y_min))) mask_hexa = np.zeros_like(image_crop) mask_hexa = set_hexagon(mask_hexa, center=center_crop, r1=self.r1, r2=self.r2) self.image_cone = image_crop * mask_hexa if cone_filename is not None: hdu = fits.PrimaryHDU(self.image_cone) hdu.header['center'] = ( self.center_fitted[0] + 1j * self.center_fitted[1], 'fitted position (in original image) of the hexagon center') hdu.header['ks1'] = (self.ks[0, 0] + 1j * self.ks[0, 1], ( '1st vector of the base of the hexagonal lattice' ' in reciprocal space')) hdu.header['ks2'] = (self.ks[1, 0] + 1j * self.ks[1, 1], ( '2nd vector of the base of the hexagonal lattice ' 'in reciprocal space')) hdu.header['v1'] = ( self.v1_lattice[0] + 1j * self.v1_lattice[1], 'spacing between 2 hexagons along the 1st axis') hdu.header['v2'] = ( self.v2_lattice[0] + 1j * self.v2_lattice[1], 'spacing between 2 hexagons along the 2nd axis') hdu.header['v3'] = ( self.v3_lattice[0] + 1j * self.v3_lattice[1], 'spacing between 2 hexagons along the 3rd axis') hdu.header['r1'] = (self.r1[0] + 1j * self.r1[1], '1st radius of the hexagon') hdu.header['r2'] = (self.r2[0] + 1j * self.r2[1], '2nd radius of the hexagon') hdu.header['r3'] = (self.r3[0] + 1j * self.r3[1], '3rd radius of the hexagon') hdu.writeto(cone_filename, overwrite=True) print('cone saved to ', cone_filename) if output_filename is not None: plt.ioff() fig = plt.figure(figsize=(8, 6), dpi=600) ax = plt.gca() plt.imshow(self.image_cone, cmap='gray') plt.autoscale(False) plt.plot(self.center_fitted[0] - pixels_x_min, self.center_fitted[1] - pixels_y_min, 'y+') plt.grid(None) plt.axis('off') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.savefig(output_filename, bbox_inches='tight', pad_inches=0) plt.close(fig) print(output_filename, 'saved.')
def fit(self, X, y): start_time = time.time() times = [] # store the x min and max for normalization self.x_min = np.min(X, axis=0, keepdims=True) self.x_max = np.max(X, axis=0, keepdims=True) #normalize X X = self.normalize_X(X) # store the x min and max for normalization self.y_min = np.min(y, axis=0, keepdims=True) self.y_max = np.max(y, axis=0, keepdims=True) #normalize X y = self.normalize_Y(y) #store x and y for use in loss calculation self.x = X self.y = y # initiaize variables #Dimension d = X.shape[1] #w = [random.uniform(0, 1)] * (d) # initialize w vector to have dimension D #weights w = np.random.uniform(0, 1, d) w_0 = random.uniform(0, 1) w = np.append(w, w_0) #diff diff = 1 #gradient if self.options is 'sto': gradient = grad(self.sto_loss_function) else: gradient = grad(self.loss_function) t = 0 obj_val = [] #Delta check for loss function ''' delta = np.random.uniform(0,1,d+1) * 1e-5 print("Gradient Check") print(self.loss_function(w+delta) - self.loss_function(w)) print(np.matmul(gradient(w).transpose(),delta)) ''' while (diff > 1e-4): w_prev = w.copy() for i in range(self.steps): t += 1 # variant step size #np.append(obj_val, self.loss_function(w)) w = w - ((0.1 / (1000 * (1 + t))) * gradient(w)) #w -=self.step_size * gradient(w) times.append(time.time() - start_time) obj_val.append(self.loss_function(w)) diff = (1 / (1 + d)) * np.sum(np.abs(w - w_prev)) print(diff) #store weights self.w = w val = range(len(obj_val)) # Objective function plotting plt.xlabel("iterations") plt.ylabel("cost") plt.title("Objective Curve Stochastic") plt.legend() plt.plot(val, obj_val) plt.autoscale() plt.show()
cpuErrorPercent = (1.*cpuErrorRate/iterations)*100 gpuErrorPercent = (1.*gpuErrorRate/iterations)*100 mismatchErrorPercent = (1.*mismatchErrorRate/iterations)*100 gpuRuntime[pathIter] /= iterations cpuRuntime[pathIter] /= iterations print("For paths creating %s, cpuErrorPercentage: %f, gpuErrorPercentage: %f, cpu-gpu Mismatch ErrorPercentage: %f" % (path, cpuErrorPercent, gpuErrorPercent, mismatchErrorPercent)) # Plot plt.gcf() for iteration,path in enumerate(move_path): plt.plot(iteration, cpuRuntime[iteration], marker='o', markersize=3, color='red', label="CPU_"+path) plt.plot(iteration, gpuRuntime[iteration], marker='o', markersize=3, color='green', label="GPU_"+path) plt.legend(loc='best') plt.xticks(range(len(move_path)),move_path) plt.xlabel("Path Type") plt.ylabel('RunTime (s)') plt.title("pythonCPU RunTime vs CUDA GPU RunTime") plt.gca().set_ylim([0,max(max(cpuRuntime),max(gpuRuntime))]) plt.autoscale() plt.tight_layout() plt.ticklabel_format(axis='y',style='sci') # ax.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter('%.2e')) plt.savefig('pythonCPU_gpuCUDA_runtime_plot.png',bbox_inches='tight') plt.close()
def plot_camera_geometry(self, output_filename='cones-presence-filtered.png'): """ Plot the lid CCD image along with the camera geometry: - blue cross are shown for each of the detected pixel which match the best camera geometry found. - yellow cross are shown for each of the detected pixel which does not match the best camera geometry found. - green cross are shown for each pixel of the best camera geometry found with a detected pixel - red cross are shown for each pixel of the best camera geometry found without a detected pixel :param output_filename: path for the resulting image """ nv_prec = Decimal('1') v_matrix = np.array([self.v1_lattice, self.v2_lattice]).transpose() pixels_fit_nvs = np.linalg.pinv(v_matrix).dot( (self.pixels_fit_px - self.center_fitted).transpose()) pixels_fit_nvs_dec = [[ Decimal(n1 * 3).quantize(nv_prec, rounding=ROUND_HALF_EVEN) / 3, Decimal(n2 * 3).quantize(nv_prec, rounding=ROUND_HALF_EVEN) / 3 ] for n1, n2 in pixels_fit_nvs.transpose()] pixels_fit_nvs_set = set(map(tuple, pixels_fit_nvs_dec)) pixels_nvs_dec = [[ Decimal(n1 * 3).quantize(nv_prec, rounding=ROUND_HALF_EVEN) / 3, Decimal(n2 * 3).quantize(nv_prec, rounding=ROUND_HALF_EVEN) / 3 ] for n1, n2 in self.pixels_nvs.transpose()] pixels_nvs_set = set(map(tuple, pixels_nvs_dec)) nvs_fit_matching_set = pixels_fit_nvs_set.intersection(pixels_nvs_set) is_fit_matching = np.array( [tuple(nv) in nvs_fit_matching_set for nv in pixels_fit_nvs_dec]) fit_not_matching = is_fit_matching == 0 is_pixel_fitted = np.array( [tuple(nv) in nvs_fit_matching_set for nv in pixels_nvs_dec]) pixels_not_fitted = is_pixel_fitted == 0 fig = plt.figure(figsize=(8, 6), dpi=600) ax = plt.gca() plt.imshow(self.image_cones, cmap='gray') # plt.imshow(self.cone_presence, cmap='gray') plt.autoscale(False) plt.plot(self.pixels_pos_predict[0, is_pixel_fitted], self.pixels_pos_predict[1, is_pixel_fitted], 'gx', ms=5, mew=0.2) plt.plot(self.pixels_pos_predict[0, pixels_not_fitted], self.pixels_pos_predict[1, pixels_not_fitted], 'rx', ms=5, mew=0.2) plt.plot(self.pixels_fit_px[is_fit_matching, 0], self.pixels_fit_px[is_fit_matching, 1], 'b+', ms=5, mew=0.2) plt.plot(self.pixels_fit_px[fit_not_matching, 0], self.pixels_fit_px[fit_not_matching, 1], 'y+', ms=5, mew=0.2) plt.grid(None) plt.axis('off') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.savefig(output_filename, bbox_inches='tight', pad_inches=0) plt.close(fig) print(output_filename, 'saved.')
#iterate towards point of pursuit. positions stack along third dim positions = np.expand_dims(np.copy(locs),2) #get fig ready fig=plt.figure(facecolor=(0,0,0),figsize=[4,4]) plt.xlim([0,sze]) plt.ylim([0,sze]) plt.axis('off') plt.xlim([0,sze]) plt.ylim([0,sze]) plt.gca().axis('off') ax = plt.axes([0,0,1,1], frameon=False) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.autoscale(tight=False) newlocs = locs prevlocs = locs for i in range(int(fps) * length): #a=notavar #prevlocs = positions[:,:,i] prevlocs = newlocs #reassignt to previous newlocs = np.copy(prevlocs) diffs = newlocs[pursue,:] - prevlocs mag = np.linalg.norm(diffs, axis=1) diffs[:,0] = dx * diffs[:,0] / mag diffs[:,1] = dx * diffs[:,1] / mag #print(mag) newlocs+=diffs
def scan_cone_position(self, radius_mask, output_filename='hexagonalicity.png', center_scan=None, rotations=(60, 300)): """ Calculate the hexagonalicity for each pixel inside a camera's pixel. Parameter --------- radius_mask: radius of the mask used for extracting pixels from image (see plot_cones()) output_filename: path where to put the resulting image. center_scan: optional position of the center of the camera's pixel. Center of image is used if None (default) rotations: angles in degrees used for calculating hexagonalicity. remark : output should look like http://n.ethz.ch/~nielssi/download/4.%20Semester/AC%20II/Unterlagen/symmetry_2D_3.pdf page 22 """ if type(radius_mask) is not float: raise AttributeError('radius_mask must be a float.') if self.r1 is None or self.r2 is None: raise AttributeError('camera pixel geometry must be computed ' 'prior of calling scan_cone_position().') mask = self.get_fft_mask(radius=radius_mask) image_cones = np.real(np.fft.ifft2(self.fft_image_cones * mask)) center_image = (np.array(image_cones.shape[::-1]) - 1) / 2 if center_scan is None: center_scan = center_image scan_area = np.zeros(image_cones.shape, dtype=bool) scan_area = set_hexagon(scan_area, center_scan, r1=self.r1, r2=self.r2, value=1) scan_result = np.zeros(image_cones.shape, dtype=float) all_pixels_y, all_pixels_x = np.indices(scan_area.shape) pixels_x = all_pixels_x[scan_area == 1].flatten() pixels_y = all_pixels_y[scan_area == 1].flatten() npixel = pixels_x.shape[0] print('calculating hexagonalicity for each position in the pixel:') last_precent = 0 for pixel_x, pixel_y, iter in zip(pixels_x[::1], pixels_y[::1], range(npixel)): percent_done = np.floor((iter + 1) * 100 / npixel) if percent_done > last_precent: print(percent_done, '%') last_precent = percent_done hexagonalicity = -get_neg_hexagonalicity_with_mask( (pixel_x, pixel_y), image_cones, self.r1, self.r2, rotations=rotations) scan_result[pixel_y, pixel_x] = hexagonalicity plt.ioff() fig = plt.figure(figsize=(8, 6), dpi=600) ax = plt.subplot(1, 2, 2) vmin = np.min(scan_result[scan_result > 0]) vmax = np.max(scan_result[scan_result > 0]) plt.imshow(scan_result, cmap='gray', vmin=vmin, vmax=vmax) plt.autoscale(False) max_y, max_x = np.unravel_index(np.argmax(scan_result), dims=scan_result.shape) plt.plot(max_x, max_y, 'r+') plt.grid(None) plt.axis('off') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.xlim([np.min(pixels_x) - 0.5, np.max(pixels_x) + 0.5]) plt.ylim([np.min(pixels_y) - 0.5, np.max(pixels_y) + 0.5]) ax = plt.subplot(1, 2, 1) plt.imshow(image_cones * scan_area, cmap='gray') plt.plot(max_x, max_y, 'r+') plt.grid(None) plt.axis('off') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.savefig(output_filename, bbox_inches='tight', pad_inches=0) plt.close(fig) print(output_filename, 'saved.')
def showSpectrum(cfg, spectrum, show_butterfly=True): """ Generate spectrum (points + butterfly) """ outputdir = cfg.getValue('general', 'outputdir') if has_matplotlib == False: Utilities.warning('No matplotlib module ==> EXIT!!!') return ################################################################# ### Taken from $CTOOLS/share/examples/python/make_spectrum.py ### ################################################################# # Read spectrum file table = spectrum.table(1) c_energy = table["Energy"] c_ed = table["ed_Energy"] c_eu = table["eu_Energy"] c_flux = table["Flux"] c_eflux = table["e_Flux"] c_ts = table["TS"] c_upper = table["UpperLimit"] # Initialise arrays to be filled energies = [] flux = [] ed_engs = [] eu_engs = [] e_flux = [] ul_energies = [] ul_ed_engs = [] ul_eu_engs = [] ul_flux = [] # Loop over rows of the file nrows = table.nrows() for row in range(nrows): # Get TS ts = c_ts.real(row) flx = c_flux.real(row) e_flx = c_eflux.real(row) # Switch if ts > 9.0 and e_flx < flx: # Add information energies.append(c_energy.real(row)) flux.append(c_flux.real(row)) ed_engs.append(c_ed.real(row)) eu_engs.append(c_eu.real(row)) e_flux.append(c_eflux.real(row)) # else: # Add information ul_energies.append(c_energy.real(row)) ul_flux.append(c_upper.real(row)) ul_ed_engs.append(c_ed.real(row)) ul_eu_engs.append(c_eu.real(row)) # Create figure plt.figure() plt.title("Crab spectrum") # Plot the spectrum plt.loglog() plt.grid() plt.errorbar(energies, flux, yerr=e_flux, xerr=[ed_engs, eu_engs], fmt='ro') if len(ul_energies) > 0: plt.errorbar(ul_energies, ul_flux, xerr=[ul_ed_engs, ul_eu_engs], yerr=1.0e-11, uplims=True, fmt='ro') plt.xlabel("Energy (TeV)") plt.ylabel(r"dN/dE (erg cm$^{-2}$ s$^{-1}$)") if show_butterfly is True: ################################################################## ### Taken from $CTOOLS/share/examples/python/show_butterfly.py ### ################################################################## # Read given butterfly file filename = outputdir + '/' + cfg.getValue('ctbutterfly', 'output') csv = gammalib.GCsv(filename) # Initialise arrays to be filled butterfly_x = [] butterfly_y = [] line_x = [] line_y = [] # Loop over rows of the file nrows = csv.nrows() for row in range(nrows): # Compute upper edge of confidence band butterfly_x.append(csv.real(row, 0) * 1.e-6) # JLK MeV -> TeV # butterfly_y.append(csv.real(row,2)*1.e6) ## JLK MeV -> TeV scale = csv.real(row, 0) * csv.real(row, 0) * 1.e-6 * 1.6 # JLK MeV -> TeV -> erg butterfly_y.append(csv.real(row, 2) * scale) # Set line values line_x.append(csv.real(row, 0) * 1.e-6) # JLK MeV -> TeV line_y.append(csv.real(row, 1) * scale) # JLK MeV -> TeV -> erg # Loop over the rows backwards to compute the lower edge # of the confidence band for row in range(nrows): index = nrows - 1 - row butterfly_x.append(csv.real(index, 0) * 1.e-6) # JLK MeV -> TeV scale = csv.real(index, 0) * csv.real(index, 0) * 1.e-6 * 1.6 low_error = csv.real(index, 3) * scale # JLK MeV -> TeV -> erg if low_error < 1e-26: low_error = 1e-26 butterfly_y.append(low_error) plt.fill(butterfly_x, butterfly_y, color='green', alpha=0.5) plt.plot(line_x, line_y, color='black', ls='-') plt.autoscale(tight=True) outputdir = cfg.getValue('general', 'outputdir') plt.savefig(outputdir + '/' + cfg.getValue('plots', 'spec_outfile'))
print(MonthlyAveragedTbV) MonthlyAveragedTbV[np.isnan(MonthlyAveragedTbV)] = 0 MonthlyAveragedTbV = np.reshape(MonthlyAveragedTbV, np.shape(NbUsefulDates)) Sigmas = [[np.std(MonthlyAveragedTbV[:, i, j]) for j in np.arange(0, 224, 1)] for i in np.arange(0, 200, 1)] print(Sigmas) # Geographic plot fig, ax = plt.subplots(nrows=1, ncols=1) norm = mpl.colors.Normalize(vmin=0, vmax=1) cmap = mpl.cm.spectral myplot = ax.pcolormesh(Sigmas, cmap=cmap, norm=norm) cbar = fig.colorbar(myplot, ticks=np.arange(0, 1.01, 0.5)) cbar.ax.set_xticklabels(['0', '0.5', '1']) # vertically oriented colorbar plt.autoscale(True) plt.axis('equal') plt.savefig("../../SourceData/SMOS/SMOS_StDev_Monthly.png") plt.show() #norm = mpl.colors.Normalize(vmin=200, vmax=0) #plt.pcolormesh(MonthlyAveragedTbV[1,:,:], cmap=cmap, norm=norm) #Export NectCDF file nc_averaged = Dataset('../../SourceData/WorkingFiles/TimeAveragedSMOS.nc', 'w', format='NETCDF4') nc_averaged.description = "SMOS data averaged over 8 years, month by month" #Create NetCDF dimensions nc_averaged.createDimension('m', 12)
def compute_transport(historyFileList, casename, meshfile, maskfile, figdir,\ transectName='Drake Passage', outfile='transport.nc'): mesh = xr.open_dataset(meshfile) mask = get_mask_short_names(xr.open_dataset(maskfile)) if transectName == 'all' or transectName == 'StandardTransportSectionsRegionsGroup': transectList = mask.shortNames[:].values condition = transectList != "Atlantic Transec" transectList = np.extract(condition, transectList) else: transectList = transectName.split(',') if platform.python_version()[0] == '3': for i in range(len(transectList)): transectList[i] = "b'" + transectList[i] print('Computing Transport for the following transects ', transectList) nTransects = len(transectList) maxEdges = mask.dims['maxEdgesInTransect'] # Compute refLayerThickness to avoid need for hist file refBottom = mesh.refBottomDepth.values nz = mesh.dims['nVertLevels'] h = np.zeros(nz) h[0] = refBottom[0] for i in range(1, nz): h[i] = refBottom[i] - refBottom[i - 1] # Get a list of edges and total edges in each transect nEdgesInTransect = np.zeros(nTransects) edgeVals = np.zeros((nTransects, maxEdges)) for i in range(nTransects): amask = mask.sel(shortNames=transectList[i]).squeeze() transectEdges = amask.transectEdgeGlobalIDs.values inds = np.where(transectEdges > 0)[0] nEdgesInTransect[i] = len(inds) transectEdges = transectEdges[inds] edgeVals[i, :len(inds)] = np.asarray(transectEdges - 1, dtype='i') nEdgesInTransect = np.asarray(nEdgesInTransect, dtype='i') # Create a list with the start and stop for transect bounds nTransectStartStop = np.zeros(nTransects + 1) for j in range(1, nTransects + 1): nTransectStartStop[j] = nTransectStartStop[j - 1] + nEdgesInTransect[j - 1] edgesToRead = edgeVals[0, :nEdgesInTransect[0]] for i in range(1, nTransects): edgesToRead = np.hstack( [edgesToRead, edgeVals[i, :nEdgesInTransect[i]]]) edgesToRead = np.asarray(edgesToRead, dtype='i') dvEdge = mesh.dvEdge.sel(nEdges=edgesToRead).values cellsOnEdge = mesh.cellsOnEdge.sel(nEdges=edgesToRead).values edgeSigns = np.zeros((nTransects, len(edgesToRead))) for i in range(nTransects): edgeSigns[i, :] = mask.sel( nEdges=edgesToRead, shortNames=transectList[i]).squeeze().transectEdgeMaskSigns.values latmean = 180.0 / np.pi * np.nanmean( mesh.latEdge.sel(nEdges=edgesToRead).values) lonmean = 180.0 / np.pi * np.nanmean( mesh.lonEdge.sel(nEdges=edgesToRead).values) pressure = gsw.p_from_z(-refBottom, latmean) # Read history files one at a time and slice fileList = sorted(glob.glob(historyFileList)) vol_transport = np.zeros((len(fileList), nTransects)) vol_transportIn = np.zeros((len(fileList), nTransects)) vol_transportOut = np.zeros((len(fileList), nTransects)) heat_transport = np.zeros((len(fileList), nTransects)) # Tref = 0degC heat_transportIn = np.zeros((len(fileList), nTransects)) heat_transportOut = np.zeros((len(fileList), nTransects)) heat_transportTfp = np.zeros( (len(fileList), nTransects)) # Tref = T freezing point computed below (Tfp) heat_transportTfpIn = np.zeros((len(fileList), nTransects)) heat_transportTfpOut = np.zeros((len(fileList), nTransects)) salt_transport = np.zeros((len(fileList), nTransects)) # Sref = saltRef salt_transportIn = np.zeros((len(fileList), nTransects)) salt_transportOut = np.zeros((len(fileList), nTransects)) t = np.zeros(len(fileList)) for i, fname in enumerate(fileList): ncid = Dataset(fname, 'r') if 'timeMonthly_avg_normalTransportVelocity' in ncid.variables.keys(): vel = ncid.variables['timeMonthly_avg_normalTransportVelocity'][ 0, edgesToRead, :] elif 'timeMonthly_avg_normalVelocity' in ncid.variables.keys(): vel = ncid.variables['timeMonthly_avg_normalVelocity'][ 0, edgesToRead, :] if 'timeMonthly_avg_normalGMBolusVelocity' in ncid.variables.keys( ): vel += ncid.variables['timeMonthly_avg_normalGMBolusVelocity'][ 0, edgesToRead, :] else: raise KeyError('no appropriate normalVelocity variable found') tempOnCell1 = ncid.variables[ 'timeMonthly_avg_activeTracers_temperature'][0, cellsOnEdge[:, 0] - 1, :] tempOnCell2 = ncid.variables[ 'timeMonthly_avg_activeTracers_temperature'][0, cellsOnEdge[:, 1] - 1, :] saltOnCell1 = ncid.variables['timeMonthly_avg_activeTracers_salinity'][ 0, cellsOnEdge[:, 0] - 1, :] saltOnCell2 = ncid.variables['timeMonthly_avg_activeTracers_salinity'][ 0, cellsOnEdge[:, 1] - 1, :] t[i] = ncid.variables['timeMonthly_avg_daysSinceStartOfSim'][:] / 365. ncid.close() # Mask T,S values that fall on land tempOnCell1[cellsOnEdge[:, 0] == 0, :] = np.nan tempOnCell2[cellsOnEdge[:, 1] == 0, :] = np.nan saltOnCell1[cellsOnEdge[:, 0] == 0, :] = np.nan saltOnCell2[cellsOnEdge[:, 1] == 0, :] = np.nan # Interpolate T,S values onto edges tempOnEdge = np.nanmean(np.array([tempOnCell1, tempOnCell2]), axis=0) saltOnEdge = np.nanmean(np.array([saltOnCell1, saltOnCell2]), axis=0) # Mask values that fall onto topography tempOnEdge[np.logical_or(tempOnEdge > 1e15, tempOnEdge < -1e15)] = np.nan saltOnEdge[np.logical_or(saltOnEdge > 1e15, saltOnEdge < -1e15)] = np.nan # Compute freezing temperature SA = gsw.SA_from_SP(saltOnEdge, pressure, lonmean, latmean) CTfp = gsw.CT_freezing(SA, pressure, 0.) Tfp = gsw.pt_from_CT(SA, CTfp) # Compute transports for each transect for j in range(nTransects): start = int(nTransectStartStop[j]) stop = int(nTransectStartStop[j + 1]) dArea = dvEdge[start:stop, np.newaxis] * h[np.newaxis, :] normalVel = vel[start:stop, :] * edgeSigns[j, start:stop, np.newaxis] temp = tempOnEdge[start:stop, :] salt = saltOnEdge[start:stop, :] tfreezing = Tfp[start:stop, :] indVelP = np.where(normalVel > 0) indVelM = np.where(normalVel < 0) vol_transport[i, j] = np.nansum(np.nansum(normalVel * dArea)) vol_transportIn[i, j] = np.nansum( np.nansum(normalVel[indVelP] * dArea[indVelP])) vol_transportOut[i, j] = np.nansum( np.nansum(normalVel[indVelM] * dArea[indVelM])) heat_transport[i, j] = np.nansum(np.nansum(temp * normalVel * dArea)) heat_transportIn[i, j] = np.nansum( np.nansum(temp[indVelP] * normalVel[indVelP] * dArea[indVelP])) heat_transportOut[i, j] = np.nansum( np.nansum(temp[indVelM] * normalVel[indVelM] * dArea[indVelM])) heat_transportTfp[i, j] = np.nansum( np.nansum((temp - tfreezing) * normalVel * dArea)) heat_transportTfpIn[i, j] = np.nansum( np.nansum((temp[indVelP] - tfreezing[indVelP]) * normalVel[indVelP] * dArea[indVelP])) heat_transportTfpOut[i, j] = np.nansum( np.nansum((temp[indVelM] - tfreezing[indVelM]) * normalVel[indVelM] * dArea[indVelM])) salt_transport[i, j] = np.nansum(np.nansum(salt * normalVel * dArea)) salt_transport[ i, j] = vol_transport[i, j] - salt_transport[i, j] / saltRef salt_transportIn[i, j] = np.nansum( np.nansum(salt[indVelP] * normalVel[indVelP] * dArea[indVelP])) salt_transportIn[ i, j] = vol_transportIn[i, j] - salt_transportIn[i, j] / saltRef salt_transportOut[i, j] = np.nansum( np.nansum(salt[indVelM] * normalVel[indVelM] * dArea[indVelM])) salt_transportOut[ i, j] = vol_transportOut[i, j] - salt_transportOut[i, j] / saltRef vol_transport = m3ps_to_Sv * vol_transport vol_transportIn = m3ps_to_Sv * vol_transportIn vol_transportOut = m3ps_to_Sv * vol_transportOut heat_transport = W_to_TW * rhoRef * cp * heat_transport heat_transportIn = W_to_TW * rhoRef * cp * heat_transportIn heat_transportOut = W_to_TW * rhoRef * cp * heat_transportOut heat_transportTfp = W_to_TW * rhoRef * cp * heat_transportTfp heat_transportTfpIn = W_to_TW * rhoRef * cp * heat_transportTfpIn heat_transportTfpOut = W_to_TW * rhoRef * cp * heat_transportTfpOut salt_transport = m3ps_to_km3py * salt_transport salt_transportIn = m3ps_to_km3py * salt_transportIn salt_transportOut = m3ps_to_km3py * salt_transportOut # Define some dictionaries for transect plotting obsDict = {'Drake Passage':[120, 175], 'Tasmania-Ant':[147, 167], 'Africa-Ant':None, 'Antilles Inflow':[-23.1, -13.7], \ 'Mona Passage':[-3.8, -1.4],'Windward Passage':[-7.2, -6.8], 'Florida-Cuba':[30, 33], 'Florida-Bahamas':[30, 33], \ 'Indonesian Throughflow':[-21, -11], 'Agulhas':[-90, -50], 'Mozambique Channel':[-20, -8], \ 'Bering Strait':[0.6, 1.0], 'Lancaster Sound':[-1.0, -0.5], 'Fram Strait':[-4.7, 0.7], \ 'Robeson Channel':None, 'Davis Strait':[-1.6, -3.6], 'Barents Sea Opening':[1.4, 2.6], \ 'Nares Strait':[-1.8, 0.2], 'Denmark Strait':None, 'Iceland-Faroe-Scotland':None} labelDict = {'Drake Passage':'drake', 'Tasmania-Ant':'tasmania', 'Africa-Ant':'africaAnt', 'Antilles Inflow':'antilles', \ 'Mona Passage':'monaPassage', 'Windward Passage':'windwardPassage', 'Florida-Cuba':'floridaCuba', \ 'Florida-Bahamas':'floridaBahamas', 'Indonesian Throughflow':'indonesia', 'Agulhas':'agulhas', \ 'Mozambique Channel':'mozambique', 'Bering Strait':'beringStrait', 'Lancaster Sound':'lancasterSound', \ 'Fram Strait':'framStrait', 'Robeson Channel':'robeson', 'Davis Strait':'davisStrait', 'Barents Sea Opening':'barentsSea', \ 'Nares Strait':'naresStrait', 'Denmark Strait':'denmarkStrait', 'Iceland-Faroe-Scotland':'icelandFaroeScotland'} figsize = (20, 10) figdpi = 80 for i in range(nTransects): if platform.python_version()[0] == '3': searchString = transectList[i][2:] else: searchString = transectList[i] # Plot Volume Transport figfile = '{}/volTransport_{}_{}.png'.format(figdir, labelDict[searchString], casename) plt.figure(figsize=figsize, dpi=figdpi) bounds = obsDict[searchString] plt.plot(t, vol_transport[:, i], 'k', linewidth=2, label='model (net)') plt.plot(t, vol_transportIn[:, i], 'r', linewidth=2, label='model (inflow)') plt.plot(t, vol_transportOut[:, i], 'b', linewidth=2, label='model (outflow)') if bounds is not None: plt.gca().fill_between(t, bounds[0] * np.ones_like(t), bounds[1] * np.ones_like(t), alpha=0.3, label='observations (net)') plt.autoscale(enable=True, axis='x', tight=True) plt.ylabel('Volume transport (Sv)', fontsize=12, fontweight='bold') plt.xlabel('Time (Years)', fontsize=12, fontweight='bold') plt.title('Volume transport for {} ({}, mean (net)={:5.2f} $\pm$ {:5.2f})'.format(searchString, casename, \ np.nanmean(vol_transport[:,i]), np.nanstd(vol_transport[:,i]), fontsize=16, fontweight='bold')) plt.legend() plt.savefig(figfile, bbox_inches='tight') # Plot Heat Transport wrt Tref=0 figfile = '{}/heatTransport_{}_{}.png'.format(figdir, labelDict[searchString], casename) plt.figure(figsize=figsize, dpi=figdpi) plt.plot(t, heat_transport[:, i], 'k', linewidth=2, label='model (net)') plt.plot(t, heat_transportIn[:, i], 'r', linewidth=2, label='model (inflow)') plt.plot(t, heat_transportOut[:, i], 'b', linewidth=2, label='model (outflow)') plt.autoscale(enable=True, axis='x', tight=True) plt.ylabel('Heat transport wrt 0$^\circ$C (TW)', fontsize=12, fontweight='bold') plt.xlabel('Time (Years)', fontsize=12, fontweight='bold') plt.title('Heat transport for {} ({}, mean (net)={:5.2f} $\pm$ {:5.2f})'.format(searchString, casename, \ np.nanmean(heat_transport[:,i]), np.nanstd(heat_transport[:,i]), fontsize=16, fontweight='bold')) plt.legend() plt.savefig(figfile, bbox_inches='tight') # Plot Heat Transport wrt Tref=tempRef figfile = '{}/heatTransportTfp_{}_{}.png'.format( figdir, labelDict[searchString], casename) plt.figure(figsize=figsize, dpi=figdpi) plt.plot(t, heat_transportTfp[:, i], 'k', linewidth=2, label='model (net)') plt.plot(t, heat_transportTfpIn[:, i], 'r', linewidth=2, label='model (inflow)') plt.plot(t, heat_transportTfpOut[:, i], 'b', linewidth=2, label='model (outflow)') plt.autoscale(enable=True, axis='x', tight=True) plt.ylabel('Heat transport wrt freezing point (TW)', fontsize=12, fontweight='bold') plt.xlabel('Time (Years)', fontsize=12, fontweight='bold') plt.title('Heat transport for {} ({}, mean (net)={:5.2f} $\pm$ {:5.2f})'.format(searchString, casename, \ np.nanmean(heat_transportTfp[:,i]), np.nanstd(heat_transportTfp[:,i]), fontsize=16, fontweight='bold')) plt.legend() plt.savefig(figfile, bbox_inches='tight') # Plot FW Transport figfile = '{}/fwTransport_{}_{}.png'.format(figdir, labelDict[searchString], casename) plt.figure(figsize=figsize, dpi=figdpi) plt.plot(t, salt_transport[:, i], 'k', linewidth=2, label='model (net)') plt.plot(t, salt_transportIn[:, i], 'r', linewidth=2, label='model (inflow)') plt.plot(t, salt_transportOut[:, i], 'b', linewidth=2, label='model (outflow)') plt.autoscale(enable=True, axis='x', tight=True) plt.ylabel('FW transport (km$^3$/year)', fontsize=12, fontweight='bold') plt.xlabel('Time (Years)', fontsize=12, fontweight='bold') plt.title('FW transport for {} ({}, mean (net)={:5.2f} $\pm$ {:5.2f})'.format(searchString, casename, \ np.nanmean(salt_transport[:,i]), np.nanstd(salt_transport[:,i]), fontsize=16, fontweight='bold')) plt.legend() plt.savefig(figfile, bbox_inches='tight') # Add calls to save transport and then can build up ncid = Dataset(outfile, mode='w', clobber=True, format='NETCDF3_CLASSIC') # Add calls to save transport and then can build up ncid = Dataset(outfile, mode='w', clobber=True, format='NETCDF3_CLASSIC') ncid.createDimension('Time', None) ncid.createDimension('nTransects', nTransects) ncid.createDimension('StrLen', 64) transectNames = ncid.createVariable('TransectNames', 'c', ('nTransects', 'StrLen')) times = ncid.createVariable('Time', 'f8', 'Time') vol_transportVar = ncid.createVariable('volTransport', 'f8', ('Time', 'nTransects')) vol_transportInVar = ncid.createVariable('volTransportIn', 'f8', ('Time', 'nTransects')) vol_transportOutVar = ncid.createVariable('volTransportOut', 'f8', ('Time', 'nTransects')) heat_transportVar = ncid.createVariable('heatTransport', 'f8', ('Time', 'nTransects')) heat_transportInVar = ncid.createVariable('heatTransportIn', 'f8', ('Time', 'nTransects')) heat_transportOutVar = ncid.createVariable('heatTransportOut', 'f8', ('Time', 'nTransects')) heat_transportTfpVar = ncid.createVariable('heatTransportTfp', 'f8', ('Time', 'nTransects')) heat_transportTfpInVar = ncid.createVariable('heatTransportTfpIn', 'f8', ('Time', 'nTransects')) heat_transportTfpOutVar = ncid.createVariable('heatTransportTfpOut', 'f8', ('Time', 'nTransects')) salt_transportVar = ncid.createVariable('FWTransport', 'f8', ('Time', 'nTransects')) salt_transportInVar = ncid.createVariable('FWTransportIn', 'f8', ('Time', 'nTransects')) salt_transportOutVar = ncid.createVariable('FWTransportOut', 'f8', ('Time', 'nTransects')) vol_transportVar.units = 'Sv' vol_transportInVar.units = 'Sv' vol_transportOutVar.units = 'Sv' heat_transportVar.units = 'TW' heat_transportInVar.units = 'TW' heat_transportOutVar.units = 'TW' heat_transportTfpVar.units = 'TW' heat_transportTfpInVar.units = 'TW' heat_transportTfpOutVar.units = 'TW' salt_transportVar.units = 'km^3/year' salt_transportInVar.units = 'km^3/year' salt_transportOutVar.units = 'km^3/year' vol_transportVar.description = 'Net volume transport across transect' vol_transportInVar.description = 'Inflow volume transport across transect (in/out determined by edgeSign)' vol_transportOutVar.description = 'Outflow volume transport across transect (in/out determined by edgeSign)' heat_transportVar.description = 'Net heat transport (wrt 0degC) across transect' heat_transportInVar.description = 'Inflow heat transport (wrt 0degC) across transect (in/out determined by edgeSign)' heat_transportOutVar.description = 'Outflow heat transport (wrt 0degC) across transect (in/out determined by edgeSign)' heat_transportTfpVar.description = 'Net heat transport (wrt freezing point) across transect' heat_transportTfpInVar.description = 'Inflow heat transport (wrt freezing point) across transect (in/out determined by edgeSign)' heat_transportTfpOutVar.description = 'Outflow heat transport (wrt freezing point) across transect (in/out determined by edgeSign)' salt_transportVar.description = 'Net FW transport (wrt {:4.1f} psu) across transect'.format( saltRef) salt_transportInVar.description = 'Inflow FW transport (wrt {:4.1f} psu) across transect (in/out determined by edgeSign)'.format( saltRef) salt_transportOutVar.description = 'Outflow FW transport (wrt {:4.1f} psu) across transect (in/out determined by edgeSign)'.format( saltRef) times[:] = t vol_transportVar[:, :] = vol_transport vol_transportInVar[:, :] = vol_transportIn vol_transportOutVar[:, :] = vol_transportOut heat_transportVar[:, :] = heat_transport heat_transportInVar[:, :] = heat_transportIn heat_transportOutVar[:, :] = heat_transportOut heat_transportTfpVar[:, :] = heat_transportTfp heat_transportTfpInVar[:, :] = heat_transportTfpIn heat_transportTfpOutVar[:, :] = heat_transportTfpOut salt_transportVar[:, :] = salt_transport salt_transportInVar[:, :] = salt_transportIn salt_transportOutVar[:, :] = salt_transportOut for i in range(nTransects): nLetters = len(transectList[i]) transectNames[i, :nLetters] = transectList[i] ncid.close()
def graphwerk(start, finish): open = [] high = [] low = [] close = [] volume = [] # decision = [] date = [] c_open = [] c_high = [] c_low = [] c_close = [] c_volume = [] c_date = [] c_start = start + 18 for x in range(finish - start - 6): c_open.append(float(pd[c_start][1])) c_high.append(float(pd[c_start][2])) c_low.append(float(pd[c_start][3])) c_close.append(float(pd[c_start][4])) c_volume.append(float(pd[c_start][5])) c_date.append(pd[c_start][0]) c_start = c_start + 1 for x in range(finish - start): # Below filtering is valid for eurusd.csv file. Other financial data files have different orders so you need to find out # what means open, high and close in their respective order. open.append(float(pd[start][1])) high.append(float(pd[start][2])) low.append(float(pd[start][3])) close.append(float(pd[start][4])) volume.append(float(pd[start][5])) # decision.append(str(pd[start][6])) date.append(pd[start][0]) start = start + 1 decision = "sell" min_forecast = min(c_low) max_forecast = max(c_high) if close[-1] * 1.06 < max_forecast: decision = "buy" # for z in all_prices: # if close[-1] * 1.03 < z: # decision = "buy" sma = convolve_sma(close, 5) smb = list(sma) diff = sma[-1] - sma[-2] for x in range(len(close) - len(smb)): smb.append(smb[-1] + diff) fig = plt.figure(num=1, figsize=(3, 3), dpi=50, facecolor="w", edgecolor="k") dx = fig.add_subplot(111) # mpl_finance.volume_overlay(ax, open, close, volume, width=0.4, colorup='b', colordown='b', alpha=1) mpl_finance.candlestick2_ochl( dx, open, close, high, low, width=1.5, colorup="g", colordown="r", alpha=0.5 ) plt.autoscale() # plt.plot(smb, color="blue", linewidth=10, alpha=0.5) plt.axis("off") if decision == "sell": print("last value: " + str(close[-1])) print( "range of values in next 13 bars: " + str(min_forecast) + "-" + str(max_forecast) ) print("sell") plt.savefig(sell_dir + str(uuid.uuid4()) + ".jpg", bbox_inches="tight") else: print("last value: " + str(close[-1])) print( "range of values in next 13 bars: " + str(min_forecast) + "-" + str(max_forecast) ) print("buy") plt.savefig(buy_dir + str(uuid.uuid4()) + ".jpg", bbox_inches="tight") # if close[-1] >= close_next: # print('previous value is bigger') # print('last value: ' + str(close[-1])) # print('next value: ' + str(close_next)) # print('sell') # plt.savefig(sell_dir + str(uuid.uuid4()) +'.jpg', bbox_inches='tight') # else: # print('previous value is smaller') # print('last value: '+ str(close[-1])) # print('next value: ' + str(close_next)) # print('buy') # plt.savefig(buy_dir + str(uuid.uuid4())+'.jpg', bbox_inches='tight') # plt.show() open.clear() close.clear() volume.clear() high.clear() low.clear() plt.cla() plt.clf()
def vis_live(im, im_name, output_dir, boxes, segms=None, keypoints=None, thresh=0.9, kp_thresh=2, dpi=200, box_alpha=0.0, dataset=None, show_class=False, ext='pdf'): """Visual debugging of detections.""" if not os.path.exists(output_dir): os.makedirs(output_dir) if isinstance(boxes, list): boxes, segms, keypoints, classes = convert_from_cls_format( boxes, segms, keypoints) if boxes is None or boxes.shape[0] == 0 or max(boxes[:, 4]) < thresh: return dataset_keypoints, _ = keypoint_utils.get_keypoints() if segms is not None: masks = mask_util.decode(segms) color_list = colormap(rgb=True) / 255 kp_lines = kp_connections(dataset_keypoints) cmap = plt.get_cmap('rainbow') colors = [cmap(i) for i in np.linspace(0, 1, len(kp_lines) + 2)] fig = plt.figure(frameon=False) fig.set_size_inches(im.shape[1] / dpi, im.shape[0] / dpi) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.axis('off') fig.add_axes(ax) ax.imshow(im) # Display in largest to smallest order to reduce occlusion areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) sorted_inds = np.argsort(-areas) mask_color_id = 0 for i in sorted_inds: bbox = boxes[i, :4] score = boxes[i, -1] if score < thresh: continue # show box (off by default) ax.add_patch( plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False, edgecolor='g', linewidth=0.5, alpha=box_alpha)) if show_class: ax.text(bbox[0], bbox[1] - 2, get_class_string(classes[i], score, dataset), fontsize=30, family='serif', bbox=dict(facecolor='g', alpha=0.4, pad=0, edgecolor='none'), color='white') # show mask if segms is not None and len(segms) > i: img = np.ones(im.shape) color_mask = color_list[mask_color_id % len(color_list), 0:3] mask_color_id += 1 w_ratio = .4 for c in range(3): color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio for c in range(3): img[:, :, c] = color_mask[c] e = masks[:, :, i] _, contour, hier = cv2.findContours(e.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) for c in contour: polygon = Polygon(c.reshape((-1, 2)), fill=True, facecolor=color_mask, edgecolor='w', linewidth=1.2, alpha=0.5) ax.add_patch(polygon) # show keypoints if keypoints is not None and len(keypoints) > i: kps = keypoints[i] plt.autoscale(False) for l in range(len(kp_lines)): i1 = kp_lines[l][0] i2 = kp_lines[l][1] if kps[2, i1] > kp_thresh and kps[2, i2] > kp_thresh: x = [kps[0, i1], kps[0, i2]] y = [kps[1, i1], kps[1, i2]] line = plt.plot(x, y) plt.setp(line, color=colors[l], linewidth=1.0, alpha=0.7) if kps[2, i1] > kp_thresh: plt.plot(kps[0, i1], kps[1, i1], '.', color=colors[l], markersize=3.0, alpha=0.7) if kps[2, i2] > kp_thresh: plt.plot(kps[0, i2], kps[1, i2], '.', color=colors[l], markersize=3.0, alpha=0.7) # add mid shoulder / mid hip for better visualization mid_shoulder = ( kps[:2, dataset_keypoints.index('right_shoulder')] + kps[:2, dataset_keypoints.index('left_shoulder')]) / 2.0 sc_mid_shoulder = np.minimum( kps[2, dataset_keypoints.index('right_shoulder')], kps[2, dataset_keypoints.index('left_shoulder')]) mid_hip = (kps[:2, dataset_keypoints.index('right_hip')] + kps[:2, dataset_keypoints.index('left_hip')]) / 2.0 sc_mid_hip = np.minimum( kps[2, dataset_keypoints.index('right_hip')], kps[2, dataset_keypoints.index('left_hip')]) if (sc_mid_shoulder > kp_thresh and kps[2, dataset_keypoints.index('nose')] > kp_thresh): x = [mid_shoulder[0], kps[0, dataset_keypoints.index('nose')]] y = [mid_shoulder[1], kps[1, dataset_keypoints.index('nose')]] line = plt.plot(x, y) plt.setp(line, color=colors[len(kp_lines)], linewidth=1.0, alpha=0.7) if sc_mid_shoulder > kp_thresh and sc_mid_hip > kp_thresh: x = [mid_shoulder[0], mid_hip[0]] y = [mid_shoulder[1], mid_hip[1]] line = plt.plot(x, y) plt.setp(line, color=colors[len(kp_lines) + 1], linewidth=1.0, alpha=0.7) canvas = FigureCanvas(fig) ax = fig.gca() ax.text(0.0, 0.0, "Test", fontsize=45) ax.axis('off') canvas.draw() # draw the canvas, cache the renderer data = np.frombuffer(canvas.tostring_rgb(), dtype='uint8') data = data.reshape(fig.canvas.get_width_height()[::-1] + (3, )) plt.close('all') return data
}) #-----------------------------# # Reading and processing data # #-----------------------------# data = pd.read_csv('xxx.csv', delimiter=',') # read csv into pandas dataframe # choose right delimiter: , ; # first line of datafile has to be column names # this names are also used for plotting # example Time,Current #-----------------# # Plotting # #-----------------# plt.figure(1, figsize=(width, height)) plt.autoscale(enable=True, axis='both', tight=None) ax = plt.gcf().gca() #plot with pandas data.plot(x='ColumnNameX', y='ColumNameY', kind='line', label='label', c='dimgray', linewidth=1, ax=ax) data.plot(x='ColumnNameX', y='ColumnNameY', kind='line', label='label', c='dimgray',
def operatingIndex(SaveFileType, LineType, FileFormat, TableShow=None): try: # validate parmas when run script from command line from path_config import img_file_path # ReadData = CsvData.readData() # read CSV data dataRead = CsvData.readData() # read CSV data multiple file incr = 1 for ReadData in dataRead: ##Start of font setting matplotlib.font_manager._rebuild() fontpath = 'AGaramondPro-Regular.otf' prop = font_manager.FontProperties(fname=fontpath) titlefont = {'fontname': prop.get_name()} legendfont = prop.get_name() plt.rcParams['font.family'] = 'Arial' ##End of font setting now = datetime.datetime.now() curTime = now.strftime("%Y_%m_%d") # Reading filename from arguments PRINT_FORMAT = SaveFileType # Reading printing mode (landscape[l] or portrait[p]) saveFileSizeParam = FileFormat # Reading line format for company performance straight line or interpolated COMPANY_PERFORMANCE_LINE = LineType # Reading input data filename saveinputFile = ReadData['fileName'] # save path from export_path.py saveFile = "_oi_" + saveinputFile + "_" + LineType + "_" + saveFileSizeParam # reading chart title (#titleName = 'Operating Index \nEBIT Margin' # Static Title Entry) titleName = ReadData['title'][0] titleName = titleName.replace('\\n', '\n') # table titleseq tabletitleName = ReadData['tabletitle'][-1] color = [ darkskyblue_obermatt, blue_obermatt, lightskyblue_obermatt, orange_obermatt, blue_obermatt ] # line type style = ['-', '-', '-', '-', ''] # Marker type marker = ['', '', '', 'o', ''] # legend names reading from csv data file legendLabel = ReadData['legendName'] # company name (reading from CSV data file) compname = legendLabel[3] # Check if CSV file value is in percentage or not percentageExist = ReadData["perExist"] percentageFormat = '{:3.1f}' percentageFormatYLables = '{:3.0f}%' if percentageExist: percentageFormat = '{:3.1f}%' intFormat = '{:3.0f}' # Marker width size markerSize = 5 # Line width value lineWidthArr = [2, 1, 2, 1, 1] # Orange and blue Line width lineWidth = 1.5 # company name row id dottedKey = 0 # marker color markerColor = '#ff6100' # title font size titleSize = 16 # number font size numberFontSize = 10.5 plt.figure() #Disabling Autoscale for y axis plt.autoscale(enable=False, axis='y') ax = plt.subplot() # hide right and left line of chart ax.spines['right'].set_visible(False) ax.spines['left'].set_visible(False) #ax.spines['bottom'].set_position(('axes', -0.005)) #ax.spines['top'].set_position(('axes', 1.005)) # X Axis Data points x = np.array(list(range(len(ReadData['xAxisName'])))) # Static Y Axis Data points yAxisValue = np.array(ReadData['axisValue']) # added below to set Y axis value static & dynamic Start yMin = ReadData['yMin'] yMax = ReadData['yMax'] y = np.array(yAxisValue) plt.ylim(int(yMin), int(yMax)) # x Axis Data points my_xticks = ReadData['xAxisName'] # set X axis replace static number with original key value plt.xticks(x, my_xticks, fontsize=numberFontSize) # interpolation technique is used to convert the straight line with curve -- Importing base library from scipy import interpolate # 0- companydata # 1-75 Percentile # 2-Median # 3-25 Percentile # 4-Count # COLOR_CODE of line color = [ orange_obermatt, lightskyblue_obermatt, blue_obermatt, darkskyblue_obermatt, blue_obermatt ] # Loop start count count = 0 fillData = {} lastcolumncount = len(y) - 1 for data in y: if count == 0 and COMPANY_PERFORMANCE_LINE == "s": f = interpolate.interp1d(np.arange(len(data)), data, kind='linear') else: # interpolate value if found Nan value in array data = np.array(data) nans, xdata = nan_helper(data) data[nans] = np.interp(xdata(nans), xdata(~nans), data[~nans]) # Draw Curve Linenan if count == 0: f = interpolate.interp1d(np.arange(len(data)), data, kind='cubic') # else: f = interpolate.interp1d(np.arange(len(data)), data, kind='cubic') xnew = np.arange(0, len(data) - 1, 0.01) ynew = f(xnew) fillData[count] = f(xnew) # Set plot final plot if count == 0: plt.plot(xnew, ynew, color=color[count], linestyle=style[count], markersize=markerSize, linewidth=lineWidthArr[count], label=legendLabel[count], zorder=102) else: if count == 2: plt.plot(xnew, ynew, color=color[count], linestyle=style[count], markersize=markerSize, linewidth=lineWidthArr[count], label=legendLabel[count], zorder=101) else: plt.plot(xnew, ynew, color=color[count], linestyle=style[count], markersize=markerSize, linewidth=lineWidthArr[count], label=legendLabel[count], zorder=99) count = count + 1 # Fill color between two line start # from y0 to y1 fill1 = [1, 2] # from y1 to y2 fill2 = [2, 3] # COLOR_CODE colorFill = [lightskyblue_obermatt, darkskyblue_obermatt] count1 = 0 for a, b in zip(fill1, fill2): plt.fill_between(xnew, fillData[a], fillData[b], color=colorFill[count1], alpha='1', interpolate=True, zorder=100) count1 = count1 + 1 # first add orange marker without line plt.plot(y[dottedKey], color=markerColor, linestyle='', markersize=markerSize, linewidth=lineWidth, marker='o', zorder=102) # added to display value on marker for i, j in zip(x, y[dottedKey]): # converted values into percentage value ax.annotate(percentageFormat.format(j), xy=(i, j), horizontalalignment='right', verticalalignment='bottom', fontsize=numberFontSize, zorder=103) vals = ax.get_yticks() # converted values into percentage value ax.set_yticklabels( [percentageFormatYLables.format(x) for x in vals], fontsize=numberFontSize) # Display table or not based on parameter passed by user, by default table will display on graph, 0 -> dont display table, 1 -> Display table try: TableShow # Parameter which is entered by user except Exception as e: showTable = False # default display Table # boxx=0.26 # boxy=-0.15 boxx = 1 boxy = 0.50 else: showTable = False # boxx=0.26 # boxy=-0.15 boxx = 1 boxy = 0.50 if TableShow == 't': # if user enter 1 display table else dont display the table showTable = True boxx = 1 boxy = 0.50 saveFile += "_t" saveFile += "." + PRINT_FORMAT plt.xticks(x, my_xticks, fontsize=numberFontSize) # ---------------------------------------Code for Showing table started------------------------ count4 = 0 if showTable: # -------------- Data Table start----------------------- yx = y y_without_nan = y y_without_nan_formatted = [[intFormat.format(k) for k in l] for l in y_without_nan] y_without_nan_formatted[:-1] = y[:-1] y_without_nan_formatted = np.array(y_without_nan_formatted) the_table = plt.table(cellText=y_without_nan_formatted, colLabels=my_xticks, loc='bottom', cellLoc='right', colLoc='right', rowLoc='left') the_table.set_fontsize(numberFontSize) the_table.scale(1, 1.5) # Remove Border of table 1 cell for key, cell in the_table.get_celld().items(): cell.set_linewidth(0) # -------------------Data Table end-------------------------- # ---------------right side table of company name start------------------------------ my_xticks_1 = [tabletitleName] legendLabel_1 = np.reshape(legendLabel, (-1, 1)) the_table1 = plt.table(cellText=legendLabel_1, colLabels=my_xticks_1, loc='bottom right', colLoc='bottom center', rowLoc='bottom left', animated=True) # the_table1.auto_set_column_width([-1,0,1]) # set column width the_table1.set_fontsize(numberFontSize) the_table1.scale(.5, 1.5) cells = the_table1.properties()["celld"] # row text left align cellLength = len(legendLabel) # length of row for i in range(0, cellLength + 1): cells[i, 0]._loc = 'left' # Remove Border of table 2 cell for key, cell in the_table1.get_celld().items(): cell.set_linewidth(0) # -------------------------right side table of company name end------------------------------------- # plt.xticks([]) # remove x Axis values, already put value using table my_xticks = ['', '', '', '', '', '', '', '', '', ''] plt.tick_params( axis='x', # changes apply to the x-axis which='both', # both major and minor ticks are affected bottom='off', # ticks along the bottom edge are off top='off', # ticks along the top edge are off labelbottom='off') # labels along the bottom edge are off # set X axis replace static number with original key value plt.xticks(x, my_xticks, fontsize=numberFontSize, ha="right") # ---------------------------------------Code for Showing table Ended------------------------ # Set Graph title plt.title(titleName, loc='left', fontsize=titleSize, fontweight="regular", color=brown_obermatt, **titlefont) fig = plt.gcf() # defining portrait or landscape mode if saveFileSizeParam == 'p': fig.set_size_inches(10.3, 7.3) dpi = 500 else: fig.set_size_inches(9.84, 5.9) dpi = 500 # -------------------- Start of designing custome legends------------------------ m2, = ax.plot([], []) m3, = ax.plot([], []) m3, = ax.plot([], [], color='#ffffff', marker='', markersize=2, fillstyle='bottom', linestyle='none', linewidth=1) m4, = ax.plot([], [], color=orange_obermatt, marker='o', linestyle='none', solid_joinstyle='round', linewidth=1) legendtext1 = ReadData['axisfigtext'][0] legendtext2 = ReadData['axisfigtext'][1] # setup the handler instance for the scattered data custom_handler = CustomeLegend.ImageHandler() custom_handler.set_image('./legend_images/legend1.png', image_stretch=(10, 1)) custom_handler2 = CustomeLegend.ImageHandler() custom_handler2.set_image('./legend_images/legend2.png', image_stretch=(10, 1)) if saveFileSizeParam == "l": plt.legend([m2, m3], [legendtext1, legendtext2], handler_map={ m2: custom_handler, m3: custom_handler2 }, labelspacing=1, loc='center left', bbox_to_anchor=(boxx, boxy), frameon=False, prop={ 'family': legendfont, 'size': 11 }) else: plt.legend([m2, m3], [legendtext1, legendtext2], handler_map={ m2: custom_handler, m3: custom_handler2 }, labelspacing=1, loc='left', bbox_to_anchor=(boxx, boxy), frameon=False, prop={ 'family': legendfont, 'size': 11 }) # -------------------- End of designing custome legends------------------------ if showTable: plt.subplots_adjust(bottom=0.28, right=0.70, left=0.05, top=0.89, hspace=0.5, wspace=0.5) # plt.subplots_adjust(bottom=0.35,right=0.8,hspace=0.5,wspace=0.5) #Margin size of plot else: plt.subplots_adjust(bottom=0.11, right=0.70, left=0.05) # Margin size of plot plt.savefig(img_file_path + curTime + saveFile, dpi=dpi, format=PRINT_FORMAT) # plt.savefig(img_file_path+curTime+saveFile, dpi=dpi, bbox_inches='tight', format=PRINT_FORMAT) # plt.show() print(str(incr) + " : " + curTime + saveFile) incr = incr + 1 except Exception as e: print( "Something Went wrong at Oi chart! Unable to process your request." ) print(e)
for i in range(fut_pred): seq = torch.FloatTensor(test_inputs[-train_window:]) with torch.no_grad(): model.hidden = (torch.zeros(1, 1, model.hidden_layer_size), torch.zeros(1, 1, model.hidden_layer_size)) test_inputs.append(model(seq).item()) actual_predictions = scaler.inverse_transform(np.array(test_inputs[train_window:] ).reshape(-1, 1)) x = np.arange(train_data.shape[0], data.shape[0], 1) plt.title('days vs cases') plt.ylabel('cases') plt.grid(True) plt.autoscale(axis='x', tight=True) plt.plot(data) plt.plot(x,actual_predictions) plt.show() plt.title('days vs cases') plt.ylabel('cases') plt.grid(True) plt.autoscale(axis='x', tight=True) plt.plot(data[-train_window:]) plt.plot(actual_predictions) plt.show()
def mostrar_info(cromosoma_final, maximos, minimos, promedios, prob_cross, prob_mut): """Crea un archivo HTML que muestra la información que se pide.""" f = open('resultados.html', 'w') html_inicial = """<!DOCTYPE html> <html> <head> <title>Trabajo Práctico N°1</title> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div> <h1>Trabajo Práctico N°1</h1> <h2>Algoritmos Genéticos</h2> <p>Rafael Verde</p> <p>Braian Villasanti</p>""" # Muestra el cromosoma máximo final. cromosoma_maximo = """<p><b>Cromosoma máximo: </b><div class="monoespaciado">%s</div></p>""" % cromosoma_final #Muestra los valores de las probabilidades valores = """<p><b>Probabilidad de crossover: </b>%d% <b>Probabilidad de mutacion: </b>%d% </p> <h1>Tablas de valores</h1>""" % (prob_cross * 100, prob_mut * 100) # Muestra el valor máximo, mínimo, y promedio de cada población. header_tabla = """ <table> <tr> <th>Generación</th> <th>Máximos</th> <th>Mínimos</th> <th>Promedios</th> </tr> """ f.write(html_inicial) f.write(cromosoma_maximo) f.write(valores) f.write(header_tabla) for i in range(200): tabla = """<tr> <td>%d</td> <td>%f</td> <td>%f</td> <td>%f</td> </tr>""" % (i + 1, maximos[i], minimos[i], promedios[i]) f.write(tabla) # Muestra tablas de máximos, mínimos, y promedios para 20, 100, y 200 corridas. tabla2 = """</table> <h1>Valores para las 20, 100, y 200 corridas</h1> <table> <tr> <th>Generación</th> <th>Máximos</th> <th>Mínimos</th> <th>Promedios</th> </tr> <tr> <td>20</td> <td>%f</td> <td>%f</td> <td>%f</td> </tr> <tr> <td>100</td> <td>%f</td> <td>%f</td> <td>%f</td> </tr> <tr> <td>200</td> <td>%f</td> <td>%f</td> <td>%f</td> </tr> </table>""" % (maximos[19], minimos[19], promedios[19], maximos[99], minimos[99], promedios[99], maximos[199], minimos[199], promedios[199]) plt.plot(range(200), maximos) plt.xlim(0, 200) plt.ylim(0, 1) plt.autoscale(False) plt.savefig('graficos/maximos.svg', bbox_inches='tight') plt.clf() plt.plot(range(200), minimos) plt.xlim(0, 200) plt.ylim(0, 1) plt.autoscale(False) plt.savefig('graficos/minimos.svg', bbox_inches='tight') plt.clf() plt.plot(range(200), promedios) plt.xlim(0, 200) plt.ylim(0, 1) plt.autoscale(False) plt.savefig('graficos/promedios.svg', bbox_inches='tight') html_final = """ <h1>Máximos<h1> <img src="graficos/maximos.svg"> <h1>Mínimos<h1> <img src="graficos/minimos.svg"> <h1>Promedios<h1> <img src="graficos/promedios.svg"> </div> </body> </html>""" f.write(tabla2) f.write(html_final) f.close() webbrowser.open_new_tab('resultados.html')
def main(grid_size): print("\n \nStart!!") rospy.init_node('a_star_test', anonymous=True, disable_signals=True) xmin = round(min(ox)) xmax = round(max(ox)) ymin = round(min(oy)) ymax = round(max(oy)) print("X input range: ["), print(xmin), print(" --> "), print(xmax), print("]") print("Y input range: ["), print(ymin), print(" --> "), print(ymax), print("]\n") dittoS = str( input("please enter the number of the robot you want to start from: ")) dittoG = str( input("please enter the number of the robot you want to attach to: ")) rospy.Subscriber("ditto" + dittoS + "/odom", Odometry, callbackS) rospy.Subscriber("ditto" + dittoG + "/odom", Odometry, callbackG) rate = rospy.Rate(15) global flagoff while not rospy.is_shutdown(): if (sx == 0.0 and sy == 0.0 and syaw == 0.0): rospy.wait_for_message("ditto" + dittoS + "/odom", Odometry, timeout=10) sx_round = round(sx, 2) sy_round = round(sy, 2) print("Current X Position: "), print(sx_round) print("Current Y Position: "), print(sy_round) print("Current Yaw: "), print(syaw) print(" ") if (gx == 0.0 and gy == 0.0 and gtheta == 0.0): rospy.wait_for_message("ditto" + dittoG + "/odom", Odometry, timeout=10) print("The Goal Pose You Requested is :"), print(gx, gy, gtheta) gx_round = round(gx, 2) gy_round = round(gy, 2) print("sx_round:: ", str(sx_round), "sy_round:: ", str(sy_round)) print("gx_round:: ", str(gx_round), "gy_round:: ", str(gy_round)) if show_animation: # pragma: no cover plt.plot(ox, oy, ".k") plt.plot(sx_round, sy_round, "og") plt.plot(gx_round, gy_round, "xb") plt.grid(True) plt.axis("equal") plt.show() a_star = AStarPlanner(ox, oy, grid_size, robot_radius) rx, ry = a_star.planning(sx, sy, gx, gy) rx.reverse() ry.reverse() rx_round = [round(num, 2) for num in rx] ry_round = [round(num, 2) for num in ry] print("rx: "), print(rx_round) print("ry: "), print(ry_round) path = path_points() path.x = rx_round path.y = ry_round path.yaw = gtheta path.dOne = dittoS path.dTwo = dittoG if show_animation: # pragma: no cover plt.figure(num="Planned Path") plt.grid(True) plt.xlabel("X-Position") plt.ylabel("Y-Position") plt.plot(rx_round, ry_round, "-r") plt.autoscale(enable=True, axis='both') plt.show() # path_pub.publish(path) # flagoff==True # if flagoff==True: # rate.sleep() #flagoff=True #if flagoff==True: #rospy.signal_shutdown(10) answer = None while answer not in ("yes", "no"): answer = raw_input( "Do you want to edit the plan?,Answer with 'yes' or 'no': ") if answer == "yes": plt.close('all') #print(" ") elif answer == "no": while True: path_pub.publish(path) rate.sleep() else: print("Please enter 'yes' or 'no'.")
def main(): f1 = Figlet(font='slant') print(f1.renderText('Projeto 7 - receptor')) print("\n[+]---Inicializando decoder\n\n") signal = signalMeu() duration = 2 #tempo em segundos que ira aquisitar o sinal acustico captado pelo mic sd.default.channels = 1 sd.default.samplerate = 44100 numAmostras = 44100 * duration print("\n[+]---A captação do som começará em 5 segundos...") time.sleep(5) print("\n[+]---Gravação inicializada!") audio = sd.rec(int(2 * 44100), 44100, channels=1) sd.wait() print("\n[+]---Fim da gravação.") tempo = np.linspace(0, 2, 88200) arrayAudio = np.ndarray(shape=(88200, ), dtype=np.float32) for i in range(0, arrayAudio.shape[0]): arrayAudio[i] = audio[i][0] # # plot do gravico áudio gravado (dados) vs tempo! print("\n[+]---Plotando gráfico do áudio gravado.") plt.figure("Senoide") plt.plot(tempo[:400], arrayAudio[:400]) plt.grid(True) plt.title("Áudio gravado x Tempo") plt.autoscale(enable=True, axis="both", tight=True) plt.savefig("img/senoidesAudioGravado.png", format="png") plt.show() # ## Calcula e exibe o Fourier do sinal audio. como saida tem-se a amplitude e as frequencias print("\n[+]---Realizando a transformada de Fourier.") xf, yf = signal.calcFFT(arrayAudio, 44100) print("\n[+]---Plotando gráfico da transformada de Fourier.") plt.figure("Fourier") plt.plot(xf, yf) plt.grid(True) plt.title('Transformada de Fourier - Áudio gravado') plt.autoscale(enable=True, axis="both", tight=True) plt.savefig("img/transformadaFourier.png", format="png") plt.show() ## Adquirindo a tecla a partir do som obtido e da transformada de fourier print("\n[+]---Identificando picos.") index = peakutils.indexes(yf, thres=0.3, min_dist=1000) frqObtidaLista = [[], []] for frequencia in xf[index]: #Linha if int(frequencia) in range(640, 990): frqObtidaLista[0].append(int(frequencia)) #Coluna elif int(frequencia) in range(1160, 1680): frqObtidaLista[1].append(int(frequencia)) tabelaDTMF = { "1": [697, 1209], "2": [697, 1336], "3": [697, 1477], "A": [697, 1633], "4": [770, 1209], "5": [770, 1336], "6": [770, 1477], "B": [770, 1633], "7": [852, 1209], "8": [852, 1336], "9": [852, 1477], "C": [852, 1633], "X": [941, 1209], "0": [941, 1336], "#": [941, 1477], "D": [941, 1633] } character = "None" resolucao = 30 for tecla, frequencias in tabelaDTMF.items(): if frqObtidaLista[0][ 0] <= frequencias[0] + resolucao and frqObtidaLista[0][ 0] >= frequencias[0] - resolucao and frqObtidaLista[1][ 0] <= frequencias[1] + resolucao and frqObtidaLista[1][ 0] >= frequencias[1] - resolucao: character = tecla print(f"\n[+]---Tecla referente ao som gravado: {character}")
def Silhouette_metrics(start, stop, X, y, dataframe, data, sample_size, new_prior="False"): print() print("We are working with this sample size: " + str(sample_size)) print() k_sample_result = [] cc = { 'AF': "Black", 'Critical voice RTP': "Red", 'Network or Intenetwork control': "Blue", 'Not Known': "Yellow", 'best effort': "Green" } #Defyning the range of centroids to be considered range_n_clusters = list(range(start, stop + 1)) for n_clusters in range_n_clusters: print() print("Now we are evaluating this number of clusters: ") print(str(n_clusters)) print() # Create a subplot with 1 row and 2 columns #fig, (ax1, ax2) = plt.subplots(1, 2) fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2, projection='3d') #33 -22 fig.set_size_inches(18, 9) # The 1st subplot is the silhouette plot # The silhouette coefficient can assume a value from -1 to 1 but in #this example all lie within [-0.1, 1] ax1.set_xlim([-0.05, 1]) # The (n_clusters+1)*10 is for inserting blank space between silhouette # plots of individual clusters, to demarcate them clearly. #ax1.set_ylim([0, len(X) + (n_clusters + 1) * 10]) #Al posto di X che ha tutte le osservazioni inserire il sottoCampione !!! #Utilizzata la stessa grandezza del sample #ax1.set_ylim([0, 70000 + (n_clusters + 1) * 10]) #era 50 ora mettiamo 100 ax1.set_ylim([0, sample_size + (n_clusters + 1) * 260]) # Initialize the clusterer with n_clusters value and a random generator # seed of 10 for reproducibility. clusterer = KMeans(n_clusters=n_clusters, init='k-means++', random_state=0) cluster_labels = clusterer.fit_predict(X) print("Salvo Centroidi") centers = clusterer.cluster_centers_ #To save centroids savetxt('data.csv', centers, delimiter=',') #Computing the Error Obtained previously print() err, s = error_Cluster(cluster_labels, dataframe) print("The total error with K == " + str(n_clusters) + " is : ") print(err) ##Correspondence for coloring diz_class_occ = {} for elem in list(set(cluster_labels)): diz_class_occ[elem] = [] for pos in [ x for x in range(len(cluster_labels)) if cluster_labels[x] == elem ]: diz_class_occ[elem].append(dataframe.iloc[pos, 0]) print() print("We are working with this number of clusters: " + str(n_clusters)) print() color_clutser = {} for i in diz_class_occ: #print() #print("# of Cluster is : " + str(i)) diz = Counter(diz_class_occ[i]) #print(diz) #print() a = max(diz.items(), key=operator.itemgetter(1))[0] #print(a) #print(cc[a]) color_clutser[i] = [cc[a], a] #Inserita nelle variabili della funzione #sample_size = 70000 indices = np.random.RandomState(seed=42).permutation( X.shape[0])[:sample_size] new_X, new_cluster_labels = X[indices], cluster_labels[indices] print() print("This is the Silhouette result : ") print() # The silhouette_score gives the average value for all the samples. # This gives a perspective into the density and separation of the formed # clusters silhouette_avg = silhouette_score(new_X, new_cluster_labels) print("For n_clusters =", n_clusters, "The average silhouette_score is :", silhouette_avg) #Saving the result of Silhouette in a dict k_sample_result.append(silhouette_avg) # Compute the silhouette scores for each sample sample_silhouette_values = silhouette_samples(new_X, new_cluster_labels) #era 50 messo 100 per dare spazio y_lower = 260 #Definire il dizionario dal quale poi possiamo stabilire #le differenti priorità diz_priority = {poss: [] for poss in cc.keys()} for i in range(n_clusters): # Aggregate Silhouette scores for samples belonging to # cluster i-th, and sort them ith_cluster_silhouette_values = \ sample_silhouette_values[new_cluster_labels == i] ith_cluster_silhouette_values.sort() for ke, el in cc.items(): if el == color_clutser[i][0]: try: diz_priority[ke].append( (i, sum(ith_cluster_silhouette_values) / len(ith_cluster_silhouette_values))) except: print( "errore -- forse avviene per il sample troppo piccolo" ) #Capire errore diz_priority[ke].append((i, 0)) #print("ith_cluster_silhouette_values") #print(ith_cluster_silhouette_values) #print("ith_cluster_silhouette_values") #print(ith_cluster_silhouette_values) #print() #print() size_cluster_i = ith_cluster_silhouette_values.shape[0] #print(size_cluster_i) y_upper = y_lower + size_cluster_i color = color_clutser[i][0] ax1.fill_betweenx(np.arange(y_lower, y_upper), 0, ith_cluster_silhouette_values, facecolor=color, edgecolor=color, alpha=0.7) ax1.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i)) # Compute the new y_lower for next plot #era 50 messo 100 y_lower = y_upper + 260 # 10 for the 0 samples ax1.set_title("The silhouette plot for the various clusters", fontsize=15) ax1.set_xlabel("The silhouette coefficient values", fontsize=13.5) ax1.set_ylabel("Cluster label", fontsize=13.5) # The vertical line for average silhouette score of all the values ax1.axvline(x=silhouette_avg, color="red", linestyle="--") ax1.set_yticks([]) # Clear the yaxis labels / ticks ax1.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1]) # 2nd Plot showing the actual clusters formed # Labeling the clusters centers = clusterer.cluster_centers_ # Draw white circles at cluster centers ax2.scatter(centers[:, 0], centers[:, 1], centers[:, 2], marker='*', c="lightyellow", s=150, edgecolor='k', label="Centroids", alpha=0.8) #Plotting the centroids number #for i, c in enumerate(centers): # ax2.scatter(c[0], c[1], c[2], marker='$%d$' % i, alpha=1, # s=120, edgecolor='k') for i in list(set((cluster_labels))): #LABEL #ax2.scatter(data[cluster_labels == i, 0], data[cluster_labels == i, 1], data[cluster_labels == i, 2], s = 50, c = color_clutser[i][0], alpha = 0.3,label = color_clutser[i][1]) #No label ax2.scatter(data[cluster_labels == i, 0], data[cluster_labels == i, 1], data[cluster_labels == i, 2], s=50, c=color_clutser[i][0], alpha=0.3) #ax2.scatter(data[cluster_labels == i, 0], data[cluster_labels == i, 1], s = 50, c = color_clutser[i][0], alpha = 0.3,label = color_clutser[i][1]) #ax2.scatter(train[cluster_labels == 1, 0], train[cluster_labels == 1, 1], train[cluster_labels == 1, 2], s = 50, c = color_clutser[1][0], alpha = 0.3, label = color_clutser[1][1]) #ax2.scatter(train[cluster_labels == 2, 0], train[cluster_labels == 2, 1], train[cluster_labels == 2, 2], s = 50, c = color_clutser[2][0], alpha = 0.3, label = color_clutser[2][1]) #ax2.scatter(train[cluster_labels == 3, 0], train[cluster_labels == 3, 1], train[cluster_labels == 3, 2], s = 50, c = color_clutser[3][0], alpha = 0.3, label = color_clutser[3][1]) #ax2.scatter(train[cluster_labels == 4, 0], train[cluster_labels == 4, 1], train[cluster_labels == 4, 2], s = 50, c = color_clutser[4][0], alpha = 0.3, label = color_clutser[4][1]) ax2.set_title("3D - CLUSTERING with K-Means", pad=20) ax2.set_xlabel("Ax_1") ax2.set_ylabel("Ax_2") ax2.set_zlabel("Ax_3") #ax2.dist = 10 plt.suptitle( ("Silhouette analysis for KMeans clustering on sample data " "with n_clusters = %d" % n_clusters), fontsize=14, fontweight='bold') #ax2.legend(bbox_to_anchor=(0.20,0.60)) plt.autoscale(enable=True, axis='x', tight=True) plt.savefig("SilhoutteComparison_with" + str(n_clusters) + ".png", dpi=250) plt.close() #plt.show() #extract the legend fig = plt.figure(figsize=(30, 17)) ax = fig.add_subplot(111, projection='3d') # Labeling the clusters centers = clusterer.cluster_centers_ # Draw white circles at cluster centers ax.scatter(centers[:, 0], centers[:, 1], centers[:, 2], marker='*', c="lightyellow", s=150, edgecolor='k', label="Centroids", alpha=0.8) #Plotting the centroids number #for i, c in enumerate(centers): # ax2.scatter(c[0], c[1], c[2], marker='$%d$' % i, alpha=1, # s=120, edgecolor='k') for i in list(set((cluster_labels))): #LABEL ax.scatter(data[cluster_labels == i, 0], data[cluster_labels == i, 1], data[cluster_labels == i, 2], s=50, c=color_clutser[i][0], alpha=0.3, label=color_clutser[i][1]) #No label #ax.scatter(data[cluster_labels == i, 0], data[cluster_labels == i, 1], data[cluster_labels == i, 2], s = 50, c = color_clutser[i][0], alpha = 0.3) #ax2.scatter(data[cluster_labels == i, 0], data[cluster_labels == i, 1], s = 50, c = color_clutser[i][0], alpha = 0.3,label = color_clutser[i][1]) #ax2.scatter(train[cluster_labels == 1, 0], train[cluster_labels == 1, 1], train[cluster_labels == 1, 2], s = 50, c = color_clutser[1][0], alpha = 0.3, label = color_clutser[1][1]) #ax2.scatter(train[cluster_labels == 2, 0], train[cluster_labels == 2, 1], train[cluster_labels == 2, 2], s = 50, c = color_clutser[2][0], alpha = 0.3, label = color_clutser[2][1]) #ax2.scatter(train[cluster_labels == 3, 0], train[cluster_labels == 3, 1], train[cluster_labels == 3, 2], s = 50, c = color_clutser[3][0], alpha = 0.3, label = color_clutser[3][1]) #ax2.scatter(train[cluster_labels == 4, 0], train[cluster_labels == 4, 1], train[cluster_labels == 4, 2], s = 50, c = color_clutser[4][0], alpha = 0.3, label = color_clutser[4][1]) ax.set_title("3D - CLUSTERING with K-Means") ax.set_xlabel("Ax_1") ax.set_ylabel("Ax_2") ax.set_zlabel("Ax_3") ax.dist = 10 #ax.legend(loc="best", fontsize = "xx-small") ax.legend(bbox_to_anchor=(1, 1), loc=2, borderaxespad=0.) plt.savefig("legend.png", dpi=250) plt.close() #Algoritmo di base per definire come clusterizzare i dati if new_prior == "False": return k_sample_result #Ritorna il dizionario da cui possiamo stabilire la nuova priorità elif new_prior == "True": #Facciamo ritornare anche cluster labels perché al suo interno abbiamo le nuove labels return diz_priority, cluster_labels