Skip to content

Commit 1e33ea7

Browse files
committed
RF: anticipated files of different shapes, fixed table display, corrected tests
1 parent 9b123f6 commit 1e33ea7

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

nibabel/cmdline/diff.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -165,42 +165,45 @@ def get_data_diff(files, max_abs=0, max_rel=0):
165165

166166
for j, d2 in enumerate(data[i + 1:], i + 1):
167167

168-
abs_diff = np.abs(d1 - d2)
169-
mean_abs = (np.abs(d1) + np.abs(d2)) * 0.5
170-
candidates = np.logical_or(mean_abs != 0, abs_diff != 0)
171-
172-
if max_abs:
173-
candidates[abs_diff <= max_abs] = False
174-
175-
max_abs_diff = np.max(abs_diff)
176-
if np.any(candidates):
177-
rel_diff = abs_diff[candidates] / mean_abs[candidates]
178-
if max_rel:
179-
sub_thr = rel_diff <= max_rel
180-
# Since we operated on sub-selected values already, we need
181-
# to plug them back in
182-
candidates[
183-
tuple((indexes[sub_thr] for indexes in np.where(candidates)))
184-
] = False
185-
max_rel_diff = np.max(rel_diff)
186-
else:
187-
max_rel_diff = 0
188-
189-
if np.any(candidates):
168+
if d1.shape == d2.shape:
169+
abs_diff = np.abs(d1 - d2)
170+
mean_abs = (np.abs(d1) + np.abs(d2)) * 0.5
171+
candidates = np.logical_or(mean_abs != 0, abs_diff != 0)
172+
173+
if max_abs:
174+
candidates[abs_diff <= max_abs] = False
175+
176+
max_abs_diff = np.max(abs_diff)
177+
if np.any(candidates):
178+
rel_diff = abs_diff[candidates] / mean_abs[candidates]
179+
if max_rel:
180+
sub_thr = rel_diff <= max_rel
181+
# Since we operated on sub-selected values already, we need
182+
# to plug them back in
183+
candidates[
184+
tuple((indexes[sub_thr] for indexes in np.where(candidates)))
185+
] = False
186+
max_rel_diff = np.max(rel_diff)
187+
else:
188+
max_rel_diff = 0
189+
190+
if np.any(candidates):
191+
192+
diff_rec = OrderedDict() # so that abs goes before relative
193+
194+
diff_rec['abs'] = max_abs_diff
195+
diff_rec['rel'] = max_rel_diff
196+
diffs1.append(diff_rec)
197+
else:
198+
diffs1.append(None)
190199

191-
diff_rec = OrderedDict() # so that abs goes before relative
192-
193-
diff_rec['abs'] = max_abs_diff
194-
diff_rec['rel'] = max_rel_diff
195-
diffs1.append(diff_rec)
196200
else:
197-
diffs1.append(None)
201+
diffs1.append({'CMP': "incompat"})
198202

199203
if any(diffs1):
200204

201205
diffs['DATA(diff %d:)' % (i + 1)] = diffs1
202206

203-
204207
return diffs
205208

206209

@@ -219,13 +222,14 @@ def display_diff(files, diff):
219222
"""
220223
output = ""
221224
field_width = "{:<15}"
225+
filename_width = "{:<53}"
222226
value_width = "{:<55}"
223227

224228
output += "These files are different.\n"
225229
output += field_width.format('Field/File')
226230

227231
for i, f in enumerate(files, 1):
228-
output += "%d:%s" % (i, value_width.format(os.path.basename(f)))
232+
output += "%d:%s" % (i, filename_width.format(os.path.basename(f)))
229233

230234
output += "\n"
231235

nibabel/cmdline/tests/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ def test_display_diff():
9696
("bitpix", [np.array(8).astype(dtype="uint8"), np.array(16).astype(dtype="uint8")])
9797
])
9898

99-
expected_output = "These files are different.\n" + "Field/File hellokitty.nii.gz" \
100-
" " \
101-
"privettovarish.nii.gz \n" \
99+
expected_output = "These files are different.\n" + "Field/File 1:hellokitty.nii.gz" \
100+
" " \
101+
"2:privettovarish.nii.gz \n" \
102102
"datatype " \
103103
"2 " \
104104
"4 \n" \

nibabel/tests/test_scripts.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ def check_nib_diff_examples():
7575
checked_fields = ["Field/File", "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", "DATA(md5)"]
78+
"srow_y", "srow_z", "DATA(md5)", "DATA(diff 1:)"]
7979
for item in checked_fields:
80+
if item not in stdout:
81+
print(item)
82+
print(stdout)
8083
assert_true(item in stdout)
8184

8285
fnames2 = [pjoin(DATA_PATH, f)

0 commit comments

Comments
 (0)