|
3 | 3 | import random
|
4 | 4 | from pathlib import Path
|
5 | 5 |
|
| 6 | +import h5py |
6 | 7 | import numpy
|
7 | 8 | import pytest
|
8 | 9 | import tifffile
|
9 | 10 | import yaml
|
10 | 11 |
|
| 12 | +from ..adapters.hdf5 import HDF5Adapter |
11 | 13 | from ..adapters.tiff import TiffAdapter
|
12 | 14 | from ..catalog import in_memory
|
13 | 15 | from ..catalog.register import (
|
@@ -237,3 +239,57 @@ def read_tiff_with_yaml_metadata(
|
237 | 239 | client = from_context(context)
|
238 | 240 | assert numpy.array_equal(data, client["image"][:])
|
239 | 241 | assert client["image"].metadata["test_key"] == 3.0
|
| 242 | + |
| 243 | + |
| 244 | +@pytest.mark.asyncio |
| 245 | +async def test_hdf5_virtual_datasets(tmpdir): |
| 246 | + layout = h5py.VirtualLayout(shape=(4, 100), dtype="i4") |
| 247 | + |
| 248 | + data_filepaths = [] |
| 249 | + for n in range(1, 5): |
| 250 | + filepath = Path(tmpdir, f"{n}.h5") |
| 251 | + data_filepaths.append(filepath) |
| 252 | + vsource = h5py.VirtualSource(filepath, "data", shape=(100,)) |
| 253 | + layout[n - 1] = vsource |
| 254 | + |
| 255 | + # Add virtual dataset to output file |
| 256 | + filepath = Path(tmpdir, "VDS.h5") |
| 257 | + with h5py.File(filepath, "w", libver="latest") as file: |
| 258 | + file.create_virtual_dataset("data", layout, fillvalue=-5) |
| 259 | + |
| 260 | + assets = [ |
| 261 | + Asset( |
| 262 | + data_uri=str(ensure_uri(str(fp))), |
| 263 | + is_directory=False, |
| 264 | + parameter=None, # an indirect dependency |
| 265 | + ) |
| 266 | + for fp in data_filepaths |
| 267 | + ] |
| 268 | + assets.append( |
| 269 | + Asset( |
| 270 | + data_uri=str(ensure_uri(str(filepath))), |
| 271 | + is_directory=False, |
| 272 | + parameter="filepath", |
| 273 | + ) |
| 274 | + ) |
| 275 | + catalog = in_memory(writable_storage=tmpdir) |
| 276 | + with Context.from_app(build_app(catalog)) as context: |
| 277 | + adapter = HDF5Adapter.from_filepath(filepath) |
| 278 | + await create_node_safe( |
| 279 | + catalog, |
| 280 | + key="VDS", |
| 281 | + structure_family=adapter.structure_family, |
| 282 | + metadata=dict(adapter.metadata()), |
| 283 | + specs=adapter.specs, |
| 284 | + data_sources=[ |
| 285 | + DataSource( |
| 286 | + mimetype="application/x-hdf5", |
| 287 | + structure=None, |
| 288 | + parameters={}, |
| 289 | + management=Management.external, |
| 290 | + assets=assets, |
| 291 | + ) |
| 292 | + ], |
| 293 | + ) |
| 294 | + client = from_context(context) |
| 295 | + client["VDS"]["data"][:] |
0 commit comments