code: def _calc_fwhm(
      volume,
      mask,
      voxel_size=[1.0, 1.0, 1.0],
  ):
      """Calculate the FWHM of a volume
      Estimates the FWHM (mm) of a volume's non-masked voxels
  
      Parameters
      ----------
  
      volume : 3 dimensional array
          Functional data to have the FWHM measured.
  
      mask : 3 dimensional array
          A binary mask of the brain voxels in volume
  
      voxel_size : length 3 list, float
          Millimeters per voxel for x, y and z.
  
      Returns
      -------
  
      fwhm : float, list
          Returns the FWHM of each TR in mm
  
      """
  
      # What are the dimensions of the volume
      dimensions = volume.shape
  
      # Iterate through the TRs, creating a FWHM for each TR
  
      # Preset
      v_count = 0
      v_sum = 0
      v_sq = 0
  
      d_sum = [0.0, 0.0, 0.0]
      d_sq = [0.0, 0.0, 0.0]
      d_count = [0, 0, 0]
  
      # Pull out all the voxel coordinates
      coordinates = list(
          product(
              range(dimensions[0]),
              range(dimensions[1]),
              range(dimensions[2]),
          )
      )
  
      # Find the sum of squared error for the non-masked voxels in the brain
      for i in list(range(len(coordinates))):
          # Pull out this coordinate
          x, y, z = coordinates[i]
  
          # Is this within the mask?
          if mask[x, y, z] > 0:
              # Find the the volume sum and squared values
              v_count += 1
              v_sum += np.abs(volume[x, y, z])
              v_sq += volume[x, y, z] ** 2
  
      # Get the volume variance
      v_var = (v_sq - ((v_sum**2) / v_count)) / (v_count - 1)
  
      for i in list(range(len(coordinates))):
          # Pull out this coordinate
          x, y, z = coordinates[i]
  
          # Is this within the mask?
          if mask[x, y, z] > 0:
              # For each xyz dimension calculate the squared
              # difference of this voxel and the next
  
              in_range = x < dimensions[0] - 1
              in_mask = in_range and (mask[x + 1, y, z] > 0)
              included = in_mask and (~np.isnan(volume[x + 1, y, z]))
              if included:
                  d_sum[0] += volume[x, y, z] - volume[x + 1, y, z]
                  d_sq[0] += (
                      volume[x, y, z] - volume[x + 1, y, z]
                  ) ** 2
                  d_count[0] += 1
  
              in_range = y < dimensions[1] - 1
              in_mask = in_range and (mask[x, y + 1, z] > 0)
              included = in_mask and (~np.isnan(volume[x, y + 1, z]))
              if included:
                  d_sum[1] += volume[x, y, z] - volume[x, y + 1, z]
                  d_sq[1] += (
                      volume[x, y, z] - volume[x, y + 1, z]
                  ) ** 2
                  d_count[1] += 1
  
              in_range = z < dimensions[2] - 1
              in_mask = in_range and (mask[x, y, z + 1] > 0)
              included = in_mask and (~np.isnan(volume[x, y, z + 1]))
              if included:
                  d_sum[2] += volume[x, y, z] - volume[x, y, z + 1]
                  d_sq[2] += (
                      volume[x, y, z] - volume[x, y, z + 1]
                  ) ** 2
                  d_count[2] += 1
  
      # Find the variance
      d_var = np.divide(
          (d_sq - np.divide(np.power(d_sum, 2), d_count)),
          (np.add(d_count, -1)),
      )
  
      o_var = np.divide(-1, (4 * np.log(1 - (0.5 * d_var / v_var))))
      fwhm3 = np.sqrt(o_var) * 2 * np.sqrt(2 * np.log(2))
      fwhm = np.prod(np.multiply(fwhm3, voxel_size)) ** (1 / 3)
  
      return fwhm
  
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 10 for calling a global function with example usage: _calc_fwhm(volume, mask, voxel_size) and returns: fwhm