def test_ilr(): """Test ilr transformations.""" # Given data = np.random.random([2, 3]) # When data = coda.closure(data) output_1 = coda.ilr_transformation(data) output_2 = coda.inverse_ilr_transformation(output_1) # Then assert output_2 == pytest.approx(data)
# Plot related operations p_mask = mask.reshape(dims[0] * dims[1] * dims[2]) p_comp = comp[p_mask > 0] # Centering center = tet.sample_center(p_comp) # center = np.array([[ 0.06521165, 0.66942364, 0.26536471]]) # 0 noise center print "Sample center: " + str(center) c_temp = np.ones(p_comp.shape) * center p_comp = tet.perturb(p_comp, c_temp**-1) # Standardize totvar = tet.sample_total_variance(p_comp, center) p_comp = tet.power(p_comp, np.power(totvar, -1. / 2.)) # Isometric logratio transformation for plotting ilr = tet.ilr_transformation(p_comp) # Plot 2D histogram of ilr transformed data plt.hist2d( ilr[:, 0], ilr[:, 1], bins=2000, norm=LogNorm(), # vmax=100, cmap='inferno') plt.xlabel('$v_1$') plt.ylabel('$v_2$') plt.axes().set_aspect('equal') plt.axes().set_xlim([-3, 3]) plt.axes().set_ylim([-3, 3]) plt.colorbar()
# (optional) truncate and rescale # for i in range(comp.shape[1]): # temp = comp[:, i] # temp = truncate_range(temp) # temp = scale_range(temp, scale_factor=1000) # comp[:, i] = temp # Impute comp[comp == 0] = 1. # Closure comp = coda.closure(comp) # Isometric logratio transformation before any centering ilr_orig = coda.ilr_transformation(np.copy(comp)) # Centering center = coda.sample_center(comp) print("Sample center: " + str(center)) c_temp = np.ones(comp.shape) * center p_comp = coda.perturb(comp, c_temp**-1) # Standardize totvar = coda.sample_total_variance(comp, center) comp = coda.power(comp, np.power(totvar, -1. / 2.)) # Isometric logratio transformation for plotting ilr = coda.ilr_transformation(comp) # Plots fig = plt.figure()
import matplotlib.pyplot as plt import seaborn from compoda.core import closure, ilr_transformation # create euclidean 3D lattice parcels = 10 data = np.zeros([parcels**3, 3], dtype=float) coords = np.linspace(1000, 2000, parcels) x, y, z = np.meshgrid(coords, coords, coords) data[:, 0] = x.flatten() data[:, 1] = y.flatten() data[:, 2] = z.flatten() # isometric logratio transformation bary = closure(np.copy(data)) ilr = ilr_transformation(bary) # calculate intensity to demonstrate hexcone interpretation inten = (np.max(data, axis=1) + np.min(data, axis=1)) / 2. # intensity hexc = np.zeros(data.shape) hexc[:, 0:2], hexc[:, 2] = ilr, inten # Plots fig = plt.figure() ax_1 = plt.subplot(131, projection='3d') ax_1.set_title('Euclidean space') ax_1.set_aspect('equal') ax_1.scatter(data[:, 0], data[:, 1], data[:, 2], color='k', s=3) ax_2 = plt.subplot(132) ax_2.scatter(ilr[:, 0], ilr[:, 1], color='k', s=3)
temp = truncate_range(temp) temp = scale_range(temp, scale_factor=1000) comp[:, i] = temp # Impute comp[comp == 0] = 1. # Closure comp = tet.closure(comp) # Plot related operations p_mask = mask.reshape(dims[0] * dims[1] * dims[2]) p_comp = comp[p_mask > 0] # Isometric logratio transformation before any centering ilr_orig = tet.ilr_transformation(np.copy(p_comp)) # Centering center = tet.sample_center(p_comp) print("Sample center: " + str(center)) c_temp = np.ones(p_comp.shape) * center p_comp = tet.perturb(p_comp, c_temp**-1) # Standardize totvar = tet.sample_total_variance(p_comp, center) p_comp = tet.power(p_comp, np.power(totvar, -1. / 2.)) # Isometric logratio transformation for plotting ilr = tet.ilr_transformation(p_comp) # Plots fig = plt.figure()
data[650:660, 2] = temp[5::10] # closure data = tet.closure(data) # R^D # alr alr = tet.alr_transformation(data) # R^D to S^(D-1) ialr = tet.inverse_alr_transformation(alr) # S^(D-1) to R^(D-1) np.testing.assert_almost_equal(ialr, data, decimal=7, verbose=True) # clr clr = tet.clr_transformation(data) # R^D to S^D iclr = tet.inverse_clr_transformation(clr) # S^D to R^D # ilr ilr = tet.ilr_transformation(data) # R^D to S^(D-1) iilr = tet.inverse_ilr_transformation(ilr) # S^(D-1) to R^(D-1) np.testing.assert_almost_equal(iilr, data, decimal=7, verbose=True) # plots fig = plt.figure() ax_1 = plt.subplot(131) ax_2 = plt.subplot(132, projection='3d') ax_3 = plt.subplot(133) ax_1.set_title('Additive log-ratio transformation\n(alr)') ax_2.set_title('Centered log-ratio transformation\ntransformation(clr)') ax_3.set_title('Isometric log-ratio transformation\ntransformation(ilr)') ax_1.set_aspect('equal', 'datalim') ax_2.set_aspect('equal') ax_3.set_aspect('equal', 'datalim') ax_1.scatter(alr[:, 0], alr[:, 1], color='red', s=2)