Exemple #1
0
def pcoa(lines):
    """Run PCoA on the distance matrix present on lines"""
    # Parse the distance matrix
    dist_mtx = DistanceMatrix.from_file(lines)
    # Create the PCoA object
    pcoa_obj = PCoA(dist_mtx)
    # Get the PCoA results and return them
    return pcoa_obj.scores()
Exemple #2
0
 def setup(self):
     with open(get_data_path('PCoA_sample_data_3'), 'U') as lines:
         dist_matrix = DistanceMatrix.from_file(lines)
     self.ordination = PCoA(dist_matrix)
     self.ids = [
         'PC.636', 'PC.635', 'PC.356', 'PC.481', 'PC.354', 'PC.593',
         'PC.355', 'PC.607', 'PC.634'
     ]
Exemple #3
0
class TestPCoAResultsExtensive(object):
    def setup(self):
        matrix = np.loadtxt(get_data_path('PCoA_sample_data_2'))
        self.ids = map(str, range(matrix.shape[0]))
        dist_matrix = DistanceMatrix(matrix, self.ids)
        self.ordination = PCoA(dist_matrix)

    def test_values(self):
        results = self.ordination.scores()

        npt.assert_almost_equal(len(results.eigvals), len(results.species[0]))

        expected = np.array([[-0.028597, 0.22903853, 0.07055272,
                              0.26163576, 0.28398669, 0.0],
                             [0.37494056, 0.22334055, -0.20892914,
                              0.05057395, -0.18710366, 0.0],
                             [-0.33517593, -0.23855979, -0.3099887,
                              0.11521787, -0.05021553, 0.0],
                             [0.25412394, -0.4123464, 0.23343642,
                              0.06403168, -0.00482608, 0.0],
                             [-0.28256844, 0.18606911, 0.28875631,
                              -0.06455635, -0.21141632, 0.0],
                             [0.01727687, 0.012458, -0.07382761,
                              -0.42690292, 0.1695749, 0.0]])
        npt.assert_almost_equal(*normalize_signs(expected, results.species))

        expected = np.array([0.3984635, 0.36405689, 0.28804535, 0.27479983,
                            0.19165361, 0.0])
        npt.assert_almost_equal(results.eigvals, expected)

        expected = np.array([0.2626621381, 0.2399817314, 0.1898758748,
                             0.1811445992, 0.1263356565, 0.0])
        npt.assert_almost_equal(results.proportion_explained, expected)

        npt.assert_equal(results.ids, self.ids)
class TestPCoAEigenResults(object):
    def setup(self):
        with open(get_data_path('PCoA_sample_data_3'), 'U') as lines:
            dist_matrix = DistanceMatrix.from_file(lines)
        self.ordination = PCoA(dist_matrix)
        self.ids = ['PC.636', 'PC.635', 'PC.356', 'PC.481', 'PC.354', 'PC.593',
                    'PC.355', 'PC.607', 'PC.634']

    def test_values(self):
        results = self.ordination.scores()

        npt.assert_almost_equal(len(results.eigvals), len(results.site[0]))

        expected = np.loadtxt(get_data_path('exp_PCoAEigenResults_site'))
        npt.assert_almost_equal(*normalize_signs(expected, results.site))

        expected = np.array([0.51236726, 0.30071909, 0.26791207, 0.20898868,
                             0.19169895, 0.16054235,  0.15017696,  0.12245775,
                             0.0])
        npt.assert_almost_equal(results.eigvals, expected)

        expected = np.array([0.2675738328, 0.157044696, 0.1399118638,
                             0.1091402725, 0.1001110485, 0.0838401162,
                             0.0784269939, 0.0639511764, 0.0])
        npt.assert_almost_equal(results.proportion_explained, expected)

        npt.assert_equal(results.site_ids, self.ids)
