def generate(self, title=None): max_duration = 0 for machine_nr in self.__tasks: machine = self.__tasks[machine_nr] for start_time in machine: job_nr = machine[start_time]['job_nr'] duration = machine[start_time]['duration'] color = self.__colors[job_nr % len(self.__colors)] plt.hlines(machine_nr, start_time, start_time + duration, colors=color, lw=50) plt.text(start_time + 0.1, machine_nr + 0.1, str(job_nr), bbox=dict(facecolor='white', alpha=1.0)) #fontdict=dict(color='white')) if duration + start_time > max_duration: max_duration = duration + start_time plt.margins(1) if self.__n_machines is 0: plt.axis([0, max_duration, 0.8, len(self.__tasks)]) else: plt.axis([0, max_duration, -0.8, self.__n_machines]) plt.xticks(range(0, max_duration, 1)) if title: plt.title(title) plt.xlabel("Time") plt.ylabel("Machines") if self.__n_machines is 0: plt.yticks(range(0, len(self.__tasks), 1)) else: plt.yticks(range(0, self.__n_machines, 1)) self.__fig.savefig(self.__out_dir + sep + title + '.png')
def statistics_charts(self): if plt is None: return for chart in self.stats_charts: if chart["type"] == "plot": fig = plt.figure(figsize=(8, 2)) for xdata, ydata, label in chart["data"]: plt.plot(xdata, ydata, "-", label=label) plt.legend(loc="center left", bbox_to_anchor=(1, 0.5)) elif chart["type"] == "timeline": fig = plt.figure(figsize=(16, 2)) for i, (starts, stops, label) in enumerate(chart["data"]): plt.hlines([i] * len(starts), starts, stops, label=label) plt.ylim(-1, len(chart["data"])) elif chart["type"] == "bars": fig = plt.figure(figsize=(16, 4)) plt.bar(range(len(chart["data"])), chart["data"]) elif chart["type"] == "boxplot": fig = plt.figure(figsize=(16, 4)) plt.boxplot(chart["data"]) else: raise Exception("Unknown chart") png = serialize_fig(fig) yield chart["name"], html_embed_img(png)
def plot_max_avg_Rho_lev012(lev0,lev1,lev2, ic, spath): '''lev -- TimeProfQs() object, whose lev.convert() attribute has been called''' #avgRho Tratio = lev0.t / ic.tCr fig, ax = plt.subplots() #initial plt.hlines(ic.rho0, Tratio.min(),Tratio.max(), colors="magenta", linestyles='dashed',label="initial") #lev0 plt.plot(Tratio,lev0.maxRho,ls="-",c="black",lw=2.,label="max Lev0") plt.plot(Tratio,lev0.minRho,ls="-",c="green",lw=2.,label="min Lev0") plt.plot(Tratio,lev0.avgRho,ls="-",c="blue",lw=2.,label="avg Lev0") plt.plot(Tratio,lev0.avgRho_HiPa,ls="-",c="red",lw=2.,label=r"avg ($\rho > \rho_0$)") #lev1 plt.plot(Tratio,lev1.maxRho,ls="--",c="black",lw=2.,label="max Lev1") plt.plot(Tratio,lev1.minRho,ls="--",c="green",lw=2.,label="min Lev1") plt.plot(Tratio,lev1.avgRho,ls="--",c="blue",lw=2.,label="avg Lev1") plt.plot(Tratio,lev1.avgRho_HiPa,ls="--",c="red",lw=2.,label=r"avg Lev1 ($\rho > \rho_0$)") #lev2 plt.plot(Tratio,lev2.maxRho,ls=":",c="black",lw=2.,label="max Lev2") plt.plot(Tratio,lev2.minRho,ls=":",c="green",lw=2.,label="min Lev2") plt.plot(Tratio,lev2.avgRho,ls=":",c="blue",lw=2.,label="avg Lev2") plt.plot(Tratio,lev0.avgRho_HiPa,ls=":",c="red",lw=2.,label=r"avg Lev1 ($\rho > \rho_0$)") #finish plt.yscale("log") plt.xlabel("t / t_cross") plt.ylabel("Densities [g/cm^3]") plt.title(r"Max & Avg Densities, Lev0") py.legend(loc=4, fontsize="small") name = spath+"max_avg_Rho_Lev012.pdf" plt.savefig(name,format="pdf") plt.close()
def plot_singleCurrStepResp(filename, condition, Voltages, meanVolt, fitData, Rin, tauFit_ms, plateau, DC, Cap, pre, step, volt_dt, ax0, ay0, dx, dy, xwidth, xswidth, ywidth, yswidth, plotPos): # # plot voltage traces and fit # n,m=plotPos xAxis = np.linspace(1., len(meanVolt), len(meanVolt)) axesPos = [ax0+(m*(dx+xwidth)), 1-ay0-n*(dy+ywidth), xwidth, ywidth] ax = plt.axes(axesPos) for k in np.arange(np.shape(Voltages)[1]): plt.plot(xAxis,Voltages[:,k], color = '0.8') plt.plot(meanVolt, color = 'k', linewidth = 3) plt.plot(xAxis[pre+step:pre+step+fitData.size], fitData, color = 'g', linewidth = 3) ax.set_autoscale_on(False) plotSpace = (np.max(meanVolt) - np.min(meanVolt))*0.4 ax.set_ylim(np.min(meanVolt)-plotSpace, np.max(meanVolt)+plotSpace) # ax.set_ylim((wcCurr[pre + (0.1*pre):pre + (1.8*pre),:].min())*1.3, (wcCurr[pre + (0.1*pre):pre + (1.8*pre),:].max())) # ax.set_ylim((wcCurr[2*pre:3*pre,:].min())*1.5, (wcCurr[2*pre:3*pre,:].max())*1.3) # ax.set_xlim(pre*0.5, wcCurr.shape[0]) ax.set_frame_on(False) ax.set_xticks([]) ax.set_yticks([]) # # plot scale bars # # specify size of voltage and time scalebar: voltSclb = 5 # in mV timeSclb = 0.1 # in seconds xRange = ax.get_xlim()[1] yRange = ax.get_ylim()[1] - ax.get_ylim()[0] # scalebar coordinates (Lx: left x, Rx: right x, Ly: lower y, Uy: upper y): sclbLx = ax.get_xlim()[1]-(pre) sclbRx = sclbLx + timeSclb/volt_dt sclbLy = ax.get_ylim()[1]-yRange*0.5 sclbUy = sclbLy + voltSclb # plt.hlines(ax.get_ylim()[0], pre*0.52, pre*0.52 + 0.1/volt_dt, colors = 'k', linestyles = 'solid', linewidth = 3) plt.hlines(sclbLy, sclbLx, sclbRx, colors = 'k', linestyles = 'solid', linewidth = 3) plt.text(sclbLx + 0.02/volt_dt, sclbLy - 0.1*yRange,'{0} ms'.format(timeSclb*1000), fontsize = 8,) plt.vlines(sclbLx, sclbLy, sclbUy, colors = 'k', linestyles = 'solid', linewidth = 3) # minYax = ax.get_ylim()[0] # maxYax = ax.get_ylim()[1] plt.text(sclbLx - 0.06*xRange, sclbLy + 0.3,'{0} mV'.format(voltSclb), fontsize = 8, rotation = 'vertical') # plt.text(pre+1.2*step, maxYax-(0.3*(maxYax-minYax)), 'Rin = {0} MOhm'.format(Rin), fontsize = 12) # plt.text(pre+1.2*step, maxYax-(0.4*(maxYax-minYax)), 'Tau = {0} ms'.format(tauFit_ms), fontsize = 12) # plt.text(pre+1.2*step, maxYax-(0.5*(maxYax-minYax)), 'Cap = {0} pF'.format(Cap), fontsize = 12) # plt.text(pre+1.2*step, maxYax-(0.6*(maxYax-minYax)), 'DC = {0} pA'.format(DC), fontsize = 12) plt.text(pre+step*0.5, ax.get_ylim()[1] - (ax.get_ylim()[1]-ax.get_ylim()[0])*0.0, 'file {0},{1} baseline mp: {2:.1f}mV {3} plateuiness: {4}'.format(filename, '\n', condition, '\n', plateau), fontsize = 10) # plt.text(pre*0.5, ax.get_ylim()[1] + (ax.get_ylim()[1]-ax.get_ylim()[0])*0.03, ('passive responses ' + conditions[n]), fontsize = 10) return
def plot_percentiles(data, numbins, xlim, ylim, vert = True, color = 'k', linestyle = 'solid', linew = 2): perc = 1. / numbins for i in range(1, numbins): if vert: plt.vlines(sts.scoreatpercentile(data, i * perc * 100.), ylim[0], ylim[1], color, linestyle, linewidth = linew) else: plt.hlines(sts.scoreatpercentile(data, i * perc * 100.), xlim[0], xlim[1], color, linestyle, linewidth = linew)
def test_trigger_onset(self): """ Test trigger onset function """ on_of = np.array([[6.0, 31], [69, 94], [131, 181], [215, 265], [278, 315], [480, 505], [543, 568], [605, 631]]) cft = np.concatenate((np.sin(np.arange(0, 5 * np.pi, 0.1)) + 1, np.sin(np.arange(0, 5 * np.pi, 0.1)) + 2.1, np.sin(np.arange(0, 5 * np.pi, 0.1)) + 0.4, np.sin(np.arange(0, 5 * np.pi, 0.1)) + 1)) picks = trigger_onset(cft, 1.5, 1.0, max_len=50) np.testing.assert_array_equal(picks, on_of) # check that max_len_delete drops the picks picks_del = trigger_onset(cft, 1.5, 1.0, max_len=50, max_len_delete=True) np.testing.assert_array_equal( picks_del, on_of[np.array([0, 1, 5, 6, 7])]) # # set True for visual understanding the tests if False: # pragma: no cover import matplotlib.pyplot as plt plt.plot(cft) plt.hlines([1.5, 1.0], 0, len(cft)) on_of = np.array(on_of) plt.vlines(picks[:, 0], 1.0, 2.0, color='g', linewidth=2, label="ON max_len") plt.vlines(picks[:, 1], 0.5, 1.5, color='r', linewidth=2, label="OF max_len") plt.vlines(picks_del[:, 0] + 2, 1.0, 2.0, color='y', linewidth=2, label="ON max_len_delete") plt.vlines(picks_del[:, 1] + 2, 0.5, 1.5, color='b', linewidth=2, label="OF max_len_delete") plt.legend() plt.show()
def show_fixed_lag_numberline(): fig = plt.figure() ax = fig.add_subplot(111) ax.set_xlim(0,10) ax.set_ylim(0,10) # draw lines xmin = 1 xmax = 9 y = 5 height = 1 plt.hlines(y, xmin, xmax) plt.vlines(xmin, y - height / 2., y + height / 2.) plt.vlines(4.5, y - height / 2., y + height / 2.) plt.vlines(6, y - height / 2., y + height / 2.) plt.vlines(xmax, y - height / 2., y + height / 2.) plt.vlines(xmax-1, y - height / 2., y + height / 2.) # add numbers plt.text(xmin, y-1.1, '$x_0$', fontsize=20, horizontalalignment='center') plt.text(xmax, y-1.1, '$x_k$', fontsize=20, horizontalalignment='center') plt.text(xmax-1, y-1.1, '$x_{k-1}$', fontsize=20, horizontalalignment='center') plt.text(4.5, y-1.1, '$x_{k-N+1}$', fontsize=20, horizontalalignment='center') plt.text(6, y-1.1, '$x_{k-N+2}$', fontsize=20, horizontalalignment='center') plt.text(2.7, y-1.1, '.....', fontsize=20, horizontalalignment='center') plt.text(7.2, y-1.1, '.....', fontsize=20, horizontalalignment='center') plt.axis('off') plt.show()
def codon_usage_sics_nonsics(pfname, tseq, figname): tid2sig, tid2rest = get_sig_peaks_from_file(pfname) codon_list = generate_cc_list() codon_usage = get_codon_usage(tid2rest.keys(), 1, 1, tseq, True) codon_peak = get_codon_peak_freq(tid2rest, tseq) nonsics_bg = codon_dic_to_list(codon_list, codon_usage) nonsics_fg = codon_dic_to_list(codon_list, codon_peak) print "nonsics to bg", scipy.spatial.distance.euclidean(nonsics_bg, nonsics_fg) codon_usage = get_codon_usage(tid2sig.keys(), 1, 1, tseq, True) codon_peak = get_codon_peak_freq(tid2sig, tseq) sics_bg = codon_dic_to_list(codon_list, codon_usage) sics_fg = codon_dic_to_list(codon_list, codon_peak) print "sics to bg", scipy.spatial.distance.euclidean(sics_bg, sics_fg) print "sics to nonsics", scipy.spatial.distance.euclidean(sics_fg, nonsics_fg) sics_ratio = np.ones(len(sics_bg)) sics_ratio[sics_bg!=0] = sics_fg[sics_bg!=0]/sics_bg[sics_bg!=0] sics_ratio = np.log2(sics_ratio) print "plotting" aa2color = get_aa_colormap() c = [ aa2color[codon2aa[codon]] for codon in codon_list ] x = range(len(codon_list)) plt.figure(figsize=(12,6)) plt.bar(x, sics_ratio, color=c, edgecolor='white', align='center') plt.hlines([1,-1], x[0]-1, x[-1]+1, 'r') plt.xticks(x, codon_list, rotation='vertical', fontsize=12) plt.xlim((x[0]-1, x[-1]+1)) plt.ylabel('codon usage ratio log2(fg/bg)') plt.tight_layout() plt.savefig(figname, bbox_inches='tight') plt.close()
def _expand_dendrogram(cNode,swc_tree,off_x,off_y,radius,transform='plain') : global max_width,max_height # middle name d.i.r.t.y. ''' Gold old fashioned recursion... sys.setrecursionlimit()! ''' place_holder_h = H_SPACE max_degree = swc_tree.degree_of_node(cNode) required_h_space = max_degree * place_holder_h start_x = off_x-(required_h_space/2.0) if(required_h_space > max_width) : max_width = required_h_space if swc_tree.is_root(cNode) : print 'i am expanding the root' cNode.children.remove(swc_tree.get_node_with_index(2)) cNode.children.remove(swc_tree.get_node_with_index(3)) for cChild in cNode.children : l = _path_between(swc_tree,cChild,cNode,transform=transform) r = cChild.content['p3d'].radius cChild_degree = swc_tree.degree_of_node(cChild) new_off_x = start_x + ( (cChild_degree/2.0)*place_holder_h ) new_off_y = off_y+(V_SPACE*2)+l r = r if radius else 1 plt.vlines(new_off_x,off_y+V_SPACE,new_off_y,linewidth=r,colors=C) if((off_y+(V_SPACE*2)+l) > max_height) : max_height = off_y+(V_SPACE*2)+l _expand_dendrogram(cChild,swc_tree,new_off_x,new_off_y,radius=radius,transform=transform) start_x = start_x + (cChild_degree*place_holder_h) plt.hlines(off_y+V_SPACE,off_x,new_off_x,colors=C)
def get_samples(depends_on, includes, burn): """Get all samples from all runs.""" paths = make_paths(depends_on, includes, 0) sample_files = [f for f in ld(paths['model_dir']) if 'samples_' in f] dfs = [ pd.read_csv(pj(paths['model_dir'], f), index_col=0).ix[1000:] for f in sample_files ] df = pd.concat(dfs, axis=0) for vcol in [c for c in df.columns if 'v_' in c]: df[vcol] = -df[vcol] for i, iv in enumerate(['age', 'group', 'group:age', 'Intercept'], 1): plt.subplot(2, 2, i) if iv == 'Intercept': _df = df[['%s_%s' % (p, iv) for p in 'atv']] else: _df = df[['%s_%s' % (p, iv) for p in depends_on]] sns.violinplot(data=_df) plt.hlines(0, 0, 3) plt.show() return df
def width_feature(im, f2, plot=False): ''' f7 is median of the gap lengths f8 is f2 / f7 ''' transitions = [] for i in xrange(len(im)): groups = itertools.groupby(list(im[i])) transitions.append((i, len([x for x in groups]))) max_line, max_trans = max(transitions, key=operator.itemgetter(1)) gap_lengths = [] zeros_start = 0 zeros = True for i in xrange(len(im[max_line])): pixel = im[max_line][i] if pixel == 1 and zeros is True: gap_lengths.append(i - zeros_start) zeros = False if pixel == 0 and zeros is not True: zeros_start = i zeros = True if plot: print gap_lengths plt.imshow(im, cmap=cm.Greys_r) plt.hlines(numpy.array([max_line]), 0, im.shape[1], linewidth=3, colors='y') plt.show() f7 = numpy.median(gap_lengths) return f7, float(f2) / f7
def plot_simp(n): xi, wi = qnwsimp(n+1, xmin, xmax) fig = plt.figure() plt.plot(x, f(x), linewidth=3, label=r'$f(x)$') for k in range(n//2): xii = xi[(2*k):(2*k+3)] xiii = linspace(xii[0], xii[2], 125) p = fitquad(xii) plt.fill_between(xiii, p(xiii), color='yellow') if k==0: plt.plot(xiii, p(xiii),'r--', label=r'$\tilde{f}_{%d}(x)$' % (n+1)) else: plt.plot(xiii, p(xiii),'r--') plt.vlines(xi, 0, f(xi),'k', linestyle=':') plt.hlines(0,xmin-0.1, xmax+0.1,'k',linewidth=2) plt.xlim(xmin-0.1, xmax+0.1) xtl = ['$x_{%d}$' % i for i in range(n+1)] xtl[0] += '=a' xtl[n] += '=b' plt.xticks(xi, xtl) plt.yticks([0],['0']) plt.legend() return fig
def MissingMassVolume(path): mvd_pt = open(path+'mvd_pt.pkl','rb') data = pickle.load(mvd_pt) mvd_pt.close() plt.figure() plt.ylabel('sum voxels') x = np.arange(0,len(data['amountRm'])) plt.scatter(x,data['amountRm'],c = 'r', label='Vol voxels removed') plt.hold(True) plt.scatter(x,data['sum_before'], c='b',label='original seg Vol') plt.scatter(x,data['vol_vox'], c = 'k',label='Final volume') m = np.mean(data['amountRm']) print m plt.hlines(m,0,len(data['amountRm'])) plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fancybox=True, shadow=True, ncol=5) plt.savefig(path+'MissingVol.png') plt.figure() plt.ylabel('sum Linear Attenuation') plt.scatter(x,data['mass'], c = 'k', label= 'Final mass') plt.scatter(x,data['mass_incompressible'], c= 'g',label='Mass "incompressible" removed') plt.hlines(np.mean(data['mass_incompressible']),0,len(data['amountRm'])) plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fancybox=True, shadow=True, ncol=5) plt.savefig(path+'Missing.png')
def plot(self): plt.hlines(self.baseCr, self.crsAll.index.min(), self.crsAll.index.max(), linestyles='dotted') plt.plot(self.crsAll.index, self.crsAll.values) plt.plot(self.crsAll.index, self.crsAll.values, 'g.') plt.title(str(self)) plt.xlabel("Date") plt.ylabel("Creatinine")
def OScrossPower(angSep, crossCorr, crossCorrErr, tex=True): if tex == True: plt.rcParams['text.usetex'] = True fig, ax = plt.subplots() seps = [] rows,cols = angSep.shape for ii in range(rows): for jj in range(ii+1,cols): seps.append(np.arccos(angSep[ii,jj])) #ax.plot(seps, crossCorr, 'k', color='#1B2ACC') #ax.fill_between(seps, crossCorr-crossCorrErr, crossCorr-crossCorrErr, #alpha=0.2, edgecolor='#1B2ACC', facecolor='#089FFF', linewidth=4, #linestyle='dashdot', antialiased=True) ax.errorbar(np.array(seps)*180.0/np.pi, crossCorr, yerr=crossCorrErr, fmt='o', color='#089FFF', ecolor='#089FFF', capsize=8, linewidth=3) plt.hlines(y=0.0, xmin=0.0, xmax=180.0, linewidth=3.0, linestyle='solid', color='black') ax.set_xlabel('Pulsar angular separation [degrees]', fontsize=20) ax.set_ylabel('Cross power', fontsize=20) ax.minorticks_on() plt.tick_params(labelsize=18) plt.grid(which='major') plt.grid(which='minor') plt.xlim(0.0,180.0) plt.title('Cross-power measurements') plt.show()
def plot_phi(): n = 10 nf = 1000 # for the red points x = np.linspace(0, 1, n) # for the curve xf = np.linspace(0, 1, nf) phi = lambda x: x + 0.1 * np.sin(3 * np.pi * x) y = phi(x) # default style 'b-' solid blue line plt.plot(xf, phi(xf), lw=2) # lw line width # 'ro' plot points as red circles plt.plot(x, y, "ro", ms=8) # ms marker size # The 'bases' (blobs) of the vlines and hlines plt.plot(x, [0] * n, "ko", ms=8) plt.plot([0] * n, y, "ko", ms=8) # Dotted vlines and hlines plt.vlines(x, [0], y, linestyle="--") plt.hlines(y, [0], x, linestyle="--") plt.title(r"$y=\phi(x)$", fontsize=24) plt.xlabel(r"$x$", fontsize=24) plt.ylabel(r"$y$", fontsize=24) plt.show()
def plot(self): global TESTING, UNITS, HEIGHTS '''Prepares the data using self.prepare_data and then graphs the data on a plot.''' self.prepare_data() plt.plot(HEIGHTS, self.data) plt.hlines(self.significant_shear, 0, HEIGHTS[-1]) plt.vlines(self.significant_shear_height, -1, 2) print 'Significant shear at image {0}'.format(self.x_significant_shear) if not TESTING: print 'Theoretical significant shear at height {0} {1}'.format(self.significant_shear_height, UNITS) plt.ylim([-1, 2]) plt.xlim([HEIGHTS[0], HEIGHTS[-1]]) plt.xlabel('Height ({0})'.format(UNITS)) plt.ylabel('Coverage') plt.title(self.dp_path.split('/')[-1]) try: os.mkdir('{0}/res'.format(self.dp_path)) except: pass plt.savefig('{0}/res/results.png'.format(self.dp_path)) with open('{0}/res/results.txt'.format(self.dp_path), 'w') as f: global MODE f.write('{0}\nMODE {1}\n'.format(str(self.significant_shear_height), MODE))
def multi_regression(): ''' 多元回归 :return: ''' from sklearn.cross_validation import train_test_split X = df.iloc[:, :-1].values y = df['MEDV'].values X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) slr = LinearRegression() slr.fit(X_train, y_train) y_train_pred = slr.predict(X_train) y_test_pred = slr.predict(X_test) # 计算Mean Squared Error (MSE) print('MSE train: %.3f, test: %.3f' % ( mean_squared_error(y_train, y_train_pred), mean_squared_error(y_test, y_test_pred))) # MSE train: 19.958, test: 27.196 => over fitting # 计算R*R # If R*R =1, the model ts the data perfectly with a corresponding MSE = 0 . print('R^2 train: %.3f, test: %.3f' % (r2_score(y_train, y_train_pred), r2_score(y_test, y_test_pred))) # plot plt.scatter(y_train_pred, y_train_pred - y_train, c='blue', marker='o', label='Training data') plt.scatter(y_test_pred, y_test_pred - y_test, c='lightgreen', marker='s', label='Test data') plt.xlabel('Predicted values') plt.ylabel('Residuals') plt.legend(loc='upper left') plt.hlines(y=0, xmin=-10, xmax=50, lw=2, color='red') plt.xlim([-10, 50]) plt.show()
def plot_comparison_numpy(params1, label1, color1='black', params2=None, label2="", color2='red', title="", plotname=""): """Plot same fn for 2 sets of parameters, with indiviudal labels Uses numpy. """ pt = np.arange(0.5, 20, 0.5) corrections1 = pf_func(pt, params1) plt.plot(pt, corrections1, 'x-', color=color1, label=label1, lw=1.5) if params2: corrections2 = pf_func(pt, params2) plt.plot(pt, corrections2, 'd-', color=color2, label=label2, lw=1.5) plt.xlabel(r"$p_T^{in} \mathrm{[GeV]}$") plt.ylabel("Corr. factor") # plt.set_xscale('log') plt.minorticks_on() plt.grid(b=True, which='major', axis='both') plt.grid(b=True, which='minor', axis='both') plt.xlim(left=pt[0]-0.5) # draw intersection lines for 5, 10 for p, lc in zip([0.5, 5, 10], ["purple", "blue", "green"]): corr = pf_func(p, params1) plt.vlines(p, ymin=plt.ylim()[0], ymax=corr, color=lc, linestyle='dashed', linewidth=1.5, label=r'$p_T^{in}$' + ' = %g GeV,\ncorr. factor = %.3f' % (p, corr)) plt.hlines(corr, xmin=0, xmax=p, color=lc, linestyle='dashed', linewidth=1.5) plt.title(title) plt.legend(fontsize=12, loc=0) if plotname != "": plt.savefig(plotname) plt.cla()
def model_scatterplot(iterables, colors=['r','b'], mean_mm2_ferret=0.75, lw=3, s=60, hline_width=0.16): """ Scatterplot of simulation data for the GCAL and L models to overlay over the animal data. The two iterables are to contain zipped kmax and pinwheel densities. mean_mm2_ferret - mean hypercolumn size in millimeters squared Sets a value for the models (uncalibrated). """ fig = plt.figure(figsize=(8,6)) ax = plt.subplot(111) plt.axhline(y=math.pi, linestyle='dotted') for iterable, color in zip(iterables, colors): units_per_hc, pw_densities = zip(*iterable) units_per_hc2 = np.array(units_per_hc)**2 # Center both clusters on the given mean hypercolumn size for ferret. HCmm2 = units_per_hc2 * (mean_mm2_ferret / units_per_hc2.mean()) point_kws = dict(edgecolor=color, marker='o', facecolor=(0,0,0,0)) plt.scatter(HCmm2, pw_densities, s=s,lw=lw, **point_kws) medx = np.median(pw_densities); medy = np.median(HCmm2) hline_kws = dict(colors=color, linestyles='solid', lw=lw) plt.hlines(medy, medx-hline_width, medx+hline_width, **hline_kws) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) [spine.set_visible(False) for spine in ax.spines.values()] plt.ylim((0, 12)); plt.xlim((0, 1.1)) return fig
def plot_posteriors(x, y, labels, gs=6, bins=50, nlevels=10, cmap=plt.cm.gray_r, kde=True): # levels = get_levels(x, y, bins=bins) plt.subplot2grid((gs, gs), (1, 0), rowspan=gs-1, colspan=gs-1, axisbg=cmap(1./(nlevels+1))) if kde: X, Y, Z = kernel_2d(x, y) # plt.contourf(X, Y, Z, levels=levels, cmap=cmap) plt.contourf(X, Y, Z, nlevels, cmap=cmap) else: # plot_contour(x, y, bins=bins, levels=levels, cmap=cmap) plot_contour(x, y, bins=bins, nlevels=nlevels, cmap=cmap) plt.xlabel(labels[0]) # plt.ylabel(labels[1], rotation=0) plt.ylabel(labels[1]) xlim, ylim = plt.xlim(), plt.ylim() plt.gca().ticklabel_format(style='plain', axis='both') plt.subplot2grid((gs, gs), (0, 0), colspan=gs-1, axisbg=cmap(1./(nlevels+1))) plt.hist(x, bins=50, histtype='stepfilled', alpha=0.5, color=cmap(1-1./nlevels)) # plt.vlines(alpha_pred, *plt.ylim()) plt.vlines([x.mean()+x.std(), x.mean()-x.std()], *plt.ylim(), linestyles='dashed') plt.gca().get_xaxis().set_visible(False) plt.gca().get_yaxis().set_visible(False) plt.xlim(*xlim) plt.subplot2grid((gs, gs), (1, gs-1), rowspan=gs-1, axisbg=cmap(1./(nlevels+1))) plt.hist(y, bins=50, histtype='stepfilled', alpha=0.5, orientation='horizontal', color=cmap(1-1./nlevels)) # plt.hlines(beta_pred, *plt.xlim()) plt.hlines([y.mean()+y.std(), y.mean()-y.std()], *plt.xlim(), linestyles='dashed') plt.gca().get_xaxis().set_visible(False) plt.gca().get_yaxis().set_visible(False) plt.ylim(*ylim) plt.show()
def singlet_return_with_dc_noise(**params): plt.figure() operators = ['evolution'] #operators.append('J_hf') # UNCOMMENT THIS LINE TO INCLUDE HF NOISE AS WELL AS DC times = dqd.p.range('t',t=(0,'3*T_2s',1000),**params) times2 = dqd.p.convert(times,output='ns') results = dqd.measure.amplitude.iterate(ranges={'de':('-3*e_deviation','3*e_deviation',30)},int_times=times,int_initial=['ground'],params=params,int_operators=operators) ranges,ranges_eval,r = results.ranges, results.ranges_eval, results.results['amplitude'] des = ranges_eval['de'] weights = stats.norm.pdf(des,loc=0,scale=dqd.p('e_deviation',**params)) amps = np.average(r['amplitude'],axis=0,weights=weights)[0] for i in xrange(r['amplitude'].shape[3]): plt.plot(times2, amps[:,i], label='amplitude_%d'%i) plt.plot(times2,[dqd.p(p_DC,t=t,**params) for t in times]) plt.vlines([dqd.p('_T_2s',**params).value],0,1,linestyles='dashed') plt.grid() xext = plt.xlim() plt.xlabel("ns") plt.ylim(-0.1,1.1) plt.hlines([0.5+0.5/math.e,0.5-0.5/math.e],*xext) plt.savefig('noise_DC.pdf')
def draw_clade(clade, x_start, color='k', lw=1): """Recursively draw a tree, down from the given clade.""" x_here = x_posns[clade] y_here = y_posns[clade] # phyloXML-only graphics annotations if hasattr(clade, 'color') and clade.color is not None: color = clade.color.to_hex() if hasattr(clade, 'width') and clade.width is not None: lw = clade.width # Draw a horizontal line from start to here plt.hlines(y_here, x_start, x_here, color=color, lw=lw) # Add node/taxon labels label = label_func(clade) if label not in (None, clade.__class__.__name__): plt.text(x_here, y_here, ' ' + label, fontsize=10, verticalalignment='center') # Add confidence if hasattr(clade, 'confidences'): # phyloXML supports multiple confidences conf_label = '/'.join(map(str, map(float, clade.confidences))) elif clade.confidence is not None: conf_label = str(clade.confidence) else: conf_label = None if conf_label: plt.text(x_start, y_here, str(float(clade.confidence)), fontsize=9) if clade.clades: # Draw a vertical line connecting all children y_top = y_posns[clade.clades[0]] y_bot = y_posns[clade.clades[-1]] # Only apply widths to horizontal lines, like Archaeopteryx plt.vlines(x_here, y_bot, y_top, color=color) # Draw descendents for child in clade: draw_clade(child, x_here, color=color, lw=lw)
def show(self): import numpy as np import matplotlib.pyplot as plt from scipy.signal import freqz # Sample rate and desired cutoff frequencies (in Hz). fs = 5000.0 lowcut = 500.0 highcut = 1250.0 order = 5 bpf = BandPassFilter(lowcut, highcut, fs, order) # Plot the frequency response for a few different orders. plt.figure(1) plt.clf() for order in [3, 6, 9]: b, a = bpf.butter_bandpass(order) w, h = freqz(b, a, worN=2000) plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="order = %d" % order) plt.plot([0, 0.5 * fs], [np.sqrt(0.5), np.sqrt(0.5)], '--', label='sqrt(0.5)') plt.xlabel('Frequency (Hz)') plt.ylabel('Gain') plt.grid(True) plt.legend(loc='best') # Filter a noisy signal. T = 0.05 nsamples = T * fs t = np.linspace(0, T, nsamples, endpoint=False) a = 0.02 f0 = 600.0 x = 0.1 * np.sin(2 * np.pi * 1.2 * np.sqrt(t)) x += 0.01 * np.cos(2 * np.pi * 312 * t + 0.1) x += a * np.cos(2 * np.pi * f0 * t + .11) x += 0.03 * np.cos(2 * np.pi * 2000 * t) """ wf = wave.open('../Alarm01.wav', 'rb') signal = wf.readframes(1024) signal = np.fromstring(signal, dtype=np.int16) x = signal[0::2] right = signal[1::2] """ y = bpf.butter_bandpass_filter(x, order) plt.figure(2) plt.clf() plt.plot(t, x, label='Noisy signal') plt.plot(t, y, label='Filtered signal (%g Hz)' % f0) plt.xlabel('time (seconds)') plt.hlines([-a, a], 0, T, linestyles='--') plt.grid(True) plt.axis('tight') plt.legend(loc='upper left') plt.show()
def PlotProfile(label1, label2, data1, data2, filename, smooth, binsz): plt.clf() plt.figure(figsize=(5,4)) plt.rc('text', usetex=True) plt.rc('font', family='serif') counts1 = np.append(data1['FLUX'].data/(EXPOSURE*PIXEL_SA), 0) glats1 = np.append(data1['GLAT_MIN'][0], data1['GLAT_MAX'].data) profile1 = np.histogram(glats1, bins=np.sort(glats1), weights=counts1) xstep=binsz y_smooth_1 = gaussian_filter(profile1[0], smooth / xstep) print y_smooth_1.mean() counts2 = np.append(data2['FLUX'].data/(EXPOSURE*PIXEL_SA*(data2['FLUX'].data.size())), 0) glats2 = np.append(data2['GLAT_MIN'][0], data2['GLAT_MAX'].data) profile2 = np.histogram(glats2, bins=np.sort(glats2), weights=counts2) xstep=binsz y_smooth_2 = gaussian_filter(profile2[0], smooth / xstep) print y_smooth_2.mean() x1 = 0.5 * (glats1[1:] + glats1[:-1]) plt.plot(x1, y_smooth_1, label='{0}'.format(label1)) plt.plot(x1, y_smooth_2, label='{0}'.format(label2)) plt.hlines(0, LatLow+binsz, LatHigh-binsz) plt.xlabel(r'Galactic Latitude/$deg$', fontsize=10) plt.ylabel(r'Surface Brightness/ph cm$^{-2}$ s$^{-1} sr^{-1}$', fontsize=10) plt.xlim([LatLow+binsz, LatHigh-binsz]) plt.tick_params(axis='x', labelsize=10) plt.grid(b=True, which='major', color='0.75', linewidth=0.5) plt.legend(prop={'size':8}) plt.savefig(filename)
def graphMembershipIncomeOverTime(membertimedata, memberinfos, membership): ydates, yvalues = zip(*membertimedata) ydates = list(ydates) yvalues = map(lambda x:x[1],yvalues) plotlabel = u"Membership Income over time" plt.plot(ydates, yvalues, '^g',linewidth=2, markevery=1) plt.ylabel("Euro") plt.xlabel("Month") plt.grid(True) plt.title(plotlabel) ## label with +x-y members per month membersinmonth = dict(membertimedata) #print "\n".join([ "%s:%s" % (x[0],str(x[1])) for x in extractPlusMinusFromMemberships(membership).items()]) pm_dates, pm_fee_dates = extractPlusMinusFromMemberships(membership) for astrdate, tpl in pm_fee_dates.items(): adate = datetime.datetime.strptime(astrdate, dateformat_monthonly_).date()- dateutil.relativedelta.relativedelta(months=1) assert(adate.day==1) if adate in membersinmonth: plt.vlines(adate + dateutil.relativedelta.relativedelta(days=11),tpl.minus+membersinmonth[adate][1],tpl.plus+membersinmonth[adate][1]) plt.hlines(membersinmonth[adate][1], adate + dateutil.relativedelta.relativedelta(days=10),adate + dateutil.relativedelta.relativedelta(days=12)) xstart,xend = plt.xlim() locs, labels = plt.xticks(np.arange(xstart,xend,61)) plt.gca().xaxis.set_major_formatter(plt.matplotlib.dates.DateFormatter(dateformat_monthonly_, tz=None)) plt.setp(labels, rotation=80) plt.subplots_adjust(left=0.06, bottom=0.08, right=0.99, top=0.95)
def plot_mdotdata_mdotsteadyline(simul,tnorm,mdot,args): '''1 plot per sink, so nsink plots this is a check that steady mdot calculation looks reasonable for each sink's mdot data''' time= simul.time/tBHA mdotBHA= simul.mdot.copy()/mdot.norm/args.answer for sid in range(8): #range(args.nsinks): lab="" if args.vrms_vs_time: lab= " vrms=f(t) corrected" plt.plot(time,mdotBHA,'k-',label='data'+lab) steady=simul.tavg_mdot[sid]/mdot.norm/args.answer plt.hlines(simul.tavg_mdot[sid]/mdot.norm/args.answer,time.min(),time.max(),\ linestyles='dashed',colors='b',label="steady= %.2g" % steady) plt.xlabel("$\mathbf{ t/t_{bh} }$") ylab= r"$\mathbf{ \dot{M} / \dot{M}_0 / \dot{M}_{ans} }$" ylab += r" (<$\mathbf{ \dot{M} }$> after t= %.2f tBH)" % args.tReachSteady plt.ylabel(ylab) plt.yscale('log') # ax.legend(loc=4) if args.ylim: plt.ylim(args.ylim[0],args.ylim[1]) if args.xlim: plt.xlim(args.xlim[0],args.xlim[1]) if args.LinearY: plt.yscale('linear') plt.legend(loc=(1.01,0.01),ncol=2,fontsize='xx-small') fsave="mdot_%d.png" % sid plt.savefig(fsave) plt.close()
def plot_turb_lines(linestyles="solid", linewidth=2, color="gray"): plt.hlines(0.5, -1, 1, linestyles=linestyles, colors=color, linewidth=linewidth) plt.vlines(-1, -0.2, 0.5, linestyles=linestyles, colors=color, linewidth=linewidth) plt.vlines(1, -0.2, 0.5, linestyles=linestyles, colors=color, linewidth=linewidth)
def plot(self, slice=None): if slice == None: slice = len(self.cutoffs) plt.hlines(1,0,1) plt.eventplot(self.cutoffs[:slice], orientation='horizontal', colors='b') plt.show() plt.pause(0.0001)
def animal_scatterplot(json_filename, lw=3, s=60, hline_width=0.16): """ Reproduces the animal data from Kaschube et al. 2010 as a scatter plot in a similar style. One key difference is that the area of the markers no longer signifies anything. """ fig = plt.figure(figsize=(8,6)) ax = plt.subplot(111) plt.axhline(y=math.pi, linestyle='dotted') d = json.load(open(json_filename,'r'))['Kaschube 2010'] animals = ('Ferret','Tree shrew','Galago') colors = ('#27833a','#7fcbf1', '#f9ab27') for (animal, color) in zip(animals,colors): point_kws = dict(edgecolor=color, marker='D', facecolor=(0,0,0,0)) plt.scatter(d[animal]['x'], d[animal]['y'], s=s, lw=lw, **point_kws) medx = np.median(d[animal]['x']) medy = np.median(d[animal]['y']) hline_kws = dict(colors=color, linestyles='solid', lw=lw) plt.hlines(medy, medx-hline_width, medx+hline_width, **hline_kws) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) [spine.set_visible(False) for spine in ax.spines.values()] plt.ylim((0, 12)); plt.xlim((0, 1.1)) return fig
import matplotlib.pyplot as plt import numpy as np from scipy import stats def ginv(x): y = 1/(1+np.exp(-1*x + 5)) return y mu =6; sigma=1 n_samples=1e6 x = stats.norm.rvs(loc=mu, scale=sigma**0.5, size=int(n_samples)) y = ginv(x) plt.hist(x, bins=50, align='mid', orientation='vertical', rwidth=0.8,density=True, color='red') plt.hist(y, bins=50, align='mid', orientation='horizontal', rwidth=0.8,density=True, color='green') x_s = np.linspace(np.min(x), np.max(x), 100) y_s = ginv(x_s) plt.plot(x_s, y_s, color='blue', linestyle='-', linewidth=3) plt.vlines(mu, 0, ginv(mu), color='black', linewidth=3) plt.hlines(ginv(mu), 0, mu, color='black', linewidth=3) plt.show()
ppm_us = decomp_us[frequ_rows] ppm_DS = decomp_DS[frequ_rows] pp = numpy.concatenate((ppm_us[:, plusmodes].T, ppm_DS[:, plusmodes].T)) pm = numpy.concatenate((ppm_us[:, minusmodes].T, ppm_DS[:, minusmodes].T)) S[:, :, fIndx] = numpy.dot(pp, numpy.linalg.pinv(pm)) # %% # 5. Plot # ------- # We can plot the transmission coefficients. Transmission coefficients higher than 1 indicate frequencies where # amplification can occur. amplification_us = numpy.abs(S[1, 0, :]) > 1 amplification_ds = numpy.abs(S[0, 1, :]) > 1 plt.hlines(1, frequs[0], frequs[-1], linestyles="dashed", color="grey") plt.plot(frequs, numpy.abs(S[1, 0, :]), ls="-", color="#D38D7B", alpha=0.5) plt.plot(frequs, numpy.abs(S[0, 1, :]), ls="-", color="#67A3C1", alpha=0.5) plt.plot(frequs[amplification_us], numpy.abs(S[1, 0, amplification_us]), ls="-", color="#D38D7B", label="Transmission Upstream") plt.plot(frequs[amplification_ds], numpy.abs(S[0, 1, amplification_ds]), ls="-", color="#67A3C1", label="Transmission Downstream")
plt.show() explained_variance = [] for i in range(1, data.shape[1] + 1): compare_pca = PCA(n_components=i) compare_pca.fit_transform(data) explained_variance.append(sum(compare_pca.explained_variance_ratio_)) plt.figure(figsize=(9, 6)) plt.bar(range(1, len(explained_variance) + 1), explained_variance, alpha=0.5, align='center') plt.hlines(0.85, 0.6, 9.4, colors='red') plt.ylabel('Sum of explained variance ratio') plt.xlabel('Components count') plt.show() # PCA 85% pca_85 = PCA(n_components=4, whiten=True) pca_data_85 = pca_85.fit_transform(data) print(pca_85) print(pca_85.explained_variance_ratio_) # PCA inverse pca_inverse = pca.inverse_transform(pca_data) pca_inverse_85 = pca_85.inverse_transform(pca_data_85) df_inverse_pca = pd.DataFrame(pca_inverse) df_inverse_pca.columns = df.columns[:-1]
import matplotlib.pyplot as plt import numpy as np from sklearn import datasets, linear_model, metrics from sklearn.model_selection import train_test_split as tts boston = datasets.load_boston() x = boston.data y = boston.target x_train, x_test, y_train, y_test = tts(x, y, test_size=1, random_state=1) regr = linear_model.LinearRegression() regr.fit(x_train, y_train) print('Coefficients: {}'.format(regr.coef_)) print('Variance Score: {}'.format(regr.score(x_test, y_test))) plt.scatter(regr.predict(x_train), regr.predict(x_train) - y_train, color='green', label='Train Data') plt.scatter(regr.predict(x_test), regr.predict( x_test)-y_test, color='blue', label='Test Data') plt.hlines(y=0, xmin=0, xmax=0, linewidth=2) plt.show()
V_b_mess = D_sorted_inv @ V_c_mess @ D_sorted_inv b_mess_normed = b_mess.copy() sigma_b_mess = np.sqrt(np.diag(V_b_mess)) for i in range(0, np.shape(b_mess_normed)[0]): b_mess_normed[i, :] /= sigma_b_mess[i] #print(np.mean(b_mess,axis = 1)) #print(np.mean(b_mess_normed, axis = 1)) #print(b_wahr) plt.plot(np.arange(20), np.mean(b_mess_normed, axis=1), 'kx', label=r'$b_{mess}$') plt.hlines([1, -1], colors='blue', xmin=0, xmax=20, linestyles='dashed') plt.xlabel(r'Index $j$') plt.ylabel(r'Koeffizient $b_j$') plt.yscale('symlog') plt.legend(loc='lower right') plt.savefig('build/A36d.pdf') plt.clf() #e) f_mess = U_sorted @ b_mess b_mess_reg = b_mess.copy() b_mess_reg[10:] = 0 f_mess_reg = U_sorted @ b_mess_reg x_plot = np.linspace(0, 2, 20) plt.plot(x_plot, np.mean(f_mess, axis=1), 'kx', label=r'$f_{unregularisiert}$') plt.plot(x_plot,
os.getcwd #3 data = scipy.io.loadmat('D:/data/1D/var6.mat') n = data['p'] max_ = np.max(n) min_ = np.min(n) mean_ = np.mean(n) median_ = np.median(n) var_ = np.var(n) std_ = np.std(n) #4 plt.plot(n) plt.hlines(mean_, 0, len(n), colors='r', linestyles='solid') plt.hlines(mean_ + std_, 0, len(n), colors='g', linestyles='dashed') plt.hlines(mean_ - std_, 0, len(n), colors='g', linestyles='dashed') plt.show() plt.hist(n, bins=20) plt.grid() plt.show() #5 def autocorrelate(a): n = len(a) cor = [] for i in range(n // 2, n // 2 + n):
coef.drop(columns=['feature', 'Unnamed: 0'], inplace=True) coef.head() x = coef.loc[:, ['coef']] coef['coef_z'] = (x - x.mean()) / x.std() coef = coef[(coef["coef_z"] < -0.4) | (coef["coef_z"] > 0.4)] coef['colors'] = ['red' if x < 0 else 'green' for x in coef['coef_z']] coef.sort_values('coef_z', inplace=True) coef.reset_index(inplace=True) coef.shape # Draw plot plt.figure(figsize=(10, 8), dpi=80) plt.hlines(y=coef.index, xmin=0, xmax=coef.coef_z) for x, y, tex in zip(coef.coef_z, coef.index, coef.coef_z): t = plt.text(x, y, round(tex, 2), horizontalalignment='right' if x < 0 else 'left', verticalalignment='center', fontdict={ 'color': 'red' if x < 0 else 'green', 'size': 14 }) # Decorations plt.yticks(coef.index, coef.features, fontsize=11) plt.title('Most Impactful Coefficients on Adoption Outcome', fontdict={'size': 14})
def IC(start_date, end_date, index_code, model_name='input', feature='alpha'): # 读取自编码器编码数据 factors_df = pd.read_csv('../model1/{}/{}_{}_{}.csv'.format( model_name, feature, start_date, end_date), index_col=['DateTime'], dtype={'SecCode': np.str}) factors_df = factors_df.reset_index().set_index(['SecCode', 'DateTime']) factors_daily_IC_dict = {} for i in range(factors_df.shape[1]): factors_daily_IC_dict[i] = pd.Series() # 获取不同时期的成份股列表 f_list = os.listdir('../data/features/amp') f_list.sort() for f in f_list: f_splits = f.split('_') if int(start_date) < int(f_splits[-1].split('.')[0]) < int( end_date) and f_splits[0] == index_code: df = pd.read_csv('../data/features/amp/{}'.format(f), index_col='DateTime') cons_stocks_list = list(df.columns) # constituent stocks cons_stocks_list = [int(con) for con in cons_stocks_list] # print(cons_stocks_list) s_date = f_splits[2] # start date e_date = f_splits[-1].split('.')[0] # end date print(s_date, e_date) for i in range(0, factors_df.shape[1]): # for each factor factor_df = factors_df[factors_df.columns[i]] factor_df = factor_df.reset_index()[ (factor_df.reset_index().DateTime > s_date) & (factor_df.reset_index().DateTime < e_date)] factor_df = factor_df.set_index(['SecCode', 'DateTime']) factor_df = factor_df.loc[:, str(i)] factor_df = factor_df.unstack(0) factor_df = factor_df.loc[:, cons_stocks_list] print(factor_df.shape) for j in range(0, 1): # for each delay future_ret_df = pd.read_csv( '../data/predicts/{}_{}_{}_return_{}-{}min.csv'.format( index_code, s_date, e_date, 241 * j, 241 * (j + 1)), index_col='DateTime') IC_min = future_ret_df.corrwith(factor_df, axis=1, drop=True) IC_daily = IC_min.iloc[range(0, (len(IC_min) // 241) * 241, 241)] factors_daily_IC_dict[i] = factors_daily_IC_dict[i].append( IC_daily) for i in range(factors_df.shape[1]): plt.figure(figsize=(16, 9), dpi=300) plt.xticks(fontsize=18) plt.yticks(fontsize=18) plt.xlabel('days', fontsize=18) plt.ylabel('IC', fontsize=18) daily_IC, = plt.plot(list(factors_daily_IC_dict[i])) daily_IC_ma, = plt.plot( list(factors_daily_IC_dict[i].rolling(30).mean())) plt.legend([daily_IC, daily_IC_ma], ['IC', 'IC 30 days\' Moving Avg.'], loc='upper left', fontsize=16) plt.plot(list(factors_daily_IC_dict[i])) plt.plot(list(factors_daily_IC_dict[i].rolling(30).mean())) plt.hlines(factors_daily_IC_dict[i].mean(), 0, len(list(factors_daily_IC_dict[i])), colors='g') plt.hlines(0.05, 0, len(list(factors_daily_IC_dict[i])), colors='r', linestyles="dashed") plt.hlines(-0.05, 0, len(list(factors_daily_IC_dict[i])), colors='r', linestyles="dashed") plt.hlines(0.1, 0, len(list(factors_daily_IC_dict[i])), colors='c', linestyles="dashed") plt.hlines(-0.1, 0, len(list(factors_daily_IC_dict[i])), colors='c', linestyles="dashed") plt.title('{}_{}_{}_factor{}'.format(model_name, start_date, end_date, i), fontsize=18) # plt.savefig('../ai/factors_testing/encoding_effects/{}/{}_{}_{}_{}_IC={}_IR={}.jpeg'. # format(mode, model_name, factors_df.columns[i], mode, data_set, factors_daily_IC_dict[i].mean(), # factors_daily_IC_dict[i].mean() / factors_daily_IC_dict[i].std())) plt.show() plt.close()
def pairwise_plots(table, output_folder, alpha=0.05): """ Makes a bunch scatter plots and histograms """ combine_plots = [ # x, y suffix, s, xlog, ylog ["Gene_Length", "Diversity", 2, True, False], ["Gene_Length", "Hits", 2, True, True], ["TA_Count", "Diversity", 4, True, False], ["TA_Count", "Hits", 4, True, True], ["Start", "Diversity", 8, False, False], ["Start", "Hits", None, False, True], ["GC", "Diversity", 4, False, True], ["GC", "Hits", 4, False, True], ["Log2FC_Reads", "Diversity", 4, False, False], ] single_plots = [ # x, y, s, xlog, ylog ["Gene_Length", "TA_Count", 6, True, True], ["Control_Hits", "Sample_Hits", 6, True, True], ["Start", "GC", None, False, False], # Reads differences ["Start", "Log2FC_Reads", None, False, False], ["Start", "LinearDiff_Reads", None, False, False], ["Log2FC_Reads", "LinearDiff_Reads", None, False, False], # Inserts differences ["Start", "Log2FC_Inserts", None, False, False], ["Start", "LinearDiff_Inserts", None, False, False], ["Log2FC_Inserts", "LinearDiff_Inserts", None, False, False], # Survival Index ["Start", "Log2SI", None, False, False], ["Log2FC_Reads", "Log2SI", None, False, False], # Fitness ["Start", "Sample_Fitness", None, False, False], ["Log2FC_Reads", "Sample_Fitness", None, False, False], ] hist_plots = [ # column, bins ["Log2SI", 100], ["Log2FC_Reads", 100], ["Log2FC_Inserts", 100], ["Sample_Fitness", 100], ["P_Value", 50], ["Q_Value", 50], ] # print(table.columns) # f = table.boxplot(column=['Control_Hits', 'Sample_Hits'], showfliers=False) # plt.savefig("temp.png") # exit() colors = {False: 'tab:green', True: 'tab:red'} p_sig_colors = table["P_Sig"].map(colors) for x, y, s, xlog, ylog in combine_plots: if not all(i in table.columns for i in [x, f"Control_{y}", f"Sample_{y}"]): continue print("Plotting Combined: x={} y={}".format(x, y)) fig = plt.figure(figsize=[16, 8]) ax = fig.add_subplot(111) ax.scatter(x=table[x], y=table[f"Control_{y}"], s=s, alpha=0.7, color="tab:green", label="Control") ax.scatter(x=table[x], y=table[f"Sample_{y}"], s=s, alpha=0.7, color="tab:red", label="Sample") ax.set_xlabel(x) ax.set_ylabel(y) if xlog: ax.set_xscale('log') if ylog: ax.set_yscale('log') plt.legend() fig.tight_layout() plt.savefig(f"{output_folder}/{x}_vs_{y}.png", bbox_inches="tight") plt.close(fig) for x, y, s, xlog, ylog in single_plots: if not all(i in table.columns for i in [x, y]): continue print("Plotting: x={} y={}".format(x, y)) fig = plt.figure(figsize=[16, 8]) ax = fig.add_subplot(111) ax.scatter(x=table[x], y=table[y], s=s, alpha=0.7, color=p_sig_colors) # plt.hlines(table[y].median(), xmin=table[x].min(), xmax=table[x].max(), color="tab:red", label="Median={:.2f}".format(table[y].median())) # plt.hlines(table[y].mean(), xmin=table[x].min(), xmax=table[x].max(), color="tab:blue", label="Mean={:.2f}".format(table[y].mean())) ax.set_xlabel(x) ax.set_ylabel(y) if xlog: ax.set_xscale('log') if ylog: ax.set_yscale('log') fig.tight_layout() plt.savefig(f"{output_folder}/{x}_vs_{y}.png", bbox_inches="tight") plt.close(fig) for col, bins in hist_plots: if col not in table.columns: continue print("Plotting Histogram: col={}".format(col)) fig = plt.figure(figsize=[12, 8]) ax = fig.add_subplot(111) df = table[col].replace([np.inf, -np.inf], np.nan, inplace=False) df.plot.hist(bins=bins) plt.xlabel(f"{col}") plt.savefig(f"{output_folder}/{col}_hist.png", bbox_inches="tight") plt.close(fig) # Make the MA plot print("Plotting MA plot") fig = plt.figure(figsize=[12, 8]) A = 0.5 * (np.log2(table["Sample_Hits"]) + np.log2(table["Control_Hits"])) M = table["Log2FC_Reads"] plt.scatter(x=A, y=M, s=10, alpha=0.9, color=p_sig_colors) plt.hlines(M.median(), xmin=A.min(), xmax=A.max(), color="tab:red", label="Median={:.2f}".format(M.median())) plt.hlines(M.mean(), xmin=A.min(), xmax=A.max(), color="tab:blue", label="Mean={:.2f}".format(M.mean())) plt.hlines([-1, 1], xmin=A.min(), xmax=A.max(), color="tab:blue", linestyle='dashed') plt.legend(loc="upper left") plt.xlabel("A = 1/2 * ( log2(sample) + log2(control) )") plt.ylabel("M = log2(sample) - log2(control)") fig.tight_layout() plt.savefig(f"{output_folder}/MA_Plot.png", bbox_inches="tight") plt.close(fig) # Make the volcano plot print("Plotting Volcano plot") for col in ["Log10P", "Log10Q"]: if col not in table.columns: continue fig = plt.figure(figsize=[12, 8]) X = table["Log2FC_Reads"] Y = table[col] plt.scatter(x=X, y=Y, s=8, alpha=0.9, color=p_sig_colors) plt.hlines(-np.log10(alpha), xmin=X.min(), xmax=X.max(), color="tab:blue", linestyle='dashed') plt.vlines([-1, 1], ymin=Y.min(), ymax=Y.max(), color="tab:blue", linestyle='dashed') plt.xlabel("log2 fold Change") plt.ylabel(f"-log10({col})") fig.tight_layout() plt.ylim(0, min(30, Y.max())) plt.savefig(f"{output_folder}/Volcano_Plot_{col}.png", bbox_inches="tight") plt.ylim(0, min(4, Y.max())) plt.savefig(f"{output_folder}/Volcano_Plot_{col}_Trim.png", bbox_inches="tight") plt.close(fig)
def plot(title: str, page: str, entries: object): entries.sort(key=lambda x: x['startedDateTime']) size = page.split('/')[-2] numObjects = page.split('/')[-1].split('.')[0].split('-')[-1] start_time = datetime.strptime(entries[0]['startedDateTime'], ISO_8601_FORMAT) captions = list(map(lambda x: x['request']['url'].split('/')[-1], entries)) captions.insert(0, '') fig = plt.figure() plt.title(title) plt.yticks(np.arange(len(entries) + 1), captions) plt.ylim(0, len(entries) + 1) if size == '10kb': if numObjects == '1': plt.xlim(0, 500) elif numObjects == '10': plt.xlim(0, 1500) elif numObjects == '100': plt.xlim(0, 5000) elif size == '100kb': if numObjects == '1': plt.xlim(0, 1000) elif numObjects == '10': plt.xlim(0, 5000) elif numObjects == '100': plt.xlim(0, 15000) elif size == '1000kb': if numObjects == '1': plt.xlim(0, 3000) elif numObjects == '10': plt.xlim(0, 10000) elif numObjects == '100': plt.xlim(0, 20000) plt.autoscale(False) plt.xlabel('Time (ms)') plt.legend(handles=[ mpatches.Patch(color='green', label='connect'), mpatches.Patch(color='cyan', label='send'), mpatches.Patch(color='yellow', label='wait'), mpatches.Patch(color='magenta', label='receive') ]) for i, entry in enumerate(entries): start = datetime.strptime(entry['startedDateTime'], ISO_8601_FORMAT) end = start + timedelta(milliseconds=entry['time']) connect, send, wait, receive, = itemgetter('connect', 'send', 'wait', 'receive')(entry['timings']) y = i + 1 xstart = (start - start_time) / timedelta(milliseconds=1) xstop = (end - start_time) / timedelta(milliseconds=1) # Total time plt.hlines(y, xstart, xstop, 'r', lw=4) # line_height = len(entries) / 40 # plt.vlines(xstart, y+line_height, y-line_height, 'k', lw=2) # plt.vlines(xstop, y+line_height, y-line_height, 'k', lw=2) # Connect time: green if connect != -1: plt.hlines(y, xstart, xstart + connect, 'g', lw=4) xstart += connect # Send time: cyan plt.hlines(y, xstart, xstart + send, 'c', lw=4) xstart += send # Wait time: yellow plt.hlines(y, xstart, xstart + wait, 'y', lw=4) xstart += wait # Receive time: magenta plt.hlines(y, xstart, xstart + receive, 'm', lw=4) xstart += receive # plt.show() graph_dir = Path.joinpath(Path.home(), 'quic', 'graphs', size, numObjects, title) Path(graph_dir).mkdir(parents=True, exist_ok=True) graph_file = Path.joinpath(graph_dir, 'graph.png') if os.path.isfile(graph_file): os.remove(graph_file) fig.savefig(graph_file, dpi=fig.dpi) plt.close(fig=fig)
VarsNames = [ 'Precipitation', 'Open Defecation', 'War', 'Elevation', 'Female Ed', 'Dist to Coasts', 'Forest Cover', 'GPD', 'Nutrition', 'Population', 'Refugees', 'HDI', 'Conflicts', 'Female Ed', 'Electricity', 'Christian', 'Muslim', 'Agriculture GDP', 'Bare' ] ##### Cumulative Importances ##### cumulative_importances = np.cumsum(importancesN) x = np.arange(len(importancesN)) fig = plt.figure(figsize=(6, 7)) plt.clf() plt.plot(x, cumulative_importances, 'g*-') plt.hlines(y=0.97, xmin=0, xmax=len(x), color='r', linestyles='dashed') plt.title('Cumalative Importances: Northern Africa') plt.grid(True, linestyle=':') plt.xticks(x, VarsNames, rotation='vertical') plt.gcf().subplots_adjust(bottom=0.2) plt.ylim([0, 1.05]) plt.savefig(wdfigs + 'cumulative_importances_north.pdf') ### South for icountry in range(len(southernCountries)): vars()['Importances' + southernCountries[icountry]] = {} f = open(wdfigs + 'randomForest/' + southernCountries[icountry] + '/importances.csv') l = -1 for line in f: l += 1
plt.scatter(y_train_pred, y_train_pred - y_train, c='steelblue', marker='o', edgecolor='white', label='Training data') plt.scatter(y_test_pred, y_test_pred - y_test, c='limegreen', marker='s', edgecolor='white', label='Test data') plt.xlabel('Predicted values') plt.ylabel('Residuals') plt.legend(loc='upper left') plt.hlines(y=0, xmin=-10, xmax=50, color='black', lw=2) plt.xlim([-10, 50]) plt.tight_layout() plt.show() print('MSE train: %.3f, test: %.3f' % (mean_squared_error( y_train, y_train_pred), mean_squared_error(y_test, y_test_pred))) print('R^2 train: %.3f, test: %.3f' % (r2_score(y_train, y_train_pred), r2_score(y_test, y_test_pred))) print("\n") # Ridge model performance evaluation print("Ridge model performance evaluation") ridge = Ridge(alpha=1.0) ridge.fit(X_train, y_train) y_train_pred = ridge.predict(X_train) y_test_pred = ridge.predict(X_test)
def clustering(): global target, daypara, df, df2, df_4pycaret, df_temp st.write(df) lookback = len(df.index) * (-1) X = np.array(df["price"][lookback:]) sum_of_squared_distances = [] K = range(1, 15) for k in K: km = KMeans(n_clusters=k) km = km.fit(X.reshape(-1, 1)) sum_of_squared_distances.append(km.inertia_) kn = KneeLocator(K, sum_of_squared_distances, S=1.0, curve="convex", direction="decreasing") kn.plot_knee(figsize=(7, 3)) st.set_option('deprecation.showPyplotGlobalUse', False) st.subheader("Search Number of Regime") st.pyplot() st.subheader("Plotting in Reallity") with st.spinner("Loading Chart..."): kmeans = KMeans(n_clusters=kn.knee).fit(X.reshape(-1, 1)) c = kmeans.predict(X.reshape(-1, 1)) minmax = [] for i in range(kn.knee): minmax.append([-np.inf, np.inf]) for i in range(len(X)): cluster = c[i] if X[i] > minmax[cluster][0]: minmax[cluster][0] = X[i] if X[i] < minmax[cluster][1]: minmax[cluster][1] = X[i] plt.figure(figsize=(11, 5), dpi=30) plt.title("Clustering Pressure/Support of {}".format(target), fontsize=20) plt.ylabel("price") index_p = [] index_s = [] a = np.transpose(minmax) a = np.sort(a) for i in range(len(X)): colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'] c = kmeans.predict(X[i].reshape(-1, 1))[0] color = colors[c] if X[i] in a[1]: index_s.append(i) if X[i] in a[0]: index_p.append(i) plt.scatter(i, X[i], c=color, s=20, marker="o") for i in range(len(minmax)): plt.hlines(a[0][i], xmin=index_p[i] - 10, xmax=index_p[i] + 10, colors="red", linestyle="--") plt.text(index_p[i] - 15, a[0][i], "Pressure= {:.2f}".format(a[0][i]), fontsize=13) plt.hlines(a[1][i], xmin=index_s[i] - 10, xmax=index_s[i] + 10, colors="b", linestyle="--") plt.text(index_s[i] - 15, a[1][i], "Support= {:.2f}".format(a[1][i]), fontsize=13) st.set_option('deprecation.showPyplotGlobalUse', False) st.pyplot()
if True: ext_size, rot, shear, scale, stretch, twist = getWarpParams(ps, amount=1.0) t = [] for i in xrange(10000): ext_size, rot, shear, scale, stretch, twist = getWarpParams(ps, amount=1.0); t.append(ext_size) # img_in = maketestimage(eff_size) # img_in = paddImage(img_in, ext_size, left_exc)[None] img_in = maketestimage(ext_size)[None] out = warp2dFast(img_in, ps, rot, shear, scale, stretch) plt.figure() plt.subplot(121) plt.imshow(img_in[0], interpolation='none', cmap='gray') plt.hlines(ext_size[0] / 2 - 0.5, 0, ext_size[1] - 1, color='r') plt.vlines(ext_size[1] / 2 - 0.5, 0, ext_size[0] - 1, color='r') plt.subplot(122) plt.imshow(out[0], interpolation='none', cmap='gray') plt.hlines(ps[0] / 2 - 0.5, 0, ps[1] - 1, color='r') plt.vlines(ps[1] / 2 - 0.5, 0, ps[0] - 1, color='r') if False: # visual 2d #out = _warp2d_c(test_img, 20, 10, (1,1.1), (0.1, 0)) test_img = np.concatenate((test_img[None], np.exp(test_img[None])), axis=0) out2 = warp2dFast(test_img, (512, 512), 20, 10, (1, 1.1), (0.1, 0)) plt.figure() plt.subplot(121) plt.imshow(test_img, interpolation='none', cmap='gray') plt.subplot(122) plt.imshow(out2[0], interpolation='none', cmap='gray')
def render(superposes=False, ncols=None): """ Affiche la (ou les) courbe(s) definis avec draw(). Note: Avec `superposes=True`, les titres et étiquettes de votre dernier `draw()` auront priorité sur les autres. ## Exemples - `render()` - `render(plot_par_rangee=3)` - `render(superposes=True)` ## Parametres - ncols (int): Nombre de colonnes - superposes (boolean): Si les deux graphiques sont superposés (défaut: False) """ # Vérifie pour des erreurs encontres precedamment, si il y en a, quitte la fonction if error: return "error" # Calcul du nombre de colonnes et de rangées pour les subplots if not ncols: ncols = len(graphs) // 3 + 1 num = 100 * np.ceil(len(graphs) / ncols) + 10 * ncols + 1 plt.subplots_adjust( left=0.125, bottom=0.05, right=0.9, top=0.95, wspace=0.2, hspace=0.4 ) for graph in graphs: modele, x, y, titre, legende_x, legende_y, grandeur_x, grandeur_y, ligne_x, cs, quadrillage, axes = graph # Sépare les deux graphes si on ne superpose pas if not superposes: plt.subplot(num) # Déssine les points des données expérimentales plt.plot(x, y, "+") # Si on precise un titre, legende_x ou legende_y, applique les if titre and not superposes: plt.title(titre) if legende_x: plt.xlabel(legende_x) if legende_y: plt.ylabel(legende_y) if quadrillage: plt.grid() if axes: plt.axvline(x=0, color="k", linewidth=1) plt.axhline(y=0, color="k", linewidth=1) try: f = Function(modele, libraries) except: print( """ERREUR: Le programme ne comprend pas votre équation ({}) Problèmes possibles: - vous avez un caractère non-compris par python (ex: ^ au lieu de **) - vous avez mis côte-à-côte un numéro et lettre (ex: 3x au lieu de 3*x) - vous utilisez une virgule au lieu d'un point (ex: 3,1 au lieu de 3.1) """.format( modele ) ) return "error" # Valeurs x du modèle. On cherche a tracer une ligne aussi lisse que possible, # donc il nous faut beaucoup de points entre xmin et xmax if ligne_x: xmin = min(min(x), ligne_x, 0) * 1.1 xmax = max(max(x), ligne_x) * 1.1 else: xmin = min(min(x), 0) * 1.1 xmax = max(x) * 1.1 modele_x = np.linspace(xmin, xmax, precision) # Si f.no_fit == True, on skip curve_fit() # curve_fit determine les valeures de constantes dans une fonction python negative_x = True if min(x) < 0 else False modele_y, negative_y = [], False if not f.no_fit: f.set_params(curve_fit(f.real_func, x, y)[0]) print(np.all(np.isfinite(curve_fit(f.real_func, x, y)[1]))) for i in modele_x: res = f.func(i) if res < 0: negative_y = True modele_y.append(res) residuals = np.array([ yval - f.func(xval) for xval, yval in zip(x, y) ]) ss_res = np.sum(residuals ** 2) ss_tot = np.sum((y - np.mean(y)) ** 2) r_squared = 1 - (ss_res / ss_tot) cc = str(round_cs(r_squared, 4)) if not negative_x: plt.xlim(0) if not negative_y: plt.ylim(0) # Si une abscisse est donnée, tracer la ligne correspondante pour trouver l'ordonée if ligne_x: plt.vlines(ligne_x, 0, f.func(ligne_x), color="b") plt.hlines(f.func(ligne_x), 0, ligne_x, color="b") label = modele.replace("y", grandeur_y, 1) f.stripped = f.stripped.rjust(len(label)) for param, letter in zip(f.params, f.letters): while letter in f.stripped: val = str(round_cs(param, cs)) index = f.stripped.find(letter) f.stripped = ( f.stripped[:index] + " " * len(val) + f.stripped[index + 1 :] ) label = label[:index] + val + label[index + 1 :] index = f.stripped.find("x") if index != -1: label = label[:index] + grandeur_x + label[index + 1 :] label = u"Modèle: " + label # Affiche un appercu dans la console print("========================= NOUVEAU GRAPHIQUE =========================") print( "Modèle tracé à partir de l'équation '{}' avec {} inconnus ({})".format( modele, len(f.letters), ", ".join(f.letters) ) ) print(" - " + label) print(" - " + u"R\u00B2 = " + cc) label += u"\nR\u00B2 = " + cc if superposes: label = titre + "\n" + label plt.plot(modele_x, modele_y, label=label) plt.legend() num += 1 plt.show()
def create(path: str, name: str, header: dict, traces: List[Trace], old_style: bool = False) -> Figure: rcParams.update({'font.size': 9}) log.info('Making PID plot...') fig = plt.figure('Response plot: Log number: ' + header['logNum'] + ' ' + path, figsize=(16, 8)) # gridspec devides window into 24 horizontal, 3*10 vertical fields gs1 = GridSpec(24, 3 * 10, wspace=0.6, hspace=0.7, left=0.04, right=1., bottom=0.05, top=0.97) for i, trace in enumerate(traces): ax0 = plt.subplot(gs1[0:6, i * 10:i * 10 + 9]) plt.title(trace.name) plt.plot(trace.time, trace.gyro, label=trace.name + ' gyro') plt.plot(trace.time, trace.input, label=trace.name + ' loop input') plt.ylabel('degrees/second') ax0.get_yaxis().set_label_coords(-0.1, 0.5) plt.grid() tracelim = np.max([np.abs(trace.gyro), np.abs(trace.input)]) plt.ylim([-tracelim * 1.1, tracelim * 1.1]) plt.legend(loc=1) plt.setp(ax0.get_xticklabels(), visible=False) ax1 = plt.subplot(gs1[6:8, i * 10:i * 10 + 9], sharex=ax0) plt.hlines(header['tpa_percent'], trace.time[0], trace.time[-1], label='tpa', colors='red', alpha=0.5) plt.fill_between(trace.time, 0., trace.throttle, label='throttle', color='grey', alpha=0.2) plt.ylabel('throttle %') ax1.get_yaxis().set_label_coords(-0.1, 0.5) plt.grid() plt.xlim([trace.time[0], trace.time[-1]]) plt.ylim([0, 100]) plt.legend(loc=1) plt.xlabel('log time in s') if old_style: # response vs. time in color plot plt.setp(ax1.get_xticklabels(), visible=False) ax2 = plt.subplot(gs1[9:16, i * 10:i * 10 + 9], sharex=ax0) plt.pcolormesh(trace.avr_t, trace.time_resp, np.transpose(trace.spec_sm), vmin=0, vmax=2.) plt.ylabel('response time in s') ax2.get_yaxis().set_label_coords(-0.1, 0.5) plt.xlabel('log time in s') plt.xlim([trace.avr_t[0], trace.avr_t[-1]]) else: # response vs throttle plot. more useful. ax2 = plt.subplot(gs1[9:16, i * 10:i * 10 + 9]) plt.title(trace.name + ' response', y=0.88, color='w') plt.pcolormesh(trace.thr_response['throt_scale'], trace.time_resp, trace.thr_response['hist2d_norm'], vmin=0., vmax=2.) plt.ylabel('response time in s') ax2.get_yaxis().set_label_coords(-0.1, 0.5) plt.xlabel('throttle in %') plt.xlim([0., 100.]) cmap = plt.cm.get_cmap('Blues') cmap._init() alphas = np.abs(np.linspace(0., 0.5, cmap.N, dtype=np.float64)) cmap._lut[:-3, -1] = alphas ax3 = plt.subplot(gs1[17:, i * 10:i * 10 + 9]) plt.contourf(*trace.resp_low[2], cmap=cmap, linestyles=None, antialiased=True, levels=np.linspace(0, 1, 20, dtype=np.float64)) plt.plot(trace.time_resp, trace.resp_low[0], label=trace.name + ' step response ' + '(<' + str(int(Trace.threshold)) + ') ' + ' PID ' + header[trace.name + 'PID']) if trace.high_mask.sum() > 0: cmap = plt.cm.get_cmap('Oranges') cmap._init() alphas = np.abs(np.linspace(0., 0.5, cmap.N, dtype=np.float64)) cmap._lut[:-3, -1] = alphas plt.contourf(*trace.resp_high[2], cmap=cmap, linestyles=None, antialiased=True, levels=np.linspace(0, 1, 20, dtype=np.float64)) plt.plot(trace.time_resp, trace.resp_high[0], label=trace.name + ' step response ' + '(>' + str(int(Trace.threshold)) + ') ' + ' PID ' + header[trace.name + 'PID']) plt.xlim([-0.001, 0.501]) plt.legend(loc=1) plt.ylim([0., 2]) plt.ylabel('strength') ax3.get_yaxis().set_label_coords(-0.1, 0.5) plt.xlabel('response time in s') plt.grid() meanfreq = 1. / (traces[0].time[1] - traces[0].time[0]) ax4 = plt.subplot(gs1[12, -1]) t = BANNER + " | Betaflight: Version " + header['version'] + ' | Craftname: ' + header[ 'craftName'] + \ ' | meanFreq: ' + str(int(meanfreq)) + ' | rcRate/Expo: ' + header['rcRate'] + '/' + header[ 'rcExpo'] + '\nrcYawRate/Expo: ' + header['rcYawRate'] + '/' \ + header['rcYawExpo'] + ' | deadBand: ' + header['deadBand'] + ' | yawDeadBand: ' + \ header['yawDeadBand'] \ + ' | Throttle min/tpa/max: ' + header['minThrottle'] + '/' + header['tpa_breakpoint'] + '/' + \ header['maxThrottle'] \ + ' | dynThrPID: ' + header['dynThrottle'] + '| D-TermSP: ' + header[ 'dTermSetPoint'] + '| vbatComp: ' + header['vbatComp'] plt.text(0, 0, t, ha='left', va='center', rotation=90, color='grey', alpha=0.5, fontsize=TEXTSIZE) ax4.axis('off') log.info('Saving as image...') plt.savefig(path[:-13] + name + '_' + str(header['logNum']) + '_response.png') return fig
def plot_nees(nees, dt, t_0, n_mov_ave=20, alpha=0.05, style=None, yscale='symlog', axis=None, title=None, save_path=None, display=True): n, m = nees.shape if alpha is not None: ci = [alpha / 2, 1 - alpha / 2] if axis == 0: cr = stats.chi2.ppf(ci, df=n * 6) / n elif axis == 1 or axis is None: cr = stats.chi2.ppf(ci, df=n * m * 6) / (n * m) cr_points = stats.chi2.ppf(ci, df=6) if n_mov_ave is not None: cr_bar = stats.chi2.ppf(ci, df=n_mov_ave * m * 6) / (n_mov_ave * m) if axis == 0: x = np.array(range(m)) anees = np.mean(nees, axis=0) x_lim = (0, m - 1) elif axis == 1 or axis is None: x = [t_0 + timedelta(seconds=dt * i) for i in range(n)] x_lim = (t_0.toordinal(), t_0.toordinal() + n / (24 * 60 * 60 / dt)) anees = np.mean(nees, axis=1) if n_mov_ave is not None: anees_bar = np.copy(anees) anees_bar[:] = 0 for i in range(len(anees)): anees_bar[i] = np.mean(anees[:i + 1][-n_mov_ave:]) # plot with Reward over Time fig = plt.figure() if title is not None: fig.suptitle(title) if style is None: style = 'seaborn-deep' mpl.style.use(style) # ANEES by RSO or Time if axis is None: y_lim = (np.min(nees), np.max(nees)) for i in range(m): plt.scatter(x=x, y=nees[:, i], s=2, alpha=0.5, marker='x') plt.hlines(y=np.mean(nees), xmin=x[0], xmax=x[-1], linewidth=1, color='black', label='Overall ANEES = ' + str(np.round(np.mean(anees), 2))) legend_elements = [ mpl.lines.Line2D([0], [0], marker='x', lw=0, color='black', label='NEES (Colored by RSO ID)', markerfacecolor='g', markersize=5) ] if alpha is not None: points_contained = np.mean( (nees > cr_points[0]) * (nees < cr_points[1])) plt.hlines(y=cr_points[0], xmin=x[0], xmax=x[-1], color='red', linestyle='--', label='Confidence Region (points), alpha = ' + str(alpha)) plt.hlines(y=cr_points[1], xmin=x[0], xmax=x[-1], color='red', linestyle='--') label = 'Point Confidence Region, alpha = ' + str( alpha) + '; ' + str(np.round(points_contained * 100, 2)) + '% contained' legend_elements.append( mpl.lines.Line2D([0], [0], color='red', lw=2, linestyle='--', label=label)) legend_elements.append( mpl.lines.Line2D([0], [0], color='black', lw=2, label='Overall ANEES = ' + str(np.round(np.mean(anees), 2)))) if alpha is not None: plt.hlines(y=cr[0], xmin=x[0], xmax=x[-1], color='black', linestyle='--', label='Confidence Region (overall), alpha = ' + str(alpha)) plt.hlines(y=cr[1], xmin=x[0], xmax=x[-1], color='black', linestyle='--') legend_elements.append( mpl.lines.Line2D([0], [0], color='black', lw=2, linestyle='--', label='Overall Confidence Region, alpha = ' + str(alpha))) if n_mov_ave is not None: plt.plot(x, anees_bar, linewidth=1, color='blue', label='ANEES Moving Average, n = ' + str(n_mov_ave)) legend_elements.append( mpl.lines.Line2D([0], [0], color='blue', lw=2, label='ANEES Moving Average, n = ' + str(n_mov_ave))) if alpha is not None: plt.hlines( y=cr_bar[0], xmin=x[0], xmax=x[-1], color='blue', linestyle='--', label='Confidence Region (moving average), alpha = ' + str(alpha)) plt.hlines(y=cr_bar[1], xmin=x[0], xmax=x[-1], color='blue', linestyle='--') legend_elements.append( mpl.lines.Line2D( [0], [0], color='blue', lw=2, linestyle='--', label='Moving Average Confidence Region, alpha = ' + str(alpha))) plt.axhline(y=1, linewidth=1, color='k', xmin=x_lim[0], xmax=x_lim[1]) plt.ylabel( r'$(x_i^j-\hat{x}_i^j)^T(P_{i}^j)^{-1}(x_i^j-\hat{x}_i^j)$ for i = [0, ' + str(n) + '), j = [0, ' + str(m) + ')') plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mpl.dates.DateFormatter('%H:%M')) plt.xlabel('Simulation Time (HH:MM)') plt.title( 'Normalized Estimation Error Squared (NEES) by RSO over Time') plt.legend(handles=legend_elements) if axis == 0: y_lim = (np.min(anees), np.max(anees)) plt.bar(x, anees) plt.axhline(y=np.mean(anees), linewidth=1, color='black', xmin=x_lim[0], xmax=x_lim[1], label='Overall ANEES = ' + str(np.round(np.mean(anees), 2))) legend_elements = [ mpl.lines.Line2D([0], [0], color='blue', lw=2, label='ANEES by RSO'), mpl.lines.Line2D([0], [0], color='black', lw=2, label='Overall ANEES = ' + str(np.round(np.mean(anees), 2))) ] if alpha is not None: plt.hlines(y=cr[0], xmin=x[0], xmax=x[-1], color='red', linestyle='--', label='Confidence Region, alpha = ' + str(alpha)) plt.hlines(y=cr[1], xmin=x[0], xmax=x[-1], color='red', linestyle='--') legend_elements.append( mpl.lines.Line2D([0], [0], color='red', lw=2, linestyle='--', label='Confidence Region, alpha = ' + str(alpha))) plt.ylabel( r'$\frac{1}{n_x M}\sum_{i=1}^M((x_m^i-\hat{x}_m^i)^T(P_{k|k}^i)^{-1}(x_m^i-\hat{x}_m^i))$' ) plt.xlabel('RSO ID') plt.gca().xaxis.set_major_locator(mpl.ticker.MaxNLocator(integer=True)) # plt.yticks(np.arange(y_lim[0], y_lim[1], (y_lim[1]-y_lim[0])/10)) plt.title( 'Averaged Normalized Estimation Error Squared (ANEES) by RSO') plt.legend(handles=legend_elements) elif axis == 1: y_lim = (np.min(anees), np.max(anees)) plt.plot(x, anees, linewidth=1) plt.ylabel( r'$\frac{1}{n_x M}\sum_{i=1}^M((x_m^i-\hat{x}_m^i)^T(P_{k|k}^i)^{-1}(x_m^i-\hat{x}_m^i))$' ) plt.gcf().autofmt_xdate() plt.gca().xaxis.set_major_formatter(mpl.dates.DateFormatter('%H:%M')) plt.xlabel('Simulation Time (HH:MM)') plt.title( 'Averaged Normalized Estimation Error Squared (ANEES) over Time') plt.yscale(yscale) if yscale != 'linear': plt.ylim(y_lim) plt.xlim(x_lim) plt.grid(True) if save_path is not None: plt.savefig(save_path, dpi=300, format='svg') if display: plt.show() else: plt.close()
# (array([ 1., 2., 4.]), array([ 1., 2., 3., 4.]), # array([1, 2, 1, 2, 3])) # As a second example, we now generate some random data of sailing boat speed # as a function of wind speed, and then determine how fast our boat is for # certain wind speeds: windspeed = 8 * np.random.rand(500) boatspeed = .3 * windspeed**.5 + .2 * np.random.rand(500) bin_means, bin_edges, binnumber = stats.binned_statistic( windspeed, boatspeed, statistic='median', bins=[1, 2, 3, 4, 5, 6, 7]) plt.figure() plt.plot(windspeed, boatspeed, 'b.', label='raw data') plt.hlines(bin_means, bin_edges[:-1], bin_edges[1:], colors='g', lw=5, label='binned statistic of data') plt.legend() # Now we can use ``binnumber`` to select all datapoints with a windspeed # below 1: low_boatspeed = boatspeed[binnumber == 0] # As a final example, we will use ``bin_edges`` and ``binnumber`` to make a # plot of a distribution that shows the mean and distribution around that # mean per bin, on top of a regular histogram and the probability # distribution function: x = np.linspace(0, 5, num=500)
def plot_force_data_and_position_data(overwrite_plots, smoothing, filtertype): import matplotlib.pyplot as plt import seaborn as sn import pandas as pd import os.path import numpy as np from glob import glob from forceAnalysis.utils import auxiliaryfunctions feet = ['FR', 'FL', 'HR', 'HL'] print("plotting forces and positions...") if overwrite_plots == True: path = os.path.join(os.getcwd(), "assembled_csv") summary_path = os.path.join(path, "summary_data") output_path = os.path.join(path, "force_and_position_plots") output_path2 = os.path.join(output_path, "foot_plots") auxiliaryfunctions.attempttomakefolder(output_path) auxiliaryfunctions.attempttomakefolder(output_path2) if os.path.isdir(summary_path): summary_data = pd.read_csv( os.path.join(summary_path, "summary_data.csv")) data_vel_and_freq = summary_data[[ 'run', 'velocity', 'step_frequency', 'footfallpattern', 'direction', 'surface', 'forcesbiased' ]] else: print( "no summary_data folder. Run forceAnalysis.assemble() first.") if os.path.isdir(path): number_of_files = len(glob(os.path.join(path, "*.csv"))) print("number_of_files = ", number_of_files) if number_of_files == 0: print( "no files in assemble_csv found, run forceAnalysis.assemble() first." ) exit() # create empty dataframe to store swing phase data for all runs: all_swing_data = {} for i, file in enumerate(glob(os.path.join(path, "*.csv"))): print("Progress: ", i, "/", number_of_files) # print("file: ", file) data = pd.read_csv(file) # print(data) sensorfoot = data['sensorfoot'][1] data_forces_and_pos = data[[ 'force_x', 'force_y', 'force_z', 'FR_pos_x', 'FL_pos_x', 'HR_pos_x', 'HL_pos_x', 'force_timestamp', 'imu_linacc_y', 'imu_timestamp', 'FR_timestamp', 'FL_timestamp', 'HR_timestamp', 'HL_timestamp' ]] #print("data_forces_and_pos: ", data_forces_and_pos.head()) sample_spacing_foot = data_forces_and_pos['FR_timestamp'][ 5] - data_forces_and_pos['FR_timestamp'][4] sample_spacing_force = data_forces_and_pos['force_timestamp'][ 5] - data_forces_and_pos['force_timestamp'][4] # print("sample freq foot: ", sample_spacing_foot, # "\nsample freq force: ", sample_spacing_force) #x_forces = range(data_forces_and_pos['force_x'].count()) min_x_pos = range(100000) for foot in feet: x_pos = range(data_forces_and_pos[f'{foot}_pos_x'].count()) if len(x_pos) < len(min_x_pos): min_x_pos = x_pos run_number = file.rsplit(os.sep, 1)[1] run_number = run_number.split("_", 1)[0] # get velocity and step_frequency of current run df_current_run_summary = data_vel_and_freq.loc[ data_vel_and_freq['run'] == run_number] #print(df_current_run_summary) print("run number: ", run_number, "\nmin_x_pos: ", len(min_x_pos)) colours = [ '#e3433d', '#0b6ade', '#5bdea5', '#690612', '#150669', '#1c5239' ] sn.set_style('whitegrid') scalefactor = auxiliaryfunctions.define_scalefactor() FR_pos_x = data_forces_and_pos['FR_pos_x'] FR_pos_x = [y * scalefactor for y in FR_pos_x] FR_pos_x = FR_pos_x[0:len(min_x_pos)] FL_pos_x = data_forces_and_pos['FL_pos_x'] FL_pos_x = [y * scalefactor for y in FL_pos_x] FL_pos_x = FL_pos_x[0:len(min_x_pos)] HR_pos_x = data_forces_and_pos['HR_pos_x'] HR_pos_x = [y * scalefactor for y in HR_pos_x] HR_pos_x = HR_pos_x[0:len(min_x_pos)] HL_pos_x = data_forces_and_pos['HL_pos_x'] HL_pos_x = [y * scalefactor for y in HL_pos_x] HL_pos_x = HL_pos_x[0:len(min_x_pos)] shift_value = abs(np.mean(FR_pos_x[:10])) + abs( np.mean(HR_pos_x[:10])) HR_pos_x = [y + shift_value for y in HR_pos_x] HL_pos_x = [y + shift_value for y in HL_pos_x] imu_y = data_forces_and_pos['imu_linacc_y'] imu_y = [y + np.mean(FR_pos_x[:10]) for y in imu_y] #### SMOOTHING swing_phases_feet = {} for foot in feet: print("\n>>>>> FOOT: ", foot, "\n") # get sensorfoot: sensorfoot_of_run = auxiliaryfunctions.get_sensorfoot_for_run( run_number) print(run_number, sensorfoot_of_run) # smooth foot data if isinstance(foot, str) and foot == "FR": x_values, foot_smoothed = smooth_foot_position( FR_pos_x, data_forces_and_pos['FR_timestamp'] [:len(min_x_pos)], sample_spacing_foot, df_current_run_summary, filtertype=filtertype) elif isinstance(foot, str) and foot == "HR": x_values, foot_smoothed = smooth_foot_position( HR_pos_x, data_forces_and_pos['HR_timestamp'] [:len(min_x_pos)], sample_spacing_foot, df_current_run_summary, filtertype=filtertype) elif isinstance(foot, str) and foot == "FL": x_values, foot_smoothed = smooth_foot_position( FL_pos_x, data_forces_and_pos['FL_timestamp'] [:len(min_x_pos)], sample_spacing_foot, df_current_run_summary, filtertype=filtertype) elif isinstance(foot, str) and foot == "HL": x_values, foot_smoothed = smooth_foot_position( HL_pos_x, data_forces_and_pos['HL_timestamp'] [:len(min_x_pos)], sample_spacing_foot, df_current_run_summary, filtertype=filtertype) # find all maxima of smoothed foot: df_extrema, df_extrema_filtered, keepers2, three_max = auxiliaryfunctions.find_all_max_and_min_of_function( x_values, foot_smoothed, run_number) df_highest_max = df_extrema_filtered[ df_extrema_filtered['y'] == max( df_extrema_filtered['y'])] # if the current foot is the sensorfoot, store data for plotting later if foot == sensorfoot_of_run: df_extrema_filtered_sf = df_extrema_filtered df_highest_max_sf = df_highest_max foot_smoothed_sf = foot_smoothed # find all swing phases of smoothed foot: ## find the step intervals: swings = auxiliaryfunctions.find_step_intervals( df_extrema, df_extrema_filtered, foot_smoothed, keepers2, three_max) swing_phases_feet[foot] = swings # TESTPLOTS for individual feet: sn.lineplot(x_values, foot_smoothed, color='grey') sn.scatterplot(x='x', y='y', data=df_extrema, alpha=0.5) sn.scatterplot(x='x', y='y', data=df_extrema_filtered, color='red') sn.scatterplot( x='x', y='y', data=df_extrema_filtered[df_extrema_filtered['y'] == max(three_max)], color='green') # plot swing phases: for swing in swings: plt.vlines(x_values[swing[0]], ymin=min(foot_smoothed), ymax=max(foot_smoothed), linewidth=0.8) plt.vlines(x_values[swing[1]], ymin=min(foot_smoothed), ymax=max(foot_smoothed), linewidth=0.8) plt.title(run_number + "_" + foot) fig1 = plt.gcf() fig1.savefig(os.path.join(output_path2, f'{run_number}_{foot}.png'), dpi=300) plt.show() if foot == sensorfoot_of_run: # get the neutral z force: inflection_point, neutral_force_mean, neutral_force_std = get_neutral_force_z( foot_smoothed=foot_smoothed, x_values=x_values, data_forces_and_pos=data_forces_and_pos, run_number=run_number, df_extrema_filtered=df_extrema_filtered) # swing_phases_feet contains all the swing phases for each foot for the current run print("\n\nswing_phases_feet: \n", swing_phases_feet, '\n') # Plotting fig, ax = plt.subplots() #ax2 = ax.twiny() # plot imu data: sn.lineplot(x=data_forces_and_pos['imu_timestamp'], y=imu_y, color='black', alpha=0.6, linewidth=1, dashes=(5, 5), label='imu linear acc. y') # plot forces: sn.lineplot(x=data_forces_and_pos['force_timestamp'], y=data_forces_and_pos['force_x'], ax=ax, color=colours[0], label=f'{sensorfoot}_force_x') sn.lineplot(x=data_forces_and_pos['force_timestamp'], y=data_forces_and_pos['force_y'], ax=ax, color=colours[1], label=f'{sensorfoot}_force_y') sn.lineplot(x=data_forces_and_pos['force_timestamp'], y=data_forces_and_pos['force_z'], ax=ax, color=colours[2], label=f'{sensorfoot}_force_z') # plot foot position data: sn.lineplot( x=data_forces_and_pos['FR_timestamp'][:len(min_x_pos)], y=FR_pos_x, label='FR', dashes=[(2, 2)], linewidth=1) sn.lineplot( x=data_forces_and_pos['FL_timestamp'][:len(min_x_pos)], y=FL_pos_x, label='FL', dashes=[(2, 2)], linewidth=1) sn.lineplot( x=data_forces_and_pos['HL_timestamp'][:len(min_x_pos)], y=HL_pos_x, label='HL', dashes=[(2, 2)], linewidth=1) sn.lineplot( x=data_forces_and_pos['HR_timestamp'][:len(min_x_pos)], y=HR_pos_x, label='HR', dashes=[(2, 2)], linewidth=1) if smoothing == True and isinstance(sensorfoot_of_run, str): sn.lineplot(x=x_values, y=foot_smoothed_sf, label=f'{sensorfoot_of_run} pos {filtertype}', linewidth=1) sn.scatterplot(x='x', y='y', data=df_extrema_filtered_sf, color='red') sn.scatterplot(x='x', y='y', data=df_highest_max_sf, color='green') plt.scatter(inflection_point[0], inflection_point[1], marker='o', color='black') if max(data_forces_and_pos['force_z']) < max( foot_smoothed): plt.vlines(inflection_point[0], ymin=min(data_forces_and_pos['force_z']), ymax=max(foot_smoothed), linewidth=0.8) else: plt.vlines(inflection_point[0], ymin=min(foot_smoothed), ymax=max(data_forces_and_pos['force_z']), linewidth=0.8) # plot the std of the mean z force as shady area: # x = np.arange(min(data_forces_and_pos['force_timestamp']), max(data_forces_and_pos['force_timestamp']), 100000000.0) # plt.fill_between(x, neutral_force_mean-neutral_force_std, neutral_force_mean+neutral_force_std, alpha=0.3) # plot the mean of the z force as horizontal line plt.hlines( neutral_force_mean, xmin=min(data_forces_and_pos['force_timestamp']), xmax=max(data_forces_and_pos['force_timestamp']), linewidth=0.8) ax.set_xlabel("rosbag timesteps") #ax2.set_xlabel("position timesteps") plt.ylabel(f'{run_number}_forces') plt.legend(loc='best') plt.setp(ax.get_legend().get_texts(), fontsize='8') # for legend text fig1 = plt.gcf() fig1.savefig(os.path.join(output_path, f'{run_number}_force_pos_plot.png'), dpi=300) plt.show() ### assemble the swing phase data for current run #swing_data_run = {} list_of_rows = [] #print(data_forces_and_pos) for k, v in swing_phases_feet.items(): # Foot and the dict of swings print("k: ", k, "v: ", v) for i, swing in enumerate(v): print('swing: ', swing) # get the timestamps for the swing index: swing_timestamp = [ x_values[swing[0]], x_values[swing[1]] ] print("swing_timestamp: ", swing_timestamp) run = run_number foot = k sensorfoot = sensorfoot_of_run # find the clostest timestamp of the foot swing phase data to the force data: clostest_low = auxiliaryfunctions.find_closest_value( list(data_forces_and_pos['force_timestamp']), swing_timestamp[0]) clostest_high = auxiliaryfunctions.find_closest_value( list(data_forces_and_pos['force_timestamp']), swing_timestamp[1]) current_row_interval = [ data_forces_and_pos[ data_forces_and_pos['force_timestamp'] == clostest_low].index[0], data_forces_and_pos[ data_forces_and_pos['force_timestamp'] == clostest_high].index[0] ] print("current_row_interval: ", current_row_interval) force_max = max(data_forces_and_pos.loc[ current_row_interval[0]:current_row_interval[1], 'force_z']) force_min = min(data_forces_and_pos.loc[ current_row_interval[0]:current_row_interval[1], 'force_z']) force_mean = np.mean(data_forces_and_pos.loc[ current_row_interval[0]:current_row_interval[1], 'force_z']) neutral_force = neutral_force_mean gait = summary_data[summary_data['run'] == run]['footfallpattern'].values[0] velocity = summary_data[summary_data['run'] == run]['velocity'].values[0] step_frequency = summary_data[ summary_data['run'] == run]['step_frequency'].values[0] surface = summary_data[summary_data['run'] == run]['surface'].values[0] direction = summary_data[summary_data['run'] == run]['direction'].values[0] forcesbiased = summary_data[ summary_data['run'] == run]['forcesbiased'].values[0] list_of_rows.append([ run, foot, sensorfoot, force_max, force_min, force_mean, neutral_force, gait, velocity, step_frequency, surface, direction, forcesbiased ]) all_swing_data[run_number] = list_of_rows print("all swing data: \n", all_swing_data) columns = [ 'run', 'foot', 'sensorfoot', 'force_max', 'force_min', 'force_mean', 'neutral_force', 'gait', 'velocity', 'step_frequency', 'surface', 'direction', 'forcesbiased' ] df_all_swing_data = pd.DataFrame(columns=columns) # fill dataframe: for k, v in all_swing_data.items(): for list_row in v: df_all_swing_data.loc[len(df_all_swing_data)] = list_row print(df_all_swing_data) # save the dataframe: df_all_swing_data.to_csv(os.path.join( output_path, "magneto_swing_phase_data.csv"), index=True, header=True) else: print( "no assembled_csv folder. Run forceAnalysis.assemble() first.") return
def plot_entity(entity, ver_supp=None, number_of_all_tirps=None, number_of_patients=None): """ Plot entity's time intervals of events. Show vertical support if ver_supp is not None. :param entity: dict - key: state, value: list of time intervals of specific event :param ver_supp: optional parameter for vertical support :param number_of_all_tirps: integer number of all TIRPs above minimum support :param number_of_patients: integer number of all patients in entity list used for calculating showed TIRPs :return: None """ # make fullscreen window and clear figure mng = plt.get_current_fig_manager() mng.resize(*mng.window.maxsize()) plt.clf() colors = ['r', 'c', 'y', 'k', 'b', 'g', 'm'] labels = list(entity.keys()) padding = 0.25 min_num = np.inf max_num = -np.inf for i, label in enumerate(labels): for od, do in entity[label]: if od == do: # draw time event point plt.plot(od, i + 1, color=colors[i % len(colors)], marker='x', markersize=10) else: # draw time interval line plt.hlines(i + 1, od, do, colors=colors[i % len(colors)]) plt.vlines(od, 1 - padding, len(labels) + padding, colors='lightgray', linestyles='dotted') plt.vlines(do, 1 - padding, len(labels) + padding, colors='lightgray', linestyles='dotted') # set min and max number if od < min_num: min_num = od if do > max_num: max_num = do if None not in (ver_supp, number_of_all_tirps, number_of_patients): global tirp_indexxx plt.title('TIRP: %d / %d\n\nVertical support: %.2f (%d patients)' % (tirp_indexxx + 1, number_of_all_tirps, ver_supp, round(number_of_patients * ver_supp))) plt.yticks(np.arange(len(labels)) + 1, labels=labels) plt.xticks(np.arange(min_num - 2, max_num + 3)) plt.xlabel('time') plt.ylabel('state') plt.show()
title='Weight and Height in adults') x_train, x_test, y_train, y_test = train_test_split(df['Height'], df['Weight'], test_size=0.3) x_train = np.reshape(x_train, (-1,1)) x_test = np.reshape(x_test, (-1,1)) y_train = np.reshape(y_train, (-1,1)) y_test = np.reshape(y_test, (-1,1)) print x_train print('Train - Predictors shape', x_train.shape) print('Test - Predictors shape', x_test.shape) print('Train - Target shape', y_train.shape) print('Test - Target shape', y_test.shape) cls = linear_model.LinearRegression() #Fit method is used for fitting your training data into the model cls.fit(x_train,y_train) prediction = cls.predict(x_test) print('Co-efficient of linear regression',cls.coef_) print('Intercept of linear regression model',cls.intercept_) print('Mean Square Error', metrics.mean_squared_error(y_test, prediction)) print('Model R^2 Square value', metrics.r2_score(y_test, prediction)) plt.scatter(x_test, y_test) plt.plot(x_test, prediction, color='red', linewidth=3) plt.xlabel('Hours') plt.ylabel('Marks') plt.title('Linear Regression') #residual Plot plt.scatter(cls.predict(x_test), cls.predict(x_test) - y_test, c='g', s = 40) plt.hlines(y=0, xmin=0, xmax=100) plt.title('Residual plot') plt.ylabel('Residual')
plt.vlines(a[:, 0], a[:, 1], a[:, 2], color='black', lw=1.0, linestyles='--', label='Intervals(Basic)') plt.vlines(b[:, 0], b[:, 1], b[:, 2], color='#FF8080', lw=1.0, linestyles='--', label='Intervals(Antithetic)') plt.hlines(-660.801, -1000, 101000, lw=0.5, color='blue', label='L3 = -660.801') #plt.plot(a[:,0],a[:,1],color = '#FF8080',lw = 0.8,label = 'Bounds(Basic)' ) #plt.plot(a[:,0],a[:,2],color='#FF8080',lw = 0.8 ) plt.plot(b[:, 0], b[:, 1], color='#FF8080', lw=0.8, label='Bounds(Antithetic) ') plt.plot(b[:, 0], b[:, 2], color='#FF8080', lw=0.8) plt.legend(prop={'size': 15}) plt.show()
]) #print(np.sum(-path_cmaf[idc,:] + path_db[idc,:])) #print(np.sum((walk_db(n(coefssl[1:][idc,:]),.05)**2)) - np.sum((walk_db(n(coefss4[1:][idc,:]),.05)**2))) fig = plt.figure(figsize=(5, 5)) xxx = np.arange(0, 120) * 0.05 plt.plot(xxx, path_db[idc, :], c="blue", lw=3.2, label="DBA-ATT") plt.plot(xxx, path_l[idc, :], c="green", lw=3.2, label="LIME-ATT") plt.plot(xxx, path_cmaf[idc, :], c="red", lw=3.2, label="CEM-MAF") #plt.plot(path_gs[i,:], c = "magenta", lw = 3.2, label = "GS") plt.hlines(.5, -.1, 4, linestyles="dashed", label="Decision \nboundary", colors="red") plt.title("Latent Space Evaluation", size=20) plt.yticks(size=20) plt.xticks(size=20) plt.legend(loc='upper center', bbox_to_anchor=(.9, 1.01), shadow=True, ncol=1, fontsize=17) #plt.xlabel(r"Distance from $\boldsymbol{x}_0$", size = 20,labelpad = 10) #plt.ylabel("CNN Response", size = 20, labelpad = 10) #fig.savefig("latent_path_comparison_161_final.pdf", bbox_inches = "tight") plt.xlim(0, 4)
def medicion_ritmo_cardiaco(): # Comencemos con nuestro primer ejemplo # Señal capturada con un pulsómetro con la técnica # de fotoplestimografía (PPG). data, timer = hp.load_exampledata(0) sample_rate = 100 plt.figure() plt.plot(data) plt.title("Ritmo cardíaco: Ejemplo 1") plt.legend(['ritmo cardíaco']) plt.ylabel('pulsos') plt.xlabel('muestras') plt.show() # Utilizaremos la libreria scipy para detectar picos # https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html sample_rate = 100 peaks, _ = find_peaks(data, distance=50) # Plotear los picos plt.figure() plt.plot(data) plt.plot(peaks, data[peaks], "x") plt.title("Ritmo cardíaco: Ejemplo 1") plt.legend(['ritmo cardíaco', 'pulsos']) plt.ylabel('pulsos') plt.xlabel('muestras') mplcursors.cursor(multiple=True) plt.show() # Calcular el ritmo cardíaco como la diferencia # de tiempo entre los picos delta_time = np.diff(peaks) / 100 promedio = np.mean(delta_time) pulsaciones_por_minuto = promedio * 60 print("Pulsaciones", pulsaciones_por_minuto) # Ahora veamos un ejemplo más real de uso, en donde se puede # ver el ruido introducido por la colocación del instrumento # y los movimientos del paciente. data, timer = hp.load_exampledata(1) sample_rate = hp.get_samplerate_mstimer(timer) plt.figure() peaks, _ = find_peaks(data, distance=sample_rate / 2.0) plt.plot(data) plt.plot(peaks, data[peaks], "x") plt.title("Ritmo cardíaco: Ejemplo 2") plt.legend(['ritmo cardíaco', 'pulsos']) plt.ylabel('pulsos') plt.xlabel('muestras') mplcursors.cursor(multiple=True) plt.show() delta_time = np.diff(peaks) / 100 promedio = np.mean(delta_time) pulsaciones_por_minuto = promedio * 60 print("Pulsaciones", pulsaciones_por_minuto) # Buscar picos que prevalezcan superiores a su entorno por más del tiempo # definido (medio ciclo de la señal) --> "prominencia" peaks, properties = find_peaks(data, prominence=1, width=sample_rate / 2.0) plt.figure() plt.plot(data) plt.plot(peaks, data[peaks], "x") plt.vlines(x=peaks, ymin=data[peaks] - properties["prominences"], ymax=data[peaks], color="C1") plt.hlines(y=properties["width_heights"], xmin=properties["left_ips"], xmax=properties["right_ips"], color="C1") mplcursors.cursor(multiple=True) plt.show() # Veamos ahora el potencial de heartpy # Ejemplo 1 data, timer = hp.load_exampledata(0) sample_rate = 100 wd, m = hp.process(data, sample_rate=sample_rate) hp.plotter(wd, m) print("Pulsaciones", m['bpm']) # Ejemplo 2 data, timer = hp.load_exampledata(1) sample_rate = hp.get_samplerate_mstimer(timer) wd, m = hp.process(data, sample_rate=sample_rate) hp.plotter(wd, m) print("Pulsaciones", m['bpm'])
else: temp.append(center_x[i]) sameLevel_x.append(temp) mid_x.append(int((min(temp) + max(temp)) / 2)) temp = [] if abs(center_y[i + 1] - center_y[i]) <= 20: temp2.append(center_y[i]) else: temp2.append(center_y[i]) sameLevel_y.append(temp2) mid_y.append(int((min(temp2) + max(temp2)) / 2)) temp2 = [] for i in range(len(mid_y)): plt.hlines(mid_y[i] - 20, 0, 2592, colors='lightblue', linewidth=1) hline.append(mid_y[i] - 20) if i == len(mid_y) - 1: plt.hlines(mid_y[i] + 20, 0, 2592, colors='lightblue', linewidth=1) hline.append(mid_y[i] + 20) hline.sort() braille = [] count = 0 mid_x.append(0) mid_x.append(0) for i in range(len(mid_x) - 2):
nsamples = T * fs t = np.linspace(0, T, nsamples, endpoint=False) a = 0.02 f0 = 600.0 x = 0.1 * np.sin(2 * np.pi * 1.2 * np.sqrt(t)) x += 0.01 * np.cos(2 * np.pi * 312 * t + 0.1) x += a * np.cos(2 * np.pi * f0 * t + .11) x += 0.03 * np.cos(2 * np.pi * 2000 * t) plt.figure(2) plt.clf() plt.plot(t, x, label='Noisy signal') y = butter_bandpass_filter(x, lowcut, highcut, fs, order=6) plt.plot(t, y, label='Filtered signal (%g Hz)' % f0) plt.xlabel('time (seconds)') plt.hlines([-a, a], 0, T, linestyles='--') plt.grid(True) plt.axis('tight') plt.legend(loc='upper left') plt.savefig('demo2.png', bbox_inches='tight') # # Plot Noise Removal # x = my_data[:, 2] # y = butter_bandpass_filter(x, lowcut, highcut, fs, order=6) # plt.figure(2) # plt.clf() # plt.plot(t, x, label='Noisy signal') # plt.plot(t, y, label='Filtered signal (%g Hz)' % 100) # plt.xlabel('time (seconds)') # plt.grid(True)
smax = 10 S = np.exp(np.linspace(np.log(smin), np.log(smax), 1000)) x = 2.0 / np.pi * np.arcsin(S / (S + N)) sigma_x = np.sqrt(1 - x**2) sigma_rho = np.pi / 2 * np.cos(np.pi / 2 * x) * sigma_x sigma_s = (S + N)**2 / N * sigma_rho ref = S / (S + N) corr_eff = (S + N) / sigma_s plt.clf() plt.figure(figsize=(10, 5)) plt.plot(S, corr_eff, 'k-') plt.hlines([0.64, 0.32], 0, 10, '#888888', linestyles='dashed') plt.text(8, 0.68, '$\epsilon = 0.64$', color='#888888') plt.text(8, 0.34, '$\epsilon = 0.32$', color='#888888') plt.xlim(0, 10) plt.ylim(0, 1) plt.xlabel('Signal-to-noise ratio') plt.ylabel('Correlator efficiency ($\epsilon$)') #plt.loglog(S,corr_eff) imin = (2 * len(S)) // 3 pp = np.polyfit(np.log(S[imin:]), np.log(corr_eff[imin:]), 1) print(pp) #pred=1.0/(S)**(1/4) #pred=(S**pp[0])*np.exp(pp[1])
color='black', lw=2) # total DOS plt.xlim(0, 3) # Range of intensity of DOS plt.yticks([-1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5]) # Range of energy plt.xticks([]) plt.subplots_adjust(wspace=0) plt.show(fig) else: # Plot spin up channel figup = plt.figure() plt.hlines(0, 0, 5, colors='b', linestyles='dashed') for loop1 in range(0, PlaneNum): plt.hlines(0, 0, 5, colors='b', linestyles='dashed') plt.subplot(1, PlaneNum, loop1 + 1) plt.xlim(0, 8) # Range of intensity of DOS plt.ylim(-3, 3) if loop1 == 0: plt.yticks( [-3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3]) else: plt.yticks([]) plt.xticks([]) for loop2 in range(0, orbnum): if orbital[loop2] == 1: plt.plot(data[loop1][energylow:energyhigh, orbital[loop2]],
# fitsファイルのheaderと銀河のra, decから, # 画像内の座標を得る wcs = astropy.wcs.WCS(header) px, py = wcs.wcs_world2pix(ra_deg, dec_deg, 0) px = int(px) py = int(py) # 得られた座標を中心として画像を切り出し、保存する r = 50 img_galaxy = data[py - r:py + r, px - r:px + r] plt.title('RA = {}, Dec = {}'.format(ra_hms, dec_hms)) plt.imshow(np.log10(img_galaxy.T[::-1, ::-1]), cmap='gray', vmin=3.05, vmax=3.09) # 画像上で5秒角に対応する長さの線を描画する cd1_2 = header['CD1_2'] pix_corresponding_to_5s = round(DEG_CORRESPONDING_TO_5S / cd1_2) print(pix_corresponding_to_5s) plt.hlines(y=17, xmin=10, xmax=10 + pix_corresponding_to_5s) plt.savefig('data/img/' + str(objid), bbox_inches='tight') plt.close() # 画像とGalaxy Zooのデータをnpzで保存する np.savez('data/npz/' + str(objid), img_galaxy, galaxy_info) # img = Image.fromarray(np.uint8(fits[0].data[py-r:py+r, px-r:px+r])) # img.save(str(objid) + '.png')
#linear regression model lm = linear_model.LinearRegression() #train the model model = lm.fit(X_train, y_train) pred = lm.predict(X_test) #intercept lm.intercept_ #coefficients lm.coef_ #accuracy score print ("Score:", model.score(X_test, y_test)) #Scatterplot for true and predicted values plt.scatter(y_test, pred) plt.xlabel("True Values") plt.ylabel("Predictions") #Residual errors in training data plt.scatter(model.predict(X_train), model.predict(X_train) - y_train, color = "green", s = 10, label = 'Train data') #Residual errors in test data plt.scatter(model.predict(X_test), model.predict(X_test) - y_test, color = "blue", s = 10, label = 'Test data') #Line for zero residual error plt.hlines(y = 0, xmin = 1.4, xmax = 2.2, linewidth = 0.1) #Legend plt.legend(loc = 'upper right') #Title plt.title("Residual errors") #Show plot plt.show()