Process landscape snapshots

library(data.table)
library(glue)
library(stringr)

# load local functions
devtools::load_all()

Items per cell

Prepare the data paths.

# read paths
paths <- list.dirs("data/", recursive = F)
paths <- paths[grep("rep_001", paths)]

# select gens
gens_for_figure <- stringr::str_pad(c(1, 10, 50, 950), width = 5, pad = "0")

# make df
data_landscape = CJ(
  path = paths,
  gen = gens_for_figure
)

# get replicate
data_landscape[, rep := as.numeric(
  stringr::str_extract_all(path, "rep_(\\d{3})") |> 
    stringr::str_extract_all("\\d{3}")
)]

# get sim type
data_landscape[, sim_type := stringr::str_extract(
  path, "obligate|facultative|foragers|random"
 ) 
]

data_landscape[, regrowth := as.numeric(str_extract(path, "(0\\.\\d+)"))]

# get paths
data_landscape$image <- glue_data(data_landscape, "{path}/{gen}.png")

Get data from prepared paths.

# get data
data_landscape$data <- lapply(data_landscape$image,
  read_landscape,
  layer = 4,
  crop_dim = 60,
  type = "items"
)

# convert gen to numeric
data_landscape[, gen := as.numeric(gen)]

# unlist
data_landscape <- data_landscape[, unlist(data, recursive = F),
  by = c("sim_type", "gen", "regrowth")
]

# save
fwrite(data_landscape,
  file = "data/results/data_landscape_progression.csv"
)

Foragers per cell

# read paths
paths <- list.dirs("data/", recursive = F)
paths <- paths[grep("rep_001", paths)]

# select gens
gens_for_figure <- stringr::str_pad(c(1, 10, 50, 950), width = 5, pad = "0")

# get paths for files
data_landscape <- CJ(
  gen = gens_for_figure,
  path = paths
)

# get replicate
data_landscape[, rep := as.numeric(
  stringr::str_extract_all(path, "rep_(\\d{3})") |> 
    stringr::str_extract_all("\\d{3}")
)]

# get sim type
data_landscape[, sim_type := stringr::str_extract(
  path, "obligate|facultative|foragers|random"
 ) 
]

data_landscape[, regrowth := as.numeric(str_extract(path, "(0\\.\\d+)"))]

# get paths
data_landscape$image <- glue_data(data_landscape, "{path}/{gen}.png")

# get data
data_landscape$agent_data <- lapply(data_landscape$image,
  read_landscape,
  layer = c(1, 2, 3),
  crop_dim = 60,
  type = "items"
)

# convert gen to numeric
data_landscape[, gen := as.numeric(gen)]

# unlist
data_landscape <- data_landscape[, unlist(agent_data, recursive = F),
  by = c("sim_type", "gen", "regrowth")
]

# rename items to agents
setnames(data_landscape, "items", "agents")

# rescale
data_landscape$agents = data_landscape$agents / 
  (min(data_landscape$agents[data_landscape$agents > 0]))

# save data
fwrite(data_landscape, file = "data/results/data_agent_locations.csv")


pratikunterwegs/kleptomove-ms documentation built on March 17, 2023, 1:39 a.m.