Exemplo n.º 1
0
def readDataFiles(filename, x=all, y=all, z=all, **args):
    """Read data from individual images assuming they are the z slices

    Arguments:
        filename (str): file name as regular expression
        x,y,z (tuple): data range specifications
    
    Returns:
        array: image data
    """

    fpath, fl = readFileList(filename)
    nz = len(fl)

    # read first image to get data size and type
    rz = io.toDataRange(nz, r=z)
    sz = io.toDataSize(nz, r=z)
    fn = os.path.join(fpath, fl[rz[0]])
    img = io.readData(fn, x=x, y=y)
    nxy = img.shape
    data = numpy.zeros(nxy + (sz,), dtype=img.dtype)
    data[:, :, 0] = img

    for i in range(rz[0] + 1, rz[1]):
        fn = os.path.join(fpath, fl[i])
        data[:, :, i - rz[0]] = io.readData(fn, x=x, y=y)

    return data
Exemplo n.º 2
0
def readDataFiles(filename, x=all, y=all, z=all, **args):
    """Read data from individual images assuming they are the z slices

    Arguments:
        filename (str): file name as regular expression
        x,y,z (tuple): data range specifications
    
    Returns:
        array: image data
    """

    fpath, fl = readFileList(filename)
    nz = len(fl)

    #read first image to get data size and type
    rz = io.toDataRange(nz, r=z)
    sz = io.toDataSize(nz, r=z)
    fn = os.path.join(fpath, fl[rz[0]])
    img = io.readData(fn, x=x, y=y)
    nxy = img.shape
    data = numpy.zeros(nxy + (sz, ), dtype=img.dtype)
    data[:, :, 0] = img

    for i in range(rz[0] + 1, rz[1]):
        fn = os.path.join(fpath, fl[i])
        data[:, :, i - rz[0]] = io.readData(fn, x=x, y=y)

    return data
Exemplo n.º 3
0
def overlayLabel(dataSource,
                 labelSource,
                 sink=None,
                 alpha=False,
                 labelColorMap='jet',
                 x=all,
                 y=all,
                 z=all):
    """Overlay a gray scale image with colored labeled image
    
    Arguments:
        dataSouce (str or array): volumetric image data
        labelSource (str or array): labeled image to be overlayed on the image data
        sink (str or None): destination for the overlayed image
        alpha (float or False): transparency
        labelColorMap (str or object): color map for the labels
        x, y, z (all or tuple): sub-range specification
    
    Returns:
        (array or str): figure handle
        
    See Also:
        :func:`overlayPoints`
    """

    label = io.readData(labelSource, x=x, y=y, z=z)
    image = io.readData(dataSource, x=x, y=y, z=z)

    lmax = labelSource.max()

    if lmax <= 1:
        carray = numpy.array([[1, 0, 0, 1]])
    else:
        cm = mpl.cm.get_cmap(labelColorMap)
        cNorm = mpl.colors.Normalize(vmin=1, vmax=int(lmax))
        carray = mpl.cm.ScalarMappable(norm=cNorm, cmap=cm)
        carray = carray.to_rgba(numpy.arange(1, int(lmax + 1)))

    if alpha == False:
        carray = numpy.concatenate(([[0, 0, 0, 1]], carray), axis=0)
    else:
        carray = numpy.concatenate(([[1, 1, 1, 1]], carray), axis=0)

    cm = mpl.colors.ListedColormap(carray)
    carray = cm(label)
    carray = carray.take([0, 1, 2], axis=-1)

    if alpha == False:
        cimage = (label == 0) * image
        cimage = numpy.repeat(cimage, 3)
        cimage = cimage.reshape(image.shape + (3, ))
        cimage = cimage.astype(carray.dtype)
        cimage += carray
    else:
        cimage = numpy.repeat(image, 3)
        cimage = cimage.reshape(image.shape + (3, ))
        cimage = cimage.astype(carray.dtype)
        cimage *= carray

    return io.writeData(sink, cimage)
Exemplo n.º 4
0
def overlayLabel(dataSource, labelSource, sink = None,  alpha = False, labelColorMap = 'jet', x = all, y = all, z = all):
    """Overlay a gray scale image with colored labeled image
    
    Arguments:
        dataSouce (str or array): volumetric image data
        labelSource (str or array): labeled image to be overlayed on the image data
        sink (str or None): destination for the overlayed image
        alpha (float or False): transparency
        labelColorMap (str or object): color map for the labels
        x, y, z (all or tuple): sub-range specification
    
    Returns:
        (array or str): figure handle
        
    See Also:
        :func:`overlayPoints`
    """ 
    
    label = io.readData(labelSource, x= x, y = y, z = z);
    image = io.readData(dataSource, x= x, y = y, z = z);
    
    lmax = label.max();
    
    if lmax <= 1:
        carray = numpy.array([[1,0,0,1]]);
    else:
        cm = mpl.cm.get_cmap(labelColorMap);
        cNorm  = mpl.colors.Normalize(vmin=1, vmax = int(lmax));
        carray = mpl.cm.ScalarMappable(norm=cNorm, cmap=cm);
        carray = carray.to_rgba(numpy.arange(1, int(lmax + 1)));

    if alpha == False:
        carray = numpy.concatenate(([[0,0,0,1]], carray), axis = 0);
    else:
        carray = numpy.concatenate(([[1,1,1,1]], carray), axis = 0);
        
    cm = mpl.colors.ListedColormap(carray);
    carray = cm(label);
    carray = carray.take([0,1,2], axis = -1);

    if alpha == False:
        cimage = (label == 0) * image;
        cimage = numpy.repeat(cimage, 3);
        cimage = cimage.reshape(image.shape + (3,)); 
        cimage = cimage.astype(carray.dtype);
        cimage += carray;
    else:
        cimage = numpy.repeat(image, 3);
        cimage = cimage.reshape(image.shape + (3,));
        cimage = cimage.astype(carray.dtype);
        cimage *= carray;

    return io.writeData(sink, cimage);
Exemplo n.º 5
0
def _processSubStack(dsr):
    """Helper to process stack in parallel"""

    sf  = dsr[0];
    pp  = dsr[1];
    sub = dsr[2];
    verbose = dsr[3];

    timer = Timer();
    pw = ProcessWriter(sub["stackId"]);
    
    if verbose:
        pw.write("processing substack " + str(sub["stackId"]) + "/" + str(sub["nStacks"]));
        pw.write("file          = " + sub["source"]);
        pw.write("segmentation  = " + str(sf));
        pw.write("ranges: x,y,z = " + str(sub["x"]) +  "," + str(sub["y"]) + "," + str(sub["z"])); 
    
    img = io.readData(sub["source"], x = sub["x"], y = sub["y"], z = sub["z"]);
    
    if verbose:
        pw.write(timer.elapsedTime(head = 'Reading data of size ' + str(img.shape)));
    
    timer.reset();
    seg = sf(img, subStack = sub, out = pw, **pp);    

    if verbose:    
        pw.write(timer.elapsedTime(head = 'Processing substack of size ' + str(img.shape)));
    
    return seg;
Exemplo n.º 6
0
def test():
    """Test Spot Detection Module"""
    import os
    import ClearMap.ImageProcessing.SpotDetection as self
    reload(self)
    import ClearMap.IO as io
    import ClearMap.Settings as settings
    from ClearMap.ImageProcessing.CellDetection import detectCells
    basedir = settings.ClearMapPath
    #Default tifs do not load for some reason (tifffile is not happy) but
    #simply resaving them seems to do the trick.
    fn = os.path.join(basedir, 'Test/Data/synthetic2/test_iDISCO_\d{3}.tif')
    #fn = os.path.join(basedir, 'Test/Data/OME/16-17-27_0_8X-s3-20HF_UltraII_C00_xyz-Table Z\d{4}.ome.tif');
    img = io.readData(fn)
    #img = dataset[0:500,0:500,1000:1008];
    #img = dataset[600:1000,1600:1800,800:830];
    #img = dataset[500:1500,500:1500,800:809];
    img = img.astype('int16')
    #m = sys.modules['iDISCO.ImageProcessing.SpotDetection']
    #c = self.detectCells(img);
    c = self.detectSpots(img, hMax=10, threshold=100, verbose=True)
    print 'done, found %d cells !' % c[0].shape[0]

    #test intensities:
    import numpy
    x = numpy.random.rand(30, 30, 10)
    centers = numpy.array([[0, 0, 0], [29, 29, 9]])
    i = self.findIntensity(x, centers, boxSize=(1, 1, 1))
    print i
Exemplo n.º 7
0
def makeColorAnnotations(filename, labeledImage=None):
    if labeledImage is None:
        labeledImage = DefaultLabeledImageFile

    li = io.readData(labeledImage)
    dsize = li.shape

    lr = numpy.zeros(dsize, dtype=numpy.uint8)
    lg = lr.copy()
    lb = lr.copy()

    global Label
    maxlabel = max(Label.ids)
    colarray = numpy.zeros((maxlabel, 3))
    for i in Label.ids:
        colarray[i - 1, :] = Label.color(i)

    for i in Label.ids:
        ll = li == i
        lr[ll] = colarray[i - 1, 0]
        lg[ll] = colarray[i - 1, 1]
        lb[ll] = colarray[i - 1, 2]

    io.writeData(filename + "_r.tif", lr)
    io.writeData(filename + "_g.tif", lg)
    io.writeData(filename + "_b.tif", lb)

    return (lr, lg, lb)
