cosminbasca/asciinet

Documentation?

Opened this issue · 1 comments

Looking to add node data to graphs. Any plans on showing how to do this?

If you haven't found a solution, I just found asciinet and wanted the same thing. Here is my very simple solution -- adding a labels dict as a kwarg to the graph_to_ascii function and method. labels is keyed by the node label and provides a string of data for each node -- the more obvious way to do this would be running thru the nodes and extracting node attributes for these values.

I noticed that the vertice and edge labels had to be changed in the same way.

The updated provided earlier by @noragen also here (line 107).

/usr/local/src/asciinet/pyasciinet/asciinet$ diff __init__.py __init_orig__.py 
76,86c76
<     def graph_to_ascii(self, graph, labels=None, timeout=10):
<         """
<         RL added labels
<         Args:
<             graph:
<             labels: dict of graph node -> label string
<             timeout:
< 
<         Returns:
< 
<         """
---
>     def graph_to_ascii(self, graph, timeout=10):
88,97c78,81
<             if labels:
<                 graph_repr = dumps({
<                     'vertices': [labels[v] for v in graph.nodes()],
<                     'edges': [[labels[e[0]], labels[e[1]]] for e in graph.edges()],
<                 })
<             else:
<                 graph_repr = dumps({
<                     'vertices': [str(v) for v in graph.nodes()],
<                     'edges': [[str(e[0]), str(e[1])] for e in graph.edges()],
<                 })
---
>             graph_repr = dumps({
>                 'vertices': [str(v) for v in graph.nodes()],
>                 'edges': [[str(e[0]), str(e[1])] for e in graph.edges()],
>             })
120c104
< def graph_to_ascii(graph, labels=None, timeout=10):
---
> def graph_to_ascii(graph, timeout=10):
123,127c107,108
<     #return _asciigraph.graph_to_ascii(graph, timeout=timeout).decode(encoding='UTF-8')
<     try:
<         return _asciigraph.graph_to_ascii(graph, labels=labels, timeout=timeout).decode(encoding='UTF-8')
<     except AttributeError:
<         return _asciigraph.graph_to_ascii(graph, labels=labels, timeout=timeout)
\ No newline at end of file
---
> 
>     return _asciigraph.graph_to_ascii(graph, timeout=timeout).decode(encoding='UTF-8')
\ No newline at end of file

So now the toy example:

import networkx as nx
from asciinet import graph_to_ascii

#
# create a simple graph
#
G = nx.Graph()
G.add_node(1)
G.add_nodes_from([2, 3, 4])
G.add_edges_from([(1, 2), (1, 3), (3, 4), (1, 4), (2, 4)])

labels = {1:'fruit\napples', 2:'apples\neating', 3:'apples\ncooking', 4:'variety\nGala'}
print(graph_to_ascii(G, labels=labels))

gives a (properly formatted):

    ───────┐      
  │ fruit │      
  │apples │      
  └─┬─┬─┬─┘      
    │ │ │        
 ┌──┘ │ └───────┐
 │    └───┐     │
 │        │     │
 v        v     │

┌──────┐ ┌───────┐ │
│apples│ │apples │ │
│eating│ │cooking│ │
└───┬──┘ └───┬───┘ │
│ │ │
└──┐ ┌───┘ │
│ │ ┌───────┘
│ │ │
v v v
┌───────┐
│variety│
│ Gala │
└───────┘