Esempio n. 1
0
def node_gen_masking(mask, coords, parcel_list, label_names, dir_path, ID,
                     parc):
    from pynets import nodemaker
    try:
        import cPickle as pickle
    except ImportError:
        import _pickle as pickle
    ##Mask Parcels
    if parc == True:
        [coords, label_names, parcel_list_masked
         ] = nodemaker.parcel_masker(mask, coords, parcel_list, label_names,
                                     dir_path, ID)
        [net_parcels_map_nifti,
         parcel_list_adj] = nodemaker.create_parcel_atlas(parcel_list_masked)
    ##Mask Coordinates
    elif parc == False:
        [coords, label_names] = nodemaker.coord_masker(mask, coords,
                                                       label_names)
        ##Save coords to pickle
        coord_path = "%s%s%s%s" % (dir_path, '/whole_brain_atlas_coords_',
                                   os.path.basename(mask).split('.')[0],
                                   '.pkl')
        with open(coord_path, 'wb') as f:
            pickle.dump(coords, f, protocol=2)
        net_parcels_map_nifti = None
    ##Save labels to pickle
    labels_path = "%s%s%s%s" % (dir_path, '/whole_brain_atlas_labelnames_',
                                os.path.basename(mask).split('.')[0], '.pkl')
    with open(labels_path, 'wb') as f:
        pickle.dump(label_names, f, protocol=2)
    return (net_parcels_map_nifti, coords, label_names)
