Esempio n. 1
0
def test_nodemaker_tools_masking_parlistfile_RSN():
    """
    Test nodemaker_tools_masking_parlistfile_RSN functionality
    """
    # Set example inputs
    base_dir = str(Path(__file__).parent/"examples")
    dir_path = base_dir + '/002/fmri'
    func_file = dir_path + '/002.nii.gz'
    parlistfile = base_dir + '/whole_brain_cluster_labels_PCA200.nii.gz'
    roi = base_dir + '/pDMN_3_bin.nii.gz'
    network = 'Default'
    ID = '002'
    perc_overlap = 0.10
    parc = True

    start_time = time.time()
    [coords, _, _] = nodemaker.get_names_and_coords_of_parcels(parlistfile)
    print("%s%s%s" % ('get_names_and_coords_of_parcels --> finished: ',
    str(np.round(time.time() - start_time, 1)), 's'))

    labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()

    start_time = time.time()
    parcel_list = nodemaker.gen_img_list(parlistfile)
    [net_coords, net_parcel_list, net_labels, network] = nodemaker.get_node_membership(network, func_file, coords,
                                                                                            labels, parc,
                                                                                            parcel_list)
    print("%s%s%s" % ('get_node_membership --> finished: ', str(np.round(time.time() - start_time, 1)), 's'))

    start_time = time.time()
    [net_coords_masked, net_labels_masked, net_parcel_list_masked] = nodemaker.parcel_masker(roi, net_coords,
                                                                                                  net_parcel_list,
                                                                                                  net_labels,
                                                                                                  dir_path, ID,
                                                                                                  perc_overlap)
    print("%s%s%s" % ('parcel_masker --> finished: ', str(np.round(time.time() - start_time, 1)), 's'))

    start_time = time.time()
    [net_parcels_map_nifti, parcel_list_exp] = nodemaker.create_parcel_atlas(net_parcel_list_masked)
    print("%s%s%s" % ('create_parcel_atlas --> finished: ', str(np.round(time.time() - start_time, 1)), 's'))

    start_time = time.time()
    out_path = nodemaker.gen_network_parcels(parlistfile, network, net_labels_masked, dir_path)
    print("%s%s%s" % ('gen_network_parcels --> finished: ', str(np.round(time.time() - start_time, 1)), 's'))

    assert coords is not None
    assert net_coords is not None
    assert net_labels is not None
    assert net_parcel_list is not None
    assert net_coords_masked is not None
    assert net_labels_masked is not None
    assert net_parcel_list_masked is not None
    assert out_path is not None
    assert net_parcels_map_nifti is not None
    assert parcel_list_exp is not None
    assert network is not None
Esempio n. 2
0
def test_nodemaker_tools_masking_parlistfile_WB():
    """
    Test nodemaker_tools_masking_parlistfile_WB functionality
    """
    # Set example inputs
    base_dir = str(Path(__file__).parent / "examples")
    dir_path = f"{base_dir}/BIDS/sub-0025427/ses-1/func"
    parlistfile = f"{base_dir}/miscellaneous/whole_brain_cluster_labels_PCA200.nii.gz"
    atlas = 'whole_brain_cluster_labels_PCA200'
    roi = f"{base_dir}/miscellaneous/pDMN_3_bin.nii.gz"
    ID = '002'
    parc = True
    perc_overlap = 0.10

    start_time = time.time()
    [WB_coords, _, _] = nodemaker.get_names_and_coords_of_parcels(parlistfile)
    print("%s%s%s" % (
        'get_names_and_coords_of_parcels (Masking whole-brain version) --> finished: ',
        str(np.round(time.time() - start_time, 1)), 's'))

    WB_labels = np.arange(len(WB_coords) +
                          1)[np.arange(len(WB_coords) + 1) != 0].tolist()

    start_time = time.time()
    WB_parcel_list = nodemaker.gen_img_list(parlistfile)
    [_, _, WB_parcel_list_masked
     ] = nodemaker.parcel_masker(roi, WB_coords, WB_parcel_list, WB_labels,
                                 dir_path, ID, perc_overlap)
    print("%s%s%s" %
          ('parcel_masker (Masking whole-brain version) --> finished: ',
           np.round(time.time() - start_time, 1), 's'))

    start_time = time.time()
    [WB_parcels_map_nifti,
     parcel_list_exp] = nodemaker.create_parcel_atlas(WB_parcel_list_masked)
    print("%s%s%s" %
          ('create_parcel_atlas (Masking whole-brain version) --> finished: ',
           np.round(time.time() - start_time, 1), 's'))

    start_time = time.time()
    [WB_net_parcels_map_nifti_unmasked, WB_coords_unmasked, _, _, _,
     dir_path] = nodemaker.node_gen(WB_coords, WB_parcel_list, WB_labels,
                                    dir_path, ID, parc, atlas, parlistfile)
    print("%s%s%s" % ('node_gen (Masking whole-brain version) --> finished: ',
                      np.round(time.time() - start_time, 1), 's'))

    start_time = time.time()
    [
        WB_net_parcels_map_nifti_masked, WB_coords_masked, WB_labels_masked, _,
        _, _
    ] = nodemaker.node_gen_masking(roi, WB_coords, WB_parcel_list, WB_labels,
                                   dir_path, ID, parc, atlas, parlistfile)

    print("%s%s%s" %
          ('node_gen_masking (Masking whole-brain version) --> finished: ',
           np.round(time.time() - start_time, 1), 's'))

    assert WB_coords is not None
    assert WB_labels is not None
    assert WB_parcel_list is not None
    assert WB_coords_masked is not None
    assert WB_labels_masked is not None
    assert WB_parcel_list_masked is not None
    assert WB_parcels_map_nifti is not None
    assert parcel_list_exp is not None
    assert WB_net_parcels_map_nifti_unmasked is not None
    assert WB_coords_unmasked is not None
    assert WB_net_parcels_map_nifti_masked is not None
    assert WB_coords_masked is not None
