Beispiel #1
0
    def check_input1(self, gui=True):
        print '**** Check Inputs  ****'
        diffusion_available = False
        bvecs_available = False
        bvals_available = False
        t1_available = False
        t2_available = False
        valid_inputs = False

        dwi_file = os.path.join(self.subject_directory, 'dwi',
                                self.subject + '_dwi.nii.gz')
        bval_file = os.path.join(self.subject_directory, 'dwi',
                                 self.subject + '_dwi.bval')
        bvec_file = os.path.join(self.subject_directory, 'dwi',
                                 self.subject + '_dwi.bvec')
        T1_file = os.path.join(self.subject_directory, 'anat',
                               self.subject + '_T1w.nii.gz')
        T2_file = os.path.join(self.subject_directory, 'anat',
                               self.subject + '_T2w.nii.gz')

        print "Looking for...."
        print "dwi_file : %s" % dwi_file
        print "bvecs_file : %s" % bvec_file
        print "bvals_file : %s" % bval_file
        print "T1_file : %s" % T1_file
        print "T2_file : %s" % T2_file

        try:
            layout = BIDSLayout(self.base_directory)
            print "Valid BIDS dataset with %s subjects" % len(
                layout.get_subjects())
            for subj in layout.get_subjects():
                self.global_conf.subjects.append('sub-' + str(subj))
            # self.global_conf.subjects = ['sub-'+str(subj) for subj in layout.get_subjects()]
            self.global_conf.modalities = [
                str(mod) for mod in layout.get_modalities()
            ]
            # mods = layout.get_modalities()
            types = layout.get_types()
            # print "Available modalities :"
            # for mod in mods:
            #     print "-%s" % mod

            for typ in types:
                if typ == 'dwi' and os.path.isfile(dwi_file):
                    print "%s available" % typ
                    diffusion_available = True

                if typ == 'T1w' and os.path.isfile(T1_file):
                    print "%s available" % typ
                    t1_available = True

                if typ == 'T2w' and os.path.isfile(T2_file):
                    print "%s available" % typ
                    t2_available = True
        except:
            error(
                message=
                "Invalid BIDS dataset. Please see documentation for more details.",
                title="Error",
                buttons=['OK', 'Cancel'],
                parent=None)
            return

        if os.path.isfile(bval_file): bvals_available = True

        if os.path.isfile(bvec_file): bvecs_available = True

        mem = Memory(base_dir=os.path.join(self.derivatives_directory, 'cmp',
                                           self.subject, 'tmp', 'nipype'))
        swap_and_reorient = mem.cache(SwapAndReorient)

        if diffusion_available:
            if bvals_available and bvecs_available:
                self.stages[
                    'Diffusion'].config.diffusion_imaging_model_choices = self.diffusion_imaging_model

                #Copy diffusion data to derivatives / cmp  / subject / dwi
                out_dwi_file = os.path.join(self.derivatives_directory, 'cmp',
                                            self.subject, 'dwi',
                                            self.subject + '_dwi.nii.gz')
                out_bval_file = os.path.join(self.derivatives_directory, 'cmp',
                                             self.subject, 'dwi',
                                             self.subject + '_dwi.bval')
                out_bvec_file = os.path.join(self.derivatives_directory, 'cmp',
                                             self.subject, 'dwi',
                                             self.subject + '_dwi.bvec')

                shutil.copy(src=dwi_file, dst=out_dwi_file)
                shutil.copy(src=bvec_file, dst=out_bvec_file)
                shutil.copy(src=bval_file, dst=out_bval_file)

                if t2_available:
                    print "Swap and reorient T2"
                    swap_and_reorient(
                        src_file=os.path.join(self.subject_directory, 'anat',
                                              self.subject + '_T2w.nii.gz'),
                        ref_file=os.path.join(self.subject_directory, 'dwi',
                                              self.subject + '_dwi.nii.gz'),
                        out_file=os.path.join(self.derivatives_directory,
                                              'cmp', self.subject, 'anat',
                                              self.subject + '_T2w.nii.gz'))
                if t1_available:
                    swap_and_reorient(
                        src_file=os.path.join(self.subject_directory, 'anat',
                                              self.subject + '_T1w.nii.gz'),
                        ref_file=os.path.join(self.subject_directory, 'dwi',
                                              self.subject + '_dwi.nii.gz'),
                        out_file=os.path.join(self.derivatives_directory,
                                              'cmp', self.subject, 'anat',
                                              self.subject + '_T1w.nii.gz'))
                    valid_inputs = True
                    input_message = 'Inputs check finished successfully.\nDiffusion and morphological data available.'
                else:
                    input_message = 'Error during inputs check.\nMorphological data (T1) not available.'
            else:
                input_message = 'Error during inputs check.\nDiffusion bvec or bval files not available.'
        elif t1_available:
            input_message = 'Error during inputs check. \nDiffusion data not available (DSI/DTI/HARDI).'
        else:
            input_message = 'Error during inputs check. No diffusion or morphological data available in folder ' + os.path.join(
                self.base_directory, 'RAWDATA') + '!'

        #diffusion_imaging_model = diffusion_imaging_model[0]

        if gui:
            #input_notification = Check_Input_Notification(message=input_message, diffusion_imaging_model_options=diffusion_imaging_model,diffusion_imaging_model=diffusion_imaging_model)
            #input_notification.configure_traits()
            print input_message
            self.global_conf.diffusion_imaging_model = self.diffusion_imaging_model
            # diffusion_file = os.path.join(self.subject_directory,'dwi',self.subject+'_dwi.nii.gz')
            # n_vol = nib.load(diffusion_file).shape[3]
            # if self.stages['Preprocessing'].config.end_vol == 0 or self.stages['Preprocessing'].config.end_vol == self.stages['Preprocessing'].config.max_vol or self.stages['Preprocessing'].config.end_vol >= n_vol-1:
            #     self.stages['Preprocessing'].config.end_vol = n_vol-1
            # self.stages['Preprocessing'].config.max_vol = n_vol-1
            self.stages[
                'Registration'].config.diffusion_imaging_model = self.diffusion_imaging_model
            self.stages[
                'Diffusion'].config.diffusion_imaging_model = self.diffusion_imaging_model
        else:
            print input_message
            self.global_conf.diffusion_imaging_model = self.diffusion_imaging_model
            # diffusion_file = os.path.join(self.subject_directory,'dwi',self.subject+'_dwi.nii.gz')
            # n_vol = nib.load(diffusion_file).shape[3]
            # if self.stages['Preprocessing'].config.end_vol == 0 or self.stages['Preprocessing'].config.end_vol == self.stages['Preprocessing'].config.max_vol or self.stages['Preprocessing'].config.end_vol >= n_vol-1:
            #     self.stages['Preprocessing'].config.end_vol = n_vol-1
            # self.stages['Preprocessing'].config.max_vol = n_vol-1
            self.stages[
                'Registration'].config.diffusion_imaging_model = self.diffusion_imaging_model
            self.stages[
                'Diffusion'].config.diffusion_imaging_model = self.diffusion_imaging_model

        if t2_available:
            self.stages['Registration'].config.registration_mode_trait = [
                'Linear + Non-linear (FSL)'
            ]  #,'BBregister (FS)','Nonlinear (FSL)']

        if (t1_available and diffusion_available):
            valid_inputs = True
        else:
            print "Missing required inputs."
            error(
                message=
                "Missing required inputs. Please see documentation for more details.",
                title="Error",
                buttons=['OK', 'Cancel'],
                parent=None)

        for stage in self.stages.values():
            if stage.enabled:
                print stage.name
                print stage.stage_dir

        self.fill_stages_outputs()

        return valid_inputs
