|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | 3 | import os |
4 | | -import unittest |
| 4 | +import pytest |
5 | 5 | import numpy as np |
6 | 6 |
|
7 | 7 | from glassure import Pattern, convert_density_to_atoms_per_cubic_angstrom |
|
14 | 14 | calculate_s0, |
15 | 15 | ) |
16 | 16 | from glassure.transform import calculate_sq, calculate_fr |
17 | | -from glassure.normalization import normalize_fit |
| 17 | +from glassure.normalization import normalize_fit, normalize |
18 | 18 | from glassure.configuration import OptimizeConfig |
19 | 19 | from glassure.optimization import optimize_sq, optimize_density |
20 | 20 | from glassure.calc import calculate_pdf, create_calculate_pdf_configs |
|
32 | 32 | background_path_SiO2 = os.path.join(unittest_data_path, "SiO2_bkg.xy") |
33 | 33 |
|
34 | 34 |
|
35 | | -def test_optimize_sq(): |
36 | | - data = Pattern.from_file(data_path_alloy) |
37 | | - background = Pattern.from_file(background_path_alloy) |
38 | | - composition = {"Fe": 0.81, "S": 0.19} |
39 | | - density = 7.9 |
40 | | - atomic_density = convert_density_to_atoms_per_cubic_angstrom(composition, density) |
41 | | - f_squared_mean = calculate_f_squared_mean(composition, data.x) |
42 | | - f_mean_squared = calculate_f_mean_squared(composition, data.x) |
43 | | - incoherent_scattering = calculate_incoherent_scattering(composition, data.x) |
44 | | - background_scaling = 0.97 |
| 35 | +@pytest.fixture |
| 36 | +def data_path(): |
| 37 | + """Path to the test data file.""" |
| 38 | + return os.path.join(unittest_data_path, "SiO2.xy") |
45 | 39 |
|
46 | | - sample_pattern = data - background_scaling * background |
47 | | - |
48 | | - sq = calculate_sq(sample_pattern, f_squared_mean, f_mean_squared) |
49 | | - sq = extrapolate_to_zero_poly(sq, np.min(sq.x) + 0.3) |
50 | | - sq_optimized = optimize_sq(sq, 1.6, 5, atomic_density) |
51 | | - assert not np.allclose(sq.y, sq_optimized.y) |
52 | 40 |
|
| 41 | +@pytest.fixture |
| 42 | +def bkg_path(): |
| 43 | + """Path to the background data file.""" |
| 44 | + return os.path.join(unittest_data_path, "SiO2_bkg.xy") |
53 | 45 |
|
54 | | -def test_optimize_sq_fft(): |
55 | | - data = Pattern.from_file(data_path_SiO2) |
56 | | - background = Pattern.from_file(background_path_SiO2) |
57 | | - composition = {"Si": 1, "O": 2} |
58 | | - density = 2.2 |
59 | | - atomic_density = convert_density_to_atoms_per_cubic_angstrom(composition, density) |
60 | | - background_scaling = 1.0 |
61 | 46 |
|
62 | | - sample_pattern = data - background_scaling * background |
63 | | - sample_pattern = sample_pattern.limit(0, 17).rebin(0.05) |
64 | | - q = sample_pattern.x |
65 | | - f_squared_mean = calculate_f_squared_mean(composition, q) |
66 | | - f_mean_squared = calculate_f_mean_squared(composition, q) |
67 | | - incoherent_scattering = calculate_incoherent_scattering(composition, q) |
| 47 | +@pytest.fixture |
| 48 | +def sample(data_path, bkg_path): |
| 49 | + """Create a sample pattern by subtracting background from data.""" |
| 50 | + data = Pattern.from_file(data_path) |
| 51 | + bkg = Pattern.from_file(bkg_path) |
| 52 | + sample = data - bkg |
| 53 | + return sample.limit(1, 17) |
68 | 54 |
|
69 | | - _, norm_pattern = normalize_fit( |
70 | | - sample_pattern, f_squared_mean, incoherent_scattering |
71 | | - ) |
72 | 55 |
|
73 | | - sq = calculate_sq(norm_pattern, f_squared_mean, f_mean_squared) |
| 56 | +@pytest.fixture |
| 57 | +def sq(normalized_pattern, f_squared_mean, f_mean_squared, composition): |
| 58 | + """Create a sq pattern for testing.""" |
| 59 | + sq = calculate_sq(normalized_pattern, f_squared_mean, f_mean_squared) |
74 | 60 | sq = extrapolate_to_zero_linear(sq, y0=calculate_s0(composition)) |
| 61 | + sq = sq.rebin(0.05) |
75 | 62 | sq.x[0] = 1e-10 |
76 | | - iterations = 5 |
| 63 | + return sq |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def composition(): |
| 68 | + """Sample composition for testing.""" |
| 69 | + return {"Si": 1, "O": 2} |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture |
| 73 | +def density(): |
| 74 | + """Sample density for testing.""" |
| 75 | + return 2.2 |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture |
| 79 | +def atomic_density(composition, density): |
| 80 | + """Calculate atomic density from composition and density.""" |
| 81 | + return convert_density_to_atoms_per_cubic_angstrom(composition, density) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture |
| 85 | +def f_squared_mean(composition, sample): |
| 86 | + """Calculate f squared mean for the sample.""" |
| 87 | + return calculate_f_squared_mean(composition, sample.x) |
77 | 88 |
|
78 | | - fr = calculate_fr(sq, method="fft") |
79 | 89 |
|
| 90 | +@pytest.fixture |
| 91 | +def f_mean_squared(composition, sample): |
| 92 | + """Calculate f mean squared for the sample.""" |
| 93 | + return calculate_f_mean_squared(composition, sample.x) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.fixture |
| 97 | +def incoherent_scattering(composition, sample): |
| 98 | + """Calculate incoherent scattering for the sample.""" |
| 99 | + return calculate_incoherent_scattering(composition, sample.x) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.fixture |
| 103 | +def normalized_pattern(sample, f_squared_mean, incoherent_scattering): |
| 104 | + """Create normalized pattern for testing.""" |
| 105 | + _, normalized = normalize_fit( |
| 106 | + sample, f_squared_mean, incoherent_scattering, q_cutoff=10 |
| 107 | + ) |
| 108 | + return normalized |
| 109 | + |
| 110 | + |
| 111 | +def test_optimize_sq(sq, atomic_density): |
| 112 | + sq_optimized = optimize_sq(sq, 1.4, 5, atomic_density) |
| 113 | + assert not np.allclose(sq.y, sq_optimized.y) |
| 114 | + |
| 115 | + |
| 116 | +def test_optimize_sq_fft(sq, atomic_density): |
| 117 | + iterations = 5 |
| 118 | + r_step = 0.001 # need high value to be accurate for fft |
80 | 119 | sq_optimized = optimize_sq( |
81 | | - sq, 1.3, iterations, atomic_density, fourier_transform_method="integral" |
| 120 | + sq, 1.3, iterations, atomic_density, fourier_transform_method="integral", r_step=r_step |
82 | 121 | ) |
83 | 122 | fr_optimized = calculate_fr(sq_optimized, method="fft") |
84 | 123 |
|
| 124 | + |
85 | 125 | sq_optimized_fft = optimize_sq( |
86 | | - sq, 1.3, iterations, atomic_density, fourier_transform_method="fft" |
| 126 | + sq, 1.3, iterations, atomic_density, fourier_transform_method="fft", r_step=r_step |
87 | 127 | ) |
88 | 128 | fr_optimized_fft = calculate_fr(sq_optimized_fft, method="fft") |
89 | 129 |
|
|
0 commit comments