From 599a13ad3488b7197159e65e66a7f13500a56b30 Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Tue, 19 Nov 2024 12:57:00 -0600 Subject: [PATCH 1/6] Update 07 initially --- .../07-transit-fit-with-exotic.ipynb | 60 ++++++++----------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/stellarphot/notebooks/photometry/07-transit-fit-with-exotic.ipynb b/stellarphot/notebooks/photometry/07-transit-fit-with-exotic.ipynb index 154cd36c..9c781bc7 100644 --- a/stellarphot/notebooks/photometry/07-transit-fit-with-exotic.ipynb +++ b/stellarphot/notebooks/photometry/07-transit-fit-with-exotic.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -14,14 +14,11 @@ "\n", "from astropy.table import Table\n", "\n", - "from stellarphot.transit_fitting.gui.gui_core import (\n", - " exotic_arguments,\n", - " generate_json_file_name,\n", - " get_values_from_widget,\n", - " exotic_settings_widget,\n", - " populate_TOI_boxes,\n", - ")\n", - "from stellarphot.settings.fits_opener import FitsOpener" + "#from try_json_editing_rev1 import whole_thing, generate_json_file_name, exotic_arguments, get_values_from_widget\n", + "from stellarphot.transit_fitting.gui import generate_json_file_name, exotic_arguments, get_values_from_widget, exotic_settings_widget, populate_TOI_boxes\n", + "from stellarphot.settings.fits_opener import FitsOpener\n", + "from stellarphot import PhotometryData\n", + "from stellarphot.io import TOI" ] }, { @@ -39,11 +36,9 @@ "metadata": {}, "outputs": [], "source": [ - "fits_openr = FitsOpener(\n", - " title=\"Select your photometry/relative flux file\", filter_pattern=[\"*.csv\"]\n", - ")\n", + "fits_opener = FitsOpener(title=\"Select your photometry/relative flux file\", filter_pattern=[\"*.ecsv\"])\n", "\n", - "fits_openr.file_chooser" + "fits_opener.file_chooser" ] }, { @@ -52,11 +47,10 @@ "metadata": {}, "outputs": [], "source": [ - "input_data_file = fits_openr.path\n", - "toi_info_file = \"tess-info.pickle\"\n", + "input_data_file = fits_opener.path\n", + "toi_info_file = Path(\"TIC-194461202-info.json\")\n", "\n", - "with open(toi_info_file, \"rb\") as f:\n", - " tess_info = pickle.load(f)" + "tess_info = TOI.model_validate_json(toi_info_file.read_text())" ] }, { @@ -67,7 +61,7 @@ "source": [ "settings = exotic_settings_widget()\n", "\n", - "exotic_data_file = \"data_for_exotic.csv\"" + "exotic_data_file = 'data_for_exotic.csv'" ] }, { @@ -76,12 +70,12 @@ "metadata": {}, "outputs": [], "source": [ - "inp_data = Table.read(input_data_file)\n", - "inp_data = inp_data[inp_data[\"star_id\"] == 1]\n", - "\n", + "inp_data = PhotometryData.read(input_data_file)\n", + "inp_data = inp_data[inp_data['star_id'] == 1]\n", + "inp_data.add_bjd_col()\n", "# inp_data = inp_data[inp_data['BJD'] > 2459795.75]\n", "\n", - "out_data = inp_data[\"BJD\", \"relative_flux\", \"relative_flux_error\", \"airmass\"]\n", + "out_data = inp_data['bjd', 'relative_flux', 'relative_flux_error', 'airmass']\n", "out_data.write(exotic_data_file, overwrite=True)" ] }, @@ -91,8 +85,8 @@ "metadata": {}, "outputs": [], "source": [ - "settings.data_file_widget[\"candidate\"].value = exotic_data_file\n", - "settings.data_file_widget[\"known\"].value = exotic_data_file\n", + "settings.data_file_widget['candidate'].value = exotic_data_file\n", + "settings.data_file_widget['known'].value = exotic_data_file\n", "populate_TOI_boxes(tess_info, settings.value_widget)" ] }, @@ -121,7 +115,7 @@ "metadata": {}, "outputs": [], "source": [ - "Path(contents[\"user_info\"][\"Directory to Save Plots\"]).mkdir(exist_ok=True)" + "Path(contents['user_info']['Directory to Save Plots']).mkdir(exist_ok=True)" ] }, { @@ -130,7 +124,7 @@ "metadata": {}, "outputs": [], "source": [ - "with open(settings_name, \"w\") as f:\n", + "with open(settings_name, 'w') as f:\n", " json.dump(contents, f)" ] }, @@ -140,12 +134,8 @@ "metadata": {}, "outputs": [], "source": [ - "print(\n", - " \"COPY AND PASTE THE LINE BELOW INTO AN EMPTY CELL TO RUN EXOTIC (include the exclamation point at the beginning):\\n\\n\"\n", - ")\n", - "print(\n", - " f'!exotic {\" \".join(exotic_arguments[settings.planet_type.value])} {settings_name}'\n", - ")\n", + "print('COPY AND PASTE THE LINE BELOW INTO AN EMPTY CELL TO RUN EXOTIC (include the exclamation point at the beginning):\\n\\n')\n", + "print(f'!exotic {\" \".join(exotic_arguments[settings.planet_type.value])} {settings_name}')\n", "print(\"\\n\\n\")" ] }, @@ -155,7 +145,7 @@ "metadata": {}, "outputs": [], "source": [ - "!exotic --override --pre TIC_402828941-2022-08-03-SI.json" + "!exotic --override --pre TIC_194461202-2024-10-08-SR.json" ] }, { @@ -168,7 +158,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "stelldev-pyd2", "language": "python", "name": "python3" }, @@ -182,7 +172,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.4" + "version": "3.11.9" } }, "nbformat": 4, From 8e31f2971ce7306e398cedbf5070797498308f0d Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Tue, 19 Nov 2024 20:59:59 -0600 Subject: [PATCH 2/6] Minor updates to relative flux notebook --- ...5-relative-flux-calculation-template.ipynb | 76 ++++++------------- 1 file changed, 22 insertions(+), 54 deletions(-) mode change 100755 => 100644 stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb diff --git a/stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb b/stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb old mode 100755 new mode 100644 index 5442ce31..1bf7adde --- a/stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb +++ b/stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb @@ -19,7 +19,7 @@ "\n", "import numpy as np\n", "\n", - "from stellarphot.differential_photometry.aij_rel_fluxes import *\n", + "from stellarphot.differential_photometry.aij_rel_fluxes import calc_aij_relative_flux\n", "from stellarphot.settings.fits_opener import FitsOpener\n", "from stellarphot import PhotometryData, SourceListData" ] @@ -79,15 +79,6 @@ "output_file = photometry_file.stem + \"-relative-flux\" + photometry_file.suffix" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "photometry_file" - ] - }, { "cell_type": "code", "execution_count": null, @@ -96,10 +87,7 @@ }, "outputs": [], "source": [ - "\n", - "photometry = PhotometryData.read(photometry_file)\n", - "del_rows = photometry[\"file\"] == \"image_file.fits\"\n", - "photometry = photometry[~del_rows]" + "photometry = PhotometryData.read(photometry_file)" ] }, { @@ -113,17 +101,6 @@ "comp_table = SourceListData.read(aperture_file)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "comp_table" - ] - }, { "cell_type": "code", "execution_count": null, @@ -133,38 +110,13 @@ "outputs": [], "source": [ "comp_table[\"coord\"] = SkyCoord(\n", - " ra=comp_table[\"ra\"], dec=comp_table[\"dec\"], unit=\"degree\", frame=\"icrs\"\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ + " ra=comp_table[\"ra\"], dec=comp_table[\"dec\"], frame=\"icrs\"\n", + ")\n", "comp_bool = comp_table[\"marker name\"] == [\"APASS comparison\"]\n", "only_comp_stars = comp_table[comp_bool]\n", "coords = only_comp_stars[\"coord\"]" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "ra = [i.ra for i in coords]\n", - "dec = [i.dec for i in coords]\n", - "comp_coords = Table()\n", - "comp_coords[\"ra\"] = ra * u.degree\n", - "comp_coords[\"dec\"] = dec * u.degree" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -181,7 +133,23 @@ "outputs": [], "source": [ "# calculate flux\n", - "flux_table = calc_aij_relative_flux(photometry, comp_coords)" + "flux_table = calc_aij_relative_flux(photometry, only_comp_stars)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if \"bjd\" not in flux_table.colnames:\n", + " flux_group = flux_table.group_by(\"file\")\n", + " # Add dummy BJD column so the whole table has one\n", + " flux[\"bjd\"] = np.nan\n", + " for group in flux_group.groups:\n", + " mean_ra = group[\"ra\"].mean()\n", + " mean_dec = group[\"dec\"].mean()\n", + " group.add_bjd_col(bjd_coordinates=SkyCoord(mean_ra, mean_dec))" ] }, { @@ -219,7 +187,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.9" } }, "nbformat": 4, From 865b6e29ce3782d06c362e7891b8e1ef8bb2cc52 Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Wed, 20 Nov 2024 09:24:10 -0600 Subject: [PATCH 3/6] Add function for generating flux column given files --- .../differential_photometry/aij_rel_fluxes.py | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/stellarphot/differential_photometry/aij_rel_fluxes.py b/stellarphot/differential_photometry/aij_rel_fluxes.py index cf6a37c1..5e438d1f 100644 --- a/stellarphot/differential_photometry/aij_rel_fluxes.py +++ b/stellarphot/differential_photometry/aij_rel_fluxes.py @@ -3,7 +3,9 @@ from astropy.coordinates import SkyCoord from astropy.table import QTable, Table -__all__ = ["add_in_quadrature", "calc_aij_relative_flux"] +from stellarphot import PhotometryData, SourceListData + +__all__ = ["add_in_quadrature", "calc_aij_relative_flux", "add_relative_flux_column"] def add_in_quadrature(array): @@ -192,3 +194,59 @@ def calc_aij_relative_flux( star_data["comparison error"] = comp_error_vector return star_data + + +def add_relative_flux_column( + photometry_data_file, + source_list_file, + add_name="-relative-flux", + verbose=False, +): + """ + Add AIJ-style relative flux columns to a photometry data file. + + Parameters + ---------- + + photometry_data_file : str + Path to the photometry data file. + + source_list_file : str + Path to the source list file. + + add_name : str, optional + String to add to the end of the new file name before the suffix. + + Returns + ------- + + None; writes a file with the relative flux columns added. + """ + if verbose: + print("Reading photometry data and source list") + photometry_data = PhotometryData.read(photometry_data_file) + source_list = SourceListData.read(source_list_file) + output_file = photometry_data_file.stem + add_name + photometry_data_file.suffix + source_list["coord"] = SkyCoord( + ra=source_list["ra"], dec=source_list["dec"], frame="icrs" + ) + comp_bool = source_list["marker name"] == ["APASS comparison"] + only_comp_stars = source_list[comp_bool] + if verbose: + print("Adding AIJ-style relative flux columns to photomotry data") + flux_table = calc_aij_relative_flux(photometry_data, only_comp_stars) + # Add bjd if needed + + if "bjd" not in flux_table.colnames: + if verbose: + print("Adding BJD column to photometry data") + # Add dummy BJD column so the whole table has one + flux_table["bjd"] = np.nan + flux_group = flux_table.group_by("file") + for group in flux_group.groups: + mean_ra = group["ra"].mean() + mean_dec = group["dec"].mean() + group.add_bjd_col(bjd_coordinates=SkyCoord(mean_ra, mean_dec)) + if verbose: + print("Writing photometry data with relative flux columns") + flux_table.write(output_file, overwrite=True) From 8c8f550aab078c032bd5a5c454117158b5bf977b Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Wed, 20 Nov 2024 09:24:53 -0600 Subject: [PATCH 4/6] Simplify relative flux notebook --- ...5-relative-flux-calculation-template.ipynb | 143 ++++-------------- 1 file changed, 26 insertions(+), 117 deletions(-) diff --git a/stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb b/stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb index 1bf7adde..dc596b15 100644 --- a/stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb +++ b/stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb @@ -19,9 +19,9 @@ "\n", "import numpy as np\n", "\n", - "from stellarphot.differential_photometry.aij_rel_fluxes import calc_aij_relative_flux\n", + "from stellarphot.differential_photometry.aij_rel_fluxes import add_relative_flux_column\n", "from stellarphot.settings.fits_opener import FitsOpener\n", - "from stellarphot import PhotometryData, SourceListData" + "from stellarphot.settings.custom_widgets import Spinner" ] }, { @@ -42,7 +42,28 @@ "fits_openr = FitsOpener(title=\"Select your photometry file\", filter_pattern=[\"*.csv\", \"*.ecsv\"])\n", "fits_openr2 = FitsOpener(\n", " title=\"Select your aperture file\", filter_pattern=[\"*.csv\", \"*.fits\", \"*.ecsv\"]\n", - ")" + ")\n", + "go = ipw.Button(description=\"Add flux column\", disabled=True)\n", + "prog_box = ipw.HBox()\n", + "progress_output = ipw.Output()\n", + "spinner = Spinner()\n", + "prog_box.children = [spinner, progress_output]\n", + "\n", + "def on_click(button):\n", + " aperture_file = fits_openr2.path\n", + " photometry_file = fits_openr.path\n", + " spinner.start()\n", + " with progress_output:\n", + " add_relative_flux_column(photometry_file, aperture_file, verbose=True)\n", + " spinner.stop()\n", + "\n", + "def enable_go(change):\n", + " if fits_openr.file_chooser.selected_filename and fits_openr2.file_chooser.selected_filename:\n", + " go.disabled = False\n", + "\n", + "fits_openr.file_chooser.observe(enable_go, names=\"_value\")\n", + "fits_openr2.file_chooser.observe(enable_go, names=\"_value\")\n", + "go.on_click(on_click)" ] }, { @@ -54,121 +75,9 @@ "outputs": [], "source": [ "box = ipw.VBox()\n", - "box.children = [fits_openr.file_chooser, fits_openr2.file_chooser]\n", + "box.children = [fits_openr.file_chooser, fits_openr2.file_chooser, go, prog_box]\n", "box" ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Run the remaining cells" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "aperture_file = fits_openr2.path\n", - "photometry_file = fits_openr.path\n", - "\n", - "output_file = photometry_file.stem + \"-relative-flux\" + photometry_file.suffix" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "photometry = PhotometryData.read(photometry_file)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "comp_table = SourceListData.read(aperture_file)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "comp_table[\"coord\"] = SkyCoord(\n", - " ra=comp_table[\"ra\"], dec=comp_table[\"dec\"], frame=\"icrs\"\n", - ")\n", - "comp_bool = comp_table[\"marker name\"] == [\"APASS comparison\"]\n", - "only_comp_stars = comp_table[comp_bool]\n", - "coords = only_comp_stars[\"coord\"]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Calculate flux" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# calculate flux\n", - "flux_table = calc_aij_relative_flux(photometry, only_comp_stars)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "if \"bjd\" not in flux_table.colnames:\n", - " flux_group = flux_table.group_by(\"file\")\n", - " # Add dummy BJD column so the whole table has one\n", - " flux[\"bjd\"] = np.nan\n", - " for group in flux_group.groups:\n", - " mean_ra = group[\"ra\"].mean()\n", - " mean_dec = group[\"dec\"].mean()\n", - " group.add_bjd_col(bjd_coordinates=SkyCoord(mean_ra, mean_dec))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "flux_table.write(output_file, overwrite=True)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -187,7 +96,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.11.8" } }, "nbformat": 4, From 48b958c927c69918d744ab34f2ee024d76d08e9a Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Wed, 20 Nov 2024 09:35:21 -0600 Subject: [PATCH 5/6] Move flux notebook --- ...calculation-template.ipynb => relative-flux-calculation.ipynb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename stellarphot/notebooks/{photometry/05-relative-flux-calculation-template.ipynb => relative-flux-calculation.ipynb} (100%) diff --git a/stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb b/stellarphot/notebooks/relative-flux-calculation.ipynb similarity index 100% rename from stellarphot/notebooks/photometry/05-relative-flux-calculation-template.ipynb rename to stellarphot/notebooks/relative-flux-calculation.ipynb From 972b3563e4d2b013f87379e288939e7566f1ac8e Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Wed, 20 Nov 2024 09:37:51 -0600 Subject: [PATCH 6/6] Update app launcher to include new notebook --- .jp_app_launcher_stellarphot.yaml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.jp_app_launcher_stellarphot.yaml b/.jp_app_launcher_stellarphot.yaml index f299d3c5..61250644 100644 --- a/.jp_app_launcher_stellarphot.yaml +++ b/.jp_app_launcher_stellarphot.yaml @@ -37,9 +37,17 @@ # Notebook in the "Stellarphot analysis and tools" section of the launcher - title: Generate TESS target list - description: View and export photometry results + description: Retrieve TESS target list and TIC information source: ../stellarphot/tess-target-source-list-generator.ipynb icon: ../stellarphot/stellarphot-logo.svg cwd: not_used type: notebook catalog: Stellarphot analysis and tools + +- title: Calculate relative flux + description: Add a relative flux column to the photometry table + source: ../stellarphot/relative-flux-calculation.ipynb + icon: ../stellarphot/stellarphot-logo.svg + cwd: not_used + type: notebook + catalog: Stellarphot analysis and tools