Skip to content

Commit

Permalink
test: more thorough SingleDimensionPermutationTransform
Browse files Browse the repository at this point in the history
* test Views.permuteCoordinatesInverse for all dimensions
  • Loading branch information
bogovicj authored and tpietzsch committed Feb 19, 2025
1 parent 60c1707 commit b72bb42
Showing 1 changed file with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@
*/
package net.imglib2.transform.integer.permutation;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.IntStream;

import net.imglib2.Cursor;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.array.ArrayCursor;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.img.array.ArrayImgs;
Expand Down Expand Up @@ -202,4 +207,54 @@ public void test()

}

@Test
public void testViewsPermuteCoordinatesInverse()
{
final ArrayImg<IntType, IntArray> img = ArrayImgs.ints(2, 3, 4);

// identity permutation
testHelper( img, d -> {
return IntStream.iterate(0, i -> i+1).limit(img.dimension(d)).toArray();
});

// reversal permutation
testHelper( img, d -> {
return IntStream.iterate((int)img.dimension(d) - 1, i -> i-1).limit(img.dimension(d)).toArray();
});

// cycle permutation
testHelper( img, d -> {
int N = (int)img.dimension(d);
return IntStream.iterate(1, i -> (i+1) % N ).limit(N).toArray();
});
}

private void testHelper(
final RandomAccessibleInterval<IntType> img,
Function<Integer, int[]> permutationForDimension ) {

final int nd = img.numDimensions();
for( int dimToPermute = 0; dimToPermute < nd; dimToPermute++ ) {

fillHyperSlicesWithIndex(img, dimToPermute);
final int[] permutation = permutationForDimension.apply(dimToPermute);

final IntervalView<IntType> permuted = Views.permuteCoordinatesInverse(img, permutation, dimToPermute);
for( int i = 0; i < permuted.dimension(dimToPermute); i++ ) {
final int expectedVal = permutation[i];
Views.hyperSlice(permuted, dimToPermute, i).forEach( x -> {
assertEquals(expectedVal, x.get());
});
}
}
}

private void fillHyperSlicesWithIndex(final RandomAccessibleInterval<IntType> img, int d ) {

for( int i = 0; i < img.dimension(d); i++ ) {
final int val = i;
Views.hyperSlice(img, d, i).forEach( x -> x.set(val));
}
}

}

0 comments on commit b72bb42

Please sign in to comment.