Skip to content

Commit 3fe0bb1

Browse files
committed
doc update, error check, etc
1 parent 5dfa4a0 commit 3fe0bb1

9 files changed

Lines changed: 38 additions & 36 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: ClassicHCAS
22
Type: Package
33
Title: The Classic Habitat Condition Assessment System (HCAS).
44
Version: 1.0.0
5-
Date: 2025-10-08
5+
Date: 2025-10-15
66
Author: Roozbeh Valavi, Chris Ware, Eric Lehmann, Kristen Williams, Mike Birchall, Simon Collings, Simon Ferrier, and Tom Harwood
77
Maintainer: <roozbeh.valavi@csiro.au>
88
Description: The HCAS evaluates habitat condition by comparing observed and

R/benchmark.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Condition benchmarking of target points
1+
#' Condition benchmarking of target sites
22
#'
33
#' The HCAS (Habitat Condition Assessment System) benchmarking function evaluates habitat
44
#' condition by comparing observed and predicted remote sensing (RS) variables. It integrates
@@ -64,9 +64,9 @@
6464
#' @param confidence Numeric. The confidence value for LDC methods. See details below..
6565
#' @param lambda Numeric. The lambda param for LDC Cauchy weighting...
6666
#' @param exclude_slef Logical. To exclude a benchmark point from assessing itself.
67-
#' @param drop_features Integer vector. Completely remove the RS variable from the benchmarking process. For
68-
#' consistency, it is recommended to exclude the same variables used in the histogram step; unless
69-
#' you have a specific reason not to.
67+
#' @param drop_features Integer vector of remote sensing variable IDs. Completely remove the RS variable
68+
#' from the benchmarking process. For consistency, it is recommended to exclude the same variables used
69+
#' in the histogram step; unless you have a specific reason not to.
7070
#' @param make_su Logical. To make the uncertainty map or not.
7171
#' @param ... Additional arguments for writing raster outputs e.g. \code{filename},
7272
#' \code{overwrite}, and \code{wopt} from terra \code{\link[terra]{predict}}.
@@ -146,6 +146,9 @@ benchmark <- function(
146146
# drop features from calculation if requested
147147
if (length(drop_features)) {
148148
n_vars <- (ncol(samples) - 2) / 2
149+
if (any(drop_features > n_vars)) {
150+
stop("The feature IDs in drop_features must not be larger than the number of remote sensing variables.")
151+
}
149152
exclude_var <- c(drop_features + 2, drop_features + 2 + n_vars)
150153
}
151154

R/histogram.R

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
#' @param bin_num Integer. Specifies the number of bins for the histogram. It is generally recommended
4848
#' to use the default value of 650. Adjusting \code{bin_width} is often more effective than changing
4949
#' \code{bin_num}.
50-
#' @param drop_features Integer vector. Completely remove the RS variable from the histogram generation. For
51-
#' consistency, it is recommended to exclude the same variables later in the benchmarking step; unless
52-
#' you have a specific reason not to.
50+
#' @param drop_features Integer vector of remote sensing variable IDs. Completely remove the RS variable
51+
#' from the histogram generation. For consistency, it is recommended to exclude the same variables later
52+
#' in the benchmarking step; unless you have a specific reason not to.
5353
#' @param num_threads Integer. Specifies the number of CPU threads to be used for processing. A value
5454
#' below 1 indicates that all available threads will be utilized (default). Refer to the details section for
5555
#' more information.
@@ -105,10 +105,10 @@ histogram <- function(
105105

106106

107107
# number of observed RS vars
108-
num_layers <- (ncol(data_vals) - 2) / 2
108+
n_vars <- (ncol(data_vals) - 2) / 2
109109
# id of obs and mod for saving
110-
mod_layers <- seq_len(num_layers) + 2
111-
obs_layers <- mod_layers + num_layers
110+
mod_layers <- seq_len(n_vars) + 2
111+
obs_layers <- mod_layers + n_vars
112112

113113
# get the correct columns for the C++ code
114114
samples_xy <- data_vals[, 1:2]
@@ -133,6 +133,9 @@ histogram <- function(
133133

134134
# drop features from calculation if requested
135135
if (length(drop_features)) {
136+
if (any(drop_features > n_vars)) {
137+
stop("The feature IDs in drop_features must not be larger than the number of remote sensing variables.")
138+
}
136139
observed[, drop_features] <- 0
137140
modelled[, drop_features] <- 0
138141
}

man/benchmark.Rd

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

man/calibrate.Rd

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/histogram.Rd

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

src/Benchmark.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ Rcpp::NumericMatrix bench_cpp(
8484
// Cast the values once; cleaner code
8585
const float32_t xypenalty = static_cast<float32_t>(xy_penalty);
8686
std::vector<float32_t> xystats(xy_stats.begin(), xy_stats.end());
87-
87+
// Check the size of stats so indexing doesn't panic!
88+
if (xystats.size() != 4) {
89+
Rcpp::stop("xy_stats must contain exactly 4 values!");
90+
}
91+
8892
// Normalise XY and apply weight to it
8993
samples.col(0) = ((samples.col(0).array() - xystats[0]) / xystats[2]) * xypenalty;
9094
samples.col(1) = ((samples.col(1).array() - xystats[1]) / xystats[3]) * xypenalty;
@@ -189,17 +193,17 @@ Rcpp::NumericMatrix bench_cpp(
189193
if (make_su)
190194
{
191195
// output matrix - both condition and SU values
192-
for (const auto& cval : condition_vect) {
193-
out_mat(i, 0) = cval.hc;
194-
out_mat(i, 1) = std::log(cval.su);
196+
for (const auto& v : condition_vect) {
197+
out_mat(i, 0) = v.hc;
198+
out_mat(i, 1) = std::log(v.su);
195199
i++;
196200
}
197201
}
198202
else
199203
{
200204
// output matrix - only condition values
201-
for (const auto& cval : condition_vect) {
202-
out_mat(i, 0) = cval.hc;
205+
for (const auto& v : condition_vect) {
206+
out_mat(i, 0) = v.hc;
203207
i++;
204208
}
205209
}

src/Helper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ std::vector<int> combined_Search(
3030
int64_t cos_scale,
3131
bool is_geo
3232
) {
33-
const int size = sample_x.size();
3433
// Use pair<ENV_Distance, Original_Index>
3534
std::vector<std::pair<float, int>> dist_idx;
3635
// Reserve 5000; it's a heuristic, but better than nothing
3736
dist_idx.reserve(5000);
3837

38+
const int size = sample_x.size();
3939
// 1. Perform radius check, and calculate ENV distance for KNN later
4040
for (int i = 0; i < size; ++i) {
4141
const int64_t dx = sample_x[i] - query_x;
@@ -108,7 +108,7 @@ inline Condition get_Condition(
108108
double p_dist = pred_dists[i];
109109

110110
double weight = 1.0;
111-
if (p_dist > 0) {
111+
if (p_dist > 0.0) {
112112
weight = 1.0 / (PI_SQ * p_dist * lambda * (1.0 + (p_dist * p_dist) / lambda_sq));
113113
}
114114

src/Histogram.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,10 @@ Rcpp::IntegerMatrix histo_cpp(
5555
RowMajorMatrix<double> xy = get_XY(xy_vals);
5656
// define the output matrix;
5757
RowMajorMatrix<uint64_t> matrix(bin_num, bin_num);
58-
// Matrix MUST be initialised with zero
58+
// Matrix MUST be initialised with zeros
5959
matrix.setZero();
6060

6161
const int nr = rs.rows();
62-
63-
6462
double scale;
6563
int64_t squared_dist;
6664
const double radius_m = radius_km * 1000.0;

0 commit comments

Comments
 (0)