@@ -13,16 +13,14 @@ import sys
13
13
import os
14
14
import struct
15
15
import ctypes
16
- import pickle
17
16
import shutil
18
17
import tempfile
19
18
from collections import namedtuple
20
19
from glob import glob
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
# ##############################################################################
@@ -118,21 +116,21 @@ def _normalize_cparams(cname=None, clevel=None, shuffle=None):
118
116
"""
119
117
120
118
# determine compressor
121
- cname = cname if cname is not None else defaults .cname
119
+ cname = cname if cname is not None else _defaults .cname
122
120
if type (cname) != bytes:
123
- cname = cname.encode()
121
+ cname = cname.encode(' ascii ' )
124
122
# check compressor is available
125
123
if blosc_compname_to_compcode(cname) < 0 :
126
124
raise ValueError (" compressor not available: %s " % cname)
127
125
128
126
# determine compression level
129
- clevel = clevel if clevel is not None else defaults .clevel
127
+ clevel = clevel if clevel is not None else _defaults .clevel
130
128
clevel = int (clevel)
131
129
if clevel < 0 or clevel > 9 :
132
130
raise ValueError (' invalid compression level: %s ' % clevel)
133
131
134
132
# determine shuffle filter
135
- shuffle = shuffle if shuffle is not None else defaults .shuffle
133
+ shuffle = shuffle if shuffle is not None else _defaults .shuffle
136
134
shuffle = int (shuffle)
137
135
if shuffle not in [0 , 1 , 2 ]:
138
136
raise ValueError (' invalid shuffle: %s ' % shuffle)
@@ -678,28 +676,6 @@ def _normalize_chunks(chunks, tuple shape):
678
676
return chunks
679
677
680
678
681
- def _read_array_metadata (path ):
682
-
683
- # check path exists
684
- if not os.path.exists(path):
685
- raise ValueError (' path not found: %s ' % path)
686
-
687
- # check metadata file
688
- meta_path = os.path.join(path, defaults.metapath)
689
- if not os.path.exists(meta_path):
690
- raise ValueError (' array metadata not found: %s ' % path)
691
-
692
- with open (meta_path, ' rb' ) as f:
693
- meta = pickle.load(f)
694
- return meta
695
-
696
-
697
- def _write_array_metadata (path , meta ):
698
- meta_path = os.path.join(path, defaults.metapath)
699
- with open (meta_path, ' wb' ) as f:
700
- pickle.dump(meta, f, protocol = 0 )
701
-
702
-
703
679
def _array_resize (BaseArray array , *args ):
704
680
705
681
# normalize new shape argument
@@ -1147,7 +1123,7 @@ cdef class PersistentArray(BaseArray):
1147
1123
# a : read/write if exists, create otherwise (default)
1148
1124
1149
1125
# use metadata file as indicator of array existence
1150
- meta_path = os.path.join(path, defaults .metapath)
1126
+ meta_path = os.path.join(path, _defaults .metapath)
1151
1127
1152
1128
if mode in [' r' , ' r+' ]:
1153
1129
self ._open(path, ** kwargs)
@@ -1195,7 +1171,7 @@ cdef class PersistentArray(BaseArray):
1195
1171
cname = None , clevel = None , shuffle = None , fill_value = None ):
1196
1172
1197
1173
# create directories
1198
- data_path = os.path.join(path, defaults .datapath)
1174
+ data_path = os.path.join(path, _defaults .datapath)
1199
1175
if not os.path.exists(data_path):
1200
1176
os.makedirs(data_path)
1201
1177
@@ -1208,20 +1184,20 @@ cdef class PersistentArray(BaseArray):
1208
1184
self ._fill_value = fill_value
1209
1185
1210
1186
# write metadata
1211
- metadata = { ' shape ' : self ._shape ,
1212
- ' chunks ' : self ._chunks ,
1213
- ' dtype ' : self ._dtype ,
1214
- ' cname ' : self ._cname ,
1215
- ' clevel ' : self ._clevel ,
1216
- ' shuffle ' : self ._shuffle ,
1217
- ' fill_value ' : self ._fill_value}
1218
- _write_array_metadata(path, metadata )
1187
+ _meta.write_array_metadata(path ,
1188
+ shape = self ._shape ,
1189
+ chunks = self ._chunks ,
1190
+ dtype = self ._dtype ,
1191
+ cname = self ._cname ,
1192
+ clevel = self ._clevel ,
1193
+ shuffle = self ._shuffle,
1194
+ fill_value = self ._fill_value )
1219
1195
1220
1196
def _open (self , path , shape = None , chunks = None , dtype = None , cname = None ,
1221
1197
clevel = None , shuffle = None , fill_value = None ):
1222
1198
1223
1199
# read metadata
1224
- metadata = _read_array_metadata (path)
1200
+ metadata = _meta.read_array_metadata (path)
1225
1201
1226
1202
# set attributes
1227
1203
self ._shape = metadata[' shape' ]
@@ -1258,8 +1234,8 @@ cdef class PersistentArray(BaseArray):
1258
1234
return self ._cdata[cidx]
1259
1235
1260
1236
cdef object get_chunk_path(self , tuple cidx):
1261
- chunk_filename = ' .' .join(map (str , cidx)) + defaults .datasuffix
1262
- chunk_path = os.path.join(self ._path, defaults .datapath,
1237
+ chunk_filename = ' .' .join(map (str , cidx)) + _defaults .datasuffix
1238
+ chunk_path = os.path.join(self ._path, _defaults .datapath,
1263
1239
chunk_filename)
1264
1240
return chunk_path
1265
1241
@@ -1278,14 +1254,14 @@ cdef class PersistentArray(BaseArray):
1278
1254
_array_resize(self , * args)
1279
1255
1280
1256
# write metadata
1281
- metadata = { ' shape ' : self ._shape ,
1282
- ' chunks ' : self ._chunks ,
1283
- ' dtype ' : self ._dtype ,
1284
- ' cname ' : self ._cname ,
1285
- ' clevel ' : self ._clevel ,
1286
- ' shuffle ' : self ._shuffle ,
1287
- ' fill_value ' : self ._fill_value}
1288
- _write_array_metadata( self ._path, metadata )
1257
+ _meta.write_array_metadata( self ._path ,
1258
+ shape = self ._shape ,
1259
+ chunks = self ._chunks ,
1260
+ dtype = self ._dtype ,
1261
+ cname = self ._cname ,
1262
+ clevel = self ._clevel ,
1263
+ shuffle = self ._shuffle,
1264
+ fill_value = self ._fill_value )
1289
1265
1290
1266
def __setitem__ (self , key , value ):
1291
1267
if self ._mode == ' r' :
@@ -1322,6 +1298,7 @@ cdef class SynchronizedPersistentArray(PersistentArray):
1322
1298
# ##############################################################################
1323
1299
1324
1300
1301
+ # noinspection PyUnresolvedReferences,PyProtectedMember
1325
1302
cdef _lazy_get_chunk(BaseArray array, tuple cidx):
1326
1303
try :
1327
1304
chunk = array._cdata[cidx]
@@ -1464,18 +1441,18 @@ cdef class LazyPersistentArray(PersistentArray):
1464
1441
def __get__ (self ):
1465
1442
# N.B., chunk objects are instantiated lazily, so there may be
1466
1443
# data on disk but no corresponding chunk object yet
1467
- data_dir = os.path.join(self ._path, defaults .datapath)
1444
+ data_dir = os.path.join(self ._path, _defaults .datapath)
1468
1445
return sum (os.path.getsize(os.path.join(data_dir, fn))
1469
1446
for fn in os.listdir(data_dir))
1470
1447
1471
1448
property is_initialized :
1472
1449
def __get__ (self ):
1473
1450
# N.B., chunk objects are instantiated lazily, so there may be
1474
1451
# data on disk but no corresponding chunk object yet
1475
- data_dir = os.path.join(self ._path, defaults .datapath)
1452
+ data_dir = os.path.join(self ._path, _defaults .datapath)
1476
1453
a = np.zeros(self ._cdata_shape, dtype = ' b1' )
1477
- for fn in glob(os.path.join(data_dir, ' *' + defaults .datasuffix)):
1478
- bn = os.path.basename(fn)[:- len (defaults .datasuffix)]
1454
+ for fn in glob(os.path.join(data_dir, ' *' + _defaults .datasuffix)):
1455
+ bn = os.path.basename(fn)[:- len (_defaults .datasuffix)]
1479
1456
cidx = tuple (map (int , bn.split(' .' )))
1480
1457
a[cidx] = True
1481
1458
return a
@@ -1517,14 +1494,14 @@ cdef class LazyPersistentArray(PersistentArray):
1517
1494
_lazy_resize(self , * args)
1518
1495
1519
1496
# write metadata
1520
- metadata = { ' shape ' : self ._shape ,
1521
- ' chunks ' : self ._chunks ,
1522
- ' dtype ' : self ._dtype ,
1523
- ' cname ' : self ._cname ,
1524
- ' clevel ' : self ._clevel ,
1525
- ' shuffle ' : self ._shuffle ,
1526
- ' fill_value ' : self ._fill_value}
1527
- _write_array_metadata( self ._path, metadata )
1497
+ _meta.write_array_metadata( self ._path ,
1498
+ shape = self ._shape ,
1499
+ chunks = self ._chunks ,
1500
+ dtype = self ._dtype ,
1501
+ cname = self ._cname ,
1502
+ clevel = self ._clevel ,
1503
+ shuffle = self ._shuffle,
1504
+ fill_value = self ._fill_value )
1528
1505
1529
1506
1530
1507
# noinspection PyAbstractClass
@@ -1539,8 +1516,8 @@ cdef class SynchronizedLazyPersistentArray(LazyPersistentArray):
1539
1516
return _lazy_get_chunk(self , cidx)
1540
1517
1541
1518
cdef BaseChunk create_chunk(self , tuple cidx):
1542
- chunk_filename = ' .' .join(map (str , cidx)) + defaults .datasuffix
1543
- chunk_path = os.path.join(self ._path, defaults .datapath,
1519
+ chunk_filename = ' .' .join(map (str , cidx)) + _defaults .datasuffix
1520
+ chunk_path = os.path.join(self ._path, _defaults .datapath,
1544
1521
chunk_filename)
1545
1522
return SynchronizedPersistentChunk(
1546
1523
path = chunk_path, shape = self ._chunks, dtype = self ._dtype,
0 commit comments