HyperstackUtils { public static <T extends RealType<T> & NativeType<T>> Stream<Subspace<T>> splitSubspaces(final ImgPlus<T> hyperStack, final Collection<AxisType> subspaceTypes) { final Builder<Subspace<T>> builder = Stream.builder(); final int[] splitIndices = findSplitAxisIndices(hyperStack, subspaceTypes); final long[] typeSubscripts = mapTypeSubscripts(hyperStack, splitIndices); final int numSplits = splitIndices.length; final HyperAxisMeta[] subspaceMeta = new HyperAxisMeta[numSplits]; final List<ValuePair<IntType, LongType>> split = new ArrayList<>(); splitDims(hyperStack, splitIndices, numSplits - 1, subspaceMeta, split, typeSubscripts, builder); return builder.build(); } private  HyperstackUtils(); static Stream<Subspace<T>> split3DSubspaces(final ImgPlus<T> hyperStack); static Stream<Subspace<T>> splitSubspaces(final ImgPlus<T> hyperStack,
			final Collection<AxisType> subspaceTypes);  }
@Test public void testMultipleAxesOfType() { final Img<ByteType> img = ArrayImgs.bytes(2, 2, 2, 2); final Iterator<long[]> expectedSubscripts = Stream.generate( () -> new long[] { 1, 2 }).limit(4).iterator(); final Iterator<String> expectedStrings = Stream.of("Time: 1, Time(2): 1", "Time: 2, Time(2): 1", "Time: 1, Time(2): 2", "Time: 2, Time(2): 2") .iterator(); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", X_AXIS, Y_AXIS, T_AXIS, T_AXIS); final List<AxisType> types = Arrays.asList(Axes.X, Axes.Y); final Stream<Subspace<ByteType>> subspaces = splitSubspaces(imgPlus, types); subspaces.forEach(subspace -> { assertTrue(subspace.getAxisTypes().allMatch(t -> t == Axes.TIME)); assertArrayEquals( "The subscripts of the multiple axes of the same type are incorrect", expectedSubscripts.next(), subspace.getSubScripts().toArray()); assertEquals("The string describing the subspace position is incorrect", expectedStrings.next(), subspace.toString()); }); }
@Test public void testSplit3DSubspacesWith2DImgPlus() { final long height = 3; final Img<ByteType> img = ArrayImgs.bytes(2, height); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", X_AXIS, Y_AXIS); final List<AxisType> subspaceTypes = Arrays.asList(Axes.X, Axes.Z); final List<Subspace<ByteType>> subspaces = splitSubspaces(imgPlus, subspaceTypes).collect(Collectors.toList()); assertEquals(height, subspaces.size()); subspaces.forEach(s -> { final List<AxisType> resultTypes = s.getAxisTypes().collect(Collectors .toList()); assertEquals(1, resultTypes.size()); assertEquals(Axes.Y, resultTypes.get(0)); }); }
@Test public void testSplitSubspaces() { final long tSize = 3; final long cSize = 3; final long wSize = 3; final long expectedSubspaces = tSize * cSize * wSize; final long depth = 2; final long height = 10; final long width = 15; final Img<ByteType> img = ArrayImgs.bytes(width, height, depth, wSize, cSize, tSize); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", X_AXIS, Y_AXIS, Z_AXIS, W_AXIS, C_AXIS, T_AXIS); final List<Subspace<ByteType>> subspaces = HyperstackUtils.split3DSubspaces( imgPlus).collect(Collectors.toList()); assertEquals("Wrong number of subspaces", expectedSubspaces, subspaces .size()); subspaces.stream().map(s -> s.interval).forEach(v -> { assertEquals("Subspace has wrong number of dimensions", 3, v .numDimensions()); assertEquals("Incorrect max X", width - 1, v.max(0)); assertEquals("Incorrect min X", 0, v.min(0)); assertEquals("Incorrect max Y", height - 1, v.max(1)); assertEquals("Incorrect min Y", 0, v.min(1)); assertEquals("Incorrect max Z", depth - 1, v.max(2)); assertEquals("Incorrect min Z", 0, v.min(2)); }); }
@Test public void testSplitSubspacesEmptySubspaces() { final Img<ByteType> img = ArrayImgs.bytes(2, 2); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", X_AXIS, Y_AXIS); final List<AxisType> subspaceTypes = Collections.singletonList( Axes.CHANNEL); final Stream<Subspace<ByteType>> subspaces = splitSubspaces(imgPlus, subspaceTypes); assertEquals(0, subspaces.count()); }
@Test public void testSplitSubspacesEmptyTypes() { final Img<ByteType> img = ArrayImgs.bytes(2, 2); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", X_AXIS, Y_AXIS); final List<AxisType> emptyList = Collections.emptyList(); final Stream<Subspace<ByteType>> subspaces = splitSubspaces(imgPlus, emptyList); assertEquals(0, subspaces.count()); }
@Test public void testSplitSubspacesIdentical() { final Img<ByteType> img = ArrayImgs.bytes(2, 3); final AxisType[] types = { Axes.X, Axes.Y }; final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", types); final List<Subspace<ByteType>> subspaces = splitSubspaces(imgPlus, Arrays .asList(types)).collect(Collectors.toList()); assertEquals(1, subspaces.size()); assertEquals(imgPlus, subspaces.get(0).interval); }
@Test public void testSplitSubspacesMultipleSubspaceTypes() { final long depth = 5; final Img<ByteType> img = ArrayImgs.bytes(2, 2, depth, 13, 14); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", X_AXIS, Y_AXIS, Z_AXIS, T_AXIS, T_AXIS); final List<AxisType> subspaceTypes = Arrays.asList(Axes.X, Axes.Y, Axes.TIME); final Stream<Subspace<ByteType>> subspaces = splitSubspaces(imgPlus, subspaceTypes); assertEquals(depth, subspaces.count()); }
@Test public void testSplitSubspacesNoImgPlusMeta() { final Img<ByteType> img = ArrayImgs.bytes(2, 2, 2); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", BAD_AXIS, BAD_AXIS, BAD_AXIS); final List<AxisType> subspaceTypes = Arrays.asList(Axes.X, Axes.Y); final Stream<Subspace<ByteType>> subspaces = splitSubspaces(imgPlus, subspaceTypes); assertEquals(0, subspaces.count()); }
@Test(expected = NullPointerException.class) public void testSplitSubspacesThrowsNPEIfTypesNull() { final Img<ByteType> img = ArrayImgs.bytes(2, 2); final ImgPlus<ByteType> imgPlus = new ImgPlus<>(img, "", X_AXIS, Y_AXIS); splitSubspaces(imgPlus, null); }
@Test(expected = NullPointerException.class) public void testSplitSubspacesThrowsNPEIfImgPlusNull() { splitSubspaces(null, Arrays.asList(Axes.X, Axes.Y)); }