Skip to content

Commit dd7909e

Browse files
committed
Include doc strings.
1 parent 83dc4de commit dd7909e

File tree

1 file changed

+113
-4
lines changed

1 file changed

+113
-4
lines changed

axelrod/result_set.py

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,12 +707,36 @@ def __ne__(self, other):
707707

708708
class ResultSetFromFile(ResultSet):
709709
"""
710-
Proof of concept for a result set that loops over the data file twice.
710+
Read the result set directly from file.
711711
"""
712712

713713
def __init__(self, filename, progress_bar=True,
714714
num_interactions=False, players=False, nrepetitions=False,
715715
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+
"""
716740
if game is None:
717741
self.game = Game()
718742
else:
@@ -732,19 +756,34 @@ def __init__(self, filename, progress_bar=True,
732756
keep_interactions=keep_interactions)
733757

734758
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+
"""
735767
if not self.num_interactions:
736768
self.num_interactions = sum(1 for line in open(self.filename))
737769
return tqdm.tqdm(total=self.num_interactions, desc=desc)
738770

739771
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+
"""
741780

742781
if progress_bar:
743782
progress_bar = self.create_progress_bar(desc="Counting")
744783

745784
self.players_d = {}
746785
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:
748787
for row in csv.reader(f):
749788
index_pair = (int(row[0]), int(row[1]))
750789
players = (row[2], row[3])
@@ -762,22 +801,61 @@ def _read_players_and_repetition_numbers(self, progress_bar=False):
762801
return players, nrepetitions
763802

764803
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+
"""
765815
for index, player in zip(index_pair, players):
766816
if index not in self.players_d:
767817
self.players_d[index] = player
768818

769819
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+
"""
770829
try:
771830
self.repetitions_d[index_pair] += 1
772831
except KeyError:
773832
self.repetitions_d[index_pair] = 1
774833

775834
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+
"""
776844
nrepetitions = max(self.repetitions_d.values())
845+
777846
del self.repetitions_d # Manual garbage collection
778847
return nrepetitions
779848

780849
def _build_players(self):
850+
"""
851+
List the players
852+
853+
Returns
854+
-------
855+
856+
players : list
857+
An ordered list of players
858+
"""
781859
players = []
782860
for i in range(len(self.players_d)):
783861
players.append(self.players_d[i])
@@ -786,7 +864,22 @@ def _build_players(self):
786864
return players
787865

788866
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+
"""
790883

791884
if progress_bar:
792885
progress_bar = self.create_progress_bar(desc="Analysing")
@@ -812,6 +905,12 @@ def _build_empty_metrics(self, keep_interactions=False):
812905
"""
813906
Creates the various empty metrics ready to be updated as the data is
814907
read.
908+
909+
Parameters
910+
----------
911+
912+
keep_interactions : bool
913+
Whether or not to load the interactions in to memory
815914
"""
816915
plist = range(self.nplayers)
817916
replist = range(self.nrepetitions)
@@ -915,6 +1014,16 @@ def build_good_partner_rating(self):
9151014

9161015
def _build_score_related_metrics(self, progress_bar=False,
9171016
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+
"""
9181027
match_chunks = self.read_match_chunks(progress_bar)
9191028

9201029
for match in match_chunks:

0 commit comments

Comments
 (0)