Skip to content

Commit

Permalink
Use stdatamodels
Browse files Browse the repository at this point in the history
TST: Update test_parse_jwst_niriss_grism
because asdf_in_fits do the invalid WCS handling natively now.
  • Loading branch information
pllim committed Mar 9, 2023
1 parent 50edc29 commit 8046f48
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ body:
import scipy; print("scipy", scipy.__version__)
import skimage; print("scikit-image", skimage.__version__)
import asdf; print("asdf", asdf.__version__)
import stdatamodels; print("stdatamodels", stdatamodels.__version__)
import gwcs; print("gwcs", gwcs.__version__)
import regions; print("regions", regions.__version__)
import specutils; print("specutils", specutils.__version__)
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Cubeviz
Imviz
^^^^^

- ASDF parser for JWST images now uses ``stdatamodels``. [#2052]

Mosviz
^^^^^^

Expand Down
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ def pytest_configure(config):
PYTEST_HEADER_MODULES['vispy'] = 'vispy'
PYTEST_HEADER_MODULES['gwcs'] = 'gwcs'
PYTEST_HEADER_MODULES['asdf'] = 'asdf'
PYTEST_HEADER_MODULES['stdatamodels'] = 'stdatamodels'

TESTED_VERSIONS['jdaviz'] = __version__
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ def get_comment(key):
self.reset()


# TODO: If this is natively supported by asdf in the future, replace with native function.
# TODO: If this generalized in stdatamodels in the future, replace with native function.
# See https://github.com/spacetelescope/stdatamodels/issues/131
# This code below is taken code from stdatamodels/model_base.py, and the method to_flat_dict()
def flatten_nested_dict(asdfnode, include_arrays=True):
"""
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/configs/imviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def load_data(self, data, data_label=None, do_link=True, show_in_viewer=True, **
* ``'filename.fits[SCI,2]'`` (loads the second SCI extension)
* ``'filename.jpg'`` (requires ``scikit-image``; grayscale only)
* ``'filename.png'`` (requires ``scikit-image``; grayscale only)
* JWST ASDF-in-FITS file (requires ``asdf`` and ``gwcs``; ``data`` or given
* JWST ASDF-in-FITS file (requires ``stdatamodels`` and ``gwcs``; ``data`` or given
``ext`` + GWCS)
* `~astropy.io.fits.HDUList` object (first image extension found
is loaded unless ``ext`` keyword is also given)
Expand Down
4 changes: 2 additions & 2 deletions jdaviz/configs/imviz/plugins/parsers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os

import numpy as np
from asdf.fits_embed import AsdfInFits
from astropy import units as u
from astropy.io import fits
from astropy.nddata import NDData
from astropy.wcs import WCS
from glue.core.data import Component, Data
from gwcs.wcs import WCS as GWCS
from stdatamodels import asdf_in_fits

from jdaviz.core.registries import data_parser_registry
from jdaviz.core.events import SnackbarMessage
Expand Down Expand Up @@ -229,7 +229,7 @@ def _jwst2data(file_obj, ext, data_label):

try:
# This is very specific to JWST pipeline image output.
with AsdfInFits.open(file_obj) as af:
with asdf_in_fits.open(file_obj) as af:
dm = af.tree
dm_meta = af.tree["meta"]
data.meta.update(standardize_metadata(dm_meta))
Expand Down
16 changes: 7 additions & 9 deletions jdaviz/configs/imviz/tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
import pytest
from asdf.fits_embed import AsdfInFits
from astropy import units as u
from astropy.io import fits
from astropy.nddata import NDData, StdDevUncertainty
Expand All @@ -11,6 +10,7 @@
from numpy.testing import assert_allclose, assert_array_equal
from regions import CirclePixelRegion, RectanglePixelRegion
from skimage.io import imsave
from stdatamodels import asdf_in_fits

from jdaviz.configs.imviz.helper import split_filename_with_fits_ext
from jdaviz.configs.imviz.plugins.parsers import (
Expand Down Expand Up @@ -221,14 +221,13 @@ def test_filelist(self, imviz_helper, tmp_path):
with pytest.raises(ValueError, match='Do not manually overwrite data_label'):
imviz_helper.load_data(flist, data_label='foo', show_in_viewer=False)

def test_parse_asdf_in_fits_4d(self, imviz_helper, tmpdir):
def test_parse_asdf_in_fits_4d(self, imviz_helper, tmp_path):
hdulist = fits.HDUList([
fits.PrimaryHDU(),
fits.ImageHDU(np.zeros((1, 2, 5, 5)), name='SCI')])
tree = {'data': {'data': hdulist['SCI'].data}}
filename = str(tmpdir.join('myasdf.fits'))
ff = AsdfInFits(hdulist, tree)
ff.write_to(filename, overwrite=True)
filename = str(tmp_path / 'myasdf.fits')
asdf_in_fits.write(filename, tree, hdulist=hdulist, overwrite=True)

with pytest.raises(ValueError, match='Imviz cannot load this HDU'):
parse_data(imviz_helper.app, filename)
Expand Down Expand Up @@ -360,16 +359,15 @@ def test_parse_jwst_nircam_level2(self, imviz_helper):

@pytest.mark.remote_data
def test_parse_jwst_niriss_grism(self, imviz_helper):
"""No valid image GWCS for Imviz, will fall back to FITS loading without WCS."""
"""No valid image GWCS for Imviz, will fall back to loading without WCS."""
filename = download_file(self.jwst_asdf_url_2, cache=True)

imviz_helper.load_data(filename, show_in_viewer=False)
data = imviz_helper.app.data_collection[0]
comp = data.get_component('SCI,1')
assert data.label == 'contents[SCI,1]' # download_file returns cache loc
comp = data.get_component('DATA')
assert data.label == 'contents[DATA]' # download_file returns cache loc
assert data.shape == (2048, 2048)
assert data.coords is None
assert data.meta['RADESYS'] == 'ICRS'
assert comp.units == 'DN/s'
assert comp.data.shape == (2048, 2048)

Expand Down
1 change: 1 addition & 0 deletions jdaviz/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,6 @@ def pytest_configure(config):
PYTEST_HEADER_MODULES['vispy'] = 'vispy'
PYTEST_HEADER_MODULES['gwcs'] = 'gwcs'
PYTEST_HEADER_MODULES['asdf'] = 'asdf'
PYTEST_HEADER_MODULES['stdatamodels'] = 'stdatamodels'

TESTED_VERSIONS['jdaviz'] = __version__
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ install_requires =
# vispy is an indirect dependency, but older vispy's don't play nice with jdaviz install
vispy>=0.6.5
asdf>=2.14.3
# TODO: update stdatamodels to version without stcal
stdatamodels>=1.1
gwcs>=0.16.1
regions>=0.6
scikit-image
Expand Down Expand Up @@ -98,7 +100,6 @@ filterwarnings =
ignore:.*With traitlets 4\.1, metadata should be set using the \.tag\(\) method:DeprecationWarning
ignore:Widget.* is deprecated:DeprecationWarning
ignore:zmq\.eventloop\.ioloop is deprecated in pyzmq:DeprecationWarning
ignore:AsdfInFits has been deprecated
ignore:The unit 'Angstrom' has been deprecated in the VOUnit standard\. Suggested.* 0\.1nm\.
ignore:specutils uses the deprecated entry point asdf_extensions
ignore::DeprecationWarning:glue
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ deps =
devdeps: git+https://github.com/astropy/photutils.git
devdeps: git+https://github.com/spacetelescope/gwcs.git
devdeps: git+https://github.com/asdf-format/asdf.git
devdeps: git+https://github.com/spacetelescope/stdatamodels.git
devdeps: git+https://github.com/bqplot/[email protected]
devdeps: git+https://github.com/glue-viz/glue.git
devdeps: git+https://github.com/voila-dashboards/voila.git
Expand Down

0 comments on commit 8046f48

Please sign in to comment.