def test(): data_size = 1000 dims = range(1, 10, 2) stds = range(1, 10, 2) all_test_losses = dict() datasets = dim_noise_graphs(dims, stds) torch.manual_seed(0) for dim in dims: dataset_train = datasets['sc_std1_dim{}'.format(dim)]( data_size=data_size) datasets_test = { 'sc_std{}_dim{}'.format(std, dim): datasets['sc_std{}_dim{}'.format(std, dim)](data_size=data_size) for std in stds } test_losses = run_experiment(dataset_train, datasets_test) all_test_losses[dim] = test_losses print(test_losses) plot( test_losses, fname='dim{}_noise.png'.format(dim), title='{}-Dim: Varying Noise for Spurious Correlation'.format(dim)) plot3d(data=all_test_losses, axis3dinfo=Axis3dInfo(x=AxisInfo(range=dims, name='Dim'), y=AxisInfo(range=stds, name='Std'), z=AxisInfo(range=None, name='Test Loss')), plot_info=PlotInfo( fname=os.path.join(subroot, 'dim_noise2.png'), title='Varying Noise and Dim for Spurious Correlation'))
def test_downsampling_fourier(): image = get_test_3d_image() res = 1 image_fourier = scipy.fftpack.fftn(image) image_fourier_downsampled = crop_freq_3d(image_fourier, res) image_downsampled = np.absolute( scipy.fftpack.ifftn(image_fourier_downsampled)) plot3d(image_downsampled)
def test_filters(): x = y = z = 32 j = 1 alpha = 0 beta = 0 gamma = np.pi / 4 gabor = get_gabor_filter_gpu(x, y, z, j, alpha, beta, gamma) gaussian = get_gaussian_filter_gpu(x, y, z, j) plot3d(np.real(gaussian))
def test_apply_gaussian_filter(): image = get_test_3d_image() width, height, depth = image.shape j = 0 J = 0 sigma = 0.5 gaussian_filter = get_gaussian_filter_gpu(width, height, depth, J, sigma=sigma) plot3d(gaussian_filter) result = abs_after_convolve(image.astype(np.complex64), gaussian_filter.astype(np.complex64), j) plot3d(result.astype(np.float32))
def test_filter_bank(): width = depth = 32 height = 64 js = [0, 1] J = 6 L = 3 sigma = 5 # xi = np.array([3*np.pi/4, 3*np.pi/4, 3*np.pi/4]) xi = np.array([3 * np.pi / 4, np.pi / 4, np.pi / 4]) filters = filter_bank(width, height, depth, js, J, L, sigma, xi=xi) # for psi_filter in filters['psi']: # print(psi_filter["j"], psi_filter["alpha"]) filter = filters['psi'][13] print(filter["j"], filter["alpha"], filter["beta"], filter["gamma"]) plot3d(np.real(filter[0])) plot3d(np.imag(filter[0]))
def test_downsampling_fourier_gpu(): image = get_test_3d_image() x, y, z = image.shape res = 1 image_fourier = scipy.fftpack.fftn(image) image_fourier_gpu = cuda.to_device(image_fourier) result = cuda.device_array((x // 2**res, y // 2**res, z // 2**res), dtype=np.complex64) blockspergrid, threadsperblock = get_blocks_and_threads( result.shape[0], result.shape[1], result.shape[2]) image_fourier_downsampled = crop_freq_3d_gpu[blockspergrid, threadsperblock]( image_fourier_gpu, result) result = result.copy_to_host() image_downsampled = np.absolute(scipy.fftpack.ifftn(result)) plot3d(image_downsampled)
def test_rotated_gabor_filter(): x = z = 128 y = 256 j = 3 alpha = 2 * np.pi / 3 beta = np.pi / 3 gamma = 2 * np.pi / 3 sigma = 2 xi = np.array([3 * np.pi / 4, 0, 0]) gabor_filter = get_gabor_filter_gpu(x, y, z, j, alpha, beta, gamma, sigma=sigma, xi=xi) plot3d(np.real(gabor_filter)) plot3d(np.imag(gabor_filter))
def test_filters_fourier_support(): x = z = 128 y = 256 j = 0 alpha = 0 beta = 0 gamma = 0 # xi = np.array([3*np.pi/4, 0, 0]) xi = np.array([0, 0, 0]) sigma = 5 J = 3 for j in range(J + 1): gabor = get_gabor_filter_gpu(x, y, z, j, alpha, beta, gamma, xi=xi, sigma=sigma) plot3d(np.real(gabor))
def test_periodize(): image = get_test_3d_image() plot3d(image) image_fourier = scipy.fftpack.fftn(image) res = 1 result = periodize(image_fourier, res) result = np.abs(scipy.fftpack.ifftn(result)) plot3d(result) plot3d(downsample(image, res))
def test_apply_gabor_filter(): image = get_test_3d_image().astype(np.complex64) width, height, depth = image.shape j = 1 alpha = np.pi / 8 beta = np.pi / 6 gamma = np.pi / 2 sigma = 1 xi = np.array([3 * np.pi / 4, 0.1, 0.1]) gabor_filter = get_gabor_filter_gpu(width, height, depth, j, alpha, beta, gamma, sigma=sigma, xi=xi) plot3d(np.real(gabor_filter)) plot3d(np.imag(gabor_filter)) result = abs_after_convolve(image, gabor_filter, j).astype(np.float32) plot3d(result)
''' Display residuals ''' x = np.array([k_v[JOINT_ID[i]], k_tau[JOINT_ID[i]]]) A = np.zeros((m, 2)) A[:, 0] = dq[:, i] A[:, 1] = tau[:, i] f, ax = plt.subplots(1, 1, sharex=True) res = np.abs(np.dot(A, x)) - np.abs(delta_q) binwidth = (max(res) - min(res)) / 20.0 ax.hist(res, bins=np.arange(min(res), max(res) + binwidth, binwidth)) ax.set_title('Residuals with asymmetric penalty') ''' Plot 3d surface ''' x_grid = np.linspace(min(dq[:, i]), max(dq[:, i]), 15) y_grid = np.linspace(min(tau[:, i]), max(tau[:, i]), 15) X, Y = np.meshgrid(x_grid, y_grid) Z = x[0] * X + x[1] * Y ax = plot3d(dq[:, i], tau[:, i], delta_q, 'Asymmetric ID - ' + data_folder, ['dq', 'tau', 'delta q'], 'r ') ax.plot_wireframe(X, Y, Z) ''' Plot 3d surface with only low-velocity data''' ind = np.where(np.abs(dq[:, i]) < 0.01) dq2 = dq[:, i][ind] tau2 = tau[:, i][ind] delta_q2 = delta_q[ind] x_grid = np.linspace(min(dq2), max(dq2), 15) y_grid = np.linspace(min(tau2), max(tau2), 15) X, Y = np.meshgrid(x_grid, y_grid) Z = x[0] * X + x[1] * Y ax = plot3d(dq2, tau2, delta_q2, 'Low velocity data ' + data_folder, ['dq', 'tau', 'delta q'], 'r ') ax.plot_wireframe(X, Y, Z) plt.show()
y_grid = np.linspace(min(tau), max(tau), 15) X, Y = np.meshgrid(x_grid, y_grid) Xp = np.zeros(X.shape) Xn = np.zeros(X.shape) Yp = np.zeros(X.shape) Yn = np.zeros(X.shape) Xp[np.where(X > ZERO_VEL_THR)] = 1 Xn[np.where(X < -ZERO_VEL_THR)] = -1 Yp[np.where(Y > 0)] = 1 Yn[np.where(Y < 0)] = -1 Z = k_v * X + k_tau * Y fig = plt.figure() ax = fig.add_subplot(221, projection='3d') plot3d(dq, tau, delta_q, 'Asymmetric ID - ' + data_folder, ['dq', 'tau', 'delta q'], 'r ', ax=ax) ax.plot_wireframe(X, Y, Z) ax = fig.add_subplot(222, projection='3d') Z = k_v_ls * X + k_tau_ls * Y plot3d(dq, tau, delta_q, 'Symmetric ID - ' + data_folder, ['dq', 'tau', 'delta q'], 'r ', ax=ax) ax.plot_wireframe(X, Y, Z) ax = fig.add_subplot(223, projection='3d') Z = k_v_c1 * X + k_tau_c1 * Y + k_cp_c1 * Xp + k_cn_c1 * Xn + k_tp_c1 * Yp + k_tn_c1 * Yn plot3d(dq,
# for i in range(len(JOINT_ID)): # f, ax = plt.subplots(1,1,sharex=True); # ax.plot(tau[:,i], qDes[:,i]-enc[:,JOINT_ID[i]],'r. '); # tau_model = np.array([tau_min[i], tau_max[i]]); # delta_q_model = k_tau[i]*tau_model; # ax.plot(tau_model, delta_q_model,'b'); # ax.set_title('Torque VS delta_q '+str(JOINT_ID[i])); # ax.set_xlabel('Torque [Nm]'); # ax.set_ylabel('Delta_q [rad]'); ''' 3D PLOT ''' tau_min = np.min(tau, 0) tau_max = np.max(tau, 0) for i in range(len(JOINT_ID)): x_grid = np.linspace(min(dq[:, i]), max(dq[:, i]), 15) y_grid = np.linspace(min(tau[:, i]), max(tau[:, i]), 15) X, Y = np.meshgrid(x_grid, y_grid) Z = k_v[i] * X + k_tau[i] * Y fig = plt.figure() ax = fig.add_subplot(111, projection='3d') plut.plot3d(dq[:, i], tau[:, i], delta_q[:, i], 'Joit ' + str(JOINT_ID[i]), ['dq', 'tau', 'delta q'], 'r ', ax=ax) ax.plot_wireframe(X, Y, Z) # plt.figure(); plt.plot(tau[:,i], currents[:,i]); plt.title('Torque VS current '+str(JOINT_ID[i])); # plt.figure(); plt.plot(p_gains[:,i]); plt.title('Proportional gain '+str(JOINT_ID[i])); plt.show()
''' Plot 3d surface ''' x_grid = np.linspace(min(dq), max(dq), 15); y_grid = np.linspace(min(tau), max(tau), 15); X, Y = np.meshgrid(x_grid, y_grid); Xp = np.zeros(X.shape); Xn = np.zeros(X.shape); Yp = np.zeros(X.shape); Yn = np.zeros(X.shape); Xp[np.where(X>ZERO_VEL_THR)] = 1; Xn[np.where(X<-ZERO_VEL_THR)] = -1; Yp[np.where(Y>0)] = 1; Yn[np.where(Y<0)] = -1; Z = k_v*X + k_tau*Y; fig = plt.figure(); ax = fig.add_subplot(221, projection='3d'); plot3d(dq, tau, delta_q, 'Asymmetric ID - '+data_folder, ['dq','tau','delta q'],'r ', ax=ax); ax.plot_wireframe(X,Y,Z); ax = fig.add_subplot(222, projection='3d'); Z = k_v_ls*X + k_tau_ls*Y; plot3d(dq, tau, delta_q, 'Symmetric ID - '+data_folder, ['dq','tau','delta q'],'r ', ax=ax); ax.plot_wireframe(X,Y,Z); ax = fig.add_subplot(223, projection='3d'); Z = k_v_c1*X + k_tau_c1*Y + k_cp_c1*Xp + k_cn_c1*Xn + k_tp_c1*Yp + k_tn_c1*Yn; plot3d(dq, tau, delta_q, 'Symmetric ID with Coloumb friction- '+data_folder, ['dq','tau','delta q'],'r ', ax=ax); ax.plot_wireframe(X,Y,Z); if(PLOT_LOW_VEL_DATA_3D==True): x_grid = np.linspace(min(dq_zero_vel), max(dq_zero_vel), 15); y_grid = np.linspace(min(tau_zero_vel), max(tau_zero_vel), 15); X, Y = np.meshgrid(x_grid, y_grid);
# plt.figure(); plt.plot(enc[:,JOINT_ID[i]]-qDes[:,i]); plt.title('Delta_q '+str(JOINT_ID[i])); # plt.figure(); plt.plot(dq[:,i]); plt.title('Joint velocity '+str(JOINT_ID[i])); # plt.figure(); plt.plot(tau[:,i]); plt.title('Joint torque '+str(JOINT_ID[i])); # for i in range(len(JOINT_ID)): # f, ax = plt.subplots(1,1,sharex=True); # ax.plot(tau[:,i], qDes[:,i]-enc[:,JOINT_ID[i]],'r. '); # tau_model = np.array([tau_min[i], tau_max[i]]); # delta_q_model = k_tau[i]*tau_model; # ax.plot(tau_model, delta_q_model,'b'); # ax.set_title('Torque VS delta_q '+str(JOINT_ID[i])); # ax.set_xlabel('Torque [Nm]'); # ax.set_ylabel('Delta_q [rad]'); ''' 3D PLOT ''' tau_min = np.min(tau,0); tau_max = np.max(tau,0); for i in range(len(JOINT_ID)): x_grid = np.linspace(min(dq[:,i]), max(dq[:,i]), 15); y_grid = np.linspace(min(tau[:,i]), max(tau[:,i]), 15); X, Y = np.meshgrid(x_grid, y_grid); Z = k_v[i]*X + k_tau[i]*Y; fig = plt.figure(); ax = fig.add_subplot(111, projection='3d'); plut.plot3d(dq[:,i], tau[:,i], delta_q[:,i], 'Joit '+str(JOINT_ID[i]), ['dq','tau','delta q'],'r ', ax=ax); ax.plot_wireframe(X,Y,Z); # plt.figure(); plt.plot(tau[:,i], currents[:,i]); plt.title('Torque VS current '+str(JOINT_ID[i])); # plt.figure(); plt.plot(p_gains[:,i]); plt.title('Proportional gain '+str(JOINT_ID[i])); plt.show();
if __name__ == '__main__': # 1D Case: # N = 64 # xi_radians = 4*np.pi/5 # xi = N * xi_radians/(2*np.pi) # sigma_spatial = 0.6 # sigma_fourier = 1 / sigma_spatial # # for j in range(4): # result = morlet_fourier_1d(N, j, xi, sigma_fourier) # print(result[0]) # plot(result) dimensions = np.array([32, 32, 32]) xi_radians = 4*np.pi/5 xi = np.ceil(dimensions[0] * xi_radians/(2*np.pi)) xi = np.array([xi, 0, 0]) sigma_spatial = 0.2 sigma_fourier = 1 / sigma_spatial n_points_fourier_sphere = 4 rotation_matrices = rotation_matrices_fibonacci_spiral_unit_x(n_points_fourier_sphere) for j in range(3): for r in rotation_matrices: result = morlet_fourier_3d(dimensions, j, r, xi, sigma_fourier) print("Zero frequency: ", result[0, 0, 0]) maximum_pos = np.unravel_index(np.argmax(result), result.shape) print("Position of maximum: ", maximum_pos) plot3d(result)
def test_downsampling_real_space(): image = get_test_3d_image() res = 1 image = downsample(image, res) plot3d(image)