Skip to content

Commit

Permalink
DNM: add subset_state to Callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
dhomeier committed Jan 31, 2024
1 parent 2d92be3 commit 2567e06
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
9 changes: 3 additions & 6 deletions glue/core/state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ def __init__(self, state, attribute, random_subset=10000, margin=0, **kwargs):
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):
# If the lower and upper limits are already set, we need to make
# sure we don't override them, so we set the percentile mode to
Expand All @@ -313,7 +312,8 @@ def __init__(self, state, attribute, random_subset=10000, margin=0, **kwargs):

def update_values(self, force=False, use_default_modifiers=False, **properties):

if not force and not any(prop in properties for prop in ('attribute', 'percentile', 'log')):
if not force and not any(prop in properties for prop in ('attribute', ) +
self.modifiers_names):
self.set(percentile='Custom')
return

Expand All @@ -325,11 +325,8 @@ def update_values(self, force=False, use_default_modifiers=False, **properties):
log = getattr(self, 'log', None) or False

if not force and (percentile == 'Custom' or not hasattr(self, 'data') or self.data is None):

self.set(percentile=percentile, log=log)

else:

# Shortcut if the component ID is a pixel component ID
if isinstance(self.component_id, PixelComponentID) and percentile == 100 and not log:
lower = -0.5
Expand Down Expand Up @@ -361,7 +358,6 @@ def update_values(self, force=False, use_default_modifiers=False, **properties):
if not isinstance(lower, np.datetime64) and np.isnan(lower):
lower, upper = 0, 1
else:

if self.data.get_kind(self.component_id) == 'categorical':
lower = np.floor(lower - 0.5) + 0.5
upper = np.ceil(upper + 0.5) - 0.5
Expand All @@ -382,6 +378,7 @@ def flip_limits(self):

def set_slice(self, slices):
self.set(subset_state=None if slices is None else SliceSubsetState(self.data, slices))
self.update_values(force=True)


class StateAttributeSingleValueHelper(StateAttributeCacheHelper):
Expand Down
11 changes: 6 additions & 5 deletions glue/core/tests/test_state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ 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,10 +187,9 @@ def test_manual_edit(self):

def test_subset(self):

# Set subset to compute limits from
# Directly set subset to compute limits from
assert self.helper.percentile == 100
self.helper.subset_state = SliceSubsetState(self.helper.data, [slice(6000)])
self.helper.percentile = 100
# self.helper.update_values()
assert_allclose(self.helper.lower, -100)
assert_allclose(self.helper.upper, 19.992)
self.helper.percentile = 90
Expand All @@ -199,8 +200,8 @@ def test_slice(self):

# Set subset to compute limits from slice
self.helper.set_slice([slice(2000, 8000)])
self.helper.percentile = 100
# self.helper.update_values()
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
Expand Down
4 changes: 2 additions & 2 deletions glue/viewers/image/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,8 @@ def set_slice(self, slices):
----------
slices : iterable of :class:`slice` or `None`
An iterable containing :class:`slice` objects that can instantiate
a :class:`~glue.core.subset.SliceSubsetState` and has to be consistent
with the shape of `self.data`; `None` to unslice.
a :class:`SliceSubsetState` and has to be consistent with the
shape of `self.data`; `None` to unslice.
"""
self.attribute_lim_helper.set_slice(slices)

Check warning on line 586 in glue/viewers/image/state.py

View check run for this annotation

Codecov / codecov/patch

glue/viewers/image/state.py#L586

Added line #L586 was not covered by tests

Expand Down

0 comments on commit 2567e06

Please sign in to comment.