---
title: "ABM simulations Bavarian/German 2022/2023: Log summarisation"
author: "Markus Jochim and Felicitas Kleber"
date: "2023-05-11"
output:
  html_document:
    toc: yes
    number_sections: yes
    toc_float:
      collapsed: yes
    df_print: paged
    code_folding: hide
  html_notebook:
    toc: yes
    number_sections: yes
    toc_float:
      collapsed: yes
    df_print: paged
    code_folding: hide
---


```{r setup, message=FALSE, warning=FALSE, results=FALSE}
library(tidyverse)
library(data.table)
library(rlist)

base_directory = ".."
results_directory = file.path(base_directory, "03_logs")

between_group_contact_scenario = "ABM20220913155302"
symmetric_contact_scenario     = "ABM20220922125435"
asymmetric_contact_scenario    = "ABM20220914134457"
within_group_contact_scenario  = "ABM20220913185547"


source(file.path(base_directory, "02_abm_code/functions/simulations.R"))


get_data_from_all_runs = function(simulation_name,
                                  results_directory,
                                  snapshot_ids,
                                  run_ids) {
  current_parameter_set = get_params(results_directory, simulation_name)

  run_directories = paste0(results_directory,
                           "/",
                           simulation_name,
                           "/",
                           run_ids,
                           "/") %>% as.factor
  
  snapshot_files = paste0("pop.",
                          snapshot_ids,
                          ".rds") %>% as.factor
  
  files = character(length(run_directories) * length(snapshot_files))
  
  for (i in 1:length(run_directories)) {
    for (j in 1:length(snapshot_files)) {
      files[(i-1) * length(snapshot_files) + j] = paste0(run_directories[i],
                                                         snapshot_files[j])
    }
  }

  files = tibble(path = files) %>%
    mutate (run      = sub(path, pattern = ".*/([0-9]*)/pop\\.([0-9]*)\\.rds.*", replacement = "\\1"),
            snapshot = sub(path, pattern = ".*/([0-9]*)/pop\\.([0-9]*)\\.rds.*", replacement = "\\2"),
            snapshot = as.numeric(snapshot))
  
  all_data = rbindlist(lapply(rownames(files), function(x) {
    r = readRDS(files[x,]$path)
    r[, `:=` (condition = condition * current_parameter_set$interactionsPerSnapshot,
              closure_ms = P1,
              run = files[x,]$run,
              snapshot = files[x,]$snapshot
              
    )]
    r[valid == TRUE,]
  }))
  
  return(all_data)
}
```


# Between-group only scenario

```{r between_group}
simulations = between_group_contact_scenario

for (run_id in 1:100) {
  if (run_id == 1) {
    append = FALSE
  } else  {
    append = TRUE
  }

  data_chunk = get_data_from_all_runs(simulations,
                                     results_directory = results_directory,
                                     snapshot_ids = 0:15,
                                     run_ids = run_id)
  
  average_per_population = data_chunk %>%
    group_by(run, group, condition) %>%
    dplyr::summarise(closure_ms_mean = mean(closure_ms),
                     closure_ms_q25  = quantile(closure_ms, 0.25),
                     closure_ms_q50  = quantile(closure_ms, 0.50),
                     closure_ms_q75  = quantile(closure_ms, 0.75),
                     .groups = "drop_last")
  
  write_csv(x = average_per_population,
            append = append,
            file = file.path(base_directory,
                             "04_summarised_logs",
                             "average_per_population_between_group.csv"))
  
  average_per_agent = data_chunk %>%
    group_by(run, group, agentID, condition) %>%
    dplyr::summarise(closure_ms_mean = mean(closure_ms),
                     closure_ms_q25  = quantile(closure_ms, 0.25),
                     closure_ms_q50  = quantile(closure_ms, 0.50),
                     closure_ms_q75  = quantile(closure_ms, 0.75),
                     .groups = "drop_last")
  
  write_csv(x = average_per_agent,
            append = append,
            file = file.path(base_directory,
                             "04_summarised_logs",
                             "average_per_agent_between_group.csv"))
}
```


