def single_shell_extraction(dir_src, dir_out, verbose=False):

    fbval = pjoin(dir_src, 'bvals')
    fbvec = pjoin(dir_src, 'bvecs')
    fmask = pjoin(dir_src, 'nodif_brain_mask.nii.gz')
    fdwi = pjoin(dir_src, 'data.nii.gz')

    bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
    data, affine = load_nifti(fdwi, verbose)

    if par_b_shell == 1000:
        sind = (bvals < 10) | ((bvals < 1100) & (bvals > 900))
    elif par_b_shell == 2000:
        sind = (bvals < 10) | ((bvals < 2100) & (bvals > 1900))
    elif par_b_shell == 3000:
        sind = (bvals < 10) | ((bvals < 3100) & (bvals > 2900))

    shell_data = data[..., sind]
    shell_gtab = gradient_table(bvals[sind], bvecs[sind, :],
                                  b0_threshold=par_b0_threshold)

    fname = 'data_' + par_b_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, fname), shell_data, affine)
    np.savetxt(pjoin(dir_out, 'bvals_' + par_b_tag), shell_gtab.bvals)
    np.savetxt(pjoin(dir_out, 'bvecs_' + par_b_tag), shell_gtab.bvecs.T)
示例#2
0
def single_shell_extraction(dir_src, dir_out, verbose=False):

    fbval = pjoin(dir_src, 'bvals')
    fbvec = pjoin(dir_src, 'bvecs')
    fmask = pjoin(dir_src, 'nodif_brain_mask.nii.gz')
    fdwi = pjoin(dir_src, 'data.nii.gz')

    bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
    data, affine = load_nifti(fdwi, verbose)

    if par_b_shell == 1000:
        sind = (bvals < 10) | ((bvals < 1100) & (bvals > 900))
    elif par_b_shell == 2000:
        sind = (bvals < 10) | ((bvals < 2100) & (bvals > 1900))
    elif par_b_shell == 3000:
        sind = (bvals < 10) | ((bvals < 3100) & (bvals > 2900))

    shell_data = data[..., sind]
    shell_gtab = gradient_table(bvals[sind],
                                bvecs[sind, :],
                                b0_threshold=par_b0_threshold)

    fname = 'data_' + par_b_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, fname), shell_data, affine)
    np.savetxt(pjoin(dir_out, 'bvals_' + par_b_tag), shell_gtab.bvals)
    np.savetxt(pjoin(dir_out, 'bvecs_' + par_b_tag), shell_gtab.bvecs.T)
def constrained_spherical_deconvolution(dir_src, dir_out, verbose=False):

    # Load data
    fbval = pjoin(dir_src, 'bvals_' + par_b_tag)
    fbvec = pjoin(dir_src, 'bvecs_' + par_b_tag)
    fdwi =  pjoin(dir_src, 'data_' + par_b_tag + '_' + par_dim_tag + '.nii.gz')
    #fmask = pjoin(dir_src, 'nodif_brain_mask_' + par_dim_tag + '.nii.gz')
    fmask = pjoin(dir_src, 'wm_mask_' + par_b_tag + '_' + par_dim_tag + '.nii.gz')

    bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
    gtab = gradient_table(bvals, bvecs, b0_threshold=par_b0_threshold)
    data, affine = load_nifti(fdwi, verbose)
    mask, _ = load_nifti(fmask, verbose)

    sphere = get_sphere('symmetric724')

    response, ratio = auto_response(gtab, data, roi_radius=par_ar_radius, 
                                    fa_thr=par_ar_fa_th)
    # print('Response function', response)

    # Model fitting
    csd_model = ConstrainedSphericalDeconvModel(gtab, response)
    csd_fit = csd_model.fit(data, mask=mask)

    # Saving Spherical Harmonic Coefficient
    out_peaks = 'sh_' + par_b_tag + '_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, out_peaks), csd_fit.shm_coeff, affine)
示例#4
0
def constrained_spherical_deconvolution(dir_src, dir_out, verbose=False):

    # Load data
    fbval = pjoin(dir_src, 'bvals_' + par_b_tag)
    fbvec = pjoin(dir_src, 'bvecs_' + par_b_tag)
    fdwi = pjoin(dir_src, 'data_' + par_b_tag + '_' + par_dim_tag + '.nii.gz')
    #fmask = pjoin(dir_src, 'nodif_brain_mask_' + par_dim_tag + '.nii.gz')
    fmask = pjoin(dir_src,
                  'wm_mask_' + par_b_tag + '_' + par_dim_tag + '.nii.gz')

    bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
    gtab = gradient_table(bvals, bvecs, b0_threshold=par_b0_threshold)
    data, affine = load_nifti(fdwi, verbose)
    mask, _ = load_nifti(fmask, verbose)

    sphere = get_sphere('symmetric724')

    response, ratio = auto_response(gtab,
                                    data,
                                    roi_radius=par_ar_radius,
                                    fa_thr=par_ar_fa_th)
    # print('Response function', response)

    # Model fitting
    csd_model = ConstrainedSphericalDeconvModel(gtab, response)
    csd_fit = csd_model.fit(data, mask=mask)

    # Saving Spherical Harmonic Coefficient
    out_peaks = 'sh_' + par_b_tag + '_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, out_peaks), csd_fit.shm_coeff, affine)
