Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
initial version
  • Loading branch information
jreps committed Oct 5, 2022
0 parents commit fb8c7f1
Show file tree
Hide file tree
Showing 9 changed files with 1,845 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
137 changes: 137 additions & 0 deletions Main.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Copyright 2022 Observational Health Data Sciences and Informatics
#
# This file is part of CohortGeneratorModule
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Module methods -------------------------
getModuleInfo <- function() {
checkmate::assert_file_exists("MetaData.json")
return(ParallelLogger::loadSettingsFromJson("MetaData.json"))
}

getSharedResourceByClassName <- function(sharedResources, className) {
returnVal <- NULL
for (i in 1:length(sharedResources)) {
if (className %in% class(sharedResources[[i]])) {
returnVal <- sharedResources[[i]]
break
}
}
invisible(returnVal)
}

createCohortDefinitionSetFromJobContext <- function(sharedResources, settings) {
cohortDefinitions <- list()
if (length(sharedResources) <= 0) {
stop("No shared resources found")
}
cohortDefinitionSharedResource <- getSharedResourceByClassName(sharedResources = sharedResources,
class = "CohortDefinitionSharedResources")
if (is.null(cohortDefinitionSharedResource)) {
stop("Cohort definition shared resource not found!")
}
cohortDefinitions <- cohortDefinitionSharedResource$cohortDefinitions
if (length(cohortDefinitions) <= 0) {
stop("No cohort definitions found")
}
cohortDefinitionSet <- CohortGenerator::createEmptyCohortDefinitionSet()
for (i in 1:length(cohortDefinitions)) {
cohortJson <- cohortDefinitions[[i]]$cohortDefinition
cohortDefinitionSet <- rbind(cohortDefinitionSet, data.frame(
cohortId = as.integer(cohortDefinitions[[i]]$cohortId),
cohortName = cohortDefinitions[[i]]$cohortName,
json = cohortJson,
stringsAsFactors = FALSE
))
}
return(cohortDefinitionSet)
}

# Module methods -------------------------
execute <- function(jobContext) {
rlang::inform("Validating inputs")
inherits(jobContext, 'list')

if (is.null(jobContext$settings)) {
stop("Analysis settings not found in job context")
}
if (is.null(jobContext$sharedResources)) {
stop("Shared resources not found in job context")
}
if (is.null(jobContext$moduleExecutionSettings)) {
stop("Execution settings not found in job context")
}

workFolder <- jobContext$moduleExecutionSettings$workSubFolder
resultsFolder <- jobContext$moduleExecutionSettings$resultsSubFolder

rlang::inform("Executing PLP")
moduleInfo <- getModuleInfo()

# Creating database details
databaseDetails <- PatientLevelPrediction::createDatabaseDetails(
connectionDetails = jobContext$moduleExecutionSettings$connectionDetails,
cdmDatabaseSchema = jobContext$moduleExecutionSettings$cdmDatabaseSchema,
cohortDatabaseSchema = jobContext$moduleExecutionSettings$workDatabaseSchema,
cdmDatabaseName = jobContext$moduleExecutionSettings$connectionDetailsReference,
cdmDatabaseId = jobContext$moduleExecutionSettings$databaseId,
#tempEmulationSchema = , is there s temp schema specified anywhere?
cohortTable = jobContext$moduleExecutionSettings$cohortTableNames$cohortTable,
outcomeDatabaseSchema = jobContext$moduleExecutionSettings$workDatabaseSchema,
outcomeTable = jobContext$moduleExecutionSettings$cohortTableNames$cohortTable
)

# find where cohortDefinitions are as sharedResources is a list
cohortDefinitionSet <- createCohortDefinitionSetFromJobContext(
sharedResources = jobContext$sharedResources,
settings = jobContext$settings
)

# run the models
PatientLevelPrediction::runMultiplePlp(
databaseDetails = databaseDetails,
modelDesignList = jobContext$settings,
cohortDefinitions = cohortDefinitionSet,
saveDirectory = workFolder
)

# Export the results
rlang::inform("Export data to csv files")

sqliteConnectionDetails <- DatabaseConnector::createConnectionDetails(
dbms = 'sqlite',
server = file.path(workFolder, "sqlite","databaseFile.sqlite")
)

PatientLevelPrediction::extractDatabaseToCsv(
connectionDetails = sqliteConnectionDetails,
databaseSchemaSettings = PatientLevelPrediction::createDatabaseSchemaSettings(
resultSchema = 'main', # sqlite settings
tablePrefix = '', # sqlite settings
targetDialect = 'sqlite',
tempEmulationSchema = NULL
),
csvFolder = file.path(workFolder, 'results'),
fileAppend = NULL
)

# Zip the results
rlang::inform("Zipping csv files")
DatabaseConnector::createZipFile(
zipFile = file.path(resultsFolder, 'results.zip'),
files = file.path(workFolder, 'results')
)


}
6 changes: 6 additions & 0 deletions MetaData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Name": "PatientLevelPredictionModule",
"Version": "0.0.1",
"Dependencies": ["CohortGeneratorModule"],
"TablePrefix": "plp_"
}
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PatientLevelPredictionModule 0.0.1
=======================

Initial module
18 changes: 18 additions & 0 deletions PatientLevelPredictionModule.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# PatientLevelPredictionModule

# Introduction

This is a module for developing patient level prediction modules. The user needs to specify a list of model designs and a model is fit per design.


# Technology

PatientLevelPredictionModule is an R project.

# System requirements

Requires R (version 3.6.0 or higher).

# User Documentation

Coming Soon

# Support

Please use the GitHub bug tracker

# Contributing

Read [here](https://ohdsi.github.io/Hades/contribute.html) how you can contribute to this package.


# Development

This project is being developed in RStudio.

### Development status

Beta
18 changes: 18 additions & 0 deletions SettingsFunctions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
createPatientLevelPredictionModuleSpecifications <- function(
modelDesignList
) {
#analysis <- list()
#for (name in names(formals(createCohortDiagnosticsModuleSpecifications))) {
# analysis[[name]] <- get(name)
#}

specifications <- list(
module = "PatientLevelPredictionModule",
version = "0.0.1",
remoteRepo = "github.com",
remoteUsername = "ohdsi",
settings = modelDesignList
)
class(specifications) <- c("PatientLevelPredictionModuleSpecifications", "ModuleSpecifications")
return(specifications)
}
Loading

0 comments on commit fb8c7f1

Please sign in to comment.