Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SN Ia e2e test to use oversampled opsim #132

Merged
merged 7 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 148 additions & 60 deletions docs/notebooks/test_snia.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,50 @@
{
"cell_type": "code",
"execution_count": null,
"id": "1bd86f47-7061-4aff-a7e0-be226219b139",
"metadata": {},
"id": "f2dc23f31b3601e1",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:20.170140Z",
"start_time": "2024-09-26T15:38:20.167668Z"
}
},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
"# %load_ext autoreload\n",
"# %autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4bf6fe10-59dc-439e-9371-c0cc15755f62",
"metadata": {},
"id": "4e459a387df01a7c",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:20.177116Z",
"start_time": "2024-09-26T15:38:20.174746Z"
}
},
"outputs": [],
"source": [
"import sys\n",
"from pathlib import Path\n",
"import tdastro\n",
"\n",
"sys.path.append(\n",
" str((Path(tdastro.__file__).parent / \"..\" / \"..\" / \"tests\" / \"tdastro\" / \"sources\").resolve())\n",
")"
"test_path = Path(tdastro.__file__).parent.parent.parent / \"tests\" / \"tdastro\"\n",
"\n",
"sys.path.append(str(test_path.resolve()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "817f5d23-925d-4ef7-bc22-ad110020e57d",
"metadata": {},
"id": "824593e5f3032eb6",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:20.667800Z",
"start_time": "2024-09-26T15:38:20.233254Z"
}
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
Expand All @@ -42,30 +57,48 @@
{
"cell_type": "code",
"execution_count": null,
"id": "c87c4488-9368-4a0a-8400-1596bb15492a",
"metadata": {},
"id": "3742a30a04a1b62c",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:20.797258Z",
"start_time": "2024-09-26T15:38:20.672792Z"
}
},
"outputs": [],
"source": [
"from test_snia import test_snia_end2end"
"import conftest\n",
"from sources.test_snia import test_snia_end2end"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3285d471-c454-4c7a-b7cb-b8a79ab7ec98",
"metadata": {},
"id": "58a2bc03f3a3a3a0",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:47.963561Z",
"start_time": "2024-09-26T15:38:20.802799Z"
}
},
"outputs": [],
"source": [
"res = test_snia_end2end(\n",
" None, opsim_db_file=None, opsim=False, nsample=10, return_result=True, phase_rest=np.linspace(-15, 45, 20)\n",
"res, passbands = test_snia_end2end(\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference would be not to use test_snia_end2end here and instead repeat the code in the notebook. I know code repetition is usually considered bad, but I think the code of interest to the notebook user will be what is in that function. Leaving the code out of the notebook doesn't provide the user all the information.

Alternatively, if we really want to keep code in sync, we could break out modules into new files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have planned to update notebook to show how model is defined with more text descriptions by breaking the end2end test into multiple steps. Right now I prefer to keep it this way so I don't need to change the same thing in two different places when experimenting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this, but I've just keep the same NB structure as it was before. I'd keep these changes out of the scope of this PR or ask someone to help me commiting/PRing these changes

" conftest.oversampled_observations(conftest.opsim_shorten(conftest.test_data_dir())),\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few different approaches we could use to access the opsim_shorten here without having to change the pytest fixtures in the conftest.py file.

  1. [My preference] Move the directory names to the top level __init__.py file (or a new paths.py file in the base directory):
_TDASTRO_BASE_DIR = os.path.dirname(__file__)
_TDASTRO_TEST_DIR = os.path.join(_TDASTRO_BASE_DIR, "tests", "tdastro")
_TDASTRO_TEST_DATA_DIR = os.path.join(_TDASTRO_TEST_DIR, "data")

Then both the notebooks and conftest.py use these.

We would need to include a cell to do:

base_opsim = OpSim.from_db(opsim_shorten)
oversampled_observations = oversample_opsim(
        base_opsim,
        pointing=(0.0, 0.0),
        search_radius=180.0,
        delta_t=0.01,
        time_range=(61406.0, 61771.0),
        bands=None,
        strategy="darkest_sky",
    )

but I think it would be good to explicitly include that in the notebook. It allows the user to see where you are pulling the data and how you are transforming it.

  1. [Not prefered] I think in notebooks it is fair to just provide a path like
test_opsim_filename = "../../tests/tdastro/data/opsim_shorten.db")

