Exemple #1
0
 def _format_arg(self, opt, spec, val):
     """Convert input to appropriate format for spm
     """
     if opt == 'in_files':
         return scans_for_fnames(filename_to_list(val))
     if opt == 'target':
         return scans_for_fname(filename_to_list(val))
     if opt == 'deformation':
         return np.array([list_to_filename(val)], dtype=object)
     if opt == 'deformation_field':
         return np.array([list_to_filename(val)], dtype=object)
     return val
Exemple #2
0
 def _format_arg(self, opt, spec, val):
     """Convert input to appropriate format for spm
     """
     if opt == 'in_files':
         return scans_for_fnames(filename_to_list(val))
     if opt == 'target':
         return scans_for_fname(filename_to_list(val))
     if opt == 'deformation':
         return np.array([list_to_filename(val)], dtype=object)
     if opt == 'deformation_field':
         return np.array([list_to_filename(val)], dtype=object)
     return val
def bandpass_filter(files, lowpass_freq, highpass_freq, fs):
    """Bandpass filter the input files

    Parameters
    ----------
    files: list of 4d nifti files
    lowpass_freq: cutoff frequency for the low pass filter (in Hz)
    highpass_freq: cutoff frequency for the high pass filter (in Hz)
    fs: sampling rate (in Hz)
    """
    out_files = []
    for filename in filename_to_list(files):
        path, name, ext = split_filename(filename)
        out_file = os.path.join(os.getcwd(), name + '_bp' + ext)
        img = nb.load(filename)
        timepoints = img.shape[-1]
        F = np.zeros((timepoints))
        lowidx = int(timepoints / 2) + 1
        if lowpass_freq > 0:
            lowidx = np.round(float(lowpass_freq) / fs * timepoints)
        highidx = 0
        if highpass_freq > 0:
            highidx = np.round(float(highpass_freq) / fs * timepoints)
        F[highidx:lowidx] = 1
        F = ((F + F[::-1]) > 0).astype(int)
        data = img.get_data()
        if np.all(F == 1):
            filtered_data = data
        else:
            filtered_data = np.real(np.fft.ifftn(np.fft.fftn(data) * F))
        img_out = nb.Nifti1Image(filtered_data, img.affine, img.header)
        img_out.to_filename(out_file)
        out_files.append(out_file)
    return list_to_filename(out_files)
Exemple #4
0
    def _list_outputs(self):
        outputs = self._outputs().get()

        jobtype = self.inputs.jobtype
        if jobtype.startswith('est'):
            outputs['normalization_parameters'] = []
            for imgf in filename_to_list(self.inputs.source):
                outputs['normalization_parameters'].append(fname_presuffix(imgf, suffix='_sn.mat', use_ext=False))
            outputs['normalization_parameters'] = list_to_filename(outputs['normalization_parameters'])

        if self.inputs.jobtype == "estimate":
            if isdefined(self.inputs.apply_to_files):
                outputs['normalized_files'] = self.inputs.apply_to_files
            outputs['normalized_source'] = self.inputs.source
        elif 'write' in self.inputs.jobtype:
            outputs['normalized_files'] = []
            if isdefined(self.inputs.apply_to_files):
                for imgf in filename_to_list(self.inputs.apply_to_files):
                    outputs['normalized_files'].append(fname_presuffix(imgf, prefix='w'))

            if isdefined(self.inputs.source):
                outputs['normalized_source'] = []
                for imgf in filename_to_list(self.inputs.source):
                    outputs['normalized_source'].append(fname_presuffix(imgf, prefix='w'))

        return outputs
Exemple #5
0
 def _list_outputs(self):
     outputs = self.output_spec().get()
     subjects_dir = self.inputs.subjects_dir
     subject_path = op.join(subjects_dir, self.inputs.subject_id)
     output_traits = self._outputs()
     mesh_paths = []
     for k in outputs.keys():
         if k != 'mesh_files':
             val = self._get_files(subject_path, k,
                                   output_traits.traits()[k].loc,
                                   output_traits.traits()[k].altkey)
             if val:
                 value_list = list_to_filename(val)
                 if isinstance(value_list, list):
                     out_files = []
                     for value in value_list:
                         out_files.append(op.abspath(value))
                 elif isinstance(value_list, str):
                     out_files = op.abspath(value_list)
                 else:
                     raise TypeError
                 outputs[k] = out_files
                 if not k.rfind('surface') == -1:
                     mesh_paths.append(out_files)
     outputs['mesh_files'] = mesh_paths
     return outputs
