From 0ca72605d33ada9f445dfbbe857cec0ff578e641 Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Wed, 27 Nov 2024 15:25:10 -0600 Subject: [PATCH 1/6] Add fix for adding BJD column --- .../differential_photometry/aij_rel_fluxes.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/stellarphot/differential_photometry/aij_rel_fluxes.py b/stellarphot/differential_photometry/aij_rel_fluxes.py index 5e438d1f..36875eb3 100644 --- a/stellarphot/differential_photometry/aij_rel_fluxes.py +++ b/stellarphot/differential_photometry/aij_rel_fluxes.py @@ -2,6 +2,7 @@ import numpy as np from astropy.coordinates import SkyCoord from astropy.table import QTable, Table +from astropy.time import Time from stellarphot import PhotometryData, SourceListData @@ -240,13 +241,20 @@ def add_relative_flux_column( 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 + # Accumulate the BJD here + bjds = [] + 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)) + bjds.extend(group["bjd"].jd) + + # Each (ephemeral) group had a BJD, this adds the column to the + # original table. + flux_group["bjd"] = Time(bjds, scale="tdb", format="jd") + if verbose: print("Writing photometry data with relative flux columns") - flux_table.write(output_file, overwrite=True) + flux_group.write(output_file, overwrite=True) From a7030a3e3a9f3db1cb2313925963b53ac3d78edd Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Sat, 30 Nov 2024 20:08:41 -1000 Subject: [PATCH 2/6] Fix name and a couple bugs in transit model notebook --- .jp_app_launcher_stellarphot.yaml | 2 +- ...odel-fit.ipynb => tess-initial-model-fit.ipynb} | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) rename stellarphot/notebooks/{tess-intial-model-fit.ipynb => tess-initial-model-fit.ipynb} (97%) diff --git a/.jp_app_launcher_stellarphot.yaml b/.jp_app_launcher_stellarphot.yaml index 2231f776..6e67a623 100644 --- a/.jp_app_launcher_stellarphot.yaml +++ b/.jp_app_launcher_stellarphot.yaml @@ -54,7 +54,7 @@ - title: Initial exoplanet model description: Do an initial fit to a transit - source: ../stellarphot/tess-intial-model-fit.ipynb + source: ../stellarphot/tess-initial-model-fit.ipynb icon: ../stellarphot/stellarphot-logo.svg cwd: not_used type: notebook diff --git a/stellarphot/notebooks/tess-intial-model-fit.ipynb b/stellarphot/notebooks/tess-initial-model-fit.ipynb similarity index 97% rename from stellarphot/notebooks/tess-intial-model-fit.ipynb rename to stellarphot/notebooks/tess-initial-model-fit.ipynb index 6340166d..6dfac6d7 100644 --- a/stellarphot/notebooks/tess-intial-model-fit.ipynb +++ b/stellarphot/notebooks/tess-initial-model-fit.ipynb @@ -69,6 +69,16 @@ "### Get just the target star and some information about it" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if np.isnan(inp_photometry['bjd']).all():\n", + " inp_photometry.add_bjd_col()\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -198,7 +208,7 @@ "binned2 = aggregate_downsample(ts2, time_bin_size=model_options.bin_size * u.min, aggregate_func=add_quad)\n", "\n", "binned[\"normalized_flux_error\"] = binned2[\"normalized_flux_error\"]\n", - "# binned_time = BinnedTimeSeries(photometry['bjd'], time_bin_start=first_time, time_bin_end=last_time, time_bin_size=bin_size)" + "binned = binned[~np.isnan(binned[\"normalized_flux\"])]" ] }, { @@ -389,7 +399,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.9" } }, "nbformat": 4, From 572f85cb4a933ac92e2d9df00d2679ad8a2817e2 Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Sat, 30 Nov 2024 20:14:51 -1000 Subject: [PATCH 3/6] update fancy plot notebook --- .../06b-transit-fit-template-fancy-plot.ipynb | 135 ++++++------------ 1 file changed, 43 insertions(+), 92 deletions(-) diff --git a/stellarphot/notebooks/photometry/06b-transit-fit-template-fancy-plot.ipynb b/stellarphot/notebooks/photometry/06b-transit-fit-template-fancy-plot.ipynb index 527778fb..07b39bcf 100644 --- a/stellarphot/notebooks/photometry/06b-transit-fit-template-fancy-plot.ipynb +++ b/stellarphot/notebooks/photometry/06b-transit-fit-template-fancy-plot.ipynb @@ -2,21 +2,18 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "tags": [] }, "outputs": [], "source": [ - "from itertools import product\n", - "\n", "import numpy as np\n", "\n", "from astropy.timeseries import TimeSeries, aggregate_downsample\n", "from astropy.time import Time\n", - "from astropy.table import Table, Column\n", + "from astropy.table import Column\n", "from astropy import units as u\n", - "from matplotlib import pyplot as plt\n", "\n", "from stellarphot.transit_fitting import TransitModelFit, TransitModelOptions\n", "from stellarphot.io import TOI\n", @@ -45,6 +42,36 @@ "taic" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Fit settings\n", + "\n", + "+ Do any detrending by a covariate?\n", + "+ Which parameters are fixed?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# These affect the fitting that is done\n", + "\n", + "model_options = TransitModelOptions(\n", + " bin_size=5, # minutes\n", + " keep_transit_time_fixed=True,\n", + " keep_radius_planet_fixed=False,\n", + " keep_radius_orbit_fixed=False,\n", + " fit_airmass=False,\n", + " fit_spp=False,\n", + ")\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -75,37 +102,24 @@ "metadata": {}, "outputs": [], "source": [ - "photometry = inp_photometry.lightcurve_for(1, flux_column=\"relative_flux\", passband=taic.passband).remove_nans()" + "if np.isnan(inp_photometry['bjd']).all():\n", + " inp_photometry.add_bjd_col()" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ - "### You may need to alter some of the settings here" + "photometry = inp_photometry.lightcurve_for(1, flux_column=\"relative_flux\", passband=taic.passband).remove_nans()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Fit settings\n", - "\n", - "+ Do any detrending by a covariate?\n", - "+ Which parameters are fixed?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# These affect the fitting that is done\n", - "\n", - "model_options = TransitModelOptions()" + "### You may need to alter some of the settings here" ] }, { @@ -198,7 +212,8 @@ "binned2 = aggregate_downsample(ts2, time_bin_size=model_options.bin_size * u.min, aggregate_func=add_quad)\n", "\n", "binned[\"normalized_flux_error\"] = binned2[\"normalized_flux_error\"]\n", - "# binned_time = BinnedTimeSeries(photometry['bjd'], time_bin_start=first_time, time_bin_end=last_time, time_bin_size=bin_size)" + "\n", + "binned = binned[~np.isnan(binned[\"normalized_flux\"])]" ] }, { @@ -262,22 +277,6 @@ "### Look at the results" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "plt.plot(mod.times, mod.data, \".\")\n", - "plt.plot(mod.times, mod.model_light_curve())\n", - "plt.vlines(start.jd - 2400000, 0.98, 1.02, colors=\"r\", linestyle=\"--\", alpha=0.5)\n", - "plt.vlines(end.jd - 2400000, 0.98, 1.02, colors=\"r\", linestyle=\"--\", alpha=0.5)\n", - "plt.title(\"Data and fit\")\n", - "plt.grid()" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -321,7 +320,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Make the big plot" + "### Model fit with binning" ] }, { @@ -340,54 +339,6 @@ ")" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Attempt to calculate BIC, but...this seems to have side effects on the rest of notebook " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "def evaluate_fits(mod):\n", - " BICs = []\n", - " settings = []\n", - " all_trendable = mod._all_detrend_params\n", - " tf_sequence = product([True, False], repeat=len(all_trendable))\n", - " for fixed in tf_sequence:\n", - " this_summary = []\n", - " for param, fix in zip(all_trendable, fixed):\n", - " trend_mod = getattr(mod.model, f\"{param}_trend\")\n", - " if fix:\n", - " setattr(mod.model, f\"{param}_trend\", 0.0)\n", - " trend_mod.fixed = fix\n", - " this_summary.append(f\"{param}: {not fix}\")\n", - "\n", - " settings.append(\", \".join(this_summary))\n", - " mod.fit()\n", - " BICs.append(mod.BIC)\n", - " return Table(data=[settings, BICs], names=[\"Fit this param?\", \"BIC\"])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "bic_table = evaluate_fits(mod)\n", - "bic_table.sort(\"BIC\")\n", - "bic_table" - ] - }, { "cell_type": "code", "execution_count": null, @@ -412,7 +363,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.9" } }, "nbformat": 4, From 9cabbd8c087c503bc56328a591384bdd678b23d6 Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Sat, 30 Nov 2024 20:17:28 -1000 Subject: [PATCH 4/6] Rename notebook and add to the launcher --- .jp_app_launcher_stellarphot.yaml | 8 ++++++++ ...plate-fancy-plot.ipynb => tess-second-model-fit.ipynb} | 0 2 files changed, 8 insertions(+) rename stellarphot/notebooks/{photometry/06b-transit-fit-template-fancy-plot.ipynb => tess-second-model-fit.ipynb} (100%) diff --git a/.jp_app_launcher_stellarphot.yaml b/.jp_app_launcher_stellarphot.yaml index 6e67a623..ce7ac318 100644 --- a/.jp_app_launcher_stellarphot.yaml +++ b/.jp_app_launcher_stellarphot.yaml @@ -59,3 +59,11 @@ cwd: not_used type: notebook catalog: Stellarphot analysis and tools + +- title: Second exoplanet model fit + description: Second transit model fit + source: ../stellarphot/tess-second-model-fit.ipynb + icon: ../stellarphot/stellarphot-logo.svg + cwd: not_used + type: notebook + catalog: Stellarphot analysis and tools diff --git a/stellarphot/notebooks/photometry/06b-transit-fit-template-fancy-plot.ipynb b/stellarphot/notebooks/tess-second-model-fit.ipynb similarity index 100% rename from stellarphot/notebooks/photometry/06b-transit-fit-template-fancy-plot.ipynb rename to stellarphot/notebooks/tess-second-model-fit.ipynb From c024b5fb8c031357a5155050df88fc379dc651a0 Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Sat, 30 Nov 2024 20:31:31 -1000 Subject: [PATCH 5/6] Update exotic notebook --- .../07-transit-fit-with-exotic.ipynb | 86 +++++++++++++++---- 1 file changed, 69 insertions(+), 17 deletions(-) diff --git a/stellarphot/notebooks/photometry/07-transit-fit-with-exotic.ipynb b/stellarphot/notebooks/photometry/07-transit-fit-with-exotic.ipynb index 9c781bc7..8a97a57d 100644 --- a/stellarphot/notebooks/photometry/07-transit-fit-with-exotic.ipynb +++ b/stellarphot/notebooks/photometry/07-transit-fit-with-exotic.ipynb @@ -10,15 +10,16 @@ "from pathlib import Path\n", "import pickle\n", "\n", - "import numpy as np\n", + "import numpy as np \n", "\n", - "from astropy.table import Table\n", + "from astropy.table import Table \n", "\n", "#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" + "from stellarphot.io import TOI\n", + "from stellarphot.gui_tools.photometry_widget_functions import TessAnalysisInputControls" ] }, { @@ -30,15 +31,49 @@ "This notebook will do a different kind of fit to your exoplanet data. It takes longer to run than the fitting we've done so far, which is why we didn't begin with this type of fit." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 0. Get some data\n", + "\n", + "+ Select photometry file with relative flux\n", + "+ Select passband\n", + "+ Select TESS info file" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "fits_opener = FitsOpener(title=\"Select your photometry/relative flux file\", filter_pattern=[\"*.ecsv\"])\n", + "taic = TessAnalysisInputControls()\n", + "taic" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# 👉 File with photometry, including flux\n", + "photometry_file = taic.photometry_data_file\n", + "inp_photometry = taic.phot_data\n", "\n", - "fits_opener.file_chooser" + "# 👉 File with exoplanet info in\n", + "tess_info_output_file = taic.tic_info_file\n", + "tess_info = TOI.model_validate_json(tess_info_output_file.read_text())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Get just the target star and some information about it" ] }, { @@ -47,10 +82,17 @@ "metadata": {}, "outputs": [], "source": [ - "input_data_file = fits_opener.path\n", - "toi_info_file = Path(\"TIC-194461202-info.json\")\n", - "\n", - "tess_info = TOI.model_validate_json(toi_info_file.read_text())" + "if np.isnan(inp_photometry['bjd']).all():\n", + " inp_photometry.add_bjd_col()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "photometry = inp_photometry.lightcurve_for(1, flux_column=\"relative_flux\", passband=taic.passband).remove_nans()" ] }, { @@ -70,12 +112,8 @@ "metadata": {}, "outputs": [], "source": [ - "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 = photometry['bjd', 'relative_flux', 'relative_flux_error', 'airmass']\n", "out_data.write(exotic_data_file, overwrite=True)" ] }, @@ -90,6 +128,13 @@ "populate_TOI_boxes(tess_info, settings.value_widget)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## YOU MAY NEED DATA FROM HERE: https://exofop.ipac.caltech.edu/tess/\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -145,9 +190,16 @@ "metadata": {}, "outputs": [], "source": [ - "!exotic --override --pre TIC_194461202-2024-10-08-SR.json" + "!exotic --override --pre TIC_370981403-2024-10-18-SG.json" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -158,7 +210,7 @@ ], "metadata": { "kernelspec": { - "display_name": "stelldev-pyd2", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -172,7 +224,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.12.3" } }, "nbformat": 4, From 3c8b98e782672f010066cca34bc3e7de6ee5cfa6 Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Sat, 30 Nov 2024 20:33:43 -1000 Subject: [PATCH 6/6] Rename exotic notebook and add to launcher --- .jp_app_launcher_stellarphot.yaml | 8 ++++++++ ...ransit-fit-with-exotic.ipynb => tess-EXOTIC-fit.ipynb} | 0 2 files changed, 8 insertions(+) rename stellarphot/notebooks/{photometry/07-transit-fit-with-exotic.ipynb => tess-EXOTIC-fit.ipynb} (100%) diff --git a/.jp_app_launcher_stellarphot.yaml b/.jp_app_launcher_stellarphot.yaml index ce7ac318..d1530c4b 100644 --- a/.jp_app_launcher_stellarphot.yaml +++ b/.jp_app_launcher_stellarphot.yaml @@ -67,3 +67,11 @@ cwd: not_used type: notebook catalog: Stellarphot analysis and tools + +- title: Exoplanet model fit with EXOTIC + description: The final exoplanet fit + source: ../stellarphot/tess-EXOTIC-fit.ipynb + icon: ../stellarphot/stellarphot-logo.svg + cwd: not_used + type: notebook + catalog: Stellarphot analysis and tools diff --git a/stellarphot/notebooks/photometry/07-transit-fit-with-exotic.ipynb b/stellarphot/notebooks/tess-EXOTIC-fit.ipynb similarity index 100% rename from stellarphot/notebooks/photometry/07-transit-fit-with-exotic.ipynb rename to stellarphot/notebooks/tess-EXOTIC-fit.ipynb