Skip to content

Commit ce25fa5

Browse files
committed
add blosc2 class compression codecs
1 parent af6c79a commit ce25fa5

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

jdata/jdata.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
'complex64':'single','longlong':'int64','ulonglong':'uint64',
3131
'csingle':'single','cdouble':'double'};
3232

33-
_zipper=['zlib','gzip','lzma','lz4','base64'];
33+
_zipper=['zlib','gzip','lzma','lz4','blosc2blosclz','blosc2lz4','blosc2lz4hc','blosc2zlib','blosc2zstd','base64'];
3434

3535
##====================================================================================
3636
## Python to JData encoding function
@@ -60,6 +60,11 @@ def encode(d, opt={}):
6060
import lz4.frame
6161
except ImportError:
6262
raise Exception('JData', 'you must install "lz4" module to compress with this format')
63+
elif(opt['compression'].startswith('blosc2')):
64+
try:
65+
import blosc2
66+
except ImportError:
67+
raise Exception('JData', 'you must install "blosc2" module to compress with this format')
6368

6469
if isinstance(d, float):
6570
if(np.isnan(d)):
@@ -104,21 +109,30 @@ def encode(d, opt={}):
104109
newobj['_ArrayZipData_']=zlib.compress(newobj['_ArrayZipData_'],zlib.MAX_WBITS|32);
105110
elif(opt['compression']=='lzma'):
106111
try:
107-
try:
108-
import lzma
109-
except ImportError:
110-
from backports import lzma
111112
newobj['_ArrayZipData_']=lzma.compress(newobj['_ArrayZipData_'],lzma.FORMAT_ALONE);
112113
except Exception:
113114
print('you must install "lzma" module to compress with this format, ignoring')
114115
pass
115116
elif(opt['compression']=='lz4'):
116117
try:
117-
import lz4.frame
118118
newobj['_ArrayZipData_']=lz4.frame.compress(newobj['_ArrayZipData_']);
119119
except ImportError:
120120
print('you must install "lz4" module to compress with this format, ignoring')
121121
pass
122+
elif(opt['compression'].startswith('blosc2')):
123+
try:
124+
BLOSC2CODEC={
125+
'blosc2blosclz': blosc2.Codec.BLOSCLZ, 'blosc2lz4': blosc2.Codec.LZ4,
126+
'blosc2lz4hc': blosc2.Codec.LZ4HC, 'blosc2zlib': blosc2.Codec.ZLIB,
127+
'blosc2zstd': blosc2.Codec.ZSTD
128+
}
129+
blosc2nthread = 1
130+
if('nthread' in opt):
131+
blosc2nthread = opt['nthread']
132+
newobj['_ArrayZipData_']=blosc2.compress2(newobj['_ArrayZipData_'], compcode=BLOSC2CODEC[opt['compression']], typesize=d.dtype.itemsize, nthread=blosc2nthread)
133+
except ImportError:
134+
print('you must install "blosc2" module to compress with this format, ignoring')
135+
pass
122136
if(('base64' in opt) and (opt['base64'])) or opt['compression']=='base64':
123137
newobj['_ArrayZipData_']=base64.b64encode(newobj['_ArrayZipData_']);
124138
newobj.pop('_ArrayData_');
@@ -178,6 +192,16 @@ def decode(d, opt={}):
178192
except Exception:
179193
print('Warning: you must install "lz4" module to decompress a data record in this file, ignoring')
180194
pass
195+
elif(d['_ArrayZipType_'].startswith('blosc2')):
196+
try:
197+
import blosc2
198+
blosc2nthread = 1
199+
if('nthread' in opt):
200+
blosc2nthread = opt['nthread']
201+
newobj=blosc2.decompress2(bytes(newobj), as_bytearray=False, nthread=blosc2nthread)
202+
except Exception:
203+
print('Warning: you must install "blosc2" module to decompress a data record in this file, ignoring')
204+
pass
181205
newobj=np.fromstring(newobj,dtype=np.dtype(d['_ArrayType_'])).reshape(d['_ArrayZipSize_']);
182206
if('_ArrayIsComplex_' in d and newobj.shape[0]==2):
183207
newobj=newobj[0]+1j*newobj[1];

0 commit comments

Comments
 (0)