Skip to content

Commit

Permalink
fix: number of downsampling levels
Browse files Browse the repository at this point in the history
see #98
  • Loading branch information
bogovicj committed Dec 4, 2024
1 parent 976766a commit 51671ce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ public <T extends RealType<T> & NativeType<T>, M extends N5DatasetMetadata, N ex
Arrays.fill(relativeFactors, 1);

if (s > 0) {
relativeFactors = getRelativeDownsampleFactors(currentMetadata, currentChannelImg.numDimensions(), s, currentAbsoluteDownsampling);
relativeFactors = getRelativeDownsampleFactors(currentMetadata, currentChannelImg, s, currentAbsoluteDownsampling);

// update absolute downsampling factors
for (int i = 0; i < nd; i++)
Expand Down Expand Up @@ -610,7 +610,7 @@ public <T extends RealType<T> & NativeType<T>, M extends N5DatasetMetadata, N ex
anyScalesWritten = true;

// chunkSize variable is updated by the write method
if (lastScale(chunkSize, currentChannelImg))
if (lastScale(chunkSize, currentChannelImg, currentMetadata))
break;
}

Expand Down Expand Up @@ -720,13 +720,15 @@ protected <N extends SpatialMetadataGroup<?>> N finalizeMultiscaleMetadata(final
return multiscaleMetadata;
}

protected boolean lastScale(final int[] chunkSize, final Interval imageDimensions) {
protected <M extends N5Metadata> boolean lastScale(final int[] chunkSize, final Interval imageDimensions, final M metadata) {

final Axis[] axes = getAxes(metadata, imageDimensions.numDimensions());

for (int i = 0; i < imageDimensions.numDimensions(); i++) {
if (imageDimensions.dimension(i) <= chunkSize[i])
return true;
if (axes[i].getType().equals(Axis.SPACE) && imageDimensions.dimension(i) > chunkSize[i])
return false;
}
return false;
return true;
}

protected <M extends N5DatasetMetadata> void fillResolution(final M baseMetadata, final double[] resolution) {
Expand Down Expand Up @@ -861,17 +863,18 @@ protected <M extends N5Metadata> long[] getDownsampleFactors(final M metadata, f
return factors;
}

protected <M extends N5Metadata> long[] getRelativeDownsampleFactors(final M metadata, final int nd, final int scale,
protected <M extends N5Metadata> long[] getRelativeDownsampleFactors(final M metadata, final Interval img, final int scale,
final long[] downsampleFactors) {

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

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

// only downsample spatial dimensions
if (axes[i].getType().equals(Axis.SPACE))
if (axes[i].getType().equals(Axis.SPACE) && img.dimension(i) > 1)
factors[i] = 2;
else
factors[i] = 1;
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/janelia/saalfeldlab/n5/TestExportImports.java
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,26 @@ public void testReadWriteParsePyramid()
}
}

@Test
public void testNumDownsamplingLevels() {

final int bitDepth = 8;
final String dset = "scaleLevelsTest";

// the size of mitosis.tif sample image
final ImagePlus imp = NewImage.createImage("test", 171, 196, 2 * 5 * 51, bitDepth, NewImage.FILL_BLACK);
imp.setDimensions(2, 5, 51);

final N5ScalePyramidExporter exp = new N5ScalePyramidExporter();
exp.setOptions(imp, baseDir.getAbsolutePath(), dset, "16", true, N5ScalePyramidExporter.DOWN_AVERAGE,
N5Importer.MetadataOmeZarrKey, N5ScalePyramidExporter.RAW_COMPRESSION);
exp.run();

try (final N5Reader n5 = new N5FSReader(baseDir.getAbsolutePath())) {
assertEquals("5 scale levels", 5, n5.list(dset).length);
}
}

public void pyramidReadWriteParseTest(
final ImagePlus imp,
final String outputPath,
Expand Down

0 comments on commit 51671ce

Please sign in to comment.