code: def assert_memory_less_than(
      memory_limit, tolerance, callable_obj, *args, **kwargs
  ):
      """Check memory consumption of a callable stays below a given limit.
  
      Parameters
      ----------
      memory_limit : int
          The expected memory limit in MiB.
  
      tolerance : float
          As memory_profiler results have some variability, this adds some
          tolerance around memory_limit. Accepted values are in range [0.0, 1.0].
  
      callable_obj : callable
          The function to be called to check memory consumption.
  
      """
      mem_used = memory_used(callable_obj, *args, **kwargs)
  
      if mem_used > memory_limit * (1 + tolerance):
          raise ValueError(
              f"Memory consumption measured ({mem_used:.2f} MiB) is "
              f"greater than required memory limit ({memory_limit} MiB) within "
              f"accepted tolerance ({tolerance * 100:.2f}%)."
          )
  
      # We are confident in memory_profiler measures above 100MiB.
      # We raise an error if the measure is below the limit of 50MiB to avoid
      # false positive.
      if mem_used < 50:
          raise ValueError(
              "Memory profiler measured an untrustable memory "
              f"consumption ({mem_used:.2f} MiB). The expected memory "
              f"limit was {memory_limit:.2f} MiB. Try to bench with larger "
              "objects (at least 100MiB in memory)."
          )
  
file_path: nilearn/nilearn/_utils/testing.py
imports: import functools
  import gc
  import os
  import sys
  import tempfile
  import warnings
  from pathlib import Path
  import pytest
label_desc: Use image node_id 3 for calling a global function with example usage: assert_memory_less_than(memory_limit, tolerance, callable_obj) without return types