Reading NRRD files
|
Read a NRRD file and return the header and data |
|
Read contents of header and parse values from |
|
Read data from file into |
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
- filename
str
Filename of the NRRD file
- custom_field_map
dict
(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)).
- filename
- Returns
- data
numpy.ndarray
Data read from NRRD file
- header
dict
(str
,Object
) Dictionary containing the header fields and their corresponding parsed value
- data
See also
- 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
andfilename
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 thefilename
parameter is required to obtain the absolute path to the data file.See Reading NRRD files for more information on reading NRRD files.
- Parameters
- header
dict
(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.
- filename
str
, 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)).
- header
- Returns
- data
numpy.ndarray
Data read from NRRD file
- data
See also
- 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, thefile
parameter can be any sort of iterator that returns a string each timenext()
is called. The two common objects that meet these requirements are file objects and a list of strings. Whenfile
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
- Returns
See also
- 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.