Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix method #329

Merged
merged 10 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pipeline/src/EpiAwarePipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ module EpiAwarePipeline

using CSV, Dagger, DataFramesMeta, Dates, Distributions, DocStringExtensions, DrWatson,
EpiAware, Plots, Statistics, ADTypes, AbstractMCMC, Plots, JLD2, MCMCChains, Turing,
DynamicPPL, LogExpFunctions, RCall, LinearAlgebra
DynamicPPL, LogExpFunctions, RCall, LinearAlgebra, Random

# Exported pipeline types
export AbstractEpiAwarePipeline, EpiAwarePipeline, RtwithoutRenewalPipeline,
RtwithoutRenewalPriorPipeline, EpiAwareExamplePipeline
export AbstractEpiAwarePipeline, EpiAwarePipeline, AbstractRtwithoutRenewalPipeline,
RtwithoutRenewalPriorPipeline, EpiAwareExamplePipeline, SmoothOutbreakPipeline,
MeasuresOutbreakPipeline, SmoothEndemicPipeline, RoughEndemicPipeline

# Exported utility functions
export calculate_processes
Expand Down
163 changes: 163 additions & 0 deletions pipeline/src/constructors/make_Rt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,166 @@ function make_Rt(pipeline::AbstractEpiAwarePipeline; A = 0.3, P = 30.0)
[1.0 + A * sin.(2 * π * (t - ϕ) / P) for t in 1:(N - length(Rt))]]
return true_Rt
end

"""
Constructs the time-varying reproduction number (Rt) for a given SmoothOutbreakPipeline.

# Arguments
- `pipeline::SmoothOutbreakPipeline`: The SmoothOutbreakPipeline object for which to construct Rt.
- `N::Float64 = 70.0`: The number of time steps to consider.
- `initial_Rt::Float64 = 1.5`: The initial value of Rt.
- `reduction::Float64 = 0.5`: The reduction factor applied to Rt over time.
- `r::Float64 = 1 / 3.5`: The rate of change of Rt over time.
- `shift::Float64 = 35.0`: The time shift applied to Rt.

# Returns
- `Rt::Vector{Float64}`: The time-varying reproduction number (Rt) as a vector.

# Example

```julia
using EpiAwarePipeline, Plots
pipeline = SmoothOutbreakPipeline()
Rt = make_Rt(pipeline) |> Rt -> plot(Rt,
xlabel = "Time",
ylabel = "Rt",
lab = "",
title = "Smooth outbreak scenario")
```
"""
function make_Rt(pipeline::SmoothOutbreakPipeline; N = 70.0, initial_Rt = 1.5,
reduction = 0.5, r = 1 / 3.5, shift = 35.0)
Rt = map(1.0:N) do t
initial_Rt * (1 - reduction * logistic(r * (t - shift)))
end
return Rt
end

"""
Constructs the time-varying reproduction number (Rt) for a given outbreak with measures scenario.

# Arguments
- `pipeline::MeasuresOutbreakPipeline`: The outbreak pipeline object.
- `N::Float64`: The total number of time steps.
- `initial_Rt::Float64`: The initial reproduction number before any measures are implemented.
- `measures_Rt::Float64`: The reproduction number during the period when measures are implemented.
- `post_measures_Rt::Float64`: The reproduction number after the measures are lifted.
- `reduction::Float64`: The eventual reduction factor applied to the reproduction number after measures are lifted over time.
- `r::Float64`: The rate of decay towards the reduction factor.
- `shift::Float64`: The time shift applied to the decay function after end of measures.
- `t₁::Float64`: The time step at which measures are implemented.
- `t₂::Float64`: The time step at which measures are lifted.

# Returns
- `Rt::Vector{Float64}`: The time-varying reproduction number.

# Example

```julia
using EpiAwarePipeline, Plots
pipeline = MeasuresOutbreakPipeline()
Rt = make_Rt(pipeline) |> Rt -> plot(Rt,
xlabel = "Time",
ylabel = "Rt",
lab = "",
title = "Outbreak scenario with measures")
```
"""
function make_Rt(
pipeline::MeasuresOutbreakPipeline; N = 70.0, initial_Rt = 1.5, measures_Rt = 0.8,
post_measures_Rt = 1.2, reduction = 0.6, r = 1 / 3.5, shift = 21.0, t₁ = 21.0, t₂ = 42.0)
Rt = vcat(map(t -> initial_Rt, 1.0:t₁),
map(t -> measures_Rt, (t₁ + 1.0):t₂),
map((t₂ + 1):N) do t
post_measures_Rt * (1 - reduction * logistic(r * (t - t₂ - shift)))
end)

