prabhakarlab / Banksy

BANKSY: spatial clustering
https://prabhakarlab.github.io/Banksy
Other
74 stars 12 forks source link

Error in `[.data.table`(knn_df, , abs(gcm[, to, drop = FALSE] %*% (weight * : negative length vectors are not allowed Calls: computeBanksy ... <Anonymous> -> computeHarmonics -> [ -> [.data.table #20

Closed WangGuixiangCoder closed 8 months ago

WangGuixiangCoder commented 8 months ago

Error occurred when using computeHarmonics()

computeHarmonics <- function(gcm, knn_df, M, center, verbose) {
    from <- to <- weight <- phi <- .N <- count <- . <- NULL
    j <- sqrt(as.complex(-1))

    mean_k <- round(mean(knn_df[, .(count = .N), by = from]$count), 1)

    if (verbose) message("Computing harmonic m = ", M)
    if (verbose) message("Using ", mean_k, " neighbors")
    if (center) {
        if (verbose) message("Centering")
        aggr <- knn_df[, abs(
            fscale(gcm[, to, drop = FALSE]) %*% (weight * exp(j * M * phi))
        ), by = from]
    } else {
        aggr <- knn_df[, abs(
            gcm[, to, drop = FALSE] %*% (weight * exp(j * M * phi))
        ), by = from]
    }
    ncm <- matrix(aggr$V1, nrow = nrow(gcm), ncol = ncol(gcm))
    rownames(ncm) <- rownames(gcm)
    colnames(ncm) <- colnames(gcm)
    if (verbose) message("Done")

    return(ncm)
}

the data

> knn_df

         from    to     weight      phi
        <int> <int>      <num>    <num>
     1:     1 18565 0.12945572 1.570796
     2:     2  7472 0.12945572 4.712389
     3:     3 42788 0.11031116 1.570796
     4:     4  5922 0.12945572 4.712389
     5:     5  8740 0.12945572 0.000000
    ---                                
723251: 48213 33495 0.01751993 4.248741
723252: 48214 17838 0.02875326 5.497787
723253: 48215  7858 0.03925429 2.034444
723254: 48216 27058 0.01887885 5.497787
723255: 48217  5530 0.03834914 5.176037
> dim(gcm)

[1] 50061 48217

> gcm[1:4, 1:5]

         196_408 180_482 141_483 160_222 309_425
A1BG           1       2       1       3       1
A1BG-AS1       0       0       0       0       0
A1CF           0       0       0       0       0
A2M            3       0       0      10       4

>  from <- to <- weight <- phi <- .N <- count <- . <- NULL
    j <- sqrt(as.complex(-1))

> center

[1] FALSE
> M

[1] 0
> aggr <- knn_df[, abs(
            gcm[, to, drop = FALSE] %*% (weight * exp(j * M * phi))
        ), by = from]

Error in [.data.table(knn_df, , abs(gcm[, to, drop = FALSE] %% (weight : negative length vectors are not allowed

WangGuixiangCoder commented 8 months ago

When reducing the number of rows in the data, the error no longer occurs.

> gcm_1 = gcm[1:20000,]
> out <- computeHarmonics(gcm_1, knn_df, M, center, verbose = verbose)
jleechung commented 8 months ago

Hi @WangGuixiangCoder, thanks for raising this issue! This overflow error happens due to the large number of cells and features. To mitigate this, we recommend doing feature selection before computing the BANKSY matrices. We will add functionality to support large feature sets in the near future.

WangGuixiangCoder commented 8 months ago

Thanks!