Skip to content

Commit 053c4be

Browse files
Merge pull request #1305 from gaffney2010/fix-rs
Fix ResultSet __eq__ to handle nans
2 parents 68601b5 + 457bf7f commit 053c4be

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
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/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: 31 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,36 @@ 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, row in enumerate(matrix):
195+
for j, _ in enumerate(row):
196+
matrix[i][j] = 0
197+
198+
def test_ne_vectors(self):
199+
rs_1 = axelrod.ResultSet(self.filename, self.players, self.repetitions)
200+
201+
rs_2 = axelrod.ResultSet(self.filename, self.players, self.repetitions)
202+
203+
# A different vector
204+
rs_2.eigenmoses_rating = (-1, -1, -1)
205+
206+
self.assertNotEqual(rs_1, rs_2)
207+
208+
def test_nan_vectors(self):
209+
rs_1 = axelrod.ResultSet(self.filename, self.players, self.repetitions)
210+
# Force a broken eigenmoses, by replacing vengeful_cooperation with
211+
# zeroes.
212+
self._clear_matrix(rs_1.vengeful_cooperation)
213+
rs_1.eigenmoses_rating = rs_1._build_eigenmoses_rating()
214+
215+
rs_2 = axelrod.ResultSet(self.filename, self.players, self.repetitions)
216+
# Force a broken eigenmoses, by replacing vengeful_cooperation with
217+
# zeroes.
218+
self._clear_matrix(rs_2.vengeful_cooperation)
219+
rs_2.eigenmoses_rating = rs_2._build_eigenmoses_rating()
220+
221+
self.assertEqual(rs_1, rs_2)
222+
193223
def test_init_multiprocessing(self):
194224
rs = axelrod.ResultSet(
195225
self.filename,

0 commit comments

Comments
 (0)