# Symmetric scenario

```{r symmetric}
simulations = symmetric_contact_scenario

for (run_id in 1:25) {
  if (run_id == 1) {
    append = FALSE
  } else  {
    append = TRUE
  }

  data_chunk = get_data_from_all_runs(simulations,
                                      results_directory = results_directory,
                                      snapshot_ids = seq(0, 60, by = 5),
                                      run_ids = run_id)

  average_per_population = data_chunk %>%
    group_by(run, group, condition) %>%
    dplyr::summarise(closure_ms_mean = mean(closure_ms),
                     closure_ms_q25  = quantile(closure_ms, 0.25),
                     closure_ms_q50  = quantile(closure_ms, 0.50),
                     closure_ms_q75  = quantile(closure_ms, 0.75),
                     .groups = "drop_last")
  
  write_csv(x = average_per_population,
            append = append,
            file = file.path(base_directory,
                             "04_summarised_logs",
                             "average_per_population_symmetric.csv"))
  
  average_per_agent = data_chunk %>%
    group_by(run, group, agentID, condition) %>%
    dplyr::summarise(closure_ms_mean = mean(closure_ms),
                     closure_ms_q25  = quantile(closure_ms, 0.25),
                     closure_ms_q50  = quantile(closure_ms, 0.50),
                     closure_ms_q75  = quantile(closure_ms, 0.75),
                     .groups = "drop_last")
  
  write_csv(x = average_per_agent,
            append = append,
            file = file.path(base_directory,
                             "04_summarised_logs",
                             "average_per_agent_symmetric.csv"))
}
```


# Asymmetric scenario

```{r asymmetric}
simulations = asymmetric_contact_scenario

for (run_id in 1:25) {
  if (run_id == 1) {
    append = FALSE
  } else  {
    append = TRUE
  }

  data_chunk = get_data_from_all_runs(simulations,
                                      results_directory = results_directory,
                                      snapshot_ids = seq(0, 60, by = 5),
                                      run_ids = run_id)
  
  average_per_population = data_chunk %>%
    group_by(run, group, condition) %>%
    dplyr::summarise(closure_ms_mean = mean(closure_ms),
                     closure_ms_q25  = quantile(closure_ms, 0.25),
                     closure_ms_q50  = quantile(closure_ms, 0.50),
                     closure_ms_q75  = quantile(closure_ms, 0.75),
                     .groups = "drop_last")
  
  write_csv(x = average_per_population,
            append = append,
            file = file.path(base_directory,
                             "04_summarised_logs",
                             "average_per_population_asymmetric.csv"))
  
  average_per_agent = data_chunk %>%
    group_by(run, group, agentID, condition) %>%
    dplyr::summarise(closure_ms_mean = mean(closure_ms),
                     closure_ms_q25  = quantile(closure_ms, 0.25),
                     closure_ms_q50  = quantile(closure_ms, 0.50),
                     closure_ms_q75  = quantile(closure_ms, 0.75),
                     .groups = "drop_last")
  
  write_csv(x = average_per_agent,
            append = append,
            file = file.path(base_directory,
                             "04_summarised_logs",
                             "average_per_agent_asymmetric.csv"))
}
```


# Within-group only scenario

