Skip to content

Commit

Permalink
Fixes for sampled usage and docs - changed api
Browse files Browse the repository at this point in the history
  • Loading branch information
azimov committed Aug 26, 2024
1 parent cbf145f commit e10ac60
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ License: Apache License
VignetteBuilder: knitr
URL: https://ohdsi.github.io/CohortDiagnostics, https://github.com/OHDSI/CohortDiagnostics
BugReports: https://github.com/OHDSI/CohortDiagnostics/issues
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Encoding: UTF-8
Language: en-US
StagedInstall: no
Expand Down
19 changes: 11 additions & 8 deletions R/CohortLevelDiagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ getCohortCounts <- function(connectionDetails = NULL,
)
counts <-
DatabaseConnector::querySql(connection, sql, snakeCaseToCamelCase = TRUE) %>%
tidyr::tibble()
tidyr::tibble()

if (length(cohortIds) > 0) {
cohortIdDf <- tidyr::tibble(cohortId = as.numeric(cohortIds))
Expand Down Expand Up @@ -97,7 +97,8 @@ computeCohortCounts <- function(connection,
cohorts,
exportFolder,
minCellCount,
databaseId) {
databaseId,
writeResult = TRUE) {
ParallelLogger::logInfo("Counting cohort records and subjects")
cohortCounts <- getCohortCounts(
connection = connection,
Expand All @@ -117,11 +118,13 @@ computeCohortCounts <- function(connection,
databaseId = databaseId
)

writeToCsv(
data = cohortCounts,
fileName = file.path(exportFolder, "cohort_count.csv"),
incremental = FALSE,
cohortId = cohorts$cohortId
)
if (writeResult) {
writeToCsv(
data = cohortCounts,
fileName = file.path(exportFolder, "cohort_count.csv"),
incremental = FALSE,
cohortId = cohorts$cohortId
)
}
return(cohortCounts)
}
3 changes: 2 additions & 1 deletion R/Incremental.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ writeToCsv <- function(data, fileName, incremental = FALSE, ...) {
UseMethod("writeToCsv", data)
}


#' @noRd
writeToCsv.default <- function(data, fileName, incremental = FALSE, ...) {
colnames(data) <- SqlRender::camelCaseToSnakeCase(colnames(data))
if (incremental) {
Expand Down Expand Up @@ -186,6 +186,7 @@ writeToCsv.default <- function(data, fileName, incremental = FALSE, ...) {
}
}

#'@noRd
writeToCsv.tbl_Andromeda <-
function(data, fileName, incremental = FALSE, ...) {
if (incremental && file.exists(fileName)) {
Expand Down
3 changes: 2 additions & 1 deletion R/Private.R
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,9 @@ getPrefixedTableNames <- function(tablePrefix) {
return(resultList)
}

#' @noRd

#' Internal utility function for logging execution of variables
#' @noRd
timeExecution <- function(exportFolder,
taskName,
cohortIds = NULL,
Expand Down
27 changes: 20 additions & 7 deletions R/RunDiagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ getDefaultCovariateSettings <- function() {
#' @param seedArgs List. Additional arguments to pass to the sampling function.
#' This can be used to control aspects of the sampling process beyond the seed and sample size.
#'
#' @param sampleIdentifierExpression Character. An expression that generates unique identifiers for each sample.
#' This expression can use the variables 'cohortId' and 'seed'.
#' Default is "cohortId * 1000 + seed", which ensures unique identifiers
#' as long as there are fewer than 1000 cohorts.

#' @examples
#' \dontrun{
#' # Load cohorts (assumes that they have already been instantiated)
Expand Down Expand Up @@ -857,8 +852,15 @@ executeDiagnostics <- function(cohortDefinitionSet,

feCohortDefinitionSet <- cohortDefinitionSet
feCohortTable <- cohortTable
feCohortCounts <- cohortCounts

if (runFeatureExtractionOnSample & !isTRUE(attr(cohortDefinitionSet, "isSampledCohortDefinition"))) {
cohortTableNames$cohortSampleTable <- paste0(cohortTableNames$cohortTable, "_cd_sample")
CohortGenerator::createCohortTables(connection = connection,
cohortTableNames = cohortTableNames,
cohortDatabaseSchema = cohortDatabaseSchema,
incremental = TRUE)

feCohortTable <- cohortTableNames$cohortSampleTable
feCohortDefinitionSet <-
CohortGenerator::sampleCohortDefinitionSet(
Expand All @@ -870,10 +872,21 @@ executeDiagnostics <- function(cohortDefinitionSet,
n = sampleN,
seed = seed,
seedArgs = seedArgs,
identifierExpression = sampleIdentifierExpression,
identifierExpression = "cohortId",
incremental = incremental,
incrementalFolder = incrementalFolder
)

feCohortCounts <- computeCohortCounts(
connection = connection,
cohortDatabaseSchema = cohortDatabaseSchema,
cohortTable = cohortTableNames$cohortSampleTable,
cohorts = feCohortDefinitionSet,
exportFolder = exportFolder,
minCellCount = minCellCount,
databaseId = databaseId,
writeResult = FALSE
)
}


Expand All @@ -888,7 +901,7 @@ executeDiagnostics <- function(cohortDefinitionSet,
tempEmulationSchema = tempEmulationSchema,
cdmVersion = cdmVersion,
cohorts = feCohortDefinitionSet,
cohortCounts = cohortCounts,
cohortCounts = feCohortCounts,
minCellCount = minCellCount,
instantiatedCohorts = instantiatedCohorts,
incremental = incremental,
Expand Down
13 changes: 4 additions & 9 deletions man/executeDiagnostics.Rd

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

0 comments on commit e10ac60

Please sign in to comment.