def test_closure(): """Test closure operator.""" # Given data = np.random.random([2, 3]) expected = np.ones(2) # When output = np.sum(coda.closure(data), axis=1) # Then assert output == pytest.approx(expected)
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)
def compute_diffusion_weights(eigvals, mode, LAMBDA=0.001, ALPHA=0.001, M=4): """Vectorized computation diffusion weights. References ---------- - Weickert, J. (1998). Anisotropic diffusion in image processing. Image Rochester NY, 256(3), 170. - Mirebeau, J.-M., Fehrenbach, J., Risser, L., & Tobji, S. (2015). Anisotropic Diffusion in ITK, 1-9. """ idx_pos_e2 = eigvals[:, 1] > 0 # positive indices of second eigen value c = (1. - ALPHA) # related to matrix condition if mode in ['EED', 'cEED', 'iEED']: # TODO: I might remove these. if mode == 'EED': # edge enhancing diffusion mu = np.ones(eigvals.shape) term1 = LAMBDA term2 = eigvals[idx_pos_e2, 1:] - eigvals[idx_pos_e2, 0, None] mu[idx_pos_e2, 1:] = 1. - c * np.exp(-(term1 / term2)**M) # weights for the non-positive eigen values mu[~idx_pos_e2, 2] = ALPHA # surely surfels elif mode == 'cEED': # FIXME: Not working at all, for now... term1 = LAMBDA term2 = eigvals mu = 1. - c * np.exp(-(term1 / term2)**M) elif mode in ['CED', 'cCED']: if mode == 'CED': # coherence enhancing diffusion (FIXME: not tested) mu = np.ones(eigvals.shape) * ALPHA term1 = LAMBDA term2 = eigvals[:, 2, None] - eigvals[:, :-1] mu[:, :-1] = ALPHA + c * np.exp(-(term1 / term2)**M) elif mode == 'cCED': # conservative coherence enhancing diffusion mu = np.ones(eigvals.shape) * ALPHA term1 = LAMBDA + eigvals[:, 0:2] term2 = eigvals[:, 2, None] - eigvals[:, 0:2] mu[:, 0:2] = ALPHA + c * np.exp(-(term1 / term2)**M) elif mode == 'CURED': # NOTE: Somewhat experimental import compoda.core as coda mu = np.ones(eigvals.shape) mu[idx_pos_e2, :] = 1. - coda.closure(eigvals[idx_pos_e2, :]) elif mode == 'STEDI': # NOTE: Somewhat more experimental import compoda.core as coda mu = np.ones(eigvals.shape) eigs = eigvals[idx_pos_e2, :] term1 = coda.closure(eigs) term2 = np.abs((np.max(term1, axis=-1) - np.min(term1, axis=-1)) - 0.5) term2 += 0.5 mu[idx_pos_e2, :] = np.abs(term2[:, None] - term1) else: mu = np.ones(eigvals.shape) print(' Invalid smoothing mesthod. Weights are all set to ones.') return mu
vol2 = nii2.get_data() vol3 = nii3.get_data() dims = vol1.shape + (3, ) comp = np.zeros(dims) comp[..., 0] = vol1 * mask comp[..., 1] = vol2 * mask comp[..., 2] = vol3 * mask comp = comp.reshape(dims[0] * dims[1] * dims[2], dims[3]) # 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] # 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.))
def compute_diffusion_weights(eigvals, mode, LAMBDA=0.001, ALPHA=0.001, M=4): """Vectorized computation diffusion weights. References ---------- - Weickert, J. (1998). Anisotropic diffusion in image processing. Image Rochester NY, 256(3), 170. - Mirebeau, J.-M., Fehrenbach, J., Risser, L., & Tobji, S. (2015). Anisotropic Diffusion in ITK, 1-9. """ idx_pos_e2 = eigvals[:, 1] > 0 # positive indices of second eigen value c = (1. - ALPHA) # related to matrix condition if mode in ['EED', 'cEED', 'iEED']: # TODO: I might remove these. if mode == 'EED': # edge enhancing diffusion mu = np.ones(eigvals.shape) term1 = LAMBDA term2 = eigvals[idx_pos_e2, 1:] - eigvals[idx_pos_e2, 0, None] mu[idx_pos_e2, 1:] = 1. - c * np.exp(-(term1/term2)**M) # weights for the non-positive eigen values mu[~idx_pos_e2, 2] = ALPHA # surely surfels elif mode == 'cEED': # FIXME: Not working at all, for now... term1 = LAMBDA term2 = eigvals mu = 1. - c * np.exp(-(term1/term2)**M) elif mode in ['CED', 'cCED']: if mode == 'CED': # coherence enhancing diffusion (FIXME: not tested) mu = np.ones(eigvals.shape) * ALPHA term1 = LAMBDA term2 = eigvals[:, 2, None] - eigvals[:, :-1] mu[:, :-1] = ALPHA + c * np.exp(-(term1/term2)**M) elif mode == 'cCED': # conservative coherence enhancing diffusion mu = np.ones(eigvals.shape) * ALPHA term1 = LAMBDA + eigvals[:, 0:2] term2 = eigvals[:, 2, None] - eigvals[:, 0:2] mu[:, 0:2] = ALPHA + c * np.exp(-(term1/term2)**M) elif mode == 'CURED': # NOTE: Somewhat experimental import compoda.core as coda mu = np.ones(eigvals.shape) mu[idx_pos_e2, :] = 1. - coda.closure(eigvals[idx_pos_e2, :]) elif mode == 'STEDI': # NOTE: Somewhat more experimental import compoda.core as coda mu = np.ones(eigvals.shape) eigs = eigvals[idx_pos_e2, :] term1 = coda.closure(eigs) term2 = np.abs((np.max(term1, axis=-1) - np.min(term1, axis=-1)) - 0.5) term2 += 0.5 mu[idx_pos_e2, :] = np.abs(term2[:, None] - term1) else: mu = np.ones(eigvals.shape) print(' Invalid smoothing mesthod. Weights are all set to ones.') return mu
comp[..., 0] = vol1[idx_mask] comp[..., 1] = vol2[idx_mask] comp[..., 2] = vol3[idx_mask] # (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)
from mpl_toolkits.mplot3d import Axes3D 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)
orig = np.zeros(dims) orig[..., 0] = vol1 * mask orig[..., 1] = vol2 * mask orig[..., 2] = vol3 * mask orig = orig.reshape(dims[0] * dims[1] * dims[2], dims[3]) # Impute orig[orig <= 0] = 1 comp = np.copy(orig) # Lightness light = (np.max(comp, axis=1) + np.min(comp, axis=1)) / 2. # Closure comp = coda.closure(comp) # Do not consider masked values p_mask = mask.reshape(dims[0] * dims[1] * dims[2]) p_comp = comp[p_mask > 0] # Centering center = coda.sample_center(p_comp) temp = np.ones(p_comp.shape) * center p_comp = coda.perturb(p_comp, temp**-1.) # Standardize totvar = coda.sample_total_variance(p_comp, center) p_comp = coda.power(p_comp, np.power(totvar, -1. / 2.)) # Use Aitchison norm and powerinf for truncation of extreme compositions anorm_thr = 3
comp[..., 2] = vol3 * mask comp = comp.reshape(dims[0] * dims[1] * dims[2], dims[3]) # (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 = 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)
comp[..., 0] = vol1[idx_mask] comp[..., 1] = vol2[idx_mask] comp[..., 2] = vol3[idx_mask] # (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) # Aitchison inner product ref = np.ones(comp.shape) ref[:, 1] = ref[:, 1] * 10 ref = coda.closure(ref) ip = coda.aitchison_inner_product(comp, ref) cos_theta = ip / (coda.aitchison_norm(comp) * coda.aitchison_norm(ref)) rad = np.arccos(cos_theta) deg = rad * (360 / (2*np.pi)) # # Determine sector # idx_sector2 = comp[:, 1] > comp[:, 2] # deg[idx_sector2] = 360. - deg[idx_sector2]
img = np.copy(orig) * 1.0 dims = img.shape # impute zeros idx1 = (img[..., 0] <= 0) + (img[..., 1] <= 0) + (img[..., 2] <= 0) gauss = gaussian(img, sigma=0.5, multichannel=True) img[idx1, :] = gauss[idx1, :] # rgb to hsv for control hsv = color.rgb2hsv(img) hsv[..., 0] = -np.abs(hsv[..., 0] - 0.5) + 0.5 # coda stuff angdif_norm_int = np.zeros(img.shape) temp = img.reshape(dims[0] * dims[1], dims[2]) * 1.0 bary = coda.closure(temp, 100) anorm = coda.aitchison_norm(bary) # prepare reference vector for angular difference ref = np.ones(bary.shape) ref[:, 0] = ref[:, 0] * 0.8 ref[:, 1] = ref[:, 1] * 0.1 ref[:, 2] = ref[:, 2] * 0.1 # compute aitchion angular difference ainp = coda.aitchison_inner_product(bary, ref) ref_norm = coda.aitchison_norm(ref) ang_dif = np.zeros(anorm.shape) # deal with zero norms idx = anorm != 0 # (choose one) wrapped angle range ang_dif[idx] = np.arccos(ainp[idx]/(anorm[idx] * ref_norm[idx]))
data[600:610, 1] = temp[5::10] data[610:620, 1] = 100 data[610:620, 0] = temp[5::10] # constant primary color 2, increasing primary color 3 mixture and vice versa data[620:630, 1] = 100 data[620:630, 2] = temp[5::10] data[630:640, 2] = 100 data[630:640, 1] = temp[5::10] # constant primary color 3, increasing primary color 1 mixture and vice versa data[640:650, 2] = 100 data[640:650, 0] = temp[5::10] data[650:660, 0] = 100 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)
vol2 = nii2.get_data() vol3 = nii3.get_data() dims = vol1.shape + (3, ) rgb = np.zeros(dims) rgb[..., 0] = vol1 rgb[..., 1] = vol2 rgb[..., 2] = vol3 rgb = rgb.reshape(dims[0] * dims[1] * dims[2], dims[3]) # impute rgb[rgb == 0] = 1. # barycentric decomposition intensity = np.sum(rgb, axis=-1) / rgb.shape[-1] bary = coda.closure(rgb) anorm = coda.aitchison_norm(bary) # prepare reference vector for anglular difference # TODO: export for all three main references ref = np.ones(bary.shape) ref[:, 0] = ref[:, 0] * 0.05 ref[:, 1] = ref[:, 1] * 0.9 ref[:, 2] = ref[:, 2] * 0.05 # compute aitchion angular difference ainp = coda.aitchison_inner_product(bary, ref) # Export inner product img = ainp.reshape(dims[:-1]) out = Nifti1Image(img, affine=nii1.affine)