Skip to content

Commit

Permalink
refactor: tune performance
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Feb 13, 2021
1 parent 5f22e94 commit c7d8bcb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
25 changes: 17 additions & 8 deletions r_package/R/data-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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()
Expand All @@ -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))
}
Expand Down Expand Up @@ -185,24 +188,30 @@ 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
})
set_names = strsplit(dd$c_name, symbol, fixed = TRUE)
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"
)
Expand Down
31 changes: 18 additions & 13 deletions r_package/R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down Expand Up @@ -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,
Expand All @@ -346,7 +343,7 @@ fromDataFrame = function(upsetjs,
} else {
generateCombinationsImpl(
sorted_sets,
ifelse(is.null(c_type), 'distinctIntersection', c_type),
gen_type,
0,
NULL,
TRUE,
Expand All @@ -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,
Expand All @@ -369,7 +366,7 @@ fromDataFrame = function(upsetjs,
} else {
generateCombinationsImpl(
sorted_sets,
ifelse(is.null(c_type), 'intersection', c_type),
gen_type,
0,
NULL,
FALSE,
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion r_package/tests/testthat/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
20 changes: 10 additions & 10 deletions r_package/tests/testthat/test-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit c7d8bcb

Please sign in to comment.