def runner(parser, options, args): if not hasattr(parser, 'runner'): options.output_path = None if args: if len(args) == 1: if options.input_path: print >> sys.stderr, "WARNING: overwriting input path %r with %r" % ( options.input_path, args[0]) options.input_path = args[0] elif len(args) == 2: if options.input_path: print >> sys.stderr, "WARNING: overwriting input path %r with %r" % ( options.input_path, args[0]) options.input_path = args[0] if options.output_path: print >> sys.stderr, "WARNING: overwriting output path %r with %r" % ( options.output_path, args[1]) options.output_path = args[1] else: parser.error( "Incorrect number of arguments (expected upto 2 but got %s)" % (len(args))) if options.input_path is None: parser.error('Expected --input-path but got nothing') options.input_path = fix_path(options.input_path) stack = ImageStack.load(options.input_path, options=options) numpy_types = numpy.typeDict.values() if options.output_type in ['<detect>', None]: if str(stack.images.dtype).startswith('float'): output_type_name = 'float32' elif str(stack.images.dtype).startswith('int'): output_type_name = 'int32' elif str(stack.images.dtype).startswith('uint'): output_type_name = 'uint32' else: output_type_name = 'int32' else: output_type_name = options.output_type.lower() output_type = getattr(numpy, output_type_name, None) mn, mx = stack.images.min(), stack.images.max() print 'Input minimum and maximum: %s, %s' % (mn, mx) if options.scale and 'int' in output_type_name: tmn, tmx = get_dtype_min_max(output_type) new_images = (tmn + float(tmx - tmn) * (stack.images - float(mn)) / (mx - mn)).astype(output_type) else: new_images = stack.images.astype(output_type) print 'Output minimum and maximum: %s, %s' % (new_images.min(), new_images.max()) output_path = options.output_path output_ext = options.output_ext if output_path is None: dn = os.path.dirname(options.input_path) bn = os.path.basename(options.input_path) if os.path.isfile(options.input_path): fn, ext = os.path.splitext(bn) type_part = None for t in numpy_types: if fn.endswith('_' + t.__name__): type_part = t.__name__ break if type_part is None: output_path = os.path.join( dn, fn + '_' + output_type_name + '.' + output_ext) else: output_path = os.path.join( dn, fn[:-len(type_part)] + output_type_name + '.' + output_ext) elif os.path.isdir(options.input_path): output_path = os.path.join( dn, bn + '_' + output_type_name + '.' + output_ext) else: raise NotImplementedError('%s is not file nor directory' % (options.input_path)) output_path = fix_path(output_path) print 'Saving new stack to', output_path if output_ext == 'tif': ImageStack(new_images, stack.pathinfo, options=options).save(output_path) elif output_ext == 'data': from iocbio.microscope.psf import normalize_unit_volume, discretize value_resolution = stack.pathinfo.get_value_resolution() normal_images = normalize_unit_volume(new_images, stack.get_voxel_sizes()) discrete = discretize(new_images / value_resolution) signal_indices = numpy.where(discrete > 0) new_value_resolution = value_resolution * normal_images.max( ) / new_images.max() ImageStack(normal_images, stack.pathinfo, value_resolution=new_value_resolution).save( output_path, zip(*signal_indices)) elif output_ext == 'vtk': from pyvtk import VtkData, StructuredPoints, PointData, Scalars vtk = VtkData(StructuredPoints(new_images.shape), PointData(Scalars(new_images.T.ravel()))) vtk.tofile(output_path, 'binary') else: raise NotImplementedError( ` output_ext `)
def runner (parser, options, args): if not hasattr(parser, 'runner'): options.output_path = None if args: if len (args)==1: if options.input_path: print >> sys.stderr, "WARNING: overwriting input path %r with %r" % (options.input_path, args[0]) options.input_path = args[0] elif len(args)==2: if options.input_path: print >> sys.stderr, "WARNING: overwriting input path %r with %r" % (options.input_path, args[0]) options.input_path = args[0] if options.output_path: print >> sys.stderr, "WARNING: overwriting output path %r with %r" % (options.output_path, args[1]) options.output_path = args[1] else: parser.error("Incorrect number of arguments (expected upto 2 but got %s)" % (len(args))) if options.input_path is None: parser.error('Expected --input-path but got nothing') options.input_path = fix_path (options.input_path) stack = ImageStack.load(options.input_path, options=options) numpy_types = numpy.typeDict.values() if options.output_type in ['<detect>', None]: if str (stack.images.dtype).startswith ('float'): output_type_name = 'float32' elif str (stack.images.dtype).startswith ('int'): output_type_name = 'int32' elif str (stack.images.dtype).startswith ('uint'): output_type_name = 'uint32' else: output_type_name = 'int32' else: output_type_name = options.output_type.lower() output_type = getattr (numpy, output_type_name, None) mn, mx = stack.images.min (), stack.images.max () print 'Input minimum and maximum: %s, %s' % (mn, mx) if options.scale and 'int' in output_type_name: tmn, tmx = get_dtype_min_max(output_type) new_images = (tmn + float(tmx - tmn) * (stack.images-float(mn)) / (mx - mn)).astype (output_type) else: new_images = stack.images.astype (output_type) print 'Output minimum and maximum: %s, %s' % (new_images.min (), new_images.max ()) output_path = options.output_path output_ext = options.output_ext if output_path is None: dn = os.path.dirname(options.input_path) bn = os.path.basename(options.input_path) if os.path.isfile(options.input_path): fn, ext = os.path.splitext (bn) type_part = None for t in numpy_types: if fn.endswith('_' + t.__name__): type_part = t.__name__ break if type_part is None: output_path = os.path.join(dn, fn + '_' + output_type_name + '.' + output_ext) else: output_path = os.path.join(dn, fn[:-len(type_part)] + output_type_name + '.' + output_ext) elif os.path.isdir (options.input_path): output_path = os.path.join (dn, bn+'_'+output_type_name + '.' + output_ext) else: raise NotImplementedError ('%s is not file nor directory' % (options.input_path)) output_path = fix_path(output_path) print 'Saving new stack to',output_path if output_ext=='tif': ImageStack(new_images, stack.pathinfo, options=options).save(output_path) elif output_ext=='data': from iocbio.microscope.psf import normalize_unit_volume, discretize value_resolution = stack.pathinfo.get_value_resolution() normal_images = normalize_unit_volume(new_images, stack.get_voxel_sizes()) discrete = discretize(new_images / value_resolution) signal_indices = numpy.where(discrete>0) new_value_resolution = value_resolution * normal_images.max() / new_images.max() ImageStack(normal_images, stack.pathinfo, value_resolution = new_value_resolution).save(output_path, zip(*signal_indices)) elif output_ext=='vtk': from pyvtk import VtkData, StructuredPoints, PointData, Scalars vtk = VtkData (StructuredPoints (new_images.shape), PointData(Scalars(new_images.T.ravel()))) vtk.tofile(output_path, 'binary') else: raise NotImplementedError (`output_ext`)
def runner (parser, options, args): if not hasattr(parser, 'runner'): options.output_path = None if args: if len (args)==1: if options.input_path: print >> sys.stderr, "WARNING: overwriting input path %r with %r" % (options.input_path, args[0]) options.input_path = args[0] elif len(args)==2: if options.input_path: print >> sys.stderr, "WARNING: overwriting input path %r with %r" % (options.input_path, args[0]) options.input_path = args[0] if options.output_path: print >> sys.stderr, "WARNING: overwriting output path %r with %r" % (options.output_path, args[1]) options.output_path = args[1] else: parser.error("Incorrect number of arguments (expected upto 2 but got %s)" % (len(args))) if options.input_path is None: parser.error('Expected --input-path but got nothing') options.input_path = fix_path (options.input_path) stack = ImageStack.load(options.input_path, options=options) numpy_types = numpy.typeDict.values() if options.output_type in ['<detect>', None]: output_type_name = stack.images.dtype.name else: output_type_name = options.output_type.lower() output_type = getattr (numpy, output_type_name, None) nof_stacks = stack.get_nof_stacks() old_shape = stack.images.shape new_shape = (nof_stacks, old_shape[0]//nof_stacks) + old_shape[1:] new_images = numpy.zeros (new_shape[1:], dtype=output_type_name) first_stack = None last_stack = None for i, stacki in enumerate(stack.images.reshape(new_shape)): if i==0: first_stack = stacki.astype (float) new_images[:] = stacki else: err_first = abs(stacki - first_stack).mean() err_last = abs(stacki - last_stack).mean() print ('Stack %i: mean abs difference from first and last stack: %.3f, %.3f' % (i+1, err_first, err_last)) new_images += stacki last_stack = stacki.astype(float) output_path = options.output_path output_ext = options.output_ext if output_path is None: dn = os.path.dirname(options.input_path) bn = os.path.basename(options.input_path) if os.path.isfile(options.input_path): fn, ext = os.path.splitext (bn) fn += '_sumstacks%s' % (nof_stacks) type_part = None for t in numpy_types: if fn.endswith('_' + t.__name__): type_part = t.__name__ break if type_part is None: output_path = os.path.join(dn, fn + '_' + output_type_name + '.' + output_ext) else: output_path = os.path.join(dn, fn[:-len(type_part)] + output_type_name + '.' + output_ext) elif os.path.isdir (options.input_path): bn += '_sumstacks%s' % (nof_stacks) output_path = os.path.join (dn, bn+'_'+output_type_name + '.' + output_ext) else: raise NotImplementedError ('%s is not file nor directory' % (options.input_path)) output_path = fix_path(output_path) print 'Saving new stack to',output_path if output_ext=='tif': ImageStack(new_images, stack.pathinfo, options=options).save(output_path) elif output_ext=='data': from iocbio.microscope.psf import normalize_unit_volume, discretize value_resolution = stack.pathinfo.get_value_resolution() normal_images = normalize_unit_volume(new_images, stack.get_voxel_sizes()) discrete = discretize(new_images / value_resolution) signal_indices = numpy.where(discrete>0) new_value_resolution = value_resolution * normal_images.max() / new_images.max() ImageStack(normal_images, stack.pathinfo, value_resolution = new_value_resolution).save(output_path, zip(*signal_indices)) elif output_ext=='vtk': from pyvtk import VtkData, StructuredPoints, PointData, Scalars vtk = VtkData (StructuredPoints (new_images.shape), PointData(Scalars(new_images.T.ravel()))) vtk.tofile(output_path, 'binary') else: raise NotImplementedError (`output_ext`)