def build_input_node(self): """Build and connect an input node to the pipeline. """ from os import pardir from os.path import dirname, join, abspath, exists from colorama import Fore import nipype.interfaces.utility as nutil import nipype.pipeline.engine as npe from clinica.utils.exceptions import ClinicaBIDSError, ClinicaException from clinica.utils.filemanip import extract_subjects_sessions_from_filename from clinica.utils.inputs import clinica_file_reader from clinica.utils.input_files import T1W_NII from clinica.utils.inputs import fetch_file, RemoteFileStructure from clinica.utils.ux import print_images_to_process from clinica.utils.stream import cprint root = dirname(abspath(join(abspath(__file__), pardir, pardir))) path_to_mask = join(root, 'resources', 'masks') url_aramis = 'https://aramislab.paris.inria.fr/files/data/img_t1_linear/' FILE1 = RemoteFileStructure( filename='ref_cropped_template.nii.gz', url=url_aramis, checksum= '67e1e7861805a8fd35f7fcf2bdf9d2a39d7bcb2fd5a201016c4d2acdd715f5b3') FILE2 = RemoteFileStructure( filename='mni_icbm152_t1_tal_nlin_sym_09c.nii', url=url_aramis, checksum= '93359ab97c1c027376397612a9b6c30e95406c15bf8695bd4a8efcb2064eaa34') self.ref_template = join(path_to_mask, FILE2.filename) self.ref_crop = join(path_to_mask, FILE1.filename) if not (exists(self.ref_template)): try: fetch_file(FILE2, path_to_mask) except IOError as err: cprint( 'Unable to download required template (mni_icbm152) for processing:', err) if not (exists(self.ref_crop)): try: fetch_file(FILE1, path_to_mask) except IOError as err: cprint( 'Unable to download required template (ref_crop) for processing:', err) # Display image(s) already present in CAPS folder # =============================================== processed_ids = self.get_processed_images(self.caps_directory, self.subjects, self.sessions) if len(processed_ids) > 0: cprint( "%sClinica found %s image(s) already processed in CAPS directory:%s" % (Fore.YELLOW, len(processed_ids), Fore.RESET)) for image_id in processed_ids: cprint("%s\t%s%s" % (Fore.YELLOW, image_id.replace('_', ' | '), Fore.RESET)) cprint("%s\nImage(s) will be ignored by Clinica.\n%s" % (Fore.YELLOW, Fore.RESET)) input_ids = [ p_id + '_' + s_id for p_id, s_id in zip(self.subjects, self.sessions) ] to_process_ids = list(set(input_ids) - set(processed_ids)) self.subjects, self.sessions = extract_subjects_sessions_from_filename( to_process_ids) # Inputs from anat/ folder # ======================== # T1w file: try: t1w_files = clinica_file_reader(self.subjects, self.sessions, self.bids_directory, T1W_NII) except ClinicaException as e: err = 'Clinica faced error(s) while trying to read files in your BIDS directory.\n' + str( e) raise ClinicaBIDSError(err) if len(self.subjects): print_images_to_process(self.subjects, self.sessions) cprint('The pipeline will last approximately 6 minutes per image.') read_node = npe.Node( name="ReadingFiles", iterables=[ ('t1w', t1w_files), ], synchronize=True, interface=nutil.IdentityInterface(fields=self.get_input_fields())) self.connect([ (read_node, self.input_node, [('t1w', 't1w')]), ])
def build_input_node(self): """Build and connect an input node to the pipeline.""" import os import nipype.interfaces.utility as nutil import nipype.pipeline.engine as npe from clinica.utils.exceptions import ClinicaCAPSError, ClinicaException from clinica.utils.filemanip import extract_subjects_sessions_from_filename from clinica.utils.input_files import T1_FS_DESTRIEUX from clinica.utils.inputs import clinica_file_reader from clinica.utils.longitudinal import ( get_long_id, get_participants_long_id, read_sessions, ) from clinica.utils.participant import ( get_unique_subjects, unique_subjects_sessions_to_subjects_sessions, ) from clinica.utils.stream import cprint from .longitudinal_utils import ( extract_participant_long_ids_from_filename, save_part_sess_long_ids_to_tsv, ) # Display image(s) already present in CAPS folder # =============================================== output_ids = self.get_processed_images( self.caps_directory, self.subjects, self.sessions ) ( processed_participants, processed_long_sessions, ) = extract_participant_long_ids_from_filename(output_ids) if len(processed_participants) > 0: cprint( msg=( f"Clinica found {len(processed_participants)} participant(s) " "already processed in CAPS directory:" ), lvl="warning", ) for p_id, l_id in zip(processed_participants, processed_long_sessions): cprint(f"{p_id} | {l_id}", lvl="warning") if self.overwrite_caps: output_folder = "<CAPS>/subjects/<participant_id>/<long_id>/freesurfer_unbiased_template/" cprint(f"Output folders in {output_folder} will be recreated.", lvl="warning") else: cprint("Participant(s) will be ignored by Clinica.", lvl="warning") input_ids = [ p_id + "_" + s_id for p_id, s_id in zip(self.subjects, self.sessions) ] processed_sessions_per_participant = [ read_sessions(self.caps_directory, p_id, l_id) for (p_id, l_id) in zip( processed_participants, processed_long_sessions ) ] participants, sessions = unique_subjects_sessions_to_subjects_sessions( processed_participants, processed_sessions_per_participant ) processed_ids = [ p_id + "_" + s_id for p_id, s_id in zip(participants, sessions) ] to_process_ids = list(set(input_ids) - set(processed_ids)) self.subjects, self.sessions = extract_subjects_sessions_from_filename( to_process_ids ) # Check that t1-freesurfer has run on the CAPS directory try: clinica_file_reader( self.subjects, self.sessions, self.caps_directory, T1_FS_DESTRIEUX ) except ClinicaException as e: err_msg = ( "Clinica faced error(s) while trying to read files in your CAPS directory.\n" + str(e) ) raise ClinicaCAPSError(err_msg) # Save subjects to process in <WD>/<Pipeline.name>/participants.tsv folder_participants_tsv = os.path.join(self.base_dir, self.name) long_ids = get_participants_long_id(self.subjects, self.sessions) save_part_sess_long_ids_to_tsv( self.subjects, self.sessions, long_ids, folder_participants_tsv ) [list_participant_id, list_list_session_ids] = get_unique_subjects( self.subjects, self.sessions ) list_long_id = [ get_long_id(list_session_ids) for list_session_ids in list_list_session_ids ] def print_images_to_process( unique_part_list, per_part_session_list, list_part_long_id ): cprint( f"The pipeline will be run on the following {len(unique_part_list)} participant(s):" ) for (part_id, list_sess_id, list_id) in zip( unique_part_list, per_part_session_list, list_part_long_id ): sessions_participant = ", ".join(s_id for s_id in list_sess_id) cprint(f"\t{part_id} | {sessions_participant} | {list_id}") if len(self.subjects): # TODO: Generalize long IDs to the message display print_images_to_process( list_participant_id, list_list_session_ids, list_long_id ) cprint( "List available in %s" % os.path.join(folder_participants_tsv, "participants.tsv") ) cprint("The pipeline will last approximately 10 hours per participant.") read_node = npe.Node( name="ReadingFiles", iterables=[ ("participant_id", list_participant_id), ("list_session_ids", list_list_session_ids), ], synchronize=True, interface=nutil.IdentityInterface(fields=self.get_input_fields()), ) # fmt: off self.connect( [ (read_node, self.input_node, [("participant_id", "participant_id")]), (read_node, self.input_node, [("list_session_ids", "list_session_ids")]), ] )
def build_input_node(self): """Build and connect an input node to the pipeline.""" import os from colorama import Fore import nipype.interfaces.utility as nutil import nipype.pipeline.engine as npe from clinica.utils.exceptions import ClinicaException, ClinicaCAPSError from clinica.utils.filemanip import extract_subjects_sessions_from_filename from clinica.utils.inputs import clinica_file_reader from clinica.utils.input_files import T1_FS_DESTRIEUX from clinica.utils.longitudinal import get_long_id, read_sessions, get_participants_long_id from clinica.utils.participant import get_unique_subjects, unique_subjects_sessions_to_subjects_sessions from clinica.utils.stream import cprint from .longitudinal_utils import extract_participant_long_ids_from_filename, save_part_sess_long_ids_to_tsv # Display image(s) already present in CAPS folder # =============================================== output_ids = self.get_processed_images(self.caps_directory, self.subjects, self.sessions) processed_participants, processed_long_sessions = extract_participant_long_ids_from_filename( output_ids) if len(processed_participants) > 0: cprint( "%sClinica found %s participant(s) already processed in CAPS directory:%s" % (Fore.YELLOW, len(processed_participants), Fore.RESET)) for p_id, l_id in zip(processed_participants, processed_long_sessions): cprint("%s\t%s | %s%s" % (Fore.YELLOW, p_id, l_id, Fore.RESET)) if self.overwrite_caps: output_folder = "<CAPS>/subjects/<participant_id>/<long_id>/freesurfer_unbiased_template/" cprint("%s\nOutput folders in %s will be recreated.\n%s" % (Fore.YELLOW, output_folder, Fore.RESET)) else: cprint("%s\nParticipant(s) will be ignored by Clinica.\n%s" % (Fore.YELLOW, Fore.RESET)) input_ids = [ p_id + '_' + s_id for p_id, s_id in zip(self.subjects, self.sessions) ] processed_sessions_per_participant = [ read_sessions(self.caps_directory, p_id, l_id) for (p_id, l_id) in zip(processed_participants, processed_long_sessions) ] participants, sessions = unique_subjects_sessions_to_subjects_sessions( processed_participants, processed_sessions_per_participant) processed_ids = [ p_id + '_' + s_id for p_id, s_id in zip(participants, sessions) ] to_process_ids = list(set(input_ids) - set(processed_ids)) self.subjects, self.sessions = extract_subjects_sessions_from_filename( to_process_ids) # Check that t1-freesurfer has run on the CAPS directory try: clinica_file_reader(self.subjects, self.sessions, self.caps_directory, T1_FS_DESTRIEUX) except ClinicaException as e: err_msg = 'Clinica faced error(s) while trying to read files in your CAPS directory.\n' + str( e) raise ClinicaCAPSError(err_msg) # Save subjects to process in <WD>/<Pipeline.name>/participants.tsv folder_participants_tsv = os.path.join(self.base_dir, self.name) long_ids = get_participants_long_id(self.subjects, self.sessions) save_part_sess_long_ids_to_tsv(self.subjects, self.sessions, long_ids, folder_participants_tsv) [list_participant_id, list_list_session_ids] = get_unique_subjects(self.subjects, self.sessions) list_long_id = [ get_long_id(list_session_ids) for list_session_ids in list_list_session_ids ] def print_images_to_process(unique_part_list, per_part_session_list, list_part_long_id): cprint( 'The pipeline will be run on the following %s participant(s):' % len(unique_part_list)) for (part_id, list_sess_id, list_id) in zip(unique_part_list, per_part_session_list, list_part_long_id): sessions_participant = ', '.join(s_id for s_id in list_sess_id) cprint("\t%s | %s | %s" % (part_id, sessions_participant, list_id)) if len(self.subjects): # TODO: Generalize long IDs to the message display print_images_to_process(list_participant_id, list_list_session_ids, list_long_id) cprint('List available in %s' % os.path.join(folder_participants_tsv, 'participants.tsv')) cprint( 'The pipeline will last approximately 10 hours per participant.' ) read_node = npe.Node( name="ReadingFiles", iterables=[ ('participant_id', list_participant_id), ('list_session_ids', list_list_session_ids), ], synchronize=True, interface=nutil.IdentityInterface(fields=self.get_input_fields())) self.connect([ (read_node, self.input_node, [('participant_id', 'participant_id') ]), (read_node, self.input_node, [('list_session_ids', 'list_session_ids')]), ])
def build_input_node(self): """Build and connect an input node to the pipeline. Raise: ClinicaBIDSError: If there are duplicated files or missing files for any subject """ import os import nipype.interfaces.utility as nutil import nipype.pipeline.engine as npe from colorama import Fore from clinica.iotools.utils.data_handling import ( check_volume_location_in_world_coordinate_system, ) from clinica.utils.exceptions import ClinicaBIDSError, ClinicaException from clinica.utils.filemanip import ( extract_subjects_sessions_from_filename, save_participants_sessions, ) from clinica.utils.input_files import T1W_NII from clinica.utils.inputs import clinica_file_reader from clinica.utils.stream import cprint from clinica.utils.ux import print_images_to_process # Display image(s) already present in CAPS folder # =============================================== processed_ids = self.get_processed_images(self.caps_directory, self.subjects, self.sessions) if len(processed_ids) > 0: cprint(f"{Fore.YELLOW}Clinica found {len(processed_ids)} image(s) " f"already processed in CAPS directory:{Fore.RESET}") for image_id in processed_ids: cprint( f"{Fore.YELLOW}\t{image_id.replace('_', ' | ')}{Fore.RESET}" ) if self.overwrite_caps: output_folder = "<CAPS>/subjects/<participant_id>/<session_id>/t1/freesurfer_cross_sectional" cprint( f"{Fore.YELLOW}\nOutput folders in {output_folder} will be recreated.\n{Fore.RESET}" ) else: cprint( f"{Fore.YELLOW}\nImage(s) will be ignored by Clinica.\n{Fore.RESET}" ) input_ids = [ p_id + "_" + s_id for p_id, s_id in zip(self.subjects, self.sessions) ] to_process_ids = list(set(input_ids) - set(processed_ids)) self.subjects, self.sessions = extract_subjects_sessions_from_filename( to_process_ids) # Inputs from anat/ folder # ======================== # T1w file: try: t1w_files = clinica_file_reader(self.subjects, self.sessions, self.bids_directory, T1W_NII) except ClinicaException as e: err_msg = ( "Clinica faced error(s) while trying to read files in your BIDS directory.\n" + str(e)) raise ClinicaBIDSError(err_msg) # Save subjects to process in <WD>/<Pipeline.name>/participants.tsv folder_participants_tsv = os.path.join(self.base_dir, self.name) save_participants_sessions(self.subjects, self.sessions, folder_participants_tsv) if len(self.subjects): print_images_to_process(self.subjects, self.sessions) cprint("List available in %s" % os.path.join(folder_participants_tsv, "participants.tsv")) cprint("The pipeline will last approximately 10 hours per image.") read_node = npe.Node( name="ReadingFiles", iterables=[ ("t1w", t1w_files), ], synchronize=True, interface=nutil.IdentityInterface(fields=self.get_input_fields()), ) check_volume_location_in_world_coordinate_system( t1w_files, self.bids_directory) self.connect([ (read_node, self.input_node, [("t1w", "t1w")]), ])
def build_input_node(self): """Build and connect an input node to the pipeline.""" from os import pardir from os.path import abspath, dirname, exists, join import nipype.interfaces.utility as nutil import nipype.pipeline.engine as npe from clinica.utils.exceptions import ClinicaBIDSError, ClinicaException from clinica.utils.filemanip import extract_subjects_sessions_from_filename from clinica.utils.input_files import T1W_NII from clinica.utils.inputs import ( RemoteFileStructure, clinica_file_reader, fetch_file, ) from clinica.utils.stream import cprint from clinica.utils.ux import print_images_to_process root = dirname(abspath(join(abspath(__file__), pardir, pardir))) path_to_mask = join(root, "resources", "masks") url_aramis = "https://aramislab.paris.inria.fr/files/data/img_t1_linear/" FILE1 = RemoteFileStructure( filename="ref_cropped_template.nii.gz", url=url_aramis, checksum= "67e1e7861805a8fd35f7fcf2bdf9d2a39d7bcb2fd5a201016c4d2acdd715f5b3", ) FILE2 = RemoteFileStructure( filename="mni_icbm152_t1_tal_nlin_sym_09c.nii", url=url_aramis, checksum= "93359ab97c1c027376397612a9b6c30e95406c15bf8695bd4a8efcb2064eaa34", ) self.ref_template = join(path_to_mask, FILE2.filename) self.ref_crop = join(path_to_mask, FILE1.filename) if not (exists(self.ref_template)): try: fetch_file(FILE2, path_to_mask) except IOError as err: cprint( msg= f"Unable to download required template (mni_icbm152) for processing: {err}", lvl="error", ) if not (exists(self.ref_crop)): try: fetch_file(FILE1, path_to_mask) except IOError as err: cprint( msg= f"Unable to download required template (ref_crop) for processing: {err}", lvl="error", ) # Display image(s) already present in CAPS folder # =============================================== processed_ids = self.get_processed_images(self.caps_directory, self.subjects, self.sessions) if len(processed_ids) > 0: cprint( msg= f"Clinica found {len(processed_ids)} image(s) already processed in CAPS directory:", lvl="warning", ) for image_id in processed_ids: cprint(msg=f"{image_id.replace('_', ' | ')}", lvl="warning") cprint(msg=f"Image(s) will be ignored by Clinica.", lvl="warning") input_ids = [ p_id + "_" + s_id for p_id, s_id in zip(self.subjects, self.sessions) ] to_process_ids = list(set(input_ids) - set(processed_ids)) self.subjects, self.sessions = extract_subjects_sessions_from_filename( to_process_ids) # Inputs from anat/ folder # ======================== # T1w file: try: t1w_files = clinica_file_reader(self.subjects, self.sessions, self.bids_directory, T1W_NII) except ClinicaException as e: err = ( "Clinica faced error(s) while trying to read files in your BIDS directory.\n" + str(e)) raise ClinicaBIDSError(err) if len(self.subjects): print_images_to_process(self.subjects, self.sessions) cprint("The pipeline will last approximately 6 minutes per image.") read_node = npe.Node( name="ReadingFiles", iterables=[ ("t1w", t1w_files), ], synchronize=True, interface=nutil.IdentityInterface(fields=self.get_input_fields()), ) self.connect([ (read_node, self.input_node, [("t1w", "t1w")]), ])