1
1
# -*- coding: utf-8 -*-
2
2
from __future__ import absolute_import , print_function , division
3
3
import json
4
- import math
5
4
6
5
7
- from nose .tools import eq_ as eq , assert_is_none , assert_raises , assert_is
6
+ from nose .tools import eq_ as eq , assert_is_none , assert_raises
8
7
import numpy as np
9
8
10
9
11
- from zarr .compat import PY2
10
+ from zarr .compat import binary_type , text_type
12
11
from zarr .meta import decode_array_metadata , encode_dtype , decode_dtype , \
13
12
ZARR_FORMAT , decode_group_metadata , encode_array_metadata
14
13
from zarr .errors import MetadataError
15
14
16
15
16
+ def assert_json_eq (expect , actual ): # pragma: no cover
17
+ if isinstance (expect , binary_type ):
18
+ expect = text_type (expect , 'ascii' )
19
+ if isinstance (actual , binary_type ):
20
+ actual = text_type (actual , 'ascii' )
21
+ ej = json .loads (expect )
22
+ aj = json .loads (actual )
23
+ eq (ej , aj )
24
+
25
+
17
26
def test_encode_decode_array_1 ():
18
27
19
28
meta = dict (
@@ -26,29 +35,23 @@ def test_encode_decode_array_1():
26
35
order = 'C'
27
36
)
28
37
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
43
- if not PY2 :
44
- meta_bytes = meta_bytes .encode ('ascii' )
38
+ meta_json = '''{
39
+ "chunks": [10],
40
+ "compression": "zlib",
41
+ "compression_opts": 1,
42
+ "dtype": "<f8",
43
+ "fill_value": null,
44
+ "order": "C",
45
+ "shape": [100],
46
+ "zarr_format": %s
47
+ }''' % ZARR_FORMAT
45
48
46
49
# test encoding
47
50
meta_enc = encode_array_metadata (meta )
48
- eq ( meta_bytes , meta_enc )
51
+ assert_json_eq ( meta_json , meta_enc )
49
52
50
53
# test decoding
51
- meta_dec = decode_array_metadata (meta_bytes )
54
+ meta_dec = decode_array_metadata (meta_enc )
52
55
eq (ZARR_FORMAT , meta_dec ['zarr_format' ])
53
56
eq (meta ['shape' ], meta_dec ['shape' ])
54
57
eq (meta ['chunks' ], meta_dec ['chunks' ])
@@ -72,44 +75,27 @@ def test_encode_decode_array_2():
72
75
order = 'F'
73
76
)
74
77
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
104
- if not PY2 :
105
- meta_bytes = meta_bytes .encode ('ascii' )
78
+ meta_json = '''{
79
+ "chunks": [10, 10],
80
+ "compression": "blosc",
81
+ "compression_opts": {
82
+ "clevel": 3,
83
+ "cname": "lz4",
84
+ "shuffle": 2
85
+ },
86
+ "dtype": [["a", "<i4"], ["b", "|S10"]],
87
+ "fill_value": 42,
88
+ "order": "F",
89
+ "shape": [100, 100],
90
+ "zarr_format": %s
91
+ }''' % ZARR_FORMAT
106
92
107
93
# test encoding
108
94
meta_enc = encode_array_metadata (meta )
109
- eq ( meta_bytes , meta_enc )
95
+ assert_json_eq ( meta_json , meta_enc )
110
96
111
97
# test decoding
112
- meta_dec = decode_array_metadata (meta_bytes )
98
+ meta_dec = decode_array_metadata (meta_enc )
113
99
eq (ZARR_FORMAT , meta_dec ['zarr_format' ])
114
100
eq (meta ['shape' ], meta_dec ['shape' ])
115
101
eq (meta ['chunks' ], meta_dec ['chunks' ])
@@ -122,31 +108,27 @@ def test_encode_decode_array_2():
122
108
123
109
def test_encode_decode_array_nan_fill_value ():
124
110
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
- )
111
+ meta = dict (
112
+ shape = (100 ,),
113
+ chunks = (10 ,),
114
+ dtype = np .dtype ('f8' ),
115
+ compression = 'zlib' ,
116
+ compression_opts = 1 ,
117
+ fill_value = np .nan ,
118
+ order = 'C'
119
+ )
136
120
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 )
121
+ # test fill value round trip
122
+ meta_enc = encode_array_metadata (meta )
123
+ meta_dec = decode_array_metadata (meta_enc )
124
+ actual = meta_dec ['fill_value' ]
125
+ assert np .isnan (actual )
144
126
145
127
146
128
def test_decode_array_unsupported_format ():
147
129
148
130
# unsupported format
149
- meta_bytes = '''{
131
+ meta_json = '''{
150
132
"zarr_format": %s,
151
133
"shape": [100],
152
134
"chunks": [10],
@@ -156,22 +138,18 @@ def test_decode_array_unsupported_format():
156
138
"fill_value": null,
157
139
"order": "C"
158
140
}''' % (ZARR_FORMAT - 1 )
159
- if not PY2 :
160
- meta_bytes = meta_bytes .encode ('ascii' )
161
141
with assert_raises (MetadataError ):
162
- decode_array_metadata (meta_bytes )
142
+ decode_array_metadata (meta_json )
163
143
164
144
165
145
def test_decode_array_missing_fields ():
166
146
167
147
# missing fields
168
- meta_bytes = '''{
148
+ meta_json = '''{
169
149
"zarr_format": %s
170
150
}''' % ZARR_FORMAT
171
- if not PY2 :
172
- meta_bytes = meta_bytes .encode ('ascii' )
173
151
with assert_raises (MetadataError ):
174
- decode_array_metadata (meta_bytes )
152
+ decode_array_metadata (meta_json )
175
153
176
154
177
155
def test_encode_decode_dtype ():
@@ -190,16 +168,12 @@ def test_decode_group():
190
168
b = '''{
191
169
"zarr_format": %s
192
170
}''' % ZARR_FORMAT
193
- if not PY2 :
194
- b = b .encode ('ascii' )
195
171
meta = decode_group_metadata (b )
196
172
eq (ZARR_FORMAT , meta ['zarr_format' ])
197
173
198
174
# unsupported format
199
175
b = '''{
200
176
"zarr_format": %s
201
177
}''' % (ZARR_FORMAT - 1 )
202
- if not PY2 :
203
- b = b .encode ('ascii' )
204
178
with assert_raises (MetadataError ):
205
179
decode_group_metadata (b )
0 commit comments