def testCleanup(self): try: CacheMemoryManager().disable() sampleData = np.random.randint(0, 256, size=(50, 30, 10)) sampleData = sampleData.astype(np.uint8) sampleData = vigra.taggedView(sampleData, axistags='xyz') graph = Graph() opData = OpArrayPiper(graph=graph) opData.Input.setValue(sampleData) op = OpLabelVolume(graph=graph) op.Input.connect(opData.Output) x = op.Output[...].wait() op.Input.disconnect() op.cleanUp() r = weakref.ref(op) del op gc.collect() ref = r() if ref is not None: for i, o in enumerate(gc.get_referrers(ref)): print "Object", i, ":", type(o), ":", o assert r() is None, "OpBlockedArrayCache was not cleaned up correctly" finally: CacheMemoryManager().enable()
def testCleanup(self, cacheMemoryManager): try: cacheMemoryManager.disable() sampleData = np.random.randint(0, 256, size=(50, 30, 10)) sampleData = sampleData.astype(np.uint8) sampleData = vigra.taggedView(sampleData, axistags="xyz") graph = Graph() opData = OpArrayPiper(graph=graph) opData.Input.setValue(sampleData) op = OpLabelVolume(graph=graph) op.Input.connect(opData.Output) x = op.Output[...].wait() op.Input.disconnect() op.cleanUp() r = weakref.ref(op) del op gc.collect() ref = r() if ref is not None: for i, o in enumerate(gc.get_referrers(ref)): print("Object", i, ":", type(o), ":", o) assert r( ) is None, "OpBlockedArrayCache was not cleaned up correctly" finally: cacheMemoryManager.enable()
def testThreadSafety(self): g = Graph() vol = np.zeros((1000, 100, 10)) vol = vol.astype(np.uint8) vol = vigra.taggedView(vol, axistags="xyz") vol[:200, ...] = 1 vol[800:, ...] = 1 opCount = CountExecutes(graph=g) opCount.Input.setValue(vol) op = OpLabelVolume(graph=g) op.Method.setValue(self.method) op.Input.connect(opCount.Output) reqs = [op.CachedOutput[...] for i in range(4)] [r.submit() for r in reqs] [r.block() for r in reqs] assert opCount.numExecutes == 1, "Parallel requests to CachedOutput resulted in recomputation " "({}/4)".format( opCount.numExecutes) # reset numCounts opCount.numExecutes = 0 reqs = [op.Output[250 * i:250 * (i + 1), ...] for i in range(4)] [r.submit() for r in reqs] [r.block() for r in reqs] assert opCount.numExecutes == 4, "Not all requests to Output were computed on demand " "({}/4)".format( opCount.numExecutes)
def testSetDirty(self): g = Graph() vol = np.zeros((5, 2, 200, 100, 10)) vol = vol.astype(np.uint8) vol = vigra.taggedView(vol, axistags="tcxyz") vol[:200, ...] = 1 vol[800:, ...] = 1 op = OpLabelVolume(graph=g) op.Method.setValue(self.method) op.Input.setValue(vol) opCheck = DirtyAssert(graph=g) opCheck.Input.connect(op.Output) opCheck.willBeDirty(1, 1) roi = SubRegion(op.Input, start=(1, 1, 0, 0, 0), stop=(2, 2, 200, 100, 10)) with pytest.raises(PropagateDirtyCalled): op.Input.setDirty(roi) opCheck.Input.disconnect() opCheck.Input.connect(op.CachedOutput) opCheck.willBeDirty(1, 1) out = op.Output[...].wait() roi = SubRegion(op.Input, start=(1, 1, 0, 0, 0), stop=(2, 2, 200, 100, 10)) with pytest.raises(PropagateDirtyCalled): op.Input.setDirty(roi)
def testSingletonZ(self): vol = np.zeros((82, 70, 1, 5, 5), dtype=np.uint8) vol = vigra.taggedView(vol, axistags='xyzct') blocks = np.zeros(vol.shape, dtype=np.uint8) blocks[30:50, 40:60, :, 2:4, 3:5] = 1 blocks[30:50, 40:60, :, 2:4, 0:2] = 2 blocks[60:70, 30:40, :, :, :] = 3 vol[blocks == 1] = 255 vol[blocks == 2] = 255 vol[blocks == 3] = 255 op = OpLabelVolume(graph=Graph()) op.Method.setValue(self.method) op.Input.setValue(vol) out = op.Output[...].wait() tags = op.Output.meta.getTaggedShape() print(tags) out = vigra.taggedView(out, axistags="".join([s for s in tags])) for c in range(out.shape[3]): for t in range(out.shape[4]): print("t={}, c={}".format(t, c)) assertEquivalentLabeling(blocks[..., c, t], out[..., c, t])
def setUp(self): g = Graph() self.features = { NAME: { "Count": {}, "RegionCenter": {}, "Mean": {}, "Coord<Minimum>": {}, "Coord<Maximum>": {}, "Mean in neighborhood": { "margin": (30, 30, 1) }, "Sum": {}, "Sum in neighborhood": { "margin": (30, 30, 1) } } } binimage = binaryImage() self.rawimage = rawImage() self.labelop = OpLabelVolume(graph=g) self.op = OpRegionFeatures(graph=g) self.op.LabelVolume.connect(self.labelop.Output) self.op.RawVolume.setValue(self.rawimage) self.op.Features.setValue(self.features) self.img = binaryImage() self.labelop.Input.setValue(binimage)
def setUp(self): g = Graph() self.labelop = OpLabelVolume(graph=g) self.op = OpRegionFeatures(graph=g) self.op.LabelVolume.connect(self.labelop.Output) # Raw image is arbitrary for our purposes. Just re-use the # label image self.op.RawVolume.connect(self.labelop.Output) self.op.Features.setValue(FEATURES) self.img = binaryImage() self.labelop.Input.setValue(self.img)
def testUnsupported(self): g = Graph() vol = np.zeros((50, 50)) vol = vol.astype(np.int16) vol = vigra.taggedView(vol, axistags="xy") vol[:200, ...] = 1 vol[800:, ...] = 1 op = OpLabelVolume(graph=g) op.Method.setValue(self.method) with pytest.raises(ValueError): op.Input.setValue(vol)
def testSimpleUsage(self): vol = np.random.randint(255, size=(100, 30, 4)) vol = vol.astype(np.uint8) vol = vigra.taggedView(vol, axistags="xyz") op = OpLabelVolume(graph=Graph()) op.Method.setValue(self.method) op.Input.setValue(vol) out = op.Output[...].wait() assert_array_equal(vol.shape, out.shape)
def testConsistency(self): vol = np.zeros((1000, 100, 10)) vol = vol.astype(np.uint8) vol = vigra.taggedView(vol, axistags="xyz") vol[:200, ...] = 1 vol[800:, ...] = 1 op = OpLabelVolume(graph=Graph()) op.Method.setValue(self.method) op.Input.setValue(vol) out1 = op.CachedOutput[:500, ...].wait() out2 = op.CachedOutput[500:, ...].wait() assert out1[0, 0, 0] != out2[499, 0, 0]
def testCorrectLabeling(self): vol = np.zeros((1000, 100, 10)) vol = vol.astype(np.uint8) vol = vigra.taggedView(vol, axistags="xyz") vol[20:40, 10:30, 2:4] = 1 op = OpLabelVolume(graph=Graph()) op.Method.setValue(self.method) op.Input.setValue(vol) out = op.Output[...].wait() tags = op.Output.meta.getTaggedShape() out = vigra.taggedView(out, axistags="".join([s for s in tags])) assertEquivalentLabeling(vol, out)
def __init__(self, *args, **kwargs): super(OpObjectExtraction, self).__init__(*args, **kwargs) # internal operators #TODO BinaryImage is not binary in some workflows, could be made more # efficient self._opLabelVolume = OpLabelVolume(parent=self) self._opLabelVolume.name = "OpObjectExtraction._opLabelVolume" self._opRegFeats = OpCachedRegionFeatures(parent=self) self._opRegFeatsAdaptOutput = OpAdaptTimeListRoi(parent=self) self._opObjectCenterImage = OpObjectCenterImage(parent=self) # connect internal operators self._opLabelVolume.Input.connect(self.BinaryImage) self._opLabelVolume.InputHdf5.connect(self.LabelInputHdf5) self._opLabelVolume.Background.connect(self.BackgroundLabels) self._opRegFeats.RawImage.connect(self.RawImage) self._opRegFeats.LabelImage.connect(self._opLabelVolume.CachedOutput) self._opRegFeats.Features.connect(self.Features) self.RegionFeaturesCleanBlocks.connect(self._opRegFeats.CleanBlocks) self._opRegFeats.CacheInput.connect(self.RegionFeaturesCacheInput) self._opRegFeatsAdaptOutput.Input.connect(self._opRegFeats.Output) self._opObjectCenterImage.BinaryImage.connect(self.BinaryImage) self._opObjectCenterImage.RegionCenters.connect( self._opRegFeatsAdaptOutput.Output) self._opCenterCache = OpCompressedCache(parent=self) self._opCenterCache.name = "OpObjectExtraction._opCenterCache" self._opCenterCache.Input.connect(self._opObjectCenterImage.Output) # connect outputs self.LabelImage.connect(self._opLabelVolume.CachedOutput) self.ObjectCenterImage.connect(self._opCenterCache.Output) self.RegionFeatures.connect(self._opRegFeatsAdaptOutput.Output) self.BlockwiseRegionFeatures.connect(self._opRegFeats.Output) self.LabelOutputHdf5.connect(self._opLabelVolume.OutputHdf5) self.CleanLabelBlocks.connect(self._opLabelVolume.CleanBlocks) self.ComputedFeatureNames.connect(self.Features) # As soon as input data is available, check its constraints self.RawImage.notifyReady(self._checkConstraints) self.BinaryImage.notifyReady(self._checkConstraints)
def __init__(self): # Set memory and number of threads here #lazyflow.request.Request.reset_thread_pool(2) #Memory.setAvailableRam(500*1024**2) binary_img = binaryImage() raw_img = rawImage() g = Graph() # Reorder axis operators self.op5Raw = OpReorderAxes(graph=g) self.op5Raw.AxisOrder.setValue("txyzc") #self.op5Raw.Input.connect(self.opReaderRaw.OutputImage)#self.opReaderRaw.OutputImage) self.op5Raw.Input.setValue(raw_img) self.op5Binary = OpReorderAxes(graph=g) self.op5Binary.AxisOrder.setValue("txyzc") #self.op5Binary.Input.connect(self.opReaderBinary.OutputImage) self.op5Binary.Input.setValue(binary_img) # Cache operators self.opCacheRaw = OpBlockedArrayCache(graph=g) self.opCacheRaw.Input.connect(self.op5Raw.Output) self.opCacheRaw.BlockShape.setValue((1, ) + self.op5Raw.Output.meta.shape[1:]) self.opCacheBinary = OpBlockedArrayCache(graph=g) self.opCacheBinary.Input.connect(self.op5Binary.Output) self.opCacheBinary.BlockShape.setValue( (1, ) + self.op5Binary.Output.meta.shape[1:]) # Label volume operator self.opLabel = OpLabelVolume(graph=g) self.opLabel.Input.connect(self.op5Binary.Output) #self.opLabel.Input.connect(self.opCacheBinary.Output) # Object extraction self.opObjectExtraction = OpObjectExtraction(graph=g) self.opObjectExtraction.RawImage.connect(self.op5Raw.Output) self.opObjectExtraction.BinaryImage.connect(self.op5Binary.Output) self.opObjectExtraction.Features.setValue(FEATURES) # Simplified object features operator (No overhead) self.opObjectFeaturesSimp = OpObjectFeaturesSimplified(graph=g) self.opObjectFeaturesSimp.RawVol.connect(self.opCacheRaw.Output) self.opObjectFeaturesSimp.BinaryVol.connect(self.opCacheBinary.Output)
def testBackground(self): vol = np.zeros((1000, 100, 10)) vol = vol.astype(np.uint8) vol = vigra.taggedView(vol, axistags='xyz') vol[20:40, 10:30, 2:4] = 1 op = OpLabelVolume(graph=Graph()) op.Method.setValue(self.method) op.Background.setValue(1) op.Input.setValue(vol) out = op.Output[...].wait() tags = op.Output.meta.getTaggedShape() out = vigra.taggedView(out, axistags="".join([s for s in tags])) assert np.all(out[20:40, 10:30, 2:4] == 0) assertEquivalentLabeling(1-vol, out) vol = vol.withAxes(*'xyzct') vol = np.concatenate(3*(vol,), axis=3) vol = np.concatenate(4*(vol,), axis=4) vol = vigra.taggedView(vol, axistags='xyzct') assert len(vol.shape) == 5 assert vol.shape[3] == 3 assert vol.shape[4] == 4 #op = OpLabelVolume(graph=Graph()) op.Method.setValue(self.method) bg = np.asarray([[1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 0, 1]], dtype=np.uint8) bg = vigra.taggedView(bg, axistags='ct') assert len(bg.shape) == 2 assert bg.shape[0] == 3 assert bg.shape[1] == 4 op.Background.setValue(bg) op.Input.setValue(vol) for c in range(bg.shape[0]): for t in range(bg.shape[1]): out = op.Output[..., c, t].wait() out = vigra.taggedView(out, axistags=op.Output.meta.axistags) if bg[c, t]: assertEquivalentLabeling(1-vol[..., c, t], out.squeeze()) else: assertEquivalentLabeling(vol[..., c, t], out.squeeze())
def __init__(self, *args, **kwargs): super(_OpThresholdOneLevel, self).__init__(*args, **kwargs) self._opThresholder = OpPixelOperator(parent=self) self._opThresholder.Input.connect(self.InputImage) self._opLabeler = OpLabelVolume(parent=self) self._opLabeler.Method.setValue(_labeling_impl) self._opLabeler.Input.connect(self._opThresholder.Output) self.BeforeSizeFilter.connect(self._opLabeler.Output) self._opFilter = OpFilterLabels(parent=self) self._opFilter.Input.connect(self._opLabeler.Output) self._opFilter.MinLabelSize.connect(self.MinSize) self._opFilter.MaxLabelSize.connect(self.MaxSize) self._opFilter.BinaryOut.setValue(False) self.Output.connect(self._opFilter.Output)
def testNoRecomputation(self): g = Graph() vol = np.zeros((1000, 100, 10)) vol = vol.astype(np.uint8) vol = vigra.taggedView(vol, axistags='xyz') vol[:200, ...] = 1 vol[800:, ...] = 1 opCount = CountExecutes(graph=g) opCount.Input.setValue(vol) op = OpLabelVolume(graph=g) op.Method.setValue(self.method) op.Input.connect(opCount.Output) out1 = op.CachedOutput[:500, ...].wait() out2 = op.CachedOutput[500:, ...].wait() assert opCount.numExecutes == 1
def testCorrectBlocking(self): g = Graph() c, t = 2, 3 vol = np.zeros((1000, 100, 10, 2, 3)) vol = vol.astype(np.uint8) vol = vigra.taggedView(vol, axistags="xyzct") vol[:200, ...] = 1 vol[800:, ...] = 1 opCount = CountExecutes(graph=g) opCount.Input.setValue(vol) op = OpLabelVolume(graph=g) op.Method.setValue(self.method) op.Input.connect(opCount.Output) out1 = op.CachedOutput[:500, ...].wait() out2 = op.CachedOutput[500:, ...].wait() assert opCount.numExecutes == c * t
def testThreadSafety(self): g = Graph() vol = np.zeros((1000, 100, 10)) vol = vol.astype(np.uint8) vol = vigra.taggedView(vol, axistags='xyz') vol[:200, ...] = 1 vol[800:, ...] = 1 opCount = CountExecutes(graph=g) opCount.Input.setValue(vol) opCount.Output.meta["ideal_blockshape"] = vol.shape op = OpLabelVolume(graph=g) op.Method.setValue(self.method) op.Input.connect(opCount.Output) reqs = [op.CachedOutput[...] for i in range(4)] [r.submit() for r in reqs] [r.block() for r in reqs] assert opCount.numExecutes == 1,\ "Parallel requests to CachedOutput resulted in recomputation "\ "({}/4)".format(opCount.numExecutes)
def testDtypeUint16(self): vol = np.zeros((82, 70, 75, 5, 5), dtype=np.uint16) vol = vigra.taggedView(vol, axistags="xyzct") blocks = np.zeros(vol.shape, dtype=np.uint8) blocks[30:50, 40:60, 50:70, 2:4, 3:5] = 1 blocks[30:50, 40:60, 50:70, 2:4, 0:2] = 2 blocks[60:70, 30:40, 10:33, :, :] = 3 vol[blocks == 1] = 255 vol[blocks == 2] = 255 vol[blocks == 3] = 255 op = OpLabelVolume(graph=Graph()) op.Method.setValue(self.method) op.Input.setValue(vol) out = op.Output[...].wait() tags = op.Output.meta.getTaggedShape() out = vigra.taggedView(out, axistags="".join([s for s in tags])) for c in range(out.shape[3]): for t in range(out.shape[4]): assertEquivalentLabeling(blocks[..., c, t], out[..., c, t])
def __init__(self, *args, **kwargs): super(_OpThresholdTwoLevels, self).__init__(*args, **kwargs) self._opLowThresholder = OpPixelOperator(parent=self) self._opLowThresholder.Input.connect(self.InputImage) self._opHighThresholder = OpPixelOperator(parent=self) self._opHighThresholder.Input.connect(self.InputImage) self._opLowLabeler = OpLabelVolume(parent=self) self._opLowLabeler.Method.setValue(_labeling_impl) self._opLowLabeler.Input.connect(self._opLowThresholder.Output) self._opHighLabeler = OpLabelVolume(parent=self) self._opHighLabeler.Method.setValue(_labeling_impl) self._opHighLabeler.Input.connect(self._opHighThresholder.Output) self._opHighLabelSizeFilter = OpFilterLabels(parent=self) self._opHighLabelSizeFilter.Input.connect(self._opHighLabeler.Output) self._opHighLabelSizeFilter.MinLabelSize.connect(self.MinSize) self._opHighLabelSizeFilter.MaxLabelSize.connect(self.MaxSize) self._opHighLabelSizeFilter.BinaryOut.setValue( False) # we do the binarization in opSelectLabels # this way, we get to display pretty colors self._opSelectLabels = OpSelectLabels(parent=self) self._opSelectLabels.BigLabels.connect(self._opLowLabeler.Output) self._opSelectLabels.SmallLabels.connect( self._opHighLabelSizeFilter.Output) # remove the remaining very large objects - # they might still be present in case a big object # was split into many small ones for the higher threshold # and they got reconnected again at lower threshold self._opFinalLabelSizeFilter = OpFilterLabels(parent=self) self._opFinalLabelSizeFilter.Input.connect(self._opSelectLabels.Output) self._opFinalLabelSizeFilter.MinLabelSize.connect(self.MinSize) self._opFinalLabelSizeFilter.MaxLabelSize.connect(self.MaxSize) self._opFinalLabelSizeFilter.BinaryOut.setValue(False) self._opCache = OpCompressedCache(parent=self) self._opCache.name = "_OpThresholdTwoLevels._opCache" self._opCache.InputHdf5.connect(self.InputHdf5) self._opCache.Input.connect(self._opFinalLabelSizeFilter.Output) # Connect our own outputs self.Output.connect(self._opFinalLabelSizeFilter.Output) self.CachedOutput.connect(self._opCache.Output) # Serialization outputs self.CleanBlocks.connect(self._opCache.CleanBlocks) self.OutputHdf5.connect(self._opCache.OutputHdf5) #self.InputChannel.connect( self._opChannelSelector.Output ) # More debug outputs. These all go through their own caches self._opBigRegionCache = OpCompressedCache(parent=self) self._opBigRegionCache.name = "_OpThresholdTwoLevels._opBigRegionCache" self._opBigRegionCache.Input.connect(self._opLowThresholder.Output) self.BigRegions.connect(self._opBigRegionCache.Output) self._opSmallRegionCache = OpCompressedCache(parent=self) self._opSmallRegionCache.name = "_OpThresholdTwoLevels._opSmallRegionCache" self._opSmallRegionCache.Input.connect(self._opHighThresholder.Output) self.SmallRegions.connect(self._opSmallRegionCache.Output) self._opFilteredSmallLabelsCache = OpCompressedCache(parent=self) self._opFilteredSmallLabelsCache.name = "_OpThresholdTwoLevels._opFilteredSmallLabelsCache" self._opFilteredSmallLabelsCache.Input.connect( self._opHighLabelSizeFilter.Output) self._opColorizeSmallLabels = OpColorizeLabels(parent=self) self._opColorizeSmallLabels.Input.connect( self._opFilteredSmallLabelsCache.Output) self.FilteredSmallLabels.connect(self._opColorizeSmallLabels.Output)
self.Output.setDirty(roi) def execute(self, slot, sunbindex, roi, result): self.numExecutes += 1 req = self.Input.get(roi) req.writeInto(result) req.block() class PropagateDirtyCalled(Exception): pass if __name__ == "__main__": method = np.asarray(["lazy"], dtype=np.object) vol = np.random.randint(255, size=(10, 10, 10)) vol = vol.astype(np.uint8) vol = vigra.taggedView(vol, axistags="xyz") op = OpLabelVolume(graph=Graph()) op.Method.setValue(method) op.Input.setValue(vol) out = op.Output[...].wait() assert_array_equal(vol.shape, out.shape) import nose ret = nose.run(defaultTest=__file__)