code: def test_nifti_maps_masker(tmp_path):
      """Check working of shape/affine checks."""
      n_regions = 9
      length = 3
  
      shape1 = (13, 11, 12, length)
      affine1 = np.eye(4)
  
      shape2 = (12, 10, 14, length)
      affine2 = np.diag((1, 2, 3, 1))
  
      fmri11_img, mask11_img = data_gen.generate_random_img(
          shape1,
          affine=affine1,
      )
      fmri12_img, mask12_img = data_gen.generate_random_img(
          shape1,
          affine=affine2,
      )
      fmri21_img, mask21_img = data_gen.generate_random_img(
          shape2,
          affine=affine1,
      )
  
      labels11_img, _ = data_gen.generate_maps(
          shape1[:3], n_regions, affine=affine1
      )
  
      # No exception raised here
      for create_files in (True, False):
          labels11 = testing.write_imgs_to_path(
              labels11_img,
              file_path=tmp_path,
              create_files=create_files,
          )
          masker11 = NiftiMapsMasker(labels11, resampling_target=None)
          signals11 = masker11.fit().transform(fmri11_img)
          assert signals11.shape == (length, n_regions)
          # enables to delete "labels11" on windows
          del masker11
  
      masker11 = NiftiMapsMasker(
          labels11_img, mask_img=mask11_img, resampling_target=None
      )
  
      with pytest.raises(ValueError, match="has not been fitted. "):
          masker11.transform(fmri11_img)
      signals11 = masker11.fit().transform(fmri11_img)
      assert signals11.shape == (length, n_regions)
  
      NiftiMapsMasker(labels11_img).fit_transform(fmri11_img)
  
      # Test all kinds of mismatches between shapes and between affines
      for create_files in (True, False):
          images = testing.write_imgs_to_path(
              labels11_img,
              mask12_img,
              file_path=tmp_path,
              create_files=create_files,
          )
          labels11, mask12 = images
          masker11 = NiftiMapsMasker(labels11, resampling_target=None)
          masker11.fit()
          with pytest.raises(ValueError):
              masker11.transform(fmri12_img)
          with pytest.raises(ValueError):
              masker11.transform(fmri21_img)
  
          masker11 = NiftiMapsMasker(
              labels11, mask_img=mask12, resampling_target=None
          )
          with pytest.raises(ValueError):
              masker11.fit()
          del masker11
  
      masker11 = NiftiMapsMasker(
          labels11_img, mask_img=mask21_img, resampling_target=None
      )
      with pytest.raises(ValueError):
          masker11.fit()
  
      # Transform, with smoothing (smoke test)
      masker11 = NiftiMapsMasker(
          labels11_img, smoothing_fwhm=3, resampling_target=None
      )
      # Check attributes defined at fit
      assert not hasattr(masker11, "maps_img_")
      assert not hasattr(masker11, "n_elements_")
  
      masker11.fit()
  
      # Check attributes defined at fit
      assert hasattr(masker11, "maps_img_")
      assert hasattr(masker11, "n_elements_")
      assert masker11.n_elements_ == n_regions
  
      signals11 = masker11.transform(fmri11_img)
  
      assert signals11.shape == (length, n_regions)
  
      masker11 = NiftiMapsMasker(
          labels11_img, smoothing_fwhm=3, resampling_target=None
      )
      signals11 = masker11.fit_transform(fmri11_img)
      assert signals11.shape == (length, n_regions)
  
      with pytest.raises(ValueError, match="has not been fitted. "):
          NiftiMapsMasker(labels11_img).inverse_transform(signals11)
  
      # Call inverse transform (smoke test)
      fmri11_img_r = masker11.inverse_transform(signals11)
      assert fmri11_img_r.shape == fmri11_img.shape
      np.testing.assert_almost_equal(
          fmri11_img_r.affine, fmri11_img.affine
      )
  
      # Now try on a masker that has never seen the call to "transform"
      masker2 = NiftiMapsMasker(labels11_img, resampling_target=None)
      masker2.fit()
      masker2.inverse_transform(signals11)
  
      # Test with data and atlas of different shape: the atlas should be
      # resampled to the data
      shape22 = (5, 5, 6, length)
      affine2 = 2 * np.eye(4)
      affine2[-1, -1] = 1
  
      fmri22_img, _ = data_gen.generate_random_img(
          shape22,
          affine=affine2,
      )
      masker = NiftiMapsMasker(labels11_img, mask_img=mask21_img)
  
      masker.fit_transform(fmri22_img)
      np.testing.assert_array_equal(
          masker._resampled_maps_img_.affine, affine2
      )
  
file_path: nilearn/nilearn/maskers/tests/test_nifti_maps_masker.py
imports: import warnings
  import nibabel
  import numpy
  import pytest
  from nilearn._utils import data_gen, testing
  from nilearn._utils.exceptions import DimensionError
  from nilearn.image import get_data
  from nilearn.maskers import NiftiMapsMasker
label_desc: Use image node_id 1 for calling a global function with example usage: test_nifti_maps_masker(tmp_path) without return types