It's not as robust as the approach above, but that is okay since users are expected to interact with the notebooks.

" passbands_dir=conftest.passbands_dir(conftest.test_data_dir()),\n",
" nsample=20,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c60e849f-8529-4714-92e1-345182a12a68",
"metadata": {},
"id": "5c4b0574aec01df4",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:47.971555Z",
"start_time": "2024-09-26T15:38:47.969424Z"
}
},
"outputs": [],
"source": [
"hostmass = [x[\"parameter_values\"][\"hostmass\"] for x in res]\n",
Expand All @@ -80,8 +113,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "57607e1e-e18a-4408-a7b9-ec400c72c9a8",
"metadata": {},
"id": "aaefb2f29d444cf2",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:48.053565Z",
"start_time": "2024-09-26T15:38:47.976849Z"
}
},
"outputs": [],
"source": [
"plt.hist(hostmass)"
Expand All @@ -90,8 +128,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "561c6c8c-84d9-484d-9759-56b020a018d6",
"metadata": {},
"id": "b86d82fe1a518d5a",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:48.225753Z",
"start_time": "2024-09-26T15:38:48.093211Z"
}
},
"outputs": [],
"source": [
"plt.hist(x1)\n",
Expand All @@ -101,8 +144,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "8addd896-8f21-4813-8053-b75debf114b2",
"metadata": {},
"id": "edc24806f5752b25",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:48.299469Z",
"start_time": "2024-09-26T15:38:48.236483Z"
}
},
"outputs": [],
"source": [
"plt.hist(c)"
Expand All @@ -111,8 +159,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "bd1bbdd5-f236-4e6d-84f3-161ab6cca4fd",
"metadata": {},
"id": "35ab73410bd98ac1",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:48.352012Z",
"start_time": "2024-09-26T15:38:48.305324Z"
}
},
"outputs": [],
"source": [
"plt.scatter(hostmass, x1)"
Expand All @@ -121,8 +174,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "151bd30a-26f4-40eb-ba99-611f82d61edf",
"metadata": {},
"id": "2b56df734bccfe1b",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:48.411671Z",
"start_time": "2024-09-26T15:38:48.362721Z"
}
},
"outputs": [],
"source": [
"plt.hist(x0)"
Expand All @@ -131,8 +189,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3b9e3896-c530-419c-8bb0-a9c1984eae0d",
"metadata": {},
"id": "1140a75e38023d80",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:48.467349Z",
"start_time": "2024-09-26T15:38:48.417896Z"
}
},
"outputs": [],
"source": [
"plt.hist(z)"
Expand All @@ -141,8 +204,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "b31893a6-be21-4c69-8e6c-4cfec316d309",
"metadata": {},
"id": "75b49a8b4f8fa1b4",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:48.583009Z",
"start_time": "2024-09-26T15:38:48.498730Z"
}
},
"outputs": [],
"source": [
"# cosmo = FlatLambdaCDM(H0=73, Om0=0.3)\n",
Expand Down Expand Up @@ -170,8 +238,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d7443d9e-7bbd-4087-b31e-8a4030c32b16",
"metadata": {},
"id": "98996d76d56cb7b9",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:48.700933Z",
"start_time": "2024-09-26T15:38:48.618682Z"
}
},
"outputs": [],
"source": [
"plt.scatter(z, mb)\n",
Expand All @@ -183,8 +256,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "494d4400-c7ef-453d-bfa3-4abea973997e",
"metadata": {},
"id": "320e5ddcec3fcdd",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:48.751128Z",
"start_time": "2024-09-26T15:38:48.706426Z"
}
},
"outputs": [],
"source": [
"plt.scatter(hostmass, mu - distmod)\n",
Expand All @@ -194,8 +272,13 @@
{
"cell_type": "code",
"execution_count": null,
"id": "55a3c72f-a231-4108-b518-779e37502cd7",
"metadata": {},
"id": "4dce3f1ba8792c87",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:48.887113Z",
"start_time": "2024-09-26T15:38:48.757687Z"
}
},
"outputs": [],
"source": [
"for i in range(0, 3):\n",
Expand All @@ -204,7 +287,7 @@
" except Exception:\n",
" continue\n",
" saltpars = {\"x0\": x0[i], \"x1\": x1[i], \"c\": c[i], \"z\": z[i], \"t0\": t0[i]}\n",
" model = sncosmo.Model(\"salt2-h17\")\n",
" model = sncosmo.Model(\"salt3\")\n",
" model.update(saltpars)\n",
" print(saltpars)\n",
" print(model.parameters)\n",
Expand All @@ -217,41 +300,46 @@
{
"cell_type": "code",
"execution_count": null,
"id": "acdcbb01-ead0-4a20-b479-3324ccd7e562",
"metadata": {},
"id": "6e1d0698de0bfaaa",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:49.434973Z",
"start_time": "2024-09-26T15:38:48.893241Z"
}
},
"outputs": [],
"source": [
"for i in range(0, 3):\n",
" phase_obs = res[i][\"phase_rest\"] * (1 + z[i])\n",
" times = res[i][\"times\"]\n",
" colors = \"gr\"\n",
" for color, f in zip(\"gr\", colors):\n",
" plt.plot(\n",
" times, res[i][\"bandfluxes\"][\"LSST_\" + f], \"-\", marker=\"o\", label=f, color=color, alpha=0.6, lw=2\n",
" )\n",
" colors = [\"red\", \"brown\"]\n",
" for f, color in zip(\"ri\", colors):\n",
" band_name = f\"LSST_{f}\"\n",
" plt.plot(times, res[i][\"bandfluxes\"][band_name], \"-\", label=f, color=color, alpha=0.6, lw=2)\n",
" saltpars = {\"x0\": x0[i], \"x1\": x1[i], \"c\": c[i], \"z\": z[i], \"t0\": t0[i]}\n",
" model = sncosmo.Model(\"salt2-h17\")\n",
" model = sncosmo.Model(\"salt3\")\n",
" model.update(saltpars)\n",
" print(saltpars)\n",
" flux = model.bandflux(\"lsst\" + f, times, zpsys=\"ab\", zp=8.9 + 2.5 * 9) # -48.6)\n",
" sncosmo_band = sncosmo.Bandpass(\n",
" *passbands.passbands[band_name].processed_transmission_table.T, name=band_name\n",
" )\n",
" flux = model.bandflux(sncosmo_band, times, zpsys=\"ab\", zp=8.9 + 2.5 * 9) # -48.6)\n",
" plt.plot(times, flux, \"--\", label=f, color=color)\n",
" plt.xlabel(\"MJD\")\n",
" plt.ylabel(\"Flux, nJy\")\n",
" plt.legend()\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d95053a7-1e5c-4d36-8579-82b3dd8b9cbb",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc680d32-cc9c-428f-90c4-944649999d9f",
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-26T15:38:49.443348Z",
"start_time": "2024-09-26T15:38:49.441986Z"
}
},
"outputs": [],
"source": []
}
Expand Down
13 changes: 13 additions & 0 deletions tests/tdastro/astro_utils/test_opsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,16 @@ def test_oversample_opsim(opsim_shorten):
oversampled["skyBrightness"].unique().size >= oversampled["filter"].unique().size
), "there should be at least as many skyBrightness values as bands"
assert oversampled["skyBrightness"].isna().sum() == 0, "skyBrightness has NaN values"


def test_fixture_oversampled_observations(oversampled_observations):
"""Test the fixture oversampled_observations."""
assert len(oversampled_observations) == 36_500
assert set(oversampled_observations["filter"]) == {"g", "r"}
assert oversampled_observations["skyBrightness"].isna().sum() == 0
assert oversampled_observations["skyBrightness"].unique().size >= 2
assert np.all(oversampled_observations["observationStartMJD"] >= 61406.0)
assert np.all(oversampled_observations["observationStartMJD"] <= 61771.0)
np.testing.assert_allclose(oversampled_observations["fieldRA"], 0.0)
np.testing.assert_allclose(oversampled_observations["fieldDec"], 0.0)
np.testing.assert_allclose(np.diff(oversampled_observations["observationStartMJD"]), 0.01)
Loading
Loading