from __future__ import annotations
import json
import warnings
from pathlib import Path
import xgi
OUT = Path(__file__).with_name("xgi_ground_truth.json")
def _ids(H: xgi.Hypergraph) -> list:
return list(H.edges)
def _members_sorted(H: xgi.Hypergraph) -> dict:
return {str(e): sorted(str(n) for n in H.edges.members(e)) for e in H.edges}
def v_add_edge_empty_creates_edge() -> dict:
H = xgi.Hypergraph()
ret = H.add_edge([])
return {
"return": ret,
"num_edges": H.num_edges,
"num_nodes": H.num_nodes,
"edge_ids": _ids(H),
"members": _members_sorted(H),
}
def v_add_edge_three_empty_auto_ids() -> dict:
H = xgi.Hypergraph()
for _ in range(3):
H.add_edge([])
return {"edge_ids": _ids(H), "num_edges": H.num_edges}
def v_add_edge_dup_idx_warns_noop() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a"], idx="e1")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = H.add_edge(["b"], idx="e1")
return {
"return": ret,
"warned": len(caught) > 0,
"warning_prefix": str(caught[0].message)[:22] if caught else None,
"num_edges": H.num_edges,
"members": _members_sorted(H),
}
def v_add_edge_int_idx_bumps_counter() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a"], idx=5)
H.add_edge(["b"])
return {"edge_ids": _ids(H)}
def v_add_edge_str_idx_no_bump() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a"], idx="5")
H.add_edge(["b"])
return {"edge_ids": _ids(H)}
def v_add_edge_float_idx_bumps() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a"], idx=5.0)
H.add_edge(["b"])
return {"edge_ids": _ids(H)}
def v_add_node_existing_updates_attrs() -> dict:
H = xgi.Hypergraph()
H.add_node("a", x=1)
H.add_node("a", y=2)
return {"attrs": dict(H.nodes["a"]), "num_nodes": H.num_nodes}
def v_add_edge_dedups_members() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "a", "b"], idx="e1")
return {"members": _members_sorted(H), "num_nodes": H.num_nodes}
def v_add_edge_auto_ids_sequence() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a"])
H.add_edge(["b"])
return {"edge_ids": _ids(H)}
def v_remove_edge_basic() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b"], idx="e1")
H.add_edge(["a", "c"], idx="e2")
H.remove_edge("e1")
return {
"edge_ids": _ids(H),
"num_edges": H.num_edges,
"num_nodes": H.num_nodes,
"node_ids": sorted(str(n) for n in H.nodes),
"members": _members_sorted(H),
"memberships": {str(n): sorted(str(e) for e in H.nodes.memberships(n)) for n in H.nodes},
}
def v_remove_edge_missing_raises() -> dict:
H = xgi.Hypergraph()
try:
H.remove_edge("nonexistent")
except Exception as exc: return {"exception": type(exc).__name__, "message": str(exc)}
return {"exception": None, "message": None}
def v_remove_node_weak() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b", "c"], idx="e1")
H.add_edge(["b", "d"], idx="e2")
H.add_edge(["b"], idx="e3") H.remove_node("b") return {
"edge_ids": _ids(H),
"num_edges": H.num_edges,
"num_nodes": H.num_nodes,
"node_ids": sorted(str(n) for n in H.nodes),
"members": _members_sorted(H),
"memberships": {str(n): sorted(str(e) for e in H.nodes.memberships(n)) for n in H.nodes},
}
def v_remove_node_strong() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b"], idx="e1")
H.add_edge(["a", "c", "d"], idx="e2")
H.add_edge(["c", "d"], idx="e4")
H.remove_node("a", strong=True)
return {
"edge_ids": _ids(H),
"num_edges": H.num_edges,
"num_nodes": H.num_nodes,
"node_ids": sorted(str(n) for n in H.nodes),
"members": _members_sorted(H),
"memberships": {str(n): sorted(str(e) for e in H.nodes.memberships(n)) for n in H.nodes},
}
def v_remove_node_missing_raises() -> dict:
H = xgi.Hypergraph()
try:
H.remove_node("nonexistent")
except Exception as exc: return {"exception": type(exc).__name__, "message": str(exc)}
return {"exception": None, "message": None}
def v_remove_edge_then_readd() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b"], idx="e1")
H.remove_edge("e1")
H.add_edge(["c"], idx="e1") ret = H.add_edge(["d"]) return {
"return": ret,
"edge_ids": _ids(H),
"num_edges": H.num_edges,
"num_nodes": H.num_nodes,
"node_ids": sorted(str(n) for n in H.nodes),
"members": _members_sorted(H),
}
def v_node_attr_set_read() -> dict:
H = xgi.Hypergraph()
H.add_node("a")
H.nodes["a"]["color"] = "red" return {"attrs": dict(H.nodes["a"])}
def v_clear_all() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b"]) H.add_edge(["c", "d"], idx="e1", heat=0.5)
H.add_node("lonely", x=1)
H["name"] = "test"
H.clear() state = {
"num_nodes": H.num_nodes,
"num_edges": H.num_edges,
"node_ids": sorted(str(n) for n in H.nodes),
"edge_ids": _ids(H),
"net_attrs": dict(H._net_attr),
}
H.add_edge(["z"])
state["auto_ids_after_clear"] = _ids(H)
return state
def v_add_edges_from_dup_warns_continues() -> dict:
H = xgi.Hypergraph()
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = H.add_edges_from([(["a"], "e1"), (["b"], "e1"), (["c"], "e2")])
return {
"return": ret,
"warned": len(caught) > 0,
"warning_prefix": str(caught[0].message)[:22] if caught else None,
"edge_ids": _ids(H),
"num_edges": H.num_edges,
"num_nodes": H.num_nodes,
"members": _members_sorted(H),
}
def v_eq_structural() -> dict:
def build(members, idx, edge_attr=None, node_attrs=None, net=None):
H = xgi.Hypergraph()
H.add_edge(list(members), idx=idx, **(edge_attr or {}))
for n, a in (node_attrs or {}).items():
if n in H:
H.nodes[n].update(a)
else:
H.add_node(n, **a)
for k, val in (net or {}).items():
H[k] = val
return H
A = build(["a", "b"], "e1", {"w": 1})
return {
"same": build(["a", "b"], "e1", {"w": 1}) == A,
"diff_edge_attr": build(["a", "b"], "e1", {"w": 2}) == A,
"diff_members": build(["a", "c"], "e1") == A,
"diff_edge_id": build(["a", "b"], "e2") == A,
"diff_net_attr": build(["a", "b"], "e1", {"w": 1}, net={"name": "x"}) == A,
"member_order_insignificant": build(["b", "a"], "e1", {"w": 1}) == A,
"lonely_same": build([], "e1", node_attrs={"solo": {"color": "red"}})
== build([], "e1", node_attrs={"solo": {"color": "red"}}),
"diff_node_attr": build([], "e1", node_attrs={"solo": {"color": "red"}})
== build([], "e1", node_attrs={"solo": {"color": "blue"}}),
}
def v_copy_counter_preserved() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a"]) cp = H.copy()
cp.add_edge(["b"]) return {
"h_edge_ids": _ids(H),
"cp_edge_ids": _ids(cp),
"h_num_edges": H.num_edges,
"cp_num_edges": cp.num_edges,
}
def v_copy_independence() -> dict:
H = xgi.Hypergraph()
H.add_node("a", color="red")
H.add_edge(["a", "b"], idx="e1", heat=0.5)
H["name"] = "test"
C = H.copy()
H.nodes["a"]["color"] = "blue"
H.edges["e1"]["heat"] = 0.9
H["name"] = "modified"
H.add_node("c")
return {
"node_attrs": dict(C.nodes["a"]),
"edge_attrs": dict(C.edges["e1"]),
"net_name": C["name"],
"num_nodes": C.num_nodes,
"num_edges": C.num_edges,
"has_c": "c" in C,
}
def v_add_node_to_edge_autocreate() -> dict:
H = xgi.Hypergraph()
ret_create = H.add_node_to_edge("new_edge", "new_node") H.add_edge(["a", "b"], idx="e1", heat=0.5)
H.add_node_to_edge("e1", "c") ret_readd = H.add_node_to_edge("e1", "c") H.add_node_to_edge("e1", "a") return {
"return_create": ret_create,
"return_readd": ret_readd,
"edge_ids": _ids(H),
"num_edges": H.num_edges,
"num_nodes": H.num_nodes,
"node_ids": sorted(str(n) for n in H.nodes),
"members": _members_sorted(H),
"memberships": {str(n): sorted(str(e) for e in H.nodes.memberships(n)) for n in H.nodes},
"edge_attrs_e1": dict(H.edges["e1"]),
}
def v_add_node_to_edge_numeric_id_no_bump() -> dict:
H = xgi.Hypergraph()
H.add_node_to_edge(5, "x")
H.add_edge(["y"]) return {"edge_ids": _ids(H), "members": _members_sorted(H)}
def v_remove_node_from_edge_keep_empty() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a"], idx="e1")
H.remove_node_from_edge("e1", "a", remove_empty=False)
return {
"edge_ids": _ids(H),
"num_edges": H.num_edges,
"num_nodes": H.num_nodes,
"members": _members_sorted(H),
"memberships": {str(n): sorted(str(e) for e in H.nodes.memberships(n)) for n in H.nodes},
}
def v_remove_node_from_edge_drop_empty() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b"], idx="e1")
H.add_edge(["b"], idx="e2")
H.remove_node_from_edge("e2", "b")
return {
"edge_ids": _ids(H),
"num_edges": H.num_edges,
"num_nodes": H.num_nodes,
"node_ids": sorted(str(n) for n in H.nodes),
"members": _members_sorted(H),
"memberships": {str(n): sorted(str(e) for e in H.nodes.memberships(n)) for n in H.nodes},
}
def v_membership_errors() -> dict:
out = {}
H = xgi.Hypergraph()
try:
H.remove_node_from_edge("noedge", "a")
except Exception as exc: out["missing_edge"] = {"exception": type(exc).__name__, "message": str(exc)}
H2 = xgi.Hypergraph()
H2.add_edge(["a"], idx="e1")
try:
H2.remove_node_from_edge("e1", "ghost")
except Exception as exc:
out["missing_node"] = {"exception": type(exc).__name__, "message": str(exc)}
H3 = xgi.Hypergraph()
H3.add_edge(["a"], idx="e1")
H3.add_node("b")
try:
H3.remove_node_from_edge("e1", "b")
except Exception as exc:
out["not_in_edge"] = {"exception": type(exc).__name__, "message": str(exc)}
return out
def v_remove_node_remove_empty() -> dict:
out = {}
H = xgi.Hypergraph()
H.add_edge(["a", "b"], idx="e1")
H.add_edge(["b"], idx="e2")
ret = H.remove_node("b", strong=False, remove_empty=False)
out["weak_keep"] = {
"return": ret,
"edge_ids": _ids(H),
"num_edges": H.num_edges,
"node_ids": sorted(str(n) for n in H.nodes),
"has_node_b": "b" in H,
"members": _members_sorted(H),
}
H = xgi.Hypergraph()
H.add_edge(["a", "b"], idx="e1")
H.add_edge(["b"], idx="e2")
H.remove_node("b", strong=False, remove_empty=True)
out["weak_drop"] = {
"edge_ids": _ids(H),
"num_edges": H.num_edges,
"members": _members_sorted(H),
}
H = xgi.Hypergraph()
H.add_edge(["a", "b"], idx="e1")
H.add_edge(["a", "c", "d"], idx="e2")
H.add_edge(["c", "d"], idx="e4")
H.remove_node("a", strong=True, remove_empty=False)
out["strong_ignores_flag"] = {
"edge_ids": _ids(H),
"node_ids": sorted(str(n) for n in H.nodes),
"members": _members_sorted(H),
}
return out
def v_remove_nodes_from_missing() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b"], idx="e1")
H.add_edge(["b", "c"], idx="e2")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = H.remove_nodes_from(["b", "ghost", "c"])
return {
"return": ret,
"warned": len(caught) > 0,
"warning_message": str(caught[0].message) if caught else None,
"node_ids": sorted(str(n) for n in H.nodes),
"edge_ids": _ids(H),
"members": _members_sorted(H),
}
def v_remove_edges_from_missing() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b"], idx="e1")
H.add_edge(["b", "c"], idx="e2")
H.add_edge(["c", "d"], idx="e3")
out = {}
try:
H.remove_edges_from(["e1", "ghost", "e3"])
except Exception as exc: out["exception"] = type(exc).__name__
out["message"] = str(exc)
out["edge_ids_after"] = _ids(H)
out["num_nodes"] = H.num_nodes
H2 = xgi.Hypergraph()
H2.add_edge(["a", "b"], idx="e1")
H2.add_edge(["b", "c"], idx="e2")
out["all_valid_return"] = H2.remove_edges_from(["e1", "e2"])
out["all_valid_edges"] = _ids(H2)
out["all_valid_nodes"] = sorted(str(n) for n in H2.nodes)
return out
def v_set_node_attributes_bulk() -> dict:
H = xgi.Hypergraph()
H.add_node("a", x=1)
H.add_node("b")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = H.set_node_attributes(
{"a": {"color": "red"}, "b": {"color": "blue"}, "ghost": {"color": "green"}}
)
out = {
"return": ret,
"warned": len(caught) > 0,
"warning_message": str(caught[0].message) if caught else None,
"attrs_a": dict(H.nodes["a"]), "attrs_b": dict(H.nodes["b"]),
"num_nodes": H.num_nodes, }
H2 = xgi.Hypergraph()
H2.add_node("a")
try:
H2.set_node_attributes([("a", {"k": "v"})])
except Exception as exc: out["pairs_exception"] = type(exc).__name__
out["pairs_message"] = str(exc)
return out
def v_set_edge_attributes_bulk() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a"], idx="e1", w=1)
H.add_edge(["b"], idx="e2")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = H.set_edge_attributes({"e1": {"heat": 0.5}, "ghost": {"x": 1}})
return {
"return": ret,
"warned": len(caught) > 0,
"warning_message": str(caught[0].message) if caught else None,
"attrs_e1": dict(H.edges["e1"]), "attrs_e2": dict(H.edges["e2"]),
"num_edges": H.num_edges,
}
def v_clear_edges_keeps_nodes_counter() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b"]) H.add_edge(["c", "d"], idx="e1", heat=0.5)
H.add_node("lonely", x=1)
H["name"] = "test"
ret = H.clear_edges()
state = {
"return": ret,
"num_nodes": H.num_nodes,
"num_edges": H.num_edges,
"node_ids": sorted(str(n) for n in H.nodes),
"edge_ids": _ids(H),
"lonely_attrs": dict(H.nodes["lonely"]),
"net_attrs": dict(H._net_attr),
"memberships_a": sorted(str(e) for e in H.nodes.memberships("a")),
}
H.add_edge(["z"])
state["auto_ids_after_clear_edges"] = _ids(H)
return state
def v_freeze_blocks_mutation() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b"], idx="e1", w=1)
was_frozen = H.is_frozen
H.freeze()
probes = [
("add_node", lambda: H.add_node("z")),
("add_edge", lambda: H.add_edge(["a"])),
("remove_node", lambda: H.remove_node("a")),
("remove_edge", lambda: H.remove_edge("e1")),
("add_node_to_edge", lambda: H.add_node_to_edge("e1", "c")),
("remove_node_from_edge", lambda: H.remove_node_from_edge("e1", "a")),
("clear", lambda: H.clear()),
("set_node_attributes", lambda: H.set_node_attributes({"a": {"k": 1}})),
("set_edge_attributes", lambda: H.set_edge_attributes({"e1": {"k": 1}})),
("clear_edges", lambda: H.clear_edges()),
]
guarded = {}
for name, fn in probes:
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fn()
guarded[name] = None except Exception as exc: guarded[name] = {"exception": type(exc).__name__, "message": str(exc)}
H2 = xgi.Hypergraph()
H2.add_edge(["a"], idx="e1")
H2.freeze()
return {
"is_frozen_before": was_frozen,
"is_frozen_after": H.is_frozen,
"guarded": guarded,
"copy_of_frozen_is_frozen": H2.copy().is_frozen,
}
def v_repr_format() -> dict:
H = xgi.Hypergraph()
H.add_edge(["a", "b", "c"])
H.add_edge(["b", "c"], idx="e1")
H.add_node("lonely")
empty = xgi.Hypergraph()
lone_empty_edge = xgi.Hypergraph()
lone_empty_edge.add_edge([])
return {
"repr_prefix": repr(H)[:11], "members_sorted": [sorted(str(n) for n in H.edges.members(e)) for e in H.edges],
"repr_empty": repr(empty), "repr_lone_empty_edge": repr(lone_empty_edge), }
def _di_ids(DH: xgi.DiHypergraph) -> list:
return list(DH.edges)
def _di_dimembers_sorted(DH: xgi.DiHypergraph) -> dict:
return {
str(e): {
"tail": sorted(str(n) for n in DH.edges.tail(e)),
"head": sorted(str(n) for n in DH.edges.head(e)),
}
for e in DH.edges
}
def _di_members_sorted(DH: xgi.DiHypergraph) -> dict:
return {str(e): sorted(str(n) for n in DH.edges.members(e)) for e in DH.edges}
def _di_dimemberships_sorted(DH: xgi.DiHypergraph) -> dict:
return {
str(n): [
sorted(str(e) for e in DH.nodes.dimemberships(n)[0]),
sorted(str(e) for e in DH.nodes.dimemberships(n)[1]),
]
for n in DH.nodes
}
def v_di_add_edge_dimembers() -> dict:
DH = xgi.DiHypergraph()
ret = DH.add_edge(([1, 2, 3], [2, 3, 4]))
return {
"return": ret,
"edge_ids": _di_ids(DH),
"num_nodes": DH.num_nodes,
"node_ids": sorted(str(n) for n in DH.nodes),
"dimembers": _di_dimembers_sorted(DH),
"members": _di_members_sorted(DH),
"dimemberships": _di_dimemberships_sorted(DH),
"memberships": {str(n): sorted(str(e) for e in DH.nodes.memberships(n)) for n in DH.nodes},
}
def v_di_uid_counter() -> dict:
out = {}
DH = xgi.DiHypergraph()
DH.add_edge(([1], [2]))
DH.add_edge(([3], [4]))
DH.add_edge(([5], [6]), idx=5)
DH.add_edge(([7], [8]))
out["int_idx_bumps"] = _di_ids(DH)
DH = xgi.DiHypergraph()
DH.add_edge(([1], [2]), idx="5")
DH.add_edge(([3], [4]))
out["str_idx_no_bump"] = _di_ids(DH)
DH = xgi.DiHypergraph()
DH.add_edge(([1], [2]), idx=5.0)
DH.add_edge(([3], [4]))
out["float_idx_bumps"] = _di_ids(DH)
DH = xgi.DiHypergraph()
DH.add_edge(([1], [2]), idx="x")
DH.add_edge(([3], [4]))
out["nonnumeric_idx_no_bump"] = _di_ids(DH)
return out
def v_di_dup_idx() -> dict:
DH = xgi.DiHypergraph()
DH.add_edge(([1], [2]), idx="e1")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = DH.add_edge(([3], [4]), idx="e1")
return {
"return": ret,
"warned": len(caught) > 0,
"warning_prefix": str(caught[0].message)[:22] if caught else None,
"edge_ids": _di_ids(DH),
"num_edges": DH.num_edges,
"num_nodes": DH.num_nodes,
"dimembers": _di_dimembers_sorted(DH),
}
def v_di_empty_edge() -> dict:
out = {}
DH = xgi.DiHypergraph()
ret = DH.add_edge(([], []))
out["both_empty"] = {
"return": ret,
"edge_ids": _di_ids(DH),
"num_edges": DH.num_edges,
"num_nodes": DH.num_nodes,
"dimembers": _di_dimembers_sorted(DH),
}
DH = xgi.DiHypergraph()
DH.add_edge(([], [1]))
out["empty_tail"] = {"dimembers": _di_dimembers_sorted(DH)}
DH = xgi.DiHypergraph()
DH.add_edge(([1], []))
out["empty_head"] = {"dimembers": _di_dimembers_sorted(DH)}
return out
def v_di_both_directions() -> dict:
DH = xgi.DiHypergraph()
DH.add_edge(([1, 1, 2], [2, 2, 3]), idx="e1")
return {
"dimembers": _di_dimembers_sorted(DH),
"members": _di_members_sorted(DH),
"dimemberships": _di_dimemberships_sorted(DH),
"num_nodes": DH.num_nodes,
}
def v_di_members_must_be_pair() -> dict:
out = {}
DH = xgi.DiHypergraph()
try:
DH.add_edge({1, 2, 3}) except Exception as exc: out["set_members"] = {"exception": type(exc).__name__, "message": str(exc)}
try:
DH.add_edge("abc") except Exception as exc:
out["str_members"] = {"exception": type(exc).__name__, "message": str(exc)}
try:
DH.add_edge([1, 2, 3]) except Exception as exc:
out["int_list_members"] = {"exception": type(exc).__name__, "message": str(exc)}
DH2 = xgi.DiHypergraph()
DH2.add_edge(([1], [2]), idx="e1")
try:
DH2.add_node_to_edge("e1", 3, "sideways")
except Exception as exc:
out["invalid_direction"] = {"exception": type(exc).__name__, "message": str(exc)}
return out
def v_di_add_node_to_edge() -> dict:
DH = xgi.DiHypergraph()
ret_in = DH.add_node_to_edge("newedge", "newnode", "in")
ret_out = DH.add_node_to_edge("e2", "n2", "out")
DH.add_edge(([1], [2]), idx="e1")
DH.add_node_to_edge("e1", 1, "in") DH.add_node_to_edge("e1", 1, "out") out = {
"return_in": ret_in,
"return_out": ret_out,
"edge_ids": _di_ids(DH),
"num_nodes": DH.num_nodes,
"node_ids": sorted(str(n) for n in DH.nodes),
"dimembers": _di_dimembers_sorted(DH),
"dimemberships": _di_dimemberships_sorted(DH),
}
DH2 = xgi.DiHypergraph()
DH2.add_node_to_edge(5, "x", "in")
DH2.add_edge(([1], [2])) out["numeric_id_no_bump"] = {
"edge_ids": _di_ids(DH2),
"dimembers": _di_dimembers_sorted(DH2),
}
return out
def v_di_remove_node_from_edge() -> dict:
out = {}
DH = xgi.DiHypergraph()
DH.add_edge(([1, 2], [2, 3]), idx="e1")
DH.remove_node_from_edge("e1", 2, "in", remove_empty=False)
out["direction_only"] = {
"dimembers": _di_dimembers_sorted(DH),
"dimemberships": _di_dimemberships_sorted(DH),
"edge_ids": _di_ids(DH),
"node_ids": sorted(str(n) for n in DH.nodes),
}
DH2 = xgi.DiHypergraph()
DH2.add_edge(([1], [2]), idx="e2")
DH2.remove_node_from_edge("e2", 1, "in") out["empty_means_both_sides"] = {
"edge_ids": _di_ids(DH2),
"dimembers": _di_dimembers_sorted(DH2),
}
DH2.remove_node_from_edge("e2", 2, "out")
out["both_empty_dropped"] = {
"edge_ids": _di_ids(DH2),
"node_ids": sorted(str(n) for n in DH2.nodes),
}
DH3 = xgi.DiHypergraph()
DH3.add_edge(([1], []), idx="e3")
DH3.remove_node_from_edge("e3", 1, "in", remove_empty=False)
out["keep_empty"] = {
"edge_ids": _di_ids(DH3),
"dimembers": _di_dimembers_sorted(DH3),
}
errs = {}
DH4 = xgi.DiHypergraph()
try:
DH4.remove_node_from_edge("noedge", "a", "in")
except Exception as exc: errs["missing_edge"] = {"exception": type(exc).__name__, "message": str(exc)}
DH4.add_edge(([1], [2]), idx="e1")
try:
DH4.remove_node_from_edge("e1", "ghost", "in")
except Exception as exc:
errs["missing_node"] = {"exception": type(exc).__name__, "message": str(exc)}
DH4.add_node("b")
try:
DH4.remove_node_from_edge("e1", "b", "in")
except Exception as exc:
errs["not_in_tail"] = {"exception": type(exc).__name__, "message": str(exc)}
try:
DH4.remove_node_from_edge("e1", "b", "out")
except Exception as exc:
errs["not_in_head"] = {"exception": type(exc).__name__, "message": str(exc)}
try:
DH4.remove_node_from_edge("e1", 2, "in") except Exception as exc:
errs["wrong_direction_member"] = {
"exception": type(exc).__name__,
"message": str(exc),
}
out["errors"] = errs
return out
def v_di_remove_node_modes() -> dict:
out = {}
DH = xgi.DiHypergraph()
DH.add_edge(([1, 2], [2, 3]), idx="e1")
DH.add_edge(([2], [4]), idx="e2")
DH.add_edge(([5], [2]), idx="e3")
ret = DH.remove_node(2, strong=False, remove_empty=False)
out["weak_keep"] = {
"return": ret,
"edge_ids": _di_ids(DH),
"node_ids": sorted(str(n) for n in DH.nodes),
"has_node_2": 2 in DH,
"dimembers": _di_dimembers_sorted(DH),
}
DH2 = xgi.DiHypergraph()
DH2.add_edge(([2], [4]), idx="e2")
DH2.add_edge(([2], []), idx="e5")
DH2.remove_node(2) out["weak_drop"] = {
"edge_ids": _di_ids(DH2),
"dimembers": _di_dimembers_sorted(DH2),
}
DH3 = xgi.DiHypergraph()
DH3.add_edge(([1, 2], [3]), idx="e1")
DH3.add_edge(([4], [2, 5]), idx="e2")
DH3.add_edge(([6], [7]), idx="e4")
DH3.remove_node(2, strong=True, remove_empty=False)
out["strong_ignores_flag"] = {
"edge_ids": _di_ids(DH3),
"node_ids": sorted(str(n) for n in DH3.nodes),
"dimembers": _di_dimembers_sorted(DH3),
}
DH4 = xgi.DiHypergraph()
try:
DH4.remove_node("ghost")
except Exception as exc: out["missing_node"] = {"exception": type(exc).__name__, "message": str(exc)}
DH5 = xgi.DiHypergraph()
DH5.add_edge(([1, 2], [3]), idx="e1")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = DH5.remove_nodes_from([1, "ghost", 2])
out["nodes_from_missing"] = {
"return": ret,
"warned": len(caught) > 0,
"warning_message": str(caught[0].message) if caught else None,
"node_ids": sorted(str(n) for n in DH5.nodes),
"edge_ids": _di_ids(DH5),
"dimembers": _di_dimembers_sorted(DH5),
}
DH6 = xgi.DiHypergraph()
DH6.add_edge(([1], [2]), idx="e1")
DH6.add_edge(([2], [3]), idx="e2")
DH6.add_edge(([3], [4]), idx="e3")
edges_from = {}
try:
DH6.remove_edges_from(["e1", "ghost", "e3"])
except Exception as exc: edges_from["exception"] = type(exc).__name__
edges_from["message"] = str(exc)
edges_from["edge_ids_after"] = _di_ids(DH6)
out["edges_from_missing"] = edges_from
DH7 = xgi.DiHypergraph()
try:
DH7.remove_edge("ghost")
except Exception as exc: out["remove_edge_missing"] = {
"exception": type(exc).__name__,
"message": str(exc),
}
return out
def v_di_repr() -> dict:
DH = xgi.DiHypergraph()
DH.add_edge((["a", "b", "c"], ["b", "d"]))
DH.add_edge((["a"], []), idx="e1")
DH.add_node("lonely")
empty = xgi.DiHypergraph()
both_empty = xgi.DiHypergraph()
both_empty.add_edge(([], []))
return {
"repr_prefix": repr(DH)[:13], "dimembers_sorted": [
[
sorted(str(n) for n in DH.edges.tail(e)),
sorted(str(n) for n in DH.edges.head(e)),
]
for e in DH.edges
],
"repr_empty": repr(empty), "repr_both_empty_edge": repr(both_empty), }
def v_di_eq() -> dict:
def build(edge, edge_attr=None, node_attrs=None, net=None):
members, idx = edge
DH = xgi.DiHypergraph()
DH.add_edge(members, idx=idx, **(edge_attr or {}))
for n, a in (node_attrs or {}).items():
if n in DH:
DH.nodes[n].update(a)
else:
DH.add_node(n, **a)
for k, val in (net or {}).items():
DH[k] = val
return DH
A = build(((["a", "b"], ["c"]), "e1"))
return {
"same": build(((["a", "b"], ["c"]), "e1")) == A,
"direction_flipped": build(((["c"], ["a", "b"]), "e1")) == A,
"member_order_insignificant": build(((["b", "a"], ["c"]), "e1")) == A,
"diff_edge_id": build(((["a", "b"], ["c"]), "e2")) == A,
"diff_edge_attr": build(((["a", "b"], ["c"]), "e1"), {"w": 2})
== build(((["a", "b"], ["c"]), "e1"), {"w": 1}),
"lonely_same": build(((["a", "b"], ["c"]), "e1"), None, {"solo": {"color": "red"}})
== build(((["a", "b"], ["c"]), "e1"), None, {"solo": {"color": "red"}}),
"diff_node_attr": build(((["a", "b"], ["c"]), "e1"), None, {"solo": {"color": "red"}})
== build(((["a", "b"], ["c"]), "e1"), None, {"solo": {"color": "blue"}}),
"lonely_missing": build(((["a", "b"], ["c"]), "e1"))
== build(((["a", "b"], ["c"]), "e1"), None, {"solo": {"color": "red"}}),
"diff_net_attr": build(((["a", "b"], ["c"]), "e1"), {"w": 1}, None, {"name": "x"})
== build(((["a", "b"], ["c"]), "e1"), {"w": 1}),
"both_vs_tail_only": build(((["a", "b"], ["b"]), "e1")) == build(((["a", "b"], []), "e1")),
"empty_attr_lonely_vs_absent": build(((["a", "b"], ["c"]), "e1"), None, {"solo": {}})
== build(((["a", "b"], ["c"]), "e1")),
}
def v_di_add_edges_from() -> dict:
out = {}
DH = xgi.DiHypergraph()
ret = DH.add_edges_from(
[
( (["a", "b"], ["c"])
),
(["d"], ["e", "f"]),
]
)
out["members_only"] = {
"return": ret,
"edge_ids": _di_ids(DH),
"dimembers": _di_dimembers_sorted(DH),
}
DH = xgi.DiHypergraph()
DH.add_edges_from( [
((["a"], ["b"]), "one", {"color": "red"}),
((["c"], []), "two", {"color": "blue", "age": 40}),
]
)
out["with_idx_attrs"] = {
"edge_ids": _di_ids(DH),
"attrs": {str(e): dict(DH.edges[e]) for e in DH.edges},
"dimembers": _di_dimembers_sorted(DH),
}
DH = xgi.DiHypergraph()
DH.add_edges_from( [((["a"], ["b"]), {"color": "red"})], color="blue", size=10
)
out["broadcast"] = {str(e): dict(DH.edges[e]) for e in DH.edges}
DH = xgi.DiHypergraph()
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = DH.add_edges_from(
[
((["a"], ["b"]), "e1"),
((["c"], ["d"]), "e1"), ((["e"], ["f"]), "e2"),
]
)
out["dup_warns_continues"] = {
"return": ret,
"warned": len(caught) > 0,
"warning_prefix": str(caught[0].message)[:22] if caught else None,
"edge_ids": _di_ids(DH),
"num_nodes": DH.num_nodes,
"dimembers": _di_dimembers_sorted(DH),
}
DH = xgi.DiHypergraph()
DH.add_edges_from([([], []), (["a"], ["b"])])
out["empty_created"] = {
"edge_ids": _di_ids(DH),
"dimembers": _di_dimembers_sorted(DH),
}
return out
def v_di_set_attrs() -> dict:
out = {}
DH = xgi.DiHypergraph()
DH.add_edge((["a", "b"], ["c"]), idx="e1")
DH.nodes["a"].update({"x": 1})
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = DH.set_node_attributes(
{"a": {"color": "red"}, "b": {"color": "blue"}, "ghost": {"color": "green"}}
)
out["node_dict_of_dicts"] = {
"return": ret,
"warned": len(caught) > 0,
"warning_message": str(caught[0].message) if caught else None,
"attrs_a": dict(DH.nodes["a"]), "attrs_b": dict(DH.nodes["b"]),
"num_nodes": DH.num_nodes, }
DH2 = xgi.DiHypergraph()
DH2.add_node("a")
try:
DH2.set_node_attributes([("a", {"k": "v"})])
except Exception as exc: out["node_pairs_exception"] = type(exc).__name__
out["node_pairs_message"] = str(exc)
DH3 = xgi.DiHypergraph()
DH3.add_edge((["a", "b"], ["c"]), idx="e1")
DH3.set_node_attributes("red", name="color")
out["node_scalar_broadcast"] = {
"attrs_a": dict(DH3.nodes["a"]),
"attrs_c": dict(DH3.nodes["c"]),
}
DH4 = xgi.DiHypergraph()
DH4.add_edge((["a", "b"], ["c"]), idx="e1")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
DH4.set_node_attributes({"a": "red", "ghost": "green"}, name="color")
out["node_dict_of_scalars"] = {
"warned": len(caught) > 0,
"warning_message": str(caught[0].message) if caught else None,
"attrs_a": dict(DH4.nodes["a"]),
"attrs_b": dict(DH4.nodes["b"]),
}
DH5 = xgi.DiHypergraph()
DH5.add_edge((["a"], ["b"]), idx="e1", w=1)
DH5.add_edge((["c"], ["d"]), idx="e2")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = DH5.set_edge_attributes({"e1": {"heat": 0.5}, "ghost": {"x": 1}})
out["edge_dict_of_dicts"] = {
"return": ret,
"warned": len(caught) > 0,
"warning_message": str(caught[0].message) if caught else None,
"attrs_e1": dict(DH5.edges["e1"]), "attrs_e2": dict(DH5.edges["e2"]),
"num_edges": DH5.num_edges,
}
return out
def v_di_clear_freeze() -> dict:
DH = xgi.DiHypergraph()
DH.add_edge((["a", "b"], ["c"])) DH.add_edge((["d"], ["e"]), idx="e1", heat=0.5)
DH.add_node("lonely", x=1)
DH["name"] = "test"
DH.clear()
out = {
"clear": {
"num_nodes": DH.num_nodes,
"num_edges": DH.num_edges,
"node_ids": sorted(str(n) for n in DH.nodes),
"edge_ids": _di_ids(DH),
"net_attrs": dict(DH._net_attr),
}
}
DH.add_edge((["z"], []))
out["clear"]["auto_ids_after_clear"] = _di_ids(DH)
DH = xgi.DiHypergraph()
DH.add_edge((["a", "b"], ["c"]), idx="e1", w=1)
was_frozen = DH.is_frozen
DH.freeze()
probes = [
("add_node", lambda: DH.add_node("z")),
("add_nodes_from", lambda: DH.add_nodes_from(["z"])),
("add_edge", lambda: DH.add_edge((["a"], ["b"]))),
("add_edges_from", lambda: DH.add_edges_from([(["a"], ["b"])])),
("remove_node", lambda: DH.remove_node("a")),
("remove_nodes_from", lambda: DH.remove_nodes_from(["a"])),
("remove_edge", lambda: DH.remove_edge("e1")),
("remove_edges_from", lambda: DH.remove_edges_from(["e1"])),
("add_node_to_edge", lambda: DH.add_node_to_edge("e1", "n", "in")),
("remove_node_from_edge", lambda: DH.remove_node_from_edge("e1", "a", "in")),
("clear", lambda: DH.clear()),
("set_node_attributes", lambda: DH.set_node_attributes({"a": {"k": 1}})),
("set_edge_attributes", lambda: DH.set_edge_attributes({"e1": {"k": 1}})),
("clear_edges", lambda: DH.clear_edges()),
]
guarded = {}
for name, fn in probes:
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fn()
guarded[name] = None except Exception as exc: guarded[name] = {"exception": type(exc).__name__, "message": str(exc)}
DH2 = xgi.DiHypergraph()
DH2.add_edge((["a"], ["b"]))
DH2.freeze()
out["freeze"] = {
"is_frozen_before": was_frozen,
"is_frozen_after": DH.is_frozen,
"guarded": guarded,
"copy_of_frozen_is_frozen": DH2.copy().is_frozen,
}
return out
def _sc_members_sorted(S: xgi.SimplicialComplex) -> dict:
return {str(e): sorted(str(n) for n in S.edges.members(e)) for e in S.edges}
def v_sc_add_simplex_closure() -> dict:
out = {}
S = xgi.SimplicialComplex()
ret = S.add_simplex([1, 2, 3])
out["three"] = {
"return": ret,
"edge_ids": list(S.edges),
"members": _sc_members_sorted(S),
}
S = xgi.SimplicialComplex()
S.add_simplex([1, 2, 3, 4])
out["four"] = {"edge_ids": list(S.edges), "members": _sc_members_sorted(S)}
S = xgi.SimplicialComplex()
S.add_simplex([1, 2])
out["two"] = {"edge_ids": list(S.edges), "members": _sc_members_sorted(S)}
S = xgi.SimplicialComplex()
S.add_simplex([1, 2, 3], idx="top")
out["str_idx_no_bump"] = {
"edge_ids": list(S.edges),
"members": _sc_members_sorted(S),
}
S = xgi.SimplicialComplex()
S.add_simplex([1, 2, 3], idx=10)
out["int_idx_bumps"] = {"edge_ids": list(S.edges), "members": _sc_members_sorted(S)}
return out
def v_sc_redundant_simplex() -> dict:
S = xgi.SimplicialComplex()
S.add_simplex([1, 2, 3])
out: dict = {"num_edges_before": S.num_edges}
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = S.add_simplex([3, 2, 1])
out["reorder"] = {
"return": ret,
"warned": len(caught) > 0,
"num_edges": S.num_edges,
"edge_ids": list(S.edges),
}
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = S.add_simplex([3, 2, 1], idx="newid")
out["reorder_new_idx"] = {
"return": ret,
"warned": len(caught) > 0,
"num_edges": S.num_edges,
"edge_ids": list(S.edges),
}
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = S.add_simplex([1, 2])
out["existing_face"] = {
"return": ret,
"warned": len(caught) > 0,
"num_edges": S.num_edges,
"edge_ids": list(S.edges),
}
return out
def v_sc_dup_idx() -> dict:
S = xgi.SimplicialComplex()
S.add_simplex([1, 2, 3], idx="s1")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
ret = S.add_simplex([4, 5], idx="s1")
return {
"return": ret,
"warned": len(caught) > 0,
"warning_prefix": str(caught[0].message)[:22] if caught else None,
"edge_ids": list(S.edges),
"num_nodes": S.num_nodes,
"num_edges": S.num_edges,
"members": _sc_members_sorted(S),
}
def v_sc_empty_simplex() -> dict:
S = xgi.SimplicialComplex()
ret = S.add_simplex([])
out = {
"return": ret,
"edge_ids": list(S.edges),
"num_nodes": S.num_nodes,
"num_edges": S.num_edges,
"members": _sc_members_sorted(S),
"has_empty": S.has_simplex([]),
}
ret2 = S.add_simplex([])
out["again"] = {"return": ret2, "num_edges": S.num_edges, "edge_ids": list(S.edges)}
return out
def v_sc_attrs_no_propagate() -> dict:
S = xgi.SimplicialComplex()
S.add_simplex([1, 2, 3], color="red")
return {"attrs": {str(e): dict(S.edges[e]) for e in S.edges}}
def v_sc_has_simplex() -> dict:
S = xgi.SimplicialComplex([[1, 2], [2, 3, 4]])
return {
"reordered": S.has_simplex([2, 1]),
"missing_set": S.has_simplex({1, 3}),
"top": S.has_simplex([2, 3, 4]),
"closure_face": S.has_simplex([2, 3]),
"no_singletons": S.has_simplex([2]),
}
def v_sc_falsy_idx_is_auto() -> dict:
S = xgi.SimplicialComplex()
S.add_simplex([1, 2, 3], idx=5)
out = {"after_int_idx": list(S.edges)}
S.add_simplex([4, 5], idx=0)
out["zero_idx_edges"] = list(S.edges)
S2 = xgi.SimplicialComplex()
S2.add_simplex([1, 2], idx="")
out["empty_str_idx_edges"] = list(S2.edges)
return out
def main() -> None:
vectors = {
name.removeprefix("v_"): fn()
for name, fn in sorted(globals().items())
if name.startswith("v_") and callable(fn)
}
payload = {
"xgi_version": xgi.__version__,
"generated_by": "conformance/generate_fixtures.py",
"vectors": vectors,
}
OUT.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n")
print(f"wrote {OUT} ({len(vectors)} vectors, xgi {xgi.__version__})")
if __name__ == "__main__":
main()