def thresh_diff(dens_thresh, thr, conn_model, network, ID, dir_path, mask, node_size, conn_matrix, parc): from pynets import utils, thresholding thr_perc = 100 * float(thr) edge_threshold = "%s%s" % (str(thr_perc), '%') if parc is True: node_size = 'parc' if dens_thresh is False: print("%s%.2f%s" % ('\nThresholding proportionally at: ', thr_perc, '% ...\n')) else: print("%s%.2f%s" % ('\nThresholding to achieve density of: ', thr_perc, '% ...\n')) if dens_thresh is False: conn_matrix_thr = thresholding.threshold_proportional( conn_matrix, float(thr)) else: conn_matrix_thr = thresholding.density_thresholding( conn_matrix, float(thr)) # Save thresholded mat est_path = utils.create_est_path(ID, network, conn_model, thr, mask, dir_path, node_size) np.save(est_path, conn_matrix_thr) return conn_matrix_thr, edge_threshold, est_path, thr, node_size, network
def thresh_diff(dens_thresh, thr, conn_model, network, ID, dir_path, mask, node_size, conn_matrix): from pynets import utils, thresholding if not dens_thresh: print("%s%.2f%s" % ('\nThresholding proportionally at: ', 100 * float(thr), '% ...\n')) else: print("%s%.2f%s" % ('\nThresholding to achieve density of: ', 100 * float(thr), '% ...\n')) if dens_thresh == False: ##Save thresholded conn_matrix_thr = thresholding.threshold_proportional( conn_matrix, float(thr)) edge_threshold = str(float(thr) * 100) + '%' est_path = utils.create_est_path(ID, network, conn_model, thr, mask, dir_path, node_size) else: conn_matrix_thr = thresholding.density_thresholding( conn_matrix, float(thr)) edge_threshold = str(float(thr) * 100) + '%' est_path = utils.create_est_path(ID, network, conn_model, thr, mask, dir_path, node_size) np.save(est_path, conn_matrix_thr) return (conn_matrix_thr, edge_threshold, est_path, thr, node_size, network)
def thresh_and_fit(dens_thresh, thr, ts_within_nodes, conn_model, network, ID, dir_path, mask, node_size): from pynets import utils, thresholding, graphestimation thr_perc = 100 * float(thr) edge_threshold = "%s%s" % (str(thr_perc), '%') if not dens_thresh: print("%s%.2f%s" % ('\nThresholding proportionally at: ', thr_perc, '% ...\n')) else: print("%s%.2f%s" % ('\nThresholding to achieve density of: ', thr_perc, '% ...\n')) # Fit mat conn_matrix = graphestimation.get_conn_matrix(ts_within_nodes, conn_model) # Save unthresholded unthr_path = utils.create_unthr_path(ID, network, conn_model, mask, dir_path) np.save(unthr_path, conn_matrix) if dens_thresh is False: conn_matrix_thr = thresholding.threshold_proportional( conn_matrix, float(thr)) else: conn_matrix_thr = thresholding.density_thresholding( conn_matrix, float(thr)) # Save thresholded mat est_path = utils.create_est_path(ID, network, conn_model, thr, mask, dir_path, node_size) np.save(est_path, conn_matrix_thr) return conn_matrix_thr, edge_threshold, est_path, thr, node_size, network
def thresh_diff(dens_thresh, thr, conn_model, network, ID, dir_path, mask, node_size, conn_matrix, parc, min_span_tree, disp_filt, atlas_select, uatlas_select, label_names, coords): from pynets import utils, thresholding thr_perc = 100 * float(thr) edge_threshold = "%s%s" % (str(thr_perc), '%') if parc is True: node_size = 'parc' if min_span_tree is True: print( 'Using local thresholding option with the Minimum Spanning Tree (MST)...\n' ) if dens_thresh is False: thr_type = 'MSTprop' conn_matrix_thr = thresholding.local_thresholding_prop( conn_matrix, thr) else: thr_type = 'MSTdens' conn_matrix_thr = thresholding.local_thresholding_dens( conn_matrix, thr) elif disp_filt is True: thr_type = 'DISPα' G1 = thresholding.disparity_filter(nx.from_numpy_array(conn_matrix)) # G2 = nx.Graph([(u, v, d) for u, v, d in G1.edges(data=True) if d['alpha'] < thr]) print('Computing edge disparity significance with alpha = %s' % thr) print('Filtered graph: nodes = %s, edges = %s' % (G1.number_of_nodes(), G1.number_of_edges())) # print('Backbone graph: nodes = %s, edges = %s' % (G2.number_of_nodes(), G2.number_of_edges())) #print(G2.edges(data=True)) conn_matrix_thr = nx.to_numpy_array(G1) else: if dens_thresh is False: thr_type = 'prop' print("%s%.2f%s" % ('\nThresholding proportionally at: ', thr_perc, '% ...\n')) conn_matrix_thr = thresholding.threshold_proportional( conn_matrix, float(thr)) else: thr_type = 'dens' print("%s%.2f%s" % ('\nThresholding to achieve density of: ', thr_perc, '% ...\n')) conn_matrix_thr = thresholding.density_thresholding( conn_matrix, float(thr)) if not nx.is_connected(nx.from_numpy_matrix(conn_matrix_thr)): print('Warning: Fragmented graph') # Save thresholded mat smooth = 0 est_path = utils.create_est_path(ID, network, conn_model, thr, mask, dir_path, node_size, smooth, thr_type) np.save(est_path, conn_matrix_thr) return conn_matrix_thr, edge_threshold, est_path, thr, node_size, network, conn_model, mask, atlas_select, uatlas_select, label_names, coords
def thresh_and_fit(adapt_thresh, dens_thresh, thr, ts_within_nodes, conn_model, network, ID, dir_path, mask): from pynets import utils, thresholding, graphestimation ##Adaptive thresholding scenario if adapt_thresh is not False: try: est_path2 = dir_path + '/' + ID + '_structural_est.txt' if os.path.isfile(est_path2) == True: #[conn_matrix_thr, est_path, edge_threshold, thr] = adaptive_thresholding(ts_within_nodes, conn_model, network, ID, est_path2, dir_path) ##Save unthresholded unthr_path = utils.create_unthr_path(ID, network, conn_model, mask, dir_path) #np.savetxt(unthr_path, conn_matrix_thr, delimiter='\t') edge_threshold = str(float(thr) * 100) + '%' else: print('No structural mx found! Exiting...') sys.exit() except: print('No structural mx assigned! Exiting...') sys.exit() else: if not dens_thresh: print( '\nRunning graph estimation and thresholding proportionally at: ' + str(thr) + '% ...\n') else: print( '\nRunning graph estimation and thresholding to achieve density of: ' + str(100 * dens_thresh) + '% ...\n') ##Fit mat conn_matrix = graphestimation.get_conn_matrix(ts_within_nodes, conn_model) ##Save unthresholded unthr_path = utils.create_unthr_path(ID, network, conn_model, mask, dir_path) np.savetxt(unthr_path, conn_matrix, delimiter='\t') if not dens_thresh: ##Save thresholded conn_matrix_thr = thresholding.threshold_proportional( conn_matrix, float(thr)) edge_threshold = str(float(thr) * 100) + '%' est_path = utils.create_est_path(ID, network, conn_model, thr, mask, dir_path) else: conn_matrix_thr = thresholding.density_thresholding( conn_matrix, dens_thresh) edge_threshold = str((1 - float(dens_thresh)) * 100) + '%' est_path = utils.create_est_path(ID, network, conn_model, dens_thresh, mask, dir_path) np.savetxt(est_path, conn_matrix_thr, delimiter='\t') return (conn_matrix_thr, edge_threshold, est_path, thr)
def thresh_diff(dens_thresh, thr, conn_matrix, conn_model, network, ID, dir_path, roi, node_size, min_span_tree, disp_filt, parc, prune, atlas_select, uatlas_select, label_names, coords, norm, binary, target_samples, track_type, atlas_mni, streams): from pynets import utils, thresholding thr_perc = 100 * float(thr) edge_threshold = "%s%s" % (str(thr_perc), '%') if parc is True: node_size = 'parc' if np.count_nonzero(conn_matrix) == 0: raise ValueError('ERROR: Raw connectivity matrix contains only zeros.') # Save unthresholded unthr_path = utils.create_unthr_path(ID, network, conn_model, roi, dir_path) utils.save_mat(conn_matrix, unthr_path) if min_span_tree is True: print('Using local thresholding option with the Minimum Spanning Tree (MST)...\n') if dens_thresh is False: thr_type = 'MSTprop' conn_matrix_thr = thresholding.local_thresholding_prop(conn_matrix, thr) else: thr_type = 'MSTdens' conn_matrix_thr = thresholding.local_thresholding_dens(conn_matrix, thr) elif disp_filt is True: thr_type = 'DISP_alpha' G1 = thresholding.disparity_filter(nx.from_numpy_array(conn_matrix)) # G2 = nx.Graph([(u, v, d) for u, v, d in G1.edges(data=True) if d['alpha'] < thr]) print('Computing edge disparity significance with alpha = %s' % thr) print('Filtered graph: nodes = %s, edges = %s' % (G1.number_of_nodes(), G1.number_of_edges())) # print('Backbone graph: nodes = %s, edges = %s' % (G2.number_of_nodes(), G2.number_of_edges())) #print(G2.edges(data=True)) conn_matrix_thr = nx.to_numpy_array(G1) else: if dens_thresh is False: thr_type='prop' print("%s%.2f%s" % ('\nThresholding proportionally at: ', thr_perc, '% ...\n')) conn_matrix_thr = thresholding.threshold_proportional(conn_matrix, float(thr)) else: thr_type = 'dens' print("%s%.2f%s" % ('\nThresholding to achieve density of: ', thr_perc, '% ...\n')) conn_matrix_thr = thresholding.density_thresholding(conn_matrix, float(thr)) if not nx.is_connected(nx.from_numpy_matrix(conn_matrix_thr)): print('Warning: Fragmented graph') # Save thresholded mat est_path = utils.create_est_path_diff(ID, network, conn_model, thr, roi, dir_path, node_size, target_samples, track_type, thr_type) utils.save_mat(conn_matrix_thr, est_path) return conn_matrix_thr, edge_threshold, est_path, thr, node_size, network, conn_model, roi, prune, ID, dir_path, atlas_select, uatlas_select, label_names, coords, norm, binary, target_samples, track_type, atlas_mni, streams
def density_thresholding(conn_matrix, thr): from pynets import thresholding work_thr = 0.0 conn_matrix = thresholding.normalize(conn_matrix) np.fill_diagonal(conn_matrix, 0) i = 1 thr_max = 0.50 G = nx.from_numpy_matrix(conn_matrix) density = nx.density(G) while float(work_thr) <= float(thr_max) and float(density) > float(thr): work_thr = float(work_thr) + float(0.01) conn_matrix = thresholding.threshold_proportional(conn_matrix, work_thr) G = nx.from_numpy_matrix(conn_matrix) density = nx.density(G) print("%s%d%s%.2f%s%.2f%s" % ('Iteratively thresholding -- Iteration ', i, ' -- with thresh: ', float(work_thr), ' and Density: ', float(density), '...')) i = i + 1 return conn_matrix
def thresh_struct(dens_thresh, thr, conn_matrix, conn_model, network, ID, dir_path, roi, node_size, min_span_tree, disp_filt, parc, prune, atlas, uatlas, labels, coords, norm, binary, target_samples, track_type, atlas_mni, streams): """ Threshold a structural connectivity matrix using any of a variety of methods. Parameters ---------- dens_thresh : bool Indicates whether a target graph density is to be used as the basis for thresholding. thr : float A value, between 0 and 1, to threshold the graph using any variety of methods triggered through other options. conn_matrix : array Adjacency matrix stored as an m x n array of nodes and edges. conn_model : str Connectivity estimation model (e.g. corr for correlation, cov for covariance, sps for precision covariance, partcorr for partial correlation). sps type is used by default. network : str Resting-state network based on Yeo-7 and Yeo-17 naming (e.g. 'Default') used to filter nodes in the study of brain subgraphs. ID : str A subject id or other unique identifier. dir_path : str Path to directory containing subject derivative data for given run. roi : str File path to binarized/boolean region-of-interest Nifti1Image file. node_size : int Spherical centroid node size in the case that coordinate-based centroids are used as ROI's. min_span_tree : bool Indicates whether local thresholding from the Minimum Spanning Tree should be used. disp_filt : bool Indicates whether local thresholding using a disparity filter and 'backbone network' should be used. parc : bool Indicates whether to use parcels instead of coordinates as ROI nodes. prune : bool Indicates whether to prune final graph of disconnected nodes/isolates. atlas : str Name of atlas parcellation used. uatlas : str File path to atlas parcellation Nifti1Image in MNI template space. labels : list List of string labels corresponding to ROI nodes. coords : list List of (x, y, z) tuples corresponding to a coordinate atlas used or which represent the center-of-mass of each parcellation node. norm : int Indicates method of normalizing resulting graph. binary : bool Indicates whether to binarize resulting graph edges to form an unweighted graph. target_samples : int Total number of streamline samples specified to generate streams. track_type : str Tracking algorithm used (e.g. 'local' or 'particle'). atlas_mni : str File path to atlas parcellation Nifti1Image in T1w-warped MNI space. streams : str File path to save streamline array sequence in .trk format. Returns ------- conn_matrix_thr : array Weighted, thresholded, NxN matrix. edge_threshold : str The string percentage representation of thr. est_path : str File path to the thresholded graph, conn_matrix_thr, saved as a numpy array in .npy format. thr : float The value, between 0 and 1, used to threshold the graph using any variety of methods triggered through other options. node_size : int Spherical centroid node size in the case that coordinate-based centroids are used as ROI's. network : str Resting-state network based on Yeo-7 and Yeo-17 naming (e.g. 'Default') used to filter nodes in the study of brain subgraphs. conn_model : str Connectivity estimation model (e.g. corr for correlation, cov for covariance, sps for precision covariance, partcorr for partial correlation). sps type is used by default. roi : str File path to binarized/boolean region-of-interest Nifti1Image file. prune : bool Indicates whether to prune final graph of disconnected nodes/isolates. ID : str A subject id or other unique identifier. dir_path : str Path to directory containing subject derivative data for given run. atlas : str Name of atlas parcellation used. uatlas : str File path to atlas parcellation Nifti1Image in MNI template space. labels : list List of string labels corresponding to ROI nodes. coords : list List of (x, y, z) tuples corresponding to a coordinate atlas used or which represent the center-of-mass of each parcellation node. norm : int Indicates method of normalizing resulting graph. binary : bool Indicates whether to binarize resulting graph edges to form an unweighted graph. target_samples : int Total number of streamline samples specified to generate streams. track_type : str Tracking algorithm used (e.g. 'local' or 'particle'). atlas_mni : str File path to atlas parcellation Nifti1Image in T1w-warped MNI space. streams : str File path to save streamline array sequence in .trk format. """ from pynets import utils, thresholding thr_perc = 100 * float(thr) edge_threshold = "%s%s" % (str(thr_perc), '%') if parc is True: node_size = 'parc' if np.count_nonzero(conn_matrix) == 0: raise ValueError('ERROR: Raw connectivity matrix contains only zeros.') # Save unthresholded unthr_path = utils.create_unthr_path(ID, network, conn_model, roi, dir_path) utils.save_mat(conn_matrix, unthr_path) if min_span_tree is True: print( 'Using local thresholding option with the Minimum Spanning Tree (MST)...\n' ) if dens_thresh is False: thr_type = 'MSTprop' conn_matrix_thr = thresholding.local_thresholding_prop( conn_matrix, thr) else: thr_type = 'MSTdens' conn_matrix_thr = thresholding.local_thresholding_dens( conn_matrix, thr) elif disp_filt is True: thr_type = 'DISP_alpha' G1 = thresholding.disparity_filter(nx.from_numpy_array(conn_matrix)) # G2 = nx.Graph([(u, v, d) for u, v, d in G1.edges(data=True) if d['alpha'] < thr]) print('Computing edge disparity significance with alpha = %s' % thr) print('Filtered graph: nodes = %s, edges = %s' % (G1.number_of_nodes(), G1.number_of_edges())) # print('Backbone graph: nodes = %s, edges = %s' % (G2.number_of_nodes(), G2.number_of_edges())) # print(G2.edges(data=True)) conn_matrix_thr = nx.to_numpy_array(G1) else: if dens_thresh is False: thr_type = 'prop' print("%s%.2f%s" % ('\nThresholding proportionally at: ', thr_perc, '% ...\n')) conn_matrix_thr = thresholding.threshold_proportional( conn_matrix, float(thr)) else: thr_type = 'dens' print("%s%.2f%s" % ('\nThresholding to achieve density of: ', thr_perc, '% ...\n')) conn_matrix_thr = thresholding.density_thresholding( conn_matrix, float(thr)) if not nx.is_connected(nx.from_numpy_matrix(conn_matrix_thr)): print('Warning: Fragmented graph') # Save thresholded mat est_path = utils.create_est_path_diff(ID, network, conn_model, thr, roi, dir_path, node_size, target_samples, track_type, thr_type, parc) utils.save_mat(conn_matrix_thr, est_path) return conn_matrix_thr, edge_threshold, est_path, thr, node_size, network, conn_model, roi, prune, ID, dir_path, atlas, uatlas, labels, coords, norm, binary, target_samples, track_type, atlas_mni, streams
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
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
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