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

Documentation? #6

Open
woodg07 opened this issue Oct 16, 2020 · 1 comment
Open

Documentation? #6

woodg07 opened this issue Oct 16, 2020 · 1 comment

Comments

@woodg07
Copy link

woodg07 commented Oct 16, 2020

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

@ricosaurus
Copy link

ricosaurus commented Apr 21, 2021

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 │
└───────┘

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