Skip to content

Commit fbb2b67

Browse files
committed
incorporate feedback for java version and merge tests
1 parent 8d4eae5 commit fbb2b67

File tree

3 files changed

+34
-79
lines changed

3 files changed

+34
-79
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<version>0.0.1-SNAPSHOT</version>
1010

1111
<properties>
12-
<maven.compiler.source>11</maven.compiler.source>
13-
<maven.compiler.target>11</maven.compiler.target>
12+
<maven.compiler.source>8</maven.compiler.source>
13+
<maven.compiler.target>8</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<jackson.version>2.14.2</jackson.version>
1616
<aws.version>1.12.477</aws.version>

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

+24-69
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,17 @@ public void testZstdLibrary(int clevel, boolean checksumFlag) throws IOException
129129

130130
@ParameterizedTest
131131
@ValueSource(strings = {"blosc", "gzip", "zstd", "bytes", "transpose", "sharding_start", "sharding_end", "crc32c"})
132-
public void testWriteToZarrita(String codec) throws IOException, ZarrException, InterruptedException {
132+
public void testWriteRead(String codec) throws IOException, ZarrException, InterruptedException {
133+
int[] testData = new int[16 * 16 * 16];
134+
Arrays.setAll(testData, p -> p);
135+
133136
StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("write_to_zarrita", codec);
134137
ArrayMetadataBuilder builder = Array.metadataBuilder()
135-
.withShape(16, 16)
138+
.withShape(16, 16, 16)
136139
.withDataType(DataType.UINT32)
137-
.withChunkShape(8, 8)
138-
.withFillValue(0);
140+
.withChunkShape(2, 4, 8)
141+
.withFillValue(0)
142+
.withAttributes(Map.of("test_key", "test_value"));
139143

140144
switch (codec) {
141145
case "blosc":
@@ -151,13 +155,13 @@ public void testWriteToZarrita(String codec) throws IOException, ZarrException,
151155
builder = builder.withCodecs(c -> c.withBytes("LITTLE"));
152156
break;
153157
case "transpose":
154-
builder = builder.withCodecs(c -> c.withTranspose(new int[]{1, 0}));
158+
builder = builder.withCodecs(c -> c.withTranspose(new int[]{1, 0, 2}));
155159
break;
156160
case "sharding_start":
157-
builder = builder.withCodecs(c -> c.withSharding(new int[]{4, 4}, c1 -> c1.withBytes("LITTLE"), "start"));
161+
builder = builder.withCodecs(c -> c.withSharding(new int[]{2, 2, 4}, c1 -> c1.withBytes("LITTLE"), "start"));
158162
break;
159163
case "sharding_end":
160-
builder = builder.withCodecs(c -> c.withSharding(new int[]{4, 4}, c1 -> c1.withBytes("LITTLE"), "end"));
164+
builder = builder.withCodecs(c -> c.withSharding(new int[]{2, 2, 4}, c1 -> c1.withBytes("LITTLE"), "end"));
161165
break;
162166
case "crc32c":
163167
builder = builder.withCodecs(CodecBuilder::withCrc32c);
@@ -166,12 +170,21 @@ public void testWriteToZarrita(String codec) throws IOException, ZarrException,
166170
throw new IllegalArgumentException("Invalid Codec: " + codec);
167171
}
168172

169-
Array array = Array.create(storeHandle, builder.build());
173+
Array writeArray = Array.create(storeHandle, builder.build());
174+
writeArray.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{16, 16, 16}, testData));
175+
176+
//read in zarr-java
177+
Array readArray = Array.open(storeHandle);
178+
ucar.ma2.Array result = readArray.read();
170179

171-
int[] data = new int[16 * 16];
172-
Arrays.setAll(data, p -> p);
173-
array.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{16, 16}, data));
180+
Assertions.assertArrayEquals(new int[]{16, 16, 16}, result.getShape());
181+
Assertions.assertEquals(DataType.UINT32, readArray.metadata.dataType);
182+
Assertions.assertArrayEquals(new int[]{2, 4, 8}, readArray.metadata.chunkShape());
183+
Assertions.assertEquals("test_value", readArray.metadata.attributes.get("test_key"));
174184

185+
Assertions.assertArrayEquals(testData, (int[]) result.get1DJavaArray(ucar.ma2.DataType.INT));
186+
187+
//read in zarrita
175188
String command = pythonPath();
176189

177190
ProcessBuilder pb = new ProcessBuilder(command, PYTHON_TEST_PATH.resolve("zarrita_read.py").toString(), codec, TESTOUTPUT.toString());
@@ -192,64 +205,6 @@ public void testWriteToZarrita(String codec) throws IOException, ZarrException,
192205
assert exitCode == 0;
193206
}
194207

