networkx / networkx-metis

NetworkX Addon to allow graph partitioning with METIS
Other
79 stars 21 forks source link

Edge weights cannot be non integers #72

Open Xiuyu-Li opened 3 years ago

Xiuyu-Li commented 3 years ago

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!

dschult commented 3 years ago

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")
Xiuyu-Li commented 3 years ago

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.