Example #1
0
def fdg_pet_paths_to_bids(images, bids_dir, dcm2niix="dcm2niix", dcm2nii="dcm2nii", mod_to_update=False):
    """

    Args:
        images:
        bids_dir:
        dcm2niix:
        dcm2nii:
        mod_to_update:

    Returns:

    """
    from clinica.iotools.converters.adni_to_bids import adni_utils
    import os
    from os import path
    import glob
    from clinica.utils.stream import cprint

    count = 0
    total = images.shape[0]

    for row in images.iterrows():
        image = row[1]
        subject = image.Subject_ID
        count += 1

        if image.Path == '':
            cprint('No path specified for ' + image.Subject_ID + ' in session ' + image.VISCODE)
            continue

        cprint('Processing subject ' + str(subject) + ' - session ' + image.VISCODE + ', ' + str(count) + ' / ' + str(total))

        session = adni_utils.viscode_to_session(image.VISCODE)
        image_path = image.Path

        # ADDED lines
        # ------------------
        image_id = image.Image_ID
        # If the original image is a DICOM, check if contains two DICOM inside the same folder
        if image.Is_Dicom:
            image_path = adni_utils.check_two_dcm_folder(image_path, bids_dir, image_id)
        # ------------------

        bids_subj = subject.replace('_', '')
        output_path = path.join(bids_dir, 'sub-ADNI' + bids_subj + '/ses-' + session + '/pet')
        output_filename = 'sub-ADNI' + bids_subj + '_ses-' + session + '_task-rest_acq-fdg_pet'

        # ADDED lines
        # ------------------
        # If updated mode is selected, check if an old FDG image is existing and remove it
        existing_fdg = glob.glob(path.join(output_path, output_filename + '*'))
        if mod_to_update and len(existing_fdg) > 0:
            cprint('Removing the old FDG image...')
            for fdg in existing_fdg:
                os.remove(fdg)
                # ------------------

        try:
            os.makedirs(output_path)
        except OSError:
            if not path.isdir(output_path):
                raise

        if not image.Is_Dicom:
            adni_utils.center_nifti_origin(image_path, path.join(output_path, output_filename + '.nii.gz'))
        else:
            command = dcm2niix + ' -b n -z n -o ' + output_path + ' -f ' + output_filename + ' ' + image_path
            os.system(command)
            nifti_file = path.join(output_path, output_filename + '.nii')
            output_image = nifti_file + '.gz'

            # Check if conversion worked (output file exists?)
            if not path.isfile(nifti_file):
                command = dcm2nii + ' -a n -d n -e n -i y -g n -p n -m n -r n -x n -o ' + output_path + ' ' + image_path
                os.system(command)
                nifti_file = path.join(output_path, subject.replace('_', '') + '.nii')
                output_image = path.join(output_path, output_filename + '.nii.gz')

                if not path.isfile(nifti_file):
                    cprint('DICOM to NIFTI conversion error for ' + image_path)
                    continue

            adni_utils.center_nifti_origin(nifti_file, output_image)
            os.remove(nifti_file)

    adni_utils.remove_tmp_dmc_folder(bids_dir)
