Skip to content

Commit

Permalink
Address review comments from kecnry
Browse files Browse the repository at this point in the history
  • Loading branch information
pllim authored and kecnry committed Feb 27, 2023
1 parent f8a0bd8 commit ad0d3e0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/cubeviz/export_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where the mask (if available) is as defined in

.. code-block:: python
mydata.write("mydata.fits", format="jdaviz-cube-writer")
mydata.write("mydata.fits", format="jdaviz-cube")
Data can also be accessed directly from ``data_collection`` using the following code:

Expand Down
9 changes: 6 additions & 3 deletions jdaviz/configs/cubeviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,14 @@ def jdaviz_cube_fitswriter(spectrum, file_name, **kwargs):
1 SCI 1 ImageHDU (float32)
2 MASK 1 ImageHDU (uint16)
The FITS file generated by this writer does not need a
custom reader to be read back into Spectrum1D.
Examples
--------
To write out a Spectrum1D cube using this writer:
>>> spec.write("my_output.fits", format="jdaviz-cube-writer", overwrite=True) # doctest: +SKIP
>>> spec.write("my_output.fits", format="jdaviz-cube", overwrite=True) # doctest: +SKIP
"""
pri_hdu = fits.PrimaryHDU()
Expand Down Expand Up @@ -183,7 +186,7 @@ def jdaviz_cube_fitswriter(spectrum, file_name, **kwargs):

if _astropy_has_priorities():
kwargs = {"priority": 0}
else:
else: # pragma: no cover
kwargs = {}
io_registry.register_writer(
"jdaviz-cube-writer", Spectrum1D, jdaviz_cube_fitswriter, force=False, **kwargs)
"jdaviz-cube", Spectrum1D, jdaviz_cube_fitswriter, force=False, **kwargs)
2 changes: 1 addition & 1 deletion jdaviz/configs/cubeviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _return_spectrum_with_correct_units(flux, wcs, metadata, data_type, target_w
for cunit_num in (3, 1):
cunit_key = f"CUNIT{cunit_num}"
ctype_key = f"CTYPE{cunit_num}"
if not found_target and cunit_key in hdr and 'WAVE' in hdr[ctype_key]:
if cunit_key in hdr and 'WAVE' in hdr[ctype_key]:
target_wave_unit = u.Unit(hdr[cunit_key])
found_target = True
break
Expand Down
28 changes: 25 additions & 3 deletions jdaviz/configs/default/plugins/model_fitting/tests/test_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from astropy import units as u
from astropy.io import fits
from astropy.io.registry.base import IORegistryError
from astropy.modeling import models, parameters as params
from astropy.nddata import StdDevUncertainty
from astropy.wcs import WCS
Expand Down Expand Up @@ -116,7 +117,7 @@ def test_fitting_backend(unc):


@pytest.mark.parametrize('unc', ('zeros', None))
def test_cube_fitting_backend(unc, tmp_path):
def test_cube_fitting_backend(cubeviz_helper, unc, tmp_path):
np.random.seed(42)

SIGMA = 0.1 # noise in data
Expand Down Expand Up @@ -226,13 +227,14 @@ def test_cube_fitting_backend(unc, tmp_path):

# Check I/O roundtrip.
out_fn = tmp_path / "fitted_cube.fits"
fitted_spectrum.write(out_fn, format="jdaviz-cube-writer", overwrite=True)
fitted_spectrum.write(out_fn, format="jdaviz-cube", overwrite=True)
flux_unit_str = fitted_spectrum.flux.unit.to_string(format="fits")
coo_expected = fitted_spectrum.wcs.pixel_to_world(1, 0, 2)
with fits.open(out_fn) as pf:
assert len(pf) == 3
assert pf[0].name == "PRIMARY"
assert pf[1].name == "SCI"
assert pf[1].header["BUNIT"] == fitted_spectrum.flux.unit.to_string(format="fits")
assert pf[1].header["BUNIT"] == flux_unit_str
assert_allclose(pf[1].data, fitted_spectrum.flux.value)
assert pf[2].name == "MASK"
assert_array_equal(pf[2].data, mask)
Expand All @@ -241,3 +243,23 @@ def test_cube_fitting_backend(unc, tmp_path):
assert_allclose(coo[0].value, coo_expected[0].value) # SpectralCoord
assert_allclose([coo[1].ra.deg, coo[1].dec.deg],
[coo_expected[1].ra.deg, coo_expected[1].dec.deg])

# Our custom format is not registered to readers, just writers.
# You can read it back in without custom read. See "Cubeviz roundtrip" below.
with pytest.raises(IORegistryError, match="No reader defined"):
Spectrum1D.read(out_fn, format="jdaviz-cube")

# Check Cubeviz roundtrip.
cubeviz_helper.load_data(out_fn)
assert len(cubeviz_helper.app.data_collection) == 2
data_sci = cubeviz_helper.app.data_collection["fitted_cube.fits[SCI]"]
flux_sci = data_sci.get_component("flux")
assert_allclose(flux_sci.data, fitted_spectrum.flux.value)
assert flux_sci.units == flux_unit_str
coo = data_sci.coords.pixel_to_world(1, 0, 2)
assert_allclose(coo[0].value, coo_expected[0].value) # SpectralCoord
assert_allclose([coo[1].ra.deg, coo[1].dec.deg],
[coo_expected[1].ra.deg, coo_expected[1].dec.deg])
data_mask = cubeviz_helper.app.data_collection["fitted_cube.fits[MASK]"]
flux_mask = data_mask.get_component("flux")
assert_array_equal(flux_mask.data, mask)

0 comments on commit ad0d3e0

Please sign in to comment.