Skip to content

Commit

Permalink
index only if rsqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
egillax committed Feb 6, 2025
1 parent 1752836 commit 6510e1a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 56 deletions.
60 changes: 33 additions & 27 deletions R/AndromedaHelperFunctions.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @file AndromedaHelperFunctions.R
#
# Copyright 2025 Observational Health Data Sciences and Informatics
#
# @file AndromedaHelperFunctions.R
#
# Copyright 2025 Observational Health Data Sciences and Informatics
#
# This file is part of PatientLevelPrediction
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -26,19 +26,23 @@ limitCovariatesToPopulation <- function(covariateData, rowIds) {
)

covariateData$pop <- data.frame(rowId = rowIds)
Andromeda::createIndex(
tbl = covariateData$pop, columnNames = "rowId",
indexName = "pop_rowIds"
)
if (inherits(covariateData, "RSQLiteConnection")) {
Andromeda::createIndex(
tbl = covariateData$pop, columnNames = "rowId",
indexName = "pop_rowIds"

Check warning on line 32 in R/AndromedaHelperFunctions.R

View check run for this annotation

Codecov / codecov/patch

R/AndromedaHelperFunctions.R#L30-L32

Added lines #L30 - L32 were not covered by tests
)
}

on.exit(covariateData$pop <- NULL, add = TRUE)

newCovariateData$covariates <- covariateData$covariates %>%
dplyr::inner_join(covariateData$pop, by = "rowId")
Andromeda::createIndex(
tbl = newCovariateData$covariates, columnNames = "covariateId",
indexName = "covariates_ncovariateIds"
)
if (inherits(newCovariateData, "RSQLiteConnection")) {
Andromeda::createIndex(
tbl = newCovariateData$covariates, columnNames = "covariateId",
indexName = "covariates_ncovariateIds"

Check warning on line 43 in R/AndromedaHelperFunctions.R

View check run for this annotation

Codecov / codecov/patch

R/AndromedaHelperFunctions.R#L41-L43

Added lines #L41 - L43 were not covered by tests
)
}

metaData$populationSize <- length(rowIds)
attr(newCovariateData, "metaData") <- metaData
Expand Down Expand Up @@ -74,21 +78,23 @@ batchRestrict <- function(covariateData, population, sizeN = 10000000) {
batchSize = sizeN
)

Andromeda::createIndex(
tbl = newCovariateData$covariates,
columnNames = "covariateId",
indexName = "covariates_ncovariateIds"
)
Andromeda::createIndex(
tbl = newCovariateData$covariates,
columnNames = "rowId",
indexName = "covariates_rowId"
)
Andromeda::createIndex(
tbl = newCovariateData$covariates,
columnNames = c("covariateId", "covariateValue"),
indexName = "covariates_covariateId_value"
)
if (inherits(newCovariateData, "RSQLiteConnection")) {
Andromeda::createIndex(
tbl = newCovariateData$covariates,
columnNames = "covariateId",
indexName = "covariates_ncovariateIds"

Check warning on line 85 in R/AndromedaHelperFunctions.R

View check run for this annotation

Codecov / codecov/patch

R/AndromedaHelperFunctions.R#L82-L85

Added lines #L82 - L85 were not covered by tests
)
Andromeda::createIndex(
tbl = newCovariateData$covariates,
columnNames = "rowId",
indexName = "covariates_rowId"

Check warning on line 90 in R/AndromedaHelperFunctions.R

View check run for this annotation

Codecov / codecov/patch

R/AndromedaHelperFunctions.R#L87-L90

Added lines #L87 - L90 were not covered by tests
)
Andromeda::createIndex(
tbl = newCovariateData$covariates,
columnNames = c("covariateId", "covariateValue"),
indexName = "covariates_covariateId_value"

Check warning on line 95 in R/AndromedaHelperFunctions.R

View check run for this annotation

Codecov / codecov/patch

R/AndromedaHelperFunctions.R#L92-L95

Added lines #L92 - L95 were not covered by tests
)
}

