Пример #1
0
    def setZ(self, z):
        def handleClic(curve, edge):
            print curve, "edge", edge
            curve.setPen(pg.mkPen({'color': (0, 0, 1), 'width': 6}))

        pen = pg.mkPen({'color': "FF0", 'width': 6})
        vb = self.viewBox

        def vbAdd(item):
            if item.zValue() < vb.zValue():
                item.setZValue(vb.zValue() + 1)

            #item.setParentItem(vb.childGroup)
            vb.addedItems.append(item)

        dataSlice = self.dataDict['raw'][:, :, z]
        labelSlice = self.labels[:, :, z]

        self.imgItem.setImage(dataSlice)
        self.imgItem.update()
        #self.viewBox.update()

        #for curve in self.curves:
        #    self.viewBox.removeItem(curve)
        self.curves = []

        slicesEdges = vigra.graphs.SliceEdges(self.rag)

        with vigra.Timer("findSlicesEdges"):
            slicesEdges.findSlicesEdges(labelSlice)

        with vigra.Timer("render them"):

            visibleEdges = slicesEdges.visibleEdges()
            for edge in visibleEdges:

                edge = long(edge)
                lineIds = slicesEdges.lineIds(edge)
                totalLine = []
                for lid in lineIds:
                    line = slicesEdges.line(long(lid)).astype('float32') / 2.0
                    totalLine.append(line)
                    totalLine.append([[float('nan'), float('nan')]])
                totalLine = numpy.concatenate(totalLine, axis=0)
                lx = totalLine[:, 0]
                ly = totalLine[:, 1]
                #with vigra.Timer("get curve"):
                curve = pg.PlotCurveItem(clickable=True, parent=vb.childGroup)
                curve.setPen(pen)
                curve.setData(lx, ly, connect="finite")
                curve.sigClicked.connect(partial(handleClic, edge=edge))

                self.curves.append(curve)
                #with vigra.Timer("add curve"):
                #vbAdd(curve)
                #self.viewBox.update()
        self.allCurves.setCurves(self.curves)
        self.viewBox.addItem(self.allCurves)
        with vigra.Timer("update auto range"):
            vb.updateAutoRange()
Пример #2
0
    def _update_rendering(self):
        if not self.render:
            return

        op = self.topLevelOperatorView
        if not self._renderMgr.ready:
            shape = op.InputData.meta.shape[1:4]
            self._renderMgr.setup(op.InputData.meta.shape[1:4])

        # remove nonexistent objects
        self._shownObjects3D = dict((k, v) for k, v in self._shownObjects3D.iteritems()
                                    if k in op.MST.value.object_lut.keys())

        lut = numpy.zeros(op.MST.value.nodeNum+1, dtype=numpy.int32)
        for name, label in self._shownObjects3D.iteritems():
            objectSupervoxels = op.MST.value.objects[name]
            lut[objectSupervoxels] = label

        if self._showSegmentationIn3D:
            # Add segmentation as label, which is green
            lut[:] = numpy.where( op.MST.value.getSuperVoxelSeg() == 2, self._segmentation_3d_label, lut )
        import vigra
        with vigra.Timer("remapping"):          
            self._renderMgr.volume = lut[op.MST.value.supervoxelUint32] # (Advanced indexing)
        self._update_colors()
        self._renderMgr.update()
Пример #3
0
    def setZ(self, z):

        vb = self.viewBox
        dataSlice = self.dataDict['raw'][:, :, z]
        labelSlice = self.labels[:, :, z]

        self.imgItem.setImage(dataSlice)
        self.imgItem.update()

        slicesEdges = vigra.graphs.SliceEdges(self.rag)

        with vigra.Timer("find slices"):
            slicesEdges.findSlicesEdges(labelSlice)

        if self.allCurves is None:
            self.allCurves = AllCurves()
            #self.viewBox.addItem(self.allCurves)

        self.curves = self.allCurves.curves

        ci = 0

        #with vigra.Timer("build curves"):
        visibleEdges = slicesEdges.visibleEdges()
        for edge in visibleEdges:

            edge = long(edge)
            lineIds = slicesEdges.lineIds(edge)
            totalLine = []
            for lid in lineIds:
                line = slicesEdges.line(long(lid)).astype('float32') / 2.0
                totalLine.append(line)
                totalLine.append([[float('nan'), float('nan')]])
            totalLine = numpy.concatenate(totalLine, axis=0)
            lx = totalLine[:, 0]
            ly = totalLine[:, 1]

            try:
                curve = self.curves[ci]
            except:
                curve = BvPlotCurveItem(clickable=False, parent=self.imgItem)
                self.curves.append(curve)
                curve.viewer = self
                #self.viewBox.addItem(curve,ignoreBounds=True)
            ci += 1

            leftTop = numpy.nanmin(lx), numpy.nanmin(ly)
            rightBottom = numpy.nanmax(lx), numpy.nanmax(ly)
            curve.setToolTip("id%d" % edge)
            curve.bRect.setCoords(leftTop[0], leftTop[1], rightBottom[0],
                                  rightBottom[1])
            curve.edge = edge
            curve.setPen(self.getPen(edge))
            curve.setData(lx, ly, connect="finite")
            curve.setVisible(True)

        self.nCurves = ci
        print self.nCurves
        for cii in range(ci, len(self.curves)):
            self.curves[cii].setVisible(False)
Пример #4
0
def largeSeedWatershed(raw, pmap, seeds, membraneWidth=7.0, visu=False):

    blockShape = (100, ) * 3
    cOpts = vbw.convOpts

    pmap = numpy.require(pmap, dtype='float32')

    with vigra.Timer("add noise"):
        mx = pmap.max()
        sshape = pmap.squeeze().shape
        noise = numpy.random.rand(*sshape) * (0.05 * mx)
        noise = vigra.taggedView(noise.astype('float32'), 'xyz')
        opts = vbw.convOpts(blockShape=blockShape, sigma=4.0)
        noise = vbw.gaussianSmooth(noise, options=opts)
        pmap += noise

    with vigra.Timer("smoothed tie breaker"):
        # compute a smoothed map as tie breaker
        opts = vbw.convOpts(blockShape=blockShape, sigma=membraneWidth / 2.0)
        gaussianSmoothedPmap = vbw.gaussianSmooth(pmap, options=opts)
        addEps = 0.3
        growingMap = gaussianSmoothedPmap
        growingMap *= addEps

        #
        opts = vbw.convOpts(blockShape=blockShape, sigma=membraneWidth / 7.50)
        notSoMuch = vbw.gaussianSmooth(pmap, options=opts)

        # get the actual growing mapz
        growingMap += notSoMuch
        growingMap /= 1.0 + addEps

    with vigra.Timer("watershedsNew"):
        # do the actual watershed
        growingMap = vigra.taggedView(growingMap, 'xyz')
        seeds = numpy.require(seeds, dtype='uint32')
        seeds = vigra.taggedView(seeds, 'xyz')
        seg, nSeg = vigra.analysis.watershedsNew(image=growingMap, seeds=seeds)

    if visu:
        grayData = [(raw, "raw"), (pmap, "pmap"), (growingMap, "growingMap")]
        segData = [(seeds, "seeds"), (seg, "seg")]
        skneuro.addHocViewer(grayData, segData)

    return seg, nSeg
Пример #5
0
 def scrolled(d):
     if d > 0:
         d = 5
     else:
         d = -5
     if self.ndim == 3:
         newSlice = min(self.nSlices - 1, self.currentSlice - d)
         newSlice = max(0, newSlice)
         self.currentSlice = newSlice
         with vigra.Timer("scroll"):
             self.setZ(self.currentSlice)
Пример #6
0
def computeRag(oversegFile, dset):

    oversegFileH5 = h5py.File(oversegFile)
    oversegDset = oversegFileH5[dset]
    try:
        maxLabel = oversegDset.attrs['maxLabel']
        #print("load max label")
    except:
        print("compute max label")
        maxLabel = h5Max(oversegDset)
        oversegDset.attrs['maxLabel'] = maxLabel

    oversegFileH5.close()

    print("max label", maxLabel)

    nLabels = maxLabel + 1

    cs = nifty.hdf5.CacheSettings()
    cs.hashTabelSize = 977
    cs.nBytes = 990000000
    cs.rddc = 0.5

    h5File = nifty.hdf5.openFile(oversegFile, cs)
    labelsArray = nifty.hdf5.Hdf5ArrayUInt32(h5File, dset)

    ragFile = os.path.join(workDir, 'rag.h5')
    if not os.path.isfile(ragFile):

        with vigra.Timer("rag"):
            gridRag = nifty.graph.rag.gridRagHdf5(labelsArray,
                                                  nLabels,
                                                  blockShape=[150, 150, 150],
                                                  numberOfThreads=20)

        print("serialize")
        serialization = gridRag.serialize()

        print("save serialization")
        ragH5File = h5py.File(ragFile)
        ragH5File['data'] = serialization
        ragH5File.close()
    else:
        ragH5File = h5py.File(ragFile, 'r')
        serialization = ragH5File['data']
        gridRag = nifty.graph.rag.gridRagHdf5(labelsArray,
                                              nLabels,
                                              serialization=serialization)
        ragH5File.close()

    return gridRag
Пример #7
0
if True:
    #pmapH5 = h5py.File(pmapPath,'r')
    #pmapDset = pmapH5['data']

    print("load pmap in ram (since we have enough")
    pmapArray = pmapDset[:, :, :, :]
    pmapArray = pmapArray[:, :, :, 0]

    heightMapH5 = h5py.File(heightMapFile, 'w')
    heightMapDset = heightMapH5.create_dataset('data',
                                               shape=pmapDset.shape[0:3],
                                               chunks=(100, 100, 100),
                                               dtype='float32')

    with vigra.Timer("st"):
        params = {
            "axisResolution": [2.0, 2.0, 2.0],
            "featureBlockShape": [400, 400, 400],
            "invertPmap": False,
            #"roiBegin": [0,0,0],
            #"roiEnd":   [1000,1000,1000],
            #"nWorkers":1,
        }
        print(pmapDset.shape)
        membraneOverseg3D(pmapArray, heightMapDset, **params)

    heightMapH5.close()
    pmapH5.close()

if True:
Пример #8
0
        options.blockShape = (64, ) * 3

        print "hessianEv"
        ev = bw.hessianOfGaussianFirstEigenvalue(volume, options)
        print ev.shape, ev.dtype

        options.stdDev = (4.5, ) * 3
        print "smooth"
        ev = bw.gaussianSmooth(ev, options)

        vigra.impex.writeHDF5(ev, "growmap.h5", 'data')

    else:
        ev = vigra.impex.readHDF5("growmap.h5", 'data')

    with vigra.Timer("watershedsNew"):
        labels, nseg = vigra.analysis.unionFindWatershed3D(ev, (100, 100, 100))

    print "gridGraph"
    gridGraph = graphs.gridGraph(labels.shape)
    rag = graphs.regionAdjacencyGraph(gridGraph, labels)
    rag.writeHDF5("rag.h5", 'data')
else:
    rag = vigra.graphs.loadGridRagHDF5("rag.h5", 'data')
    labels = rag.labels

app = QtGui.QApplication([])


