def write_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', 'tsv']: raise IncompetentDeveloperError("Unknown file format") if fmt == 'json': with open(option_value, 'w') as f: f.write(table.to_json(generatedby())) elif fmt == 'tsv': with open(option_value, 'w') as f: f.write(table) f.write('\n') else: if HAVE_H5PY: import h5py else: raise ImportError("h5py is not available, cannot write HDF5!") with h5py.File(option_value, 'w') as f: table.to_hdf5(f, generatedby())
def __init__(self, Parameter=None, Type=None, Handler=None, Name=None, Help=None): self.Parameter = Parameter if self.Parameter is None: if Name is None: raise IncompetentDeveloperError("Must specify a Name for the " "InterfaceOption since it " "doesn't have a Parameter.") if Help is None: raise IncompetentDeveloperError("Must specify Help for the " "InterfaceOption since it " "doesn't have a Parameter.") self.Name = Name self.Help = Help else: # Transfer information from Parameter unless overridden here. self.Name = Parameter.Name if Name is None else Name self.Help = Parameter.Description if Help is None else Help # This information is never contained in a Parameter. self.Type = Type self.Handler = Handler
def _validate_inputs_outputs(self, inputs, outputs): """Perform validation on interface IO objects. ``inputs`` will be the output of ``self._get_inputs()``. Subclasses can override to perform validation that requires a list of all input options. Validation that should be performed on a per-option basis should instead go into ``InterfaceOption._validate_option``. ``outputs`` will be the output of ``self._get_outputs()``. Subclasses can override to perform validation that requires a list of all interface results. Validation that should be performed on a per-interface result basis should instead go into ``InterfaceOutputOption._validate_result``. """ param_names = [ input_.getParameterName() for input_ in inputs if input_.getParameterName() is not None ] if len(param_names) != len(set(param_names)): raise IncompetentDeveloperError("Found more than one " "InterfaceOption mapping to the " "same Parameter.") input_names = set([i.Name for i in inputs]) for ifout in outputs: if ifout.InputName is None: continue if ifout.InputName not in input_names: raise IncompetentDeveloperError(\ "Could not link %s to an input!" % ifout.InputName)
def _validate_usage_example(self): if self.ShortDesc is None: raise IncompetentDeveloperError("Must define ShortDesc") if self.LongDesc is None: raise IncompetentDeveloperError("Must define LongDesc") if self.Ex is None: raise IncompetentDeveloperError("Must define Ex")
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 _validate_inputs_outputs(self, inputs, outputs): super(HTMLInterface, self)._validate_inputs_outputs(inputs, outputs) if len(outputs) > 1: raise IncompetentDeveloperError("There can be only one... output") if not (isinstance(outputs[0], HTMLPage) or isinstance(outputs[0], HTMLDownload)): raise IncompetentDeveloperError( "Output must subclass HTMLPage or HTMLDownload")
def _validate_usage_examples(self, usage_examples): super(HTMLInterface, self)._validate_usage_examples(usage_examples) if usage_examples: raise IncompetentDeveloperError( "There shouldn't be usage examples " "associated with this command.")
def _validate_option(self): if self.Type not in self._type_handlers: raise IncompetentDeveloperError( "Unsupported Type in HTMLInputOption: %s" % self.Type) #From optparse's __init__.py, inside class PyqiOption if self.Type == "multiple_choice": if self.Choices is None: raise IncompetentDeveloperError( "must supply a list of Choices for type '%s'" % self.type, self) elif type(self.Choices) not in (types.TupleType, types.ListType): raise IncompetentDeveloperError( "choices must be a list of strings ('%s' supplied)" % str(type(self.Choices)).split("'")[1], self) elif self.Choices is not None: raise IncompetentDeveloperError( "must not supply Choices for type %r" % self.type, self)
def load_file_lines(option_value): """Return a list of strings, one per line in the file. Each line will have leading and trailing whitespace stripped from it. """ if not hasattr(option_value, 'read'): raise IncompetentDeveloperError("Input type must be a file object.") return [line.strip() for line in option_value]
def __init__(self, Parameters): self.Parameters = Parameters for p in self.Parameters: if p.Name in self: raise IncompetentDeveloperError("Found duplicate Parameter " "name '%s'. Parameter names " "must be unique." % p.Name) else: super(ParameterCollection, self).__setitem__(p.Name, p)
def __init__(self, **kwargs): """ """ self.CmdInstance = None if self.CommandConstructor is None: raise IncompetentDeveloperError("Cannot construct an Interface " "without a CommandConstructor.") self.CmdInstance = self.CommandConstructor(**kwargs) self._validate_usage_examples(self._get_usage_examples()) self._validate_inputs_outputs(self._get_inputs(), self._get_outputs())
def write_string(result_key, data, option_value=None): """Write a string to a file. A newline will be added to the end of the file. """ if option_value is None: raise IncompetentDeveloperError("Cannot write output without a " "filepath.") if os.path.exists(option_value): raise IOError("Output path '%s' already exists." % option_value) with open(option_value, 'w') as f: f.write(data) f.write('\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_string_to_dir(result_key, data, option_value=None): """Write a string to a file option_value is the base output directory """ 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) write_string(result_key, data, option_value=output_fp)
def __init__(self, Name, DataType, Description, Required=False, Default=None, DefaultDescription=None, **kwargs): self.Required = Required self.Default = Default self.DefaultDescription = DefaultDescription if Required and Default is not None: raise IncompetentDeveloperError( "Found required CommandIn '%s' " "with default value '%r'. Required CommandIns cannot have " "default values." % (Name, Default)) super(CommandIn, self).__init__(Name, DataType, Description, **kwargs)
def write_comp_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 : CompData 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) # Create the plots with the benchmark comparison time_plot_fp = join(option_value, "time_fig.png") make_comparison_plot(data.x, data.time, "Running time", "Time (seconds)", time_plot_fp) mem_plot_fp = join(option_value, "mem_fig.png") make_comparison_plot(data.x, data.mem, "Memory usage", "Memory (GB)", mem_plot_fp, scale=1024 * 1024)
def __init__(self, Action=None, Required=False, Default=None, ShortName=None, DefaultDescription=None, convert_to_dashed_name=True, **kwargs): super(InterfaceInputOption, self).__init__(**kwargs) self.Required = Required self.Default = Default self.DefaultDescription = DefaultDescription self.ShortName = ShortName self.Action = Action if convert_to_dashed_name: self.Name = self.Name.replace('_', '-') if self.Required and self.Default is not None: raise IncompetentDeveloperError( "Found required option '%s' " "with default value '%s'. Required options cannot have " "default values." % (self.Name, self.Default)) if self.Default is None and self.Parameter is not None: self.Default = self.Parameter.Default self.DefaultDescription = self.Parameter.DefaultDescription # If a parameter is required, the option is always required, but # if a parameter is not required, but the option does require it, # then we make the option required. if self.Parameter is not None: self.Required = self.Parameter.Required if not self.Parameter.Required and Required: self.Required = True self._convert_primitive_strings() self._validate_option()
def write_matplotlib_figure(result_key, data, option_value=None): """Write a matplotlib figure to disk option_value is the base output directory """ 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.png" % result_key) if os.path.exists(output_fp): raise IOError("Output path %s already exists." % output_fp) data.savefig(output_fp)
def __init__(self, Name, DataType, Description, ValidateValue=None): """ ``Name`` should be a valid Python name so that users can supply either a dictionary as input or named arguments. ``DataType`` specifies the type that the input must be. The input should be an instance of type ``DataType``. ``ValidateValue`` can be set as a function that will validate the value associated with a ``Parameter``. """ if not self._is_valid_name(Name): raise IncompetentDeveloperError("Parameter '%s' is not a valid " "Python variable name. Parameter " "names must be alphanumeric and " "start with a letter or " "underscore." % Name) self.Name = Name self.DataType = DataType self.Description = Description self.ValidateValue = ValidateValue
def __init__(self, MIMEType=None, **kwargs): super(HTMLResult, self).__init__(**kwargs) if MIMEType is None: raise IncompetentDeveloperError( "A valid MIMEType must be provided") self.MIMEType = MIMEType
def _the_out_validator(self, out_): """Validate output coming from the command call""" if not isinstance(out_, dict): raise IncompetentDeveloperError("Unsupported result '%r'. Result " "must be a dict." % out_)
def _the_in_validator(self, in_): """Validate input coming from the command line""" if not isinstance(in_, list): raise IncompetentDeveloperError("Unsupported input '%r'. Input " "must be a list." % in_)
def _validate_usage_examples(self, usage_examples): super(OptparseInterface, self)._validate_usage_examples(usage_examples) if len(usage_examples) < 1: raise IncompetentDeveloperError("There are no usage examples " "associated with this command.")
def load_file_contents(option_value): """Return the contents of a file as a single string.""" if not hasattr(option_value, 'read'): raise IncompetentDeveloperError("Input type must be a file object.") return option_value.read()
def _the_in_validator(self, in_): """Validate input coming from the postvars""" if not isinstance(in_, FieldStorage): raise IncompetentDeveloperError("Unsupported input '%r'. Input " "must be FieldStorage." % in_)
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)