Skip to content

Commit baf6cdc

Browse files
committed
changes per Yariks comments!
1 parent 672661e commit baf6cdc

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

nibabel/cmdline/diff.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ def are_values_different(*values):
6262
if type(value0) != type(value): # if types are different, then we consider them different
6363
return True
6464
elif isinstance(value0, np.ndarray):
65-
if np.any(value0 != value): # special test for ndarray
66-
return True
67-
else:
68-
return False
65+
return np.any(value0 != value)
6966

7067
elif value0 != value:
7168
return True
@@ -104,7 +101,18 @@ def get_headers_diff(file_headers, names=None):
104101
return difference
105102

106103

107-
def get_data_md5sums(files):
104+
def get_data_diff(files):
105+
"""Get difference between md5 values
106+
107+
Parameters
108+
----------
109+
files: list of actual files
110+
111+
Returns
112+
-------
113+
list
114+
np.array: md5 values of respective files
115+
"""
108116

109117
md5sums = [
110118
hashlib.md5(np.ascontiguousarray(nib.load(f).get_data(), dtype=np.float32)).hexdigest()
@@ -177,18 +185,15 @@ def main(args=None, out=None):
177185

178186
file_headers = [nib.load(f).header for f in files]
179187

180-
if opts: # will almost always have a header field
181-
# signals "all fields"
182-
if opts.header_fields == 'all':
183-
# TODO: header fields might vary across file types, thus prior sensing would be needed
184-
header_fields = file_headers[0].keys()
185-
else:
186-
header_fields = opts.header_fields.split(',')
187-
else:
188+
# signals "all fields"
189+
if opts.header_fields == 'all':
190+
# TODO: header fields might vary across file types, thus prior sensing would be needed
188191
header_fields = file_headers[0].keys()
192+
else:
193+
header_fields = opts.header_fields.split(',')
189194

190195
diff = get_headers_diff(file_headers, header_fields)
191-
data_diff = get_data_md5sums(files)
196+
data_diff = get_data_diff(files)
192197

193198
if data_diff:
194199
diff['DATA(md5)'] = data_diff
@@ -199,3 +204,4 @@ def main(args=None, out=None):
199204

200205
else:
201206
out.write("These files are identical.\n")
207+
raise SystemExit(0)

nibabel/cmdline/tests/test_utils.py

Lines changed: 13 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
14+
from nibabel.cmdline.diff import get_headers_diff, display_diff, main, get_data_diff
1515
from os.path import (join as pjoin)
1616
from nibabel.testing import data_path
1717
from collections import OrderedDict
@@ -110,6 +110,13 @@ def test_display_diff():
110110
assert_equal(display_diff(bogus_names, dict_values), expected_output)
111111

112112

113+
def test_get_data_diff():
114+
# testing for identical files specifically as md5 may vary by computer
115+
test_names = [pjoin(data_path, f)
116+
for f in ('standard.nii.gz', 'standard.nii.gz')]
117+
assert_equal(get_data_diff(test_names), [])
118+
119+
113120
def test_main():
114121
test_names = [pjoin(data_path, f)
115122
for f in ('standard.nii.gz', 'example4d.nii.gz')]
@@ -150,3 +157,8 @@ def test_main():
150157

151158
with assert_raises(SystemExit):
152159
np.testing.assert_equal(main(test_names, StringIO()), expected_difference)
160+
161+
test_names_2 = [pjoin(data_path, f) for f in ('standard.nii.gz', 'standard.nii.gz')]
162+
163+
with assert_raises(SystemExit):
164+
assert_equal(main(test_names_2, StringIO()), "These files are identical.")

nibabel/tests/data/spmT_0001.nii.gz

-1.36 MB
Binary file not shown.

nibabel/tests/data/spmT_0001_2.nii.gz

-1.36 MB
Binary file not shown.

nibabel/tests/test_scripts.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def check_nib_diff_examples():
7575
checked_fields = ["Field", "regular", "dim_info", "dim", "datatype", "bitpix", "pixdim", "slice_end",
7676
"xyzt_units", "cal_max", "descrip", "qform_code", "sform_code", "quatern_b",
7777
"quatern_c", "quatern_d", "qoffset_x", "qoffset_y", "qoffset_z", "srow_x",
78-
"srow_y", "srow_z"]
78+
"srow_y", "srow_z", "DATA(md5)"]
7979
for item in checked_fields:
8080
assert_true(item in stdout)
8181

@@ -84,6 +84,18 @@ def check_nib_diff_examples():
8484
code, stdout, stderr = run_command(['nib-diff'] + fnames2, check_code=False)
8585
assert_equal(stdout, "These files are identical.")
8686

87+
fnames3 = [pjoin(DATA_PATH, f)
88+
for f in ('standard.nii.gz', 'example4d.nii.gz', 'example_nifti2.nii.gz')]
89+
code, stdout, stderr = run_command(['nib-diff'] + fnames3, check_code=False)
90+
for item in checked_fields:
91+
assert_true(item in stdout)
92+
93+
fnames4 = [pjoin(DATA_PATH, f)
94+
for f in ('standard.nii.gz', 'standard.nii.gz', 'standard.nii.gz')]
95+
code, stdout, stderr = run_command(['nib-diff'] + fnames4, check_code=False)
96+
assert_equal(stdout, "These files are identical.")
97+
98+
8799

88100
@script_test
89101
def test_nib_ls():

0 commit comments

Comments
 (0)