brainhops.io.images.minc
Readers for images stored in MINC files (.mnc), MINC1 and MINC2.
MINC is the volume format of the Montreal Neurological Institute and of
the MINC toolkit (MNI, CIVET, BigBrain and many rodent pipelines). It
comes in two containers, both named .mnc, which are recognised from
their content whatever the file name:
| Class | Container | Hints |
|---|---|---|
Minc1Image |
NetCDF classic (CDF\x01, CDF\x02) |
minc, minc.1 |
Minc2Image |
HDF5, with a /minc-2.0 group |
minc, minc.2 |
Both are read with nibabel (the minc extra, pip install
brainhops[minc]); MINC2 also needs h5py. A MINC1 file may be gzipped
(.mnc.gz). MincImage reads
either and returns the matching class.
import brainhops.io as io
image = io.images.load("t1.mnc") # a Minc1Image or a Minc2Image
image.data.shape # F order: (x, y, z) for z,y,x files
image.transformation # voxel -> MINC world (RAS, mm)
image.transformations[0] # voxel -> physical (|step|, mm)
image.dimensions # the raw MINC dimensions, file order
image.vox2world # the (4, 4) matrix of the world affine
Coordinate systems. The voxels are presented in F order (the file's
dimension order reversed), and the voxel axes are named after the MINC
dimensions (xspace -> x, yspace -> y, zspace -> z, time
-> t), whatever order the file stores them in. The world space is
MINC's: right, anterior and superior along x, y and z, with each
dimension running along its direction cosines from its start by its
(possibly negative) step. See brainhops.io.base.minc for the
file layout and the limitations.
Writing. nibabel cannot write MINC, so these classes are
read-only: save a MINC image to another format (e.g. NIfTI) instead.
Writing MINC would need another backend (pyminc, or the MINC
toolkit).
Metadata. The dimensions (start, step, direction cosines,
units) are kept on the object as
[dimensions][brainhops.io.base.minc.MincParser.dimensions]. The other
MINC attributes (patient, acquisition, history) are not read.
Classes
Minc1Image
Minc1Image(
dimensions: tuple[MincDimension, ...] = (),
_source: _Source | None = None,
)
Bases: MincImage
An image that is encoded by a MINC1 file: a NetCDF classic file,
possibly gzipped (.mnc.gz).
It answers to the hints "minc1" and "minc.1". See
MincImage.
Attributes
PREFIXES
class-attribute
PREFIXES: tuple[str, ...] = ()
Filename prefixes required by this parser, e.g. ("y_", "iy_").
An empty tuple means "no constraint". A parser that constrains the prefix is more specific than one that does not, and wins ties.
Declaring EXTENSIONS and PREFIXES separately states the
cross-product implicitly, which is how these conventions actually
work: SPM's four names are {y_, iy_} x {.nii, .nii.gz}.
PRIORITY
class-attribute
PRIORITY: int = 10
Kind precedence, used only to break ties that confidence could not.
A NIfTI file is legitimately both an image and a set of affines, so when nothing else separates them the image wins. Scoring sniffers (e.g. NIfTI intent codes) normally decide well before this matters.
shape
property
shape: tuple[int, ...] | None
The shape of the volume in F order (fastest dimension first).
grid
property
grid: CartesianField
The Cartesian field that defines the sampling grid of the image.
This is the grid of the image's geometry.
data
property
writable
The voxels in F order, scaled to real values, read on first access and cached, unless set explicitly.
transformations
property
writable
transformations: list[Transformation]
The voxel-to-physical and voxel-to-world transformations recorded by the dimensions, decoded on first access unless set explicitly.
transformation
property
writable
transformation: Transformation
The preferred transformation.
It is always the last transformation in the list.
Assigning a transformation appends it as the new preferred transformation. Assigning an integer or a string selects an existing transformation by position or by output-space name and moves it to the end. Assigning a transformation that is already in the list moves it to the end instead of adding a copy.
A transformation is recognized as already present by identity (transformations compare by identity): a distinct transformation with the same parameters is appended as a new preferred transformation.
geometry
property
geometry: Geometry
A transformation that is the composition of the preferred voxel-to-world transformation and the cartesian field corresponding to the image's shape.
This transformation can be used to reslice any image onto the same grid as this image.
VARIANTS
class-attribute
VARIANTS: tuple[type, ...] = ()
The classes that a class with no VERSION hands files to.
vox2world
property
The (4, 4) voxel-to-world (RAS) matrix of the F-ordered array.
Its columns follow the spatial axes of system (the
non-spatial ones, such as time, are skipped). It is None unless
the volume has the three spatial dimensions.
system
property
writable
system: CoordinateSystem | None
The voxel coordinate system, in F order, derived from the dimensions unless set explicitly.
Methods:
sniff
classmethod
sniff(
file: FileOrContentLike,
error: bool | Type[Exception] = False,
**kwargs,
) -> type | None
On a dispatcher, identify which registered format would read
file. On a concrete format, score how confident it is that
file, in any supported form, is its own.
sniff_file
classmethod
On a dispatcher, identify which registered format would read the file (path or file-like object). On a concrete format, score how confident it is that the file is its own.
sniff_filename
classmethod
sniff_filename(
filename: FilenameLike,
error: bool | Type[Exception] = False,
**kwargs,
) -> float
Score a file by its content (a MINC2 file is opened by name, so that only its header is read).
sniff_fileobj
classmethod
Score how confident the class is that an open file object holds a MINC file of its version, from its content.
A MINC1 file is a NetCDF file (gzipped or not) whose header names
a MINC spatial dimension and an image variable; a NetCDF file
that does not is only weakly accepted. A MINC2 file is an HDF5
file with a /minc-2.0 group.
sniff_content
classmethod
sniff_content(
content: ContentLike,
error: bool | Type[Exception] = False,
**kwargs,
) -> type | None
On a dispatcher, identify which registered format would read the content (text or bytes). On a concrete format, score how confident it is that the content is its own.
sniff_bytes
classmethod
Score how confident the class is that bytes hold a MINC file of its version.
sniff_text
classmethod
On a dispatcher, identify which registered format would read the text. On a concrete format, score how confident it is that the text is its own.
sniff_lines
classmethod
sniff_lines(
lines: Iterable[str],
error: bool | Type[Exception] = False,
**kwargs,
) -> type | None
On a dispatcher, identify which registered format would read the lines. On a concrete format, score how confident it is that the lines are its own.
sniff_line
classmethod
On a dispatcher, identify which registered format would read the line. On a concrete format, score how confident it is that the line is its own.
load
classmethod
load(other: FileOrContentLike, **kwargs) -> Self
On a dispatcher, pick the best-matching registered format and
build an instance of it from other. On a concrete format,
build an instance of this class from other, in any supported
form.
from_spec
classmethod
from_spec(spec: SourceSpec, **kwargs) -> Self
Load a structured source specification through this dispatcher.
from_file
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the file (path or file-like object). On a concrete format, build an instance of this class from the file.
from_filename
classmethod
from_filename(filename: FilenameLike, **kwargs) -> Self
Build the object from a MINC file.
Only the header is read: the voxels are read from the file, by name, on first access. A file that is not local, or that is gzipped, is read into memory first.
from_fileobj
classmethod
Build the object from an open MINC file object (gzipped or not), which is read into memory.
from_content
classmethod
from_content(content: ContentLike, **kwargs) -> Self
On a dispatcher, pick the best-matching registered format and build an instance of it from the content (text or bytes). On a concrete format, build an instance of this class from the content.
from_bytes
classmethod
Build the object from the bytes of a MINC file (gzipped or not).
from_text
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the text. On a concrete format, build an instance of this class from the text.
from_lines
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the lines. On a concrete format, build an instance of this class from the lines.
from_line
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the line. On a concrete format, build an instance of this class from the line.
from_dict
classmethod
Create an instance of the class from a dictionary-like object.
Only keys in the dictionary that match keyword-like fields of
this class, or the keywords its constructor takes without
storing them (its InitVars, such as the matrix= of an
Affine), will be used. Other keys are ignored, but see
from_other,
which refuses them.
Additional positional and/or keyword arguments can be provided, and will take precedence over the values in the dictionary.
A key naming a field that this class fixes (a field that cannot
be passed to its constructor) is checked instead of used: a
dictionary that sets it to anything other than None or the
value of this class is refused with a ValueError.
from_instance
classmethod
Create an instance from an instance of a similar class.
The data model copies the fields both classes share, by name.
A field that a file format declares for its own use -- such as
the nibabel image and header of the NIfTI and MGH formats
-- is only copied from an object of that same format: from any
other object, a field of the same name holds something else
(a NIfTI image is no MGH image), so this class's default is
kept instead. Saving a NIfTI image to MGH, or the converse,
therefore converts the data model only, and the format-specific
state is rebuilt by the writer.
from_other
classmethod
Create an instance from a file, or from anything the data model reads.
A path (str or os.PathLike), an open file, bytes or a
structured source (SourceSpec)
is read with load: on a dispatcher such as FileBasedImage,
the best-matching registered format reads it, and on a concrete
format, that format does. Any other value is handed to the data
model's own from_other, which reads a mapping field by field,
copies an instance of a similar class, and passes anything else
to the constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
A file, its content, a mapping, or an instance of a similar class. |
required |
*args
|
Constructor arguments. A file is read with keyword options only. |
()
|
|
**kwargs
|
Format-specific options when reading a file, and field values otherwise. |
{}
|
Returns:
| Type | Description |
|---|---|
obj
|
The object that was built. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If positional arguments come with a file to read. |
reslice
reslice(
geometry: Self
| Geometry
| Transformation
| None = None,
degree: int = 1,
bound: str = "reflect",
coeff: bool = False,
copy: bool = False,
) -> Self
Apply transformations to current data and return new image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry
|
Image | Geometry | Transformation
|
Geometry of the output image. The geometry is a voxel-to-world transformation that defines the grid onto which the image will be resliced. If it is a If it is |
None
|
degree
|
0..5
|
The spline degree. 0=nearest, 1=linear, 2=quadratic, etc. |
0..5
|
bound
|
(nearest, reflect, mirror, grid - wrap, wrap)
|
The boundary condition. If a string, one of: - 'nearest': nearest edge value (a a a a | a b c d | d d d d) - 'reflect': reflect at edge (d c b a | a b c d | d c b a) - 'mirror': mirror at edge (d c b | a b c d | c b a) - 'grid-wrap': wrap around (a b c d | a b c d | a b c d) - 'wrap': wrap around with shift (d b c d | a b c d | b c a b) If a float, the constant value to use beyond the edge. |
'nearest'
|
coeff
|
bool
|
If True, the input image is assumed to already contain spline coefficients. If False, the input image is prefiltered before interpolation. |
False
|
copy
|
bool
|
Whether the output data must be a fresh array. As with
|
False
|
Returns:
| Type | Description |
|---|---|
Image
|
The resliced image. |
__call__
__call__(transform: Transformation) -> SingleScaleImage
Apply a transformation to the image, but do not compute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Transformation
|
The transformation to apply. The output space of this transformation should match
(or be compatible with) the output space of the preferred
transformation. That is, the new "voxel-to-world" transformation
is defined as |
required |
Returns:
| Type | Description |
|---|---|
Image
|
The updated (not-yet-resliced) image. |
__getitem__
__getitem__(
index: tuple[int | slice | None, ...],
) -> SingleScaleImage
Index into the image data while preserving the geometry of the image.
Minc2Image
Minc2Image(
dimensions: tuple[MincDimension, ...] = (),
_source: _Source | None = None,
)
Bases: MincImage
An image that is encoded by a MINC2 file: an HDF5 file with a
/minc-2.0 group. Reading it needs h5py.
It answers to the hints "minc2" and "minc.2". See
MincImage.
Attributes
PREFIXES
class-attribute
PREFIXES: tuple[str, ...] = ()
Filename prefixes required by this parser, e.g. ("y_", "iy_").
An empty tuple means "no constraint". A parser that constrains the prefix is more specific than one that does not, and wins ties.
Declaring EXTENSIONS and PREFIXES separately states the
cross-product implicitly, which is how these conventions actually
work: SPM's four names are {y_, iy_} x {.nii, .nii.gz}.
PRIORITY
class-attribute
PRIORITY: int = 10
Kind precedence, used only to break ties that confidence could not.
A NIfTI file is legitimately both an image and a set of affines, so when nothing else separates them the image wins. Scoring sniffers (e.g. NIfTI intent codes) normally decide well before this matters.
shape
property
shape: tuple[int, ...] | None
The shape of the volume in F order (fastest dimension first).
grid
property
grid: CartesianField
The Cartesian field that defines the sampling grid of the image.
This is the grid of the image's geometry.
data
property
writable
The voxels in F order, scaled to real values, read on first access and cached, unless set explicitly.
transformations
property
writable
transformations: list[Transformation]
The voxel-to-physical and voxel-to-world transformations recorded by the dimensions, decoded on first access unless set explicitly.
transformation
property
writable
transformation: Transformation
The preferred transformation.
It is always the last transformation in the list.
Assigning a transformation appends it as the new preferred transformation. Assigning an integer or a string selects an existing transformation by position or by output-space name and moves it to the end. Assigning a transformation that is already in the list moves it to the end instead of adding a copy.
A transformation is recognized as already present by identity (transformations compare by identity): a distinct transformation with the same parameters is appended as a new preferred transformation.
geometry
property
geometry: Geometry
A transformation that is the composition of the preferred voxel-to-world transformation and the cartesian field corresponding to the image's shape.
This transformation can be used to reslice any image onto the same grid as this image.
VARIANTS
class-attribute
VARIANTS: tuple[type, ...] = ()
The classes that a class with no VERSION hands files to.
vox2world
property
The (4, 4) voxel-to-world (RAS) matrix of the F-ordered array.
Its columns follow the spatial axes of system (the
non-spatial ones, such as time, are skipped). It is None unless
the volume has the three spatial dimensions.
system
property
writable
system: CoordinateSystem | None
The voxel coordinate system, in F order, derived from the dimensions unless set explicitly.
Methods:
sniff
classmethod
sniff(
file: FileOrContentLike,
error: bool | Type[Exception] = False,
**kwargs,
) -> type | None
On a dispatcher, identify which registered format would read
file. On a concrete format, score how confident it is that
file, in any supported form, is its own.
sniff_file
classmethod
On a dispatcher, identify which registered format would read the file (path or file-like object). On a concrete format, score how confident it is that the file is its own.
sniff_filename
classmethod
sniff_filename(
filename: FilenameLike,
error: bool | Type[Exception] = False,
**kwargs,
) -> float
Score a file by its content (a MINC2 file is opened by name, so that only its header is read).
sniff_fileobj
classmethod
Score how confident the class is that an open file object holds a MINC file of its version, from its content.
A MINC1 file is a NetCDF file (gzipped or not) whose header names
a MINC spatial dimension and an image variable; a NetCDF file
that does not is only weakly accepted. A MINC2 file is an HDF5
file with a /minc-2.0 group.
sniff_content
classmethod
sniff_content(
content: ContentLike,
error: bool | Type[Exception] = False,
**kwargs,
) -> type | None
On a dispatcher, identify which registered format would read the content (text or bytes). On a concrete format, score how confident it is that the content is its own.
sniff_bytes
classmethod
Score how confident the class is that bytes hold a MINC file of its version.
sniff_text
classmethod
On a dispatcher, identify which registered format would read the text. On a concrete format, score how confident it is that the text is its own.
sniff_lines
classmethod
sniff_lines(
lines: Iterable[str],
error: bool | Type[Exception] = False,
**kwargs,
) -> type | None
On a dispatcher, identify which registered format would read the lines. On a concrete format, score how confident it is that the lines are its own.
sniff_line
classmethod
On a dispatcher, identify which registered format would read the line. On a concrete format, score how confident it is that the line is its own.
load
classmethod
load(other: FileOrContentLike, **kwargs) -> Self
On a dispatcher, pick the best-matching registered format and
build an instance of it from other. On a concrete format,
build an instance of this class from other, in any supported
form.
from_spec
classmethod
from_spec(spec: SourceSpec, **kwargs) -> Self
Load a structured source specification through this dispatcher.
from_file
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the file (path or file-like object). On a concrete format, build an instance of this class from the file.
from_filename
classmethod
from_filename(filename: FilenameLike, **kwargs) -> Self
Build the object from a MINC file.
Only the header is read: the voxels are read from the file, by name, on first access. A file that is not local, or that is gzipped, is read into memory first.
from_fileobj
classmethod
Build the object from an open MINC file object (gzipped or not), which is read into memory.
from_content
classmethod
from_content(content: ContentLike, **kwargs) -> Self
On a dispatcher, pick the best-matching registered format and build an instance of it from the content (text or bytes). On a concrete format, build an instance of this class from the content.
from_bytes
classmethod
Build the object from the bytes of a MINC file (gzipped or not).
from_text
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the text. On a concrete format, build an instance of this class from the text.
from_lines
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the lines. On a concrete format, build an instance of this class from the lines.
from_line
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the line. On a concrete format, build an instance of this class from the line.
from_dict
classmethod
Create an instance of the class from a dictionary-like object.
Only keys in the dictionary that match keyword-like fields of
this class, or the keywords its constructor takes without
storing them (its InitVars, such as the matrix= of an
Affine), will be used. Other keys are ignored, but see
from_other,
which refuses them.
Additional positional and/or keyword arguments can be provided, and will take precedence over the values in the dictionary.
A key naming a field that this class fixes (a field that cannot
be passed to its constructor) is checked instead of used: a
dictionary that sets it to anything other than None or the
value of this class is refused with a ValueError.
from_instance
classmethod
Create an instance from an instance of a similar class.
The data model copies the fields both classes share, by name.
A field that a file format declares for its own use -- such as
the nibabel image and header of the NIfTI and MGH formats
-- is only copied from an object of that same format: from any
other object, a field of the same name holds something else
(a NIfTI image is no MGH image), so this class's default is
kept instead. Saving a NIfTI image to MGH, or the converse,
therefore converts the data model only, and the format-specific
state is rebuilt by the writer.
from_other
classmethod
Create an instance from a file, or from anything the data model reads.
A path (str or os.PathLike), an open file, bytes or a
structured source (SourceSpec)
is read with load: on a dispatcher such as FileBasedImage,
the best-matching registered format reads it, and on a concrete
format, that format does. Any other value is handed to the data
model's own from_other, which reads a mapping field by field,
copies an instance of a similar class, and passes anything else
to the constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
A file, its content, a mapping, or an instance of a similar class. |
required |
*args
|
Constructor arguments. A file is read with keyword options only. |
()
|
|
**kwargs
|
Format-specific options when reading a file, and field values otherwise. |
{}
|
Returns:
| Type | Description |
|---|---|
obj
|
The object that was built. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If positional arguments come with a file to read. |
reslice
reslice(
geometry: Self
| Geometry
| Transformation
| None = None,
degree: int = 1,
bound: str = "reflect",
coeff: bool = False,
copy: bool = False,
) -> Self
Apply transformations to current data and return new image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry
|
Image | Geometry | Transformation
|
Geometry of the output image. The geometry is a voxel-to-world transformation that defines the grid onto which the image will be resliced. If it is a If it is |
None
|
degree
|
0..5
|
The spline degree. 0=nearest, 1=linear, 2=quadratic, etc. |
0..5
|
bound
|
(nearest, reflect, mirror, grid - wrap, wrap)
|
The boundary condition. If a string, one of: - 'nearest': nearest edge value (a a a a | a b c d | d d d d) - 'reflect': reflect at edge (d c b a | a b c d | d c b a) - 'mirror': mirror at edge (d c b | a b c d | c b a) - 'grid-wrap': wrap around (a b c d | a b c d | a b c d) - 'wrap': wrap around with shift (d b c d | a b c d | b c a b) If a float, the constant value to use beyond the edge. |
'nearest'
|
coeff
|
bool
|
If True, the input image is assumed to already contain spline coefficients. If False, the input image is prefiltered before interpolation. |
False
|
copy
|
bool
|
Whether the output data must be a fresh array. As with
|
False
|
Returns:
| Type | Description |
|---|---|
Image
|
The resliced image. |
__call__
__call__(transform: Transformation) -> SingleScaleImage
Apply a transformation to the image, but do not compute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Transformation
|
The transformation to apply. The output space of this transformation should match
(or be compatible with) the output space of the preferred
transformation. That is, the new "voxel-to-world" transformation
is defined as |
required |
Returns:
| Type | Description |
|---|---|
Image
|
The updated (not-yet-resliced) image. |
__getitem__
__getitem__(
index: tuple[int | slice | None, ...],
) -> SingleScaleImage
Index into the image data while preserving the geometry of the image.
MincImage
MincImage(
dimensions: tuple[MincDimension, ...] = (),
_source: _Source | None = None,
)
Bases: MincParser, FileBasedImage, SingleScaleImage
An image that is encoded by a MINC file (MINC1 or MINC2).
This class reads both versions and hands a file to
Minc1Image or
Minc2Image, which are the
classes registered for dispatch. It answers to the hint "minc".
The voxels are read with nibabel, scaled to real values (MINC's
image-min/image-max, per slice or global), and presented in F
order: the dimensions are reversed from the order the file lists
them in, so that a zspace, yspace, xspace file reads as
(x, y, z). The voxel axes are named after the MINC dimensions
(xspace -> x, yspace -> y, zspace -> z, time -> t),
whatever their order in the file.
The transformations are, in order:
- a
Scalingfrom voxels to the scaled voxel space"physical": the absolutestepof each dimension, in itsunits(millimetres by default for the spatial ones); - the voxel-to-world affine, whose output is the RAS space named
"world": each spatial dimension runs along its direction cosines, scaled by its (signed)step, from itsstart. It is the preferred transformation. It is only recorded for a volume with the three spatial dimensions.
MINC cannot be written: nibabel only reads it.
Why the bases are in this order
As for NiftiImage:
SingleScaleImage.data has no default, so it comes last, and
MincParser leads so that its lazy data/system properties
win.
Attributes
PREFIXES
class-attribute
PREFIXES: tuple[str, ...] = ()
Filename prefixes required by this parser, e.g. ("y_", "iy_").
An empty tuple means "no constraint". A parser that constrains the prefix is more specific than one that does not, and wins ties.
Declaring EXTENSIONS and PREFIXES separately states the
cross-product implicitly, which is how these conventions actually
work: SPM's four names are {y_, iy_} x {.nii, .nii.gz}.
PRIORITY
class-attribute
PRIORITY: int = 10
Kind precedence, used only to break ties that confidence could not.
A NIfTI file is legitimately both an image and a set of affines, so when nothing else separates them the image wins. Scoring sniffers (e.g. NIfTI intent codes) normally decide well before this matters.
shape
property
shape: tuple[int, ...] | None
The shape of the volume in F order (fastest dimension first).
grid
property
grid: CartesianField
The Cartesian field that defines the sampling grid of the image.
This is the grid of the image's geometry.
data
property
writable
The voxels in F order, scaled to real values, read on first access and cached, unless set explicitly.
transformation
property
writable
transformation: Transformation
The preferred transformation.
It is always the last transformation in the list.
Assigning a transformation appends it as the new preferred transformation. Assigning an integer or a string selects an existing transformation by position or by output-space name and moves it to the end. Assigning a transformation that is already in the list moves it to the end instead of adding a copy.
A transformation is recognized as already present by identity (transformations compare by identity): a distinct transformation with the same parameters is appended as a new preferred transformation.
geometry
property
geometry: Geometry
A transformation that is the composition of the preferred voxel-to-world transformation and the cartesian field corresponding to the image's shape.
This transformation can be used to reslice any image onto the same grid as this image.
VERSION
class-attribute
VERSION: int | None = None
The MINC version read by this class, or None for both.
VARIANTS
class-attribute
VARIANTS: tuple[type, ...] = ()
The classes that a class with no VERSION hands files to.
vox2world
property
The (4, 4) voxel-to-world (RAS) matrix of the F-ordered array.
Its columns follow the spatial axes of system (the
non-spatial ones, such as time, are skipped). It is None unless
the volume has the three spatial dimensions.
system
property
writable
system: CoordinateSystem | None
The voxel coordinate system, in F order, derived from the dimensions unless set explicitly.
transformations
property
writable
transformations: list[Transformation]
The voxel-to-physical and voxel-to-world transformations recorded by the dimensions, decoded on first access unless set explicitly.
Methods:
sniff
classmethod
sniff(
file: FileOrContentLike,
error: bool | Type[Exception] = False,
**kwargs,
) -> type | None
On a dispatcher, identify which registered format would read
file. On a concrete format, score how confident it is that
file, in any supported form, is its own.
sniff_file
classmethod
On a dispatcher, identify which registered format would read the file (path or file-like object). On a concrete format, score how confident it is that the file is its own.
sniff_filename
classmethod
sniff_filename(
filename: FilenameLike,
error: bool | Type[Exception] = False,
**kwargs,
) -> float
Score a file by its content (a MINC2 file is opened by name, so that only its header is read).
sniff_fileobj
classmethod
Score how confident the class is that an open file object holds a MINC file of its version, from its content.
A MINC1 file is a NetCDF file (gzipped or not) whose header names
a MINC spatial dimension and an image variable; a NetCDF file
that does not is only weakly accepted. A MINC2 file is an HDF5
file with a /minc-2.0 group.
sniff_content
classmethod
sniff_content(
content: ContentLike,
error: bool | Type[Exception] = False,
**kwargs,
) -> type | None
On a dispatcher, identify which registered format would read the content (text or bytes). On a concrete format, score how confident it is that the content is its own.
sniff_bytes
classmethod
Score how confident the class is that bytes hold a MINC file of its version.
sniff_text
classmethod
On a dispatcher, identify which registered format would read the text. On a concrete format, score how confident it is that the text is its own.
sniff_lines
classmethod
sniff_lines(
lines: Iterable[str],
error: bool | Type[Exception] = False,
**kwargs,
) -> type | None
On a dispatcher, identify which registered format would read the lines. On a concrete format, score how confident it is that the lines are its own.
sniff_line
classmethod
On a dispatcher, identify which registered format would read the line. On a concrete format, score how confident it is that the line is its own.
load
classmethod
load(other: FileOrContentLike, **kwargs) -> Self
On a dispatcher, pick the best-matching registered format and
build an instance of it from other. On a concrete format,
build an instance of this class from other, in any supported
form.
from_spec
classmethod
from_spec(spec: SourceSpec, **kwargs) -> Self
Load a structured source specification through this dispatcher.
from_file
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the file (path or file-like object). On a concrete format, build an instance of this class from the file.
from_filename
classmethod
from_filename(filename: FilenameLike, **kwargs) -> Self
Build the object from a MINC file.
Only the header is read: the voxels are read from the file, by name, on first access. A file that is not local, or that is gzipped, is read into memory first.
from_fileobj
classmethod
Build the object from an open MINC file object (gzipped or not), which is read into memory.
from_content
classmethod
from_content(content: ContentLike, **kwargs) -> Self
On a dispatcher, pick the best-matching registered format and build an instance of it from the content (text or bytes). On a concrete format, build an instance of this class from the content.
from_bytes
classmethod
Build the object from the bytes of a MINC file (gzipped or not).
from_text
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the text. On a concrete format, build an instance of this class from the text.
from_lines
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the lines. On a concrete format, build an instance of this class from the lines.
from_line
classmethod
On a dispatcher, pick the best-matching registered format and build an instance of it from the line. On a concrete format, build an instance of this class from the line.
from_dict
classmethod
Create an instance of the class from a dictionary-like object.
Only keys in the dictionary that match keyword-like fields of
this class, or the keywords its constructor takes without
storing them (its InitVars, such as the matrix= of an
Affine), will be used. Other keys are ignored, but see
from_other,
which refuses them.
Additional positional and/or keyword arguments can be provided, and will take precedence over the values in the dictionary.
A key naming a field that this class fixes (a field that cannot
be passed to its constructor) is checked instead of used: a
dictionary that sets it to anything other than None or the
value of this class is refused with a ValueError.
from_instance
classmethod
Create an instance from an instance of a similar class.
The data model copies the fields both classes share, by name.
A field that a file format declares for its own use -- such as
the nibabel image and header of the NIfTI and MGH formats
-- is only copied from an object of that same format: from any
other object, a field of the same name holds something else
(a NIfTI image is no MGH image), so this class's default is
kept instead. Saving a NIfTI image to MGH, or the converse,
therefore converts the data model only, and the format-specific
state is rebuilt by the writer.
from_other
classmethod
Create an instance from a file, or from anything the data model reads.
A path (str or os.PathLike), an open file, bytes or a
structured source (SourceSpec)
is read with load: on a dispatcher such as FileBasedImage,
the best-matching registered format reads it, and on a concrete
format, that format does. Any other value is handed to the data
model's own from_other, which reads a mapping field by field,
copies an instance of a similar class, and passes anything else
to the constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Any
|
A file, its content, a mapping, or an instance of a similar class. |
required |
*args
|
Constructor arguments. A file is read with keyword options only. |
()
|
|
**kwargs
|
Format-specific options when reading a file, and field values otherwise. |
{}
|
Returns:
| Type | Description |
|---|---|
obj
|
The object that was built. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If positional arguments come with a file to read. |
reslice
reslice(
geometry: Self
| Geometry
| Transformation
| None = None,
degree: int = 1,
bound: str = "reflect",
coeff: bool = False,
copy: bool = False,
) -> Self
Apply transformations to current data and return new image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry
|
Image | Geometry | Transformation
|
Geometry of the output image. The geometry is a voxel-to-world transformation that defines the grid onto which the image will be resliced. If it is a If it is |
None
|
degree
|
0..5
|
The spline degree. 0=nearest, 1=linear, 2=quadratic, etc. |
0..5
|
bound
|
(nearest, reflect, mirror, grid - wrap, wrap)
|
The boundary condition. If a string, one of: - 'nearest': nearest edge value (a a a a | a b c d | d d d d) - 'reflect': reflect at edge (d c b a | a b c d | d c b a) - 'mirror': mirror at edge (d c b | a b c d | c b a) - 'grid-wrap': wrap around (a b c d | a b c d | a b c d) - 'wrap': wrap around with shift (d b c d | a b c d | b c a b) If a float, the constant value to use beyond the edge. |
'nearest'
|
coeff
|
bool
|
If True, the input image is assumed to already contain spline coefficients. If False, the input image is prefiltered before interpolation. |
False
|
copy
|
bool
|
Whether the output data must be a fresh array. As with
|
False
|
Returns:
| Type | Description |
|---|---|
Image
|
The resliced image. |
__call__
__call__(transform: Transformation) -> SingleScaleImage
Apply a transformation to the image, but do not compute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Transformation
|
The transformation to apply. The output space of this transformation should match
(or be compatible with) the output space of the preferred
transformation. That is, the new "voxel-to-world" transformation
is defined as |
required |
Returns:
| Type | Description |
|---|---|
Image
|
The updated (not-yet-resliced) image. |
__getitem__
__getitem__(
index: tuple[int | slice | None, ...],
) -> SingleScaleImage
Index into the image data while preserving the geometry of the image.