-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.py
186 lines (160 loc) · 7.32 KB
/
debug.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
from reddwarf.polis import PolisClient
from reddwarf.data_presenter import DataPresenter
import pandas as pd
from reddwarf import utils
CONVOS = {
# Topic: What were the most significant developments in tech and politics in 2018?
# 5 groups, 65 ptpts (56 grouped), 43 comments (open)
"tech-politics-2018": {
"report_id": "r2dfw8eambusb8buvecjt",
"convo_id": "6jrufhr6dp",
},
# Topic: How should we operate vehicle-for-hire, e.g. Uber, Lyft and taxis in Toronto?
# 2 groups, 47 ptpts (36 grouped), 69 comments (open)
"rideshare-toronto": {
"report_id": "r8xhmkwp6shm9yfermteh",
"convo_id": "7vampckwrh",
},
# Topic: Help us pick rules for our AI chatbot! 7/7
# 2 groups, 1_127 ptpts (1_094 grouped), 1_418 comments (closed)
"anthropic-ccai": {
"report_id": "r3rwrinr5udrzwkvxtdkj",
"convo_id": "3akt5cdsfk",
},
# Topic: How should we use open source tools in governmment?
# Test convo using xids and avatar images.
"xid-testing": {
"convo_id": "4kjz5rrrfe",
},
}
if True:
# testing representativeness calculations
report_id = CONVOS["tech-politics-2018"]["report_id"]
print(f"Loading data from report: https://pol.is/report/{report_id}")
client = PolisClient()
client.load_data(report_id=report_id)
# Generate vote matrix and run clustering
vote_matrix = client.get_matrix(is_filtered=True)
client.run_pca()
client.scale_projected_data()
client.find_optimal_k() # Find optimal number of clusters
cluster_labels = client.optimal_cluster_labels
group_count = cluster_labels.max()+1
for group_id in range(group_count):
print(f"representativeness for group {group_id}")
group_representativeness = utils.calculate_representativeness(
vote_matrix=vote_matrix,
cluster_labels=cluster_labels,
group_id=group_id,
)
print(group_representativeness)
presenter = DataPresenter(client=client)
presenter.render_optimal_cluster_figure()
if False:
# test agora method
from reddwarf.agora import run_clustering
from reddwarf.types.agora import Conversation
report_id = CONVOS["tech-politics-2018"]["report_id"]
print(f"Loading data from report: https://pol.is/report/{report_id}")
client = PolisClient()
client.load_data(report_id=report_id)
convo: Conversation = {
"id": "demo",
"votes": client.data_loader.votes_data,
}
results = run_clustering(conversation=convo)
from pprint import pprint
pprint(results)
if False:
# Render a figure with best hulls.
report_id = CONVOS["tech-politics-2018"]["report_id"]
print(f"Loading data from report: https://pol.is/report/{report_id}")
client = PolisClient()
client.load_data(report_id=report_id)
# To see the consequences of not having pass/neutral/zero votes
DO_STRIP_PASS=False
if DO_STRIP_PASS:
client.votes = []
client.load_votes_data(data=[v for v in client.data_loader.votes_data if v["vote"] != 0])
client.get_matrix(is_filtered=True)
client.run_pca()
client.scale_projected_data()
client.find_optimal_k()
presenter = DataPresenter(client=client)
presenter.render_optimal_cluster_figure()
# client.generate_figure(coord_dataframe=client.projected_data)
if False:
# Show convo with duplicate votes.
# Shareable demo: https://gist.github.com/patcon/9c1a39291cd75b23722a5379d7cfc3cc
report_id = CONVOS["tech-politics-2018"]["report_id"]
print(f"Loading data from report: https://pol.is/report/{report_id}")
client = PolisClient(is_strict_moderation=False)
client.load_data(report_id=report_id, data_source="csv_export")
client.get_matrix(is_filtered=True)
if False:
client = PolisClient()
client.load_data(conversation_id="9xxwa9jpkm")
# Reproducing this output: https://github.com/compdemocracy/openData/blob/master/london.youth.policing
# participant-votes.csv matches, but votes.csv is missing entries.
# BUG: dates for exports seemingly not matching between matrix build and vote export.
matrix_raw = client.get_matrix(is_filtered=False, cutoff=1658934741418)
participants_votes_df = client.build_participants_dataframe(matrix_raw)
print(participants_votes_df)
# This is a sanity-check for equality of a participants-votes.csv dataframe generated from raw votes vs a downloaded export.
if False:
# Generate the participants-votes dataframe from raw data
client = PolisClient(is_strict_moderation=True)
client.load_data(directory_url="https://raw.githubusercontent.com/compdemocracy/openData/refs/heads/master/london.youth.policing/")
raw_matrix = client.get_matrix(is_filtered=False)
# Drop statement columns if no votes.
raw_matrix = raw_matrix.dropna(axis="columns", how="all")
# Convert all int columns to strings for easier comparison.
raw_matrix.columns = raw_matrix.columns.astype(str)
participants_votes_generated = client.build_participants_dataframe(vote_matrix=raw_matrix)
participants_votes_generated = participants_votes_generated.join(raw_matrix)
# Remove rows for participants with zero votes.
non_voting_participant_ids = participants_votes_generated[participants_votes_generated["n_votes"] == 0].index
participants_votes_generated = participants_votes_generated.drop(index=non_voting_participant_ids)
# Generate dataframe from downloaded CSV.
col_mapper = {
"participant": "participant_id",
"group-id": "group_id",
"n-comments": "n_comments",
"n-votes": "n_votes",
"n-agree": "n_agree",
"n-disagree": "n_disagree",
}
participants_votes_downloaded = (
pd
.read_csv("https://raw.githubusercontent.com/compdemocracy/openData/refs/heads/master/london.youth.policing/participants-votes.csv")
.rename(columns=col_mapper)
# Override group_id for now, until we can generate and compare.
.assign(group_id=None)
.set_index("participant_id")
.sort_index()
)
is_dataframes_equal = participants_votes_downloaded.equals(participants_votes_generated)
print(f"downloaded and generated dataframes are equal? {is_dataframes_equal}")
print(participants_votes_downloaded.shape)
print(participants_votes_generated.shape)
print(participants_votes_downloaded.compare(participants_votes_generated))
if False:
client = PolisClient()
# client.load_data(report_id=CONVOS["rideshare-toronto"]["report_id"])
client.load_data(report_id=CONVOS["tech-politics-2018"]["report_id"])
# client.load_data(conversation_id=CONVOS["anthropic-ccai"]["convo_id"])
matrix_raw = client.get_matrix(is_filtered=False)
client.matrix = None # Flush matrix
matrix_filtered = client.get_matrix(is_filtered=True)
matrix_filtered_imputed = client.impute_missing_votes()
presenter = DataPresenter()
# presenter.generate_vote_heatmap(matrix_raw)
presenter.generate_vote_heatmap(matrix_filtered)
# presenter.generate_vote_heatmap(matrix_filtered_imputed)
if False:
client = PolisClient()
client.load_data(conversation_id=CONVOS["xid-testing"]["convo_id"])
xids = [12334552, 12334553, 12334554, "foobar"]
mappings = client.data_loader.fetch_xid_to_pid_mappings(xids)
for xid, pid in mappings.items():
print(f"{pid=} => {xid=}")