Skip to content

Commit

Permalink
Added tests for indexed data to the different viewers
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Jul 12, 2024
1 parent 63281df commit 912929d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
7 changes: 2 additions & 5 deletions glue/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def __init__(self):

self.style = VisualAttributes(parent=self)

self.uuid = str(uuid.uuid4())

@property
def label(self):
"""
Expand Down Expand Up @@ -773,11 +775,6 @@ def __init__(self, label="", coords=None, **kwargs):

self._key_joins = {}

# To avoid circular references when saving objects with references to
# the data, we make sure that all Data objects have a UUID that can
# uniquely identify them.
self.uuid = str(uuid.uuid4())

@property
def coords(self):
"""
Expand Down
27 changes: 27 additions & 0 deletions glue/viewers/histogram/tests/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from glue.viewers.histogram.viewer import SimpleHistogramViewer
from glue.core.application_base import Application
from glue.core.data import Data
from glue.core.data_derived import IndexedData


@visual_test
Expand Down Expand Up @@ -119,3 +120,29 @@ def test_reset_limits():

assert_allclose(viewer.state.hist_x_min, 49.95)
assert_allclose(viewer.state.hist_x_max, 949.05)


def test_indexed_data():

# Make sure that the scatter viewer works properly with IndexedData objects

data_4d = Data(label='hypercube',
x=np.random.random((3, 5, 4, 3)),
y=np.random.random((3, 5, 4, 3)))

data_2d = IndexedData(data_4d, (2, None, 3, None))

application = Application()

session = application.session

hub = session.hub

data_collection = session.data_collection
data_collection.append(data_4d)
data_collection.append(data_2d)

viewer = application.new_data_viewer(SimpleHistogramViewer)
viewer.add_data(data_2d)

assert viewer.state.x_att is data_2d.main_components[0]
1 change: 1 addition & 0 deletions glue/viewers/image/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ def reset_contrast_bias(self):

def _update_attribute_display_unit_choices(self, *args):

# NOTE: only Data and its subclasses support specifying units
if self.layer is None or self.attribute is None or not isinstance(self.layer, Data):
ImageLayerState.attribute_display_unit.set_choices(self, [])
return
Expand Down
9 changes: 5 additions & 4 deletions glue/viewers/profile/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

import numpy as np

from glue.core import Subset
from glue.core import Subset, Data
from echo import delay_callback
from glue.viewers.matplotlib.state import (MatplotlibDataViewerState,
MatplotlibLayerState,
DeferredDrawCallbackProperty as DDCProperty,
DeferredDrawSelectionCallbackProperty as DDSCProperty)
from glue.core.data_combo_helper import ManualDataComboHelper, ComponentIDComboHelper
from glue.utils import defer_draw, avoid_circular
from glue.core.data import BaseData
from glue.core.link_manager import is_convertible_to_single_pixel_cid
from glue.core.exceptions import IncompatibleDataException
from glue.core.message import SubsetUpdateMessage
Expand Down Expand Up @@ -264,7 +263,8 @@ def _layers_changed(self, *args):

def _update_x_display_unit_choices(self):

if self.reference_data is None:
# NOTE: only Data and its subclasses support specifying units
if self.reference_data is None or not isinstance(self.reference_data, Data):
ProfileViewerState.x_display_unit.set_choices(self, [])
return

Expand All @@ -280,7 +280,8 @@ def _update_y_display_unit_choices(self):

component_units = set()
for layer_state in self.layers:
if isinstance(layer_state.layer, BaseData):
# NOTE: only Data and its subclasses support specifying units
if isinstance(layer_state.layer, Data):
component = layer_state.layer.get_component(layer_state.attribute)
if component.units:
component_units.add((layer_state.layer, layer_state.attribute, component.units))
Expand Down
27 changes: 27 additions & 0 deletions glue/viewers/profile/tests/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from glue.config import settings, unit_converter
from glue.plugins.wcs_autolinking.wcs_autolinking import WCSLink
from glue.core.roi import XRangeROI
from glue.core.data_derived import IndexedData


@visual_test
Expand Down Expand Up @@ -171,3 +172,29 @@ def test_unit_conversion():

viewer.state.reference_data = d2
viewer.state.y_display_unit = 'mJy'


def test_indexed_data():

# Make sure that the profile viewer works properly with IndexedData objects

data_4d = Data(label='hypercube_wcs',
x=np.random.random((3, 5, 4, 3)),
coords=WCS(naxis=4))

data_2d = IndexedData(data_4d, (2, None, 3, None))

application = Application()

session = application.session

hub = session.hub

data_collection = session.data_collection
data_collection.append(data_4d)
data_collection.append(data_2d)

viewer = application.new_data_viewer(SimpleProfileViewer)
viewer.add_data(data_2d)

assert viewer.state.x_att is data_2d.world_component_ids[0]
28 changes: 28 additions & 0 deletions glue/viewers/scatter/tests/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from glue.core.application_base import Application
from glue.core.data import Data
from glue.core.link_helpers import LinkSame
from glue.core.data_derived import IndexedData


@visual_test
Expand Down Expand Up @@ -103,3 +104,30 @@ def test_reset_limits():

assert_allclose(viewer.state.y_min, 1000 + 67.932)
assert_allclose(viewer.state.y_max, 1000 + 931.068)


def test_indexed_data():

# Make sure that the scatter viewer works properly with IndexedData objects

data_4d = Data(label='hypercube',
x=np.random.random((3, 5, 4, 3)),
y=np.random.random((3, 5, 4, 3)))

data_2d = IndexedData(data_4d, (2, None, 3, None))

application = Application()

session = application.session

hub = session.hub

data_collection = session.data_collection
data_collection.append(data_4d)
data_collection.append(data_2d)

viewer = application.new_data_viewer(SimpleScatterViewer)
viewer.add_data(data_2d)

assert viewer.state.x_att is data_2d.main_components[0]
assert viewer.state.y_att is data_2d.main_components[1]

0 comments on commit 912929d

Please sign in to comment.