Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/4499.feat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{func}`~scanpy.tl.leiden` with `flavor="leidenalg"` clusters several graphs at once: a list in `use`, weighted by `layer_weights`, with one `resolution` for all or one per graph {smaller}`M Kuehl`
1 change: 1 addition & 0 deletions docs/release-notes/4499.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{func}`~scanpy.tl.leiden` with `flavor="networkit"` works without `igraph`, and with {attr}`~scanpy.Preset.ScanpyV2Preview` uses edges stored in only one direction of the adjacency matrix {smaller}`M Kuehl`
7 changes: 7 additions & 0 deletions src/scanpy/_utils/__init__.py

@maltekuehl maltekuehl Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This used to only keep the upper triangle, which could drop unidirectional connections. Since it was already included in a release like this, I am gating it behind a v2 flag and having it match the max-preserving behavior of the v2 igraph variant.

Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,19 @@ def get_igraph_from_adjacency(adjacency: CSBase, *, directed: bool) -> Graph:
def get_networkit_from_adjacency(adjacency: CSBase, *, weighted: bool = True):
"""Get a NetworKit graph from an adjacency matrix.

Each edge has the weight in the upper triangle,
or with ScanpyV2Preview the larger weight of both directions.
Used func:`networkit.GraphFromCsr` (networkit/networkit#1422)
"""
import networkit as nk
import scipy.sparse as sps

import scanpy as sc

assert adjacency.shape is not None
if sc.settings.preset is sc.Preset.ScanpyV2Preview:
adjacency = sps.csr_array(adjacency) # noqa: TID251
adjacency = adjacency.maximum(adjacency.T)
upper = sps.triu(adjacency, k=1).tocsr()
return nk.GraphFromCsr(upper, directed=False, weighted=weighted)

Expand Down
7 changes: 4 additions & 3 deletions src/scanpy/metrics/_metrics.py

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiplex Leiden returns a modularity per graph.

Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def modularity(
use: GraphAcc | str | None = None,
mode: Literal["calculate", "update", "retrieve"] = "calculate",
neighbors_key: str | None = None,
) -> float: ...
) -> float | list[float]: ...


@_doc_params(use=doc_use_neighbors())
Expand All @@ -140,7 +140,7 @@ def modularity(
mode: Literal["calculate", "update", "retrieve"] = "calculate",
# deprecated
neighbors_key: str | None = None,
) -> float:
) -> float | list[float]:
"""Compute the modularity of a graph given its connectivities and labels.

Parameters
Expand All @@ -165,6 +165,7 @@ def modularity(
Returns
-------
The modularity of the graph based on the provided clustering.
A list with `mode='retrieve'` if :func:`~scanpy.tl.leiden` clustered several graphs.
"""
if isinstance(adata_or_connectivities, AnnData):
if is_directed:
Expand Down Expand Up @@ -196,7 +197,7 @@ def modularity_adata(
use: GraphAcc | str | None,
neighbors_key: str | None,
mode: Literal["calculate", "update", "retrieve"],
) -> float:
) -> float | list[float]:
if mode in {"retrieve", "update"} and not isinstance(labels, str):
msg = "`labels` must be a string when `mode` is `'retrieve'` or `'update'`"
raise ValueError(msg)
Expand Down
Loading
Loading