def bandpass_filter(files, lowpass_freq, highpass_freq, fs):
    """Bandpass filter the input files

    Parameters
    ----------
    files: list of 4d nifti files
    lowpass_freq: cutoff frequency for the low pass filter (in Hz)
    highpass_freq: cutoff frequency for the high pass filter (in Hz)
    fs: sampling rate (in Hz)
    """
    out_files = []
    for filename in filename_to_list(files):
        path, name, ext = split_filename(filename)
        out_file = os.path.join(os.getcwd(), name + '_bp' + ext)
        img = nb.load(filename)
        timepoints = img.shape[-1]
        F = np.zeros((timepoints))
        lowidx = timepoints / 2 + 1
        if lowpass_freq > 0:
            lowidx = np.round(float(lowpass_freq) / fs * timepoints)
        highidx = 0
        if highpass_freq > 0:
            highidx = np.round(float(highpass_freq) / fs * timepoints)
        F[highidx:lowidx] = 1
        F = ((F + F[::-1]) > 0).astype(int)
        data = img.get_data()
        if np.all(F == 1):
            filtered_data = data
        else:
            filtered_data = np.real(np.fft.ifftn(np.fft.fftn(data) * F))
        img_out = nb.Nifti1Image(filtered_data, img.get_affine(),
                                 img.get_header())
        img_out.to_filename(out_file)
        out_files.append(out_file)
    return list_to_filename(out_files)
Exemple #7
0
 def _format_arg(self, opt, spec, val):
     """Convert input to appropriate format for spm"""
     if opt == "deformation_field":
         return np.array([list_to_filename(val)], dtype=object)
     if opt == "in_files":
         return np.array(filename_to_list(val), dtype=object)
     return val
Exemple #8
0
    def _list_outputs(self):
        outputs = self._outputs().get()

        jobtype = self.inputs.jobtype
        if jobtype.startswith('est'):
            outputs['normalization_parameters'] = []
            for imgf in filename_to_list(self.inputs.source):
                outputs['normalization_parameters'].append(fname_presuffix(imgf, suffix='_sn.mat', use_ext=False))
            outputs['normalization_parameters'] = list_to_filename(outputs['normalization_parameters'])

        if self.inputs.jobtype == "estimate":
            if isdefined(self.inputs.apply_to_files):
                outputs['normalized_files'] = self.inputs.apply_to_files
            outputs['normalized_source'] = self.inputs.source
        elif 'write' in self.inputs.jobtype:
            outputs['normalized_files'] = []
            if isdefined(self.inputs.apply_to_files):
                filelist = filename_to_list(self.inputs.apply_to_files)
                for f in filelist:
                    if isinstance(f, list):
                        run = [fname_presuffix(in_f, prefix=self.inputs.out_prefix) for in_f in f]
                    else:
                        run = [fname_presuffix(f, prefix=self.inputs.out_prefix)]
                    outputs['normalized_files'].extend(run)
            if isdefined(self.inputs.source):
                outputs['normalized_source'] = fname_presuffix(self.inputs.source, prefix=self.inputs.out_prefix)

        return outputs
Exemple #9
0
 def _list_outputs(self):
     outputs = self.output_spec().get()
     subjects_dir = self.inputs.subjects_dir
     subject_path = op.join(subjects_dir, self.inputs.subject_id)
     output_traits = self._outputs()
     mesh_paths = []
     for k in outputs.keys():
         if k != 'mesh_files':
             val = self._get_files(subject_path, k,
                                   output_traits.traits()[k].loc,
                                   output_traits.traits()[k].altkey)
             if val:
                 value_list = list_to_filename(val)
                 if isinstance(value_list, list):
                     out_files = []
                     for value in value_list:
                         out_files.append(op.abspath(value))
                 elif isinstance(value_list, str):
                     out_files = op.abspath(value_list)
                 else:
                     raise TypeError
                 outputs[k] = out_files
                 if not k.rfind('surface') == -1:
                     mesh_paths.append(out_files)
     outputs['mesh_files'] = mesh_paths
     return outputs
Exemple #10
0
    def _list_outputs(self):
        outputs = self._outputs().get()

        jobtype = self.inputs.jobtype
        if jobtype.startswith("est"):
            outputs["normalization_parameters"] = []
            for imgf in filename_to_list(self.inputs.source):
                outputs["normalization_parameters"].append(fname_presuffix(imgf, suffix="_sn.mat", use_ext=False))
            outputs["normalization_parameters"] = list_to_filename(outputs["normalization_parameters"])

        if self.inputs.jobtype == "estimate":
            if isdefined(self.inputs.apply_to_files):
                outputs["normalized_files"] = self.inputs.apply_to_files
            outputs["normalized_source"] = self.inputs.source
        elif "write" in self.inputs.jobtype:
            outputs["normalized_files"] = []
            if isdefined(self.inputs.apply_to_files):
                for imgf in filename_to_list(self.inputs.apply_to_files):
                    outputs["normalized_files"].append(fname_presuffix(imgf, prefix="w"))

            if isdefined(self.inputs.source):
                outputs["normalized_source"] = []
                for imgf in filename_to_list(self.inputs.source):
                    outputs["normalized_source"].append(fname_presuffix(imgf, prefix="w"))

        return outputs
Exemple #11
0
    def _list_outputs(self):
        outputs = self._outputs().get()
        
        outputs['outlier_files'] = []
        outputs['intensity_files'] = []
        outputs['statistic_files'] = []
        for i,f in enumerate(filename_to_list(self.inputs.realigned_files)):
            outlierfile,intensityfile,statsfile, _ = self._get_output_filenames(f,os.getcwd())
            outputs['outlier_files'].insert(i,outlierfile)
            outputs['intensity_files'].insert(i,intensityfile)     
            outputs['statistic_files'].insert(i,statsfile)

        outputs['outlier_files'] = list_to_filename(outputs['outlier_files'])
        outputs['intensity_files'] = list_to_filename(outputs['intensity_files'])
        outputs['statistic_files'] = list_to_filename(outputs['statistic_files'])
        return outputs
