Skip to content

Commit 9deee55

Browse files
committed
Fix nans on eigenmoses variable, and add some tests.
1 parent 251a430 commit 9deee55

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

axelrod/result_set.py

Lines changed: 14 additions & 2 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
@@ -609,6 +610,17 @@ def __eq__(self, other):
609610
other : axelrod.ResultSet
610611
Another results set against which to check equality
611612
"""
613+
def list_equal(v1: List[float], v2: List[float]) -> bool:
614+
"""Matches lists, accounting for NaNs."""
615+
if len(v1) != len(v2):
616+
return False
617+
for i1, i2 in zip(v1, v2):
618+
if np.isnan(i1) and np.isnan(i2):
619+
continue
620+
if i1 != i2:
621+
return False
622+
return True
623+
612624
return all(
613625
[
614626
self.wins == other.wins,
@@ -628,8 +640,8 @@ def __eq__(self, other):
628640
self.cooperating_rating == other.cooperating_rating,
629641
self.good_partner_matrix == other.good_partner_matrix,
630642
self.good_partner_rating == other.good_partner_rating,
631-
self.eigenmoses_rating == other.eigenmoses_rating,
632-
self.eigenjesus_rating == other.eigenjesus_rating,
643+
list_equal(self.eigenmoses_rating, other.eigenmoses_rating),
644+
list_equal(self.eigenjesus_rating, other.eigenjesus_rating),
633645
]
634646
)
635647

axelrod/tests/unit/test_eigen.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def test_identity_matrices(self):
1515
self.assertAlmostEqual(evalue, 1)
1616
assert_array_almost_equal(evector, _normalise(numpy.ones(size)))
1717

18+
def test_zero_matrix(self):
19+
mat = numpy.array([[0, 0], [0, 0]])
20+
evector, evalue = principal_eigenvector(mat)
21+
self.assertTrue(numpy.isnan(evalue))
22+
self.assertTrue(numpy.isnan(evector[0]))
23+
self.assertTrue(numpy.isnan(evector[1]))
24+
1825
def test_2x2_matrix(self):
1926
mat = numpy.array([[2, 1], [1, 2]])
2027
evector, evalue = principal_eigenvector(mat)

axelrod/tests/unit/test_resultset.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd
88
from axelrod.result_set import create_counter_dict
99
from axelrod.tests.property import prob_end_tournaments, tournaments
10-
from numpy import mean, nanmedian, std
10+
from numpy import mean, nan, nanmedian, std
1111

1212
from dask.dataframe.core import DataFrame
1313
from hypothesis import given, settings
@@ -190,6 +190,34 @@ def test_init(self):
190190
self.assertEqual(rs.players, self.players)
191191
self.assertEqual(rs.num_players, len(self.players))
192192

193+
def _clear_matrix(self, matrix):
194+
for i in range(len(matrix)):
195+
for j in range(len(matrix[i])):
196+
matrix[i][j] = 0
197+
198+
def test_nan_vectors(self):
199+
rs_1 = axelrod.ResultSet(
200+
self.filename,
201+
self.players,
202+
self.repetitions
203+
)
204+
# Force a broken eigenmoses, by replacing vengeful_cooperation with
205+
# zeroes.
206+
self._clear_matrix(rs_1.vengeful_cooperation)
207+
rs_1.eigenmoses_rating = rs_1._build_eigenmoses_rating()
208+
209+
rs_2 = axelrod.ResultSet(
210+
self.filename,
211+
self.players,
212+
self.repetitions
213+
)
214+
# Force a broken eigenmoses, by replacing vengeful_cooperation with
215+
# zeroes.
216+
self._clear_matrix(rs_2.vengeful_cooperation)
217+
rs_2.eigenmoses_rating = rs_2._build_eigenmoses_rating()
218+
219+
self.assertEqual(rs_1, rs_2)
220+
193221
def test_init_multiprocessing(self):
194222
rs = axelrod.ResultSet(
195223
self.filename,

0 commit comments

Comments
 (0)