diff --git a/build.gradle b/build.gradle index fd9fe25..1bdd473 100644 --- a/build.gradle +++ b/build.gradle @@ -25,9 +25,6 @@ repositories { maven { url 'https://artifacts.glencoesoftware.com/artifactory/scijava-thirdparty' } - maven { - url 'https://artifacts.glencoesoftware.com/artifactory/jzarr-releases/' - } maven { url 'https://repo.glencoesoftware.com/repository/bioformats2raw2ometiff/' } @@ -36,7 +33,7 @@ repositories { dependencies { api 'org.openmicroscopy:omero-blitz:5.6.2' implementation 'com.github.ben-manes.caffeine:caffeine:3.1.8' - implementation 'com.bc.zarr:jzarr:0.3.5' + implementation 'dev.zarr:jzarr:0.4.2' implementation 'org.lasersonlab:s3fs:2.2.3' implementation 'com.amazonaws:aws-java-sdk-s3:1.12.659' diff --git a/src/test/java/com/glencoesoftware/omero/ms/core/AbstractZarrPixelBufferTest.java b/src/test/java/com/glencoesoftware/omero/ms/core/AbstractZarrPixelBufferTest.java deleted file mode 100644 index 7590c10..0000000 --- a/src/test/java/com/glencoesoftware/omero/ms/core/AbstractZarrPixelBufferTest.java +++ /dev/null @@ -1,197 +0,0 @@ -/* - * Copyright (C) 2021 Glencoe Software, Inc. All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * 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, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package com.glencoesoftware.omero.ms.core; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Rule; -import org.junit.rules.TemporaryFolder; - -import com.bc.zarr.ZarrGroup; -import com.glencoesoftware.bioformats2raw.Converter; - -import picocli.CommandLine; - -public abstract class AbstractZarrPixelBufferTest { - - @Rule - public TemporaryFolder tmpDir = new TemporaryFolder(); - - /** - * Run the bioformats2raw main method and check for success or failure. - * - * @param additionalArgs CLI arguments as needed beyond "input output" - */ - void assertBioFormats2Raw(Path input, Path output, String...additionalArgs) - throws IOException { - List args = new ArrayList( - Arrays.asList(new String[] { "--compression", "null" })); - for (String arg : additionalArgs) { - args.add(arg); - } - args.add(input.toString()); - args.add(output.toString()); - try { - Converter converter = new Converter(); - CommandLine cli = new CommandLine(converter); - cli.execute(args.toArray(new String[]{})); - Assert.assertTrue(Files.exists(output.resolve(".zattrs"))); - Assert.assertTrue(Files.exists( - output.resolve("OME").resolve("METADATA.ome.xml"))); - } catch (RuntimeException rt) { - throw rt; - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - - static Path fake(String...args) { - Assert.assertTrue(args.length %2 == 0); - Map options = new HashMap(); - for (int i = 0; i < args.length; i += 2) { - options.put(args[i], args[i+1]); - } - return fake(options); - } - - static Path fake(Map options) { - return fake(options, null); - } - - /** - * Create a Bio-Formats fake INI file to use for testing. - * @param options map of the options to assign as part of the fake filename - * from the allowed keys - * @param series map of the integer series index and options map (same format - * as options to add to the fake INI content - * @see https://docs.openmicroscopy.org/bio-formats/6.4.0/developers/ - * generating-test-images.html#key-value-pairs - * @return path to the fake INI file that has been created - */ - static Path fake(Map options, - Map> series) - { - return fake(options, series, null); - } - - static Path fake(Map options, - Map> series, - Map originalMetadata) - { - StringBuilder sb = new StringBuilder(); - sb.append("image"); - if (options != null) { - for (Map.Entry kv : options.entrySet()) { - sb.append("&"); - sb.append(kv.getKey()); - sb.append("="); - sb.append(kv.getValue()); - } - } - sb.append("&"); - try { - List lines = new ArrayList(); - if (originalMetadata != null) { - lines.add("[GlobalMetadata]"); - for (String key : originalMetadata.keySet()) { - lines.add(String.format( - "%s=%s", key, originalMetadata.get(key))); - } - } - if (series != null) { - for (int s : series.keySet()) { - Map seriesOptions = series.get(s); - lines.add(String.format("[series_%d]", s)); - for (String key : seriesOptions.keySet()) { - lines.add(String.format( - "%s=%s", key, seriesOptions.get(key))); - } - } - } - Path ini = Files.createTempFile(sb.toString(), ".fake.ini"); - File iniAsFile = ini.toFile(); - String iniPath = iniAsFile.getAbsolutePath(); - String fakePath = iniPath.substring(0, iniPath.length() - 4); - Path fake = Paths.get(fakePath); - File fakeAsFile = fake.toFile(); - Files.write(fake, new byte[]{}); - Files.write(ini, lines); - iniAsFile.deleteOnExit(); - fakeAsFile.deleteOnExit(); - return ini; - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public Path writeTestZarr( - int sizeT, - int sizeC, - int sizeZ, - int sizeY, - int sizeX, - String pixelType, - int resolutions) throws IOException { - Path input = fake( - "sizeT", Integer.toString(sizeT), - "sizeC", Integer.toString(sizeC), - "sizeZ", Integer.toString(sizeZ), - "sizeY", Integer.toString(sizeY), - "sizeX", Integer.toString(sizeX), - "pixelType", pixelType, - "resolutions", Integer.toString(resolutions)); - Path output = tmpDir.getRoot().toPath().resolve("output.zarr"); - assertBioFormats2Raw(input, output); - List msArray = new ArrayList<>(); - Map msData = new HashMap<>(); - Map msMetadata = new HashMap<>(); - msMetadata.put("method", "loci.common.image.SimpleImageScaler"); - msMetadata.put("version", "Bio-Formats 6.5.1"); - msData.put("metadata", msMetadata); - msData.put("datasets", getDatasets(resolutions)); - msData.put("version", "0.1"); - msArray.add(msData); - ZarrGroup z = ZarrGroup.open(output.resolve("0")); - Map attrs = new HashMap(); - attrs.put("multiscales", msArray); - z.writeAttributes(attrs); - return output; - } - - List> getDatasets(int resolutions) { - List> datasets = new ArrayList<>(); - for (int i = 0; i < resolutions; i++) { - Map resObj = new HashMap<>(); - resObj.put("path", Integer.toString(i)); - datasets.add(resObj); - } - return datasets; - } - -} diff --git a/src/test/java/com/glencoesoftware/omero/ms/core/ZarrPixelBufferTest.java b/src/test/java/com/glencoesoftware/omero/ms/core/ZarrPixelBufferTest.java index b4dc861..19e7308 100644 --- a/src/test/java/com/glencoesoftware/omero/ms/core/ZarrPixelBufferTest.java +++ b/src/test/java/com/glencoesoftware/omero/ms/core/ZarrPixelBufferTest.java @@ -19,23 +19,33 @@ package com.glencoesoftware.omero.ms.core; import java.awt.Dimension; +import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.IntStream; import org.junit.Assert; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; import org.junit.Test; import com.bc.zarr.ZarrArray; +import com.bc.zarr.ZarrGroup; import com.github.benmanes.caffeine.cache.Caffeine; +import picocli.CommandLine; +import com.glencoesoftware.bioformats2raw.Converter; + import loci.formats.FormatTools; import loci.formats.in.FakeReader; import ome.io.nio.DimensionsOutOfBoundsException; @@ -46,7 +56,10 @@ import ucar.ma2.DataType; import ucar.ma2.InvalidRangeException; -public class ZarrPixelBufferTest extends AbstractZarrPixelBufferTest { +public class ZarrPixelBufferTest { + + @Rule + public TemporaryFolder tmpDir = new TemporaryFolder(); public ZarrPixelBuffer createPixelBuffer( Pixels pixels, Path path, @@ -62,6 +75,157 @@ public ZarrPixelBuffer createPixelBuffer( ); } + /** + * Run the bioformats2raw main method and check for success or failure. + * + * @param additionalArgs CLI arguments as needed beyond "input output" + */ + void assertBioFormats2Raw(Path input, Path output, String...additionalArgs) + throws IOException { + List args = new ArrayList( + Arrays.asList(new String[] { "--compression", "null" })); + for (String arg : additionalArgs) { + args.add(arg); + } + args.add(input.toString()); + args.add(output.toString()); + try { + Converter converter = new Converter(); + CommandLine cli = new CommandLine(converter); + cli.execute(args.toArray(new String[]{})); + Assert.assertTrue(Files.exists(output.resolve(".zattrs"))); + Assert.assertTrue(Files.exists( + output.resolve("OME").resolve("METADATA.ome.xml"))); + } catch (RuntimeException rt) { + throw rt; + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + + static Path fake(String...args) { + Assert.assertTrue(args.length %2 == 0); + Map options = new HashMap(); + for (int i = 0; i < args.length; i += 2) { + options.put(args[i], args[i+1]); + } + return fake(options); + } + + static Path fake(Map options) { + return fake(options, null); + } + + /** + * Create a Bio-Formats fake INI file to use for testing. + * @param options map of the options to assign as part of the fake filename + * from the allowed keys + * @param series map of the integer series index and options map (same format + * as options to add to the fake INI content + * @see https://docs.openmicroscopy.org/bio-formats/6.4.0/developers/ + * generating-test-images.html#key-value-pairs + * @return path to the fake INI file that has been created + */ + static Path fake(Map options, + Map> series) + { + return fake(options, series, null); + } + + static Path fake(Map options, + Map> series, + Map originalMetadata) + { + StringBuilder sb = new StringBuilder(); + sb.append("image"); + if (options != null) { + for (Map.Entry kv : options.entrySet()) { + sb.append("&"); + sb.append(kv.getKey()); + sb.append("="); + sb.append(kv.getValue()); + } + } + sb.append("&"); + try { + List lines = new ArrayList(); + if (originalMetadata != null) { + lines.add("[GlobalMetadata]"); + for (String key : originalMetadata.keySet()) { + lines.add(String.format( + "%s=%s", key, originalMetadata.get(key))); + } + } + if (series != null) { + for (int s : series.keySet()) { + Map seriesOptions = series.get(s); + lines.add(String.format("[series_%d]", s)); + for (String key : seriesOptions.keySet()) { + lines.add(String.format( + "%s=%s", key, seriesOptions.get(key))); + } + } + } + Path ini = Files.createTempFile(sb.toString(), ".fake.ini"); + File iniAsFile = ini.toFile(); + String iniPath = iniAsFile.getAbsolutePath(); + String fakePath = iniPath.substring(0, iniPath.length() - 4); + Path fake = Paths.get(fakePath); + File fakeAsFile = fake.toFile(); + Files.write(fake, new byte[]{}); + Files.write(ini, lines); + iniAsFile.deleteOnExit(); + fakeAsFile.deleteOnExit(); + return ini; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public Path writeTestZarr( + int sizeT, + int sizeC, + int sizeZ, + int sizeY, + int sizeX, + String pixelType, + int resolutions) throws IOException { + Path input = fake( + "sizeT", Integer.toString(sizeT), + "sizeC", Integer.toString(sizeC), + "sizeZ", Integer.toString(sizeZ), + "sizeY", Integer.toString(sizeY), + "sizeX", Integer.toString(sizeX), + "pixelType", pixelType, + "resolutions", Integer.toString(resolutions)); + Path output = tmpDir.getRoot().toPath().resolve("output.zarr"); + assertBioFormats2Raw(input, output); + List msArray = new ArrayList<>(); + Map msData = new HashMap<>(); + Map msMetadata = new HashMap<>(); + msMetadata.put("method", "loci.common.image.SimpleImageScaler"); + msMetadata.put("version", "Bio-Formats 6.5.1"); + msData.put("metadata", msMetadata); + msData.put("datasets", getDatasets(resolutions)); + msData.put("version", "0.1"); + msArray.add(msData); + ZarrGroup z = ZarrGroup.open(output.resolve("0")); + Map attrs = new HashMap(); + attrs.put("multiscales", msArray); + z.writeAttributes(attrs); + return output; + } + + List> getDatasets(int resolutions) { + List> datasets = new ArrayList<>(); + for (int i = 0; i < resolutions; i++) { + Map resObj = new HashMap<>(); + resObj.put("path", Integer.toString(i)); + datasets.add(resObj); + } + return datasets; + } + @Test public void testGetChunks() throws IOException { int sizeT = 1;