Source code for launch_tools.csv_tools
1"""Contains snippets that can can import/export vehicle positions from/to csv files."""
2from __future__ import annotations
3
4# pyright: reportUnknownMemberType=none
5# pyright: reportUnknownVariableType=information
6from typing import TYPE_CHECKING
7if TYPE_CHECKING:
8 from os import PathLike
9
10import carla
11import pandas as pd
12
13__all__ = [
14 "csv_to_transformations",
15 "transform_to_pandas",
16 "vehicle_location_to_dataframe",
17]
18
19# A dataframe template to store the locations of vehicles
20LOC_DF = pd.DataFrame(columns=["x", "y", "z", "pitch", "yaw", "roll"])
21
22# -----------------------------------
23
24
[docs]
25def transform_to_pandas(transform: carla.Transform) -> pd.Series[float]:
26 """
27 Format a :py:class:`carla.Transform` object to a :py:class:`pandas.Series`.
28 """
29 loc = transform.location
30 rot = transform.rotation
31 return pd.Series({"x": loc.x, "y": loc.y, "z": loc.z,
32 "pitch": rot.pitch, "yaw": rot.yaw, "roll": rot.roll})
33
34
[docs]
35def vehicle_location_to_dataframe(vehicles: list[carla.Actor]) -> pd.DataFrame:
36 """
37 Exports the locations of vehicles to a :py:class:`pandas.Dataframe`.
38 """
39 df = LOC_DF.copy()
40 for i, v in enumerate(vehicles):
41 df.loc[i] = transform_to_pandas(v.get_transform())
42 return df
43
44
[docs]
45def csv_to_transformations(path: str | PathLike[str]) -> list[carla.Transform]:
46 """
47 Read a csv file and return a list of Transform objects.
48 Expected columns: :code:`x, y, z, pitch, yaw, roll`.
49 """
50 df = pd.read_csv(path) # pyright: ignore
51 transformations: list[carla.Transform] = []
52 for _, data in df.iterrows(): # pyright: ignore
53 loc = carla.Location(data.x, data.y, data.z) # type: ignore[attr-defined]
54 rot = carla.Rotation(pitch=data.pitch, yaw=data.yaw, roll=data.roll) # type: ignore[attr-defined]
55 t = carla.Transform(loc, rot)
56 transformations.append(t)
57 return transformations