def runner(parser, options, args): if not hasattr(parser, 'runner'): options.psf_path = None options.input_path = None options.output_path = None if args: if len(args) in [2, 3]: if options.psf_path: print >> sys.stderr, "WARNING: overwriting psf path %r with %r" % ( options.psf_path, args[0]) options.psf_path = args[0] if options.input_path: print >> sys.stderr, "WARNING: overwriting input path %r with %r" % ( options.input_path, args[1]) options.input_path = args[1] else: parser.error( "Incorrect number of arguments (expected 2 or 3 but got %r)" % ((args))) if len(args) == 3: if options.output_path: print >> sys.stderr, "WARNING: overwriting output path %r with %r" % ( options.output_path, args[2]) options.output_path = args[2] options.input_path = fix_path(options.input_path) if options.output_path is None: deconvolve_dir = get_path_dir(options.input_path, 'iocbio.deconvolve') else: deconvolve_dir = get_path_dir(options.output_path, 'iocbio.deconvolve') psf_path = get_psf_path(options) psf = ImageStack.load(psf_path, options=options) stack = ImageStack.load(options.input_path, options=options) deconvolved_image = deconvolve(psf, stack, deconvolve_dir, options=options) if options.output_path is None: b, e = os.path.splitext(options.input_path) suffix = deconvolved_image.pathinfo.suffix options.output_path = b + suffix + (e or '.tif') options.output_path = fix_path(options.output_path) if 1: print 'Saving result to %r' % (options.output_path) deconvolved_image.save(options.output_path)
def runner(parser, options, args): if not hasattr(parser, 'runner'): options.output_path = None if args: raise NotImplementedError( ` 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] options.output_path = args[1] else: parser.error( "incorrect number of arguments (expected upto 2 but got %s)" % (len(args))) options.kernel_path = fix_path(options.kernel_path) options.input_path = fix_path(options.input_path) if options.output_path is None: b, e = os.path.splitext(options.input_path) suffix = '_convolved' % () options.output_path = b + suffix + (e or '.tif') options.output_path = fix_path(options.output_path) kernel = ImageStack.load(options.kernel_path, options=options) stack = ImageStack.load(options.input_path, options=options) result = convolve(kernel.images, stack.images, options=options) if 1: print 'Saving result to', options.output_path ImageStack(result, stack.pathinfo).save(options.output_path)
def runner(parser, options, args): if not hasattr(parser, 'runner'): options.output_path = None if args: raise NotImplementedError (`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] options.output_path = args[1] else: parser.error("incorrect number of arguments (expected upto 2 but got %s)" % (len(args))) options.kernel_path = fix_path(options.kernel_path) options.input_path = fix_path(options.input_path) if options.output_path is None: b,e = os.path.splitext(options.input_path) suffix = '_convolved' % () options.output_path = b + suffix + (e or '.tif') options.output_path = fix_path(options.output_path) kernel = ImageStack.load(options.kernel_path, options=options) stack = ImageStack.load(options.input_path, options=options) result = convolve (kernel.images, stack.images, options=options) if 1: print 'Saving result to',options.output_path ImageStack(result, stack.pathinfo).save(options.output_path)
def runner(parser, options, args): if not hasattr(parser, 'runner'): options.input_path = None options.output_path = None if args: if len(args) in [1, 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] else: parser.error( "Incorrect number of arguments (expected 1 or 2 but got %r)" % ((args))) if len(args) == 2: if options.output_path: print >> sys.stderr, "WARNING: overwriting output path %r with %r" % ( options.output_path, args[1]) options.output_path = args[1] options.input_path = fix_path(options.input_path) if options.output_path is None: deconvolve_dir = get_path_dir(options.input_path, 'ioc.deconvolve_w_sphere') else: deconvolve_dir = get_path_dir(options.output_path, 'ioc.deconvolve_w_sphere') stack = ImageStack.load(options.input_path, options=options) deconvolved_image = deconvolve_sphere(stack, options.diameter * 1e-9, deconvolve_dir, options=options) if options.output_path is None: b, e = os.path.splitext(options.input_path) suffix = deconvolved_image.pathinfo.suffix options.output_path = b + suffix + (e or '.tif') options.output_path = fix_path(options.output_path) if 1: print 'Saving result to %r' % (options.output_path) deconvolved_image.save(options.output_path)
def runner(parser, options, args): if not hasattr(parser, 'runner'): options.output_path = None options.input_path = fix_path(options.input_path) stack = ImageStack.load(options.input_path, options=options) voxel_sizes = stack.get_voxel_sizes() roi_center_line = [ int(n.strip()) for n in options.roi_center_line.split(',') ] assert len(roi_center_line) == 4, ` roi_center_line ` roi_width = int(options.roi_width) N = options.nof_points lines = [] time_lst = [] last_lines = [] for i, image in enumerate(stack.images): result, labels = sarcomere_length(image, voxel_sizes[1:], roi_center_line, roi_width, N) if not lines: for r in result: lines.append([]) last_lines.append([]) for j, r in enumerate(result): last_lines[j].append(r) if len(last_lines[j]) >= 2: last_lines[j].pop(0) lines[j].append(numpy.mean(last_lines[j])) time_lst.append(i) import matplotlib.pyplot as plt for line in lines: plt.plot(time_lst, line) plt.legend(labels) plt.show()
def runner(parser, options, args): verbose = options.verbose if options.verbose is not None else True 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))) options.input_path = fix_path(options.input_path) if options.output_path is None: b, e = os.path.splitext(options.input_path) suffix = "_noised_%s" % (options.noise_type) options.output_path = b + suffix + (e or ".tif") options.output_path = fix_path(options.output_path) stack = ImageStack.load(options.input_path, options=options) if options.noise_type == "poisson": new_images = stack.images.copy() new_images[new_images <= 0] = 1 new_images = poisson.rvs(new_images) else: raise NotImplementedError(` options.noise_type `) if verbose: print "Saving result to", options.output_path ImageStack(new_images, stack.pathinfo).save(options.output_path)
def runner(parser, options, args): if not hasattr(parser, 'runner'): options.output_path = None options.input_path = fix_path(options.input_path) stack = ImageStack.load(options.input_path, options=options) voxel_sizes = stack.get_voxel_sizes() roi_center_line = [int(n.strip()) for n in options.roi_center_line.split(',')] assert len (roi_center_line)==4,`roi_center_line` roi_width = int (options.roi_width) N = options.nof_points lines = [] time_lst = [] last_lines = [] for i,image in enumerate(stack.images): result, labels = sarcomere_length (image, voxel_sizes[1:], roi_center_line, roi_width, N) if not lines: for r in result: lines.append([]) last_lines.append([]) for j,r in enumerate (result): last_lines[j].append(r) if len(last_lines[j])>=2: last_lines[j].pop(0) lines[j].append(numpy.mean (last_lines[j])) time_lst.append (i) import matplotlib.pyplot as plt for line in lines: plt.plot (time_lst, line) plt.legend (labels) plt.show ()
def runner (parser, options, args): if not hasattr(parser, 'runner'): options.input_path = None options.output_path = None if args: if len(args) in [1,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] else: parser.error("Incorrect number of arguments (expected 1 or 2 but got %r)" % ((args))) if len(args)==2: if options.output_path: print >> sys.stderr, "WARNING: overwriting output path %r with %r" % (options.output_path, args[1]) options.output_path = args[1] options.input_path = fix_path (options.input_path) if options.output_path is None: deconvolve_dir = get_path_dir(options.input_path, 'ioc.deconvolve_w_sphere') else: deconvolve_dir = get_path_dir(options.output_path, 'ioc.deconvolve_w_sphere') stack = ImageStack.load(options.input_path, options=options) deconvolved_image = deconvolve_sphere(stack, options.diameter*1e-9, deconvolve_dir, options=options) if options.output_path is None: b,e = os.path.splitext(options.input_path) suffix = deconvolved_image.pathinfo.suffix options.output_path = b + suffix + (e or '.tif') options.output_path = fix_path(options.output_path) if 1: print 'Saving result to %r' % (options.output_path) deconvolved_image.save(options.output_path)
import numpy from iocbio.ops import convolve from iocbio.microscope.deconvolution import deconvolve from iocbio.io import ImageStack import scipy.stats from matplotlib import pyplot as plt kernel = numpy.array([0, 1, 3, 1, 0]) test_data = numpy.array([0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0]) * 50 data = convolve(kernel, test_data) degraded_data = scipy.stats.poisson.rvs(numpy.where(data <= 0, 1e-16, data)).astype(data.dtype) psf = ImageStack(kernel, voxel_sizes=(1, )) stack = ImageStack(degraded_data, voxel_sizes=(1, )) deconvolved_data = deconvolve(psf, stack).images plt.plot(test_data, label='test') plt.plot(data, label='convolved') plt.plot(degraded_data, label='degraded') plt.plot(deconvolved_data, label='deconvolved') plt.legend() plt.ylabel('data') plt.xlabel('index') plt.title('Deconvolving degraded test data.') plt.savefig('deconvolve_poisson_1d.png') plt.show()
def runner(parser, options, args): if not hasattr(parser, 'runner'): options.output_path = None options.input_path = fix_path(options.input_path) stack = ImageStack.load(options.input_path, options=options) voxel_sizes = stack.get_voxel_sizes() dr = stack.get_lateral_resolution() dz = stack.get_axial_resolution() if dr is not None: print 'lateral resolution: %.3f um (%.1f x %.1f px^2)' % (1e6*dr, dr/voxel_sizes[1], dr/voxel_sizes[2]) print 'axial resolution: %.3f um (%.1fpx)' % (1e6*dz, dz / voxel_sizes[0]) vz,vy,vx = voxel_sizes m = 1 scales = (m*vz/dz, m*vy/dr, m*vx/dr) else: raise NotImplementedError ('get_lateral_resolution') kdims = [1+2*(int(numpy.ceil(1/s))//2) for s in scales] k = 'x'.join (map (str, kdims)) print 'Averaging window box:', k kernel_type = options.kernel smoothing_method = options.method boundary_condition = options.boundary mn, mx = stack.images.min (), stack.images.max() high_indices = numpy.where(stack.images >= mn + 0.9*(mx-mn)) high = stack.images[high_indices] from iocbio.ops import regress average, average_grad = regress (stack.images, scales, kernel = kernel_type, method = smoothing_method, boundary = boundary_condition, verbose = True, enable_fft=True) ImageStack(average, pathinfo=stack.pathinfo).save('average.tif') noise = stack.images - average ImageStack(noise-noise.min(), pathinfo=stack.pathinfo).save('noise.tif') bright_level = 0.999 *average.max() + 0.001 * average.min() bright_indices = numpy.where (average >= bright_level) print len(bright_indices[0]) bright_noise = stack.images[bright_indices] - average[bright_indices] a = stack.images[bright_indices].mean() d = stack.images[bright_indices].std() print 'mean=',a,'std=',d print 'peak SNR=',a/d print 'AVERAGE min, max, mean = %s, %s, %s' % (average.min (), average.max (), average.mean ()) print numpy.histogram(stack.images)[0] sys.exit () noise = stack.images - average var, var_grad = regress (noise*noise, scales, kernel = kernel_type, method = smoothing_method, boundary = boundary_condition, verbose = True, enable_fft=True) print 'VAR min, max, mean = %s, %s, %s' % (var.min (), var.max (), var.mean ()) indices = numpy.where (var > 0) print len(numpy.where (var==0)[0]), var.shape, var.dtype var[numpy.where (var<=0)] = 1 snr = average / numpy.sqrt(var) snr1 = snr[indices] print 'STACK min, max = %s, %s' % (mn, mx) print 'SNR min, max, mean = %s, %s, %s' % (snr1.min (), snr1.max (), snr1.mean ()) ImageStack(average, pathinfo=stack.pathinfo).save('average.tif') ImageStack(snr, pathinfo=stack.pathinfo).save('snr.tif') ImageStack(noise-noise.min(), pathinfo=stack.pathinfo).save('noise.tif')
def runner(parser, options, args): verbose = options.verbose if options.verbose is not None else True 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))) options.input_path = fix_path(options.input_path) kernel_width = options.kernel_width or None kernel_type = options.kernel smoothing_method = options.method boundary_condition = options.boundary stack = ImageStack.load(options.input_path, options=options) voxel_sizes = stack.get_voxel_sizes() if kernel_width is None: dr = stack.get_lateral_resolution() dz = stack.get_axial_resolution() if dr is None or dz is None: kernel_width = 3 else: print 'lateral resolution: %.3f um (%.1f x %.1f px^2)' % ( 1e6 * dr, dr / voxel_sizes[1], dr / voxel_sizes[2]) print 'axial resolution: %.3f um (%.1fpx)' % (1e6 * dz, dz / voxel_sizes[0]) vz, vy, vx = voxel_sizes m = 1 scales = (m * vz / dz, m * vy / dr, m * vx / dr) if kernel_width is not None: w = float(kernel_width) * min(voxel_sizes) scales = tuple([s / w for s in voxel_sizes]) print 'Window sizes:', [1 / s for s in scales] kdims = [1 + 2 * (int(numpy.ceil(1 / s)) // 2) for s in scales] k = 'x'.join(map(str, kdims)) if options.output_path is None: b, e = os.path.splitext(options.input_path) suffix = '_%s_%s%s' % (smoothing_method, kernel_type, k) options.output_path = b + suffix + (e or '.tif') options.output_path = fix_path(options.output_path) if options.link_function == 'identity': images = stack.images elif options.link_function == 'log': images = stack.images mn, mx = get_dtype_min_max(images.dtype) images = numpy.log(images) images[numpy.where(numpy.isnan(images))] = numpy.log(mn) else: raise NotImplementedError( ` options.link_function `) new_images, new_images_grad = regress(images, scales, kernel=kernel_type, method=smoothing_method, boundary=boundary_condition, verbose=verbose) if options.link_function == 'identity': pass elif options.link_function == 'log': new_images = numpy.exp(new_images) new_images = numpy.nan_to_num(new_images) else: raise NotImplementedError( ` options.link_function `) if verbose: print 'Leak: %.3f%%' % (100 * (1 - new_images.sum() / stack.images.sum())) print 'MSE:', ((new_images - stack.images)**2).mean() print 'Energy:', ((stack.images)**2).sum() print 'Saving result to', options.output_path ImageStack(new_images, stack.pathinfo).save(options.output_path)
def runner(parser, options, args): options = Options(options) 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] 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) images = stack.images roll_axis = dict(XY=0, XZ=1, YZ=2).get(options.projection, 0) axis_labels = ('Z', 'Y', 'X') #roll_axis = int (options.roll_axis) voxel_sizes = stack.get_voxel_sizes() resolution = ['', '', ''] title = '%s[%s]' % (os.path.basename(options.input_path), images.dtype) dr = stack.get_lateral_resolution() dz = stack.get_axial_resolution() if voxel_sizes: if dr is not None: resolution[1] = tostr(dr / voxel_sizes[1]) + 'px' resolution[2] = tostr(dr / voxel_sizes[2]) + 'px' if dz is not None: resolution[0] = tostr(dz / voxel_sizes[0]) + 'px' resolution = tuple(resolution) if roll_axis: images = numpy.rollaxis(images, roll_axis) if voxel_sizes: voxel_sizes = ( voxel_sizes[roll_axis], ) + voxel_sizes[:roll_axis] + voxel_sizes[roll_axis + 1:] axis_labels = (axis_labels[roll_axis], ) + axis_labels[:roll_axis] + axis_labels[roll_axis + 1:] resolutions = (resolution[roll_axis], ) + resolution[:roll_axis] + resolution[roll_axis + 1:] if voxel_sizes: xlabel = '%s, resol=%s, px size=%sum, size=%sum' \ % (axis_labels[-1], resolution[-1], tostr(voxel_sizes[-1]*1e6), tostr(voxel_sizes[-1]*1e6*images.shape[-1])) ylabel = '%s, resol=%s, px size=%sum, size=%sum' \ % (axis_labels[-2], resolution[-2], tostr(voxel_sizes[-2]*1e6), tostr(voxel_sizes[-2]*1e6*images.shape[-2])) else: xlabel = '%s' % (axis_labels[-1]) ylabel = '%s' % (axis_labels[-2]) import matplotlib.cm as cm import matplotlib.pyplot as pyplot from iocbio.io.tifffile import imshow if options.invert_cmap: cmap = getattr(cm, options.cmap + '_r', options.cmap) else: cmap = getattr(cm, options.cmap, 'gray') view_3d = options.get(view_3d='none') if view_3d and view_3d.lower() == 'none': view_3d = None if view_3d: l = [] for i, d in enumerate(view_3d.split(',')): d = d.strip() if d.lower() == 'c': d = images.shape[i] // 2 else: try: d = int(d) except: d = images.shape[i] // 2 d = max(min(images.shape[i] - 1, d), 0) l.append(d) view_3d = l def mark_image(image, c1, c2, ln, wd, mx): image[c1 - wd:c1 + wd + 1, :ln] = mx image[c1 - wd:c1 + wd + 1, -ln:] = mx image[:ln, c2 - wd:c2 + wd + 1] = mx image[-ln:, c2 - wd:c2 + wd + 1] = mx return image if view_3d: import scipy.ndimage as ndimage pyplot.rc('font', family='sans-serif', weight='normal', size=8) figure = pyplot.figure(dpi=options.get(dpi=96), figsize=(8, 8), frameon=True, facecolor='1.0', edgecolor='w') yz_scale = voxel_sizes[1] / voxel_sizes[0] zx_scale = voxel_sizes[0] / voxel_sizes[2] yx_scale = voxel_sizes[1] / voxel_sizes[2] yx_image = images[view_3d[0]].copy() zx_image = images[:, view_3d[1], :].copy() yz_image = images[:, :, view_3d[2]].T.copy() image = numpy.zeros((yx_image.shape[0] + zx_image.shape[0] + 1, yx_image.shape[1] + yz_image.shape[1] + 1)) wd = image.shape[0] // 300 ln = max(1, image.shape[1] // 20) mx = yx_image.max() def fix_image(image, scale, c1, c2, ln, wd): mx = image.max() if scale == 1: mark_image(image, c1, c2, ln, wd, mx) elif scale > 1: image = ndimage.interpolation.zoom(image, [scale, 1.0], order=0) mark_image(image, c1 * scale, c2, ln, wd, mx) else: image = ndimage.interpolation.zoom(image, [1.0, 1.0 / scale], order=0) mark_image(image, c1, c2 / scale, ln, wd, mx) return image yx_image = fix_image(yx_image, yx_scale, view_3d[1], view_3d[2], ln, wd) zx_image = fix_image(zx_image, zx_scale, view_3d[0], view_3d[2], ln, wd) yz_image = fix_image(yz_image, yz_scale, view_3d[1], view_3d[0], ln, wd) image = numpy.zeros((yx_image.shape[0] + zx_image.shape[0] + 1, yx_image.shape[1] + yz_image.shape[1] + 1)) image[:yx_image.shape[0], :yx_image.shape[1]] = yx_image image[:yx_image.shape[0], yx_image.shape[1] + 1:] = yz_image image[yx_image.shape[0] + 1:, :yx_image.shape[1]] = zx_image image_plot = pyplot.imshow( image, interpolation=options.get(interpolation='nearest'), cmap=cmap, ) pyplot.title(title, size=11) axes = pyplot.gca() xtickdata = [ (0, '0'), #(yx_image.shape[1]/2, 'X'), (yx_image.shape[1], '%.1fum' % (voxel_sizes[2] * images.shape[2] * 1e6)), ((yx_image.shape[1] + image.shape[1]) / 2, 'Z'), (image.shape[1] - 1, '%.1fum' % (voxel_sizes[0] * images.shape[0] * 1e6)), (yx_image.shape[1] * view_3d[2] / images.shape[2], 'X=%spx' % (view_3d[2])), ] ytickdata = [ (0, '0'), #(yx_image.shape[0]/2, 'Y'), (yx_image.shape[0], '%.1fum' % (voxel_sizes[1] * images.shape[1] * 1e6)), #((yx_image.shape[0]+image.shape[0])/2, 'Z'), (image.shape[0] - 1, '%.1fum' % (voxel_sizes[0] * images.shape[0] * 1e6)), (yx_image.shape[0] * view_3d[1] / images.shape[1], 'Y=%spx' % (view_3d[1])), (yx_image.shape[0] + yz_image.shape[1] * view_3d[0] / images.shape[0], 'Z=%spx' % (view_3d[0])), ] xtickdata.sort() xticks, xticklabels = zip(*xtickdata) ytickdata.sort() yticks, yticklabels = zip(*ytickdata) axes.set_xticks(xticks) axes.set_xticklabels(xticklabels) axes.set_yticks(yticks) axes.set_yticklabels(yticklabels) cbar = pyplot.colorbar(shrink=0.8) def on_keypressed(event): """Callback for key press event.""" key = event.key if key == 'q': sys.exit(0) figure.canvas.mpl_connect('key_press_event', on_keypressed) else: figure, subplot, image = imshow( images, title=title, #miniswhite=page.photometric=='miniswhite', interpolation=options.interpolation, cmap=cmap, dpi=options.dpi, isrgb=options.rgb, show_hist=options.histogram_bins, auto_scale=options.auto_scale) axes = figure.get_axes()[0] axes.set_xlabel(xlabel) axes.set_ylabel(ylabel) output_path = options.get(output_path='') if output_path: pyplot.savefig(output_path) print 'wrote', output_path sys.exit(0) pyplot.show()
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`)
def runner(parser, options, args): smoothness = int(options.smoothness or 1) verbose = options.verbose if options.verbose is not None else True 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] options.output_path = args[1] else: parser.error( "incorrect number of arguments (expected upto 2 but got %s)" % (len(args))) options.input_path = fix_path(options.input_path) stack = ImageStack.load(options.input_path, options=options) voxel_sizes = stack.get_voxel_sizes() new_images = stack.images.copy() background = (stack.pathinfo.get_background() or [0, 0])[0] print 'Image has background', background window_width = options.window_width or None if window_width is None: dr = stack.get_lateral_resolution() dz = stack.get_axial_resolution() if dr is None or dz is None: window_width = 3.0 scales = tuple( [s / (window_width * min(voxel_sizes)) for s in voxel_sizes]) else: print 'lateral resolution: %.3f um (%.1f x %.1f px^2)' % ( 1e6 * dr, dr / voxel_sizes[1], dr / voxel_sizes[2]) print 'axial resolution: %.3f um (%.1fpx)' % (1e6 * dz, dz / voxel_sizes[0]) vz, vy, vx = voxel_sizes m = 3 scales = (m * vz / dz, m * vy / dr, m * vx / dr) window_width = '%.1fx%.1f' % (dz / m / vz, dr / m / vy) else: window_width = options.window_width scales = tuple( [s / (window_width * min(voxel_sizes)) for s in voxel_sizes]) print 'Window size in pixels:', [1 / s for s in scales] apply_window_inplace(new_images, scales, smoothness, background) if options.output_path is None: b, e = os.path.splitext(options.input_path) suffix = '_window%s_%s' % (window_width, smoothness) options.output_path = b + suffix + (e or '.tif') options.output_path = fix_path(options.output_path) if verbose: print 'Leak: %.3f%%' % (100 * (1 - new_images.sum() / stack.images.sum())) print 'MSE:', ((new_images - stack.images)**2).mean() print 'Energy:', ((stack.images)**2).sum() print 'Saving result to', options.output_path ImageStack(new_images, stack.pathinfo).save(options.output_path)
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 options.input_path = fix_path(options.input_path) stack = ImageStack.load(options.input_path, options=options) voxel_sizes = stack.get_voxel_sizes() dr = stack.get_lateral_resolution() dz = stack.get_axial_resolution() if dr is not None: print 'lateral resolution: %.3f um (%.1f x %.1f px^2)' % ( 1e6 * dr, dr / voxel_sizes[1], dr / voxel_sizes[2]) print 'axial resolution: %.3f um (%.1fpx)' % (1e6 * dz, dz / voxel_sizes[0]) vz, vy, vx = voxel_sizes m = 1 scales = (m * vz / dz, m * vy / dr, m * vx / dr) else: raise NotImplementedError('get_lateral_resolution') kdims = [1 + 2 * (int(numpy.ceil(1 / s)) // 2) for s in scales] k = 'x'.join(map(str, kdims)) print 'Averaging window box:', k kernel_type = options.kernel smoothing_method = options.method boundary_condition = options.boundary mn, mx = stack.images.min(), stack.images.max() high_indices = numpy.where(stack.images >= mn + 0.9 * (mx - mn)) high = stack.images[high_indices] from iocbio.ops import regress average, average_grad = regress(stack.images, scales, kernel=kernel_type, method=smoothing_method, boundary=boundary_condition, verbose=True, enable_fft=True) ImageStack(average, pathinfo=stack.pathinfo).save('average.tif') noise = stack.images - average ImageStack(noise - noise.min(), pathinfo=stack.pathinfo).save('noise.tif') bright_level = 0.999 * average.max() + 0.001 * average.min() bright_indices = numpy.where(average >= bright_level) print len(bright_indices[0]) bright_noise = stack.images[bright_indices] - average[bright_indices] a = stack.images[bright_indices].mean() d = stack.images[bright_indices].std() print 'mean=', a, 'std=', d print 'peak SNR=', a / d print 'AVERAGE min, max, mean = %s, %s, %s' % ( average.min(), average.max(), average.mean()) print numpy.histogram(stack.images)[0] sys.exit() noise = stack.images - average var, var_grad = regress(noise * noise, scales, kernel=kernel_type, method=smoothing_method, boundary=boundary_condition, verbose=True, enable_fft=True) print 'VAR min, max, mean = %s, %s, %s' % (var.min(), var.max(), var.mean()) indices = numpy.where(var > 0) print len(numpy.where(var == 0)[0]), var.shape, var.dtype var[numpy.where(var <= 0)] = 1 snr = average / numpy.sqrt(var) snr1 = snr[indices] print 'STACK min, max = %s, %s' % (mn, mx) print 'SNR min, max, mean = %s, %s, %s' % (snr1.min(), snr1.max(), snr1.mean()) ImageStack(average, pathinfo=stack.pathinfo).save('average.tif') ImageStack(snr, pathinfo=stack.pathinfo).save('snr.tif') ImageStack(noise - noise.min(), pathinfo=stack.pathinfo).save('noise.tif')
def runner(parser, options, args): smoothness = int(options.smoothness or 1) verbose = options.verbose if options.verbose is not None else True 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] options.output_path = args[1] else: parser.error("incorrect number of arguments (expected upto 2 but got %s)" % (len(args))) options.input_path = fix_path(options.input_path) stack = ImageStack.load(options.input_path, options=options) voxel_sizes = stack.get_voxel_sizes() new_images = stack.images.copy() background = (stack.pathinfo.get_background() or [0,0])[0] print 'Image has background', background window_width = options.window_width or None if window_width is None: dr = stack.get_lateral_resolution() dz = stack.get_axial_resolution() if dr is None or dz is None: window_width = 3.0 scales = tuple([s/(window_width*min(voxel_sizes)) for s in voxel_sizes]) else: print 'lateral resolution: %.3f um (%.1f x %.1f px^2)' % (1e6*dr, dr/voxel_sizes[1], dr/voxel_sizes[2]) print 'axial resolution: %.3f um (%.1fpx)' % (1e6*dz, dz / voxel_sizes[0]) vz,vy,vx = voxel_sizes m = 3 scales = (m*vz/dz, m*vy/dr, m*vx/dr) window_width = '%.1fx%.1f' % (dz/m/vz, dr/m/vy) else: window_width = options.window_width scales = tuple([s/(window_width*min(voxel_sizes)) for s in voxel_sizes]) print 'Window size in pixels:', [1/s for s in scales] apply_window_inplace (new_images, scales, smoothness, background) if options.output_path is None: b,e = os.path.splitext(options.input_path) suffix = '_window%s_%s' % (window_width, smoothness) options.output_path = b + suffix + (e or '.tif') options.output_path = fix_path(options.output_path) if verbose: print 'Leak: %.3f%%' % ( 100*(1-new_images.sum ()/stack.images.sum ())) print 'MSE:', ((new_images - stack.images)**2).mean() print 'Energy:', ((stack.images)**2).sum() print 'Saving result to',options.output_path ImageStack(new_images, stack.pathinfo).save(options.output_path)
def runner (parser, options, args): options = Options (options) 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] 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) images = stack.images roll_axis = dict(XY=0, XZ=1, YZ=2).get(options.projection, 0) axis_labels = ('Z', 'Y', 'X') #roll_axis = int (options.roll_axis) voxel_sizes = stack.get_voxel_sizes() resolution = ['', '', ''] title = '%s[%s]' % (os.path.basename (options.input_path), images.dtype) dr = stack.get_lateral_resolution() dz = stack.get_axial_resolution() if voxel_sizes: if dr is not None: resolution[1] = tostr(dr/voxel_sizes[1]) + 'px' resolution[2] = tostr(dr/voxel_sizes[2]) + 'px' if dz is not None: resolution[0] = tostr(dz/voxel_sizes[0]) + 'px' resolution = tuple (resolution) if roll_axis: images = numpy.rollaxis(images, roll_axis) if voxel_sizes: voxel_sizes = (voxel_sizes[roll_axis],) + voxel_sizes[:roll_axis] + voxel_sizes[roll_axis+1:] axis_labels = (axis_labels[roll_axis],) + axis_labels[:roll_axis] + axis_labels[roll_axis+1:] resolutions = (resolution[roll_axis],) + resolution[:roll_axis] + resolution[roll_axis+1:] if voxel_sizes: xlabel = '%s, resol=%s, px size=%sum, size=%sum' \ % (axis_labels[-1], resolution[-1], tostr(voxel_sizes[-1]*1e6), tostr(voxel_sizes[-1]*1e6*images.shape[-1])) ylabel = '%s, resol=%s, px size=%sum, size=%sum' \ % (axis_labels[-2], resolution[-2], tostr(voxel_sizes[-2]*1e6), tostr(voxel_sizes[-2]*1e6*images.shape[-2])) else: xlabel = '%s' % (axis_labels[-1]) ylabel = '%s' % (axis_labels[-2]) import matplotlib.cm as cm import matplotlib.pyplot as pyplot from iocbio.io.tifffile import imshow if options.invert_cmap: cmap = getattr(cm, options.cmap+'_r', options.cmap) else: cmap = getattr(cm, options.cmap, 'gray') view_3d = options.get(view_3d = 'none') if view_3d and view_3d.lower()=='none': view_3d = None if view_3d: l = [] for i,d in enumerate(view_3d.split (',')): d = d.strip() if d.lower()=='c': d = images.shape[i]//2 else: try: d = int(d) except: d = images.shape[i]//2 d = max(min(images.shape[i]-1, d), 0) l.append(d) view_3d = l def mark_image (image, c1, c2, ln, wd, mx): image[c1-wd:c1+wd+1,:ln] = mx image[c1-wd:c1+wd+1,-ln:] = mx image[:ln,c2-wd:c2+wd+1] = mx image[-ln:,c2-wd:c2+wd+1] = mx return image if view_3d: import scipy.ndimage as ndimage pyplot.rc('font', family='sans-serif', weight='normal', size=8) figure = pyplot.figure(dpi=options.get(dpi=96), figsize=(8, 8), frameon=True, facecolor='1.0', edgecolor='w') yz_scale = voxel_sizes[1] / voxel_sizes[0] zx_scale = voxel_sizes[0] / voxel_sizes[2] yx_scale = voxel_sizes[1] / voxel_sizes[2] yx_image = images[view_3d[0]].copy () zx_image = images[:,view_3d[1],:].copy () yz_image = images[:,:,view_3d[2]].T.copy() image = numpy.zeros((yx_image.shape[0] + zx_image.shape[0]+1, yx_image.shape[1]+yz_image.shape[1]+1)) wd = image.shape[0]//300 ln = max(1, image.shape[1]//20) mx = yx_image.max() def fix_image (image, scale, c1, c2, ln, wd): mx = image.max() if scale==1: mark_image (image, c1, c2, ln, wd, mx) elif scale>1: image = ndimage.interpolation.zoom(image, [scale, 1.0], order=0) mark_image (image, c1*scale, c2, ln, wd, mx) else: image = ndimage.interpolation.zoom(image, [1.0, 1.0/scale], order=0) mark_image (image, c1, c2/scale, ln, wd, mx) return image yx_image = fix_image(yx_image, yx_scale, view_3d[1], view_3d[2], ln, wd) zx_image = fix_image(zx_image, zx_scale, view_3d[0], view_3d[2], ln, wd) yz_image = fix_image(yz_image, yz_scale, view_3d[1], view_3d[0], ln, wd) image = numpy.zeros((yx_image.shape[0] + zx_image.shape[0]+1, yx_image.shape[1]+yz_image.shape[1]+1)) image[:yx_image.shape[0], :yx_image.shape[1]] = yx_image image[:yx_image.shape[0], yx_image.shape[1]+1:] = yz_image image[yx_image.shape[0]+1:, :yx_image.shape[1]] = zx_image image_plot = pyplot.imshow(image, interpolation=options.get(interpolation='nearest'), cmap = cmap, ) pyplot.title(title, size=11) axes = pyplot.gca() xtickdata = [(0,'0'), #(yx_image.shape[1]/2, 'X'), (yx_image.shape[1], '%.1fum' % (voxel_sizes[2]*images.shape[2]*1e6)), ( (yx_image.shape[1]+image.shape[1])/2, 'Z'), (image.shape[1]-1, '%.1fum' % (voxel_sizes[0]*images.shape[0]*1e6)), (yx_image.shape[1]*view_3d[2]/images.shape[2],'X=%spx' % (view_3d[2])), ] ytickdata = [(0,'0'), #(yx_image.shape[0]/2, 'Y'), (yx_image.shape[0], '%.1fum' % (voxel_sizes[1]*images.shape[1]*1e6)), #((yx_image.shape[0]+image.shape[0])/2, 'Z'), ( image.shape[0]-1, '%.1fum' % (voxel_sizes[0]*images.shape[0]*1e6)), (yx_image.shape[0]*view_3d[1]/images.shape[1],'Y=%spx' % (view_3d[1])), (yx_image.shape[0]+yz_image.shape[1]*view_3d[0]/images.shape[0], 'Z=%spx' % (view_3d[0])), ] xtickdata.sort () xticks, xticklabels = zip(*xtickdata) ytickdata.sort () yticks, yticklabels = zip(*ytickdata) axes.set_xticks(xticks) axes.set_xticklabels (xticklabels) axes.set_yticks(yticks) axes.set_yticklabels (yticklabels) cbar = pyplot.colorbar(shrink=0.8) def on_keypressed(event): """Callback for key press event.""" key = event.key if key == 'q': sys.exit(0) figure.canvas.mpl_connect('key_press_event', on_keypressed) else: figure, subplot, image = imshow(images, title = title, #miniswhite=page.photometric=='miniswhite', interpolation=options.interpolation, cmap=cmap, dpi=options.dpi, isrgb=options.rgb, show_hist = options.histogram_bins, auto_scale = options.auto_scale) axes = figure.get_axes()[0] axes.set_xlabel(xlabel) axes.set_ylabel(ylabel) output_path = options.get (output_path='') if output_path: pyplot.savefig (output_path) print 'wrote',output_path sys.exit(0) pyplot.show()
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): verbose = options.verbose if options.verbose is not None else True 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))) options.input_path = fix_path(options.input_path) kernel_width = options.kernel_width or None kernel_type = options.kernel smoothing_method = options.method boundary_condition = options.boundary stack = ImageStack.load(options.input_path, options=options) voxel_sizes = stack.get_voxel_sizes() if kernel_width is None: dr = stack.get_lateral_resolution() dz = stack.get_axial_resolution() if dr is None or dz is None: kernel_width = 3 else: print 'lateral resolution: %.3f um (%.1f x %.1f px^2)' % (1e6*dr, dr/voxel_sizes[1], dr/voxel_sizes[2]) print 'axial resolution: %.3f um (%.1fpx)' % (1e6*dz, dz / voxel_sizes[0]) vz,vy,vx = voxel_sizes m = 1 scales = (m*vz/dz, m*vy/dr, m*vx/dr) if kernel_width is not None: w = float(kernel_width) * min (voxel_sizes) scales = tuple([s/w for s in voxel_sizes]) print 'Window sizes:', [1/s for s in scales] kdims = [1+2*(int(numpy.ceil(1/s))//2) for s in scales] k = 'x'.join (map (str, kdims)) if options.output_path is None: b,e = os.path.splitext(options.input_path) suffix = '_%s_%s%s' % (smoothing_method, kernel_type, k) options.output_path = b + suffix + (e or '.tif') options.output_path = fix_path(options.output_path) if options.link_function == 'identity': images = stack.images elif options.link_function == 'log': images = stack.images mn, mx = get_dtype_min_max (images.dtype) images = numpy.log(images) images[numpy.where (numpy.isnan(images))] = numpy.log(mn) else: raise NotImplementedError (`options.link_function`) new_images, new_images_grad = regress (images, scales, kernel = kernel_type, method = smoothing_method, boundary = boundary_condition, verbose = verbose) if options.link_function == 'identity': pass elif options.link_function == 'log': new_images = numpy.exp(new_images) new_images = numpy.nan_to_num(new_images) else: raise NotImplementedError (`options.link_function`) if verbose: print 'Leak: %.3f%%' % ( 100*(1-new_images.sum()/stack.images.sum()) ) print 'MSE:', ((new_images - stack.images)**2).mean() print 'Energy:', ((stack.images)**2).sum() print 'Saving result to',options.output_path ImageStack(new_images, stack.pathinfo).save(options.output_path)