Skip to content

Commit 0775645

Browse files
authored
Merge pull request #672 from Axelrod-Python/573
Re write of the ResultsSetFromFile
2 parents 06a2887 + ab3516c commit 0775645

File tree

10 files changed

+661
-123
lines changed

10 files changed

+661
-123
lines changed

axelrod/interaction_utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
This is used by both the Match class and the ResultSet class which analyse
99
interactions.
1010
"""
11+
import csv
12+
1113
from .game import Game
1214
from axelrod import Actions
1315

16+
import tqdm
1417

1518
C, D = Actions.C, Actions.D
1619

@@ -101,6 +104,37 @@ def compute_sparklines(interactions, c_symbol=u'█', d_symbol=u' '):
101104
sparkline(histories[1], c_symbol, d_symbol))
102105

103106

107+
def read_interactions_from_file(filename, progress_bar=True,
108+
num_interactions=False):
109+
"""
110+
Reads a file and returns a dictionary mapping tuples of player pairs to
111+
lists of interactions
112+
"""
113+
if progress_bar:
114+
if not num_interactions:
115+
with open(filename) as f:
116+
num_interactions = sum(1 for line in f)
117+
progress_bar = tqdm.tqdm(total=num_interactions, desc="Loading")
118+
119+
pairs_to_interactions = {}
120+
with open(filename, 'r') as f:
121+
for row in csv.reader(f):
122+
index_pair = (int(row[0]), int(row[1]))
123+
interaction = list(zip(row[4], row[5]))
124+
125+
try:
126+
pairs_to_interactions[index_pair].append(interaction)
127+
except KeyError:
128+
pairs_to_interactions[index_pair] = [interaction]
129+
130+
if progress_bar:
131+
progress_bar.update()
132+
133+
if progress_bar:
134+
progress_bar.close()
135+
return pairs_to_interactions
136+
137+
104138
def string_to_interactions(string):
105139
"""
106140
Converts a compact string representation of an interaction to an

0 commit comments

Comments
 (0)