Skip to content

Commit d47760a

Browse files
authored
Exposing attr in open api (#80)
* Exposing attr in open api
1 parent ebdb115 commit d47760a

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

tests/integration/converters/test_ome_tiff.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def test_ome_tiff_converter(tmp_path, open_fileobj):
2222
with open(input_path, "rb") as f:
2323
OMETiffConverter.to_tiledb(f, output_path)
2424
else:
25-
print(output_path)
2625
OMETiffConverter.to_tiledb(input_path, output_path)
2726

2827
with TileDBOpenSlide(output_path) as t:

tests/unit/test_openslide.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import random
22

3+
import pytest
4+
35
import tiledb
46
from tests import get_schema
57
from tiledb.bioimg.helpers import open_bioimg
68
from tiledb.bioimg.openslide import TileDBOpenSlide
79

10+
from .. import ATTR_NAME
11+
812

913
class TestTileDBOpenSlide:
1014
def test(self, tmp_path):
@@ -26,3 +30,11 @@ def r():
2630
with TileDBOpenSlide(group_path) as t:
2731
assert t.level_count == len(level_dimensions)
2832
assert t.level_dimensions == tuple(level_dimensions)
33+
34+
with TileDBOpenSlide(group_path, attr=ATTR_NAME) as t:
35+
assert t.level_count == len(level_dimensions)
36+
assert t.level_dimensions == tuple(level_dimensions)
37+
38+
with pytest.raises(KeyError) as e_info:
39+
_ = TileDBOpenSlide(group_path, attr="test_attr_name")
40+
assert "No attribute matching 'test_attr_name'" in str(e_info.value)

tiledb/bioimg/converters/base.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import tiledb
1919
from tiledb.cc import WebpInputFormat
2020

21+
from .. import ATTR_NAME
2122
from ..helpers import (
2223
ReadWriteGroup,
2324
create_image_pyramid,
@@ -135,7 +136,12 @@ class ImageConverter:
135136

136137
@classmethod
137138
def from_tiledb(
138-
cls, input_path: str, output_path: str, *, level_min: int = 0
139+
cls,
140+
input_path: str,
141+
output_path: str,
142+
*,
143+
level_min: int = 0,
144+
attr: str = ATTR_NAME,
139145
) -> None:
140146
"""
141147
Convert a TileDB Group of Arrays back to other format images, one per level.
@@ -144,11 +150,12 @@ def from_tiledb(
144150
:param output_path: path to the image
145151
:param level_min: minimum level of the image to be converted. By default set to 0
146152
to convert all levels.
153+
:param attr: attribute name for backwards compatiblity support
147154
"""
148155
if cls._ImageWriterType is None:
149156
raise NotImplementedError(f"{cls} does not support exporting")
150157

151-
slide = TileDBOpenSlide(input_path)
158+
slide = TileDBOpenSlide(input_path, attr=attr)
152159
writer = cls._ImageWriterType(output_path)
153160
with slide, writer:
154161
writer.write_group_metadata(slide.properties)

tiledb/bioimg/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def get_or_create(self, name: str, schema: tiledb.ArraySchema) -> Tuple[str, boo
7373
return uri, create
7474

7575

76-
def open_bioimg(uri: str, mode: str = "r") -> tiledb.Array:
77-
return tiledb.open(uri, mode=mode, attr=ATTR_NAME if mode == "r" else None)
76+
def open_bioimg(uri: str, mode: str = "r", attr: str = ATTR_NAME) -> tiledb.Array:
77+
return tiledb.open(uri, mode=mode, attr=attr if mode == "r" else None)
7878

7979

8080
def get_schema(

tiledb/bioimg/openslide.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,30 @@
1313

1414
import tiledb
1515

16+
from . import ATTR_NAME
1617
from .converters.axes import Axes
1718
from .helpers import open_bioimg
1819

1920

2021
class TileDBOpenSlide:
2122
@classmethod
22-
def from_group_uri(cls, uri: str) -> TileDBOpenSlide:
23+
def from_group_uri(cls, uri: str, attr: str = ATTR_NAME) -> TileDBOpenSlide:
2324
warnings.warn(
2425
"This method is deprecated, please use TileDBOpenSlide() instead",
2526
DeprecationWarning,
2627
stacklevel=2,
2728
)
28-
return cls(uri)
29+
return cls(uri, attr=attr)
2930

30-
def __init__(self, uri: str):
31+
def __init__(self, uri: str, *, attr: str = ATTR_NAME):
3132
"""Open this TileDBOpenSlide.
3233
3334
:param uri: uri of a tiledb.Group containing the image
3435
"""
3536
self._group = tiledb.Group(uri)
3637
pixel_depth = self._group.meta.get("pixel_depth", 1)
3738
self._levels = sorted(
38-
(TileDBOpenSlideLevel(o.uri, pixel_depth) for o in self._group),
39+
(TileDBOpenSlideLevel(o.uri, pixel_depth, attr=attr) for o in self._group),
3940
key=attrgetter("level"),
4041
)
4142

@@ -154,8 +155,8 @@ def _read_image(
154155

155156

156157
class TileDBOpenSlideLevel:
157-
def __init__(self, uri: str, pixel_depth: int):
158-
self._tdb = open_bioimg(uri)
158+
def __init__(self, uri: str, pixel_depth: int, *, attr: str):
159+
self._tdb = open_bioimg(uri, attr=attr)
159160
self._pixel_depth = pixel_depth
160161

161162
@property

0 commit comments

Comments
 (0)