Esempio n. 2
0
def test_nodemaker_tools_masking_coords_WB():
    # Set example inputs
    base_dir = str(Path(__file__).parent / "examples")
    #base_dir = '/Users/rxh180012/PyNets-development/tests/examples'
    dir_path = base_dir + '/997'
    mask = dir_path + '/pDMN_3_bin.nii.gz'
    atlas_select = 'coords_dosenbach_2010'
    error = 2

    start_time = time.time()
    [WB_coords, _, _,
     WB_label_names] = nodemaker.fetch_nilearn_atlas_coords(atlas_select)
    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_label_names_masked] = nodemaker.coord_masker(mask, WB_coords,
                                                     WB_label_names, error)
    print("%s%s%s" %
          ('coord_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_label_names is not None
    assert WB_label_names_masked is not None
Esempio n. 3
0
def test_nodemaker_tools_masking_coords_RSN():
    ##Set example inputs##
    base_dir = str(Path(__file__).parent / "examples")
    dir_path = base_dir + '/997'
    func_file = dir_path + '/sub-997_ses-01_task-REST_run-01_bold_space-MNI152NLin2009cAsym_preproc_masked.nii.gz'
    mask = dir_path + '/pDMN_3_bin.nii.gz'
    atlas_select = 'coords_dosenbach_2010'
    network = 'Default'
    parc = False
    parcel_list = None

    [coords, atlas_select, networks_list,
     label_names] = nodemaker.fetch_nilearn_atlas_coords(atlas_select)

    [net_coords, net_parcel_list, net_label_names,
     network] = nodemaker.get_node_membership(network, func_file, coords,
                                              label_names, parc, parcel_list)

    [net_coords_masked,
     net_label_names_masked] = nodemaker.coord_masker(mask, net_coords,
                                                      net_label_names)

    assert coords is not None
    assert net_coords is not None
    assert net_coords_masked is not None
    assert net_label_names is not None
    assert net_label_names_masked is not None
    assert network is not None
Esempio n. 4
0
def test_nodemaker_tools():
    ##Set example inputs##
    NETWORK = 'DMN'
    mask = Path(__file__).parent / "examples" / "997" / "pDMN_3_bin.nii.gz"
    parlistfile = Path(
        __file__
    ).parent / "examples" / "whole_brain_cluster_labels_PCA100.nii.gz"
    atlas_select = 'coords_power_2011'
    [coords, atlas_name, networks_list,
     label_names] = nodemaker.fetch_nilearn_atlas_coords(atlas_select)

    if atlas_name == 'Power 2011 atlas':
        network_coords_ref = NETWORK + '_coords.csv'
        atlas_coords = pkgutil.get_data("pynets",
                                        "rsnrefs/" + network_coords_ref)
        df = pd.read_csv(io.BytesIO(atlas_coords)).ix[:, 0:4]
        i = 1
        net_coords = []
        ix_labels = []
        for i in range(len(df)):
            x = int(df.ix[i, 1])
            y = int(df.ix[i, 2])
            z = int(df.ix[i, 3])
            net_coords.append((x, y, z))
            ix_labels.append(i)
            i = i + 1
            label_names = ix_labels

    if label_names != ix_labels:
        try:
            label_names = label_names.tolist()
        except:
            pass
        label_names = [label_names[i] for i in ix_labels]

    ##Test 1
    [net_coords,
     label_names_out] = nodemaker.coord_masker(str(mask), net_coords,
                                               label_names)
    ##Test 2
    [coords, atlas_name,
     par_max] = nodemaker.get_names_and_coords_of_parcels(str(parlistfile))
    ##Test 3
    #out_path = nodemaker.gen_network_parcels(str(parlistfile), NETWORK, labels_names)

    assert net_coords is not None
    assert label_names_out is not None
    assert mask is not None
    assert coords is not None
    assert atlas_name is not None
    assert par_max is not None
Esempio n. 5
0
def node_gen_masking(mask, coords, parcel_list, label_names, dir_path, ID,
                     parc, atlas_select, uatlas_select):
    from pynets import nodemaker
    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
        if 'bedpostX' in dir_path:
            perc_overlap = 0.01
        else:
            perc_overlap = 0.75
        [coords, label_names, parcel_list_masked
         ] = nodemaker.parcel_masker(mask, coords, parcel_list, label_names,
                                     dir_path, ID, perc_overlap)
        [net_parcels_map_nifti,
         _] = nodemaker.create_parcel_atlas(parcel_list_masked)
        vox_list = []
        for i in range(len(parcel_list)):
            vox_list.append(np.count_nonzero(parcel_list[i].get_data()))
        vox_array = np.array(vox_list).astype('float64')
    # Mask Coordinates
    else:
        if 'bedpostX' in dir_path:
            error = 60
        else:
            error = 2
        [coords, label_names] = nodemaker.coord_masker(mask, coords,
                                                       label_names, error)
        # Save coords to pickle
        coord_path = "%s%s%s%s" % (dir_path, '/atlas_coords_',
                                   os.path.basename(mask).split('.')[0],
                                   '.pkl')
        with open(coord_path, 'wb') as f:
            pickle.dump(coords, f, protocol=2)
        net_parcels_map_nifti = None
        vox_array = None
    # Save labels to pickle
    labels_path = "%s%s%s%s" % (dir_path, '/atlas_labelnames_',
                                os.path.basename(mask).split('.')[0], '.pkl')
    with open(labels_path, 'wb') as f:
        pickle.dump(label_names, f, protocol=2)

    return net_parcels_map_nifti, coords, label_names, atlas_select, uatlas_select, vox_array
Esempio n. 6
0
def test_nodemaker_tools_masking_coords_WB():
    ##Set example inputs##
    base_dir = str(Path(__file__).parent / "examples")
    dir_path = base_dir + '/997'
    mask = dir_path + '/pDMN_3_bin.nii.gz'
    atlas_select = 'coords_dosenbach_2010'

    [WB_coords, atlas_select, networks_list,
     WB_label_names] = nodemaker.fetch_nilearn_atlas_coords(atlas_select)

    [WB_coords_masked,
     WB_label_names_masked] = nodemaker.coord_masker(mask, WB_coords,
                                                     WB_label_names)

    assert WB_coords is not None
    assert WB_coords is not None
    assert WB_coords_masked is not None
    assert WB_label_names is not None
    assert WB_label_names_masked is not None
Esempio n. 7
0
def node_gen_masking(roi, coords, parcel_list, label_names, dir_path, ID, parc,
                     atlas_select, uatlas_select, mask):
    from pynets import nodemaker
    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
        if 'bedpostX' in dir_path:
            perc_overlap = 0.01
        else:
            perc_overlap = 0.75
        [coords, label_names, parcel_list_masked
         ] = nodemaker.parcel_masker(roi, coords, parcel_list, label_names,
                                     dir_path, ID, mask, perc_overlap)
        [net_parcels_map_nifti,
         _] = nodemaker.create_parcel_atlas(parcel_list_masked)
    # Mask Coordinates
    else:
        if 'bedpostX' in dir_path:
            error = 60
        else:
            error = 2
        [coords,
         label_names] = nodemaker.coord_masker(roi, coords, label_names, mask,
                                               error)
        # Save coords to pickle
        coord_path = "%s%s%s%s" % (dir_path, '/atlas_coords_',
                                   os.path.basename(roi).split('.')[0], '.pkl')
        with open(coord_path, 'wb') as f:
            pickle.dump(coords, f, protocol=2)
        net_parcels_map_nifti = None
    # Save labels to pickle
    labels_path = "%s%s%s%s" % (dir_path, '/atlas_labelnames_',
                                os.path.basename(roi).split('.')[0], '.pkl')
    with open(labels_path, 'wb') as f:
        pickle.dump(label_names, f, protocol=2)

    return net_parcels_map_nifti, coords, label_names, atlas_select, uatlas_select
Esempio n. 8
0
def test_nodemaker_tools_masking_coords_RSN():
    # Set example inputs
    base_dir = str(Path(__file__).parent / "examples")
    #base_dir = '/Users/rxh180012/PyNets-development/tests/examples'
    dir_path = base_dir + '/997'
    func_file = dir_path + '/sub-997_ses-01_task-REST_run-01_bold_space-MNI152NLin2009cAsym_preproc_masked.nii.gz'
    mask = dir_path + '/pDMN_3_bin.nii.gz'
    atlas_select = 'coords_dosenbach_2010'
    network = 'Default'
    parc = False
    parcel_list = None
    error = 2

    start_time = time.time()
    [coords, _, _,
     label_names] = nodemaker.fetch_nilearn_atlas_coords(atlas_select)
    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_label_names,
     network] = nodemaker.get_node_membership(network, func_file, coords,
                                              label_names, 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_label_names_masked] = nodemaker.coord_masker(mask, net_coords,
                                                      net_label_names, error)
    print("%s%s%s" % ('coord_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_label_names is not None
    assert net_label_names_masked is not None
    assert network is not None
Esempio n. 9
0
def wb_connectome_with_us_atlas_coords(input_file, ID, atlas_select, NETWORK,
                                       node_size, mask, thr, parlistfile,
                                       all_nets, conn_model, dens_thresh, conf,
                                       adapt_thresh, plot_switch,
                                       bedpostx_dir):
    nilearn_atlases = [
        'atlas_aal', 'atlas_craddock_2012', 'atlas_destrieux_2009'
    ]

    ##Input is nifti file
    func_file = input_file

    ##Test if atlas_select is a nilearn atlas
    if atlas_select in nilearn_atlases:
        try:
            parlistfile = getattr(datasets, 'fetch_%s' % atlas_select)().maps
            try:
                label_names = getattr(datasets,
                                      'fetch_%s' % atlas_select)().labels
            except:
                label_names = None
            try:
                networks_list = getattr(datasets,
                                        'fetch_%s' % atlas_select)().networks
            except:
                networks_list = None
        except:
            print(
                'PyNets is not ready for multi-scale atlases like BASC just yet!'
            )
            sys.exit()

    ##Fetch user-specified atlas coords
    [coords, atlas_name,
     par_max] = nodemaker.get_names_and_coords_of_parcels(parlistfile)
    atlas_select = atlas_name

    try:
        label_names
    except:

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

    ##Get subject directory path
    dir_path = os.path.dirname(
        os.path.realpath(func_file)) + '/' + atlas_select
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)

    ##Get coord membership dictionary if all_nets option triggered
    if all_nets != None:
        try:
            networks_list
        except:
            networks_list = None
        [membership,
         membership_plotting] = nodemaker.get_mem_dict(func_file, coords,
                                                       networks_list)

    ##Describe user atlas coords
    print('\n' + atlas_name + ' comes with {0} '.format(par_max) + 'parcels' +
          '\n')
    print('\n' + 'Stacked atlas coordinates in array of shape {0}.'.format(
        coords.shape) + '\n')

    ##Mask coordinates
    if mask is not None:
        [coords, label_names] = nodemaker.coord_masker(mask, coords,
                                                       label_names)

    ##Save coords and label_names to pickles
    coord_path = dir_path + '/coords_wb_' + str(thr) + '.pkl'
    with open(coord_path, 'wb') as f:
        pickle.dump(coords, f)

    labels_path = dir_path + '/labelnames_wb_' + str(thr) + '.pkl'
    with open(labels_path, 'wb') as f:
        pickle.dump(label_names, f)

    if bedpostx_dir is not None:
        from pynets.diffconnectometry import run_struct_mapping
        FSLDIR = os.environ['FSLDIR']
        try:
            FSLDIR
        except NameError:
            print('FSLDIR environment variable not set!')
        est_path2 = run_struct_mapping(FSLDIR, ID, bedpostx_dir, dir_path,
                                       NETWORK, coords, node_size)

    ##extract time series from whole brain parcellaions:
    parcellation = nib.load(parlistfile)
    parcel_masker = input_data.NiftiLabelsMasker(labels_img=parcellation,
                                                 background_label=0,
                                                 memory='nilearn_cache',
                                                 memory_level=5,
                                                 standardize=True)
    ts_within_parcels = parcel_masker.fit_transform(func_file, confounds=conf)
    print('\n' +
          'Time series has {0} samples'.format(ts_within_parcels.shape[0]) +
          '\n')

    ##Save time series as txt file
    out_path_ts = dir_path + '/' + ID + '_whole_brain_ts_within_parcels.txt'
    np.savetxt(out_path_ts, ts_within_parcels)

    ##Fit connectivity model
    if adapt_thresh is not False:
        if os.path.isfile(est_path2) == True:
            [conn_matrix, est_path, edge_threshold,
             thr] = thresholding.adaptive_thresholding(ts_within_parcels,
                                                       conn_model, NETWORK, ID,
                                                       est_path2, dir_path)
        else:
            print('No structural mx found! Exiting...')
            sys.exit(0)
    elif dens_thresh is None:
        edge_threshold = str(float(thr) * 100) + '%'
        [conn_matrix,
         est_path] = graphestimation.get_conn_matrix(ts_within_parcels,
                                                     conn_model, NETWORK, ID,
                                                     dir_path, thr)
        conn_matrix = thresholding.threshold_proportional(
            conn_matrix, float(thr), dir_path)
        conn_matrix = thresholding.normalize(conn_matrix)
    elif dens_thresh is not None:
        [conn_matrix, est_path, edge_threshold,
         thr] = thresholding.density_thresholding(ts_within_parcels,
                                                  conn_model, NETWORK, ID,
                                                  dens_thresh, dir_path)

    if plot_switch == True:
        ##Plot connectogram
        plotting.plot_connectogram(conn_matrix, conn_model, atlas_name,
                                   dir_path, ID, NETWORK, label_names)

        ##Plot adj. matrix based on determined inputs
        atlast_graph_title = plotting.plot_conn_mat(conn_matrix, conn_model,
                                                    atlas_name, dir_path, ID,
                                                    NETWORK, label_names, mask)

        ##Plot connectome viz for all Yeo networks
        if all_nets != False:
            plotting.plot_membership(membership_plotting, conn_matrix,
                                     conn_model, coords, edge_threshold,
                                     atlas_name, dir_path)
        else:
            out_path_fig = dir_path + '/' + ID + '_connectome_viz.png'
            niplot.plot_connectome(conn_matrix,
                                   coords,
                                   title=atlast_graph_title,
                                   edge_threshold=edge_threshold,
                                   node_size=20,
                                   colorbar=True,
                                   output_file=out_path_fig)
    return est_path, thr
Esempio n. 10
0
def network_connectome(input_file, ID, atlas_select, NETWORK, node_size, mask,
                       thr, parlistfile, all_nets, conn_model, dens_thresh,
                       conf, adapt_thresh, plot_switch, bedpostx_dir):
    nilearn_atlases = [
        'atlas_aal', 'atlas_craddock_2012', 'atlas_destrieux_2009'
    ]

    ##Input is nifti file
    func_file = input_file

    ##Test if atlas_select is a nilearn atlas
    if atlas_select in nilearn_atlases:
        atlas = getattr(datasets, 'fetch_%s' % atlas_select)()
        try:
            parlistfile = atlas.maps
            try:
                label_names = atlas.labels
            except:
                label_names = None
            try:
                networks_list = atlas.networks
            except:
                networks_list = None
        except RuntimeError:
            print('Error, atlas fetching failed.')
            sys.exit()

    if parlistfile == None and atlas_select not in nilearn_atlases:
        ##Fetch nilearn atlas coords
        [coords, atlas_name, networks_list,
         label_names] = nodemaker.fetch_nilearn_atlas_coords(atlas_select)

        if atlas_name == 'Power 2011 atlas':
            ##Reference RSN list
            import pkgutil
            import io
            network_coords_ref = NETWORK + '_coords.csv'
            atlas_coords = pkgutil.get_data("pynets",
                                            "rsnrefs/" + network_coords_ref)
            df = pd.read_csv(io.BytesIO(atlas_coords)).ix[:, 0:4]
            i = 1
            net_coords = []
            ix_labels = []
            for i in range(len(df)):
                #print("ROI Reference #: " + str(i))
                x = int(df.ix[i, 1])
                y = int(df.ix[i, 2])
                z = int(df.ix[i, 3])
                #print("X:" + str(x) + " Y:" + str(y) + " Z:" + str(z))
                net_coords.append((x, y, z))
                ix_labels.append(i)
                i = i + 1
                #print(net_coords)
                label_names = ix_labels
        elif atlas_name == 'Dosenbach 2010 atlas':
            coords = list(tuple(x) for x in coords)

            ##Get coord membership dictionary
            [membership, membership_plotting
             ] = nodemaker.get_mem_dict(func_file, coords, networks_list)

            ##Convert to membership dataframe
            mem_df = membership.to_frame().reset_index()

            nets_avail = list(set(list(mem_df['index'])))
            ##Get network name equivalents
            if NETWORK == 'DMN':
                NETWORK = 'default'
            elif NETWORK == 'FPTC':
                NETWORK = 'fronto-parietal'
            elif NETWORK == 'CON':
                NETWORK = 'cingulo-opercular'
            elif NETWORK not in nets_avail:
                print('Error: ' + NETWORK + ' not available with this atlas!')
                sys.exit()

            ##Get coords for network-of-interest
            mem_df.loc[mem_df['index'] == NETWORK]
            net_coords = mem_df.loc[mem_df['index'] == NETWORK][[0]].values[:,
                                                                            0]
            net_coords = list(tuple(x) for x in net_coords)
            ix_labels = mem_df.loc[mem_df['index'] == NETWORK].index.values
            ####Add code for any special RSN reference lists for the nilearn atlases here#####
            ##If labels_names are not indices and NETWORK is specified, sub-list label names

        if label_names != ix_labels:
            try:
                label_names = label_names.tolist()
            except:
                pass
            label_names = [label_names[i] for i in ix_labels]

        ##Get subject directory path
        dir_path = os.path.dirname(
            os.path.realpath(func_file)) + '/' + atlas_select
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        ##If masking, remove those coords that fall outside of the mask
        if mask != None:
            [net_coords,
             label_names] = nodemaker.coord_masker(mask, net_coords,
                                                   label_names)

        ##Save coords and label_names to pickles
        coord_path = dir_path + '/coords_' + NETWORK + '_' + str(thr) + '.pkl'
        with open(coord_path, 'wb') as f:
            pickle.dump(net_coords, f)

        labels_path = dir_path + '/labelnames_' + NETWORK + '_' + str(
            thr) + '.pkl'
        with open(labels_path, 'wb') as f:
            pickle.dump(label_names, f)

        if bedpostx_dir is not None:
            from pynets.diffconnectometry import run_struct_mapping
            FSLDIR = os.environ['FSLDIR']
            try:
                FSLDIR
            except NameError:
                print('FSLDIR environment variable not set!')
            est_path2 = run_struct_mapping(FSLDIR, ID, bedpostx_dir, dir_path,
                                           NETWORK, net_coords, node_size)

    else:
        ##Fetch user-specified atlas coords
        [coords_all, atlas_name,
         par_max] = nodemaker.get_names_and_coords_of_parcels(parlistfile)
        coords = list(tuple(x) for x in coords_all)

        ##Get subject directory path
        dir_path = os.path.dirname(
            os.path.realpath(func_file)) + '/' + atlas_name
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        ##Get coord membership dictionary
        try:
            networks_list
        except:
            networks_list = None
        [membership,
         membership_plotting] = nodemaker.get_mem_dict(func_file, coords,
                                                       networks_list)

        ##Convert to membership dataframe
        mem_df = membership.to_frame().reset_index()

        ##Get coords for network-of-interest
        mem_df.loc[mem_df['index'] == NETWORK]
        net_coords = mem_df.loc[mem_df['index'] == NETWORK][[0]].values[:, 0]
        net_coords = list(tuple(x) for x in net_coords)
        ix_labels = mem_df.loc[mem_df['index'] == NETWORK].index.values
        try:
            label_names = [label_names[i] for i in ix_labels]
        except:
            label_names = ix_labels

        if mask != None:
            [net_coords,
             label_names] = nodemaker.coord_masker(mask, net_coords,
                                                   label_names)

        ##Save coords and label_names to pickles
        coord_path = dir_path + '/coords_' + NETWORK + '_' + str(thr) + '.pkl'
        with open(coord_path, 'wb') as f:
            pickle.dump(net_coords, f)

        labels_path = dir_path + '/labelnames_' + NETWORK + '_' + str(
            thr) + '.pkl'
        with open(labels_path, 'wb') as f:
            pickle.dump(label_names, f)

        if bedpostx_dir is not None:
            from pynets.diffconnectometry import run_struct_mapping
            est_path2 = run_struct_mapping(FSLDIR, ID, bedpostx_dir, dir_path,
                                           NETWORK, net_coords, node_size)

        ##Generate network parcels image (through refinement, this could be used
        ##in place of the 3 lines above)
        #net_parcels_img_path = gen_network_parcels(parlistfile, NETWORK, labels)
        #parcellation = nib.load(net_parcels_img_path)
        #parcel_masker = input_data.NiftiLabelsMasker(labels_img=parcellation, background_label=0, memory='nilearn_cache', memory_level=5, standardize=True)
        #ts_within_parcels = parcel_masker.fit_transform(func_file)
        #net_ts = ts_within_parcels

    ##Grow ROIs
    masker = input_data.NiftiSpheresMasker(seeds=net_coords,
                                           radius=float(node_size),
                                           allow_overlap=True,
                                           memory_level=5,
                                           memory='nilearn_cache',
                                           verbose=2,
                                           standardize=True)
    ts_within_spheres = masker.fit_transform(func_file, confounds=conf)
    net_ts = ts_within_spheres

    ##Save time series as txt file
    out_path_ts = dir_path + '/' + ID + '_' + NETWORK + '_net_ts.txt'
    np.savetxt(out_path_ts, net_ts)

    ##Fit connectivity model
    if adapt_thresh is not False:
        if os.path.isfile(est_path2) == True:
            [conn_matrix, est_path, edge_threshold,
             thr] = thresholding.adaptive_thresholding(ts_within_spheres,
                                                       conn_model, NETWORK, ID,
                                                       est_path2, dir_path)
        else:
            print('No structural mx found! Exiting...')
            sys.exit(0)
    elif dens_thresh is None:
        edge_threshold = str(float(thr) * 100) + '%'
        [conn_matrix,
         est_path] = graphestimation.get_conn_matrix(ts_within_spheres,
                                                     conn_model, NETWORK, ID,
                                                     dir_path, thr)
        conn_matrix = thresholding.threshold_proportional(
            conn_matrix, float(thr), dir_path)
        conn_matrix = thresholding.normalize(conn_matrix)
    elif dens_thresh is not None:
        [conn_matrix, est_path, edge_threshold,
         thr] = thresholding.density_thresholding(ts_within_spheres,
                                                  conn_model, NETWORK, ID,
                                                  dens_thresh, dir_path)

    if plot_switch == True:
        ##Plot connectogram
        plotting.plot_connectogram(conn_matrix, conn_model, atlas_name,
                                   dir_path, ID, NETWORK, label_names)

        ##Plot adj. matrix based on determined inputs
        plotting.plot_conn_mat(conn_matrix, conn_model, atlas_name, dir_path,
                               ID, NETWORK, label_names, mask)

        ##Plot network time-series
        plotting.plot_timeseries(net_ts, NETWORK, ID, dir_path, atlas_name,
                                 label_names)

        ##Plot connectome viz for specific Yeo networks
        title = "Connectivity Projected on the " + NETWORK
        out_path_fig = dir_path + '/' + ID + '_' + NETWORK + '_connectome_plot.png'
        niplot.plot_connectome(conn_matrix,
                               net_coords,
                               edge_threshold=edge_threshold,
                               title=title,
                               display_mode='lyrz',
                               output_file=out_path_fig)
    return est_path, thr
Esempio n. 11
0
def wb_connectome_with_nl_atlas_coords(input_file, ID, atlas_select, NETWORK,
                                       node_size, mask, thr, all_nets,
                                       conn_model, dens_thresh, conf,
                                       adapt_thresh, plot_switch,
                                       bedpostx_dir):
    nilearn_atlases = [
        'atlas_aal', 'atlas_craddock_2012', 'atlas_destrieux_2009'
    ]

    ##Input is nifti file
    func_file = input_file

    ##Fetch nilearn atlas coords
    [coords, atlas_name, networks_list,
     label_names] = nodemaker.fetch_nilearn_atlas_coords(atlas_select)

    ##Get subject directory path
    dir_path = os.path.dirname(
        os.path.realpath(func_file)) + '/' + atlas_select
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)

    ##Get coord membership dictionary if all_nets option triggered
    if all_nets != False:
        try:
            networks_list
        except:
            networks_list = None
        [membership,
         membership_plotting] = nodemaker.get_mem_dict(func_file, coords,
                                                       networks_list)

    ##Mask coordinates
    if mask is not None:
        [coords, label_names] = nodemaker.coord_masker(mask, coords,
                                                       label_names)

    ##Save coords and label_names to pickles
    coord_path = dir_path + '/coords_wb_' + str(thr) + '.pkl'
    with open(coord_path, 'wb') as f:
        pickle.dump(coords, f)

    labels_path = dir_path + '/labelnames_wb_' + str(thr) + '.pkl'
    with open(labels_path, 'wb') as f:
        pickle.dump(label_names, f)

    if bedpostx_dir is not None:
        from pynets.diffconnectometry import run_struct_mapping
        FSLDIR = os.environ['FSLDIR']
        try:
            FSLDIR
        except NameError:
            print('FSLDIR environment variable not set!')
        est_path2 = run_struct_mapping(FSLDIR, ID, bedpostx_dir, dir_path,
                                       NETWORK, coords, node_size)

    ##Extract within-spheres time-series from funct file
    spheres_masker = input_data.NiftiSpheresMasker(seeds=coords,
                                                   radius=float(node_size),
                                                   memory='nilearn_cache',
                                                   memory_level=5,
                                                   verbose=2,
                                                   standardize=True)
    ts_within_spheres = spheres_masker.fit_transform(func_file, confounds=conf)
    print('\n' +
          'Time series has {0} samples'.format(ts_within_spheres.shape[0]) +
          '\n')

    ##Save time series as txt file
    out_path_ts = dir_path + '/' + ID + '_whole_brain_ts_within_spheres.txt'
    np.savetxt(out_path_ts, ts_within_spheres)

    ##Fit connectivity model
    if adapt_thresh is not False:
        if os.path.isfile(est_path2) == True:
            [conn_matrix, est_path, edge_threshold,
             thr] = thresholding.adaptive_thresholding(ts_within_spheres,
                                                       conn_model, NETWORK, ID,
                                                       est_path2, dir_path)
        else:
            print('No structural mx found! Exiting...')
            sys.exit(0)
    elif dens_thresh is None:
        edge_threshold = str(float(thr) * 100) + '%'
        [conn_matrix,
         est_path] = graphestimation.get_conn_matrix(ts_within_spheres,
                                                     conn_model, NETWORK, ID,
                                                     dir_path, thr)
        conn_matrix = thresholding.threshold_proportional(
            conn_matrix, float(thr), dir_path)
        conn_matrix = thresholding.normalize(conn_matrix)
    elif dens_thresh is not None:
        [conn_matrix, est_path, edge_threshold,
         thr] = thresholding.density_thresholding(ts_within_spheres,
                                                  conn_model, NETWORK, ID,
                                                  dens_thresh, dir_path)

    if plot_switch == True:
        ##Plot connectogram
        plotting.plot_connectogram(conn_matrix, conn_model, atlas_name,
                                   dir_path, ID, NETWORK, label_names)

        ##Plot adj. matrix based on determined inputs
        plotting.plot_conn_mat(conn_matrix, conn_model, atlas_name, dir_path,
                               ID, NETWORK, label_names, mask)

        ##Plot connectome viz for all Yeo networks
        if all_nets != False:
            plotting.plot_membership(membership_plotting, conn_matrix,
                                     conn_model, coords, edge_threshold,
                                     atlas_name, dir_path)
        else:
            out_path_fig = dir_path + '/' + ID + '_' + atlas_name + '_connectome_viz.png'
            niplot.plot_connectome(conn_matrix,
                                   coords,
                                   title=atlas_name,
                                   edge_threshold=edge_threshold,
                                   node_size=20,
                                   colorbar=True,
                                   output_file=out_path_fig)
    return est_path, thr