Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0231645
New code Alfonso
Feb 16, 2022
4c788dc
WGSMART 2022 working code
colinpmillar Dec 2, 2022
2711177
modify code to allow taf.bootstrap() to run
colinpmillar Dec 2, 2022
0eb9d81
data section modifications
colinpmillar Dec 2, 2022
9db9121
put plyr before dplyr
colinpmillar Dec 2, 2022
70bf829
message to possibly clean up ad_long
colinpmillar Dec 2, 2022
39f8493
bug fix, possible change in behaviour of table()
colinpmillar Dec 2, 2022
19f539c
utf8
colinpmillar Dec 2, 2022
7af132f
model.R running
colinpmillar Dec 2, 2022
22c6da3
strata set in web app, not in config file. always 'strata' column
colinpmillar Dec 2, 2022
d71d526
clean up report.R slightly
colinpmillar Dec 2, 2022
c779e98
add setwd code in case its useful
colinpmillar Dec 2, 2022
cb480db
update readme
colinpmillar Dec 2, 2022
c0ce992
Column naming and inter reader bias test
Dec 13, 2022
d0d9c69
delete old data from bootstrap
colinpmillar Dec 13, 2022
a0ab3e2
Update data_processing.R
gonpatricia Dec 16, 2022
95d10d5
Change the data files with experts data to"adv"
gonpatricia Dec 16, 2022
a9be61f
Update of the age error matrix format
gonpatricia Dec 16, 2022
bc9948c
Add library(janitor)
gonpatricia Dec 16, 2022
baf955a
Delete report_full.Rmd
gonpatricia Dec 16, 2022
75537e0
Delete report_summary.Rmd
gonpatricia Dec 16, 2022
34a2a4a
Small correction in the text and tables name (adv)
gonpatricia Dec 16, 2022
5be3113
Small corrections, table names to "adv"
gonpatricia Dec 16, 2022
3c859e4
Update report_summary.Rmd
gonpatricia Dec 16, 2022
36d37de
Update report_full.Rmd
gonpatricia Dec 16, 2022
42fc0fb
Update report.R
gonpatricia Dec 22, 2022
1af544d
keep strata set to "strata"
colinpmillar Jan 6, 2023
d7543d7
add line in worksheet to install dependencies
colinpmillar Jan 6, 2023
2350ef7
fix for server
colinpmillar Jan 16, 2023
f9b67ff
Added a case for inter reader bias table when two readers have no ove…
Feb 13, 2023
57f94c4
Update report_full.Rmd
gonpatricia May 3, 2023
2828779
Update report_full.Rmd
gonpatricia Nov 28, 2023
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# 2018_smartdotsReport_template

This template is used to create reports from smartdots events. The settings for the report are contained in the [config.json](config.json) file. Currently, the code requires direct access to the smartdots database and so must be run by someone with the appropriate access. To run the template:
This template is used to create reports from smartdots events. The
settings for the report are contained in the [config.json](config.json)
file, in the bootstrap/initial/data folder. Currently, the code requires
direct access to the smartdots database and so must be run by someone
with the appropriate access. To run the template:

```r
library(icesTAF)
# download data
taf.bootstrap(taf = TRUE)
# run analysis and create reports
sourceAll()
```

To change the configuration and run the project, the script `worksheet.R`
is provided to set up the config file when changeing eventID for example.
15 changes: 8 additions & 7 deletions bootstrap/initial/data/config.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"event_id": 225,
"event_id": 347,
"ma_method": "Mode",
"strata": "strata",
"onlyApproved": true,
"summary_name": "SmartDots_Summary",
"summary_title": "SmartDots Summary",
"report_name": "SmartDots_Report",
"report_title": "SmartDots Report",
"report_tokens": "token_goes_here"
"summary_name": "SmartDots_Summary_Event_347",
"summary_title": "SmartDots Summary for event 347",
"report_name": "SmartDots_Report_Event_347",
"report_title": "SmartDots Report for event 347",
"report_tokens": "tokens goes here",
"mode_definition": "multistage",
"strata": ["strata"]
}
Binary file modified bootstrap/initial/data/reportTemplate.docx
Binary file not shown.
27 changes: 11 additions & 16 deletions bootstrap/smartdots_db.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,31 @@
library(icesTAF)
library(jsonlite)


# load configuration
config <- read_json("../config.json", simplifyVector = TRUE)
config <- read_json(taf.data.path("config.json"), simplifyVector = TRUE)

# get data from api --------
zipfile <- "smartdots_data.zip"
download.file(
paste0("https://smartdots.ices.dk/download/DownloadEvent.ashx?Token=3F482827-282B-46E0-8C58-FBCF6C4B2C55&tblEventID=", config$event_id),
zipfile,
mode = "wb"
)

url <-
paste0(
"https://smartdots.ices.dk/download/DownloadEvent.ashx?",
"Token=", config$token,
"&tblEventID=", config$event_id
)

download.file(url,
zipfile, mode = "wb")
files <- unzip(zipfile, list = TRUE)$Name
files <- files[grep("*.csv", files)]
unzip(zipfile, files = files, exdir = ".")

# read in and write out again
dist <- read.csv(files[grep("DotsDistances", files)], stringsAsFactors = FALSE)
ad <- read.csv(files[grep("Annotations", files)], stringsAsFactors = FALSE)
data <- read.csv(files[grep("Annotations", files)], stringsAsFactors = FALSE)

# delete downloaded data
unlink(files)
unlink(zipfile)

# drop comments feild
ad <- ad[,names(ad) != "Comment"]
data <- data[, names(data) != "Comment"]

