original_docstring: 
      Utility function to calculate a mask in which check
      for :abbr:`EM (electromagnetic)` spikes.
      
docstring: Utility function to calculate a mask in which check
  for :abbr:`EM (electromagnetic)` spikes.
docstring_tokens: Utility function to calculate a mask in which check for : abbr : ` EM ( electromagnetic ) ` spikes .
code: def spikes_mask(in_file, in_mask=None, out_file=None):
      """
      Utility function to calculate a mask in which check
      for :abbr:`EM (electromagnetic)` spikes.
      """
  
      import os.path as op
      import nibabel as nb
      import numpy as np
      from nilearn.image import mean_img
      from nilearn.plotting import plot_roi
      from scipy import ndimage as nd
  
      if out_file is None:
          fname, ext = op.splitext(op.basename(in_file))
          if ext == '.gz':
              fname, ext2 = op.splitext(fname)
              ext = ext2 + ext
          out_file = op.abspath('{}_spmask{}'.format(fname, ext))
          out_plot = op.abspath('{}_spmask.pdf'.format(fname))
  
      in_4d_nii = nb.load(in_file)
      orientation = nb.aff2axcodes(in_4d_nii.affine)
  
      if in_mask:
          mask_data = nb.load(in_mask).get_data()
          a = np.where(mask_data != 0)
          bbox = np.max(a[0]) - np.min(a[0]), np.max(a[1]) - \
              np.min(a[1]), np.max(a[2]) - np.min(a[2])
          longest_axis = np.argmax(bbox)
  
          # Input here is a binarized and intersected mask data from previous section
          dil_mask = nd.binary_dilation(
              mask_data, iterations=int(mask_data.shape[longest_axis] / 9))
  
          rep = list(mask_data.shape)
          rep[longest_axis] = -1
          new_mask_2d = dil_mask.max(axis=longest_axis).reshape(rep)
  
          rep = [1, 1, 1]
          rep[longest_axis] = mask_data.shape[longest_axis]
          new_mask_3d = np.logical_not(np.tile(new_mask_2d, rep))
      else:
          new_mask_3d = np.zeros(in_4d_nii.shape[:3]) == 1
  
      if orientation[0] in ['L', 'R']:
          new_mask_3d[0:2, :, :] = True
          new_mask_3d[-3:-1, :, :] = True
      else:
          new_mask_3d[:, 0:2, :] = True
          new_mask_3d[:, -3:-1, :] = True
  
      mask_nii = nb.Nifti1Image(new_mask_3d.astype(np.uint8), in_4d_nii.get_affine(),
                                in_4d_nii.get_header())
      mask_nii.to_filename(out_file)
  
      plot_roi(mask_nii, mean_img(in_4d_nii), output_file=out_plot)
      return out_file, out_plot
code_tokens: def spikes_mask ( in_file , in_mask = None , out_file = None ) : import os . path as op import nibabel as nb import numpy as np from nilearn . image import mean_img from nilearn . plotting import plot_roi from scipy import ndimage as nd if out_file is None : fname , ext = op . splitext ( op . basename ( in_file ) ) if ext == '.gz' : fname , ext2 = op . splitext ( fname ) ext = ext2 + ext out_file = op . abspath ( '{}_spmask{}' . format ( fname , ext ) ) out_plot = op . abspath ( '{}_spmask.pdf' . format ( fname ) ) in_4d_nii = nb . load ( in_file ) orientation = nb . aff2axcodes ( in_4d_nii . affine ) if in_mask : mask_data = nb . load ( in_mask ) . get_data ( ) a = np . where ( mask_data != 0 ) bbox = np . max ( a [ 0 ] ) - np . min ( a [ 0 ] ) , np . max ( a [ 1 ] ) - np . min ( a [ 1 ] ) , np . max ( a [ 2 ] ) - np . min ( a [ 2 ] ) longest_axis = np . argmax ( bbox ) dil_mask = nd . binary_dilation ( mask_data , iterations = int ( mask_data . shape [ longest_axis ] / 9 ) ) rep = list ( mask_data . shape ) rep [ longest_axis ] = - 1 new_mask_2d = dil_mask . max ( axis = longest_axis ) . reshape ( rep ) rep = [ 1 , 1 , 1 ] rep [ longest_axis ] = mask_data . shape [ longest_axis ] new_mask_3d = np . logical_not ( np . tile ( new_mask_2d , rep ) ) else : new_mask_3d = np . zeros ( in_4d_nii . shape [ : 3 ] ) == 1 if orientation [ 0 ] in [ 'L' , 'R' ] : new_mask_3d [ 0 : 2 , : , : ] = True new_mask_3d [ - 3 : - 1 , : , : ] = True else : new_mask_3d [ : , 0 : 2 , : ] = True new_mask_3d [ : , - 3 : - 1 , : ] = True mask_nii = nb . Nifti1Image ( new_mask_3d . astype ( np . uint8 ) , in_4d_nii . get_affine ( ) , in_4d_nii . get_header ( ) ) mask_nii . to_filename ( out_file ) plot_roi ( mask_nii , mean_img ( in_4d_nii ) , output_file = out_plot ) return out_file , out_plot
short_docstring: Utility function to calculate a mask in which check
  for :abbr:`EM (electromagnetic)` spikes.
short_docstring_tokens: Utility function to calculate a mask in which check for : abbr : ` EM ( electromagnetic ) ` spikes .
comment: """
      Utility function to calculate a mask in which check
      for :abbr:`EM (electromagnetic)` spikes.
      """$SEP$# Input here is a binarized and intersected mask data from previous section