networkx-metis
networkx-metis copied to clipboard
Edge weights cannot be non integers
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!
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")
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.