Skip to content

Commit

Permalink
Move open to launcher module
Browse files Browse the repository at this point in the history
  • Loading branch information
duytnguyendtn committed Jun 28, 2023
1 parent 51b7a4a commit 535687b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 72 deletions.
2 changes: 1 addition & 1 deletion jdaviz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +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
from jdaviz.core.launcher import open # noqa: F401

# Clean up namespace.
del os
Expand Down
69 changes: 0 additions & 69 deletions jdaviz/core/data_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
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 @@ -279,70 +277,3 @@ def identify_helper(filename, ext=1):
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
All other arguments are interpreted as load_data/load_spectrum arguments for
the autoidentified configuration class
Returns
-------
Jdaviz ConfigHelper : jdaviz.core.helpers.ConfigHelper
The autoidentified ConfigHelper for the given data
'''
# Identify the correct config
helper_str, hdul = identify_helper(filename)
return _launch_config_with_data(helper_str, hdul, show, **kwargs)


def _launch_config_with_data(config, data=None, show=True, **kwargs):
'''
Launch jdaviz with a specific, known configuration and data
Parameters
----------
config : str (path-like)
Name for a local data file.
data : str or any Jdaviz-compatible data
A filepath or Jdaviz-compatible data object (such as Spectrum1D or CCDData)
show : bool
Determines whether to immediately show the application
All other arguments are interpreted as load_data/load_spectrum arguments for
the autoidentified configuration class
Returns
-------
Jdaviz ConfigHelper : jdaviz.core.helpers.ConfigHelper
The loaded ConfigHelper with data loaded
'''
viz_class = getattr(jdaviz_configs, config.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
if data not in (None, ''):
if config == "specviz":
viz_helper.load_spectrum(data, **kwargs)
else:
viz_helper.load_data(data, **kwargs)

# Display app
if show:
viz_helper.show()

return viz_helper
76 changes: 74 additions & 2 deletions jdaviz/core/launcher.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,80 @@
import ipyvuetify as v
from ipywidgets import jslink

from jdaviz.cli import ALL_JDAVIZ_CONFIGS
from jdaviz.core.data_formats import identify_helper, _launch_config_with_data
from jdaviz import configs as jdaviz_configs
from jdaviz.cli import DEFAULT_VERBOSITY, DEFAULT_HISTORY_VERBOSITY, ALL_JDAVIZ_CONFIGS
from jdaviz.core.data_formats import identify_helper


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
All other arguments are interpreted as load_data/load_spectrum arguments for
the autoidentified configuration class
Returns
-------
Jdaviz ConfigHelper : jdaviz.core.helpers.ConfigHelper
The autoidentified ConfigHelper for the given data
'''
# Identify the correct config
compatible_helpers, hdul = identify_helper(filename)
if len(compatible_helpers) > 0:
raise NotImplementedError(f"Multiple helpers provided: {compatible_helpers}."
"Unsure which to launch")
else:
return _launch_config_with_data(compatible_helpers[0], hdul, show, **kwargs)


def _launch_config_with_data(config, data=None, show=True, **kwargs):
'''
Launch jdaviz with a specific, known configuration and data
Parameters
----------
config : str (path-like)
Name for a local data file.
data : str or any Jdaviz-compatible data
A filepath or Jdaviz-compatible data object (such as Spectrum1D or CCDData)
show : bool
Determines whether to immediately show the application
All other arguments are interpreted as load_data/load_spectrum arguments for
the autoidentified configuration class
Returns
-------
Jdaviz ConfigHelper : jdaviz.core.helpers.ConfigHelper
The loaded ConfigHelper with data loaded
'''
viz_class = getattr(jdaviz_configs, config.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
if data not in (None, ''):
if config == "specviz":
viz_helper.load_spectrum(data, **kwargs)
else:
viz_helper.load_data(data, **kwargs)

# Display app
if show:
viz_helper.show()

return viz_helper


def show_launcher(configs=['imviz', 'specviz', 'mosviz', 'cubeviz', 'specviz2d']):
Expand Down

0 comments on commit 535687b

Please sign in to comment.