Skip to content

Commit

Permalink
Merge pull request #14 from esqLABS/simulation-parameterization
Browse files Browse the repository at this point in the history
Simulation parameterization
  • Loading branch information
Felixmil authored Nov 14, 2024
2 parents 8b5faa5 + 2edd37a commit 21d2bc7
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 31 deletions.
6 changes: 6 additions & 0 deletions app/logic/constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ box::use(config)
#' @export
ORGAN_TAB <- config$get("tab_items", config = "organ_tab")

#' @export
GENDERS <- c("male", "female")

#' @export
POPULATIONS <- c("European", "White American", "Black American", "Mexican American", "Asian", "Japanese")

#' @export
COFFEE_TYPES <- list(
`Espresso` = list(caffeine = 64, water = 30),
Expand Down
34 changes: 27 additions & 7 deletions app/logic/individual.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ box::use(
dplyr[case_match]
)

box::use(
app / logic / constants[POPULATIONS, GENDERS],
)

set_individual <- function(simulation, user_data) {
message("Applying individual characteristics to the simulation")
# Create individual
Expand Down Expand Up @@ -38,20 +42,21 @@ set_individual <- function(simulation, user_data) {
}

create_individual <- function(user_data) {
unit_system <- user_data$unit
ind_carac <- ospsuite::createIndividualCharacteristics(
species = "Human",
population = translate_population(user_data$ethnicity),
gender = translate_gender(user_data$gender),
age = as.double(user_data$age),
weight = as.double(user_data$weight),
height = as.double(convert_height(user_data$height)),
weight = convert_weight(user_data$weight, unit_system),
height = convert_height(user_data$height, unit_system),
seed = 42
)
return(ospsuite::createIndividual(ind_carac))
}

translate_population <- function(population) {
rlang::arg_match(population, c("European", "White American", "Black American", "Mexican American", "Asian", "Japanese"))
rlang::arg_match(population, POPULATIONS)
case_match(
population,
"European" ~ ospsuite::HumanPopulation$European_ICRP_2002,
Expand All @@ -64,15 +69,30 @@ translate_population <- function(population) {
}

translate_gender <- function(gender) {
rlang::arg_match(gender, c("male", "female"))
rlang::arg_match(gender, GENDERS)
case_match(
gender,
"male" ~ ospsuite::Gender$Male,
"female" ~ ospsuite::Gender$Female
)
}
convert_height <- function(height, unit_system) {
height <- if (unit_system == "imperial") {
# feet to cm
height * 30.48
} else {
# meters to cm
height * 100
}
return(as.double(height))
}

convert_height <- function(height) {
# meters to cm
return(height * 100)
convert_weight <- function(weight, unit_system) {
weight <- if (unit_system == "imperial") {
# lbs to kg
weight * 0.453592
} else {
weight
}
return(as.double(weight))
}
18 changes: 11 additions & 7 deletions app/logic/intakes.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set_intakes <- function(simulation, intakes) {
# Get all relevant paths
time_paths <- purrr::map(ospsuite::getAllParametersMatching("**|Start time", simulation), "path")
dose_paths <- purrr::map(ospsuite::getAllParametersMatching("**|Dose", simulation), "path")
volume_paths <- purrr::map(ospsuite::getAllParametersMatching("**|Volume of water/body weight", simulation), "path")
volume_paths <- purrr::map(ospsuite::getAllParametersMatching("Applications|Coffee Drinks|Solution|Application_*|ProtocolSchemaItem|Amount of water", simulation), "path")

# Keep only enabled intakes
enabled_intakes <- purrr::keep(intakes, ~ .x$selected)
Expand All @@ -20,9 +20,8 @@ set_intakes <- function(simulation, intakes) {
# Add the caffeine and water content to the intake
# Set the intake in the simulation
for (i in seq_along(enabled_intakes)) {

caffein_dose <- enabled_intakes[[i]]$type[[1]]$caffeine
water_volume <-enabled_intakes[[i]]$type[[1]]$water
water_volume <- enabled_intakes[[i]]$type[[1]]$water
time <- enabled_intakes[[i]]$time

# Set the caffeine dose
Expand All @@ -42,11 +41,16 @@ set_intakes <- function(simulation, intakes) {
)

# Set the volume of water
# TODO
ospsuite::setParameterValuesByPath(
parameterPaths = volume_paths[[i]],
values = water_volume,
unit = "ml",
simulation = simulation
)
}

# Set remaining intakes to 0
for (j in (i+1):length(dose_paths)) {
for (j in (i + 1):length(dose_paths)) {
if (j > length(dose_paths)) break

ospsuite::setParameterValuesByPath(
Expand All @@ -66,14 +70,14 @@ set_intakes <- function(simulation, intakes) {
ospsuite::setParameterValuesByPath(
parameterPaths = volume_paths[[j]],
values = 0,
unit = "l/kg",
unit = "ml",
simulation = simulation
)
}
}


convert_time_to_min <- function(time){
convert_time_to_min <- function(time) {
# Convert time to minutes from midnight
time <- as.POSIXct(time, format = "%H:%M")

Expand Down
7 changes: 2 additions & 5 deletions app/logic/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ box::use(
ospsuite
)

get_plot <- function(simulation_results){
get_plot <- function(simulation_results) {
message("Generating time profile plot")
dc <- ospsuite::DataCombined$new()

Expand All @@ -14,8 +14,5 @@ get_plot <- function(simulation_results){

dc$addSimulationResults(simulation_results[[1]])

return(ospsuite::plotIndividualTimeProfile(dc,defaultPlotConfiguration = pc))

return(ospsuite::plotIndividualTimeProfile(dc, defaultPlotConfiguration = pc))
}


12 changes: 10 additions & 2 deletions app/logic/simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ box::use(
)


load_simulation <- function(){
load_simulation <- function() {
message("Initializing Simulation")
simulation <- ospsuite::loadSimulation("app/logic/coffee-sim.pkml")
ospsuite::setOutputInterval(simulation, 0, 24*60-1, resolution = 1/2)
ospsuite::setOutputInterval(simulation, 0, 24 * 60 - 1, resolution = 1 / 2)
ospsuite::setOutputs(
c(
"Organism|PeripheralVenousBlood|Caffeine|Plasma (Peripheral Venous Blood)",
"Organism|Brain|Intracellular|Caffeine|Concentration",
"Organism|Heart|Intracellular|Caffeine|Concentration"
),
simulation
)
return(simulation)
}
1 change: 0 additions & 1 deletion app/main.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,5 @@ server <- function(id) {
app_data$destroy_intro_screen <- TRUE
result_screen$server("result_screen", app_data)
})

})
}
11 changes: 5 additions & 6 deletions app/view/intro_screen.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ box::use(
shiny[moduleServer, NS, tagList, observeEvent],
)
box::use(
app/logic/constants[COFFEE_TYPES],
app/logic/constants[COFFEE_TYPES, POPULATIONS],
app/view/react[intro_screen_page], # Import the component.
)

Expand All @@ -14,10 +14,10 @@ ui <- makeModuleUIDestroyable(
tagList(
intro_screen_page(
id = ns("stepper"),
ethinicity_options = c("European", "White American", "Black American","Mexican American", "Asian", "Japanese"),
ethinicity_options = POPULATIONS,
metabolism_options = c("low", "normal", "high"),
coffee_type_options = COFFEE_TYPES,
unit="metric",
unit = "metric",
unit_options = c("metric", "imperial"),
init_shiny_data = list(
ethnicity = "European",
Expand Down Expand Up @@ -60,21 +60,20 @@ server <- makeModuleServerDestroyable(
# Observe User Data changes
observeEvent(input$stepper_userdata, {
app_data$user_data <- input$stepper_userdata
print(app_data$user_data) #! dev
print(app_data$user_data) # ! dev
})

# Observe Intake Data changes
observeEvent(input$stepper_intake, {
app_data$intake_data <- input$stepper_intake$intakes
app_data$destroy_intro_screen <- TRUE
print(app_data$intake_data) #! dev
print(app_data$intake_data) # ! dev
})

# Remove loader when calcularion finished
observeEvent(app_data$destroy_intro_screen, {
destroyModule()
})

})
}
)
7 changes: 4 additions & 3 deletions app/view/result_screen.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ box::use(
ui <- function(id) {
ns <- NS(id)
tagList(
uiOutput(ns("result_screen")),
fluidRow(column(plotOutput(ns("plot")), width = 10, offset = 1))
uiOutput(ns("result_screen"))

)
}

Expand All @@ -34,7 +34,8 @@ server <- function(id, app_data) {
div(
style = "margin-top: 2rem;",
organ_info_tabpanel$ui(session$ns("organ_info_tabpanel"))
)
),
fluidRow(column(plotOutput(session$ns("plot")), width = 10, offset = 1))
)
})

Expand Down

0 comments on commit 21d2bc7

Please sign in to comment.