Skip to content

Commit

Permalink
Support smaller array size than chunk size (zarr-developers#9)
Browse files Browse the repository at this point in the history
* inner chunks evenly divide outer chunks

* add testLargerChunkSizeThanArraySize and extend testCheckShardingBounds
  • Loading branch information
brokkoli71 authored Aug 30, 2024
1 parent 122ca90 commit 8342e1c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
10 changes: 2 additions & 8 deletions src/main/java/dev/zarr/zarrjava/v3/ArrayMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@ public ArrayMetadata(
throw new ZarrException("Shape (ndim=" + shape.length + ") and chunk shape (ndim=" +
chunkShape.length + ") need to have the same number of dimensions.");
}
for (int i = 0; i < shape.length; i++) {
if (shape[i] < chunkShape[i]) {
throw new ZarrException("Shape " + Arrays.toString(shape) + " can not contain chunk shape "
+ Arrays.toString(chunkShape));
}
}

Optional<Codec> shardingCodec = getShardingIndexedCodec(codecs);
int[] outerChunkShape = chunkShape;
Expand All @@ -115,8 +109,8 @@ public ArrayMetadata(
if (outerChunkShape.length != innerChunkShape.length)
throw new ZarrException("Sharding dimensions mismatch of outer chunk shape " + Arrays.toString(outerChunkShape) + " and inner chunk shape" + Arrays.toString(innerChunkShape));
for (int i = 0; i < outerChunkShape.length; i++) {
if (outerChunkShape[i] < innerChunkShape[i])
throw new ZarrException("Sharding outer chunk shape " + Arrays.toString(outerChunkShape) + " can not contain inner chunk shape " + Arrays.toString(innerChunkShape));
if (outerChunkShape[i] % innerChunkShape[i] != 0)
throw new ZarrException("Sharding inner chunk shape " + Arrays.toString(innerChunkShape) + " does not evenly divide the outer chunk size " + Arrays.toString(outerChunkShape));
}
outerChunkShape = innerChunkShape;
shardingCodec = getShardingIndexedCodec(shardingConfig.codecs);
Expand Down
70 changes: 44 additions & 26 deletions src/test/java/dev/zarr/zarrjava/ZarrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,38 @@ public void testCheckInvalidCodecConfiguration(Function<CodecBuilder, CodecBuild
assertThrows(ZarrException.class, () -> Array.create(storeHandle, builder.build()));
}

@Test
public void testLargerChunkSizeThanArraySize() throws ZarrException, IOException {
int[] testData = new int[16 * 16 * 16];
Arrays.setAll(testData, p -> p);

StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("larger_chunk_size_than_array");
ArrayMetadata metadata = Array.metadataBuilder()
.withShape(16, 16, 16)
.withDataType(DataType.UINT32)
.withChunkShape(32, 32, 32)
.withFillValue(0)
.build();
Array writeArray = Array.create(storeHandle, metadata);
writeArray.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{16, 16, 16}, testData));

//read in zarr-java
Array readArray = Array.open(storeHandle);
ucar.ma2.Array result = readArray.read();

Assertions.assertArrayEquals(testData, (int[]) result.get1DJavaArray(ucar.ma2.DataType.INT));
}

static Stream<int[]> invalidChunkSizes() {
return Stream.of(
new int[]{1},
new int[]{1, 1, 1},
new int[] {5, 1},
new int[] {1, 5}
new int[]{1, 1, 1}
);
}

@ParameterizedTest
@MethodSource("invalidChunkSizes")
public void testCheckInvalidChunkBounds(int[] chunkSize) throws Exception {
public void testCheckInvalidChunkDimensions(int[] chunkSize) {
long[] shape = new long[] {4, 4};

StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("invalid_chunksize");
Expand All @@ -278,35 +298,33 @@ public void testCheckInvalidChunkBounds(int[] chunkSize) throws Exception {
assertThrows(ZarrException.class, builder::build);
}

static Stream<int[]> invalidShardSizes() {
return Stream.of(
new int[]{4}, //wrong dims
new int[]{4, 4, 4}, //wrong dims
new int[]{1, 1}, //smaller than inner chunk shape
new int[]{5, 5}, //no exact multiple of inner chunk shape
new int[]{2, 1}, //smaller than inner chunk shape in 2nd dimension
new int[]{2, 5} //no exact multiple of inner chunk shape in 2nd dimension
);
}

@ParameterizedTest
@ValueSource(strings = {"large", "small", "nested", "wrong dims", "correct"})
public void testCheckShardingBounds(String scenario) throws Exception {
long[] shape = new long[] {4, 4};
int[] shardSize = new int[] {2, 2};
int[] chunkSize = new int[] {2, 2};

if (scenario.equals("large"))
shardSize = new int[] {8, 8};
if (scenario.equals("small"))
shardSize = new int[] {1, 1};
if (scenario.equals("wrong dims"))
shardSize = new int[] {1};
StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("illegal_shardsize");
@MethodSource("invalidShardSizes")
public void testCheckShardingBounds(int[] shardSize) throws Exception {
long[] shape = new long[] {10, 10};
int[] innerChunkSize = new int[] {2, 2};

ArrayMetadataBuilder builder = Array.metadataBuilder()
.withShape(shape)
.withDataType(DataType.UINT32).withChunkShape(shardSize);

if (scenario.equals("nested")) {
if (false) {
int[] nestedChunkSize = new int[]{4, 4};
builder = builder.withCodecs(c -> c.withSharding(chunkSize, c1 -> c1.withSharding(nestedChunkSize, c2 -> c2.withBytes("LITTLE"))));
} else {
builder = builder.withCodecs(c -> c.withSharding(chunkSize, c1 -> c1.withBytes("LITTLE")));
}
if (scenario.equals("correct")){
builder.build();
}else{
assertThrows(ZarrException.class, builder::build);
builder = builder.withCodecs(c -> c.withSharding(new int[]{2, 2}, c1 -> c1.withSharding(nestedChunkSize, c2 -> c2.withBytes("LITTLE"))));
}
builder = builder.withCodecs(c -> c.withSharding(innerChunkSize, c1 -> c1.withBytes("LITTLE")));
assertThrows(ZarrException.class, builder::build);
}

@ParameterizedTest
Expand Down

0 comments on commit 8342e1c

Please sign in to comment.