mantispy.pp.feature_select#
- mantispy.pp.feature_select(adata, operations=('drop_degenerate', 'blocklist', 'drop_na_columns', 'variance_threshold', 'correlation_threshold'), min_variance=1e-06, freq_cut=0.05, unique_cut=0.01, corr_threshold=0.9, corr_method='pearson', corr_window=None, corr_stride=None, na_cutoff=0.05, outlier_cutoff=500.0, blocklist='default', noise_removal_perturb_groups='Metadata_Perturbation', noise_removal_stdev_cutoff=0.8, corr_absolute=False, corr_iterative=False, decorrelate=False, decorr_threshold=0.99, decorr_method='pearson', key_added='selected', copy=False, *, noise_removal_cutoff_mode='absolute', subset=False)[source]#
Flag the features worth keeping.
- Parameters:
- adata
AnnData Object to select features on. Usually well-level profiles.
- operations
Sequence[str] (default:('drop_degenerate', 'blocklist', 'drop_na_columns', 'variance_threshold', 'correlation_threshold')) Which operations to run, from
OPERATIONS, in the order they run. The default,DEFAULT_OPERATIONS, holds pycytominer’s usual operations plusdrop_degenerate, which removes nothing from an objectnormalize()did not flag, ordered cheapest first socorrelation_thresholdruns last on what is left. Passing pycytominer’s order,["variance_threshold", "correlation_threshold", "drop_na_columns", "blocklist"], gives pycytominer’s selection. Withoutdrop_outliers, a warning names the kept features whose values sit more thanoutlier_cutoffrobust standard deviations from their own median.- min_variance
float(default:1e-06) variance_threshold: keep features with variance above this.- freq_cut
float(default:0.05) frequency_threshold: drop a feature when the count of its second most common value divided by the count of its most common is below this. Either this rule orunique_cutdrops a feature.- unique_cut
float(default:0.01) frequency_threshold: drop a feature when its share of distinct values is below this.- corr_threshold
float(default:0.9) correlation_threshold: drop one member of every pair correlated above this.- corr_method
str(default:'pearson') correlation_threshold:"pearson"or"spearman".- corr_window
int|None(default:None) correlation_threshold:Noneruns the exact pass (the default, matching pycytominer). An int switches to the two-pass fast path (prune redundancy within name-sorted windows of that size, then run the exact pass on the survivors); 500 is a good default, several times faster on large screens. It keeps a different set of features (a different member of each correlated group) but preserves the information and the downstream signal.- corr_stride
int|None(default:None) correlation_threshold: step between windows, default half the window.- corr_absolute
bool(default:False) correlation_threshold: threshold|r|rather than the signed correlation, so a strongly anti-correlated pair is also reduced. Off by default, matching pycytominer.- corr_iterative
bool(default:False) correlation_threshold: drop the most-connected feature one at a time until no pair is left, keeping at least as many features as the single pass. Off by default. Withcorr_absolutethis approximates cytominer’s R path (caret::findCorrelation).- decorrelate
bool(default:False) Run an extra, experimental redundancy step after the operations, on the features they keep. Unlike
correlation_thresholdit removes features that are a linear combination of several others, not just pairwise duplicates, by a rank-revealing QR. Off by default, and not part of pycytominer.- decorr_threshold
float(default:0.99) decorrelate: drop a feature once its multiple correlation with the kept set reaches this. The default 0.99 removes only near-collinear features, so the kept set spans almost the same space; lower it towardscorr_thresholdfor a smaller, more aggressive set.- decorr_method
str(default:'pearson') decorrelate:"pearson"or"spearman".- na_cutoff
float(default:0.05) drop_na_columns: drop features missing in more than this fraction of rows.- outlier_cutoff
float(default:500.0) drop_outliers: drop features whose absolute value exceeds this.- blocklist
str|Sequence[str] (default:'default') blocklist:"default"for the bundled list, or explicit names. Matched against the current names and againstvar["original_name"], so it works either side ofstandardize_feature_names(). A name without an object prefix, as cp_measure writes them, is matched on its parsed group, feature, channels and scale, so the default list also applies to cp_measure features.- noise_removal_perturb_groups
str(default:'Metadata_Perturbation') noise_removal:obscolumn grouping replicates.- noise_removal_stdev_cutoff
float(default:0.8) noise_removal: drop features whose within-group standard deviation, averaged over groups, is above this. An absolute threshold on the scalenormalize()left the values on, so it is only meaningful next to the normalization that produced them; pycytominer’s default assumes whole-plate standardization.- key_added
str(default:'selected') Name of the boolean
varcolumn to write.- copy
bool(default:False) Return a modified copy instead of mutating in place.
- noise_removal_cutoff_mode
str(default:'absolute') noise_removal: whatnoise_removal_stdev_cutoffis compared against."absolute"(the default) keeps pycytominer’s behaviour, comparing the raw within-group spread and so tied to the normalization scale."ratio"compares the within-group spread to the feature’s total spread, and"quantile"reads the cutoff as a quantile of the observed within-group spreads; both keep a useful subset under the control-based or MAD normalization the package recommends, where the absolute cutoff keeps everything or nothing.- subset
bool(default:False) Inplace subset to the selected features, as well as flagging them in
var[key_added]. Withcopy=Truethe returned copy is the subset.
- adata
- Return type:
- Returns:
None, or the modified copy. Writesvar[key_added]and a per-operation count of removals touns["mantispy"]["feature_select"], each count being what that operation removed from the features the earlier ones kept. Nothing is dropped unlesssubset=True;subset_features()does it afterwards.- Raises:
ValueError – If
operationsnames an operation that is not inOPERATIONS.KeyError – If
noise_removalis requested butnoise_removal_perturb_groupsis not anobscolumn.
Notes
drop_degenerateruns first: a featurenormalize()could not scale can hold values large enough to decide the correlation ranking of every feature it is compared with. The other operations then run in the orderoperationslists them, each judging only the features the earlier ones kept, as pycytominer does. The counts inuns["mantispy"]["feature_select"]therefore do not overlap, and they sum to the number of features removed. Order matters only forcorrelation_threshold, which ranks a feature against the other features still present; every other operation judges each feature on its own values. So the default order, which runs the correlation last, can keep a different member of a correlated pair than pycytominer’s order, which runs it beforedrop_na_columnsandblocklist; it is also much faster on data with many mostly-missing features.decorrelateruns last, on the features the operations kept.correlation_thresholdis the most expensive operation. pycytominer usespandas.DataFrame.corr, one Cython pass per column pair. Here the pairs come from chunked matrix products over blocks of columns, so non_vars ** 2array is held in memory.