networkx-metis icon indicating copy to clipboard operation
networkx-metis copied to clipboard

Edge weights cannot be non integers

Open Xiuyu-Li opened this issue 4 years ago • 2 comments

It seems like the nxmetis.partition cannot run on graph with edge weights that are not integers. Is there any suggestion on how to deal with this issue? Thank you!

Xiuyu-Li avatar Oct 11 '20 20:10 Xiuyu-Li

Typically, you figure out how many decimal places of accuracy you really want (for example 5). Then you multiply by 10^5 and round to create integer values. The integer values are large, but they allow the process to work. Any weight related results you compute should change the units back to your original units by dividing by 10^5.

nx.set_edge_attributes(G, {e: round(wt * 1e5) for e, wt in nx.get_edge_attributes(G, "weight").items()}, "int_weight")

dschult avatar Oct 11 '20 21:10 dschult

Typically, you figure out how many decimal places of accuracy you really want (for example 5). Then you multiply by 10^5 and round to create integer values. The integer values are large, but they allow the process to work. Any weight related results you compute should change the units back to your original units by dividing by 10^5.

nx.set_edge_attributes(G, {e: round(wt * 1e5) for e, wt in nx.get_edge_attributes(G, "weight").items()}, "int_weight")

Thanks for the advice! This could work with a tiny modification: need to use int(round(wt * 1e5)) instead of round(wt * 1e5) to make sure that the weights are integers.

Xiuyu-Li avatar Oct 12 '20 19:10 Xiuyu-Li