Skip to content

Commit 001f69b

Browse files
committed
test nan fill value; improve meta tests
1 parent 60a272b commit 001f69b

File tree

1 file changed

+140
-59
lines changed

1 file changed

+140
-59
lines changed

zarr/tests/test_meta.py

Lines changed: 140 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,152 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, print_function, division
33
import json
4+
import math
45

56

6-
from nose.tools import eq_ as eq, assert_is_none, assert_raises
7+
from nose.tools import eq_ as eq, assert_is_none, assert_raises, assert_is
78
import numpy as np
89

910

1011
from zarr.compat import PY2
1112
from zarr.meta import decode_array_metadata, encode_dtype, decode_dtype, \
12-
ZARR_FORMAT, decode_group_metadata
13+
ZARR_FORMAT, decode_group_metadata, encode_array_metadata
1314
from zarr.errors import MetadataError
1415

1516

16-
def test_decode_array():
17-
18-
# typical
19-
b = '''{
20-
"zarr_format": %s,
21-
"shape": [100],
22-
"chunks": [10],
23-
"dtype": "<f8",
24-
"compression": "zlib",
25-
"compression_opts": 1,
26-
"fill_value": null,
27-
"order": "C"
28-
}''' % ZARR_FORMAT
17+
def test_encode_decode_array_1():
18+
19+
meta = dict(
20+
shape=(100,),
21+
chunks=(10,),
22+
dtype=np.dtype('f8'),
23+
compression='zlib',
24+
compression_opts=1,
25+
fill_value=None,
26+
order='C'
27+
)
28+
29+
meta_bytes = '''{
30+
"chunks": [
31+
10
32+
],
33+
"compression": "zlib",
34+
"compression_opts": 1,
35+
"dtype": "<f8",
36+
"fill_value": null,
37+
"order": "C",
38+
"shape": [
39+
100
40+
],
41+
"zarr_format": %s
42+
}''' % ZARR_FORMAT
2943
if not PY2:
30-
b = b.encode('ascii')
31-
meta = decode_array_metadata(b)
32-
eq(ZARR_FORMAT, meta['zarr_format'])
33-
eq((100,), meta['shape'])
34-
eq((10,), meta['chunks'])
35-
eq(np.dtype('<f8'), meta['dtype'])
36-
eq('zlib', meta['compression'])
37-
eq(1, meta['compression_opts'])
38-
assert_is_none(meta['fill_value'])
39-
eq('C', meta['order'])
40-
41-
# variations
42-
b = '''{
43-
"zarr_format": %s,
44-
"shape": [100, 100],
45-
"chunks": [10, 10],
46-
"dtype": [["a", "i4"], ["b", "S10"]],
47-
"compression": "blosc",
48-
"compression_opts": {
49-
"cname": "lz4",
50-
"clevel": 3,
51-
"shuffle": 2
52-
},
53-
"fill_value": 42,
54-
"order": "F"
55-
}''' % ZARR_FORMAT
44+
meta_bytes = meta_bytes.encode('ascii')
45+
46+
# test encoding
47+
meta_enc = encode_array_metadata(meta)
48+
eq(meta_bytes, meta_enc)
49+
50+
# test decoding
51+
meta_dec = decode_array_metadata(meta_bytes)
52+
eq(ZARR_FORMAT, meta_dec['zarr_format'])
53+
eq(meta['shape'], meta_dec['shape'])
54+
eq(meta['chunks'], meta_dec['chunks'])
55+
eq(meta['dtype'], meta_dec['dtype'])
56+
eq(meta['compression'], meta_dec['compression'])
57+
eq(meta['compression_opts'], meta_dec['compression_opts'])
58+
eq(meta['order'], meta_dec['order'])
59+
assert_is_none(meta_dec['fill_value'])
60+
61+
62+
def test_encode_decode_array_2():
63+
64+
# some variations
65+
meta = dict(
66+
shape=(100, 100),
67+
chunks=(10, 10),
68+
dtype=np.dtype([('a', 'i4'), ('b', 'S10')]),
69+
compression='blosc',
70+
compression_opts=dict(cname='lz4', clevel=3, shuffle=2),
71+
fill_value=42,
72+
order='F'
73+
)
74+
75+
meta_bytes = '''{
76+
"chunks": [
77+
10,
78+
10
79+
],
80+
"compression": "blosc",
81+
"compression_opts": {
82+
"clevel": 3,
83+
"cname": "lz4",
84+
"shuffle": 2
85+
},
86+
"dtype": [
87+
[
88+
"a",
89+
"<i4"
90+
],
91+
[
92+
"b",
93+
"|S10"
94+
]
95+
],
96+
"fill_value": 42,
97+
"order": "F",
98+
"shape": [
99+
100,
100+
100
101+
],
102+
"zarr_format": %s
103+
}''' % ZARR_FORMAT
56104
if not PY2:
57-
b = b.encode('ascii')
58-
meta = decode_array_metadata(b)
59-
eq(ZARR_FORMAT, meta['zarr_format'])
60-
eq((100, 100), meta['shape'])
61-
eq((10, 10), meta['chunks'])
62-
# check structured dtype
63-
eq(np.dtype([('a', 'i4'), ('b', 'S10')]), meta['dtype'])
64-
# check structured compression_opts
65-
eq(dict(cname='lz4', clevel=3, shuffle=2), meta['compression_opts'])
66-
# check fill value
67-
eq(42, meta['fill_value'])
68-
eq('F', meta['order'])
105+
meta_bytes = meta_bytes.encode('ascii')
106+
107+
# test encoding
108+
meta_enc = encode_array_metadata(meta)
109+
eq(meta_bytes, meta_enc)
110+
111+
# test decoding
112+
meta_dec = decode_array_metadata(meta_bytes)
113+
eq(ZARR_FORMAT, meta_dec['zarr_format'])
114+
eq(meta['shape'], meta_dec['shape'])
115+
eq(meta['chunks'], meta_dec['chunks'])
116+
eq(meta['dtype'], meta_dec['dtype'])
117+
eq(meta['compression'], meta_dec['compression'])
118+
eq(meta['compression_opts'], meta_dec['compression_opts'])
119+
eq(meta['order'], meta_dec['order'])
120+
eq(meta['fill_value'], meta_dec['fill_value'])
121+
122+
123+
def test_encode_decode_array_nan_fill_value():
124+
125+
for fill in math.nan, np.nan:
126+
127+
meta = dict(
128+
shape=(100,),
129+
chunks=(10,),
130+
dtype=np.dtype('f8'),
131+
compression='zlib',
132+
compression_opts=1,
133+
fill_value=fill,
134+
order='C'
135+
)
136+
137+
# test fill value round trip
138+
meta_enc = encode_array_metadata(meta)
139+
meta_dec = decode_array_metadata(meta_enc)
140+
actual = meta_dec['fill_value']
141+
print(repr(actual))
142+
print(type(actual))
143+
assert np.isnan(actual)
144+
145+
146+
def test_decode_array_unsupported_format():
69147

70148
# unsupported format
71-
b = '''{
149+
meta_bytes = '''{
72150
"zarr_format": %s,
73151
"shape": [100],
74152
"chunks": [10],
@@ -79,18 +157,21 @@ def test_decode_array():
79157
"order": "C"
80158
}''' % (ZARR_FORMAT - 1)
81159
if not PY2:
82-
b = b.encode('ascii')
160+
meta_bytes = meta_bytes.encode('ascii')
83161
with assert_raises(MetadataError):
84-
decode_array_metadata(b)
162+
decode_array_metadata(meta_bytes)
163+
164+
165+
def test_decode_array_missing_fields():
85166

86167
# missing fields
87-
b = '''{
168+
meta_bytes = '''{
88169
"zarr_format": %s
89170
}''' % ZARR_FORMAT
90171
if not PY2:
91-
b = b.encode('ascii')
172+
meta_bytes = meta_bytes.encode('ascii')
92173
with assert_raises(MetadataError):
93-
decode_array_metadata(b)
174+
decode_array_metadata(meta_bytes)
94175

95176

96177
def test_encode_decode_dtype():

0 commit comments

Comments
 (0)