Skip to content

Commit

Permalink
Merge pull request #22 from KateMoreva/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
KateMoreva authored Sep 2, 2021
2 parents df9aeac + 96aa81a commit d723edb
Show file tree
Hide file tree
Showing 14 changed files with 896 additions and 10 deletions.
8 changes: 5 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<version>2.22.0</version>
<configuration>
<argLine>-Xmx4024m</argLine>
</configuration>
Expand Down Expand Up @@ -176,8 +176,10 @@
<version>${j3dcore.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.embl.cba</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package de.embl.cba.n5.ome.zarr.writers.imgplus;

import bdv.export.ExportMipmapInfo;
import bdv.export.ProgressWriter;
import bdv.export.ProposeMipmaps;
import bdv.export.SubTaskProgressWriter;
import bdv.spimdata.SequenceDescriptionMinimal;
import bdv.spimdata.SpimDataMinimal;
import bdv.spimdata.XmlIoSpimDataMinimal;
import de.embl.cba.n5.ome.zarr.loaders.N5OMEZarrImageLoader;
import de.embl.cba.n5.util.DownsampleBlock;
import de.embl.cba.n5.util.ExportScalePyramid;
import de.embl.cba.n5.util.writers.WriteImgPlusToN5;
import de.embl.cba.n5.ome.zarr.readers.N5OmeZarrReader;
import ij.IJ;
import ij.ImagePlus;
import mpicbg.spim.data.SpimDataException;
import mpicbg.spim.data.generic.sequence.BasicViewSetup;
import mpicbg.spim.data.registration.ViewRegistration;
import mpicbg.spim.data.registration.ViewRegistrations;
import mpicbg.spim.data.sequence.FinalVoxelDimensions;
import net.imglib2.FinalDimensions;
import net.imglib2.realtransform.AffineTransform3D;
import org.janelia.saalfeldlab.n5.Compression;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import static de.embl.cba.n5.util.writers.WriteImgPlusToN5Helper.*;

public class WriteImgPlusToN5BdvOmeZarr extends WriteImgPlusToN5 {

@Override
public void export(ImagePlus imp, int[][] resolutions, int[][] subdivisions, String xmlPath,
AffineTransform3D sourceTransform, DownsampleBlock.DownsamplingMethod downsamplingMethod,
Compression compression, String[] viewSetupNames) {
if (resolutions.length == 0) {
IJ.showMessage("Invalid resolutions - length 0");
return;
}

if (subdivisions.length == 0) {
IJ.showMessage(" Invalid subdivisions - length 0");
return;
}

if (resolutions.length != subdivisions.length) {
IJ.showMessage("Subsampling factors and chunk sizes must have the same number of elements");
return;
}

String seqFilename = xmlPath;
if (!seqFilename.endsWith(".xml"))
seqFilename += ".xml";
final File seqFile = getSeqFileFromPath(seqFilename);
if (seqFile == null) {
return;
}

final File zarrFile = getOmeZarrFileFromXmlPath(seqFilename);

// TODO - check transform and downsampling mode

Parameters exportParameters = new Parameters(resolutions, subdivisions, seqFile, zarrFile, sourceTransform,
downsamplingMethod, compression, viewSetupNames);

export(imp, exportParameters);
}

// TODO - split some of this into common functions
@Override
protected Parameters generateDefaultParameters(ImagePlus imp, String xmlPath, AffineTransform3D sourceTransform,
DownsampleBlock.DownsamplingMethod downsamplingMethod, Compression compression,
String[] viewSetupNames) {
FinalVoxelDimensions voxelSize = getVoxelSize(imp);
FinalDimensions size = getSize(imp);

// propose reasonable mipmap settings
final int maxNumElements = 64 * 64 * 64;
final ExportMipmapInfo autoMipmapSettings = ProposeMipmaps.proposeMipmaps(
new BasicViewSetup(0, "", size, voxelSize),
maxNumElements);

int[][] resolutions = autoMipmapSettings.getExportResolutions();
int[][] subdivisions = autoMipmapSettings.getSubdivisions();

if (resolutions.length == 0 || subdivisions.length == 0 || resolutions.length != subdivisions.length) {
IJ.showMessage("Error with calculating default subdivisions and resolutions");
return null;
}

String seqFilename = xmlPath;
if (!seqFilename.endsWith(".xml"))
seqFilename += ".xml";
final File seqFile = getSeqFileFromPath(seqFilename);
if (seqFile == null) {
return null;
}

final File zarrFile = getOmeZarrFileFromXmlPath(seqFilename);

return new Parameters(resolutions, subdivisions, seqFile, zarrFile, sourceTransform,
downsamplingMethod, compression, viewSetupNames);
}

@Override
protected void writeFiles(SequenceDescriptionMinimal seq, Map<Integer, ExportMipmapInfo> perSetupExportMipmapInfo,
Parameters params, ExportScalePyramid.LoopbackHeuristic loopbackHeuristic,
ExportScalePyramid.AfterEachPlane afterEachPlane, int numCellCreatorThreads,
ProgressWriter progressWriter, int numTimepoints, int numSetups) throws IOException, SpimDataException {
WriteSequenceToN5OmeZarr.writeOmeZarrFile(seq, perSetupExportMipmapInfo,
params.downsamplingMethod,
params.compression, params.n5File,
loopbackHeuristic, afterEachPlane, numCellCreatorThreads,
new SubTaskProgressWriter(progressWriter, 0, 0.95));

// write xml sequence description
final N5OMEZarrImageLoader zarrLoader = new N5OMEZarrImageLoader(
new N5OmeZarrReader(params.n5File.getAbsolutePath()), seq);
final SequenceDescriptionMinimal seqh5 = new SequenceDescriptionMinimal(seq, zarrLoader);

final ArrayList<ViewRegistration> registrations = new ArrayList<>();
for (int t = 0; t < numTimepoints; ++t)
for (int s = 0; s < numSetups; ++s)
registrations.add(new ViewRegistration(t, s, params.sourceTransform));

final File basePath = params.seqFile.getParentFile();
final SpimDataMinimal spimData = new SpimDataMinimal(basePath, seqh5, new ViewRegistrations(registrations));

new XmlIoSpimDataMinimal().save(spimData, params.seqFile.getAbsolutePath());

progressWriter.setProgress(1.0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package de.embl.cba.n5.ome.zarr.writers.imgplus;

import bdv.export.ExportMipmapInfo;
import bdv.export.ProgressWriter;
import bdv.export.ProposeMipmaps;
import bdv.export.SubTaskProgressWriter;
import bdv.spimdata.SequenceDescriptionMinimal;
import de.embl.cba.n5.util.DownsampleBlock;
import de.embl.cba.n5.util.ExportScalePyramid;
import de.embl.cba.n5.util.writers.WriteImgPlusToN5;
import ij.IJ;
import ij.ImagePlus;
import mpicbg.spim.data.SpimDataException;
import mpicbg.spim.data.generic.sequence.BasicViewSetup;
import mpicbg.spim.data.sequence.FinalVoxelDimensions;
import net.imglib2.FinalDimensions;
import net.imglib2.realtransform.AffineTransform3D;
import org.janelia.saalfeldlab.n5.Compression;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import static de.embl.cba.n5.util.writers.WriteImgPlusToN5Helper.getSize;
import static de.embl.cba.n5.util.writers.WriteImgPlusToN5Helper.getVoxelSize;

public class WriteImgPlusToN5OmeZarr extends WriteImgPlusToN5 {

// TODO - deal with transforms properly - is there somewhere in ome-zarr this can be written?

// export, generating default source transform, and default resolutions / subdivisions
@Override
public void export( ImagePlus imp, String zarrPath, DownsampleBlock.DownsamplingMethod downsamplingMethod,
Compression compression ) {
super.export( imp, zarrPath, downsamplingMethod, compression );
}

// export, generating default resolutions / subdivisions
@Override
public void export(ImagePlus imp, String zarPath, AffineTransform3D sourceTransform,
DownsampleBlock.DownsamplingMethod downsamplingMethod, Compression compression ) {
super.export( imp, zarPath, sourceTransform, downsamplingMethod, compression );
}


// export, generating default resolutions / subdivisions
@Override
public void export(ImagePlus imp, String zarrPath, AffineTransform3D sourceTransform,
DownsampleBlock.DownsamplingMethod downsamplingMethod, Compression compression,
String[] viewSetupNames ) {
super.export( imp, zarrPath, sourceTransform, downsamplingMethod, compression, viewSetupNames );
}

@Override
public void export( ImagePlus imp, int[][] resolutions, int[][] subdivisions, String zarrPath,
AffineTransform3D sourceTransform, DownsampleBlock.DownsamplingMethod downsamplingMethod,
Compression compression ) {
export( imp, resolutions, subdivisions, zarrPath, sourceTransform, downsamplingMethod, compression, null );
}

@Override
public void export( ImagePlus imp, int[][] resolutions, int[][] subdivisions, String zarrPath,
AffineTransform3D sourceTransform, DownsampleBlock.DownsamplingMethod downsamplingMethod,
Compression compression, String[] viewSetupNames ) {
if ( resolutions.length == 0 ) {
IJ.showMessage( "Invalid resolutions - length 0" );
return;
}

if ( subdivisions.length == 0 ) {
IJ.showMessage( " Invalid subdivisions - length 0" );
return;
}

if ( resolutions.length != subdivisions.length ) {
IJ.showMessage( "Subsampling factors and chunk sizes must have the same number of elements" );
return;
}

final File zarrFile = new File( zarrPath );

// TODO - check transform and downsampling mode

Parameters exportParameters = new Parameters( resolutions, subdivisions, null, zarrFile, sourceTransform,
downsamplingMethod, compression, viewSetupNames );

export( imp, exportParameters );
}

@Override
protected Parameters generateDefaultParameters(ImagePlus imp, String zarrPath, AffineTransform3D sourceTransform,
DownsampleBlock.DownsamplingMethod downsamplingMethod, Compression compression,
String[] viewSetupNames ) {
FinalVoxelDimensions voxelSize = getVoxelSize( imp );
FinalDimensions size = getSize( imp );

// propose reasonable mipmap settings
final int maxNumElements = 64 * 64 * 64;
final ExportMipmapInfo autoMipmapSettings = ProposeMipmaps.proposeMipmaps(
new BasicViewSetup(0, "", size, voxelSize),
maxNumElements);

int[][] resolutions = autoMipmapSettings.getExportResolutions();
int[][] subdivisions = autoMipmapSettings.getSubdivisions();

if ( resolutions.length == 0 || subdivisions.length == 0 || resolutions.length != subdivisions.length ) {
IJ.showMessage( "Error with calculating default subdivisions and resolutions");
return null;
}

final File zarrFile = new File( zarrPath );

return new Parameters( resolutions, subdivisions, null, zarrFile, sourceTransform,
downsamplingMethod, compression, viewSetupNames );
}

@Override
protected void writeFiles(SequenceDescriptionMinimal seq, Map<Integer, ExportMipmapInfo> perSetupExportMipmapInfo,
Parameters params, ExportScalePyramid.LoopbackHeuristic loopbackHeuristic,
ExportScalePyramid.AfterEachPlane afterEachPlane, int numCellCreatorThreads,
ProgressWriter progressWriter, int numTimepoints, int numSetups ) throws IOException, SpimDataException {
WriteSequenceToN5OmeZarr.writeOmeZarrFile( seq, perSetupExportMipmapInfo,
params.downsamplingMethod,
params.compression, params.n5File,
loopbackHeuristic, afterEachPlane, numCellCreatorThreads,
new SubTaskProgressWriter( progressWriter, 0, 0.95 ) );

progressWriter.setProgress( 1.0 );
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.embl.cba.n5.ome.zarr.projectcreator;
package de.embl.cba.n5.ome.zarr.writers.imgplus;

import bdv.export.ExportMipmapInfo;
import bdv.export.ProgressWriter;
Expand All @@ -7,10 +7,12 @@
import bdv.img.cache.SimpleCacheArrayLoader;
import bdv.img.n5.N5ImageLoader;
import com.google.gson.GsonBuilder;
import de.embl.cba.n5.ome.zarr.writers.N5OMEZarrWriter;
import de.embl.cba.n5.util.DownsampleBlock;
import de.embl.cba.n5.util.ExportScalePyramid;
import de.embl.cba.n5.ome.zarr.util.OmeZarrMultiscales;
import de.embl.cba.n5.ome.zarr.util.ZarrAxes;
import de.embl.cba.n5.ome.zarr.util.ZarrDatasetAttributes;
import de.embl.cba.n5.ome.zarr.writers.N5OMEZarrWriter;
import mpicbg.spim.data.generic.sequence.AbstractSequenceDescription;
import mpicbg.spim.data.generic.sequence.BasicImgLoader;
import mpicbg.spim.data.generic.sequence.BasicSetupImgLoader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.embl.cba.n5.ome.zarr.projectcreator;
package de.embl.cba.n5.util;

/*-
* #%L
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.embl.cba.n5.ome.zarr.projectcreator;
package de.embl.cba.n5.util;

/*
* #%L
Expand Down
Loading

0 comments on commit d723edb

Please sign in to comment.