{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "# Tutorial 5: Analysis from memory", "id": "bd0f8378fe6c4303" }, { "metadata": {}, "cell_type": "markdown", "source": [ "Instead of loading images and peaks from a file and converting them to polar coordinates at each step, we developed an approach that works directly from a `pygid.Conversion` instance. After the analysis, all results can be saved to a NeXus file for further use.\n", "\n", "---\n", "### 1. `pygid.Conversion` initialization\n", "\n", "Create a `pygid.Conversion` class instance based on the experimental parameters and load metadata using `pygid`. The detailed description can be found in [`pygid` documentation](https://pygid.readthedocs.io/en/latest/tutorials_toctree.html)." ], "id": "25b3fd41c4c0ed19" }, { "metadata": { "ExecuteTime": { "end_time": "2026-03-27T12:21:55.299536379Z", "start_time": "2026-03-27T12:21:55.288173017Z" } }, "cell_type": "code", "source": [ "import pygid\n", "\n", "# Experimental metadata\n", "exp_metadata = pygid.ExpMetadata(\n", " start_time = r\"2025-09-09T20:36:23.076828\",\n", " end_time = r\"2025-09-09T20:37:24.076828\",\n", " source_type = \"synchrotron\",\n", " source_name = \"ESRF ID10\",\n", " detector = \"eiger4m\",\n", " monitor = 294302)\n", "\n", "# Sample metadata from YAML file\n", "smpl_metadata = pygid.SampleMetadata(path_to_load=\"../../example/sample.yaml\")" ], "id": "f5cc2a7e4042b94d", "outputs": [], "execution_count": 7 }, { "metadata": { "ExecuteTime": { "end_time": "2026-03-27T12:21:55.323345856Z", "start_time": "2026-03-27T12:21:55.300591604Z" } }, "cell_type": "code", "source": [ "poni_path = '../../example/laB6_2025_09_05.poni' # path to the PONI file\n", "mask_path = '../../example/mask.npy' # path to the mask file\n", "filename = '../../example/eiger4m_0000.h5' # path to the raw data\n", "dataset = '/entry/data0/image' # image root in the raw data file\n", "frame_num = None # frame number to process\n", "\n", "# Set the experimental parameters\n", "params = pygid.ExpParams(\n", "\tponi_path = poni_path,\n", " mask_path = mask_path,\n", " fliplr = True,\n", " flipud = True,\n", " # transp = False,\n", " ai = 0.075\n", ")\n", "\n", "# Set the range, resolution and intensity corrections\n", "matrix = pygid.CoordMaps(\n", " params,\n", " vert_positive = True, hor_positive = True,\n", " q_xy_range = (0, 3.5), q_z_range = (0, 3.5), dq = 0.002,\n", ")\n", "\n", "# Load the data\n", "conversion = pygid.Conversion(\n", " matrix = matrix,\n", " path = filename,\n", " dataset = dataset,\n", " frame_num = frame_num\n", ")" ], "id": "c2dde4a74433fd2", "outputs": [], "execution_count": 8 }, { "metadata": {}, "cell_type": "markdown", "source": "NOTE: the choice of the frame number will be ignored during the analysis. All images loaded at this step (`pygid.Conversion`) will be processed.", "id": "de6f193e6d882028" }, { "metadata": {}, "cell_type": "markdown", "source": [ "---\n", "\n", "### 2. `mlgidBASE` initialization\n", "The initialization of `mlgidBASE` should be performed before the conversion." ], "id": "3c279af3986b29b5" }, { "metadata": { "ExecuteTime": { "end_time": "2026-03-27T12:21:55.751925508Z", "start_time": "2026-03-27T12:21:55.323807995Z" } }, "cell_type": "code", "source": [ "from mlgidbase import mlgidBASE\n", "analysis = mlgidBASE(pygid_conversion=conversion)" ], "id": "3a911dc23c676f41", "outputs": [], "execution_count": 9 }, { "metadata": {}, "cell_type": "markdown", "source": [ "---\n", "\n", "### 3. Run analysis methods\n", "\n", "Run analysis functions as described in previous tutorials:" ], "id": "b2e056b7ab3a6b85" }, { "metadata": { "ExecuteTime": { "end_time": "2026-03-27T12:22:01.491424637Z", "start_time": "2026-03-27T12:21:55.755121183Z" } }, "cell_type": "code", "source": [ "analysis.run_detection()\n", "analysis.run_fitting()\n", "analysis.run_matching(\n", " cif_prepr = r'../../example/prepr_cifs.pickle',\n", " peaks_type='segments')" ], "id": "977a20912c6cbbae", "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO - Loading model\n", "INFO - Using the GPU for inference\n" ] } ], "execution_count": 10 }, { "metadata": {}, "cell_type": "markdown", "source": [ "---\n", "### 4. Save result" ], "id": "7092b2add763de46" }, { "metadata": { "ExecuteTime": { "end_time": "2026-03-27T12:22:01.535824738Z", "start_time": "2026-03-27T12:22:01.496427778Z" } }, "cell_type": "code", "source": [ "analysis.save_result(\n", " path_to_save='../../example/BA2PbI4.h5',\n", " smpl_metadata=smpl_metadata,\n", " exp_metadata=exp_metadata,\n", ")" ], "id": "c558a9b762ba1c1b", "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO - Saved in /home/ainurabukaev/data/Programming/mlgid/mlgidBASE/examples/BA2PbI4.h5 in group entry_0000\n" ] } ], "execution_count": 11 }, { "metadata": {}, "cell_type": "markdown", "source": "NOTE: The converted image is not available in memory after saving", "id": "1162f76ffaffb79e" }, { "metadata": {}, "cell_type": "markdown", "source": [ "---\n", "\n", "### Parameters\n", "\n", "- `path_to_save` (str) – output path for the HDF5 result file.\n", "- `h5_group` (str) – name of the NXentry dataset within the HDF5 file.\n", "- `overwrite_file` (bool) – if `True`, overwrites existing HDF5 file. Default is `True`.\n", "- `overwrite_group` (bool) – if `True`, overwrites existing NXentry group. Default is `True`.\n", "- `exp_metadata` (`ExpMetadata` )– instance containing experimental information\n", "- `smpl_metadata` (`SampleMetadata` ) – instance containing sample information" ], "id": "6f16700a8dfcc5a5" }, { "metadata": {}, "cell_type": "markdown", "source": [ "---\n", "### Batch processing\n", "The analysis is not implemented for batch processing. For usage with huge amount of images use an external loop for data conversion and analysis:\n", "\n", "\n", "```python\n", "for frame_num in range(100):\n", " # load a single image\n", " conversion = pygid.Conversion(\n", " matrix = matrix,\n", " path = filename,\n", " dataset = dataset,\n", " frame_num = frame_num)\n", "\n", " # run analysis\n", " analysis = mlgidBASE(pygid_conversion=conversion)\n", " analysis.run_detection()\n", " analysis.run_fitting()\n", " analysis.run_matching(\n", " cif_prepr = r'../../example/prepr_cifs.pickle',\n", " peaks_type='segments')\n", "\n", " # save result\n", " analysis.save_result(\n", " path_to_save='result_from_conversion.h5',\n", " smpl_metadata = smpl_metadata,\n", " exp_metadata=exp_metadata,\n", " overwrite_file=False,\n", " overwrite_group=False)\n", "```" ], "id": "a42678b1f777cdc7" }, { "metadata": { "ExecuteTime": { "end_time": "2026-03-27T12:22:01.540713703Z", "start_time": "2026-03-27T12:22:01.537595476Z" } }, "cell_type": "code", "source": "", "id": "9cc68fc3d3f748ea", "outputs": [], "execution_count": 11 } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }