-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindexing.R
293 lines (263 loc) Β· 8.45 KB
/
indexing.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#' The Zarr OIndex class.
#' @title OIndex Class
#' @docType class
#' @description
#' @keywords internal
#' Reference: https://github.com/zarr-developers/zarr-python/blob/5dd4a0/zarr/indexing.py#L655
#'
#' @rdname OIndex
#' @export
OIndex <- R6::R6Class("OIndex",
public = list(
#' @field array
#' @keywords internal
array = NULL,
#' @description
#' Create a new OIndex instance.
#' @return An `OIndex` instance.
initialize = function(array) {
self$array <- array
}
)
)
#' The Zarr VIndex class.
#' @title VIndex Class
#' @docType class
#' @description
#' @keywords internal
#' Reference: https://github.com/zarr-developers/zarr-python/blob/5dd4a0/zarr/indexing.py#L811
#'
#' @rdname VIndex
#' @export
VIndex <- R6::R6Class("VIndex",
public = list(
#' @field array
#' @keywords internal
array = NULL,
#' @description
#' Create a new VIndex instance.
#' @return A `VIndex` instance.
initialize = function(array) {
self$array <- array
}
)
)
#' The Zarr IntDimIndexer class.
#' @title IntDimIndexer Class
#' @docType class
#' @description
#' @keywords internal
#' Reference: https://github.com/zarr-developers/zarr-python/blob/5dd4a0/zarr/indexing.py#L138
#'
#' @rdname IntDimIndexer
#' @export
IntDimIndexer <- R6::R6Class("IntDimIndexer",
inherit = DimIndexer,
public = list(
#' @field dim_sel
#' @keywords internal
dim_sel = NULL,
#' @field dim_len
#' @keywords internal
dim_len = NULL,
#' @field dim_chunk_len
#' @keywords internal
dim_chunk_len = NULL,
#' @description
#' Create a new IntDimIndexer instance.
#' @return A `IntDimIndexer` instance.
initialize = function(dim_sel, dim_len, dim_chunk_len) {
# Normalize
dim_sel <- normalize_integer_selection(dim_sel, dim_len)
self$dim_sel <- dim_sel
self$dim_len <- dim_len
self$dim_chunk_len <- dim_chunk_len
self$num_items <- 1
},
iter = function() {
# TODO: use generator/yield features from async package
dim_chunk_index <- floor(self$dim_sel / self$dim_chunk_len)
dim_offset <- dim_chunk_index * self$dim_chunk_len
dim_chunk_sel <- self$dim_sel - dim_offset
dim_out_sel <- NA
return(list(
ChunkDimProjection$new(
dim_chunk_index,
dim_chunk_sel,
dim_out_sel
)
))
}
)
)
#' The Zarr SliceDimIndexer class.
#' @title SliceDimIndexer Class
#' @docType class
#' @description
#' @keywords internal
#' Reference: https://github.com/zarr-developers/zarr-python/blob/5dd4a0/zarr/indexing.py#L163
#'
#' @rdname SliceDimIndexer
#' @export
SliceDimIndexer <- R6::R6Class("SliceDimIndexer",
inherit = DimIndexer,
public = list(
dim_len = NULL,
dim_chunk_len = NULL,
num_chunks = NULL,
start = NULL,
stop = NULL,
step = NULL,
#' @description
#' Create a new SliceDimIndexer instance.
#' @return A `SliceDimIndexer` instance.
initialize = function(dim_sel, dim_len, dim_chunk_len) {
# Reference: https://github.com/gzuidhof/zarr.js/blob/292804/src/core/indexing.ts#L311
si <- dim_sel$indices(dim_len)
self$start <- si[1]
self$stop <- si[2]
self$step <- si[3]
if(self$step < 1) {
stop('NegativeStepError')
}
self$dim_len <- dim_len
self$dim_chunk_len <- dim_chunk_len
self$num_items <- max(0, ceiling((self$stop - self$start) / self$step))
self$num_chunks <- ceiling(self$dim_len / self$dim_chunk_len)
},
iter = function() {
# TODO: use generator/yield features from async package
dim_chunk_index_from <- floor(self$start / self$dim_chunk_len)
dim_chunk_index_to <- ceiling(self$stop / self$dim_chunk_len)
# START R-SPECIFIC
if(dim_chunk_index_from == dim_chunk_index_to) {
dim_chunk_index_to <- dim_chunk_index_to + 1
}
# END R-SPECIFIC
# Iterate over chunks in range
result <- list()
for(dim_chunk_index in seq(from = dim_chunk_index_from, to = (dim_chunk_index_to - 1), by = 1)) {
# Compute offsets for chunk within overall array
dim_offset <- dim_chunk_index * self$dim_chunk_len
dim_limit <- min(self$dim_len, (dim_chunk_index + 1) * self$dim_chunk_len)
# Determine chunk length, accounting for trailing chunk
dim_chunk_len <- dim_limit - dim_offset
dim_chunk_sel_start <- 0
dim_chunk_sel_stop <- 0
dim_out_offset <- 0
if(self$start < dim_offset) {
# Selection starts before the current chunk
dim_chunk_sel_start <- 0
remainder <- (dim_offset - self$start) %% self$step
if(remainder > 0) {
dim_chunk_sel_start <- dim_chunk_sel_start + (self$step - remainder)
}
# Compute number of previous items, provides offset into output array
dim_out_offset <- ceiling((dim_offset - self$start) / self$step)
} else {
# Selection starts within the current chunk
dim_chunk_sel_start <- self$start - dim_offset
dim_out_offset <- 0
}
if(self$stop > dim_limit) {
# Selection ends after current chunk
dim_chunk_sel_stop <- self$dim_chunk_len
} else {
# Selection ends within current chunk
dim_chunk_sel_stop <- self$stop - dim_offset
}
dim_chunk_sel <- zb_slice(dim_chunk_sel_start, dim_chunk_sel_stop, self$step)
dim_chunk_num_items <- ceiling((dim_chunk_sel_stop - dim_chunk_sel_start) / self$step)
dim_out_sel <- zb_slice(dim_out_offset, dim_out_offset + dim_chunk_num_items)
result <- append(result, ChunkDimProjection$new(
dim_chunk_index,
dim_chunk_sel,
dim_out_sel
))
}
return(result)
}
)
)
#' The Zarr BasicIndexer class.
#' @title BasicIndexer Class
#' @docType class
#' @description
#' @keywords internal
#' Reference: https://github.com/zarr-developers/zarr-python/blob/5dd4a0/zarr/indexing.py#L326
#'
#' @rdname BasicIndexer
#' @export
BasicIndexer <- R6::R6Class("BasicIndexer",
inherit = Indexer,
public = list(
#' @field dim_indexers
#' @keywords internal
dim_indexers = NULL,
#' @description
#' Create a new VIndex instance.
#' @return A `VIndex` instance.
initialize = function(selection, array) {
shape <- array$get_shape()
chunks <- array$get_chunks()
selection <- normalize_list_selection(selection, shape)
# Setup per-dimension indexers
dim_indexers <- list()
for(i in seq_along(selection)) {
dim_sel <- selection[[i]]
dim_len <- shape[i]
dim_chunk_len <- chunks[i]
if(is.null(dim_sel)) {
dim_sel <- zb_slice(NA)
}
if(is_integer(dim_sel)) {
dim_indexer <- IntDimIndexer$new(dim_sel, dim_len, dim_chunk_len)
} else if(is_slice(dim_sel)) {
dim_indexer <- SliceDimIndexer$new(dim_sel, dim_len, dim_chunk_len)
} else {
stop('Unsupported selection item for basic indexing, expected integer or slice')
}
dim_indexers <- append(dim_indexers, dim_indexer)
}
self$shape <- list()
for(d in dim_indexers) {
if(class(d)[[1]] == "SliceDimIndexer") {
self$shape <- append(self$shape, d$num_items)
}
}
self$drop_axes <- NA
self$dim_indexers <- dim_indexers
},
iter = function() {
# TODO: use generator/yield features from async package
result <- list()
# dim_indexers is a list of DimIndexer objects.
# dim_indexer_iterables is a list (one per dimension)
# of lists of IntDimIndexer or SliceDimIndexer objects.
dim_indexer_iterables <- lapply(self$dim_indexers, function(di) di$iter())
dim_indexer_product <- get_list_product(dim_indexer_iterables)
for(row_i in seq_len(length(dim_indexer_product))) {
dim_proj <- dim_indexer_product[[row_i]]
chunk_coords <- list()
chunk_sel <- list()
out_sel <- list()
if(!is.list(dim_proj)) {
dim_proj <- list(dim_proj)
}
for(p in dim_proj) {
chunk_coords <- append(chunk_coords, p$dim_chunk_index)
chunk_sel <- append(chunk_sel, p$dim_chunk_sel)
if(!is_na(p$dim_out_sel)) {
out_sel <- append(out_sel, p$dim_out_sel)
}
}
result <- append(result, ChunkProjection$new(
chunk_coords,
chunk_sel,
out_sel
))
}
return(result)
}
)
)