From c7d2cbb5b2d605f6c0f41e6c6a0c5255c93fa05c Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Wed, 9 Oct 2024 17:31:53 -0500 Subject: [PATCH] Manage SciJava Context lifecycle And fix failing tests on Windows. This changes all of labkit-ui to eschew usage of the SingletonContext class in favor of passing the context between components as appropriate, creating new contexts in relevant situations, and disposing of contexts when they are finished being used in all cases except for demo code. It also escapes backslashes of Windows paths hardcoded into macros, to account for ImageJ macro using backslash as an escape character. Some of these changes change existing public API, so this commit increments the major/minor digit of the development version. --- pom.xml | 3 +- .../java/sc/fiji/labkit/ui/LabkitFrame.java | 5 -- .../ui/models/DefaultSegmentationModel.java | 4 +- .../labkit/ui/panel/AddSegmenterPanel.java | 11 +++- ...AbstractProcessFilesInDirectoryPlugin.java | 6 +- ...lateProbabilityMapWithLabkitIJ1Plugin.java | 3 +- ...lculateProbabilityMapWithLabkitPlugin.java | 3 +- .../SegmentImageWithLabkitIJ1Plugin.java | 3 +- .../plugin/SegmentImageWithLabkitPlugin.java | 3 +- .../ui/segmentation/SegmentationTool.java | 13 ++--- .../weka/PixelClassificationPlugin.java | 4 +- ...hangingImageSegmentationComponentDemo.java | 4 +- src/test/java/demo/MultiChannelMovieDemo.java | 6 +- .../fiji/labkit/ui/InitialLabelingTest.java | 20 ++++++- .../labkit/ui/SegmentationUseCaseTest.java | 29 +++++++--- .../labeling/LabelingSerializationTest.java | 21 +++++-- .../models/CustomCachedImageFactoryDemo.java | 14 ++--- .../models/DefaultSegmentationModelTest.java | 18 +++++- .../multi_image/ProjectSegmentationModel.java | 4 +- ...ProbabilityMapWithLabkitIJ1PluginTest.java | 58 ++++++++++--------- ...ateProbabilityMapWithLabkitPluginTest.java | 35 ++++++----- ...lityMapForImagesInDirectoryPluginTest.java | 42 ++++++-------- ...kitSegmentImagesInDirectoryPluginTest.java | 43 +++++++------- .../SegmentImageWithLabkitIJ1PluginTest.java | 47 +++++++-------- .../SegmentImageWithLabkitPluginTest.java | 31 +++++----- .../labkit/ui/plugin/SegmentRGBImageTest.java | 41 +++++++++---- .../project/LabeledImagesListPanelDemo.java | 6 +- .../LabkitProjectFileSerializerTest.java | 26 ++++++--- .../ui/segmentation/SegmentationToolTest.java | 24 ++++++-- .../TrainableSegmentationSegmenterTest.java | 4 +- 30 files changed, 316 insertions(+), 215 deletions(-) diff --git a/pom.xml b/pom.xml index c7643a72..19eea40a 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ sc.fiji labkit-ui - 0.4.1-SNAPSHOT + 0.5.0-SNAPSHOT Labkit The Labkit image segmentation tool for Fiji. @@ -95,6 +95,7 @@ 0.15.3 10.6.0 0.1.18 + 2.3.4 diff --git a/src/main/java/sc/fiji/labkit/ui/LabkitFrame.java b/src/main/java/sc/fiji/labkit/ui/LabkitFrame.java index de421672..9da4ecd8 100644 --- a/src/main/java/sc/fiji/labkit/ui/LabkitFrame.java +++ b/src/main/java/sc/fiji/labkit/ui/LabkitFrame.java @@ -39,7 +39,6 @@ import sc.fiji.labkit.ui.models.DefaultSegmentationModel; import sc.fiji.labkit.ui.models.SegmentationModel; import sc.fiji.labkit.ui.utils.Notifier; -import sc.fiji.labkit.pixel_classification.utils.SingletonContext; import org.scijava.Context; import javax.swing.*; @@ -65,8 +64,6 @@ public class LabkitFrame { public static LabkitFrame showForFile(Context context, final String filename) { - if (context == null) - context = SingletonContext.getInstance(); Dataset dataset = openDataset(context, filename); return showForImage(context, new DatasetInputImage(dataset)); } @@ -83,8 +80,6 @@ private static Dataset openDataset(Context context, String filename) { public static LabkitFrame showForImage(Context context, final InputImage inputImage) { - if (context == null) - context = SingletonContext.getInstance(); final SegmentationModel model = new DefaultSegmentationModel(context, inputImage); model.imageLabelingModel().labeling().set(InitialLabeling.initialLabeling(context, inputImage)); return show(model, inputImage.imageForSegmentation().getName()); diff --git a/src/main/java/sc/fiji/labkit/ui/models/DefaultSegmentationModel.java b/src/main/java/sc/fiji/labkit/ui/models/DefaultSegmentationModel.java index 816bf3b1..d94afa79 100644 --- a/src/main/java/sc/fiji/labkit/ui/models/DefaultSegmentationModel.java +++ b/src/main/java/sc/fiji/labkit/ui/models/DefaultSegmentationModel.java @@ -92,7 +92,7 @@ List> getSegmentations(T type) Stream trainedSegmenters = getTrainedSegmenters(); return trainedSegmenters .map(segmenter -> { - SegmentationTool segmentationTool = new SegmentationTool(segmenter); + SegmentationTool segmentationTool = new SegmentationTool(context, segmenter); segmentationTool.setProgressWriter(new DummyProgressWriter()); return segmentationTool.segment(image, type); }) @@ -105,7 +105,7 @@ public List> getPredictions() { Stream trainedSegmenters = getTrainedSegmenters(); return trainedSegmenters .map(segmenter -> { - SegmentationTool segmentationTool = new SegmentationTool(segmenter); + SegmentationTool segmentationTool = new SegmentationTool(context, segmenter); segmentationTool.setProgressWriter(new DummyProgressWriter()); return segmentationTool.probabilityMap(image); }) diff --git a/src/main/java/sc/fiji/labkit/ui/panel/AddSegmenterPanel.java b/src/main/java/sc/fiji/labkit/ui/panel/AddSegmenterPanel.java index fc64abf0..3307b729 100644 --- a/src/main/java/sc/fiji/labkit/ui/panel/AddSegmenterPanel.java +++ b/src/main/java/sc/fiji/labkit/ui/panel/AddSegmenterPanel.java @@ -33,12 +33,13 @@ import sc.fiji.labkit.ui.models.SegmenterListModel; import sc.fiji.labkit.ui.segmentation.SegmentationPlugin; import sc.fiji.labkit.ui.segmentation.SegmentationPluginService; -import sc.fiji.labkit.pixel_classification.utils.SingletonContext; import net.miginfocom.swing.MigLayout; import org.scijava.Context; import javax.swing.*; import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; /** * Panel that shows a list of available segmentation algorithms. This panel is @@ -69,10 +70,16 @@ private void addButtons(SegmenterListModel segmenterListModel, JPanel list) { public static void main(String... args) { JFrame frame = new JFrame("Select Segmentation Algorithm"); - Context context = SingletonContext.getInstance(); + Context context = new Context(); SegmenterListModel slm = new SegmenterListModel(context, new ExtensionPoints()); frame.add(new AddSegmenterPanel(slm)); frame.setSize(300, 300); frame.setVisible(true); + frame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosed(WindowEvent e) { + context.dispose(); + } + }); } } diff --git a/src/main/java/sc/fiji/labkit/ui/plugin/AbstractProcessFilesInDirectoryPlugin.java b/src/main/java/sc/fiji/labkit/ui/plugin/AbstractProcessFilesInDirectoryPlugin.java index 639beabd..cc09ff6a 100644 --- a/src/main/java/sc/fiji/labkit/ui/plugin/AbstractProcessFilesInDirectoryPlugin.java +++ b/src/main/java/sc/fiji/labkit/ui/plugin/AbstractProcessFilesInDirectoryPlugin.java @@ -38,6 +38,7 @@ import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.filefilter.WildcardFileFilter; import org.scijava.Cancelable; +import org.scijava.Context; import org.scijava.app.StatusService; import org.scijava.command.Command; import org.scijava.log.Logger; @@ -52,6 +53,9 @@ abstract class AbstractProcessFilesInDirectoryPlugin implements Command, Cancelable { + @Parameter + private Context context; + @Parameter private DatasetIOService io; @@ -84,7 +88,7 @@ abstract class AbstractProcessFilesInDirectoryPlugin implements Command, Cancela @Override public void run() { - SegmentationTool segmenter = new SegmentationTool(); + SegmentationTool segmenter = new SegmentationTool(context); segmenter.setUseGpu(use_gpu); segmenter.openModel(segmenter_file.getAbsolutePath()); segmenter.setProgressWriter(new StatusServiceProgressWriter(statusService)); diff --git a/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1Plugin.java b/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1Plugin.java index 56aa75c6..d1701ce1 100644 --- a/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1Plugin.java +++ b/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1Plugin.java @@ -72,8 +72,7 @@ public class CalculateProbabilityMapWithLabkitIJ1Plugin implements Command, Canc @Override public void run() { - SegmentationTool segmenter = new SegmentationTool(); - segmenter.setContext(context); + SegmentationTool segmenter = new SegmentationTool(context); segmenter.openModel(segmenter_file.getAbsolutePath()); segmenter.setUseGpu(use_gpu); segmenter.setProgressWriter(new StatusServiceProgressWriter(statusService)); diff --git a/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPlugin.java b/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPlugin.java index 7287c956..87daaa3e 100644 --- a/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPlugin.java +++ b/src/main/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPlugin.java @@ -74,8 +74,7 @@ public class CalculateProbabilityMapWithLabkitPlugin implements Command, Cancela @Override public void run() { - SegmentationTool segmenter = new SegmentationTool(); - segmenter.setContext(context); + SegmentationTool segmenter = new SegmentationTool(context); segmenter.openModel(segmenter_file.getAbsolutePath()); segmenter.setUseGpu(use_gpu); segmenter.setProgressWriter(new StatusServiceProgressWriter(statusService)); diff --git a/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1Plugin.java b/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1Plugin.java index df010aff..e75643c3 100644 --- a/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1Plugin.java +++ b/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitIJ1Plugin.java @@ -71,8 +71,7 @@ public class SegmentImageWithLabkitIJ1Plugin implements Command, Cancelable { @Override public void run() { - SegmentationTool segmenter = new SegmentationTool(); - segmenter.setContext(context); + SegmentationTool segmenter = new SegmentationTool(context); segmenter.setUseGpu(use_gpu); segmenter.setProgressWriter(new StatusServiceProgressWriter(statusService)); segmenter.openModel(segmenter_file.getAbsolutePath()); diff --git a/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitPlugin.java b/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitPlugin.java index f345b701..a65a9441 100644 --- a/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitPlugin.java +++ b/src/main/java/sc/fiji/labkit/ui/plugin/SegmentImageWithLabkitPlugin.java @@ -74,8 +74,7 @@ public class SegmentImageWithLabkitPlugin implements Command, Cancelable { @Override public void run() { - SegmentationTool segmenter = new SegmentationTool(); - segmenter.setContext(context); + SegmentationTool segmenter = new SegmentationTool(context); segmenter.setUseGpu(use_gpu); segmenter.setProgressWriter(new StatusServiceProgressWriter(statusService)); segmenter.openModel(segmenter_file.getAbsolutePath()); diff --git a/src/main/java/sc/fiji/labkit/ui/segmentation/SegmentationTool.java b/src/main/java/sc/fiji/labkit/ui/segmentation/SegmentationTool.java index 2f22ac84..c4db73d0 100644 --- a/src/main/java/sc/fiji/labkit/ui/segmentation/SegmentationTool.java +++ b/src/main/java/sc/fiji/labkit/ui/segmentation/SegmentationTool.java @@ -47,7 +47,6 @@ import net.imglib2.util.Intervals; import org.apache.commons.lang3.ArrayUtils; import org.scijava.Context; -import sc.fiji.labkit.pixel_classification.utils.SingletonContext; import sc.fiji.labkit.ui.inputimage.DatasetInputImage; import sc.fiji.labkit.ui.inputimage.ImgPlusViewsOld; import sc.fiji.labkit.ui.models.CachedImageFactory; @@ -71,16 +70,16 @@ public class SegmentationTool { private final CachedImageFactory cachedImageFactory = DefaultCachedImageFactory.getInstance(); - public SegmentationTool() { - + public SegmentationTool(Context context) { + this(context, null); } - public SegmentationTool(Segmenter segmenter) { + public SegmentationTool(Context context, Segmenter segmenter) { + this.context = Objects.requireNonNull(context); this.segmenter = segmenter; } public void openModel(String classifierFile) { - Context context = this.context != null ? this.context : SingletonContext.getInstance(); Segmenter segmenter = new TrainableSegmentationSegmenter(context); segmenter.openModel(classifierFile); setSegmenter(segmenter); @@ -94,10 +93,6 @@ public void setSegmenter(Segmenter segmenter) { this.segmenter.setUseGpu(useGpu); } - public void setContext(Context context) { - this.context = Objects.requireNonNull(context); - } - public void setProgressWriter(ProgressWriter progressWriter) { this.progressWriter = Objects.requireNonNull(progressWriter); } diff --git a/src/main/java/sc/fiji/labkit/ui/segmentation/weka/PixelClassificationPlugin.java b/src/main/java/sc/fiji/labkit/ui/segmentation/weka/PixelClassificationPlugin.java index a9ddf130..8ba1a31a 100644 --- a/src/main/java/sc/fiji/labkit/ui/segmentation/weka/PixelClassificationPlugin.java +++ b/src/main/java/sc/fiji/labkit/ui/segmentation/weka/PixelClassificationPlugin.java @@ -31,7 +31,6 @@ import sc.fiji.labkit.ui.segmentation.SegmentationPlugin; import sc.fiji.labkit.ui.segmentation.Segmenter; -import sc.fiji.labkit.pixel_classification.utils.SingletonContext; import org.scijava.Context; import org.scijava.plugin.Parameter; import org.scijava.plugin.Plugin; @@ -66,8 +65,7 @@ public boolean canOpenFile(String filename) { } } - public static SegmentationPlugin create() { - Context context = SingletonContext.getInstance(); + public static SegmentationPlugin create(Context context) { PixelClassificationPlugin plugin = new PixelClassificationPlugin(); context.inject(plugin); return plugin; diff --git a/src/test/java/demo/ChangingImageSegmentationComponentDemo.java b/src/test/java/demo/ChangingImageSegmentationComponentDemo.java index b5f3c607..3e7ec76f 100644 --- a/src/test/java/demo/ChangingImageSegmentationComponentDemo.java +++ b/src/test/java/demo/ChangingImageSegmentationComponentDemo.java @@ -41,7 +41,6 @@ import sc.fiji.labkit.ui.models.DefaultSegmentationModel; import sc.fiji.labkit.ui.models.ImageLabelingModel; import sc.fiji.labkit.ui.models.SegmentationModel; -import sc.fiji.labkit.pixel_classification.utils.SingletonContext; import net.imglib2.type.numeric.integer.UnsignedByteType; import net.imglib2.view.Views; import net.miginfocom.swing.MigLayout; @@ -93,8 +92,7 @@ private static void onChangeImageButtonClicked(SegmentationModel segmentationMod DatasetInputImage datasetInputImage = new DatasetInputImage(image); model.showable().set(datasetInputImage.showable()); model.imageForSegmentation().set(datasetInputImage.imageForSegmentation()); - model.labeling().set(InitialLabeling.initialLabeling(SingletonContext.getInstance(), - datasetInputImage)); + model.labeling().set(InitialLabeling.initialLabeling(new Context(), datasetInputImage)); } private JPanel getBottomPanel() { diff --git a/src/test/java/demo/MultiChannelMovieDemo.java b/src/test/java/demo/MultiChannelMovieDemo.java index 6298160a..b0530735 100644 --- a/src/test/java/demo/MultiChannelMovieDemo.java +++ b/src/test/java/demo/MultiChannelMovieDemo.java @@ -73,8 +73,12 @@ */ public class MultiChannelMovieDemo { + private static Context context; + public static void main(String... args) { + context = new Context(); main2(); + // TODO: Dispose context when relevant window is closed. } private static void main1() { @@ -120,7 +124,7 @@ public void testInputImageImageForSegmentation() { SegmentationModel segmentationModel = new DefaultSegmentationModel( new Context(), inputImage); SegmentationItem segmenter = segmentationModel.segmenterList().addSegmenter( - PixelClassificationPlugin.create()); + PixelClassificationPlugin.create(context)); Labeling labeling1 = labeling5d(); segmentationModel.imageLabelingModel().labeling().set(labeling1); segmenter.train(Collections.singletonList(new ValuePair<>(inputImage diff --git a/src/test/java/sc/fiji/labkit/ui/InitialLabelingTest.java b/src/test/java/sc/fiji/labkit/ui/InitialLabelingTest.java index 99989842..57a0759b 100644 --- a/src/test/java/sc/fiji/labkit/ui/InitialLabelingTest.java +++ b/src/test/java/sc/fiji/labkit/ui/InitialLabelingTest.java @@ -34,7 +34,9 @@ import net.imagej.axis.EnumeratedAxis; import net.imglib2.img.Img; import net.imglib2.img.array.ArrayImgs; -import sc.fiji.labkit.pixel_classification.utils.SingletonContext; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.scijava.Context; import sc.fiji.labkit.ui.inputimage.DatasetInputImage; import sc.fiji.labkit.ui.labeling.Labeling; import net.imglib2.type.numeric.integer.UnsignedByteType; @@ -49,6 +51,18 @@ public class InitialLabelingTest { + private static Context context; + + @BeforeClass + public static void setUp() { + context = new Context(); + } + + @AfterClass + public static void tearDown() { + context.dispose(); + } + @Test public void testDoNotCrashWhenLabelingFileIsEmpty() throws IOException { File empty = File.createTempFile("labkit-InitialLabelingTest-", @@ -59,7 +73,7 @@ public void testDoNotCrashWhenLabelingFileIsEmpty() throws IOException { DatasetInputImage inputImage = new DatasetInputImage(image); List defaultLabels = Collections.emptyList(); Labeling result = InitialLabeling.initLabeling(inputImage, - SingletonContext.getInstance(), + context, defaultLabels); assertNotNull(result); } @@ -75,7 +89,7 @@ public void testEnumeratedAxis() { EnumeratedAxis yAxis = new EnumeratedAxis(Axes.Y, "mm", new double[] { 0, 0.3 }); ImgPlus image = new ImgPlus<>(img, "test", xAxis, yAxis); DatasetInputImage inputImage = new DatasetInputImage(image); - Labeling result = InitialLabeling.initialLabeling(SingletonContext.getInstance(), inputImage); + Labeling result = InitialLabeling.initialLabeling(context, inputImage); assertNotNull(result); } } diff --git a/src/test/java/sc/fiji/labkit/ui/SegmentationUseCaseTest.java b/src/test/java/sc/fiji/labkit/ui/SegmentationUseCaseTest.java index 363fb4f6..be4281eb 100644 --- a/src/test/java/sc/fiji/labkit/ui/SegmentationUseCaseTest.java +++ b/src/test/java/sc/fiji/labkit/ui/SegmentationUseCaseTest.java @@ -39,6 +39,8 @@ import net.imglib2.img.Img; import net.imglib2.img.array.ArrayImgs; import net.imglib2.type.numeric.IntegerType; +import org.junit.AfterClass; +import org.junit.BeforeClass; import sc.fiji.labkit.ui.bdv.BdvShowable; import sc.fiji.labkit.ui.inputimage.DatasetInputImage; import sc.fiji.labkit.ui.inputimage.InputImage; @@ -51,7 +53,6 @@ import sc.fiji.labkit.ui.segmentation.weka.PixelClassificationPlugin; import sc.fiji.labkit.ui.segmentation.SegmentationPlugin; import net.imglib2.roi.labeling.LabelingType; -import net.imglib2.type.numeric.integer.ShortType; import net.imglib2.type.numeric.integer.UnsignedByteType; import net.imglib2.util.Intervals; import net.imglib2.util.ValuePair; @@ -70,22 +71,34 @@ public class SegmentationUseCaseTest { + private static Context context; + + @BeforeClass + public static void setUp() { + context = new Context(); + } + + @AfterClass + public static void tearDown() { + context.dispose(); + } + @Test public void test() { ImgPlus image = new ImgPlus<>(ArrayImgs.unsignedBytes(new byte[] { 1, 1, 2, 2 }, 2, 2)); InputImage inputImage = new DatasetInputImage(image); SegmentationModel segmentationModel = new DefaultSegmentationModel( - new Context(), inputImage); + context, inputImage); addLabels(segmentationModel.imageLabelingModel()); - SegmentationPlugin plugin = PixelClassificationPlugin.create(); + SegmentationPlugin plugin = PixelClassificationPlugin.create(context); SegmentationItem segmenter = segmentationModel.segmenterList().addSegmenter(plugin); segmenter.train(Collections.singletonList(new ValuePair<>(image, segmentationModel.imageLabelingModel().labeling().get()))); RandomAccessibleInterval> result = segmenter.results(segmentationModel.imageLabelingModel()).segmentation(); List list = new ArrayList<>(); - Views.iterable(result).forEach(x -> list.add(x.getInteger())); + result.forEach(x -> list.add(x.getInteger())); assertEquals(Arrays.asList(1, 1, 0, 0), list); } @@ -99,7 +112,7 @@ private void addLabels(ImageLabelingModel imageLabelingModel) { } @Test - public void testMultiChannel() throws InterruptedException { + public void testMultiChannel() { Img img = ArrayImgs.unsignedBytes(new byte[] { -1, 0, -1, 0, -1, -1, 0, 0 }, 2, 2, 2); ImgPlus imgPlus = new ImgPlus<>(img, "Image", @@ -108,17 +121,17 @@ public void testMultiChannel() throws InterruptedException { .wrap(Views.hyperSlice(img, 2, 0))); Labeling labeling = getLabeling(); - SegmentationModel segmentationModel = new DefaultSegmentationModel(new Context(), + SegmentationModel segmentationModel = new DefaultSegmentationModel(context, inputImage); ImageLabelingModel imageLabelingModel = segmentationModel.imageLabelingModel(); imageLabelingModel.labeling().set(labeling); SegmentationItem segmenter = segmentationModel.segmenterList().addSegmenter( - PixelClassificationPlugin.create()); + PixelClassificationPlugin.create(context)); segmenter.train(Collections.singletonList(new ValuePair<>(imgPlus, imageLabelingModel.labeling().get()))); RandomAccessibleInterval> result = segmenter.results(imageLabelingModel).segmentation(); - Iterator> it = Views.iterable(result).iterator(); + Iterator> it = result.iterator(); assertEquals(1, it.next().getInteger()); assertEquals(0, it.next().getInteger()); assertEquals(0, it.next().getInteger()); diff --git a/src/test/java/sc/fiji/labkit/ui/labeling/LabelingSerializationTest.java b/src/test/java/sc/fiji/labkit/ui/labeling/LabelingSerializationTest.java index 66b05147..3cb498cf 100644 --- a/src/test/java/sc/fiji/labkit/ui/labeling/LabelingSerializationTest.java +++ b/src/test/java/sc/fiji/labkit/ui/labeling/LabelingSerializationTest.java @@ -38,9 +38,11 @@ import net.imglib2.img.Img; import net.imglib2.img.array.ArrayImgs; import net.imglib2.roi.IterableRegion; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.scijava.Context; import sc.fiji.labkit.ui.utils.sparse.SparseIterableRegion; import net.imglib2.test.ImgLib2Assert; -import sc.fiji.labkit.pixel_classification.utils.SingletonContext; import net.imglib2.type.logic.BitType; import net.imglib2.type.numeric.ARGBType; import net.imglib2.type.numeric.integer.UnsignedByteType; @@ -65,8 +67,19 @@ */ public class LabelingSerializationTest { - private final LabelingSerializer serializer = new LabelingSerializer(SingletonContext - .getInstance()); + private static Context context; + + @BeforeClass + public static void setUp() { + context = new Context(); + } + + @AfterClass + public static void tearDown() { + context.dispose(); + } + + private final LabelingSerializer serializer = new LabelingSerializer(context); @Test public void testJson() throws IOException { @@ -155,7 +168,7 @@ private static IterableRegion exampleRegion(long... position) { public void testOpenFromTiff() throws IOException { Img input = ArrayImgs.unsignedBytes(new byte[] { 10, 20, 0, 0 }, 2, 2); Path file = Files.createTempFile("test", ".tif"); - new ImgSaver(SingletonContext.getInstance()).saveImg(file.toString(), input, new SCIFIOConfig() + new ImgSaver(context).saveImg(file.toString(), input, new SCIFIOConfig() .writerSetFailIfOverwriting(false)); Labeling labeling = serializer.open(file.toString()); assertEquals(Arrays.asList("10", "20"), labeling.getLabels().stream().map(Label::name).collect( diff --git a/src/test/java/sc/fiji/labkit/ui/models/CustomCachedImageFactoryDemo.java b/src/test/java/sc/fiji/labkit/ui/models/CustomCachedImageFactoryDemo.java index f89b38c9..6f4bb4f1 100644 --- a/src/test/java/sc/fiji/labkit/ui/models/CustomCachedImageFactoryDemo.java +++ b/src/test/java/sc/fiji/labkit/ui/models/CustomCachedImageFactoryDemo.java @@ -37,10 +37,10 @@ import net.imglib2.img.VirtualStackAdapter; import net.imglib2.img.array.ArrayImgFactory; import net.imglib2.img.cell.CellGrid; +import org.scijava.Context; import sc.fiji.labkit.ui.LabkitFrame; import sc.fiji.labkit.ui.inputimage.DatasetInputImage; import sc.fiji.labkit.ui.segmentation.Segmenter; -import sc.fiji.labkit.pixel_classification.utils.SingletonContext; import net.imglib2.type.NativeType; import java.util.function.Consumer; @@ -57,21 +57,19 @@ */ public class CustomCachedImageFactoryDemo { - static { - LegacyInjector.preinit(); - } - public static void main(String... args) { + Context context = new Context(); final ImagePlus imp = new ImagePlus("https://imagej.nih.gov/ij/images/FluorescentCells.jpg"); ImgPlus image = VirtualStackAdapter.wrap(imp); - DefaultSegmentationModel segmentationModel = new DefaultSegmentationModel(SingletonContext - .getInstance(), new DatasetInputImage(image)); + DefaultSegmentationModel segmentationModel = new DefaultSegmentationModel(context, + new DatasetInputImage(image)); segmentationModel.extensionPoints().setCachedSegmentationImageFactory( CustomCachedImageFactoryDemo::noCacheFactory); segmentationModel.extensionPoints().setCachedPredictionImageFactory( CustomCachedImageFactoryDemo::noCacheFactory); LabkitFrame.show(segmentationModel, - "Custom Cached Image Factory Demo, That actually uses no cahce."); + "Custom Cached Image Factory Demo, That actually uses no cache."); + // TODO: Dispose context when Labkit frame is closed. } private static > Img noCacheFactory(Segmenter segmenter, diff --git a/src/test/java/sc/fiji/labkit/ui/models/DefaultSegmentationModelTest.java b/src/test/java/sc/fiji/labkit/ui/models/DefaultSegmentationModelTest.java index aabc5e02..f1282e4e 100644 --- a/src/test/java/sc/fiji/labkit/ui/models/DefaultSegmentationModelTest.java +++ b/src/test/java/sc/fiji/labkit/ui/models/DefaultSegmentationModelTest.java @@ -30,6 +30,8 @@ package sc.fiji.labkit.ui.models; import net.imglib2.img.array.ArrayImgs; +import org.junit.AfterClass; +import org.junit.BeforeClass; import sc.fiji.labkit.ui.inputimage.DatasetInputImage; import sc.fiji.labkit.ui.labeling.Label; import sc.fiji.labkit.ui.labeling.Labeling; @@ -52,6 +54,18 @@ public class DefaultSegmentationModelTest { + private static Context context; + + @BeforeClass + public static void setUp() { + context = new Context(); + } + + @AfterClass + public static void tearDown() { + context.dispose(); + } + @Test public void testListener() { BitType flag = new BitType(false); @@ -60,7 +74,7 @@ public void testListener() { model.segmenters().notifier().addListener(flag::setOne); assertFalse(flag.get()); assertEquals(0, model.segmenters().get().size()); - model.addSegmenter(PixelClassificationPlugin.create()); + model.addSegmenter(PixelClassificationPlugin.create(context)); assertTrue(flag.get()); assertEquals(1, model.segmenters().get().size()); flag.set(false); @@ -84,7 +98,7 @@ public void testLoadClassifierWithDifferentLabels() throws IOException { List