Aluode/PerceptionLabPortable
0
1# Authors: The MNE-Python contributors.2# License: BSD-3-Clause3# Copyright the MNE-Python contributors.4 5import numpy as np6from scipy.stats import zscore7 8 9def _find_outliers(X, threshold=3.0, max_iter=2, tail=0):10 """Find outliers based on iterated Z-scoring.11 12 This procedure compares the absolute z-score against the threshold.13 After excluding local outliers, the comparison is repeated until no14 local outlier is present any more.15 16 Parameters17 ----------18 X : np.ndarray of float, shape (n_elemenets,)19 The scores for which to find outliers.20 threshold : float21 The value above which a feature is classified as outlier.22 max_iter : int23 The maximum number of iterations.24 tail : {0, 1, -1}25 Whether to search for outliers on both extremes of the z-scores (0),26 or on just the positive (1) or negative (-1) side.27 28 Returns29 -------30 bad_idx : np.ndarray of int, shape (n_features)31 The outlier indices.32 """33 my_mask = np.zeros(len(X), dtype=bool)34 for _ in range(max_iter):35 X = np.ma.masked_array(X, my_mask)36 if tail == 0:37 this_z = np.abs(zscore(X))38 elif tail == 1:39 this_z = zscore(X)40 elif tail == -1:41 this_z = -zscore(X)42 else:43 raise ValueError(f"Tail parameter {tail} not recognised.")44 local_bad = this_z > threshold45 my_mask = np.max([my_mask, local_bad], 0)46 if not np.any(local_bad):47 break48 49 bad_idx = np.where(my_mask)[0]50 return bad_idx51 