Skip to content

Commit 1cbd71a

Browse files
drvinceknightmarcharper
authored andcommitted
Add iu.read_interactions_from_file to docs.
This highlights it as a user facing function but I'm happy to do something further.
1 parent 9c9f335 commit 1cbd71a

File tree

2 files changed

+85
-96
lines changed

2 files changed

+85
-96
lines changed

axelrod/interaction_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,7 @@ def compute_sparklines(interactions, c_symbol='█', d_symbol=' '):
240240
sparkline(histories[1], c_symbol, d_symbol))
241241

242242

243-
def read_interactions_from_file(filename,
244-
progress_bar=True,
245-
):
243+
def read_interactions_from_file(filename, progress_bar=True):
246244
"""
247245
Reads a file and returns a dictionary mapping tuples of player pairs to
248246
lists of interactions

docs/tutorials/advanced/reading_and_writing_interactions.rst

Lines changed: 84 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -13,107 +13,98 @@ a :code:`filename` argument to the :code:`play` method of a tournament::
1313
This will create a file `basic_tournament.csv` with data that looks something
1414
like::
1515

16-
0,0,Alternator,Alternator,CDCD,CDCD
17-
0,0,Alternator,Alternator,CDCD,CDCD
18-
0,1,Alternator,Anti Tit For Tat,CDCD,CDCD
19-
0,1,Alternator,Anti Tit For Tat,CDCD,CDCD
20-
0,2,Alternator,Bully,CDCD,DDCD
21-
0,2,Alternator,Bully,CDCD,DDCD
22-
0,3,Alternator,Cooperator,CDCD,CCCC
23-
0,3,Alternator,Cooperator,CDCD,CCCC
24-
0,4,Alternator,Defector,CDCD,DDDD
25-
0,4,Alternator,Defector,CDCD,DDDD
26-
0,5,Alternator,Suspicious Tit For Tat,CDCD,DCDC
27-
0,5,Alternator,Suspicious Tit For Tat,CDCD,DCDC
28-
0,6,Alternator,Tit For Tat,CDCD,CCDC
29-
0,6,Alternator,Tit For Tat,CDCD,CCDC
30-
0,7,Alternator,Win-Stay Lose-Shift,CDCD,CCDD
31-
0,7,Alternator,Win-Stay Lose-Shift,CDCD,CCDD
32-
1,1,Anti Tit For Tat,Anti Tit For Tat,CDCD,CDCD
33-
1,1,Anti Tit For Tat,Anti Tit For Tat,CDCD,CDCD
34-
1,2,Anti Tit For Tat,Bully,CCCC,DDDD
35-
1,2,Anti Tit For Tat,Bully,CCCC,DDDD
36-
1,3,Anti Tit For Tat,Cooperator,CDDD,CCCC
37-
1,3,Anti Tit For Tat,Cooperator,CDDD,CCCC
38-
1,4,Anti Tit For Tat,Defector,CCCC,DDDD
39-
1,4,Anti Tit For Tat,Defector,CCCC,DDDD
40-
1,5,Anti Tit For Tat,Suspicious Tit For Tat,CCDD,DCCD
41-
1,5,Anti Tit For Tat,Suspicious Tit For Tat,CCDD,DCCD
42-
1,6,Anti Tit For Tat,Tit For Tat,CDDC,CCDD
43-
1,6,Anti Tit For Tat,Tit For Tat,CDDC,CCDD
44-
1,7,Anti Tit For Tat,Win-Stay Lose-Shift,CDDC,CCDC
45-
1,7,Anti Tit For Tat,Win-Stay Lose-Shift,CDDC,CCDC
46-
2,2,Bully,Bully,DCDC,DCDC
47-
2,2,Bully,Bully,DCDC,DCDC
48-
2,3,Bully,Cooperator,DDDD,CCCC
49-
2,3,Bully,Cooperator,DDDD,CCCC
50-
2,4,Bully,Defector,DCCC,DDDD
51-
2,4,Bully,Defector,DCCC,DDDD
52-
2,5,Bully,Suspicious Tit For Tat,DCCD,DDCC
53-
2,5,Bully,Suspicious Tit For Tat,DCCD,DDCC
54-
2,6,Bully,Tit For Tat,DDCC,CDDC
55-
2,6,Bully,Tit For Tat,DDCC,CDDC
56-
2,7,Bully,Win-Stay Lose-Shift,DDCD,CDCC
57-
2,7,Bully,Win-Stay Lose-Shift,DDCD,CDCC
58-
3,3,Cooperator,Cooperator,CCCC,CCCC
59-
3,3,Cooperator,Cooperator,CCCC,CCCC
60-
3,4,Cooperator,Defector,CCCC,DDDD
61-
3,4,Cooperator,Defector,CCCC,DDDD
62-
3,5,Cooperator,Suspicious Tit For Tat,CCCC,DCCC
63-
3,5,Cooperator,Suspicious Tit For Tat,CCCC,DCCC
64-
3,6,Cooperator,Tit For Tat,CCCC,CCCC
65-
3,6,Cooperator,Tit For Tat,CCCC,CCCC
66-
3,7,Cooperator,Win-Stay Lose-Shift,CCCC,CCCC
67-
3,7,Cooperator,Win-Stay Lose-Shift,CCCC,CCCC
68-
4,4,Defector,Defector,DDDD,DDDD
69-
4,4,Defector,Defector,DDDD,DDDD
70-
4,5,Defector,Suspicious Tit For Tat,DDDD,DDDD
71-
4,5,Defector,Suspicious Tit For Tat,DDDD,DDDD
72-
4,6,Defector,Tit For Tat,DDDD,CDDD
73-
4,6,Defector,Tit For Tat,DDDD,CDDD
74-
4,7,Defector,Win-Stay Lose-Shift,DDDD,CDCD
75-
4,7,Defector,Win-Stay Lose-Shift,DDDD,CDCD
76-
5,5,Suspicious Tit For Tat,Suspicious Tit For Tat,DDDD,DDDD
77-
5,5,Suspicious Tit For Tat,Suspicious Tit For Tat,DDDD,DDDD
78-
5,6,Suspicious Tit For Tat,Tit For Tat,DCDC,CDCD
79-
5,6,Suspicious Tit For Tat,Tit For Tat,DCDC,CDCD
80-
5,7,Suspicious Tit For Tat,Win-Stay Lose-Shift,DCDD,CDDC
81-
5,7,Suspicious Tit For Tat,Win-Stay Lose-Shift,DCDD,CDDC
82-
6,6,Tit For Tat,Tit For Tat,CCCC,CCCC
83-
6,6,Tit For Tat,Tit For Tat,CCCC,CCCC
84-
6,7,Tit For Tat,Win-Stay Lose-Shift,CCCC,CCCC
85-
6,7,Tit For Tat,Win-Stay Lose-Shift,CCCC,CCCC
86-
7,7,Win-Stay Lose-Shift,Win-Stay Lose-Shift,CCCC,CCCC
87-
7,7,Win-Stay Lose-Shift,Win-Stay Lose-Shift,CCCC,CCCC
88-
89-
The columns of this file are of the form:
90-
91-
1. Index of first player
92-
2. Index of second player
93-
3. Name of first player
94-
4. Name of second player
95-
5. History of play of the first player
96-
6. History of play of the second player
16+
Interaction index,Player index,Opponent index,Repetition,Player name,Opponent name,Actions,Score,Score difference,Turns,Score per turn,Score difference per turn,Win,Initial cooperation,Cooperation count,CC count,CD count,DC count,DD count,CC to C count,CC to D count,CD to C count,CD to D count,DC to C count,DC to D count,DD to C count,DD to D count,Good partner
17+
0,0,0,0,Alternator,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
18+
0,0,0,0,Alternator,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
19+
1,0,0,1,Alternator,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
20+
1,0,0,1,Alternator,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
21+
2,0,1,0,Alternator,Anti Tit For Tat,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
22+
2,1,0,0,Anti Tit For Tat,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
23+
3,0,1,1,Alternator,Anti Tit For Tat,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
24+
3,1,0,1,Anti Tit For Tat,Alternator,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
25+
4,0,2,0,Alternator,Bully,CDCD,5,-5,4,1.25,-1.25,0,True,2,1,1,0,2,0,1,0,1,0,0,1,0,1
26+
4,2,0,0,Bully,Alternator,DDCD,10,5,4,2.5,1.25,1,False,1,1,0,1,2,0,1,0,0,0,1,1,0,0
27+
5,0,2,1,Alternator,Bully,CDCD,5,-5,4,1.25,-1.25,0,True,2,1,1,0,2,0,1,0,1,0,0,1,0,1
28+
5,2,0,1,Bully,Alternator,DDCD,10,5,4,2.5,1.25,1,False,1,1,0,1,2,0,1,0,0,0,1,1,0,0
29+
6,0,3,0,Alternator,Cooperator,CDCD,16,10,4,4.0,2.5,1,True,2,2,0,2,0,0,2,0,0,1,0,0,0,0
30+
6,3,0,0,Cooperator,Alternator,CCCC,6,-10,4,1.5,-2.5,0,True,4,2,2,0,0,2,0,1,0,0,0,0,0,1
31+
7,0,3,1,Alternator,Cooperator,CDCD,16,10,4,4.0,2.5,1,True,2,2,0,2,0,0,2,0,0,1,0,0,0,0
32+
7,3,0,1,Cooperator,Alternator,CCCC,6,-10,4,1.5,-2.5,0,True,4,2,2,0,0,2,0,1,0,0,0,0,0,1
33+
8,0,4,0,Alternator,Cycler DC,CDCD,10,0,4,2.5,0.0,0,True,2,0,2,2,0,0,0,0,2,1,0,0,0,1
34+
8,4,0,0,Cycler DC,Alternator,DCDC,10,0,4,2.5,0.0,0,False,2,0,2,2,0,0,0,0,1,2,0,0,0,1
35+
9,0,4,1,Alternator,Cycler DC,CDCD,10,0,4,2.5,0.0,0,True,2,0,2,2,0,0,0,0,2,1,0,0,0,1
36+
9,4,0,1,Cycler DC,Alternator,DCDC,10,0,4,2.5,0.0,0,False,2,0,2,2,0,0,0,0,1,2,0,0,0,1
37+
10,0,5,0,Alternator,Defector,CDCD,2,-10,4,0.5,-2.5,0,True,2,0,2,0,2,0,0,0,2,0,0,1,0,1
38+
10,5,0,0,Defector,Alternator,DDDD,12,10,4,3.0,2.5,1,False,0,0,0,2,2,0,0,0,0,0,2,0,1,0
39+
11,0,5,1,Alternator,Defector,CDCD,2,-10,4,0.5,-2.5,0,True,2,0,2,0,2,0,0,0,2,0,0,1,0,1
40+
11,5,0,1,Defector,Alternator,DDDD,12,10,4,3.0,2.5,1,False,0,0,0,2,2,0,0,0,0,0,2,0,1,0
41+
12,0,6,0,Alternator,Grudger,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1
42+
12,6,0,0,Grudger,Alternator,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,1,0,0,1,0,1,0,0,1
43+
13,0,6,1,Alternator,Grudger,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1
44+
13,6,0,1,Grudger,Alternator,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,1,0,0,1,0,1,0,0,1
45+
14,0,7,0,Alternator,Suspicious Tit For Tat,CDCD,10,0,4,2.5,0.0,0,True,2,0,2,2,0,0,0,0,2,1,0,0,0,1
46+
14,7,0,0,Suspicious Tit For Tat,Alternator,DCDC,10,0,4,2.5,0.0,0,False,2,0,2,2,0,0,0,0,1,2,0,0,0,1
47+
15,0,7,1,Alternator,Suspicious Tit For Tat,CDCD,10,0,4,2.5,0.0,0,True,2,0,2,2,0,0,0,0,2,1,0,0,0,1
48+
15,7,0,1,Suspicious Tit For Tat,Alternator,DCDC,10,0,4,2.5,0.0,0,False,2,0,2,2,0,0,0,0,1,2,0,0,0,1
49+
16,0,8,0,Alternator,Tit For Tat,CDCD,13,5,4,3.25,1.25,1,True,2,1,1,2,0,0,1,0,1,1,0,0,0,0
50+
16,8,0,0,Tit For Tat,Alternator,CCDC,8,-5,4,2.0,-1.25,0,True,3,1,2,1,0,1,0,0,1,1,0,0,0,1
51+
17,0,8,1,Alternator,Tit For Tat,CDCD,13,5,4,3.25,1.25,1,True,2,1,1,2,0,0,1,0,1,1,0,0,0,0
52+
17,8,0,1,Tit For Tat,Alternator,CCDC,8,-5,4,2.0,-1.25,0,True,3,1,2,1,0,1,0,0,1,1,0,0,0,1
53+
18,0,9,0,Alternator,Win-Shift Lose-Stay: D,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1
54+
18,9,0,0,Win-Shift Lose-Stay: D,Alternator,DCCD,9,0,4,2.25,0.0,0,False,2,1,1,1,1,0,1,1,0,1,0,0,0,1
55+
19,0,9,1,Alternator,Win-Shift Lose-Stay: D,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1
56+
19,9,0,1,Win-Shift Lose-Stay: D,Alternator,DCCD,9,0,4,2.25,0.0,0,False,2,1,1,1,1,0,1,1,0,1,0,0,0,1
57+
20,0,10,0,Alternator,Win-Stay Lose-Shift: C,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1
58+
20,10,0,0,Win-Stay Lose-Shift: C,Alternator,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,1,0,0,1,0,1,0,0,1
59+
21,0,10,1,Alternator,Win-Stay Lose-Shift: C,CDCD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,1,1,0,0,0,1
60+
21,10,0,1,Win-Stay Lose-Shift: C,Alternator,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,1,0,0,1,0,1,0,0,1
61+
22,1,1,0,Anti Tit For Tat,Anti Tit For Tat,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
62+
22,1,1,0,Anti Tit For Tat,Anti Tit For Tat,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
63+
23,1,1,1,Anti Tit For Tat,Anti Tit For Tat,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
64+
23,1,1,1,Anti Tit For Tat,Anti Tit For Tat,CDCD,8,0,4,2.0,0.0,0,True,2,2,0,0,2,0,2,0,0,0,0,1,0,1
65+
24,1,2,0,Anti Tit For Tat,Bully,CCCC,0,-20,4,0.0,-5.0,0,True,4,0,4,0,0,0,0,3,0,0,0,0,0,1
66+
24,2,1,0,Bully,Anti Tit For Tat,DDDD,20,20,4,5.0,5.0,1,False,0,0,0,4,0,0,0,0,0,0,3,0,0,0
67+
25,1,2,1,Anti Tit For Tat,Bully,CCCC,0,-20,4,0.0,-5.0,0,True,4,0,4,0,0,0,0,3,0,0,0,0,0,1
68+
25,2,1,1,Bully,Anti Tit For Tat,DDDD,20,20,4,5.0,5.0,1,False,0,0,0,4,0,0,0,0,0,0,3,0,0,0
69+
26,1,3,0,Anti Tit For Tat,Cooperator,CDDD,18,15,4,4.5,3.75,1,True,1,1,0,3,0,0,1,0,0,0,2,0,0,0
70+
26,3,1,0,Cooperator,Anti Tit For Tat,CCCC,3,-15,4,0.75,-3.75,0,True,4,1,3,0,0,1,0,2,0,0,0,0,0,1
71+
27,1,3,1,Anti Tit For Tat,Cooperator,CDDD,18,15,4,4.5,3.75,1,True,1,1,0,3,0,0,1,0,0,0,2,0,0,0
72+
27,3,1,1,Cooperator,Anti Tit For Tat,CCCC,3,-15,4,0.75,-3.75,0,True,4,1,3,0,0,1,0,2,0,0,0,0,0,1
73+
28,1,4,0,Anti Tit For Tat,Cycler DC,CCDC,7,-5,4,1.75,-1.25,0,True,3,2,1,0,1,0,1,1,0,0,0,1,0,1
74+
28,4,1,0,Cycler DC,Anti Tit For Tat,DCDC,12,5,4,3.0,1.25,1,False,2,2,0,1,1,0,1,0,0,1,0,1,0,0
75+
29,1,4,1,Anti Tit For Tat,Cycler DC,CCDC,7,-5,4,1.75,-1.25,0,True,3,2,1,0,1,0,1,1,0,0,0,1,0,1
76+
29,4,1,1,Cycler DC,Anti Tit For Tat,DCDC,12,5,4,3.0,1.25,1,False,2,2,0,1,1,0,1,0,0,1,0,1,0,0
77+
30,1,5,0,Anti Tit For Tat,Defector,CCCC,0,-20,4,0.0,-5.0,0,True,4,0,4,0,0,0,0,3,0,0,0,0,0,1
78+
30,5,1,0,Defector,Anti Tit For Tat,DDDD,20,20,4,5.0,5.0,1,False,0,0,0,4,0,0,0,0,0,0,3,0,0,0
79+
31,1,5,1,Anti Tit For Tat,Defector,CCCC,0,-20,4,0.0,-5.0,0,True,4,0,4,0,0,0,0,3,0,0,0,0,0,1
80+
31,5,1,1,Defector,Anti Tit For Tat,DDDD,20,20,4,5.0,5.0,1,False,0,0,0,4,0,0,0,0,0,0,3,0,0,0
81+
32,1,6,0,Anti Tit For Tat,Grudger,CDDC,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,0,0,1,1,0,1
82+
32,6,1,0,Grudger,Anti Tit For Tat,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,1,0,0,1,0,0,0,1,1
83+
33,1,6,1,Anti Tit For Tat,Grudger,CDDC,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,0,0,0,1,1,0,1
84+
33,6,1,1,Grudger,Anti Tit For Tat,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,1,0,0,1,0,0,0,1,1
85+
34,1,7,0,Anti Tit For Tat,Suspicious Tit For Tat,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,1,0,0,1,0,0,1
86+
34,7,1,0,Suspicious Tit For Tat,Anti Tit For Tat,DCCD,9,0,4,2.25,0.0,0,False,2,1,1,1,1,1,0,0,1,1,0,0,0,1
87+
35,1,7,1,Anti Tit For Tat,Suspicious Tit For Tat,CCDD,9,0,4,2.25,0.0,0,True,2,1,1,1,1,0,1,1,0,0,1,0,0,1
88+
35,7,1,1,Suspicious Tit For Tat,Anti Tit For Tat,DCCD,9,0,4,2.25,0.0,0,False,2,1,1,1,1,1,0,0,1,1,0,0,0,1
89+
...
9790

9891
Note that depending on the order in which the matches have been played, the rows
9992
could also be in a different order.
10093

101-
:code:`Alternator` versus :code:`TitForTat` has the following interactions:
102-
:code:`CCDC, CDCD`:
94+
It is possible to read in this data file to obtain interactions::
10395

104-
- First turn: :code:`C` versus :code:`C` (the first two letters)
105-
- Second turn: :code:`D` versus :code:`C` (the second pair of letters)
106-
- Third turn: :code:`C` versus :code:`D` (the third pair of letters)
107-
- Fourth turn: :code:`D` versus :code:`C` (the fourth pair of letters)
96+
>>> interactions = axl.interaction_utils.read_interactions_from_file("basic_tournament.csv")
10897

109-
This can be transformed in to the usual interactions by zipping:
98+
This gives a dictionary mapping pairs of player indices to interaction
99+
histories::
110100

111-
>>> from axelrod.action import str_to_actions
112-
>>> list(zip(str_to_actions("CCDC"), str_to_actions("CDCD")))
113-
[(C, C), (C, D), (D, C), (C, D)]
101+
>>> interactions[(0, 1)]
102+
[[(C, C), (D, D), (C, C), (D, D)], [(C, C), (D, D), (C, C), (D, D)]]
114103

115104
This should allow for easy manipulation of data outside of the capabilities
116-
within the library. Note that you can supply `build_results=False` as a keyword
105+
within the library.
106+
107+
Note that you can supply `build_results=False` as a keyword
117108
argument to `tournament.play()` to prevent keeping or loading interactions in
118109
memory, since the total memory footprint can be large for various combinations
119-
of parameters. The memory usage scales as :math:`O(\text{players}^2 * \text{turns} * \text{repetitions})`.
110+
of parameters. The memory usage scales as :math:`O(\text{players}^2 \times \text{turns} \times \text{repetitions})`.

0 commit comments

Comments
 (0)