code: def test_nifti_labels_masker_resampling(tmp_path):
      """Test resampling in NiftiLabelsMasker."""
      n_regions = 9
      length = 3
  
      shape1 = (10, 11, 12, length)
      affine = np.eye(4)
  
      # mask
      shape2 = (16, 17, 18, length)
  
      # labels
      shape3 = (13, 14, 15)
  
      # With data of the same affine
      fmri11_img, _ = data_gen.generate_random_img(
          shape1,
          affine=affine,
      )
      _, mask22_img = data_gen.generate_random_img(
          shape2,
          affine=affine,
      )
  
      labels33_img = data_gen.generate_labeled_regions(
          shape3,
          n_regions,
          affine=affine,
      )
  
      # Test error checking
      with pytest.raises(ValueError):
          NiftiLabelsMasker(labels33_img, resampling_target="mask")
      with pytest.raises(ValueError):
          NiftiLabelsMasker(
              labels33_img,
              resampling_target="invalid",
          )
  
      # Target: labels
      masker = NiftiLabelsMasker(
          labels33_img, mask_img=mask22_img, resampling_target="labels"
      )
  
      masker.fit()
      np.testing.assert_almost_equal(
          masker.labels_img_.affine, labels33_img.affine
      )
      assert masker.labels_img_.shape == labels33_img.shape
  
      np.testing.assert_almost_equal(
          masker.mask_img_.affine, masker.labels_img_.affine
      )
      assert masker.mask_img_.shape == masker.labels_img_.shape[:3]
  
      transformed = masker.transform(fmri11_img)
      assert transformed.shape == (length, n_regions)
  
      fmri11_img_r = masker.inverse_transform(transformed)
      np.testing.assert_almost_equal(
          fmri11_img_r.affine, masker.labels_img_.affine
      )
      assert fmri11_img_r.shape == (
          masker.labels_img_.shape[:3] + (length,)
      )
  
      # Test with clipped labels: mask does not contain all labels.
      # Shapes do matter in that case, because there is some resampling
      # taking place.
      n_regions = 9
      length = 21
  
      shape1 = (10, 11, 12, length)  # fmri
      shape2 = (8, 9, 10, length)  # mask
      shape3 = (16, 18, 20)  # maps
  
      fmri11_img, _ = data_gen.generate_random_img(
          shape1,
          affine=affine,
      )
      _, mask22_img = data_gen.generate_random_img(
          shape2,
          affine=affine,
      )
  
      # Target: labels
      labels33_img = data_gen.generate_labeled_regions(
          shape3, n_regions, affine=affine
      )
  
      masker = NiftiLabelsMasker(
          labels33_img, mask_img=mask22_img, resampling_target="labels"
      )
  
      masker.fit()
      np.testing.assert_almost_equal(
          masker.labels_img_.affine, labels33_img.affine
      )
      assert masker.labels_img_.shape == labels33_img.shape
  
      np.testing.assert_almost_equal(
          masker.mask_img_.affine, masker.labels_img_.affine
      )
      assert masker.mask_img_.shape == masker.labels_img_.shape[:3]
  
      uniq_labels = np.unique(get_data(masker.labels_img_))
      assert uniq_labels[0] == 0
      assert len(uniq_labels) - 1 == n_regions
  
      transformed = masker.transform(fmri11_img)
      assert transformed.shape == (length, n_regions)
      # Some regions have been clipped. Resulting signal must be zero
      assert (transformed.var(axis=0) == 0).sum() < n_regions
  
      fmri11_img_r = masker.inverse_transform(transformed)
      np.testing.assert_almost_equal(
          fmri11_img_r.affine, masker.labels_img_.affine
      )
      assert fmri11_img_r.shape == (
          masker.labels_img_.shape[:3] + (length,)
      )
  
      # 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 = NiftiLabelsMasker(labels33_img, mask_img=mask22_img)
  
      masker.fit_transform(fmri22_img)
      np.testing.assert_array_equal(
          masker._resampled_labels_img_.affine, affine2
      )
  
      # Test with filenames
      filename = testing.write_imgs_to_path(
          fmri22_img, file_path=tmp_path
      )
      masker = NiftiLabelsMasker(labels33_img, resampling_target="data")
      masker.fit_transform(filename)
  
      # test labels masker with resampling target in 'data', 'labels' to return
      # resampled labels having number of labels equal with transformed shape of
      # 2nd dimension. This tests are added based on issue #1673 in Nilearn
      shape = (13, 11, 12, 21)
      affine = np.eye(4) * 2
  
      fmri_img, _ = data_gen.generate_random_img(shape, affine=affine)
      labels_img = data_gen.generate_labeled_regions(
          (9, 8, 6), affine=np.eye(4), n_regions=10
      )
      for resampling_target in ["data", "labels"]:
          masker = NiftiLabelsMasker(
              labels_img=labels_img, resampling_target=resampling_target
          )
          if resampling_target == "data":
              with pytest.warns(
                  UserWarning,
                  match="After resampling the label image "
                  "to the data image, the following "
                  "labels were removed",
              ):
                  transformed = masker.fit_transform(fmri_img)
          else:
              transformed = masker.fit_transform(fmri_img)
          resampled_labels_img = masker._resampled_labels_img_
          n_resampled_labels = len(
              np.unique(get_data(resampled_labels_img))
          )
          assert n_resampled_labels - 1 == transformed.shape[1]
          # inverse transform
          compressed_img = masker.inverse_transform(transformed)
  
          # Test that compressing the image a second time should yield an image
          # with the same data as compressed_img.
          transformed2 = masker.fit_transform(fmri_img)
          # inverse transform again
          compressed_img2 = masker.inverse_transform(transformed2)
          np.testing.assert_array_equal(
              get_data(compressed_img), get_data(compressed_img2)
          )
  
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 NiftiLabelsMasker, NiftiMasker
label_desc: Use image node_id 7 for calling a global function with example usage: test_nifti_labels_masker_resampling(tmp_path) without return types