Skip to content

Commit

Permalink
Make Zarr optional for testing (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 authored Jul 5, 2024
1 parent 8917eaf commit 62edbe4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dependencies = [
"pandas>=1.0.5",
"ruamel.yaml>=0.16",
"scipy>=1.4",
"zarr >= 2.12.0",
"importlib-resources; python_version < '3.9'", # TODO: remove when minimum python version is 3.9
]
dynamic = ["version"]
Expand Down
13 changes: 9 additions & 4 deletions src/hdmf/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from warnings import warn
from typing import Tuple
from itertools import product, chain
from zarr import Array as ZarrArray

try:
from zarr import Array as ZarrArray
ZARR_INSTALLED = True
except ImportError:
ZARR_INSTALLED = False

import h5py
import numpy as np
Expand All @@ -16,9 +21,6 @@ def append_data(data, arg):
if isinstance(data, (list, DataIO)):
data.append(arg)
return data
elif isinstance(data, ZarrArray):
data.append([arg], axis=0)
return data
elif type(data).__name__ == 'TermSetWrapper': # circular import
data.append(arg)
return data
Expand All @@ -33,6 +35,9 @@ def append_data(data, arg):
data.resize(shape)
data[-1] = arg
return data
elif ZARR_INSTALLED and isinstance(data, ZarrArray):
data.append([arg], axis=0)
return data
else:
msg = "Data cannot append to object of type '%s'" % type(data)
raise ValueError(msg)
Expand Down
25 changes: 24 additions & 1 deletion tests/unit/utils_test/test_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,32 @@

import numpy as np
from numpy.testing import assert_array_equal
import zarr

try:
import zarr
ZARR_INSTALLED = True
except ImportError:
ZARR_INSTALLED = False


class MyIterable:
def __init__(self, data):
self.data = data


class TestAppendData(TestCase):
def test_append_exception(self):
data = MyIterable([1, 2, 3, 4, 5])
with self.assertRaises(ValueError):
append_data(data, 4)


class TestZarrAppendData(TestCase):

def setUp(self):
if not ZARR_INSTALLED:
self.skipTest("optional Zarr package is not installed")


def test_append_data_zarr(self):
zarr_array = zarr.array([1,2,3])
Expand Down

0 comments on commit 62edbe4

Please sign in to comment.