Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectorize to increase speed of Counter(). #199

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jdblischak
Copy link

Thanks for the great package. My colleagues and I find UpSet plots very useful.

For one of our larger data sets, we observed that upset() takes ~5 seconds to complete. After profiling, we found that the following lines contribute the large majority of this compute time (~3 seconds).

UpSetR/R/MainBar.R

Lines 29 to 31 in df2151f

for(i in 1:nrow(Freqs)){
Freqs$degree[i] <- rowSums(Freqs[ i ,1:num_sets])
}

In this PR, I converted the above to:

Freqs$degree <- rowSums(Freqs[, 1:num_sets])

This reduced the runtime of upset() to ~2 seconds.

The results should be identical, but to test I ran the following example code from ?upset:

movies <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"),
                   header=TRUE, sep=";" )
upset(movies, nsets = 7, nintersects = 30, mb.ratio = c(0.5, 0.5),
      order.by = c("freq", "degree"), decreasing = c(TRUE,FALSE))

Before:

image

After:

image

@jdblischak
Copy link
Author

In one of our analyses with 18 sets, we identified another bottleneck in Counter():

UpSetR/R/MainBar.R

Lines 50 to 53 in b14854a

for( i in 1:nrow(Freqs)){
Freqs$x[i] <- i
Freqs$color <- mbar_color
}

Vectorizing the above to:

  Freqs$x <- 1:nrow(Freqs)
  Freqs$color <- mbar_color

decreased the compute time from over 5 minutes to ~5 seconds for our use case.

And for consistency, I made the same change to specific_intersections().

@jdblischak jdblischak changed the title Vectorize rowSums() to increase speed of Counter(). Vectorize to increase speed of Counter(). Feb 9, 2021
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.

1 participant