def get_wm_mask(wm=data_path + 'SUB1_wm_mask.nii.gz', resample=None): """ Get me a white matter mask. Resample if need be """ bm_nii = ni.load(wm) if resample is not None: return ozv.resample_volume(wm, resample).get_data() else: return ni.load(wm).get_data()
def get_brain_mask(bm=data_path + 'brainMask.nii.gz',resample=None): """ Get the brain mask (derived from DWI using mrDiffusion) and resample if needed """ bm_nii = ni.load(bm) if resample is not None: return ozv.resample_volume(bm, resample).get_data() else: return ni.load(bm).get_data()
def get_brain_mask(bm=data_path + 'brainMask.nii.gz', resample=None): """ Get the brain mask (derived from DWI using mrDiffusion) and resample if needed """ bm_nii = ni.load(bm) if resample is not None: return ozv.resample_volume(bm, resample).get_data() else: return ni.load(bm).get_data()
def get_t1(subject, resample=None): """ Get the high-res T1-weighted anatomical scan. If requested, resample it to the resolution of a nifti file for which the name is provided as input """ t1=data_path + '/%s/%s_t1.nii.gz'%(subject, subject) t1_nii = ni.load(t1) if resample is not None: return ozv.resample_volume(t1, resample).get_data() else: return ni.load(t1).get_data()
def get_t1(subject, resample=None): """ Get the high-res T1-weighted anatomical scan. If requested, resample it to the resolution of a nifti file for which the name is provided as input """ t1 = data_path + '/%s/%s_t1.nii.gz' % (subject, subject) t1_nii = ni.load(t1) if resample is not None: return ozv.resample_volume(t1, resample).get_data() else: return ni.load(t1).get_data()
def test_resample_volume(): """ Testing resampling of one volume into another volumes space (a t1 into a dwi space, for example) """ source = ni.Nifti1Image(np.random.randn(10,10,10), np.eye(4)) target = ni.Nifti1Image(np.random.randn(10,10,10), np.eye(4)) new_vol = ozv.resample_volume(source, target) npt.assert_equal(new_vol.shape, target.shape[:3])
def make_wm_mask(seg_path, dwi_path, out_path=data_path + 'wm_mask.nii.gz', n_IQR=2): """ Function that makes a white-matter mask from an itk-gray class file and DWI data, at the resolution of the DWI data. Parameters ---------- seg_path : str Full path to an itk-gray segmentation dwi_path : str Full path to some DWI data (used to determine the mean diffusivity in each voxel, see below) out_path : str (optional) Where to put the resulting file n_IQR: float (optional) How many IQRs away for the definition of MD outliers (see below) Note ---- This follows [Jeurissen2012]_. First, we resample a segmentation based on a T1-weighted image. Then, we calculate the mean diffusivity in a coregistered DWI image. We then also exclude from the segmentation white matter voxels that have MD > median (MD) + n_IQR * IQR (MD) Where IQR is the interquartile range. Note that per default we take a slightly more restrictive criterion (of 2 * IQR, instead of 1.5 * IQR), based on some empirical looking at data. Not so sure why there would be a difference. [Jeurissen2012]Jeurissen B, Leemans A, Tournier, J-D, Jones, DK and Sijbers J (2012). Investigating the prevalence of complex fiber configurations in white matter tissue with diffusion magnetic resonance imaging. Human Brain Mapping. doi: 10.1002/hbm.2209 """ dwi_nii, dwi_bvecs, dwi_bvals = make_data_set(dwi_path) # Resample the segmentation image to the DWI resolution: seg_resamp = ozv.resample_volume(seg_path, dwi_nii) # Find WM voxels (this is ITK gray coding 3=LHWM, 4=RHWM) wm_idx = np.where(np.logical_or(seg_resamp==3, seg_resamp==4)) vol = np.zeros(seg_resamp.shape) vol[wm_idx] = 1 # OK - now we need to find and exclude MD outliers: TM = ozm.TensorModel(dwi_nii, dwi_bvecs, dwi_bvals, mask=vol, params_file='temp') MD = TM.mean_diffusivity IQR = (stats.scoreatpercentile(MD[np.isfinite(MD)],75) - stats.scoreatpercentile(MD[np.isfinite(MD)],25)) cutoff = np.median(MD[np.isfinite(MD)]) + n_IQR * 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 make_wm_mask(seg_path, dwi_path, out_path=data_path + 'wm_mask.nii.gz', n_IQR=2): """ Function that makes a white-matter mask from an itk-gray class file and DWI data, at the resolution of the DWI data. Parameters ---------- seg_path : str Full path to an itk-gray segmentation dwi_path : str Full path to some DWI data (used to determine the mean diffusivity in each voxel, see below) out_path : str (optional) Where to put the resulting file n_IQR: float (optional) How many IQRs away for the definition of MD outliers (see below) Note ---- This follows [Jeurissen2012]_. First, we resample a segmentation based on a T1-weighted image. Then, we calculate the mean diffusivity in a coregistered DWI image. We then also exclude from the segmentation white matter voxels that have MD > median (MD) + n_IQR * IQR (MD) Where IQR is the interquartile range. Note that per default we take a slightly more restrictive criterion (of 2 * IQR, instead of 1.5 * IQR), based on some empirical looking at data. Not so sure why there would be a difference. [Jeurissen2012]Jeurissen B, Leemans A, Tournier, J-D, Jones, DK and Sijbers J (2012). Investigating the prevalence of complex fiber configurations in white matter tissue with diffusion magnetic resonance imaging. Human Brain Mapping. doi: 10.1002/hbm.2209 """ dwi_nii, dwi_bvecs, dwi_bvals = make_data_set(dwi_path) # Resample the segmentation image to the DWI resolution: seg_resamp = ozv.resample_volume(seg_path, dwi_nii) # Find WM voxels (this is ITK gray coding 3=LHWM, 4=RHWM) wm_idx = np.where(np.logical_or(seg_resamp == 3, seg_resamp == 4)) vol = np.zeros(seg_resamp.shape) vol[wm_idx] = 1 # OK - now we need to find and exclude MD outliers: TM = ozm.TensorModel(dwi_nii, dwi_bvecs, dwi_bvals, mask=vol, params_file='temp') MD = TM.mean_diffusivity IQR = (stats.scoreatpercentile(MD[np.isfinite(MD)], 75) - stats.scoreatpercentile(MD[np.isfinite(MD)], 25)) cutoff = np.median(MD[np.isfinite(MD)]) + n_IQR * 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)