Skip to content

Commit

Permalink
tests: fix TestExportImports and make more granular
Browse files Browse the repository at this point in the history
  • Loading branch information
bogovicj authored Sep 25, 2024
1 parent 8a395a1 commit 6a18630
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 64 deletions.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,19 @@
<url>https://maven.scijava.org/content/groups/public</url>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
</configuration>
</plugin>
</plugins>
</build>


</project>
63 changes: 41 additions & 22 deletions src/test/java/org/janelia/saalfeldlab/n5/MetadataIoTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Optional;
import java.util.stream.IntStream;

import org.codehaus.plexus.util.FileUtils;
import org.janelia.saalfeldlab.n5.hdf5.N5HDF5Writer;
import org.janelia.saalfeldlab.n5.ij.N5IJUtils;
import org.janelia.saalfeldlab.n5.metadata.imagej.CosemToImagePlus;
Expand All @@ -53,8 +54,9 @@
import org.janelia.saalfeldlab.n5.universe.metadata.N5SingleScaleMetadataParser;
import org.janelia.saalfeldlab.n5.universe.metadata.N5ViewerMultiscaleMetadataParser;
import org.janelia.saalfeldlab.n5.zarr.N5ZarrWriter;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import ij.ImagePlus;
Expand All @@ -65,7 +67,7 @@

