Change minimizer¶
This example shows how to change the minimization engine for refinement.
Import EasyDiffraction¶
import easydiffraction as ed
Create a job¶
Create a job — the main object to store all the information
job = ed.Job()
Define a model¶
Download the CIF file from the EasyDiffraction repository on GitHub
ed.download_from_repository('lbco_adp.cif', destination='data')
Load a phase from the downloaded CIF file
job.add_phase_from_file('data/lbco_adp.cif')
print(job.phases)
Collection of 1 phases: ['lbco']
Show phase info in CIF format
phase = job.phases['lbco']
print(phase.cif)
data_lbco _cell_length_a 3.89 _cell_length_b 3.89 _cell_length_c 3.89 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _space_group_name_H-M_ref 'P m -3 m' loop_ _atom_site_label _atom_site_type_symbol _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy _atom_site_adp_type _atom_site_B_iso_or_equiv La La 0.00000000 0.00000000 0.00000000 0.5 Biso 0.4958 Ba Ba 0.00000000 0.00000000 0.00000000 0.5 Biso 0.4943 Co Co 0.5 0.5 0.5 1.00000000 Biso 0.2567 O O 0.00000000 0.5 0.5 1.00000000 Biso 1.4041
Display the crystal structure of a given model
job.show_crystal_structure(id='lbco')
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
Define an experiment¶
Download the data file from the EasyDiffraction repository on GitHub
ed.download_from_repository('hrpt.xye', destination='data')
Show content of the downloaded file (first 6 lines)
with open('data/hrpt.xye') as f:
print(''.join(f.readlines()[:6]))
# 2theta intensity su 10.00 167.00 12.60 10.05 157.00 12.50 10.10 187.00 13.30 10.15 197.00 14.00 10.20 164.00 12.50
Load experimentally measured data from a file in XYE format
job.add_experiment_from_file('data/hrpt.xye')
Display the experimentally measured data
job.show_experiment_chart()
Define a point background
background_points = [(10.0, 170),
(165.0, 170)]
job.set_background(background_points)
Display the experiment chart after setting the background
job.show_experiment_chart()
Perform an analysis¶
Display the analysis chart before setting initial parameter values
job.show_analysis_chart()
Create aliases for the two types of experimental parameters
pattern = job.pattern
instrument = job.instrument
Change the default value of the wavelength used in the experiment and display the analysis chart again
instrument.wavelength = 1.494
job.show_analysis_chart()
Select parameters to be refined
phase.scale.free = True
phase.cell.length_a.free = True
pattern.zero_shift.free = True
instrument.resolution_u.free = True
instrument.resolution_v.free = True
instrument.resolution_w.free = True
instrument.resolution_y.free = True
Set the initial values of the parameters to be refined and display the analysis chart again
phase.scale = 6
phase.cell.length_a = 3.88
pattern.zero_shift = 0.3
instrument.resolution_u = 0.1
instrument.resolution_v = -0.1
instrument.resolution_w = 0.1
instrument.resolution_y = 0
job.show_analysis_chart()
Print parameters to be refined (free parameters) before fitting with Lmfit
job.show_free_parameters()
name | value | error | ||
---|---|---|---|---|
1 | .phases['lbco'].cell.length_a | 3.88 | Å | 0.0 |
2 | .phases['lbco'].scale | 6.00 | 0.0 | |
3 | .instrument.resolution_u | 0.10 | 0.0 | |
4 | .instrument.resolution_v | -0.10 | 0.0 | |
5 | .instrument.resolution_w | 0.10 | 0.0 | |
6 | .instrument.resolution_y | 0.00 | 0.0 |
Show the current minimization engine
print(job.analysis.current_minimizer)
LMFit_leastsq
Start minimization using the default minimizer (Least Squares method from the Lmfit library)
job.fit()
Fitting result Status: 🥳 Success Duration: ⌛ 2.10 s Reduced χ²: 👍 1.31
Print the refined parameters after fitting with Lmfit
job.show_free_parameters()
name | value | error | ||
---|---|---|---|---|
1 | .phases['lbco'].cell.length_a | 3.890880 | Å | 0.000040 |
2 | .phases['lbco'].scale | 9.202849 | 0.032388 | |
3 | .instrument.resolution_u | 0.081730 | 0.003211 | |
4 | .instrument.resolution_v | -0.115064 | 0.006879 | |
5 | .instrument.resolution_w | 0.118244 | 0.003306 | |
6 | .instrument.resolution_y | 0.087071 | 0.001971 | |
7 | .pattern.zero_shift | 0.622785 | deg | 0.001079 |
Display the analysis chart after the fitting with Lmfit
job.show_analysis_chart()
Show list of available minimisation engines
for mini in job.analysis.available_minimizers:
print(mini)
LMFit LMFit_leastsq LMFit_powell LMFit_cobyla LMFit_differential_evolution LMFit_scipy_least_squares Bumps Bumps_simplex Bumps_newton Bumps_lm DFO DFO_leastsq
Change the minimization method to simplex from the Bumps library
job.analysis.current_minimizer = 'Bumps_lm'
print(job.analysis.current_minimizer)
Bumps_lm
Reset the free parameters to their initial values (before fitting with Lmfit) and display the analysis chart
phase.scale = 6
phase.cell.length_a = 3.88
pattern.zero_shift = 0.3
instrument.resolution_u = 0.1
instrument.resolution_v = -0.1
instrument.resolution_w = 0.1
instrument.resolution_y = 0
job.show_analysis_chart()
Print free parameters before the fitting with Bumps
job.show_free_parameters()
name | value | error | ||
---|---|---|---|---|
1 | .phases['lbco'].cell.length_a | 3.88 | Å | 0.000040 |
2 | .phases['lbco'].scale | 6.00 | 0.032388 | |
3 | .instrument.resolution_u | 0.10 | 0.003211 | |
4 | .instrument.resolution_v | -0.10 | 0.006879 | |
5 | .instrument.resolution_w | 0.10 | 0.003306 | |
6 | .instrument.resolution_y | 0.00 | 0.001971 | |
7 | .pattern.zero_shift | 0.30 | deg | 0.001079 |
Start minimization with Bumps
job.fit()
Fitting result Status: 🥳 Success Duration: ⌛ 2.08 s Reduced χ²: 👍 1.31
Print free parameters after the fitting with Bumps
job.show_free_parameters()
name | value | error | ||
---|---|---|---|---|
1 | .phases['lbco'].cell.length_a | 3.890896 | Å | 0.000035 |
2 | .phases['lbco'].scale | 9.202826 | 0.028286 | |
3 | .instrument.resolution_u | 0.081818 | 0.002806 | |
4 | .instrument.resolution_v | -0.115158 | 0.006010 | |
5 | .instrument.resolution_w | 0.118262 | 0.002888 | |
6 | .instrument.resolution_y | 0.087064 | 0.001721 | |
7 | .pattern.zero_shift | 0.623144 | deg | 0.000943 |
Display the analysis chart after the fitting with Bumps
job.show_analysis_chart()