Esempio n. 3
0
def test_nodemaker_tools_masking_parlistfile_RSN():
    """
    Test nodemaker_tools_masking_parlistfile_RSN functionality
    """
    # Set example inputs
    base_dir = str(Path(__file__).parent / "examples")
    func_file = f"{base_dir}/BIDS/sub-0025427/ses-1/func/sub-0025427_ses-1_task-rest_space-MNI152NLin2009cAsym_desc-smoothAROMAnonaggr_bold.nii.gz"
    dir_path = f"{base_dir}/BIDS/sub-0025427/ses-1/func"
    parlistfile = f"{base_dir}/miscellaneous/whole_brain_cluster_labels_PCA200.nii.gz"
    roi = f"{base_dir}/miscellaneous/pDMN_3_bin.nii.gz"
    network = 'Default'
    ID = '002'
    perc_overlap = 0.10
    parc = True

    start_time = time.time()
    [coords, _, _] = nodemaker.get_names_and_coords_of_parcels(parlistfile)
    print("%s%s%s" % ('get_names_and_coords_of_parcels --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    labels = np.arange(len(coords) +
                       1)[np.arange(len(coords) + 1) != 0].tolist()

    start_time = time.time()
    parcel_list = nodemaker.gen_img_list(parlistfile)
    [net_coords, net_parcel_list, net_labels,
     network] = nodemaker.get_node_membership(network, func_file, coords,
                                              labels, parc, parcel_list)
    print("%s%s%s" % ('get_node_membership --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    start_time = time.time()
    [net_coords_masked, net_labels_masked, net_parcel_list_masked
     ] = nodemaker.parcel_masker(roi, net_coords, net_parcel_list, net_labels,
                                 dir_path, ID, perc_overlap)
    print("%s%s%s" % ('parcel_masker --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    start_time = time.time()
    [net_parcels_map_nifti,
     parcel_list_exp] = nodemaker.create_parcel_atlas(net_parcel_list_masked)
    print("%s%s%s" % ('create_parcel_atlas --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    start_time = time.time()
    out_path = nodemaker.gen_network_parcels(parlistfile, network,
                                             net_labels_masked, dir_path)
    print("%s%s%s" % ('gen_network_parcels --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    assert coords is not None
    assert net_coords is not None
    assert net_labels is not None
    assert net_parcel_list is not None
    assert net_coords_masked is not None
    assert net_labels_masked is not None
    assert net_parcel_list_masked is not None
    assert out_path is not None
    assert net_parcels_map_nifti is not None
    assert parcel_list_exp is not None
    assert network is not None
Esempio n. 4
0
def test_nodemaker_tools_masking_parlistfile_RSN():
    """
    Test nodemaker_tools_masking_parlistfile_RSN functionality
    """
    # Set example inputs
    import pkg_resources
    base_dir = str(Path(__file__).parent / "examples")
    template = pkg_resources.resource_filename(
        "pynets", f"templates/MNI152_T1_brain_2mm.nii.gz")
    dir_path = f"{base_dir}/BIDS/sub-25659/ses-1/func"
    parlistfile = f"{base_dir}/miscellaneous/whole_brain_cluster_labels_PCA200.nii.gz"
    roi = f"{base_dir}/miscellaneous/pDMN_3_bin.nii.gz"
    network = 'Default'
    ID = '002'
    perc_overlap = 0.10
    parc = True

    start_time = time.time()
    [coords, _, _, _] = nodemaker.get_names_and_coords_of_parcels(parlistfile)
    print("%s%s%s" % ('get_names_and_coords_of_parcels --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    labels = np.arange(len(coords) +
                       1)[np.arange(len(coords) + 1) != 0].tolist()

    start_time = time.time()
    parcel_list = nodemaker.gen_img_list(parlistfile)
    [net_coords, net_parcel_list, net_labels,
     network] = nodemaker.get_node_membership(network, template, coords,
                                              labels, parc, parcel_list)
    print("%s%s%s" % ('get_node_membership --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    start_time = time.time()
    [net_coords_masked, net_labels_masked,
     net_parcel_list_masked] = nodemaker.parcel_masker(roi,
                                                       net_coords,
                                                       net_parcel_list,
                                                       net_labels,
                                                       dir_path,
                                                       ID,
                                                       perc_overlap,
                                                       vox_size='2mm')
    print("%s%s%s" % ('parcel_masker --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    start_time = time.time()
    [net_parcels_map_nifti,
     parcel_list_exp] = nodemaker.create_parcel_atlas(net_parcel_list_masked)
    print("%s%s%s" % ('create_parcel_atlas --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    start_time = time.time()
    out_path = nodemaker.gen_network_parcels(parlistfile, network,
                                             net_labels_masked, dir_path)
    print("%s%s%s" % ('gen_network_parcels --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    assert coords is not None
    assert net_coords is not None
    assert net_labels is not None
    assert net_parcel_list is not None
    assert net_coords_masked is not None
    assert net_labels_masked is not None
    assert net_parcel_list_masked is not None
    assert out_path is not None
    assert net_parcels_map_nifti is not None
    assert parcel_list_exp is not None
    assert network is not None
Esempio n. 5
0
def node_gen_masking(roi,
                     coords,
                     parcel_list,
                     labels,
                     dir_path,
                     ID,
                     parc,
                     atlas,
                     uatlas,
                     perc_overlap=0.75,
                     error=2):
    """
    In the case that masking was applied, this function generate nodes based on atlas definitions established by
    fetch_nodes_and_labels.

    Parameters
    ----------
    roi : str
        File path to binarized/boolean region-of-interest Nifti1Image file.
    coords : list
        List of (x, y, z) tuples in mm-space corresponding to a coordinate atlas used or
        which represent the center-of-mass of each parcellation node.
    parcel_list : list
        List of 3D boolean numpy arrays or binarized Nifti1Images corresponding to ROI masks.
    labels : list
        List of string labels corresponding to ROI nodes.
    dir_path : str
        Path to directory containing subject derivative data for given run.
    ID : str
        A subject id or other unique identifier.
    parc : bool
        Indicates whether to use parcels instead of coordinates as ROI nodes.
    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.
    uatlas : str
        File path to atlas parcellation Nifti1Image in MNI template space.
    perc_overlap : float
        Value 0-1 indicating a threshold of spatial overlap to use as a spatial error cushion in the case of
        evaluating mask/RSN membership from a given list of parcel masks. Default is 0.75.
    error : int
        Rounded euclidean distance, in units of voxel number, to use as a spatial error cushion in the case of
        evaluating mask/RSN membership from a given list of coordinates. Default is 4.

    Returns
    -------
    net_parcels_map_nifti : Nifti1Image
        A nibabel-based nifti image consisting of a 3D array with integer voxel intensities corresponding to ROI
        membership.
    coords : list
        List of (x, y, z) tuples in mm-space corresponding to a coordinate atlas used or
        which represent the center-of-mass of each parcellation node.
    labels : list
        List of string labels corresponding to ROI nodes.
    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.
    uatlas : str
        File path to atlas parcellation Nifti1Image in MNI template space.
    dir_path : str
        Path to directory containing subject derivative data for given run.
    """
    from pynets.core import nodemaker
    import os.path as op
    try:
        import cPickle as pickle
    except ImportError:
        import _pickle as pickle

    # Mask Parcels
    if parc is True:
        # For parcel masking, specify overlap thresh and error cushion in mm voxels
        [coords, labels, parcel_list_masked
         ] = nodemaker.parcel_masker(roi, coords, parcel_list, labels,
                                     dir_path, ID, perc_overlap)
        [net_parcels_map_nifti,
         _] = nodemaker.create_parcel_atlas(parcel_list_masked)
    # Mask Coordinates
    else:
        [coords, labels] = nodemaker.coords_masker(roi, coords, labels, error)
        # Save coords to pickle
        coords_path = f"{dir_path}/atlas_coords_{op.basename(roi).split('.')[0]}.pkl"
        with open(coords_path, 'wb') as f:
            pickle.dump(coords, f, protocol=2)

        net_parcels_map_nifti = None
    # Save labels to pickle
    labels_path = f"{dir_path}/atlas_labelnames_{op.basename(roi).split('.')[0]}.pkl"
    with open(labels_path, 'wb') as f:
        pickle.dump(labels, f, protocol=2)

    return net_parcels_map_nifti, coords, labels, atlas, uatlas, dir_path
Esempio n. 6
0
def test_nodemaker_tools_masking_parlistfile_WB():
    """
    Test nodemaker_tools_masking_parlistfile_WB functionality
    """
    # Set example inputs
    parlistfile = pkg_resources.resource_filename(
        "pynets", "templates/atlases/whole_brain_cluster_labels_PCA200.nii.gz")
    dir_path = str(tempfile.TemporaryDirectory().name)
    os.makedirs(dir_path, exist_ok=True)
    shutil.copy2(parlistfile, f"{dir_path}/{os.path.basename(parlistfile)}")
    parlistfile = f"{dir_path}/{os.path.basename(parlistfile)}"

    atlas = "whole_brain_cluster_labels_PCA200"
    roi = tempfile.NamedTemporaryFile(mode="w+", suffix=".nii.gz").name
    data_gen.generate_mni_space_img()[1].to_filename(roi)
    ID = "002"
    parc = True
    perc_overlap = 0.10

    start_time = time.time()
    [WB_coords, _, _,
     _] = nodemaker.get_names_and_coords_of_parcels(parlistfile)
    print("%s%s%s" % (
        "get_names_and_coords_of_parcels (Masking whole-brain "
        "version) --> finished: ",
        str(np.round(time.time() - start_time, 1)),
        "s",
    ))

    WB_labels = np.arange(len(WB_coords) +
                          1)[np.arange(len(WB_coords) + 1) != 0].tolist()

    start_time = time.time()

    WB_parcel_list = nodemaker.three_to_four_parcellation(parlistfile)

    start_time = time.time()
    [
        WB_net_parcels_map_nifti_unmasked,
        WB_coords_unmasked,
        _,
        _,
        _,
        dir_path,
    ] = nodemaker.node_gen(WB_coords, WB_parcel_list, WB_labels, dir_path, ID,
                           parc, atlas, parlistfile)
    print("%s%s%s" % (
        "node_gen (Masking whole-brain version) --> finished: ",
        np.round(time.time() - start_time, 1),
        "s",
    ))

    start_time = time.time()
    WB_parcel_list = nodemaker.three_to_four_parcellation(parlistfile)
    [WB_parcels_map_nifti,
     parcel_list_exp] = nodemaker.create_parcel_atlas(WB_parcel_list)
    print("%s%s%s" % (
        "create_parcel_atlas (Masking whole-brain version) --> finished: ",
        np.round(time.time() - start_time, 1),
        "s",
    ))

    start_time = time.time()
    WB_parcel_list = nodemaker.three_to_four_parcellation(parlistfile)
    [
        WB_net_parcels_map_nifti_masked,
        WB_coords_masked,
        WB_labels_masked,
        _,
        _,
        _,
    ] = nodemaker.node_gen_masking(
        roi,
        WB_coords,
        WB_parcel_list,
        WB_labels,
        dir_path,
        ID,
        parc,
        atlas,
        parlistfile,
        vox_size="2mm",
    )

    WB_parcel_list = nodemaker.three_to_four_parcellation(parlistfile)
    WB_parcel_list_masked = nodemaker.parcel_masker(
        roi,
        WB_coords,
        WB_parcel_list,
        WB_labels,
        dir_path,
        ID,
        perc_overlap,
        vox_size="2mm",
    )[2]
    print("%s%s%s" % (
        "parcel_masker (Masking whole-brain version) --> "
        "finished: ",
        np.round(time.time() - start_time, 1),
        "s",
    ))

    print("%s%s%s" % (
        "node_gen_masking (Masking whole-brain version) --> "
        "finished: ",
        np.round(time.time() - start_time, 1),
        "s",
    ))

    assert WB_coords is not None
    assert WB_labels is not None
    assert WB_parcel_list is not None
    assert WB_coords_masked is not None
    assert WB_labels_masked is not None
    assert WB_parcel_list_masked is not None
    assert WB_parcels_map_nifti is not None
    assert parcel_list_exp is not None
    assert WB_net_parcels_map_nifti_unmasked is not None
    assert WB_coords_unmasked is not None
    assert WB_net_parcels_map_nifti_masked is not None
    assert WB_coords_masked is not None
Esempio n. 7
0
def test_nodemaker_tools_masking_parlistfile_RSN():
    """
    Test nodemaker_tools_masking_parlistfile_RSN functionality
    """
    # Set example inputs

    template = pkg_resources.resource_filename(
        "pynets", f"templates/standard/MNI152_T1_brain_2mm.nii.gz")

    tmp = tempfile.TemporaryDirectory()
    dir_path = str(tmp.name)
    os.makedirs(dir_path, exist_ok=True)
    parlistfile = pkg_resources.resource_filename(
        "pynets", "templates/atlases/whole_brain_cluster_labels_PCA200.nii.gz")
    shutil.copy2(parlistfile, f"{dir_path}/{os.path.basename(parlistfile)}")
    parlistfile = f"{dir_path}/{os.path.basename(parlistfile)}"

    roi = tempfile.NamedTemporaryFile(mode="w+", suffix=".nii.gz").name
    data_gen.generate_mni_space_img()[1].to_filename(roi)

    subnet = "Default"
    ID = "002"
    perc_overlap = 0.75
    parc = True

    start_time = time.time()
    coords = nodemaker.get_names_and_coords_of_parcels(parlistfile)[0]
    print("%s%s%s" % (
        "get_names_and_coords_of_parcels --> finished: ",
        str(np.round(time.time() - start_time, 1)),
        "s",
    ))

    labels = np.arange(len(coords) +
                       1)[np.arange(len(coords) + 1) != 0].tolist()

    start_time = time.time()
    parcels_4d_img = nodemaker.three_to_four_parcellation(parlistfile)
    [net_coords, net_parcels_4d_img, net_labels, subnet] = \
        nodemaker.get_node_membership(
        subnet, template, coords, labels, parc, parcels_4d_img
    )
    print("%s%s%s" % (
        "get_node_membership --> finished: ",
        str(np.round(time.time() - start_time, 1)),
        "s",
    ))

    start_time = time.time()
    [
        net_coords_masked,
        net_labels_masked,
        net_parcels_4d_img_masked,
    ] = nodemaker.parcel_masker(
        roi,
        net_coords,
        net_parcels_4d_img,
        net_labels,
        dir_path,
        ID,
        perc_overlap,
        vox_size="2mm",
    )
    print("%s%s%s" % (
        "parcel_masker --> finished: ",
        str(np.round(time.time() - start_time, 1)),
        "s",
    ))

    start_time = time.time()
    [net_parcels_map_nifti, parcel_list_exp
     ] = nodemaker.create_parcel_atlas(net_parcels_4d_img_masked)
    print("%s%s%s" % (
        "create_parcel_atlas --> finished: ",
        str(np.round(time.time() - start_time, 1)),
        "s",
    ))

    start_time = time.time()
    out_path = nodemaker.gen_network_parcels(parlistfile, subnet,
                                             net_labels_masked, dir_path)
    print("%s%s%s" % (
        "gen_network_parcels --> finished: ",
        str(np.round(time.time() - start_time, 1)),
        "s",
    ))

    assert coords is not None
    assert net_coords is not None
    assert net_labels is not None
    assert net_parcels_4d_img is not None
    assert net_coords_masked is not None
    assert net_labels_masked is not None
    assert net_parcels_4d_img_masked is not None
    assert out_path is not None
    assert net_parcels_map_nifti is not None
    assert parcel_list_exp is not None
    assert subnet is not None
    tmp.cleanup()