def test4dAnd5d(self): g = Graph() oper = OpThresholdTwoLevels(graph=g) oper.InputImage.setValue(self.data.withAxes(*"tzyxc")) oper.MinSize.setValue(self.minSize) oper.MaxSize.setValue(self.maxSize) oper.HighThreshold.setValue(self.highThreshold) oper.LowThreshold.setValue(self.lowThreshold) oper.SmootherSigma.setValue(self.sigma) oper.CurOperator.setValue(1) output = oper.Output[:].wait() output = vigra.taggedView(output, axistags=oper.Output.meta.axistags) self.checkResult(output) oper5d = OpThresholdTwoLevels(graph=g) oper5d.InputImage.setValue(self.data5d) oper5d.MinSize.setValue(self.minSize) oper5d.MaxSize.setValue(self.maxSize) oper5d.HighThreshold.setValue(self.highThreshold) oper5d.LowThreshold.setValue(self.lowThreshold) oper5d.SmootherSigma.setValue({"z": 0, "y": 0, "x": 0}) oper5d.Channel.setValue(0) oper5d.CoreChannel.setValue(0) oper5d.CurOperator.setValue(1) out5d = oper5d.Output[0:1, ...].wait() out5d = vigra.taggedView(out5d, axistags=oper5d.Output.meta.axistags) self.checkResult(out5d) numpy.testing.assert_array_equal(out5d, output)
def testChannel(self): shape = (200, 100, 1) tags = 'xyc' zeros = numpy.zeros(shape) ones = numpy.ones(shape) vol = numpy.concatenate((zeros, ones), axis=2) vol = vigra.taggedView(vol, axistags=tags) assert vol.shape[vol.axistags.index('c')] == 2 oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(vol) oper5d.MinSize.setValue(1) oper5d.MaxSize.setValue(zeros.size) oper5d.HighThreshold.setValue(.5) oper5d.LowThreshold.setValue(.5) oper5d.SmootherSigma.setValue({'z': 0.0, 'y': 0.0, 'x': 0.0}) oper5d.CurOperator.setValue(1) # channel 0 oper5d.Channel.setValue(0) oper5d.CoreChannel.setValue(0) out5d = oper5d.Output[:].wait() assert numpy.all(out5d == 0), str(numpy.histogram(out5d)) # channel 0 oper5d.Channel.setValue(1) oper5d.CoreChannel.setValue(1) out5d = oper5d.Output[:].wait() assert numpy.all(out5d > 0), str(numpy.histogram(out5d))
def testReconnect(self): """ Can we connect an image, then replace it with a differently-ordered image? """ predRaw = np.zeros((20, 22, 21, 3), dtype=np.uint32) pred1 = vigra.taggedView(predRaw, axistags='zyxc') pred2 = vigra.taggedView(predRaw, axistags='tyxc') oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(pred1) oper5d.MinSize.setValue(self.minSize) oper5d.MaxSize.setValue(self.maxSize) oper5d.HighThreshold.setValue(self.highThreshold) oper5d.LowThreshold.setValue(self.lowThreshold) oper5d.SmootherSigma.setValue(self.sigma) oper5d.Channel.setValue(0) oper5d.CoreChannel.setValue(0) oper5d.CurOperator.setValue(1) out5d = oper5d.CachedOutput[:].wait() oper5d.InputImage.disconnect( ) # FIXME: Why is this line necessary? Ideally, it shouldn't be... oper5d.InputImage.setValue(pred2) out5d = oper5d.CachedOutput[:].wait()
def testEvenFunnierAxes(self): vol = numpy.random.randint(0, 255, size=(5, 17, 31, 42, 11)) vol = vol.astype(numpy.uint8) # don't care about corner cases vol[vol == 128] = 129 vol[vol == 127] = 126 desiredResult = numpy.where(vol > 128, 1, 0) vol = vigra.taggedView(vol, axistags='xtycz') oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(vol) oper5d.MinSize.setValue(1) oper5d.MaxSize.setValue(vol.size) oper5d.LowThreshold.setValue(128) oper5d.SmootherSigma.setValue({'z': 0.0, 'y': 0.0, 'x': 0.0}) oper5d.CurOperator.setValue(self.curOperator) #for i in range(vol.shape[3]): for i in range(2, 5): # just test some sample slices (runtime :) oper5d.Channel.setValue(i) oper5d.CoreChannel.setValue(i) out5d = oper5d.Output[:].wait() out5d[out5d > 0] = 1 numpy.testing.assert_array_equal(out5d.squeeze(), desiredResult[..., i, :])
def testSingletonx(self): shape = (200, 100, 1) tags = "xyc" zeros = numpy.zeros(shape) ones = numpy.ones(shape) vol = numpy.concatenate((zeros, ones), axis=2) vol = vigra.taggedView(vol, axistags=tags) assert vol.shape[vol.axistags.index("c")] == 2 oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(vol) oper5d.MinSize.setValue(1) oper5d.MaxSize.setValue(zeros.size) oper5d.HighThreshold.setValue(0.5) oper5d.LowThreshold.setValue(0.5) oper5d.Channel.setValue(0) oper5d.CoreChannel.setValue(0) oper5d.CurOperator.setValue(1) # no smoothing oper5d.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0}) out5d = oper5d.Output[:].wait() assert numpy.all(out5d == 0), str(numpy.histogram(out5d)) # smoothing oper5d.SmootherSigma.setValue({"z": 1.0, "y": 1.0, "x": 1.0}) out5d = oper5d.Output[:].wait() assert numpy.all(out5d == 0), str(numpy.histogram(out5d))
def testAgainstOwn(self): g = Graph() oper = OpThresholdTwoLevels(graph=g) oper.InputImage.setValue(self.data5d) oper.MinSize.setValue(self.minSize) oper.MaxSize.setValue(self.maxSize) oper.HighThreshold.setValue(self.highThreshold) oper.LowThreshold.setValue(self.lowThreshold) oper.SmootherSigma.setValue({"z": 0, "y": 0, "x": 0}) oper.CurOperator.setValue(1) output = oper.Output[0, ..., 0].wait() output = vigra.taggedView(output, axistags=oper.Output.meta.axistags) output = output.withAxes(*"zyx") output2 = self.thresholdTwoLevels(self.data5d[0, ..., 0]) output2 = vigra.taggedView(output2, axistags="zyx") ref = output * output2 idx = np.where(ref != output) # print(str(oper.Output.meta.getTaggedShape())) # print(str(output.shape)) # print(str(idx)) # print(str(output[idx])) # print(str(output2[idx])) numpy.testing.assert_array_almost_equal(ref, output)
def testNoOp(self): oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(self.data5d[0:1, ..., 0:1]) oper5d.MinSize.setValue(1) oper5d.MaxSize.setValue(self.data5d.size) oper5d.HighThreshold.setValue(-0.01) oper5d.LowThreshold.setValue(-0.01) out5d = oper5d.Output[:].wait() assert numpy.all(out5d > 0), "{}% elements <= 0".format((out5d <= 0).sum() / float(out5d.size) * 100)
def test4dAnd5d(self): g = Graph() oper = OpThresholdTwoLevels4d(graph=g) oper.InputImage.setValue(self.data) oper.MinSize.setValue(self.minSize) oper.MaxSize.setValue(self.maxSize) oper.HighThreshold.setValue(self.highThreshold) oper.LowThreshold.setValue(self.lowThreshold) #oper.SmootherSigma.setValue(self.sigma) output = oper.Output[:].wait() output = output.reshape((self.nx, self.ny, self.nz)) clusters = generateData((self.nx, self.ny, self.nz, self.nc)) cluster1 = numpy.logical_and(output, clusters[0]) assert numpy.any(cluster1 != 0) == False cluster2 = numpy.logical_and(output, clusters[1]) assert numpy.any(cluster2 != 0) == True cluster3 = numpy.logical_and(output, clusters[2]) assert numpy.any(cluster3 != 0) == False cluster4 = numpy.logical_and(output, clusters[3]) assert numpy.any(cluster4 != 0) == False cluster5 = numpy.logical_and(output, clusters[4]) assert numpy.all(cluster5 == 0) oper.InputImage.setValue(self.dataChannels) output = oper.Output[:].wait() output3d = output.reshape((self.nx, self.ny, self.nz)) cluster1 = numpy.logical_and(output3d, clusters[0]) assert numpy.any(cluster1 != 0) == False cluster2 = numpy.logical_and(output3d, clusters[1]) assert numpy.any(cluster2 != 0) == True cluster3 = numpy.logical_and(output3d, clusters[2]) assert numpy.any(cluster3 != 0) == False cluster4 = numpy.logical_and(output3d, clusters[3]) assert numpy.any(cluster4 != 0) == False oper5d = OpThresholdTwoLevels(graph=g) oper5d.InputImage.setValue(self.data5d) oper5d.MinSize.setValue(self.minSize) oper5d.MaxSize.setValue(self.maxSize) oper5d.HighThreshold.setValue(self.highThreshold) oper5d.LowThreshold.setValue(self.lowThreshold) oper5d.SmootherSigma.setValue(self.sigma) oper5d.Channel.setValue(2) oper5d.CurOperator.setValue(1) out5d = oper5d.Output[:].wait() # output is 4d (xyzc), out5d is 5d (xyzct). assert numpy.all(out5d == output[..., numpy.newaxis])
def testSimpleUsage(self): oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(self.data5d) oper5d.MinSize.setValue(self.minSize) oper5d.MaxSize.setValue(self.maxSize) oper5d.LowThreshold.setValue(0.5) oper5d.SmootherSigma.setValue(self.sigma) oper5d.Channel.setValue(0) oper5d.CoreChannel.setValue(0) oper5d.CurOperator.setValue(self.curOperator) out5d = oper5d.Output[:].wait() numpy.testing.assert_array_equal(out5d.shape, self.data5d.shape)
def testWith(self): oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(self.data5d) oper5d.MinSize.setValue(self.minSize) oper5d.MaxSize.setValue(self.maxSize) oper5d.HighThreshold.setValue(self.highThreshold) oper5d.LowThreshold.setValue(self.lowThreshold) oper5d.SmootherSigma.setValue(self.sigma) oper5d.Channel.setValue(0) oper5d.CurOperator.setValue(2) oper5d.UsePreThreshold.setValue(True) out5d = oper5d.CachedOutput[:].wait() numpy.testing.assert_array_equal(out5d.shape, self.data5d.shape)
def testWrongChannel(self): oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(self.data5d) oper5d.MinSize.setValue(self.minSize) oper5d.MaxSize.setValue(self.maxSize) oper5d.LowThreshold.setValue(0.5) oper5d.SmootherSigma.setValue(self.sigma) # the operator should be able to figure out that this channel index is wrong oper5d.Channel.setValue(15) oper5d.CoreChannel.setValue(15) oper5d.CurOperator.setValue(self.curOperator) with self.assertRaises(Exception): out5d = oper5d.Output[:].wait()
def testSimpleUseCase(self): g = Graph() oper = OpThresholdTwoLevels(graph=g) oper.InputImage.setValue(self.tvol) oper.MinSize.setValue(1) oper.MaxSize.setValue(np.prod(self.tvol.shape[1:])) oper.HighThreshold.setValue(0.7) oper.LowThreshold.setValue(0.3) oper.SmootherSigma.setValue({"z": 0, "y": 0, "x": 0}) oper.CurOperator.setValue(1) output = oper.Output[:].wait() output = vigra.taggedView(output, axistags=oper.Output.meta.axistags) self.checkCorrect(output)
def testStrangeAxesWithout(self): pred = np.zeros((20, 22, 21, 3), dtype=np.uint32) pred = vigra.taggedView(pred, axistags='tyxc') oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(pred) oper5d.MinSize.setValue(self.minSize) oper5d.MaxSize.setValue(self.maxSize) oper5d.HighThreshold.setValue(self.highThreshold) oper5d.LowThreshold.setValue(self.lowThreshold) oper5d.SmootherSigma.setValue(self.sigma) oper5d.Channel.setValue(0) oper5d.CurOperator.setValue(2) oper5d.UsePreThreshold.setValue(False) out5d = oper5d.CachedOutput[:].wait()
def testPropagateDirty(self): g = Graph() oper = OpThresholdTwoLevels(graph=g) oper.InputImage.setValue(self.data5d) oper.MinSize.setValue(1) oper.MaxSize.setValue(np.prod(self.data5d.shape[1:])) oper.HighThreshold.setValue(0.7) oper.LowThreshold.setValue(0.3) oper.SmootherSigma.setValue({"z": 0, "y": 0, "x": 0}) oper.CurOperator.setValue(1) inspector = DirtyAssert(graph=g) inspector.Input.connect(oper.CachedOutput) with self.assertRaises(DirtyAssert.WasSetDirty): oper.CurOperator.setValue(0)
def test(self): g = Graph() oper = OpThresholdOneLevel(graph=g) oper.MinSize.setValue(self.minSize) oper.MaxSize.setValue(self.maxSize) oper.Threshold.setValue(0.5) oper.InputImage.setValue(self.data) output = oper.Output[:].wait() clusters = generateData((self.nx, self.ny, self.nz, self.nc)) cluster1 = numpy.logical_and(output, clusters[0]) assert numpy.any(cluster1 != 0) oper.MinSize.setValue(5) output = oper.Output[:].wait() cluster1 = numpy.logical_and(output, clusters[0]) assert numpy.all(cluster1 == 0) cluster4 = numpy.logical_and(output.squeeze(), clusters[3]) assert numpy.all(cluster4 == 0) cluster5 = numpy.logical_and(output.squeeze(), clusters[2]) assert numpy.all(cluster5 == 0) oper.Threshold.setValue(0.2) output = oper.Output[:].wait() cluster5 = numpy.logical_and(output.squeeze(), clusters[2]) assert numpy.any(cluster5 != 0) oper5d = OpThresholdTwoLevels(graph=g) oper5d.InputImage.setValue(self.data5d) oper5d.MinSize.setValue(oper.MinSize.value) oper5d.MaxSize.setValue(oper.MaxSize.value) oper5d.SingleThreshold.setValue(oper.Threshold.value) oper5d.SmootherSigma.setValue(self.sigma) oper5d.Channel.setValue(0) oper5d.CurOperator.setValue(0) output5d = oper5d.Output[:].wait() tIndex = oper5d.Output.meta.axistags.index('t') slicing = 5 * [slice(None, None, None)] slicing[tIndex] = slice(0, 1, None) assert numpy.all(output5d[slicing].squeeze() == output[:].squeeze())
def testReconnect(self): predRaw = np.zeros((20, 22, 21, 3), dtype=np.uint32) pred1 = vigra.taggedView(predRaw, axistags='xyzc') pred2 = vigra.taggedView(predRaw, axistags='tyxc') oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(pred1) oper5d.MinSize.setValue(self.minSize) oper5d.MaxSize.setValue(self.maxSize) oper5d.HighThreshold.setValue(self.highThreshold) oper5d.LowThreshold.setValue(self.lowThreshold) oper5d.SmootherSigma.setValue(self.sigma) oper5d.Channel.setValue(0) oper5d.CurOperator.setValue(1) out5d = oper5d.CachedOutput[:].wait() oper5d.InputImage.setValue(pred2) out5d = oper5d.CachedOutput[:].wait()
def testNoOp(self): oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(self.data5d) oper5d.MinSize.setValue(1) oper5d.MaxSize.setValue(self.data5d.size) oper5d.HighThreshold.setValue(-0.01) oper5d.LowThreshold.setValue(-0.01) oper5d.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0}) oper5d.Channel.setValue(0) oper5d.CoreChannel.setValue(0) oper5d.CurOperator.setValue(1) out5d = oper5d.Output[:].wait() big = vigra.taggedView(oper5d.BigRegions[:].wait(), axistags=oper5d.BigRegions.meta.axistags) assert numpy.all(big > 0) small = vigra.taggedView(oper5d.SmallRegions[:].wait(), axistags=oper5d.SmallRegions.meta.axistags) assert numpy.all(small > 0) numpy.testing.assert_array_equal(out5d.shape, self.data5d.shape) assert numpy.all(out5d > 0)
def testNoOp(self): oper5d = OpThresholdTwoLevels(graph=Graph()) oper5d.InputImage.setValue(self.data5d) oper5d.MinSize.setValue(1) oper5d.MaxSize.setValue(self.data5d.size) oper5d.LowThreshold.setValue(-0.01) oper5d.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0}) oper5d.Channel.setValue(0) oper5d.CoreChannel.setValue(0) oper5d.CurOperator.setValue(self.curOperator) out5d = oper5d.Output[:].wait() numpy.testing.assert_array_equal(out5d.shape, self.data5d.shape) # the image should be white, because of negative threshold assert numpy.all(out5d > 0), "{}% elements <= 0".format((out5d <= 0).sum() / float(out5d.size) * 100) oper5d.LowThreshold.setValue(1.01) out5d = oper5d.Output[:].wait() # the image should be black, because of threshold larger than 1 assert numpy.all(out5d == 0), "{}% elements > 0".format((out5d > 0).sum() / float(out5d.size) * 100)
def test5d(self): g = Graph() oper = OpThresholdTwoLevels(graph=g) oper.MinSize.setValue(self.minSize) oper.MaxSize.setValue(self.maxSize) oper.SmootherSigma.setValue({'z': 0.0, 'y': 0.0, 'x': 0.0}) oper.LowThreshold.setValue(0.5) oper.InputImage.setValue(self.data5d) output = oper.Output[:].wait() assert numpy.all(output.shape == self.data5d.shape) output = vigra.taggedView(output, axistags=oper.Output.meta.axistags) output = output.withAxes(*'zyx') clusters = self.generateData((self.nz, self.ny, self.nx)) cluster1 = numpy.logical_and(output, clusters[0]) assert numpy.any(cluster1 != 0) oper.MinSize.setValue(5) output = oper.Output[:].wait() output = vigra.taggedView(output, axistags=oper.Output.meta.axistags) output = output.withAxes(*'zyx') cluster1 = numpy.logical_and(output, clusters[0]) assert numpy.all(cluster1 == 0) cluster4 = numpy.logical_and(output.squeeze(), clusters[3]) assert numpy.all(cluster4 == 0) cluster5 = numpy.logical_and(output.squeeze(), clusters[2]) assert numpy.all(cluster5 == 0) oper.LowThreshold.setValue(0.2) output = oper.Output[:].wait() output = vigra.taggedView(output, axistags=oper.Output.meta.axistags) output = output.withAxes(*'zyx') cluster5 = numpy.logical_and(output.squeeze(), clusters[2]) assert numpy.any(cluster5 != 0)
def testFunnyAxes(self): vol = self.data.withAxes(*"ztxcy") g = Graph() oper = OpThresholdTwoLevels(graph=g) oper.MinSize.setValue(self.minSize) oper.MaxSize.setValue(self.maxSize) oper.LowThreshold.setValue(0.5) oper.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0}) oper.InputImage.setValue(vol) output = oper.Output[:, 0, :, 0, :].wait() assert numpy.all(output.shape == vol.shape) clusters = self.generateData((self.nz, self.ny, self.nx)) output = vigra.taggedView(output, axistags=oper.Output.meta.axistags) output = output.withAxes(*"zyx") cluster1 = numpy.logical_and(output, clusters[0]) assert numpy.any(cluster1 != 0) oper.MinSize.setValue(5) output = oper.Output[:, 0, :, 0, :].wait() output = vigra.taggedView(output, axistags=oper.Output.meta.axistags) output = output.withAxes(*"zyx") cluster1 = numpy.logical_and(output, clusters[0]) assert numpy.all(cluster1 == 0) cluster4 = numpy.logical_and(output.squeeze(), clusters[3]) assert numpy.all(cluster4 == 0) cluster5 = numpy.logical_and(output.squeeze(), clusters[2]) assert numpy.all(cluster5 == 0) oper.LowThreshold.setValue(0.2) output = oper.Output[:, 0, :, 0, :].wait() output = vigra.taggedView(output, axistags=oper.Output.meta.axistags) output = output.withAxes(*"zyx") cluster5 = numpy.logical_and(output.squeeze(), clusters[2]) assert numpy.any(cluster5 != 0)