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

GSOC: add from hdf method to spectrum class #2995

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Josh Shields <[email protected]>
Karan Desai <[email protected]>
Karan Desai <[email protected]> karandesai-96 <[email protected]>

Karthik Rishinarada <[email protected]>

Kaushik Varanasi <[email protected]>
Kaushik Varanasi <[email protected]> kaushik94 <[email protected]>

Expand Down
31 changes: 30 additions & 1 deletion docs/physics/spectrum/basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,35 @@
"spectrum.plot(mode='wavelength')"
]
},
{
"cell_type": "markdown",
"id": "4a34a1ba",
"metadata": {},
"source": [
"Inorder to retrieve your Spectrum class from an hdf file, follow the below steps"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abef6bb1",
"metadata": {},
"outputs": [],
"source": [
"spectrum.to_hdf(\"test.hdf\", overwrite=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2eb89482",
"metadata": {},
"outputs": [],
"source": [
"obj = spectrum.from_hdf_to_class(\"test.hdf\")\n",
"print(obj._frequency == spectrum._frequency)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand Down Expand Up @@ -460,7 +489,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.12.0"
}
},
"nbformat": 4,
Expand Down
15 changes: 15 additions & 0 deletions tardis/spectrum/spectrum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import warnings

import numpy as np
import pandas as pd
from astropy import units as u

from tardis.io.util import HDFWriterMixin
Expand Down Expand Up @@ -151,6 +152,20 @@ def plot(self, ax=None, mode="wavelength", **kwargs):
else:
warnings.warn(f"Did not find plotting mode {mode}, doing nothing.")

def from_hdf_to_class(self,hdf_path):
try:
with pd.HDFStore(hdf_path, mode="r") as store:
_freq = store['/tardis_spectrum/_frequency']
_f = np.array(_freq) * u.Hz

_lumi = store['/tardis_spectrum/luminosity']
_l = np.array(_lumi) * (u.erg/u.s)

return TARDISSpectrum(_f, _l)

except FileNotFoundError as e:
print(f"File not found: {e}")

def to_ascii(self, fname, mode="luminosity_density"):
if mode == "luminosity_density":
np.savetxt(
Expand Down
8 changes: 8 additions & 0 deletions tardis/spectrum/tests/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ def test_f_nu_to_f_lambda(spectrum):
spectrum.luminosity_density_lambda.value, expected.value
)


def test_from_hdf_to_class(spectrum):
actual = TARDISSpectrum(spectrum._frequency, spectrum.luminosity)
actual.to_hdf("test.hdf", overwrite=True)
expected = actual.from_hdf_to_class("test.hdf")

compare_spectra(actual, expected)


###
# Save and Load
Expand Down
Loading