Skip to content

Commit

Permalink
Update ConceptCounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Marta Alcalde-Herraiz committed Nov 5, 2024
1 parent c653d94 commit 98030e8
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 59 deletions.
8 changes: 6 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: OmopSketch
Title: Characterise Tables of an OMOP Common Data Model Instance
Version: 0.1.2
Version: 0.1.1
Authors@R: c(
person(
"Marta", "Alcalde-Herraiz",
Expand All @@ -15,6 +15,10 @@ Authors@R: c(
"Elin", "Rowlands", email = "[email protected]",
role = c("aut"), comment = c(ORCID = "0009-0005-5166-0417")
),
person(
"Cecilia", "Campanile", email = "[email protected]",
role = c("aut"), comment = c(ORCID = "0009-0007-6629-4661")
),
person(
"Edward", "Burn", email = "[email protected]",
role = c("aut"), comment = c(ORCID = "0000-0002-9286-1128")
Expand Down Expand Up @@ -71,4 +75,4 @@ URL: https://OHDSI.github.io/OmopSketch/
BugReports: https://github.com/OHDSI/OmopSketch/issues
VignetteBuilder: knitr
Remotes:
darwin-eu-dev/omopgenerics@mah_vl
darwin-eu-dev/omopgenerics
35 changes: 22 additions & 13 deletions R/plotConceptCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#'
#' result |>
#' filter(variable_name == "Number subjects") |>
#' plotConceptCounts(facet = "codelist_name", colour = "codelist_name")
#' plotConceptCounts(facet = "codelist_name", colour = "standard_concept_name")
#'
#' PatientProfiles::mockDisconnect(cdm)
#' }
Expand All @@ -35,9 +35,7 @@ plotConceptCounts <- function(result,

# subset to results of interest
result <- result |>
visOmopResults::filterSettings(.data$result_type == "summarise_concept_counts") |>
dplyr::mutate(variable_level = gsub(" to.*","",.data$variable_level)) |>
dplyr::mutate(variable_level = gsub("-01$","",.data$variable_level))
visOmopResults::filterSettings(.data$result_type == "summarise_concept_counts")

if (nrow(result) == 0) {
cli::cli_abort(c("!" = "No records found with result_type == summarise_concept_counts"))
Expand All @@ -52,12 +50,14 @@ plotConceptCounts <- function(result,
))
}

result1 <- result |> visOmopResults::splitAdditional()
# Detect if there are several time intervals
if(length(unique(result$variable_level)) > 1 ){
if("time_interval" %in% colnames(result1)){
# Line plot where each concept is a different line
p <- result |>
dplyr::filter(.data$variable_level != "overall") |>
visOmopResults::scatterPlot(x = "variable_level",
p <- result1 |>
dplyr::filter(.data$time_interval != "overall") |>
visOmopResults::uniteAdditional(cols = c("time_interval", "standard_concept_name", "standard_concept_id", "source_concept_name", "source_concept_id", "domain_id")) |>
visOmopResults::scatterPlot(x = "time_interval",

Check warning on line 60 in R/plotConceptCounts.R

View check run for this annotation

Codecov / codecov/patch

R/plotConceptCounts.R#L57-L60

Added lines #L57 - L60 were not covered by tests
y = "count",
line = TRUE,
point = TRUE,
Expand All @@ -66,11 +66,20 @@ plotConceptCounts <- function(result,
facet = facet,
colour = colour)
}else{
p <- result |>
visOmopResults::barPlot(x = "standard_concept_name",
y = "count",
facet = facet,
colour = colour) +
if("standard_concept_name" %in% colnames(result1)){
p <- result |>
visOmopResults::barPlot(x = "standard_concept_name",
y = "count",
facet = facet,
colour = colour)
}else{
p <- result |>
visOmopResults::barPlot(x = "codelist_name",
y = "count",
facet = facet,
colour = colour)

Check warning on line 80 in R/plotConceptCounts.R

View check run for this annotation

Codecov / codecov/patch

R/plotConceptCounts.R#L76-L80

Added lines #L76 - L80 were not covered by tests
}
p <- p +
ggplot2::labs(
x = "Concept name"
)
Expand Down
4 changes: 2 additions & 2 deletions R/plotRecordCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ plotRecordCount <- function(result,
)
}else{
p <- result |>
visOmopResults::barPlot(x = "additional_level",
visOmopResults::barPlot(x = "variable_name",

Check warning on line 60 in R/plotRecordCount.R

View check run for this annotation

Codecov / codecov/patch

R/plotRecordCount.R#L60

Added line #L60 was not covered by tests
y = "count",
facet = facet,
colour = colour) +
ggplot2::labs(
y = "Count",
x = "Number records"
x = ""

Check warning on line 66 in R/plotRecordCount.R

View check run for this annotation

Codecov / codecov/patch

R/plotRecordCount.R#L66

Added line #L66 was not covered by tests
)
}
p
Expand Down
6 changes: 4 additions & 2 deletions R/summariseConceptCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ getCodeUse <- function(x,
}

cc <- records |>
# dplyr::collect() |> # https://github.com/darwin-eu-dev/PatientProfiles/issues/706
PatientProfiles::summariseResult(strata = strata,
variable = "standard_concept_name",
group = group,
Expand All @@ -211,7 +210,10 @@ getCodeUse <- function(x,
if(interval != "overall"){
cc <- cc |>
visOmopResults::splitStrata() |>
dplyr::mutate(variable_level = .data$interval_group) |>
dplyr::mutate("additional_level" = dplyr::if_else(.data$interval_group == "overall", .data$additional_level, paste0(.data$interval_group, " &&& ", .data$additional_level))) |>
dplyr::mutate("additional_name" = dplyr::if_else(.data$interval_group == "overall", .data$additional_name, paste0("time_interval &&& ", .data$additional_name))) |>
dplyr::mutate("additional_level" = gsub(" &&& overall$", "", .data$additional_level)) |>
dplyr::mutate("additional_name" = gsub(" &&& overall$", "", .data$additional_name)) |>
visOmopResults::uniteStrata(unique(unlist(strata))[unique(unlist(strata)) != "interval_group"]) |>
dplyr::select(-"interval_group")
}
Expand Down
2 changes: 1 addition & 1 deletion R/summariseInObservation.R
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ addSexOverall <- function(result, sex){
dplyr::group_by(.data$age_group, .data$additional_level, .data$variable_name) |>
dplyr::summarise(estimate_value = sum(.data$estimate_value, na.rm = TRUE), .groups = "drop") |>
dplyr::mutate(sex = "overall",
additional_name = dplyr::if_else(additional_level == "overall", "overall", "time_interval"))
additional_name = dplyr::if_else(.data$additional_level == "overall", "overall", "time_interval"))
)
}
return(result)
Expand Down
2 changes: 1 addition & 1 deletion R/summariseRecordCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ createSummarisedResultRecordCount <- function(result, strata, omopTable, omopTab
result <- result |>
visOmopResults::splitStrata() |>
dplyr::mutate(additional_level = .data$interval_group) |>
dplyr::mutate(additional_name = dplyr::if_else(additional_level == "overall", "overall", "time_interval")) |>
dplyr::mutate(additional_name = dplyr::if_else(.data$additional_level == "overall", "overall", "time_interval")) |>
visOmopResults::uniteStrata(unique(unlist(strata))[unique(unlist(strata)) != "interval_group"]) |>
dplyr::select(-"interval_group")
}
Expand Down
1 change: 1 addition & 0 deletions man/OmopSketch-package.Rd

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

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

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

78 changes: 41 additions & 37 deletions tests/testthat/test-summariseConceptCounts.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("summarise code use - eunomia", {
skip_on_cran()
cdm <- cdmEunomia()
cdm <- cdmEunomia()
acetiminophen <- c(1125315, 1127433, 40229134,
40231925, 40162522, 19133768, 1127078)
poliovirus_vaccine <- c(40213160)
Expand All @@ -10,6 +10,8 @@ test_that("summarise code use - eunomia", {
results <- summariseConceptCounts(cdm = cdm,
conceptId = cs,
interval = "years",
countBy = c("record", "person"),
concept = TRUE,
sex = TRUE,
ageGroup = list(c(0,17),
c(18,65),
Expand All @@ -23,12 +25,15 @@ test_that("summarise code use - eunomia", {
# min cell counts:
expect_equal(
omopgenerics::suppress(results, 5) |>
visOmopResults::splitAdditional() |>
dplyr::filter(
variable_name == "overall",
strata_level == "1909-01-01 to 1909-12-31",
strata_level == "overall",
variable_name == "Number records",
standard_concept_id == "overall",
time_interval == "1909-01-01 to 1909-12-31",
group_level == "acetiminophen") |>
dplyr::pull("estimate_value"),
as.character()
as.character(NA)
)

# check is a summarised result
Expand All @@ -38,13 +43,12 @@ test_that("summarise code use - eunomia", {

# overall record count
expect_true(results %>%
dplyr::filter(group_name == "codelist_name" &
strata_name == "overall" &
strata_level == "overall" &
variable_level == "overall" &
group_level == "acetiminophen" &
variable_name == "Number records",
additional_name == "overall") %>%
dplyr::filter(group_name == "codelist_name",
strata_name == "overall",
strata_level == "overall",
additional_level == "overall",
group_level == "acetiminophen",
variable_name == "Number records") %>%
dplyr::pull("estimate_value") |>
as.numeric() ==
cdm$drug_exposure %>%
Expand All @@ -58,7 +62,6 @@ test_that("summarise code use - eunomia", {
dplyr::filter(group_name == "codelist_name" &
strata_name == "overall" &
strata_level == "overall" &
variable_level == "overall" &
group_level == "acetiminophen" &
variable_name == "Number subjects",
additional_name == "overall") %>%
Expand All @@ -74,12 +77,13 @@ test_that("summarise code use - eunomia", {
# by year
# overall record count
expect_true(results %>%
visOmopResults::splitAdditional() |>
dplyr::filter(group_name == "codelist_name" &
strata_name == "overall" &
variable_level == "2008-01-01 to 2008-12-31" &
time_interval == "2008-01-01 to 2008-12-31" &
group_level == "acetiminophen" &
variable_name == "Number records",
additional_name == "overall") %>%
standard_concept_name == "overall") %>%
dplyr::pull("estimate_value") |>
as.numeric() ==
cdm$drug_exposure %>%
Expand All @@ -90,12 +94,13 @@ test_that("summarise code use - eunomia", {

# overall person count
expect_true(results %>%
visOmopResults::splitAdditional() |>
dplyr::filter(group_name == "codelist_name" &
strata_name == "overall" &
variable_level == "2008-01-01 to 2008-12-31" &
time_interval == "2008-01-01 to 2008-12-31" &
group_level == "acetiminophen" &
variable_name == "Number subjects",
additional_name == "overall") %>%
standard_concept_name == "overall") %>%
dplyr::pull("estimate_value") |>
as.numeric() ==
cdm$drug_exposure %>%
Expand All @@ -112,7 +117,6 @@ test_that("summarise code use - eunomia", {
dplyr::filter(group_name == "codelist_name" &
strata_name == "sex" &
strata_level == "Male" &
variable_level == "overall" &
group_level == "acetiminophen" &
variable_name == "Number records" &
additional_name == "overall") %>%
Expand All @@ -129,7 +133,6 @@ test_that("summarise code use - eunomia", {
dplyr::filter(group_name == "codelist_name" &
strata_name == "age_group &&& sex" &
strata_level == "18 to 65 &&& Male" &
variable_level == "overall" &
group_level == "acetiminophen" &
variable_name == "Number records",
additional_name == "overall") %>%
Expand All @@ -150,7 +153,6 @@ test_that("summarise code use - eunomia", {
dplyr::filter(group_name == "codelist_name" &
strata_name == "age_group &&& sex" &
strata_level == "18 to 65 &&& Male" &
variable_level == "overall" &
group_level == "acetiminophen" &
variable_name == "Number subjects",
additional_name == "overall") %>%
Expand All @@ -177,33 +179,35 @@ test_that("summarise code use - eunomia", {
c(18,65),
c(66, 100)))

expect_true(results1$additional_level |> unique() |> length() == 1)
expect_equal(
results1 |>
visOmopResults::splitAdditional() |>
dplyr::filter(variable_name == "Number records") |>
dplyr::arrange(dplyr::across(dplyr::everything())),
results |>
dplyr::filter(variable_name == "Number records", additional_name == "overall") |>
visOmopResults::splitAdditional() |>
dplyr::filter(variable_name == "Number records", standard_concept_name == "overall") |>
dplyr::select(-c(starts_with("standard_"), starts_with("source_"), "domain_id")) |>
dplyr::arrange(dplyr::across(dplyr::everything()))
)
expect_true(results1 |>
dplyr::filter(variable_name == "Number subjects",
group_level == "acetiminophen",
variable_level == "1909-01-01 to 1909-12-31",
strata_level == "0 to 17") |>
dplyr::pull("estimate_value") |>
as.numeric() ==
cdm$drug_exposure %>%
dplyr::filter(drug_concept_id %in% acetiminophen) %>%
PatientProfiles::addAge(indexDate = "drug_exposure_start_date") %>%
PatientProfiles::addSex() %>%
dplyr::filter(age >= "0", age <= "17", clock::get_year(drug_exposure_start_date) == 1909) |>
dplyr::select("person_id") %>%
dplyr::distinct() %>%
dplyr::tally() %>%
dplyr::pull("n"))
visOmopResults::splitAdditional() |>
dplyr::filter(variable_name == "Number subjects",
group_level == "acetiminophen",
time_interval == "1909-01-01 to 1909-12-31",
strata_level == "0 to 17") |>
dplyr::pull("estimate_value") |>
as.numeric() ==
cdm$drug_exposure %>%
dplyr::filter(drug_concept_id %in% acetiminophen) %>%
PatientProfiles::addAge(indexDate = "drug_exposure_start_date") %>%
PatientProfiles::addSex() %>%
dplyr::filter(age >= "0", age <= "17", clock::get_year(drug_exposure_start_date) == 1909) |>
dplyr::select("person_id") %>%
dplyr::distinct() %>%
dplyr::tally() %>%
dplyr::pull("n"))
expect_true(results1$group_level |> unique() |> length() == 2)
expect_true(results1$additional_name |> unique() |> length() == 1)

results <- summariseConceptCounts(list("acetiminophen" = acetiminophen),
cdm = cdm, countBy = "person",
Expand Down

0 comments on commit 98030e8

Please sign in to comment.