Skip to content

Architecture

Overview

Trajectory datasets suitable for conversion into the CommonRoad format come in many different formats. Therefore, high flexibility for the conversion framework is required. We tackle this problem with a hierarchical approach, allowing customization and adaptation on each layer. On the highest level, a simple single-producer, multi-consumer scheme is introduced to split work into independent jobs (usually creating one scenario), which can be run in parallel.

Producer-Consumer Diagram

A runner object drives the processing and takes care of the (optional) multiprocessing.

crdata.conversion.util.runner

WorkerRunner dataclass

Bases: Generic[JobT]

Run a single producer-multi consumer scheme.

If only one consumer process is requested, the scheme does not use the multiprocessing library. This is useful for debugging.

Source code in crdata/conversion/util/runner.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@dataclass
class WorkerRunner(Generic[JobT]):
    """Run a single producer-multi consumer scheme.

    If only one consumer process is requested, the scheme does not use the multiprocessing library.
    This is useful for debugging.
    """

    generator: Iterable[JobT] | Collection[JobT]
    processor: Callable[[JobT], None]
    #: Number of consumer processes.
    num_processes: int = 1

    def run(self) -> None:
        num_processes = self.num_processes
        if isinstance(self.generator, Sized):
            num_scenarios = len(self.generator)
            num_processes = min(self.num_processes, num_scenarios)
        else:
            num_scenarios = None

        if num_processes > 1:
            with Pool(num_processes) as pool:
                iterator = pool.imap_unordered(self.processor, self.generator)
                for _ in tqdm(
                    iterator,
                    total=num_scenarios,
                    desc="Creating scenarios",
                    unit="scenarios",
                ):
                    pass
        else:
            for params in tqdm(
                self.generator,
                total=num_scenarios,
                desc="Creating scenarios",
                unit="scenarios",
            ):
                self.processor(params)

Factory

To separate object creation logic from processing logic, the factory pattern is used. For each dataset, a factory builds the runner object considering the parameters for the conversion. Factories can be reused by splitting up the tasks of building the producer and consumer into more fine-grained tasks and overriding methods, as done in crdata.conversion.tabular.factory.TabularConverterFactory.

Tabular API

Since many trajectory datasets come in the form of tabular data or can be easily brought into a tabular form, we create an API specifically designed to make the conversion of these datasets as simple as possible. The main idea is to bring the dataset into a form consisting of:

  1. A table of dynamic obstacle states,
  2. A table of static information about obstacles (e.g., obstacle shape and type),
  3. A static scene environment represented as a scenario without any dynamic obstacles.

Tabular API Data Flow

To convert a new tabular dataset, only an iterator providing time windows of the dynamic obstacle information (1. and 2.) and a meta scenario creator need to be implemented. Practically speaking, this is fulfilled by implementing the corresponding interfaces interface.IRecordingGenerator and interface.IMetaScenarioCreator.

    @dataclass
    class MyRecordingMeta:
        # Store metadata, e.g., to select the map corresponding to the recording.
        recording_meta: Dict[str, Any]


    @dataclass
    class MyMetaScenarioCreator(IMetaScenarioCreator[MyRecordingMeta]):
        map_dir: Path
        ...

        def __call__(self, window: Window, window_meta: MyRecordingMeta) -> Scenario:
            # Read map from file or cache
            return self.open_map(self.map_dir)


    @dataclass
    class MyRecordingGenerator(IRecordingGenerator[MyRecordingMeta]):
        data_path: Path
        ...

        def __iter__(self) -> Iterable[Tuple[Window, MyRecordingMeta]]:
                # Read dynamic obstacle data from files
                yield Window(
                    vehicle_tracks[["x", "y", "velocity", "orientation", "acceleration"]],
                    tracks_meta[["width", "length", "obstacle_type"]],
                    dt
                ), MyRecordingMeta(recording_meta_data)

The base classes are generic to allow passing a WindowMeta object. This object tells the MetaScenarioCreator which meta scenario to create. Finally, building the converter is achieved by creating a corresponding factory inheriting from interface.TabularConverterFactory, instantiating the previously created classes.

        class MyConverterFactory(TabularConverterFactory, metaclass=ABCMeta):
        input_dir: Path
        ...

        def build_recording_generator(self) -> MyRecordingCreator:
            return MyRecordingGenerator(self.input_dir)

        def build_meta_scenario_creator(self) -> MyMetaScenarioGenerator:
            return MyMetaScenarioCreator(
                self.input_dir.parent / "DEU_Map-1.xml"
            )