return Rt
end

"""
Constructs the time-varying reproduction number (Rt) for a smoothly varying endemic scenario.

In this context "endemic" means a scenario where the geometric average of the
reproduction number is 1.0, that is the following holds:

```math
(\\prod_{t = 1}^n[R_t])^{1/n} = 1.
```

This implies that the long-term muliplicative growth of the infected community is 1.0.

# Arguments
- `pipeline::SmoothEndemicPipeline`: The pipeline for which Rt is constructed.
- `N::Float64 = 70.0`: The number of time points to generate Rt for.
- `A::Float64 = 0.1`: The amplitude of the sinusoidal variation in Rt.
- `P::Float64 = 28.0`: The period of the sinusoidal variation in Rt.

# Returns
- `Rt::Vector{Float64}`: The time-varying reproduction number.

# Examples

```julia
using EpiAwarePipeline, Plots
pipeline = SmoothEndemicPipeline()
Rt = make_Rt(pipeline) |> Rt -> plot(Rt,
xlabel = "Time",
ylabel = "Rt",
lab = "",
title = "Smoothly varying endemic scenario")
```
"""
function make_Rt(
pipeline::SmoothEndemicPipeline; N = 70.0, A = 0.1, P = 28.0)
log_Rt = map(1.0:N) do t
A * sinpi(2 * t / P)
end
Rt = exp.(log_Rt .- mean(log_Rt)) # Normalize to have geometric mean 1.0
return Rt
end

"""
Constructs the time-varying reproduction number (Rt) for an randomly varying endemic scenario.

In this context "endemic" means a scenario where the geometric average of the
reproduction number is 1.0, that is the following holds:

```math
(\\prod_{t = 1}^n[R_t])^{1/n} = 1.
```

This implies that the long-term muliplicative growth of the infected community is 1.0.

# Arguments
- `pipeline::RoughEndemicPipeline`: The `RoughEndemicPipeline` object for which Rt is being constructed.
- `N_steps::Int = 10`: The number of steps to generate random values for Rt.
- `rng::AbstractRNG = MarsenneTwister(1234)`: The random number generator to use.
- `stride::Int = 7`: The stride length for repeating each random value.
- `σ::Float64 = 0.05`: The standard deviation of the random values.

# Returns
- `Rt::Vector{Float64}`: The time-varying reproduction number Rt.

# Example

```julia
using EpiAwarePipeline, Plots
pipeline = RoughEndemicPipeline()
Rt = make_Rt(pipeline) |> Rt -> plot(Rt,
xlabel = "Time",
ylabel = "Rt",
lab = "",
title = "Rough Rt path endemic scenario")
```
"""
function make_Rt(
pipeline::RoughEndemicPipeline; N_steps = 10, rng = MersenneTwister(1234), stride = 7, σ = 0.05)
X = σ * randn(rng, N_steps)
log_Rt = mapreduce(vcat, X) do x
fill(x, stride)
end
Rt = exp.(log_Rt .- mean(log_Rt)) # Normalize to have geometric mean 1.0

return Rt
end
21 changes: 6 additions & 15 deletions pipeline/src/infer/generate_inference_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ Generate inference results based on the given configuration of inference model o
"""
function generate_inference_results(
truthdata, inference_config, pipeline::AbstractEpiAwarePipeline;
tspan, inference_method,
prefix_name = "observables", datadir_name = "epiaware_observables")
tspan, inference_method, datadir_name = "epiaware_observables")
config = InferenceConfig(
inference_config; case_data = truthdata["y_t"], tspan, epimethod = inference_method)

# produce or load inference results
prfx = prefix_name * "_igp_" * string(inference_config["igp"]) * "_latentmodel_" *
inference_config["latent_namemodels"].first * "_truth_gi_mean_" *
string(truthdata["truth_gi_mean"]) * "_used_gi_mean_" *
string(inference_config["gi_mean"])
prfx = _inference_prefix(truthdata, inference_config, pipeline)

inference_results, inferencefile = produce_or_load(
infer, config, datadir(datadir_name); prefix = prfx)
Expand All @@ -51,15 +47,12 @@ which is deleted after the function call.
"""
function generate_inference_results(
truthdata, inference_config, pipeline::EpiAwareExamplePipeline;
tspan, inference_method, prefix_name = "testmode_observables")
tspan, inference_method)
config = InferenceConfig(inference_config; case_data = truthdata["y_t"],
tspan = tspan, epimethod = inference_method)

