Skip to content

Commit

Permalink
remove six from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaslovsky committed Aug 27, 2020
1 parent 847c0d0 commit a6458c8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
21 changes: 10 additions & 11 deletions tests/test_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from collections import Counter

import numpy as np
from six import iteritems

import coupled_biased_random_walks.count as cnt

Expand All @@ -26,7 +25,7 @@ def test_insert(self):
'expected_dict': {'a': 0, 'c': 1, 'b': 2, 'd': 3}
}
}
for test_name, test in iteritems(table):
for test_name, test in table.items():
self.setUp()
for key in test['keys_to_insert']:
self.d.insert(key)
Expand Down Expand Up @@ -61,7 +60,7 @@ class TestObservationCounter(unittest.TestCase):
# keep a set of all feature_name, feature_val pairs for testing
all_index_keys = set()
for observation in observations:
for item in iteritems(observation):
for item in observation.items():
all_index_keys.add(item)

def setUp(self):
Expand All @@ -75,7 +74,7 @@ def test_update(self):
'feature_b': 2,
'feature_c': 2
}
for feature_name, count in iteritems(self.oc.n_obs):
for feature_name, count in self.oc.n_obs.items():
self.assertEqual(count, expected_counts[feature_name])

# test index
Expand Down Expand Up @@ -103,7 +102,7 @@ def test_update(self):
]
}
}
for feature, test in iteritems(table):
for feature, test in table.items():
counts = self.oc.counts[feature]
expected = sorted(test['expected'])
self.assertListEqual(sorted(list(counts.items())), expected, feature)
Expand Down Expand Up @@ -137,7 +136,7 @@ def test_get_count(self):
'expected': 0
},
}
for test_name, test in iteritems(table):
for test_name, test in table.items():
count = self.oc.get_count(test['feature tuple'])
expected = test['expected']
self.assertEqual(count, expected, test_name)
Expand All @@ -156,7 +155,7 @@ class TestObservationCounterWithMissingData(unittest.TestCase):
# keep a set of all feature_name, feature_val pairs for testing
all_index_keys = set()
for observation in observations:
for item in iteritems(observation):
for item in observation.items():
if not cnt.isnan(cnt.get_feature_value(item)):
all_index_keys.add(item)

Expand All @@ -171,7 +170,7 @@ def test_update(self):
'feature_b': 1,
'feature_c': 1
}
for feature_name, count in iteritems(self.oc.n_obs):
for feature_name, count in self.oc.n_obs.items():
self.assertEqual(count, expected_counts[feature_name])

# test index
Expand Down Expand Up @@ -201,7 +200,7 @@ def test_update(self):
'expected': []
}
}
for feature, test in iteritems(table):
for feature, test in table.items():
counts = self.oc.counts.get(feature, {})
expected = sorted(test['expected'])
self.assertListEqual(sorted(list(counts.items())), expected, feature)
Expand Down Expand Up @@ -250,7 +249,7 @@ def test_isnan(self):
'expected': False
},
}
for test_name, test in iteritems(table):
for test_name, test in table.items():
isnan_result = cnt.isnan(test['test'])
self.assertEqual(isnan_result, test['expected'], test_name)

Expand Down Expand Up @@ -281,7 +280,7 @@ def test_get_mode(self):
'expected': 2
},
}
for test_name, test in iteritems(table):
for test_name, test in table.items():
mode = cnt.get_mode(test['counter'])
self.assertEqual(mode, test['expected'], test_name)

Expand Down
26 changes: 14 additions & 12 deletions tests/test_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

import numpy as np
from scipy.sparse import csr_matrix
from six import itervalues

from coupled_biased_random_walks.count import isnan
from coupled_biased_random_walks.detect import (CBRW, CBRWFitError,
CBRWScoreError)
from coupled_biased_random_walks.detect import (
CBRW,
CBRWFitError,
CBRWScoreError,
)


class TestCBRW(unittest.TestCase):
Expand Down Expand Up @@ -231,7 +233,7 @@ def test_value_scores(self):
value_scores = self.cbrw.value_scores(to_be_scored)
value_scores = value_scores[0]
self.assertListEqual(sorted(value_scores.keys()), sorted(to_be_scored.keys()))
for vs in itervalues(value_scores):
for vs in value_scores.values():
self.assertGreaterEqual(vs, 0)
self.assertLessEqual(vs, 1)

Expand Down Expand Up @@ -272,8 +274,8 @@ def test_value_scores_unknown_features_default(self):
value_scores = self.cbrw.value_scores(to_be_scored)
valid_scores = value_scores[0]
invalid_scores = value_scores[1]
self.assertTrue(all(not isnan(valid_score) for valid_score in itervalues(valid_scores)))
self.assertTrue(any(isnan(invalid_score) for invalid_score in itervalues(invalid_scores)))
self.assertTrue(all(not isnan(valid_score) for valid_score in valid_scores.values()))
self.assertTrue(any(isnan(invalid_score) for invalid_score in invalid_scores.values()))

