Skip to content

Commit 3d1cd03

Browse files
No dynamic classes (#2)
* Refactor peak asymmetry classes and improve docs * Refactor peak broadening handling with a factory * Enhance Descriptor class with new attributes * Refactor type hints for clarity * Update peak broadening parameters and descriptions * Refactor parameter initialization for clarity * Create class Peak * New implementation with dynamically attached attributes * Refactor experiment imports and add convenience API * Fix advanced example * Refine strategy descriptions and cleanup code * Refactor background handling and add new types * Add CIF analysis display feature * Update descriptions for clarity and detail * Clean up * Add item access and iteration methods * Clean up * Improve project loading and saving functionality * Update project save path for clarity * Clean up * Refactor component base and instrument classes * Clean up * Refactor attribute formatting and rename files * Refactor components for better parameter handling * New peak.py * Refactor peak profile handling * Refactors component architecture for iterables * Refactors CIF category definition for components * Adds generic `as_dict` method to ComponentBase * Refactors core component structure * Renames "refinement" to "fitting" for clarity.
1 parent 663ba8d commit 3d1cd03

40 files changed

+1592
-1379
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Development playground for the new EasyDiffraction API.
2525
```
2626

2727
## Testing
28+
2829
```bash
2930
PYTHONPATH=$(pwd)/src python -m pytest tests/ --color=yes
3031
```
@@ -33,9 +34,9 @@ Development playground for the new EasyDiffraction API.
3334

3435
- Simplified API:
3536
```bash
36-
PYTHONPATH=$(pwd)/src python examples/joint-refinement_simple-api.py
37+
PYTHONPATH=$(pwd)/src python examples/joint-fit_simple-api.py
3738
```
3839
- Advanced API:
3940
```bash
40-
PYTHONPATH=$(pwd)/src python examples/joint-refinement_advanced-api.py
41+
PYTHONPATH=$(pwd)/src python examples/joint-fit_advanced-api.py
4142
```

examples/joint-refinement_advanced-api.py renamed to examples/joint-fit_advanced-api.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929

3030
# Experiment 1: Neutron powder diffraction
3131
expt1 = Experiment(id="npd", radiation_probe="neutron", data_path="examples/data/pbso4_powder_neutron_cw.dat")
32-
expt1.instr_setup.wavelength = 1.91
33-
expt1.instr_calib.twotheta_offset = -0.1406
34-
expt1.peak_broad.gauss_u = 0.139
35-
expt1.peak_broad.gauss_v = -0.412
36-
expt1.peak_broad.gauss_w = 0.386
37-
expt1.peak_broad.lorentz_x = 0
38-
expt1.peak_broad.lorentz_y = 0.088
32+
expt1.instrument.setup_wavelength = 1.91
33+
expt1.instrument.calib_twotheta_offset = -0.1406
34+
expt1.peak.broad_gauss_u = 0.139
35+
expt1.peak.broad_gauss_v = -0.412
36+
expt1.peak.broad_gauss_w = 0.386
37+
expt1.peak.broad_lorentz_x = 0
38+
expt1.peak.broad_lorentz_y = 0.088
3939
expt1.linked_phases.add("pbso4", scale=1.0)
40+
expt1.background_type = "line-segment"
4041
for x, y in [
4142
(11.0, 206.1624),
4243
(15.0, 194.75),
@@ -51,23 +52,22 @@
5152

5253
# Experiment 2: X-ray powder diffraction
5354
expt2 = Experiment(id="xrd", radiation_probe="xray", data_path="examples/data/pbso4_powder_xray.dat")
54-
expt2.instr_setup.wavelength = 1.540567
55-
expt2.instr_calib.twotheta_offset = -0.05181
56-
expt2.peak_broad.gauss_u = 0.304138
57-
expt2.peak_broad.gauss_v = -0.112622
58-
expt2.peak_broad.gauss_w = 0.021272
59-
expt2.peak_broad.lorentz_x = 0
60-
expt2.peak_broad.lorentz_y = 0.057691
55+
expt2.instrument.setup_wavelength = 1.540567
56+
expt2.instrument.calib_twotheta_offset = -0.05181
57+
expt2.peak.broad_gauss_u = 0.304138
58+
expt2.peak.broad_gauss_v = -0.112622
59+
expt2.peak.broad_gauss_w = 0.021272
60+
expt2.peak.broad_lorentz_x = 0
61+
expt2.peak.broad_lorentz_y = 0.057691
6162
expt2.linked_phases.add("pbso4", scale=0.005)
63+
expt2.background_type = "chebyshev polynomial"
6264
for x, y in [
63-
(11.0, 141.8516),
64-
(13.0, 102.8838),
65-
(16.0, 78.0551),
66-
(20.0, 124.0121),
67-
(30.0, 123.7123),
68-
(50.0, 120.8266),
69-
(90.0, 113.7473),
70-
(110.0, 132.4643),
65+
(0, 119.195),
66+
(1, 6.221),
67+
(2, -45.725),
68+
(3, 8.119),
69+
(4, 54.552),
70+
(5, -20.661),
7171
]:
7272
expt2.background.add(x, y)
7373

@@ -80,7 +80,7 @@
8080
# Set calculator, minimizer and refinement strategy
8181
project.analysis.current_calculator = "crysfml"
8282
project.analysis.current_minimizer = "lmfit (leastsq)"
83-
project.analysis.refinement_strategy = 'combined'
83+
project.analysis.fit_mode = 'joint'
8484

8585
# Define free parameters
8686
model.cell.length_a.free = True

examples/joint-refinement_simple-api.py renamed to examples/joint-fit_simple-api.py

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
The objective is to accurately refine the crystal structure of PbSO4."""
3333

3434
# Save the initial project specifying the directory path
35-
project.save_as("examples/pbso4_joint")
35+
project.save_as("examples/projects/pbso4_joint")
3636

3737
# Show project metadata
3838
project.info.show_as_cif()
@@ -104,17 +104,14 @@
104104

105105
print(ed.chapter('Step 3: Add Experiments (Instrument models and measured data)'))
106106

107-
print(ed.section('Add experiments'))
108-
109-
# Add two experiments
110107
project.experiments.add(id="npd",
111-
diffr_mode="powder",
112-
expt_mode="constant_wavelength",
108+
sample_form="powder",
109+
beam_mode="constant wavelength",
113110
radiation_probe="neutron",
114111
data_path="examples/data/pbso4_powder_neutron_cw.dat")
115112
project.experiments.add(id="xrd",
116-
diffr_mode="powder",
117-
expt_mode="constant_wavelength",
113+
sample_form="powder",
114+
beam_mode="constant wavelength",
118115
radiation_probe="xray",
119116
data_path="examples/data/pbso4_powder_xray.dat")
120117

@@ -126,25 +123,31 @@
126123
project.experiments['xrd'].show_meas_chart(x_min=26, x_max=28)
127124

128125
# Modify experimental parameters
129-
project.experiments['npd'].instr_setup.wavelength = 1.91
130-
project.experiments["npd"].instr_calib.twotheta_offset = -0.1406
131-
project.experiments["npd"].peak_broad.gauss_u = 0.139
132-
project.experiments["npd"].peak_broad.gauss_v = -0.412
133-
project.experiments["npd"].peak_broad.gauss_w = 0.386
134-
project.experiments["npd"].peak_broad.lorentz_x = 0
135-
project.experiments["npd"].peak_broad.lorentz_y = 0.088
136-
137-
project.experiments['xrd'].instr_setup.wavelength = 1.540567
138-
project.experiments["xrd"].instr_calib.twotheta_offset = -0.05181
139-
project.experiments["xrd"].peak_broad.gauss_u = 0.304138
140-
project.experiments["xrd"].peak_broad.gauss_v = -0.112622
141-
project.experiments["xrd"].peak_broad.gauss_w = 0.021272
142-
project.experiments["xrd"].peak_broad.lorentz_x = 0
143-
project.experiments["xrd"].peak_broad.lorentz_y = 0.057691
126+
project.experiments['npd'].instrument.setup_wavelength = 1.91
127+
project.experiments["npd"].instrument.calib_twotheta_offset = -0.1406
128+
129+
project.experiments["npd"].show_supported_peak_profile_types()
130+
project.experiments["npd"].peak_profile_type = "ikeda-carpenter"
131+
project.experiments["npd"].show_current_peak_profile_type()
132+
project.experiments["npd"].peak_profile_type = "split pseudo-voigt"
133+
project.experiments["npd"].peak_profile_type = "pseudo-voigt"
134+
project.experiments["npd"].peak.broad_gauss_u = 0.139
135+
project.experiments["npd"].peak.broad_gauss_v = -0.412
136+
project.experiments["npd"].peak.broad_gauss_w = 0.386
137+
project.experiments["npd"].peak.broad_lorentz_x = 0
138+
project.experiments["npd"].peak.broad_lorentz_y = 0.088
139+
140+
project.experiments['xrd'].instrument.setup_wavelength = 1.540567
141+
project.experiments["xrd"].instrument.calib_twotheta_offset = -0.05181
142+
project.experiments["xrd"].peak.broad_gauss_u = 0.304138
143+
project.experiments["xrd"].peak.broad_gauss_v = -0.112622
144+
project.experiments["xrd"].peak.broad_gauss_w = 0.021272
145+
project.experiments["xrd"].peak.broad_lorentz_x = 0
146+
project.experiments["xrd"].peak.broad_lorentz_y = 0.057691
144147

145148
# Link sample model to experiments
146149
project.experiments['npd'].linked_phases.add(id='pbso4', scale=1.0)
147-
project.experiments['xrd'].linked_phases.add(id='pbso4', scale=0.005)
150+
project.experiments['xrd'].linked_phases.add(id='pbso4', scale=0.002)
148151

149152
# Show experiments as CIF
150153
project.experiments["npd"].show_as_cif()
@@ -161,7 +164,7 @@
161164
print(ed.chapter('Step 4: Analysis'))
162165

163166
print(ed.section('Set calculator'))
164-
project.analysis.show_available_calculators()
167+
project.analysis.show_supported_calculators()
165168
project.analysis.show_current_calculator()
166169
project.analysis.current_calculator = 'crysfml'
167170

@@ -175,6 +178,9 @@
175178

176179
# The following background points represent the baseline noise in the diffraction data.
177180
print(ed.section('Add background'))
181+
project.experiments["npd"].background_type = "point"
182+
project.experiments["npd"].show_supported_background_types()
183+
project.experiments["npd"].show_current_background_type()
178184
project.experiments["npd"].background.add(x=11.0, y=206.1624)
179185
project.experiments["npd"].background.add(x=15.0, y=194.75)
180186
project.experiments["npd"].background.add(x=20.0, y=194.505)
@@ -185,14 +191,13 @@
185191
project.experiments["npd"].background.add(x=153.0, y=226.0595)
186192
project.experiments["npd"].background.show()
187193

188-
project.experiments["xrd"].background.add(x=11.0, y=141.8516)
189-
project.experiments["xrd"].background.add(x=13.0, y=102.8838)
190-
project.experiments["xrd"].background.add(x=16.0, y=78.0551)
191-
project.experiments["xrd"].background.add(x=20.0, y=124.0121)
192-
project.experiments["xrd"].background.add(x=30.0, y=123.7123)
193-
project.experiments["xrd"].background.add(x=50.0, y=120.8266)
194-
project.experiments["xrd"].background.add(x=90.0, y=113.7473)
195-
project.experiments["xrd"].background.add(x=110.0, y=132.4643)
194+
project.experiments["xrd"].background_type = "chebyshev polynomial"
195+
project.experiments["xrd"].background.add(order=0, coef=119.195)
196+
project.experiments["xrd"].background.add(order=1, coef=6.221)
197+
project.experiments["xrd"].background.add(order=2, coef=-45.725)
198+
project.experiments["xrd"].background.add(order=3, coef=8.119)
199+
project.experiments["xrd"].background.add(order=4, coef=54.552)
200+
project.experiments["xrd"].background.add(order=5, coef=-20.661)
196201
project.experiments["xrd"].background.show()
197202

198203
print(ed.section('Show experiments as CIF. Now the background points are included'))
@@ -221,10 +226,10 @@
221226

222227
project.analysis.show_free_params()
223228

224-
print(ed.section('Set refinement strategy'))
225-
project.analysis.show_available_refinement_strategies()
226-
project.analysis.show_current_refinement_strategy()
227-
#project.analysis.refinement_strategy = 'single'
229+
print(ed.section('Set fit mode'))
230+
project.analysis.show_available_fit_modes()
231+
project.analysis.show_current_fit_mode()
232+
#project.analysis.fit_mode = 'single'
228233

229234
print(ed.section('Set fitting engine'))
230235
project.analysis.show_available_minimizers()
@@ -250,7 +255,7 @@
250255
project.analysis.show_meas_vs_calc_chart(expt_id="xrd", x_min=26, x_max=28, show_residual=True)
251256

252257
print(ed.section('Change calculator'))
253-
project.analysis.show_available_calculators()
258+
project.analysis.show_supported_calculators()
254259
project.analysis.current_calculator = 'cryspy'
255260

256261
print(ed.section('Start 3rd fitting'))
@@ -260,13 +265,13 @@
260265
project.analysis.show_meas_vs_calc_chart(expt_id="npd", x_min=62, x_max=66, show_residual=True)
261266
project.analysis.show_meas_vs_calc_chart(expt_id="xrd", x_min=26, x_max=28, show_residual=True)
262267

263-
print(ed.section('Change refinement strategy'))
264-
project.analysis.show_available_refinement_strategies()
265-
project.analysis.show_current_refinement_strategy()
266-
project.analysis.refinement_strategy = 'combined'
268+
print(ed.section('Change fit mode'))
269+
project.analysis.show_available_fit_modes()
270+
project.analysis.show_current_fit_mode()
271+
project.analysis.fit_mode = 'joint'
267272

268273
print(ed.section('Change calculator'))
269-
project.analysis.show_available_calculators()
274+
project.analysis.show_supported_calculators()
270275
project.analysis.show_current_calculator()
271276
project.analysis.current_calculator = 'crysfml'
272277

@@ -277,6 +282,9 @@
277282
project.analysis.show_meas_vs_calc_chart(expt_id="npd", x_min=62, x_max=66, show_residual=True)
278283
project.analysis.show_meas_vs_calc_chart(expt_id="xrd", x_min=26, x_max=28, show_residual=True)
279284

285+
# Show analysis as CIF
286+
project.analysis.show_as_cif()
287+
280288
# Save the project state after analysis
281289
project.save()
282290

src/easydiffraction/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
)
1212

1313
# Experiment creation and collection management
14-
from easydiffraction.experiments.experiments import (
15-
Experiment,
16-
Experiments
17-
)
14+
from easydiffraction.experiments.experiment import Experiment
15+
from easydiffraction.experiments.experiments import Experiments
1816

1917
# Analysis
2018
from easydiffraction.analysis.analysis import Analysis

0 commit comments

Comments
 (0)