Skip to content

Commit cf64bc0

Browse files
author
Martin
committed
Updated physical decoding logic to always group by data length before processing
1 parent 8929cc3 commit cf64bc0

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

examples/data-processing/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ To use the Transport Protocol functionality in other scripts, you need to make m
4444
1. Ensure that you import the `MultiFrameDecoder` class from `utils.py`
4545
2. Specify the type via the `tp_type` variable e.g. to `j1939`
4646
3. After you've extract the normal raw dataframe, parse it to the `tp.combine_tp_frames` function as below
47-
4. Ensure you include `tp_type` in the `extract_phys` function
4847

4948
See below example:
5049

@@ -53,7 +52,6 @@ tp_type = "j1939"
5352
df_raw, device_id = proc.get_raw_data(log_file)
5453
tp = MultiFrameDecoder(tp_type)
5554
df_raw = tp.combine_tp_frames(df_raw)
56-
df_phys = proc.extract_phys(df_raw, tp_type=tp_type)
5755
```
5856

5957

examples/data-processing/utils.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@ def list_log_files(fs, devices, start_times, verbose=True):
5757
if len(log_files_device) > 0:
5858
with fs.open(log_files_device[0], "rb") as handle:
5959
mdf_file = mdf_iter.MdfFile(handle)
60-
6160
df_raw_lin = mdf_file.get_data_frame_lin()
6261
df_raw_lin["IDE"] = 0
6362
df_raw_can = mdf_file.get_data_frame()
6463
df_raw = df_raw_can.append(df_raw_lin)
65-
6664
end_time = df_raw.index[-1]
6765

6866
if end_time < start:
@@ -129,7 +127,7 @@ def __init__(self, fs, db_list, signals=[], days_offset=None, verbose=True):
129127
self.verbose = verbose
130128
return
131129

132-
def extract_phys(self, df_raw, tp_type=None):
130+
def extract_phys(self, df_raw):
133131
"""Given df of raw data and list of decoding databases, create new def with
134132
physical values (no duplicate signals and optionally filtered/rebaselined)
135133
"""
@@ -140,15 +138,12 @@ def extract_phys(self, df_raw, tp_type=None):
140138
for db in self.db_list:
141139
df_decoder = can_decoder.DataFrameDecoder(db)
142140

143-
if tp_type != None:
144-
df_phys_tp = pd.DataFrame()
145-
for length, group in df_raw.groupby("DataLength"):
146-
df_phys_group = df_decoder.decode_frame(group)
147-
df_phys_tp = df_phys_tp.append(df_phys_group)
141+
df_phys_temp = pd.DataFrame()
142+
for length, group in df_raw.groupby("DataLength"):
143+
df_phys_group = df_decoder.decode_frame(group)
144+
df_phys_temp = df_phys_temp.append(df_phys_group)
148145

149-
df_phys = df_phys.append(df_phys_tp.sort_index())
150-
else:
151-
df_phys = df_phys.append(df_decoder.decode_frame(df_raw))
146+
df_phys = df_phys.append(df_phys_temp.sort_index())
152147

153148
# remove duplicates in case multiple DBC files contain identical signals
154149
df_phys["datetime"] = df_phys.index
@@ -157,20 +152,21 @@ def extract_phys(self, df_raw, tp_type=None):
157152

158153
# optionally filter and rebaseline the data
159154
df_phys = self.filter_signals(df_phys)
160-
df_phys = self.rebaseline_data(df_phys)
155+
156+
if not df_phys.empty and type(self.days_offset) == int:
157+
df_phys = self.rebaseline_data(df_phys)
161158

162159
return df_phys
163160

164161
def rebaseline_data(self, df_phys):
165162
"""Given a df of physical values, this offsets the timestamp
166163
to be equal to today, minus a given number of days.
167164
"""
168-
if not df_phys.empty and type(self.days_offset) == int:
169-
from datetime import datetime, timezone
170-
import pandas as pd
165+
from datetime import datetime, timezone
166+
import pandas as pd
171167

172-
delta_days = (datetime.now(timezone.utc) - df_phys.index.min()).days - self.days_offset
173-
df_phys.index = df_phys.index + pd.Timedelta(delta_days, "day")
168+
delta_days = (datetime.now(timezone.utc) - df_phys.index.min()).days - self.days_offset
169+
df_phys.index = df_phys.index + pd.Timedelta(delta_days, "day")
174170

175171
return df_phys
176172

0 commit comments

Comments
 (0)