Reading NRRD files

nrrd.read(filename[, custom_field_map, ...])

Read a NRRD file and return the header and data

nrrd.read_header(file[, custom_field_map])

Read contents of header and parse values from file

nrrd.read_data(header[, fh, filename, ...])

Read data from file into numpy.ndarray

nrrd.reader.ALLOW_DUPLICATE_FIELD

Allow duplicate header fields when reading NRRD files

nrrd.read(filename: str, custom_field_map: Optional[Dict[str, Literal['int', 'double', 'string', 'int list', 'double list', 'string list', 'quoted string list', 'int vector', 'double vector', 'int matrix', 'double matrix']]] = None, index_order: Literal['F', 'C'] = 'F') Tuple[NDArray[Any, Any], Dict[str, Any]][source]

Read a NRRD file and return the header and data

See Reading NRRD files for more information on reading NRRD files.

Note

Users should be aware that the index_order argument needs to be consistent between nrrd.read and nrrd.write. I.e., reading an array with index_order=’F’ will result in a transposed version of the original data and hence the writer needs to be aware of this.

Parameters
filenamestr

Filename of the NRRD file

custom_field_mapdict (str, str), optional

Dictionary used for parsing custom field types where the key is the custom field name and the value is a string identifying datatype for the custom field.

index_order{‘C’, ‘F’}, optional

Specifies the index order of the resulting data array. Either ‘C’ (C-order) where the dimensions are ordered from slowest-varying to fastest-varying (e.g. (z, y, x)), or ‘F’ (Fortran-order) where the dimensions are ordered from fastest-varying to slowest-varying (e.g. (x, y, z)).

Returns
datanumpy.ndarray

Data read from NRRD file

headerdict (str, Object)

Dictionary containing the header fields and their corresponding parsed value

nrrd.read_data(header: Dict[str, Any], fh: Optional[IO] = None, filename: Optional[str] = None, index_order: Literal['F', 'C'] = 'F') NDArray[Any, Any][source]

Read data from file into numpy.ndarray

The two parameters fh and filename are optional depending on the parameters but it never hurts to specify both. The file handle (fh) is necessary if the header is attached with the NRRD data. However, if the NRRD data is detached from the header, then the filename parameter is required to obtain the absolute path to the data file.

See Reading NRRD files for more information on reading NRRD files.

Parameters
headerdict (str, Object)

Parsed fields/values obtained from read_header() function

fhfile-object, optional

File object pointing to first byte of data. Only necessary if data is attached to header.

filenamestr, optional

Filename of the header file. Only necessary if data is detached from the header. This is used to get the absolute data path.

index_order{‘C’, ‘F’}, optional

Specifies the index order of the resulting data array. Either ‘C’ (C-order) where the dimensions are ordered from slowest-varying to fastest-varying (e.g. (z, y, x)), or ‘F’ (Fortran-order) where the dimensions are ordered from fastest-varying to slowest-varying (e.g. (x, y, z)).

Returns
datanumpy.ndarray

Data read from NRRD file

See also

read(), read_header()
nrrd.read_header(file: Union[str, Iterable], custom_field_map: Optional[Dict[str, Literal['int', 'double', 'string', 'int list', 'double list', 'string list', 'quoted string list', 'int vector', 'double vector', 'int matrix', 'double matrix']]] = None) Dict[str, Any][source]

Read contents of header and parse values from file

file can be a filename indicating where the NRRD header is located or a string iterator object. If a filename is specified, then the file will be opened and closed after the header is read from it. If not specifying a filename, the file parameter can be any sort of iterator that returns a string each time next() is called. The two common objects that meet these requirements are file objects and a list of strings. When file is a file object, it must be opened with the binary flag (‘b’) on platforms where that makes a difference, such as Windows.

See Reading NRRD files for more information on reading NRRD files.

Parameters
filestr or string iterator

Filename, file object or string iterator object to read NRRD header from

custom_field_mapdict (str, str), optional

Dictionary used for parsing custom field types where the key is the custom field name and the value is a string identifying datatype for the custom field.

Returns
headerdict (str, Object)

Dictionary containing the header fields and their corresponding parsed value

See also

read(), read_data()
nrrd.reader.ALLOW_DUPLICATE_FIELD = False

Allow duplicate header fields when reading NRRD files

When there are duplicated fields in a NRRD file header, pynrrd throws an error by default. Setting this field as True will instead show a warning.

Example:

Reading a NRRD file with duplicated header field ‘space’ with field set to False.

>>> filedata, fileheader = nrrd.read('filename_duplicatedheader.nrrd')
nrrd.errors.NRRDError: Duplicate header field: 'space'

Set the field as True to receive a warning instead.

>>> nrrd.reader.ALLOW_DUPLICATE_FIELD = True
>>> filedata, fileheader = nrrd.read('filename_duplicatedheader.nrrd')
UserWarning: Duplicate header field: 'space' warnings.warn(dup_message)
Note:

Duplicated fields are prohibited by the NRRD file specification.