Skip to content

Commit

Permalink
improves sits_get_probs and corrects logit values to avoid -Inf and +Inf
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertocamara committed Nov 19, 2024
1 parent dad9b66 commit 87bcf5b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 4 additions & 0 deletions R/api_smooth.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@
# Check values length
input_pixels <- nrow(values)
# Compute logit
# adjust values to avoid -Inf or +Inf in logits
values[values == 1.0] <- 0.999999
values[values == 0.0] <- 0.000001
# tranform to logits
values <- log(values / (rowSums(values) - values))
# Process Bayesian
values <- bayes_smoother_fraction(
Expand Down
6 changes: 3 additions & 3 deletions R/sits_get_probs.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ sits_get_probs.sf <- function(cube, samples, window_size = NULL){
}
#' @rdname sits_get_probs
#' @export
sits_get_probs.sits <- function(cube, samples){
.check_set_caller("sits_get_data")
sits_get_probs.sits <- function(cube, samples, window_size = NULL){
.check_set_caller("sits_get_probs")
# get the data
data <- .data_get_probs(
cube = cube,
Expand All @@ -110,7 +110,7 @@ sits_get_probs.sits <- function(cube, samples){
}
#' @rdname sits_get_probs
#' @export
sits_get_probs.data.frame <- function(cube, samples){
sits_get_probs.data.frame <- function(cube, samples, window_size = NULL){
.check_set_caller("sits_get_probs")
# get the data
data <- .data_get_probs(
Expand Down
5 changes: 3 additions & 2 deletions src/smooth_bayes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ NumericVector bayes_smoother_fraction(const NumericMatrix& logits,
for (int band = 0; band < logits.ncol(); ++band) {
// compute the neighborhood
for (int wi = 0; wi < window_size; ++wi)
for (int wj = 0; wj < window_size; ++wj)
for (int wj = 0; wj < window_size; ++wj) {
neigh(wi * window_size + wj) =
logits(loci(wi + i) * ncols + locj(wj + j), band);
}
// remove NA
NumericVector neigh2 = na_omit(neigh);
if (neigh_fraction < 1.0)
Expand All @@ -62,7 +63,7 @@ NumericVector bayes_smoother_fraction(const NumericMatrix& logits,
double m0 = mean(noNA(high_values));
// get the current value
double x0 = logits(i * ncols + j, band);
if (std::isnan(x0)) {
if (std::isnan(x0) || s0 < 1e-04) {
res(i * ncols + j, band) = m0;
} else {
// weight for Bayesian estimator
Expand Down

0 comments on commit 87bcf5b

Please sign in to comment.