From 2f2ee0f623c5070634a1db311a3f758eda39da3b Mon Sep 17 00:00:00 2001 From: protosphinx <133899485+protosphinx@users.noreply.github.com> Date: Fri, 15 May 2026 16:10:59 +0000 Subject: [PATCH] test(diffusion,eval): cover non-square matrix, 1D input, max_iters cap, and repeated-event traces Three gaps in test_diffusion.py: - non-square p_t matrix triggers the shape[0]!=shape[1] ValueError - 1D p_t array triggers the ndim!=2 ValueError - max_iters=1 exits before convergence but still returns a valid L1-normalized probability vector Two gaps in test_eval.py: - extract_cases with a trace of repeated identical events produces the correct LocalizationCase pairs - evaluate with duplicate k values in the ks list deduplicates them and returns only unique keys --- tests/test_diffusion.py | 31 ++++++++++++++++++++++++++++++- tests/test_eval.py | 14 ++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/tests/test_diffusion.py b/tests/test_diffusion.py index 6da765e..e4c6272 100644 --- a/tests/test_diffusion.py +++ b/tests/test_diffusion.py @@ -52,5 +52,34 @@ def test_ppr_higher_alpha_concentrates_at_seed() -> None: seed = np.array([1.0, 0.0, 0.0]) r_low = personalized_pagerank(p.T, seed, alpha=0.05) r_high = personalized_pagerank(p.T, seed, alpha=0.9) - # higher alpha → more mass at seed + # higher alpha -> more mass at seed assert r_high[0] > r_low[0] + + +def test_ppr_non_square_matrix_raises() -> None: + p_t = np.zeros((2, 3)) + with pytest.raises(ValueError): + personalized_pagerank(p_t, np.zeros(2)) + + +def test_ppr_1d_input_raises() -> None: + with pytest.raises(ValueError): + personalized_pagerank(np.zeros(3), np.zeros(3)) + + +def test_ppr_max_iters_cap_returns_normalized() -> None: + # 4-node ring that needs many iterations to converge. + # max_iters=1 exits before convergence but result must still be + # a valid L1-normalized probability vector. + p = np.array( + [ + [0.0, 1.0, 0.0, 0.0], + [0.0, 0.0, 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + [1.0, 0.0, 0.0, 0.0], + ] + ) + seed = np.array([1.0, 0.0, 0.0, 0.0]) + r = personalized_pagerank(p.T, seed, alpha=0.15, max_iters=1) + assert pytest.approx(float(r.sum()), rel=1e-6) == 1.0 + assert float(r.min()) >= 0.0 diff --git a/tests/test_eval.py b/tests/test_eval.py index e374de9..4412186 100644 --- a/tests/test_eval.py +++ b/tests/test_eval.py @@ -83,3 +83,17 @@ def test_demo_traces_round_trip_through_eval() -> None: # Demo is constructed so PPR should localize *something* - # top-10 over a 10-node graph should include the truth often. assert score.top_k[10] > 0.5 + + +def test_extract_cases_repeated_events_in_trace() -> None: + cases = extract_cases([["a", "a", "a"]]) + assert len(cases) == 2 + assert cases[0] == LocalizationCase(prefix=["a"], next_event="a") + assert cases[1] == LocalizationCase(prefix=["a", "a"], next_event="a") + + +def test_evaluate_ks_deduplication() -> None: + idx = build_index(demo_graph(), demo_events()) + cases = extract_cases(demo_traces()) + score = evaluate(idx, cases, ks=[1, 1, 3]) + assert set(score.top_k.keys()) == {1, 3}