-
-
Notifications
You must be signed in to change notification settings - Fork 206
feat: Add attribute combination support to graph operators #2676
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
Open
schochastics
wants to merge
22
commits into
main
Choose a base branch
from
feat-attrib_comb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c75e174
Add attribute combination support to graph operators (union, intersec…
schochastics 41c4b0c
Merge branch 'main' into feat-attrib_comb
krlmlr 5f7521a
make document work locally
schochastics cb49b91
Merge branch 'main' into feat-attrib_comb
schochastics d7e8bdc
chore: Auto-update from GitHub Actions
schochastics c26ab02
Merge branch 'main' into feat-attrib_comb
schochastics f9ad5b3
refactor: snake_case graph-operator attr-comb args, extract rename he…
schochastics b58da44
feat: snake_case *.attr.comb arguments and options, soft-deprecate do…
schochastics 04ab715
chore: Auto-update from GitHub Actions
schochastics 6d94668
Add `graph_attr_comb` igraph option to control default graph attribut…
schochastics 5bebc2e
Merge remote-tracking branch 'origin/main' into feat-attrib_comb
schochastics 05336ba
chore: Auto-update from GitHub Actions
schochastics a85a78c
rename *_comb to *_combine
schochastics d1ac089
chore: Auto-update from GitHub Actions
schochastics 96e21e2
Sort known_names alphabetically and reorder known_codes accordingly i…
schochastics 874f8eb
Add documentation for attribute combination codes and use integer lit…
schochastics eb89372
Reorder condition check in attribute combination to test for "rename"…
schochastics fde3bed
add example to rename
schochastics fbf2bcc
Update testthat minimum version to >= 3.3.0 and use expect_all_true
schochastics 9a635d7
Merge remote-tracking branch 'origin/main' into feat-attrib_comb
schochastics 409b310
chore: Auto-update from GitHub Actions
schochastics a8987c4
Merge branch 'main' into feat-attrib_comb
schochastics File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,7 @@ Suggests: | |
| scales, | ||
| stats4, | ||
| tcltk, | ||
| testthat, | ||
| testthat (>= 3.3.0), | ||
| vdiffr, | ||
| withr | ||
| Enhances: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1290,7 +1290,7 @@ is_bipartite <- function(graph) { | |
|
|
||
| ############# | ||
|
|
||
| igraph.i.attribute.combination <- function(comb) { | ||
| igraph.i.attribute.combination <- function(comb, allow_rename = FALSE) { | ||
| if (is.function(comb)) { | ||
| comb <- list(comb) | ||
| } | ||
|
|
@@ -1310,34 +1310,45 @@ igraph.i.attribute.combination <- function(comb) { | |
| if (anyDuplicated(names(comb)) > 0) { | ||
| cli::cli_warn("Some attributes are duplicated") | ||
| } | ||
| # `known_codes` are the numeric values of the `igraph_attribute_combination_type_t` | ||
| # enum in the C library (see src/vendor/cigraph/include/igraph_attributes.h). | ||
| # Each code must stay aligned with its name in `known_names`. The DEFAULT (1) and | ||
| # FUNCTION (2) enum values are intentionally absent: FUNCTION is handled by the | ||
| # `!is.character(x)` branch below, and DEFAULT is not selectable by name. | ||
| known_names <- c( | ||
| "concat", | ||
| "first", | ||
| "ignore", | ||
| "last", | ||
| "max", | ||
| "mean", | ||
| "median", | ||
| "min", | ||
| "prod", | ||
| "random", | ||
| "sum" | ||
| ) | ||
| known_codes <- c(12L, 8L, 0L, 9L, 6L, 10L, 11L, 5L, 4L, 7L, 3L) | ||
| if (allow_rename) { | ||
| known_names <- c(known_names, "rename") | ||
| known_codes <- c(known_codes, NA_integer_) | ||
| } | ||
| comb <- lapply(comb, function(x) { | ||
| if (!is.character(x)) { | ||
| x | ||
| } else { | ||
| known <- data.frame( | ||
| n = c( | ||
| "ignore", | ||
| "sum", | ||
| "prod", | ||
| "min", | ||
| "max", | ||
| "random", | ||
| "first", | ||
| "last", | ||
| "mean", | ||
| "median", | ||
| "concat" | ||
| ), | ||
| i = c(0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), | ||
| stringsAsFactors = FALSE | ||
| ) | ||
| x <- pmatch(tolower(x), known[, 1]) | ||
| if (is.na(x)) { | ||
| idx <- pmatch(tolower(x), known_names) | ||
| if (is.na(idx)) { | ||
| if (identical(tolower(x), "rename") && !allow_rename) { | ||
| cli::cli_abort( | ||
| "{.val rename} is only supported by graph operators ({.fn union}, {.fn intersection}, {.fn compose}, {.fn disjoint_union}), not by this function." | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rephrase to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This gets surprisingly complicated so I would opt out of that and keep it as is |
||
| ) | ||
| } | ||
| cli::cli_abort( | ||
| "Unknown/unambigous attribute combination specification." | ||
| ) | ||
| } | ||
| known[, 2][x] | ||
| if (is.na(known_codes[idx])) "rename" else known_codes[idx] | ||
| } | ||
| }) | ||
|
|
||
|
|
@@ -1353,7 +1364,7 @@ igraph.i.attribute.combination <- function(comb) { | |
| #' vertex/edge attributes in these cases. | ||
| #' | ||
| #' The functions that support the combination of attributes have one or two | ||
| #' extra arguments called `vertex.attr.comb` and/or `edge.attr.comb` | ||
| #' extra arguments called `vertex_attr_combine` and/or `edge_attr_combine` | ||
|
schochastics marked this conversation as resolved.
|
||
| #' that specify how to perform the mapping of the attributes. E.g. | ||
| #' [contract()] contracts many vertices into a single one, the | ||
| #' attributes of the vertices can be combined and stores as the vertex | ||
|
|
@@ -1435,6 +1446,18 @@ igraph.i.attribute.combination <- function(comb) { | |
| #' Concatenate the attributes, using the [c()] function. | ||
| #' This results almost always a complex attribute. | ||
| #' } | ||
| #' \item{"rename"}{ | ||
| #' Keep clashing attributes side-by-side under disambiguated names by | ||
| #' appending `_1`, `_2`, ... suffixes. For example, if two graphs each | ||
| #' have an attribute called `group`, the resulting graph will have | ||
| #' attributes `group_1` and `group_2`, corresponding to the first and | ||
| #' second input graph, respectively. This is the default for the | ||
| #' graph operators [union()], [intersection()], [compose()] and | ||
| #' [disjoint_union()] and preserves their historical behaviour. | ||
| #' Only those operators accept `"rename"`; [simplify()] and | ||
| #' [contract()] will reject it because the rename strategy has no | ||
|
schochastics marked this conversation as resolved.
|
||
| #' per-element interpretation when many input values collapse into one. | ||
| #' } | ||
| #' } | ||
| #' @author Gabor Csardi \email{csardi.gabor@@gmail.com} | ||
| #' @seealso [graph_attr()], [vertex_attr()], | ||
|
|
@@ -1452,22 +1475,22 @@ igraph.i.attribute.combination <- function(comb) { | |
| #' igraph_options(print.edge.attributes = TRUE) | ||
| #' | ||
| #' ## new attribute is the sum of the old ones | ||
| #' simplify(g, edge.attr.comb = "sum") | ||
| #' simplify(g, edge_attr_combine = "sum") | ||
| #' | ||
| #' ## collect attributes into a string | ||
| #' simplify(g, edge.attr.comb = toString) | ||
| #' simplify(g, edge_attr_combine = toString) | ||
| #' | ||
| #' ## concatenate them into a vector, this creates a complex | ||
| #' ## attribute | ||
| #' simplify(g, edge.attr.comb = "concat") | ||
| #' simplify(g, edge_attr_combine = "concat") | ||
| #' | ||
| #' E(g)$name <- letters[seq_len(ecount(g))] | ||
| #' | ||
| #' ## both attributes are collected into strings | ||
| #' simplify(g, edge.attr.comb = toString) | ||
| #' simplify(g, edge_attr_combine = toString) | ||
| #' | ||
| #' ## harmonic average of weights, names are dropped | ||
| #' simplify(g, edge.attr.comb = list( | ||
| #' simplify(g, edge_attr_combine = list( | ||
| #' weight = function(x) length(x) / sum(1 / x), | ||
| #' name = "ignore" | ||
| #' )) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.