示例#1
0
文件: io.py 项目: armaneshaghi/DCMPy
    def _list_outputs(self):
        # infields are mandatory, however I could not figure out how to set 'mandatory' flag dynamically
        # hence manual check
        if self._infields:
            for key in self._infields:
                value = getattr(self.inputs, key)
                if not isdefined(value):
                    msg = "%s requires a value for input '%s' because it was listed in 'infields'" % \
                        (self.__class__.__name__, key)

        outputs = {}
        subject_id = self.inputs.subject_id
        tp, group, pid = subject_id.split('_')

        for key, args in self.inputs.template_args.items():
            outputs[key] = []
            template = self.inputs.template
            if hasattr(self.inputs, 'field_template') and \
                    isdefined(self.inputs.field_template) and \
                    key in self.inputs.field_template:
                template = self.inputs.field_template[key]
            if isdefined(self.inputs.base_directory):
                template = os.path.join(
                    os.path.abspath(self.inputs.base_directory), template)
            else:
                template = os.path.abspath(template)
            
            if (('concatenated_fmri.nii.gz' not in template) and ('filled_t1' not in template)):
                template  = template % (tp, group, key, pid,
                          key, tp, pid)
            elif 'concatenated_fmri.nii.gz' in template:
                template = template % (tp, group, key, pid)
            elif 'filled_t1' in template:
                template = template  %( tp, group, pid, tp, 
                        group, pid, tp, pid)
            filelist = glob.glob(template)
            if len(filelist) == 0:
                msg = 'output key: %s template: %s returned no files' % (
                        key, template)
                if self.inputs.raise_on_empty:
                    raise IOError(msg)
                else:
                    warn(msg)
            else:
                if self.inputs.sort_filelist:
                    filelist = human_order_sorted(filelist)
                outputs[key] = list_to_filename(filelist)
            if any([val is None for val in outputs[key]]):
                outputs[key] = []
            if len(outputs[key]) == 0:
                outputs[key] = None
            elif len(outputs[key]) == 1:
                outputs[key] = outputs[key][0]
        return outputs
示例#2
0
    def _list_outputs(self):
        outputs = self._outputs().get()
        pth = op.join(os.getcwd(), self.inputs.log_dir)

        pes = human_order_sorted(glob(os.path.join(pth, "pe[0-9]*.*")))
        assert len(pes) >= 1, "No pe volumes generated by FSL Estimate"
        outputs["pes"] = pes

        res4d = human_order_sorted(glob(os.path.join(pth, "res4d.*")))
        assert len(res4d) == 1, "No residual volume generated by FSL Estimate"
        outputs["res4d"] = res4d[0]

        copes = human_order_sorted(glob(os.path.join(pth, "cope[0-9]*.*")))
        assert len(copes) >= 1, "No cope volumes generated by FSL CEstimate"
        outputs["copes"] = copes

        var_copes = human_order_sorted(
            glob(os.path.join(pth, "varcope[0-9]*.*")))
        assert len(
            var_copes) >= 1, "No varcope volumes generated by FSL CEstimate"
        outputs["var_copes"] = var_copes

        zstats = human_order_sorted(glob(os.path.join(pth, "zstat[0-9]*.*")))
        assert len(zstats) >= 1, "No zstat volumes generated by FSL CEstimate"
        outputs["zstats"] = zstats

        if isdefined(self.inputs.f_con_file
                     ) and self.inputs.f_con_file is not False:
            zfstats = human_order_sorted(
                glob(os.path.join(pth, "zfstat[0-9]*.*")))
            assert len(
                zfstats) >= 1, "No zfstat volumes generated by FSL CEstimate"
            outputs["zfstats"] = zfstats

            fstats = human_order_sorted(
                glob(os.path.join(pth, "fstat[0-9]*.*")))
            assert len(
                fstats) >= 1, "No fstat volumes generated by FSL CEstimate"
            outputs["fstats"] = fstats

        tstats = human_order_sorted(glob(os.path.join(pth, "tstat[0-9]*.*")))
        assert len(tstats) >= 1, "No tstat volumes generated by FSL CEstimate"
        outputs["tstats"] = tstats

        mrefs = human_order_sorted(
            glob(os.path.join(pth, "mean_random_effects_var[0-9]*.*")))
        assert len(
            mrefs) >= 1, "No mean random effects volumes generated by FLAMEO"
        outputs["mrefvars"] = mrefs

        tdof = human_order_sorted(glob(os.path.join(pth, "tdof_t[0-9]*.*")))
        assert len(tdof) >= 1, "No T dof volumes generated by FLAMEO"
        outputs["tdof"] = tdof

        weights = human_order_sorted(glob(os.path.join(pth,
                                                       "weights[0-9]*.*")))
        assert len(weights) >= 1, "No weight volumes generated by FLAMEO"
        outputs["weights"] = weights

        outputs["stats_dir"] = pth

        return outputs
