def testAddEdges(self): IV = vigraph.INVALID elist = [[1, 3], [1, 5], [5, 7], [3, 4]] edges = np.array(elist, dtype=np.uint32) nodeIds = np.unique(edges.reshape(-1)) g = vigraph.listGraph() edgeIds = g.addEdges(edges) assert g.edgeNum == len(elist) assert g.edgeNum == len(edgeIds) assert g.nodeNum == len(nodeIds) assert g.maxNodeId == nodeIds.max() for ui, vi in elist: assert g.findEdge(ui, vi) != IV assert g.findEdge(g.nodeFromId(ui), g.nodeFromId(vi)) != IV for nId in nodeIds: nId = int(nId) assert g.nodeFromId(nId) != IV for eId in edgeIds: eId = int(eId) assert g.edgeFromId(eId) != IV findEdges = g.findEdges(edges) assert np.array_equal(findEdges, edgeIds)
def testIters(self): g = vigraph.listGraph() nodes = [n for n in g.nodeIter()] assert len(nodes) == 0 g.addNode(3) nodes = [n for n in g.nodeIter()] assert len(nodes) == 1 assert g.id(nodes[0]) == 3 g.addNode(6) nodes = [n for n in g.nodeIter()] assert len(nodes) == 2 assert g.id(nodes[0]) == 3 assert g.id(nodes[1]) == 6 g.addNode(2) nodes = [n for n in g.nodeIter()] assert len(nodes) == 3 assert g.id(nodes[0]) == 2 assert g.id(nodes[1]) == 3 assert g.id(nodes[2]) == 6
def testIters(self): g = vigraph.listGraph() nodes = [n for n in g.nodeIter()] assert len(nodes)==0 g.addNode(3) nodes = [n for n in g.nodeIter()] assert len(nodes)==1 assert g.id(nodes[0]) == 3 g.addNode(6) nodes = [n for n in g.nodeIter()] assert len(nodes)==2 assert g.id(nodes[0]) == 3 assert g.id(nodes[1]) == 6 g.addNode(2) nodes = [n for n in g.nodeIter()] assert len(nodes)==3 assert g.id(nodes[0]) == 2 assert g.id(nodes[1]) == 3 assert g.id(nodes[2]) == 6
def testAddEdges(self): IV = vigraph.INVALID elist = [ [1,3], [1,5], [5,7], [3,4] ] edges = np.array(elist,dtype=np.uint32) nodeIds = np.unique(edges.reshape(-1)) g = vigraph.listGraph() edgeIds = g.addEdges(edges) assert g.edgeNum == len(elist) assert g.edgeNum == len(edgeIds) assert g.nodeNum == len(nodeIds) assert g.maxNodeId == nodeIds.max() for ui,vi in elist : assert g.findEdge(ui,vi)!=IV assert g.findEdge(g.nodeFromId(ui),g.nodeFromId(vi))!=IV for nId in nodeIds : nId = int(nId) assert g.nodeFromId(nId)!=IV for eId in edgeIds : eId = int(eId) assert g.edgeFromId(eId)!=IV findEdges = g.findEdges(edges) assert np.array_equal(findEdges,edgeIds)
from vigra import numpy from vigra import Timer iEdgeMap = graphs.implicitMeanEdgeMap numpy.random.seed(42) # input shape = [200, 100, 100] data = numpy.random.rand(*shape).astype(numpy.float32) data = vigra.taggedView(data, "xyz") if False: labels = numpy.random.randint(5, size=shape[0] * shape[1] * shape[2]) labels = labels.reshape(shape).astype(numpy.uint32) labels = vigra.analysis.labelVolume(labels) adjListGraph = graphs.listGraph() gridGraph = graphs.gridGraph(shape) rag = graphs.regionAdjacencyGraph(gridGraph, labels) rag.writeHDF5("bla.h5", "dset") else: # load the region adjacency graph rag = graphs.loadGridRagHDF5("bla.h5", "dset") print rag.labels.shape, rag.labels.dtype, type(rag.labels) print "accumulate edge and node features" edgeCuesMean = rag.accumulateEdgeFeatures(iEdgeMap(rag.baseGraph, data)) edgeCuesMean = numpy.array([edgeCuesMean, edgeCuesMean]).T
def clusteringFeatures(ds, segId, extraUV, edgeIndicator, liftedNeighborhood, is_perturb_and_map=False, with_defects=False): print "Computing clustering features for lifted neighborhood", liftedNeighborhood if is_perturb_and_map: print "For perturb and map" else: print "For normal clustering" uvs_local = modified_adjacency(ds, segId) if ( with_defects and ds.defect_slices) else ds._adjacent_segments(segId) n_nodes = uvs_local.max() + 1 # if we have a segmentation mask, remove all the uv ids that link to the ignore segment (==0) if ds.has_seg_mask: where_uv_local = (uvs_local != ds.ignore_seg_value).all(axis=1) uvs_local = uvs_local[where_uv_local] edgeIndicator = edgeIndicator[where_uv_local] assert numpy.sum((extraUV == ds.ignore_seg_value).any(axis=1)) == 0 assert edgeIndicator.shape[0] == uvs_local.shape[0] originalGraph = vgraph.listGraph(n_nodes) originalGraph.addEdges(uvs_local) extraUV = numpy.require(extraUV, dtype='uint32') uvOriginal = originalGraph.uvIds() liftedGraph = vgraph.listGraph(originalGraph.nodeNum) liftedGraph.addEdges(uvOriginal) liftedGraph.addEdges(extraUV) uvLifted = liftedGraph.uvIds() foundEdges = originalGraph.findEdges(uvLifted) foundEdges[foundEdges >= 0] = 0 foundEdges *= -1 nAdditionalEdges = liftedGraph.edgeNum - originalGraph.edgeNum whereLifted = numpy.where(foundEdges == 1)[0].astype('uint32') assert len(whereLifted) == nAdditionalEdges assert foundEdges.sum() == nAdditionalEdges eLen = vgraph.getEdgeLengths(originalGraph) nodeSizes_ = vgraph.getNodeSizes(originalGraph) # FIXME GIL is not lifted for vigra function (probably cluster) def cluster(wardness): edgeLengthsNew = numpy.concatenate( [eLen, numpy.zeros(nAdditionalEdges)]).astype('float32') edgeIndicatorNew = numpy.concatenate( [edgeIndicator, numpy.zeros(nAdditionalEdges)]).astype('float32') nodeSizes = nodeSizes_.copy() nodeLabels = vgraph.graphMap(originalGraph, 'node', dtype='uint32') nodeFeatures = vgraph.graphMap(liftedGraph, 'node', addChannelDim=True) nodeFeatures[:] = 0 outWeight = vgraph.graphMap(liftedGraph, item='edge', dtype=numpy.float32) mg = vgraph.mergeGraph(liftedGraph) clusterOp = vgraph.minEdgeWeightNodeDist(mg, edgeWeights=edgeIndicatorNew, edgeLengths=edgeLengthsNew, nodeFeatures=nodeFeatures, nodeSizes=nodeSizes, nodeLabels=nodeLabels, beta=0.5, metric='l1', wardness=wardness, outWeight=outWeight) clusterOp.setLiftedEdges(whereLifted) hc = vgraph.hierarchicalClustering(clusterOp, nodeNumStopCond=1, buildMergeTreeEncoding=False) hc.cluster() assert mg.edgeNum == 0, str(mg.edgeNum) # FIXME I am disabling these checks for now, but will need to investigate this further # They can fail because with defects and seg mask we can get unconnected pieces in the graph # if we have completely defected slcies, we get a non-connected merge graph # TODO I don't know if this is a problem, if it is, we could first remove them # and then add dummy values later #if not with_defects: # assert mg.nodeNum == 1, str(mg.nodeNum) #else: # # TODO test hypothesis # assert mg.nodeNum == len(ds.defect_slice_list) + 1, "%i, %i" % (mg.nodeNum, len(ds.defect_slice_list) + 1) tweight = edgeIndicatorNew.copy() hc.ucmTransform(tweight) whereInLifted = liftedGraph.findEdges(extraUV) assert whereInLifted.min() >= 0 feat = tweight[whereInLifted] assert feat.shape[0] == extraUV.shape[0] return feat[:, None] wardness_vals = [0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7] # TODO set from ppl parameter with futures.ThreadPoolExecutor(max_workers=8) as executor: tasks = [executor.submit(cluster, w) for w in wardness_vals] allFeat = [t.result() for t in tasks] weights = numpy.concatenate(allFeat, axis=1) mean = numpy.mean(weights, axis=1)[:, None] stddev = numpy.std(weights, axis=1)[:, None] allFeat = numpy.nan_to_num( numpy.concatenate([weights, mean, stddev], axis=1)) allFeat = numpy.require(allFeat, dtype='float32') assert allFeat.shape[0] == extraUV.shape[0] return allFeat
numpy.random.seed(42) # input shape = [200, 100, 100] data = numpy.random.rand(*shape).astype(numpy.float32) data = vigra.taggedView(data,"xyz") if False: labels = numpy.random.randint(5, size=shape[0]*shape[1]*shape[2]) labels = labels.reshape(shape).astype(numpy.uint32) labels = vigra.analysis.labelVolume(labels) adjListGraph = graphs.listGraph() gridGraph = graphs.gridGraph(shape) rag = graphs.regionAdjacencyGraph(gridGraph, labels) rag.writeHDF5("bla.h5", "dset") else : # load the region adjacency graph rag = graphs.loadGridRagHDF5("bla.h5","dset") print rag.labels.shape, rag.labels.dtype ,type(rag.labels) print "accumulate edge and node features"
def testAddNodesWithIds(self): IV = vigraph.INVALID g = vigraph.listGraph() assert g.nodeNum == 0 assert g.edgeNum == 0 assert g.nodeFromId(0) == IV assert g.nodeFromId(1) == IV assert g.nodeFromId(2) == IV assert g.nodeFromId(3) == IV assert g.nodeFromId(4) == IV assert g.nodeFromId(5) == IV assert g.nodeFromId(6) == IV assert g.nodeFromId(7) == IV n5 = g.addNode(5) assert g.id(n5) == 5 assert n5 != IV assert g.nodeNum == 1 assert g.edgeNum == 0 assert g.nodeFromId(0) == IV assert g.nodeFromId(1) == IV assert g.nodeFromId(2) == IV assert g.nodeFromId(3) == IV assert g.nodeFromId(4) == IV assert g.nodeFromId(5) != IV assert g.nodeFromId(6) == IV assert g.nodeFromId(7) == IV n2 = g.addNode(2) assert n2 != IV assert g.nodeNum == 2 assert g.nodeFromId(0) == IV assert g.nodeFromId(1) == IV assert g.nodeFromId(2) != IV assert g.nodeFromId(3) == IV assert g.nodeFromId(4) == IV assert g.nodeFromId(5) != IV assert g.nodeFromId(6) == IV assert g.nodeFromId(7) == IV n6 = g.addNode(6) assert n6 != IV assert g.nodeNum == 3 assert g.nodeFromId(0) == IV assert g.nodeFromId(1) == IV assert g.nodeFromId(2) != IV assert g.nodeFromId(3) == IV assert g.nodeFromId(4) == IV assert g.nodeFromId(5) != IV assert g.nodeFromId(6) != IV assert g.nodeFromId(7) == IV n6 = g.addNode(5) n2 = g.addNode(2) n6 = g.addNode(6) assert g.nodeNum == 3 assert g.nodeFromId(0) == IV assert g.nodeFromId(1) == IV assert g.nodeFromId(2) != IV assert g.nodeFromId(3) == IV assert g.nodeFromId(4) == IV assert g.nodeFromId(5) != IV assert g.nodeFromId(6) != IV assert g.nodeFromId(7) == IV
def testAddNodesWithIds(self): IV = vigraph.INVALID g = vigraph.listGraph() assert g.nodeNum == 0 assert g.edgeNum == 0 assert g.nodeFromId(0)==IV assert g.nodeFromId(1)==IV assert g.nodeFromId(2)==IV assert g.nodeFromId(3)==IV assert g.nodeFromId(4)==IV assert g.nodeFromId(5)==IV assert g.nodeFromId(6)==IV assert g.nodeFromId(7)==IV n5 =g.addNode(5) assert g.id(n5)==5 assert n5!=IV assert g.nodeNum == 1 assert g.edgeNum == 0 assert g.nodeFromId(0)==IV assert g.nodeFromId(1)==IV assert g.nodeFromId(2)==IV assert g.nodeFromId(3)==IV assert g.nodeFromId(4)==IV assert g.nodeFromId(5)!=IV assert g.nodeFromId(6)==IV assert g.nodeFromId(7)==IV n2=g.addNode(2) assert n2!=IV assert g.nodeNum == 2 assert g.nodeFromId(0)==IV assert g.nodeFromId(1)==IV assert g.nodeFromId(2)!=IV assert g.nodeFromId(3)==IV assert g.nodeFromId(4)==IV assert g.nodeFromId(5)!=IV assert g.nodeFromId(6)==IV assert g.nodeFromId(7)==IV n6=g.addNode(6) assert n6!=IV assert g.nodeNum == 3 assert g.nodeFromId(0)==IV assert g.nodeFromId(1)==IV assert g.nodeFromId(2)!=IV assert g.nodeFromId(3)==IV assert g.nodeFromId(4)==IV assert g.nodeFromId(5)!=IV assert g.nodeFromId(6)!=IV assert g.nodeFromId(7)==IV n6=g.addNode(5) n2=g.addNode(2) n6=g.addNode(6) assert g.nodeNum == 3 assert g.nodeFromId(0)==IV assert g.nodeFromId(1)==IV assert g.nodeFromId(2)!=IV assert g.nodeFromId(3)==IV assert g.nodeFromId(4)==IV assert g.nodeFromId(5)!=IV assert g.nodeFromId(6)!=IV assert g.nodeFromId(7)==IV