|
| 1 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 2 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 3 | +"""The minc module provides classes for interfacing with the `MINC |
| 4 | +<http://www.bic.mni.mcgill.ca/ServicesSoftware/MINC>`_ command line tools. This |
| 5 | +module was written to work with MINC version 2.2.00. |
| 6 | +
|
| 7 | +Author: Carlo Hamalainen <[email protected]> |
| 8 | + http://carlo-hamalainen.net |
| 9 | +""" |
| 10 | + |
| 11 | +from ..base import ( |
| 12 | + TraitedSpec, |
| 13 | + CommandLineInputSpec, |
| 14 | + CommandLine, |
| 15 | + StdOutCommandLineInputSpec, |
| 16 | + StdOutCommandLine, |
| 17 | + File, |
| 18 | + Directory, |
| 19 | + InputMultiPath, |
| 20 | + OutputMultiPath, |
| 21 | + traits, |
| 22 | + isdefined, |
| 23 | +) |
| 24 | + |
| 25 | +import glob |
| 26 | +import os |
| 27 | +import os.path |
| 28 | +import re |
| 29 | + |
| 30 | +import warnings |
| 31 | +warn = warnings.warn |
| 32 | +warnings.filterwarnings('always', category=UserWarning) |
| 33 | + |
| 34 | + |
| 35 | +def check_minc(): |
| 36 | + """Returns True if and only if MINC is installed.' |
| 37 | + """ |
| 38 | + |
| 39 | + return Info.version() is not None |
| 40 | + |
| 41 | + |
| 42 | +def no_minc(): |
| 43 | + """Returns True if and only if MINC is *not* installed. |
| 44 | + """ |
| 45 | + return not check_minc() |
| 46 | + |
| 47 | + |
| 48 | +class Info(object): |
| 49 | + """Handle MINC version information. |
| 50 | +
|
| 51 | + version refers to the version of MINC on the system |
| 52 | + """ |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def version(): |
| 56 | + """Check for minc version on the system |
| 57 | +
|
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + None |
| 61 | +
|
| 62 | + Returns |
| 63 | + ------- |
| 64 | + version : dict |
| 65 | + Version number as dict or None if MINC not found |
| 66 | +
|
| 67 | + """ |
| 68 | + try: |
| 69 | + clout = CommandLine(command='mincinfo', |
| 70 | + args='-version', |
| 71 | + terminal_output='allatonce').run() |
| 72 | + except IOError: |
| 73 | + return None |
| 74 | + |
| 75 | + out = clout.runtime.stdout |
| 76 | + |
| 77 | + def read_program_version(s): |
| 78 | + if 'program' in s: |
| 79 | + return s.split(':')[1].strip() |
| 80 | + return None |
| 81 | + |
| 82 | + def read_libminc_version(s): |
| 83 | + if 'libminc' in s: |
| 84 | + return s.split(':')[1].strip() |
| 85 | + return None |
| 86 | + |
| 87 | + def read_netcdf_version(s): |
| 88 | + if 'netcdf' in s: |
| 89 | + return ' '.join(s.split(':')[1:]).strip() |
| 90 | + return None |
| 91 | + |
| 92 | + def read_hdf5_version(s): |
| 93 | + if 'HDF5' in s: |
| 94 | + return s.split(':')[1].strip() |
| 95 | + return None |
| 96 | + |
| 97 | + versions = {'minc': None, |
| 98 | + 'libminc': None, |
| 99 | + 'netcdf': None, |
| 100 | + 'hdf5': None, } |
| 101 | + |
| 102 | + for l in out.split('\n'): |
| 103 | + for (name, f) in [('minc', read_program_version), |
| 104 | + ('libminc', read_libminc_version), |
| 105 | + ('netcdf', read_netcdf_version), |
| 106 | + ('hdf5', read_hdf5_version), ]: |
| 107 | + if f(l) is not None: |
| 108 | + versions[name] = f(l) |
| 109 | + |
| 110 | + return versions |
| 111 | + |
| 112 | + |
| 113 | +def aggregate_filename(files, new_suffix): |
| 114 | + """ |
| 115 | + Try to work out a sensible name given a set of files that have |
| 116 | + been combined in some way (e.g. averaged). If we can't work out a |
| 117 | + sensible prefix, we use the first filename in the list. |
| 118 | +
|
| 119 | + Examples |
| 120 | + -------- |
| 121 | +
|
| 122 | + >>> from nipype.interfaces.minc.base import aggregate_filename |
| 123 | + >>> f = aggregate_filename(['/tmp/foo1.mnc', '/tmp/foo2.mnc', '/tmp/foo3.mnc'], 'averaged') |
| 124 | + >>> os.path.split(f)[1] # This has a full path, so just check the filename. |
| 125 | + 'foo_averaged.mnc' |
| 126 | +
|
| 127 | + >>> f = aggregate_filename(['/tmp/foo1.mnc', '/tmp/blah1.mnc'], 'averaged') |
| 128 | + >>> os.path.split(f)[1] # This has a full path, so just check the filename. |
| 129 | + 'foo1_averaged.mnc' |
| 130 | +
|
| 131 | + """ |
| 132 | + |
| 133 | + path = os.path.split(files[0])[0] |
| 134 | + names = [os.path.splitext(os.path.split(x)[1])[0] for x in files] |
| 135 | + common_prefix = os.path.commonprefix(names) |
| 136 | + |
| 137 | + path = os.getcwd() |
| 138 | + |
| 139 | + if common_prefix == '': |
| 140 | + return os.path.abspath(os.path.join(path, os.path.splitext(files[0])[0] + '_' + new_suffix + '.mnc')) |
| 141 | + else: |
| 142 | + return os.path.abspath(os.path.join(path, common_prefix + '_' + new_suffix + '.mnc')) |
0 commit comments