docs: def roi_extraction(imdic, amyroi, datain, Cnt, use_stored=False):
      '''
      Extracting ROI values of upsampled PET.  If not provided it will be upsampled.
      imdic: dictionary of all image parts (image, path to file and affine transformation)    
       > fpet: file name for quantitative PET
       > faff: file name and path to the affine (rigid) transformations, most likely for upscaled PET
      amyroi: module with ROI definitions in numbers
      datain: dictionary of all the input data
      '''
  
      fpet = imdic['fpet']
      faff = imdic['faff']
      im   = imdic['im']
  
      if Cnt['VERBOSE']: print 'i> extracting ROI values'
  
      # split the labels path
      lbpth = os.path.split(datain['T1lbl'])
      # # MR T1w NIfTI
      # if os.path.isfile(datain['T1nii']):
      #     ft1w = datain['T1nii']
      # elif os.path.isfile(datain['T1bc']):
      #     ft1w = datain['T1nii']
      # else:
      #     print 'e> no T1w NIfTI!'
      #     sys.exit()
  
      prcl_dir = os.path.dirname(datain['T1lbl'])
  
      dscrp = imio.getnii_descr(fpet)
      impvc = False
      if 'pvc' in dscrp.keys():       
          impvc = True
          if Cnt['VERBOSE']: print 'i> the provided image is partial volume corrected.'
  
      #------------------------------------------------------------------------------------------------------------  
      # next steps after having sorted out the upsampled input image and the rigid transformation for the T1w -> PET:
  
      # Get the labels before resampling to PET space so that the regions can be separated (more difficult to separate after resampling)
      if os.path.isfile(datain['T1lbl']):
          nilb = nib.load(datain['T1lbl'])
          A = nilb.get_sform()
          imlb = nilb.get_data()
      else:
          print 'e> parcellation label image not present!'
          sys.exit()
  
      # ===============================================================
      # get the segmentation/parcellation by creating an image for ROI extraction
      # (still in the original T1 orientation)
      roi_img = np.copy(imlb)
  
      # sum of all voxel values in any given ROI
      roisum = {}
      # sum of the mask values <0,1> used for getting the mean value of any ROI
      roimsk = {}
      for k in amyroi.rois.keys():
          roisum[k] = []
          roimsk[k] = []
  
      # extract the ROI values from PV corrected and uncorrected PET images
      for k in amyroi.rois.keys():
          roi_img[:] = 0
          for i in amyroi.rois[k]:
              roi_img[imlb==i] = 1.
          # now save the image mask to nii file <froi1>
          froi1 = os.path.join(prcl_dir, lbpth[1].split('.')[0][:8]+'_'+k+'.nii.gz')
          nii_roi = nib.Nifti1Image(roi_img, A)
          nii_roi.header['cal_max'] = 1.
          nii_roi.header['cal_min'] = 0.
          nib.save(nii_roi, froi1)
          # file name for the resampled ROI to PET space
          froi2 = os.path.join(prcl_dir, os.path.basename(datain['T1lbl']).split('.')[0]+ '_toPET_'+k+'.nii.gz')
          if not use_stored and os.path.isfile( Cnt['RESPATH'] ):
              cmd = [Cnt['RESPATH'],
                  '-ref', fpet,
                  '-flo', froi1,
                  '-trans', faff,
                  '-res', froi2]#,
                  #'-pad', '0']
              if not Cnt['VERBOSE']: cmd.append('-voff') 
              call(cmd)
          # get the resampled ROI mask image
          rmsk = imio.getnii(froi2)
          rmsk[rmsk>1.] = 1.
          rmsk[rmsk<0.] = 0.
  
          # erode the cerebral white matter region if no PVC image
          if k=='wm' and not impvc:
              if Cnt['VERBOSE']: print 'i> eroding white matter as PET image not partial volume corrected.'
              nilb = nib.load(froi2)
              B = nilb.get_sform()
              tmp = ndi.filters.gaussian_filter(rmsk, imio.fwhm2sig(12,Cnt['VOXY']), mode='mirror')
              rmsk = np.float32(tmp>0.85)
              froi3 = os.path.join(prcl_dir, os.path.basename(datain['T1lbl']).split('.')[0]+ '_toPET_'+k+'_eroded.nii.gz')
              savenii(rmsk, froi3, B, Cnt)
  
          # ROI value and mask sums:
          rvsum  = np.sum(im*rmsk)
          rmsum  = np.sum(rmsk)
          roisum[k].append( rvsum )
          roimsk[k].append( rmsum )
          if Cnt['VERBOSE']: 
              print ''
              print '================================================================'
              print 'i> ROI extracted:', k
              print '   > value sum:', rvsum
              print '   > # voxels :', rmsum
              print '================================================================'
      # --------------------------------------------------------------
  
      return roisum, roimsk