Skip to content

Commit

Permalink
background spectrum: live-preview and export
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Sep 29, 2022
1 parent 1aeb356 commit 3d6ae9b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
13 changes: 9 additions & 4 deletions docs/specviz2d/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ The background step of the plugin allows for creating background and background-
images via `specreduce.background <https://specreduce.readthedocs.io/en/latest/#module-specreduce.background>`_.

Once you interact with any of the inputs in the background step or hover over that area
of the plugin, the live visualization will change to show the center (dotted line) and edges
(solid lines) of the background region(s). Choose between creating the background
around the trace defined in the Trace section, or around a "Manual" flat trace.
of the plugin, the live visualization in the 2d spectrum viewer will change to show the center
(dotted line) and edges (solid lines) of the background region(s). The 1D representation of the
background will also be visualized in the 1D spectrum viewer (dotted line).

Choose between creating the background around the trace defined in the Trace section, or around a "Manual" flat trace.

To visualize the resulting background or background-subtracted image, click on the respective panel,
and choose a label for the new data entry. The exported images will now appear in the data dropdown
Expand All @@ -108,7 +110,10 @@ To export and access the specreduce Background object defined in the plugin, cal

bg = sp_ext.export_bg()

To access the background image or background-subtracted image as a :class:`~specutils.Spectrum1D` object, call :meth:`~jdaviz.configs.specviz2d.plugins.spectral_extraction.spectral_extraction.SpectralExtraction.export_bg_img` or :meth:`~jdaviz.configs.specviz2d.plugins.spectral_extraction.spectral_extraction.SpectralExtraction.export_bg_img`, respectively.
To access the background image, background spectrum, or background-subtracted image as a :class:`~specutils.Spectrum1D` object,
call :meth:`~jdaviz.configs.specviz2d.plugins.spectral_extraction.spectral_extraction.SpectralExtraction.export_bg_img`,
:meth:`~jdaviz.configs.specviz2d.plugins.spectral_extraction.spectral_extraction.SpectralExtraction.export_bg_spectrum`,
or :meth:`~jdaviz.configs.specviz2d.plugins.spectral_extraction.spectral_extraction.SpectralExtraction.export_bg_sub`, respectively.

To import the parameters from a specreduce Background object into the plugin, whether it's new or was exported and modified in the notebook, call :meth:`~jdaviz.configs.specviz2d.plugins.spectral_extraction.spectral_extraction.SpectralExtraction.import_bg`::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ class SpectralExtraction(PluginTemplateMixin):
bg_add_to_viewer_items = List().tag(sync=True)
bg_add_to_viewer_selected = Unicode().tag(sync=True)

bg_spec_results_label = Unicode().tag(sync=True)
bg_spec_results_label_default = Unicode().tag(sync=True)
bg_spec_results_label_auto = Bool(True).tag(sync=True)
bg_spec_results_label_invalid_msg = Unicode('').tag(sync=True)
bg_spec_results_label_overwrite = Bool().tag(sync=True)
bg_spec_add_to_viewer_items = List().tag(sync=True)
bg_spec_add_to_viewer_selected = Unicode().tag(sync=True)

bg_sub_results_label = Unicode().tag(sync=True)
bg_sub_results_label_default = Unicode().tag(sync=True)
bg_sub_results_label_auto = Bool(True).tag(sync=True)
Expand Down Expand Up @@ -175,6 +183,16 @@ def __init__(self, *args, **kwargs):
self.bg_add_results.viewer.filters = ['is_spectrum_2d_viewer']
self.bg_results_label_default = 'background'

self.bg_spec_add_results = AddResults(self, 'bg_spec_results_label',
'bg_spec_results_label_default',
'bg_spec_results_label_auto',
'bg_spec_results_label_invalid_msg',
'bg_spec_results_label_overwrite',
'bg_spec_add_to_viewer_items',
'bg_spec_add_to_viewer_selected')
self.bg_spec_add_results.viewer.filters = ['is_spectrum_viewer']
self.bg_spec_results_label_default = 'background-spectrum'

self.bg_sub_add_results = AddResults(self, 'bg_sub_results_label',
'bg_sub_results_label_default',
'bg_sub_results_label_auto',
Expand Down Expand Up @@ -312,7 +330,7 @@ def _update_plugin_marks(self, *args):
'bg': ['trace',
'bg1_center', 'bg1_lower', 'bg1_upper',
'bg2_center', 'bg2_lower', 'bg2_upper',
'extract'],
'bg_spec', 'extract'],
'ext': ['trace',
'ext_upper', 'ext_lower',
'extract']}
Expand Down Expand Up @@ -353,11 +371,12 @@ def marks(self):
# NOTE: += won't trigger the figure to notice new marks
viewer2d.figure.marks = viewer2d.figure.marks + list(self._marks.values())

