mantispy.pp.impute#
- mantispy.pp.impute(adata, method='knn', reference=None, by=None, n_neighbors=5, n_donors=10000, seed=0, layer=None, key_added=None, copy=False)[source]#
Fill missing feature values, so that tools which refuse them, such as
map(), can run.Features that are missing in many rows carry little information and are better dropped first, with
feature_select()and itsna_cutoff. Cells missing many features can be dropped withmax_nan_fractionincalculate_qc_metrics()andfilter_cells(). Brieflow does both before imputing the rest withmethod="knn". OnlyNaNis filled; infinite values are left as they are.- Parameters:
- adata
AnnData Object to impute.
- method
str(default:'knn') "knn"fills each missing value with the mean of that feature over the row’sn_neighborsnearest reference rows that measured it, as sklearn’sKNNImputerdoes, with distances over the features both rows measured. Distances mix every feature, so run it afternormalize(), which puts the features on one scale."reference_median"fills with the feature’s median over the reference rows of the row’s group."zero"fills with zero, which after a normalization centred on the controls is their median.- reference
str|None(default:None) Rows that donate values:
Nonefor all rows,"negcon"for the negative controls, or the name of a booleanobscolumn, as innormalize(). Unused by"zero".- by
str|list[str] |None(default:None) Column(s) of
obswhose groups are imputed separately, so a row takes values only from reference rows of its own group, such as its plate. Unused by"zero".- n_neighbors
int(default:5) Reference rows averaged per missing value by
"knn".- n_donors
int|None(default:10000) "knn"searches a random sample of at most this many reference rows per group, rather than all of them, so the cost per imputed row is bounded.Nonesearches every reference row.- seed
int(default:0) Seed for the
n_donorssample.- layer
str|None(default:None) Read this layer instead of
X.- key_added
str|None(default:None) Write to
layers[key_added]instead of overwritingX.- copy
bool(default:False) Return an imputed copy instead of imputing in place.
- adata
- Return type:
- Returns:
None, or the imputed copy. WritesXorlayers[key_added], andvar["n_imputed"], orvar["n_imputed_<key_added>"]when writing to a layer, with the number of values filled in each feature. A value is left missing, with a warning, when no reference row of its group measured that feature.- Raises:
KeyError – If
byorreferencenames a column thatobsdoes not have.ValueError – If
methodis unknown,n_neighborsorn_donorsis below 1, orreferenceselects no rows at all or none in some group.