@@ -13,7 +13,7 @@ Output a summary table for neuroimaging files (resolution, dimensionality, etc.)
13
13
from __future__ import division , print_function , absolute_import
14
14
15
15
__author__ = 'Yaroslav Halchenko'
16
- __copyright__ = 'Copyright (c) 2011-2015 Yaroslav Halchenko ' \
16
+ __copyright__ = 'Copyright (c) 2011-2016 Yaroslav Halchenko ' \
17
17
'and NiBabel contributors'
18
18
__license__ = 'MIT'
19
19
@@ -22,6 +22,7 @@ import sys
22
22
from math import ceil
23
23
from optparse import OptionParser , Option
24
24
from io import StringIO
25
+ from collections import Counter
25
26
26
27
import numpy as np
27
28
@@ -149,6 +150,10 @@ def get_opt_parser():
149
150
action = "store_true" , dest = 'stats' , default = False ,
150
151
help = "Output basic data statistics" ),
151
152
153
+ Option ("-c" , "--counts" ,
154
+ action = "store_true" , dest = 'counts' , default = False ,
155
+ help = "Output counts - number of entries for each numeric value (useful for int ROI maps)" ),
156
+
152
157
Option ("-z" , "--zeros" ,
153
158
action = "store_true" , dest = 'stats_zeros' , default = False ,
154
159
help = "Include zeros into output basic data statistics (--stats)" ),
@@ -214,18 +219,24 @@ def proc_file(f, opts):
214
219
else :
215
220
row += ['error' ]
216
221
217
- if opts .stats :
222
+ if opts .stats or opts . counts :
218
223
# We are doomed to load data
219
224
try :
220
225
d = vol .get_data ()
221
226
if not opts .stats_zeros :
222
227
d = d [np .nonzero (d )]
223
- # just # of elements
224
- row += ["[%d] " % np .prod (d .shape )]
225
- # stats
226
- row += [len (d ) and '%.2g:%.2g' % (np .min (d ), np .max (d )) or '-' ]
228
+ if opts .stats :
229
+ # just # of elements
230
+ row += ["[%d]" % np .prod (d .shape )]
231
+ # stats
232
+ row += [len (d ) and '[%.2g, %.2g]' % (np .min (d ), np .max (d )) or '-' ]
233
+ if opts .counts :
234
+ counter = Counter ()
235
+ counter .update (d .flatten ())
236
+ # go through each entry and report only non-0 ones
237
+ row += [" " .join ("%g:%d" % (i , counter [i ]) for i in sorted (counter ))]
227
238
except Exception as e :
228
- verbose (2 , "Failed to obtain stats -- %s" % str (e ))
239
+ verbose (2 , "Failed to obtain stats/counts -- %s" % str (e ))
229
240
row += ['error' ]
230
241
return row
231
242
0 commit comments