|
2 | 2 | # Copyright (c) 2025 Scipp contributors (https://github.com/scipp) |
3 | 3 | """Data files bundled with ESSreduce.""" |
4 | 4 |
|
5 | | -from functools import cache |
6 | 5 | from pathlib import Path |
7 | 6 |
|
| 7 | +from ._registry import Entry, LocalRegistry, PoochRegistry, Registry, make_registry |
8 | 8 |
|
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', |
77 | 11 | files={ |
78 | 12 | "BIFROST_20240914T053723.h5": "md5:0f2fa5c9a851f8e3a4fa61defaa3752e", |
79 | 13 | }, |
80 | 14 | version='1', |
81 | 15 | ) |
82 | 16 |
|
83 | 17 |
|
84 | | -_dream_registry = Registry( |
85 | | - instrument='dream', |
| 18 | +_dream_registry = make_registry( |
| 19 | + 'ess/dream', |
86 | 20 | files={ |
87 | 21 | "TEST_977695_00068064.hdf": "md5:9e6ee9ec70d7c5e8c0c93b9e07e8949f", |
88 | 22 | }, |
89 | 23 | version='2', |
90 | 24 | ) |
91 | 25 |
|
92 | 26 |
|
93 | | -_loki_registry = Registry( |
94 | | - instrument='loki', |
| 27 | +_loki_registry = make_registry( |
| 28 | + 'ess/loki', |
95 | 29 | files={ |
96 | 30 | # Files from LoKI@Larmor detector test experiment |
97 | 31 | # |
@@ -152,3 +86,19 @@ def dream_coda_test_file() -> Path: |
152 | 86 | See ``tools/shrink_nexus.py``. |
153 | 87 | """ |
154 | 88 | 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