Skip to content

Commit

Permalink
Merge pull request #299 from OHDSI/297-sample-function
Browse files Browse the repository at this point in the history
sample option
  • Loading branch information
cecicampanile authored Jan 16, 2025
2 parents 02ee7fc + bb21545 commit 6048e96
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 60 deletions.
5 changes: 1 addition & 4 deletions R/summariseClinicalRecords.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#' @param ageGroup A list of age groups to stratify results by.
#' @param sex Boolean variable. Whether to stratify by sex (TRUE) or not
#' (FALSE).
#' @param sample An integer to sample the tables to only that number of records.
#' If NULL no sample is done.
#' @param dateRange A list containing the minimum and the maximum dates
#' defining the time range within which the analysis is performed.
#' @return A summarised_result object.
Expand Down Expand Up @@ -57,7 +55,6 @@ summariseClinicalRecords <- function(cdm,
typeConcept = TRUE,
sex = FALSE,
ageGroup = NULL,
sample = NULL,
dateRange = NULL) {
# Initial checks ----
cdm <- omopgenerics::validateCdmArgument(cdm)
Expand All @@ -78,7 +75,7 @@ summariseClinicalRecords <- function(cdm,
omopgenerics::assertLogical(typeConcept, length = 1)
omopgenerics::assertLogical(sex, length = 1)
ageGroup <- omopgenerics::validateAgeGroupArgument(ageGroup, multipleAgeGroup = FALSE)
omopgenerics::assertNumeric(sample, integerish = TRUE, min = 1, null = TRUE, length = 1)


# warnings for observation_period
warnStandardConcept <- standardConcept & !missing(standardConcept)
Expand Down
4 changes: 0 additions & 4 deletions R/summariseConceptSetCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#' @param sex TRUE or FALSE. If TRUE code use will be summarised by sex.
#' @param ageGroup A list of ageGroup vectors of length two. Code use will be
#' thus summarised by age groups.
#' @param sample An integer to sample the tables in the cdm object to only that number of records.
#' If NULL no sample is done.
#' @param dateRange A list containing the minimum and the maximum dates
#' defining the time range within which the analysis is performed.
#' @return A summarised_result object with results overall and, if specified, by
Expand Down Expand Up @@ -39,7 +37,6 @@ summariseConceptSetCounts <- function(cdm,
interval = "overall",
sex = FALSE,
ageGroup = NULL,
sample = NULL,
dateRange = NULL) {
# initial check
cdm <- omopgenerics::validateCdmArgument(cdm)
Expand All @@ -49,7 +46,6 @@ summariseConceptSetCounts <- function(cdm,
omopgenerics::assertLogical(sex, length = 1)
ageGroup <- omopgenerics::validateAgeGroupArgument(ageGroup)
dateRange <- validateStudyPeriod(cdm, dateRange)
omopgenerics::assertNumeric(sample, integerish = TRUE, min = 0, null = TRUE, length = 1)
conceptSet <- omopgenerics::validateConceptSetArgument(conceptSet = conceptSet)

countBy[countBy == "record"] <- "records"
Expand Down
14 changes: 2 additions & 12 deletions R/summariseInternal.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,10 @@ sampleOmopTable <- function(x, sample, name) {
if (is.infinite(sample)) return(x)
if (x |> dplyr::tally() |> dplyr::pull() <= sample) return(x)

cdm <- omopgenerics::cdmReference(x)
id <- omopgenerics::omopColumns(table = omopgenerics::tableName(x), field = "unique_id")
idTibble <- x |>
dplyr::pull(dplyr::all_of(id)) |>
base::sample(size = sample) |>
list() |>
rlang::set_names(id) |>
dplyr::as_tibble()
idName <- "ids_sample"
cdm <- omopgenerics::insertTable(cdm = cdm, name = idName, table = idTibble)
x <- x |>
dplyr::inner_join(cdm[[idName]], by = id) |>
dplyr::slice_sample(n = sample) |>
dplyr::compute(name = name, temporary = FALSE)
omopgenerics::dropSourceTable(cdm = cdm, name = idName)

return(x)
}
addStratifications <- function(x, indexDate, sex, ageGroup, interval, intervalName, name) {
Expand Down
4 changes: 4 additions & 0 deletions R/summariseRecordCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ summariseRecordCount <- function(cdm,
# prefix for temp tables
prefix <- omopgenerics::tmpPrefix()

# sample table
omopTable <- omopTable |>
sampleOmopTable(sample = sample, name = omopgenerics::uniqueTableName(prefix))

startDate <- omopgenerics::omopColumns(table = table, field = "start_date")

# Incidence counts
Expand Down
1 change: 0 additions & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
0 errors | 0 warnings | 1 note

* This is a new release.
* There is a change of maintainer: Marta Alcalde-Herraiz -> Cecilia Campanile
4 changes: 0 additions & 4 deletions man/summariseClinicalRecords.Rd

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

4 changes: 0 additions & 4 deletions man/summariseConceptSetCounts.Rd

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

17 changes: 1 addition & 16 deletions tests/testthat/test-summariseClinicalRecords.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,6 @@ test_that("dateRange argument works", {

})

test_that("sample argument works", {
skip_on_cran()
# Load mock database ----
cdm <- cdmEunomia()

expect_no_error(x<-summariseClinicalRecords(cdm,"drug_exposure", sample = 50))
expect_no_error(y<-summariseClinicalRecords(cdm,"drug_exposure"))
n <- cdm$drug_exposure |>
dplyr::tally()|>
dplyr::pull(n)
expect_no_error(z<-summariseClinicalRecords(cdm,"drug_exposure",sample = n))
expect_equal(y,z)
PatientProfiles::mockDisconnect(cdm = cdm)
})

test_that("tableClinicalRecords() works", {
skip_on_cran()
Expand Down Expand Up @@ -327,8 +313,7 @@ test_that("no tables created", {
ageGroup = list(c(0,17),
c(18,65),
c(66, 100)),
dateRange = as.Date(c("2012-01-01", "2018-01-01")),
sample = 100)
dateRange = as.Date(c("2012-01-01", "2018-01-01")))

endNames <- CDMConnector::listSourceTables(cdm)

Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-summariseConceptIdCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ test_that("sample argument works", {
dplyr::pull(n)
expect_no_error(z<-summariseConceptIdCounts(cdm,"drug_exposure",sample = n))
expect_equal(y |> sortTibble(), z |> sortTibble())
expect_equal(summariseConceptIdCounts(cdm,"drug_exposure", sample = 1) |>
dplyr::filter(.data$estimate_name == "Number records") |>
dplyr::pull(.data$estimate_value) |>
as.integer(), 1L)

PatientProfiles::mockDisconnect(cdm = cdm)
})

Expand Down
15 changes: 0 additions & 15 deletions tests/testthat/test-summariseConceptSetCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -559,21 +559,6 @@ test_that("dateRange argument works", {
PatientProfiles::mockDisconnect(cdm = cdm)
})

test_that("sample argument works", {
skip_on_cran()
# Load mock database ----
cdm <- cdmEunomia()

expect_no_error(d<-summariseConceptSetCounts(cdm,conceptSet = list("zoster vax" = c(40213260)), sample = 50))
expect_no_error(y<-summariseConceptSetCounts(cdm,conceptSet = list("zoster vax" = c(40213260))))
n <- cdm$drug_exposure |>
dplyr::tally()|>
dplyr::pull(n)
expect_no_error(z<-summariseConceptSetCounts(cdm,conceptSet = list("zoster vax" = c(40213260)),sample = n))
expect_equal(y,z)
PatientProfiles::mockDisconnect(cdm = cdm)
})

test_that("interval argument works", {
skip_on_cran()
# Load mock database ----
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-summariseRecordCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ test_that("sample argument works", {
dplyr::pull(n)
expect_no_error(z<-summariseRecordCount(cdm,"drug_exposure",sample = n))
expect_equal(y,z)
expect_equal(summariseRecordCount(cdm,"drug_exposure", sample = 1) |>
dplyr::filter(.data$variable_name == "Number records") |>
dplyr::pull(.data$estimate_value) |>
as.integer(), 1L)
PatientProfiles::mockDisconnect(cdm = cdm)
})

Expand Down

0 comments on commit 6048e96

Please sign in to comment.