def gen_peak_mask(anat_mask,func_activation): import nibabel as nb import numpy as np from fir_utils import (binarize_peak, dilate_mask) import sys import os anatmask = nb.load(anat_mask) mask_data = np.asarray(anatmask.get_data()) mask_header = anatmask.get_header() mask_affine = anatmask.get_affine() func = nb.load(func_activation) func_data = np.asarray(func.get_data()) func_header = func.get_header() func_affine = func.get_affine() if np.array(func_data).shape != np.array(mask_data).shape: print np.array(func_data).shape print np.array(mask_data).shape sys.exit("functional and mask do not have the same 3d shape!") masked_activation = np.zeros(func_data.shape) masked_activation[np.nonzero(mask_data)] = func_data[np.nonzero(mask_data)] peak_mask = binarize_peak(masked_activation) dilated_peak_mask = dilate_mask(peak_mask) path = '%s/%s.nii'%(os.path.abspath(os.path.curdir),'peak_mask') out_image = nb.Nifti1Image(dilated_peak_mask,mask_affine,mask_header) out_image.to_filename(path) return path
def extract_segmentation_roi(func_aparc_aseg, segmentation_value): """binarizes the volume at locations equal to segmentation_value""" import nibabel as nb import numpy as np import os from fir_utils import (functional_binarize, dilate_mask) import sys segmentation = nb.load(func_aparc_aseg) seg_data = segmentation.get_data() header = segmentation.get_header() affine = segmentation.get_affine() seg_mask = functional_binarize(seg_data,lambda x: x == segmentation_value) path = '%s/%s.nii'%(os.path.abspath(os.path.curdir),'mask_'+str(segmentation_value)) dilated_mask = dilate_mask(np.array(seg_mask)) out_image = nb.Nifti1Image(dilated_mask,affine,header) out_image.to_filename(path) return path