Example #2
0
def create_file(image, modality, total, bids_dir, mod_to_update):
    import subprocess
    from colorama import Fore
    from clinica.utils.stream import cprint
    from clinica.iotools.converters.adni_to_bids import adni_utils
    from numpy import nan
    import os
    from os import path
    from glob import glob

    modality_specific = {'t1': {'output_path': 'anat',
                                'output_filename': '_T1w'},
                         'av45': {'output_path': 'pet',
                                  'output_filename': '_task-rest_acq-av45_pet'},
                         'fdg': {'output_path': 'pet',
                                 'output_filename': '_task-rest_acq-fdg_pet'}
                         }

    global counter

    with counter.get_lock():
        counter.value += 1

    subject = image.Subject_ID
    if image.Path == '':
        cprint(Fore.RED + '[' + modality.upper() + '] No path specified for '
               + image.Subject_ID + ' in session '
               + image.VISCODE + ' ' + str(counter.value)
               + ' / ' + str(total) + Fore.RESET)
        return nan
    cprint('[' + modality.upper() + '] Processing subject ' + str(subject)
           + ' - session ' + image.VISCODE + ', ' + str(counter.value)
           + ' / ' + str(total))
    session = adni_utils.viscode_to_session(image.VISCODE)
    image_path = image.Path
    image_id = image.Image_ID
    # If the original image is a DICOM, check if contains two DICOM
    # inside the same folder
    if image.Is_Dicom:
        image_path = adni_utils.check_two_dcm_folder(image_path,
                                                     bids_dir,
                                                     image_id)
    bids_subj = subject.replace('_', '')
    output_path = path.join(bids_dir, 'sub-ADNI' + bids_subj, 'ses-'
                            + session, modality_specific[modality]['output_path'])
    output_filename = 'sub-ADNI' + bids_subj + '_ses-' + session + modality_specific[modality]['output_filename']

    # If updated mode is selected, check if an old image is existing and remove it
    existing_im = glob(path.join(output_path, output_filename + '*'))
    if mod_to_update and len(existing_im) > 0:
        print('Removing the old image...')
        for im in existing_im:
            os.remove(im)

    try:
        os.makedirs(output_path)
    except OSError:
        # Folder already created with previous instance
        pass

    if image.Is_Dicom:
        command = 'dcm2niix -b n -z n -o ' + output_path + ' -f ' + output_filename + ' ' + image_path
        subprocess.run(command,
                       shell=True,
                       stderr=subprocess.DEVNULL,
                       stdout=subprocess.DEVNULL)
        nifti_file = path.join(output_path, output_filename + '.nii')
        output_image = nifti_file + '.gz'

        # Check if conversion worked (output file exists?)
        if not path.isfile(nifti_file):
            cprint('\tConversion with dcm2niix failed, trying with dcm2nii')
            command = 'dcm2nii -a n -d n -e n -i y -g n -p n -m n -r n -x n -o ' + output_path + ' ' + image_path
            subprocess.run(command,
                           shell=True,
                           stdout=subprocess.DEVNULL,
                           stderr=subprocess.DEVNULL)
            nifti_file = path.join(output_path,
                                   subject.replace('_', '') + '.nii')
            output_image = path.join(output_path,
                                     output_filename + '.nii.gz')

            if not path.isfile(nifti_file):
                cprint('DICOM to NIFTI conversion error for ' + image_path)
                return nan

        adni_utils.center_nifti_origin(nifti_file, output_image)
        os.remove(nifti_file)

    else:
        output_image = path.join(output_path, output_filename + '.nii.gz')
        adni_utils.center_nifti_origin(image_path, output_image)

    # Check if there is still the folder tmp_dcm_folder and remove it
    adni_utils.remove_tmp_dmc_folder(bids_dir, image_id)
    return output_image
Example #3
0
def av45_pet_paths_to_bids(images, bids_dir, dcm2niix="dcm2niix", dcm2nii="dcm2nii", mod_to_update=False):
    """
    Read the paths generated by the method compute_av45_pet and convert the original images to the BIDS standard

    Args:
        images: the dataframe returned by the method compute_av45_pet
        bids_dir: path to the BIDS directory
        dcm2niix: path to the dcm2niix tool (not useful anymore)
        dcm2nii: path to the dcm2nii tool (not useful anymore

    """
    import os
    import glob
    from numpy import nan
    from clinica.iotools.converters.adni_to_bids import adni_utils
    from clinica.utils.stream import cprint

    count = 0
    total = images.shape[0]

    for row in images.iterrows():
        image = row[1]
        subject = image.Subject_ID
        count += 1

        if image.Path is nan:
            cprint('No path specified for ' + image.Subject_ID + ' in session ' + image.VISCODE)
            continue

        cprint('Processing subject ' + str(subject) + ' - session ' + image.VISCODE + ', ' + str(count) + ' / ' + str(total))

        session = adni_utils.viscode_to_session(image.VISCODE)
        image_path = image.Path

        # ADDED lines
        # ------------------
        image_id = image.Image_ID
        # If the original image is a DICOM, check if contains two DICOM inside the same folder
        if image.Is_Dicom:
            image_path = adni_utils.check_two_dcm_folder(image_path, bids_dir, image_id)
        # ------------------

        bids_subj = subject.replace('_', '')
        output_path = os.path.join(bids_dir, 'sub-ADNI' + bids_subj +'/ses-' + session + '/pet')
        output_filename = 'sub-ADNI' + bids_subj + '_ses-' + session + '_task-rest_acq-av45_pet'

        # ADDED lines
        # ------------------
        # If updated mode is selected, check if an old AV45 image is existing and remove it
        existing_av45 = glob.glob(os.path.join(output_path, output_filename + '*'))
        if mod_to_update and len(existing_av45) > 0:
            cprint('Removing the old AV45 image...')
            for av45 in existing_av45:
                os.remove(av45)
                # ------------------

        try:
            os.makedirs(output_path)
        except OSError:
            if not os.path.isdir(output_path):
                raise

        if not image.Is_Dicom:
            adni_utils.center_nifti_origin(image_path, os.path.join(output_path, output_filename + '.nii.gz'))
        else:
            command = dcm2niix + ' -b n -z n -o ' + output_path + ' -f ' + output_filename + ' ' + image_path
            os.system(command)
            nifti_file = os.path.join(output_path, output_filename + '.nii')
            output_image = nifti_file + '.gz'

            # Check if conversion worked (output file exists?)
            if not os.path.isfile(nifti_file):
                cprint('Conversion with dcm2niix failed, trying with dcm2nii')
                command = dcm2nii + ' -a n -d n -e n -i y -g n -p n -m n -r n -x n -o ' + output_path + ' ' + image_path
                os.system(command)
                nifti_file = os.path.join(output_path, subject.replace('_', '') + '.nii')
                output_image = os.path.join(output_path, output_filename + '.nii.gz')

                if not os.path.isfile(nifti_file):
                    cprint('DICOM to NIFTI conversion error for ' + image_path)
                    continue

            adni_utils.center_nifti_origin(nifti_file, output_image)
            os.remove(nifti_file)

    adni_utils.remove_tmp_dmc_folder(bids_dir)
Example #4
0
def t1_paths_to_bids(images,
                     bids_dir,
                     dcm2niix="dcm2niix",
                     dcm2nii="dcm2nii",
                     mod_to_update=False):
    import os
    from os import path
    from numpy import nan
    from clinica.iotools.converters.adni_to_bids import adni_utils
    from glob import glob
    from clinica.utils.stream import cprint

    count = 0
    total = images.shape[0]

    for row in images.iterrows():
        image = row[1]
        subject = image.Subject_ID
        count += 1

        if image.Path is nan:
            cprint('No path specified for ' + image.Subject_ID +
                   ' in session ' + image.VISCODE)
            continue
            cprint('Processing subject ' + str(subject) + ' - session ' +
                   image.VISCODE + ', ' + str(count) + ' / ' + str(total))

        session = adni_utils.viscode_to_session(image.VISCODE)
        image_path = image.Path
        # ADDED lines
        # ------------------
        image_id = image.Image_ID
        # If the original image is a DICOM, check if contains two DICOM inside the same folder
        if image.Is_Dicom:
            image_path = adni_utils.check_two_dcm_folder(
                image_path, bids_dir, image_id)
        # ------------------

        bids_subj = subject.replace('_', '')
        output_path = path.join(bids_dir, 'sub-ADNI' + bids_subj,
                                'ses-' + session, 'anat')
        output_filename = 'sub-ADNI' + bids_subj + '_ses-' + session + '_T1w'

        # ADDED lines
        # ------------------
        # If updated mode is selected, check if an old T1 image is existing and remove it
        existing_t1 = glob(path.join(output_path, output_filename + '*'))

        if mod_to_update and len(existing_t1) > 0:
            print 'Removing the old T1 image...'
            for t1 in existing_t1:
                os.remove(t1)
        # ------------------

        try:
            os.makedirs(output_path)
        except OSError:
            if not path.isdir(output_path):
                raise

        if image.Is_Dicom:
            command = dcm2niix + ' -b n -z n -o ' + output_path + ' -f ' + output_filename + ' ' + image_path
            os.system(command)
            nifti_file = path.join(output_path, output_filename + '.nii')
            output_image = nifti_file + '.gz'

            # Check if conversion worked (output file exists?)
            if not path.isfile(nifti_file):
                cprint('Conversion with dcm2niix failed, trying with dcm2nii')
                command = dcm2nii + ' -a n -d n -e n -i y -g n -p n -m n -r n -x n -o ' + output_path + ' ' + image_path
                os.system(command)
                nifti_file = path.join(output_path,
                                       subject.replace('_', '') + '.nii')
                output_image = path.join(output_path,
                                         output_filename + '.nii.gz')

                if not path.isfile(nifti_file):
                    # TODO - LOG THIS
                    cprint('DICOM to NIFTI conversion error for ' + image_path)
                    continue

            adni_utils.center_nifti_origin(nifti_file, output_image)
            os.remove(nifti_file)

        else:
            output_image = path.join(output_path, output_filename + '.nii.gz')
            adni_utils.center_nifti_origin(image_path, output_image)

    # Check if there is still the folder tmp_dcm_folder and remove it
    adni_utils.remove_tmp_dmc_folder(bids_dir)