Skip to content

Commit 05ce8d2

Browse files
committed
ENH+RF: nib-ls -c/--counts to report counts for each value (useful for ROI maps)
Also adjusted slightly output of --stats to include range in [from, to]
1 parent 094e6d2 commit 05ce8d2

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

bin/nib-ls

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Output a summary table for neuroimaging files (resolution, dimensionality, etc.)
1313
from __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
2222
from math import ceil
2323
from optparse import OptionParser, Option
2424
from io import StringIO
25+
from collections import Counter
2526

2627
import 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

nibabel/tests/test_scripts.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ def script_test(func):
5353
DATA_PATH = abspath(pjoin(dirname(__file__), 'data'))
5454

5555

56-
def check_nib_ls_example4d(opts=[], hdrs_str=""):
56+
def check_nib_ls_example4d(opts=[], hdrs_str="", other_str=""):
5757
# test nib-ls script
5858
fname = pjoin(DATA_PATH, 'example4d.nii.gz')
5959
expected_re = (" (int16|[<>]i2) \[128, 96, 24, 2\] "
60-
"2.00x2.00x2.20x2000.00 #exts: 2%s sform$"
61-
% hdrs_str)
60+
"2.00x2.00x2.20x2000.00 #exts: 2%s sform%s$"
61+
% (hdrs_str, other_str))
6262
cmd = ['nib-ls'] + opts + [fname]
6363
code, stdout, stderr = run_command(cmd)
6464
assert_equal(fname, stdout[:len(fname)])
@@ -68,7 +68,15 @@ def check_nib_ls_example4d(opts=[], hdrs_str=""):
6868
def test_nib_ls():
6969
yield check_nib_ls_example4d
7070
yield check_nib_ls_example4d, \
71-
['-H', 'dim,bitpix'], " \[ 4 128 96 24 2 1 1 1\] 16"
71+
['-H', 'dim,bitpix'], " \[ 4 128 96 24 2 1 1 1\] 16"
72+
yield check_nib_ls_example4d, ['-c'], "", " 2:3 3:2 4:1 5:1.*"
73+
# both stats and counts
74+
yield check_nib_ls_example4d, \
75+
['-c', '-s'], "", " \[229725\] \[2, 1.2e\+03\] 2:3 3:2 4:1 5:1.*"
76+
# and must not error out if we allow for zeros
77+
yield check_nib_ls_example4d, \
78+
['-c', '-s', '-z'], "", " \[589824\] \[0, 1.2e\+03\] 0:360099 2:3 3:2 4:1 5:1.*"
79+
7280

7381
@script_test
7482
def test_nib_ls_multiple():

0 commit comments

Comments
 (0)