Skip to content

Commit

Permalink
Split into two files
Browse files Browse the repository at this point in the history
  • Loading branch information
anitaapplegarth committed Jan 23, 2025
1 parent dc7935e commit 4d3184b
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 158 deletions.
80 changes: 80 additions & 0 deletions R/simulation.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

# Example usage:
run_complete_simulation <- function(output_dir,
output_file = "output.csv",
plot_file = "SIR_plot.png",
use_toy_example = TRUE,
simulation_duration,
initial_infected) {
# Initialize environment
pe <- initialize_simulation_env()

pe <- configure_parameters(pe, input_dir, config_parameters)

# Create all parameter sets
pop_params <- list(
population_size = as.integer(100),
cell_number = as.integer(2),
microcell_number = as.integer(2),
household_number = as.integer(5),
place_number = as.integer(2)
)

sim_params <- list(
simulation_start_time = as.integer(0),
simulation_end_time = as.integer(simulation_duration),
simulation_seed = TRUE,
initial_infected_number = as.integer(initial_infected),
initial_infect_cell = TRUE,
include_waning = TRUE
)

file_params <- list(
output_file = output_file,
output_dir = output_dir,
spatial_output = FALSE,
age_stratified = FALSE
)

dem_file_params <- list(
output_dir = output_dir,
spatial_output = FALSE,
age_output = FALSE
)

inf_history_params <- list(
output_dir = output_dir,
status_output = TRUE,
infectiousness_output = TRUE,
compress = FALSE,
secondary_infections_output = TRUE,
generation_time_output = TRUE
)

# User defined variables - see README for instructions
input_dir <- "./data/Andorra/inputs"
config_parameters <- ".data/Andorra/inputs/simple_parameters"
epigeopop_file <- "./data/Andorra/inputs/Andorra_microcells.csv"
seed <- 42
output_dir <- "./data/Andorra/simulation_outputs"
simulation_duration <- 60
initial_infected <- 10

# Run simulation
source(wrapper.R)
sim <- run_simulation(pe, sim_params, file_params, dem_file_params,
inf_history_params, pop_params,
epigeopop_file = epigeopop_file, seed,
use_toy_example = use_toy_example)

# Process data and create plot
df_long <- process_simulation_data(file.path(output_dir, output_file))
plot <- create_sir_plot(df_long)

# Save plot
save_sir_plot(plot, file.path(output_dir, plot_file))

return(list(simulation = sim, data = df_long, plot = plot))
}

results <- run_complete_simulation()
157 changes: 0 additions & 157 deletions R/simulation_flow_check.R

This file was deleted.

132 changes: 132 additions & 0 deletions R/wrapper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
library(reticulate)
library(here)
library(tidyr)
library(ggplot2)

# Initialize Python environment
initialize_simulation_env <- function() {
source("R/zzz.R")
initialize_python_env()
check_python_env()

# Import Python dependencies
os <- import("os", delay_load = TRUE)
logging <- import("logging", delay_load = TRUE)
pd <- import("pandas", delay_load = TRUE)
plt <- import("matplotlib.pyplot", delay_load = TRUE)
pe <- import("pyEpiabm", delay_load = TRUE)
return(pe)
}

configure_parameters <- function(pe, input_dir, config_parameters) {
pe$Parameters$set_file(here(input_dir, config_parameters))
return(pe)
}

# Wrap python simulation function
run_simulation <- function(pe, sim_params, file_params, dem_file_params,
inf_history_params, pop_params = NULL,
epigeopop_file = "", seed = 42,
use_toy_example = FALSE) {
# Set seed
pe$routine$Simulation$set_random_seed(seed = as.integer(seed))
# Create population or load from file
if (epigeopop_file == "") {
population <- pe$routine$ToyPopulationFactory()$
make_pop(pop_params)
} else {
population <- pe$routine$FilePopulationFactory()$
make_pop_from_file(epigeopop_file)
}

# Create and configure simulation
sim <- pe$routine$Simulation()
sim$configure(
population,
list(
pe$sweep$InitialInfectedSweep(),
pe$sweep$InitialDemographicsSweep(dem_file_params)
),
list(
pe$sweep$HouseholdSweep(),
pe$sweep$QueueSweep(),
pe$sweep$HostProgressionSweep()
),
sim_params,
file_params,
inf_history_params
)

# Run simulation
sim$run_sweeps()
sim$compress_csv()

return(sim)
}

# Process simulation data
process_simulation_data <- function(output_file) {
df <- read.csv(here(output_file))

status_columns <- c(
"InfectionStatus.Susceptible",
"InfectionStatus.InfectMild",
"InfectionStatus.Recovered",
"InfectionStatus.Dead"
)
df_long <- pivot_longer(
df,
cols = all_of(status_columns),
names_to = "Status",
values_to = "Count"
)
df_long$Status <- factor(
df_long$Status,
levels = status_columns,
labels = c("Susceptible", "Infected", "Recovered", "Dead")
)

return(df_long)
}

# Create SIR plot
create_sir_plot <- function(df_long, title = "SIR Model Flow", display = TRUE) {
p <- ggplot(df_long, aes(x = time, y = Count, color = Status)) +
geom_line() +
scale_color_manual(
values = c(
"Susceptible" = "blue",
"Infected" = "red",
"Recovered" = "green",
"Dead" = "black"
)
) +
theme_minimal() +
labs(
title = title,
x = "Time",
y = "Count"
) +
theme(
legend.position = "right",
plot.title = element_text(hjust = 0.5),
panel.grid.minor = element_blank()
)

if (display) {
print(p)
}

return(p)
}

# Save plot
save_sir_plot <- function(plot, filename, width = 10, height = 6, dpi = 300) {
ggsave(
filename = here(filename),
plot = plot,
width = width,
height = height,
dpi = dpi
)
}
3 changes: 2 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ for package in packages:
# Install pyEpiabm from GitHub
subprocess.check_call([
sys.executable, '-m', 'pip', 'install', '--upgrade',
'git+https://github.com/SABS-R3-Epidemiology/epiabm.git@main#egg=pyEpiabm&subdirectory=pyEpiabm'
'git+https://github.com/SABS-R3-Epidemiology/epiabm.git@main#egg=pyEpiabm&subdirectory=pyEpiabm' # nolint: line_length_linter.
])
")
# Verify installations
Expand Down Expand Up @@ -108,6 +108,7 @@ load_python_modules <- function() {
modules <- list(
os = "os",
logging = "logging",
np = "numpy",
pd = "pandas",
plt = "matplotlib.pyplot",
pe = "pyEpiabm"
Expand Down

0 comments on commit 4d3184b

Please sign in to comment.