Exemple #12
0
 def _format_arg(self, opt, spec, val):
     """Convert input to appropriate format for SPM
     """
     if opt in ['in_files']:
         return scans_for_fnames(filename_to_list(val), keep4d=False)
     if opt == 'deformation_field':
         return np.array([list_to_filename(val)], dtype=object)
     return super(ApplyDeformationField, self)._format_arg(opt, spec, val)
    def _list_outputs(self):
        outputs = self._outputs().get()
        jobtype = self.inputs.jobtype
        if jobtype == "calculatevdm":
            outputs['vdm'] = []
            for phase in filename_to_list(self.inputs.phase):
                outputs['vdm'].append(fname_presuffix(phase, prefix='vdm5_sc'))
            outputs['vdm'] = list_to_filename(outputs['vdm'])

        return outputs
Exemple #14
0
 def _list_outputs(self):
     subjects_dir = self.inputs.subjects_dir
     subject_path = os.path.join(subjects_dir, self.inputs.subject_id)
     output_traits = self._outputs()
     outputs = output_traits.get()
     for k in outputs.keys():
         val = self._get_files(subject_path, k, output_traits.traits()[k].loc, output_traits.traits()[k].altkey)
         if val:
             outputs[k] = list_to_filename(val)
     return outputs
 def get_names(files, suffix):
     """Generate appropriate names for output files
     """
     from nipype.utils.filemanip import (split_filename, filename_to_list,
                                         list_to_filename)
     out_names = []
     for filename in files:
         _, name, _ = split_filename(filename)
         out_names.append(name + suffix)
     return list_to_filename(out_names)
 def get_names(files, suffix):
     """Generate appropriate names for output files
     """
     from nipype.utils.filemanip import (split_filename, filename_to_list,
                                         list_to_filename)
     out_names = []
     for filename in files:
         _, name, _ = split_filename(filename)
         out_names.append(name + suffix)
     return list_to_filename(out_names)
Exemple #17
0
def bandpass_filter(files, lowpass_freq=0.1, highpass_freq=0.01, tr=2):
    """Bandpass filter the input files

    Parameters
    ----------
    files: list of str
        List 4d nifti file paths.

    lowpass_freq: float
        Cutoff frequency for the low pass filter (in Hz).

    highpass_freq: float
        Cutoff frequency for the high pass filter (in Hz).

    tr: float
        The repetition time in seconds. The inverse of sampling rate (in Hz).
    """
    import os

    import nibabel as nb
    import numpy as np
    from   nipype.utils.filemanip import (filename_to_list,
                                          list_to_filename,
                                          split_filename)

    fs = 1./tr

    out_files = []
    for filename in filename_to_list(files):
        path, name, ext = split_filename(filename)
        out_file = os.path.join(os.getcwd(), name + '_bandpassed' + ext)

        img = nb.load(filename)
        timepoints = img.shape[-1]
        F = np.zeros((timepoints))

        lowidx = int(timepoints / 2) + 1
        if lowpass_freq > 0:
            lowidx = np.round(float(lowpass_freq) / fs * timepoints)

        highidx = 0
        if highpass_freq > 0:
            highidx = np.round(float(highpass_freq) / fs * timepoints)
        F[int(highidx):int(lowidx)] = 1
        F = ((F + F[::-1]) > 0).astype(int)
        data = img.get_data()
        if np.all(F == 1):
            filtered_data = data
        else:
            filtered_data = np.real(np.fft.ifftn(np.fft.fftn(data) * F))
        img_out = nb.Nifti1Image(filtered_data, img.affine, img.header)
        img_out.to_filename(out_file)
        out_files.append(out_file)

    return list_to_filename(out_files)
Exemple #18
0
def bandpass_filter(files, lowpass_freq=0.1, highpass_freq=0.01, tr=2):
    """Bandpass filter the input files

    Parameters
    ----------
    files: list of str
        List 4d nifti file paths.

    lowpass_freq: float
        Cutoff frequency for the low pass filter (in Hz).

    highpass_freq: float
        Cutoff frequency for the high pass filter (in Hz).

    tr: float
        The repetition time in seconds. The inverse of sampling rate (in Hz).
    """
    import os

    import nibabel as nb
    import numpy as np
    from   nipype.utils.filemanip import (filename_to_list,
                                          list_to_filename,
                                          split_filename)

    fs = 1./tr

    out_files = []
    for filename in filename_to_list(files):
        path, name, ext = split_filename(filename)
        out_file = os.path.join(os.getcwd(), name + '_bandpassed' + ext)

        img = nb.load(filename)
        timepoints = img.shape[-1]
        F = np.zeros((timepoints))

        lowidx = int(timepoints / 2) + 1
        if lowpass_freq > 0:
            lowidx = np.round(float(lowpass_freq) / fs * timepoints)

        highidx = 0
        if highpass_freq > 0:
            highidx = np.round(float(highpass_freq) / fs * timepoints)
        F[highidx:lowidx] = 1
        F = ((F + F[::-1]) > 0).astype(int)
        data = img.get_data()
        if np.all(F == 1):
            filtered_data = data
        else:
            filtered_data = np.real(np.fft.ifftn(np.fft.fftn(data) * F))
        img_out = nb.Nifti1Image(filtered_data, img.affine, img.header)
        img_out.to_filename(out_file)
        out_files.append(out_file)

    return list_to_filename(out_files)
