Skip to content

Commit

Permalink
Add a fix to deal with nan values.
Browse files Browse the repository at this point in the history
Problem: JSONSchema does not support nan values.

Solution: Convert nan values to null.

This commit also populates the sequence field with a message counter
in the MCAP writer.
  • Loading branch information
YvesSchoenberg committed Mar 12, 2024
1 parent d33b635 commit 257e067
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion actions/ulog_ingestion/src/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_create_mcap_file_from_ulog(tmp_path):
print(data_object.name)
utils.create_per_topic_mcap_from_ulog(output_path_per_topic_mcap,
data_object,
schema_registry_dict)
schema_registry_dict[data_object.name])

assert output_path_per_topic_mcap.exists()

Expand Down
3 changes: 3 additions & 0 deletions actions/ulog_ingestion/src/ulog_ingestion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def process_data(
message_count = len(ulog_data_object.data["timestamp"])

json_schema_topic = utils.create_json_schema(ulog_message_format_object[topic_name].fields)

schema_checksum = utils.compute_checksum(json_schema_topic)

# Create Topic Record
Expand Down Expand Up @@ -183,6 +184,8 @@ def process_data(
version=1,
)
)
relative_file_name = ulog_file_path.split(input_dir)[1][1:]
print(f"https://app-beta.roboto.ai/visualize/{utils.generate_config(file_record.file_id, relative_file_name)}")
return


Expand Down
20 changes: 18 additions & 2 deletions actions/ulog_ingestion/src/ulog_ingestion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import json
import os
import tempfile
import math
from mcap.writer import Writer
from typing import Dict, List, Tuple, Any
from roboto.domain import topics
import base64

# Mapping from message definition types to JSON schema types
TYPE_MAPPING = {
Expand Down Expand Up @@ -116,6 +118,7 @@ def parse_values_to_json(values: List[Tuple[str, str, Any]]) -> Dict[str, Any]:
array_values = {}

for field_name, field_type, field_value in values:

if "[" in field_name:
array_name = field_name.split("[")[0]
array_values.setdefault(array_name, []).append(
Expand All @@ -139,6 +142,9 @@ def convert_value(field_type: str, value: Any) -> Any:
Returns:
- The converted value in its appropriate JSON type.
"""
if math.isnan(value):
return None

json_type = TYPE_MAPPING.get(field_type, "string")
if json_type == "integer":
return int(value)
Expand Down Expand Up @@ -214,10 +220,19 @@ def create_message_path_records(topic: Any, field_data: Any) -> None:
print(
f"Adding field: {field.field_name}, type: {field.type_str}, canonical: {canonical_data_type}"
)

return


def generate_config(file_id, relative_path):
viz_config = {
"version": "v1",
"files": [{"fileId": file_id, "relativePath": relative_path}],
}
return base64.urlsafe_b64encode(json.dumps(viz_config).encode("utf-8")).decode(
"utf-8"
)


def create_per_topic_mcap_from_ulog(
output_path_per_topic_mcap: Any, d: str, json_schema_topic: Any
) -> None:
Expand All @@ -236,7 +251,6 @@ def create_per_topic_mcap_from_ulog(
with open(output_path_per_topic_mcap, "wb") as stream:
writer = Writer(stream)
writer.start()

schema_id = writer.register_schema(
name=d.name,
encoding="jsonschema",
Expand All @@ -253,13 +267,15 @@ def create_per_topic_mcap_from_ulog(
values = list()
for f in d.field_data:
values.append((f.field_name, f.type_str, d.data[f.field_name][i]))

json_msg_instance = parse_values_to_json(values)

timestamp_ns = int(d.data["timestamp"][i] * 1000)

writer.add_message(
channel_id=channel_id,
log_time=timestamp_ns,
sequence=i,
data=json.dumps(json_msg_instance).encode("utf-8"),
publish_time=timestamp_ns,
)
Expand Down

0 comments on commit 257e067

Please sign in to comment.