# produce or load inference results
prfx = prefix_name * "_igp_" * string(inference_config["igp"]) * "_latentmodel_" *
inference_config["latent_namemodels"].first * "_truth_gi_mean_" *
string(truthdata["truth_gi_mean"]) * "_used_gi_mean_" *
string(inference_config["gi_mean"])
prfx = _inference_prefix(truthdata, inference_config, pipeline)

datadir_name = mktempdir()

Expand All @@ -73,14 +66,12 @@ Method for prior predictive modelling.
"""
function generate_inference_results(
inference_config, pipeline::RtwithoutRenewalPriorPipeline;
tspan, prefix_name = "prior_observables")
tspan)
config = InferenceConfig(
inference_config; case_data = missing, tspan, epimethod = DirectSample())

# produce or load inference results
prfx = prefix_name * "_igp_" * string(inference_config["igp"]) * "_latentmodel_" *
inference_config["latent_namemodels"].first * "_truth_gi_mean_" *
string(inference_config["gi_mean"])
prfx = _inference_prefix(truthdata, inference_config, pipeline)

datadir_name = mktempdir()

Expand Down
1 change: 1 addition & 0 deletions pipeline/src/infer/infer.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include("InferenceConfig.jl")
include("inference_prefix.jl")
include("generate_inference_results.jl")
include("map_inference_results.jl")
include("define_epiprob.jl")
54 changes: 54 additions & 0 deletions pipeline/src/infer/inference_prefix.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
This is an internal method that generates the prefix for the inference results file name.
"""
function _inference_prefix(truthdata, inference_config, pipeline::AbstractEpiAwarePipeline)
seabbs marked this conversation as resolved.
Show resolved Hide resolved
return "observables" * "_igp_" * string(inference_config["igp"]) * "_latentmodel_" *
inference_config["latent_namemodels"].first * "_truth_gi_mean_" *
string(truthdata["truth_gi_mean"]) * "_used_gi_mean_" *
string(inference_config["gi_mean"])
end

function _inference_prefix(truthdata, inference_config, pipeline::EpiAwareExamplePipeline)
return "testmode_observables" * "_igp_" * string(inference_config["igp"]) *
"_latentmodel_" *
inference_config["latent_namemodels"].first * "_truth_gi_mean_" *
string(truthdata["truth_gi_mean"]) * "_used_gi_mean_" *
string(inference_config["gi_mean"])
end

function _inference_prefix(
truthdata, inference_config, pipeline::RtwithoutRenewalPriorPipeline)
return "prior_observables" * "_igp_" * string(inference_config["igp"]) *
"_latentmodel_" *
inference_config["latent_namemodels"].first * "_truth_gi_mean_" *
string(inference_config["gi_mean"])
end

function _inference_prefix(truthdata, inference_config, pipeline::SmoothOutbreakPipeline)
return "smooth_outbreak" * "_igp_" * string(inference_config["igp"]) * "_latentmodel_" *
inference_config["latent_namemodels"].first * "_truth_gi_mean_" *
string(truthdata["truth_gi_mean"]) * "_used_gi_mean_" *
string(inference_config["gi_mean"])
end

function _inference_prefix(truthdata, inference_config, pipeline::MeasuresOutbreakPipeline)
return "measures_outbreak" * "_igp_" * string(inference_config["igp"]) *
"_latentmodel_" *
inference_config["latent_namemodels"].first * "_truth_gi_mean_" *
string(truthdata["truth_gi_mean"]) * "_used_gi_mean_" *
string(inference_config["gi_mean"])
end

function _inference_prefix(truthdata, inference_config, pipeline::SmoothEndemicPipeline)
return "smooth_endemic" * "_igp_" * string(inference_config["igp"]) * "_latentmodel_" *
inference_config["latent_namemodels"].first * "_truth_gi_mean_" *
string(truthdata["truth_gi_mean"]) * "_used_gi_mean_" *
string(inference_config["gi_mean"])
end

