Skip to content

Commit 506e58f

Browse files
committed
feat: add getChunkSize
* getBlockSize should correspond to readBlock / writeBlock * useful to have an analogue for readChunk / writeChunk
1 parent 7b06bdb commit 506e58f

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

src/main/java/org/janelia/saalfeldlab/n5/DatasetAttributes.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ public class DatasetAttributes implements Serializable {
8989

9090
private final long[] dimensions;
9191

92+
// number of samples per chunk per dimension
93+
private final int[] chunkSize;
94+
9295
// number of samples per block per dimension
96+
// identical to chunkSize for non-sharded datasets
9397
private final int[] blockSize;
9498

95-
// TODO add a getter?
96-
// the shard size
97-
private final int[] outerBlockSize;
98-
9999
private final DataType dataType;
100100

101101
private final JsonElement defaultValue;
@@ -108,7 +108,7 @@ public class DatasetAttributes implements Serializable {
108108

109109
public DatasetAttributes(
110110
final long[] dimensions,
111-
final int[] outerBlockSize,
111+
final int[] blockSize,
112112
final DataType dataType,
113113
final JsonElement defaultValue,
114114
final BlockCodecInfo blockCodecInfo,
@@ -117,7 +117,7 @@ public DatasetAttributes(
117117

118118
this.dimensions = dimensions;
119119
this.dataType = dataType;
120-
this.outerBlockSize = outerBlockSize;
120+
this.blockSize = blockSize;
121121
this.defaultValue = defaultValue == null ? JsonNull.INSTANCE : defaultValue;
122122

123123
this.blockCodecInfo = blockCodecInfo == null ? defaultBlockCodecInfo() : blockCodecInfo;
@@ -131,7 +131,7 @@ public DatasetAttributes(
131131
.toArray(DataCodecInfo[]::new);
132132

133133
access = createDatasetAccess();
134-
blockSize = access.getGrid().getBlockSize(0);
134+
chunkSize = access.getGrid().getBlockSize(0);
135135
}
136136

137137
public DatasetAttributes(
@@ -200,7 +200,7 @@ protected DatasetAccess<?> createDatasetAccess() {
200200
// NestedGrid validates block sizes, so instantiate it before creating the blockCodecs
201201
// blockCodecInfo.create below could fail unexpecedly with invalid
202202
// blockSizes so validate first
203-
blockSizes[m - 1] = outerBlockSize;
203+
blockSizes[m - 1] = blockSize;
204204
BlockCodecInfo tmpInfo = blockCodecInfo;
205205
for (int l = m - 1; l > 0; --l) {
206206
final ShardCodecInfo info = (ShardCodecInfo)tmpInfo;
@@ -274,6 +274,11 @@ public int getNumDimensions() {
274274
return dimensions.length;
275275
}
276276

277+
public int[] getChunkSize() {
278+
279+
return chunkSize;
280+
}
281+
277282
public int[] getBlockSize() {
278283

279284
return blockSize;
@@ -334,7 +339,6 @@ public NestedGrid getNestedBlockGrid() {
334339
return getDatasetAccess().getGrid();
335340
}
336341

337-
338342
public BlockCodecInfo getBlockCodecInfo() {
339343

340344
return blockCodecInfo;
@@ -359,7 +363,7 @@ public HashMap<String, Object> asMap() {
359363

360364
final HashMap<String, Object> map = new HashMap<>();
361365
map.put(DIMENSIONS_KEY, dimensions);
362-
map.put(BLOCK_SIZE_KEY, blockSize);
366+
map.put(BLOCK_SIZE_KEY, chunkSize);
363367
map.put(DATA_TYPE_KEY, dataType);
364368
map.put(COMPRESSION_KEY, getCompression());
365369
return map;
@@ -425,7 +429,7 @@ public static class DatasetAttributesAdapter implements JsonSerializer<DatasetAt
425429

426430
final JsonObject obj = new JsonObject();
427431
obj.add(DIMENSIONS_KEY, context.serialize(src.dimensions));
428-
obj.add(BLOCK_SIZE_KEY, context.serialize(src.blockSize));
432+
obj.add(BLOCK_SIZE_KEY, context.serialize(src.chunkSize));
429433
obj.add(DATA_TYPE_KEY, context.serialize(src.dataType));
430434

431435
final DataCodecInfo[] codecs = src.dataCodecInfos;

src/test/java/org/janelia/saalfeldlab/n5/DatasetAttributesTest.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,41 +54,45 @@ public void testValidateBlockShardSizesValid() {
5454
// Test case 1: shard size equals block size
5555
long[] dimensions = new long[]{100, 200, 300};
5656
int[] shardSize = new int[]{64, 64, 64};
57-
int[] blockSize = new int[]{64, 64, 64};
57+
int[] chunkSize = new int[]{64, 64, 64};
5858
DataType dataType = DataType.UINT8;
5959

6060
// This should not throw any exception
61-
DatasetAttributes attrs = shardDatasetAttributes(dimensions, shardSize, blockSize, dataType);
62-
assertEquals(blockSize, attrs.getBlockSize());
61+
DatasetAttributes attrs = shardDatasetAttributes(dimensions, shardSize, chunkSize, dataType);
62+
assertEquals(chunkSize, attrs.getChunkSize());
63+
assertEquals(shardSize, attrs.getBlockSize());
6364
NestedGrid grid = attrs.getNestedBlockGrid();
64-
assertEquals(blockSize, grid.getBlockSize(0));
65+
assertEquals(chunkSize, grid.getBlockSize(0));
6566
assertEquals(shardSize, grid.getBlockSize(1));
6667

6768
// Test case 2: shard size is a multiple of block size
6869
shardSize = new int[]{128};
69-
blockSize = new int[]{64};
70-
attrs = shardDatasetAttributes(new long[]{128}, shardSize, blockSize, dataType);
71-
assertEquals(blockSize, attrs.getBlockSize());
70+
chunkSize = new int[]{64};
71+
attrs = shardDatasetAttributes(new long[]{128}, shardSize, chunkSize, dataType);
72+
assertEquals(chunkSize, attrs.getChunkSize());
73+
assertEquals(shardSize, attrs.getBlockSize());
7274
grid = attrs.getNestedBlockGrid();
73-
assertEquals(blockSize, grid.getBlockSize(0));
75+
assertEquals(chunkSize, grid.getBlockSize(0));
7476
assertEquals(shardSize, grid.getBlockSize(1));
7577

7678
// Test case 3: different multiples per dimension
7779
shardSize = new int[]{128, 256, 32, 2};
78-
blockSize = new int[]{32, 64, 32, 1};
79-
attrs = shardDatasetAttributes(new long[]{128, 128, 128, 128}, shardSize, blockSize, dataType );
80-
assertEquals(blockSize, attrs.getBlockSize());
80+
chunkSize = new int[]{32, 64, 32, 1};
81+
attrs = shardDatasetAttributes(new long[]{128, 128, 128, 128}, shardSize, chunkSize, dataType );
82+
assertEquals(chunkSize, attrs.getChunkSize());
83+
assertEquals(shardSize, attrs.getBlockSize());
8184
grid = attrs.getNestedBlockGrid();
82-
assertEquals(blockSize, grid.getBlockSize(0));
85+
assertEquals(chunkSize, grid.getBlockSize(0));
8386
assertEquals(shardSize, grid.getBlockSize(1));
8487

8588
// Test case 4: large multiples
8689
shardSize = new int[]{1024, 2048, 512};
87-
blockSize = new int[]{32, 64, 16};
88-
attrs = shardDatasetAttributes(dimensions, shardSize, blockSize, dataType);
89-
assertEquals(blockSize, attrs.getBlockSize());
90+
chunkSize = new int[]{32, 64, 16};
91+
attrs = shardDatasetAttributes(dimensions, shardSize, chunkSize, dataType);
92+
assertEquals(chunkSize, attrs.getChunkSize());
93+
assertEquals(shardSize, attrs.getBlockSize());
9094
grid = attrs.getNestedBlockGrid();
91-
assertEquals(blockSize, grid.getBlockSize(0));
95+
assertEquals(chunkSize, grid.getBlockSize(0));
9296
assertEquals(shardSize, grid.getBlockSize(1));
9397
}
9498

0 commit comments

Comments
 (0)