render_spec_with_graphviz.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Google Inc. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # ==============================================================================
  16. """Renders DRAGNN specs with Graphviz."""
  17. from __future__ import absolute_import
  18. from __future__ import division
  19. from __future__ import print_function
  20. import warnings
  21. import pygraphviz
  22. from dragnn.protos import spec_pb2
  23. def _component_contents(component):
  24. """Generates the label on component boxes.
  25. Args:
  26. component: spec_pb2.ComponentSpec proto
  27. Returns:
  28. String label
  29. """
  30. return """<
  31. <B>{name}</B><BR />
  32. {transition_name}<BR />
  33. {network_name}<BR />
  34. {num_actions_str}<BR />
  35. hidden: {num_hidden}
  36. >""".format(
  37. name=component.name,
  38. transition_name=component.transition_system.registered_name,
  39. network_name=component.network_unit.registered_name,
  40. num_actions_str="{} action{}".format(component.num_actions, "s" if
  41. component.num_actions != 1 else ""),
  42. num_hidden=component.network_unit.parameters.get("hidden_layer_sizes",
  43. "not specified"))
  44. def _linked_feature_label(linked_feature):
  45. """Generates the label on edges between components.
  46. Args:
  47. linked_feature: spec_pb2.LinkedFeatureChannel proto
  48. Returns:
  49. String label
  50. """
  51. return """<
  52. <B>{name}</B><BR />
  53. F={num_features} D={projected_dim}<BR />
  54. {fml}<BR />
  55. <U>{source_translator}</U><BR />
  56. <I>{source_layer}</I>
  57. >""".format(
  58. name=linked_feature.name,
  59. num_features=linked_feature.size,
  60. projected_dim=linked_feature.embedding_dim,
  61. fml=linked_feature.fml,
  62. source_translator=linked_feature.source_translator,
  63. source_layer=linked_feature.source_layer)
  64. def master_spec_graph(master_spec):
  65. """Constructs a master spec graph.
  66. Args:
  67. master_spec: MasterSpec proto.
  68. Raises:
  69. TypeError, if master_spec is not the right type. N.B. that this may be
  70. raised if you import proto classes in non-standard ways (e.g. dynamically).
  71. Returns:
  72. SVG graph contents as a string.
  73. """
  74. if not isinstance(master_spec, spec_pb2.MasterSpec):
  75. raise TypeError("master_spec_graph() expects a MasterSpec input.")
  76. graph = pygraphviz.AGraph(directed=True)
  77. graph.node_attr.update(
  78. shape="box",
  79. style="filled",
  80. fillcolor="white",
  81. fontname="roboto, helvetica, arial",
  82. fontsize=11)
  83. graph.edge_attr.update(fontname="roboto, helvetica, arial", fontsize=11)
  84. for component in master_spec.component:
  85. graph.add_node(component.name, label=_component_contents(component))
  86. for component in master_spec.component:
  87. for linked_feature in component.linked_feature:
  88. graph.add_edge(
  89. linked_feature.source_component,
  90. component.name,
  91. label=_linked_feature_label(linked_feature))
  92. with warnings.catch_warnings():
  93. # Fontconfig spews some warnings, suppress them for now. (Especially because
  94. # they can clutter IPython notebooks).
  95. warnings.simplefilter("ignore")
  96. return graph.draw(format="svg", prog="dot")