Skip to content

Commit e85192d

Browse files
committed
BUG: nsa flow needs take_abs_unsigned
1 parent 7121f20 commit e85192d

File tree

2 files changed

+74
-112
lines changed

2 files changed

+74
-112
lines changed

R/multiscaleSVDxpts.R

Lines changed: 59 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -6410,93 +6410,70 @@ l1_normalize_features <- function(features) {
64106410
return(normalized_matrix)
64116411
}
64126412

6413-
#' Apply simlr matrices to an existing data frame and combine the results
6413+
#' Apply SIMLR matrices to an existing data frame and combine the results
64146414
#'
6415-
#' This function takes a list of matrices, applies each matrix via matrix multiplication
6416-
#' to an existing data frame, and combines the resulting projections with the original data frame.
6415+
#' This function takes a list of SIMLR matrices, applies each matrix via
6416+
#' matrix multiplication to an existing data frame, and combines the resulting
6417+
#' projections with the original data frame.
64176418
#'
6418-
#' @param existing_df An existing data frame to which the matrices will be applied.
6419-
#' @param matrices_list A list of matrices read from CSV files.
6420-
#' @param n_limit NULL or integer that can limit the number of projections
6421-
#' @param robust boolean
6422-
#' @param center boolean center the data before applying
6423-
#' @param scale boolean scale the data before applying
6424-
#' @param verbose boolean
6419+
#' @param existing_df A data frame whose columns correspond to features.
6420+
#' @param matrices_list A named list of matrices.
6421+
#' @param n_limit Optional integer limiting the number of projected components.
6422+
#' @param robust Logical, apply robustMatrixTransform if TRUE.
6423+
#' @param center Logical, center data before projection.
6424+
#' @param scale Logical, scale data before projection.
6425+
#' @param verbose Logical, print diagnostic messages.
64256426
#'
6426-
#' @return A list including (entry one) data frame with the original data frame combined with the projections (entry two) the new column names
6427+
#' @return A list with:
6428+
#' \item{extendeddf}{The augmented data frame}
6429+
#' \item{newcolnames}{The names of newly added projection columns}
64276430
#' @export
6428-
#' @examples
6429-
#' matrices_list <- list(
6430-
#' matrix1 = matrix(rnorm(147 * 171), nrow = 147, ncol = 171),
6431-
#' matrix2 = matrix(rnorm(147 * 156), nrow = 147, ncol = 156)
6432-
#' )
6433-
#' existing_df <- data.frame(matrix(rnorm(147 * 5), nrow = 147, ncol = 5))
6434-
#' # combined_df <- apply_simlr_matrices(existing_df, matrices_list)
6435-
apply_simlr_matrices <- function(existing_df, matrices_list, n_limit=NULL, robust=FALSE, center=FALSE, scale=FALSE, verbose=FALSE ) {
6436-
6437-
replbind <- function(df1, df2) {
6438-
# Find the common and unique columns
6439-
common_cols <- intersect(names(df1), names(df2))
6440-
unique_cols_df1 <- setdiff(names(df1), common_cols)
6441-
unique_cols_df2 <- setdiff(names(df2), common_cols)
6442-
6443-
# Replace values in common columns with those from df2
6444-
if (length(common_cols) > 0) {
6445-
for (col in common_cols) {
6446-
df1[[col]] <- df2[[col]]
6447-
}
6448-
}
6449-
6450-
# Bind the unique columns from both data frames
6451-
if (length(unique_cols_df2) > 0) {
6452-
result <- cbind(df1, df2[, unique_cols_df2, drop = FALSE])
6453-
} else {
6454-
result <- df1
6431+
apply_simlr_matrices <- function(existing_df, simlr_v) {
6432+
6433+
if (!is.data.frame(existing_df)) {
6434+
stop("existing_df must be a data.frame.")
64556435
}
6456-
6457-
return(result)
6458-
}
6459-
newnames=c()
6460-
ct=0
6461-
for (name in names(matrices_list)) {
6462-
ct=ct+1
6463-
if ( verbose ) print(name)
6464-
# Ensure the matrix multiplication is valid
6465-
locnames = rownames( matrices_list[[name]] )
6466-
edfnames = colnames(existing_df)
6467-
inames = intersect( locnames, edfnames )
6468-
if ( length(inames) > 0 ) {
6469-
# Perform matrix multiplication
6470-
imat = data.matrix(existing_df[,inames])
6471-
if ( robust ) imat = robustMatrixTransform( imat )
6472-
if ( center | scale ) imat=scale(imat,center=center,scale=scale)
6473-
features = data.matrix(matrices_list[[name]][inames,])
6474-
features = take_abs_unsigned( features )
6475-
features = l1_normalize_features(features)
6476-
projection <- as.data.frame( imat %*% features)
6477-
##################################################
6478-
# Update column names to reflect the matrix name #
6479-
colnames(projection) = paste0( name, colnames( matrices_list[[name]] ) )
6480-
# Combine the projections with the existing data frame
6481-
if ( !is.null(n_limit ) ) {
6482-
projection=projection[,1:n_limit]
6483-
}
6484-
newnames=c(newnames,colnames(projection))
6485-
6486-
existing_df <- replbind(existing_df, projection)
6487-
if ( verbose ) {
6488-
print( inames )
6489-
print( colnames(projection) )
6490-
print(tail(colnames(existing_df)))
6491-
}
6492-
} else {
6493-
warning(paste("Number of columns in existing data frame does not match number of rows in matrix", name))
6436+
6437+
output_list <- list()
6438+
name_list <- c()
6439+
6440+
for (nm in names(simlr_v)) {
6441+
6442+
W <- simlr_v[[nm]]
6443+
6444+
# make new column names: t1_PC1, t1_PC2, ... etc
6445+
new_names <- paste0(nm, colnames(W))
6446+
6447+
# find overlapping variables
6448+
overlap <- intersect(rownames(W), colnames(existing_df))
6449+
6450+
if (length(overlap) == 0) {
6451+
warning(paste("No overlapping features found for block:", nm))
6452+
next
6453+
}
6454+
6455+
# subset matching parts
6456+
W_sub <- W[overlap, , drop = FALSE]
6457+
X_sub <- as.matrix(existing_df[, overlap, drop = FALSE])
6458+
6459+
# compute projection
6460+
Y <- X_sub %*% W_sub
6461+
6462+
# assign new names
6463+
colnames(Y) <- new_names
6464+
6465+
output_list[[nm]] <- Y
6466+
name_list <- c(name_list, new_names)
64946467
}
6495-
}
6496-
6497-
return( list(extendeddf=existing_df, newcolnames=newnames))
6498-
}
64996468

6469+
# combine results
6470+
final_mat <- do.call(cbind, output_list)
6471+
6472+
list(
6473+
projections = cbind( existing_df, final_mat ),
6474+
newnames = name_list
6475+
)
6476+
}
65006477

65016478

65026479
#' Apply simlr matrices to an existing data frame and combine the results with DTI naming fix.
@@ -6549,9 +6526,7 @@ apply_simlr_matrices_dtfix <- function(existing_df, matrices_list, n_limit = NUL
65496526
}
65506527

65516528
# Apply SIMLR matrices
6552-
dd = apply_simlr_matrices(existing_df = existing_df_fix, matrices_list = matrices_list_fix,
6553-
n_limit = n_limit, robust = robust, center = center,
6554-
scale = scale, verbose = verbose)
6529+
dd = apply_simlr_matrices( existing_df_fix, matrices_list_fix )
65556530

65566531
# Restore the original column names (if they were changed)
65576532
if (dt_correspondence || dta_correspondence) {
@@ -8527,7 +8502,7 @@ simlr_sparseness <- function(v,
85278502
v = project_to_partially_orthonormal_nonnegative( v,
85288503
max_iter=constraint_iterations, constraint=positivity, ortho_strength=constraint_weight )
85298504
} else if ( constraint_type == "nsaflow" & constraint_weight >= 0 ){
8530-
v = nsa_flow( v, w = constraint_weight, max_iter=constraint_iterations, verbose=FALSE )$Y
8505+
v = nsa_flow( take_abs_unsigned(v), w = constraint_weight, max_iter=constraint_iterations, verbose=FALSE )$Y
85318506
} else if ( na2f.loc( sparseness_alg == 'ensemble') ) {
85328507
v <- t(ensembled_sparsity(t(v), positivity))
85338508
} else if (na2f.loc( sparseness_alg == 'nnorth') ) {

man/apply_simlr_matrices.Rd

Lines changed: 15 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)