Skip to content

Commit a1d3ee8

Browse files
authored
Valgrind issue 353 (#357)
* valgrind issue solved * updating cran comments
1 parent 500dded commit a1d3ee8

File tree

8 files changed

+31
-19
lines changed

8 files changed

+31
-19
lines changed

R/compute_mallows.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ compute_mallows <- function(
7777
validate_class(compute_options, "BayesMallowsComputeOptions")
7878
validate_class(priors, "BayesMallowsPriors")
7979
validate_class(initial_values, "BayesMallowsInitialValues")
80+
validate_rankings(data)
8081
validate_preferences(data, model_options)
8182
validate_initial_values(initial_values, data)
8283

R/validation_functions.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ validate_preferences <- function(data, model) {
5757
}
5858
}
5959

60+
validate_rankings <- function(data) {
61+
if (nrow(data$rankings) <= 0) stop("Data must have at least one row.")
62+
}
63+
6064
validate_initial_values <- function(initial_values, data) {
6165
if (!is.null(initial_values$rho)) {
6266
if (length(unique(initial_values$rho)) != length(initial_values$rho)) {

cran-comments.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Resubmission Note
22

3+
UPDATE: The original resubmission did not fix a valgrind error. We have reproduced the error and fixed it.
4+
35
This is a resubmission, fixing issues with "clang-UBSAN", "gcc-UBSAN", and "valgrind". We have reproduced the errors using the Docker images rocker/r-devel-san, and made the necessary changes.
46

57
There are also two failing unit tests on noLD. We believe these to be cause be differences in floating point numbers, since the test expectations are based on values after running thousands of steps of an MCMC algorithm. We have added testthat::skip_on_cran() to the two tests in question.

src/classes.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ struct Clustering {
8484
void update_dist_mat(const Data& dat, const Parameters& pars,
8585
const std::unique_ptr<Distance>& distfun);
8686

87-
arma::mat cluster_probs;
8887
arma::vec current_cluster_probs;
89-
arma::umat cluster_assignment;
90-
arma::uvec current_cluster_assignment;
91-
arma::mat within_cluster_distance;
9288
const bool clustering;
9389
const unsigned int clus_thinning;
9490

@@ -97,6 +93,13 @@ struct Clustering {
9793
arma::mat dist_mat;
9894
const bool include_wcd;
9995
const bool save_ind_clus;
96+
int n_cluster_assignments;
97+
98+
public:
99+
arma::mat cluster_probs;
100+
arma::umat cluster_assignment;
101+
arma::uvec current_cluster_assignment;
102+
arma::mat within_cluster_distance;
100103
};
101104

102105
struct Augmentation {

src/clustering_class.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@ Clustering::Clustering(const Parameters& pars,
88
clustering {pars.n_clusters > 1},
99
clus_thinning { compute_options["clus_thinning"] },
1010
index { regspace<uvec>(0, pars.n_clusters - 1) },
11+
dist_mat { zeros(n_assessors, pars.n_clusters) },
1112
include_wcd { compute_options["include_wcd"] },
12-
save_ind_clus { compute_options["save_ind_clus"] } {
13-
int n_cluster_assignments = pars.n_clusters > 1 ?
14-
std::ceil(static_cast<double>(pars.nmc * 1.0 / clus_thinning)) : 1;
15-
cluster_probs.set_size(pars.n_clusters, n_cluster_assignments);
13+
save_ind_clus { compute_options["save_ind_clus"] },
14+
n_cluster_assignments { pars.n_clusters > 1 ?
15+
static_cast<int>(std::ceil(static_cast<double>(pars.nmc * 1.0 / clus_thinning))) : 1 },
16+
cluster_probs { zeros(pars.n_clusters, n_cluster_assignments) },
17+
cluster_assignment { zeros<umat>(n_assessors, n_cluster_assignments) },
18+
current_cluster_assignment { cluster_assignment.col(0) },
19+
within_cluster_distance { zeros(pars.n_clusters, include_wcd ? pars.nmc : 1) }
20+
{
1621
cluster_probs.col(0).fill(1.0 / pars.n_clusters);
1722
current_cluster_probs = cluster_probs.col(0);
18-
cluster_assignment.set_size(n_assessors, n_cluster_assignments);
1923
ivec a = Rcpp::sample(pars.n_clusters, n_assessors, true) - 1;
2024
cluster_assignment.col(0) = conv_to<uvec>::from(a);
21-
current_cluster_assignment = cluster_assignment.col(0);
22-
23-
dist_mat.set_size(n_assessors, pars.n_clusters);
24-
within_cluster_distance.set_size(
25-
pars.n_clusters, include_wcd ? pars.nmc : 1);
2625
update_wcd(0);
2726
}
2827

tests/testthat/test-assign_cluster.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test_that("assign_cluster works", {
2020
a1 <- assign_cluster(mod, soft = FALSE, expand = FALSE)
2121
expect_equal(dim(a1), c(60, 3))
2222
agg1 <- aggregate(assessor ~ map_cluster, a1, length)
23-
expect_equal(agg1$assessor, c(23, 17, 20))
23+
expect_equal(agg1$assessor, c(21, 20, 19))
2424

2525
a2 <- assign_cluster(mod, soft = TRUE, expand = FALSE)
2626
expect_equal(ncol(a2), 4)

tests/testthat/test-compute_posterior_intervals.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ test_that("compute_posterior_intervals works with clusters", {
4747
)
4848

4949
pi <- compute_posterior_intervals(mod)
50-
expect_equal(pi$hpdi[[3]], "[0.536,0.766]")
51-
expect_equal(pi$central_interval[[2]], "[1.263,1.846]")
50+
expect_equal(pi$hpdi[[3]], "[0.608,0.753]")
51+
expect_equal(pi$central_interval[[2]], "[0.710,1.140]")
5252

5353
pi <- compute_posterior_intervals(mod, parameter = "rho")
5454
expect_equal(unique(pi$cluster), factor(paste("Cluster", 1:3)))
@@ -57,8 +57,8 @@ test_that("compute_posterior_intervals works with clusters", {
5757
parameter = "cluster_probs",
5858
level = .96, decimals = 4
5959
)
60-
expect_equal(pi$hpdi[[2]], "[0.3623,0.5374]")
61-
expect_equal(pi$central_interval[[1]], "[0.2129,0.3812]")
60+
expect_equal(pi$hpdi[[2]], "[0.2552,0.3708]")
61+
expect_equal(pi$central_interval[[1]], "[0.3184,0.4840]")
6262
})
6363

6464
test_that("compute_posterior_intervals works for SMC", {

work-docs/docker.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ docker run -v "$(pwd)":"/opt/$(basename $(pwd))" -it --cap-add=SYS_PTRACE rocker
66

77
# To get httr package:
88
apt-get install libssl-dev
9+
10+
11+
RD -d "valgrind --tool=memcheck --leak-check=full --track-origins=yes"

0 commit comments

Comments
 (0)