Skip to content

Commit

Permalink
Add tests for naive disjoint-set class and remove unused method.
Browse files Browse the repository at this point in the history
  • Loading branch information
TallJimbo committed Mar 4, 2024
1 parent f685e80 commit df5c1b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
21 changes: 0 additions & 21 deletions python/lsst/daf/butler/queries/overlaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,6 @@ def __init__(self, superset: Iterable[_T]):
self._subsets = [{k} for k in superset]
self._subsets.sort(key=len, reverse=True)

def add(self, k: _T) -> bool: # numpydoc ignore=PR04
"""Add a new element as its own single-element subset unless it is
already present.
Parameters
----------
k
Value to add.
Returns
-------
added : `bool`:
`True` if the value was actually added, `False` if it was already
present.
"""
for subset in self._subsets:
if k in subset:
return False
self._subsets.append({k})
return True

def merge(self, a: _T, b: _T) -> bool: # numpydoc ignore=PR04
"""Merge the subsets containing the given elements.
Expand Down
18 changes: 17 additions & 1 deletion tests/test_query_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from lsst.daf.butler.dimensions import DimensionElement, DimensionGroup
from lsst.daf.butler.queries import tree as qt
from lsst.daf.butler.queries.expression_factory import ExpressionFactory
from lsst.daf.butler.queries.overlaps import OverlapsVisitor
from lsst.daf.butler.queries.overlaps import OverlapsVisitor, _NaiveDisjointSet
from lsst.daf.butler.queries.visitors import PredicateVisitFlags
from lsst.sphgeom import Mq3cPixelization, Region

Expand Down Expand Up @@ -466,5 +466,21 @@ def test_one_temporal_family(self) -> None:
# trivially duplicates the spatial-join logic.


class NaiveDisjointSetTestCase(unittest.TestCase):
"""Test the naive disjoint-set implementation that backs automatic overlap
join creation.
"""

def test_naive_disjoint_set(self) -> None:
s = _NaiveDisjointSet(range(8))
self.assertCountEqual(s.subsets(), [{n} for n in range(8)])
s.merge(3, 4)
self.assertCountEqual(s.subsets(), [{0}, {1}, {2}, {3, 4}, {5}, {6}, {7}])
s.merge(2, 1)
self.assertCountEqual(s.subsets(), [{0}, {1, 2}, {3, 4}, {5}, {6}, {7}])
s.merge(1, 3)
self.assertCountEqual(s.subsets(), [{0}, {1, 2, 3, 4}, {5}, {6}, {7}])


if __name__ == "__main__":
unittest.main()

0 comments on commit df5c1b1

Please sign in to comment.