|
8 | 8 | This is used by both the Match class and the ResultSet class which analyse
|
9 | 9 | interactions.
|
10 | 10 | """
|
| 11 | +import csv |
| 12 | + |
11 | 13 | from .game import Game
|
12 | 14 | from axelrod import Actions
|
13 | 15 |
|
| 16 | +import tqdm |
14 | 17 |
|
15 | 18 | C, D = Actions.C, Actions.D
|
16 | 19 |
|
@@ -101,6 +104,37 @@ def compute_sparklines(interactions, c_symbol=u'█', d_symbol=u' '):
|
101 | 104 | sparkline(histories[1], c_symbol, d_symbol))
|
102 | 105 |
|
103 | 106 |
|
| 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 | + |
104 | 138 | def string_to_interactions(string):
|
105 | 139 | """
|
106 | 140 | Converts a compact string representation of an interaction to an
|
|
0 commit comments