Skip to content

Commit 06e8dd7

Browse files
committed
data comparison function added
1 parent 92e4ed0 commit 06e8dd7

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

nibabel/cmdline/diff.py

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,13 @@
1414

1515
import sys
1616
from optparse import OptionParser, Option
17-
from six import binary_type
1817

1918
import numpy as np
2019

21-
import json_tricks
22-
import yaml
23-
2420
import nibabel as nib
2521
import nibabel.cmdline.utils
26-
from nibabel.cmdline.utils import _err, verbose, table2string, ap, safe_get
27-
import fileinput
28-
29-
__author__ = 'Yaroslav Halchenko & Christopher Cheng'
30-
__copyright__ = 'Copyright (c) 2017 NiBabel contributors'
31-
__license__ = 'MIT'
22+
import itertools
23+
import hashlib
3224

3325

3426
def get_opt_parser():
@@ -45,18 +37,6 @@ def get_opt_parser():
4537
Option("-H", "--header-fields",
4638
dest="header_fields", default='all',
4739
help="Header fields (comma separated) to be printed as well (if present)"),
48-
49-
Option("-t", "--text",
50-
action='store_true',
51-
help="Print output in a very nice-looking way"),
52-
53-
Option("-j", "--json",
54-
action='store_true',
55-
help="Print output in a json way"),
56-
57-
Option("-y", "--yaml",
58-
action='store_true',
59-
help="Print output in a yaml way"),
6040
])
6141

6242
return p
@@ -68,6 +48,8 @@ def diff_values(compare1, compare2):
6848
return True
6949
elif type(compare1) != type(compare2):
7050
return True
51+
elif compare1 != compare2:
52+
return True
7153
else:
7254
return False
7355

@@ -80,19 +62,19 @@ def diff_header_fields(key, inputs):
8062
for i in inputs: # stores each file's respective header files
8163
field_value = i[key]
8264

83-
try:
65+
try: # filter numpy arrays
8466
if np.all(np.isnan(field_value)):
8567
continue
8668
except TypeError:
8769
pass
8870

89-
for x in inputs[1:]:
71+
for x in inputs[1:]: # compare different values, print all as soon as diff is found
9072
data_diff = diff_values(str(x[key].dtype), str(field_value.dtype))
9173

9274
if data_diff:
9375
break
9476

95-
if data_diff:
77+
if data_diff: # prints data types if they're different and not if they're not
9678
if field_value.ndim < 1:
9779
keyed_inputs.append("{}@{}".format(field_value, field_value.dtype))
9880
elif field_value.ndim == 1:
@@ -134,6 +116,15 @@ def get_headers_diff(files, opts):
134116
return output
135117

136118

119+
def get_data_diff(files):
120+
121+
data_list = [nib.load(f).get_data() for f in files]
122+
123+
for a, b in itertools.combinations(data_list, 2):
124+
return diff_values(hashlib.md5(repr(a).encode('utf-8')).hexdigest(), hashlib.md5(
125+
repr(b).encode('utf-8')).hexdigest())
126+
127+
137128
def main():
138129
"""NO DAYS OFF"""
139130

@@ -149,25 +140,27 @@ def main():
149140
nib.imageglobals.logger.level = 50
150141

151142
diff = get_headers_diff(files, opts)
143+
diff2 = get_data_diff(files)
152144

153-
if opts.text: # using string formatting to print a table of the results
154-
print("{:<11}".format('Field'), end="")
145+
print("{:<11}".format('Field'), end="")
155146

156-
for f in files:
157-
print("{:<45}".format(f), end="")
158-
print()
147+
for f in files:
148+
print("{:<45}".format(f), end="")
149+
print()
159150

160-
for x in diff:
161-
print("{:<11}".format(x), end="")
151+
for x in diff:
152+
print("{:<11}".format(x), end="")
162153

163-
for e in diff[x]:
164-
print("{:<45}".format(e), end="")
154+
for e in diff[x]:
155+
print("{:<45}".format(e), end="")
165156

166-
print()
167-
raise SystemExit(1)
157+
print()
158+
159+
print("DATA: ", end="")
160+
161+
if diff2:
162+
print("These files are different.")
163+
else:
164+
print("These files are identical!")
168165

169-
# elif opts.json:
170-
# print(json_tricks.dumps(diff, conv_str_byte=True))
171-
#
172-
# elif opts.yaml:
173-
# print(yaml.dump(diff))
166+
raise SystemExit(1)

nibabel/cmdline/ls.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
import nibabel.cmdline.utils
2222
from nibabel.cmdline.utils import _err, verbose, table2string, ap, safe_get
2323

24-
__author__ = 'Yaroslav Halchenko'
25-
__copyright__ = 'Copyright (c) 2011-2016 Yaroslav Halchenko ' \
26-
'and NiBabel contributors'
27-
__license__ = 'MIT'
28-
2924

3025
MAX_UNIQUE = 1000 # maximal number of unique values to report for --counts
3126

0 commit comments

Comments
 (0)