tests/test-gadgetlikelihood.R

library(Rgadget)
library(mfdb)  # TODO: Ick?
library(unittest, quietly = TRUE)
library(magrittr)

ver_string <- paste("; Generated by Rgadget", utils::packageVersion("Rgadget"))

# Read all files in a directory
dir_list <- function (dir) {
    files <- sort(list.files(dir, full.names = FALSE, recursive = TRUE), method = 'radix')
    structure(
        lapply(files, function (f) readLines(file.path(dir, f), n = -1)),
        names = files
    )
}

ok_group("Can create multiple likelihood files and update", {
    path <- tempfile()

    gadgetlikelihood('likelihood', path, missingOkay = TRUE) %>%
        gadget_update('penalty', name = "frank", weight = 0.2) %>%
        gadget_update('penalty', name = "arnold", weight = 0.3) %>%
        gadget_update('understocking', weight = 0.4) %>%
        write.gadget.file(path)
    gadgetlikelihood('likelihood2', path, missingOkay = TRUE) %>%
        gadget_update('penalty', weight = 0.8) %>%
        write.gadget.file(path)
    
    ok(ut_cmp_identical(dir_list(path), list(
        "Data/arnold.penaltyfile" = c(
            ver_string,
            "; -- data --",
            "; switch\tpower",
            "default\t2",
        NULL),
        "Data/frank.penaltyfile" = c(
            ver_string,
            "; -- data --",
            "; switch\tpower",
            "default\t2",
        NULL),
        "Data/penalty.penaltyfile" = c(
            ver_string,
            "; -- data --",
            "; switch\tpower",
            "default\t2",
        NULL),
        likelihood = c(
            ver_string,
            "; ",
            "[component]",
            "name\tfrank",
            "weight\t0.2",
            "type\tpenalty",
            "datafile\tData/frank.penaltyfile",
            "; ",
            "[component]",
            "name\tarnold",
            "weight\t0.3",
            "type\tpenalty",
            "datafile\tData/arnold.penaltyfile",
            "; ",
            "[component]",
            "name\tunderstocking",
            "weight\t0.4",
            "type\tunderstocking",
        NULL),
        likelihood2 = c(
            ver_string,
            "; ",
            "[component]",
            "name\tpenalty",
            "weight\t0.8",
            "type\tpenalty",
            "datafile\tData/penalty.penaltyfile",
        NULL),
        main = c(
            ver_string,
            "timefile\t",
            "areafile\t",
            "printfiles\t; Required comment",
            "[stock]",
            "[tagging]",
            "[otherfood]",
            "[fleet]",
            "[likelihood]",
            "likelihoodfiles\tlikelihood2\tlikelihood",
        NULL)
    )), "Wrote out 2 likelihood files")

    gadgetlikelihood('likelihood', path, missingOkay = TRUE) %>%
        gadget_update('penalty', name = "frank", weight = 0.99) %>%
        write.gadget.file(path)

    ok(ut_cmp_identical(dir_list(path), list(
        "Data/arnold.penaltyfile" = c(
            ver_string,
            "; -- data --",
            "; switch\tpower",
            "default\t2",
        NULL),
        "Data/frank.penaltyfile" = c(
            ver_string,
            "; -- data --",
            "; switch\tpower",
            "default\t2",
        NULL),
        "Data/penalty.penaltyfile" = c(
            ver_string,
            "; -- data --",
            "; switch\tpower",
            "default\t2",
        NULL),
        likelihood = c(
            ver_string,
            "; ",
            "[component]",
            "name\tfrank",
            "weight\t0.99",
            "type\tpenalty",
            "datafile\tData/frank.penaltyfile",
            "; ",
            "[component]",
            "name\tarnold",
            "weight\t0.3",
            "type\tpenalty",
            "datafile\tData/arnold.penaltyfile",
            "; ",
            "[component]",
            "name\tunderstocking",
            "weight\t0.4",
            "type\tunderstocking",
        NULL),
        likelihood2 = c(
            ver_string,
            "; ",
            "[component]",
            "name\tpenalty",
            "weight\t0.8",
            "type\tpenalty",
            "datafile\tData/penalty.penaltyfile",
        NULL),
        main = c(
            ver_string,
            "timefile\t",
            "areafile\t",
            "printfiles\t; Required comment",
            "[stock]",
            "[tagging]",
            "[otherfood]",
            "[fleet]",
            "[likelihood]",
            "likelihoodfiles\tlikelihood\tlikelihood2",
        NULL)
    )), "Updated one component in first likelihood file")
})

ok_group("Can create likelihood files in a variant directory", {
    path <- tempfile()
    variant_dir <- gadget.variant.dir(path, 'bs.1')

    gadgetlikelihood('likelihood', path, missingOkay = TRUE) %>%
        gadget_update('penalty', name = "frank", weight = 0.2) %>%
        gadget_update('understocking', weight = 0.4) %>%
        write.gadget.file(path)
    gadgetlikelihood('likelihood_bootstrap', variant_dir, missingOkay = TRUE) %>%
        gadget_update('penalty', weight = 0.8) %>%
        write.gadget.file(variant_dir)
    
    ok(ut_cmp_identical(dir_list(path), list(
        "Data/frank.penaltyfile" = c(
            ver_string,
            "; -- data --",
            "; switch\tpower",
            "default\t2",
        NULL),
        "bs.1/Data/penalty.penaltyfile" = c(
            ver_string,
            "; -- data --",
            "; switch\tpower",
            "default\t2",
        NULL),
        "bs.1/likelihood_bootstrap" = c(
            ver_string,
            "; ",
            "[component]",
            "name\tpenalty",
            "weight\t0.8",
            "type\tpenalty",
            "datafile\tbs.1/Data/penalty.penaltyfile",
        NULL),
        "bs.1/main" = c(
            ver_string,
            "timefile\t",
            "areafile\t",
            "printfiles\t; Required comment",
            "[stock]",
            "[tagging]",
            "[otherfood]",
            "[fleet]",
            "[likelihood]",
            "likelihoodfiles\tbs.1/likelihood_bootstrap\tlikelihood",
        NULL),
        likelihood = c(
            ver_string,
            "; ",
            "[component]",
            "name\tfrank",
            "weight\t0.2",
            "type\tpenalty",
            "datafile\tData/frank.penaltyfile",
            "; ",
            "[component]",
            "name\tunderstocking",
            "weight\t0.4",
            "type\tunderstocking",
        NULL),
        main = c(
            ver_string,
            "timefile\t",
            "areafile\t",
            "printfiles\t; Required comment",
            "[stock]",
            "[tagging]",
            "[otherfood]",
            "[fleet]",
            "[likelihood]",
            "likelihoodfiles\tlikelihood",
        NULL)
    )), "Wrote out 2 likelihood files")
})
gadget-framework/rgadget documentation built on July 20, 2022, 12:16 p.m.