Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update names in BIOLOGY table #711

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 13 additions & 17 deletions R/SS_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -2400,6 +2400,11 @@ SS_output <-
adjust1 = ifelse(custom, 2, 1),
header = TRUE, type.convert = TRUE
)
# updated BIOLOGY table names based on change July 2022 change
# https://github.com/nmfs-stock-synthesis/stock-synthesis/issues/348
biology <- df.rename(biology,
oldnames = c("Low", "Mean_Size", "Wt_len_F", "Mat_len", "Spawn", "Wt_len_M", "Fecundity"),
newnames = c("Len_lo", "Len_mean", "Wt_F", "Mat", "Mat*Fec", "Wt_M", "Fec"))

# determine fecundity type
FecType <- 0
Expand Down Expand Up @@ -2437,7 +2442,7 @@ SS_output <-
FecPar2name <- pl[FecGrep5[1]]
}
if (is.na(lbinspop[1])) {
lbinspop <- biology[["Low"]][biology[["GP"]] == 1]
lbinspop <- biology[["Len_lo"]][biology[["GP"]] == 1]
}

# warning for 3.30 models with multiple growth patterns that have
Expand All @@ -2456,29 +2461,20 @@ SS_output <-
# fix for extra header associated with extra column header
# for single sex models that got fixed in 3.30.16
if (nsexes == 1 &&
is.na(biology[["Fecundity"]][1]) &&
"Wt_len_M" %in% names(biology)) {
is.na(biology[["Fec"]][1]) &&
"Wt_M" %in% names(biology)) {
# copy Wt_len_M to Fecundity
biology[["Fecundity"]] <- biology[["Wt_len_M"]]
biology[["Fec"]] <- biology[["Wt_M"]]
# remove Wt_len_M
biology <- biology[, !names(biology) %in% "Wt_len_M"]
biology <- biology[, !names(biology) %in% "Wt_M"]
}

# test to figure out if fecundity is proportional to spawning biomass

# first get weight-at-length column (Wt_len_F for 2-sex models,
# Wt_len for 1-sex models starting with 3.30.16)
if ("Wt_len" %in% names(biology)) {
Wt_len_F <- biology[["Wt_len"]]
} else {
Wt_len_F <- biology[["Wt_len_F"]]
}

