ElementFractionWrapper extends BoneJCommand { @Override public void run() { statusService.showStatus("Element fraction: initializing"); findSubspaces(inputImage); prepareResultDisplay(); final String name = inputImage.getName(); for (int i = 0; i < subspaces.size(); i++) { final Subspace<BitType> subspace = subspaces.get(i); statusService.showStatus("Element fraction: calculating subspace #" + (i + 1)); statusService.showProgress(i, subspaces.size()); final IterableInterval<BitType> interval = Views.flatIterable( subspace.interval); final double foregroundSize = opService.stats().sum(interval) .getRealDouble() * elementSize; final double totalSize = interval.size() * elementSize; final double ratio = foregroundSize / totalSize; final String suffix = subspace.toString(); final String label = suffix.isEmpty() ? name : name + " " + suffix; addResults(label, foregroundSize, totalSize, ratio); } resultsTable = SharedTable.getTable(); reportUsage(); }  @Override void run();  }
@Test public void testResults3DHyperstack() throws Exception { final String unit = "mm"; final double scale = 0.9; final int cubeSide = 5; final int stackSide = cubeSide + 2; final int expectedSize = 4; final double elementSize = scale * scale * scale; final double cubeVolume = cubeSide * cubeSide * cubeSide * elementSize; final double spaceVolume = stackSide * stackSide * stackSide * elementSize; final double ratio = cubeVolume / spaceVolume; final double[] expectedVolumes = { cubeVolume, 0, 0, cubeVolume }; final double[] expectedTotalVolumes = { spaceVolume, spaceVolume, spaceVolume, spaceVolume }; final double[] expectedRatios = { ratio, 0, 0, ratio }; final double[][] expectedValues = { expectedVolumes, expectedTotalVolumes, expectedRatios }; final String[] expectedHeaders = { "Bone volume (" + unit + "³)", "Total volume (" + unit + "³)", "Volume ratio" }; final Img<BitType> img = ArrayImgs.bits(stackSide, stackSide, stackSide, 2, 2); Views.interval(img, new long[] { 1, 1, 1, 0, 0 }, new long[] { 5, 5, 5, 0, 0 }).forEach(BitType::setOne); Views.interval(img, new long[] { 1, 1, 1, 1, 1 }, new long[] { 5, 5, 5, 1, 1 }).forEach(BitType::setOne); final double[] calibration = { scale, scale, scale, 1.0, 1.0 }; final String[] units = { unit, unit, unit, "", "" }; final AxisType[] axes = { Axes.X, Axes.Y, Axes.Z, Axes.TIME, Axes.CHANNEL }; final ImgPlus<BitType> imgPlus = new ImgPlus<>(img, "Cube", axes, calibration, units); final CommandModule module = command().run( ElementFractionWrapper.class, true, "inputImage", imgPlus).get(); @SuppressWarnings("unchecked") final List<DefaultColumn<Double>> table = (List<DefaultColumn<Double>>) module.getOutput("resultsTable"); assertNotNull(table); assertEquals("Wrong number of columns", 3, table.size()); for (int i = 0; i < 3; i++) { final DefaultColumn<Double> column = table.get(i); assertEquals("Column has wrong number of rows", expectedSize, column .size()); assertEquals("Column has incorrect header", expectedHeaders[i], column .getHeader()); for (int j = 0; j < expectedSize; j++) { assertEquals("Incorrect value at row " + j + ", column " + i, expectedValues[i][j], column.get(j), 1e-12); } } }
@Test public void testResultsComposite2D() throws Exception { final String unit = "mm"; final int squareSide = 5; final int stackSide = squareSide + 2; final int expectedSize = 2; final double squareArea = squareSide * squareSide; final double spaceArea = stackSide * stackSide; final double ratio = squareArea / spaceArea; final double[] expectedAreas = { 0, squareArea }; final double[] expectedTotalAreas = { spaceArea, spaceArea }; final double[] expectedRatios = { 0, ratio }; final double[][] expectedValues = { expectedAreas, expectedTotalAreas, expectedRatios }; final String[] expectedHeaders = { "Bone area (" + unit + "\u00B2)", "Total area (" + unit + "\u00B2)", "Area ratio" }; final Img<BitType> img = ArrayImgs.bits(stackSide, stackSide, 2); Views.interval(img, new long[] { 1, 1, 1 }, new long[] { 5, 5, 1 }).forEach( BitType::setOne); final double[] calibration = { 1.0, 1.0, 1.0 }; final String[] units = { unit, unit, "" }; final AxisType[] axes = { Axes.X, Axes.Y, Axes.CHANNEL }; final ImgPlus<BitType> imgPlus = new ImgPlus<>(img, "Square", axes, calibration, units); final CommandModule module = command().run( ElementFractionWrapper.class, true, "inputImage", imgPlus).get(); @SuppressWarnings("unchecked") final List<DefaultColumn<Double>> table = (List<DefaultColumn<Double>>) module.getOutput("resultsTable"); assertNotNull(table); assertEquals("Wrong number of columns", 3, table.size()); for (int i = 0; i < 3; i++) { final DefaultColumn<Double> column = table.get(i); assertEquals("Column has wrong number of rows", expectedSize, column .size()); assertEquals("Column has incorrect header", expectedHeaders[i], column .getHeader()); for (int j = 0; j < expectedSize; j++) { assertEquals("Incorrect value at row " + j + ", column " + i, expectedValues[i][j], column.get(j), 1e-12); } } }
@Test public void testWeirdSpatialImageCancelsPlugin() throws Exception { final SwingDialogPrompt mockPrompt = mock(SwingDialogPrompt.class); when(MOCK_UI.dialogPrompt(anyString(), anyString(), any(), any())) .thenReturn(mockPrompt); final DefaultLinearAxis xAxis = new DefaultLinearAxis(Axes.X); final DefaultLinearAxis cAxis = new DefaultLinearAxis(Axes.CHANNEL); final Img<DoubleType> img = ArrayImgs.doubles(5, 5); final ImgPlus<DoubleType> imgPlus = new ImgPlus<>(img, "Test image", xAxis, cAxis); final CommandModule module = command().run( ElementFractionWrapper.class, true, "inputImage", imgPlus).get(); assertTrue("A non 2D and 3D image should have cancelled the plugin", module .isCanceled()); assertEquals("Cancel reason is incorrect", CommonMessages.WEIRD_SPATIAL, module.getCancelReason()); verify(MOCK_UI, timeout(1000)).dialogPrompt(anyString(), anyString(), any(), any()); }