Skip to content

Commit

Permalink
Fix read_intermediate_results_from_log()
Browse files Browse the repository at this point in the history
- make "pattern_remove" an argument
- filter actively for code or assignment rows
- convert string variable (usage) to numeric
  • Loading branch information
hsonne committed Oct 28, 2023
1 parent 0886870 commit 545109b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
31 changes: 24 additions & 7 deletions R/read_intermediate_results_from_log.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
#' Read Intermediate Results from Log File
#'
#' @param file path to log file
#' @param pattern_remove regular expression matching lines to remove from the
#' log file before looking for "variable=expression" assignments
read_intermediate_results_from_log <- function(
file = file.path(tempdir(), "abimo_input_result.log")
file = file.path(tempdir(), "abimo_input_result.log"),
pattern_remove = "Start|unknown|angenommen|nicht definiert|std::"
)
{
#file <- file.path(tempdir(), "abimo_input_result.log")
raw_text <- readLines(file)

# Remove informative lines or empty lines
# e.g. "Nutzungstyp nicht definiert fuer Element"
pattern_remove <- "Start|unknown|unbekannt|Nutzungstyp|angenommen|^$"
# Remove irrelevant lines
text <- grep(pattern_remove, raw_text, value = TRUE, invert = TRUE)

# Pattern matching lines with code
pattern_code <- "^\\*\\*\\* Code: "

text <- grep(pattern_remove, raw_text, value = TRUE, invert = TRUE)
# Keep only lines with code or with equal sign (variable=value)
text <- grep(paste0(pattern_code, "|="), text, value = TRUE)

if (length(text) == 0L) {
message("No intermediates found in log file. Returning NULL.")
Expand All @@ -31,7 +37,9 @@ read_intermediate_results_from_log <- function(
n_matching <- function(x, pattern) length(grep(pattern, x))

# In each block all but the first line must contain the equal sign
stopifnot(all(lengths(textblocks) == sapply(textblocks, n_matching, "=")))
stopifnot(all(
lengths(textblocks) - 1L == sapply(textblocks, n_matching, "=")
))

# Read the codes from the first lines of the text blocks
code_lines <- sapply(textblocks, "[", 1L)
Expand All @@ -49,7 +57,16 @@ read_intermediate_results_from_log <- function(
)
})

result <- kwb.utils::rbindAll(matrices, nameColumn = "code")
result <- kwb.utils::rbindAll(
x = matrices,
nameColumn = "code",
namesAsFactor = FALSE
)

# Convert usage string to numeric (number of letter in alphabet)
is_usage <- result$variable == "ut.usage"
result$value[is_usage] <- match(result$value[is_usage], LETTERS)

# Let "code" be the first column
kwb.utils::moveColumnsToFront(result, "code")
}
8 changes: 6 additions & 2 deletions man/read_intermediate_results_from_log.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 545109b

Please sign in to comment.