-
Notifications
You must be signed in to change notification settings - Fork 6
/
d3output.py
executable file
·133 lines (118 loc) · 4.26 KB
/
d3output.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
#!/usr/bin/env python
import os
import sys
import json
import csv
import io
def nodeslinks(threshold):
nodes = []
links = []
# lines look like "nodeA,nodeB,123"
for line in sys.stdin:
tokens = line.split(",")
# use try to ignore header line
try:
if int(tokens[2]) >= threshold:
if not tokens[0] in nodes:
nodes.append(tokens[0])
if not tokens[1] in nodes:
nodes.append(tokens[1])
links.append({"source": nodes.index(tokens[0]),
"target": nodes.index(tokens[1]),
"value": int(tokens[2])})
except:
continue
nodelist = []
for node in nodes:
nodelist.append({"name": node})
print(json.dump({"nodes": nodelist, "links": links}))
def nodeslinktrees(profile, nodes):
# generate nodes json
nodesoutput = []
linksoutput = []
if hasattr(profile["opts"], "graph"):
graph = profile["opts"]["graph"]
else:
graph = ""
for node in nodes:
if graph == "directed":
title = " (" + str(node["tweetcount"]) + " tweet"
if node["tweetcount"] != 1:
title += "s"
title += ": " + unicode(node["source"]) + " out/" + unicode(node["target"]) + " in)"
else:
title = " (" + str(node["tweetcount"]) + " tweet"
if node["tweetcount"] != 1:
title += "s"
title += ")"
nodesoutput.append({"name": node["name"],
"title": node["name"] + title})
# generate links
for targetname in node["links"].keys():
target = node["links"][targetname]
if target["count"] >= profile["opts"]["threshold"]:
linksoutput.append({
"source": node["id"],
"target": target["id"],
"value": target["count"]
})
return {"profile": profile, "nodes": nodesoutput, "links": linksoutput}
def namevaluecsv(data):
csvout = io.StringIO()
csvwriter = csv.writer(csvout)
csvwriter.writerow(["name", "value"])
for key, value in sorted(data.items()):
csvwriter.writerow([key, value])
return csvout.getvalue()
def valuecsv(data):
csvout = io.StringIO()
csvwriter = csv.writer(csvout)
csvwriter.writerow(["value"])
for d in data:
csvwriter.writerow([d])
return csvout.getvalue()
def nodeslinkcsv(data):
# convert link-nodes objects into csv
# e.g. {"A": {"B": 3, "C": 7}} to A,B,3 and A,C,7
csvout = io.StringIO()
csvwriter = csv.writer(csvout)
csvwriter.writerow(["source", "target", "value"])
for node in data:
source = node["name"]
# generate csv rows
for targetname in node["links"].iterkeys():
csvwriter.writerow([source, targetname, node["links"][targetname]["count"]])
return csvout.getvalue()
def namevaluejson(data):
output = []
for key, value in sorted(data.items()):
output.append({"name": key, "value": value})
return output
def valuejson(data):
output = []
for d in data:
output.append(d)
return output
def embed(template, d3json):
# load metadata.json if present
# d3json["args"] contains filenams passed in, with wildcards resolved
if d3json["profile"]["metadatafile"]:
metadata_file = d3json["profile"]["metadatafile"]
else:
metadata_file = os.path.join(os.path.dirname(d3json["profile"]["args"][0]), "metadata.json")
try:
with open(metadata_file) as json_data:
metadata = json.load(json_data)
json_data.close()
except:
#sys.exit("Cannot read metadata file " + metadata_file)
metadata = {"title": d3json["profile"]["args"][0]
+ (" (+)" if len(d3json["profile"]["args"]) > 1 else "") }
d3json["metadata"] = metadata
# generate html by replacing token in template
template_file = os.path.join(os.path.dirname(__file__), "templates", template)
with open (template_file, "r") as template:
output = template.read()
output = output.replace("$TITLE$", metadata["title"])
output = output.replace("$DATA$", json.dumps(d3json))
print(output)