Skip to content

Commit 7ec5e82

Browse files
committed
fix rds matrix conversion and compiling warning
1 parent 32842c4 commit 7ec5e82

9 files changed

Lines changed: 102 additions & 27 deletions

File tree

R/benchmark.R

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,19 @@ benchmark <- function(
130130
}
131131
}
132132

133-
# interpolate reference density
134-
if (interpolate) {
135-
ref_density <- terra::as.matrix(
136-
terra::disagg(terra::rast(ref_density), fact = 2, method = "bilinear"),
137-
wide = TRUE
138-
)
139-
# update the binwidth and offset
140-
bin_width <- bin_width / 2
141-
offset <- offset * 2
133+
# interpolate reference density
134+
if (interpolate) {
135+
ref_density <- terra::as.matrix(
136+
terra::disagg(
137+
terra::rast(.check_mat(unclass(ref_density), name = "ref_density")),
138+
fact = 2,
139+
method = "bilinear"
140+
),
141+
wide = TRUE
142+
)
143+
# update the binwidth and offset
144+
bin_width <- bin_width / 2
145+
offset <- offset * 2
142146
}
143147
# get the bin number after interpolation
144148
bin_num <- min(dim(ref_density))

R/ref_density.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,6 @@ plot.reference_density <- function(x, ...) {
200200
message("For aesthetic, the normalised reference density plot is reversed and transposed.")
201201
}
202202
terra::plot(
203-
terra::rast(x), col = palettes(150, "ref_density"), ...
203+
terra::rast(.check_mat(unclass(x), name = "x")), col = palettes(150, "ref_density"), ...
204204
)
205205
}

src/Benchmark.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// [[Rcpp::plugins(openmp)]]
22
// [[Rcpp::plugins("cpp11")]]
3+
#if defined(__GNUC__) || defined(__clang__)
4+
#pragma GCC diagnostic push
5+
#pragma GCC diagnostic ignored "-Wignored-attributes"
6+
#endif
37
#include <Rcpp.h>
48
#include <cmath>
59
#include <vector>
@@ -241,3 +245,7 @@ Rcpp::NumericMatrix bench_cpp(
241245

242246
return out_mat;
243247
}
248+
249+
#if defined(__GNUC__) || defined(__clang__)
250+
#pragma GCC diagnostic pop
251+
#endif

src/Matrix.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#ifndef MATRIX_T
22
#define MATRIX_T
33

4-
#include "RcppEigenQuiet.h"
4+
#include <Rcpp.h>
5+
#include <RcppEigen.h>
56

67
// Generic row-major matrix type
78
template <typename T>

src/Radial.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// [[Rcpp::plugins(openmp)]]
22
// [[Rcpp::plugins("cpp11")]]
3+
#if defined(__GNUC__) || defined(__clang__)
4+
#pragma GCC diagnostic push
5+
#pragma GCC diagnostic ignored "-Wignored-attributes"
6+
#endif
37
#include <Rcpp.h>
48
#include <vector>
59
#include <cmath>
@@ -120,5 +124,9 @@ Rcpp::IntegerVector radial_count_cpp(
120124
out[i] = count;
121125
}
122126

123-
return Rcpp::wrap(out);
124-
}
127+
return Rcpp::wrap(out);
128+
}
129+
130+
#if defined(__GNUC__) || defined(__clang__)
131+
#pragma GCC diagnostic pop
132+
#endif

src/RcppEigenQuiet.h

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Reference.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// [[Rcpp::plugins(openmp)]]
22
// [[Rcpp::plugins("cpp11")]]
3+
#if defined(__GNUC__) || defined(__clang__)
4+
#pragma GCC diagnostic push
5+
#pragma GCC diagnostic ignored "-Wignored-attributes"
6+
#endif
37
#include <Rcpp.h>
48
#include <cmath>
59
#include <vector>
@@ -151,3 +155,7 @@ Rcpp::IntegerMatrix ref_density_cpp(
151155

152156
return matrix;
153157
}
158+
159+
#if defined(__GNUC__) || defined(__clang__)
160+
#pragma GCC diagnostic pop
161+
#endif

tests/testthat/test-benchmark-wrapper.R

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,49 @@ test_that("benchmark uses reference density attributes when they are not supplie
4343
expect_equal(dim(out), c(2, 1))
4444
expect_true(all(is.finite(out[, 1])))
4545
})
46+
47+
test_that("benchmark interpolates reference_density objects", {
48+
skip_if_not_installed("terra")
49+
50+
samples <- matrix(
51+
c(
52+
0, 0, 0.2, 0.3, 0.2, 0.3,
53+
1, 1, 0.4, 0.5, 0.4, 0.5,
54+
2, 2, 0.6, 0.7, 0.6, 0.7,
55+
3, 3, 0.8, 0.9, 0.8, 0.9
56+
),
57+
ncol = 6,
58+
byrow = TRUE
59+
)
60+
61+
ref <- ref_density(
62+
data = samples,
63+
radius_km = 1000,
64+
bin_width = 0.1,
65+
bin_num = 10,
66+
num_threads = 1L
67+
)
68+
ref_norm <- normalise(
69+
x = ref,
70+
trim_size = 6,
71+
offset = 1
72+
)
73+
74+
expect_no_error(
75+
out <- benchmark(
76+
data = samples[1:2, , drop = FALSE],
77+
samples = samples,
78+
ref_density = ref_norm,
79+
radius_km = 1000,
80+
k_pred = 2,
81+
k_obs = 1,
82+
bin_width = NULL,
83+
offset = NULL,
84+
interpolate = TRUE,
85+
num_threads = 1L
86+
)
87+
)
88+
89+
expect_equal(dim(out), c(2, 1))
90+
expect_true(all(is.finite(out[, 1])))
91+
})

tests/testthat/test-reference-density-normalise.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,17 @@ test_that("normalise handles reference_density objects and normalises rows", {
4747
expect_equal(rowSums(out), rep(1, 4), tolerance = 1e-8)
4848
expect_true(file.exists(paste0(outfile, ".txt")))
4949
})
50+
51+
test_that("plot.reference_density handles custom class objects", {
52+
skip_if_not_installed("terra")
53+
54+
x <- matrix(seq_len(36), nrow = 6)
55+
class(x) <- c("reference_density", "matrix", "array")
56+
attr(x, "bin.width") <- 0.5
57+
58+
plot_file <- tempfile(fileext = ".pdf")
59+
grDevices::pdf(plot_file)
60+
on.exit(grDevices::dev.off(), add = TRUE)
61+
62+
expect_no_error(plot(x))
63+
})

0 commit comments

Comments
 (0)