{ "cells": [ { "cell_type": "markdown", "id": "ffb84091-b850-4054-bcbf-b17bc9e1f517", "metadata": {}, "source": [ "# Create the instrument.detector.data from tiff files\n", "\n", "If your acquisition has been saved under .tiff file you can use them as the 'instrument.detector.data' dataset" ] }, { "cell_type": "markdown", "id": "04ffec29-1d3c-4de8-8f6e-a5d85a6a57ff", "metadata": {}, "source": [ "## create an Nxtomo\n", "\n", "See tutorial on 'create an NXtomo (from scratch)'\n", "\n", "Here we create will bottstrap creation of an nxtomo with one dark and one flat frame at the beginning then 10 projections" ] }, { "cell_type": "code", "execution_count": null, "id": "49758bba-6dfa-4f70-8c4c-07bb1210ce9a", "metadata": {}, "outputs": [], "source": [ "import numpy\n", "from nxtomo import NXtomo\n", "from nxtomo.nxobject.nxdetector import ImageKey\n", "\n", "image_key_control = numpy.concatenate([\n", " [ImageKey.DARK_FIELD] * 1,\n", " [ImageKey.FLAT_FIELD] * 1,\n", " [ImageKey.PROJECTION] * 10,\n", "])\n", "\n", "my_nxtomo = NXtomo()\n", "my_nxtomo.instrument.detector.image_key_control = numpy.concatenate([\n", " [ImageKey.DARK_FIELD] * 1,\n", " [ImageKey.FLAT_FIELD] * 1,\n", " [ImageKey.PROJECTION] * 10,\n", "])\n", "my_nxtomo.sample.rotation_angle = numpy.concatenate([\n", " [0, 0],\n", " numpy.linspace(0, 360, 10),\n", "])\n", "# ..." ] }, { "cell_type": "markdown", "id": "07227061-08f5-4ccf-bd2b-98279a191e40", "metadata": {}, "source": [ "## append the tiff files\n", "\n", "To append a serie of tiff file we can use the util function `create_detector_dataset_from_tiff`.\n", "This function will create one [HDF5 virtual source](https://docs.h5py.org/en/stable/vds.html#h5py.VirtualSource) for each tiff file.\n", "\n", "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](https://docs.h5py.org/en/stable/vds.html#virtual-datasets-vds) and provide access to your tiff file.\n", "\n", "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.\n", "\n", "Note: Creation of the [HDF5 Virtual Dataset](https://docs.h5py.org/en/stable/vds.html#virtual-datasets-vds) will be handled by library when saving the NXtomo to disk." ] }, { "cell_type": "code", "execution_count": null, "id": "8296baa3-4e74-4817-80f6-96b345742356", "metadata": {}, "outputs": [], "source": [ "import h5py\n", "import os\n", "from nxtomo.utils.utils import create_detector_dataset_from_tiff\n", "\n", "output_file = \"my_nxtomo_with_tiff.nx\"\n", "with h5py.File(output_file, mode=\"w\") as h5f:\n", " external_dataset_group = h5f.require_group(\"external_datasets\")\n", "\n", " assert os.path.exists(\"resources/raw_tiff_files/\")\n", " my_nxtomo.instrument.detector.data = create_detector_dataset_from_tiff(\n", " tiff_files=(\n", " \"resources/raw_tiff_files/dark.tif\",\n", " \"resources/raw_tiff_files/flat_0000.tif\",\n", " \"resources/raw_tiff_files/projection_0000.tif\",\n", " \"resources/raw_tiff_files/projection_0001.tif\",\n", " \"resources/raw_tiff_files/projection_0002.tif\",\n", " \"resources/raw_tiff_files/projection_0003.tif\",\n", " \"resources/raw_tiff_files/projection_0004.tif\",\n", " \"resources/raw_tiff_files/projection_0005.tif\",\n", " \"resources/raw_tiff_files/projection_0006.tif\",\n", " \"resources/raw_tiff_files/projection_0007.tif\",\n", " \"resources/raw_tiff_files/projection_0008.tif\",\n", " \"resources/raw_tiff_files/projection_0009.tif\",\n", " ),\n", " external_dataset_group=external_dataset_group,\n", " relative_link=False,\n", " )" ] }, { "cell_type": "markdown", "id": "99e2b6f0-e9ae-49a1-9950-954ab4f7e8e4", "metadata": {}, "source": [ "### Note on using relative link vs absolute link !!!\n", "\n", "The most robust way to create a NXtomo should go by using relative link (in order to share it with the .tif files). Nevertheless there is today limitation on the resolution of the relative link with external dataset. (resolution is done according to the current working directory instead of the file...)\n", "The tomotools will handle it anyway but other software might not (like silx as this is a workaround and it should be handled at HDF5 level...) So be aware that those links might 'appear' broken when using relative link. This won't happen when using absolute links..." ] }, { "cell_type": "markdown", "id": "aad6d713-39c7-4f93-be53-87ae96c7070c", "metadata": {}, "source": [ "## save the final nxtomo" ] }, { "cell_type": "code", "execution_count": null, "id": "b9b4da86-58cb-446a-bd42-e241aa4359ed", "metadata": {}, "outputs": [], "source": [ "my_nxtomo.save(output_file, \"entry0000\")" ] }, { "cell_type": "markdown", "id": "a55f8933-6f59-4e59-b8ce-8056f17ed4f5", "metadata": {}, "source": [ "you can now use this NXtomo as any other nxtomo and process it with nabu or others." ] }, { "cell_type": "markdown", "id": "46f5dc6c-1588-4813-94fd-30d3628aea73", "metadata": {}, "source": [ "## Note on performances:\n", "\n", "Having one external link per frame will have a serious effect on performances.\n", "\n", "when create a sinogram we will be force to open each of the file and read a single line..." ] }, { "cell_type": "markdown", "id": "7ec41c95-6b4f-4176-8aa6-ba95a12e3f7b", "metadata": {}, "source": [ "## remove the output file" ] }, { "cell_type": "code", "execution_count": null, "id": "6c880208-cfc2-4c20-ac5d-1a62c8fed593", "metadata": {}, "outputs": [], "source": [ "os.remove(output_file)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.2" } }, "nbformat": 4, "nbformat_minor": 5 }