Wrapper for the scipy.signal.savgol_filter function that handles Nan values.

    See: https://github.com/wheeler-microfluidics/dmf-control-board-firmware/issues/3

    Returns
    -------
    y : ndarray, same shape as `x`
        The filtered data.

def savgol_filter(x, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0):
    '''
    Wrapper for the scipy.signal.savgol_filter function that handles Nan values.

    See: https://github.com/wheeler-microfluidics/dmf-control-board-firmware/issues/3

    Returns
    -------
    y : ndarray, same shape as `x`
        The filtered data.
    '''
    # linearly interpolate missing values before filtering
    x = np.ma.masked_invalid(pd.Series(x).interpolate())

    try:
        # start filtering from the first non-zero value since these won't be addressed by
        # the interpolation above
        ind = np.isfinite(x).nonzero()[0][0]
        x[ind:] = signal.savgol_filter(x[ind:], window_length, polyorder, deriv,
                                       delta, axis, mode, cval)
    except IndexError:
        pass
    return np.ma.masked_invalid(x)