py3plex

py3plex is a Python library for analyzing, querying, and visualizing multilayer and multiplex networks—graphs where nodes interact across multiple layers or relation types.
Installation
pip install py3plex
Or use uv pip install py3plex inside an activated virtual environment.
Quickstart
Load a built-in dataset and query node replicas with aggregate degree above five:
from py3plex.datasets import load_aarhus_cs
from py3plex.dsl import Q
network = load_aarhus_cs()
result = (Q.nodes().where(degree__gt=5)
.compute("degree_centrality").execute(network))
print(result)
print(result.to_pandas()[["id", "layer", "degree_centrality"]].head())
Rows identify node replicas (id, layer), not distinct physical nodes.
Continue with the 10-Minute Tutorial.
Virtual environments and optional feature extras
uv venv .venv && source .venv/bin/activate
uv pip install py3plex
pip install py3plex[algos] # Community detection & extra algorithms
pip install py3plex[viz] # Advanced visualization
pip install py3plex[mcp] # MCP server for AI agents (Python 3.10+)
pip install py3plex[optional] # Algorithms + visualization + workflows + Arrow + MCP
uv pip install -e ".[dev]" # From source with dev dependencies
Key Features
- Fluent DSL v2 (
Q / L / UQ) & SQL-like queries for multilayer filtering, per-layer grouping, and uncertainty quantification
- Multilayer algorithms: centrality, community detection, random walks, and epidemic dynamics
- Visualization and NetworkX / pandas / Arrow interoperability
- Reproducible workflows: seeded execution, null models, CLI, Docker, and MCP
Documentation Map
| Resource |
Best For |
Location |
| User & API Docs |
Tutorials, how-to guides, DSL & API reference |
Online Docs · docfiles/ |
| Technical Book (PDF) |
Theory and case studies |
py3plex_book.pdf · book/ |
| Runnable Examples |
Standalone analysis scripts |
examples/ |
| Agent Playbook |
End-to-end rules & recipes for LLM coding agents |
AGENTS.md |

End-to-End Example (Communities + UQ + Explanations)
Expand a full workflow combining community detection, cross-layer hubs, and uncertainty quantification
from py3plex.datasets import make_random_multiplex
from py3plex.dsl import Q
network = make_random_multiplex(
n_nodes=24, n_layers=3, p=0.25, random_state=42,
)
assignments = (
Q.communities().auto(fast=True, seed=42).execute(network)
.to_pandas()[["node", "layer", "community"]]
.rename(columns={"node": "id"})
)
hubs = (
Q.nodes()
.compute("betweenness_centrality")
.per_layer()
.top_k(8, "betweenness_centrality")
.end_grouping()
.coverage(mode="at_least", k=2)
.execute(network)
)
estimates = (
Q.nodes()
.uq(method="perturbation", n_samples=20, ci=0.95, seed=42)
.compute("betweenness_centrality", "pagerank", "degree_centrality")
.explain(neighbors_top=5)
.execute(network)
)
selected = hubs.to_pandas()[["id", "layer"]]
df = selected.merge(
estimates.to_pandas(expand_uncertainty=True, expand_explanations=True),
on=["id", "layer"],
validate="one_to_one",
).merge(assignments, on=["id", "layer"], validate="one_to_one")
df["score"] = (
0.5 * df["betweenness_centrality"]
+ 0.3 * df["pagerank"]
+ 0.2 * df["degree_centrality"]
)
print(df.sort_values("score", ascending=False)[[
"id", "layer", "community", "betweenness_centrality",
"betweenness_centrality_ci95_low", "betweenness_centrality_ci95_high",
"score", "top_neighbors",
]].head(10))
The hub query selects eight replicas per layer, then keeps physical nodes
selected in at least two layers. The final table joins community assignments
with centrality estimates and neighbor explanations. betweenness_centrality
holds the estimated mean; _ci95_low and _ci95_high hold its interval bounds.
The weighted score is an illustrative ranking, not a calibrated measure.
When to Use py3plex
Use py3plex when layer identity matters: the same entity can have separate
replicas in several layers, connected by within-layer and between-layer edges.
The query builder supports layer selection, grouped rankings, and uncertainty
estimates. For a single-layer graph without these requirements, NetworkX may
be sufficient.
Contributing (First PR Quick Path)
# 1) Create local dev environment and install package
make setup
make dev-install
# 2) Run formatting and checks before opening a PR
make format
make lint
make test
Helpful commands:
make help # List all project commands
make ci # Run lint + tests in CI-style order
Safety notes for contributors:
- Keep changes focused and add tests next to the feature you modify.
- Do not add new markdown files unless explicitly requested (enforced by
tests/test_link_checker.py to prevent documentation drift).
- Prefer running targeted tests first, then broader checks before opening a PR.
- For docs-only updates, run
make docs-check before opening a PR.
To report a bug, request a feature, or ask for help using py3plex, open an issue in the issue tracker. Include a minimal example and the py3plex and Python versions where relevant.
MCP Integration (AI Agents)
py3plex provides a Model Context Protocol (MCP) server for integration with AI coding assistants (requires Python 3.10+):
pip install py3plex[mcp]
py3plex-mcp
Configure Claude Desktop (claude_desktop_config.json):
{
"mcpServers": {
"py3plex": {
"command": "py3plex-mcp"
}
}
}
The MCP server exposes 7 tools (load networks, DSL v2 queries, community detection, exports) and 3 resources (documentation, DSL v2 reference, tool schemas). See AGENTS.md for complete MCP documentation.
License
Py3plex is released under the MIT License. Prior to version 1.0, the project was distributed under the BSD-3-Clause license.
Citation
@Article{Skrlj2019,
author={Skrlj, Blaz and Kralj, Jan and Lavrac, Nada},
title={Py3plex toolkit for visualization and analysis of multilayer networks},
journal={Applied Network Science},
year={2019},
volume={4},
number={1},
pages={94},
doi={10.1007/s41109-019-0203-7},
url={https://doi.org/10.1007/s41109-019-0203-7}
}
@InProceedings{10.1007/978-3-030-05411-3_60,
author="{\v{S}}krlj, Bla{\v{z}} and Kralj, Jan and Lavra{\v{c}}, Nada",
editor="Aiello, Luca Maria and Cherifi, Chantal and Cherifi, Hocine and Lambiotte, Renaud and Li{\'o}, Pietro and Rocha, Luis M.",
title="Py3plex: A Library for Scalable Multilayer Network Analysis and Visualization",
booktitle="Complex Networks and Their Applications VII",
year="2019",
publisher="Springer International Publishing",
address="Cham",
pages="757--768",
isbn="978-3-030-05411-3"
}
评论