LukasZahradnik / PyNeuraLogic

PyNeuraLogic lets you use Python to create Differentiable Logic Programs
https://pyneuralogic.readthedocs.io/
MIT License
269 stars 17 forks source link

[🐛 Bug Report]: Visualization draw_model function crashes on None/Null #32

Closed martin-krutsky closed 2 years ago

martin-krutsky commented 2 years ago

Describe the bug

From the This example notebook Visualization.ipynb: Call to neuralogic.utils.visualize.draw_model function, draw_model(model), results in the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-59797e1249fd> in <module>
      7 model = template.build(Backend.JAVA, Settings())
      8 
----> 9 draw_model(model)

...\lib\site-packages\neuralogic\utils\visualize\__init__.py in draw_model(model, filename, draw_ipython, img_type, value_detail, *args, **kwargs)
     93     template_drawer = get_template_drawer(get_drawing_settings(img_type=img_type, value_detail=value_detail))
     94 
---> 95     return draw(template_drawer, template, filename, draw_ipython, img_type, *args, **kwargs)
     96 
     97 

...\lib\site-packages\neuralogic\utils\visualize\__init__.py in draw(drawer, obj, filename, draw_ipython, img_type, *args, **kwargs)
     55         return
     56 
---> 57     data = bytes(drawer.drawIntoBytes(obj))
     58 
     59     if draw_ipython:

TypeError: cannot convert 'NoneType' object to bytes

Similarly, adding a filename to the call (draw_model(model, 'img.png'), see code snippets below) results in a NullPointerException:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
...\lib\site-packages\_jpype.cp38-win_amd64.pyd in cz.cvut.fel.ida.drawing.Drawer.drawIntoFile()

...\lib\site-packages\_jpype.cp38-win_amd64.pyd in cz.cvut.fel.ida.drawing.GraphViz.writeImageToFile()

~\Anaconda3\envs\bakalarka\lib\site-packages\_jpype.cp38-win_amd64.pyd in java.io.FileOutputStream.write()

Exception: Java Exception

The above exception was the direct cause of the following exception:

java.lang.NullPointerException            Traceback (most recent call last)
<ipython-input-8-91eec29cdcf5> in <module>
      7 model = template.build(Backend.JAVA, Settings())
      8 
----> 9 draw_model(model, 'img.png')

...\lib\site-packages\neuralogic\utils\visualize\__init__.py in draw_model(model, filename, draw_ipython, img_type, value_detail, *args, **kwargs)
     93     template_drawer = get_template_drawer(get_drawing_settings(img_type=img_type, value_detail=value_detail))
     94 
---> 95     return draw(template_drawer, template, filename, draw_ipython, img_type, *args, **kwargs)
     96 
     97 

...\lib\site-packages\neuralogic\utils\visualize\__init__.py in draw(drawer, obj, filename, draw_ipython, img_type, *args, **kwargs)
     51 def draw(drawer, obj, filename: Optional[str] = None, draw_ipython=True, img_type="png", *args, **kwargs):
     52     if filename is not None:
---> 53         drawer.drawIntoFile(obj, os.path.abspath(filename))
     54 
     55         return

java.lang.NullPointerException: java.lang.NullPointerException

Steps to reproduce the behavior

I have come across this error in my own example codes, but it is enough to run the Visualization.ipynb notebook, concretely:

from neuralogic.utils.visualize import draw_model
from neuralogic.utils.data import XOR_Vectorized
from neuralogic.core import Settings, Backend

template, dataset = XOR_Vectorized()
model = template.build(Backend.JAVA, Settings())

draw_model(model)

For the NullPointerException, I used:

from neuralogic.utils.visualize import draw_model
from neuralogic.utils.data import XOR_Vectorized
from neuralogic.core import Settings, Backend

template, dataset = XOR_Vectorized()
model = template.build(Backend.JAVA, Settings())

draw_model(model, 'img.png')

Expected behavior

I should be able to visualize the model.

Environment

No response

Additional context

No response

LukasZahradnik commented 2 years ago

Hi, thanks for the bug report. Can you verify you have the latest version of PyNeuraLogic, which is 0.1.4? I feel like this issue has been solved.

Thanks

martin-krutsky commented 2 years ago

Indeed, this is with the current version 0.1.4. I was actually getting other error before upgrading the package (I haven't saved the stack trace, but AFAIK the error was about missing a data parameter). I can try to completely reinstall the package later, if that helps. EDIT: Even after reinstalling the library, the error prevails.

borisrakovan commented 2 years ago

I can confirm that the same issue appears on macOS as well

LukasZahradnik commented 2 years ago

Hi, I released a new version 0.1.5 (changelog), that should contain a fix for this issue. I added some tests running on Windows, and it seems to work, but unfortunately, I wasn't able to check macOS. Now, NeuraLogic will try to use the dot executable from Path. If it isn't installed on your Path, then you can specify the path via the new graphviz_path parameter of draw_model and draw_sample.

Can you please try it out again if it works now @martin-krutsky @borisrakovan ? Thanks

martin-krutsky commented 2 years ago

I can confirm it works for me on Windows, both in the visualization notebook and in my own experiment. Thanks! @borisrakovan, feel free to close this issue, if you can confirm the issue is solved on macOS, as well.

borisrakovan commented 2 years ago

I bumped the version to 0.1.5 and the drawing works fine on macOS. Thanks. Unfortunately I don't have rights to close the issue.