From 62edbe44892d10d199393eae3f6c7645a2689ea4 Mon Sep 17 00:00:00 2001 From: Matthew Avaylon Date: Fri, 5 Jul 2024 12:40:28 -0700 Subject: [PATCH] Make Zarr optional for testing (#1141) --- pyproject.toml | 1 - src/hdmf/data_utils.py | 13 ++++++++---- tests/unit/utils_test/test_data_utils.py | 25 +++++++++++++++++++++++- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a089113c0..6ef9850a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"] diff --git a/src/hdmf/data_utils.py b/src/hdmf/data_utils.py index 71f5bdf6d..798a40973 100644 --- a/src/hdmf/data_utils.py +++ b/src/hdmf/data_utils.py @@ -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 @@ -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 @@ -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) diff --git a/tests/unit/utils_test/test_data_utils.py b/tests/unit/utils_test/test_data_utils.py index 2e0df7ba8..b5a5e50e7 100644 --- a/tests/unit/utils_test/test_data_utils.py +++ b/tests/unit/utils_test/test_data_utils.py @@ -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])