Skip to content

Commit

Permalink
changing the calibrate_sir function with new simulated parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
sima-njf committed Jan 21, 2025
1 parent 6a71fa6 commit e4db2e7
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 149 deletions.
56 changes: 22 additions & 34 deletions Final_slides.html
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ <h1><span style="font-size: 60px;">Welcome to <strong>epiworldRcalibrate</strong
</section>
<section class="slide level2">

<h3 id="what-we-have-done-so-far">What we have done so far 🦠📈</h3>
<h3 id="what-we-have-done-so-far">What we have done so far? 🦠📈</h3>

<img data-src="misc/plan.jpg" style="width:180.0%" class="r-stretch quarto-figure-center"><div style="display: flex; align-items: flex-start; gap: 15px;">

Expand Down Expand Up @@ -453,8 +453,8 @@ <h3>🛠️ <strong>States of the SIR Model</strong></h3>
<h3>📊 <strong>Model Parameters</strong></h3>
<table>
<colgroup>
<col style="width: 35%">
<col style="width: 64%">
<col style="width: 36%">
<col style="width: 63%">
</colgroup>
<thead>
<tr class="header">
Expand Down Expand Up @@ -499,7 +499,7 @@ <h1>🚀 Simulate and calibrate SIR Function</h1>
<span id="cb2-5"><a href="#cb2-5"></a>ncores <span class="ot">=</span> <span class="dv">20</span></span>
<span id="cb2-6"><a href="#cb2-6"></a>verbose <span class="ot">=</span> <span class="dv">2</span></span>
<span id="cb2-7"><a href="#cb2-7"></a><span class="fu">simulate_calibrate_sir</span>(N, n, ndays, ncores, epochs, verbose)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="columns" style="font-size:17px;">
<div class="columns" style="font-size:15px;">
<div class="column" style="width:40%;">
<p>🎯 <span style="color: #4CAF50;"><strong>What Does This Function Do?</strong></span></p>
<p>1️⃣ <span style="color: #2196F3;"><strong>Data Generation:</strong></span> Create realistic epidemic scenarios</p>
Expand All @@ -508,12 +508,12 @@ <h1>🚀 Simulate and calibrate SIR Function</h1>
<p>4️⃣ <span style="color: #FF5722;"><strong>Evaluation:</strong></span> Assess performance (e.g., MAE)</p>
<p>5️⃣ <span style="color: #673AB7;"><strong>Visualization:</strong></span> Generate intuitive plots</p>
</div><div class="column" style="width:60%;">
<table style="width:99%;">
<table>
<colgroup>
<col style="width: 28%">
<col style="width: 21%">
<col style="width: 28%">
<col style="width: 20%">
<col style="width: 26%">
<col style="width: 23%">
<col style="width: 26%">
<col style="width: 23%">
</colgroup>
<thead>
<tr class="header">
Expand Down Expand Up @@ -569,24 +569,12 @@ <h1>🚀 Simulate and calibrate SIR Function</h1>
<h2>🌟 Going Deep into the Function</h2>
<div class="columns">
<div class="column" style="width:60%;">
<div id="code-container" style="
font-family: 'Courier New', Courier, monospace;
background-color: #1e1e1e;
color: #dcdcdc;
padding: 20px;
border-radius: 10px;
line-height: 1.8;
max-width: 100%;
position: relative;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.3);
font-size: 20px;">

