babylon.engine.graph_wrappers

Typed graph wrappers for Spec 040 Discipline 5.

Enforces separation between dyadic edges (NetworkX DiGraph) and hyperedges (XGI Hypergraph). Systems access graph topology through these typed wrappers, not raw data structures.

DyadicGraph: Wraps nx.DiGraph for pairwise (source → target) edges. CommunityHypergraph: Wraps xgi.Hypergraph for n-ary community membership.

Classes

CommunityHypergraph([hypergraph])

Typed wrapper for XGI Hypergraph (n-ary community membership).

DyadicGraph(graph)

Typed wrapper for NetworkX DiGraph (pairwise edges).

class babylon.engine.graph_wrappers.DyadicGraph(graph)[source]

Bases: object

Typed wrapper for NetworkX DiGraph (pairwise edges).

Provides typed edge operations that enforce EdgeType on all edge queries and mutations.

Parameters:

graph (nx.DiGraph[str])

raw

The underlying NetworkX DiGraph.

__init__(graph)[source]

Wrap a NetworkX DiGraph.

Parameters:

graph – The raw NetworkX directed graph.

property raw: nx.DiGraph[str]

Access the underlying raw graph.

add_edge(source, target, edge_type, **attrs)[source]

Add a typed dyadic edge.

Parameters:
  • source (str) – Source node ID.

  • target (str) – Target node ID.

  • edge_type (EdgeType) – Type of the edge.

  • **attrs (Any) – Additional edge attributes.

Return type:

None

edges_of_type(edge_type)[source]

Iterate edges of a specific type.

Parameters:

edge_type (EdgeType) – Filter to this edge type.

Yields:

(source, target) tuples for matching edges.

Return type:

Iterator[tuple[str, str]]

__len__()[source]

Number of nodes in the graph.

Return type:

int

class babylon.engine.graph_wrappers.CommunityHypergraph(hypergraph=None)[source]

Bases: object

Typed wrapper for XGI Hypergraph (n-ary community membership).

Provides typed community operations for adding communities, querying membership, and computing overlap.

Parameters:

hypergraph (xgi.Hypergraph | None)

__init__(hypergraph=None)[source]

Initialize with an optional existing hypergraph.

Parameters:

hypergraph (Hypergraph | None) – Existing XGI Hypergraph, or None for empty.

Return type:

None

property raw: Hypergraph

Access the underlying raw hypergraph.

add_community(community_id, members, **attrs)[source]

Add a community as a hyperedge.

Parameters:
  • community_id (str) – Unique community identifier.

  • members (list[str]) – List of agent IDs in this community.

  • **attrs (Any) – Additional community attributes.

Return type:

None

property community_ids: set[Any]

Set of all community IDs (hyperedge IDs).

members_of(community_id)[source]

Get members of a community.

Parameters:

community_id (str) – The community hyperedge ID.

Return type:

list[str]

Returns:

List of agent IDs in the community.

shared_communities(agent_a, agent_b)[source]

Find communities shared by two agents.

Parameters:
  • agent_a (str) – First agent ID.

  • agent_b (str) – Second agent ID.

Return type:

set[Any]

Returns:

Set of community IDs containing both agents.

__len__()[source]

Number of communities (hyperedges).

Return type:

int