Skip to content

Commit cef1f2e

Browse files
committed
[bench] add nifti file benchmark
1 parent 5c7e71a commit cef1f2e

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

benchmark/benchcodecs.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
Speed benchmark for saving/loading numpy arrays using various compression codecs
3+
"""
4+
import jdata as jd
5+
import numpy as np
6+
import time
7+
import os
8+
9+
print("jdata version:" + jd.__version__)
10+
11+
codecs = [
12+
"npy",
13+
"npz",
14+
"bjd",
15+
"zlib",
16+
"lzma",
17+
"lz4",
18+
"blosc2blosclz",
19+
"blosc2lz4",
20+
"blosc2lz4hc",
21+
"blosc2zlib",
22+
"blosc2zstd",
23+
]
24+
nthread = 8
25+
26+
27+
def benchmark(codec, x):
28+
t0 = time.time()
29+
ext = suffix
30+
if codec == "npy":
31+
ext = "." + codec
32+
np.save("matrix_" + codec + ext, x)
33+
elif codec == "npz":
34+
ext = "." + codec
35+
np.savez_compressed("matrix_" + codec + ext, x)
36+
elif codec == "bjd":
37+
ext = "." + codec
38+
jd.save(x, "matrix_" + codec + ext, {"encode": False})
39+
else:
40+
jd.save(x, "matrix_" + codec + ext, {"compression": codec, "nthread": nthread})
41+
dt = time.time() - t0 # saving time
42+
res = {"codec": codec, "save": dt}
43+
if codec == "npy":
44+
y = np.load("matrix_" + codec + ext)
45+
elif codec == "npz":
46+
y = np.load("matrix_" + codec + ext)["arr_0"]
47+
else:
48+
y = jd.load("matrix_" + codec + ext, {"nthread": nthread}) # loading
49+
res["sum"] = y.sum()
50+
res["load"] = time.time() - t0 - dt # loading time
51+
res["size"] = os.path.getsize("matrix_" + codec + ext)
52+
print(res)
53+
return res
54+
55+
56+
## a highly compressible matrix
57+
x = np.eye(10000)
58+
59+
## a less compressible random matrix
60+
# np.random.seed(0)
61+
# x = np.random.rand(2000,2000)
62+
63+
print("\n- Testing binary JSON (BJData) files (.jdb) ...")
64+
65+
suffix = ".jdb"
66+
res = list(map(benchmark, codecs, [x] * len(codecs)))
67+
# print(np.array(res))
68+
69+
print("\n- Testing text-based JSON files (.jdt) ...")
70+
71+
suffix = ".jdt"
72+
res = list(map(benchmark, codecs, [x] * len(codecs)))
73+
# print(np.array(res))

benchmark/benchnifti.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import time
2+
import os
3+
import glob
4+
import urllib.request
5+
import zipfile
6+
import tempfile
7+
import shutil
8+
9+
import nibabel as nib
10+
import numpy as np
11+
import jdata as jd
12+
13+
tempdir = tempfile.mkdtemp()
14+
15+
url = "https://github.com/neurolabusc/niivue-images/archive/refs/heads/main.zip"
16+
fname = os.path.join(tempdir, "niivue-images.zip")
17+
urllib.request.urlretrieve(url, fname)
18+
19+
with zipfile.ZipFile(fname, "r") as zip_ref:
20+
zip_ref.extractall(tempdir)
21+
22+
niifiles = glob.glob(os.path.join(tempdir, "niivue-images-main/", "*.nii.gz"))
23+
24+
for ff in niifiles:
25+
# benchmark loading time from nib.load()
26+
t0 = time.time()
27+
img = nib.load(ff)
28+
data = np.asarray(img.dataobj)
29+
try:
30+
s1 = np.sum(data).item()
31+
except:
32+
s1 = -1
33+
dt1 = time.time() - t0
34+
35+
# benchmark loading time from jd.loadnifti()
36+
t1 = time.time()
37+
nii = jd.loadnifti(ff)
38+
s2 = np.sum(nii["NIFTIData"]).item()
39+
dt2 = time.time() - t1
40+
jd.show(
41+
{
42+
"file": ff,
43+
"nib": [list(data.shape), data.dtype.str, s1],
44+
"jd": [list(nii["NIFTIData"].shape), nii["NIFTIData"].dtype.str, s2],
45+
"nibtime": dt1,
46+
"jdtime": dt2,
47+
"speedup": dt1 / dt2,
48+
}
49+
)
50+
51+
try:
52+
shutil.rmtree(tempdir)
53+
except OSError as e:
54+
print(f"unable to delete the temporary folder: {e}")

0 commit comments

Comments
 (0)