Skip to content

Commit

Permalink
Ariel
Browse files Browse the repository at this point in the history
  • Loading branch information
schmelly committed Dec 30, 2023
2 parents 42a32f9 + 849d2ee commit 69d8937
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 109 deletions.
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Run GraXpert",
"type": "python",
"request": "launch",
"module": "graxpert.main",
"justMyCode": true
}
]
}
164 changes: 119 additions & 45 deletions graxpert/CommandLineTool.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import json
import logging
import os
import sys
from textwrap import dedent

import numpy as np
from appdirs import user_config_dir

from graxpert.ai_model_handling import (ai_model_path_from_version,
download_version, latest_version,
list_local_versions)
from graxpert.ai_model_handling import ai_model_path_from_version, download_version, latest_version, list_local_versions
from graxpert.astroimage import AstroImage
from graxpert.background_extraction import extract_background
from graxpert.preferences import load_preferences, save_preferences
from graxpert.preferences import Prefs, load_preferences, save_preferences

user_preferences_filename = os.path.join(user_config_dir(appname="GraXpert"), "preferences.json")


class CommandLineTool:
Expand All @@ -18,99 +21,170 @@ def __init__(self, args):

def execute(self):
astro_Image = AstroImage(do_update_display=False)
astro_Image.set_from_file(self.args.filename)
astro_Image.set_from_file(self.args.filename, None, None)

processed_Astro_Image = AstroImage(do_update_display=False)
background_Astro_Image = AstroImage(do_update_display=False)

processed_Astro_Image.fits_header = astro_Image.fits_header
background_Astro_Image.fits_header = astro_Image.fits_header

ai_version = self.get_ai_version()

downscale_factor = 1

if self.args.preferences_file is not None:
preferences = Prefs()
preferences.interpol_type_option = "AI"
try:
preferences_file = os.path.abspath(self.args.preferences_file)
if os.path.isfile(preferences_file):
with open(preferences_file, "r") as f:
json_prefs = json.load(f)
if "background_points" in json_prefs:
preferences.background_points = json_prefs["background_points"]
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 "smoothing_option" in json_prefs:
preferences.smoothing_option = json_prefs["smoothing_option"]
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 "corr_type" in json_prefs:
preferences.corr_type = json_prefs["corr_type"]
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

except Exception as e:
logging.exception(e)
logging.shutdown()
sys.exit(1)
else:
preferences = Prefs()
preferences.interpol_type_option = "AI"

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 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,
[],
"AI",
self.args.smoothing,
1,
50,
"RBF",
0,
self.args.correction,
ai_model_path_from_version(ai_version),
np.array(preferences.background_points),
preferences.interpol_type_option,
preferences.smoothing_option,
downscale_factor,
preferences.sample_size,
preferences.RBF_kernel,
preferences.spline_order,
preferences.corr_type,
ai_model_path,
)
)

processed_Astro_Image.set_from_array(astro_Image.img_array)

processed_Astro_Image.save(self.get_save_path(), self.get_output_file_format())
if (self.args.bg):
if self.args.bg:
background_Astro_Image.save(self.get_background_save_path(), self.get_output_file_format())


def get_ai_version(self):
prefs_filename = os.path.join(
user_config_dir(appname="GraXpert"), "preferences.json"
)
prefs = load_preferences(prefs_filename)
def get_ai_version(self, prefs):
user_preferences = load_preferences(user_preferences_filename)

ai_version = None
if self.args.ai_version:
ai_version = self.args.ai_version
logging.info(f"Using user-supplied AI version {ai_version}.")
else:
ai_version = prefs.ai_version

if ai_version is None:
ai_version = latest_version()

logging.info(
"using AI version {}. you can change this by providing the argument '-ai_version'".format(
ai_version
)
)
logging.info(f"Using AI version {ai_version}. You can overwrite this by providing the argument '-ai_version'")

if not ai_version in [v["version"] for v in list_local_versions()]:
try:
logging.info(
"AI version {} not found locally, downloading...".format(ai_version)
)
logging.info(f"AI version {ai_version} not found locally, downloading...")
download_version(ai_version)
logging.info("download successful".format(ai_version))
logging.info("download successful")
except Exception as e:
logging.exception(e)
logging.shutdown()
sys.exit(1)

prefs.ai_version = ai_version
save_preferences(prefs_filename, prefs)
user_preferences.ai_version = ai_version
save_preferences(user_preferences_filename, user_preferences)

return ai_version

def get_output_file_ending(self):
file_ending = os.path.splitext(self.args.filename)[-1]

if file_ending.lower() == ".xisf":
return ".xisf"
else:
return ".fits"

