# NiBabel

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

# `openers`

Context manager openers for various fileobject types

`Fileish`(*args, **kwargs) |
`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

## `Fileish`

class nibabel.openers.Fileish(*args, **kwargs)

  Bases: `Protocol`

  __init__(*args, **kwargs)
  read(size: int = -1, /) → bytes
  write(b: bytes, /) → int | None

## `ImageOpener`

class nibabel.openers.ImageOpener(fileish: str | IOBase, *args, **kwargs)

  Bases: `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: str | IOBase, *args, **kwargs)
  compress_ext_map = {'.bz2': (<class 'bz2.BZ2File'>, ('mode', 'buffering', 'compresslevel')), '.gz': (<function gzip_open>, ('mode', 'compresslevel', 'mtime', 'keep_open')), '.mgz': (<function gzip_open>, ('mode', 'compresslevel', 'mtime', 'keep_open')), '.zst': (<function zstd_open>, ('mode', 'level', 'option', 'zstd_dict')), None: (<built-in function open>, ('mode', 'buffering'))}

## `Opener`

class nibabel.openers.Opener(fileish: str | IOBase, *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: str | IOBase, *args, **kwargs)
  bz2_def = (<class 'bz2.BZ2File'>, ('mode', 'buffering', 'compresslevel'))
  close() → None
  close_if_mine() → None

    Close `self.fobj` iff we opened it in the constructor

  property closed: bool
  compress_ext_icase: bool = True

    whether to ignore case looking for compression extensions

  compress_ext_map: dict[str | None, OpenerDef] = {'.bz2': (<class 'bz2.BZ2File'>, ('mode', 'buffering', 'compresslevel')), '.gz': (<function gzip_open>, ('mode', 'compresslevel', 'mtime', 'keep_open')), '.zst': (<function zstd_open>, ('mode', 'level', 'option', 'zstd_dict')), None: (<built-in function open>, ('mode', 'buffering'))}
  default_compresslevel = 1

    default compression level when writing gz and bz2 files

  default_zst_level = {'r': None, 'rb': None, 'w': 3, 'wb': 3}

    default compression level for zst files

  fileno() → int
  fobj: io.IOBase
  gz_def = (<function gzip_open>, ('mode', 'compresslevel', 'mtime', 'keep_open'))
  property mode: str
  property name: str | None

    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(size: int = -1, /) → bytes
  readinto(buffer: WriteableBuffer, /) → int | None
  seek(pos: int, whence: int = 0, /) → int
  tell() → int
  write(b: bytes, /) → int | None
  writelines(lines: ty.Iterable[ReadableBuffer], /) → None
  zstd_def = (<function zstd_open>, ('mode', 'level', 'option', 'zstd_dict'))