Exemplo n.º 1
0
def test_nodemaker_tools_masking_coords_WB(atlas):
    """
    Test nodemaker_tools_masking_coords_WB functionality
    """
    # Set example inputs
    roi = tempfile.NamedTemporaryFile(mode="w+", suffix=".nii.gz").name
    data_gen.generate_mni_space_img()[1].to_filename(roi)
    error = 2

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

    start_time = time.time()
    [WB_coords_masked,
     WB_labels_masked] = nodemaker.coords_masker(roi, WB_coords, WB_labels,
                                                 error)
    print("%s%s%s" % (
        "coords_masker (Masking whole-brain coords version) "
        "--> finished: ",
        str(np.round(time.time() - start_time, 1)),
        "s",
    ))

    assert WB_coords is not None
    assert WB_coords is not None
    assert WB_coords_masked is not None
    assert WB_labels is not None
    assert WB_labels_masked is not None
Exemplo n.º 2
0
def test_nodemaker_tools_masking_coords_WB(atlas):
    """
    Test nodemaker_tools_masking_coords_WB functionality
    """
    # Set example inputs
    base_dir = str(Path(__file__).parent / "examples")
    roi = f"{base_dir}/miscellaneous/pDMN_3_bin.nii.gz"
    error = 2

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

    start_time = time.time()
    [WB_coords_masked,
     WB_labels_masked] = nodemaker.coords_masker(roi, WB_coords, WB_labels,
                                                 error)
    print("%s%s%s" %
          ('coords_masker (Masking whole-brain coords version) --> finished: ',
           str(np.round(time.time() - start_time, 1)), 's'))

    assert WB_coords is not None
    assert WB_coords is not None
    assert WB_coords_masked is not None
    assert WB_labels is not None
    assert WB_labels_masked is not None
Exemplo n.º 3
0
def test_nodemaker_tools_masking_coords_RSN(atlas):
    """
    Test nodemaker_tools_masking_coords_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'
    roi = base_dir + '/pDMN_3_bin.nii.gz'
    network = 'Default'
    parc = False
    parcel_list = None
    error = 2
    start_time = time.time()
    [coords, _, _, labels] = nodemaker.fetch_nilearn_atlas_coords(atlas)
    print("%s%s%s" % ('fetch_nilearn_atlas_coords (Masking RSN version) --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

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

    start_time = time.time()
    [net_coords_masked, net_labels_masked] = nodemaker.coords_masker(roi, net_coords, net_labels, error)
    print("%s%s%s" % ('coords_masker (Masking RSN version) --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    assert coords is not None
    assert net_coords is not None
    assert net_coords_masked is not None
    assert net_labels is not None
    assert net_labels_masked is not None
    assert network is not None
Exemplo n.º 4
0
def test_nodemaker_tools_masking_coords_RSN(atlas):
    """
    Test nodemaker_tools_masking_coords_RSN functionality
    """
    # Set example inputs
    template = pkg_resources.resource_filename(
        "pynets", f"templates/standard/MNI152_T1_brain_2mm.nii.gz")
    dir_path = str(tempfile.TemporaryDirectory().name)
    os.makedirs(dir_path, exist_ok=True)
    roi = tempfile.NamedTemporaryFile(mode="w+", suffix=".nii.gz").name
    data_gen.generate_mni_space_img()[1].to_filename(roi)

    subnet = "Default"
    parc = False
    parcels_4d_img = None
    error = 2
    start_time = time.time()
    [coords, _, _, labels] = nodemaker.fetch_nilearn_atlas_coords(atlas)
    print("%s%s%s" % (
        "fetch_nilearn_atlas_coords (Masking subnet version) --> "
        "finished: ",
        str(np.round(time.time() - start_time, 1)),
        "s",
    ))

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

    start_time = time.time()
    [net_coords_masked,
     net_labels_masked] = nodemaker.coords_masker(roi, net_coords, net_labels,
                                                  error)
    print("%s%s%s" % (
        "coords_masker (Masking subnet version) --> finished: ",
        str(np.round(time.time() - start_time, 1)),
        "s",
    ))

    assert coords is not None
    assert net_coords is not None
    assert net_coords_masked is not None
    assert net_labels is not None
    assert net_labels_masked is not None
    assert subnet is not None
Exemplo n.º 5
0
def test_nodemaker_tools_masking_coords_RSN(atlas):
    """
    Test nodemaker_tools_masking_coords_RSN functionality
    """
    import pkg_resources
    # Set example inputs
    base_dir = str(Path(__file__).parent / "examples")
    template = pkg_resources.resource_filename(
        "pynets", f"templates/MNI152_T1_brain_2mm.nii.gz")
    roi = f"{base_dir}/miscellaneous/pDMN_3_bin.nii.gz"
    network = 'Default'
    parc = False
    parcel_list = None
    error = 2
    start_time = time.time()
    [coords, _, _, labels] = nodemaker.fetch_nilearn_atlas_coords(atlas)
    print("%s%s%s" %
          ('fetch_nilearn_atlas_coords (Masking RSN version) --> finished: ',
           str(np.round(time.time() - start_time, 1)), 's'))

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

    start_time = time.time()
    [net_coords_masked,
     net_labels_masked] = nodemaker.coords_masker(roi, net_coords, net_labels,
                                                  error)
    print("%s%s%s" % ('coords_masker (Masking RSN version) --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    assert coords is not None
    assert net_coords is not None
    assert net_coords_masked is not None
    assert net_labels is not None
    assert net_labels_masked is not None
    assert network is not None
Exemplo n.º 6
0
def test_nodemaker_tools_masking_coords_RSN(atlas):
    """
    Test nodemaker_tools_masking_coords_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"
    roi = f"{base_dir}/miscellaneous/pDMN_3_bin.nii.gz"
    network = 'Default'
    parc = False
    parcel_list = None
    error = 2
    start_time = time.time()
    [coords, _, _, labels] = nodemaker.fetch_nilearn_atlas_coords(atlas)
    print("%s%s%s" %
          ('fetch_nilearn_atlas_coords (Masking RSN version) --> finished: ',
           str(np.round(time.time() - start_time, 1)), 's'))

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

    start_time = time.time()
    [net_coords_masked,
     net_labels_masked] = nodemaker.coords_masker(roi, net_coords, net_labels,
                                                  error)
    print("%s%s%s" % ('coords_masker (Masking RSN version) --> finished: ',
                      str(np.round(time.time() - start_time, 1)), 's'))

    assert coords is not None
    assert net_coords is not None
    assert net_coords_masked is not None
    assert net_labels is not None
    assert net_labels_masked is not None
    assert network is not None
Exemplo n.º 7
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