# NiBabel

### Access a cacophony of neuro-imaging file formats

# `_compression`

Constants and types for dealing transparently with compression

`DeterministicGzipFile`([filename, mode, ...]) | Deterministic variant of GzipFile
`gzip_open`(filename[, mode, compresslevel, ...]) | Open a gzip file for reading or writing.
`zstd_open`(filename[, mode, level, options, ...]) | Open a zstd file for reading or writing.

## `DeterministicGzipFile`

class nibabel._compression.DeterministicGzipFile(filename: str | None = None, mode: Mode | None = None, compresslevel: int = 9, fileobj: io.FileIO | None = None, mtime: int = 0)

  Bases: `GzipFile`

  Deterministic variant of GzipFile

  This writer does not add filename information to the header, and defaults to a modification time (`mtime`) of 0 seconds.

  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, an io.BytesIO 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 include 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’, ‘wb’, ‘x’, or ‘xb’ depending on whether the file will be read or written. The default is the mode of fileobj if discernible; otherwise, the default is ‘rb’. A mode of ‘r’ is equivalent to one of ‘rb’, and similarly for ‘w’ and ‘wb’, ‘a’ and ‘ab’, and ‘x’ and ‘xb’.

  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 optional mtime argument is the timestamp requested by gzip. The time is in Unix format, i.e., seconds since 00:00:00 UTC, January 1, 1970. If mtime is omitted or None, the current time is used. Use mtime = 0 to generate a compressed stream that does not depend on creation time.

  __init__(filename: str | None = None, mode: Mode | None = None, compresslevel: int = 9, fileobj: io.FileIO | None = None, mtime: int = 0)

    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, an io.BytesIO 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 include 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’, ‘wb’, ‘x’, or ‘xb’ depending on whether the file will be read or written. The default is the mode of fileobj if discernible; otherwise, the default is ‘rb’. A mode of ‘r’ is equivalent to one of ‘rb’, and similarly for ‘w’ and ‘wb’, ‘a’ and ‘ab’, and ‘x’ and ‘xb’.

    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 optional mtime argument is the timestamp requested by gzip. The time is in Unix format, i.e., seconds since 00:00:00 UTC, January 1, 1970. If mtime is omitted or None, the current time is used. Use mtime = 0 to generate a compressed stream that does not depend on creation time.

## gzip_open

nibabel._compression.gzip_open(filename: str, mode: Mode = 'rb', compresslevel: int = 9, mtime: int = 0, keep_open: bool = False) → gzip.GzipFile

  Open a gzip file for reading or writing.

  If opening a file for reading, and `indexed_gzip` is available, an `IndexedGzipFile` is returned.

  Otherwise (opening for writing, or `indexed_gzip` not available), a `DeterministicGzipFile` is returned.

## zstd_open

nibabel._compression.zstd_open(filename: str, mode: Mode = 'r', *, level: int | None = None, options: dict | None = None, zstd_dict: zstd.ZstdDict | None = None, level_or_option: int | dict | None = None) → zstd.ZstdFile

  Open a zstd file for reading or writing.

  The specific object returned will be a `compression.zstd.ZstdFile` or a `backports.zstd.ZstdFile`.

  Parameters:
    filename str

      Path of file to open.

    mode str

      Opening mode.

    zstd_dict ZstdDict

      Dictionary used for compression/decompression.

    level int

      Compression level when writing.

    options dict

      Dictionary of compression/decompression options.