Ejemplo n.º 1
0
def seg_setup(alg):
    MNI_T2_img = afd.read_mni_template()
    img = nib.load("dwi.nii")
    mapping = reg.read_mapping("mapping.nii.gz", img, MNI_T2_img)

    if alg == "waypoint":
        bundles = api.make_bundle_dict(
            bundle_names=[
                "CST",
                "ARC",
            ],
            resample_to=MNI_T2_img,
        )  # CST ARC
    else:
        bundles = api.make_bundle_dict(
            bundle_names=[
                "CST",
                "UF",
                "CC_ForcepsMajor",
                "CC_ForcepsMinor",
                "OR",
                "VOF",
            ],
            seg_algo="reco80",
            resample_to=MNI_T2_img,
        )  # CST ARC

    return {
        "MNI_T2_img": MNI_T2_img,
        "img": img,
        "mapping": mapping,
        "bundles": bundles,
    }
Ejemplo n.º 2
0
def test_AFQ_custom_bundle_dict():
    bids_path = create_dummy_bids_path(3, 1)
    bundle_dict = api.make_bundle_dict()
    my_afq = api.AFQ(
        bids_path,
        dmriprep="synthetic",
        bundle_info=bundle_dict)
Ejemplo n.º 3
0
def test_make_bundle_dict():
    """
    Tests bundle dict
    """

    # test defaults
    afq_bundles = api.make_bundle_dict()

    # bundles restricted within hemisphere
    # NOTE: FA and FP cross midline so are removed
    # NOTE: all others generate two bundles
    num_hemi_bundles = (len(api.BUNDLES) - 2) * 2

    # bundles that cross the midline
    num_whole_bundles = 2

    assert len(afq_bundles) == num_hemi_bundles + num_whole_bundles

    # Arcuate Fasciculus
    afq_bundles = api.make_bundle_dict(bundle_names=["ARC"])

    assert len(afq_bundles) == 2

    # Forceps Minor
    afq_bundles = api.make_bundle_dict(bundle_names=["FA"])

    assert len(afq_bundles) == 1

    # Cingulum Hippocampus
    # not included but exists in templates
    afq_bundles = api.make_bundle_dict(bundle_names=["HCC"])

    assert len(afq_bundles) == 2

    # Vertical Occipital Fasciculus
    # not included and does not exist in templates
    afq_bundles = api.make_bundle_dict(bundle_names=["VOF"])

    assert len(afq_bundles) == 0
Ejemplo n.º 4
0
    track = config["track"]

# load dwi data and generate gradient table
dwi_img = nib.load(dwi)
gtab = dpg.gradient_table(bvals, bvecs)

# load MNI template and syn register dwi data to MNI
MNI_T2_img = dpd.read_mni_template()
warped_hardi, mapping = reg.syn_register_dwi(dwi, gtab)

# load tractogram
tg = load_tractogram(track, dwi_img)
#tg_acpc = transform_streamlines(tg.streamlines,dwi_img.get_affine())

# download and load waypoint ROIs and make bundle dictionary
bundles = api.make_bundle_dict(resample_to=MNI_T2_img)
bundle_names = list(bundles.keys())

print(f"Space before segmentation: {tg.space}")

# initialize segmentation and segment major fiber groups
print("running AFQ segmentation")
segmentation = seg.Segmentation(return_idx=True)
segmentation.segment(bundles,
                     tg,
                     fdata=dwi,
                     fbval=bvals,
                     fbvec=bvecs,
                     mapping=mapping,
                     reg_template=MNI_T2_img,
                     reset_tg_space=True)
Ejemplo n.º 5
0
FA_data = FA_img.get_fdata()

print("Registering to template...")
MNI_T2_img = afd.read_mni_template()
if not op.exists('mapping.nii.gz'):
    import dipy.core.gradients as dpg
    gtab = dpg.gradient_table(hardi_fbval, hardi_fbvec)
    warped_hardi, mapping = reg.syn_register_dwi(hardi_fdata,
                                                 gtab,
                                                 template=MNI_T2_img)
    reg.write_mapping(mapping, './mapping.nii.gz')
else:
    mapping = reg.read_mapping('./mapping.nii.gz', img, MNI_T2_img)

bundle_names = ["CST", "UF", "CC_ForcepsMajor", "CC_ForcepsMinor"]
bundles = api.make_bundle_dict(bundle_names=bundle_names, seg_algo="reco")

print("Tracking...")
if not op.exists('dti_streamlines_reco.trk'):
    seed_roi = np.zeros(img.shape[:-1])
    for bundle in bundles:
        if bundle != 'whole_brain':
            sl_xform = dts.Streamlines(
                dtu.transform_tracking_output(bundles[bundle]['sl'],
                                              MNI_T2_img.affine))

            delta = dts.values_from_volume(mapping.backward, sl_xform,
                                           np.eye(4))
            sl_xform = [sum(d, s) for d, s in zip(delta, sl_xform)]

            sl_xform = dts.Streamlines(
Ejemplo n.º 6
0
    warped_hardi, mapping = reg.syn_register_dwi(hardi_fdata, gtab)
    reg.write_mapping(mapping, './mapping.nii.gz')
else:
    mapping = reg.read_mapping('./mapping.nii.gz', img, MNI_T2_img)


##########################################################################
# Read in bundle specification
# -------------------------------------------
# The waypoint ROIs, in addition to bundle probability maps are stored in this
# data structure. The templates are first resampled into the MNI space, before
# they are brought into the subject's individual native space.
# For speed, we only segment two bundles here.

bundles = api.make_bundle_dict(bundle_names= ["CST", "ARC"],
                               resample_to=MNI_T2_img)


##########################################################################
# Tracking
# --------
# Streamlines are generate using DTI and a deterministic tractography algorithm.
# For speed, we seed only within the waypoint ROIs for each bundle.

print("Tracking...")
if not op.exists('dti_streamlines.trk'):
    seed_roi = np.zeros(img.shape[:-1])
    for bundle in bundles:
        for idx, roi in enumerate(bundles[bundle]['ROIs']):
            if bundles[bundle]['rules'][idx]:
                warped_roi = patch_up_roi(