{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:42.067291Z", "iopub.status.busy": "2026-06-30T22:38:42.067089Z", "iopub.status.idle": "2026-06-30T22:38:42.071755Z", "shell.execute_reply": "2026-06-30T22:38:42.070851Z" }, "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.19.1" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "# Pair Distribution Function: Si, NPD\n", "\n", "This example demonstrates a pair distribution function (PDF) analysis\n", "of Si, based on data collected from a time-of-flight neutron powder\n", "diffraction experiment at NOMAD at SNS." ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## πŸ› οΈ Import Library" ] }, { "cell_type": "code", "execution_count": 2, "id": "3", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:42.073417Z", "iopub.status.busy": "2026-06-30T22:38:42.073243Z", "iopub.status.idle": "2026-06-30T22:38:44.862942Z", "shell.execute_reply": "2026-06-30T22:38:44.861976Z" } }, "outputs": [], "source": [ "import easydiffraction as edi" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## πŸ“¦ Define Project" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "### Create Project" ] }, { "cell_type": "code", "execution_count": 3, "id": "6", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:44.864680Z", "iopub.status.busy": "2026-06-30T22:38:44.864399Z", "iopub.status.idle": "2026-06-30T22:38:45.076323Z", "shell.execute_reply": "2026-06-30T22:38:45.075464Z" } }, "outputs": [], "source": [ "project = edi.Project(name='si_nomad_pdf')" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "### Set Plotting Engine" ] }, { "cell_type": "code", "execution_count": 4, "id": "8", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.078132Z", "iopub.status.busy": "2026-06-30T22:38:45.077958Z", "iopub.status.idle": "2026-06-30T22:38:45.090747Z", "shell.execute_reply": "2026-06-30T22:38:45.089915Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mRendering Plot types\u001b[0m\n" ] }, { "data": { "text/html": [ "
TypeDescription
1*autoEnvironment default rendering_plot engine
2asciichartpyConsole ASCII line charts
3plotlyInteractive browser-based graphing library
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.rendering_plot.show_supported()" ] }, { "cell_type": "code", "execution_count": 5, "id": "9", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.092423Z", "iopub.status.busy": "2026-06-30T22:38:45.092268Z", "iopub.status.idle": "2026-06-30T22:38:45.095375Z", "shell.execute_reply": "2026-06-30T22:38:45.094289Z" } }, "outputs": [], "source": [ "# Set global plot range for plots\n", "project.rendering_plot.plotter.x_max = 40" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "### Add Structure" ] }, { "cell_type": "code", "execution_count": 6, "id": "11", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.096870Z", "iopub.status.busy": "2026-06-30T22:38:45.096712Z", "iopub.status.idle": "2026-06-30T22:38:45.099804Z", "shell.execute_reply": "2026-06-30T22:38:45.099104Z" } }, "outputs": [], "source": [ "project.structures.create(name='si')" ] }, { "cell_type": "code", "execution_count": 7, "id": "12", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.101548Z", "iopub.status.busy": "2026-06-30T22:38:45.101385Z", "iopub.status.idle": "2026-06-30T22:38:45.106382Z", "shell.execute_reply": "2026-06-30T22:38:45.105574Z" } }, "outputs": [], "source": [ "structure = project.structures['si']\n", "structure.space_group.name_h_m.value = 'F d -3 m'\n", "structure.space_group.coord_system_code = '1'\n", "structure.cell.length_a = 5.43146\n", "structure.atom_sites.create(\n", " id='Si',\n", " type_symbol='Si',\n", " fract_x=0,\n", " fract_y=0,\n", " fract_z=0,\n", " adp_iso=0.5,\n", ")" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "### Display Structure" ] }, { "cell_type": "code", "execution_count": 8, "id": "14", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.108136Z", "iopub.status.busy": "2026-06-30T22:38:45.107900Z", "iopub.status.idle": "2026-06-30T22:38:45.409032Z", "shell.execute_reply": "2026-06-30T22:38:45.408126Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mStructure 🧩 \u001b[0m\u001b[32m'si'\u001b[0m\u001b[1;36m \u001b[0m\u001b[1;36m(\u001b[0m\u001b[1;36mAtom view type: \u001b[0m\u001b[32m'covalent'\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='si')" ] }, { "cell_type": "markdown", "id": "15", "metadata": {}, "source": [ "### Add Experiment" ] }, { "cell_type": "code", "execution_count": 9, "id": "16", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.410690Z", "iopub.status.busy": "2026-06-30T22:38:45.410465Z", "iopub.status.idle": "2026-06-30T22:38:45.419221Z", "shell.execute_reply": "2026-06-30T22:38:45.418375Z" } }, "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'meas-si-pdf-nomad'\u001b[0m: Si, NOMAD \u001b[1m(\u001b[0mSNS\u001b[1m)\u001b[0m, PDF\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "βœ… Data \u001b[32m'meas-si-pdf-nomad'\u001b[0m already present at \u001b[32m'../../../data/meas-si-pdf-nomad.gr'\u001b[0m. Keeping existing.\n" ] } ], "source": [ "data_path = edi.download_data('meas-si-pdf-nomad', destination='data')" ] }, { "cell_type": "code", "execution_count": 10, "id": "17", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.420813Z", "iopub.status.busy": "2026-06-30T22:38:45.420603Z", "iopub.status.idle": "2026-06-30T22:38:45.960841Z", "shell.execute_reply": "2026-06-30T22:38:45.960108Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "⚠️ No uncertainty (sy) column provided. Defaulting to 0.03. \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mData loaded successfully\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Experiment πŸ”¬ \u001b[32m'nomad'\u001b[0m. Number of data points: \u001b[1;36m5033\u001b[0m.\n" ] } ], "source": [ "project.experiments.add_from_data_path(\n", " name='nomad',\n", " data_path=data_path,\n", " sample_form='powder',\n", " beam_mode='time-of-flight',\n", " radiation_probe='neutron',\n", " scattering_type='total',\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "id": "18", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.962552Z", "iopub.status.busy": "2026-06-30T22:38:45.962396Z", "iopub.status.idle": "2026-06-30T22:38:45.966379Z", "shell.execute_reply": "2026-06-30T22:38:45.965620Z" } }, "outputs": [], "source": [ "experiment = project.experiments['nomad']\n", "experiment.linked_structures.create(structure_id='si', scale=1.0)\n", "experiment.peak.damp_q = 0.02\n", "experiment.peak.broad_q = 0.03\n", "experiment.peak.cutoff_q = 35.0\n", "experiment.peak.sharp_delta_1 = 0.0\n", "experiment.peak.sharp_delta_2 = 4.0\n", "experiment.peak.damp_particle_diameter = 0" ] }, { "cell_type": "markdown", "id": "19", "metadata": {}, "source": [ "## πŸš€ Perform Analysis" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "### Set Free Parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "21", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.967782Z", "iopub.status.busy": "2026-06-30T22:38:45.967630Z", "iopub.status.idle": "2026-06-30T22:38:45.970691Z", "shell.execute_reply": "2026-06-30T22:38:45.969926Z" } }, "outputs": [], "source": [ "project.structures['si'].cell.length_a.free = True\n", "project.structures['si'].atom_sites['Si'].adp_iso.free = True\n", "experiment.linked_structures['si'].scale.free = True" ] }, { "cell_type": "code", "execution_count": 13, "id": "22", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.972033Z", "iopub.status.busy": "2026-06-30T22:38:45.971892Z", "iopub.status.idle": "2026-06-30T22:38:45.975032Z", "shell.execute_reply": "2026-06-30T22:38:45.974114Z" } }, "outputs": [], "source": [ "experiment.peak.damp_q.free = True\n", "experiment.peak.broad_q.free = True\n", "experiment.peak.sharp_delta_1.free = True\n", "experiment.peak.sharp_delta_2.free = True" ] }, { "cell_type": "markdown", "id": "23", "metadata": {}, "source": [ "### Run Fitting" ] }, { "cell_type": "code", "execution_count": 14, "id": "24", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:45.976478Z", "iopub.status.busy": "2026-06-30T22:38:45.976329Z", "iopub.status.idle": "2026-06-30T22:39:02.439977Z", "shell.execute_reply": "2026-06-30T22:39:02.439018Z" } }, "outputs": [ { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function() {\n", " const button = document.getElementById('ed-fit-stop-34ad2e41147e45bdb5d42f3eaf9ee3a5-button');\n", " const status = document.getElementById('ed-fit-stop-34ad2e41147e45bdb5d42f3eaf9ee3a5-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": [ "\u001b[1;36mStandard fitting\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "πŸ“‹ Using experiment πŸ”¬ \u001b[32m'nomad'\u001b[0m for \u001b[32m'single'\u001b[0m fitting\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "πŸš€ Starting fit process with \u001b[32m'lmfit \u001b[0m\u001b[32m(\u001b[0m\u001b[32mleastsq\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m\u001b[33m...\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "πŸ“ˆ Goodness-of-fit progress:\n" ] }, { "data": { "text/html": [ "
iterationtime (s)χ²change / status
110.263102.63
2112.671679.1045.9% ↓
3194.45326.0480.6% ↓
4276.33178.3745.3% ↓
5358.22170.584.4% ↓
65713.43170.54
76816.11170.54
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "πŸ† Best goodness-of-fit \u001b[1m(\u001b[0mreduced χ²\u001b[1m)\u001b[0m is \u001b[1;36m170.54\u001b[0m at iteration \u001b[1;36m67\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "βœ… Fitting complete.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "βš™οΈ Settings used:\n" ] }, { "data": { "text/html": [ "
NameValueDescription
1max_iterations1000Maximum solver iterations.
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "πŸ“‹ Least-squares fit results:\n" ] }, { "data": { "text/html": [ "
MetricValue
1πŸ§ͺ Minimizerlmfit (leastsq)
2βœ… Overall statussuccess
3⏱️ Fitting time (seconds)16.11
4πŸ” Iterations65
5πŸ“ Goodness-of-fit (reduced χ²)170.54
6πŸ“ R-factor (Rf, %)8.40
7πŸ“ R-factor squared (RfΒ², %)8.30
8πŸ“ Weighted R-factor (wR, %)8.30
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "πŸ“ˆ Refined parameters:\n" ] }, { "data": { "text/html": [ "
datablockcategoryentryparameterunitsstartvalues.u.change
1sicelllength_aΓ…5.43155.43060.00000.02 % ↓
2siatom_siteSiadp_isoΓ…Β²0.50000.71700.005543.40 % ↑
3nomadlinked_structuresiscale1.00001.61990.003761.99 % ↑
4nomadpeakdamp_qÅ⁻¹0.02000.02510.000125.66 % ↑
5nomadpeakbroad_qÅ⁻²0.03000.01830.000339.10 % ↓
6nomadpeaksharp_delta_1Γ…0.00002.54010.0529N/A
7nomadpeaksharp_delta_2Γ…Β²4.0000-1.75250.1244143.81 % ↓
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
β€’ start = parameter value before refinement
β€’ value = refined value from least-squares minimization
β€’ s.u. = standard uncertainty (one sigma), from the covariance matrix
β€’ change = relative change from start, in %; ↑ = increase, ↓ = decrease
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n", "
\n", "
Loading plot…
\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.analysis.fit()\n", "project.display.fit.results()\n", "project.display.fit.correlations()" ] }, { "cell_type": "markdown", "id": "25", "metadata": {}, "source": [ "### Display Pattern" ] }, { "cell_type": "code", "execution_count": 15, "id": "26", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:39:02.441763Z", "iopub.status.busy": "2026-06-30T22:39:02.441598Z", "iopub.status.idle": "2026-06-30T22:39:02.489349Z", "shell.execute_reply": "2026-06-30T22:39:02.488384Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.pattern(expt_name='nomad')" ] }, { "cell_type": "markdown", "id": "27", "metadata": {}, "source": [ "## πŸ’Ύ Save Project" ] }, { "cell_type": "code", "execution_count": 16, "id": "28", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:39:02.491567Z", "iopub.status.busy": "2026-06-30T22:39:02.491319Z", "iopub.status.idle": "2026-06-30T22:39:02.876254Z", "shell.execute_reply": "2026-06-30T22:39:02.875439Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mSaving project πŸ“¦ \u001b[0m\u001b[32m'si_nomad_pdf'\u001b[0m\u001b[1;36m to \u001b[0m\u001b[32m'../../../projects/pdf-si-nomad'\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”œβ”€β”€ πŸ“„ project.edi\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”œβ”€β”€ πŸ“ structures/\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”‚ └── πŸ“„ si.edi\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”œβ”€β”€ πŸ“ experiments/\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”‚ └── πŸ“„ nomad.edi\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”œβ”€β”€ πŸ“ analysis/\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”‚ └── πŸ“„ analysis.edi\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "└── πŸ“ reports/\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " └── πŸ“„ si_nomad_pdf.html\n" ] } ], "source": [ "project.save_as(dir_path='projects/pdf-si-nomad')" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }