code: def test_dict_learning(mask_img):
      data, components, _ = _make_canica_test_data()
  
      masker = NiftiMasker(mask_img=mask_img).fit()
      mask = get_data(mask_img) != 0
      flat_mask = mask.ravel()
      masked_components = components[:, flat_mask]
      dict_init = masker.inverse_transform(masked_components)
  
      dict_learning = DictLearning(
          n_components=4,
          random_state=0,
          dict_init=dict_init,
          mask=mask_img,
          smoothing_fwhm=0.0,
          alpha=1,
      )
  
      dict_learning_auto_init = DictLearning(
          n_components=4,
          random_state=0,
          mask=mask_img,
          smoothing_fwhm=0.0,
          n_epochs=10,
          alpha=1,
      )
      maps = {}
      for estimator in [dict_learning, dict_learning_auto_init]:
          estimator.fit(data)
          maps[estimator] = get_data(estimator.components_img_)
          maps[estimator] = np.reshape(
              np.rollaxis(maps[estimator], 3, 0)[:, mask],
              (4, flat_mask.sum()),
          )
  
      for this_dict_learning in [dict_learning]:
          these_maps = maps[this_dict_learning]
          S = np.sqrt(np.sum(masked_components**2, axis=1))
          S[S == 0] = 1
          masked_components /= S[:, np.newaxis]
  
          S = np.sqrt(np.sum(these_maps**2, axis=1))
          S[S == 0] = 1
          these_maps /= S[:, np.newaxis]
  
          K = np.abs(masked_components.dot(these_maps.T))
          recovered_maps = np.sum(K > 0.9)
  
          assert recovered_maps >= 2
  
imports: import numpy
  import pytest
  from nibabel import Nifti1Image
  from nilearn._utils.testing import write_imgs_to_path
  from nilearn.conftest import _affine_eye
  from nilearn.decomposition.dict_learning import DictLearning
  from nilearn.decomposition.tests.test_canica import _make_canica_test_data
  from nilearn.image import get_data, iter_img
  from nilearn.maskers import NiftiMasker
label_desc: Use image node_id 4 for calling a global function with example usage: test_dict_learning(mask_img) without return types