Skip to content

Commit

Permalink
test: BdvMetadataIoTests improvements
Browse files Browse the repository at this point in the history
* use temp folder
* use try-with n5writer
  • Loading branch information
bogovicj committed Oct 4, 2024
1 parent 71b7a89 commit 677aa02
Showing 1 changed file with 60 additions and 58 deletions.
118 changes: 60 additions & 58 deletions src/test/java/org/janelia/saalfeldlab/n5/bdv/BdvMetadataIoTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -15,7 +16,6 @@
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import org.janelia.saalfeldlab.n5.N5Reader;
import org.janelia.saalfeldlab.n5.N5Writer;
import org.janelia.saalfeldlab.n5.ij.N5Importer;
import org.janelia.saalfeldlab.n5.ij.N5ScalePyramidExporter;
Expand Down Expand Up @@ -54,8 +54,12 @@ public class BdvMetadataIoTests {
@Before
public void before() {

final URL configUrl = BdvMetadataIoTests.class.getResource("/plugins.config");
baseDir = new File(configUrl.getFile()).getParentFile();
try {
baseDir = Files.createTempDirectory("n5-ij-tests-").toFile();
baseDir.deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
}
}

public <T extends NumericType<T> & NativeType<T>, V extends Volatile<T> & NumericType<V>> void readWriteParseTest(
Expand All @@ -77,64 +81,62 @@ public <T extends NumericType<T> & NativeType<T>, V extends Volatile<T> & Numeri

final String readerDataset = dataset;

final N5Reader n5 = new N5Factory().openReader(outputPath);
final N5DatasetDiscoverer datasetDiscoverer = new N5DatasetDiscoverer(n5, Executors.newSingleThreadExecutor(), (x) -> true,
Arrays.asList(N5ViewerCreator.n5vParsers),
Arrays.asList(N5ViewerCreator.n5vGroupParsers));

final N5TreeNode root = datasetDiscoverer.discoverAndParseRecursive("");
final Optional<N5TreeNode> metaOpt = root.getDescendant(readerDataset);
if (!metaOpt.isPresent())
fail("could not find metadata at: " + readerDataset);

final List<ConverterSetup> converterSetups = new ArrayList<>();
final List<SourceAndConverter<T>> sourcesAndConverters = new ArrayList<>();

final SharedQueue sharedQueue = new SharedQueue(1);
final BdvOptions options = BdvOptions.options().frameTitle("N5 Viewer");

final int numTimepoints = N5Viewer.buildN5Sources(
n5,
new DataSelection(n5, Collections.singletonList(metaOpt.get().getMetadata())),
sharedQueue,
converterSetups,
sourcesAndConverters,
options);

assertEquals(String.format("channels for %s", dataset), imp.getNChannels(), sourcesAndConverters.size());
assertEquals(String.format("time points for %s", dataset), imp.getNFrames(), numTimepoints);

final Source<T> src0 = sourcesAndConverters.get(0).getSpimSource();
assertEquals(String.format("slices for %s", dataset), imp.getNSlices(),
src0.getSource(0, 0).dimension(2));

final AffineTransform3D tform = new AffineTransform3D();
src0.getSourceTransform(0, 0, tform);
final double rx = tform.get(0, 0);
final double ry = tform.get(1, 1);
final double rz = tform.get(2, 2);
final String unit = src0.getVoxelDimensions().unit();

if (testMeta) {
final boolean resEqual = rx == imp.getCalibration().pixelWidth &&
ry == imp.getCalibration().pixelHeight &&
rz == imp.getCalibration().pixelDepth;

assertTrue(String.format("%s resolutions ", dataset), resEqual);
assertTrue(String.format("%s units ", dataset),
unit.equals(imp.getCalibration().getUnit()));
try( final N5Writer n5 = new N5Factory().openWriter(outputPath) ) {

}
final N5DatasetDiscoverer datasetDiscoverer = new N5DatasetDiscoverer(n5, Executors.newSingleThreadExecutor(), (x) -> true,
Arrays.asList(N5ViewerCreator.n5vParsers),
Arrays.asList(N5ViewerCreator.n5vGroupParsers));

if (testData) {
final List<Source<T>> srcList = sourcesAndConverters.stream().map(sac -> sac.getSpimSource()).collect(Collectors.toList());
assertTrue(String.format("%s data ", dataset), sourceDataIdentical(imp, srcList));
}
n5.close();
final N5TreeNode root = datasetDiscoverer.discoverAndParseRecursive("");
final Optional<N5TreeNode> metaOpt = root.getDescendant(readerDataset);
if (!metaOpt.isPresent())
fail("could not find metadata at: " + readerDataset);

final List<ConverterSetup> converterSetups = new ArrayList<>();
final List<SourceAndConverter<T>> sourcesAndConverters = new ArrayList<>();

final SharedQueue sharedQueue = new SharedQueue(1);
final BdvOptions options = BdvOptions.options().frameTitle("N5 Viewer");

final int numTimepoints = N5Viewer.buildN5Sources(
n5,
new DataSelection(n5, Collections.singletonList(metaOpt.get().getMetadata())),
sharedQueue,
converterSetups,
sourcesAndConverters,
options);

assertEquals(String.format("channels for %s", dataset), imp.getNChannels(), sourcesAndConverters.size());
assertEquals(String.format("time points for %s", dataset), imp.getNFrames(), numTimepoints);

// remove
final N5Writer n5w = new N5Factory().openWriter(outputPath);
n5w.remove();
final Source<T> src0 = sourcesAndConverters.get(0).getSpimSource();
assertEquals(String.format("slices for %s", dataset), imp.getNSlices(),
src0.getSource(0, 0).dimension(2));

final AffineTransform3D tform = new AffineTransform3D();
src0.getSourceTransform(0, 0, tform);
final double rx = tform.get(0, 0);
final double ry = tform.get(1, 1);
final double rz = tform.get(2, 2);
final String unit = src0.getVoxelDimensions().unit();

if (testMeta) {
final boolean resEqual = rx == imp.getCalibration().pixelWidth &&
ry == imp.getCalibration().pixelHeight &&
rz == imp.getCalibration().pixelDepth;

assertTrue(String.format("%s resolutions ", dataset), resEqual);
assertTrue(String.format("%s units ", dataset),
unit.equals(imp.getCalibration().getUnit()));

}

if (testData) {
final List<Source<T>> srcList = sourcesAndConverters.stream().map(sac -> sac.getSpimSource()).collect(Collectors.toList());
assertTrue(String.format("%s data ", dataset), sourceDataIdentical(imp, srcList));
}
n5.remove();
}
}

/*
Expand Down

0 comments on commit 677aa02

Please sign in to comment.