nabu.io.writer module

nabu.io.writer.get_datetime()[source]

Function used by some writers to indicate the current date.

class nabu.io.writer.Writer(fname)[source]

Bases: object

Base class for all writers.

get_filename()[source]
class nabu.io.writer.TomoscanNXProcessWriter(fname, entry=None, filemode='a', overwrite=False)[source]

Bases: Writer

A class to write Nexus file with a processing result - using tomoscan.volumes as a backend

Initialize a NXProcessWriter.

Parameters:
  • fname (str) – Path to the HDF5 file.

  • entry (str, optional) – Entry in the HDF5 file. Default is “entry”

write(result, process_name, processing_index=0, config=None, data_name='data', is_frames_stack=True, direct_access=True) str[source]

Write the result in the current NXProcess group.

Parameters:
  • result (numpy.ndarray) – Array containing the processing result

  • process_name (str) – Name of the processing

  • processing_index (int) – Index of the processing (in a pipeline)

  • config (dict, optional) – Dictionary containing the configuration.

nabu.io.writer.h5_write_object(h5group, key, value, overwrite=False, default_val=None)[source]
class nabu.io.writer.NXProcessWriter(fname, entry=None, filemode='a', overwrite=False)[source]

Bases: Writer

A class to write Nexus file with a processing result.

Initialize a NXProcessWriter.

Parameters:
  • fname (str) – Path to the HDF5 file.

  • entry (str, optional) – Entry in the HDF5 file. Default is “entry”

write(result, process_name, processing_index=0, config=None, data_name='data', is_frames_stack=True, direct_access=True)[source]

Write the result in the current NXProcess group.

Parameters:
  • result (numpy.ndarray) – Array containing the processing result

  • process_name (str) – Name of the processing

  • processing_index (int) – Index of the processing (in a pipeline)

  • config (dict, optional) – Dictionary containing the configuration.

class nabu.io.writer.NXVolVolume(**kwargs)[source]

Bases: NXProcessWriter

An interface to NXProcessWriter with the same API than tomoscan.esrf.volume.

NX files are written in two ways:

  1. Partial files containing sub-volumes

  2. Final volume: master file with virtual dataset pointing to partial files

This class handles the first one, therefore expects the “start_index” parameter. In the case of HDF5, a sub-directory is creating to contain the partial files. In other words, if file_prefix=”recons” and output_dir=”/path/to/out”:

/path/to/out/recons.h5 # final master file /path/to/out/recons/

/path/to/out/recons/recons_00000.h5 /path/to/out/recons/recons_00100.h5 …

Initialize a NXProcessWriter.

Parameters:
  • fname (str) – Path to the HDF5 file.

  • entry (str, optional) – Entry in the HDF5 file. Default is “entry”

save()[source]
save_metadata()[source]
browse_data_files()[source]
nabu.io.writer.LegacyNXProcessWriter

alias of NXProcessWriter

nabu.io.writer.export_dict_to_h5(dic, h5file, h5path, overwrite_data=True, mode='a')[source]

Wrapper on top of silx.io.dictdump.dicttoh5 replacing None with “None”

Parameters:
  • dic (dict) – Dictionary containing the options

  • h5file (str) – File name

  • h5path (str) – Path in the HDF5 file

  • overwrite_data (bool, optional) – Whether to overwrite data when writing HDF5. Default is True

  • mode (str, optional) – File mode. Default is “a” (append).

nabu.io.writer.create_virtual_layout(files_or_pattern, h5_path, base_dir=None, axis=0, dtype='f')[source]

Create a HDF5 virtual layout.

Parameters:
  • files_or_pattern (str or list) – A list of file names, or a wildcard pattern. If a list is provided, it will not be sorted! This will have to be done before calling this function.

  • h5_path (str) – Path inside the HDF5 input file(s)

  • base_dir (str, optional) – Base directory when using relative file names.

  • axis (int, optional) – Data axis to merge. Default is 0.

