Skip to content

Commit

Permalink
no mem leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nowosad committed Jan 7, 2025
1 parent 0076023 commit f2456fb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions R/aoa.R
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ aoa <- function(newdata,
maxLPD = maxLPD) {
if (method == "L2") {
# Euclidean Distance
return(FNN::knnx.dist(reference, point, k = maxLPD))
return(FNN::knnx.dist(reference, point, k = maxLPD, algorithm = "brute"))
} else if (method == "MD") {
return(t(sapply(1:dim(point)[1],
function(y)
Expand All @@ -470,7 +470,7 @@ aoa <- function(newdata,
maxLPD = maxLPD) {
if (method == "L2") {
# Euclidean Distance
return(FNN::knnx.index(reference, point, k = maxLPD))
return(FNN::knnx.index(reference, point, k = maxLPD, algorithm = "brute"))
} else if (method == "MD") {
stop("MD currently not implemented for LPD")
}
Expand Down
8 changes: 4 additions & 4 deletions R/geodist.R
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ sample2sample <- function(x, type,variables,time_unit,timevar, catVars){
for (i in 1:nrow(x_clean)){

if(is.null(catVars)) {
trainDist <- FNN::knnx.dist(x_clean[i,],x_clean,k=1)
trainDist <- FNN::knnx.dist(x_clean[i,],x_clean,k=1, algorithm = "brute")
} else {
trainDist <- gower::gower_dist(x_clean[i,],x_clean)
}
Expand Down Expand Up @@ -378,7 +378,7 @@ sample2prediction = function(x, modeldomain, type, samplesize,variables,time_uni
for (i in 1:nrow(modeldomain)){

if(is.null(catVars)) {
trainDist <- FNN::knnx.dist(modeldomain[i,],x_clean,k=1)
trainDist <- FNN::knnx.dist(modeldomain[i,],x_clean,k=1, algorithm = "brute")
} else {
trainDist <- gower::gower_dist(modeldomain[i,], x_clean)
}
Expand Down Expand Up @@ -460,7 +460,7 @@ sample2test <- function(x, testdata, type,variables,time_unit,timevar, catVars){
for (i in 1:nrow(testdata)){

if(is.null(catVars)) {
testDist <- FNN::knnx.dist(testdata[i,],x_clean,k=1)
testDist <- FNN::knnx.dist(testdata[i,],x_clean,k=1, algorithm = "brute")
} else {
testDist <- gower::gower_dist(testdata[i,], x_clean)
}
Expand Down Expand Up @@ -550,7 +550,7 @@ cvdistance <- function(x, cvfolds, cvtrain, type, variables,time_unit,timevar, c
for (k in 1:nrow(testdata_i)){

if(is.null(catVars)) {
trainDist <- tryCatch(FNN::knnx.dist(testdata_i[k,],traindata_i,k=1),
trainDist <- tryCatch(FNN::knnx.dist(testdata_i[k,],traindata_i,k=1, algorithm = "brute"),
error = function(e)e)
if(inherits(trainDist, "error")){
trainDist <- NA
Expand Down
10 changes: 5 additions & 5 deletions R/knndm.R
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ knndm_geo <- function(tpoints, predpoints, k, maxp, clustering, linkf, islonglat
units(Gij) <- NULL
Gij <- apply(Gij, 1, min)
}else{
Gj <- c(FNN::knn.dist(tcoords, k = 1))
Gj <- c(FNN::knn.dist(tcoords, k = 1, algorithm = "brute"))
Gij <- c(FNN::knnx.dist(query = sf::st_coordinates(predpoints)[,1:2],
data = tcoords, k = 1))
data = tcoords, k = 1, algorithm = "brute"))
}

# Check if Gj > Gij (warning suppressed regarding ties)
Expand Down Expand Up @@ -576,8 +576,8 @@ knndm_feature <- function(tpoints, predpoints, k, maxp, clustering, linkf, catVa

} else {
# use FNN with Euclidean distances if no categorical variables are present
Gj <- c(FNN::knn.dist(tpoints, k = 1))
Gij <- c(FNN::knnx.dist(query = predpoints, data = tpoints, k = 1))
Gj <- c(FNN::knn.dist(tpoints, k = 1, algorithm = "brute"))
Gij <- c(FNN::knnx.dist(query = predpoints, data = tpoints, k = 1, algorithm = "brute"))
}


Expand Down Expand Up @@ -793,7 +793,7 @@ distclust_euclidean <- function(tr_coords, folds){
alldist <- rep(NA, length(folds))
for(f in unique(folds)){
alldist[f == folds] <- c(FNN::knnx.dist(query = tr_coords[f == folds,,drop=FALSE],
data = tr_coords[f != folds,,drop=FALSE], k = 1))
data = tr_coords[f != folds,,drop=FALSE], k = 1, algorithm = "brute"))
}
alldist
}
Expand Down
2 changes: 1 addition & 1 deletion R/nndm.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ nndm <- function(tpoints, modeldomain = NULL, predpoints = NULL,
if(is.null(catVars)) {

# Euclidean distances if no categorical variables are present
Gij <- c(FNN::knnx.dist(query = predpoints, data = tpoints, k = 1))
Gij <- c(FNN::knnx.dist(query = predpoints, data = tpoints, k = 1, algorithm = "brute"))
tdist <- as.matrix(stats::dist(tpoints, upper = TRUE))
diag(tdist) <- NA
Gj <- apply(tdist, 1, function(x) min(x, na.rm=TRUE))
Expand Down
6 changes: 3 additions & 3 deletions R/trainDI.R
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ aoa_get_variables <- function(variables, model, train){
.mindistfun <- function(point, reference, method, S_inv=NULL){

if (method == "L2"){ # Euclidean Distance
return(c(FNN::knnx.dist(reference, point, k = 1)))
return(c(FNN::knnx.dist(reference, point, k = 1, algorithm = "brute")))
} else if (method == "MD"){ # Mahalanobis Distance
return(sapply(1:dim(point)[1],
function(y) min(sapply(1:dim(reference)[1],
Expand All @@ -533,9 +533,9 @@ aoa_get_variables <- function(variables, model, train){

if (method == "L2"){ # Euclidean Distance
if(sorted){
return(FNN::knnx.dist(reference, point, k = dim(reference)[1]))
return(FNN::knnx.dist(reference, point, k = dim(reference)[1], algorithm = "brute"))
} else {
return(FNN::knnx.dist(point,reference,k=1))
return(FNN::knnx.dist(point,reference,k=1, algorithm = "brute"))
}
} else if (method == "MD"){ # Mahalanobis Distance
if(sorted){
Expand Down

0 comments on commit f2456fb

Please sign in to comment.