def GenerateWpaGraph(GamePck): """Returns a list of the homeTeam wPA and generates the Win probability graph of a specified game """ game = get('game_winProbability', {'ver': 'v1', 'gamePk': GamePck}) atbat = [0] home = 50 homeL = [50] for item in game: for key, val in item.items(): if key == 'atBatIndex': atbat.append(val) if key == 'homeTeamWinProbabilityAdded': home += val homeL.append(home) c = ['g' if a >= 50 else 'r' for a in homeL] lines = [ ((x0, y0), (x1, y1)) for x0, y0, x1, y1 in zip(atbat[:], homeL[:], atbat[1:], homeL[1:]) ] coloredLines = LineCollection(lines, colors=c, linewidths=(2, )) fig, ax = plt.subplots(1) ax.add_collection(coloredLines) ax.autoscale_view() plt.show() return homeL
def plot_rv(mod): lvs = mod.right_eigenvectors_ nstates = min(5, lvs.shape[1]) fig, axes = plt.subplots(nrows=nstates, sharey=False, sharex=True) for idx, ax in enumerate(axes): ax.bar(range(lvs.shape[0]), lvs[:, idx]) plt.savefig('msm-rvs.png')
def plotMyMetric(allmetrics, acts, actmap={}): acount = len(acts) col = min(4, acount) row = int(np.ceil((acount) / float(col))) import result_analyse.SpiderChart # result_analyse.SpiderChart.radar_factory(5, frame='polygon') m_fig, m_ax = plt.subplots(row, col, figsize=(col * 3, row * 3), subplot_kw=dict(projection='radar')) if type(m_ax) != np.ndarray: m_ax = np.array([m_ax]) m_ax = m_ax.flatten() for i, act in enumerate(acts): metrics = allmetrics[act] if ('avg' in metrics): metrics = metrics['avg'] #plotJoinAct(dataset,real_events,pred_events,onlyAct=i) df = pd.DataFrame(metrics) name = actmap[act] if act in actmap else act result_analyse.SpiderChart.radar_factory(len(metrics.columns), frame='polygon') print(name, "========") print(df.round(2)) print('average=', np.average(list(df.loc['f1']))) df = df.drop(['tp', 'fp', 'fn']) spiderchart.plot(df, [0.25, .5, .75], title=name, ax=m_ax[i]) m_fig.tight_layout(pad=0, h_pad=-10.0, w_pad=3.0)
def plot_events(real,pred,meta,real_,pred_, label=None): from matplotlib.pylab import plt import random fig,ax = plt.subplots(figsize=(15, .8)) ax.set_title(label) plt.xlim(0,max(meta[1],10)) ax.set_xticks(np.arange(0,max(real[-1][1],10),.1),minor=True) maxsize=20 # random.random()/4 for i in range(min(maxsize,len(pred_))): d = pred_[i] plt.axvspan(d[0], d[1], 0, 0.6,linewidth=0,edgecolor='k',facecolor='#edb4b4', alpha=.6) plt.text((d[1] + d[0]) / 2, 0.2,f'{i}' , horizontalalignment='center', verticalalignment='center') for i in range(min(maxsize,len(pred))): d = pred[i] plt.axvspan(d[0], d[1], 0.0, 0.6,linewidth=0,edgecolor='k',facecolor='#a31f1f', alpha=.6) plt.text((d[1] + d[0]) / 2, 0.2,f'{i}' , horizontalalignment='center', verticalalignment='center') # maxsize=len(real) for i in range(min(maxsize,len(real_))): gt = real_[i] plt.axvspan(gt[0], gt[1], 0.4, 1,linewidth=0,edgecolor='k',facecolor='#d2f57a', alpha=.6) plt.text((gt[1] + gt[0]) / 2, 0.8,f'{i}' , horizontalalignment='center', verticalalignment='center') for i in range(min(maxsize,len(real))): gt = real[i] plt.axvspan(gt[0], gt[1], 0.4, 1,linewidth=0,edgecolor='k',facecolor='#1fa331', alpha=.6) plt.text((gt[1] + gt[0]) / 2, 0.8,f'{i}' , horizontalalignment='center', verticalalignment='center') # plt.grid(True) plt.minorticks_on() ax.set(yticks=[.25,.75], yticklabels=['P','R']) # plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) plt.show()
def plot_data(): fig, axs = plt.subplots(2) fig.tight_layout(pad=3.0) for key in sorted(data_dict.keys()): X_data = data_dict[key][0] Y_data = data_dict[key][1] axs[0].plot(X_data, Y_data) axs[0].set_xlabel('X-coordinate') axs[0].set_xlim(0, 250) axs[0].set_ylabel('Y-coordinate') axs[0].set_ylim(250, 0) axs[0].set_title('Mitochondrial Tracking') for key in sorted(data_dict.keys()): X_data = data_dict[key][0] Y_data = data_dict[key][1] if (determine_direction(Y_data[0], Y_data[-1])): axs[1].plot(X_data, Y_data, 'b') print('Blue ', key) else: axs[1].plot(X_data, Y_data, 'y') print('Gold', key) axs[1].set_xlabel('X-coordinate') axs[1].set_xlim(0, 250) axs[1].set_ylabel('Y-coordinate') axs[1].set_ylim( 250, 0 ) #Inverts the y-axis to match the numbering system in Fiji Manual Tracking axs[1].set_title('Mitochondrial Directionality') plt.show()
def run(plotIt=True, nx=5, ny=5): """ Utils: surface2ind_topo ======================= Here we show how to use :code:`Utils.surface2ind_topo` to identify cells below a topographic surface. """ mesh = Mesh.TensorMesh([nx,ny], x0='CC') # 2D mesh xtopo = np.linspace(mesh.gridN[:,0].min(), mesh.gridN[:,0].max()) topo = 0.4*np.sin(xtopo*5) # define a topographic surface Topo = np.hstack([Utils.mkvc(xtopo,2), Utils.mkvc(topo,2)]) #make it an array indcc = surface2ind_topo(mesh, Topo, 'CC') if plotIt: from matplotlib.pylab import plt from scipy.interpolate import interp1d fig, ax = plt.subplots(1,1, figsize=(6,6)) mesh.plotGrid(ax=ax, nodes=True, centers=True) ax.plot(xtopo,topo,'k',linewidth=1) ax.plot(mesh.vectorCCx, interp1d(xtopo,topo)(mesh.vectorCCx),'--k',linewidth=3) aveN2CC = Utils.sdiag(mesh.aveN2CC.T.sum(1))*mesh.aveN2CC.T a = aveN2CC * indcc a[a > 0] = 1. a[a < 0.25] = np.nan a = a.reshape(mesh.vnN, order='F') masked_array = np.ma.array(a, mask=np.isnan(a)) ax.pcolor(mesh.vectorNx,mesh.vectorNy,masked_array.T, cmap=plt.cm.gray, alpha=0.2) plt.show()
def plotJoinMetric(joinmetrics, acts, actmap={}): acount = len(acts) col = min(4, acount) row = int(np.ceil((acount) / float(col))) import result_analyse.SpiderChart result_analyse.SpiderChart.radar_factory(5, frame='polygon') m_fig, m_ax = plt.subplots(row, col, figsize=(col * 3, row * 3), subplot_kw=dict(projection='radar')) if type(m_ax) != np.ndarray: m_ax = np.array([m_ax]) m_ax = m_ax.flatten() for i, act in enumerate(acts): try: all = None name = actmap[act] if act in actmap else act print(name, "========") # actres[act]={(m,e):joinmetrics[m][act]['avg'][e] for m in joinmetrics for e in joinmetrics[m][act]['avg']} # import pandas as pd # from IPython.display import display, HTML # if(len(actres[act])==0): # print('No Eval') # else: # df2=pd.DataFrame(actres[act]).round(2) # display(HTML(df2.to_html())) for item in joinmetrics: metrics = joinmetrics[item][act] if ('avg' in metrics): metrics = metrics['avg'] #plotJoinAct(dataset,real_events,pred_events,onlyAct=i) df = pd.DataFrame(metrics) print(f'df={df}') if not ('f1' in df.index): print('f1 is not exist') continue print('average=', np.average(list(df.loc['f1']))) # df=df.drop(['tp','fp','fn','recall','precision']) # df=df.loc[['f1']] calcf1 = 2 * df.loc['recall'] * df.loc['precision'] / ( df.loc['recall'] + df.loc['precision']) df.loc['f1'] = calcf1 df = df.loc[['f1']] if all is None: all = df.drop(['f1']) all.loc[item] = df.loc['f1'] if not (all is None): #print(f'all={all} title={name}, ax={m_ax[i]}') spiderchart.plot(all, [0.25, .5, .75], title=name, ax=m_ax[i]) except Exception as e: console.log(e) traceback.print_exception(*sys.exc_info()) m_fig.tight_layout(pad=0, h_pad=-10.0, w_pad=3.0)
def figure(df): fig, ax = plt.subplots(figsize=(12, 4)) df.plot(ax=ax) return Export.figure("Figure", image=fig, data=df, caption="A figure caption.")
def plot_ppc_and_score(trace, data, ax=None, title='PPC', paras=None): # Sample PPC ppc_trace = pm.sample_posterior_predictive(trace=trace, var_names=['y']) # Calculate LOO score loo = az.loo(trace).loo loo_text = "LOO = %.2f"%loo # Aggregate binary responses new_trace = [] for soa in sorted(set((data.SOA_IN_FRAMES))): new_trace.append(ppc_trace['y'][:,(data.SOA_IN_FRAMES==soa) & (data.PROBE_SALIENT==0)].mean(axis=1)) new_trace.append(ppc_trace['y'][:,(data.SOA_IN_FRAMES==soa) & (data.PROBE_SALIENT==1)].mean(axis=1)) ppc_trace = {'y': np.array(new_trace).T} # Prepare axes if none provided if ax is None: f,ax= plt.subplots() # Get SOAs and condition mask from data SOAs = sorted(set(data['SOA_IN_MS'])) cond = data.groupby(['SOA_IN_MS', 'PROBE_SALIENT'])['PROBE_SALIENT'].min().values # Plot az.plot_hdi(y=ppc_trace['y'][:,cond==0],x=SOAs, color='k', ax=ax, hdi_prob=0.95, fill_kwargs={'alpha' : 0.23}) az.plot_hdi(y=ppc_trace['y'][:,cond==1],x=SOAs, color='g', ax=ax, hdi_prob=0.95, fill_kwargs={'alpha' : 0.23}) ax.plot(SOAs, np.mean(ppc_trace['y'][:,cond==0],axis=0), color='k') ax.plot(SOAs, np.mean(ppc_trace['y'][:,cond==1],axis=0), color='g') pf_mean = data.groupby(['SOA_IN_MS', 'PROBE_SALIENT']).mean().PROBE_FIRST_RESPONSE pf_count = data.groupby(['SOA_IN_MS', 'PROBE_SALIENT']).sum().PROBE_FIRST_RESPONSE pf_obs = data.groupby(['SOA_IN_MS', 'PROBE_SALIENT']).count().PROBE_FIRST_RESPONSE pf_ci = abs(np.array(prop_ci(pf_count.values, pf_obs.values)) - pf_mean.values) ax.plot(SOAs, pf_mean.values[::2], 'k.') ax.errorbar(np.array(SOAs)-0.5, pf_mean.values[::2], pf_ci[:,::2], fmt='none', color='k', alpha=0.5) ax.plot(SOAs, pf_mean.values[1::2], 'g.') ax.errorbar(np.array(SOAs)+0.5, pf_mean.values[1::2], pf_ci[:,1::2], fmt='none', color='g', alpha=0.5) ax.axvline(0, linestyle='dashed') ax.axhline(0.5, linestyle='dashed') ax.text(-20,0, loo_text) if paras is not None: for i, varname in enumerate(paras): stats = az.summary(trace, var_names=[varname], hdi_prob=.95) for j, s in enumerate(stats['mean']): text = r'$' + varname + r'$: %.2f [%.2f, %.2f]' text = text%(s, stats['hdi_2.5%'][j], stats['hdi_97.5%'][j]) posx, posy = .1 + .5 - (1 - j) * .5, 0.95 - (.05*i) - ((1-j)*.5) ax.text(posx, posy, text, transform = ax.transAxes, color=['k','g'][j]) ax.set_title(title)
def plot_events(real, pred, real_, pred_, label=None): from matplotlib.pylab import plt import random fig, ax = plt.subplots(figsize=(10, 2)) ax.set_title(label) plt.xlim(0, max(real[-1][1], 10)) ax.set_xticks(np.arange(0, max(real[-1][1], 10), .1), minor=True) maxsize = 20 for i in range(min(maxsize, len(pred_))): d = pred_[i] plt.axvspan(d[0], d[1], 0, 0.4, linewidth=1, edgecolor='k', facecolor='m', alpha=.6) for i in range(min(maxsize, len(pred))): d = pred[i] plt.axvspan(d[0], d[1], 0.1, 0.5, linewidth=1, edgecolor='k', facecolor='r', alpha=.6) # maxsize=len(real) for i in range(min(maxsize, len(real_))): gt = real_[i] plt.axvspan(gt[0], gt[1], 0.6, 1, linewidth=1, edgecolor='k', facecolor='y', alpha=.6) for i in range(min(maxsize, len(real))): gt = real[i] plt.axvspan(gt[0], gt[1], 0.5, .9, linewidth=1, edgecolor='k', facecolor='g', alpha=.6) plt.grid(True) plt.minorticks_on() plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
def plot_forec_val(ytest, predict, my_dates, scl, y_hat): date_app = pd.date_range(start=my_dates[-1], periods=len(y_hat), freq='M') fig2, ax2 = plt.subplots(figsize=(18, 5)) ax2.plot(my_dates, ytest, color='black', label='Факт') ax2.plot(my_dates, scl.inverse_transform(predict), '-.', color='blue', label='Прогноз') ax2.plot(date_app, scl.inverse_transform(y_hat), '-.', color='blue') ax2.legend(loc='best', fontsize=16) ax2.grid()
def plot_events_with_event_scores(gt_event_scores, detected_event_scores, ground_truth_events, detected_events, label=None): fig, ax = plt.subplots(figsize=(10, 2)) ax.set_title(label) maxsize = 10 maxsize = len(detected_events) for i in range(min(maxsize, len(detected_event_scores))): d = detected_events[i] plt.axvspan(d[0], d[1], 0, 0.5, linewidth=1, edgecolor='k', facecolor='r', alpha=.6) txt = detected_event_scores[i] if type(txt) == int or type(txt) == float: txt = "%g" % round(detected_event_scores[i], 2 if detected_event_scores[i] < 10 else 0) plt.text((d[1] + d[0]) / 2, 0.1 + random.random() / 4, txt, horizontalalignment='center', verticalalignment='center') maxsize = len(ground_truth_events) for i in range(min(maxsize, len(gt_event_scores))): gt = ground_truth_events[i] plt.axvspan(gt[0], gt[1], 0.5, 1, linewidth=1, edgecolor='k', facecolor='g', alpha=.6) txt = gt_event_scores[i] if type(txt) == int or type(txt) == float: txt = "%g" % round(gt_event_scores[i], 2 if gt_event_scores[i] < 10 else 0) plt.text((gt[1] + gt[0]) / 2, 0.6 + random.random() / 4, txt, horizontalalignment='center', verticalalignment='center')
def plot_df(name: str = 'start_date_analysis1.pkl'): """ Plot 2D for file/test density :param name: :return: """ df = pd.read_pickle(name) print(df) fig, axarr = plt.subplots(2, 2, sharey=True, sharex=True) df = df.iloc[::-1] plt.suptitle('Threshold Start Date Analysis', fontsize=14) for idx, row in enumerate(sorted(df['threshold_pairs'].unique())): data = df[df['threshold_pairs'] == row] if idx == 0 or idx == 1: column = 0 else: column = 1 sns.lineplot(x="date", y="mpf", hue="threshold", data=data, palette='tab10', ax=axarr[idx % 2, column]) sns.lineplot(x="date", y="tpt", hue="threshold", data=data, palette='tab10', ax=axarr[idx % 2, column], linestyle='--', legend=False) axarr[idx % 2, column].set_xlabel('Start Date') axarr[idx % 2, column].set_ylabel('Frequency') axarr[idx % 2, column].set_title(f'Pairs Threshold - {row}') axarr[idx % 2, column].legend(loc='center left') # plot vertical line # plt.axvline(x=3, linestyle='-.', label='Optimal Value') # plt.tight_layout() plt.show()
def f3(): xs = [0, 1, 2, 5] ys = [0, 0, 1, 1] fig, ax = plt.subplots() ax.set_yticklabels([]) ax.set_xticklabels([]) plt.plot(xs, ys, label="$f_n(x)$") plt.xticks(xs, ['', r'$n-1$', r'$n$', '']) plt.yticks([1], ['$1$']) plt.legend() ax.spines['left'].set_position('zero') ax.spines['right'].set_color('none') ax.spines['bottom'].set_position('zero') ax.spines['top'].set_color('none') ax.axis('equal') plt.show()
def run(plotIt=True, nx=5, ny=5): """ Utils: surface2ind_topo ======================= Here we show how to use :code:`Utils.surface2ind_topo` to identify cells below a topographic surface. """ mesh = Mesh.TensorMesh([nx, ny], x0='CC') # 2D mesh xtopo = np.linspace(mesh.gridN[:, 0].min(), mesh.gridN[:, 0].max()) topo = 0.4 * np.sin(xtopo * 5) # define a topographic surface Topo = np.hstack([Utils.mkvc(xtopo, 2), Utils.mkvc(topo, 2)]) #make it an array indcc = surface2ind_topo(mesh, Topo, 'CC') if plotIt: from matplotlib.pylab import plt from scipy.interpolate import interp1d fig, ax = plt.subplots(1, 1, figsize=(6, 6)) mesh.plotGrid(ax=ax, nodes=True, centers=True) ax.plot(xtopo, topo, 'k', linewidth=1) ax.plot(mesh.vectorCCx, interp1d(xtopo, topo)(mesh.vectorCCx), '--k', linewidth=3) aveN2CC = Utils.sdiag(mesh.aveN2CC.T.sum(1)) * mesh.aveN2CC.T a = aveN2CC * indcc a[a > 0] = 1. a[a < 0.25] = np.nan a = a.reshape(mesh.vnN, order='F') masked_array = np.ma.array(a, mask=np.isnan(a)) ax.pcolor(mesh.vectorNx, mesh.vectorNy, masked_array.T, cmap=plt.cm.gray, alpha=0.2) plt.show()
def update_NIdaq_data(args, window=20, subsampling=100): metadata = np.load(os.path.join(args.datafolder, 'metadata.npy'), allow_pickle=True).item() if os.path.isfile(os.path.join(args.datafolder, 'NIdaq.start.npy')): NIdaq_Tstart = np.load(os.path.join(args.datafolder, 'NIdaq.start.npy'))[0] NIdaq_Tstart = np.load(os.path.join(args.datafolder, 'NIdaq.start.npy'))[0] data = np.load(os.path.join(args.datafolder, 'NIdaq.npy'), allow_pickle=True).item() t = np.arange(len(data['analog'][0,:]))/float(metadata['NIdaq-acquisition-frequency']) # checking that the two realisations are the same: fig, ax = plt.subplots(1, figsize=(7,5)) cond = (t>(args.time-window)) & (t<(args.time+window)) ax.plot(t[cond][::subsampling], data['analog'][0,cond][::subsampling], label='data') ax.plot(args.time*np.ones(2), ax.get_ylim(), 'r-', label='reset point') plt.legend() plt.show() y = input(' /!\ ----------------------- /!\ \n Confirm that you want to remove episodes after the reset point ? [yes/No]\n') if y in ['y', 'Y', 'yes', 'Yes']: temp = str(tempfile.NamedTemporaryFile().name)+'.npy' print(""" ---> moving the old data to the temporary file directory as: "%s" [...] """ % temp) shutil.move(os.path.join(args.datafolder, 'NIdaq.npy'), temp) print('applying changes [...]') cond = (t>args.time) data['analog'][0,cond] = data['analog'][0,cond][0] np.save(os.path.join(args.datafolder, 'NIdaq.npy'), data) print('done !') else: print('--> data update aborted !! ')
def f4(): capped_xs = np.linspace(-0.94, 0.8, 1000) xs = np.linspace(-1, 1, 1000) ys = [1 / (1 - x) for x in capped_xs] def getN(N): def fN(x): total = 0.0 for i in range(N + 1): total += x**i return total return fN yss = [] for N in range(1, 4): fN = getN(N) yss.append([fN(x) for x in xs]) fig, ax = plt.subplots() ax.set_yticklabels([]) ax.set_xticklabels([]) plt.plot(capped_xs, ys, label=r"$\frac{1}{1-x}$") for i, ys in enumerate(yss): plt.plot(xs, ys, label="$N={}$".format(i + 1), alpha=0.3) plt.plot([1, 1], [0, 5], "k--", alpha=0.3) plt.plot(-1, 0.5, 'o', markerfacecolor='None', markeredgecolor='C0', markersize=5) plt.xticks([-1, 1], ['-1', '1']) plt.legend() ax.spines['left'].set_position('zero') ax.spines['right'].set_color('none') ax.spines['bottom'].set_position('zero') ax.spines['top'].set_color('none') ax.axis('equal') plt.show()
def display_populations(self, top_k=10, max_chromosome_len=1024, rounding=None): for pop_idx, population in enumerate(self._populations): num_chromosomes = min(len(population), top_k) heatmap = [] gene_max = 0 for cidx, chromosome in enumerate(population): genes = [] for gene in chromosome[:max_chromosome_len]: if isinstance(rounding, type(None)): genes.append(str(gene)) elif rounding > 0: genes.append(round(float(gene), rounding)) else: genes.append(int(gene)) gene_max = max(gene_max, gene) heatmap.append(genes) if cidx > num_chromosomes: break fig, ax = plt.subplots() im = ax.imshow(heatmap, interpolation='nearest', cmap="viridis", aspect='auto') # gradient bar cbar = ax.figure.colorbar(im, ax=ax) cbar.ax.set_ylabel('Gene value', rotation=-90, va="bottom") plt.title(f'Population - top {num_chromosomes}', fontsize=16, fontweight='bold') plt.xlabel("Genes") plt.ylabel("Chromosome") # fig.tight_layout() plt.show()
def construct_network(pts, connectivity_matrix, current=None, target=None): # Scatter plot of points, color coded by class fig, ax = plt.subplots() size = 35 for cluster, color in CLUSTER_COLORS.iteritems(): class_points = [x for x in pts if x.cluster == cluster] ax.scatter([p.x for p in class_points], [p.y for p in class_points], c=color, s=size) # Draw the connections if connectivity_matrix is not None: for start_ix, connections in enumerate(connectivity_matrix): for connect_ix, connected in enumerate(connections): if connected and connect_ix != start_ix: ax.plot(*zip( (pts[start_ix].x, pts[start_ix].y), (pts[connect_ix].x, pts[connect_ix].y)), c='k', linewidth=0.5) # Show where the message is going and where it currently is if current and target: ax.scatter(pts[current].x, pts[current].y, c='m', s=150) ax.scatter(pts[target].x, pts[target].y, c='y', s=190) return fig
if __name__ == '__main__': # Load meta = load_meta() tops = preload_tops(meta) # Select featurizer feature_name = 'Positions' reference = md.load('topology.pdb') featurizer = RawPositionsFeaturizer(ref_traj=reference) args = zip(meta.iterrows(), [featurizer] * meta.shape[0], [tops] * meta.shape[0]) # Do it in parallel with Pool() as pool: feature_trajs = dict(pool.imap_unordered(feat, args)) # Plot unscaled features ftrajs = np.concatenate([fx[::100] for fx in feature_trajs.values()]) fig, ax = plt.subplots(figsize=(15, 5)) plot_box(ax, fxx=ftrajs, feature_name='Unscaled {}'.format(feature_name)) fig.tight_layout() fig.savefig("Unscaled-{}-box.pdf".format(feature_name)) ## Save save_trajs(feature_trajs, 'Unscaled-{}-ftraj'.format(feature_name), meta) save_generic(featurizer, 'Unscaled-{}-featurizer.pickl'.format(feature_name))
print('\ncorr-kendall:\n', data.corr(method='kendall')) # spearman만 유사 # 상관계수를 방향성 있는 색으로 시각화 (heatmap) import seaborn as sns plt.rc('font', family='malgun gothic') sns.heatmap(data.corr()) plt.show() # hitmap에 텍스트 표시 추가사항 적용해 보기 corr = data.corr() # Generate a mask for the upper triangle mask = np.zeros_like(corr, dtype=np.bool) # 상관계수값 표시 mask[np.triu_indices_from(mask)] = True # Draw the heatmap with the mask and correct aspect ratio vmax = np.abs(corr.values[~mask]).max() fig, ax = plt.subplots() # Set up the matplotlib figure sns.heatmap(corr, mask=mask, vmin=-vmax, vmax=vmax, square=True, linecolor="lightgray", linewidths=1, ax=ax) for i in range(len(corr)): ax.text(i + 0.5, len(corr) - (i + 0.5), corr.columns[i], ha="center",
lags = np.array(lags) timescales = np.array(timescales).T / ps_to_ns msm = all_msms[np.extract(lags == 2000, np.arange(len(lags)))[0]] m = 2 # 1 timescales means 2 states pcca = PCCAPlus.from_msm(msm, n_macrostates=m) pcca_mapping = pcca.x print(len(pcca_mapping)) plt.clf() sns.color_palette('colorblind', m) with sns.plotting_context("notebook", font_scale=1.5): vec = msm.right_eigenvectors_ n_states = vec.shape[ 0] # may be less than 200 as T may be non-ergodic. fig, axes = plt.subplots(nrows=m, sharex=True) for i in range(m): for j in range(m): mask = pcca_mapping == j axes[i].bar(np.arange(n_states)[mask], vec[mask, i], label='PCCA State {}'.format(j), align='center') axes[i].yaxis.set_major_formatter(FormatStrFormatter('%.2f')) axes[i].legend() axes[i].set_ylabel('Cluster projection') plt.xlabel('Cluster') plt.savefig('figures/rmsd_msm_right_eigenvectors-pcca.png', transparent=True)
for dummy in cnt: xx,yy = dummy[0],dummy[1] the_x.append(xx); the_y.append(yy) corr_coeff = df.corr()["AdjGross"].sort_values(ascending=False) data = corr_coeff[["TheatersOpening","AveStudioShare","Budget","GenreScore","Runtime"]].to_dict() cnt = Counter(data).most_common() the_x1 = []; the_y1 = [] for dummy in cnt: xx,yy = dummy[0],dummy[1] the_x1.append(xx); the_y1.append(yy) the_labels = ["# Theaters", "Studio Score", "Budget", "Genre Score", "Runtime"] for what_plot in [0,1]: f, ax = plt.subplots(figsize=(10, 8)) ax.legend(ncol=2, loc="lower right", frameon=True,fontsize="large") ax.tick_params(labelsize=20) # sns.set(style="whitegrid") sns.set_color_codes("pastel") if(what_plot==1): sns.barplot(y=the_labels,x=[data[dummy] for dummy in the_x],color="b",label="Total Gross",orient="h") if(what_plot==0): sns.barplot(y=the_labels,x=the_y,color="b",label="Opening",orient="h") sns.despine(left=True, bottom=True) # ax.legend(ncol=2, loc="lower right", frameon=True) ax.set_xlabel("Correlation Coefficient",fontsize=20) ax.set_xlim(0,1) plt.savefig("correlation_coefficients_"+str(what_plot)+".png",bbox_inches="tight") plt.close("all") # STUDIO AND GENRE SCORES
sessions_per_feedback=4, antithetic=True, lr=1e-3, noise_std=1e-2) click_model2sessions2trajectory = foltr.tolist() click_model2sessions2trajectory09 = foltr09.tolist() click_model2sessions2trajectory05 = foltr05.tolist() # click_model2sessions2trajectory_b = b.tolist() sns.set(style="darkgrid") plt.close('all') # rcParams['figure.figsize'] = 12, 2 rcParams['figure.figsize'] = 28, 3 f, ax = plt.subplots(nrows=1, ncols=3, sharex=True) linear, two_layer = click_model2sessions2trajectory linear09, two_layer09 = click_model2sessions2trajectory09 linear05, two_layer05 = click_model2sessions2trajectory05 m = common_params['sessions_per_feedback'] * common_params['n_clients'] mid = 0 for row, model in enumerate([PERFECT_MODEL, NAVIGATIONAL_MODEL, INFORMATIONAL_MODEL]): if n_clients == 2000: Linear_ys = np.zeros(250) Linear_ys09 = np.zeros(250) Linear_ys05 = np.zeros(250) Neural_ys = np.zeros(250) Neural_ys09 = np.zeros(250) Neural_ys05 = np.zeros(250)
from matplotlib.pylab import plt from models import logistic_regression_model, tvatoj_model, aqgp_model from score_and_plot import plot_ppc_and_score df = pd.read_csv('dataset.csv') all_participants = sorted(set(df['PARTICIPANT_NUMBER'])) # For all participants (in subsets of 2) ... for ps in [(0, 2), (2, 4), (4, 6), (6, 8)]: # Select subset of the data participants = all_participants[ps[0]:ps[1]] # Create empty figure f, axs = plt.subplots(3, 2, sharex=True, figsize=(6, 8)) # Sample from each model and create plots for i, p in enumerate(participants): # Exclude observations where fixation was lost data = df[(df['PARTICIPANT_NUMBER'] == p) & (df['EYE_ERROR'] == 0)] # Run logistic regression model with logistic_regression_model(data) as _lr_model: lr_trace = pm.sample(4000, tune=2000, init='adapt_diag', chains=4) plot_ppc_and_score(lr_trace, data, paras=['PSS', 'DL'], title='P' + str(p) + ': Logistic Regression', ax=axs[0, i])
sample = sample[:, np.arange(0, ftraj.shape[1], 2)] print(feature, sample.shape) elif feature in ['contacts']: sample = ftraj[np.random.choice(ftraj.shape[0], size=10000)] print(feature, sample.shape) else: sample = ftraj[np.random.choice(ftraj.shape[0], size=10000), :] print(feature, sample.shape) try: n_feats_plot = sample.shape[1] except IndexError: n_feat_plot = 1 # BOX PLOT fig, axes = plt.subplots() axes.boxplot( sample, boxprops={'color': colors[0]}, whiskerprops={'color': colors[0]}, capprops={'color': colors[0]}, medianprops={'color': colors[2]}, ) axes.set_xlabel("Feature Index", fontsize=16) xx = np.arange(0, n_feats_plot, 10) axes.set_xticks(xx) axes.set_xticklabels([str(x) for x in xx]) axes.set_xlim((0, n_feats_plot + 1)) axes.set_ylabel("{} Value".format(feature), fontsize=16) plt.savefig("figures/{}-features-box.pdf".format(feature))
plt.ylabel("y") x_list2,y_list2,vx_list2,vy_list2=projectile(np.pi/4,60,25) plt.plot(x_list2,y_list2) plt.title("Combine graph of two different projectiles without drag force") plt.show() range2=max(x_list2) print (range2) plot2=plt.figure(2) x_list3,y_list3,vx_list3,vy_list3=projectile(np.pi/3,30,30,0.005) plt.plot(x_list3,y_list3) plt.xlabel("x") plt.ylabel("y") plt.title ("Graph of projectile with drag force") plt.show() t = np.linspace(0,30,300) t=t[0:len(vx_list3)] fig, (ax1,ax2)=plt.subplots(1,2) ax1.plot(vx_list3,t) ax2.plot(vy_list3,t) ax1.set(xlabel="time", ylabel="x-component of velocity") ax2.set(xlabel="time", ylabel="y-component of velocity") fig.suptitle("x and y component of velocity vs time") plt.show()
def plot_hmm(mcmc_step, obs_index=None, axes=None, smpl_subset=None, states_true=None, plot_samples=True, range_slice=slice(0, None)): r""" Plot the observations, estimated observation mean parameter's statistics and estimated state sequence statistics. Parameters ========== mcmc_res: a pymc.MCMC object The MCMC object after estimation. obs_index: pandas indices or None Time series index for observations. axes: list of matplotlib axes Axes to use for plotting. smpl_subset: float, numpy.ndarray of int, or None If a float, the percent of samples to plot; if a `numpy.ndarray`, the samples to plot; if None, plot all samples. states_true: pandas.DataFrame True states time series. plot_samples: bool If True, plot the individual sample values, along with the means. Returns ======= A matplotlib plot object. """ if axes is None: from matplotlib.pylab import plt fig, axes = plt.subplots(2, 1, sharex=True) _ = [ax_.clear() for ax_ in axes] #start_idx = y_baseline.index.searchsorted("2014-11-23 00:00:00") #end_idx = y_baseline.index.searchsorted("2014-12-01 00:00:00") #range_slice = slice(start_idx, end_idx) y_rv, = mcmc_step.observed_stochastics y_df = pd.DataFrame(y_rv.value[range_slice], index=obs_index[range_slice], columns=[r"$y_t$"]) y_df.plot(ax=axes[0], drawstyle='steps-mid', zorder=3) # Would be nice to have a y_rv.mean, or something. if isinstance(y_rv, (pymc.Normal, pymc.Poisson, pymc.TruncatedNormal)): mu_rv = y_rv.parents['mu'] elif len(y_rv.parents) == 1: mu_rv, = y_rv.parents.values() else: raise ValueError("Unsupported observation distribution") mu_samples = mu_rv.trace().T[range_slice] if plot_samples: N_samples = mcmc_step._iter if smpl_subset is not None: if isinstance(smpl_subset, float): size_pct = int(N_samples * smpl_subset) smpls_idx = np.random.randint(0, N_samples, size=size_pct) else: smpls_idx = smpl_subset else: smpls_idx = xrange(N_samples) mu_df = pd.DataFrame(mu_samples[:, smpls_idx], index=y_df.index) mu_df.plot(ax=axes[0], zorder=1, drawstyle='steps-mid', legend=False, color='lightgreen', alpha=5. / len(smpls_idx), label=None, rasterized=True) mu_mean = mu_samples.mean(axis=1) mu_mean_df = pd.DataFrame(mu_mean, index=y_df.index, columns=['$E[\mu_t \mid y_{1:T}]$']) mu_mean_df.plot(ax=axes[0], zorder=1, drawstyle='steps-mid', color='green', alpha=0.5, fillstyle=None) # TODO: Add mu_true and posterior predictives. leg_top = axes[0].get_legend() leg_top.get_frame().set_alpha(0.7) if states_true is not None: #states_true_df = pd.DataFrame(states_true, index=y_df.index, # columns=["$S_t$ true"]) + 1 states_true_df = states_true[range_slice] + 1 states_true_df.columns = ["$S_t$ true"] states_true_df.plot(ax=axes[1], zorder=2, alpha=0.5, drawstyle='steps-mid', linewidth=3, linestyle="dashed") from amimodels.stochastics import HMMStateSeq states_rv, = filter(lambda x: isinstance(x, HMMStateSeq), mcmc_step.stochastics) states_samples = states_rv.trace().T[range_slice] + 1 if plot_samples: states_df = pd.DataFrame(states_samples[:, smpls_idx], index=y_df.index) states_df.plot(ax=axes[1], zorder=1, alpha=0.5, drawstyle='steps-mid', legend=False, color='lightgreen', label=None, rasterized=True) states_mean = states_samples.mean(axis=1) states_mean_df = pd.DataFrame(states_mean, index=y_df.index, columns=['$E[S_t \mid y_{1:T}]$']) states_mean_df.plot(ax=axes[1], zorder=2, drawstyle='steps-mid', alpha=0.5, color='green') axes[1].set_yticks(range(1, states_rv.K + 1)) axes[1].set_ybound((0.8, states_rv.K + 0.2)) leg_bottom = axes[1].get_legend() leg_bottom.get_frame().set_alpha(0.7) fig = getattr(axes[0], 'figure', None) if fig is not None: fig.tight_layout() return axes
def plot_his(self, hist): fig1, ax1 = plt.subplots(figsize=(18, 5)) ax1.plot(hist.history['mean_squared_error'], label='Обучение') ax1.plot(hist.history['val_mean_squared_error'], label='Валидация') ax1.legend(loc='best', fontsize=16) ax1.grid()
def plot_metric(df_metrics, name, batch_size=10, epochs=10): """ Parameter tuning plots with several subplots :param df_metrics: :param name: :param batch_size: :param epochs: :return: """ # One groupplot fig, axarr = plt.subplots(3, 4, sharey=True, sharex=True) plotname = 'apfd' subplot_labels = ['(a)', '(b)', '(c)'] for column, nr in enumerate(sorted(df_metrics['negative_ratio'].unique())): for row, emb_size in enumerate(df_metrics['emb_size'].unique()): for agidx, (labeltext, task, linestyle) in enumerate([ ('Classification', 'True', '-'), ('Regression', 'False', '-.') ]): rel_df = df_metrics[(df_metrics['emb_size'] == str(emb_size)) & (df_metrics['negative_ratio'] == str(nr)) & (df_metrics['batch_size'] == str(batch_size)) & (df_metrics['epochs'] == str(epochs))] # rel_df[rel_df['agent'] == agent].plot(x='step', y='napfd', label=labeltext, ylim=[0, 1], linewidth=0.8, # style=linestyle, color=sns.color_palette()[agidx], ax=axarr[row,column]) apfd = rel_df.loc[rel_df['classification'] == task, 'apfd'] miu = np.round(np.mean(apfd), 2) sigma = np.round(np.std(apfd), 2) label = labeltext + '\n $\mu$ - ' + str( miu) + ' $\sigma$ - ' + str(sigma) # sns.displot(data=rel_df, x="apfd", hue='classification', kde=True, ax=axarr[row, column]) sns.distplot(apfd, kde=True, bins=int(180 / 5), color=sns.color_palette()[agidx], hist_kws={'edgecolor': 'black'}, kde_kws={ 'linewidth': 4, 'clip': (0.0, 1.0) }, label=label, ax=axarr[row, column]) axarr[row, column].xaxis.grid(True, which='major') axarr[row, column].set_title('Emb_size - %s - Neg_Ratio - %s' % (emb_size, nr), fontsize=10) if row == 2: axarr[row, column].set_xlabel('APFD') if column == 0: axarr[row, column].set_ylabel('Density') axarr[row, column].legend(frameon=True, prop={'size': 6}) # Tweak spacing to prevent clipping of ylabel fig.suptitle('APFD Parameter Tuning - %d Epochs and batch-size - %d' % (epochs, batch_size)) fig.tight_layout() plt.savefig(name, bbox_inches='tight') plt.show()
classify_stars_candidates = pd.read_csv('/Volumes/VINCE/OAC/classify_stars_all.csv') classify_stars_candidates.columns = ['obj_ID', 'flag'] stars_candidates = pd.merge(stars_candidates,classify_stars_candidates,on='obj_ID') # - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - GCs = candidates[candidates.flag == 'g'] GCssorted = GCs.sort_values('SN1', ascending = False) stars = stars_candidates[stars_candidates.flag == 's'] # - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - f, ax = plt.subplots(2, 3, sharex='col', sharey='row') plt.rc('text', usetex=True) plt.rc('font', family='serif') ax[0][0].scatter(result['g_auto'] - result['r_auto'], result['g_auto'] - result['i_auto'], s=5, c='gray', edgecolor='none', alpha = 0.5) ax[0][0].scatter(GCs['g_auto'] - GCs['r_auto'], GCs['g_auto'] - GCs['i_auto'], s=13, c='red', edgecolor='none') ax[0][0].scatter(stars['g_auto'] - stars['r_auto'], stars['g_auto'] - stars['i_auto'], s=10, c = 'green', edgecolor='none') ax[0][0].set_xlabel('(g - r)') ax[0][0].set_ylabel('(g - i)') ax[0][0].set_xlim(-0.2,2) ax[0][0].set_ylim(0,3)
fclaynms = [ "defc7", "defc6", "defc5", ] for layername, W in zip(fclaynms, [W7, W6, W5]): U, S, V = np.linalg.svd(W) SgVals[layername] = S #%% SV_all = OrderedDict() for layername in fclaynms + laynms: SV_all[layername] = SgVals[layername] #%% #%% fig, ax = plt.subplots() colormap = plt.get_cmap('jet') ax.set_prop_cycle(cycler(color=[colormap(k) for k in np.linspace(0, 1, 12)])) for layername, SV in SV_all.items(): print(layername, SV.shape) SVnum = np.prod(SV.shape) plt.plot(np.arange(SVnum) / SVnum, np.sort(SV, axis=None)[::-1]) # np.sort(SV.reshape(-1))) plt.ylabel("SV") plt.xlabel("Singular Value Id (Ratio of All SV that layer)") plt.title("Singular Value per Layer in FC6 GAN") plt.legend(fclaynms + laynms) plt.savefig(join(figdir, "SV_per_layer_ratio.png")) plt.savefig(join(figdir, "SV_per_layer_ratio.pdf")) plt.show() #%%
shell=True) if 1 in plt_list: anim_daily_total(death.subset(names1), "daily_total_date", "Death") if 2 in plt_list: anim_daily_total(confirmed.subset(names1), "daily_total_date_confirmed", kind="Confirmed", minlim=10) if 3 in plt_list: #fig = plt.figure() #axes = plt.gca() fig, axs = plt.subplots(1, 2) axes = axs[0] axes2 = axs[1] file_name = "death_over_total" FFMpegWriter = manimation.writers['ffmpeg'] metadata = dict(title='Corona virus temp evolution', artist='Sylvain Guieu', comment='https://github.com/SylvainGuieu/corona') writer = FFMpegWriter(fps=2, metadata=metadata) # from coronapy import _default_styles subset = death.subset( ['Italy', 'Spain', 'France', 'Hubei', 'US', 'Japan', 'Korea, South']) #origin = subset.when_case_exceed(5)s