Skip to content

Commit 76ca32f

Browse files
committed
elaborated docstring, modified get_data_diff to allow direct array input, added tests for coverage
1 parent 1e33ea7 commit 76ca32f

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

nibabel/cmdline/diff.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ def get_data_diff(files, max_abs=0, max_rel=0):
145145
146146
Parameters
147147
----------
148+
files: list of (str or ndarray)
149+
If list of strings is provided -- they must be existing file names
148150
max_abs: float, optional
149151
Maximal absolute difference to tolerate.
150152
max_rel: float, optional
@@ -157,7 +159,7 @@ def get_data_diff(files, max_abs=0, max_rel=0):
157159
TODO
158160
"""
159161
# we are doomed to keep them in RAM now
160-
data = [nib.load(f).get_data() for f in files]
162+
data = [f if isinstance(f, np.ndarray) else nib.load(f).get_data() for f in files]
161163
diffs = OrderedDict()
162164
for i, d1 in enumerate(data[:-1]):
163165
# populate empty entries for non-compared

nibabel/cmdline/tests/test_utils.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import nibabel as nib
1212
import numpy as np
1313
from nibabel.cmdline.utils import *
14-
from nibabel.cmdline.diff import get_headers_diff, display_diff, main, get_data_md5_diff
14+
from nibabel.cmdline.diff import get_headers_diff, display_diff, main, get_data_md5_diff, get_data_diff
1515
from os.path import (join as pjoin)
1616
from nibabel.testing import data_path
1717
from collections import OrderedDict
@@ -116,6 +116,36 @@ def test_get_data_diff():
116116
for f in ('standard.nii.gz', 'standard.nii.gz')]
117117
assert_equal(get_data_md5_diff(test_names), [])
118118

119+
# testing the maximum relative and absolute differences' different use cases
120+
test_array = np.arange(16).reshape(4, 4)
121+
test_array_2 = np.arange(1, 17).reshape(4, 4)
122+
test_array_3 = np.arange(2, 18).reshape(4, 4)
123+
test_array_4 = np.arange(100).reshape(10, 10)
124+
test_array_5 = np.arange(64).reshape(8, 8)
125+
126+
# same shape, 2 files
127+
assert_equal(get_data_diff([test_array, test_array_2]),
128+
OrderedDict([('DATA(diff 1:)', [None, OrderedDict([('abs', 1), ('rel', 2.0)])])]))
129+
130+
# same shape, 3 files
131+
assert_equal(get_data_diff([test_array, test_array_2, test_array_3]),
132+
OrderedDict([('DATA(diff 1:)', [None, OrderedDict([('abs', 1), ('rel', 2.0)]),
133+
OrderedDict([('abs', 2), ('rel', 2.0)])]),
134+
('DATA(diff 2:)', [None, None,
135+
OrderedDict([('abs', 1), ('rel', 0.66666666666666663)])])]))
136+
137+
# same shape, 2 files, modified maximum abs/rel
138+
assert_equal(get_data_diff([test_array, test_array_2], max_abs=2, max_rel=2), OrderedDict())
139+
140+
# different shape, 2 files
141+
assert_equal(get_data_diff([test_array_2, test_array_4]),
142+
OrderedDict([('DATA(diff 1:)', [None, {'CMP': 'incompat'}])]))
143+
144+
# different shape, 3 files
145+
assert_equal(get_data_diff([test_array_4, test_array_5, test_array_2]),
146+
OrderedDict([('DATA(diff 1:)', [None, {'CMP': 'incompat'}, {'CMP': 'incompat'}]),
147+
('DATA(diff 2:)', [None, None, {'CMP': 'incompat'}])]))
148+
119149

120150
def test_main():
121151
test_names = [pjoin(data_path, f)

0 commit comments

Comments
 (0)