|
| 1 | +#!/usr/bin/python |
| 2 | +#emacs: -*- mode: python-mode; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*- |
| 3 | +#ex: set sts=4 ts=4 sw=4 noet: |
| 4 | +""" |
| 5 | +Little script to list summary over given nifti files |
| 6 | +
|
| 7 | + COPYRIGHT: Yaroslav Halchenko 2011 |
| 8 | +
|
| 9 | + LICENSE: MIT |
| 10 | +
|
| 11 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | + of this software and associated documentation files (the "Software"), to deal |
| 13 | + in the Software without restriction, including without limitation the rights |
| 14 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | + copies of the Software, and to permit persons to whom the Software is |
| 16 | + furnished to do so, subject to the following conditions: |
| 17 | +
|
| 18 | + The above copyright notice and this permission notice shall be included in |
| 19 | + all copies or substantial portions of the Software. |
| 20 | +
|
| 21 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | + THE SOFTWARE. |
| 28 | +""" |
| 29 | +#-----------------\____________________________________/------------------ |
| 30 | + |
| 31 | +__author__ = 'Yaroslav Halchenko' |
| 32 | +__copyright__ = 'Copyright (c) 2011 Yaroslav Halchenko' |
| 33 | +__license__ = 'MIT' |
| 34 | + |
| 35 | +import sys |
| 36 | +from mvpa2.base.dochelpers import table2string |
| 37 | +from mvpa2.base import verbose, debug |
| 38 | +from mvpa2.misc.cmdline import parser, opts, Option |
| 39 | +import nibabel as nib |
| 40 | +import numpy as np |
| 41 | + |
| 42 | + |
| 43 | +def ap(l, format, sep=', '): |
| 44 | + """Little helper to enforce consistency""" |
| 45 | + ls = [format % x for x in l] |
| 46 | + return sep.join(ls) |
| 47 | + |
| 48 | + |
| 49 | +parser.usage = """Usage: %s [options] [infile1] [infile2] ...""" % sys.argv[0] |
| 50 | +parser.option_groups = [opts.common] |
| 51 | + |
| 52 | +parser.add_options([ |
| 53 | + Option("-s", "--stats", |
| 54 | + action="store_true", dest='stats', default=False, |
| 55 | + help="Output basic data statistics"), |
| 56 | + Option("-z", "--zeros", |
| 57 | + action="store_true", dest='stats_zeros', default=False, |
| 58 | + help="Include zeros into output basic data statistics (--stats)"), |
| 59 | + ]) |
| 60 | + |
| 61 | +(options, files) = parser.parse_args() |
| 62 | + |
| 63 | +if verbose.level < 3: |
| 64 | + # suppress nibabel format-compliance warnings |
| 65 | + nib.imageglobals.logger.level = 50 |
| 66 | + |
| 67 | +if len(files): |
| 68 | + maxfnlen = max([len(f) for f in files]) |
| 69 | + |
| 70 | + t = [] |
| 71 | + for f in files: |
| 72 | + row = ["%%-%ds" % maxfnlen % f] |
| 73 | + verbose(2, "Loading %s" % f) |
| 74 | + try: |
| 75 | + vol = nib.load(f) |
| 76 | + h = vol.get_header() |
| 77 | + except Exception, e: |
| 78 | + row += ['failed'] |
| 79 | + t.append(row) |
| 80 | + verbose(2, "Failed to gather information due to %s" % str(e)) |
| 81 | + continue |
| 82 | + row += [ str(h.get_data_dtype()), |
| 83 | + '@l[%s]' %ap(h.get_data_shape(), '%3g'), |
| 84 | + '@l%s' % ap(h.get_zooms(), '%.2f', 'x') ] |
| 85 | + # Slope |
| 86 | + if (h.has_data_slope or h.has_data_intercept) \ |
| 87 | + and not h.get_slope_inter() in [(1.0, 0.0), (None, None)]: |
| 88 | + row += ['@l*%.3g+%.3g' % h.get_slope_inter()] |
| 89 | + else: |
| 90 | + row += [ '' ] |
| 91 | + |
| 92 | + if (hasattr(h, 'extensions') and len(h.extensions)): |
| 93 | + row += ['@l#exts: %d' % len(h.extensions)] |
| 94 | + else: |
| 95 | + row += [ '' ] |
| 96 | + |
| 97 | + try: |
| 98 | + if (h.get_qform() != h.get_sform()).any(): |
| 99 | + row += ['sform'] |
| 100 | + else: |
| 101 | + row += [''] |
| 102 | + except Exception, e: |
| 103 | + verbose(2, "Failed to obtain qform or sform for %s due to %s" % (h, e)) |
| 104 | + if isinstance(h, nib.AnalyzeHeader): |
| 105 | + row += [''] |
| 106 | + else: |
| 107 | + row += ['error'] |
| 108 | + if options.stats: |
| 109 | + # We are doomed to load data |
| 110 | + d = vol.get_data() |
| 111 | + if not options.stats_zeros: |
| 112 | + d = d[np.nonzero(d)] |
| 113 | + # just # of elements |
| 114 | + row += ["[%d] " % np.prod(d.shape)] |
| 115 | + # stats |
| 116 | + row += [len(d) and '%.2g:%.2g' % (np.min(d), np.max(d)) or '-'] |
| 117 | + |
| 118 | + t.append(row) |
| 119 | + |
| 120 | + print(table2string(t)) |
0 commit comments