Exemple #1
0
def connectivity_data():
    """Fixture for connectivity tests."""

    base_dir = os.path.abspath(pkg_resources.resource_filename(
        "pynets", "../data/examples"))
    func_file = f"{base_dir}/BIDS/sub-25659/ses-1/func/" \
                f"sub-25659_ses-1_task-rest_space-T1w_desc-preproc_bold.nii.gz"
    mask_file = f"{base_dir}/BIDS/sub-25659/ses-1/func/" \
                f"sub-25659_ses-1_task-rest_space-T1w_desc-preproc_" \
                f"bold_mask.nii.gz"
    parcellation = pkg_resources.resource_filename(
        "pynets", "templates/atlases/DesikanKlein2012.nii.gz"
    )

    masker = NiftiLabelsMasker(
        labels_img=nib.load(parcellation), background_label=0,
        resampling_target="labels", dtype="auto",
        mask_img=nib.load(mask_file), standardize=True)

    time_series = masker.fit_transform(func_file)
    conn_measure = ConnectivityMeasure(
        kind="correlation")
    conn_matrix = conn_measure.fit_transform([time_series])[0]
    [coords, _, _, label_intensities] = \
        get_names_and_coords_of_parcels(parcellation)

    labels = ['ROI_' + str(idx) for idx, val in enumerate(label_intensities)]

    yield {'time_series': time_series, 'conn_matrix': conn_matrix,
           'labels': labels, 'coords': coords, 'indices': label_intensities}
