Segmentation Error After Sub-Setting Pixels

38 views
Skip to first unread message

Raven Buckman

unread,
Aug 7, 2024, 6:33:19 PM8/7/24
to Cardinal MSI Help
We are utilizing SSC to first separate background and tissue pixels in our MSI data. After subsetting the pixels into "leaf" and background ("bkgd"), an error message appears when we attempt to use SSC to perform segmentation on the isolated pixels (see below).

> zeroA_ssc <- spatialShrunkenCentroids(zeroA, r = 1, s=0,k=2, weights = "gaussian")
calculating gaussian weights
calculating global centroid
initializing clusters with spatial k-means
fitting k = 2, s = 0
clustering iteration 1: 477 cluster assignments updated (96.02% complete)
clustering iteration 2: 72 cluster assignments updated (99.4% complete)
returning shrunken centroids clustering
> image(zeroA_ssc)      
> leaf<-subsetPixels(zeroA, zeroA_ssc@model[["class"]]==2)
> leaf_ssc <- spatialShrunkenCentroids(leaf, r=1, s=2, k=8, weights = "gaussian")
calculating gaussian weights
calculating global centroid
initializing clusters with spatial k-means
fitting k = 8, s = 2
clustering iteration 1: 1216 cluster assignments updated (79.04% complete)
clustering iteration 2: 626 cluster assignments updated (89.21% complete)
clustering iteration 3: 453 cluster assignments updated (92.19% complete)
clustering iteration 4: 324 cluster assignments updated (94.42% complete)
clustering iteration 5: 268 cluster assignments updated (95.38% complete)
clustering iteration 6: 199 cluster assignments updated (96.57% complete)
clustering iteration 7: 175 cluster assignments updated (96.98% complete)
clustering iteration 8: 146 cluster assignments updated (97.48% complete)
clustering iteration 9: 149 cluster assignments updated (97.43% complete)
clustering iteration 10: 156 cluster assignments updated (97.31% complete)
returning shrunken centroids clustering
> image(leaf_ssc)
> bkgd<-subsetPixels(zeroA, zeroA_ssc@model[["class"]]==1)
> bkgd_ssc <- spatialShrunkenCentroids(bkgd, r=1, s=2, k=8, weights = "gaussian")
calculating gaussian weights
calculating global centroid
initializing clusters with spatial k-means
fitting k = 8, s = 2
Error in h(simpleError(msg, call)) :
  error in evaluating the argument 'x' in selecting a method for function 't': 'x' must be an array of at least two dimensions

> zeroA
MSImagingExperiment with 885 features and 11980 spectra
spectraData(1): intensity
featureData(3): mz, count, freq
pixelData(3): x, y, run
coord(2): x = 1...126, y = 1...118
runNames(1): 0A
metadata(1): processing
experimentData(14): spectrumType, spectrumRepresentation, contactName, ..., scanType, lineScanDirection, pixelSize
mass range:  104.1075 to 1167.5282
centroided: TRUE
> leaf
MSImagingExperiment with 885 features and 5802 spectra
spectraData(1): intensity
featureData(3): mz, count, freq
pixelData(3): x, y, run
coord(2): x = 2...107, y = 13...106
runNames(1): 0A
metadata(1): processing
experimentData(14): spectrumType, spectrumRepresentation, contactName, ..., scanType, lineScanDirection, pixelSize
mass range:  104.1075 to 1167.5282
centroided: TRUE
> bkgd
MSImagingExperiment with 885 features and 6178 spectra
spectraData(1): intensity
featureData(3): mz, count, freq
pixelData(3): x, y, run
coord(2): x = 1...126, y = 1...118
runNames(1): 0A
metadata(1): processing
experimentData(14): spectrumType, spectrumRepresentation, contactName, ..., scanType, lineScanDirection, pixelSize
mass range:  104.1075 to 1167.5282
centroided: TRUE 

This error message occurs only for the first segmentation class (regardless of the pixel locations). If I repeat the segmentation to separate the background from the tissue (highlighted in green), and the tissue segment is "1" then the error occurs and I can segment the background pixels with SSC no problem. 

I cannot seem to track down the true source of the error. Any guidance or other considerations would be greatly appreciated.

Sincerely, 
Raven

kbemis

unread,
Aug 9, 2024, 12:46:29 PM8/9/24
to Cardinal MSI Help
Can you paste the output of traceback() and sessionInfo() after the error?

Thanks.

-Kylie

Raven Buckman

unread,
Aug 9, 2024, 12:48:40 PM8/9/24
to Cardinal MSI Help
> bkgd<-subsetPixels(zeroA, zeroA_ssc@model[["class"]]==1)
> bkgd_ssc <- spatialShrunkenCentroids(bkgd, r=1, s=2, k=8, weights = "gaussian")
calculating gaussian weights
calculating global centroid
initializing clusters with spatial k-means
fitting k = 8, s = 2
Error in h(simpleError(msg, call)) :
  error in evaluating the argument 'x' in selecting a method for function 't': 'x' must be an array of at least two dimensions
