Skip to content

Commit

Permalink
feat(FIMSFrame): added start_year, end_year
Browse files Browse the repository at this point in the history
start_year and end_year are slots in FIMSFrame that are automatically
determined by the data, users do not need to add anything. If they want
to change the start or end year, then they could add dummy data such as
a year of catch for a fleet with zero landings.
  • Loading branch information
k-doering-NOAA authored and peterkuriyama-NOAA committed Nov 22, 2023
1 parent c07ab58 commit acc80b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
33 changes: 23 additions & 10 deletions R/fimsframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ setClass(
slots = c(
data = "data.frame", # can use c( ) or list here.
fleets = "numeric",
nyrs = "integer"
nyrs = "integer",
start_year = "integer",
end_year = "integer"
)
)

Expand Down Expand Up @@ -55,6 +57,12 @@ setMethod("fleets", "FIMSFrame", function(x) x@fleets)
setGeneric("nyrs", function(x) standardGeneric("nyrs"))
setMethod("nyrs", "FIMSFrame", function(x) x@nyrs)

setGeneric("start_year", function(x) standardGeneric("start_year"))
setMethod("start_year", "FIMSFrame", function(x) x@start_year)

setGeneric("end_year", function(x) standardGeneric("end_year"))
setMethod("end_year", "FIMSFrame", function(x) x@end_year)

# additional accessors for FIMSFrameAge
setGeneric("ages", function(x) standardGeneric("ages"))
setMethod("ages", "FIMSFrameAge", function(x) x@ages)
Expand Down Expand Up @@ -252,6 +260,7 @@ setValidity(
if (!"dateend" %in% colnames(object@data)) {
errors <- c(errors, "data must contain 'uncertainty'")
}


# TODO: Add checks for other slots

Expand Down Expand Up @@ -302,10 +311,10 @@ setValidity(
#' on the child class. Use [showClass()] to see all available slots.
FIMSFrame <- function(data) {
# Get the earliest and latest year of data and use to calculate n years for population simulation
start_yr <- as.numeric(strsplit(min(data[["datestart"]], na.rm = TRUE), "-")[[1]][1])
end_yr <- as.numeric(strsplit(max(data[["dateend"]], na.rm = TRUE), "-")[[1]][1])
nyrs <- as.integer(end_yr - start_yr + 1)
years <- start_yr:end_yr
start_year <- as.integer(strsplit(min(data[["datestart"]], na.rm = TRUE), "-")[[1]][1])
end_year <- as.integer(strsplit(max(data[["dateend"]], na.rm = TRUE), "-")[[1]][1])
nyrs <- as.integer(end_year - start_year + 1)
years <- start_year:end_year

# Get the fleets represented in the data
fleets <- unique(data[["name"]])[grep("fleet", unique(data[["name"]]))]
Expand All @@ -317,7 +326,9 @@ FIMSFrame <- function(data) {
out <- new("FIMSFrame",
data = data,
fleets = fleets,
nyrs = nyrs
nyrs = nyrs,
start_year = start_year,
end_year = end_year
)
return(out)
}
Expand All @@ -326,10 +337,10 @@ FIMSFrame <- function(data) {
#' @rdname FIMSFrame
FIMSFrameAge <- function(data) {
# Get the earliest and latest year of data and use to calculate n years for population simulation
start_yr <- as.numeric(strsplit(min(data[["datestart"]], na.rm = TRUE), "-")[[1]][1])
end_yr <- as.numeric(strsplit(max(data[["dateend"]], na.rm = TRUE), "-")[[1]][1])
nyrs <- as.integer(end_yr - start_yr + 1)
years <- start_yr:end_yr
start_year <- as.integer(strsplit(min(data[["datestart"]], na.rm = TRUE), "-")[[1]][1])
end_year <- as.integer(strsplit(max(data[["dateend"]], na.rm = TRUE), "-")[[1]][1])
nyrs <- as.integer(end_year - start_year + 1)
years <- start_year:end_year
# Get the fleets represented in the data
fleets <- unique(data[["name"]])[grep("fleet", unique(data[["name"]]))]
fleets <- as.numeric(unlist(lapply(strsplit(fleets, "fleet"), function(x) x[2])))
Expand All @@ -346,6 +357,8 @@ FIMSFrameAge <- function(data) {
data = data,
fleets = fleets,
nyrs = nyrs,
start_year = start_year,
end_year = end_year,
ages = ages,
nages = nages,
weightatage = weightatage
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-fimsframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ test_that("Accessors work as expected in FIMSFrame", {

expect_type(nyrs(fims_frame), "integer")
expect_length(nyrs(fims_frame), 1)

expect_type(start_year(fims_frame), "integer")
expect_length(start_year(fims_frame), 1)

expect_type(end_year(fims_frame), "integer")
expect_length(end_year(fims_frame), 1)
})

test_that("Accessors work as expected in FIMSFrameAge", {
Expand All @@ -42,6 +48,13 @@ test_that("Accessors work as expected in FIMSFrameAge", {
expect_type(nyrs(age_frame), "integer")
expect_length(nyrs(age_frame), 1)

expect_type(start_year(age_frame), "integer")
expect_length(start_year(age_frame), 1)

expect_type(end_year(age_frame), "integer")
expect_length(end_year(age_frame), 1)


expect_vector(ages(age_frame), ptype = integer())

expect_type(nages(age_frame), "integer")
Expand Down

0 comments on commit acc80b0

Please sign in to comment.