code: def _generate_noise_temporal_autoregression(
      timepoints,
      noise_dict,
      dimensions,
      mask,
  ):
      """Generate the autoregression noise
      Make a slowly drifting timecourse with the given autoregression
      parameters. This can take in both AR and MA components
  
      Parameters
      ----------
  
      timepoints : 1 Dimensional array
          What time points are sampled by a TR
  
      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'
  
      dimensions : 3 length array, int
          What is the shape of the volume to be generated
  
      mask : 3 dimensional array, binary
          The masked brain, thresholded to distinguish brain and non-brain
  
      Returns
      ----------
      noise_autoregression : one dimensional array, float
          Generates the autoregression noise timecourse
  
      """
  
      # Pull out the relevant noise parameters
      auto_reg_rho = noise_dict["auto_reg_rho"]
      ma_rho = noise_dict["ma_rho"]
  
      # Specify the order based on the number of rho supplied
      auto_reg_order = len(auto_reg_rho)
      ma_order = len(ma_rho)
  
      # This code assumes that the AR order is higher than the MA order
      if ma_order > auto_reg_order:
          msg = (
              "MA order (%d) is greater than AR order (%d). Cannot run."
              % (ma_order, auto_reg_order)
          )
          raise ValueError(msg)
  
      # Generate a random variable at each time point that is a decayed value
      # of the previous time points
      noise_autoregression = np.zeros(
          (dimensions[0], dimensions[1], dimensions[2], len(timepoints))
      )
      err_vols = np.zeros(
          (dimensions[0], dimensions[1], dimensions[2], len(timepoints))
      )
      for tr_counter in range(len(timepoints)):
          # Create a brain shaped volume with appropriate smoothing properties
          noise = _generate_noise_spatial(
              dimensions=dimensions,
              mask=mask,
              fwhm=noise_dict["fwhm"],
          )
  
          # Store all of the noise volumes
          err_vols[:, :, :, tr_counter] = noise
  
          if tr_counter == 0:
              noise_autoregression[:, :, :, tr_counter] = noise
  
          else:
              # Preset the volume to collect the AR estimated process
              AR_vol = np.zeros(
                  (dimensions[0], dimensions[1], dimensions[2])
              )
  
              # Iterate through both the AR and MA values
              for pCounter in list(range(1, auto_reg_order + 1)):
                  past_TR = int(tr_counter - pCounter)
  
                  if tr_counter - pCounter >= 0:
                      # Pull out a previous TR
                      past_vols = noise_autoregression[:, :, :, past_TR]
  
                      # Add the discounted previous volume
                      AR_vol += past_vols * auto_reg_rho[pCounter - 1]
  
                      # If the MA order has at least this many coefficients
                      # then consider the error terms
                      if ma_order >= pCounter:
                          # Pull out a previous TR
                          past_noise = err_vols[:, :, :, past_TR]
  
                          # Add the discounted previous noise
                          AR_vol += past_noise * ma_rho[pCounter - 1]
  
              noise_autoregression[:, :, :, tr_counter] = AR_vol + noise
  
      # Z score the data so that all of the standard deviations of the voxels
      # are one (but the ARMA coefs are unchanged)
      noise_autoregression = stats.zscore(noise_autoregression, 3)
  
      return noise_autoregression
  
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 18 for calling a global function with example usage: _generate_noise_temporal_autoregression(timepoints, noise_dict, dimensions, mask) and returns: noise_autoregression