diff --git a/glue/core/state_objects.py b/glue/core/state_objects.py index f9f3a8c75..4d1089908 100644 --- a/glue/core/state_objects.py +++ b/glue/core/state_objects.py @@ -292,7 +292,7 @@ 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): @@ -300,7 +300,7 @@ def __init__(self, state, attribute, random_subset=10000, margin=0, **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): @@ -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): @@ -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) diff --git a/glue/core/tests/test_state_objects.py b/glue/core/tests/test_state_objects.py index 019425f9f..de009873e 100644 --- a/glue/core/tests/test_state_objects.py +++ b/glue/core/tests/test_state_objects.py @@ -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, @@ -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'] @@ -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) diff --git a/glue/viewers/image/tests/test_state.py b/glue/viewers/image/tests/test_state.py index 055c995c5..ce88efb0c 100644 --- a/glue/viewers/image/tests/test_state.py +++ b/glue/viewers/image/tests/test_state.py @@ -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