def mrinfo(image_path, field): #pylint: disable=unused-variable from mrtrix3 import app, run #pylint: disable=import-outside-toplevel command = [ run.exe_name(run.version_match('mrinfo')), image_path, '-' + field ] if app.VERBOSITY > 1: app.console('Command: \'' + ' '.join(command) + '\' (piping data to local storage)') proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None) #pylint: disable=consider-using-with result = proc.communicate()[0].rstrip().decode('utf-8') if app.VERBOSITY > 1: app.console('Result: ' + result) # Don't exit on error; let the calling function determine whether or not # the absence of the key is an issue return result
def statistics(image_path, **kwargs): #pylint: disable=unused-variable from mrtrix3 import app, run #pylint: disable=import-outside-toplevel mask = kwargs.pop('mask', None) allvolumes = kwargs.pop('allvolumes', False) ignorezero = kwargs.pop('ignorezero', False) if kwargs: raise TypeError( 'Unsupported keyword arguments passed to image.statistics(): ' + str(kwargs)) command = [run.exe_name(run.version_match('mrstats')), image_path] for stat in IMAGE_STATISTICS: command.extend(['-output', stat]) if mask: command.extend(['-mask', mask]) if allvolumes: command.append('-allvolumes') if ignorezero: command.append('-ignorezero') if app.VERBOSITY > 1: app.console('Command: \'' + ' '.join(command) + '\' (piping data to local storage)') try: from subprocess import DEVNULL #pylint: disable=import-outside-toplevel except ImportError: DEVNULL = open(os.devnull, 'wb') proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=DEVNULL) stdout = proc.communicate()[0] if proc.returncode: raise MRtrixError( 'Error trying to calculate statistics from image \'' + image_path + '\'') stdout_lines = [ line.strip() for line in stdout.decode('cp437').splitlines() ] result = [] for line in stdout_lines: line = line.replace('N/A', 'nan').split() assert len(line) == len(IMAGE_STATISTICS) result.append( ImageStatistics(float(line[0]), float(line[1]), float(line[2]), float(line[3]), float(line[4]), float(line[5]), int(line[6]))) if len(result) == 1: result = result[0] if app.VERBOSITY > 1: app.console('Result: ' + str(result)) return result
def __init__(self, image_path): from mrtrix3 import app, path, run #pylint: disable=import-outside-toplevel filename = path.name_temporary('json') command = [ run.exe_name(run.version_match('mrinfo')), image_path, '-json_all', filename ] if app.VERBOSITY > 1: app.console('Loading header for image file \'' + image_path + '\'') app.debug(str(command)) result = subprocess.call(command, stdout=None, stderr=None) if result: raise MRtrixError( 'Could not access header information for image \'' + image_path + '\'') try: with open(filename, 'r') as json_file: data = json.load(json_file) except UnicodeDecodeError: with open(filename, 'r') as json_file: data = json.loads(json_file.read().decode('utf-8', errors='replace')) os.remove(filename) try: #self.__dict__.update(data) # Load the individual header elements manually, for a couple of reasons: # - So that pylint knows that they'll be there # - Write to private members, and give read-only access self._name = data['name'] self._size = data['size'] self._spacing = data['spacing'] self._strides = data['strides'] self._format = data['format'] self._datatype = data['datatype'] self._intensity_offset = data['intensity_offset'] self._intensity_scale = data['intensity_scale'] self._transform = data['transform'] if not 'keyval' in data or not data['keyval']: self._keyval = {} else: self._keyval = data['keyval'] except: raise MRtrixError( 'Error in reading header information from file \'' + image_path + '\'') app.debug(str(vars(self)))
def statistic(image_path, stat, options=''): #pylint: disable=unused-variable import shlex, subprocess from mrtrix3 import app, MRtrixError, run command = [ run.exe_name(run.version_match('mrstats')), image_path, '-output', stat ] if options: command.extend(shlex.split(options)) if app.VERBOSITY > 1: app.console('Command: \'' + ' '.join(command) + '\' (piping data to local storage)') proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None) result = [ line.strip() for line in proc.communicate()[0].decode('cp437').splitlines() ] if stat == 'count': result = [ int(i) for i in result ] else: result = [ float(f) for f in result ] if len(result) == 1: result = result[0] if app.VERBOSITY > 1: app.console('Result: ' + str(result)) if proc.returncode: raise MRtrixError('Error trying to calculate statistic \'' + stat + '\' from image \'' + image_path + '\'') return result