def write_subsetted_biom_table(result_key, data, option_value=None): """Write a string to a file""" if option_value is None: raise IncompetentDeveloperError("Cannot write output without a " "filepath.") if exists(option_value): raise IOError("Output path '%s' already exists." % option_value) table, fmt = data if fmt not in ['hdf5', 'json']: raise IncompetentDeveloperError("Unknown file format") if fmt == 'json': write_list_of_strings(result_key, table, option_value) else: if HAVE_H5PY: import h5py else: # This should never be raised here raise ImportError("h5py is not available, cannot write HDF5!") with h5py.File(option_value, 'w') as f: table.to_hdf5(f, generatedby())
def test_write_list_of_strings(self): """Correctly writes a list of strings to file.""" # can't write without a path self.assertRaises(IncompetentDeveloperError, write_list_of_strings, 'a', ['b', 'c']) write_list_of_strings('foo', ['bar', 'baz'], self.fp) with open(self.fp, 'U') as obs_f: obs = obs_f.read() self.assertEqual(obs, 'bar\nbaz\n')
def write_summarized_results(result_key, data, option_value=None): """Write the benchmark results in a tab-delimited format option_value is the base output directory Writes a file with the benchmark results in a tab-delimited form, with the following headers: label, wall_mean, wall_std, user_mean, user_std, kernel_mean, kernel_std, mem_mean, mem_std Each row contains the results for a single experiment """ if option_value is None: raise IncompetentDeveloperError("Cannot write output without an " "output directory.") if os.path.exists(option_value): if os.path.isfile(option_value): raise IOError( "Output directory '%s' already exists and it is a file." % option_value) else: os.mkdir(option_value) output_fp = os.path.join(option_value, "%s.txt" % result_key) lines = [] headers = [ "#label", "wall_mean", "wall_std", "user_mean", "user_std", "kernel_mean", "kernel_std", "mem_mean", "mem_std" ] lines.append("\t".join(headers)) # Loop over all the experiments for i, label in enumerate(data['label']): values = [str(label)] values.append(str(data['wall_time'][0][i])) values.append(str(data['wall_time'][1][i])) values.append(str(data['cpu_user'][0][i])) values.append(str(data['cpu_user'][1][i])) values.append(str(data['cpu_kernel'][0][i])) values.append(str(data['cpu_kernel'][1][i])) values.append(str(data['memory'][0][i])) values.append(str(data['memory'][1][i])) lines.append("\t".join(values)) write_list_of_strings(result_key, lines, option_value=output_fp)
def write_summarized_results(result_key, data, option_value=None): """Write the benchmark results in a tab-delimited format option_value is the base output directory Writes a file with the benchmark results in a tab-delimited form, with the following headers: label, wall_mean, wall_std, user_mean, user_std, kernel_mean, kernel_std, mem_mean, mem_std Each row contains the results for a single experiment """ if option_value is None: raise IncompetentDeveloperError("Cannot write output without an " "output directory.") if os.path.exists(option_value): if os.path.isfile(option_value): raise IOError("Output directory '%s' already exists and it is a file." % option_value) else: os.mkdir(option_value) output_fp = os.path.join(option_value, "%s.txt" % result_key) lines = [] headers = ["#label", "wall_mean", "wall_std", "user_mean", "user_std", "kernel_mean", "kernel_std", "mem_mean", "mem_std"] lines.append("\t".join(headers)) # Loop over all the experiments for i, label in enumerate(data['label']): values = [str(label)] values.append(str(data['wall_time'][0][i])) values.append(str(data['wall_time'][1][i])) values.append(str(data['cpu_user'][0][i])) values.append(str(data['cpu_user'][1][i])) values.append(str(data['cpu_kernel'][0][i])) values.append(str(data['cpu_kernel'][1][i])) values.append(str(data['memory'][0][i])) values.append(str(data['memory'][1][i])) lines.append("\t".join(values)) write_list_of_strings(result_key, lines, option_value=output_fp)
def write_bench_results(result_key, data, option_value=None): """Output handler for the bench_results_processer command Parameters ---------- result_key : string The key used in the results dictionary data : BenchData namedtuple The results of the command option_value : string Path to the output directory Raises ------ IOError If the output directory exists and it's a file """ # Check that we are not dealing with incompetent developers if option_value is None: raise IncompetentDeveloperError("Cannot write output without an " "output directory.") # Check that the output directory exists if exists(option_value): # Check that it is not a file, so we can use it if isfile(option_value): raise IOError("Output directory '%s' already exists and it is a " "file." % option_value) else: # The output directory does not exists, create it mkdir(option_value) # Write a tab delimited file with a summary of the benchmark results summary_fp = join(option_value, "summarized_results.txt") lines = ["\t".join(["#label", "wall_mean", "wall_std", "user_mean", "user_std", "kernel_mean", "kernel_std", "mem_mean", "mem_std"])] # Loop over all the tests cases for i, label in enumerate(data.labels): lines.append("\t".join([label, str(data.means.wall[i]), str(data.stdevs.wall[i]), str(data.means.user[i]), str(data.stdevs.user[i]), str(data.means.kernel[i]), str(data.stdevs.kernel[i]), str(data.means.mem[i]), str(data.stdevs.mem[i]) ])) write_list_of_strings(result_key, lines, option_value=summary_fp) # Write the polynomials that fit the wall time and memory usage in # human-readable form poly_fp = join(option_value, "curves.txt") lines = ["Wall time fitted curve", generate_poly_label(data.wall_curve.poly, data.wall_curve.deg), "Memory usage fitted curve", generate_poly_label(data.mem_curve.poly, data.mem_curve.deg)] write_list_of_strings(result_key, lines, option_value=poly_fp) # Create plots with benchmark results # Create a plot with the time results time_plot_fp = join(option_value, "time_fig.png") ys = [data.means.wall, data.means.user, data.means.kernel] y_errors = [data.stdevs.wall, data.stdevs.user, data.stdevs.kernel] labels = ['wall', 'user', 'kernel'] make_bench_plot(data.labels, ys, y_errors, labels, "Running time", "Time (seconds)", data.wall_curve.poly, data.wall_curve.deg, time_plot_fp) # Create a plot with the memory results mem_plot_fp = join(option_value, "mem_fig.png") y_errors = [data.stdevs.mem] labels = ['memory'] make_bench_plot(data.labels, ys, y_errors, labels, "Memory usage", "Memory (GB)", data.mem_curve.poly, data.mem_curve.deg, mem_plot_fp, scale=1024*1024)
def write_bench_results(result_key, data, option_value=None): """Output handler for the bench_results_processer command Parameters ---------- result_key : string The key used in the results dictionary data : BenchData namedtuple The results of the command option_value : string Path to the output directory Raises ------ IOError If the output directory exists and it's a file """ # Check that we are not dealing with incompetent developers if option_value is None: raise IncompetentDeveloperError("Cannot write output without an " "output directory.") # Check that the output directory exists if exists(option_value): # Check that it is not a file, so we can use it if isfile(option_value): raise IOError("Output directory '%s' already exists and it is a " "file." % option_value) else: # The output directory does not exists, create it mkdir(option_value) # Write a tab delimited file with a summary of the benchmark results summary_fp = join(option_value, "summarized_results.txt") lines = [ "\t".join([ "#label", "wall_mean", "wall_std", "user_mean", "user_std", "kernel_mean", "kernel_std", "mem_mean", "mem_std" ]) ] # Loop over all the tests cases for i, label in enumerate(data.labels): lines.append("\t".join([ label, str(data.means.wall[i]), str(data.stdevs.wall[i]), str(data.means.user[i]), str(data.stdevs.user[i]), str(data.means.kernel[i]), str(data.stdevs.kernel[i]), str(data.means.mem[i]), str(data.stdevs.mem[i]) ])) write_list_of_strings(result_key, lines, option_value=summary_fp) # Write the polynomials that fit the wall time and memory usage in # human-readable form poly_fp = join(option_value, "curves.txt") lines = [ "Wall time fitted curve", generate_poly_label(data.wall_curve.poly, data.wall_curve.deg), "Memory usage fitted curve", generate_poly_label(data.mem_curve.poly, data.mem_curve.deg) ] write_list_of_strings(result_key, lines, option_value=poly_fp) # Create plots with benchmark results # Create a plot with the time results time_plot_fp = join(option_value, "time_fig.png") ys = [data.means.wall, data.means.user, data.means.kernel] y_errors = [data.stdevs.wall, data.stdevs.user, data.stdevs.kernel] labels = ['wall', 'user', 'kernel'] make_bench_plot(data.labels, ys, y_errors, labels, "Running time", "Time (seconds)", data.wall_curve.poly, data.wall_curve.deg, time_plot_fp) # Create a plot with the memory results mem_plot_fp = join(option_value, "mem_fig.png") y_errors = [data.stdevs.mem] labels = ['memory'] make_bench_plot(data.labels, ys, y_errors, labels, "Memory usage", "Memory (GB)", data.mem_curve.poly, data.mem_curve.deg, mem_plot_fp, scale=1024 * 1024)