Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@pytest.mark.parametrize("atlas", ['atlas_aal', 'atlas_talairach_gyrus', 'atlas_talairach_ba', 'atlas_talairach_lobe',
'atlas_harvard_oxford', 'atlas_destrieux_2009'])
def test_nilearn_atlas_helper(atlas):
parc = False
[labels, networks_list, parlistfile] = nodemaker.nilearn_atlas_helper(atlas, parc)
print(labels)
print(networks_list)
print(parlistfile)
assert labels is not None
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
labels_adj = list(labels)
coords_adj = list(tuple(x) for x in coords)
parcel_list_adj = parcel_list
try:
for ix in sorted(indices, reverse=True):
print("%s%s%s%s" % ('Removing: ', labels_adj[ix], ' at ', coords_adj[ix]))
labels_adj.pop(ix)
coords_adj.pop(ix)
parcel_list_adj.pop(ix)
except RuntimeError:
print('ERROR: Restrictive masking. No parcels remain after masking with brain mask/roi...')
# Create a resampled 3D atlas that can be viewed alongside mask img for QA
resampled_parcels_nii_path = "%s%s%s%s%s%s" % (dir_path, '/', ID, '_parcels_resampled2roimask_',
op.basename(roi).split('.')[0], '.nii.gz')
resampled_parcels_map_nifti = resample_img(nodemaker.create_parcel_atlas(parcel_list_adj)[0],
target_affine=mask_aff, target_shape=mask_data.shape,
interpolation='nearest')
nib.save(resampled_parcels_map_nifti, resampled_parcels_nii_path)
resampled_parcels_map_nifti.uncache()
if not coords_adj:
raise ValueError('\nERROR: ROI mask was likely too restrictive and yielded < 2 remaining parcels')
return coords_adj, labels_adj, parcel_list_adj
else:
if (ref_txt is not None) and (op.exists(ref_txt)) and (use_AAL_naming is False):
labels = pd.read_csv(ref_txt, sep=" ", header=None, names=["Index", "Region"])['Region'].tolist()
else:
try:
ref_txt = "%s%s%s%s" % (str(Path(base_path).parent), '/labelcharts/', atlas, '.txt')
if op.exists(ref_txt) and (use_AAL_naming is False):
try:
labels = pd.read_csv(ref_txt,
sep="\t", header=None, names=["Index", "Region"])['Region'].tolist()
except:
labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()
else:
if use_AAL_naming is True:
try:
labels = nodemaker.AAL_naming(coords)
except:
print('AAL reference labeling failed!')
labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()
else:
print('Using generic index labels...')
labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()
except:
print("Label reference file not found. Attempting AAL naming...")
if use_AAL_naming is True:
try:
labels = nodemaker.AAL_naming(coords)
except:
print('AAL reference labeling failed!')
labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()
else:
print('Using generic index labels...')
base_path = utils.get_file()
# Test if atlas is a nilearn atlas. If so, fetch coords, labels, and/or networks.
nilearn_parc_atlases = ['atlas_harvard_oxford', 'atlas_aal', 'atlas_destrieux_2009', 'atlas_talairach_gyrus',
'atlas_talairach_ba', 'atlas_talairach_lobe']
nilearn_coords_atlases = ['coords_power_2011', 'coords_dosenbach_2010']
nilearn_prob_atlases = ['atlas_msdl', 'atlas_pauli_2017']
if uatlas is None and atlas in nilearn_parc_atlases:
[labels, networks_list, uatlas] = nodemaker.nilearn_atlas_helper(atlas, parc)
if uatlas:
if not isinstance(uatlas, str):
nib.save(uatlas, "%s%s%s" % ('/tmp/', atlas, '.nii.gz'))
uatlas = "%s%s%s" % ('/tmp/', atlas, '.nii.gz')
[coords, _, par_max] = nodemaker.get_names_and_coords_of_parcels(uatlas)
if parc is True:
parcel_list = nodemaker.gen_img_list(uatlas)
else:
parcel_list = None
else:
raise ValueError("%s%s%s" % ('\nERROR: Atlas file for ', atlas, ' not found!'))
elif uatlas is None and parc is False and atlas in nilearn_coords_atlases:
print('Fetching coords and labels from nilearn coordinate-based atlas library...')
# Fetch nilearn atlas coords
[coords, _, networks_list, labels] = nodemaker.fetch_nilearn_atlas_coords(atlas)
parcel_list = None
par_max = None
elif uatlas is None and parc is False and atlas in nilearn_prob_atlases:
from nilearn.plotting import find_probabilistic_atlas_cut_coords
print('Fetching coords and labels from nilearn probabilistic atlas library...')
# Fetch nilearn atlas coords
[labels, networks_list, uatlas] = nodemaker.nilearn_atlas_helper(atlas, parc)
coords = find_probabilistic_atlas_cut_coords(maps_img=uatlas)