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 its na_cutoff. Cells missing many features can be dropped with max_nan_fraction in calculate_qc_metrics() and filter_cells(). Brieflow does both before imputing the rest with method="knn". Only NaN is 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’s n_neighbors nearest reference rows that measured it, as sklearn’s KNNImputer does, with distances over the features both rows measured. Distances mix every feature, so run it after normalize(), 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: None for all rows, "negcon" for the negative controls, or the name of a boolean obs column, as in normalize(). Unused by "zero".

by str | list[str] | None (default: None)

Column(s) of obs whose 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. None searches every reference row.

seed int (default: 0)

Seed for the n_donors sample.

layer str | None (default: None)

Read this layer instead of X.

key_added str | None (default: None)

Write to layers[key_added] instead of overwriting X.

copy bool (default: False)

Return an imputed copy instead of imputing in place.

Return type:

AnnData | None

Returns:

None, or the imputed copy. Writes X or layers[key_added], and var["n_imputed"], or var["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 by or reference names a column that obs does not have.

  • ValueError – If method is unknown, n_neighbors or n_donors is below 1, or reference selects no rows at all or none in some group.