[eval_records]

- refactored
This commit is contained in:
2024-10-27 20:40:11 +01:00
parent aab4f4a0bf
commit e6afc01c4a
2 changed files with 47 additions and 18 deletions
+44 -15
View File
@@ -2,6 +2,7 @@
import os import os
import json import json
import numpy as np import numpy as np
from datetime import datetime, timedelta
import dateutil.parser as dup import dateutil.parser as dup
from jay_diff import jay_merge_full from jay_diff import jay_merge_full
from matplotlib import pyplot as plot from matplotlib import pyplot as plot
@@ -45,7 +46,8 @@ def convert(_filename: str) -> int:
class Endpoint: class Endpoint:
def __init__(self, _ep_spec: dict, _ts_format='%Y-%m-%dT%H:%M:%S.%f%z'): def __init__(self, _name: str, _ep_spec: dict, _ts_format='%Y-%m-%dT%H:%M:%S.%f%z'):
self._name = _name
self._ep_path = _ep_spec["values"] self._ep_path = _ep_spec["values"]
self._ep_path_keys = self.to_keys(self._ep_path) self._ep_path_keys = self.to_keys(self._ep_path)
self._timestamps = [] self._timestamps = []
@@ -57,6 +59,10 @@ class Endpoint:
def timestamps_str(self) -> str: def timestamps_str(self) -> str:
return f"{self._ep_path}:{len(self._timestamps)}: {self._timestamps}" return f"{self._ep_path}:{len(self._timestamps)}: {self._timestamps}"
@property
def name(self):
return self._name
@property @property
def timestamps(self) -> list[int]: def timestamps(self) -> list[int]:
return self._timestamps return self._timestamps
@@ -126,7 +132,7 @@ def check_sel(root_path, sel: list):
return res return res
def process(end_points, base_path: str, user: str, vin: str, sel_years=None, sel_months=None, sel_days=None, sel_hours=None): def process(end_points: list[Endpoint], base_path: str, user: str, vin: str, sel_years=None, sel_months=None, sel_days=None, sel_hours=None):
_path = os.path.join(base_path, user) _path = os.path.join(base_path, user)
root_path = os.path.join(_path, vin) root_path = os.path.join(_path, vin)
@@ -172,44 +178,67 @@ def process(end_points, base_path: str, user: str, vin: str, sel_years=None, sel
vehicle_data = jay_merge_full(last, vehicle_diff) vehicle_data = jay_merge_full(last, vehicle_diff)
last = vehicle_data last = vehicle_data
try: try:
for k in end_points.keys(): for ep in end_points:
end_points[k].assign({'info': record['info'], 'data': vehicle_data}) ep.assign({'info': record['info'], 'data': vehicle_data})
except KeyError: except KeyError:
pass pass
for k in end_points.keys(): for ep in end_points:
end_points[k].remove_dups() ep.remove_dups()
def find_ep_by_name(end_points: list[Endpoint], name: str):
for ep in end_points:
if name in ep.name:
return ep
return None
def process_events(_ep_dict: dict, ts_from: datetime, ts_to: datetime, ts_step: timedelta):
for ep_key in _ep_dict:
for ep in _ep_dict[ep_key]:
now = ts_from
while now < ts_to:
print(f"Date/time: {now}")
now += ts_step
if __name__ == '__main__': if __name__ == '__main__':
end_points = {} end_points = []
for k in settings.eps.keys(): for k in settings.eps.keys():
end_points[k] = Endpoint(settings.eps[k]) end_points.append(Endpoint(k, settings.eps[k]))
process(end_points, settings.BASE, settings.USER, settings.VIN, sel_days=settings.sel_days) process(end_points, settings.BASE, settings.USER, settings.VIN, sel_days=settings.sel_days)
# process_events(end_points, datetime(2024, 10, 25, 0, 0, 0), datetime(2024, 10, 26, 0, 0, 0), timedelta(hours=1))
speed = [0] speed = [0]
_speed = 0 _speed = 0
for i in range(1, len(end_points['odo'].timestamps)): ep_odo = find_ep_by_name(end_points, name="odo")
dt = float(end_points['odo'].timestamps[i] - end_points['odo'].timestamps[i-1])/(60*100000000) ep_soc = find_ep_by_name(end_points, name="soc")
dv = end_points['odo'].values[i] - end_points['odo'].values[i-1] ep_range = find_ep_by_name(end_points, name="range")
for i in range(1, len(ep_odo.timestamps)):
dt = float(ep_odo.timestamps[i] - ep_odo.timestamps[i-1])/(60*100000000)
dv = ep_odo.values[i] - ep_odo.values[i-1]
_speed = 0.9*_speed + 0.1*dv/dt _speed = 0.9*_speed + 0.1*dv/dt
speed.append(_speed) speed.append(_speed)
plot.subplot(4, 1, 1) plot.subplot(4, 1, 1)
plot.plot(np.array(end_points['odo'].timestamps) - end_points['odo'].timestamps[0], np.array(end_points['odo'].values) - end_points['odo'].values[0]) plot.plot(np.array(ep_odo.timestamps) - ep_odo.timestamps[0], np.array(ep_odo.values) - ep_odo.values[0])
plot.title("Kilometerstand") plot.title("Kilometerstand")
plot.grid() plot.grid()
plot.subplot(4, 1, 2) plot.subplot(4, 1, 2)
plot.plot(np.array(end_points['odo'].timestamps) - end_points['odo'].timestamps[0], speed) plot.plot(np.array(ep_odo.timestamps) - ep_odo.timestamps[0], speed)
plot.title("Speed") plot.title("Speed")
plot.grid() plot.grid()
plot.subplot(4, 1, 3) plot.subplot(4, 1, 3)
plot.plot(np.array(end_points['soc'].timestamps) - end_points['soc'].timestamps[0], end_points['soc'].values) plot.plot(np.array(ep_soc.timestamps) - ep_soc.timestamps[0], ep_soc.values)
plot.title("Akkustand") plot.title("Akkustand")
plot.grid() plot.grid()
plot.subplot(4, 1, 4) plot.subplot(4, 1, 4)
plot.plot(np.array(end_points['range'].timestamps) - end_points['range'].timestamps[0], end_points['range'].values) plot.plot(np.array(ep_range.timestamps) - ep_range.timestamps[0], ep_range.values)
plot.title("Range") plot.title("Range")
plot.grid() plot.grid()
+3 -3
View File
@@ -13,18 +13,18 @@ eps = {
{ {
"values": "data/measurements/odometerStatus/value/odometer", "values": "data/measurements/odometerStatus/value/odometer",
"timestamps": "data/measurements/odometerStatus/value/carCapturedTimestamp" "timestamps": "data/measurements/odometerStatus/value/carCapturedTimestamp"
# "timestamps": "info/timestamp"
}, },
"soc": "soc":
{ {
"values": "data/measurements/fuelLevelStatus/value/currentSOC_pct", "values": "data/measurements/fuelLevelStatus/value/currentSOC_pct",
"timestamps": "data/measurements/fuelLevelStatus/value/carCapturedTimestamp" "timestamps": "data/measurements/fuelLevelStatus/value/carCapturedTimestamp"
# "timestamps": "info/timestamp"
}, },
"range": "range":
{ {
"values": "data/fuelStatus/rangeStatus/value/totalRange_km", "values": "data/fuelStatus/rangeStatus/value/totalRange_km",
"timestamps": "data/fuelStatus/rangeStatus/value/carCapturedTimestamp" "timestamps": "data/fuelStatus/rangeStatus/value/carCapturedTimestamp"
# "timestamps": "info/timestamp"
} }
} }
# 'odo': Endpoint('data/measurements/odometerStatus/value/odometer','info/timestamp'),
# 'soc': Endpoint('data/measurements/fuelLevelStatus/value/currentSOC_pct','info/timestamp'),