daft-dev / daft

Render probabilistic graphical models using matplotlib
https://docs.daft-pgm.org
MIT License
675 stars 120 forks source link

runtime warning for add-edge #154

Open flyaflya opened 2 years ago

flyaflya commented 2 years ago

For the following code:

pgm = daft.PGM(dpi = 150, alternate_style="outer")

pgm.add_node("sb","Start\nBalance", 1, 4, aspect = 2.2, observed = True, plot_params = {'facecolor': 'cadetblue'})
pgm.add_node("ba","Bet\nAmount", 1, 3, aspect = 2.2, shape = "rectangle", plot_params = {'facecolor': 'thistle'})

pgm.add_edge("sb", "ba")
pgm.render()

I get a runtime warning error message:

/usr/local/lib/python3.7/dist-packages/daft.py:843: RuntimeWarning: divide by zero encountered in double_scalars
  * (np.sign(dy) or 1.0)
<matplotlib.axes._axes.Axes at 0x7f2085d4d190>

image

The graph renders just fine, but I am unsure whether I am doing something wrong or if the Warning should not really occur. Thanks!

dfm commented 2 years ago

Looks like this warning originates here (the line numbers have changed since the release):

https://github.com/daft-dev/daft/blob/75a4052a87514c328af21730bcbdf57e027acfbf/daft.py#L851

when the node shape is "rectangle" and dx = 0. This definitely seems like a bug and should be looked into!

flyaflya commented 2 years ago

In that case, I should mention that something similar occurs when using a horizontal edge from the rectangle as well:

pgm = daft.PGM(node_fc="aliceblue", dpi = 150, alternate_style="outer")

pgm.add_node("sb","Start\nBalance", 1, 4, aspect = 2.2, observed = True, plot_params = {'facecolor': 'cadetblue'})
pgm.add_node("ba","Bet\nAmount", 1, 3, aspect = 2.2, shape = "rectangle", plot_params = {'facecolor': 'thistle'})
pgm.add_node("w","Winnings", 2.7, 3, aspect = 2.2, alternate = True)

pgm.add_edge("sb", "ba")
pgm.add_edge("ba","w")
pgm.render()

yields:

/usr/local/lib/python3.7/dist-packages/daft.py:852: RuntimeWarning: divide by zero encountered in double_scalars
  * (np.sign(dy) or 1.0)
/usr/local/lib/python3.7/dist-packages/daft.py:857: RuntimeWarning: divide by zero encountered in double_scalars
  dxx2 = self.scale * 0.5 * np.abs(dx / dy) * (np.sign(dx) or 1.0)
<matplotlib.axes._axes.Axes at 0x7f220e0761d0>

image

Thanks for looking into it!!! I really like the package.👍