openers

Context manager openers for various fileobject types

BufferedGzipFile([filename, mode, ...]) GzipFile able to readinto buffer >= 2**32 bytes.
ImageOpener(fileish, *args, **kwargs) Opener-type class to collect extra compressed extensions
Opener(fileish, *args, **kwargs) Class to accept, maybe open, and context-manage file-likes / filenames

BufferedGzipFile

class nibabel.openers.BufferedGzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)

Bases: gzip.GzipFile

GzipFile able to readinto buffer >= 2**32 bytes.

This class only differs from gzip.GzipFile in Python 3.5.0.

This works around a known issue in Python 3.5. See https://bugs.python.org/issue25626

Constructor for the GzipFile class.

At least one of fileobj and filename must be given a non-trivial value.

The new class instance is based on fileobj, which can be a regular file, a StringIO object, or any other object which simulates a file. It defaults to None, in which case filename is opened to provide a file object.

When fileobj is not None, the filename argument is only used to be included in the gzip file header, which may includes the original filename of the uncompressed file. It defaults to the filename of fileobj, if discernible; otherwise, it defaults to the empty string, and in this case the original filename is not included in the header.

The mode argument can be any of ‘r’, ‘rb’, ‘a’, ‘ab’, ‘w’, or ‘wb’, depending on whether the file will be read or written. The default is the mode of fileobj if discernible; otherwise, the default is ‘rb’. Be aware that only the ‘rb’, ‘ab’, and ‘wb’ values should be used for cross-platform portability.

The compresslevel argument is an integer from 0 to 9 controlling the level of compression; 1 is fastest and produces the least compression, and 9 is slowest and produces the most compression. 0 is no compression at all. The default is 9.

The mtime argument is an optional numeric timestamp to be written to the stream when compressing. All gzip compressed streams are required to contain a timestamp. If omitted or None, the current time is used. This module ignores the timestamp when decompressing; however, some programs, such as gunzip, make use of it. The format of the timestamp is the same as that of the return value of time.time() and of the st_mtime member of the object returned by os.stat().

__init__(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)

Constructor for the GzipFile class.

At least one of fileobj and filename must be given a non-trivial value.

The new class instance is based on fileobj, which can be a regular file, a StringIO object, or any other object which simulates a file. It defaults to None, in which case filename is opened to provide a file object.

When fileobj is not None, the filename argument is only used to be included in the gzip file header, which may includes the original filename of the uncompressed file. It defaults to the filename of fileobj, if discernible; otherwise, it defaults to the empty string, and in this case the original filename is not included in the header.

The mode argument can be any of ‘r’, ‘rb’, ‘a’, ‘ab’, ‘w’, or ‘wb’, depending on whether the file will be read or written. The default is the mode of fileobj if discernible; otherwise, the default is ‘rb’. Be aware that only the ‘rb’, ‘ab’, and ‘wb’ values should be used for cross-platform portability.

The compresslevel argument is an integer from 0 to 9 controlling the level of compression; 1 is fastest and produces the least compression, and 9 is slowest and produces the most compression. 0 is no compression at all. The default is 9.

The mtime argument is an optional numeric timestamp to be written to the stream when compressing. All gzip compressed streams are required to contain a timestamp. If omitted or None, the current time is used. This module ignores the timestamp when decompressing; however, some programs, such as gunzip, make use of it. The format of the timestamp is the same as that of the return value of time.time() and of the st_mtime member of the object returned by os.stat().

ImageOpener

class nibabel.openers.ImageOpener(fileish, *args, **kwargs)

Bases: nibabel.openers.Opener

Opener-type class to collect extra compressed extensions

A trivial sub-class of opener to which image classes can add extra extensions with custom openers, such as compressed openers.

To add an extension, add a line to the class definition (not __init__):

ImageOpener.compress_ext_map[ext] = func_def

ext is a file extension beginning with ‘.’ and should be included in the image class’s valid_exts tuple.

func_def is a (function, (args,)) tuple, where function accepts a filename as the first parameter, and `args defines the other arguments that function accepts. These arguments must be any (unordered) subset of mode, compresslevel, and buffering.

__init__(fileish, *args, **kwargs)
compress_ext_map = {'.bz2': (<type 'bz2.BZ2File'>, ('mode', 'buffering', 'compresslevel')), '.mgz': (<function _gzip_open at 0x7fd5b0719a28>, ('mode', 'compresslevel', 'keep_open')), None: (<built-in function open>, ('mode', 'buffering')), '.gz': (<function _gzip_open at 0x7fd5b0719a28>, ('mode', 'compresslevel', 'keep_open'))}

Opener

class nibabel.openers.Opener(fileish, *args, **kwargs)

Bases: object

Class to accept, maybe open, and context-manage file-likes / filenames

Provides context manager to close files that the constructor opened for you.

Parameters:

fileish : str or file-like

if str, then open with suitable opening method. If file-like, accept as is

*args : positional arguments

passed to opening method when fileish is str. mode, if not specified, is rb. compresslevel, if relevant, and not specified, is set from class variable default_compresslevel. keep_open, if relevant, and not specified, is False.

**kwargs : keyword arguments

passed to opening method when fileish is str. Change of defaults as for *args

__init__(fileish, *args, **kwargs)
bz2_def = (<type 'bz2.BZ2File'>, ('mode', 'buffering', 'compresslevel'))
close(*args, **kwargs)
close_if_mine()

Close self.fobj iff we opened it in the constructor

closed
compress_ext_icase = True

whether to ignore case looking for compression extensions

compress_ext_map = {'.bz2': (<type 'bz2.BZ2File'>, ('mode', 'buffering', 'compresslevel')), None: (<built-in function open>, ('mode', 'buffering')), '.gz': (<function _gzip_open at 0x7fd5b0719a28>, ('mode', 'compresslevel', 'keep_open'))}
default_compresslevel = 1

default compression level when writing gz and bz2 files

fileno()
gz_def = (<function _gzip_open at 0x7fd5b0719a28>, ('mode', 'compresslevel', 'keep_open'))
mode
name

Return self.fobj.name or self._name if not present

self._name will be None if object was created with a fileobj, otherwise it will be the filename.

read(*args, **kwargs)
seek(*args, **kwargs)
tell(*args, **kwargs)
write(*args, **kwargs)