def make_wm_mask(seg_path, dwi_path, out_path, TensorModel, cutoff=2): """ Function to make a conservative WM mask, excluding partial volumed voxels Parameters ---------- seg_path : the full path to a white matter segmentation generated with the itkGray conventions (in particular, 3 and 4 are the classification for WM in left and right hemispheres). cutoff : how many IQRs away from the median should we cut off from. """ ## if __name__=="__main__": ## seg_path = sys.argv[1] ## dwi_path = sys.argv[2] # This should lead to nii.gz, bvecs and bvals ## out_path = sys.argv[3] # Load the classification information: seg_ni = ni.load(seg_path) seg_vimg = as_volume_img(seg_ni) dwi_ni = ni.load(dwi_path + '.nii.gz') vimg = as_volume_img(seg_ni) # This does the magic - resamples using nearest neighbor interpolation: seg_resamp_vimg = vimg.as_volume_img(affine=dwi_ni.get_affine(), shape=dwi_ni.shape[:-1], interpolation='nearest') seg_resamp = seg_resamp_vimg.get_data() # We know that WM is classified as 3 and 4 (for the two different # hemispheres): wm_idx = np.where(np.logical_or(seg_resamp==3, seg_resamp==4)) vol = np.zeros(seg_resamp.shape) vol[wm_idx] = 1 if TensorModel is None: # OK - now we need to find and exclude MD outliers: TensorModel = ozm.TensorModel(dwi_path + '.nii.gz', dwi_path + '.bvecs', dwi_path + '.bvals', mask=vol, params_file='temp') MD = TensorModel.mean_diffusivity IQR = (stats.scoreatpercentile(MD[np.isfinite(MD)],75) - stats.scoreatpercentile(MD[np.isfinite(MD)],25)) cutoff = np.median(MD[np.isfinite(MD)]) + 2 * IQR cutoff_idx = np.where(MD > cutoff) # Null 'em out: vol[cutoff_idx] = 0 # Now, let's save some output: ni.Nifti1Image(vol, dwi_ni.get_affine()).to_filename(out_path)
def resample_volume(source, target): """ Resample the file in file_orig (full path string) to the resolution and affine of file_target (also a full path string) """ if not isinstance(source, ni.Nifti1Image): source = ni.load(source) if not isinstance(target, ni.Nifti1Image): target = ni.load(target) source_vimg = as_volume_img(source) # This does the magic - resamples using nearest neighbor interpolation: source_resamp_vimg = source_vimg.as_volume_img(affine=target.get_affine(), shape=target.shape[:3], interpolation='nearest') return ni.Nifti1Image(source_resamp_vimg.get_data(), target.get_affine())
def make_wm_mask(seg_path, dwi, out_path, cutoff=2): """ Function to make a conservative WM mask, excluding partial volumed voxels Parameters ---------- seg_path : the full path to a white matter segmentation generated with the itkGray conventions (in particular, 3 and 4 are the classification for WM in left and right hemispheres). dwi : the full path to a diffusion imaging data set, which will be used to calculate the mean diffusivity for the exclusion of outliers (presumably partial-volumed with the CSF) OR a TensorModel object, from which the MD will be calculated (which can be a way to save time on the calculation of the tensor parameters out_path : the full path to where you want to put the wm mask once it's calculated cutoff : how many IQR away from median should we cut off on. Note ---- Don't forget to look at your data, to make sure that things are sane. """ # Load the classification information: seg_ni = ni.load(seg_path) seg_vimg = as_volume_img(seg_ni) if isinstance(dwi, str): dwi_ni = ni.load(dwi + '.nii.gz') else: dwi_ni = ni.load(dwi.data_file) vimg = as_volume_img(seg_ni) # This does the magic - resamples using nearest neighbor interpolation: seg_resamp_vimg = vimg.as_volume_img(affine=dwi_ni.get_affine(), shape=dwi_ni.shape[:-1], interpolation='nearest') seg_resamp = seg_resamp_vimg.get_data() # We know that WM is classified as 3 and 4 (for the two different # hemispheres): wm_idx = np.where(np.logical_or(seg_resamp == 3, seg_resamp == 4)) vol = np.zeros(seg_resamp.shape) vol[wm_idx] = 1 if cutoff: if isinstance(dwi, str): # OK - now we need to find and exclude MD outliers: TensorModel = ozm.TensorModel(dwi + '.nii.gz', dwi + '.bvecs', dwi + '.bvals', mask=vol, params_file='temp') else: TensorModel = dwi MD = TensorModel.mean_diffusivity IQR = (stats.scoreatpercentile(MD[np.isfinite(MD)], 75) - stats.scoreatpercentile(MD[np.isfinite(MD)], 25)) co = np.median(MD[np.isfinite(MD)]) + cutoff * IQR co_idx = np.where(MD > co) # Null 'em out: vol[co_idx] = 0 # Now, let's save some output: ni.Nifti1Image(vol, dwi_ni.get_affine()).to_filename(out_path)
def make_wm_mask(seg_path, dwi, out_path, cutoff=2): """ Function to make a conservative WM mask, excluding partial volumed voxels Parameters ---------- seg_path : the full path to a white matter segmentation generated with the itkGray conventions (in particular, 3 and 4 are the classification for WM in left and right hemispheres). dwi : the full path to a diffusion imaging data set, which will be used to calculate the mean diffusivity for the exclusion of outliers (presumably partial-volumed with the CSF) OR a TensorModel object, from which the MD will be calculated (which can be a way to save time on the calculation of the tensor parameters out_path : the full path to where you want to put the wm mask once it's calculated cutoff : how many IQR away from median should we cut off on. Note ---- Don't forget to look at your data, to make sure that things are sane. """ # Load the classification information: seg_ni = ni.load(seg_path) seg_vimg = as_volume_img(seg_ni) if isinstance(dwi, str): dwi_ni = ni.load(dwi + '.nii.gz') else: dwi_ni = ni.load(dwi.data_file) vimg = as_volume_img(seg_ni) # This does the magic - resamples using nearest neighbor interpolation: seg_resamp_vimg = vimg.as_volume_img(affine=dwi_ni.get_affine(), shape=dwi_ni.shape[:-1], interpolation='nearest') seg_resamp = seg_resamp_vimg.get_data() # We know that WM is classified as 3 and 4 (for the two different # hemispheres): wm_idx = np.where(np.logical_or(seg_resamp==3, seg_resamp==4)) vol = np.zeros(seg_resamp.shape) vol[wm_idx] = 1 if cutoff: if isinstance(dwi, str): # OK - now we need to find and exclude MD outliers: TensorModel = ozm.TensorModel(dwi + '.nii.gz', dwi + '.bvecs', dwi + '.bvals', mask=vol, params_file='temp') else: TensorModel = dwi MD = TensorModel.mean_diffusivity IQR = (stats.scoreatpercentile(MD[np.isfinite(MD)],75) - stats.scoreatpercentile(MD[np.isfinite(MD)],25)) co = np.median(MD[np.isfinite(MD)]) + cutoff * IQR co_idx = np.where(MD > co) # Null 'em out: vol[co_idx] = 0 # Now, let's save some output: ni.Nifti1Image(vol, dwi_ni.get_affine()).to_filename(out_path)