rescale
rescale ¶
rescale(
data,
gate=None,
log=True,
imageId="imageid",
verbose=True,
randomState=0,
gmmComponents=3,
outputDir=None,
layer="raw",
rescaleMethod="minmax",
streamData=False,
sdataTable=None,
)
Rescale every marker to a 0-1 scale centred on its gate.
Raw intensities are not comparable between markers or between images: a CD45 value of 8000 might be strongly positive in one slide and background in the next. Rescaling maps each marker's gate to exactly 0.5, so a single threshold works everywhere.
Two scaling methods:
'minmax'maps values below the gate onto[0, 0.5]and values above onto[0.5, 1], linearly within each half.'sigmoid'applies a logistic curve with the gate at its midpoint, which is smoother but compresses the extremes.
Gates come from one of three places, in order of precedence: the gate
argument, pre-computed gates in uns['gates'], and otherwise a Gaussian Mixture
Model fitted per marker per image. GMM fitting is parallelised across a
ThreadPoolExecutor; gate lookup uses Polars.
Whichever route is taken, the gates end up in uns['gates'] (markers as rows,
images as columns) so a run can be reproduced or hand-corrected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(AnnData | SpatialData | str, required)
|
The cell table. An |
required |
gate
|
str | DataFrame | DataFrame
|
Manual gates. A path to a CSV, or the table itself, with markers in the
first column and one column of gate values per image. A single value
column is applied to every image. Overrides |
None
|
log
|
bool
|
Apply |
True
|
imageId
|
str
|
Column in |
'imageid'
|
verbose
|
bool
|
Print progress messages. |
True
|
randomState
|
int
|
Seed for the GMM, so automatic gates are reproducible. |
0
|
gmmComponents
|
int
|
Number of components in the Gaussian Mixture Model. |
3
|
outputDir
|
str
|
Directory to write the updated object to. With |
None
|
layer
|
str
|
Which matrix to rescale. |
'raw'
|
rescaleMethod
|
str
|
|
'minmax'
|
streamData
|
bool
|
Run out of core against an |
False
|
sdataTable
|
str
|
Which |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
adata |
AnnData | SpatialData | None
|
The updated object with rescaled values in |
Example
# Automatic gates from a GMM.
adata = sp.pp.rescale(adata)
adata.uns["gates"]
# Manual gates from a CSV, one column per image.
adata = sp.pp.rescale(adata, gate="manual_gates.csv")
# Check the result: every marker should straddle 0.5.
sp.pl.distPlot(adata, vline=0.5)
# Out of core.
sp.pp.rescale("big.h5ad", gate="manual_gates.csv", streamData=True)