Skip to content

Commit

Permalink
plugging memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
bcjaeger committed Oct 20, 2023
1 parent c87fc58 commit 20df562
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 325 deletions.
4 changes: 0 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ S3method(predict,orsf_fit)
S3method(print,orsf_fit)
S3method(print,orsf_summary_uni)
export(orsf)
export(orsf_control_classification)
export(orsf_control_cph)
export(orsf_control_custom)
export(orsf_control_fast)
export(orsf_control_net)
export(orsf_control_regression)
export(orsf_control_survival)
export(orsf_ice_inb)
export(orsf_ice_new)
export(orsf_ice_oob)
Expand Down
5 changes: 0 additions & 5 deletions R/orsf_control.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#' @return an object of class `'orsf_control'`, which should be used as
#' an input for the `control` argument of [orsf].
#'
#' @export
#'
#' @family orsf_control
#'
#' @details
Expand Down Expand Up @@ -370,7 +368,6 @@ orsf_control <- function(tree_type,
}

#' @rdname orsf_control
#' @export
orsf_control_classification <- function(method = 'glm',
scale_x = TRUE,
net_mix = 0.5,
Expand All @@ -394,7 +391,6 @@ orsf_control_classification <- function(method = 'glm',
}

#' @rdname orsf_control
#' @export
orsf_control_regression <- function(method = 'glm',
scale_x = TRUE,
net_mix = 0.5,
Expand All @@ -418,7 +414,6 @@ orsf_control_regression <- function(method = 'glm',
}

#' @rdname orsf_control
#' @export
orsf_control_survival <- function(method = 'glm',
scale_x = TRUE,
ties = 'efron',
Expand Down
7 changes: 5 additions & 2 deletions src/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

Data() = default;

Data(const Data&) = delete;
Data& operator=(const Data&) = delete;

virtual ~Data() = default;

Data(arma::mat& x,
arma::mat& y,
arma::vec& w) {
Expand All @@ -35,8 +40,6 @@

}

Data(const Data&) = delete;
Data& operator=(const Data&) = delete;

arma::uword get_n_rows() {
return(n_rows);
Expand Down
6 changes: 5 additions & 1 deletion src/Forest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ void Forest::compute_prediction_accuracy(Data* prediction_data,

}


std::vector<std::vector<arma::mat>> Forest::compute_dependence(bool oobag){

std::vector<std::vector<arma::mat>> result;
Expand Down Expand Up @@ -510,7 +511,10 @@ std::vector<std::vector<arma::mat>> Forest::compute_dependence(bool oobag){

if(oobag) oobag_denom.fill(0);

mat preds = predict(oobag);
// No. of cols in pred mat depend on the type of forest
mat preds;
resize_pred_mat(preds);
predict_single_thread(data.get(), oobag, preds);

if(pd_type == PD_SUMMARY){

Expand Down
7 changes: 5 additions & 2 deletions src/ForestSurvival.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ void ForestSurvival::load(
);
}

// Create thread ranges
equalSplit(thread_ranges, 0, n_tree - 1, n_thread);
if(n_thread > 1){
// Create thread ranges
equalSplit(thread_ranges, 0, n_tree - 1, n_thread);
}


}

Expand Down
1 change: 1 addition & 0 deletions src/ForestSurvival.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ForestSurvival: public Forest {
double split_min_events,
arma::vec& pred_horizon);

virtual ~ForestSurvival() override = default;

ForestSurvival(const ForestSurvival&) = delete;
ForestSurvival& operator=(const ForestSurvival&) = delete;
Expand Down
2 changes: 2 additions & 0 deletions src/TreeSurvival.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
TreeSurvival(const TreeSurvival&) = delete;
TreeSurvival& operator=(const TreeSurvival&) = delete;

virtual ~TreeSurvival() override = default;

TreeSurvival(double leaf_min_events,
double split_min_events,
arma::vec* unique_event_times,
Expand Down
2 changes: 2 additions & 0 deletions src/orsf_oop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
result.push_back(tree.get_leaf_pred_chaz(), "chaz");
result.push_back(tree.get_leaf_summary(), "mort");

delete tree.unique_event_times;

return(result);

}
Expand Down
3 changes: 2 additions & 1 deletion src/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@

vec beta(X.n_cols, fill::zeros);

mat hessian;
mat hessian(X.n_cols, X.n_cols);

for (uword iter = 0; iter < iter_max; ++iter) {

Expand Down Expand Up @@ -498,4 +498,5 @@

}


}
82 changes: 40 additions & 42 deletions tests/testthat/test-lincomb_linreg.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@

set.seed(123)

test_that(
desc = "linreg_fit with weights approximately equal to glm()",
code = {

nrows <- 1000
ncols <- 20

X <- matrix(data = rnorm(nrows*ncols), nrow = nrows, ncol = ncols)

# X <- cbind(1, X)

colnames(X) <- c(
# "intercept",
paste0("x", seq(ncols))
)

Y <- matrix(rnorm(nrows), ncol = 1)

glm_data <- as.data.frame(cbind(y=as.numeric(Y), X))

# Fit logistic regression using the custom function

W <- sample(1:3, nrow(X), replace=TRUE)

cpp = linreg_fit_exported(X, Y, W, do_scale = TRUE,
epsilon = 1e-9, iter_max = 20)

R = lm(y ~ ., weights = as.integer(W), data = glm_data)

R_summary <- summary(R)

R_beta_est <- as.numeric(R_summary$coefficients[-1, 'Estimate'])
R_beta_pvalues <- as.numeric(R_summary$coefficients[-1, 'Pr(>|t|)'])

expect_equal(cpp[,1], R_beta_est, tolerance = 1e-9)
expect_equal(cpp[,2], R_beta_pvalues, tolerance = 1e-9)


}
)
# test_that(
# desc = "linreg_fit with weights approximately equal to glm()",
# code = {
#
# nrows <- 1000
# ncols <- 20
#
# X <- matrix(data = rnorm(nrows*ncols), nrow = nrows, ncol = ncols)
#
# # X <- cbind(1, X)
#
# colnames(X) <- c(
# # "intercept",
# paste0("x", seq(ncols))
# )
#
# Y <- matrix(rnorm(nrows), ncol = 1)
#
# glm_data <- as.data.frame(cbind(y=as.numeric(Y), X))
#
# # Fit logistic regression using the custom function
#
# W <- sample(1:3, nrow(X), replace=TRUE)
#
# cpp = linreg_fit_exported(X, Y, W, do_scale = TRUE,
# epsilon = 1e-9, iter_max = 20)
#
# R = lm(y ~ ., weights = as.integer(W), data = glm_data)
#
# R_summary <- summary(R)
#
# R_beta_est <- as.numeric(R_summary$coefficients[-1, 'Estimate'])
# R_beta_pvalues <- as.numeric(R_summary$coefficients[-1, 'Pr(>|t|)'])
#
# expect_equal(cpp[,1], R_beta_est, tolerance = 1e-9)
# expect_equal(cpp[,2], R_beta_pvalues, tolerance = 1e-9)
#
#
# }
# )
96 changes: 47 additions & 49 deletions tests/testthat/test-lincomb_logreg.R
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@

set.seed(123)

test_that(
desc = "logreg_fit with weights approximately equal to glm()",
code = {

nrows <- 1000
ncols <- 20

X <- matrix(data = rnorm(nrows*ncols), nrow = nrows, ncol = ncols)

# X <- cbind(1, X)

colnames(X) <- c(
# "intercept",
paste0("x", seq(ncols))
)

Y <- matrix(rbinom(nrows, size = 1, prob = 0.7), ncol = 1)

glm_data <- as.data.frame(cbind(y=as.numeric(Y), X))

# Fit logistic regression using the custom function

W <- sample(1:3, nrow(X), replace=TRUE)

control <- glm.control()

cpp = logreg_fit_exported(X, Y, W, do_scale = T,
epsilon = control$epsilon,
iter_max = control$maxit)

R = glm(y ~ .,
weights = as.integer(W),
control = control,
data = glm_data,
family = 'binomial')

R_summary <- summary(R)

R_beta_est <- as.numeric(R_summary$coefficients[-1, 'Estimate'])
R_beta_pvalues <- as.numeric(R_summary$coefficients[-1, 'Pr(>|z|)'])

expect_equal(cpp[,1], R_beta_est, tolerance = 1e-3)
expect_equal(cpp[,2], R_beta_pvalues, tolerance = 1e-3)


}
)
# test_that(
# desc = "logreg_fit with weights approximately equal to glm()",
# code = {
#
# nrows <- 1000
# ncols <- 20
#
# X <- matrix(data = rnorm(nrows*ncols), nrow = nrows, ncol = ncols)
#
# # X <- cbind(1, X)
#
# colnames(X) <- c(
# # "intercept",
# paste0("x", seq(ncols))
# )
#
# Y <- matrix(rbinom(nrows, size = 1, prob = 0.7), ncol = 1)
#
# glm_data <- as.data.frame(cbind(y=as.numeric(Y), X))
#
# # Fit logistic regression using the custom function
#
# W <- sample(1:3, nrow(X), replace=TRUE)
#
# control <- glm.control()
#
# cpp = logreg_fit_exported(X, Y, W, do_scale = T,
# epsilon = control$epsilon,
# iter_max = control$maxit)
#
# R = glm(y ~ .,
# weights = as.integer(W),
# control = control,
# data = glm_data,
# family = 'binomial')
#
# R_summary <- summary(R)
#
# R_beta_est <- as.numeric(R_summary$coefficients[-1, 'Estimate'])
# R_beta_pvalues <- as.numeric(R_summary$coefficients[-1, 'Pr(>|z|)'])
#
# expect_equal(cpp[,1], R_beta_est, tolerance = 1e-3)
# expect_equal(cpp[,2], R_beta_pvalues, tolerance = 1e-3)
#
#
# }
# )

Loading

0 comments on commit 20df562

Please sign in to comment.