Skip to content

Unmodeled Adapter

mosaicolabs.bridges.mcap.adapters.unmodeled

UnmodeledAdapter

Bases: MCAPAdapterBase[UnmodeledT, McapT], Generic[UnmodeledT, McapT]

Adapter for translating MCAP messages to Mosaico Unmodeled subclasses.

This is the only MCAP adapter provided by the library. MCAP is a container format that can carry messages of any schema and encoding, so the structure of a message cannot be known in advance and no dedicated adapters can be shipped for it. Every MCAP message is therefore stored in Mosaico as an Unmodeled payload, keeping its decoded content as raw data.

Rebuilding the original MCAP message from the data saved in Mosaico requires the original object type to fill with that data, in the same way as the ROS UnmodeledAdapter relies on a Typestore holding the original message types. For this purpose, the adapter defines the __mcap_class__ class variable, which holds the class used to reconstruct the original object. It is set when each new UnmodeledAdapter is created.

from_dict classmethod

from_dict(mcap_data, publish_time_ns)

Converts the raw dictionary data into the specific Mosaico Unmodeled type.

Parameters:

Name Type Description Default
mcap_data dict

The decoded MCAP message payload. It is updated in place with the publish_time_ns key.

required
publish_time_ns int

Message publish time (nanoseconds).

required

Returns:

Name Type Description
UnmodeledT UnmodeledT

The constructed Unmodeled subclass instance wrapping mcap_data.

to_mcap classmethod

to_mcap(mosaico_data)

Rebuilds the native MCAP message from the raw_data of an Unmodeled instance.

Parameters:

Name Type Description Default
mosaico_data UnmodeledT

The Unmodeled instance to convert. The publish_time_ns key is popped from its raw_data.

required

Returns:

Type Description
Tuple[McapT, Optional[int]]

Tuple[McapT, Optional[int]]: The __mcap_class__ instance built from raw_data and the publish time (nanoseconds), or None if not present.

get_or_create classmethod

get_or_create(
    ontology_type, mcap_class, schema_name, schema_encoding
)

Gets or creates an unmodeled adapter for the provided schema name and schema encoding.

If an adapter for the pair is already registered it is returned immediately; otherwise a new subclass is created, registered and returned.

Parameters:

Name Type Description Default
ontology_type Type[UnmodeledT]

The Unmodeled subclass produced by the adapter.

required
mcap_class Type[McapT]

The native MCAP message class rebuilt by to_mcap().

required
schema_name str

The MCAP schema name handled by the adapter.

required
schema_encoding str

The MCAP schema encoding handled by the adapter.

required

Returns:

Type Description
Type[UnmodeledAdapter]

Type[UnmodeledAdapter]: The registered adapter class.

to_native classmethod

to_native(mosaico_data, **kwargs)

Converts a Mosaico message or ontology object into a serialized native MCAP message.

Parameters:

Name Type Description Default
mosaico_data Union[Message, OntologyT]

A Message wrapper or a raw Serializable ontology instance to convert.

required
**kwargs Any

Extra arguments forwarded to to_mcap.

{}

Returns:

Name Type Description
McapReturnType McapReturnType

The native MCAP message serialized according to schema_encoding (protobuf wire bytes or UTF-8 JSON), plus the publish time returned by to_mcap().

Raises:

Type Description
TypeError

If to_mcap() returns a type not matching schema_encoding.

NotImplementedError

If schema_encoding is neither "protobuf" nor "jsonschema", or if the adapter does not implement to_mcap().

translate classmethod

translate(msg, **kwargs)

Translates a MCAP message instance into a Mosaico Message.

Parameters:

Name Type Description Default
msg MCAPMessage

The source container yielded by the MCAPLoader.

required
**kwargs Any

Contextual data such as calibration parameters or frame overrides.

{}

Returns:

Name Type Description
Message Message

A Mosaico Message object containing the instantiated ontology data.

Raises:

Type Description
Exception

If translation fails due to missing fields, type mismatches, or other errors.

unpack_mosaico_msg classmethod

unpack_mosaico_msg(mosaico_msg)

Extracts the typed Mosaico payload and its Header (if present) from a wrapped or bare message.

Handles two input cases:

  • Message wrapper: the typed data is extracted via get_data().
  • Raw ontology instance: returned as-is.

In both cases the Header is extracted from the ontology (if supported), otherwise a default Header (empty frame_id and zero Time) is returned.

Parameters:

Name Type Description Default
mosaico_msg Union[Message, T]

Either a Message envelope or a raw instance of cls.__mosaico_ontology_type__.

required

Returns:

Type Description
T

tuple[T, Header]: A (data, header) tuple where data is the typed ontology object and

Header

header is the corresponding Header, or a default Header (empty frame_id and

tuple[T, Header]

zero Time) if not present.

Raises:

Type Description
TypeError

If mosaico_msg is neither a Message nor an instance of the expected ontology type.

is_mcap_type_valid classmethod

is_mcap_type_valid(
    encoding_to_validate, schema_name_to_validate
)

Checks whether a given MCAP message type (schema_name, schema_encoding) is handled by this adapter.

Parameters:

Name Type Description Default
encoding_to_validate str

The full MCAP encoding string to check (e.g., "protobuf").

required
schema_name_to_validate str

The full MCAP schema name string to check (e.g., "foxglove.Imu").

required

Returns:

Name Type Description
bool bool

True if the adapter supports this type, False otherwise.

schema_metadata classmethod

schema_metadata(channel_name, channel_encoding, schema_def)

Builds the MCAP-specific schema metadata for this adapter.

Parameters:

Name Type Description Default
channel_name str

The full name of the MCAP channel the adapter is associated to.

required
channel_encoding str

The encoding of the MCAP channel the adapter is associated to.

required
schema_def str

The string representation of the MCAP schema the adapter is associated to, as produced by the resolved McapSchemaConverter.stringify_schema_def().

required

Returns:

Type Description
Optional[dict]

Optional[dict]: The schema metadata, wrapped under the MCAPSchemaMetadata.KEY namespace.

ontology_data_type classmethod

ontology_data_type()

Returns the Ontology class type associated with this adapter.