Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revamp kit.smooth_1D (resolves #376) #746

Merged
merged 2 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions WrightTools/kit/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,50 @@ def share_nans(*arrs) -> tuple:
return tuple([a + nans for a in arrs])


def smooth_1D(arr, n=10) -> np.ndarray:
"""Smooth 1D data by 'running average'.
def smooth_1D(arr, n=10, smooth_type="flat") -> np.ndarray:
"""Smooth 1D data using a window function.

Edge effects will be present.

Parameters
----------
n : int
number of points to average
arr : array_like
Input array, 1D.
n : int (optional)
Window length.
smooth_type : {'flat', 'hanning', 'hamming', 'bartlett', 'blackman'} (optional)
Type of window function to convolve data with.
'flat' window will produce a moving average smoothing.

Returns
-------
array_like
Smoothed 1D array.
"""
for i in range(n, len(arr) - n):
window = arr[i - n : i + n].copy()
arr[i] = window.mean()
return arr

# check array input
if arr.ndim != 1:
raise wt_exceptions.DimensionalityError(1, arr.ndim)
if arr.size < n:
raise wt_exceptions.ValueError("Input array size must be larger than window size.")
if n < 3:
return arr
# construct window array
if smooth_type == "flat":
w = np.ones(n, dtype=arr.dtype)
elif smooth_type == "hanning":
w = np.hanning(n)
elif smooth_type == "hamming":
w = np.hamming(n)
elif smooth_type == "bartlett":
w = np.bartlett(n)
elif smooth_type == "blackman":
w = np.blackman(n)
else:
raise wt_exceptions.ValueError("Given smooth_type not available.")
ksunden marked this conversation as resolved.
Show resolved Hide resolved
# convolve reflected array with window function
out = np.convolve(w / w.sum(), arr, mode="same")
return out


def svd(a, i=None) -> tuple:
Expand Down
34 changes: 34 additions & 0 deletions tests/kit/smooth_1D.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Test kit.smooth_1D."""


# --- import --------------------------------------------------------------------------------------


import numpy as np

import WrightTools as wt


# --- test ----------------------------------------------------------------------------------------


def test_1():
ksunden marked this conversation as resolved.
Show resolved Hide resolved
# create arrays
x = np.linspace(0, 10, 1000)
y = np.sin(x)
r = np.random.rand(1000) - .5
ksunden marked this conversation as resolved.
Show resolved Hide resolved
yr = y + r
# iterate through window types
windows = ["flat", "hanning", "hamming", "bartlett", "blackman"]
for w in windows:
out = wt.kit.smooth_1D(yr, n=101, smooth_type=w)
check_arr = out - y
check_arr = check_arr[50:-50] # get rid of edge effects
assert np.allclose(check_arr, 0, rtol=.2, atol=.2)


# --- run -----------------------------------------------------------------------------------------


if __name__ == "__main__":
test_1()