66import matplotlib .pyplot as plt
77import numpy as np
88import tqdm
9+ import dask .dataframe as dd
10+ import dask as da
911from mpl_toolkits .axes_grid1 import make_axes_locatable
1012
1113import axelrod as axl
@@ -266,7 +268,7 @@ def construct_tournament_elements(self, step: float,
266268
267269 def fingerprint (
268270 self , turns : int = 50 , repetitions : int = 10 , step : float = 0.01 ,
269- processes : int = None , filename : str = None , in_memory : bool = False ,
271+ processes : int = None , filename : str = None ,
270272 progress_bar : bool = True
271273) -> dict :
272274 """Build and play the spatial tournament.
@@ -290,10 +292,7 @@ def fingerprint(
290292 The number of processes to be used for parallel processing
291293 filename: str, optional
292294 The name of the file for self.spatial_tournament's interactions.
293- if None and in_memory=False, will auto-generate a filename.
294- in_memory: bool
295- Whether self.spatial_tournament keeps interactions_dict in memory or
296- in a file.
295+ if None, will auto-generate a filename.
297296 progress_bar : bool
298297 Whether or not to create a progress bar which will be updated
299298
@@ -305,7 +304,7 @@ def fingerprint(
305304 """
306305
307306 temp_file_descriptor = None
308- if not in_memory and filename is None :
307+ if filename is None :
309308 temp_file_descriptor , filename = mkstemp ()
310309
311310 edges , tourn_players = self .construct_tournament_elements (
@@ -318,13 +317,10 @@ def fingerprint(
318317 self .spatial_tournament .play (build_results = False ,
319318 filename = filename ,
320319 processes = processes ,
321- in_memory = in_memory ,
322320 progress_bar = progress_bar )
323- if in_memory :
324- self .interactions = self .spatial_tournament .interactions_dict
325- else :
326- self .interactions = read_interactions_from_file (
327- filename , progress_bar = progress_bar )
321+
322+ self .interactions = read_interactions_from_file (
323+ filename , progress_bar = progress_bar )
328324
329325 if temp_file_descriptor is not None :
330326 os .close (temp_file_descriptor )
@@ -483,17 +479,21 @@ def analyse_cooperation_ratio(filename):
483479 opponent in each turn. The ith row corresponds to the ith opponent
484480 and the jth column the jth turn.
485481 """
486- did_c = np .vectorize (lambda action : int (action == 'C' ))
482+ did_c = np .vectorize (lambda actions : [int (action == 'C' )
483+ for action in actions ])
487484
488485 cooperation_rates = {}
489- with open (filename , "r" ) as f :
490- reader = csv .reader (f )
491- for row in reader :
492- opponent_index , player_history = int (row [1 ]), list (row [4 ])
493- if opponent_index in cooperation_rates :
494- cooperation_rates [opponent_index ].append (did_c (player_history ))
495- else :
496- cooperation_rates [opponent_index ] = [did_c (player_history )]
486+ df = dd .read_csv (filename )
487+ # We ignore the actions of all opponents. So we filter the dataframe to
488+ # only include the results of the player with index `0`.
489+ df = df [df ["Player index" ] == 0 ][["Opponent index" , "Actions" ]]
490+
491+ for _ , row in df .iterrows ():
492+ opponent_index , player_history = row ["Opponent index" ], row ["Actions" ]
493+ if opponent_index in cooperation_rates :
494+ cooperation_rates [opponent_index ].append (did_c (player_history ))
495+ else :
496+ cooperation_rates [opponent_index ] = [did_c (player_history )]
497497
498498 for index , rates in cooperation_rates .items ():
499499 cooperation_rates [index ] = np .mean (rates , axis = 0 )
0 commit comments