Skip to content

Commit 0bffc16

Browse files
authored
Merge pull request #34 from davidbrochart/read_with_xtensor_zarr
Read with xtensor-zarr, support v3
2 parents 2c1445e + 7808eaf commit 0bffc16

File tree

3 files changed

+102
-45
lines changed

3 files changed

+102
-45
lines changed

environment.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ dependencies:
66
- maven
77
- make
88
- cmake
9-
- xtensor-zarr=0.0.3=*_1
10-
- xtensor != 0.23.5
9+
- xtensor-zarr=0.0.4
1110
- openimageio
1211
- zlib
1312
- blosc
1413
- nodejs
15-
- z5py >= 2.0.8
14+
- z5py >= 2.0.10
1615
- python == 3.7.9
1716
- scikit-image
1817
- pytest

generate_data/xtensor_zarr/src/main.cpp

+82-41
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,96 @@
22
#include <xtensor-io/xio_zlib.hpp>
33
#include <xtensor-io/xio_blosc.hpp>
44
#include <xtensor-io/ximage.hpp>
5+
#include <xtensor-io/xnpz.hpp>
56
#include <xtensor-zarr/xzarr_hierarchy.hpp>
67

78
using namespace xt;
89

9-
int main()
10+
int main(int argc, char** argv)
1011
{
11-
namespace fs = ghc::filesystem;
12-
const std::string hier_path = "../../../data/xtensor_zarr.zr";
13-
fs::remove_all(hier_path);
14-
fs::create_directory(hier_path);
15-
const auto img = load_image("../../../data/reference_image.png");
16-
17-
const auto& s = img.shape();
18-
std::vector<size_t> shape;
19-
std::copy(s.cbegin(), s.cend(), std::back_inserter(shape));
20-
std::vector<size_t> chunk_shape = {100, 100, 1};
21-
auto h = create_zarr_hierarchy(hier_path, "2");
22-
auto g = h.create_group("/");
23-
24-
// raw
25-
xzarr_create_array_options<xio_binary_config> raw_options;
26-
raw_options.fill_value = 0;
27-
zarray z_raw = h.create_array("/raw", shape, chunk_shape, "|u1", raw_options);
28-
noalias(z_raw) = img;
29-
30-
// gzip
3112
xzarr_register_compressor<xzarr_file_system_store, xio_gzip_config>();
32-
xzarr_create_array_options<xio_gzip_config> gzip_options;
33-
gzip_options.fill_value = 0;
34-
zarray z_gzip = h.create_array("/gzip", shape, chunk_shape, "|u1", gzip_options);
35-
noalias(z_gzip) = img;
36-
37-
// zlib
3813
xzarr_register_compressor<xzarr_file_system_store, xio_zlib_config>();
39-
xzarr_create_array_options<xio_zlib_config> zlib_options;
40-
zlib_options.fill_value = 0;
41-
zarray z_zlib = h.create_array("/zlib", shape, chunk_shape, "|u1", zlib_options);
42-
noalias(z_zlib) = img;
43-
44-
// blosc
4514
xzarr_register_compressor<xzarr_file_system_store, xio_blosc_config>();
46-
xio_blosc_config blosc_config;
47-
blosc_config.cname = "lz4";
48-
xzarr_create_array_options<xio_blosc_config> blosc_options;
49-
blosc_options.fill_value = 0;
50-
blosc_options.compressor = blosc_config;
51-
auto g_blosc = h.create_group("/blosc/");
52-
zarray z_blosc_lz4 = h.create_array("/blosc/lz4", shape, chunk_shape, "|u1", blosc_options);
53-
noalias(z_blosc_lz4) = img;
15+
16+
if (argc == 1)
17+
{
18+
namespace fs = ghc::filesystem;
19+
std::string hier_path = "../../../data/xtensor_zarr.zr";
20+
const auto img = load_image("../../../data/reference_image.png");
21+
22+
const auto& s = img.shape();
23+
std::vector<size_t> shape;
24+
std::copy(s.cbegin(), s.cend(), std::back_inserter(shape));
25+
std::vector<size_t> chunk_shape = {100, 100, 1};
26+
27+
char zarr_version[2] = "2";
28+
const char* data_type = "|u1";
29+
30+
for (int v = 2; v <= 3; v++)
31+
{
32+
if (v == 3)
33+
{
34+
hier_path += "3";
35+
zarr_version[0] = '3';
36+
data_type ++;
37+
}
38+
fs::remove_all(hier_path);
39+
fs::create_directory(hier_path);
40+
auto h = create_zarr_hierarchy(hier_path.c_str(), zarr_version);
41+
auto g = h.create_group("/");
42+
43+
// raw
44+
xzarr_create_array_options<xio_binary_config> raw_options;
45+
raw_options.fill_value = 0;
46+
zarray z_raw = h.create_array("/raw", shape, chunk_shape, data_type, raw_options);
47+
noalias(z_raw) = img;
48+
49+
// gzip
50+
xzarr_create_array_options<xio_gzip_config> gzip_options;
51+
gzip_options.fill_value = 0;
52+
zarray z_gzip = h.create_array("/gzip", shape, chunk_shape, data_type, gzip_options);
53+
noalias(z_gzip) = img;
54+
55+
// zlib
56+
xzarr_create_array_options<xio_zlib_config> zlib_options;
57+
zlib_options.fill_value = 0;
58+
zarray z_zlib = h.create_array("/zlib", shape, chunk_shape, data_type, zlib_options);
59+
noalias(z_zlib) = img;
60+
61+
// blosc
62+
xio_blosc_config blosc_config;
63+
blosc_config.cname = "lz4";
64+
xzarr_create_array_options<xio_blosc_config> blosc_options;
65+
blosc_options.fill_value = 0;
66+
blosc_options.compressor = blosc_config;
67+
auto g_blosc = h.create_group("/blosc/");
68+
zarray z_blosc_lz4 = h.create_array("/blosc/lz4", shape, chunk_shape, data_type, blosc_options);
69+
noalias(z_blosc_lz4) = img;
70+
}
71+
}
72+
else
73+
{
74+
const std::string hier_path = argv[1];
75+
std::string ds_name = argv[2];
76+
const std::string v3_ext = ".zr3";
77+
std::string array_path;
78+
if (hier_path.compare(hier_path.length() - v3_ext.length(), v3_ext.length(), v3_ext) == 0)
79+
{
80+
array_path = hier_path;
81+
ds_name = "/" + ds_name;
82+
}
83+
else
84+
{
85+
array_path = hier_path + '/' + ds_name;
86+
ds_name = "";
87+
}
88+
89+
auto h = get_zarr_hierarchy(array_path.c_str());
90+
auto z = h.get_array(ds_name);
91+
auto a = z.get_array<uint8_t>();
92+
93+
dump_npz("a.npz", "a", a, false, false);
94+
}
5495

5596
return 0;
5697
}

