Support for adding weighted edges via graph/encoding/dot?

149 views
Skip to first unread message

Paul Capestany

unread,
Nov 24, 2019, 4:56:16 PM11/24/19
to gonum-dev
First time user of gonum here (awesome package btw!).. quick question: is it currently possible to import .dot graphviz network graphs in a way such that edges annotated with weight values in the .dot format are properly reflected in the final/generated gonum graph struct?

From what I can tell this feature doesn't seem to be implemented yet—just want to sanity check if that's correct? 

The use case I've got in mind is starting off with a (ideally) simple graph format ala .dot to import/export weighted directed multigraphs, analyzing/manipulating the graphs with gonum in-between. I noticed gonum already has support for incorporating labeled nodes (and arbitrary attributes) via dot-imported graphs, which is great—to me it looks like adding functionality to incorporate edge weights from .dot files could be very helpful for folks trying to quickly kick the tires of the graph pkgs. Thoughts/pointers/suggestions?


Dan Kortschak

unread,
Nov 25, 2019, 4:46:53 AM11/25/19
to Paul Capestany, gonum-dev
If you want to deserialise a weight attribute from DOT into the weight
of an edge, you need to make your edge type satisfy
graph/encoding.AttributeSetter and have the SetAttribute method on the
edge type check for the Key you want to use for weight (presumably
"weight") and use the parsed value of the Value to set the weight of
the edge.

There are too many ways to do this for us to choose the correct way for
every case, so we provide the hooks and let you do it the way you want.

Dan

Paul Capestany

unread,
Nov 25, 2019, 6:34:15 PM11/25/19
to gonum-dev
Thanks for your response Dan!

Following your suggestion I've been working off of the decode_test.go code as an example for using func (e *dotEdge) SetAttribute(attr encoding.Attribute) error for setting weight values. However, it's not clear to me how to go about basically forcing SetAttribute to get called on a "dotWeightedEdge" prior to SetEdge getting called. Seems like it should be trivial to add a weighted edge scenario to the test cases—I must be missing something which in retrospect will probably be obvious..

Dan Kortschak

unread,
Nov 25, 2019, 10:24:16 PM11/25/19
to Paul Capestany, gonum-dev
Here is an example (also at https://play.golang.org/p/AQeMZnzKGds).

Note that there is a caveat in the code that relates to
gonum/gonum#1183.

Dan

On Mon, 2019-11-25 at 15:34 -0800, Paul Capestany wrote:
> Thanks for your response Dan!
>
> Following your suggestion I've been working off of the
> decode_test.go
> <
> https://github.com/gonum/gonum/blob/master/graph/encoding/dot/decode_test.go#L598
> > code
> as an example for using func (e *dotEdge) SetAttribute(attr encoding.
> Attribute) error for setting weight values. However, it's not clear
> to me
> how to go about basically forcing SetAttribute to get called on a
> "dotWeightedEdge" *prior* to SetEdge getting called. Seems like it
weighted.go

Paul Capestany

unread,
Nov 26, 2019, 12:23:07 AM11/26/19
to gonum-dev
Welp, you made that look as easy I suspected you might Dan —*SUPER* appreciated!

I tend to use a couple Go packages like spew and/or q for easy pretty-printing, as well as Goland's debugging features to generally try to sort this kinda stuff out... but alas wasn't able to solve this puzzle on my own, so, again, many thanks for the help

I'll be keeping an eye on Issue #1183, and (once I have a firmer grasp on how you worked your magic) I'd be happy to add to the decode test cases (if it might make sense to). It's late in my timezone but it seems like I might need to brush up on my Go skills a bit more, as they might be a bit rusty :)

Dan Kortschak

unread,
Nov 26, 2019, 2:58:05 AM11/26/19
to Paul Capestany, gonum-dev
Yeah, structures help, but architecture is more than just static
structure.

Also, take a look at github.com/kortschak/utter, it's forked from go-
spew, but represents the data in something that is much closer to Go
syntax.

Dan

On Mon, 2019-11-25 at 21:23 -0800, Paul Capestany wrote:
> I tend to use a couple Go packages like spew
> <https://github.com/davecgh/go-spew> and/or q
> <https://github.com/y0ssar1an/q> for easy pretty-printing, as well
Reply all
Reply to author
Forward
0 new messages