Module object_to_scatterplot
|
|
1
2
3
4 """object_to_scatterplot
5
6 Load a serialized object in memory and generate input files for ploticus.
7 Then call ploticus to create the png file
8 """
9
10 __author__ = "Jerome Hussenet, Cedric Bonhomme"
11 __version__ = "$Revision: 0.2 $"
12 __date__ = "$Date: 2009/03/01 $"
13 __copyright__ = "Copyright (c) 2009 Jerome Hussenet, Copyright (c) 2009 Cedric Bonhomme"
14 __license__ = "Python"
15
16 import os
17 import sys
18 import pickle
19
21 """Generate a scatter plot graph.
22 """
23 dic_obj = open(obj_file, "r")
24 if options.verbose:
25 print "Loading dictionary..."
26 dic_ip = pickle.load(dic_obj)
27
28 dic_l = {}
29 for i in dic_ip:
30 if not str(i) in dic_l:
31 dic_l[str(i)] = 0
32 for j in dic_ip[i]:
33 if not str(j) in dic_l:
34 dic_l[str(j)] = 0
35
36 if options.verbose:
37 print "Creating ploticus categories file"
38 cat_f = open("./scatterplot/cat.inc", "w")
39 for i in dic_l:
40 cat_f.write(i + "\n")
41 cat_f.close()
42
43 if options.verbose:
44 print "Creating ploticus data file"
45 data_f = open("./scatterplot/data.inc", "w")
46 for s in dic_ip:
47 for d in dic_ip[s]:
48 data_f.write(s+" "+d+" "+str(dic_ip[s][d])+"\n")
49 data_f.close()
50
51
52 ploticus = '\tploticus -o ./scatterplot/scatterplot.png -png ./scatterplot/scatterplot -csmap -maxproclines'
53 if options.verbose:
54 print "Command to execute :"
55 print ploticus
56
57 (child_stdin, child_stdout, child_stderr) = os.popen3(ploticus)
58 stderr = child_stderr.readlines()
59 stdout = child_stdout.readlines()
60 child_stdin.close()
61 child_stdout.close()
62 child_stderr.close()
63
64 if options.verbose:
65 if stderr != []:
66 print "Problem(s) :"
67 print "\n".join(stderr)
68
69
70 html = '<html>\n<head>\n<title>IP-Link -- Scatterplot</tile>\n</head>\n<body>'
71 html += '\n<map name="map1">\n'
72 for area_shape in stdout:
73 html += "\t" + area_shape + "\n"
74 html += '</map>\n<img src="scatterplot.png" usemap="#map1">'
75 html += '\n</body>\n</html>'
76
77 if options.verbose:
78 print "Creating HTML map"
79 html_file = open(scatter_file, "w")
80 html_file.write(html)
81 html_file.close()
82
83
84 if __name__ == "__main__":
85
86 from optparse import OptionParser
87 parser = OptionParser()
88 parser.add_option("-i", "--input", dest="obj_file",
89 help="Python serialized object")
90 parser.add_option("-o", "--output", dest="scatter_file",
91 help="Scatter plot HTML map file")
92 parser.add_option("-q", "--quiet",
93 action="store_false", dest="verbose",
94 help="be vewwy quiet (I'm hunting wabbits)")
95 parser.set_defaults(obj_file = './data/dic.pyobj',
96 scatter_file = './scatterplot/index.html',
97 verbose = True)
98
99 (options, args) = parser.parse_args()
100
101 object_to_scatterplot(options.obj_file, options.scatter_file)
102