Skip to content

Commit 375a2fe

Browse files
committed
Merge to origin head
2 parents f5bad9f + d4735cb commit 375a2fe

File tree

87 files changed

+2526
-2447
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2526
-2447
lines changed

axelrod/result_set.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import csv
33
import itertools
44
from multiprocessing import cpu_count
5+
from typing import List
56
import warnings
67

78
import numpy as np
@@ -426,7 +427,9 @@ def _build_initial_cooperation_rate(self, interactions_series):
426427
with warnings.catch_warnings():
427428
warnings.simplefilter("ignore")
428429
initial_cooperation_rate = list(
429-
np.nan_to_num(np.array(self.initial_cooperation_count) / interactions_array)
430+
np.nan_to_num(
431+
np.array(self.initial_cooperation_count) / interactions_array
432+
)
430433
)
431434
return initial_cooperation_rate
432435

@@ -609,6 +612,18 @@ def __eq__(self, other):
609612
other : axelrod.ResultSet
610613
Another results set against which to check equality
611614
"""
615+
616+
def list_equal_with_nans(v1: List[float], v2: List[float]) -> bool:
617+
"""Matches lists, accounting for NaNs."""
618+
if len(v1) != len(v2):
619+
return False
620+
for i1, i2 in zip(v1, v2):
621+
if np.isnan(i1) and np.isnan(i2):
622+
continue
623+
if i1 != i2:
624+
return False
625+
return True
626+
612627
return all(
613628
[
614629
self.wins == other.wins,
@@ -628,8 +643,8 @@ def __eq__(self, other):
628643
self.cooperating_rating == other.cooperating_rating,
629644
self.good_partner_matrix == other.good_partner_matrix,
630645
self.good_partner_rating == other.good_partner_rating,
631-
self.eigenmoses_rating == other.eigenmoses_rating,
632-
self.eigenjesus_rating == other.eigenjesus_rating,
646+
list_equal_with_nans(self.eigenmoses_rating, other.eigenmoses_rating),
647+
list_equal_with_nans(self.eigenjesus_rating, other.eigenjesus_rating),
633648
]
634649
)
635650

axelrod/tests/integration/test_filtering.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import unittest
22

3-
from axelrod import filtered_strategies, seed, short_run_time_strategies
3+
import axelrod as axl
44
from axelrod.tests.property import strategy_lists
5+
56
from hypothesis import example, given, settings
67
from hypothesis.strategies import integers
78

@@ -26,7 +27,7 @@ def test_boolean_filtering(self, strategies):
2627
for classifier in classifiers:
2728
comprehension = set([s for s in strategies if s.classifier[classifier]])
2829
filterset = {classifier: True}
29-
filtered = set(filtered_strategies(filterset, strategies=strategies))
30+
filtered = set(axl.filtered_strategies(filterset, strategies=strategies))
3031
self.assertEqual(comprehension, filtered)
3132

3233
@given(
@@ -39,7 +40,7 @@ def test_boolean_filtering(self, strategies):
3940
min_memory_depth=float("inf"),
4041
max_memory_depth=float("inf"),
4142
memory_depth=float("inf"),
42-
strategies=short_run_time_strategies,
43+
strategies=axl.short_run_time_strategies,
4344
)
4445
@settings(max_examples=5)
4546
def test_memory_depth_filtering(
@@ -55,7 +56,7 @@ def test_memory_depth_filtering(
5556
]
5657
)
5758
min_filterset = {"min_memory_depth": min_memory_depth}
58-
min_filtered = set(filtered_strategies(min_filterset,
59+
min_filtered = set(axl.filtered_strategies(min_filterset,
5960
strategies=strategies))
6061
self.assertEqual(min_comprehension, min_filtered)
6162

@@ -67,7 +68,7 @@ def test_memory_depth_filtering(
6768
]
6869
)
6970
max_filterset = {"max_memory_depth": max_memory_depth}
70-
max_filtered = set(filtered_strategies(max_filterset,
71+
max_filtered = set(axl.filtered_strategies(max_filterset,
7172
strategies=strategies))
7273
self.assertEqual(max_comprehension, max_filtered)
7374

@@ -79,7 +80,7 @@ def test_memory_depth_filtering(
7980
]
8081
)
8182
filterset = {"memory_depth": memory_depth}
82-
filtered = set(filtered_strategies(filterset, strategies=strategies))
83+
filtered = set(axl.filtered_strategies(filterset, strategies=strategies))
8384
self.assertEqual(comprehension, filtered)
8485

8586
@given(seed_=integers(min_value=0, max_value=4294967295),
@@ -95,7 +96,7 @@ def test_makes_use_of_filtering(self, seed_, strategies):
9596
classifiers = [["game"], ["length"], ["game", "length"]]
9697

9798
for classifier in classifiers:
98-
seed(seed_)
99+
axl.seed(seed_)
99100
comprehension = set(
100101
[
101102
s
@@ -104,9 +105,9 @@ def test_makes_use_of_filtering(self, seed_, strategies):
104105
]
105106
)
106107

107-
seed(seed_)
108+
axl.seed(seed_)
108109
filterset = {"makes_use_of": classifier}
109-
filtered = set(filtered_strategies(filterset, strategies=strategies))
110+
filtered = set(axl.filtered_strategies(filterset, strategies=strategies))
110111

111112
self.assertEqual(
112113
comprehension, filtered, msg="classifier: {}".format(classifier)

axelrod/tests/integration/test_matches.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
"""Tests for some expected match behaviours"""
22
import unittest
33

4-
import axelrod
4+
import axelrod as axl
55
from axelrod.tests.property import strategy_lists
66

77
from hypothesis import given, settings
88
from hypothesis.strategies import integers
99

10-
C, D = axelrod.Action.C, axelrod.Action.D
10+
C, D = axl.Action.C, axl.Action.D
1111

1212
deterministic_strategies = [
13-
s for s in axelrod.short_run_time_strategies if not s().classifier["stochastic"]
13+
s for s in axl.short_run_time_strategies if not s().classifier["stochastic"]
1414
]
1515
stochastic_strategies = [
16-
s for s in axelrod.short_run_time_strategies if s().classifier["stochastic"]
16+
s for s in axl.short_run_time_strategies if s().classifier["stochastic"]
1717
]
1818

1919

@@ -29,7 +29,7 @@ def test_outcome_repeats(self, strategies, turns):
2929
"""A test that if we repeat 3 matches with deterministic and well
3030
behaved strategies then we get the same result"""
3131
players = [s() for s in strategies]
32-
matches = [axelrod.Match(players, turns) for _ in range(3)]
32+
matches = [axl.Match(players, turns) for _ in range(3)]
3333
self.assertEqual(matches[0].play(), matches[1].play())
3434
self.assertEqual(matches[1].play(), matches[2].play())
3535

@@ -46,9 +46,9 @@ def test_outcome_repeats_stochastic(self, strategies, turns, seed):
4646
same result"""
4747
results = []
4848
for _ in range(3):
49-
axelrod.seed(seed)
49+
axl.seed(seed)
5050
players = [s() for s in strategies]
51-
results.append(axelrod.Match(players, turns).play())
51+
results.append(axl.Match(players, turns).play())
5252

