From aadfe011b75c85e3bf943d997673033056d16311 Mon Sep 17 00:00:00 2001 From: Mark Keller <7525285+keller-mark@users.noreply.github.com> Date: Tue, 7 May 2024 08:29:04 -0400 Subject: [PATCH] zarr_open --- NAMESPACE | 1 + R/creation.R | 48 +++++++++++++++++++++++++++++++++++- man/zarr_open.Rd | 29 ++++++++++++++++++++++ pkgdown/_pkgdown.yml | 1 + tests/testthat/test-compat.R | 10 ++++++++ 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 man/zarr_open.Rd diff --git a/NAMESPACE b/NAMESPACE index 557d5b8..ed7c1f6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -33,6 +33,7 @@ export(zarr_create_array) export(zarr_create_empty) export(zarr_create_group) export(zarr_create_zeros) +export(zarr_open) export(zarr_open_array) export(zarr_open_group) export(zarr_save_array) diff --git a/R/creation.R b/R/creation.R index 60f6690..06f0fe9 100644 --- a/R/creation.R +++ b/R/creation.R @@ -790,4 +790,50 @@ zarr_save_array <- function(store, arr, ...) { # Reference: https://github.com/zarr-developers/zarr-python/blob/5dd4a0e6cdc04c6413e14f57f61d389972ea937c/zarr/convenience.py#L112 store <- normalize_store_arg(store) zarr_create_array(data=arr$get_item("..."), shape=arr$get_shape(), store=store, ...) -} \ No newline at end of file +} + +#' Convenience function to open a group or array using file-mode-like semantics. +#' @param store : MutableMapping or string, optional +#' Store or path to directory in file system or name of zip file. +#' @param mode : {'r', 'r+', 'a', 'w', 'w-'}, optional +#' Persistence mode: 'r' means read only (must exist); 'r+' means +#' read/write (must exist); 'a' means read/write (create if doesn't +#' exist); 'w' means create (overwrite if exists); 'w-' means create +#' (fail if exists). +#' @param path : str or NA, optional +#' The path within the store to open. +#' @param ... Additional arguments to pass to zarr_open_array or zarr_open_group. +#' @returns ZarrArray or ZarrGroup +#' @export +zarr_open <- function(store = NA, mode = NA, path = NA, ...) { + kwargs <- list(...) + + if(is_na(mode)) { + mode <- "a" + } + + store <- normalize_store_arg(store) + path <- normalize_storage_path(path) + + if(mode %in% c("w", "w-", "x")) { + if("shape" %in% names(kwargs)) { + return(zarr_open_array(store=store, mode=mode, path=path, ...)) + } else { + return(zarr_open_group(store=store, mode=mode, path=path, ...)) + } + } else if(mode == "a") { + if("shape" %in% names(kwargs) || contains_array(store, path)) { + return(zarr_open_array(store=store, mode=mode, path=path, ...)) + } else { + return(zarr_open_group(store=store, mode=mode, path=path, ...)) + } + } else { + if(contains_array(store, path)) { + return(zarr_open_array(store=store, mode=mode, path=path, ...)) + } else if(contains_group(store, path)) { + return(zarr_open_group(store=store, mode=mode, path=path, ...)) + } else { + stop("PathNotFoundError(path)") + } + } +} diff --git a/man/zarr_open.Rd b/man/zarr_open.Rd new file mode 100644 index 0000000..8133590 --- /dev/null +++ b/man/zarr_open.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/creation.R +\name{zarr_open} +\alias{zarr_open} +\title{Convenience function to open a group or array using file-mode-like semantics.} +\usage{ +zarr_open(store = NA, mode = NA, path = NA, ...) +} +\arguments{ +\item{store}{: MutableMapping or string, optional +Store or path to directory in file system or name of zip file.} + +\item{mode}{: {'r', 'r+', 'a', 'w', 'w-'}, optional +Persistence mode: 'r' means read only (must exist); 'r+' means +read/write (must exist); 'a' means read/write (create if doesn't +exist); 'w' means create (overwrite if exists); 'w-' means create +(fail if exists).} + +\item{path}{: str or NA, optional +The path within the store to open.} + +\item{...}{Additional arguments to pass to zarr_open_array or zarr_open_group.} +} +\value{ +ZarrArray or ZarrGroup +} +\description{ +Convenience function to open a group or array using file-mode-like semantics. +} diff --git a/pkgdown/_pkgdown.yml b/pkgdown/_pkgdown.yml index 4bcf255..357781c 100644 --- a/pkgdown/_pkgdown.yml +++ b/pkgdown/_pkgdown.yml @@ -62,6 +62,7 @@ reference: - zarr_create_zeros - zarr_create_array - zarr_create_group + - zarr_open - zarr_open_group - zarr_open_array - zarr_save_array diff --git a/tests/testthat/test-compat.R b/tests/testthat/test-compat.R index 1a6db28..1031e3f 100644 --- a/tests/testthat/test-compat.R +++ b/tests/testthat/test-compat.R @@ -10,6 +10,16 @@ test_that("Can open Zarr group using convenience function", { expect_equal(a$get_shape(), c(4)) }) +test_that("Can open Zarr group or array using convenience function", { + + root <- system.file("extdata", "fixtures", "v2", "data.zarr", package="pizzarr") + g <- zarr_open(root) + a <- zarr_open(root, path="1d.contiguous.lz4.i2") + + expect_equal(class(g)[1], "ZarrGroup") + expect_equal(class(a)[1], "ZarrArray") +}) + test_that("Can open Zarr group and read a 1D 2-byte integer array with LZ4 compression", { root <- system.file("extdata", "fixtures", "v2", "data.zarr", package="pizzarr")