Exemplo n.º 8
0
def labelPoints(points, labeledImage = DefaultLabeledImageFile, level = None, collapse = None):
    
    #points are (y,x,z) -> which is also the way the labeled image is read in
    #x = points[:,1];
    #y = points[:,0];
    #z = points[:,2];

    x = points[:,0];
    y = points[:,1];
    z = points[:,2]; 
    
    nPoint = x.size;    
    
    pointLabels = numpy.zeros(nPoint, 'int32');

    labelImage = io.readData(labeledImage);    
    dsize = labelImage.shape;

    for i in range(nPoint):
        #if y[i] >= 0 and y[i] < dsize[0] and x[i] >= 0 and x[i] < dsize[1] and z[i] >= 0 and z[i] < dsize[2]:
        #     pointLabels[i] = labelImage[y[i], x[i], z[i]];
        if x[i] >= 0 and x[i] < dsize[0] and y[i] >= 0 and y[i] < dsize[1] and z[i] >= 0 and z[i] < dsize[2]:
            pointLabels[i] = labelImage[int(x[i]), int(y[i]), int(z[i])];
    
    if collapse is None:
        pointLabels = labelAtLevel(pointLabels, level);
    else:
        pointLabels = labelAtCollapse(pointLabels);
    
    return pointLabels;
Exemplo n.º 9
0
def makeColorAnnotations(filename, labeledImage = None):
    if labeledImage is None:
        labeledImage = DefaultLabeledImageFile;
    
    li = io.readData(labeledImage);
    dsize = li.shape;
    
    lr = numpy.zeros(dsize, dtype = numpy.uint8);
    lg = lr.copy();
    lb = lr.copy();
    
    global Label; 
    maxlabel = max(Label.ids);
    colarray = numpy.zeros((maxlabel, 3));
    for i in Label.ids:
        colarray[i-1,:] = Label.color(i);
    
    for i in Label.ids:
        ll = li == i;
        lr[ll] = colarray[i-1,0];
        lg[ll] = colarray[i-1,1];
        lb[ll] = colarray[i-1,2];
    
    io.writeData(filename + "_r.tif", lr);
    io.writeData(filename + "_g.tif", lg);  
    io.writeData(filename + "_b.tif", lb);
    
    return (lr,lg,lb);
def _processSubStack(dsr):
    """Helper to process stack in parallel"""

    sf  = dsr[0];
    pp  = dsr[1];
    sub = dsr[2];
    verbose = dsr[3];

    timer = Timer();
    pw = ProcessWriter(sub["stackId"]);
    
    if verbose:
        pw.write("processing substack " + str(sub["stackId"]) + "/" + str(sub["nStacks"]));
        pw.write("file          = " + sub["source"]);
        pw.write("segmentation  = " + str(sf));
        pw.write("ranges: x,y,z = " + str(sub["x"]) +  "," + str(sub["y"]) + "," + str(sub["z"])); 
    
    img = io.readData(sub["source"], x = sub["x"], y = sub["y"], z = sub["z"]);
    
    if verbose:
        pw.write(timer.elapsedTime(head = 'Reading data of size ' + str(img.shape)));
    
    timer.reset();
    seg = sf(img, subStack = sub, out = pw, **pp);    

    if verbose:    
        pw.write(timer.elapsedTime(head = 'Processing substack of size ' + str(img.shape)));
    
    return seg;
Exemplo n.º 11
0
def makeColorAnnotations(filename, labeledImage=None):
    if labeledImage is None:
        labeledImage = DefaultLabeledImageFile

    li = io.readData(labeledImage)
    dsize = li.shape

    lr = numpy.zeros(dsize, dtype=numpy.uint8)
    lg = lr.copy()
    lb = lr.copy()

    global Label
    maxlabel = max(Label.ids)
    colarray = numpy.zeros((maxlabel, 3))
    for i in Label.ids:
        colarray[i - 1, :] = Label.color(i)

    for i in Label.ids:
        ll = li == i
        lr[ll] = colarray[i - 1, 0]
        lg[ll] = colarray[i - 1, 1]
        lb[ll] = colarray[i - 1, 2]

    io.writeData(filename + "_r.tif", lr)
    io.writeData(filename + "_g.tif", lg)
    io.writeData(filename + "_b.tif", lb)

    return (lr, lg, lb)
Exemplo n.º 12
0
def labelPoints(points, labeledImage=DefaultLabeledImageFile, level=None, collapse=None):

    # points are (y,x,z) -> which is also the way the labeled image is read in
    # x = points[:,1];
    # y = points[:,0];
    # z = points[:,2];

    x = points[:, 0]
    y = points[:, 1]
    z = points[:, 2]

    nPoint = x.size

    pointLabels = numpy.zeros(nPoint, "int32")

    labelImage = io.readData(labeledImage)
    dsize = labelImage.shape

    for i in range(nPoint):
        # if y[i] >= 0 and y[i] < dsize[0] and x[i] >= 0 and x[i] < dsize[1] and z[i] >= 0 and z[i] < dsize[2]:
        #     pointLabels[i] = labelImage[y[i], x[i], z[i]];
        if x[i] >= 0 and x[i] < dsize[0] and y[i] >= 0 and y[i] < dsize[1] and z[i] >= 0 and z[i] < dsize[2]:
            pointLabels[i] = labelImage[int(x[i]), int(y[i]), int(z[i])]

    if collapse is None:
        pointLabels = labelAtLevel(pointLabels, level)
    else:
        pointLabels = labelAtCollapse(pointLabels)

    return pointLabels
Exemplo n.º 13
0
def removeBackground(img, removeBackgroundParameter = None, size = None, save = None, verbose = False,
                     subStack = None, out = sys.stdout, **parameter):
    """Remove background via subtracting a morphological opening from the original image 
    
    Background removal is done z-slice by z-slice.
    
    Arguments:
        img (array): image data
        removeBackGroundParameter (dict):
            ========= ==================== ===========================================================
            Name      Type                 Descritption
            ========= ==================== ===========================================================
            *size*    (tuple or None)      size for the structure element of the morphological opening
                                           if None, do not correct for any background
            *save*    (str or None)        file name to save result of this operation
                                           if None dont save to file
            *verbose* (bool or int)        print / plot information about this step                                 
            ========= ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
        
    Returns:
        array: background corrected image
    """
    
    size = getParameter(removeBackgroundParameter, "size", size);
    save = getParameter(removeBackgroundParameter, "save", save);    
    verbose = getParameter(removeBackgroundParameter, "verbose", verbose);   
    
    if verbose:
        writeParameter(out = out, head = 'Background Removal:', size = size, save = save);    
    
    if size is None:    
        return img;
        
    img = io.readData(img);
    
    timer = Timer();
    # background subtraction in each slice
    se = structureElement('Disk', size).astype('uint8');
    for z in range(img.shape[2]):
         #img[:,:,z] = img[:,:,z] - grey_opening(img[:,:,z], structure = structureElement('Disk', (30,30)));
         #img[:,:,z] = img[:,:,z] - morph.grey_opening(img[:,:,z], structure = self.structureELement('Disk', (150,150)));
         img[:,:,z] = img[:,:,z] - cv2.morphologyEx(img[:,:,z], cv2.MORPH_OPEN, se)
    
    if not save is None:
        writeSubStack(save, img, subStack = subStack)

    if verbose > 1:
        plotTiling(10*img);

    if verbose:
        out.write(timer.elapsedTime(head = 'Background') + '\n');
    
    return img
Exemplo n.º 14
0
def overlayPoints(dataSource,
                  pointSource,
                  sink=None,
                  pointColor=[1, 0, 0],
                  x=all,
                  y=all,
                  z=all):
    """Overlay points on 3D data and return as color image
    
    Arguments:
        dataSouce (str or array): volumetric image data
        pointSource (str or array): point data to be overlayed on the image data
        pointColor (array): RGB color for the overlayed points
        x, y, z (all or tuple): sub-range specification
    
    Returns:
        (str or array): image overlayed with points
        
    See Also:
        :func:`overlayLabel`
    """
    data = io.readData(dataSource, x=x, y=y, z=z)
    points = io.readPoints(pointSource, x=x, y=y, z=z, shift=True)
    #print data.shape

    if not pointColor is None:
        dmax = data.max()
        dmin = data.min()
        if dmin == dmax:
            dmax = dmin + 1
        cimage = numpy.repeat((data - dmin) / (dmax - dmin), 3)
        cimage = cimage.reshape(data.shape + (3, ))

        if data.ndim == 2:
            for p in points:  # faster version using voxelize ?
                cimage[p[0], p[1], :] = pointColor
        elif data.ndim == 3:
            for p in points:  # faster version using voxelize ?
                cimage[p[0], p[1], p[2], :] = pointColor
        else:
            raise RuntimeError(
                'overlayPoints: data dimension %d not suported' % data.ndim)

    else:
        cimage = vox.voxelize(points, data.shape, voxelizationMethod='Pixel')
        cimage = cimage.astype(data.dtype) * data.max()
        data.shape = data.shape + (1, )
        cimage.shape = cimage.shape + (1, )
        cimage = numpy.concatenate((data, cimage), axis=3)

    #print cimage.shape
    return io.writeData(sink, cimage)
Exemplo n.º 15
0
def readData(filename, **args):
    """Read image stack from single or multiple images
    
    Arguments:
        filename (str): file name as regular expression
        x,y,z (tuple): data range specifications
    
    Returns:
        array: image data
    """

    if os.path.exists(filename):
        return io.readData(filename, **args)
    else:
        return readDataFiles(filename, **args)
Exemplo n.º 16
0
def readData(filename, **args):
    """Read image stack from single or multiple images
    
    Arguments:
        filename (str): file name as regular expression
        x,y,z (tuple): data range specifications
    
    Returns:
        array: image data
    """

    if os.path.exists(filename):
        return io.readData(filename, **args)
    else:
        return readDataFiles(filename, **args)
