Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and updates #96

Merged
merged 4 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@

<!-- Java 8 Update Site -->
<imglib2-cache.version>1.0.0-beta-16</imglib2-cache.version>
<bigdataviewer-core.version>10.2.1</bigdataviewer-core.version>
<bigdataviewer-core.version>10.2.2</bigdataviewer-core.version>
<bigdataviewer-vistools.version>1.0.0-beta-28</bigdataviewer-vistools.version>

<!-- spim_data.version required for writing tests to run properly -->
Expand Down
55 changes: 41 additions & 14 deletions src/main/java/org/embl/mobie/io/SpimDataOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import bdv.spimdata.SpimDataMinimal;
import bdv.util.volatiles.SharedQueue;
import ij.IJ;
import lombok.extern.slf4j.Slf4j;
import mpicbg.spim.data.SpimData;
import mpicbg.spim.data.SpimDataException;
import mpicbg.spim.data.generic.AbstractSpimData;
import net.imglib2.util.Cast;
import org.embl.mobie.io.n5.openers.N5Opener;
import org.embl.mobie.io.n5.openers.N5S3Opener;
import org.embl.mobie.io.ome.zarr.loaders.N5S3OMEZarrImageLoader;
Expand All @@ -32,6 +34,7 @@
import static mpicbg.spim.data.XmlKeys.IMGLOADER_TAG;
import static mpicbg.spim.data.XmlKeys.SEQUENCEDESCRIPTION_TAG;

@Slf4j
public class SpimDataOpener {

public static final String ERROR_WHILE_TRYING_TO_READ_SPIM_DATA = "Error while trying to read spimData";
Expand Down Expand Up @@ -156,18 +159,39 @@ private SpimData openOpenOrganelleS3(String path) throws SpimDataException {
}
}

