Example #1
0
def get_input_files(input_files_listing, base_dir):
    input_files = []

    if input_files_listing:
        for filename in input_files_listing:
            try:
                input_files.append(nifti_filepath_resolution(filename))
            except ValueError:
                input_files.append(
                    nifti_filepath_resolution(base_dir + '/' + filename))

    return input_files
Example #2
0
    def run(self, args, extra_args):
        mask = mdt.load_brain_mask(
            nifti_filepath_resolution(os.path.realpath(args.mask)))

        file_names = []
        for file in args.input_files:
            file_names.extend(glob.glob(file))

        for file in file_names:
            if args.overwrite:
                mdt.apply_mask_to_file(file, mask)
            else:
                folder, basename, ext = split_image_path(
                    nifti_filepath_resolution(os.path.realpath(file)))
                mdt.apply_mask_to_file(file,
                                       mask,
                                       output_fname=os.path.join(
                                           folder, basename + '_masked' + ext))
Example #3
0
 def find_niftis(paths, recurse=True):
     niftis = []
     for path in paths:
         if os.path.isdir(path):
             if recurse:
                 niftis.extend(find_niftis(glob.glob(path + '/*.nii*'), recurse=False))
         else:
             try:
                 niftis.append(nifti_filepath_resolution(path))
             except ValueError:
                 pass
     return niftis
Example #4
0
def load_nifti(nifti_volume):
    """Load and return a nifti file.

    This will apply path resolution if a filename without extension is given. See the function
    :func:`nifti_filepath_resolution` for details.

    Args:
        nifti_volume (string): The filename of the volume to use.

    Returns:
        :class:`nibabel.spatialimages.SpatialImage`
    """
    path = nifti_filepath_resolution(nifti_volume)
    return nib.load(path)
Example #5
0
    def run(self, args, extra_args):
        for image in args.images:
            image_path = os.path.realpath(image)

            try:
                image_path = nifti_filepath_resolution(image_path)
                img = load_nifti(image_path)
                header = img.get_header()
                print('{}'.format(image))
                self.print_info(header)
                print('')

            except ValueError:
                warnings.warn('Could not load image "{}"'.format(image_path))
Example #6
0
    def _load_niftis(self):
        niftis = []
        for filename in self._input_filenames:
            filename = nifti_filepath_resolution(filename)
            _, basename, extension = split_image_path(filename)

            if extension.endswith('gz'):
                unzipped_nifti_path = os.path.join(self._tmp_dir,
                                                   basename + '.nii')
                unzip_nifti(filename, unzipped_nifti_path)

                self._files_to_remove.append(unzipped_nifti_path)
                resolved_path = unzipped_nifti_path
            else:
                resolved_path = filename

            niftis.append(load_nifti(resolved_path))
        return niftis
Example #7
0
def get_input_file(input_file, base_dir):
    try:
        return nifti_filepath_resolution(input_file)
    except ValueError:
        return nifti_filepath_resolution(base_dir + '/' + input_file)