From 88e3cb74e031f4c0446f246d5657651a6b36bfa3 Mon Sep 17 00:00:00 2001 From: x-io Technologies Date: Mon, 2 Sep 2024 11:02:52 +0100 Subject: [PATCH] Use conventional package aliases --- Python/advanced_example.py | 64 ++++++++++++++++++++++---------------- Python/simple_example.py | 12 +++---- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/Python/advanced_example.py b/Python/advanced_example.py index 36b9f4b..aebc66a 100644 --- a/Python/advanced_example.py +++ b/Python/advanced_example.py @@ -1,10 +1,10 @@ import imufusion -import matplotlib.pyplot as pyplot -import numpy +import matplotlib.pyplot as plt +import numpy as np import sys # Import sensor data -data = numpy.genfromtxt("sensor_data.csv", delimiter=",", skip_header=1) +data = np.genfromtxt("sensor_data.csv", delimiter=",", skip_header=1) sample_rate = 100 # 100 Hz @@ -17,19 +17,21 @@ offset = imufusion.Offset(sample_rate) ahrs = imufusion.Ahrs() -ahrs.settings = imufusion.Settings(imufusion.CONVENTION_NWU, # convention - 0.5, # gain - 2000, # gyroscope range - 10, # acceleration rejection - 10, # magnetic rejection - 5 * sample_rate) # recovery trigger period = 5 seconds +ahrs.settings = imufusion.Settings( + imufusion.CONVENTION_NWU, # convention + 0.5, # gain + 2000, # gyroscope range + 10, # acceleration rejection + 10, # magnetic rejection + 5 * sample_rate, # recovery trigger period = 5 seconds +) # Process sensor data -delta_time = numpy.diff(timestamp, prepend=timestamp[0]) +delta_time = np.diff(timestamp, prepend=timestamp[0]) -euler = numpy.empty((len(timestamp), 3)) -internal_states = numpy.empty((len(timestamp), 6)) -flags = numpy.empty((len(timestamp), 4)) +euler = np.empty((len(timestamp), 3)) +internal_states = np.empty((len(timestamp), 6)) +flags = np.empty((len(timestamp), 4)) for index in range(len(timestamp)): gyroscope[index] = offset.update(gyroscope[index]) @@ -39,30 +41,38 @@ euler[index] = ahrs.quaternion.to_euler() ahrs_internal_states = ahrs.internal_states - internal_states[index] = numpy.array([ahrs_internal_states.acceleration_error, - ahrs_internal_states.accelerometer_ignored, - ahrs_internal_states.acceleration_recovery_trigger, - ahrs_internal_states.magnetic_error, - ahrs_internal_states.magnetometer_ignored, - ahrs_internal_states.magnetic_recovery_trigger]) + internal_states[index] = np.array( + [ + ahrs_internal_states.acceleration_error, + ahrs_internal_states.accelerometer_ignored, + ahrs_internal_states.acceleration_recovery_trigger, + ahrs_internal_states.magnetic_error, + ahrs_internal_states.magnetometer_ignored, + ahrs_internal_states.magnetic_recovery_trigger, + ] + ) ahrs_flags = ahrs.flags - flags[index] = numpy.array([ahrs_flags.initialising, - ahrs_flags.angular_rate_recovery, - ahrs_flags.acceleration_recovery, - ahrs_flags.magnetic_recovery]) + flags[index] = np.array( + [ + ahrs_flags.initialising, + ahrs_flags.angular_rate_recovery, + ahrs_flags.acceleration_recovery, + ahrs_flags.magnetic_recovery, + ] + ) def plot_bool(axis, x, y, label): axis.plot(x, y, "tab:cyan", label=label) - pyplot.sca(axis) - pyplot.yticks([0, 1], ["False", "True"]) + plt.sca(axis) + plt.yticks([0, 1], ["False", "True"]) axis.grid() axis.legend() # Plot Euler angles -figure, axes = pyplot.subplots(nrows=11, sharex=True, gridspec_kw={"height_ratios": [6, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1]}) +figure, axes = plt.subplots(nrows=11, sharex=True, gridspec_kw={"height_ratios": [6, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1]}) figure.suptitle("Euler angles, internal states, and flags") @@ -107,4 +117,4 @@ def plot_bool(axis, x, y, label): plot_bool(axes[10], timestamp, flags[:, 3], "Magnetic recovery") -pyplot.show(block="no_block" not in sys.argv) # don't block when script run by CI +plt.show(block="no_block" not in sys.argv) # don't block when script run by CI diff --git a/Python/simple_example.py b/Python/simple_example.py index 233fef1..6d9f2e5 100644 --- a/Python/simple_example.py +++ b/Python/simple_example.py @@ -1,17 +1,17 @@ import imufusion -import matplotlib.pyplot as pyplot -import numpy +import matplotlib.pyplot as plt +import numpy as np import sys # Import sensor data -data = numpy.genfromtxt("sensor_data.csv", delimiter=",", skip_header=1) +data = np.genfromtxt("sensor_data.csv", delimiter=",", skip_header=1) timestamp = data[:, 0] gyroscope = data[:, 1:4] accelerometer = data[:, 4:7] # Plot sensor data -_, axes = pyplot.subplots(nrows=3, sharex=True) +_, axes = plt.subplots(nrows=3, sharex=True) axes[0].plot(timestamp, gyroscope[:, 0], "tab:red", label="X") axes[0].plot(timestamp, gyroscope[:, 1], "tab:green", label="Y") @@ -31,7 +31,7 @@ # Process sensor data ahrs = imufusion.Ahrs() -euler = numpy.empty((len(timestamp), 3)) +euler = np.empty((len(timestamp), 3)) for index in range(len(timestamp)): ahrs.update_no_magnetometer(gyroscope[index], accelerometer[index], 1 / 100) # 100 Hz sample rate @@ -47,4 +47,4 @@ axes[2].grid() axes[2].legend() -pyplot.show(block="no_block" not in sys.argv) # don't block when script run by CI +plt.show(block="no_block" not in sys.argv) # don't block when script run by CI