Skip to content

Commit 0f33017

Browse files
authored
Merge pull request #778 from Axelrod-Python/768
Add a variety of progress bars to the fingerprint.
2 parents 481f022 + 451e9bd commit 0f33017

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

axelrod/fingerprint.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axelrod as axl
22
import numpy as np
33
import matplotlib.pyplot as plt
4+
import tqdm
45
from axelrod.strategy_transformers import JossAnnTransformer, DualTransformer
56
from axelrod.interaction_utils import compute_final_score_per_turn, read_interactions_from_file
67
from axelrod import on_windows
@@ -11,7 +12,7 @@
1112
Point = namedtuple('Point', 'x y')
1213

1314

14-
def create_points(step):
15+
def create_points(step, progress_bar=True):
1516
"""Creates a set of Points over the unit square.
1617
1718
A Point has coordinates (x, y). This function constructs points that are
@@ -23,15 +24,29 @@ def create_points(step):
2324
step : float
2425
The separation between each Point. Smaller steps will produce more
2526
Points with coordinates that will be closer together.
27+
progress_bar : bool
28+
Whether or not to create a progress bar which will be updated
2629
2730
Returns
2831
----------
2932
points : list
3033
of Point objects with coordinates (x, y)
3134
"""
3235
num = int((1 / step) // 1) + 1
33-
points = [Point(j, k) for j in np.linspace(0, 1, num)
34-
for k in np.linspace(0, 1, num)]
36+
37+
if progress_bar:
38+
p_bar = tqdm.tqdm(total=num ** 2, desc="Generating points")
39+
40+
points = []
41+
for x in np.linspace(0, 1, num):
42+
for y in np.linspace(0, 1, num):
43+
points.append(Point(x, y))
44+
45+
if progress_bar:
46+
p_bar.update()
47+
48+
if progress_bar:
49+
p_bar.close()
3550

3651
return points
3752

@@ -76,7 +91,7 @@ def create_jossann(point, probe):
7691
return joss_ann
7792

7893
@staticmethod
79-
def create_edges(points):
94+
def create_edges(points, progress_bar=True):
8095
"""Creates a set of edges for a spatial tournament.
8196
8297
Constructs edges that correspond to `points`. All edges begin at 0, and
@@ -86,6 +101,9 @@ def create_edges(points):
86101
----------
87102
points : list
88103
of Point objects with coordinates (x, y)
104+
progress_bar : bool
105+
Whether or not to create a progress bar which will be updated
106+
89107
90108
Returns
91109
----------
@@ -94,10 +112,12 @@ def create_edges(points):
94112
first element. The second element is the index of the
95113
corresponding probe (+1 to allow for including the Strategy).
96114
"""
115+
if progress_bar:
116+
points = tqdm.tqdm(points, desc="Generating network edges")
97117
edges = [(0, index + 1) for index, point in enumerate(points)]
98118
return edges
99119

100-
def create_probes(self, probe, points):
120+
def create_probes(self, probe, points, progress_bar=True):
101121
"""Creates a set of probe strategies over the unit square.
102122
103123
Constructs probe strategies that correspond to points with coordinates
@@ -109,24 +129,31 @@ def create_probes(self, probe, points):
109129
A class that must be descended from axelrod.strategies.
110130
points : list
111131
of Point objects with coordinates (x, y)
132+
progress_bar : bool
133+
Whether or not to create a progress bar which will be updated
112134
113135
Returns
114136
----------
115137
probes : list
116138
A list of `JossAnnTransformer` players with parameters that
117139
correspond to point.
118140
"""
141+
if progress_bar:
142+
points = tqdm.tqdm(points, desc="Generating probes")
119143
probes = [self.create_jossann(point, probe) for point in points]
120144
return probes
121145

122-
def construct_tournament_elements(self, step):
146+
def construct_tournament_elements(self, step, progress_bar=True):
123147
"""Build the elements required for a spatial tournament
124148
125149
Parameters
126150
----------
127151
step : float
128152
The separation between each Point. Smaller steps will
129153
produce more Points that will be closer together.
154+
progress_bar : bool
155+
Whether or not to create a progress bar which will be updated
156+
130157
131158
Returns
132159
----------
@@ -140,11 +167,11 @@ def construct_tournament_elements(self, step):
140167
original player, the rest are the probes.
141168
142169
"""
143-
probe_points = create_points(step)
144-
self.points = probe_points
145-
edges = self.create_edges(probe_points)
170+
self.points = create_points(step, progress_bar=progress_bar)
171+
edges = self.create_edges(self.points, progress_bar=progress_bar)
172+
probe_players = self.create_probes(self.probe, self.points,
173+
progress_bar=progress_bar)
146174

147-
probe_players = self.create_probes(self.probe, probe_points)
148175
tournament_players = [self.strategy()] + probe_players
149176

150177
return edges, tournament_players
@@ -219,7 +246,8 @@ def fingerprint(self, turns=50, repetitions=10, step=0.01, processes=None,
219246
outputfile = NamedTemporaryFile(mode='w')
220247
filename = outputfile.name
221248

222-
edges, tourn_players = self.construct_tournament_elements(step)
249+
edges, tourn_players = self.construct_tournament_elements(step,
250+
progress_bar=progress_bar)
223251
self.step = step
224252
self.spatial_tournament = axl.SpatialTournament(tourn_players,
225253
turns=turns,
@@ -233,7 +261,8 @@ def fingerprint(self, turns=50, repetitions=10, step=0.01, processes=None,
233261
if in_memory:
234262
self.interactions = self.spatial_tournament.interactions_dict
235263
else:
236-
self.interactions = read_interactions_from_file(filename)
264+
self.interactions = read_interactions_from_file(filename,
265+
progress_bar=progress_bar)
237266

238267
self.data = self.generate_data(self.interactions, self.points, edges)
239268
return self.data

axelrod/tests/unit/test_fingerprint.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,24 @@ def test_init(self):
3333
self.assertEqual(fingerprint.probe, probe)
3434

3535
def test_create_points(self):
36-
test_points = create_points(0.5)
36+
test_points = create_points(0.5, progress_bar=False)
3737
self.assertEqual(test_points, self.expected_points)
3838

3939
def test_create_probes(self):
4040
af = AshlockFingerprint(self.strategy, self.probe)
41-
probes = af.create_probes(self.probe, self.expected_points)
41+
probes = af.create_probes(self.probe, self.expected_points,
42+
progress_bar=False)
4243
self.assertEqual(len(probes), 9)
4344

4445
def test_create_edges(self):
4546
af = AshlockFingerprint(self.strategy, self.probe)
46-
edges = af.create_edges(self.expected_points)
47+
edges = af.create_edges(self.expected_points, progress_bar=False)
4748
self.assertEqual(edges, self.expected_edges)
4849

4950
def test_construct_tournament_elemets(self):
5051
af = AshlockFingerprint(self.strategy, self.probe)
51-
edges, tournament_players = af.construct_tournament_elements(0.5)
52+
edges, tournament_players = af.construct_tournament_elements(0.5,
53+
progress_bar=False)
5254
self.assertEqual(edges, self.expected_edges)
5355
self.assertEqual(len(tournament_players), 10)
5456
self.assertEqual(tournament_players[0].__class__, af.strategy)

0 commit comments

Comments
 (0)