Skip to content

Commit

Permalink
refactor: update as per review
Browse files Browse the repository at this point in the history
  • Loading branch information
bczech committed Aug 19, 2024
1 parent 071934e commit 7b1da3b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(aggregate_assay)
export(apply_bumpy_function)
export(assert_choices)
export(average_biological_replicates_dt)
export(calc_sd)
export(capVals)
export(cap_xc50)
export(convert_colData_to_json)
Expand Down
34 changes: 23 additions & 11 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -470,19 +470,11 @@ average_biological_replicates_dt <- function(
if (add_sd) {
# Calculate standard deviation for both average_fields and geometric_average_fields
sd_fields <- paste0(average_fields, "_sd")
geo_sd_fields <- paste0(geometric_average_fields, "_sd")
geom_sd_fields <- paste0(geometric_average_fields, "_sd")

data <- data[, (sd_fields) := lapply(.SD,
function(x) {
ifelse(length(x) > 1,
stats::sd(x, na.rm = TRUE), 0)
}),
data <- data[, (sd_fields) := lapply(.SD, calc_sd),
.SDcols = average_fields, by = group_by]
data <- data[, (geo_sd_fields) := lapply(.SD,
function(x) {
ifelse(length(x) > 1,
stats::sd(x, na.rm = TRUE), 0)
}),
data <- data[, (geom_sd_fields) := lapply(.SD, calc_sd),
.SDcols = geometric_average_fields, by = group_by]

# Calculate count and add as a single column
Expand Down Expand Up @@ -751,3 +743,23 @@ get_additional_variables <- function(dt_list,
}
}

#' Calculate Standard Deviation or Return Zero
#'
#' This function calculates the standard deviation of a numeric vector. If the vector has a length of 1 and it is numeric, it returns 0.
#'
#' @param x A numeric vector.
#' @return The standard deviation of the vector if its length is greater than 1 or it is not numeric, otherwise 0.
#' @examples
#' calc_sd(c(1, 2, 3, 4, 5)) # Should return the standard deviation
#' calc_sd(c(1)) # Should return 0
#' calc_sd(numeric(0)) # Should return NA
#' calc_sd(c("a", "b", "c")) # Should return NA
#' @export
calc_sd <- function(x) {
if (length(x) == 1 && is.numeric(x)) {
return(0)
} else {
return(stats::sd(x, na.rm = TRUE))
}
}

2 changes: 1 addition & 1 deletion man/average_biological_replicates_dt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/calc_sd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ test_that("average_biological_replicates_dt works as expected", {
add_sd = TRUE)

expect_equal(dim(avg_metrics_data2), c(40, 44))
expect_equal(sum(grepl("_sd", names(avg_metrics_data2))), 15)
expect_true("count" %in% names(avg_metrics_data2))
})

test_that("get_duplicated_rows works as expected", {
Expand Down Expand Up @@ -564,4 +566,27 @@ test_that("capVals works as expected", {
expect_equal(dt1c, dt2)

expect_error(capVals(as.list(dt1)), "Must be a data.table")
})
})

test_that("calc_sd returns correct standard deviation for vectors with length > 1", {
expect_equal(calc_sd(c(1, 2, 3, 4, 5)), sd(c(1, 2, 3, 4, 5), na.rm = TRUE))
expect_equal(calc_sd(c(10, 20, 30)), sd(c(10, 20, 30), na.rm = TRUE))
})

test_that("calc_sd returns 0 for vectors with length == 1 and numeric", {
expect_equal(calc_sd(c(1)), 0)
})

test_that("calc_sd returns NA for vectors with length == 1 and non-numeric", {
expect_true(is.na(calc_sd("2")))
expect_true(is.na(calc_sd(TRUE)))
})

test_that("calc_sd returns NA for empty vectors", {
expect_true(is.na(calc_sd(numeric(0))))
})

test_that("calc_sd handles NA values correctly", {
expect_equal(calc_sd(c(1, 2, NA, 4, 5)), sd(c(1, 2, NA, 4, 5), na.rm = TRUE))
expect_true(is.na(calc_sd(c(NA, NA, NA))))
})

0 comments on commit 7b1da3b

Please sign in to comment.