def add_op(volumes, onGrid=None, boundingGrid=None, subregion='all', step=1, gridSubregion='all', gridStep=1, inPlace=False, scaleFactors=None, modelId=None): volumes = filter_volumes(volumes) if boundingGrid is None and not inPlace: boundingGrid = (onGrid is None) if onGrid is None: onGrid = volumes[:1] onGrid = filter_volumes(onGrid, 'onGrid') subregion = parse_subregion(subregion) step = parse_step(step) gridSubregion = parse_subregion(gridSubregion, 'gridSubregion') gridStep = parse_step(gridStep, 'gridStep') if inPlace: if boundingGrid or gridStep != 1 or gridSubregion != 'all': raise CommandError, "Can't use inPlace option with boundingGrid or gridStep or gridSubregion options" for gv in onGrid: if not gv.data.writable: raise CommandError, "Can't modify volume in place: %s" % gv.name if not gv in volumes: raise CommandError, "Can't change grid in place" if isinstance(scaleFactors, basestring): scaleFactors = parse_floats(scaleFactors, 'scaleFactors', len(volumes)) modelId = parse_model_id(modelId) for gv in onGrid: add_operation(volumes, subregion, step, gv, gridSubregion, gridStep, boundingGrid, inPlace, scaleFactors, modelId)
def add_op(volumes, onGrid = None, boundingGrid = None, subregion = 'all', step = 1, gridSubregion = 'all', gridStep = 1, inPlace = False, scaleFactors = None, modelId = None): volumes = filter_volumes(volumes) if boundingGrid is None and not inPlace: boundingGrid = (onGrid is None) if onGrid is None: onGrid = volumes[:1] onGrid = filter_volumes(onGrid, 'onGrid') subregion = parse_subregion(subregion) step = parse_step(step) gridSubregion = parse_subregion(gridSubregion, 'gridSubregion') gridStep = parse_step(gridStep, 'gridStep') if inPlace: if boundingGrid or gridStep != 1 or gridSubregion != 'all': raise CommandError, "Can't use inPlace option with boundingGrid or gridStep or gridSubregion options" for gv in onGrid: if not gv.data.writable: raise CommandError, "Can't modify volume in place: %s" % gv.name if not gv in volumes: raise CommandError, "Can't change grid in place" if isinstance(scaleFactors, basestring): scaleFactors = parse_floats(scaleFactors, 'scaleFactors', len(volumes)) modelId = parse_model_id(modelId) for gv in onGrid: add_operation(volumes, subregion, step, gv, gridSubregion, gridStep, boundingGrid, inPlace, scaleFactors, modelId)
def map_statistics(operation, volume, step=1, subregion='all'): from VolumeViewer import Volume vlist = [m for m in volume if isinstance(m, Volume)] if len(vlist) == 0: raise CommandError('No volume specified') from Commands import parse_step, parse_subregion subregion_arg = subregion subregion = parse_subregion(subregion) step_arg = step step = parse_step(step) import VolumeStatistics as VS for v in vlist: m = v.matrix(step=step, subregion=subregion) mean, sd, rms = VS.mean_sd_rms(m) descrip = v.name if step_arg != 1: descrip += ', step %s' % step_arg if subregion_arg != 'all': descrip += ', subregion %s' % subregion_arg msg = '%s: mean = %.5g, SD = %.5g, RMS = %.5g' % (descrip, mean, sd, rms) VS.message(msg, show_reply_log=True)
def map_sum(operation, volume, aboveThreshold=None, step=1, subregion='all'): from VolumeViewer import Volume vlist = [m for m in volume if isinstance(m, Volume)] if len(vlist) == 0: raise CommandError('No volume specified') from Commands import parse_step, parse_subregion subregion_arg = subregion subregion = parse_subregion(subregion) step_arg = step step = parse_step(step) import numpy import VolumeStatistics as VS for v in vlist: m = v.matrix(step=step, subregion=subregion) if aboveThreshold is None: s = m.sum(dtype=numpy.float64) else: ma = (m >= aboveThreshold) na = ma.sum(dtype=numpy.int64) mt = ma.astype(m.dtype) mt *= m s = mt.sum(dtype=numpy.float64) descrip = v.name if step_arg != 1: descrip += ', step %s' % step_arg if subregion_arg != 'all': descrip += ', subregion %s' % subregion_arg if not aboveThreshold is None: descrip += ', level >= %.5g, npoints %d' % (aboveThreshold, na) msg = '%s: sum = %.5g' % (descrip, s) VS.message(msg, show_reply_log=True)
def scale_op(volumes, shift=0, factor=1, type=None, subregion='all', step=1, modelId=None): volumes = filter_volumes(volumes) check_number(shift, 'shift') check_number(factor, 'factor') if not type is None: import numpy as n types = { 'int8': n.int8, 'uint8': n.uint8, 'int16': n.int16, 'uint16': n.uint16, 'int32': n.int32, 'uint32': n.uint32, 'float32': n.float32, 'float64': n.float64, } if type in types: type = types[type] else: raise CommandError, ('Unknown data value type "%s", use %s' % (type, ', '.join(types.keys()))) subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from scale import scaled_volume for v in volumes: scaled_volume(v, factor, shift, type, step, subregion, modelId)
def morph_op(volumes, frames = 25, start = 0, playStep = 0.04, playDirection = 1, playRange = None, addMode = False, constantVolume = False, scaleFactors = None, hideOriginalMaps = True, subregion = 'all', step = 1, modelId = None): volumes = filter_volumes(volumes) check_number(frames, 'frames', int, nonnegative = True) check_number(start, 'start') check_number(playStep, 'playStep', nonnegative = True) if playRange is None: if addMode: prange = (-1.0,1.0) else: prange = (0.0,1.0) else: prange = parse_floats(playRange, 'playRange', 2) check_number(playDirection, 'playDirection') sfactors = parse_floats(scaleFactors, 'scaleFactors', len(volumes)) subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) vs = [tuple(v.matrix_size(step = step, subregion = subregion)) for v in volumes] if len(set(vs)) > 1: sizes = ' and '.join([str(s) for s in vs]) raise CommandError, "Volume grid sizes don't match: %s" % sizes from MorphMap import morph_maps morph_maps(volumes, frames, start, playStep, playDirection, prange, addMode, constantVolume, sfactors, hideOriginalMaps, subregion, step, modelId)
def scale_op(volumes, shift = 0, factor = 1, type = None, subregion = 'all', step = 1, modelId = None): volumes = filter_volumes(volumes) check_number(shift, 'shift') check_number(factor, 'factor') if not type is None: import numpy as n types = {'int8': n.int8, 'uint8': n.uint8, 'int16': n.int16, 'uint16': n.uint16, 'int32': n.int32, 'uint32': n.uint32, 'float32': n.float32, 'float64': n.float64, } if type in types: type = types[type] else: raise CommandError, ('Unknown data value type "%s", use %s' % (type, ', '.join(types.keys()))) subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from scale import scaled_volume for v in volumes: scaled_volume(v, factor, shift, type, step, subregion, modelId)
def fourier_op(volumes, subregion = 'all', step = 1, modelId = None): volumes = filter_volumes(volumes) subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from fourier import fourier_transform for v in volumes: fourier_transform(v, step, subregion, modelId)
def laplacian_op(volumes, subregion = 'all', step = 1, modelId = None): volumes = filter_volumes(volumes) subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from laplace import laplacian for v in volumes: laplacian(v, step, subregion, modelId)
def z_flip_op(volumes, subregion='all', step=1, inPlace=False, modelId=None): volumes = filter_volumes(volumes) subregion = parse_subregion(subregion) step = parse_step(step) check_in_place(inPlace, volumes[:1]) modelId = parse_model_id(modelId) for v in volumes: zflip_operation(v, subregion, step, inPlace, modelId)
def resample_op(volumes, onGrid = None, boundingGrid = False, subregion = 'all', step = 1, gridSubregion = 'all', gridStep = 1, modelId = None): volumes = filter_volumes(volumes) if onGrid is None: raise CommandError, 'Resample operation must specify onGrid option' subregion = parse_subregion(subregion) step = parse_step(step) gridSubregion = parse_subregion(gridSubregion, 'gridSubregion') gridStep = parse_step(gridStep, 'gridStep') onGrid = filter_volumes(onGrid, 'onGrid') modelId = parse_model_id(modelId) for v in volumes: for gv in onGrid: add_operation([v], subregion, step, gv, gridSubregion, gridStep, boundingGrid, False, None, modelId)
def laplacian_op(volumes, subregion='all', step=1, modelId=None): volumes = filter_volumes(volumes) subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from laplace import laplacian for v in volumes: laplacian(v, step, subregion, modelId)
def fourier_op(volumes, subregion='all', step=1, modelId=None): volumes = filter_volumes(volumes) subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from fourier import fourier_transform for v in volumes: fourier_transform(v, step, subregion, modelId)
def z_flip_op(volumes, subregion = 'all', step = 1, inPlace = False, modelId = None): volumes = filter_volumes(volumes) subregion = parse_subregion(subregion) step = parse_step(step) check_in_place(inPlace, volumes[:1]) modelId = parse_model_id(modelId) for v in volumes: zflip_operation(v, subregion, step, inPlace, modelId)
def gaussian_op(volumes, sDev=1.0, subregion='all', step=1, modelId=None): volumes = filter_volumes(volumes) check_number(sDev, 'sDev', positive=True) subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from gaussian import gaussian_convolve for v in volumes: gaussian_convolve(v, sDev, step, subregion, modelId)
def gaussian_op(volumes, sDev = 1.0, subregion = 'all', step = 1, modelId = None): volumes = filter_volumes(volumes) check_number(sDev, 'sDev', positive = True) subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from gaussian import gaussian_convolve for v in volumes: gaussian_convolve(v, sDev, step, subregion, modelId)
def resample_op(volumes, onGrid=None, boundingGrid=False, subregion='all', step=1, gridSubregion='all', gridStep=1, modelId=None): volumes = filter_volumes(volumes) if onGrid is None: raise CommandError, 'Resample operation must specify onGrid option' subregion = parse_subregion(subregion) step = parse_step(step) gridSubregion = parse_subregion(gridSubregion, 'gridSubregion') gridStep = parse_step(gridStep, 'gridStep') onGrid = filter_volumes(onGrid, 'onGrid') modelId = parse_model_id(modelId) for v in volumes: for gv in onGrid: add_operation([v], subregion, step, gv, gridSubregion, gridStep, boundingGrid, False, None, modelId)
def boxes_op(volumes, markers, size = 0, useMarkerSize = False, subregion = 'all', step = 1, modelId = None): volumes = filter_volumes(volumes) check_number(size, 'size') if size <= 0 and not useMarkerSize: raise CommandError, 'Must specify size or enable useMarkerSize' subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from boxes import boxes for v in volumes: boxes(v, markers, size, useMarkerSize, step, subregion, modelId)
def bin_op(volumes, subregion='all', step=1, binSize=(2, 2, 2), modelId=None): volumes = filter_volumes(volumes) subregion = parse_subregion(subregion) step = parse_step(step) if isinstance(binSize, int): binSize = (binSize, binSize, binSize) elif isinstance(binSize, basestring): binSize = parse_ints(binSize, 'binSize', 3) modelId = parse_model_id(modelId) from bin import bin for v in volumes: bin(v, binSize, step, subregion, modelId)
def permute_axes_op(volumes, axisOrder = 'xyz', subregion = 'all', step = 1, modelId = None): volumes = filter_volumes(volumes) ao = {'xyz':(0,1,2), 'xzy':(0,2,1), 'yxz':(1,0,2), 'yzx':(1,2,0), 'zxy':(2,0,1), 'zyx':(2,1,0)} if not axisOrder in ao: raise CommandError, 'Axis order must be xyz, xzy, zxy, zyx, yxz, or yzx' subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from permute import permute_axes for v in volumes: permute_axes(v, ao[axisOrder], step, subregion, modelId)
def bin_op(volumes, subregion = 'all', step = 1, binSize = (2,2,2), modelId = None): volumes = filter_volumes(volumes) subregion = parse_subregion(subregion) step = parse_step(step) if isinstance(binSize, int): binSize = (binSize, binSize, binSize) elif isinstance(binSize, basestring): binSize = parse_ints(binSize, 'binSize', 3) modelId = parse_model_id(modelId) from bin import bin for v in volumes: bin(v, binSize, step, subregion, modelId)
def octant_complement_op(volumes, center = None, iCenter = None, subregion = 'all', step = 1, inPlace = False, fillValue = 0, modelId = None): volumes = filter_volumes(volumes) center = parse_floats(center, 'center', 3) iCenter = parse_floats(iCenter, 'iCenter', 3) subregion = parse_subregion(subregion) step = parse_step(step) check_in_place(inPlace, volumes) check_number(fillValue, 'fillValue') modelId = parse_model_id(modelId) outside = False for v in volumes: octant_operation(v, outside, center, iCenter, subregion, step, inPlace, fillValue, modelId)
def median_op(volumes, binSize = 3, iterations = 1, subregion = 'all', step = 1, modelId = None): volumes = filter_volumes(volumes) check_number(iterations, 'iterations', positive = True) binSize = parse_step(binSize, 'binSize', require_3_tuple = True) for b in binSize: if b <= 0 or b % 2 == 0: raise CommandError, 'Bin size must be positive odd integer, got %d' % b subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from median import median_filter for v in volumes: median_filter(v, binSize, iterations, step, subregion, modelId)
def boxes_op(volumes, markers, size=0, useMarkerSize=False, subregion='all', step=1, modelId=None): volumes = filter_volumes(volumes) check_number(size, 'size') if size <= 0 and not useMarkerSize: raise CommandError, 'Must specify size or enable useMarkerSize' subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from boxes import boxes for v in volumes: boxes(v, markers, size, useMarkerSize, step, subregion, modelId)
def median_op(volumes, binSize=3, iterations=1, subregion='all', step=1, modelId=None): volumes = filter_volumes(volumes) check_number(iterations, 'iterations', positive=True) binSize = parse_step(binSize, 'binSize', require_3_tuple=True) for b in binSize: if b <= 0 or b % 2 == 0: raise CommandError, 'Bin size must be positive odd integer, got %d' % b subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from median import median_filter for v in volumes: median_filter(v, binSize, iterations, step, subregion, modelId)
def octant_complement_op(volumes, center=None, iCenter=None, subregion='all', step=1, inPlace=False, fillValue=0, modelId=None): volumes = filter_volumes(volumes) center = parse_floats(center, 'center', 3) iCenter = parse_floats(iCenter, 'iCenter', 3) subregion = parse_subregion(subregion) step = parse_step(step) check_in_place(inPlace, volumes) check_number(fillValue, 'fillValue') modelId = parse_model_id(modelId) outside = False for v in volumes: octant_operation(v, outside, center, iCenter, subregion, step, inPlace, fillValue, modelId)
def morph_op(volumes, frames=25, start=0, playStep=0.04, playDirection=1, playRange=None, addMode=False, constantVolume=False, scaleFactors=None, hideOriginalMaps=True, subregion='all', step=1, modelId=None): volumes = filter_volumes(volumes) check_number(frames, 'frames', int, nonnegative=True) check_number(start, 'start') check_number(playStep, 'playStep', nonnegative=True) if playRange is None: if addMode: prange = (-1.0, 1.0) else: prange = (0.0, 1.0) else: prange = parse_floats(playRange, 'playRange', 2) check_number(playDirection, 'playDirection') sfactors = parse_floats(scaleFactors, 'scaleFactors', len(volumes)) subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) vs = [ tuple(v.matrix_size(step=step, subregion=subregion)) for v in volumes ] if len(set(vs)) > 1: sizes = ' and '.join([str(s) for s in vs]) raise CommandError, "Volume grid sizes don't match: %s" % sizes from MorphMap import morph_maps morph_maps(volumes, frames, start, playStep, playDirection, prange, addMode, constantVolume, sfactors, hideOriginalMaps, subregion, step, modelId)
def permute_axes_op(volumes, axisOrder='xyz', subregion='all', step=1, modelId=None): volumes = filter_volumes(volumes) ao = { 'xyz': (0, 1, 2), 'xzy': (0, 2, 1), 'yxz': (1, 0, 2), 'yzx': (1, 2, 0), 'zxy': (2, 0, 1), 'zyx': (2, 1, 0) } if not axisOrder in ao: raise CommandError, 'Axis order must be xyz, xzy, zxy, zyx, yxz, or yzx' subregion = parse_subregion(subregion) step = parse_step(step) modelId = parse_model_id(modelId) from permute import permute_axes for v in volumes: permute_axes(v, ao[axisOrder], step, subregion, modelId)