Skip to content

Commit

Permalink
long-overdue tidy style reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmelville committed Dec 11, 2023
1 parent 07b8a5d commit ba85ba8
Show file tree
Hide file tree
Showing 24 changed files with 1,028 additions and 963 deletions.
62 changes: 30 additions & 32 deletions R/affinity.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ fuzzy_set_union <- function(X, set_op_mix_ratio = 1) {
XX <- X * Matrix::t(X)
if (set_op_mix_ratio == 0) {
Matrix::drop0(XX)
}
else if (set_op_mix_ratio == 1) {
} else if (set_op_mix_ratio == 1) {
Matrix::drop0(X + Matrix::t(X) - XX)
}
else {
} else {
Matrix::drop0(
set_op_mix_ratio * (X + Matrix::t(X) - XX) + (1 - set_op_mix_ratio) * XX
)
Expand All @@ -33,12 +31,12 @@ smooth_knn <- function(nn_dist,
}
tsmessage(
"Commencing smooth kNN distance calibration",
pluralize("thread", n_threads, " using"), appendLF = FALSE
pluralize("thread", n_threads, " using"),
appendLF = FALSE
)
if (length(target) == 1) {
tsmessage(" with target n_neighbors = ", formatC(2 ^ target), time_stamp = FALSE)
}
else {
tsmessage(" with target n_neighbors = ", formatC(2^target), time_stamp = FALSE)
} else {
tsmessage(time_stamp = FALSE)
}
affinity_matrix_res <- smooth_knn_distances_parallel(
Expand Down Expand Up @@ -87,8 +85,7 @@ smooth_knn_matrix <- function(nn,
target <- log2(n_nbrs + 1) * bandwidth
}
skip_first <- FALSE
}
else {
} else {
nnt <- nn_graph_t(nn)
n_nbrs <- nrow(nnt$dist)
if (is.null(target)) {
Expand All @@ -113,12 +110,13 @@ smooth_knn_matrix <- function(nn,
v <- affinity_matrix_res$matrix
if (is_sparse_matrix(nn)) {
# use j instead of i to transpose it
v <- Matrix::sparseMatrix(j = osparse$i, p = osparse$p, x = v,
dims = osparse$dims, index1 = FALSE)
v <- Matrix::sparseMatrix(
j = osparse$i, p = osparse$p, x = v,
dims = osparse$dims, index1 = FALSE
)
Matrix::diag(v) <- 0.0
v <- Matrix::drop0(v)
}
else {
} else {
v <- nng_to_sparse(nnt$idx, v, self_nbr = TRUE, by_row = FALSE)
}
affinity_matrix_res$matrix <- v
Expand All @@ -139,14 +137,16 @@ fuzzy_simplicial_set <- function(nn,
n_threads = NULL,
grain_size = 1,
verbose = FALSE) {
affinity_matrix_res <- smooth_knn_matrix(nn = nn,
target = target,
local_connectivity = local_connectivity,
bandwidth = bandwidth,
ret_sigma = ret_sigma,
n_threads = n_threads,
grain_size = grain_size,
verbose = verbose)
affinity_matrix_res <- smooth_knn_matrix(
nn = nn,
target = target,
local_connectivity = local_connectivity,
bandwidth = bandwidth,
ret_sigma = ret_sigma,
n_threads = n_threads,
grain_size = grain_size,
verbose = verbose
)
res <- fuzzy_set_union(affinity_matrix_res$matrix, set_op_mix_ratio = set_op_mix_ratio)
if (ret_sigma) {
res <- list(matrix = res)
Expand Down Expand Up @@ -214,8 +214,7 @@ perplexity_similarities <- function(nn, perplexity = NULL, ret_sigma = FALSE,
if (!is.null(affinity_matrix_res$sigma)) {
sigma <- affinity_matrix_res$sigma
}
}
else {
} else {
# knn kernel
tsmessage("Using knn graph for input weights with k = ", ncol(nn$idx))
# Make each row sum to 1, ignoring the self-index
Expand Down Expand Up @@ -248,14 +247,12 @@ nn_to_sparse <- function(nn_idxv, n_obs, val = 1, self_nbr = FALSE,

if (length(val) == 1) {
xs <- rep(val, n_obs * n_nbrs)
}
else {
} else {
xs <- val
}
if (by_row) {
is <- rep(1:n_obs, times = n_nbrs)
}
else {
} else {
is <- rep(1:n_obs, each = n_nbrs)
}

Expand All @@ -273,13 +270,14 @@ nng_to_sparse <- function(nn_idx, val = 1, self_nbr = FALSE,
max_nbr_id = NULL, by_row = TRUE) {
if (by_row) {
n_obs <- nrow(nn_idx)
}
else {
} else {
n_obs <- ncol(nn_idx)
}

nn_to_sparse(as.vector(nn_idx), n_obs, val = val, self_nbr = self_nbr,
max_nbr_id = max_nbr_id, by_row = by_row)
nn_to_sparse(as.vector(nn_idx), n_obs,
val = val, self_nbr = self_nbr,
max_nbr_id = max_nbr_id, by_row = by_row
)
}

# transpose the index and distance matrix
Expand Down
50 changes: 24 additions & 26 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
laplacian_eigenmap <- function(A, ndim = 2, verbose = FALSE, force_irlba = FALSE) {
if (rspectra_is_installed() && !force_irlba) {
coords <- rspectra_laplacian_eigenmap(A, ndim, verbose = verbose)
}
else {
} else {
coords <- irlba_laplacian_eigenmap(A, ndim, verbose = verbose)
}
coords
Expand Down Expand Up @@ -96,8 +95,7 @@ form_modified_laplacian <- function(A, ret_d = FALSE) {
Matrix::diag(L) <- 1 + Matrix::diag(L)
if (ret_d) {
list(L = L, Disqrt = 1 / Dsq)
}
else {
} else {
L
}
}
Expand All @@ -111,8 +109,7 @@ sort_eigenvectors <- function(eig_res, ndim) {
normalized_laplacian_init <- function(A, ndim = 2, verbose = FALSE, force_irlba = FALSE) {
if (rspectra_is_installed() && !force_irlba) {
coords <- rspectra_normalized_laplacian_init(A, ndim, verbose = verbose)
}
else {
} else {
coords <- irlba_normalized_laplacian_init(A, ndim, verbose = verbose)
}
coords
Expand Down Expand Up @@ -166,22 +163,24 @@ irlba_spectral_tsvd <- function(L, n, iters = 1000) {
}

irlba_eigs_asym <- function(L, ndim) {
suppressWarnings(res <- tryCatch({
res <- irlba::partial_eigen(
L,
n = ndim + 1,
symmetric = FALSE,
smallest = TRUE,
tol = 1e-3,
maxit = 1000,
verbose = TRUE
)
res$values <- sqrt(res$values)
res
},
error = function(c) {
NULL
}))
suppressWarnings(res <- tryCatch(
{
res <- irlba::partial_eigen(
L,
n = ndim + 1,
symmetric = FALSE,
smallest = TRUE,
tol = 1e-3,
maxit = 1000,
verbose = TRUE
)
res$values <- sqrt(res$values)
res
},
error = function(c) {
NULL
}
))
res
}

Expand Down Expand Up @@ -240,8 +239,7 @@ spectral_init <- function(A, ndim = 2, verbose = FALSE, force_irlba = FALSE) {
if (rspectra_is_installed() && !force_irlba) {
tsmessage("Initializing from normalized Laplacian + noise (using RSpectra)")
coords <- rspectra_normalized_laplacian_init(A, ndim, verbose = FALSE)
}
else {
} else {
tsmessage("Initializing from normalized Laplacian + noise (using irlba)")
coords <- irlba_tsvd_normalized_laplacian_init(A, ndim, verbose = FALSE)
}
Expand All @@ -262,7 +260,7 @@ irlba_spectral_init <- function(A, ndim = 2, verbose = FALSE) {
# Scales coords so that the largest absolute coordinate is 10.0 then jitters by
# adding gaussian noise with mean 0 and standard deviation sd
scale_and_jitter <- function(coords, max_coord = 10.0, sd = 0.0001) {
expansion <- 10.0 / max(abs(coords))
expansion <- 10.0 / max(abs(coords))
(coords * expansion) + matrix(stats::rnorm(n = prod(dim(coords)), sd = sd),
ncol = ncol(coords)
)
Expand Down Expand Up @@ -305,7 +303,7 @@ scale_coords <- function(X, sdev = 1e-4, verbose = FALSE) {
# Returns the score matrix unless ret_extra is TRUE, in which case a list
# is returned also containing the eigenvalues
pca_init <- function(X, ndim = min(dim(X)), center = TRUE, ret_extra = FALSE,
pca_method = "auto", verbose = FALSE) {
pca_method = "auto", verbose = FALSE) {
if (methods::is(X, "dist")) {
res_mds <- stats::cmdscale(X, x.ret = TRUE, eig = TRUE, k = ndim)

Expand Down
Loading

0 comments on commit ba85ba8

Please sign in to comment.