code: def _generate_noise_temporal(
      stimfunction_tr,
      tr_duration,
      dimensions,
      template,
      mask,
      noise_dict,
  ):
      """Generate the temporal noise
      Generate the time course of the average brain voxel. To change the
      relative mixing of the noise components, change the sigma's specified
      below.
  
      Parameters
      ----------
  
      stimfunction_tr : 1 Dimensional array
          This is the timecourse of the stimuli in this experiment,
          each element represents a TR
  
      tr_duration : int
          How long is a TR, in seconds
  
      dimensions : 3 length array, int
          What is the shape of the volume to be generated
  
      template : 3d array, float
          A continuous (0 -> 1) volume describing the likelihood a voxel is in
          the brain. This can be used to contrast the brain and non brain.
  
      mask : 3 dimensional array, binary
          The masked brain, thresholded to distinguish brain and non-brain
  
      noise_dict : dict
          A dictionary specifying the types of noise in this experiment. The
          noise types interact in important ways. First, autoregressive,
          physiological and task-based noise types are mixed together in
          _generate_temporal_noise. The parameter values for 'auto_reg_sigma',
          'physiological_sigma' and 'task_sigma' describe the proportion of
          mixing of these elements, respectively. However critically, 'SFNR' is
          the parameter that controls how much noise these components contribute
          to the brain. 'auto_reg_rho' and 'ma_rho' set parameters for the
          autoregressive noise being simulated. Second, drift noise is added to
          this, according to the size of 'drift_sigma'. Thirdly, system noise is
          added based on the 'SNR' parameter. Finally, 'fwhm' is used to estimate
          the smoothness of the noise being inserted. If 'matched' is set to
          true, then it will fit the parameters to match the participant as best
          as possible.
  
          Variables defined as follows:
  
          snr [float]: Ratio of MR signal to the spatial noise
          sfnr [float]: Ratio of the MR signal to the temporal noise. This is the
          total variability that the following sigmas 'sum' to:
  
          task_sigma [float]: Size of the variance of task specific noise
          auto_reg_sigma [float]: Size of the variance of autoregressive
          noise. This is an ARMA process where the AR and MA components can be
          separately specified
          physiological_sigma [float]: Size of the variance of physiological
          noise
  
          drift_sigma [float]: Size of the variance of drift noise
  
          auto_reg_rho [list]: The coefficients of the autoregressive
          components you are modeling
          ma_rho [list]:The coefficients of the moving average components you
          are modeling
          max_activity [float]: The max value of the averaged brain in order
          to reference the template
          voxel_size [list]: The mm size of the voxels
          fwhm [float]: The gaussian smoothing kernel size (mm)
          matched [bool]: Specify whether you are fitting the noise parameters
  
          The volumes of brain noise that are generated have smoothness
          specified by 'fwhm'
  
      Returns
      ----------
  
      noise_temporal : one dimensional array, float
          Generates the temporal noise timecourse for these parameters
  
      """
  
      # Set up common parameters
      # How many TRs are there
      trs = len(stimfunction_tr)
  
      # What time points are sampled by a TR?
      timepoints = list(np.linspace(0, (trs - 1) * tr_duration, trs))
  
      # Preset the volume
      noise_volume = np.zeros(
          (dimensions[0], dimensions[1], dimensions[2], trs)
      )
  
      # Generate the physiological noise
      if noise_dict["physiological_sigma"] != 0:
          # Calculate the physiological time course
          noise = _generate_noise_temporal_phys(
              timepoints,
          )
  
          # Create a brain shaped volume with similar smoothing properties
          volume = _generate_noise_spatial(
              dimensions=dimensions,
              mask=mask,
              fwhm=noise_dict["fwhm"],
          )
  
          # Combine the volume and noise
          noise_volume += (
              np.multiply.outer(volume, noise)
              * noise_dict["physiological_sigma"]
          )
  
      # Generate the AR noise
      if noise_dict["auto_reg_sigma"] != 0:
          # Calculate the AR time course volume
          noise = _generate_noise_temporal_autoregression(
              timepoints,
              noise_dict,
              dimensions,
              mask,
          )
  
          # Combine the volume and noise
          noise_volume += noise * noise_dict["auto_reg_sigma"]
  
      # Generate the task related noise
      if noise_dict["task_sigma"] != 0 and np.sum(stimfunction_tr) > 0:
          # Calculate the task based noise time course
          noise = _generate_noise_temporal_task(
              stimfunction_tr,
          )
  
          # Create a brain shaped volume with similar smoothing properties
          volume = _generate_noise_spatial(
              dimensions=dimensions,
              mask=mask,
              fwhm=noise_dict["fwhm"],
          )
          # Combine the volume and noise
          noise_volume += (
              np.multiply.outer(volume, noise)
              * noise_dict["task_sigma"]
          )
  
      # Finally, z score each voxel so things mix nicely
      noise_volume = stats.zscore(noise_volume, 3)
  
      # If it is a nan it is because you just divided by zero (since some
      # voxels are zeros in the template)
      noise_volume[np.isnan(noise_volume)] = 0
  
      return noise_volume
  
imports: import logging
  from itertools import product
  from statsmodels.tsa.arima.model import ARIMA
  import math
  import numpy
  from numpy.linalg import LinAlgError
  from pkg_resources import resource_stream
  from scipy import stats
  from scipy import signal
  import scipy.ndimage
  import copy
  from scipy import optimize
label_desc: Use image node_id 21 for calling a global function with example usage: _generate_noise_temporal(stimfunction_tr, tr_duration, dimensions, template, mask, noise_dict) and returns: noise_volume