5353
self.assertEqual(results[0], results[1])
5454
self.assertEqual(results[1], results[2])
@@ -57,15 +57,15 @@ def test_matches_with_det_player_for_stochastic_classes(self):
5757
"""A test based on a bug found in the cache.
5858
5959
See: https://github.com/Axelrod-Python/Axelrod/issues/779"""
60-
p1 = axelrod.MemoryOnePlayer(four_vector=(0, 0, 0, 0))
61-
p2 = axelrod.MemoryOnePlayer(four_vector=(1, 0, 1, 0))
62-
p3 = axelrod.MemoryOnePlayer(four_vector=(1, 1, 1, 0))
60+
p1 = axl.MemoryOnePlayer(four_vector=(0, 0, 0, 0))
61+
p2 = axl.MemoryOnePlayer(four_vector=(1, 0, 1, 0))
62+
p3 = axl.MemoryOnePlayer(four_vector=(1, 1, 1, 0))
6363

64-
m = axelrod.Match((p1, p2), turns=3)
64+
m = axl.Match((p1, p2), turns=3)
6565
self.assertEqual(m.play(), [(C, C), (D, C), (D, D)])
6666

67-
m = axelrod.Match((p2, p3), turns=3)
67+
m = axl.Match((p2, p3), turns=3)
6868
self.assertEqual(m.play(), [(C, C), (C, C), (C, C)])
6969

