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

Extract SpectralRegions from specviz app #161

Merged
merged 4 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 0 additions & 3 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
from glue.core.subset import Subset
from glue_jupyter.app import JupyterApplication
from glue_jupyter.state_traitlets_helpers import GlueState
from ipygoldenlayout import GoldenLayout
from ipyvuetify import VuetifyTemplate
from ipysplitpanes import SplitPanes
from traitlets import Dict

from .core.events import (LoadDataMessage, NewViewerMessage, AddDataMessage,
SnackbarMessage, RemoveDataMessage)
Expand Down
39 changes: 31 additions & 8 deletions jdaviz/configs/specviz/helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pathlib
import uuid

from specutils import Spectrum1D, SpectrumCollection
from specutils import Spectrum1D, SpectrumCollection, SpectralRegion

from jdaviz.core.helpers import ConfigHelper

Expand All @@ -11,32 +11,55 @@ class SpecViz(ConfigHelper):

_default_configuration = 'specviz'

def load_data(self, data, data_label='', format=None):
"""Loads a data file or Spectrum1D object into SpecViz
def load_data(self, data, data_label=None, format=None):
"""
Loads a data file or `~specutils.Spectrum1D` object into SpecViz.

:param data: Spectrum1D spectra, or path to compatible data file
:param data_label: Name/identifier of data
:param format: Spectrum1D data format to load
Parameters
----------
data : str or `~specutils.Spectrum1D`
Spectrum1D spectra, or path to compatible data file.
data_label : str
The Glue data label found in the ``DataCollection``.
format : str
Loader format specification used to indicate data format in
`~specutils.Spectrum1D.read` io method.
"""
# If no data label is assigned, give it a unique identifier
if not data_label:
if data_label is None:
data_label = "specviz_data|" + uuid.uuid4().hex

# If data provided is a path, try opening into a Spectrum1D object
try:
path = pathlib.Path(data)

if path.is_file():
data = Spectrum1D.read(path, format=format)
else:
raise FileNotFoundError("No such file: " + path)
# If not, it must be a Spectrum1D object. Otherwise, it's unsupported
except TypeError:
if type(data) is SpectrumCollection:
raise TypeError("SpectrumCollection detected. Please provide a Spectrum1D")
raise TypeError("`SpectrumCollection` detected. Please "
"provide a `Spectrum1D`.")
elif type(data) is not Spectrum1D:
raise TypeError("Data is not a Spectrum1D object or compatible file")

self.app.add_data(data, data_label)
self.app.add_data_to_viewer('spectrum-viewer', data_label)

def get_spectra(self):
"""Returns the current data loaded into the main viewer"""
return self.app.get_data_from_viewer('spectrum-viewer')

def get_spectral_regions(self):
eteq marked this conversation as resolved.
Show resolved Hide resolved
regions = self.app.get_subsets_from_viewer('spectrum-viewer')

spec_regs = {}

for name, reg in regions.items():
spec_reg = SpectralRegion.from_center(reg.center.x, reg.width)

spec_regs[name] = spec_reg

return spec_regs