Skip to content

Commit

Permalink
fix: NgffSingleScaleMetadataParser more robust
Browse files Browse the repository at this point in the history
* handle situations with unusually scale/translation ordering
* or multiple scales and/or translations

see #17
  • Loading branch information
bogovicj committed May 2, 2024
1 parent 596d4e9 commit 1706d96
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.janelia.saalfeldlab.n5.universe.metadata;

import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;

Expand All @@ -25,6 +23,7 @@
import net.imglib2.realtransform.Translation;
import net.imglib2.realtransform.Translation2D;
import net.imglib2.realtransform.Translation3D;
import net.imglib2.util.Pair;

public class MetadataUtils {

Expand Down Expand Up @@ -112,6 +111,32 @@ public static CoordinateTransformation<?>[] buildScaleTranslationTransformList(f
return coordinateTransformations;
}

public static ScaleAndTranslation scaleTranslationFromCoordinateTransformations(final CoordinateTransformation<?>[] cts) {

if (cts == null || cts.length == 0)
return null;

ScaleAndTranslation out = coordinateTransformToScaleAndTranslation(cts[0]);
for (int i = 1; i < cts.length; i++) {
out.preConcatenate(coordinateTransformToScaleAndTranslation(cts[i]));
}
return out;
}

public static ScaleAndTranslation coordinateTransformToScaleAndTranslation(CoordinateTransformation<?> ct) {

if (ct.getType().equals(ScaleCoordinateTransformation.TYPE)) {
final double[] s = ((ScaleCoordinateTransformation)ct).getScale();
return new ScaleAndTranslation(s, new double[s.length]);
} else if (ct.getType().equals(TranslationCoordinateTransformation.TYPE)) {
final double[] t = ((TranslationCoordinateTransformation)ct).getTranslation();
final double[] s = new double[t.length];
Arrays.fill(s, 1.0);
return new ScaleAndTranslation(s, t);
}
return null;
}

/**
* Returns a new {@link N5SingleScaleMetadata} equal to the baseMetadata, but with
* {@link DatasetAttributes} coming from datasetMetadata.ew
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public NgffSingleScaleAxesMetadata(final String path,

this.datasetAttributes = datasetAttributes;

// TODO generalize for translation first
coordinateTransformations = MetadataUtils.buildScaleTranslationTransformList(scale, translation);

this.transform = MetadataUtils.scaleTranslationTransforms(scale, translation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.janelia.saalfeldlab.n5.N5Reader;
import org.janelia.saalfeldlab.n5.N5Writer;
import org.janelia.saalfeldlab.n5.universe.N5TreeNode;
import org.janelia.saalfeldlab.n5.universe.metadata.MetadataUtils;
import org.janelia.saalfeldlab.n5.universe.metadata.N5MetadataParser;
import org.janelia.saalfeldlab.n5.universe.metadata.N5MetadataWriter;
import org.janelia.saalfeldlab.n5.universe.metadata.axes.Axis;
Expand All @@ -19,6 +20,8 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

import net.imglib2.realtransform.ScaleAndTranslation;

public class NgffSingleScaleMetadataParser implements N5MetadataParser<NgffSingleScaleAxesMetadata>, N5MetadataWriter<NgffSingleScaleAxesMetadata> {

private final Gson gson;
Expand All @@ -43,14 +46,9 @@ public Optional<NgffSingleScaleAxesMetadata> parseMetadata(final N5Reader n5, fi
if (cts == null || cts.length == 0)
return Optional.of(new NgffSingleScaleAxesMetadata(node.getPath(), null, null, null));

double[] scale = null;
double[] translation = null;
for (int i = 0; i < cts.length; i++) {
if (cts[i] instanceof ScaleCoordinateTransformation)
scale = ((ScaleCoordinateTransformation)cts[i]).getScale();
else if (cts[i] instanceof TranslationCoordinateTransformation)
translation = ((TranslationCoordinateTransformation)cts[i]).getTranslation();
}
final ScaleAndTranslation scaleAndTranslation = MetadataUtils.scaleTranslationFromCoordinateTransformations(cts);
final double[] scale = scaleAndTranslation.getScaleCopy();
final double[] translation = scaleAndTranslation.getTranslationCopy();

final DatasetAttributes dsetAttrs = n5.getDatasetAttributes(node.getPath());
if( OmeNgffMetadataParser.cOrder(dsetAttrs))
Expand Down

0 comments on commit 1706d96

Please sign in to comment.