Exemplo n.º 17
0
def readImportPreview(importResult):
    """Returns the prestiched preview file to check orientations and coarse alignment
  
  Arguments:
    importResult (str): the base directory of the image data or the import xml file
  
  Returns:
    array: preview image
  """

    baseDirectory = baseDirectoryFromXMLImport(importResult)
    if baseDirectory is None:
        baseDirectory = importResult

    fn = os.path.join(baseDirectory, "test_middle_slice.tif")
    return io.readData(fn)
Exemplo n.º 18
0
def _cropParallel(arg):
    """Cropping helper function to use for parallel cropping of image slices"""

    fileSource = arg[0]
    fileSink = arg[1]
    x = arg[2]
    y = arg[3]
    ii = arg[4]
    nn = arg[5]

    if ii is not None:
        pw = ProcessWriter(ii)
        pw.write("cropData: corpping image %d / %d" % (ii, nn))
        # pw.write('%s -> %s' % (fileSource, fileSink));

    data = io.readData(fileSource, x=x, y=y)
    io.writeData(fileSink, data)
Exemplo n.º 19
0
def test():
    """Test Elastix module"""
    import ClearMap.Alignment.Elastix as self
    reload(self)
    
    from ClearMap.Settings import ClearMapPath;
    import os, numpy
    
    p = ClearMapPath;
    
    resultdir = os.path.join(p, 'Test/Elastix/Output');
    
    print 'Searching for transformation parameter file in ' + resultdir;
    pf = self.getTransformParameterFile(resultdir)
      
    print 'Found: ' + pf;
    
    
    #replace path in trasform parameter files:
    self.setPathTransformParameterFiles(resultdir)
    
    #initialize
    self.initializeElastix('/home/ckirst/programs/elastix')
    self.printSettings()

    #transform points
    pts = numpy.random.rand(5,3);    
     
    print 'Transforming points: '
    tpts = self.transformPoints(pts, transformParameterFile = pf, indices = False);
    print pts
    print 'Transformed points: '
    print tpts
    
    
    #deformation and distance fields     
    df = self.deformationField(transformParameterFile = pf, resultDirectory = None);
    #df = '/tmp/elastix_output/deformationField.mhd';

    import ClearMap.IO as io
    data = io.readData('/tmp/elastix_output/deformationField.mhd');
    
    ds = self.deformationDistance(data);
    
    io.writeData(os.path.join(p, 'Test/Elastix/Output/distances.raw'), ds);
Exemplo n.º 20
0
def test():
    """Test Elastix module"""
    import ClearMap.Alignment.Elastix as self
    reload(self)
    
    from ClearMap.Settings import ClearMapPath;
    import os, numpy
    
    p = ClearMapPath;
    
    resultdir = os.path.join(p, 'Test/Elastix/Output');
    
    print 'Searching for transformation parameter file in ' + resultdir;
    pf = self.getTransformParameterFile(resultdir)
      
    print 'Found: ' + pf;
    
    
    #replace path in trasform parameter files:
    self.setPathTransformParameterFiles(resultdir)
    
    #initialize
    self.initializeElastix('/home/ckirst/programs/elastix')
    self.printSettings()

    #transform points
    pts = numpy.random.rand(5,3);    
     
    print 'Transforming points: '
    tpts = self.transformPoints(pts, transformParameterFile = pf, indices = False);
    print pts
    print 'Transformed points: '
    print tpts
    
    
    #deformation and distance fields     
    df = self.deformationField(transformParameterFile = pf, resultDirectory = None);
    #df = '/tmp/elastix_output/deformationField.mhd';

    import ClearMap.IO as io
    data = io.readData('/tmp/elastix_output/deformationField.mhd');
    
    ds = self.deformationDistance(data);
    
    io.writeData(os.path.join(p, 'Test/Elastix/Output/distances.raw'), ds);
Exemplo n.º 21
0
def readDataGroup(filenames, combine = True, **args):
    """Turn a list of filenames for data into a numpy stack"""
    
    #check if stack already:
    if isinstance(filenames, numpy.ndarray):
        return filenames;
    
    #read the individual files
    group = [];
    for f in filenames:
        data = io.readData(f, **args);
        data = numpy.reshape(data, (1,) + data.shape);
        group.append(data);
    
    if combine:
        return numpy.vstack(group);
    else:
        return group;
Exemplo n.º 22
0
def readDataGroup(filenames, combine=True, **args):
    """Turn a list of filenames for data into a numpy stack"""

    #check if stack already:
    if isinstance(filenames, numpy.ndarray):
        return filenames

    #read the individual files
    group = []
    for f in filenames:
        data = io.readData(f, **args)
        data = numpy.reshape(data, (1, ) + data.shape)
        group.append(data)

    if combine:
        return numpy.vstack(group)
    else:
        return group
Exemplo n.º 23
0
def overlayPoints(dataSource, pointSource, sink = None, pointColor = [1,0,0], x = all, y = all, z = all):
    """Overlay points on 3D data and return as color image
    
    Arguments:
        dataSouce (str or array): volumetric image data
        pointSource (str or array): point data to be overlayed on the image data
        pointColor (array): RGB color for the overlayed points
        x, y, z (all or tuple): sub-range specification
    
    Returns:
        (str or array): image overlayed with points
        
    See Also:
        :func:`overlayLabel`
    """
    data = io.readData(dataSource, x = x, y = y, z = z);
    points = io.readPoints(pointSource, x = x, y = y, z = z, shift = True);
    #print data.shape
    
    if not pointColor is None:
        dmax = data.max(); dmin = data.min();
        if dmin == dmax:
            dmax = dmin + 1;
        cimage = numpy.repeat( (data - dmin) / (dmax - dmin), 3);
        cimage = cimage.reshape(data.shape + (3,));    
    
        if data.ndim == 2:
            for p in points: # faster version using voxelize ?
                cimage[p[0], p[1], :] = pointColor;
        elif data.ndim == 3:
            for p in points: # faster version using voxelize ?
                cimage[p[0], p[1], p[2], :] = pointColor;
        else:
            raise RuntimeError('overlayPoints: data dimension %d not suported' % data.ndim);
    
    else:
        cimage = vox.voxelize(points, data.shape, method = 'Pixel');
        cimage = cimage.astype(data.dtype) * data.max();
        data.shape = data.shape + (1,);
        cimage.shape =  cimage.shape + (1,);
        cimage = numpy.concatenate((data, cimage), axis  = 3);
    
    #print cimage.shape    
    return io.writeData(sink, cimage);   
Exemplo n.º 24
0
def test():
    """Test Spot Detection Module"""
    import os
    import ClearMap.ImageProcessing.SpotDetection as self
    reload(self)
    import ClearMap.IO as io
    import ClearMap.Settings as settings

    basedir = settings.ClearMapPath
    #fn = '/home/ckirst/Science/Projects/BrainActivityMap/Data/iDISCO_2015_06/Adult cfos C row 20HF 150524.ims';
    fn = os.path.join(basedir, 'Test/Data/Synthetic/label_iDISCO_\d{3}.tif')
    fn = os.path.join(
        basedir,
        'Test/Data/OME/16-17-27_0_8X-s3-20HF_UltraII_C00_xyz-Table Z\d{4}.ome.tif'
    )
    #fn = '/run/media/ckirst/ChristophsBackuk4TB/iDISCO_2015_06/Adult cfos C row 20HF 150524.ims';
    #fn = '/home/nicolas/Windows/Nico/cfosRegistrations/Adult cfos C row 20HF 150524 - Copy.ims';
    #fn = '/home/ckirst/Science/Projects/BrainActivityMap/iDISCO_2015_04/test for spots added spot.ims'

    img = io.readData(fn)
    #img = dataset[0:500,0:500,1000:1008];
    #img = dataset[600:1000,1600:1800,800:830];
    #img = dataset[500:1500,500:1500,800:809];
    img = img.astype('int16')

    #m = sys.modules['iDISCO.ImageProcessing.SpotDetection']
    #c = self.detectCells(img);

    c = self.detectCells(
        img,
        dogSize=None,
        cellShapeThreshold=1,
        cellShapeFile=
        '/home/ckirst/Science/Projects/BrainActivityMap/Analysis/iDISCO/Test/Data/CellShape/cellshape_\d{3}.tif'
    )

    print 'done, found %d cells !' % c[0].shape[0]

    #test intensities:
    import numpy
    x = numpy.random.rand(30, 30, 10)
    centers = numpy.array([[0, 0, 0], [29, 29, 9]])
    i = self.findIntensity(x, centers, boxSize=(1, 1, 1))
    print i
Exemplo n.º 25
0
def readData(filename, **args):
    """Read nrrd file image data
    
    Arguments:
        filename (str): file name as regular expression
        x,y,z (tuple): data range specifications
    
    Returns:
        array: image data
    """

    with open(filename,'rb') as filehandle:
        header = readHeader(filehandle)
        #print header
        data = _read_data(header, filehandle, filename)
        #return (data, header)
        #return data.transpose([1,0,2]);
        data = io.readData(data, **args);
        return data;
Exemplo n.º 26
0
def readData(filename, **args):
    """Read nrrd file image data
    
    Arguments:
        filename (str): file name as regular expression
        x,y,z (tuple): data range specifications
    
    Returns:
        array: image data
    """

    with open(filename, 'rb') as filehandle:
        header = readHeader(filehandle)
        #print header
        data = _read_data(header, filehandle, filename)
        #return (data, header)
        #return data.transpose([1,0,2]);
        data = io.readData(data, **args)
        return data
Exemplo n.º 27
0
def deformationDistance(deformationField, sink=None, scale=None):
    """Compute the distance field from a deformation vector field

    Arguments:
        deformationField (str or array): source of the deformation field determined by :func:`deformationField`
        sink (str or None): image sink to save the deformation field to
        scale (tuple or None): scale factor for each dimension, if None = (1,1,1)

    Returns:
        array or str: array or file name of the transformed data
    """

    deformationField = io.readData(deformationField)

    df = numpy.square(deformationField)
    if not scale is None:
        for i in range(3):
            df[:, :, :, i] = df[:, :, :, i] * (scale[i] * scale[i])

    return io.writeData(sink, numpy.sqrt(numpy.sum(df, axis=3)))
