Exemplo n.º 1
0
def test_truncate_range():
    """Test range truncation."""
    # Given
    data = np.random.random(100)
    data.ravel()[np.random.choice(data.size, 10, replace=False)] = 0
    data.ravel()[np.random.choice(data.size, 5, replace=False)] = np.nan
    p_min, p_max = 2.5, 97.5
    expected = np.nanpercentile(data, [p_min, p_max])
    # When
    output = truncate_range(data,
                            percMin=p_min,
                            percMax=p_max,
                            discard_zeros=False)
    # Then
    assert all(np.nanpercentile(output, [0, 100]) == expected)
Exemplo n.º 2
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
anorm = coda.aitchison_norm(p_comp)
idx_trunc = anorm > anorm_thr
truncation_power = anorm[idx_trunc] / anorm_thr
correction = np.ones(anorm.shape)
correction[idx_trunc] = truncation_power
comp_bal = coda.power(p_comp, correction[:, None])

# go to hexcone lattice for exports
comp[p_mask > 0] = comp_bal

# lightness balance
light = truncate_range(light, percMin=1, percMax=99)
hexc = comp * light[:, None]

hexc = hexc.reshape(dims[0], dims[1], dims[2], dims[3])
for i in range(hexc.shape[3]):
    img = Nifti1Image(hexc[..., i], affine=nii1.affine)
    save(img,
         os.path.join(dirname, 'input' + str(i + 1) + '_simplex_cbal.nii.gz'))
Exemplo n.º 3
0
from compoda.utils import truncate_range
from nibabel import load, save, Nifti1Image

# Load data
nii1 = load('/path/to/file1.nii.gz')
nii2 = load('/path/to/file2.nii.gz')
nii3 = load('/path/to/file3.nii.gz')

mask = load("/path/to/mask.nii.gz").get_data()
mask[mask > 0] = 1.  # binarize

basename = nii1.get_filename().split(os.extsep, 1)[0]
dirname = os.path.dirname(nii1.get_filename())

vol1 = nii1.get_data()
vol2 = nii2.get_data()
vol3 = nii3.get_data()
dims = vol1.shape + (3,)

orig = np.zeros(dims)
orig[..., 0] = vol1 * mask
orig[..., 1] = vol2 * mask
orig[..., 2] = vol3 * mask

# simplest color balance
for i in range(orig.shape[-1]):
    img = orig[..., i]
    img = truncate_range(img, percMin=1, percMax=99, discard_zeros=True)
    out = Nifti1Image(img, affine=nii1.affine)
    save(out, os.path.join(dirname, 'input'+str(i+1)+'_simplest_cbal.nii.gz'))
Exemplo n.º 4
0
vol1 = nii1.get_data()
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])

# (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))
Exemplo n.º 5
0
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]))
ang_dif[np.isnan(ang_dif)] = 0  # fix nans assigned to bright
# (choose one) full angle range
idx_s = bary[:, 1] > bary[:, 2]
# ang_dif[idx_s] = np.arccos(ainp[idx_s]/(anorm[idx_s] * ref_norm[idx_s]))
# ang_dif[~idx_s] = 2*np.pi - np.arccos(ainp[~idx_s]/(anorm[~idx_s] * ref_norm[~idx_s]))
# truncate anorm
anorm = truncate_range(anorm, percMin=0, percMax=99)

# reassign
angdif_norm_int[..., 0] = ang_dif.reshape(dims[:-1])
angdif_norm_int[..., 1] = anorm.reshape(dims[:-1])
angdif_norm_int[..., 2] = np.sum(img, axis=-1) / 3.

# Exports
print('Exporting images...')
imsave('00_original.png', orig)
out = hsv[..., 0]
imsave('01_hue.png', out/out.max())
out = hsv[..., 1]
imsave('02_saturation.png', out/out.max())
out = hsv[..., 2]
imsave('03_value.png', out/out.max())