mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
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:
@@ -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))
|
||||
|
||||
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user