Beispiel #2
0
    def check_input(self, layout, gui=True):
        print '**** Check Inputs  ****'
        diffusion_available = False
        bvecs_available = False
        bvals_available = False
        valid_inputs = False

        if self.global_conf.subject_session == '':
            subject = self.subject
        else:
            subject = "_".join(
                (self.subject, self.global_conf.subject_session))

        dwi_file = os.path.join(self.subject_directory, 'dwi',
                                subject + '_dwi.nii.gz')
        bval_file = os.path.join(self.subject_directory, 'dwi',
                                 subject + '_dwi.bval')
        bvec_file = os.path.join(self.subject_directory, 'dwi',
                                 subject + '_dwi.bvec')

        subjid = self.subject.split("-")[1]

        try:
            layout = BIDSLayout(self.base_directory)
            print "Valid BIDS dataset with %s subjects" % len(
                layout.get_subjects())
            for subj in layout.get_subjects():
                self.global_conf.subjects.append('sub-' + str(subj))
            # self.global_conf.subjects = ['sub-'+str(subj) for subj in layout.get_subjects()]
            self.global_conf.modalities = [
                str(mod) for mod in layout.get_modalities()
            ]
            # mods = layout.get_modalities()
            types = layout.get_types()
            # print "Available modalities :"
            # for mod in mods:
            #     print "-%s" % mod

            if self.global_conf.subject_session == '':

                files = layout.get(subject=subjid,
                                   type='dwi',
                                   extensions='.nii.gz')
                if len(files) > 0:
                    dwi_file = files[0].filename
                    print dwi_file
                else:
                    error(message="Diffusion image not found for subject %s." %
                          (subjid),
                          title="Error",
                          buttons=['OK', 'Cancel'],
                          parent=None)
                    return

                files = layout.get(subject=subjid,
                                   type='dwi',
                                   extensions='.bval')
                if len(files) > 0:
                    bval_file = files[0].filename
                    print bval_file
                else:
                    error(
                        message="Diffusion bval image not found for subject %s."
                        % (subjid),
                        title="Error",
                        buttons=['OK', 'Cancel'],
                        parent=None)
                    return

                files = layout.get(subject=subjid,
                                   type='dwi',
                                   extensions='.bvec')
                if len(files) > 0:
                    bvec_file = files[0].filename
                    print bvec_file
                else:
                    error(
                        message="Diffusion bvec image not found for subject %s."
                        % (subjid),
                        title="Error",
                        buttons=['OK', 'Cancel'],
                        parent=None)
                    return
            else:
                sessid = self.global_conf.subject_session.split("-")[1]

                files = layout.get(subject=subjid,
                                   type='dwi',
                                   extensions='.nii.gz',
                                   session=sessid)
                if len(files) > 0:
                    dwi_file = files[0].filename
                    print dwi_file
                else:
                    error(
                        message=
                        "Diffusion image not found for subject %s, session %s."
                        % (subjid, self.global_conf.subject_session),
                        title="Error",
                        buttons=['OK', 'Cancel'],
                        parent=None)
                    return

                files = layout.get(subject=subjid,
                                   type='dwi',
                                   extensions='.bval',
                                   session=sessid)
                if len(files) > 0:
                    bval_file = files[0].filename
                    print bval_file
                else:
                    error(
                        message=
                        "Diffusion bval image not found for subject %s, session %s."
                        % (subjid, self.global_conf.subject_session),
                        title="Error",
                        buttons=['OK', 'Cancel'],
                        parent=None)
                    return

                files = layout.get(subject=subjid,
                                   type='dwi',
                                   extensions='.bvec',
                                   session=sessid)
                if len(files) > 0:
                    bvec_file = files[0].filename
                    print bvec_file
                else:
                    error(
                        message=
                        "Diffusion bvec image not found for subject %s, session %s."
                        % (subjid, self.global_conf.subject_session),
                        title="Error",
                        buttons=['OK', 'Cancel'],
                        parent=None)
                    return

            print "Looking for...."
            print "dwi_file : %s" % dwi_file
            print "bvecs_file : %s" % bvec_file
            print "bvals_file : %s" % bval_file

            for typ in types:
                if typ == 'dwi' and os.path.isfile(dwi_file):
                    print "%s available" % typ
                    diffusion_available = True

        except:
            error(
                message=
                "Invalid BIDS dataset. Please see documentation for more details.",
                title="Error",
                buttons=['OK', 'Cancel'],
                parent=None)
            return

        if os.path.isfile(bval_file): bvals_available = True

        if os.path.isfile(bvec_file): bvecs_available = True

        if diffusion_available:
            if bvals_available and bvecs_available:
                self.stages[
                    'Diffusion'].config.diffusion_imaging_model_choices = self.diffusion_imaging_model

                #Copy diffusion data to derivatives / cmp  / subject / dwi
                if self.global_conf.subject_session == '':
                    out_dwi_file = os.path.join(self.derivatives_directory,
                                                'cmp', self.subject, 'dwi',
                                                subject + '_dwi.nii.gz')
                    out_bval_file = os.path.join(self.derivatives_directory,
                                                 'cmp', self.subject, 'dwi',
                                                 subject + '_dwi.bval')
                    out_bvec_file = os.path.join(self.derivatives_directory,
                                                 'cmp', self.subject, 'dwi',
                                                 subject + '_dwi.bvec')
                else:
                    out_dwi_file = os.path.join(
                        self.derivatives_directory, 'cmp', self.subject,
                        self.global_conf.subject_session, 'dwi',
                        subject + '_dwi.nii.gz')
                    out_bval_file = os.path.join(
                        self.derivatives_directory, 'cmp', self.subject,
                        self.global_conf.subject_session, 'dwi',
                        subject + '_dwi.bval')
                    out_bvec_file = os.path.join(
                        self.derivatives_directory, 'cmp', self.subject,
                        self.global_conf.subject_session, 'dwi',
                        subject + '_dwi.bvec')

                if not os.path.isfile(out_dwi_file):
                    shutil.copy(src=dwi_file, dst=out_dwi_file)
                if not os.path.isfile(out_bvec_file):
                    shutil.copy(src=bvec_file, dst=out_bvec_file)
                if not os.path.isfile(out_bval_file):
                    shutil.copy(src=bval_file, dst=out_bval_file)

                valid_inputs = True
                input_message = 'Inputs check finished successfully.\nDiffusion and morphological data available.'
            else:
                input_message = 'Error during inputs check.\nDiffusion bvec or bval files not available.'
        else:
            if self.global_conf.subject_session == '':
                input_message = 'Error during inputs check. No diffusion data available in folder ' + os.path.join(
                    self.base_directory, self.subject, 'dwi') + '!'
            else:
                input_message = 'Error during inputs check. No diffusion data available in folder ' + os.path.join(
                    self.base_directory, self.subject,
                    self.global_conf.subject_session, 'dwi') + '!'
        #diffusion_imaging_model = diffusion_imaging_model[0]

        if gui:
            #input_notification = Check_Input_Notification(message=input_message, diffusion_imaging_model_options=diffusion_imaging_model,diffusion_imaging_model=diffusion_imaging_model)
            #input_notification.configure_traits()
            print input_message
            self.global_conf.diffusion_imaging_model = self.diffusion_imaging_model

            # if diffusion_available:
            #     n_vol = nib.load(dwi_file).shape[3]
            #     if self.stages['Preprocessing'].config.end_vol == 0 or self.stages['Preprocessing'].config.end_vol == self.stages['Preprocessing'].config.max_vol or self.stages['Preprocessing'].config.end_vol >= n_vol-1:
            #         self.stages['Preprocessing'].config.end_vol = n_vol-1
            #     self.stages['Preprocessing'].config.max_vol = n_vol-1

            self.stages[
                'Registration'].config.diffusion_imaging_model = self.diffusion_imaging_model
            self.stages[
                'Diffusion'].config.diffusion_imaging_model = self.diffusion_imaging_model
        else:
            print input_message
            self.global_conf.diffusion_imaging_model = self.diffusion_imaging_model

            # if diffusion_available:
            #     n_vol = nib.load(dwi_file).shape[3]
            #     if self.stages['Preprocessing'].config.end_vol == 0 or self.stages['Preprocessing'].config.end_vol == self.stages['Preprocessing'].config.max_vol or self.stages['Preprocessing'].config.end_vol >= n_vol-1:
            #         self.stages['Preprocessing'].config.end_vol = n_vol-1
            #     self.stages['Preprocessing'].config.max_vol = n_vol-1

            self.stages[
                'Registration'].config.diffusion_imaging_model = self.diffusion_imaging_model
            self.stages[
                'Diffusion'].config.diffusion_imaging_model = self.diffusion_imaging_model

        if (diffusion_available):
            valid_inputs = True
        else:
            print "Missing required inputs."
            error(
                message=
                "Missing diffusion inputs. Please see documentation for more details.",
                title="Error",
                buttons=['OK', 'Cancel'],
                parent=None)

        for stage in self.stages.values():
            if stage.enabled:
                print stage.name
                print stage.stage_dir

        self.fill_stages_outputs()

        return valid_inputs
def bidsPipeline(resultsDir, workDir, subDir, subid):
	layout = BIDSLayout("/ds102/")
	subjects = layout.get_subjects()
	mods = layout.get_modalities()
	mod_types = layout.get_types(modality='func')
	tasks = layout.get_tasks()