Skip to content

Commit 21ca728

Browse files
committed
Merge from origin
2 parents 122e4ce + d4735cb commit 21ca728

File tree

87 files changed

+2634
-2622
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

+2634
-2622
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: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import unittest
22
import warnings
33

4+
import axelrod as axl
5+
from axelrod.tests.property import strategy_lists
6+
47
from hypothesis import example, given, settings
58
from hypothesis.strategies import integers
69

7-
from axelrod import filtered_strategies, seed, short_run_time_strategies
8-
from axelrod import Classifiers
9-
from axelrod.tests.property import strategy_lists
10-
1110

1211
class TestFiltersAgainstComprehensions(unittest.TestCase):
1312
"""
1413
Test that the results of filtering strategies via a filterset dict
1514
match the results from using a list comprehension.
1615
"""
16+
1717
def setUp(self) -> None:
1818
# Ignore warnings about classifiers running on instances
1919
warnings.simplefilter("ignore", category=UserWarning)
@@ -33,9 +33,9 @@ def test_boolean_filtering(self, strategies):
3333
]
3434

3535
for classifier in classifiers:
36-
comprehension = set(filter(Classifiers[classifier], strategies))
36+
comprehension = set(filter(axl.Classifiers[classifier], strategies))
3737
filterset = {classifier: True}
38-
filtered = set(filtered_strategies(filterset, strategies=strategies))
38+
filtered = set(axl.filtered_strategies(filterset, strategies=strategies))
3939
self.assertEqual(comprehension, filtered)
4040

4141
@given(
@@ -48,43 +48,54 @@ def test_boolean_filtering(self, strategies):
4848
min_memory_depth=float("inf"),
4949
max_memory_depth=float("inf"),
5050
memory_depth=float("inf"),
51-
strategies=short_run_time_strategies,
51+
strategies=axl.short_run_time_strategies,
5252
)
5353
@settings(max_examples=5)
5454
def test_memory_depth_filtering(
55-
self, min_memory_depth, max_memory_depth, memory_depth,
56-
strategies
55+
self, min_memory_depth, max_memory_depth, memory_depth, strategies
5756
):
5857

5958
min_comprehension = set(
60-
[s for s in strategies if
61-
Classifiers["memory_depth"](s) >= min_memory_depth]
59+
[
60+
s
61+
for s in strategies
62+
if axl.Classifiers["memory_depth"](s) >= min_memory_depth
63+
]
6264
)
6365
min_filterset = {"min_memory_depth": min_memory_depth}
64-
min_filtered = set(filtered_strategies(min_filterset,
65-
strategies=strategies))
66+
min_filtered = set(
67+
axl.filtered_strategies(min_filterset, strategies=strategies)
68+
)
6669
self.assertEqual(min_comprehension, min_filtered)
6770

6871
max_comprehension = set(
69-
[s for s in strategies if
70-
Classifiers["memory_depth"](s) <= max_memory_depth]
72+
[
73+
s
74+
for s in strategies
75+
if axl.Classifiers["memory_depth"](s) <= max_memory_depth
76+
]
7177
)
7278
max_filterset = {"max_memory_depth": max_memory_depth}
73-
max_filtered = set(filtered_strategies(max_filterset,
74-
strategies=strategies))
79+
max_filtered = set(
80+
axl.filtered_strategies(max_filterset, strategies=strategies)
81+
)
7582
self.assertEqual(max_comprehension, max_filtered)
7683

7784
comprehension = set(
78-
[s for s in strategies if
79-
Classifiers["memory_depth"](s) == memory_depth]
85+
[
86+
s
87+
for s in strategies
88+
if axl.Classifiers["memory_depth"](s) == memory_depth
89+
]
8090
)
8191
filterset = {"memory_depth": memory_depth}
82-
filtered = set(filtered_strategies(filterset, strategies=strategies))
92+
filtered = set(axl.filtered_strategies(filterset, strategies=strategies))
8393
self.assertEqual(comprehension, filtered)
8494

