Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/silx/io/blissdatah5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import logging
from typing import Generator, Literal

import numpy

from . import commonh5

from blissdata.h5api import abstract as abc
from blissdata.h5api.redis_hdf5 import File

_logger = logging.getLogger(__name__)


class BlissDataH5(commonh5.File):
def __init__(
self,
name: str,
mode: Literal["r"] | None = None,
attrs: dict | None = None,
) -> None:
if mode not in ("r", None):
raise ValueError(f"Unsupported mode: {mode}")

if attrs is None:
attrs = {}

self.__file = File(name)

super().__init__(name, mode, attrs={**self.__file.attrs, **attrs})

for child in _children(self.__file):
self.add_node(child)

_logger.warning(
"blissdata support is a preview feature: This may change or be removed without notice."
)

def close(self) -> None:
super().close()
self.__file.close()
self.__file = None


class BlissDataGroup(commonh5.LazyLoadableGroup):
def __init__(
self,
name: str,
group: abc.Group,
parent: BlissDataH5 | "BlissDataGroup" | None = None,
attrs: dict | None = None,
) -> None:
super().__init__(name, parent, attrs)
self.__group = group

def _create_child(self) -> None:
for child in _children(self.__group):
self.add_node(child)


class BlissDataDataset(commonh5.Dataset):

@property
def shape(self) -> tuple[int, ...]:
return self._get_data().shape

@property
def size(self) -> int:
return self._get_data().size

def __len__(self) -> int:
return len(self._get_data())

def __getitem__(self, item):
if isinstance(item, tuple) and len(item):
return self._get_data()[()][item]
return self._get_data()[item]

@property
def value(self) -> numpy.ndarray:
return self._get_data()[()]


def _children(group: abc.Group) -> Generator[BlissDataDataset | BlissDataGroup]:
for name in group.keys():
item = group[name]
if isinstance(item, abc.Group):
yield BlissDataGroup(name, item, parent=group, attrs=item.attrs)
elif isinstance(item, abc.Dataset):
yield BlissDataDataset(name, item, parent=group, attrs=item.attrs)
else:
_logger.warning(f"Cannot map child {name}: Ignored")
1 change: 1 addition & 0 deletions src/silx/io/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ subdir('specfile')
py.install_sources([
'__init__.py',
'_sliceh5.py',
'blissdatah5.py',
'commonh5.py',
'configdict.py',
'convert.py',
Expand Down
2 changes: 1 addition & 1 deletion src/silx/io/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class DataUrl:
be false.
"""

_SCHEMES = ("fabio", "silx", "http", "https")
_SCHEMES = ("fabio", "silx", "http", "https", "blissdata")

def __init__(
self,
Expand Down
8 changes: 8 additions & 0 deletions src/silx/io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,14 @@ def open(filename): # pylint:disable=redefined-builtin
h5_file = _open_local_file(url.file_path())
elif url.scheme() in ("http", "https"):
return _open_url_with_h5pyd(filename)
elif url.scheme() == "blissdata":
try:
from .blissdatah5 import BlissDataH5
except ImportError:
raise IOError(
f"blissdata support is not available, cannot open: {filename}"
)
h5_file = BlissDataH5(url.file_path())
else:
raise OSError(f"Unsupported URL scheme {url.scheme}: {filename}")

Expand Down
Loading