datamonger 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- datamonger/__init__.py +41 -0
- datamonger/_api.py +589 -0
- datamonger/_cache.py +557 -0
- datamonger/_cache_management.py +258 -0
- datamonger/_canonical.py +235 -0
- datamonger/_data/__init__.py +1 -0
- datamonger/_data/index.json +1 -0
- datamonger/_decode.py +337 -0
- datamonger/_decode_libsvm.py +275 -0
- datamonger/_errors.py +73 -0
- datamonger/_models.py +296 -0
- datamonger/_registry.py +331 -0
- datamonger/_selection.py +84 -0
- datamonger/_validate.py +34 -0
- datamonger/errors.py +43 -0
- datamonger/py.typed +1 -0
- datamonger-0.1.0.dist-info/METADATA +319 -0
- datamonger-0.1.0.dist-info/RECORD +20 -0
- datamonger-0.1.0.dist-info/WHEEL +4 -0
- datamonger-0.1.0.dist-info/licenses/LICENSE +21 -0
datamonger/__init__.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""Datamonger Python reference client."""
|
|
2
|
+
|
|
3
|
+
from datamonger._api import data_info, fetch_artifact, fetch_data, list_data
|
|
4
|
+
from datamonger._cache_management import cache_clean, cache_info
|
|
5
|
+
from datamonger._models import (
|
|
6
|
+
CacheCleanResult,
|
|
7
|
+
CacheEntry,
|
|
8
|
+
CacheInfo,
|
|
9
|
+
DataInfo,
|
|
10
|
+
DatasetData,
|
|
11
|
+
FetchInfo,
|
|
12
|
+
FetchResult,
|
|
13
|
+
Registry,
|
|
14
|
+
SparseDataset,
|
|
15
|
+
SparseDatasetSplit,
|
|
16
|
+
)
|
|
17
|
+
from datamonger._registry import BUNDLED_REGISTRY, resolve_registry
|
|
18
|
+
from datamonger._selection import active_registry, set_registry
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"BUNDLED_REGISTRY",
|
|
22
|
+
"CacheCleanResult",
|
|
23
|
+
"CacheEntry",
|
|
24
|
+
"CacheInfo",
|
|
25
|
+
"DataInfo",
|
|
26
|
+
"DatasetData",
|
|
27
|
+
"FetchInfo",
|
|
28
|
+
"FetchResult",
|
|
29
|
+
"Registry",
|
|
30
|
+
"SparseDataset",
|
|
31
|
+
"SparseDatasetSplit",
|
|
32
|
+
"active_registry",
|
|
33
|
+
"cache_clean",
|
|
34
|
+
"cache_info",
|
|
35
|
+
"data_info",
|
|
36
|
+
"fetch_artifact",
|
|
37
|
+
"fetch_data",
|
|
38
|
+
"list_data",
|
|
39
|
+
"resolve_registry",
|
|
40
|
+
"set_registry",
|
|
41
|
+
]
|
datamonger/_api.py
ADDED
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
"""Public retrieval API for the Python vertical proof."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterator, Mapping, Sequence
|
|
6
|
+
from contextlib import ExitStack, contextmanager
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any, Literal, overload
|
|
9
|
+
|
|
10
|
+
from datamonger._cache import (
|
|
11
|
+
default_cache_root,
|
|
12
|
+
verified_cache_lease,
|
|
13
|
+
verified_download_lease,
|
|
14
|
+
)
|
|
15
|
+
from datamonger._canonical import canonical_sha256
|
|
16
|
+
from datamonger._decode import decode_delimited_text
|
|
17
|
+
from datamonger._decode_libsvm import decode_libsvm, decode_libsvm_split
|
|
18
|
+
from datamonger._errors import (
|
|
19
|
+
ArtifactIntegrityError,
|
|
20
|
+
ArtifactSelectionError,
|
|
21
|
+
ArtifactUnavailableError,
|
|
22
|
+
DecodedIntegrityError,
|
|
23
|
+
OfflineError,
|
|
24
|
+
RetrievalError,
|
|
25
|
+
RetrievalLocationsError,
|
|
26
|
+
UnsupportedDecoderError,
|
|
27
|
+
UnsupportedRegistryError,
|
|
28
|
+
)
|
|
29
|
+
from datamonger._models import (
|
|
30
|
+
DataInfo,
|
|
31
|
+
DatasetData,
|
|
32
|
+
DecodedSparseDataset,
|
|
33
|
+
DecodedSparseDatasetSplit,
|
|
34
|
+
DecodedTable,
|
|
35
|
+
FetchInfo,
|
|
36
|
+
FetchResult,
|
|
37
|
+
LogicalComponent,
|
|
38
|
+
LogicalSparseMatrix,
|
|
39
|
+
LogicalValueComponent,
|
|
40
|
+
Pathish,
|
|
41
|
+
Registry,
|
|
42
|
+
)
|
|
43
|
+
from datamonger._registry import load_registry, resolve_dataset
|
|
44
|
+
from datamonger._selection import active_registry
|
|
45
|
+
from datamonger._validate import (
|
|
46
|
+
require_array,
|
|
47
|
+
require_integer,
|
|
48
|
+
require_mapping,
|
|
49
|
+
require_string,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _object(value: object, field: str) -> Mapping[str, Any]:
|
|
54
|
+
return require_mapping(value, field, UnsupportedRegistryError)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _array(value: object, field: str) -> Sequence[object]:
|
|
58
|
+
return require_array(value, field, UnsupportedRegistryError)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _string(value: object, field: str) -> str:
|
|
62
|
+
return require_string(value, field, UnsupportedRegistryError)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _integer(value: object, field: str) -> int:
|
|
66
|
+
integer = require_integer(value, field, UnsupportedRegistryError)
|
|
67
|
+
if not 0 <= integer <= 2**53 - 1:
|
|
68
|
+
raise UnsupportedRegistryError(f"{field} must be an exact JSON integer")
|
|
69
|
+
return integer
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _load_registry(
|
|
73
|
+
registry: Registry, cache_root: Path, *, offline: bool
|
|
74
|
+
) -> Mapping[str, Any]:
|
|
75
|
+
if offline:
|
|
76
|
+
return load_registry(registry, cache_root, offline=True)
|
|
77
|
+
return load_registry(registry, cache_root)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _artifacts(dataset: Mapping[str, Any]) -> list[Mapping[str, Any]]:
|
|
81
|
+
return [
|
|
82
|
+
_object(raw_artifact, "artifact")
|
|
83
|
+
for raw_artifact in _array(dataset.get("artifacts"), "dataset.artifacts")
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _available_artifacts(artifacts: Sequence[Mapping[str, Any]]) -> str:
|
|
88
|
+
return ", ".join(
|
|
89
|
+
_string(artifact.get("name"), "artifact name") for artifact in artifacts
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _select_artifact(
|
|
94
|
+
dataset: Mapping[str, Any], artifact_name: str | None
|
|
95
|
+
) -> Mapping[str, Any]:
|
|
96
|
+
artifacts = _artifacts(dataset)
|
|
97
|
+
available = _available_artifacts(artifacts)
|
|
98
|
+
if artifact_name is None:
|
|
99
|
+
if len(artifacts) == 1:
|
|
100
|
+
return artifacts[0]
|
|
101
|
+
raise ArtifactSelectionError(
|
|
102
|
+
f"artifact name is required; available artifacts: {available or '(none)'}"
|
|
103
|
+
)
|
|
104
|
+
for artifact in artifacts:
|
|
105
|
+
if artifact.get("name") == artifact_name:
|
|
106
|
+
return artifact
|
|
107
|
+
raise ArtifactSelectionError(
|
|
108
|
+
f"unknown artifact {artifact_name!r}; "
|
|
109
|
+
f"available artifacts: {available or '(none)'}"
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _artifacts_for_representation(
|
|
114
|
+
dataset: Mapping[str, Any],
|
|
115
|
+
representation: Mapping[str, Any],
|
|
116
|
+
roles: Sequence[str],
|
|
117
|
+
) -> tuple[Mapping[str, Any], ...]:
|
|
118
|
+
inputs = _object(representation.get("inputs"), "representation.inputs")
|
|
119
|
+
if set(inputs) != set(roles):
|
|
120
|
+
raise UnsupportedRegistryError(
|
|
121
|
+
f"representation inputs must be exactly {list(roles)!r}"
|
|
122
|
+
)
|
|
123
|
+
artifacts = _artifacts(dataset)
|
|
124
|
+
selected: list[Mapping[str, Any]] = []
|
|
125
|
+
for role in roles:
|
|
126
|
+
artifact_name = _string(inputs.get(role), f"representation.inputs.{role}")
|
|
127
|
+
for artifact in artifacts:
|
|
128
|
+
if artifact.get("name") == artifact_name:
|
|
129
|
+
selected.append(artifact)
|
|
130
|
+
break
|
|
131
|
+
else:
|
|
132
|
+
raise UnsupportedRegistryError(
|
|
133
|
+
f"representation input refers to unknown artifact {artifact_name!r}"
|
|
134
|
+
)
|
|
135
|
+
return tuple(selected)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _libsvm_compression(artifact: Mapping[str, Any]) -> str:
|
|
139
|
+
artifact_format = artifact.get("format")
|
|
140
|
+
if artifact_format not in {"libsvm", "svmlight"}:
|
|
141
|
+
raise UnsupportedDecoderError(
|
|
142
|
+
"LIBSVM decoding requires a LIBSVM or SVMLight artifact"
|
|
143
|
+
)
|
|
144
|
+
compression = artifact.get("compression")
|
|
145
|
+
if not isinstance(compression, str) or compression not in {
|
|
146
|
+
"none",
|
|
147
|
+
"gzip",
|
|
148
|
+
"bzip2",
|
|
149
|
+
}:
|
|
150
|
+
raise UnsupportedDecoderError(
|
|
151
|
+
f"unsupported LIBSVM compression: {compression!r}"
|
|
152
|
+
)
|
|
153
|
+
return compression
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@contextmanager
|
|
157
|
+
def _retrieve_artifact_lease(
|
|
158
|
+
artifact: Mapping[str, Any], cache_root: Path, *, offline: bool
|
|
159
|
+
) -> Iterator[Path]:
|
|
160
|
+
artifact_name = _string(artifact.get("name"), "artifact name")
|
|
161
|
+
distribution = artifact.get("distribution")
|
|
162
|
+
if distribution == "metadata-only":
|
|
163
|
+
raise ArtifactUnavailableError(
|
|
164
|
+
f"artifact {artifact_name!r} is metadata-only and cannot be retrieved"
|
|
165
|
+
)
|
|
166
|
+
if distribution not in {"mirror", "upstream-only"}:
|
|
167
|
+
raise UnsupportedRegistryError(
|
|
168
|
+
f"artifact {artifact_name!r} has unsupported distribution {distribution!r}"
|
|
169
|
+
)
|
|
170
|
+
downloads = _array(artifact.get("downloads"), "artifact.downloads")
|
|
171
|
+
if not downloads:
|
|
172
|
+
raise UnsupportedRegistryError("artifact declares no download locations")
|
|
173
|
+
digest = _string(artifact.get("sha256"), "artifact SHA-256")
|
|
174
|
+
size = _integer(artifact.get("size"), "artifact size")
|
|
175
|
+
if offline:
|
|
176
|
+
with verified_cache_lease(
|
|
177
|
+
cache_root=cache_root,
|
|
178
|
+
namespace="objects",
|
|
179
|
+
digest=digest,
|
|
180
|
+
size=size,
|
|
181
|
+
integrity_error=ArtifactIntegrityError,
|
|
182
|
+
unavailable_error=OfflineError,
|
|
183
|
+
description=f"artifact {artifact_name!r}",
|
|
184
|
+
) as path:
|
|
185
|
+
yield path
|
|
186
|
+
return
|
|
187
|
+
# Locations are tried in manifest order; a transport error or an integrity
|
|
188
|
+
# mismatch moves on to the next location, and the final error must still
|
|
189
|
+
# distinguish unavailability from an integrity failure.
|
|
190
|
+
failures: list[str] = []
|
|
191
|
+
integrity_failure = False
|
|
192
|
+
for raw_download in downloads:
|
|
193
|
+
download = _object(raw_download, "artifact download")
|
|
194
|
+
if download.get("kind") not in {"mirror", "upstream"}:
|
|
195
|
+
raise UnsupportedRegistryError("unsupported artifact download kind")
|
|
196
|
+
url = _string(download.get("url"), "artifact download URL")
|
|
197
|
+
stack = ExitStack()
|
|
198
|
+
try:
|
|
199
|
+
path = stack.enter_context(
|
|
200
|
+
verified_download_lease(
|
|
201
|
+
cache_root=cache_root,
|
|
202
|
+
namespace="objects",
|
|
203
|
+
url=url,
|
|
204
|
+
digest=digest,
|
|
205
|
+
size=size,
|
|
206
|
+
integrity_error=ArtifactIntegrityError,
|
|
207
|
+
)
|
|
208
|
+
)
|
|
209
|
+
except RetrievalError as error:
|
|
210
|
+
stack.close()
|
|
211
|
+
integrity_failure |= isinstance(error, ArtifactIntegrityError)
|
|
212
|
+
failures.append(f"{url}: {error}")
|
|
213
|
+
else:
|
|
214
|
+
with stack:
|
|
215
|
+
yield path
|
|
216
|
+
return
|
|
217
|
+
failed = ArtifactIntegrityError if integrity_failure else RetrievalLocationsError
|
|
218
|
+
raise failed(f"all retrieval locations failed: {'; '.join(failures)}")
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _retrieve_artifact(
|
|
222
|
+
artifact: Mapping[str, Any], cache_root: Path, *, offline: bool
|
|
223
|
+
) -> Path:
|
|
224
|
+
with _retrieve_artifact_lease(artifact, cache_root, offline=offline) as path:
|
|
225
|
+
return path
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def fetch_artifact(
|
|
229
|
+
name: str,
|
|
230
|
+
*,
|
|
231
|
+
source: str,
|
|
232
|
+
version: str | None = None,
|
|
233
|
+
artifact: str | None = None,
|
|
234
|
+
registry: Registry | None = None,
|
|
235
|
+
cache_dir: Pathish | None = None,
|
|
236
|
+
offline: bool = False,
|
|
237
|
+
) -> Path:
|
|
238
|
+
"""Resolve and retrieve one verified artifact without decoding it.
|
|
239
|
+
|
|
240
|
+
``artifact`` may be omitted only when the resolved dataset version declares
|
|
241
|
+
exactly one artifact. An explicit registry overrides session and project
|
|
242
|
+
selection. ``offline=True`` permits verified cache hits but performs no
|
|
243
|
+
network requests.
|
|
244
|
+
"""
|
|
245
|
+
|
|
246
|
+
cache_root = Path(cache_dir) if cache_dir is not None else default_cache_root()
|
|
247
|
+
selected_registry = registry if registry is not None else active_registry()
|
|
248
|
+
index = _load_registry(selected_registry, cache_root, offline=offline)
|
|
249
|
+
dataset = resolve_dataset(index, source=source, name=name, version=version)
|
|
250
|
+
selected_artifact = _select_artifact(dataset, artifact)
|
|
251
|
+
return _retrieve_artifact(selected_artifact, cache_root, offline=offline)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _records(value: object, field: str) -> tuple[Mapping[str, Any], ...]:
|
|
255
|
+
return tuple(dict(_object(record, field)) for record in _array(value, field))
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def _optional_records(value: object, field: str) -> tuple[Mapping[str, Any], ...]:
|
|
259
|
+
return () if value is None else _records(value, field)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _data_info(
|
|
263
|
+
dataset: Mapping[str, Any],
|
|
264
|
+
*,
|
|
265
|
+
registry: Registry,
|
|
266
|
+
) -> DataInfo:
|
|
267
|
+
source = _string(dataset.get("source"), "dataset.source")
|
|
268
|
+
name = _string(dataset.get("name"), "dataset.name")
|
|
269
|
+
version = _string(dataset.get("version"), "dataset.version")
|
|
270
|
+
representation = dict(
|
|
271
|
+
_object(dataset.get("representation"), "dataset.representation")
|
|
272
|
+
)
|
|
273
|
+
expect = _object(representation.get("expect"), "representation.expect")
|
|
274
|
+
return DataInfo(
|
|
275
|
+
dataset_id=f"{source}:{name}@{version}",
|
|
276
|
+
source=source,
|
|
277
|
+
name=name,
|
|
278
|
+
version=version,
|
|
279
|
+
registry_release=registry.release,
|
|
280
|
+
registry_index_sha256=registry.index_sha256,
|
|
281
|
+
title=_string(dataset.get("title"), "dataset.title"),
|
|
282
|
+
description=_string(dataset.get("description"), "dataset.description"),
|
|
283
|
+
modality=_string(dataset.get("modality"), "dataset.modality"),
|
|
284
|
+
provenance=dict(_object(dataset.get("provenance"), "dataset.provenance")),
|
|
285
|
+
license=dict(_object(dataset.get("license"), "dataset.license")),
|
|
286
|
+
artifacts=tuple(dict(artifact) for artifact in _artifacts(dataset)),
|
|
287
|
+
representation=representation,
|
|
288
|
+
expected_components=_records(
|
|
289
|
+
expect.get("components"), "representation.expect.components"
|
|
290
|
+
),
|
|
291
|
+
verification_records=_records(
|
|
292
|
+
expect.get("verification"), "representation.expect.verification"
|
|
293
|
+
),
|
|
294
|
+
related=_optional_records(dataset.get("related"), "dataset.related"),
|
|
295
|
+
tasks=_optional_records(dataset.get("tasks"), "dataset.tasks"),
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def data_info(
|
|
300
|
+
name: str,
|
|
301
|
+
*,
|
|
302
|
+
source: str,
|
|
303
|
+
version: str | None = None,
|
|
304
|
+
registry: Registry | None = None,
|
|
305
|
+
cache_dir: Pathish | None = None,
|
|
306
|
+
offline: bool = False,
|
|
307
|
+
) -> DataInfo:
|
|
308
|
+
"""Return registry metadata for the version selected by fetch resolution.
|
|
309
|
+
|
|
310
|
+
This operation retrieves only the selected registry index. An explicit
|
|
311
|
+
registry overrides session and project selection, and ``offline=True``
|
|
312
|
+
permits a bundled or verified cached index without network requests.
|
|
313
|
+
"""
|
|
314
|
+
|
|
315
|
+
cache_root = Path(cache_dir) if cache_dir is not None else default_cache_root()
|
|
316
|
+
selected_registry = registry if registry is not None else active_registry()
|
|
317
|
+
index = _load_registry(selected_registry, cache_root, offline=offline)
|
|
318
|
+
dataset = resolve_dataset(index, source=source, name=name, version=version)
|
|
319
|
+
return _data_info(dataset, registry=selected_registry)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def list_data(
|
|
323
|
+
*,
|
|
324
|
+
registry: Registry | None = None,
|
|
325
|
+
cache_dir: Pathish | None = None,
|
|
326
|
+
offline: bool = False,
|
|
327
|
+
) -> tuple[DataInfo, ...]:
|
|
328
|
+
"""List every dataset version in the selected immutable registry release."""
|
|
329
|
+
|
|
330
|
+
cache_root = Path(cache_dir) if cache_dir is not None else default_cache_root()
|
|
331
|
+
selected_registry = registry if registry is not None else active_registry()
|
|
332
|
+
index = _load_registry(selected_registry, cache_root, offline=offline)
|
|
333
|
+
datasets = []
|
|
334
|
+
for raw_dataset in _array(index.get("datasets"), "registry.datasets"):
|
|
335
|
+
candidate = _object(raw_dataset, "registry dataset")
|
|
336
|
+
source = _string(candidate.get("source"), "dataset.source")
|
|
337
|
+
name = _string(candidate.get("name"), "dataset.name")
|
|
338
|
+
version = _string(candidate.get("version"), "dataset.version")
|
|
339
|
+
datasets.append(
|
|
340
|
+
resolve_dataset(index, source=source, name=name, version=version)
|
|
341
|
+
)
|
|
342
|
+
return tuple(
|
|
343
|
+
_data_info(dataset, registry=selected_registry) for dataset in datasets
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def _validate_components(
|
|
348
|
+
components: Sequence[LogicalValueComponent], expected: Sequence[object]
|
|
349
|
+
) -> None:
|
|
350
|
+
if len(components) != len(expected):
|
|
351
|
+
raise DecodedIntegrityError(
|
|
352
|
+
f"expected {len(expected)} components, decoded {len(components)}"
|
|
353
|
+
)
|
|
354
|
+
for component, raw_expectation in zip(components, expected, strict=True):
|
|
355
|
+
expectation = _object(raw_expectation, "component expectation")
|
|
356
|
+
if isinstance(component, LogicalComponent):
|
|
357
|
+
if expectation.get("kind") != "vector":
|
|
358
|
+
raise DecodedIntegrityError(
|
|
359
|
+
f"decoded component {component.name!r} has the wrong kind"
|
|
360
|
+
)
|
|
361
|
+
matches = (
|
|
362
|
+
expectation.get("name") == component.name
|
|
363
|
+
and expectation.get("type") == component.logical_type
|
|
364
|
+
and expectation.get("length") == len(component.values)
|
|
365
|
+
)
|
|
366
|
+
elif isinstance(component, LogicalSparseMatrix):
|
|
367
|
+
if expectation.get("kind") != "sparse_matrix":
|
|
368
|
+
raise DecodedIntegrityError(
|
|
369
|
+
f"decoded component {component.name!r} has the wrong kind"
|
|
370
|
+
)
|
|
371
|
+
# Canonical form 1 has a single sparse element type, so the
|
|
372
|
+
# normative manifests may omit "type" for sparse matrices.
|
|
373
|
+
matches = (
|
|
374
|
+
expectation.get("name") == component.name
|
|
375
|
+
and expectation.get("type", component.logical_type)
|
|
376
|
+
== component.logical_type
|
|
377
|
+
and expectation.get("rows") == component.rows
|
|
378
|
+
and expectation.get("columns") == component.columns
|
|
379
|
+
)
|
|
380
|
+
else:
|
|
381
|
+
raise UnsupportedDecoderError("unsupported logical component")
|
|
382
|
+
if not matches:
|
|
383
|
+
raise DecodedIntegrityError(
|
|
384
|
+
f"decoded component {component.name!r} does not match its expectation"
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
def _verification_record(
|
|
389
|
+
index: Mapping[str, Any],
|
|
390
|
+
dataset: Mapping[str, Any],
|
|
391
|
+
expect: Mapping[str, Any],
|
|
392
|
+
) -> Mapping[str, Any]:
|
|
393
|
+
identity = {
|
|
394
|
+
"source": _string(dataset.get("source"), "dataset.source"),
|
|
395
|
+
"name": _string(dataset.get("name"), "dataset.name"),
|
|
396
|
+
"version": _string(dataset.get("version"), "dataset.version"),
|
|
397
|
+
}
|
|
398
|
+
revoked: list[Mapping[str, Any]] = []
|
|
399
|
+
for raw_erratum in _array(index.get("errata", ()), "registry.errata"):
|
|
400
|
+
erratum = _object(raw_erratum, "registry erratum")
|
|
401
|
+
if erratum.get("dataset") != identity:
|
|
402
|
+
continue
|
|
403
|
+
target = _object(erratum.get("target"), "erratum.target")
|
|
404
|
+
if target.get("kind") == "verification":
|
|
405
|
+
revoked.append(_object(erratum.get("original"), "erratum.original"))
|
|
406
|
+
|
|
407
|
+
records = _array(expect.get("verification"), "representation.expect.verification")
|
|
408
|
+
for raw_record in reversed(records):
|
|
409
|
+
record = _object(raw_record, "verification record")
|
|
410
|
+
if (
|
|
411
|
+
type(record.get("canonical_form")) is int
|
|
412
|
+
and record.get("canonical_form") == 1
|
|
413
|
+
and record.get("algorithm") == "sha256"
|
|
414
|
+
and record not in revoked
|
|
415
|
+
):
|
|
416
|
+
return record
|
|
417
|
+
raise UnsupportedDecoderError("no supported decoded-verification record")
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
@overload
|
|
421
|
+
def fetch_data(
|
|
422
|
+
name: str,
|
|
423
|
+
*,
|
|
424
|
+
source: str,
|
|
425
|
+
version: str | None = None,
|
|
426
|
+
registry: Registry | None = None,
|
|
427
|
+
cache_dir: Pathish | None = None,
|
|
428
|
+
offline: bool = False,
|
|
429
|
+
verify_decoded: bool = True,
|
|
430
|
+
return_info: Literal[False] = False,
|
|
431
|
+
) -> DatasetData: ...
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
@overload
|
|
435
|
+
def fetch_data(
|
|
436
|
+
name: str,
|
|
437
|
+
*,
|
|
438
|
+
source: str,
|
|
439
|
+
version: str | None = None,
|
|
440
|
+
registry: Registry | None = None,
|
|
441
|
+
cache_dir: Pathish | None = None,
|
|
442
|
+
offline: bool = False,
|
|
443
|
+
verify_decoded: bool = True,
|
|
444
|
+
return_info: Literal[True],
|
|
445
|
+
) -> FetchResult: ...
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
@overload
|
|
449
|
+
def fetch_data(
|
|
450
|
+
name: str,
|
|
451
|
+
*,
|
|
452
|
+
source: str,
|
|
453
|
+
version: str | None = None,
|
|
454
|
+
registry: Registry | None = None,
|
|
455
|
+
cache_dir: Pathish | None = None,
|
|
456
|
+
offline: bool = False,
|
|
457
|
+
verify_decoded: bool = True,
|
|
458
|
+
return_info: bool,
|
|
459
|
+
) -> DatasetData | FetchResult: ...
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
def fetch_data(
|
|
463
|
+
name: str,
|
|
464
|
+
*,
|
|
465
|
+
source: str,
|
|
466
|
+
version: str | None = None,
|
|
467
|
+
registry: Registry | None = None,
|
|
468
|
+
cache_dir: Pathish | None = None,
|
|
469
|
+
offline: bool = False,
|
|
470
|
+
verify_decoded: bool = True,
|
|
471
|
+
return_info: bool = False,
|
|
472
|
+
) -> DatasetData | FetchResult:
|
|
473
|
+
"""Resolve, retrieve, verify, and decode one registered dataset.
|
|
474
|
+
|
|
475
|
+
An explicit registry overrides session and project selection. ``offline=True``
|
|
476
|
+
requires both the selected registry and its artifact to be bundled or cached.
|
|
477
|
+
"""
|
|
478
|
+
|
|
479
|
+
cache_root = Path(cache_dir) if cache_dir is not None else default_cache_root()
|
|
480
|
+
selected_registry = registry if registry is not None else active_registry()
|
|
481
|
+
index = _load_registry(selected_registry, cache_root, offline=offline)
|
|
482
|
+
dataset = resolve_dataset(index, source=source, name=name, version=version)
|
|
483
|
+
resolved_version = _string(dataset.get("version"), "dataset.version")
|
|
484
|
+
representation = _object(dataset.get("representation"), "dataset.representation")
|
|
485
|
+
decoder = representation.get("decoder")
|
|
486
|
+
decoder_version = representation.get("decoder_version")
|
|
487
|
+
if (
|
|
488
|
+
type(decoder_version) is not int
|
|
489
|
+
or decoder_version != 1
|
|
490
|
+
or decoder
|
|
491
|
+
not in {
|
|
492
|
+
"delimited-text",
|
|
493
|
+
"libsvm",
|
|
494
|
+
"libsvm-split",
|
|
495
|
+
}
|
|
496
|
+
):
|
|
497
|
+
raise UnsupportedDecoderError(
|
|
498
|
+
"the Python client supports delimited-text, LIBSVM, and "
|
|
499
|
+
"LIBSVM split version 1"
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
roles = ("train", "test") if decoder == "libsvm-split" else ("data",)
|
|
503
|
+
artifacts = _artifacts_for_representation(dataset, representation, roles)
|
|
504
|
+
options = _object(representation.get("options"), "representation.options")
|
|
505
|
+
compressions: tuple[str, ...]
|
|
506
|
+
if decoder == "delimited-text":
|
|
507
|
+
artifact = artifacts[0]
|
|
508
|
+
artifact_format = artifact.get("format")
|
|
509
|
+
compression = artifact.get("compression")
|
|
510
|
+
if artifact_format not in {"csv", "tsv"}:
|
|
511
|
+
raise UnsupportedDecoderError(
|
|
512
|
+
"delimited-text requires a CSV or TSV artifact"
|
|
513
|
+
)
|
|
514
|
+
expected_delimiter = "," if artifact_format == "csv" else "\t"
|
|
515
|
+
if options.get("delimiter") != expected_delimiter:
|
|
516
|
+
raise UnsupportedDecoderError("artifact format and delimiter disagree")
|
|
517
|
+
if not isinstance(compression, str) or compression not in {
|
|
518
|
+
"none",
|
|
519
|
+
"gzip",
|
|
520
|
+
"bzip2",
|
|
521
|
+
}:
|
|
522
|
+
raise UnsupportedDecoderError(
|
|
523
|
+
f"unsupported delimited-text compression: {compression!r}"
|
|
524
|
+
)
|
|
525
|
+
compressions = (compression,)
|
|
526
|
+
else:
|
|
527
|
+
compressions = tuple(_libsvm_compression(artifact) for artifact in artifacts)
|
|
528
|
+
with ExitStack() as stack:
|
|
529
|
+
artifact_paths = tuple(
|
|
530
|
+
stack.enter_context(
|
|
531
|
+
_retrieve_artifact_lease(artifact, cache_root, offline=offline)
|
|
532
|
+
)
|
|
533
|
+
for artifact in artifacts
|
|
534
|
+
)
|
|
535
|
+
decoded: DecodedTable | DecodedSparseDataset | DecodedSparseDatasetSplit
|
|
536
|
+
if decoder == "delimited-text":
|
|
537
|
+
decoded = decode_delimited_text(
|
|
538
|
+
artifact_paths[0], options, compression=compressions[0]
|
|
539
|
+
)
|
|
540
|
+
elif decoder == "libsvm":
|
|
541
|
+
decoded = decode_libsvm(
|
|
542
|
+
artifact_paths[0], options, compression=compressions[0]
|
|
543
|
+
)
|
|
544
|
+
else:
|
|
545
|
+
decoded = decode_libsvm_split(
|
|
546
|
+
artifact_paths[0],
|
|
547
|
+
artifact_paths[1],
|
|
548
|
+
options,
|
|
549
|
+
train_compression=compressions[0],
|
|
550
|
+
test_compression=compressions[1],
|
|
551
|
+
)
|
|
552
|
+
canonical_form: int | None = None
|
|
553
|
+
canonical_digest: str | None = None
|
|
554
|
+
verification: Literal["artifact", "decoded"] = "artifact"
|
|
555
|
+
if verify_decoded:
|
|
556
|
+
expect = _object(representation.get("expect"), "representation.expect")
|
|
557
|
+
_validate_components(
|
|
558
|
+
decoded.components,
|
|
559
|
+
_array(expect.get("components"), "representation.expect.components"),
|
|
560
|
+
)
|
|
561
|
+
record = _verification_record(index, dataset, expect)
|
|
562
|
+
canonical_form = _integer(record.get("canonical_form"), "canonical form")
|
|
563
|
+
expected_digest = _string(record.get("digest"), "canonical digest")
|
|
564
|
+
canonical_digest = canonical_sha256(decoded.components)
|
|
565
|
+
if canonical_digest != expected_digest:
|
|
566
|
+
raise DecodedIntegrityError(
|
|
567
|
+
f"decoded SHA-256 mismatch: expected {expected_digest}, "
|
|
568
|
+
f"received {canonical_digest}"
|
|
569
|
+
)
|
|
570
|
+
verification = "decoded"
|
|
571
|
+
|
|
572
|
+
dataset_id = f"{source}:{name}@{resolved_version}"
|
|
573
|
+
info = FetchInfo(
|
|
574
|
+
dataset_id=dataset_id,
|
|
575
|
+
registry_release=selected_registry.release,
|
|
576
|
+
registry_index_sha256=selected_registry.index_sha256,
|
|
577
|
+
artifact_digests={
|
|
578
|
+
_string(artifact.get("name"), "artifact name"): _string(
|
|
579
|
+
artifact.get("sha256"), "artifact SHA-256"
|
|
580
|
+
)
|
|
581
|
+
for artifact in artifacts
|
|
582
|
+
},
|
|
583
|
+
verification=verification,
|
|
584
|
+
canonical_form=canonical_form,
|
|
585
|
+
canonical_digest=canonical_digest,
|
|
586
|
+
)
|
|
587
|
+
if return_info:
|
|
588
|
+
return FetchResult(data=decoded.data, info=info)
|
|
589
|
+
return decoded.data
|