Skip to content

Commit

Permalink
Fix bug due to zarr constructor being function
Browse files Browse the repository at this point in the history
  • Loading branch information
clbarnes committed Nov 13, 2019
1 parent 20befdc commit 8e2b2be
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
31 changes: 29 additions & 2 deletions elf/io/extensions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import functools

import numpy as np

from .knossos_wrapper import KnossosFile, KnossosDataset


__all__ = [
"FILE_CONSTRUCTORS", "GROUP_LIKE", "DATASET_LIKE", "h5py", "z5py", "pyn5", "zarr"
"FILE_CONSTRUCTORS", "GROUP_LIKE", "DATASET_LIKE",
"h5py", "z5py", "pyn5", "zarr", "zarr_open",
]

FILE_CONSTRUCTORS = {}
Expand Down Expand Up @@ -61,14 +64,38 @@ def register_filetype(constructor, extensions=(), groups=(), datasets=()):
except ImportError:
pyn5 = None


def identity(arg):
return arg


def noop(*args, **kwargs):
pass


try:
# will not override z5py
import zarr

# zarr stores cannot be used as context managers,
# which breaks compatibility with similar libraries.
# This wrapper patches in those methods.
@functools.wraps(zarr.open)
def zarr_open(*args, **kwargs):
z = zarr.open(*args, **kwargs)
ztype = type(z)
if not hasattr(ztype, "__enter__"):
ztype.__enter__ = identity
if not hasattr(ztype, "__exit__"):
ztype.__exit__ = noop
return z

register_filetype(
zarr.open, N5_EXTS + ZARR_EXTS, zarr.hierarchy.Group, zarr.core.Array
zarr_open, N5_EXTS + ZARR_EXTS, zarr.hierarchy.Group, zarr.core.Array
)
except ImportError:
zarr = None
zarr_open = None

# Are there any typical knossos extensions?
# add knossos (no extension)
Expand Down
14 changes: 12 additions & 2 deletions elf/io/files.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from .extensions import FILE_CONSTRUCTORS, GROUP_LIKE, DATASET_LIKE
from .extensions import h5py, z5py
from .extensions import (
FILE_CONSTRUCTORS, GROUP_LIKE, DATASET_LIKE,
h5py, z5py, pyn5, zarr,
)
from .knossos_wrapper import KnossosFile, KnossosDataset


Expand Down Expand Up @@ -61,6 +63,14 @@ def is_h5py(node):
return h5py and isinstance(node, (h5py.Dataset, h5py.Group))


def is_zarr(node):
return zarr and isinstance(node, (zarr.core.Array, zarr.hierarchy.Group))


def is_pyn5(node):
return pyn5 and isinstance(node, (pyn5.Dataset, pyn5.Group))


def is_knossos(node):
""" Check if this is a KnossosWrapper object
"""
Expand Down
4 changes: 2 additions & 2 deletions test/io_tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from shutil import rmtree

import numpy as np
from elf.io.extensions import h5py, z5py, pyn5, zarr, FILE_CONSTRUCTORS
from elf.io.extensions import h5py, z5py, pyn5, zarr, zarr_open, FILE_CONSTRUCTORS


class FileTestBase(unittest.TestCase):
Expand Down Expand Up @@ -81,7 +81,7 @@ class TestPyn5Files(FileTestBase, FileTestMixin):
@unittest.skipUnless(zarr, "Need zarr")
class TestZarrFiles(FileTestBase, FileTestMixin):
ext = ".zr"
constructor = getattr(zarr, "open", None)
constructor = staticmethod(zarr_open)


class TestBackendPreference(unittest.TestCase):
Expand Down

0 comments on commit 8e2b2be

Please sign in to comment.