File size: 2,343 Bytes
dd990bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import pytest
from evidence_graph_reranker import (Passage, graph_features, select_with_features,
                                     rank_with_scores, EvidenceGraphReranker)

def test_no_links_no_titles_preserves_score_order():
    docs=["abc", "xyz", "pqr"]
    result=rank_with_scores("anything",docs,[0.1,0.9,0.5],k=3)
    assert [r['index'] for r in result]==[1,2,0]

def test_chain_complementarity():
    docs=[Passage("Atlas device","Atlas device was released by Novus Labs"),
          Passage("Novus Labs","Novus Labs was founded by Mira Chen"),
          Passage("Other company","This is irrelevant")]
    q='Who founded the company that released the Atlas device?'
    link,match=graph_features(q,docs)
    assert link[0,1] == 1.0
    selected=select_with_features([.95,.15,.4],link,match,k=2)
    assert selected==[0,1]

def test_matches_exact_reference_on_synthetic_random_rows():
    import sys
    from pathlib import Path
    sys.path.insert(0,'/mnt/data')
    try:
        from neural_evidence_graph_core import graph_select as reference, graph_arrays as ref_features
    except ImportError:
        pytest.skip('Reference experiment script is absent; public package is still standalone')
    rng=np.random.default_rng(11)
    docs=[{'title':'Atlas device','body':'Novus Labs made Atlas device'},
          {'title':'Novus Labs','body':'Novus Labs founded by Mira Chen'},
          {'title':'Mira Chen','body':'A researcher'}]
    row={'query':'Who founded Novus Labs that made Atlas device?', 'docs':docs}
    link,match=ref_features(row)
    L,M=graph_features(row['query'],[Passage(d['title'],d['body']) for d in docs])
    np.testing.assert_array_equal(L,link)
    np.testing.assert_array_equal(M,match)
    for _ in range(100):
        scores=rng.normal(size=3)
        assert reference(scores,link,match,3)==select_with_features(scores,L,M,3)

def test_precomputed_scores_do_not_load_models():
    obj=EvidenceGraphReranker()
    chosen=obj.rank('A', ['x', 'y'], scores=[1.0,.3], k=1)
    assert chosen[0]['index']==0 and obj._model is None

def test_invalid_inputs():
    with pytest.raises(ValueError): rank_with_scores('q',['a','b'],[1],k=2)
    with pytest.raises(ValueError): rank_with_scores('q',['a'],[float('nan')],k=1)
    with pytest.raises(ValueError): rank_with_scores('q',['a'],[1],k=0)