@@ -13,7 +13,7 @@ Output a summary table for neuroimaging files (resolution, dimensionality, etc.)
1313from __future__ import division , print_function , absolute_import
1414
1515__author__ = 'Yaroslav Halchenko'
16- __copyright__ = 'Copyright (c) 2011-2015 Yaroslav Halchenko ' \
16+ __copyright__ = 'Copyright (c) 2011-2016 Yaroslav Halchenko ' \
1717 'and NiBabel contributors'
1818__license__ = 'MIT'
1919
@@ -22,6 +22,7 @@ import sys
2222from math import ceil
2323from optparse import OptionParser , Option
2424from io import StringIO
25+ from collections import Counter
2526
2627import numpy as np
2728
@@ -149,6 +150,10 @@ def get_opt_parser():
149150 action = "store_true" , dest = 'stats' , default = False ,
150151 help = "Output basic data statistics" ),
151152
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+
152157 Option ("-z" , "--zeros" ,
153158 action = "store_true" , dest = 'stats_zeros' , default = False ,
154159 help = "Include zeros into output basic data statistics (--stats)" ),
@@ -214,18 +219,24 @@ def proc_file(f, opts):
214219 else :
215220 row += ['error' ]
216221
217- if opts .stats :
222+ if opts .stats or opts . counts :
218223 # We are doomed to load data
219224 try :
220225 d = vol .get_data ()
221226 if not opts .stats_zeros :
222227 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 ))]
227238 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 ))
229240 row += ['error' ]
230241 return row
231242
0 commit comments