Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphviz representation not working when creating the model programmatically #3143

Open
ddelpiano opened this issue Dec 13, 2024 · 1 comment

Comments

@ddelpiano
Copy link

As per title, when creating the model programmatically using the ports params for mechanisms and the composition api, the graphviz representation, graphviz fails to serialise the model representation in json format. Thanks!

stacktrace:

python test.py 
Traceback (most recent call last):
  File "/home/ddelpiano/miniconda3/envs/psyneu/lib/python3.10/site-packages/graphviz/backend/execute.py", line 91, in run_check
    proc.check_returncode()
  File "/home/ddelpiano/miniconda3/envs/psyneu/lib/python3.10/subprocess.py", line 457, in check_returncode
    raise CalledProcessError(self.returncode, self.args, self.stdout,
subprocess.CalledProcessError: Command '[PosixPath('dot'), '-Kdot', '-Tjson']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ddelpiano/test.py", line 19, in <module>
    gv_node.pipe('json', quiet=True).decode()
  File "/home/ddelpiano/miniconda3/envs/psyneu/lib/python3.10/site-packages/graphviz/piping.py", line 104, in pipe
    return self._pipe_legacy(format,
  File "/home/ddelpiano/miniconda3/envs/psyneu/lib/python3.10/site-packages/graphviz/_tools.py", line 171, in wrapper
    return func(*args, **kwargs)
  File "/home/ddelpiano/miniconda3/envs/psyneu/lib/python3.10/site-packages/graphviz/piping.py", line 121, in _pipe_legacy
    return self._pipe_future(format,
  File "/home/ddelpiano/miniconda3/envs/psyneu/lib/python3.10/site-packages/graphviz/piping.py", line 161, in _pipe_future
    return self._pipe_lines(*args, input_encoding=self.encoding, **kwargs)
  File "/home/ddelpiano/miniconda3/envs/psyneu/lib/python3.10/site-packages/graphviz/backend/piping.py", line 161, in pipe_lines
    proc = execute.run_check(cmd, capture_output=True, quiet=quiet, **kwargs)
  File "/home/ddelpiano/miniconda3/envs/psyneu/lib/python3.10/site-packages/graphviz/backend/execute.py", line 93, in run_check
    raise CalledProcessError(*e.args)
graphviz.backend.execute.CalledProcessError: Command '[PosixPath('dot'), '-Kdot', '-Tjson']' returned non-zero exit status 1. [stderr: b'Error: syntax error in line 1 \n... <i></i> ...\nin label of node output\nError: syntax error in line 1 \n... <i></i> ...\nin label of node mid\nError: syntax error in line 1 \n... <i></i> ...\nin label of node input\nWarning: node mid, port OutputPort-mid_OutputPort_0 unrecognized\nWarning: node output, port InputPort-output_InputPort_0 unrecognized\nWarning: node input, port OutputPort-input_OutputPort_0 unrecognized\nWarning: node mid, port InputPort-mid_InputPort_0 unrecognized\n']

model.py

import psyneulink as pnl
from psyneulink import *

output = pnl.ProcessingMechanism(function = pnl.Linear(slope=2,intercept=0,), name = 'output', output_ports = ['output_OutputPort_0'], input_ports = ['output_InputPort_0'])
mid = pnl.ProcessingMechanism(function = pnl.Linear(slope=2,intercept=0,), name = 'mid', output_ports = ['mid_OutputPort_0'], input_ports = ['mid_InputPort_0'])
input = pnl.ProcessingMechanism(function = pnl.Linear(slope=1,intercept=0,), name = 'input', output_ports = ['input_OutputPort_0'], input_ports = ['input_InputPort_0'])
link_mid_to_output = pnl.MappingProjection(sender = mid, receiver = output, name = 'link_mid_to_output')
link_input_to_mid = pnl.MappingProjection(sender = input, receiver = mid, name = 'link_input_to_mid')
comp = pnl.Composition(name = 'comp')

comp.add_node(output)
comp.add_node(mid)
comp.add_node(input)

comp.add_projection(link_mid_to_output)
comp.add_projection(link_input_to_mid)

gv_node = comp.show_graph(show_node_structure=pnl.ALL, output_fmt="gv")
gv_node.pipe('json', quiet=True).decode()

# comp.run(inputs=[10, 1, 2, 3, 4, 5, 10, 7])
# print(comp.results)
@kmantel
Copy link
Collaborator

kmantel commented Dec 14, 2024

I noticed that there was no error if comp.show_graph(show_node_structure=pnl.ALL) was called before show_graph with 'gv'. It looks related to these lines

if output_fmt != 'gv':
composition._analyze_graph(context=context)
if composition._need_check_for_unused_projections:
composition._check_for_unused_projections(context)

If the _analyze_graph gets called, the error also goes away, but I'm not sure the reason it's skipped for 'gv' @jdcpni

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants