## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  echo = TRUE,
  message = FALSE,
  warning = FALSE,
  collapse = TRUE,
  comment = "#>"
)

library(cdmetapopR)

## ----overview-sketch, eval=FALSE----------------------------------------------
# mycv     <- ClassVars()
# mypv     <- PatchVars(class_vars = mycv)       # an object, not a path
# mypopv   <- PopVars(xyfilename = mypv)
# myrv     <- RunVars(Popvars = mypopv)
# run_dir  <- launch_cdmetapop(myrv, pythonFilepath = "python",
#                              CDMetaPOPFilepath = "CDMetaPOP.py")

## ----launch-example, eval=FALSE-----------------------------------------------
# run_dir  <- launch_cdmetapop(RunVars(), pythonFilepath = "python",
#                              CDMetaPOPFilepath = "CDMetaPOP.py")

## ----classvars-default--------------------------------------------------------
mycv <- ClassVars()
mycv$ages

## ----classvars-inspect--------------------------------------------------------
as.data.frame(mycv)[, c("Age class", "Body Size Mean", "Age Mortality Out")]

## ----classvars-modify---------------------------------------------------------
mycv$age_mortality_out <- c(0.6, 0.4, 0.2, 0.1, 0.1, 0.1)
mycv$maturation <- c(0, 0, 0.5, 1, 1, 1)
mycv$age_mortality_out

# Alternatively, edit the columns at creation
mycv <- ClassVars(age_mortality_out = c(0.6, 0.4, 0.2, 0.1, 0.1, 0.1),
                  maturation = c(0, 0, 0.5, 1, 1, 1))

## ----classvars-addrow---------------------------------------------------------
add_rows(mycv)
mycv$ages
mycv$maturation

## ----patchvars-build----------------------------------------------------------
mypv <- PatchVars(class_vars = mycv, genes_initialize = "random")
mypv$patch_id

## ----patchvars-modify---------------------------------------------------------
mypv$k <- c(300, 300, 500, 500, 200, 200, 200)
mypv$n0 <- 100

# Alternatively,
mypv <- PatchVars(k = c(300, 300, 500, 500, 200, 200, 200),
                  n0 = 100)

add_rows(mypv)
mypv$patch_id
mypv$k

## ----delimiters---------------------------------------------------------------
# One patch's mortality changing over time: 0, then 0.2, then 0.5
mypv$mortality_out <- "0|0.2|0.5"

# Eight patches, each with different moratlity values over time
mypv$mortality_out <- c("0|0.2|0.2","0|0.2|0.3","0|0.2|0.4","0|0.2|0.5","0|0.2|0.6","0|0.2|0.7","0|0.2|0.8","0|0.2|0.8")

# Eight patches, each with its own constant mortality
mypv$mortality_out <- c(0, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3)

## ----delimiters-validated, error=TRUE-----------------------------------------
try({
mypv$mortality_out <- "0|0.2|1.5"
})

## ----delimiters-reset, include=FALSE------------------------------------------
mypv$mortality_out <- c(0, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3)

## ----delimiters-tilde---------------------------------------------------------
# XX~XY
mycv$sex_ratio <- "0.5~0.5"

## ----cdmat--------------------------------------------------------------------
coords <- as.data.frame(mypv)[, c("X", "Y")]
cdmat <- create_cdmat(coords, method = "euclidean")

dim(cdmat)
round(cdmat[1:4, 1:4])

## ----popvars-build------------------------------------------------------------
mypopv <- PopVars(
  n_batches = 1,
  xyfilename = mypv,
  mate_cdmat = cdmat,
  migrateout_cdmat = cdmat,
  migrateback_cdmat = cdmat,
  stray_cdmat = cdmat,
  disperseLocal_cdmat = cdmat,
  correlation_matrix = "N",
  subpopmort_file = "N",
  betaFile_selection = "N"
)

## ----popvars-modify-----------------------------------------------------------
mypopv$loci <- 4
mypopv$matemoveno <- "4"

add_rows(mypopv)
mypopv$popmodel <- c("packing", "logistic_back")
as.data.frame(mypopv)[, c("loci", "alleles", "matemoveno", "popmodel")]

## ----runvars-build------------------------------------------------------------
myrv <- RunVars(n_runs = 1, Popvars = mypopv)
myrv$runtime <- 50
myrv$output_years <- 5
myrv

## ----write-one----------------------------------------------------------------
out_dir <- file.path(tempdir(), "cdmetapop_inputs")
dir.create(out_dir, showWarnings = FALSE)

write_cdmetapop(mycv, file.path(out_dir, "ClassVars.csv"))

## ----read-two-ways------------------------------------------------------------
cv_a <- read_cdmetapop(file.path(out_dir, "ClassVars.csv"), type = "ClassVars")
cv_b <- ClassVars(path = file.path(out_dir, "ClassVars.csv"))

identical(as.data.frame(cv_a), as.data.frame(cv_b))
identical(as.data.frame(cv_a), as.data.frame(mycv))

## ----read-override------------------------------------------------------------
cv_c <- ClassVars(path = file.path(out_dir, "ClassVars.csv"), maturation = 1)
cv_c$maturation

## ----write-warning, warning=TRUE----------------------------------------------
write_cdmetapop(myrv, file.path(out_dir, "RunVars_nested.csv"))

## ----launch, eval=FALSE-------------------------------------------------------
# run_dir <- launch_cdmetapop(
#   myrv,
#   pythonFilepath = "C:/Users/User1/anaconda3/python.exe",
#   CDMetaPOPFilepath = "C:/Users/User1/CDMetaPOP/src/CDMetaPOP.py"
# )

## ----launch-path, eval=FALSE--------------------------------------------------
# launch_cdmetapop("C:/Users/User1/CDMetaPOP/example_files", wait = TRUE)

## ----validation-enum, error=TRUE----------------------------------------------
try({
myrv$gridformat <- "csv"
})

## ----validation-range, error=TRUE---------------------------------------------
try({
mypv$straying_prob <- 1.5
})

## ----validation-length, error=TRUE--------------------------------------------
try({
mycv$maturation <- c(0, 1)
})

## ----read-results, eval=FALSE-------------------------------------------------
# summary_pop(run_dir, type = "N_Initial")

## ----read-results-live, fig.width=7, fig.height=4.5, out.width="100%"---------
example_out <- system.file("extdata", "Example_dat", package = "cdmetapopR")
summary_pop(example_out, type = "N_Initial")

## ----cleanup, include=FALSE---------------------------------------------------
unlink(out_dir, recursive = TRUE)

