|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Fiji distribution of ImageJ for the life sciences. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2010 - 2023 Fiji developers. |
| 6 | + * %% |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as |
| 9 | + * published by the Free Software Foundation, either version 3 of the |
| 10 | + * License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public |
| 18 | + * License along with this program. If not, see |
| 19 | + * <http://www.gnu.org/licenses/gpl-3.0.html>. |
| 20 | + * #L% |
| 21 | + */ |
| 22 | +package trainableSegmentation; |
| 23 | + |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | +import static org.junit.Assert.assertNotNull; |
| 26 | +import static org.junit.Assert.fail; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.net.URL; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | +import java.util.ArrayList; |
| 33 | + |
| 34 | +import org.junit.Test; |
| 35 | + |
| 36 | +import ij.ImagePlus; |
| 37 | +import ij.gui.Roi; |
| 38 | + |
| 39 | +public class ExamplesTest { |
| 40 | + @Test |
| 41 | + public void testExamples() { |
| 42 | + final URL url = getClass().getResource("/bridge.png"); |
| 43 | + final ImagePlus bridge = new ImagePlus(url.getPath());; |
| 44 | + assertNotNull(bridge); |
| 45 | + |
| 46 | + final ArrayList<Roi> expected_examples = new ArrayList<Roi>(); |
| 47 | + final Roi roi = new Roi(100, 100, 30, 30); |
| 48 | + expected_examples.add(roi); |
| 49 | + |
| 50 | + WekaSegmentation segmentator = new WekaSegmentation(bridge); |
| 51 | + segmentator.addClass(); |
| 52 | + assertEquals(3, segmentator.getNumOfClasses()); |
| 53 | + segmentator.setClassLabel(2, "label"); |
| 54 | + segmentator.addExample(0, new Roi(10, 10, 50, 50), 1); |
| 55 | + segmentator.addExample(1, new Roi(400, 400, 30, 30), 1); |
| 56 | + segmentator.addExample(2, roi, 1); |
| 57 | + try { |
| 58 | + final Path path = Files.createTempFile("tws-", ".twse.gz"); |
| 59 | + segmentator.saveExamples(path.toString()); |
| 60 | + |
| 61 | + segmentator.removeClass(0); |
| 62 | + assertEquals(2, segmentator.getNumOfClasses()); |
| 63 | + assertEquals("class 2", segmentator.getClassLabel(0)); |
| 64 | + assertEquals("label", segmentator.getClassLabel(1)); |
| 65 | + assertEquals("class 3", segmentator.getClassLabel(2)); |
| 66 | + assertEquals(expected_examples, segmentator.getExamples(1, 1)); |
| 67 | + |
| 68 | + segmentator = new WekaSegmentation(bridge); |
| 69 | + assertEquals(2, segmentator.getNumOfClasses()); |
| 70 | + segmentator.loadExamples(path.toString()); |
| 71 | + assertEquals(3, segmentator.getNumOfClasses()); |
| 72 | + assertEquals("label", segmentator.getClassLabel(2)); |
| 73 | + assertEquals(expected_examples, segmentator.getExamples(2, 1)); |
| 74 | + |
| 75 | + Files.delete(path); |
| 76 | + } catch (IOException e) { |
| 77 | + e.printStackTrace(); |
| 78 | + fail("Caught IOException"); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments