def savgol_filter(array, win=2001, polyn=3, iteration=1, threshold=1):
"""Apply scipy.signal.savgol_filter iteratively.
Args:
array (decode.array): Decode array which will be filtered.
win (int): Length of window.
polyn (int): Order of polynominal function.
iteration (int): The number of iterations.
threshold (float): Threshold above which the data will be used as signals
in units of sigma.
Returns:
fitted (decode.array): Fitted results.
"""
logger = getLogger('decode.models.savgol_filter')
logger.warning('Do not use this function. We recommend you to use dc.models.pca instead.')
logger.info('win polyn iteration threshold')
logger.info('{} {} {} {}'.format(win, polyn, iteration, threshold))
### 1st estimation
array = array.copy()
fitted = signal.savgol_filter(array, win, polyn, axis=0)
filtered = array - fitted
### nth iteration
for i in range(iteration):
sigma = filtered.std(axis=0)
mask = (filtered >= threshold * sigma)
masked = np.ma.array(filtered, mask=mask)
### mask?????????threshold * sigma????????
filled = masked.filled(threshold * sigma)
### fitted data???????????????savgol_filter????
clipped = filled + fitted
fitted = signal.savgol_filter(clipped, win, polyn, axis=0)
filtered = array - fitted
return fitted
评论列表
文章目录