mantispy.pp.calculate_qc_metrics#

mantispy.pp.calculate_qc_metrics(adata, image_shape=None, border_margin=10, max_nan_fraction=0.5, area_feature=None, qc_metrics=('nan', 'border', 'area_outlier'), copy=False)[source]#

Compute per-cell and per-feature QC metrics.

Parameters:
adata AnnData

Object to annotate.

image_shape tuple[int, int] | None (default: None)

(height, width) of a field of view. Without it, and without Metadata_Center_X/_Y in obs, the border flag stays False.

border_margin int (default: 10)

Distance from the image edge, in pixels, inside which a cell is a border cell.

max_nan_fraction float (default: 0.5)

Largest fraction of missing features a cell may have before it is flagged. Partial NaN is routine in CellProfiler output (Zernike and RadialDistribution features are undefined for small objects), so requiring no missing values would flag almost every cell.

area_feature str | None (default: None)

The var name of the area measurement to score for outliers. By default the columns whose parsed feature is Area are used; pass this for a table whose names do not parse (mt.io.stamp could not fill var["feature"]).

qc_metrics Sequence[str] (default: ('nan', 'border', 'area_outlier'))

Metrics to compute. "nan" writes obs["qc_n_nan_features"], obs["qc_nan_fraction"] and var["qc_n_nan"]; "border" writes obs["qc_is_border"]; "area_outlier" writes obs["qc_area_outlier"]; "variance" and "n_unique" write var["qc_variance"] and var["qc_n_unique"]. The per-feature "variance" and "n_unique" are left out by default: filter_cells() reads neither, and "n_unique" sorts every feature, which dominates the runtime on large single-cell objects.

copy bool (default: False)

Return a modified copy instead of mutating in place.

Return type:

AnnData | None

Returns:

None, or the modified copy. Writes the columns of each metric in qc_metrics, and obs["qc_fail"]. Every qc_* boolean is True for a problem, matching the ecosystem convention; qc_fail is the union of the computed flags and of a missing fraction above max_nan_fraction, and is the flag filter_cells() reads. When no area column is found and area_feature is not given, qc_area_outlier is skipped (left all False) with a warning and the other metrics are still computed.

Raises:
  • KeyError – area_feature is given but is not a var name.

  • ValueError – qc_metrics names an unknown metric.