From f9c142965923621bd255dcdb8cd041720e026d40 Mon Sep 17 00:00:00 2001 From: remydubois Date: Sat, 21 Jan 2023 17:57:00 +0100 Subject: [PATCH] Assert leaf_size > 0 --- changelog.md | 4 +++- lsnms/rtree.py | 1 + tests/{test_boxtree.py => test_rtree.py} | 15 ++++++++++++++- tests/test_util.py | 18 ++++-------------- 4 files changed, 22 insertions(+), 16 deletions(-) rename tests/{test_boxtree.py => test_rtree.py} (78%) diff --git a/changelog.md b/changelog.md index 4c6b813..e95ed35 100644 --- a/changelog.md +++ b/changelog.md @@ -5,7 +5,9 @@ Version 0.3.2 ------------ - Fixed issue https://github.com/remydubois/lsnms/issues/12, by masking scores as well as boxes. - Added torch and torchvision as proper dev dependencies -- Fixed Pillow version to 9.3.0 in dev dependencies because 9.4.0 does not compile on my mbp (see https://github.com/python-pillow/Pillow/issues/6862) +- Fixed Pillow version (dev dep) to 9.3.0 in dev dependencies because 9.4.0 does not compile on my mbp (see https://github.com/python-pillow/Pillow/issues/6862) +- Removed deprecated arguments: `cutoff_distance` and `tree`. Removed associated tests. +- Added sanity check to ensure `leaf_size` is strictly positive. Version 0.3.1 diff --git a/lsnms/rtree.py b/lsnms/rtree.py index 4acb0a9..50d0d53 100644 --- a/lsnms/rtree.py +++ b/lsnms/rtree.py @@ -57,6 +57,7 @@ def __init__(self, data, leaf_size=16, axis=0, indices=None): self.data = data self.axis = axis # Quick sanity checks + assert leaf_size > 0, "Leaf size must be strictly positive" assert len(data) > 0, "Empty dataset" assert self.data.shape[-1] % 2 == 0, "odd dimensionality" assert data.ndim == 2, "Boxes to index should be (n_boxes, 4)" diff --git a/tests/test_boxtree.py b/tests/test_rtree.py similarity index 78% rename from tests/test_boxtree.py rename to tests/test_rtree.py index c43f4b6..8f42dd6 100644 --- a/tests/test_boxtree.py +++ b/tests/test_rtree.py @@ -1,6 +1,7 @@ import numpy as np +import pytest -from lsnms.rtree import RTree +from lsnms.rtree import RNode, RTree def intersection(boxA, boxB): @@ -35,3 +36,15 @@ def test_intersect_tree(): out_inter = np.array([inter for i, inter in enumerate(np_intersect) if i not in indices]) np.testing.assert_allclose(in_inter, intersections) np.testing.assert_array_less(out_inter, min_area) + + +def test_build_odd_tree(instances): + boxes, _ = instances + with pytest.raises(AssertionError): + _ = RTree(boxes, leaf_size=0) + + +def test_build_rnode_default_args(instances): + boxes, _ = instances + + _ = RNode(boxes) diff --git a/tests/test_util.py b/tests/test_util.py index 375d363..0976bf2 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -5,16 +5,6 @@ from lsnms.util import box_englobing_boxes, intersection, offset_bboxes -def datagen(n=10_000): - topleft = np.random.uniform(0.0, high=1_000, size=(n, 2)) - wh = np.random.uniform(15, 45, size=topleft.shape) - - boxes = np.concatenate([topleft, topleft + wh], axis=1) - scores = np.random.uniform(0.1, 1.0, size=len(topleft)) - - return boxes, scores - - @njit def intersect_many(tree, boxes): intersections = [] @@ -31,8 +21,8 @@ def assert_no_intersect(boxesA, boxesB): assert intersection(boxA, boxB) == 0.0 -def test_offset_bboxes(): - boxes, _ = datagen() +def test_offset_bboxes(instances): + boxes, _ = instances rng = np.random.default_rng(0) class_ids = rng.integers(0, 2, size=len(boxes)) @@ -50,10 +40,10 @@ def test_offset_bboxes(): @pytest.mark.skip(reason="Visual test") -def test_visual_offset_bboxes(): +def test_visual_offset_bboxes(instances): import matplotlib.pyplot as plt - boxes, _ = datagen() + boxes, _ = instances # boxes = boxes.reshape(-1, 2, 2).mean(1) rng = np.random.default_rng(0) class_ids = rng.integers(0, 3, size=len(boxes))