Merge pull request #1279 from LimbC-C/fix/array-negative-delete-ids

Ignore negative deletion IDs in NumPy and Torch ANN indexes
This commit is contained in:
David Mezzetti
2026-09-30 08:59:03 -04:00
committed by GitHub
2 changed files with 20 additions and 2 deletions
+2 -2
View File
@@ -60,8 +60,8 @@ class NumPy(ANN):
self.metadata()
def delete(self, ids):
# Filter any index greater than size of array
ids = [x for x in ids if x < self.backend.shape[0]]
# Ignore IDs outside the array; negative IDs must not wrap to live rows.
ids = [x for x in ids if 0 <= x < self.backend.shape[0]]
# Clear specified ids, zeros must match the array data type (e.g. uint8 for quantized data)
self.backend[ids] = self.tensor(self.zeros((len(ids), self.backend.shape[1]), dtype=self.backend.dtype))
+18
View File
@@ -418,6 +418,24 @@ class TestDense(unittest.TestCase):
self.runTests("numpy")
def testArrayDeleteBounds(self):
"""
Invalid array deletion IDs must not wrap to live rows or raise IndexError.
"""
for backend in ("numpy", "torch"):
for quantize in (None, 1):
with self.subTest(backend=backend, quantize=quantize):
data = np.array([[255, 0], [0, 255], [255, 255]], dtype=np.uint8) if quantize else np.eye(3, dtype=np.float32)
ann = ANNFactory.create({"backend": backend, "dimensions": data.shape[1], "quantize": quantize})
self.addCleanup(ann.close)
ann.index(data.copy())
ann.delete([-1, -4, 0, 0, 3, 99])
expected = data.copy()
expected[0] = 0
np.testing.assert_array_equal(ann.numpy(ann.backend), expected)
self.assertEqual(ann.count(), 2)
@patch.dict(os.environ, {"ALLOW_PICKLE": "True"})
def testNumPyLegacy(self):
"""