mantispy.pp.outliers#
- mantispy.pp.outliers(adata, method='ecod', contamination=0.01, score_cutoff=None, key='selected', by=None, seed=0, ecod_aggregation='pyod', *, n_fit=None, key_added='qc_outlier', copy=False)[source]#
Flag outlying cells.
Every method produces a score where higher means more outlying, and the same thresholding applies to all of them, so
contaminationis the flagged fraction whichever method is used.- Parameters:
- adata
AnnData Object to flag.
- method
str(default:'ecod') "ecod"[Li et al., 2023] needs no tuning and is interpretable per feature,"isolation_forest"catches outliers defined by feature interactions,"lof"is sklearn’s local outlier factor with its default 20 neighbours, which scores a cell against the density of its neighbourhood, and"mad"takes the largest robust z-score across features, which is easy to explain but sees each feature alone."ecod"and"lof"fill a missing value with the mean of the feature’s finite values, and"lof"reads an infinite value as missing; a cell with no finite selected feature is left unscored and unflagged by"lof".- contamination
float(default:0.01) Fraction of cells to flag, rounded up to a whole cell within each
bygroup, so a non-empty group always flags its most outlying cell and the flagged fraction is higher than asked for in a group smaller than1 / contamination. Ignored whenscore_cutoffis given.- score_cutoff
float|None(default:None) Threshold the score absolutely instead of by quantile. With
method="mad"the score is a robust z-score, soscore_cutoff=5gives the usual rule.- key
str|None(default:'selected') Input: the name of a boolean
varcolumn; only the features it flags are scored, usually"selected". Brieflow’s intensity filter ismethod="lof"with akeythat flags the mean intensity of each channel. Falls back to every feature when the column is absent. Not the output name, which iskey_added.- by
str|None(default:None) Threshold within each group of this
obscolumn, e.g. per plate, rather than globally.- seed
int(default:0) Seed for
isolation_forestand then_fitsample.- ecod_aggregation
str(default:'pyod') How
"ecod"combines features."pyod"sums each feature’s larger tail, or both tails where its skewness is zero or undefined, as pyod and scmorph do, so no score depends on which way round a feature is measured."paper"is Algorithm 1 of Li et al. [2023], the largest of the left-tail, right-tail and skew-directed sums.- n_fit
int|None(default:None) "lof"fits its density on a random sample of at most this many rows perbygroup and scores every row against that sample, rather than fitting on all of them, so its time and memory grow with the sample rather than the whole screen and stay bounded however large the screen is. A smaller sample sets a coarser scale for “normal” density, measuring each cell against a sparser neighbourhood than a full fit would, so the sampled run flags its most outlying cells by a different standard and can disagree substantially over which cells those are, more so the smaller the sample and the denser the data; it trades that fidelity for a run whose cost no longer rises with the number of cells.None, the default, fits on every row, the exact local outlier factor, which runs on every thread numba is allowed. A group with no more thann_fitrows is fit in full either way.- key_added
str(default:'qc_outlier') Output: the prefix for the results, written to
obs[key_added]andobs[key_added + "_score"].- copy
bool(default:False) Return a modified copy instead of mutating in place.
- adata
- Return type:
- Returns:
None, or the modified copy. Writes the booleanobs[key_added]and the score itself toobs[key_added + "_score"]. The flagged cells are also added toobs["qc_fail"], the union of theqc_*flags thatfilter_cells()drops, which is created if absent; a cell already failing keeps failing.- Raises:
ValueError – If
methodorecod_aggregationis unknown,contaminationis outside(0, 1)and noscore_cutoffis given, orn_fitis below 2.