Skip to content

Commit 5db2654

Browse files
committed
fixed changes as yarik requested
1 parent 0cf2a8c commit 5db2654

File tree

3 files changed

+63
-48
lines changed

3 files changed

+63
-48
lines changed

nibabel/cmdline/diff.py

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ def diff_values(compare1, compare2):
4848
return True
4949
elif type(compare1) != type(compare2):
5050
return True
51-
elif compare1 != compare2:
52-
return True
5351
else:
54-
return False
52+
return compare1 != compare2
5553

5654

5755
def diff_header_fields(key, inputs):
@@ -103,10 +101,19 @@ def diff_header_fields(key, inputs):
103101

104102

105103
def get_headers_diff(files, opts):
104+
"""
105+
Getting the difference of headers.
106+
Returns a dictionary that is later processed.
107+
108+
Parameters
109+
----------
110+
files: list of files
111+
opts: any options included from the command line
112+
"""
106113

107114
header_list = [nib.load(f).header for f in files]
108115

109-
if opts.header_fields:
116+
if opts.header_fields: # will almost always have a header field
110117
# signals "all fields"
111118
if opts.header_fields == 'all':
112119
# TODO: header fields might vary across file types, thus prior sensing would be needed
@@ -123,24 +130,27 @@ def get_headers_diff(files, opts):
123130
output[f] = val
124131

125132
return output
133+
else:
134+
return {}
126135

127136

128137
def get_data_diff(files):
129138

130139
data_list = [nib.load(f).get_data() for f in files]
131-
temp_bool = False
140+
output = []
132141

133142
for a, b in itertools.combinations(data_list, 2):
134-
if diff_values(hashlib.md5(repr(a).encode('utf-8')).hexdigest(), hashlib.md5(
135-
repr(b).encode('utf-8')).hexdigest()):
136-
temp_bool = True
137-
break
143+
if diff_values(hash(str(a)), hash(str(b))):
144+
if hash(str(a)) not in output:
145+
output.append(str(hash(str(a))))
146+
if hash(str(b)) not in output:
147+
output.append(str(hash(str(b))))
138148

139-
return temp_bool
149+
return output
140150

141151

142152
def main():
143-
"""NO DAYS OFF"""
153+
"""Getting the show on the road"""
144154

145155
parser = get_opt_parser()
146156
(opts, files) = parser.parse_args()
@@ -153,38 +163,42 @@ def main():
153163
# suppress nibabel format-compliance warnings
154164
nib.imageglobals.logger.level = 50
155165

156-
diff = get_headers_diff(files, opts)
157-
diff2 = get_data_diff(files)
166+
header_diff = get_headers_diff(files, opts)
167+
data_diff = get_data_diff(files)
158168

159-
print("{:<11}".format('Field'), end="")
169+
if len(data_diff) != 0 and len(header_diff) != 0:
170+
print("{:<11}".format('Field'), end="")
160171

161-
for f in files:
162-
output = ""
163-
i = 0
164-
while i < len(f):
165-
if f[i] == "/":
166-
output = ""
167-
else:
168-
output += f[i]
169-
i += 1
172+
for f in files:
173+
output = ""
174+
i = 0
175+
while i < len(f):
176+
if f[i] == "/" or f[i] == "\\":
177+
output = ""
178+
else:
179+
output += f[i]
180+
i += 1
170181

171-
print("{:<45}".format(output), end="")
182+
print("{:<45}".format(output), end="")
172183

173-
print()
184+
print()
174185

175-
for x in diff:
176-
print("{:<11}".format(x), end="")
186+
for x in header_diff:
187+
print("{:<11}".format(x), end="")
177188

178-
for e in diff[x]:
179-
print("{:<45}".format(e), end="")
189+
for e in header_diff[x]:
190+
print("{:<45}".format(e), end="")
180191

181-
print()
192+
print()
182193

183194
print("DATA: ", end="")
184195

185-
if diff2:
196+
if len(data_diff) != 0:
186197
print("These files are different.")
198+
print("{:<11}".format("Checksum"), end="")
199+
for i in data_diff:
200+
print("{:45}".format(i[0:8]), end="")
201+
print()
202+
raise SystemExit(1)
187203
else:
188204
print("These files are identical!")
189-
190-
raise SystemExit(1)

nibabel/cmdline/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Helper utilities to be used in cmdline applications
1111
"""
1212

13+
1314
# global verbosity switch
1415
import re
1516
from io import StringIO

nibabel/tests/test_diff.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ def test_diff_values_int(data):
2424
y = data.draw(st.integers(min_value = x + 1), label='x+1')
2525
z = data.draw(st.integers(max_value = x - 1), label='x-1')
2626

27-
assert diff_values(x, x) is False
28-
assert diff_values(x, y) == True
29-
assert diff_values(x, z) == True
30-
assert diff_values(y, z) == True
27+
assert not diff_values(x, x)
28+
assert diff_values(x, y)
29+
assert diff_values(x, z)
30+
assert diff_values(y, z)
3131

3232

3333
@given(st.data())
@@ -36,10 +36,10 @@ def test_diff_values_float(data):
3636
y = data.draw(st.floats(min_value = 1e8), label='y')
3737
z = data.draw(st.floats(max_value = -1e8), label='z')
3838

39-
assert diff_values(x, x) is False
40-
assert diff_values(x, y) == True
41-
assert diff_values(x, z) == True
42-
assert diff_values(y, z) == True
39+
assert not diff_values(x, x)
40+
assert diff_values(x, y)
41+
assert diff_values(x, z)
42+
assert diff_values(y, z)
4343

4444

4545
@given(st.data())
@@ -48,10 +48,10 @@ def test_diff_values_mixed(data):
4848
type_int = data.draw(st.integers(), label='int')
4949
type_none = data.draw(st.none(), label='none')
5050

51-
assert diff_values(type_float, type_int) == True
52-
assert diff_values(type_float, type_none) == True
53-
assert diff_values(type_int, type_none) == True
54-
assert diff_values(type_none, type_none) is False
51+
assert diff_values(type_float, type_int)
52+
assert diff_values(type_float, type_none)
53+
assert diff_values(type_int, type_none)
54+
assert not diff_values(type_none, type_none)
5555

5656

5757
@given(st.data())
@@ -62,6 +62,6 @@ def test_diff_values_array(data):
6262
d = data.draw(st.lists(elements=st.floats(max_value=-1e8), min_size=1))
6363
# TODO: Figure out a way to include 0 in lists (arrays)
6464

65-
assert diff_values(a, b) == True
66-
assert diff_values(c, d) == True
67-
assert diff_values(a, a) == False
65+
assert diff_values(a, b)
66+
assert diff_values(c, d)
67+
assert not diff_values(a, a)

0 commit comments

Comments
 (0)