code: def compute_mask_files(
      input_filename,
      output_filename=None,
      return_mean=False,
      m=0.2,
      M=0.9,
      cc=1,
      exclude_zeros=False,
      opening=2,
  ):
      """
      Compute a mask file from fMRI nifti file(s)
  
      Compute and write the mask of an image based on the grey level
      This is based on an heuristic proposed by T.Nichols:
      find the least dense point of the histogram, between fractions
      m and M of the total image histogram.
  
      In case of failure, it is usually advisable to increase m.
  
      Parameters
      ----------
      input_filename : string
          nifti filename (4D) or list of filenames (3D).
      output_filename : string or None, optional
          path to save the output nifti image (if not None).
      return_mean : boolean, optional
          if True, and output_filename is None, return the mean image also, as
          a 3D array (2nd return argument).
      m : float, optional
          lower fraction of the histogram to be discarded.
      M: float, optional
          upper fraction of the histogram to be discarded.
      cc: boolean, optional
          if cc is True, only the largest connect component is kept.
      exclude_zeros: boolean, optional
          Consider zeros as missing values for the computation of the
          threshold. This option is useful if the images have been
          resliced with a large padding of zeros.
      opening: int, optional
          Size of the morphological opening performed as post-processing
  
      Returns
      -------
      mask : 3D boolean array
          The brain mask
      mean_image : 3d ndarray, optional
          The main of all the images used to estimate the mask. Only
          provided if `return_mean` is True.
      """
      if isinstance(input_filename, str):
          # One single filename or image
          nim = load(input_filename)  # load the image from the path
          vol_arr = get_unscaled_data(nim)
          header = get_header(nim)
          affine = get_affine(nim)
          if vol_arr.ndim == 4:
              if isinstance(vol_arr, np.memmap):
                  # Get rid of memmapping: it is faster.
                  mean_volume = np.array(vol_arr, copy=True).mean(
                      axis=-1
                  )
              else:
                  mean_volume = vol_arr.mean(axis=-1)
              # Make a copy, to avoid holding a reference on the full array,
              # and thus polluting the memory.
              first_volume = vol_arr[..., 0].copy()
          elif vol_arr.ndim == 3:
              mean_volume = first_volume = vol_arr
          else:
              raise ValueError("Need 4D file for mask")
          del vol_arr
      else:
          # List of filenames
          if len(list(input_filename)) == 0:
              raise ValueError(
                  "input_filename should be a non-empty "
                  "list of file names"
              )
          # We have several images, we do mean on the fly,
          # to avoid loading all the data in the memory
          # We do not use the unscaled data here?:
          # if the scalefactor is being used to record real
          # differences in intensity over the run this would break
          for index, filename in enumerate(input_filename):
              nim = load(filename)
              if index == 0:
                  first_volume = nim.get_fdata().squeeze()
                  mean_volume = first_volume.copy().astype(np.float32)
                  header = get_header(nim)
                  affine = get_affine(nim)
              else:
                  mean_volume += nim.get_fdata().squeeze()
          mean_volume /= float(len(list(input_filename)))
      del nim
      if np.isnan(mean_volume).any():
          tmp = mean_volume.copy()
          tmp[np.isnan(tmp)] = 0
          mean_volume = tmp
  
      mask = compute_mask(
          mean_volume,
          first_volume,
          m,
          M,
          cc,
          opening=opening,
          exclude_zeros=exclude_zeros,
      )
  
      if output_filename is not None:
          header["descrip"] = "mask"
          output_image = nifti1.Nifti1Image(
              mask.astype(np.uint8), affine=affine, header=header
          )
          save(output_image, output_filename)
      if not return_mean:
          return mask
      else:
          return mask, mean_volume
  
file_path: nipy/nipy/labs/mask.py
imports: import math
  import numpy
  from nibabel import load, nifti1, save
  from scipy import ndimage
  from ..io.nibcompat import get_affine, get_header, get_unscaled_data
label_desc: Use image node_id 3 for calling a global function with example usage: compute_mask_files(input_filename, output_filename, return_mean, m, M, cc, exclude_zeros, opening) and returns: mask, mask,  mean_volume