Repository navigation
Expand file tree
/
Copy pathcontinousConsensus.py
More file actions
311 lines (208 loc) · 7.98 KB
/
Copy pathcontinousConsensus.py
File metadata and controls
311 lines (208 loc) · 7.98 KB
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
import numpy as np
from numpy.core.fromnumeric import shape
import pandas as pd
from polis_data_processing import count_majority_vote
from matplotlib import pyplot as plt
import preprocessing as pre
import os
'''
Takes in the data of one of the openData provided by polis and continuously after each vote updates the consensus
and creates a plot out of this development.
'''
TO_CSV = False
def cum_mean(df,path):
sorted_votes = df[:,2]
sorted_votes = (sorted_votes == 1)
print("mean", np.mean(sorted_votes))
cum_sum = np.cumsum(sorted_votes)
cum_sum_mean = cum_sum / np.arange(1, len(sorted_votes)+1)
start_point = 50
cum_sum_mean = cum_sum_mean[start_point:]
len_votes = len(cum_sum_mean)
idx = np.arange(start_point, start_point + len_votes)
if TO_CSV:
a = pd.DataFrame(columns= ["n_vote", "mean"])
for i in range(len_votes):
a = a.append({"n_vote": i, "mean": cum_sum_mean[i] }, ignore_index = True)
a.to_csv("data/regression_data/mean/"+path+".csv")
plt.plot(idx, cum_sum_mean)
plt.xlabel("votes")
plt.ylabel("% of agreement")
# plt.set_dpi(300)
plt.savefig("figures/mean/"+path+".png")
plt.close()
def cum_std(df, path):
# number of different questions
n_comment = max(df[:,0]) + 1
# number of votes for each question
n_votes_comment = np.zeros(int(n_comment))
# the consensus updated after each time step
out_consensus = []
## array used to keep track of decisions taken
restructured_decisions = []
# creates a list of np arrays which then will include the decisions which are included
for i in range(n_comment):
restructured_decisions.append(np.zeros(0))
min_votes = 2
consensus_comment = np.zeros(n_comment)
# for each row in the sorted df we check the
for row in df:
comment_id = row[0]
vote = row[2]
n_votes_comment[comment_id] += 1
votes_on_comment_id = restructured_decisions[comment_id]
restructured_decisions[comment_id] = np.append(votes_on_comment_id, vote)
# if there are more than 2 votes for comment x the std gets updated and further investigated
if n_votes_comment[comment_id] > min_votes:
std_comment = np.std(restructured_decisions[comment_id])
consensus_comment[comment_id] = std_comment
else:
continue
consensus = 0
sum_decisions = sum(n_votes_comment)
if sum_decisions > 2:
for i in range(len(n_votes_comment)):
consensus += n_votes_comment[i] * consensus_comment[i] / sum_decisions
out_consensus.append(consensus)
out_consensus = out_consensus[50:]
plt.plot(out_consensus)
plt.xlabel('votes')
plt.ylabel('standard deviation of votes')
plt.savefig("figures/std/" + path + ".pdf")
plt.close()
if TO_CSV:
len_votes = len(out_consensus)
a = pd.DataFrame(columns= ["n_vote", "std"])
for i in range(len_votes):
a = a.append({"n_vote": i, "std": out_consensus[i] }, ignore_index = True)
a.to_csv("data/regression_data/std/"+path+".csv")
def cum_own_metric(df,path):
# number of different questions
n_comment = max(df[:,0]) + 1
print(n_comment)
# number of votes for each question
n_votes_comment = np.zeros(int(n_comment))
# the consensus updated after each time step
out_consensus = []
## array used to keep track of decisions taken
restructured_decisions = []
# creates a list of np arrays which then will include the decisions which are included
for _ in range(n_comment):
restructured_decisions.append(np.zeros(0))
min_votes = 2
majority_comment = np.zeros(n_comment)
# for each row in the sorted df we check the
for row in df:
comment_id = row[0]
vote = row[2]
n_votes_comment[comment_id] += 1
votes_on_comment_id = restructured_decisions[comment_id]
restructured_decisions[comment_id] = np.append(votes_on_comment_id, vote)
# if there are more than 2 votes for comment x the std gets updated and further investigated
if n_votes_comment[comment_id] > min_votes:
n_majority_vote = count_majority_vote(restructured_decisions[comment_id])
majority_comment[comment_id] = n_majority_vote
else:
continue
consensus = 0
sum_decisions = sum(n_votes_comment)
if sum_decisions > 2:
arr = n_votes_comment[n_votes_comment > 2]
n_all_considered_votes = sum(arr)
consensus = sum(majority_comment) / n_all_considered_votes
out_consensus.append(consensus)
out_consensus = out_consensus[50:]
plt.plot(out_consensus)
plt.xlabel('votes')
plt.ylabel('metric of votes ')
plt.savefig("figures/own_metric/" + path + ".pdf")
plt.close()
if TO_CSV:
len_votes = len(out_consensus)
a = pd.DataFrame(columns= ["n_vote", "own_metric"])
for i in range(len_votes):
a = a.append({"n_vote": i, "own_metric": out_consensus[i] }, ignore_index = True)
a.to_csv("data/regression_data/own_metric/"+path+".csv")
return out_consensus
def mean_cum_own_metric():
directory ='../polis/openData'
sub_dir = next(os.walk(directory))[1]
all_data = np.zeros([1,15000])
for sub in sub_dir:
if sub == '.git':
continue
complete = directory + "/" + sub
df = pd.read_csv(complete + "/votes.csv")
df = df.sort_values(by = 'timestamp')
df = df.drop(['datetime'], axis = 1)
df = df.drop(['timestamp'], axis = 1)
df = df.values
if df.shape[0] > 20000:
df = df[:20000,:]
own_data = cum_own_metric(df, sub)
own_data = np.array(own_data)
own_data = np.array(own_data)
print("shape before increasing modifying size", own_data.shape[0])
if own_data.shape[0] < 15000:
add_lenth = 15000 - own_data.shape[0]
empty_array = np.empty(add_lenth)
# empty_array[:] = np.nan
empty_array[:] = own_data[-1]
own_data = np.append(own_data, empty_array)
else:
print("ELSE")
own_data = own_data[:15000]
own_data = np.reshape(own_data, [1,15000])
print(all_data.shape)
all_data = np.append(all_data, own_data, axis=0)
print(all_data)
all_data = np.delete(all_data, (0), axis = 1)
print(all_data.shape)
print(all_data)
mean_data = np.nanmean(all_data,axis = 0)
print(mean_data.shape)
plt.plot(mean_data)
plt.xlabel("votes")
plt.ylabel("% of people in the majority")
plt.savefig("tmp/mean_own_metric_all_data.pdf")
plt.close()
pass
def analyse_polis():
sub_dir = pre.get_all_sub_dir()
for sub in sub_dir:
fromPolis = True
df, path = pre.preprossessing(fromPolis, False, sub)
cum_mean(df,path)
# cum_std(df,path)
# cum_own_metric(df,path)
# mean_cum_own_metric()
def analyse_own():
id = 82
sub_dir = pre.get_all_sub_dir()
for sub in sub_dir:
fromPolis = False
df, path = pre.preprossessing(fromPolis, False, sub)
cum_mean(df,path)
# cum_std(df,path)
# cum_own_metric(df,path)
def test():
df = pd.read_csv("tmp/vote_hist_american-assembly.bowling-green.csv")
df.columns = ['idx', 'comment-id', 'voter-id', 'vote']
df = df.drop('idx' ,axis=1)
df = df.values
df = df.astype(int)
path = "test"
cum_mean(df,path)
cum_std(df,path)
cum_own_metric(df,path)
if __name__ == '__main__':
# analyse_polis()
# analyse_own()
a = pd.read_csv('data/1th/0/vote_hist_100.csv')
data = a.drop(a.columns[0], axis=1).values
data = data.astype(int)
print(data)
cum_mean(data, "test123")
cum_std(data,"test123")
cum_own_metric(data,"test123")
pass