Exemple #19
0
    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
def rename(in_files, suffix=None):
    from nipype.utils.filemanip import (filename_to_list, split_filename,
                                        list_to_filename)
    out_files = []
    for idx, filename in enumerate(filename_to_list(in_files)):
        _, name, ext = split_filename(filename)
        if suffix is None:
            out_files.append(name + ('_%03d' % idx) + ext)
        else:
            out_files.append(name + suffix + ext)
    return list_to_filename(out_files)
def rename(in_files, suffix=None):
    from nipype.utils.filemanip import (filename_to_list, split_filename,
                                        list_to_filename)
    out_files = []
    for idx, filename in enumerate(filename_to_list(in_files)):
        _, name, ext = split_filename(filename)
        if suffix is None:
            out_files.append(name + ('_%03d' % idx) + ext)
        else:
            out_files.append(name + suffix + ext)
    return list_to_filename(out_files)
Exemple #22
0
 def _list_outputs(self):
     subjects_dir = self.inputs.subjects_dir
     subject_path = os.path.join(subjects_dir, self.inputs.subject_id)
     output_traits = self._outputs()
     outputs = output_traits.get()
     for k in outputs.keys():
         val = self._get_files(subject_path, k,
                               output_traits.traits()[k].loc,
                               output_traits.traits()[k].altkey)
         if val:
             outputs[k] = list_to_filename(val)
     return outputs
Exemple #23
0
def selectindex(files, idx, flatten=True):
    """ Select the items in the list `files` in the indexes given by the list of
    integers `idx`."""
    import numpy as np
    from nipype.utils.filemanip import filename_to_list, list_to_filename
    from pypes._utils import flatten_list

    if flatten:
        files = flatten_list(files)

    if isinstance(idx, list):
        return list_to_filename(
            np.array(filename_to_list(files))[idx].tolist())
    else:
        return files[idx]
Exemple #24
0
 def _format_arg(self, opt, spec, val):
     """Convert input to appropriate format for spm
     """
     if opt == 'template':
         return scans_for_fname(filename_to_list(val))
     if opt == 'source':
         return scans_for_fname(filename_to_list(val))
     if opt == 'apply_to_files':
         return scans_for_fnames(filename_to_list(val))
     if opt == 'parameter_file':
         return np.array([list_to_filename(val)], dtype=object)
     if opt in ['write_wrap']:
         if len(val) != 3:
             raise ValueError('%s must have 3 elements' % opt)
     return super(Normalize, self)._format_arg(opt, spec, val)
Exemple #25
0
 def _list_outputs(self):
     #TODO: figure out file names and get rid off the globs
     outputs = self._outputs().get()
     root = self._get_design_root(list_to_filename(self.inputs.fsf_file))
     design_file = glob(os.path.join(os.getcwd(), '%s*.mat' % root))
     assert len(design_file) == 1, 'No mat file generated by FEAT Model'
     outputs['design_file'] = design_file[0]
     con_file = glob(os.path.join(os.getcwd(), '%s*.con' % root))
     assert len(con_file) == 1, 'No con file generated by FEAT Model'
     outputs['con_file'] = con_file[0]
     fcon_file = glob(os.path.join(os.getcwd(), '%s*.fts' % root))
     if fcon_file:
         assert len(fcon_file) == 1, 'No fts file generated by FEAT Model'
         outputs['fcon_file'] = fcon_file[0]
     return outputs
Exemple #26
0
 def _format_arg(self, opt, spec, val):
     """Convert input to appropriate format for spm
     """
     if opt == "template":
         return scans_for_fname(filename_to_list(val))
     if opt == "source":
         return scans_for_fname(filename_to_list(val))
     if opt == "apply_to_files":
         return scans_for_fnames(filename_to_list(val))
     if opt == "parameter_file":
         return np.array([list_to_filename(val)], dtype=object)
     if opt in ["write_wrap"]:
         if len(val) != 3:
             raise ValueError("%s must have 3 elements" % opt)
     return val
Exemple #27
0
 def _copyfiles_to_wd(self, outdir, execute):
     """ copy files over and change the inputs"""
     if hasattr(self._interface,'_get_filecopy_info'):
         for info in self._interface._get_filecopy_info():
             files = self.inputs.get().get(info['key'])
             if not isdefined(files):
                 continue
             if files:
                 infiles = filename_to_list(files)
                 if execute:
                     newfiles = copyfiles(infiles, [outdir], copy=info['copy'])
                 else:
                     newfiles = fnames_presuffix(infiles, newpath=outdir)
                 if not isinstance(files, list):
                     newfiles = list_to_filename(newfiles)
                 setattr(self.inputs, info['key'], newfiles)
