Skip to content

Commit 25d8169

Browse files
committed
test and fix IndexingUtils.computeProjection
1 parent 93b10bb commit 25d8169

3 files changed

Lines changed: 99 additions & 2 deletions

File tree

src/main/java/dev/zarr/zarrjava/utils/IndexingUtils.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static ChunkProjection computeProjection(
8787

8888
if (selOffset[dimIdx] + selShape[dimIdx] > dimLimit) {
8989
// selection ends after current chunk
90-
shape[dimIdx] = (int) (chunkShape[dimIdx] - (selOffset[dimIdx] % chunkShape[dimIdx]));
90+
shape[dimIdx] = (int) (chunkShape[dimIdx] - chunkOffset[dimIdx]);
9191
} else {
9292
// selection ends within current chunk
9393
shape[dimIdx] = (int) (selOffset[dimIdx] + selShape[dimIdx] - dimOffset
@@ -187,5 +187,15 @@ public ChunkProjection(
187187
this.outOffset = outOffset;
188188
this.shape = shape;
189189
}
190+
191+
@Override
192+
public String toString() {
193+
return "ChunkProjection{" +
194+
"chunkCoords=" + Arrays.toString(chunkCoords) +
195+
", chunkOffset=" + Arrays.toString(chunkOffset) +
196+
", outOffset=" + Arrays.toString(outOffset) +
197+
", shape=" + Arrays.toString(shape) +
198+
'}';
199+
}
190200
}
191201
}

src/test/java/dev/zarr/zarrjava/TestUtils.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package dev.zarr.zarrjava;
22

33

4+
import dev.zarr.zarrjava.utils.IndexingUtils;
45
import org.junit.Test;
56
import org.junit.jupiter.api.Assertions;
67

78
import java.util.Arrays;
89

10+
import static dev.zarr.zarrjava.utils.IndexingUtils.computeChunkCoords;
911
import static dev.zarr.zarrjava.utils.Utils.inversePermutation;
1012
import static dev.zarr.zarrjava.utils.Utils.isPermutation;
1113

@@ -27,5 +29,52 @@ public void testInversePermutation() {
2729
Assertions.assertFalse(Arrays.equals(new int[]{2, 0, 1}, inversePermutation(new int[]{2, 0, 1})));
2830
}
2931

32+
@Test
33+
public void testComputeChunkCoords(){
34+
long[] arrayShape = new long[]{100, 100};
35+
int[] chunkShape = new int[]{30, 30};
36+
long[] selOffset = new long[]{50, 20};
37+
int[] selShape = new int[]{20, 1};
38+
long[][] chunkCoords = computeChunkCoords(arrayShape, chunkShape, selOffset, selShape);
39+
long[][] expectedChunkCoords = new long[][]{
40+
{1, 0},
41+
{2, 0},
42+
};
43+
Assertions.assertArrayEquals(expectedChunkCoords, chunkCoords);
44+
45+
arrayShape = new long[]{1, 52};
46+
chunkShape = new int[]{1, 17};
47+
selOffset = new long[]{0, 32};
48+
selShape = new int[]{1, 20};
49+
chunkCoords = computeChunkCoords(arrayShape, chunkShape, selOffset, selShape);
50+
expectedChunkCoords = new long[][]{
51+
{0, 1},
52+
{0, 2},
53+
{0, 3},
54+
};
55+
Assertions.assertArrayEquals(expectedChunkCoords, chunkCoords);
56+
}
57+
58+
@Test
59+
public void testComputeProjection(){
60+
// chunk (0,2) contains indexes 34-50 along axis 1
61+
// thus the overlap with selection 32-52 is 34-50
62+
// which is offset 2 in the selection and offset 0 in the chunk
63+
// and has full chunk length 17
64+
final long[] chunkCoords = new long[]{0, 2};
65+
final long[] arrayShape = new long[]{1, 52};
66+
final int[] chunkShape = new int[]{1, 17};
67+
final long[] selOffset = new long[]{0, 32};
68+
final int[] selShape = new int[]{1, 20};
69+
70+
IndexingUtils.ChunkProjection projection = IndexingUtils.computeProjection(
71+
chunkCoords, arrayShape, chunkShape, selOffset, selShape
72+
);
73+
Assertions.assertArrayEquals(chunkCoords, projection.chunkCoords);
74+
Assertions.assertArrayEquals(new int[]{0,0}, projection.chunkOffset);
75+
Assertions.assertArrayEquals(new int[]{0,2}, projection.outOffset);
76+
Assertions.assertArrayEquals(new int[]{1, 17}, projection.shape);
77+
}
78+
3079
}
3180

src/test/java/dev/zarr/zarrjava/ZarrV3Test.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.BufferedReader;
2828
import java.io.IOException;
29+
import java.nio.ByteBuffer;
2930
import java.nio.file.Files;
3031
import java.nio.file.NoSuchFileException;
3132
import java.nio.file.Path;
@@ -735,4 +736,41 @@ public void testGroupAttributes() throws IOException, ZarrException {
735736
group = Group.open(storeHandle);
736737
Assertions.assertEquals("group_value", group.metadata().attributes().getString("group_attr"));
737738
}
738-
}
739+
740+
@Test
741+
public void testTemp() throws ZarrException, IOException {
742+
String filePath = TESTOUTPUT + "/testTempV3.txt";
743+
int imageWidth = 52;
744+
int imageHeight = 1;
745+
int chunkWidth = 17;
746+
Array input = Array.create(
747+
new FilesystemStore(filePath).resolve("0"),
748+
Array.metadataBuilder()
749+
.withShape(imageHeight, imageWidth)
750+
.withDataType(DataType.UINT8)
751+
.withChunkShape(imageHeight, chunkWidth)
752+
.withFillValue(0)
753+
.build()
754+
);
755+
756+
byte[] buf = new byte[imageWidth];
757+
for (int i = 0; i < buf.length; i++) {
758+
buf[i] = (byte) i;
759+
}
760+
ByteBuffer bytes = ByteBuffer.wrap(buf);
761+
ucar.ma2.Array data = ucar.ma2.Array.factory(ucar.ma2.DataType.BYTE, new int[]{imageHeight, imageWidth}, bytes);
762+
input.write(new long[]{0, 0}, data);
763+
764+
long[] readPosition = new long[]{0, 0};
765+
int[] readSize = new int[]{1, 32};
766+
for (int tile = 0; tile < buf.length; tile += readSize[1]) {
767+
readPosition[1] = tile;
768+
readSize[1] = (int) Math.min(readSize[1], buf.length - readPosition[1]);
769+
ucar.ma2.Array readTile = input.read(readPosition, readSize);
770+
for (int i = 0; i < readSize[1]; i++) {
771+
byte pixel = readTile.getByte(i);
772+
Assertions.assertEquals(buf[tile + i], pixel);
773+
}
774+
}
775+
}
776+
}

0 commit comments

Comments
 (0)