AxisUtils { public static <S extends AnnotatedSpace<C>, C extends CalibratedAxis> Optional<String> getSpatialUnit(final S space, final UnitService unitService) throws IllegalArgumentException { if (!hasSpatialDimensions(space)) { throw new IllegalArgumentException("Space has no spatial axes."); } if (!isUnitsConvertible(space, unitService)) { return Optional.empty(); } final String unit = space.axis(0).unit(); return unit == null ? Optional.of("") : Optional.of(unit); } private  AxisUtils(); static long countSpatialDimensions(final S space); static Optional<String> getSpatialUnit(final S space, final UnitService unitService); static boolean hasChannelDimensions(final S space); static boolean hasSpatialDimensions(final S space); static boolean hasTimeDimensions(final S space); static boolean isSpatialCalibrationsIsotropic(final S space, final double tolerance,
			final UnitService unitService);  }
@Test public void testGetSpatialUnit() { final DefaultLinearAxis xAxis = new DefaultLinearAxis(Axes.X, "µm", 1.0); final DefaultLinearAxis yAxis = new DefaultLinearAxis(Axes.Y, "mm", 5.0); final Img<ByteType> img = ArrayImgs.bytes(1, 1); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", xAxis, yAxis); final Optional<String> unit = AxisUtils.getSpatialUnit(imgPlus, unitService); assertTrue("String should be present when units are convertible", unit .isPresent()); assertEquals("Unit is incorrect", "µm", unit.get()); }
@Test public void testGetSpatialUnitAllAxesUncalibrated() { final DefaultLinearAxis xAxis = new DefaultLinearAxis(Axes.X, 1.0); final DefaultLinearAxis yAxis = new DefaultLinearAxis(Axes.Y, "", 5.0); final Img<ByteType> img = ArrayImgs.bytes(1, 1); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", xAxis, yAxis); final Optional<String> unit = AxisUtils.getSpatialUnit(imgPlus, unitService); assertTrue("String should be present when units are convertible", unit .isPresent()); assertTrue("Unit should be empty", unit.get().isEmpty()); }
@Test public void testGetSpatialUnitInconvertibleUnits() { final String[][] units = { { "m", "" }, { "cm", "kg" } }; final Img<ByteType> img = ArrayImgs.bytes(1, 1); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img); for (final String[] unit : units) { final CalibratedAxis xAxis = new DefaultLinearAxis(Axes.X, unit[0]); final CalibratedAxis yAxis = new DefaultLinearAxis(Axes.Y, unit[1]); imgPlus.setAxis(xAxis, 0); imgPlus.setAxis(yAxis, 1); final Optional<String> result = AxisUtils.getSpatialUnit(imgPlus, unitService); assertFalse(result.isPresent()); } }
@Test(expected = IllegalArgumentException.class) public void testGetSpatialUnitThrowsIAEIfNoSpatialAxes() { final DefaultLinearAxis cAxis = new DefaultLinearAxis(Axes.CHANNEL); final DefaultLinearAxis tAxis = new DefaultLinearAxis(Axes.TIME); final Img<ByteType> img = ArrayImgs.bytes(1, 1); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", cAxis, tAxis); AxisUtils.getSpatialUnit(imgPlus, unitService); }
@Test(expected = NullPointerException.class) public void testGetSpatialUnitThrowsNPEIfSpaceNull() { AxisUtils.getSpatialUnit(null, unitService); }
@Test(expected = NullPointerException.class) public void testGetSpatialUnitThrowsNPEIfUnitServiceNull() { final DefaultLinearAxis xAxis = new DefaultLinearAxis(Axes.X, "µm", 1.0); final DefaultLinearAxis yAxis = new DefaultLinearAxis(Axes.Y, "mm", 5.0); final Img<ByteType> img = ArrayImgs.bytes(1, 1); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", xAxis, yAxis); AxisUtils.getSpatialUnit(imgPlus, null); }