Skip to content

Commit 9ac64dc

Browse files
committed
test: add mcstas test files and tests
1 parent dd1b7c0 commit 9ac64dc

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

src/ess/estia/data.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3+
4+
from ..reflectometry.types import Filename, ReferenceRun, SampleRun
5+
6+
_version = "1"
7+
8+
9+
def _make_pooch():
10+
import pooch
11+
12+
return pooch.create(
13+
path=pooch.os_cache("ess/amor"),
14+
env="ESS_ESTIA_DATA_DIR",
15+
base_url="https://public.esss.dk/groups/scipp/ess/estia/{version}/",
16+
version=_version,
17+
registry={
18+
"218610_tof_detector_list.p.x.y.t.L.sx.sy": "md5:65145a26c36d12954a97d27d6e7f4ed9", # noqa: E501
19+
"218611_tof_detector_list.p.x.y.t.L.sx.sy": "md5:4599e938568f3b73a72d6d48fe5160e7", # noqa: E501
20+
"218612_tof_detector_list.p.x.y.t.L.sx.sy": "md5:6bacd1e4d922007c7f574f20378b28f2", # noqa: E501
21+
"218613_tof_detector_list.p.x.y.t.L.sx.sy": "md5:7c17cb8a2fe38f4f0976de1254295636", # noqa: E501
22+
"218614_tof_detector_list.p.x.y.t.L.sx.sy": "md5:78cf399dcedea2a2d4178e11b95c53f2", # noqa: E501
23+
},
24+
)
25+
26+
27+
_pooch = _make_pooch()
28+
29+
30+
def estia_mcstas_reference_run() -> Filename[ReferenceRun]:
31+
return Filename[ReferenceRun](
32+
_pooch.fetch("218610_tof_detector_list.p.x.y.t.L.sx.sy")
33+
)
34+
35+
36+
def estia_mcstas_sample_run(number: int | str) -> Filename[SampleRun]:
37+
return Filename[SampleRun](
38+
_pooch.fetch(f"2186{int(number):02d}_tof_detector_list.p.x.y.t.L.sx.sy")
39+
)
40+
41+
42+
__all__ = [
43+
"estia_mcstas_reference_run",
44+
"estia_mcstas_sample_run",
45+
]

tests/estia/mcstas_test.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2024 Scipp contributors (https://github.com/scipp)
3+
# flake8: noqa: F403, F405
4+
5+
import numpy as np
6+
import pytest
7+
import sciline
8+
import scipp as sc
9+
10+
from ess.estia import EstiaWorkflow
11+
from ess.estia.data import estia_mcstas_reference_run, estia_mcstas_sample_run
12+
from ess.estia.load import load_mcstas_events
13+
from ess.reflectometry.types import (
14+
BeamDivergenceLimits,
15+
Filename,
16+
ProtonCurrent,
17+
QBins,
18+
ReducibleData,
19+
ReferenceRun,
20+
ReflectivityOverQ,
21+
SampleRun,
22+
WavelengthBins,
23+
YIndexLimits,
24+
ZIndexLimits,
25+
)
26+
27+
28+
@pytest.fixture
29+
def estia_mcstas_pipeline() -> sciline.Pipeline:
30+
wf = EstiaWorkflow()
31+
wf.insert(load_mcstas_events)
32+
wf[Filename[ReferenceRun]] = estia_mcstas_reference_run()
33+
34+
wf[YIndexLimits] = sc.scalar(35), sc.scalar(64)
35+
wf[ZIndexLimits] = sc.scalar(0), sc.scalar(14 * 32)
36+
wf[BeamDivergenceLimits] = sc.scalar(-1.0, unit='deg'), sc.scalar(1.0, unit='deg')
37+
wf[WavelengthBins] = sc.geomspace('wavelength', 3.5, 12, 2001, unit='angstrom')
38+
wf[ProtonCurrent[SampleRun]] = sc.DataArray(
39+
sc.array(dims=('time',), values=[]),
40+
coords={'time': sc.array(dims=('time',), values=[], unit='s')},
41+
)
42+
wf[ProtonCurrent[ReferenceRun]] = sc.DataArray(
43+
sc.array(dims=('time',), values=[]),
44+
coords={'time': sc.array(dims=('time',), values=[], unit='s')},
45+
)
46+
47+
return wf
48+
49+
50+
def test_mcstas_loading(estia_mcstas_pipeline: sciline.Pipeline):
51+
estia_mcstas_pipeline[Filename[SampleRun]] = estia_mcstas_sample_run(11)
52+
da = estia_mcstas_pipeline.compute(ReducibleData[SampleRun])
53+
assert da.dims == ('blade', 'wire', 'stripe')
54+
assert da.shape == (14, 32, 64)
55+
assert 'position' in da.coords
56+
assert 'sample_rotation' in da.coords
57+
assert 'detector_rotation' in da.coords
58+
assert 'theta' in da.coords
59+
assert 'wavelength' in da.bins.coords
60+
assert 'Q' in da.bins.coords
61+
62+
63+
def test_can_compute_reflectivity_curve(estia_mcstas_pipeline: sciline.Pipeline):
64+
estia_mcstas_pipeline[Filename[SampleRun]] = estia_mcstas_sample_run(11)
65+
estia_mcstas_pipeline[QBins] = sc.geomspace('Q', 0.005, 0.1, 200, unit='1/angstrom')
66+
r = estia_mcstas_pipeline.compute(ReflectivityOverQ)
67+
assert "Q" in r.coords
68+
assert "Q_resolution" in r.coords
69+
r = r.hist()
70+
min_q = sc.where(
71+
r.data > sc.scalar(0),
72+
sc.midpoints(r.coords['Q']),
73+
sc.scalar(np.nan, unit='1/angstrom'),
74+
).nanmin()
75+
max_q = sc.where(
76+
r.data > sc.scalar(0),
77+
sc.midpoints(r.coords['Q']),
78+
sc.scalar(np.nan, unit='1/angstrom'),
79+
).nanmax()
80+
81+
assert max_q > sc.scalar(0.075, unit='1/angstrom')
82+
assert min_q < sc.scalar(0.007, unit='1/angstrom')

0 commit comments

Comments
 (0)