Skip to content

Commit 67b53ce

Browse files
Add integration test (#141)
* Add integration test * Add licence header
1 parent 8d97ad8 commit 67b53ce

1 file changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# SPDX-FileCopyrightText: 2026 EasyScience contributors <https://github.com/easyscience>
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
import numpy as np
5+
import pooch
6+
7+
from easydynamics.analysis.analysis import Analysis
8+
from easydynamics.experiment import Experiment
9+
from easydynamics.sample_model import ComponentCollection
10+
from easydynamics.sample_model import DeltaFunction
11+
from easydynamics.sample_model import Gaussian
12+
from easydynamics.sample_model import Lorentzian
13+
from easydynamics.sample_model import Polynomial
14+
from easydynamics.sample_model.background_model import BackgroundModel
15+
from easydynamics.sample_model.diffusion_model.brownian_translational_diffusion import (
16+
BrownianTranslationalDiffusion,
17+
)
18+
from easydynamics.sample_model.instrument_model import InstrumentModel
19+
from easydynamics.sample_model.resolution_model import ResolutionModel
20+
from easydynamics.sample_model.sample_model import SampleModel
21+
22+
23+
class TestFittingWithDiffusionModel:
24+
def test_fitting_with_diffusion_model(self):
25+
# Load the vanadium data
26+
vanadium_experiment = Experiment('Vanadium')
27+
28+
file_path = pooch.retrieve(
29+
url='https://github.com/easyscience/dynamics-lib/raw/refs/heads/master/docs/docs/tutorials/data/vanadium_data_example.h5',
30+
known_hash='16cc1b327c303feeb88fb9dda5390dc4880b62396b1793f98c6fef0b27c7b873',
31+
)
32+
33+
vanadium_experiment.load_hdf5(filename=file_path)
34+
35+
delta_function = DeltaFunction(display_name='DeltaFunction', area=1)
36+
sample_model = SampleModel(components=delta_function)
37+
38+
resolution_components = ComponentCollection()
39+
res_gauss = Gaussian(width=0.1, area=1, display_name='Res. Gauss')
40+
res_gauss.area.fixed = True
41+
resolution_components.append_component(res_gauss)
42+
resolution_model = ResolutionModel(components=resolution_components)
43+
44+
background_model = BackgroundModel(components=Polynomial(coefficients=[0.001]))
45+
46+
instrument_model = InstrumentModel(
47+
resolution_model=resolution_model,
48+
background_model=background_model,
49+
)
50+
51+
vanadium_analysis = Analysis(
52+
display_name='Vanadium Full Analysis',
53+
experiment=vanadium_experiment,
54+
sample_model=sample_model,
55+
instrument_model=instrument_model,
56+
)
57+
58+
fit_result_independent_single_Q = vanadium_analysis.fit(
59+
fit_method='independent', Q_index=5
60+
)
61+
62+
assert fit_result_independent_single_Q.success
63+
assert fit_result_independent_single_Q.chi2 < 75.0
64+
assert fit_result_independent_single_Q.reduced_chi < 0.4
65+
66+
diffusion_experiment = Experiment('Diffusion')
67+
68+
file_path = pooch.retrieve(
69+
url='https://github.com/easyscience/dynamics-lib/raw/refs/heads/master/docs/docs/tutorials/data/diffusion_data_example.h5',
70+
known_hash='5fe846b19aacbda8b8b936eb2e5310d025dc56c25b0b353521e7d6b921f229ab',
71+
)
72+
73+
diffusion_experiment.load_hdf5(filename=file_path)
74+
75+
delta_function = DeltaFunction(display_name='DeltaFunction', area=0.2)
76+
lorentzian = Lorentzian(display_name='Lorentzian', area=0.5, width=0.3)
77+
component_collection = ComponentCollection(
78+
components=[delta_function, lorentzian],
79+
)
80+
81+
sample_model = SampleModel(
82+
components=component_collection,
83+
)
84+
85+
background_model = BackgroundModel(components=Polynomial(coefficients=[0.001]))
86+
87+
instrument_model = InstrumentModel(
88+
background_model=background_model,
89+
resolution_model=vanadium_analysis.instrument_model.resolution_model,
90+
)
91+
instrument_model.resolution_model.fix_all_parameters()
92+
93+
diffusion_analysis = Analysis(
94+
display_name='Diffusion Analysis',
95+
experiment=diffusion_experiment,
96+
sample_model=sample_model,
97+
instrument_model=instrument_model,
98+
)
99+
100+
fit_result = diffusion_analysis.fit(fit_method='independent')
101+
102+
assert fit_result[0].success
103+
assert fit_result[0].chi2 < 43.0
104+
assert fit_result[0].reduced_chi < 0.3
105+
106+
###############
107+
# Diffusion model
108+
###############
109+
110+
delta_function = DeltaFunction(display_name='DeltaFunction', area=0.2)
111+
component_collection = ComponentCollection(
112+
components=[delta_function],
113+
)
114+
diffusion_model = BrownianTranslationalDiffusion(
115+
display_name='Brownian Translational Diffusion',
116+
diffusion_coefficient=2.4e-9,
117+
scale=0.5,
118+
)
119+
120+
sample_model = SampleModel(
121+
components=component_collection,
122+
diffusion_models=diffusion_model,
123+
)
124+
125+
background_model = BackgroundModel(components=Polynomial(coefficients=[0.001]))
126+
127+
instrument_model = InstrumentModel(
128+
background_model=background_model,
129+
resolution_model=vanadium_analysis.instrument_model.resolution_model,
130+
)
131+
132+
diffusion_model_analysis = Analysis(
133+
display_name='Diffusion Full Analysis',
134+
experiment=diffusion_experiment,
135+
sample_model=sample_model,
136+
instrument_model=instrument_model,
137+
)
138+
139+
diffusion_model_analysis.instrument_model.resolution_model.fix_all_parameters()
140+
141+
fit_result = diffusion_model_analysis.fit(fit_method='simultaneous')
142+
143+
assert fit_result[0].success
144+
assert fit_result[0].chi2 < 56.0
145+
assert fit_result[0].reduced_chi < 0.4
146+
147+
pars = diffusion_model.get_all_parameters()
148+
149+
tol = 10 * pars[0].error
150+
assert np.isclose(pars[0].value, 1.1258025622851164e-08, atol=tol)
151+
tol = 10 * pars[1].error
152+
assert np.isclose(pars[1].value, 0.6937774083152299, atol=tol)

0 commit comments

Comments
 (0)