Skip to content

Commit

Permalink
Update to scijava pom 37.0.0 N5 to ImagePlus updates
Browse files Browse the repository at this point in the history
  • Loading branch information
karlduderstadt committed Nov 2, 2023
1 parent 9084298 commit f1cb4c6
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 43 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>35.1.0</version>
<version>37.0.0</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -151,6 +151,13 @@
<artifactId>n5-imglib2</artifactId>
<version>${n5-imglib2.version}</version>
</dependency>

<!-- Mars dependencies -->
<dependency>
<groupId>de.mpg.biochem</groupId>
<artifactId>mars-scifio</artifactId>
<version>0.5.5</version>
</dependency>
</dependencies>

<repositories>
Expand Down
27 changes: 20 additions & 7 deletions src/main/java/de/mpg/biochem/mars/n5/MarsN5Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ public class MarsN5Factory implements Serializable {
private int[] hdf5DefaultBlockSize = {64, 64, 64, 1, 1};
private boolean hdf5OverrideBlockSize = false;
private GsonBuilder gsonBuilder = new GsonBuilder();
private boolean cacheAttributes = true;
private String zarrDimensionSeparator = ".";
private boolean zarrMapN5DatasetAttributes = true;
private boolean zarrMergeAttributes = true;
private String googleCloudProjectId = null;

public MarsN5Factory hdf5DefaultBlockSize(final int... blockSize) {
Expand Down Expand Up @@ -245,7 +247,7 @@ public N5FSReader openFSReader(final String path) throws IOException {
*/
public N5ZarrReader openZarrReader(final String path) throws IOException {

return new N5ZarrReader(path, gsonBuilder, zarrDimensionSeparator, zarrMapN5DatasetAttributes);
return new N5ZarrReader(path, gsonBuilder, zarrMapN5DatasetAttributes, zarrMergeAttributes, cacheAttributes);
}

/**
Expand Down Expand Up @@ -292,9 +294,12 @@ public N5GoogleCloudStorageReader openGoogleCloudReader(final String url) throws
* @throws IOException the io exception
*/
public N5AmazonS3Reader openAWSS3Reader(final String url) throws IOException {
AmazonS3URI s3uri = new AmazonS3URI(url);

return new N5AmazonS3Reader(
createS3(url),
new AmazonS3URI(url),
s3uri.getBucket(),
s3uri.getKey(),
gsonBuilder);
}

Expand All @@ -307,9 +312,12 @@ public N5AmazonS3Reader openAWSS3Reader(final String url) throws IOException {
* @throws IOException the io exception
*/
public N5AmazonS3Reader openAWSS3ReaderWithEndpoint(final String s3Url, final String endpointUrl) throws IOException {
AmazonS3URI s3uri = new AmazonS3URI(s3Url);

return new N5AmazonS3Reader(
createS3WithEndpoint(endpointUrl),
new AmazonS3URI(s3Url),
s3uri.getBucket(),
s3uri.getKey(),
gsonBuilder);
}

Expand All @@ -334,8 +342,9 @@ public N5FSWriter openFSWriter(final String path) throws IOException {
* @return the N5ZarrWriter
* @throws IOException the io exception
*/
public N5ZarrWriter openZarrWriter(final String path) throws IOException {
return new N5ZarrWriter(path, gsonBuilder, zarrDimensionSeparator, zarrMapN5DatasetAttributes);
public N5ZarrWriter openZarrWriter(final String path) {

return new N5ZarrWriter(path, gsonBuilder, zarrDimensionSeparator, zarrMapN5DatasetAttributes, true);
}

/**
Expand Down Expand Up @@ -389,10 +398,12 @@ public N5GoogleCloudStorageWriter openGoogleCloudWriter(final String url) throws
* @throws IOException the io exception
*/
public N5AmazonS3Writer openAWSS3Writer(final String url) throws IOException {
AmazonS3URI s3uri = new AmazonS3URI(url);

return new N5AmazonS3Writer(
createS3(url),
new AmazonS3URI(url),
s3uri.getBucket(),
s3uri.getKey(),
gsonBuilder);
}

Expand All @@ -405,10 +416,12 @@ public N5AmazonS3Writer openAWSS3Writer(final String url) throws IOException {
* @throws IOException the io exception
*/
public N5AmazonS3Writer openAWSS3WriterWithEndpoint(final String s3Url, final String endpointUrl) throws IOException {
AmazonS3URI s3uri = new AmazonS3URI(s3Url);

return new N5AmazonS3Writer(
createS3WithEndpoint(endpointUrl),
new AmazonS3URI(s3Url),
s3uri.getBucket(),
s3uri.getKey(),
gsonBuilder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@
import com.amazonaws.services.s3.AmazonS3;
import ij.*;
import ij.io.FileInfo;
import io.scif.FormatException;
import io.scif.img.SCIFIOImgPlus;
import net.imagej.DatasetService;
import net.imagej.ImgPlus;
import net.imagej.axis.AxisType;
import org.apache.commons.io.IOUtils;
import org.janelia.saalfeldlab.n5.ij.N5Importer;
import org.janelia.saalfeldlab.n5.imglib2.N5Utils;
import org.janelia.saalfeldlab.n5.s3.N5AmazonS3Reader;
import org.janelia.saalfeldlab.n5.universe.metadata.N5DatasetMetadata;
import org.janelia.saalfeldlab.n5.universe.metadata.ome.ngff.v04.OmeNgffMetadataParser;
import org.scijava.Context;
import org.scijava.ItemIO;
import org.scijava.app.StatusService;
import org.scijava.command.Command;
Expand All @@ -45,15 +54,24 @@
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

import net.imagej.axis.Axis;
import net.imagej.axis.Axes;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import net.imglib2.cache.img.CachedCellImg;
import net.imglib2.type.numeric.NumericType;
import net.imagej.Dataset;
import ij.ImagePlus;
import net.imagej.DatasetService;
Expand All @@ -73,13 +91,38 @@
import org.janelia.saalfeldlab.n5.N5Reader;
import org.janelia.saalfeldlab.n5.ij.N5Importer.N5BasePathFun;
import org.janelia.saalfeldlab.n5.metadata.*;
import org.janelia.saalfeldlab.n5.metadata.canonical.CanonicalMetadataParser;
import org.janelia.saalfeldlab.n5.metadata.imagej.ImagePlusLegacyMetadataParser;
import org.janelia.saalfeldlab.n5.ui.DataSelection;
import org.janelia.saalfeldlab.n5.ui.DatasetSelectorDialog;
import org.janelia.saalfeldlab.n5.ui.N5DatasetTreeCellRenderer;
//import org.janelia.saalfeldlab.n5.universe.metadata.axes.AxisMetadata;
//import org.janelia.saalfeldlab.n5.universe.metadata.axes.AxisUtils;

import org.janelia.saalfeldlab.n5.universe.metadata.N5CosemMetadata;
import org.janelia.saalfeldlab.n5.universe.metadata.N5CosemMetadataParser;
import org.janelia.saalfeldlab.n5.universe.metadata.N5CosemMultiScaleMetadata;
import org.janelia.saalfeldlab.n5.universe.metadata.N5DatasetMetadata;
import org.janelia.saalfeldlab.n5.universe.metadata.N5GenericSingleScaleMetadataParser;
import org.janelia.saalfeldlab.n5.universe.metadata.N5Metadata;
import org.janelia.saalfeldlab.n5.universe.metadata.N5MetadataParser;
import org.janelia.saalfeldlab.n5.universe.metadata.N5SingleScaleMetadata;
import org.janelia.saalfeldlab.n5.universe.metadata.N5SingleScaleMetadataParser;
import org.janelia.saalfeldlab.n5.universe.metadata.N5ViewerMultiscaleMetadataParser;
import org.janelia.saalfeldlab.n5.universe.metadata.axes.AxisMetadata;
import org.janelia.saalfeldlab.n5.universe.metadata.axes.AxisUtils;
import org.janelia.saalfeldlab.n5.universe.metadata.canonical.CanonicalDatasetMetadata;
import org.janelia.saalfeldlab.n5.universe.metadata.canonical.CanonicalMetadataParser;
import org.janelia.saalfeldlab.n5.universe.metadata.canonical.CanonicalSpatialDatasetMetadata;
import org.janelia.saalfeldlab.n5.universe.metadata.ome.ngff.v04.OmeNgffMetadataParser;

import net.imglib2.img.imageplus.ImagePlusImg;
import net.imglib2.img.imageplus.ImagePlusImgFactory;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.util.Util;
import net.imglib2.view.Views;
import net.imglib2.parallel.DefaultTaskExecutor;
import org.scijava.ui.UIService;
import io.scif.Metadata;

import de.mpg.biochem.mars.scifio.MarsMicromanagerFormat;

@Plugin(type = Command.class, label = "Open N5 as ImagePlus", menu = { @Menu(
label = MenuConstants.PLUGINS_LABEL, weight = MenuConstants.PLUGINS_WEIGHT,
Expand All @@ -101,22 +144,27 @@ public class MarsOpenN5asImagePlusCommand extends DynamicCommand implements Comm
@Parameter
private ConvertService convertService;

@Parameter
private UIService uiService;

@Parameter
private DatasetService datasetService;

@Parameter(label = "image", type = ItemIO.OUTPUT)
private Dataset dataset;
public static final N5MetadataParser<?>[] PARSERS = new N5MetadataParser[]{
new ImagePlusLegacyMetadataParser(),
new N5CosemMetadataParser(),
new N5SingleScaleMetadataParser(),
new CanonicalMetadataParser(),
new N5GenericSingleScaleMetadataParser()
};

@Override
public void run() {
DatasetSelectorDialog selectionDialog = new DatasetSelectorDialog(
new MarsN5ViewerReaderFun(), new N5BasePathFun(),
"", new N5MetadataParser[] {},
new N5MetadataParser[] { new ImagePlusLegacyMetadataParser(),
new N5CosemMetadataParser(),
new N5SingleScaleMetadataParser(),
new CanonicalMetadataParser(),
new N5GenericSingleScaleMetadataParser() });
"",
new N5MetadataParser[]{ new OmeNgffMetadataParser() }, // need the ngff parser because it's where the metadata are
PARSERS);

selectionDialog.setVirtualOption(true);
selectionDialog.setCropOption(false);
Expand All @@ -129,48 +177,128 @@ public void run() {

final Consumer<DataSelection> callback = (
DataSelection dataSelection) -> {
//Read metadata file
//MarsMicromanagerFormat.Parser

//parsePositionMV2(final String jsonData, final Metadata meta,
//final int posIndex)



String rootPath = selectionDialog.getN5RootPath();
String datasetPath = dataSelection.metadata.get(0).getPath();
N5DatasetMetadata datasetMeta = (N5DatasetMetadata) dataSelection.metadata.get(0);

//Build n5 reader
N5Reader n5 = new MarsN5ViewerReaderFun().apply(rootPath);
ExecutorService exec = Executors.newFixedThreadPool(8);

//System.out.println(((N5AmazonS3Reader) n5).getKeyValueAccess().lockForReading(datasetPath + "/metadata.txt").newInputStream());
try {
InputStream inputStream = ((N5AmazonS3Reader) n5).getKeyValueAccess().lockForReading(datasetPath + "/metadata.txt").newInputStream();
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
String[] jsonData = new String[1];
jsonData[0] = result;

System.out.println("datasetMeta " + dataSelection.metadata.get(0));
MarsMicromanagerFormat.Parser parser = new MarsMicromanagerFormat.Parser();
MarsMicromanagerFormat.Metadata source = new MarsMicromanagerFormat.Metadata();

//if (datasetMeta != null && datasetMeta instanceof AxisMetadata) {
// System.out.println("AxisMetadata " + (AxisMetadata) datasetMeta));
//}
if (source.getContext() == null) source.setContext(getContext());
if (parser.getContext() == null) parser.setContext(getContext());

ImagePlus imp;
try {
imp = N5Importer.read(n5, exec, datasetMeta, null, selectionDialog.isVirtual(), null);
final List<MarsMicromanagerFormat.Position> positions = new ArrayList<>();
final MarsMicromanagerFormat.Position p = new MarsMicromanagerFormat.Position();
positions.add(p);
source.setPositions(positions);

//meta.setFiltered(config.parserIsFiltered());

//meta.setDatasetName(handle.get().getName());
//meta.setSource(handle);
//meta.setSourceLocation(handle.get());

parser.populateMetadata(jsonData, source, source, false);
source.populateImageMetadata();

Dataset dataset = getImage(n5, datasetMeta, source);

//System.out.println("dataset " + dataset.numDimensions() + " " + dataset.getImgPlus().dimensionsAsLongArray());

//dataset.getProperties().put("scifio.metadata.global", source);
uiService.show(dataset);

FileInfo fileInfo = imp.getOriginalFileInfo();
if (fileInfo == null)
fileInfo = new FileInfo();

fileInfo.url = rootPath + "?" + datasetMeta.getPath();
imp.setFileInfo(fileInfo);
imp.show();
//System.out.println(result);

//FileInfo fileInfo = imp.getOriginalFileInfo();
//if (fileInfo == null)
// fileInfo = new FileInfo();

//fileInfo.url = rootPath + "?" + datasetMeta.getPath();
//imp.setFileInfo(fileInfo);
//imp.show();
} catch (final IOException e) {
IJ.error("failed to read n5");
} catch (FormatException e) {
throw new RuntimeException(e);
}
};
};

selectionDialog.run(callback);
}
@SuppressWarnings({"rawtypes", "unchecked"})
private <T extends NumericType<T> & NativeType<T>> Dataset getImage(final N5Reader n5, final N5DatasetMetadata datasetMeta, final boolean asVirtual) {
ExecutorService exec = Executors.newFixedThreadPool(8);

final CachedCellImg imgRaw = N5Utils.open(n5, datasetMeta.getPath());

//Images are stored as XYZCT and we permute to XYCZT which is ImagePlus axis order
final RandomAccessibleInterval<T> imgView = Views.permute( imgRaw, 2, 3 );

ImagePlus imp;
if (asVirtual) {
imp = ImageJFunctions.wrap(imgView, datasetMeta.getName(), exec);
} else {
final ImagePlusImg<T, ?> ipImg = new ImagePlusImgFactory<>(Util.getTypeFromInterval(imgView)).create(imgView);
LoopBuilder.setImages(imgView, ipImg)
.multiThreaded(new DefaultTaskExecutor(exec))
.forEachPixel((x, y) -> y.set(x));

imp = ipImg.getImagePlus();
}

return convertService.convert(imp, Dataset.class);
}

@SuppressWarnings({"rawtypes", "unchecked"})
private <T extends NumericType<T> & NativeType<T>> Dataset getImage(final N5Reader n5, final N5DatasetMetadata datasetMeta, final Metadata metadata) {
ExecutorService exec = Executors.newFixedThreadPool(8);

final CachedCellImg imgRaw = N5Utils.open(n5, datasetMeta.getPath());
//Images are stored as XYZCT and we permute to XYCZT which is ImagePlus axis order
//final RandomAccessibleInterval<T> imgView = Views.permute( imgRaw, 2, 3 );

AxisType[] axes = new AxisType[5];
axes[0] = Axes.X;
axes[1] = Axes.Y;
axes[2] = Axes.Z;
axes[3] = Axes.CHANNEL;
axes[4] = Axes.TIME;

final SCIFIOImgPlus<T> imgPlus = new SCIFIOImgPlus(imgRaw, datasetMeta.getName(), axes);
imgPlus.setMetadata(metadata);
imgPlus.setImageMetadata(metadata.get(0));

final Dataset dataset = datasetService.create((ImgPlus) imgPlus);

return dataset;


/*ImagePlus imp;
if (asVirtual) {
imp = ImageJFunctions.wrap(imgView, datasetMeta.getName(), exec);
} else {
final ImagePlusImg<T, ?> ipImg = new ImagePlusImgFactory<>(Util.getTypeFromInterval(imgView)).create(imgView);
LoopBuilder.setImages(imgView, ipImg)
.multiThreaded(new DefaultTaskExecutor(exec))
.forEachPixel((x, y) -> y.set(x));
imp = ipImg.getImagePlus();
}
return convertService.convert(imp, Dataset.class);
*/
}

private String getMetadataString(String url) {
return "";
Expand Down

0 comments on commit f1cb4c6

Please sign in to comment.