Skip to content

Commit 3aec823

Browse files
committed
update v0.2; adding drop features
1 parent 8cdd543 commit 3aec823

6 files changed

Lines changed: 58 additions & 43 deletions

File tree

ClassicHCAS.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: 9e256ccf-6452-4993-ae45-57abc259c716
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: ClassicHCAS
22
Type: Package
33
Title: The Classic Habitat Condition Assessment System (HCAS)
4-
Version: 0.1.6
5-
Date: 2024-11-11
4+
Version: 0.2.0
5+
Date: 2025-06-17
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

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Version 0.2.0
2+
* Added `drop_features` parameter to fully exclude specific variables from both the `histogram` and `benchmark` functions.
3+
* Adding a condition to ensure that `k_obs` is less than or equal to `k_pred`.
4+
15
# Version 0.1.6
26
* Added a `scale_factor` parameter to allow user-defined correction of geographic CRS distance calculations, enhancing flexibility in handling distance conversions (previously set to a default value only).
37
* Enhanced C++ code for improved efficiency in point class creation.

R/benchmark.R

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
#' @param confidence Numeric. The confidence value for LDC methods. See details below..
5656
#' @param lambda Numeric. The lambda param for LDC Cauchy weighting...
5757
#' @param exclude_slef Logical. To exclude a benchmark point from assessing itself.
58+
#' @param drop_features Integer vector. Completely remove the RS variable from the benchmarking process. For
59+
#' consistency, it is recommended to exclude the same variables used in the histogram step; unless
60+
#' you have a specific reason not to.
5861
#' @param make_su Logical. To make the uncertainty map or not.
5962
#' @param num_threads Integer. Specifies the number of CPU threads to be used for processing. A value
6063
#' below 1 indicates that all available threads will be utilized. Refer to the details section for
@@ -90,68 +93,67 @@ benchmark <- function(
9093
confidence = 0.5,
9194
lambda = 2.0,
9295
exclude_slef = TRUE,
96+
drop_features = NULL,
9397
make_su = FALSE,
9498
num_threads = -1,
9599
...) {
96100

101+
# check k_pred and k_obs
102+
if (k_pred < k_obs) stop("'k_obs' must be smaller or equal to 'k_pred'.")
97103
# check samples and histograms
98-
if (.is_mat(samples)) {
99-
samples <- .check_mat(samples)
100-
} else {
101-
stop("'samples' must be a matrix or an object convertibe to matrix")
102-
}
103-
if (.is_mat(histogram)) {
104-
histogram <- .check_mat(histogram)
105-
} else {
106-
stop("'histogram' must be a matrix or an object convertibe to matrix")
107-
}
108-
109-
if (nrow(histogram) != ncol(histogram)) {
110-
warning("Historgram dimensions are not the same!\n")
111-
}
112-
# cat("Histogram dimension:", dim(histogram), "\n")
104+
samples <- if (.is_mat(samples)) .check_mat(samples) else stop("'samples' must be a matrix or convertible to one.")
105+
histogram <- if (.is_mat(histogram)) .check_mat(histogram) else stop("'histogram' must be a matrix or convertible to one.")
106+
if (nrow(histogram) != ncol(histogram)) warning("Historgram dimensions are not the same!\n")
113107

114108
if (methods::is(histogram, "histo")) {
115109
# check for histo bin_width consistency
116110
if (is.null(bin_width)) {
117111
bin_width <- attributes(histogram)$bin.width
118112
} else {
119113
if (bin_width != attributes(histogram)$bin.width) {
120-
warning("The supplied 'bin_width` is different from the arrtibute(histogram)$bin.width from the input.")
114+
warning("Provided 'bin_width' differs from histogram attribute.")
121115
}
122116
}
123117
# check for histo offset consistency
124118
if (is.null(offset)) {
125119
offset <- attributes(histogram)$offset
126120
} else {
127121
if (offset != attributes(histogram)$offset) {
128-
warning("The supplied 'offset` is different from the arrtibute(histogram)$offset from the input.")
122+
warning("Provided 'offset' differs from histogram attribute.")
129123
}
130124
}
131125
}
132126

133127
# interpolate histogram
134128
if (interpolate) {
135129
histogram <- terra::as.matrix(
136-
x = terra::disagg(
137-
x = terra::rast(histogram),
138-
fact = 2,
139-
method = "bilinear"
140-
),
130+
terra::disagg(terra::rast(histogram), fact = 2, method = "bilinear"),
141131
wide = TRUE
142132
)
143133
}
144134
# get the bin number after interpolation
145135
bin_num <- min(dim(histogram))
146136

147-
137+
exclude_var <- NULL
138+
# drop features from calculation if requested
139+
if (length(drop_features)) {
140+
n_vars <- (ncol(samples) - 2) / 2
141+
exclude_var <- c(drop_features + 2, drop_features + 2 + n_vars)
142+
}
148143

149144
if (.is_mat(data)) {
150145
# check and convert to matrix
151146
data <- .check_mat(data)
152147
# correction scale for long-lat CRS
153148
correction <- ifelse(.is_lonlat(data), scale_factor, 1)
154149

150+
if (ncol(samples) != ncol(data)) {
151+
stop("Samples must include all raster values (matching column count with 'data').")
152+
}
153+
154+
# remove the features from the reference samples as well
155+
if (!is.null(exclude_var)) samples[, exclude_var] <- 0
156+
155157
tryCatch(
156158
{
157159
output <- benchmarking(
@@ -172,6 +174,7 @@ benchmark <- function(
172174
confidence = confidence,
173175
lambda = lambda,
174176
exclude_slef = exclude_slef,
177+
drop = exclude_var,
175178
make_su = make_su,
176179
num_threads = num_threads
177180
)
@@ -192,16 +195,14 @@ benchmark <- function(
192195
# sample extraction if needed
193196
if (ncol(samples) == 2) {
194197
cat("Extracting sample values...\n")
195-
# add xy to the stack and extract
196-
samples <- cbind(
197-
samples,
198-
as.matrix(terra::extract(x = data, y = samples, ID = FALSE))
199-
)
200-
} else if ((ncol(samples) - 2) == terra::nlyr(data)) {
201-
cat("Samples with raster values are provided!\n")
202-
} else{
203-
# this should include rows/cols columns as well
204-
stop("Samples must either be coordinate values exclusively or include all raster layer values.")
198+
samples <- cbind(samples, as.matrix(terra::extract(data, samples, ID = FALSE)))
199+
} else if ((ncol(samples) - 2) != terra::nlyr(data)) {
200+
stop("Sample feature count does not match number of raster layers.")
201+
}
202+
203+
# remove the features from the reference samples as well
204+
if (length(drop_features)) {
205+
samples[, exclude_var] <- 0
205206
}
206207

207208
tryCatch(
@@ -225,6 +226,7 @@ benchmark <- function(
225226
confidence = confidence,
226227
lambda = lambda,
227228
exclude_slef = exclude_slef,
229+
drop = exclude_var,
228230
make_su = make_su,
229231
num_threads = num_threads,
230232
...
@@ -248,7 +250,7 @@ benchmark <- function(
248250
# the generic HCAS prediction function for C++ integration with terra
249251
# NOTE: the na.rm arg in terra::predict doesn't provide the correct mask
250252
# and keep model an empty list
251-
benchmarking <- function(model, newdata, make_su, ...){
253+
benchmarking <- function(model, newdata, make_su, drop = NULL, ...){
252254
# check for NAs
253255
has_na <- anyNA(newdata)
254256
# number of output columns; TRUE/FALSE + 1
@@ -268,6 +270,9 @@ benchmarking <- function(model, newdata, make_su, ...){
268270
dat <- as.matrix(newdata)
269271
}
270272

273+
# if drop_features are provided exclude them from features
274+
if (length(drop)) dat[, drop] <- 0
275+
271276
tryCatch(
272277
{
273278
# the HCAS C++ function
@@ -278,7 +283,7 @@ benchmarking <- function(model, newdata, make_su, ...){
278283
)
279284
},
280285
error = function(cond) {
281-
message("Error: the benchmarking C++ function faild, returning -0.02!")
286+
message("Benchmarking C++ function failed. Returning -0.02 for all cells.")
282287
# return error values -0.02
283288
return(
284289
matrix(-0.02, nrow = nr, ncol = nc)

R/histogram.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#' meters in unprojected coordinate systems. The default is 100,000 for an average conversion in Australia.
3636
#' On the equator, this factor is approximately 111,235, and it varies with latitude according to a cosine
3737
#' function.
38+
#' @param drop_features Integer vector. Completely remove the RS variable from the histogram generation. For
39+
#' consistency, it is recommended to exclude the same variables later in the benchmarking step; unless
40+
#' you have a specific reason not to.
3841
#' @param num_threads Integer. Specifies the number of CPU threads to be used for processing. A value
3942
#' below 1 indicates that all available threads will be utilized. Refer to the details section for
4043
#' additional information.
@@ -60,15 +63,12 @@ histogram <- function(
6063
bin_width = 0.05,
6164
bin_num = 650,
6265
scale_factor = 100000,
66+
drop_features = NULL,
6367
num_threads = -1,
6468
filename = "") {
6569

6670
# check samples_xy
67-
if (.is_mat(samples_xy)) {
68-
samples_xy <- .check_mat(samples_xy)
69-
} else {
70-
stop("'samples_xy' must be a matrix or an object convertibe to matrix")
71-
}
71+
samples_xy <- if (.is_mat(samples_xy)) .check_mat(samples_xy) else stop("'samples_xy' must be a matrix or convertible to one.")
7272

7373
if (ncol(samples_xy) != 2)
7474
stop("'samples_xy' must be a data.frame or matrix with exactly two columns: one for longitude (x) and one for latitude (y).")
@@ -118,6 +118,12 @@ histogram <- function(
118118
cat("Predicted:", colnames(predicted), "\n")
119119
}
120120

121+
# drop features from calculation if requested
122+
if (length(drop_features)) {
123+
observed[, drop_features] <- 0
124+
predicted[, drop_features] <- 0
125+
}
126+
121127
# run histo calculate in C++
122128
tryCatch(
123129
{

src/Helper.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
22

3-
#define PI 3.14159265358978993897
43
#define PI_Sq 9.869604401089358
54

65

0 commit comments

Comments
 (0)