def init_input_node(t1w, recon_all_args, output_dir): """Initialize the pipeline. This function will: - Extract <image_id> (e.g. sub-CLNC01_ses-M00) T1w filename; - Check FOV of T1w; - Create SUBJECTS_DIR for recon-all (otherwise, the command won't run); - Print begin execution message. """ import os import errno from clinica.utils.filemanip import get_subject_id from clinica.utils.freesurfer import check_flags from clinica.utils.ux import print_begin_image # Extract <image_id> image_id = get_subject_id(t1w) # Check flags for T1w flags = check_flags(t1w, recon_all_args) # Create SUBJECTS_DIR for recon-all (otherwise, the command won't run) subjects_dir = os.path.join(output_dir, image_id) try: os.makedirs(subjects_dir) except OSError as e: if e.errno != errno.EEXIST: # EEXIST: folder already exists raise e print_begin_image(image_id, ['ReconAllArgs'], [flags]) return image_id, t1w, flags, subjects_dir
def init_input_node(pet_nii): import datetime import nibabel as nib from colorama import Fore from clinica.utils.filemanip import get_subject_id from clinica.utils.stream import cprint from clinica.utils.ux import print_begin_image # Extract image ID image_id = get_subject_id(pet_nii) # Check that the PET file is a 3D volume img = nib.load(pet_nii) if len(img.shape) == 4: now = datetime.datetime.now().strftime("%H:%M:%S") error_msg = ( f"{Fore.RED}[{now}] Error: Clinica does not handle 4D volumes " f"for {image_id.replace('_', ' | ')}{Fore.RESET}") cprint(error_msg) raise NotImplementedError(error_msg) # Print begin message print_begin_image(image_id) return pet_nii
def init_input_node(t1w, recon_all_args, output_dir): """Initialize the pipeline. This function will: - Extract <image_id> (e.g. sub-CLNC01_ses-M00) T1w filename; - Check FOV of T1w; - Create SUBJECTS_DIR for recon-all (otherwise, the command won't run); - Print begin execution message. """ import os from clinica.utils.filemanip import get_subject_id from clinica.utils.freesurfer import check_flags from clinica.utils.ux import print_begin_image # Extract <image_id> image_id = get_subject_id(t1w) # Check flags for T1w flags = check_flags(t1w, recon_all_args) # Create SUBJECTS_DIR for recon-all (otherwise, the command won't run) subjects_dir = os.path.join(output_dir, image_id) os.makedirs(subjects_dir, exist_ok=True) print_begin_image(image_id, ["ReconAllArgs"], [flags]) return image_id, t1w, flags, subjects_dir
def atlas_statistics(in_image, atlas_list): """ For each atlas name provided it calculates for the input image the mean for each region in the atlas and saves it to a TSV file. Args: in_image: A Nifti image atlas_list: List of names of atlas to be applied Returns: List of paths to TSV files """ from os.path import abspath, join from nipype.utils.filemanip import split_filename from clinica.utils.atlas import AtlasAbstract from clinica.utils.statistics import statistics_on_atlas from clinica.utils.filemanip import get_subject_id from clinica.utils.ux import print_end_image subject_id = get_subject_id(in_image) orig_dir, base, ext = split_filename(in_image) atlas_classes = AtlasAbstract.__subclasses__() atlas_statistics_list = [] for atlas in atlas_list: for atlas_class in atlas_classes: if atlas_class.get_name_atlas() == atlas: out_atlas_statistics = abspath( join('./' + base + '_space-' + atlas + '_map-graymatter_statistics.tsv')) statistics_on_atlas(in_image, atlas_class(), out_atlas_statistics) atlas_statistics_list.append(out_atlas_statistics) print_end_image(subject_id) return atlas_statistics_list
def print_end_pipeline(t1w, final_file): """ Display end message for <subject_id> when <final_file> is connected. """ from clinica.utils.ux import print_end_image from clinica.utils.filemanip import get_subject_id print_end_image(get_subject_id(t1w))
def init_input_node(t1w, dwi, bvec, bval, dwi_json): """Initialize the pipeline.""" from clinica.utils.dwi import bids_dir_to_fsl_dir, check_dwi_volume from clinica.utils.filemanip import extract_metadata_from_json, get_subject_id from clinica.utils.ux import print_begin_image # Extract image ID image_id = get_subject_id(t1w) # Check that the number of DWI, bvec & bval are the same check_dwi_volume(dwi, bvec, bval) # Read metadata from DWI JSON file: [total_readout_time, phase_encoding_direction] = extract_metadata_from_json( dwi_json, ["TotalReadoutTime", "PhaseEncodingDirection"] ) phase_encoding_direction = bids_dir_to_fsl_dir(phase_encoding_direction) # Print begin message print_begin_image( image_id, ["TotalReadoutTime", "PhaseEncodingDirection"], [str(total_readout_time), phase_encoding_direction], ) return ( image_id, t1w, dwi, bvec, bval, total_readout_time, phase_encoding_direction, )
def init_input_node(pet): from clinica.utils.filemanip import get_subject_id from clinica.utils.ux import print_begin_image # Extract image ID image_id = get_subject_id(pet) print_begin_image(image_id) return pet
def init_input_node(t1w): """Extract "sub-<participant_id>_ses-<session_label>" from <t1w> and print begin message.""" from clinica.utils.filemanip import get_subject_id from clinica.utils.ux import print_begin_image subject_id = get_subject_id(t1w) print_begin_image(subject_id) return subject_id, t1w
def init_input_node(pet_nii): import nibabel as nib from clinica.utils.filemanip import get_subject_id from clinica.utils.stream import cprint from clinica.utils.ux import print_begin_image # Extract image ID image_id = get_subject_id(pet_nii) # Check that the PET file is a 3D volume img = nib.load(pet_nii) if len(img.shape) == 4: error_msg = f"Clinica does not handle 4D volumes for {image_id.replace('_', ' | ')}" cprint(error_msg, lvl="error") raise NotImplementedError(error_msg) # Print begin message print_begin_image(image_id) return pet_nii
def print_begin_pipeline(in_bids_or_caps_file: str) -> None: from clinica.utils.filemanip import get_subject_id from clinica.utils.ux import print_begin_image print_begin_image(get_subject_id(in_bids_or_caps_file))
def init_input_node(dwi, bvec, bval, dwi_json, fmap_magnitude, fmap_phasediff, fmap_phasediff_json): """Initialize pipeline (read JSON, check files and print begin message).""" import datetime import nibabel as nib from clinica.utils.dwi import bids_dir_to_fsl_dir, check_dwi_volume from clinica.utils.filemanip import extract_metadata_from_json, get_subject_id from clinica.utils.stream import cprint from clinica.utils.ux import print_begin_image # Extract image ID image_id = get_subject_id(dwi) # Check that the number of DWI, bvec & bval are the same try: check_dwi_volume(dwi, bvec, bval) except ValueError as e: now = datetime.datetime.now().strftime("%H:%M:%S") error_msg = ( f"[{now}] Error: Number of DWIs, b-vals and b-vecs mismatch for {image_id.replace('_', ' | ')}" ) cprint(error_msg, lvl="error") raise ValueError(e) # Check that PhaseDiff and magnitude1 have the same header # Otherwise, FSL in FugueExtrapolationFromMask will crash img_phasediff = nib.load(fmap_phasediff) img_magnitude = nib.load(fmap_magnitude) if img_phasediff.shape != img_magnitude.shape: now = datetime.datetime.now().strftime("%H:%M:%S") error_msg = ( f"[{now}] Error: Headers of PhaseDiff and Magnitude1 are not the same " f"for {image_id.replace('_', ' | ')} ({img_phasediff.shape} vs {img_magnitude.shape})" ) cprint(error_msg, lvl="error") raise NotImplementedError(error_msg) # Read metadata from DWI JSON file: [total_readout_time, phase_encoding_direction] = extract_metadata_from_json( dwi_json, ["TotalReadoutTime", "PhaseEncodingDirection"]) phase_encoding_direction = bids_dir_to_fsl_dir(phase_encoding_direction) # Read metadata from PhaseDiff JSON file: [echo_time1, echo_time2] = extract_metadata_from_json(fmap_phasediff_json, ["EchoTime1", "EchoTime2"]) delta_echo_time = abs(echo_time2 - echo_time1) # Print begin message print_begin_image( image_id, ["TotalReadoutTime", "PhaseEncodingDirection", "DeltaEchoTime"], [ str(total_readout_time), phase_encoding_direction, str(delta_echo_time) ], ) return ( image_id, dwi, bvec, bval, total_readout_time, phase_encoding_direction, fmap_magnitude, fmap_phasediff, delta_echo_time, )
def print_end_pipeline(in_bids_or_caps_file, final_file_1, final_file_2): from clinica.utils.filemanip import get_subject_id from clinica.utils.ux import print_end_image print_end_image(get_subject_id(in_bids_or_caps_file))