Skip to content

Commit 51cfd82

Browse files
LarytheLordflying-sheep
authored andcommitted
Backport PR #3986: docs: deduplicate louvain and leiden parameter docs
1 parent d348605 commit 51cfd82

4 files changed

Lines changed: 86 additions & 37 deletions

File tree

docs/release-notes/3986.doc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clarify and deduplicate shared parameter documentation between {func}`scanpy.tl.leiden` and {func}`scanpy.tl.louvain` {smaller}`Abi`

src/scanpy/tools/_docs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Shared docstrings for tools function parameters."""
2+
3+
from __future__ import annotations
4+
5+
doc_adata = """\
6+
adata
7+
The annotated data matrix.\
8+
"""
9+
10+
doc_random_state = """\
11+
random_state
12+
Change the initialization of the optimization.\
13+
"""
14+
15+
doc_restrict_to = """\
16+
restrict_to
17+
Restrict the clustering to the categories within the key for sample
18+
annotation, tuple needs to contain `(obs_key, list_of_categories)`.\
19+
"""
20+
21+
doc_adjacency = """\
22+
adjacency
23+
Sparse adjacency matrix of the graph, defaults to neighbors connectivities.\
24+
"""
25+
26+
doc_neighbors_key = """\
27+
neighbors_key
28+
Use neighbors connectivities as adjacency.
29+
If not specified, {method} looks at .obsp['connectivities'] for connectivities
30+
(default storage place for pp.neighbors).
31+
If specified, {method} looks at
32+
.obsp[.uns[neighbors_key]['connectivities_key']] for connectivities.\
33+
"""
34+
35+
doc_obsp = """\
36+
obsp
37+
Use .obsp[obsp] as adjacency. You can't specify both
38+
`obsp` and `neighbors_key` at the same time.\
39+
"""

src/scanpy/tools/_leiden.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99
from .. import _utils
1010
from .. import logging as logg
1111
from .._compat import warn
12+
from .._utils import _doc_params
1213
from .._utils.random import set_igraph_random_state
14+
from ._docs import (
15+
doc_adata,
16+
doc_adjacency,
17+
doc_neighbors_key,
18+
doc_obsp,
19+
doc_random_state,
20+
doc_restrict_to,
21+
)
1322
from ._utils_clustering import rename_groups, restrict_adjacency
1423

1524
if TYPE_CHECKING:
@@ -29,6 +38,14 @@
2938
MutableVertexPartition.__module__ = "leidenalg.VertexPartition"
3039

3140

41+
@_doc_params(
42+
doc_adata=doc_adata,
43+
random_state=doc_random_state,
44+
restrict_to=doc_restrict_to,
45+
adjacency=doc_adjacency,
46+
neighbors_key=doc_neighbors_key.format(method="leiden"),
47+
obsp=doc_obsp,
48+
)
3249
def leiden( # noqa: PLR0913
3350
adata: AnnData,
3451
resolution: float = 1,
@@ -60,22 +77,17 @@ def leiden( # noqa: PLR0913
6077
6178
Parameters
6279
----------
63-
adata
64-
The annotated data matrix.
80+
{doc_adata}
6581
resolution
6682
A parameter value controlling the coarseness of the clustering.
6783
Higher values lead to more clusters.
6884
Set to `None` if overriding `partition_type`
6985
to one that doesn’t accept a `resolution_parameter`.
70-
random_state
71-
Change the initialization of the optimization.
72-
restrict_to
73-
Restrict the clustering to the categories within the key for sample
74-
annotation, tuple needs to contain `(obs_key, list_of_categories)`.
86+
{random_state}
87+
{restrict_to}
7588
key_added
7689
`adata.obs` key under which to add the cluster labels.
77-
adjacency
78-
Sparse adjacency matrix of the graph, defaults to neighbors connectivities.
90+
{adjacency}
7991
directed
8092
Whether to treat the graph as directed or undirected.
8193
use_weights
@@ -91,15 +103,8 @@ def leiden( # noqa: PLR0913
91103
Defaults to :class:`~leidenalg.RBConfigurationVertexPartition`.
92104
For the available options, consult the documentation for
93105
:func:`~leidenalg.find_partition`.
94-
neighbors_key
95-
Use neighbors connectivities as adjacency.
96-
If not specified, leiden looks at .obsp['connectivities'] for connectivities
97-
(default storage place for pp.neighbors).
98-
If specified, leiden looks at
99-
.obsp[.uns[neighbors_key]['connectivities_key']] for connectivities.
100-
obsp
101-
Use .obsp[obsp] as adjacency. You can't specify both
102-
`obsp` and `neighbors_key` at the same time.
106+
{neighbors_key}
107+
{obsp}
103108
copy
104109
Whether to copy `adata` or modify it inplace.
105110
flavor

src/scanpy/tools/_louvain.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
from .. import _utils
1212
from .. import logging as logg
1313
from .._compat import deprecated, old_positionals, pkg_version, warn
14-
from .._utils import _choose_graph, dematrix
14+
from .._utils import _choose_graph, _doc_params, dematrix
15+
from ._docs import (
16+
doc_adata,
17+
doc_adjacency,
18+
doc_neighbors_key,
19+
doc_obsp,
20+
doc_random_state,
21+
doc_restrict_to,
22+
)
1523
from ._utils_clustering import rename_groups, restrict_adjacency
1624

1725
if TYPE_CHECKING:
@@ -46,6 +54,14 @@
4654
"copy",
4755
)
4856
@deprecated("Use `scanpy.tl.leiden` instead")
57+
@_doc_params(
58+
doc_adata=doc_adata,
59+
random_state=doc_random_state,
60+
restrict_to=doc_restrict_to,
61+
adjacency=doc_adjacency,
62+
neighbors_key=doc_neighbors_key.format(method="louvain"),
63+
obsp=doc_obsp,
64+
)
4965
def louvain( # noqa: PLR0912, PLR0913, PLR0915
5066
adata: AnnData,
5167
resolution: float | None = None,
@@ -80,22 +96,17 @@ def louvain( # noqa: PLR0912, PLR0913, PLR0915
8096
8197
Parameters
8298
----------
83-
adata
84-
The annotated data matrix.
99+
{doc_adata}
85100
resolution
86101
For the default flavor (``'vtraag'``) or for ```RAPIDS```, you can provide a
87102
resolution (higher resolution means finding more and smaller clusters),
88103
which defaults to 1.0.
89104
See “Time as a resolution parameter” in :cite:t:`Lambiotte2014`.
90-
random_state
91-
Change the initialization of the optimization.
92-
restrict_to
93-
Restrict the clustering to the categories within the key for sample
94-
annotation, tuple needs to contain ``(obs_key, list_of_categories)``.
105+
{random_state}
106+
{restrict_to}
95107
key_added
96108
Key under which to add the cluster labels. (default: ``'louvain'``)
97-
adjacency
98-
Sparse adjacency matrix of the graph, defaults to neighbors connectivities.
109+
{adjacency}
99110
flavor
100111
Choose between to packages for computing the clustering.
101112
@@ -118,15 +129,8 @@ def louvain( # noqa: PLR0912, PLR0913, PLR0915
118129
partition_kwargs
119130
Key word arguments to pass to partitioning,
120131
if ``vtraag`` method is being used.
121-
neighbors_key
122-
Use neighbors connectivities as adjacency.
123-
If not specified, louvain looks .obsp['connectivities'] for connectivities
124-
(default storage place for pp.neighbors).
125-
If specified, louvain looks
126-
.obsp[.uns[neighbors_key]['connectivities_key']] for connectivities.
127-
obsp
128-
Use .obsp[obsp] as adjacency. You can't specify both
129-
`obsp` and `neighbors_key` at the same time.
132+
{neighbors_key}
133+
{obsp}
130134
copy
131135
Copy adata or modify it inplace.
132136

0 commit comments

Comments
 (0)