195-
196-
@ParameterizedTest
197-
@ValueSource(strings = {"blosc", "gzip", "zstd", "bytes", "transpose", "sharding_start", "sharding_end", "crc32c"})
198-
public void testCodecsWriteRead(String codec) throws IOException, ZarrException {
199-
int[] testData = new int[16 * 16 * 16];
200-
Arrays.setAll(testData, p -> p);
201-
202-
StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("testWriteAndRead3d", codec);
203-
ArrayMetadataBuilder builder = Array.metadataBuilder()
204-
.withShape(16, 16, 16)
205-
.withDataType(DataType.UINT32)
206-
.withChunkShape(2, 4, 8)
207-
.withFillValue(0)
208-
.withAttributes(Map.of("test_key", "test_value"));
209-
210-
switch (codec) {
211-
case "blosc":
212-
builder = builder.withCodecs(CodecBuilder::withBlosc);
213-
break;
214-
case "gzip":
215-
builder = builder.withCodecs(CodecBuilder::withGzip);
216-
break;
217-
case "zstd":
218-
builder = builder.withCodecs(c -> c.withZstd(0));
219-
break;
220-
case "bytes":
221-
builder = builder.withCodecs(c -> c.withBytes("LITTLE"));
222-
break;
223-
case "transpose":
224-
builder = builder.withCodecs(c -> c.withTranspose(new int[]{1, 0, 2}));
225-
break;
226-
case "sharding_start":
227-
builder = builder.withCodecs(c -> c.withSharding(new int[]{2, 2, 4}, c1 -> c1.withBytes("LITTLE"), "end"));
228-
break;
229-
case "sharding_end":
230-
builder = builder.withCodecs(c -> c.withSharding(new int[]{2, 2, 4}, c1 -> c1.withBytes("LITTLE"), "start"));
231-
break;
232-
case "crc32c":
233-
builder = builder.withCodecs(CodecBuilder::withCrc32c);
234-
break;
235-
default:
236-
throw new IllegalArgumentException("Invalid Codec: " + codec);
237-
}
238-
239-
Array writeArray = Array.create(storeHandle, builder.build());
240-
writeArray.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{16, 16, 16}, testData));
241-
242-
Array readArray = Array.open(storeHandle);
243-
ucar.ma2.Array result = readArray.read();
244-
245-
Assertions.assertArrayEquals(new int[]{16, 16, 16}, result.getShape());
246-
Assertions.assertEquals(DataType.UINT32, readArray.metadata.dataType);
247-
Assertions.assertArrayEquals(new int[]{2, 4, 8}, readArray.metadata.chunkShape());
248-
Assertions.assertEquals("test_value", readArray.metadata.attributes.get("test_key"));
249-
250-
Assertions.assertArrayEquals(testData, (int[]) result.get1DJavaArray(ucar.ma2.DataType.INT));
251-
}
252-
253208
@ParameterizedTest
254209
@CsvSource({"0,true", "0,false", "5, true", "5, false"})
255210
public void testZstdCodecReadWrite(int clevel, boolean checksum) throws ZarrException, IOException {

src/test/python-scripts/zarrita_read.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,32 @@
1414
elif codec_string == "bytes":
1515
codec = [zarrita.codecs.bytes_codec()]
1616
elif codec_string == "transpose":
17-
codec = [zarrita.codecs.transpose_codec((1, 0)), zarrita.codecs.bytes_codec()]
17+
codec = [zarrita.codecs.transpose_codec((1, 0, 2)), zarrita.codecs.bytes_codec()]
1818
elif codec_string == "sharding_start":
19-
codec= zarrita.codecs.sharding_codec(chunk_shape=(4, 4), codecs=[zarrita.codecs.bytes_codec("little")], index_location= zarrita.metadata.ShardingCodecIndexLocation.start),
19+
codec= zarrita.codecs.sharding_codec(chunk_shape=(2, 2, 4), codecs=[zarrita.codecs.bytes_codec("little")], index_location= zarrita.metadata.ShardingCodecIndexLocation.start),
2020
elif codec_string == "sharding_end":
21-
codec= zarrita.codecs.sharding_codec(chunk_shape=(4, 4), codecs=[zarrita.codecs.bytes_codec("little")], index_location= zarrita.metadata.ShardingCodecIndexLocation.end),
21+
codec= zarrita.codecs.sharding_codec(chunk_shape=(2, 2, 4), codecs=[zarrita.codecs.bytes_codec("little")], index_location= zarrita.metadata.ShardingCodecIndexLocation.end),
2222
elif codec_string == "crc32c":
2323
codec = [zarrita.codecs.bytes_codec(), zarrita.codecs.crc32c_codec()]
2424
else:
2525
raise ValueError(f"Invalid {codec=}")
2626

2727

2828
store = zarrita.LocalStore(sys.argv[2])
29-
expected_data = np.arange(16*16, dtype='int32').reshape(16, 16)
29+
expected_data = np.arange(16*16*16, dtype='int32').reshape(16, 16, 16)
3030

3131
a = zarrita.Array.open(store / 'write_to_zarrita' / codec_string)
3232
read_data = a[:, :]
3333
assert np.array_equal(read_data, expected_data), f"got:\n {read_data} \nbut expected:\n {expected_data}"
3434

35-
# might need to check individual properties
3635
b = zarrita.Array.create(
3736
store / 'read_from_zarrita_expected' / codec_string,
38-
shape=(16, 16),
39-
chunk_shape=(8, 8),
37+
shape=(16, 16, 16),
38+
chunk_shape=(2, 4, 8),
4039
dtype="uint32",
4140
fill_value=0,
41+
attributes={'test_key': 'test_value'},
4242
codecs=codec
4343
)
4444

45-
assert a.metadata == b.metadata, f"not equal: \n{a.metadata.codecs=}\n{b.metadata.codecs=}"
45+
assert a.metadata == b.metadata, f"not equal: \n{a.metadata=}\n{b.metadata=}"

0 commit comments

Comments
 (0)