mark1d = PluginLine(viewer1d, visible=self.plugin_opened)
self._marks['extract'] = mark1d
self._marks['extract'] = PluginLine(viewer1d, visible=self.plugin_opened)
self._marks['bg_spec'] = PluginLine(viewer1d, visible=self.plugin_opened, line_style='dotted') # noqa

# NOTE: += won't trigger the figure to notice new marks
viewer1d.figure.marks = viewer1d.figure.marks + [mark1d]
viewer1d.figure.marks = viewer1d.figure.marks + [self._marks['extract'],
self._marks['bg_spec']]

return self._marks

Expand Down Expand Up @@ -397,7 +416,7 @@ def _interaction_in_bg_step(self, event={}):
except Exception:
# NOTE: ignore error, but will be raised when clicking ANY of the export buttons
for mark in ['trace', 'bg1_center', 'bg1_lower', 'bg1_upper',
'bg2_center', 'bg2_lower', 'bg2_upper']:
'bg2_center', 'bg2_lower', 'bg2_upper', 'bg_spec']:
self.marks[mark].clear()
else:
xs = range(len(trace.trace))
Expand Down Expand Up @@ -430,6 +449,13 @@ def _interaction_in_bg_step(self, event={}):
for mark in ['bg2_center', 'bg2_lower', 'bg2_upper']:
self.marks[mark].clear()

try:
spec = self.export_bg_spectrum()
except Exception:
self.marks['bg_spec'].clear()
else:
self.marks['bg_spec'].update_xy(spec.spectral_axis, spec.flux)

self.active_step = 'bg'
self._update_plugin_marks()

Expand Down Expand Up @@ -664,6 +690,28 @@ def vue_create_bg_img(self, *args):
color='error', sender=self)
)

def export_bg_spectrum(self, add_data=False, **kwargs):
"""
Create a background 1D spectrum from the input parameters defined in the plugin.
Parameters
----------
add_data : bool
Whether to add the resulting spectrum to the application, according to the options
defined in the plugin.
"""
bg = self.export_bg(**kwargs)
spec = bg.bkg_spectrum()

if add_data:
# TODO: implement new add_results
self.bg_spec_add_results.add_results_from_plugin(spec, replace=False)

return spec

def vue_create_bg_spec(self, *args):
self.export_bg_spectrum(add_data=True)

def export_bg_sub(self, add_data=False, **kwargs):
"""
Create a background-subtracted 2D spectrum from the input parameters defined in the plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
<v-expansion-panels popout>
<v-expansion-panel>
<v-expansion-panel-header v-slot="{ open }">
<span style="padding: 6px">Export Background</span>
<span style="padding: 6px">Export Background Image</span>
</v-expansion-panel-header>
<v-expansion-panel-content>
<plugin-add-results
Expand All @@ -251,7 +251,31 @@
<v-expansion-panels popout>
<v-expansion-panel>
<v-expansion-panel-header v-slot="{ open }">
<span style="padding: 6px">Export Subtracted</span>
<span style="padding: 6px">Export Background Spectrum</span>
</v-expansion-panel-header>
<v-expansion-panel-content>
<plugin-add-results
:label.sync="bg_spec_results_label"
:label_default="bg_spec_results_label_default"
:label_auto.sync="bg_spec_results_label_auto"
:label_invalid_msg="bg_spec_results_label_invalid_msg"
:label_overwrite="bg_spec_results_label_overwrite"
label_hint="Label for the background spectrum"
:add_to_viewer_items="bg_spec_add_to_viewer_items"
:add_to_viewer_selected.sync="bg_spec_add_to_viewer_selected"
action_label="Export"
action_tooltip="Create Background Spectrum"
@click:action="create_bg_spec"
></plugin-add-results>
</v-expansion-panel-content>
</v-expansion-panel>
</v-expansion-panels>
</v-row>
<v-row>
<v-expansion-panels popout>
<v-expansion-panel>
<v-expansion-panel-header v-slot="{ open }">
<span style="padding: 6px">Export Background-Subtracted Image</span>
</v-expansion-panel-header>
<v-expansion-panel-content>
<plugin-add-results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def test_plugin(specviz2d_helper):
assert pext.bg_width == 4
bg_img = pext.export_bg_img()
assert isinstance(bg_img, Spectrum1D)
bg_spec = pext.export_bg_spectrum()
assert isinstance(bg_spec, Spectrum1D)
bg_sub = pext.export_bg_sub()
assert isinstance(bg_sub, Spectrum1D)

Expand Down

0 comments on commit 3d6ae9b

Please sign in to comment.