Add IMU frame loss analysis script
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from scripts import analyze_imu_frame_loss
|
||||
|
||||
|
||||
class AnalyzeImuFrameLossTests(unittest.TestCase):
|
||||
def _write_csv(self, path: Path, times: list[float], odr: str = "0x0F - 500 Hz"):
|
||||
lines = [
|
||||
f"# odr={odr}",
|
||||
"sensor_uptime_s,temp_c,acc_x_g,acc_y_g,acc_z_g,gyro_x_dps,gyro_y_dps,gyro_z_dps",
|
||||
]
|
||||
lines.extend(f"{time},28,0,0,1,0,0,0" for time in times)
|
||||
path.write_text("\n".join(lines), encoding="utf-8-sig")
|
||||
|
||||
def test_counts_missing_frames_from_odr_period(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "imu.csv"
|
||||
self._write_csv(path, [0.0, 0.002, 0.008])
|
||||
|
||||
result = analyze_imu_frame_loss.analyze_file(path)
|
||||
|
||||
self.assertEqual(result.received_frames, 3)
|
||||
self.assertEqual(result.missing_frames, 2)
|
||||
self.assertEqual(result.expected_frames, 5)
|
||||
self.assertAlmostEqual(result.loss_rate, 0.4)
|
||||
self.assertEqual(result.segment_count, 1)
|
||||
|
||||
def test_device_restart_starts_new_segment_without_loss(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "imu.csv"
|
||||
self._write_csv(path, [1.0, 1.002, 0.0, 0.002])
|
||||
|
||||
result = analyze_imu_frame_loss.analyze_file(path)
|
||||
|
||||
self.assertEqual(result.missing_frames, 0)
|
||||
self.assertEqual(result.segment_count, 2)
|
||||
|
||||
def test_non_integral_period_gap_reports_data_row(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "imu.csv"
|
||||
self._write_csv(path, [0.0, 0.003])
|
||||
|
||||
with self.assertRaisesRegex(ValueError, "data row 2.*0.003.*0.002"):
|
||||
analyze_imu_frame_loss.analyze_file(path)
|
||||
|
||||
def test_missing_odr_metadata_fails_clearly(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "imu.csv"
|
||||
self._write_csv(path, [0.0, 0.002], odr="unknown")
|
||||
|
||||
with self.assertRaisesRegex(ValueError, "odr.*Hz"):
|
||||
analyze_imu_frame_loss.analyze_file(path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user