def extract_data(nifti_file, mask_file, out_file, zscore, detrend, smoothing_fwmw):
if mask_file is None:
#whole brain, get coordinate info from nifti_file itself
mask = nib.load(nifti_file.name)
else:
mask = nib.load(mask_file.name)
affine = mask.get_affine()
if mask_file is None:
mask_data = mask.get_data()
if mask_data.ndim == 4:
#get mask in 3D
img_data_type = mask.header.get_data_dtype()
n_tr = mask_data.shape[3]
mask_data = mask_data[:,:,:,n_tr//2].astype(bool)
mask = nib.Nifti1Image(mask_data.astype(img_data_type), affine)
else:
mask_data = mask_data.astype(bool)
else:
mask_data = mask.get_data().astype(bool)
#get voxel coordinates
R = np.float64(np.argwhere(mask_data))
#get scanner RAS coordinates based on voxel coordinates
if affine is not []:
R = (np.dot(affine[:3,:3], R.T) + affine[:3,3:4]).T
#get ROI data, and run preprocessing
nifti_masker = NiftiMasker(mask_img=mask, standardize=zscore, detrend=detrend, smoothing_fwhm=smoothing_fwmw)
img = nib.load(nifti_file.name)
all_images = np.float64(nifti_masker.fit_transform(img))
data = all_images.T.copy()
#save data
subj_data = {'data': data, 'R': R}
scipy.io.savemat(out_file.name, subj_data)
评论列表
文章目录