def white_matter_mask_FA(dir_src, dir_out, verbose=False):

    src_fa =  'data_' + par_b_tag + '_' + par_dim_tag + '_FA.nii.gz'
    src_md =  'data_' + par_b_tag + '_' + par_dim_tag + '_MD.nii.gz'
    FA, affine = load_nifti(pjoin(dir_src, src_fa), verbose)
    MD, _ = load_nifti(pjoin(dir_src, src_md), verbose)

    wm_mask = (np.logical_or(FA >= 0.4, 
                             (np.logical_and(FA >= 0.15, MD >= 0.0011/2.))))

    out_wm = 'wm_mask_' + par_b_tag + '_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, out_wm), wm_mask.astype('f4'), affine)
示例#6
0
def white_matter_mask_FA(dir_src, dir_out, verbose=False):

    src_fa = 'data_' + par_b_tag + '_' + par_dim_tag + '_FA.nii.gz'
    src_md = 'data_' + par_b_tag + '_' + par_dim_tag + '_MD.nii.gz'
    FA, affine = load_nifti(pjoin(dir_src, src_fa), verbose)
    MD, _ = load_nifti(pjoin(dir_src, src_md), verbose)

    wm_mask = (np.logical_or(FA >= 0.4,
                             (np.logical_and(FA >= 0.15, MD >= 0.0011 / 2.))))

    out_wm = 'wm_mask_' + par_b_tag + '_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, out_wm), wm_mask.astype('f4'), affine)
def resample_data_resolution(dir_src, dir_out, verbose=False):
    
    fmask = pjoin(dir_src, 'nodif_brain_mask.nii.gz')
    fdwi = pjoin(dir_out, 'data_' + par_b_tag + '.nii.gz')
    
    data, affine = load_nifti(fdwi, verbose)
    mask, _ = load_nifti(fmask, verbose)

    data2, affine2 = reslice(data, affine, (1.25,) * 3, (par_dim_vox,) * 3)
    mask2, _ = reslice(mask, affine, (1.25,) * 3, (par_dim_vox,) * 3, order=0)

    fname = 'data_' + par_b_tag + '_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, fname), data2, affine2)
    fname = 'nodif_brain_mask_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, fname), mask2, affine2)
    
    fwmparc = pjoin(dir_src, '../wmparc.nii.gz')
    data, affine = load_nifti(fwmparc, verbose)
    data2, affine2 = reslice(data, affine, (0.7,) * 3, (par_dim_vox,) * 3, order=0)
    fname = 'wmparc_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, fname), data2, affine2)

    ft1w = pjoin(dir_src, '../T1w_acpc_dc_restore_brain.nii.gz')
    data, affine = load_nifti(ft1w, verbose)
    data2, affine2 = reslice(data, affine, (0.7,) * 3, (par_dim_vox,) * 3, order=0, mode='constant')
    fname = 't1w_acpc_dc_restore_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, fname), data2, affine2)
示例#8
0
def resample_data_resolution(dir_src, dir_out, verbose=False):

    fmask = pjoin(dir_src, 'nodif_brain_mask.nii.gz')
    fdwi = pjoin(dir_out, 'data_' + par_b_tag + '.nii.gz')

    data, affine = load_nifti(fdwi, verbose)
    mask, _ = load_nifti(fmask, verbose)

    data2, affine2 = reslice(data, affine, (1.25, ) * 3, (par_dim_vox, ) * 3)
    mask2, _ = reslice(mask,
                       affine, (1.25, ) * 3, (par_dim_vox, ) * 3,
                       order=0)

    fname = 'data_' + par_b_tag + '_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, fname), data2, affine2)
    fname = 'nodif_brain_mask_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, fname), mask2, affine2)

    fwmparc = pjoin(dir_src, '../wmparc.nii.gz')
    data, affine = load_nifti(fwmparc, verbose)
    data2, affine2 = reslice(data,
                             affine, (0.7, ) * 3, (par_dim_vox, ) * 3,
                             order=0)
    fname = 'wmparc_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, fname), data2, affine2)

    ft1w = pjoin(dir_src, '../T1w_acpc_dc_restore_brain.nii.gz')
    data, affine = load_nifti(ft1w, verbose)
    data2, affine2 = reslice(data,
                             affine, (0.7, ) * 3, (par_dim_vox, ) * 3,
                             order=0,
                             mode='constant')
    fname = 't1w_acpc_dc_restore_' + par_dim_tag + '.nii.gz'
    save_nifti(pjoin(dir_out, fname), data2, affine2)