Exemple #5
0
    def test_values(self):
        """Adapted from cogent's `test_principal_coordinate_analysis`:
        "I took the example in the book (see intro info), and did the
        principal coordinates analysis, plotted the data and it looked
        right"."""
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=RuntimeWarning)
            ordination = PCoA(self.dist_matrix)
        scores = ordination.scores()

        # Note the absolute value because column can have signs swapped
        npt.assert_almost_equal(np.abs(scores.species[0, 0]),
                                0.24078813304509292)

        # cogent returned the scores transposed
        npt.assert_almost_equal(np.abs(scores.species[0, 1]),
                                0.23367716219400031)
Exemple #6
0
    def test_values(self):
        """Adapted from cogent's `test_principal_coordinate_analysis`:
        "I took the example in the book (see intro info), and did the
        principal coordinates analysis, plotted the data and it looked
        right"."""
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=RuntimeWarning)
            ordination = PCoA(self.dist_matrix)
        scores = ordination.scores()

        # Note the absolute value because column can have signs swapped
        npt.assert_almost_equal(np.abs(scores.species[0, 0]),
                                0.24078813304509292)

        # cogent returned the scores transposed
        npt.assert_almost_equal(np.abs(scores.species[0, 1]),
                                0.23367716219400031)
Exemple #7
0
class TestPCoAEigenResults(object):
    def setup(self):
        with open(get_data_path('PCoA_sample_data_3'), 'U') as lines:
            dist_matrix = DistanceMatrix.from_file(lines)
        self.ordination = PCoA(dist_matrix)
        self.ids = ['PC.636', 'PC.635', 'PC.356', 'PC.481', 'PC.354', 'PC.593',
                    'PC.355', 'PC.607', 'PC.634']

    def test_values(self):
        results = self.ordination.scores()

        npt.assert_almost_equal(len(results.eigvals), len(results.species[0]))

        expected = np.array([[-0.25846546, 0.17399955, 0.03828758, -0.19447751,
                              0.0831176, 0.26243033, -0.02316364, -0.0184794,
                              0.0],
                             [-0.27100114, -0.01859513, -0.08648419,
                              0.11806425, -0.19880836, -0.02117236,
                              -0.19102403, 0.15564659, 0.0],
                             [0.2350779, 0.09625193, -0.34579273, -0.00320863,
                              -0.09637777, 0.04570254, 0.18547281, 0.0404094,
                              0.0],
                             [0.02614077, -0.01114597, 0.1476606, 0.29087661,
                              0.20394547, 0.06197124, 0.10164133, 0.105691,
                              0.0],
                             [0.28500755, -0.01925499, 0.06232634, 0.1381268,
                              -0.1047986, 0.09517207, -0.1296361, -0.22068717,
                              0.0],
                             [0.20463633, -0.13936115, 0.29151382, -0.18156679,
                              -0.15958013, -0.02464121, 0.08662524,
                              0.09962215, 0.0],
                             [0.2334824, 0.22525797, -0.01886231, -0.10772998,
                              0.177109, -0.19290584, -0.14981947, 0.0383549,
                              0.0],
                             [-0.09496319, -0.4209748, -0.15486945,
                              -0.08984275, 0.15261819, -0.03342327,
                              -0.02512248, -0.05089885, 0.0],
                             [-0.35991516, 0.1138226, 0.06622034, 0.029758,
                              -0.05722541, -0.19313351, 0.14502633,
                              -0.14965861, 0.0]])
        npt.assert_almost_equal(*normalize_signs(expected, results.species))

        expected = np.array([0.51236726, 0.30071909, 0.26791207, 0.20898868,
                             0.19169895, 0.16054235,  0.15017696,  0.12245775,
                             0.0])
        npt.assert_almost_equal(results.eigvals, expected)

        expected = np.array([0.2675738328, 0.157044696, 0.1399118638,
                             0.1091402725, 0.1001110485, 0.0838401162,
                             0.0784269939, 0.0639511764, 0.0])
        npt.assert_almost_equal(results.proportion_explained, expected)

        npt.assert_equal(results.ids, self.ids)
    def test_values(self):
        """Adapted from cogent's `test_principal_coordinate_analysis`:
        "I took the example in the book (see intro info), and did the
        principal coordinates analysis, plotted the data and it looked
        right"."""
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=RuntimeWarning)
            ordination = PCoA(self.dist_matrix)
        scores = ordination.scores()

        exp_eigvals = np.array([0.73599103, 0.26260032, 0.14926222, 0.06990457,
                                0.02956972, 0.01931184, 0., 0., 0., 0., 0., 0.,
                                0., 0.])
        exp_site = np.loadtxt(get_data_path('exp_PCoAzeros_site'))
        exp_prop_expl = np.array([0.58105792, 0.20732046, 0.1178411,
                                  0.05518899, 0.02334502, 0.01524651, 0., 0.,
                                  0., 0., 0., 0., 0., 0.])
        exp_site_ids = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                        '10', '11', '12', '13']
        # Note the absolute value because column can have signs swapped
        npt.assert_almost_equal(scores.eigvals, exp_eigvals)
        npt.assert_almost_equal(np.abs(scores.site), exp_site)
        npt.assert_almost_equal(scores.proportion_explained, exp_prop_expl)
        npt.assert_equal(scores.site_ids, exp_site_ids)
