larmor.io.formats#
Reading and writing each file format; larmor.io loads them by path.
Reads the vertex indices of a FreeSurfer label file, such as |
|
Reads a FreeSurfer triangle surface, such as |
|
Reads a FreeSurfer per-vertex file ("new curv" format), such as |
|
Reads a GIfTI surface (.surf.gii) and any values per vertex stored with it. |
|
Reads the values per vertex of a GIfTI file (.shape.gii, .func.gii, .label.gii). |
|
Reads a MIF file ( |
|
Reads the gradient table ( |
|
Reads only the header of a MIF file, without the data. |
|
Reads a NIfTI-1 or NIfTI-2 file, plain or gzip, into a |
|
Reads only the header of a NIfTI file, without the data. |
|
Reads a TCK file (MRtrix tractogram) into a |
|
Reads a TRK file (TrackVis tractogram) into a |
|
Reads a legacy VTK polydata file of lines ( |
|
Writes a surface as a FreeSurfer triangle file. |
|
Writes values per vertex as a FreeSurfer "new curv" file. |
|
Writes a surface and its values per vertex to a GIfTI file (.surf.gii). |
|
Writes named values per vertex to a GIfTI file (.shape.gii or .func.gii). |
|
Writes a |
|
Writes a |
|
Writes a |
|
Writes a |
|
Writes a |
- larmor.io.formats.read_freesurfer_label(path)#
Reads the vertex indices of a FreeSurfer label file, such as
lh.cortex.label.The file is text: a comment line, the vertex count, then one line per vertex with its index, its tkregister coordinates and a value. Only the indices are returned; turn them into a mask for a surface with
mask = np.zeros(surface.num_vertices, dtype=bool); mask[indices] = True.- Parameters:
- Returns:
np.ndarray – (N,) int64 vertex indices, in the file’s order.
- Raises:
ValueError – If the count is not a number or is negative, or the file has fewer vertex lines than its count or a malformed one. The message starts with the path.
- Return type:
- larmor.io.formats.read_freesurfer_surface(path)#
Reads a FreeSurfer triangle surface, such as
lh.whiteorlh.pial.FreeSurfer stores vertices in tkregister space. With a valid volume info footer they are converted to scanner RAS and the footer’s volume becomes the reference; without one they are kept as stored and the reference is None.
- Parameters:
- Returns:
Surface – The mesh, with no values per vertex.
- Raises:
ValueError – If the file is not a FreeSurfer triangle file or is truncated.
- Return type:
- larmor.io.formats.read_freesurfer_values(path)#
Reads a FreeSurfer per-vertex file (“new curv” format), such as
lh.curv.- Parameters:
- Returns:
np.ndarray – (V,) float32, one value per vertex.
- Raises:
ValueError – If the file is not in the new curv format, holds more than one value per vertex, or is truncated.
- Return type:
- larmor.io.formats.read_gifti_surface(path)#
Reads a GIfTI surface (.surf.gii) and any values per vertex stored with it.
Files written by FreeSurfer (mris_convert) store tkregister RAS and describe the volume in the pointset’s VolGeom metadata: those vertices are converted to scanner RAS and the volume becomes the reference, as for a FreeSurfer surface, unless the pointset’s DataSpace already says scanner space. Other vertices are taken as stored (Workbench, fMRIPrep and nibabel write scanner RAS); the file’s coordinate transform is not applied.
- Parameters:
- Returns:
Surface –
- The mesh, with the file’s other arrays in
vertex_data, named after their “Name” metadata (or “data<index>”). The reference is the VolGeom volume, or None without one.
- The mesh, with the file’s other arrays in
- Raises:
ValueError – If the file is not GIfTI XML, does not hold exactly one pointset and one triangle array, an array cannot be decoded, or its VolGeom metadata is malformed. The message starts with the path.
- Return type:
- larmor.io.formats.read_gifti_values(path, *, unnamed=None)#
Reads the values per vertex of a GIfTI file (.shape.gii, .func.gii, .label.gii).
- Parameters:
- Returns:
dict[str, np.ndarray] –
- Each array that is not a mesh, named after its
”Name” metadata (or as
unnamedsays). Label files give the integer keys; their label table is not read.
- Raises:
ValueError – If the file is not GIfTI XML or an array cannot be decoded. The message starts with the path.
- Return type:
- larmor.io.formats.read_mif(path, *, on_progress=None)#
Reads a MIF file (
mif,mif.gzormih) into aVolume.The axes are those of the header’s
dim, whatever the layout on disk. The data keeps its on-disk type, in native byte order (Bit becomes bool);scalingis stored on theVolume, not applied.- Parameters:
- Returns:
Volume – The volume.
- Raises:
ValueError – If the file is not a MIF file Larmor can read, or is too short.
ReadCancelledError – If on_progress raises it.
- Return type:
- larmor.io.formats.read_mif_gradients(path)#
Reads the gradient table (
dw_scheme) of a MIF file, from its header only.The directions are already in world coordinates, so nothing is converted.
- Parameters:
- Returns:
GradientTable – The table.
- Raises:
ValueError – If the header has no
dw_scheme, a line is not 4 numbers, or the number of lines differs from the fourth dimension.- Return type:
- larmor.io.formats.read_mif_header(path)#
Reads only the header of a MIF file, without the data.
- larmor.io.formats.read_nifti(path, *, on_progress=None)#
Reads a NIfTI-1 or NIfTI-2 file, plain or gzip, into a
Volume.The data keeps its on-disk type, in native byte order;
scl_slopeandscl_interare stored on theVolume, not applied.- Parameters:
- Returns:
Volume – The image, with its scaling not applied.
- Raises:
ValueError – If the file is not a NIfTI file Larmor can read, or is too short.
ReadCancelledError – If on_progress raises it.
- Return type:
- larmor.io.formats.read_nifti_header(path)#
Reads only the header of a NIfTI file, without the data.
- larmor.io.formats.read_tck(path, *, on_progress=None)#
Reads a TCK file (MRtrix tractogram) into a
Tractogram.The data is read to the end, whatever the header’s
countsays: an incomplete last point or last streamline (an interruptedtckgen) is dropped with a warning, and a count that disagrees with the data, as in a file whose writing was interrupted, gives a warning. The points are float32; the tractogram has no reference grid.- Parameters:
- Returns:
The streamlines of the file.
- Raises:
ValueError – If the file is not a TCK file Larmor can read.
ReadCancelledError – If on_progress raises it.
- Return type:
- larmor.io.formats.read_trk(path, *, reference=None, on_progress=None)#
Reads a TRK file (TrackVis tractogram) into a
Tractogram.Points are converted from TrackVis voxel mm to world coordinates as nibabel does. Scalars go into
point_dataand properties intostreamline_data, named as in the header. The data is read to the end, whatevern_countsays; an incomplete last streamline is dropped with a warning. A file thatTrkWriterdid not finish (an interrupted run), or whosen_countis not 0 and disagrees with the data, gives a warning that it may be incomplete.- Parameters:
- Returns:
Tractogram – The streamlines, in world coordinates, with the file’s scalars and properties.
- Raises:
ValueError – If the file is not a TRK file Larmor can read, has no vox_to_ras and no reference is given, or the reference does not match the file.
ReadCancelledError – If on_progress raises it.
- Return type:
- larmor.io.formats.read_vtk(path, *, on_progress=None)#
Reads a legacy VTK polydata file of lines (
vtk,fib) into aTractogram.ASCII and binary files are read, with legacy or version 5.1
LINES. The points are taken as stored (world coordinates) and gathered in line order; points no line uses are dropped.POINT_DATAandCELL_DATAarrays go intopoint_dataandstreamline_data.- Parameters:
- Returns:
Tractogram – The streamlines, in world coordinates.
- Raises:
ValueError – If the file is not a legacy VTK polydata of lines Larmor can read.
ReadCancelledError – If on_progress raises it.
- Return type:
- larmor.io.formats.write_freesurfer_surface(surface, path)#
Writes a surface as a FreeSurfer triangle file.
With a reference, the vertices are converted back to tkregister space and the reference is written as the volume info footer. Values per vertex are not written; use
write_freesurfer_values.
- larmor.io.formats.write_freesurfer_values(values, path, *, num_faces=0)#
Writes values per vertex as a FreeSurfer “new curv” file.
- Parameters:
- Raises:
ValueError – If
valuesis not one-dimensional.
- larmor.io.formats.write_gifti_surface(surface, path)#
Writes a surface and its values per vertex to a GIfTI file (.surf.gii).
Vertices are written as float32 in scanner space, faces as int32, and each
vertex_dataarray as its own data array named by its key. The reference is not written: GIfTI has no place for it.- Parameters:
- Raises:
ValueError – If a
vertex_dataarray holds integers beyond int32.
- larmor.io.formats.write_gifti_values(values, path)#
Writes named values per vertex to a GIfTI file (.shape.gii or .func.gii).
- larmor.io.formats.write_mif(volume, path, *, gradients=None)#
Writes a
Volume, and optionally its gradient table, as MIF.A
mifholds header and data, compressed if the name ends in “.gz”; amihholds the header, with the data in adatof the same name. The data is written in its own type, little-endian, x fastest (layout: +0,+1,+2); bool as Bit. No other metadata is written.- Parameters:
volume (Volume) – The volume to write.
path (str | Path) – The
mif,mif.gzormihfile; existing files are overwritten.gradients (GradientTable | None) – Written into the header as
dw_scheme, in world coordinates. Defaults to None.
- Raises:
ValueError – If MIF cannot store the data’s type (complex, float16), or
gradientsdoes not have one entry per volume.
- larmor.io.formats.write_nifti(volume, path)#
Writes a
Volumeas a NIfTI file, compressed if the name ends in “.gz”.The file is NIfTI-1, or NIfTI-2 when a dimension is larger than NIfTI-1 can hold (32767), as MRtrix does; little-endian, in the data’s own type, with the volume’s slope and intercept. Boolean data (masks) is written as uint8. No other metadata is written.
- Parameters:
- Raises:
ValueError – If NIfTI cannot store the data’s type (for example float16).
- larmor.io.formats.write_tck(tractogram, path, *, timestamp=None)#
Writes a
Tractogramas a TCK file, as little-endian float32.Only the points are written: TCK holds no
point_data,streamline_dataor reference grid.- Parameters:
tractogram (Tractogram) – The tractogram to write.
path (str | Path) – The
tckfile; an existing file is overwritten.timestamp (str | None) – Written as the header’s
timestamp, which MRtrix uses to pair the file with its TSF files: pass the same value towrite_tsf. Defaults to None, no timestamp.
- Raises:
ValueError – If a point is NaN or infinite, which TCK reads as the end of a streamline or of the file, or
timestamphas a ‘#’ or a line break.
- larmor.io.formats.write_trk(tractogram, path, *, reference=None)#
Writes a
Tractogramas a TRK file (version 2, little-endian).The header comes from the reference grid:
dim,voxel_size,vox_to_rasandvoxel_order(the orientation of its affine).point_datais written as scalars andstreamline_dataas properties, named as nibabel names them.- Parameters:
tractogram (Tractogram) – The tractogram to write.
path (str | Path) – The
trkfile; an existing file is overwritten.reference (Grid | None) – The grid of the image the tractogram belongs to. Defaults to the tractogram’s own
reference.
- Raises:
ValueError – If there is no reference grid, its shape does not fit TRK, or there are too many or too long names.
- larmor.io.formats.write_vtk(tractogram, path, *, binary=True)#
Writes a
Tractogramas a legacy VTK polydata file (vtkorfib).Version 3.0, big-endian float32 points and int32 lines.
point_dataandstreamline_dataare written as FIELD arrays underPOINT_DATAandCELL_DATA, each in its own type (bool as unsigned_char).- Parameters:
tractogram (Tractogram) – The tractogram to write.
path (str | Path) – The
vtkorfibfile; an existing file is overwritten.binary (bool) – Binary (the default, smaller and faster) or ASCII.
- Raises:
ValueError – If a data name is empty or has whitespace, a data array has more than 2 dimensions or a type VTK cannot store, or the tractogram is too large for VTK’s int32 indices.