def all_error_time(files): """ Generates the 3 x 1 plot of all wall clock times plots """ fig=plt.figure(dpi=150) ax1 = plt.subplot(311) ax2 = plt.subplot(312) ax3 = plt.subplot(313) #plt.setp(ax2.get_yticklabels()[0], visible=False) ax1.get_shared_x_axes().join(ax1, ax2) ax3.get_shared_x_axes().join(ax1, ax2, ax3) ax1.set_xticklabels([]) ax2.set_xticklabels([]) # ax2.autoscale() ## call autoscale if needed fig.subplots_adjust(hspace=0.1) axes = [ax1, ax2, ax3] for i,f in enumerate(files): df = pd.read_csv(f,header=[0,1]) ax = axes[i] for c in set(df.columns.get_level_values(0)): total_time = 0. if c != 'Exact(SVD)': plot_kwargs = ihs_plot_params[c] iter_time = df[c]['Sketch'] + df[c]['SVD'] + df[c]['Solve'] if c == 'Classical': ax.plot(iter_time.cumsum()[0],df[c,'Coefficient Error'][0],label=c,**plot_kwargs) else: ax.plot(iter_time.cumsum(),df[c,'Coefficient Error'],label=c,**plot_kwargs) # Formatting for all axes for ax in axes: ax.set_ylim(1E-6,1E-3) ax.set_yscale('log',base=10) ax.set_xscale('log',base=2) ax.grid() ax.axvline(x=df['Exact(SVD)', 'SVD'].iloc[0],color='black',linestyle=(0, (5, 1)),label='SVD') ax.set_ylabel('Log Coefficient Error') ax3.set_xlabel('Log (Time (seconds))') # Legend: ax1.legend(loc='upper center', bbox_to_anchor=(0.5, 1.5), fancybox=False, shadow=False, ncol=3,frameon=False) out_fname = 'cal_housing_all_wall_clock_times.tex' tikzplotlib.save(out_fname)
def save(filename, scale_legend=None, show=False, fig=None, plt=None): # assert show == (plt is not None), "Add plt argument in order to show the figure" ext = filename.rsplit('.', 1)[-1] assert ext == "tex", ValueError("Only tex extensions are allowed") current_dir = os.path.dirname(os.path.abspath(__file__)) output_path = os.path.abspath(os.path.join(current_dir, 'Result')) if not os.path.isdir(output_path): os.mkdir(output_path) out = os.path.join(output_path, filename) print(F"Saving to: {out} with extension {ext}") extra_axis_param = [r"width = \linewidth", r"axis lines* = {left}"] if scale_legend: extra_axis_param.extend([ r"legend style={nodes={scale=" + scale_legend + ", transform shape}}" ]) if fig is None: fig = plt.gcf() if show: plt.show() print( r"Replace 'table' with 'table[row sep=\\]' in the tex file. I have opened an issue in matplotlib2tikz; let's hope that this is resolved in a future release" ) print( "If you have still problems, you will probably need to add some newlines manually in the file (because the inline is too long)" ) tikzplotlib.save(out, figure=fig, textsize=8, extra_axis_parameters=extra_axis_param, float_format="{:.5f}", table_row_sep=r"\\")
def tikz_time(func_of_t, time_fs, t_idx, ylabel, filename): xlabel = r'Time in fs' _fig, (ax1) = plt.subplots(1) _lines_exact_E_dir = ax1.plot(time_fs[t_idx], func_of_t[t_idx], marker='') t_lims = (time_fs[t_idx[0]], time_fs[t_idx[-1]]) ax1.grid(True, axis='both', ls='--') ax1.set_xlim(t_lims) ax1.set_xlabel(xlabel) ax1.set_ylabel(ylabel) ax1.legend(loc='upper right') tikzplotlib.save(filename + ".tikz", axis_height='\\figureheight', axis_width='\\figurewidth')
def make_multiplot(x_axis, y_axis, y_legend, ylabel='Not Given', xlabel='Not Given', save_fig=False, figname='Unkown', today_folder=""): for array, legend in zip(y_axis, y_legend): plt.plot(x_axis, array, label=f"Users = {legend}") plt.xlabel(xlabel) plt.ylabel(ylabel) plt.legend() figname = today_folder + figname if save_fig: plt.savefig(F"{figname}.png", dpi=300) tikzplotlib.save(F"{figname}.tex") plt.show()
def ask_plot(self, an): dt = 0.1 t = np.arange(0, 45, dt) p = [] An = [] for i in range(15): for j in range(30): p.append(an[i]) c = np.sin(2 * np.pi * 0.75 * t) plt.plot(t, p) plt.plot(t, p * c) plt.plot(t, -1 * np.array(p)) plt.xlabel('Tiempo [s]') plt.ylabel('Amplitud') plt.title('Señal modulada ASK') tk.save('ask.tikz') plt.show()
def storeAEResult(self, folder, trainDataset, testDataset, algorithm, testName): cwd = os.getcwd() os.chdir(folder) if isinstance(self.result, plt.Figure): plt.savefig(os.path.join(os.getcwd(), str(self.name) + '.png')) tikz.save(os.path.join(os.getcwd(), str(self.name) + '.tex'), encoding='utf-8') plt.close('all') elif isinstance(self.result, pd.DataFrame): self.result.to_csv(os.getcwd() + '/' + str(self.name) + '.csv', header=False) else: print( "Your result is not in the appropriate format, hence it did not get saved" ) os.chdir(cwd)
def save_scatter(ax, file, size): low_x = ax.get_xlim()[0] up_x = ax.get_xlim()[1] low_y = ax.get_ylim()[0] up_y = ax.get_ylim()[1] lower = max(ax.get_xlim()[0],ax.get_ylim()[0]) upper = min(ax.get_xlim()[1],ax.get_ylim()[1]) ax.plot(lower, lower, 'o',c='black',ms=1) ax.plot(upper, upper, 'o',c='black',ms=1) ax.set_axis_off() plt.savefig(file+".png") def bbox(im): a = np.array(im)[:,:,:3] # keep RGB only m = np.any(a != [255, 255, 255], axis=2) coords = np.argwhere(m) y0, x0, y1, x1 = *np.min(coords, axis=0), *np.max(coords, axis=0) return (x0, y0, x1+1, y1+1) im = Image.open(file+".png") im2 = im.crop(bbox(im)) im2.save(file+".png") for el in plt.gca().lines + plt.gca().collections: el.remove() ax.set_xlim(low_x,up_x) ax.set_ylim(low_y,up_y) ax.plot([lower, upper], [lower, upper], ls="--", c=".3") ax.set_axis_on() tikzplotlib.save(file) with open(file, 'r') as f: content = f.read() idx = content.index("\\begin{axis}") content = content[:idx] + "\\node[inner sep=0pt,anchor=south west] at (0,0) {\includegraphics[width="+str(size)+"cm]{"+file+".png}};\n" + content[idx:] idx = content.index("\\begin{axis}[") + len("\\begin{axis}[") content = content[:idx] + "\nwidth="+str(size)+"cm, height="+str(size)+"cm, scale only axis, at={(0,0)}," + content[idx:] with open(file, 'w') as f: f.write(content)
def test_error_vs_iterations_time(df,fname): fig, axes = plt.subplots(dpi=150,ncols=2,gridspec_kw={'wspace':0.0}) iterations = 1 + df.index ax_iters, ax_time = axes opt_test_error = df['Exact(SVD)']['Test Error'][0] # Plotting for iterations for c in set(df.columns.get_level_values(0)): if c != 'Exact(SVD)': plot_kwargs = ihs_plot_params[c] test_err_ratio = np.abs(df[c,'Test Error']/opt_test_error) ax_iters.plot(iterations, test_err_ratio,label=c,**plot_kwargs) # Plotting for time for c in set(df.columns.get_level_values(0)): if c != 'Exact(SVD)': plot_kwargs = ihs_plot_params[c] test_err_ratio = np.abs(df[c,'Test Error']/opt_test_error) iter_time = df[c]['Sketch'] + df[c]['SVD'] + df[c]['Solve'] if c == 'Classical': ax_time.plot(iter_time.cumsum()[0],test_err_ratio[0],label=c,**plot_kwargs) else: ax_time.plot(iter_time.cumsum(),test_err_ratio,label=c,**plot_kwargs) else: ax_time.axvline(x=df['Exact(SVD)', 'SVD'].iloc[0],color='black',linestyle=':',label='SVD') for ax in axes: ax.grid() #ax.set_ylim(0.9,2.1) # format ax_iters ax_iters.set_ylabel('Test Error ratio') ax_iters.set_xlabel('Iterations') # format ax_time ax_time.set_xlabel('Log Time') ax_time.set_yticklabels([]) ax_time.set_xscale('log',base=2) ax_time.legend(loc='upper center', bbox_to_anchor=(0., 1.2), fancybox=False, shadow=False, ncol=3,frameon=False) out_fname = 'cal_housing_ihs/' + fname[:-4] + 'test-error-iters-wall-time.tex' print(out_fname) tikzplotlib.save(out_fname)
def plt_acc_classifiers(): accuracy = pickle.load( open("../report/data/classifier_performance.p", "rb")) names = accuracy.keys() values = list(range(2, 11)) sizes = [(n - 1) * 100 / n for n in values] clrs = sns.color_palette("husl", 2) for i, name in enumerate(names): test = [] testerr = [] train = [] trainerr = [] for nf in values: test.append(np.array(accuracy[name][nf]['test']).mean() * 100) testerr.append(np.array(accuracy[name][nf]['test']).std() * 100) train.append(np.array(accuracy[name][nf]['train']).mean() * 100) trainerr.append(np.array(accuracy[name][nf]['train']).std() * 100) plt.figure() plt.plot(sizes, test, c=clrs[0]) plt.fill_between(sizes, [i - j for i, j in zip(test, testerr)], [i + j for i, j in zip(test, testerr)], alpha=0.3, facecolor=clrs[0]) plt.plot(sizes, train, c=clrs[1]) plt.fill_between(sizes, [i - j for i, j in zip(train, trainerr)], [i + j for i, j in zip(train, trainerr)], alpha=0.3, facecolor=clrs[1]) plt.xlabel("Training set size (\\si{\\percent} of total)") plt.legend(["Test", "Train"]) plt.ylabel("Prediction accuracy (\\si{\percent})") tikzplotlib.save("../report/plots/%s_performance.tikz" % name, axis_width="\\linewidth")
def cpu_gpu_standard(): n_lines = 29 c = np.arange(1, n_lines + 1) norm = mpl.colors.Normalize(vmin=c.min(), vmax=c.max()) cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.jet) cmap.set_array([]) fig, ax = plt.subplots(dpi=100) cpu_df = pd.read_csv("cpu_run_times.csv") cpu_df = pd.DataFrame( cpu_df.groupby(["n_bin", "max_sigma"]).mean().to_records()) gpu_df = pd.read_csv("gpu_standard_run_times_with_img_copy.csv") gpu_df = pd.DataFrame( gpu_df.groupby(["n_bin", "max_sigma"]).mean().to_records()) for i in range(3, 31): cpu = cpu_df[cpu_df["max_sigma"] == i]["time"].values gpu = gpu_df[gpu_df["max_sigma"] == i]["time"].values if len(cpu) == 0 or len(gpu) == 0: continue ax.plot(np.arange(2, 51), cpu, "--", c=cmap.to_rgba(i + 1)) ax.plot(np.arange(2, 51), gpu, c=cmap.to_rgba(i + 1)) # ax.set_yscale('log') ax.set_title("GPU vs. CPU performance") ax.set_xticks([0, 3, 10, 20, 30, 40, 50]) ax.set_ylabel("time (s)") ax.set_xlabel("filters") fig.colorbar(cmap, ticks=c, label="max radius") cpuline = Line2D([0], [0], color="blue", linewidth=2, linestyle="--") gpuline = Line2D([0], [0], color="blue", linewidth=2, linestyle="-") gpu_copy_line = Line2D([0], [0], color="blue", linewidth=2, linestyle=":") fig.legend( [cpuline, gpuline, gpu_copy_line], ["cpu", "gpu"], loc="upper left", bbox_to_anchor=(0.1, 0.9), ) # fig.show() tikzplotlib.save("test.tex", figure=fig)
def postprocessor(BarType): """ Print stresses at Gauss points, plot displacement and stress distributions obtained by FE analysis calling disp_and_stress and by exact solution calling ExactSolution. """ print() print('Print stresses at the Gauss points \n') print('%8s %12s %12s %16s %16s' % ("Element", "x(gauss1)", "x(gauss2)", "stress(gauss1)", "stress(gauss2)")) print( '--------------------------------------------------------------------') fig, (ax1, ax2) = plt.subplots(2, 1) plt.tight_layout() ax1.set_title('FE analysis of 1D bar') ax1.set_ylabel('displacement') ax2.set_xlabel('x') ax2.set_ylabel('stress') # loop over elements to compute the stresses for e in range(model.nel): # compute stresses and displacements for the current element disp_and_stress(e, model.d, ax1, ax2) # plot the exact solution if BarType == "TaperedBar": ExactSolution_TaperedBar(ax1, ax2) elif BarType == "CompressionBar": ExactSolution_CompressionBar(ax1, ax2) elif BarType != None: print('Exact solution for %s is not available' % (BarType)) ax1.legend() ax2.legend() plt.show() # Convert matplotlib figures into PGFPlots figures stored in a Tikz file, # which can be added into your LaTex source code by "\input{fe_plot.tex}" if model.plot_tex == "yes": tikzplotlib.save("fe_plot.tex")
def plot_2d_classification(self, name="Test", colorlist=[]): # initialize first half space PointsH_1 = np.ndarray(shape=(len(self.C_A), self.dimension_E)) counter = 0 for i in self.C_A: PointsH_1[counter] = self.get_point(i) counter += 1 self.C_A = ConvexHull(PointsH_1, 1) # Initialize second half space PointsH_2 = np.ndarray(shape=(len(self.C_B), self.dimension_E)) counter = 0 for i in self.C_B: PointsH_2[counter] = self.get_point(i) counter += 1 self.C_B = ConvexHull(PointsH_2, 1) # Draw convex initial_hulls of disjoint convex sets for simplex in self.C_A.simplices: plt.plot(self.C_A.points[simplex, 0], self.C_A.points[simplex, 1], 'k-') for simplex in self.C_B.simplices: plt.plot(self.C_B.points[simplex, 0], self.C_B.points[simplex, 1], 'k-') x_val_dict = {} y_val_dict = {} if colorlist == []: colorlist = self.colors_E for i, x in enumerate(colorlist, 0): if x not in x_val_dict: x_val_dict[x] = [self.E[i][0]] y_val_dict[x] = [self.E[i][1]] else: x_val_dict[x].append(self.E[i][0]) y_val_dict[x].append(self.E[i][1]) for key, value in x_val_dict.items(): plt.scatter(value, y_val_dict[key], c=key) tikzplotlib.save(name + ".tex") plt.show()
def plot_figure7(filename): """ Plots the figure 7 of the paper. :return: """ kdeamd = read_kdeamd(35, 35, 50) globalkde = read_kde() dmarkov = read_dmarkov(40, 40, 1, SymbolizationType.EQUAL_WIDTH, DivisionOrder.ROWS_THEN_COLUMNS) kalman = read_kalman() nn_positions = read_nn_positions() plot_roc_curves([kdeamd, globalkde, dmarkov, kalman, nn_positions], labels=[ "KDE-AMD", "Global KDE", "$D$-Markov machine", "Kalman filter", "Deep neural network" ]) tikzplotlib.save(filename)
def plot_positivity_ratio_over_attributes(attribute_names, positivity_ratios, filename, save_plot=False): idxs = (-positivity_ratios).argsort() fig, ax = plt.subplots() y_pos = np.arange(len(attribute_names)) + 1 ax.bar(y_pos, positivity_ratios[idxs]) attribute_names = np.array(attribute_names) plt.xticks(y_pos, attribute_names[idxs], rotation=90) plt.ylabel("Positivity ratio") plt.xlabel("Attributes") plt.tight_layout() if save_plot: plt.savefig(filename + ".png", format="png") tikz.save(filename + ".tex") print("Saved positivity ratio by hardness at " + filename) plt.show()
def plot_2d_gaussian(mu, Sigma, size=3.0, interval=0.01, filename="2d_guassian", save=True): rv = mvn(mu, Sigma) x, y = np.mgrid[0 - size:0 + size:interval, 0 - size:0 + size:interval] xy = np.dstack([x, y]) _, ax = plt.subplots() CS = ax.contour(x, y, rv.pdf(xy)) # CS = ax.contourf(x, y, rv.pdf(X)) ax.clabel(CS, inline=0.5, fontsize=8) ax.set_aspect('equal', 'box') if save: tikz.save("./figure/" + filename + ".tex") else: plt.show()
def plot_correction_factor_st(x, expected, predicted, x_label, y_label, unique=False): """Plot estimated and predicted values of a spatio-temporal fitted correction factor""" unique_x = pd.unique(x) if unique else x unique_expected = (np.array(expected)[x.index.get_loc(KEYS.SINGLE_VIDEO)] if unique else expected) unique_predicted = (np.array(predicted)[x.index.get_loc(KEYS.SINGLE_VIDEO)] if unique else predicted) plt.figure() plt.scatter(unique_x, unique_expected[:len(unique_x)]) plt.plot(unique_x, unique_predicted[:len(unique_x)]) plt.xlabel(x_label) plt.ylabel(y_label) tikzplotlib.save(f"{y_label.lower()}.tex") print_metrics_for_plot(expected, predicted)
def main(args): args.recordings = Path(args.recordings).absolute() args.participant_info = Path(args.data_root_path).joinpath( "metadata/participant-info/participant-info.csv.gz").absolute() recordings = pd.read_csv(str(args.recordings)) participant_info = pd.read_csv(str(args.participant_info)) fig, ax = plt.subplots(nrows=3, ncols=1) for i in [1, 2, 3]: recordings.merge(participant_info, on="ParticipantID", how="inner").groupby("Wave").get_group(i).hist( column='HoursRecorded', bins=range(0, 200, 10), ax=ax[i - 1]) ax[i - 1].set_xlim((0, 175)) print(f"Saving to file {args.output}") save(args.output)
def two_plots(fp1, plot_name1, fp2, plot_name2, tikz=False): n_lines = 30 c = np.arange(1, n_lines + 1) norm = mpl.colors.Normalize(vmin=c.min(), vmax=c.max()) cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.jet) cmap.set_array([]) fig, ax = plt.subplots(dpi=100) gpu_standard_df = pd.read_csv(fp1) gpu_standard_df = pd.DataFrame( gpu_standard_df.groupby(["n_bin", "max_sigma"]).mean().to_records()) gpu_df = pd.read_csv(fp2) gpu_df = pd.DataFrame( gpu_df.groupby(["n_bin", "max_sigma"]).mean().to_records()) for i in range(3, 31): gpu_st = gpu_standard_df[gpu_standard_df["max_sigma"] == i]["time"].values gpu = gpu_df[gpu_df["max_sigma"] == i]["time"].values if len(gpu_st) == 0 or len(gpu) == 0: continue ax.plot(np.arange(3, 51), gpu_st[1:], "--", c=cmap.to_rgba(i + 1)) ax.plot(np.arange(3, 51), gpu[1:], c=cmap.to_rgba(i + 1)) ax.set_yscale('log') ax.set_xticks([0, 3, 10, 20, 30, 40, 50]) ax.set_ylabel("time (s)") ax.set_xlabel("filters") fig.colorbar(cmap, ticks=c, label="max radius") cpuline = Line2D([0], [0], color="blue", linewidth=2, linestyle="--") gpuline = Line2D([0], [0], color="blue", linewidth=2, linestyle="-") fig.legend( [cpuline, gpuline], [plot_name1, plot_name2], loc="upper left", bbox_to_anchor=(0.1, 0.9), ) if tikz: tikzplotlib.save(f"{plot_name1}_{plot_name2}.tex", figure=fig) else: fig.show()
def uf_plots(): n_lines = 30 c = np.arange(1, n_lines + 1) norm = mpl.colors.Normalize(vmin=c.min(), vmax=c.max()) cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.jet) cmap.set_array([]) fig, ax = plt.subplots(dpi=100) gpu2 = pd.read_csv( "/Users/maksim/dev_projects/merf/figures/time_results/gpu_model_parallel_fft_run_times_uf_2_gpus.csv" ) gpu3 = pd.read_csv( "/Users/maksim/dev_projects/merf/figures/time_results/gpu_model_parallel_fft_run_times_uf_3_gpus.csv" ) gpu4 = pd.read_csv( "/Users/maksim/dev_projects/merf/figures/time_results/gpu_model_parallel_fft_run_times_uf_4_gpus.csv" ) for i in range(3, 31): gpu22 = gpu2[gpu2["max_sigma"] == i]["time"].values gpu33 = gpu3[gpu3["max_sigma"] == i]["time"].values gpu44 = gpu4[gpu4["max_sigma"] == i]["time"].values ax.plot(np.arange(3, 41), gpu22[1:], "--", c=cmap.to_rgba(i + 1)) ax.plot(np.arange(3, 41), gpu33[1:], "-.", c=cmap.to_rgba(i + 1)) ax.plot(np.arange(3, 41), gpu44[1:], ":", c=cmap.to_rgba(i + 1)) ax.set_xticks([0, 3, 10, 20, 30, 40]) ax.set_yscale('log') ax.set_ylabel("time (s)") ax.set_xlabel("filters") fig.colorbar(cmap, ticks=c, label="max radius") gpu2line = Line2D([0], [0], color="red", linewidth=2, linestyle="--") gpu3line = Line2D([0], [0], color="red", linewidth=2, linestyle="-.") gpu4line = Line2D([0], [0], color="red", linewidth=2, linestyle=":") fig.legend( [gpu2line, gpu3line, gpu4line], ["2", "3", "4"], loc="upper left", bbox_to_anchor=(0.1, 0.9), ) tikzplotlib.save("multiple_gpu.tex")
def blob_detection_compare_plots_gen(): img = imread('img/104_E5R_0.jpg') img_crop = crop_ui(rgb2gray(img)) img_prep = full_prepare(img) blobs_list = compare_detection(img_prep) suffixes = ('LoG', 'DoG', 'DoH') for blobs, suffix in zip(blobs_list, suffixes): _, ax = plt.subplots() plt.title('Liczba wykrytych detali: {}'.format(len(blobs))) plt.imshow(img_crop, cmap=plt.get_cmap('gray')) for blob in blobs: y, x, r = blob c = plt.Circle((x, y), r, color='r', fill=False) ax.add_patch(c) ax.set_axis_off() tikzplotlib.save('exports/blob_detection_compare_' + suffix)
def plot_results_by_algo(self, algo): algo = algo + "_results" fig = plt.figure() ax = fig.add_subplot(111) ax.set_title("Graph: " + self.data + ", Pattern size: " + str(self.tree_pattern_size) + ", Algo: " + self.plot_algo[algo]) for alg, pattern_data in self.results.items(): if alg == algo: for pattern, time_data in pattern_data.items(): data = {} if pattern not in self.pattern_timeout.keys(): exact_number = self.exact_results[pattern][ "embeddings"] for time, value in time_data.items(): if value["size"] == str(self.tree_pattern_size): if int(time) not in data: data[int(time)] = abs( value["embeddings"] - exact_number) / exact_number plt.plot(sorted(data.keys()), [ y for _, y in sorted(zip(data.keys(), data.values())) ], c=self.plot_color[algo], ls="-") # plt.plot(sorted(data.keys()), data.values(), [y for _,y in sorted(zip(data.keys(), data.values()))], "b-") plt.xlabel("Runtime [s]") # plt.xticks([12, 24, 36, 48, 60], ["2%", "4%", "6%", "8%", "10%"]) plt.ylabel("Relative Error") plt.savefig(self.save_name + 'size' + str(self.tree_pattern_size) + "_" + algo + '.png') tikzplotlib.save(self.save_name + 'size' + str(self.tree_pattern_size) + "_" + algo + '.tex', table_row_sep='\\\\') plt.show()
def main(): args = parser.parse_args() fping_df = None if args.fping: fping_df = load_df(args.fping) iperf_df = None if args.iperf: iperf_df = load_df(args.iperf) # delay histograms distribution_type = 'ccdf' fit_distribution = False plotf = plt.loglog plt.figure() if fping_df is not None: plot_distribution( fping_df, label='Ping', plotf=plotf, type=distribution_type, fit_distribution=fit_distribution, ) if iperf_df is not None: bs = [1, 2, 3, 10] for b in bs: g = iperf_df.loc[iperf_df['bin_mb'] == b] print(f'{b} MB: {len(g)} samples') plot_distribution( g, label=f'{b} MB', plotf=plotf, type=distribution_type, fit_distribution=fit_distribution, ) plt.xlim(1e-5, 1) plt.ylim(1e-4, 1) plt.title('Network delay') plt.xlabel('Delay t [s]') plt.ylabel('Pr(delay > t)') plt.grid() plt.legend() tikzplotlib.clean_figure() tikzplotlib.save('ccdf.tex') plt.show() return
def feature_selection_test(n, beta, r, alpha): """Runs test for feature selection methods and compares performance for given parameters""" iter = int(1e4) preds_lfdr = [None] * iter preds_lfdr_easy = [None] * iter preds_mu = [None] * iter a_n = a_value(n, alpha) for i in tqdm(range(iter)): null_samples, alt_samples, true_val = gaussian_mixture(n, beta, r, 4) est_mu_vec = estimate_mu(alt_samples, 3) if len(est_mu_vec[np.nonzero(est_mu_vec)]) != 0: est_mu = np.percentile(est_mu_vec[np.nonzero(est_mu_vec)], 22) #est_mu = np.mean(est_mu_vec[np.nonzero(est_mu_vec)]) predictions_mu = np.zeros(n) predictions_mu[np.nonzero(est_mu_vec)] = 1 easy_eps = len(est_mu_vec[np.nonzero(est_mu_vec)]) / n else: est_mu = 0 easy_eps = 0 predictions_mu = np.zeros(n) est_eps = estimate_epsilon(alt_samples, a_n, est_mu_vec) predictions_lfdr = lfdr(alt_samples, est_eps, est_mu) predictions_easyeps = lfdr(alt_samples, easy_eps, est_mu) preds_lfdr[i] = compute_error(predictions_lfdr, true_val) preds_lfdr_easy[i] = compute_error(predictions_easyeps, true_val) preds_mu[i] = compute_error(predictions_mu, true_val) bins = 100 fig, axs = plt.subplots(3, sharex=True) fig.suptitle( r'Sum of type I and II errors for feature selection, $(\beta,r,n) = $(' + str(beta) + ', ' + str(r) + ', ' + str(n) + ') ') axs[0].hist(preds_lfdr, bins=bins) axs[0].set_title('Lfdr using estimates by Cai et. al') axs[1].hist(preds_lfdr_easy, bins=bins) axs[1].set_title(r'Lfdr using easy estimate of $\varepsilon$') axs[2].hist(preds_mu, bins=bins) axs[2].set_title(r'Selection based on non-zero values of $\mu$') figname = "figures/feature_selection_n_" + str(n) + "_r_" + str(r).replace( '.', '_') + "_beta_" + str(beta).replace('.', '_') + ".tex" tikzplotlib.save(figname) plt.show() return
def main (argv): plt.rcParams["savefig.format"] = 'pdf' #markers=['.','o','v','^','<','>','1','2','3','4','8','s','p','P','*','h','H','+','x','X','D','d','|','_',','] #markers=['v','^','<','>','1','2','3','4','8','s','p','P','*','h','H','+','x','X','D','d','|','_',',','.','o'] markers = ['^', 'o', 's', 'p', 'x', 'd', '+', 'v', '*', '|', '<','>','1','2','3','4','8','h' ] #colors=['b', 'g', 'r', 'c', 'm', 'y', 'k'] colors=['tab:blue', 'tab:red', 'tab:green', 'tab:orange','tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'] plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.margins(x=0,y=0) X = np.array ([]) Y1 = np.array ([]) Y2 = np.array ([]) for i in range(0,1000): val = i/10.0 X = np.append (X, val) Y1 = np.append (Y1, math.cos (math.sqrt(val))) Y2 = np.append (Y2, math.sin (math.sqrt(val))) 'fig = plt.figure (figsize= (10,4)) plt.xlim ([0,100]) plt.plot (X, Y1) plt.plot (X, Y2, linestyle=':') plt.title ('Comparison of two functions $\cos (\sqrt{x})$ and $\sin (\sqrt{x})$') plt.xlabel ('$x$ value') plt.ylabel ('Function value') plt.legend(['$\cos (\sqrt{x})$', '$\sin (\sqrt{x})$']) plt.savefig('FunctionCompare.pdf', bbox_inches='tight') import tikzplotlib out_file_name = 'function_compare.tex' tikzplotlib.save(out_file_name,axis_height='8cm', axis_width='15cm', strict=True) plt.show ()
def stacked_bars(nvidia_ops_means, opencl_ops_means, mango_ops_means, hhal_ops_means): labels = ['NVIDIA', 'OPENCL', 'MANGO'] kernel_execution_means = list( map(to_ms, [nvidia_ops_means[0], opencl_ops_means[0], hhal_ops_means[0]])) buffer_write_means = list( map(to_ms, [nvidia_ops_means[1], opencl_ops_means[1], hhal_ops_means[1]])) buffer_read_means = list( map(to_ms, [nvidia_ops_means[2], opencl_ops_means[2], hhal_ops_means[2]])) width = 0.35 fig, ax = plt.subplots() ax.bar(labels, buffer_read_means, width, bottom=[ kernel_execution_means[i] + buffer_write_means[i] for i in range(len(kernel_execution_means)) ], label='Buffer reads') ax.bar(labels, buffer_write_means, width, bottom=kernel_execution_means, label='Buffer writes') ax.bar(labels, kernel_execution_means, width, label='Kernel executions') ax.set_ylabel('Time (ms)') ax.set_xlabel("Programming Model") ax.set_title('Benchmark breakdown') ax.yaxis.grid(True) # Shrink current axis by 30% box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.70, box.height]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) tikz.save(f'{dest_dir}/breakdown.tex') plt.show()
def show_positivity_over_hardness(filename, attribute_names, labels, predictions, hp_scores, resolution=50, save_plot=False): num_datapoints = labels.shape[0] num_attributes = len(attribute_names) x = np.arange(0, 1, 1 / resolution) y = np.zeros((x.size, num_attributes)) num_select = num_datapoints // resolution num_rest = num_datapoints % resolution sorted_idxs = hp_scores.argsort(0) randomly_selected_idx = np.random.choice(num_datapoints, num_datapoints - num_rest, replace=False) randomly_selected_idx.sort() print("Ignoring {} randomly selected samples to avoid rest. ".format( num_rest)) assert num_select * resolution == randomly_selected_idx.size for att_idx in range(num_attributes): att_idxs = sorted_idxs[:, att_idx][randomly_selected_idx] att_lables = labels[:, att_idx] for i in range(resolution): selected_idx = att_idxs[num_select * i:num_select * (i + 1)] y[i, att_idx] = att_lables[selected_idx].sum(0) / num_select fig, ax = plt.subplots() ax.plot(x, y) #if attribute_name: # title += "; Attribute = " + attribute_name #fig.suptitle(title) plt.xlabel("Rejected quantile sorted by hardness") plt.ylabel("Positive label distribution") #plt.yscale("log") #plt.ylim(0, 1) ax.legend(attribute_names) if save_plot: plt.savefig(filename + ".png", format="png") tikz.save(filename + ".tex") print("Saved positivity ratio by hardness at " + filename) plt.show()
def single_histogram(bibliography, field): if field in bibliography.columns: if field is 'year': names = bibliography[field].value_counts(dropna=False).index values = bibliography[field].value_counts(dropna=False).values fig, ax = plt.subplots() # ax = fig.add_axes([0,0,1,1]) ax.bar(names, values) plt.show() tikzplotlib.save(field + "_hist_.tex") print(bibliography[field].value_counts(dropna=False)) else: names = bibliography[field].value_counts(dropna=False).index names = [str(i) for i in names] values = bibliography[field].value_counts(dropna=False).values fig, ax = plt.subplots() # ax = fig.add_axes([0,0,1,1]) ax.bar(names, values) plt.show() print(bibliography[field].value_counts(dropna=False))
def create_linked_surface(grid_size, width, height, distance, link_length, linkable_length, max_links, figure_size, show_plot=True): if figure_size == 0: return linking_simulation = LinkingSimulation(link_length, linkable_length, width, height, grid_size, max_links) result = linking_simulation.run(distance) Analyzer.report(result) Analyzer.visualization(result) tikzplotlib.save( "./generated/linking_g{}_d{}_x{}_y{}_l{}_c{}_f{}.tex".format( grid_size, distance, width, height, link_length + 2 * linkable_length, max_links, figure_size)) if show_plot: plt.show()
def plot_paac_training(): with open(os.path.join(base_path, 'resources/paac-training'), 'r') as f: paac_data = load_metrics(f) fig = plt.figure() plt.plot(*paac_data['reward'], c='r', linewidth=0.7, label='reward') plt.xlabel('frames') plt.ylabel('return') plt.grid(linestyle='--') #plt.title('PAAC training cummulative reward') #plt.xlim(-, 1.2e7) ax = plt.gca() #plt.xlim(-, 1.2e7) plt.tight_layout() format_axes(ax) plt.savefig(os.path.join(output_path, "paac-reward.pdf")) plt.savefig(os.path.join(output_path, "paac-reward.eps")) tikzplotlib.clean_figure() tikzplotlib.save(os.path.join(output_path, "paac-reward.tex"))
def create_surface_from_drop(grid_size, simulation_area, total_area, volume, concentration, immobilization_probability, figure_size, show_plot=False): grid = HexagonalGrid(grid_size) surface = RandomSurface(grid) surface.distribute_targets_from_liquid_drop(simulation_area, total_area, volume, concentration, immobilization_probability) surface.visualization(figure_size) tikzplotlib.save( "./generated/surface_from_drop_g{}_sa{}_ta{}_v{}_c{}_p{}_f{}.tex". format(grid_size, simulation_area, total_area, volume, concentration, immobilization_probability, figure_size)) if show_plot: plt.show()