From 443231c278d699f9ff22bf6e794dc3743ee1625f Mon Sep 17 00:00:00 2001 From: jmoore Date: Sun, 19 Sep 2021 17:15:10 +0200 Subject: [PATCH 1/2] Replace Doubles with Integers --- src/loci/formats/in/ZarrReader.java | 90 ++++++++++++++--------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/src/loci/formats/in/ZarrReader.java b/src/loci/formats/in/ZarrReader.java index 83d6052..d3356a6 100644 --- a/src/loci/formats/in/ZarrReader.java +++ b/src/loci/formats/in/ZarrReader.java @@ -11,12 +11,12 @@ * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public * License along with this program. If not, see * . @@ -72,7 +72,7 @@ public class ZarrReader extends FormatReader { private HashMap> resSeries = new HashMap>(); private HashMap resCounts = new HashMap(); private HashMap resIndexes = new HashMap(); - + private boolean hasSPW = false; public ZarrReader() { @@ -80,7 +80,7 @@ public ZarrReader() { suffixSufficient = false; domains = new String[] {FormatTools.UNKNOWN_DOMAIN}; } - + /* @see loci.formats.IFormatReader#isThisType(String, boolean) */ @Override public boolean isThisType(String name, boolean open) { @@ -90,7 +90,7 @@ public boolean isThisType(String name, boolean open) { } return super.isThisType(name, open); } - + /* @see loci.formats.IFormatReader#close() */ @Override public void close() throws IOException { @@ -110,14 +110,14 @@ public int getOptimalTileHeight() { FormatTools.assertId(currentId, true, 1); return zarrService.getChunkSize()[1]; } - + /* @see loci.formats.IFormatReader#getOptimalTileHeight() */ @Override public int getOptimalTileWidth() { FormatTools.assertId(currentId, true, 1); return zarrService.getChunkSize()[0]; } - + /* @see loci.formats.FormatReader#initFile(String) */ @Override protected void initFile(String id) throws FormatException, IOException { @@ -128,9 +128,9 @@ protected void initFile(String id) throws FormatException, IOException { String zarrRootPath = zarrPath.substring(0, zarrPath.indexOf(".zarr") + 5); String name = zarrRootPath.substring(zarrRootPath.lastIndexOf(File.separator)+1, zarrRootPath.length() - 5); Location omeMetaFile = new Location( zarrRootPath, name+".ome.xml" ); - + initializeZarrService(zarrRootPath); - + /* * Open OME metadata file * TODO: Old code to either be reworked with writer or removed entirely @@ -150,7 +150,7 @@ protected void initFile(String id) throws FormatException, IOException { } } omeDocument.getDocumentElement().normalize(); - + OMEXMLService service = null; String xml = null; try @@ -171,17 +171,17 @@ protected void initFile(String id) throws FormatException, IOException { { LOGGER.debug( "", e1 ); } - + int numDatasets = omexmlMeta.getImageCount(); - + int oldSeries = getSeries(); core.clear(); for (int i=0; i attributes = zarrService.getGroupAttr(zarrRootPath+File.separator+key); @@ -245,7 +245,7 @@ protected void initFile(String id) throws FormatException, IOException { try { jsonAttr = ZarrUtils.toJson(attributes, true); store.setXMLAnnotationValue(jsonAttr, attrIndex); - String xml_id = MetadataTools.createLSID("AttributesAnnotation:"+key, attrIndex); + String xml_id = MetadataTools.createLSID("AttributesAnnotation:"+key, attrIndex); store.setXMLAnnotationID(xml_id, attrIndex); } catch (JZarrException e) { LOGGER.warn("Failed to convert attributes to JSON"); @@ -253,7 +253,7 @@ protected void initFile(String id) throws FormatException, IOException { } } } - + // Parse array attributes for (String key: zarrService.getArrayKeys(zarrRootPath)) { Map attributes = zarrService.getArrayAttr(zarrRootPath+File.separator+key); @@ -263,7 +263,7 @@ protected void initFile(String id) throws FormatException, IOException { try { jsonAttr = ZarrUtils.toJson(attributes, true); store.setXMLAnnotationValue(jsonAttr, attrIndex); - String xml_id = MetadataTools.createLSID("AttributesAnnotation:"+key, attrIndex); + String xml_id = MetadataTools.createLSID("AttributesAnnotation:"+key, attrIndex); store.setXMLAnnotationID(xml_id, attrIndex); } catch (JZarrException e) { LOGGER.warn("Failed to convert attributes to JSON"); @@ -274,9 +274,9 @@ protected void initFile(String id) throws FormatException, IOException { arrayPaths = new ArrayList(); arrayPaths.addAll(zarrService.getArrayKeys(zarrRootPath)); - + orderArrayPaths(zarrRootPath); - + core.clear(); int resolutionTotal = 0; for (int i=0; i attr = zarrService.getGroupAttr(path); @@ -462,7 +462,7 @@ private void parseResolutionCount(String root, String key) throws IOException, F } } } - + private void parsePlate(String root, String key, MetadataStore store) throws IOException, FormatException { String path = key.isEmpty() ? root : root + File.separator + key; Map attr = zarrService.getGroupAttr(path); @@ -477,7 +477,7 @@ private void parsePlate(String root, String key, MetadataStore store) throws IOE ArrayList acquistions = (ArrayList )plates.get("acquisitions"); String plateName = (String) plates.get("name"); String fieldCount = (String) plates.get("filed_count"); - + String plate_id = MetadataTools.createLSID("Plate", p); store.setPlateID(plate_id, p); store.setPlateName(plateName, p); @@ -518,7 +518,7 @@ private void parsePlate(String root, String key, MetadataStore store) throws IOE } } } - + private int parseWells(String root, String key, MetadataStore store, int plateIndex, int wellIndex, int wellSamplesCount) throws IOException, FormatException { String path = key.isEmpty() ? root : root + File.separator + key; Map attr = zarrService.getGroupAttr(path); @@ -530,8 +530,8 @@ private int parseWells(String root, String key, MetadataStore store, int plateIn for (int i = 0; i < images.size(); i++) { Map image = (Map) images.get(i); String imagePath = (String) image.get("path"); - double acquisition = (double) image.get("acquisition"); - + double acquisition = (double) image.get("acquisition"); + String site_id = MetadataTools.createLSID("WellSample", wellSamplesCount); store.setWellSampleID(site_id, plateIndex, wellIndex, i); store.setWellSampleIndex(new NonNegativeInteger(i), plateIndex, wellIndex, i); @@ -543,7 +543,7 @@ private int parseWells(String root, String key, MetadataStore store, int plateIn } return wellSamplesCount; } - + private void parseLabels(String root, String key) throws IOException, FormatException { String path = key.isEmpty() ? root : root + File.separator + key; Map attr = zarrService.getGroupAttr(path); @@ -554,7 +554,7 @@ private void parseLabels(String root, String key) throws IOException, FormatExce } } } - + private void parseImageLabels(String root, String key) throws IOException, FormatException { String path = key.isEmpty() ? root : root + File.separator + key; Map attr = zarrService.getGroupAttr(path); @@ -593,7 +593,7 @@ private void parseImageLabels(String root, String key) throws IOException, Forma } } } - + private void parseOmeroMetadata(String root, String key) throws IOException, FormatException { String path = key.isEmpty() ? root : root + File.separator + key; Map attr = zarrService.getGroupAttr(path); @@ -613,10 +613,10 @@ private void parseOmeroMetadata(String root, String key) throws IOException, For String channelLabel = (String) channel.get("label"); Map window = (Map)channel.get("window"); if (window != null) { - Double windowStart = (Double) window.get("start"); - Double windowEnd = (Double) window.get("end"); - Double windowMin = (Double) window.get("min"); - Double windowMax = (Double) window.get("max"); + Integer windowStart = (Integer) window.get("start"); + Integer windowEnd = (Integer) window.get("end"); + Integer windowMin = (Integer) window.get("min"); + Integer windowMax = (Integer) window.get("max"); } } Map rdefs = (Map)omeroMetadata.get("rdefs"); @@ -627,11 +627,11 @@ private void parseOmeroMetadata(String root, String key) throws IOException, For } } } - + /* @see loci.formats.IFormatReader#getUsedFiles(boolean) */ @Override public String[] getUsedFiles(boolean noPixels) { - + FormatTools.assertId(currentId, true, 1); String zarrRootPath = currentId.substring(0, currentId.indexOf(".zarr") + 5); ArrayList usedFiles = new ArrayList(); @@ -654,7 +654,7 @@ public String[] getUsedFiles(boolean noPixels) { fileArr = usedFiles.toArray(fileArr); return fileArr; } - + /* @see loci.formats.SubResolutionFormatReader#getDomains() */ @Override public String[] getDomains() { From 8d7904bfc118024cbab2815452e365d8f7f91420 Mon Sep 17 00:00:00 2001 From: jmoore Date: Mon, 20 Sep 2021 15:27:03 +0200 Subject: [PATCH 2/2] Use Number.doubleValue to handle JSON parsing The JSON library sometimes returns a Double and sometimes an Integer. Since we expect this field to be a double, we forcefully cast. see: https://stackoverflow.com/questions/21965828/why-double-is-converted-to-int-in-json-string/21965943 --- src/loci/formats/in/ZarrReader.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/loci/formats/in/ZarrReader.java b/src/loci/formats/in/ZarrReader.java index d3356a6..2416b1e 100644 --- a/src/loci/formats/in/ZarrReader.java +++ b/src/loci/formats/in/ZarrReader.java @@ -613,10 +613,10 @@ private void parseOmeroMetadata(String root, String key) throws IOException, For String channelLabel = (String) channel.get("label"); Map window = (Map)channel.get("window"); if (window != null) { - Integer windowStart = (Integer) window.get("start"); - Integer windowEnd = (Integer) window.get("end"); - Integer windowMin = (Integer) window.get("min"); - Integer windowMax = (Integer) window.get("max"); + Double windowStart = getDouble(window, "start"); + Double windowEnd = getDouble(window, "end"); + Double windowMin = getDouble(window, "min"); + Double windowMax = getDouble(window, "max"); } } Map rdefs = (Map)omeroMetadata.get("rdefs"); @@ -628,6 +628,14 @@ private void parseOmeroMetadata(String root, String key) throws IOException, For } } + private Double getDouble(Map src, String key) { + Number val = (Number) src.get(key); + if (val == null) { + return null; + } + return val.doubleValue(); + } + /* @see loci.formats.IFormatReader#getUsedFiles(boolean) */ @Override public String[] getUsedFiles(boolean noPixels) {