Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor test files #1133

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ffa9602
add option to use Achilles for concept counts
ablack3 Jun 12, 2024
bbd89a3
Merge branch 'achilles_concept_counts' into v3.3.0
ablack3 Sep 13, 2024
54a450e
refactor setup test file to more easily allow for running tests on on…
ablack3 Sep 13, 2024
87cfa33
switch to base pipe in tests to avoid loading dplyr when running tests
ablack3 Sep 13, 2024
be84320
fix database names in setup test file
ablack3 Sep 13, 2024
a98e556
import and re-export getCohortCounts from CohortGenerator.
ablack3 Sep 15, 2024
47497eb
remove unused parameter. inline the checkIfCohortInstatiated function…
ablack3 Sep 15, 2024
0ebc368
remove redundant min cell count supression. Don't assume we have writ…
ablack3 Sep 15, 2024
0f1cfe5
use "achillesDatabaseSchema" for the location of the achilles tables.…
ablack3 Sep 15, 2024
6565c7d
rename the file runDiagnostics to executeDiagnostics to match the mai…
ablack3 Sep 15, 2024
69b36d2
remove redundant parameter instantiatedCohorts. All cohorts in cohort…
ablack3 Sep 15, 2024
204151e
refactor makeDataExportable and remove the enforceMinCellValueInDataf…
ablack3 Sep 15, 2024
e635d78
add parameter to getResultsDataModelSpecification to return just the …
ablack3 Sep 15, 2024
79295d8
move has data to private.R
ablack3 Sep 15, 2024
1d24b30
remove sql files we no longer need.
ablack3 Sep 15, 2024
e813731
use achilles_database_schema for the location of the achilles tables.
ablack3 Sep 15, 2024
b6dfe12
update documentation
ablack3 Sep 15, 2024
73e6e86
create the concept table in executeDiagnostics
ablack3 Sep 15, 2024
7f36ad4
update setup test file.
ablack3 Sep 15, 2024
4e1cbaf
add parameter for workDatabaseSchema where concept counts table can b…
ablack3 Sep 16, 2024
790239a
refactor againstCdm test file to use loop over databases
ablack3 Sep 17, 2024
724a99d
fix sql errors
ablack3 Sep 17, 2024
db41bc3
export the createConceptCounts table
ablack3 Sep 17, 2024
2ff0deb
undo pipe switch just to reduce the files changed.
ablack3 Sep 17, 2024
44ee362
fix setup file
ablack3 Sep 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(runCohortRelationshipDiagnostics)
export(runCohortTimeSeriesDiagnostics)
export(uploadResults)
import(DatabaseConnector)
importFrom(CohortGenerator,getCohortCounts)
importFrom(CohortGenerator,getCohortTableNames)
importFrom(FeatureExtraction,createDefaultCovariateSettings)
importFrom(FeatureExtraction,createTemporalCovariateSettings)
Expand Down
6 changes: 6 additions & 0 deletions R/CohortDiagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@
#' @importFrom methods is
#' @importFrom FeatureExtraction createDefaultCovariateSettings createTemporalCovariateSettings
NULL

# re-export getCohortCounts from CohortGenerator

#' @importFrom CohortGenerator getCohortCounts
#' @export
CohortGenerator::getCohortCounts
127 changes: 0 additions & 127 deletions R/CohortLevelDiagnostics.R

This file was deleted.

18 changes: 0 additions & 18 deletions R/ConceptIds.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

createConceptTable <- function(connection, tempEmulationSchema) {
ParallelLogger::logTrace("Creating concept ID table for tracking concepts used in diagnostics")
sql <-
SqlRender::loadRenderTranslateSql(
"CreateConceptIdTable.sql",
packageName = utils::packageName(),
dbms = connection@dbms,
tempEmulationSchema = tempEmulationSchema,
table_name = "#concept_ids"
)
DatabaseConnector::executeSql(
connection = connection,
sql = sql,
progressBar = FALSE,
reportOverallTime = FALSE
)
}