Exemplo n.º 28
0
def deformationDistance(deformationField, sink = None, scale = None):
    """Compute the distance field from a deformation vector field
    
    Arguments:
        deformationField (str or array): source of the deformation field determined by :func:`deformationField`
        sink (str or None): image sink to save the deformation field to
        scale (tuple or None): scale factor for each dimension, if None = (1,1,1)
        
    Returns:
        array or str: array or file name of the transformed data
    """
    
    deformationField = io.readData(deformationField);
    
    df = numpy.square(deformationField);
    if not scale is None:
        for i in range(3):
            df[:,:,:,i] = df[:,:,:,i] * (scale[i] * scale[i]);
            
    return io.writeData(sink, numpy.sqrt(numpy.sum(df, axis = 3)));
Exemplo n.º 29
0
def test():
    """Test Spot Detection Module"""
    import os
    import ClearMap.ImageProcessing.SpotDetection as self
    reload(self)
    import ClearMap.IO as io  
    import ClearMap.Settings as settings
    
    basedir = settings.ClearMapPath;
    #fn = '/home/ckirst/Science/Projects/BrainActivityMap/Data/iDISCO_2015_06/Adult cfos C row 20HF 150524.ims';
    fn = os.path.join(basedir, 'Test/Data/Synthetic/label_iDISCO_\d{3}.tif');
    fn = os.path.join(basedir, 'Test/Data/OME/16-17-27_0_8X-s3-20HF_UltraII_C00_xyz-Table Z\d{4}.ome.tif');
    #fn = '/run/media/ckirst/ChristophsBackuk4TB/iDISCO_2015_06/Adult cfos C row 20HF 150524.ims';
    #fn = '/home/nicolas/Windows/Nico/cfosRegistrations/Adult cfos C row 20HF 150524 - Copy.ims';
    #fn = '/home/ckirst/Science/Projects/BrainActivityMap/iDISCO_2015_04/test for spots added spot.ims'

    img = io.readData(fn);
    #img = dataset[0:500,0:500,1000:1008];
    #img = dataset[600:1000,1600:1800,800:830];
    #img = dataset[500:1500,500:1500,800:809]; 
    img = img.astype('int16');
    
    #m = sys.modules['iDISCO.ImageProcessing.SpotDetection']
    #c = self.detectCells(img);
    
    c = self.detectCells(img, dogSize = None, cellShapeThreshold = 1, cellShapeFile = '/home/ckirst/Science/Projects/BrainActivityMap/Analysis/iDISCO/Test/Data/CellShape/cellshape_\d{3}.tif');
    
    print 'done, found %d cells !' % c[0].shape[0]


    #test intensities:
    import numpy;
    x = numpy.random.rand(30,30,10);
    centers = numpy.array([[0,0,0], [29,29,9]]);
    i = self.findIntensity(x, centers, boxSize = (1,1,1));
    print i
Exemplo n.º 30
0
import os
import ClearMap.Settings as settings
filename = os.path.join(settings.ClearMapPath, 'Test/Data/ImageAnalysis/cfos-substack.tif');
import ClearMap.Visualization.Plot as plt
import ClearMap.IO as io
data = io.readData(filename, z = (0,26));
import ClearMap.ImageProcessing.BackgroundRemoval as bgr
dataBGR = bgr.removeBackground(data.astype('float'), size=(3,3), verbose = True);
plt.plotTiling(dataBGR, inverse = True, z = (10,16));
Exemplo n.º 31
0
def plotTiling(dataSource, tiling = "automatic", maxtiles = 20, x = all, y = all, z = all, inverse = False): 
    """Plot 3d image as 2d tiles
    
    Arguments:
        dataSouce (str or array): volumetric image data
        tiling (str or tuple): tiling specification
        maxtiles: maximalnumber of tiles
        x, y, z (all or tuple): sub-range specification
        inverse (bool):invert image
    
    Returns:
        (object): figure handle
    """
    
    image = io.readData(dataSource, x = x, y = y, z = z);
    dim = image.ndim;
    
    if dim < 2 or dim > 4:
        raise StandardError('plotTiling: image dimension must be 2 to 4');    
    
    if dim == 2:
        image = image.reshape(image.shape + (1,));
        dim = 3;

    if image.ndim == 3: 
        if image.shape[2] == 3:  # 2d color image
            ntiles = 1;
            cmap = None;
            image = image.reshape((image.shape[0], image.shape[1], 1, 3));
        else:                   # 3d gray image
            ntiles = image.shape[2];
            cmap = plt.cm.gray;
            image = image.reshape(image.shape + (1,));
    else:
        ntiles = image.shape[2]; # 3d color = 4d
        cmap = None;
    
    if ntiles > maxtiles:
        print "plotTiling: number of tiles %d very big! Clipping at %d!" % (ntiles, maxtiles);
        ntiles = maxtiles;
    
    if tiling == "automatic":
        nx = math.floor(math.sqrt(ntiles));
        ny = int(math.ceil(ntiles / nx));
        nx = int(nx);
    else:
        nx = int(tiling[0]);
        ny = int(tiling[1]);     
    
    #print image.shape
        
    fig, axarr = plt.subplots(nx, ny, sharex = True, sharey = True);
    fig.subplots_adjust(wspace=0.05, hspace=0.05);
    axarr = numpy.array(axarr);
    axarr = axarr.flatten();
    
    imin = image.min();
    imax = image.max();
    
    if inverse:
        (imin, imax) = (-float(imax), -float(imin));
    #print imin, imax    
    
    for i in range(0, ntiles): 
        a = axarr[i];
        imgpl = image[:,:,i,:].copy();
        imgpl = imgpl.transpose([1,0,2]);  
        
        if imgpl.shape[2] == 1:
            imgpl = imgpl.reshape((imgpl.shape[0], imgpl.shape[1]));       
            
        if inverse:
            imgpl = -imgpl.astype('float');
        
        #a.imshow(imgpl, interpolation='none', cmap = cmap, vmin = imin, vmax = imax);
        a.imshow(imgpl, interpolation='none', cmap = cmap, vmin = imin, vmax = imax);
    
    #fig.canvas.manager.window.activateWindow()
    #fig.canvas.manager.window.raise_()
    
    return fig;
Exemplo n.º 32
0
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 10 11:44:11 2017