Exemple #2
0
def test_nodemaker_tools_parlistfile_WB():
    """
    Test nodemaker_tools_parlistfile_WB functionality
    """
    # Set example inputs
    base_dir = str(Path(__file__).parent / "examples")
    parlistfile = f"{base_dir}/miscellaneous/whole_brain_cluster_labels_PCA200.nii.gz"

    start_time = time.time()
    [WB_coords, _, _] = nodemaker.get_names_and_coords_of_parcels(parlistfile)
    print("%s%s%s" % (
        'get_names_and_coords_of_parcels (User-atlas 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_parcels_map_nifti,
     parcel_list_exp] = nodemaker.create_parcel_atlas(WB_parcel_list)
    print(
        "%s%s%s" %
        ('create_parcel_atlas (User-atlas whole-brain version) --> finished: ',
         str(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_parcels_map_nifti is not None
    assert parcel_list_exp is not None
Exemple #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")
    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
Exemple #4
0
def make_node_dict_from_parcellation(parcellation, dir_path, vox_size='2mm'):
    from pynets.core.nodemaker import get_names_and_coords_of_parcels, \
        parcel_naming
    from pynets.core.utils import save_coords_and_labels_to_json
    coords, _, _, label_intensities = \
        get_names_and_coords_of_parcels(parcellation)
    labels = parcel_naming(coords, vox_size)
    node_file = save_coords_and_labels_to_json(coords, labels,
                                               dir_path, network='regen',
                                               indices=label_intensities)
    return node_file
Exemple #5
0
def test_drop_coords_labels_from_restricted_parcellation():
    import tempfile
    from nipype.utils.filemanip import copyfile

    base_dir = str(Path(__file__).parent / "examples")
    parlistfile = f"{base_dir}/miscellaneous/whole_brain_cluster_labels_PCA200.nii.gz"

    [coords, _, _, label_intensities] = \
        nodemaker.get_names_and_coords_of_parcels(parlistfile)

    labels = np.arange(len(coords) +
                       1)[np.arange(len(coords) + 1) != 0].tolist()
    labs = list(zip(labels, label_intensities))
    [parcellation_okay, cleaned_coords, cleaned_labels] = \
        nodemaker.drop_coords_labels_from_restricted_parcellation(parlistfile,
                                                                  coords,
                                                                  labs)

    parcellation_okay_img = nib.load(parcellation_okay)
    intensities_ok = list(
        np.unique(np.asarray(parcellation_okay_img.dataobj).astype("int"))[1:])

    assert len(cleaned_coords) == len(cleaned_labels) == len(intensities_ok)

    parlist_img = nib.load(parlistfile)
    parlist_img_data = parlist_img.get_fdata()
    parlist_img_data[np.where(parlist_img_data == 10)] = 0
    par_tmp = tempfile.NamedTemporaryFile(mode='w+', suffix='.nii.gz')
    nib.save(nib.Nifti1Image(parlist_img_data, affine=parlist_img.affine),
             par_tmp.name)
    [parcellation_okay, cleaned_coords, cleaned_labels] = \
        nodemaker.drop_coords_labels_from_restricted_parcellation(parlistfile,
                                                                  coords,
                                                                  labs)
    parcellation_okay_img = nib.load(parcellation_okay)
    intensities_ok = list(
        np.unique(np.asarray(parcellation_okay_img.dataobj).astype("int"))[1:])

    assert len(cleaned_coords) == len(cleaned_labels) == len(intensities_ok)

    bad_coords = np.delete(coords, 30, axis=0)
    del labs[-30]
    par_tmp2 = tempfile.NamedTemporaryFile(mode='w+', suffix='.nii.gz').name
    copyfile(parlistfile, par_tmp2, copy=True, use_hardlink=False)
    [parcellation_mod, cleaned_coords, cleaned_labels] = \
        nodemaker.drop_coords_labels_from_restricted_parcellation(par_tmp2,
                                                                  bad_coords,
                                                                  labs)
    parcellation_mod_img = nib.load(parcellation_mod)
    intensities_ok = list(
        np.unique(np.asarray(parcellation_mod_img.dataobj).astype("int"))[1:])

    assert len(cleaned_coords) == len(cleaned_labels) == len(intensities_ok)
Exemple #6
0
def test_nodemaker_tools_parlistfile_RSN():
    """
    Test nodemaker_tools_parlistfile_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")
    dir_path = f"{base_dir}/BIDS/sub-25659/ses-1/func"
    parlistfile = f"{base_dir}/miscellaneous/whole_brain_cluster_labels_PCA200.nii.gz"
    network = 'Default'
    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_parcels_map_nifti,
     parcel_list_exp] = nodemaker.create_parcel_atlas(net_parcel_list)
    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,
                                             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 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
Exemple #7
0
def test_nodemaker_tools_parlistfile_RSN():
    """
    Test nodemaker_tools_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"
    network = 'Default'
    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_parcels_map_nifti,
     parcel_list_exp] = nodemaker.create_parcel_atlas(net_parcel_list)
    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,
                                             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 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
Exemple #8
0
def test_nodemaker_tools_parlistfile_WB():
    """
    Test nodemaker_tools_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)}"

    start_time = time.time()
    [WB_coords, _, _,
     _] = nodemaker.get_names_and_coords_of_parcels(parlistfile)
    print("%s%s%s" % (
        "get_names_and_coords_of_parcels (User-atlas "
        "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)
    [WB_parcels_map_nifti,
     parcel_list_exp] = nodemaker.create_parcel_atlas(WB_parcel_list)
    print("%s%s%s" % (
        "create_parcel_atlas (User-atlas whole-brain version) "
        "--> finished: ",
        str(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_parcels_map_nifti is not None
    assert parcel_list_exp is not None
Exemple #9
0
def test_streams2graph(fa_wei, dsn):
    from dipy.core.gradients import gradient_table
    from pynets.registration import register
    from pynets.core import nodemaker
    from dipy.io import save_pickle
    import os

    base_dir = str(Path(__file__).parent / "examples")
    dwi_file = f"{base_dir}/003/test_out/003/dwi/sub-003_dwi_reor-RAS_res-2mm.nii.gz"
    conn_model = 'csd'
    min_length = 10
    error_margin = 2
    directget = 'prob'
    track_type = 'particle'
    target_samples = 500
    overlap_thr = 1
    min_span_tree = True
    prune = 3
    norm = 6
    binary = False
    roi = f"{base_dir}/miscellaneous/pDMN_3_bin.nii.gz"
    network = 'Default'
    ID = '003'
    parc = True
    disp_filt = False
    node_size = None
    dens_thresh = False
    atlas = 'whole_brain_cluster_labels_PCA200'
    uatlas = f"{base_dir}/miscellaneous/whole_brain_cluster_labels_PCA200.nii.gz"
    t1_aligned_mni = f"{base_dir}/miscellaneous/whole_brain_cluster_labels_PCA200.nii.gz"
    atlas_dwi = f"{base_dir}/003/dmri/whole_brain_cluster_labels_PCA200_dwi_track.nii.gz"
    streams = f"{base_dir}/miscellaneous/003_streamlines_est-csd_nodetype-parc_samples-1000streams_tt-particle_" \
        f"dg-prob_ml-10.trk"
    B0_mask = f"{base_dir}/003/anat/mean_B0_bet_mask_tmp.nii.gz"
    dir_path = f"{base_dir}/003/dmri"
    bvals = f"{dir_path}/sub-003_dwi.bval"
    bvecs = f"{base_dir}/003/test_out/003/dwi/bvecs_reor.bvec"
    gtab_file = f"{base_dir}/gtab.pkl"
    gtab = gradient_table(bvals, bvecs)
    gtab.b0_threshold = 50
    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
    save_pickle(gtab_file, gtab)
    fa_path = tens_mod_fa_est(gtab_file, dwi_file, B0_mask)[0]

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

    # if dsn is True:
    #     os.makedirs(f"{dir_path}/dmri_reg/DSN", exist_ok=True)
    #     (streams_mni, dir_path, track_type, target_samples, conn_model, network, node_size, dens_thresh, ID, roi,
    #      min_span_tree, disp_filt, parc, prune, atlas, uatlas, labels, coords, norm, binary, atlas_mni, directget,
    #      warped_fa, min_length, error_margin) = register.direct_streamline_norm(streams, fa_path, fa_path, dir_path,
    #                                                                             track_type, target_samples, conn_model,
    #                                                                             network, node_size, dens_thresh, ID,
    #                                                                             roi, min_span_tree, disp_filt, parc,
    #                                                                             prune, atlas, atlas_dwi, uatlas,
    #                                                                             labels, coords, norm, binary, uatlas,
    #                                                                             dir_path, [0.1, 0.2], [40, 30],
    #                                                                             directget, min_length, t1_aligned_mni,
    #                                                                             error_margin)
    #
    #     conn_matrix = streams2graph(atlas_mni, streams_mni, overlap_thr, dir_path, track_type, target_samples,
    #                                 conn_model, network, node_size, dens_thresh, ID, roi, min_span_tree,
    #                                 disp_filt, parc, prune, atlas, uatlas, labels, coords, norm, binary,
    #                                 directget, warped_fa, error_margin, min_length, fa_wei)[2]
    # else:
    #     conn_matrix = streams2graph(atlas_dwi, streams, overlap_thr, dir_path, track_type, target_samples,
    #                                 conn_model, network, node_size, dens_thresh, ID, roi, min_span_tree,
    #                                 disp_filt, parc, prune, atlas, atlas_dwi, labels, coords, norm, binary,
    #                                 directget, fa_path, error_margin, min_length, fa_wei)[2]

    conn_matrix = streams2graph(
        atlas_dwi, streams, overlap_thr, dir_path, track_type, target_samples,
        conn_model, network, node_size, dens_thresh, ID, roi, min_span_tree,
        disp_filt, parc, prune, atlas, atlas_dwi, labels, coords, norm, binary,
        directget, fa_path, error_margin, min_length, fa_wei)[2]

    assert conn_matrix is not None
Exemple #10
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
Exemple #11
0
def test_nodemaker_tools_parlistfile_RSN():
    """
    Test nodemaker_tools_parlistfile_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)
    parlistfile = pkg_resources.resource_filename(
        "pynets", "templates/atlases/whole_brain_cluster_labels_PCA200.nii.gz")
    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)}"
    subnet = "Default"
    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_parcels_map_nifti,
     parcel_list_exp] = nodemaker.create_parcel_atlas(net_parcels_4d_img)
    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,
                                             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 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
