|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright (c) 2026 DMSC |
| 3 | +"""Tests for analyzing reduced diffraction data using easydiffraction. |
| 4 | +
|
| 5 | +These tests verify the complete workflow: |
| 6 | +1. Define project |
| 7 | +2. Add sample model manually defined |
| 8 | +3. Modify experiment CIF file |
| 9 | +4. Add experiment from modified CIF file |
| 10 | +5. Modify default experiment configuration |
| 11 | +6. Select parameters to be fitted |
| 12 | +7. Do fitting |
| 13 | +""" |
| 14 | + |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +import easydiffraction as ed |
| 20 | + |
| 21 | +# CIF experiment type tags required by easydiffraction to identify |
| 22 | +# the experiment configuration (powder TOF neutron diffraction) |
| 23 | +EXPT_TYPE_TAGS = { |
| 24 | + '_expt_type.sample_form': 'powder', |
| 25 | + '_expt_type.beam_mode': 'time-of-flight', |
| 26 | + '_expt_type.radiation_probe': 'neutron', |
| 27 | + '_expt_type.scattering_type': 'bragg', |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(scope='module') |
| 32 | +def prepared_cif_path( |
| 33 | + cif_path: str, |
| 34 | + tmp_path_factory: pytest.TempPathFactory, |
| 35 | +) -> str: |
| 36 | + """Prepare CIF file with experiment type tags for |
| 37 | + easydiffraction. |
| 38 | + """ |
| 39 | + with Path(cif_path).open() as f: |
| 40 | + content = f.read() |
| 41 | + |
| 42 | + # Add experiment type tags if missing |
| 43 | + for tag, value in EXPT_TYPE_TAGS.items(): |
| 44 | + if tag not in content: |
| 45 | + content += f'\n{tag} {value}' |
| 46 | + |
| 47 | + # Write to temp file |
| 48 | + tmp_dir = tmp_path_factory.mktemp('dream_data') |
| 49 | + prepared_path = tmp_dir / 'dream_reduced_prepared.cif' |
| 50 | + prepared_path.write_text(content) |
| 51 | + |
| 52 | + return str(prepared_path) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture(scope='module') |
| 56 | +def project_with_data( |
| 57 | + prepared_cif_path: str, |
| 58 | +) -> ed.Project: |
| 59 | + """Create project with sample model, experiment data, and |
| 60 | + configuration. |
| 61 | +
|
| 62 | + 1. Define project |
| 63 | + 2. Add sample model manually defined |
| 64 | + 3. Modify experiment CIF file |
| 65 | + 4. Add experiment from modified CIF file |
| 66 | + 5. Modify default experiment configuration |
| 67 | + """ |
| 68 | + # Step 1: Define Project |
| 69 | + project = ed.Project() |
| 70 | + |
| 71 | + # Step 2: Define Sample Model manually |
| 72 | + project.sample_models.add(name='si') |
| 73 | + sample_model = project.sample_models['si'] |
| 74 | + |
| 75 | + sample_model.space_group.name_h_m = 'F d -3 m' |
| 76 | + sample_model.space_group.it_coordinate_system_code = '1' |
| 77 | + |
| 78 | + sample_model.cell.length_a = 5.43146 |
| 79 | + |
| 80 | + sample_model.atom_sites.add( |
| 81 | + label='Si', |
| 82 | + type_symbol='Si', |
| 83 | + fract_x=0.125, |
| 84 | + fract_y=0.125, |
| 85 | + fract_z=0.125, |
| 86 | + wyckoff_letter='c', |
| 87 | + b_iso=1.1, |
| 88 | + ) |
| 89 | + |
| 90 | + # Step 3: Add experiment from modified CIF file |
| 91 | + project.experiments.add(cif_path=prepared_cif_path) |
| 92 | + experiment = project.experiments['reduced_tof'] |
| 93 | + |
| 94 | + # Step 4: Configure experiment |
| 95 | + # Link phase |
| 96 | + experiment.linked_phases.add(id='si', scale=0.8) |
| 97 | + |
| 98 | + # Instrument setup |
| 99 | + experiment.instrument.setup_twotheta_bank = 90.0 |
| 100 | + experiment.instrument.calib_d_to_tof_linear = 18630.0 |
| 101 | + |
| 102 | + # Peak profile parameters |
| 103 | + experiment.peak.broad_gauss_sigma_0 = 48500.0 |
| 104 | + experiment.peak.broad_gauss_sigma_1 = 3000.0 |
| 105 | + experiment.peak.broad_gauss_sigma_2 = 0.0 |
| 106 | + experiment.peak.broad_mix_beta_0 = 0.05 |
| 107 | + experiment.peak.broad_mix_beta_1 = 0.0 |
| 108 | + experiment.peak.asym_alpha_0 = 0.0 |
| 109 | + experiment.peak.asym_alpha_1 = 0.26 |
| 110 | + |
| 111 | + # Excluded regions |
| 112 | + experiment.excluded_regions.add(id='1', start=0, end=10000) |
| 113 | + experiment.excluded_regions.add(id='2', start=70000, end=200000) |
| 114 | + |
| 115 | + # Background points |
| 116 | + background_points = [ |
| 117 | + ('2', 10000, 0.01), |
| 118 | + ('3', 14000, 0.2), |
| 119 | + ('4', 21000, 0.7), |
| 120 | + ('5', 27500, 0.6), |
| 121 | + ('6', 40000, 0.3), |
| 122 | + ('7', 50000, 0.6), |
| 123 | + ('8', 61000, 0.7), |
| 124 | + ('9', 70000, 0.6), |
| 125 | + ] |
| 126 | + for id_, x, y in background_points: |
| 127 | + experiment.background.add(id=id_, x=x, y=y) |
| 128 | + |
| 129 | + return project |
| 130 | + |
| 131 | + |
| 132 | +@pytest.fixture(scope='module') |
| 133 | +def fitted_project( |
| 134 | + project_with_data: ed.Project, |
| 135 | +) -> ed.Project: |
| 136 | + """Perform fit and return project with results. |
| 137 | +
|
| 138 | + 6. Select parameters to be fitted |
| 139 | + 7. Do fitting |
| 140 | + """ |
| 141 | + project = project_with_data |
| 142 | + sample_model = project.sample_models['si'] |
| 143 | + experiment = project.experiments['reduced_tof'] |
| 144 | + |
| 145 | + # Step 5: Select parameters to be fitted |
| 146 | + # Set free parameters for sample model |
| 147 | + sample_model.atom_sites['Si'].b_iso.free = True |
| 148 | + |
| 149 | + # Set free parameters for experiment |
| 150 | + experiment.linked_phases['si'].scale.free = True |
| 151 | + experiment.instrument.calib_d_to_tof_linear.free = True |
| 152 | + |
| 153 | + experiment.peak.broad_gauss_sigma_0.free = True |
| 154 | + experiment.peak.broad_gauss_sigma_1.free = True |
| 155 | + experiment.peak.broad_mix_beta_0.free = True |
| 156 | + |
| 157 | + # Set free parameters for background |
| 158 | + for point in experiment.background: |
| 159 | + point.y.free = True |
| 160 | + |
| 161 | + # Step 6: Do fitting |
| 162 | + project.analysis.fit() |
| 163 | + |
| 164 | + return project |
| 165 | + |
| 166 | + |
| 167 | +# Test: Data Loading |
| 168 | + |
| 169 | + |
| 170 | +def test_analyze_reduced_data__load_cif( |
| 171 | + project_with_data: ed.Project, |
| 172 | +) -> None: |
| 173 | + """Verify CIF data loads into project correctly.""" |
| 174 | + assert 'reduced_tof' in project_with_data.experiments.names |
| 175 | + |
| 176 | + |
| 177 | +def test_analyze_reduced_data__data_size( |
| 178 | + project_with_data: ed.Project, |
| 179 | +) -> None: |
| 180 | + """Verify loaded data has expected size.""" |
| 181 | + experiment = project_with_data.experiments['reduced_tof'] |
| 182 | + # Data should have substantial number of points |
| 183 | + assert experiment.data.x.size > 100 |
| 184 | + |
| 185 | + |
| 186 | +# Test: Configuration |
| 187 | + |
| 188 | + |
| 189 | +def test_analyze_reduced_data__phase_linked( |
| 190 | + project_with_data: ed.Project, |
| 191 | +) -> None: |
| 192 | + """Verify phase is correctly linked to experiment.""" |
| 193 | + experiment = project_with_data.experiments['reduced_tof'] |
| 194 | + assert 'si' in experiment.linked_phases.names |
| 195 | + |
| 196 | + |
| 197 | +def test_analyze_reduced_data__background_set( |
| 198 | + project_with_data: ed.Project, |
| 199 | +) -> None: |
| 200 | + """Verify background points are configured.""" |
| 201 | + experiment = project_with_data.experiments['reduced_tof'] |
| 202 | + assert len(experiment.background.names) >= 5 |
| 203 | + |
| 204 | + |
| 205 | +# Test: Fitting |
| 206 | + |
| 207 | + |
| 208 | +def test_analyze_reduced_data__fit_quality( |
| 209 | + fitted_project: ed.Project, |
| 210 | +) -> None: |
| 211 | + """Verify fit quality is reasonable (chi-square value).""" |
| 212 | + chi_square = fitted_project.analysis.fit_results.reduced_chi_square |
| 213 | + assert chi_square == pytest.approx(16.0, abs=0.1) |
0 commit comments