Skip to content

Commit

Permalink
WIP Run Launcher widget
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcraig committed Jul 4, 2024
1 parent 820d34f commit 5ada920
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 62 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies = [
"ipywidgets",
"jupyter-app-launcher",
"matplotlib",
"papermill",
"pandas",
"photutils >=1.9",
"pydantic >=2",
Expand Down Expand Up @@ -178,4 +179,6 @@ filterwarnings = [
'ignore:RADECSYS=:',
# photutils changed the name of a function again
'ignore:The make_gaussian_sources_image function is deprecated',
# papermill is using deprecated jupyter paths
'ignore:Jupyter is migrating its paths to use standard platformdirs:DeprecationWarning',
]
82 changes: 20 additions & 62 deletions stellarphot/notebooks/run-launcher.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,38 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "e5f2fc8f-b893-400e-8fb7-4dd8e871334f",
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as ipw\n",
"import papermill as pm\n",
"\n",
"from ipyautoui.custom import FileChooser\n",
"\n",
"from stellarphot.gui_tools import FitsOpener\n",
"from stellarphot.settings.custom_widgets import Confirm\n",
"from stellarphot.settings import PhotometryRunSettings"
"from stellarphot.settings.custom_widgets import Kaboodle"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2389ff77-c8ac-4bad-afb7-79cdd5c0f365",
"metadata": {},
"outputs": [],
"source": [
"class Kaboodle(ipw.VBox):\n",
" def __init__(self, *args, **kwargs):\n",
" super().__init__(*args, **kwargs)\n",
" self.fo = FitsOpener(title=\"Choose any image in the folder of images to do photometry on that contains the object of interest\")\n",
" self.info_box = ipw.HTML()\n",
" self.run_output = ipw.Output()\n",
" self.confirm = Confirm(message=\"Is this correct?\")\n",
" self.children = (self.fo.file_chooser, self.info_box, self.confirm, self.run_output)\n",
" self.fo.file_chooser.observe(self._file_chosen, \"_value\")\n",
" self.confirm.observe(self._confirmation, \"value\")\n",
" self.run_settings = None\n",
"\n",
" def _file_chosen(self, change=None):\n",
" self.run_settings = PhotometryRunSettings(\n",
" directory_with_images=self.fo.path.parent, \n",
" object_of_interest=self.fo.header['object']\n",
" )\n",
" self.info_box.value = \"<h2>\" + self.info_message + \"</br>Is this correct?\" + \"</h2>\"\n",
" self.confirm.show()\n",
"\n",
" @property\n",
" def info_message(self):\n",
" return (f\"Photomery will be done on all images of the object \"\n",
" f\"'<code>{self.run_settings.object_of_interest}</code>' in the \"\n",
" f\"folder '<code>{self.run_settings.directory_with_images}</code>'\")\n",
" \n",
" def _confirmation(self, change=None):\n",
" if change[\"new\"]:\n",
" # User said yes\n",
"\n",
" # Update informational message\n",
" self.info_box.value = \"<h2>\" + self.info_message + \"</br>Photometry is running...\" + \"</h2>\"\n",
" with self.run_output:\n",
" pm.execute_notebook(\n",
" \"stellarphot/notebooks/photometry_runner.ipynb\",\n",
" \"MEEEPhonk.ipynb\",\n",
" parameters=self.run_settings.model_dump(mode=\"json\")\n",
" )\n",
" else:\n",
" # User said no, so reset to initial state.\n",
" self.fo.file_chooser.reset()\n",
" self.info_box.value = \"\"\n",
" self.run_settings = None\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "c5eed8a2-7533-4411-8f6e-303b854f4e3e",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "415ea731dc1d42279fcce2ff8a718672",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Kaboodle(children=(FileChooser(path='/Users/mattcraig/development/astronomy/stellarphot/stellarphot/notebooks'…"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"kab = Kaboodle()\n",
"kab"
Expand Down
72 changes: 72 additions & 0 deletions stellarphot/settings/custom_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class StrEnum(str, Enum):


import ipywidgets as ipw
import papermill as pm
import traitlets as tr
from astropy.utils.data import get_pkg_data_filename
from camel_converter import to_snake
from ipyautoui.autoobject import AutoObject
from ipyautoui.custom.iterable import ItemBox
Expand All @@ -25,10 +27,12 @@ class StrEnum(str, Enum):
Observatory,
PartialPhotometrySettings,
PassbandMap,
PhotometryRunSettings,
PhotometryWorkingDirSettings,
SavedSettings,
ui_generator,
)
from stellarphot.settings.fits_opener import FitsOpener

__all__ = ["ChooseOrMakeNew", "Confirm", "SettingWithTitle"]

Expand Down Expand Up @@ -985,3 +989,71 @@ def save_wd(_=None):
raise ValueError(
f"The widget {setting_widget} is not a recognized type of widget."
)


class PhotometryRunner(ipw.VBox):
def __init__(
self, photometry_notebook_name="photometry_run.ipynb", *args, **kwargs
):
super().__init__(*args, **kwargs)
self.photometry_notebook_name = photometry_notebook_name
self.fo = FitsOpener(

Check warning on line 1000 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L998-L1000

Added lines #L998 - L1000 were not covered by tests
title=(
"Choose any image in the folder of images to do photometry on that "
"contains the object of interest"
)
)
self.info_box = ipw.HTML()
self.run_output = ipw.Output()
self.confirm = Confirm(message="Is this correct?")
self.children = (

Check warning on line 1009 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1006-L1009

Added lines #L1006 - L1009 were not covered by tests
self.fo.file_chooser,
self.info_box,
self.confirm,
self.run_output,
)
self.fo.file_chooser.observe(self._file_chosen, "_value")
self.confirm.observe(self._confirmation, "value")
self.run_settings = None

Check warning on line 1017 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1015-L1017

Added lines #L1015 - L1017 were not covered by tests

def _file_chosen(self, _):
self.run_settings = PhotometryRunSettings(

Check warning on line 1020 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1020

Added line #L1020 was not covered by tests
directory_with_images=self.fo.path.parent,
object_of_interest=self.fo.header["object"],
)
self.info_box.value = (

Check warning on line 1024 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1024

Added line #L1024 was not covered by tests
"<h2>" + self.info_message + "</br>Is this correct?" + "</h2>"
)
self.confirm.show()

Check warning on line 1027 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1027

Added line #L1027 was not covered by tests

@property
def info_message(self):
return (

Check warning on line 1031 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1031

Added line #L1031 was not covered by tests
f"Photometry will be done on all images of the object "
f"'<code>{self.run_settings.object_of_interest}</code>' in the "
f"folder '<code>{self.run_settings.directory_with_images}</code>'"
)

def _confirmation(self, change=None):
if change["new"]:

Check warning on line 1038 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1038

Added line #L1038 was not covered by tests
# User said yes

# Update informational message
self.info_box.value = (

Check warning on line 1042 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1042

Added line #L1042 was not covered by tests
"<h2>" + self.info_message + "</br>Photometry is running..." + "</h2>"
)
template_nb = get_pkg_data_filename(

Check warning on line 1045 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1045

Added line #L1045 was not covered by tests
"photometry_runner.ipynb", package="stellarphot.notebooks"
)
print(template_nb)
with self.run_output:
pm.execute_notebook(

Check warning on line 1050 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1048-L1050

Added lines #L1048 - L1050 were not covered by tests
template_nb,
self.photometry_notebook_name,
parameters=self.run_settings.model_dump(mode="json"),
)
else:
# User said no, so reset to initial state.
self.fo.file_chooser.reset()
self.info_box.value = ""
self.run_settings = None

Check warning on line 1059 in stellarphot/settings/custom_widgets.py

View check run for this annotation

Codecov / codecov/patch

stellarphot/settings/custom_widgets.py#L1057-L1059

Added lines #L1057 - L1059 were not covered by tests

0 comments on commit 5ada920

Please sign in to comment.