class DownCtrl(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
Пример #9
0
def oversegPipeline(pmapPath,
                    outFolder,
                    outName,
                    pixelResNm=2.0,
                    reduceBy=[5, 10, 20]):

    scale = pixelResNm / 2.0

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

    class DummyWithStatement:
        def __enter__(self):
            pass

        def __exit__(self, type, value, traceback):
            pass

    def makeBall(r):
        size = 2 * r + 1

        mask = numpy.zeros([size] * 3)

        for x0 in range(-1 * r, r + 1):
            for x1 in range(-1 * r, r + 1):
                for x2 in range(-1 * r, r + 1):

                    if math.sqrt(x0**2 + x1**2 + x2**2) <= r:
                        mask[x0 + r, x1 + r, x2 + r] = 1

        return mask, (r, r, r)

    def membraneOverseg3D(pmapDset, heightMapDset, **kwargs):

        axisResolution = kwargs.get("axisResolution", ['4nm'] * 3)
        featureBlockShape = kwargs.get("featureBlockShape", ['100'] * 3)
        shape = pmapDset.shape[0:3]

        roiBegin = kwargs.get("roiBegin", [0] * 3)
        roiEnd = kwargs.get("roiEnd", shape)
        nWorkers = kwargs.get("nWorkers", cpu_count())
        invertPmap = kwargs.get("invertPmap", False)

        blocking = nifty.tools.blocking(roiBegin=roiBegin,
                                        roiEnd=roiEnd,
                                        blockShape=featureBlockShape)
        margin = [45, 45, 45]

        def pmapToHeightMap(pmap):

            r = int(min(3.0 * scale, 1.0) + 0.5)
            footprint, origin = makeBall(r=r)

            blurredSmall = fastfilters.gaussianSmoothing(pmap, 1.0 * scale)
            blurredLarge = fastfilters.gaussianSmoothing(pmap, 6.0 * scale)
            blurredSuperLarge = fastfilters.gaussianSmoothing(
                pmap, 10.0 * scale)

            combined = pmap + blurredSuperLarge * 0.3 + 0.15 * blurredLarge + 0.1 * blurredSmall

            r = int(min(5.0 * scale, 1.0) + 0.5)
            footprint, origin = makeBall(r=r)

            combined = scipy.ndimage.percentile_filter(
                input=combined,
                #size=(20,20,20),
                footprint=footprint,
                #origin=origin,
                mode='reflect',
                percentile=50.0)

            if False:
                nifty.viewer.view3D(pmap, show=False, title='pm', cmap='gray')
                nifty.viewer.view3D(medianImg,
                                    show=False,
                                    title='medianImg',
                                    cmap='gray')
                nifty.viewer.view3D(combined,
                                    show=False,
                                    title='combined',
                                    cmap='gray')
                pylab.show()

            return combined

        numberOfBlocks = blocking.numberOfBlocks
        lock = threading.Lock()
        noLock = DummyWithStatement()
        done = [0]

        for blockIndex in range(numberOfBlocks):
            blockWithHalo = blocking.getBlockWithHalo(blockIndex, margin)
            block = blocking.getBlock(blockIndex)
            outerBlock = blockWithHalo.outerBlock
            innerBlock = blockWithHalo.innerBlock
            innerBlockLocal = blockWithHalo.innerBlockLocal

            #print("B ",block.begin, block.end)
            #print("O ",outerBlock.begin, outerBlock.end)
            #print("I ",innerBlock.begin, innerBlock.end)
            #print("IL",innerBlockLocal.begin, innerBlockLocal.end)

        with nifty.tools.progressBar(size=numberOfBlocks) as bar:

            def f(blockIndex):
                blockWithHalo = blocking.getBlockWithHalo(
                    blockIndex, margin, margin)
                #print "fo"
                outerBlock = blockWithHalo.outerBlock
                outerSlicing = nifty.tools.getSlicing(outerBlock.begin,
                                                      outerBlock.end)
                b, e = outerBlock.begin, outerBlock.end
                #print bi
                #

                maybeLock = [lock, noLock][isinstance(pmapDset, numpy.ndarray)]

                with maybeLock:
                    if invertPmap:
                        outerPmap = 1.0 - pmapDset[b[0]:e[0], b[1]:e[1],
                                                   b[2]:e[2]]
                    else:
                        outerPmap = pmapDset[b[0]:e[0], b[1]:e[1], b[2]:e[2]]

                heightMap = pmapToHeightMap(outerPmap)

                #
                innerBlockLocal = blockWithHalo.innerBlockLocal
                b, e = innerBlockLocal.begin, innerBlockLocal.end
                innerHeightMap = heightMap[b[0]:e[0], b[1]:e[1], b[2]:e[2]]

                b, e = blockWithHalo.innerBlock.begin, blockWithHalo.innerBlock.end

                if isinstance(heightMapDset, numpy.ndarray):
                    #print("NOT LOCKED")
                    heightMapDset[b[0]:e[0], b[1]:e[1],
                                  b[2]:e[2]] = innerHeightMap
                    with lock:
                        done[0] += 1
                        bar.update(done[0])

                else:
                    with lock:
                        #print("locked",b,e)

                        heightMapDset[b[0]:e[0], b[1]:e[1],
                                      b[2]:e[2]] = innerHeightMap
                        done[0] += 1
                        bar.update(done[0])

            nifty.tools.parallelForEach(range(blocking.numberOfBlocks),
                                        f=f,
                                        nWorkers=nWorkers)

    def makeSmallerSegNifty(oseg, volume_feat, reduceBySetttings,
                            wardnessSettings, baseFilename):
        import nifty
        import nifty.graph
        import nifty.graph.rag
        import nifty.graph.agglo

        nrag = nifty.graph.rag
        nagglo = nifty.graph.agglo

        print("overseg in c order starting at zero")
        oseg = numpy.require(oseg, dtype='uint32', requirements='C')
        oseg -= 1

        print("make rag")
        rag = nifty.graph.rag.gridRag(oseg)

        print("volfeatshape")
        vFeat = numpy.require(volume_feat, dtype='float32', requirements='C')

        print("accumulate means and counts")
        eFeatures, nFeatures = nrag.accumulateMeanAndLength(
            rag, vFeat, [100, 100, 100], -1)

        eMeans = eFeatures[:, 0]
        eSizes = eFeatures[:, 1]
        nSizes = nFeatures[:, 1]

        print("get clusterPolicy")

        for wardness in wardnessSettings:
            for reduceBy in reduceBySetttings:

                print("wardness", wardness, "reduceBy", reduceBy)

                numberOfNodesStop = int(
                    float(rag.numberOfNodes) / float(reduceBy) + 0.5)
                numberOfNodesStop = max(1, numberOfNodesStop)
                numberOfNodesStop = min(rag.numberOfNodes, numberOfNodesStop)

                clusterPolicy = nagglo.edgeWeightedClusterPolicy(
                    graph=rag,
                    edgeIndicators=eMeans,
                    edgeSizes=eSizes,
                    nodeSizes=nSizes,
                    numberOfNodesStop=numberOfNodesStop,
                    sizeRegularizer=float(wardness))

                print("do clustering")
                agglomerativeClustering = nagglo.agglomerativeClustering(
                    clusterPolicy)
                agglomerativeClustering.run()
                seg = agglomerativeClustering.result()  #out=[1,2,3,4])

                print("make seg dense")
                dseg = nifty.tools.makeDense(seg)

                print(dseg.dtype, type(dseg))

                print("project to pixels")
                pixelData = nrag.projectScalarNodeDataToPixels(
                    rag, dseg.astype('uint32'))
                print("done")

                outFilename = baseFilename + str(wardness) + "_" + str(
                    reduceBy) + ".h5"

                agglosegH5 = h5py.File(outFilename, 'w')
                agglosegDset = agglosegH5.create_dataset('data',
                                                         shape=oseg.shape[0:3],
                                                         chunks=(100, 100,
                                                                 100),
                                                         dtype='uint32',
                                                         compression="gzip")
                agglosegDset[:, :, :] = pixelData
                agglosegH5.close()

    if True:

        pmapH5 = h5py.File(pmapPath, 'r')
        pmapDset = pmapH5['data']
        shape = list(pmapDset.shape[0:3])

        subset = None
        sshape = (subset, ) * 3
        heightMapFile = os.path.join(outFolder, "%s_heightMap.h5" % outName)
        heightMapH5 = h5py.File(heightMapFile, 'w')
        heightMapDset = heightMapH5.create_dataset('data',
                                                   shape=shape,
                                                   dtype='float32',
                                                   chunks=(64, ) * 3)

        print("bra", heightMapFile)
        #sys.exit(1)

        print("load pmap in ram (since we have enough")
        if subset is None:
            pmapArray = pmapDset[:, :, :]
        else:
            pmapArray = pmapDset[0:subset, 0:subset, 0:subset]
        pmapH5.close()

        if shape[0] >= 2000:
            featureBlockShape = [350, 350, 350]
        elif shape[0] < 2000 and shape[0] >= 1000:
            featureBlockShape = [250, 250, 250]
        elif shape[0] < 1000 and shape[0] >= 500:
            featureBlockShape = [100, 100, 100]

        with vigra.Timer("st"):
            params = {
                "axisResolution": [2.0, 2.0, 2.0],
                "featureBlockShape": featureBlockShape,
                "invertPmap": False,
                #"roiBegin": [0,0,0],
                #"roiEnd":   sshape
                #"nWorkers":1,
            }
            membraneOverseg3D(pmapArray, heightMapDset, **params)

        heightMapH5.close()

    if True:

        with vigra.Timer("read hmap"):
            heightMapFile = os.path.join(outFolder,
                                         "%s_heightMap.h5" % outName)
            heightMapH5 = h5py.File(heightMapFile, 'r')
            heightMapDset = heightMapH5['data']
            heightMap = heightMapDset[:, :, :]
            shape = list(heightMap.shape)
            heightMapH5.close()

        with vigra.Timer("do overseg"):
            overseg, nseg = vigra.analysis.unionFindWatershed3D(
                heightMap.T, blockShape=(100, 100, 100))
            overseg = overseg.T

        with vigra.Timer("write res"):
            oversegFile = os.path.join(outFolder,
                                       "%s_ufd_overseg.h5" % outName)
            oversegH5 = h5py.File(oversegFile, 'w')
            oversegDset = oversegH5.create_dataset('data',
                                                   shape=shape,
                                                   data=overseg,
                                                   chunks=(64, ) * 3,
                                                   compression='gzip')
            oversegDset.attrs['nseg'] = nseg
            oversegH5.close()

    #if False:
    #    import nifty.hdf5
    #    with vigra.Timer("ws"):
    #        nifty.hdf5.unionFindWatershed(heightMapFile,'data', oversegFile,'data',[100,100,100])

    if True:

        heightMapFile = os.path.join(outFolder, "%s_heightMap.h5" % outName)
        heightMapH5 = h5py.File(heightMapFile, 'r')
        heightMapDset = heightMapH5['data']

        oversegFile = os.path.join(outFolder, "%s_ufd_overseg.h5" % outName)
        oversegH5 = h5py.File(oversegFile, 'r')
        oversegDset = oversegH5['data']

        print("read hmap")
        heightMap = heightMapDset[:, :, :]

        print("read oseg")
        overseg = oversegDset[:, :, :]

        heightMapH5.close()
        oversegH5.close()

        print("make smaller")
        agglosegBaseFile = os.path.join(outFolder, "%s_agglo_seg" % outName)
        makeSmallerSegNifty(overseg, heightMap, reduceBy, [0.3],
                            agglosegBaseFile)
Пример #10
0
def singleBlockPipeline(rawFile, pmapFile, oversegFile, ilpFile, outFolder, outName, sigmas=(2.0,4.0,8.0), filtOnRaw=True, filtOnPmap=True, blockShape=(254,)*3,
    minSegSize=None ,beta=0.5, stages=[1,1,1]):


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


    # save stuff
    def saveRes(data,name):
        f5 = h5py.File(os.path.join(outFolder,'%s_%s.h5'%(outName,name)),'w')
        f5['data'] = data
        f5.close()

    # load stuff
    def loadRes(name):
        f5 = h5py.File(os.path.join(outFolder,'%s_%s.h5'%(outName,name)),'r')
        data = f5['data'][:]
        f5.close()
        return data








    rawH5File  = h5py.File(rawFile,'r')
    osegH5File = h5py.File(oversegFile,'r')
    pmapH5File = h5py.File(pmapFile,'r')


    # dsets
    rawDset  = rawH5File['data']  
    osegDset = osegH5File['data'] 
    pmapDset = pmapH5File['data'] 

    print("raw",rawDset.shape)
    print("oseg",osegDset.shape)
    print("pmap",pmapDset.shape)

    shape = rawDset.shape

    #load labels
    uvIdsTrain, labelsTrain = getIlpLabels(ilpFile)


    blocking = nifty.tools.blocking(roiBegin=[0]*3, roiEnd=shape, 
                                    blockShape=blockShape)
    nBlocks = blocking.numberOfBlocks
    lock = threading.Lock()
    lock2 = threading.Lock()
    blockRes = [None]*nBlocks


    if len(sigmas) > 0:
        maxSigma = max(sigmas)
    



    if stages[0] == 1:
        print("STAGE 0")
        filtList = []
        filtListPmap = []

        bs = 100
        hs = [s//2 for s in shape]
        raw  = rawDset[hs[0] : hs[0] + bs, hs[1] : hs[1] + bs, hs[2] : hs[2] + bs].astype('float32')
        pm  =  pmapDset[hs[0] : hs[0] + bs, hs[1] : hs[1] + bs, hs[2] : hs[2] + bs].astype('float32')

        for sigma in sigmas:

            if filtOnRaw:
                filtList.append(GaussianGradientMagnitude(raw=raw, sigma=sigma))
                filtList.append(LaplacianOfGaussian(raw=raw, sigma=sigma))
                filtList.append(HessianOfGaussianEv(raw=raw, sigma=sigma))
                filtList.append(GaussianSmoothing(raw=raw, sigma=sigma))

            if filtOnPmap:
                filtListPmap.append(GaussianGradientMagnitude(raw=pm, sigma=sigma))
                filtListPmap.append(LaplacianOfGaussian(raw=pm, sigma=sigma))
                filtListPmap.append(HessianOfGaussianEv(raw=pm, sigma=sigma))
                filtListPmap.append(GaussianSmoothing(raw=pm, sigma=sigma))




        def f(blockIndex):

            # labels halo
            blockWithBorder =  blocking.getBlockWithHalo(blockIndex, [0,0,0],[1,1,1])
            outerBlock = blockWithBorder.outerBlock
            bAcc, eAcc = outerBlock.begin, outerBlock.end


            # filters
            
            filterRes= []

            if len(sigmas) > 0 and (filtOnRaw or filtOnPmap):
                halo = [int(round(3.5*maxSigma + 3.0))] * 3
            else:
                halo = [0]*3

            blockWithBorder = blocking.addHalo(outerBlock, halo)
            outerBlock = blockWithBorder.outerBlock
            innerBlockLocal = blockWithBorder.innerBlockLocal
            bFilt, eFilt = outerBlock.begin, outerBlock.end
            bFiltCore, eFiltCore = innerBlockLocal.begin, innerBlockLocal.end

            with lock:
                rawFilt  = rawDset[bFilt[0]:eFilt[0], bFilt[1]:eFilt[1], bFilt[2]:eFilt[2]].astype('float32')
                pmFilt   = pmapDset[bFilt[0]:eFilt[0], bFilt[1]:eFilt[1], bFilt[2]:eFilt[2]].astype('float32')
                oseg     = osegDset[bAcc[0]:eAcc[0], bAcc[1]:eAcc[1], bAcc[2]:eAcc[2]]

            raw = rawFilt[bFiltCore[0]:eFiltCore[0], bFiltCore[1]:eFiltCore[1], bFiltCore[2]:eFiltCore[2]]/255.0
            pmap = pmFilt[bFiltCore[0]:eFiltCore[0], bFiltCore[1]:eFiltCore[1], bFiltCore[2]:eFiltCore[2]]/255.0


            filterRes.append(raw[...,None])
            filterRes.append(pmap[...,None])

            for filt in filtList:
                res = filt(rawFilt)
                res = res[bFiltCore[0]:eFiltCore[0], bFiltCore[1]:eFiltCore[1], bFiltCore[2]:eFiltCore[2],:]
                filterRes.append(res)

            for filt in filtListPmap:
                res = filt(pmFilt)
                res = res[bFiltCore[0]:eFiltCore[0], bFiltCore[1]:eFiltCore[1], bFiltCore[2]:eFiltCore[2],:]
                filterRes.append(res)




            #for vd in filterRes:
            #    print(vd.shape,vd.dtype)
            #    
            voxelData = numpy.concatenate(filterRes, axis=3)
            voxelData = numpy.require(voxelData,requirements=['C_CONTIGUOUS'],dtype='float32')

            #print(voxelData.strides)
            #last axis is continous
            assert voxelData.strides[3] == 4

            blockData = nseg.BlockData(blocking=blocking, blockIndex=blockIndex, 
                numberOfChannels=voxelData.shape[3],
                numberOfBins=20)

            blockData.accumulate(oseg, voxelData)

            blockRes[blockIndex] = blockData

        nifty.tools.parallelForEach(iterable=range(nBlocks), f=f, 
                                    nWorkers=-1, showBar=True,
                                    size=nBlocks, name="ComputeFeatures")


        def mergePairwise(toMerge):

            while(len(toMerge) != 1):
                newList = []

                l = len(toMerge)
                pairs  = (l+1)//2
                print(l)
                for p in range(pairs):
                    p0 = p*2
                    p1 = p*2 + 1

                    if(p1<l):
                        b0 = toMerge[p0]
                        b1 = toMerge[p1]
                        b0.merge(b1)
                        newList.append(b0)
                    else:
                        newList.append(toMerge[p0])

                toMerge = newList
            return toMerge[0]

        mergedRes = mergePairwise(blockRes) 



        uvIds = mergedRes.uvIds()
        allFeat = mergedRes.extractFeatures()

        nodeCounts   = mergedRes.nodeCounts()
        edgeCounts   = mergedRes.edgeCounts()

        # mean pmap
        nodeMeansP   = mergedRes.nodeMeans(1)
        edgeMeansP   = mergedRes.edgeMeans(1)



        saveRes(nodeCounts,'nodeCounts')
        saveRes(edgeCounts,'edgeCounts')
        saveRes(nodeMeansP,'nodeMeansP')
        saveRes(edgeMeansP,'edgeMeansP')
        saveRes(uvIds,'uvIds')
        saveRes(allFeat,'allFeat')
            

    if stages[1] == 1:

        from sklearn.ensemble import RandomForestClassifier



        #load stuff
        uvIds = loadRes('uvIds')
        allFeat = loadRes('allFeat')

        nodeCounts = loadRes('nodeCounts')
        edgeCounts = loadRes('edgeCounts')
        nodeMeansP = loadRes('nodeMeansP')
        edgeMeansP = loadRes('edgeMeansP')


        # get the graph
        numberOfNodes = uvIds.max() + 1
        g = nifty.graph.UndirectedGraph(numberOfNodes)
        g.insertEdges(uvIds)

        print("graph",g)

        if False:
            print("ucm feat")
            print("edgeMeansP",edgeMeansP)
            ucmFeat = nagglo.ucmFeatures(graph=g, edgeIndicators=edgeMeansP, 
                               edgeSizes=edgeCounts, nodeSizes=nodeCounts,
                               sizeRegularizers=numpy.arange(0.025,1,0.1))

            allFeat = numpy.concatenate([allFeat, ucmFeat],axis=1)
            sys.exit()

        print("rest")
        # mapping
        uvToIndex = dict()
        for i,uv in enumerate(uvIds):   
            uv = long(uv[0]),long(uv[1])
            uvToIndex[uv] = i

        featTrain = []
        lTrain = []

        for i,(uv,l) in enumerate(zip(uvIdsTrain,labelsTrain)):   
            uv = long(uv[0]),long(uv[1])
            if uv not in uvToIndex:
                print(g,uv)
                assert False
            else:
                i = uvToIndex[uv]
                featTrain.append(allFeat[i,:][None,:])
                lTrain.append(l)

        featTrain = numpy.concatenate(featTrain,axis=0).astype('float32')
        labelsTrain = numpy.array(lTrain,dtype='uint32')#[:,None]

        print("training set size",featTrain.shape,labelsTrain.shape)

        # train the rf
        #rf = vigra.learning.RandomForest(treeCount = 10000,
        #    #min_split_node_size=20,
        #    #sample_classes_individually=True
        #)
        #oob = rf.learnRF(featTrain, labelsTrain)

        rf = RandomForestClassifier(n_estimators=1000, n_jobs=40)
        rf.fit(featTrain, labelsTrain)
        p1 = rf.predict_proba(allFeat)[:,1]


        #print("oob",oob)
        #with vigra.Timer("predict"):
        #    p1 = rf.predictProbabilities(allFeat)[:,1]



        # get weights
        eps = 0.00001
        beta = float(beta)

        p1 = numpy.clip(p1, eps, 1.0 - eps)
        p0 = 1.0 - p1
        w = numpy.log(p0/p1) + numpy.log((1.0-beta)/beta)
        w *= edgeCounts

        obj = nifty.graph.multicut.multicutObjective(g, w)


        ilpFactory =  obj.multicutIlpFactory()
        fusionMove = obj.fusionMoveSettings(mcFactory=ilpFactory)
        factory =  obj.fusionMoveBasedFactory(fusionMove=fusionMove,
            stopIfNoImprovement=200)

        solver = ilpFactory.create(obj)

        visitor = obj.multicutVerboseVisitor()

        with vigra.Timer("optimzie"):
            ret = solver.optimize(visitor=visitor)

        ret = nifty.tools.makeDense(ret)
        

        resName = '%s_%s.h5'%(outName,"resultSeg")
        resultH5File = h5py.File(os.path.join(outFolder,resName),'w')
        resultSegDset = resultH5File.create_dataset('data', shape=shape, chunks=(64,)*3, compression='gzip', dtype='uint32')



        blocking = nifty.tools.blocking(roiBegin=[0]*3, roiEnd=shape, 
                                    blockShape=(256,)*3)
        nBlocks = blocking.numberOfBlocks

        def f(blockIndex):

            # labels halo
            block =  blocking.getBlock(blockIndex)
            b, e = block.begin, block.end

            # load files
            with lock:
                oseg = osegDset[b[0]:e[0], b[1]:e[1], b[2]:e[2]]

            relabeld = numpy.take(ret, oseg)

            with lock2:
                resultSegDset[b[0]:e[0], b[1]:e[1], b[2]:e[2]] = relabeld

            #print("relabeld shape",relabeld.shape)





        nifty.tools.parallelForEach(iterable=range(nBlocks), f=f, 
                                    nWorkers=-1, showBar=True,
                                    size=nBlocks, name="Braaa")
        resultH5File.close()
        

    if stages[2] == 1:

        if minSegSize is not None:
            with vigra.Timer("load the segIn"):
                resName = '%s_%s.h5'%(outName,"resultSeg")
                resultH5File = h5py.File(os.path.join(outFolder,resName),'r')
                segIn = resultH5File['data'][:,:,:]#[0:200,0:200,0:200]

            with vigra.Timer("load the pmap"):
                # load the pmap
                pmap = pmapDset[:,:,:,]#[0:200,0:200,0:200]

            with vigra.Timer("make rag"):
                rag = nifty.graph.rag.gridRag(segIn)
                print(rag)
            
            with vigra.Timer("acc"):
                eFeatures, nFeatures = nifty.graph.rag.accumulateMeanAndLength(rag, pmap, [100,100,100],-1)
                eMeans = eFeatures[:,0]
                eSizes = eFeatures[:,1]
                nSizes = nFeatures[:,1]

            with vigra.Timer("cluster"):
                res = nagglo.sizeLimitClustering(graph=rag, nodeSizes=nSizes, minimumNodeSize=int(minSegSize), 
                                          edgeIndicators=eMeans,edgeSizes=eSizes, 
                                          sizeRegularizer=0.001, gamma=0.999,
                                          makeDenseLabels=True)
                #res = numpy.arange(rag.nodeIdUpperBound + 1)

            resName = '%s_%s.h5'%(outName,"outSegSmallRemoved")
            resultH5File = h5py.File(os.path.join(outFolder,resName),'w')
            resultSegDset = resultH5File.create_dataset('data', shape=shape, chunks=(64,)*3, compression='gzip', dtype='uint32')


            with vigra.Timer("makeDense"):
                def f(blockIndex):

                    # labels halo
                    block =  blocking.getBlock(blockIndex)
                    b, e = block.begin, block.end


                    oseg = segIn[b[0]:e[0], b[1]:e[1], b[2]:e[2]]

                    relabeld = numpy.take(res, oseg)

                    with lock:
                        resultSegDset[b[0]:e[0], b[1]:e[1], b[2]:e[2]] = relabeld

                    #print("relabeld shape",relabeld.shape)





                nifty.tools.parallelForEach(iterable=range(nBlocks), f=f, 
                                            nWorkers=-1, showBar=True,
                                            size=nBlocks, name="write results")


    
    rawH5File.close() 
    pmapH5File.close()
    osegH5File.close()
Пример #11
0
def getLargeSeeds(raw, pmap, membraneWidth, threshold, rank, visu=False):

    with vigra.Timer("ballRankOrderFilter"):
        # make mebrane wider with ballRankOrderFilter
        r = int(membraneWidth * 0.35 + 0.5)
        r = max(r, 1)
        widerPmap1 = denoise.ballRankOrderFilter(pmap, radius=r, rank=rank)
        widerPmap = denoise.ballRankOrderFilter(widerPmap1,
                                                radius=r,
                                                rank=rank)
        widerPmap1 = None

    with vigra.Timer("normalize"):
        # renormalize
        widerPmap -= widerPmap.min()
        widerPmap /= widerPmap.max()

    with vigra.Timer("binarize"):
        # binarize
        binaryPmap = numpy.zeros(widerPmap.shape, dtype='uint8')
        binaryPmap[pmap > threshold] = 1
        #widerPmap = None  # save mem
        if visu == False:
            widerPmap = None

    with vigra.Timer("multiBinaryDilation"):
        # morphology
        # 1) make membrane wider by  r
        r = int(membraneWidth * 1.2 + 0.5)
        r = max(r, 2)
        mBinaryPmapA = vigra.filters.multiBinaryDilation(binaryPmap, r)
        #binaryPmap = None  # save mem
        if visu == False:
            binaryPmap = None

    #with  vigra.Timer("multiBinaryErosion"):
    #    # morphology
    #    # 1) make membrane smaller by  r
    #    r = int(membraneWidth*0.1 + 0.5)
    #    r = max(r, 1)
    #    mBinaryPmapB = vigra.filters.multiBinaryErosion(mBinaryPmapA,r)
    #    if visu == False:
    #        mBinaryPmapA = None

    with vigra.Timer("labelVolumeWithBackground"):
        # get seeds
        invertedBinaryPmap = 1 - mBinaryPmapA
        if visu == False:
            mBinaryPmapB = None

        invertedBinaryPmap = numpy.require(invertedBinaryPmap, dtype='uint32')
        ccImg = vigra.analysis.labelVolumeWithBackground(invertedBinaryPmap)

        if visu == False:
            invertedBinaryPmap = None

    if visu:
        grayData = [
            (raw, "raw"),
            (pmap, "pmap"),
            (widerPmap, "widerPmap"),
            (binaryPmap, "binaryPmap"),
            (mBinaryPmapA, "mBinaryPmapA"),
            #(mBinaryPmapB,"mBinaryPmapB"),
        ]
        segData = [
            (ccImg, "seeds"),
            #(seg, "seg")
        ]
        skneuro.addHocViewer(grayData, segData)

    return ccImg
Пример #12
0
    for fi in mInputs:

        name = fi.name()
        voxelData = fi().squeeze()

        if voxelData.ndim == 3:
            voxelData = voxelData[:, :, :, None]

        for c in range(voxelData.shape[3]):

            vd = voxelData[:, :, :, c]
            print "DO ACCUMULATOION :", name

            #raise RuntimeError("check for existence here")

            with vigra.Timer("accumulate features"):
                eFeatures, nFeatures = learning.accumulateFeatures(rag=rag,
                                                                   volume=vd)

            #with vigra.Timer("save edge features"):
            vigra.impex.writeHDF5(eFeatures,
                                  featureDir + name + '_c_%d.h5' % c,
                                  'edgeFeatures')
            vigra.impex.writeHDF5(nFeatures,
                                  featureDir + name + '_c_%d.h5' % c,
                                  'nodeFeatures')

            del eFeatures
            del nFeatures
            del vd
Пример #13
0
labPath = ('segMaskOnly.h5', 'data')  # labeled image path

# load volume
labels = vigra.impex.readHDF5(*labPath).astype(np.uint32)  #[:,:,0:20]
volume = vigra.impex.readHDF5(*imPath)  #[:,:,0:20]

volume = volume.astype('float32')

gridGraph = graphs.gridGraph(labels.shape)
rag = graphs.regionAdjacencyGraph(gridGraph, labels)

featureExtractor = graphs.gridRagFeatureExtractor(rag, labels)

accSimpel = featureExtractor.accumulatedFeaturesSimple

with vigra.Timer("acc raw"):
    miMa = float(volume.min()), float(volume.max())
    accFeatRaw = featureExtractor.accumulatedFeatures(volume, miMa[0], miMa[1])

with vigra.Timer("acc grad"):
    grad = vigra.filters.gaussianGradientMagnitude(volume, 1.0)
    accFeatGrad = accSimpel(grad)

with vigra.Timer("acc hessian"):
    grad = vigra.filters.hessianOfGaussianEigenvalues(volume, 1.0)[:, :, 0]
    accFeatHess = accSimpel(grad)
    gui = vigra.graphs.TinyEdgeLabelGui(rag=rag,
                                        img=volume,
                                        edgeLabels=accFeatHess[:, 0],
                                        labelMode=False)
    gui.startGui()
Пример #14
0
import vigra
from vigra import graphs

filepath = '12003.jpg'
img = vigra.impex.readImage(filepath).astype('float32')
imgM = img.copy()

sigmaS = 1.0
sigmaB = 5.0

with vigra.Timer("ransk"):
    for c in range(3):
        print c
        imgC = img[:, :, c].squeeze()
        imgM[:, :,
             c] = vigra.histogram.gaussianRankOrder(imgC,
                                                    sigmas=(sigmaS, sigmaS,
                                                            sigmaB),
                                                    ranks=(0.5, ),
                                                    bins=255).squeeze()
vigra.imshow(vigra.taggedView(imgM, 'xyc'))
vigra.show()
Пример #15
0
import vigra
from vigra import graphs
from vigra import numpy
from vigra import Timer
from vigra import blockwise as bw

numpy.random.seed(42)

# input
shape = (500, 500, 500)

data = numpy.random.rand(*shape).astype('float32')

print "make options object"
options = bw.BlockwiseConvolutionOptions3D()
print type(options)

sigma = 1.0
options.stdDev = (sigma, ) * 3
options.blockShape = (128, ) * 3

print "stddev", options.stdDev
print "call blockwise filter"

with vigra.Timer("AllThread"):
    res = bw.gaussianSmooth(data, options)
with vigra.Timer("1thread"):
    resRef = vigra.gaussianSmoothing(data, sigma)

print numpy.sum(numpy.abs(res - resRef))
Пример #16
0
import skneuro
import vigra
from volumina.api import Viewer
from PyQt4.QtGui import QApplication
import skneuro.denoising as dn

p = '/home/tbeier/Desktop/blocks/data_sub_3.h5'
data = vigra.impex.readHDF5(p, 'data')[0:200, :,
                                       0:200].astype('uint8').squeeze()

app = QApplication(sys.argv)
v = Viewer()

v.addGrayscaleLayer(data, name="raw")

with vigra.Timer("get ranks 8*2"):
    a = dn.ballRankOrder(data,
                         radius=12,
                         takeNth=2,
                         ranks=(0.01, 0.1, 0.5, 0.9, 0.99),
                         useHistogram=True,
                         minVal=0.0,
                         maxVal=255.0,
                         nBins=256)

v.addGrayscaleLayer(a, name="0.5 8* 2")

v.setWindowTitle("data")
v.showMaximized()
app.exec_()
shape = [s / 2 for s in shape]

timg = vigra.sampling.resize(img, shape)

# get orientation

tensor = vigra.filters.structureTensor(timg, 1.0, 2.0)
eigenrep = vigra.filters.tensorEigenRepresentation2D(tensor)

# print get oriented repulsive edges
edgePmap = eigenrep[:, :, 0].copy()
edgePmap -= edgePmap.min()
edgePmap /= edgePmap.max()

graph = agraph.gridGraph(shape)
model = agraph.liftedMcModel(graph)

with vigra.Timer("add long range edges"):
    agraph.addLongRangeEdges(model, edgePmap, 0.5, 2, 5)

settings = agraph.settingsParallelLiftedMc(model)
solver = agraph.parallelLiftedMc(model, settings)

out = solver.run()

nodeLabels = model.edgeLabelsToNodeLabels(out)
nodeLabels = nodeLabels.reshape(shape)

vigra.imshow(nodeLabels)
vigra.show()
Пример #18
0
import vigra
from vigra import numpy
import h5py
import skneuro
from skneuro import oversegmentation as oseg

with vigra.Timer("load pmap"):
    path = "/home/tbeier/Desktop/blocks/data_sub_3.h5"
    f = h5py.File(path, 'r')
    raw = f['data'][0:200, 0:200, 0:200]
    f.close()


def normalVol(shape, center, scale):
    size = numpy.prod(shape)
    a = numpy.random.normal(center, scale, size).reshape(shape)
    a = vigra.taggedView(a, 'xyz')
    return a


def augmentGaussian(data, lAdd, gAdd, gMult):
    """
        lAdd : sigma of local additive gaussian noise
        gAdd : sigma of global additive gaussian noise
        gMult : sigma of global multiplicative guasian noise
    """
    data = vigra.taggedView(data, 'xyz')
    shape = data.shape

    # local and global additive and multiplicative
    # gaussian noise