Skip to content

Commit

Permalink
- Reduced all instances of "seed" to one set.seed() call at app start
Browse files Browse the repository at this point in the history
- Used unicode encoding instead of the actual symbol for plots' y labels
- Removed crashing species by modifying random number scale factor in modifyFishingMatrix()
  • Loading branch information
rklasky committed May 18, 2024
1 parent e133cfb commit 4360095
Show file tree
Hide file tree
Showing 41 changed files with 25 additions and 30 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 1 addition & 2 deletions tests/testthat/test-constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ CREATE_BASELINE_FILES <- FALSE

NUMBER_OF_STEPS <- 5 # should be an odd multiple of nrows=600 (i.e., 5,15,30)
FACTOR_VALUE <- 5
SEED_VALUE <- 1
SEED_OFFSET <- 2000
SEED_VALUE <- 7
TOLERANCE_VALUE <- 1e-5
RUN_QUIET <- TRUE
YLIMIT_DIFFERENCE_PLOTS <- 0.05
Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/test-rpath.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ print(paste0("OUTPUT_DATA_DIR: ", OUTPUT_DATA_DIR)) #RSK
print(paste0("INPUT_DATA_DIR_BASELINE: ", INPUT_DATA_DIR_BASELINE)) #RSK
print(paste0("INPUT_DATA_DIR_CURRENT: ", INPUT_DATA_DIR_CURRENT)) #RSK

# Set the seed here so that all runs will be deterministic.
set.seed(SEED_VALUE)

