Fix flaky float tests

This commit is contained in:
2021-05-28 17:28:22 +02:00
parent 27f772e496
commit ee5020e6d9

View File

@@ -15,6 +15,11 @@ class MFS_test(unittest.TestCase):
mdlp = MDLP(random_state=1) mdlp = MDLP(random_state=1)
self.X_i = mdlp.fit_transform(X, self.y_i).astype("int64") self.X_i = mdlp.fit_transform(X, self.y_i).astype("int64")
def assertListAlmostEqual(self, list1, list2, tol=7):
self.assertEqual(len(list1), len(list2))
for a, b in zip(list1, list2):
self.assertAlmostEqual(a, b, tol)
def test_initialize(self): def test_initialize(self):
mfs = MFS() mfs = MFS()
mfs.fcbs(self.X_w, self.y_w, 0.05) mfs.fcbs(self.X_w, self.y_w, 0.05)
@@ -27,7 +32,7 @@ class MFS_test(unittest.TestCase):
def test_csf_wine(self): def test_csf_wine(self):
mfs = MFS() mfs = MFS()
expected = [6, 12, 9, 4, 10, 0] expected = [6, 12, 9, 4, 10, 0]
self.assertListEqual( self.assertListAlmostEqual(
expected, mfs.cfs(self.X_w, self.y_w).get_results() expected, mfs.cfs(self.X_w, self.y_w).get_results()
) )
expected = [ expected = [
@@ -38,26 +43,26 @@ class MFS_test(unittest.TestCase):
0.28795671854246285, 0.28795671854246285,
0.2309165735173175, 0.2309165735173175,
] ]
self.assertListEqual(expected, mfs.get_scores()) self.assertListAlmostEqual(expected, mfs.get_scores())
def test_csf_iris(self): def test_csf_iris(self):
mfs = MFS() mfs = MFS()
expected = [3, 2, 0, 1] expected = [3, 2, 0, 1]
computed = mfs.cfs(self.X_i, self.y_i).get_results() computed = mfs.cfs(self.X_i, self.y_i).get_results()
self.assertListEqual(expected, computed) self.assertListAlmostEqual(expected, computed)
expected = [ expected = [
0.870521418179061, 0.870521418179061,
0.8968651482682227, 0.8968651482682227,
0.5908278453318913, 0.5908278453318913,
0.40371971570693366, 0.40371971570693366,
] ]
self.assertListEqual(expected, mfs.get_scores()) self.assertListAlmostEqual(expected, mfs.get_scores())
def test_fcbs_wine(self): def test_fcbs_wine(self):
mfs = MFS() mfs = MFS()
computed = mfs.fcbs(self.X_w, self.y_w, threshold=0.05).get_results() computed = mfs.fcbs(self.X_w, self.y_w, threshold=0.05).get_results()
expected = [6, 9, 12, 0, 11, 4] expected = [6, 9, 12, 0, 11, 4]
self.assertListEqual(expected, computed) self.assertListAlmostEqual(expected, computed)
expected = [ expected = [
0.5218299405215557, 0.5218299405215557,
0.46224298637417455, 0.46224298637417455,
@@ -66,23 +71,23 @@ class MFS_test(unittest.TestCase):
0.3790082191220976, 0.3790082191220976,
0.24972405134844652, 0.24972405134844652,
] ]
self.assertListEqual(expected, mfs.get_scores()) self.assertListAlmostEqual(expected, mfs.get_scores())
def test_fcbs_iris(self): def test_fcbs_iris(self):
mfs = MFS() mfs = MFS()
computed = mfs.fcbs(self.X_i, self.y_i, threshold=0.05).get_results() computed = mfs.fcbs(self.X_i, self.y_i, threshold=0.05).get_results()
expected = [3, 2] expected = [3, 2]
self.assertListEqual(expected, computed) self.assertListAlmostEqual(expected, computed)
expected = [0.870521418179061, 0.810724587460511] expected = [0.870521418179061, 0.810724587460511]
self.assertListEqual(expected, mfs.get_scores()) self.assertListAlmostEqual(expected, mfs.get_scores())
def test_compute_su_labels(self): def test_compute_su_labels(self):
mfs = MFS() mfs = MFS()
mfs.fcbs(self.X_i, self.y_i, threshold=0.05) mfs.fcbs(self.X_i, self.y_i, threshold=0.05)
expected = [0.0, 0.0, 0.810724587460511, 0.870521418179061] expected = [0.0, 0.0, 0.810724587460511, 0.870521418179061]
self.assertListEqual(expected, mfs._compute_su_labels().tolist()) self.assertListAlmostEqual(expected, mfs._compute_su_labels().tolist())
mfs._su_labels = [1, 2, 3, 4] mfs._su_labels = [1, 2, 3, 4]
self.assertListEqual([1, 2, 3, 4], mfs._compute_su_labels()) self.assertListAlmostEqual([1, 2, 3, 4], mfs._compute_su_labels())
def test_invalid_threshold(self): def test_invalid_threshold(self):
mfs = MFS() mfs = MFS()
@@ -93,10 +98,10 @@ class MFS_test(unittest.TestCase):
mfs = MFS() mfs = MFS()
computed = mfs.fcbs(self.X_w, self.y_w, threshold=0.4).get_results() computed = mfs.fcbs(self.X_w, self.y_w, threshold=0.4).get_results()
expected = [6, 9, 12] expected = [6, 9, 12]
self.assertListEqual(expected, computed) self.assertListAlmostEqual(expected, computed)
expected = [ expected = [
0.5218299405215557, 0.5218299405215557,
0.46224298637417455, 0.46224298637417455,
0.44518278979085646, 0.44518278979085646,
] ]
self.assertListEqual(expected, mfs.get_scores()) self.assertListAlmostEqual(expected, mfs.get_scores())