<pre><code id="code-block">
simulate_calibrate_sir &lt;- function(N, n, ndays, ncores, epochs, verbose) {
theta &lt;- generate_theta(N, n)
seeds &lt;- sample.int(.Machine$integer.max, N, TRUE)
path = "~/epiworldRcalibrate/misc/simulated_data/sir-%06i.rds"


matrices &lt;- run_simulations(N, n, ndays, ncores, theta, seeds)

filtered_data &lt;- filter_non_null(matrices, theta)
Expand Down Expand Up @@ -614,7 +602,6 @@ <h2>🌟 Going Deep into the Function</h2>
return(list(pred = pred, MAEs = MAEs))
}
</code></pre>
</div>
</div><div class="column" style="width:40%;">
<h6 id="video-example">🎥 Video Example:</h6>
<iframe src="https://drive.google.com/file/d/1O4S4RqJPBQAZF_0MYK5n98yQevXyYZyg/preview" width="100%" height="500" frameborder="0" allowfullscreen="">
Expand Down Expand Up @@ -648,8 +635,9 @@ <h6 id="video-example">🎥 Video Example:</h6>
});
</script>
</section>
<section id="model-calibration-made-easy" class="slide level2">
<h2>Model Calibration Made Easy 🎯🤖</h2>
<section id="empower-yourself-perform-your-own-calibration" class="slide level2">
<h2>🎯 Empower Yourself: Perform Your Own Calibration:</h2>
<p>Model Calibration Made Easy 🎯🤖</p>
<div class="columns">
<div class="column" style="font-size: 20px;">
<h3 id="calibrate-parameters">🔍 Calibrate Parameters:</h3>
Expand Down Expand Up @@ -700,16 +688,16 @@ <h3 id="smart-model-selection">🔧 Smart Model Selection:</h3>
<span id="cb3-22"><a href="#cb3-22"></a>data<span class="ot">=</span>incidence<span class="sc">$</span>Infected</span>
<span id="cb3-23"><a href="#cb3-23"></a>result<span class="ot">=</span><span class="fu">calibrate_sir</span>(data)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="empower-yourself-perform-your-own-calibration" class="slide level2">
<h2>🎯 Empower Yourself: Perform Your Own Calibration</h2>
<section id="results" class="slide level2">
<h2>Results:</h2>
<h3 id="sir-model-actual-vs-predicted-table"><span style="font-size: 20px;">SIR Model Actual vs Predicted Table</span></h3>
<table style="width:97%;">
<table>
<colgroup>
<col style="width: 15%">
<col style="width: 12%">
<col style="width: 21%">
<col style="width: 26%">
<col style="width: 19%">
<col style="width: 19%">
<col style="width: 19%">
<col style="width: 21%">
<col style="width: 19%">
</colgroup>
<thead>
<tr class="header">
Expand Down Expand Up @@ -756,8 +744,8 @@ <h2>🙏 Thank You!</h2>
<strong>Thank You for Your Attention!</strong>
</p>
<p style="font-size: 20px; color: #555;">
Questions? Feel free to ask! 😊
</p>
</p><p>Questions? Feel free to ask! 😊</p>
<p></p>
</div>
<div style="text-align: center; font-size: 16px; color: #888; margin-top: 30px;">
<p>
Expand Down
89 changes: 33 additions & 56 deletions Final_slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ library(epiworldRcalibrate)

------------------------------------------------------------------------

### What we have done so far 🦠📈
### What we have done so far? 🦠📈

```{r, echo=FALSE, fig.align='center', out.width="180%"}
knitr::include_graphics("~/epiworldRcalibrate/epiworldRcalibrate/misc/plan.jpg")
Expand All @@ -62,17 +62,12 @@ knitr::include_graphics("~/epiworldRcalibrate/epiworldRcalibrate/misc/plan.jpg")
::: {style="font-size: 12px; line-height: 1; width: 50%; text-align: left; border: 1px solid #ddd; padding: 8px; border-radius: 3px; background-color: #f9f9f9;"}
### 📊 **Model Parameters**

+--------------------------+-------------------------------------------------+
| **Parameter** | **Description** |
+==========================+=================================================+
|--------------------------|----------------------------------------------|
| 🤝 **Contact Rate** | Number of contacts per individual per unit time |
+--------------------------+-------------------------------------------------+
| 💉 **Transmission Rate** | Probability of disease transmission per contact |
+--------------------------+-------------------------------------------------+
| 📈 **Prevalence** | Proportion of the population currently infected |
+--------------------------+-------------------------------------------------+
| 🛌 **Recovery Rate** | Rate at which infected individuals recover |
+--------------------------+-------------------------------------------------+
:::

# 🚀🎯 epiworldRcalibrate
Expand All @@ -95,7 +90,7 @@ verbose = 2
simulate_calibrate_sir(N, n, ndays, ncores, epochs, verbose)
```

::: {.columns style="font-size:17px;"}
::: {.columns style="font-size:15px;"}
::: {.column width="40%"}
🎯 [**What Does This Function Do?**]{style="color: #4CAF50;"}

