def setUp(self): segimg = segImage() binimg = (segimg>0).astype(np.uint8) labels = {0 : np.array([0, 1, 2]), 1 : np.array([0, 1, 1, 2])} rawimg = np.indices(segimg.shape).sum(0).astype(np.float32) rawimg = rawimg.view(vigra.VigraArray) rawimg.axistags = vigra.defaultAxistags('txyzc') g = Graph() features = {"Standard Object Features": {"Count":{}, "RegionCenter":{}, "Coord<Principal<Kurtosis>>":{}, \ "Coord<Minimum>":{}, "Coord<Maximum>":{}, "Mean":{}, \ "Mean in neighborhood":{"margin":(30, 30, 1)}}} sel_features = {"Standard Object Features": {"Count":{}, "Mean":{}, "Mean in neighborhood":{"margin":(30, 30, 1)}, "Variance":{}}} self.extrOp = OpObjectExtraction(graph=g) self.extrOp.BinaryImage.setValue(binimg) self.extrOp.RawImage.setValue(rawimg) self.extrOp.Features.setValue(features) assert self.extrOp.RegionFeatures.ready() self.classOp = OpObjectClassification(graph=g) self.classOp.BinaryImages.setValues([binimg]) self.classOp.SegmentationImages.setValues([segimg]) self.classOp.RawImages.setValues([rawimg]) self.classOp.LabelInputs.setValues([labels]) self.classOp.ObjectFeatures.connect(self.extrOp.RegionFeatures) self.classOp.ComputedFeatureNames.connect(self.extrOp.Features) self.classOp.SelectedFeatures.setValue(sel_features)
def test(self): coords_old = dict() coords_old["Coord<Minimum>"]=numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 0], [15, 15, 15], [22, 22, 22], [31, 31, 31]]) coords_old["Coord<Maximum>"]=numpy.array([[50, 50, 50], [10, 10, 10], [3, 3, 3], [20, 20, 20], [30, 30, 30], [35, 35, 35]]) coords_new = dict() coords_new["Coord<Minimum>"]=numpy.array([[0, 0, 0], [2, 2, 2], [17, 17, 17], [22, 22, 22], [26, 26, 26]]) coords_new["Coord<Maximum>"]=numpy.array([[50, 50, 50], [5, 5, 5], [20, 20, 20], [25, 25, 25], [33, 33, 33]]) labels = numpy.zeros((6,)) labels[0]=0 labels[1]=1 labels[2]=0 labels[3]=2 labels[4]=3 labels[5]=4 newlabels, oldlost, newlost = OpObjectClassification.transferLabels(labels, coords_old, coords_new, None) assert numpy.all(newlabels == [0, 1, 2, 0, 0]) assert len(oldlost["full"])==0 assert len(oldlost["partial"])==1 min4 = coords_old["Coord<Minimum>"][4] max4 = coords_old["Coord<Maximum>"][4] assert numpy.all(oldlost["partial"]==(min4+(max4-min4)//2)) newmin4 = coords_new["Coord<Minimum>"][4] newmax4 = coords_new["Coord<Maximum>"][4] assert numpy.all(newlost["conflict"]==(newmin4+(newmax4-newmin4)/2.))
def setUp(self): g = Graph() rawimg = np.random.randint(0, 255, (2, 10, 10, 10, 1)) binimg = rawimg>100 cc0 = vigra.analysis.labelVolumeWithBackground(binimg[0,:, :, :, 0].astype(np.uint8)) cc1 = vigra.analysis.labelVolumeWithBackground(binimg[1,:, :, :, 0].astype(np.uint8)) nobj = np.max(cc0)+1+np.max(cc1)+1 segmimg = np.zeros(rawimg.shape, cc0.dtype) segmimg[0,:, : , :, 0] = cc0[:] segmimg[1,:, :, :,0] = cc1[:] rawimg = vigra.taggedView(rawimg, 'txyzc') binimg = vigra.taggedView(rawimg, 'txyzc') segmimg = vigra.taggedView(segmimg, 'txyzc') self.features = {"Bad Plugin": {"bad_feature_1": {}, "bad_feature_2":{}}} self.featureArrays = {0: {"Bad Plugin":{"bad_feature_1": np.array(range(nobj)), \ "bad_feature_2": np.array(range(nobj))}}, 1: {"Bad Plugin":{"bad_feature_1": np.array(range(nobj)), \ "bad_feature_2": np.array(range(nobj))}}} self.op = OpObjectClassification(graph = g) self.op.RawImages.setValues([rawimg]) self.op.BinaryImages.setValues([binimg]) self.op.SegmentationImages.setValues([segmimg]) self.op.ObjectFeatures.setValues([self.featureArrays]) self.op.ComputedFeatureNames.setValue(self.features) self.op.SelectedFeatures.setValue(self.features)
def test(self): coords_old = dict() coords_old["Coord<Minimum>"]=numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 0], [15, 15, 15], [22, 22, 22], [31, 31, 31]]) coords_old["Coord<Maximum>"]=numpy.array([[50, 50, 50], [10, 10, 10], [3, 3, 3], [20, 20, 20], [30, 30, 30], [35, 35, 35]]) coords_new = dict() coords_new["Coord<Minimum>"]=numpy.array([[0, 0, 0], [2, 2, 2], [17, 17, 17], [22, 22, 22], [26, 26, 26]]) coords_new["Coord<Maximum>"]=numpy.array([[50, 50, 50], [5, 5, 5], [20, 20, 20], [25, 25, 25], [33, 33, 33]]) labels = numpy.zeros((6,)) labels[0]=0 labels[1]=1 labels[2]=0 labels[3]=2 labels[4]=3 labels[5]=4 newlabels, oldlost, newlost = OpObjectClassification.transferLabels(labels, coords_old, coords_new, None) assert numpy.all(newlabels == [0, 1, 2, 0, 0]) assert len(oldlost["full"])==0 assert len(oldlost["partial"])==1 min4 = coords_old["Coord<Minimum>"][4] max4 = coords_old["Coord<Maximum>"][4] assert numpy.all(oldlost["partial"]==(min4+(max4-min4)/2)) newmin4 = coords_new["Coord<Minimum>"][4] newmax4 = coords_new["Coord<Maximum>"][4] assert numpy.all(newlost["conflict"]==(newmin4+(newmax4-newmin4)/2.))
class TestFullOperator(unittest.TestCase): def setUp(self): segimg = segImage() binimg = (segimg > 0).astype(np.uint8) labels = {0: np.array([0, 1, 2]), 1: np.array([0, 1, 1, 2])} rawimg = np.indices(segimg.shape).sum(0).astype(np.float32) rawimg = rawimg.view(vigra.VigraArray) rawimg.axistags = vigra.defaultAxistags('txyzc') g = Graph() features = {"Standard Object Features": {"Count":{}, "RegionCenter":{}, "Coord<Principal<Kurtosis>>":{}, \ "Coord<Minimum>":{}, "Coord<Maximum>":{}, "Mean":{}, \ "Mean in neighborhood":{"margin":(30, 30, 1)}}} sel_features = { "Standard Object Features": { "Count": {}, "Mean": {}, "Mean in neighborhood": { "margin": (30, 30, 1) }, "Variance": {} } } self.extrOp = OpObjectExtraction(graph=g) self.extrOp.BinaryImage.setValue(binimg) self.extrOp.RawImage.setValue(rawimg) self.extrOp.Features.setValue(features) assert self.extrOp.RegionFeatures.ready() self.classOp = OpObjectClassification(graph=g) self.classOp.BinaryImages.setValues([binimg]) self.classOp.SegmentationImages.setValues([segimg]) self.classOp.RawImages.setValues([rawimg]) self.classOp.LabelInputs.setValues([labels]) self.classOp.LabelsAllowedFlags.resize(1) self.classOp.LabelsAllowedFlags.setValues([True]) self.classOp.ObjectFeatures.connect(self.extrOp.RegionFeatures) self.classOp.ComputedFeatureNames.connect( self.extrOp.ComputedFeatureNames) self.classOp.SelectedFeatures.setValue(sel_features) def test(self): self.assertTrue( self.classOp.Predictions.ready(), "Prediction slot of OpObjectClassification wasn't ready.") probs = self.classOp.PredictionImages[0][:].wait() def testExport(self): table = self.classOp.createExportTable(0, []) print table["Object id"] print table["Default features, RegionCenter_ch_1"] print table["Prediction"] def test_unfavorable_conditions(self): #TODO write test with not so nice input pass
def setUp(self): self.rawimg = numpy.random.randint(0, 256, (1, 200, 200, 1, 1)) self.binimg = self.rawimg > 100 cc = vigra.analysis.labelImageWithBackground( self.binimg.squeeze().astype(numpy.uint8)) nobj = numpy.max(cc) if nobj < 7: # somehow, we randomly didn't get enough objects. # generate some bright points instead self.rawimg[3, 3] = 255 self.rawimg[6, 6] = 255 self.rawimg[10, 10] = 255 self.rawimg[20, 20] = 255 self.rawimg[30, 30] = 255 self.rawimg[50, 50] = 255 self.rawimg[100, 100] = 255 self.rawimg[150, 150] = 255 self.binimg = self.rawimg > 100 cc = vigra.analysis.labelImageWithBackground(self.binimg.squeeze()) self.segmimg = cc.reshape((1, ) + cc.shape + (1, ) + (1, )) self.rawimg = vigra.taggedView(self.rawimg, "txyzc") self.binimg = vigra.taggedView(self.binimg, "txyzc") self.segmimg = vigra.taggedView(self.segmimg, "txyzc") self.features = { "Bad Plugin": { "bad_feature_1": {}, "bad_feature_2": {} } } featureArray1 = numpy.zeros((nobj + 1, 1)) featureArray1[2][0] = numpy.Inf featureArray1[4][0] = numpy.inf featureArray2 = numpy.zeros((nobj + 1, 1)) featureArray2[1][0] = numpy.NaN featureArray2[3][0] = numpy.nan self.featureArrays = { 0: { "Bad Plugin": { "bad_feature_1": featureArray1, "bad_feature_2": featureArray2 } } } g = Graph() self.op = OpObjectClassification(graph=g) self.op.RawImages.setValues([self.rawimg]) self.op.BinaryImages.setValues([self.binimg]) self.op.SegmentationImages.setValues([self.segmimg]) self.op.ObjectFeatures.setValues([self.featureArrays]) self.op.ComputedFeatureNames.setValue(self.features) self.op.SelectedFeatures.setValue(self.features)
def testMultipleImages(self): # Now add the images multiple times and distribute labels # between different copies. Assert same results gr = Graph() opPredict = OpObjectClassification(graph=gr) bin_orig = self.binimg.squeeze() segimg = vigra.analysis.labelVolumeWithBackground(bin_orig) vfeats = vigra.analysis.extractRegionFeatures( segimg.astype(numpy.float32), segimg, ["Count"]) counts = vfeats["Count"] counts[0] = 0 counts = counts.reshape(counts.shape + (1, )) feats = {0: {"Standard Object Features": {"Count": counts}}} featnames = {'Standard Object Features': {'Count': {}}} segimg = segimg.reshape((1, ) + segimg.shape + (1, )) segimg = vigra.taggedView(segimg, 'txyzc') opPredict.RawImages.setValues([self.rawimg, self.rawimg, self.rawimg]) opPredict.BinaryImages.setValues( [self.binimg, self.binimg, self.binimg]) opPredict.SegmentationImages.setValues([segimg, segimg, segimg]) opPredict.ObjectFeatures.setValues([feats, feats, feats]) opPredict.ComputedFeatureNames.setValue(featnames) #run the workflow with the test blocks in the gui, #if you want to see why these labels are chosen #object 11 -small white square #object 27 -large grey square labelArray11 = numpy.zeros((12, )) labelArray11[11] = 1 labelArray27 = numpy.zeros((28, )) labelArray27[27] = 2 labelArray0 = numpy.zeros((2, )) labelDict11 = {0: labelArray11} labelDict27 = {0: labelArray27} labelDict0 = {0: labelArray0} opPredict.LabelInputs.setValues([labelDict11, labelDict0, labelDict27]) #Predict by size selFeatures = {"Standard Object Features": {"Count": {}}} opPredict.SelectedFeatures.setValue(selFeatures) #[0][0] - first image, first time slice predictions = opPredict.Predictions[0][0].wait() predicted_labels = predictions[0] assert predicted_labels[0] == 0 assert numpy.all(predicted_labels[1:16] == 1) assert numpy.all(predicted_labels[16:] == 2)
class TestFullOperator(unittest.TestCase): def setUp(self): segimg = segImage() binimg = (segimg>0).astype(np.uint8) labels = {0 : np.array([0, 1, 2]), 1 : np.array([0, 1, 1, 2])} rawimg = np.indices(segimg.shape).sum(0).astype(np.float32) rawimg = rawimg.view(vigra.VigraArray) rawimg.axistags = vigra.defaultAxistags('txyzc') g = Graph() features = {"Standard Object Features": {"Count":{}, "RegionCenter":{}, "Coord<Principal<Kurtosis>>":{}, \ "Coord<Minimum>":{}, "Coord<Maximum>":{}, "Mean":{}, \ "Mean in neighborhood":{"margin":(30, 30, 1)}}} sel_features = {"Standard Object Features": {"Count":{}, "Mean":{}, "Mean in neighborhood":{"margin":(30, 30, 1)}, "Variance":{}}} self.extrOp = OpObjectExtraction(graph=g) self.extrOp.BinaryImage.setValue(binimg) self.extrOp.RawImage.setValue(rawimg) self.extrOp.Features.setValue(features) assert self.extrOp.RegionFeatures.ready() self.classOp = OpObjectClassification(graph=g) self.classOp.BinaryImages.setValues([binimg]) self.classOp.SegmentationImages.setValues([segimg]) self.classOp.RawImages.setValues([rawimg]) self.classOp.LabelInputs.setValues([labels]) self.classOp.LabelsAllowedFlags.resize(1) self.classOp.LabelsAllowedFlags.setValues([True]) self.classOp.ObjectFeatures.connect(self.extrOp.RegionFeatures) self.classOp.ComputedFeatureNames.connect(self.extrOp.ComputedFeatureNames) self.classOp.SelectedFeatures.setValue(sel_features) def test(self): self.assertTrue(self.classOp.Predictions.ready(), "Prediction slot of OpObjectClassification wasn't ready.") probs = self.classOp.PredictionImages[0][:].wait() def testExport(self): table = self.classOp.createExportTable(0, []) print table["Object id"] print table["Default features, RegionCenter_ch_1"] print table["Prediction"] def test_unfavorable_conditions(self): #TODO write test with not so nice input pass
def testTime(self): # Move the labels around different time steps. # Assert the same results gr = Graph() opExtract = OpObjectExtraction(graph=gr) opPredict = OpObjectClassification(graph=gr) opExtract.RawImage.setValue(self.rawimg) opExtract.BinaryImage.setValue(self.binimg) opExtract.Features.setValue(self.features) opPredict.RawImages.setValues([self.rawimg]) opPredict.BinaryImages.setValues([self.binimg]) opPredict.SegmentationImages.resize(1) opPredict.SegmentationImages[0].connect(opExtract.LabelImage) opPredict.ObjectFeatures.resize(1) opPredict.ObjectFeatures[0].connect(opExtract.RegionFeatures) opPredict.ComputedFeatureNames.connect(opExtract.Features) grT = Graph() opExtractT = OpObjectExtraction(graph=grT) opPredictT = OpObjectClassification(graph=grT) opExtractT.RawImage.setValue(self.rawimgt) opExtractT.BinaryImage.setValue(self.binimgt) opExtractT.Features.setValue(self.features) opPredictT.RawImages.setValues([self.rawimgt]) opPredictT.BinaryImages.setValues([self.binimgt]) opPredictT.SegmentationImages.resize(1) opPredictT.SegmentationImages[0].connect(opExtractT.LabelImage) opPredictT.ObjectFeatures.resize(1) opPredictT.ObjectFeatures[0].connect(opExtractT.RegionFeatures) opPredictT.ComputedFeatureNames.connect(opExtractT.Features) #run the workflow with the test blocks in the gui, #if you want to see why these labels are chosen #object 11 -small white square #object 27 -large grey square labelArray = numpy.zeros((28, )) labelArray[11] = 1 labelArray[27] = 2 labelDict = {0: labelArray} opPredict.LabelInputs.setValues([labelDict]) labelArray11 = numpy.zeros((12, )) labelArray11[11] = 1 labelArray27 = numpy.zeros((28, )) labelArray27[27] = 2 labelArray0 = numpy.zeros((2, )) labelDictT = { 0: labelArray0, 1: labelArray11, 2: labelArray0, 3: labelArray27 } opPredictT.LabelInputs.setValues([labelDictT]) #Predict by size selFeatures = {"Standard Object Features": {"Count": {}}} opPredict.SelectedFeatures.setValue(selFeatures) #[0][0] - first image, first time slice predictions = opPredict.Predictions[0][0].wait() predicted_labels = predictions[0] assert predicted_labels[0] == 0 assert numpy.all(predicted_labels[1:16] == 1) assert numpy.all(predicted_labels[16:] == 2) opPredictT.SelectedFeatures.setValue(selFeatures) predictionsT = opPredictT.Predictions[0][1].wait() predicted_labels_T = predictionsT[1] assert predicted_labels_T[0] == 0 assert numpy.all(predicted_labels_T[1:16] == 1) assert numpy.all(predicted_labels_T[16:] == 2) #Predict by color selFeatures = {"Standard Object Features": {"Mean": {}}} opPredict.SelectedFeatures.setValue(selFeatures) predictions = opPredict.Predictions[0][0].wait() predicted_labels = predictions[0] assert predicted_labels[0] == 0 assert predicted_labels[1] == 1 assert predicted_labels[11] == 1 assert predicted_labels[16] == 1 assert predicted_labels[23] == 1 assert predicted_labels[2] == 2 assert predicted_labels[18] == 2 assert predicted_labels[24] == 2 assert predicted_labels[26] == 2 opPredictT.SelectedFeatures.setValue(selFeatures) predictionsT = opPredictT.Predictions[0][2].wait() predicted_labels_T = predictionsT[2] assert predicted_labels_T[0] == 0 assert predicted_labels_T[1] == 1 assert predicted_labels_T[11] == 1 assert predicted_labels_T[16] == 1 assert predicted_labels_T[23] == 1 assert predicted_labels_T[2] == 2 assert predicted_labels_T[18] == 2 assert predicted_labels_T[24] == 2 assert predicted_labels_T[26] == 2 #Predict by neighborhood color selFeatures = { "Standard Object Features": { "Mean in neighborhood": { "margin": (5, 5, 2) } } } opPredict.SelectedFeatures.setValue(selFeatures) predictions = opPredict.Predictions[0][0].wait() predicted_labels = predictions[0] assert predicted_labels[0] == 0 assert predicted_labels[1] == 1 assert predicted_labels[8] == 1 assert predicted_labels[24] == 1 assert predicted_labels[28] == 1 assert predicted_labels[9] == 2 assert predicted_labels[14] == 2 assert predicted_labels[26] == 2 assert predicted_labels[29] == 2 opPredictT.SelectedFeatures.setValue(selFeatures) predictionsT = opPredictT.Predictions[0][3].wait() predicted_labels_T = predictionsT[3] assert predicted_labels_T[0] == 0 assert predicted_labels_T[1] == 1 assert predicted_labels_T[8] == 1 assert predicted_labels_T[24] == 1 assert predicted_labels_T[28] == 1 assert predicted_labels_T[9] == 2 assert predicted_labels_T[14] == 2 assert predicted_labels_T[26] == 2 assert predicted_labels_T[29] == 2
def setUpClassifier(self): ''' Prepare the classifier that will be used in blockwise prediction. Errors that occur here are real errors (as opposed to test failures). ''' opObjectClassification = OpObjectClassification(graph=self.graph) # STEP 1: connect the operator # raw image data, i.e. cubes with intensity 1 or 1/2 opObjectClassification.RawImages.resize(1) opObjectClassification.RawImages[0].connect(self.rawSource.Output) # threshold images, i.e. cubes with intensity 1 opObjectClassification.BinaryImages.resize(1) opObjectClassification.BinaryImages.connect(self.binarySource.Output) # segmentation images from object extraction opObjectClassification.SegmentationImages.resize(1) opObjectClassification.SegmentationImages[0].connect( self.objExtraction.LabelImage) opObjectClassification.ObjectFeatures.resize(1) opObjectClassification.ObjectFeatures[0].connect( self.objExtraction.RegionFeatures) opObjectClassification.SelectedFeatures.setValue(self.testingFeatures) opObjectClassification.ComputedFeatureNames.connect( self.objExtraction.Features) # STEP 2: simulate labeling object_volume = self.objExtraction.LabelImage[:].wait() raw_5d = self.rawSource.Output[:].wait() # Big: Starting at 0,20,40, etc. # Small: Starting at 10,30,50, etc. # Gray: 0<=x<50 # White: 50<=x<100 # Must provide label names, since that determines the number of classes the operator knows about. opObjectClassification.LabelNames.setValue( ["BigWhite and SmallGray", "SmallWhite and Big Gray"]) # big & white: label 1 big_white_coords = [(0, 60, 0, 0, 0), (0, 60, 20, 0, 0), (0, 60, 0, 20, 0)] for coord in big_white_coords: assert raw_5d[ coord] == 255, "Input data error: expected this pixel to be white, but it was {}".format( raw_5d[coord]) opObjectClassification.assignObjectLabel(0, coord, 1) # small & gray: label 1 small_gray_coords = [(0, 10, 10, 10, 0), (0, 10, 30, 10, 0), (0, 10, 10, 30, 0)] for coord in small_gray_coords: assert raw_5d[ coord] == 255 // 2, "Input data error: expected this pixel to be gray, but it was {}".format( raw_5d[coord]) opObjectClassification.assignObjectLabel(0, coord, 1) # small & white: label 2 small_white_coords = [(0, 70, 10, 10, 0), (0, 70, 30, 10, 0), (0, 70, 10, 30, 0)] for coord in small_white_coords: assert raw_5d[ coord] == 255, "Input data error: expected this pixel to be white, but it was {}".format( raw_5d[coord]) opObjectClassification.assignObjectLabel(0, coord, 2) # big & gray: label 2 big_gray_coords = [(0, 0, 0, 0, 0), (0, 0, 20, 0, 0), (0, 0, 0, 20, 0)] for coord in big_gray_coords: assert raw_5d[ coord] == 255 // 2, "Input data error: expected this pixel to be gray, but it was {}".format( raw_5d[coord]) opObjectClassification.assignObjectLabel(0, coord, 2) assert opObjectClassification.SegmentationImages[0].ready() assert opObjectClassification.NumLabels.value == 2, "Wrong number of labels: {}".format( opObjectClassification.NumLabels.value) prediction_volume = opObjectClassification.PredictionImages[0][:].wait( ) self.prediction_volume = prediction_volume coordE = (0, 60, 20, 20, 0) assert prediction_volume[ coordE] == 1, "Expected object #{} to be predicted as class 1, but got prediction {}".format( object_volume[coordE], prediction_volume[coordE]) coordF = (0, 10, 30, 30, 0) assert prediction_volume[ coordF] == 1, "Expected object #{} to be predicted as class 1, but got prediction {}".format( object_volume[coordF], prediction_volume[coordF]) coordG = (0, 70, 30, 30, 0) assert prediction_volume[ coordG] == 2, "Expected object #{} to be predicted as class 2, but got prediction {}".format( object_volume[coordG], prediction_volume[coordG]) coordH = (0, 20, 20, 20, 0) assert prediction_volume[ coordH] == 2, "Expected object #{} to be predicted as class 2, but got prediction {}".format( object_volume[coordH], prediction_volume[coordH]) self.classifier = opObjectClassification
def setUpClassifier(self): ''' Prepare the classifier that will be used in blockwise prediction. Errors that occur here are real errors (as opposed to test failures). ''' opObjectClassification = OpObjectClassification( graph=self.graph ) # STEP 1: connect the operator # raw image data, i.e. cubes with intensity 1 or 1/2 opObjectClassification.RawImages.resize(1) opObjectClassification.RawImages[0].connect( self.rawSource.Output ) # threshold images, i.e. cubes with intensity 1 opObjectClassification.BinaryImages.resize(1) opObjectClassification.BinaryImages.connect( self.binarySource.Output ) # segmentation images from object extraction opObjectClassification.SegmentationImages.resize(1) opObjectClassification.SegmentationImages[0].connect( self.objExtraction.LabelImage ) opObjectClassification.ObjectFeatures.resize(1) opObjectClassification.ObjectFeatures[0].connect( self.objExtraction.RegionFeatures ) opObjectClassification.SelectedFeatures.setValue(self.testingFeatures) opObjectClassification.ComputedFeatureNames.connect(self.objExtraction.Features) # STEP 2: simulate labeling object_volume = self.objExtraction.LabelImage[:].wait() raw_5d = self.rawSource.Output[:].wait() # Big: Starting at 0,20,40, etc. # Small: Starting at 10,30,50, etc. # Gray: 0<=x<50 # White: 50<=x<100 # Must provide label names, since that determines the number of classes the operator knows about. opObjectClassification.LabelNames.setValue( ["BigWhite and SmallGray", "SmallWhite and Big Gray"] ) # big & white: label 1 big_white_coords = [(0, 60, 0, 0, 0), (0, 60, 20, 0, 0), (0, 60, 0, 20, 0)] for coord in big_white_coords: assert raw_5d[coord] == 255, "Input data error: expected this pixel to be white, but it was {}".format( raw_5d[coord] ) opObjectClassification.assignObjectLabel( 0, coord, 1) # small & gray: label 1 small_gray_coords = [(0, 10, 10, 10, 0), (0, 10, 30, 10, 0), (0, 10, 10, 30, 0)] for coord in small_gray_coords: assert raw_5d[coord] == 255//2, "Input data error: expected this pixel to be gray, but it was {}".format( raw_5d[coord] ) opObjectClassification.assignObjectLabel( 0, coord, 1) # small & white: label 2 small_white_coords = [(0, 70, 10, 10, 0), (0, 70, 30, 10, 0), (0, 70, 10, 30, 0)] for coord in small_white_coords: assert raw_5d[coord] == 255, "Input data error: expected this pixel to be white, but it was {}".format( raw_5d[coord] ) opObjectClassification.assignObjectLabel( 0, coord, 2) # big & gray: label 2 big_gray_coords = [(0, 0, 0, 0, 0), (0, 0, 20, 0, 0), (0, 0, 0, 20, 0)] for coord in big_gray_coords: assert raw_5d[coord] == 255//2, "Input data error: expected this pixel to be gray, but it was {}".format( raw_5d[coord] ) opObjectClassification.assignObjectLabel( 0, coord, 2) assert opObjectClassification.SegmentationImages[0].ready() assert opObjectClassification.NumLabels.value == 2, "Wrong number of labels: {}".format( opObjectClassification.NumLabels.value ) prediction_volume = opObjectClassification.PredictionImages[0][:].wait() self.prediction_volume = prediction_volume coordE = (0, 60, 20, 20, 0) assert prediction_volume[coordE] == 1, "Expected object #{} to be predicted as class 1, but got prediction {}".format( object_volume[coordE], prediction_volume[coordE] ) coordF = (0, 10, 30, 30, 0) assert prediction_volume[coordF] == 1, "Expected object #{} to be predicted as class 1, but got prediction {}".format( object_volume[coordF], prediction_volume[coordF] ) coordG = (0, 70, 30, 30, 0) assert prediction_volume[coordG] == 2, "Expected object #{} to be predicted as class 2, but got prediction {}".format( object_volume[coordG], prediction_volume[coordG] ) coordH = (0, 20, 20, 20, 0) assert prediction_volume[coordH] == 2, "Expected object #{} to be predicted as class 2, but got prediction {}".format( object_volume[coordH], prediction_volume[coordH] ) self.classifier = opObjectClassification
def testEverything(self): graph = Graph() op5Raw = Op5ifyer( graph=graph ) op5Raw.input.setValue( self.test_volume_intensity ) op5Binary = Op5ifyer( graph=graph ) op5Binary.input.setValue( self.test_volume_binary ) opObjectExtraction = OpObjectExtraction( graph=graph ) opObjectExtraction.RawImage.connect( op5Raw.output ) opObjectExtraction.BinaryImage.connect( op5Binary.output ) opObjectExtraction.BackgroundLabels.setValue( [0] ) # Ensure subset assert set(self.testingFeatures) <= set(opObjectExtraction._opRegFeats._featureNames[0]) opObjectClassification = OpObjectClassification( graph=graph ) opObjectClassification.RawImages.resize(1) opObjectClassification.RawImages[0].connect( op5Raw.output ) opObjectClassification.BinaryImages.resize(1) opObjectClassification.BinaryImages.connect( op5Binary.output ) opObjectClassification.SegmentationImages.resize(1) opObjectClassification.SegmentationImages[0].connect( opObjectExtraction.LabelImage ) opObjectClassification.ObjectFeatures.resize(1) opObjectClassification.ObjectFeatures[0].connect( opObjectExtraction.RegionFeatures ) opObjectClassification.LabelsAllowedFlags.setValues( [True] ) object_volume = opObjectExtraction.LabelImage[:].wait() raw_5d = op5Raw.output[:].wait() # Big: Starting at 0,20,40, etc. # Small: Starting at 10,30,50, etc. # Gray: 0<=x<50 # White: 50<=x<100 # big & white: label 1 big_white_coords = [(0, 60, 0, 0, 0), (0, 60, 20, 0, 0), (0, 60, 0, 20, 0)] for coord in big_white_coords: assert raw_5d[coord] == 255, "Input data error: expected this pixel to be white, but it was {}".format( raw_5d[coord] ) opObjectClassification.assignObjectLabel( 0, coord, 1) # small & gray: label 1 small_gray_coords = [(0, 10, 10, 10, 0), (0, 10, 30, 10, 0), (0, 10, 10, 30, 0)] for coord in small_gray_coords: assert raw_5d[coord] == 255/2, "Input data error: expected this pixel to be gray, but it was {}".format( raw_5d[coord] ) opObjectClassification.assignObjectLabel( 0, coord, 1) # small & white: label 2 small_white_coords = [(0, 70, 10, 10, 0), (0, 70, 30, 10, 0), (0, 70, 10, 30, 0)] for coord in small_white_coords: assert raw_5d[coord] == 255, "Input data error: expected this pixel to be white, but it was {}".format( raw_5d[coord] ) opObjectClassification.assignObjectLabel( 0, coord, 2) # big & gray: label 2 big_gray_coords = [(0, 0, 0, 0, 0), (0, 0, 20, 0, 0), (0, 0, 0, 20, 0)] for coord in big_gray_coords: assert raw_5d[coord] == 255/2, "Input data error: expected this pixel to be gray, but it was {}".format( raw_5d[coord] ) opObjectClassification.assignObjectLabel( 0, coord, 2) assert opObjectClassification.SegmentationImages[0].ready() assert opObjectClassification.NumLabels.value == 2 prediction_volume = opObjectClassification.PredictionImages[0][:].wait() coordE = (0, 60, 20, 20, 0) assert prediction_volume[coordE] == 1, "Expected object #{} to be predicted as class 1, but got prediction {}".format( object_volume[coordE], prediction_volume[coordE] ) coordF = (0, 10, 30, 30, 0) assert prediction_volume[coordF] == 1, "Expected object #{} to be predicted as class 1, but got prediction {}".format( object_volume[coordF], prediction_volume[coordF] ) coordG = (0, 70, 30, 30, 0) assert prediction_volume[coordG] == 2, "Expected object #{} to be predicted as class 2, but got prediction {}".format( object_volume[coordG], prediction_volume[coordG] ) coordH = (0, 20, 20, 20, 0) assert prediction_volume[coordH] == 2, "Expected object #{} to be predicted as class 2, but got prediction {}".format( object_volume[coordH], prediction_volume[coordH] ) opBlockwise = OpBlockwiseObjectClassification( graph=Graph() ) # First, try with the default block/halo sizes, which will yield only one block with our test volume opBlockwise.RawImage.connect( opObjectClassification.RawImages[0] ) opBlockwise.BinaryImage.connect( opObjectClassification.BinaryImages[0] ) opBlockwise.Classifier.connect( opObjectClassification.Classifier ) opBlockwise.LabelsCount.connect( opObjectClassification.NumLabels ) assert (opBlockwise.PredictionImage[:].wait() == prediction_volume).all(), \ "Blockwise prediction operator did not produce the same prediction image" \ "as the non-blockwise prediction operator!" # Now try with smaller blocks. warnings.warn("FIXME: This halo choice is sensitive to OpRegionFeatures3d.margin") opBlockwise.BlockShape3dDict.setValue( {'x' : 42, 'y' : 42, 'z' : 42} ) opBlockwise.HaloPadding3dDict.setValue( {'x' : 30, 'y' : 30, 'z' : 30} ) blockwise_prediction_volume = opBlockwise.PredictionImage[:].wait() blockwise_prediction_volume.view(vigra.VigraArray).writeHDF5('/tmp/blockwise_prediction_volume.h5', 'volume') assert (blockwise_prediction_volume == prediction_volume).all(), \ "Blockwise prediction operator did not produce the same prediction image" \ "as the non-blockwise prediction operator!" # If we shrink the halo down to zero, then we get different predictions... warnings.warn("FIXME: This halo choice is sensitive to OpRegionFeatures3d.margin") opBlockwise.BlockShape3dDict.setValue( {'x' : 42, 'y' : 42, 'z' : 42} ) opBlockwise.HaloPadding3dDict.setValue( {'x' : 0, 'y' : 0, 'z' : 0} ) blockwise_prediction_volume = opBlockwise.PredictionImage[:].wait() #blockwise_prediction_volume.view(vigra.VigraArray).writeHDF5('/tmp/blockwise_prediction_volume.h5', 'volume') assert not (blockwise_prediction_volume == prediction_volume).all(), \ "Blockwise prediction operator produced the same prediction image" \ "as the non-blockwise prediction operator, despite having a pathological block/halo combination!"