nabu.io.writer.merge_hdf5_files(files_or_pattern, h5_path, output_file, process_name, output_entry=None, output_filemode='a', data_name='data', processing_index=0, config=None, base_dir=None, axis=0, overwrite=False, dtype='f')[source]
Parameters:
  • files_or_pattern (str or list) – A list of file names, or a wildcard pattern. If a list is provided, it will not be sorted! This will have to be done before calling this function.

  • h5_path (str) – Path inside the HDF5 input file(s)

  • output_file (str) – Path of the output file

  • process_name (str) – Name of the process

  • output_entry (str, optional) – Output HDF5 root entry (default is “/entry”)

  • output_filemode (str, optional) – File mode for output file. Default is “a” (append)

  • processing_index (int, optional) – Processing index for the output file. Default is 0.

  • config (dict, optional) – Dictionary describing the configuration needed to get the results.

  • base_dir (str, optional) – Base directory when using relative file names.

  • axis (int, optional) – Data axis to merge. Default is 0.

  • overwrite (bool, optional) – Whether to overwrite already existing data in the final file. Default is False.

class nabu.io.writer.TIFFWriter(fname, multiframe=False, start_index=0, filemode=None, append=False, big_tiff=None)[source]

Bases: Writer

Tiff writer.

Parameters:
  • fname (str) – Path to the output file name

  • multiframe (bool, optional) – Whether to write all data in one single file. Default is False.

  • start_index (int, optional) – When writing a stack of images, each image is written in a dedicated file (unless multiframe is set to True). In this case, the output is a series of files filename_0000.tif, filename_0001.tif, etc. This parameter is the starting index for file names. This option is ignored when multiframe is True.

  • filemode (str, optional) – DEPRECATED. Will be ignored. Please refer to ‘append’

  • append (bool, optional) – Whether to append data to the file rather than overwriting. Default is False.

  • big_tiff (bool, optional) – Whether to write in “big tiff” format: https://www.awaresystems.be/imaging/tiff/bigtiff.html Default is True when multiframe is True. Note that default “standard” tiff cannot exceed 4 GB.

Notes

If multiframe is False (default), then each image will be written in a dedicated tiff file.

write(data, *args, config=None, **kwargs)[source]
class nabu.io.writer.EDFWriter(fname, start_index=0, filemode='w')[source]

Bases: Writer

EDF (ESRF Data Format) writer.

Parameters:
  • fname (str) – Path to the output file name

  • start_index (int, optional) – When writing a stack of images, each image is written in a dedicated file In this case, the output is a series of files filename_0000.tif, filename_0001.edf, etc. This parameter is the starting index for file names.

write(data, *args, config=None, **kwargs)[source]
class nabu.io.writer.JP2Writer(fname, start_index=0, filemode='wb', psnr=None, cratios=None, auto_convert=True, float_clip_values=None, n_threads=None, overwrite=False, single_file=True)[source]

Bases: Writer

JPEG2000 writer. This class requires the python package glymur and the library libopenjp2.

Parameters:
  • fname (str) – Path to the output file name

  • start_index (int, optional) – When writing a stack of images, each image is written in a dedicated file The output is a series of files filename_0000.tif, filename_0001.tif, etc. This parameter is the starting index for file names.

  • psnr (list of int, optional) – The PSNR (Peak Signal-to-Noise ratio) for each jpeg2000 layer. This defines a quality metric for lossy compression. The number “0” stands for lossless compression.

  • cratios (list of int, optional) – Compression ratio for each jpeg2000 layer

  • auto_convert (bool, optional) – Whether to automatically cast floating point data to uint16. Default is True.

  • float_clip_values (tuple of floats, optional) – If set to a tuple of two values (min, max), then each image values will be clipped to these minimum and maximum values.

  • n_threads (int, optional) – Number of threads to use for encoding. Default is the number of available threads. Needs libopenjpeg >= 2.4.0.

write(data, *args, **kwargs)[source]
class nabu.io.writer.NPYWriter(fname)[source]

Bases: Writer

write(result, *args, **kwargs)[source]
class nabu.io.writer.NPZWriter(fname)[source]

Bases: Writer

write(result, *args, **kwargs)[source]
class nabu.io.writer.HSTVolWriter(fname, append=False, **kwargs)[source]

Bases: Writer

A writer to mimic PyHST2 “.vol” files

generate_metadata(data, **kwargs)[source]
static sanitize_metadata(metadata)[source]
write(data, *args, config=None, **kwargs)[source]
class nabu.io.writer.HSTVolVolume(**kwargs)[source]

Bases: HSTVolWriter

An interface to HSTVolWriter with the same API than tomoscan.esrf.volume. This is really not ideal, see nabu:#381

save()[source]
save_metadata()[source]
browse_data_files()[source]