示例#1
0
文件: convolve.py 项目: raj347/iocbio
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)
示例#2
0
文件: regress.py 项目: raj347/iocbio
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)
示例#3
0
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 `)
示例#4
0
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()
示例#5
0
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')
示例#6
0
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`)
示例#7
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)