Skip to content

Commit

Permalink
fix: value_at for multiple cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sgibb committed Jun 9, 2024
1 parent a507998 commit b1bf478
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(add_anaesthesia_case_id)
export(case_duration)
export(case_end)
export(case_start)
Expand Down
30 changes: 17 additions & 13 deletions R/read_logbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)
)[order(DateTime),]
lb[, Current := as.double(Current)]
.add_anaesthesia_case_id(lb)
add_anaesthesia_case_id(lb)
lb[!is.na(CaseId) & CaseId > 0,]
}

Expand All @@ -61,12 +61,11 @@ value_at <- function(x, label, time,
"mechanical-ventilation"
)) {
reference <- match.arg(reference)
x[,
.SD[
Label == label &
DateTime <= .reference_time(x, reference) + minutes(time),
Current[.N]
],
r <- .reference_time(x, reference)
r[, ReferenceTime := ReferenceTime + minutes(time)]
m <- merge(x, r)
m[,
.SD[Label == label & DateTime <= ReferenceTime, Current[.N]],
by = CaseId
]
}
Expand All @@ -84,8 +83,9 @@ value_at <- function(x, label, time,
"mechanical-ventilation" = "Ventilation settings"
)
x[,
.SD[Label == ref_label & !Current %in% c(0, 9), DateTime[1]],
by = CaseId][,V1]
.(ReferenceTime =
.SD[Label == ref_label & !Current %in% c(0, 9), DateTime[1]]),
by = CaseId]
}

#' Filter cases
Expand Down Expand Up @@ -132,7 +132,7 @@ case_start <- function(x, reference = c(
"start", "vaporizer-opening",
"mechanical-ventilation"
)) {
.reference_time(x, reference)
.reference_time(x, reference)[, ReferenceTime]
}

#' @return `POSIXct`, end times
Expand Down Expand Up @@ -166,10 +166,14 @@ is_volatile_anesthesia <- function(x) {
][, V1]
}

#' Case Id
#'
#' Add an additional column with a corresponding case id.
#'
#' @param x `data.table`, logbook data
#' @return `integer`, row indices
#' @noRd
.add_anaesthesia_case_id <- function(x) {
#' @return `data.table`, with additional CaseId column.
#' @export
add_anaesthesia_case_id <- function(x) {
setorder(x, DateTime)
x[, CaseId := cumsum(Label == "Start of therapy")]
x[, CaseId := fifelse(
Expand Down
17 changes: 17 additions & 0 deletions man/add_anaesthesia_case_id.Rd

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

27 changes: 15 additions & 12 deletions tests/testthat/test_read_logbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ test_that(".read_logbook", {

test_that("value_at", {
lb <- data.table(
DateTime = 1:8 * 60,
DateTime = lubridate::ymd_hms("2024-06-09 20:00:00") +
lubridate::minutes(c(1:9, 12)),
Label = c("Start of therapy", "Ventilation settings",
"Vaporizer setting", "Ventilation settings",
"PEEP", "FGF", "FGF", "FGF"),
Current = c(2, 0, 0.5, 2, 5, 0.5, 0.6, 0.7),
CaseId = 1
"PEEP", "FGF", "FGF", "FGF", "Start of therapy", "FGF"),
Current = c(2, 0, 0.5, 2, 5, 0.5, 0.6, 0.7, 2, 1.5),
CaseId = c(rep(1, 8), 2, 2)
)
expect_equal(value_at(lb, "FGF", 5, "start")[,V1], 0.5)
expect_equal(value_at(lb, "FGF", 6, "start")[,V1], 0.6)
expect_equal(value_at(lb, "FGF", 7, "start")[,V1], 0.7)
expect_equal(value_at(lb, "FGF", 5, "start")[,V1], c(0.5, 1.5))
expect_equal(value_at(lb, "FGF", 6, "start")[,V1], c(0.6, 1.5))
expect_equal(value_at(lb, "FGF", 7, "start")[,V1], c(0.7, 1.5))
expect_equal(value_at(lb, "FGF", 3, "vaporizer-opening")[,V1], 0.5)
expect_equal(value_at(lb, "FGF", 2, "mechanical-ventilation")[,V1], 0.5)
expect_equal(value_at(lb, "PEEP", 15, "start")[,V1], 5)
Expand All @@ -36,9 +37,11 @@ test_that(".reference_time", {
Current = c(2, 0, 0.5, 2),
CaseId = 1
)
expect_equal(.reference_time(lb, "start"), 1)
expect_equal(.reference_time(lb, "vaporizer-opening"), 3)
expect_equal(.reference_time(lb, "mechanical-ventilation"), 4)
expect_equal(.reference_time(lb, "start")[, ReferenceTime], 1)
expect_equal(.reference_time(lb, "vaporizer-opening")[, ReferenceTime], 3)
expect_equal(
.reference_time(lb, "mechanical-ventilation")[, ReferenceTime], 4
)
})

test_that("filter_short_cases", {
Expand Down Expand Up @@ -102,15 +105,15 @@ test_that("is_volatile_anesthesia", {
expect_equal(is_volatile_anesthesia(lb), c(FALSE, FALSE, TRUE, FALSE))
})

test_that(".add_anaesthesia_case_id", {
test_that("add_anaesthesia_case_id", {
lb <- data.table(
DateTime = 1:6,
Label = c(
"Start of therapy", "Case duration", "bar",
"Start of therapy", "Case duration", "foo"
)
)
expect_equal(.add_anaesthesia_case_id(lb)$CaseId, c(1, 1, NA, 2, 2, NA))
expect_equal(add_anaesthesia_case_id(lb)$CaseId, c(1, 1, NA, 2, 2, NA))
})

test_that(".fgf_index", {
Expand Down

0 comments on commit b1bf478

Please sign in to comment.