Ejemplo n.º 1
0
    def wrapper_multiprocessing(self, fileIDs, subjects, modality):
        """To avoid redundancy between CT and MR  registration to common space, this part is wrapped up here """

        working_dir = self.cfg['folders']['nifti']
        prefix = '{}2template'.format(modality)

        # Start multiprocessing framework
        start_multi = time.time()
        status = mp.Queue()
        processes = [
            mp.Process(target=self.ANTsCoregisterMultiprocessing,
                       args=(filename_fixed, filename_moving, no_subj,
                             os.path.join(working_dir, no_subj),
                             'registration', prefix, status))
            for filename_fixed, filename_moving, no_subj in fileIDs
        ]

        for p in processes:
            p.start()

        while any([p.is_alive() for p in processes]):
            while not status.empty():
                filename_fixed, filename_moving, no_subj = status.get()
                print("\tRegistering {} (f) to {} (m), in ANTsPy\n".format(
                    filename_fixed,
                    os.path.split(filename_moving)[1], no_subj))
            time.sleep(0.1)

        for p in processes:
            p.join()

        # Functions creating/updating pipeline log, which documents all steps along with settings
        for subjID in subjects:
            files_processed = [(os.path.split(file_moving)[1],
                                os.path.split(file_fixed)[1])
                               for file_fixed, file_moving, subj_no in fileIDs
                               if subj_no == subjID]
            log_text = "{} successfully registered (@{}) to \n{}, \n\n Mean Duration per subject: {:.2f} " \
                       "secs".format(files_processed[0][0], time.strftime("%Y%m%d-%H%M%S"), files_processed[0][1],
                                     (time.time() - start_multi) / len(subjects))
            Output.logging_routine(text=Output.split_lines(log_text),
                                   cfg=self.cfg,
                                   subject=str(subjID),
                                   module='{}-Registration'.format(modality),
                                   opt=self.cfg['preprocess']['registration'],
                                   project="")

        print(
            '\nIn total, a list of {} subject(s) was processed; {} registration took {:.2f}secs. '
            'overall'.format(len(subjects), modality,
                             time.time() - start_multi))
Ejemplo n.º 2
0
    def dcm2niix_multiprocessing(self, name_subj, no_subj, dcm2niix_bin,
                                 last_idx, total_subj, status):
        """function intended to provide multiprocessing approach to speed up extraction of DICOM data to nifti files"""

        modalities = ['CT', 'MRI']
        if self.logfile:
            log_filename = os.path.join(
                ROOTDIR, 'logs', 'log_DCM2NII_' + str(no_subj + last_idx) +
                time.strftime("%Y%m%d-%H%M%S"))
        else:
            log_filename = os.devnull

        subj_outdir = os.path.join(
            self.outdir,
            self.cfg['folders']['prefix'] + str(no_subj + last_idx))
        FileOperations.create_folder(subj_outdir)
        start_time_subject = time.time()
        keptfiles, deletedfiles = ([] for _ in range(2))

        for mod in modalities:
            status.put((name_subj, mod, no_subj, total_subj))
            input_folder_name = os.path.join(self.inputdir, name_subj + mod)
            # input_folder_files = [f.path for f in os.scandir(input_folder_name)
            #                       if (f.is_dir() and ('100' in f.path or 'DICOM' in f.path or '001' in f.path))]
            input_folder_files = []
            [
                input_folder_files.append(item)
                for item in os.listdir(input_folder_name)
                if (os.path.isdir(os.path.join(input_folder_name, item)) and (
                    '100' in item or 'DICOM' in item or '001' in item))
            ]

            orig_stdout = sys.stdout
            sys.stdout = open(log_filename, 'w')
            for folder in input_folder_files:
                subprocess.call(
                    [
                        dcm2niix_bin,
                        '-a',
                        'y',  # anonimisation of DICOM data
                        '-b',
                        self.cfg['preprocess']['dcm2nii']['BIDSsidecar'][0],
                        '-z',
                        self.cfg['preprocess']['dcm2nii']['OutputCompression']
                        [0],
                        '-f',
                        self.cfg['preprocess']['dcm2nii']['OutputFileStruct'],
                        '-o',
                        subj_outdir,
                        '-w',
                        str(self.cfg['preprocess']['dcm2nii']
                            ['NameConflicts']),
                        '-v',
                        str(self.cfg['preprocess']['dcm2nii']['Verbosity']),
                        '-x',
                        str(self.cfg['preprocess']['dcm2nii']['ReorientCrop']),
                        folder
                    ],
                    stdout=sys.stdout,
                    stderr=subprocess.STDOUT)

            sys.stdout.close()
            sys.stdout = orig_stdout

            files_kept, files_deleted = self.select_sequences(subj_outdir)
            keptfiles.extend(files_kept)
            deletedfiles.extend(files_deleted)

        # Functions creating/updating pipeline log, which document individually all steps along with settings
        log_text = "{} files successfully converted: {}, \n\nand {} deleted: {}.\nDuration: {:.2f} secs" \
            .format(len(set(keptfiles)),
                    '\n\t{}'.format('\n\t'.join(os.path.split(x)[1] for x in sorted(set(keptfiles)))),
                    len(set(deletedfiles)),
                    '\n\t{}'.format('\n\t'.join(os.path.split(x)[1] for x in sorted(set(deletedfiles)))),
                    time.time() - start_time_subject)
        Output.logging_routine(text=Output.split_lines(log_text),
                               cfg=self.cfg,
                               subject=self.cfg['folders']['prefix'] +
                               str(no_subj),
                               module='dcm2nii',
                               opt=self.cfg['preprocess']['dcm2nii'],
                               project="")
Ejemplo n.º 3
0
    def N4BiasCorrection(self, subjects):
        """N4BiasCorrection according to N.J. Tustison, ..., and J.C. Gee.
        "N4ITK: Improved N3 Bias Correction" IEEE Transactions on Medical Imaging, 29(6):1310-1320, June 2010."""

        print('\nDebiasing imaging of {} subject(s)'.format(len(subjects)))
        allfiles = FileOperations.get_filelist_as_tuple(inputdir=self.cfg['folders']['nifti'], subjects=subjects)
        strings2exclude = ['CT', self.cfg['preprocess']['ANTsN4']['prefix'], 'reg_run', 'Mask', 'Warp', 'Registration']

        allfiles = [x for x in allfiles if x[0].endswith('.nii') and not
        any(re.search(r'\w+(?!_).({})|^({})\w+.'.format(z, z), os.path.basename(x[0]), re.IGNORECASE)
            for z in strings2exclude)]

        file_id_DTI = [x for x in allfiles if (x[0].endswith('.nii') and self.cfg['preprocess']['ANTsN4']['dti_prefix']
                                               in x[0]) and not
                       any(re.search(r'\w+(?!_).({})|^({})\w+.'.format(z, z), os.path.basename(x[0]), re.IGNORECASE)
                           for z in strings2exclude)]

        file_id_noDTI = [x for x in allfiles if (x[0].endswith('.nii') and not
        self.cfg['preprocess']['ANTsN4']['dti_prefix'] in x[0]) and not
                         any(re.search(r'\w+(?!_).({})|^({})\w+.'.format(z, z), os.path.basename(x[0]), re.IGNORECASE)
                             for z in strings2exclude)]

        seq = 'struct'  # For debugging purposes
        if not seq:
            fileIDs = allfiles
        elif seq == 'struct':
            fileIDs = list(set(allfiles) - set(file_id_DTI))
        elif seq == 'dwi':
            fileIDs = list(set(allfiles) - set(file_id_noDTI))

        start_multi = time.time()
        status = mp.Queue()
        processes = [mp.Process(target=self.N4BiasCorrection_multiprocessing,
                                args=(name_file, no_subj, os.path.join(self.cfg['folders']['nifti'], no_subj), status))
                     for name_file, no_subj in fileIDs]

        for p in processes:
            p.start()

        while any([p.is_alive() for p in processes]):
            while not status.empty():
                process, no_subj, filename = status.get()
                print("Process: {}; Debiasing {}, filename: {}".format(process, no_subj, filename))
            time.sleep(0.1)

        for p in processes:
            p.join()

        # Functions creating/updating pipeline log, which document individually all steps along with settings
        for subjID in subjects:
            allfiles_subj = [os.path.split(files_subj)[1] for files_subj, subj_no in fileIDs if subj_no == subjID]
            log_text = "{} files successfully processed (@{}): {}, \n\n Mean Duration per subject: {:.2f} secs" \
                .format(len(set(allfiles_subj)), time.strftime("%Y%m%d-%H%M%S"),
                        '\n\t{}'.format('\n\t'.join(os.path.split(x)[1] for x in sorted(set(allfiles_subj)))),
                        (time.time() - start_multi) / len(subjects))
            Output.logging_routine(text=Output.split_lines(log_text), cfg=self.cfg,
                                   subject=str(subjID), module='N4BiasCorrection',
                                   opt=self.cfg['preprocess']['ANTsN4'], project="")

        print('\nIn total, a list of {} subject(s) was processed \nOverall, bias correction took '
              '{:.1f} secs.'.format(len(subjects), time.time() - start_multi))