Created
April 16, 2024 19:49
-
-
Save bogovicj/ff035113c88bc6f138603bc7c5d7920c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@ File(styles="both") root | |
#@ String(value="/") datasetPath | |
/* | |
* If you used Fiji to export a multi-scale image with N5Viewer metadata using average downsampling | |
* between approximately March 13 and April 15 2024, the translation metadata may be incorrect. | |
* | |
* Running this script will correct the metadata. | |
* | |
* John Bogovic | |
*/ | |
String normDatasetPath = N5URI.normalizeGroupPath(datasetPath); | |
N5Writer n5 = new N5Factory() | |
.openWriter(root.getAbsolutePath()); | |
N5SingleScaleMetadataParser metadataParser = new N5SingleScaleMetadataParser(); | |
N5TreeNode treeRoot = N5DatasetDiscoverer.discover(n5, | |
Collections.singletonList(metadataParser), | |
Collections.emptyList()); | |
Optional node = treeRoot.getDescendant(normDatasetPath); | |
if (node.isPresent()) { | |
// for each channel | |
for (N5TreeNode channelNode : node.get().childrenList()) | |
{ | |
// get the highest resolution dataset | |
N5SingleScaleMetadata baseTform = null; | |
for (N5TreeNode c : channelNode.childrenList()) | |
{ | |
if (c.getMetadata().getPath().endsWith("s0")) | |
{ | |
baseTform = ((N5SingleScaleMetadata)c.getMetadata()); | |
break; | |
} | |
} | |
for (N5TreeNode c : channelNode.childrenList()) | |
{ | |
N5Metadata meta = c.getMetadata(); | |
if (meta instanceof N5SingleScaleMetadata && !meta.getPath().endsWith("s0")) | |
{ | |
N5SingleScaleMetadata newMetadata = correct(baseTform, (N5SingleScaleMetadata)meta); | |
try { | |
metadataParser.writeMetadata( | |
newMetadata, | |
n5, | |
N5URI.normalizeGroupPath(normDatasetPath + "/" + meta.getPath())); | |
} catch (Exception e) { } | |
} | |
} | |
} | |
} else { | |
System.err.println("could not find : " + datasetPath); | |
return; | |
} | |
def N5SingleScaleMetadata correct(N5SingleScaleMetadata baseTransform, N5SingleScaleMetadata tform) { | |
double[] res = baseTransform.getPixelResolution(); | |
double[] s = tform.getPixelResolution(); | |
int nd = s.length; | |
for( int i = 0; i < nd; i++ ) | |
{ | |
tform.getDownsamplingFactors()[i] = s[i] / res[i]; // set downsamplingFactors | |
s[i] = res[i]; // set resolution to base resolution | |
} | |
Scale editTform = new Scale(tform.getDownsamplingFactors()); | |
return tform.modifySpatialTransform(tform.getPath(), editTform.inverse()); | |
} | |
import java.io.File; | |
import java.util.Collections; | |
import java.util.Optional; | |
import org.janelia.saalfeldlab.n5.N5URI; | |
import org.janelia.saalfeldlab.n5.N5Writer; | |
import org.janelia.saalfeldlab.n5.universe.N5DatasetDiscoverer; | |
import org.janelia.saalfeldlab.n5.universe.N5Factory; | |
import org.janelia.saalfeldlab.n5.universe.N5TreeNode; | |
import org.janelia.saalfeldlab.n5.universe.metadata.N5Metadata; | |
import org.janelia.saalfeldlab.n5.universe.metadata.N5SingleScaleMetadata; | |
import org.janelia.saalfeldlab.n5.universe.metadata.N5SingleScaleMetadataParser; | |
import net.imglib2.realtransform.Scale; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment