Skip to content

Commit

Permalink
Added a test of stretch_global for image viewer, don't treat subset_s…
Browse files Browse the repository at this point in the history
…tate as a helper modifier for now
  • Loading branch information
astrofrog committed Apr 19, 2024
1 parent d81645d commit 467db11
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
15 changes: 8 additions & 7 deletions glue/core/state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,15 @@ class StateAttributeLimitsHelper(StateAttributeCacheHelper):
"""

values_names = ('lower', 'upper')
modifiers_names = ('log', 'percentile', 'display_units', 'subset_state')
modifiers_names = ('log', 'percentile', 'display_units')

def __init__(self, state, attribute, random_subset=10000, margin=0, **kwargs):

super(StateAttributeLimitsHelper, self).__init__(state, attribute, **kwargs)

self.margin = margin
self.random_subset = random_subset
self.subset_state = None
self._subset_state = None

if self.attribute is not None:
if (self.lower is not None and self.upper is not None and getattr(self, 'percentile', None) is None):
Expand Down Expand Up @@ -371,20 +371,20 @@ def update_values(self, force=False, use_default_modifiers=False, **properties):
if percentile == 100:
lower = self.data.compute_statistic('minimum', cid=self.component_id,
finite=True, positive=log,
subset_state=self.subset_state,
subset_state=self._subset_state,
random_subset=self.random_subset)
upper = self.data.compute_statistic('maximum', cid=self.component_id,
finite=True, positive=log,
subset_state=self.subset_state,
subset_state=self._subset_state,
random_subset=self.random_subset)
else:
lower = self.data.compute_statistic('percentile', cid=self.component_id,
percentile=exclude, positive=log,
subset_state=self.subset_state,
subset_state=self._subset_state,
random_subset=self.random_subset)
upper = self.data.compute_statistic('percentile', cid=self.component_id,
percentile=100 - exclude, positive=log,
subset_state=self.subset_state,
subset_state=self._subset_state,
random_subset=self.random_subset)

if not isinstance(lower, np.datetime64) and np.isnan(lower):
Expand Down Expand Up @@ -418,7 +418,8 @@ def flip_limits(self):
def set_slice(self, slices):
"""Set subset for compute_statistic to current slice or global"""

self.set(subset_state=None if slices is None else SliceSubsetState(self.data, slices))
self._subset_state = None if slices is None else SliceSubsetState(self.data, slices)

# Force update if percentile not set to 'Custom'.
if isinstance(self.percentile, (int, float)):
self.update_values(force=True)
Expand Down
23 changes: 7 additions & 16 deletions glue/core/tests/test_state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from echo import CallbackProperty, ListCallbackProperty
from glue.core import Data, DataCollection
from glue.core.subset import SliceSubsetState

from .test_state import clone
from ..state_objects import (State, StateAttributeLimitsHelper,
Expand Down Expand Up @@ -95,13 +94,11 @@ class SimpleState(State):
upper = CallbackProperty()
log = CallbackProperty(False)
scale = CallbackProperty(100)
subset_state = CallbackProperty()

self.state = SimpleState()

self.helper = StateAttributeLimitsHelper(self.state, attribute='comp',
lower='lower', upper='upper',
subset_state='subset_state',
percentile='scale', log='log')
self.state.data = self.data
self.state.comp = self.data.id['x']
Expand Down Expand Up @@ -185,31 +182,25 @@ def test_manual_edit(self):
assert self.helper.upper == 234
assert self.helper.log

def test_subset(self):

# Directly set subset to compute limits from
assert self.helper.percentile == 100
self.helper.subset_state = SliceSubsetState(self.helper.data, [slice(6000)])
assert_allclose(self.helper.lower, -100)
assert_allclose(self.helper.upper, 19.992)
self.helper.percentile = 90
assert_allclose(self.helper.lower, -94.0004)
assert_allclose(self.helper.upper, 13.9924)

def test_slice(self):
def test_set_slice(self):

# Set subset to compute limits from slice
self.helper.set_slice([slice(2000, 8000)])

assert self.helper.percentile == 100
assert self.helper.subset_state.slices == [slice(2000, 8000)]

assert_allclose(self.helper.lower, -59.996)
assert_allclose(self.helper.upper, 59.996)

self.helper.percentile = 90

assert_allclose(self.helper.lower, -53.9964)
assert_allclose(self.helper.upper, 53.9964)

self.helper.set_slice(None)

self.helper.percentile = 95

assert_allclose(self.helper.lower, -95)
assert_allclose(self.helper.upper, 95)

Expand Down
40 changes: 40 additions & 0 deletions glue/viewers/image/tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,43 @@ def test_attribute_units():

assert_allclose(layer_state1.v_min, 2.475)
assert_allclose(layer_state1.v_max, 50)


def test_stretch_global():

# Test the option of using global vs per-slice stretch

viewer_state = ImageViewerState()

data1 = Data(x=np.arange(1000).reshape((10, 10, 10)))

layer_state = ImageLayerState(layer=data1, viewer_state=viewer_state)
viewer_state.layers.append(layer_state)

assert layer_state.stretch_global is True

assert layer_state.percentile == 100
assert layer_state.v_min == 0
assert layer_state.v_max == 999

assert viewer_state.slices == (0, 0, 0)

layer_state.stretch_global = False

assert layer_state.v_min == 0
assert layer_state.v_max == 99

viewer_state.slices = (9, 0, 0)

assert layer_state.v_min == 900
assert layer_state.v_max == 999

layer_state.percentile = 90

assert layer_state.v_min == 904.95
assert layer_state.v_max == 994.05

layer_state.stretch_global = True

assert layer_state.v_min == 49.95
assert layer_state.v_max == 949.05

0 comments on commit 467db11

Please sign in to comment.