nateosher / DIMPLE

MIT License
2 stars 1 forks source link

Error caused by vectors of length 0 in update_qdist #109

Open ashtsang opened 1 month ago

ashtsang commented 1 month ago

Hi there,

Thanks for this work! I'm encountering the following error when running the update_qdist function and would greatly appreciate your input:

Progress interrupted by purrr_error_indexed condition: ℹ In index: 1. Caused by error in map_dbl(): ℹ In index: 1. Caused by error in jsd_checks(): ! vectors cannot be of length 0 Error in (function (.x, .f, ..., .progress = FALSE) : ℹ In index: 1. Caused by error in map_dbl(): ℹ In index: 1. Caused by error in jsd_checks(): ! vectors cannot be of length 0

Upon further inspection of the out_qfac output, it seems that the joined_q$qfac output only contains values of 2 and 3, but contains no values of quantile 1, which I suspect is the reason why this error occurs.

This is so far occurring for one of the samples in the experiment, which I have included here, as well as the code to reproduce the error.

sample10_issue.csv

library(DIMPLE)
library(tidyverse)

#####
## setup experiment and compute quantiles

tbl <- read.table('sample10_issue.csv', 
                  header=TRUE,sep=",", comment.char = "%", check.names=TRUE) 

experiment <- new_MltplxExperiment(x = tbl$Cell.X.Position, 
                                  y = tbl$Cell.Y.Position,
                                  marks = factor(tbl$Cell.Type),
                                  slide_id = tbl$Annotation.ID)

experiment <- update_intensity(experiment, 10, 30)

q_probs <- tibble(from=c(0,34,67),to=c(33,66,100))

# the error is raised at this call
experiment <- update_qdist(experiment,
                                  jsd,
                                  'Tumor',
                                  q_probs)

#####
## inspect issue with sample 10
mltplx_intensity <- experiment$mltplx_objects[[1]]$mltplx_intensity
mask_type <- "Tumor"

intensities <- mltplx_intensity$intensities %>%
  as.data.frame()

out_qfac <- calc_qfac(q_probs,intensities,mask_type)
joined_q <- out_qfac$joined_q
q <- out_qfac$q

imat <- joined_q %>%
  select(-c(X,Y,q1,q2,p1,p2,q_fac))

cell_types <- mltplx_intensity$cell_types
p <- length(cell_types)
K <- nrow(q_probs)
quantile_dist_array <- array(dim = c(p,p,K),dimnames = list(cell_types,cell_types,NULL))

## the issue might be here: idx is empty for k=1
for(k in 1:K) {
  idx <- joined_q$q_fac == k 
  print(sum(idx)) # idx is empty/sum is 0, which is what I suspect raises the "! vectors cannot be of length 0" error
  quantile_dist_array[,,k] <- MakeDistMat(imat[idx,], dist_metric)
}

Thank you for your help! Ashley

nateosher commented 1 month ago

Hi Ashley,

Thank you for reporting this! I'll try to take a look as soon as I can.