1
1
# -*- coding: utf-8 -*-
2
2
from __future__ import absolute_import , print_function , division
3
3
import json
4
+ import math
4
5
5
6
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
7
8
import numpy as np
8
9
9
10
10
11
from zarr .compat import PY2
11
12
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
13
14
from zarr .errors import MetadataError
14
15
15
16
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
29
43
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
56
104
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 ():
69
147
70
148
# unsupported format
71
- b = '''{
149
+ meta_bytes = '''{
72
150
"zarr_format": %s,
73
151
"shape": [100],
74
152
"chunks": [10],
@@ -79,18 +157,21 @@ def test_decode_array():
79
157
"order": "C"
80
158
}''' % (ZARR_FORMAT - 1 )
81
159
if not PY2 :
82
- b = b .encode ('ascii' )
160
+ meta_bytes = meta_bytes .encode ('ascii' )
83
161
with assert_raises (MetadataError ):
84
- decode_array_metadata (b )
162
+ decode_array_metadata (meta_bytes )
163
+
164
+
165
+ def test_decode_array_missing_fields ():
85
166
86
167
# missing fields
87
- b = '''{
168
+ meta_bytes = '''{
88
169
"zarr_format": %s
89
170
}''' % ZARR_FORMAT
90
171
if not PY2 :
91
- b = b .encode ('ascii' )
172
+ meta_bytes = meta_bytes .encode ('ascii' )
92
173
with assert_raises (MetadataError ):
93
- decode_array_metadata (b )
174
+ decode_array_metadata (meta_bytes )
94
175
95
176
96
177
def test_encode_decode_dtype ():
0 commit comments