Exemple #12
0
def test_drop_coords_labels_from_restricted_parcellation():
    from nipype.utils.filemanip import copyfile

    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)}"

    [coords, _, _, label_intensities] = \
        nodemaker.get_names_and_coords_of_parcels(parlistfile
    )

    labels = np.arange(len(coords) +
                       1)[np.arange(len(coords) + 1) != 0].tolist()
    labs = list(zip(labels, label_intensities))
    [
        parcellation_okay,
        cleaned_coords,
        cleaned_labels,
    ] = nodemaker.drop_coords_labels_from_restricted_parcellation(
        parlistfile, coords, labs)

    parcellation_okay_img = nib.load(parcellation_okay)
    intensities_ok = list(
        np.unique(np.asarray(parcellation_okay_img.dataobj).astype("int"))[1:])

    assert len(cleaned_coords) == len(cleaned_labels) == len(intensities_ok)

    parlist_img = nib.load(parlistfile)
    parlist_img_data = parlist_img.get_fdata()
    parlist_img_data[np.where(parlist_img_data == 10)] = 0
    par_tmp = tempfile.NamedTemporaryFile(mode="w+", suffix=".nii.gz")
    nib.save(nib.Nifti1Image(parlist_img_data, affine=parlist_img.affine),
             par_tmp.name)
    [
        parcellation_okay,
        cleaned_coords,
        cleaned_labels,
    ] = nodemaker.drop_coords_labels_from_restricted_parcellation(
        parlistfile, coords, labs)
    parcellation_okay_img = nib.load(parcellation_okay)
    intensities_ok = list(
        np.unique(np.asarray(parcellation_okay_img.dataobj).astype("int"))[1:])

    assert len(cleaned_coords) == len(cleaned_labels) == len(intensities_ok)

    bad_coords = np.delete(coords, 30, axis=0)
    del labs[-30]
    par_tmp2 = tempfile.NamedTemporaryFile(mode="w+", suffix=".nii.gz").name
    copyfile(parlistfile, par_tmp2, copy=True, use_hardlink=False)
    [
        parcellation_mod,
        cleaned_coords,
        cleaned_labels,
    ] = nodemaker.drop_coords_labels_from_restricted_parcellation(
        par_tmp2, bad_coords, labs)
    parcellation_mod_img = nib.load(parcellation_mod)
    intensities_ok = list(
        np.unique(np.asarray(parcellation_mod_img.dataobj).astype("int"))[1:])

    assert len(cleaned_coords) == len(cleaned_labels) == len(intensities_ok)