public class MetadataIoTests
{
static private String testDirPath = createTestDirPath("n5-test");
private static String testDirPath = createTestDirPath("n5-ij-metatest-");
private static String createTestDirPath(String dirName) {
try {
return Files.createTempDirectory(dirName).toString();
Expand All @@ -75,13 +77,13 @@ private static String createTestDirPath(String dirName) {
}
static private String testBaseDatasetName = "/test/data";

private ImagePlus imp2d;
private ImagePlus imp3d;
private ImagePlus imp4d;
private ImagePlus imp5d;
private static ImagePlus imp2d;
private static ImagePlus imp3d;
private static ImagePlus imp4d;
private static ImagePlus imp5d;

@Before
public void setUp() throws IOException
@BeforeClass
public static void setUp() throws IOException
{
final ArrayImgFactory< UnsignedByteType > factory = new ArrayImgFactory<>( new UnsignedByteType() );

Expand All @@ -108,6 +110,18 @@ public void setUp() throws IOException
imp5d.getCalibration().pixelDepth = 1.1;
}

@AfterClass
public static void tearDown() {

try {
final File d = new File(testDirPath);
FileUtils.deleteDirectory(d);
d.delete();
} catch (IOException e) {
e.printStackTrace();
}
}

@Test
public void testCustomMetadata()
{
Expand All @@ -117,7 +131,7 @@ public void testCustomMetadata()
N5FSWriter n5 = null;
try
{
n5 = new N5FSWriter( testDirPath+ ".n5" );
n5 = new N5FSWriter(new File(testDirPath, "testCustom.n5").getAbsolutePath());
n5.createDataset( "/testpath",
new long[]{2,3,4}, new int[] {2,3,4}, DataType.UINT8 , new RawCompression() );

Expand Down Expand Up @@ -245,28 +259,31 @@ public void testCosem()
@Test
public void testH5()
{
final N5HDF5Writer n5 = new N5HDF5Writer( testDirPath + ".h5", 32, 32, 32, 32, 32 );
testAllMetadataTypes( n5 );
final N5HDF5Writer h5 = new N5HDF5Writer( new File( testDirPath, "test.h5").getAbsolutePath(), 32, 32, 32, 32, 32 );
testAllMetadataTypes( h5 );
h5.close();
}

@Test
public void testZarr()
{
try
{
final N5ZarrWriter n5 = new N5ZarrWriter( testDirPath+ ".zarr" );
testAllMetadataTypes( n5 );
final N5ZarrWriter zarr = new N5ZarrWriter(new File(testDirPath, "test.zarr").getAbsolutePath());
testAllMetadataTypes( zarr );

n5.remove( testBaseDatasetName + "/imp2d" );
n5.remove( testBaseDatasetName + "/imp2_cosemd" );
n5.remove( testBaseDatasetName + "/imp2_n5v" );
zarr.remove( testBaseDatasetName + "/imp2d" );
zarr.remove( testBaseDatasetName + "/imp2_cosemd" );
zarr.remove( testBaseDatasetName + "/imp2_n5v" );

n5.remove( testBaseDatasetName + "/imp3d" );
n5.remove( testBaseDatasetName + "/imp3d_cosem" );
n5.remove( testBaseDatasetName + "/imp3d_n5v" );
zarr.remove( testBaseDatasetName + "/imp3d" );
zarr.remove( testBaseDatasetName + "/imp3d_cosem" );
zarr.remove( testBaseDatasetName + "/imp3d_n5v" );

n5.remove( testBaseDatasetName + "/imp4d" );
n5.remove( testBaseDatasetName + "/imp5d" );
zarr.remove( testBaseDatasetName + "/imp4d" );
zarr.remove( testBaseDatasetName + "/imp5d" );

zarr.close();
}
catch ( final N5Exception e )
{
Expand All @@ -281,7 +298,7 @@ public void testN5FileSystem()
N5FSWriter n5;
try
{
n5 = new N5FSWriter( testDirPath+ ".n5" );
n5 = new N5FSWriter(new File(testDirPath, "test.n5").getAbsolutePath());
testAllMetadataTypes( n5 );

n5.remove( testBaseDatasetName + "/imp2d" );
Expand All @@ -294,6 +311,8 @@ public void testN5FileSystem()

n5.remove( testBaseDatasetName + "/imp4d" );
n5.remove( testBaseDatasetName + "/imp5d" );

n5.close();
}
catch ( final N5Exception e )
{
Expand Down
83 changes: 48 additions & 35 deletions src/test/java/org/janelia/saalfeldlab/n5/TestExportImports.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -28,9 +29,9 @@
import org.janelia.saalfeldlab.n5.universe.metadata.ome.ngff.v04.NgffSingleScaleMetadataParser;
import org.janelia.saalfeldlab.n5.zarr.N5ZarrReader;
import org.janelia.saalfeldlab.n5.zarr.N5ZarrWriter;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import ij.ImagePlus;
Expand All @@ -49,10 +50,10 @@
public class TestExportImports
{

private File baseDir;
private static File baseDir;

@Before
public void before() {
@BeforeClass
public static void setup() {

final URL configUrl = RunImportExportTest.class.getResource("/plugins.config");
baseDir = new File(configUrl.getFile()).getParentFile();
Expand All @@ -65,20 +66,25 @@ public void before() {
}
}

@After
public void after() {
@AfterClass
public static void tearDown() {

baseDir.delete();
}

private boolean deleteContainer(final String rootPath) {
private static boolean deleteContainer(final String rootPath) {

N5Writer n5w = new N5Factory().openWriter(rootPath);
return n5w.remove();
try ( final N5Writer n5w = new N5Factory().openWriter(rootPath) ) {
return n5w.remove();
} catch( N5Exception e ) {
e.printStackTrace();
}

return false;
}

@Test
public void testEmptyMeta()
public void testEmptyMeta() throws InterruptedException
{
final ImagePlus imp = NewImage.createImage("test", 8, 6, 2, 16, NewImage.FILL_NOISE);
final String metaType = N5Importer.MetadataDefaultKey;
Expand Down Expand Up @@ -127,7 +133,7 @@ public void test4dN5v()
}

@Test
public void testReadWriteParse()
public void testReadWriteParse() throws InterruptedException
{
final HashMap<String,String> typeToExtension = new HashMap<>();
typeToExtension.put( "FILESYSTEM", "n5" );
Expand All @@ -152,11 +158,12 @@ public void testReadWriteParse()
{
for( final String metatype : metadataTypes )
{
final String n5RootPath = baseDir + "/test." + typeToExtension.get( containerType );
final String n5RootPath = baseDir + "/test-" + metatype + "-" + bitDepth + "." + typeToExtension.get( containerType );
final String datasetBase = "/test_"+metatype+"_"+bitDepth;
final String dataset = datasetBase;

singleReadWriteParseTest( imp, n5RootPath, dataset, blockSizeString, metatype, compressionString, true );
Thread.sleep(25);
}
}
}
Expand Down Expand Up @@ -249,20 +256,20 @@ public void singleReadWriteParseTest(
final String blockSizeString,
final String metadataType,
final String compressionType,
final boolean testMeta )
final boolean testMeta ) throws InterruptedException
{
singleReadWriteParseTest( imp, outputPath, dataset, blockSizeString, metadataType, compressionType, testMeta, true);
}

public void singleReadWriteParseTest(
public static void singleReadWriteParseTest(
final ImagePlus imp,
final String outputPath,
final String dataset,
final String blockSizeString,
final String metadataType,
final String compressionType,
final boolean testMeta,
final boolean testData )
final boolean testData ) throws InterruptedException
{
final N5ScalePyramidExporter writer = new N5ScalePyramidExporter();
writer.setOptions( imp, outputPath, dataset, N5ScalePyramidExporter.AUTO_FORMAT, blockSizeString, false,
Expand All @@ -279,10 +286,19 @@ else if (metadataType.equals(N5Importer.MetadataOmeZarrKey) || metadataType.equa

final String n5PathAndDataset = outputPath + readerDataset;

final File n5RootWritten = new File(outputPath);
assertTrue("root does not exist: " + outputPath, n5RootWritten.exists());
if (outputPath.endsWith(".h5"))
assertTrue("hdf5 file exists", n5RootWritten.exists());
else
assertTrue("n5 or zarr root is not a directory:" + outputPath, n5RootWritten.isDirectory());

Thread.sleep(25);
final N5Importer reader = new N5Importer();
reader.setShow( false );
final List< ImagePlus > impList = reader.process( n5PathAndDataset, false );

assertNotNull(String.format( "Failed to open image: %s %s ", outputPath, dataset ), impList);
assertEquals( String.format( "%s %s one image opened ", outputPath, dataset ), 1, impList.size() );

final double EPS = 1e-9;
Expand All @@ -293,8 +309,7 @@ else if (metadataType.equals(N5Importer.MetadataOmeZarrKey) || metadataType.equa
assertEquals( String.format( "%s resolutions y", dataset ), imp.getCalibration().pixelHeight, impRead.getCalibration().pixelHeight, EPS );
assertEquals( String.format( "%s resolutions z", dataset ), imp.getCalibration().pixelDepth, impRead.getCalibration().pixelDepth, EPS );

final boolean unitsEqual = impRead.getCalibration().getUnit().equals( imp.getCalibration().getUnit() );
assertTrue( String.format( "%s units ", dataset ), unitsEqual );
final boolean unitsEqual = impRead.getCalibration().getUnit().equals( imp.getCalibration().getUnit() ); assertTrue( String.format( "%s units ", dataset ), unitsEqual );
}

if( testData )
Expand All @@ -311,19 +326,12 @@ else if (metadataType.equals(N5Importer.MetadataOmeZarrKey) || metadataType.equa
assertTrue( String.format( "%s data ", dataset ), imagesEqual );
}

try {
final N5Writer n5w = new N5Factory().openWriter(outputPath);
n5w.remove();
} catch (final N5Exception e) {
e.printStackTrace();
}

impRead.close();

deleteContainer(outputPath);
}

@Test
public void testRgb()
public void testRgb() throws InterruptedException
{
final ImagePlus imp = NewImage.createRGBImage("test", 8, 6, 4, NewImage.FILL_NOISE);
final String metaType = N5Importer.MetadataImageJKey;
Expand All @@ -346,10 +354,14 @@ public void testMultiChannel()
{
for( final String suffix : new String[] { ".h5", ".n5", ".zarr" })
{
testMultiChannelHelper(N5Importer.MetadataN5ViewerKey, suffix);
testMultiChannelHelper(N5Importer.MetadataN5CosemKey, suffix);
testMultiChannelHelper(N5Importer.MetadataOmeZarrKey, suffix);
testMultiChannelHelper(N5Importer.MetadataImageJKey, suffix);
try {
testMultiChannelHelper(N5Importer.MetadataN5ViewerKey, suffix);
testMultiChannelHelper(N5Importer.MetadataN5CosemKey, suffix);
testMultiChannelHelper(N5Importer.MetadataOmeZarrKey, suffix);
testMultiChannelHelper(N5Importer.MetadataImageJKey, suffix);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Expand Down Expand Up @@ -563,11 +575,9 @@ public void reset() {

}

public void testMultiChannelHelper( final String metatype, final String suffix )
public void testMultiChannelHelper( final String metatype, final String suffix ) throws InterruptedException
{
final int bitDepth = 8;

final String n5RootPath = baseDir + "/test_"+ metatype+"_dimCombos" + suffix;
final String blockSizeString = "16";
final String compressionString = "raw";

Expand All @@ -591,8 +601,11 @@ public void testMultiChannelHelper( final String metatype, final String suffix )
if( nz > 1 )
imp.getCalibration().pixelDepth = 0.7;

final String dataset = String.format("/c%dz%dt%d", nc, nz, nt);
final String dimCode = String.format("c%dz%dt%d", nc, nz, nt);
final String n5RootPath = baseDir + "/test_" + metatype + "_" + dimCode + suffix;
final String dataset = String.format("/%s", dimCode);
singleReadWriteParseTest( imp, n5RootPath, dataset, blockSizeString, metatype, compressionString, true, nc == 1 );
Thread.sleep(25);
}
}
}
Expand Down
Loading

0 comments on commit 6a18630

Please sign in to comment.