def __nn_filter_helper(R_data, R_indices, R_ptr, S, aggregate):
s_out = np.empty_like(S)<EOL>for i in range(len(R_ptr)-<NUM_LIT:1>):<EOL><INDENT>targets = R_indices[R_ptr[i]:R_ptr[i+<NUM_LIT:1>]]<EOL>if not len(targets):<EOL><INDENT>s_out[i] = S[i]<EOL>continue<EOL><DEDENT>neighbors = np.take(S, targets, axis=<NUM_LIT:0>)<EOL>if aggregate is np.average:<EOL><INDENT>weights = R_data[R_ptr[i]:R_ptr[i+<NUM_LIT:1>]]<EOL>s_out[i] = aggregate(neighbors, axis=<NUM_LIT:0>, weights=weights)<EOL><DEDENT>else:<EOL><INDENT>s_out[i] = aggregate(neighbors, axis=<NUM_LIT:0>)<EOL><DEDENT><DEDENT>return s_out<EOL>
Nearest-neighbor filter helper function.

    This is an internal function, not for use outside of the decompose module.

    It applies the nearest-neighbor filter to S, assuming that the first index
    corresponds to observations.

    Parameters
    ----------
    R_data, R_indices, R_ptr : np.ndarrays
        The `data`, `indices`, and `indptr` of a scipy.sparse matrix

    S : np.ndarray
        The observation data to filter

    aggregate : callable
        The aggregation operator


    Returns
    -------
    S_out : np.ndarray like S
        The filtered data array