Process philosophy

The Topical Tuna Treatment (T3) aim to provide a support for the data manipulation of tropical tuna surface fisheries. This support can take many different forms and have various objectives associated. For that purpose, T3 can be processed in various ways, depending on what question(s) drive your reflection. You will find below several examples for running process and help you regarding your personal work. Furthermore, don’t hesitate to take a look on the package’s issues webpage (https://github.com/OB7-IRD/t3/issues) or send an mail to if you need specific support.

Furthermore, in the following functions the “database_con” argument refers to an R object which contains connection information with an observe (or AVDTH) database. For more detail about connections available check the function documentation. In addition, the argument “output_path” refer to the output directory on your computer. The function associated, “initiate_directory”, will create the output structure for the process.

Running the process step by step

The T3 process consists of three different parts that you can run independently. However, in some cases, you may want to run the entire process step by step, for example to closely track several particular data with accuracy. Below in the code, you will find an example of the entire process being executed step by step. The parameters associated are displayed in the section called “setup”. You can modify them to customize the example. If you need more information regarding functions arguments, take a look at the R function documentation (or in the reference website section https://ob7-ird.github.io/t3/reference/index.html).

# setup ----
library(t3)
years_period <- as.integer(c(2006,
                             2007,
                             2008))
target_year <- as.integer(2008)
period_duration <- as.integer(2)
# countries for the France fleet
flag_codes <- c("FRA",
                "MYT")
# code 4,5 and 6 for Seiner with baits, Seiner without baits and Large seiner 
vessel_type_codes <- as.integer(c(4,5,6))
# code 1 for the Atlantic Ocean
ocean <- as.integer(1)
# code 1 for landing data
sample_type_codes <- as.integer(1)
# for french fleet YFT, SKJ, BET, ALB, MIX and LOT
species_fao_codes_rf1 <- c("YFT", "SKJ", "BET", "ALB","TUN", "LOT")
species_fate_codes_rf1 = as.integer(c(6, 11))
# Species fate codes
# Retained, presumably destined for the cannery : 6,
# Discarded status unknown (only for EMS and logbook) : 11,
species_fate_codes <- as.integer(c(6,11))
# output directories initiation
final_output_path <- initiate_directory(output_path = output_path,
                                        level = "all")
# output format
output_format <- "eu"
# generation of the confidence interval
confidence_interval <- TRUE

# model creation ----
object_model_data <- t3:::object_model_data$new()
# object trips creation
object_model_data$trips_object_creation(data_source = "observe_database",
                                        years_period = years_period,
                                        flag_codes = flag_codes,
                                        ocean_codes = ocean,
                                        vessel_type_codes = vessel_type_codes,
                                        database_connection = observe_database_con)
# object activities and elementarycatches creation
object_model_data$activities_object_creation(data_source = "observe_database",
                                             years_period = years_period,
                                             flag_codes = flag_codes,
                                             ocean_codes = ocean,
                                             vessel_type_codes = vessel_type_codes,
                                             species_fate_codes = species_fate_codes,
                                             database_connection = observe_database_con)
# object elementarylandings creation
object_model_data$elementarylandings_object_creation(data_source = "observe_database",
                                                     years_period = years_period,
                                                     flag_codes = flag_codes,
                                                     ocean_codes = ocean,
                                                     vessel_type_codes = vessel_type_codes,
                                                     database_connection = observe_database_con)
# object wells creation
object_model_data$wells_object_creation(data_source = "observe_database",
                                        years_period = years_period,
                                        flag_codes = flag_codes,
                                        ocean_codes = ocean,
                                        vessel_type_codes = vessel_type_codes,
                                        sample_type_codes = sample_type_codes,
                                        database_connection = observe_database_con)
# model creation: set durations data
object_model_data$setdurationrefs_data(data_source = "csv_file",
                                       data_path = system.file("set_duration_ref.csv",
                                                               package = "t3"))
# model creation: activity code data
object_model_data$activitycoderefs_data(data_source = "csv_file",
                                        data_path = system.file("time_allocation_activity_code_ref.csv",
                                                                package = "furdeb"))
# model creation: length steps data
object_model_data$lengthsteps_data(data_source = "csv_file",
                                   data_path = system.file("length_step.csv",
                                                           package = "t3"))
# model creation: sample sets data
object_model_data$samplesets_data(data_source = "observe_database",
                                  years_period = years_period,
                                  flag_codes = flag_codes,
                                  ocean_codes = ocean,
                                  vessel_type_codes = vessel_type_codes,
                                  database_connection = observe_database_con)
# model creation: length weight relationships data
object_model_data$lengthweightrelationships_data(data_source = "csv_file",
                                                 data_path = system.file("length_weight_relationship.csv",
                                                                         package = "t3"))
# initialisation object for full trips class
object_full_trips <- t3:::full_trips$new()
# object full_trip creation
object_full_trips$create_full_trips(object_trips = object_model_data$.__enclos_env__$private$trips)
# add activities and elementarycatches to trips selected
object_full_trips$add_activities(object_activities = object_model_data$.__enclos_env__$private$activities)
# filter by reference year
object_full_trips$filter_by_years_period(years_period = years_period)
# add elementarylandings to trips selected
object_full_trips$add_elementarylandings(object_elementarylandings = object_model_data$.__enclos_env__$private$elementarylandings)
# add well(s) and sample(s) to trip(s) selected
object_full_trips$add_wells_samples(object_wells = object_model_data$.__enclos_env__$private$wells)

# level 1 ---
# level 1.1: rf1 and rf2
object_full_trips$rf1(species_fao_codes_rf1 = species_fao_codes_rf1,
                      species_fate_codes_rf1 = species_fate_codes_rf1,
                      global_output_path = final_output_path,
                      output_format = output_format)
# level 1.2: logbook weight categories conversion ----
object_full_trips$conversion_weight_category(global_output_path = final_output_path,
                                             output_format = output_format,
                                             referential_template = "observe")
# level 1.3: set count
object_full_trips$set_count(global_output_path = final_output_path,
                            output_format = output_format,
                            referential_template = "observe")
# level 1.4: Fishing effort indicators calculation (set duration, time at sea, fishing and searching time)
object_full_trips$fishing_effort(set_duration_ref = object_model_data$.__enclos_env__$private$setdurationrefs,
                                 activity_code_ref = object_model_data$.__enclos_env__$private$activitycoderefs,
                                 global_output_path = final_output_path,
                                 output_format = output_format,
                                 referential_template = "observe")
# level 2 ----
# level 2.1: sample length class ld1 to lf conversion
object_full_trips$sample_length_class_ld1_to_lf(length_step = object_model_data$.__enclos_env__$private$lengthsteps,
                                                global_output_path = final_output_path,
                                                output_format = output_format,
                                                referential_template = "observe")
# level 2.2: sample number measured extrapolation
object_full_trips$sample_number_measured_extrapolation(global_output_path = final_output_path,
                                                       output_format = output_format)
# level 2.3: sample step length class standardisation
object_full_trips$sample_length_class_step_standardisation(global_output_path = final_output_path,
                                                           output_format = output_format)
# level 2.4: well set weight categories
object_full_trips$well_set_weight_categories(sample_set = object_model_data$.__enclos_env__$private$samplesets,
                                             global_output_path = final_output_path,
                                             output_format = output_format,
                                             referential_template = "observe")
# level 2.5: standardised sample creation
object_full_trips$standardised_sample_creation(global_output_path = final_output_path,
                                               output_format = output_format)
# level 2.6: sample number standardisation
object_full_trips$standardised_sample_set_creation(length_weight_relationship_data = object_model_data$.__enclos_env__$private$lengthweightrelationships,
                                                   global_output_path = final_output_path,
                                                   output_format = output_format)
# level 2.7: raised factors determination
object_full_trips$raised_factors_determination(global_output_path = final_output_path,
                                               output_format = output_format)
# level 2.8: samples number standardisation at set scale
object_full_trips$raised_standardised_sample_set(global_output_path = final_output_path,
                                                 output_format = output_format)

# level 3 ----
process_level3 <- object_full_trips$path_to_level3()
# level 3.1: data preparatory
process_level3 <- object_full_trips$data_preparatory(inputs_level3 = process_level3[[1]],
                                                     output_directory = final_output_path,
                                                     target_year = target_year,
                                                     period_duration = period_duration)
# level 3.2: random forest models ----
process_level3$output_level3_process2 <- object_full_trips$random_forest_models(output_level3_process1 = process_level3$output_level3_process1$data_lb_sample_screened$data4mod)
# level 3.3: models checking ----
process_level3$output_level3_process3 <- object_full_trips$models_checking(output_level3_process2 = process_level3$output_level3_process2,
                                                                           output_directory = process_level3$output_directory)
# level 3.4: data formatting for predictions ----
process_level3$output_level3_process4 <- object_full_trips$data_formatting_for_predictions(inputs_level3 = process_level3$raw_inputs_level3,
                                                                                           output_level3_process1 = process_level3$output_level3_process1$data_lb_sample_screened$data4mod,
                                                                                           target_year = target_year,
                                                                                           country_flag=flag_codes[1],
                                                                                           input_type = "observe_database")
# level 3.5: predictions ----
process_level3$output_level3_process5 <- object_full_trips$model_predictions(output_level3_process2 = process_level3$output_level3_process2,
                                                                             output_level3_process4 = process_level3$output_level3_process4,
                                                                             output_directory = process_level3$output_directory,
                                                                             ci = confidence_interval,
                                                                             country_flag=flag_codes[1])

Shortcut functions

Through the following codes, you will be able to run separately each part of the treatment.

# setup ----
library(t3)
# Reference period 
years_period <- as.integer(c(2006,
                             2007,
                             2008))
target_year <- as.integer(2008)
period_duration <- as.integer(2)
flag_codes <- c("FRA",
                "MYT")
# Seiner with baits, Seiner without baits and Large seiner (code 4,5 and 6)
vessel_type_codes <- as.integer(c(4,5,6))
# code 1 for the Atlantic Ocean
ocean <- as.integer(1)
# code 1 for landing data
sample_type_codes <- as.integer(1)
# For french fleet 
species_fao_codes_rf1 <- c("YFT", "SKJ", "BET", "ALB", "TUN", "LOT")
# Species fate codes
# Retained, presumably destined for the cannery : 6,
# Discarded status unknown (only for EMS and logbook) : 11,
species_fate_codes <- as.integer(c(6,11))
# output directories initiation
final_output_path <- initiate_directory(output_path = output_path,
                                        level = "all")
# output format
output_format <- "eu"
# generation of the confidence interval
confidence_interval <- TRUE

# data model initialization ----
t3_process <- t3::data_model_initialisation(data_source = "observe_database",
                                            database_connection = observe_database_con,
                                            species_fate_codes = species_fate_codes,
                                            years_period = years_period,
                                            flag_codes = flag_codes,
                                            vessel_type_codes = vessel_type_codes,
                                            ocean_codes = ocean,
                                            sample_type_codes = sample_type_codes)
# level 1 ----
t3_process[[2]] <- t3::t3_level1(object_model_data = t3_process[[1]],
                                 object_full_trips = t3_process[[2]],
                                 species_fao_codes_rf1 = species_fao_codes_rf1,
                                 species_fate_codes_rf1 = species_fate_codes,
                                 vessel_type_codes_rf1 = vessel_type_codes,
                                 output_path = final_output_path,
                                 output_format = output_format, 
                                 referential_template= "observe")
# level 2 ----
t3_process[[2]] <- t3::t3_level2(object_model_data = t3_process[[1]],
                                 object_full_trips = t3_process[[2]],
                                 output_path = final_output_path,
                                 output_format = output_format,
                                 referential_template= "observe")
# level 3 ----
t3_process[[3]] <- t3_process[[2]]$path_to_level3()
names(t3_process)[3] <- "process_level3"
t3_process[[3]] <- t3::t3_level3(inputs_level3 = t3_process[[3]][[1]],
                                 target_year = target_year,
                                 period_duration = period_duration,
                                 country_flag = flag_codes[1],
                                 input_type = "observe_database",
                                 ci = confidence_interval,
                                 output_path = final_output_path,
                                 output_format = output_format)

Global shortcut function

The last section presents an example of a function able to run all the t3 process through one command line. Like before, you can “play” with the arguments to customize the function, especially if you want to skip part(s) of the process.

# setup ----
library(t3)
years_period <- as.integer(c(2006,
                             2007,
                             2008))
target_year <- as.integer(2008)
period_duration <- as.integer(2)
# countries for the France fleet
flag_codes  <- c("FRA",
                 "MYT")
# Seiner with baits, Seiner without baits and Large seiner (code 4,5 and 6)
vessel_type_codes <- as.integer(c(4,5,6))
# code 1 for the Atlantic Ocean
ocean <- as.integer(1)
# code 1 for landing data
sample_type_codes <- as.integer(1)
# for french fleet 
species_fao_codes_rf1 <- c("YFT", "SKJ", "BET", "ALB", "TUN", "LOT")
# Species fate codes
# Retained, presumably destined for the cannery : 6,
# Discarded status unknown (only for EMS and logbook) : 11,
species_fate_codes <- as.integer(c(6,11))
species_fate_codes_rf1 <- as.integer(c(6, 11))
# output directories initiation
final_output_path <- initiate_directory(output_path = output_path,
                                        level = "all")
# output format
output_format <- "eu"
# generation of the confidence interval
confidence_interval <- TRUE

# process ----
t3_process_all <- t3::t3_process(process = "all",
                                 data_source = "observe_database",
                                 database_connection = observe_database_con,
                                 output_path = output_path,
                                 output_format = "eu",
                                 years_period = years_period,
                                 flag_codes = flag_codes,
                                 ocean_codes = ocean,
                                 sample_type_codes = sample_type_codes,
                                 species_fate_codes = species_fate_codes,
                                 vessel_type_codes = vessel_type_codes,
                                 species_fao_codes_rf1 = species_fao_codes_rf1,
                                 species_fate_codes_rf1 = species_fate_codes_rf1,
                                 target_year = target_year,
                                 period_duration = period_duration,
                                 ci = confidence_interval)