-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset_create.py
370 lines (227 loc) · 11.9 KB
/
dataset_create.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import json
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import datetime
import matplotlib.dates as mdates
from tabulate import tabulate
import math
def truncate(number, decimals=0):
"""
Returns a value truncated to a specific number of decimal places.
"""
if not isinstance(decimals, int):
raise TypeError("decimal places must be an integer.")
elif decimals < 0:
raise ValueError("decimal places has to be 0 or more.")
elif decimals == 0:
return math.trunc(number)
factor = 10.0 ** decimals
return math.trunc(number * factor) / factor
# ----------------------------- Load output json --------------------------
# credResult_Maker.json
# 'credResult_Pollen_10012020.json'
filename = 'Cred_data.csv'
raw_cred_file = 'credResult_Maker_12282020.json'
with open(raw_cred_file) as f:
cred = json.load(f)
# ----------------------------- Load node data into array ------------------
num_nodes = len(cred[1]['credData']['nodeSummaries'])
nodes = []
for i in range(num_nodes):
# if (cred[1]['weightedGraph'][1]['graphJSON'][1]['sortedNodeAddresses'][i][1] == 'discourse'):
node = {}
node['address'] = cred[1]['weightedGraph'][1]['graphJSON'][1]['sortedNodeAddresses'][i]
node['cred'] = cred[1]['credData']['nodeSummaries'][i]['cred']
if cred[1]['credData']['nodeOverTime'][i] is None:
node ['credOverTime'] = []
else:
node ['credOverTime'] = cred[1]['credData']['nodeOverTime'][i]['cred']
node['description'] = cred[1]['weightedGraph'][1]['graphJSON'][1]['nodes'][i]['description']
node['timestamp'] = cred[1]['weightedGraph'][1]['graphJSON'][1]['nodes'][i]['timestampMs']
node['user'] = ''
if (node['address'][2] == 'IDENTITY'):
node['user'] = cred[1]['weightedGraph'][1]['graphJSON'][1]['nodes'][i]['description']
node['node_type'] = 'user'
else:
node['node_type'] = node['address'][2]
nodes.append(node)
# --------------------------- Write csv file --------------------------
import csv
toCSV = nodes
with open(filename, 'w', encoding='utf8', newline='') as output_file:
fc = csv.DictWriter(output_file,
fieldnames=toCSV[0].keys(),
)
fc.writeheader()
fc.writerows(toCSV)
# {# -------------------------- Set dates time-filtering ------------------------
# # start_date = '2018/11/10 18:56:36'
# start_date = '2020/12/20 00:00:00'
# end_date = '2020/12/27 00:00:00'
# start_datetime = datetime.datetime.strptime(start_date, '%Y/%m/%d %H:%M:%S')
# end_datetime = datetime.datetime.strptime(end_date, '%Y/%m/%d %H:%M:%S')
# num_nodes = len(cred[1]['credData']['nodeSummaries'])
# nodes = []
# for i in range(num_nodes):
# # if (cred[1]['weightedGraph'][1]['graphJSON'][1]['sortedNodeAddresses'][i][1] == 'discourse'):
# node = {}
# node['address'] = cred[1]['weightedGraph'][1]['graphJSON'][1]['sortedNodeAddresses'][i]
# node['cred'] = cred[1]['credData']['nodeSummaries'][i]['cred']
# if cred[1]['credData']['nodeOverTime'][i] is None:
# node ['credOverTime'] = []
# else:
# node ['credOverTime'] = cred[1]['credData']['nodeOverTime'][i]['cred']
# node['description'] = cred[1]['weightedGraph'][1]['graphJSON'][1]['nodes'][i]['description']
# node['timestamp'] = cred[1]['weightedGraph'][1]['graphJSON'][1]['nodes'][i]['timestampMs']
# node['user'] = ''
# if (node['address'][2] == 'IDENTITY'):
# node['user'] = cred[1]['weightedGraph'][1]['graphJSON'][1]['nodes'][i]['description']
# nodes.append(node)
# # --------- ----------------- filter nodes by type --------------------------
# nodes_filt = [ node for node in nodes if (node['address'][2]== 'post') ]
# # nodes_filt = [ node for node in cred[1]["orderedNodes"] if (node['address'][2] == 'topic' )] # Filter by topic
# # nodes_filt = [ node for node in cred[1]["orderedNodes"] if (node['address'][2] == 'topic' or node['address'][2]== 'post') ] # Filter by topic AND post
# # nodes_filt = cred[1]["orderedNodes"] # No filter (pass through..)
# # --------------------------- filter nodes by dateTime --------------------------
# nodes_time_filt = [ node for node in nodes_filt if (datetime.datetime.fromtimestamp(node['timestamp']/1000 ) >= \
# start_datetime and datetime.datetime.fromtimestamp(node['timestamp']/1000 ) <= end_datetime) ]
# # --------------------- sort nodes by cred (created LAST interval) ------------
# nodes_sorted3 = sorted(nodes_time_filt, key=lambda e: e['cred'], reverse=True)
# table_row_contrib = []
# # pring top 10 cred
# for i in range(10):
# table_row_contrib.append([truncate(nodes_sorted3[i]['cred'],1), nodes_sorted3[i]['description'],'user'])
# print(nodes_sorted3[i]['description'])
# print("cred: " + str(truncate(nodes_sorted3[i]['cred'],1)))
# # Print markdown table showing top contributions ranked by Cred
# print(tabulate(table_row_contrib, ["Cred", "Contribution", "Author"], tablefmt="github"))
# # ---------------- sort by Cred earned in specified time interval (contribution created any time) --------------------
# nodes_filt = [ node for node in nodes if (node['address'][2] == 'IDENTITY' )]
# num_intervals = len(nodes[0]['credOverTime'])
# intervals = cred[1]['credData']['intervals']#['intervalEnds']#[-span:-1]
# interval = num_intervals - 1
# days_l = 5 #3
# nodes_sorted4 = sorted(nodes_filt, key=lambda e: e['credOverTime'][interval-1]*(days_l/7) + e['credOverTime'][interval]*((7-days_l)/7) , reverse=True)
# table_row_user = []
# # Calculating top 10 Cred contributions
# for i in range(10):
# cred_node = truncate(nodes_sorted4[i]['credOverTime'][interval-1]*(days_l/7) + nodes_sorted4[i]['credOverTime'][interval]*((7-days_l)/7),1)
# table_row_user.append([cred_node, nodes_sorted4[i]['description']])
# # Print markdown table showing Cred by contributor
# print(tabulate(table_row_user, ["Cred", "Contributor"], tablefmt="github"))
# # ------------------------- Plot top users Cred over time --------------------
# # filter user nodes
# nodes_filt = [ node for node in nodes if (node['address'][2] == 'IDENTITY') ]
# span = 7 #6
# num_display = 10
# nodes_sorted5 = sorted(nodes_filt, key=lambda e: sum(e['credOverTime'][-span:-1]), reverse=True)
# top_nodes = nodes_sorted5[:num_display]
# intervals = cred[1]['credData']['intervals'][-span:-1] #['endTimeMs']# ['intervalEnds'][-span:-1]
# EndDateTime = [] # Get datetime of intervals (end)
# for i in range(len(intervals)):
# EndDateTime.append(datetime.datetime.fromtimestamp(intervals[i]['endTimeMs']/1000 ))
# for i in range(num_display):
# # plt.plot(EndDateTime,top_nodes[i]['credOverTime'][-span:], label=top_nodes[i]['address'][4])
# plt.plot(EndDateTime,top_nodes[i]['credOverTime'][-span:-1], label=top_nodes[i]['description'])
# plt.xlabel('date')
# plt.ylabel('Cred')
# plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
# # plt.gca().xaxis.set_major_locator(mdates.DayLocator())
# plt.gcf().autofmt_xdate()
# plt.legend()
# plt.show()
# # ------------------------- Plot total Cred over time --------------------
# # filter user nodes
# nodes_filt = [ node for node in nodes if (node['address'][2] == 'IDENTITY') ]
# # create interval date array
# intervals = cred[1]['credData']['intervals'][:-1]
# EndDateTime = [] # Get datetime of intervals (end)
# for i in range(len(intervals)):
# EndDateTime.append(datetime.datetime.fromtimestamp(intervals[i]['endTimeMs']/1000 ))
# interval_sum = []
# for i in range(len(intervals)):
# interval_cred = 0
# for j in range(len(nodes_filt)):
# interval_cred = interval_cred + nodes_filt[j]['credOverTime'][i]
# interval_sum.append(interval_cred)
# # plt.title('Maker Discourse')
# plt.xlabel('Date', size=12)
# plt.ylabel('Cred', size=12)
# plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
# # plt.gca().xaxis.set_major_locator(mdates.DayLocator())
# plt.plot(EndDateTime,interval_sum)
# plt.gcf().autofmt_xdate()
# plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%y'))
# plt.show()
# # --------------- Top new contributors (in time period) ------------
# # -------------------------- Set dates time-filtering ------------------------
# # # start_date = '2018/11/10 18:56:36'
# # start_date = '2020/09/22 00:00:00'
# # end_date = '2020/11/22 00:00:00'
# # start_datetime = datetime.datetime.strptime(start_date, '%Y/%m/%d %H:%M:%S')
# # end_datetime = datetime.datetime.strptime(end_date, '%Y/%m/%d %H:%M:%S')
# # nodes_filt = [ node for node in nodes if (node['address'][2]== 'post') ]
# # nodes_filt = [ node for node in cred[1]["orderedNodes"] if (node['address'][2] == 'topic' )] # Filter by topic
# # nodes_filt = [ node for node in cred[1]["orderedNodes"] if (node['address'][2] == 'topic' or node['address'][2]== 'post') ] # Filter by topic AND post
# # nodes_filt = cred[1]["orderedNodes"] # No filter (pass through..)
# nodes_filt = [ node for node in nodes if (node['address'][2] == 'IDENTITY') ]
# num_intervals = len(nodes[0]['credOverTime'])
# intervals = cred[1]['credData']['intervals']#['intervalEnds']#[-span:-1]
# interval = num_intervals - 1
# span = 6 #6
# num_display = 10
# nodes_filt_new = []
# for i in range(len(nodes_filt)):
# if sum(nodes_filt[i]['credOverTime'][:(num_intervals-span)]) == 0: # if new user (1st Cred received in last 'span' weeks)
# nodes_filt_new.append(nodes_filt[i])
# nodes_sorted5 = sorted(nodes_filt_new, key=lambda e: sum(e['credOverTime'][-span:-1]), reverse=True)
# top_nodes = nodes_sorted5[:num_display]
# intervals = cred[1]['credData']['intervals'][-span:-1] #['endTimeMs']# ['intervalEnds'][-span:-1]
# EndDateTime = [] # Get datetime of intervals (end)
# for i in range(len(intervals)):
# EndDateTime.append(datetime.datetime.fromtimestamp(intervals[i]['endTimeMs']/1000 ))
# for i in range(num_display):
# # plt.plot(EndDateTime,top_nodes[i]['credOverTime'][-span:], label=top_nodes[i]['address'][4])
# plt.plot(EndDateTime,top_nodes[i]['credOverTime'][-span:-1], label=top_nodes[i]['description'])
# plt.xlabel('date')
# plt.ylabel('Cred')
# plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
# # plt.gca().xaxis.set_major_locator(mdates.DayLocator())
# plt.gcf().autofmt_xdate()
# plt.legend()
# plt.show()
# # ------------------------- Plot specific user's Cred over time --------------------
# # filter user nodes
# nodes_filt = [ node for node in nodes if (node['address'][2] == 'IDENTITY') ]
# user = 'g-dip'
# # for i in range(len(nodes_filt)):
# # if nodes_filt[i]['user'] == user:
# # print(nodes_filt[i])
# # print(nodes_filt[i]['user'])
# user_node = [ nodes_filt for nodes_filt in nodes_filt if (nodes_filt['user'] == user) ]
# span = 55 #6
# intervals = cred[1]['credData']['intervals'][-span:-1]
# EndDateTime = [] # Get datetime of intervals (end)
# for i in range(len(intervals)):
# EndDateTime.append(datetime.datetime.fromtimestamp(intervals[i]['endTimeMs']/1000 ))
# plt.plot(EndDateTime,user_node[0]['credOverTime'][-span:-1], label=user_node[0]['description'])
# plt.show()
# span = 7 #6
# num_display = 10
# nodes_sorted5 = sorted(nodes_filt, key=lambda e: sum(e['credOverTime'][-span:-1]), reverse=True)
# top_nodes = nodes_sorted5[:num_display]
# intervals = cred[1]['credData']['intervals'][-span:-1] #['endTimeMs']# ['intervalEnds'][-span:-1]
# EndDateTime = [] # Get datetime of intervals (end)
# for i in range(len(intervals)):
# EndDateTime.append(datetime.datetime.fromtimestamp(intervals[i]['endTimeMs']/1000 ))
# for i in range(num_display):
# # plt.plot(EndDateTime,top_nodes[i]['credOverTime'][-span:], label=top_nodes[i]['address'][4])
# plt.plot(EndDateTime,top_nodes[i]['credOverTime'][-span:-1], label=top_nodes[i]['description'])
# plt.xlabel('date')
# plt.ylabel('Cred')
# plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
# # plt.gca().xaxis.set_major_locator(mdates.DayLocator())
# plt.gcf().autofmt_xdate()
# plt.legend()
# plt.show()}