def compute_tensor_model(dir_src, dir_out, verbose=False):

    fbval = pjoin(dir_src, 'bvals_' + par_b_tag)
    fbvec = pjoin(dir_src, 'bvecs_' + par_b_tag)
    fdwi =  pjoin(dir_src, 'data_' + par_b_tag + '_' + par_dim_tag + '.nii.gz')
    fmask = pjoin(dir_src, 'nodif_brain_mask_' + par_dim_tag + '.nii.gz')

    bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
    gtab = gradient_table(bvals, bvecs, b0_threshold=par_b0_threshold)
    data, affine = load_nifti(fdwi, verbose)
    mask, _ = load_nifti(fmask, verbose)

    ten_model = TensorModel(gtab)
    ten_fit = ten_model.fit(data, mask)

    FA = ten_fit.fa
    MD = ten_fit.md
    EV = ten_fit.evecs.astype(np.float32)

    fa_name = 'data_' + par_b_tag + '_' + par_dim_tag + '_FA.nii.gz'
    save_nifti(pjoin(dir_out, fa_name), FA, affine)
    md_name = 'data_' + par_b_tag + '_' + par_dim_tag + '_MD.nii.gz'
    save_nifti(pjoin(dir_out, md_name), MD, affine)
    ev_name = 'data_' + par_b_tag + '_' + par_dim_tag + '_EV.nii.gz'
    save_nifti(pjoin(dir_out, ev_name), EV, affine)
示例#10
0
def compute_tensor_model(dir_src, dir_out, verbose=False):

    fbval = pjoin(dir_src, 'bvals_' + par_b_tag)
    fbvec = pjoin(dir_src, 'bvecs_' + par_b_tag)
    fdwi = pjoin(dir_src, 'data_' + par_b_tag + '_' + par_dim_tag + '.nii.gz')
    fmask = pjoin(dir_src, 'nodif_brain_mask_' + par_dim_tag + '.nii.gz')

    bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
    gtab = gradient_table(bvals, bvecs, b0_threshold=par_b0_threshold)
    data, affine = load_nifti(fdwi, verbose)
    mask, _ = load_nifti(fmask, verbose)

    ten_model = TensorModel(gtab)
    ten_fit = ten_model.fit(data, mask)

    FA = ten_fit.fa
    MD = ten_fit.md
    EV = ten_fit.evecs.astype(np.float32)

    fa_name = 'data_' + par_b_tag + '_' + par_dim_tag + '_FA.nii.gz'
    save_nifti(pjoin(dir_out, fa_name), FA, affine)
    md_name = 'data_' + par_b_tag + '_' + par_dim_tag + '_MD.nii.gz'
    save_nifti(pjoin(dir_out, md_name), MD, affine)
    ev_name = 'data_' + par_b_tag + '_' + par_dim_tag + '_EV.nii.gz'
    save_nifti(pjoin(dir_out, ev_name), EV, affine)
def white_matter_mask_wmparc(dir_src, dir_out, verbose=False):

    fwmparc_src = pjoin(dir_src, 'wmparc.nii.gz')
    fribbon_src = pjoin(dir_src, 'ribbon.nii.gz')
    wmparc, affine = load_nifti(fwmparc_src)
    ribbon, affine = load_nifti(fribbon_src)
    mask = np.zeros_like(wmparc)

    for label in ribbon_structures:
        mask[ribbon == label] = 1

    for label in wmparc_structures + wmparc_cc_structures:
        mask[wmparc == label] = 1

    for label in wmparc_del_structures + wmparc_del_structures2:
        mask[wmparc == label] = 0

    mask = mask.astype('f8')
    mask2, affine2 = resample(mask, affine,
                              (0.7,) * 3, (par_dim_vox,) * 3, order=0)

    wm_out = "wm_mask_%s_%s_%s.nii.gz" % (par_b_tag, par_dim_tag, par_wmp_tag)
    save_nifti(pjoin(dir_out, wm_out), mask2, affine2)
示例#12
0
def white_matter_mask_wmparc(dir_src, dir_out, verbose=False):

    fwmparc_src = pjoin(dir_src, 'wmparc.nii.gz')
    fribbon_src = pjoin(dir_src, 'ribbon.nii.gz')
    wmparc, affine = load_nifti(fwmparc_src)
    ribbon, affine = load_nifti(fribbon_src)
    mask = np.zeros_like(wmparc)

    for label in ribbon_structures:
        mask[ribbon == label] = 1

    for label in wmparc_structures + wmparc_cc_structures:
        mask[wmparc == label] = 1

    for label in wmparc_del_structures + wmparc_del_structures2:
        mask[wmparc == label] = 0

    mask = mask.astype('f8')
    mask2, affine2 = resample(mask, affine,
                              (0.7,) * 3, (par_dim_vox,) * 3, order=0)

    wm_out = "wm_mask_%s_%s_%s.nii.gz" % (par_b_tag, par_dim_tag, par_wmp_tag)
    save_nifti(pjoin(dir_out, wm_out), mask2, affine2)