Skip to content

Commit 024a211

Browse files
committed
Added custom signal calculation example
1 parent 08507aa commit 024a211

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import mdf_iter
2+
import canedge_browser
3+
import can_decoder
4+
5+
import pandas as pd
6+
7+
from datetime import datetime, timezone
8+
from utils import setup_fs, custom_sig
9+
10+
11+
# specify which devices to process (from local folder or S3 bucket)
12+
devices = ["LOG/958D2219"]
13+
14+
# specify which time period to fetch log files for
15+
start = datetime(year=2020, month=1, day=13, hour=0, minute=0, tzinfo=timezone.utc)
16+
stop = datetime(year=2099, month=1, day=1, tzinfo=timezone.utc)
17+
18+
# specify DBC path
19+
dbc_path = r"CSS-Electronics-SAE-J1939-DEMO.dbc"
20+
21+
# ---------------------------------------------------
22+
# initialize DBC converter and file loader
23+
db = can_decoder.load_dbc(dbc_path)
24+
df_decoder = can_decoder.DataFrameDecoder(db)
25+
26+
fs = setup_fs()
27+
28+
# List log files based on inputs and select first log file
29+
log_files = canedge_browser.get_log_files(fs, devices, start_date=start, stop_date=stop)
30+
log_file = log_files[0]
31+
32+
with fs.open(log_file, "rb") as handle:
33+
mdf_file = mdf_iter.MdfFile(handle)
34+
df_raw = mdf_file.get_data_frame()
35+
36+
# extract all DBC decodable signals and print dataframe
37+
df_phys = df_decoder.decode_frame(df_raw)
38+
39+
40+
# define a function for calculating the custom signal
41+
def ratio(s1, s2):
42+
if s2 != 0:
43+
return 100 * s1 / s2
44+
else:
45+
return np.nan
46+
47+
48+
# calculate custom signal and append to main dataframe, then save as CSV
49+
df_ratio = custom_sig(df_phys, "WheelBasedVehicleSpeed", "EngineSpeed", ratio, "Ratio")
50+
51+
df_phys = df_phys.append(df_ratio)
52+
df_phys.to_csv("data_incl_custom_signal.csv")
53+
print("Saved data incl. custom signal to CSV")

examples/data-processing/process_data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@
4949
path = device_id + log_file.split(device_id)[1].replace("MF4", "csv").replace(
5050
"/", "_"
5151
)
52-
df_phys.to_csv(path)
5352

5453
if df_phys.empty:
5554
continue
5655

56+
df_phys.to_csv(path)
57+
5758
# create a list of the individual DBC decoded dataframes:
5859
df_concat.append(df_phys)
5960

examples/data-processing/utils.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def setup_fs_s3():
88
secret="<secret>",
99
client_kwargs={
1010
"endpoint_url": "<endpoint>",
11-
"verify": "path\\to\\public_certificate.crt", # for TLS enabled MinIO servers
11+
# "verify": "path\\to\\public_certificate.crt", # for TLS enabled MinIO servers
1212
},
1313
)
1414

@@ -26,3 +26,33 @@ def setup_fs():
2626
fs = LocalFileSystem()
2727

2828
return fs
29+
30+
31+
def custom_sig(df, signal1, signal2, function, new_signal):
32+
"""Helper function for calculating a new signal based on two signals and a function.
33+
Returns a dataframe with the new signal name and physical values
34+
"""
35+
import pandas as pd
36+
37+
try:
38+
s1 = df[df["Signal"] == signal1]["Physical Value"].rename(signal1)
39+
s2 = df[df["Signal"] == signal2]["Physical Value"].rename(signal2)
40+
41+
df_new_sig = pd.merge_ordered(
42+
s1, s2, on="TimeStamp", fill_method="ffill",
43+
).set_index("TimeStamp")
44+
45+
df_new_sig = (
46+
df_new_sig.apply(lambda x: function(x[0], x[1]), axis=1)
47+
.dropna()
48+
.rename("Physical Value")
49+
.to_frame()
50+
)
51+
52+
df_new_sig["Signal"] = new_signal
53+
54+
return df_new_sig
55+
56+
except:
57+
print(f"Warning: Custom signal {new_signal} not created\n")
58+
return pd.DataFrame()

0 commit comments

Comments
 (0)