Skip to content

Commit

Permalink
RangesMatrix: add close_gaps
Browse files Browse the repository at this point in the history
  • Loading branch information
mhasself committed Feb 2, 2024
1 parent 83a3d20 commit 50b184e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/proj/ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ def collect(items, join_depth):
return RangesMatrix(ranges, child_shape=items[0].shape[1:])
return collect(items, axis)

def close_gaps(self, gap_size=0):
"""Call close_gaps(gap_size) on all children. Any ranges that abutt
each other within the gap_size are merged into a single entry.
Usually a gap_size of 0 is not possible, but if a caller is
carefully using append_interval_no_check, then it can happen.
"""
for r in self.ranges:
r.close_gaps(gap_size)

def get_stats(self):
samples, intervals = [], []
for r in self.ranges:
Expand Down
18 changes: 18 additions & 0 deletions test/test_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,21 @@ def test_int_args(self):
with self.assertRaises(ValueError):
r.add_interval(object(), object())
self.assertEqual(len(r.ranges()), 2)

def test_close_gaps(self):
def _get_gapped(size=0):
r = Ranges(1000)
r.append_interval_no_check(10, 20)
r.append_interval_no_check(20+size, 30+size)
return r
r = _get_gapped()
assert(len(r.ranges()) == 2)
r.close_gaps(0)
assert(len(r.ranges()) == 1)

rr = RangesMatrix([_get_gapped(), _get_gapped(10)])
rr.close_gaps()
assert(len(rr[0].ranges()) == 1)
assert(len(rr[1].ranges()) == 2)
rr.close_gaps(10)
assert(len(rr[1].ranges()) == 1)

0 comments on commit 50b184e

Please sign in to comment.