From 2404922646373f72c888848814870bbb069780ba Mon Sep 17 00:00:00 2001 From: David Schmelter Date: Sat, 30 Dec 2023 14:31:59 +0100 Subject: [PATCH] fix handling of cli default values, improve logging --- graxpert/CommandLineTool.py | 59 ++++++++++++++++++++++++++++++------- graxpert/main.py | 8 +++-- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/graxpert/CommandLineTool.py b/graxpert/CommandLineTool.py index a3fdc13..4e7cef0 100644 --- a/graxpert/CommandLineTool.py +++ b/graxpert/CommandLineTool.py @@ -2,6 +2,7 @@ import logging import os import sys +from textwrap import dedent import numpy as np from appdirs import user_config_dir @@ -30,7 +31,7 @@ def execute(self): downscale_factor = 1 - if self.args.preferences_file: + if self.args.preferences_file is not None: preferences = Prefs() preferences.interpol_type_option = "AI" try: @@ -38,13 +39,18 @@ def execute(self): if os.path.isfile(preferences_file): with open(preferences_file, "r") as f: json_prefs = json.load(f) - preferences.background_points = json_prefs["background_points"] - preferences.sample_size = json_prefs["sample_size"] - preferences.spline_order = json_prefs["spline_order"] - preferences.RBF_kernel = json_prefs["RBF_kernel"] - preferences.interpol_type_option = json_prefs["interpol_type_option"] - preferences.ai_version = json_prefs["ai_version"] - + if "background_points" in json_prefs: + preferences.background_points = json_prefs["background_points"] + if "sample_size" in json_prefs: + preferences.sample_size = json_prefs["sample_size"] + if "spline_order" in json_prefs: + preferences.spline_order = json_prefs["spline_order"] + if "RBF_kernel" in json_prefs: + preferences.RBF_kernel = json_prefs["RBF_kernel"] + if "interpol_type_option" in json_prefs: + preferences.interpol_type_option = json_prefs["interpol_type_option"] + if "ai_version" in json_prefs: + preferences.ai_version = json_prefs["ai_version"] if preferences.interpol_type_option == "Kriging" or preferences.interpol_type_option == "RBF": downscale_factor = 4 @@ -53,22 +59,53 @@ def execute(self): logging.shutdown() sys.exit(1) else: - preferences = load_preferences(user_preferences_filename) + preferences = Prefs() preferences.interpol_type_option = "AI" - if self.args.smoothing: + if self.args.smoothing is not None: preferences.smoothing_option = self.args.smoothing logging.info(f"Using user-supplied smoothing value {preferences.smoothing_option}.") + else: + logging.info(f"Using stored smoothing value {preferences.smoothing_option}.") - if self.args.correction: + if self.args.correction is not None: preferences.corr_type = self.args.correction logging.info(f"Using user-supplied correction type {preferences.corr_type}.") + else: + logging.info(f"Using stored correction type {preferences.corr_type}.") if preferences.interpol_type_option == "AI": ai_model_path = ai_model_path_from_version(self.get_ai_version(preferences)) else: ai_model_path = None + if preferences.interpol_type_option == "AI": + logging.info( + dedent( + f"""\ + Excecuting background extraction with the following parameters: + interpolation type - {preferences.interpol_type_option} + smoothing - {preferences.smoothing_option} + correction type - {preferences.corr_type} + AI model path - {ai_model_path}""" + ) + ) + else: + logging.info( + dedent( + f"""\ + Excecuting background extraction with the following parameters: + interpolation type - {preferences.interpol_type_option} + background points - {preferences.background_points} + sample size - {preferences.sample_size} + kernel - {preferences.RBF_kernel} + spline order - {preferences.spline_order} + smoothing - {preferences.smoothing_option} + orrection type - {preferences.corr_type} + downscale_factor - {downscale_factor}""" + ) + ) + background_Astro_Image.set_from_array( extract_background( astro_Image.img_array, diff --git a/graxpert/main.py b/graxpert/main.py index 0456315..55a9bda 100644 --- a/graxpert/main.py +++ b/graxpert/main.py @@ -176,14 +176,14 @@ def main(): type=version_type, help='Version of the AI model, default: "latest"; available locally: [{}], available remotely: [{}]'.format(", ".join(available_local_versions), ", ".join(available_remote_versions)), ) - parser.add_argument("-correction", "--correction", nargs="?", required=False, default="Subtraction", choices=["Subtraction", "Division"], type=str, help="Subtraction or Division") - parser.add_argument("-smoothing", "--smoothing", nargs="?", required=False, default=0.0, type=float, help="Strength of smoothing between 0 and 1") + parser.add_argument("-correction", "--correction", nargs="?", required=False, default=None, choices=["Subtraction", "Division"], type=str, help="Subtraction or Division") + parser.add_argument("-smoothing", "--smoothing", nargs="?", required=False, default=None, type=float, help="Strength of smoothing between 0 and 1") parser.add_argument( "-preferences_file", "--preferences_file", nargs="?", required=False, - default="", + default=None, type=str, help="Allows GraXpert commandline to run all extraction methods based on a preferences file that contains background grid points", ) @@ -197,10 +197,12 @@ def main(): if args.cli: from graxpert.CommandLineTool import CommandLineTool + logging.info(f"Starting GraXpert CLI, version: {graxpert_version} release: {graxpert_release}") clt = CommandLineTool(args) clt.execute() logging.shutdown() else: + logging.info(f"Starting GraXpert UI, version: {graxpert_version} release: {graxpert_release}") ui_main() else: