From c7d8bcb4029024d06a8a56922701fb10130fd077 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Sun, 14 Feb 2021 00:07:32 +0100 Subject: [PATCH] refactor: tune performance --- r_package/R/data-helpers.R | 25 +++++++++++++++------- r_package/R/data.R | 31 ++++++++++++++++------------ r_package/tests/testthat/helpers.R | 5 ++++- r_package/tests/testthat/test-data.R | 20 +++++++++--------- 4 files changed, 49 insertions(+), 32 deletions(-) diff --git a/r_package/R/data-helpers.R b/r_package/R/data-helpers.R index 0481fd9..f9baea1 100644 --- a/r_package/R/data-helpers.R +++ b/r_package/R/data-helpers.R @@ -75,7 +75,7 @@ generateCombinationsImpl = function(sets, cc = colorLookup(colors) mergeUnion = function(a, b) { - ab_sets = sort(union(a$setNames, b$setNames)) + ab_sets = union(a$setNames, b$setNames) ab_name = paste(ab_sets, collapse = symbol) ab_elems = c() if (a$cardinality == 0) { @@ -89,7 +89,7 @@ generateCombinationsImpl = function(sets, } mergeIntersect = function(a, b) { - ab_sets = sort(union(a$setNames, b$setNames)) + ab_sets = union(a$setNames, b$setNames) ab_name = paste(ab_sets, collapse = symbol) ab_elems = c() if (a$cardinality > 0 && b$cardinality > 0) { @@ -104,6 +104,9 @@ generateCombinationsImpl = function(sets, if (s$degree < min || (!is.null(max) && s$degree > max) || (s$cardinality == 0 && !empty)) { return() } + if (!store.elems) { + s <<- asCombination(s$name, c(), 'distinctIntersection', s$setNames, cardinality=s$cardinality, color=s$color) + } if (!distinct || s$degree == 1) { combinations <<- c(combinations, list(s)) return() @@ -125,7 +128,7 @@ generateCombinationsImpl = function(sets, return() } - sDistinct = asCombination(s$name, dElems, 'distinctIntersection', s$setNames, color=s$color) + sDistinct = asCombination(s$name, if (store.elems) { dElems } else { c() }, 'distinctIntersection', s$setNames, cardinality=length(dElems), color=s$color) if (sDistinct$cardinality > 0 || empty) { combinations <<- c(combinations, list(sDistinct)) } @@ -185,8 +188,14 @@ extractCombinationsImpl = function(df, elems = rownames(df) df_sets = df[, all_set_names] - c_name = apply(df_sets, 1, function(r) paste(all_set_names[as.logical(r)], collapse=symbol)) - + c_name = apply(df_sets, 1, function(r) { + nn = all_set_names[as.logical(r)] + if (length(nn) == 1) { + nn + } else { + paste(nn, collapse=symbol) + } + }) dd = aggregate(elems, list(c_name = c_name), function(r) { r }) @@ -194,15 +203,15 @@ extractCombinationsImpl = function(df, set_colors = cc(dd$c_name) combinations = lapply(1:nrow(dd), function(i) { - elems = as.character(unlist(dd[i, 'x'])) + elems = as.character(dd[i, 'x'][[1]]) structure( list( name = dd[i, 'c_name'], color = set_colors[i], type = 'distinctIntersection', - elems = elems, + elems = if (store.elems) elems else c(), cardinality = length(elems), - setNames = unlist(set_names[i]) + setNames = set_names[i][[1]] ), class = "upsetjs_combination" ) diff --git a/r_package/R/data.R b/r_package/R/data.R index e4c548b..5170bc7 100644 --- a/r_package/R/data.R +++ b/r_package/R/data.R @@ -111,33 +111,29 @@ fromList = function(upsetjs, } sorted_sets = sortSets(sets, order.by = order.by, limit = limit) - gen_sets = if (is.null(limit)) - sets - else - sorted_sets gen = if (!is.null(c_type) && c_type == "none") { list() } else if (isVennDiagram(upsetjs) || isKarnaughMap(upsetjs)) { generateCombinationsImpl( - gen_sets, + sorted_sets, ifelse(is.null(c_type), 'distinctIntersection', c_type), 0, NULL, TRUE, 'degree', - NULL, + limit, colors ) } else { generateCombinationsImpl( - gen_sets, + sorted_sets, ifelse(is.null(c_type), 'intersection', c_type), 0, NULL, FALSE, order.by, - NULL, + limit, colors ) } @@ -326,14 +322,15 @@ fromDataFrame = function(upsetjs, cc = colorLookup(colors) - sorted_sets = extractSetsFromDataFrame(df, attributes, order.by, limit, colors, store.elems = store.elems) + gen_type = ifelse(!is.null(c_type), c_type, ifelse(isVennDiagram(upsetjs) || isKarnaughMap(upsetjs), 'distinctIntersection', 'intersection')) + sorted_sets = extractSetsFromDataFrame(df, attributes, order.by, limit, colors, store.elems = store.elems || gen_type != 'distinctIntersection') elems = rownames(df) gen = if (!is.null(c_type) && c_type == "none") { list() } else if (isVennDiagram(upsetjs) || isKarnaughMap(upsetjs)) { - if (is.null(c_type) || c_type == 'distinctIntersection') { + if (gen_type == 'distinctIntersection') { extractCombinationsImpl( df, sorted_sets, @@ -346,7 +343,7 @@ fromDataFrame = function(upsetjs, } else { generateCombinationsImpl( sorted_sets, - ifelse(is.null(c_type), 'distinctIntersection', c_type), + gen_type, 0, NULL, TRUE, @@ -356,7 +353,7 @@ fromDataFrame = function(upsetjs, store.elems = store.elems ) } - } else if (!is.null(c_type) && c_type == 'distinctIntersection') { + } else if (gen_type == 'distinctIntersection') { extractCombinationsImpl( df, sorted_sets, @@ -369,7 +366,7 @@ fromDataFrame = function(upsetjs, } else { generateCombinationsImpl( sorted_sets, - ifelse(is.null(c_type), 'intersection', c_type), + gen_type, 0, NULL, FALSE, @@ -379,6 +376,14 @@ fromDataFrame = function(upsetjs, store.elems = store.elems ) } + + if (!store.elems && gen_type != 'distinctIntersection') { + # delete + for(i in 1:length(sorted_sets)) { + sorted_sets[[i]]$elems = c() + } + } + props = list( sets = sorted_sets, combinations = gen, diff --git a/r_package/tests/testthat/helpers.R b/r_package/tests/testthat/helpers.R index 34dde86..e035aa0 100644 --- a/r_package/tests/testthat/helpers.R +++ b/r_package/tests/testthat/helpers.R @@ -17,7 +17,10 @@ upsetjs_mock = function() { r } -expect_set = function(s, name, cardinality) { +expect_set = function(s, name, cardinality, check.length=TRUE) { expect_equal(s$name, name) expect_equal(s$cardinality, cardinality) + if (check.length) { + expect_equal(length(s$elems), cardinality) + } } \ No newline at end of file diff --git a/r_package/tests/testthat/test-data.R b/r_package/tests/testthat/test-data.R index 2ef6364..141540f 100644 --- a/r_package/tests/testthat/test-data.R +++ b/r_package/tests/testthat/test-data.R @@ -74,19 +74,19 @@ test_that("fromExpression", { u = upsetjs_mock() %>% fromExpression(expressionInput, order.by='degree') sets = u %>% getSets() expect_equal(length(sets), 3) - expect_set(sets[[1]], 'one', 9) - expect_set(sets[[2]], 'three', 9) - expect_set(sets[[3]], 'two', 5) + expect_set(sets[[1]], 'one', 9, check.length=FALSE) + expect_set(sets[[2]], 'three', 9, check.length=FALSE) + expect_set(sets[[3]], 'two', 5, check.length=FALSE) cc = u %>% getCombinations() expect_equal(length(cc), 7) - expect_set(cc[[1]], 'one', 9) - expect_set(cc[[2]], 'three', 9) - expect_set(cc[[3]], 'two', 5) - expect_set(cc[[4]], 'one&three', 6) - expect_set(cc[[5]], 'one&two', 3) - expect_set(cc[[6]], 'two&three', 3) - expect_set(cc[[7]], 'one&two&three', 2) + expect_set(cc[[1]], 'one', 9, check.length=FALSE) + expect_set(cc[[2]], 'three', 9, check.length=FALSE) + expect_set(cc[[3]], 'two', 5, check.length=FALSE) + expect_set(cc[[4]], 'one&three', 6, check.length=FALSE) + expect_set(cc[[5]], 'one&two', 3, check.length=FALSE) + expect_set(cc[[6]], 'two&three', 3, check.length=FALSE) + expect_set(cc[[7]], 'one&two&three', 2, check.length=FALSE) }) test_that("fromDataFrame", {