Exemple #9
0
class TestPCoAResultsExtensive(object):
    def setup(self):
        matrix = np.loadtxt(get_data_path('PCoA_sample_data_2'))
        self.ids = map(str, range(matrix.shape[0]))
        dist_matrix = DistanceMatrix(matrix, self.ids)
        self.ordination = PCoA(dist_matrix)

    def test_values(self):
        results = self.ordination.scores()

        npt.assert_almost_equal(len(results.eigvals), len(results.species[0]))

        expected = np.array([
            [-0.028597, 0.22903853, 0.07055272, 0.26163576, 0.28398669, 0.0],
            [
                0.37494056, 0.22334055, -0.20892914, 0.05057395, -0.18710366,
                0.0
            ],
            [
                -0.33517593, -0.23855979, -0.3099887, 0.11521787, -0.05021553,
                0.0
            ],
            [0.25412394, -0.4123464, 0.23343642, 0.06403168, -0.00482608, 0.0],
            [
                -0.28256844, 0.18606911, 0.28875631, -0.06455635, -0.21141632,
                0.0
            ],
            [0.01727687, 0.012458, -0.07382761, -0.42690292, 0.1695749, 0.0]
        ])
        npt.assert_almost_equal(*normalize_signs(expected, results.species))

        expected = np.array(
            [0.3984635, 0.36405689, 0.28804535, 0.27479983, 0.19165361, 0.0])
        npt.assert_almost_equal(results.eigvals, expected)

        expected = np.array([
            0.2626621381, 0.2399817314, 0.1898758748, 0.1811445992,
            0.1263356565, 0.0
        ])
        npt.assert_almost_equal(results.proportion_explained, expected)

        npt.assert_equal(results.ids, self.ids)
Exemple #10
0
 def test_F_matrix(self):
     F = PCoA._F_matrix(self.matrix2)
     expected_F = np.zeros((3, 3))
     # Note that `test_make_F_matrix` in cogent is wrong
     npt.assert_almost_equal(F, expected_F)
Exemple #11
0
 def test_E_matrix(self):
     E = PCoA._E_matrix(self.matrix)
     expected_E = np.array([[-0.5,  -2.,  -4.5],
                            [-8., -12.5, -18.]])
     npt.assert_almost_equal(E, expected_E)
Exemple #12
0
 def setup(self):
     with open(get_data_path('PCoA_sample_data_3'), 'U') as lines:
         dist_matrix = DistanceMatrix.from_file(lines)
     self.ordination = PCoA(dist_matrix)
     self.ids = ['PC.636', 'PC.635', 'PC.356', 'PC.481', 'PC.354', 'PC.593',
                 'PC.355', 'PC.607', 'PC.634']
Exemple #13
0
 def setup(self):
     matrix = np.loadtxt(get_data_path('PCoA_sample_data_2'))
     self.ids = map(str, range(matrix.shape[0]))
     dist_matrix = DistanceMatrix(matrix, self.ids)
     self.ordination = PCoA(dist_matrix)
