dragonpilot - 基於 openpilot 的開源駕駛輔助系統
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.6 KiB

5 years ago
#!/usr/bin/env python3
import sys
import math
import capnp
import numbers
5 years ago
import dictdiffer
from collections import Counter
from openpilot.tools.lib.logreader import LogReader
5 years ago
EPSILON = sys.float_info.epsilon
5 years ago
def remove_ignored_fields(msg, ignore):
msg = msg.as_builder()
for key in ignore:
5 years ago
attr = msg
keys = key.split(".")
if msg.which() != keys[0] and len(keys) > 1:
5 years ago
continue
for k in keys[:-1]:
# indexing into list
if k.isdigit():
attr = attr[int(k)]
else:
attr = getattr(attr, k)
v = getattr(attr, keys[-1])
if isinstance(v, bool):
val = False
elif isinstance(v, numbers.Number):
val = 0
elif isinstance(v, (list, capnp.lib.capnp._DynamicListBuilder)):
val = []
else:
raise NotImplementedError(f"Unknown type: {type(v)}")
setattr(attr, keys[-1], val)
return msg
5 years ago
def compare_logs(log1, log2, ignore_fields=None, ignore_msgs=None, tolerance=None,):
if ignore_fields is None:
ignore_fields = []
if ignore_msgs is None:
ignore_msgs = []
tolerance = EPSILON if tolerance is None else tolerance
log1, log2 = (
[m for m in log if m.which() not in ignore_msgs]
for log in (log1, log2)
)
if len(log1) != len(log2):
cnt1 = Counter(m.which() for m in log1)
cnt2 = Counter(m.which() for m in log2)
raise Exception(f"logs are not same length: {len(log1)} VS {len(log2)}\n\t\t{cnt1}\n\t\t{cnt2}")
5 years ago
diff = []
for msg1, msg2 in zip(log1, log2, strict=True):
5 years ago
if msg1.which() != msg2.which():
raise Exception("msgs not aligned between logs")
5 years ago
msg1 = remove_ignored_fields(msg1, ignore_fields)
msg2 = remove_ignored_fields(msg2, ignore_fields)
5 years ago
if msg1.to_bytes() != msg2.to_bytes():
msg1_dict = msg1.as_reader().to_dict(verbose=True)
msg2_dict = msg2.as_reader().to_dict(verbose=True)
dd = dictdiffer.diff(msg1_dict, msg2_dict, ignore=ignore_fields)
# Dictdiffer only supports relative tolerance, we also want to check for absolute
# TODO: add this to dictdiffer
def outside_tolerance(diff):
try:
if diff[0] == "change":
a, b = diff[2]
finite = math.isfinite(a) and math.isfinite(b)
if finite and isinstance(a, numbers.Number) and isinstance(b, numbers.Number):
return abs(a - b) > max(tolerance, tolerance * max(abs(a), abs(b)))
except TypeError:
pass
return True
dd = list(filter(outside_tolerance, dd))
5 years ago
diff.extend(dd)
return diff
5 years ago
if __name__ == "__main__":
log1 = list(LogReader(sys.argv[1]))
log2 = list(LogReader(sys.argv[2]))
print(compare_logs(log1, log2, sys.argv[3:]))