Skip to content

Commit

Permalink
Release 1.5
Browse files Browse the repository at this point in the history
Release 1.5
  • Loading branch information
Yuri05 authored Dec 7, 2023
2 parents ff066f6 + ff3982b commit d2b4a09
Show file tree
Hide file tree
Showing 114 changed files with 16,525 additions and 185 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: ospsuite.utils
Title: Utility Functions for Open Systems Pharmacology R Packages
Version: 1.4.0
Version: 1.5.0
Authors@R: c(
person("Open-Systems-Pharmacology Community", role = c("cph", "fnd")),
person("Michael", "Sevestre", , "michael@design2code.ca", role = c("aut", "cre")),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export(enumPut)
export(enumRemove)
export(enumValues)
export(flattenList)
export(foldSafe)
export(formatNumerics)
export(getEnumKey)
export(getOSPSuiteUtilsSetting)
Expand All @@ -25,6 +26,7 @@ export(isIncluded)
export(isOfLength)
export(isOfType)
export(isSameLength)
export(logSafe)
export(messages)
export(objectCount)
export(ospsuiteUtilsSettingNames)
Expand Down
19 changes: 18 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# ospsuite.utils 1.4.0
# ospsuite.utils 1.5.0

NEW FUNCTIONS

* `logSafe()` to compute logarithm of values that could be close to 0 or slightly
negative.

* `foldSafe()` to compute `x / y` when `x` or `y` could be negative or zero. All values below a
certain threshold `epsilon` are substituted by `epsilon`. NOTE: not suited for
calculating fold differences of negative numbers.

BUG FIXES

* The print function of the `Printable` class now converts values using the `format`
function before printing. E.g., numerical value "0.99999999" will be displayed as "1".
https://github.com/Open-Systems-Pharmacology/OSPSuite.RUtils/issues/120

# ospsuite.utils 1.4.23

NEW FUNCTIONS

Expand Down
3 changes: 3 additions & 0 deletions R/ospsuite.utils-env.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ ospsuiteUtilsEnv$suiteName <- "Open Systems Pharmacology"
# default values for the `formatNumerics()` helper function
ospsuiteUtilsEnv$formatNumericsDigits <- 2L

# Small value added to zero when calculating log
ospsuiteUtilsEnv$LOG_SAFE_EPSILON <- 1e-20

#' Names of the settings stored in `ospsuiteEnv`. Can be used with
#' `getOSPSuiteUtilsSetting()`
#'
Expand Down
4 changes: 2 additions & 2 deletions R/printable.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Printable <- R6::R6Class(
entries <- c(" ", entries)
}

entries <- c(entries, value)
entries <- c(entries, "\n")
value <- format(value)
entries <- c(entries, value, "\n")
cat(entries, sep = " ")
invisible(self)
},
Expand Down
2 changes: 1 addition & 1 deletion R/utilities-validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
.isBaseType <- function(x) {
baseTypes <- c("character", "logical", "integer", "double")

if (typeof(x) %in% baseTypes) {
if (any(baseTypes == typeof(x))) {
return(TRUE)
}

Expand Down
69 changes: 64 additions & 5 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ flattenList <- function(x, type) {

if (is.list(x)) {
x <- switch(type,
"character" = purrr::flatten_chr(x),
"character" = purrr::list_c(x, ptype = "c"),
"numeric" = ,
"real" = ,
"double" = purrr::flatten_dbl(x),
"integer" = purrr::flatten_int(x),
"logical" = purrr::flatten_lgl(x),
purrr::flatten(x)
"double" = purrr::list_c(x, ptype = 1.0),
"integer" = purrr::list_c(x, ptype = 1),
"logical" = purrr::list_c(x, ptype = TRUE),
purrr::list_flatten(x)
)
}

Expand Down Expand Up @@ -111,3 +111,62 @@ toMissingOfType <- function(x, type) {

return(x)
}

#' Computes logarithm of a number or of a vector of numbers and handles zeros while
#' substituting all values below `epsilon` by `epsilon`.
#'
#' @param x A numeric or a vector of numerics.
#' @param base a positive or complex number: the base with respect to which logarithms are computed. Defaults to e = exp(1).
#' @param epsilon A very small number which is considered as threshold below which
#' all values are treated as `epsilon`. Allows computation of `log` close to 0.
#' Default value is `getOSPSuiteUtilsSetting("LOG_SAFE_EPSILON")`.
#'
#' @return `log(x, base = base)` for `x > epsilon`, or `log(epsilon, base = base)`,
#' or `NA_real_` for `NA` elements.
#' @export
#'
#' @examples
#' inputVector <- c(NA, 1, 5, 0, -1)
#' logSafe(inputVector)
logSafe <- function(x, base = exp(1), epsilon = ospsuiteUtilsEnv$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)
}

#' Safe fold calculation
#'
#' @description
#' Calculates `x / y` while substituting values below `epsilon` (for x and y) by `epsilon`.
#' `x` and `y` must be of the same length
#'
#'
#' @param x A numeric or a vector of numerics.
#' @param y A numeric or a vector of numerics.
#' @param epsilon A very small number which is considered as threshold below which
#' all values are treated as `epsilon`. Allows computation of fold changes for values close to 0.
#' Default value is `getOSPSuiteUtilsSetting("LOG_SAFE_EPSILON")`.
#'
#' @return A vector with `x / y`.
#' @export
#'
#' @examples
#' inputX <- c(NA, 1, 5, 0, -1)
#' inputY <- c(1, -1, NA, 0, -1)
#' folds <- foldSafe(inputX, inputY)
foldSafe <- function(x, y, epsilon = ospsuiteUtilsEnv$LOG_SAFE_EPSILON) {
validateIsSameLength(x, y)
x[x <= epsilon] <- epsilon
y[y <= epsilon] <- epsilon

return(x / y)
}
2 changes: 1 addition & 1 deletion R/validation-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ validateIsInteger <- function(object, nullAllowed = FALSE) {
}

if (is.list(object)) {
object <- unlist(object)
object <- unlist(object, use.names = FALSE)
}

# if it's an actual integer (e.g. 5L)
Expand Down
6 changes: 6 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ reference:
- messages
- objectCount
- flattenList
- logSafe
- foldSafe

- tite: Math helpers
contents:
- logSafe

- title: Package settings
contents:
Expand Down
9 changes: 3 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ install:
- git submodule update --init --recursive

environment:
app_version: "1.4"
app_version: "1.5"
USE_RTOOLS: true
R_VERSION: "4.1.0"
R_VERSION: "4.3.1"
R_ARCH: x64
KEEP_VIGNETTES: true
NOT_CRAN: true
R_PKGTYPE: binary

version: "$(app_version).{build}"

Expand Down Expand Up @@ -50,14 +51,10 @@ before_build:
build_script:
- travis-tool.sh install_deps
- travis-tool.sh r_binary_install curl
- Rscript -e "install.packages('covr', repos = 'http://cran.us.r-project.org', type = 'source')"

test_script:
- travis-tool.sh run_tests

on_success:
- Rscript -e "covr::codecov()"

after_test:
- ps: copy ospsuite.utils_*.zip ospsuite.utils.zip

Expand Down
10 changes: 5 additions & 5 deletions docs/404.html

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

Loading

0 comments on commit d2b4a09

Please sign in to comment.