def headerKeyValue(image_path, key): import subprocess from mrtrix3 import app, run command = [ run.exeName(run.versionMatch('mrinfo')), image_path, '-property', key ] if app._verbosity > 1: app.console('Command: \'' + ' '.join(command) + '\' (piping data to local storage)') proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None) result, err = proc.communicate() result = result.rstrip().decode('utf-8') if app._verbosity > 1: app.console('Result: ' + result) return result
def mrinfo(image_path, field): #pylint: disable=unused-variable import subprocess from mrtrix3 import app, run command = [ run.exeName(run.versionMatch('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) result, dummy_err = proc.communicate() result = result.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 statistic(image_path, statistic, mask_path = ''): import subprocess from mrtrix3 import app, run command = [ run.exeName(run.versionMatch('mrstats')), image_path, '-output', statistic ] if mask_path: command.extend([ '-mask', mask_path ]) if app._verbosity > 1: app.console('Command: \'' + ' '.join(command) + '\' (piping data to local storage)') proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None) result, err = proc.communicate() result = result.rstrip().decode('utf-8') if app._verbosity > 1: app.console('Result: ' + result) return result
def statistic(image_path, stat, options=''): #pylint: disable=unused-variable import shlex, subprocess from mrtrix3 import app, run command = [ run.exeName(run.versionMatch('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, dummy_err = proc.communicate() result = result.rstrip().decode('utf-8') if app.verbosity > 1: app.console('Result: ' + result) if proc.returncode: app.error('Error trying to calculate statistic \'' + stat + '\' from image \'' + image_path + '\'') return result
def headerField(image_path, field): import subprocess from mrtrix3 import app, run command = [ run.exeName(run.versionMatch('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) result, err = proc.communicate() result = result.rstrip().decode('utf-8') if app._verbosity > 1: if '\n' in result: app.console('Result: (' + str(result.count('\n')+1) + ' lines)') app.debug(result) else: app.console('Result: ' + result) return result
def getScheme(image_path): import subprocess from mrtrix3 import app, run command = [ run.versionMatch('mrinfo'), image_path, '-petable' ] if app._verbosity > 1: app.console('Command: \'' + ' '.join(command) + '\' (piping data to local storage)') proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None) result, err = proc.communicate() result = result.rstrip().decode('utf-8') if result: result = [ [ float(f) for f in line.split() ] for line in result.split('\n') ] if app._verbosity > 1: if not result: app.console('Result: No phase encoding table found') else: app.console('Result: ' + str(len(result)) + ' x ' + str(len(result[0])) + ' table') app.debug(str(result)) return result
def __init__(self, image_path): import json, os, subprocess from mrtrix3 import app, path, run filename = path.newTemporary('json') command = [ run.exeName(run.versionMatch('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: app.error('Could not access header information for image \'' + image_path + '\'') try: with open(filename, 'r') as f: data = json.load(f) except UnicodeDecodeError: with open(filename, 'r') as f: data = json.loads(f.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: app.error('Error in reading header information from file \'' + image_path + '\'') app.debug(str(vars(self)))
def __init__(self, image_path): import json, os, subprocess from mrtrix3 import app, path, run filename = path.newTemporary('json') command = [ run.exeName(run.versionMatch('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: app.error('Could not access header information for image \'' + image_path + '\'') try: with open(filename, 'r') as f: data = json.load(f) except UnicodeDecodeError: with open(filename, 'r') as f: data = json.loads(f.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: app.error('Error in reading header information from file \'' + image_path + '\'') app.debug(str(vars(self)))