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)
def setup(self): dist_matrix = DistanceMatrix.read(get_data_path('PCoA_sample_data_3')) 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 pcoa(lines): """Run PCoA on the distance matrix present on lines""" # Parse the distance matrix dist_mtx = DistanceMatrix.read(lines) # Create the PCoA object pcoa_obj = PCoA(dist_mtx) # Get the PCoA results and return them return pcoa_obj.scores()
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 js_PCoA(distributions): """Dimension reduction via Jensen-Shannon Divergence & Principal Components Parameters ---------- distributions : array-like, shape (`n_dists`, `k`) Matrix of distributions probabilities. Returns ------- pcoa : array, shape (`n_dists`, 2) """ dist_matrix = DistanceMatrix(dist.squareform(dist.pdist(distributions.values, _jensen_shannon))) pcoa = PCoA(dist_matrix).scores() return pcoa.site[:,0:2]
def test_input(self): with npt.assert_raises(TypeError): PCoA([[1, 2], [3, 4]])
def setup(self): matrix = np.loadtxt(get_data_path('PCoA_sample_data_2')) self.ids = [str(i) for i in range(matrix.shape[0])] dist_matrix = DistanceMatrix(matrix, self.ids) self.ordination = PCoA(dist_matrix)
}, 'B': { 'Méthode': 's2' }, 'C': { 'Méthode': 's3' }, 'D': { 'Méthode': 's4' }, 'E': { 'Méthode': 's5' } } df = pd.DataFrame.from_dict(metadata, orient='index') pcoa_results = PCoA(dm).scores() print(pcoa_results) fig = pcoa_results.plot( df=df, column='Méthode', title='Estimation methods projected on 3 first principal components', cmap='Set1', s=500) plt.show() """ digits = datasets.load_digits() X = np.array([[ 0. ,35.57933426 ,17.75168991 ,32.03273392 ,33.87740707],[35.57933426 , 0. ,17.86463547 , 7.161726 , 5.87323952], [17.75168991 ,17.86463547 , 0. ,14.88137054 ,16.6187191 ], [32.03273392 , 7.161726 ,14.88137054 , 0. ,3.63054395], [33.87740707 , 5.87323952 ,16.6187191 , 3.63054395 ,0. ]] ) print(type(X) ) y = np.array( [1, 2, 3, 4, 5]) print(y) print(type(y) )
# Determine if the resulting distance matrices are significantly correlated # by computing the Mantel correlation between them. Then determine if the # p-value is significant based on an alpha of 0.05: from skbio.stats.distance import mantel r, p_value, n = mantel(j_dm, bc_dm) print(r) # -0.209362157621 print(p_value < 0.05) # False # Compute PCoA for both distance matrices, and then find the Procrustes # M-squared value that results from comparing the coordinate matrices. from skbio.stats.ordination import PCoA bc_pc = PCoA(bc_dm).scores() j_pc = PCoA(j_dm).scores() from skbio.stats.spatial import procrustes print(procrustes(bc_pc.site, j_pc.site)[2]) # 0.466134984787 # All of this only gets interesting in the context of sample metadata, so # let's define some: import pandas as pd try: # not necessary for normal use pd.set_option('show_dimensions', True) except KeyError: pass sample_md = {
def pcoa(adist, cluster_members=None): from skbio import DistanceMatrix from skbio.stats.ordination import PCoA pcoa_results = PCoA(DistanceMatrix(adist)).scores()