Skip to content
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

Fix tiff reader #292

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion ci/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ dependencies:
- fsspec
- s3fs
- fastparquet
# for opening tiff files
# for opening and creating test tiff files
- tifffile
- pillow
# for opening FITS files
- astropy
13 changes: 13 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,16 @@ def simple_netcdf4(tmpdir):
ds.to_netcdf(filepath)

return filepath


@pytest.fixture
def random_tiff(tmpdir):
from PIL import Image

array = np.random.randint(0, 255, (128, 128), dtype=np.uint8)
img = Image.fromarray(array)

filepath = tmpdir / "rand.tiff"
img.save(filepath)
Comment on lines +129 to +133
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following will save a multi-page TIFF:

Suggested change
array = np.random.randint(0, 255, (128, 128), dtype=np.uint8)
img = Image.fromarray(array)
filepath = tmpdir / "rand.tiff"
img.save(filepath)
array = np.random.randint(0, 255, (5, 128, 128), dtype=np.uint8)
imgs = [Image.fromarray(frame) for frame in array]
filepath = tmpdir / "rand.tiff"
imgs[0].save(filepath, save_all=True, append_images=imgs[1:])


return str(filepath)
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ test = [
"ruff",
"s3fs",
"scipy",
"tifffile",
"pillow",
]


Expand Down
5 changes: 3 additions & 2 deletions virtualizarr/readers/tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
open_loadable_vars_and_indexes,
)
from virtualizarr.translators.kerchunk import (
extract_group,
virtual_vars_and_metadata_from_kerchunk_refs,
)
from virtualizarr.types.kerchunk import KerchunkStoreRefs
Expand Down Expand Up @@ -45,7 +44,9 @@ def open_virtual_dataset(
# handle inconsistency in kerchunk, see GH issue https://github.com/zarr-developers/VirtualiZarr/issues/160
refs = KerchunkStoreRefs({"refs": tiff_to_zarr(filepath, **reader_options)})

refs = extract_group(refs, group)
print(refs)

# refs = extract_group(refs, group)

virtual_vars, attrs, coord_names = virtual_vars_and_metadata_from_kerchunk_refs(
refs,
Expand Down
1 change: 1 addition & 0 deletions virtualizarr/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def _importorskip(
has_s3fs, requires_s3fs = _importorskip("s3fs")
has_scipy, requires_scipy = _importorskip("scipy")
has_tifffile, requires_tifffile = _importorskip("tifffile")
has_pillow, requires_pillow = _importorskip("PIL")


def create_manifestarray(
Expand Down
26 changes: 26 additions & 0 deletions virtualizarr/tests/test_readers/test_tiff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import numpy as np
from xarray import Dataset

from virtualizarr import open_virtual_dataset
from virtualizarr.manifests import ManifestArray
from virtualizarr.tests import requires_pillow


@requires_pillow
def test_random_tiff(random_tiff):
vds = open_virtual_dataset(random_tiff, indexes={})

assert isinstance(vds, Dataset)

# TODO what is the name of this array expected to be??
assert list(vds.variables) == ["foo"]
vda = vds["foo"]

assert vda.sizes == {"X": 128, "Y": 128}
assert vda.dtype == np.uint8

assert isinstance(vda.data, ManifestArray)
manifest = vda.data.manifest
assert manifest.dict() == {
"0.0": {"path": random_tiff, "offset": 122, "length": 16384}
}
Loading