Skip to content

Commit ea45ed5

Browse files
committed
fix: number of downsampling levels
see #98
1 parent ddfff93 commit ea45ed5

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/main/java/org/janelia/saalfeldlab/n5/ij/N5ScalePyramidExporter.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ public <T extends RealType<T> & NativeType<T>, M extends N5DatasetMetadata, N ex
576576
Arrays.fill(relativeFactors, 1);
577577

578578
if (s > 0) {
579-
relativeFactors = getRelativeDownsampleFactors(currentMetadata, currentChannelImg.numDimensions(), s, currentAbsoluteDownsampling);
579+
relativeFactors = getRelativeDownsampleFactors(currentMetadata, currentChannelImg, s, currentAbsoluteDownsampling);
580580

581581
// update absolute downsampling factors
582582
for (int i = 0; i < nd; i++)
@@ -615,7 +615,7 @@ public <T extends RealType<T> & NativeType<T>, M extends N5DatasetMetadata, N ex
615615
anyScalesWritten = true;
616616

617617
// chunkSize variable is updated by the write method
618-
if (lastScale(chunkSize, currentChannelImg))
618+
if (lastScale(chunkSize, currentChannelImg, currentMetadata))
619619
break;
620620
}
621621

@@ -725,13 +725,15 @@ protected <N extends SpatialMetadataGroup<?>> N finalizeMultiscaleMetadata(final
725725
return multiscaleMetadata;
726726
}
727727

728-
protected boolean lastScale(final int[] chunkSize, final Interval imageDimensions) {
728+
protected <M extends N5Metadata> boolean lastScale(final int[] chunkSize, final Interval imageDimensions, final M metadata) {
729+
730+
final Axis[] axes = getAxes(metadata, imageDimensions.numDimensions());
729731

730732
for (int i = 0; i < imageDimensions.numDimensions(); i++) {
731-
if (imageDimensions.dimension(i) <= chunkSize[i])
732-
return true;
733+
if (axes[i].getType().equals(Axis.SPACE) && imageDimensions.dimension(i) > chunkSize[i])
734+
return false;
733735
}
734-
return false;
736+
return true;
735737
}
736738

737739
protected <M extends N5DatasetMetadata> void fillResolution(final M baseMetadata, final double[] resolution) {
@@ -866,17 +868,18 @@ protected <M extends N5Metadata> long[] getDownsampleFactors(final M metadata, f
866868
return factors;
867869
}
868870

869-
protected <M extends N5Metadata> long[] getRelativeDownsampleFactors(final M metadata, final int nd, final int scale,
871+
protected <M extends N5Metadata> long[] getRelativeDownsampleFactors(final M metadata, final Interval img, final int scale,
870872
final long[] downsampleFactors) {
871873

874+
int nd = img.numDimensions();
872875
final Axis[] axes = getAxes(metadata, nd);
873876

874877
// under what condisions is nd != axes.length
875878
final long[] factors = new long[axes.length];
876879
for (int i = 0; i < nd; i++) {
877880

878881
// only downsample spatial dimensions
879-
if (axes[i].getType().equals(Axis.SPACE))
882+
if (axes[i].getType().equals(Axis.SPACE) && img.dimension(i) > 1)
880883
factors[i] = 2;
881884
else
882885
factors[i] = 1;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,26 @@ public void testReadWriteParsePyramid()
659659
}
660660
}
661661

662+
@Test
663+
public void testNumDownsamplingLevels() {
664+
665+
final int bitDepth = 8;
666+
final String dset = "scaleLevelsTest";
667+
668+
// the size of mitosis.tif sample image
669+
final ImagePlus imp = NewImage.createImage("test", 171, 196, 2 * 5 * 51, bitDepth, NewImage.FILL_BLACK);
670+
imp.setDimensions(2, 5, 51);
671+
672+
final N5ScalePyramidExporter exp = new N5ScalePyramidExporter();
673+
exp.setOptions(imp, baseDir.getAbsolutePath(), dset, "16", true, N5ScalePyramidExporter.DOWN_AVERAGE,
674+
N5Importer.MetadataOmeZarrKey, N5ScalePyramidExporter.RAW_COMPRESSION);
675+
exp.run();
676+
677+
try (final N5Reader n5 = new N5FSReader(baseDir.getAbsolutePath())) {
678+
assertEquals("5 scale levels", 5, n5.list(dset).length);
679+
}
680+
}
681+
662682
public void pyramidReadWriteParseTest(
663683
final ImagePlus imp,
664684
final String outputPath,

0 commit comments

Comments
 (0)