Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion R/bk_lookupfun.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Generate Chinook bk_fram look-up table
#'
#' At present the FRAM databases use a differents stock_id numbering system in the BackwardsFRAM
Expand Down
78 changes: 78 additions & 0 deletions data-raw/filter_id_lists.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
## pulled from framrsquared
## To avoid autocomplete ambiguity, not using more natural `filter_chinook_fishery` etc. pattern.
##
## Concerns to check:
## are all fisheries either net or sport? Currently assumes so
## overlap in filter_STATE
##
## Update: have been walking through and trying separate out state filter criterion
##
## Notes:
## KMZ and So. Calif. fisheries are JUST california
## Columbia river fisheries are both WA and OR
##

## Chinook

filterlist_chinook_fishery <-
list(
filter_sport = c(
3, 8, 11, 13, 14, 15,
18, 22, 27, 29, 31, 33,
35, 48, 60, 62, 72, 36,
42, 45, 53, 54, 56, 57,
64, 67
),
filter_net = c(2:7, 19, 23:25, 28, 37:40),
filter_puget_sound = 36:71,
filter_wa = c(16:29, 36:73),
filter_bc = 4:15,
filter_ak = 1:3,
filter_ca = 32:34,
filter_or = 28:33,
filter_coast = 1:35,
filter_commercial_wa_nt = c(58, 65, 68, 70, 37, 39, 43, 46, 49, 51)
)
## filter_marine is defined as not fishery_id 72 or 73 for chinook
filterlist_chinook_fishery$filter_net <- setdiff(framrosetta::fishery_chinook_fram$fishery_id,
72:73)

usethis::use_data(fishery_coho_psc, overwrite = TRUE)

## Coho

filterlist_coho_fishery <-
list(
filter_sport = c(
3, 5, 7, 15, 17, 19, 21, 23, 24,
28, 29, 31, 33, 37, 40, 41, 45, 46,
48, 49, 51, 54, 58, 59, 60, 61, 62,
65, 66, 67, 70, 73, 76, 89, 90, 91,
92, 93, 94, 95, 99, 100, 106, 107,
108, 115, 116, 117, 118, 127, 129,
135, 136, 149, 150, 151, 152, 163,
164, 165, 166, 169, 186, 187, 188,
189, 190, 191, 192, 193
),
filter_puget_sound = 76:166,
filter_wa = 23:166,
filter_bc = 167:193,
filter_ak = 194:198,
filter_ca = 1:8,
filter_or = 10:32,
filter_coast = c(1:22, 33:75),
filter_marine = c(
3:8, 15:22, 33:50,
79:83, 87:88, 91:93,
96:97, 101:102, 105:107,
109:112, 115, 118:124,
129:133, 136:146, 152:160,
170:198
),
filter_commercial_wa_nt = c(82, 87, 96, 101, 109, 111, 119, 121, 123, 130, 132, 137, 139, 141, 143, 145, 153, 155, 157, 159)
)
filterlist_coho_fishery$filter_net <- setdiff(framrosetta::fishery_coho_fram$fishery_id,
filterlist_coho_fishery$filter_sport)



11 changes: 10 additions & 1 deletion data-raw/table_extractor.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ fishery_chinook_fram <- fetch_table(db, "Fishery") |>
comment(fishery_chinook_fram) <- c(description = paste0("`fishery_chinook_fram` is a copy of the Fishery table from a Chinook FRAM database (", file.chin, "), and can be used to map fishery ID numbers to fisheries or vice versa. Note that TAMM and FRAM have slightly different fishery ID numbers."))
timestep_chinook_fram <- fetch_table(db, "TimeStep") |>
filter(species == "CHINOOK")

## fixing timestep problems
timestep_chinook_fram <- timestep_chinook_fram |>
mutate(across(c(time_step_name, time_step_title), ~gsub("-2", "2", .x))) |>
mutate(time_step_title = gsub("([^ ])-", "\\1 -", time_step_title)) |>
mutate(time_step_title = gsub("-([^ ])", "- \\1", time_step_title))



comment(timestep_chinook_fram) <- c(description = paste0("`timestep_chinook_fram` is a copy of the TimeStep table from a Chinook FRAM database (", file.chin, ")."))
disconnect_fram_db(db)

