scanpy icon indicating copy to clipboard operation
scanpy copied to clipboard

Leiden and louvain params not saved to matching `key_added` key in `uns` dictionary

Open theJasonFan opened this issue 1 year ago • 0 comments

Please make sure these conditions are met

  • [X] I have checked that this issue has not already been reported.
  • [X] I have confirmed this bug exists on the latest version of scanpy.
  • [X] (optional) I have confirmed this bug exists on the main branch of scanpy.

What happened?

Leiden and Louvain clustering params are not saved to matching key_added key in uns dictionary but are ovewritten to hardcoded key instead. One use case is that a user may want to run Leiden/Louvain clustering multiple times with different resolutions / parameters. One may specify different keys to store results under. However, if you do so, the metadata for the parameterization of the clustering algorithms are overwritten because the lines below do not respect the user provided key_added parameter.

I think the desired behavior is to store data under adata.uns[key_added]["params"]

I think I've found the pertinent lines below. Happy to submit a PR if maintainers agree :D.

  • https://github.com/scverse/scanpy/blob/91ea0fbb03392795d1506d297d4b4847c646db04/scanpy/tools/_leiden.py#L206
  • https://github.com/scverse/scanpy/blob/91ea0fbb03392795d1506d297d4b4847c646db04/scanpy/tools/_louvain.py#L259

Minimal code sample

sc.tl.leiden(adata, resolution=0.8, key_added="leiden_0.8")
assert "leiden_0.8" not in adata.uns
params = adata.uns["leiden"] 

sc.tl.leiden(adata, resolution=1.2, key_added="leiden_1.2")
assert "leiden_1.2" not in adata.uns
overwritten_params = adata.uns["leiden"] 
assert params == overwritten_params # should fail

Error output

No response

Versions

Confirmed that params are overwritten in source in main branch. (see permalinks)

theJasonFan avatar Feb 23 '24 16:02 theJasonFan