Files
arzani 577b9f4557
LocationBasedServices/smartguide/pipeline/head This commit looks good
initial version of multipliers in three levels: bridgers, mentors & amplifiers
2026-07-16 01:52:54 +02:00

131 lines
4.4 KiB
Python

"""Tests for graph construction (pair generation, weights, caps, node aggregates)."""
from __future__ import annotations
import pytest
from graph import build_graph, focal_subgraph, short_id
def make_work(wid, year, authors, field_id=None):
"""authors: list of (author_id, institutions[(id,type)], position)."""
return {
"id": f"https://openalex.org/{wid}",
"publication_year": year,
"primary_topic": (
{"field": {"id": f"https://openalex.org/fields/{field_id}"}}
if field_id
else None
),
"authorships": [
{
"author": {"id": f"https://openalex.org/{aid}", "display_name": aid},
"author_position": pos,
"institutions": [
{"id": f"https://openalex.org/{iid}", "type": itype}
for iid, itype in insts
],
}
for aid, insts, pos in authors
],
}
UDE = [("I1", "education")]
COMPANY = [("I9", "company")]
def test_short_id():
assert short_id("https://openalex.org/A5023888391") == "A5023888391"
assert short_id("A5023888391") == "A5023888391"
assert short_id(None) is None
def test_edge_weight_decay_and_team_size():
works = [
make_work("W1", 2026, [("A1", UDE, "first"), ("A2", UDE, "last")]),
make_work("W2", 2025, [("A1", UDE, "first"), ("A2", UDE, "last")]),
]
g = build_graph(works, {"A1", "A2"}, decay=0.8)
assert g.ref_year == 2026
e = g.edges[("A1", "A2")]
# W1: 0.8^0 / 1 = 1.0 ; W2: 0.8^1 / 1 = 0.8
assert e.weight == pytest.approx(1.8)
assert e.n_works == 2
assert e.first_joint_year == 2025
assert e.last_joint_year == 2026
def test_team_size_divides_weight():
works = [
make_work(
"W1", 2026,
[("A1", UDE, "first"), ("A2", UDE, "middle"), ("A3", UDE, "last")],
)
]
g = build_graph(works, {"A1", "A2", "A3"})
# n_authors=3 -> each pair gets 1/(3-1) = 0.5
assert g.edges[("A1", "A2")].weight == pytest.approx(0.5)
assert len(g.edges) == 3
def test_hyperauthorship_cap_excludes_pairs_but_keeps_years():
big = make_work(
"W1", 2020, [(f"A{i}", UDE, "middle") for i in range(1, 61)]
)
small = make_work("W2", 2021, [("A1", UDE, "first"), ("A2", UDE, "last")])
g = build_graph([big, small], {"A1", "A2"}, max_authors_per_work=50)
assert g.n_works_capped == 1
assert g.n_works_used == 1
assert set(g.edges) == {("A1", "A2")}
# capped work still contributes publication years
assert 2020 in g.nodes["A1"].publication_years
assert 2021 in g.nodes["A1"].publication_years
def test_cross_institution_weight():
works = [
make_work("W1", 2026, [("A1", UDE, "first"), ("A2", COMPANY, "last")]),
make_work("W2", 2026, [("A1", UDE, "first"), ("A3", UDE, "last")]),
]
g = build_graph(works, {"A1", "A2", "A3"})
assert g.edges[("A1", "A2")].cross_inst_weight == pytest.approx(1.0)
assert g.edges[("A1", "A3")].cross_inst_weight == pytest.approx(0.0)
def test_company_share_and_fields():
works = [
make_work("W1", 2026, [("A1", UDE, "first"), ("A2", COMPANY, "last")], "F11"),
make_work("W2", 2026, [("A1", UDE, "first"), ("A3", UDE, "last")], "F22"),
]
g = build_graph(works, {"A1"})
n = g.nodes["A1"]
assert n.n_company_works == 1 and n.n_typed_works == 2
assert n.field_counts == {"F11": 1, "F22": 1}
def test_focal_subgraph_excludes_externals():
works = [
make_work("W1", 2026, [("A1", UDE, "first"), ("EXT", COMPANY, "last")]),
make_work("W2", 2026, [("A1", UDE, "first"), ("A2", UDE, "last")]),
]
g = build_graph(works, {"A1", "A2"})
internal = focal_subgraph(g)
assert "EXT" not in internal
assert set(internal["A1"].keys()) == {"A2"}
def test_last_author_junior_first_recorded():
works = [
make_work("W1", 2026, [("JUNIOR", UDE, "first"), ("PROF", UDE, "last")]),
]
g = build_graph(works, {"JUNIOR", "PROF"})
assert g.nodes["PROF"].last_author_junior_first == [("JUNIOR", 2026)]
assert g.nodes["JUNIOR"].last_author_junior_first == []
def test_duplicate_authorships_deduped():
w = make_work("W1", 2026, [("A1", UDE, "first"), ("A1", UDE, "last"), ("A2", UDE, "middle")])
g = build_graph([w], {"A1", "A2"})
# A1 duplicated -> counts once; n_authors=2 -> weight divisor 1
assert g.edges[("A1", "A2")].weight == pytest.approx(1.0)