Expand All @@ -111,46 +106,27 @@ simulate_calibrate_sir(N, n, ndays, ncores, epochs, verbose)
:::

::: {.column width="60%"}
+--------------------------------------------------+--------------------------------------+--------------------------------------------------+------------------------------------+
| [Category]{style="color: #4CAF50;"} | [Parameter]{style="color: #2196F3;"} | [Description]{style="color: #FFC107;"} | [Example]{style="color: #9C27B0;"} |
+==================================================+======================================+==================================================+====================================+
|-------------------|-----------------|-------------------|-----------------|
| [**Simulation Inputs**]{style="color: #4CAF50;"} | N | Total number of simulations to run. | 20,000 |
+--------------------------------------------------+--------------------------------------+--------------------------------------------------+------------------------------------+
| | n | Population size for each simulation. | 5,000 |
+--------------------------------------------------+--------------------------------------+--------------------------------------------------+------------------------------------+
| | ndays | Duration of the simulation in days. | 50 |
+--------------------------------------------------+--------------------------------------+--------------------------------------------------+------------------------------------+
| | ncores | Number of cores for parallel processing. | 20 |
+--------------------------------------------------+--------------------------------------+--------------------------------------------------+------------------------------------+
| [**Model Training**]{style="color: #FF5722;"} | epochs | Number of training iterations for the CNN model. | 100 |
+--------------------------------------------------+--------------------------------------+--------------------------------------------------+------------------------------------+
| [**Inputs**]{style="color: #FFC107;"} | verbose | Level of detail in the training output. | |
+--------------------------------------------------+--------------------------------------+--------------------------------------------------+------------------------------------+
:::
:::

## 🌟 Going Deep into the Function

::: {.columns}

::: columns
::: {.column width="60%"}
<div id="code-container" style="
font-family: 'Courier New', Courier, monospace;
background-color: #1e1e1e;
color: #dcdcdc;
padding: 20px;
border-radius: 10px;
line-height: 1.8;
max-width: 100%;
position: relative;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.3);
font-size: 20px;">
```{=html}
<pre><code id="code-block">
simulate_calibrate_sir <- function(N, n, ndays, ncores, epochs, verbose) {
theta <- generate_theta(N, n)
seeds <- sample.int(.Machine$integer.max, N, TRUE)
path = "~/epiworldRcalibrate/misc/simulated_data/sir-%06i.rds"

matrices <- run_simulations(N, n, ndays, ncores, theta, seeds)
filtered_data <- filter_non_null(matrices, theta)
Expand Down Expand Up @@ -178,22 +154,19 @@ simulate_calibrate_sir <- function(N, n, ndays, ncores, epochs, verbose) {
return(list(pred = pred, MAEs = MAEs))
}
</code></pre>
</div>
```
:::

::: {.column width="40%"}
###### 🎥 Video Example:

<iframe src="https://drive.google.com/file/d/1O4S4RqJPBQAZF_0MYK5n98yQevXyYZyg/preview"
width="100%"
height="500"
frameborder="0"
allowfullscreen>
<iframe src="https://drive.google.com/file/d/1O4S4RqJPBQAZF_0MYK5n98yQevXyYZyg/preview" width="100%" height="500" frameborder="0" allowfullscreen>
</iframe>
:::

:::

```{=html}
<script>
const allLines = document.querySelector("#code-block").innerHTML.split("\n");
let currentIndex = 0;
Expand All @@ -220,9 +193,10 @@ document.addEventListener("keydown", function (event) {
}
});
</script>
```


## Model Calibration Made Easy 🎯🤖
## 🎯 Empower Yourself: Perform Your Own Calibration:
Model Calibration Made Easy 🎯🤖

