Skip to content

Commit

Permalink
Merge pull request #3374 from pllim/fix-comp-subset-test
Browse files Browse the repository at this point in the history
TST: Fix test_get_regions_composite
  • Loading branch information
pllim authored Dec 31, 2024
2 parents a8b3e03 + 1f0ef6c commit 982bebb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import warnings
import pytest

import numpy as np
import pytest
from astropy import units as u
from astropy.nddata import NDData
import astropy.units as u
from regions import CirclePixelRegion, PixCoord
from specutils import SpectralRegion
from glue.core.edit_subset_mode import ReplaceMode, OrMode, NewMode
from glue.core.roi import EllipticalROI, CircularROI, CircularAnnulusROI, RectangularROI
from glue.core.edit_subset_mode import ReplaceMode, OrMode
from numpy.testing import assert_allclose
from regions import CirclePixelRegion, PixCoord
from specutils import SpectralRegion

from jdaviz.configs.default.plugins.subset_tools import utils
from jdaviz.core.region_translators import regions2roi


@pytest.mark.filterwarnings('ignore')
def test_plugin(specviz_helper, spectrum1d):
specviz_helper.load_data(spectrum1d)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
specviz_helper.load_data(spectrum1d)
p = specviz_helper.plugins['Subset Tools']

# regression test for https://github.com/spacetelescope/jdaviz/issues/1693
Expand Down Expand Up @@ -276,8 +277,7 @@ def test_get_regions(cubeviz_helper, spectrum1d_cube, imviz_helper):
plg.get_regions(region_type='fail')


@pytest.mark.xfail(reason='Unskip once issue XXXX is resolved.')
def test_get_regions_composite(cubeviz_helper, spectrum1d_cube):
def test_get_regions_composite(imviz_helper):
"""
If you apply a circular subset mask to a circular subset to make a
composite subset, and they aren't exactly aligned at the center to form a
Expand All @@ -287,34 +287,31 @@ def test_get_regions_composite(cubeviz_helper, spectrum1d_cube):
``app.get_subsets``. This test ensures that a region is returned through
both ``app.get_subsets`` and ``get_regions``.
"""
cubeviz_helper.load_data(spectrum1d_cube)
plg = cubeviz_helper.plugins['Subset Tools']

# For some reason, Subset 2 disappears after the third subset is applied
# when loaded this way. Uncomment to replace _apply_interactive_region once
# JDAT-5014 is resolved
# plg.import_region(CirclePixelRegion(center=PixCoord(x=96.0, y=96.0),
# radius=45.0), combination_mode='new')
# plg.import_region(CirclePixelRegion(center=PixCoord(x=95.0, y=95.0),
# radius=25.0), combination_mode='new')
a = np.ones((200, 200))
imviz_helper.load_data(a, data_label="test")
plg = imviz_helper.plugins['Subset Tools']

# apply two circular subsets
cubeviz_helper._apply_interactive_region('bqplot:truecircle', (51, 51), (141, 141))
cubeviz_helper._apply_interactive_region('bqplot:truecircle', (70, 70), (120, 120))
plg.import_region(CirclePixelRegion(center=PixCoord(x=96.0, y=96.0),
radius=45.0), combination_mode='new')
plg.import_region(CirclePixelRegion(center=PixCoord(x=95.0, y=95.0),
radius=25.0), combination_mode='new')

# apply composite subset created from two existing circular subsets
subset_groups = cubeviz_helper.app.data_collection.subset_groups
imviz_helper.app.session.edit_subset_mode._mode = NewMode
subset_groups = imviz_helper.app.data_collection.subset_groups
new_subset = subset_groups[0].subset_state & ~subset_groups[1].subset_state
cubeviz_helper.default_viewer._obj.apply_subset_state(new_subset)
imviz_helper.default_viewer._obj.apply_subset_state(new_subset)

# make sure Subset 3, the composite subset, is retrieved.
# FIXME: make sure Subset 3, the composite subset, is retrieved.
# This needs https://jira.stsci.edu/browse/JDAT-5035
regions = plg.get_regions()
ss_labels = ['Subset 1', 'Subset 2', 'Subset 3']
assert np.all([regions[ss] for ss in ss_labels])
assert sorted(regions) == ["Subset 1", "Subset 2"] # What we have
# assert sorted(regions) == ["Subset 1", "Subset 2", "Subset 3"] # What we want after JDAT-5035

