-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpca.py
executable file
·96 lines (86 loc) · 2.58 KB
/
wpca.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
#!/usr/bin/env python3
import csv
import json
import operator
import numpy
import requests
import sys
import rpy2.robjects as robjects
r=robjects.r
#exec(open("load_datapackage.py").read())
url = sys.argv[1]
req = requests.get(url)
data = req.json()
# reorder for easier access:
people = {}
for person in data["people"]:
people[person['id']] = person
vote_events = {}
for vote_event in data["vote_events"]:
vote_events[vote_event['id']] = vote_event
# load into row vector in R
Xsourcevector = []
for row in data["votes"]:
Xsourcevector.append(row["vote_event_id"])
Xsourcevector.append(row["voter_id"])
Xsourcevector.append(row["option"])
robjects.globalenv["X_source_vector"] = robjects.StrVector(Xsourcevector)
r.source("prepare_matrix.r")
# calculate WPCA
r.source("wpca_script.r")
# put into object
wpca = {"people":[]}
i = 0 # all people
k = 0 # cutted people
rXproj = numpy.array(robjects.globalenv['X_proj_unit'])
rXpeople = robjects.globalenv['X_people']
rXvote_events = robjects.globalenv['X_vote_events']
for item in robjects.globalenv['person_I']:
if item:
it = {
'id': people[rXpeople[i]]['id'],
'name': people[rXpeople[i]]['name'],
'party': people[rXpeople[i]]['party'],
'wpca:d1': rXproj[k,0],
'wpca:d2': rXproj[k,1]
}
try:
it['color'] = people[rXpeople[i]]['color']
except:
nothing = 0
wpca['people'].append(it)
k = k + 1
i = i + 1
# CUTTING LINES
wpca['vote_events'] = []
try:
if sys.argv[2] == 'yes':
# modulo
try:
robjects.globalenv["modulo"] = int(sys.argv[3])
except:
robjects.globalenv["modulo"] = 1
# calculate
r.source("wpca_cutting_lines_script.r")
# put into object
rcl = numpy.array(robjects.globalenv['cl'])
i = 0 # all vote events
for ve in rcl:
rcl_li = list(rcl[i])
it = {
'normal_x': rcl_li[0],
'normal_y': rcl_li[1],
'cl_beta0': rcl_li[2],
'loss': rcl_li[3],
'w1': rcl_li[4],
'w2': rcl_li[5],
'id': vote_events[rXvote_events[i]]["id"],
'motion:name': vote_events[rXvote_events[i]]["motion:name"],
'start_date': vote_events[rXvote_events[i]]["start_date"],
'result': vote_events[rXvote_events[i]]["result"]
}
wpca['vote_events'].append(it)
i = i + 1
except:
nothing = None
print(json.dumps(wpca))