Skip to content

Commit 6875ffc

Browse files
committed
Small helper function for object size and validation fixes
1 parent b2b6dbd commit 6875ffc

File tree

5 files changed

+157
-3
lines changed

5 files changed

+157
-3
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export(modal)
126126
export(new_id)
127127
export(new_waiver)
128128
export(nicheplot)
129+
export(objects_size)
129130
export(partial)
130131
export(partial.DistributionModel)
131132
export(partial_density)
@@ -172,3 +173,4 @@ import(terra)
172173
importFrom(foreach,"%do%")
173174
importFrom(foreach,"%dopar%")
174175
importFrom(stats,effects)
176+
importFrom(utils,object.size)

R/utils.R

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,108 @@ collect_occurrencepoints <- function(model, include_absences = FALSE,
632632
}
633633
return(locs)
634634
}
635+
636+
#' @title Shows size of objects in the R environment
637+
#' @description Shows the size of the objects currently in the R environment.
638+
#' Helps to locate large objects cluttering the R environment and/or
639+
#' causing memory problems during the execution of large workflows.
640+
#'
641+
#' @param n Number of objects to show, Default: `10`
642+
#' @return A data frame with the row names indicating the object name,
643+
#' the field 'Type' indicating the object type, 'Size' indicating the object size,
644+
#' and the columns 'Length/Rows' and 'Columns' indicating the object dimensions if applicable.
645+
#'
646+
#' @examples
647+
#' if(interactive()){
648+
#'
649+
#' #creating dummy objects
650+
#' x <- matrix(runif(100), 10, 10)
651+
#' y <- matrix(runif(10000), 100, 100)
652+
#'
653+
#' #reading their in-memory size
654+
#' objects_size()
655+
#'
656+
#' }
657+
#' @author Bias Benito
658+
#' @rdname objects_size
659+
#' @importFrom utils object.size
660+
#' @export
661+
objects_size <- function(n = 10) {
662+
663+
.ls.objects <- function (
664+
pos = 1,
665+
pattern,
666+
order.by,
667+
decreasing=FALSE,
668+
head=FALSE,
669+
n=5
670+
){
671+
672+
napply <- function(names, fn) sapply(
673+
names,
674+
function(x) fn(get(x, pos = pos))
675+
)
676+
677+
names <- ls(
678+
pos = pos,
679+
pattern = pattern
680+
)
681+
682+
obj.class <- napply(
683+
names,
684+
function(x) as.character(class(x))[1]
685+
)
686+
687+
obj.mode <- napply(
688+
names,
689+
mode
690+
)
691+
692+
obj.type <- ifelse(
693+
is.na(obj.class),
694+
obj.mode,
695+
obj.class
696+
)
697+
698+
obj.prettysize <- napply(
699+
names,
700+
function(x) {format(utils::object.size(x), units = "auto") }
701+
)
702+
703+
obj.size <- napply(
704+
names,
705+
object.size
706+
)
707+
708+
obj.dim <- t(
709+
napply(
710+
names,
711+
function(x)as.numeric(dim(x))[1:2]
712+
)
713+
)
714+
715+
vec <- is.na(obj.dim)[, 1] & (obj.type != "function")
716+
717+
obj.dim[vec, 1] <- napply(names, length)[vec]
718+
719+
out <- data.frame(
720+
obj.type,
721+
obj.prettysize,
722+
obj.dim
723+
)
724+
names(out) <- c("Type", "Size", "Length/Rows", "Columns")
725+
if (!missing(order.by))
726+
out <- out[order(out[[order.by]], decreasing=decreasing), ]
727+
if (head)
728+
out <- head(out, n)
729+
out
730+
}
731+
732+
.ls.objects(
733+
order.by = "Size",
734+
decreasing=TRUE,
735+
head=TRUE,
736+
n=n
737+
)
738+
739+
}

R/validate.R

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ methods::setMethod(
426426
return(results)
427427
}
428428

429+
# R2 score
430+
R2_Score <- function(pred, obs, na.rm = TRUE) {
431+
return( 1 - sum((obs - pred)^2,na.rm = na.rm)/sum((obs - mean(obs,na.rm = na.rm))^2,na.rm = na.rm) )
432+
}
429433
# Function for Root-mean square error
430434
RMSE <- function(pred, obs, na.rm = TRUE) {
431435
sqrt(mean((pred - obs)^2, na.rm = na.rm))
@@ -434,6 +438,10 @@ methods::setMethod(
434438
MAE <- function(pred, obs, na.rm = TRUE) {
435439
mean(abs(pred - obs), na.rm = na.rm)
436440
}
441+
# Mean Absolute Percentage Error Loss
442+
MAPE <- function(pred, obs, na.rm = TRUE){
443+
mean(abs((obs - pred)/obs), na.rm = TRUE)
444+
}
437445
# Function for log loss/cross-entropy loss.
438446
Poisson_LogLoss <- function(y_pred, y_true) {
439447
eps <- 1e-15
@@ -458,21 +466,23 @@ methods::setMethod(
458466
modelid = id,
459467
name = name,
460468
method = method,
461-
metric = c('n','rmse', 'mae',
469+
metric = c('n', 'r2', 'rmse', 'mae', 'mape',
462470
'logloss','normgini',
463471
'cont.boyce'),
464472
value = NA
465473
)
466474
# - #
467475
out$value[out$metric=='n'] <- nrow(df2) # Number of records
476+
out$value[out$metric=='r2'] <- R2_Score(pred = df2$pred, obs = df2[[point_column]]) # R2
468477
out$value[out$metric=='rmse'] <- RMSE(pred = df2$pred, obs = df2[[point_column]]) # RMSE
469478
out$value[out$metric=='mae'] <- MAE(pred = df2$pred, obs = df2[[point_column]]) # Mean absolute error
479+
out$value[out$metric=='mape'] <- MAPE(pred = df2$pred, obs = df2[[point_column]]) # Mean Absolute Percentage Error Loss
470480
out$value[out$metric=='normgini'] <- NormalizedGini(y_pred = df2$pred, y_true = df2[[point_column]])
471481

472482
if(!is.null(mod)){
473483
if( any( sapply(mod$model$biodiversity, function(x) x$family) == "binomial" ) ){
474-
LogLoss <- function(y_pred, y_true) {
475-
y_pred <- pmax(y_pred, 1e-15)
484+
LogLoss <- function(y_pred, y_true, eps = 1e-15) {
485+
y_pred <- pmax(pmin(y_pred, 1 - eps), eps)
476486
LogLoss <- -mean(y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred))
477487
return(LogLoss)
478488
}

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ reference:
136136
- predictor_derivate
137137
- predictor_filter
138138
- interpolate_gaps
139+
- objects_size
139140
- run_stan
140141
- wrap_stanmodel
141142
- sanitize_names

man/objects_size.Rd

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)