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

Utilize Jdaviz.open for the CLI notebook #2233

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions jdaviz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from jdaviz.configs.cubeviz import Cubeviz # noqa: F401
from jdaviz.configs.imviz import Imviz # noqa: F401
from jdaviz.utils import enable_hot_reloading # noqa: F401
from jdaviz.core.data_formats import open # noqa: F401

# Clean up namespace.
del os
Expand Down
4 changes: 3 additions & 1 deletion jdaviz/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
__all__ = ['main']

JDAVIZ_DIR = pathlib.Path(__file__).parent.resolve()
DEFAULT_VERBOSITY = 'warning'
DEFAULT_HISTORY_VERBOSITY = 'info'


def main(filepaths=None, layout='default', instrument=None, browser='default',
theme='light', verbosity='warning', history_verbosity='info',
theme='light', verbosity=DEFAULT_VERBOSITY, history_verbosity=DEFAULT_HISTORY_VERBOSITY,
hotreload=False):
"""
Start a Jdaviz application instance with data loaded from FILENAME.
Expand Down
1 change: 1 addition & 0 deletions jdaviz/configs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .cubeviz import * # noqa
from .specviz import * # noqa
from .specviz2d import * # noqa
from .default import * # noqa
from .mosviz import * # noqa
from .imviz import * # noqa
72 changes: 59 additions & 13 deletions jdaviz/core/data_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from stdatamodels import asdf_in_fits

from jdaviz.core.config import list_configurations
from jdaviz import configs as jdaviz_configs
from jdaviz.cli import DEFAULT_VERBOSITY, DEFAULT_HISTORY_VERBOSITY

__all__ = [
'guess_dimensionality',
Expand Down Expand Up @@ -156,6 +158,9 @@ def identify_helper(filename, ext=1):
-------
helper_name : str
Name of the best-guess helper for ``filename``.

Fits HDUList : astropy.io.fits.HDUList
The HDUList of the file opened to identify the helper
"""
supported_dtypes = [
Spectrum1D,
Expand All @@ -167,10 +172,11 @@ def identify_helper(filename, ext=1):
if filename.lower().endswith('asdf'):
# ASDF files are only supported in jdaviz for
# Roman WFI 2D images, so suggest imviz:
return 'imviz'
return ('imviz', None)

header = fits.getheader(filename, ext=ext)
data = fits.getdata(filename, ext=ext)
hdul = fits.open(filename)
data = hdul[ext]
header = data.header
wcs = _get_wcs(filename, header)
has_spectral_axis = 'spectral' in wcs.world_axis_object_classes

Expand Down Expand Up @@ -201,10 +207,10 @@ def identify_helper(filename, ext=1):
# could be 2D spectrum or 2D image. break tie with WCS:
if has_spectral_axis:
if n_axes > 1:
return 'specviz2d'
return 'specviz'
return ('specviz2d', hdul)
return ('specviz', hdul)
elif not isinstance(data, fits.BinTableHDU):
return 'imviz'
return ('imviz', hdul)

# Ensure specviz is chosen when ``data`` is a table or recarray
# and there's a "known" spectral column name:
Expand All @@ -230,7 +236,7 @@ def identify_helper(filename, ext=1):

# if at least one spectral column is found:
if sum(found_spectral_columns):
return 'specviz'
return ('specviz', hdul)

# If the data could be spectral:
for cls in [Spectrum1D, SpectrumList]:
Expand All @@ -240,10 +246,10 @@ def identify_helper(filename, ext=1):
# first catch known JWST spectrum types:
if (n_axes == 3 and
recognized_spectrum_format.find('s3d') > -1):
return 'cubeviz'
return ('cubeviz', hdul)
elif (n_axes == 2 and
recognized_spectrum_format.find('x1d') > -1):
return 'specviz'
return ('specviz', hdul)

# we intentionally don't choose specviz2d for
# data recognized as 's2d' as we did with the cases above,
Expand All @@ -253,11 +259,11 @@ def identify_helper(filename, ext=1):
# Use WCS to break the tie below:
elif n_axes == 2:
if has_spectral_axis:
return 'specviz2d'
return 'imviz'
return ('specviz2d', hdul)
return ('imviz', hdul)

elif n_axes == 1:
return 'specviz'
return ('specviz', hdul)

try:
# try using the specutils registry:
Expand All @@ -269,6 +275,46 @@ def identify_helper(filename, ext=1):

if n_axes == 2 and not has_spectral_axis:
# at this point, non-spectral 2D data are likely images:
return 'imviz'
return ('imviz', hdul)

raise ValueError(f"No helper could be auto-identified for {filename}.")


def open(filename, show=True, **kwargs):
'''
Automatically detect the correct configuration based on a given file,
load the data, and display the configuration

Parameters
----------
filename : str (path-like)
Name for a local data file.
show : bool
Determines whether to immediately show the application

Returns
-------
Jdaviz ConfigHelper : jdaviz.core.helpers.ConfigHelper
The autoidentified ConfigHelper for the given data
'''
# Identify the correct config
helper_str, hdul = identify_helper(filename)
viz_class = getattr(jdaviz_configs, helper_str.capitalize())

# Create config instance
verbosity = kwargs.pop('verbosity', DEFAULT_VERBOSITY)
history_verbosity = kwargs.pop('history_verbosity', DEFAULT_HISTORY_VERBOSITY)
viz_helper = viz_class(verbosity=verbosity, history_verbosity=history_verbosity)

# Load data
data = hdul if (hdul is not None) else filename
if helper_str == "specviz":
viz_helper.load_spectrum(data, **kwargs)
else:
viz_helper.load_data(data, **kwargs)

# Display app
if show:
viz_helper.show()

return viz_helper
33 changes: 33 additions & 0 deletions jdaviz/core/tests/test_autoconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Tests automatic config detection against our example notebook data

from pathlib import Path
from tempfile import TemporaryDirectory

import pytest
from astroquery.mast import Observations

from jdaviz import open as jdaviz_open
from jdaviz.configs import Specviz2d, Cubeviz, Imviz # , Specviz


@pytest.mark.remote_data
@pytest.mark.filterwarnings('ignore')
@pytest.mark.parametrize('uris', (
# ("mast:JWST/product/jw02732-o004_t004_miri_ch1-shortmediumlong_x1d.fits", Specviz),
# Specviz check disabled due to https://github.com/spacetelescope/jdaviz/issues/2229
("mast:JWST/product/jw01538-o160_s00004_nirspec_f170lp-g235h-s1600a1-sub2048_s2d.fits", Specviz2d), # noqa
("mast:JWST/product/jw02727-o002_t062_nircam_clear-f090w_i2d.fits", Imviz),
("mast:JWST/product/jw02732-o004_t004_miri_ch1-shortmediumlong_s3d.fits", Cubeviz))
)
def test_autoconfig(uris):
# Setup temporary directory
with TemporaryDirectory(ignore_cleanup_errors=True) as tempdir:
uri = uris[0]
helper_class = uris[1]
download_path = str(Path(tempdir) / Path(uri).name)
Observations.download_file(uri, local_path=download_path)

viz_helper = jdaviz_open(download_path, show=False)

assert type(viz_helper) == helper_class
assert len(viz_helper.app.data_collection) > 0
7 changes: 2 additions & 5 deletions jdaviz/jdaviz_cli.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
"outputs": [],
"source": [
"# PREFIX\n",
"from jdaviz import CONFIG\n",
"from jdaviz import open as jdaviz_open\n",
"\n",
"viz = CONFIG(verbosity='JDAVIZ_VERBOSITY', history_verbosity='JDAVIZ_HISTORY_VERBOSITY')\n",
"for data in DATA_LIST:\n",
" viz.load_data(data) #ADDITIONAL_LOAD_DATA_ARGS\n",
"viz.show()"
"viz = jdaviz_open(verbosity='JDAVIZ_VERBOSITY', history_verbosity='JDAVIZ_HISTORY_VERBOSITY') #ADDITIONAL_LOAD_DATA_ARGS"
]
}
],
Expand Down
9 changes: 8 additions & 1 deletion notebooks/CubevizExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@
" cubeviz.load_data(f'{data_dir}/{fn}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternatively, the data and the configuration can be autodetected and loaded simultaneously by calling `jdaviz.open(f'{data_dir}/{fn}')`"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -225,7 +232,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down
10 changes: 9 additions & 1 deletion notebooks/ImvizExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@
"imviz.show()"
]
},
{
"cell_type": "markdown",
"id": "99d4bdef",
"metadata": {},
"source": [
"Alternatively, the data and the configuration can be autodetected and loaded simultaneously by calling `jdaviz.open(f'{data_dir}/{fn}')`"
]
},
{
"cell_type": "markdown",
"id": "3e78efeb",
Expand Down Expand Up @@ -720,7 +728,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down
10 changes: 9 additions & 1 deletion notebooks/Specviz2dExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@
"source": [
"specviz2d.load_data(f'{data_dir}/{fn}')"
]
},
{
"cell_type": "markdown",
"id": "795bc520",
"metadata": {},
"source": [
"Alternatively, the data and the configuration can be autodetected and loaded simultaneously by calling `jdaviz.open(f'{data_dir}/{fn}')`"
]
}
],
"metadata": {
Expand All @@ -114,7 +122,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down
9 changes: 8 additions & 1 deletion notebooks/SpecvizExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@
"specviz.load_spectrum(f'{data_dir}/{fn}', \"myfile\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternatively, the data and the configuration can be autodetected and loaded simultaneously by calling `jdaviz.open(f'{data_dir}/{fn}')`"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -293,7 +300,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down