Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_do_dir_path():
"""
Test do_dir_path functionality
"""
base_dir = str(Path(__file__).parent/"examples")
func_path = base_dir + '/002/fmri'
dwi_path = base_dir + '/002/dmri'
in_func = func_path + '/002.nii.gz'
in_dwi = dwi_path + '/std_dmri/iso_eddy_corrected_data_denoised_pre_reor.nii.gz'
in_files = [in_func, in_dwi]
atlases = ['Power', 'Shirer', 'Shen', 'Smith']
for inputs in in_files:
for atlas in atlases:
dir_path = utils.do_dir_path(atlas, inputs)
assert dir_path is not None
atlas_par = uatlas.split('/')[-1].split('.')[0]
print(atlas_par)
ref_txt = "%s%s" % (uatlas.split('/')[-1:][0].split('.')[0], '.txt')
print("%s%s" % ('Using label reference: ', ref_txt))
if multi_atlas:
print('\nIterating across multiple predefined atlases...')
if dwi_file_list:
for _dwi_file in dwi_file_list:
for _atlas in multi_atlas:
if (parc is True) and (_atlas in nilearn_coord_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', _atlas,
' is a coordinate atlas and must be used with the -spheres '
'flag.'))
else:
print(_atlas)
do_dir_path(_atlas, _dwi_file)
else:
for _atlas in multi_atlas:
if (parc is True) and (_atlas in nilearn_coord_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', _atlas,
' is a coordinate atlas and must be used with the -spheres '
'flag.'))
else:
print(_atlas)
do_dir_path(_atlas, dwi_file)
elif atlas:
if (parc is True) and (atlas in nilearn_coord_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', atlas,
' is a coordinate atlas and must be used with the -spheres flag.'))
else:
print("%s%s" % ("\nNilearn atlas: ", atlas))
if dwi_file_list:
print('\nUsing multiple custom input graphs...')
conn_model = None
conn_model_list = []
i = 1
for graph in multi_graph:
conn_model_list.append(str(i))
if '.txt' in graph:
graph_name = op.basename(graph).split('.txt')[0]
elif '.npy' in graph:
graph_name = op.basename(graph).split('.npy')[0]
else:
print('Error: input graph file format not recognized. See -help for supported formats.')
sys.exit(0)
print(graph_name)
atlas = "%s%s%s" % (graph_name, '_', ID)
do_dir_path(atlas, graph)
i = i + 1
else:
if '.txt' in graph:
graph_name = op.basename(graph).split('.txt')[0]
elif '.npy' in graph:
graph_name = op.basename(graph).split('.npy')[0]
else:
print('Error: input graph file format not recognized. See -help for supported formats.')
sys.exit(0)
print('\nUsing single custom graph input...')
print(graph_name)
atlas = "%s%s%s" % (graph_name, '_', ID)
do_dir_path(atlas, graph)
if func_file or func_file_list:
if (uatlas is not None) and (k_clustering == 0) and (user_atlas_list is None):
print('Error: the -ua flag cannot be used alone with the clustering option. Use the -cm flag instead.')
sys.exit(0)
if multi_atlas is not None:
print('\nIterating across multiple predefined atlases...')
if func_file_list:
for _func_file in func_file_list:
for _atlas in multi_atlas:
if (parc is True) and (_atlas in nilearn_coord_atlases or _atlas in
nilearn_prob_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', _atlas,
' is a coordinate atlas and must be used with the -spheres '
'flag.'))
else:
print(_atlas)
do_dir_path(_atlas, _func_file)
else:
for _atlas in multi_atlas:
if (parc is True) and (_atlas in nilearn_coord_atlases or _atlas in
nilearn_prob_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', _atlas,
' is a coordinate atlas and must be used with the -spheres '
'flag.'))
else:
print(_atlas)
do_dir_path(_atlas, func_file)
elif atlas is not None:
if (parc is True) and (atlas in nilearn_coord_atlases or atlas in nilearn_prob_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', atlas,
' is a coordinate atlas and must be used with the -spheres flag.'))
else:
print("%s%s" % ("\nPredefined atlas: ", atlas))
if (parc is True) and (_atlas in nilearn_coord_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', _atlas,
' is a coordinate atlas and must be used with the -spheres '
'flag.'))
else:
print(_atlas)
do_dir_path(_atlas, _dwi_file)
else:
for _atlas in multi_atlas:
if (parc is True) and (_atlas in nilearn_coord_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', _atlas,
' is a coordinate atlas and must be used with the -spheres '
'flag.'))
else:
print(_atlas)
do_dir_path(_atlas, dwi_file)
elif atlas:
if (parc is True) and (atlas in nilearn_coord_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', atlas,
' is a coordinate atlas and must be used with the -spheres flag.'))
else:
print("%s%s" % ("\nNilearn atlas: ", atlas))
if dwi_file_list:
for _dwi_file in dwi_file_list:
do_dir_path(atlas, _dwi_file)
else:
do_dir_path(atlas, dwi_file)
else:
if uatlas is None:
raise KeyError('\nERROR: No atlas specified!')
else:
pass
if (parc is True) and (_atlas in nilearn_coord_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', _atlas,
' is a coordinate atlas and must be used with the -spheres '
'flag.'))
else:
print(_atlas)
do_dir_path(_atlas, dwi_file)
elif atlas:
if (parc is True) and (atlas in nilearn_coord_atlases):
raise ValueError("%s%s%s" % ('\nERROR: ', atlas,
' is a coordinate atlas and must be used with the -spheres flag.'))
else:
print("%s%s" % ("\nNilearn atlas: ", atlas))
if dwi_file_list:
for _dwi_file in dwi_file_list:
do_dir_path(atlas, _dwi_file)
else:
do_dir_path(atlas, dwi_file)
else:
if uatlas is None:
raise KeyError('\nERROR: No atlas specified!')
else:
pass
if target_samples:
print("%s%s%s" % ('Using ', target_samples, ' samples...'))
if max_length:
print("%s%s%s" % ('Using ', max_length, ' maximum length of streamlines...'))
if (dwi_file or dwi_file_list) and not (func_file or func_file_list):
print('\nRunning dmri connectometry only...')
if dwi_file_list:
for (_dwi_file, _fbval, _fbvec, _anat_file) in dwi_file_list:
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...')
labels = np.arange(len(coords) + 1)[np.arange(len(coords) + 1) != 0].tolist()
else:
print('WARNING: No labels available since atlas name is not specified!')
print("%s%s" % ('Labels:\n', labels))
dir_path = utils.do_dir_path(atlas.replace('reor_RAS_nores2mm_', ''), in_file)
if len(coords) != len(labels):
labels = len(coords) * [np.nan]
if len(coords) != len(labels):
raise ValueError('ERROR: length of coordinates is not equal to length of label names')
return labels, coords, atlas, networks_list, parcel_list, par_max, uatlas, dir_path