GSgalgoR Callbacks Mechanism (original) (raw)
Contents
- 1 Introduction
- 2 Example 1: A simple custom callback function definition
- 3 Example 2: Saving partial population pool using custom callback function
- 4 Callbacks implemented in GSgalgoR
- 5 Session info
Introduction
The GSgalgoR package provides a practical but straightforward callback mechanism for adapting different galgo()
execution sections to final user needs. The GSgalgoR callbacks mechanism enables adding custom functions to change thegalgo()
function behavior by including minor modification to galgo’s workflow. A common application of the callback mechanism is to implement personalized reports, saving partial information during the evolution process or compute the execution time.
There are five possible points where the user can hook its own code insidegalgo()
execution process.
- At the beginning of the
galgo()
execution process. (i.e. whengalgo()
is about to start.) - At the end of the
galgo()
execution process. (i.e. whengalgo()
is about to finish. ) - At the beginning of the evolution process. (i.e. at the beginning of each generation/iteration. )
- At the end of the evolution process. (i.e. at the end of each generation/iteration.)
- In the middle of the evolution process. (i.e. in the middle of the generation, right after the new mating pool have been created.)
Each one of the five possible hooks can be accessed through parameters with the *_callback* suffix in the galgo()
function.
galgo(...,
start_galgo_callback = callback_default,# `galgo()` is about to start.
end_galgo_callback = callback_default, # `galgo()` is about to finish.
start_gen_callback = callback_default, # At the beginning of each generation
end_gen_callback = callback_default, # At the end of each generation
report_callback = callback_default, # In the middle of the generation,
# right after the new mating pool
# have been created.
...)
Example 1: A simple custom callback function definition
A callback function definition can be any R function accepting six parameters.
-userdir
: the directory (“character”) where the user can save information into local filesystem. -generation
: the number (“integer”) of the current generation/iteration. -pop_pool
: the data.frame containing the resulting solutions for current iteration. -pareto
: the solutions found by galgo()
accross all generations in the solution space -prob_matrix
: the expression set (“matrix) where features are rows and samples distributed in columns. -current_time
: The current time (an object of class”POSIXct").
The following callback function example prints the generation number and current time every two iterations
library(GSgalgoR)
my_callback <-
function(userdir = "",
generation,
pop_pool,
pareto,
prob_matrix,
current_time) {
# code starts here
if (generation%%2 == 0)
message(paste0("generation: ",generation,
" current_time: ",current_time))
}
then, the my_callback()
function needs to be assigned to some of the available hooks provided by the galgo()
. An example of such assignment and the resulting output is provided in the two snippets below.
A reduced version of theTRANSBIGdataset is used to setup the expression and clinical information required for the galgo()
function.
library(breastCancerTRANSBIG)
data(transbig)
train <- transbig
rm(transbig)
expression <- Biobase::exprs(train)
clinical <- Biobase::pData(train)
OS <- survival::Surv(time = clinical$t.rfs, event = clinical$e.rfs)
# use a reduced dataset for the example
expression <- expression[sample(1:nrow(expression), 100), ]
# scale the expression matrix
expression <- t(scale(t(expression)))
Then, the galgo()
function is invoked and the recently defined functionmy_callback()
is assigned to the report_callback
hook-point.
library(GSgalgoR)
# Running galgo
GSgalgoR::galgo(generations = 6,
population = 15,
prob_matrix = expression,
OS = OS,
start_galgo_callback = GSgalgoR::callback_default,
end_galgo_callback = GSgalgoR::callback_default,
report_callback = my_callback, # call `my_callback()` in the mile
# of each generation/iteration.
start_gen_callback = GSgalgoR::callback_default,
end_gen_callback = GSgalgoR::callback_default)
#> Using CPU for computing pearson distance
#> generation: 2 current_time: 2025-04-15 18:43:11.412438
#> generation: 4 current_time: 2025-04-15 18:43:12.848502
#> generation: 6 current_time: 2025-04-15 18:43:14.090269
#> NULL
Example 2: Saving partial population pool using custom callback function
The following callback function save in a temporary directory the solutions obtained every five generation/iteration. A file the number of the generation and with a rda.
extension will be left in a directory defined by the tempdir()
function.
my_save_pop_callback <-
function(userdir = "",
generation,
pop_pool,
pareto,
prob_matrix,
current_time) {
directory <- paste0(tempdir(), "/")
if (!dir.exists(directory)) {
dir.create(directory, recursive = TRUE)
}
filename <- paste0(directory, generation, ".rda")
if (generation%%2 == 0){
save(file = filename, pop_pool)
}
message(paste("solution file saved in",filename))
}
As usual, the galgo()
function is invoked and the recently defined function my_save_pop_callback()
is assigned to the end_gen_callback
hook-point. As a result, every five generation/iteration the complete solution obtained by galgo will be saved in a file.
# Running galgo
GSgalgoR::galgo(
generations = 6,
population = 15,
prob_matrix = expression,
OS = OS,
start_galgo_callback = GSgalgoR::callback_default,
end_galgo_callback = GSgalgoR::callback_default,
report_callback = my_callback,# call `my_callback()`
# in the middle of each generation/iteration.
start_gen_callback = GSgalgoR::callback_default,
end_gen_callback = my_save_pop_callback # call `my_save_pop_callback()`
# at the end of each
# generation/iteration
)
#> Using CPU for computing pearson distance
#> solution file saved in /tmp/RtmpzKWp4x/1.rda
#> generation: 2 current_time: 2025-04-15 18:43:20.235736
#> solution file saved in /tmp/RtmpzKWp4x/2.rda
#> solution file saved in /tmp/RtmpzKWp4x/3.rda
#> generation: 4 current_time: 2025-04-15 18:43:21.521303
#> solution file saved in /tmp/RtmpzKWp4x/4.rda
#> solution file saved in /tmp/RtmpzKWp4x/5.rda
#> generation: 6 current_time: 2025-04-15 18:43:22.89971
#> solution file saved in /tmp/RtmpzKWp4x/6.rda
#> NULL
Callbacks implemented in GSgalgoR
By default, GSfalgoR implements four callback functions
callback_default()
a simple callback that does nothing at all. It is just used for setting the default behavior of some of the hook-points inside galgo()
callback_base_report()
a report callback for printing basic information about the solution provided by galgo()
such as fitness and crowding distance.callback_no_report()
a report callback for informing the user galgo is running. Not valuable information is shown.callback_base_return_pop()
a callback function for building and returning t he galgo.Obj
object.
In the the default definition of the galgo()
function the hook-points are defined as follow:
-start_galgo_callback = callback_default
-end_galgo_callback = callback_base_return_pop
-report_callback = callback_base_report
-start_gen_callback = callback_default
-end_gen_callback = callback_default
Notice by using the callback mechanism it is possible to modify even the returning value of the galgo()
function. The defaultcallback_base_return_pop()
returns a galgo.Obj
object, however it would simple to change that behavior for something like the my_save_pop_callback()
and the function will not returning any value.
# Running galgo
GSgalgoR::galgo(
generations = 6,
population = 15,
prob_matrix = expression,
OS = OS,
start_galgo_callback = GSgalgoR::callback_default,
end_galgo_callback = my_save_pop_callback,
report_callback = my_callback, # call `my_callback()`
# in the middle of each generation/iteration
start_gen_callback = GSgalgoR::callback_default,
end_gen_callback = GSgalgoR::callback_default
)
#> Using CPU for computing pearson distance
#> generation: 2 current_time: 2025-04-15 18:43:29.667906
#> generation: 4 current_time: 2025-04-15 18:43:31.080897
#> generation: 6 current_time: 2025-04-15 18:43:32.764776
#> solution file saved in /tmp/RtmpzKWp4x/6.rda
For preserving the return behavior of the galgo()
function,callback_base_return_pop()
should be called inside a custom callback. An example of such situation is shown below:
another_callback <-
function(userdir = "",
generation,
pop_pool,
pareto,
prob_matrix,
current_time) {
# code starts here
# code ends here
callback_base_return_pop(userdir,
generation,
pop_pool,
prob_matrix,
current_time)
}
Session info
sessionInfo()
#> R version 4.5.0 RC (2025-04-04 r88126)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.2 LTS
#>
#> Matrix products: default
#> BLAS: /home/biocbuild/bbs-3.21-bioc/R/lib/libRblas.so
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0 LAPACK version 3.12.0
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_GB LC_COLLATE=C
#> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: America/New_York
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] survminer_0.5.0 ggpubr_0.6.0
#> [3] ggplot2_3.5.2 genefu_2.40.0
#> [5] AIMS_1.40.0 e1071_1.7-16
#> [7] iC10_2.0.2 biomaRt_2.64.0
#> [9] survcomp_1.58.0 prodlim_2024.06.25
#> [11] survival_3.8-3 Biobase_2.68.0
#> [13] BiocGenerics_0.54.0 generics_0.1.3
#> [15] GSgalgoR_1.18.0 breastCancerUPP_1.45.0
#> [17] breastCancerTRANSBIG_1.45.0 BiocStyle_2.36.0
#>
#> loaded via a namespace (and not attached):
#> [1] jsonlite_2.0.0 magrittr_2.0.3 magick_2.8.6
#> [4] SuppDists_1.1-9.9 farver_2.1.2 rmarkdown_2.29
#> [7] vctrs_0.6.5 memoise_2.0.1 tinytex_0.57
#> [10] rstatix_0.7.2 htmltools_0.5.8.1 progress_1.2.3
#> [13] curl_6.2.2 broom_1.0.8 Formula_1.2-5
#> [16] sass_0.4.10 parallelly_1.43.0 KernSmooth_2.23-26
#> [19] bslib_0.9.0 httr2_1.1.2 impute_1.82.0
#> [22] zoo_1.8-14 cachem_1.1.0 commonmark_1.9.5
#> [25] iterators_1.0.14 lifecycle_1.0.4 pkgconfig_2.0.3
#> [28] Matrix_1.7-3 R6_2.6.1 fastmap_1.2.0
#> [31] GenomeInfoDbData_1.2.14 future_1.40.0 digest_0.6.37
#> [34] colorspace_2.1-1 AnnotationDbi_1.70.0 S4Vectors_0.46.0
#> [37] nsga2R_1.1 RSQLite_2.3.9 labeling_0.4.3
#> [40] filelock_1.0.3 km.ci_0.5-6 httr_1.4.7
#> [43] abind_1.4-8 compiler_4.5.0 proxy_0.4-27
#> [46] bit64_4.6.0-1 withr_3.0.2 doParallel_1.0.17
#> [49] backports_1.5.0 carData_3.0-5 DBI_1.2.3
#> [52] ggsignif_0.6.4 lava_1.8.1 rappdirs_0.3.3
#> [55] tools_4.5.0 iC10TrainingData_2.0.1 future.apply_1.11.3
#> [58] bootstrap_2019.6 glue_1.8.0 gridtext_0.1.5
#> [61] grid_4.5.0 cluster_2.1.8.1 gtable_0.3.6
#> [64] KMsurv_0.1-5 class_7.3-23 tidyr_1.3.1
#> [67] data.table_1.17.0 hms_1.1.3 xml2_1.3.8
#> [70] car_3.1-3 XVector_0.48.0 markdown_2.0
#> [73] foreach_1.5.2 pillar_1.10.2 stringr_1.5.1
#> [76] limma_3.64.0 splines_4.5.0 ggtext_0.1.2
#> [79] dplyr_1.1.4 BiocFileCache_2.16.0 lattice_0.22-7
#> [82] bit_4.6.0 tidyselect_1.2.1 Biostrings_2.76.0
#> [85] knitr_1.50 gridExtra_2.3 litedown_0.7
#> [88] bookdown_0.43 IRanges_2.42.0 pamr_1.57
#> [91] stats4_4.5.0 xfun_0.52 statmod_1.5.0
#> [94] stringi_1.8.7 UCSC.utils_1.4.0 yaml_2.3.10
#> [97] evaluate_1.0.3 codetools_0.2-20 tibble_3.2.1
#> [100] BiocManager_1.30.25 cli_3.6.4 survivalROC_1.0.3.1
#> [103] xtable_1.8-4 munsell_0.5.1 jquerylib_0.1.4
#> [106] survMisc_0.5.6 Rcpp_1.0.14 GenomeInfoDb_1.44.0
#> [109] rmeta_3.0 globals_0.16.3 dbplyr_2.5.0
#> [112] png_0.1-8 parallel_4.5.0 mco_1.17
#> [115] blob_1.2.4 prettyunits_1.2.0 mclust_6.1.1
#> [118] listenv_0.9.1 scales_1.3.0 purrr_1.0.4
#> [121] crayon_1.5.3 rlang_1.1.6 KEGGREST_1.48.0