StillWorking commited on
Commit
9bd0b72
·
verified ·
1 Parent(s): dd61fed

Upload backend/tests/test_faiss_store.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. backend/tests/test_faiss_store.py +111 -0
backend/tests/test_faiss_store.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import faiss
2
+ import numpy as np
3
+ import pytest
4
+
5
+ import core.faiss_store as store
6
+
7
+
8
+ @pytest.fixture()
9
+ def tiny_store(monkeypatch):
10
+ """A 4-dimensional index with two orthogonal unit vectors."""
11
+ index = faiss.IndexFlatL2(4)
12
+ index.add(np.array([
13
+ [1.0, 0.0, 0.0, 0.0],
14
+ [0.0, 1.0, 0.0, 0.0],
15
+ ], dtype="float32"))
16
+
17
+ monkeypatch.setattr(store, "_index", index)
18
+ monkeypatch.setattr(store, "_metadata", {
19
+ "000001.jpg": ["Male", "Smiling"],
20
+ "000002.jpg": ["Female", "Young"],
21
+ })
22
+ monkeypatch.setattr(store, "_faiss_order", ["000001.jpg", "000002.jpg"])
23
+ return store
24
+
25
+
26
+ def test_squared_l2_to_cosine_endpoints():
27
+ assert store.squared_l2_to_cosine(0.0) == pytest.approx(1.0)
28
+ assert store.squared_l2_to_cosine(2.0) == pytest.approx(0.0)
29
+ assert store.squared_l2_to_cosine(4.0) == pytest.approx(-1.0)
30
+ # Orthogonal-ish float noise must stay inside [-1, 1].
31
+ assert -1.0 <= store.squared_l2_to_cosine(4.5) <= 1.0
32
+
33
+
34
+ def test_search_returns_cosine_similarity_not_distance(tiny_store):
35
+ results = tiny_store.search_by_vector(
36
+ np.array([1.0, 0.0, 0.0, 0.0], dtype="float32"),
37
+ top_k=2,
38
+ )
39
+
40
+ assert [r["image_id"] for r in results] == ["000001.jpg", "000002.jpg"]
41
+ assert results[0]["similarity"] == pytest.approx(1.0)
42
+ assert results[1]["similarity"] == pytest.approx(0.0)
43
+ assert results[0]["attributes"] == ["Male", "Smiling"]
44
+
45
+
46
+ def test_search_normalises_query(tiny_store):
47
+ results = tiny_store.search_by_vector(
48
+ np.array([5.0, 0.0, 0.0, 0.0], dtype="float32"),
49
+ top_k=1,
50
+ )
51
+ assert results[0]["similarity"] == pytest.approx(1.0)
52
+
53
+
54
+ def test_search_rejects_wrong_dimension(tiny_store):
55
+ with pytest.raises(ValueError, match="dimension"):
56
+ tiny_store.search_by_vector(np.zeros(3, dtype="float32"), top_k=1)
57
+
58
+
59
+ def test_search_attributes_is_case_insensitive(tiny_store):
60
+ matches = tiny_store.search_by_attributes(["male", "smiling"], top_k=5)
61
+ assert len(matches) == 1
62
+ assert matches[0]["image_id"] == "000001.jpg"
63
+ assert matches[0]["faiss_id"] == 0
64
+
65
+
66
+ def test_search_attributes_requires_all(tiny_store):
67
+ assert tiny_store.search_by_attributes(["Male", "Young"], top_k=5) == []
68
+
69
+
70
+ def test_search_attributes_honours_exclusions(tiny_store):
71
+ matches = tiny_store.search_by_attributes(
72
+ ["Young"],
73
+ top_k=5,
74
+ excluded_attrs=["Male"],
75
+ )
76
+ assert [m["image_id"] for m in matches] == ["000002.jpg"]
77
+
78
+
79
+ def test_search_attributes_exclusion_alone_filters_everything(tiny_store):
80
+ matches = tiny_store.search_by_attributes(
81
+ [],
82
+ top_k=5,
83
+ excluded_attrs=["male"],
84
+ )
85
+ assert [m["image_id"] for m in matches] == ["000002.jpg"]
86
+
87
+
88
+ def test_search_attributes_unknown_requirement_returns_empty(tiny_store):
89
+ assert tiny_store.search_by_attributes(["Does_Not_Exist"], top_k=5) == []
90
+
91
+
92
+ def test_attribute_matrix_is_cached(tiny_store):
93
+ matrix_one, columns_one = tiny_store.get_attribute_matrix()
94
+ matrix_two, columns_two = tiny_store.get_attribute_matrix()
95
+
96
+ assert matrix_one is matrix_two
97
+ assert columns_one is columns_two
98
+
99
+
100
+ def test_attribute_matrix_rebuilds_when_metadata_is_replaced(tiny_store, monkeypatch):
101
+ tiny_store.warm_attribute_index()
102
+
103
+ monkeypatch.setattr(store, "_metadata", {
104
+ "000001.jpg": ["Young"],
105
+ "000002.jpg": ["Male"],
106
+ })
107
+ monkeypatch.setattr(store, "_faiss_order", ["000001.jpg", "000002.jpg"])
108
+
109
+ matches = tiny_store.search_by_attributes(["Young"], top_k=5)
110
+
111
+ assert [m["image_id"] for m in matches] == ["000001.jpg"]