def draw_overall_charts(stats): #draws charts for data from multiple simulations #print stats[0] #chart 1 : average score per turn sns.set(style = "darkgrid", palette = "muted") fig = plt.subplots(1, 1, figsize = (4, 2.5)) b, g, r, p = sns.color_palette("muted", 4) ax = sns.tsplot(stats[0], color=g) ax.set(ylabel = "Average score per turn") ax.set_xlabel("Generation") plt.gcf().subplots_adjust(bottom = 0.22) plt.savefig("images/historical_overall.png") plt.clf() plt.cla() #chart 3: cooperation and defection sns.set(style="darkgrid", palette="muted") fig = plt.subplots(1, 1, figsize=(4, 3)) b, g, r, p = sns.color_palette("muted", 4) data = np.dstack([[j for j in stats[i]] for i in [1,2]]) ax = sns.tsplot(data, color = [ b, r]) ax.set(ylabel = "percent coop/defect") ax.set_xlabel("Generation") plt.gcf().subplots_adjust(bottom = 0.22) plt.savefig("images/cooppct_overall.png") return
def display_trial_stats(df, title_prefix, ylim_bottom, ylim_top): """ Displays summary statistics and time series plot describing the 4 columns in a simulation stats DataFrame: The length (number of steps) in each trial of the simulation, the total reward for each trial, the total negative reward in each trial, and whether each trial reach the designated destination. """ successes = df[df.reached_destination==True].Trial failures = df[df.reached_destination==False].Trial print "The destination was reached in {} out of {} trials.".format(successes.shape[0], df.shape[0]) display(df[['total_reward', 'negative_reward', 'trial_length']].describe().T) sns.set(font_scale=1.5, style={"axes.facecolor": "white"}) sns.plt.figure(figsize=(16, 8)) ax = sns.tsplot(df.trial_length, color='.75', legend=True, condition='Trial Length') ax = sns.tsplot(df.total_reward, color='#106B70', legend=True, condition='Total Reward') ax = sns.tsplot(df.negative_reward, color='#D43500', legend=True, condition='Negative Reward') ax = sns.rugplot(successes, color='green', height=1, linewidth=10, alpha=0.1) ax = sns.rugplot(failures, color='red', height=1, linewidth=10, alpha=0.1) sns.plt.legend(labels=['Trial Length', 'Total Reward', 'Negative Reward', 'Reached Destination'], frameon=True) ax.set(xlabel='Trial', ylabel='Value') ax.set_title(title_prefix + ': Trial Length, Total Reward, and Negative Reward for each Trial') sns.plt.ylim(ylim_bottom, ylim_top) sns.plt.plot([0, 100], [0, 0], linewidth=1, color='.5')
def main(): data = [] thread_list = [] repeats = 1 steps = 1000000 nsteps = 10 max_num_threads = 8 mdp = ElectricityMDP(0.1) learner = QLearning(mdp, 0.01, 0.3) if 0: while repeats > 0: if threading.active_count() - 1 < max_num_threads: repeats = repeats - 1 # remaining repeats p = threading.Thread(target=simulate, args=(steps, nsteps, copy.deepcopy(mdp), copy.deepcopy(learner), data)) thread_list.append(p) p.start() for thread in thread_list: thread.join() else: simulate(steps, nsteps, copy.deepcopy(mdp), copy.deepcopy(learner), data) pkl_file = open( "data3.p", "wb") pickle.dump(data, pkl_file) pkl_file.close() sns.tsplot(time=range(1, steps+1, (steps/nsteps)),data=np.asarray(data), condition="Q-Learning", err_style="ci_bars", color="g") plt.xlabel('Iterations') plt.ylabel('Utility') plt.show()
def plot_reconstruction_result(res): """ Plot original and reconstructed graph plus time series """ fig = plt.figure(figsize=(32, 8)) gs = mpl.gridspec.GridSpec(1, 4) # original graph orig_ax = plt.subplot(gs[0]) plot_graph(nx.from_numpy_matrix(res.A.orig), orig_ax) orig_ax.set_title('Original graph') # time series ax = plt.subplot(gs[1:3]) sns.tsplot( time='time', value='theta', unit='source', condition='oscillator', estimator=np.mean, legend=False, data=compute_solutions(res), ax=ax) ax.set_title(r'$A_{{err}} = {:.2}, B_{{err}} = {:.2}$'.format(*compute_error(res))) # reconstructed graph rec_ax = plt.subplot(gs[3]) tmp = res.A.rec tmp[abs(tmp) < 1e-1] = 0 plot_graph(nx.from_numpy_matrix(tmp), rec_ax) rec_ax.set_title('Reconstructed graph') plt.tight_layout() save(fig, 'reconstruction_overview')
def plot_data(data, value="AverageReturn"): if isinstance(data, list): data = pd.concat(data, ignore_index=True) sns.set(style="darkgrid", font_scale=1.5) sns.tsplot(data=data, time="Iteration", value=value, unit="Unit", condition="Condition") plt.legend(loc='best').draggable() plt.show()
def drift_plot(self, title=""): """Plot the average change in x0 and y0""" # set up plot fig, (ax0, ax1) = plt.subplots(1, 2) # make holders for coordinates x = [] y = [] # loop through fits for f in self.fits: # if everything is finite add the coordinates minus bias if np.isfinite(f.x0).all() and np.isfinite(f.y0).all(): x.append(f.x0 - f.x0.mean()) y.append(f.y0 - f.y0.mean()) assert x and y, "x or y is empty" # plot the mean with ci 90% bands sns.tsplot(x, ax=ax0, ci=90, color='b') sns.tsplot(y, ax=ax0, ci=90, color='r') ax0.set_xlabel('Frame #') ax0.set_ylabel('Distance (pixel)') # flatten data xar = np.array(x).ravel() yar = np.array(y).ravel() # determine good histogram bin size nbins = int(np.sqrt(xar.size)) # plot hists ax1.hist(xar, color=ColorConverter().to_rgba("b", 0.5), normed=True, label="$x$", bins=nbins, histtype="stepfilled", range=(-1, 1)) ax1.hist(yar, color=ColorConverter().to_rgba("r", 0.5), normed=True, label="$y$", bins=nbins, histtype="stepfilled", range=(-1, 1)) ax1.set_xlabel("Distance (pixel)") ax1.legend() fig.suptitle(title) fig.tight_layout() return fig, (ax0, ax1)
def draw_plot(directory, fname): sns.set_context("talk") with open("{}/{}.json".format(directory, fname)) as json_file: data = json.load(json_file) sensitivity = np.array(data["sensitivity"]) specificity = np.array(data["specificity"]) value_range = np.array(data["value_range"]) ax = sns.tsplot(sensitivity, time=value_range, err_style="unit_points") ax.set_xlabel("Number of samples") ax.set_ylabel("Sensitivity") ax.set_ylim([0., 1.]) plt.savefig("{}/{}_sensitivity.svg".format(directory, fname)) plt.clf() ax2 = sns.tsplot(specificity, time=value_range, err_style="unit_points") ax2.set_xlabel("Number of samples") ax2.set_ylabel("Specificity") ax2.set_ylim([0., 1.02]) plt.savefig("{}/{}_specificity.svg".format(directory, fname)) plt.clf()
def plot_global_utility(data, parameters, axis, par_name=None, par_value=None, fontsize=15): """ Plot global utility against time. data: Contains the output of compute_run. parameters: Parameters of the run. axis: Matplotlib axis to add this plot to. par_name: Parameter name that we're varying in the simulation. par_value: Parameter value that we're varying in the simulation. fontsize: Font size for legends and tick marks. """ # Data to plot Ug_data = get_values_from_compute_run(data, with_reflexivity=True, variable='global_utility') activation_time = compute_activation_time(data, parameters) # Plot adjustments if par_name is not None and par_value is not None: axis.set_title(r'$%s = %s$' % (par_name, str(par_value)), fontsize=fontsize) axis.set_ylabel('$U_G$', fontsize=fontsize) axis.tick_params(labelsize=fontsize-2) plt.setp(axis.get_xticklabels(), visible=False) # Plots sns.tsplot(data=Ug_data, color=sns.xkcd_rgb["medium green"], ax=axis) axis.axvline(x=activation_time, linestyle='--', linewidth=1, color='0.4')
def plot_variation(output_samples, kendalls, q_func, plot_area='left', plt_lib='seaborn', figsize=(7, 4), ylabel=None, colors={'Normal': 'b', 'Clayton': 'g', 'Gumbel': 'r', 'Joe': 'm'}, n_boot=5000, ci=99.9): """ """ set_style_paper() if plot_area == 'full': taken = np.ones(kendalls.shape, dtype=bool) elif plot_area == 'left': taken = kendalls <= 0. elif plot_area == 'right': taken = kendalls >= 0. sorting = np.argsort(kendalls[taken]) fig, ax = plt.subplots(figsize=figsize) for copula in output_samples: if plt_lib == 'matplotlib': quantities = q_func(output_samples[copula].T) ax.plot(kendalls[taken][sorting], quantities[taken] [sorting], 'o-', label=copula, markersize=5) else: sns.tsplot(output_samples[copula][:, taken], time=kendalls[taken], condition=copula, err_style='ci_band', ci=ci, estimator=q_func, n_boot=n_boot, color=colors[copula], ax=ax) ax.set_xlabel('Kendall coefficient') if ylabel is not None: ax.set_ylabel(ylabel) else: ax.set_ylabel('Output quantity') ax.legend(loc=0) ax.axis('tight') fig.tight_layout()
def plot_segment_stats(segments, segment_keys=['full_mismatch', 'full_match', 'some_mismatch'], colors=['red', 'blue', 'green'], measure_key='li'): for seg_key, color in zip(segment_keys, colors): grouped_time_samples = _group_sample_by_time(segments[seg_key], key=measure_key) sns.tsplot(data=grouped_time_samples.T, color=color)
def main(): path = "C:/Users/Andrew/Documents/GitHub/summer_game_theory_repo/" path2= "C:/Users/Andrew/Documents/GitHub/summer_game_theory_repo/IPD_output/images/" avg_turn_score_df=pd.io.json.read_json(path+"avgscore.json") generations,simulations=avg_turn_score_df.shape avg_turn_score_df=avg_turn_score_df.sort() # jesus this object is whiny print avg_turn_score_df sns.set(style = "darkgrid", palette = "muted",rc={"lines.linewidth":0.1}) fig = plt.subplots(1, 1, figsize = (16, 12)) b, g, r, p = sns.color_palette("muted", 4) stuff_t= np.transpose(np.array(avg_turn_score_df)) #ax = sns.tsplot(result, color=g) cis = np.linspace(98, 10, 4) #acts like range does #ax = sns.tsplot(stats[0] ,err_style="boot_traces", n_boot=simulations) #balls=[avg_turn_score_df[22][i] for i in range(generations)] ax=sns.tsplot( stuff_t ,err_style="ci_band",ci = cis, color=p) ax.set_autoscale_on(False) ax.axis([0,generations,0,3]) #[xmin,xmax,ymin,ymax] '''for i in range(simulations): plt.plot(range(generations), avg_turn_score_df[i], color='black', alpha=0.4)''' plt.xlabel('Generation') plt.ylabel('Average score per turn') plt.title('Distribution of Average Scores per Turn') plt.savefig(path2+"overall_avg_turn_score.png") #plt.show() plt.clf() plt.cla() avg_coop_pct_df=pd.io.json.read_json(path+"avgcoop.json") avg_defect_pct_df=pd.io.json.read_json(path+"avgdefect.json") avg_coop_pct_df=avg_coop_pct_df.sort() # tres important! avg_defect_pct_df=avg_defect_pct_df.sort() # do not forget! stuff_c=np.transpose(np.array(avg_coop_pct_df)) stuff_d=np.transpose(np.array(avg_defect_pct_df)) sns.set(style="darkgrid", palette="muted",rc={"lines.linewidth":0.5}) fig = plt.subplots(1, 1, figsize=(16, 12)) b, g, r, p = sns.color_palette("muted", 4) sns.tsplot( stuff_c ,err_style="ci_band",ci = cis, color=b) sns.tsplot( stuff_d ,err_style="ci_band",ci = cis, color=r) '''for i in range(simulations): plt.plot(range(generations), avg_coop_pct_df[i], color='blue', alpha=0.01) plt.plot(range(generations), avg_defect_pct_df[i], color='red', alpha=0.01)''' plt.xlabel('Generation') plt.ylabel('Percent') plt.title('Percentage of cooperation vs defection') plt.savefig(path2+"overall_cooppct.png")
def plot_scores(matched_signals, unique_clrs, ind, stimulus_on_time, stimulus_off_time): ######Plot mean signals according to color and boxplot of number of pixels in each plane sns.tsplot(np.array(matched_signals[ind].clr_grped_signal), linewidth=3, ci=95, err_style="ci_band", color=unique_clrs[ind]) plt.locator_params(axis = 'y', nbins = 4) sns.axlabel("Time (seconds)","a.u") plot_vertical_lines_onset(stimulus_on_time) plot_vertical_lines_offset(stimulus_off_time) plt.axhline(y=0, linestyle='-', color='k', linewidth=1)
def show_clusters( y_kmeans, Vorg, title=None): set_y = set(y_kmeans) for i in set_y: c = plt.cm.rainbow_r(i/max(set_y)) sns.tsplot( Vorg[y_kmeans==i,:], color=c) plt.xlabel('Time') plt.ylabel('Maganitude') if title: plt.title(title)
def plot_spectral_gaps(max_n_dims, n_trials=25, full=True, save_directory='~/Desktop'): """ Generates the spectral gap figure :param max_n_dims: max number of dimensions to go up to :param n_trials: number of trials averaged at each dimension :param full: True for computing the spectral gap of the full transition matrix (an 2*n_dim X 2*n_dim matrix) :param save_directory: path to save figure to :returns: None, saves a figure at the specified path :rtype: None """ hmc_sg = [] rf_sg = [] sgs = [] t_begin = time.clock() orders = np.arange(3, max_n_dims) * 2 for order in orders: t_start = time.clock() hmc_trials = [] rf_trials = [] for _ in xrange(n_trials): H = np.random.randn(order / 2) hmc = AlgebraicHMC(order, energies=H) rf = AlgebraicReducedFlip(order, energies=H) hmc_trials.append(sg(hmc, full)) rf_trials.append(sg(rf, full)) hmc_sg.append(hmc_trials) rf_sg.append(rf_trials) print "order {} took {} seconds".format(order, time.clock() - t_start) hmc_sg = np.array(hmc_sg) rf_sg = np.array(rf_sg) # putting into dataframe for seaborn for idx in xrange(n_trials): hmc_df = pd.DataFrame(dict( Sampler=["Discrete-time HMC"] * len(orders), subj=["subj{}".format(idx)] * len(orders), order=orders, sg=hmc_sg[:,idx]), dtype=np.float) rf_df = pd.DataFrame(dict( Sampler=["Markov Jump HMC"] * len(orders), subj=["subj{}".format(idx)] * len(orders), order=orders, sg=rf_sg[:,idx]), dtype=np.float) sgs.append(hmc_df) sgs.append(rf_df) sgs_df = pd.concat(sgs) print "computation finished. total time elapsed: {}".format(time.clock() - t_begin) sns.tsplot(sgs_df, time="order", unit="subj", condition="Sampler", value="sg") plt.ylabel("Spectral gap (log)") plt.xlabel("Number of states in ladder") plt.yscale('log') plt.title("Spectral gap vs. number of system states") plt.savefig("{}/sg_gap_{}_energies_{}_trials.pdf".format( expanduser(save_directory), max_n_dims, n_trials))
def tsplot_for_facetgrid(*args, **kwargs): """ sns.tsplot does not work with sns.FacetGrid.map (all condition in a subplot are drawn in the same color). This is because either tsplot misinterprets the color parameter, or because FacetGrid incorrectly decides to pass in a color parameter to tsplot. Not sure which in which, but removing that parameter fixes the problem """ if 'color' in kwargs: kwargs.pop('color') sns.tsplot(*args, **kwargs)
def show_km(y, n=4, c=['b', 'g', 'r', 'k'], title='KMeans Clustering'): km = cluster.KMeans(n) yi = km.fit_predict(y) #c = ['b', 'g', 'r', 'k'] for i in range(n): sns.tsplot(y[yi==i], color=c[i]) plt.title(title)
def createRegressionPlots(predictions,performance,coefs,fb_coefs,nfb_coefs,GroupDF,goodsubj,savefig=True): f=plt.figure(figsize=(22,12)) ax1=plt.subplot2grid((2,4),(0,0), colspan=3) ax2=plt.subplot2grid((2,4),(0,3)) ax3=plt.subplot2grid((2,4),(1,0), colspan=2) ax4=plt.subplot2grid((2,4),(1,2), colspan=2) dmnIdeal=pd.read_csv('/home/jmuraskin/Projects/NFB/analysis/DMN_ideal_2.csv') sns.tsplot(data=predictions,time='TR',value='predicted',unit='subj',condition='fb',ax=ax1) ax1.plot((dmnIdeal['Wander']-dmnIdeal['Focus'])/3,'k--') ax1.set_title('Average Predicted Time Series') g=sns.violinplot(data=performance,x='fb',y='R',split=True,bw=.3,inner='quartile',ax=ax2) # plt.close(g.fig) g=sns.violinplot(data=coefs,x='pe',y='Coef',hue='fb',split=True,bw=.3,inner='quartile',ax=ax3) g.plot([-1,len(unique(coefs['pe']))],[0,0],'k--') g.set_xlim([-.5,len(unique(coefs['pe']))]) ylim=g.get_ylim() t,p = ttest_1samp(np.array(performance[performance.fb=='FEEDBACK']['R'])-np.array(performance[performance.fb=='NOFEEDBACK']['R']),0) ax2.set_title('Mean Subject Time Series Correlations-p=%0.2f' % p) t,p = ttest_1samp(np.array(fb_coefs['Coef'].reshape(len(unique(GroupDF[GroupDF.Subject_ID.isin(goodsubj)]['Subject_ID'])),len(unique(coefs['pe'])))),0) p05_FB,padj=fdr_correction(p,0.05) t,p = ttest_1samp(np.array(nfb_coefs['Coef'].reshape(len(unique(GroupDF[GroupDF.Subject_ID.isin(goodsubj)]['Subject_ID'])),len(unique(coefs['pe'])))),0) p05_NFB,padj=fdr_correction(p,0.05) for idx,(pFDR_FB,pFDR_NFB) in enumerate(zip(p05_FB,p05_NFB)): if pFDR_FB: ax3.scatter(idx,ylim[1]-.05,marker='*',s=75) if pFDR_NFB: ax3.scatter(idx,ylim[0]+.05,marker='*',s=75) t,p=ttest_1samp(np.array(fb_coefs['Coef']-nfb_coefs['Coef']).reshape(len(unique(GroupDF[GroupDF.Subject_ID.isin(goodsubj)]['Subject_ID'])),len(unique(coefs['pe']))),0) p05,padj=fdr_correction(p,0.05) sns.barplot(x=range(len(t)),y=t,ax=ax4,color='Red') for idx,pFDR in enumerate(p05): if pFDR: ax4.scatter(idx,t[idx]+ np.sign(t[idx])*0.2,marker='*',s=75) ax4.set_xlim([-0.5,len(unique(coefs['pe']))]) ax4.set_xlabel('pe') ax4.set_ylabel('t-value') ax4.set_title('FB vs. nFB PE') for ax in [ax1,ax2,ax3,ax4]: for item in ([ax.title, ax.xaxis.label, ax.yaxis.label]): item.set_fontsize(18) for item in (ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(12) f.tight_layout() if savefig: f.savefig('%s/RSN_LinearRegPrediction.pdf' % saveFigureLocation,dpi=300)
def plotpsc(tseries, colors, condition='condition'): roilist=tseries.roi.unique() for roi in roilist: roiseries=tseries[tseries['roi']==roi] f,ax=plt.subplots(figsize=[12,5]) sns.tsplot(roiseries, value='value', condition=condition, unit='subject', time='timename', color=colors,ci=68, ax=ax, err_style='ci_bars', err_kws={'alpha':.4}) ax.legend(loc=[1.02,.4], ncol=2) ax.set_title(roi) ax.set_ylabel('PSC (+/- 1 SEM)') ax.set_xlabel('time (TRs from onset)') sns.despine()
def show_cluster(y, yi, c=['b', 'g', 'r', 'k'], title='Clustering Resutls'): #km = cluster.KMeans(n) #yi = km.fit_predict(y) #c = ['b', 'g', 'r', 'k'] set_yi = list(set(yi)) n = len(set_yi) for i in set_yi: sns.tsplot(y[yi==i], color=c[set_yi.index(i)]) plt.title(title)
def plot_scores(self, fig1, gs, matched_signals, unique_clrs, plot_title='Habenula', gridspecs='[0,0]'): with sns.axes_style('dark'): ax1 = eval('fig1.add_subplot(gs' + gridspecs + ')') for ind in range(0, size(unique_clrs, 0)): sns.tsplot(array(matched_signals[ind].clr_grped_signal), linewidth=5, ci=95, err_style="ci_band", color=unique_clrs[ind]) ax1.locator_params(axis='y', nbins=4) sns.axlabel("Time (seconds)", "a.u") plt.title(plot_title, fontsize=14) self.plot_vertical_lines_onset() self.plot_vertical_lines_offset() plt.axhline(y=0, linestyle='-', color='k', linewidth=1)
def plot_appliances_aggregate(x,time=None): import seaborn as sns if isinstance(x,np.ndarray): appliances = x else: appliances,time = collect_appliances(x) time = downsample(time) appliances = downsample(appliances) sns.tsplot(appliances,time=time) plt.xlabel("steps") plt.ylabel("appliances")
def timeseries(data, columns, x_keys, x_label, y_label, relative_to=None, group_by=None, filter_by=None, **plot_args): data = prep_data(data, columns, x_keys, x_label, y_label, relative_to, group_by, filter_by) data[group_by] = data[group_by].apply(float) # data.sort_values(by=group_by, inplace=True) # plot_args.setdefault("err_style", "ci_bars") # plot_args.setdefault("estimator", stats.nanmedian) plot_args.setdefault("estimator", stats.nanmean) plot_args.setdefault("ci", 95) sns.tsplot(data=data, time=group_by, unit="seed", value=y_label, condition=x_label, **plot_args) sns.despine()
def plot_lagged(melted, ax, title): sns.set_context('poster') sns.tsplot(melted, time='lag', unit='unit', condition='impaired', value='value', estimator=np.nanmean, ax=ax, color={True: 'r', False: 'b'}) ax.set_title(title) ax.set_ylabel('Pearson Correlation') ax.set_xlabel('Lag (seconds)') ax.set_ylim((-1.05, 1.05))
def create_plot(df, column, ax, title, ylim=None, ylabel="Error", ylog=True, xlog=True, palette=None, legend=True): sns.despine(left=True) if ylog: ax.set_yscale("log") if xlog: ax.set_xscale("log") ax.tick_params(which="both", width=1, length=10) sns.tsplot(data=df[df["gen"] % 10 == 0], time="gen", value=column, unit="run", condition="algorithm", ax=ax, color=palette, legend=legend) if ylim is not None: ax.set_ylim(0, ylim) ax.set_ylabel(ylabel) ax.set_xlabel("Generations") ax.set_title(title)
def _test_individual_growth(self): dfs = [] time_obj = time_unit.Time(0, 10, step_size=1) print len(time_obj.t) # simulate environment def constant_env(time_obj): return [0] * len(time_obj.t) num_iters = 200 print "simulating individual cell growth" init_pop_size = 1 for n_iter in xrange(num_iters): # make on the fly a policy with the given constant # growth rate def constant_policy(time_obj, env, params={}): return {"nutrient_states": [0] * len(time_obj.t)} policy_obj = policies.GrowthPolicy(constant_policy) nutr_labels = ["glucose", "galactose"] nutr_simulator = constant_env env_obj = env.MixedDiscEnvironment(nutr_labels, nutr_simulator, mismatch_growth_rate=0.0, nutr_growth_rates=[0.3, 0.3/4]) env_obj.simulate(time_obj) growth_obj = growth.Growth(init_pop_size, env=env_obj, policy=policy_obj) results = growth_obj.simulate(time_obj, individual_growth=True) df = pandas.DataFrame({"time": time_obj.t, "iter": n_iter, "pop_size": np.power(2, results["log_pop_size"]), "log_pop_size": results["log_pop_size"]}) dfs.append(df) merged_df = pandas.concat(dfs, ignore_index=True) plt.figure() sns.set_style("ticks") sns.tsplot(time="time", unit="iter", err_style="unit_traces", value="pop_size", data=merged_df) plt.xlabel("Time step") plt.ylabel("Pop size") sns.despine(trim=True, offset=1) plot_fname = "./test_individual_growth.pdf" print "mean final population size: " print merged_df[merged_df["time"] == time_obj.t[-1]]["pop_size"].mean() print "Saving test results to: %s" %(plot_fname) plt.savefig(plot_fname)
def plot_spectral_gaps(max_n_energies, n_trials=25, full=True): """ plot the spectral gap """ hmc_sg = [] rf_sg = [] sgs = [] t_begin = time.clock() orders = np.arange(3, max_n_energies) * 2 for order in orders: t_start = time.clock() hmc_trials = [] rf_trials = [] for _ in xrange(n_trials): H = np.random.randn(order / 2) hmc = AlgebraicHMC(order, energies=H) rf = AlgebraicReducedFlip(order, energies=H) hmc_trials.append(sg(hmc, full)) rf_trials.append(sg(rf, full)) hmc_sg.append(hmc_trials) rf_sg.append(rf_trials) print "order {} took {} seconds".format(order, time.clock() - t_start) hmc_sg = np.array(hmc_sg) rf_sg = np.array(rf_sg) # putting into dataframe for seaborn for idx in xrange(n_trials): hmc_df = pd.DataFrame(dict( Sampler=["Discrete-time HMC"] * len(orders), subj=["subj{}".format(idx)] * len(orders), order=orders, sg=hmc_sg[:,idx]), dtype=np.float) rf_df = pd.DataFrame(dict( Sampler=["Markov Jump HMC"] * len(orders), subj=["subj{}".format(idx)] * len(orders), order=orders, sg=rf_sg[:,idx]), dtype=np.float) sgs.append(hmc_df) sgs.append(rf_df) sgs_df = pd.concat(sgs) print "computation finished. total time elapsed: {}".format(time.clock() - t_begin) sns.tsplot(sgs_df, time="order", unit="subj", condition="Sampler", value="sg") plt.ylabel("Spectral gap (log)") plt.xlabel("Number of states in ladder") plt.yscale('log') plt.title("Spectral gap vs. number of system states") plt.savefig("../../figures/sg_gap_{}_energies_{}_trials.pdf".format( max_n_energies, n_trials))
def result(): form = SubmitForm() # Import csv df = pd.read_csv(form.myFile.data) # Convert registered_at from object to datetime df['registered_at'] = pd.to_datetime(df['registered_at']) # Begin plotting df["count"] = 1 df['registered_at_date'] = pd.DatetimeIndex(df.registered_at).normalize() results = df.set_index('registered_at_date') running_results = results.groupby(pd.TimeGrouper('D'))["count"].count().cumsum() img = StringIO.StringIO() step = pd.Series(range(0,len(running_results)), name="# of days") sns.plt.title(form.title.data) sns.tsplot(running_results, value="Registrants", time=step, color="husl") plt.savefig(img, format='png') img.seek(0) plot_url = base64.b64encode(img.getvalue()) total = df.shape[0] # Begin calculation today = datetime.date.today() registration_open = form.registration_opens.data registration_close = form.registration_closes.data diff = registration_close - today diff_days = diff.days # Create pivot table to see day-to-day details date_pivot_table = df[['registered_at_date', 'count']].groupby(['registered_at_date']).agg(['count']) total = (date_pivot_table.shape[0])+1 days_until_today_dt = today - registration_open days_until_today = days_until_today_dt.days rate = total/days_until_today target = form.target.data diff_target = target - total target_rate = diff_target/diff.days return render_template('result.html', title='Sup yo', form=form, plot_url=plot_url, total=total, rate=rate, diff_target=diff_target, target_rate=target_rate, diff_days=diff_days)
def create_timing_plots(db, instance): dbDf = pd.read_sql_query('select variant, runtime_s as runtime, treewidth, seed' ' from validationresults where instance = "%s"' % instance, db) filename = 'validation-validationset-%s-timings.pdf' % instance.replace('_', '-') if os.path.exists(filename): print('skipping timing plots for %s (file exists: %s)' % (instance, filename)) else: max_treewidth = dbDf.treewidth.max() max_time = dbDf.runtime.max() bins_limit = max_time + 1 logspace_base = 10 logspace_end = math.ceil(math.log(bins_limit) / math.log(logspace_base)) logspace_n_samples = bins_limit / 36 bins = np.array([x - 1 for x in np.logspace(0, logspace_end, num=logspace_n_samples, base=logspace_base) if x < bins_limit] + [bins_limit]) seeds = dbDf.seed.drop_duplicates().values frames = [] for variant in VARIANTS: for seed in seeds: # print('---frame for %s:%s\n%r' % (variant, seed, dbDf.loc[(dbDf.variant == variant) & (dbDf.seed == seed)])) frames.append(normalized_measurements(dbDf.loc[(dbDf.variant == variant) & (dbDf.seed == seed)], bins, start_value=max_treewidth)) runs = pd.concat(frames) with sns.axes_style('ticks'): f, axes = plt.subplots(1, 2, sharey=False, figsize=(5.4, 2.28), dpi=600) # by default the 68% confidence interval is plotted, which corresponds to the standard error of the estimator (ci=68) sns.tsplot(runs, time='runtime', value='treewidth', unit='seed', condition='variant', ax=axes[0]) sns.tsplot(runs, time='runtime', value='treewidth', unit='seed', condition='variant', ax=axes[1]) # axes[0].set_title('solution quality over time, linear time-scale') axes[0].set_xlim([0, max_time]) axes[0].set_xticks(list(range(0, int(max_time+1), 600))) second_largest_treewidth = dbDf.treewidth.drop_duplicates().sort(ascending=False, inplace=False)[2] axes[0].set_ylim([0, second_largest_treewidth]) axes[0].legend().remove() # axes[1].set_title('solution quality over time, logarithmic time-scale') axes[1].set_xscale('log') axes[1].set_xlim([0, max_time]) axes[1].set_ylim([0, max_treewidth * 1.01]) axes[1].legend().set_title(None) sns.despine() f.tight_layout() plt.savefig(filename) plt.clf() plt.close('all') gc.collect() subprocess.check_call(['pdfcrop', '--luatex', '--margins', '0 0 10 0', filename], stdout=subprocess.DEVNULL)
def plot_agent_power(x,time=None): import seaborn as sns if isinstance(x,np.ndarray): P_all=x else: P_all,time = collect_power(x) P_all = downsample(P_all) time = downsample(time) sns.tsplot(P_all,time=time, err_style="unit_traces", err_palette=sns.dark_palette("crimson", len(P_all)), color="k"); plt.xlabel("steps") plt.ylabel("P") return P_all
def demo(): res = False try: logger.info("****** Seaborn demo *******") def random_walk(n, start=0, p_inc=.2): return start + np.cumsum(np.random.uniform(size=n) < p_inc) starts = np.random.choice(range(4), 10) probs = [.1, .3, .5] walks = np.dstack([[random_walk(15, s, p) for s in starts] for p in probs]) print(walks) fig = sns.tsplot(walks) plt.show() except Exception: logger.exception("Seaborn demo failed") raise else: res = True finally: return res
ax1.plot(x_ego_list.T,y_ego_list.T) ax1.set_xlabel('x-position in [m]') ax1.set_ylabel('y-position in [m]') ax1.set_title('Trajectory distribution') ax1.grid() plt.savefig(image_save_path + "trajectory/" + str(num_of_episodes) + "_trajectory_" + str(r_seed) + ".png") plt.show(block=False) plt.clf() #### Add Driving parameter distribution plt.figure(3) ax1 = plt.subplot(3,1,1) sns.tsplot(v_ego_list) #sns.lineplot(x="timestep",y="velocity",data=v_ego_list) ax1.set_xlabel('timestep') ax1.set_ylabel('velocity in [m/s]') ax1.set_title('Velocity') ax2 = plt.subplot(3,1,2) sns.tsplot(y_acc_list) ax2.set_xlabel('timestep') ax2.set_ylabel('acc in [m/s^2]') ax2.set_title('y-acceleration') ax3 = plt.subplot(3,1,3) sns.tsplot(x_acc_list) ax3.set_xlabel('timestep') ax3.set_ylabel('acc in [m/s^2]') ax3.set_title('x-acceleration') plt.tight_layout()
def describe(x, reduce='IncrementalPCA', max_dims=None, show=True, format_data=True): """ Create plot describing covariance with as a function of number of dimensions This function correlates the raw data with reduced data to get a sense for how well the data can be summarized with n dimensions. Useful for evaluating quality of dimensionality reduced plots. Parameters ---------- x : Numpy array, DataFrame or list of arrays/dfs A list of Numpy arrays or Pandas Dataframes reduce : str or dict Decomposition/manifold learning model to use. Models supported: PCA, IncrementalPCA, SparsePCA, MiniBatchSparsePCA, KernelPCA, FastICA, FactorAnalysis, TruncatedSVD, DictionaryLearning, MiniBatchDictionaryLearning, TSNE, Isomap, SpectralEmbedding, LocallyLinearEmbedding, and MDS. Can be passed as a string, but for finer control of the model parameters, pass as a dictionary, e.g. reduce={'model' : 'PCA', 'params' : {'whiten' : True}}. See scikit-learn specific model docs for details on parameters supported for each model. max_dims : int Maximum number of dimensions to consider show : bool Plot the result (default : true) format_data : bool Whether or not to first call the format_data function (default: True). Returns ---------- result : dict A dictionary with the analysis results. 'average' is the correlation by number of components for all data. 'individual' is a list of lists, where each list is a correlation by number of components vector (for each input list). """ warnings.warn('When input data is large, this computation can take a long time.') def summary(x, max_dims=None): # if data is a list, stack it if type(x) is list: x = np.vstack(x) # if max dims is not set, make it the length of the minimum number of columns if max_dims is None: if x.shape[1]>x.shape[0]: max_dims = x.shape[0] else: max_dims = x.shape[1] # correlation matrix for all dimensions alldims = get_cdist(x) corrs=[] for dims in range(2, max_dims): reduced = get_cdist(reducer(x, ndims=dims, reduce=reduce)) corrs.append(get_corr(alldims, reduced)) del reduced return corrs # common format if format_data: x = formatter(x, ppca=True) # a dictionary to store results result = {} result['average'] = summary(x, max_dims) result['individual'] = [summary(x_i, max_dims) for x_i in x] if max_dims is None: max_dims = len(result['average']) # if show, plot it if show: fig, ax = plt.subplots() ax = sns.tsplot(data=result['individual'], time=[i for i in range(2, max_dims+2)], err_style="unit_traces") ax.set_title('Correlation with raw data by number of components') ax.set_ylabel('Correlation') ax.set_xlabel('Number of components') plt.show() return result
# convolve with our kernel for i in range(all_cells.shape[1]): all_cells[:, i] = convolve(all_cells[:, i], kernel, mode='full')[:all_cells.shape[0]] # load activity clusters from file or create if necessary clust_ids = a.cluster_activity(8, all_cells, "cluster_info.hdf5")[0] f_on_data = a.trial_average(all_cells[:, clust_ids == fast_on_like], 3).T s_on_data = a.trial_average(all_cells[:, clust_ids == slow_on_like], 3).T f_off_data = a.trial_average(all_cells[:, clust_ids == fast_off_like], 3).T s_off_data = a.trial_average(all_cells[:, clust_ids == slow_off_like], 3).T trial_time = np.arange(f_on_data.shape[1]) / GlobalDefs.frame_rate # second panel - fish-like on types fig, ax = pl.subplots() sns.tsplot(f_on_data, trial_time, n_boot=1000, color="C3") sns.tsplot(s_on_data, trial_time, n_boot=1000, color="C1") ax.set_xlabel("Time [s]") ax.set_ylabel("Activation [AU]") ax.set_xticks([0, 30, 60, 90, 120, 150]) sns.despine(fig, ax) fig.savefig(save_folder + "fishlike_on_types.pdf", type="pdf") # third panel - fish-like off types fig, ax = pl.subplots() sns.tsplot(f_off_data, trial_time, n_boot=1000, color="C2") sns.tsplot(s_off_data, trial_time, n_boot=1000, color="C0") ax.set_xlabel("Time [s]") ax.set_ylabel("Activation [AU]") ax.set_xticks([0, 30, 60, 90, 120, 150]) sns.despine(fig, ax)
def describe_pca(x, show=True): """ Create plot describing covariance with as a function of number of dimensions This function correlates the raw data with PCA reduced data to get a sense for how well the data can be summarized with n dimensions. Useful for evaluating quality of PCA reduced plots. Parameters ---------- x : Numpy array, DataFrame or list of arrays/dfs A list of Numpy arrays or Pandas Dataframes Returns ---------- fig, ax, attr : maplotlib.Figure, matplotlib.Axes, dict By default, a matplotlib figure and axis handle, and a data dictionary are returned. The dictionary comprises: PCA_summary : dict and average : list. This is a list of the average (over input lists) correlation between the raw data and the dimensionality reduced data. The length is determined by the number of components that explain the most data. Note: the length is typically not as long as the number of features because the PCA model is whitened. If show=False, only attr is returned """ warnings.warn( 'This function is deprecated. Please use the new "describe" function.' ) warnings.warn( 'When input data is large, this computation can take a long time.') ##SUB FUNCTIONS## def PCA_summary(x, max_dims=None): if type(x) is list: x = np.vstack(x) if max_dims is None: max_dims = x.shape[1] cov_alldims = pdist(x, 'correlation') corrs = [] for num in range(2, max_dims): cov_PCA = pdist(np.vstack(reduceD(x, ndims=num, internal=True)), 'correlation') corrs.append(np.corrcoef(cov_alldims, cov_PCA)[0][1]) del cov_PCA return corrs x = format_data(x, ppca=True) attrs = {} attrs['PCA_summary'] = {} attrs['PCA_summary']['average'] = PCA_summary(x, x[0].shape[1]) max_group = np.where(attrs['PCA_summary']['average'] == np.max( attrs['PCA_summary']['average']))[0][0] attrs['PCA_summary']['individual'] = [ PCA_summary(x_i, max_group) for x_i in x ] if show: fig, ax = plt.subplots() ax = sns.tsplot(data=attrs['PCA_summary']['individual'], time=[i for i in range(2, max_group)], err_style="unit_traces") ax.set_title('Correlation with raw data by number of PCA components') ax.set_ylabel('Correlation') ax.set_xlabel('Number of PCA components') plt.show() return fig, ax, attrs else: return attrs
algo_type.extend(["Proposed"]*length) filename += "OPTIMAL-" for i in range(5): temp = readFile(0, filename) avg = np.mean(temp) reward.extend([avg]*length) time.extend(range(0,length)) seed.extend([i]*length) algo_type.extend(["Optimal"]*length) reward = [x/1000 for x in reward] df = pd.DataFrame({"Simulation time (s)": time, "seed": seed, "algo": algo_type, "Reward x 1000": reward}) sns.tsplot(time="Simulation time (s)", value="Reward x 1000", unit="seed", condition="algo", data=df) plt.legend(bbox_to_anchor=(1, 0), loc="lower right", borderaxespad=0.,fontsize=label_size) # for i in range(len(xcoords)): # plt.axvline(x=xcoords[i], c=colors, linewidth=0.5, linestyle='-.') plt.xticks(np.arange(0, 117, 29.3)) loc, labels = plt.xticks() plt.xticks(loc,(0, 60, 120, 180, 240)) plt.axvline(x=29.3, c=colors, linewidth=0.8, linestyle='-.') plt.savefig("figure/ctl1000_0.002_1000_0.02_pkt500_reward.pdf", bbox_inches='tight') # -------------------response time------------------ plt.figure() filename1 = "respTime/ctl1000_0.002_1000_0.02_pkt500/" filename2 = "pktNum/ctl1000_0.002_1000_0.02_pkt500/" sns.set_style("white")
fig7 = plt.figure(7) ax7 = fig7.add_subplot(111, projection='3d') x7, y7, z7 = axes3d.get_test_data() print(axes3d.__file__) ax7.plot_wireframe(x7, y7, z7, rstride=3, cstride=3) ax7.set_xlabel('x axis') ax7.set_ylabel('y axis') ax7.set_zlabel('z axis') plt.show() """ #sns.figure(8) sns.set(style='darkgrid', palette='Set2') """ # create a noisy periodic data set sines = [] rs = np.random.RandomState(8) for _ in range(15): x = np.linspace(0, 30 / 2, 30) y = np.sin(x) + rs.normal(0, 1.5) + rs.normal(0, .3, 30) sines.append(y) # plot th eaverage over replicates with bootstrap resamples sns.tsplot(sines, err_style='boot_traces', n_boot=500) """ x = list(range(1, 1000)) y = [math.log(i, 2) for i in x] z = [math.sin(i / 10) for i in x]
import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt sns.set(color_codes=True) hour, direction = np.meshgrid(np.arange(24), np.arange(1, 3)) df = pd.DataFrame({"hour": hour.flatten(), "direction": direction.flatten()}) df["hourly_avg_count"] = np.random.randint(14, 30, size=len(df)) plt.figure(figsize=(12, 8)) sns.tsplot(df, time='hour', unit="direction", condition='direction', value='hourly_avg_count') plt.show()
gdatas = [datas.groupby('c0').get_group(item) for item in items] pdatas = gdatas[3]['c1'].dropna() n = len(pdatas) # %% Excel export #wb = xw.Book() #for i in range(len(items)): # xw.Range((1, i+1), index=False).value = gdatas[i].loc[:, 1] # xw.Range((1,i+1)).value = gdatas[i][0].unique() # %% seaborn graph plt.figure(1) sn.distplot(pdatas) plt.figure(2) sn.tsplot(pdatas) # %% plotly graph hist_data2 = [pdatas.tolist()] hist_data = [[1.3,1,2,3,5,4,1,2,6,3,5,2]] group_labels = ['distplot'] fig = FF.create_distplot(hist_data2, group_labels,bin_size=0.005) py.offline.plot(fig, filename='Simple Distplot', validate=False) trace = go.Scatter( x = np.linspace(0,n,n+1), y = pdatas ) data = [trace]
def plot_conditions(epochs, conditions=OrderedDict(), ci=97.5, n_boot=1000, title='', palette=None, ylim=(-6, 6), diff_waveform=(1, 2)): """Plot ERP conditions. Args: epochs (mne.epochs): EEG epochs Keyword Args: conditions (OrderedDict): dictionary that contains the names of the conditions to plot as keys, and the list of corresponding marker numbers as value. E.g., conditions = {'Non-target': [0, 1], 'Target': [2, 3, 4]} ci (float): confidence interval in range [0, 100] n_boot (int): number of bootstrap samples title (str): title of the figure palette (list): color palette to use for conditions ylim (tuple): (ymin, ymax) diff_waveform (tuple or None): tuple of ints indicating which conditions to subtract for producing the difference waveform. If None, do not plot a difference waveform Returns: (matplotlib.figure.Figure): figure object (list of matplotlib.axes._subplots.AxesSubplot): list of axes """ if isinstance(conditions, dict): conditions = OrderedDict(conditions) if palette is None: palette = sns.color_palette("hls", len(conditions) + 1) X = epochs.get_data() * 1e6 times = epochs.times y = pd.Series(epochs.events[:, -1]) fig, axes = plt.subplots(2, 2, figsize=[12, 6], sharex=True, sharey=True) axes = [axes[1, 0], axes[0, 0], axes[0, 1], axes[1, 1]] for ch in range(4): for cond, color in zip(conditions.values(), palette): sns.tsplot(X[y.isin(cond), ch], time=times, color=color, n_boot=n_boot, ci=ci, ax=axes[ch]) if diff_waveform: diff = (np.nanmean(X[y == diff_waveform[1], ch], axis=0) - np.nanmean(X[y == diff_waveform[0], ch], axis=0)) axes[ch].plot(times, diff, color='k', lw=1) axes[ch].set_title(epochs.ch_names[ch]) axes[ch].set_ylim(ylim) axes[ch].axvline(x=0, ymin=ylim[0], ymax=ylim[1], color='k', lw=1, label='_nolegend_') axes[0].set_xlabel('Time (s)') axes[0].set_ylabel('Amplitude (uV)') axes[-1].set_xlabel('Time (s)') axes[1].set_ylabel('Amplitude (uV)') if diff_waveform: legend = (['{} - {}'.format(diff_waveform[1], diff_waveform[0])] + list(conditions.keys())) else: legend = conditions.keys() axes[-1].legend(legend) sns.despine() plt.tight_layout() if title: fig.suptitle(title, fontsize=20) return fig, axes
'ytick.color': '.1', 'ytick.major.size': 5.0, 'ytick.minor.size': 5.0, 'grid.color': '.85', 'grid.linestyle': u'-' }) pcolor = ( col_maxsum, #col_ccg_maxsum, #col_ccg_maxsum_k, #col_d_ccg_maxsum_k, #col_s, col_dsa, B[5]) f = sns.tsplot(data=data, time='iter', condition='alg', value='best_cost', unit='instance', color=pcolor, estimator=np.mean, ci=30) #ci=18) f.set(xlabel='Iterations', ylabel='Average Cost') f.legend_.remove() plt.xscale('symlog') #plt.xscale('symlog') #plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.show()
def plot_psth(ephys, stim_data, stim_data_idx_tokeep, channels): length = len(ephys['trial_data'][0]) print 'length,fs = ', length, fs time = np.linspace(-0.5 * length / fs, 0.5 * length / fs, length) #time = downsample(time, 10) #time -= time/2 ## make it -0.5 to +0.5 sec from stim onset. print 'time === ', time print 'shape time ', time.shape #rand_dat = np.random.rand(len(ephys['trial_data']),100) #print 'shape of data = ', ephys['trial_data'].shape #for thing in ephys: #x=2 #print type(thing['trial_data']) tmp = [item['trial_data'] for item in ephys] for item in tmp: print 'shape of item in tmp = ', item.shape ##!!!! should be samples x trials x channels, e.g. (30e3,7,64) data = np.concatenate(tmp, axis=1) #data = rectify(data) #data = downsample(data,10) print 'shape data = ', data.shape ###### call parts of data by the stimulus identity (i.e. grating orientation) or index within stim_data.orientations # get indices of same orientations: ### !!!! stim_data still contains elements that we're getting rid of b/c they didn't coincide with proper times in the ephys. Get rid of these? print 'stim_data_idx_tokeep = ', stim_data_idx_tokeep print 'shape stim_data = ', stim_data.shape #stim_data.orientations = stim_data.orientations[stim_data_idx_tokeep] #stim_data.times = stim_data.times[stim_data_idx_tokeep] tmp_stim_data = stim_data.iloc[stim_data_idx_tokeep] print tmp_stim_data stim_orientation, idx, inv = np.unique(tmp_stim_data.orientations, return_index=True, return_inverse=True) ## inv = elements in stim_data.orientations corresponding to the uniques in stim_orientation ## e.g. stim_data.orientations = [18 162 0 18...] # stim_orientation = [0 18 36 54...] ## inv = [1 9 0 1 ...] #for orientation in stim_orientation: ## take individual orientations ## from data, (shaped [30e3,128] - samples x trials), get trial indices for each orientation and plot those separately # stim_indices.append(idx[i]) print 'stim_orientation,idx,inv = ', stim_orientation, idx, inv #print stim_data.orientations[stim_data.orientations==18].values stim_dict = dict() psth_folder = './psth/' for ch_idx, ch in enumerate(channels): ##! check if directory ./psth exists; if not, create it save_folder = psth_folder + 'channel_' + str(ch + 1) if not os.path.exists(save_folder): os.makedirs(save_folder) ##!! create directory save_dir = ./psth/ch for ori in stim_orientation: stim_dict[str(ori)] = tmp_stim_data.orientations[ tmp_stim_data.orientations == ori].index.values ### put indices of stim_data that correspond to this orientation, from inv print 'Plotting channel %d, orientation %d' % (ch + 1, ori) data_to_plot = downsample( rectify(data[:, stim_dict[str(ori)], ch_idx]), 10).T print 'Shape of plot data = ', data_to_plot.shape plot_data = pd.DataFrame(data=data_to_plot, columns=downsample(time, 10)) #img = mpimg.imread('stinkbug.png') fig = plt.figure(figsize=(20, 10)) gs = gridspec.GridSpec(2, 1, width_ratios=[1, 1]) ax1 = plt.subplot(gs[0]) ax1 = sns.heatmap(plot_data, robust=True, xticklabels=1000, cbar=False) #plt.subplot(gs[1]) ax1.set_title('PSTH for orientation ' + str(ori)) ax2 = sns.set_style("white", {'axes.linewidth': 0.01}) ax2 = plt.subplot(gs[1]) #ax2 = ax1.twinx() ax2 = sns.tsplot(stats.trim_mean(data_to_plot, proportiontocut=0.25, axis=0), time=downsample(time, 10), value='Voltage (uV)', color='black') #plt.show() #fig.savefig((save_folder + '/' + str(ori) +".pdf")) plot_raw_traces(data[:, stim_dict[str(ori)], ch_idx], time, ori, ch, save_folder)
def plot_score_vs_n_evals(xvals, yvals, algo, color): ax = sns.tsplot(yvals, xvals, ci=90, condition=algo, color=color) ax.set_xlim(-xvals[-1] * 0.03, ) plt.xlabel('Number of evaluations', fontsize=14) plt.ylabel('Rewards', fontsize=14) plt.legend(fontsize=14)
sf1 = sf.where(sf['Category'] == dist) #Data is kinda ready after that #sf1 = sf1.where(sf['Category']=='LARCENY/THEFT') #print(sf1.head(20)) print('-----------------------------------------------------') #print(sf1.head(5)) #sf1 = sf1.groupby(['DayOfWeek']) grouped = sf1.groupby(pd.TimeGrouper(freq='60Min')).count() grouped['index1'] = grouped.index.hour pdnum = grouped.as_matrix(['index1', 'Resolution']) time = pdnum[:, 0] values = pdnum[:, 1] ax = sns.tsplot(data=values, time=time, legend=True, color=colors[i], condition=dist) i += 1 plt.legend(bbox_to_anchor=(1.01, 1), loc=2, borderaxespad=0) fig = ax.get_figure() fig.set_size_inches(13, 8) fig.savefig('crimes_hour_all.png') #time = np.array(grouped.idx) #values = np.array(grouped['Resolution']) print(time, values) print(grouped.head(27)) #
from matplotlib.backends.backend_pdf import PdfPages sns.set(style="dark") sns.set_context("poster") df_eegfmri = pd.DataFrame({ 'ID': id_eegfmri, 'block': block_eegfmri, 'trialtype': trialtype_eegfmri, 'reaction time [ms]': rt_eegfmri }) f1, ax = plt.subplots() figfmri = sns.tsplot(data=df_eegfmri, time="block", unit="ID", condition="trialtype", value="reaction time [ms]", err_style="ci_band", ci=60) figfmri.set(xticks=df_eegfmri.block[0::4]) handles, labels = ax.get_legend_handles_labels() ax.legend_.remove() sns.set(style="dark") sns.set_context("poster") df_eeg = pd.DataFrame({ 'ID': id_eeg, 'block': block_eeg, 'trialtype': trialtype_eeg, 'reaction time [ms]': rt_eeg
def arma(self, signal='L_T1'): sensors = self.sensors signals = self.batadal3 test = self.batadaltest # These orders were found using the bruteforce method, and corresponds with the sensors. orders = [ (10, 0, 2), # L_T1 (8, 0, 2), # L_T2 (3, 0, 4), # L_T3 (6, 0, 2), # L_T4 (4, 0, 3), # L_T5 (2, 0, 4), # L_T6 (2, 0, 4), # L_T7 (9, 0, 3), # F_PU1 (11, 0, 3), # F_PU2 (4, 0, 2), # F_PU4 (1, 0, 2), # F_PU6 (4, 0, 3), # F_PU7 (3, 0, 3), # F_PU8 (1, 0, 1), # F_PU10 (5, 0, 3), # F_PU11 (5, 0, 3), # F_V2 (2, 0, 9), # P_J280 (9, 0, 3), # P_J269 (6, 0, 2), # P_J300 (4, 0, 3), # P_J256 (6, 0, 2), # P_J289 (3, 0, 3), # P_J415 (5, 0, 3), # P_J302 (4, 0, 3), # P_J306 (5, 0, 3), # P_J307 (3, 0, 3), # P_J317 (6, 0, 3), # P_J14 (6, 0, 2) # P_J422 ] for i in range(len(sensors)): if sensors[i] != signal: continue signals2 = signals[[sensors[i]]] # Plot signal pyplot.plot(signals2) pyplot.title("Data " + sensors[i]) pyplot.show() # ACF plot_acf(signals2) pyplot.title("autocorrelation " + sensors[i]) pyplot.show() # PACF pyplot.plot(pacf(signals2)) pyplot.title("partial autocorrelation " + sensors[i]) pyplot.show() # Fit model model = ARIMA(signals2, order=orders[i]) model_fit = model.fit(disp=0) #Plot residuals residuals = DataFrame(model_fit.resid) pyplot.title("Residuals " + sensors[i]) sns.tsplot(model_fit.resid) pyplot.show() residuals.plot() pyplot.title("ARMA Fit Residual Error Line Plot " + sensors[i]) pyplot.show() residuals.plot(kind='kde') pyplot.title("ARMA Fit Residual Error Density Plot " + sensors[i]) pyplot.show() print(residuals.describe()) predictions = [] # List of predictions used to plotting the graph. history = signals2[sensors[i]].tolist()[ -100:] # Last 100 entries of the training set. test2 = test[sensors[i]].tolist( ) # Change from Series to list for compatability reasons. for t in range(len(test2)): if t < 5: history.append(test2[t]) continue # Run ARIMA and give prediction predicted_value = 0 # Used when ARIMA fails. try: model = ARIMA(history, order=orders[i]) model_fit = model.fit(disp=0) output = model_fit.forecast() predicted_value = output[0] except: pass predictions.append(predicted_value) # Effectively add one element to the end of the list, and taking one out in the beginning actual_value = test2[t] history.append(actual_value) history = history[-105:] print( str(t) + '/' + str(len(test2)) + ' predicted=%f, expected=%f' % (predicted_value, actual_value)) # Take out the first 5 that was used as history. test2 = test2[5:] error = mean_squared_error(test2, predictions) print('Test MSE: %.3f' % error) # Plot pyplot.title("Predictions for " + str(sensors[i])) pyplot.plot(test2) pyplot.plot(predictions, color='red') pyplot.show() break
def TESS_2018_plot(): ll = np.load('/Users/mskirk/data/PCH Project/Example_LatLon.npy') lats = np.radians(ll[0]) lons = np.radians(ll[1]) test_lons = np.radians(np.arange(0, 360, 0.01)) test3 = np.random.choice(range(len(lats)), 100, replace=False) test4 = np.random.choice(range(len(lats)), 100, replace=False) test5 = np.random.choice(range(len(lats)), 100, replace=False) test6 = np.random.choice(range(len(lats)), 100, replace=False) test7 = np.random.choice(range(len(lats)), 100, replace=False) test8 = np.random.choice(range(len(lats)), 100, replace=False) hole_fit3 = PCH_Tools.trigfit((lons[test3] * u.rad), (lats[test3] * u.rad), degree=7) hole_fit4 = PCH_Tools.trigfit((lons[test4] * u.rad), (lats[test4] * u.rad), degree=7) hole_fit5 = PCH_Tools.trigfit((lons[test5] * u.rad), (lats[test5] * u.rad), degree=7) hole_fit6 = PCH_Tools.trigfit((lons[test6] * u.rad), (lats[test6] * u.rad), degree=7) hole_fit7 = PCH_Tools.trigfit((lons[test7] * u.rad), (lats[test7] * u.rad), degree=7) hole_fit8 = PCH_Tools.trigfit((lons[test8] * u.rad), (lats[test8] * u.rad), degree=7) plt.plot(np.degrees(test_lons), np.sin(hole_fit3['fitfunc'](test_lons)), color='r', linewidth=8) plt.plot(np.degrees(test_lons), np.sin(hole_fit4['fitfunc'](test_lons)), color='m', linewidth=8) plt.plot(np.degrees(test_lons), np.sin(hole_fit5['fitfunc'](test_lons)), color='orange', linewidth=8) plt.plot(np.degrees(test_lons), np.sin(hole_fit6['fitfunc'](test_lons)), color='g', linewidth=8) plt.plot(np.degrees(test_lons), np.sin(hole_fit7['fitfunc'](test_lons)), color='b', linewidth=8) plt.plot(np.degrees(test_lons), np.sin(hole_fit8['fitfunc'](test_lons)), color='c', linewidth=8) plt.plot(np.degrees(lons[test3]), np.sin(lats[test3]), '.', markersize=12, color='r') plt.plot(np.degrees(lons[test4]), np.sin(lats[test4]), '.', markersize=12, color='m') plt.plot(np.degrees(lons[test5]), np.sin(lats[test5]), '.', markersize=12, color='orange') plt.plot(np.degrees(lons[test6]), np.sin(lats[test6]), '.', markersize=12, color='g') plt.plot(np.degrees(lons[test7]), np.sin(lats[test7]), '.', markersize=12, color='b') plt.plot(np.degrees(lons[test8]), np.sin(lats[test8]), '.', markersize=12, color='c') plt.ylim([0.7, 1]) plt.xlim([0, 360]) plt.ylabel('Sine Latitude', fontweight='bold') plt.xlabel('Degrees Longitude', fontweight='bold') fittest = np.concatenate([[np.sin(hole_fit3['fitfunc'](test_lons))], [np.sin(hole_fit4['fitfunc'](test_lons))], [np.sin(hole_fit5['fitfunc'](test_lons))], [np.sin(hole_fit6['fitfunc'](test_lons))], [np.sin(hole_fit7['fitfunc'](test_lons))], [np.sin(hole_fit8['fitfunc'](test_lons))]]) ax = sns.tsplot(data=fittest, time=np.degrees(test_lons), ci=[90, 95, 99], linewidth=8) plt.gcf().clear() # --------------------- testbed = np.concatenate([test3, test4, test5, test6, test7, test8]) ind = np.sort(testbed) test_lats2 = np.sin(lats[ind]) test_lons2 = lons[ind] number_of_bins = 50 bin_median, bin_edges, binnumber = stats.binned_statistic( test_lons2, test_lats2, statistic='median', bins=number_of_bins) a = [np.where(binnumber == ii)[0] for ii in range(1, number_of_bins + 1)] lens = [len(aa) for aa in a] binned_lats = np.zeros([max(lens), number_of_bins]) for jj, ar in enumerate(a): binned_lats[:, jj] = np.nanmean(test_lats2[ar]) binned_lats[0:lens[jj], jj] = test_lats2[ar] ax = sns.tsplot(data=binned_lats, time=np.degrees(bin_edges[0:number_of_bins]), err_style='boot_traces', n_boot=800, color='m')
def plot_conditions( epochs, conditions=OrderedDict(), ci=97.5, n_boot=1000, title="", palette=None, ylim=(-6, 6), diff_waveform=(1, 2), channel_count=4, ): """Plot ERP conditions. Args: epochs (mne.epochs): EEG epochs Keyword Args: conditions (OrderedDict): dictionary that contains the names of the conditions to plot as keys, and the list of corresponding marker numbers as value. E.g., conditions = {'Non-target': [0, 1], 'Target': [2, 3, 4]} ci (float): confidence interval in range [0, 100] n_boot (int): number of bootstrap samples title (str): title of the figure palette (list): color palette to use for conditions ylim (tuple): (ymin, ymax) diff_waveform (tuple or None): tuple of ints indicating which conditions to subtract for producing the difference waveform. If None, do not plot a difference waveform channel_count (int): number of channels to plot. Default set to 4 for backward compatibility with Muse implementations Returns: (matplotlib.figure.Figure): figure object (list of matplotlib.axes._subplots.AxesSubplot): list of axes """ if isinstance(conditions, dict): conditions = OrderedDict(conditions) if palette is None: palette = sns.color_palette("hls", len(conditions) + 1) X = epochs.get_data() * 1e6 times = epochs.times y = pd.Series(epochs.events[:, -1]) midaxis = math.ceil(channel_count / 2) fig, axes = plt.subplots(2, midaxis, figsize=[12, 6], sharex=True, sharey=True) # get individual plot axis plot_axes = [] for axis_y in range(midaxis): for axis_x in range(2): plot_axes.append(axes[axis_x, axis_y]) axes = plot_axes for ch in range(channel_count): for cond, color in zip(conditions.values(), palette): sns.tsplot( X[y.isin(cond), ch], time=times, color=color, n_boot=n_boot, ci=ci, ax=axes[ch], ) if diff_waveform: diff = np.nanmean(X[y == diff_waveform[1], ch], axis=0) - np.nanmean( X[y == diff_waveform[0], ch], axis=0 ) axes[ch].plot(times, diff, color="k", lw=1) axes[ch].set_title(epochs.ch_names[ch]) axes[ch].set_ylim(ylim) axes[ch].axvline( x=0, ymin=ylim[0], ymax=ylim[1], color="k", lw=1, label="_nolegend_" ) axes[0].set_xlabel("Time (s)") axes[0].set_ylabel("Amplitude (uV)") axes[-1].set_xlabel("Time (s)") axes[1].set_ylabel("Amplitude (uV)") if diff_waveform: legend = ["{} - {}".format(diff_waveform[1], diff_waveform[0])] + list( conditions.keys() ) else: legend = conditions.keys() axes[-1].legend( legend, bbox_to_anchor=(1.05, 1), loc="upper left", borderaxespad=0.0 ) sns.despine() plt.tight_layout() if title: fig.suptitle(title, fontsize=20) return fig, axes
# Set the palette colors. sns.set(palette="Set2") # Build the sin wave def sine_wave(n_x, obs_err_sd=1.5, tp_err_sd=.3): x = np.linspace(0, (n_x - 1) / 2, n_x) y = np.sin(x) + np.random.normal(0, obs_err_sd) + np.random.normal( 0, tp_err_sd, n_x) return y sines = np.array([sine_wave(31) for _ in range(20)]) # Generate the Seaborn plot with "ci" bars. ax = sns.tsplot(sines, err_style="ci_bars", interpolate=False) xmin, xmax = ax.get_xlim() x = np.linspace(xmin, xmax, sines.shape[1]) out, _ = optimize.leastsq(lambda p: sines.mean(0) - (np.sin(x / p[1]) + p[0]), (0, 2)) a, b = out xx = np.linspace(xmin, xmax, 100) plt.plot(xx, np.sin(xx / b) + a, c="#444444") plt.title("Seaborn tsplot with CI in bokeh.") output_file("seaborn_errorbar.html", title="seaborn_errorbar.py example") show(mpl.to_bokeh())
cond3 = [[20, 20, 20, 20, 19, 17, 12], [18, 20, 19, 18, 13, 4, 1], [20, 19, 18, 17, 13, 2, 0], [19, 18, 20, 20, 15, 6, 0]] return basecond, cond1, cond2, cond3 if __name__ == '__main__': data = getdata() fig = plt.figure() xdata = np.array([0, 1, 2, 3, 4, 5, 6])/5 linestyle = ['-', '--', ':', '-.'] color = ['r', 'g', 'b', 'k'] label = ['algo1', 'algo2', 'algo3', 'algo4'] for i in range(4): sns.tsplot(time=xdata, data=data[i], color=color[i], linestyle=linestyle[i], condition=label[i]) df = pd.DataFrame(dict(time=np.arange(500), value=np.random.randn(500).cumsum())) # g = sns.relplot(x="time", y="value", kind="line", data=df) # sns.lineplot() plt.ylabel("Success Rate", fontsize=25) plt.xlabel("Iteration Number", fontsize=25) plt.title("Awesome Robot Performance", fontsize=30) plt.legend(loc='bottom left') plt.savefig("/home/zhr/Pictures/img1.png") plt.show()
"Counts": counts }) data_frame_two.append({ "File": item, "Family": family, "Distance": distance, "Counts": counts_two }) import pandas as pd data_for_plot = pd.DataFrame(data_frame_one) data_for_plot_two = pd.DataFrame(data_frame_two) sns.tsplot(time="Distance", condition="Family", value="Counts", unit="File", data=pd.DataFrame(data_frame_one)) sns.plt.savefig(".".join( [args.output_with_prefix, "minFamilyDistance.png"])) sns.plt.close() sns.tsplot(time="Distance", condition="Family", value="Counts", unit="File", data=pd.DataFrame(data_frame_two)) sns.plt.savefig(".".join( [args.output_with_prefix, "minFamilyConsensusDistance.png"])) sns.plt.close() cur_min = float("inf")
sess, path_save + "random_" + str(r_seed) + "_" + "Final.ckpt") print("Model saved in: %s", final_save_path) end = time.time() print("Elapsed time for one cycle: ", end - start) file = open(path_save + 'training_process' + str(r_seed) + '.txt', 'a') file.write("Elapsed Time for one random seed:" + str(end - start) + "\n") plt.figure(4) ax = plt.subplot(1, 1, 1) ax.set_title("Reward over time") ax.set_xlabel("epsisode/100") ax.set_ylabel("reward") ax.grid() sns.tsplot(reward_average) manager = plt.get_current_fig_manager() manager.resize(*manager.window.maxsize()) plt.tight_layout() plt.savefig(path_save + 'reward' + '.png') plt.close() plt.figure(5) ax = plt.subplot(1, 1, 1) ax.set_title("Success time") ax.set_xlabel("epsiode/100") ax.set_ylabel("Finished Episodes/100") ax.grid() sns.tsplot(finished_average) manager = plt.get_current_fig_manager() manager.resize(*manager.window.maxsize())
@author: vivekchari """ import warnings warnings.simplefilter(action='ignore', category = UserWarning) #the tsplot deprecated warnings annoy me :/ import pandas as pd import os import matplotlib.pyplot as plt import seaborn as sns sns.set(context = 'paper') from os import listdir from os.path import isfile, join data_dir = '/Users/vivekchari/Downloads/drugs_and_working_memory-master/WorkingMemory/data/Day 0 to 2400Mon Aug 10 15:33:20 2020' paths = [f for f in listdir(data_dir) if isfile(join(data_dir, f))] sns.color_palette("rocket_r", 4) databases = [] for i in range(len(paths)): databases.append(pd.read_csv(os.path.join(data_dir, paths[i]))) a = databases[0] flatui = ["#9b59b6", "#3498db", "#3F8798 ", "#1FA2BE", "#00BDE5"] try: c = sns.tsplot(time='time', value='correct', unit='trial', condition='day',data = a, ci=95, alpha = .7, color=sns.color_palette()) c.set(xlabel = 'time(s)', ylabel = 'sDRT Accuracy Percentage') c.set(ylim = (.3,1.0)) plt.show(c) except UserWarning: pass
thres = imgf > imgf.mean() # count and label objects labeled, nr_objects = mh.label(thres) # find coordinates for centers of mass com = mh.center_of_mass(thres, labeled) plt.plot(com[:, 1], com[:, 0], 'r.') plt.xlim([0, img.shape[1]]) plt.ylim([img.shape[0], 0]) plt.imshow(img) plt.show() # 4) plot HTTP data epa = pd.read_csv('/Users/brucehao/Google Drive/CUNY/git/DATA602/epa-http.txt', header=None, sep='\s+', error_bad_lines=False) # assign column names epa.columns = ['host', 'date', 'request', 'reply', 'bytes'] # busiest hour in terms of requests # add hour column based on datetime split epa['hour'] = epa['date'].str.split(":").str[1] epa.head() hour = epa.groupby('hour')['hour'].count() sns.tsplot(hour.values, hour.index) plt.ylabel('number of requests') plt.show()
def main(): """ """ fig, axes = plt.subplots(1, 2, figsize=(20, 12)) model = PlasticModel() model.fit() tot_methane = model._final_values mmin = min(tot_methane) mmax = max(tot_methane) mmean = np.mean(tot_methane) mstd = np.std(tot_methane) mmed = np.median(tot_methane) print('min: {0} max:{1} mean:{2} std:{3} med:{4}'.format( mmin, mmax, mmean, mstd, mmed)) years = [year for year in model.methane_production_total.keys()] # make time serie for total methane production evolution ax = axes[0] #[0] seaborn.set(font_scale=1.25) fontsize = 17 matrix = np.asarray( [arr for arr in model.methane_production_total.values()]) sea = seaborn.tsplot( matrix.T, time=years, legend=True, # ci=[0, 100], err_style=[ 'unit_traces', #'ci_bars', # 'ci_band' ], ci='sd', color='#0b2d91', err_palette=['#959699'], n_boot=300, estimator=np.mean, ax=ax) # ax.set_title('Total methane production through plastic', fontsize=fontsize, # fontname='Times New Roman') ax.set_ylim(bottom=-0.005) ax.set_ylabel(r'Tg of CH$_4$ per annum', fontsize=fontsize, fontname='Times New Roman') ax.set_xlabel(r'Year', fontsize=fontsize, fontname='Times New Roman') ax.tick_params(axis='both', labelsize=16) ax = axes[1] #[0] matrix_multiple = np.asarray( [[arr for arr in model.plastic_in_ocean[key].values()] for key in model.plastic_in_ocean]) labels = [label for label in model.plastic_in_ocean.keys()] sea = seaborn.tsplot( data=matrix_multiple.T, time=years, # value='plastic mass', err_style='unit_traces', condition=labels, color=['#2f3fd6', '#e09a0f', '#3dd82f', '#de7bfc'], legend=True, # ci=[68, 95], n_boot=300, ax=ax) ax.legend(fontsize=20) ax.set_ylabel(r'Plastic weight (million of MT)', fontsize=fontsize, fontname='Times New Roman') ax.set_xlabel(r'Year', fontsize=fontsize, fontname='Times New Roman') ax.set_ylim(bottom=-0.05) ax.tick_params(axis='both', labelsize=16) plt.tight_layout() fig.show() input('figure 1') fig, axes = plt.subplots(1, 2, figsize=(20, 12)) # make time serie for annual methane production evolution ax = axes[0] matrix = np.asarray( [arr for arr in model.methane_production_year.values()]) sea = seaborn.tsplot( matrix.T, time=years, legend=True, # ci=[0, 100], err_style=[ 'unit_traces', #'ci_bars', # 'ci_band' ], ci='sd', color='#0b2d91', err_palette=['yellow'], n_boot=300, estimator=np.mean, ax=ax) # ax.set_title('Total methane production through plastic', fontsize=fontsize, # fontname='Times New Roman') ax.set_ylabel(r'Tg of CH$_4$ per annum', fontsize=fontsize, fontname='Times New Roman') ax.set_xlabel(r'Year', fontsize=fontsize, fontname='Times New Roman') # sea = seaborn.kdeplot(matrix.T, model._final_values, # ax=ax, shade=True) # ax.set_title('Influence of plastic removed and the powder conversion to methane') # ax.set_xlabel('Percentage plastic in suspension') # ax.set_ylabel('Percentage of plastic removed from the ocean') ax = axes[1] labels, scores = zip( *sorted(model.features_score.items(), key=lambda x: x[1])) ax.bar(range(len(scores)), scores) ax.set_xticks(np.array(range(len(scores))) + 0.0) ax.set_xticklabels(labels, rotation=90, fontsize=10) ax.set_title('feature score') plt.tight_layout() fig.show() input('figure 2')
def best_conf(mrun, npop, ngen, cxpb, mutpb): if (verbose): print('... Searching best configuration for GAEEs algorithms') vca = DEMO([data_loc, gt_loc, num_endm, 'VCA'], verbose) initPurePixels = vca.ee.extract_endmember()[1] results = { 'GAEE': [], 'GAEE-IVFm': [], 'GAEE-VCA': [], 'GAEE-IVFm-VCA': [] } for i in npop: for j in ngen: for k in cxpb: for l in mutpb: gaee = DEMO([ data_loc, gt_loc, num_endm, 'GAEE', i, j, k, l, None, False ], verbose) ivfm = DEMO([ data_loc, gt_loc, num_endm, 'GAEE-IVFm', i, j, k, l, None, True ], verbose) gaee_vca = DEMO([ data_loc, gt_loc, num_endm, 'GAEE-VCA', i, j, k, l, initPurePixels, False ], verbose) ivfm_vca = DEMO([ data_loc, gt_loc, num_endm, 'GAEE-IVFm-VCA', i, j, k, l, initPurePixels, True ], verbose) algo = [gaee, ivfm, gaee_vca, ivfm_vca] for m in algo: print(i, j, k, l, m.name) m.best_run(mrun) results[m.name].append([[i, j, k, l], m, np.mean(m.sam_values_min)]) algo_bconf = { 'GAEE': [], 'GAEE-IVFm': [], 'GAEE-VCA': [], 'GAEE-IVFm-VCA': [] } aux = results['GAEE'][0][1].gen_max name = [] gen = [] subject = [] value = [] for i in results: aux1 = [j[2] for j in results[i]] indx = np.argmin(aux1) algo_bconf[i].append(results[i][indx]) for l in range(len(aux)): for i in results: k = 0 name.append(i) gen.append(l) value.append(results[i][0][1].gen_max[l][0]) subject.append(k) k += 1 name.append(i) gen.append(l) value.append(results[i][0][1].gen_min[l][0]) subject.append(k) k += 1 d = { 'Algorithms': name, 'Generations': gen, 'subject': subject, 'Log10(volume)': value } df = pd.DataFrame(data=d) plt.figure() ax = sns.tsplot(time="Generations", value="Log10(volume)", unit="subject", condition="Algorithms", data=df) plt.title("GAEEs Convergence") plt.tight_layout() plt.savefig('./IMAGES/Convergence.png', format='png', dpi=200) return algo_bconf
for p in val['logbook']: p['rep'] = i p['Generation'] = p['gen'] p['Maximum Fitness'] = p['max'] p['Encoding'] = 'Denoiser' pages.extend([p for p in val['logbook']]) dfindirect = pd.DataFrame.from_records(pages) sns.set() ax = sns.tsplot(data=dfindirect, time='Generation', unit='rep', condition='Encoding', value='Maximum Fitness', ci=95, linestyle='-') ax = sns.tsplot(data=dfdirect, time='Generation', unit='rep', condition='Encoding', value='Maximum Fitness', ci=95, linestyle=':', color=sns.color_palette()[1]) plt.title("Maximum Fitness by Generation") plt.show()
def analyze_param_results(): n_nodes = 100 iters = 100 all_shortest = 'all' real_degree,real_pc = get_real_degree(0.05) real_degree = np.array(real_degree) real_degree = real_degree[real_degree>0] real_pc = np.array(real_pc) real_pc[np.isnan(real_pc)] = 0.0 df = pd.DataFrame(columns = ['Model','Variable','Q_SP Ratio','Value']) mean_df = pd.DataFrame(columns = ['Model','Variable','Q_SP Ratio','Value']) for q_ratio in np.arange(50,101)*0.01: pc_graphs = np.load('/home/despoB/mb3152/dynamic_mod/results/new_gen_results/rich_club_gen_graphs_both_%s_%s_%s_%s'%(iters,n_nodes,all_shortest,q_ratio)) pc_both = np.load('/home/despoB/mb3152/dynamic_mod/results/new_gen_results/rich_club_gen_pc_both_%s_%s_%s_%s.npy'%(iters,n_nodes,all_shortest,q_ratio)) sp = [] bcsp = [] mod = [] dd_fit = [] pc = [] pc_rc = [] for i,g in enumerate(pc_graphs): vc = g.community_fastgreedy().as_clustering() v = brain_graphs.brain_graph(vc) p = np.array(v.pc) community_matrix = brain_graphs.community_matrix(vc.membership,0) theshort = np.array(g.shortest_paths()) sp.append(np.sum(theshort)) bcsp.append(np.sum(theshort[community_matrix!=1])) mod.append(vc.modularity) pc.append(scipy.stats.entropy(np.histogram(p,10)[0],np.histogram(np.array(real_pc),10)[0])) pc_rc.append(np.nanmean(pc_both[i,int(n_nodes*.85):int(n_nodes*.9)])) dd_fit.append(scipy.stats.entropy(np.histogram(g.degree(),10)[0],np.histogram(np.array(real_degree),10)[0])) mean_df = mean_df.append({'Variable':'Efficiency','Q_SP Ratio':q_ratio,'Value':np.nanmean(sp)*-1},ignore_index=True) mean_df = mean_df.append({'Variable':'Between Community Efficiency','Q_SP Ratio':q_ratio,'Value':np.nanmean(bcsp)*-1},ignore_index=True) mean_df = mean_df.append({'Variable':'Q','Q_SP Ratio':q_ratio,'Value':np.nanmean(mod)},ignore_index=True) mean_df = mean_df.append({'Variable':'Degree Distribution Fit','Q_SP Ratio':q_ratio,'Value':np.nanmean(dd_fit)*-1},ignore_index=True) mean_df = mean_df.append({'Variable':'PC Fit','Q_SP Ratio':q_ratio,'Value':np.nanmean(pc)*-1},ignore_index=True) mean_df = mean_df.append({'Variable':'RCC','Q_SP Ratio':q_ratio,'Value':np.nanmean(pc_rc)},ignore_index=True) for s,i,j,k,l,m,n in zip(range(0,100),sp,bcsp,mod,dd_fit,pc,pc_rc): df = df.append({'Model':s,'Variable':'Efficiency','Q_SP Ratio':q_ratio,'Value':i*-1},ignore_index=True) df = df.append({'Model':s,'Variable':'Between Community Efficiency','Q_SP Ratio':q_ratio,'Value':j*-1},ignore_index=True) df = df.append({'Model':s,'Variable':'Q','Q_SP Ratio':q_ratio,'Value':k},ignore_index=True) df = df.append({'Model':s,'Variable':'Degree Distribution Fit','Q_SP Ratio':q_ratio,'Value':l*-1},ignore_index=True) df = df.append({'Model':s,'Variable':'PC Fit','Q_SP Ratio':q_ratio,'Value':m*-1},ignore_index=True) df = df.append({'Model':s,'Variable':'RCC','Q_SP Ratio':q_ratio,'Value':n},ignore_index=True) df['Value'][df.Variable=='RCC'][np.isfinite(df['Value'][df.Variable=='RCC'])==False] = np.nan df = df.dropna() params = ['Efficiency','Between Community Efficiency','Q','Degree Distribution Fit','PC Fit', 'RCC'] g = sns.FacetGrid(df,col='Variable',sharex=False, sharey=False,col_wrap=3) for param,ax,c in zip(params,g.axes.reshape(-1),sns.color_palette()): d = np.zeros((100,51)) temp_df = df[df.Variable==param].copy() for i, q_ratio in enumerate(np.arange(50,101)*0.01): d[:,i] = temp_df.Value[temp_df['Q_SP Ratio']==q_ratio].values sns.tsplot(d,np.arange(50,101)*0.01,ax=ax,color =c,ci=95) ax.set_title(param) sns.plt.savefig('/home/despoB/mb3152/dynamic_mod/figures/multi_parameter_figure_new.pdf') sns.plt.show() sns.plt.close() # ax.figure.set_size_inches(*args, **kwargs) def normalize(df,col_name,val_name): norm_df = df.copy() for feature_name in np.unique(df['%s'%(col_name)]): norm_df[val_name][norm_df[col_name]==feature_name] = scipy.stats.zscore(df[val_name][df[col_name]==feature_name]) return norm_df norm_df = normalize(df,'Variable','Value') sns.tsplot(data=norm_df,time='Q_SP Ratio',unit='Model',condition='Variable',value='Value') sns.plt.savefig('/home/despoB/mb3152/dynamic_mod/figures/parameter_figure_new.pdf') sns.plt.show()
def plot_data(data, xaxis='Epoch', value="AverageEpRet", condition="Condition1", smooth=1, **kwargs): if smooth > 1: """ smooth data with moving window average. that is, smoothed_y[t] = average(y[t-k], y[t-k+1], ..., y[t+k-1], y[t+k]) where the "smooth" param is width of that window (2k+1) """ y = np.ones(smooth) for datum in data: x = np.asarray(datum[value]) z = np.ones(len(x)) smoothed_x = np.convolve(x, y, 'same') / np.convolve(z, y, 'same') datum[value] = smoothed_x if isinstance(data, list): # for df in data: # df.rename(columns={"EpCost":"Cost","EpRet":"Reward", # "TotalEnvInteracts":"Steps"}, inplace=True) data = pd.concat(data, ignore_index=True) #print(data.head()) data.rename(columns={ "EpCost": "Cost", "EpRet": "Reward", "TotalEnvInteracts": "Steps" }, inplace=True) data.rename(columns={ "AverageEpCost": "Cost", "AverageEpRet": "Reward" }, inplace=True) xaxis = "Steps" if xaxis == "TotalEnvInteracts" else xaxis #print(value) value = "Cost" if value == "EpCost" or value == "AverageEpCost" else value value = "Reward" if value == "EpRet" or value == "AverageEpRet" else value #plt.figure(figsize=(16,8)) sns.set(style="darkgrid", font_scale=1.5) sns.tsplot(data=data, time=xaxis, value=value, unit="Unit", condition=condition, ci='sd', **kwargs) #sns.lineplot(data=data, x=xaxis, y=value, hue=condition, ci='sd', **kwargs) """ If you upgrade to any version of Seaborn greater than 0.8.1, switch from tsplot to lineplot replacing L29 with: sns.lineplot(data=data, x=xaxis, y=value, hue=condition, ci='sd', **kwargs) Changes the colorscheme and the default legend style, though. """ #sns.lineplot(data=data, x=xaxis, y=value, hue=condition, ci='sd', **kwargs) plt.legend(loc='best').set_draggable(True) #plt.legend(loc='upper center', ncol=3, handlelength=1, # borderaxespad=0., prop={'size': 13}) """ For the version of the legend used in the Spinning Up benchmarking page, swap L38 with: plt.legend(loc='upper center', ncol=6, handlelength=1, mode="expand", borderaxespad=0., prop={'size': 13}) """ xscale = np.max(np.asarray(data[xaxis])) > 5e3 if xscale: # Just some formatting niceness: x-axis scale in scientific notation if max x is large plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
import numpy as np import matplotlib.pyplot as plt import seaborn as sns # This i s j u s t a dummy f u n c ti o n t o g e n e r a t e some a r b i t r a r y data d e f g e t d a t a ( ) : b a se c ond = [ [ 1 8 , 2 0 , 1 9 , 1 8 , 1 3 , 4 , 1 ] , [ 2 0 , 1 7 , 1 2 , 9 , 3 , 0 , 0 ] , [ 2 0 , 2 0 , 2 0 , 1 2 , 5 , 3 , 0 ] ] cond1 = [ [ 1 8 , 1 9 , 1 8 , 1 9 , 2 0 , 1 5 , 1 4 ] , [ 1 9 , 2 0 , 1 8 , 1 6 , 2 0 , 1 5 , 9 ] , [ 1 9 , 2 0 , 2 0 , 2 0 , 1 7 , 1 0 , 0 ] , [ 2 0 , 2 0 , 2 0 , 2 0 , 7 , 9 , 1 ] ] cond2= [ [ 2 0 , 2 0 , 2 0 , 2 0 , 1 9 , 1 7 , 4 ] , [ 2 0 , 2 0 , 2 0 , 2 0 , 2 0 , 1 9 , 7 ] , [ 1 9 , 2 0 , 2 0 , 1 9 , 1 9 , 1 5 , 2 ] ] cond3 = [ [ 2 0 , 2 0 , 2 0 , 2 0 , 1 9 , 1 7 , 1 2 ] , [ 1 8 , 2 0 , 1 9 , 1 8 , 1 3 , 4 , 1 ] , [ 2 0 , 1 9 , 1 8 , 1 7 , 1 3 , 2 , 0 ] , [ 1 9 , 1 8 , 2 0 , 2 0 , 1 5 , 6 , 0 ] ] r e t u r n ba se cond , cond1 , cond2 , cond3 # Load the data . r e s u l t s = g e t d a t a ( ) f i g = p l t . f i g u r e ( ) # We w i l l pl o t i t e r a t i o n s 0 . . . 6 xdata = np . a r r a y ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] ) / 5 . # Pl o t each l i n e # (may want t o automate t h i s p a r t e . g . with a l o o p ) . sns.tsplot(time=xdata , data=r e s u l t s [ 0 ] , c o l o r =’ r ’ , l i n e s t y l e =’−’) sns . t s p l o t ( time=xdata , data=r e s u l t s [ 1 ] , c o l o r =’g ’ , l i n e s t y l e =’−−’) sns . t s p l o t ( time=xdata , data=r e s u l t s [ 2 ] , c o l o r =’b ’ , l i n e s t y l e = ’: ’ ) sns . t s p l o t ( time=xdata , data=r e s u l t s [ 3 ] , c o l o r =’k ’ , l i n e s t y l e = ’ −. ’)
def draw_eyetrack_spatial_error_plot(input_file, output_file): global eyetrack # Load the long-form example gammas dataset eyetrack = pd.read_csv(path.join(INPUT_PATH, input_file), sep=';') sns.set_style("whitegrid") colors = sns.color_palette("BrBG", 7) f, ax = plt.subplots(figsize=(8, 3)) ax.xaxis.grid(False) def draw_linear_trendline_for_axis(axis, color): global color_map x_axis = eyetrack[eyetrack.axis == axis] x_axis_time = x_axis.time.values x_axis_values = x_axis.value.values # plot line of generated linear fit slope, intercept, r_value, p_value, std_err = stats.linregress( x_axis_time, x_axis_values) line = slope * x_axis_time + intercept plt.plot(x_axis_time, line, color=color, linewidth=1) draw_linear_trendline_for_axis('x', colors[1]) draw_linear_trendline_for_axis('y', colors[-2]) color_map = { 'x': colors[0], 'y': colors[-1], } # Plot the response with standard error eyetrack_tsplot = sns.tsplot(data=eyetrack, time="time", unit="subject", condition="axis", value="value", ci="sd", color=color_map) # eyetrack_tsplot = sns.regplot(data=eyetrack, time="time", unit="subject", condition="axis", value="value") # customization in design eyetrack_tsplot.set(xticks=[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 ]) # eyetrack_tsplot.set(xlim=(0, 25)) eyetrack_tsplot.set(ylim=(0, 180)) eyetrack_tsplot.set(ylabel='Spatial Error in Pixel') eyetrack_tsplot.set(xlabel='Rest Condition') # remove lines around graph sns.despine(trim=True) plt.tight_layout() # show interactively # plt.title("title") # plt.show(eyetrack_tsplot) # save output as file, in a high resolution fig = eyetrack_tsplot.get_figure() fig.savefig(path.join(OUTPUT_PATH, output_file), dpi=300, transparent=False)