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

Remove inplace modification in signal.clean #2125

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion nilearn/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,10 @@ def clean(signals, sessions=None, detrend=True, standardize='zscore',
signals = as_ndarray(signals)

if ensure_finite:
signals[np.logical_not(np.isfinite(signals))] = 0
mask = np.logical_not(np.isfinite(signals))
if mask.any():
signals = signals.copy()
signals[mask] = 0

# Read confounds
if confounds is not None:
Expand Down
25 changes: 25 additions & 0 deletions nilearn/tests/test_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,31 @@ def test_clean_frequencies():
assert_true(np.sum(Pxx_den_high[f <= high_pass / 2.]) <= 1e-4)


def test_clean_finite_no_inplace_mod():
"""
Test for verifying that the passed in signal array is not modified.
For PR #2125 . This test is failing on master, passing in this PR.
"""
rng = np.random.RandomState(0)
n_samples = 2
# n_features Must be higher than 500
n_features = 501
x_orig, _, _ = generate_signals(n_features=n_features,
length=n_samples)
x_orig_inital_copy = x_orig.copy()

x_orig_with_nans = x_orig.copy()
x_orig_with_nans[0, 0] = np.nan
x_orig_with_nans_initial_copy = x_orig_with_nans.copy()

cleaned_x_orig = clean(x_orig)
assert np.array_equal(x_orig, x_orig_inital_copy)

cleaned_x_orig_with_nans = clean(x_orig_with_nans, ensure_finite=True)
assert np.isnan(x_orig_with_nans_initial_copy[0, 0])
assert np.isnan(x_orig_with_nans[0, 0])


def test_high_variance_confounds():

# C and F order might take different paths in the function. Check that the
Expand Down