Load Project and Fit: LBCO, HRPT¶
This is the most minimal example of using EasyDiffraction. It shows how to load a previously saved project from a directory and run refinement — all in just a few lines of code.
For this example, constant-wavelength neutron powder diffraction data for La0.5Ba0.5CoO3 from HRPT at PSI is used.
It does not contain any advanced features or options, and includes no comments or explanations — these can be found in the other tutorials.
Import Modules¶
In [2]:
Copied!
from easydiffraction import Project
from easydiffraction import download_data
from easydiffraction import extract_project_from_zip
from easydiffraction import Project
from easydiffraction import download_data
from easydiffraction import extract_project_from_zip
Download Project Archive¶
In [3]:
Copied!
zip_path = download_data(id=30, destination='data')
zip_path = download_data(id=30, destination='data')
Getting data...
Data #30: La0.5Ba0.5CoO3, HRPT (PSI), 300 K - Updated #28
✅ Data #30 downloaded to 'data/ed-30.zip'
Extract Project¶
In [4]:
Copied!
project_dir = extract_project_from_zip(zip_path, destination='data')
project_dir = extract_project_from_zip(zip_path, destination='data')
Load Project¶
In [5]:
Copied!
project = Project.load(project_dir)
project = Project.load(project_dir)
Perform Analysis¶
In [6]:
Copied!
project.analysis.fit()
project.analysis.fit()
Standard fitting
📋 Using experiment 🔬 'hrpt' for 'single' fitting
🚀 Starting fit process with 'lmfit (leastsq)'...
📈 Goodness-of-fit (reduced χ²) change:
| iteration | χ² | improvement [%] | |
|---|---|---|---|
| 1 | 1 | 254.47 | |
| 2 | 21 | 43.04 | 83.1% ↓ |
| 3 | 39 | 36.17 | 16.0% ↓ |
| 4 | 57 | 34.88 | 3.6% ↓ |
| 5 | 76 | 22.76 | 34.8% ↓ |
| 6 | 94 | 17.59 | 22.7% ↓ |
| 7 | 112 | 8.38 | 52.4% ↓ |
| 8 | 130 | 4.57 | 45.5% ↓ |
| 9 | 148 | 2.18 | 52.2% ↓ |
| 10 | 166 | 1.88 | 14.1% ↓ |
| 11 | 184 | 1.53 | 18.5% ↓ |
| 12 | 202 | 1.42 | 7.3% ↓ |
| 13 | 220 | 1.25 | 11.5% ↓ |
| 14 | 329 | 1.25 |
🏆 Best goodness-of-fit (reduced χ²) is 1.25 at iteration 328
✅ Fitting complete.
Saving project 📦 'lbco_hrpt' to
/home/runner/work/diffraction-lib/diffraction-lib/docs/docs/tutorials/data/lbco_project
├── 📄 project.cif
├── 📁 structures/
│ └── 📄 lbco.cif
├── 📁 experiments/
│ └── 📄 hrpt.cif
├── 📁 analysis/
│ └── 📄 analysis.cif
└── 📄 summary.cif
Show Results¶
In [7]:
Copied!
project.analysis.display.fit_results()
project.analysis.display.fit_results()
Fit results
✅ Success: True
⏱️ Fitting time: 18.24 seconds
📏 Goodness-of-fit (reduced χ²): 1.25
📏 R-factor (Rf): 5.48%
📏 R-factor squared (Rf²): 4.94%
📏 Weighted R-factor (wR): 3.97%
📈 Fitted parameters:
| datablock | category | entry | parameter | start | fitted | uncertainty | units | change | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | lbco | cell | length_a | 3.8800 | 3.8914 | 0.0001 | Å | 0.29 % ↑ | |
| 2 | lbco | atom_site | La | adp_iso | 0.5000 | 0.5052 | 0.0273 | Ų | 1.05 % ↑ |
| 3 | lbco | atom_site | Co | adp_iso | 0.5000 | 0.2469 | 0.0558 | Ų | 50.62 % ↓ |
| 4 | lbco | atom_site | O | adp_iso | 1.0000 | 1.3921 | 0.0158 | Ų | 39.21 % ↑ |
| 5 | hrpt | linked_phases | lbco | scale | 7.0000 | 9.1234 | 0.0530 | 30.33 % ↑ | |
| 6 | hrpt | peak | asym_empir_2 | 0.0000 | -0.0177 | 0.0019 | N/A | ||
| 7 | hrpt | peak | asym_empir_4 | 0.0000 | 0.0318 | 0.0041 | N/A | ||
| 8 | hrpt | peak | broad_gauss_u | 0.1000 | 0.0859 | 0.0032 | deg² | 14.11 % ↓ | |
| 9 | hrpt | peak | broad_gauss_v | -0.1000 | -0.1206 | 0.0067 | deg² | 20.57 % ↑ | |
| 10 | hrpt | peak | broad_gauss_w | 0.1000 | 0.1225 | 0.0032 | deg² | 22.49 % ↑ | |
| 11 | hrpt | peak | broad_lorentz_y | 0.0000 | 0.0830 | 0.0021 | deg | N/A | |
| 12 | hrpt | instrument | twotheta_offset | 0.0000 | 0.6264 | 0.0026 | deg | N/A | |
| 13 | hrpt | background | 1 | y | 168.4237 | 168.4068 | 1.3761 | 0.01 % ↓ | |
| 14 | hrpt | background | 2 | y | 164.3727 | 164.4507 | 0.9871 | 0.05 % ↑ | |
| 15 | hrpt | background | 3 | y | 166.8885 | 167.0188 | 0.7279 | 0.08 % ↑ | |
| 16 | hrpt | background | 4 | y | 175.3971 | 175.4557 | 0.6394 | 0.03 % ↑ | |
| 17 | hrpt | background | 5 | y | 174.3046 | 174.4162 | 0.8815 | 0.06 % ↑ |
Show parameter correlations¶
In [8]:
Copied!
project.plotter.plot_param_correlations()
project.plotter.plot_param_correlations()
Plot Meas vs Calc¶
In [9]:
Copied!
project.plotter.plot_meas_vs_calc(expt_name='hrpt', show_residual=True)
project.plotter.plot_meas_vs_calc(expt_name='hrpt', show_residual=True)