2121from zarr .codecs import Zlib
2222
2323
24+ # something bcolz-like
25+ class MockBcolzArray (object ):
26+
27+ def __init__ (self , data , chunklen ):
28+ self .data = data
29+ self .chunklen = chunklen
30+
31+ def __getattr__ (self , item ):
32+ return getattr (self .data , item )
33+
34+ def __getitem__ (self , item ):
35+ return self .data [item ]
36+
37+
38+ # something h5py-like
39+ class MockH5pyDataset (object ):
40+
41+ def __init__ (self , data , chunks ):
42+ self .data = data
43+ self .chunks = chunks
44+
45+ def __getattr__ (self , item ):
46+ return getattr (self .data , item )
47+
48+ def __getitem__ (self , item ):
49+ return self .data [item ]
50+
51+
2452def test_array ():
2553
2654 # with numpy array
@@ -44,25 +72,23 @@ def test_array():
4472 eq (z .dtype , z2 .dtype )
4573 assert_array_equal (z [:], z2 [:])
4674
47- # with something bcolz-like
48- class MockBcolzArray (object ):
49-
50- def __init__ (self , data , chunklen ):
51- self .data = data
52- self .chunklen = chunklen
53-
54- def __getattr__ (self , item ):
55- return getattr (self .data , item )
56-
57- def __getitem__ (self , item ):
58- return self .data [item ]
59-
6075 b = np .arange (1000 ).reshape (100 , 10 )
6176 c = MockBcolzArray (b , 10 )
6277 z3 = array (c )
6378 eq (c .shape , z3 .shape )
6479 eq ((10 , 10 ), z3 .chunks )
6580
81+ b = np .arange (1000 ).reshape (100 , 10 )
82+ c = MockH5pyDataset (b , chunks = (10 , 2 ))
83+ z4 = array (c )
84+ eq (c .shape , z4 .shape )
85+ eq ((10 , 2 ), z4 .chunks )
86+
87+ c = MockH5pyDataset (b , chunks = None )
88+ z5 = array (c )
89+ eq (c .shape , z5 .shape )
90+ assert_is_instance (z5 .chunks , tuple )
91+
6692
6793def test_empty ():
6894 z = empty (100 , chunks = 10 )
@@ -174,6 +200,7 @@ def test_open_array():
174200
175201
176202def test_empty_like ():
203+
177204 # zarr array
178205 z = empty (100 , chunks = 10 , dtype = 'f4' , compressor = Zlib (5 ),
179206 order = 'F' )
@@ -184,18 +211,35 @@ def test_empty_like():
184211 eq (z .compressor .get_config (), z2 .compressor .get_config ())
185212 eq (z .fill_value , z2 .fill_value )
186213 eq (z .order , z2 .order )
214+
187215 # numpy array
188216 a = np .empty (100 , dtype = 'f4' )
189217 z3 = empty_like (a )
190218 eq (a .shape , z3 .shape )
191219 eq ((100 ,), z3 .chunks )
192220 eq (a .dtype , z3 .dtype )
193221 assert_is_none (z3 .fill_value )
222+
194223 # something slightly silly
195224 a = [0 ] * 100
196225 z3 = empty_like (a , shape = 200 )
197226 eq ((200 ,), z3 .shape )
198227
228+ # other array-likes
229+ b = np .arange (1000 ).reshape (100 , 10 )
230+ c = MockBcolzArray (b , 10 )
231+ z = empty_like (c )
232+ eq (b .shape , z .shape )
233+ eq ((10 , 10 ), z .chunks )
234+ c = MockH5pyDataset (b , chunks = (10 , 2 ))
235+ z = empty_like (c )
236+ eq (b .shape , z .shape )
237+ eq ((10 , 2 ), z .chunks )
238+ c = MockH5pyDataset (b , chunks = None )
239+ z = empty_like (c )
240+ eq (b .shape , z .shape )
241+ assert_is_instance (z .chunks , tuple )
242+
199243
200244def test_zeros_like ():
201245 # zarr array
0 commit comments