Skip to content

Commit

Permalink
FIX failing load_confound test related to standardisation (#4411)
Browse files Browse the repository at this point in the history
* ADD linear trend to the simulated image

* add latest entry
  • Loading branch information
htwangtw committed May 15, 2024
1 parent 7a68dab commit 9745e42
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions doc/changes/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ NEW
Fixes
-----

- :bdg-dark:`Code` Fix failing test in ``test_nilearn_standardize`` on MacOS 14 by adding trend in simulated data (:gh:`4411` by `Hao-Ting Wang`_).

Enhancements
------------

Expand Down
14 changes: 10 additions & 4 deletions nilearn/interfaces/fmriprep/tests/test_load_confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
get_legal_confound,
)
from nilearn.maskers import NiftiMasker
from nilearn.tests.test_signal import generate_trends


def _simu_img(tmp_path, demean):
def _simu_img(tmp_path, trend, demean):
"""Simulate an nifti image based on confound file \
with some parts confounds and some parts noise."""
file_nii, _ = create_tmp_filepath(tmp_path, copy_confounds=True)
Expand Down Expand Up @@ -68,6 +69,11 @@ def _simu_img(tmp_path, demean):
# Shift the mean to non-zero
vol = vol + 10

# add a linear trend to the data
if trend:
signal_trend = generate_trends(n_features=nx * ny * 2 * nz, length=nt)
vol += signal_trend.reshape(nx, ny, 2 * nz, nt)

# create an nifti image with the data, and corresponding mask
img = Nifti1Image(vol, np.eye(4))
mask_conf = Nifti1Image(vol_conf, np.eye(4))
Expand Down Expand Up @@ -106,7 +112,7 @@ def _handle_non_steady(confounds):
def _regression(confounds, tmp_path):
"""Perform simple regression with NiftiMasker."""
# Simulate data
img, mask_conf, _, _, _ = _simu_img(tmp_path, demean=False)
img, mask_conf, _, _, _ = _simu_img(tmp_path, trend=False, demean=False)
confounds = _handle_non_steady(confounds)
# Do the regression
masker = NiftiMasker(mask_img=mask_conf, standardize=True)
Expand Down Expand Up @@ -206,7 +212,7 @@ def test_nilearn_standardize_false(tmp_path):

# Simulate data; set demean to False as standardize_confounds=True
(img, mask_conf, mask_rand, confounds, sample_mask) = _simu_img(
tmp_path, demean=False
tmp_path, trend=False, demean=False
)

# Check that most variance is removed
Expand Down Expand Up @@ -248,7 +254,7 @@ def test_nilearn_standardize(
"""Test confounds removal with logical parameters for processing signal."""
# demean is set to False to let signal.clean handle everything
(img, mask_conf, mask_rand, confounds, mask) = _simu_img(
tmp_path, demean=False
tmp_path, trend=True, demean=False
)
# We now load the time series with vs without confounds
# in voxels composed of pure confounds
Expand Down

0 comments on commit 9745e42

Please sign in to comment.