2

I am trying to convert my text file to an undirected graph automatically with the help of graphviz. The text file consists of the following code:

0

A
Relation
B
A
Relation
C
B
Relation
C
1

0

A
Relation
C

B
Relation
C
1

Here A, B and C are nodes. I may require a single or multiple graphs. 0 and 1 represent the start and end of each graph. The number of relations may also vary. I tried to proceed with sed, but got lost. How should I proceed to get the graph I require? Thanks for your help.

greg
  • 4,843
  • 32
  • 47
abhisekG
  • 426
  • 2
  • 5
  • 16
  • Could you give an example of what you expect the resulting graphs to look like? Am I correct in thinking that the second one would look something like `A -- C -- B` and the first would be a triangle with A, B and C in the three corners. – Simon Jan 13 '14 at 06:25
  • yes exactly. The first would contain A, B and C in three corners. The second would contain A--C--B i.e. C is connected with B and A both. – abhisekG Jan 13 '14 at 08:05

2 Answers2

3

I don't use PyGraphViz myself, but doing the text processing in Python is easy enough. Given the input file in the question, which I've called gra1.txt, and a Python file gr.py as follows:

import sys, subprocess

count = 0
for line in sys.stdin:
    if line[0] == '0':
        outf = "g%d" % (count)
        g = "graph G%d {\n" % (count)
        count += 1
    elif line[0] == '1':
        g += "}\n"
        dot = subprocess.Popen(["dot", "-Tjpg", "-o%s.jpg" % outf], 
                stdin=subprocess.PIPE,universal_newlines=True)
        print (g)
        dot.communicate(g)  
    elif len(line.rstrip()) == 0:
        pass
    else:
        first = line.rstrip()
        rel = sys.stdin.readline()
        last = sys.stdin.readline().rstrip()
        g += "%s -- %s\n" % (first,last)

... the command python gra1.py <gra1.txt produces the output:

$ python gra1.py <gra1.txt
graph G0 {
A -- B
A -- C
B -- C
}

graph G1 {
A -- C
B -- C
}

... along with the files g0.jpg:

enter image description here

... and g1.jpg:

enter image description here

Simon
  • 10,679
  • 1
  • 30
  • 44
1

You can do it with the graphviz python library. To install it you just need to launch:

pip install graphviz

and then in Python you can do:

from graphviz import Source

text_from_file = str()
with open('graphviz_dot_file.txt') as file:
    text_from_file = file.read()

src = Source(text_from_file)
src.render(test.gv', view=True ) 

You can find more informations in the Graphviz's manual

alexandre-rousseau
  • 2,321
  • 26
  • 33
  • I got the error while trying your solution, PUN-U531305L001:Desktop 531305$ python graph.py Traceback (most recent call last): File "graph.py", line 8, in src.render('test.gv', view=True) File "/Library/Python/2.7/site-packages/graphviz/files.py", line 226, in render 'are on your systems\' path' % cmd) RuntimeError: failed to execute ['dot', '-Tpdf', '-O', 'test.gv'], make sure the Graphviz executables are on your systems' path – Bhupesh Pant Jul 25 '16 at 04:47
  • 1
    I had the similar issue. It's seem be you use **Graphviz** on Widows. Make sure you have **Graphviz** on your *System path variables*. Here [your solution](http://stackoverflow.com/questions/18438997/why-is-pydot-unable-to-find-graphvizs-executables-in-windows-8/20458620#20458620) – alexandre-rousseau Jul 25 '16 at 12:11