render_spec_with_graphviz.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. """Renders DRAGNN specs with Graphviz."""
  3. from __future__ import absolute_import
  4. from __future__ import division
  5. from __future__ import print_function
  6. import warnings
  7. import pygraphviz
  8. from dragnn.protos import spec_pb2
  9. def _component_contents(component):
  10. """Generates the label on component boxes.
  11. Args:
  12. component: spec_pb2.ComponentSpec proto
  13. Returns:
  14. String label
  15. """
  16. return """<
  17. <B>{name}</B><BR />
  18. {transition_name}<BR />
  19. {network_name}<BR />
  20. {num_actions_str}<BR />
  21. hidden: {num_hidden}
  22. >""".format(
  23. name=component.name,
  24. transition_name=component.transition_system.registered_name,
  25. network_name=component.network_unit.registered_name,
  26. num_actions_str="{} action{}".format(component.num_actions, "s" if
  27. component.num_actions != 1 else ""),
  28. num_hidden=component.network_unit.parameters.get("hidden_layer_sizes",
  29. "not specified"))
  30. def _linked_feature_label(linked_feature):
  31. """Generates the label on edges between components.
  32. Args:
  33. linked_feature: spec_pb2.LinkedFeatureChannel proto
  34. Returns:
  35. String label
  36. """
  37. return """<
  38. <B>{name}</B><BR />
  39. F={num_features} D={projected_dim}<BR />
  40. {fml}<BR />
  41. <U>{source_translator}</U><BR />
  42. <I>{source_layer}</I>
  43. >""".format(
  44. name=linked_feature.name,
  45. num_features=linked_feature.size,
  46. projected_dim=linked_feature.embedding_dim,
  47. fml=linked_feature.fml,
  48. source_translator=linked_feature.source_translator,
  49. source_layer=linked_feature.source_layer)
  50. def master_spec_graph(master_spec):
  51. """Constructs a master spec graph.
  52. Args:
  53. master_spec: MasterSpec proto.
  54. Raises:
  55. TypeError, if master_spec is not the right type. N.B. that this may be
  56. raised if you import proto classes in non-standard ways (e.g. dynamically).
  57. Returns:
  58. SVG graph contents as a string.
  59. """
  60. if not isinstance(master_spec, spec_pb2.MasterSpec):
  61. raise TypeError("master_spec_graph() expects a MasterSpec input.")
  62. graph = pygraphviz.AGraph(directed=True)
  63. graph.node_attr.update(
  64. shape="box",
  65. style="filled",
  66. fillcolor="white",
  67. fontname="roboto, helvetica, arial",
  68. fontsize=11)
  69. graph.edge_attr.update(fontname="roboto, helvetica, arial", fontsize=11)
  70. for component in master_spec.component:
  71. graph.add_node(component.name, label=_component_contents(component))
  72. for component in master_spec.component:
  73. for linked_feature in component.linked_feature:
  74. graph.add_edge(
  75. linked_feature.source_component,
  76. component.name,
  77. label=_linked_feature_label(linked_feature))
  78. with warnings.catch_warnings():
  79. # Fontconfig spews some warnings, suppress them for now. (Especially because
  80. # they can clutter IPython notebooks).
  81. warnings.simplefilter("ignore")
  82. return graph.draw(format="svg", prog="dot")