{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:32:12.241203Z", "iopub.status.busy": "2026-08-18T18:32:12.241024Z", "iopub.status.idle": "2026-08-18T18:32:12.244879Z", "shell.execute_reply": "2026-08-18T18:32:12.244322Z" }, "tags": [ "hide-in-docs" ] }, "outputs": [], "source": [ "# Check whether easydiffraction is installed; install it if needed.\n", "# Required for remote environments such as Google Colab.\n", "import importlib.util\n", "\n", "if importlib.util.find_spec('easydiffraction') is None:\n", " %pip install easydiffraction==0.20.1" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "# Co2SiO4 T-scan Resume\n", "\n", "This example loads a previously saved Co2SiO4 project after a\n", "sequential refinement was stopped before all scan files were\n", "processed. The saved project contains 213 scan files and a partial\n", "`analysis/results.csv` with 210 completed rows. Running\n", "`project.analysis.fit()` resumes from the remaining three datasets and\n", "appends the missing results." ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## πŸ› οΈ Import Library" ] }, { "cell_type": "code", "execution_count": 2, "id": "3", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:32:12.246466Z", "iopub.status.busy": "2026-08-18T18:32:12.246302Z", "iopub.status.idle": "2026-08-18T18:32:15.006278Z", "shell.execute_reply": "2026-08-18T18:32:15.005367Z" } }, "outputs": [], "source": [ "import easydiffraction as edi" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## πŸ“‚ Load Project" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "### Locate Project\n", "\n", "Download and extract the saved Co2SiO4 scan project from the\n", "EasyDiffraction data repository. The project contains the scan data and\n", "the partial sequential-fit results needed for resuming." ] }, { "cell_type": "code", "execution_count": 3, "id": "6", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:32:15.008344Z", "iopub.status.busy": "2026-08-18T18:32:15.008044Z", "iopub.status.idle": "2026-08-18T18:32:15.657312Z", "shell.execute_reply": "2026-08-18T18:32:15.656417Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mGetting data\u001b[0m\u001b[1;36m...\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data \u001b[32m'proj-cosio-d20-scan'\u001b[0m: Co2SiO4, D20 \u001b[1m(\u001b[0mILL\u001b[1m)\u001b[0m, \u001b[1;36m213\u001b[0m-file T-scan resume project with \u001b[1;36m210\u001b[0m completed results\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "βœ… Data \u001b[32m'proj-cosio-d20-scan'\u001b[0m downloaded and extracted to \u001b[32m'../../../projects/proj-cosio-d20-scan-cee816aee63d'\u001b[0m\n" ] } ], "source": [ "project_dir = edi.download_data('proj-cosio-d20-scan', destination='projects', overwrite=True)" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "### Load Project\n", "\n", "The project is downloaded into a fresh writable working directory so\n", "resuming the sequential fit appends to its `analysis/results.csv`." ] }, { "cell_type": "code", "execution_count": 4, "id": "8", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:32:15.659099Z", "iopub.status.busy": "2026-08-18T18:32:15.658824Z", "iopub.status.idle": "2026-08-18T18:32:16.913491Z", "shell.execute_reply": "2026-08-18T18:32:16.912625Z" } }, "outputs": [], "source": [ "project = edi.Project.load(project_dir)" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "## πŸš€ Perform Analysis" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "### Display Structure\n", "\n", "Render the Co2SiO4 structure restored from the saved project." ] }, { "cell_type": "code", "execution_count": 5, "id": "11", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:32:16.915264Z", "iopub.status.busy": "2026-08-18T18:32:16.915096Z", "iopub.status.idle": "2026-08-18T18:32:16.946586Z", "shell.execute_reply": "2026-08-18T18:32:16.946107Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mStructure 🧩 \u001b[0m\u001b[32m'cosio'\u001b[0m\u001b[1;36m \u001b[0m\u001b[1;36m(\u001b[0m\u001b[1;36mAtom view type: \u001b[0m\u001b[32m'adp'\u001b[0m\u001b[1;36m)\u001b[0m\n" ] }, { "data": { "text/html": [ "
\n", "
\n", "
Loading plot…
\n", "
\n", "
\n", "
\n", "
drag = rotate
wheel = zoom
right-drag = pan
\n", "
\n", "
\n", "\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.structure(struct_name='cosio')" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "### Resume Sequential Analysis\n", "\n", "This project already stores the template experiment, sequential-fit\n", "settings, and the partial `analysis/results.csv` from the previous\n", "run. Running the fit again skips datasets already present in the CSV\n", "and continues from the remaining files." ] }, { "cell_type": "code", "execution_count": 6, "id": "13", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:32:16.948521Z", "iopub.status.busy": "2026-08-18T18:32:16.948337Z", "iopub.status.idle": "2026-08-18T18:33:23.027359Z", "shell.execute_reply": "2026-08-18T18:33:23.026496Z" } }, "outputs": [ { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function() {\n", " const button = document.getElementById('ed-fit-stop-16661004514440bd9bbdcdd05c236c96-button');\n", " const status = document.getElementById('ed-fit-stop-16661004514440bd9bbdcdd05c236c96-status');\n", " const kernelId = '';\n", " if (!button) {\n", " return;\n", " }\n", "\n", " function setStatus(text) {\n", " if (status) {\n", " status.textContent = text;\n", " }\n", " }\n", "\n", " function pageConfig() {\n", " const element = document.getElementById('jupyter-config-data');\n", " if (!element || !element.textContent) {\n", " return {};\n", " }\n", " try {\n", " return JSON.parse(element.textContent);\n", " } catch (error) {\n", " return {};\n", " }\n", " }\n", "\n", " function baseUrl(config) {\n", " const configured = config.baseUrl || config.base_url ||\n", " (window.Jupyter && Jupyter.notebook && Jupyter.notebook.base_url);\n", " if (configured) {\n", " return configured.endsWith('/') ? configured : configured + '/';\n", " }\n", " const markers = ['/lab/', '/notebooks/', '/tree/'];\n", " for (const marker of markers) {\n", " const index = window.location.pathname.indexOf(marker);\n", " if (index >= 0) {\n", " return window.location.pathname.slice(0, index + 1);\n", " }\n", " }\n", " return '/';\n", " }\n", "\n", " function token(config) {\n", " return config.token || new URLSearchParams(window.location.search).get('token') || '';\n", " }\n", "\n", " function cookie(name) {\n", " const prefix = name + '=';\n", " for (const part of document.cookie.split(';')) {\n", " const trimmed = part.trim();\n", " if (trimmed.startsWith(prefix)) {\n", " return decodeURIComponent(trimmed.slice(prefix.length));\n", " }\n", " }\n", " return '';\n", " }\n", "\n", " function notebookPath() {\n", " const decoded = decodeURIComponent(window.location.pathname);\n", " const markers = ['/lab/tree/', '/notebooks/', '/tree/'];\n", " for (const marker of markers) {\n", " const index = decoded.indexOf(marker);\n", " if (index >= 0) {\n", " return decoded.slice(index + marker.length);\n", " }\n", " }\n", " return '';\n", " }\n", "\n", " async function kernelFromSessions(config) {\n", " const url = new URL(baseUrl(config) + 'api/sessions', window.location.origin);\n", " const authToken = token(config);\n", " if (authToken) {\n", " url.searchParams.set('token', authToken);\n", " }\n", " const response = await fetch(url, {credentials: 'same-origin'});\n", " if (!response.ok) {\n", " return '';\n", " }\n", " const sessions = await response.json();\n", " const path = notebookPath();\n", " const session = sessions.find((item) => item.path === path) || sessions[0];\n", " return session && session.kernel ? session.kernel.id : '';\n", " }\n", "\n", " async function interruptKernel(config, resolvedKernelId) {\n", " const url = new URL(\n", " baseUrl(config) + 'api/kernels/' + resolvedKernelId + '/interrupt',\n", " window.location.origin\n", " );\n", " const authToken = token(config);\n", " if (authToken) {\n", " url.searchParams.set('token', authToken);\n", " }\n", " const xsrfToken = cookie('_xsrf');\n", " const headers = {};\n", " if (xsrfToken) {\n", " headers['X-XSRFToken'] = xsrfToken;\n", " }\n", " const response = await fetch(url, {\n", " method: 'POST',\n", " credentials: 'same-origin',\n", " headers: headers\n", " });\n", " return response.ok;\n", " }\n", "\n", " button.addEventListener('click', async function() {\n", " button.disabled = true;\n", " setStatus('Stopping...');\n", " const config = pageConfig();\n", " try {\n", " const resolvedKernelId = kernelId || await kernelFromSessions(config);\n", " if (!resolvedKernelId) {\n", " throw new Error('Could not resolve the current kernel id.');\n", " }\n", " const interrupted = await interruptKernel(config, resolvedKernelId);\n", " if (!interrupted) {\n", " throw new Error('Jupyter Server rejected the interrupt request.');\n", " }\n", " setStatus('Interrupt sent...');\n", " } catch (error) {\n", " button.disabled = false;\n", " setStatus('Use Kernel > Interrupt to stop this fit.');\n", " }\n", " });\n", "})();\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "πŸ“‚ Resuming from CSV: \u001b[1;36m210\u001b[0m files already fitted.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mSequential fitting\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "πŸš€ Starting fit process with \u001b[32m'bumps \u001b[0m\u001b[32m(\u001b[0m\u001b[32mlm\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m\u001b[33m...\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "πŸ“‹ \u001b[1;36m3\u001b[0m files in \u001b[1;36m1\u001b[0m chunks \u001b[1m(\u001b[0m\u001b[33mmax_workers\u001b[0m=\u001b[1;36m4\u001b[0m\u001b[1m)\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "πŸ“ˆ Goodness-of-fit progress:\n" ] }, { "data": { "text/html": [ "
chunkprogresstime (s)filescountaverage χ²status
11/1100.0%64.70002p3630_all594632.dat - 002p3440_all594631.dat353.31βœ…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "βœ… Sequential fitting complete: \u001b[1;36m213\u001b[0m files processed.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "πŸ“„ Results saved to \u001b[32m'../../../projects/proj-cosio-d20-scan-cee816aee63d/analysis/results.csv'\u001b[0m\n" ] } ], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "### Replay Fitted Datasets\n", "\n", "Apply fitted parameters from the first CSV row and plot the result." ] }, { "cell_type": "code", "execution_count": 7, "id": "15", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:33:23.028918Z", "iopub.status.busy": "2026-08-18T18:33:23.028755Z", "iopub.status.idle": "2026-08-18T18:33:23.653749Z", "shell.execute_reply": "2026-08-18T18:33:23.652903Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.apply_params_from_csv(row_index=0)\n", "project.display.pattern(expt_name='d20')" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "\n", "Apply fitted parameters from the last CSV row and plot the result." ] }, { "cell_type": "code", "execution_count": 8, "id": "17", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:33:23.656729Z", "iopub.status.busy": "2026-08-18T18:33:23.656534Z", "iopub.status.idle": "2026-08-18T18:33:24.537042Z", "shell.execute_reply": "2026-08-18T18:33:24.536195Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.apply_params_from_csv(row_index=-1)\n", "project.display.pattern(expt_name='d20')" ] }, { "cell_type": "markdown", "id": "18", "metadata": {}, "source": [ "### Display Parameter Evolution\n", "\n", "Use the same persisted diffrn path stored in `analysis/results.csv`\n", "for the x-axis." ] }, { "cell_type": "code", "execution_count": 9, "id": "19", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:33:24.539771Z", "iopub.status.busy": "2026-08-18T18:33:24.539599Z", "iopub.status.idle": "2026-08-18T18:33:24.542675Z", "shell.execute_reply": "2026-08-18T18:33:24.541579Z" } }, "outputs": [], "source": [ "temperature = 'diffrn.ambient_temperature'" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "Plot fit quality metrics vs. temperature." ] }, { "cell_type": "code", "execution_count": 10, "id": "21", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:33:24.544124Z", "iopub.status.busy": "2026-08-18T18:33:24.543973Z", "iopub.status.idle": "2026-08-18T18:33:24.596614Z", "shell.execute_reply": "2026-08-18T18:33:24.595742Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.fit.series(\n", " project.analysis.fit_result.success,\n", " versus=temperature,\n", ")\n", "project.display.fit.series(\n", " project.analysis.fit_result.reduced_chi_square,\n", " versus=temperature,\n", ")\n", "project.display.fit.series(\n", " project.analysis.fit_result.iterations,\n", " versus=temperature,\n", ")" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "Omitting `param` plots every fitted parameter one after another." ] }, { "cell_type": "code", "execution_count": 11, "id": "23", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:33:24.598282Z", "iopub.status.busy": "2026-08-18T18:33:24.598053Z", "iopub.status.idle": "2026-08-18T18:33:25.461129Z", "shell.execute_reply": "2026-08-18T18:33:25.460277Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.fit.series(versus=temperature)" ] }, { "cell_type": "markdown", "id": "24", "metadata": {}, "source": [ "## πŸ’Ύ Save Project" ] }, { "cell_type": "code", "execution_count": 12, "id": "25", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:33:25.462966Z", "iopub.status.busy": "2026-08-18T18:33:25.462787Z", "iopub.status.idle": "2026-08-18T18:33:25.834526Z", "shell.execute_reply": "2026-08-18T18:33:25.833813Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mSaving project πŸ“¦ \u001b[0m\u001b[32m'cosio_d20_scan'\u001b[0m\u001b[1;36m to \u001b[0m\u001b[32m'../../../projects/refine-cosio-d20-tscan-resumed'\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”œβ”€β”€ πŸ“„ project.edi\n", "β”œβ”€β”€ πŸ“ structures/\n", "β”‚ └── πŸ“„ cosio.edi\n", "β”œβ”€β”€ πŸ“ experiments/\n", "β”‚ └── πŸ“„ d20.edi\n", "β”œβ”€β”€ πŸ“ analysis/\n", "β”‚ └── πŸ“„ analysis.edi\n", "└── πŸ“ reports/\n", " └── πŸ“„ cosio_d20_scan.html\n" ] } ], "source": [ "project.save_as(dir_path='projects/refine-cosio-d20-tscan-resumed')" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" }, "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.14.6" } }, "nbformat": 4, "nbformat_minor": 5 }