Skip to content

Commit f5922bb

Browse files
Merge pull request #36 from constantinpape/zarr-support
Zarr support
2 parents e5fa1f0 + e7e85c2 commit f5922bb

File tree

9 files changed

+87
-39
lines changed

9 files changed

+87
-39
lines changed

.github/workflows/build.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
name: build
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
test:
8+
name: ${{ matrix.os }} ${{ matrix.python-version }}
9+
runs-on: ${{ matrix.os }}
10+
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
os: [ubuntu-latest]
15+
python-version: [3.7, 3.8, 3.9]
16+
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v2
20+
21+
- name: Setup miniconda
22+
uses: conda-incubator/setup-miniconda@v2
23+
with:
24+
activate-environment: pybdv-build-env
25+
auto-update-conda: true
26+
channels: conda-forge
27+
environment-file: .github/workflows/environment.yaml
28+
python-version: ${{ matrix.python-version }}
29+
auto-activate-base: false
30+
env:
31+
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
32+
33+
- name: Install pybdv
34+
if: matrix.os == 'ubuntu-latest'
35+
shell: bash -l {0}
36+
run: pip install -e .
37+
38+
- name: Run tests
39+
shell: bash -l {0}
40+
run: python -m unittest discover -s test -v

.github/workflows/environment.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: pybdv-build-env
2+
channels:
3+
- conda-forge
4+
dependencies:
5+
- imageio
6+
- h5py
7+
- scikit-image
8+
- tqdm
9+
- z5py
10+
# zarr doesn't work yet, because it doesn't allow writing 'dataType' as group attribute
11+
# - zarr

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Build Status](https://travis-ci.org/constantinpape/pybdv.svg?branch=master)](https://travis-ci.org/constantinpape/pybdv)
1+
[![Build Status](https://github.com/constantinpape/pybdv/workflows/build/badge.svg)](https://github.com/constantinpape/pybdv/actions)
22

33
# pyBDV
44

pybdv/util.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,29 @@
1919

2020
try:
2121
import z5py
22+
n5_file = z5py.File
2223
except ImportError:
23-
z5py = None
24+
n5_file = None
25+
26+
if n5_file is None:
27+
try:
28+
import zarr
29+
n5_file = zarr.open
30+
except ImportError:
31+
n5_file = None
2432

2533
def open_file(path, mode='r'):
2634
ext = os.path.splitext(path)[1].lower()
2735
if ext in HDF5_EXTENSIONS:
2836
return h5py.File(path, mode=mode)
2937
elif ext in N5_EXTENSIONS:
30-
if z5py is None:
31-
raise ValueError("Need z5py to open n5 files")
32-
return z5py.File(path, mode=mode)
38+
if n5_file is None:
39+
raise ValueError("Need zarr or z5py to open n5 files")
40+
return n5_file(path, mode=mode)
3341
else:
3442
raise ValueError(f"Invalid extension: {ext}")
43+
else:
44+
n5_file = open_file
3545

3646

3747
def get_key(is_h5, timepoint=None, setup_id=None, scale=None):

test/test_bdv_datasets.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55

66
import numpy as np
77
from pybdv import make_bdv, BdvDataset
8-
from pybdv.util import open_file, get_key, HDF5_EXTENSIONS
8+
from pybdv.util import open_file, get_key, HDF5_EXTENSIONS, n5_file
99

10-
try:
11-
import z5py
12-
except ImportError:
13-
z5py = None
1410

1511
DOWNSCALE_MODE = 'mean'
1612

@@ -174,7 +170,7 @@ class TestBdvDatasetH5(BdvDatasetTestMixin, unittest.TestCase):
174170
out_path = './tmp/test.h5'
175171

176172

177-
@unittest.skipUnless(z5py is not None, "Need z5py for n5 support")
173+
@unittest.skipIf(n5_file is None, "Need zarr or z5py for n5 support")
178174
class TestBdvDatasetN5(BdvDatasetTestMixin, unittest.TestCase):
179175
out_path = './tmp/test.n5'
180176

test/test_convert_to_bdv.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
from shutil import rmtree
66

77
import numpy as np
8-
from pybdv.util import get_key, open_file
9-
10-
try:
11-
import z5py
12-
except ImportError:
13-
z5py = None
8+
from pybdv.util import get_key, open_file, n5_file
149

1510

1611
class ConvertToBdvTestMixin(ABC):
@@ -124,7 +119,7 @@ class TestConvertToBdvH5(ConvertToBdvTestMixin, unittest.TestCase):
124119
is_h5 = True
125120

126121

127-
@unittest.skipUnless(z5py is not None, "Need z5py for n5 support")
122+
@unittest.skipIf(n5_file is None, "Need zarr or z5py for n5 support")
128123
class TestConvertToBdvN5(ConvertToBdvTestMixin, unittest.TestCase):
129124
in_path = './tmp/in.n5'
130125
out_path = './tmp/test.n5'

test/test_external.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import imageio
66
import h5py
77
import numpy as np
8-
import skimage.data
98

109

1110
# dummy test data, needs to be converted to bdv/xml
1211
# with FIJI externally
1312
def make_test_data():
13+
import skimage.data
1414
d = skimage.data.astronaut()
1515
d = d.transpose((2, 0, 1))
1616
d = np.concatenate(2 * [d], axis=0)
@@ -22,6 +22,14 @@ def make_test_data():
2222

2323
class TestExternal(unittest.TestCase):
2424
tmp_folder = 'tmp'
25+
inp_path = os.path.join(
26+
os.path.split(__file__)[0],
27+
'../data/example.tif'
28+
)
29+
exp_path = os.path.join(
30+
os.path.split(__file__)[0],
31+
'../data/example.h5'
32+
)
2533

2634
def setUp(self):
2735
os.makedirs(self.tmp_folder, exist_ok=True)
@@ -34,12 +42,11 @@ def tearDown(self):
3442

3543
def test_external(self):
3644
from pybdv import make_bdv
37-
d = imageio.volread('../data/example.tif')
45+
d = imageio.volread(self.inp_path)
3846
res_path = os.path.join(self.tmp_folder, 'data.h5')
39-
exp_path = '../data/example.h5'
4047
make_bdv(d, res_path, convert_dtype=True)
4148

42-
with h5py.File(exp_path, 'r') as f:
49+
with h5py.File(self.exp_path, 'r') as f:
4350
ds = f['t00000/s00/0/cells']
4451
exp = ds[:]
4552
with h5py.File(res_path, 'r') as f:

test/test_make_bdv.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
from shutil import rmtree
55

66
import numpy as np
7-
from pybdv.util import get_key, open_file
8-
9-
try:
10-
import z5py
11-
except ImportError:
12-
z5py = None
7+
from pybdv.util import get_key, open_file, n5_file
138

149

1510
class MakeBdvTestMixin(ABC):
@@ -329,7 +324,7 @@ class TestMakeBdvH5(MakeBdvTestMixin, unittest.TestCase):
329324
is_h5 = True
330325

331326

332-
@unittest.skipUnless(z5py is not None, "Need z5py for n5 support")
327+
@unittest.skipIf(n5_file is None, "Need zarr or z5py for n5 support")
333328
class TestMakeBdvN5(MakeBdvTestMixin, unittest.TestCase):
334329
out_path = './tmp/test.n5'
335330
is_h5 = False

test/test_metadata.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55

66
import numpy as np
77
from pybdv import make_bdv
8-
from pybdv.util import open_file
9-
10-
try:
11-
import z5py
12-
except ImportError:
13-
z5py = None
14-
8+
from pybdv.util import n5_file
159

1610

1711
class MetadataTestMixin(ABC):
@@ -69,12 +63,12 @@ def test_validate_attributes(self):
6963

7064
attrs2 = {'channel': {'id': None, 'name': 'foo'}}
7165
attrs2_exp = {'channel': {'id': 0, 'name': 'foo'}}
72-
attrs2_ = validate_attributes(self.xml_path, attrs1, 0, True)
66+
attrs2_ = validate_attributes(self.xml_path, attrs2, 0, True)
7367
self.assertEqual(attrs2_exp, attrs2_)
7468

7569
attrs3 = {'channel': {'name': 'bar'}}
7670
with self.assertRaises(ValueError):
77-
validate_attributes(self.xml_path, attrs1, 1, True)
71+
validate_attributes(self.xml_path, attrs3, 1, True)
7872

7973
attrs4 = {'displaysettings': {'id': 0, 'name': 'baz', 'min': 0, 'max': 1, 'isset': True,
8074
'color': [255, 255, 255, 255]}}
@@ -96,7 +90,7 @@ class TestMetadataH5(MetadataTestMixin, unittest.TestCase):
9690
bdv_format = 'bdv.hdf5'
9791

9892

99-
@unittest.skipUnless(z5py is not None, "Need z5py for n5 support")
93+
@unittest.skipIf(n5_file is None, "Need zarr or z5py for n5 support")
10094
class TestMetadataN5(MetadataTestMixin, unittest.TestCase):
10195
out_path = './tmp/test.n5'
10296
out_path2 = './tmp/test2.n5'

0 commit comments

Comments
 (0)