def run(self, input_files, input_metadata, output_files): """ The main function to run the compute_metrics tool. :param input_files: List of input files - In this case there are no input files required. :param input_metadata: Matching metadata for each of the files, plus any additional data. :param output_files: List of the output files that are to be generated. :type input_files: dict :type input_metadata: dict :type output_files: dict :return: List of files with a single entry (output_files), List of matching metadata for the returned files (output_metadata). :rtype: dict, dict """ try: # Set and check execution directory. If not exists the directory will be created. execution_path = os.path.abspath( self.configuration.get('execution', '.')) execution_parent_dir = os.path.dirname(execution_path) if not os.path.isdir(execution_parent_dir): os.makedirs(execution_parent_dir) # Update working directory to execution path os.chdir(execution_path) logger.debug("Execution path: {}".format(execution_path)) # Set file names for output files (with random name if not predefined) for key in output_files.keys(): if output_files[key] is not None: pop_output_path = os.path.abspath(output_files[key]) self.populable_outputs[key] = pop_output_path output_files[key] = pop_output_path else: errstr = "The output_file[{}] can not be located. Please specify its expected path.".format( key) logger.error(errstr) raise Exception(errstr) logger.debug( "Init execution of the Machine Learning Model generation") # Prepare file paths for key in input_files.keys(): if key == 'radiomic_features': dataset = input_files[key] elif key == 'ML_technique': ml = input_files[key] else: logger.debug('Unrecognized input file key {}'.format(key)) continue output_metadata = {} for key in output_files.keys(): logger.info('VRE_ML: Iterating over Key {}'.format(key)) if os.path.isfile(output_files[key]): meta = Metadata() meta.file_path = output_files[ key] # Set file_path for output files logger.info( 'VRE_ML: Update metadata with key {} and value {}'. format(key, meta.file_path)) meta.data_type = 'tool_statistics' meta.file_type = 'PDF' # Set sources for output files meta.sources = [output_files[key] + '.pdf'] # Generate model generate_model.run(dataset=dataset, output_files[key] + '.pdf') # Append new element in output metadata logger.info( 'VRE_ML: Update metadata with key {} and value {}'. format(key, meta.file_path)) output_metadata.update({key: meta}) else: logger.warning( "Output {} not found. Path {} not exists".format( key, output_files[key])) logger.debug("Output metadata created") return output_files, output_metadata except Exception: errstr = "VRE ML RUNNER pipeline failed. See logs" logger.fatal(errstr) raise Exception(errstr)
def run(self, input_files, input_metadata, output_files): """ The main function to run the compute_metrics tool. :param input_files: List of input files - In this case there are no input files required. :param input_metadata: Matching metadata for each of the files, plus any additional data. :param output_files: List of the output files that are to be generated. :type input_files: dict :type input_metadata: dict :type output_files: dict :return: List of files with a single entry (output_files), List of matching metadata for the returned files (output_metadata). :rtype: dict, dict """ try: # Set and check execution directory. If not exists the directory will be created. execution_path = os.path.abspath(self.configuration.get('execution', '.')) execution_parent_dir = os.path.dirname(execution_path) if not os.path.isdir(execution_parent_dir): os.makedirs(execution_parent_dir) # Update working directory to execution path os.chdir(execution_path) logger.debug("Execution path: {}".format(execution_path)) logger.debug("Init execution of the Segmentation") # Prepare file paths for key in input_files.keys(): if key == 'model': model = input_files[key] elif key == 'images': datasets = input_files[key] else: logger.debug('Unrecognized key {}'.format(key)) continue # Segment images masks = run(model, datasets, input_metadata['output_folder']) output_files = [] out_meta = [] for _file, _meta in masks: if os.path.isfile(_file): meta = Metadata() meta.file_path = _file # Set file_path for output files meta.data_type = 'image_mask' meta.file_type = 'NIFTI' meta.meta_data = _meta out_meta.append(meta) output_files.append({ 'name': 'image_mask', 'file_path': _file }) else: logger.warning("Output not found. Path \"{}\" does not exist".format(_file)) output_metadata = {'output_files': out_meta} logger.debug("Output metadata created") return output_files, output_metadata except Exception: errstr = "VRE CWL RUNNER pipeline failed. See logs" logger.fatal(errstr) raise Exception(errstr)
def _write_results( # pylint: disable=no-self-use,too-many-arguments input_files, input_metadata, # pylint: disable=unused-argument output_files, output_metadata, json_path): """ Write results.json using information from input_files and output_files: - input_files: dict containing absolute paths of input files - input_metadata: dict containing metadata on input files - output_files: dict containing absolute paths of output files - output_metadata: dict containing metadata on output files Note that values of output_files may be either str or list, according to whether "allow_multiple" is True for the role; in which case, the Tool may have generated multiple output files for that role. Values of output_metadata for roles for which "allow_multiple" is True can be either a list of instances of Metadata, or a single instance. In the former case, the list is assumed to be the same length as that in output_files. In the latter, the same instance of Metadata is used for all outputs for that role. For more information see the schema for results.json. """ results = [] def _newresult(role, path, metadata): result = { "name": role, "type": path[1], "file_path": path[0], "data_type": metadata.data_type, "file_type": metadata.file_type, "compressed": metadata.compressed, "sources": metadata.sources, "meta_data": metadata.meta_data } if result["compressed"] is None: del result["compressed"] if result["sources"] is None: del result["sources"] return result for role, path in (itertools.chain.from_iterable([ itertools.product((k, ), v) for k, v in output_files.items() if v is not None ])): for metadata in output_metadata: name = metadata["name"] if name == role: meta = Metadata() meta.type = path[1] # Set type meta.file_path = path[0] # Set file_path # Set data_type and file_type meta.data_type = metadata["file"].get("data_type", None) meta.file_type = metadata["file"].get("file_type", None) # Set compressed meta.compressed = metadata["file"].get("compressed", None) # Set input sources if meta.data_type == "provenance_data": # execution provenance meta.sources = list( ) # FIXME: this is for RO-Crate hack else: # Set input sources meta_sources_list = list() for input_name in input_metadata.keys(): meta_sources_list.append( input_metadata[input_name][1].file_path) meta.sources = meta_sources_list # Set metadata meta.meta_data = metadata["file"].get("meta_data", None) results.append(_newresult(role, path, meta)) # Create JSON output_metadata file json.dump({"output_files": results}, open(json_path, 'w'), indent=2, separators=(',', ': ')) if os.path.isfile( json_path ): # check if results was created, if not execution stops return True else: return False