def test_get_loc_closed(self, closed): tree = IntervalTree([0], [1], closed=closed) for p, errors in [(0, tree.open_left), (1, tree.open_right)]: if errors: with pytest.raises(KeyError): tree.get_loc(p) else: tm.assert_numpy_array_equal(tree.get_loc(p), np.array([0], dtype='int64'))
def test_get_loc_closed(self, closed): tree = IntervalTree([0], [1], closed=closed) for p, errors in [(0, tree.open_left), (1, tree.open_right)]: if errors: with pytest.raises(KeyError, match=str(p)): tree.get_loc(p) else: result = tree.get_loc(p) expected = np.array([0], dtype="intp") tm.assert_numpy_array_equal(result, expected)
def test_get_loc_closed(self, closed): tree = IntervalTree([0], [1], closed=closed) for p, errors in [(0, tree.open_left), (1, tree.open_right)]: if errors: with pytest.raises(KeyError): tree.get_loc(p) else: tm.assert_numpy_array_equal(tree.get_loc(p), np.array([0], dtype='int64'))
def test_get_loc_closed(self): for closed in ['left', 'right', 'both', 'neither']: tree = IntervalTree([0], [1], closed=closed) for p, errors in [(0, tree.open_left), (1, tree.open_right)]: if errors: with self.assertRaises(KeyError): tree.get_loc(p) else: self.assert_numpy_array_equal(tree.get_loc(p), np.array([0], dtype='int64'))
def test_get_indexer_non_unique_overflow(self, dtype, target_value, target_dtype): left, right = np.array([0, 2], dtype=dtype), np.array([1, 3], dtype=dtype) tree = IntervalTree(left, right) target = np.array([target_value], dtype=target_dtype) result_indexer, result_missing = tree.get_indexer_non_unique(target) expected_indexer = np.array([-1], dtype="intp") tm.assert_numpy_array_equal(result_indexer, expected_indexer) expected_missing = np.array([0], dtype="intp") tm.assert_numpy_array_equal(result_missing, expected_missing)
def test_duplicates(self): tree = IntervalTree([0, 0, 0], [1, 1, 1]) tm.assert_numpy_array_equal(np.sort(tree.get_loc(0.5)), np.array([0, 1, 2], dtype='int64')) with pytest.raises(KeyError): tree.get_indexer(np.array([0.5])) indexer, missing = tree.get_indexer_non_unique(np.array([0.5])) tm.assert_numpy_array_equal(np.sort(indexer), np.array([0, 1, 2], dtype='int64')) tm.assert_numpy_array_equal(missing, np.array([], dtype='int64'))
def test_get_indexer_closed(self, closed, leaf_size): x = np.arange(1000, dtype='float64') found = x.astype('intp') not_found = (-1 * np.ones(1000)).astype('intp') tree = IntervalTree(x, x + 0.5, closed=closed, leaf_size=leaf_size) tm.assert_numpy_array_equal(found, tree.get_indexer(x + 0.25)) expected = found if tree.closed_left else not_found tm.assert_numpy_array_equal(expected, tree.get_indexer(x + 0.0)) expected = found if tree.closed_right else not_found tm.assert_numpy_array_equal(expected, tree.get_indexer(x + 0.5))
def test_get_indexer_closed(self, closed, leaf_size): x = np.arange(1000, dtype='float64') found = x.astype('intp') not_found = (-1 * np.ones(1000)).astype('intp') tree = IntervalTree(x, x + 0.5, closed=closed, leaf_size=leaf_size) tm.assert_numpy_array_equal(found, tree.get_indexer(x + 0.25)) expected = found if tree.closed_left else not_found tm.assert_numpy_array_equal(expected, tree.get_indexer(x + 0.0)) expected = found if tree.closed_right else not_found tm.assert_numpy_array_equal(expected, tree.get_indexer(x + 0.5))
def test_inf_bound_infinite_recursion(self, left, right, expected): # GH 46658 tree = IntervalTree(left * 101, right * 101) result = tree.root.pivot assert result == expected
def test_is_overlapping_endpoints(self, closed, order): """shared endpoints are marked as overlapping""" # GH 23309 left, right = np.arange(3, dtype="int64"), np.arange(1, 4) tree = IntervalTree(left[order], right[order], closed=closed) result = tree.is_overlapping expected = closed == "both" assert result is expected
def setUp(self): gentree = lambda dtype: IntervalTree(np.arange(5, dtype=dtype), np.arange(5, dtype=dtype) + 2) self.tree = gentree('int64') self.trees = { dtype: gentree(dtype) for dtype in ['int32', 'int64', 'float32', 'float64'] }
def test_interval_tree_error_and_warning(self): # GH 40245 msg = ( "Deprecated argument `closed` cannot " "be passed if argument `inclusive` is not None" ) with pytest.raises(ValueError, match=msg): left, right = np.arange(10), [np.iinfo(np.int64).max] * 10 IntervalTree(left, right, closed="both", inclusive="both") msg = "Argument `closed` is deprecated in favor of `inclusive`" with tm.assert_produces_warning( FutureWarning, match=msg, check_stacklevel=False ): left, right = np.arange(10), [np.iinfo(np.int64).max] * 10 IntervalTree(left, right, closed="both")
def test_duplicates(self, dtype): left = np.array([0, 0, 0], dtype=dtype) tree = IntervalTree(left, left + 1) with pytest.raises( KeyError, match="'indexer does not intersect a unique set of intervals'" ): tree.get_indexer(np.array([0.5])) indexer, missing = tree.get_indexer_non_unique(np.array([0.5])) result = np.sort(indexer) expected = np.array([0, 1, 2], dtype="intp") tm.assert_numpy_array_equal(result, expected) result = missing expected = np.array([], dtype="intp") tm.assert_numpy_array_equal(result, expected)
def test_get_indexer_closed(self): x = np.arange(1000, dtype='intp') found = x not_found = (-1 * np.ones(1000)).astype('intp') for leaf_size in [1, 10, 100, 10000]: for closed in ['left', 'right', 'both', 'neither']: tree = IntervalTree(x, x + 0.5, closed=closed, leaf_size=leaf_size) self.assert_numpy_array_equal(found, tree.get_indexer(x + 0.25)) expected = found if tree.closed_left else not_found self.assert_numpy_array_equal(expected, tree.get_indexer(x + 0.0)) expected = found if tree.closed_right else not_found self.assert_numpy_array_equal(expected, tree.get_indexer(x + 0.5))
def test_construction_overflow(self): # GH 25485 left, right = np.arange(101, dtype="int64"), [np.iinfo(np.int64).max] * 101 tree = IntervalTree(left, right) # pivot should be average of left/right medians result = tree.root.pivot expected = (50 + np.iinfo(np.int64).max) / 2 assert result == expected
def test_duplicates(self, dtype): left = np.array([0, 0, 0], dtype=dtype) tree = IntervalTree(left, left + 1) result = np.sort(tree.get_loc(0.5)) expected = np.array([0, 1, 2], dtype='intp') tm.assert_numpy_array_equal(result, expected) with pytest.raises(KeyError): tree.get_indexer(np.array([0.5])) indexer, missing = tree.get_indexer_non_unique(np.array([0.5])) result = np.sort(indexer) expected = np.array([0, 1, 2], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = missing expected = np.array([], dtype='intp') tm.assert_numpy_array_equal(result, expected)
def test_duplicates(self): tree = IntervalTree([0, 0, 0], [1, 1, 1]) tm.assert_numpy_array_equal(np.sort(tree.get_loc(0.5)), np.array([0, 1, 2], dtype='int64')) with pytest.raises(KeyError): tree.get_indexer(np.array([0.5])) indexer, missing = tree.get_indexer_non_unique(np.array([0.5])) tm.assert_numpy_array_equal(np.sort(indexer), np.array([0, 1, 2], dtype='int64')) tm.assert_numpy_array_equal(missing, np.array([], dtype='int64'))
def test_duplicates(self, dtype): left = np.array([0, 0, 0], dtype=dtype) tree = IntervalTree(left, left + 1) result = np.sort(tree.get_loc(0.5)) expected = np.array([0, 1, 2], dtype='intp') tm.assert_numpy_array_equal(result, expected) with pytest.raises(KeyError): tree.get_indexer(np.array([0.5])) indexer, missing = tree.get_indexer_non_unique(np.array([0.5])) result = np.sort(indexer) expected = np.array([0, 1, 2], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = missing expected = np.array([], dtype='intp') tm.assert_numpy_array_equal(result, expected)
def _engine(self): left = self._maybe_convert_i8(self.left) right = self._maybe_convert_i8(self.right) return IntervalTree(left, right, closed=self.closed)
def _engine(self): return IntervalTree(self.left, self.right, closed=self.closed)
def gentree(dtype): left = np.arange(5, dtype=dtype) right = left + 2 return IntervalTree(left, right)
def tree(request, leaf_size): left = request.param return IntervalTree(left, left + 2, leaf_size=leaf_size)
def tree(dtype): left = np.arange(5, dtype=dtype) return IntervalTree(left, left + 2)
def tree(request, leaf_size): left = request.param return IntervalTree(left, left + 2, leaf_size=leaf_size, inclusive="right")
def _engine(self) -> IntervalTree: # type: ignore[override] left = self._maybe_convert_i8(self.left) right = self._maybe_convert_i8(self.right) return IntervalTree(left, right, closed=self.closed)
def test_is_overlapping_trivial(self, closed, left, right): # GH 23309 tree = IntervalTree(left, right, closed=closed) assert tree.is_overlapping is False
def test_is_overlapping(self, closed, order, left, right, expected): # GH 23309 tree = IntervalTree(left[order], right[order], closed=closed) result = tree.is_overlapping assert result is expected