Expand Down Expand Up @@ -77,7 +86,7 @@ limiting_stock_coho <- tibble(
"Snohomish wild",
"Hood Canal wild",
"Juan de Fuca wild",
"Quileyte Fall wild",
"Quileute Fall wild",
"Hoh wild",
"Queets wild",
"Grays Harbor wild",
Expand Down
Binary file modified data/fishery_chinook_fram.rda
Binary file not shown.
Binary file modified data/fishery_coho_fram.rda
Binary file not shown.
Binary file modified data/limiting_stock_coho.rda
Binary file not shown.
Binary file modified data/stock_chinook_fram.rda
Binary file not shown.
Binary file modified data/stock_coho_fram.rda
Binary file not shown.
Binary file modified data/timestep_chinook_fram.rda
Binary file not shown.
Binary file modified data/timestep_coho_fram.rda
Binary file not shown.
80 changes: 80 additions & 0 deletions tests/testthat/test-add_columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,83 @@ test_that("label_fisheries gives correct answers", {
expect_equal(label_stocks(.data = stock_chinook_fram)$stock_label, stock_chinook_fram$stock_long_name)
expect_equal(label_stocks(.data = stock_coho_fram)$stock_label, stock_coho_fram$stock_long_name)
})


# ## test written with claude
# test_that("label_fisheries works correctly, written by Claude", {
# # Test data
# test_data <- data.frame(fishery_id = c(1, 2, 3, NA))
#
# # Mock framrosetta data
# mock_chinook = data.frame(
# fishery_id = 1:5,
# fishery_title = paste("Fishery", 1:5)
# )
# mock_coho = data.frame(
# fishery_id = 1:3,
# fishery_title = paste("Coho Fishery", 1:3)
# )
#
# with_mocked_bindings(
# code = {
# # Test basic functionality
# result <- label_fisheries(test_data, species = "CHINOOK")
# expect_true("fishery_label" %in% names(result))
# expect_equal(result$fishery_label[1], "Fishery 1")
# expect_true(is.na(result$fishery_label[4]))
#
# # Test with species attribute
# attr(test_data, "species") <- "COHO"
# result2 <- label_fisheries(test_data)
# expect_equal(result2$fishery_label[1], "Coho Fishery 1")
#
# # Test custom column name
# test_data2 <- data.frame(custom_fishery = c(1, 2))
# result3 <- label_fisheries(test_data2, species = "CHINOOK", fisheries_col = "custom_fishery")
# expect_true("fishery_label" %in% names(result3))
#
# # Test errors
# expect_error(label_fisheries(data.frame(x = 1), species = "CHINOOK"))
# expect_error(label_fisheries(test_data))
# expect_error(label_fisheries(data.frame(fishery_id = "a"), species = "CHINOOK"))
# expect_error(label_fisheries(data.frame(fishery_id = 999), species = "CHINOOK"))
# },
# fishery_chinook_fram = mock_chinook,
# fishery_coho_fram = mock_coho,
# .package = "framrosetta"
# )
# })
# The error occurs because `with_mocked_bindings()` creates temporary bindings that only exist within the `code` block, but your original tests outside the mocked block are still trying to access `fishery_chinook_fram` and `fishery_coho_fram` which don't exist in the global environment.

# Move your original tests inside the `with_mocked_bindings()` block:
# r
# test_that("label_fisheries works correctly v2, claude", {
# # Mock data
# mock_chinook <- data.frame(
# fishery_id = 1:5,
# fishery_title = paste("Fishery", 1:5)
# )
# mock_coho <- data.frame(
# fishery_id = 1:3,
# fishery_title = paste("Coho Fishery", 1:3)
# )
#
# with_mocked_bindings(
# code = {
# # Your original validation tests
# expect_error(label_fisheries(.data = 1:5, species = "coho"))
# data <- data.frame(x = 1:10, y = 501:510, z = letters[1:10], alpha = c(1:9, NA))
# expect_error(label_fisheries(data))
# expect_error(label_fisheries(data, species = "COHO"))
# expect_error(label_fisheries(data, species = "COHO", fisheries_col = "z"))
# expect_error(label_fisheries(data, species = "COHO", fisheries_col = "y"))
# expect_no_error(label_fisheries(data, species = "COHO", fisheries_col = "alpha"))
#
# # Your original correctness tests
# expect_equal(label_fisheries(.data = fishery_chinook_fram)$fishery_label, fishery_chinook_fram$fishery_title)
# expect_equal(label_fisheries(.data = fishery_coho_fram)$fishery_label, fishery_coho_fram$fishery_title)
# },
# fishery_chinook_fram = mock_chinook,
# fishery_coho_fram = mock_coho
# )
# })