6161# '
6262# ' @export
6363SpatialDataAttrs <- \(x , type = c(" image" , " label" , " frame" ),
64- trans = NULL , ver = " 0.4 " , dim = 2 , nch = 3 , ... )
64+ trans = NULL , ver = " 0.3 " , dim = 2 , nch = 3 , ... )
6565{
6666 stopifnot(
6767 length(dim ) == 1 , is.numeric(dim ), dim %in% seq(2 , 4 ),
@@ -72,16 +72,29 @@ SpatialDataAttrs <- \(x, type=c("image", "label", "frame"),
7272 ax <- .default_ax(type , dim )
7373 # transformations:
7474 ct <- trans %|| % .default_ct(ax )
75+ # datasets:
76+ ds <- .default_ds(.ax_names(ax ))
7577 # .zattrs list:
7678 if (type != " frame" ) {
7779 # default structure
78- res <- list (
79- omero = list (channels = list (label = letters [seq_len(nch )])),
80- multiscales = list (list (
81- axes = ax ,
82- version = " 0.4" ,
83- coordinateTransformations = ct ,
84- datasets = list (list (path = " 0" , coordinateTransformations = list (list (type = " scale" , scale = list (1 , 1 ))))))))
80+ res <- list ()
81+ if (type != " label" )
82+ res <- c(res ,
83+ list (omero = list (channels = lapply(letters [seq_len(nch )],
84+ \(. ) list (label = . )))))
85+ res <- c(res ,
86+ list (
87+ version = .get_ome_version(ver ),
88+ multiscales =
89+ list (
90+ list (
91+ axes = ax ,
92+ coordinateTransformations = ct ,
93+ datasets = ds
94+ )
95+ )
96+ )
97+ )
8598 if (ver == " 0.3" ) res <- list (ome = res )
8699 } else {
87100 # points/shapes
@@ -120,13 +133,41 @@ SpatialDataAttrs <- \(x, type=c("image", "label", "frame"),
120133 return (ax )
121134}
122135
136+ # Internal helper to get axes names
137+ .ax_names <- function (ax ){
138+ if (is.character(ax [[1 ]])) {
139+ unlist(ax )
140+ } else {
141+ vapply(ax , \(. ) . $ name , character (1 ))
142+ }
143+ }
144+
123145# Internal helper to generate coordinate transformations
124146.default_ct <- \(axes , name = " global" , type = " identity" , data = NULL ) {
125147 ct <- list (input = axes , output = list (name = name ), type = type )
126148 if (! is.null(data )) ct [[type ]] <- data
127149 list (ct )
128150}
129151
152+ # Internal helper to generate datasets
153+ .default_ds <- function (axes , scale_factors = NULL ){
154+ scale_factors <- cumprod(c(1 ,scale_factors ))
155+ paths <- paste0(seq_along(scale_factors ) - 1 )
156+ mapply(\(p ,s ) {
157+ list (
158+ coordinateTransformations = list (
159+ list (
160+ scale = lapply(
161+ axes ,
162+ \(. ) if (. == " c" ) 1 else s ),
163+ type = " scale"
164+ )
165+ ),
166+ path = p
167+ )
168+ }, paths , scale_factors , USE.NAMES = FALSE , SIMPLIFY = FALSE )
169+ }
170+
130171# ' @export
131172# ' @importFrom utils .DollarNames
132173.DollarNames.SpatialDataAttrs <- \(x , pattern = " " ) names(x )
@@ -147,6 +188,15 @@ setMethod("$", "SpatialDataAttrs", \(x, name) x[[name]])
147188 return (v )
148189}
149190
191+ .get_ome_version <- \(x ){
192+ switch (as.character(x ),
193+ " 0.1" = " 0.4" ,
194+ " 0.2" = " 0.4-dev-spatialdata" ,
195+ " 0.3" = " 0.5-dev-spatialdata" ,
196+ stop(" Invalid SpatialDataImage/Label format! " ,
197+ " Must be 0.1, 0.2, or 0.3" ))
198+ }
199+
150200# internal use only!
151201# ' @noRd
152202setMethod ("multiscales ", "list", \(x) {
0 commit comments