Exemple #28
0
def rename(in_files, suffix=None):
    """Rename all the files in `in_files` adding the `suffix` keeping its
    extension and basedir."""
    import os.path as path
    from nipype.utils.filemanip import (filename_to_list, split_filename,
                                        list_to_filename)
    out_files = []
    for idx, filename in enumerate(filename_to_list(in_files)):
        base, name, ext = split_filename(filename)
        if suffix is None:
            new_name = name + ('_%03d' % idx) + ext
        else:
            new_name = name + suffix + ext

        out_files.append(path.join(base, new_name))

    return list_to_filename(out_files)
Exemple #29
0
def rename(in_files, suffix=None):
    """Rename all the files in `in_files` adding the `suffix` keeping its
    extension and basedir."""
    import os.path as path
    from nipype.utils.filemanip import (filename_to_list, split_filename,
                                        list_to_filename)
    out_files = []
    for idx, filename in enumerate(filename_to_list(in_files)):
        base, name, ext = split_filename(filename)
        if suffix is None:
            new_name = name + ('_%03d' % idx) + ext
        else:
            new_name = name + suffix + ext

        out_files.append(path.join(base, new_name))

    return list_to_filename(out_files)
Exemple #30
0
 def _make_matlab_command(self, content):
     """validates spm options and generates job structure
     if mfile is True uses matlab .m file
     else generates a job structure and saves in .mat
     """
     if isdefined(self.inputs.mask_image):
         # SPM doesn't handle explicit masking properly, especially
         # when you want to use the entire mask image
         postscript = "load SPM;\n"
         postscript += "SPM.xM.VM = spm_vol('%s');\n" % list_to_filename(self.inputs.mask_image)
         postscript += "SPM.xM.I = 0;\n"
         postscript += "SPM.xM.T = [];\n"
         postscript += "SPM.xM.TH = ones(size(SPM.xM.TH))*(%s);\n" % self.inputs.mask_threshold
         postscript += "SPM.xM.xs = struct('Masking', 'explicit masking only');\n"
         postscript += "save SPM SPM;\n"
     else:
         postscript = None
     return super(Level1Design, self)._make_matlab_command(content, postscript=postscript)
Exemple #31
0
 def _make_matlab_command(self, content):
     """validates spm options and generates job structure
     if mfile is True uses matlab .m file
     else generates a job structure and saves in .mat
     """
     if isdefined(self.inputs.mask_image):
         # SPM doesn't handle explicit masking properly, especially
         # when you want to use the entire mask image
         postscript = "load SPM;\n"
         postscript += "SPM.xM.VM = spm_vol('%s');\n" % list_to_filename(self.inputs.mask_image)
         postscript += "SPM.xM.I = 0;\n"
         postscript += "SPM.xM.T = [];\n"
         postscript += "SPM.xM.TH = ones(size(SPM.xM.TH))*(%s);\n" % self.inputs.mask_threshold
         postscript += "SPM.xM.xs = struct('Masking', 'explicit masking only');\n"
         postscript += "save SPM SPM;\n"
     else:
         postscript = None
     return super(Level1Design, self)._make_matlab_command(content, postscript=postscript)
def bandpass_filter(files, lowpass_freq, highpass_freq, fs):
    """Bandpass filter the input files

    Parameters
    ----------
    files: list of 4d nifti files
    lowpass_freq: cutoff frequency for the low pass filter (in Hz)
    highpass_freq: cutoff frequency for the high pass filter (in Hz)
    fs: sampling rate (in Hz)
    """
    from nipype.utils.filemanip import split_filename, list_to_filename
    import numpy as np
    import nibabel as nb
    from nipype.utils import NUMPY_MMAP
    out_files = []
    for filename in filename_to_list(files):
        path, name, ext = split_filename(filename)
        out_file = os.path.join(os.getcwd(), name + '_bp' + ext)
        img = nb.load(filename, mmap=NUMPY_MMAP)
        timepoints = img.shape[-1]
        F = np.zeros((timepoints))
        lowidx = int(timepoints / 2) + 1
        if lowpass_freq > 0:
            lowidx = np.round(lowpass_freq / fs * timepoints)
        highidx = 0
        if highpass_freq > 0:
            highidx = np.round(highpass_freq / fs * timepoints)
        F[highidx:lowidx] = 1
        F = ((F + F[::-1]) > 0).astype(int)
        data = img.get_data()
        if np.all(F == 1):
            filtered_data = data
        else:
            filtered_data = np.real(np.fft.ifftn(np.fft.fftn(data) * F))
        img_out = nb.Nifti1Image(filtered_data, img.affine, img.header)
        img_out.to_filename(out_file)
        out_files.append(out_file)
    return list_to_filename(out_files)