exportConceptInformation <- function(connection = NULL,
cdmDatabaseSchema,
tempEmulationSchema,
Expand Down
145 changes: 120 additions & 25 deletions R/ConceptSetUtils.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,125 @@
# See the License for the specific language governing permissions and
# limitations under the License.

#' Create the concept counts table used by CohortDiagnostics
#'
#' CohortDiagnostics requires the record and person counts for all concepts in the CDM.
#' createConceptCountsTable will create this table. To speed up execution pre-computed
#' Achilles data can be used in the creation of the table. This table can also be persisted
#' across executions of CohortDiagnostics and does not need to be recreated with each run.
#'
#' @template Connection
#' @template CdmDatabaseSchema
#' @template TempEmulationSchema
#' @param conceptCountsDatabaseSchema character. The schema where the concept counts table should be created.
#' @param conceptCountsTable character. The name of concept counts table. If the name starts with # then a
#' temporary table will be created.
#' @param achillesDatabaseSchema character. Schema where Achilles tables are stored in the database.
#' If provided and the correct analysis IDs exist
#' then the Achilles results will be used to create the concept counts table.
#' @param overwrite logical. Should the table be overwritten if it already exists? TRUE or FALSE (default)
#'
#' @return
#' @export
#'
#' @examples
#' \dontrun{
#' connectionDetails <- Eunomia::getEunomiaConnectionDetails()
#' connection <- DatabaseConnector::connect(connectionDetails)
#'
#' createConceptCountsTable(connection = connection,
#' cdmDatabaseSchema = "main",
#' conceptCountsDatabaseSchema = "main",
#' conceptCountsTable = "concept_counts",
#' achillesDatabaseSchema = NULL,
#' overwrite = FALSE)
#'
#' DatabaseConnector::disconnect(connection)
#' }
createConceptCountsTable <- function(connection = NULL,
cdmDatabaseSchema,
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema"),
conceptCountsDatabaseSchema = NULL,
conceptCountsTable = "concept_counts",
achillesDatabaseSchema = NULL,
overwrite = FALSE) {browser()

dbRegex <- "^[a-zA-Z][a-zA-Z0-9_]*$" # regex for letters, numbers, and _ but must start with a letter
checkmate::assertClass(connection, "DatabaseConnectorConnection")
checkmate::assertCharacter(cdmDatabaseSchema, len = 1, min.chars = 1, any.missing = FALSE, pattern = dbRegex)
checkmate::assertCharacter(tempEmulationSchema, len = 1, min.chars = 1, any.missing = FALSE, pattern = dbRegex, null.ok = TRUE)
checkmate::assertCharacter(achillesDatabaseSchema, len = 1, min.chars = 1, any.missing = FALSE, pattern = dbRegex, null.ok = TRUE)
checkmate::assertLogical(overwrite, any.missing = FALSE, len = 1)
checkmate::assertCharacter(conceptCountsTable, len = 1, min.chars = 1, any.missing = FALSE)

# check if table exists, has expected column names, and at least a few rows

conceptCountsTableIsTemp <- (substr(conceptCountsTable, 1, 1) == "#")
if (conceptCountsTableIsTemp) {
checkmate::assertCharacter(conceptCountsDatabaseSchema, len = 1, min.chars = 1, any.missing = FALSE, pattern = dbRegex)
}

if (isFALSE(conceptCountsTableIsTemp) && isFALSE(overwrite)) {

tablesInSchema <- DatabaseConnector::getTableNames(connection, databaseSchema = conceptCountsDatabaseSchema, cast = "lower")

if (tolower(conceptCountsTable) %in% tablesInSchema) {
# check that the table has the correct column names and at least a few rows
result <- DatabaseConnector::renderTranslateQuerySql(
connection = connection,
sql = "select top 5 * from @schema.@concept_counts_table;",
tempEmulationSchema = tempEmulationSchema,
schema = conceptCountsDatabaseSchema,
concept_counts_table = conceptCountsTable
)

columnsExist <- all(c("concept_id", "concept_count", "concept_subjects") %in% tolower(colnames(result)))

if (columnsExist && (nrow(result) > 3)) {
ParallelLogger::logInfo("Concept counts table already exists and is not being overwritten")
return(invisible(NULL))
}
}
}

ParallelLogger::logInfo("Creating concept counts table")

useAchilles <- FALSE
if (!is.null(achillesDatabaseSchema)) {
# check that achilles tables exist
tablesInAchillesSchema <- DatabaseConnector::getTableNames(connection, databaseSchema = achillesDatabaseSchema, cast = "lower")
if ("achilles_results" %in% tablesInAchillesSchema) {
ParallelLogger::logInfo("Using achilles_results table to create the concept_counts table")
useAchilles <- TRUE
} else {
ParallelLogger::logInfo("achilles_results table was not found in the achilles schema")
}
}

sql <- SqlRender::readSql(system.file("sql", "sql_server", "CreateConceptCountTable.sql", package = "CohortDiagnostics"))
sql <- SqlRender::render(sql,
tempEmulationSchema = tempEmulationSchema,
cdm_database_schema = cdmDatabaseSchema,
work_database_schema = conceptCountsDatabaseSchema,
concept_counts_table = conceptCountsTable,
table_is_temp = conceptCountsTableIsTemp,
use_achilles = useAchilles,
achilles_database_schema = achillesDatabaseSchema)
sql <- SqlRender::translate(sql, DatabaseConnector::dbms(connection))
DatabaseConnector::executeSql(connection = connection, sql = sql)

n <- DatabaseConnector::renderTranslateQuerySql(
connection = connection,
sql = "select count(*) as n from @schema.@table;",
schema = conceptCountsDatabaseSchema,
table = conceptCountsTable
)

ParallelLogger::logInfo(paste("Concept counts table created with", n[[1]], "rows"))
if (n == 0) ParallelLogger::logWarn("concept_counts table has zeros rows!")
return(invisible(NULL))
}

.findOrphanConcepts <- function(connectionDetails = NULL,
connection = NULL,
cdmDatabaseSchema,
Expand Down Expand Up @@ -76,28 +195,4 @@
return(orphanConcepts)
}

createConceptCountsTable <- function(connectionDetails = NULL,
connection = NULL,
cdmDatabaseSchema,
tempEmulationSchema = NULL,
conceptCountsDatabaseSchema = cdmDatabaseSchema,
conceptCountsTable = "concept_counts",
conceptCountsTableIsTemp = FALSE) {
ParallelLogger::logInfo("Creating internal concept counts table")
if (is.null(connection)) {
connection <- DatabaseConnector::connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(connection))
}
sql <-
SqlRender::loadRenderTranslateSql(
"CreateConceptCountTable.sql",
packageName = utils::packageName(),
dbms = connection@dbms,
tempEmulationSchema = tempEmulationSchema,
cdm_database_schema = cdmDatabaseSchema,
work_database_schema = conceptCountsDatabaseSchema,
concept_counts_table = conceptCountsTable,
table_is_temp = conceptCountsTableIsTemp
)
DatabaseConnector::executeSql(connection, sql)
}

