thomasp85 / tidygraph

A tidy API for graph manipulation
https://tidygraph.data-imaginist.com
Other
543 stars 61 forks source link

Default handling of edge weights #162

Closed luukvdmeer closed 1 year ago

luukvdmeer commented 1 year ago

When working with tidygraph I always struggle with the following. In igraph setting weights = NULL (the default) will always use an edge attribute named "weight" as edge weights, whenever this attribute is present. In tidygraph, weights = NULL always means that no weights are used, no matter if there is an edge attribute named "weight".

I understand both ideas. From the one hand, it is nice that you can create an edge attribute "weight" once and know this will be always used as edge weights in any function in which weights play a role, without always having to set explicitly weights = "weight" every time you call such a function. From the other hand, it may be more intuitive that weights = NULL means that no weights are used (in igraph you can do this instead with weights = NA).

However, now the situation is quite confusing, since the same setting (weights = NULL) means something completely different in tidygraph than it does in igraph. That means you have to know about this and always remember it when you are using an igraph function inside a tidygraph workflow, or vice versa. Speaking for myself, I can say this already got me wrong results a few times (without warning). Taken from the tidygraph introduction, the idea is that tidygraph and igraph objects and/or functions can be easily used together:

Underneath the hood of tidygraph lies the well-oiled machinery of igraph, ensuring efficient graph manipulation. Rather than keeping the node and edge data in a list and creating igraph objects on the fly when needed, tidygraph subclasses igraph with the tbl_graph class and simply exposes it in a tidy manner. This ensures that all your beloved algorithms that expects igraph objects still works with tbl_graph objects. Further, tidygraph is very careful not to override any of igraphs exports so the two packages can coexist quite happily.

Would it be an idea to stick to the igraph default regarding the default handling of edge weights? Hence, whenever your edges table has a column named "weight" these will be used as edge weights without having to set that explicitly at every function call. I know this is a backwards incompatible change, but it might save more confusion on the long run.

luukvdmeer commented 1 year ago

In some tidygraph functions it is actually also documented to work that way: https://tidygraph.data-imaginist.com/reference/local_graph.html

thomasp85 commented 1 year ago

I have always hated that some magic attribute name would completely change the computations in igraph, so I'm afraid I will not change this behaviour... The example you shared with local_ave_degree() also follows the rest of the tidygraph approach but the documentation is wrong - it is inherited from igraph but I'll change that to make sure it doesn't falsely state the behaviour of the "weight" attribute