def test_perturb(self): pmat = perturb(closure(self.data1), closure(np.array([1, 1, 1]))) npt.assert_allclose(pmat, np.array([[.2, .2, .6], [.4, .4, .2]])) pmat = perturb(closure(self.data1), closure(np.array([10, 10, 20]))) npt.assert_allclose(pmat, np.array([[.125, .125, .75], [1./3, 1./3, 1./3]])) pmat = perturb(closure(self.data1), closure(np.array([10, 10, 20]))) npt.assert_allclose(pmat, np.array([[.125, .125, .75], [1./3, 1./3, 1./3]])) pmat = perturb(closure(self.data2), closure([1, 2, 1])) npt.assert_allclose(pmat, np.array([1./6, 2./6, 3./6])) pmat = perturb(closure(self.data5), closure(np.array([1, 1, 1]))) npt.assert_allclose(pmat, np.array([[.2, .2, .6], [.4, .4, .2]])) with self.assertRaises(ValueError): perturb(closure(self.data5), self.bad1) # make sure that inplace modification is not occurring perturb(self.data2, [1, 2, 3]) npt.assert_allclose(self.data2, np.array([2, 2, 6]))
def noisify(table_file, metadata_file, sigma, output_file): metadata = pd.read_table(metadata_file, index_col=0) table = load_table(table_file) table = pd.DataFrame(np.array(table.matrix_data.todense()).T, index=table.ids(axis='sample'), columns=table.ids(axis='observation')) cov = np.eye(table.shape[1] - 1) m_noise = compositional_noise(cov, nsamp=table.shape[0]) table_ = table.values table_ = np.vstack( [perturb(table_[i, :], m_noise[i, :]) for i in range(table_.shape[0])]) # note that this assumes that the column is named `library_size table_ = pd.DataFrame( multinomial_sample(table_, depths=metadata['library_size'])) table_.index = table.index table_.columns = list(table.columns) metadata['observed'] = np.sum(table_.sum(axis=0) > 0) metadata['unobserved'] = np.sum(table_.sum(axis=0) == 0) metadata.to_csv(metadata_file, sep='\t') # drop zeros -- they are not informative table_ = table_.loc[:, table_.sum(axis=0) > 0] t = Table(table_.T.values, table_.columns.values, table_.index.values) with biom_open(output_file, 'w') as f: t.to_hdf5(f, generated_by='moi')
def test_perturb_inv(self): pmat = perturb_inv(closure(self.cdata1), closure([.1, .1, .1])) imat = perturb(closure(self.cdata1), closure([10, 10, 10])) npt.assert_allclose(pmat, imat) pmat = perturb_inv(closure(self.cdata1), closure([1, 1, 1])) npt.assert_allclose(pmat, closure([[.2, .2, .6], [.4, .4, .2]])) pmat = perturb_inv(closure(self.cdata5), closure([.1, .1, .1])) imat = perturb(closure(self.cdata1), closure([10, 10, 10])) npt.assert_allclose(pmat, imat) with self.assertRaises(ValueError): perturb_inv(closure(self.cdata1), self.bad1) # make sure that inplace modification is not occurring perturb_inv(self.cdata2, [1, 2, 3]) npt.assert_allclose(self.cdata2, np.array([2, 2, 6]))
def test_perturb_inv(self): pmat = perturb_inv(closure(self.data1), closure([.1, .1, .1])) imat = perturb(closure(self.data1), closure([10, 10, 10])) npt.assert_allclose(pmat, imat) pmat = perturb_inv(closure(self.data1), closure([1, 1, 1])) npt.assert_allclose(pmat, closure([[.2, .2, .6], [.4, .4, .2]])) pmat = perturb_inv(closure(self.data5), closure([.1, .1, .1])) imat = perturb(closure(self.data1), closure([10, 10, 10])) npt.assert_allclose(pmat, imat) with self.assertRaises(ValueError): perturb_inv(closure(self.data1), self.bad1) # make sure that inplace modification is not occurring perturb_inv(self.data2, [1, 2, 3]) npt.assert_allclose(self.data2, np.array([2, 2, 6]))
plot_histogram_all() plot_histogram_subset(s1, "Surviving repl 1") plot_histogram_subset(s2, "Surviving repl 2") plot_histogram_subset(s3, "Surviving repl 3") plot_histogram_subset(d1, "Dead repl 1") plot_histogram_subset(d2, "Dead repl 2") plot_histogram_subset(d3, "Dead repl 3") df_tresholded = df_merged[df_merged["PEP"] < 0.01] # THIS IS GOOD and what we work with... ############ import numpy as np import skbio.stats.composition from skbio.stats.composition import perturb otus = np.array([1. / 3, 1. / 3., 1. / 3]) antibiotic = np.array([1. / 2, 1. / 2, 1]) perturb(otus, antibiotic) from skbio.stats.composition import closure X = np.array([[2, 2, 6], [4, 4, 2]]) closure(X) from skbio.stats.composition import clr x = np.array([.1, .3, .4, .2]) clr(x) clr(X)