```{r asymmetric}
simulations = within_group_contact_scenario

for (run_id in 1:100) {
  if (run_id == 1) {
    append = FALSE
  } else  {
    append = TRUE
  }
  
  data_chunk = get_data_from_all_runs(simulations,
                                      results_directory = results_directory,
                                      snapshot_ids = 0:15,
                                      run_ids = run_id)
  
  average_per_population = data_chunk %>%
    group_by(run, group, condition) %>%
    dplyr::summarise(closure_ms_mean = mean(closure_ms),
                     closure_ms_q25  = quantile(closure_ms, 0.25),
                     closure_ms_q50  = quantile(closure_ms, 0.50),
                     closure_ms_q75  = quantile(closure_ms, 0.75),
                     .groups = "drop_last")
  
  write_csv(x = average_per_population,
            append = append,
            file = file.path(base_directory,
                             "04_summarised_logs",
                             "average_per_population_within_group.csv"))
  
  average_per_agent = data_chunk %>%
    group_by(run, group, agentID, condition) %>%
    dplyr::summarise(closure_ms_mean = mean(closure_ms),
                     closure_ms_q25  = quantile(closure_ms, 0.25),
                     closure_ms_q50  = quantile(closure_ms, 0.50),
                     closure_ms_q75  = quantile(closure_ms, 0.75),
                     .groups = "drop_last")
  
  write_csv(x = average_per_agent,
            append = append,
            file = file.path(base_directory,
                             "04_summarised_logs",
                             "average_per_agent_within_group.csv"))
}
```

# High time resolution

## Symmetric scenario

```{r symmetric}
simulations = symmetric_contact_scenario

for (run_id in 1:25) {
  for (snapshot_id in 0:60) {
    if (run_id == 1 && snapshot_id == 1) {
      append = FALSE
    } else  {
      append = TRUE
    }
    
    data_chunk = get_data_from_all_runs(simulations,
                                        results_directory = results_directory,
                                        snapshot_ids = snapshot_id,
                                        run_ids = run_id)
    
    average_per_population = data_chunk %>%
      group_by(run, group, condition) %>%
      dplyr::summarise(closure_ms_q50  = quantile(closure_ms, 0.50),
                       .groups = "drop_last")
    
    write_csv(x = average_per_population,
              append = append,
              file = file.path(base_directory,
                               "04_summarised_logs",
                               "average_per_population_symmetric_high_time_resolution.csv"))
    
    average_per_agent = data_chunk %>%
      group_by(run, group, agentID, condition) %>%
      dplyr::summarise(closure_ms_q50  = quantile(closure_ms, 0.50),
                       .groups = "drop_last")
    
    write_csv(x = average_per_agent,
              append = append,
              file = file.path(base_directory,
                               "04_summarised_logs",
                               "average_per_agent_symmetric_high_time_resolution.csv"))
    
  }
}
```


## Asymmetric scenario

```{r asymmetric}
simulations = asymmetric_contact_scenario

for (run_id in 1:25) {
  for (snapshot_id in 0:60) {
    if (run_id == 1 && snapshot_id == 1) {
      append = FALSE
    } else  {
      append = TRUE
    }
    
    data_chunk = get_data_from_all_runs(simulations,
                                        results_directory = results_directory,
                                        snapshot_ids = snapshot_id,
                                        run_ids = run_id)
    
    average_per_population = data_chunk %>%
      group_by(run, group, condition) %>%
      dplyr::summarise(closure_ms_mean = mean(closure_ms),
                       closure_ms_q25  = quantile(closure_ms, 0.25),
                       closure_ms_q50  = quantile(closure_ms, 0.50),
                       closure_ms_q75  = quantile(closure_ms, 0.75),
                       .groups = "drop_last")
    
    write_csv(x = average_per_population,
              append = append,
              file = file.path(base_directory,
                               "04_summarised_logs",
                               "average_per_population_asymmetric_high_time_resolution.csv"))
    
    average_per_agent = data_chunk %>%
      group_by(run, group, agentID, condition) %>%
      dplyr::summarise(closure_ms_mean = mean(closure_ms),
                       closure_ms_q25  = quantile(closure_ms, 0.25),
                       closure_ms_q50  = quantile(closure_ms, 0.50),
                       closure_ms_q75  = quantile(closure_ms, 0.75),
                       .groups = "drop_last")
    
    write_csv(x = average_per_agent,
              append = append,
              file = file.path(base_directory,
                               "04_summarised_logs",
                               "average_per_agent_asymmetric_high_time_resolution.csv"))
  }
}
```