Exemple #33
0
    def _list_outputs(self):
        # infields are mandatory, however I could not figure out
        # how to set 'mandatory' flag dynamically, hence manual check

        cache_dir = self.inputs.cache_dir or tempfile.gettempdir()

        if self.inputs.config:
            xnat = pyxnat.Interface(config=self.inputs.config)
        else:
            xnat = pyxnat.Interface(self.inputs.server,
                                    self.inputs.user,
                                    self.inputs.pwd,
                                    cache_dir
                                    )

        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)
                           )
                    raise ValueError(msg)

        outputs = {}
        for key, args in self.inputs.query_template_args.items():
            outputs[key] = []
            template = self.inputs.query_template
            if hasattr(self.inputs, 'field_template') and \
                    isdefined(self.inputs.field_template) and \
                    self.inputs.field_template.has_key(key):
                template = self.inputs.field_template[key]
            if not args:
                file_objects = xnat.select(template).get('obj')
                if file_objects == []:
                    raise IOError('Template %s returned no files' \
                                      % template
                                  )
                outputs[key] = list_to_filename(
                                        [str(file_object.get())
                                         for file_object in file_objects
                                         if file_object.exists()
                                        ])
            for argnum, arglist in enumerate(args):
                maxlen = 1
                for arg in arglist:
                    if isinstance(arg, str) and hasattr(self.inputs, arg):
                        arg = getattr(self.inputs, arg)
                    if isinstance(arg, list):
                        if (maxlen > 1) and (len(arg) != maxlen):
                            raise ValueError('incompatible number '
                                             'of arguments for %s' % key
                                             )
                        if len(arg)>maxlen:
                            maxlen = len(arg)
                outfiles = []
                for i in range(maxlen):
                    argtuple = []
                    for arg in arglist:
                        if isinstance(arg, str) and \
                                hasattr(self.inputs, arg):
                            arg = getattr(self.inputs, arg)
                        if isinstance(arg, list):
                            argtuple.append(arg[i])
                        else:
                            argtuple.append(arg)
                    if argtuple:
                        target = template % tuple(argtuple)
                        file_objects = xnat.select(target).get('obj')

                        if file_objects == []:
                            raise IOError('Template %s '
                                          'returned no files' % target
                                          )

                        outfiles = list_to_filename(
                            [str(file_object.get())
                             for file_object in file_objects
                             if file_object.exists()
                             ]
                            )
                    else:
                        file_objects = xnat.select(template).get('obj')

                        if file_objects == []:
                            raise IOError('Template %s '
                                          'returned no files' % template
                                          )

                        outfiles = list_to_filename(
                            [str(file_object.get())
                             for file_object in file_objects
                             if file_object.exists()
                             ]
                            )

                    outputs[key].insert(i,outfiles)
            if len(outputs[key]) == 0:
                outputs[key] = None
            elif len(outputs[key]) == 1:
                outputs[key] = outputs[key][0]
        return outputs
Exemple #34
0
    def _list_outputs(self):
        # infields are mandatory, however I could not figure out how to set 'mandatory' flag dynamically
        # hence manual check

        cache_dir = self.inputs.cache_dir or tempfile.gettempdir()

        if self.inputs.xnat_server:
            xnat = pyxnat.Interface(self.inputs.xnat_server, self.inputs.xnat_user, self.inputs.xnat_pwd, cache_dir)
        else:
            xnat = pyxnat.Interface(self.inputs.xnat_config)

        #        xnat.set_offline_mode()

        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,
                    )
                    raise ValueError(msg)

        outputs = {}
        for key, args in self.inputs.query_template_args.items():
            outputs[key] = []
            template = self.inputs.query_template
            if (
                hasattr(self.inputs, "field_template")
                and isdefined(self.inputs.field_template)
                and self.inputs.field_template.has_key(key)
            ):
                template = self.inputs.field_template[key]
            if not args:
                file_objects = xnat.select(template).get("obj")
                if file_objects == []:
                    raise IOError("Template %s returned no files" % template)
                outputs[key] = list_to_filename([str(file_object.get()) for file_object in file_objects])
            for argnum, arglist in enumerate(args):
                maxlen = 1
                for arg in arglist:
                    if isinstance(arg, str) and hasattr(self.inputs, arg):
                        arg = getattr(self.inputs, arg)
                    if isinstance(arg, list):
                        if (maxlen > 1) and (len(arg) != maxlen):
                            raise ValueError("incompatible number of arguments for %s" % key)
                        if len(arg) > maxlen:
                            maxlen = len(arg)
                outfiles = []
                for i in range(maxlen):
                    argtuple = []
                    for arg in arglist:
                        if isinstance(arg, str) and hasattr(self.inputs, arg):
                            arg = getattr(self.inputs, arg)
                        if isinstance(arg, list):
                            argtuple.append(arg[i])
                        else:
                            argtuple.append(arg)
                    if argtuple:
                        file_objects = xnat.select(template % tuple(argtuple)).get("obj")
                        if file_objects == []:
                            raise IOError("Template %s returned no files" % (template % tuple(argtuple)))
                        outfiles = list_to_filename([str(file_object.get()) for file_object in file_objects])
                    else:
                        file_objects = xnat.select(template).get("obj")
                        if file_objects == []:
                            raise IOError("Template %s returned no files" % template)
                        outfiles = list_to_filename([str(file_object.get()) for file_object in file_objects])
                    outputs[key].insert(i, outfiles)
            if len(outputs[key]) == 0:
                outputs[key] = None
            elif len(outputs[key]) == 1:
                outputs[key] = outputs[key][0]
        return outputs
