Added root endpoint (/) with API info
Browse files- plumber_hf.R +97 -80
plumber_hf.R
CHANGED
|
@@ -1,80 +1,97 @@
|
|
| 1 |
-
library(dampack)
|
| 2 |
-
library(readr)
|
| 3 |
-
library(assertthat)
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
#*
|
| 7 |
-
#*
|
| 8 |
-
#*
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#*
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
library(dampack)
|
| 2 |
+
library(readr)
|
| 3 |
+
library(assertthat)
|
| 4 |
+
library(jsonlite)
|
| 5 |
+
|
| 6 |
+
#* @apiTitle DARTH Health Economic Model API
|
| 7 |
+
#*
|
| 8 |
+
#* @apiDescription This API runs a health economic Markov model (Sick-Sicker)
|
| 9 |
+
#* for probabilistic sensitivity analysis. Hosted on Hugging Face Spaces.
|
| 10 |
+
|
| 11 |
+
#* Welcome page - API info
|
| 12 |
+
#* @get /
|
| 13 |
+
function() {
|
| 14 |
+
list(
|
| 15 |
+
name = "DARTH Health Economic Model API",
|
| 16 |
+
version = "1.0",
|
| 17 |
+
status = "running",
|
| 18 |
+
endpoints = list(
|
| 19 |
+
"GET /" = "This welcome page",
|
| 20 |
+
"GET /health" = "Health check",
|
| 21 |
+
"POST /runDARTHmodel" = "Run the DARTH model with custom parameters"
|
| 22 |
+
)
|
| 23 |
+
)
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
#* Health check endpoint
|
| 28 |
+
#* @get /health
|
| 29 |
+
function() {
|
| 30 |
+
list(status = "healthy", timestamp = Sys.time())
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
#* Run the DARTH model
|
| 34 |
+
#* @serializer csv
|
| 35 |
+
#* @param param_updates gives the replacement values of the editable parameters (JSON string)
|
| 36 |
+
#* @post /runDARTHmodel
|
| 37 |
+
function(param_updates = NULL) {
|
| 38 |
+
|
| 39 |
+
# Source the model functions from local file or GitHub
|
| 40 |
+
model_functions_path <- "/app/model/darth_funcs.R"
|
| 41 |
+
if (file.exists(model_functions_path)) {
|
| 42 |
+
source(model_functions_path)
|
| 43 |
+
} else {
|
| 44 |
+
# Fallback to GitHub if local file not found
|
| 45 |
+
source(paste0("https://raw.githubusercontent.com/",
|
| 46 |
+
"BresMed/plumberHE/main/R/darth_funcs.R"))
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# Read in the CSV containing parameter inputs
|
| 50 |
+
psa_inputs_path <- "/app/parameter_distributions.csv"
|
| 51 |
+
if (!file.exists(psa_inputs_path)) {
|
| 52 |
+
psa_inputs_path <- "parameter_distributions.csv"
|
| 53 |
+
}
|
| 54 |
+
psa_inputs <- as.data.frame(readr::read_csv(psa_inputs_path))
|
| 55 |
+
|
| 56 |
+
# Parse param_updates if provided
|
| 57 |
+
if (!is.null(param_updates) && param_updates != "") {
|
| 58 |
+
# Handle both JSON string and data frame input
|
| 59 |
+
if (is.character(param_updates)) {
|
| 60 |
+
param_updates <- jsonlite::fromJSON(param_updates)
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
# Convert to data frame if needed
|
| 64 |
+
if (!is.data.frame(param_updates)) {
|
| 65 |
+
param_updates <- as.data.frame(param_updates)
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
# For each row of the data-frame containing the variables to be changed...
|
| 69 |
+
for(n in 1:nrow(param_updates)) {
|
| 70 |
+
# Update parameters from API input
|
| 71 |
+
psa_inputs <- overwrite_parameter_value(
|
| 72 |
+
existing_df = psa_inputs,
|
| 73 |
+
parameter = param_updates[n, "parameter"],
|
| 74 |
+
distribution = param_updates[n, "distribution"],
|
| 75 |
+
v1 = param_updates[n, "v1"],
|
| 76 |
+
v2 = param_updates[n, "v2"]
|
| 77 |
+
)
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
# Run the model using the single run-model function
|
| 82 |
+
results <- run_model(psa_inputs)
|
| 83 |
+
|
| 84 |
+
# Check that the model results being returned are the correct dimensions
|
| 85 |
+
assertthat::assert_that(
|
| 86 |
+
all(dim(x = results) == c(1000, 6)),
|
| 87 |
+
class(results) == "data.frame",
|
| 88 |
+
msg = "Dimensions or type of data are incorrect, please check the model code."
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Check that no sensitive data is included in the output
|
| 92 |
+
assertthat::assert_that(
|
| 93 |
+
all(psa_inputs[, 1] %in% as.character(unlist(x = results, recursive = TRUE)) == FALSE)
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
return(results)
|
| 97 |
+
}
|