Skip to content

Commit 49f7fb7

Browse files
committed
Improve documentation for state_distribution functions
1 parent 8d0033e commit 49f7fb7

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

axelrod/interaction_utils.py

+32-7
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,51 @@ def compute_normalised_cooperation(interactions):
9191
def compute_state_distribution(interactions):
9292
"""
9393
Returns the count of each state for a set of interactions.
94+
95+
Parameters
96+
----------
97+
interactions : list of tuples
98+
A list containing the interactions of the match as shown at the top of
99+
this file.
100+
101+
Returns
102+
----------
103+
Counter(interactions) : Counter Object
104+
Dictionary where the keys are the states and the values are the number
105+
of times that state occurs.
94106
"""
95-
if len(interactions) == 0:
107+
if not interactions:
96108
return None
97109
return Counter(interactions)
98110

99111

100112
def compute_normalised_state_distribution(interactions):
101113
"""
102114
Returns the normalized count of each state for a set of interactions.
115+
116+
Parameters
117+
----------
118+
interactions : list of tuples
119+
A list containing the interactions of the match as shown at the top of
120+
this file.
121+
122+
Returns
123+
----------
124+
normalized_count : Counter Object
125+
Dictionary where the keys are the states and the values are a normalized
126+
count of the number of times that state occurs.
103127
"""
104-
if len(interactions) == 0:
128+
if not interactions:
105129
return None
106130

107-
normalized_count = Counter(interactions)
108-
total = sum(normalized_count.values(), 0.0)
131+
interactions_count = Counter(interactions)
132+
total = sum(interactions_count.values(), 0.0)
109133
# By starting the sum with 0.0 we make sure total is a floating point value,
110-
# avoiding the Python 2 floor division behaviour of / with integer operands (Stack Overflow)
134+
# avoiding the Python 2 floor division behaviour of / with integer operands
135+
# (Stack Overflow)
111136

112-
for key in normalized_count:
113-
normalized_count[key] /= total
137+
normalized_count = Counter({key: value / total for key, value in
138+
interactions_count.items()})
114139
return normalized_count
115140

116141

axelrod/match.py

+6
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,15 @@ def normalised_cooperation(self):
160160
return iu.compute_normalised_cooperation(self.result)
161161

162162
def state_distribution(self):
163+
"""
164+
Returns the count of each state for a set of interactions.
165+
"""
163166
return iu.compute_state_distribution(self.result)
164167

165168
def normalised_state_distribution(self):
169+
"""
170+
Returns the normalized count of each state for a set of interactions.
171+
"""
166172
return iu.compute_normalised_state_distribution(self.result)
167173

168174
def sparklines(self, c_symbol=u'█', d_symbol=u' '):

0 commit comments

Comments
 (0)