Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/convert_trace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./mf4
13 changes: 13 additions & 0 deletions scripts/convert_trace/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Jsonl to MDF

Convert a [JSON lines](https://github.com/macformula/hil/blob/main/canlink/jsonl.go) log file from our [CAN tracer](https://github.com/macformula/hil/blob/main/canlink/tracer.go) to an MDF file.

For developers to convert JSON lines log format to MDF for integration with [`asammdf`](https://asammdf.readthedocs.io/en/latest/). With this format, visualization tools such as [Asammdf GUI](https://canlogger.csselectronics.com/canedge-getting-started/ce2/log-file-tools/asammdf-gui/) can be used to view and debug traces.

# Usage

It is recommended to use uv python package manager.

```bash
python ./jsonl_to_mdf.py ./trace.jsonl
```
33 changes: 33 additions & 0 deletions scripts/convert_trace/jsonl_to_mf4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from asammdf import MDF, Signal
import sys
import numpy as np
import pandas as pd

input = sys.argv[1] # .jsonl
output = input + ".mdf" # .mf4
mdf = MDF(version="4.10")

# read jsonl file into pandas dataframe
dataframe = pd.read_json(input, lines=True)

# convert each can id to mdf signal
for can_id, group in dataframe.groupby("id"):
# convert time to seconds from start
times = pd.to_datetime(group["time"], format="%H:%M:%S.%f")
t0 = times.iloc[0]
timestamps = (times - t0).dt.total_seconds().to_numpy(dtype=np.float64)

# bytes is tuple, make 2D uint8 array [n samples, n bytes]
samples = np.array(group["bytes"].tolist(), dtype=np.uint8)

# generate signal & append mdf file
sig = Signal(
name = str(can_id),
samples = samples,
timestamps = timestamps,
)
mdf.append(sig)

# save mdf file (.mf4)
mdf.save(output)
mdf.close()
1 change: 1 addition & 0 deletions scripts/convert_trace/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asammdf