-
Notifications
You must be signed in to change notification settings - Fork 19
/
clr.py
243 lines (189 loc) · 6.2 KB
/
clr.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
# -*- coding: utf-8 -*-
"""Define the Grants CLR calculations configuration.
Copyright (C) 2020 Gitcoin Core
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import numpy as np
CLR_PERCENTAGE_DISTRIBUTED = 0
GRANT_CONTRIBUTIONS_EXAMPLE = [
{
'id': '4',
'contributions': [
{ '1': 10.0 },
{ '2': 5.0 },
{ '2': 10.0 },
{ '3': 7.0 },
{ '5': 5.0 },
{ '4': 10.0 },
{ '5': 5.0 },
{ '5': 5.0 }
]
},
{
'id': '5',
'contributions': [
{ '1': 10.0 },
{ '1': 5.0 },
{ '2': 20.0 },
{ '3': 3.0 },
{ '8': 2.0 },
{ '9': 10.0 },
{ '7': 7.0 },
{ '2': 5.0 }
]
}
]
'''
translates django grant data structure to a list of lists
args:
django grant data structure
{
'id': (string) ,
'contibutions' : [
{
contributor_profile (str) : summed_contributions
}
]
}
returns:
list of lists of grant data
[[grant_id (str), user_id (str), contribution_amount (float)]]
'''
def translate_data(grants_data):
grants_list = []
for g in grants_data:
grant_id = g.get('id')
for c in g.get('contributions'):
val = [grant_id] + [list(c.keys())[0], list(c.values())[0]]
grants_list.append(val)
return grants_list
'''
aggregates contributions by contributor
args:
list of lists of grant data
[[grant_id (str), user_id (str), contribution_amount (float)]]
returns:
aggregated contributions by pair nested dict
{
grant_id (str): {
user_id (str): aggregated_amount (float)
}
}
'''
def aggregate_contributions(grant_contributions):
contrib_dict = {}
for proj, user, amount in grant_contributions:
if proj not in contrib_dict:
contrib_dict[proj] = {}
contrib_dict[proj][user] = contrib_dict[proj].get(user, 0) + amount
return contrib_dict
'''
gets pair totals between pairs, between current round, across all grants
args:
aggregated contributions by pair nested dict
{
grant_id (str): {
user_id (str): aggregated_amount (float)
}
}
returns:
pair totals between current round
{user_id (str): {user_id (str): pair_total (float)}}
'''
def get_totals_by_pair(contrib_dict):
tot_overlap = {}
# start pairwise match
for _, contribz in contrib_dict.items():
for k1, v1 in contribz.items():
if k1 not in tot_overlap:
tot_overlap[k1] = {}
# pairwise matches to current round
for k2, v2 in contribz.items():
if k2 not in tot_overlap[k1]:
tot_overlap[k1][k2] = 0
tot_overlap[k1][k2] += (v1 * v2) ** 0.5
return tot_overlap
'''
calculates the clr amount at the given threshold and total pot
args:
aggregated contributions by pair nested dict
{
grant_id (str): {
user_id (str): aggregated_amount (float)
}
}
pair_totals
{user_id (str): {user_id (str): pair_total (float)}}
threshold
float
total_pot
float
returns:
total clr award by grant, analytics, possibly normalized by the normalization factor
[{'id': proj, 'number_contributions': _num, 'contribution_amount': _sum, 'clr_amount': tot}]
'''
def calculate_clr(aggregated_contributions, pair_totals, threshold, total_pot):
bigtot = 0
totals = []
for proj, contribz in aggregated_contributions.items():
tot = 0
_num = 0
_sum = 0
# pairwise match
for k1, v1 in contribz.items():
_num += 1
_sum += v1
for k2, v2 in contribz.items():
if k2 > k1:
# quadratic formula
tot += ((v1 * v2) ** 0.5) / (pair_totals[k1][k2] / (threshold + 1))
if type(tot) == complex:
tot = float(tot.real)
bigtot += tot
totals.append({'id': proj, 'number_contributions': _num, 'contribution_amount': _sum, 'clr_amount': tot})
global CLR_PERCENTAGE_DISTRIBUTED
# if we reach saturation, we need to normalize
if bigtot >= total_pot:
CLR_PERCENTAGE_DISTRIBUTED = 100
for t in totals:
t['clr_amount'] = ((t['clr_amount'] / bigtot) * total_pot)
return totals
'''
clubbed function that runs all calculation functions
args:
grant_contribs_curr
{
'id': (string) ,
'contibutions' : [
{
contributor_profile (str) : contribution_amount (int)
}
]
}
threshold : float
total_pot : float
returns:
grants clr award amounts
'''
def run_clr_calcs(grant_contribs_curr, threshold, total_pot):
# get data
contrib_data = translate_data(grant_contribs_curr)
# aggregate data
agg = aggregate_contributions(contrib_data)
# get pair totals
ptots = get_totals_by_pair(agg)
# clr calcluation
totals = calculate_clr(agg, ptots, threshold, total_pot)
return totals
if __name__ == '__main__':
res = run_clr_calcs(GRANT_CONTRIBUTIONS_EXAMPLE, 25.0, 5000)
print(res)