{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:32:57.794670Z", "iopub.status.busy": "2026-06-30T22:32:57.794489Z", "iopub.status.idle": "2026-06-30T22:32:57.798760Z", "shell.execute_reply": "2026-06-30T22:32:57.797998Z" }, "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": [ "# Na₂Ca₃Al₂F₁₄ — powder neutron TOF — Jorgensen-Von Dreele (Gaussian)\n", "\n", "Verifies the Jorgensen-Von Dreele pseudo-Voigt profile for the NCAF\n", "time-of-flight powder reference, with the Lorentzian terms (γ₀, γ₁,\n", "γ₂) forced to zero so it reduces to the Gaussian case.\n", "\n", "**Refinement:** the overall scale only; all other parameters are\n", "taken from the FullProf reference." ] }, { "cell_type": "code", "execution_count": 2, "id": "2", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:32:57.800755Z", "iopub.status.busy": "2026-06-30T22:32:57.800589Z", "iopub.status.idle": "2026-06-30T22:33:00.661525Z", "shell.execute_reply": "2026-06-30T22:33:00.660620Z" } }, "outputs": [], "source": [ "import easydiffraction as edi\n", "from easydiffraction import ExperimentFactory\n", "from easydiffraction import StructureFactory\n", "from easydiffraction.analysis import verification as verify" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "## Build the project" ] }, { "cell_type": "code", "execution_count": 3, "id": "4", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:33:00.663752Z", "iopub.status.busy": "2026-06-30T22:33:00.663442Z", "iopub.status.idle": "2026-06-30T22:33:00.877841Z", "shell.execute_reply": "2026-06-30T22:33:00.876968Z" } }, "outputs": [], "source": [ "project = edi.Project()" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "## Define the structure" ] }, { "cell_type": "code", "execution_count": 4, "id": "6", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:33:00.879727Z", "iopub.status.busy": "2026-06-30T22:33:00.879545Z", "iopub.status.idle": "2026-06-30T22:33:00.889859Z", "shell.execute_reply": "2026-06-30T22:33:00.889049Z" } }, "outputs": [], "source": [ "structure = StructureFactory.from_scratch(name='ncaf')\n", "structure.space_group.name_h_m = 'I 21 3' # FullProf Space group symbol\n", "structure.cell.length_a = 10.250256 # FullProf a\n", "structure.atom_sites.create(\n", " id='Ca', # FullProf Atom\n", " type_symbol='Ca', # FullProf Typ\n", " fract_x=0.46610, # FullProf X\n", " fract_y=0.0, # FullProf Y\n", " fract_z=0.25, # FullProf Z\n", " adp_type='Biso', # FullProf Biso\n", " adp_iso=0.88721, # FullProf Biso\n", ")\n", "structure.atom_sites.create(\n", " id='Al', # FullProf Atom\n", " type_symbol='Al', # FullProf Typ\n", " fract_x=0.25163, # FullProf X\n", " fract_y=0.25163, # FullProf Y\n", " fract_z=0.25163, # FullProf Z\n", " adp_type='Biso', # FullProf Biso\n", " adp_iso=0.65230, # FullProf Biso\n", ")\n", "structure.atom_sites.create(\n", " id='Na', # FullProf Atom\n", " type_symbol='Na', # FullProf Typ\n", " fract_x=0.08472, # FullProf X\n", " fract_y=0.08472, # FullProf Y\n", " fract_z=0.08472, # FullProf Z\n", " adp_type='Biso', # FullProf Biso\n", " adp_iso=1.89168, # FullProf Biso\n", ")\n", "structure.atom_sites.create(\n", " id='F1', # FullProf Atom\n", " type_symbol='F', # FullProf Typ\n", " fract_x=0.13748, # FullProf X\n", " fract_y=0.30533, # FullProf Y\n", " fract_z=0.11947, # FullProf Z\n", " adp_type='Biso', # FullProf Biso\n", " adp_iso=0.89535, # FullProf Biso\n", ")\n", "structure.atom_sites.create(\n", " id='F2', # FullProf Atom\n", " type_symbol='F', # FullProf Typ\n", " fract_x=0.36263, # FullProf X\n", " fract_y=0.36333, # FullProf Y\n", " fract_z=0.18669, # FullProf Z\n", " adp_type='Biso', # FullProf Biso\n", " adp_iso=1.27175, # FullProf Biso\n", ")\n", "structure.atom_sites.create(\n", " id='F3', # FullProf Atom\n", " type_symbol='F', # FullProf Typ\n", " fract_x=0.46120, # FullProf X\n", " fract_y=0.46120, # FullProf Y\n", " fract_z=0.46120, # FullProf Z\n", " adp_type='Biso', # FullProf Biso\n", " adp_iso=0.78029, # FullProf Biso\n", ")\n", "\n", "project.structures.add(structure)" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "## Load the FullProf reference" ] }, { "cell_type": "code", "execution_count": 5, "id": "8", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:33:00.891557Z", "iopub.status.busy": "2026-06-30T22:33:00.891336Z", "iopub.status.idle": "2026-06-30T22:33:00.907742Z", "shell.execute_reply": "2026-06-30T22:33:00.906849Z" } }, "outputs": [], "source": [ "FULLPROF_PROJECT_DIR = 'pd-neut-tof_ncaf_jorgensen-von-dreele'\n", "FULLPROF_PRF_FILE = 'tmpl_one_bank.prf'\n", "FULLPROF_SUM_FILE = 'tmpl_one_bank.sum'\n", "FULLPROF_BAC_FILE = 'tmpl_one_bank.bac'\n", "FULLPROF_LABEL = verify.fullprof_label(FULLPROF_PROJECT_DIR, FULLPROF_SUM_FILE)\n", "\n", "FULLPROF_ZERO = -13.88128 # FullProf Zero\n", "FULLPROF_SCALE = 4.019304 # FullProf Scale\n", "FULLPROF_TWOTHETA_BANK = 152.827 # FullProf 2ThetaBank\n", "FULLPROF_DTT1 = 20773.12305 # FullProf Dtt1\n", "FULLPROF_DTT2 = -1.08308 # FullProf Dtt2\n", "FULLPROF_SIGMA_0 = 0.0 # FullProf Sigma-0\n", "FULLPROF_SIGMA_1 = 0.0 # FullProf Sigma-1\n", "FULLPROF_SIGMA_2 = 15.6959 # FullProf Sigma-2\n", "FULLPROF_GAMMA_0 = 0.0 # FullProf Gamma-0\n", "FULLPROF_GAMMA_1 = 0.0 # FullProf Gamma-1\n", "FULLPROF_GAMMA_2 = 0.0 # FullProf Gamma-2\n", "FULLPROF_ALPHA_0 = -0.009276 # FullProf alph0\n", "FULLPROF_ALPHA_1 = 0.109622 # FullProf alph1\n", "FULLPROF_BETA_0 = 0.006705 # FullProf beta0\n", "FULLPROF_BETA_1 = 0.009708 # FullProf beta1\n", "FULLPROF_WDT = 40.0 # FullProf Wdt\n", "\n", "x, calc_fullprof = verify.load_fullprof_calc_profile(\n", " FULLPROF_PROJECT_DIR,\n", " FULLPROF_PRF_FILE,\n", " FULLPROF_BAC_FILE,\n", " FULLPROF_ZERO,\n", ")" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "## Create the experiment" ] }, { "cell_type": "code", "execution_count": 6, "id": "10", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:33:00.909623Z", "iopub.status.busy": "2026-06-30T22:33:00.909329Z", "iopub.status.idle": "2026-06-30T22:33:01.482118Z", "shell.execute_reply": "2026-06-30T22:33:01.481389Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "⚠️ Switching peak profile type adds these settings with defaults: \n", " • broad_lorentz_gamma_0=0.0 \n", " • broad_lorentz_gamma_1=0.0 \n", " • broad_lorentz_gamma_2=0.0 \n", " • broad_lorentz_size_l=0.0 \n", " • broad_lorentz_strain_l=0.0 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mPeak profile type for experiment \u001b[0m\u001b[32m'ncaf'\u001b[0m\u001b[1;36m changed to\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "jorgensen-von-dreele\n" ] } ], "source": [ "experiment = ExperimentFactory.from_scratch(\n", " name='ncaf',\n", " sample_form='powder',\n", " beam_mode='time-of-flight',\n", " radiation_probe='neutron',\n", " scattering_type='bragg',\n", ")\n", "verify.set_reference_as_measured(experiment, x, calc_fullprof)\n", "\n", "experiment.linked_structures.create(structure_id='ncaf', scale=FULLPROF_SCALE)\n", "\n", "experiment.instrument.setup_twotheta_bank = FULLPROF_TWOTHETA_BANK\n", "experiment.instrument.calib_d_to_tof_offset = FULLPROF_ZERO\n", "experiment.instrument.calib_d_to_tof_linear = FULLPROF_DTT1\n", "experiment.instrument.calib_d_to_tof_quadratic = FULLPROF_DTT2\n", "\n", "experiment.peak.type = 'jorgensen-von-dreele'\n", "experiment.peak.broad_gauss_sigma_0 = FULLPROF_SIGMA_0\n", "experiment.peak.broad_gauss_sigma_1 = FULLPROF_SIGMA_1\n", "experiment.peak.broad_gauss_sigma_2 = FULLPROF_SIGMA_2\n", "experiment.peak.broad_lorentz_gamma_0 = FULLPROF_GAMMA_0\n", "experiment.peak.broad_lorentz_gamma_1 = FULLPROF_GAMMA_1\n", "experiment.peak.broad_lorentz_gamma_2 = FULLPROF_GAMMA_2\n", "experiment.peak.rise_alpha_0 = FULLPROF_ALPHA_0\n", "experiment.peak.rise_alpha_1 = FULLPROF_ALPHA_1\n", "experiment.peak.decay_beta_0 = FULLPROF_BETA_0\n", "experiment.peak.decay_beta_1 = FULLPROF_BETA_1\n", "\n", "experiment.excluded_regions.create(id='1', start=0, end=30000)\n", "experiment.excluded_regions.create(id='2', start=50000, end=200000)\n", "\n", "experiment.peak.cutoff_fwhm = FULLPROF_WDT\n", "\n", "project.experiments.add(experiment)" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "## edi-cryspy VS FullProf" ] }, { "cell_type": "code", "execution_count": 7, "id": "12", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:33:01.483852Z", "iopub.status.busy": "2026-06-30T22:33:01.483687Z", "iopub.status.idle": "2026-06-30T22:33:01.972481Z", "shell.execute_reply": "2026-06-30T22:33:01.971602Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mCalculator for experiment \u001b[0m\u001b[32m'ncaf'\u001b[0m\u001b[1;36m already set to\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "cryspy\n" ] }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "experiment.calculator.type = 'cryspy'\n", "\n", "experiment.linked_structures['ncaf'].scale = FULLPROF_SCALE\n", "\n", "project.analysis.calculate()\n", "calc_ed_cryspy = experiment.data.intensity_calc\n", "LABEL_ED_CRYSPY = verify.engine_label('cryspy')\n", "\n", "project.display.pattern_comparison(\n", " 'ncaf',\n", " reference=calc_fullprof,\n", " candidate=calc_ed_cryspy,\n", " reference_label=FULLPROF_LABEL,\n", " candidate_label=LABEL_ED_CRYSPY,\n", ")" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "## Fit edi-cryspy to FullProf" ] }, { "cell_type": "code", "execution_count": 8, "id": "14", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:33:01.974632Z", "iopub.status.busy": "2026-06-30T22:33:01.974460Z", "iopub.status.idle": "2026-06-30T22:33:03.420325Z", "shell.execute_reply": "2026-06-30T22:33:03.419346Z" } }, "outputs": [ { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function() {\n", " const button = document.getElementById('ed-fit-stop-4c3293fca7a34ca984bafe3edc2eaa0f-button');\n", " const status = document.getElementById('ed-fit-stop-4c3293fca7a34ca984bafe3edc2eaa0f-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'ncaf'\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.092235878.98
250.403.39100.0% ↓
380.653.39
" ], "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;36m3.39\u001b[0m at iteration \u001b[1;36m5\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)0.65
4🔁 Iterations5
5📏 Goodness-of-fit (reduced χ²)3.39
6📏 R-factor (Rf, %)0.18
7📏 R-factor squared (Rf², %)0.12
8📏 Weighted R-factor (wR, %)0.12
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "📈 Refined parameters:\n" ] }, { "data": { "text/html": [ "
datablockcategoryentryparameterunitsstartvalues.u.change
1ncaflinked_structurencafscale4.019377.01220.00321816.06 % ↑
" ], "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": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# experiment.linked_structures['ncaf'].scale = 1.0927822317965166\n", "experiment.linked_structures['ncaf'].scale.free = True\n", "\n", "project.analysis.fit()\n", "project.display.fit.results()\n", "\n", "project.analysis.calculate()\n", "calc_ed_cryspy_refined = experiment.data.intensity_calc\n", "LABEL_ED_CRYSPY_REFINED = verify.engine_label('cryspy', note='refined')\n", "\n", "project.display.pattern_comparison(\n", " 'ncaf',\n", " reference=calc_fullprof,\n", " candidate=calc_ed_cryspy_refined,\n", " reference_label=FULLPROF_LABEL,\n", " candidate_label=LABEL_ED_CRYSPY_REFINED,\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "id": "15", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:33:03.421940Z", "iopub.status.busy": "2026-06-30T22:33:03.421768Z", "iopub.status.idle": "2026-06-30T22:33:03.426200Z", "shell.execute_reply": "2026-06-30T22:33:03.425356Z" } }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "experiment.linked_structures['ncaf'].scale" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "## Agreement check" ] }, { "cell_type": "code", "execution_count": 10, "id": "17", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:33:03.427774Z", "iopub.status.busy": "2026-06-30T22:33:03.427579Z", "iopub.status.idle": "2026-06-30T22:33:03.436295Z", "shell.execute_reply": "2026-06-30T22:33:03.435360Z" } }, "outputs": [ { "data": { "text/html": [ "
ComparisonMetricExpectedActualOK
1edi 0.19.1 (cryspy 0.12.1, refined) vs FullProf 8.40Profile diff (%)< 2.50.12
2Max deviation (%)< 60.12
3Area ratio0.99 to 1.011.0008
4Shape correlation> 0.9991.0000
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# cryspy matches FullProf after refining scale, so it is gated as a\n", "# regression test.\n", "verify.assert_patterns_agree([\n", " (\n", " f'{LABEL_ED_CRYSPY_REFINED} vs {FULLPROF_LABEL}',\n", " verify.restrict_to_included(experiment, calc_fullprof),\n", " calc_ed_cryspy_refined,\n", " ),\n", "])" ] } ], "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 }