NIPY logo

Site Navigation

NIPY Community

Table Of Contents

Previous topic

io.imageformats.eulerangles

Next topic

io.imageformats.filetuples

This Page

io.imageformats.filename_parser

Module: io.imageformats.filename_parser

Inheritance diagram for nipy.io.imageformats.filename_parser:

Create filename pairs, triplets etc, with expected extensions

Class

TypesFilenamesError

class nipy.io.imageformats.filename_parser.TypesFilenamesError

Bases: exceptions.Exception

__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

args
message

Functions

nipy.io.imageformats.filename_parser.splitext_addext(filename, addexts=('.gz', '.bz2'))

Split /pth/fname.ext.gz into /pth/fname, .ext, .gz

where .gz may be any of passed addext trailing suffixes.

Parameters :

filename : str

filename that may end in any or none of addexts

Returns :

froot : str

Root of filename - e.g. /pth/fname in example above

ext : str

Extension, where extension is not in addexts - e.g. .ext in example above

addext : str

Any suffixes appearing in addext occuring at end of filename

Examples

>>> splitext_addext('fname.ext.gz')
('fname', '.ext', '.gz')
>>> splitext_addext('fname.ext')
('fname', '.ext', '')
>>> splitext_addext('fname.ext.foo', ('.foo', '.bar'))
('fname', '.ext', '.foo')
nipy.io.imageformats.filename_parser.types_filenames(template_fname, types_exts, trailing_suffixes=('.gz', '.bz2'), enforce_extensions=True)

Return filenames with standard extensions from template name

The typical case is returning image and header filenames for an Analyze image, that expects and ‘image’ file type, with, extension .img, and a ‘header’ file type, with extension .hdr.

Parameters :

template_fname : str

template filename from which to construct output dict of filenames, with given types_exts type to extension mapping. If self.enforce_extensions is True, then filename must have one of the defined extensions from the types list. If self.enforce_extensions is False, then the other filenames are guessed at by adding extensions to the base filename. Ignored suffixes (from trailing_suffixes) append themselves to the end of all the filenames.

types_exts : sequence of sequences

sequence of (name, extension) str sequences defining type to extension mapping.

trailing_suffixes : sequence of strings, optional

suffixes that should be ignored when looking for extensions - default is ('.gz', '.bz2')

enforce_extensions : {True, False}, optional

If True, raise an error when attempting to set value to type which has the wrong extension

Returns :

types_fnames : dict

dict with types as keys, and generated filenames as values. The types are given by the first elements of the tuples in types_exts.

Examples

>>> types_exts = (('t1','.ext1'),('t2', '.ext2'))
>>> tfns = types_filenames('/path/test.ext1', types_exts)
>>> tfns == {'t1': '/path/test.ext1', 't2': '/path/test.ext2'}
True

Bare file roots without extensions get them added

>>> tfns = types_filenames('/path/test', types_exts)
>>> tfns == {'t1': '/path/test.ext1', 't2': '/path/test.ext2'}
True

With enforce_extensions == False, allow first type to have any extension.

>>> tfns = types_filenames('/path/test.funny', types_exts,
...                        enforce_extensions=False)
>>> tfns == {'t1': '/path/test.funny', 't2': '/path/test.ext2'}
True