::: columns
::: {.column width="50%" style="font-size: 20px;"}
Expand Down Expand Up @@ -275,19 +249,15 @@ data=incidence$Infected
result=calibrate_sir(data)
```

## 🎯 Empower Yourself: Perform Your Own Calibration
## Results:

### [SIR Model Actual vs Predicted Table]{style="font-size: 20px;"}

+---------------+------------+----------------------+---------------------------+----------------------+
|   | Prevalence | Contact Rate (crate) | Transmission Rate (ptran) | Recovery Rate (prec) |
+===============+============+======================+===========================+======================+
| | Prevalence | Contact Rate (crate) | Transmission Rate (ptran) | Recovery Rate (prec) |
|--------------|--------------|--------------|---------------|--------------|
| **Actual** | 0.11900 | 0.64784 | 0.07971 | 0.54576 |
+---------------+------------+----------------------+---------------------------+----------------------+
| **Predicted** | 0.22479 | 0.60218 | 0.22618 | 0.54581 |
+---------------+------------+----------------------+---------------------------+----------------------+
| **MAE** | 0.10579 | 0.04566 | 0.14647 | 0.00005 |
+---------------+------------+----------------------+---------------------------+----------------------+

## [📊 Visualizing SIR Model Results: Actual vs Predicted Parameters]{style="font-size: 40px;"}

Expand All @@ -296,12 +266,19 @@ knitr::include_graphics("~/epiworldRcalibrate/epiworldRcalibrate/misc/plots.png"
```

## 🙏 Thank You!
<div style="text-align: center; font-size: 36px; color: #4CAF50; margin-top: 50px;">
<p><strong>Thank You for Your Attention!</strong></p>
<p style="font-size: 20px; color: #555;">Questions? Feel free to ask! 😊</p>
</div>

<div style="text-align: center; font-size: 16px; color: #888; margin-top: 30px;">
<p>Advanced R Programming</p>
<p>Population Health Sciences, University of Utah</p>
</div>

::: {style="text-align: center; font-size: 36px; color: #4CAF50; margin-top: 50px;"}
<p><strong>Thank You for Your Attention!</strong></p>

<p style="font-size: 20px; color: #555;">

Questions? Feel free to ask! 😊

</p>
:::

::: {style="text-align: center; font-size: 16px; color: #888; margin-top: 30px;"}
<p>Advanced R Programming</p>

<p>Population Health Sciences, University of Utah</p>
:::
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export(train_model)
import(epiworldR)
importFrom(data.table,as.data.table)
importFrom(data.table,copy)
importFrom(data.table,data.table)
importFrom(data.table,dcast)
importFrom(data.table,melt)
importFrom(data.table,nafill)
importFrom(data.table,setnames)
importFrom(dplyr,mutate)
importFrom(dplyr,row_number)
Expand All @@ -32,6 +34,7 @@ importFrom(ggplot2,geom_abline)
importFrom(ggplot2,geom_boxplot)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,labs)
importFrom(keras3,array_reshape)
importFrom(parallel,clusterEvalQ)
importFrom(parallel,clusterExport)
importFrom(parallel,makeCluster)
Expand Down
8 changes: 2 additions & 6 deletions R/calibrate_sir.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@
calibrate_sir <- function(data) {
ans <- preprocessing_data(data)
a <- length(ans)
ans <- tensorflow::tf$reshape(ans, shape = c(1L, 1L, a, 1L))

model_path <- if (a <= 31) {
system.file("models", "sir30-cnn.keras", package = "epiworldRcalibrate")
system.file("models", "sir30-test2-4e4-cnn.keras", package = "epiworldRcalibrate")
} else {
system.file("models", "sir60-cnn.keras", package = "epiworldRcalibrate")
system.file("models", "sir60-test2-2e4-cnn.keras", package = "epiworldRcalibrate")
}

if (model_path == "") {
stop("Model file not found. Please ensure the models are included in the 'epiworldRcalibrate' package.")
}

model <- tensorflow::tf$keras$models$load_model(model_path)

pred <- model$predict(ans) |>
data.table::as.data.table() |>
data.table::setnames(c("preval", "crate", "ptran", "prec"))
Expand Down
3 changes: 1 addition & 2 deletions R/dataprep.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#' @export

prepare_data <- function(m, max_days =max_days) {

err <- tryCatch({
ans <- list(
repnum = epiworldR::plot_reproductive_number(m, plot = FALSE),
Expand Down Expand Up @@ -88,4 +87,4 @@ prepare_data <- function(m, max_days =max_days) {
)

}

prepare_data(m,60)
Loading

0 comments on commit e4db2e7

Please sign in to comment.