{ "cells": [ { "cell_type": "markdown", "id": "c7b28cea", "metadata": {}, "source": [ "# Interactive Features in cdiutils\n", "\n", "This notebook demonstrates the interactive visualisation and data exploration tools available in `cdiutils`:\n", "\n", "1. **`plot_3d_isosurface`**: Interactive 3D isosurface plotting with multiple quantities\n", "2. **`ThreeDViewer`**: Widget for exploring complex 3D arrays (amplitude + phase)\n", "3. **`TabPlotData`**: Interactive browser for exploring CDI reconstruction results\n", "\n", "## Installation\n", "\n", "These features require additional dependencies:\n", "```bash\n", "pip install cdiutils[interactive]\n", "```\n", "\n", "Or install individually:\n", "```bash\n", "pip install plotly scikit-image scipy ipywidgets h5glance\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "642ba5e5", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "import cdiutils\n", "from cdiutils.interactive import TabPlotData, ThreeDViewer, plot_3d_isosurface\n", "\n", "cdiutils.update_plot_params()" ] }, { "cell_type": "code", "execution_count": null, "id": "194ae93a", "metadata": {}, "outputs": [], "source": [ "# load reconstruction data from CXI file (here, we assume it has been generated by CDIutils pipeline)\n", "data_path = \"\"\n", "data = cdiutils.io.load_cxi(\n", " data_path,\n", " \"amplitude\",\n", " \"phase\",\n", " \"het_strain\",\n", " \"lattice_parameter\",\n", " \"dspacing\",\n", " \"support\",\n", ")\n", "voxel_size = cdiutils.io.load_cxi(data_path, \"voxel_size\")\n", "\n", "print(f\"Data shape: {data['amplitude'].shape}\")\n", "print(f\"Voxel size: {voxel_size} nm\")" ] }, { "cell_type": "markdown", "id": "89cbf86f", "metadata": {}, "source": [ "## `plot_3d_isosurface`\n", "\n", "Interactive isosurface plot with multiple quantities. Features:\n", "- **Threshold slider**: control isosurface level\n", "- **Quantity dropdown**: switch between different data arrays\n", "- **Colormap dropdown**: choose from 20+ colormaps\n", "- **Colorbar controls**: auto-scale, symmetric mode, or manual limits\n", "- **Replace NaN checkbox**: replace NaN values with mean to avoid artefacts" ] }, { "cell_type": "code", "execution_count": null, "id": "e0834298", "metadata": {}, "outputs": [], "source": [ "plot_3d_isosurface(\n", " data[\"amplitude\"],\n", " data,\n", " voxel_size=voxel_size,\n", ")" ] }, { "cell_type": "markdown", "id": "64e8f12e", "metadata": {}, "source": [ "## `ThreeDViewer` Class\n", "\n", "A widget for exploring complex 3D arrays (amplitude + phase). Features:\n", "- **Threshold slider**: control isosurface level based on amplitude\n", "- **Phase/Amplitude toggle**: switch between phase and amplitude display\n", "- **Colormap dropdown**: choose from 20+ colormaps\n", "- **Colorbar controls**: auto-scale, symmetric mode, or manual limits\n", "- **Replace NaN checkbox**: replace NaN values with mean\n", "- **Rotation toggle**: enable continuous rotation animation\n", "- **Theme toggle**: switch between light and dark themes" ] }, { "cell_type": "code", "execution_count": null, "id": "47d7a5ec", "metadata": {}, "outputs": [], "source": [ "# create complex 3D array\n", "complex_3d = data[\"amplitude\"] * np.exp(1j * data[\"phase\"])\n", "\n", "# create viewer\n", "viewer = ThreeDViewer(complex_3d, voxel_size=voxel_size, figsize=(9, 6))\n", "viewer.show()" ] }, { "cell_type": "markdown", "id": "abfb7c1f", "metadata": {}, "source": [ "## Notes\n", "\n", "- Try adjusting the threshold slider to explore different isosurface levels\n", "- Use the colormap dropdown to find the best visualisation for your data\n", "- Enable \"Replace NaN with mean\" if you see unusual colours (NaN artefacts)\n", "- For strain data, use symmetric colorbar with diverging colormaps like \"cet_CET_D13\"\n", "- The dark theme can be helpful when working in low-light environments" ] }, { "cell_type": "markdown", "id": "e4c1cfee", "metadata": {}, "source": [ "## `TabPlotData` Class\n", "\n", "An interactive browser for exploring CDI reconstruction results from a folder structure. Features:\n", "- **Tab navigation**: browse through different scans and reconstructions\n", "- **Interactive plots**: 2D slices and 3D visualisations\n", "- **Data inspection**: view metadata and reconstruction parameters\n", "- **Quick comparison**: easily compare results from different scans" ] }, { "cell_type": "code", "execution_count": null, "id": "6b4dc0a0", "metadata": {}, "outputs": [], "source": [ "path_to_results_dir = \"\"\n", "tab = TabPlotData(path_to_results_dir)\n", "tab.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }