Repository navigation
Expand file tree
/
Copy patharrays.py
More file actions
64 lines (52 loc) · 3.11 KB
/
Copy patharrays.py
File metadata and controls
64 lines (52 loc) · 3.11 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
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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.
"""numpy arrays with jdai: any element type and memory order, compression with byte shuffling
and N-D tiles, reading a slice or a block without decompressing the rest, writing a block back
in place, and zero-copy views of an uncompressed file."""
import os
import sys
import tempfile
import numpy as np
import jdai
ok = True
x, y, z = np.meshgrid(np.arange(128), np.arange(128), np.arange(96), indexing="ij")
volume = (np.sin(0.1 * x) * np.cos(0.1 * y) + 0.01 * z).astype(np.float32) # 6 MiB
doc = {
"name": "phantom",
"affine": np.array([[1, 0, 0, -90], [0, 1, 0, -126], [0, 0, 1, -72], [0, 0, 0, 1]], dtype=np.float64),
"labels": np.arange(4, dtype=np.uint8),
"mask": volume > 0.5, # logical
"fortran": np.asfortranarray(np.arange(12, dtype=np.int16).reshape(3, 4)),
"volume": volume,
}
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "phantom.pmat")
# the recommended settings for a small file that is fast to read: zstd, byte shuffling,
# N-D tiles of 256 KiB to 1 MiB for arrays of 2 MiB or more
jdai.save(doc, path, compression="zstd", shuffle=True, chunks="auto")
print("file: %.2f MB for %.2f MB of arrays" % (os.path.getsize(path) / 1e6, sum(a.nbytes for a in doc.values() if isinstance(a, np.ndarray)) / 1e6))
back = jdai.load(path)
for k, a in doc.items():
if isinstance(a, np.ndarray):
ok = ok and back[k].dtype == a.dtype and back[k].shape == a.shape and np.array_equal(back[k], a)
print("every array back with its type and shape:", ok, "| the Fortran-ordered one is F-contiguous:",
back["fortran"].flags.f_contiguous)
# a slice, a line and a block: only the tiles they meet are decompressed
s = jdai.load_region(path, "volume", (64,))
line = jdai.load_region(path, "volume", (10, 20))
block = jdai.load_region(path, "volume", (slice(32, 64), slice(32, 64), slice(16, 48)))
ok = ok and np.array_equal(s, volume[64]) and np.array_equal(line, volume[10, 20]) and np.array_equal(block, volume[32:64, 32:64, 16:48])
print("slice", s.shape, "line", line.shape, "block", block.shape)
# write a block back in place: only its tiles are compressed and written
patch = np.full((32, 32, 32), 7, dtype=np.float32)
jdai.update_region(path, "volume", (slice(0, 32), slice(0, 32), slice(0, 32)), patch)
ok = ok and np.array_equal(jdai.load_region(path, "volume", (slice(0, 32), slice(0, 32), slice(0, 32))), patch)
print("block written back in place:", ok)
# uncompressed: arrays are zero-copy views of the mapped file, read only when used
raw = os.path.join(tmp, "raw.pmat")
jdai.save({"volume": volume}, raw)
v = jdai.load(raw, view=True)["volume"]
print("view of the file:", not v.flags.owndata, "| sum %.1f" % float(v.sum()))
ok = ok and np.array_equal(v, volume)
del v # a view keeps the file mapped: release it before removing the file
sys.exit(0 if ok else 1)