Open-Systems-Pharmacology / OSPSuite.RUtils

Utility functions for Open Systems Pharmacology R packages
https://www.open-systems-pharmacology.org/OSPSuite.RUtils/dev/
GNU General Public License v2.0
1 stars 3 forks source link

Implement `logSafe` #126

Closed PavelBal closed 1 year ago

PavelBal commented 1 year ago

From ospsuite-r, as this function is also used by the ParameterIdentification and should be also used by reporting engine?

# TODO:
#
# Depending on what is decided in issue
# https://github.com/Open-Systems-Pharmacology/OSPSuite-R/issues/1091, change
# defaults for `base` for `.log_safe`.

#' @keywords internal
#' @noRd
.log_safe <- function(x, base = 10, epsilon = ospsuiteEnv$LOG_SAFE_EPSILON) {
  x <- sapply(X = x, function(element) {
    element <- ospsuite.utils::toMissingOfType(element, type = "double")
    if (is.na(element)) {
      return(NA_real_)
    } else if (element < epsilon) {
      return(log(epsilon, base = base))
    } else {
      return(log(element, base = base))
    }
  })

  return(x)
}