Skip to content

Ellipsoid Gate: Appearance != Performance #268

@gunthergl

Description

@gunthergl

A particular ellipsoid gate gives completely off counts.

To reproduce,

  • I create a grid of single cells
  • gate_ellipsoid is visually as intended but results in wrong frequencies/counts (31%)
  • gate_ellipsoid_2 and gate_rectangle seem to work fine (1.9%)

Expected behavior
gate_ellipsoid should have a similar percentage of cells as the other two gates.

Additional info

  • I checked that the covariance matrices are positive definite with eigen().
  • gate_ellipsoid_2 is similar, but not identical, so that is no workaround

Any idea what is happening?

example_expression <- expand.grid(
    x = seq(1, 1e4, length.out = 1e4 / 150),
    y = seq(1, 2e3, length.out = 2e3 / 20)
)
ff <- flowCore::flowFrame(exprs = as.matrix(example_expression))

my_covmat <- matrix(c(774761, -309675, -12387, 16687), nrow = 2)
colnames(my_covmat) <- c("x", "y")
rownames(my_covmat) <- c("x", "y")
eigen(my_covmat)
#> eigen() decomposition
#> $values
#> [1] 779787.79  11660.21
#> 
#> $vectors
#>            [,1]       [,2]
#> [1,]  0.9266082 0.01623032
#> [2,] -0.3760282 0.99986828


my_covmat_2 <- matrix(c(774761, -100000, -10000, 16687), nrow = 2)
colnames(my_covmat_2) <- c("x", "y")
rownames(my_covmat_2) <- c("x", "y")
eigen(my_covmat_2)
#> eigen() decomposition
#> $values
#> [1] 776077.84  15370.16
#> 
#> $vectors
#>            [,1]       [,2]
#> [1,]  0.9914408 0.01316731
#> [2,] -0.1305574 0.99991331

gate_ellipsoid <- flowCore::ellipsoidGate(mean = c(5000, 1000), cov = my_covmat)
gate_ellipsoid_2 <- flowCore::ellipsoidGate(mean = c(5000, 1000), cov = my_covmat_2)
gate_rectangle <- flowCore::rectangleGate("x" = c(4000, 6000), "y" = c(900, 1100))
plotlist <- lapply(list(gate_ellipsoid, gate_ellipsoid_2, gate_rectangle), function(gate_x) {
    flowViz::xyplot(
        y ~ x,
        data = ff,
        nrpoints = 10000,
        smooth = FALSE,
        stats = TRUE,
        filter = gate_x
    )
})
print(plotlist)
#> [[1]]

#> 
#> [[2]]

#> 
#> [[3]]

sessionInfo()
#> R version 4.3.3 (2024-02-29)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.6 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3;  LAPACK version 3.9.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Europe/Berlin
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] compiler_4.3.3      reprex_2.1.0.9000   Rcpp_1.0.12        
#>  [4] Biobase_2.60.0      cytolib_2.12.0      png_0.1-8          
#>  [7] yaml_2.3.8          fastmap_1.1.1       lattice_0.22-5     
#> [10] deldir_2.0-2        hexbin_1.28.3       RProtoBufLib_2.12.0
#> [13] latticeExtra_0.6-30 knitr_1.45          MASS_7.3-60        
#> [16] BiocGenerics_0.46.0 tibble_3.2.1        interp_1.1-6       
#> [19] R.cache_0.15.0      RColorBrewer_1.1-3  pillar_1.9.0       
#> [22] R.utils_2.11.0      rlang_1.1.3         utf8_1.2.4         
#> [25] flowCore_2.12.0     xfun_0.42           fs_1.6.3           
#> [28] IDPmisc_1.1.21      cli_3.6.2           withr_3.0.0        
#> [31] magrittr_2.0.3      digest_0.6.34       grid_4.3.3         
#> [34] lifecycle_1.0.4     R.methodsS3_1.8.1   R.oo_1.24.0        
#> [37] S4Vectors_0.38.1    vctrs_0.6.5         KernSmooth_2.23-22 
#> [40] evaluate_0.23       glue_1.7.0          flowViz_1.64.0     
#> [43] styler_1.7.0        stats4_4.3.3        fansi_1.0.6        
#> [46] rmarkdown_2.26      purrr_1.0.2         jpeg_0.1-10        
#> [49] matrixStats_1.2.0   tools_4.3.3         pkgconfig_2.0.3    
#> [52] htmltools_0.5.7

Created on 2024-03-13 with reprex v2.1.0.9000

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions