Anders and Briegel in Python
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

46 linhas
1.5KB

  1. """
  2. Useful but messy crap
  3. """
  4. import cPickle
  5. import networkx as nx
  6. from matplotlib import pyplot as plt
  7. from graph import *
  8. import clifford
  9. import numpy as np
  10. VOP_COLORS = ["red", "green", "blue", "orange", "yellow", "purple", "black", "white"]
  11. def cache_to_disk(file_name):
  12. """ A decorator to cache the output of a function to disk """
  13. def wrap(func):
  14. def modified(*args, **kwargs):
  15. try:
  16. output = cPickle.load(open(file_name, "r"))
  17. except (IOError, ValueError):
  18. output = func(*args, **kwargs)
  19. with open(file_name, "w") as f:
  20. cPickle.dump(output, f)
  21. return output
  22. return modified
  23. return wrap
  24. def draw(graph, vops, filename="out.pdf", pos=None, ns=500):
  25. """ Draw a graph with networkx layout """
  26. plt.clf()
  27. g = nx.from_edgelist(edgelist(graph))
  28. pos = nx.spring_layout(g) if pos==None else pos
  29. colors = [VOP_COLORS[vop % len(VOP_COLORS)] for vop in vops]
  30. nx.draw_networkx_nodes(g, pos, node_color="white", node_size=ns)
  31. nx.draw_networkx_nodes(g, pos, node_color=colors, node_size=ns, alpha=.4)
  32. nx.draw_networkx_edges(g, pos, edge_color="gray")
  33. nx.draw_networkx_labels(g, pos, font_family="FreeSans")
  34. labels = {i: clifford.name_of(vops[i]) for i in g.nodes()}
  35. pos = {k: v + np.array([0, -.1]) for k, v in pos.items()}
  36. nx.draw_networkx_labels(g, pos, labels, font_family="FreeSans")
  37. plt.axis('off')
  38. plt.savefig(filename)
  39. return pos