def test_invalid_input(self): """Test error-handling upon invalid input.""" # Invalid dimensions. with self.assertRaises(DissimilarityMatrixError): _ = randdm(0) # Invalid dimensions. with self.assertRaises(ValueError): _ = randdm(-1) # Invalid number of IDs. with self.assertRaises(DissimilarityMatrixError): _ = randdm(2, ids=['foo'])
def test_default_usage(self): """Test generating random distance matrices.""" exp = DistanceMatrix(np.asarray([[0.0]]), ['1']) obs = randdm(1) self.assertEqual(obs, exp) obs = randdm(2) self.assertEqual(obs.shape, (2, 2)) self.assertEqual(obs.ids, ('1', '2')) obs1 = randdm(5) num_trials = 10 found_diff = False for _ in range(num_trials): obs2 = randdm(5) if obs1 != obs2: found_diff = True break self.assertTrue(found_diff)
def test_random_fn(self): """Test passing a different random function than the default.""" def myrand(num_rows, num_cols): # One dm to rule them all... data = np.empty((num_rows, num_cols)) data.fill(42) return data exp = DistanceMatrix(np.asarray([[0, 42, 42], [42, 0, 42], [42, 42, 0]]), ['1', '2', '3']) obs = randdm(3, random_fn=myrand) self.assertEqual(obs, exp)
def test_random_fn(self): """Test passing a different random function than the default.""" def myrand(num_rows, num_cols): # One dm to rule them all... data = np.empty((num_rows, num_cols)) data.fill(42) return data exp = DistanceMatrix( np.asarray([[0, 42, 42], [42, 0, 42], [42, 42, 0]]), ['1', '2', '3']) obs = randdm(3, random_fn=myrand) self.assertEqual(obs, exp)
def test_constructor(self): """Test generating random dist mats with a specific constructor.""" exp = DissimilarityMatrix(np.asarray([[0.0]]), ['1']) obs = randdm(1, constructor=DissimilarityMatrix) self.assertEqual(obs, exp) self.assertEqual(type(obs), DissimilarityMatrix)
def test_ids(self): """Test generating random distance matrices with specific IDs.""" ids = ['foo', 'bar', 'baz'] obs = randdm(3, ids=ids) self.assertEqual(obs.shape, (3, 3)) self.assertEqual(obs.ids, tuple(ids))