Skip to content

Commit e1efb4d

Browse files
committed
add some warm and fuzzies
1 parent 0721b6f commit e1efb4d

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: SimDesign
22
Title: Structure for Organizing Monte Carlo Simulation Designs
3-
Version: 2.6.4
3+
Version: 2.6.5
44
Authors@R: c(person("Phil", "Chalmers", email = "[email protected]", role = c("aut", "cre"),
55
comment = c(ORCID="0000-0001-5332-2810")),
66
person("Matthew", "Sigal", role = c("ctb")),

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## Changes in SimDesign 2.7
44

5+
- `SimExtract()` gains a `fuzzy` argument to allow fuzzy matching of error and warning messages.
6+
This helps collapse very similar errors messages in the recorded tables,
7+
thereby improving how to discern any pattern in the errors/warnings (e.g., Messages such as
8+
*"ERROR: system is computationally singular: reciprocal condition number = 9.63735e-18"* and
9+
*"ERROR: system is computationally singular: reciprocal condition number = 6.74615e-17"* are
10+
effectively the same, and so their number of recorded occurrences should be collapsed)
11+
512
- Added `AnalyseIf()` function to allow specific analysis function to be included explicitly. Useful
613
when the defined analysis function is not compatible with a row-condition in the `Design` object.
714
Only relevant when the `analyse` argument was defined as a named list of functions

R/SimExtract.R

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
#' Note that \code{'warning_seeds'} are not stored automatically in
1919
#' simulations and require passing \code{store_warning_seeds = TRUE} to \code{\link{runSimulation}}.
2020
#'
21+
#' @param fuzzy logical; use fuzzy string matching to reduce effectively identical messages?
22+
#' For example, when attempting to invert a matrix the error message
23+
#' \emph{"System is computationally singular: reciprocal condition number = 1.92747e-17"} and
24+
#' \emph{"System is computationally singular: reciprocal condition number = 2.15321e-16"} are
25+
#' effectively the same, and likely should be reported in the same columns of the extracted output
26+
#'
2127
#' @export
2228
#'
2329
#' @references
@@ -70,7 +76,7 @@
7076
#'
7177
#'
7278
#' }
73-
SimExtract <- function(object, what){
79+
SimExtract <- function(object, what, fuzzy = TRUE){
7480
stopifnot(is(object, "SimDesign"))
7581
what <- tolower(what)
7682
pick <- attr(object, 'design_names')$design
@@ -82,25 +88,31 @@ SimExtract <- function(object, what){
8288
ret <- if(what == 'results'){
8389
extract_results(object)
8490
} else if(what == 'errors'){
85-
cbind(Design, extract_errors(object))
91+
cbind(Design, extract_errors(object, fuzzy=fuzzy))
8692
} else if(what == 'summarise'){
8793
extract_summarise(object)
8894
} else if(what == 'error_seeds'){
8995
extract_error_seeds(object)
9096
} else if(what == 'warnings'){
91-
cbind(Design, extract_warnings(object))
97+
cbind(Design, extract_warnings(object, fuzzy=fuzzy))
9298
} else if(what == 'warning_seeds'){
9399
extract_warning_seeds(object)
94100
} else stop('Input provided to \"what" is not supported')
95101
ret
96102
}
97103

98-
extract_errors <- function(object){
99-
attr(object, 'ERROR_msg')
104+
extract_errors <- function(object, fuzzy){
105+
ret <- attr(object, 'ERROR_msg')
106+
if(fuzzy)
107+
ret <- fuzzy_reduce(ret)
108+
ret
100109
}
101110

102-
extract_warnings <- function(object){
103-
attr(object, 'WARNING_msg')
111+
extract_warnings <- function(object, fuzzy){
112+
ret <- attr(object, 'WARNING_msg')
113+
if(fuzzy)
114+
ret <- fuzzy_reduce(ret)
115+
ret
104116
}
105117

106118
extract_results <- function(object){
@@ -142,3 +154,19 @@ extract_summarise <- function(object){
142154
names(ret) <- nms
143155
ret
144156
}
157+
158+
fuzzy_reduce <- function(df){
159+
nms <- colnames(df)
160+
matched <- logical(length(nms))
161+
unames <- c()
162+
udf <- df[,0]
163+
for(i in 1L:length(nms)){
164+
if(matched[i]) next
165+
unames <- c(unames, nms[i])
166+
udf <- cbind(udf, df[,i])
167+
temp_matched <- agrepl(nms[i], nms)
168+
udf[,ncol(udf)] <- rowSums(df[,temp_matched], na.rm = TRUE)
169+
matched <- matched | temp_matched
170+
}
171+
udf
172+
}

man/SimExtract.Rd

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tests/test-SimDesign.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,5 +644,28 @@ test_that('SimDesign', {
644644
expect_true(all(c("analyse1.a1", "analyse2.a2") %in% names(res)))
645645
expect_true(is.na(res$analyse1.a1[2]))
646646

647+
# fuzzy strings
648+
Analyse <- function(condition, dat, fixed_objects = NULL) {
649+
C <- matrix(c(1,.2, 0, 1), 2)
650+
if(sample(c(TRUE, FALSE), 1))
651+
C[2,2] <- runif(1, -1e-8, 1e-8)
652+
ret <- det(solve(C %*% t(C)))
653+
ret
654+
}
655+
656+
Summarise <- function(condition, results, fixed_objects = NULL) {
657+
ret <- c(bias = NaN, RMSE = NaN)
658+
ret
659+
}
660+
661+
#-------------------------------------------------------------------
662+
663+
res <- runSimulation(replications=200, analyse=Analyse, summarise=Summarise,
664+
verbose=FALSE, seed = 1234)
665+
out <- SimExtract(res, what = 'errors')
666+
expect_true(ncol(out) == 2L)
667+
out <- SimExtract(res, what = 'errors', fuzzy = FALSE)
668+
expect_true(ncol(out) > 2L)
669+
647670
})
648671

0 commit comments

Comments
 (0)