Vis evolved paths agent paths over generations

# load libraries
library(data.table)
library(glue)
library(stringr)

library(ggplot2)
library(patchwork)

# source funs
source("R/fun_dist_disp.R")
source("R/fun_read_pos.R")
source("R/fun_steplength_timescale.R")
source("R/fun_read_config.R")
source("R/fun_link_landscape.R")

List files and read data

needs to be made recursive over dirs.

paths = list.dirs(path = "data", recursive = F)
paths = paths[grep("sim", paths)]

Read parameters

params = lapply(paths, read_config)

Select individual paths

Read scaled weights

# read scaled weight data
data_sc_wt = fread("data/data_lt_wts_scaled.csv")
data_sc_wt = data_sc_wt[growth %in% c(0.01, 0.03) & id < 100]

# assign types
data_sc_wt[, move_strategy := dplyr::case_when(
  wt_4 > 0.6 ~ "prey tracking",
  ((wt_4 > 0) & (wt_3 > 0) & (abs(wt_4 - wt_3) < 0.1))  ~ "prey and handler tracking",
  wt_3 > 0.5 ~ "handler tracking",
  wt_3 < -0.5 ~ "handler avoiding",
  wt_2 > 0.5 ~ "non-handler tracking",
  wt_2 < -0.5 ~ "non-handler avoiding",
  T ~ "other"
)]

# get ids with each strategy per sim
data_to_get = data_sc_wt[, 
  list(ids = list(head(unique(id)))), 
  by = c("move_strategy", "comp_strat", "type", "growth", "rep_")
]

# unlist
data_to_get = data_to_get[, 
  list(id = unlist(ids)), 
  by = c("type", "growth", "rep_", "move_strategy", "comp_strat")
]

# select first four of all strategies
data_to_get = data_to_get[, 
  .SD[seq(4)], 
  by = c("type", "growth", "move_strategy", "comp_strat")
]

# drop NAs
data_to_get = data_to_get[complete.cases(data_to_get)]

Read from paths

Prepare paths.

params = lapply(params, function(df) as.data.table(as.list(df))) |> 
  rbindlist()
params[, paths := paths]

params[, c("growth", "rep_") := lapply(.SD, as.numeric),
  .SD = c("growth", "rep_")
]

# link ids to read
paths_to_read = merge(data_to_get, params, all.x = T,
  by = c("type", "growth", "rep_")
)

# nest ids
paths_to_read = paths_to_read[, list(
  ids = list(id)
), by = c("type", "growth", "rep_", "move_strategy", "comp_strat", "paths")]
data_id = copy(paths_to_read)
data_id[, data := Map(paths, ids, f = function(path, ids) {
    fread(glue("{path}/00250_pos.csv"))[id %in% ids,]
  }
)]

# unlist data
data_id = data_id[, unlist(data, recursive = F), 
  by = c("rep_", "growth", "type", "move_strategy", "comp_strat")
]

# bind
data_id[, day := cumsum(t %% 100 == 0), by = c("id", "rep_", "growth", "type")]
data_id[, interval := cumsum(t %% 1000 == 0), by = c("id", "rep_", "growth", "type")]

data_id = data_id[interval %in% c(1,3,6,8)]

data_id = split(data_id, by = c("id", "interval", "growth", "type", "rep_"))

# transform paths using first point as origin to handle wrapping
data_id = lapply(data_id, function(df) {
  setorder(df, t)
  df$x_wrap = scale_wrap_coords(df$x) + rnorm(length(df$x), 0, 0.2)
  df$y_wrap = scale_wrap_coords(df$y) + rnorm(length(df$x), 0, 0.2)
  df
})

# bind
data_id = rbindlist(data_id)
prod = read_landscape("data/data_parameters/kernels32.png")
data_id = link_landscape(data_id, prod)
setorder(data_id, growth, type, id, t)
fwrite(data_id, file = "data/data_lt_plot.csv")


pratikunterwegs/process-pattern documentation built on Dec. 22, 2021, 9:50 a.m.