Skip to content

Commit

Permalink
small bug corrected in network_knn.mc and network_knn
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyGelb committed Nov 7, 2023
1 parent 72c334a commit 808a2cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ This is an important release!

When using ESC-NKDE with bandwidths selection, negative density values could be obtained (with a small parameter of depth). It led to NAN values in LOO score. They are now treated like zeros.

A small bug has been corrected in the functions *network_knn* and *network_knn.mc* caused with grids with very few number of points.

# spNetwork 0.4.3.7

This is only a maintenance release. A bug in CRAN caused by multiprocessing in example caused the rejection of hte package.
Expand Down
27 changes: 17 additions & 10 deletions R/knn_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,22 @@ network_knn_worker <- function(points, lines, k, direction = NULL, use_dest = FA
fids <- fids[order(distsf)]
distsf <- distsf[order(distsf)]
n <- length(fids)
if(n < k){
fids <- c(fids, rep(NA,(k-n-1)))
distsf <- c(distsf, rep(NA,(k-n-1)))
}
if(n>k){
raiseWarning <<- TRUE
fids <- fids[1:k]
distsf <- distsf[1:k]
if(n == 0){
fids <- rep(NA,(k))
distsf <- rep(NA,(k))
}else{
if(n < k){
fids <- c(fids, rep(NA,(k-length(fids))))
distsf <- c(distsf, rep(NA,(k-length(distsf))))
}
if(n>k){
raiseWarning <<- TRUE
fids <- fids[1:k]
distsf <- distsf[1:k]
}
}


return(list(fids, distsf))
})

Expand Down Expand Up @@ -215,7 +221,7 @@ network_knn <- function(origins, lines, k, destinations = NULL, maxdistance = 0,
listvalues <- lapply(1:nrow(grid),function(i){
quadra <- grid[i,]
if(verbose){
print(paste("working on quadra : ",i,"/",length(grid),sep=""))
print(paste("working on quadra : ",i,"/",nrow(grid),sep=""))
}
elements <- list_elements[[i]]
if(length(elements)==0){
Expand All @@ -224,13 +230,14 @@ network_knn <- function(origins, lines, k, destinations = NULL, maxdistance = 0,
all_pts <- elements[[1]]
selected_lines <- elements[[2]]
#calculating the elements
values <- network_knn_worker(all_pts, selected_lines, k, direction=direction,
values <- network_knn_worker(points = all_pts, lines = selected_lines, k = k, direction=direction,
use_dest = use_dest,
verbose = verbose, digits = digits, tol=tol)
return(values)
}
})


## step8 combining the results in two global matrices
okvalues <- listvalues[lengths(listvalues) != 0]
matdists <- do.call(rbind, lapply(okvalues, function(l){
Expand Down

0 comments on commit 808a2cc

Please sign in to comment.