Coverage for nilearn/maskers/tests/test_base_masker.py: 0%

17 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-16 12:32 +0200

1"""Test the base_masker module.""" 

2 

3import numpy as np 

4from nibabel import Nifti1Image 

5from numpy.testing import assert_array_almost_equal 

6 

7from nilearn import image 

8from nilearn.maskers.nifti_masker import filter_and_mask 

9 

10 

11def test_cropping_code_paths(rng): 

12 """Mask data with an identically sampled mask and with a smaller mask. 

13 

14 The results must be identical. 

15 """ 

16 data = np.zeros([20, 30, 40, 5]) 

17 data[10:15, 5:20, 10:30, :] = 1.0 + rng.uniform(size=(5, 15, 20, 5)) 

18 

19 affine = np.eye(4) 

20 

21 img = Nifti1Image(data, affine=affine) 

22 

23 mask = (data[..., 0] > 0).astype("uint8") 

24 mask_img = Nifti1Image(mask, affine=affine) 

25 

26 # the mask in mask_img has the same shape and affine as the 

27 # data and should thus avoid resampling 

28 

29 # we now crop the mask to its non-zero part. Masking with this 

30 # mask must yield the same result 

31 

32 cropped_mask_img = image.crop_img(mask_img, copy_header=True) 

33 

34 parameters = { 

35 "smoothing_fwhm": None, 

36 "high_pass": None, 

37 "low_pass": None, 

38 "t_r": None, 

39 "detrend": False, 

40 "standardize": "zscore", 

41 "standardize_confounds": True, 

42 "clean_kwargs": {}, 

43 } 

44 

45 # Now do the two maskings 

46 out_data_uncropped = filter_and_mask(img, mask_img, parameters) 

47 out_data_cropped = filter_and_mask(img, cropped_mask_img, parameters) 

48 

49 assert_array_almost_equal(out_data_cropped, out_data_uncropped)