Skip to content

Handle all-zero features in density calculation#38

Open
foertsch wants to merge 2 commits into
powellgenomicslab:masterfrom
foertsch:fix-zero-density-handling
Open

Handle all-zero features in density calculation#38
foertsch wants to merge 2 commits into
powellgenomicslab:masterfrom
foertsch:fix-zero-density-handling

Conversation

@foertsch

Copy link
Copy Markdown

Fixes #20

This PR makes plot_density() handle all-zero feature weights safely.

Summary

  • guard calculate_density() against sum(w) <= 0
  • return zero densities instead of propagating invalid values
  • add regression tests for all-zero inputs

Why

When the selected feature has all-zero weights, the density calculation currently divides by sum(w), which becomes zero and produces invalid values downstream.

This can cause plot_density() to fail for zero-expression features instead of returning a valid empty/zero-density plot.

What changed

  • if sum(w) <= 0 and map = TRUE, return a zero vector of length nrow(x)
  • if sum(w) <= 0 and map = FALSE, return a zero-valued density object compatible with the existing downstream code
  • add tests covering both calculate_density() and plot_density() on an all-zero feature

Verification

Tested locally with:

testthat::test_file("tests/testthat/test-zero-density.R")
testthat::test_file("tests/testthat/test-plot_density.R")

Also verified directly with a synthetic all-zero metadata feature on SeuratObject::pbmc_small, where plot_density() now returns a ggplot.

Related issue

This likely also addresses issue #29 when the reported Empty raster error is caused by a zero-valued feature, but I have not reproduced the exact original object from that report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make function density_plot to work smoothly with genes that have only zero read counts

1 participant