70-
m = axelrod.Match((p1, p3), turns=3)
70+
m = axl.Match((p1, p3), turns=3)
7171
self.assertEqual(m.play(), [(C, C), (D, C), (D, C)])
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import unittest
22

3-
from axelrod import all_strategies
3+
import axelrod as axl
44

55

66
class TestNames(unittest.TestCase):
77
def test_all_strategies_have_names(self):
8-
names = [s.name for s in all_strategies if s.name]
9-
self.assertEqual(len(names), len(all_strategies))
8+
names = [s.name for s in axl.all_strategies if s.name]
9+
self.assertEqual(len(names), len(axl.all_strategies))
1010

1111
def test_all_names_are_unique(self):
12-
names = set(s.name for s in all_strategies)
13-
self.assertEqual(len(names), len(all_strategies))
12+
names = set(s.name for s in axl.all_strategies)
13+
self.assertEqual(len(names), len(axl.all_strategies))

axelrod/tests/integration/test_sample_tournaments.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import unittest
22

3-
import axelrod
3+
import axelrod as axl
44

5-
C, D = axelrod.Action.C, axelrod.Action.D
5+
C, D = axl.Action.C, axl.Action.D
66

77

88
class TestSampleTournaments(unittest.TestCase):
99
@classmethod
1010
def setUpClass(cls):
11-
cls.game = axelrod.Game()
11+
cls.game = axl.Game()
1212

1313
@classmethod
1414
def get_test_outcome(cls, outcome, turns=10):
1515

1616
# Extract the name of players from the outcome tuples,
1717
# and initiate the players by getting the classes from axelrod.
1818
names = [out[0] for out in outcome]
19-
players = [getattr(axelrod, n)() for n in names]
19+
players = [getattr(axl, n)() for n in names]
2020

