-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilters.R
52 lines (52 loc) · 1.93 KB
/
filters.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
# transforming filters to be passed to ZarrArray$get_orthogonal_selection()
#
# a:b => slice(a,b)
# seq(from, to, by) => slice(start, stop, step) ? for now indices of seq(from, to, by) are passed to get_orthogonal_selection (check below, TODO)
# c(a,b,c) => c(a,b,c), combine elements are passed as indices
# empty dimension => return everything
#
manage_filters <- function(filters) {
lapply(filters, function(x) {
# Proceed based on type of filter
if(typeof(x) == "symbol") {
# When empty dimension, return everything
if(x == "") {
return(NULL)
} else {
stop("Unsupported filter '", as.character(x), "' supplied")
}
} else if(typeof(x) == "double") {
# Return single value for dimension
return(slice(x, x))
} else if(typeof(x) == "language") {
x <- as.list(x)
# Return a range (supplied via : or seq())
if(x[[1]] == ":") {
return(slice(x[[2]], x[[3]]))
} else if(x[[1]] == "seq") {
# TODO: do we need slicing for this case ? otherwise implement slice(start, stop, step)
arg_names <- names(x)
from <- ifelse("from" %in% arg_names, x[[which("from" == arg_names)]], x[[2]])
to <- ifelse("to" %in% arg_names, x[[which("to" == arg_names)]], x[[3]])
if(length(x) > 3) {
by <- ifelse("by" %in% arg_names, x[[which("by" == arg_names)]], x[[4]])
return(int(seq(from, to, by)))
} else {
by <- NA
return(int(seq(from, to)))
}
return(int(seq(from, to, by)))
} else if(x[[1]] == "c") {
# return elements of the combine function as indices
check_func <- sapply(x, function(y) {
!is.function(eval(y))
})
return(int(floor(unlist(x[check_func]))))
} else {
stop("Unsupported filter '", as.character(x), "' supplied")
}
} else {
stop("Unsupported filter '", as.character(x), "' supplied")
}
})
}