def rerank(self, sub_path): conf_xml = sub_path.get_study_conf() script_elems = conf_xml.xpath('/librec-auto/rerank/script') if len(script_elems) > 0: script_elem = script_elems[0] param_spec = create_param_spec(script_elem) script_path = get_script_path(script_elem, 'rerank') study_path = self._files.get_study_path() result_path = sub_path.get_path('result') original_path = self.find_original_results(result_path, script_path, sub_path) ret_val = self.run_script(script_path, sub_path, original_path, param_spec) script_name = script_elem.find('script-name').text if ret_val != 0: self.status = Cmd.STATUS_ERROR raise ScriptFailureException(script_name, f"Reranking script at {str(script_path)} failed.", ret_val) if len(script_elems) > 1: logging.warning( 'More than one re-ranking script found. Ignoring.')
def execute(self, config: ConfigCmd): self._config = config self.status = Cmd.STATUS_INPROC files = config.get_files() target = files.get_study_path() post_path = files.get_post_path() if not post_path.exists(): print('librec-auto: post directory missing. Creating. ', target) os.makedirs(str(post_path)) post_elems = config.get_xml().xpath(self.POST_ELEM_XPATH) for post_elem in post_elems: param_spec = utils.create_param_spec(post_elem) param_spec = self.handle_password(post_elem, config, param_spec) script_path = utils.get_script_path(post_elem, 'post') exec_path = config.get_files().get_study_path() proc_spec = [ sys.executable, script_path.absolute().as_posix(), self._config.get_files().get_config_file_path().name ] + param_spec print(f'librec-auto: Running post-processing script {proc_spec}') subprocess.call(proc_spec, cwd=str(exec_path.absolute()))
def dry_run(self, config): self._files = config.get_files() self._config = config files = config.get_files() if files.get_exp_count() > 0: for i in range(0, files.get_exp_count()): sub_paths = files.get_exp_paths(i) script_elem = single_xpath(sub_paths.get_study_conf(), '/librec-auto/rerank/script') param_spec = create_param_spec(script_elem) script_path = get_script_path(script_elem, 'rerank') ref_path = sub_paths.get_ref_exp_name() result_path = sub_paths.get_path('result') original_path = self.find_original_results(result_path, script_path, sub_paths) print( f'librec-auto (DR): Running re-ranking command {self} for {sub_paths.exp_name}' ) proc_spec = [ sys.executable, script_path.as_posix(), self._config.get_files().get_config_file_path().name, original_path.absolute().as_posix(), sub_paths.get_path('result').absolute().as_posix() ] + param_spec print_process_cli(proc_spec, str(self._config.get_files().get_study_path().absolute()))
def create_proc_spec(self, script_xml): # need to know what command type means script_path = get_script_path(script_xml, 'alg') if script_path: return ['python', script_path] else: return None
def get_metrics(self) -> list: """ Gets a list of the metrics to be evaluated. Structure like: ``` [ { element: <XML element for the metric>, class: <python class for the metric, params: <dict with parameters> }, ... ] ``` """ def get_params(metric_element) -> dict: """ Returns a params dict from the metric_element's params child. """ params_elements = metric_element.xpath('param') if params_elements is None: return {} params = {} for child in params_elements: params[child.get('name')] = child.text print("param name: " + child.text) return params metric_elements = self._config.get_python_metrics() metric_classes = [] for metric_element in metric_elements: script_path = utils.get_script_path(metric_element, 'eval') metric_classes.append({ 'script': script_path, 'params': get_params(metric_element) }) # Not sure we want to support this # if metric_element.find('script-name') != None: # This is a custom metric with a passed script # else: # metric_name = metric_element.find('name').text # metric_classes.append({ # 'element': # metric_element, # 'class': # metric_name_to_class[metric_name], # 'params': # get_params(metric_element) # }) return metric_classes
def dry_run(self, config): self._config = config print(f'librec-auto (DR): Running post command {self}') post_elems = config.get_xml().xpath(self.POST_ELEM_XPATH) for post_elem in post_elems: param_spec = utils.create_param_spec(post_elem) if single_xpath(post_elem, "//param[@name='password']") is not None: param_spec = param_spec + ['--password=<password hidden>'] script_path = utils.get_script_path(post_elem, 'post') print(f'\tPost script: {script_path}') print(f'\tParameters: {param_spec}')
def execute(self, config: ConfigCmd): self._config = config self.status = Cmd.STATUS_INPROC files = config.get_files() StudyStatus(config) target = files.get_study_path() post_path = files.get_post_path() if not post_path.exists(): print('librec-auto: post directory missing. Creating. ', target) os.makedirs(str(post_path)) post_elems = config.get_xml().xpath(self.POST_ELEM_XPATH) for post_elem in post_elems: param_spec = utils.create_param_spec(post_elem) param_spec = self.handle_password(post_elem, config, param_spec) script_path = utils.get_script_path(post_elem, 'post') exec_path = config.get_files().get_study_path() proc_spec = [ sys.executable, script_path.absolute().as_posix(), self._config.get_files().get_config_file_path().name ] + param_spec print(f'librec-auto: Running post-processing script {proc_spec}') # replace with safe_run_subprocess() # subprocess.call(proc_spec, cwd=str(exec_path.absolute())) run_script = safe_run_subprocess(proc_spec, str(exec_path.absolute())) script_name = re.split(r'/', str(script_path))[-1] if run_script != 0: self.status = Cmd.STATUS_ERROR raise ScriptFailureException( script_name, f"Post processing script at {str(script_path)} failed.", run_script) self.status = Cmd.STATUS_COMPLETE
def execute_algorithm(self, sub_path, dry_run=False): conf_xml = sub_path.get_study_conf() script_elems = conf_xml.xpath('/librec-auto/alg/script') # cmd = self.create_proc_spec(script_elems) if len(script_elems) > 0: script_elem = script_elems[0] param_spec = create_param_spec(script_elem) script_path = get_script_path(script_elem, 'alg') cv_data = self.get_all_splits() print( f'librec-auto: Running algorithm script {script_path} for splits in {sub_path.exp_name}' ) for cv_item in cv_data: train = cv_item['train'] test = cv_item['test'] split = cv_item['split'] result_file = sub_path.get_path('result') / f"out-{split}.txt" ret_val = self.run_script(script_path, sub_path, train, test, result_file, param_spec, dry_run=dry_run) if ret_val != 0: self.status = Cmd.STATUS_ERROR script_name = script_elem.find('script-name').text raise ScriptFailureException( script_name, f"Algorithm script at {str(script_path)} failed.", ret_val) else: self.status = Cmd.STATUS_INPROC self.status = Cmd.STATUS_COMPLETE if len(script_elems) > 1: logging.warning('More than one algorithm script found. Ignoring.')
def dry_run(self, config): self._files = config.get_files() self._config = config files = config.get_files() if files.get_exp_count() > 0: for i in range(0, files.get_exp_count()): sub_path = files.get_exp_paths(i) script_elem = single_xpath(sub_path.get_study_conf(), '/librec-auto/rerank/script') param_spec = create_param_spec(script_elem) script_path = get_script_path(script_elem, 'rerank') ref_path = sub_path.get_ref_exp_name() print( f'librec-auto (DR): Running re-ranking command {self} for {sub_path.exp_name}' ) print(f'\tRe-rank script: {script_path}') print(f'\tParameters: {param_spec}') if ref_path: print(f'\tResults from: {ref_path}')
def dry_run(self, config): self._config = config print(f'librec-auto (DR): Running post command {self}') post_elems = config.get_xml().xpath(self.POST_ELEM_XPATH) for post_elem in post_elems: param_spec = utils.create_param_spec(post_elem) if single_xpath(post_elem, "//param[@name='password']") is not None: param_spec = param_spec + ['--password=<password hidden>'] script_path = utils.get_script_path(post_elem, 'post') proc_spec = [ sys.executable, script_path.absolute().as_posix(), self._config.get_files().get_config_file_path().name ] + param_spec print_process_cli( proc_spec, self._config.get_files().get_study_path().absolute)
def rerank(self, sub_path): conf_xml = sub_path.get_study_conf() script_elems = conf_xml.xpath('/librec-auto/rerank/script') if len(script_elems) > 0: script_elem = script_elems[0] param_spec = create_param_spec(script_elem) script_path = get_script_path(script_elem, 'rerank') result_path = sub_path.get_path('result') if not result_path.exists(): print('librec-auto: No results. Cannot rerank ', self._config.get_target()) return if not sub_path.get_ref_exp_name( ): # If there is no ref, then LibRec was run sub_path.results2original() # and there are results here original_path = sub_path.get_path('original') else: # If there is a ref, the results are in ref_sub_path = sub_path.get_ref_exp_path( ) # that experiment's directory original_path = ref_sub_path.get_path('original') print( f'librec-auto: Running re-ranking script {script_path} for {sub_path.exp_name}' ) ret_val = self.run_script(script_path, sub_path, original_path, param_spec) if ret_val != 0: logging.warning('Reranking script failed.') self.status = Cmd.STATUS_ERROR if len(script_elems) > 1: logging.warning( 'More than one re-ranking script found. Ignoring.')