Skip to content

Instantly share code, notes, and snippets.

@bogovicj
Created April 16, 2024 15:57
Show Gist options
  • Save bogovicj/9c1ab4c32e21d26fb2b5c5b533966adb to your computer and use it in GitHub Desktop.
Save bogovicj/9c1ab4c32e21d26fb2b5c5b533966adb to your computer and use it in GitHub Desktop.
#@ File(styles="both") root
#@ String(value="/") datasetPath
/*
* If you used Fiji to export a multi-scale an image with COSEM 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());
N5TreeNode treeRoot = N5DatasetDiscoverer.discover(n5,
Collections.singletonList(new N5CosemMetadataParser()),
Collections.emptyList());
node = treeRoot.getDescendant(normDatasetPath);
if (node.isPresent()) {
CosemTransform baseTform = null;
for( N5TreeNode c : node.get().childrenList())
{
N5Metadata meta = c.getMetadata();
if( meta.getPath().endsWith("s0"))
{
baseTform = ((N5CosemMetadata)meta).getCosemTransform();
break;
}
}
for( N5TreeNode c : node.get().childrenList())
{
N5Metadata meta = c.getMetadata();
if( meta instanceof N5CosemMetadata )
{
N5CosemMetadata cosemMeta = (N5CosemMetadata)meta;
if( !meta.getPath().endsWith("s0"))
{
correct( baseTform, cosemMeta.getCosemTransform());
n5.setAttribute(N5URI.normalizeGroupPath(normDatasetPath + "/" + meta.getPath() ), "transform", cosemMeta.getCosemTransform());
}
}
}
} else {
System.err.println("could not find : " + datasetPath);
return;
}
def correct(CosemTransform baseTransform, CosemTransform tform ) {
double EPS = 1e-3;
double[] res = baseTransform.scale;
double[] s = tform.scale;
double[] t = tform.translate;
if( t[0] < 0.1 * EPS * s[0] )
return;
int nd = s.length;
for( int i = 0; i < nd; i++ )
{
double factor = s[i] / res[i];
t[i] = res[i] * (0.5 * factor - 0.5);
}
}
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.N5CosemMetadata;
import org.janelia.saalfeldlab.n5.universe.metadata.N5CosemMetadata.CosemTransform;
import org.janelia.saalfeldlab.n5.universe.metadata.N5CosemMetadataParser;
import org.janelia.saalfeldlab.n5.universe.metadata.N5Metadata;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment