{
"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": [
" | iteration | time (s) | ΟΒ² | change / status |
|---|
| 1 | 1 | 1.79 | 911.16 | |
|---|
| 2 | 4 | 7.19 | 890.92 | 2.2% β |
|---|
| 3 | 7 | 12.63 | 911.16 | |
|---|
| 4 | 9 | 16.18 | 220.45 | 75.3% β |
|---|
| 5 | 12 | 21.48 | 220.44 | |
|---|
| 6 | 15 | 26.76 | 207.23 | 6.0% β |
|---|
| 7 | 18 | 31.98 | 207.23 | |
|---|
| 8 | 21 | 37.27 | 207.10 | |
|---|
| 9 | 24 | 42.47 | 207.10 | |
|---|
| 10 | 27 | 47.68 | 207.10 | |
|---|
| 11 | 30 | 52.94 | 207.10 | |
|---|
| 12 | 33 | 58.27 | 207.10 | |
|---|
| 13 | 36 | 63.63 | 207.10 | |
|---|
| 14 | 39 | 68.89 | 207.10 | |
|---|
| 15 | 41 | 74.28 | 207.10 | |
|---|
| 16 | 43 | 80.14 | 207.10 | |
|---|
| 17 | 45 | 86.44 | 207.10 | |
|---|
| 18 | 47 | 92.75 | 207.10 | |
|---|
| 19 | 49 | 97.80 | 207.10 | |
|---|
| 20 | 52 | 103.35 | 207.10 | |
|---|
| 21 | 54 | 107.10 | 207.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": [
" | Name | Value | Description |
|---|
| 1 | max_iterations | 1000 | Maximum solver iterations. |
|---|
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"π Least-squares fit results:\n"
]
},
{
"data": {
"text/html": [
" | Metric | Value |
|---|
| 1 | π§ͺ Minimizer | lmfit (leastsq) |
|---|
| 2 | β
Overall status | success |
|---|
| 3 | β±οΈ Fitting time (seconds) | 107.10 |
|---|
| 4 | π Iterations | 51 |
|---|
| 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": [
" | datablock | category | entry | parameter | units | start | value | s.u. | change |
|---|
| 1 | ni | cell | | length_a | Γ
| 3.5239 | 3.5260 | 0.0000 | 0.06 % β |
|---|
| 2 | ni | atom_site | Ni | adp_iso | Γ
Β² | 0.5000 | 0.4281 | 0.0013 | 14.39 % β |
|---|
| 3 | pdf | linked_structure | ni | scale | | 1.0000 | 0.9785 | 0.0014 | 2.15 % β |
|---|
| 4 | pdf | peak | | broad_q | Γ
β»Β² | 0.0200 | 0.0220 | 0.0001 | 9.84 % β |
|---|
| 5 | pdf | peak | | sharp_delta_2 | Γ
Β² | 2.8000 | 2.5589 | 0.0444 | 8.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": [
""
],
"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
}