-
Notifications
You must be signed in to change notification settings - Fork 80
silx.io.open: Added basic support for tiled URLs #4121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
t20100
wants to merge
13
commits into
silx-kit:main
Choose a base branch
from
t20100:tiled
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f484137
Add basic wrapper of tiled
t20100 70ea52d
simple integration of tiled
t20100 24e18f8
Load data on demand
t20100 3d50223
Keep tiled: prefix in name
t20100 11d2e84
add number of children limit, typing and docstring
t20100 13125ae
Change prefix from tiled: to tiled- to avoid issues with url parsing
t20100 0819f5b
Rework silx.io.open to support tiled URL without prefix and try both
t20100 0c2c05f
Update docstrings + rework way to clip items
t20100 7ccc86b
rename MAX_CHILDREN to MAX_CHILDREN_PER_GROUP
t20100 f2b248b
remove support of tiled- prefix
t20100 0f74562
inherit from Dataset rather than lazy-loaded one
t20100 f82c450
try using tiled cache
t20100 cdf4af5
Advertise tiled as a preview feature
t20100 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| # /*########################################################################## | ||
| # Copyright (C) 2024 European Synchrotron Radiation Facility | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in | ||
| # all copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| # THE SOFTWARE. | ||
| # | ||
| # ############################################################################*/ | ||
| """ | ||
| Provides a wrapper to expose `Tiled <https://blueskyproject.io/tiled/>`_ | ||
|
|
||
| This is a preview feature. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
|
|
||
| from functools import lru_cache | ||
| import logging | ||
| import numpy | ||
| from . import commonh5 | ||
| import h5py | ||
| import tiled.client | ||
| from tiled.client.cache import Cache | ||
|
|
||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _get_children( | ||
| parent: TiledH5 | TiledGroup, | ||
| container: tiled.client.container.Container, | ||
| max_children: int | None = None, | ||
| ): | ||
| """Return first max_children entries of given container as a dict of commonh5 wrappers. | ||
|
|
||
| :param parent: The commonh5 wrapper for which to retrieve children. | ||
| :param container: The tiled container from which to retrieve the entries. | ||
| :param max_children: The maximum number of children to retrieve. | ||
| """ | ||
| items = container.items() | ||
|
|
||
| if max_children is not None and len(items) > max_children: | ||
| items = items.head(max_children) | ||
| _logger.warning( | ||
| f"{container.uri} contains too many entries: Only loading first {max_children}." | ||
| ) | ||
|
|
||
| children = {} | ||
| for key, client in items: | ||
| if isinstance(client, tiled.client.container.Container): | ||
| children[key] = TiledGroup(client, name=key, parent=parent) | ||
| elif isinstance(client, tiled.client.array.ArrayClient): | ||
| children[key] = TiledDataset(client, name=key, parent=parent) | ||
| else: | ||
| _logger.warning(f"Unsupported child type: {key}: {client}") | ||
| children[key] = commonh5.Dataset( | ||
| key, | ||
| numpy.array("Unsupported", h5py.special_dtype(vlen=str)), | ||
| parent=parent, | ||
| ) | ||
| return children | ||
|
|
||
|
|
||
| class TiledH5(commonh5.File): | ||
| """tiled client wrapper""" | ||
|
|
||
| MAX_CHILDREN_PER_GROUP: int | None = None | ||
| """Maximum number of children to instantiate for each group. | ||
|
|
||
| Set to None for allowing an unbound number of children per group. | ||
| """ | ||
|
|
||
| _cache = None | ||
| """Shared tiled cache with lazy initialization""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| name: str, | ||
| mode: str | None = None, | ||
| attrs: dict | None = None, | ||
| ): | ||
| assert mode in ("r", None) | ||
| super().__init__(name, mode, attrs) | ||
| if self._cache is None: | ||
| TiledH5._cache = Cache() # Use tiled cache default | ||
|
t20100 marked this conversation as resolved.
|
||
| self.__container = tiled.client.from_uri(name, cache=self._cache) | ||
| assert isinstance(self.__container, tiled.client.container.Container) | ||
| _logger.warning("tiled support is a preview feature: This may change or be removed without notice.") | ||
|
|
||
| def close(self): | ||
| super().close() | ||
| self.__container = None | ||
|
|
||
| @lru_cache | ||
| def _get_items(self): | ||
| return _get_children(self, self.__container, self.MAX_CHILDREN_PER_GROUP) | ||
|
|
||
|
|
||
| class TiledGroup(commonh5.Group): | ||
| """tiled Container wrapper""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| container: tiled.client.container.Container, | ||
| name: str, | ||
| parent: TiledH5 | TiledGroup | None = None, | ||
| attrs: dict | None = None, | ||
| ): | ||
| super().__init__(name, parent, attrs) | ||
| self.__container = container | ||
|
|
||
| @lru_cache | ||
| def _get_items(self): | ||
| return _get_children(self, self.__container, self.file.MAX_CHILDREN_PER_GROUP) | ||
|
|
||
|
|
||
| class TiledDataset(commonh5.Dataset): | ||
| """tiled ArrayClient wrapper""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| client: tiled.client.array.ArrayClient, | ||
| name: str, | ||
| parent: TiledH5 | TiledGroup | None = None, | ||
| attrs: dict | None = None, | ||
| ): | ||
| super().__init__(name, client, parent, attrs) | ||
|
|
||
| @property | ||
| def shape(self): | ||
| return self._get_data().shape | ||
|
|
||
| @property | ||
| def size(self): | ||
| return self._get_data().size | ||
|
|
||
| def __len__(self): | ||
| return len(self.__client) | ||
|
|
||
| def __getitem__(self, item): | ||
| return self._get_data()[item] | ||
|
|
||
| @property | ||
| def value(self): | ||
| return self._get_data()[()] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.