Skip to content

Commit

Permalink
allow normalizing extracted bg spectrum per-spaxel
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed May 23, 2024
1 parent 9231d1d commit fd25317
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ class SpectralExtraction(PluginTemplateMixin, ApertureSubsetSelectMixin,
Whether the ``background`` aperture should be considered wavelength-dependent (requires
``wavelength_dependent`` to also be set to ``True``). The cone is defined
to intersect ``background`` at ``reference_spectral_value``.
* ```bg_spec_per_spaxel``:
Whether to normalize the resulting summed background per spaxel when calling
``extract_bg_spectrum``. Otherwise, the spectrum will be scaled by the ratio between the
areas of the aperture and the background aperture. Only applicable if ``function`` is 'Sum'.
* ``bg_spec_add_results`` (:class:`~jdaviz.core.template_mixin.AddResults`)
* :meth:`extract_bg_spectrum`
* ``aperture_method`` (:class:`~jdaviz.core.template_mixin.SelectPluginComponent`):
Method to use for extracting spectrum (and background, if applicable).
* ``add_results`` (:class:`~jdaviz.core.template_mixin.AddResults`)
Expand All @@ -78,6 +83,7 @@ class SpectralExtraction(PluginTemplateMixin, ApertureSubsetSelectMixin,
bg_scale_factor = Float(1).tag(sync=True)
bg_wavelength_dependent = Bool(False).tag(sync=True)

bg_spec_per_spaxel = Bool(False).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)
Expand Down Expand Up @@ -184,7 +190,7 @@ def __init__(self, *args, **kwargs):
def user_api(self):
expose = ['dataset', 'function', 'aperture',
'background', 'bg_wavelength_dependent',
'bg_spec_add_results', 'extract_bg_spectrum',
'bg_spec_per_spaxel', 'bg_spec_add_results', 'extract_bg_spectrum',
'add_results', 'extract',
'wavelength_dependent', 'reference_spectral_value',
'aperture_method']
Expand Down Expand Up @@ -472,7 +478,7 @@ def extract(self, return_bg=False, add_data=True, **kwargs):
self.wavelength_dependent,
selected_func, **kwargs)

bg_spec = self.extract_bg_spectrum(add_data=False)
bg_spec = self.extract_bg_spectrum(add_data=False, bg_spec_per_spaxel=False)
if bg_spec is not None:
spec = spec - bg_spec

Expand Down Expand Up @@ -518,15 +524,20 @@ def extract_bg_spectrum(self, add_data=False, **kwargs):
Additional keyword arguments passed to the NDDataArray collapse operation.
Examples include ``propagate_uncertainties`` and ``operation_ignores_mask``.
"""
# allow internal calls to override the behavior of the bg_spec_per_spaxel traitlet
bg_spec_per_spaxel = kwargs.pop('bg_spec_per_spaxel', self.bg_spec_per_spaxel)
if self.background.selected != self.background.default_text:
bg_spec = self._extract_from_aperture(self.spectral_cube, self.uncert_cube,
self.background, self.bg_weight_mask,
self.bg_wavelength_dependent,
self.function_selected.lower(), **kwargs)
if self.function_selected.lower() == 'sum':
# then scale according to aperture areas across the spectral axis (allowing for
# independent wavelength-dependence btwn the aperture and background)
bg_spec *= self.aperture_area_along_spectral / self.bg_area_along_spectral
if bg_spec_per_spaxel:
bg_spec *= 1 / self.bg_area_along_spectral
else:
# then scale according to aperture areas across the spectral axis (allowing for
# independent wavelength-dependence btwn the aperture and background)
bg_spec *= self.aperture_area_along_spectral / self.bg_area_along_spectral
else:
bg_spec = None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@
<span style="padding: 6px">Export Background Spectrum</span>
</v-expansion-panel-header>
<v-expansion-panel-content class="plugin-expansion-panel-content">
<v-row v-if="function_selected === 'Sum'">
<v-switch
v-model="bg_spec_per_spaxel"
label="Normalize per-spaxel"
hint="Whether to normalize the resulting summed background per spaxel (not shown in preview). Otherwise, the spectrum will be scaled by the ratio between the areas of the aperture to the background aperture."
persistent-hint
></v-switch>
</v-row>
<plugin-add-results
:label.sync="bg_spec_results_label"
:label_default="bg_spec_results_label_default"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ def test_background_subtraction(cubeviz_helper, spectrum1d_cube_largest):
assert extract_plg._obj.marks['bg_spec'].visible

bg_spec = extract_plg.extract_bg_spectrum()
extract_plg.bg_spec_per_spaxel = True
bg_spec_normed = extract_plg.extract_bg_spectrum()
assert np.all(bg_spec_normed.flux.value < bg_spec.flux.value)
spec = extract_plg.extract()

assert np.allclose(spec.flux, spec_no_bg.flux - bg_spec.flux)
Expand Down

0 comments on commit fd25317

Please sign in to comment.