-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from ErlendHaa/rolling-median
Add rolling median filter processor
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pandas as pd | ||
import numpy as np | ||
|
||
def process(signal, wsize, tolerance=None): | ||
""" Process rolling median | ||
Rolling window calculations | ||
Parameters | ||
---------- | ||
signal : pd.Series | ||
wsize : int or pd.Timedelta | ||
size of the rolling window | ||
tolerance : float, optional | ||
threshold for outliers, default to the standard deviation of signal | ||
Returns | ||
------- | ||
pd.Series | ||
filtered signal | ||
Examples | ||
-------- | ||
>>> s = pd.Series(signal, index=t) | ||
>>> processed = process.rolling_median(s, wsize=20, tolerance=2.0) | ||
""" | ||
|
||
dt = signal.index[1] - signal.index[0] | ||
if isinstance(wsize, pd.Timedelta): | ||
if wsize < dt: | ||
problem = "wsize is smaller than the samplerate of signal: {} < {}".format(wsize, dt) | ||
raise ValueError(problem) | ||
|
||
wsize = int(np.ceil(wsize / dt)) | ||
|
||
if tolerance == None: tolerance = 1.0 * signal.std() | ||
|
||
rolling = signal.rolling(window=wsize, min_periods=1, center=True).median() | ||
outliers = signal[(signal - rolling).abs() >= tolerance] | ||
cleaned = signal.drop(outliers.index).iloc[wsize:-wsize] | ||
return cleaned |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env python | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from camille import process | ||
|
||
def test_process(): | ||
index = pd.date_range('1/1/2018', periods=100, freq='S') | ||
|
||
t = np.linspace(-np.pi, np.pi, num=100) | ||
signal = np.sin(t) | ||
signal[40] *= 5 #create an outlier | ||
|
||
s = pd.Series(signal, index=index) | ||
dt = pd.Timedelta(seconds=5) | ||
|
||
processed = process.rolling_median(s, wsize=dt) | ||
|
||
expected = s.drop(s.index[40]).iloc[5:-5] | ||
pd.testing.assert_series_equal(expected, processed) |