Coverage for fundamentals/stats/rolling_window_sigma_clip.py : 71%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# encoding: utf-8
3"""
4*given a sorted list of values, median sigma-clip values based on a window of values either side of each value (rolling window) and return the array mask*
6:Author:
7 David Young
9:Date Created:
10 January 1, 2021
11"""
12from builtins import object
13import sys
14import os
15os.environ['TERM'] = 'vt100'
16from fundamentals import tools
19def rolling_window_sigma_clip(
20 log,
21 array,
22 clippingSigma,
23 windowSize):
24 """*given a sorted list of values, median sigma-clip values based on a window of values either side of each value (rolling window) and return the array mask*
26 **Key Arguments:**
28 - `log` -- logger
29 - `array` -- the array to clean up (arrays of length < 5 are not clipped but returned unmasked)
30 - `clippingSigma` -- the minimum sigma to clip (using median-absolute distribution as sigma proxy)
31 - `windowSize` -- the size of the window to use when calculating the median distribution (window of 11 will use 5 values each side of the value in question)
33 **Usage:**
35 ```python
36 from fundamentals.stats import rolling_window_sigma_clip
37 arrayMask = rolling_window_sigma_clip(
38 log=self.log,
39 array=myArray,
40 clippingSigma=2.2,
41 windowSize=11)
43 ## JUST KEEP UNMASKED VALUES
44 try:
45 myArray = [e for e, m in zip(
46 myArray, arrayMask) if m == False]
47 except:
48 myArray = []
49 ```
50 """
51 log.debug('starting the ``rolling_window_sigma_clip`` function')
53 from astropy.stats import sigma_clip, mad_std
55 midWindow = int((windowSize + 1) / 2)
56 # ACCOMODATE SMALL LIST SIZES
57 if len(array) < 5:
58 return len(array) * [False]
59 elif len(array) < windowSize:
60 masked = sigma_clip(
61 array, sigma_lower=clippingSigma, sigma_upper=clippingSigma, maxiters=7, cenfunc='median', stdfunc=mad_std)
62 return list(masked.mask)
63 startOfWindow = 0
64 endOfWindow = windowSize
65 maskedArray = []
66 dataIndex = 0
67 while len(array) >= endOfWindow:
68 arrayWindow = array[startOfWindow:endOfWindow]
69 startOfWindow += 1
70 endOfWindow += 1
71 masked = sigma_clip(
72 arrayWindow, sigma_lower=clippingSigma, sigma_upper=clippingSigma, maxiters=7, cenfunc='median', stdfunc=mad_std)
74 if dataIndex == 0:
75 # 0,1,2...midWindow-1
76 maskedArray += list(masked.mask)[0:midWindow]
77 dataIndex += midWindow
78 elif len(array) == endOfWindow - 1:
79 # -midWindow...-2,-1
80 maskedArray += list(masked.mask)[-midWindow:]
81 dataIndex += midWindow
82 else:
83 maskedArray += [list(masked.mask)[midWindow - 1]]
84 dataIndex += 1
86 log.debug('completed the ``rolling_window_sigma_clip`` function')
87 return maskedArray