Skip to content

Commit

Permalink
Code formatting changes according to styler
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmelville committed Aug 19, 2018
1 parent 1bc9edf commit ac57db1
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 86 deletions.
30 changes: 19 additions & 11 deletions R/affinity.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ fuzzy_set_union <- function(X, set_op_mix_ratio = 1) {
}
else {
Matrix::drop0(
set_op_mix_ratio * (X + Matrix::t(X) - XX) + (1 - set_op_mix_ratio) * XX)
set_op_mix_ratio * (X + Matrix::t(X) - XX) + (1 - set_op_mix_ratio) * XX
)
}
}

# Abstracts over whether the smooth knn distances uses the multithreaded code
# or not
smooth_knn <- function(nn,
local_connectivity = 1.0, bandwidth = 1.0,
n_threads = max(1,
RcppParallel::defaultNumThreads() / 2),
n_threads = max(
1,
RcppParallel::defaultNumThreads() / 2
),
grain_size = 1,
verbose = FALSE) {
tsmessage(
Expand Down Expand Up @@ -52,9 +55,11 @@ smooth_knn <- function(nn,
fuzzy_simplicial_set <- function(nn,
set_op_mix_ratio = 1.0,
local_connectivity = 1.0, bandwidth = 1.0,
n_threads =
max(1,
RcppParallel::defaultNumThreads() / 2),
n_threads =
max(
1,
RcppParallel::defaultNumThreads() / 2
),
grain_size = 1,
verbose = FALSE) {
affinity_matrix <- smooth_knn(nn,
Expand All @@ -77,9 +82,11 @@ symmetrize <- function(P) {
}

perplexity_similarities <- function(nn, perplexity = NULL,
n_threads =
max(1,
RcppParallel::defaultNumThreads() / 2),
n_threads =
max(
1,
RcppParallel::defaultNumThreads() / 2
),
grain_size = 1,
kernel = "gauss",
verbose = FALSE) {
Expand Down Expand Up @@ -118,8 +125,9 @@ perplexity_similarities <- function(nn, perplexity = NULL,
# edge has a weight of val (scalar or vector)
# return a sparse matrix with dimensions of nrow(nn_idx) x max_nbr_id
nn_to_sparse <- function(nn_idx, val = 1, byrow = FALSE, self_nbr = FALSE,
max_nbr_id = ifelse(self_nbr,
nrow(nn_idx), max(nn_idx))) {
max_nbr_id = ifelse(self_nbr,
nrow(nn_idx), max(nn_idx)
)) {
nd <- nrow(nn_idx)
k <- ncol(nn_idx)

Expand Down
12 changes: 8 additions & 4 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ laplacian_eigenmap <- function(A, ndim = 2, verbose = FALSE) {
)

if (is.null(eig_res) || ncol(eig_res$vectors) < ndim + 1) {
message("Laplacian Eigenmap failed to converge, ",
"using random initialization instead")
message(
"Laplacian Eigenmap failed to converge, ",
"using random initialization instead"
)
return(rand_init(nrow(A), ndim))
}
vecs <- eig_res$vectors[, 2:(ndim + 1)]
Expand Down Expand Up @@ -66,8 +68,10 @@ normalized_laplacian_init <- function(A, ndim = 2, verbose = FALSE) {
}
)
if (is.null(res) || ncol(res$vectors) < ndim) {
message("Spectral initialization failed to converge, ",
"using random initialization instead")
message(
"Spectral initialization failed to converge, ",
"using random initialization instead"
)
return(rand_init(n, ndim))
}
vec_indices <- rev(order(res$values, decreasing = TRUE)[1:ndim])
Expand Down
6 changes: 4 additions & 2 deletions R/neighbors.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ sparse_nn <- function(X, k, include_self = TRUE) {
is_nonzero <- dists != 0
dist_nonzero <- dists[is_nonzero]
if (length(dist_nonzero) < k) {
stop("Row ", i, " of distance matrix has only ", length(dist_nonzero),
" defined distances")
stop(
"Row ", i, " of distance matrix has only ", length(dist_nonzero),
" defined distances"
)
}

k_order <- order(dist_nonzero)[1:k]
Expand Down
8 changes: 4 additions & 4 deletions R/supervised.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
# Return The resulting intersected fuzzy simplicial set.
categorical_simplicial_set_intersection <- function(
simplicial_set, target,
unknown_dist = 1.0,
far_dist = 5.0,
unknown_dist = 1.0,
far_dist = 5.0,
verbose = FALSE) {

# Convert to dgTMatrix to get to the j indices
Expand Down Expand Up @@ -113,11 +113,11 @@ general_sset_intersection <- function(indptr1,

if (left_val > left_min || right_val > right_min) {
if (mix_weight < 0.5) {
result_val[idx] <- left_val *
result_val[idx] <- left_val *
right_val^(mix_weight / (1.0 - mix_weight))
}
else {
result_val[idx] <- right_val *
result_val[idx] <- right_val *
left_val^(((1.0 - mix_weight) / mix_weight))
}
}
Expand Down
4 changes: 2 additions & 2 deletions R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ umap_transform <- function(X, model,
init_weighted = TRUE,
search_k = NULL,
n_epochs = NULL,
n_threads =
n_threads =
max(1, RcppParallel::defaultNumThreads() / 2),
grain_size = 1,
verbose = FALSE) {
Expand Down Expand Up @@ -181,7 +181,7 @@ umap_transform <- function(X, model,
}

init_new_embedding <- function(train_embedding, nn, graph, weighted = TRUE,
n_threads =
n_threads =
max(1, RcppParallel::defaultNumThreads() / 2),
grain_size = 1, verbose = FALSE) {
parallelize <- n_threads > 0
Expand Down
20 changes: 12 additions & 8 deletions R/uwot.R
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ tumap <- function(X, n_neighbors = 15, n_components = 2, metric = "euclidean",
uwot(
X = X, n_neighbors = n_neighbors, n_components = n_components,
metric = metric,
n_epochs = n_epochs, alpha = alpha, scale = scale, init = init,
n_epochs = n_epochs, alpha = alpha, scale = scale, init = init,
spread = NULL, min_dist = NULL, set_op_mix_ratio = set_op_mix_ratio,
local_connectivity = local_connectivity, bandwidth = bandwidth,
gamma = gamma, negative_sample_rate = negative_sample_rate,
Expand Down Expand Up @@ -536,7 +536,7 @@ lvish <- function(X, perplexity = 50, n_neighbors = perplexity * 3,
uwot(X,
n_neighbors = n_neighbors, n_components = n_components,
metric = metric,
n_epochs = n_epochs, alpha = alpha, scale = scale, init = init,
n_epochs = n_epochs, alpha = alpha, scale = scale, init = init,
gamma = gamma, negative_sample_rate = negative_sample_rate,
nn_method = nn_method, n_trees = n_trees, search_k = search_k,
method = "largevis", perplexity = perplexity,
Expand All @@ -548,15 +548,15 @@ lvish <- function(X, perplexity = 50, n_neighbors = perplexity * 3,
# Function that does all the real work
uwot <- function(X, n_neighbors = 15, n_components = 2, metric = "euclidean",
n_epochs = NULL,
alpha = 1, scale = FALSE, init = "spectral",
alpha = 1, scale = FALSE, init = "spectral",
spread = 1, min_dist = 0.01,
set_op_mix_ratio = 1.0, local_connectivity = 1.0,
bandwidth = 1.0, gamma = 1.0,
negative_sample_rate = 5.0, a = NULL, b = NULL,
nn_method = NULL, n_trees = 50,
search_k = 2 * n_neighbors * n_trees,
method = "umap", perplexity = 50, approx_pow = FALSE,
y = NULL, target_n_neighbors = n_neighbors,
y = NULL, target_n_neighbors = n_neighbors,
target_weight = 0.5,
n_threads = max(1, RcppParallel::defaultNumThreads() / 2),
kernel = "gauss",
Expand Down Expand Up @@ -651,8 +651,10 @@ uwot <- function(X, n_neighbors = 15, n_components = 2, metric = "euclidean",
}
nn_method <- match.arg(tolower(nn_method), c("annoy", "fnn"))
if (nn_method == "fnn" && metric != "euclidean") {
stop("nn_method = 'FNN' is only compatible with distance metric ",
"'euclidean'")
stop(
"nn_method = 'FNN' is only compatible with distance metric ",
"'euclidean'"
)
}
if (nn_method == "fnn" && ret_model) {
stop("nn_method = 'FNN' is incompatible with ret_model = TRUE")
Expand Down Expand Up @@ -949,8 +951,10 @@ scale_input <- function(X, scale_type, ret_model = FALSE, verbose = FALSE) {
scale_type <- ifelse(scale_type, "scale", "none")
}

scale_type <- match.arg(tolower(scale_type),
c("none", "scale", "range", "maxabs"))
scale_type <- match.arg(
tolower(scale_type),
c("none", "scale", "range", "maxabs")
)
switch(scale_type,
range = {
tsmessage("Range scaling X")
Expand Down
60 changes: 35 additions & 25 deletions tests/testthat/helper_fuzzy_sets.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

# Asymmetric fuzzy set data
V_asymm <- sparseMatrix(
i = c(5, 6, 8, 3, 9, 10, 2, 4, 7, 9, 10, 2, 3, 7, 9, 10, 1, 6, 8, 3, 5, 1,
5, 6, 7, 4, 1, 2, 4, 8),
j = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 7, 7, 8,
8, 8, 8, 9, 10, 10, 10, 10),
i = c(
5, 6, 8, 3, 9, 10, 2, 4, 7, 9, 10, 2, 3, 7, 9, 10, 1, 6, 8, 3, 5, 1,
5, 6, 7, 4, 1, 2, 4, 8
),
j = c(
1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 7, 7, 8,
8, 8, 8, 9, 10, 10, 10, 10
),
x = c(
1, 1, 1, 0.328, 0.4252, 1, 0.5386, 1, 1, 0.5748, 0.5, 0.4614, 1, 0.6463,
1, 0.5, 1, 1, 0.6894, 0.672, 0.2807, 0.8381, 0.7193, 5.129e-10, 0.3538,
1, 0.5, 1, 1, 0.6894, 0.672, 0.2807, 0.8381, 0.7193, 5.129e-10, 0.3538,
0.5447, 0.1619, 1, 0.4553, 0.3106
)
)
Expand All @@ -25,9 +29,9 @@ V_union <- sparseMatrix(
7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 10
),
x = c(
1, 1, 1, 0.1619, 0.6899, 0.4614, 0.4252, 1, 0.6899, 1, 1, 0.5748, 0.5,
1, 1, 1, 0.1619, 0.6899, 0.4614, 0.4252, 1, 0.6899, 1, 1, 0.5748, 0.5,
0.4614, 1, 0.6463, 1, 0.7277, 1, 1, 0.2807, 0.9128, 1, 1, 5.129e-10, 1,
0.6463, 0.2807, 0.3538, 1, 0.9128, 5.129e-10, 0.3538, 0.3106, 0.4252,
0.6463, 0.2807, 0.3538, 1, 0.9128, 5.129e-10, 0.3538, 0.3106, 0.4252,
0.5748, 1, 0.1619, 1, 0.5, 0.7277, 0.3106
)
)
Expand All @@ -43,10 +47,10 @@ V_mix <- sparseMatrix(
7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 10
),
x = c(
1, 0.5, 0.919, 0.08095, 0.4333, 0.2307, 0.2126, 1, 0.4333, 1, 0.836,
0.2874, 0.25, 0.2307, 1, 0.3231, 0.7723, 0.4777, 1, 0.5, 0.1404, 0.7043,
0.5, 0.5, 2.564e-10, 0.836, 0.3231, 0.1404, 0.1769, 0.919, 0.7043,
2.564e-10, 0.1769, 0.1553, 0.2126, 0.2874, 0.7723, 0.08095, 1, 0.25,
1, 0.5, 0.919, 0.08095, 0.4333, 0.2307, 0.2126, 1, 0.4333, 1, 0.836,
0.2874, 0.25, 0.2307, 1, 0.3231, 0.7723, 0.4777, 1, 0.5, 0.1404, 0.7043,
0.5, 0.5, 2.564e-10, 0.836, 0.3231, 0.1404, 0.1769, 0.919, 0.7043,
2.564e-10, 0.1769, 0.1553, 0.2126, 0.2874, 0.7723, 0.08095, 1, 0.25,
0.4777, 0.1553
)
)
Expand All @@ -63,10 +67,14 @@ V_intersect <- sparseMatrix(

# asymm with local connectivity = 1.5
V_asymm_local <- sparseMatrix(
i = c(5, 6, 8, 3, 9, 10, 2, 4, 7, 9, 10, 2, 3, 7, 9, 10, 1, 6, 8, 3, 5, 1,
5, 6, 7, 4, 1, 2, 4, 8),
j = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 7, 7, 8, 8,
8, 8, 9, 10, 10, 10, 10),
i = c(
5, 6, 8, 3, 9, 10, 2, 4, 7, 9, 10, 2, 3, 7, 9, 10, 1, 6, 8, 3, 5, 1,
5, 6, 7, 4, 1, 2, 4, 8
),
j = c(
1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 7, 7, 8, 8,
8, 8, 9, 10, 10, 10, 10
),
x = c(
1, 1, 1, 0.2559, 0.3748, 1, 0.5698, 1, 1, 0.6252, 0.5, 0.4302, 1, 0.7157,
1, 0.5, 1, 1, 0.7622, 0.7441, 0.2084, 0.8925, 0.7916, 5.129e-10, 0.2843,
Expand All @@ -76,17 +84,17 @@ V_asymm_local <- sparseMatrix(

V_union_local <- sparseMatrix(
i = c(
5, 6, 8, 10, 3, 4, 9, 10, 2, 4, 7, 9, 10, 2, 3, 7, 9, 10, 1, 6, 7, 8, 1,
5, 6, 8, 10, 3, 4, 9, 10, 2, 4, 7, 9, 10, 2, 3, 7, 9, 10, 1, 6, 7, 8, 1,
5, 8, 3, 4, 5, 8, 1, 5, 6, 7, 10, 2, 3, 4, 1, 2, 3, 4, 8
),
j = c(
1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6,
1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6,
6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 10
),
x = c(
1, 1, 1, 0.1075, 0.6799, 0.4302, 0.3748, 1, 0.6799, 1, 1, 0.6252, 0.5,
0.4302, 1, 0.7157, 1, 0.7102, 1, 1, 0.2084, 0.9504, 1, 1, 5.129e-10, 1,
0.7157, 0.2084, 0.2843, 1, 0.9504, 5.129e-10, 0.2843, 0.2378, 0.3748,
1, 1, 1, 0.1075, 0.6799, 0.4302, 0.3748, 1, 0.6799, 1, 1, 0.6252, 0.5,
0.4302, 1, 0.7157, 1, 0.7102, 1, 1, 0.2084, 0.9504, 1, 1, 5.129e-10, 1,
0.7157, 0.2084, 0.2843, 1, 0.9504, 5.129e-10, 0.2843, 0.2378, 0.3748,
0.6252, 1, 0.1075, 1, 0.5, 0.7102, 0.2378
)
)
Expand All @@ -101,16 +109,18 @@ V_union_bandwidth <- sparseMatrix(
7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 10
),
x = c(
1, 1, 1, 0.02621, 0.3664, 0.2129, 0.1808, 1, 0.3664, 1, 1, 0.3304, 0.25,
0.2129, 1, 0.4176, 1, 0.4055, 1, 1, 0.07881, 0.7467, 1, 1, 2.63e-19, 1,
0.4176, 0.07881, 0.1251, 1, 0.7467, 2.63e-19, 0.1251, 0.09647, 0.1808,
1, 1, 1, 0.02621, 0.3664, 0.2129, 0.1808, 1, 0.3664, 1, 1, 0.3304, 0.25,
0.2129, 1, 0.4176, 1, 0.4055, 1, 1, 0.07881, 0.7467, 1, 1, 2.63e-19, 1,
0.4176, 0.07881, 0.1251, 1, 0.7467, 2.63e-19, 0.1251, 0.09647, 0.1808,
0.3304, 1, 0.02621, 1, 0.25, 0.4055, 0.09647
)
)

V_intersect_local_bandwidth <- sparseMatrix(
i = c(5, 8, 3, 10, 2, 4, 7, 3, 9, 10, 1, 8, 3, 1, 5, 4, 2, 4),
j = c(1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 7, 8, 8, 9, 10, 10),
x = c(1, 0.7966, 0.02126, 1, 0.02126, 1, 0.5536, 1, 0.336, 0.04417, 1, 0.364,
0.5536, 0.7966, 0.364, 0.336, 1, 0.04417)
x = c(
1, 0.7966, 0.02126, 1, 0.02126, 1, 0.5536, 1, 0.336, 0.04417, 1, 0.364,
0.5536, 0.7966, 0.364, 0.336, 1, 0.04417
)
)
6 changes: 4 additions & 2 deletions tests/testthat/test_knn_aff.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ expected_sym_nn_graph[10, c(1, 2, 3, 4, 8)] <- c(o6, o3, o6, o3, o6)

expect_equal(sum(res), 10)
expect_true(Matrix::isSymmetric(res))
expect_equal(as.matrix(res), expected_sym_nn_graph, check.attributes = FALSE,
tol = 1e-7)
expect_equal(as.matrix(res), expected_sym_nn_graph,
check.attributes = FALSE,
tol = 1e-7
)
12 changes: 8 additions & 4 deletions tests/testthat/test_neighbors.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ res <- annoy_nn(ui10, k = 4, include_self = TRUE, n_threads = 0)
expect_equal(res$idx, self_nn_index4, check.attributes = FALSE)
expect_equal(res$dist, self_nn_dist4, check.attributes = FALSE, tol = 1e-6)

res <- annoy_nn(ui10, k = 4, include_self = FALSE, n_threads = 0,
ret_index = TRUE)
res <- annoy_nn(ui10,
k = 4, include_self = FALSE, n_threads = 0,
ret_index = TRUE
)
expect_equal(res$idx, nn_index4, check.attributes = FALSE)
expect_equal(res$dist, nn_dist4, check.attributes = FALSE, tol = 1e-6)
expect_true(!is.null(res$index))
Expand All @@ -126,8 +128,10 @@ res <- annoy_nn(ui10, k = 4, include_self = TRUE, n_threads = 1)
expect_equal(res$idx, self_nn_index4, check.attributes = FALSE)
expect_equal(res$dist, self_nn_dist4, check.attributes = FALSE, tol = 1e-6)

res <- annoy_nn(ui10, k = 4, include_self = FALSE, n_threads = 1,
ret_index = TRUE)
res <- annoy_nn(ui10,
k = 4, include_self = FALSE, n_threads = 1,
ret_index = TRUE
)
expect_equal(res$idx, nn_index4, check.attributes = FALSE)
expect_equal(res$dist, nn_dist4, check.attributes = FALSE, tol = 1e-6)
expect_true(!is.null(res$index))
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ expect_ok_matrix(res)

# UMAP and cosine metric n_threads = 1 issue #5
res <- umap(iris10,
n_neighbors = 4, n_epochs = 2, alpha = 0.5, metric = "cosine",
init = "spectral", verbose = FALSE, n_threads = 1
n_neighbors = 4, n_epochs = 2, alpha = 0.5, metric = "cosine",
init = "spectral", verbose = FALSE, n_threads = 1
)
expect_ok_matrix(res)

# metric = Manhattan
res <- umap(iris10,
n_neighbors = 4, n_epochs = 2, alpha = 0.5, metric = "manhattan",
init = "rand", verbose = FALSE, n_threads = 0
n_neighbors = 4, n_epochs = 2, alpha = 0.5, metric = "manhattan",
init = "rand", verbose = FALSE, n_threads = 0
)
expect_ok_matrix(res)
res <- umap(iris10,
n_neighbors = 4, n_epochs = 2, alpha = 0.5, metric = "manhattan",
init = "spca", verbose = FALSE, n_threads = 1
n_neighbors = 4, n_epochs = 2, alpha = 0.5, metric = "manhattan",
init = "spca", verbose = FALSE, n_threads = 1
)
expect_ok_matrix(res)

Expand Down
Loading

0 comments on commit ac57db1

Please sign in to comment.