Skip to content

Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)

[skip ci] Avoid re-encoding normalized index staging rows

Cinna Davis354ed19f88dc
diff --git a/src/dopplergeist/data_access.py b/src/dopplergeist/data_access.py
index 3430d3b..7eb6cf8 100644
--- a/src/dopplergeist/data_access.py
+++ b/src/dopplergeist/data_access.py
@@ -37,6 +37,17 @@ _EQUIVALENT_NORMALIZERS = {
}
+def _staging_rows(frame):
+ """Keep pandas' normalized JSON rows without encoding each one twice."""
+ payload = frame.to_json(orient="records", double_precision=15)
+ decoder = json.JSONDecoder()
+ position = 1 # Skip the opening array bracket.
+ while position < len(payload) - 1:
+ row, end = decoder.raw_decode(payload, position)
+ yield str(row["flight_id"]), row["time_s"], payload[position:end]
+ position = end + 1 # Skip the comma (or closing bracket).
+
+
def _frame_track(name, frame):
velocity, inferred = _segment_velocity(frame)
track = Track(name, frame.time_s.to_numpy(), frame.lat.to_numpy(), frame.lon.to_numpy(), frame.alt_m.to_numpy(),
@@ -278,9 +289,8 @@ class FlightIndex:
clean["aircraft_class"] = clean.typecode.map(class_map).astype(object).fillna("unclassified") if "typecode" in clean else "unclassified"
altitude.update(clean.altitude_basis.value_counts().to_dict() if "altitude_basis" in clean else {"unspecified": len(clean)})
# pandas normalizes numpy scalars/NaNs to JSON numbers/null.
- records = json.loads(clean.to_json(orient="records", double_precision=15))
- db.executemany("INSERT INTO samples VALUES(?,?,?)", [(str(r["flight_id"]), r["time_s"], json.dumps(r)) for r in records])
- rows += len(records)
+ db.executemany("INSERT INTO samples VALUES(?,?,?)", _staging_rows(clean))
+ rows += len(clean)
db.execute("CREATE INDEX flight_samples ON samples(flight,time)")
db.commit()
segments = 0
diff --git a/tests/test_real_index_staging.py b/tests/test_real_index_staging.py
new file mode 100644
index 0000000..3cd1f66
--- /dev/null
+++ b/tests/test_real_index_staging.py
@@ -0,0 +1,20 @@
+"""Check index staging against archived real flight observations."""
+import json
+from pathlib import Path
+
+import pandas as pd
+
+from dopplergeist.data_access import _staging_rows
+
+
+def test_staging_preserves_real_observation_values():
+ source = Path(__file__).resolve().parents[1] / "research/trajectory-motion/frozen/canonical-observations.json"
+ frame = pd.DataFrame(json.loads(source.read_text()))
+ expected = json.loads(frame.to_json(orient="records", double_precision=15))
+ rows = list(_staging_rows(frame))
+ assert len(rows) == len(expected) > 0
+ for record, (flight, timestamp, payload) in zip(expected, rows):
+ assert flight == str(record["flight_id"])
+ assert timestamp == record["time_s"]
+ assert json.loads(payload) == record
+ assert list(_staging_rows(frame.iloc[:0])) == []