Create the instrument.detector.data from tiff files#

If your acquisition has been saved under .tiff file you can use them as the ‘instrument.detector.data’ dataset

create an Nxtomo#

See tutorial on ‘create an NXtomo (from scratch)’

Here we create will bottstrap creation of an nxtomo with one dark and one flat frame at the beginning then 10 projections

[1]:
import numpy
from nxtomo import NXtomo
from nxtomo.nxobject.nxdetector import ImageKey

image_key_control = numpy.concatenate([
    [ImageKey.DARK_FIELD] * 1,
    [ImageKey.FLAT_FIELD] * 1,
    [ImageKey.PROJECTION] * 10,
])

my_nxtomo = NXtomo()
my_nxtomo.instrument.detector.image_key_control = numpy.concatenate([
    [ImageKey.DARK_FIELD] * 1,
    [ImageKey.FLAT_FIELD] * 1,
    [ImageKey.PROJECTION] * 10,
])
my_nxtomo.sample.rotation_angle = numpy.concatenate([
    [0, 0],
    numpy.linspace(0, 360, 10),
])
# ...

append the tiff files#

To append a serie of tiff file we can use the util function create_detector_dataset_from_tiff. This function will create one HDF5 virtual source for each tiff file.

In order to create this virtual source it need to have one ‘external dataset’ per file. This external dataset define the way to access the raw data. This is this external dataset that will be used by the HDF5 Virtual Dataset and provide access to your tiff file.

In order to create the external dataset the create_detector_dataset_from_tiff needs to have a HDF5 group (as output group). It should be the file containing the NXtomo.

Note: Creation of the HDF5 Virtual Dataset will be handled by library when saving the NXtomo to disk.

[2]:
import h5py
import os
from nxtomo.utils.utils import create_detector_dataset_from_tiff

output_file = "my_nxtomo_with_tiff.nx"
with h5py.File(output_file, mode="w") as h5f:
    external_dataset_group = h5f.require_group("external_datasets")

    assert os.path.exists("resources/raw_tiff_files/")
    my_nxtomo.instrument.detector.data = create_detector_dataset_from_tiff(
        tiff_files=(
            "resources/raw_tiff_files/dark.tif",
            "resources/raw_tiff_files/flat_0000.tif",
            "resources/raw_tiff_files/projection_0000.tif",
            "resources/raw_tiff_files/projection_0001.tif",
            "resources/raw_tiff_files/projection_0002.tif",
            "resources/raw_tiff_files/projection_0003.tif",
            "resources/raw_tiff_files/projection_0004.tif",
            "resources/raw_tiff_files/projection_0005.tif",
            "resources/raw_tiff_files/projection_0006.tif",
            "resources/raw_tiff_files/projection_0007.tif",
            "resources/raw_tiff_files/projection_0008.tif",
            "resources/raw_tiff_files/projection_0009.tif",
        ),
        external_dataset_group=external_dataset_group,
        relative_link=False,
    )

save the final nxtomo#

[3]:
my_nxtomo.save(output_file, "entry0000")

you can now use this NXtomo as any other nxtomo and process it with nabu or others.

Note on performances:#

Having one external link per frame will have a serious effect on performances.

when create a sinogram we will be force to open each of the file and read a single line…

remove the output file#

[4]:
os.remove(output_file)