Exemple #13
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
Exemple #14
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()
Exemple #15
0
    def _run_interface(self, runtime):
        from pynets.core import utils, nodemaker
        from nipype.utils.filemanip import fname_presuffix, copyfile
        from nilearn.image import concat_imgs
        import pandas as pd
        import time
        import textwrap
        from pathlib import Path
        import os.path as op
        import glob

        base_path = utils.get_file()
        # Test if atlas is a nilearn atlas. If so, fetch coords, labels, and/or
        # networks.
        nilearn_parc_atlases = [
            "atlas_harvard_oxford",
            "atlas_aal",
            "atlas_destrieux_2009",
            "atlas_talairach_gyrus",
            "atlas_talairach_ba",
            "atlas_talairach_lobe",
        ]
        nilearn_coords_atlases = ["coords_power_2011", "coords_dosenbach_2010"]
        nilearn_prob_atlases = ["atlas_msdl", "atlas_pauli_2017"]
        local_atlases = [
            op.basename(i).split(".nii")[0]
            for i in glob.glob(f"{str(Path(base_path).parent.parent)}"
                               f"/templates/atlases/*.nii.gz")
            if "_4d" not in i
        ]

        if self.inputs.parcellation is None and self.inputs.atlas in \
                nilearn_parc_atlases:
            [labels, networks_list, parcellation
             ] = nodemaker.nilearn_atlas_helper(self.inputs.atlas,
                                                self.inputs.parc)
            if parcellation:
                if not isinstance(parcellation, str):
                    nib.save(
                        parcellation, f"{runtime.cwd}"
                        f"{self.inputs.atlas}{'.nii.gz'}")
                    parcellation = f"{runtime.cwd}" \
                                   f"{self.inputs.atlas}{'.nii.gz'}"
                if self.inputs.clustering is False:
                    [parcellation,
                     labels] = \
                        nodemaker.enforce_hem_distinct_consecutive_labels(
                        parcellation, label_names=labels)
                [coords, atlas, par_max, label_intensities] = \
                    nodemaker.get_names_and_coords_of_parcels(parcellation)
                if self.inputs.parc is True:
                    parcels_4d_img = nodemaker.three_to_four_parcellation(
                        parcellation)
                else:
                    parcels_4d_img = None
            else:
                raise FileNotFoundError(
                    f"\nAtlas file for {self.inputs.atlas} not found!")

            atlas = self.inputs.atlas
        elif (self.inputs.parcellation is None and self.inputs.parc is False
              and self.inputs.atlas in nilearn_coords_atlases):
            print("Fetching coords and labels from nilearn coordinate-based"
                  " atlas library...")
            # Fetch nilearn atlas coords
            [coords, _, networks_list,
             labels] = nodemaker.fetch_nilearn_atlas_coords(self.inputs.atlas)
            parcels_4d = None
            par_max = None
            atlas = self.inputs.atlas
            parcellation = None
            label_intensities = None
        elif (self.inputs.parcellation is None and self.inputs.parc is False
              and self.inputs.atlas in nilearn_prob_atlases):
            import matplotlib
            matplotlib.use("agg")
            from nilearn.plotting import find_probabilistic_atlas_cut_coords

            print("Fetching coords and labels from nilearn probabilistic atlas"
                  " library...")
            # Fetch nilearn atlas coords
            [labels, networks_list, parcellation
             ] = nodemaker.nilearn_atlas_helper(self.inputs.atlas,
                                                self.inputs.parc)
            coords = find_probabilistic_atlas_cut_coords(maps_img=parcellation)
            if parcellation:
                if not isinstance(parcellation, str):
                    nib.save(
                        parcellation, f"{runtime.cwd}"
                        f"{self.inputs.atlas}{'.nii.gz'}")
                    parcellation = f"{runtime.cwd}" \
                                   f"{self.inputs.atlas}{'.nii.gz'}"
                if self.inputs.clustering is False:
                    [parcellation,
                     labels] = \
                        nodemaker.enforce_hem_distinct_consecutive_labels(
                        parcellation, label_names=labels)
                if self.inputs.parc is True:
                    parcels_4d_img = nodemaker.three_to_four_parcellation(
                        parcellation)
                else:
                    parcels_4d_img = None
            else:
                raise FileNotFoundError(
                    f"\nAtlas file for {self.inputs.atlas} not found!")

            par_max = None
            atlas = self.inputs.atlas
            label_intensities = None
        elif self.inputs.parcellation is None and self.inputs.atlas in \
            local_atlases:
            parcellation_pre = (
                f"{str(Path(base_path).parent.parent)}/templates/atlases/"
                f"{self.inputs.atlas}.nii.gz")
            parcellation = fname_presuffix(parcellation_pre,
                                           newpath=runtime.cwd)
            copyfile(parcellation_pre,
                     parcellation,
                     copy=True,
                     use_hardlink=False)
            try:
                par_img = nib.load(parcellation)
            except indexed_gzip.ZranError as e:
                print(
                    e, "\nCannot load subnetwork reference image. "
                    "Do you have git-lfs installed?")
            try:
                if self.inputs.clustering is False:
                    [parcellation, _] = \
                        nodemaker.enforce_hem_distinct_consecutive_labels(
                            parcellation)

                # Fetch user-specified atlas coords
                [coords, _, par_max, label_intensities] = \
                    nodemaker.get_names_and_coords_of_parcels(parcellation)
                if self.inputs.parc is True:
                    parcels_4d_img = nodemaker.three_to_four_parcellation(
                        parcellation)
                else:
                    parcels_4d_img = None
                # Describe user atlas coords
                print(f"\n{self.inputs.atlas} comes with {par_max} parcels\n")
            except ValueError as e:
                print(
                    e, "Either you have specified the name of an atlas that "
                    "does not exist in the nilearn or local repository or "
                    "you have not supplied a 3d atlas parcellation image!")
            labels = None
            networks_list = None
            atlas = self.inputs.atlas
        elif self.inputs.parcellation:
            if self.inputs.clustering is True:
                while True:
                    if op.isfile(self.inputs.parcellation):
                        break
                    else:
                        print("Waiting for atlas file...")
                        time.sleep(5)

            try:
                parcellation_tmp_path = fname_presuffix(
                    self.inputs.parcellation, newpath=runtime.cwd)
                copyfile(self.inputs.parcellation,
                         parcellation_tmp_path,
                         copy=True,
                         use_hardlink=False)
                # Fetch user-specified atlas coords
                if self.inputs.clustering is False:
                    [parcellation,
                     _] = nodemaker.enforce_hem_distinct_consecutive_labels(
                         parcellation_tmp_path)
                else:
                    parcellation = parcellation_tmp_path
                [coords, atlas, par_max, label_intensities] = \
                    nodemaker.get_names_and_coords_of_parcels(parcellation)
                if self.inputs.parc is True:
                    parcels_4d_img = nodemaker.three_to_four_parcellation(
                        parcellation)
                else:
                    parcels_4d_img = None

                atlas = utils.prune_suffices(atlas)

                # Describe user atlas coords
                print(f"\n{atlas} comes with {par_max} parcels\n")
            except ValueError as e:
                print(
                    e, "Either you have specified the name of an atlas that "
                    "does not exist in the nilearn or local repository or "
                    "you have not supplied a 3d atlas parcellation image!")
            labels = None
            networks_list = None
        else:
            raise ValueError(
                "Either you have specified the name of an atlas that does"
                " not exist in the nilearn or local repository or you have"
                " not supplied a 3d atlas parcellation image!")

        # Labels prep
        if atlas and not labels:
            if (self.inputs.ref_txt is not None) and (op.exists(
                    self.inputs.ref_txt)):
                labels = pd.read_csv(self.inputs.ref_txt,
                                     sep=" ",
                                     header=None,
                                     names=["Index",
                                            "Region"])["Region"].tolist()
            else:
                if atlas in local_atlases:
                    ref_txt = (
                        f"{str(Path(base_path).parent.parent)}/templates/"
                        f"labels/"
                        f"{atlas}.txt")
                else:
                    ref_txt = self.inputs.ref_txt
                if ref_txt is not None:
                    try:
                        labels = pd.read_csv(ref_txt,
                                             sep=" ",
                                             header=None,
                                             names=["Index", "Region"
                                                    ])["Region"].tolist()
                    except BaseException:
                        if self.inputs.use_parcel_naming is True:
                            try:
                                labels = nodemaker.parcel_naming(
                                    coords, self.inputs.vox_size)
                            except BaseException:
                                print("AAL reference labeling failed!")
                                labels = np.arange(len(coords) + 1)[
                                    np.arange(len(coords) + 1) != 0].tolist()
                        else:
                            print("Using generic index labels...")
                            labels = np.arange(len(coords) +
                                               1)[np.arange(len(coords) +
                                                            1) != 0].tolist()
                else:
                    if self.inputs.use_parcel_naming is True:
                        try:
                            labels = nodemaker.parcel_naming(
                                coords, self.inputs.vox_size)
                        except BaseException:
                            print("AAL reference labeling failed!")
                            labels = np.arange(len(coords) +
                                               1)[np.arange(len(coords) +
                                                            1) != 0].tolist()
                    else:
                        print("Using generic index labels...")
                        labels = np.arange(len(coords) +
                                           1)[np.arange(len(coords) +
                                                        1) != 0].tolist()

        dir_path = utils.do_dir_path(atlas, self.inputs.outdir)

        if len(coords) != len(labels):
            labels = [
                i for i in labels if (i != 'Unknown' and i != 'Background')
            ]
            if len(coords) != len(labels):
                print("Length of coordinates is not equal to length of "
                      "label names...")
                if self.inputs.use_parcel_naming is True:
                    try:
                        print("Attempting consensus parcel naming instead...")
                        labels = nodemaker.parcel_naming(
                            coords, self.inputs.vox_size)
                    except BaseException:
                        print("Reverting to integer labels instead...")
                        labels = np.arange(len(coords) +
                                           1)[np.arange(len(coords) +
                                                        1) != 0].tolist()
                else:
                    print("Reverting to integer labels instead...")
                    labels = np.arange(len(coords) +
                                       1)[np.arange(len(coords) +
                                                    1) != 0].tolist()

        print(f"Coordinates:\n{coords}")
        print(f"Labels:\n"
              f"{textwrap.shorten(str(labels), width=1000, placeholder='...')}"
              f"")

        assert len(coords) == len(labels)

        if label_intensities is not None:
            self._results["labels"] = list(zip(labels, label_intensities))
        else:
            self._results["labels"] = labels
        self._results["coords"] = coords
        self._results["atlas"] = atlas
        self._results["networks_list"] = networks_list
        # TODO: Optimize this with 4d array concatenation and .npyz

        out_path = f"{runtime.cwd}/parcels_4d.nii.gz"
        nib.save(parcels_4d_img, out_path)
        self._results["parcels_4d"] = out_path
        self._results["par_max"] = par_max
        self._results["parcellation"] = parcellation
        self._results["dir_path"] = dir_path

        return runtime
