def test_random_fn(self): 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_default_usage(self): 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_invalid_input(self): # 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_constructor(self): 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): ids = ['foo', 'bar', 'baz'] obs = randdm(3, ids=ids) self.assertEqual(obs.shape, (3, 3)) self.assertEqual(obs.ids, tuple(ids))
def test_large_matrix_for_symmetry(self): obs3 = randdm(100) self.assertEqual(obs3, obs3.T)
def _create_distance_matrix(self, sample_ids): dm = randdm(len(sample_ids), sample_ids) fd, fp = mkstemp(suffix='.txt', dir=self.out_dir) close(fd) dm.write(fp) return fp
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))