20
20
21
21
def update_progress_bar (method ):
22
22
"""A decorator to update a progress bar if it exists"""
23
- def wrapper (* args ):
23
+ def wrapper (* args , ** kwargs ):
24
24
"""Run the method and update the progress bar if it exists"""
25
- output = method (* args )
25
+ output = method (* args , ** kwargs )
26
26
27
27
try :
28
28
args [0 ].progress_bar .update (1 )
@@ -89,13 +89,32 @@ def _reshape_out(self,
89
89
set the corresponding attributes.
90
90
"""
91
91
92
- self .payoffs = self ._build_payoffs (mean_per_reps_player_opponent_df ["Score per turn" ])
93
- self .score_diffs = self ._build_score_diffs (mean_per_reps_player_opponent_df ["Score difference per turn" ])
94
- self .match_lengths = self ._build_match_lengths (mean_per_reps_player_opponent_df ["Turns" ])
92
+ self .payoffs = self ._reshape_three_dim_list (
93
+ mean_per_reps_player_opponent_df ["Score per turn" ],
94
+ first_dimension = range (self .num_players ),
95
+ second_dimension = range (self .num_players ),
96
+ third_dimension = range (self .repetitions ),
97
+ key_order = [2 , 0 , 1 ])
98
+
99
+ self .score_diffs = self ._reshape_three_dim_list (
100
+ mean_per_reps_player_opponent_df ["Score difference per turn" ],
101
+ first_dimension = range (self .num_players ),
102
+ second_dimension = range (self .num_players ),
103
+ third_dimension = range (self .repetitions ),
104
+ key_order = [2 , 0 , 1 ],
105
+ alternative = 0 )
106
+
107
+ self .match_lengths = self ._reshape_three_dim_list (
108
+ mean_per_reps_player_opponent_df ["Turns" ],
109
+ first_dimension = range (self .repetitions ),
110
+ second_dimension = range (self .num_players ),
111
+ third_dimension = range (self .num_players ),
112
+ alternative = 0 )
113
+
114
+ self .wins = self ._reshape_two_dim_list (sum_per_player_repetition_df ["Win" ])
115
+ self .scores = self ._reshape_two_dim_list (sum_per_player_repetition_df ["Score" ])
116
+ self .normalised_scores = self ._reshape_two_dim_list (normalised_scores_series )
95
117
96
- self .wins = self ._build_wins (sum_per_player_repetition_df ["Win" ])
97
- self .scores = self ._build_scores (sum_per_player_repetition_df ["Score" ])
98
- self .normalised_scores = self ._build_normalised_scores (normalised_scores_series )
99
118
self .cooperation = self ._build_cooperation (sum_per_player_opponent_df ["Cooperation count" ])
100
119
self .good_partner_matrix = self ._build_good_partner_matrix (sum_per_player_opponent_df ["Good partner" ])
101
120
@@ -121,125 +140,76 @@ def _reshape_out(self,
121
140
self .normalised_cooperation = self ._build_normalised_cooperation ()
122
141
self .ranking = self ._build_ranking ()
123
142
self .ranked_names = self ._build_ranked_names ()
124
- self .payoff_matrix = self ._build_payoff_matrix ()
125
- self .payoff_stddevs = self ._build_payoff_stddevs ()
143
+
144
+ self .payoff_matrix = self ._build_summary_matrix (self .payoffs )
145
+ self .payoff_stddevs = self ._build_summary_matrix (self .payoffs ,
146
+ func = np .std )
147
+
126
148
self .payoff_diffs_means = self ._build_payoff_diffs_means ()
127
149
self .cooperating_rating = self ._build_cooperating_rating ()
128
150
self .vengeful_cooperation = self ._build_vengeful_cooperation ()
129
151
self .eigenjesus_rating = self ._build_eigenjesus_rating ()
130
152
self .eigenmoses_rating = self ._build_eigenmoses_rating ()
131
153
132
154
@update_progress_bar
133
- def _build_payoffs (self , payoffs_series ):
155
+ def _reshape_three_dim_list (self , series ,
156
+ first_dimension ,
157
+ second_dimension ,
158
+ third_dimension ,
159
+ alternative = None ,
160
+ key_order = [0 , 1 , 2 ]):
134
161
"""
135
162
Parameters
136
163
----------
137
164
138
165
payoffs_series : pandas.Series
166
+ first_dimension : iterable
167
+ second_dimension : iterable
168
+ third_dimension : iterable
169
+ alternative : int
170
+ What to do if there is no entry at given position
171
+ key_order : list
172
+ Indices re-ording the dimensions to the correct keys in the
173
+ series
139
174
140
175
Returns:
141
176
--------
142
- The mean of per turn payoffs.
143
- List of the form:
144
- [ML1, ML2, ML3..., MLn]
145
- Where n is the number of players and MLi is a list of the form:
146
- [pi1, pi2, pi3, ..., pim]
147
- Where m is the number of players and pij is a list of the form:
148
- [uij1, uij2, ..., uijk]
149
- Where k is the number of repetitions and u is the mean utility (over
150
- all repetitions) obtained by player i against player j.
177
+ A three dimensional list across the three dimensions
151
178
"""
152
- payoffs_dict = dict ( payoffs_series )
153
- payoffs = []
154
- for player_index in range ( self . num_players ) :
179
+ series_dict = series . to_dict ( )
180
+ output = []
181
+ for first_index in first_dimension :
155
182
matrix = []
156
- for opponent_index in range ( self . num_players ) :
183
+ for second_index in second_dimension :
157
184
row = []
158
- for repetition in range (self .repetitions ):
159
- key = (repetition , player_index , opponent_index )
160
- if key in payoffs_dict :
161
- row .append (payoffs_dict [key ])
185
+ for third_index in third_dimension :
186
+ key = (first_index , second_index , third_index )
187
+ key = tuple ([key [order ] for order in key_order ])
188
+ if key in series_dict :
189
+ row .append (series_dict [key ])
190
+ elif alternative is not None :
191
+ row .append (alternative )
162
192
matrix .append (row )
163
- payoffs .append (matrix )
164
- return payoffs
193
+ output .append (matrix )
194
+ return output
165
195
166
196
@update_progress_bar
167
- def _build_score_diffs (self , payoff_diffs_series ):
197
+ def _reshape_two_dim_list (self , series ):
168
198
"""
169
199
Parameters
170
200
----------
171
201
172
- payoffs_diffs_series : pandas.Series
202
+ series : pandas.Series
173
203
174
204
Returns:
175
205
--------
176
- The mean of per turn payoff differences
177
- List of the form:
178
- [ML1, ML2, ML3..., MLn]
179
- Where n is the number of players and MLi is a list of the form:
180
- [pi1, pi2, pi3, ..., pim]
181
- Where m is the number of players and pij is a list of the form:
182
- [uij1, uij2, ..., uijk]
183
- Where k is the number of repetitions and u is the mean utility
184
- difference (over all repetitions) obtained by player i against
185
- player j.
206
+ A two dimensional list across repetitions and opponents
186
207
"""
187
-
188
- payoff_diffs_dict = payoff_diffs_series .to_dict ()
189
- score_diffs = []
190
- for player_index in range (self .num_players ):
191
- matrix = []
192
- for opponent_index in range (self .num_players ):
193
- row = []
194
- for repetition in range (self .repetitions ):
195
- row .append (payoff_diffs_dict .get ((repetition ,
196
- player_index ,
197
- opponent_index ,
198
- ), 0 ))
199
- matrix .append (row )
200
- score_diffs .append (matrix )
201
- return score_diffs
202
-
203
- @update_progress_bar
204
- def _build_match_lengths (self , length_series ):
205
- length_dict = dict (length_series )
206
- match_lengths = []
207
- for repetition in range (self .repetitions ):
208
- matrix = []
209
- for player_index in range (self .num_players ):
210
- row = []
211
- for opponent_index in range (self .num_players ):
212
- row .append (length_dict .get ((repetition ,
213
- player_index ,
214
- opponent_index ), 0 ))
215
- matrix .append (row )
216
- match_lengths .append (matrix )
217
- return match_lengths
218
-
219
- @update_progress_bar
220
- def _build_wins (self , wins_series ):
221
- wins_dict = wins_series .to_dict ()
222
- wins = [[wins_dict .get ((player_index , repetition ), 0 )
208
+ series_dict = series .to_dict ()
209
+ out = [[series_dict .get ((player_index , repetition ), 0 )
223
210
for repetition in range (self .repetitions )]
224
211
for player_index in range (self .num_players )]
225
- return wins
226
-
227
- @update_progress_bar
228
- def _build_scores (self , scores_series ):
229
- scores_dict = scores_series .to_dict ()
230
- scores = [[scores_series .get ((player_index , repetition ), 0 )
231
- for repetition in range (self .repetitions )]
232
- for player_index in range (self .num_players )]
233
- return scores
234
-
235
- @update_progress_bar
236
- def _build_normalised_scores (self , normalised_scores_series ):
237
- normalised_scores_dict = normalised_scores_series .to_dict ()
238
- normalised_scores = [[normalised_scores_series .get ((player_index ,
239
- repetition ), 0 )
240
- for repetition in range (self .repetitions )]
241
- for player_index in range (self .num_players )]
242
- return normalised_scores
212
+ return out
243
213
244
214
@update_progress_bar
245
215
def _build_cooperation (self , cooperation_series ):
@@ -258,7 +228,7 @@ def _build_cooperation(self, cooperation_series):
258
228
259
229
@update_progress_bar
260
230
def _build_good_partner_matrix (self , good_partner_series ):
261
- good_partner_dict = dict ( good_partner_series )
231
+ good_partner_dict = good_partner_series . to_dict ( )
262
232
good_partner_matrix = []
263
233
for player_index in range (self .num_players ):
264
234
row = []
@@ -273,35 +243,19 @@ def _build_good_partner_matrix(self, good_partner_series):
273
243
good_partner_matrix .append (row )
274
244
return good_partner_matrix
275
245
276
-
277
246
@update_progress_bar
278
- def _build_payoff_matrix (self ):
279
- payoff_matrix = [[0 for opponent_index in range (self .num_players )]
280
- for player_index in range (self .num_players )]
281
-
282
- pairs = itertools .product (range (self .num_players ), repeat = 2 )
283
-
284
- for player_index , opponent_index in pairs :
285
- utilities = self .payoffs [player_index ][opponent_index ]
286
- if utilities :
287
- payoff_matrix [player_index ][opponent_index ] = np .mean (utilities )
288
-
289
- return payoff_matrix
290
-
291
- @update_progress_bar
292
- def _build_payoff_stddevs (self ):
293
- payoff_stddevs = [[0 for opponent_index in range (self .num_players )]
294
- for player_index in range (self .num_players )]
247
+ def _build_summary_matrix (self , attribute , func = np .mean ):
248
+ matrix = [[0 for opponent_index in range (self .num_players )]
249
+ for player_index in range (self .num_players )]
295
250
296
251
pairs = itertools .product (range (self .num_players ), repeat = 2 )
297
252
298
253
for player_index , opponent_index in pairs :
299
- utilities = self . payoffs [player_index ][opponent_index ]
254
+ utilities = attribute [player_index ][opponent_index ]
300
255
if utilities :
301
- payoff_stddevs [player_index ][opponent_index ] = np .std (utilities )
302
-
303
- return payoff_stddevs
256
+ matrix [player_index ][opponent_index ] = func (utilities )
304
257
258
+ return matrix
305
259
306
260
@update_progress_bar
307
261
def _build_payoff_diffs_means (self ):
0 commit comments