File: test_svd.py

package info (click to toggle)
python-cogent 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,424 kB
  • ctags: 24,343
  • sloc: python: 134,200; makefile: 100; ansic: 17; sh: 10
file content (100 lines) | stat: -rw-r--r-- 3,978 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
"""Unit tests for the svd-supporting functionality."""

from cogent.util.unit_test import TestCase, main
from cogent.maths.svd import ratio_two_best, ratio_best_to_sum, \
     euclidean_distance, euclidean_norm, _dists_from_mean_slow, \
     dists_from_v, weiss, three_item_combos, two_item_combos
from numpy import array, sqrt

__author__ = "Rob Knight"
__copyright__ = "Copyright 2007-2012, The Cogent Project"
__contributors__ = ["Rob Knight", "Daniel McDonald"]
__license__ = "GPL"
__version__ = "1.5.3"
__maintainer__ = "Rob Knight"
__email__ = "rob@spot.colorado.edu"
__status__ = "Production"

class functionTests(TestCase):
    """Tests of top-level functions."""
    
    def test_ratio_two_best(self):
        """ratio_two_best should return ratio of two biggest items in list"""
        v = array([3, 2, 5, 2, 4, 10, 3])
        self.assertEqual(ratio_two_best(v), 2)
        #should return 1 if items the same
        v = array([2,2,2,2,2])
        self.assertEqual(ratio_two_best(v), 1)
        #check that it works on floating-point
        v = array([3,2,1])
        self.assertEqual(ratio_two_best(v), 1.5)

    def test_ratio_best_to_sum(self):
        """ratio_best_to_sum should return ratio of biggest item to sum"""
        v = [3, 2, 5, 2, 4, 10, 3]
        self.assertFloatEqual(ratio_best_to_sum(v), 10/29.0)
        v = [2,2,2,2,2]
        self.assertEqual(ratio_best_to_sum(v), 2/10.0)
        #check that it works on floating-point
        v = [3,2,1]
        self.assertEqual(ratio_best_to_sum(v), 0.5)

    def test_euclidean_distance(self):
        """euclidean_distance should return distance between two points"""
        first = array([2, 3, 4])
        second = array([4, 8, 10])
        self.assertEqual(euclidean_distance(first, first), 0)
        self.assertEqual(euclidean_distance(second, second), 0)
        self.assertFloatEqual(euclidean_distance(first, second), sqrt(65))
        self.assertFloatEqual(euclidean_distance(second, first), sqrt(65))

    def test_euclidean_norm(self):
        """euclidean_norm should match hand-calculated results"""
        first = array([3,4])
        self.assertEqual(euclidean_norm(first), 5)

    def test_dists_from_mean_slow(self):
        """_dists_from_mean_slow should return distance of each item from mean"""
        m = [[1,2,3,4],[2,3,4,5],[0,1,2,3]]
        self.assertEqual(_dists_from_mean_slow(m), array([0.0,2.0,2.0]))

    def test_dists_from_v(self):
        """dists_from_v should return distance of each item from v, or mean"""
        m = [[1,2,3,4],[2,3,4,5],[0,1,2,3]]
        #should calculate distances from mean by default
        self.assertEqual(dists_from_v(m), array([0.0,2.0,2.0]))
        #should caculate distances from vector if supplied
        v = array([2,2,2,3])
        self.assertEqual(dists_from_v(m, v), sqrt(array([3,9,5])))
        
    def test_weiss(self):
        """weiss should perform weiss calculation correctly"""
        e = array([12.0, 5.0, 0.1, 1e-3, 1e-15])
        self.assertFloatEqual(weiss(e), 4.453018506827001)

    def test_three_item_combos(self):
        """three_item_combos should return items in correct order"""
        items = list(three_item_combos('abcde'))
        self.assertEqual(items, map(tuple, \
            ['abc','abd','abe','acd','ace','ade','bcd','bce','bde','cde']))

    def test_two_item_combos(self):
        """two_item_combos should return items in correct order"""
        items = list(two_item_combos('abcd'))
        self.assertEqual(items, map(tuple, ['ab','ac','ad','bc','bd','cd']))

    def test_pca_qs(self):
        """pca_qs not tested b/c it just wraps eigenvalues(corrcoef(qs))"""
        pass

    def test_pca_cov_qs(self):
        """pca_cov_qs not tested b/c it just wraps eigenvalues(cov(qs))"""
        pass

    def test_svd_qs(self):
        """svd_qs not tested b/c it just wraps singular_value_decompositon(qs)"""
        pass

if __name__ == '__main__':
    main()