From 305678a62e41d10e0032bafbfa79b13598089d30 Mon Sep 17 00:00:00 2001 From: Matt Craig Date: Mon, 25 Nov 2024 10:15:36 -0600 Subject: [PATCH] Move time filtering to a function and out of notebook --- .../gui_tools/photometry_widget_functions.py | 47 ++++++++ .../photometry/06-transit-fit-template.ipynb | 105 ++++++------------ 2 files changed, 78 insertions(+), 74 deletions(-) diff --git a/stellarphot/gui_tools/photometry_widget_functions.py b/stellarphot/gui_tools/photometry_widget_functions.py index b04f4be4..7391fd13 100644 --- a/stellarphot/gui_tools/photometry_widget_functions.py +++ b/stellarphot/gui_tools/photometry_widget_functions.py @@ -85,6 +85,53 @@ def passband(self): return self._passband.value +def filter_by_dates( + phot_times=None, + use_no_data_before=None, + use_no_data_between=None, + use_no_data_after=None, +): + n_dropped = 0 + + bad_data = phot_times < use_no_data_before + + n_dropped = bad_data.sum() + + if n_dropped > 0: + print( + f"👉👉👉👉 Dropping {n_dropped} data points before " + f"BJD {use_no_data_before}" + ) + + bad_data = bad_data | ( + (use_no_data_between[0][0] < phot_times) + & (phot_times < use_no_data_between[0][1]) + ) + + new_dropped = bad_data.sum() - n_dropped + + if new_dropped: + print( + f"👉👉👉👉 Dropping {new_dropped} data points between " + f"BJD {use_no_data_between[0][0]} and {use_no_data_between[0][1]}" + ) + + n_dropped += new_dropped + + bad_data = bad_data | (phot_times > use_no_data_after) + + new_dropped = bad_data.sum() - n_dropped + + if new_dropped: + print( + f"👉👉👉👉 Dropping {new_dropped} data points after " + f"BJD {use_no_data_after}" + ) + + n_dropped += new_dropped + return bad_data + + class PhotometrySettingsOLDBAD: """ A class to hold the widgets for photometry settings. diff --git a/stellarphot/notebooks/photometry/06-transit-fit-template.ipynb b/stellarphot/notebooks/photometry/06-transit-fit-template.ipynb index c5ca34d2..f93ed34f 100644 --- a/stellarphot/notebooks/photometry/06-transit-fit-template.ipynb +++ b/stellarphot/notebooks/photometry/06-transit-fit-template.ipynb @@ -21,7 +21,7 @@ "from stellarphot.transit_fitting import TransitModelFit, TransitModelOptions\n", "from stellarphot.io import TOI\n", "from stellarphot.plotting import plot_transit_lightcurve\n", - "from stellarphot.gui_tools.photometry_widget_functions import TessAnalysisInputControls\n" + "from stellarphot.gui_tools.photometry_widget_functions import TessAnalysisInputControls, filter_by_dates\n" ] }, { @@ -78,79 +78,6 @@ "photometry = inp_photometry.lightcurve_for(1, flux_column=\"relative_flux\", passband=taic.passband).remove_nans()" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 👇👇👇 use this to exclude some data (only if needed!) 👇👇👇\n", - "\n", - "Option to filter by date..even though you do not know yet what the data looks like" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "use_no_data_before = Time(2400000, format=\"jd\", scale=\"tdb\")\n", - "\n", - "use_no_data_between = [\n", - " [Time(2400000, format=\"jd\", scale=\"tdb\"), Time(2400000, format=\"jd\", scale=\"tdb\")]\n", - "]\n", - "\n", - "use_no_data_after = Time(2499999, format=\"jd\", scale=\"tdb\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Actually filter out data by date" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "phot_times = Time(photometry[\"bjd\"], format=\"jd\", scale=\"tdb\")\n", - "\n", - "n_dropped = 0\n", - "\n", - "bad_data = phot_times < use_no_data_before\n", - "\n", - "n_dropped = bad_data.sum()\n", - "\n", - "if n_dropped > 0:\n", - " print(f\"👉👉👉👉 Dropping {n_dropped} data points before BJD {use_no_data_before}\")\n", - "\n", - "bad_data = bad_data | (\n", - " (use_no_data_between[0][0] < phot_times) & (phot_times < use_no_data_between[0][1])\n", - ")\n", - "\n", - "new_dropped = bad_data.sum() - n_dropped\n", - "\n", - "if new_dropped:\n", - " print(\n", - " f\"👉👉👉👉 Dropping {new_dropped} data points between BJD {use_no_data_between[0][0]} and {use_no_data_between[0][1]}\"\n", - " )\n", - "\n", - "n_dropped += new_dropped\n", - "\n", - "bad_data = bad_data | (phot_times > use_no_data_after)\n", - "\n", - "new_dropped = bad_data.sum() - n_dropped\n", - "\n", - "if new_dropped:\n", - " print(f\"👉👉👉👉 Dropping {new_dropped} data points after BJD {use_no_data_after}\")\n", - "\n", - "n_dropped += new_dropped\n", - "\n", - "photometry = photometry[~bad_data]" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -352,6 +279,36 @@ "plt.grid()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 👇👇👇 use this to exclude some data (only if needed!) 👇👇👇\n", + "\n", + "Option to filter by date..even though you do not know yet what the data looks like" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "bad_time = filter_by_dates(\n", + " phot_times=photometry[\"bjd\"],\n", + " use_no_data_before=Time(2400000, format=\"jd\", scale=\"tdb\"),\n", + " use_no_data_between=[\n", + " [\n", + " Time(2400000, format=\"jd\", scale=\"tdb\"),\n", + " Time(2400000, format=\"jd\", scale=\"tdb\"),\n", + " ]\n", + " ],\n", + " use_no_data_after=Time(2499999, format=\"jd\", scale=\"tdb\"),\n", + ")\n", + "\n", + "photometry = photometry[~bad_time]" + ] + }, { "cell_type": "code", "execution_count": null,