diff --git a/src/loci/formats/in/ZarrReader.java b/src/loci/formats/in/ZarrReader.java index 86265db..bf8822f 100644 --- a/src/loci/formats/in/ZarrReader.java +++ b/src/loci/formats/in/ZarrReader.java @@ -1,4 +1,3 @@ - package loci.formats.in; /*- @@ -44,7 +43,6 @@ import java.util.Hashtable; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.stream.Stream; import javax.xml.parsers.ParserConfigurationException; @@ -68,8 +66,6 @@ import loci.formats.FormatReader; import loci.formats.FormatTools; import loci.formats.MetadataTools; -import loci.formats.in.DynamicMetadataOptions; -import loci.formats.in.MetadataOptions; import loci.formats.meta.MetadataStore; import loci.formats.ome.OMEXMLMetadata; import loci.formats.services.JZarrServiceImpl; @@ -77,8 +73,6 @@ import ome.xml.meta.MetadataRoot; import ome.xml.model.MapAnnotation; import ome.xml.model.OME; -import ome.xml.model.Plate; -import ome.xml.model.Screen; import ome.xml.model.StructuredAnnotations; import ome.xml.model.primitives.NonNegativeInteger; import ome.xml.model.primitives.PositiveInteger; @@ -205,7 +199,7 @@ protected void initFile(String id) throws FormatException, IOException { int attrIndex = 0; if (attr != null && !attr.isEmpty()) { parseResolutionCount(zarrRootPath, "", attr); - parseOmeroMetadata(zarrRootPath, attr); + parseOmeroMetadata(attr); if (saveAnnotations()) { String jsonAttr; try { @@ -903,7 +897,7 @@ private void parseImageLabels(String root, Map attr) throws IOEx for (int p = 0; p < properties.size(); p++) { Map prop = (Map) properties.get(p); Integer labelValue = (Integer) prop.get("label-value"); - Double area = (Double) prop.get("area (pixels)"); + Number area = (Number) prop.get("area (pixels)"); String propClass = (String) prop.get("class"); } } @@ -920,7 +914,7 @@ private void parseImageLabels(String root, Map attr) throws IOEx } } - private void parseOmeroMetadata(String root, Map attr) throws IOException, FormatException { + public void parseOmeroMetadata(Map attr) throws IOException, FormatException { Map omeroMetadata = (Map) attr.get("omero"); if (omeroMetadata != null) { Integer id = (Integer) omeroMetadata.get("id"); @@ -930,17 +924,17 @@ private void parseOmeroMetadata(String root, Map attr) throws IO for (int i = 0; i < channels.size(); i++) { Map channel = (Map) channels.get(i); Boolean channelActive = (Boolean) channel.get("active"); - Double channelCoefficient = (Double) channel.get("coefficient"); + Number channelCoefficient = (Number) channel.get("coefficient"); String channelColor = (String) channel.get("color"); String channelFamily = (String) channel.get("family"); Boolean channelInverted = (Boolean) channel.get("inverted"); String channelLabel = (String) channel.get("label"); Map window = (Map)channel.get("window"); if (window != null) { - Double windowStart = getDouble(window, "start"); - Double windowEnd = getDouble(window, "end"); - Double windowMin = getDouble(window, "min"); - Double windowMax = getDouble(window, "max"); + Number windowStart = getDouble(window, "start"); + Number windowEnd = getDouble(window, "end"); + Number windowMin = getDouble(window, "min"); + Number windowMax = getDouble(window, "max"); } } Map rdefs = (Map)omeroMetadata.get("rdefs"); @@ -1112,7 +1106,7 @@ public static String getRowString(int rowIndex) { return sb.reverse().toString(); } - private Double getDouble(Map src, String key) { + private Number getDouble(Map src, String key) { Number val = (Number) src.get(key); if (val == null) { return null; @@ -1125,6 +1119,7 @@ private Double getDouble(Map src, String key) { public String[] getUsedFiles(boolean noPixels) { FormatTools.assertId(currentId, true, 1); String zarrRootPath = currentId.substring(0, currentId.indexOf(".zarr") + 5); + int rootPathLength = zarrRootPath.length(); ArrayList usedFiles = new ArrayList(); reloadOptionsFile(zarrRootPath); @@ -1132,12 +1127,17 @@ public String[] getUsedFiles(boolean noPixels) { boolean includeLabels = includeLabels(); try (Stream paths = Files.walk(Paths.get(zarrRootPath), FileVisitOption.FOLLOW_LINKS)) { paths.filter(Files::isRegularFile) - .forEach(path -> {if ((!skipPixels && includeLabels) || - (!skipPixels && !includeLabels && !path.toString().toLowerCase().contains("labels")) || - (skipPixels && includeLabels && (path.endsWith(".zgroup") || path.endsWith(".zattrs") || path.endsWith(".xml"))) || - (skipPixels && !includeLabels && !path.toString().toLowerCase().contains("labels") &&(path.endsWith(".zgroup") || path.endsWith(".zattrs") || path.endsWith(".xml")))) - usedFiles.add(path.toFile().getAbsolutePath()); - }); + .forEach(path -> { + if ( + (!skipPixels && includeLabels) || + (!skipPixels && !includeLabels && (path.toString().toLowerCase().lastIndexOf("labels") omeroMetadata = new HashMap<>(); + omeroMetadata.put("id", 1); + omeroMetadata.put("name", "Test Image"); + omeroMetadata.put("version", "0.1"); + + ArrayList channels = new ArrayList<>(); + Map channel = new HashMap<>(); + channel.put("active", true); + channel.put("coefficient", 1); + channel.put("color", "FFFFFF"); + channel.put("family", "linear"); + channel.put("inverted", false); + channel.put("label", "Channel 1"); + + Map window = new HashMap<>(); + window.put("start", 0); + window.put("end", 255); + window.put("min", 0); + window.put("max", 255); + channel.put("window", window); + + channels.add(channel); + omeroMetadata.put("channels", channels); + + Map rdefs = new HashMap<>(); + rdefs.put("defaultT", 0); + rdefs.put("defaultZ", 0); + rdefs.put("model", "color"); + omeroMetadata.put("rdefs", rdefs); + + Map test = new HashMap<>(); + test.put("omero", omeroMetadata); + try { + reader.parseOmeroMetadata(test); + } catch (IOException | FormatException e) { + fail("Unexpected exception while parsing Omero metadata with Integer values"); + } + } + + @Test + public void testParseOmeroMetadataWithDoubleValues() { + Map omeroMetadata = new HashMap<>(); + omeroMetadata.put("id", 1); + omeroMetadata.put("name", "Test Image"); + omeroMetadata.put("version", "0.1"); + + ArrayList channels = new ArrayList<>(); + Map channel = new HashMap<>(); + channel.put("active", true); + channel.put("coefficient", 1.0); + channel.put("color", "FFFFFF"); + channel.put("family", "linear"); + channel.put("inverted", false); + channel.put("label", "Channel 1"); + + Map window = new HashMap<>(); + window.put("start", 0.0); + window.put("end", 255.0); + window.put("min", 0.0); + window.put("max", 255.0); + channel.put("window", window); + + channels.add(channel); + omeroMetadata.put("channels", channels); + + Map rdefs = new HashMap<>(); + rdefs.put("defaultT", 0); + rdefs.put("defaultZ", 0); + rdefs.put("model", "color"); + omeroMetadata.put("rdefs", rdefs); + + Map test = new HashMap<>(); + test.put("omero", omeroMetadata); + try { + reader.parseOmeroMetadata(test); + } catch (IOException | FormatException e) { + fail("Unexpected exception while parsing Omero metadata with Double values"); + } + } }