Beispiel #1
0
def test_normalize_grads():
    """
    Test make_gtab_and_bmask functionality
    """
    base_dir = str(Path(__file__).parent / "examples")
    fbval = f"{base_dir}/BIDS/sub-25659/ses-1/dwi/final_bval.bval"
    fbvec = f"{base_dir}/BIDS/sub-25659/ses-1/dwi/final_bvec.bvec"
    bvals = np.loadtxt(fbval)
    bvecs = np.loadtxt(fbvec)
    b0_threshold = 50
    bvecs_normed, bvals_normed = dmriutils.normalize_gradients(
        bvecs, bvals, b0_threshold, bvec_norm_epsilon=0.1, b_scale=True)
    assert bvecs_normed is not None
    assert bvals_normed is not None
Beispiel #2
0
def test_normalize_grads():
    """
    Test make_gtab_and_bmask functionality
    """
    base_dir = str(Path(__file__).parent / "examples")
    dwi_path = base_dir + '/002/dmri'
    fbval = dwi_path + '/bval.bval'
    fbvec = dwi_path + '/bvec.bvec'
    bvals = np.loadtxt(fbval)
    bvecs = np.loadtxt(fbvec)
    b0_threshold = 50
    bvecs_normed, bvals_normed = dmriutils.normalize_gradients(
        bvecs, bvals, b0_threshold, bvec_norm_epsilon=0.1, b_scale=True)
    assert bvecs_normed is not None
    assert bvals_normed is not None
Beispiel #3
0
def make_gtab_and_bmask(fbval,
                        fbvec,
                        dwi_file,
                        network,
                        node_size,
                        atlas,
                        b0_thr=50):
    """
    Create gradient table from bval/bvec, and a mean B0 brain mask.

    Parameters
    ----------
    fbval : str
        File name of the b-values file.
    fbvec : str
        File name of the b-vectors file.
    dwi_file : str
        File path to diffusion weighted image.
    network : str
        Resting-state network based on Yeo-7 and Yeo-17 naming (e.g. 'Default') used to filter nodes in the study of
        brain subgraphs.
    node_size : int
        Spherical centroid node size in the case that coordinate-based centroids
        are used as ROI's.
    atlas : str
        Name of a Nilearn-hosted coordinate or parcellation/label-based atlas supported for fetching.
        See Nilearn's datasets.atlas module for more detailed reference.

    Returns
    -------
    gtab_file : str
        File path to pickled DiPy gradient table object.
    nodif_b0_bet : str
        File path to mean brain-extracted B0 image.
    B0_mask : str
        File path to mean B0 brain mask.
    dwi_file : str
        File path to diffusion weighted image.
    """
    import os
    from dipy.io import save_pickle
    import os.path as op
    from dipy.io import read_bvals_bvecs
    from dipy.core.gradients import gradient_table
    from pynets.dmri.dmri_utils import make_mean_b0, normalize_gradients

    outdir = op.dirname(dwi_file)

    namer_dir = outdir + '/dmri_tmp'
    if not os.path.isdir(namer_dir):
        os.makedirs(namer_dir, exist_ok=True)

    B0_bet = "%s%s" % (namer_dir, "/mean_B0_bet.nii.gz")
    B0_mask = "%s%s" % (namer_dir, "/mean_B0_bet_mask.nii.gz")
    fbvec_norm = "%s%s" % (namer_dir, "/bvec_normed.bvec")
    fbval_norm = "%s%s" % (namer_dir, "/bval_normed.bvec")
    gtab_file = "%s%s" % (namer_dir, "/gtab.pkl")
    all_b0s_file = "%s%s" % (namer_dir, "/all_b0s.nii.gz")

    # loading bvecs/bvals
    bvals, bvecs = read_bvals_bvecs(fbval, fbvec)

    bvecs_norm, bvals_norm = normalize_gradients(bvecs,
                                                 bvals,
                                                 b0_threshold=b0_thr)

    # Save corrected
    np.savetxt(fbval_norm, bvals_norm)
    np.savetxt(fbvec_norm, bvecs_norm)

    # Creating the gradient table
    gtab = gradient_table(bvals_norm, bvecs_norm)

    # Correct b0 threshold
    gtab.b0_threshold = b0_thr

    # Get b0 indices
    b0s = np.where(gtab.bvals <= gtab.b0_threshold)[0]
    print("%s%s" % ('b0\'s found at: ', b0s))

    # Correct bvals to set 0's for B0 based on thresh
    gtab_bvals = gtab.bvals.copy()
    b0_thr_ixs = np.where(gtab_bvals < gtab.b0_threshold)[0]
    gtab_bvals[b0_thr_ixs] = 0
    gtab.b0s_mask = gtab_bvals == 0

    # Show info
    print(gtab.info)

    # Save gradient table to pickle
    save_pickle(gtab_file, gtab)

    # Extract and Combine all b0s collected, make mean b0
    print("Extracting b0's...")
    b0_vols = []
    dwi_img = nib.load(dwi_file)
    all_b0s_aff = dwi_img.affine.copy()
    dwi_data = np.asarray(dwi_img.dataobj)
    dwi_img.uncache()
    for b0 in b0s:
        print(b0)
        b0_vols.append(dwi_data[:, :, :, b0])

    all_b0s_aff[3][3] = len(b0_vols)
    nib.save(nib.Nifti1Image(np.stack(b0_vols, axis=3), affine=all_b0s_aff),
             all_b0s_file)
    mean_b0_file = make_mean_b0(all_b0s_file)

    # Create mean b0 brain mask
    cmd = 'bet ' + mean_b0_file + ' ' + B0_bet + ' -m -f 0.2'
    os.system(cmd)

    del dwi_data

    return gtab_file, B0_bet, B0_mask, dwi_file