{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "0",
"metadata": {
"execution": {
"iopub.execute_input": "2026-06-30T22:32:04.776197Z",
"iopub.status.busy": "2026-06-30T22:32:04.775981Z",
"iopub.status.idle": "2026-06-30T22:32:04.780869Z",
"shell.execute_reply": "2026-06-30T22:32:04.780125Z"
},
"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": [
"# LBCO Resume\n",
"\n",
"This is the most minimal example of using EasyDiffraction. It shows\n",
"how to load a previously saved project from a directory and run\n",
"refinement β all in just a few lines of code.\n",
"\n",
"For this example, constant-wavelength neutron powder diffraction data\n",
"for La0.5Ba0.5CoO3 from HRPT at PSI is used.\n",
"\n",
"It does not contain any advanced features or options, and includes no\n",
"comments or explanations β these can be found in the other tutorials."
]
},
{
"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:32:04.783474Z",
"iopub.status.busy": "2026-06-30T22:32:04.783269Z",
"iopub.status.idle": "2026-06-30T22:32:08.993946Z",
"shell.execute_reply": "2026-06-30T22:32:08.993001Z"
}
},
"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 project from the EasyDiffraction data\n",
"repository."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6",
"metadata": {
"execution": {
"iopub.execute_input": "2026-06-30T22:32:08.996735Z",
"iopub.status.busy": "2026-06-30T22:32:08.996353Z",
"iopub.status.idle": "2026-06-30T22:32:09.223550Z",
"shell.execute_reply": "2026-06-30T22:32:09.222396Z"
}
},
"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-lbco-hrpt'\u001b[0m: La0.5Ba0.5CoO3, HRPT \u001b[1m(\u001b[0mPSI\u001b[1m)\u001b[0m, \u001b[1;36m300\u001b[0m K\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"β
Data \u001b[32m'proj-lbco-hrpt'\u001b[0m downloaded and extracted to \u001b[32m'../../../projects/proj-lbco-hrpt-69567a192590'\u001b[0m\n"
]
}
],
"source": [
"project_dir = edi.download_data('proj-lbco-hrpt', destination='projects')"
]
},
{
"cell_type": "markdown",
"id": "7",
"metadata": {},
"source": [
"### Load Project"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8",
"metadata": {
"execution": {
"iopub.execute_input": "2026-06-30T22:32:09.225591Z",
"iopub.status.busy": "2026-06-30T22:32:09.225359Z",
"iopub.status.idle": "2026-06-30T22:32:12.678514Z",
"shell.execute_reply": "2026-06-30T22:32:12.676976Z"
}
},
"outputs": [],
"source": [
"project = edi.Project.load(project_dir)"
]
},
{
"cell_type": "markdown",
"id": "9",
"metadata": {},
"source": [
"Re-save the project to a fresh working directory so fitting below\n",
"writes there instead of the bundled read-only copy."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "10",
"metadata": {
"execution": {
"iopub.execute_input": "2026-06-30T22:32:12.680731Z",
"iopub.status.busy": "2026-06-30T22:32:12.680487Z",
"iopub.status.idle": "2026-06-30T22:32:13.330117Z",
"shell.execute_reply": "2026-06-30T22:32:13.328850Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;36mSaving project π¦ \u001b[0m\u001b[32m'lbco_hrpt'\u001b[0m\u001b[1;36m to \u001b[0m\u001b[32m'../../../projects/load-and-fit-lbco-hrpt'\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": [
"β βββ π lbco.edi\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"βββ π experiments/\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"β βββ π hrpt.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": [
" βββ π lbco_hrpt.html\n"
]
}
],
"source": [
"project.save_as(dir_path='projects/load-and-fit-lbco-hrpt')"
]
},
{
"cell_type": "markdown",
"id": "11",
"metadata": {},
"source": [
"## π Perform Analysis"
]
},
{
"cell_type": "markdown",
"id": "12",
"metadata": {},
"source": [
"### Display Structure"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "13",
"metadata": {
"execution": {
"iopub.execute_input": "2026-06-30T22:32:13.332107Z",
"iopub.status.busy": "2026-06-30T22:32:13.331872Z",
"iopub.status.idle": "2026-06-30T22:32:13.395335Z",
"shell.execute_reply": "2026-06-30T22:32:13.394279Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;36mStructure π§© \u001b[0m\u001b[32m'lbco'\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='lbco')"
]
},
{
"cell_type": "markdown",
"id": "14",
"metadata": {},
"source": [
"### Run Fitting"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "15",
"metadata": {
"execution": {
"iopub.execute_input": "2026-06-30T22:32:13.397563Z",
"iopub.status.busy": "2026-06-30T22:32:13.397348Z",
"iopub.status.idle": "2026-06-30T22:32:16.473163Z",
"shell.execute_reply": "2026-06-30T22:32:16.472512Z"
}
},
"outputs": [
{
"data": {
"text/html": [],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"\n",
"(function() {\n",
" const button = document.getElementById('ed-fit-stop-e3fe595eb096456488870ce84f1b341b-button');\n",
" const status = document.getElementById('ed-fit-stop-e3fe595eb096456488870ce84f1b341b-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'hrpt'\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 | 0.08 | 1.27 | |
|---|
| 2 | 45 | 2.49 | 1.26 | |
|---|
"
],
"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;36m1.26\u001b[0m at iteration \u001b[1;36m24\u001b[0m\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"β
Fitting complete.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;36mSaving project π¦ \u001b[0m\u001b[32m'lbco_hrpt'\u001b[0m\u001b[1;36m to \u001b[0m\u001b[32m'../../../projects/load-and-fit-lbco-hrpt'\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": [
"β βββ π lbco.edi\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"βββ π experiments/\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"β βββ π hrpt.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": [
" βββ π lbco_hrpt.html\n"
]
}
],
"source": [
"project.analysis.fit()"
]
},
{
"cell_type": "markdown",
"id": "16",
"metadata": {},
"source": [
"### Display Fit Results"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "17",
"metadata": {
"execution": {
"iopub.execute_input": "2026-06-30T22:32:16.475364Z",
"iopub.status.busy": "2026-06-30T22:32:16.475138Z",
"iopub.status.idle": "2026-06-30T22:32:16.558321Z",
"shell.execute_reply": "2026-06-30T22:32:16.557224Z"
}
},
"outputs": [
{
"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 |
|---|
| 2 | β
Overall status | success |
|---|
| 3 | β±οΈ Fitting time (seconds) | 2.49 |
|---|
| 4 | π Iterations | 42 |
|---|
| 5 | π Goodness-of-fit (reduced ΟΒ²) | 1.26 |
|---|
| 6 | π R-factor (Rf, %) | 5.57 |
|---|
| 7 | π R-factor squared (RfΒ², %) | 5.26 |
|---|
| 8 | π Weighted R-factor (wR, %) | 4.46 |
|---|
"
],
"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 | lbco | cell | | length_a | Γ
| 3.8908 | 3.8908 | 0.0000 | 0.00 % β |
|---|
| 2 | lbco | atom_site | La | adp_iso | Γ
Β² | 0.4920 | 0.4634 | 0.0268 | 5.82 % β |
|---|
| 3 | lbco | atom_site | Co | adp_iso | Γ
Β² | 0.2490 | 0.2924 | 0.0482 | 17.41 % β |
|---|
| 4 | lbco | atom_site | O | adp_iso | Γ
Β² | 1.4040 | 1.4063 | 0.0159 | 0.17 % β |
|---|
| 5 | hrpt | linked_structure | lbco | scale | | 9.1280 | 9.1076 | 0.0548 | 0.22 % β |
|---|
| 6 | hrpt | peak | | broad_gauss_u | degΒ² | 0.0803 | 0.0807 | 0.0031 | 0.44 % β |
|---|
| 7 | hrpt | peak | | broad_gauss_v | degΒ² | -0.1125 | -0.1138 | 0.0067 | 1.13 % β |
|---|
| 8 | hrpt | peak | | broad_gauss_w | degΒ² | 0.1195 | 0.1197 | 0.0032 | 0.17 % β |
|---|
| 9 | hrpt | peak | | broad_lorentz_y | deg | 0.0836 | 0.0842 | 0.0021 | 0.75 % β |
|---|
| 10 | hrpt | instrument | | twotheta_offset | deg | 0.6205 | 0.6194 | 0.0008 | 0.17 % β |
|---|
| 11 | hrpt | background | 1 | intensity | | 166.9200 | 166.9137 | 0.8922 | 0.00 % β |
|---|
| 12 | hrpt | background | 2 | intensity | | 167.6100 | 167.6764 | 0.6899 | 0.04 % β |
|---|
| 13 | hrpt | background | 3 | intensity | | 172.8800 | 172.6639 | 0.7114 | 0.13 % β |
|---|
| 14 | hrpt | background | 4 | intensity | | 180.9200 | 181.0553 | 0.8746 | 0.07 % β |
|---|
| 15 | hrpt | background | 5 | intensity | | 168.7000 | 162.6311 | 1.2441 | 3.60 % β |
|---|
"
],
"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"
}
],
"source": [
"project.display.fit.results()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "18",
"metadata": {
"execution": {
"iopub.execute_input": "2026-06-30T22:32:16.560177Z",
"iopub.status.busy": "2026-06-30T22:32:16.559934Z",
"iopub.status.idle": "2026-06-30T22:32:16.592773Z",
"shell.execute_reply": "2026-06-30T22:32:16.591971Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"project.display.fit.correlations()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "19",
"metadata": {
"execution": {
"iopub.execute_input": "2026-06-30T22:32:16.594428Z",
"iopub.status.busy": "2026-06-30T22:32:16.594261Z",
"iopub.status.idle": "2026-06-30T22:32:16.664262Z",
"shell.execute_reply": "2026-06-30T22:32:16.663400Z"
}
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"project.display.pattern(expt_name='hrpt')"
]
},
{
"cell_type": "markdown",
"id": "20",
"metadata": {},
"source": [
"## πΎ Save Project"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "21",
"metadata": {
"execution": {
"iopub.execute_input": "2026-06-30T22:32:16.668161Z",
"iopub.status.busy": "2026-06-30T22:32:16.667974Z",
"iopub.status.idle": "2026-06-30T22:32:17.135525Z",
"shell.execute_reply": "2026-06-30T22:32:17.134870Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;36mSaving project π¦ \u001b[0m\u001b[32m'lbco_hrpt'\u001b[0m\u001b[1;36m to \u001b[0m\u001b[32m'../../../projects/load-and-fit-lbco-hrpt'\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": [
"β βββ π lbco.edi\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"βββ π experiments/\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"β βββ π hrpt.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": [
" βββ π lbco_hrpt.html\n"
]
}
],
"source": [
"project.save_as(dir_path='projects/load-and-fit-lbco-hrpt')"
]
}
],
"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
}