-
Notifications
You must be signed in to change notification settings - Fork 270
Issue 138 - Adds functions to calculate the state distribution of a Match #717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
interactions. | ||
""" | ||
import csv | ||
from collections import Counter | ||
|
||
from .game import Game | ||
from axelrod import Actions | ||
|
@@ -87,6 +88,57 @@ def compute_normalised_cooperation(interactions): | |
return normalised_cooperation | ||
|
||
|
||
def compute_state_distribution(interactions): | ||
""" | ||
Returns the count of each state for a set of interactions. | ||
|
||
Parameters | ||
---------- | ||
interactions : list of tuples | ||
A list containing the interactions of the match as shown at the top of | ||
this file. | ||
|
||
Returns | ||
---------- | ||
Counter(interactions) : Counter Object | ||
Dictionary where the keys are the states and the values are the number | ||
of times that state occurs. | ||
""" | ||
if not interactions: | ||
return None | ||
return Counter(interactions) | ||
|
||
|
||
def compute_normalised_state_distribution(interactions): | ||
""" | ||
Returns the normalized count of each state for a set of interactions. | ||
|
||
Parameters | ||
---------- | ||
interactions : list of tuples | ||
A list containing the interactions of the match as shown at the top of | ||
this file. | ||
|
||
Returns | ||
---------- | ||
normalized_count : Counter Object | ||
Dictionary where the keys are the states and the values are a normalized | ||
count of the number of times that state occurs. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstring needs to define arguments and return value |
||
if not interactions: | ||
return None | ||
|
||
interactions_count = Counter(interactions) | ||
total = sum(interactions_count.values(), 0.0) | ||
# By starting the sum with 0.0 we make sure total is a floating point value, | ||
# avoiding the Python 2 floor division behaviour of / with integer operands | ||
# (Stack Overflow) | ||
|
||
normalized_count = Counter({key: value / total for key, value in | ||
interactions_count.items()}) | ||
return normalized_count | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might be a bit more 'pythonic' with a comprehension. I've not tested it, but something like:
|
||
|
||
|
||
def sparkline(actions, c_symbol=u'█', d_symbol=u' '): | ||
return u''.join([ | ||
c_symbol if play == 'C' else d_symbol for play in actions]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,6 +159,18 @@ def normalised_cooperation(self): | |
"""Returns the count of cooperations by each player per turn""" | ||
return iu.compute_normalised_cooperation(self.result) | ||
|
||
def state_distribution(self): | ||
""" | ||
Returns the count of each state for a set of interactions. | ||
""" | ||
return iu.compute_state_distribution(self.result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we have a brief docstring for these two methods (similar to the ones in the interactions_utils). |
||
|
||
def normalised_state_distribution(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstring needed here |
||
""" | ||
Returns the normalized count of each state for a set of interactions. | ||
""" | ||
return iu.compute_normalised_state_distribution(self.result) | ||
|
||
def sparklines(self, c_symbol=u'█', d_symbol=u' '): | ||
return iu.compute_sparklines(self.result, c_symbol, d_symbol) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring needs the full definition of the arguments and return value