forked from nipy/nibabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_load_save.py
68 lines (56 loc) · 2.24 KB
/
bench_load_save.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
""" Benchmarks for load and save of image arrays
Run benchmarks with::
import nibabel as nib
nib.bench()
If you have doctests enabled by default in nose (with a noserc file or
environment variable), and you have a numpy version <= 1.6.1, this will also run
the doctests, let's hope they pass.
Run this benchmark with:
nosetests -s --match '(?:^|[\\b_\\.//-])[Bb]ench' /path/to/bench_load_save.py
"""
from __future__ import division, print_function
import sys
import numpy as np
from ..externals.six import BytesIO
from .. import Nifti1Image
from .butils import print_git_title
from numpy.testing import measure
def bench_load_save():
rng = np.random.RandomState(20111001)
repeat = 10
img_shape = (128, 128, 64, 10)
arr = rng.normal(size=img_shape)
img = Nifti1Image(arr, np.eye(4))
sio = BytesIO()
img.file_map['image'].fileobj = sio
hdr = img.header
sys.stdout.flush()
print()
print_git_title("Image load save")
hdr.set_data_dtype(np.float32)
mtime = measure('sio.truncate(0); img.to_file_map()', repeat)
print('%30s %6.2f' % ('Save float64 to float32', mtime))
mtime = measure('img.from_file_map(img.file_map)', repeat)
print('%30s %6.2f' % ('Load from float32', mtime))
hdr.set_data_dtype(np.int16)
mtime = measure('sio.truncate(0); img.to_file_map()', repeat)
print('%30s %6.2f' % ('Save float64 to int16', mtime))
mtime = measure('img.from_file_map(img.file_map)', repeat)
print('%30s %6.2f' % ('Load from int16', mtime))
# Set a lot of NaNs to check timing
arr[:, :, :20] = np.nan
mtime = measure('sio.truncate(0); img.to_file_map()', repeat)
print('%30s %6.2f' % ('Save float64 to int16, NaNs', mtime))
mtime = measure('img.from_file_map(img.file_map)', repeat)
print('%30s %6.2f' % ('Load from int16, NaNs', mtime))
# Int16 input, float output
arr = np.random.random_integers(low=-1000, high=1000, size=img_shape)
arr = arr.astype(np.int16)
img = Nifti1Image(arr, np.eye(4))
sio = BytesIO()
img.file_map['image'].fileobj = sio
hdr = img.header
hdr.set_data_dtype(np.float32)
mtime = measure('sio.truncate(0); img.to_file_map()', repeat)
print('%30s %6.2f' % ('Save Int16 to float32', mtime))
sys.stdout.flush()