{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:36:22.045830Z", "iopub.status.busy": "2026-06-30T22:36:22.045652Z", "iopub.status.idle": "2026-06-30T22:36:22.049933Z", "shell.execute_reply": "2026-06-30T22:36:22.049021Z" }, "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: Ni, NPD\n", "\n", "This example demonstrates a pair distribution function (PDF) analysis\n", "of Ni, based on data collected from a constant wavelength neutron\n", "powder diffraction experiment.\n", "\n", "The dataset is taken from:\n", "https://github.com/diffpy/cmi_exchange/tree/main/cmi_scripts/fitNiPDF" ] }, { "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:36:22.051562Z", "iopub.status.busy": "2026-06-30T22:36:22.051417Z", "iopub.status.idle": "2026-06-30T22:36:24.911894Z", "shell.execute_reply": "2026-06-30T22:36:24.910994Z" } }, "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:36:24.913901Z", "iopub.status.busy": "2026-06-30T22:36:24.913505Z", "iopub.status.idle": "2026-06-30T22:36:25.136207Z", "shell.execute_reply": "2026-06-30T22:36:25.135333Z" } }, "outputs": [], "source": [ "project = edi.Project(name='ni_pdf')" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "### Add Structure" ] }, { "cell_type": "code", "execution_count": 4, "id": "8", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:36:25.137848Z", "iopub.status.busy": "2026-06-30T22:36:25.137682Z", "iopub.status.idle": "2026-06-30T22:36:25.141469Z", "shell.execute_reply": "2026-06-30T22:36:25.140639Z" } }, "outputs": [], "source": [ "project.structures.create(name='ni')" ] }, { "cell_type": "code", "execution_count": 5, "id": "9", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:36:25.143032Z", "iopub.status.busy": "2026-06-30T22:36:25.142870Z", "iopub.status.idle": "2026-06-30T22:36:25.147638Z", "shell.execute_reply": "2026-06-30T22:36:25.146846Z" } }, "outputs": [], "source": [ "project.structures['ni'].space_group.name_h_m = 'F m -3 m'\n", "project.structures['ni'].space_group.coord_system_code = '1'\n", "project.structures['ni'].cell.length_a = 3.52387\n", "project.structures['ni'].atom_sites.create(\n", " id='Ni',\n", " type_symbol='Ni',\n", " fract_x=0.0,\n", " fract_y=0.0,\n", " fract_z=0.0,\n", " adp_iso=0.5,\n", ")" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "### Display Structure" ] }, { "cell_type": "code", "execution_count": 6, "id": "11", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:36:25.149216Z", "iopub.status.busy": "2026-06-30T22:36:25.149054Z", "iopub.status.idle": "2026-06-30T22:36:25.498983Z", "shell.execute_reply": "2026-06-30T22:36:25.498070Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mStructure 🧩 \u001b[0m\u001b[32m'ni'\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='ni')" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "### Add Experiment" ] }, { "cell_type": "code", "execution_count": 7, "id": "13", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:36:25.500536Z", "iopub.status.busy": "2026-06-30T22:36:25.500367Z", "iopub.status.idle": "2026-06-30T22:36:25.733505Z", "shell.execute_reply": "2026-06-30T22:36:25.732704Z" } }, "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-ni-pdf'\u001b[0m: Ni, PDF\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "βœ… Data \u001b[32m'meas-ni-pdf'\u001b[0m downloaded to \u001b[32m'../../../data/meas-ni-pdf.gr'\u001b[0m\n" ] } ], "source": [ "data_path = edi.download_data('meas-ni-pdf', destination='data')" ] }, { "cell_type": "code", "execution_count": 8, "id": "14", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:36:25.735261Z", "iopub.status.busy": "2026-06-30T22:36:25.735073Z", "iopub.status.idle": "2026-06-30T22:36:26.737993Z", "shell.execute_reply": "2026-06-30T22:36:26.737126Z" } }, "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'pdf'\u001b[0m. Number of data points: \u001b[1;36m9801\u001b[0m.\n" ] } ], "source": [ "project.experiments.add_from_data_path(\n", " name='pdf',\n", " data_path=data_path,\n", " sample_form='powder',\n", " beam_mode='constant wavelength',\n", " radiation_probe='neutron',\n", " scattering_type='total',\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "id": "15", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:36:26.739503Z", "iopub.status.busy": "2026-06-30T22:36:26.739336Z", "iopub.status.idle": "2026-06-30T22:36:26.743665Z", "shell.execute_reply": "2026-06-30T22:36:26.742728Z" } }, "outputs": [], "source": [ "project.experiments['pdf'].linked_structures.create(structure_id='ni', scale=1.0)\n", "project.experiments['pdf'].peak.damp_q = 0\n", "project.experiments['pdf'].peak.broad_q = 0.02\n", "project.experiments['pdf'].peak.cutoff_q = 27.0\n", "project.experiments['pdf'].peak.sharp_delta_1 = 0.0\n", "project.experiments['pdf'].peak.sharp_delta_2 = 2.8\n", "project.experiments['pdf'].peak.damp_particle_diameter = 0" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "## πŸš€ Perform Analysis" ] }, { "cell_type": "markdown", "id": "17", "metadata": {}, "source": [ "### Set Free Parameters" ] }, { "cell_type": "code", "execution_count": 10, "id": "18", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:36:26.745519Z", "iopub.status.busy": "2026-06-30T22:36:26.745212Z", "iopub.status.idle": "2026-06-30T22:36:26.749261Z", "shell.execute_reply": "2026-06-30T22:36:26.748447Z" } }, "outputs": [], "source": [ "project.structures['ni'].cell.length_a.free = True\n", "project.structures['ni'].atom_sites['Ni'].adp_iso.free = True" ] }, { "cell_type": "code", "execution_count": 11, "id": "19", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:36:26.751047Z", "iopub.status.busy": "2026-06-30T22:36:26.750833Z", "iopub.status.idle": "2026-06-30T22:36:26.754546Z", "shell.execute_reply": "2026-06-30T22:36:26.753741Z" } }, "outputs": [], "source": [ "project.experiments['pdf'].linked_structures['ni'].scale.free = True\n", "project.experiments['pdf'].peak.broad_q.free = True\n", "project.experiments['pdf'].peak.sharp_delta_2.free = True" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "### Run Fitting" ] }, { "cell_type": "code", "execution_count": 12, "id": "21", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:36:26.756338Z", "iopub.status.busy": "2026-06-30T22:36:26.756114Z", "iopub.status.idle": "2026-06-30T22:38:15.949311Z", "shell.execute_reply": "2026-06-30T22:38:15.945374Z" } }, "outputs": [ { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function() {\n", " const button = document.getElementById('ed-fit-stop-ec0ce028662d4478b738ca939f6a9ec2-button');\n", " const status = document.getElementById('ed-fit-stop-ec0ce028662d4478b738ca939f6a9ec2-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'pdf'\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
111.79911.16
247.19890.922.2% ↓
3712.63911.16
4916.18220.4575.3% ↓
51221.48220.44
61526.76207.236.0% ↓
71831.98207.23
82137.27207.10
92442.47207.10
102747.68207.10
113052.94207.10
123358.27207.10
133663.63207.10
143968.89207.10
154174.28207.10
164380.14207.10
174586.44207.10
184792.75207.10
194997.80207.10
2052103.35207.10
2154107.10207.10
" ], "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;36m207.10\u001b[0m at iteration \u001b[1;36m45\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)107.10
4πŸ” Iterations51
5πŸ“ Goodness-of-fit (reduced χ²)207.10
6πŸ“ R-factor (Rf, %)9.85
7πŸ“ R-factor squared (RfΒ², %)9.48
8πŸ“ Weighted R-factor (wR, %)9.48
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "πŸ“ˆ Refined parameters:\n" ] }, { "data": { "text/html": [ "
datablockcategoryentryparameterunitsstartvalues.u.change
1nicelllength_aΓ…3.52393.52600.00000.06 % ↑
2niatom_siteNiadp_isoΓ…Β²0.50000.42810.001314.39 % ↓
3pdflinked_structureniscale1.00000.97850.00142.15 % ↓
4pdfpeakbroad_qÅ⁻²0.02000.02200.00019.84 % ↑
5pdfpeaksharp_delta_2Γ…Β²2.80002.55890.04448.61 % ↓
" ], "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" }, { "name": "stdout", "output_type": "stream", "text": [ "⚠️ No parameter pairs with |correlation| >= 0.75 were found. \n" ] } ], "source": [ "project.analysis.fit()\n", "project.display.fit.results()\n", "project.display.fit.correlations(threshold=0.75)" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "### Display Pattern" ] }, { "cell_type": "code", "execution_count": 13, "id": "23", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:15.952972Z", "iopub.status.busy": "2026-06-30T22:38:15.952790Z", "iopub.status.idle": "2026-06-30T22:38:16.071820Z", "shell.execute_reply": "2026-06-30T22:38:16.070907Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.pattern(expt_name='pdf')" ] }, { "cell_type": "markdown", "id": "24", "metadata": {}, "source": [ "## πŸ’Ύ Save Project" ] }, { "cell_type": "code", "execution_count": 14, "id": "25", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:16.074377Z", "iopub.status.busy": "2026-06-30T22:38:16.074217Z", "iopub.status.idle": "2026-06-30T22:38:16.822752Z", "shell.execute_reply": "2026-06-30T22:38:16.822001Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mSaving project πŸ“¦ \u001b[0m\u001b[32m'ni_pdf'\u001b[0m\u001b[1;36m to \u001b[0m\u001b[32m'../../../projects/pdf-ni-npd'\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": [ "β”‚ └── πŸ“„ ni.edi\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”œβ”€β”€ πŸ“ experiments/\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”‚ └── πŸ“„ pdf.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": [ " └── πŸ“„ ni_pdf.html\n" ] } ], "source": [ "project.save_as(dir_path='projects/pdf-ni-npd')" ] } ], "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 }