Skip to content

Commit

Permalink
fix annoy search with non-multicore future plans
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewwbutler committed Sep 18, 2020
1 parent 02145bc commit 1392158
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ importFrom(cowplot,plot_grid)
importFrom(cowplot,theme_cowplot)
importFrom(fitdistrplus,fitdist)
importFrom(future,nbrOfWorkers)
importFrom(future,plan)
importFrom(future.apply,future_lapply)
importFrom(future.apply,future_sapply)
importFrom(ggplot2,Geom)
Expand Down
6 changes: 6 additions & 0 deletions R/clustering.R
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,18 @@ AnnoyBuildIndex <- function(data, metric = "euclidean", n.trees = 50) {
# @param search.k During the query it will inspect up to search_k nodes which
# gives you a run-time tradeoff between better accuracy and speed.
# @ param include.distance Include the corresponding distances
#' @importFrom future plan
#' @importFrom future.apply future_lapply
#
AnnoySearch <- function(index, query, k, search.k = -1, include.distance = TRUE) {
n <- nrow(x = query)
idx <- matrix(nrow = n, ncol = k)
dist <- matrix(nrow = n, ncol = k)
convert <- methods::is(index, "Rcpp_AnnoyAngular")
if (!inherits(x = plan(), what = "multicore")) {
oplan <- plan(strategy = "sequential")
on.exit(plan(oplan), add = TRUE)
}
res <- future_lapply(X = 1:n, FUN = function(x) {
res <- index$getNNsByVectorList(query[x, ], k, search.k, include.distance)
# Convert from Angular to Cosine distance
Expand Down

0 comments on commit 1392158

Please sign in to comment.