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)
def test_nodemaker_tools_masking_parlistfile_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' parlistfile = base_dir + '/whole_brain_cluster_labels_PCA200.nii.gz' mask = dir_path + '/pDMN_3_bin.nii.gz' network = 'Default' ID = '997' perc_overlap = 0.10 parc = True start_time = time.time() [coords, _, _] = nodemaker.get_names_and_coords_of_parcels(parlistfile) print("%s%s%s" % ('get_names_and_coords_of_parcels --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) label_names = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist() start_time = time.time() parcel_list = nodemaker.gen_img_list(parlistfile) [net_coords, net_parcel_list, net_label_names, network] = nodemaker.get_node_membership(network, func_file, coords, label_names, parc, parcel_list) print("%s%s%s" % ('get_node_membership --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) start_time = time.time() [net_coords_masked, net_label_names_masked, net_parcel_list_masked ] = nodemaker.parcel_masker(mask, net_coords, net_parcel_list, net_label_names, dir_path, ID, perc_overlap) print("%s%s%s" % ('parcel_masker --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) start_time = time.time() [net_parcels_map_nifti, parcel_list_exp] = nodemaker.create_parcel_atlas(net_parcel_list_masked) print("%s%s%s" % ('create_parcel_atlas --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) start_time = time.time() out_path = nodemaker.gen_network_parcels(parlistfile, network, net_label_names_masked, dir_path) print("%s%s%s" % ('gen_network_parcels --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) assert coords is not None assert net_coords is not None assert net_label_names is not None assert net_parcel_list is not None assert net_coords_masked is not None assert net_label_names_masked is not None assert net_parcel_list_masked is not None assert out_path is not None assert net_parcels_map_nifti is not None assert parcel_list_exp is not None assert network is not None
def test_nodemaker_tools_masking_parlistfile_WB(): # Set example inputs base_dir = str(Path(__file__).parent/"examples") #base_dir = '/Users/rxh180012/PyNets-development/tests/examples' dir_path = base_dir + '/997' parlistfile = base_dir + '/whole_brain_cluster_labels_PCA200.nii.gz' atlas_select = 'whole_brain_cluster_labels_PCA200' roi = dir_path + '/pDMN_3_bin.nii.gz' mask = None ID = '997' parc = True perc_overlap = 0.10 start_time = time.time() [WB_coords, _, _] = nodemaker.get_names_and_coords_of_parcels(parlistfile) print("%s%s%s" % ('get_names_and_coords_of_parcels (Masking whole-brain version) --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) WB_label_names = np.arange(len(WB_coords) + 1)[np.arange(len(WB_coords) + 1) != 0].tolist() start_time = time.time() WB_parcel_list = nodemaker.gen_img_list(parlistfile) [_, _, WB_parcel_list_masked] = nodemaker.parcel_masker(roi, WB_coords, WB_parcel_list, WB_label_names, dir_path, ID, mask, perc_overlap) print("%s%s%s" % ('parcel_masker (Masking whole-brain version) --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) start_time = time.time() [WB_parcels_map_nifti, parcel_list_exp] = nodemaker.create_parcel_atlas(WB_parcel_list_masked) print("%s%s%s" % ('create_parcel_atlas (Masking whole-brain version) --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) start_time = time.time() [WB_net_parcels_map_nifti_unmasked, WB_coords_unmasked, _, WB_atlas_select, WB_uatlas_select] = nodemaker.node_gen(WB_coords, WB_parcel_list, WB_label_names, dir_path, ID, parc, atlas_select, parlistfile) print("%s%s%s" % ('node_gen (Masking whole-brain version) --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) start_time = time.time() [WB_net_parcels_map_nifti_masked, WB_coords_masked, WB_label_names_masked, WB_atlas_select, WB_uatlas_select] = nodemaker.node_gen_masking(roi, WB_coords, WB_parcel_list, WB_label_names, dir_path, ID, parc, atlas_select, parlistfile, mask) print("%s%s%s" % ('node_gen_masking (Masking whole-brain version) --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) assert WB_coords is not None assert WB_label_names is not None assert WB_parcel_list is not None assert WB_coords_masked is not None assert WB_label_names_masked is not None assert WB_parcel_list_masked is not None assert WB_parcels_map_nifti is not None assert parcel_list_exp is not None assert WB_net_parcels_map_nifti_unmasked is not None assert WB_coords_unmasked is not None assert WB_net_parcels_map_nifti_masked is not None assert WB_coords_masked is not None
def test_nodemaker_tools_masking_parlistfile_RSN(): # Set example inputs base_dir = str(Path(__file__).parent / "examples") dir_path = base_dir + '/002/fmri' func_file = dir_path + '/002.nii.gz' parlistfile = base_dir + '/whole_brain_cluster_labels_PCA200.nii.gz' roi = base_dir + '/pDMN_3_bin.nii.gz' network = 'Default' ID = '002' perc_overlap = 0.10 parc = True start_time = time.time() [coords, _, _] = nodemaker.get_names_and_coords_of_parcels(parlistfile) print("%s%s%s" % ('get_names_and_coords_of_parcels --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist() start_time = time.time() parcel_list = nodemaker.gen_img_list(parlistfile) [net_coords, net_parcel_list, net_labels, network] = nodemaker.get_node_membership(network, func_file, coords, labels, parc, parcel_list) print("%s%s%s" % ('get_node_membership --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) start_time = time.time() [net_coords_masked, net_labels_masked, net_parcel_list_masked ] = nodemaker.parcel_masker(roi, net_coords, net_parcel_list, net_labels, dir_path, ID, perc_overlap) print("%s%s%s" % ('parcel_masker --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) start_time = time.time() [net_parcels_map_nifti, parcel_list_exp] = nodemaker.create_parcel_atlas(net_parcel_list_masked) print("%s%s%s" % ('create_parcel_atlas --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) start_time = time.time() out_path = nodemaker.gen_network_parcels(parlistfile, network, net_labels_masked, dir_path) print("%s%s%s" % ('gen_network_parcels --> finished: ', str(np.round(time.time() - start_time, 1)), 's')) assert coords is not None assert net_coords is not None assert net_labels is not None assert net_parcel_list is not None assert net_coords_masked is not None assert net_labels_masked is not None assert net_parcel_list_masked is not None assert out_path is not None assert net_parcels_map_nifti is not None assert parcel_list_exp is not None assert network is not None
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
def node_gen_masking(roi, coords, parcel_list, label_names, dir_path, ID, parc, atlas_select, uatlas_select, mask, node_size=None): from pynets import nodemaker import os.path as op 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 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: error = 2 [coords, label_names] = nodemaker.coords_masker(roi, coords, label_names, mask, error) # Save coords to pickle coords_path = "%s%s%s%s" % (dir_path, '/atlas_coords_', op.basename(roi).split('.')[0], '.pkl') with open(coords_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_', op.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, node_size
def test_nodemaker_tools_masking_parlistfile_WB(): ##Set example inputs## base_dir = str(Path(__file__).parent / "examples") dir_path = base_dir + '/997' parlistfile = base_dir + '/whole_brain_cluster_labels_PCA200.nii.gz' mask = dir_path + '/pDMN_3_bin.nii.gz' ID = '997' atlas_select = 'whole_brain_cluster_labels_PCA200' parc = True [WB_coords, atlas_select, par_max, WB_parcel_list] = nodemaker.get_names_and_coords_of_parcels(parlistfile) WB_label_names = np.arange(len(WB_coords) + 1)[np.arange(len(WB_coords) + 1) != 0].tolist() [WB_coords_masked, WB_label_names_masked, WB_parcel_list_masked ] = nodemaker.parcel_masker(mask, WB_coords, WB_parcel_list, WB_label_names, dir_path, ID) [WB_parcels_map_nifti, parcel_list_exp] = nodemaker.create_parcel_atlas(WB_parcel_list_masked) [ WB_net_parcels_map_nifti_unmasked, WB_coords_unmasked, WB_label_names_unmasked ] = nodemaker.node_gen(WB_coords, WB_parcel_list, WB_label_names, dir_path, ID, parc) [WB_net_parcels_map_nifti_masked, WB_coords_masked, WB_label_names_masked ] = nodemaker.node_gen_masking(mask, WB_coords, WB_parcel_list, WB_label_names, dir_path, ID, parc) assert WB_coords is not None assert WB_label_names is not None assert WB_parcel_list is not None assert WB_coords_masked is not None assert WB_label_names_masked is not None assert WB_parcel_list_masked is not None assert WB_parcels_map_nifti is not None assert parcel_list_exp is not None assert WB_net_parcels_map_nifti_unmasked is not None assert WB_coords_unmasked is not None assert WB_net_parcels_map_nifti_masked is not None assert WB_coords_masked is not None
def test_nodemaker_tools_masking_parlistfile_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' parlistfile = base_dir + '/whole_brain_cluster_labels_PCA200.nii.gz' mask = dir_path + '/pDMN_3_bin.nii.gz' network = 'Default' ID = '997' atlas_select = 'whole_brain_cluster_labels_PCA200' parc = True [coords, atlas_select, par_max, parcel_list] = nodemaker.get_names_and_coords_of_parcels(parlistfile) label_names = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist() [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, net_parcel_list_masked ] = nodemaker.parcel_masker(mask, net_coords, net_parcel_list, net_label_names, dir_path, ID) [net_parcels_map_nifti, parcel_list_exp] = nodemaker.create_parcel_atlas(net_parcel_list_masked) out_path = nodemaker.gen_network_parcels(parlistfile, network, net_label_names_masked, dir_path) assert coords is not None assert net_coords is not None assert net_label_names is not None assert net_parcel_list is not None assert net_coords_masked is not None assert net_label_names_masked is not None assert net_parcel_list_masked is not None assert out_path is not None assert net_parcels_map_nifti is not None assert parcel_list_exp is not None assert network is not None
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 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) # Mask Coordinates elif parc is False: 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, '/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
def node_gen_masking(roi, coords, parcel_list, labels, dir_path, ID, parc, atlas, uatlas, perc_overlap=0.75, error=4): """ In the case that masking was applied, this function generate nodes based on atlas definitions established by fetch_nodes_and_labels. Parameters ---------- roi : str File path to binarized/boolean region-of-interest Nifti1Image file. coords : list List of (x, y, z) tuples in mm-space corresponding to a coordinate atlas used or which represent the center-of-mass of each parcellation node. parcel_list : list List of 3D boolean numpy arrays or binarized Nifti1Images corresponding to ROI masks. labels : list List of string labels corresponding to ROI nodes. dir_path : str Path to directory containing subject derivative data for given run. ID : str A subject id or other unique identifier. parc : bool Indicates whether to use parcels instead of coordinates as ROI nodes. atlas : str Name of a Nilearn-hosted coordinate or parcellation/label-based atlas supported for fetching. See Nilearn's datasets.atlas module for more detailed reference. uatlas : str File path to atlas parcellation Nifti1Image in MNI template space. perc_overlap : float Value 0-1 indicating a threshold of spatial overlap to use as a spatial error cushion in the case of evaluating mask/RSN membership from a given list of parcel masks. Default is 0.75. error : int Rounded euclidean distance, in units of voxel number, to use as a spatial error cushion in the case of evaluating mask/RSN membership from a given list of coordinates. Default is 4. Returns ------- net_parcels_map_nifti : Nifti1Image A nibabel-based nifti image consisting of a 3D array with integer voxel intensities corresponding to ROI membership. coords : list List of (x, y, z) tuples in mm-space corresponding to a coordinate atlas used or which represent the center-of-mass of each parcellation node. labels : list List of string labels corresponding to ROI nodes. atlas : str Name of a Nilearn-hosted coordinate or parcellation/label-based atlas supported for fetching. See Nilearn's datasets.atlas module for more detailed reference. uatlas : str File path to atlas parcellation Nifti1Image in MNI template space. """ from pynets import nodemaker import os.path as op 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 [coords, labels, parcel_list_masked ] = nodemaker.parcel_masker(roi, coords, parcel_list, labels, dir_path, ID, perc_overlap) [net_parcels_map_nifti, _] = nodemaker.create_parcel_atlas(parcel_list_masked) # Mask Coordinates else: [coords, labels] = nodemaker.coords_masker(roi, coords, labels, error) # Save coords to pickle coords_path = "%s%s%s%s" % (dir_path, '/atlas_coords_', op.basename(roi).split('.')[0], '.pkl') with open(coords_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_', op.basename(roi).split('.')[0], '.pkl') with open(labels_path, 'wb') as f: pickle.dump(labels, f, protocol=2) return net_parcels_map_nifti, coords, labels, atlas, uatlas