> traceback()
25: h(simpleError(msg, call))
24: .handleSimpleError(function (cond)
    .Internal(C_tryCatchHelper(addr, 1L, cond)), "'x' must be an array of at least two dimensions",
        base::quote(base::colSums(x, na.rm = na.rm, dims = dims,
            ...)))
23: stop("'x' must be an array of at least two dimensions")
22: base::colSums(x, na.rm = na.rm, dims = dims, ...)
21: colSums(wi * d[nbi, ])
20: colSums(wi * d[nbi, ])
19: (function (nbi, wi)
    colSums(wi * d[nbi, ])/sum(wi))(dots[[1L]][[2487L]], dots[[2L]][[2487L]])
18: mapply(FUN = FUN, ...)
17: eval(mc, env)
16: eval(mc, env)
15: eval(mc, env)
14: standardGeneric("mapply")
13: mapply(FUN, neighbors, wts)
12: t(mapply(FUN, neighbors, wts))
11: fx(seq_len(k))
10: nscentroids(x, y = y, s = s, distfun = distfun, priors = priors,
        center = center, transpose = transpose, neighbors = neighbors,
        neighbor.weights = wts, nchunks = nchunks, verbose = verbose,
        BPPARAM = BPPARAM, ...)
9: .local(x, y, ...)
8: spatialShrunkenCentroids(x, y = y, r = r, s = si, priors = 1,
       center = center, transpose = transpose, neighbors = neighbors,
       weights = wts, nchunks = nchunks, verbose = FALSE, BPPARAM = BPPARAM,
       ...)
7: spatialShrunkenCentroids(x, y = y, r = r, s = si, priors = 1,
       center = center, transpose = transpose, neighbors = neighbors,
       weights = wts, nchunks = nchunks, verbose = FALSE, BPPARAM = BPPARAM,
       ...)
6: .local(x, ...)
5: spatialShrunkenCentroids(spectra(x), coord = coord(x), r = r,
       k = k, s = s, neighbors = neighbors, weights = weights, transpose = TRUE,
       ...)
4: spatialShrunkenCentroids(spectra(x), coord = coord(x), r = r,
       k = k, s = s, neighbors = neighbors, weights = weights, transpose = TRUE,
       ...)
3: .local(x, ...)
2: spatialShrunkenCentroids(bkgd, r = 1, s = 2, k = 8, weights = "gaussian")
1: spatialShrunkenCentroids(bkgd, r = 1, s = 2, k = 8, weights = "gaussian")
> sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: AlmaLinux 9.4 (Seafoam Ocelot)

Matrix products: default
BLAS/LAPACK: /opt/rit/el9/20240413/app/linux-rhel9-x86_64_v3/gcc-11.3.1/openblas-0.3.26-vludctyfew6nfydjx3a3anpnpdey42df/lib/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0

locale:
 [1] LC_CTYPE=C.utf8       LC_NUMERIC=C          LC_TIME=C.utf8        LC_COLLATE=C.utf8    
 [5] LC_MONETARY=C.utf8    LC_MESSAGES=C.utf8    LC_PAPER=C.utf8       LC_NAME=C            
 [9] LC_ADDRESS=C          LC_TELEPHONE=C        LC_MEASUREMENT=C.utf8 LC_IDENTIFICATION=C  

time zone: UTC
tzcode source: internal

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] Cardinal_3.6.4      S4Vectors_0.42.1    BiocParallel_1.38.0 BiocGenerics_0.50.0
[5] ProtGenerics_1.36.0 BiocManager_1.30.23

loaded via a namespace (and not attached):
 [1] nlme_3.1-165       cli_3.6.3          rlang_1.1.4        DBI_1.2.3          tiff_0.1-12      
 [6] png_0.1-8          RCurl_1.98-1.16    htmltools_0.5.8.1  ontologyIndex_2.12 Biobase_2.64.0    
[11] locfit_1.5-9.10    grid_4.4.0         abind_1.4-5        bitops_1.0-8       fastmap_1.2.0    
[16] compiler_4.4.0     codetools_0.2-20   CardinalIO_1.2.1   irlba_2.3.5.1      htmlwidgets_1.6.4
[21] fftwtools_0.9-11   EBImage_4.46.0     lattice_0.22-6     digest_0.6.36      matter_2.6.3      
[26] parallel_4.4.0     Matrix_1.7-0       tools_4.4.0        jpeg_0.1-10        biglm_0.9-3   

kbemis

unread,
Aug 9, 2024, 1:27:53 PM8/9/24
to Cardinal MSI Help
Thanks! The error seems to be caused by pixels without any neighboring pixels. (It can also be consistently replicated by setting r=0.)

I've pushed a fix in Cardinal v3.6.5. It should propagate to the Bioconductor servers in a couple days, but you can install it right away with:

if (!require("remotes", quietly = TRUE))
    install.packages("remotes")
remotes::install_github("kuwisdelu/Cardinal", ref=remotes::github_release())


-Kylie

Raven Buckman

unread,
Aug 9, 2024, 1:39:47 PM8/9/24
to Cardinal MSI Help
Thanks so much! 
Reply all
Reply to author
Forward
0 new messages