Exemple #16
0
def fetch_nodes_and_labels(atlas, uatlas, ref_txt, parc, in_file, use_AAL_naming, clustering=False):
    """
    General API for fetching, identifying, and defining atlas nodes based on coordinates and/or labels.

    Parameters
    ----------
    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.
    ref_txt : str
        Path to an atlas reference .txt file that maps labels to intensities corresponding to uatlas.
    parc : bool
        Indicates whether to use parcels instead of coordinates as ROI nodes.
    in_file : str
        File path to Nifti1Image object whose affine will provide sampling reference for fetching.
    use_AAL_naming : bool
        Indicates whether to perform Automated-Anatomical Labeling of each coordinate from a list of a voxel
        coordinates.
    clustering : bool
        Indicates whether clustering was performed. Default is False.

    Returns
    -------
    labels : list
        List of string labels corresponding to ROI nodes.
    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.
    atlas : str
        Name of atlas parcellation (can differ slightly from fetch API string).
    networks_list : list
        List of RSN's and their associated cooordinates, if predefined uniquely for a given atlas.
    parcel_list : list
        List of 3D boolean numpy arrays or binarized Nifti1Images corresponding to ROI masks.
    par_max : int
        The maximum label intensity in the parcellation image.
    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 utils, nodemaker
    import pandas as pd
    import time
    from pathlib import Path
    import os.path as op

    base_path = utils.get_file()
    # Test if atlas is a nilearn atlas. If so, fetch coords, labels, and/or networks.
    nilearn_parc_atlases = ['atlas_harvard_oxford', 'atlas_aal', 'atlas_destrieux_2009', 'atlas_talairach_gyrus',
                            'atlas_talairach_ba', 'atlas_talairach_lobe']
    nilearn_coords_atlases = ['coords_power_2011', 'coords_dosenbach_2010']
    nilearn_prob_atlases = ['atlas_msdl', 'atlas_pauli_2017']
    if uatlas is None and atlas in nilearn_parc_atlases:
        [labels, networks_list, uatlas] = nodemaker.nilearn_atlas_helper(atlas, parc)
        if uatlas:
            if not isinstance(uatlas, str):
                nib.save(uatlas, "%s%s%s" % ('/tmp/', atlas, '.nii.gz'))
                uatlas = "%s%s%s" % ('/tmp/', atlas, '.nii.gz')
            [coords, _, par_max] = nodemaker.get_names_and_coords_of_parcels(uatlas)
            if parc is True:
                parcel_list = nodemaker.gen_img_list(uatlas)
            else:
                parcel_list = None
        else:
            raise ValueError("%s%s%s" % ('\nERROR: Atlas file for ', atlas, ' not found!'))
    elif uatlas is None and parc is False and atlas in nilearn_coords_atlases:
        print('Fetching coords and labels from nilearn coordinate-based atlas library...')
        # Fetch nilearn atlas coords
        [coords, _, networks_list, labels] = nodemaker.fetch_nilearn_atlas_coords(atlas)
        parcel_list = None
        par_max = None
    elif uatlas is None and parc is False and atlas in nilearn_prob_atlases:
        from nilearn.plotting import find_probabilistic_atlas_cut_coords
        print('Fetching coords and labels from nilearn probabilistic atlas library...')
        # Fetch nilearn atlas coords
        [labels, networks_list, uatlas] = nodemaker.nilearn_atlas_helper(atlas, parc)
        coords = find_probabilistic_atlas_cut_coords(maps_img=uatlas)
        if uatlas:
            if not isinstance(uatlas, str):
                nib.save(uatlas, "%s%s%s" % ('/tmp/', atlas, '.nii.gz'))
                uatlas = "%s%s%s" % ('/tmp/', atlas, '.nii.gz')
            if parc is True:
                parcel_list = nodemaker.gen_img_list(uatlas)
            else:
                parcel_list = None
        else:
            raise ValueError("%s%s%s" % ('\nERROR: Atlas file for ', atlas, ' not found!'))
        par_max = None
    elif uatlas:
        if clustering is True:
            while True:
                if op.isfile(uatlas):
                    break
                else:
                    print('Waiting for atlas file...')
                    time.sleep(15)
        atlas = uatlas.split('/')[-1].split('.')[0]
        try:
            # Fetch user-specified atlas coords
            [coords, atlas, par_max] = nodemaker.get_names_and_coords_of_parcels(uatlas)
            if parc is True:
                parcel_list = nodemaker.gen_img_list(uatlas)
            else:
                parcel_list = None
            # Describe user atlas coords
            print("%s%s%s%s" % ('\n', atlas, ' comes with {0} '.format(par_max), 'parcels\n'))
        except ValueError:
            print('\n\nError: Either you have specified the name of a nilearn atlas that does not exist or '
                  'you have not supplied a 3d atlas parcellation image!\n\n')
            parcel_list = None
            par_max = None
            coords = None
        labels = None
        networks_list = None
    else:
        networks_list = None
        labels = None
        parcel_list = None
        par_max = None
        coords = None

    # Labels prep
    if atlas:
        if labels:
            pass
        else:
            if (ref_txt is not None) and (op.exists(ref_txt)) and (use_AAL_naming is False):
                labels = pd.read_csv(ref_txt, sep=" ", header=None, names=["Index", "Region"])['Region'].tolist()
            else:
                try:
                    ref_txt = "%s%s%s%s" % (str(Path(base_path).parent), '/labelcharts/', atlas, '.txt')
                    if op.exists(ref_txt) and (use_AAL_naming is False):
                        try:
                            labels = pd.read_csv(ref_txt,
                                                 sep="\t", header=None, names=["Index", "Region"])['Region'].tolist()
                        except:
                            labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()
                    else:
                        if use_AAL_naming is True:
                            try:
                                labels = nodemaker.AAL_naming(coords)
                            except:
                                print('AAL reference labeling failed!')
                                labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()
                        else:
                            print('Using generic index labels...')
                            labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()
                except:
                    print("Label reference file not found. Attempting AAL naming...")
                    if use_AAL_naming is True:
                        try:
                            labels = nodemaker.AAL_naming(coords)
                        except:
                            print('AAL reference labeling failed!')
                            labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()
                    else:
                        print('Using generic index labels...')
                        labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()
    else:
        print('WARNING: No labels available since atlas name is not specified!')

    print("%s%s" % ('Labels:\n', labels))
    dir_path = utils.do_dir_path(atlas, in_file)

    if len(coords) != len(labels):
        labels = len(coords) * [np.nan]
        if len(coords) != len(labels):
            raise ValueError('ERROR: length of coordinates is not equal to length of label names')

    return labels, coords, atlas, networks_list, parcel_list, par_max, uatlas, dir_path