Skip to content

Commit cab6efb

Browse files
committed
add a few helpers related to pulling data from ZARR
1 parent 1d66b45 commit cab6efb

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
9+
### Added
10+
* Introduced more basic helper functions for reading from ZARR
11+
712
## [v0.5.0] - 2025-06-17
813

914
### Added

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ lazy val compileSettings = Def.settings(
189189
Test / console / scalacOptions := (Compile / console / scalacOptions).value,
190190
)
191191

192-
lazy val versionNumber = "0.5.0"
192+
lazy val versionNumber = "0.6.0-SNAPSHOT"
193193

194194
lazy val metadataSettings = Def.settings(
195195
name := projectName,

modules/zarr/src/main/scala/ZarrArrayExtras.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ import at.ac.oeaw.imba.gerlich.gerlib.zarr.OmeZarrIndex.OmeZarrStandardCoordinat
1010

1111
/** Helpers for working with [[com.bc.zarr.ZarrArray]] */
1212
object ZarrArrayExtras:
13+
14+
// TODO: guard against atypical dimension order.
15+
private def getXY(za: ZarrArray): Either[String, Dimensions2D] =
16+
za.getShape.toList match {
17+
case _ :: _ :: _ :: y :: x :: Nil => new Dimensions2D(x = x, y = y).asRight
18+
case dims => s"${dims.length}-D array, not 5-D".asLeft
19+
}
20+
21+
def readFirstFullSize2DFrom5D(za: ZarrArray): Either[String, DataRead2D] = for
22+
dims <- getXY(za)
23+
data <- Try{
24+
JzarrTools.readFrom(za, Array(1, 1, 1, dims.y, dims.x), Array(0, 0, 0, 0, 0))
25+
}.toEither.leftMap(_.getMessage)
26+
yield DataRead2D(data, dims)
27+
1328
extension (za: ZarrArray)
1429
def read(indexMapping: IndexMapping)(
1530
origin: OmeZarrStandardCoordinate,
@@ -23,4 +38,14 @@ object ZarrArrayExtras:
2338
.leftMap(e =>
2439
s"For index mapping $indexMapping and starting from $origin, failed to read block of size $size: ${e.getMessage}"
2540
)
41+
42+
final class Dimensions2D(val x: Int, val y: Int):
43+
require(x >= 0, s"x dimension must be nonnegative, not $x")
44+
require(y >= 0, s"y dimension must be nonnegative, not $y")
45+
46+
final class DataRead2D(values: Array[Int], dimensions: Dimensions2D):
47+
require(
48+
values.length === dimensions.x * dimensions.y,
49+
s"${dimensions.x} * ${dimensions.y} = ${dimensions.x * dimensions.y}, not ${values.length}"
50+
)
2651
end ZarrArrayExtras

0 commit comments

Comments
 (0)