Create a ImageDataset object in Python#

A lot of low-level functions in Darfix takes a ImageDataset or a Dataset object as input. When testing/debugging these, it can be useful to create such an object in Python to run the functions.

To do this, we use the load_process_data function:

darfix.core.data_selection.load_process_data(filenames, root_dir=None, in_memory=True, dark_filename=None, copy_files=True, isH5=False, title='', metadata_url=None)[source]#

Loads data from filenames. If filenames is:

  • a str: consider it as a file pattern (for EDF files).

  • an iterable: consider a list of EDF files.

  • a DataUrl: consider it readable by silx get_data function

Parameters:
  • filenames (Union[str, Iterable[str], DataUrl]) – filenames to be loaded.

  • metadata_url – path to the scan metadata for HDF5 containing positioner information in order to load metadata for non-edf files

  • root_dir (Optional[str])

  • in_memory (bool)

  • dark_filename (Union[str, DataUrl, None])

  • copy_files (bool)

  • isH5 (bool)

  • title (str)

Here is an example on how to use it to load a HDF5 dataset (here the Silicon_111_reflection_0003 dataset)

from darfix.core.data_selection import load_process_data

filepath = '/data/scisoft/darfix/datasets/darfix/bliss_hdf5/Silicon_111_reflection_0003/Silicon_111_reflection_0003.h5'
scan_index = 1
detector_name = 'pco_ff'

img_dataset, indices, bg_indices, bg_dataset = load_process_data(
    filenames=f"silx://{filepath}?/{scan_index}.1/measurement/{detector_name}",
    metadata_url=f"silx://{filepath}?{scan_index}.1/instrument/positioners",
    copy_files=False,
    isH5=True,
)

Then, we use the find_dimensions method to register the appropriate dimensions in the ImageDataset. This is mandatory to run most operations on a Dataset

from darfix.core.dimension import POSITIONER_METADATA

img_dataset.find_dimensions(kind=POSITIONER_METADATA, tolerance=1e-5)

The tolerance need to be changed depending on the dataset used. We use here the value appropriate for Silicon_111_reflection_0003, the default value being 1e-9.

The ImageDataset is now ready for computations. For example, we can compute the moments (Center of Mass, Skewness, Kurtosis, …) by using apply_moments:

img_dataset.apply_moments()
moments = img_dataset.moments_dims

Tip

For some operations (e.g. noise removal), you will need a Dataset object. You can generate it from the ImageDataset object:

from darfix.dtypes import Dataset
from darfix.core.noiseremoval import apply_background_subtraction

dataset = Dataset(img_dataset)

new_img_dataset = apply_background_subtraction(dataset)