def plot_super(map_obstacles, laser_obstacles, title): f = plt.figure() sp = f.add_subplot(111) sp.plot(map_obstacles[:, 0], map_obstacles[:, 1], '.') sp.plot(laser_obstacles[:, 0], laser_obstacles[:, 1], '.r') sp.set_title(title) plt.draw()
def show(self, num, sp): """ Display RadioNode position in the 2D strucure """ x = self.position[0, num] y = self.position[1, num] sp.plot(x, y, 'ob')
def PlotFFTInput(self): """Plot Time Domain of FFT Input Slice for Last Measurement""" fig = self.CreateFigure("FFT Input") sp = fig.add_subplot(111) xaxis = range(0,self.fftn) sp.plot(xaxis,[x.real for x in self.fftia],'.-',color='b',label='I') sp.plot(xaxis,[x.imag for x in self.fftia],'.-',color='r',label='Q') sp.set_ylabel("Magnitude") sp.set_xlabel("Sample") #sp.legend(bbox_to_anchor=(1,-0.1)) sp.legend(loc=2,bbox_to_anchor=(0,-0.1),ncol=4) plt.show()
def PlotFD(self,dbfs=True): """Plot Frequency Domain for Last Measurement""" freqspectrum = np.abs(self.fftoa) freqspectrum = np.concatenate( [freqspectrum[self.fftn/2:self.fftn],freqspectrum[0:self.fftn/2]] ) if dbfs: zerodb = 20*np.log10(self.fftn/2) freqspectrum = (20*np.log10(abs(freqspectrum))) - zerodb fig = self.CreateFigure("Frequency Domain") sp = fig.add_subplot(111) xaxis = np.r_[0:self.fftn] * (self.Sr/self.fftn) xaxis = np.concatenate( [(xaxis[self.fftn/2:self.fftn] - self.Sr),xaxis[0:self.fftn/2]]) sp.plot(xaxis,freqspectrum,'.-',color='b',label='Spectrum') sp.set_ylabel("dBFS") sp.set_xlabel("Frequency") sp.legend(loc=2,bbox_to_anchor=(0,-0.1),ncol=4) plt.show()
def PlotTD(self): """Plot Time Domain of Jack Input and Output Arrays for Last Measurement""" fig = self.CreateFigure("Time Domain") sp = fig.add_subplot(111) xaxis = range(0,len(self.iIa)) sp.plot(xaxis,self.iIa,'.-',color='b',label='iI') ## 180 phase shift as complex portion is created with -1j sp.plot(xaxis,-1*self.iQa,'.-',color='r',label='iQ') sp.plot(xaxis,self.oIa,'.-',color='c',label='oI') sp.plot(xaxis,self.oQa,'.-',color='m',label='oQ') ## Identify RTFrames maxy = self.oIa.max() sp.plot([self.rtframes,self.rtframes],[-maxy,maxy],'k-',lw=3,label='RT Frames') ## Identify Sync Index sp.plot([self.synci+self.sync2fft,self.synci+self.sync2fft],[-maxy,maxy],'g-',lw=3,label='FFT Start') sp.plot([self.synci+self.sync2fft+self.fftn,self.synci+self.sync2fft+self.fftn],[-maxy,maxy],'y-',lw=3,label='FFT End') sp.set_ylabel("Magnitude") sp.set_xlabel("Sample") #sp.legend(bbox_to_anchor=(1,-0.1)) sp.legend(loc=2,bbox_to_anchor=(0,-0.1),ncol=7) plt.show()
def plot_reg_bayes(df, xy, traces_ind, traces_hier, feat='no_feat', burn_ind=2000, burn_hier=None, quad=False, clr_school=True): """ create plot for bayesian derived regression lines, no groups """ keys = traces_ind.keys() fig, axes1d = plt.subplots(nrows=1, ncols=len(keys), sharex=True, sharey=True, figsize=(8*len(keys),8)) fig.suptitle('Bayesian hierarchical regression of pre-test vs post-test scores') cm_cmap = cm.get_cmap('Set2') clrs = {} clrs['ind'] = ['#00F5FF','#006266','#00585C'] clrs['hier'] = ['#FF7538','#661f00','#572610'] point_clrs = 'cm_cmap(grp.clr)' if clr_school else 'cm_cmap(j/len(keys))' if len(keys) == 1: axes1d = [axes1d] for j, (sp, key) in enumerate(zip(axes1d,keys)): # scatterplot datapoints and subplot count title if feat == 'no_feat': x = df[xy['x']] y = df[xy['y']] for grpkey, grp in df.groupby('schoolid'): sp.scatter(grp[xy['x']],grp[xy['y']],s=40,color=eval(point_clrs),label='{} ({})'.format(grpkey,len(grp)) ,alpha=0.7,edgecolor='#333333') if feat != 'no_feat': x = df.loc[df[feat] == key,xy['x']] y = df.loc[df[feat] == key,xy['y']] for grpkey, grp in df.loc[df[feat] == key].groupby('schoolid'): sp.scatter(grp[xy['x']],grp[xy['y']],s=40,color=eval(point_clrs),label='{} ({})'.format(grpkey,len(grp)) ,alpha=0.7,edgecolor='#333333') sp.annotate('{} ({} samples)'.format(key,len(x)) ,xy=(0.5,1),xycoords='axes fraction',size=14,ha='center' ,xytext=(0,6),textcoords='offset points') if clr_school: sp.legend(scatterpoints=1, loc=8, ncol=1, bbox_to_anchor=(1.0, 0.35), fancybox=True, shadow=True) # setup xlims and plot 1:1 line # BODGED the xlims xfit = np.linspace(x.min(), x.max(), 10) sp.plot(np.array([65,135]),np.array([65,135]),linestyle='dotted',linewidth=0.5,color='#666666') # plot actual data mean sp.scatter(x.mean(),y.mean(),marker='+',s=500,color='#551A8B') # plot regression: individual alpha = traces_ind[key]['alpha'][burn_ind:] beta = traces_ind[key]['beta'][burn_ind:] yfit = alpha[:, None] + beta[:, None] * xfit # <- yfit for all samples at x in xfit ind yfit_at_xmean = alpha[:, None] + beta[:, None] * x.mean() note = '{}\nslope: {:.2f}\nincpt: {:.2f}\nmeanx: {:.2f}\nin@mx: {:.2f}'.format('individual' ,beta.mean(), alpha.mean(), x.mean(), yfit_at_xmean.mean()-x.mean()) if quad: gamma = traces_ind[key]['gamma'][burn_ind:] yfit = alpha[:, None] + beta[:, None] * xfit + gamma[:, None] * xfit ** 2 yfit_at_xmean = alpha[:, None] + beta[:, None] * x.mean() + gamma[:, None] * x.mean() ** 2 note = '{}\ny={:.2f} + {:.2f}x + {:.3f}x^2\nmeanx: {:.2f}\nin@mx: {:.2f}'.format('individual' ,alpha.mean(),beta.mean(),gamma.mean(),x.mean(), yfit_at_xmean.mean()-x.mean()) mu = yfit.mean(0) yerr_975 = np.percentile(yfit,97.5,axis=0) yerr_025 = np.percentile(yfit,2.5,axis=0) sp.plot(xfit, mu,linewidth=3, color=clrs['ind'][0], alpha=0.8) sp.fill_between(xfit, yerr_025, yerr_975, color=clrs['ind'][2],alpha=0.3) sp.annotate(note,xy=(1,0),xycoords='axes fraction',xytext=(-12,6),textcoords='offset points' ,color=clrs['ind'][1],weight='bold',size=12,ha='right',va='bottom') # plot regression: hierarchical if traces_hier is not None: alpha = traces_hier['alpha'][burn_hier:,j] beta = traces_hier['beta'][burn_hier:,j] yfit = alpha[:, None] + beta[:, None] * xfit yfit_at_xmean = alpha[:, None] + beta[:, None] * x.mean() note = '{}\nslope: {:.2f}\nincpt: {:.2f}\nmeanx: {:.2f}\nin@mx: {:.2f}'.format('hierarchical' ,beta.mean(),alpha.mean(),x.mean(),yfit_at_xmean.mean()-x.mean()) # if quad: # gamma = traces_hier['gamma'][burn_hier:,j] # yfit = alpha[:, None] + beta[:, None] * xfit + gamma[:, None] * xfit ** 2 # note = '{}\ny={:.2f} + {:.2f}x + {:.2f}x^2'.format('individual',alpha.mean(),beta.mean(),gamma.mean()) mu = yfit.mean(0) yerr_975 = np.percentile(yfit,97.5,axis=0) yerr_025 = np.percentile(yfit,2.5,axis=0) sp.plot(xfit, mu,linewidth=3, color=clrs['hier'][0], alpha=0.8) sp.fill_between(xfit, yerr_025, yerr_975, color=clrs['hier'][2], alpha=0.3) sp.annotate(note, xy=(0,1),xycoords='axes fraction',xytext=(100,-6),textcoords='offset points' ,color=clrs['hier'][1],weight='bold',size=12,ha='right',va='top') plt.show()
cm_cmap = cm.get_cmap('hsv') fig, axes1d = plt.subplots(nrows=1, ncols=1, sharex=True, figsize=(8,8)) fig.subplots_adjust(wspace=0.2) fig.suptitle('Correlation of pre-test and post-test scores') sp = axes1d clr = cm_cmap(0.6) sp.scatter(x=x,y=y,color=clr,alpha=0.6,edgecolor='#999999') # fit and plot a new linear model regr = linear_model.LinearRegression() regr.fit(pd.DataFrame(x),pd.DataFrame(y)) x_prime = np.linspace(x.min(),x.max(),num=10)[:,np.newaxis] y_hat = regr.predict(x_prime) sp.plot(x_prime,y_hat,linewidth=2,linestyle='dashed',color='green',alpha=0.8) ss_res = regr.residues_[0] ss_tot = np.sum((y - np.mean(y))**2) rsq = 1 - (ss_res/ss_tot) sp.annotate('R^2: {:.2f}\nCoef: {:.2f}\nIntr: {:.2f}'.format(rsq,regr.coef_[0][0],regr.intercept_[0]) ,xy=(1,0),xycoords='axes fraction',xytext=(-12,6),textcoords='offset points' ,color='green',weight='bold',size=12,ha='right',va='bottom') sp.set_ylabel('Post-test Score') sp.set_xlabel('Pre-test Score') sp.axes.grid(True,linestyle='-',color='lightgrey') plt.subplots_adjust(top=0.95) plt.show()
def run(self, n_simulations=1, repeat=[], plot=False): total_sims = len(self.simulations_raw) if type(repeat) is int: repeat = list(range(repeat)) print('Simulations: n =', n_simulations, ', repeat =', repeat) for n_sim in tqdm(list(range(total_sims, total_sims + n_simulations)) + repeat, total=n_simulations + len(repeat)): if n_sim not in self.simulations_raw.index: print('\n' * 2 + '*' * 70 + '\n' + '*' * 70 + '\nSimulation #' + str(n_sim)) obs_j, x_obs, y_obs, valid_j, x_valid, y_valid, test_j, x_test, y_test = self.new_data( plot=plot) self.add_simulation(n_sim, obs_j, valid_j, test_j) print('*' * 70) else: obs_j, valid_j, test_j = self.simulations_raw.loc[n_sim][ 'obs'], self.simulations_raw.loc[n_sim][ 'valid'], self.simulations_raw.loc[n_sim]['test'] x_obs, y_obs, x_test, y_test = self.data_x[obs_j], self.data_y[ obs_j], self.data_x[test_j], self.data_y[test_j] if valid_j is not None: x_valid, y_valid, = self.data_x[valid_j], self.data_y[ valid_j] else: x_valid, y_valid = None, None print('\n' * 2 + '*' * 60 + '\n' + '*' * 60 + '\nRepetition #' + str(n_sim) + '\n' + '*' * 60) for sp in tqdm(self.models, total=len(self.models)): print('\n' * 2 + '*' * 50 + '\n' + sp.name + ' #' + str(n_sim) + '\n' + '*' * 50) sp.observed(x_obs, y_obs) if plot: print('\n' + sp.name) tictoc = time.time() selected, start, params = self.select_model( sp, x_valid, y_valid) time_params, tictoc = time.time() - tictoc, time.time() if plot: sp.plot(params) sp.plot_model(params) sp.set_params(params) sp.set_space(x_obs, y_obs, obs_j) scores_obs = self.calc_scores(sp, params) time_scores_obs, tictoc = time.time() - tictoc, time.time() if valid_j is not None: sp.set_space(x_valid, y_valid, valid_j) scores_valid = self.calc_scores(sp, params) else: scores_valid = {} time_scores_valid, tictoc = time.time() - tictoc, time.time() if x_valid is not None: sp.observed(np.concatenate([x_obs, x_valid]), np.concatenate([y_obs, y_valid])) else: sp.observed(x_obs, y_obs) sp.set_space(x_test, y_test, test_j) scores_test = self.calc_scores(sp, params) time_scores_test, tictoc = time.time() - tictoc, time.time() if plot: print(scores_test, time_params, time_scores_obs, time_scores_test) self.add_result(n_sim, sp.name, selected, start, params, scores_obs, scores_valid, scores_test, time_params, time_scores_obs, time_scores_valid, time_scores_test)