From 8272698e6114106c1f44cfcf7ed85c92ba50d13a Mon Sep 17 00:00:00 2001 From: Jonathan de Bruin Date: Sun, 18 Jun 2023 12:12:04 +0200 Subject: [PATCH] Improve error messages and formatting --- asreviewcontrib/makita/entrypoint.py | 12 ++++--- asreviewcontrib/makita/template_arfi.py | 12 ++----- asreviewcontrib/makita/template_basic.py | 10 ++---- .../makita/template_multiple_models.py | 6 ++-- asreviewcontrib/makita/utils.py | 4 +-- .../scripts/merge_descriptives.py | 13 ++++---- .../arfi_example/scripts/merge_metrics.py | 26 +++++++--------- examples/arfi_example/scripts/merge_tds.py | 31 ++++++++++--------- .../scripts/merge_descriptives.py | 13 ++++---- .../basic_example/scripts/merge_metrics.py | 26 +++++++--------- examples/basic_example/scripts/merge_tds.py | 31 ++++++++++--------- .../scripts/merge_descriptives.py | 13 ++++---- .../scripts/merge_metrics.py | 26 +++++++--------- .../scripts/merge_tds.py | 31 ++++++++++--------- 14 files changed, 126 insertions(+), 128 deletions(-) diff --git a/asreviewcontrib/makita/entrypoint.py b/asreviewcontrib/makita/entrypoint.py index 4d3f54b..af7b930 100644 --- a/asreviewcontrib/makita/entrypoint.py +++ b/asreviewcontrib/makita/entrypoint.py @@ -16,7 +16,7 @@ def get_template_fp(name): def is_valid_template(fp): - if Path(fp).is_file(): + if fp and Path(fp).is_file(): return True else: raise ValueError(f"Template {fp} not found") @@ -53,9 +53,11 @@ def execute(self, argv): # noqa: C901 self._template(args_name, args_program) except Exception as err: print(f"\u001b[31mERROR: {err}\u001b[0m") - elif args_program.tool == "add-script": - self._add_script(args_name, args_program) + try: + self._add_script(args_name, args_program) + except Exception as err: + print(f"\u001b[31mERROR: {err}\u001b[0m") else: parser = _parse_arguments_program(self.version, add_help=True) parser.parse_args(argv) @@ -98,7 +100,9 @@ def _template(self, args_name, args_program): args = parser.parse_args(args_name) # check if a custom template is used, otherwise use the default template - fp_template = args.template or get_template_fp(args_template.name) + fp_template = args.template or ( + args_template.name and get_template_fp(args_template.name) + ) is_valid_template(fp_template) # load datasets diff --git a/asreviewcontrib/makita/template_arfi.py b/asreviewcontrib/makita/template_arfi.py index 0133d56..f9b5009 100644 --- a/asreviewcontrib/makita/template_arfi.py +++ b/asreviewcontrib/makita/template_arfi.py @@ -53,7 +53,7 @@ def render_jobs_arfi( } ) - # Instantiate a ConfigTemplate object, initializing a Jinja2 environment and + # Instantiate a ConfigTemplate object, initializing a Jinja2 environment and # setting up template variables and extensions. template = ConfigTemplate(fp_template) @@ -61,9 +61,7 @@ def render_jobs_arfi( if template.scripts is not None: for s in template.scripts: t_script = file_handler.render_file_from_template( - s, - "script", - output_folder=output_folder + s, "script", output_folder=output_folder ) export_fp = Path(scripts_folder, s) file_handler.add_file(t_script, export_fp) @@ -75,9 +73,7 @@ def render_jobs_arfi( s, "doc", datasets=datasets, - template_name=template.name - if template.name == "ARFI" - else "custom", + template_name=template.name if template.name == "ARFI" else "custom", template_name_long=template.name_long, template_scripts=template.scripts, output_folder=output_folder, @@ -101,7 +97,6 @@ def render_jobs_arfi( ) - def _get_priors(dataset, init_seed, n_priors): """Sample priors.""" asdata = ASReviewData.from_file(dataset) @@ -127,4 +122,3 @@ def _get_priors(dataset, init_seed, n_priors): priors.append(list(map(str, priors_list))) return priors - diff --git a/asreviewcontrib/makita/template_basic.py b/asreviewcontrib/makita/template_basic.py index dfb61ea..5c2c34b 100644 --- a/asreviewcontrib/makita/template_basic.py +++ b/asreviewcontrib/makita/template_basic.py @@ -51,7 +51,7 @@ def render_jobs_basic( } ) - # Instantiate a ConfigTemplate object, initializing a Jinja2 environment and + # Instantiate a ConfigTemplate object, initializing a Jinja2 environment and # setting up template variables and extensions. template = ConfigTemplate(fp_template) @@ -59,9 +59,7 @@ def render_jobs_basic( if template.scripts is not None: for s in template.scripts: t_script = file_handler.render_file_from_template( - s, - "script", - output_folder=output_folder + s, "script", output_folder=output_folder ) export_fp = Path(scripts_folder, s) file_handler.add_file(t_script, export_fp) @@ -73,9 +71,7 @@ def render_jobs_basic( s, "doc", datasets=datasets, - template_name=template.name - if template.name == "basic" - else "custom", + template_name=template.name if template.name == "basic" else "custom", template_name_long=template.name_long, template_scripts=template.scripts, output_folder=output_folder, diff --git a/asreviewcontrib/makita/template_multiple_models.py b/asreviewcontrib/makita/template_multiple_models.py index 1d82554..aad5a53 100644 --- a/asreviewcontrib/makita/template_multiple_models.py +++ b/asreviewcontrib/makita/template_multiple_models.py @@ -62,7 +62,7 @@ def render_jobs_multiple_models( } ) - # Instantiate a ConfigTemplate object, initializing a Jinja2 environment and + # Instantiate a ConfigTemplate object, initializing a Jinja2 environment and # setting up template variables and extensions. template = ConfigTemplate(fp_template) @@ -70,9 +70,7 @@ def render_jobs_multiple_models( if template.scripts is not None: for s in template.scripts: t_script = file_handler.render_file_from_template( - s, - "script", - output_folder=output_folder + s, "script", output_folder=output_folder ) export_fp = Path(scripts_folder, s) file_handler.add_file(t_script, export_fp) diff --git a/asreviewcontrib/makita/utils.py b/asreviewcontrib/makita/utils.py index d111bd9..4169248 100644 --- a/asreviewcontrib/makita/utils.py +++ b/asreviewcontrib/makita/utils.py @@ -8,7 +8,7 @@ class FileHandler: """ - The FileHandler class handles file operations such as adding files and rendering + The FileHandler class handles file operations such as adding files and rendering scripts. """ @@ -22,7 +22,7 @@ def add_file(self, content, export_fp): Args: content (str): The content to be written into the file. - export_fp (Path): A Path object that specifies the directory where the file + export_fp (Path): A Path object that specifies the directory where the file should be added. """ diff --git a/examples/arfi_example/scripts/merge_descriptives.py b/examples/arfi_example/scripts/merge_descriptives.py index 2713cb1..72e1390 100644 --- a/examples/arfi_example/scripts/merge_descriptives.py +++ b/examples/arfi_example/scripts/merge_descriptives.py @@ -35,10 +35,10 @@ def create_table_descriptives(datasets): for ds in datasets: with open(ds) as f: - data = json.load(f)['data']['items'] + data = json.load(f)["data"]["items"] values = {} for item in data: - values[item['id']] = item['value'] + values[item["id"]] = item["value"] stats.append(values) df = pd.DataFrame(stats, index=[Path(ds).name for ds in datasets]) @@ -46,7 +46,6 @@ def create_table_descriptives(datasets): if __name__ == "__main__": - parser = argparse.ArgumentParser( description="Merge descriptives of multiple files into single table." ) @@ -54,12 +53,14 @@ def create_table_descriptives(datasets): "-s", type=str, default="output/simulation/*/descriptives/", - help="Datasets location") + help="Datasets location", + ) parser.add_argument( "-o", type=str, default="output/tables/data_descriptives_all.csv", - help="Output table location") + help="Output table location", + ) args = parser.parse_args() # load datasets @@ -75,4 +76,4 @@ def create_table_descriptives(datasets): # store result in output folder Path(args.o).parent.mkdir(parents=True, exist_ok=True) result.to_csv(Path(args.o)) - result.to_excel(Path(args.o).with_suffix('.xlsx')) + result.to_excel(Path(args.o).with_suffix(".xlsx")) diff --git a/examples/arfi_example/scripts/merge_metrics.py b/examples/arfi_example/scripts/merge_metrics.py index e50f62d..aa03146 100644 --- a/examples/arfi_example/scripts/merge_metrics.py +++ b/examples/arfi_example/scripts/merge_metrics.py @@ -33,38 +33,36 @@ def create_table_state_metrics(metric_files): for metric in metric_files: with open(metric) as f: - data = json.load(f)['data']['items'] + data = json.load(f)["data"]["items"] values = {} - values['file_name'] = Path(metric).name + values["file_name"] = Path(metric).name for item in data: - if item['id'] == 'td': + if item["id"] == "td": continue # check if value is a list - if item['value'] is not None and isinstance(item['value'], list): - for value in item['value']: - values[item['id'] + "_" + str(value[0])] = value[1] + if item["value"] is not None and isinstance(item["value"], list): + for value in item["value"]: + values[item["id"] + "_" + str(value[0])] = value[1] else: - values[item['id']] = item['value'] + values[item["id"]] = item["value"] metrics.append(values) return pd.DataFrame(metrics) if __name__ == "__main__": - parser = argparse.ArgumentParser( description="Merge metrics of multiple states into single table." ) parser.add_argument( - "-s", - type=str, - default="output/simulation/*/metrics/", - help="states location") + "-s", type=str, default="output/simulation/*/metrics/", help="states location" + ) parser.add_argument( "-o", type=str, default="output/tables/metrics_sim_all.csv", - help="Output table location") + help="Output table location", + ) args = parser.parse_args() # load metric files @@ -80,4 +78,4 @@ def create_table_state_metrics(metric_files): # store result in output folder Path(args.o).parent.mkdir(parents=True, exist_ok=True) result.to_csv(Path(args.o)) - result.to_excel(Path(args.o).with_suffix('.xlsx')) + result.to_excel(Path(args.o).with_suffix(".xlsx")) diff --git a/examples/arfi_example/scripts/merge_tds.py b/examples/arfi_example/scripts/merge_tds.py index fc4d851..1beb52c 100644 --- a/examples/arfi_example/scripts/merge_tds.py +++ b/examples/arfi_example/scripts/merge_tds.py @@ -35,17 +35,21 @@ def create_table_state_tds(metrics): for metric in metrics: with open(metric) as f: - i = next(filter(lambda x: x['id'] == 'td', json.load(f)['data']['items']))['value'] # noqa + i = next(filter(lambda x: x["id"] == "td", json.load(f)["data"]["items"]))[ + "value" + ] # noqa values.extend((item[0], item[1], file_counter) for item in i) file_counter += 1 - df = pd.DataFrame(values, columns=['record_id', 'td', 'metric_file']) - pivoted = df.pivot_table(index='record_id', - columns='metric_file', - values='td', - aggfunc='first', - fill_value=0) - pivoted.columns = [f'td_sim_{col}' for col in pivoted.columns] + df = pd.DataFrame(values, columns=["record_id", "td", "metric_file"]) + pivoted = df.pivot_table( + index="record_id", + columns="metric_file", + values="td", + aggfunc="first", + fill_value=0, + ) + pivoted.columns = [f"td_sim_{col}" for col in pivoted.columns] return pivoted @@ -54,15 +58,14 @@ def create_table_state_tds(metrics): description="Merge tds of multiple metrics into single table." ) parser.add_argument( - "-s", - type=str, - default="output/simulation/*/metrics/", - help="metrics location") + "-s", type=str, default="output/simulation/*/metrics/", help="metrics location" + ) parser.add_argument( "-o", type=str, default="output/tables/tds_sim_all.csv", - help="Output table location") + help="Output table location", + ) args = parser.parse_args() # load metric files @@ -77,4 +80,4 @@ def create_table_state_tds(metrics): # store table Path(args.o).parent.mkdir(parents=True, exist_ok=True) states_table.to_csv(Path(args.o)) - states_table.to_excel(Path(args.o).with_suffix('.xlsx')) + states_table.to_excel(Path(args.o).with_suffix(".xlsx")) diff --git a/examples/basic_example/scripts/merge_descriptives.py b/examples/basic_example/scripts/merge_descriptives.py index 2713cb1..72e1390 100644 --- a/examples/basic_example/scripts/merge_descriptives.py +++ b/examples/basic_example/scripts/merge_descriptives.py @@ -35,10 +35,10 @@ def create_table_descriptives(datasets): for ds in datasets: with open(ds) as f: - data = json.load(f)['data']['items'] + data = json.load(f)["data"]["items"] values = {} for item in data: - values[item['id']] = item['value'] + values[item["id"]] = item["value"] stats.append(values) df = pd.DataFrame(stats, index=[Path(ds).name for ds in datasets]) @@ -46,7 +46,6 @@ def create_table_descriptives(datasets): if __name__ == "__main__": - parser = argparse.ArgumentParser( description="Merge descriptives of multiple files into single table." ) @@ -54,12 +53,14 @@ def create_table_descriptives(datasets): "-s", type=str, default="output/simulation/*/descriptives/", - help="Datasets location") + help="Datasets location", + ) parser.add_argument( "-o", type=str, default="output/tables/data_descriptives_all.csv", - help="Output table location") + help="Output table location", + ) args = parser.parse_args() # load datasets @@ -75,4 +76,4 @@ def create_table_descriptives(datasets): # store result in output folder Path(args.o).parent.mkdir(parents=True, exist_ok=True) result.to_csv(Path(args.o)) - result.to_excel(Path(args.o).with_suffix('.xlsx')) + result.to_excel(Path(args.o).with_suffix(".xlsx")) diff --git a/examples/basic_example/scripts/merge_metrics.py b/examples/basic_example/scripts/merge_metrics.py index e50f62d..aa03146 100644 --- a/examples/basic_example/scripts/merge_metrics.py +++ b/examples/basic_example/scripts/merge_metrics.py @@ -33,38 +33,36 @@ def create_table_state_metrics(metric_files): for metric in metric_files: with open(metric) as f: - data = json.load(f)['data']['items'] + data = json.load(f)["data"]["items"] values = {} - values['file_name'] = Path(metric).name + values["file_name"] = Path(metric).name for item in data: - if item['id'] == 'td': + if item["id"] == "td": continue # check if value is a list - if item['value'] is not None and isinstance(item['value'], list): - for value in item['value']: - values[item['id'] + "_" + str(value[0])] = value[1] + if item["value"] is not None and isinstance(item["value"], list): + for value in item["value"]: + values[item["id"] + "_" + str(value[0])] = value[1] else: - values[item['id']] = item['value'] + values[item["id"]] = item["value"] metrics.append(values) return pd.DataFrame(metrics) if __name__ == "__main__": - parser = argparse.ArgumentParser( description="Merge metrics of multiple states into single table." ) parser.add_argument( - "-s", - type=str, - default="output/simulation/*/metrics/", - help="states location") + "-s", type=str, default="output/simulation/*/metrics/", help="states location" + ) parser.add_argument( "-o", type=str, default="output/tables/metrics_sim_all.csv", - help="Output table location") + help="Output table location", + ) args = parser.parse_args() # load metric files @@ -80,4 +78,4 @@ def create_table_state_metrics(metric_files): # store result in output folder Path(args.o).parent.mkdir(parents=True, exist_ok=True) result.to_csv(Path(args.o)) - result.to_excel(Path(args.o).with_suffix('.xlsx')) + result.to_excel(Path(args.o).with_suffix(".xlsx")) diff --git a/examples/basic_example/scripts/merge_tds.py b/examples/basic_example/scripts/merge_tds.py index fc4d851..1beb52c 100644 --- a/examples/basic_example/scripts/merge_tds.py +++ b/examples/basic_example/scripts/merge_tds.py @@ -35,17 +35,21 @@ def create_table_state_tds(metrics): for metric in metrics: with open(metric) as f: - i = next(filter(lambda x: x['id'] == 'td', json.load(f)['data']['items']))['value'] # noqa + i = next(filter(lambda x: x["id"] == "td", json.load(f)["data"]["items"]))[ + "value" + ] # noqa values.extend((item[0], item[1], file_counter) for item in i) file_counter += 1 - df = pd.DataFrame(values, columns=['record_id', 'td', 'metric_file']) - pivoted = df.pivot_table(index='record_id', - columns='metric_file', - values='td', - aggfunc='first', - fill_value=0) - pivoted.columns = [f'td_sim_{col}' for col in pivoted.columns] + df = pd.DataFrame(values, columns=["record_id", "td", "metric_file"]) + pivoted = df.pivot_table( + index="record_id", + columns="metric_file", + values="td", + aggfunc="first", + fill_value=0, + ) + pivoted.columns = [f"td_sim_{col}" for col in pivoted.columns] return pivoted @@ -54,15 +58,14 @@ def create_table_state_tds(metrics): description="Merge tds of multiple metrics into single table." ) parser.add_argument( - "-s", - type=str, - default="output/simulation/*/metrics/", - help="metrics location") + "-s", type=str, default="output/simulation/*/metrics/", help="metrics location" + ) parser.add_argument( "-o", type=str, default="output/tables/tds_sim_all.csv", - help="Output table location") + help="Output table location", + ) args = parser.parse_args() # load metric files @@ -77,4 +80,4 @@ def create_table_state_tds(metrics): # store table Path(args.o).parent.mkdir(parents=True, exist_ok=True) states_table.to_csv(Path(args.o)) - states_table.to_excel(Path(args.o).with_suffix('.xlsx')) + states_table.to_excel(Path(args.o).with_suffix(".xlsx")) diff --git a/examples/multiple_models_example/scripts/merge_descriptives.py b/examples/multiple_models_example/scripts/merge_descriptives.py index 2713cb1..72e1390 100644 --- a/examples/multiple_models_example/scripts/merge_descriptives.py +++ b/examples/multiple_models_example/scripts/merge_descriptives.py @@ -35,10 +35,10 @@ def create_table_descriptives(datasets): for ds in datasets: with open(ds) as f: - data = json.load(f)['data']['items'] + data = json.load(f)["data"]["items"] values = {} for item in data: - values[item['id']] = item['value'] + values[item["id"]] = item["value"] stats.append(values) df = pd.DataFrame(stats, index=[Path(ds).name for ds in datasets]) @@ -46,7 +46,6 @@ def create_table_descriptives(datasets): if __name__ == "__main__": - parser = argparse.ArgumentParser( description="Merge descriptives of multiple files into single table." ) @@ -54,12 +53,14 @@ def create_table_descriptives(datasets): "-s", type=str, default="output/simulation/*/descriptives/", - help="Datasets location") + help="Datasets location", + ) parser.add_argument( "-o", type=str, default="output/tables/data_descriptives_all.csv", - help="Output table location") + help="Output table location", + ) args = parser.parse_args() # load datasets @@ -75,4 +76,4 @@ def create_table_descriptives(datasets): # store result in output folder Path(args.o).parent.mkdir(parents=True, exist_ok=True) result.to_csv(Path(args.o)) - result.to_excel(Path(args.o).with_suffix('.xlsx')) + result.to_excel(Path(args.o).with_suffix(".xlsx")) diff --git a/examples/multiple_models_example/scripts/merge_metrics.py b/examples/multiple_models_example/scripts/merge_metrics.py index e50f62d..aa03146 100644 --- a/examples/multiple_models_example/scripts/merge_metrics.py +++ b/examples/multiple_models_example/scripts/merge_metrics.py @@ -33,38 +33,36 @@ def create_table_state_metrics(metric_files): for metric in metric_files: with open(metric) as f: - data = json.load(f)['data']['items'] + data = json.load(f)["data"]["items"] values = {} - values['file_name'] = Path(metric).name + values["file_name"] = Path(metric).name for item in data: - if item['id'] == 'td': + if item["id"] == "td": continue # check if value is a list - if item['value'] is not None and isinstance(item['value'], list): - for value in item['value']: - values[item['id'] + "_" + str(value[0])] = value[1] + if item["value"] is not None and isinstance(item["value"], list): + for value in item["value"]: + values[item["id"] + "_" + str(value[0])] = value[1] else: - values[item['id']] = item['value'] + values[item["id"]] = item["value"] metrics.append(values) return pd.DataFrame(metrics) if __name__ == "__main__": - parser = argparse.ArgumentParser( description="Merge metrics of multiple states into single table." ) parser.add_argument( - "-s", - type=str, - default="output/simulation/*/metrics/", - help="states location") + "-s", type=str, default="output/simulation/*/metrics/", help="states location" + ) parser.add_argument( "-o", type=str, default="output/tables/metrics_sim_all.csv", - help="Output table location") + help="Output table location", + ) args = parser.parse_args() # load metric files @@ -80,4 +78,4 @@ def create_table_state_metrics(metric_files): # store result in output folder Path(args.o).parent.mkdir(parents=True, exist_ok=True) result.to_csv(Path(args.o)) - result.to_excel(Path(args.o).with_suffix('.xlsx')) + result.to_excel(Path(args.o).with_suffix(".xlsx")) diff --git a/examples/multiple_models_example/scripts/merge_tds.py b/examples/multiple_models_example/scripts/merge_tds.py index fc4d851..1beb52c 100644 --- a/examples/multiple_models_example/scripts/merge_tds.py +++ b/examples/multiple_models_example/scripts/merge_tds.py @@ -35,17 +35,21 @@ def create_table_state_tds(metrics): for metric in metrics: with open(metric) as f: - i = next(filter(lambda x: x['id'] == 'td', json.load(f)['data']['items']))['value'] # noqa + i = next(filter(lambda x: x["id"] == "td", json.load(f)["data"]["items"]))[ + "value" + ] # noqa values.extend((item[0], item[1], file_counter) for item in i) file_counter += 1 - df = pd.DataFrame(values, columns=['record_id', 'td', 'metric_file']) - pivoted = df.pivot_table(index='record_id', - columns='metric_file', - values='td', - aggfunc='first', - fill_value=0) - pivoted.columns = [f'td_sim_{col}' for col in pivoted.columns] + df = pd.DataFrame(values, columns=["record_id", "td", "metric_file"]) + pivoted = df.pivot_table( + index="record_id", + columns="metric_file", + values="td", + aggfunc="first", + fill_value=0, + ) + pivoted.columns = [f"td_sim_{col}" for col in pivoted.columns] return pivoted @@ -54,15 +58,14 @@ def create_table_state_tds(metrics): description="Merge tds of multiple metrics into single table." ) parser.add_argument( - "-s", - type=str, - default="output/simulation/*/metrics/", - help="metrics location") + "-s", type=str, default="output/simulation/*/metrics/", help="metrics location" + ) parser.add_argument( "-o", type=str, default="output/tables/tds_sim_all.csv", - help="Output table location") + help="Output table location", + ) args = parser.parse_args() # load metric files @@ -77,4 +80,4 @@ def create_table_state_tds(metrics): # store table Path(args.o).parent.mkdir(parents=True, exist_ok=True) states_table.to_csv(Path(args.o)) - states_table.to_excel(Path(args.o).with_suffix('.xlsx')) + states_table.to_excel(Path(args.o).with_suffix(".xlsx"))