@author: cailab
"""
import numpy
import os
import ClearMap.IO as io
import ClearMap.Settings as settings
import ClearMap.ImageProcessing.SpotDetection as sd
fn = '/media/sf_Fred_Data/testClearMap/1741-2-5/test-z\d{4}.tif'
img = io.readData(fn)

img = img.astype('int16')
# converting data to smaller integer types can be more memory efficient!
res = sd.detectSpots(img,
                     dogSize=(5, 5, 5),
                     flatfield=None,
                     threshold=5,
                     cellShapeThreshold=1)
print 'Found %d cells !' % res[0].shape[0]
def correctIllumination(img, correctIlluminationParameter = None, flatfield = None, background = None, scaling = None, save = None, verbose = False, 
                        subStack = None, out = sys.stdout, **parameter):
    """Correct illumination variations
    
     The intensity image :math:`I(x)` given a flat field :math:`F(x)` and 
     a background :math:`B(x)` the image is corrected to :math:`C(x)` as:
     
     .. math:
         C(x) = \\frac{I(x) - B(x)}{F(x) - B(x)}
         
     If the background is not given :math:`B(x) = 0`. 
     
     The correction is done slice by slice assuming the data was collected with 
     a light sheet microscope.
     
     The image is finally optionally scaled.
  
    Arguments:
        img (array): image data
        findCenterOfMaximaParameter (dict):
            ============ ==================== ===========================================================
            Name         Type                 Descritption
            ============ ==================== ===========================================================
            *flatfield*  (str, None or array) flat field intensities, if None d onot correct image for
                                              illumination, if True the 
            *background* (str, None or array) background image as file name or array
                                              if None background is assumed to be zero
            *scaling*    (str or None)        scale the corrected result by this factor
                                              if 'max'/'mean' scale to keep max/mean invariant
            *save*       (str or None)        save the corrected image to file
            *verbose*    (bool or int)        print / plot information about this step 
            ============ ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
    
    Returns:
        array: illumination corrected image
        
        
    References: 
        Fundamentals of Light Microscopy and Electronic Imaging, p 421        
        
    See Also:
        :const:`DefaultFlatFieldLineFile`
    """  
    
    flatfield  = getParameter(correctIlluminationParameter, "flatfield",  flatfield);
    background = getParameter(correctIlluminationParameter, "background", background);
    scaling    = getParameter(correctIlluminationParameter, "scaling",    scaling);
    save       = getParameter(correctIlluminationParameter, "save",       save);
    verbose    = getParameter(correctIlluminationParameter, "verbose",    verbose);

    if verbose:    
        if flatfield is None or isinstance(flatfield, str) or flatfield is True:
            fld = flatfield;
        else:
            fld = "image of size %s" % str(flatfield.shape);
    
        if background is None or isinstance(background, str):
            bkg = background;
        else:
            bkg = "image of size %s" % str(background.shape);
        
        writeParameter(out = out, head = 'Illumination correction:', flatfield = fld, background = bkg, scaling = scaling, save = save);  
    
    
    print subStack;
 
    if not subStack is None:
        x = subStack["x"];
        y = subStack["y"];
    else:
        x = all;
        y = all;
    
    #print "sizes", x, y, img.shape
 
    #read data  
 
    timer = Timer(); 
 
    if flatfield is None:
        return img;
        
    elif flatfield is True:
        # default flatfield correction
    
        if subStack is None:    
            flatfield = flatfieldFromLine(DefaultFlatFieldLineFile, img.shape[0]);
        else:
            dataSize = io.dataSize(subStack["source"]);
            flatfield = flatfieldFromLine(DefaultFlatFieldLineFile, dataSize[0]);
            
    elif isinstance(flatfield, str):
        # point or image file
        if io.isPointFile(flatfield):
            if subStack is None:    
                flatfield = flatfieldFromLine(flatfield, img.shape[0]);
            else:
               dataSize = io.dataSize(subStack["source"]);
               flatfield = flatfieldFromLine(flatfield, dataSize[0]);
        else:
            flatfield = io.readData(flatfield);
    
    ffmean = flatfield.mean();    
    ffmax = flatfield.max();

    #correct for subset
    flatfield = io.readData(flatfield, x = x, y = y);   
    
    background = io.readData(background, x = x, y = y);
    
    if flatfield.shape != img[:,:,0].shape:
        raise RuntimeError("correctIllumination: flatfield does not match image size: %s vs %s" % (flatfield.shape,  img[:,:,0].shape));
    
    #convert to float for scaling
    dtype = img.dtype;
    img = img.astype('float32');
    flatfield = flatfield.astype('float32');
    
    # illumination correction in each slice
    if background is None:
        for z in range(img.shape[2]):
            img[:,:,z] = img[:,:,z] / flatfield;
    else:
        if background.shape != flatfield.shape:
            raise RuntimeError("correctIllumination: background does not match image size: %s vs %s" % (background.shape,  img[:,:,0].shape));        
        background = background.astype('float32');

        flatfield = (flatfield - background);
        for z in range(img.shape[2]):
            img[:,:,z] = (img[:,:,z] - background) / flatfield;
    
        
    # rescale
    if scaling is True:
        scaling = "mean";
    
    if isinstance(scaling, str):
        if scaling.lower() == "mean":
            # scale back by average flat field correction:
            sf = ffmean;
        elif scaling.lower() == "max":
            sf = ffmax;
        else:
            raise RuntimeError('Scaling not "Max" or "Mean" but %s' % scaling);
    else:
        sf = scaling;
      
    if verbose:
         writeParameter(out = out, head = 'Illumination correction:',  scaling = sf);
    
        
    
    if not sf is None:
        img = img * sf;
        img = img.astype(dtype);
    
    
    #write result for inspection
    if not save is None:
        writeSubStack(save, img, subStack = subStack);    
    
    #plot result for inspection
    if verbose > 1:
        plotTiling(img);
    
    if verbose:
        out.write(timer.elapsedTime(head = 'Illumination correction') + '\n');    
    
    return img 
Exemplo n.º 34
0
import os
import ClearMap.Settings as settings
filename = os.path.join(settings.ClearMapPath,
                        'Test/Data/ImageAnalysis/cfos-substack.tif')
import ClearMap.Visualization.Plot as plt
import ClearMap.IO as io
data = io.readData(filename, z=(0, 26))
import ClearMap.ImageProcessing.BackgroundRemoval as bgr
dataBGR = bgr.removeBackground(data.astype('float'), size=(3, 3), verbose=True)
from ClearMap.ImageProcessing.Filter.DoGFilter import filterDoG
dataDoG = filterDoG(dataBGR, size=(8, 8, 4), verbose=True)
from ClearMap.ImageProcessing.MaximaDetection import findExtendedMaxima
dataMax = findExtendedMaxima(dataDoG, hMax=None, verbose=True, threshold=10)
from ClearMap.ImageProcessing.MaximaDetection import findCenterOfMaxima
cells = findCenterOfMaxima(data, dataMax)
from ClearMap.ImageProcessing.CellSizeDetection import detectCellShape
dataShape = detectCellShape(dataDoG, cells, threshold=15)
plt.plotOverlayLabel(dataDoG / dataDoG.max(), dataShape, z=(10, 16))
Exemplo n.º 35
0
def transformData(source, sink = [], transformParameterFile = None, transformDirectory = None, resultDirectory = None):
    """Transform a raw data set to reference using the elastix alignment results
    
    If the map determined by elastix is 
    :math:`T \\mathrm{fixed} \\rightarrow \\mathrm{moving}`, 
    transformix on data works as :math:`T^{-1}(\\mathrm{data})`.
        
    Arguments:
        source (str or array): image source to be transformed
        sink (str, [] or None): image sink to save transformed image to. if [] return the default name of the data file generated by transformix.
        transformParameterFile (str or None): parameter file for the primary transformation, if None, the file is determined from the transformDirectory.
        transformDirectory (str or None): result directory of elastix alignment, if None the transformParameterFile has to be given.
        resultDirectory (str or None): the directorty for the transformix results
        
    Returns:
        array or str: array or file name of the transformed data
    """
    
    global TransformixBinary;    
    
    if isinstance(source, numpy.ndarray):
        imgname = os.path.join(tempfile.gettempdir(), 'elastix_input.tif');
        io.writeData(source, imgname);
    elif isinstance(source, basestring):
        if io.dataFileNameToType(source) == "TIF":
            imgname = source;
        else:
            imgname = os.path.join(tempfile.gettempdir(), 'elastix_input.tif');
            io.transformData(source, imgname);
    else:
        raise RuntimeError('transformData: source not a string or array');

    if resultDirectory == None:
        resultdirname = os.path.join(tempfile.tempdir, 'elastix_output');
    else:
        resultdirname = resultDirectory;
        
    if not os.path.exists(resultdirname):
        os.makedirs(resultdirname);
        
    
    if transformParameterFile == None:
        if transformDirectory == None:
            raise RuntimeError('neither alignment directory and transformation parameter file specified!'); 
        transformparameterdir = transformDirectory
        transformParameterFile = getTransformParameterFile(transformparameterdir);
    else:
        transformparameterdir = os.path.split(transformParameterFile);
        transformparameterdir = transformparameterdir[0];
    
    #transform
    #make path in parameterfiles absolute
    setPathTransformParameterFiles(transformparameterdir);
   
    #transformix -in inputImage.ext -out outputDirectory -tp TransformParameters.txt
    cmd = TransformixBinary + ' -in ' + imgname + ' -out ' + resultdirname + ' -tp ' + transformParameterFile;
    
    res = os.system(cmd);
    
    if res != 0:
        raise RuntimeError('transformData: failed executing: ' + cmd);
    
    
    if not isinstance(source, basestring):
        os.remove(imgname);

    if sink == []:
        return getResultDataFile(resultdirname);
    elif sink is None:
        resultfile = getResultDataFile(resultdirname);
        return io.readData(resultfile);
    elif isinstance(sink, basestring):
        resultfile = getResultDataFile(resultdirname);
        return io.convertData(resultfile, sink);
    else:
        raise RuntimeError('transformData: sink not valid!');
Created on Sat Jun  2 03:39:40 2018

@author: smith
"""

import os
import ClearMap.IO as io
import ClearMap.Settings as settings
import ClearMap.Visualization.Plot as plt
import ClearMap.ImageProcessing.BackgroundRemoval as bgr
import matplotlib as mplt

BaseDirectory = '/d2/studies/ClearMap/IA_iDISCO/IA1_RB/'
filename = os.path.join(BaseDirectory, 'IA1_RB_cfos_stack.ome.tif')

data = io.readData(filename, z=(600, 650))
# size restriction
fileRange = 'x = (1050, 1350), y = (550,850), z = (25,34)'
""" note that the Z coordinate in fileRange variable are relative to the planes imported in the data variable.
So in this case, you would be imaging planes 675-684.
"""

plt.plotTiling(data, inverse=True, x=(1250, 1350), y=(550, 650), z=(25, 34))  #
# background subtraction
dataBGR = bgr.removeBackground(data.astype('float'),
                               size=(5, 5),
                               verbose=False,
                               save=None)
