Skip to content

Commit f4d0008

Browse files
authored
Merge pull request #261 from scipp/pooch-registry
Cache paths in Pooch
2 parents 300c26a + 571a839 commit f4d0008

File tree

1 file changed

+56
-16
lines changed

1 file changed

+56
-16
lines changed

src/ess/reduce/data.py

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,75 @@
11
# SPDX-License-Identifier: BSD-3-Clause
2-
# Copyright (c) 2024 Scipp contributors (https://github.com/scipp)
3-
import pooch
2+
# Copyright (c) 2025 Scipp contributors (https://github.com/scipp)
3+
"""Data files bundled with ESSreduce."""
4+
5+
from functools import cache
6+
from pathlib import Path
47

58

69
class Registry:
7-
def __init__(self, instrument: str, files: dict[str, str], version: str):
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+
827
self._registry = pooch.create(
928
path=pooch.os_cache(f'ess/{instrument}'),
1029
env=f'ESS_{instrument.upper()}_DATA_DIR',
1130
base_url=f'https://public.esss.dk/groups/scipp/ess/{instrument}/'
1231
+ '{version}/',
1332
version=version,
1433
registry=files,
15-
retry_if_failed=3,
34+
retry_if_failed=retry_if_failed,
1635
)
36+
self._unzip_processor = pooch.Unzip()
1737

18-
def __contains__(self, key):
38+
def __contains__(self, key: str) -> bool:
39+
"""Return True if the key is in the registry."""
1940
return key in self._registry.registry
2041

21-
def get_path(self, name: str, unzip: bool = False) -> str:
22-
"""
23-
Get the path to a file in the registry.
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.
2455
2556
Parameters
2657
----------
2758
name:
2859
Name of the file to get the path for.
2960
unzip:
3061
If `True`, unzip the file before returning the path.
62+
63+
Returns
64+
-------
65+
:
66+
The Path to the file.
3167
"""
32-
return self._registry.fetch(name, processor=pooch.Unzip() if unzip else None)
68+
return Path(
69+
self._registry.fetch(
70+
name, processor=self._unzip_processor if unzip else None
71+
)
72+
)
3373

3474

3575
_bifrost_registry = Registry(
@@ -76,37 +116,37 @@ def get_path(self, name: str, unzip: bool = False) -> str:
76116
)
77117

78118

79-
def bifrost_simulated_elastic() -> str:
119+
def bifrost_simulated_elastic() -> Path:
80120
"""McStas simulation with elastic incoherent scattering + phonon."""
81121
return _bifrost_registry.get_path('BIFROST_20240914T053723.h5')
82122

83123

84-
def loki_tutorial_sample_run_60250() -> str:
124+
def loki_tutorial_sample_run_60250() -> Path:
85125
"""Sample run with sample and sample holder/can, no transmission monitor in beam."""
86126
return _loki_registry.get_path('60250-2022-02-28_2215.nxs')
87127

88128

89-
def loki_tutorial_sample_run_60339() -> str:
129+
def loki_tutorial_sample_run_60339() -> Path:
90130
"""Sample run with sample and sample holder/can, no transmission monitor in beam."""
91131
return _loki_registry.get_path('60339-2022-02-28_2215.nxs')
92132

93133

94-
def loki_tutorial_background_run_60248() -> str:
134+
def loki_tutorial_background_run_60248() -> Path:
95135
"""Background run with sample holder/can only, no transmission monitor."""
96136
return _loki_registry.get_path('60248-2022-02-28_2215.nxs')
97137

98138

99-
def loki_tutorial_background_run_60393() -> str:
139+
def loki_tutorial_background_run_60393() -> Path:
100140
"""Background run with sample holder/can only, no transmission monitor."""
101141
return _loki_registry.get_path('60393-2022-02-28_2215.nxs')
102142

103143

104-
def loki_tutorial_sample_transmission_run() -> str:
144+
def loki_tutorial_sample_transmission_run() -> Path:
105145
"""Sample transmission run (sample + sample holder/can + transmission monitor)."""
106146
return _loki_registry.get_path('60394-2022-02-28_2215.nxs')
107147

108148

109-
def dream_coda_test_file() -> str:
149+
def dream_coda_test_file() -> Path:
110150
"""CODA file for DREAM where most pulses have been removed.
111151
112152
See ``tools/shrink_nexus.py``.

0 commit comments

Comments
 (0)