Skip to content

Commit 4b1b960

Browse files
committed
RF: compare data types using .dtype.type to avoid effect of endianness
Rationale is that in a typical case user is interested to check that file contains correct data. Comparison with effect of endianness would probably be more of interest to developers. May be someone later would add a flag to nib-diff?
1 parent df7a80d commit 4b1b960

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

nibabel/cmdline/diff.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
1010
"""
1111
Quick summary of the differences among a set of neuroimaging files
12+
13+
Notes:
14+
- difference in data types for header fields will be detected, but
15+
endianness difference will not be detected. It is done so to compare files
16+
with native endianness used in data files.
1217
"""
1318
from __future__ import division, print_function, absolute_import
1419

@@ -99,7 +104,8 @@ def are_values_different(*values):
99104
if type(value0) != type(value): # if types are different, then we consider them different
100105
return True
101106
elif isinstance(value0, np.ndarray):
102-
if value0.dtype != value.dtype or \
107+
# use .dtype.type to provide endianness agnostic comparison
108+
if value0.dtype.type != value.dtype.type or \
103109
value0.shape != value.shape:
104110
return True
105111
# there might be nans and they need special treatment

nibabel/tests/test_diff.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,9 @@ def test_diff_values_array():
7272
# and some inf should not be a problem
7373
assert not are_values_different(array([0, inf]), array([0, inf]))
7474
assert are_values_different(array([0, inf]), array([inf, 0]))
75+
76+
# we will allow for types to be of different endianness but the
77+
# same in "instnatiation" type and value
78+
assert not are_values_different(np.array(1, dtype='<i4'), np.array(1, dtype='>i4'))
79+
# but do report difference if instantiation type is different:
80+
assert are_values_different(np.array(1, dtype='<i4'), np.array(1, dtype='<i2'))

0 commit comments

Comments
 (0)