示例#3
0
    def _list_outputs(self):
        """Find the files and expose them as interface outputs."""
        outputs = {}
        info = dict([(k, v) for k, v in list(self.inputs.__dict__.items())
                     if k in self._infields])

        # check if the crumb is not absolute or if in info we have the parameter for the base directory
        if not self._crumb.isabs():
            first_arg_name, _ = self._crumb._first_open_arg()
            if first_arg_name not in info:
                raise KeyError(
                    'Crumb path is not absolute and could not find input for {}.'
                    .format(first_arg_name))
            elif not op.isabs(info[first_arg_name]):
                raise IOError(
                    'Expected an absolute path for {} argument in {} but got {}.'
                    .format(
                        first_arg_name,
                        self._crumb,
                        info[first_arg_name],
                    ))
        force_lists = self.inputs.force_lists
        if isinstance(force_lists, bool):
            force_lists = self._outfields if force_lists else []
        bad_fields = set(force_lists) - set(self._outfields)
        if bad_fields:
            bad_fields = ", ".join(list(bad_fields))
            plural = "s" if len(bad_fields) > 1 else ""
            verb = "were" if len(bad_fields) > 1 else "was"
            msg = ("The field%s '%s' %s set in 'force_lists' and not in "
                   "'templates'.") % (plural, bad_fields, verb)
            raise ValueError(msg)

        # loop over the crumb arguments to fill self_crumb
        crumb_info = {
            k: v
            for k, v in info.items() if k in self._crumb.open_args()
        }
        ocrumb = self._crumb.replace(**crumb_info)

        # check again if crumb path is absolute
        if not ocrumb.isabs():
            raise ValueError(
                'Expected a Crumb with an absolute path, got {}.'.format(
                    ocrumb))

        if not ocrumb.exists():
            raise IOError(
                'Expected an existing Crumb path, got {}.'.format(ocrumb))

        # loop over all the ouput items and fill them with the info in templates
        for field, template in self._templates.items():

            # Fill in the template and glob for files
            focrumb = ocrumb.replace(**dict(template))

            if list(focrumb.open_args()):
                raise ValueError(
                    'Expected a full specification of the Crumb path by now, got {}.'
                    .format(focrumb))

            filelist = [cr.path for cr in focrumb.unfold()]
            # Handle the case where nothing matched
            if not filelist:
                msg = "No files were found unfolding %s crumb path: %s" % (
                    field, focrumb)
                if self.inputs.raise_on_empty:
                    raise IOError(msg)
                else:
                    warn(msg)

            # Possibly sort the list
            if self.inputs.sort_filelist:
                filelist = human_order_sorted(filelist)

            # Handle whether this must be a list or not
            if field not in force_lists:
                filelist = list_to_filename(filelist)

            outputs[field] = filelist

            # add the crumb argument values for output
            for arg_name in focrumb.all_args():
                outputs[arg_name] = focrumb[arg_name][0]

        return outputs
示例#4
0
workflow.connect(sphere_left, "out_file", combine_left_right ,"in_file") 
workflow.connect(sphere_right, "out_file", combine_left_right ,"operand_file") 


