Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add center and move_to methods to RangeSubsetState #2404

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions glue/core/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,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