def create_input_table(all_results_dirs, savepath): column_labels = [ "Case " + str(int(all_results_dirs[i].split("_")[-1]) + 1) for i in range(len(all_results_dirs)) ] row_labels = [ "$Noise standard deviation (m) $", "Correlation distance (m)", "Monte-Carlo samples (-)" ] res = np.zeros([3, len(all_results_dirs)]) for i in range(len(all_results_dirs)): with open(all_results_dirs[i] + "/input_file.json") as f: data = json.load(f) res[0, i] = data["ERROR_STANDARD_DEV"] res[1, i] = data["CORRELATION_DISTANCE"] res[2, i] = int(data["N_MONTE_CARLO"]) np_array_to_latex(res, savepath, row_headers=row_labels, column_headers=column_labels, column=True, type='f', decimals=2, ante_decimals=6)
len(col_headers_array)] = int(data["NUMBER_OF_EDGES"]) parameter_matrix[2, len(col_headers_array)] = int( data["USE_BEZIER_SHAPE"]) parameter_matrix[3, len(col_headers_array)] = 1000 * float( data["INSTRUMENT_FREQUENCY_SHAPE"]) col_headers_array += [directory.replace("_", " ")] if (len(col_headers_array) == array_length): col_headers_arrays += [col_headers_array] parameter_matrices_arrays += [parameter_matrix] np_array_to_latex.np_array_to_latex( parameter_matrix, "/Users/bbercovici/GDrive/CUBoulder/Research/conferences/GNSKi_2019/paper/Figures/inputs_" + str(col_headers_array[0]).split(" ")[1] + "_" + str(col_headers_array[-1]).split(" ")[1], row_headers=row_headers, column_headers=col_headers_array) parameter_matrix = np.zeros([ 4, min( array_length, len(dir_list) - int(str(col_headers_array[-1]).split(" ")[1]) - 1) ]) col_headers_array = [] linestyles = ['-.', '--', '-d', '-v', '-D', '+', '*'] w
import numpy as np from np_array_to_latex import np_array_to_latex ## Export an np.array to the bmatrix format a = np.array(range(0,16)).reshape([4,4]) # a == [[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11] # [12 13 14 15]] np_array_to_latex(a,'a_to_latex') # a_to_latex.tex: # \begin{bmatrix} # 0 & 1 & 2 & 3\\ # 4 & 5 & 6 & 7\\ # 8 & 9 & 10 & 11\\ # 12 & 13 & 14 & 15\\ # \end{bmatrix} ## Export a one-dimensional np.array to the bmatrix format, # choosing between column vector or row vector format b = a[0,:] # b == [[0 1 2 3] # column vector format np_array_to_latex(b,'b_to_latex_column')
def plot_all_results(input_dir, output_dir=""): prefix = input_dir.split("/")[-1] + "_" print("Plotting case " + prefix + "\n") slices_x = [input_dir + "/slice_x_" + str(i) + ".txt" for i in range(20)] slices_y = [input_dir + "/slice_y_" + str(i) + ".txt" for i in range(20)] slices_z = [input_dir + "/slice_z_" + str(i) + ".txt" for i in range(20)] slices_x = [input_dir + "/slice_x_baseline.txt"] + slices_x slices_y = [input_dir + "/slice_y_baseline.txt"] + slices_y slices_z = [input_dir + "/slice_z_baseline.txt"] + slices_z cov_cm_mc = 1e6 * np.loadtxt(input_dir + "/cov_cm_mc.txt") cov_cm_model = 1e6 * np.loadtxt(input_dir + "/cm_cov.txt") cm = 1e3 * np.loadtxt(input_dir + "/cm_spread.txt") cov_inertia_mc = np.loadtxt(input_dir + "/cov_inertia_mc.txt") cov_inertia_model = np.loadtxt(input_dir + "/inertia_cov.txt") inertia = np.loadtxt(input_dir + "/inertia_spread.txt") inertia = (inertia.T - np.mean(inertia, axis=1)).T cov_moments_mc = np.loadtxt(input_dir + "/cov_moments_mc.txt") cov_moments_model = np.loadtxt(input_dir + "/moments_cov.txt") moments = np.loadtxt(input_dir + "/moments_spread.txt") moments = (moments.T - np.mean(moments, axis=1)).T cov_dims_mc = np.loadtxt(input_dir + "/cov_dims_mc.txt") cov_dims_model = np.loadtxt(input_dir + "/dims_cov.txt") dims = np.loadtxt(input_dir + "/dims_spread.txt") dims = (dims.T - np.mean(dims, axis=1)).T cov_mrp_mc = np.loadtxt(input_dir + "/cov_mrp_mc.txt") cov_mrp_model = np.loadtxt(input_dir + "/mrp_cov.txt") mrp = np.loadtxt(input_dir + "/mrp_spread.txt") mrp = (mrp.T - np.mean(mrp, axis=1)).T fig = plt.figure() if (len(output_dir) > 0): create_output_table(cov_mrp_model, cov_mrp_mc, cov_cm_model, cov_cm_mc, cov_inertia_model, cov_inertia_mc, cov_dims_model, cov_dims_mc, cov_moments_model, cov_moments_mc, prefix, output_dir) inertia_tensor = np.zeros([3, 3]) inertia_vector = np.loadtxt(input_dir + "/I.txt") inertia_tensor[0, 0] = inertia_vector[0] inertia_tensor[1, 1] = inertia_vector[1] inertia_tensor[2, 2] = inertia_vector[2] inertia_tensor[0, 1] = inertia_vector[3] inertia_tensor[0, 2] = inertia_vector[4] inertia_tensor[1, 2] = inertia_vector[5] moments_mean = np.loadtxt(input_dir + "/moments.txt")[0:3] np_array_to_latex(moments_mean, output_dir + "/" + prefix + "moments", decimals=3, type="e") dims_mean = np.loadtxt(input_dir + "/dims.txt") np_array_to_latex(dims_mean, output_dir + "/" + prefix + "dims", decimals=3, type="e") draw_slice(0, slices_x, output_dir=output_dir, prefix=prefix) draw_slice(1, slices_y, output_dir=output_dir, prefix=prefix) draw_slice(2, slices_z, output_dir=output_dir, prefix=prefix) draw_dispersions("com", 0, cm, cov_cm_mc, cov_cm_model, [ r"$\delta \mathbf{c}_{m,x}$ ($\mathrm{m}$)", r"$\delta \mathbf{c}_{m,y}$ ($\mathrm{m}$)", r"$\delta \mathbf{c}_{m,z}$ ($\mathrm{m}$)" ], output_dir=output_dir, prefix=prefix) draw_dispersions("com", 1, cm, cov_cm_mc, cov_cm_model, [ r"$\delta \mathbf{c}_{m,x}$ ($\mathrm{m}$)", r"$\delta \mathbf{c}_{m,y}$ ($\mathrm{m}$)", r"$\delta \mathbf{c}_{m,z}$ ($\mathrm{m}$)" ], output_dir=output_dir, prefix=prefix) draw_dispersions("com", 2, cm, cov_cm_mc, cov_cm_model, [ r"$\delta \mathbf{c}_{m,x}$ ($\mathrm{m}$)", r"$\delta \mathbf{c}_{m,y}$ ($\mathrm{m}$)", r"$\delta \mathbf{c}_{m,z}$ ($\mathrm{m}$)" ], output_dir=output_dir, prefix=prefix) draw_dispersions( "dims", 0, dims, cov_dims_mc, cov_dims_model, [ r"$\delta a$ ($\mathrm{km}$)", r"$\delta b$ ($\mathrm{km}$)", r"$\delta c$ ($\mathrm{km}$)" ], output_dir=output_dir, prefix=prefix) draw_dispersions( "dims", 1, dims, cov_dims_mc, cov_dims_model, [ r"$\delta a$ ($\mathrm{km}$)", r"$\delta b$ ($\mathrm{km}$)", r"$\delta c$ ($\mathrm{km}$)" ], output_dir=output_dir, prefix=prefix) draw_dispersions( "dims", 2, dims, cov_dims_mc, cov_dims_model, [ r"$\delta a$ ($\mathrm{km}$)", r"$\delta b$ ($\mathrm{km}$)", r"$\delta c$ ($\mathrm{km}$)" ], output_dir=output_dir, prefix=prefix) draw_dispersions("moments", 0, 1000 * moments, 1e6 * cov_moments_mc, 1e6 * cov_moments_model, [ r"$1000\cdot\delta A/\rho$ ($\mathrm{km}^5$)", r"$1000\cdot\delta B/\rho$ ($\mathrm{km}^5$)", r"$1000\cdot\delta C/\rho$ ($\mathrm{km}^5$)" ], output_dir=output_dir, prefix=prefix) draw_dispersions("moments", 1, 1000 * moments, 1e6 * cov_moments_mc, 1e6 * cov_moments_model, [ r"$1000\cdot\delta A/\rho$ ($\mathrm{km}^5$)", r"$1000\cdot\delta B/\rho$ ($\mathrm{km}^5$)", r"$1000\cdot\delta C/\rho$ ($\mathrm{km}^5$)" ], output_dir=output_dir, prefix=prefix) draw_dispersions("moments", 2, 1000 * moments, 1e6 * cov_moments_mc, 1e6 * cov_moments_model, [ r"$1000\cdot\delta A/\rho$ ($\mathrm{km}^5$)", r"$1000\cdot\delta B/\rho$ ($\mathrm{km}^5$)", r"$1000\cdot\delta C/\rho$ ($\mathrm{km}^5$)" ], output_dir=output_dir, prefix=prefix) draw_dispersions( "mrp", 0, mrp, cov_mrp_mc, cov_mrp_model, [r"$\delta \sigma_1$", r"$\delta \sigma_2$", r"$\delta \sigma_3$"], output_dir=output_dir, prefix=prefix) draw_dispersions( "mrp", 1, mrp, cov_mrp_mc, cov_mrp_model, [r"$\delta \sigma_1$", r"$\delta \sigma_2$", r"$\delta \sigma_3$"], output_dir=output_dir, prefix=prefix) draw_dispersions( "mrp", 2, mrp, cov_mrp_mc, cov_mrp_model, [r"$\delta \sigma_1$", r"$\delta \sigma_2$", r"$\delta \sigma_3$"], output_dir=output_dir, prefix=prefix)
def create_output_table(cov_mrp_model, cov_mrp_mc, cov_cm_model, cov_cm_mc, cov_inertia_model, cov_inertia_mc, cov_dims_model, cov_dims_mc, cov_moments_model, cov_moments_mc, prefix, output_dir): np_array_to_latex(cov_mrp_model, output_dir + "/" + prefix + "mrp_cov", decimals=3, type="e", is_symmetric="upper") np_array_to_latex(cov_mrp_mc, output_dir + "/" + prefix + "cov_mrp_mc", decimals=3, type="e", is_symmetric="upper") np_array_to_latex((cov_mrp_model - cov_mrp_mc) / cov_mrp_mc * 100, output_dir + "/" + prefix + "cov_mrp_dev", decimals=3, is_symmetric="upper") np_array_to_latex(cov_cm_model, output_dir + "/" + prefix + "cm_cov", decimals=3, is_symmetric="upper", type="e") np_array_to_latex(cov_cm_mc, output_dir + "/" + prefix + "cov_cm_mc", decimals=3, is_symmetric="upper", type="e") np_array_to_latex((cov_cm_model - cov_cm_mc) / cov_cm_mc * 100, output_dir + "/" + prefix + "cov_cm_dev", decimals=3, is_symmetric="upper") np_array_to_latex(cov_inertia_model, output_dir + "/" + prefix + "inertia_cov", decimals=3, is_symmetric="upper", type="e") np_array_to_latex(cov_inertia_mc, output_dir + "/" + prefix + "cov_inertia_mc", decimals=3, is_symmetric="upper", type="e") np_array_to_latex( (cov_inertia_model - cov_inertia_mc) / cov_inertia_mc * 100, output_dir + "/" + prefix + "cov_inertia_dev", decimals=3, is_symmetric="upper") np_array_to_latex(cov_dims_model, output_dir + "/" + prefix + "dims_cov", decimals=3, is_symmetric="upper", type="e") np_array_to_latex(cov_dims_mc, output_dir + "/" + prefix + "cov_dims_mc", decimals=3, is_symmetric="upper", type="e") np_array_to_latex((cov_dims_model - cov_dims_mc) / cov_dims_mc * 100, output_dir + "/" + prefix + "cov_dims_dev", decimals=3, is_symmetric="upper") np_array_to_latex(cov_moments_model[0:3, 0:3], output_dir + "/" + prefix + "moments_cov", decimals=3, type="e", is_symmetric="upper") np_array_to_latex(cov_moments_mc[0:3, 0:3], output_dir + "/" + prefix + "cov_moments_mc", decimals=3, type="e", is_symmetric="upper") np_array_to_latex( (cov_moments_model[0:3, 0:3] - cov_moments_mc[0:3, 0:3]) / cov_moments_mc[0:3, 0:3] * 100, output_dir + "/" + prefix + "cov_moments_dev", decimals=3, is_symmetric="upper")
def create_input_tables(all_results_dirs, save_path, table_size=10): n_tables = int(float(len(all_results_dirs)) / table_size) size_last_table = len(all_results_dirs) - n_tables * table_size print((len(all_results_dirs), n_tables, size_last_table)) for table_index in range(n_tables): table = np.zeros([5, table_size]) for i in range(table_size): data = {} with open(all_results_dirs[table_index * table_size + i] + "/input_file.json") as f: data = json.load(f) table[0, i] = data["LOS_NOISE_SD_BASELINE"] table[1, i] = 1e9 * data["PROCESS_NOISE_SIGMA_VEL"] table[2, i] = 1e9 * data["PROCESS_NOISE_SIGMA_OMEG"] table[3, i] = int(1. / data["INSTRUMENT_FREQUENCY_NAV"]) table[4, i] = str( int(data["SHAPE_RECONSTRUCTION_OUTPUT_DIR"].split("_")[-1][0]) + 1 + (6 if "robustness" in str(data["SHAPE_RECONSTRUCTION_OUTPUT_DIR"]) else 0)) print([table_index * table_size + i, table[4, i]]) np_array_to_latex( table, save_path + "/input_table_case_" + str(table_index * table_size + 1) + "_to_" + str(table_index * table_size + table_size), row_headers=[ r"$\sigma_{\rho}$ ($\mathrm{m}$)", r"$\sigma_{\ddot{\mathbf{r}}}$ ($\mathrm{nm/s^2}$)", r"$\sigma_{\dot{\boldsymbol{\omega}}}$ ($\mathrm{nrad/s^2}$)", r"$T_{\mathrm{obs}}$ ($\mathrm{s}$)", "Input Case" ], column_headers=[ "Case " + str(i) for i in range(table_index * table_size + 1, table_index * table_size + 1 + table_size + 1) ], column=True, type='f', decimals=2, ante_decimals=6, is_symmetric="no", pretty=True, integer_row=[3, 4]) table = np.zeros([5, size_last_table]) if (size_last_table > 0): for i in range(size_last_table): data = {} with open(all_results_dirs[n_tables * table_size + i] + "/input_file.json") as f: data = json.load(f) table[0, i] = data["LOS_NOISE_SD_BASELINE"] table[1, i] = 1e9 * data["PROCESS_NOISE_SIGMA_VEL"] table[2, i] = 1e9 * data["PROCESS_NOISE_SIGMA_OMEG"] table[3, i] = int(1. / data["INSTRUMENT_FREQUENCY_NAV"]) table[4, i] = str( int(data["SHAPE_RECONSTRUCTION_OUTPUT_DIR"].split("_")[-1][0]) + 1 + (6 if "robustness" in str(data["SHAPE_RECONSTRUCTION_OUTPUT_DIR"]) else 0)) np_array_to_latex( table, save_path + "/input_table_case_" + str(n_tables * table_size + 1) + "_to_" + str((n_tables) * table_size + size_last_table), row_headers=[ r"$\sigma_{\rho}$ ($\mathrm{m}$)", r"$\sigma_{\ddot{\mathbf{r}}}$ ($\mathrm{nm/s^2}$)", r"$\sigma_{\dot{\boldsymbol{\omega}}}$ ($\mathrm{nrad/s^2}$)", r"$T_{\mathrm{obs}}$ ($\mathrm{s}$)", "Input Case" ], column_headers=[ "Case " + str(i) for i in range((n_tables) * table_size + 1, (n_tables) * table_size + 1 + size_last_table) ], column=True, type='f', decimals=2, ante_decimals=6, is_symmetric="no", pretty=True, integer_row=[3, 4])
def create_output_tables(all_results_dirs, save_path, table_size=10): n_tables = int(float(len(all_results_dirs)) / table_size) size_last_table = len(all_results_dirs) - n_tables * table_size full_output_table = np.zeros([6, len(all_results_dirs)]) row_headers = [ r"$\sigma_{\mathbf{r}}$ ($\mathrm{m}$)", r"$\sigma_{\dot{\mathbf{r}}}$ ($\mathrm{mm/s}$)", r"$\sigma_{\boldsymbol{\sigma}}$ ($\mathrm{deg}$)", r"$\sigma_{\boldsymbol{\omega}}$ ($\mathrm{\mu deg/s}$)", r"$\sigma_{\mu}$ ($\mathrm{cm^3/s^2}$)", r"$\sigma_{C_r}$" ] for table_index in range(n_tables): table = np.zeros([6, table_size]) table[:] = np.nan for i in range(table_size): input_path = all_results_dirs[table_index * table_size + i] try: X_true = np.loadtxt(input_path + "/X_true.txt") X_hat = np.loadtxt(input_path + "/X_hat.txt") except IOError: print("Skipping " + str(input_path) + ", can't find outputs") continue dX = X_hat - X_true mrp_error = np.zeros([3, dX.shape[1]]) principal_angle_error = np.zeros(dX.shape[1]) for t in range(dX.shape[1]): mrp_error[:, t] = RBK.dcm_to_mrp( RBK.mrp_to_dcm(X_true[6:9, t]).T.dot( RBK.mrp_to_dcm(X_hat[6:9, t]))) principal_angle_error[t] = 4 * np.arctan( np.linalg.norm(mrp_error[:, t])) mean_dX_position = np.mean(dX[0:3, :], axis=1) mean_dX_velocity = np.mean(dX[3:6, :], axis=1) mean_dX_principal_angle = np.mean(principal_angle_error) mean_dX_omega = np.mean(dX[9:12, :], axis=1) mean_dX_Mu = np.mean(dX[12, :]) mean_dX_Cr = np.mean(dX[13, :]) sd_position = 0 sd_velocity = 0 sd_mrp = 0 sd_omega = 0 sd_mu = 0 sd_Cr = 0 for t in range(dX.shape[1]): sd_position += 1. / (dX.shape[1] - 1) * np.linalg.norm(dX[0:3, t] - mean_dX_position)**2 sd_velocity += 1. / (dX.shape[1] - 1) * np.linalg.norm(dX[3:6, t] - mean_dX_velocity)**2 sd_mrp += 1. / (dX.shape[1] - 1) * (principal_angle_error[t] - mean_dX_principal_angle)**2 sd_omega += 1. / (dX.shape[1] - 1) * np.linalg.norm(dX[9:12, t] - mean_dX_omega)**2 sd_mu += 1. / (dX.shape[1] - 1) * (dX[12, t] - mean_dX_Mu)**2 sd_Cr += 1. / (dX.shape[1] - 1) * (dX[13, t] - mean_dX_Cr)**2 sd_position = np.sqrt(sd_position) sd_velocity = 1e3 * np.sqrt(sd_velocity) sd_mrp = 180. / np.pi * np.sqrt(sd_mrp) sd_omega = 1e6 * 180. / np.pi * np.sqrt(sd_omega) sd_mu = 1e6 * np.sqrt(sd_mu) sd_Cr = np.sqrt(sd_Cr) table[0, i] = sd_position table[1, i] = sd_velocity table[2, i] = sd_mrp table[3, i] = sd_omega table[4, i] = sd_mu table[5, i] = sd_Cr full_output_table[:, table_index * table_size:table_index * table_size + table_size] = table np_array_to_latex( table, save_path + "/output_table_case_" + str(table_index * table_size + 1) + "_to_" + str(table_index * table_size + table_size), row_headers=row_headers, column_headers=[ "Case " + str(i) for i in range(table_index * table_size + 1, table_index * table_size + 1 + table_size + 1) ], column=True, type='f', decimals=3, ante_decimals=6, is_symmetric="no", pretty=True) table = np.zeros([6, size_last_table]) table[:] = np.nan if (size_last_table > 0): for i in range(size_last_table): input_path = all_results_dirs[(n_tables) * table_size + i] try: X_true = np.loadtxt(input_path + "/X_true.txt") X_hat = np.loadtxt(input_path + "/X_hat.txt") except IOError: print("Skipping " + str(input_path) + ", can't find outputs") continue dX = X_hat - X_true mrp_error = np.zeros([3, dX.shape[1]]) principal_angle_error = np.zeros(dX.shape[1]) for t in range(dX.shape[1]): mrp_error[:, t] = RBK.dcm_to_mrp( RBK.mrp_to_dcm(X_true[6:9, t]).T.dot( RBK.mrp_to_dcm(X_hat[6:9, t]))) principal_angle_error[t] = 4 * np.arctan( np.linalg.norm(mrp_error[:, t])) mean_dX_position = np.mean(dX[0:3, :], axis=1) mean_dX_velocity = np.mean(dX[3:6, :], axis=1) mean_dX_omega = np.mean(dX[9:12, :], axis=1) mean_dX_principal_angle = np.mean(principal_angle_error) mean_dX_Mu = np.mean(dX[12, :]) mean_dX_Cr = np.mean(dX[13, :]) sd_position = 0 sd_velocity = 0 sd_mrp = 0 sd_omega = 0 sd_mu = 0 sd_Cr = 0 for t in range(dX.shape[1]): sd_position += 1. / (dX.shape[1] - 1) * np.linalg.norm(dX[0:3, t] - mean_dX_position)**2 sd_velocity += 1. / (dX.shape[1] - 1) * np.linalg.norm(dX[3:6, t] - mean_dX_velocity)**2 sd_mrp += 1. / (dX.shape[1] - 1) * (principal_angle_error[t] - mean_dX_principal_angle)**2 sd_omega += 1. / (dX.shape[1] - 1) * np.linalg.norm(dX[9:12, t] - mean_dX_omega)**2 sd_mu += 1. / (dX.shape[1] - 1) * (dX[12, t] - mean_dX_Mu)**2 sd_Cr += 1. / (dX.shape[1] - 1) * (dX[13, t] - mean_dX_Cr)**2 sd_position = np.sqrt(sd_position) sd_velocity = 1e3 * np.sqrt(sd_velocity) sd_mrp = 180. / np.pi * np.sqrt(sd_mrp) sd_omega = 1e6 * 180. / np.pi * np.sqrt(sd_omega) sd_mu = 1e6 * np.sqrt(sd_mu) sd_Cr = np.sqrt(sd_Cr) table[0, i] = sd_position table[1, i] = sd_velocity table[2, i] = sd_mrp table[3, i] = sd_omega table[4, i] = sd_mu table[5, i] = sd_Cr np_array_to_latex(table, save_path + "/output_table_case_" + str(n_tables * table_size + 1) + "_to_" + str((n_tables) * table_size + size_last_table), row_headers=row_headers, column_headers=[ "Case " + str(i) for i in range((n_tables) * table_size + 1, (n_tables) * table_size + 1 + size_last_table) ], column=True, type='f', decimals=3, ante_decimals=6, is_symmetric="no", pretty=True) full_output_table[:, n_tables * table_size:(n_tables) * table_size + size_last_table] = table np.savetxt(save_path + "/all_nav_results_table.txt", full_output_table)