datasink = pe.Node(nio.DataSink(), name='sinker')
datasink.inputs.base_directory = '/scr/ilz1/huntenburg/seed_connectivity/'
datasink.inputs.parameterization = True
workflow.connect(combine_left_right, 'out_file', datasink, 'roi')


subject_id_infosource = pe.Node(util.IdentityInterface(fields=["subject_id"]), 
                                name="subject_id_infosource")

subjects = ['LEMON001']#,'LEMON006','LEMON087','LEMON030','LEMON044','LEMON071']
from nipype.utils.misc import human_order_sorted
subjects = human_order_sorted(subjects)
            
subject_id_infosource.iterables = ("subject_id", subjects)


templates={'epi_preproc':'kansas1/huntenburg/preproc/_cor_method_nonlin_ts/_subject_id_{subject_id}/bandpass_filter/nonlin_ts_detrended_regfilt_filt_300_masked.nii.gz',
           'anat_brain':'jessica2/Schaare/LEMON/preprocessed/{subject_id}/freesurfer_anatomy/brain_out.nii.gz'
}


selectfiles = pe.Node(nio.SelectFiles(templates,
                                   base_directory="/scr/"),
                   name="selectfiles")
workflow.connect(selectfiles, "anat_brain", sphere_left, "in_file_a")

示例#5
0
文件: crumb.py 项目: Neurita/pypes
    def _list_outputs(self):
        """Find the files and expose them as interface outputs."""
        outputs = {}
        info = dict([(k, v) for k, v in list(self.inputs.__dict__.items())
                     if k in self._infields])

        # check if the crumb is not absolute or if in info we have the parameter for the base directory
        if not self._crumb.isabs():
            first_arg_name, _ = self._crumb._first_open_arg()
            if first_arg_name not in info:
                raise KeyError('Crumb path is not absolute and could not find input for {}.'.format(first_arg_name))
            elif not op.isabs(info[first_arg_name]):
                raise IOError('Expected an absolute path for {} argument in {} but got {}.'.format(first_arg_name,
                                                                                                   self._crumb,
                                                                                                   info[first_arg_name],
                                                                                                   ))
        force_lists = self.inputs.force_lists
        if isinstance(force_lists, bool):
            force_lists = self._outfields if force_lists else []
        bad_fields = set(force_lists) - set(self._outfields)
        if bad_fields:
            bad_fields = ", ".join(list(bad_fields))
            plural = "s" if len(bad_fields) > 1 else ""
            verb = "were" if len(bad_fields) > 1 else "was"
            msg = ("The field%s '%s' %s set in 'force_lists' and not in "
                   "'templates'.") % (plural, bad_fields, verb)
            raise ValueError(msg)

        # loop over the crumb arguments to fill self_crumb
        crumb_info = {k: v for k, v in info.items() if k in self._crumb.open_args()}
        ocrumb = self._crumb.replace(**crumb_info)

        # check again if crumb path is absolute
        if not ocrumb.isabs():
            raise ValueError('Expected a Crumb with an absolute path, got {}.'.format(ocrumb))

        if not ocrumb.exists():
            raise IOError('Expected an existing Crumb path, got {}.'.format(ocrumb))

        # loop over all the ouput items and fill them with the info in templates
        for field, template in self._templates.items():

            # Fill in the template and glob for files
            focrumb  = ocrumb.replace(**dict(template))

            if list(focrumb.open_args()):
                raise ValueError('Expected a full specification of the Crumb path by now, got {}.'.format(focrumb))

            filelist = [cr.path for cr in focrumb.unfold()]
            # Handle the case where nothing matched
            if not filelist:
                msg = "No files were found unfolding %s crumb path: %s" % (
                    field, focrumb)
                if self.inputs.raise_on_empty:
                    raise IOError(msg)
                else:
                    warn(msg)

            # Possibly sort the list
            if self.inputs.sort_filelist:
                filelist = human_order_sorted(filelist)

            # Handle whether this must be a list or not
            if field not in force_lists:
                filelist = list_to_filename(filelist)

            outputs[field] = filelist

            # add the crumb argument values for output
            for arg_name in focrumb.all_args():
                outputs[arg_name] = focrumb[arg_name][0]

        return outputs