From 3fe39b0cf9478157a9d3a1b07d98c8d51a92d8b8 Mon Sep 17 00:00:00 2001 From: Markus Koller Date: Sat, 8 Oct 2022 18:38:24 +0200 Subject: [PATCH] Fixed minor warnings and style issues --- src/calibration.py | 27 ++++++++++++++------------- src/user_interface.py | 23 ++++++++++++++--------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/calibration.py b/src/calibration.py index 0f87c08..f7057b7 100644 --- a/src/calibration.py +++ b/src/calibration.py @@ -377,9 +377,8 @@ class MagnetometerCalibrationSimple(Thread): b_x = sample['b_x'] b_y = sample['b_y'] b_z = sample['b_z'] - res.append(m - s * ( - b_e * sin(alpha_e) + b_x * cos(alpha) * cos(beta) + b_y * cos(alpha) * sin(beta) + b_z * sin( - alpha))) + res.append(m - s * (b_e * sin(alpha_e) + b_x * cos(alpha) * cos(beta) + + b_y * cos(alpha) * sin(beta) + b_z * sin(alpha))) return res @staticmethod @@ -410,7 +409,7 @@ class MagnetometerCalibrationSimple(Thread): class MagnetometerCalibrationComplete(Thread): TEST_VECTOR_MAGNITUDE = 100e-6 # In Tesla. Chosen so it can be achieved with a 3A PSU. - def __init__(self, view_queue, calibration_points, calibration_interval, mgm_to_helmholtz_cos_trans,right_column): + def __init__(self, view_queue, calibration_points, calibration_interval, mgm_to_helmholtz_cos_trans, right_column): Thread.__init__(self) self.view_queue = view_queue self.calibration_points = calibration_points @@ -501,7 +500,7 @@ class MagnetometerCalibrationComplete(Thread): # Use collected data to build and solve system of equations # sensor_parameters = self.solve_system(raw_data) # FLAG: untested! sensor_parameters, mag_x_set, mag_y_set, mag_z_set, mag_x_m, mag_y_m, mag_z_m, cal_x, cal_y, cal_z, mag_amp_avg_set = MagnetometerCalibrationComplete.solve_system( - self.view_queue, raw_data, self.mgm_to_helmholtz_cos_trans) + self.view_queue, raw_data, self.matrix_trans_mgm_to_hh) # Pass results to UI self.put_message('calibration_data', {'results': sensor_parameters, 'raw_data': raw_data}) @@ -612,7 +611,7 @@ class MagnetometerCalibrationComplete(Thread): 'q_mat': q_mat.tolist(), 'n': n.tolist(), 'mag_amp_avg_set': mag_amp_avg_set.tolist(), - 'total_error': total_error.tolist()}] + 'total_error': total_error}] return sensor_parameters, mag_x_set, mag_y_set, mag_z_set, mag_x_m, mag_y_m, mag_z_m, cal_x, cal_y, cal_z, mag_amp_avg_set except Warning as warning_message: ui_print('A_inv could not be calculated! A warning occurred.') @@ -781,7 +780,7 @@ class MagnetometerCalibrationComplete(Thread): ax1.yaxis.set_tick_params(width=ax_width) ax1.zaxis.set_tick_params(width=ax_width) ax1.grid(which='major', linewidth=ax_width) - ax1.grid(which='minor', linewidth=ax_width/2) + ax1.grid(which='minor', linewidth=ax_width / 2) # linking lines between measured and calibrated points for i, j, k, l, m, n in zip(mag_x_set * u_tesla, mag_y_set * u_tesla, mag_z_set * u_tesla, mag_x_m * u_tesla, mag_y_m * u_tesla, @@ -799,7 +798,8 @@ class MagnetometerCalibrationComplete(Thread): x = np.outer(np.cos(u), np.sin(v)) * mag_amp_avg_set y = np.outer(np.sin(u), np.sin(v)) * mag_amp_avg_set z = np.outer(np.ones(np.size(u)), np.cos(v)) * mag_amp_avg_set - ax1.plot_wireframe(x * u_tesla, y * u_tesla, z * u_tesla, rstride=10, cstride=10, alpha=0.7, color='y', linewidth=0.1) + ax1.plot_wireframe(x * u_tesla, y * u_tesla, z * u_tesla, rstride=10, cstride=10, alpha=0.7, color='y', + linewidth=0.1) ax1.plot_surface(x * u_tesla, y * u_tesla, z * u_tesla, alpha=0.3, color='y', linewidth=0.1) # ax1.legend(loc='upper right', fontsize=plot_fontsize) @@ -815,7 +815,7 @@ class MagnetometerCalibrationComplete(Thread): ax2.yaxis.set_tick_params(width=ax_width) ax2.zaxis.set_tick_params(width=ax_width) ax2.grid(which='major', linewidth=ax_width) - ax2.grid(which='minor', linewidth=ax_width/2) + ax2.grid(which='minor', linewidth=ax_width / 2) # linking lines between measured and calibrated points for i, j, k, l, m, n in zip(mag_x_set * u_tesla, mag_y_set * u_tesla, mag_z_set * u_tesla, cal_x * u_tesla, cal_y * u_tesla, @@ -833,7 +833,8 @@ class MagnetometerCalibrationComplete(Thread): x = np.outer(np.cos(u), np.sin(v)) * mag_amp_avg_set y = np.outer(np.sin(u), np.sin(v)) * mag_amp_avg_set z = np.outer(np.ones(np.size(u)), np.cos(v)) * mag_amp_avg_set - ax2.plot_wireframe(x * u_tesla, y * u_tesla, z * u_tesla, rstride=10, cstride=10, alpha=0.7, color='y', linewidth=0.1) + ax2.plot_wireframe(x * u_tesla, y * u_tesla, z * u_tesla, rstride=10, cstride=10, alpha=0.7, color='y', + linewidth=0.1) ax2.plot_surface(x * u_tesla, y * u_tesla, z * u_tesla, alpha=0.3, color='y', linewidth=0.1) # ax2.legend(loc='upper right', fontsize=plot_fontsize) ax3 = fig1.add_subplot(222) @@ -855,7 +856,7 @@ class MagnetometerCalibrationComplete(Thread): # x panel ax5 = fig2.add_subplot(311) ax5.grid(which='major', linewidth=ax_width) - ax5.grid(which='minor', linewidth=ax_width/2) + ax5.grid(which='minor', linewidth=ax_width / 2) ax5.set_ylabel(r'$B_x [\mu T]$', fontsize=plot_fontsize) ax5.plot(np.linspace(1, meas_no, meas_no), mag_x_set * u_tesla, linewidth=0.2, color='k', linestyle='solid', label=r'$B_{x,set}$') @@ -873,7 +874,7 @@ class MagnetometerCalibrationComplete(Thread): # y panel ax6 = fig2.add_subplot(312) ax6.grid(which='major', linewidth=ax_width) - ax6.grid(which='minor', linewidth=ax_width/2) + ax6.grid(which='minor', linewidth=ax_width / 2) ax6.set_ylabel(r'$B_y [\mu T]$', fontsize=plot_fontsize) ax6.plot(np.linspace(1, meas_no, meas_no), mag_y_set * u_tesla, linewidth=0.2, color='k', linestyle='solid', label=r'$B_{y,set}$') @@ -891,7 +892,7 @@ class MagnetometerCalibrationComplete(Thread): # z panel ax7 = fig2.add_subplot(313) ax7.grid(which='major', linewidth=ax_width) - ax7.grid(which='minor', linewidth=ax_width/2) + ax7.grid(which='minor', linewidth=ax_width / 2) ax7.set_xlabel("Measurement number", fontsize=plot_fontsize) ax7.set_ylabel(r'$B_z [\mu T]$', fontsize=plot_fontsize) ax7.plot(np.linspace(1, meas_no, meas_no), mag_z_set * u_tesla, linewidth=0.2, color='k', linestyle='solid', diff --git a/src/user_interface.py b/src/user_interface.py index f177b75..0bcf95c 100644 --- a/src/user_interface.py +++ b/src/user_interface.py @@ -538,9 +538,12 @@ class ExecuteCSVMode(Frame): # Clear previous plots first try: self.plot_canvas.get_tk_widget().destroy() - except: + except Exception as e: + ui_print("Something went wrong while plotting csv data!", e) + messagebox.showerror("Error!", "Something went wrong while plotting csv data: \n%s" % e) pass - # Show new plot + + # Show new plot self.plot_canvas = FigureCanvasTkAgg(figure, self.plot_frame) # create canvas to draw figure on self.plot_canvas.draw() # equivalent to matplotlib.show() self.plot_canvas.get_tk_widget().grid(row=0, column=0, sticky="nesw") # place canvas in the UI @@ -1377,9 +1380,9 @@ class CalibrateMagnetometerSimple(Frame): for i in range(3): for j in range(3): self.mgm_to_helmholtz_cos_trans[i][j].set(matrix[i][j]) - except: + except Exception as e: # Couldn't compute matrix -> use unity matrix - ui_print("Could not normalize matrix, reverted to unity matrix!") + ui_print("Could not normalize matrix, reverted to unity matrix! \n%s" % e) self.mgm_to_helmholtz_cos_trans = [[DoubleVar(value=1), DoubleVar(value=0), DoubleVar(value=0)], [DoubleVar(value=0), DoubleVar(value=1), DoubleVar(value=0)], [DoubleVar(value=0), DoubleVar(value=0), DoubleVar(value=1)]] @@ -1808,7 +1811,7 @@ class CalibrateMagnetometerComplete(Frame): # Pass results to UI self.put_message('calibration_data', {'results': sensor_parameters, 'raw_data': raw_data}) except TypeError: - ui_print('File path is incorrect, no data could be loaded!') + ui_print('Solution could not be computed or file not found!') def export_csv_cos_trans_matrix(self): cos_trans_matrix = [ @@ -1831,14 +1834,16 @@ class CalibrateMagnetometerComplete(Frame): except FileNotFoundError: ui_print('Did not save matrix since incorrect file path specified!') - def export_figure1_to_file(self, figure): + @staticmethod + def export_figure1_to_file(figure): filename = 'MGM_calibration_ellipsoid_fit_3D' filename = filedialog.asksaveasfilename(initialfile=filename, title="Select location to save figure.", filetypes=[("PNG", '*.png'), ("PDF", '*.pdf')], defaultextension=".pdf") figure.savefig(filename, dpi=2000, transparent=True) - def export_figure2_to_file(self, figure): + @staticmethod + def export_figure2_to_file(figure): filename = 'MGM_calibration_ellipsoid_fit_2D' filename = filedialog.asksaveasfilename(initialfile=filename, title="Select location to save figure.", filetypes=[("PNG", '*.png'), ("PDF", '*.pdf')], @@ -1891,9 +1896,9 @@ class CalibrateMagnetometerComplete(Frame): for i in range(3): for j in range(3): self.mgm_to_helmholtz_cos_trans[i][j].set(matrix[i][j]) - except: + except Exception as e: # Couldn't compute matrix -> use unity matrix - ui_print("Could not normalize matrix, reverted to unity matrix!") + ui_print("Could not normalize matrix, reverted to unity matrix: \n%s" % e) self.mgm_to_helmholtz_cos_trans = [[DoubleVar(value=1), DoubleVar(value=0), DoubleVar(value=0)], [DoubleVar(value=0), DoubleVar(value=1), DoubleVar(value=0)], [DoubleVar(value=0), DoubleVar(value=0), DoubleVar(value=1)]]