Skip to content

Commit fda3aa5

Browse files
committed
Add tests
1 parent 5e58a9a commit fda3aa5

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

src/ess/dream/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
import importlib.metadata
88

9+
from . import data
910
from .io import load_nexus
1011

1112
try:
@@ -16,5 +17,6 @@
1617
del importlib
1718

1819
__all__ = [
20+
"data",
1921
"load_nexus",
2022
]

src/ess/dream/data.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3+
_version = '1'
4+
5+
__all__ = ['get_path']
6+
7+
8+
def _make_pooch():
9+
import pooch
10+
11+
return pooch.create(
12+
path=pooch.os_cache('ess/powgen'),
13+
env='ESS_DATA_DIR',
14+
base_url='https://public.esss.dk/groups/scipp/ess/dream/{version}/',
15+
version=_version,
16+
registry={
17+
'DREAM_nexus_sorted-2023-12-07.nxs': 'md5:22824e14f6eb950d24a720b2a0e2cb66',
18+
},
19+
)
20+
21+
22+
_pooch = _make_pooch()
23+
24+
25+
def get_path(name: str, unzip: bool = False) -> str:
26+
"""
27+
Return the path to a data file bundled with ess.dream.
28+
29+
This function only works with example data and cannot handle
30+
paths to custom files.
31+
"""
32+
import pooch
33+
34+
return _pooch.fetch(name, processor=pooch.Unzip() if unzip else None)

src/ess/dream/io.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
def load_nexus(
1212
path: Union[str, os.PathLike],
13+
*,
1314
load_pixel_shape: bool = False,
1415
entry: str = 'entry',
1516
fold_detectors: bool = True,
@@ -24,10 +25,10 @@ def load_nexus(
2425
2526
- Mounting-unit, cassette, and counter roughly correspond to the azimuthal angle
2627
in the mantle detector. However, counter is reversed in the current files. This
27-
may also be the case in the other detectors
28+
may also be the case in the other detectors.
2829
- The endcap detectors have a irregular structure that cannot be fully folded.
29-
This is not a problem but not again that the counter may be reversed. It is
30-
current not clear if this is a bug in the file.
30+
This is not a problem but note again that the counter may be reversed. It is
31+
currently not clear if this is a bug in the file.
3132
- The high-resolution detector has a very odd numbering scheme. The SANS detector
3233
is using the same, but is not populated in the current files. The scheme
3334
attempts to follows some sort of physical ordering in space (x,y,z), but it

tests/dream/io_test.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3+
import pytest
4+
5+
from ess import dream
6+
7+
8+
@pytest.fixture
9+
def filename():
10+
return dream.data.get_path('DREAM_nexus_sorted-2023-12-07.nxs')
11+
12+
13+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/monitor_bunker")
14+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/monitor_cave")
15+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/polarizer/rate")
16+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/sans_detector")
17+
def test_load_nexus_loads_file(filename):
18+
dg = dream.load_nexus(filename)
19+
assert 'instrument' in dg
20+
instr = dg['instrument']
21+
for name in (
22+
'mantle',
23+
'endcap_backward',
24+
'endcap_forward',
25+
'high_resolution',
26+
'sans',
27+
):
28+
assert f'{name}_detector' in instr
29+
det = instr[f'{name}_detector']
30+
assert 'pixel_shape' not in det
31+
32+
33+
def test_load_nexus_fails_if_entry_not_found(filename):
34+
with pytest.raises(KeyError):
35+
dream.load_nexus(filename, entry='foo')
36+
37+
38+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/monitor_bunker")
39+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/monitor_cave")
40+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/polarizer/rate")
41+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/sans_detector")
42+
def test_load_nexus_folds_detectors_by_default(filename):
43+
dg = dream.load_nexus(filename)
44+
instr = dg['instrument']
45+
# sans_detector is not populated in the current files
46+
for name in ('mantle', 'endcap_backward', 'endcap_forward', 'high_resolution'):
47+
det = instr[f'{name}_detector']
48+
# There may be other dims, but some are irregular and this may be subject to
49+
# change
50+
assert 'strip' in det.dims
51+
52+
53+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/monitor_bunker")
54+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/monitor_cave")
55+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/polarizer/rate")
56+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/sans_detector")
57+
def test_load_nexus_with_disabled_fold(filename):
58+
dg = dream.load_nexus(filename, fold_detectors=False)
59+
instr = dg['instrument']
60+
for name in ('mantle', 'endcap_backward', 'endcap_forward', 'high_resolution'):
61+
det = instr[f'{name}_detector']
62+
assert det.dims == ('detector_number',)
63+
64+
65+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/monitor_bunker")
66+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/monitor_cave")
67+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/polarizer/rate")
68+
@pytest.mark.filterwarnings("ignore:Failed to load /entry/instrument/sans_detector")
69+
def test_load_nexus_with_pixel_shape(filename):
70+
dg = dream.load_nexus(filename, load_pixel_shape=True)
71+
assert 'instrument' in dg
72+
instr = dg['instrument']
73+
# sans_detector is not populated in the current files
74+
for name in ('mantle', 'endcap_backward', 'endcap_forward', 'high_resolution'):
75+
assert f'{name}_detector' in instr
76+
det = instr[f'{name}_detector']
77+
assert 'pixel_shape' in det

0 commit comments

Comments
 (0)