nabu.io.reader module

class nabu.io.reader.Reader(sub_region=None)[source]

Bases: object

Abstract class for various file readers.

Parameters:

sub_region (tuple, optional) – Coordinates in the form (start_x, end_x, start_y, end_y), to read a subset of each frame. It can be used for Regions of Interest (ROI). Indices start at zero !

get_data(data_url)[source]

Get data from a silx.io.url.DataUrl

release()[source]

Release the file if needed.

class nabu.io.reader.NPReader(sub_region=None, mmap=True)[source]

Bases: Reader

Reader for NPY/NPZ files. Mostly used for internal development. Please refer to the documentation of nabu.io.reader.Reader

multi_load = True
get_data(data_url)[source]

Get data from a silx.io.url.DataUrl

release()[source]

Release the file if needed.

class nabu.io.reader.EDFReader(sub_region=None)[source]

Bases: Reader

A class for reading series of EDF Files. Multi-frames EDF are not supported.

multi_load = False
read(fname)[source]
get_data(data_url)[source]

Get data from a silx.io.url.DataUrl

class nabu.io.reader.HDF5Reader(sub_region=None)[source]

Bases: Reader

A class for reading a HDF5 File.

multi_load = True
get_data(data_url)[source]

Get data from a silx.io.url.DataUrl

release()[source]

Release the file if needed.

class nabu.io.reader.HDF5Loader(fname, data_path, sub_region=None, data_buffer=None, pre_allocate=True, dtype='f')[source]

Bases: object

An alternative class to HDF5Reader where information is first passed at class instantiation

load_data(force_load=False)[source]
class nabu.io.reader.ChunkReader(files, sub_region=None, detector_corrector=None, pre_allocate=True, data_buffer=None, convert_float=False, shape=None, dtype=None, binning=None, dataset_subsampling=None, num_threads=None)[source]

Bases: object

A reader of chunk of images.

Initialize a “ChunkReader”. A chunk is a stack of images.

Parameters:
  • files (dict) – Dictionary where the key is the file/data index, and the value is a silx.io.url.DataUrl pointing to the data. The dict must contain only the files which shall be used ! Note: the shape and data type is infered from the first data file.

  • sub_region (tuple, optional) – If provided, this must be a tuple in the form (start_x, end_x, start_y, end_y). Each image will be cropped to this region. This is used to specify a chunk of files. Each of the parameters can be None, in this case the default start and end are taken in each dimension.

  • pre_allocate (bool) – Whether to pre-allocate data before reading.

  • data_buffer (array-like, optional) – If pre_allocate is set to False, this parameter has to be provided. It is an array-like object which will hold the data.

  • convert_float (bool) – Whether to convert data to float32, regardless of the input data type.

  • shape (tuple, optional) – Shape of each image. If not provided, it is inferred from the first image in the collection.

  • dtype (numpy.dtype, optional) – Data type of each image. If not provided, it is inferred from the first image in the collection.

  • binning (int or tuple of int, optional) – Whether to bin the data. If multi-dimensional binning is done, the parameter must be in the form (binning_x, binning_y). Each image will be binned by these factors.

  • dataset_subsampling (int or tuple, optional) – Subsampling factor when reading the images. If an integer n is provided, then one image out of n will be read. If a tuple of integers (step, begin) is given, the data is read as data[begin::step]

  • num_threads (int, optional) – Number of threads to use for binning the data. Default is to use all available threads. This parameter has no effect when binning is disabled.

Notes

The files are provided as a collection of silx.io.DataURL. The file type is inferred from the extension.

Binning is different from subsampling. Using binning will not speed up the data retrieval (quite the opposite), since the whole (subregion of) data is read and then binning is performed.

get_data(file_url)[source]

Get the data associated to a file url.

load_files(overwrite: bool = False)[source]

Load the files whose links was provided at class instantiation.

Parameters:

overwrite (bool, optional) – Whether to force reloading the files if already loaded.

load_data(overwrite: bool = False)

Load the files whose links was provided at class instantiation.

Parameters:

overwrite (bool, optional) – Whether to force reloading the files if already loaded.

property data
nabu.io.reader.load_images_from_dataurl_dict(data_url_dict, **chunk_reader_kwargs)[source]

Load a dictionary of dataurl into numpy arrays.

Parameters:
  • data_url_dict (dict) – A dictionary where the keys are integers (the index of each image in the dataset), and the values are numpy.ndarray (data_url_dict).

  • chunk_reader_kwargs (params) – Named parameters passed to nabu.io.reader.ChunkReader.

Returns:

res – A dictionary where the keys are the same as data_url_dict, and the values are numpy arrays.

Return type:

dict

nabu.io.reader.load_images_stack_from_hdf5(fname, h5_data_path, sub_region=None)[source]

Load a 3D dataset from a HDF5 file.

Parameters:
  • fname (str) – File path

  • h5_data_path (str) – Data path within the HDF5 file

  • sub_region (tuple, optional) – Tuple indicating which sub-volume to load, in the form (xmin, xmax, ymin, ymax, zmin, zmax) where the 3D dataset has the python shape (N_Z, N_Y, N_X). This means that the data will be loaded as data[zmin:zmax, ymin:ymax, xmin:xmax].

nabu.io.reader.get_hdf5_dataset_shape(fname, h5_data_path, sub_region=None)[source]
nabu.io.reader.check_virtual_sources_exist(fname, data_path)[source]
nabu.io.reader.import_h5_to_dict(h5file, h5path, asarray=False)[source]

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

Parameters:
  • h5file (str) – File name

  • h5path (str) – Path in the HDF5 file

  • asarray (bool, optional) – Whether to convert each numeric value to an 0D array. Default is False.