diff --git a/glue/core/subset.py b/glue/core/subset.py index 00fbf2718..76c878c94 100644 --- a/glue/core/subset.py +++ b/glue/core/subset.py @@ -809,6 +809,14 @@ def to_mask(self, data, view=None): result = (x >= self.lo) & (x <= self.hi) return result + def center(self): + return (self.hi - self.lo) * 0.5 + self.lo + + def move_to(self, new_cen): + dx = new_cen - self.center() + self.lo = self.lo + dx + self.hi = self.hi + dx + def copy(self): return RangeSubsetState(self.lo, self.hi, self.att) diff --git a/glue/core/tests/test_subset.py b/glue/core/tests/test_subset.py index c9c1a9072..45356072a 100644 --- a/glue/core/tests/test_subset.py +++ b/glue/core/tests/test_subset.py @@ -828,12 +828,18 @@ def test_range_subset_state(self): subset = self.data.new_subset() subset.subset_state = RangeSubsetState(1.1, 1.4, self.data.id['c']) + assert_allclose(subset.subset_state.center(), 1.25) assert_equal(self.data.subsets[0].to_mask(), [1, 1, 0, 0]) data_clone = clone(self.data) assert_equal(data_clone.subsets[0].to_mask(), [1, 1, 0, 0]) + # Also test moving it. + subset.subset_state.move_to(2) + assert_allclose((subset.subset_state.lo, subset.subset_state.hi), (1.85, 2.15)) + assert_allclose(subset.subset_state.center(), 2) + def test_and_subset_state(self): subset = self.data.new_subset()