Skip to content

Commit b90fd6c

Browse files
committed
MWP blissdata redis_hdf5 support in silx view
1 parent 731158f commit b90fd6c

4 files changed

Lines changed: 107 additions & 1 deletion

File tree

src/silx/io/blissdatah5.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from __future__ import annotations
2+
3+
import logging
4+
from typing import Generator
5+
6+
import numpy
7+
8+
from . import commonh5
9+
10+
from blissdata.h5api import abstract as abc
11+
from blissdata.h5api.redis_hdf5 import File
12+
13+
14+
_logger = logging.getLogger(__name__)
15+
logging.getLogger("blissdata").setLevel(logging.DEBUG)
16+
17+
18+
class BlissDataH5(commonh5.File):
19+
def __init__(
20+
self,
21+
name: str,
22+
mode: str | None = None,
23+
attrs: dict | None = None,
24+
) -> None:
25+
assert mode in ("r", None)
26+
27+
if attrs is None:
28+
attrs = {}
29+
30+
self.__file = File(name)
31+
32+
super().__init__(name, mode, attrs={**self.__file.attrs, **attrs})
33+
34+
for child in _children(self.__file):
35+
self.add_node(child)
36+
37+
_logger.warning(
38+
"blissdata support is a preview feature: This may change or be removed without notice."
39+
)
40+
41+
def close(self) -> None:
42+
super().close()
43+
self.__file.close()
44+
self.__file = None
45+
46+
47+
class BlissDataGroup(commonh5.LazyLoadableGroup):
48+
def __init__(
49+
self,
50+
name: str,
51+
group: abc.Group,
52+
parent: BlissDataH5 | BlissDataGroup | None = None,
53+
attrs: dict | None = None,
54+
) -> None:
55+
super().__init__(name, parent, attrs)
56+
self.__group = group
57+
58+
def _create_child(self) -> None:
59+
for child in _children(self.__group):
60+
self.add_node(child)
61+
62+
63+
class BlissDataDataset(commonh5.Dataset):
64+
65+
@property
66+
def shape(self) -> tuple[int, ...]:
67+
return self._get_data().shape
68+
69+
@property
70+
def size(self) -> int:
71+
return self._get_data().size
72+
73+
def __len__(self) -> int:
74+
return len(self._get_data())
75+
76+
def __getitem__(self, item):
77+
print("getitem", item)
78+
if isinstance(item, tuple) and len(item):
79+
print("special case", item)
80+
return self._get_data()[()][item]
81+
return self._get_data()[item]
82+
83+
@property
84+
def value(self) -> numpy.ndarray:
85+
return self._get_data()[()]
86+
87+
88+
def _children(group: abc.Group) -> Generator[BlissDataDataset | BlissDataGroup]:
89+
for name in group.keys():
90+
item = group[name]
91+
print("child", name, item)
92+
if isinstance(item, abc.Group):
93+
yield BlissDataGroup(name, item)
94+
elif isinstance(item, abc.Dataset):
95+
yield BlissDataDataset(name, item)
96+
else:
97+
_logger.warning(f"Cannot map child {name}: Ignored")

src/silx/io/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ subdir('specfile')
44
py.install_sources([
55
'__init__.py',
66
'_sliceh5.py',
7+
'blissdatah5.py',
78
'commonh5.py',
89
'configdict.py',
910
'convert.py',

src/silx/io/url.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class DataUrl:
135135
be false.
136136
"""
137137

138-
_SCHEMES = ("fabio", "silx", "http", "https")
138+
_SCHEMES = ("fabio", "silx", "http", "https", "bliss", "blissdata")
139139

140140
def __init__(
141141
self,

src/silx/io/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,14 @@ def open(filename): # pylint:disable=redefined-builtin
699699
h5_file = _open_local_file(url.file_path())
700700
elif url.scheme() in ("http", "https"):
701701
return _open_url_with_h5pyd(filename)
702+
elif url.scheme() in ("bliss", "blissdata"):
703+
try:
704+
from .blissdatah5 import BlissDataH5
705+
except ImportError:
706+
raise IOError(
707+
f"blissdata support is not available, cannot open: {filename}"
708+
)
709+
h5_file = BlissDataH5(url.file_path())
702710
else:
703711
raise OSError(f"Unsupported URL scheme {url.scheme}: {filename}")
704712

0 commit comments

Comments
 (0)