-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjdata.py
463 lines (413 loc) · 17.3 KB
/
jdata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"""@package docstring
Encoding and decoding python native data structures as
portable JData-spec annotated dict structure
Copyright (c) 2019-2024 Qianqian Fang <q.fang at neu.edu>
"""
__all__ = ["encode", "decode", "jdtype", "jsonfilter"]
##====================================================================================
## dependent libraries
##====================================================================================
import numpy as np
import copy
import zlib
import base64
import os
import re
from .jfile import jdlink
##====================================================================================
## global variables
##====================================================================================
""" @brief Mapping Numpy data types to JData data types
complex-valued data are reflected in the doubled data size
"""
jdtype = {
"float32": "single",
"float64": "double",
"float_": "double",
"bool": "uint8",
"byte": "int8",
"short": "int16",
"ubyte": "uint8",
"ushort": "uint16",
"int_": "int32",
"uint": "uint32",
"complex_": "double",
"complex128": "double",
"complex64": "single",
"longlong": "int64",
"ulonglong": "uint64",
"csingle": "single",
"cdouble": "double",
}
_zipper = (
"zlib",
"gzip",
"lzma",
"lz4",
"blosc2blosclz",
"blosc2lz4",
"blosc2lz4hc",
"blosc2zlib",
"blosc2zstd",
"base64",
)
_allownumpy = ("_ArraySize_", "_ArrayData_", "_ArrayZipSize_", "_ArrayZipData_")
##====================================================================================
## Python to JData encoding function
##====================================================================================
def encode(d, opt={}):
"""@brief Encoding a Python data structure to portable JData-annotated dict constructs
This function converts complex data types (usually not JSON-serializable) into
portable JData-annotated dict/list constructs that can be easily exported as JSON/JData
files
@param[in,out] d: an arbitrary Python data
@param[in] opt: options, can contain a dict with the following keys
'compression': choose one of ['zlib','lzma','gzip','lz4','blosc2blosclz','blosc2lz4',
'blosc2lz4hc','blosc2zlib','blosc2zstd'] for compression codec, default is None
'nthread': number of compression thread of the codec is of the blosc2 class, default is 1
"""
opt.setdefault("inplace", False)
if "compression" in opt:
if opt["compression"] == "lzma":
try:
try:
import lzma
except ImportError:
from backports import lzma
except Exception:
raise Exception(
"JData",
'you must install "lzma" module to compress with this format',
)
elif opt["compression"] == "lz4":
try:
import lz4.frame
except ImportError:
raise Exception(
"JData",
'you must install "lz4" module to compress with this format',
)
elif opt["compression"].startswith("blosc2"):
try:
import blosc2
except ImportError:
raise Exception(
"JData",
'you must install "blosc2" module to compress with this format',
)
if isinstance(d, float):
if np.isnan(d):
return "_NaN_"
elif np.isinf(d):
return "_Inf_" if (d > 0) else "-_Inf_"
return d
elif isinstance(d, list) or isinstance(d, set):
return encodelist(d, opt)
elif isinstance(d, tuple) or isinstance(d, frozenset):
return encodelist(list(d), opt)
elif isinstance(d, dict):
return encodedict(d, opt)
elif isinstance(d, complex):
newobj = {
"_ArrayType_": "double",
"_ArraySize_": 1,
"_ArrayIsComplex_": True,
"_ArrayData_": [d.real, d.imag],
}
return newobj
elif isinstance(d, np.ndarray) or np.iscomplex(d):
newobj = {}
newobj["_ArrayType_"] = (
jdtype[str(d.dtype)] if (str(d.dtype) in jdtype) else str(d.dtype)
)
if np.isscalar(d):
newobj["_ArraySize_"] = 1
else:
newobj["_ArraySize_"] = list(d.shape)
if (
d.dtype == np.complex64
or d.dtype == np.complex128
or d.dtype == np.csingle
or d.dtype == np.cdouble
):
newobj["_ArrayIsComplex_"] = True
newobj["_ArrayData_"] = np.stack((d.ravel().real, d.ravel().imag))
else:
newobj["_ArrayData_"] = d.ravel()
if "compression" in opt:
if opt["compression"] not in _zipper:
raise Exception(
"JData",
"compression method {} is not supported".format(opt["compression"]),
)
newobj["_ArrayZipType_"] = opt["compression"]
newobj["_ArrayZipSize_"] = [1 + int("_ArrayIsComplex_" in newobj), d.size]
newobj["_ArrayZipData_"] = newobj["_ArrayData_"].data
if opt["compression"] == "zlib":
newobj["_ArrayZipData_"] = zlib.compress(newobj["_ArrayZipData_"])
elif opt["compression"] == "gzip":
gzipper = zlib.compressobj(wbits=(zlib.MAX_WBITS | 16))
newobj["_ArrayZipData_"] = gzipper.compress(newobj["_ArrayZipData_"])
elif opt["compression"] == "lzma":
try:
newobj["_ArrayZipData_"] = lzma.compress(
newobj["_ArrayZipData_"], lzma.FORMAT_ALONE
)
except Exception:
print(
'you must install "lzma" module to compress with this format, ignoring'
)
pass
elif opt["compression"] == "lz4":
try:
newobj["_ArrayZipData_"] = lz4.frame.compress(
newobj["_ArrayZipData_"].tobytes()
)
except ImportError:
print(
'you must install "lz4" module to compress with this format, ignoring'
)
pass
elif opt["compression"].startswith("blosc2"):
try:
BLOSC2CODEC = {
"blosc2blosclz": blosc2.Codec.BLOSCLZ,
"blosc2lz4": blosc2.Codec.LZ4,
"blosc2lz4hc": blosc2.Codec.LZ4HC,
"blosc2zlib": blosc2.Codec.ZLIB,
"blosc2zstd": blosc2.Codec.ZSTD,
}
blosc2nthread = 1
if "nthread" in opt:
blosc2nthread = opt["nthread"]
newobj["_ArrayZipData_"] = blosc2.compress2(
newobj["_ArrayZipData_"],
codec=BLOSC2CODEC[opt["compression"]],
typesize=d.dtype.itemsize,
nthreads=blosc2nthread,
)
except ImportError:
print(
'you must install "blosc2" module to compress with this format, ignoring'
)
pass
if (("base64" in opt) and (opt["base64"])) or opt[
"compression"
] == "base64":
newobj["_ArrayZipData_"] = base64.b64encode(newobj["_ArrayZipData_"])
newobj.pop("_ArrayData_")
return newobj
else:
return copy.deepcopy(d) if opt["inplace"] else d
##====================================================================================
## JData to Python decoding function
##====================================================================================
def decode(d, opt={}):
"""@brief Decoding a JData-annotated dict construct into native Python data
This function converts portable JData-annotated dict/list constructs back to native Python
data structures
@param[in,out] d: an arbitrary Python data, any JData-encoded components will be decoded
@param[in] opt: options, can contain a dict with the following keys
'nthread': number of decompression thread of the codec is of the blosc2 class, default is 1
"""
opt.setdefault("inplace", False)
opt.setdefault("maxlinklevel", 0)
if (
(isinstance(d, str) or type(d) == "unicode")
and len(d) <= 6
and len(d) > 4
and d[-1] == "_"
):
if d == "_NaN_":
return float("nan")
elif d == "_Inf_":
return float("inf")
elif d == "-_Inf_":
return float("-inf")
return d
elif isinstance(d, list) or isinstance(d, set):
return decodelist(d, opt)
elif isinstance(d, tuple) or isinstance(d, frozenset):
return decodelist(list(d), opt)
elif isinstance(d, dict):
if "_ArrayType_" in d:
if isinstance(d["_ArraySize_"], str):
d["_ArraySize_"] = np.frombuffer(bytearray(d["_ArraySize_"]))
if "_ArrayZipData_" in d:
newobj = d["_ArrayZipData_"]
if (("base64" in opt) and (opt["base64"])) or (
"_ArrayZipType_" in d and d["_ArrayZipType_"] == "base64"
):
newobj = base64.b64decode(newobj)
if "_ArrayZipType_" in d and d["_ArrayZipType_"] not in _zipper:
raise Exception(
"JData",
"compression method {} is not supported".format(
d["_ArrayZipType_"]
),
)
if d["_ArrayZipType_"] == "zlib":
newobj = zlib.decompress(bytes(newobj))
elif d["_ArrayZipType_"] == "gzip":
newobj = zlib.decompress(bytes(newobj), zlib.MAX_WBITS | 32)
elif d["_ArrayZipType_"] == "lzma":
try:
import lzma
except ImportError:
from backports import lzma
buf = bytearray(newobj) # set length to -1 (unknown) if EOF appears
buf[5:13] = b"\xff\xff\xff\xff\xff\xff\xff\xff"
newobj = lzma.decompress(buf, lzma.FORMAT_ALONE)
elif d["_ArrayZipType_"] == "lz4":
try:
import lz4.frame
newobj = lz4.frame.decompress(bytes(newobj))
except Exception:
print(
'Warning: you must install "lz4" module to decompress a data record in this file, ignoring'
)
return copy.deepcopy(d) if opt["inplace"] else d
elif d["_ArrayZipType_"].startswith("blosc2"):
try:
import blosc2
blosc2nthread = 1
if "nthread" in opt:
blosc2nthread = opt["nthread"]
newobj = blosc2.decompress2(
bytes(newobj), as_bytearray=False, nthreads=blosc2nthread
)
except Exception:
print(
'Warning: you must install "blosc2" module to decompress a data record in this file, ignoring'
)
return copy.deepcopy(d) if opt["inplace"] else d
newobj = np.frombuffer(
bytearray(newobj), dtype=np.dtype(d["_ArrayType_"])
).reshape(d["_ArrayZipSize_"])
if "_ArrayIsComplex_" in d and newobj.shape[0] == 2:
newobj = newobj[0] + 1j * newobj[1]
if "_ArrayOrder_" in d and (
d["_ArrayOrder_"].lower() == "c"
or d["_ArrayOrder_"].lower() == "col"
or d["_ArrayOrder_"].lower() == "column"
):
newobj = newobj.reshape(d["_ArraySize_"], order="F")
else:
newobj = newobj.reshape(d["_ArraySize_"])
if not hasattr(d["_ArraySize_"], "__iter__") and d["_ArraySize_"] == 1:
newobj = newobj.item()
return newobj
elif "_ArrayData_" in d:
if isinstance(d["_ArrayData_"], str):
newobj = np.frombuffer(
d["_ArrayData_"], dtype=np.dtype(d["_ArrayType_"])
)
else:
newobj = np.asarray(
d["_ArrayData_"], dtype=np.dtype(d["_ArrayType_"])
)
if "_ArrayZipSize_" in d and newobj.shape[0] == 1:
if isinstance(d["_ArrayZipSize_"], str):
d["_ArrayZipSize_"] = np.frombuffer(
bytearray(d["_ArrayZipSize_"])
)
newobj = newobj.reshape(d["_ArrayZipSize_"])
if "_ArrayIsComplex_" in d and newobj.shape[0] == 2:
newobj = newobj[0] + 1j * newobj[1]
if "_ArrayOrder_" in d and (
d["_ArrayOrder_"].lower() == "c"
or d["_ArrayOrder_"].lower() == "col"
or d["_ArrayOrder_"].lower() == "column"
):
newobj = newobj.reshape(d["_ArraySize_"], order="F")
else:
newobj = newobj.reshape(d["_ArraySize_"])
if not hasattr(d["_ArraySize_"], "__iter__") and d["_ArraySize_"] == 1:
newobj = newobj.item()
return newobj
else:
raise Exception(
"JData",
"one and only one of _ArrayData_ or _ArrayZipData_ is required",
)
elif "_DataLink_" in d:
if opt["maxlinklevel"] > 0 and "_DataLink_" in data:
if isinstance(data["_DataLink_"], str):
datalink = data["_DataLink_"]
if re.search("\:\$", datalink):
ref = re.search(
"^(?P<proto>[a-zA-Z]+://)*(?P<path>.+)(?P<delim>\:)()*(?P<jsonpath>(?<=:)\$\d*\.*.*)*",
datalink,
)
else:
ref = re.search(
"^(?P<proto>[a-zA-Z]+://)*(?P<path>.+)(?P<delim>\:)*(?P<jsonpath>(?<=:)\$\d*\..*)*",
datalink,
)
if ref and ref.group("path"):
uripath = ref.group("proto") + ref.group("path")
newobj, fname = jdlink(uripath)
if os.path.exists(fname):
opt["maxlinklevel"] = opt["maxlinklevel"] - 1
if ref.group("jsonpath"):
newobj = jsonpath(newdata, ref.group("jsonpath"))
return nrewobj
else:
raise Exception(
"JData",
"_DataLink_ contains invalid URL",
)
return decodedict(d, opt)
else:
return copy.deepcopy(d) if opt["inplace"] else d
##====================================================================================
## helper functions
##====================================================================================
def jsonfilter(obj):
if type(obj) == "long":
return str(obj)
elif type(obj).__module__ == np.__name__:
if isinstance(obj, np.ndarray):
return obj.tolist()
else:
return obj.item()
elif isinstance(obj, (bytes, bytearray)):
return obj.decode("utf-8")
elif isinstance(obj, float):
if np.isnan(obj):
return "_NaN_"
elif np.isinf(obj):
return "_Inf_" if (obj > 0) else "-_Inf_"
# -------------------------------------------------------------------------------------
def encodedict(d0, opt={}):
d = dict(d0)
for k, v in d0.items():
if isinstance(v, np.ndarray) and isinstance(k, str) and (k in _allownumpy):
continue
newkey = encode(k, opt)
d[newkey] = encode(v, opt)
if k != newkey:
d.pop(k)
return d
# -------------------------------------------------------------------------------------
def encodelist(d0, opt={}):
d = copy.deepcopy(d0) if opt["inplace"] else d0
for i, s in enumerate(d):
d[i] = encode(s, opt)
return d
# -------------------------------------------------------------------------------------
def decodedict(d0, opt={}):
d = dict(d0)
for k, v in d.items():
newkey = encode(k, opt)
d[newkey] = decode(v, opt)
if k != newkey:
d.pop(k)
return d
# -------------------------------------------------------------------------------------
def decodelist(d0, opt={}):
d = copy.deepcopy(d0) if opt["inplace"] else d0
for i, s in enumerate(d):
d[i] = decode(s, opt)
return d
# -------------------------------------------------------------------------------------