@@ -12,7 +12,6 @@ import sys
12
12
import os
13
13
import struct
14
14
import ctypes
15
- import pickle
16
15
import shutil
17
16
import tempfile
18
17
from collections import namedtuple
@@ -21,8 +20,7 @@ import multiprocessing
21
20
import fasteners
22
21
23
22
24
- from zarr import util as _util
25
- from zarr import defaults
23
+ from zarr import util as _util, meta as _meta, defaults as _defaults
26
24
27
25
28
26
# ##############################################################################
@@ -163,21 +161,21 @@ def _normalize_cparams(cname=None, clevel=None, shuffle=None):
163
161
"""
164
162
165
163
# determine compressor
166
- cname = cname if cname is not None else defaults .cname
164
+ cname = cname if cname is not None else _defaults .cname
167
165
if type (cname) != bytes:
168
- cname = cname.encode()
166
+ cname = cname.encode(' ascii ' )
169
167
# check compressor is available
170
168
if blosc_compname_to_compcode(cname) < 0 :
171
169
raise ValueError (' compressor not available: %s ' % cname)
172
170
173
171
# determine compression level
174
- clevel = clevel if clevel is not None else defaults .clevel
172
+ clevel = clevel if clevel is not None else _defaults .clevel
175
173
clevel = int (clevel)
176
174
if clevel < 0 or clevel > 9 :
177
175
raise ValueError (' invalid compression level: %s ' % clevel)
178
176
179
177
# determine shuffle filter
180
- shuffle = shuffle if shuffle is not None else defaults .shuffle
178
+ shuffle = shuffle if shuffle is not None else _defaults .shuffle
181
179
shuffle = int (shuffle)
182
180
if shuffle not in [0 , 1 , 2 ]:
183
181
raise ValueError (' invalid shuffle: %s ' % shuffle)
@@ -747,28 +745,6 @@ def _normalize_chunks(chunks, tuple shape):
747
745
return chunks
748
746
749
747
750
- def _read_array_metadata (path ):
751
-
752
- # check path exists
753
- if not os.path.exists(path):
754
- raise ValueError (' path not found: %s ' % path)
755
-
756
- # check metadata file
757
- meta_path = os.path.join(path, defaults.metapath)
758
- if not os.path.exists(meta_path):
759
- raise ValueError (' array metadata not found: %s ' % path)
760
-
761
- with open (meta_path, ' rb' ) as f:
762
- meta = pickle.load(f)
763
- return meta
764
-
765
-
766
- def _write_array_metadata (path , meta ):
767
- meta_path = os.path.join(path, defaults.metapath)
768
- with open (meta_path, ' wb' ) as f:
769
- pickle.dump(meta, f, protocol = 0 )
770
-
771
-
772
748
def _array_resize (BaseArray array , *args ):
773
749
774
750
# normalize new shape argument
@@ -1216,7 +1192,7 @@ cdef class PersistentArray(BaseArray):
1216
1192
# a : read/write if exists, create otherwise (default)
1217
1193
1218
1194
# use metadata file as indicator of array existence
1219
- meta_path = os.path.join(path, defaults .metapath)
1195
+ meta_path = os.path.join(path, _defaults .metapath)
1220
1196
1221
1197
if mode in [' r' , ' r+' ]:
1222
1198
self ._open(path, ** kwargs)
@@ -1264,7 +1240,7 @@ cdef class PersistentArray(BaseArray):
1264
1240
cname = None , clevel = None , shuffle = None , fill_value = None ):
1265
1241
1266
1242
# create directories
1267
- data_path = os.path.join(path, defaults .datapath)
1243
+ data_path = os.path.join(path, _defaults .datapath)
1268
1244
if not os.path.exists(data_path):
1269
1245
os.makedirs(data_path)
1270
1246
@@ -1277,20 +1253,20 @@ cdef class PersistentArray(BaseArray):
1277
1253
self ._fill_value = fill_value
1278
1254
1279
1255
# write metadata
1280
- metadata = { ' shape ' : self ._shape ,
1281
- ' chunks ' : self ._chunks ,
1282
- ' dtype ' : self ._dtype ,
1283
- ' cname ' : self ._cname ,
1284
- ' clevel ' : self ._clevel ,
1285
- ' shuffle ' : self ._shuffle ,
1286
- ' fill_value ' : self ._fill_value}
1287
- _write_array_metadata(path, metadata )
1256
+ _meta.write_array_metadata(path ,
1257
+ shape = self ._shape ,
1258
+ chunks = self ._chunks ,
1259
+ dtype = self ._dtype ,
1260
+ cname = self ._cname ,
1261
+ clevel = self ._clevel ,
1262
+ shuffle = self ._shuffle,
1263
+ fill_value = self ._fill_value )
1288
1264
1289
1265
def _open (self , path , shape = None , chunks = None , dtype = None , cname = None ,
1290
1266
clevel = None , shuffle = None , fill_value = None ):
1291
1267
1292
1268
# read metadata
1293
- metadata = _read_array_metadata (path)
1269
+ metadata = _meta.read_array_metadata (path)
1294
1270
1295
1271
# set attributes
1296
1272
self ._shape = metadata[' shape' ]
@@ -1327,8 +1303,8 @@ cdef class PersistentArray(BaseArray):
1327
1303
return self ._cdata[cidx]
1328
1304
1329
1305
cdef object get_chunk_path(self , tuple cidx):
1330
- chunk_filename = ' .' .join(map (str , cidx)) + defaults .datasuffix
1331
- chunk_path = os.path.join(self ._path, defaults .datapath,
1306
+ chunk_filename = ' .' .join(map (str , cidx)) + _defaults .datasuffix
1307
+ chunk_path = os.path.join(self ._path, _defaults .datapath,
1332
1308
chunk_filename)
1333
1309
return chunk_path
1334
1310
@@ -1347,14 +1323,14 @@ cdef class PersistentArray(BaseArray):
1347
1323
_array_resize(self , * args)
1348
1324
1349
1325
# write metadata
1350
- metadata = { ' shape ' : self ._shape ,
1351
- ' chunks ' : self ._chunks ,
1352
- ' dtype ' : self ._dtype ,
1353
- ' cname ' : self ._cname ,
1354
- ' clevel ' : self ._clevel ,
1355
- ' shuffle ' : self ._shuffle ,
1356
- ' fill_value ' : self ._fill_value}
1357
- _write_array_metadata( self ._path, metadata )
1326
+ _meta.write_array_metadata( self ._path ,
1327
+ shape = self ._shape ,
1328
+ chunks = self ._chunks ,
1329
+ dtype = self ._dtype ,
1330
+ cname = self ._cname ,
1331
+ clevel = self ._clevel ,
1332
+ shuffle = self ._shuffle,
1333
+ fill_value = self ._fill_value )
1358
1334
1359
1335
def __setitem__ (self , key , value ):
1360
1336
if self ._mode == ' r' :
@@ -1534,18 +1510,18 @@ cdef class LazyPersistentArray(PersistentArray):
1534
1510
def __get__ (self ):
1535
1511
# N.B., chunk objects are instantiated lazily, so there may be
1536
1512
# data on disk but no corresponding chunk object yet
1537
- data_dir = os.path.join(self ._path, defaults .datapath)
1513
+ data_dir = os.path.join(self ._path, _defaults .datapath)
1538
1514
return sum (os.path.getsize(os.path.join(data_dir, fn))
1539
1515
for fn in os.listdir(data_dir))
1540
1516
1541
1517
property is_initialized :
1542
1518
def __get__ (self ):
1543
1519
# N.B., chunk objects are instantiated lazily, so there may be
1544
1520
# data on disk but no corresponding chunk object yet
1545
- data_dir = os.path.join(self ._path, defaults .datapath)
1521
+ data_dir = os.path.join(self ._path, _defaults .datapath)
1546
1522
a = np.zeros(self ._cdata_shape, dtype = ' b1' )
1547
- for fn in glob(os.path.join(data_dir, ' *' + defaults .datasuffix)):
1548
- bn = os.path.basename(fn)[:- len (defaults .datasuffix)]
1523
+ for fn in glob(os.path.join(data_dir, ' *' + _defaults .datasuffix)):
1524
+ bn = os.path.basename(fn)[:- len (_defaults .datasuffix)]
1549
1525
cidx = tuple (map (int , bn.split(' .' )))
1550
1526
a[cidx] = True
1551
1527
return a
@@ -1587,14 +1563,14 @@ cdef class LazyPersistentArray(PersistentArray):
1587
1563
_lazy_resize(self , * args)
1588
1564
1589
1565
# write metadata
1590
- metadata = { ' shape ' : self ._shape ,
1591
- ' chunks ' : self ._chunks ,
1592
- ' dtype ' : self ._dtype ,
1593
- ' cname ' : self ._cname ,
1594
- ' clevel ' : self ._clevel ,
1595
- ' shuffle ' : self ._shuffle ,
1596
- ' fill_value ' : self ._fill_value}
1597
- _write_array_metadata( self ._path, metadata )
1566
+ _meta.write_array_metadata( self ._path ,
1567
+ shape = self ._shape ,
1568
+ chunks = self ._chunks ,
1569
+ dtype = self ._dtype ,
1570
+ cname = self ._cname ,
1571
+ clevel = self ._clevel ,
1572
+ shuffle = self ._shuffle,
1573
+ fill_value = self ._fill_value )
1598
1574
1599
1575
1600
1576
# noinspection PyAbstractClass
@@ -1609,8 +1585,8 @@ cdef class SynchronizedLazyPersistentArray(LazyPersistentArray):
1609
1585
return _lazy_get_chunk(self , cidx)
1610
1586
1611
1587
cdef BaseChunk create_chunk(self , tuple cidx):
1612
- chunk_filename = ' .' .join(map (str , cidx)) + defaults .datasuffix
1613
- chunk_path = os.path.join(self ._path, defaults .datapath,
1588
+ chunk_filename = ' .' .join(map (str , cidx)) + _defaults .datasuffix
1589
+ chunk_path = os.path.join(self ._path, _defaults .datapath,
1614
1590
chunk_filename)
1615
1591
return SynchronizedPersistentChunk(
1616
1592
path = chunk_path, shape = self ._chunks, dtype = self ._dtype,
0 commit comments