Spaces:
Paused
Paused
add tests accordingly
Browse files- tests/__init__.py +6 -0
- tests/test_config.py +6 -0
- tests/test_pdf_processor.py +6 -0
- tests/test_pooling.py +89 -1
tests/__init__.py
CHANGED
|
@@ -1,2 +1,8 @@
|
|
| 1 |
# Tests for visual-rag-toolkit
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Tests for visual-rag-toolkit
|
| 2 |
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
tests/test_config.py
CHANGED
|
@@ -108,3 +108,9 @@ qdrant:
|
|
| 108 |
section = get_section("nonexistent")
|
| 109 |
assert section == {}
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
section = get_section("nonexistent")
|
| 109 |
assert section == {}
|
| 110 |
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
tests/test_pdf_processor.py
CHANGED
|
@@ -124,3 +124,9 @@ class TestChunkIdGeneration:
|
|
| 124 |
except ValueError:
|
| 125 |
pytest.fail(f"Invalid UUID format: {chunk_id}")
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
except ValueError:
|
| 125 |
pytest.fail(f"Invalid UUID format: {chunk_id}")
|
| 126 |
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
tests/test_pooling.py
CHANGED
|
@@ -69,6 +69,95 @@ class TestGlobalPooling:
|
|
| 69 |
np.testing.assert_array_almost_equal(pooled, embedding.mean(axis=0))
|
| 70 |
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
class TestMaxSimScore:
|
| 73 |
"""Test MaxSim scoring."""
|
| 74 |
|
|
@@ -108,4 +197,3 @@ class TestMaxSimScore:
|
|
| 108 |
|
| 109 |
assert isinstance(score, float)
|
| 110 |
assert not np.isnan(score)
|
| 111 |
-
|
|
|
|
| 69 |
np.testing.assert_array_almost_equal(pooled, embedding.mean(axis=0))
|
| 70 |
|
| 71 |
|
| 72 |
+
class TestColPaliExperimentalPooling:
|
| 73 |
+
"""Test ColPali experimental pooling (convolution-style with window 3)."""
|
| 74 |
+
|
| 75 |
+
def test_output_shape_n_plus_2(self):
|
| 76 |
+
"""For N rows, should produce N + 2 vectors."""
|
| 77 |
+
from visual_rag.embedding.pooling import colpali_experimental_pooling_from_rows
|
| 78 |
+
|
| 79 |
+
for n in [4, 10, 32, 64]:
|
| 80 |
+
rows = np.random.randn(n, 128).astype(np.float32)
|
| 81 |
+
pooled = colpali_experimental_pooling_from_rows(rows)
|
| 82 |
+
assert pooled.shape == (n + 2, 128), f"Expected ({n + 2}, 128), got {pooled.shape}"
|
| 83 |
+
|
| 84 |
+
def test_32_rows_produces_34_vectors(self):
|
| 85 |
+
"""Specifically test ColPali case: 32 rows → 34 vectors."""
|
| 86 |
+
from visual_rag.embedding.pooling import colpali_experimental_pooling_from_rows
|
| 87 |
+
|
| 88 |
+
rows = np.random.randn(32, 128).astype(np.float32)
|
| 89 |
+
pooled = colpali_experimental_pooling_from_rows(rows)
|
| 90 |
+
assert pooled.shape == (34, 128), f"Expected (34, 128), got {pooled.shape}"
|
| 91 |
+
|
| 92 |
+
def test_first_vector_is_first_row(self):
|
| 93 |
+
"""Position 0 should be first row alone."""
|
| 94 |
+
from visual_rag.embedding.pooling import colpali_experimental_pooling_from_rows
|
| 95 |
+
|
| 96 |
+
rows = np.random.randn(10, 128).astype(np.float32)
|
| 97 |
+
pooled = colpali_experimental_pooling_from_rows(rows)
|
| 98 |
+
np.testing.assert_array_almost_equal(pooled[0], rows[0])
|
| 99 |
+
|
| 100 |
+
def test_second_vector_is_mean_first_two(self):
|
| 101 |
+
"""Position 1 should be mean of first 2 rows."""
|
| 102 |
+
from visual_rag.embedding.pooling import colpali_experimental_pooling_from_rows
|
| 103 |
+
|
| 104 |
+
rows = np.random.randn(10, 128).astype(np.float32)
|
| 105 |
+
pooled = colpali_experimental_pooling_from_rows(rows)
|
| 106 |
+
expected = rows[:2].mean(axis=0)
|
| 107 |
+
np.testing.assert_array_almost_equal(pooled[1], expected)
|
| 108 |
+
|
| 109 |
+
def test_third_vector_is_mean_first_three(self):
|
| 110 |
+
"""Position 2 should be mean of first 3 rows."""
|
| 111 |
+
from visual_rag.embedding.pooling import colpali_experimental_pooling_from_rows
|
| 112 |
+
|
| 113 |
+
rows = np.random.randn(10, 128).astype(np.float32)
|
| 114 |
+
pooled = colpali_experimental_pooling_from_rows(rows)
|
| 115 |
+
expected = rows[:3].mean(axis=0)
|
| 116 |
+
np.testing.assert_array_almost_equal(pooled[2], expected)
|
| 117 |
+
|
| 118 |
+
def test_sliding_window_middle(self):
|
| 119 |
+
"""Middle positions should be sliding window of 3."""
|
| 120 |
+
from visual_rag.embedding.pooling import colpali_experimental_pooling_from_rows
|
| 121 |
+
|
| 122 |
+
rows = np.random.randn(10, 128).astype(np.float32)
|
| 123 |
+
pooled = colpali_experimental_pooling_from_rows(rows)
|
| 124 |
+
expected_pos5 = rows[3:6].mean(axis=0)
|
| 125 |
+
np.testing.assert_array_almost_equal(pooled[5], expected_pos5)
|
| 126 |
+
|
| 127 |
+
def test_last_vector_is_last_row(self):
|
| 128 |
+
"""Last position should be last row alone."""
|
| 129 |
+
from visual_rag.embedding.pooling import colpali_experimental_pooling_from_rows
|
| 130 |
+
|
| 131 |
+
rows = np.random.randn(10, 128).astype(np.float32)
|
| 132 |
+
pooled = colpali_experimental_pooling_from_rows(rows)
|
| 133 |
+
np.testing.assert_array_almost_equal(pooled[-1], rows[-1])
|
| 134 |
+
|
| 135 |
+
def test_second_to_last_is_mean_last_two(self):
|
| 136 |
+
"""Position N should be mean of last 2 rows."""
|
| 137 |
+
from visual_rag.embedding.pooling import colpali_experimental_pooling_from_rows
|
| 138 |
+
|
| 139 |
+
rows = np.random.randn(10, 128).astype(np.float32)
|
| 140 |
+
pooled = colpali_experimental_pooling_from_rows(rows)
|
| 141 |
+
expected = rows[-2:].mean(axis=0)
|
| 142 |
+
np.testing.assert_array_almost_equal(pooled[-2], expected)
|
| 143 |
+
|
| 144 |
+
def test_edge_cases(self):
|
| 145 |
+
"""Test edge cases: n=1, n=2, n=3."""
|
| 146 |
+
from visual_rag.embedding.pooling import colpali_experimental_pooling_from_rows
|
| 147 |
+
|
| 148 |
+
rows1 = np.random.randn(1, 128).astype(np.float32)
|
| 149 |
+
pooled1 = colpali_experimental_pooling_from_rows(rows1)
|
| 150 |
+
assert pooled1.shape == (1, 128)
|
| 151 |
+
|
| 152 |
+
rows2 = np.random.randn(2, 128).astype(np.float32)
|
| 153 |
+
pooled2 = colpali_experimental_pooling_from_rows(rows2)
|
| 154 |
+
assert pooled2.shape == (3, 128)
|
| 155 |
+
|
| 156 |
+
rows3 = np.random.randn(3, 128).astype(np.float32)
|
| 157 |
+
pooled3 = colpali_experimental_pooling_from_rows(rows3)
|
| 158 |
+
assert pooled3.shape == (5, 128)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
class TestMaxSimScore:
|
| 162 |
"""Test MaxSim scoring."""
|
| 163 |
|
|
|
|
| 197 |
|
| 198 |
assert isinstance(score, float)
|
| 199 |
assert not np.isnan(score)
|
|
|