def testDetectorOmnipresence(self): assert self.op.has(self.op.NHistogramBins.value, method="svm"), "Detector is untrained after call to train()" assert not self.op.has(self.op.NHistogramBins.value + 2, method="svm"), "Wrong bin size trained." op2 = OpDetectMissing(graph=Graph()) assert op2.has(self.op.NHistogramBins.value, method="svm"), "Trained detectors are not global" self.op.reset() assert not self.op.has(self.op.NHistogramBins.value, method="svm"), "Detector not reset." assert not op2.has(self.op.NHistogramBins.value, method="svm"), "Detector not reset globally."
def testDetectorOmnipresence(self): if not havesklearn: raise SkipTest assert self.op.has(self.op.NHistogramBins.value, method='svm'), "Detector is untrained after call to train()" assert not self.op.has(self.op.NHistogramBins.value+2, method='svm'), "Wrong bin size trained." op2 = OpDetectMissing(graph=Graph()) assert op2.has(self.op.NHistogramBins.value, method='svm'), "Trained detectors are not global" self.op.reset() assert not self.op.has(self.op.NHistogramBins.value, method='svm'), "Detector not reset." assert not op2.has(self.op.NHistogramBins.value, method='svm'), "Detector not reset globally."
class TestDetection(unittest.TestCase): def setUp(self): v = _volume() self.op = OpDetectMissing(graph=Graph()) self.op.InputVolume.setValue(v) self.op.PatchSize.setValue(64) self.op.HaloSize.setValue(0) self.op.DetectionMethod.setValue('svm') self.op.train(force=True) def testDetectorOmnipresence(self): if not havesklearn: raise SkipTest assert self.op.has(self.op.NHistogramBins.value, method='svm'), "Detector is untrained after call to train()" assert not self.op.has(self.op.NHistogramBins.value+2, method='svm'), "Wrong bin size trained." op2 = OpDetectMissing(graph=Graph()) assert op2.has(self.op.NHistogramBins.value, method='svm'), "Trained detectors are not global" self.op.reset() assert not self.op.has(self.op.NHistogramBins.value, method='svm'), "Detector not reset." assert not op2.has(self.op.NHistogramBins.value, method='svm'), "Detector not reset globally." def testDetectorPropagation(self): if not havesklearn: raise SkipTest s = self.op.Detector[:].wait() self.op.reset() assert not self.op.has(self.op.NHistogramBins.value, method='svm'), "Detector not reset." self.op.OverloadDetector.setValue(s) assert self.op.has(self.op.NHistogramBins.value, method='svm'), "Detector not loaded." def testClassicDetection(self): self.op.DetectionMethod.setValue('classic') self.op.PatchSize.setValue(1) self.op.HaloSize.setValue(0) (v,m,_) = _singleMissingLayer(layer=15, nx=1,ny=1,nz=50,method='linear') self.op.InputVolume.setValue(v) assert_array_equal(self.op.Output[:].wait().view(type=np.ndarray),\ m.view(type=np.ndarray),\ err_msg="input with single black layer") def testSVMDetection(self): if not havesklearn: raise SkipTest self.op.DetectionMethod.setValue('svm') self.op.PatchSize.setValue(1) self.op.HaloSize.setValue(0) (v,m,_) = _singleMissingLayer(layer=15, nx=1,ny=1,nz=50,method='linear') self.op.InputVolume.setValue(v) assert_array_equal(self.op.Output[:].wait().view(type=np.ndarray),\ m.view(type=np.ndarray),\ err_msg="input with single black layer") def testSVMDetectionWithHalo(self): nBlack = 15 if not havesklearn: raise SkipTest self.op.DetectionMethod.setValue('svm') self.op.PatchSize.setValue(5) self.op.HaloSize.setValue(2) (v,m,_) = _singleMissingLayer(layer=nBlack, nx=10,ny=15,nz=50,method='linear') self.op.InputVolume.setValue(v) assert_array_equal(self.op.Output[:].wait()[...,nBlack].view(type=np.ndarray),\ m[...,nBlack].view(type=np.ndarray),\ err_msg="input with single black layer") def testSVMWithHalo(self): if not havesklearn: raise SkipTest self.op.DetectionMethod.setValue('svm') self.op.PatchSize.setValue(2) self.op.HaloSize.setValue(1) (v,m,_) = _singleMissingLayer(layer=15, nx=4,ny=4,nz=50,method='linear') self.op.InputVolume.setValue(v) assert_array_equal(self.op.Output[:].wait().view(type=np.ndarray),\ m.view(type=np.ndarray),\ err_msg="input with single black layer") def testSingleMissingLayer(self): (v,m,_) = _singleMissingLayer(layer=15, nx=64,ny=64,nz=50,method='linear') self.op.InputVolume.setValue(v) assert_array_equal(self.op.Output[:].wait().view(type=np.ndarray),\ m.view(type=np.ndarray),\ err_msg="input with single black layer") def testDoubleMissingLayer(self): (v,m,_) = _singleMissingLayer(layer=15, nx=64,ny=64,nz=50,method='linear') (v2,m2,_) = _singleMissingLayer(layer=35, nx=64,ny=64,nz=50,method='linear') m2[np.where(m2==1)] = 2 self.op.InputVolume.setValue(np.sqrt(v*v2)) assert_array_equal(self.op.Output[:].wait().view(type=np.ndarray)>0,\ (m+m2).view(type=np.ndarray)>0,\ err_msg="input with two black layers") def test4D(self): vol = vigra.VigraArray( np.ones((10,64,64,3)), axistags=vigra.defaultAxistags('cxyz') ) self.op.InputVolume.setValue(vol) self.op.Output[:].wait() def test5D(self): vol = vigra.VigraArray( np.ones((15,64,10,3,64)), axistags=vigra.defaultAxistags('cxzty') ) self.op.InputVolume.setValue(vol) self.op.Output[:].wait() def testPersistence(self): dumpedString = self.op.dumps() self.op.loads(dumpedString) def testPatchify(self): from lazyflow.operators.opDetectMissingData import _patchify as patchify X = np.vander(np.arange(2,5)) ''' results in X = array([[ 4, 2, 1], [ 9, 3, 1], [16, 4, 1]]) ''' (patches,slices) = patchify(X,1,1) expected = [np.array([[4,2],[9,3]]), \ np.array([[4,2,1],[9,3,1]]), \ np.array([[2,1],[3,1]]), \ np.array([[4,2],[9,3],[16,4]]), \ np.array([[4,2,1],[9,3,1],[16,4,1]]), \ np.array([[2,1],[3,1],[4,1]]), \ np.array([[9,3],[16,4]]), \ np.array([[9,3,1],[16,4,1]]), \ np.array([[3,1],[4,1]])] expSlices = [(slice(0,1),slice(0,1)), \ (slice(0,1), slice(1,2)), \ (slice(0,1), slice(2,3)), \ (slice(1,2), slice(0,1)), \ (slice(1,2), slice(1,2)), \ (slice(1,2), slice(2,3)), \ (slice(2,3), slice(0,1)), \ (slice(2,3), slice(1,2)), \ (slice(2,3), slice(2,3))] for ep, s in zip(expected,expSlices): #check if patch is in the result has = False for i,p in enumerate(patches): if np.all(p == ep): has = True # check if slice is ok self.assertEqual(s, slices[i]) assert has, "Mising patch {}".format(ep) pass def testPatchDetection(self): vol = vigra.taggedView(np.ones((5,5), dtype=np.uint8)*128, axistags=vigra.defaultAxistags('xy')) vol[2:5,2:5] = 0 expected = np.zeros((5,5)) expected[3:5,3:5] = 1 self.op.PatchSize.setValue(2) self.op.HaloSize.setValue(1) self.op.DetectionMethod.setValue('classic') self.op.InputVolume.setValue(vol) out = self.op.Output[:].wait() assert_array_equal(expected[3:5,3:5], out[3:5,3:5])
class TestDetection(unittest.TestCase): def setup_method(self, method): v = _volume() self.op = OpDetectMissing(graph=Graph()) self.op.InputVolume.setValue(v) self.op.PatchSize.setValue(64) self.op.HaloSize.setValue(0) self.op.DetectionMethod.setValue("svm") self.op.train(force=True) def testDetectorOmnipresence(self): if not havesklearn: raise SkipTest assert self.op.has(self.op.NHistogramBins.value, method="svm"), "Detector is untrained after call to train()" assert not self.op.has(self.op.NHistogramBins.value + 2, method="svm"), "Wrong bin size trained." op2 = OpDetectMissing(graph=Graph()) assert op2.has(self.op.NHistogramBins.value, method="svm"), "Trained detectors are not global" self.op.reset() assert not self.op.has(self.op.NHistogramBins.value, method="svm"), "Detector not reset." assert not op2.has(self.op.NHistogramBins.value, method="svm"), "Detector not reset globally." def testDetectorPropagation(self): if not havesklearn: raise SkipTest s = self.op.Detector[:].wait() self.op.reset() assert not self.op.has(self.op.NHistogramBins.value, method="svm"), "Detector not reset." self.op.OverloadDetector.setValue(s) assert self.op.has(self.op.NHistogramBins.value, method="svm"), "Detector not loaded." def testClassicDetection(self): self.op.DetectionMethod.setValue("classic") self.op.PatchSize.setValue(1) self.op.HaloSize.setValue(0) (v, m, _) = _singleMissingLayer(layer=15, nx=1, ny=1, nz=50, method="linear") self.op.InputVolume.setValue(v) assert_array_equal( self.op.Output[:].wait().view(type=np.ndarray), m.view(type=np.ndarray), err_msg="input with single black layer", ) def testSVMDetection(self): if not havesklearn: raise SkipTest self.op.DetectionMethod.setValue("svm") self.op.PatchSize.setValue(1) self.op.HaloSize.setValue(0) (v, m, _) = _singleMissingLayer(layer=15, nx=1, ny=1, nz=50, method="linear") self.op.InputVolume.setValue(v) assert_array_equal( self.op.Output[:].wait().view(type=np.ndarray), m.view(type=np.ndarray), err_msg="input with single black layer", ) def testSVMDetectionWithHalo(self): nBlack = 15 if not havesklearn: raise SkipTest self.op.DetectionMethod.setValue("svm") self.op.PatchSize.setValue(5) self.op.HaloSize.setValue(2) (v, m, _) = _singleMissingLayer(layer=nBlack, nx=10, ny=15, nz=50, method="linear") self.op.InputVolume.setValue(v) assert_array_equal( self.op.Output[:].wait()[..., nBlack].view(type=np.ndarray), m[..., nBlack].view(type=np.ndarray), err_msg="input with single black layer", ) def testSVMWithHalo(self): if not havesklearn: raise SkipTest self.op.DetectionMethod.setValue("svm") self.op.PatchSize.setValue(2) self.op.HaloSize.setValue(1) (v, m, _) = _singleMissingLayer(layer=15, nx=4, ny=4, nz=50, method="linear") self.op.InputVolume.setValue(v) assert_array_equal( self.op.Output[:].wait().view(type=np.ndarray), m.view(type=np.ndarray), err_msg="input with single black layer", ) def testSingleMissingLayer(self): (v, m, _) = _singleMissingLayer(layer=15, nx=64, ny=64, nz=50, method="linear") self.op.InputVolume.setValue(v) assert_array_equal( self.op.Output[:].wait().view(type=np.ndarray), m.view(type=np.ndarray), err_msg="input with single black layer", ) def testDoubleMissingLayer(self): (v, m, _) = _singleMissingLayer(layer=15, nx=64, ny=64, nz=50, method="linear") (v2, m2, _) = _singleMissingLayer(layer=35, nx=64, ny=64, nz=50, method="linear") m2[np.where(m2 == 1)] = 2 self.op.InputVolume.setValue(np.sqrt(v * v2)) assert_array_equal( self.op.Output[:].wait().view(type=np.ndarray) > 0, (m + m2).view(type=np.ndarray) > 0, err_msg="input with two black layers", ) def test4D(self): vol = vigra.VigraArray(np.ones((10, 64, 64, 3)), axistags=vigra.defaultAxistags("cxyz")) self.op.InputVolume.setValue(vol) self.op.Output[:].wait() def test5D(self): vol = vigra.VigraArray(np.ones((15, 64, 10, 3, 64)), axistags=vigra.defaultAxistags("cxzty")) self.op.InputVolume.setValue(vol) self.op.Output[:].wait() def testPersistence(self): dumpedString = self.op.dumps() self.op.loads(dumpedString) def testPatchify(self): from lazyflow.operators.opDetectMissingData import _patchify as patchify X = np.vander(np.arange(2, 5)) """ results in X = array([[ 4, 2, 1], [ 9, 3, 1], [16, 4, 1]]) """ (patches, slices) = patchify(X, 1, 1) expected = [ np.array([[4, 2], [9, 3]]), np.array([[4, 2, 1], [9, 3, 1]]), np.array([[2, 1], [3, 1]]), np.array([[4, 2], [9, 3], [16, 4]]), np.array([[4, 2, 1], [9, 3, 1], [16, 4, 1]]), np.array([[2, 1], [3, 1], [4, 1]]), np.array([[9, 3], [16, 4]]), np.array([[9, 3, 1], [16, 4, 1]]), np.array([[3, 1], [4, 1]]), ] expSlices = [ (slice(0, 1), slice(0, 1)), (slice(0, 1), slice(1, 2)), (slice(0, 1), slice(2, 3)), (slice(1, 2), slice(0, 1)), (slice(1, 2), slice(1, 2)), (slice(1, 2), slice(2, 3)), (slice(2, 3), slice(0, 1)), (slice(2, 3), slice(1, 2)), (slice(2, 3), slice(2, 3)), ] for ep, s in zip(expected, expSlices): # check if patch is in the result has = False for i, p in enumerate(patches): if np.array_equal(p, ep): has = True # check if slice is ok self.assertEqual(s, slices[i]) assert has, "Mising patch {}".format(ep) pass def testPatchDetection(self): vol = vigra.taggedView(np.ones((5, 5), dtype=np.uint8) * 128, axistags=vigra.defaultAxistags("xy")) vol[2:5, 2:5] = 0 expected = np.zeros((5, 5)) expected[3:5, 3:5] = 1 self.op.PatchSize.setValue(2) self.op.HaloSize.setValue(1) self.op.DetectionMethod.setValue("classic") self.op.InputVolume.setValue(vol) out = self.op.Output[:].wait() assert_array_equal(expected[3:5, 3:5], out[3:5, 3:5])