Exemple #35
0
    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)
                    raise ValueError(msg)

        outputs = {}
        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 not args:
                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.sort()
                    outputs[key] = list_to_filename(filelist)
            for argnum, arglist in enumerate(args):
                maxlen = 1
                for arg in arglist:
                    if isinstance(arg, str) and hasattr(self.inputs, arg):
                        arg = getattr(self.inputs, arg)
                    if isinstance(arg, list):
                        if (maxlen > 1) and (len(arg) != maxlen):
                            raise ValueError(
                                'incompatible number of arguments for %s' %
                                key)
                        if len(arg) > maxlen:
                            maxlen = len(arg)
                outfiles = []
                for i in range(maxlen):
                    argtuple = []
                    for arg in arglist:
                        if isinstance(arg, str) and hasattr(self.inputs, arg):
                            arg = getattr(self.inputs, arg)
                        if isinstance(arg, list):
                            argtuple.append(arg[i])
                        else:
                            argtuple.append(arg)
                    filledtemplate = template
                    if argtuple:
                        try:
                            filledtemplate = template % tuple(argtuple)
                        except TypeError as e:
                            raise TypeError(
                                e.message +
                                ": Template %s failed to convert with args %s"
                                % (template, str(tuple(argtuple))))
                    outfiles = glob.glob(filledtemplate)
                    if len(outfiles) == 0:
                        msg = 'Output key: %s Template: %s returned no files' % (
                            key, filledtemplate)
                        if self.inputs.raise_on_empty:
                            raise IOError(msg)
                        else:
                            warn(msg)
                        outputs[key].insert(i, None)
                    else:
                        if self.inputs.sort_filelist:
                            outfiles.sort()
                        outputs[key].insert(i, list_to_filename(outfiles))
            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
Exemple #36
0
def selectindex(files, idx):
    """ Select the items in the list `files` in the indexes given by the list of
    integers `idx`."""
    import numpy as np
    from nipype.utils.filemanip import filename_to_list, list_to_filename
    return list_to_filename(np.array(filename_to_list(files))[idx].tolist())
Exemple #37
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
 def selectindex(files, idx):
     import numpy as np
     from nipype.utils.filemanip import filename_to_list, list_to_filename
     return list_to_filename(
         np.array(filename_to_list(files))[idx].tolist())
 def selectN(files, N=1):
     from nipype.utils.filemanip import filename_to_list, list_to_filename
     return list_to_filename(filename_to_list(files)[:N])
Exemple #40
0
def selectindex(files, idx):
    """ Select the items in the list `files` in the indexes given by the list of
    integers `idx`."""
    import numpy as np
    from nipype.utils.filemanip import filename_to_list, list_to_filename
    return list_to_filename(np.array(filename_to_list(files))[idx].tolist())
