From cab6efb3a0e61c73ca615b6cc748a8fc1d1159b8 Mon Sep 17 00:00:00 2001 From: Vince Reuter Date: Fri, 27 Jun 2025 10:20:02 +0200 Subject: [PATCH] add a few helpers related to pulling data from ZARR --- CHANGELOG.md | 5 ++++ build.sbt | 2 +- .../zarr/src/main/scala/ZarrArrayExtras.scala | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fecbfa4..611ec9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added +* Introduced more basic helper functions for reading from ZARR + ## [v0.5.0] - 2025-06-17 ### Added diff --git a/build.sbt b/build.sbt index d41260f..61d8a9c 100644 --- a/build.sbt +++ b/build.sbt @@ -189,7 +189,7 @@ lazy val compileSettings = Def.settings( Test / console / scalacOptions := (Compile / console / scalacOptions).value, ) -lazy val versionNumber = "0.5.0" +lazy val versionNumber = "0.6.0-SNAPSHOT" lazy val metadataSettings = Def.settings( name := projectName, diff --git a/modules/zarr/src/main/scala/ZarrArrayExtras.scala b/modules/zarr/src/main/scala/ZarrArrayExtras.scala index 195a8cd..0b8bbc3 100644 --- a/modules/zarr/src/main/scala/ZarrArrayExtras.scala +++ b/modules/zarr/src/main/scala/ZarrArrayExtras.scala @@ -10,6 +10,21 @@ import at.ac.oeaw.imba.gerlich.gerlib.zarr.OmeZarrIndex.OmeZarrStandardCoordinat /** Helpers for working with [[com.bc.zarr.ZarrArray]] */ object ZarrArrayExtras: + + // TODO: guard against atypical dimension order. + private def getXY(za: ZarrArray): Either[String, Dimensions2D] = + za.getShape.toList match { + case _ :: _ :: _ :: y :: x :: Nil => new Dimensions2D(x = x, y = y).asRight + case dims => s"${dims.length}-D array, not 5-D".asLeft + } + + def readFirstFullSize2DFrom5D(za: ZarrArray): Either[String, DataRead2D] = for + dims <- getXY(za) + data <- Try{ + JzarrTools.readFrom(za, Array(1, 1, 1, dims.y, dims.x), Array(0, 0, 0, 0, 0)) + }.toEither.leftMap(_.getMessage) + yield DataRead2D(data, dims) + extension (za: ZarrArray) def read(indexMapping: IndexMapping)( origin: OmeZarrStandardCoordinate, @@ -23,4 +38,14 @@ object ZarrArrayExtras: .leftMap(e => s"For index mapping $indexMapping and starting from $origin, failed to read block of size $size: ${e.getMessage}" ) + + final class Dimensions2D(val x: Int, val y: Int): + require(x >= 0, s"x dimension must be nonnegative, not $x") + require(y >= 0, s"y dimension must be nonnegative, not $y") + + final class DataRead2D(values: Array[Int], dimensions: Dimensions2D): + require( + values.length === dimensions.x * dimensions.y, + s"${dimensions.x} * ${dimensions.y} = ${dimensions.x * dimensions.y}, not ${values.length}" + ) end ZarrArrayExtras