Skip to content

sc.pp.scale changes adata.raw.X #3756

@jpagolia

Description

@jpagolia

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

Details
scanpy: 1.11.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions