def test_create_est_path_diff(node_size, parc): """ Test create_est_path_func functionality """ import tempfile dir_path = str(tempfile.TemporaryDirectory().name) os.makedirs(dir_path) network = 'Default' ID = '002' roi = None directget = 'prob' min_length = 20 conn_model = 'corr' thr_type = 'prop' target_samples = 10 track_type = 'local' thr = 0.75 error_margin = 6 est_path = utils.create_est_path_diff(ID, network, conn_model, thr, roi, dir_path, node_size, target_samples, track_type, thr_type, parc, directget, min_length, error_margin) assert est_path is not None
def test_create_est_path_diff(node_size, parc): """ Test create_est_path_func functionality """ base_dir = str(Path(__file__).parent / "examples") dir_path = base_dir + '/002/dmri' network = 'Default' ID = '002' roi = None directget = 'prob' max_length = 200 conn_model = 'corr' thr_type = 'prop' target_samples = 10 track_type = 'local' thr = 0.75 # Delete pre-existing graphs dir graph_dir = dir_path + '/graphs' if os.path.exists(graph_dir): shutil.move(graph_dir, graph_dir + '_tmp') est_path = utils.create_est_path_diff(ID, network, conn_model, thr, roi, dir_path, node_size, target_samples, track_type, thr_type, parc, directget, max_length) assert est_path is not None # Restore pre-existing graph dir if os.path.exists(graph_dir + '_tmp'): shutil.rmtree(graph_dir) shutil.move(graph_dir + '_tmp', graph_dir)
def test_create_est_path_diff(): """ Test create_est_path_func functionality """ base_dir = str(Path(__file__).parent/"examples") dir_path = base_dir + '/002/dmri' network = 'Default' ID = '002' models = ['corr', 'cov', 'sps', 'partcorr'] roi = None node_size = 6 directget = 'prob' max_length = 200 for conn_model in models: for val in range(1, 10): thr = round(val*0.1, 1) for thr_type in ['prop', 'abs', 'dens', 'mst', 'disp']: for target_samples in range(0, 100, 1000): for track_type in ['local', 'particle']: for parc in [True, False]: est_path = utils.create_est_path_diff(ID, network, conn_model, thr, roi, dir_path, node_size, target_samples, track_type, thr_type, parc, directget, max_length) assert est_path is not None
def test_create_est_path_diff(node_size, parc): """ Test create_est_path_func functionality """ import tempfile tmp = tempfile.TemporaryDirectory() dir_path = str(tmp.name) os.makedirs(dir_path, exist_ok=True) subnet = 'Default' ID = '002' roi = None traversal = 'prob' min_length = 20 conn_model = 'corr' thr_type = 'prop' track_type = 'local' thr = 0.75 error_margin = 6 est_path = utils.create_est_path_diff(ID, subnet, conn_model, thr, roi, dir_path, node_size, track_type, thr_type, parc, traversal, min_length, error_margin) assert est_path is not None tmp.cleanup()
def test_create_csv_path(): """ Test create_csv_path functionality """ base_dir = str(Path(__file__).parent/"examples") # fmri case dir_path = base_dir + '/002/fmri' network = 'Default' ID = '002' models = ['corr', 'cov', 'sps', 'partcorr'] roi = None node_size = 6 smooth = 6 c_boot = 100 hpass = 100 parc = True directget = 'prob' max_length = 200 # Cross test various connectivity models, thresholds, and parc true/false. for conn_model in models: for val in range(1, 10): thr = round(val*0.1, 1) for thr_type in ['prop', 'abs', 'dens', 'mst', 'disp']: for parc in [True, False]: est_path = utils.create_est_path_func(ID, network, conn_model, thr, roi, dir_path, node_size, smooth, c_boot, thr_type, hpass, parc) out_path = utils.create_csv_path(dir_path, est_path) assert out_path is not None dir_path = base_dir + '/002/dmri' network = 'Default' ID = '002' models = ['corr', 'cov', 'sps', 'partcorr'] roi = None node_size = 6 for conn_model in models: for val in range(1, 10): thr = round(val*0.1, 1) for thr_type in ['prop', 'abs', 'dens', 'mst', 'disp']: for target_samples in range(0, 100, 1000): for track_type in ['local', 'particle']: for parc in [True, False]: est_path = utils.create_est_path_diff(ID, network, conn_model, thr, roi, dir_path, node_size, target_samples, track_type, thr_type, parc, directget, max_length) out_path = utils.create_csv_path(dir_path, est_path) assert out_path is not None
def test_pass_meta_outs(embed_multimodal_multiplex): """ Test pass_meta_outs functionality Note: omni argument may be failing due to functions in netmotifs or due to an unexpected input. Marked for xfail and should be updated after tests for netmotifs are created. """ base_dir = str(Path(__file__).parent / "examples") dmri_path = base_dir + '/002/dmri' func_path = base_dir + '/002/fmri' conn_model_func = 'corr' conn_model_struct = 'csa' network_func = 'Default' network_struct = 'Default' thr_func = 0.6 thr_struct = 0.8 prune_func = True prune_struct = False ID_func = '002' ID_struct = '002' roi_func = func_path + '/pDMN_3_bin_mask.nii.gz' roi_struct = dmri_path + '/pDMN_3_bin_mask.nii.gz' # this doesn't exists norm_func = 1 norm_struct = 2 binary_func = False binary_struct = True embed = embed_multimodal_multiplex[0] multimodal = embed_multimodal_multiplex[1] multiplex = embed_multimodal_multiplex[2] node_size = 6 smooth = 6 c_boot = 1000 thr_type = 'prop' hpass = 100 parc = True directget = 'prob' max_length = 200 thr_type = 'prop' target_samples = 10 track_type = 'local' conn_matrix_diff = np.random.rand(10, 10) conn_matrix_func = np.random.rand(10, 10) est_path_func = utils.create_est_path_func(ID_func, network_func, conn_model_func, thr_func, roi_func, func_path, node_size, smooth, c_boot, thr_type, hpass, parc) est_path_diff = utils.create_est_path_diff(ID_struct, network_struct, conn_model_struct, thr_struct, roi_struct, dmri_path, node_size, target_samples, track_type, thr_type, parc, directget, max_length) utils.save_mat(conn_matrix_diff, est_path_diff) utils.save_mat(conn_matrix_func, est_path_func) [ conn_model_iterlist, est_path_iterlist, network_iterlist, thr_iterlist, prune_iterlist, ID_iterlist, roi_iterlist, norm_iterlist, binary_iterlist ] = utils.pass_meta_ins_multi(conn_model_func, est_path_func, network_func, thr_func, prune_func, ID_func, roi_func, norm_func, binary_func, conn_model_struct, est_path_diff, network_struct, thr_struct, prune_struct, ID_struct, roi_struct, norm_struct, binary_struct) [ conn_model_iterlist_out, est_path_iterlist_out, network_iterlist_out, thr_iterlist_out, prune_iterlist_out, ID_iterlist_out, roi_iterlist_out, norm_iterlist_out, binary_iterlist_out ] = utils.pass_meta_outs(conn_model_iterlist, est_path_iterlist, network_iterlist, thr_iterlist, prune_iterlist, ID_iterlist, roi_iterlist, norm_iterlist, binary_iterlist, embed, multimodal, multiplex) assert conn_model_iterlist_out is not None assert est_path_iterlist_out is not None assert network_iterlist_out is not None assert thr_iterlist_out is not None assert prune_iterlist_out is not None assert ID_iterlist_out is not None assert roi_iterlist_out is not None assert norm_iterlist_out is not None assert binary_iterlist_out is not None
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, directget, min_length): """ 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. directget : str The statistical approach to tracking. Options are: det (deterministic), closest (clos), boot (bootstrapped), and prob (probabilistic). min_length : int Minimum fiber length threshold in mm to restrict tracking. 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. directget : str The statistical approach to tracking. Options are: det (deterministic), closest (clos), boot (bootstrapped), and prob (probabilistic). min_length : int Minimum fiber length threshold in mm to restrict tracking. """ import gc from pynets.core import utils, thresholding thr_perc = 100 - np.abs(100 * float(thr)) 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 utils.save_mat( conn_matrix, utils.create_raw_path_diff(ID, network, conn_model, roi, dir_path, node_size, target_samples, track_type, parc, directget, min_length)) [thr_type, edge_threshold, conn_matrix_thr, coords, labels] = thresholding.perform_thresholding(conn_matrix, coords, labels, thr, thr_perc, min_span_tree, dens_thresh, disp_filt) 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, directget, min_length) utils.save_mat(conn_matrix_thr, est_path) gc.collect() 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, directget, min_length)
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_for_streams, streams, directget, min_length, error_margin, check_consistency=True, ): """ 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_for_streams : str File path to atlas parcellation Nifti1Image in the morphological space of the streamlines. streams : str File path to save streamline array sequence in .trk format. directget : str The statistical approach to tracking. Options are: det (deterministic), closest (clos), boot (bootstrapped), and prob (probabilistic). min_length : int Minimum fiber length threshold in mm to restrict tracking. 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_for_streams : str File path to atlas parcellation Nifti1Image in the morphological space of the streamlines. streams : str File path to save streamline array sequence in .trk format. directget : str The statistical approach to tracking. Options are: det (deterministic), closest (clos), boot (bootstrapped), and prob (probabilistic). min_length : int Minimum fiber length threshold in mm to restrict tracking. References ---------- .. [1] van Wijk, B. C. M., Stam, C. J., & Daffertshofer, A. (2010). Comparing brain networks of different size and connectivity density using graph theory. PLoS ONE. https://doi.org/10.1371/journal.pone.0013701 """ import gc from pynets.core import utils, thresholding if parc is True: node_size = "parc" if np.count_nonzero(conn_matrix) == 0: print(UserWarning("Raw connectivity matrix contains only" " zeros.")) [thr_type, edge_threshold, conn_matrix_thr] = \ thresholding.perform_thresholding( conn_matrix, thr, min_span_tree, dens_thresh, disp_filt) 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, directget, min_length, error_margin) utils.save_mat(conn_matrix_thr, est_path) gc.collect() if check_consistency is True: assert len(coords) == len(labels) == conn_matrix_thr.shape[0] if network is not None: atlas_name = f"{atlas}_{network}_stage-post_thr" else: atlas_name = f"{atlas}_stage-post_thr" utils.save_coords_and_labels_to_json(coords, labels, dir_path, atlas_name, indices=None) return (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_for_streams, streams, directget, min_length, error_margin)