code: def nii2cmu(nifti_file, mask_file=None):
      """
      inputs:
        nifti_file: a filename of a .nii or .nii.gz file to be converted into
                    CMU format
  
        mask_file: a filename of a .nii or .nii.gz file to be used as a mask; all
                   zero-valued voxels in the mask will be ignored in the CMU-
                   formatted output.  If ignored or set to None, no voxels will
                   be masked out.
  
      outputs:
        Y: a number-of-timepoints by number-of-voxels numpy array containing the
           image data.  Each row of Y is an fMRI volume in the original nifti
           file.
  
        R: a number-of-voxels by 3 numpy array containing the voxel locations.
           Row indices of R match the column indices in Y.
      """
  
      def fullfact(dims):
          """
          Replicates MATLAB's fullfact function (behaves the same way)
          """
          vals = np.asmatrix(range(1, dims[0] + 1)).T
          if len(dims) == 1:
              return vals
          else:
              aftervals = np.asmatrix(fullfact(dims[1:]))
              inds = np.asmatrix(np.zeros((np.prod(dims), len(dims))))
              row = 0
              for i in range(aftervals.shape[0]):
                  inds[row : (row + len(vals)), 0] = vals
                  inds[row : (row + len(vals)), 1:] = np.tile(
                      aftervals[i, :], (len(vals), 1)
                  )
                  row += len(vals)
              return inds
  
      with warnings.catch_warnings():
          warnings.simplefilter("ignore")
  
          img = nib.load(nifti_file)
          mask = NiftiMasker(mask_strategy="background")
          if mask_file is None:
              mask.fit(nifti_file)
          else:
              mask.fit(mask_file)
  
      hdr = img.header
      S = img.get_sform()
      vox_size = hdr.get_zooms()
      im_size = img.shape
  
      if len(img.shape) > 3:
          N = img.shape[3]
      else:
          N = 1
  
      Y = np.float32(mask.transform(nifti_file)).copy()
      vmask = np.nonzero(
          np.array(
              np.reshape(
                  mask.mask_img_.dataobj,
                  (1, np.prod(mask.mask_img_.shape)),
                  order="C",
              )
          )
      )[1]
      vox_coords = fullfact(img.shape[0:3])[vmask, ::-1] - 1
  
      R = np.array(np.dot(vox_coords, S[0:3, 0:3])) + S[:3, 3]
  
      return {"Y": Y, "R": R}
  
imports: import numpy
  import pandas
  import nibabel
  import nilearn
  from nilearn.input_data import NiftiMasker
  import nilearn.plotting
  import matplotlib
  import matplotlib.pyplot
  import holoviews
  import timecorr
  import os
  from brainiak.factoranalysis.htfa import HTFA
  import warnings
  import seaborn
label_desc: Use image node_id 8 for calling a global function with example usage: nii2cmu(nifti_file, mask_file) and returns: dict, vals, inds