private SpimData openBdvOmeZarrS3(String path, SharedQueue queue) throws SpimDataException {
private SpimData openBdvOmeZarrS3(String path, SharedQueue queue) {
//Todo: finish bug fixing
try {
N5S3OMEZarrImageLoader imageLoader = createN5S3OmeZarrImageLoader(path, queue);
SpimData spimData = openBdvXml(path);
if (spimData != null) {
spimData.getSequenceDescription().setImgLoader(imageLoader);
return spimData;
SAXBuilder sax = new SAXBuilder();
InputStream stream = FileAndUrlUtils.getInputStream(path);
Document doc = sax.build(stream);
Element imgLoaderElem = doc.getRootElement().getChild("SequenceDescription").getChild("ImageLoader");
String bucketAndObject = imgLoaderElem.getChild("BucketName").getText() + "/" + imgLoaderElem.getChild("Key").getText();
String[] split = bucketAndObject.split("/");
String bucket = split[0];
String object = Arrays.stream(split).skip(1L).collect(Collectors.joining("/"));
N5S3OMEZarrImageLoader imageLoader;
if (queue != null) {
imageLoader = new N5S3OMEZarrImageLoader(imgLoaderElem.getChild("ServiceEndpoint").getText(), imgLoaderElem.getChild("SigningRegion").getText(), bucket, object, ".", queue);
} else {
throw new SpimDataException("Error while trying to read spimData. SpimData is null");
imageLoader = new N5S3OMEZarrImageLoader(imgLoaderElem.getChild("ServiceEndpoint").getText(), imgLoaderElem.getChild("SigningRegion").getText(), bucket, object, ".");
}
} catch (IOException | JDOMException e) {
throw new SpimDataException(ERROR_WHILE_TRYING_TO_READ_SPIM_DATA + e.getMessage());
SpimData spim = new SpimData(null, Cast.unchecked(imageLoader.getSequenceDescription()), imageLoader.getViewRegistrations());
SpimData sp1;
try {
KateMoreva marked this conversation as resolved.
Show resolved Hide resolved
InputStream st = FileAndUrlUtils.getInputStream(path);
sp1 = (new CustomXmlIoSpimData()).loadFromStream(st, path);
} catch (SpimDataException exception) {
log.debug("Failed to load stream from {}", path, exception);
return null;
}
sp1.setBasePath(null);
sp1.getSequenceDescription().setImgLoader(spim.getSequenceDescription().getImgLoader());
sp1.getSequenceDescription().getAllChannels().putAll(spim.getSequenceDescription().getAllChannels());
return sp1;
} catch (JDOMException | IOException var13) {
var13.printStackTrace();
KateMoreva marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
}

Expand Down Expand Up @@ -207,12 +231,15 @@ private SpimData getSpimDataWithImageLoader(String path, @Nullable SharedQueue s
final Document doc = sax.build(stream);
final Element imgLoaderElem = doc.getRootElement().getChild(SEQUENCEDESCRIPTION_TAG).getChild(IMGLOADER_TAG);
String imagesFile = XmlN5OmeZarrImageLoader.getDatasetsPathFromXml(imgLoaderElem, path);
if (imagesFile != null && new File(imagesFile).exists()) {
if (sharedQueue != null) {
return OMEZarrOpener.openFile(path, sharedQueue);
if (imagesFile != null) {
if (new File(imagesFile).exists()) {
return sharedQueue != null ? OMEZarrOpener.openFile(imagesFile, sharedQueue)
: OMEZarrOpener.openFile(imagesFile);
} else {
return sharedQueue != null ? OMEZarrS3Opener.readURL(imagesFile, sharedQueue)
: OMEZarrS3Opener.readURL(imagesFile);
}
}
return OMEZarrOpener.openFile(imagesFile);
}
} catch (JDOMException | IOException e) {
IJ.log(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private static class N5CacheArrayLoader<A> implements SimpleCacheArrayLoader<A>
}

@Override
public A loadArray(final long[] gridPosition) throws IOException {
public A loadArray(final long[] gridPosition, final int[] cellDimensions) throws IOException {
DataBlock<?> block = null;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public N5CacheArrayLoader(final N5Reader n5, final String pathName, final Datase
}

@Override
public A loadArray(final long[] gridPosition) {
public A loadArray(final long[] gridPosition, final int[] cellDimensions) {
DataBlock<?> block = null;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public RandomAccessibleInterval<T> getImage(final int level) throws IOException
final long[] cellGridPosition = new long[n];
grid.getCellDimensions(key, cellMin, cellDims);
grid.getCellGridPositionFlat(key, cellGridPosition);
return new Cell<>(cellDims, cellMin, cacheArrayLoader.loadArray(cellGridPosition));
return new Cell<>(cellDims, cellMin, cacheArrayLoader.loadArray(cellGridPosition, cellDims));
},
options().cellDimensions(cellDimensions));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ private OmeZarrMultiscales[] getMultiscale(String pathName) throws IOException {
location += "; container path: " + s3ZarrReader.getContainerPath();
location += "; path: " + pathName;
location += "; attribute: " + MULTI_SCALE_KEY;
} else {
location += " path: " + pathName;
}
throw new UnsupportedOperationException("Could not find multiscales at " + location);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
package org.embl.mobie.io.ome.zarr.readers;

import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -266,18 +267,15 @@ public DataBlock<?> readBlock(
*
* @param objectPath
* @return null if the object does not exist, otherwise the loaded attributes.
* @throws IOException
*/
public HashMap<String, JsonElement> readJson(String objectPath) throws IOException {
if (!this.s3.doesObjectExist(this.bucketName, objectPath)) {
return null;
} else {
public HashMap<String, JsonElement> readJson(String objectPath) {
try {
InputStream in = this.readS3Object(objectPath);
Throwable var4 = null;
KateMoreva marked this conversation as resolved.
Show resolved Hide resolved

HashMap<String, JsonElement> var5;
HashMap<String, JsonElement> hashMap;
try {
var5 = GsonAttributesParser.readAttributes(new InputStreamReader(in), this.gson);
hashMap = GsonAttributesParser.readAttributes(new InputStreamReader(in), this.gson);
} catch (Throwable var14) {
KateMoreva marked this conversation as resolved.
Show resolved Hide resolved
var4 = var14;
throw var14;
Expand All @@ -295,8 +293,10 @@ public HashMap<String, JsonElement> readJson(String objectPath) throws IOExcepti
}

}

return var5;
return hashMap;
} catch (SdkClientException | IOException e) {
log.debug("Failed to readJson: object {} does not exist in bucket {}", objectPath, this.bucketName);
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public N5OMEZarrCacheArrayLoader(final N5Reader n5, final String pathName, final
}

@Override
public A loadArray(final long[] gridPosition) throws IOException {
public A loadArray(final long[] gridPosition, int[] cellDimensions) throws IOException {
DataBlock<?> block = null;

long[] dataBlockIndices = toDataBlockIndices(gridPosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public RandomAccessibleInterval<T> getImage(final int level) throws IOException
final long[] cellGridPosition = new long[n];
grid.getCellDimensions(key, cellMin, cellDims);
grid.getCellGridPositionFlat(key, cellGridPosition);
return new Cell<>(cellDims, cellMin, cacheArrayLoader.loadArray(cellGridPosition));
return new Cell<>(cellDims, cellMin, cacheArrayLoader.loadArray(cellGridPosition, cellDims));
},
options().cellDimensions(cellDimensions));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class N5CacheArrayLoader<A> implements SimpleCacheArrayLoader<A> {
}

@Override
public A loadArray(final long[] gridPosition) {
public A loadArray(final long[] gridPosition, final int[] cellDimensions) {
DataBlock<?> block = null;

try {
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/embl/mobie/io/util/CustomXmlIoSpimData.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,26 @@

import java.io.File;
import java.io.InputStream;
import java.util.Objects;

import static mpicbg.spim.data.XmlKeys.SPIMDATA_TAG;


public class CustomXmlIoSpimData extends XmlIoAbstractSpimData<SequenceDescription, SpimData> {
public CustomXmlIoSpimData() {
super(SpimData.class, new XmlIoSequenceDescription(), new XmlIoViewRegistrations());
}

// NOTE we still need to pass the xml filename here, so that bdv can determine the relative
// paths for local files.
public SpimData loadFromStream(InputStream in, String xmlFilename) throws SpimDataException {
final SAXBuilder sax = new SAXBuilder();
SAXBuilder sax = new SAXBuilder();

Document doc;
try {
doc = sax.build(in);
} catch (final Exception e) {
throw new SpimDataIOException(e);
} catch (Exception exception) {
throw new SpimDataIOException(exception);
}
final Element root = doc.getRootElement();

if (root.getName() != SPIMDATA_TAG)
if (!root.getName().equals(SPIMDATA_TAG))
throw new RuntimeException("expected <" + SPIMDATA_TAG + "> root element. wrong file?");

return fromXml(root, new File(xmlFilename));
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/ui/BdvOmeZarrOpener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ui;

import bdv.util.BdvFunctions;
import bdv.util.volatiles.SharedQueue;
import mpicbg.spim.data.SpimData;
import mpicbg.spim.data.SpimDataException;
import org.embl.mobie.io.ImageDataFormat;
import org.embl.mobie.io.SpimDataOpener;

public class BdvOmeZarrOpener {
public static void main(String[] args) {
showProject();
}
public static void showProject() {
SharedQueue sharedQueue = new SharedQueue( 7 );
SpimDataOpener spimDataOpener = new SpimDataOpener();
SpimData image = null;
try {
// image =(SpimData) spimDataOpener.openSpimData("https://raw.githubusercontent.com/mobie/clem-example-project//more-views/data/hela/images/bdv-n5-s3/fluorescence-a2-FMR-c2.xml",
// ImageDataFormat.BdvN5S3, sharedQueue);
image = (SpimData) spimDataOpener.openSpimData("https://s3.embl.de/i2k-2020/project-bdv-ome-zarr/Covid19-S4-Area2/images/bdv.ome.zarr.s3/raw.xml",
ImageDataFormat.BdvOmeZarrS3, sharedQueue);
} catch (SpimDataException e) {
e.printStackTrace();
}
assert image != null;
BdvFunctions.show(image);
}
}