Skip to content

Commit

Permalink
feat: ✨ update algorithm logic to include regex in parentheses (#159)
Browse files Browse the repository at this point in the history
## Description

This PR updates the logic in `get_algorithm_logic()` to include regex in
parentheses.
We'll need for e.g., podiatrist services: `(speciale =~ '^54') AND
(barnmak != 0)`.

To test the updated regex, I added an argument to `get_algorithm_logic`
with a default of `algorithm`. This enables me to add a
`test_algorithm_logic` object instead of the algorithm we use, so the
tests won't start failing if/when the algorithm is updated.

I don't think this is good practice (adding an arg mainly just for test
purposes), but I wanted to test the logic formally now that it's a bit
more complex. Let me know if there's a better way to do this!

---------

Co-authored-by: Luke W. Johnston <lwjohnst@gmail.com>
Co-authored-by: Anders Aasted Isaksen <67263135+Aastedet@users.noreply.github.com>
Co-authored-by: Luke W. Johnston <lwjohnst86@users.noreply.github.com>
  • Loading branch information
4 people authored Dec 18, 2024
1 parent 95cabfe commit dd78c3e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions R/get-algorithm.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#' get_algorithm_logic("hba1c")
#' get_algorithm_logic("gld")
#' }
get_algorithm_logic <- function(criteria) {
algorithm |>
get_algorithm_logic <- function(criteria, algorithm_logic = algorithm) {
algorithm_logic |>
dplyr::filter(.data$name == criteria) |>
dplyr::pull(.data$logic) |>
stringr::str_replace_all("AND", "&") |>
stringr::str_replace_all("OR", "|") |>
# regex are defined with '=~', so convert them into a stringr function.
stringr::str_replace_all("(.*) \\=\\~ (.*)", "stringr::str_detect(\\1, \\2)")
stringr::str_replace_all("(\\(?)([a-zA-Z0-9_]+)\\)? \\=\\~ ('.*')", "\\1stringr::str_detect(\\2, \\3)")
}
24 changes: 24 additions & 0 deletions tests/testthat/test-get-algorithm-logic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
test_algorithm_logic <- data.frame(
name = c("hba1c", "podiatrist_services", "gld"),
logic = c(
"(analysiscode == 'NPU27300' AND value >= 48) OR (analysiscode == 'NPU03835' AND value >= 6.5)",
"(speciale =~ '^54') AND (barnmak != 0)",
"atc =~ '^A10'"
)
)

test_that("`or` logic is converted to R logic", {
get_algorithm_logic("hba1c", test_algorithm_logic) |>
expect_equal("(analysiscode == 'NPU27300' & value >= 48) | (analysiscode == 'NPU03835' & value >= 6.5)")
})

test_that("single regex is converted to R logic", {
get_algorithm_logic("gld", test_algorithm_logic) |>
expect_equal("stringr::str_detect(atc, '^A10')")
})

test_that("`and` logic and regex within parentheses are converted to R logic", {
# i.e., the regex is within a parenthesis
get_algorithm_logic("podiatrist_services", test_algorithm_logic) |>
expect_equal("(stringr::str_detect(speciale, '^54')) & (barnmak != 0)")
})

0 comments on commit dd78c3e

Please sign in to comment.