Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orthogonal selection #108

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d1a0b07
implement Int class and methods
Artur-man Aug 28, 2024
3d3231c
implement IntDimIndexer
Artur-man Aug 28, 2024
145a932
implement orthogonal indexer and orthogonal selection
Artur-man Aug 29, 2024
3903f99
implement Order and IntArrayDimIndexer classes
Artur-man Aug 29, 2024
587d9ec
fix documentation of Order class and finalize new method for IntArray…
Artur-man Aug 29, 2024
09b9bb2
implement iter method for IntArrayDimIndexer
Artur-man Aug 29, 2024
6811fc0
fix R specific indexing fixes for Orthogonal and IntArrayDim indexers
Artur-man Aug 29, 2024
88b19b7
fix zero_based_to_one_based function to accept non Slice objects and …
Artur-man Aug 30, 2024
0cdc700
update normalize_integer_selection for integer vector and fix for R i…
Artur-man Aug 30, 2024
155b5bc
fix IntArrayDimIndexer bug for nonincreasing and decreasing vector of…
Artur-man Aug 30, 2024
5c867dd
support for slicing with step size with seq function
Artur-man Aug 30, 2024
eef088b
fix chunk index selection bug in IntArrayDimIndexer
Artur-man Aug 31, 2024
98bd238
add more tests
Artur-man Aug 31, 2024
b7f5c5c
update normalization and add notes
Artur-man Aug 31, 2024
5f39613
update tests, add filtering functions, update get_item to include is_…
Artur-man Aug 31, 2024
974ae84
readd figure
Artur-man Aug 31, 2024
d8fb887
update `[` method to incorporate orthogonal selection
Artur-man Sep 3, 2024
d7d45c0
add new orthogonal selection tests
Artur-man Sep 3, 2024
0f96973
update manage_filters for orthogonal selection
Artur-man Sep 3, 2024
de8d51e
update s3 tests
Artur-man Sep 3, 2024
7acf5e8
add note for BoolArrayDimIndexer
Artur-man Sep 3, 2024
1aa13ab
remove functions and documentation of int() utilities for now #108
Artur-man Sep 7, 2024
74686fe
remove commented-out lines
Artur-man Sep 10, 2024
7b6104b
Update R/indexing.R
Artur-man Sep 10, 2024
441c6ae
Update indexing.R, remove whitespace
Artur-man Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Imports:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Suggests:
testthat,
knitr,
Expand Down
73 changes: 44 additions & 29 deletions R/array-nested.R
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
#' @keywords internal
zero_based_to_one_based <- function(selection, shape) {

if(!all(vapply(selection, is_slice, logical(length = 1))))
stop("selection must be a list of slices")
# drop this since we could do it for arbitrary indices
# if(!all(vapply(selection, is_slice, logical(length = 1))))
# stop("selection must be a list of slices")

selection_list <- list()

for(i in seq_len(length(selection))) {

# get selection
sel <- selection[[i]]
# We assume the selection uses zero-based indexing,
# and internally convert to R-based / 1-based indexing
# before accessing data on the internal self$data.
sel_start <- sel$start + 1 # Add one, since R indexing is zero-based.
sel_stop <- sel$stop # Do not subtract one, since R indexing is inclusive.
sel_step <- sel$step
if(is.na(sel_step)) sel_step <- 1
# TODO: convert these warnings to errors once we know internals do indexing correctly
if(sel_start < 1) {
sel_start <- 1
message("IndexError: NestedArray$get() received slice with start index out of bounds - too low")
}
if(sel_start > shape[i]) {
sel_start <- shape[i]
message("IndexError: NestedArray$get() received slice with start index out of bounds - too high")
}
if(sel_stop < 1) {
sel_stop <- 1
message("IndexError: NestedArray$get() received slice with stop index out of bounds - too low")
}
if(sel_stop > shape[i]) {
sel_stop <- shape[i]
message("IndexError: NestedArray$get() received slice with stop index out of bounds - too high")
}
selection_list <- append(selection_list, list(seq(from = sel_start,
to = sel_stop,
by = sel_step)))

# for slice
if(inherits(sel, "Slice")){

# We assume the selection uses zero-based indexing,
# and internally convert to R-based / 1-based indexing
# before accessing data on the internal self$data.
sel_start <- sel$start + 1 # Add one, since R indexing is zero-based.
sel_stop <- sel$stop # Do not subtract one, since R indexing is inclusive.
sel_step <- sel$step
if(is.na(sel_step)) sel_step <- 1
# TODO: convert these warnings to errors once we know internals do indexing correctly
if(sel_start < 1) {
sel_start <- 1
message("IndexError: NestedArray$get() received slice with start index out of bounds - too low")
}
if(sel_start > shape[i]) {
sel_start <- shape[i]
message("IndexError: NestedArray$get() received slice with start index out of bounds - too high")
}
if(sel_stop < 1) {
sel_stop <- 1
message("IndexError: NestedArray$get() received slice with stop index out of bounds - too low")
}
if(sel_stop > shape[i]) {
sel_stop <- shape[i]
message("IndexError: NestedArray$get() received slice with stop index out of bounds - too high")
}
selection_list <- append(selection_list, list(seq(from = sel_start,
to = sel_stop,
by = sel_step)))
} else {

sel <- sel + 1
selection_list <- append(selection_list, list(sel))
}
# } else {
# stop("Unknown selection type")
# }
}
return(selection_list)
}
Expand Down
46 changes: 46 additions & 0 deletions R/filters.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
manage_filters <- function(filters) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to add documentation comments for this function?

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(seq(from, to, by))
} else {
by <- NA
return(seq(from, to))
}
return(seq(from, to, by))
} else if(x[[1]] == "c") {
check_func <- sapply(x, function(y) {
!is.function(eval(y))
})
return(floor(unlist(x[check_func])))
} else {
stop("Unsupported filter '", as.character(x), "' supplied")
}
} else {
stop("Unsupported filter '", as.character(x), "' supplied")
}
})
}
Loading