21 changes: 8 additions & 13 deletions R/ConceptSets.R
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,17 @@ runConceptSetDiagnostics <- function(connection,
runBreakdownIndexEvents,
exportFolder,
minCellCount,
conceptCountsDatabaseSchema = cdmDatabaseSchema,
conceptCountsDatabaseSchema = NULL,
conceptCountsTable = "concept_counts",
conceptCountsTableIsTemp = FALSE,
cohortDatabaseSchema,
cohortTable,
useExternalConceptCountsTable = FALSE,
incremental = FALSE,
conceptIdTable = NULL,
recordKeepingFile) {
recordKeepingFile,
useAchilles,
resultsDatabaseSchema) {
ParallelLogger::logInfo("Starting concept set diagnostics")
startConceptSetDiagnostics <- Sys.time()
subset <- dplyr::tibble()
Expand Down Expand Up @@ -476,7 +478,9 @@ runConceptSetDiagnostics <- function(connection,
tempEmulationSchema = tempEmulationSchema,
conceptCountsDatabaseSchema = conceptCountsDatabaseSchema,
conceptCountsTable = conceptCountsTable,
conceptCountsTableIsTemp = conceptCountsTableIsTemp
conceptCountsTableIsTemp = conceptCountsTableIsTemp,
useAchilles = useAchilles,
resultsDatabaseSchema = resultsDatabaseSchema
)
}
)
Expand Down Expand Up @@ -821,17 +825,8 @@ runConceptSetDiagnostics <- function(connection,
split(subsetBreakdown, subsetBreakdown$cohortId),
getCohortIndexEventBreakdown
)

data <- dplyr::bind_rows(data)
if (nrow(data) > 0) {
data <- data %>%
dplyr::mutate(databaseId = !!databaseId)
data <-
enforceMinCellValue(data, "conceptCount", minCellCount)
if ("subjectCount" %in% colnames(data)) {
data <-
enforceMinCellValue(data, "subjectCount", minCellCount)
}
}

data <- makeDataExportable(
x = data,
Expand Down
Loading
Loading