Exemple #14
0
 def setup(self):
     matrix = np.loadtxt(get_data_path('PCoA_sample_data_2'))
     self.ids = map(str, range(matrix.shape[0]))
     dist_matrix = DistanceMatrix(matrix, self.ids)
     self.ordination = PCoA(dist_matrix)
Exemple #15
0
class TestPCoAEigenResults(object):
    def setup(self):
        with open(get_data_path('PCoA_sample_data_3'), 'U') as lines:
            dist_matrix = DistanceMatrix.from_file(lines)
        self.ordination = PCoA(dist_matrix)
        self.ids = [
            'PC.636', 'PC.635', 'PC.356', 'PC.481', 'PC.354', 'PC.593',
            'PC.355', 'PC.607', 'PC.634'
        ]

    def test_values(self):
        results = self.ordination.scores()

        npt.assert_almost_equal(len(results.eigvals), len(results.species[0]))

        expected = np.array(
            [[
                -0.25846546, 0.17399955, 0.03828758, -0.19447751, 0.0831176,
                0.26243033, -0.02316364, -0.0184794, 0.0
            ],
             [
                 -0.27100114, -0.01859513, -0.08648419, 0.11806425,
                 -0.19880836, -0.02117236, -0.19102403, 0.15564659, 0.0
             ],
             [
                 0.2350779, 0.09625193, -0.34579273, -0.00320863, -0.09637777,
                 0.04570254, 0.18547281, 0.0404094, 0.0
             ],
             [
                 0.02614077, -0.01114597, 0.1476606, 0.29087661, 0.20394547,
                 0.06197124, 0.10164133, 0.105691, 0.0
             ],
             [
                 0.28500755, -0.01925499, 0.06232634, 0.1381268, -0.1047986,
                 0.09517207, -0.1296361, -0.22068717, 0.0
             ],
             [
                 0.20463633, -0.13936115, 0.29151382, -0.18156679, -0.15958013,
                 -0.02464121, 0.08662524, 0.09962215, 0.0
             ],
             [
                 0.2334824, 0.22525797, -0.01886231, -0.10772998, 0.177109,
                 -0.19290584, -0.14981947, 0.0383549, 0.0
             ],
             [
                 -0.09496319, -0.4209748, -0.15486945, -0.08984275, 0.15261819,
                 -0.03342327, -0.02512248, -0.05089885, 0.0
             ],
             [
                 -0.35991516, 0.1138226, 0.06622034, 0.029758, -0.05722541,
                 -0.19313351, 0.14502633, -0.14965861, 0.0
             ]])
        npt.assert_almost_equal(*normalize_signs(expected, results.species))

        expected = np.array([
            0.51236726, 0.30071909, 0.26791207, 0.20898868, 0.19169895,
            0.16054235, 0.15017696, 0.12245775, 0.0
        ])
        npt.assert_almost_equal(results.eigvals, expected)

        expected = np.array([
            0.2675738328, 0.157044696, 0.1399118638, 0.1091402725,
            0.1001110485, 0.0838401162, 0.0784269939, 0.0639511764, 0.0
        ])
        npt.assert_almost_equal(results.proportion_explained, expected)

        npt.assert_equal(results.ids, self.ids)
Exemple #16
0
 def test_input(self):
     with npt.assert_raises(TypeError):
         PCoA([[1, 2], [3, 4]])
Exemple #17
0
 def test_F_matrix(self):
     F = PCoA._F_matrix(self.matrix2)
     expected_F = np.zeros((3, 3))
     # Note that `test_make_F_matrix` in cogent is wrong
     npt.assert_almost_equal(F, expected_F)
Exemple #18
0
 def test_E_matrix(self):
     E = PCoA._E_matrix(self.matrix)
     expected_E = np.array([[-0.5, -2., -4.5], [-8., -12.5, -18.]])
     npt.assert_almost_equal(E, expected_E)