Skip to content

Instantly share code, notes, and snippets.

@d-v-b
Created November 14, 2015 19:47
Show Gist options
  • Save d-v-b/59312d23e054bbd0c32a to your computer and use it in GitHub Desktop.
Save d-v-b/59312d23e054bbd0c32a to your computer and use it in GitHub Desktop.
dff in thunder
# from thunder/rdds/timeseries.py
# found at https://github.com/thunder-project/thunder/blob/b15ba0a38642312d597a98643cf3514e2d46b69d/thunder/rdds/timeseries.py
def normalize(self, baseline='percentile', window=None, perc=20):
"""
Normalize each time series by subtracting and dividing by a baseline.
Baseline can be derived from a global mean or percentile,
or a smoothed percentile estimated within a rolling window.
Parameters
----------
baseline : str, optional, default = 'percentile'
Quantity to use as the baseline, options are 'mean', 'percentile', 'window', or 'window-fast'
window : int, optional, default = 6
Size of window for baseline estimation, for 'window' and 'window-fast' baseline only
perc : int, optional, default = 20
Percentile value to use, for 'percentile', 'window', or 'window-fast' baseline only
"""
checkParams(baseline, ['mean', 'percentile', 'window', 'window-fast'])
method = baseline.lower()
from warnings import warn
if not (method == 'window' or method == 'window-fast') and window is not None:
warn('Setting window without using method "window" has no effect')
if method == 'mean':
baseFunc = mean
if method == 'percentile':
baseFunc = lambda x: percentile(x, perc)
if method == 'window':
if window & 0x1:
left, right = (ceil(window/2), ceil(window/2) + 1)
else:
left, right = (window/2, window/2)
n = len(self.index)
baseFunc = lambda x: asarray([percentile(x[max(ix-left, 0):min(ix+right+1, n)], perc)
for ix in arange(0, n)])
if method == 'window-fast':
from scipy.ndimage.filters import percentile_filter
baseFunc = lambda x: percentile_filter(x.astype(float64), perc, window, mode='nearest')
def get(y):
b = baseFunc(y)
return (y - b) / (b + 0.1)
return self.applyValues(get, keepIndex=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment