@@ -707,12 +707,36 @@ def __ne__(self, other):
707
707
708
708
class ResultSetFromFile (ResultSet ):
709
709
"""
710
- Proof of concept for a result set that loops over the data file twice .
710
+ Read the result set directly from file.
711
711
"""
712
712
713
713
def __init__ (self , filename , progress_bar = True ,
714
714
num_interactions = False , players = False , nrepetitions = False ,
715
715
game = None , keep_interactions = False ):
716
+ """
717
+ Parameters
718
+ ----------
719
+ filename : string
720
+ the file from which to read the interactions
721
+ progress_bar : bool
722
+ Whether or not to create a progress bar which will be updated
723
+ num_interactions : int
724
+ The number of interactions in the file. Used for the progress
725
+ bar. If not known but progress_bar is true, will be efficiently
726
+ read from file.
727
+ players : list
728
+ A list of the names of players. If not known will be efficiently
729
+ read from file.
730
+ nrepetitions : int
731
+ The number of repetitions of each match. If not know will be
732
+ efficiently read from file.
733
+ game : axelrod.Game
734
+ The particular game that should be used to calculate the scores.
735
+ keep_interactions : bool
736
+ Whether or not to load the interactions in to memory. WARNING:
737
+ for large tournaments this drastically increases the memory
738
+ required.
739
+ """
716
740
if game is None :
717
741
self .game = Game ()
718
742
else :
@@ -732,19 +756,34 @@ def __init__(self, filename, progress_bar=True,
732
756
keep_interactions = keep_interactions )
733
757
734
758
def create_progress_bar (self , desc = None ):
759
+ """
760
+ Create a progress bar for a read through of the data file.
761
+
762
+ Parameters
763
+ ----------
764
+ desc : string
765
+ A description.
766
+ """
735
767
if not self .num_interactions :
736
768
self .num_interactions = sum (1 for line in open (self .filename ))
737
769
return tqdm .tqdm (total = self .num_interactions , desc = desc )
738
770
739
771
def _read_players_and_repetition_numbers (self , progress_bar = False ):
740
- """Read the players and the repetitions numbers"""
772
+ """
773
+ Read the players and the repetitions numbers
774
+
775
+ Parameters
776
+ ----------
777
+ progress_bar : bool
778
+ Whether or not to display a progress bar
779
+ """
741
780
742
781
if progress_bar :
743
782
progress_bar = self .create_progress_bar (desc = "Counting" )
744
783
745
784
self .players_d = {}
746
785
self .repetitions_d = {}
747
- with open (self .filename , 'r' ) as f : # This is the only pass through of the data in this method.
786
+ with open (self .filename , 'r' ) as f :
748
787
for row in csv .reader (f ):
749
788
index_pair = (int (row [0 ]), int (row [1 ]))
750
789
players = (row [2 ], row [3 ])
@@ -762,22 +801,61 @@ def _read_players_and_repetition_numbers(self, progress_bar=False):
762
801
return players , nrepetitions
763
802
764
803
def _update_players (self , index_pair , players ):
804
+ """
805
+ During a read of the data, update the internal players dictionary
806
+
807
+ Parameters
808
+ ----------
809
+
810
+ index_pair : tuple
811
+ A tuple of player indices
812
+ players : tuple
813
+ A tuple of player names
814
+ """
765
815
for index , player in zip (index_pair , players ):
766
816
if index not in self .players_d :
767
817
self .players_d [index ] = player
768
818
769
819
def _update_repetitions (self , index_pair ):
820
+ """
821
+ During a read of the data, update the internal repetitions dictionary
822
+
823
+ Parameters
824
+ ----------
825
+
826
+ index_pair : tuple
827
+ A tuple of player indices
828
+ """
770
829
try :
771
830
self .repetitions_d [index_pair ] += 1
772
831
except KeyError :
773
832
self .repetitions_d [index_pair ] = 1
774
833
775
834
def _build_nrepetitions (self ):
835
+ """
836
+ Count the number of repetitions
837
+
838
+ Returns
839
+ -------
840
+
841
+ nrepetitions : int
842
+ The number of repetitions
843
+ """
776
844
nrepetitions = max (self .repetitions_d .values ())
845
+
777
846
del self .repetitions_d # Manual garbage collection
778
847
return nrepetitions
779
848
780
849
def _build_players (self ):
850
+ """
851
+ List the players
852
+
853
+ Returns
854
+ -------
855
+
856
+ players : list
857
+ An ordered list of players
858
+ """
781
859
players = []
782
860
for i in range (len (self .players_d )):
783
861
players .append (self .players_d [i ])
@@ -786,7 +864,22 @@ def _build_players(self):
786
864
return players
787
865
788
866
def read_match_chunks (self , progress_bar = False ):
789
- """A generator to return a given repetitions of matches"""
867
+ """
868
+ A generator to return a given repetitions of matches
869
+
870
+ Parameters
871
+ ----------
872
+
873
+ progress_bar : bool
874
+ whether or not to display a progress bar
875
+
876
+ Yields
877
+ ------
878
+ repetitions : list
879
+ A list of lists include index pairs, player pairs and
880
+ repetitions. All repetitions for a given pair are yielded
881
+ together.
882
+ """
790
883
791
884
if progress_bar :
792
885
progress_bar = self .create_progress_bar (desc = "Analysing" )
@@ -812,6 +905,12 @@ def _build_empty_metrics(self, keep_interactions=False):
812
905
"""
813
906
Creates the various empty metrics ready to be updated as the data is
814
907
read.
908
+
909
+ Parameters
910
+ ----------
911
+
912
+ keep_interactions : bool
913
+ Whether or not to load the interactions in to memory
815
914
"""
816
915
plist = range (self .nplayers )
817
916
replist = range (self .nrepetitions )
@@ -915,6 +1014,16 @@ def build_good_partner_rating(self):
915
1014
916
1015
def _build_score_related_metrics (self , progress_bar = False ,
917
1016
keep_interactions = False ):
1017
+ """
1018
+ Read the data and carry out all relevant calculations.
1019
+
1020
+ Parameters
1021
+ ----------
1022
+ progress_bar : bool
1023
+ Whether or not to display a progress bar
1024
+ keep_interactions : bool
1025
+ Whether or not to lad the interactions in to memory
1026
+ """
918
1027
match_chunks = self .read_match_chunks (progress_bar )
919
1028
920
1029
for match in match_chunks :
0 commit comments