14
14
15
15
import sys
16
16
from optparse import OptionParser , Option
17
- from six import binary_type
18
17
19
18
import numpy as np
20
19
21
- import json_tricks
22
- import yaml
23
-
24
20
import nibabel as nib
25
21
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
32
24
33
25
34
26
def get_opt_parser ():
@@ -45,18 +37,6 @@ def get_opt_parser():
45
37
Option ("-H" , "--header-fields" ,
46
38
dest = "header_fields" , default = 'all' ,
47
39
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" ),
60
40
])
61
41
62
42
return p
@@ -68,6 +48,8 @@ def diff_values(compare1, compare2):
68
48
return True
69
49
elif type (compare1 ) != type (compare2 ):
70
50
return True
51
+ elif compare1 != compare2 :
52
+ return True
71
53
else :
72
54
return False
73
55
@@ -80,19 +62,19 @@ def diff_header_fields(key, inputs):
80
62
for i in inputs : # stores each file's respective header files
81
63
field_value = i [key ]
82
64
83
- try :
65
+ try : # filter numpy arrays
84
66
if np .all (np .isnan (field_value )):
85
67
continue
86
68
except TypeError :
87
69
pass
88
70
89
- for x in inputs [1 :]:
71
+ for x in inputs [1 :]: # compare different values, print all as soon as diff is found
90
72
data_diff = diff_values (str (x [key ].dtype ), str (field_value .dtype ))
91
73
92
74
if data_diff :
93
75
break
94
76
95
- if data_diff :
77
+ if data_diff : # prints data types if they're different and not if they're not
96
78
if field_value .ndim < 1 :
97
79
keyed_inputs .append ("{}@{}" .format (field_value , field_value .dtype ))
98
80
elif field_value .ndim == 1 :
@@ -134,6 +116,15 @@ def get_headers_diff(files, opts):
134
116
return output
135
117
136
118
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
+
137
128
def main ():
138
129
"""NO DAYS OFF"""
139
130
@@ -149,25 +140,27 @@ def main():
149
140
nib .imageglobals .logger .level = 50
150
141
151
142
diff = get_headers_diff (files , opts )
143
+ diff2 = get_data_diff (files )
152
144
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 = "" )
155
146
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 ()
159
150
160
- for x in diff :
161
- print ("{:<11}" .format (x ), end = "" )
151
+ for x in diff :
152
+ print ("{:<11}" .format (x ), end = "" )
162
153
163
- for e in diff [x ]:
164
- print ("{:<45}" .format (e ), end = "" )
154
+ for e in diff [x ]:
155
+ print ("{:<45}" .format (e ), end = "" )
165
156
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!" )
168
165
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 )
0 commit comments