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_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 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