def get_output_file_format(self):
output_file_ending = self.get_output_file_ending()
if (output_file_ending) == ".xisf":
return "32 bit XISF"
else:
return "32 bit Fits"

def get_save_path(self):
if (self.args.output is not None):
if self.args.output is not None:
base_path = os.path.dirname(self.args.filename)
output_file_name = self.args.output + self.get_output_file_ending()
return os.path.join(base_path, output_file_name)

else:
return os.path.splitext(self.args.filename)[0] + "_GraXpert" + self.get_output_file_ending()

def get_background_save_path(self):
save_path = self.get_save_path()
return os.path.splitext(save_path)[0] + "_background" + self.get_output_file_ending()
18 changes: 13 additions & 5 deletions graxpert/ai_model_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@
import re
import shutil
import zipfile
from queue import Empty, Queue
from threading import Thread

from appdirs import user_data_dir
from minio import Minio
from packaging import version

from graxpert.s3_secrets import bucket_name, endpoint, ro_access_key, ro_secret_key
try:
from graxpert.s3_secrets import bucket_name, endpoint, ro_access_key, ro_secret_key

client = Minio(endpoint, ro_access_key, ro_secret_key)
except Exception as e:
logging.exception(e)
client = None

from graxpert.ui.loadingframe import DynamicProgressThread

ai_models_dir = os.path.join(user_data_dir(appname="GraXpert"), "ai-models")
os.makedirs(ai_models_dir, exist_ok=True)

client = Minio(endpoint, ro_access_key, ro_secret_key)


# ui operations
def list_remote_versions():
if client is None:
return []
try:
objects = client.list_objects(bucket_name)
versions = []
Expand Down Expand Up @@ -71,6 +76,9 @@ def latest_version():


def ai_model_path_from_version(local_version):
if local_version is None:
return None

return os.path.join(ai_models_dir, local_version, "bg_model")


Expand Down
49 changes: 38 additions & 11 deletions graxpert/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import platform
import sys

# ensure sys.stdout and sys.stderr are not None in PyInstaller environments
Expand All @@ -16,6 +17,8 @@

from graxpert.ai_model_handling import list_local_versions, list_remote_versions
from graxpert.mp_logging import configure_logging
from graxpert.version import release as graxpert_release
from graxpert.version import version as graxpert_version

available_local_versions = []
available_remote_versions = []
Expand Down Expand Up @@ -87,7 +90,7 @@ def ui_main():
from graxpert.ui.ui_events import UiEvents
from graxpert.version import release, version

def on_closing(root, logging_thread):
def on_closing(root: CTk, logging_thread):
app_state_2_prefs(graxpert.prefs, graxpert.cmd.app_state)

prefs_filename = os.path.join(user_config_dir(appname="GraXpert"), "preferences.json")
Expand Down Expand Up @@ -131,15 +134,19 @@ def check_for_new_version():
logging.warning("Could not check for newest version")

logging_thread = initialize_logging()
check_for_new_version()

style()
root = CTk()

try:
root.state("zoomed")
except:
root.geometry("1024x768")
if "Linux" == platform.system():
root.attributes("-zoomed", True)
else:
root.state("zoomed")
except Exception as e:
root.state("normal")
logging.warning(e, stack_info=True)

root.title("GraXpert | Release: '{}' ({})".format(release, version))
root.iconbitmap()
root.iconphoto(True, tk.PhotoImage(file=resource_path("img/Icon.png")))
Expand All @@ -152,6 +159,7 @@ def check_for_new_version():
app = ApplicationFrame(root)
app.grid(column=0, row=0, sticky=tk.NSEW)
root.update()
check_for_new_version()
eventbus.emit(UiEvents.DISPLAY_START_BADGE_REQUEST)
root.mainloop()

Expand All @@ -174,16 +182,35 @@ 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=None,
type=str,
help="Allows GraXpert commandline to run all extraction methods based on a preferences file that contains background grid points",
)
parser.add_argument("-output", "--output", nargs="?", required=False, type=str, help="Filename of the processed image")
parser.add_argument("-bg", "--bg", required=False, action="store_true", help="Also save the background model")
parser.add_argument("-cli", "--cli", required=False, action="store_true", help="Has to be added when using the command line integration of GraXpert")
parser.add_argument("-v", "--version", action="version", version=f"GraXpert version: {graxpert_version} release: {graxpert_release}")

args = parser.parse_args()

from graxpert.CommandLineTool import CommandLineTool
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()

clt = CommandLineTool(args)
clt.execute()
logging.shutdown()
else:
ui_main()

Expand Down
Loading

0 comments on commit 69d8937

Please sign in to comment.