# Create the current and output directories if they don't already exist.
if (! dir.exists(INPUT_DATA_DIR_CURRENT)) {
dir.create(INPUT_DATA_DIR_CURRENT,recursive=TRUE)
Expand Down Expand Up @@ -612,7 +615,6 @@ testthat::test_that("Rpath Unit Tests", {
# in the tests.yml file but don't fail when run via R-CMD-Check.yml.
#
# This line doesn't fail in git actions (it's just not the exact logic I need)
# set.seed(modNum*typeNum*SEED_OFFSET)
# REcosystem_scenario_jitter$forcing$ForcedBio <- jitter(REcosystem_scenario_jitter$forcing$ForcedBio,factor=FACTOR_VALUE)
#
# Another way to jitter
Expand All @@ -626,7 +628,7 @@ testthat::test_that("Rpath Unit Tests", {
# speciesBiomass <- REcosystem_scenario_jitter$start_state$Biomass[aSpecies]
# totSpeciesBiomass <- totSpeciesBiomass + speciesBiomass
# for (month in 1:numMonths) {
# randVal <- randomNumber(modNum*typeNum*SEED_OFFSET+speciesNum*numMonths+month)
# randVal <- ...
# jitteredValue <- speciesBiomass * (1.0 + randVal)
# totRandVal <- totRandVal + randVal
# jitterVector <- append(jitterVector,jitteredValue)
Expand Down Expand Up @@ -742,7 +744,7 @@ testthat::test_that("Rpath Unit Tests", {
numMonths <- nrow(REcosystem_scenario$fishing$ForcedEffort)
REcosystem_scenario_jitter <- REcosystem_scenario
fishingOriginalData <- list(REcosystem_scenario$fishing$ForcedEffort, REcosystem_scenario$fishing$ForcedFRate, REcosystem_scenario$fishing$ForcedCatch)
typeData <- list(FORCED_EFFORT,FORCED_FRATE,FORCED_CATCH)
typeData <- list(FORCED_EFFORT, FORCED_FRATE, FORCED_CATCH)
for (i in 1:length(fishingOriginalData)) {
theTypeData <- typeData[[i]]
modNum <- modNum + 1
Expand Down
14 changes: 6 additions & 8 deletions tests/testthat/test-utils-jitter.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ source("test-constants.R")
#'
#' @param matrix : data matrix to be jittered
#' @param factor : the R jitter factor (the larger the number the greater the jitter amount)
#' @param seedOffset : offset to the seed value, useful for getting "groups" of seed values
#' @param xlabel : x axis label for plot
#' @param ylabel : y axis label for plot
#' @param title : main title for plot
#'
#' @return Returns the jittered matrix
#'
addJitter <- function(matrix,seedOffset,xlabel,ylabel,title) {
set.seed(seedOffset) # *SEED_VALUE)
addJitter <- function(matrix,xlabel,ylabel,title) {
# From jitter() doc: If amount == 0, jitter returns factor * z/50, where
# z = max(x0) - min(x), aka the range. So if factor=5 and amount=0, jitter()
# returns a random value within a tenth of the range.
Expand All @@ -28,26 +26,26 @@ addJitter <- function(matrix,seedOffset,xlabel,ylabel,title) {

#' Create a jittered vector
#'
#' Used for jittering a matrix column. Can't use replicate because need to pass a different seed for every value for reproducibility.
#' Used for jittering a matrix column.
#'
#' @param typeData : the type of data to be jittered
#' @param value : value to add jitter to
#' @param numElements : number of elements in vector
#' @param seedOffset : offset to the main seed value
#' @param xlabel : x axis label for plot
#' @param ylabel : y axis label for plot
#' @param title : main title for plot
#' @param title : the type of random number (positive only or any)
#'
#' @return Returns the column-jittered matrix
#'
createJitterVectorFromValue <- function(typeData,value,numElements,seedOffset,xlabel,ylabel,title,randomNumberType) {
createJitterVectorFromValue <- function(typeData,value,numElements,xlabel,ylabel,title,randomNumberType) {
jitterVector <- c()
migrationScaleFactor <- 1
if (typeData == FORCED_MIGRATION) {
migrationScaleFactor = FORCED_MIGRATION_SCALE_FACTOR_JITTER # = 1000
}
for (i in 1:numElements) {
# jitteredValue <- addJitter(value,seedOffset+i,'','','')
randVal <- randomNumber(seedOffset+i,migrationScaleFactor*JITTER_AMOUNT_PCT,randomNumberType) # JITTER_AMOUNT_PCT = 0.01
randVal <- randomNumber(migrationScaleFactor*JITTER_AMOUNT_PCT,randomNumberType)
jitteredValue <- value * (1.0 + randVal)
jitterVector <- append(jitterVector,jitteredValue)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-utils-plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ source("test-constants.R")
plotResultsSuperimposed <- function(BaseData,CurrData,baseAlg,currAlg,tableName,forcedData,forcedType,species) {
plots <- list()
group <- species
yLabel <- "Biomass (mt/km²)"
yLabel <- "Biomass (mt/km\U00B2)"
currDf <- data.frame()
baseDf <- data.frame()

Expand Down Expand Up @@ -80,7 +80,7 @@ plotResultsSuperimposed <- function(BaseData,CurrData,baseAlg,currAlg,tableName,
plotResultsDifference <- function(BaseData,CurrData,baseAlg,currAlg,tableName,forcedData,forcedType,species) {
plots <- list()
group <- species
yLabel <- "Biomass (mt/km²)"
yLabel <- "Biomass (mt/km\U00B2)"
diffDf <- data.frame()

for (member in group) {
Expand Down
26 changes: 11 additions & 15 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ source("test-constants.R")

#' Random number generator
#'
#' This function returns a seeded random number from a uniform distribution between
#' This function returns a random number from a uniform distribution between
#' a min and max value.
#'
#' @param seed : the random "seed" value to make the function deterministic
#' @param pctToJitter : the percent to jitter (value=0.5 means 50%)
#' @param positiveOnly : boolean that signifies random number returned is limited to non-negative numbers
#'
#' @return Returns the random value
#'
randomNumber <- function(seed,pctToJitter,positiveOnly) {
set.seed(seed)
randomNumber <- function(pctToJitter,positiveOnly) {
minJitter <- -pctToJitter
maxJitter <- pctToJitter
if (positiveOnly) {
Expand Down Expand Up @@ -112,7 +112,7 @@ modifyFishingMatrix <- function(modNum,species,fleets,typeData,forcingData,model
const2 <- 1
upperLimit <- 0.5
usePBValue <- FALSE
scaleFactorPB <- 0.01 # Needed this to add enough randomness to the plots, else they'd be fairly smooth
scaleFactorPB <- 0.0001 # Needed this to add enough randomness to the plots, else they'd be fairly smooth
if (typeData == "Forced Effort") {
speciesOrFleets <- fleets
} else if (typeData == "Forced FRate" || typeData == "Forced Catch") {
Expand All @@ -132,14 +132,11 @@ modifyFishingMatrix <- function(modNum,species,fleets,typeData,forcingData,model
for (i in 1:length(speciesOrFleets)) {
item <- speciesOrFleets[i]
vectorData <- ForcedMatrix[,item]
# matrixDataWithJitter <- addJitter(matrixData,modNum*SEED_OFFSET*SEED_VALUE+i,"Months","Effort",paste0(typeData," with Random Noise - ",item))
newVectorWithJitter <- c()
for (value in vectorData) {
j <- j + 1
# Not sure why I need the [1] index here, this should always be just a single value but sometimes it's a list
# Forced Effort: jitteredValue <- (randomNumber(i+j,0.5))[1]
# Forced FRate: pb[species index]*randomNumber(i+j,0.5))[1]
jitteredValue <- (const1+const2*randomNumber(i+j,upperLimit,randomNumberType))[1]
jitteredValue <- (const1+const2*randomNumber(upperLimit,randomNumberType))[1]
newVectorWithJitter = append(newVectorWithJitter,jitteredValue)
}
ForcedMatrix[,item] <- newVectorWithJitter
Expand Down Expand Up @@ -170,21 +167,20 @@ modifyForcingMatrix <- function (modNum,species,modifyType,typeData,forcingData,
speciesBiomass <- scenario$start_state$Biomass[aSpecies]
startValue <- speciesBiomass
if (typeData == FORCED_MIGRATION) {
# rval <- (randomNumber(i,FORCED_MIGRATION_BIOMASS_PCT)+FORCED_MIGRATION_BIOMASS_PCT)/2.0
rval <- randomNumber(i,FORCED_MIGRATION_BIOMASS_PCT,randomNumberType)
# rval <- (randomNumber(FORCED_MIGRATION_BIOMASS_PCT)+FORCED_MIGRATION_BIOMASS_PCT)/2.0
rval <- randomNumber(FORCED_MIGRATION_BIOMASS_PCT,randomNumberType)
startValue <- rval
# print(paste0("species: ",aSpecies,", biomass: ",speciesBiomass,", randomNum: ",rval,", startValue: ",startValue))
}
# print(paste0(modNum," ",i," ",SEED_OFFSET," ",aSpecies))
if (modifyType == JITTERED) {
# print(paste0("start value: ",i,", ",startValue))
ForcedMatrix[,aSpecies] <- createJitterVectorFromValue(typeData, startValue, numMonths, modNum*i*SEED_OFFSET,
"Months","Biomass (mt/km²)",
ForcedMatrix[,aSpecies] <- createJitterVectorFromValue(typeData, startValue, numMonths,
"Months","Biomass (mt/km\U00B2)",
paste0(typeData,' with ',modifyType,' Noise - ',aSpecies),
POSITIVE_AND_NEGATIVE)
} else {
stepType <- ((i-1)%%3)+1 # Only current step types are 1, 2, or 3
ForcedMatrix[,aSpecies] <- stepifyBiomass(typeData, startValue, numMonths, stepType, "Months","Biomass (mt/km²)",
ForcedMatrix[,aSpecies] <- stepifyBiomass(typeData, startValue, numMonths, stepType, "Months","Biomass (mt/km\U00B2)",
paste0(typeData,' with ',modifyType,' Noise - ',aSpecies), scaleFactors[i])
}
}
Expand Down

0 comments on commit 4360095

Please sign in to comment.