Skip to content

Commit 354aa02

Browse files
committed
Move filename back to tournament.play and add build_results. Update tests.
1 parent 72ce432 commit 354aa02

File tree

7 files changed

+30
-74
lines changed

7 files changed

+30
-74
lines changed

axelrod/match_generator.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def build_match_chunks(self, noise=0):
8383

8484
def build_single_match_params(self, noise=0):
8585
"""Create a single match for a given pair"""
86-
return (self.turns, self.game, None, noise)
86+
cache = None
87+
return (self.turns, self.game, cache, noise)
8788

8889
def __len__(self):
8990
"""Calculates the number of matches."""

axelrod/tests/unit/test_plot.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ def test_init_from_resulsetfromfile(self):
7171
axelrod.TitForTat(),
7272
axelrod.Defector()],
7373
turns=2,
74-
repetitions=2,
75-
filename=tmp_file.name)
76-
tournament.play()
74+
repetitions=2)
75+
tournament.play(filename=tmp_file.name)
7776
tmp_file.close()
7877
rs = axelrod.ResultSetFromFile(tmp_file.name)
7978

axelrod/tests/unit/test_resultset.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,8 @@ class TestResultSetFromFile(unittest.TestCase):
335335
axelrod.TitForTat(),
336336
axelrod.Defector()],
337337
turns=2,
338-
repetitions=1,
339-
filename=tmp_file.name)
340-
tournament.play()
338+
repetitions=1)
339+
tournament.play(filename=tmp_file.name)
341340
tmp_file.close()
342341

343342

axelrod/tests/unit/test_tournament.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def test_init(self):
7474
self.assertTrue(tournament._with_morality)
7575
self.assertIsInstance(tournament._logger, logging.Logger)
7676
self.assertEqual(tournament.noise, 0.2)
77-
self.assertEqual(tournament._parallel_repetitions, 10)
7877
anonymous_tournament = axelrod.Tournament(players=self.players)
7978
self.assertEqual(anonymous_tournament.name, 'axelrod')
8079

@@ -331,9 +330,8 @@ def test_write_to_csv(self):
331330
players=self.players,
332331
game=self.game,
333332
turns=2,
334-
repetitions=2,
335-
filename=tmp_file.name)
336-
tournament.play()
333+
repetitions=2)
334+
tournament.play(filename=tmp_file.name)
337335
tmp_file.close()
338336
with open(tmp_file.name, 'r') as f:
339337
written_data = [[int(r[0]), int(r[1])] + r[2:] for r in csv.reader(f)]
@@ -397,7 +395,6 @@ def test_init(self):
397395
self.assertTrue(tournament._with_morality)
398396
self.assertIsInstance(tournament._logger, logging.Logger)
399397
self.assertEqual(tournament.noise, 0.2)
400-
self.assertEqual(tournament._parallel_repetitions, 10)
401398
anonymous_tournament = axelrod.Tournament(players=self.players)
402399
self.assertEqual(anonymous_tournament.name, 'axelrod')
403400

axelrod/tournament.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
import csv
44
from collections import defaultdict
55
import logging
6-
from tempfile import NamedTemporaryFile
7-
86
from multiprocessing import Process, Queue, cpu_count
9-
7+
from tempfile import NamedTemporaryFile
8+
import warnings
109

1110
from .game import Game
12-
from .result_set import ResultSet, ResultSetFromFile
13-
from .match_generator import RoundRobinMatches, ProbEndRoundRobinMatches
1411
from .match import Match
12+
from .match_generator import RoundRobinMatches, ProbEndRoundRobinMatches
13+
from .result_set import ResultSet, ResultSetFromFile
14+
1515

1616
class Tournament(object):
1717
game = Game()
1818

