code: def test_apply_mask(affine_eye, tmp_path):
      """Test smoothing of timeseries extraction."""
      # A delta in 3D
      # Standard masking
      data = np.zeros((40, 40, 40, 2))
      data[20, 20, 20] = 1
      mask = np.ones((40, 40, 40))
      full_mask = np.zeros((40, 40, 40))
      for create_files in (False, True):
          for affine in (
              affine_eye,
              np.diag((1, 1, -1, 1)),
              np.diag((0.5, 1, 0.5, 1)),
          ):
              data_img = Nifti1Image(data, affine)
              mask_img = Nifti1Image(mask, affine)
              filenames = write_imgs_to_path(
                  data_img,
                  mask_img,
                  file_path=tmp_path,
                  create_files=create_files,
              )
              series = masking.apply_mask(
                  filenames[0], filenames[1], smoothing_fwhm=9
              )
  
              series = np.reshape(series[0, :], (40, 40, 40))
              vmax = series.max()
              # We are expecting a full-width at half maximum of
              # 9mm/voxel_size:
              above_half_max = series > 0.5 * vmax
              for axis in (0, 1, 2):
                  proj = np.any(
                      np.any(
                          np.rollaxis(above_half_max, axis=axis),
                          axis=-1,
                      ),
                      axis=-1,
                  )
                  np.testing.assert_equal(
                      proj.sum(), 9 / np.abs(affine[axis, axis])
                  )
  
      # Check that NaNs in the data do not propagate
      data[10, 10, 10] = np.nan
      data_img = Nifti1Image(data, affine)
      mask_img = Nifti1Image(mask, affine)
      full_mask_img = Nifti1Image(full_mask, affine)
      series = masking.apply_mask(data_img, mask_img, smoothing_fwhm=9)
      assert np.all(np.isfinite(series))
  
      # veriy that 4D masks are rejected
      mask_img_4d = Nifti1Image(np.ones((40, 40, 40, 2)), affine_eye)
      with pytest.raises(
          DimensionError, match=_TEST_DIM_ERROR_MSG % "4D"
      ):
          masking.apply_mask(data_img, mask_img_4d)
  
      # Check that 3D data is accepted
      data_3d = Nifti1Image(
          np.arange(27, dtype="int32").reshape((3, 3, 3)), affine_eye
      )
      mask_data_3d = np.zeros((3, 3, 3))
      mask_data_3d[1, 1, 0] = True
      mask_data_3d[0, 1, 0] = True
      mask_data_3d[0, 1, 1] = True
      data_3d = masking.apply_mask(
          data_3d, Nifti1Image(mask_data_3d, affine_eye)
      )
      assert sorted(data_3d.tolist()) == [3.0, 4.0, 12.0]
  
      # Check data shape and affine
      with pytest.raises(
          DimensionError, match=_TEST_DIM_ERROR_MSG % "2D"
      ):
          masking.apply_mask(
              data_img, Nifti1Image(mask[20, ...], affine)
          )
      with pytest.raises(ValueError):
          masking.apply_mask(data_img, Nifti1Image(mask, affine / 2.0))
      # Check that full masking raises error
      with pytest.raises(ValueError):
          masking.apply_mask(data_img, full_mask_img)
      # Check weird values in data
      mask[10, 10, 10] = 2
      with pytest.raises(ValueError):
          masking.apply_mask(data_img, Nifti1Image(mask, affine))
      mask[15, 15, 15] = 3
      with pytest.raises(ValueError):
          masking.apply_mask(Nifti1Image(data, affine), mask_img)
  
imports: import warnings
  import numpy
  import pytest
  from nibabel import Nifti1Image
  from numpy.testing import assert_array_equal
  from sklearn.preprocessing import StandardScaler
  from nilearn import masking
  from nilearn._utils import data_gen
  from nilearn._utils.exceptions import DimensionError
  from nilearn._utils.testing import write_imgs_to_path
  from nilearn.conftest import _rng
  from nilearn.image import get_data, high_variance_confounds
  from nilearn.maskers import NiftiMasker
  from nilearn.masking import _MaskWarning, _unmask_3d, _unmask_4d, compute_background_mask, compute_brain_mask, compute_epi_mask, compute_multi_brain_mask, compute_multi_epi_mask, extrapolate_out_mask, intersect_masks, unmask, unmask_from_to_3d_array
label_desc: Use image node_id 9 for calling a global function with example usage: test_apply_mask(affine_eye, tmp_path) without return types