Skip to content

Commit

Permalink
Unit tests: add class TestResources for querying paths of resource files
Browse files Browse the repository at this point in the history
  • Loading branch information
maarzt committed Jan 11, 2024
1 parent 214ff98 commit a31d5a8
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 82 deletions.
3 changes: 2 additions & 1 deletion src/test/java/demo/LabelingComponentHdf5Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import sc.fiji.labkit.ui.LabelingComponent;
import sc.fiji.labkit.ui.inputimage.SpimDataInputImage;
import sc.fiji.labkit.ui.models.ImageLabelingModel;
import sc.fiji.labkit.ui.utils.TestResources;

import javax.swing.*;
import java.awt.event.WindowAdapter;
Expand All @@ -46,7 +47,7 @@ public class LabelingComponentHdf5Demo {

public static void main(String... args) throws SpimDataException {
JFrame frame = initFrame();
final String fn = LabelingComponentHdf5Demo.class.getResource("/export.xml").getPath();
final String fn = TestResources.fullPath("/export.xml");
frame.add(initLabelingComponent(frame, fn));
frame.setVisible(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.imglib2.type.numeric.integer.UnsignedShortType;
import net.imglib2.util.Cast;
import org.junit.Test;
import sc.fiji.labkit.ui.utils.TestResources;

import java.util.Arrays;
import java.util.stream.IntStream;
Expand All @@ -50,7 +51,7 @@ public class SpimDataToImgPlusTest {

@Test
public void testOpen() {
String filename = SpimDataToImgPlusTest.class.getResource("/export.xml").getPath();
String filename = TestResources.fullPath("/export.xml");
ImgPlus<UnsignedShortType> result = Cast.unchecked(SpimDataToImgPlus.open(filename, 0));
assertEquals(Arrays.asList(Axes.X, Axes.Y, Axes.Z, Axes.CHANNEL, Axes.TIME), ImgPlusViewsOld
.getAxes(result));
Expand All @@ -60,14 +61,13 @@ public void testOpen() {

@Test(expected = SpimDataInputException.class)
public void testExceptionForMultipleAngles() {
String filename = SpimDataToImgPlusTest.class.getResource("/multi-angle-dataset.xml").getPath();
String filename = TestResources.fullPath("/multi-angle-dataset.xml");
SpimDataToImgPlus.open(filename, 0);
}

@Test(expected = SpimDataInputException.class)
public void testExceptionForSizeMismatch() {
String filename = SpimDataToImgPlusTest.class.getResource("/size-mismatch-dataset.xml")
.getPath();
String filename = TestResources.fullPath("/size-mismatch-dataset.xml");
SpimDataToImgPlus.open(filename, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
import net.imglib2.test.ImgLib2Assert;
import org.junit.Test;
import sc.fiji.labkit.pixel_classification.utils.SingletonContext;
import sc.fiji.labkit.ui.utils.TestResources;

public class CalculateProbabilityMapWithLabkitIJ1PluginTest {

@Test
public void test() throws IOException {
SingletonContext.getInstance();
String inputImage = fullPath("/blobs.tif");
String blobsModel = fullPath("/blobs.classifier");
String source = fullPath("/blobs_probability_map.tif");
String inputImage = TestResources.fullPath("/blobs.tif");
String blobsModel = TestResources.fullPath("/blobs.classifier");
String source = TestResources.fullPath("/blobs_probability_map.tif");
File outputImage = File.createTempFile("labkit-segmentation-test", ".tif");
String macroTemplate = "close('*');\n" +
"open('INPUT_TIF');\n" +
Expand All @@ -37,11 +38,7 @@ public void test() throws IOException {
assertTrue(outputImage.exists());
ImagePlus expected = IJ.openImage(source);
ImagePlus result = IJ.openImage(outputImage.getAbsolutePath());
ImgLib2Assert.assertImageEquals(VirtualStackAdapter.wrap(expected), VirtualStackAdapter.wrap(result), Object::equals);
}

private String fullPath(String name) {
return SegmentImageWithLabkitPluginTest.class.getResource(
name).getFile();
ImgLib2Assert.assertImageEquals(VirtualStackAdapter.wrap(expected),
VirtualStackAdapter.wrap(result), Object::equals);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@
import net.imglib2.test.ImgLib2Assert;
import org.junit.Test;
import sc.fiji.labkit.pixel_classification.utils.SingletonContext;
import sc.fiji.labkit.ui.utils.TestResources;

public class CalculateProbabilityMapWithLabkitPluginTest {

@Test
public void test() throws Exception {
// setup
ImageJ imageJ = new ImageJ(SingletonContext.getInstance());
Dataset image = imageJ.scifio().datasetIO().open(fullPath("/blobs.tif"));
String blobsModel = fullPath("/blobs.classifier");
Dataset expectedImage = imageJ.scifio().datasetIO().open(fullPath(
Dataset image = imageJ.scifio().datasetIO().open(TestResources.fullPath("/blobs.tif"));
String blobsModel = TestResources.fullPath("/blobs.classifier");
Dataset expectedImage = imageJ.scifio().datasetIO().open(TestResources.fullPath(
"/blobs_probability_map.tif"));
// process
Dataset output = (Dataset) imageJ.command().run(CalculateProbabilityMapWithLabkitPlugin.class,
Expand All @@ -55,9 +56,4 @@ public void test() throws Exception {
// test
ImgLib2Assert.assertImageEqualsRealType(expectedImage, output, 0.0);
}

private String fullPath(String name) {
return CalculateProbabilityMapWithLabkitPluginTest.class.getResource(
name).getFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.scijava.io.location.FileLocation;
import org.scijava.plugin.Parameter;
import sc.fiji.labkit.pixel_classification.utils.SingletonContext;
import sc.fiji.labkit.ui.utils.TestResources;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -81,15 +82,15 @@ public void test()
"file_filter", "*.tif",
"output_directory", outputDirectory,
"output_file_suffix", "_probability_map.tif",
"segmenter_file", fullPath("/leaf.classifier"),
"segmenter_file", TestResources.fullPath("/leaf.classifier"),
"use_gpu", false).get();
testOutputDirectory(outputDirectory);
}

private File createTestInputDirectory() throws IOException {
File folder = Files.createTempDirectory("labkit-test-input").toFile();
RandomAccessibleInterval<UnsignedByteType> image =
Cast.unchecked(io.open(fullPath("/leaf.tif")));
Cast.unchecked(io.open(TestResources.fullPath("/leaf.tif")));
for (int i = 0; i < 4; i++) {
File file = new File(folder, "image" + i + ".tif");
saveImage(image, file);
Expand All @@ -99,8 +100,8 @@ private File createTestInputDirectory() throws IOException {
}

private void testOutputDirectory(File folder) throws IOException {
RandomAccessibleInterval<? extends RealType<?>> expected = io.open(fullPath(
"/leaf_probability_map.tif"));
RandomAccessibleInterval<? extends RealType<?>> expected = io.open(
TestResources.fullPath("/leaf_probability_map.tif"));
for (int i = 0; i < 4; i++) {
File file = new File(folder, "image" + i + "_probability_map.tif");
assertTrue("Expected output file is missing: " + file, file.exists());
Expand All @@ -121,9 +122,4 @@ private void saveImage(RandomAccessibleInterval<UnsignedByteType> image, File fi
dataset.axis(2).setType(Axes.CHANNEL);
io.save(dataset, new FileLocation(file));
}

private String fullPath(String name) {
return this.getClass().getResource(name).getFile();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.scijava.io.location.FileLocation;
import org.scijava.plugin.Parameter;
import sc.fiji.labkit.pixel_classification.utils.SingletonContext;
import sc.fiji.labkit.ui.utils.TestResources;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -83,15 +84,15 @@ public void test()
"file_filter", "*.tif",
"output_directory", outputDirectory,
"output_file_suffix", "_segmentation.tif",
"segmenter_file", fullPath("/leaf.classifier"),
"segmenter_file", TestResources.fullPath("/leaf.classifier"),
"use_gpu", false).get();
testOutputDirectory(outputDirectory);
}

private File createTestInputDirectory() throws IOException {
File folder = Files.createTempDirectory("labkit-test-input").toFile();
RandomAccessibleInterval<UnsignedByteType> image =
Cast.unchecked(io.open(fullPath("/leaf.tif")));
Cast.unchecked(io.open(TestResources.fullPath("/leaf.tif")));
for (int i = 0; i < 4; i++) {
File file = new File(folder, "image" + i + ".tif");
saveImage(image, file);
Expand All @@ -101,7 +102,7 @@ private File createTestInputDirectory() throws IOException {
}

private void testOutputDirectory(File folder) throws IOException {
RandomAccessibleInterval<? extends RealType<?>> expected = io.open(fullPath(
RandomAccessibleInterval<? extends RealType<?>> expected = io.open(TestResources.fullPath(
"/leaf_segmentation.tif"));
for (int i = 0; i < 4; i++) {
File file = new File(folder, "image" + i + "_segmentation.tif");
Expand Down Expand Up @@ -134,9 +135,4 @@ private void assertAlmostIdentical(RandomAccessibleInterval<? extends RealType<?
});
assertTrue(count[0] < 10);
}

private String fullPath(String name) {
return this.getClass().getResource(name).getFile();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
import net.imglib2.test.ImgLib2Assert;
import org.junit.Test;
import sc.fiji.labkit.pixel_classification.utils.SingletonContext;
import sc.fiji.labkit.ui.utils.TestResources;

public class SegmentImageWithLabkitIJ1PluginTest {

@Test
public void test() throws IOException {
SingletonContext.getInstance();
String inputImage = fullPath("/blobs.tif");
String blobsModel = fullPath("/blobs.classifier");
String source = fullPath("/blobs_segmentation.tif");
String inputImage = TestResources.fullPath("/blobs.tif");
String blobsModel = TestResources.fullPath("/blobs.classifier");
String source = TestResources.fullPath("/blobs_segmentation.tif");
File outputImage = File.createTempFile("labkit-segmentation-test", ".tif");
String macroTemplate = "close('*');\n" +
"open('INPUT_TIF');\n" +
Expand All @@ -37,11 +38,7 @@ public void test() throws IOException {
assertTrue(outputImage.exists());
ImagePlus expected = IJ.openImage(source);
ImagePlus result = IJ.openImage(outputImage.getAbsolutePath());
ImgLib2Assert.assertImageEquals(VirtualStackAdapter.wrap(expected), VirtualStackAdapter.wrap(result), Object::equals);
}

private String fullPath(String name) {
return SegmentImageWithLabkitPluginTest.class.getResource(
name).getFile();
ImgLib2Assert.assertImageEquals(VirtualStackAdapter.wrap(expected),
VirtualStackAdapter.wrap(result), Object::equals);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@
import net.imglib2.test.ImgLib2Assert;
import org.junit.Test;
import sc.fiji.labkit.pixel_classification.utils.SingletonContext;
import sc.fiji.labkit.ui.utils.TestResources;

public class SegmentImageWithLabkitPluginTest {

@Test
public void test() throws Exception {
// setup
ImageJ imageJ = new ImageJ(SingletonContext.getInstance());
Dataset image = imageJ.scifio().datasetIO().open(fullPath("/blobs.tif"));
String blobsModel = fullPath("/blobs.classifier");
Dataset expectedImage = imageJ.scifio().datasetIO().open(fullPath("/blobs_segmentation.tif"));
Dataset image = imageJ.scifio().datasetIO().open(TestResources.fullPath("/blobs.tif"));
String blobsModel = TestResources.fullPath("/blobs.classifier");
Dataset expectedImage = imageJ.scifio().datasetIO().open(TestResources.fullPath("/blobs_segmentation.tif"));
// process
Dataset output = (Dataset) imageJ.command().run(SegmentImageWithLabkitPlugin.class, true,
"input", image,
Expand All @@ -53,9 +54,4 @@ public void test() throws Exception {
// test
ImgLib2Assert.assertImageEqualsRealType(expectedImage, output, 0.0);
}

private String fullPath(String name) {
return SegmentImageWithLabkitPluginTest.class.getResource(
name).getFile();
}
}
27 changes: 10 additions & 17 deletions src/test/java/sc/fiji/labkit/ui/plugin/SegmentRGBImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,11 @@

import net.imagej.Dataset;
import net.imagej.ImageJ;
import net.imagej.ImgPlus;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.test.ImgLib2Assert;
import net.imglib2.type.numeric.integer.ShortType;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.ValuePair;
import net.imglib2.view.Views;
import org.junit.Test;
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.InputImage;
Expand All @@ -52,6 +46,7 @@
import sc.fiji.labkit.ui.models.SegmentationModel;
import sc.fiji.labkit.ui.segmentation.SegmentationPlugin;
import sc.fiji.labkit.ui.segmentation.weka.PixelClassificationPlugin;
import sc.fiji.labkit.ui.utils.TestResources;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -64,8 +59,8 @@ public void testSegmentation()
throws IOException, ExecutionException, InterruptedException
{
ImageJ imageJ = new ImageJ(SingletonContext.getInstance());
Dataset image = imageJ.scifio().datasetIO().open(fullPath("/leaf.tif"));
String blobsModel = fullPath("/leaf.classifier");
Dataset image = imageJ.scifio().datasetIO().open(TestResources.fullPath("/leaf.tif"));
String blobsModel = TestResources.fullPath("/leaf.classifier");
// process
Dataset output = (Dataset) imageJ.command().run(CalculateProbabilityMapWithLabkitPlugin.class,
true,
Expand All @@ -74,19 +69,20 @@ public void testSegmentation()
"use_gpu", false)
.get().getOutput("output");
// test
Dataset expectedImage = imageJ.scifio().datasetIO().open(fullPath("/leaf_probability_map.tif"));
Dataset expectedImage = imageJ.scifio().datasetIO().open(
TestResources.fullPath("/leaf_probability_map.tif"));
ImgLib2Assert.assertImageEqualsRealType(expectedImage, output, 0.0);
}

@Test
public void testTraining() throws IOException {
ImageJ imageJ = new ImageJ(SingletonContext.getInstance());
Dataset image = imageJ.scifio().datasetIO().open(fullPath("/leaf.tif"));
Dataset image = imageJ.scifio().datasetIO().open(TestResources.fullPath("/leaf.tif"));
InputImage inputImage = new DatasetInputImage(image);
SegmentationModel segmentationModel = new DefaultSegmentationModel(imageJ.context(),
inputImage);
Labeling labeling = new LabelingSerializer(SingletonContext.getInstance()).open(fullPath(
"/leaf.tif.labeling"));
Labeling labeling = new LabelingSerializer(SingletonContext.getInstance()).open(
TestResources.fullPath("/leaf.tif.labeling"));
segmentationModel.imageLabelingModel().labeling().set(labeling);
SegmentationPlugin plugin = PixelClassificationPlugin.create();
SegmentationItem segmenter = segmentationModel.segmenterList().addSegmenter(plugin);
Expand All @@ -98,11 +94,8 @@ public void testTraining() throws IOException {
.imageLabelingModel()).prediction();

// output
Dataset expectedImage = imageJ.scifio().datasetIO().open(fullPath("/leaf_probability_map.tif"));
Dataset expectedImage = imageJ.scifio().datasetIO().open(
TestResources.fullPath("/leaf_probability_map.tif"));
ImgLib2Assert.assertImageEqualsRealType(expectedImage, prediction, 0.0);
}

private String fullPath(String name) {
return this.getClass().getResource(name).getFile();
}
}
Loading

0 comments on commit a31d5a8

Please sign in to comment.