Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
current_iter = clinica_group_reader(self.caps_directory,
{'pattern': 'group-' + self._group_id + '/t1/group-'
+ self._group_id + '_iteration-' + str(i)
+ '_template.nii*',
'description': 'iteration #' + str(i) + ' of template for group ' + self._group_id,
'needed_pipeline': 't1-volume-create-dartel'})
dartel_iter_templates.append(current_iter)
except ClinicaException as e:
all_errors.append(e)
if len(all_errors) > 0:
error_message = 'Clinica faced error(s) while trying to read files in your CAPS/BIDS directories.\n'
for msg in all_errors:
error_message += str(msg)
raise ClinicaCAPSError(error_message)
read_input_node.inputs.dartel_input_images = d_input
read_input_node.inputs.dartel_iteration_templates = dartel_iter_templates
self.connect([
(read_input_node, self.input_node, [('dartel_input_images', 'dartel_input_images')]),
(read_input_node, self.input_node, [('dartel_iteration_templates', 'dartel_iteration_templates')])
])
else:
raise ValueError('Image type ' + image_type + ' unknown')
try:
dartel_input = clinica_group_reader(self.caps_directory,
{'pattern': 'group-' + self.parameters['group_id'] + '_template.nii*',
'description': 'template file of group ' + self.parameters['group_id'],
'needed_pipeline': 't1-volume or t1-volume-create-dartel'})
except ClinicaException as e:
all_errors.append(e)
# Raise all errors if some happened
if len(all_errors) > 0:
error_message = 'Clinica faced errors while trying to read files in your CAPS directories.\n'
for msg in all_errors:
error_message += str(msg)
raise ClinicaCAPSError(error_message)
read_parameters_node.inputs.dartel_input = dartel_input
read_parameters_node.inputs.input_image = input_image
self.connect([
(read_parameters_node, self.input_node, [('dartel_input', 'dartel_input')]),
(read_parameters_node, self.input_node, [('input_image', 'input_image')])
raise ClinicaCAPSError(Fore.RED + '\n[Error] The CAPS directory you gave is not a folder.\n' + Fore.RESET
+ Fore.YELLOW + '\nError explanations:\n' + Fore.RESET
+ ' - Clinica expected the following path to be a folder:' + Fore.BLUE + caps_directory
+ Fore.RESET + '\n'
+ ' - If you gave relative path, did you run Clinica on the good folder?')
sub_folders = [item for item in listdir(caps_directory) if item.startswith('sub-')]
if len(sub_folders) > 0:
error_string = '\n[Error] Your CAPS directory contains at least one folder whose name ' \
+ 'starts with \'sub-\'. Check that you did not swap BIDS and CAPS folders.\n' \
+ ' Folder(s) found that match(es) BIDS architecture:\n'
for dir in sub_folders:
error_string += '\t' + dir + '\n'
error_string += 'A CAPS directory has a folder \'subjects\' at its root, in which are stored the output ' \
+ 'of the pipeline for each subject.'
raise ClinicaCAPSError(error_string)
raise ClinicaCAPSError(Fore.RED + '[Error] There is no \'groups\' folder in your CAPS directory. (Have you run t1-volume pipeline ?)' + Fore.RESET)
# Check that group already exists
if not os.path.exists(os.path.join(os.path.abspath(caps_directory), 'groups', 'group-' + group_id)):
error_message = group_id \
+ ' group does not exists, please choose an other one (or maybe you need to run t1-volume-create-dartel).' \
+ '\nGroups that already exist in your CAPS directory are: \n'
list_groups = os.listdir(os.path.join(os.path.abspath(caps_directory), 'groups'))
is_empty = True
for e in list_groups:
if e.startswith('group-'):
error_message += '\t' + e + ' \n'
is_empty = False
if is_empty is True:
error_message += 'NO GROUP FOUND'
raise ClinicaCAPSError(Fore.RED + '[Error] ' + error_message + Fore.RESET)
self._group_id = group_id
self._suvr_region = ''
self._fwhm = None
self._apply_pvc = False
if fwhm_tsv is not None:
if not os.path.isfile(fwhm_tsv):
raise FileNotFoundError('Could not find the fwhm_tsv file ' + str(fwhm_tsv))
try:
fwhm_df = read_csv(fwhm_tsv, sep='\t')
except (IOError, UnicodeDecodeError):
raise RuntimeError('An error while reading '
+ str(fwhm_tsv) + ' happened')
if fwhm_df.shape[0] != len(self.subjects):
check_caps_folder function checks the following items:
- caps_directory is a string
- the provided path exists and is a directory
- provided path is not a BIDS folder (BIDS and CAPS could be swapped by user). We simply check that there is
not a folder whose name starts with 'sub-' in the provided path (that exists in BIDS hierarchy)
Keep in mind that CAPS folder can be empty
"""
from os import listdir
import os
from colorama import Fore
from clinica.utils.exceptions import ClinicaCAPSError
assert isinstance(caps_directory, str), 'Argument you provided to check_caps_folder() is not a string.'
if not os.path.isdir(caps_directory):
raise ClinicaCAPSError(Fore.RED + '\n[Error] The CAPS directory you gave is not a folder.\n' + Fore.RESET
+ Fore.YELLOW + '\nError explanations:\n' + Fore.RESET
+ ' - Clinica expected the following path to be a folder:' + Fore.BLUE + caps_directory
+ Fore.RESET + '\n'
+ ' - If you gave relative path, did you run Clinica on the good folder?')
sub_folders = [item for item in listdir(caps_directory) if item.startswith('sub-')]
if len(sub_folders) > 0:
error_string = '\n[Error] Your CAPS directory contains at least one folder whose name ' \
+ 'starts with \'sub-\'. Check that you did not swap BIDS and CAPS folders.\n' \
+ ' Folder(s) found that match(es) BIDS architecture:\n'
for dir in sub_folders:
error_string += '\t' + dir + '\n'
error_string += 'A CAPS directory has a folder \'subjects\' at its root, in which are stored the output ' \
+ 'of the pipeline for each subject.'
raise ClinicaCAPSError(error_string)
current_pattern = join(caps_directory, '**/', pattern)
current_glob_found = insensitive_glob(current_pattern, recursive=True)
if len(current_glob_found) != 1 and raise_exception is True:
error_string = Fore.RED + '\n[Error] Clinica encountered a problem while getting ' + information['description'] + '. '
if len(current_glob_found) == 0:
error_string += 'No file was found'
else:
error_string += str(len(current_glob_found)) + ' files were found:'
for found_files in current_glob_found:
error_string += '\n\t' + found_files
error_string += (Fore.RESET + '\n\tCAPS directory: ' + caps_directory + '\n' + Fore.YELLOW
+ 'Please note that the following clinica pipeline(s) must have run to obtain these files: '
+ information['needed_pipeline'] + Fore.RESET + '\n')
raise ClinicaCAPSError(error_string)
return current_glob_found[0]
results.append(current_glob_found[0])
# We do not raise an error, so that the developper can gather all the problems before Clinica crashes
if len(error_encountered) > 0 and raise_exception is True:
error_message = Fore.RED + '\n[Error] Clinica encountered ' + str(len(error_encountered)) \
+ ' problem(s) while getting ' + information['description'] + ':\n' + Fore.RESET
if 'needed_pipeline' in information.keys():
if information['needed_pipeline']:
error_message += Fore.YELLOW + 'Please note that the following clinica pipeline(s) must have run ' \
'to obtain these files: ' + information['needed_pipeline'] + Fore.RESET + '\n'
for msg in error_encountered:
error_message += msg
if is_bids:
raise ClinicaBIDSError(error_message)
else:
raise ClinicaCAPSError(error_message)
return results
all_errors.append(e)
# Preprocessed bval
try:
bval_files = clinica_file_reader(self.subjects,
self.sessions,
self.caps_directory,
input_files.DWI_PREPROC_BVAL)
except ClinicaException as e:
all_errors.append(e)
if len(all_errors) > 0:
error_message = 'Clinica faced errors while trying to read files in your BIDS or CAPS directories.\n'
for msg in all_errors:
error_message += str(msg)
raise ClinicaCAPSError(error_message)
# Check space of DWI dataset
dwi_file_spaces = [re.search('.*_space-(.*)_preproc.nii.*', file, re.IGNORECASE).group(1) for file in dwi_files]
# Return an error if all the DWI files are not in the same space
if any(a != dwi_file_spaces[0] for a in dwi_file_spaces):
raise ClinicaCAPSError('Preprocessed DWI files are not all in the '
'same space. Please process them separately '
'using the appropriate subjects/sessions '
'`.tsv` file (-tsv option).')
# Used only for for T1-B0 registration
if dwi_file_spaces[0] == 'b0':
# Brain extracted T1w
t1_brain_files = clinica_file_reader(self.subjects,
self.sessions,