test/test_read_all.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
3434
"""
3535
import os
36+
import subprocess
3637
from typing import Dict, List
3738
from pathlib import Path
3839
from skimage.io import imread
@@ -61,7 +62,12 @@
6162
"zarr": [],
6263
"zarr-v3": ["blosc", "gzip", "raw", "zlib"],
6364
"N5": [],
64-
}
65+
},
66+
"xtensor_zarr": {
67+
"zarr": ["blosc", "gzip", "raw", "zlib"],
68+
"zarr-v3": ["blosc", "gzip", "raw", "zlib"],
69+
"N5": [],
70+
},
6571
}
6672

6773

@@ -106,12 +112,22 @@ def read_with_zarrita(fpath, ds_name, nested):
106112
h = zarrita.get_hierarchy(str(fpath.absolute()))
107113
return h["/" + ds_name][:]
108114

115+
def read_with_xtensor_zarr(fpath, ds_name, nested):
116+
if ds_name == "blosc":
117+
ds_name = "blosc/lz4"
118+
fname = "a.npz"
119+
if os.path.exists(fname):
120+
os.remove(fname)
121+
subprocess.check_call(["generate_data/xtensor_zarr/build/run_xtensor_zarr", fpath, ds_name])
122+
return np.load(fname)["a"]
123+
109124

110125
READ_FNS = {
111126
"zarr": read_with_zarr,
112127
"zarrita": read_with_zarrita,
113128
"pyn5": read_with_pyn5,
114129
"z5py": read_with_z5py,
130+
"xtensor_zarr": read_with_xtensor_zarr,
115131
}
116132

117133

@@ -209,6 +225,7 @@ def _get_read_fn(reading_library):
209225
"pyn5": read_with_pyn5,
210226
"z5py": read_with_z5py,
211227
"zarrita": read_with_zarrita,
228+
"xtensor_zarr": read_with_xtensor_zarr,
212229
}[reading_library]
213230
return read_fn
214231

0 commit comments

Comments
 (0)