# make sure the same regions are returned by app.get_subsets
get_subsets = cubeviz_helper.app.get_subsets()
assert np.all([get_subsets[ss][0]['region'] == regions[ss] for ss in ss_labels])
get_subsets = imviz_helper.app.get_subsets()
assert sorted(get_subsets) == ["Subset 1", "Subset 2", "Subset 3"]


def test_check_valid_subset_label(imviz_helper):
Expand Down Expand Up @@ -353,7 +350,6 @@ def test_rename_subset(cubeviz_helper, spectrum1d_cube):

plg.rename_subset("Subset 1", "Test Rename")

print(cubeviz_helper.app.data_collection)
assert plg.subset.choices == ['Create New', 'Test Rename', 'Subset 2']
assert cubeviz_helper.app.data_collection[-1].label == "Spectrum (Test Rename, sum)"

Expand Down
32 changes: 16 additions & 16 deletions jdaviz/configs/imviz/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,29 +420,29 @@ def test_parse_hst_drz(self, imviz_helper):
assert_allclose(sky.dec.deg, 10.802045612042956, rtol=1e-3)
data_unit = u.electron / u.s
assert_quantity_allclose(tbl[0]['background'], 0.0014 * data_unit)
assert_quantity_allclose(tbl[0]['sum'], 115.944737 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['sum'], 112.712406 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['sum_aper_area'], 2583.959958 * PIX2, rtol=1e-3)
assert_array_equal(tbl[0]['pixarea_tot'], None)
assert_array_equal(tbl[0]['aperture_sum_counts'], None)
assert_array_equal(tbl[0]['aperture_sum_counts_err'], None)
assert_array_equal(tbl[0]['counts_fac'], None)
assert_array_equal(tbl[0]['aperture_sum_mag'], None)
assert_array_equal(tbl[0]['flux_scaling'], None)
assert_quantity_allclose(tbl[0]['min'], -0.042749 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['max'], 1.588676 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['mean'], 0.04495 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['median'], 0.021585 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['mode'], -0.025143 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['std'], 0.102367 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['mad_std'], 0.023415 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['var'], 0.010479 * (data_unit * data_unit), rtol=1e-3)
assert_quantity_allclose(tbl[0]['biweight_location'], 0.021173 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['biweight_midvariance'], 0.00062 * (data_unit * data_unit), rtol=1e-3) # noqa
assert_quantity_allclose(tbl[0]['fwhm'], 21.538407 * u.pix, rtol=1e-3)
assert_quantity_allclose(tbl[0]['semimajor_sigma'], 10.04406 * u.pix, rtol=1e-3)
assert_quantity_allclose(tbl[0]['semiminor_sigma'], 8.150735 * u.pix, rtol=1e-3)
assert_quantity_allclose(tbl[0]['orientation'], 79.605418 * u.deg, rtol=1e-3)
assert_quantity_allclose(tbl[0]['eccentricity'], 0.584355, rtol=1e-3)
assert_quantity_allclose(tbl[0]['min'], -0.02422 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['max'], 1.577081 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['mean'], 0.043684 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['median'], 0.02129 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['mode'], -0.023497 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['std'], 0.09908 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['mad_std'], 0.023219 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['var'], 0.009817 * (data_unit * data_unit), rtol=1e-3)
assert_quantity_allclose(tbl[0]['biweight_location'], 0.020978 * data_unit, rtol=1e-3)
assert_quantity_allclose(tbl[0]['biweight_midvariance'], 0.000606 * (data_unit * data_unit), rtol=1e-3) # noqa
assert_quantity_allclose(tbl[0]['fwhm'], 21.230628 * u.pix, rtol=1e-3)
assert_quantity_allclose(tbl[0]['semimajor_sigma'], 9.712415 * u.pix, rtol=1e-3)
assert_quantity_allclose(tbl[0]['semiminor_sigma'], 8.260686 * u.pix, rtol=1e-3)
assert_quantity_allclose(tbl[0]['orientation'], 80.054019 * u.deg, rtol=1e-3)
assert_quantity_allclose(tbl[0]['eccentricity'], 0.525929, rtol=1e-3)

# Request specific extension (name only), use given label
imviz_helper.load_data(filename, ext='CTX', data_label='jclj01010_drz',
Expand Down

0 comments on commit 982bebb

Please sign in to comment.