From fceb0ff41fec8b58231315df4303efa5e1e93d49 Mon Sep 17 00:00:00 2001 From: Stefan Hahmann Date: Tue, 19 Sep 2023 16:34:16 +0200 Subject: [PATCH] Add unit tests --- .../SegmentUsingEllipsoidsControllerTest.java | 138 ++++++++++++++++++ .../segment/config/LabelOptionsTest.java | 23 +++ .../segment/config/SegmentationTaskTest.java | 22 +++ 3 files changed, 183 insertions(+) create mode 100644 src/test/java/org/mastodon/mamut/segment/SegmentUsingEllipsoidsControllerTest.java create mode 100644 src/test/java/org/mastodon/mamut/segment/config/LabelOptionsTest.java create mode 100644 src/test/java/org/mastodon/mamut/segment/config/SegmentationTaskTest.java diff --git a/src/test/java/org/mastodon/mamut/segment/SegmentUsingEllipsoidsControllerTest.java b/src/test/java/org/mastodon/mamut/segment/SegmentUsingEllipsoidsControllerTest.java new file mode 100644 index 000000000..7430b8427 --- /dev/null +++ b/src/test/java/org/mastodon/mamut/segment/SegmentUsingEllipsoidsControllerTest.java @@ -0,0 +1,138 @@ +package org.mastodon.mamut.segment; + +import bdv.util.AbstractSource; +import bdv.util.RandomAccessibleIntervalSource; +import io.scif.img.ImgOpener; +import io.scif.img.SCIFIOImgPlus; +import mpicbg.spim.data.sequence.TimePoint; +import net.imglib2.img.Img; +import net.imglib2.realtransform.AffineTransform3D; +import net.imglib2.test.RandomImgs; +import net.imglib2.type.numeric.integer.IntType; +import net.imglib2.util.Cast; +import org.junit.Before; +import org.junit.Test; +import org.mastodon.mamut.model.Model; +import org.mastodon.mamut.model.ModelGraph; +import org.mastodon.mamut.model.Spot; +import org.mastodon.mamut.model.branch.BranchSpot; +import org.mastodon.mamut.model.branch.ModelBranchGraph; +import org.mastodon.mamut.segment.config.LabelOptions; +import org.scijava.Context; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; + +public class SegmentUsingEllipsoidsControllerTest +{ + private Model model; + + private Spot spot; + + private BranchSpot branchSpot; + + private int timepoint; + + private final long[] center = new long[] { 50, 50, 50 }; + + @Before + public void setUp() + { + model = new Model(); + ModelGraph modelGraph = model.getGraph(); + spot = modelGraph.addVertex(); + ModelBranchGraph modelBranchGraph = model.getBranchGraph(); + modelBranchGraph.graphRebuilt(); + branchSpot = modelBranchGraph.getBranchVertex( spot, modelBranchGraph.vertexRef() ); + timepoint = 0; + spot.init( timepoint, Arrays.stream( center ).asDoubleStream().toArray(), 5d ); + } + + @Test + public void testSaveEllipsoidSegmentationToFile() throws IOException + { + AbstractSource< IntType > source = createRandomSource(); + Context context = new Context(); + TimePoint timePoint = new TimePoint( timepoint ); + List< TimePoint > timePoints = Collections.singletonList( timePoint ); + SegmentUsingEllipsoidsController segmentUsingEllipsoidsController = + new SegmentUsingEllipsoidsController( model, timePoints, Cast.unchecked( source ), context ); + File outputSpot = getTempFile( "resultSpot" ); + File outputBranchSpot = getTempFile( "resultBranchSpot" ); + segmentUsingEllipsoidsController.saveEllipsoidSegmentationToFile( LabelOptions.SPOT_ID, outputSpot, true, false ); + segmentUsingEllipsoidsController.saveEllipsoidSegmentationToFile( LabelOptions.BRANCH_SPOT_ID, outputBranchSpot, true, false ); + + ImgOpener imgOpener = new ImgOpener( context ); + SCIFIOImgPlus< IntType > imgSpot = getIntTypeSCIFIOImgPlus( imgOpener, outputSpot ); + SCIFIOImgPlus< IntType > imgBranchSpot = getIntTypeSCIFIOImgPlus( imgOpener, outputBranchSpot ); + + // check that the random content has been replaced by the spot id in the center of the spot + assertEquals( spot.getInternalPoolIndex(), imgSpot.getAt( center ).get() ); + assertEquals( branchSpot.getInternalPoolIndex(), imgBranchSpot.getAt( center ).get() ); + // check that the random content has NOT been replaced outside the ellipsoid of the spot + long[] corner = new long[] { 0, 0, 0 }; + assertNotEquals( spot.getInternalPoolIndex(), imgSpot.getAt( corner ).get() ); + assertNotEquals( branchSpot.getInternalPoolIndex(), imgBranchSpot.getAt( corner ).get() ); + } + + private static SCIFIOImgPlus< IntType > getIntTypeSCIFIOImgPlus( ImgOpener imgOpener, File outputSpot ) + { + List< SCIFIOImgPlus< ? > > imgsSpot = imgOpener.openImgs( outputSpot.getAbsolutePath() ); + SCIFIOImgPlus< ? > imgSpot = imgsSpot.get( 0 ); + return Cast.unchecked( imgSpot ); + } + + private static File getTempFile( final String prefix ) throws IOException + { + File outputSpot = File.createTempFile( prefix, ".tif" ); + outputSpot.deleteOnExit(); + return outputSpot; + } + + @Test + public void testExceptions() throws IOException + { + SegmentUsingEllipsoidsController controller = + new SegmentUsingEllipsoidsController( model, Collections.emptyList(), null, new Context() ); + File file = File.createTempFile( "foo", "foo" ); + file.deleteOnExit(); + assertThrows( + IllegalArgumentException.class, + () -> controller.saveEllipsoidSegmentationToFile( LabelOptions.SPOT_ID, null, true, false ) + ); + assertThrows( IllegalArgumentException.class, () -> controller.saveEllipsoidSegmentationToFile( null, file, true, false ) ); + assertThrows( IllegalArgumentException.class, () -> controller.showEllipsoidSegmentationInBDV( null ) ); + } + + private static AbstractSource< IntType > createRandomSource() + { + Img< IntType > randomImg = RandomImgs.seed( 0 ).nextImage( new IntType() {}, 100, 100, 100 ); + return new RandomAccessibleIntervalSource<>( randomImg, new IntType(), new AffineTransform3D(), "Segmentation" ); + } + + @Test + public void testShowEllipsoidSegmentationInBDV() + { + AbstractSource< IntType > source = createRandomSource(); + Context context = new Context(); + TimePoint timePoint = new TimePoint( timepoint ); + List< TimePoint > timePoints = Collections.singletonList( timePoint ); + SegmentUsingEllipsoidsController segmentUsingEllipsoidsController = + new SegmentUsingEllipsoidsController( model, timePoints, Cast.unchecked( source ), context ); + segmentUsingEllipsoidsController.showEllipsoidSegmentationInBDV( LabelOptions.SPOT_ID ); + + // check that the random content has been replaced by the spot id in the center of the spot + assertEquals( spot.getInternalPoolIndex(), source.getSource( timepoint, 0 ).getAt( center ).get() ); + // check that the random content has NOT been replaced outside the ellipsoid of the spot + long[] corner = new long[] { 0, 0, 0 }; + assertNotEquals( spot.getInternalPoolIndex(), source.getSource( timepoint, 0 ).getAt( corner ).get() ); + } + +} diff --git a/src/test/java/org/mastodon/mamut/segment/config/LabelOptionsTest.java b/src/test/java/org/mastodon/mamut/segment/config/LabelOptionsTest.java new file mode 100644 index 000000000..55e4e9b4d --- /dev/null +++ b/src/test/java/org/mastodon/mamut/segment/config/LabelOptionsTest.java @@ -0,0 +1,23 @@ +package org.mastodon.mamut.segment.config; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class LabelOptionsTest +{ + + @Test + public void getByName() + { + assertEquals( LabelOptions.SPOT_ID, LabelOptions.getByName( "Spot Id" ) ); + assertEquals( LabelOptions.BRANCH_SPOT_ID, LabelOptions.getByName( "BranchSpot Id" ) ); + } + + @Test + public void getName() + { + assertEquals( "Spot Id", LabelOptions.SPOT_ID.getName() ); + assertEquals( "BranchSpot Id", LabelOptions.BRANCH_SPOT_ID.getName() ); + } +} diff --git a/src/test/java/org/mastodon/mamut/segment/config/SegmentationTaskTest.java b/src/test/java/org/mastodon/mamut/segment/config/SegmentationTaskTest.java new file mode 100644 index 000000000..2ce81dc43 --- /dev/null +++ b/src/test/java/org/mastodon/mamut/segment/config/SegmentationTaskTest.java @@ -0,0 +1,22 @@ +package org.mastodon.mamut.segment.config; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class SegmentationTaskTest +{ + + @Test + public void getByName() + { + assertEquals( SegmentationTask.SEGMENT_ELLIPSOIDS_BDV, SegmentationTask.getByName( "Show segmentation in BigDataViewer" ) ); + } + + @Test + public void getName() + { + assertEquals( "Show segmentation in BigDataViewer", SegmentationTask.SEGMENT_ELLIPSOIDS_BDV.getName() ); + assertEquals( "Save segmentation result to file", SegmentationTask.SEGMENT_ELLIPSOIDS_IMAGEJ.getName() ); + } +}