From e51680f7e9f7932f504bae1401829494c7f2fd5b Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Fri, 26 Jan 2024 14:16:51 +0100 Subject: [PATCH 01/11] Replace ifelse() by min() --- R/simulate.r | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/R/simulate.r b/R/simulate.r index fb3c9473..246c2278 100644 --- a/R/simulate.r +++ b/R/simulate.r @@ -224,11 +224,7 @@ simulate_chains <- function(index_cases, # Sample susceptible offspring to be infected from all possible offspring # We first adjust for the case where susceptible can be Inf but prob is max # 1. - binom_prob <- ifelse( - is.infinite(susc_pop), - 1, - susc_pop / pop - ) + binom_prob <- min(1, susc_pop / pop, na.rm = TRUE) next_gen <- stats::rbinom( n = length(next_gen), size = next_gen, From 5be819502444fec34a723ce57c2064b1b5e0bb58 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Fri, 26 Jan 2024 14:20:43 +0100 Subject: [PATCH 02/11] Always truncate at tf Since tf is Inf by default --- R/simulate.r | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/R/simulate.r b/R/simulate.r index 246c2278..27201063 100644 --- a/R/simulate.r +++ b/R/simulate.r @@ -307,9 +307,7 @@ simulate_chains <- function(index_cases, # Combine the results tree_df <- do.call(rbind, tree_df) - if (!missing(tf)) { - tree_df <- tree_df[tree_df$time < tf, ] - } + tree_df <- tree_df[tree_df$time < tf, ] # Post processing # # sort by sim_id and infector_id From 5ae9d0c2bd7958970bd73bd407781e3e00019d1f Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Fri, 26 Jan 2024 14:23:15 +0100 Subject: [PATCH 03/11] Remove unnecessary indexing Since n_offspring[!sim] is set to 0 --- R/simulate.r | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/simulate.r b/R/simulate.r index 27201063..02634993 100644 --- a/R/simulate.r +++ b/R/simulate.r @@ -212,7 +212,7 @@ simulate_chains <- function(index_cases, next_gen <- do.call( roffspring_name, c( - list(n = sum(n_offspring[sim])), + list(n = sum(n_offspring)), pars ) ) @@ -254,7 +254,7 @@ simulate_chains <- function(index_cases, # Adorn the new offspring with their information: their ids, their # infector's ids, and the generation they were infected in. # Also update the susceptible population and generation. - if (sum(n_offspring[sim]) > 0) { + if (sum(n_offspring) > 0) { infectors <- rep(infector_ids, next_gen) current_max_id <- unname(tapply(infector_ids, parent_ids, max)) parent_ids <- rep(sim, n_offspring[sim]) @@ -266,7 +266,7 @@ simulate_chains <- function(index_cases, # increment the generation generation <- generation + 1L # Update susceptible population - susc_pop <- susc_pop - sum(n_offspring[sim]) + susc_pop <- susc_pop - sum(n_offspring) # store new simulation results tree_df[[generation]] <- From 2b5915d170d4b0a9dddebaf78e654e3e938018e5 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Fri, 26 Jan 2024 14:36:16 +0100 Subject: [PATCH 04/11] Replace unlist(lapply(., seq_len)) by sequence() --- R/simulate.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/simulate.r b/R/simulate.r index 02634993..57fc81ab 100644 --- a/R/simulate.r +++ b/R/simulate.r @@ -261,7 +261,7 @@ simulate_chains <- function(index_cases, # create new ids ids <- rep(current_max_id, n_offspring[sim]) + - unlist(lapply(n_offspring[sim], seq_len)) + sequence(n_offspring[sim]) # increment the generation generation <- generation + 1L From 7911e02ceed09de00b6ea7cc7ce7ec9e3504e738 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Fri, 26 Jan 2024 17:31:26 +0100 Subject: [PATCH 05/11] Merge successive ifs --- R/simulate.r | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/simulate.r b/R/simulate.r index 57fc81ab..c7acf7c3 100644 --- a/R/simulate.r +++ b/R/simulate.r @@ -296,8 +296,6 @@ simulate_chains <- function(index_cases, if (!missing(generation_time)) { ## only continue to simulate trees that don't go beyond tf sim <- intersect(sim, unique(parent_ids)[current_min_time < tf]) - } - if (!missing(generation_time)) { times <- times[parent_ids %in% sim] } infector_ids <- ids[parent_ids %in% sim] From a6f773b94be0f2d9b3ec40838c5f6be6ddd1fd90 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Mon, 29 Jan 2024 15:10:45 +0100 Subject: [PATCH 06/11] Revert "Always truncate at tf" This reverts commit 45e29bfce5c560f37565fe177702f0a56b4e360b. --- R/simulate.r | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/simulate.r b/R/simulate.r index c7acf7c3..2d16e6ec 100644 --- a/R/simulate.r +++ b/R/simulate.r @@ -305,7 +305,9 @@ simulate_chains <- function(index_cases, # Combine the results tree_df <- do.call(rbind, tree_df) - tree_df <- tree_df[tree_df$time < tf, ] + if (!missing(tf)) { + tree_df <- tree_df[tree_df$time < tf, ] + } # Post processing # # sort by sim_id and infector_id From 99077bcfc2ea5dbaeaac29078041cc9f3dc08151 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Mon, 29 Jan 2024 15:11:42 +0100 Subject: [PATCH 07/11] Add comment for 7feb6ec234f36c590037185918c4b60ad161a96a --- R/simulate.r | 1 + 1 file changed, 1 insertion(+) diff --git a/R/simulate.r b/R/simulate.r index 2d16e6ec..3c12dd5f 100644 --- a/R/simulate.r +++ b/R/simulate.r @@ -305,6 +305,7 @@ simulate_chains <- function(index_cases, # Combine the results tree_df <- do.call(rbind, tree_df) + # time column only exists if tf was specified if (!missing(tf)) { tree_df <- tree_df[tree_df$time < tf, ] } From 13ecc866127ffc32cdd238222bed20014bc5b840 Mon Sep 17 00:00:00 2001 From: James Azam Date: Tue, 30 Jan 2024 15:34:14 +0000 Subject: [PATCH 08/11] Fix indexing issue Co-authored-by: Sebastian Funk --- R/simulate.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/simulate.r b/R/simulate.r index 3c12dd5f..2374e04b 100644 --- a/R/simulate.r +++ b/R/simulate.r @@ -212,7 +212,7 @@ simulate_chains <- function(index_cases, next_gen <- do.call( roffspring_name, c( - list(n = sum(n_offspring)), + list(n = sum(n_offspring[sim])), pars ) ) From 92353e849d56328c2934510985f933352e174e4e Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 30 Jan 2024 15:40:02 +0000 Subject: [PATCH 09/11] Automatic readme update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d3e801dd..3fe11a96 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ library("epichains") a generation time function is specified, the time of infection. - `simulate_summary()`: provides a performant version of - `simulate_chains()` that only tracks and returns a vector of realized + `simulate_chains()` that only tracks and return a vector of realized chain sizes or lengths/durations for each index case without details of the infection tree. From 87791adf4b0a420eaf6418bec85784320f66c4d2 Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Tue, 30 Jan 2024 15:40:06 +0000 Subject: [PATCH 10/11] Add Hugo as contributor --- DESCRIPTION | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index 62e01269..9c8376f8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -5,6 +5,8 @@ Version: 0.0.0.9999 Authors@R: c( person("James M.", "Azam", , "james.azam@lshtm.ac.uk", role = c("aut", "cre"), comment = c(ORCID = "https://orcid.org/0000-0001-5782-7330")), + person("Hugo", "Gruson", , "hugo@data.org", role = c("ctb"), + comment = c(ORCID = "https://orcid.org/0000-0002-4094-1476")), person("Zhian N.", "Kamvar", , "zkamvar@gmail.com", role = "ctb", comment = c(ORCID = "https://orcid.org/0000-0003-1458-7108")), person("Flavio", "Finger", , "flavio.finger@epicentre.msf.org", role = "aut", From 0009389525e9b100a483b3f856c6c9aaa7be80d3 Mon Sep 17 00:00:00 2001 From: James Azam Date: Tue, 30 Jan 2024 15:49:59 +0000 Subject: [PATCH 11/11] Revert grammatical correction --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3fe11a96..d3e801dd 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ library("epichains") a generation time function is specified, the time of infection. - `simulate_summary()`: provides a performant version of - `simulate_chains()` that only tracks and return a vector of realized + `simulate_chains()` that only tracks and returns a vector of realized chain sizes or lengths/durations for each index case without details of the infection tree.