code: def _check_memory(memory, verbose=0):
      """Ensure an instance of a joblib.Memory object.
  
      Parameters
      ----------
      memory : None,instance of joblib.Memory, str or pathlib.Path
          Used to cache the masking process.
          If a str is given, it is the path to the caching directory.
  
      verbose : int, default=0
          Verbosity level.
  
      Returns
      -------
      memory : instance of joblib.Memory.
  
      """
      if memory is None:
          memory = Memory(location=None, verbose=verbose)
      memory = stringify_path(memory)
      if isinstance(memory, str):
          cache_dir = memory
          if nilearn.EXPAND_PATH_WILDCARDS:
              cache_dir = os.path.expanduser(cache_dir)
  
          # Perform some verifications on given path.
          split_cache_dir = os.path.split(cache_dir)
          if len(split_cache_dir) > 1 and (
              not os.path.exists(split_cache_dir[0])
              and split_cache_dir[0] != ""
          ):
              if (
                  not nilearn.EXPAND_PATH_WILDCARDS
                  and cache_dir.startswith("~")
              ):
                  # Maybe the user want to enable expanded user path.
                  error_msg = (
                      "Given cache path parent directory doesn't "
                      f"exists, you gave '{split_cache_dir[0]}'. Enabling "
                      "nilearn.EXPAND_PATH_WILDCARDS could solve "
                      "this issue."
                  )
              elif memory.startswith("~"):
                  # Path built on top of expanded user path doesn't exist.
                  error_msg = (
                      "Given cache path parent directory doesn't "
                      f"exists, you gave '{split_cache_dir[0]}' "
                      "which was expanded as '{os.path.dirname(memory)}' "
                      "but doesn't exist either. "
                      "Use nilearn.EXPAND_PATH_WILDCARDS to deactivate "
                      "auto expand user path (~) behavior."
                  )
              else:
                  # The given cache base path doesn't exist.
                  error_msg = (
                      "Given cache path parent directory doesn't "
                      "exists, you gave '{split_cache_dir[0]}'."
                  )
              raise ValueError(error_msg)
  
          memory = Memory(location=cache_dir, verbose=verbose)
      return memory
  
file_path: nilearn/nilearn/_utils/cache_mixin.py
imports: import os
  import warnings
  from joblib import Memory
  import nilearn
  from .helpers import stringify_path
label_desc: Use image node_id 1 for calling a global function with example usage: _check_memory(memory, verbose) and returns: memory