class TestCAResults(object): def setup(self): """Data from table 9.11 in Legendre & Legendre 1998.""" self.X = np.loadtxt(get_data_path('L&L_CA_data')) self.ordination = CA(self.X, ['Site1', 'Site2', 'Site3'], ['Species1', 'Species2', 'Species3']) def test_scaling2(self): scores = self.ordination.scores(scaling=2) # p. 460 L&L 1998 F_hat = np.array([[0.40887, -0.06955], [-0.11539, 0.29977], [-0.30997, -0.18739]]) npt.assert_almost_equal(*normalize_signs(F_hat, scores.species), decimal=5) V_hat = np.array([[-0.84896, -0.88276], [-0.22046, 1.34482], [1.66697, -0.47032]]) npt.assert_almost_equal(*normalize_signs(V_hat, scores.site), decimal=5) def test_scaling1(self): scores = self.ordination.scores(scaling=1) # p. 458 V = np.array([[1.31871, -0.34374], [-0.37215, 1.48150], [-0.99972, -0.92612]]) npt.assert_almost_equal(*normalize_signs(V, scores.species), decimal=5) F = np.array([[-0.26322, -0.17862], [-0.06835, 0.27211], [0.51685, -0.09517]]) npt.assert_almost_equal(*normalize_signs(F, scores.site), decimal=5) def test_maintain_chi_square_distance_scaling1(self): """In scaling 1, chi^2 distance among rows (sites) is equal to euclidean distance between them in transformed space.""" frequencies = self.X / self.X.sum() chi2_distances = chi_square_distance(frequencies) transformed_sites = self.ordination.scores(1).site euclidean_distances = pdist(transformed_sites, 'euclidean') npt.assert_almost_equal(chi2_distances, euclidean_distances) def test_maintain_chi_square_distance_scaling2(self): """In scaling 2, chi^2 distance among columns (species) is equal to euclidean distance between them in transformed space.""" frequencies = self.X / self.X.sum() chi2_distances = chi_square_distance(frequencies, between_rows=False) transformed_species = self.ordination.scores(2).species euclidean_distances = pdist(transformed_species, 'euclidean') npt.assert_almost_equal(chi2_distances, euclidean_distances)
def setup(self): """Data from table 9.11 in Legendre & Legendre 1998.""" self.X = np.loadtxt(get_data_path('L&L_CA_data')) self.ordination = CA(self.X, ['Site1', 'Site2', 'Site3'], ['Species1', 'Species2', 'Species3'])
def test_negative(self): X = np.array([[1, 2], [-0.1, -2]]) with npt.assert_raises(ValueError): CA(X, None, None)
import os import numpy as np from skbio.math.stats.ordination import CA, RDA, CCA path = os.path.dirname(os.path.abspath(__file__)) def get_path(fn): return os.path.join(path, os.pardir, 'math', 'stats', 'ordination', 'test', 'data', fn) X = np.loadtxt(get_path('L&L_CA_data')) ordint = CA(X) ordint.biplot(1) ordint.biplot(2) Y = np.loadtxt(get_path('example2_Y')) X = np.loadtxt(get_path('example2_X')).reshape(-1, 4, order='F') ordint = RDA(Y, X) ordint.biplot() Y = np.loadtxt(get_path('example3_Y')) X = np.loadtxt(get_path('example3_X')).reshape(-1, 4, order='F') ordint = CCA(Y, X) ordint.biplot()