Skip to content

Commit

Permalink
MNT: change measured_values to measured_data
Browse files Browse the repository at this point in the history
  • Loading branch information
MateusStano committed Apr 2, 2024
1 parent 28ebc46 commit 378bb54
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
10 changes: 5 additions & 5 deletions docs/notebooks/sensors_testing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,9 @@
}
],
"source": [
"# get first column of every row as time from [(time,(ax,ay,az)),...] = a.measured_values\n",
"time1, ax, ay, az = zip(*accel_noisy_nosecone.measured_values)\n",
"time2, bx, by, bz = zip(*accel_clean_cdm.measured_values)\n",
"# get first column of every row as time from [(time,(ax,ay,az)),...] = a.measured_data\n",
"time1, ax, ay, az = zip(*accel_noisy_nosecone.measured_data)\n",
"time2, bx, by, bz = zip(*accel_clean_cdm.measured_data)\n",
"\n",
"\n",
"import matplotlib.pyplot as plt\n",
Expand Down Expand Up @@ -628,8 +628,8 @@
}
],
"source": [
"time1, wx, wy, wz = zip(*gyro_noisy.measured_values)\n",
"time2, zx, zy, zz = zip(*gyro_clean.measured_values)\n",
"time1, wx, wy, wz = zip(*gyro_noisy.measured_data)\n",
"time2, zx, zy, zz = zip(*gyro_clean.measured_data)\n",
"\n",
"plt.plot(time1, wx, label='Noisy Gyroscope')\n",
"plt.plot(time2, zx, label='Clean Gyroscope')\n",
Expand Down
8 changes: 4 additions & 4 deletions rocketpy/sensors/accelerometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ def measure(self, t, u, u_dot, relative_position, gravity, *args):
A = self.quantize(A)

self.measurement = tuple([*A])
self.measured_values.append((t, *A))
self.measured_data.append((t, *A))

def export_measured_values(self, filename, format="csv"):
def export_measured_data(self, filename, format="csv"):
"""
Export the measured values to a file
Expand All @@ -179,13 +179,13 @@ def export_measured_values(self, filename, format="csv"):
if format == "csv":
with open(filename, "w") as f:
f.write("t,ax,ay,az\n")
for t, ax, ay, az in self.measured_values:
for t, ax, ay, az in self.measured_data:
f.write(f"{t},{ax},{ay},{az}\n")
elif format == "json":
import json

data = {"t": [], "ax": [], "ay": [], "az": []}
for t, ax, ay, az in self.measured_values:
for t, ax, ay, az in self.measured_data:
data["t"].append(t)
data["ax"].append(ax)
data["ay"].append(ay)
Expand Down
8 changes: 4 additions & 4 deletions rocketpy/sensors/gyroscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def measure(self, t, u, u_dot, relative_position, *args):
W = self.quantize(W)

self.measurement = tuple([*W])
self.measured_values.append((t, *W))
self.measured_data.append((t, *W))

def apply_acceleration_sensitivity(
self, omega, u_dot, relative_position, rotation_matrix
Expand Down Expand Up @@ -194,7 +194,7 @@ def apply_acceleration_sensitivity(

return self.acceleration_sensitivity & A

def export_measured_values(self, filename, format="csv"):
def export_measured_data(self, filename, format="csv"):
"""
Export the measured values to a file
Expand All @@ -213,13 +213,13 @@ def export_measured_values(self, filename, format="csv"):
if format == "csv":
with open(filename, "w") as f:
f.write("t,wx,wy,wz\n")
for t, wx, wy, wz in self.measured_values:
for t, wx, wy, wz in self.measured_data:
f.write(f"{t},{wx},{wy},{wz}\n")
elif format == "json":
import json

data = {"t": [], "wx": [], "wy": [], "wz": []}
for t, wx, wy, wz in self.measured_values:
for t, wx, wy, wz in self.measured_data:
data["t"].append(t)
data["wx"].append(wx)
data["wy"].append(wy)
Expand Down
4 changes: 2 additions & 2 deletions rocketpy/sensors/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __init__(
self.name = name
self._random_walk_drift = Vector([0, 0, 0])
self.measurement = None
self.measured_values = [] # change to data
self.measured_data = [] # change to data

# handle measurement range
if isinstance(measurement_range, (tuple, list)):
Expand Down Expand Up @@ -175,7 +175,7 @@ def measure(self, *args, **kwargs):
pass

@abstractmethod
def export_measured_values(self):
def export_measured_data(self):
pass

def quantize(self, value):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_ideal_accelerometer(flight_calisto_accel_gyro):
Pytest fixture for the flight of the calisto rocket with an ideal accelerometer and a gyroscope.
"""
accelerometer = flight_calisto_accel_gyro.rocket.sensors[0].component
time, ax, ay, az = zip(*accelerometer.measured_values)
time, ax, ay, az = zip(*accelerometer.measured_data)
ax = np.array(ax)
ay = np.array(ay)
az = np.array(az)
Expand All @@ -51,7 +51,7 @@ def test_ideal_gyroscope(flight_calisto_accel_gyro):
Pytest fixture for the flight of the calisto rocket with an ideal accelerometer and a gyroscope.
"""
gyroscope = flight_calisto_accel_gyro.rocket.sensors[1].component
time, wx, wy, wz = zip(*gyroscope.measured_values)
time, wx, wy, wz = zip(*gyroscope.measured_data)
wx = np.array(wx)
wy = np.array(wy)
wz = np.array(wz)
Expand Down
48 changes: 24 additions & 24 deletions tests/unit/test_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_rotation_matrix(noisy_rotated_accelerometer):

def test_ideal_accelerometer_measure(ideal_accelerometer):
"""Test the measure method of the Accelerometer class. Checks if saved
measurement is (ax,ay,az) and if measured_values is [(t, (ax,ay,az)), ...]
measurement is (ax,ay,az) and if measured_data is [(t, (ax,ay,az)), ...]
"""
t = SOLUTION[0]
u = SOLUTION[1:]
Expand All @@ -100,18 +100,18 @@ def test_ideal_accelerometer_measure(ideal_accelerometer):
assert ideal_accelerometer.measurement == approx([ax, ay, az], abs=1e-10)

# check measured values
assert len(ideal_accelerometer.measured_values) == 1
assert len(ideal_accelerometer.measured_data) == 1
ideal_accelerometer.measure(t, u, UDOT, relative_position, gravity)
assert len(ideal_accelerometer.measured_values) == 2
assert len(ideal_accelerometer.measured_data) == 2

assert all(isinstance(i, tuple) for i in ideal_accelerometer.measured_values)
assert ideal_accelerometer.measured_values[0][0] == t
assert ideal_accelerometer.measured_values[0][1:] == approx([ax, ay, az], abs=1e-10)
assert all(isinstance(i, tuple) for i in ideal_accelerometer.measured_data)
assert ideal_accelerometer.measured_data[0][0] == t
assert ideal_accelerometer.measured_data[0][1:] == approx([ax, ay, az], abs=1e-10)


def test_ideal_gyroscope_measure(ideal_gyroscope):
"""Test the measure method of the Gyroscope class. Checks if saved
measurement is (wx,wy,wz) and if measured_values is [(t, (wx,wy,wz)), ...]
measurement is (wx,wy,wz) and if measured_data is [(t, (wx,wy,wz)), ...]
"""
t = SOLUTION[0]
u = SOLUTION[1:]
Expand All @@ -130,18 +130,18 @@ def test_ideal_gyroscope_measure(ideal_gyroscope):
assert ideal_gyroscope.measurement == approx([ax, ay, az], abs=1e-10)

# check measured values
assert len(ideal_gyroscope.measured_values) == 1
assert len(ideal_gyroscope.measured_data) == 1
ideal_gyroscope.measure(t, u, UDOT, relative_position)
assert len(ideal_gyroscope.measured_values) == 2
assert len(ideal_gyroscope.measured_data) == 2

assert all(isinstance(i, tuple) for i in ideal_gyroscope.measured_values)
assert ideal_gyroscope.measured_values[0][0] == t
assert ideal_gyroscope.measured_values[0][1:] == approx([ax, ay, az], abs=1e-10)
assert all(isinstance(i, tuple) for i in ideal_gyroscope.measured_data)
assert ideal_gyroscope.measured_data[0][0] == t
assert ideal_gyroscope.measured_data[0][1:] == approx([ax, ay, az], abs=1e-10)


def test_noisy_rotated_accelerometer(noisy_rotated_accelerometer):
"""Test the measure method of the Accelerometer class. Checks if saved
measurement is (ax,ay,az) and if measured_values is [(t, (ax,ay,az)), ...]
measurement is (ax,ay,az) and if measured_data is [(t, (ax,ay,az)), ...]
"""
t = SOLUTION[0]
u = SOLUTION[1:]
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_noisy_rotated_accelerometer(noisy_rotated_accelerometer):

def test_noisy_rotated_gyroscope(noisy_rotated_gyroscope):
"""Test the measure method of the Gyroscope class. Checks if saved
measurement is (wx,wy,wz) and if measured_values is [(t, (wx,wy,wz)), ...]
measurement is (wx,wy,wz) and if measured_data is [(t, (wx,wy,wz)), ...]
"""
t = SOLUTION[0]
u = SOLUTION[1:]
Expand Down Expand Up @@ -216,7 +216,7 @@ def test_noisy_rotated_gyroscope(noisy_rotated_gyroscope):

def test_quatization_accelerometer(quantized_accelerometer):
"""Test the measure method of the Accelerometer class. Checks if saved
measurement is (ax,ay,az) and if measured_values is [(t, (ax,ay,az)), ...]
measurement is (ax,ay,az) and if measured_data is [(t, (ax,ay,az)), ...]
"""
t = SOLUTION[0]
u = SOLUTION[1:]
Expand Down Expand Up @@ -249,7 +249,7 @@ def test_quatization_accelerometer(quantized_accelerometer):

def test_quatization_gyroscope(quantized_gyroscope):
"""Test the measure method of the Gyroscope class. Checks if saved
measurement is (wx,wy,wz) and if measured_values is [(t, (wx,wy,wz)), ...]
measurement is (wx,wy,wz) and if measured_data is [(t, (wx,wy,wz)), ...]
"""
t = SOLUTION[0]
u = SOLUTION[1:]
Expand Down Expand Up @@ -290,13 +290,13 @@ def test_export_accel_data_csv(ideal_accelerometer):

file_name = "sensors.csv"

ideal_accelerometer.export_measured_values(file_name, format="csv")
ideal_accelerometer.export_measured_data(file_name, format="csv")

with open(file_name, "r") as file:
contents = file.read()

expected_data = "t,ax,ay,az\n"
for t, ax, ay, az in ideal_accelerometer.measured_values:
for t, ax, ay, az in ideal_accelerometer.measured_data:
expected_data += f"{t},{ax},{ay},{az}\n"

assert contents == expected_data
Expand All @@ -322,12 +322,12 @@ def test_export_accel_data_json(ideal_accelerometer):

file_name = "sensors.json"

ideal_accelerometer.export_measured_values(file_name, format="json")
ideal_accelerometer.export_measured_data(file_name, format="json")

contents = json.load(open(file_name, "r"))

expected_data = {"t": [], "ax": [], "ay": [], "az": []}
for t, ax, ay, az in ideal_accelerometer.measured_values:
for t, ax, ay, az in ideal_accelerometer.measured_data:
expected_data["t"].append(t)
expected_data["ax"].append(ax)
expected_data["ay"].append(ay)
Expand Down Expand Up @@ -355,13 +355,13 @@ def test_export_gyro_data_csv(ideal_gyroscope):

file_name = "sensors.csv"

ideal_gyroscope.export_measured_values(file_name, format="csv")
ideal_gyroscope.export_measured_data(file_name, format="csv")

with open(file_name, "r") as file:
contents = file.read()

expected_data = "t,wx,wy,wz\n"
for t, wx, wy, wz in ideal_gyroscope.measured_values:
for t, wx, wy, wz in ideal_gyroscope.measured_data:
expected_data += f"{t},{wx},{wy},{wz}\n"

assert contents == expected_data
Expand All @@ -385,12 +385,12 @@ def test_export_gyro_data_json(ideal_gyroscope):

file_name = "sensors.json"

ideal_gyroscope.export_measured_values(file_name, format="json")
ideal_gyroscope.export_measured_data(file_name, format="json")

contents = json.load(open(file_name, "r"))

expected_data = {"t": [], "wx": [], "wy": [], "wz": []}
for t, wx, wy, wz in ideal_gyroscope.measured_values:
for t, wx, wy, wz in ideal_gyroscope.measured_data:
expected_data["t"].append(t)
expected_data["wx"].append(wx)
expected_data["wy"].append(wy)
Expand Down

0 comments on commit 378bb54

Please sign in to comment.