metaData$populationSize <- nrow(population)
attr(newCovariateData, "metaData") <- metaData
Expand Down
22 changes: 12 additions & 10 deletions R/ExtractData.R
Original file line number Diff line number Diff line change
Expand Up @@ -405,16 +405,18 @@ getPlpData <- function(
rowIdField = "row_id",
covariateSettings = covariateSettings
)
# add indexes for tidyCov + covariate summary
Andromeda::createIndex(covariateData$covariates, c("rowId"),
indexName = "covariates_rowId"
)
Andromeda::createIndex(covariateData$covariates, c("covariateId"),
indexName = "covariates_covariateId"
)
Andromeda::createIndex(covariateData$covariates, c("covariateId", "covariateValue"),
indexName = "covariates_covariateId_value"
)
if (inherits(covariateData, "RSQLiteConnection")) {
# add indexes for tidyCov + covariate summary
Andromeda::createIndex(covariateData$covariates, c("rowId"),
indexName = "covariates_rowId"

Check warning on line 411 in R/ExtractData.R

View check run for this annotation

Codecov / codecov/patch

R/ExtractData.R#L410-L411

Added lines #L410 - L411 were not covered by tests
)
Andromeda::createIndex(covariateData$covariates, c("covariateId"),
indexName = "covariates_covariateId"

Check warning on line 414 in R/ExtractData.R

View check run for this annotation

Codecov / codecov/patch

R/ExtractData.R#L413-L414

Added lines #L413 - L414 were not covered by tests
)
Andromeda::createIndex(covariateData$covariates, c("covariateId", "covariateValue"),
indexName = "covariates_covariateId_value"

Check warning on line 417 in R/ExtractData.R

View check run for this annotation

Codecov / codecov/patch

R/ExtractData.R#L416-L417

Added lines #L416 - L417 were not covered by tests
)
}

if (max(databaseDetails$outcomeIds) != -999) {
ParallelLogger::logTrace("Fetching outcomes from server")
Expand Down
45 changes: 26 additions & 19 deletions R/Predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
#' @examples
#' coefficients <- data.frame(
#' covariateId = c(1002),
#' coefficient = c(0.05))
#' coefficient = c(0.05)
#' )
#' model <- createGlmModel(coefficients, intercept = -2.5)
#' data("simulationProfile")
#' plpData <- simulatePlpData(simulationProfile, n=50)
#' plpData <- simulatePlpData(simulationProfile, n = 50)
#' prediction <- predictPlp(model, plpData, plpData$cohorts)
#' # see the predicted risk values
#' head(prediction)
Expand Down Expand Up @@ -119,8 +120,10 @@ predictPlp <- function(plpModel, plpData, population, timepoint) {

attr(prediction, "metaData") <- metaData
delta <- Sys.time() - start
ParallelLogger::logInfo("Prediction done in: ",
signif(delta, 3), " ", attr(delta, "units"))
ParallelLogger::logInfo(
"Prediction done in: ",
signif(delta, 3), " ", attr(delta, "units")
)
return(prediction)
}

Expand Down Expand Up @@ -168,9 +171,11 @@ applyTidyCovariateData <- function(
temp <- covariateData$covariateRef %>% dplyr::collect()
allCovariateIds <- temp$covariateId
covariateData$includeCovariates <- data.frame(covariateId = allCovariateIds[!allCovariateIds %in% deleteCovariateIds])
Andromeda::createIndex(covariateData$includeCovariates, c("covariateId"),
indexName = "includeCovariates_covariateId"
)
if (inherits(covariateData, "RSQLiteConnection")) {
Andromeda::createIndex(covariateData$includeCovariates, c("covariateId"),
indexName = "includeCovariates_covariateId"

Check warning on line 176 in R/Predict.R

View check run for this annotation

Codecov / codecov/patch

R/Predict.R#L175-L176

Added lines #L175 - L176 were not covered by tests
)
}
on.exit(covariateData$includeCovariates <- NULL, add = TRUE)
# ---

Expand All @@ -187,11 +192,12 @@ applyTidyCovariateData <- function(
}
on.exit(covariateData$maxes <- NULL, add = TRUE)

# --- added for speed
Andromeda::createIndex(covariateData$maxes, c("covariateId"),
indexName = "maxes_covariateId"
)
# ---
if (inherits(covariateData, "RSQLiteConnection")) {
# --- added for speed
Andromeda::createIndex(covariateData$maxes, c("covariateId"),
indexName = "maxes_covariateId"

Check warning on line 198 in R/Predict.R

View check run for this annotation

Codecov / codecov/patch

R/Predict.R#L197-L198

Added lines #L197 - L198 were not covered by tests
)
} # ---

newCovariateData$covariates <- covariateData$covariates %>%
dplyr::inner_join(covariateData$includeCovariates, by = "covariateId") %>% # added as join
Expand All @@ -208,14 +214,15 @@ applyTidyCovariateData <- function(
newCovariateData$covariateRef <- covariateData$covariateRef %>%
dplyr::inner_join(covariateData$includeCovariates, by = "covariateId")

# adding index for restrict to pop
Andromeda::createIndex(
newCovariateData$covariates,
c("rowId"),
indexName = "ncovariates_rowId"
)


if (inherits(newCovariateData, "RSQLiteConnection")) {
# adding index for restrict to pop
Andromeda::createIndex(
newCovariateData$covariates,
c("rowId"),
indexName = "ncovariates_rowId"

Check warning on line 223 in R/Predict.R

View check run for this annotation

Codecov / codecov/patch

R/Predict.R#L220-L223

Added lines #L220 - L223 were not covered by tests
)
}
class(newCovariateData) <- "CovariateData"

delta <- Sys.time() - start
Expand Down

0 comments on commit 6510e1a

Please sign in to comment.