Skip to content

Commit

Permalink
Add slices callback from self.viewer_state; check for "Custom" perc…
Browse files Browse the repository at this point in the history
…entile
  • Loading branch information
dhomeier committed Apr 8, 2024
1 parent 1f738f9 commit b472f52
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
8 changes: 6 additions & 2 deletions glue/core/state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def update_values(self, force=False, use_default_modifiers=False, **properties):
percentile = getattr(self, 'percentile', None) or 100
log = getattr(self, 'log', None) or False

if not force and (percentile == 'Custom' or not hasattr(self, 'data') or self.data is None):
if percentile == 'Custom' or (not force and getattr(self, 'data', None) is None):
self.set(percentile=percentile, log=log)
else:
# Shortcut if the component ID is a pixel component ID
Expand Down Expand Up @@ -377,8 +377,12 @@ def flip_limits(self):
self.set(lower=self.upper, upper=self.lower)

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.update_values(force=True)
# Force update if percentile not set to 'Custom'.
if isinstance(self.percentile, (int, float)):
self.update_values(force=True)


class StateAttributeSingleValueHelper(StateAttributeCacheHelper):
Expand Down
42 changes: 9 additions & 33 deletions glue/viewers/image/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
DeferredDrawSelectionCallbackProperty as DDSCProperty)
from glue.core.state_objects import StateAttributeLimitsHelper
from glue.utils import defer_draw, view_shape
from echo import callback_property, delay_callback
from echo import delay_callback
from glue.core.data_combo_helper import ManualDataComboHelper, ComponentIDComboHelper
from glue.core.exceptions import IncompatibleDataException
from glue.viewers.common.stretch_state_mixin import StretchStateMixin
Expand Down Expand Up @@ -572,49 +572,25 @@ def _get_image(self, view=None):

def _set_global_limits(self, global_limits=True):
if global_limits:
self.remove_callback('slice_subset_state', self._update_slice_subset)
self.viewer_state.remove_callback('slices', self._update_slice_subset)
self.attribute_lim_helper.set_slice(None)

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

View check run for this annotation

Codecov / codecov/patch

glue/viewers/image/state.py#L574-L576

Added lines #L574 - L576 were not covered by tests
else:
self.add_callback('slice_subset_state', self._update_slice_subset)
slices = [slice(s) if s is None else s for s in self.slice_subset_state]
self.attribute_lim_helper.set_slice(slices)
self.viewer_state.add_callback('slices', self._update_slice_subset)
self.attribute_lim_helper.set_slice(self.viewer_state.numpy_slice_aggregation_transpose[0])

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

View check run for this annotation

Codecov / codecov/patch

glue/viewers/image/state.py#L578-L579

Added lines #L578 - L579 were not covered by tests

def _update_slice_subset(self, slice_subset_state):
def _update_slice_subset(self, slices):
"""
Select a subset slice for determining image levels.
Parameters
----------
slice_subset_state : iterable of :class:`slice` or `None`
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.
with the shape of `self.data`; `None` to unslice
- will be used via helper property.
"""
slices = [slice(s) if s is None else s for s in slice_subset_state]
self.attribute_lim_helper.set_slice(slices)

@callback_property
def slice_subset_state(self):
"""
Returns slicing information usable by :class:`~glue.core.subset.SliceSubsetState`.
slice_subset_state = DDCProperty(docstring='Slices iterable describing the current '
'slice along all dimensions as subset')
"""
# Need a safety check here if self.viewer_state is already fully initialised.
if self.viewer_state.reference_data is None or self.viewer_state.x_att is None:
return None
else:
slices = []
for i in range(self.viewer_state.reference_data.ndim):
if i == self.viewer_state.x_att.axis or i == self.viewer_state.y_att.axis:
slices.append(None)
else:
if isinstance(self.viewer_state.slices[i], AggregateSlice):
slices.append(self.viewer_state.slices[i].slice)
else:
slices.append(self.viewer_state.slices[i])
return slices # self.viewer_state.numpy_slice_aggregation_transpose[0]
self.attribute_lim_helper.set_slice(self.viewer_state.numpy_slice_aggregation_transpose[0])

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

View check run for this annotation

Codecov / codecov/patch

glue/viewers/image/state.py#L593

Added line #L593 was not covered by tests

def flip_limits(self):
"""
Expand Down

0 comments on commit b472f52

Please sign in to comment.