code: def extrapolate_out_mask(data, mask, iterations=1):
      """Extrapolate values outside of the mask."""
      if iterations > 1:
          data, mask = extrapolate_out_mask(
              data, mask, iterations=iterations - 1
          )
      new_mask = binary_dilation(mask)
      larger_mask = np.zeros(np.array(mask.shape) + 2, dtype=bool)
      larger_mask[1:-1, 1:-1, 1:-1] = mask
      # Use nans as missing value: ugly
      masked_data = np.zeros(larger_mask.shape + data.shape[3:])
      masked_data[1:-1, 1:-1, 1:-1] = data.copy()
      masked_data[np.logical_not(larger_mask)] = np.nan
      outer_shell = larger_mask.copy()
      outer_shell[1:-1, 1:-1, 1:-1] = np.logical_xor(new_mask, mask)
      outer_shell_x, outer_shell_y, outer_shell_z = np.where(
          outer_shell
      )
      extrapolation = []
      for i, j, k in [
          (1, 0, 0),
          (-1, 0, 0),
          (0, 1, 0),
          (0, -1, 0),
          (0, 0, 1),
          (0, 0, -1),
      ]:
          this_x = outer_shell_x + i
          this_y = outer_shell_y + j
          this_z = outer_shell_z + k
          extrapolation.append(masked_data[this_x, this_y, this_z])
  
      extrapolation = np.array(extrapolation)
      extrapolation = np.nansum(extrapolation, axis=0) / np.sum(
          np.isfinite(extrapolation), axis=0
      )
      extrapolation[np.logical_not(np.isfinite(extrapolation))] = 0
      new_data = np.zeros_like(masked_data)
      new_data[outer_shell] = extrapolation
      new_data[larger_mask] = masked_data[larger_mask]
      return new_data[1:-1, 1:-1, 1:-1], new_mask
  
imports: import numbers
  import warnings
  import numpy
  from joblib import Parallel, delayed
  from scipy.ndimage import binary_dilation, binary_erosion
  from .None import _utils
  from ._utils import fill_doc
  from ._utils.cache_mixin import cache
  from ._utils.ndimage import get_border_data, largest_connected_component
  from ._utils.niimg import safe_get_data
  from .datasets import load_mni152_gm_template, load_mni152_template, load_mni152_wm_template
  from .image import get_data, new_img_like, resampling
label_desc: Use image node_id 2 for calling a global function with example usage: extrapolate_out_mask(data, mask, iterations) and returns: new_data,  new_mask