# write out 'bootstrap' data tables
write.taf(dist, "dist.csv", quote = TRUE)
write.taf(ad, "data.csv", quote = TRUE)
write.taf(dist, quote = TRUE)
write.taf(data, quote = TRUE)
1 change: 0 additions & 1 deletion data.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ sourceTAF("data_checker.R")
sourceTAF("data_processing.R")

# done

2 changes: 2 additions & 0 deletions data_checker.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ check_ad <- function(ad, what = "ad") {
"** There are no advanced readers! **\n",
"** the report scripts require there to be advanced readers. **"
)

}


msg(check_text, "\n")
}

Expand Down
67 changes: 47 additions & 20 deletions data_processing.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

library(icesTAF)
library(jsonlite)
library(plyr)
library(dplyr)
library(tidyr)
require(lubridate)

# create data directory
mkdir("data")
library(lubridate)
library(stringr)

# get utility functions
source("utilities.R")
Expand Down Expand Up @@ -42,6 +41,7 @@ ad <-
month = lubridate::month(parse_date_time(catch_date, '%d/%m/%Y %H:%M:%S'))
})


# if variables are missing add "missing"
ad$ices_area[is.na(ad$ices_area) | ad$ices_area == ""] <- "missing"
ad$stock[is.na(ad$stock) | ad$stock == ""] <- "missing"
Expand All @@ -50,6 +50,24 @@ ad$prep_method[is.na(ad$prep_method) | ad$prep_method == ""] <- "missing"
# if variables are missing add "missing"
dist$ices_area[is.na(dist$ices_area) | dist$ices_area == ""] <- "missing"

# check or create the stratification here
if (is.null(config$strata) || is.na(config$strata)) {
ad$strata = NA
} else {
ad$strata = apply(as.data.frame(ad[, config$strata]), 1, function(x) {
paste(str_to_title(config$strata), x, sep = "_", collapse = "_and_")
})
}



estratos <- plyr::ddply(ad, .(SampleID), summarise, strata = unique(strata))
dim(dist)
dist <- dist[,!colnames(dist) %in% c("strata")]
dist <- merge(dist, estratos, by.x="SampleID", by.y="SampleID")
dim(dist)


# if no advanced readers! make them all advanced
if (all(ad$expertise == 0)) {
msg("NOTE: all readers were Basic - all have been converted to Advanced")
Expand All @@ -59,18 +77,28 @@ if (all(ad$expertise == 0)) {
# convert reader expertise
ad$expertise <- c("Basic", "Advanced")[ad$expertise + 1]

# if strata is missing, add "all"
# has strata been set?
if (all(is.na(ad$strata))) {
msg("NOTE: strata column not assigned - setting strata to 'all'")
ad$strata <- "all"
dist$strata <- "all"
}
# Assign weight to the readers based in their ranking-experience
weight <- length(sort(unique(ad$reader_number))):1
reader_number <- sort(unique(ad$reader_number))
reader <- data.frame(reader_number = reader_number, weight_I = weight, weight_II = 1 / (1 + log(sort(weight, decreasing = FALSE) + 0.0000000001)))
ad <- merge(ad, reader, by.x = "reader_number", by.y = "reader_number", all.x = TRUE)


# Calculate modal ages and cv of modal age
ad_long <- add_modalage(ad, config$ma_method)
ad_long_ex <- add_modalage(ad[ad$expertise == "Advanced", ], config$ma_method)
#message("perhaps change modal calculation functions to better name columns 0, 1, 2 ...")
ad_long <- ad %>%
add_modal_trad(config$ma_method) %>%
add_modal_linearweight(config$ma_method) %>%
add_modal_negexpweight(config$ma_method)

ad_long_adv <- ad[ad$expertise == "Advanced", ] %>%
add_modal_trad(config$ma_method) %>%
add_modal_linearweight(config$ma_method) %>%
add_modal_negexpweight(config$ma_method)

# Choose the final mode (traditional, readers linear weight or negative exponential linear weight) based in the existence of multimodality or not.
ad_long <- select_mode(ad_long, config$ma_method, config$mode_definition)
ad_long_adv <- select_mode(ad_long_adv, config$ma_method, config$mode_definition)

# prepare data in wbgr output format
# IMAGE,1,2,3,4,5,6,7,8,9,10,11,12,13
Expand All @@ -79,7 +107,7 @@ ad_long_ex <- add_modalage(ad[ad$expertise == "Advanced", ], config$ma_method)
# 6698256.jpg,1,1,1,1,1,-,2,1,-,2,-,1,-
# 6698257.jpg,3,3,3,3,2,1,3,3,-,3,-,3,-
readers <- sort(unique(ad$reader))
webgr <- spread(ad_long[c("sample", "reader", "age")], key = reader, value = age)
webgr <- spread(ad[c("sample", "reader", "age")], key = reader, value = age)
webgr[] <- paste(unlist(webgr))
webgr[webgr == "NA"] <- "-"
webgr <-
Expand All @@ -89,10 +117,9 @@ webgr <-
names(webgr) <- c("IMAGE", 1:length(readers))
head(webgr)


# write out input data tables for use later
write.taf(dist, "data/dist.csv", quote = TRUE)
write.taf(ad, "data/data.csv", quote = TRUE)
write.taf(ad_long, "data/ad_long.csv", quote = TRUE)
write.taf(ad_long_ex, "data/ad_long_ex.csv", quote = TRUE)
write.taf(webgr, "data/WebGR_ages_all.csv", quote = TRUE)
write.taf(dist, dir = "data", quote = TRUE)
write.taf(ad, file = "data.csv", dir = "data", quote = TRUE)
write.taf(ad_long, dir = "data", quote = TRUE)
write.taf(ad_long_adv, dir = "data", quote = TRUE)
write.taf(webgr, file = "WebGR_ages_all.csv", dir = "data", quote = TRUE)
Loading