function _inference_prefix(truthdata, inference_config, pipeline::RoughEndemicPipeline)
return "rough_endemic" * "_igp_" * string(inference_config["igp"]) * "_latentmodel_" *
inference_config["latent_namemodels"].first * "_truth_gi_mean_" *
string(truthdata["truth_gi_mean"]) * "_used_gi_mean_" *
string(inference_config["gi_mean"])
end
46 changes: 42 additions & 4 deletions pipeline/src/pipeline/pipelinetypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ The abstract root type for all pipeline types using `EpiAware`.
abstract type AbstractEpiAwarePipeline end

"""
The pipeline type for the Rt pipeline with renewal including specific options
for plotting and saving.
An abstract type for different scenarios in the Rt without Renewal pipeline.
"""
struct RtwithoutRenewalPipeline <: AbstractEpiAwarePipeline
end
abstract type AbstractRtwithoutRenewalPipeline <: AbstractEpiAwarePipeline end

"""
The pipeline type for the Rt pipeline without renewal with only prior predictive
Expand All @@ -22,3 +20,43 @@ The pipeline type for the Rt pipeline without renewal in test mode.
"""
struct EpiAwareExamplePipeline <: AbstractEpiAwarePipeline
end

"""
The pipeline type for the Rt pipeline for an outbreak scenario where Rt decreases
smoothly over time to Rt < 1.

# Example

```julia
using EpiAwarePipeline, Plots
pipeline = SmoothOutbreakPipeline()
Rt = make_Rt(pipeline) |> Rt -> plot(Rt,
xlabel = "Time",
ylabel = "Rt",
lab = "",
title = "Smooth outbreak scenario")
```
"""
struct SmoothOutbreakPipeline <: AbstractRtwithoutRenewalPipeline
seabbs marked this conversation as resolved.
Show resolved Hide resolved
end

"""
The pipeline type for the Rt pipeline for an outbreak scenario where Rt has
discontinuous changes over time due to implementation of measures.
"""
struct MeasuresOutbreakPipeline <: AbstractRtwithoutRenewalPipeline
seabbs marked this conversation as resolved.
Show resolved Hide resolved
end

"""
The pipeline type for the Rt pipeline for an endemic scenario where Rt changes in
a smooth sinusoidal manner over time.
"""
struct SmoothEndemicPipeline <: AbstractRtwithoutRenewalPipeline
end

"""
The pipeline type for the Rt pipeline for an endemic scenario where Rt changes in
a weekly-varying discontinuous manner over time.
"""
struct RoughEndemicPipeline <: AbstractRtwithoutRenewalPipeline
end
4 changes: 3 additions & 1 deletion pipeline/src/simulate/generate_truthdata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ the `simulate` function to generate the truth data. This is the default method.
"""
function generate_truthdata(
truth_data_config, pipeline::AbstractEpiAwarePipeline; plot = true,
datadir_str = "truth_data", prefix = "truth_data")
datadir_str = "truth_data")
default_params = make_default_params(pipeline)
config = TruthSimulationConfig(
truth_process = default_params["Rt"], gi_mean = truth_data_config["gi_mean"],
gi_std = truth_data_config["gi_std"], logit_daily_ascertainment = default_params["logit_daily_ascertainment"],
cluster_factor = default_params["cluster_factor"], I0 = default_params["I0"])

prefix = _simulate_prefix(pipeline)

truthdata, truthfile = produce_or_load(
simulate, config, datadir(datadir_str); prefix = prefix)
if plot
Expand Down
1 change: 1 addition & 0 deletions pipeline/src/simulate/simulate.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include("TruthSimulationConfig.jl")
include("simulate_prefix.jl")
include("generate_truthdata.jl")
12 changes: 12 additions & 0 deletions pipeline/src/simulate/simulate_prefix.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Internal method for setting the prefix for the truth data file name.
"""
_simulate_prefix(pipeline::AbstractEpiAwarePipeline) = "truth_data"
seabbs marked this conversation as resolved.
Show resolved Hide resolved

_simulate_prefix(pipeline::SmoothOutbreakPipeline) = "truth_data_smooth_outbreak"

_simulate_prefix(pipeline::MeasuresOutbreakPipeline) = "truth_data_measures_outbreak"

_simulate_prefix(pipeline::SmoothEndemicPipeline) = "truth_data_smooth_endemic"

_simulate_prefix(pipeline::RoughEndemicPipeline) = "truth_data_rough_endemic"
Loading
Loading