Writing NRRD files
|
Write |
- nrrd.write(file: Union[str, IO], data: NDArray[Any, Any], header: Optional[Dict[str, Any]] = None, detached_header: bool = False, relative_data_path: bool = True, 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, compression_level: int = 9, index_order: Literal['F', 'C'] = 'F')[source]
Write
numpy.ndarray
to NRRD fileThe
file
parameter specifies the absolute or relative filename to write the NRRD file to or an io.BytesIO object.If
file
is a filename and the extension is .nhdr, then thedetached_header
parameter is set to true automatically. If thedetached_header
parameter is set toTrue
and thefilename
ends in .nrrd, then the header file will have the same path and base name as thefile
but with an extension of .nhdr. In all other cases, the header and data are saved in the same file.header
is an optional parameter containing the fields and values to be added to the NRRD header.Note
detached_header
is ignored iffile
is io.BytesIO and not a str filenameNote
The following fields are automatically generated based on the
data
parameter ignoring these values in theheader
: ‘type’, ‘endian’, ‘dimension’, ‘sizes’, and ‘data file’. In addition, the generated fields will be added to the givenheader
. Thus, one can check the generated fields by viewing the passedheader
.Note
The default encoding field used if not specified in
header
is ‘gzip’.Note
The
index_order
parameter must be consistent with the index order specified inread()
. Reading an NRRD file in C-order and then writing as Fortran-order or vice versa will result in the data being transposed in the NRRD file.See Writing NRRD files for more information on writing NRRD files.
- Parameters
- file
str
orio.IOBase
Filename or file object of the NRRD file
- data
numpy.ndarray
Data to save to the NRRD file
- header
dict
(str
,Object
), optional NRRD headers
- detached_header
bool
orstr
, optional Whether the header and data should be saved in separate files. Defaults to
False
. If astr
is given this specifies the path to the datafile. This path will ONLY be used if the given filename ends with nhdr (i.e. the file is a header)- relative_data_path
bool
Whether the data filename in detached header is saved with a relative path or absolute path. This parameter is ignored if there is no detached header. Defaults to
True
- 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.
- compression_level
int
Integer between 1 and 9 specifying the compression level when using a compressed encoding (gzip or bzip). A value of
1
compresses the data the least amount and is the fastest, while a value of9
compresses the data the most and is the slowest.- index_order{‘C’, ‘F’}, optional
Specifies the index order used for writing. 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)).
- file
See also