-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgexf.py
281 lines (248 loc) · 10.9 KB
/
gexf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# gexf.py - Transform a weavr's posts' keywords/emotions into a gexf file.
# Copyright (C) 2012 Rob Myers <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################################################################
# Imports
################################################################################
import cgi
import time
import weavrs
################################################################################
# Utilities
################################################################################
# http://mail.python.org/pipermail/python-list/2004-September/282520.html
def all_pairs(seq):
l = len(seq)
for i in range(l):
for j in range(i+1, l):
yield seq[i], seq[j]
def dtepoch(datetime):
"""Convert the datetime to seconds since epoch"""
return time.mktime(datetime.timetuple())
def dtxsd(dt):
"""Convert the datetime to an xsd:datetime string"""
return dt.isoformat()
################################################################################
# Emotions
################################################################################
# Keywords as nodes
# Emotions as edges
# Uses number of times keywords occur with the same emotion as edge weight
def emotion_edge_graph(runs):
"""Return a list of node names, and a dict of edge pairs and weights"""
node_names = set()
edges = dict()
for run in runs:
emotion = (run['emotion'],)
for post in run['posts']:
keywords = list(set(post['keywords'].split()))
node_names.update(keywords)
for pair in all_pairs(keywords):
edge = tuple(sorted(pair)) + emotion
edges[edge] = edges.get(edge, 0) + 1
return list(node_names), edges
def emotion_edge_graph_to_xml(stream, nodes, edges):
print >>stream, u'<?xml version="1.0" encoding="UTF-8"?>'
print >>stream, u'<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2">'
print >>stream, u'<graph mode="static" defaultedgetype="undirected">'
print >>stream, u'<nodes>'
for node in nodes:
print >> stream, u'<node id="%s" label="%s" />' % (node, node)
print >>stream, u'</nodes>'
print >>stream, u'<edges>'
edge_id = 0
for edge, weight in edges.iteritems():
print >>stream, u'<edge id="%i" source="%s" target="%s" weight="%f" label="%s" />'\
% (edge_id, edge[0], edge[1], weight, edge[2])
edge_id += 1
print >>stream, u'</edges>'
print >>stream, u'</graph>'
print >>stream, u'</gexf>'
# Emotions as nodes
# Keywords as edges
# No weight, just show if emotions ever have the same keywords
# If this isn't any good we could use how iften they have the same keywords
# Although this might not be very useful
def emotion_node_graph(runs):
"""Return a list of node names, and a dict of edge pairs and weights"""
node_names = set()
edges = dict()
for run in runs:
emotion = [run['emotion']]
node_names.update(emotion)
for post in run['posts']:
keywords = set(post['keywords'].split())
for keyword in keywords:
edges[keyword] = edges.get(keyword, set()).union(emotion)
return list(node_names), edges
def emotion_node_graph_to_xml(stream, nodes, edges):
print >>stream, u'<?xml version="1.0" encoding="UTF-8"?>'
print >>stream, u'<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2">'
print >>stream, u'<graph mode="static" defaultedgetype="undirected">'
print >>stream, u'<nodes>'
for node in nodes:
print >> stream, u'<node id="%s" label="%s" />' % (node, node)
print >>stream, u'</nodes>'
print >>stream, u'<edges>'
edge_id = 0
for keyword, emotions in edges.iteritems():
for pair in all_pairs(list(emotions)):
print >>stream, u'<edge id="%i" source="%s" target="%s" label="%s" />' % (edge_id, pair[0], pair[1], keyword)
edge_id += 1
print >>stream, u'</edges>'
print >>stream, u'</graph>'
print >>stream, u'</gexf>'
################################################################################
# Keywords
################################################################################
def keyword_graph(runs):
"""Return a list of node names, and a dict of edge pairs and weights"""
node_names = set()
edges = dict()
for run in runs:
for post in run['posts']:
keywords = list(set(post['keywords'].split()))
node_names.update(keywords)
for pair in all_pairs(keywords):
# Make sure a/b and b/a are counted the same
edge = tuple(sorted(pair))
edges[edge] = edges.get(edge, 0) + 1
return list(node_names), edges
def keyword_graph_to_xml(stream, nodes, edges):
print >>stream, u'<?xml version="1.0" encoding="UTF-8"?>'
print >>stream, u'<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2">'
print >>stream, u'<graph mode="static" defaultedgetype="undirected">'
print >>stream, u'<nodes>'
for node in nodes:
print >> stream, u'<node id="%s" label="%s" />' % (node, node)
print >>stream, u'</nodes>'
print >>stream, u'<edges>'
edge_id = 0
for edge, weight in edges.iteritems():
print >>stream, u'<edge id="%i" source="%s" target="%s" weight="%f" />'\
% (edge_id, edge[0], edge[1], weight)
edge_id += 1
print >>stream, u'</edges>'
print >>stream, u'</graph>'
print >>stream, u'</gexf>'
################################################################################
# Keywords over time
################################################################################
def run_keyword_edges(run):
"""Return a dict of (a,b):n representing the strength of the edges between
keywords for the run"""
edges = dict()
for post in run['posts']:
keywords = list(set(post['keywords'].split()))
for pair in all_pairs(keywords):
# Make sure a/b and b/a are counted the same
edge = tuple(sorted(pair))
edges[edge] = edges.get(edge, 0) + 1
return edges
def runs_keywords(runs):
"""Get all the keywords for every post of every run"""
keywords = set()
for run in runs:
for post in run['posts']:
post_keywords = set(post['keywords'].split())
keywords.update(post_keywords)
return list(keywords)
def keyword_first_run(runs, keyword):
"""Get the run a keyword first appears in"""
first_appearance = None
for run in runs:
for post in run['posts']:
if keyword in set(post['keywords'].split()):
first_appearance = run
break
return first_appearance
def keywords_first_run_times(runs, keywords):
first_appearances = []
for keyword in keywords:
first_appearances.append(keyword_first_run(runs, keyword)["datetime"])
return first_appearances
class DynamicEdge(object):
"""An Edge with a start and end time"""
def __init__(self, node_from, node_to, time_from, time_to, weight):
self.node_from = node_from
self.node_to = node_to
self.time_from = time_from
self.time_to = time_to
self.weight = weight
def keyword_edge_durations(runs):
"""Return a list of dynamic edges for the runs (excluding last run)"""
edges = list()
current_time = weavrs.parse_datetime(runs[0]['datetime'])
current_edges = run_keyword_edges(runs[0])
for run in runs[1:]:
next_time = weavrs.parse_datetime(run['datetime'])
for edge_pair, edge_weight in current_edges.iteritems():
edges.append(DynamicEdge(edge_pair[0], edge_pair[1], current_time,
next_time, edge_weight))
current_edges = run_keyword_edges(run)
current_time = next_time
return edges
def keyword_durations_to_xml(stream, nodes, edges, edge_start_times=False):
print >>stream, u'<?xml version="1.0" encoding="UTF-8"?>'
print >>stream, u'<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2">'
# xsd:dateTime for timeformat
print >>stream, u'<graph mode="dynamic" defaultedgetype="undirected" timeformat="datetime">'
print >>stream, u'<nodes>'
if edge_start_times:
for i in xrange(len(nodes)):
print >> stream, u'<node id="%s" label="%s" from="%s"/>' % \
(nodes[i], nodes[i], edge_start_times[i])
else:
for node in nodes:
print >> stream, u'<node id="%s" label="%s" />' % (node, node)
print >>stream, u'</nodes>'
print >>stream, u'<edges>'
edge_id = 0
for edge in edges:
print >>stream, u'<edge id="%i" source="%s" target="%s" start="%s" end="%s" weight="%f" />'\
% (edge_id, edge.node_from, edge.node_to, dtxsd(edge.time_from),
dtxsd(edge.time_to), edge.weight)
edge_id += 1
print >>stream, u'</edges>'
print >>stream, u'</graph>'
print >>stream, u'</gexf>'
################################################################################
# Locations
################################################################################
def locations_to_xml(stream, locations):
print >>stream, u'<?xml version="1.0" encoding="UTF-8"?>'
print >>stream, u'<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2" xmlns:viz="http://www.gexf.net/1.1draft/viz">'
print >>stream, u'<graph mode="static" defaultedgetype="undirected">'
print >>stream, u'<attributes class="node" mode="static">'
print >>stream, u'<attribute id="latitude" title="latitude" type="double" />'
print >>stream, u'<attribute id="longitude" title="longitude" type="double" />'
print >>stream, u'</attributes>'
print >>stream, u'<nodes>'
for location in locations:
name = location['title']
if name == '':
name = location['street_address']
print >>stream, u'<node id="%s" label="%s">' % (location['id'],
cgi.escape(name))
print >>stream, u'<attvalue for="latitude" value="%s" />' % \
location['lat']
print >>stream, u'<attvalue for="longitude" value="%s" />' % \
location['lon']
#print >>stream, u'<viz:position x="%s" y="%s" z="0.0" />' % \
# (location['lon'], location['lat'])
print >>stream, u'</node>'
print >>stream, u'</nodes>'
print >>stream, u'</graph>'
print >>stream, u'</gexf>'