85-
@given(seed_=integers(min_value=0, max_value=4294967295),
86-
strategies=strategy_lists(min_size=20, max_size=20),
87-
)
95+
@given(
96+
seed_=integers(min_value=0, max_value=4294967295),
97+
strategies=strategy_lists(min_size=20, max_size=20),
98+
)
8899
@settings(max_examples=5)
89100
def test_makes_use_of_filtering(self, seed_, strategies):
90101
"""
@@ -95,16 +106,18 @@ def test_makes_use_of_filtering(self, seed_, strategies):
95106
classifiers = [["game"], ["length"], ["game", "length"]]
96107

97108
for classifier in classifiers:
98-
seed(seed_)
109+
axl.seed(seed_)
99110
comprehension = set(
100-
[s for s in strategies if set(classifier).issubset(
101-
set(Classifiers["makes_use_of"](s)))]
111+
[
112+
s
113+
for s in strategies
114+
if set(classifier).issubset(set(axl.Classifiers["makes_use_of"](s)))
115+
]
102116
)
103117

104-
seed(seed_)
118+
axl.seed(seed_)
105119
filterset = {"makes_use_of": classifier}
106-
filtered = set(
107-
filtered_strategies(filterset, strategies=strategies))
120+
filtered = set(axl.filtered_strategies(filterset, strategies=strategies))
108121

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

axelrod/tests/integration/test_matches.py

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

4+
import axelrod as axl
5+
from axelrod.tests.property import strategy_lists
6+
47
from hypothesis import given, settings
58
from hypothesis.strategies import integers
69

7-
import axelrod
8-
from axelrod import Classifiers
9-
from axelrod.tests.property import strategy_lists
10-
11-
C, D = axelrod.Action.C, axelrod.Action.D
10+
C, D = axl.Action.C, axl.Action.D
1211

1312
deterministic_strategies = [
14-
s for s in axelrod.short_run_time_strategies if not Classifiers["stochastic"](s())
13+
s for s in axl.short_run_time_strategies if not axl.Classifiers["stochastic"](s())
1514
]
1615
stochastic_strategies = [
17-
s for s in axelrod.short_run_time_strategies if Classifiers["stochastic"](s())
16+
s for s in axl.short_run_time_strategies if axl.Classifiers["stochastic"](s())
1817
]
1918

2019

@@ -30,7 +29,7 @@ def test_outcome_repeats(self, strategies, turns):
3029
"""A test that if we repeat 3 matches with deterministic and well
3130
behaved strategies then we get the same result"""
3231
players = [s() for s in strategies]
33-
matches = [axelrod.Match(players, turns) for _ in range(3)]
32+
matches = [axl.Match(players, turns) for _ in range(3)]
3433
self.assertEqual(matches[0].play(), matches[1].play())
3534
self.assertEqual(matches[1].play(), matches[2].play())
3635

@@ -47,9 +46,9 @@ def test_outcome_repeats_stochastic(self, strategies, turns, seed):
4746
same result"""
4847
results = []
4948
for _ in range(3):
50-
axelrod.seed(seed)
49+
axl.seed(seed)
5150
players = [s() for s in strategies]
52-
results.append(axelrod.Match(players, turns).play())
51+
results.append(axl.Match(players, turns).play())
5352

5453
self.assertEqual(results[0], results[1])
5554
self.assertEqual(results[1], results[2])
@@ -58,15 +57,15 @@ def test_matches_with_det_player_for_stochastic_classes(self):
5857
"""A test based on a bug found in the cache.
5958
6059
See: https://github.com/Axelrod-Python/Axelrod/issues/779"""
61-
p1 = axelrod.MemoryOnePlayer(four_vector=(0, 0, 0, 0))
62-
p2 = axelrod.MemoryOnePlayer(four_vector=(1, 0, 1, 0))
63-
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))
6463

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

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

71-
m = axelrod.Match((p1, p3), turns=3)
70+
m = axl.Match((p1, p3), turns=3)
7271
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,24 +1,24 @@
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
# Extract the name of players from the outcome tuples,
1616
# and initiate the players by getting the classes from axelrod.
1717
names = [out[0] for out in outcome]
18-
players = [getattr(axelrod, n)() for n in names]
18+
players = [getattr(axl, n)() for n in names]
1919

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

0 commit comments

Comments
 (0)