Repository navigation
Expand file tree
/
Copy pathtables.py
More file actions
51 lines (41 loc) · 2.4 KB
/
Copy pathtables.py
File metadata and controls
51 lines (41 loc) · 2.4 KB
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
# Copyright (c) 2026 Qianqian Fang. All rights reserved.
# Dual-licensed under BSD-3-Clause or GPL-3.0-or-later; see LICENSE_BSD.txt and LICENSE_GPLv3.txt.
"""Records and tables with jdai: a list of dicts with the same keys is stored as a structure of
arrays (one typed column per key in BJData), repeated strings as an enumeration, and read back as
records or as columns; other Python values: bytes, complex arrays, Decimal and BJData extensions."""
import decimal
import os
import sys
import tempfile
import numpy as np
import jdai
ok = True
regions = ["frontal", "parietal", "temporal", "occipital"]
electrodes = [{"name": "E%d" % (k + 1), "x": 0.1 * k, "y": 0.2 * k, "z": 0.3 * k, "channel": k,
"region": regions[k % 4], "good": k % 7 != 0} for k in range(256)]
data = jdai.dumps({"electrodes": electrodes}) # a structure of arrays in BJData
text = jdai.dumps({"electrodes": electrodes}, format="json")
print("256 records: BJData %d bytes, JSON %d bytes" % (len(data), len(text)))
records = jdai.loads(data)["electrodes"] # a list of dicts again
ok = ok and records == electrodes
columns = jdai.loads(data, soa="columns")["electrodes"] # or a dict of numpy columns
print("as records:", records[3], "\nas columns:", {k: (v.dtype, v.shape) if isinstance(v, np.ndarray) else type(v).__name__
for k, v in columns.items()})
# repeated strings are stored once each, with integer codes (a JData enumeration)
labels = ["left", "right"] * 50000
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "labels.pmat")
jdai.save({"labels": labels}, path, compression="zstd")
print("100000 labels in %d bytes" % os.path.getsize(path))
ok = ok and jdai.load(path)["labels"] == labels
# other values Python has
other = {
"raw": b"\x00\x01\x02binary", # BJData byte array
"phase": np.exp(1j * np.linspace(0, np.pi, 8)), # complex array
"exact": decimal.Decimal("3.14159265358979323846264338327950288"), # high-precision number
"ext": jdai.Extension(10, b"\x01\x02\x03\x04"), # BJData extension type
}
back = jdai.loads(jdai.dumps(other))
ok = ok and back["raw"] == other["raw"] and np.allclose(back["phase"], other["phase"]) and back["exact"] == other["exact"] and back["ext"] == other["ext"]
print("bytes, complex, Decimal and extension values round-trip:", ok)
sys.exit(0 if ok else 1)