bjdata 0.5.4__tar.gz → 0.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bjdata-0.5.4 → bjdata-0.6.0}/PKG-INFO +6 -5
- {bjdata-0.5.4 → bjdata-0.6.0}/README.md +5 -4
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata/__init__.py +2 -2
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata/decoder.py +151 -7
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata/encoder.py +168 -4
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata.egg-info/PKG-INFO +6 -5
- {bjdata-0.5.4 → bjdata-0.6.0}/ez_setup.py +144 -112
- {bjdata-0.5.4 → bjdata-0.6.0}/setup.py +2 -2
- {bjdata-0.5.4 → bjdata-0.6.0}/src/_bjdata.c +33 -9
- {bjdata-0.5.4 → bjdata-0.6.0}/src/common.h +12 -1
- {bjdata-0.5.4 → bjdata-0.6.0}/src/decoder.c +506 -1
- {bjdata-0.5.4 → bjdata-0.6.0}/src/decoder.h +64 -2
- {bjdata-0.5.4 → bjdata-0.6.0}/src/encoder.c +395 -3
- {bjdata-0.5.4 → bjdata-0.6.0}/src/encoder.h +55 -1
- bjdata-0.6.0/test/test.py +2367 -0
- bjdata-0.5.4/test/test.py +0 -1250
- {bjdata-0.5.4 → bjdata-0.6.0}/Binary_JData_Specification.md +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/CHANGELOG +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/LICENSE +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/MANIFEST.in +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/NOTICE +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/UBJSON-Specification.md +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata/__main__.py +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata/compat.py +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata/markers.py +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata.egg-info/SOURCES.txt +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata.egg-info/dependency_links.txt +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata.egg-info/not-zip-safe +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata.egg-info/requires.txt +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/bjdata.egg-info/top_level.txt +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/pyproject.toml +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/setup.cfg +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/src/markers.h +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/src/numpyapi.h +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/src/python_funcs.c +0 -0
- {bjdata-0.5.4 → bjdata-0.6.0}/src/python_funcs.h +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bjdata
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Binary JData and UBJSON encoder/decoder
|
|
5
5
|
Home-page: https://github.com/NeuroJSON/pybj
|
|
6
6
|
Author: Qianqian Fang
|
|
@@ -58,16 +58,16 @@ Dynamic: summary
|
|
|
58
58
|
- Copyright: (C) Qianqian Fang (2020-2025) <q.fang at neu.edu>
|
|
59
59
|
- Copyright: (C) Iotic Labs Ltd. (2016-2019) <vilnis.termanis at iotic-labs.com>
|
|
60
60
|
- License: Apache License, Version 2.0
|
|
61
|
-
- Version: 0.
|
|
61
|
+
- Version: 0.6.0
|
|
62
62
|
- URL: https://pypi.org/project/bjdata/
|
|
63
63
|
- Github: https://github.com/NeuroJSON/pybj
|
|
64
|
-
- BJData Spec Version: [
|
|
64
|
+
- BJData Spec Version: [Draft 4](https://neurojson.org/bjdata/draft4)
|
|
65
65
|
- Acknowledgement: This project is supported by US National Institute of Health (NIH) grant U24-NS124027
|
|
66
66
|
|
|
67
67
|

|
|
68
68
|
|
|
69
69
|
This is a Python v3.2+ (and 2.7+) [Binary JData](https://neurojson.org) encoder
|
|
70
|
-
and decoder based on the [Draft-
|
|
70
|
+
and decoder based on the [Draft-4](Binary_JData_Specification.md) specification.
|
|
71
71
|
|
|
72
72
|
## Installing / packaging
|
|
73
73
|
```shell
|
|
@@ -190,7 +190,7 @@ This package was modified based on the py-ubjson package developed by
|
|
|
190
190
|
Project URL: https://github.com/Iotic-Labs/py-ubjson
|
|
191
191
|
|
|
192
192
|
The major changes were focused on supporting the Binary JData Specification
|
|
193
|
-
[Draft
|
|
193
|
+
[Draft 4](https://neurojson.org/bjdata/draft4) -
|
|
194
194
|
an extended Universal Binary JSON (UBJSON) Specification Draft-12 by adding
|
|
195
195
|
the below new features:
|
|
196
196
|
|
|
@@ -200,3 +200,4 @@ the below new features:
|
|
|
200
200
|
* BJData does not convert NaN/Inf/-Inf to `null`
|
|
201
201
|
* BJData uses little-Endian as the default integer/floating-point numbers while UBJSON uses big-Endian
|
|
202
202
|
* BJData only permits non-zero-fixed-length data types (`UiuImlMLhdDCB`) in strongly-typed array/object containers
|
|
203
|
+
* BJData Draft-4 adds new optimized container Stucture-of-Arrays (SoA) for packed objects.
|
|
@@ -5,16 +5,16 @@
|
|
|
5
5
|
- Copyright: (C) Qianqian Fang (2020-2025) <q.fang at neu.edu>
|
|
6
6
|
- Copyright: (C) Iotic Labs Ltd. (2016-2019) <vilnis.termanis at iotic-labs.com>
|
|
7
7
|
- License: Apache License, Version 2.0
|
|
8
|
-
- Version: 0.
|
|
8
|
+
- Version: 0.6.0
|
|
9
9
|
- URL: https://pypi.org/project/bjdata/
|
|
10
10
|
- Github: https://github.com/NeuroJSON/pybj
|
|
11
|
-
- BJData Spec Version: [
|
|
11
|
+
- BJData Spec Version: [Draft 4](https://neurojson.org/bjdata/draft4)
|
|
12
12
|
- Acknowledgement: This project is supported by US National Institute of Health (NIH) grant U24-NS124027
|
|
13
13
|
|
|
14
14
|

|
|
15
15
|
|
|
16
16
|
This is a Python v3.2+ (and 2.7+) [Binary JData](https://neurojson.org) encoder
|
|
17
|
-
and decoder based on the [Draft-
|
|
17
|
+
and decoder based on the [Draft-4](Binary_JData_Specification.md) specification.
|
|
18
18
|
|
|
19
19
|
## Installing / packaging
|
|
20
20
|
```shell
|
|
@@ -137,7 +137,7 @@ This package was modified based on the py-ubjson package developed by
|
|
|
137
137
|
Project URL: https://github.com/Iotic-Labs/py-ubjson
|
|
138
138
|
|
|
139
139
|
The major changes were focused on supporting the Binary JData Specification
|
|
140
|
-
[Draft
|
|
140
|
+
[Draft 4](https://neurojson.org/bjdata/draft4) -
|
|
141
141
|
an extended Universal Binary JSON (UBJSON) Specification Draft-12 by adding
|
|
142
142
|
the below new features:
|
|
143
143
|
|
|
@@ -147,3 +147,4 @@ the below new features:
|
|
|
147
147
|
* BJData does not convert NaN/Inf/-Inf to `null`
|
|
148
148
|
* BJData uses little-Endian as the default integer/floating-point numbers while UBJSON uses big-Endian
|
|
149
149
|
* BJData only permits non-zero-fixed-length data types (`UiuImlMLhdDCB`) in strongly-typed array/object containers
|
|
150
|
+
* BJData Draft-4 adds new optimized container Stucture-of-Arrays (SoA) for packed objects.
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
# limitations under the License.
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
"""BJData (Draft
|
|
17
|
+
"""BJData (Draft 4) and UBJSON (Draft 12) implementation
|
|
18
18
|
|
|
19
19
|
Example usage:
|
|
20
20
|
|
|
@@ -40,7 +40,7 @@ except ImportError: # pragma: no cover
|
|
|
40
40
|
from .encoder import EncoderException
|
|
41
41
|
from .decoder import DecoderException
|
|
42
42
|
|
|
43
|
-
__version__ = "0.
|
|
43
|
+
__version__ = "0.6.0"
|
|
44
44
|
|
|
45
45
|
__all__ = (
|
|
46
46
|
"EXTENSION_ENABLED",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
# limitations under the License.
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
"""BJData (Draft
|
|
17
|
+
"""BJData (Draft 4) and UBJSON decoder with SOA support"""
|
|
18
18
|
|
|
19
19
|
from io import BytesIO
|
|
20
20
|
from struct import Struct, pack, error as StructError
|
|
@@ -55,6 +55,8 @@ from numpy import (
|
|
|
55
55
|
dtype as npdtype,
|
|
56
56
|
frombuffer as buffer2numpy,
|
|
57
57
|
half as halfprec,
|
|
58
|
+
zeros as npzeros,
|
|
59
|
+
empty as npempty,
|
|
58
60
|
)
|
|
59
61
|
from array import array as typedarray
|
|
60
62
|
|
|
@@ -164,6 +166,24 @@ __DTYPELEN_MAP = {
|
|
|
164
166
|
TYPE_CHAR: 1,
|
|
165
167
|
}
|
|
166
168
|
|
|
169
|
+
# Numpy dtype strings for SOA
|
|
170
|
+
__NUMPY_DTYPE_MAP = {
|
|
171
|
+
TYPE_BYTE: "u1",
|
|
172
|
+
TYPE_INT8: "i1",
|
|
173
|
+
TYPE_UINT8: "u1",
|
|
174
|
+
TYPE_INT16: "i2",
|
|
175
|
+
TYPE_UINT16: "u2",
|
|
176
|
+
TYPE_INT32: "i4",
|
|
177
|
+
TYPE_UINT32: "u4",
|
|
178
|
+
TYPE_INT64: "i8",
|
|
179
|
+
TYPE_UINT64: "u8",
|
|
180
|
+
TYPE_FLOAT16: "f2",
|
|
181
|
+
TYPE_FLOAT32: "f4",
|
|
182
|
+
TYPE_FLOAT64: "f8",
|
|
183
|
+
TYPE_BOOL_TRUE: "?",
|
|
184
|
+
TYPE_BOOL_FALSE: "?",
|
|
185
|
+
}
|
|
186
|
+
|
|
167
187
|
|
|
168
188
|
class DecoderException(ValueError):
|
|
169
189
|
"""Raised when decoding of a UBJSON stream fails."""
|
|
@@ -358,6 +378,106 @@ def prodlist(mylist):
|
|
|
358
378
|
return result
|
|
359
379
|
|
|
360
380
|
|
|
381
|
+
def __decode_soa_schema(fp_read, intern_object_keys, le):
|
|
382
|
+
"""Decode SOA schema: {field1:type1, field2:type2, ...}"""
|
|
383
|
+
schema = []
|
|
384
|
+
marker = fp_read(1)
|
|
385
|
+
|
|
386
|
+
while marker != OBJECT_END:
|
|
387
|
+
if marker == TYPE_NOOP:
|
|
388
|
+
marker = fp_read(1)
|
|
389
|
+
continue
|
|
390
|
+
|
|
391
|
+
# Decode field name
|
|
392
|
+
field_name = __decode_object_key(fp_read, marker, intern_object_keys, le)
|
|
393
|
+
|
|
394
|
+
# Decode field type marker
|
|
395
|
+
type_marker = fp_read(1)
|
|
396
|
+
if type_marker not in __TYPES_FIXLEN and type_marker not in (
|
|
397
|
+
TYPE_BOOL_TRUE,
|
|
398
|
+
TYPE_BOOL_FALSE,
|
|
399
|
+
):
|
|
400
|
+
raise DecoderException("SOA schema only supports fixed-length types")
|
|
401
|
+
|
|
402
|
+
schema.append((field_name, type_marker))
|
|
403
|
+
marker = fp_read(1)
|
|
404
|
+
|
|
405
|
+
return schema
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
def __decode_soa(fp_read, schema, is_row_major, intern_object_keys, le):
|
|
409
|
+
"""Decode SOA payload into numpy structured array"""
|
|
410
|
+
# Read count (can be scalar or ND dimensions)
|
|
411
|
+
marker = fp_read(1)
|
|
412
|
+
if marker != CONTAINER_COUNT:
|
|
413
|
+
raise DecoderException("Expected # after SOA schema")
|
|
414
|
+
|
|
415
|
+
marker = fp_read(1)
|
|
416
|
+
if marker == ARRAY_START:
|
|
417
|
+
# ND dimensions
|
|
418
|
+
dims = []
|
|
419
|
+
marker = fp_read(1)
|
|
420
|
+
while marker != ARRAY_END:
|
|
421
|
+
if marker in __TYPES_INT:
|
|
422
|
+
dims.append(__METHOD_MAP[marker](fp_read, marker, le))
|
|
423
|
+
marker = fp_read(1)
|
|
424
|
+
count = prodlist(dims)
|
|
425
|
+
else:
|
|
426
|
+
# Scalar count
|
|
427
|
+
count = __decode_int_non_negative(fp_read, marker, le)
|
|
428
|
+
dims = [count]
|
|
429
|
+
|
|
430
|
+
# Build numpy dtype for structured array
|
|
431
|
+
dtype_list = []
|
|
432
|
+
for field_name, type_marker in schema:
|
|
433
|
+
if type_marker in (TYPE_BOOL_TRUE, TYPE_BOOL_FALSE):
|
|
434
|
+
dtype_list.append((field_name, "?"))
|
|
435
|
+
else:
|
|
436
|
+
dtype_list.append((field_name, __NUMPY_DTYPE_MAP[type_marker]))
|
|
437
|
+
|
|
438
|
+
struct_dtype = npdtype(dtype_list)
|
|
439
|
+
result = npempty(count, dtype=struct_dtype)
|
|
440
|
+
|
|
441
|
+
if is_row_major:
|
|
442
|
+
# Row-major: interleaved - read one record at a time
|
|
443
|
+
for i in range(count):
|
|
444
|
+
for field_name, type_marker in schema:
|
|
445
|
+
if type_marker in (TYPE_BOOL_TRUE, TYPE_BOOL_FALSE):
|
|
446
|
+
# Boolean: read T or F byte
|
|
447
|
+
bool_byte = fp_read(1)
|
|
448
|
+
result[field_name][i] = bool_byte == TYPE_BOOL_TRUE
|
|
449
|
+
else:
|
|
450
|
+
# Numeric: read raw bytes
|
|
451
|
+
nbytes = __DTYPELEN_MAP[type_marker]
|
|
452
|
+
raw = fp_read(nbytes)
|
|
453
|
+
value = buffer2numpy(
|
|
454
|
+
raw, dtype=npdtype(__NUMPY_DTYPE_MAP[type_marker])
|
|
455
|
+
)[0]
|
|
456
|
+
result[field_name][i] = value
|
|
457
|
+
else:
|
|
458
|
+
# Column-major: all values of field1, then field2, etc.
|
|
459
|
+
for field_name, type_marker in schema:
|
|
460
|
+
if type_marker in (TYPE_BOOL_TRUE, TYPE_BOOL_FALSE):
|
|
461
|
+
# Boolean: read T/F bytes
|
|
462
|
+
bool_bytes = fp_read(count)
|
|
463
|
+
for i in range(count):
|
|
464
|
+
result[field_name][i] = bool_bytes[i : i + 1] == TYPE_BOOL_TRUE
|
|
465
|
+
else:
|
|
466
|
+
# Numeric: read all values at once
|
|
467
|
+
nbytes = __DTYPELEN_MAP[type_marker]
|
|
468
|
+
raw = fp_read(count * nbytes)
|
|
469
|
+
values = buffer2numpy(
|
|
470
|
+
raw, dtype=npdtype(__NUMPY_DTYPE_MAP[type_marker])
|
|
471
|
+
)
|
|
472
|
+
result[field_name] = values
|
|
473
|
+
|
|
474
|
+
# Reshape if ND
|
|
475
|
+
if len(dims) > 1:
|
|
476
|
+
result = result.reshape(dims)
|
|
477
|
+
|
|
478
|
+
return result
|
|
479
|
+
|
|
480
|
+
|
|
361
481
|
def __get_container_params(
|
|
362
482
|
fp_read,
|
|
363
483
|
in_mapping,
|
|
@@ -372,6 +492,13 @@ def __get_container_params(
|
|
|
372
492
|
dims = []
|
|
373
493
|
if marker == CONTAINER_TYPE:
|
|
374
494
|
marker = fp_read(1)
|
|
495
|
+
|
|
496
|
+
# Check for SOA: ${ indicates schema object
|
|
497
|
+
if marker == OBJECT_START:
|
|
498
|
+
# This is SOA format - decode schema
|
|
499
|
+
schema = __decode_soa_schema(fp_read, intern_object_keys, islittle)
|
|
500
|
+
return marker, True, -1, schema, [], True # -1 count signals SOA
|
|
501
|
+
|
|
375
502
|
if marker not in __TYPES:
|
|
376
503
|
raise DecoderException("Invalid container type")
|
|
377
504
|
type_ = marker
|
|
@@ -413,7 +540,7 @@ def __get_container_params(
|
|
|
413
540
|
counting = False
|
|
414
541
|
else:
|
|
415
542
|
raise DecoderException("Container type without count")
|
|
416
|
-
return marker, counting, count, type_, dims
|
|
543
|
+
return marker, counting, count, type_, dims, False
|
|
417
544
|
|
|
418
545
|
|
|
419
546
|
def __decode_object(
|
|
@@ -425,7 +552,7 @@ def __decode_object(
|
|
|
425
552
|
intern_object_keys,
|
|
426
553
|
islittle,
|
|
427
554
|
):
|
|
428
|
-
|
|
555
|
+
result = __get_container_params(
|
|
429
556
|
fp_read,
|
|
430
557
|
True,
|
|
431
558
|
no_bytes,
|
|
@@ -435,6 +562,13 @@ def __decode_object(
|
|
|
435
562
|
intern_object_keys,
|
|
436
563
|
islittle,
|
|
437
564
|
)
|
|
565
|
+
|
|
566
|
+
# Check if this is SOA format
|
|
567
|
+
if len(result) == 6 and result[5]: # is_soa flag
|
|
568
|
+
schema = result[3]
|
|
569
|
+
return __decode_soa(fp_read, schema, False, intern_object_keys, islittle)
|
|
570
|
+
|
|
571
|
+
marker, counting, count, type_, dims, _ = result
|
|
438
572
|
has_pairs_hook = object_pairs_hook is not None
|
|
439
573
|
obj = [] if has_pairs_hook else {}
|
|
440
574
|
|
|
@@ -524,7 +658,7 @@ def __decode_array(
|
|
|
524
658
|
intern_object_keys,
|
|
525
659
|
islittle,
|
|
526
660
|
):
|
|
527
|
-
|
|
661
|
+
result = __get_container_params(
|
|
528
662
|
fp_read,
|
|
529
663
|
False,
|
|
530
664
|
no_bytes,
|
|
@@ -535,6 +669,13 @@ def __decode_array(
|
|
|
535
669
|
islittle,
|
|
536
670
|
)
|
|
537
671
|
|
|
672
|
+
# Check if this is SOA format (row-major)
|
|
673
|
+
if len(result) == 6 and result[5]: # is_soa flag
|
|
674
|
+
schema = result[3]
|
|
675
|
+
return __decode_soa(fp_read, schema, True, intern_object_keys, islittle)
|
|
676
|
+
|
|
677
|
+
marker, counting, count, type_, dims, _ = result
|
|
678
|
+
|
|
538
679
|
# special case - no data (None or bool)
|
|
539
680
|
if type_ in __TYPES_NO_DATA:
|
|
540
681
|
return [__METHOD_MAP[type_](fp_read, type_, islittle)] * count
|
|
@@ -566,7 +707,7 @@ def __decode_array(
|
|
|
566
707
|
container = buffer2numpy(container, dtype=npdtype(__DTYPE_MAP[type_]))
|
|
567
708
|
return container
|
|
568
709
|
|
|
569
|
-
container =
|
|
710
|
+
container = list()
|
|
570
711
|
while count > 0 and (counting or marker != ARRAY_END):
|
|
571
712
|
if marker == TYPE_NOOP:
|
|
572
713
|
marker = fp_read(1)
|
|
@@ -645,7 +786,7 @@ def load(
|
|
|
645
786
|
any other array (i.e. result in a list).
|
|
646
787
|
uint8_bytes (bool): If set, typed UBJSON arrays (uint8) will be
|
|
647
788
|
converted to a bytes instance instead of being
|
|
648
|
-
treated as an array (for UBJSON & BJData Draft
|
|
789
|
+
treated as an array (for UBJSON & BJData Draft 4).
|
|
649
790
|
Ignored if no_bytes is set.
|
|
650
791
|
object_hook (callable): Called with the result of any object literal
|
|
651
792
|
decoded (instead of dict).
|
|
@@ -659,7 +800,7 @@ def load(
|
|
|
659
800
|
in Python2 (since interning does not apply
|
|
660
801
|
to unicode) and wil be ignored.
|
|
661
802
|
islittle (1 or 0): default is 1 for little-endian for all numerics (for
|
|
662
|
-
BJData Draft
|
|
803
|
+
BJData Draft 4), change to 0 to use big-endian
|
|
663
804
|
(for UBJSON & BJData Draft 1)
|
|
664
805
|
|
|
665
806
|
Returns:
|
|
@@ -697,6 +838,9 @@ def load(
|
|
|
697
838
|
+----------------------------------+---------------+
|
|
698
839
|
| null | None |
|
|
699
840
|
+----------------------------------+---------------+
|
|
841
|
+
|
|
842
|
+
SOA (Structure of Arrays) format is automatically detected and decoded
|
|
843
|
+
to numpy structured arrays (record arrays).
|
|
700
844
|
"""
|
|
701
845
|
if object_pairs_hook is None and object_hook is None:
|
|
702
846
|
object_hook = __object_hook_noop
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
# limitations under the License.
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
"""BJData (Draft
|
|
17
|
+
"""BJData (Draft 4) and UBJSON encoder with SOA support"""
|
|
18
18
|
|
|
19
19
|
from struct import pack, Struct
|
|
20
20
|
from decimal import Decimal
|
|
@@ -91,6 +91,23 @@ __DTYPE_TO_MARKER = {
|
|
|
91
91
|
"S1": TYPE_CHAR,
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
+
# For SOA encoding
|
|
95
|
+
__NUMPY_DTYPE_TO_MARKER = {
|
|
96
|
+
"i1": TYPE_INT8,
|
|
97
|
+
"i2": TYPE_INT16,
|
|
98
|
+
"i4": TYPE_INT32,
|
|
99
|
+
"i8": TYPE_INT64,
|
|
100
|
+
"u1": TYPE_UINT8,
|
|
101
|
+
"u2": TYPE_UINT16,
|
|
102
|
+
"u4": TYPE_UINT32,
|
|
103
|
+
"u8": TYPE_UINT64,
|
|
104
|
+
"f2": TYPE_FLOAT16,
|
|
105
|
+
"f4": TYPE_FLOAT32,
|
|
106
|
+
"f8": TYPE_FLOAT64,
|
|
107
|
+
"b1": TYPE_BOOL_TRUE, # Boolean uses T marker in schema
|
|
108
|
+
"?": TYPE_BOOL_TRUE, # Boolean
|
|
109
|
+
}
|
|
110
|
+
|
|
94
111
|
# Prefix applicable to specialised byte array container
|
|
95
112
|
__BYTES_ARRAY_PREFIX = ARRAY_START + CONTAINER_TYPE + TYPE_BYTE + CONTAINER_COUNT
|
|
96
113
|
__BYTES_ARRAY_PREFIX_DRAFT2 = (
|
|
@@ -195,6 +212,120 @@ def __encode_bytes(fp_write, item, uint8_bytes, le=1):
|
|
|
195
212
|
# no ARRAY_END since length was specified
|
|
196
213
|
|
|
197
214
|
|
|
215
|
+
def __get_numpy_dtype_marker(dtype_str):
|
|
216
|
+
"""Get BJData type marker from numpy dtype string"""
|
|
217
|
+
# Handle endianness prefix
|
|
218
|
+
if len(dtype_str) >= 2 and dtype_str[0] in "<>|":
|
|
219
|
+
dtype_str = dtype_str[1:]
|
|
220
|
+
|
|
221
|
+
if dtype_str in __NUMPY_DTYPE_TO_MARKER:
|
|
222
|
+
return __NUMPY_DTYPE_TO_MARKER[dtype_str]
|
|
223
|
+
|
|
224
|
+
# Handle boolean
|
|
225
|
+
if dtype_str.startswith("b") or dtype_str == "?":
|
|
226
|
+
return TYPE_BOOL_TRUE
|
|
227
|
+
|
|
228
|
+
return None
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def __can_encode_as_soa(item):
|
|
232
|
+
"""Check if numpy structured array can be encoded as SOA"""
|
|
233
|
+
try:
|
|
234
|
+
import numpy as np
|
|
235
|
+
except ImportError:
|
|
236
|
+
return False
|
|
237
|
+
|
|
238
|
+
if not isinstance(item, np.ndarray):
|
|
239
|
+
return False
|
|
240
|
+
|
|
241
|
+
# Must be structured array with named fields
|
|
242
|
+
if item.dtype.names is None:
|
|
243
|
+
return False
|
|
244
|
+
|
|
245
|
+
# All fields must be scalar fixed-length types
|
|
246
|
+
for name in item.dtype.names:
|
|
247
|
+
field_dtype = item.dtype.fields[name][0]
|
|
248
|
+
# Check if it's a simple scalar type (not nested array or object)
|
|
249
|
+
if field_dtype.shape != ():
|
|
250
|
+
return False
|
|
251
|
+
marker = __get_numpy_dtype_marker(field_dtype.str)
|
|
252
|
+
if marker is None:
|
|
253
|
+
return False
|
|
254
|
+
|
|
255
|
+
return True
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def __encode_soa(fp_write, item, soa_format, le):
|
|
259
|
+
"""Encode numpy structured array as SOA format"""
|
|
260
|
+
import numpy as np
|
|
261
|
+
|
|
262
|
+
is_row_major = soa_format in ("row", "r")
|
|
263
|
+
count = item.size
|
|
264
|
+
dims = item.shape
|
|
265
|
+
|
|
266
|
+
# Flatten for iteration
|
|
267
|
+
flat_item = item.flatten()
|
|
268
|
+
|
|
269
|
+
# Build schema
|
|
270
|
+
schema = []
|
|
271
|
+
for name in item.dtype.names:
|
|
272
|
+
field_dtype = item.dtype.fields[name][0]
|
|
273
|
+
marker = __get_numpy_dtype_marker(field_dtype.str)
|
|
274
|
+
schema.append((name, marker, field_dtype))
|
|
275
|
+
|
|
276
|
+
# Write header: [$ or {$ + schema + # + count
|
|
277
|
+
if is_row_major:
|
|
278
|
+
fp_write(ARRAY_START)
|
|
279
|
+
else:
|
|
280
|
+
fp_write(OBJECT_START)
|
|
281
|
+
|
|
282
|
+
fp_write(CONTAINER_TYPE)
|
|
283
|
+
|
|
284
|
+
# Write schema object
|
|
285
|
+
fp_write(OBJECT_START)
|
|
286
|
+
for name, marker, _ in schema:
|
|
287
|
+
# Write field name
|
|
288
|
+
encoded_name = name.encode("utf-8")
|
|
289
|
+
__encode_int(fp_write, len(encoded_name), le)
|
|
290
|
+
fp_write(encoded_name)
|
|
291
|
+
# Write type marker
|
|
292
|
+
fp_write(marker)
|
|
293
|
+
fp_write(OBJECT_END)
|
|
294
|
+
|
|
295
|
+
# Write count
|
|
296
|
+
fp_write(CONTAINER_COUNT)
|
|
297
|
+
if len(dims) > 1:
|
|
298
|
+
# ND dimensions
|
|
299
|
+
fp_write(ARRAY_START)
|
|
300
|
+
for d in dims:
|
|
301
|
+
__encode_int(fp_write, d, le)
|
|
302
|
+
fp_write(ARRAY_END)
|
|
303
|
+
else:
|
|
304
|
+
__encode_int(fp_write, count, le)
|
|
305
|
+
|
|
306
|
+
# Write payload
|
|
307
|
+
if is_row_major:
|
|
308
|
+
# Interleaved: for each record, write all fields
|
|
309
|
+
for i in range(count):
|
|
310
|
+
for name, marker, field_dtype in schema:
|
|
311
|
+
value = flat_item[name][i]
|
|
312
|
+
if marker == TYPE_BOOL_TRUE:
|
|
313
|
+
fp_write(TYPE_BOOL_TRUE if value else TYPE_BOOL_FALSE)
|
|
314
|
+
else:
|
|
315
|
+
# Write raw bytes
|
|
316
|
+
fp_write(value.tobytes())
|
|
317
|
+
else:
|
|
318
|
+
# Columnar: for each field, write all values
|
|
319
|
+
for name, marker, field_dtype in schema:
|
|
320
|
+
if marker == TYPE_BOOL_TRUE:
|
|
321
|
+
# Boolean: write T/F for each value
|
|
322
|
+
for i in range(count):
|
|
323
|
+
fp_write(TYPE_BOOL_TRUE if flat_item[name][i] else TYPE_BOOL_FALSE)
|
|
324
|
+
else:
|
|
325
|
+
# Numeric: write all values as contiguous bytes
|
|
326
|
+
fp_write(flat_item[name].tobytes())
|
|
327
|
+
|
|
328
|
+
|
|
198
329
|
def __encode_value(
|
|
199
330
|
fp_write,
|
|
200
331
|
item,
|
|
@@ -205,6 +336,7 @@ def __encode_value(
|
|
|
205
336
|
uint8_bytes,
|
|
206
337
|
islittle,
|
|
207
338
|
default,
|
|
339
|
+
soa_format,
|
|
208
340
|
):
|
|
209
341
|
le = islittle
|
|
210
342
|
|
|
@@ -247,6 +379,7 @@ def __encode_value(
|
|
|
247
379
|
uint8_bytes,
|
|
248
380
|
islittle,
|
|
249
381
|
default,
|
|
382
|
+
soa_format,
|
|
250
383
|
)
|
|
251
384
|
|
|
252
385
|
elif isinstance(item, Sequence):
|
|
@@ -260,6 +393,7 @@ def __encode_value(
|
|
|
260
393
|
uint8_bytes,
|
|
261
394
|
islittle,
|
|
262
395
|
default,
|
|
396
|
+
soa_format,
|
|
263
397
|
)
|
|
264
398
|
|
|
265
399
|
elif default is not None:
|
|
@@ -273,10 +407,18 @@ def __encode_value(
|
|
|
273
407
|
uint8_bytes,
|
|
274
408
|
islittle,
|
|
275
409
|
default,
|
|
410
|
+
soa_format,
|
|
276
411
|
)
|
|
277
412
|
|
|
278
413
|
elif type(item).__module__ == "numpy":
|
|
279
|
-
|
|
414
|
+
# Check for SOA-compatible structured array
|
|
415
|
+
if soa_format and __can_encode_as_soa(item):
|
|
416
|
+
__encode_soa(fp_write, item, soa_format, le)
|
|
417
|
+
elif soa_format is None and __can_encode_as_soa(item):
|
|
418
|
+
# Auto-enable column-major SOA for structured arrays
|
|
419
|
+
__encode_soa(fp_write, item, "col", le)
|
|
420
|
+
else:
|
|
421
|
+
__encode_numpy(fp_write, item, uint8_bytes, islittle, default)
|
|
280
422
|
|
|
281
423
|
else:
|
|
282
424
|
raise EncoderException("Cannot encode item of type %s" % type(item))
|
|
@@ -292,6 +434,7 @@ def __encode_array(
|
|
|
292
434
|
uint8_bytes,
|
|
293
435
|
islittle,
|
|
294
436
|
default,
|
|
437
|
+
soa_format,
|
|
295
438
|
):
|
|
296
439
|
# circular reference check
|
|
297
440
|
container_id = id(item)
|
|
@@ -315,6 +458,7 @@ def __encode_array(
|
|
|
315
458
|
uint8_bytes,
|
|
316
459
|
islittle,
|
|
317
460
|
default,
|
|
461
|
+
soa_format,
|
|
318
462
|
)
|
|
319
463
|
|
|
320
464
|
if not container_count:
|
|
@@ -333,6 +477,7 @@ def __encode_object(
|
|
|
333
477
|
uint8_bytes,
|
|
334
478
|
islittle,
|
|
335
479
|
default,
|
|
480
|
+
soa_format,
|
|
336
481
|
):
|
|
337
482
|
le = islittle
|
|
338
483
|
# circular reference check
|
|
@@ -368,6 +513,7 @@ def __encode_object(
|
|
|
368
513
|
uint8_bytes,
|
|
369
514
|
islittle,
|
|
370
515
|
default,
|
|
516
|
+
soa_format,
|
|
371
517
|
)
|
|
372
518
|
|
|
373
519
|
if not container_count:
|
|
@@ -437,6 +583,7 @@ def dump(
|
|
|
437
583
|
uint8_bytes=False,
|
|
438
584
|
islittle=True,
|
|
439
585
|
default=None,
|
|
586
|
+
soa_format=None,
|
|
440
587
|
):
|
|
441
588
|
"""Writes the given object as BJData/UBJSON to the provided file-like object
|
|
442
589
|
|
|
@@ -455,14 +602,18 @@ def dump(
|
|
|
455
602
|
loss of precision.
|
|
456
603
|
uint8_bytes (bool): If set, typed UBJSON arrays (uint8) will be
|
|
457
604
|
converted to a bytes instance instead of being
|
|
458
|
-
treated as an array (for UBJSON & BJData Draft
|
|
605
|
+
treated as an array (for UBJSON & BJData Draft 4).
|
|
459
606
|
Ignored if no_bytes is set.
|
|
460
607
|
islittle (1 or 0): default is 1 for little-endian for all numerics (for
|
|
461
|
-
BJData Draft
|
|
608
|
+
BJData Draft 4), change to 0 to use big-endian
|
|
462
609
|
(for UBJSON for BJData Draft 1)
|
|
463
610
|
default (callable): Called for objects which cannot be serialised.
|
|
464
611
|
Should return a UBJSON-encodable version of the
|
|
465
612
|
object or raise an EncoderException.
|
|
613
|
+
soa_format (str): SOA format for numpy structured arrays:
|
|
614
|
+
'col' or 'column' - column-major (columnar)
|
|
615
|
+
'row' - row-major (interleaved)
|
|
616
|
+
None - auto-enable column-major for structured arrays
|
|
466
617
|
|
|
467
618
|
Raises:
|
|
468
619
|
EncoderException: If an encoding failure occured.
|
|
@@ -511,6 +662,16 @@ def dump(
|
|
|
511
662
|
float32: 1.18e-38 <= abs(value) <= 3.4e38 or value == 0
|
|
512
663
|
float64: 2.23e-308 <= abs(value) < 1.8e308
|
|
513
664
|
For other values Decimal is used.
|
|
665
|
+
|
|
666
|
+
SOA Encoding:
|
|
667
|
+
When soa_format is set and the object is a numpy structured array
|
|
668
|
+
(record array), it will be encoded using the BJData Draft 4 SOA format.
|
|
669
|
+
|
|
670
|
+
Example:
|
|
671
|
+
import numpy as np
|
|
672
|
+
dt = np.dtype([('x', 'u1'), ('y', 'f4'), ('flag', '?')])
|
|
673
|
+
data = np.array([(65, 1.5, True), (66, 2.5, False)], dtype=dt)
|
|
674
|
+
bjd = dumpb(data, soa_format='col')
|
|
514
675
|
"""
|
|
515
676
|
if not callable(fp.write):
|
|
516
677
|
raise TypeError("fp.write not callable")
|
|
@@ -526,6 +687,7 @@ def dump(
|
|
|
526
687
|
uint8_bytes,
|
|
527
688
|
islittle,
|
|
528
689
|
default,
|
|
690
|
+
soa_format,
|
|
529
691
|
)
|
|
530
692
|
|
|
531
693
|
|
|
@@ -537,6 +699,7 @@ def dumpb(
|
|
|
537
699
|
uint8_bytes=False,
|
|
538
700
|
islittle=True,
|
|
539
701
|
default=None,
|
|
702
|
+
soa_format=None,
|
|
540
703
|
):
|
|
541
704
|
"""Returns the given object as BJData/UBJSON in a bytes instance. See dump() for
|
|
542
705
|
available arguments."""
|
|
@@ -550,5 +713,6 @@ def dumpb(
|
|
|
550
713
|
uint8_bytes=uint8_bytes,
|
|
551
714
|
islittle=islittle,
|
|
552
715
|
default=default,
|
|
716
|
+
soa_format=soa_format,
|
|
553
717
|
)
|
|
554
718
|
return fp.getvalue()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bjdata
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Binary JData and UBJSON encoder/decoder
|
|
5
5
|
Home-page: https://github.com/NeuroJSON/pybj
|
|
6
6
|
Author: Qianqian Fang
|
|
@@ -58,16 +58,16 @@ Dynamic: summary
|
|
|
58
58
|
- Copyright: (C) Qianqian Fang (2020-2025) <q.fang at neu.edu>
|
|
59
59
|
- Copyright: (C) Iotic Labs Ltd. (2016-2019) <vilnis.termanis at iotic-labs.com>
|
|
60
60
|
- License: Apache License, Version 2.0
|
|
61
|
-
- Version: 0.
|
|
61
|
+
- Version: 0.6.0
|
|
62
62
|
- URL: https://pypi.org/project/bjdata/
|
|
63
63
|
- Github: https://github.com/NeuroJSON/pybj
|
|
64
|
-
- BJData Spec Version: [
|
|
64
|
+
- BJData Spec Version: [Draft 4](https://neurojson.org/bjdata/draft4)
|
|
65
65
|
- Acknowledgement: This project is supported by US National Institute of Health (NIH) grant U24-NS124027
|
|
66
66
|
|
|
67
67
|

|
|
68
68
|
|
|
69
69
|
This is a Python v3.2+ (and 2.7+) [Binary JData](https://neurojson.org) encoder
|
|
70
|
-
and decoder based on the [Draft-
|
|
70
|
+
and decoder based on the [Draft-4](Binary_JData_Specification.md) specification.
|
|
71
71
|
|
|
72
72
|
## Installing / packaging
|
|
73
73
|
```shell
|
|
@@ -190,7 +190,7 @@ This package was modified based on the py-ubjson package developed by
|
|
|
190
190
|
Project URL: https://github.com/Iotic-Labs/py-ubjson
|
|
191
191
|
|
|
192
192
|
The major changes were focused on supporting the Binary JData Specification
|
|
193
|
-
[Draft
|
|
193
|
+
[Draft 4](https://neurojson.org/bjdata/draft4) -
|
|
194
194
|
an extended Universal Binary JSON (UBJSON) Specification Draft-12 by adding
|
|
195
195
|
the below new features:
|
|
196
196
|
|
|
@@ -200,3 +200,4 @@ the below new features:
|
|
|
200
200
|
* BJData does not convert NaN/Inf/-Inf to `null`
|
|
201
201
|
* BJData uses little-Endian as the default integer/floating-point numbers while UBJSON uses big-Endian
|
|
202
202
|
* BJData only permits non-zero-fixed-length data types (`UiuImlMLhdDCB`) in strongly-typed array/object containers
|
|
203
|
+
* BJData Draft-4 adds new optimized container Stucture-of-Arrays (SoA) for packed objects.
|