示例#1
0
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)        
示例#2
0
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)
示例#3
0
def deconvolve(psf, stack, working_dir=None, data_type=None, options=None):
    """Deconvolve stack of images against given PSF.

    Parameters
    ----------
    psf : `iocbio.io.image_stack.ImageStack`
      PSF
    stack : `iocbio.io.image_stack.ImageStack`
      Scanned images.
    working_dir : {None, str}
      Directory name where to save intermediate results.
      When None then a temporary directory will be created.
    data_type : {None, str}
      Desired data type of deconvolution estimate to be returned.
    options : {None, `iocbio.utils.Options`}
      See :ref:`iocbio-deconvolve` for information about options.
      The following options attributes are used: 
      float_type, apply_window, rltv_algorithm_type, degrade_data,
      first_estimate, rltv_stop_tau, save_intermediate_results

    Returns
    -------
    estimate : `iocbio.io.image_stack.ImageStack`
      Last deconvolution estimate. See ``working_dir`` for other estimates.

    See also
    --------
    iocbio.microscope.deconvolution
    """
    if VERBOSE > 9:
        print 'Entering %s.deconvolve' % (__file__)
    options = Options(options)
    if working_dir is None:
        import tempfile
        working_dir = tempfile.mkdtemp('-iocbio.deconvolve')

    if data_type is None:
        data_type = stack.images.dtype

    dtype = float2dtype(options.get(float_type='single'))

    phf0, data = get_coherent_images(psf, stack, dtype)

    if options.get(apply_window=False):
        background = (stack.pathinfo.get_background() or [0, 0])[0]
        voxel_sizes = stack.get_voxel_sizes()
        smoothness = int(options.get(smoothness=1))
        window_width = options.get(window_width=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 = 0.3
                scales = (m * vz / dz, m * vy / dr, m * vx / dr)
        else:
            scales = tuple(
                [s / (window_width * min(voxel_sizes)) for s in voxel_sizes])
        print 'Window size in pixels:', [1 / s for s in scales]
        from iocbio.ops.apply_window_ext import apply_window_inplace
        apply_window_inplace(data, scales, smoothness, background)

    mode = options.get(rltv_algorithm_type='multiplicative').lower()
    Cls = dict(multiplicative=DeconvolveRLPoisson,
               additive=DeconvolveRLGauss)[mode]

    task = Cls(phf0, data, stack.get_voxel_sizes(), options)

    task.set_cache_dir(working_dir)
    task.set_save_data(stack.pathinfo, data.shape, data_type)
    task.set_test_data()

    estimate = task.deconvolve()

    estimate = contract_to_shape(estimate, stack.images.shape, data_type)

    return ImageStack(estimate,
                      stack.pathinfo,
                      suffix=task.get_suffix(),
                      options=options)
示例#4
0
def deconvolve(psf, stack, working_dir = None, data_type = None,
               options = None):
    """Deconvolve stack of images against given PSF.

    Parameters
    ----------
    psf : `iocbio.io.image_stack.ImageStack`
      PSF
    stack : `iocbio.io.image_stack.ImageStack`
      Scanned images.
    working_dir : {None, str}
      Directory name where to save intermediate results.
      When None then a temporary directory will be created.
    data_type : {None, str}
      Desired data type of deconvolution estimate to be returned.
    options : {None, `iocbio.utils.Options`}
      See :ref:`iocbio-deconvolve` for information about options.
      The following options attributes are used: 
      float_type, apply_window, rltv_algorithm_type, degrade_data,
      first_estimate, rltv_stop_tau, save_intermediate_results

    Returns
    -------
    estimate : `iocbio.io.image_stack.ImageStack`
      Last deconvolution estimate. See ``working_dir`` for other estimates.

    See also
    --------
    iocbio.microscope.deconvolution
    """
    if VERBOSE>9:
        print 'Entering %s.deconvolve' % (__file__)
    options = Options(options)
    if working_dir is None:
        import tempfile
        working_dir = tempfile.mkdtemp('-iocbio.deconvolve')

    if data_type is None:
        data_type = stack.images.dtype

    dtype = float2dtype(options.get(float_type='single'))

    phf0, data = get_coherent_images(psf, stack, dtype)

    if options.get(apply_window=False):
        background = (stack.pathinfo.get_background() or [0,0])[0]
        voxel_sizes = stack.get_voxel_sizes()
        smoothness = int(options.get(smoothness=1))
        window_width = options.get(window_width=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 = 0.3
                scales = (m*vz/dz, m*vy/dr, m*vx/dr)
        else:
            scales = tuple([s/(window_width*min(voxel_sizes)) for s in voxel_sizes])
        print 'Window size in pixels:', [1/s for s in scales]
        from iocbio.ops.apply_window_ext import apply_window_inplace
        apply_window_inplace(data, scales, smoothness, background)


    mode = options.get(rltv_algorithm_type='multiplicative').lower()
    Cls = dict(multiplicative=DeconvolveRLPoisson,
               additive = DeconvolveRLGauss)[mode]

    task = Cls(phf0, data, stack.get_voxel_sizes(), options)

    task.set_cache_dir(working_dir)
    task.set_save_data(stack.pathinfo, data.shape, data_type)
    task.set_test_data()

    estimate = task.deconvolve()

    estimate = contract_to_shape(estimate, 
                                 stack.images.shape, 
                                 data_type)

    return ImageStack(estimate, stack.pathinfo, suffix=task.get_suffix(),
                      options = options)