Skip to content

Commit

Permalink
Merge pull request #2404 from pllim/range-cen
Browse files Browse the repository at this point in the history
Add center and move_to methods to RangeSubsetState
  • Loading branch information
astrofrog authored May 15, 2023
2 parents 8969a97 + 6699766 commit b8ff0b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions glue/core/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions glue/core/tests/test_subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b8ff0b1

Please sign in to comment.