Skip to content

Commit addce38

Browse files
committed
Merge pull request #69 from matthew-brett/load-save-benchmarks
NF - add image load save benchmarks
2 parents e8ed3bd + f866793 commit addce38

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

nibabel/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
try:
6464
from numpy.testing import Tester
6565
test = Tester().test
66+
bench = Tester().bench
6667
del Tester
6768
except ImportError:
6869
def test(*args, **kwargs): raise RuntimeError('Need numpy >= 1.2 for tests')

nibabel/benchmarks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Benchmarks for nibabel

nibabel/benchmarks/bench_load_save.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
""" Benchmarks for load and save of image arrays
2+
3+
Run benchmarks with::
4+
5+
>>> import nibabel as nib
6+
>>> nib.bench()
7+
8+
If you have doctests enabled by default in nose (with a noserc file or
9+
environment variable), or you have a numpy version > 1.6.1, this will also run
10+
the doctests, let's hope they pass.
11+
"""
12+
import sys
13+
14+
import numpy as np
15+
16+
from ..py3k import BytesIO
17+
from .. import Nifti1Image
18+
19+
from numpy.testing import measure
20+
21+
def bench_load_save():
22+
rng = np.random.RandomState(20111001)
23+
repeat = 4
24+
img_shape = (128, 128, 64)
25+
arr = rng.normal(size=img_shape)
26+
img = Nifti1Image(arr, np.eye(4))
27+
sio = BytesIO()
28+
img.file_map['image'].fileobj = sio
29+
hdr = img.get_header()
30+
sys.stdout.flush()
31+
print "\nImage load save"
32+
print "----------------"
33+
hdr.set_data_dtype(np.float32)
34+
mtime = measure('img.to_file_map()', repeat)
35+
print '%30s %6.2f' % ('Save float64 to float32', mtime)
36+
mtime = measure('img.from_file_map(img.file_map)', repeat)
37+
print '%30s %6.2f' % ('Load from float32', mtime)
38+
hdr.set_data_dtype(np.int16)
39+
mtime = measure('img.to_file_map()', repeat)
40+
print '%30s %6.2f' % ('Save float64 to int16', mtime)
41+
mtime = measure('img.from_file_map(img.file_map)', repeat)
42+
print '%30s %6.2f' % ('Load from int16', mtime)
43+
arr = np.random.random_integers(low=-1000,high=-1000, size=img_shape)
44+
arr = arr.astype(np.int16)
45+
img = Nifti1Image(arr, np.eye(4))
46+
sio = BytesIO()
47+
img.file_map['image'].fileobj = sio
48+
hdr = img.get_header()
49+
hdr.set_data_dtype(np.float32)
50+
mtime = measure('img.to_file_map()', repeat)
51+
print '%30s %6.2f' % ('Save Int16 to float32', mtime)
52+
sys.stdout.flush()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def main(**extra_args):
8484
'nibabel.nicom.tests',
8585
'nibabel.testing',
8686
'nibabel.tests',
87+
'nibabel.benchmarks',
8788
# install nisext as its own package
8889
'nisext',
8990
'nisext.tests'],

0 commit comments

Comments
 (0)