Skip to content

Commit 80557eb

Browse files
committed
Implement data registry override
1 parent e2d41f6 commit 80557eb

File tree

2 files changed

+314
-73
lines changed

2 files changed

+314
-73
lines changed

src/ess/reduce/data.py renamed to src/ess/reduce/data/__init__.py

Lines changed: 23 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,30 @@
22
# Copyright (c) 2025 Scipp contributors (https://github.com/scipp)
33
"""Data files bundled with ESSreduce."""
44

5-
from functools import cache
65
from pathlib import Path
76

7+
from ._registry import Entry, LocalRegistry, PoochRegistry, Registry, make_registry
88

9-
class Registry:
10-
"""A registry for data files.
11-
12-
Note
13-
----
14-
This class requires [Pooch](https://www.fatiando.org/pooch/latest/) which
15-
is not a hard dependency of ESSreduce and needs to be installed separately.
16-
"""
17-
18-
def __init__(
19-
self,
20-
instrument: str,
21-
files: dict[str, str],
22-
version: str,
23-
retry_if_failed: int = 3,
24-
) -> None:
25-
import pooch
26-
27-
self._registry = pooch.create(
28-
path=pooch.os_cache(f'ess/{instrument}'),
29-
env=f'ESS_{instrument.upper()}_DATA_DIR',
30-
base_url=f'https://public.esss.dk/groups/scipp/ess/{instrument}/'
31-
+ '{version}/',
32-
version=version,
33-
registry=files,
34-
retry_if_failed=retry_if_failed,
35-
)
36-
self._unzip_processor = pooch.Unzip()
37-
38-
def __contains__(self, key: str) -> bool:
39-
"""Return True if the key is in the registry."""
40-
return key in self._registry.registry
41-
42-
@cache # noqa: B019
43-
def get_path(self, name: str, unzip: bool = False) -> Path:
44-
"""Get the path to a file in the registry.
45-
46-
Downloads the file if necessary.
47-
48-
Note that return values of this method are cached to avoid recomputing
49-
potentially expensive checksums.
50-
This usually means that the ``Registry`` object itself gets stored until the
51-
Python interpreter shuts down.
52-
However, registries are small and do not own resources.
53-
It is anyway expected that the registry objects are stored at
54-
module scope and live until program exit.
55-
56-
Parameters
57-
----------
58-
name:
59-
Name of the file to get the path for.
60-
unzip:
61-
If `True`, unzip the file before returning the path.
62-
63-
Returns
64-
-------
65-
:
66-
The Path to the file.
67-
"""
68-
return Path(
69-
self._registry.fetch(
70-
name, processor=self._unzip_processor if unzip else None
71-
)
72-
)
73-
74-
75-
_bifrost_registry = Registry(
76-
instrument='bifrost',
9+
_bifrost_registry = make_registry(
10+
'ess/bifrost',
7711
files={
7812
"BIFROST_20240914T053723.h5": "md5:0f2fa5c9a851f8e3a4fa61defaa3752e",
7913
},
8014
version='1',
8115
)
8216

8317

84-
_dream_registry = Registry(
85-
instrument='dream',
18+
_dream_registry = make_registry(
19+
'ess/dream',
8620
files={
8721
"TEST_977695_00068064.hdf": "md5:9e6ee9ec70d7c5e8c0c93b9e07e8949f",
8822
},
8923
version='2',
9024
)
9125

9226

93-
_loki_registry = Registry(
94-
instrument='loki',
27+
_loki_registry = make_registry(
28+
'ess/loki',
9529
files={
9630
# Files from LoKI@Larmor detector test experiment
9731
#
@@ -152,3 +86,19 @@ def dream_coda_test_file() -> Path:
15286
See ``tools/shrink_nexus.py``.
15387
"""
15488
return _dream_registry.get_path('TEST_977695_00068064.hdf')
89+
90+
91+
__all__ = [
92+
'Entry',
93+
'LocalRegistry',
94+
'PoochRegistry',
95+
'Registry',
96+
'bifrost_simulated_elastic',
97+
'dream_coda_test_file',
98+
'loki_tutorial_background_run_60248',
99+
'loki_tutorial_background_run_60393',
100+
'loki_tutorial_sample_run_60250',
101+
'loki_tutorial_sample_run_60339',
102+
'loki_tutorial_sample_transmission_run',
103+
'make_registry',
104+
]

0 commit comments

Comments
 (0)