forked from nveldt/fauci-email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_datasets.jl
184 lines (175 loc) · 6.11 KB
/
make_datasets.jl
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
include("methods.jl")
function _write_list(io, listname::String, items; last::Bool=false, map::Function=string)
nitems = length(items)
write(io, "\"$listname\": [", "\n")
for (ii,i) in enumerate(items)
write(io, map(i), ii < nitems ? ",\n" : "\n")
end
write(io, last ? "]\n" : "],\n") # skip comma if last...
end
function _write_edgedata(io, A::SparseMatrixCSC, nedges::Integer; last::Bool=false)
write(io, "\"edgedata\": [", "\n")
# ugh, annoying to get this precise for JSON with the last comma missing...
edata = zip(findnz(A)...)
@assert(length(edata) == nedges)
for (ei,(i,j,w)) in enumerate(edata)
write(io, string(i-1), ", ", string(j-1), ", ", string(w), ei < nedges ? ",\n" : "\n")
end
write(io, last ? "]\n" : "],\n") # skip comma if last...
end
""" Write a custom json output method to make it easy to parse these
files without depending on a full json library... but also make them
JSON so if you want to use that, it's easy! The format is:
```
{
"vertices": <number of vertices>,
"edges": <number of edges>,
"edgedata": [
<src1>, <dst1>, <weight1>,
<src2>, <dst2>, <weight2>,
...
<src_number_of_edges>, <dst_number_of_edges>, <weight_number_of_edges>
],
"labels": [
<list of labels, one per vertex>
],
"orgs": [
<list of organizations, one per vertex"
]
}
```
so that you could parse these as follows (in Python, say...)
with open("gdata.json", "r") as f:
f.readline() # read the first '{'
nverts = int(f.readline().split(':')[1].split(',')[0])
nedges = int(f.readline().split(':')[1].split(',')[0])
f.readline() # read "edgedata"
src, dst, weights = [],[],[]
for _ in range(nedges):
einfo = f.readline().split(",")
src.append(int(einfo[0]))
dst.append(int(einfo[1]))
weights.append(int(einfo[2]))
f.readline() # read end array
f.readline() # read label array start
labels = []
for _ in range(nverts)
labels.append(f.readline().strip().strip('"'))
orgs = []
for _ in range(nverts)
orgs.append(int(f.readline()))
```
all information is zero-indexed...
an undirected edge is repeated twice, one for each direction.
If you need to translate into a SNAP graph and drop weights, say, this is easy with
shell tools
```
\$ tail -n +5 fauci-email-tofrom-5.json | sed -n '/],/q;p' | sed 's/,//g' | cut -f1,2 -d" " | less
```
"""
function _write_simple_json(filename::String, G::NamedTuple)
nverts = size(G.A,1)
nedges = nnz(G.A)
labels = G.names
orgs = G.orgs
open(filename, "w") do f
write(f, "{\n")
write(f, "\"vertices\": ", string(nverts), ",\n")
write(f, "\"edges\": ", string(nedges), ",\n")
_write_edgedata(f, G.A, nedges)
@assert(length(labels) == nverts)
_write_list(f, "labels", labels; map=JSON.json)
@assert(length(orgs) == nverts)
_write_list(f, "orgs", orgs; last=true)
write(f, "}\n")
end
end
##
G = _build_email_tofrom_graph(data; maxset=5, keepfauci=false)
_write_simple_json("fauci-email-graph-tofrom-nofauci-nocc-5.json", G)
##
G = _build_email_tofrom_graph(data; maxset=5, keepfauci=false, keepcc=true)
_write_simple_json("fauci-email-graph-tofrom-nofauci-cc-5.json", G)
##
G = _build_email_repliedto_graph(data; keepfauci=false)
_write_simple_json("fauci-email-graph-repliedto-nofauci.json", G)
##
G = _build_email_hypergraph_projection(data;
hyperedgeparts=("sender","recipients"), mindegree=2)
_write_simple_json("fauci-email-graph-hypergraph-projection-nocc.json", G)
##
G = _build_email_hypergraph_projection(data;
hyperedgeparts=("sender","recipients","cc"), mindegree=2)
_write_simple_json("fauci-email-graph-hypergraph-projection-cc.json", G)
##
Tcc_ids = temporal_reachability(data) |> R->min.(R.R, R.R') |> simple_clique_heuristic
T = build_temporal_graphs(data; subset=Tcc_ids)
function _write_json_graph_sequence(filename::String, G::NamedTuple)
nverts = size(G.T[1][2],1)
ngraphs = length(G.T)
labels = G.names
orgs = G.orgs
open(filename, "w") do f
write(f, "{\n")
write(f, "\"vertices\": ", string(nverts), ",\n")
write(f, "\"graphs\": ", string(length(G.T)), ",\n")
write(f, "\"sequencedata\": [")
for (gi, (date, g)) in enumerate(G.T)
nedges = nnz(g)
write(f, "{\n")
write(f, "\"edges\": ", string(nedges), ",\n")
_write_edgedata(f, g, nedges; last=true)
write(f, gi < length(G.T) ? "},\n" : "}\n")
end
write(f, "],", "\n")
@assert issorted(first.(G.T))
_write_list(f, "dates", first.(G.T); map=JSON.json)
@assert(length(labels) == nverts)
_write_list(f, "labels", labels; map=JSON.json)
@assert(length(orgs) == nverts)
_write_list(f, "orgs", orgs; last=true)
write(f, "}\n")
end
end
_write_json_graph_sequence("fauci-email-temporalgraph-tofrom.json", T)
## Construct a hypergraph and save it.
include("methods_hypergraph.jl")
kf = false
ms = 100
mindeg = 5
parts=("sender","recipients")
H = _build_email_hypergraph(data;hyperedgeparts=parts,maxset=ms, keepfauci=kf,mindegree = mindeg)
##
function _write_hyperedgedata(io, A::SparseMatrixCSC, nedges::Integer; last::Bool=false)
write(io, "\"hyperedgedata\": [", "\n")
# ugh, annoying to get this precise for JSON with the last comma missing...
edata = zip(findnz(A)...)
@assert(length(edata) == nedges)
for (ei,(i,j,w)) in enumerate(edata)
write(io, string(j-1), ", ", string(i-1), ei < nedges ? ",\n" : "\n")
end
write(io, last ? "]\n" : "],\n") # skip comma if last...
end
function _write_hypergraph_json(filename::String, G::NamedTuple)
nverts = size(G.H,2)
nedges = size(G.H,1)
nincidence = nnz(G.H)
labels = G.names
orgs = G.orgs
weights = G.weights
open(filename, "w") do f
write(f, "{\n")
write(f, "\"vertices\": ", string(nverts), ",\n")
write(f, "\"hyperedges\": ", string(nedges), ",\n")
write(f, "\"incidences\": ", string(nincidence), ",\n")
_write_hyperedgedata(f, copy(G.H'), nincidence)
@assert(length(weights) == nedges)
_write_list(f, "weights", weights)
@assert(length(labels) == nverts)
_write_list(f, "labels", labels; map=JSON.json)
@assert(length(orgs) == nverts)
_write_list(f, "orgs", orgs; last=true)
write(f, "}\n")
end
end
_write_hypergraph_json("fauci-email-hypergraph.json", H)