scverse / scverse/scanpy

sc.pp.scale changes adata.raw.X

Open
#3,756 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs info❔
Dominant language
Python
Stars
2.6k
Forks
779
Avg merge
1d 4h
Merged PRs (30d)
27

Description

Please make sure these conditions are met
  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of scanpy.
  • (optional) I have confirmed this bug exists on the main branch of scanpy.
What happened?

Following this common workflow:

adata.layers["counts"] = adata.X.copy()
sc.pp.normalize_total(adata)
sc.pp.log1p(adata)
adata.layers['lognorm'] = adata.X.copy()
adata.raw = adata # full dimension lognormalized data
sc.pp.scale(adata, max_value=10)
adata

If you check adata.X, adata.layers['counts'], and adata.layers['lognorm'], and adata.raw.X, you will find that adata.X and adata.raw.X are the same. The desired behavior would probably be for adata.raw.X to be the same as adata.layers['lognorm']. It appears that sc.pp.scale is changing adata.raw. Why is that?

Minimal code sample
adata.layers["counts"] = adata.X.copy()
sc.pp.normalize_total(adata)
sc.pp.log1p(adata)
adata.raw = adata
sc.pp.scale(adata, max_value=10)
adata
adata.layers['lognorm']
array([[0.       , 0.       , 0.       , ..., 0.       , 0.       ,
        0.       ],
       [0.       , 0.       , 1.4028237, ..., 0.       , 0.       ,
        0.       ],
       [0.       , 0.       , 0.       , ..., 0.       , 0.       ,
        0.       ],
       ...,
       [0.       , 0.       , 0.       , ..., 0.       , 0.       ,
        0.       ],
       [0.       , 0.       , 0.       , ..., 0.       , 0.       ,
        0.       ],
       [0.       , 0.       , 0.       , ..., 0.       , 0.       ,
        0.       ]], shape=(70499, 309), dtype=float32)
adata.X
array([[-0.2976397 , -0.35878736, -0.2131979 , ..., -0.14714538,
        -0.32566202, -0.3301082 ],
       [-0.2976397 , -0.35878736,  8.037066  , ..., -0.14714538,
        -0.32566202, -0.3301082 ],
       [-0.2976397 , -0.35878736, -0.2131979 , ..., -0.14714538,
        -0.32566202, -0.3301082 ],
       ...,
       [-0.2976397 , -0.35878736, -0.2131979 , ..., -0.14714538,
        -0.32566202, -0.3301082 ],
       [-0.2976397 , -0.35878736, -0.2131979 , ..., -0.14714538,
        -0.32566202, -0.3301082 ],
       [-0.2976397 , -0.35878736, -0.2131979 , ..., -0.14714538,
        -0.32566202, -0.3301082 ]], shape=(70499, 309), dtype=float32)
adata.raw.X
array([[-0.2976397 , -0.35878736, -0.2131979 , ..., -0.14714538,
        -0.32566202, -0.3301082 ],
       [-0.2976397 , -0.35878736,  8.037066  , ..., -0.14714538,
        -0.32566202, -0.3301082 ],
       [-0.2976397 , -0.35878736, -0.2131979 , ..., -0.14714538,
        -0.32566202, -0.3301082 ],
       ...,
       [-0.2976397 , -0.35878736, -0.2131979 , ..., -0.14714538,
        -0.32566202, -0.3301082 ],
       [-0.2976397 , -0.35878736, -0.2131979 , ..., -0.14714538,
        -0.32566202, -0.3301082 ],
       [-0.2976397 , -0.35878736, -0.2131979 , ..., -0.14714538,
        -0.32566202, -0.3301082 ]], shape=(70499, 309), dtype=float32)
Versions
scanpy: 1.11.4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the sc.pp.scale entry point and how adata.raw and adata.layers reference their data in the minimal reproduction. Add a regression test showing that scaling changes adata.X without changing adata.raw.X, then verify the existing preprocessing tests still pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.