def test_value_scores_unknown_features_ignore(self):
self.cbrw = CBRW(ignore_unknown=True)
Expand All @@ -293,7 +295,7 @@ def test_value_scores_unknown_features_ignore(self):
}
value_scores = self.cbrw.value_scores(to_be_scored)[0]
actual_value_scores = self.cbrw.value_scores(actually_scored)[0]
self.assertTrue(all(not isnan(vs) for vs in itervalues(value_scores)))
self.assertTrue(all(not isnan(vs) for vs in value_scores.values()))
self.assertEqual(value_scores['feature_a'], 0)
self.assertEqual(value_scores['feature_b'], actual_value_scores['feature_b'])
self.assertEqual(value_scores['feature_c'], actual_value_scores['feature_c'])
Expand All @@ -311,7 +313,7 @@ def test_value_scores_unknown_features_ignore(self):
}
value_scores = self.cbrw.value_scores(to_be_scored)[0]
actual_value_scores = self.cbrw.value_scores(actually_scored)[0]
self.assertTrue(all(not isnan(vs) for vs in itervalues(value_scores)))
self.assertTrue(all(not isnan(vs) for vs in value_scores.values()))
self.assertEqual(value_scores['feature_x'], 0)
self.assertEqual(value_scores['feature_b'], actual_value_scores['feature_b'])
self.assertEqual(value_scores['feature_c'], actual_value_scores['feature_c'])
Expand All @@ -324,8 +326,8 @@ def test_value_scores_unknown_features_ignore(self):
'feature_z': 'z_val_1'
}
value_scores = self.cbrw.value_scores(to_be_scored)[0]
self.assertTrue(all(not isnan(vs) for vs in itervalues(value_scores)))
self.assertTrue(all(vs == 0 for vs in itervalues(value_scores)))
self.assertTrue(all(not isnan(vs) for vs in value_scores.values()))
self.assertTrue(all(vs == 0 for vs in value_scores.values()))

def test_value_scores_with_nans_default(self):
obs = deepcopy(self.observations)
Expand Down Expand Up @@ -369,7 +371,7 @@ def test_value_scores_with_nans_ignore(self):
self.cbrw.fit()
value_scores = self.cbrw.value_scores(to_be_scored)[0]
actual_value_scores = self.cbrw.value_scores(actually_scored)[0]
self.assertTrue(all(not isnan(vs) for vs in itervalues(value_scores)))
self.assertTrue(all(not isnan(vs) for vs in value_scores.values()))
self.assertEqual(value_scores['feature_a'], 0)
self.assertEqual(value_scores['feature_b'], actual_value_scores['feature_b'])
self.assertEqual(value_scores['feature_c'], actual_value_scores['feature_c'])
Expand All @@ -380,7 +382,7 @@ def test_value_scores_with_nans_ignore(self):
self.cbrw.fit()
value_scores = self.cbrw.value_scores(to_be_scored)[0]
actual_value_scores = self.cbrw.value_scores(actually_scored)[0]
self.assertTrue(all(not isnan(vs) for vs in itervalues(value_scores)))
self.assertTrue(all(not isnan(vs) for vs in value_scores.values()))
self.assertEqual(value_scores['feature_a'], 0)
self.assertEqual(value_scores['feature_b'], actual_value_scores['feature_b'])
self.assertEqual(value_scores['feature_c'], actual_value_scores['feature_c'])
16 changes: 8 additions & 8 deletions tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import numpy as np
from scipy.sparse import csr_matrix
from six import iteritems
from six.moves import zip

from coupled_biased_random_walks.matrix import (dict_to_csr_matrix,
random_walk,
row_normalize_csr_matrix)
from coupled_biased_random_walks.matrix import (
dict_to_csr_matrix,
random_walk,
row_normalize_csr_matrix,
)

np.random.seed(0)

Expand Down Expand Up @@ -85,7 +85,7 @@ def test_dict_to_csr_matrix(self):
}
}

for test_name, params in iteritems(table):
for test_name, params in table.items():
data_dict = params['data_dict']
shape = params['shape']
expected = params['expected']
Expand Down Expand Up @@ -128,13 +128,13 @@ def test_valid_row_normalize(self):
}
}

for test_name, test in iteritems(valid_table):
for test_name, test in valid_table.items():
matrix = construct_2x2_csr_matrix(test['data'])
normalized = row_normalize_csr_matrix(matrix)
row_sums = normalized.sum(axis=1)
self.assertAlmostEqual(row_sums[0], test['expected_row_0'], 3, test_name)
self.assertAlmostEqual(row_sums[1], test['expected_row_1'], 3, test_name)

for test_name, test in iteritems(invalid_table):
for test_name, test in invalid_table.items():
with self.assertRaises(test['exception']):
_ = row_normalize_csr_matrix(test['input'])

0 comments on commit a6458c8

Please sign in to comment.