Conversation
…egration Co-authored-by: SkBlaz <10035780+SkBlaz@users.noreply.github.com>
Co-authored-by: SkBlaz <10035780+SkBlaz@users.noreply.github.com>
Co-authored-by: SkBlaz <10035780+SkBlaz@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Implement multilayer Leiden community detection with uncertainty quantification
Implement production-quality multilayer Leiden community detection with uncertainty quantification
Jan 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds production-ready multilayer/multiplex Leiden algorithm with first-class uncertainty quantification (UQ) and DSL integration. The implementation provides ensemble-based stability analysis, deterministic execution, and comprehensive diagnostics for multilayer modularity optimization.
Core Implementation
multilayer_leiden(network, gamma=1.0, omega=1.0, ...)Q = (1/2μ) Σ [(A_ijs - γ_s k_is k_js/2m_s)δ_sr + δ_ij ω_sr] δ(g_is, g_jr)random_state=None→seed=0(node, layer) → comm_idand modularity scoremultilayer_leiden_uq(network, n_runs=20, method="seed", ...)seed(Monte Carlo),perturbation(structural),bootstrap(resampling)UQResultwith: consensus partition, stability metrics (VI/NMI distributions, node entropy, pairwise agreement), confidence intervals, diagnosticscanonicalize_partition(partition, node_order)0, 1, 2, ...by order of first appearanceDSL Integration
Added
.community(method="leiden", gamma=..., omega=...)operator to query builder:Usage Example
Testing
Added 23 comprehensive tests covering:
Documentation
examples/network_analysis/example_multilayer_leiden_uq.pydemonstrates all features (parameter sweeps, UQ, DSL integration)Key Attributes
random_state=Nonedefaults toseed=0; same seed yields bit-identical resultsNotes
DSL executor integration (connecting
.community()operator to execution engine) is deferred to follow-up PR. Basic operator structure is in place and functional.Original prompt
This section details on the original issue you should resolve
<issue_title>leiden</issue_title>
<issue_description>
You are working in the py3plex repo. Implement a production-quality multilayer/multiplex Leiden community detection algorithm with uncertainty quantification (UQ) and first-class DSL integration.
HARD CONSTRAINTS
Do NOT add any new .md files.
Update existing RST docs (where community detection + DSL are documented).
Update AGENTS.md (the repo’s agent/dev guide) with the new APIs, defaults, invariants, and testing strategy.
Add/extend examples (prefer existing example files) that demonstrate multilayer Leiden + UQ end-to-end.
Add thorough tests, including property-based tests + determinism tests.
Make everything deterministic given a seed.
Keep runtime reasonable; provide fast-paths and graceful fallbacks.
PRIMARY GOAL Provide:
multilayer_leiden(network, ...) -> (partition_vector, modularity_like_score, diagnostics)
multilayer_leiden_uq(network, ...) -> UQResult (ensemble runs + uncertainty summaries)
DSL v2 support: Q.nodes().community(method="leiden", ...) and ... .uq(method="ensemble", ...) integration.
DEFINITIONAL SCOPE
“Multilayer/multiplex” network: L layers with intralayer edges + optional interlayer coupling edges (node identity links across layers).
Support both: (A) Supra-graph formulation (recommended default): build supra adjacency with interlayer coupling ω. (B) Multi-slice modularity objective: equivalent to supra if identity links used; keep naming consistent.
Objective: modularity-like quality for multilayer (with resolution γ and coupling ω). Provide explicit formulas in docs.
Algorithm: Leiden (local moving + refinement + aggregation), adapted for supra-graph (simpler and robust), but expose multilayer params.
PUBLIC API DESIGN Add to py3plex.algorithms.community_detection (or existing module structure):
multilayer_leiden(network, gamma=1.0, omega=1.0, n_iterations=2, random_state=None, init_partition=None, allow_isolates=True, return_diagnostics=False, backend="auto") Returns:
partition_vector: mapping compatible with network.assign_partition(...) (consistent with existing conventions)
score: modularity-like value
diagnostics dict if return_diagnostics=True (timings, #moves, #communities per level, convergence info)
multilayer_leiden_uq(network, gamma=1.0, omega=1.0, n_runs=20, seeds=None, n_iterations=2, agg="consensus", ci=0.95, random_state=None, return_all=False) Returns UQResult with:
partitions: list (optional)
scores: list
consensus_partition
membership_probs (node -> community distribution OR node-pair co-assignment matrix approximation)
stability_metrics (VI/NMI distribution, pairwise agreement, community persistence)
ci and summary (score mean/std/CI; #communities mean/std/CI)
diagnostics (seed table, runtime per run, failure counts)
DEFAULTS (SENSIBLE + DUMMY-PROOF)
gamma=1.0, omega=1.0, n_iterations=2
random_state=None means deterministic fallback seed = 0 (document it clearly).
backend="auto" chooses best available implementation:
If igraph/leidenalg available and consistent with licensing/packaging, optionally use; otherwise implement native.
Regardless of backend, output format must be identical.
UQ defaults: n_runs=20, agg="consensus", ci=0.95. If seeds not given, derive seeds from random_state deterministically (e.g., numpy SeedSequence).
INTEGRATION POINTS A) Network representation
Identify how multilayer networks are represented in py3plex (layers, node ids, edge lists).
Implement a small adapter: “to_supra_graph(network, omega, ensure_identity_links=True/False)”
Ensure stable node ordering across runs and machines (sort keys, stable mapping).
B) DSL v2
Add .community(method="leiden", gamma=..., omega=..., ...) operator in the DSL.
Make .uq(...) combine cleanly: Example target usage:
res = (
Q.nodes()
.node_type("gene")
.community(method="leiden", gamma=1.2, omega=0.8, random_state=42)
.uq(method="ensemble", n_runs=50, ci=0.95, seed=42)
.summarize("community", "size", "score")
)
Ensure query planning / provenance captures:
chosen backend
gamma/omega/n_runs
seed strategy
objective score stats
C) Result objects
If the project uses QueryResult or similar, ensure:
partitions can be attached to the network (network.assign_partition)
UQ output is accessible both programmatically and via result.meta / result.provenance
UQ SPEC (IMPORTANT) Implement UQ via an ensemble of Leiden runs over:
different random seeds (mandatory)
optional perturbation:
edge resampling / dropout (bootstrap-like) for robust partitions, controlled by uq.method="perturbation" already present in repo (hook into it if exists). Provide:
consensus partition:
default: build co-assignment similarity matrix (approximate if too big) and run a final clustering (e.g., hierarchical/Leiden on similarity graph) OR choose medoid partition by minimizing mean VI.
uncertainty quantification:
p...
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.