dataBGR_write = plt.plotTiling(dataBGR,
                               inverse=True,
                               x=(1250, 1350),
Exemplo n.º 37
0
affineParameterFile = '/home/cailab/clearmap_ressources_mouse_brain/ClearMap_ressources/Par0000affine.txt'
bSplineParameterFile = '/home/cailab/clearmap_ressources_mouse_brain/ClearMap_ressources/BsplineDefault.txt'
alignResultDir = os.path.join(homeDir, 'Align')
tempDir = os.path.join(homeDir, 'TempAlign')
transformResultDir = os.path.join(homeDir, 'Transform')
transformFN = os.path.join(transformResultDir, 'outputpoints.txt')
tableFN = os.path.join(homeDir, 'ResultsTable.csv')

atlasDir = os.path.join('/media/sf_Fred_Data/testClearMap/',
                        orientation + 'Atlas')
annoDir = os.path.join('/media/sf_Fred_Data/testClearMap/',
                       orientation + 'Annotation')
transformParameterFN = os.path.join(alignResultDir,
                                    'TransformParameters.1.txt')

img = io.readData(input_fn)
img = img[..., numpy.newaxis]
img = img.astype('int16')
# converting data to smaller integer types can be more memory efficient!

imgB = bgr.removeBackground(img, size=(8, 8), verbose=True)
io.writeData(os.path.join(homeDir, 'Results/BackgroundRemoval.tif'), imgB)

#image filter
imgD = filterDoG(imgB, size=(15, 15, 1), verbose=True)
io.writeData(os.path.join(homeDir, 'Results/FilterDoG.tif'), imgD)

#Detect Maxima
imgMaxima = findExtendedMaxima(imgD, hMax=None, verbose=True, threshold=3)
points = findCenterOfMaxima(img, imgMaxima)
points = points.astype('int16')
Exemplo n.º 38
0
affineParameterFile = '/home/cailab/clearmap_ressources_mouse_brain/ClearMap_ressources/Par0000affine.txt'
bSplineParameterFile = '/home/cailab/clearmap_ressources_mouse_brain/ClearMap_ressources/BsplineDefault.txt'
atlasDir = os.path.join('/media/sf_Fred_Data/testClearMap/',
                        orientation + 'Atlas')
annoDir = os.path.join('/media/sf_Fred_Data/testClearMap/',
                       orientation + 'Annotation')
auto_fn = [
    '/media/sf_Fred_Data/OdorInduction/cfosOdor052417/Debugging/C2-MAX_8d_594.tif',
    '/media/sf_Fred_Data/OdorInduction/cfosOdor052417/Debugging/MAX_8d_debugging_546.tif'
]

auto_R_fn = '/media/sf_Fred_Data/OdorInduction/cfosOdor052417/testAlign/testR.tif'
tempDir = '/media/sf_Fred_Data/OdorInduction/cfosOdor052417/testAlign'

for i in range(len(auto_fn)):
    imgR = io.readData(auto_fn[i])
    imgR = imgR[..., numpy.newaxis]
    imgR = imgR.astype('int16')
    imgR, scaleFactor = resampleData(imgR,
                                     auto_R_fn,
                                     orientation=(1, 2, 3),
                                     dataSizeSink=None,
                                     resolutionSource=pixelRes,
                                     resolutionSink=(25, 25, 1),
                                     processingDirectory=None,
                                     processes=4,
                                     cleanup=True,
                                     verbose=True,
                                     interpolation='linear')

    bestSlice = autoAlignData(imgR,
Exemplo n.º 39
0
def writePoints(filename, points, labelImage = None):
    """Write point data to vtk file
    
    Arguments:
        filename (str): file name
        points (array): point data
        labelImage (str, array or None): optional label image to determine point label
    
    Returns:
        str: file name
    """

    #y = points[:,0];
    #x = points[:,1];
    #z = points[:,2];    
    
    x = points[:,0];
    y = points[:,1];
    z = points[:,2];    
    nPoint = x.size;
    
    #print nPoint;
    
    pointLabels = numpy.ones(nPoint);
    if not labelImage is None:
        if isinstance(labelImage, str):
            labelImage = io.readData(labelImage);
            
        dsize = labelImage.shape;
        for i in range(nPoint):
            #if y[i] >= 0 and y[i] < dsize[0] and x[i] >= 0 and x[i] < dsize[1] and z[i] >= 0 and z[i] < dsize[2]:
            if x[i] >= 0 and x[i] < dsize[0] and y[i] >= 0 and y[i] < dsize[1] and z[i] >= 0 and z[i] < dsize[2]:
                 #pointLabels[i] = labelImage[y[i], x[i], z[i]];
                 pointLabels[i] = labelImage[x[i], y[i], z[i]];
        
    #write VTK file
    vtkFile = open(filename, 'w')
    vtkFile.write('# vtk DataFile Version 2.0\n');
    vtkFile.write('Unstructured Grid Example\n');
    vtkFile.write('ASCII\n');
    vtkFile.write('DATASET UNSTRUCTURED_GRID\n');
    vtkFile.write("POINTS " + str(nPoint) + " float\n")
    for iPoint in range(nPoint):
        vtkFile.write(str(x[iPoint]).format('%05.20f') + " " +  str(y[iPoint]).format('%05.20f') + " " + str(z[iPoint]).format('%05.20f') + "\n");    
    
    vtkFile.write("CELLS " + str(nPoint) + " " + str(nPoint * 2) + "\n");


    for iPoint in range(nPoint):
        vtkFile.write("1 " + str(iPoint) + "\n");
    vtkFile.write("CELL_TYPES " + str(nPoint) + "\n");
    for iPoint in range(0, nPoint):
        vtkFile.write("1 \n");
    #vtkFile.write("\n");
    vtkFile.write("POINT_DATA " + str(nPoint) + "\n");
    vtkFile.write('SCALARS scalars float 1\n');
    vtkFile.write("LOOKUP_TABLE default\n");
    for iLabel in pointLabels:
        vtkFile.write(str(int(iLabel)) + " ");
        #vtkFile.write("1 ")
    vtkFile.write("\n");
    vtkFile.close();   
    
    return filename;
Exemplo n.º 40
0
def deformationField(sink = [], transformParameterFile = None, transformDirectory = None, resultDirectory = None):
    """Create the deformation field T(x) - x
    
    The map determined by elastix is 
    :math:`T \\mathrm{fixed} \\rightarrow \\mathrm{moving}`
        
    Arguments:
        sink (str, [] or None): image sink to save the transformation field; if [] return the default name of the data file generated by transformix.
        transformParameterFile (str or None): parameter file for the primary transformation, if None, the file is determined from the transformDirectory.
        transformDirectory (str or None): result directory of elastix alignment, if None the transformParameterFile has to be given.
        resultDirectory (str or None): the directorty for the transformix results
        
    Returns:
        array or str: array or file name of the transformed data
    """
    
    global TransformixBinary;    
    
    if resultDirectory == None:
        resultdirname = os.path.join(tempfile.tempdir, 'elastix_output');
    else:
        resultdirname = resultDirectory;
        
    if not os.path.exists(resultdirname):
        os.makedirs(resultdirname);
        
    if transformParameterFile == None:
        if transformDirectory == None:
            raise RuntimeError('neither alignment directory and transformation parameter file specified!'); 
        transformparameterdir = transformDirectory
        transformParameterFile = getTransformParameterFile(transformparameterdir);
    else:
        transformparameterdir = os.path.split(transformParameterFile);
        transformparameterdir = transformparameterdir[0];
    
    #transform
    #make path in parameterfiles absolute
    setPathTransformParameterFiles(transformparameterdir);
   
    #transformix -in inputImage.ext -out outputDirectory -tp TransformParameters.txt
    cmd = TransformixBinary + ' -def all -out ' + resultdirname + ' -tp ' + transformParameterFile;
    
    res = os.system(cmd);
    
    if res != 0:
        raise RuntimeError('deformationField: failed executing: ' + cmd);
    
    
    if sink == []:
        return getResultDataFile(resultdirname);
    elif sink is None:
        resultfile = getResultDataFile(resultdirname);
        data = io.readData(resultfile);
        if resultDirectory is None:
            shutil.rmtree(resultdirname);
        return data;
    elif isinstance(sink, basestring):
        resultfile = getResultDataFile(resultdirname);
        data = io.convertData(resultfile, sink);
        if resultDirectory is None:
            shutil.rmtree(resultdirname);
        return data;
    else:
        raise RuntimeError('deformationField: sink not valid!');
Exemplo n.º 41
0
def transformData(source,
                  sink=[],
                  transformParameterFile=None,
                  transformDirectory=None,
                  resultDirectory=None):
    """Transform a raw data set to reference using the elastix alignment results

    If the map determined by elastix is
    :math:`T \\mathrm{fixed} \\rightarrow \\mathrm{moving}`,
    transformix on data works as :math:`T^{-1}(\\mathrm{data})`.

    Arguments:
        source (str or array): image source to be transformed
        sink (str, [] or None): image sink to save transformed image to. if [] return the default name of the data file generated by transformix.
        transformParameterFile (str or None): parameter file for the primary transformation, if None, the file is determined from the transformDirectory.
        transformDirectory (str or None): result directory of elastix alignment, if None the transformParameterFile has to be given.
        resultDirectory (str or None): the directorty for the transformix results

    Returns:
        array or str: array or file name of the transformed data
    """

    global TransformixBinary

    if isinstance(source, numpy.ndarray):
        imgname = os.path.join(tempfile.gettempdir(), 'elastix_input.tif')
        io.writeData(source, imgname)
    elif isinstance(source, basestring):
        if io.dataFileNameToType(source) == "TIF":
            imgname = source
        else:
            imgname = os.path.join(tempfile.gettempdir(), 'elastix_input.tif')
            io.transformData(source, imgname)
    else:
        raise RuntimeError('transformData: source not a string or array')

    if resultDirectory == None:
        resultdirname = os.path.join(tempfile.gettempdir(), 'elastix_output')
    else:
        resultdirname = resultDirectory

    if not os.path.exists(resultdirname):
        os.makedirs(resultdirname)

    if transformParameterFile == None:
        if transformDirectory == None:
            raise RuntimeError(
                'neither alignment directory and transformation parameter file specified!'
            )
        transformparameterdir = transformDirectory
        transformParameterFile = getTransformParameterFile(
            transformparameterdir)
    else:
        transformparameterdir = os.path.split(transformParameterFile)
        transformparameterdir = transformparameterdir[0]

    #transform
    #make path in parameterfiles absolute
    setPathTransformParameterFiles(transformparameterdir)

    #transformix -in inputImage.ext -out outputDirectory -tp TransformParameters.txt
    cmd = TransformixBinary + ' -threads 8 -in ' + imgname + ' -out ' + resultdirname + ' -tp ' + transformParameterFile

    res = os.system(cmd)

    if res != 0:
        raise RuntimeError('transformData: failed executing: ' + cmd)

    if not isinstance(source, basestring):
        os.remove(imgname)

    if sink == []:
        return getResultDataFile(resultdirname)
    elif sink is None:
        resultfile = getResultDataFile(resultdirname)
        return io.readData(resultfile)
    elif isinstance(sink, basestring):
        resultfile = getResultDataFile(resultdirname)
        return io.convertData(resultfile, sink)
    else:
        raise RuntimeError('transformData: sink not valid!')
Exemplo n.º 42
0
def deformationField(sink=[],
                     transformParameterFile=None,
                     transformDirectory=None,
                     resultDirectory=None):
    """Create the deformation field T(x) - x

    The map determined by elastix is
    :math:`T \\mathrm{fixed} \\rightarrow \\mathrm{moving}`

    Arguments:
        sink (str, [] or None): image sink to save the transformation field; if [] return the default name of the data file generated by transformix.
        transformParameterFile (str or None): parameter file for the primary transformation, if None, the file is determined from the transformDirectory.
        transformDirectory (str or None): result directory of elastix alignment, if None the transformParameterFile has to be given.
        resultDirectory (str or None): the directorty for the transformix results

    Returns:
        array or str: array or file name of the transformed data
    """

    global TransformixBinary

    if resultDirectory == None:
        resultdirname = os.path.join(tempfile.gettempdir(), 'elastix_output')
    else:
        resultdirname = resultDirectory

    if not os.path.exists(resultdirname):
        os.makedirs(resultdirname)

    if transformParameterFile == None:
        if transformDirectory == None:
            raise RuntimeError(
                'neither alignment directory and transformation parameter file specified!'
            )
        transformparameterdir = transformDirectory
        transformParameterFile = getTransformParameterFile(
            transformparameterdir)
    else:
        transformparameterdir = os.path.split(transformParameterFile)
        transformparameterdir = transformparameterdir[0]

    #transform
    #make path in parameterfiles absolute
    setPathTransformParameterFiles(transformparameterdir)

    #transformix -in inputImage.ext -out outputDirectory -tp TransformParameters.txt
    cmd = TransformixBinary + ' -threads 8 -def all -out ' + resultdirname + ' -tp ' + transformParameterFile

    res = os.system(cmd)

    if res != 0:
        raise RuntimeError('deformationField: failed executing: ' + cmd)

    if sink == []:
        return getResultDataFile(resultdirname)
    elif sink is None:
        resultfile = getResultDataFile(resultdirname)
        data = io.readData(resultfile)
        if resultDirectory is None:
            shutil.rmtree(resultdirname)
        return data
    elif isinstance(sink, basestring):
        resultfile = getResultDataFile(resultdirname)
        data = io.convertData(resultfile, sink)
        if resultDirectory is None:
            shutil.rmtree(resultdirname)
        return data
    else:
        raise RuntimeError('deformationField: sink not valid!')
Exemplo n.º 43
0
def plotTiling(dataSource,
               tiling="automatic",
               maxtiles=20,
               x=all,
               y=all,
               z=all,
               inverse=False):
    """Plot 3d image as 2d tiles
    
    Arguments:
        dataSouce (str or array): volumetric image data
        tiling (str or tuple): tiling specification
        maxtiles: maximalnumber of tiles
        x, y, z (all or tuple): sub-range specification
        inverse (bool):invert image
    
    Returns:
        (object): figure handle
    """

    image = io.readData(dataSource, x=x, y=y, z=z)
    dim = image.ndim

    if dim < 2 or dim > 4:
        raise StandardError('plotTiling: image dimension must be 2 to 4')

    if dim == 2:
        image = image.reshape(image.shape + (1, ))
        dim = 3

    if image.ndim == 3:
        if image.shape[2] == 3:  # 2d color image
            ntiles = 1
            cmap = None
            image = image.reshape((image.shape[0], image.shape[1], 1, 3))
        else:  # 3d gray image
            ntiles = image.shape[2]
            cmap = plt.cm.gray
            image = image.reshape(image.shape + (1, ))
    else:
        ntiles = image.shape[2]
        # 3d color = 4d
        cmap = None

    if ntiles > maxtiles:
        print "plotTiling: number of tiles %d very big! Clipping at %d!" % (
            ntiles, maxtiles)
        ntiles = maxtiles

    if tiling == "automatic":
        nx = math.floor(math.sqrt(ntiles))
        ny = int(math.ceil(ntiles / nx))
        nx = int(nx)
    else:
        nx = int(tiling[0])
        ny = int(tiling[1])

    #print image.shape

    fig, axarr = plt.subplots(nx, ny, sharex=True, sharey=True)
    fig.subplots_adjust(wspace=0.05, hspace=0.05)
    axarr = numpy.array(axarr)
    axarr = axarr.flatten()

    imin = image.min()
    imax = image.max()

    if inverse:
        (imin, imax) = (-float(imax), -float(imin))
    #print imin, imax

    for i in range(0, ntiles):
        a = axarr[i]
        imgpl = image[:, :, i, :].copy()
        imgpl = imgpl.transpose([1, 0, 2])

        if imgpl.shape[2] == 1:
            imgpl = imgpl.reshape((imgpl.shape[0], imgpl.shape[1]))

        if inverse:
            imgpl = -imgpl.astype('float')

        #a.imshow(imgpl, interpolation='none', cmap = cmap, vmin = imin, vmax = imax);
        a.imshow(imgpl, interpolation='none', cmap=cmap, vmin=imin, vmax=imax)

    #fig.canvas.manager.window.activateWindow()
    #fig.canvas.manager.window.raise_()

    return fig
Exemplo n.º 44
0
    #Elastix points FN after resampling
    points_elastix_fn = os.path.join(resultDir, 'ElastixPoints.txt')

    #Points before resampling
    points_fn = os.path.join(resultDir, 'Points.npy')
    tPointsFN = os.path.join(resultDir, 'Transformed_Points.npy')

    transformFN = os.path.join(transformDir, 'outputpoints.txt')
    tableFN = os.path.join(resultDir, 'ResultsTable.csv')

    #Atlas and Annotation
    transformParameterFN = os.path.join(alignDir, 'TransformParameters.1.txt')

    print(cfos_fn)
    img = io.readData(cfos_fn)
    img = img[..., numpy.newaxis]
    img = img.astype('int16')
    # converting data to smaller integer types can be more memory efficient!

    imgB = bgr.removeBackground(img, size=backgroundSize, verbose=True)
    io.writeData(os.path.join(resultDir, fName + '_BackgroundRemoval.tif'),
                 imgB)

    ##image filter
    imgD = filterDoG(img, size=DoGSize, verbose=True)
    io.writeData(os.path.join(resultDir, fName + '_FilterDoG.tif'), imgD)

    #Detect Maxima
    imgMaxima = findExtendedMaxima(img,
                                   hMax=None,
Exemplo n.º 45
0
def cropData(source, sink=None, x=all, y=all, z=all, adjustOverlap=False, verbose=True, processes=all):
    """Crop source from start to stop point
  
  Arguments:
    source (str or array): filename or data array of source
    sink (str or None): filename or sink
    x,y,z (tuple or all): the range to crop the data to
    adjustOverlap (bool): correct overlap meta data if exists
  
  Return:
    str or array: array or filename with cropped data
  """

    if sink is None:
        return readDataFiles(source, x=x, y=y, z=z)
    else:  # sink assumed to be file expression

        if not io.isFileExpression(sink):
            raise RuntimeError("cropping data to different format not supported!")

        fileheader, fileext, digitfrmt = splitFileExpression(sink)

        # read first image to get data size and type
        fp, fl = readFileList(source)
        nz = len(fl)
        rz = io.toDataRange(nz, r=z)

        if adjustOverlap:  # change overlap in first file
            try:
                fn = os.path.join(fp, fl[0])
                info = io.readMetaData(fn, info=["description", "overlap", "resolution"])
                description = str(info["description"])
                overlap = numpy.array(info["overlap"], dtype=float)
                resolution = numpy.array(info["resolution"], dtype=float)

            except:
                raise RuntimeWarning("could not modify overlap!")

            fullsize = io.dataSize(fn)
            data = io.readData(fn, x=x, y=y)

            # overlap in pixels
            poverlap = overlap[:2] / resolution[:2]
            print poverlap

            # cropped pixel
            xr = io.toDataRange(fullsize[0], r=x)
            yr = io.toDataRange(fullsize[1], r=y)

            print xr
            print yr
            print fullsize

            poverlap[0] = poverlap[0] - xr[0] - (fullsize[0] - xr[1])
            poverlap[1] = poverlap[1] - yr[0] - (fullsize[1] - yr[1])
            print poverlap

            # new overlap in microns
            overlap = poverlap * resolution[:2]

            # check for consistency
            if numpy.abs(fullsize[0] - xr[1] - xr[0]) > 1 or numpy.abs(fullsize[1] - yr[1] - yr[0]) > 1:
                raise RuntimeWarning("cropping is inconsistent with overlap )modification!")

            # change image description
            import ClearMap.IO.TIF as CMTIF

            description = CMTIF.changeOMEMetaDataString(description, {"overlap": overlap})
            print len(description)

            # write first file
            fnout = fileheader + (digitfrmt % 0) + fileext
            io.writeData(fnout, data, info=description)

            zr = range(rz[0] + 1, rz[1])
        else:
            zr = range(rz[0], rz[1])

        print zr
        nZ = len(zr)

        if processes is None:
            processes = 1
        if processes is all:
            processes = multiprocessing.cpu_count()

        if processes > 1:  # parallel processing
            pool = multiprocessing.Pool(processes=processes)
            argdata = []

            for i, z in enumerate(zr):
                if verbose:
                    argdata.append(
                        (os.path.join(fp, fl[z]), fileheader + (digitfrmt % (i + 1)) + fileext, x, y, (i + 1), (nZ + 1))
                    )
                else:
                    argdata.append(
                        (os.path.join(fp, fl[z]), fileheader + (digitfrmt % (i + 1)) + fileext, x, y, None, None)
                    )

            pool.map(_cropParallel, argdata)

        else:  # sequential processing
            for i, z in enumerate(zr):
                if verbose:
                    print "cropData: corpping image %d / %d" % (i + 1, nZ + 1)

                fileSource = os.path.join(fp, fl[z])
                data = io.readData(fileSource, x=x, y=y)

                fileSink = fileheader + (digitfrmt % (i + 1)) + fileext
                io.writeData(fileSink, data)

        return sink
Exemplo n.º 46
0
def correctIllumination(img,
                        correctIlluminationParameter=None,
                        flatfield=None,
                        background=None,
                        scaling=None,
                        save=None,
                        verbose=False,
                        subStack=None,
                        out=sys.stdout,
                        **parameter):
    """Correct illumination variations
    
     The intensity image :math:`I(x)` given a flat field :math:`F(x)` and 
     a background :math:`B(x)` the image is corrected to :math:`C(x)` as:
     
     .. math:
         C(x) = \\frac{I(x) - B(x)}{F(x) - B(x)}
         
     If the background is not given :math:`B(x) = 0`. 
     
     The correction is done slice by slice assuming the data was collected with 
     a light sheet microscope.
     
     The image is finally optionally scaled.
  
    Arguments:
        img (array): image data
        findCenterOfMaximaParameter (dict):
            ============ ==================== ===========================================================
            Name         Type                 Descritption
            ============ ==================== ===========================================================
            *flatfield*  (str, None or array) flat field intensities, if None d onot correct image for
                                              illumination, if True the 
            *background* (str, None or array) background image as file name or array
                                              if None background is assumed to be zero
            *scaling*    (str or None)        scale the corrected result by this factor
                                              if 'max'/'mean' scale to keep max/mean invariant
            *save*       (str or None)        save the corrected image to file
            *verbose*    (bool or int)        print / plot information about this step 
            ============ ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
    
    Returns:
        array: illumination corrected image
        
        
    References: 
        Fundamentals of Light Microscopy and Electronic Imaging, p 421        
        
    See Also:
        :const:`DefaultFlatFieldLineFile`
    """

    flatfield = getParameter(correctIlluminationParameter, "flatfield",
                             flatfield)
    background = getParameter(correctIlluminationParameter, "background",
                              background)
    scaling = getParameter(correctIlluminationParameter, "scaling", scaling)
    save = getParameter(correctIlluminationParameter, "save", save)
    verbose = getParameter(correctIlluminationParameter, "verbose", verbose)

    if verbose:
        if flatfield is None or isinstance(flatfield,
                                           str) or flatfield is True:
            fld = flatfield
        else:
            fld = "image of size %s" % str(flatfield.shape)

        if background is None or isinstance(background, str):
            bkg = background
        else:
            bkg = "image of size %s" % str(background.shape)

        writeParameter(out=out,
                       head='Illumination correction:',
                       flatfield=fld,
                       background=bkg,
                       scaling=scaling,
                       save=save)

    print subStack

    if not subStack is None:
        x = subStack["x"]
        y = subStack["y"]
    else:
        x = all
        y = all

    #print "sizes", x, y, img.shape

    #read data

    timer = Timer()

    if flatfield is None:
        return img

    elif flatfield is True:
        # default flatfield correction

        if subStack is None:
            flatfield = flatfieldFromLine(DefaultFlatFieldLineFile,
                                          img.shape[0])
        else:
            dataSize = io.dataSize(subStack["source"])
            flatfield = flatfieldFromLine(DefaultFlatFieldLineFile,
                                          dataSize[0])

    elif isinstance(flatfield, str):
        # point or image file
        if io.isPointFile(flatfield):
            if subStack is None:
                flatfield = flatfieldFromLine(flatfield, img.shape[0])
            else:
                dataSize = io.dataSize(subStack["source"])
                flatfield = flatfieldFromLine(flatfield, dataSize[0])
        else:
            flatfield = io.readData(flatfield)

    ffmean = flatfield.mean()
    ffmax = flatfield.max()

    #correct for subset
    flatfield = io.readData(flatfield, x=x, y=y)

    background = io.readData(background, x=x, y=y)

    if flatfield.shape != img[:, :, 0].shape:
        raise RuntimeError(
            "correctIllumination: flatfield does not match image size: %s vs %s"
            % (flatfield.shape, img[:, :, 0].shape))

    #convert to float for scaling
    dtype = img.dtype
    img = img.astype('float32')
    flatfield = flatfield.astype('float32')

    # illumination correction in each slice
    if background is None:
        for z in range(img.shape[2]):
            img[:, :, z] = img[:, :, z] / flatfield
    else:
        if background.shape != flatfield.shape:
            raise RuntimeError(
                "correctIllumination: background does not match image size: %s vs %s"
                % (background.shape, img[:, :, 0].shape))
        background = background.astype('float32')

        flatfield = (flatfield - background)
        for z in range(img.shape[2]):
            img[:, :, z] = (img[:, :, z] - background) / flatfield

    # rescale
    if scaling is True:
        scaling = "mean"

    if isinstance(scaling, str):
        if scaling.lower() == "mean":
            # scale back by average flat field correction:
            sf = ffmean
        elif scaling.lower() == "max":
            sf = ffmax
        else:
            raise RuntimeError('Scaling not "Max" or "Mean" but %s' % scaling)
    else:
        sf = scaling

    if verbose:
        writeParameter(out=out, head='Illumination correction:', scaling=sf)

    if not sf is None:
        img = img * sf
        img = img.astype(dtype)

    #write result for inspection
    if not save is None:
        writeSubStack(save, img, subStack=subStack)

    #plot result for inspection
    if verbose > 1:
        plotTiling(img)

    if verbose:
        out.write(timer.elapsedTime(head='Illumination correction') + '\n')

    return img
Exemplo n.º 47
0
            for background in backgrounds:
                for size in sizes:
                    dst = os.path.join(
                        pth,
                        "sweep/max_threshold%02d_DoG%02d_size%02d_background%02d"
                        % (max_threshold, DoG, size, background))

                    if not os.path.exists(dst):
                        os.mkdir(dst)
                        #save tifs elsewhere
                        vis = os.path.join(dst, "stacks")
                        if not os.path.exists(vis): os.mkdir(vis)

                        print("\n\n{}".format(size))
                        for fn in vols:
                            img = io.readData(fn)  #read as x,y,z
                            img = img.astype("uint16")

                            c = detectSpots(
                                img,
                                detectSpotsParameter=None,
                                correctIlluminationParameter=None,
                                removeBackgroundParameter={
                                    "size": (background, background)
                                },
                                filterDoGParameter={"size": (DoG, DoG, 4)},
                                findExtendedMaximaParameter={
                                    "h-max": None,
                                    "size": size,
                                    "threshold": max_threshold
                                },
Exemplo n.º 48
0
def writePoints(filename, points, labelImage = None):
    """Write point data to vtk file
    
    Arguments:
        filename (str): file name
        points (array): point data
        labelImage (str, array or None): optional label image to determine point label
    
    Returns:
        str: file name
    """

    #y = points[:,0];
    #x = points[:,1];
    #z = points[:,2];    
    
    x = points[:,0];
    y = points[:,1];
    z = points[:,2];    
    nPoint = x.size;
    
    #print nPoint;
    
    pointLabels = numpy.ones(nPoint);
    if not labelImage is None:
        if isinstance(labelImage, basestring):
            labelImage = io.readData(labelImage);
            
        dsize = labelImage.shape;
        for i in range(nPoint):
            #if y[i] >= 0 and y[i] < dsize[0] and x[i] >= 0 and x[i] < dsize[1] and z[i] >= 0 and z[i] < dsize[2]:
            if x[i] >= 0 and x[i] < dsize[0] and y[i] >= 0 and y[i] < dsize[1] and z[i] >= 0 and z[i] < dsize[2]:
                 #pointLabels[i] = labelImage[y[i], x[i], z[i]];
                 pointLabels[i] = labelImage[x[i], y[i], z[i]];
        
    #write VTK file
    vtkFile = file(filename, 'w')
    vtkFile.write('# vtk DataFile Version 2.0\n');
    vtkFile.write('Unstructured Grid Example\n');
    vtkFile.write('ASCII\n');
    vtkFile.write('DATASET UNSTRUCTURED_GRID\n');
    vtkFile.write("POINTS " + str(nPoint) + " float\n")
    for iPoint in range(nPoint):
        vtkFile.write(str(x[iPoint]).format('%05.20f') + " " +  str(y[iPoint]).format('%05.20f') + " " + str(z[iPoint]).format('%05.20f') + "\n");    
    
    vtkFile.write("CELLS " + str(nPoint) + " " + str(nPoint * 2) + "\n");


    for iPoint in range(nPoint):
        vtkFile.write("1 " + str(iPoint) + "\n");
    vtkFile.write("CELL_TYPES " + str(nPoint) + "\n");
    for iPoint in range(0, nPoint):
        vtkFile.write("1 \n");
    #vtkFile.write("\n");
    vtkFile.write("POINT_DATA " + str(nPoint) + "\n");
    vtkFile.write('SCALARS scalars float 1\n');
    vtkFile.write("LOOKUP_TABLE default\n");
    for iLabel in pointLabels:
        vtkFile.write(str(int(iLabel)) + " ");
        #vtkFile.write("1 ")
    vtkFile.write("\n");
    vtkFile.close();   
    
    return filename;
xml = etree.fromstring(xmlstring);

e1 = [x for x in xml.iter('{*}xyz-Table_X_Overlap')];
e2 = [x for x in xml.iter('{*}xyz-Table_Y_Overlap')];

e10 = e1[0];
e10.attrib['Value'] = '10.0';

xmlstring2 = etree.tostring(xml, pretty_print = False);



fnout = 'test.tif';

import ClearMap.IO as io
data = io.readData(fn);
tout = t.TiffWriter(fnout);
tout.save(data, description=xmlstring2);



tn = t.TiffFile(fnout)

p = tn.pages[0];
o = p.tags['image_description']
xmlstring = o.value;

xml = etree.fromstring(xmlstring);

e0 = [x for x in xml.iter('{*}xyz-Table_X_Overlap')];
e0[0].attrib