From 9bb5b679b4292b7333360a2ae618f7b74124a096 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Fri, 12 May 2023 20:53:26 -0400 Subject: [PATCH 1/2] Add center method to RangeSubsetState --- glue/core/subset.py | 3 +++ glue/core/tests/test_subset.py | 1 + 2 files changed, 4 insertions(+) diff --git a/glue/core/subset.py b/glue/core/subset.py index 6d968455b..94ac01989 100644 --- a/glue/core/subset.py +++ b/glue/core/subset.py @@ -808,6 +808,9 @@ 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 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..2b120e944 100644 --- a/glue/core/tests/test_subset.py +++ b/glue/core/tests/test_subset.py @@ -828,6 +828,7 @@ 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) From 6699766c6fed34d83d1aecc13dc60ce043e06d17 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Fri, 12 May 2023 21:04:40 -0400 Subject: [PATCH 2/2] Also implement move_to --- glue/core/subset.py | 5 +++++ glue/core/tests/test_subset.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/glue/core/subset.py b/glue/core/subset.py index 94ac01989..8d8bc149e 100644 --- a/glue/core/subset.py +++ b/glue/core/subset.py @@ -811,6 +811,11 @@ def to_mask(self, data, view=None): 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 2b120e944..45356072a 100644 --- a/glue/core/tests/test_subset.py +++ b/glue/core/tests/test_subset.py @@ -835,6 +835,11 @@ def test_range_subset_state(self): 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()