# check for any mismatch between weight-at-length and fecundity
returndat[["SpawnOutputUnits"]] <-
ifelse(!is.null(biology[["Fecundity"]][1]) &&
!is.na(biology[["Fecundity"]][1]) &&
any(Wt_len_F != biology[["Fecundity"]]),
ifelse(!is.null(biology[["Fec"]][1]) &&
!is.na(biology[["Fec"]][1]) &&
any(biology[["Wt_F"]] != biology[["Fec"]]),
"numbers", "biomass"
)
}
Expand Down
2 changes: 1 addition & 1 deletion R/SSbiologytables.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ SSbiologytables <- function(replist = NULL, printfolder = "tables", dir = "defau

# Table
# Age: Ave Len - Ave Wgt - % mature (by sex)
# "Mat*Fecund" is = biology[["Fecundity"]] %*% alk (mat = 1, fecundity = fecundity_l * ALK)
# "Mat*Fecund" is = biology[["Fec"]] %*% alk (mat = 1, fecundity = fecundity_l * ALK)
bio <- data.frame(
Age = biology[biology[["Sex"]] == 1, "Age_Beg"],
Ave_Length_f = print(biology[biology[["Sex"]] == 1, "Len_Beg"], digits = 1),
Expand Down
63 changes: 28 additions & 35 deletions R/SSplotBiology.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,12 @@ SSplotBiology <-
## FecundAtAge <- ageselex[ageselex[["factor"]]=="Fecund", names(ageselex)%in%0:accuage]
## WtAtAge <- ageselex[ageselex[["factor"]]=="bodywt", names(ageselex)%in%0:accuage]

# column name for weight-at-length
# (Wt_len for 1-sex models starting with 3.30.16)
Wt_len_colname <- "Wt_len_F"
if ("Wt_len" %in% names(biology)) {
Wt_len_colname <- "Wt_len"
}

# determine fecundity type
# define labels and x-variable
if (FecType == 1) {
fec_ylab <- "Eggs per kg"
fec_xlab <- labels[8]
FecX <- biology[[Wt_len_colname]]
FecX <- biology[["Wt_F"]]
FecY <- FecPar1 + FecPar2 * FecX
}
if (labels[11] != "Default fecundity label") fec_ylab <- labels[11]
Expand Down Expand Up @@ -366,19 +359,19 @@ SSplotBiology <-
## This needs to be a function of sex since it can be called
## either once for a single sex model or twice to produce plots for
## each one.
x <- biology[["Mean_Size"]]
x <- biology[["Len_mean"]]
if (!wtatage_switch) { # if empirical weight-at-age is not used
if (!add) {
ymax <- max(biology[[Wt_len_colname]])
if (nsexes > 1) ymax <- max(ymax, biology[["Wt_len_M"]])
ymax <- max(biology[["Wt_F"]])
if (nsexes > 1) ymax <- max(ymax, biology[["Wt_M"]])
plot(x, x,
ylim = c(0, 1.1 * ymax), xlab = labels[1], ylab = labels[4], type = "n",
las = 1, yaxs = "i"
)
}
lines(x, biology[[Wt_len_colname]], type = "o", col = colvec[1])
lines(x, biology[["Wt_F"]], type = "o", col = colvec[1])
if (nsexes > 1) {
lines(x, biology[["Wt_len_M"]], type = "o", col = colvec[2])
lines(x, biology[["Wt_M"]], type = "o", col = colvec[2])
if (!add) {
legend(legendloc,
bty = "n", c("Females", "Males"),
Expand All @@ -403,16 +396,16 @@ SSplotBiology <-

maturity_plot <- function() { # maturity
if (!wtatage_switch) { # if empirical weight-at-age is not used
x <- biology[["Mean_Size"]]
if (min(biology[["Mat_len"]]) < 1) { # if length based
x <- biology[["Len_mean"]]
if (min(biology[["Mat"]]) < 1) { # if length based
if (!add) {
plot(x, biology[["Mat_len"]],
plot(x, biology[["Mat"]],
xlab = labels[1], ylab = labels[3],
las = 1, yaxs = "i", ylim = c(0, max(biology[["Mat_len"]])),
las = 1, yaxs = "i", ylim = c(0, max(biology[["Mat"]])),
type = "o", col = colvec[1]
)
}
if (add) lines(x, biology[["Mat_len"]], type = "o", col = colvec[1])
if (add) lines(x, biology[["Mat"]], type = "o", col = colvec[1])
} else { # else is age based
if (!add) {
plot(growdatF[["Age_Beg"]], growdatF[["Age_Mat"]],
Expand Down Expand Up @@ -535,32 +528,32 @@ SSplotBiology <-
points(FecX, FecY, col = colvec[2], pch = 19)
}
}
fecundityOK <- all(!is.na(biology[["Fecundity"]]))
fecundityOK <- all(!is.na(biology[["Fec"]]))
fec_weight_fn <- function() { # fecundity at weight from BIOLOGY section
ymax <- 1.1 * max(biology[["Fecundity"]])
ymax <- 1.1 * max(biology[["Fec"]])
if (!add) {
plot(biology[[Wt_len_colname]], biology[["Fecundity"]],
plot(biology[["Wt_F"]], biology[["Fec"]],
xlab = labels[8], ylab = labels[10],
las = 1, yaxs = "i", ylim = c(0, ymax), col = colvec[1], type = "o"
)
} else {
points(biology[["Mean_Size"]], biology[["Fecundity"]], col = colvec[1], type = "o")
points(biology[["Len_mean"]], biology[["Fec"]], col = colvec[1], type = "o")
}
}
fec_len_fn <- function() { # fecundity at length from BIOLOGY section
ymax <- 1.1 * max(biology[["Fecundity"]])
ymax <- 1.1 * max(biology[["Fec"]])
if (!add) {
plot(biology[["Mean_Size"]], biology[["Fecundity"]],
plot(biology[["Len_mean"]], biology[["Fec"]],
xlab = labels[9], ylab = labels[10],
las = 1, yaxs = "i", ylim = c(0, 1.1 * ymax), col = colvec[1], type = "o", yaxs = "i"
)
} else {
points(biology[["Mean_Size"]], biology[["Fecundity"]], col = colvec[1], type = "o")
points(biology[["Len_mean"]], biology[["Fec"]], col = colvec[1], type = "o")
}
}
spawn_output_len_fn <- function() { # spawning output at length
x <- biology[["Mean_Size"]]
y <- biology[["Spawn"]]
x <- biology[["Len_mean"]]
y <- biology[["Mat*Fec"]]
ymax <- 1.1 * max(y)
if (!add) {
plot(x, y,
Expand All @@ -585,7 +578,7 @@ SSplotBiology <-
}
}

ymax <- max(biology[["Mean_Size"]])
ymax <- max(biology[["Len_mean"]])
x <- growdatF[["Age_Beg"]]

main <- "Ending year expected growth (with 95% intervals)"
Expand Down Expand Up @@ -757,7 +750,7 @@ SSplotBiology <-
lab2 <- "Wt_Beg"
lab2long <- "Mean weight"
lab1max <- 1
lab2max <- max(c(biology[[Wt_len_colname]], biology[["Wt_len_M"]]), na.rm = TRUE)
lab2max <- max(c(biology[["Wt_F"]], biology[["Wt_M"]]), na.rm = TRUE)
lab1_axis_vec <- c(0, 0.5, 1)
}
# calculate scaling factor between CVs and SDs to share each panel
Expand Down Expand Up @@ -799,12 +792,12 @@ SSplotBiology <-
}
if (option == 2) {
# if plotting maturity and fecundity, then get this panel from length-based data
lines(biology[[Wt_len_colname]] * lab2_to_lab1_scale, biology[["Mean_Size"]],
lines(biology[["Wt_F"]] * lab2_to_lab1_scale, biology[["Len_mean"]],
col = colvec[col_index1], lwd = 3
)
lines(biology[["Mat_len"]], biology[["Mean_Size"]], col = colvec[col_index1], lty = "12")
lines(biology[["Mat"]], biology[["Len_mean"]], col = colvec[col_index1], lty = "12")
if (nsexes > 1) {
lines(biology[["Wt_len_M"]] * lab2_to_lab1_scale, biology[["Mean_Size"]],
lines(biology[["Wt_M"]] * lab2_to_lab1_scale, biology[["Len_mean"]],
col = colvec[2], lwd = 3, lty = 2
)
}
Expand Down Expand Up @@ -956,7 +949,7 @@ SSplotBiology <-
L_at_AmaxM <- Growth_Parameters[["L_a_A2"]][2]
LinfF <- Growth_Parameters[["Linf"]][1]
LinfM <- Growth_Parameters[["Linf"]][2]
ymax <- max(biology[["Mean_Size"]])
ymax <- max(biology[["Len_mean"]])
plot(0,
type = "n",
xlim = c(0, 1 + max(growdatF[["Age_Beg"]])),
Expand Down Expand Up @@ -1256,7 +1249,7 @@ SSplotBiology <-
}


x <- biology[["Mean_Size"]]
x <- biology[["Len_mean"]]
## NOTE: weight plots are now a special case since they are broken down
## by whether the model is 1-sex or 2-sex. In the latter two separate
## plots need to be made.
Expand Down Expand Up @@ -1320,7 +1313,7 @@ SSplotBiology <-
}
if (6 %in% subplots) {
file <- "bio6_maturity.png"
caption <- paste("Maturity at", ifelse(min(biology[["Mat_len"]]) < 1, "length", "age"))
caption <- paste("Maturity at", ifelse(min(biology[["Mat"]]) < 1, "length", "age"))
if (wtatage_switch) {
caption <- "Spawning output at age (maturity x fecundity)"
}
Expand Down