Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions R/bidsio.R
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ read_confounds.bids_project <- function(x, subid=".*", task=".*", session=".*",
if (!inherits(x, "bids_project")) {
stop("`x` must be a `bids_project` object.")
}

if (!is.numeric(npcs) || length(npcs) != 1 || npcs < 0 || npcs != as.integer(npcs)) {
stop("`npcs` must be a non-negative integer.")
}

if (!is.numeric(perc_var) || length(perc_var) != 1 || perc_var < 0 || perc_var > 100) {
stop("`perc_var` must be between 0 and 100.")
}

# Check participants
sids <- participants(x)
Expand Down Expand Up @@ -446,6 +454,13 @@ read_confounds.bids_project <- function(x, subid=".*", task=".*", session=".*",

#' @keywords internal
process_confounds <- function(dfx, center=TRUE, scale=TRUE, npcs=-1, perc_var=-1) {
if (!is.numeric(npcs) || length(npcs) != 1 || npcs < 0 || npcs != as.integer(npcs)) {
stop("`npcs` must be a non-negative integer.")
}

if (!is.numeric(perc_var) || length(perc_var) != 1 || perc_var < 0 || perc_var > 100) {
stop("`perc_var` must be between 0 and 100.")
}
m <- as.matrix(dfx)
# Impute NAs
if (anyNA(m)) {
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test_read_confounds.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ test_that("canonical names resolve to dataset columns", {
cols <- names(conf$data[[1]])
expect_true(all(c("CSF", "WhiteMatter", "FramewiseDisplacement", "GlobalSignal") %in% cols))
})

test_that("invalid PCA parameters trigger errors", {
setup <- create_confounds_proj()
on.exit(unlink(setup$path, recursive = TRUE, force = TRUE), add = TRUE)
expect_error(read_confounds(setup$proj, npcs = -1), "npcs")
expect_error(read_confounds(setup$proj, npcs = 1.5), "npcs")
expect_error(read_confounds(setup$proj, perc_var = -10), "perc_var")
expect_error(read_confounds(setup$proj, perc_var = 110), "perc_var")
})