1919
def __init__(self, players, match_generator=RoundRobinMatches,
2020
name='axelrod', game=None, turns=200, repetitions=10,
21-
processes=None, noise=0, with_morality=True, filename=None):
21+
processes=None, noise=0, with_morality=True):
2222
"""
2323
Parameters
2424
----------
@@ -51,34 +51,41 @@ def __init__(self, players, match_generator=RoundRobinMatches,
5151
self.match_generator = match_generator(players, turns, self.game,
5252
self.repetitions)
5353
self._with_morality = with_morality
54-
self._parallel_repetitions = repetitions
5554
self._processes = processes
5655
self._logger = logging.getLogger(__name__)
57-
self.interactions = defaultdict(list)
5856

59-
if not filename:
57+
def setup_output_file(self, filename=None):
58+
"""Open a CSV writer for tournament output."""
59+
if filename:
60+
self.outputfile = open(filename, 'a')
61+
else:
62+
# Setup a temporary file
6063
self.outputfile = NamedTemporaryFile(mode='w')
6164
filename = self.outputfile.name
62-
else:
63-
self.outputfile = open(filename, 'w')
6465
self.writer = csv.writer(self.outputfile)
66+
# Save filename for loading ResultSet later
6567
self.filename = filename
6668

67-
def play(self):
69+
def play(self, build_results=True, filename=None):
6870
"""
6971
Plays the tournament and passes the results to the ResultSet class
7072
7173
Returns
7274
-------
7375
axelrod.ResultSet
7476
"""
77+
self.setup_output_file(filename)
78+
if build_results and not filename:
79+
warnings.warn("Tournament results will not be accessible since build_results=False and no filename was supplied.")
80+
pass
7581
if self._processes is None:
7682
self._run_serial()
7783
else:
7884
self._run_parallel()
7985
# Make sure that python has finished writing to disk
8086
self.outputfile.flush()
81-
return self._build_result_set()
87+
if build_results:
88+
return self._build_result_set()
8289

8390
def _build_result_set(self):
8491
"""

docs/tutorials/advanced/reading_and_writing_interactions.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ a :code:`filename` argument to the :code:`play` method of a tournament::
77

88
>>> import axelrod as axl
99
>>> players = [s() for s in axl.basic_strategies]
10-
>>> tournament = axl.Tournament(players, turns=4, repetitions=2, filename="basic_tournament.csv")
11-
>>> results = tournament.play()
10+
>>> tournament = axl.Tournament(players, turns=4, repetitions=2)
11+
>>> results = tournament.play(filename="basic_tournament.csv")
1212

1313
This will create a file `basic_tournament.csv` with data that looks something
1414
like::

test

-47
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,3 @@
11
#!/usr/bin/env bash
22
python -m unittest discover axelrod/tests/
33
python doctests.py
4-
5-
# Deleting outputs:
6-
7-
rm basic_strategies_boxplot.svg
8-
rm basic_strategies_lengthplot.svg
9-
rm basic_strategies_payoff.svg
10-
rm basic_strategies_pdplot.svg
11-
rm basic_strategies_prob_end.csv
12-
rm basic_strategies_prob_end_boxplot.svg
13-
rm basic_strategies_prob_end_lengthplot.svg
14-
rm basic_strategies_prob_end_payoff.svg
15-
rm basic_strategies_prob_end_pdplot.svg
16-
rm basic_strategies_prob_end_reproduce.svg
17-
rm basic_strategies_prob_end_sdvplot.svg
18-
rm basic_strategies_prob_end_winplot.svg
19-
rm basic_strategies_reproduce.svg
20-
rm basic_strategies_sdvplot.svg
21-
rm basic_strategies_winplot.svg
22-
rm ordinary_strategies.csv
23-
rm ordinary_strategies_boxplot.svg
24-
rm ordinary_strategies_lengthplot.svg
25-
rm ordinary_strategies_payoff.svg
26-
rm ordinary_strategies_pdplot.svg
27-
rm ordinary_strategies_prob_end.csv
28-
rm ordinary_strategies_prob_end_boxplot.svg
29-
rm ordinary_strategies_prob_end_lengthplot.svg
30-
rm ordinary_strategies_prob_end_payoff.svg
31-
rm ordinary_strategies_prob_end_pdplot.svg
32-
rm ordinary_strategies_prob_end_reproduce.svg
33-
rm ordinary_strategies_prob_end_sdvplot.svg
34-
rm ordinary_strategies_prob_end_winplot.svg
35-
rm ordinary_strategies_reproduce.svg
36-
rm ordinary_strategies_sdvplot.svg
37-
rm ordinary_strategies_winplot.svg
38-
rm test-prob-end.csv
39-
rm test-prob-end_boxplot.svg
40-
rm test-prob-end_lengthplot.svg
41-
rm test-prob-end_payoff.svg
42-
rm test-prob-end_pdplot.svg
43-
rm test-prob-end_sdvplot.svg
44-
rm test-prob-end_winplot.svg
45-
rm test_boxplot.svg
46-
rm test_lengthplot.svg
47-
rm test_payoff.svg
48-
rm test_pdplot.svg
49-
rm test_sdvplot.svg
50-
rm test_winplot.svg

0 commit comments

Comments
 (0)