def test_2_random_vector_fields_as_deformations(get_figures=False): dec = 1 passe_partout = 3 omega = (15, 15) sigma_init = 4 sigma_gaussian_filter = 2 svf_zeros = gen_id.id_lagrangian(omega) svf_0 = gen.generate_random(omega, parameters=(sigma_init, sigma_gaussian_filter)) l_exp = lie_exp.LieExp() sdisp_0 = l_exp.scaling_and_squaring(svf_0) sdisp_0_inv = l_exp.scaling_and_squaring(-1 * svf_0) f_o_f_inv = cp.lagrangian_dot_lagrangian(sdisp_0, sdisp_0_inv, add_right=True) f_inv_o_f = cp.lagrangian_dot_lagrangian(sdisp_0_inv, sdisp_0, add_right=True) # results of a composition of 2 lagrangian must be a lagrangian zero field assert_array_almost_equal(f_o_f_inv[passe_partout:-passe_partout, passe_partout:-passe_partout, 0, 0, :], svf_zeros[passe_partout:-passe_partout, passe_partout:-passe_partout, 0, 0, :], decimal=dec) assert_array_almost_equal(f_inv_o_f[passe_partout:-passe_partout, passe_partout:-passe_partout, 0, 0, :], svf_zeros[passe_partout:-passe_partout, passe_partout:-passe_partout, 0, 0, :], decimal=dec) if get_figures: fields_at_the_window.see_field(sdisp_0, fig_tag=61) fields_at_the_window.see_field( sdisp_0_inv, fig_tag=61, input_color='r', title_input='2 displacement fields: f blue, g red') fields_at_the_window.see_field(sdisp_0, fig_tag=62) fields_at_the_window.see_field(sdisp_0_inv, fig_tag=62, input_color='r') fields_at_the_window.see_field( f_o_f_inv, fig_tag=62, input_color='g', title_input='composition (f o f^(-1)) in green') fields_at_the_window.see_field(sdisp_0, fig_tag=63) fields_at_the_window.see_field(sdisp_0_inv, fig_tag=63, input_color='r') fields_at_the_window.see_field( f_inv_o_f, fig_tag=63, input_color='g', title_input='composition (f^(-1) o f) in green') plt.show()
scale_factor = 1. / (np.max(omega) * 10) special = False hom_attributes = [2, scale_factor, 2, special] h_a, h_g = pgl2.get_random_hom_matrices(d=hom_attributes[0], scale_factor=hom_attributes[1], sigma=hom_attributes[2], special=hom_attributes[3]) svf_0 = gen.generate_from_projective_matrix(omega[::-1], h_a, structure='algebra') flow = gen.generate_from_projective_matrix(omega[::-1], h_g, structure='group') elif params['deformation_model'] == 'gauss': svf_0 = gen.generate_random(omega[::-1], 1, (20, 2)) # svf_0 = svf_0[0:omega[0], 1:omega[1], ...] flow = l_exp.scaling_and_squaring(svf_0) else: raise IOError # save svf: with open(pfi_svf0, 'wb') as f: pickle.dump(svf_0, f) print('Shapes: ') print('shape svf {}'.format(svf_0.shape)) print('shape flow {}'.format(flow.shape)) print('shape image {}'.format(coronal_slice.shape))
from calie.fields import generate_identities as gen_id from calie.fields import generate as gen from calie.fields import compose as cp # Auxiliary vector fields function def vf(t, x): global svf_0 return list(cp.one_point_interpolation(svf_0, point=x, method='cubic')) if __name__ == '__main__': # Generate the random field with the function input: svf_0 = gen.generate_random(omega=(20, 20), parameters=(4, 2)) # Initialize the displacement field that will be computed using the integral curves. disp_0 = gen_id.id_eulerian_like(svf_0) # Start getting the figure: fig = plt.figure(num=1) ax = fig.add_subplot(111) # Plot vector field id_field = gen_id.id_eulerian_like(svf_0) input_field_copy = copy.deepcopy(svf_0) ax.quiver(id_field[..., 0, 0, 0],
dm1 = beta * linear.randomgen_linear_by_taste(1, 1, [int(c / 2) for c in omega]) m1 = scipy.linalg.expm(dm1) # generate SVF svf1 = gen.generate_from_matrix(omega, dm1, t=1, structure='algebra') flow1_ground = gen.generate_from_matrix(omega, m1, t=1, structure='group') quiver_3d(svf1, flow1_ground) plt.show(block=False) # --- Gaussian example # generate SVF svf1 = gen.generate_random(omega, 1, (1, 1)) l_exp = lie_exp.LieExp() l_exp.s_i_o = 3 flow1_ground = l_exp.gss_aei(svf1) quiver_3d(svf1, flow1_ground) plt.show(block=True) # # # fig = plt.figure() # ax = fig.gca(projection='3d') # # x, y, z = np.meshgrid(np.arange(-1, 1, 0.4),
import numpy as np from scipy.integrate import ode from calie.fields import generate as gen from calie.fields import generate_identities as gen_id from calie.fields import compose as cp def vf(t, x): global field_0 return list(cp.one_point_interpolation(field_0, point=x, method='cubic')) if __name__ == '__main__': field_0 = gen.generate_random(omega=(20, 20), parameters=(7, 2)) t0, tEnd, dt = 0, 1, 0.1 ic = [[i, j] for i in range(8, 15) for j in range(8, 15)] colors = ['b'] * len(ic) r = ode(vf).set_integrator('vode', method='bdf', max_step=dt) fig = plt.figure(num=1) ax = fig.add_subplot(111) # Plot vector field id_field = gen_id.id_eulerian_like(field_0) input_field_copy = copy.deepcopy(field_0)
print( '--------------------------------------------------------------------------' ) print( 'Generating dataset GAU! filename: gau-<s>-<algebra/group>.npy j = 1,...,N ' ) print( '--------------------------------------------------------------------------' ) for s in range(params['num_samples']): # sample s # Generate SVF svf1 = gen.generate_random( omega, 1, (params['sigma_init'], params['sigma_filter'])) flow1_ground = methods[params['selected_ground']][0]( svf1, input_num_steps=params['selected_n_steps']) pfi_svf0 = jph(pfo_output_A4_GAU, 'gau-{}-algebra.npy'.format(s + 1)) pfi_flow = jph(pfo_output_A4_GAU, 'gau-{}-group.npy'.format(s + 1)) np.save(pfi_svf0, svf1) np.save(pfi_flow, flow1_ground) print('svf saved in {}'.format(pfi_svf0)) print('flow saved in {}'.format(pfi_flow)) print('\n------------------------------------------') print('Data computed and saved in external files!')
elif params['deformation_model'] == 'linear': taste = 2 beta = 0.8 x_c, y_c, z_c = [c / 2 for c in omega] dm = beta * linear.randomgen_linear_by_taste(1, taste, (x_c, y_c)) svf_0 = gen.generate_from_matrix(omega, dm, structure='algebra') sdisp_0 = l_exp.gss_aei(svf_0) elif params['deformation_model'] == 'gauss': sampling_svf = (5, 5) svf_0 = gen.generate_random(omega, 1, (20, 4)) sdisp_0 = l_exp.scaling_and_squaring(svf_0) else: raise IOError # save svf as nifti image: im_svf0 = utils_nib.set_new_data(im_slab, svf_0) nib.save(im_svf0, pfi_svf0) print('shape of vector field: {}'.format(im_svf0.shape)) # --- get integral curves and save --- # TODO visualisation of integral curves in 2d projection for the given integration_side param. # int_curves = [] #
""" import matplotlib.pyplot as plt import numpy as np from calie.operations import lie_exp from calie.visualisations.fields import fields_at_the_window from calie.fields import generate as gen from calie.fields import compose as cp if __name__ == '__main__': # generate two vector fields omega = (20, 20) svf_v = gen.generate_random(omega, parameters=(2, 2)) svf_v_inv = np.copy(-1 * svf_v) # we wrongly perform the composition of stationary velocity fields. The outcome is not the identity. v_o_v_inv_alg = cp.lagrangian_dot_lagrangian(svf_v, svf_v_inv) v_inv_o_v_alg = cp.lagrangian_dot_lagrangian(svf_v_inv, svf_v) # we correctly perform the composition after exponentiating the SVF in the Lie group. # The outcome is the identity, as expected. l_exp = lie_exp.LieExp() disp_v = l_exp.scaling_and_squaring(svf_v) disp_v_inv = l_exp.scaling_and_squaring(svf_v_inv) v_o_v_inv_grp = cp.lagrangian_dot_lagrangian(disp_v, disp_v_inv)
def test_generate_random_wrong_timepoints(): with assert_raises(IndexError): gen.generate_random((10, 11), t=2)
def test_generate_random_test_shape_3d(): vf = gen.generate_random((10, 11, 9)) assert_array_equal(vf.shape, (10, 11, 9, 1, 3))
def test_generate_random_test_shape_2d(): vf = gen.generate_random((10, 11)) assert_array_equal(vf.shape, (10, 11, 1, 1, 2))
def test_generate_random_wrong_omega(): with assert_raises(IndexError): gen.generate_random((10, 11, 12, 12), t=2)