2121
# Play the tournament and build the actual outcome tuples.
22-
tournament = axelrod.Tournament(
22+
tournament = axl.Tournament(
2323
players=players, game=cls.game, turns=turns, repetitions=1
2424
)
2525
results = tournament.play(progress_bar=False)

axelrod/tests/integration/test_tournament.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
import filecmp
21
import unittest
32

4-
from hypothesis import given, settings
3+
import filecmp
54

6-
import axelrod
5+
import axelrod as axl
76
from axelrod.load_data_ import axl_filename
87
from axelrod.strategy_transformers import FinalTransformer
98
from axelrod.tests.property import tournaments
109

10+
from hypothesis import given, settings
11+
1112

1213
class TestTournament(unittest.TestCase):
1314
@classmethod
1415
def setUpClass(cls):
15-
cls.game = axelrod.Game()
16+
cls.game = axl.Game()
1617
cls.players = [
17-
axelrod.Cooperator(),
18-
axelrod.TitForTat(),
19-
axelrod.Defector(),
20-
axelrod.Grudger(),
21-
axelrod.GoByMajority(),
18+
axl.Cooperator(),
19+
axl.TitForTat(),
20+
axl.Defector(),
21+
axl.Grudger(),
22+
axl.GoByMajority(),
2223
]
2324
cls.player_names = [str(p) for p in cls.players]
2425
cls.test_name = "test"
@@ -35,7 +36,7 @@ def setUpClass(cls):
3536

3637
@given(
3738
tournaments(
38-
strategies=axelrod.short_run_time_strategies,
39+
strategies=axl.short_run_time_strategies,
3940
min_size=10,
4041
max_size=30,
4142
min_turns=2,
@@ -54,7 +55,7 @@ def test_big_tournaments(self, tournament):
5455
)
5556

5657
def test_serial_play(self):
57-
tournament = axelrod.Tournament(
58+
tournament = axl.Tournament(
5859
name=self.test_name,
5960
players=self.players,
6061
game=self.game,
@@ -66,7 +67,7 @@ def test_serial_play(self):
6667
self.assertEqual(actual_outcome, self.expected_outcome)
6768

6869
def test_parallel_play(self):
69-
tournament = axelrod.Tournament(
70+
tournament = axl.Tournament(
7071
name=self.test_name,
7172
players=self.players,
7273
game=self.game,
@@ -81,12 +82,12 @@ def test_repeat_tournament_deterministic(self):
8182
"""A test to check that tournament gives same results."""
8283
deterministic_players = [
8384
s()
84-
for s in axelrod.short_run_time_strategies
85+
for s in axl.short_run_time_strategies
8586
if not s().classifier["stochastic"]
8687
]
8788
files = []
8889
for _ in range(2):
89-
tournament = axelrod.Tournament(
90+
tournament = axl.Tournament(
9091
name="test",
9192
players=deterministic_players,
9293
game=self.game,
@@ -105,13 +106,13 @@ def test_repeat_tournament_stochastic(self):
105106
"""
106107
files = []
107108
for _ in range(2):
108-
axelrod.seed(0)
109+
axl.seed(0)
109110
stochastic_players = [
110111
s()
111-
for s in axelrod.short_run_time_strategies
112+
for s in axl.short_run_time_strategies
112113
if s().classifier["stochastic"]
113114
]
114-
tournament = axelrod.Tournament(
115+
tournament = axl.Tournament(
115116
name="test",
116117
players=stochastic_players,
117118
game=self.game,
@@ -128,14 +129,14 @@ def test_repeat_tournament_stochastic(self):
128129
class TestNoisyTournament(unittest.TestCase):
129130
def test_noisy_tournament(self):
130131
# Defector should win for low noise
131-
players = [axelrod.Cooperator(), axelrod.Defector()]
132-
tournament = axelrod.Tournament(players, turns=5, repetitions=3, noise=0.0)
132+
players = [axl.Cooperator(), axl.Defector()]
133+
tournament = axl.Tournament(players, turns=5, repetitions=3, noise=0.0)
133134
results = tournament.play(progress_bar=False)
134135
self.assertEqual(results.ranked_names[0], "Defector")
135136

136137
# If the noise is large enough, cooperator should win
137-
players = [axelrod.Cooperator(), axelrod.Defector()]
138-
tournament = axelrod.Tournament(players, turns=5, repetitions=3, noise=0.75)
138+
players = [axl.Cooperator(), axl.Defector()]
139+
tournament = axl.Tournament(players, turns=5, repetitions=3, noise=0.75)
139140
results = tournament.play(progress_bar=False)
140141
self.assertEqual(results.ranked_names[0], "Cooperator")
141142

@@ -145,10 +146,10 @@ def test_players_do_not_know_match_length(self):
145146
"""Create two players who should cooperate on last two turns if they
146147
don't know when those last two turns are.
147148
"""
148-
p1 = FinalTransformer(["D", "D"])(axelrod.Cooperator)()
149-
p2 = FinalTransformer(["D", "D"])(axelrod.Cooperator)()
149+
p1 = FinalTransformer(["D", "D"])(axl.Cooperator)()
150+
p2 = FinalTransformer(["D", "D"])(axl.Cooperator)()
150151
players = [p1, p2]
151-
tournament = axelrod.Tournament(players, prob_end=0.5, repetitions=1)
152+
tournament = axl.Tournament(players, prob_end=0.5, repetitions=1)
152153
results = tournament.play(progress_bar=False)
153154
# Check that both plays always cooperated
154155
for rating in results.cooperating_rating:
@@ -159,12 +160,12 @@ def test_matches_have_different_length(self):
159160
A match between two players should have variable length across the
160161
repetitions
161162
"""
162-
p1 = axelrod.Cooperator()
163-
p2 = axelrod.Cooperator()
164-
p3 = axelrod.Cooperator()
163+
p1 = axl.Cooperator()
164+
p2 = axl.Cooperator()
165+
p3 = axl.Cooperator()
165166
players = [p1, p2, p3]
166-
axelrod.seed(0)
167-
tournament = axelrod.Tournament(players, prob_end=0.5, repetitions=2)
167+
axl.seed(0)
168+
tournament = axl.Tournament(players, prob_end=0.5, repetitions=2)
168169
results = tournament.play(progress_bar=False)
169170
# Check that match length are different across the repetitions
170171
self.assertNotEqual(results.match_lengths[0], results.match_lengths[1])

0 commit comments

Comments
 (0)