Exemple #41
0
    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)
                    raise ValueError(msg)

        outputs = {}
        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 \
                    self.inputs.field_template.has_key(key):
                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 not args:
                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.sort()
                    outputs[key] = list_to_filename(filelist)
            for argnum, arglist in enumerate(args):
                maxlen = 1
                for arg in arglist:
                    if isinstance(arg, str) and hasattr(self.inputs, arg):
                        arg = getattr(self.inputs, arg)
                    if isinstance(arg, list):
                        if (maxlen > 1) and (len(arg) != maxlen):
                            raise ValueError('incompatible number of arguments for %s' % key)
                        if len(arg)>maxlen:
                            maxlen = len(arg)
                outfiles = []
                for i in range(maxlen):
                    argtuple = []
                    for arg in arglist:
                        if isinstance(arg, str) and hasattr(self.inputs, arg):
                            arg = getattr(self.inputs, arg)
                        if isinstance(arg, list):
                            argtuple.append(arg[i])
                        else:
                            argtuple.append(arg)
                    filledtemplate = template
                    if argtuple:
                        filledtemplate = template%tuple(argtuple)
                    outfiles = glob.glob(filledtemplate)
                    if len(outfiles) == 0:
                        msg = 'Output key: %s Template: %s returned no files'%(key, filledtemplate)
                        if self.inputs.raise_on_empty:
                            raise IOError(msg)
                        else:
                            warn(msg)
                        outputs[key].insert(i, None)
                    else:
                        if self.inputs.sort_filelist:
                            outfiles.sort()
                        outputs[key].insert(i,list_to_filename(outfiles))
            if any([val==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
Exemple #42
0
 def _list_outputs(self):
     # infields are mandatory, however I could not figure out how to set 'mandatory' flag dynamically
     # hence manual check
     config_info = load_json(self.inputs.config_file)
     cwd = os.getcwd()
     xnat = XNATInterface(config_info['url'], config_info['username'], config_info['password'], cachedir=cwd)
     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)
                 raise ValueError(msg)
             
     outputs = {}
     for key, args in self.inputs.query_template_args.items():
         outputs[key] = []
         template = self.inputs.query_template
         if hasattr(self.inputs, 'field_template') and \
                 isdefined(self.inputs.field_template) and \
                 self.inputs.field_template.has_key(key):
             template = self.inputs.field_template[key]
         if not args:
             file_objects = xnat.select(template).request_objects()
             if file_objects == []:
                 raise IOError('Template %s returned no files'%template)
             outputs[key] = list_to_filename([str(file_object.get()) for file_object in file_objects])
         for argnum, arglist in enumerate(args):
             maxlen = 1
             for arg in arglist:
                 if isinstance(arg, str) and hasattr(self.inputs, arg):
                     arg = getattr(self.inputs, arg)
                 if isinstance(arg, list):
                     if (maxlen > 1) and (len(arg) != maxlen):
                         raise ValueError('incompatible number of arguments for %s' % key)
                     if len(arg)>maxlen:
                         maxlen = len(arg)
             outfiles = []
             for i in range(maxlen):
                 argtuple = []
                 for arg in arglist:
                     if isinstance(arg, str) and hasattr(self.inputs, arg):
                         arg = getattr(self.inputs, arg)
                     if isinstance(arg, list):
                         argtuple.append(arg[i])
                     else:
                         argtuple.append(arg)
                 if argtuple:
                     file_objects = xnat.select(template%tuple(argtuple)).request_objects()
                     if file_objects == []:
                         raise IOError('Template %s returned no files'%(template%tuple(argtuple)))
                     outfiles = list_to_filename([str(file_object.get()) for file_object in file_objects])
                 else:
                     file_objects = xnat.select(template).request_objects()
                     if file_objects == []:
                         raise IOError('Template %s returned no files'%template)
                     outfiles = list_to_filename([str(file_object.get()) for file_object in file_objects])
                 outputs[key].insert(i,outfiles)
         if len(outputs[key]) == 0:
             outputs[key] = None
         elif len(outputs[key]) == 1:
             outputs[key] = outputs[key][0]
     return outputs
 def selectindex(files, idx):
     import numpy as np
     from nipype.utils.filemanip import filename_to_list, list_to_filename
     return list_to_filename(np.array(filename_to_list(files))[idx].tolist())
Exemple #44
0
def test_list_to_filename():
    x = list_to_filename(['foo.nii'])
    yield assert_equal, x, 'foo.nii'
    x = list_to_filename(['foo', 'bar'])
    yield assert_equal, x, ['foo', 'bar']
Exemple #45
0
    def _list_outputs(self):
        # infields are mandatory, however I could not figure out
        # how to set 'mandatory' flag dynamically, hence manual check

        cache_dir = self.inputs.cache_dir or tempfile.gettempdir()

        if self.inputs.config:
            xnat = pyxnat.Interface(config=self.inputs.config)
        else:
            xnat = pyxnat.Interface(self.inputs.server, self.inputs.user,
                                    self.inputs.pwd, cache_dir)

        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))
                    raise ValueError(msg)

        outputs = {}
        for key, args in self.inputs.query_template_args.items():
            outputs[key] = []
            template = self.inputs.query_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 not args:
                file_objects = xnat.select(template).get('obj')
                if file_objects == []:
                    raise IOError('Template %s returned no files' % template)
                outputs[key] = list_to_filename([
                    str(file_object.get()) for file_object in file_objects
                    if file_object.exists()
                ])
            for argnum, arglist in enumerate(args):
                maxlen = 1
                for arg in arglist:
                    if isinstance(arg, str) and hasattr(self.inputs, arg):
                        arg = getattr(self.inputs, arg)
                    if isinstance(arg, list):
                        if (maxlen > 1) and (len(arg) != maxlen):
                            raise ValueError('incompatible number '
                                             'of arguments for %s' % key)
                        if len(arg) > maxlen:
                            maxlen = len(arg)
                outfiles = []
                for i in range(maxlen):
                    argtuple = []
                    for arg in arglist:
                        if isinstance(arg, str) and \
                                hasattr(self.inputs, arg):
                            arg = getattr(self.inputs, arg)
                        if isinstance(arg, list):
                            argtuple.append(arg[i])
                        else:
                            argtuple.append(arg)
                    if argtuple:
                        target = template % tuple(argtuple)
                        file_objects = xnat.select(target).get('obj')

                        if file_objects == []:
                            raise IOError('Template %s '
                                          'returned no files' % target)

                        outfiles = list_to_filename([
                            str(file_object.get())
                            for file_object in file_objects
                            if file_object.exists()
                        ])
                    else:
                        file_objects = xnat.select(template).get('obj')

                        if file_objects == []:
                            raise IOError('Template %s '
                                          'returned no files' % template)

                        outfiles = list_to_filename([
                            str(file_object.get())
                            for file_object in file_objects
                            if file_object.exists()
                        ])

                    outputs[key].insert(i, outfiles)
            if len(outputs[key]) == 0:
                outputs[key] = None
            elif len(outputs[key]) == 1:
                outputs[key] = outputs[key][0]
        return outputs
Exemple #46
0
def test_list_to_filename():
    x = list_to_filename(['foo.nii'])
    yield assert_equal, x, 'foo.nii'
    x = list_to_filename(['foo', 'bar'])
    yield assert_equal, x, ['foo', 'bar']
Exemple #47
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