|
libfs
Header-only C++11 library for accessing FreeSurfer neuroimaging data
|
#include <iostream>#include <climits>#include <stdio.h>#include <vector>#include <fstream>#include <cassert>#include <sstream>#include <stdexcept>#include <map>#include <unordered_set>#include <unordered_map>#include <cmath>#include <algorithm>#include <chrono>#include <cstdint>#include <cstring>
Go to the source code of this file.
Classes | |
| struct | fs::Mesh |
| Models a triangular mesh, used for brain surface meshes. More... | |
| struct | fs::Curv |
| Models a FreeSurfer curv file that contains per-vertex float data. More... | |
| struct | fs::Colortable |
| The colortable from an Annot file, can be used for parcellations and integer labels. Typically each index (in all fields) describes a brain region. More... | |
| struct | fs::Annot |
| An annotation, also known as a brain surface parcellation. Assigns to each vertex a region, identified by the region_label. The region name and color for each region can be found in the Colortable. More... | |
| struct | fs::MghHeader |
| Models the header of an MGH file. More... | |
| struct | fs::MghData |
| Models the data of an MGH file. Currently these are 1D vectors, but one can compute the 4D array using the dimXlength fields of the respective MghHeader. More... | |
| struct | fs::Mgh |
| Models a whole MGH file. More... | |
| struct | fs::Array4D< T > |
| A simple 4D array datastructure, useful for representing volume data. More... | |
| struct | fs::Nifti1Header |
| NIfTI-1 header structure (348 bytes, packed). More... | |
| struct | fs::Label |
Macros | |
| #define | LIBFS_VERSION "0.5.0" |
| Full libfs version string, in semver format. More... | |
| #define | LIBFS_VERSION_MAJOR 0 |
| Major version number (incompatible API changes). More... | |
| #define | LIBFS_VERSION_MINOR 5 |
| Minor version number (backward-compatible feature additions). More... | |
| #define | LIBFS_VERSION_PATCH 0 |
| Patch version number (backward-compatible bug fixes). More... | |
| #define | LIBFS_MAX_ALLOC_BYTES_DEFAULT (2ULL * 1024ULL * 1024ULL * 1024ULL) |
| #define | LIBFS_MAX_ALLOC_BYTES LIBFS_MAX_ALLOC_BYTES_DEFAULT |
| Maximum memory allocation limit. | |
| #define | LIBFS_MAX_STRING_LENGTH 4096 |
| Maximum length for fixed-length strings read from binary headers (e.g., filenames in annot colortables). | |
| #define | LIBFS_MAX_COLORTABLE_ENTRIES 10000 |
| Maximum number of entries in an annotation colortable. | |
| #define | LIBFS_APPTAG "[libfs] " |
| Application tag prepended to every debug message from libfs. More... | |
| #define | LIBFS_DBG_WARNING |
| Default debug level: emit warnings and everything more severe. More... | |
| #define | LIBFS_DBG_ERROR |
| Debug level: emit errors and everything more severe. More... | |
| #define | LIBFS_DBG_CRITICAL |
| Debug level: emit only critical errors. More... | |
Functions | |
| std::string | fs::util::time_tag (std::chrono::system_clock::time_point t) |
| Get current time as string, e.g. for log messages. More... | |
| void | fs::util::log (std::string const &message, std::string const loglevel="INFO") |
| Log a message, goes to stdout. More... | |
| template<typename T > | |
| std::vector< T > | fs::util::vflatten (std::vector< std::vector< T >> values) |
| Flatten 2D vector. More... | |
| bool | fs::util::starts_with (std::string const &value, std::initializer_list< std::string > prefixes) |
| Check whether a string starts with one of the given prefixes. More... | |
| bool | fs::util::file_exists (const std::string &name) |
| Check whether a file exists (can be read) at given path. More... | |
| std::string | fs::util::fullpath (std::initializer_list< std::string > path_components, std::string path_sep=std::string("/")) |
| Construct a UNIX file system path from the given path_components. More... | |
| void | fs::util::str_to_file (const std::string &filename, const std::string rep) |
| Write the given text representation (any string) to a file. More... | |
| std::vector< uint8_t > | fs::util::viridis (const std::vector< float > &data, float vmin=NAN, float vmax=NAN, uint8_t nan_r=255, uint8_t nan_g=255, uint8_t nan_b=255) |
| Map per-vertex numeric data to RGB colors using the Viridis perceptually-uniform colormap. More... | |
| void | fs::read_nifti (Mgh *mgh, std::istream *is, bool force_standard) |
| Read a NIfTI-1 file into an Mgh struct (stream overload). More... | |
| void | fs::read_nifti (Mgh *mgh, const std::string &filename, bool force_standard) |
| Read a NIfTI-1 file into an Mgh struct (filename overload). More... | |
| void | fs::read_mgh_header (MghHeader *mgh_header, const std::string &filename) |
| Read the header of a FreeSurfer volume file in MGH format into the given MghHeader struct. More... | |
| void | fs::read_mgh_header (MghHeader *mgh_header, std::istream *is) |
| Read an MGH header from a stream. More... | |
| void | fs::read_mgh (Mgh *mgh, const std::string &filename) |
| Read a FreeSurfer volume file in MGH format into the given Mgh struct. More... | |
| std::vector< std::string > | fs::read_subjectsfile (const std::string &filename) |
| Read a vector of subject identifiers from a FreeSurfer subjects file. More... | |
| void | fs::write_subjectsfile (const std::string &filename, const std::vector< std::string > &subjects) |
| Write a vector of subject identifiers to a FreeSurfer subjects file. More... | |
| void | fs::read_mgh (Mgh *mgh, std::istream *is) |
| Read MGH data from a stream. More... | |
| void | fs::read_surf (Mesh *surface, const std::string &filename) |
| Read a brain mesh from a file in binary FreeSurfer 'surf' format into the given Mesh instance. More... | |
| void | fs::read_mesh (Mesh *surface, const std::string &filename) |
| Read a triangular mesh from a surf, obj, or ply file into the given Mesh instance. More... | |
| void | fs::read_curv (Curv *curv, std::istream *is, const std::string &source_filename="") |
| Read per-vertex brain morphometry data from a FreeSurfer curv stream. More... | |
| void | fs::read_curv (Curv *curv, const std::string &filename) |
| Read Curv instance from a FreeSurfer curv format file. More... | |
| void | fs::read_annot (Annot *annot, std::istream *is) |
| Read a FreeSurfer annotation or brain surface parcellation from an annot stream. More... | |
| void | fs::read_annot (Annot *annot, const std::string &filename) |
| Read a FreeSurfer annotation from a file. More... | |
| std::vector< float > | fs::read_curv_data (const std::string &filename) |
| Read per-vertex brain morphometry data from a FreeSurfer curv format file. More... | |
| std::vector< float > | fs::read_desc_data (const std::string &filename) |
| Read per-vertex brain morphometry data from a FreeSurfer curv, MGH, or NIfTI format file. More... | |
| void | fs::write_annot (const Annot &annot, std::ostream &os) |
| Write a FreeSurfer annotation (brain surface parcellation) to a stream. More... | |
| void | fs::write_annot (const Annot &annot, const std::string &filename) |
| Write a FreeSurfer annotation (brain surface parcellation) to a file. More... | |
| void | fs::write_curv (std::ostream &os, std::vector< float > curv_data, int32_t num_faces=100000) |
| Write curv data to a stream. More... | |
| void | fs::write_curv (const std::string &filename, std::vector< float > curv_data, const int32_t num_faces=100000) |
| Write curv data to a file. More... | |
| void | fs::write_mgh (const Mgh &mgh, std::ostream &os) |
| Write MGH data to a stream. More... | |
| void | fs::write_mgh (const Mgh &mgh, const std::string &filename) |
| Write MGH data to a file. More... | |
| void | fs::write_nifti (const Mgh &mgh, std::ostream &os) |
| Write MGH data to a NIfTI-1 file (stream overload). More... | |
| void | fs::write_nifti (const Mgh &mgh, const std::string &filename) |
| Write MGH data to a NIfTI-1 file (filename overload). More... | |
| Mgh | fs::nifti_to_mgh (const std::string &filename) |
| Convert a NIfTI-1 file directly to MGH by reading it. More... | |
| void | fs::write_surf (std::vector< float > vertices, std::vector< int32_t > faces, std::ostream &os) |
| Write a mesh to a stream in FreeSurfer surf format. More... | |
| void | fs::write_surf (std::vector< float > vertices, std::vector< int32_t > faces, const std::string &filename) |
| Write a mesh to a binary file in FreeSurfer surf format. More... | |
| void | fs::write_surf (const Mesh &mesh, const std::string &filename) |
| Write a mesh to a binary file in FreeSurfer surf format. More... | |
| void | fs::read_label (Label *label, std::istream *is) |
| Read a FreeSurfer ASCII label from a stream. More... | |
| void | fs::read_label (Label *label, const std::string &filename) |
| Read a FreeSurfer ASCII label from a file. More... | |
| void | fs::write_label (const Label &label, std::ostream &os) |
| Write label data to a stream. More... | |
| void | fs::write_label (const Label &label, const std::string &filename) |
| Write label data to a file. More... | |
| void | fs::write_mesh (const Mesh &mesh, const std::string &filename) |
| Write a mesh to a file in different formats. More... | |
| void | fs::write_mesh (const Mesh &mesh, const std::string &filename, const std::vector< uint8_t > col) |
| Write a mesh to a file in different formats, with vertex colors. More... | |
Variables | |
| const std::string | fs::util::LOGTAG_CRITICAL = "CRITICAL" |
| Logging threshold for critical messages. | |
| const std::string | fs::util::LOGTAG_ERROR = "ERROR" |
| Logging threshold for error messages. | |
| const std::string | fs::util::LOGTAG_WARNING = "WARNING" |
| Logging threshold for warning messages. | |
| const std::string | fs::util::LOGTAG_INFO = "INFO" |
| Logging threshold for warning messages. | |
| const std::string | fs::util::LOGTAG_VERBOSE = "VERBOSE" |
| Logging threshold for warning messages. | |
| const std::string | fs::util::LOGTAG_EXCESSIVE = "EXCESSIVE" |
| Logging threshold for warning messages. | |
| const int | fs::MRI_UCHAR = 0 |
| MRI data type representing an 8 bit unsigned integer. | |
| const int | fs::MRI_INT = 1 |
| MRI data type representing a 32 bit signed integer. | |
| const int | fs::MRI_FLOAT = 3 |
| MRI data type representing a 32 bit float. | |
| const int | fs::MRI_SHORT = 4 |
| MRI data type representing a 16 bit signed integer. | |
NIfTI-1 Data Type Constants | |
Pixel data type codes defined by the NIfTI-1 specification. These constants correspond to the | |
| const int16_t | fs::NIFTI_DT_NONE = 0 |
| No data / unknown type (value 0). | |
| const int16_t | fs::NIFTI_DT_BINARY = 1 |
| const int16_t | fs::NIFTI_DT_UINT8 = 2 |
| const int16_t | fs::NIFTI_DT_INT16 = 4 |
| const int16_t | fs::NIFTI_DT_INT32 = 8 |
| const int16_t | fs::NIFTI_DT_FLOAT32 = 16 |
| const int16_t | fs::NIFTI_DT_COMPLEX64 = 32 |
| const int16_t | fs::NIFTI_DT_FLOAT64 = 64 |
| const int16_t | fs::NIFTI_DT_RGB24 = 128 |
| const int16_t | fs::NIFTI_DT_INT8 = 256 |
| const int16_t | fs::NIFTI_DT_UINT16 = 512 |
| const int16_t | fs::NIFTI_DT_UINT32 = 768 |
| const int16_t | fs::NIFTI_DT_INT64 = 1024 |
| const int16_t | fs::NIFTI_DT_UINT64 = 1280 |
| const int16_t | fs::NIFTI_DT_FLOAT128 = 1536 |
| const int16_t | fs::NIFTI_DT_COMPLEX128 = 1792 |
| const int16_t | fs::NIFTI_DT_COMPLEX256 = 2048 |
| #define LIBFS_APPTAG "[libfs] " |
Application tag prepended to every debug message from libfs.
Each line of output produced by the debug macros is prefixed with this string so users can identify which component emitted the message. Define LIBFS_APPTAG before including libfs.h to supply your own tag, e.g.:
If left undefined, the default "[libfs] " is used.
| #define LIBFS_DBG_CRITICAL |
Debug level: emit only critical errors.
When defined, only the most severe messages are printed. Critical errors typically precede an exception that will terminate the application unless caught. Use this level in production to keep output minimal while still reporting fatal conditions.
| #define LIBFS_DBG_ERROR |
Debug level: emit errors and everything more severe.
When defined (directly by the user or through the cascading #ifdef chain), messages tagged as errors, as well as critical errors, are printed. Error-level messages indicate a problem that prevents the current operation from completing successfully (e.g., a file could not be parsed), but the application may still continue.
| #define LIBFS_DBG_WARNING |
Default debug level: emit warnings and everything more severe.
This is the built-in fallback when no LIBFS_DBG_* macro is defined by the user. Enabling LIBFS_DBG_WARNING also automatically activates LIBFS_DBG_ERROR and LIBFS_DBG_CRITICAL (see the cascading #ifdef chain below). Typical output includes recoverable problems such as unknown file metadata keys or deprecated API usage.
| #define LIBFS_MAX_ALLOC_BYTES_DEFAULT (2ULL * 1024ULL * 1024ULL * 1024ULL) |
Default maximum memory allocation limit in bytes (2 GiB). A header claiming to need more memory than this will be rejected.
| #define LIBFS_VERSION "0.5.0" |
Full libfs version string, in semver format.
A "MAJOR.MINOR.PATCH" string, e.g. "0.4.2". Use this when displaying the library version to users or writing it into output file headers. For programmatic version checks, prefer the individual LIBFS_VERSION_MAJOR, LIBFS_VERSION_MINOR, and LIBFS_VERSION_PATCH integer macros.
| #define LIBFS_VERSION_MAJOR 0 |
Major version number (incompatible API changes).
Incremented when the public API changes in a way that breaks source or binary compatibility. For example, bumping from 0 to 1 signals a stable first release. Combine with LIBFS_VERSION_MINOR and LIBFS_VERSION_PATCH to construct a complete semver identifier.
| #define LIBFS_VERSION_MINOR 5 |
Minor version number (backward-compatible feature additions).
Incremented when new functionality is added without breaking existing API contracts. Together with LIBFS_VERSION_MAJOR this forms the MAJOR.MINOR prefix used in release tags.
| #define LIBFS_VERSION_PATCH 0 |
Patch version number (backward-compatible bug fixes).
Incremented for bug-fix releases that do not add new features or change the public API. The full version string is LIBFS_VERSION_MAJOR.LIBFS_VERSION_MINOR.LIBFS_VERSION_PATCH.
|
inline |
Check whether a file exists (can be read) at given path.
You should not rely on this as a pre-check when considering to open a file due to race conditions, just try-catch open in that case. This is intended to check whether a certain software run succeeded, by checking whether the key expected output files exist.
| name | the filename that should be checked. Examples |
| std::string fs::util::fullpath | ( | std::initializer_list< std::string > | path_components, |
| std::string | path_sep = std::string("/") |
||
| ) |
Construct a UNIX file system path from the given path_components.
Any trailing or leading slash (path_sep) will be stripped from the individual components and replaced with a single one between two components. If the first path component started with a slash, that slash will be kept (absolute paths are left intact).
| path_components | init list of strings, the path components |
| path_sep | path separator to use, typically / on Unix-based system. |
| std::invalid_argument | on empty |
path_sep.
|
inline |
Log a message, goes to stdout.
| message | the message to be logged. |
| loglevel | the log level, one of fs::util::LOGTAG_*. |
|
inline |
Convert a NIfTI-1 file directly to MGH by reading it.
This is just a convenience wrapper: read_nifti already populates an Mgh struct, so this provides a more explicit name for the "convert" use case.
| filename | Path to the input .nii or .nii.gz file. |
| std::runtime_error | on read errors. |
| void fs::read_annot | ( | Annot * | annot, |
| std::istream * | is | ||
| ) |
Read a FreeSurfer annotation or brain surface parcellation from an annot stream.
A brain parcellations contains a region table and assigns to each vertex of a surface a region.
| annot | An Annot instance to be filled. |
| is | An open istream from which to read the annot data. |
| domain_error | if the file format version is not supported or the file is missing the color table. |
| void fs::read_annot | ( | Annot * | annot, |
| const std::string & | filename | ||
| ) |
Read a FreeSurfer annotation from a file.
| annot | An Annot instance that should be filled. |
| filename | Path to the label file that should be read. |
| runtime_error | if the file cannot be opened, domain_error if the file format version is not supported or the file is missing the color table. |
| void fs::read_curv | ( | Curv * | curv, |
| std::istream * | is, | ||
| const std::string & | source_filename = "" |
||
| ) |
Read per-vertex brain morphometry data from a FreeSurfer curv stream.
The curv format is a simple binary format that stores one floating point value per vertex of a related brain surface.
| curv | A Curv instance to be filled. |
| is | An open istream from which to read the curv data. |
| source_filename | Optional human-readable name for the data source (e.g., "lh.thickness"). When supplied, it is included in exception messages to help identify which file caused the error. Leave empty (the default) to omit the filename from diagnostics. |
| domain_error | if the curv file magic mismatches or the curv file header claims that the file contains more than 1 value per vertex. |
| void fs::read_curv | ( | Curv * | curv, |
| const std::string & | filename | ||
| ) |
Read Curv instance from a FreeSurfer curv format file.
The curv format is a simple binary format that stores one floating point value per vertex of a related brain surface.
| curv | A Curv instance to be filled. |
| filename | Path to a file from which to read the curv data. |
| runtime_error | if the file cannot be opened, domain_error if the curv file magic mismatches or the curv file header claims that the file contains more than 1 value per vertex. |
| std::vector<float> fs::read_curv_data | ( | const std::string & | filename | ) |
Read per-vertex brain morphometry data from a FreeSurfer curv format file.
The curv format is a simple binary format that stores one floating point value per vertex of a related brain surface.
| filename | Path to a file from which to read the curv data. |
| runtime_error | if the file cannot be opened, domain_error if the curv file magic mismatches or the curv file header claims that the file contains more than 1 value per vertex. |
|
inline |
Read per-vertex brain morphometry data from a FreeSurfer curv, MGH, or NIfTI format file.
| filename | Path to a file from which to read the data. If the name ends with '.mgh' / '.MGH' it is read as MGH; if it ends with '.nii' / '.nii.gz' it is read as NIfTI; otherwise it is assumed to be a curv file. The data must be of type MRI_FLOAT, and only one spatial dimension may have size > 1. |
| runtime_error | if the file cannot be opened, domain_error if the curv file magic mismatches or the curv file header claims that the file contains more than 1 value per vertex. |
| void fs::read_label | ( | Label * | label, |
| std::istream * | is | ||
| ) |
Read a FreeSurfer ASCII label from a stream.
A label is a list of vertices (for a surface label, given by index) or voxels (for a volume label, given by the xyz coordinates) and one floating point value per vertex/voxel. Sometimes a label is only used to define a set of vertices/voxels (like a certain brain region), and the values are irrelevant (and typically left at 0.0).
| label | A Label instance that should be filled. |
| is | An open std::istream or derived class stream from which to read the data, e.g., std::ifstream or std::istringstream. |
| std::domain_error | if the label data format is incorrect |
| void fs::read_label | ( | Label * | label, |
| const std::string & | filename | ||
| ) |
Read a FreeSurfer ASCII label from a file.
A label is a list of vertices (for a surface label, given by index) or voxels (for a volume label, given by the xyz coordinates) and one floating point value per vertex/voxel. Sometimes a label is only used to define a set of vertices/voxels (like a certain brain region), and the values are irrelevant (and typically left at 0.0).
| label | A Label instance that should be filled. |
| filename | Path to the label file that should be read. |
| std::domain_error | if the label data format is incorrect, std::runtime_error if the file cannot be opened. |
| void fs::read_mesh | ( | Mesh * | surface, |
| const std::string & | filename | ||
| ) |
Read a triangular mesh from a surf, obj, or ply file into the given Mesh instance.
| surface | a Mesh instance representing a vertex-indexed tri-mesh. This will be filled. |
| filename | The path to the file from which to read the mesh. The format will be determined from the file extension as follows. File names ending with '.obj' are loaded as Wavefront OBJ files. File names ending with '.ply' are loaded as Stanford PLY files in format version 'ascii 1.0'. All other files are loaded as FreeSurfer binary surf files. |
| runtime_error | if the file cannot be opened, domain_error if the surf file magic mismatches. |
| void fs::read_mgh | ( | Mgh * | mgh, |
| const std::string & | filename | ||
| ) |
Read a FreeSurfer volume file in MGH format into the given Mgh struct.
| mgh | An Mgh instance that should be filled with the data from the filename. |
| filename | Path to the input MGH file. |
| runtime_error | if the file uses an unsupported MRI data type. |
| void fs::read_mgh | ( | Mgh * | mgh, |
| std::istream * | is | ||
| ) |
Read MGH data from a stream.
| mgh | An Mgh instance that should be filled with the data from the stream. |
| is | Pointer to an open istream from which to read the MGH data. |
| runtime_error | if the file uses an unsupported MRI data type. |
| void fs::read_mgh_header | ( | MghHeader * | mgh_header, |
| const std::string & | filename | ||
| ) |
Read the header of a FreeSurfer volume file in MGH format into the given MghHeader struct.
| mgh_header | An MghHeader instance that should be filled with the data from the file. |
| filename | Path to the file from which to read the MGH data. |
| runtime_error | if the file cannot be opened |
| void fs::read_mgh_header | ( | MghHeader * | mgh_header, |
| std::istream * | is | ||
| ) |
Read an MGH header from a stream.
| mgh_header | An MghHeader instance that should be filled with the data from the stream. |
| is | Pointer to an open istream from which to read the MGH data. |
| runtime_error | if the file uses an unsupported MRI file format version. Only version 1 is supported (the only existing version to my knowledge). |
|
inline |
Read a NIfTI-1 file into an Mgh struct (stream overload).
| mgh | The Mgh instance to fill with the NIfTI data. |
| is | An open input stream positioned at the start of the NIfTI file. |
| force_standard | If true, reject non-conformant headers including the FreeSurfer hack. |
| std::runtime_error | on unsupported data types, I/O errors, or dimension overflows. |
|
inline |
Read a NIfTI-1 file into an Mgh struct (filename overload).
Auto-detects .nii and .nii.gz by file extension.
| mgh | The Mgh instance to fill with the NIfTI data. |
| filename | Path to the input file (.nii or .nii.gz). |
| force_standard | If true, reject non-conformant headers including the FreeSurfer hack. |
| std::runtime_error | on unsupported data types, I/O errors, or dimension overflows. |
| std::vector<std::string> fs::read_subjectsfile | ( | const std::string & | filename | ) |
Read a vector of subject identifiers from a FreeSurfer subjects file.
| filename | a text file that contains one subject identifier per line. |
| runtime_error | if the file cannot be read |
| void fs::read_surf | ( | Mesh * | surface, |
| const std::string & | filename | ||
| ) |
Read a brain mesh from a file in binary FreeSurfer 'surf' format into the given Mesh instance.
| surface | a Mesh instance representing a vertex-indexed tri-mesh. This will be filled. |
| filename | The path to the file from which to read the mesh. Must be in binary FreeSurfer surf format. An example file is surf/lh.white. |
| runtime_error | if the file cannot be opened, domain_error if the surf file magic mismatches. |
|
inline |
Check whether a string starts with one of the given prefixes.
| value | the string for which to check whether it starts with any of the prefixes |
| prefixes | the prefixes to consider |
| void fs::util::str_to_file | ( | const std::string & | filename, |
| const std::string | rep | ||
| ) |
Write the given text representation (any string) to a file.
| filename | the file to which to write, will be overwritten if exists |
| rep | the string to write to the file |
| std::runtime_error | if the file cannot be opened. |
| std::string fs::util::time_tag | ( | std::chrono::system_clock::time_point | t | ) |
Get current time as string, e.g. for log messages.
| t | the timepoint to format as a string, typically std::system_clock::now(). |
| std::vector<T> fs::util::vflatten | ( | std::vector< std::vector< T >> | values | ) |
Flatten 2D vector.
| values | the input 2D vector. |
| std::vector<uint8_t> fs::util::viridis | ( | const std::vector< float > & | data, |
| float | vmin = NAN, |
||
| float | vmax = NAN, |
||
| uint8_t | nan_r = 255, |
||
| uint8_t | nan_g = 255, |
||
| uint8_t | nan_b = 255 |
||
| ) |
Map per-vertex numeric data to RGB colors using the Viridis perceptually-uniform colormap.
This converts the output of fs::read_curv_data (or any per-vertex float vector) into the RGB color vector expected by fs::Mesh::to_ply() / fs::Mesh::to_ply_file() (and the OFF variants). The returned vector contains 3 consecutive uint8_t values per vertex (red, green, blue), in the exact interleave order used by the mesh exporters (v0_red, v0_green, v0_blue, v1_red, ...). The input data is normalized to the [0, 1] range before sampling the colormap. By default the normalization range is computed from the finite (non-NaN) min and max of the input, and you can override either bound by passing a finite value for vmin and/or vmax (pass NAN for the bound(s) you want to be derived from the data). NaN input values are mapped to a configurable color, which defaults to white (255, 255, 255) following the standard convention in neuroimaging where missing data (e.g. the medial wall) is shown in white. Use the nan_r / nan_g / nan_b parameters to change this color. The Viridis colormap is implemented from its official 256-sample lookup table (the same data used by matplotlib), with linear interpolation between samples, so there are no external dependencies.
| data | the per-vertex scalar values to convert (one value per vertex). |
| vmin | the value mapped to the bottom of the colormap. Pass NAN (the default) to auto-compute it as the finite minimum of data. |
| vmax | the value mapped to the top of the colormap. Pass NAN (the default) to auto-compute it as the finite maximum of data. |
| nan_r | the red component (0..255) of the color used for NaN input values. Defaults to 255 (white). |
| nan_g | the green component (0..255) of the color used for NaN input values. Defaults to 255 (white). |
| nan_b | the blue component (0..255) of the color used for NaN input values. Defaults to 255 (white). |
| std::invalid_argument | if vmin and vmax are both given but vmin is greater than vmax. |
| void fs::write_annot | ( | const Annot & | annot, |
| std::ostream & | os | ||
| ) |
Write a FreeSurfer annotation (brain surface parcellation) to a stream.
| annot | The Annot instance to write. |
| os | An open output stream (binary mode). |
| std::domain_error | if the annot or colortable data is inconsistent. |
| void fs::write_annot | ( | const Annot & | annot, |
| const std::string & | filename | ||
| ) |
Write a FreeSurfer annotation (brain surface parcellation) to a file.
| annot | The Annot instance to write. |
| filename | Path to the output file. |
| std::runtime_error | if the file cannot be opened. |
| void fs::write_curv | ( | std::ostream & | os, |
| std::vector< float > | curv_data, | ||
| int32_t | num_faces = 100000 |
||
| ) |
Write curv data to a stream.
A curv file contains one floating point value per vertex (or a related mesh).
| os | An output stream to which to write the data. The stream must be open, and this function will not close it after writing to it. |
| curv_data | the data to write. |
| num_faces | the value for the header field num_faces. This is not needed afaik and typically ignored. |
| void fs::write_curv | ( | const std::string & | filename, |
| std::vector< float > | curv_data, | ||
| const int32_t | num_faces = 100000 |
||
| ) |
Write curv data to a file.
A curv file contains one floating point value per vertex (or a related mesh).
| filename | The path to the output file. |
| curv_data | the data to write. |
| num_faces | the value for the header field num_faces. This is not needed afaik and typically ignored. |
| std::runtime_error | if the file cannot be opened. |
| void fs::write_label | ( | const Label & | label, |
| std::ostream & | os | ||
| ) |
Write label data to a stream.
| label | The label to write. |
| os | An open output stream. |
| void fs::write_label | ( | const Label & | label, |
| const std::string & | filename | ||
| ) |
Write label data to a file.
| label | an fs::Label instance |
| filename | Path to the label file that should be written. |
| std::runtime_error | if the file cannot be opened. |
| void fs::write_mesh | ( | const Mesh & | mesh, |
| const std::string & | filename | ||
| ) |
Write a mesh to a file in different formats.
The output format will be auto-determined from the file extension.
| mesh | The fs::Mesh instance to write. |
| filename | The path to the output file. |
| std::runtime_error | if the file cannot be opened. |
| void fs::write_mesh | ( | const Mesh & | mesh, |
| const std::string & | filename, | ||
| const std::vector< uint8_t > | col | ||
| ) |
Write a mesh to a file in different formats, with vertex colors.
The output format will be auto-determined from the file extension. The colors are written for PLY, OFF, and OBJ formats; the surf format ignores them.
| mesh | The fs::Mesh instance to write. |
| filename | The path to the output file. |
| col | u_char vector of RGB color values, 3 per vertex. |
| std::runtime_error | if the file cannot be opened. |
| void fs::write_mgh | ( | const Mgh & | mgh, |
| std::ostream & | os | ||
| ) |
Write MGH data to a stream.
The MGH format is a binary, big-endian FreeSurfer file format for storing 4D data. Several data types are supported, and one has to check the header to see which one is contained in a file.
| mgh | An Mgh instance that should be written. |
| os | An output stream to which to write the data. The stream must be open, and this function will not close it after writing to it. |
| std::logic_error | if the mgh header and data are inconsistent, std::domain_error if the given MRI data type is unknown or unsupported. |
| void fs::write_mgh | ( | const Mgh & | mgh, |
| const std::string & | filename | ||
| ) |
Write MGH data to a file.
The MGH format is a binary, big-endian FreeSurfer file format for storing 4D data. Several data types are supported, and one has to check the header to see which one is contained in a file.
| mgh | An Mgh instance that should be written. |
| filename | Path to an output file to which to write. |
| std::runtime_error | if the file cannot be opened, std::logic_error if the mgh header and data are inconsistent, std::domain_error if the given MRI data type is unknown or unsupported. |
|
inline |
Write MGH data to a NIfTI-1 file (stream overload).
Writes a standard volumetric NIfTI-1 file in big-endian byte order (NIfTI-1 standard). The FreeSurfer hack is never produced.
| mgh | The Mgh data to write. |
| os | An open output stream. |
| std::runtime_error | if any dimension exceeds 32767 or the data type is unsupported. |
|
inline |
Write MGH data to a NIfTI-1 file (filename overload).
| mgh | The Mgh data to write. |
| filename | Path to the output file (.nii or .nii.gz). |
| std::runtime_error | if the file cannot be opened. |
| void fs::write_subjectsfile | ( | const std::string & | filename, |
| const std::vector< std::string > & | subjects | ||
| ) |
Write a vector of subject identifiers to a FreeSurfer subjects file.
| filename | Path to the output file (one subject ID per line). |
| subjects | The subject identifiers to write. |
| std::runtime_error | if the file cannot be opened. |
| void fs::write_surf | ( | std::vector< float > | vertices, |
| std::vector< int32_t > | faces, | ||
| std::ostream & | os | ||
| ) |
Write a mesh to a stream in FreeSurfer surf format.
A surf file contains a vertex index representation of a mesh, i.e., the vertices and faces vectors.
| vertices | vector of float, length 3n for n vertices. The 3D coordinates of the vertices, typically from <Mesh_instance>.vertices. |
| faces | vector of int, length 3n for n faces. The 3 vertex indices for each face, typically from <Mesh_instance>.faces. |
| os | An output stream to which to write the data. The stream must be open, and this function will not close it after writing to it. |
| std::runtime_error | if the file cannot be opened. |
| void fs::write_surf | ( | std::vector< float > | vertices, |
| std::vector< int32_t > | faces, | ||
| const std::string & | filename | ||
| ) |
Write a mesh to a binary file in FreeSurfer surf format.
A surf file contains a vertex index representation of a mesh, i.e., the vertices and faces vectors.
| vertices | vector of float, length 3n for n vertices. The 3D coordinates of the vertices, typically from <Mesh_instance>.vertices. |
| faces | vector of int, length 3n for n faces. The 3 vertex indices for each face, typically from <Mesh_instance>.faces. |
| filename | The path to the output file. |
| std::runtime_error | if the file cannot be opened. |
| void fs::write_surf | ( | const Mesh & | mesh, |
| const std::string & | filename | ||
| ) |
Write a mesh to a binary file in FreeSurfer surf format.
A surf file contains a vertex index representation of a mesh, i.e., the vertices and faces vectors.
| mesh | The Mesh instance to write. |
| filename | The path to the output file. |
| std::runtime_error | if the file cannot be opened. |
| const int16_t fs::NIFTI_DT_BINARY = 1 |
Binary mask: each voxel is a single bit packed into a byte (value 1). Useful for segmentation labels that are strictly 0 or 1.
| const int16_t fs::NIFTI_DT_COMPLEX128 = 1792 |
Complex number stored as two consecutive 64-bit floats (real, imaginary), 128 bits total (value 1792).
| const int16_t fs::NIFTI_DT_COMPLEX256 = 2048 |
Complex number stored as two consecutive 128-bit floats (real, imaginary), 256 bits total (value 2048). Highest precision complex storage; virtually never encountered in real-world datasets.
| const int16_t fs::NIFTI_DT_COMPLEX64 = 32 |
Complex number stored as two consecutive 32-bit floats (real, imaginary), 64 bits total (value 32). Rarely used in practice; mainly for frequency-domain / phase data.
| const int16_t fs::NIFTI_DT_FLOAT128 = 1536 |
128-bit IEEE-754 quadruple-precision float (value 1536). Rarely supported in practice — most toolchains read it but fall back to double precision.
| const int16_t fs::NIFTI_DT_FLOAT32 = 16 |
32-bit IEEE-754 single-precision float (value 16). The most common floating-point format for processed neuroimaging data (e.g., z-statistics, correlation maps).
| const int16_t fs::NIFTI_DT_FLOAT64 = 64 |
64-bit IEEE-754 double-precision float (value 64). Preferred when numerical precision is critical, e.g., for deformation fields or when accumulating statistics.
| const int16_t fs::NIFTI_DT_INT16 = 4 |
Signed 16-bit integer, range [-32768, 32767] (value 4). Widely used for structural MRI after scaling with scl_slope/ scl_inter.
| const int16_t fs::NIFTI_DT_INT32 = 8 |
Signed 32-bit integer, range [-2^31, 2^31-1] (value 8). Used for high-dynamic-range label maps and processed statistical images.
| const int16_t fs::NIFTI_DT_INT64 = 1024 |
Signed 64-bit integer, range [-2^63, 2^63-1] (value 1024). Suitable for high-precision integer processing and very large label spaces.
| const int16_t fs::NIFTI_DT_INT8 = 256 |
Signed 8-bit integer, range [-128, 127] (value 256). Provides a compact representation when the dynamic range fits in a single signed byte.
| const int16_t fs::NIFTI_DT_RGB24 = 128 |
RGB triple: three consecutive uint8 values per voxel (R, G, B), 24 bits total (value 128). Used for 2-D colour images stored in NIfTI format (e.g., tissue-class overlays).
| const int16_t fs::NIFTI_DT_UINT16 = 512 |
Unsigned 16-bit integer, range [0, 65535] (value 512). Common in DICOM-derived data and some microscopy formats.
| const int16_t fs::NIFTI_DT_UINT32 = 768 |
Unsigned 32-bit integer, range [0, 2^32-1] (value 768). Useful for large label sets and extended-count voxel data.
| const int16_t fs::NIFTI_DT_UINT64 = 1280 |
Unsigned 64-bit integer, range [0, 2^64-1] (value 1280). The widest integral type available in the NIfTI-1 spec.
| const int16_t fs::NIFTI_DT_UINT8 = 2 |
Unsigned 8-bit integer, range [0, 255] (value 2). Common for RGB component planes, label masks, and CT data.
1.8.13