From 1b4eb25c093a55e5344fba2d63540d66e1d96605 Mon Sep 17 00:00:00 2001 From: Stefan Hahmann Date: Thu, 27 Jun 2024 18:14:49 +0200 Subject: [PATCH] Extend unit test for classification with external projects by checking, if the tag set has been written to the external project as well --- .../ClassifyLineagesControllerTest.java | 280 +++++++++++++++--- 1 file changed, 236 insertions(+), 44 deletions(-) diff --git a/src/test/java/org/mastodon/mamut/classification/ClassifyLineagesControllerTest.java b/src/test/java/org/mastodon/mamut/classification/ClassifyLineagesControllerTest.java index 0db2474e3..aa234a8e7 100644 --- a/src/test/java/org/mastodon/mamut/classification/ClassifyLineagesControllerTest.java +++ b/src/test/java/org/mastodon/mamut/classification/ClassifyLineagesControllerTest.java @@ -29,6 +29,7 @@ package org.mastodon.mamut.classification; import ij.ImagePlus; +import mpicbg.spim.data.SpimDataException; import net.imagej.ImgPlus; import net.imagej.axis.Axes; import net.imagej.axis.AxisType; @@ -42,6 +43,7 @@ import org.mastodon.mamut.classification.config.CropCriteria; import org.mastodon.mamut.classification.config.SimilarityMeasure; import org.mastodon.mamut.feature.branch.exampleGraph.ExampleGraph2; +import org.mastodon.mamut.io.ProjectLoader; import org.mastodon.mamut.io.importer.labelimage.util.DemoUtils; import org.mastodon.mamut.model.Model; import org.mastodon.mamut.model.ModelGraph; @@ -85,11 +87,11 @@ void testCreateTagSet() final ModelGraph modelGraph = model.getGraph(); - addLineageTree1( modelGraph ); - addLineageTree2( modelGraph ); - addLineageTree3( modelGraph ); - addLineageTree4( modelGraph ); - addLineageTree5( modelGraph ); + addLineageTree11( modelGraph ); + addLineageTree21( modelGraph ); + addLineageTree31( modelGraph ); + addLineageTree41( modelGraph ); + addLineageTree51( modelGraph ); addEmptyTree( modelGraph ); String tagSetName = "Test Tag Set"; @@ -127,9 +129,10 @@ void testCreateTagSet() } @Test - void testCreateTagSetWithExternalProjects() throws IOException + void testCreateTagSetWithExternalProjects() throws IOException, SpimDataException { - final Model model = new Model(); + final Model model1 = new Model(); + final Model model2 = new Model(); try (Context context = new Context()) { @@ -137,58 +140,74 @@ void testCreateTagSetWithExternalProjects() throws IOException final ImagePlus dummyImagePlus = ImgToVirtualStack.wrap( new ImgPlus<>( dummyImg, "image", new AxisType[] { Axes.X, Axes.Y, Axes.Z } ) ); SharedBigDataViewerData dummyBdv = Objects.requireNonNull( SharedBigDataViewerData.fromImagePlus( dummyImagePlus ) ); - ProjectModel projectModel = ProjectModel.create( context, model, dummyBdv, null ); + ProjectModel projectModel1 = ProjectModel.create( context, model1, dummyBdv, null ); - final ModelGraph modelGraph = model.getGraph(); + final ModelGraph modelGraph1 = model1.getGraph(); + final ModelGraph modelGraph2 = model2.getGraph(); - addLineageTree1( modelGraph ); - addLineageTree2( modelGraph ); - addLineageTree3( modelGraph ); - addLineageTree4( modelGraph ); - addLineageTree5( modelGraph ); - addEmptyTree( modelGraph ); + addLineageTree11( modelGraph1 ); + addLineageTree21( modelGraph1 ); + addLineageTree31( modelGraph1 ); + addLineageTree41( modelGraph1 ); + addLineageTree51( modelGraph1 ); + addEmptyTree( modelGraph1 ); - File file1 = DemoUtils.saveAppModelToTempFile( dummyImg, model ); - File file2 = DemoUtils.saveAppModelToTempFile( dummyImg, model ); - File[] files = { file1, file2 }; + addLineageTree12( modelGraph2 ); + addLineageTree22( modelGraph2 ); + addLineageTree32( modelGraph2 ); + addLineageTree42( modelGraph2 ); + addLineageTree52( modelGraph2 ); + addEmptyTree( modelGraph2 ); + + File file2 = DemoUtils.saveAppModelToTempFile( dummyImg, model2 ); + File[] files = { file2 }; String tagSetName = "Test Tag Set"; - TagSetUtils.addNewTagSetToModel( model, tagSetName, Collections.emptyList() ); + TagSetUtils.addNewTagSetToModel( model1, tagSetName, Collections.emptyList() ); PrefService prefService = context.getService( PrefService.class ); MastodonProjectService projectService = context.getService( MastodonProjectService.class ); - ClassifyLineagesController controller = new ClassifyLineagesController( projectModel, prefService, projectService ); + ClassifyLineagesController controller = new ClassifyLineagesController( projectModel1, prefService, projectService ); controller.setInputParams( CropCriteria.TIMEPOINT, 0, 100, 1 ); controller.setComputeParams( SimilarityMeasure.NORMALIZED_ZHANG_DIFFERENCE, ClusteringMethod.AVERAGE_LINKAGE, 3 ); controller.setVisualisationParams( false ); controller.setExternalProjects( files, true ); controller.createTagSet(); - List< TagSetStructure.TagSet > tagSets = model.getTagSetModel().getTagSetStructure().getTagSets(); - TagSetStructure.TagSet tagSet1 = model.getTagSetModel().getTagSetStructure().getTagSets().get( 1 ); - List< TagSetStructure.Tag > tags = tagSet1.getTags(); - TagSetStructure.Tag tag0 = tags.get( 0 ); - TagSetStructure.Tag tag1 = tags.get( 1 ); - TagSetStructure.Tag tag2 = tags.get( 2 ); - - Collection< Spot > tag0Spots = model.getTagSetModel().getVertexTags().getTaggedWith( tag0 ); - Collection< Spot > tag1Spots = model.getTagSetModel().getVertexTags().getTaggedWith( tag1 ); - Collection< Spot > tag2Spots = model.getTagSetModel().getVertexTags().getTaggedWith( tag2 ); - Set< String > expectedClassNames = new HashSet<>( Arrays.asList( "Class 1", "Class 2", "Class 3" ) ); - Set< String > actualClassNames = new HashSet<>( Arrays.asList( tag0.label(), tag1.label(), tag2.label() ) ); - Set< Integer > expectedClassCounts = new HashSet<>( Arrays.asList( 9, 12, 14 ) ); - Set< Integer > actualClassCounts = new HashSet<>( Arrays.asList( tag0Spots.size(), tag1Spots.size(), tag2Spots.size() ) ); - assertEquals( "Average Classification (time: 0-100, classes: 3, min. div: 1) ", tagSet1.getName() ); assertTrue( controller.isValidParams() ); - assertEquals( 2, tagSets.size() ); - assertEquals( 3, tags.size() ); - assertEquals( expectedClassNames, actualClassNames ); - assertEquals( expectedClassCounts, actualClassCounts ); + assertClassificationEquals( model1, 1, expectedClassNames, expectedClassCounts ); + ProjectModel pm2 = ProjectLoader.open( file2.getAbsolutePath(), context, false, true ); + assertClassificationEquals( pm2.getModel(), 0, expectedClassNames, expectedClassCounts ); } } + private void assertClassificationEquals( Model model, int existingTagSets, Set< String > expectedClassNames, + Set< Integer > expectedClassCounts ) + { + List< TagSetStructure.TagSet > tagSets = model.getTagSetModel().getTagSetStructure().getTagSets(); + int expectedTagSets = existingTagSets + 1; + TagSetStructure.TagSet tagSet = tagSets.get( existingTagSets ); + List< TagSetStructure.Tag > tags1 = tagSet.getTags(); + TagSetStructure.Tag tag0 = tags1.get( 0 ); + TagSetStructure.Tag tag1 = tags1.get( 1 ); + TagSetStructure.Tag tag2 = tags1.get( 2 ); + + Collection< Spot > tag0Spots = model.getTagSetModel().getVertexTags().getTaggedWith( tag0 ); + Collection< Spot > tag1Spots = model.getTagSetModel().getVertexTags().getTaggedWith( tag1 ); + Collection< Spot > tag2Spots = model.getTagSetModel().getVertexTags().getTaggedWith( tag2 ); + + Set< String > actualClassNames = new HashSet<>( Arrays.asList( tag0.label(), tag1.label(), tag2.label() ) ); + Set< Integer > actualClassCounts = new HashSet<>( Arrays.asList( tag0Spots.size(), tag1Spots.size(), tag2Spots.size() ) ); + + assertEquals( "Average Classification (time: 0-100, classes: 3, min. div: 1) ", tagSet.getName() ); + assertEquals( expectedTagSets, tagSets.size() ); + assertEquals( 3, tags1.size() ); + assertEquals( expectedClassNames, actualClassNames ); + assertEquals( expectedClassCounts, actualClassCounts ); + } + @Test void testSetExternalProjects() throws IOException { @@ -269,7 +288,7 @@ void testGetParameters() * branchSpot2(lifespan=10) branchSpot3(lifespan=30) * */ - private static void addLineageTree1( final ModelGraph modelGraph ) + private static void addLineageTree11( final ModelGraph modelGraph ) { Spot spot1 = modelGraph.addVertex().init( 0, new double[ 3 ], 0 ); Spot spot2 = modelGraph.addVertex().init( 20, new double[ 3 ], 0 ); @@ -278,6 +297,34 @@ private static void addLineageTree1( final ModelGraph modelGraph ) Spot spot5 = modelGraph.addVertex().init( 20, new double[ 3 ], 0 ); Spot spot6 = modelGraph.addVertex().init( 50, new double[ 3 ], 0 ); + spot1.setLabel( "tree1" ); + + modelGraph.addEdge( spot1, spot2 ); + modelGraph.addEdge( spot2, spot3 ); + modelGraph.addEdge( spot2, spot5 ); + modelGraph.addEdge( spot3, spot4 ); + modelGraph.addEdge( spot5, spot6 ); + } + + /** + *
+	 *                             branchSpot1(lifespan=21)
+	 *                    ┌-─────────┴─────────────┐
+	 *                    │                        │
+	 *                  branchSpot2(lifespan=10)    branchSpot3(lifespan=30)
+	 * 
+ */ + private static void addLineageTree12( final ModelGraph modelGraph ) + { + Spot spot1 = modelGraph.addVertex().init( 0, new double[ 3 ], 0 ); + Spot spot2 = modelGraph.addVertex().init( 21, new double[ 3 ], 0 ); + Spot spot3 = modelGraph.addVertex().init( 21, new double[ 3 ], 0 ); + Spot spot4 = modelGraph.addVertex().init( 31, new double[ 3 ], 0 ); + Spot spot5 = modelGraph.addVertex().init( 21, new double[ 3 ], 0 ); + Spot spot6 = modelGraph.addVertex().init( 51, new double[ 3 ], 0 ); + + spot1.setLabel( "tree1" ); + modelGraph.addEdge( spot1, spot2 ); modelGraph.addEdge( spot2, spot3 ); modelGraph.addEdge( spot2, spot5 ); @@ -293,7 +340,7 @@ private static void addLineageTree1( final ModelGraph modelGraph ) * branchSpot2(lifespan=10) branchSpot3(lifespan=20) * */ - public static void addLineageTree2( final ModelGraph modelGraph ) + public static void addLineageTree21( final ModelGraph modelGraph ) { Spot spot1 = modelGraph.addVertex().init( 0, new double[ 3 ], 0 ); Spot spot2 = modelGraph.addVertex().init( 30, new double[ 3 ], 0 ); @@ -302,6 +349,34 @@ public static void addLineageTree2( final ModelGraph modelGraph ) Spot spot5 = modelGraph.addVertex().init( 30, new double[ 3 ], 0 ); Spot spot6 = modelGraph.addVertex().init( 50, new double[ 3 ], 0 ); + spot1.setLabel( "tree2" ); + + modelGraph.addEdge( spot1, spot2 ); + modelGraph.addEdge( spot2, spot3 ); + modelGraph.addEdge( spot2, spot5 ); + modelGraph.addEdge( spot3, spot4 ); + modelGraph.addEdge( spot5, spot6 ); + } + + /** + *
+	 *                               branchSpot1(lifespan=31)
+	 *                      ┌-─────────┴─────────────┐
+	 *                      │                        │
+	 *                    branchSpot2(lifespan=10) branchSpot3(lifespan=20)
+	 * 
+ */ + public static void addLineageTree22( final ModelGraph modelGraph ) + { + Spot spot1 = modelGraph.addVertex().init( 0, new double[ 3 ], 0 ); + Spot spot2 = modelGraph.addVertex().init( 30, new double[ 3 ], 0 ); + Spot spot3 = modelGraph.addVertex().init( 31, new double[ 3 ], 0 ); + Spot spot4 = modelGraph.addVertex().init( 41, new double[ 3 ], 0 ); + Spot spot5 = modelGraph.addVertex().init( 31, new double[ 3 ], 0 ); + Spot spot6 = modelGraph.addVertex().init( 51, new double[ 3 ], 0 ); + + spot1.setLabel( "tree2" ); + modelGraph.addEdge( spot1, spot2 ); modelGraph.addEdge( spot2, spot3 ); modelGraph.addEdge( spot2, spot5 ); @@ -320,7 +395,7 @@ public static void addLineageTree2( final ModelGraph modelGraph ) * branchSpot4(lifespan=1) branchSpot5(lifespan=100) * */ - private static void addLineageTree3( final ModelGraph modelGraph ) + private static void addLineageTree31( final ModelGraph modelGraph ) { Spot spot1 = modelGraph.addVertex().init( 0, new double[ 3 ], 0 ); Spot spot2 = modelGraph.addVertex().init( 1, new double[ 3 ], 0 ); @@ -333,6 +408,45 @@ private static void addLineageTree3( final ModelGraph modelGraph ) Spot spot9 = modelGraph.addVertex().init( 2, new double[ 3 ], 0 ); Spot spot10 = modelGraph.addVertex().init( 102, new double[ 3 ], 0 ); + spot1.setLabel( "tree3" ); + + modelGraph.addEdge( spot1, spot2 ); + modelGraph.addEdge( spot2, spot3 ); + modelGraph.addEdge( spot2, spot5 ); + modelGraph.addEdge( spot3, spot4 ); + modelGraph.addEdge( spot5, spot6 ); + modelGraph.addEdge( spot6, spot7 ); + modelGraph.addEdge( spot6, spot9 ); + modelGraph.addEdge( spot7, spot8 ); + modelGraph.addEdge( spot9, spot10 ); + } + + /** + *
+	 *                              branchSpot1(lifespan=1)
+	 *                     ┌-─────────┴─────────────┐
+	 *                     │                        │
+	 *                   branchSpot2(lifespan=1)  branchSpot3(lifespan=1)
+	 *          ┌-─────────┴─────────────┐
+	 *          │                        │
+	 *        branchSpot4(lifespan=1)  branchSpot5(lifespan=101)
+	 * 
+ */ + private static void addLineageTree32( final ModelGraph modelGraph ) + { + Spot spot1 = modelGraph.addVertex().init( 0, new double[ 3 ], 0 ); + Spot spot2 = modelGraph.addVertex().init( 1, new double[ 3 ], 0 ); + Spot spot3 = modelGraph.addVertex().init( 1, new double[ 3 ], 0 ); + Spot spot4 = modelGraph.addVertex().init( 2, new double[ 3 ], 0 ); + Spot spot5 = modelGraph.addVertex().init( 1, new double[ 3 ], 0 ); + Spot spot6 = modelGraph.addVertex().init( 2, new double[ 3 ], 0 ); + Spot spot7 = modelGraph.addVertex().init( 2, new double[ 3 ], 0 ); + Spot spot8 = modelGraph.addVertex().init( 3, new double[ 3 ], 0 ); + Spot spot9 = modelGraph.addVertex().init( 2, new double[ 3 ], 0 ); + Spot spot10 = modelGraph.addVertex().init( 103, new double[ 3 ], 0 ); + + spot1.setLabel( "tree3" ); + modelGraph.addEdge( spot1, spot2 ); modelGraph.addEdge( spot2, spot3 ); modelGraph.addEdge( spot2, spot5 ); @@ -355,7 +469,7 @@ private static void addLineageTree3( final ModelGraph modelGraph ) * * */ - private static void addLineageTree4( final ModelGraph modelGraph ) + private static void addLineageTree41( final ModelGraph modelGraph ) { Spot spot1 = modelGraph.addVertex().init( 0, new double[ 3 ], 0 ); Spot spot2 = modelGraph.addVertex().init( 3, new double[ 3 ], 0 ); @@ -373,6 +487,56 @@ private static void addLineageTree4( final ModelGraph modelGraph ) Spot spot13 = modelGraph.addVertex().init( 11, new double[ 3 ], 0 ); Spot spot14 = modelGraph.addVertex().init( 13, new double[ 3 ], 0 ); + spot1.setLabel( "tree4" ); + + modelGraph.addEdge( spot1, spot2 ); + modelGraph.addEdge( spot2, spot3 ); + modelGraph.addEdge( spot2, spot5 ); + modelGraph.addEdge( spot3, spot4 ); + modelGraph.addEdge( spot5, spot6 ); + + modelGraph.addEdge( spot6, spot7 ); + modelGraph.addEdge( spot6, spot9 ); + modelGraph.addEdge( spot7, spot8 ); + modelGraph.addEdge( spot9, spot10 ); + + modelGraph.addEdge( spot4, spot11 ); + modelGraph.addEdge( spot4, spot13 ); + modelGraph.addEdge( spot11, spot12 ); + modelGraph.addEdge( spot13, spot14 ); + } + + /** + *
+	 *                       branchSpot1(lifespan=3)
+	 *              ┌-─────────┴─────────────────────────────────────────┐
+	 *              │                                                    │
+	 *            branchSpot2(lifespan=9)                     branchSpot3(lifespan=9)
+	 *  ┌-───────────┴─────────────┐                        ┌-───────────┴─────────────┐
+	 * branchSpot4(lifespan=4)   branchSpot5(lifespan=4)  branchSpot6(lifespan=1)   branchSpot7(lifespan=2)
+	 *
+	 * 
+ */ + private static void addLineageTree42( final ModelGraph modelGraph ) + { + Spot spot1 = modelGraph.addVertex().init( 0, new double[ 3 ], 0 ); + Spot spot2 = modelGraph.addVertex().init( 3, new double[ 3 ], 0 ); + Spot spot3 = modelGraph.addVertex().init( 3, new double[ 3 ], 0 ); + Spot spot4 = modelGraph.addVertex().init( 12, new double[ 3 ], 0 ); + Spot spot5 = modelGraph.addVertex().init( 3, new double[ 3 ], 0 ); + Spot spot6 = modelGraph.addVertex().init( 12, new double[ 3 ], 0 ); + Spot spot7 = modelGraph.addVertex().init( 12, new double[ 3 ], 0 ); + Spot spot8 = modelGraph.addVertex().init( 16, new double[ 3 ], 0 ); + Spot spot9 = modelGraph.addVertex().init( 12, new double[ 3 ], 0 ); + Spot spot10 = modelGraph.addVertex().init( 16, new double[ 3 ], 0 ); + + Spot spot11 = modelGraph.addVertex().init( 12, new double[ 3 ], 0 ); + Spot spot12 = modelGraph.addVertex().init( 13, new double[ 3 ], 0 ); + Spot spot13 = modelGraph.addVertex().init( 12, new double[ 3 ], 0 ); + Spot spot14 = modelGraph.addVertex().init( 14, new double[ 3 ], 0 ); + + spot1.setLabel( "tree4" ); + modelGraph.addEdge( spot1, spot2 ); modelGraph.addEdge( spot2, spot3 ); modelGraph.addEdge( spot2, spot5 ); @@ -398,7 +562,7 @@ private static void addLineageTree4( final ModelGraph modelGraph ) * branchSpot2(lifespan=10) branchSpot3(lifespan=30) * */ - private static void addLineageTree5( final ModelGraph modelGraph ) + private static void addLineageTree51( final ModelGraph modelGraph ) { Spot spot1 = modelGraph.addVertex().init( 101, new double[ 3 ], 0 ); Spot spot2 = modelGraph.addVertex().init( 121, new double[ 3 ], 0 ); @@ -407,6 +571,8 @@ private static void addLineageTree5( final ModelGraph modelGraph ) Spot spot5 = modelGraph.addVertex().init( 121, new double[ 3 ], 0 ); Spot spot6 = modelGraph.addVertex().init( 151, new double[ 3 ], 0 ); + spot1.setLabel( "tree5" ); + modelGraph.addEdge( spot1, spot2 ); modelGraph.addEdge( spot2, spot3 ); modelGraph.addEdge( spot2, spot5 ); @@ -414,6 +580,32 @@ private static void addLineageTree5( final ModelGraph modelGraph ) modelGraph.addEdge( spot5, spot6 ); } + /** + *
+	 *                             branchSpot1(lifespan=21)
+	 *                    ┌-─────────┴─────────────┐
+	 *                    │                        │
+	 *                  branchSpot2(lifespan=10)    branchSpot3(lifespan=30)
+	 * 
+ */ + private static void addLineageTree52( final ModelGraph modelGraph ) + { + Spot spot1 = modelGraph.addVertex().init( 101, new double[ 3 ], 0 ); + Spot spot2 = modelGraph.addVertex().init( 122, new double[ 3 ], 0 ); + Spot spot3 = modelGraph.addVertex().init( 122, new double[ 3 ], 0 ); + Spot spot4 = modelGraph.addVertex().init( 132, new double[ 3 ], 0 ); + Spot spot5 = modelGraph.addVertex().init( 122, new double[ 3 ], 0 ); + Spot spot6 = modelGraph.addVertex().init( 152, new double[ 3 ], 0 ); + + modelGraph.addEdge( spot1, spot2 ); + modelGraph.addEdge( spot2, spot3 ); + modelGraph.addEdge( spot2, spot5 ); + modelGraph.addEdge( spot3, spot4 ); + modelGraph.addEdge( spot5, spot6 ); + + spot1.setLabel( "tree5" ); + } + private static void addEmptyTree( final ModelGraph modelGraph ) { modelGraph.addVertex().init( 0, new double[ 3 ], 0 );