Skip to content

Commit

Permalink
Add flag to load analysis data ordered by the seed of the dynamic_dat…
Browse files Browse the repository at this point in the history
…a files
  • Loading branch information
tillschallau committed Nov 14, 2023
1 parent 687601f commit 520bb1e
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,54 @@ import tools.aqua.stars.data.av.Segment
* Returns a [Sequence] of [Segment]s given a [List] of [CarlaSimulationRunsWrapper]s. Each
* [CarlaSimulationRunsWrapper] contains the information about the used map data and the dynamic
* data, each as [Path]s.
*
* @param simulationRunsWrappers The [List] of [CarlaSimulationRunsWrapper]s that wrap the map data
* to its dynamic data
* @param useEveryVehicleAsEgo Whether the [Segment]s should be enriched by considering every
* vehicle as the "ego" vehicle
* @param minSegmentTickCount The amount of ticks there should be at minimum to generate a [Segment]
* @param orderFilesBySeed Whether the dynamic data files should be sorted by their seeds instead of
* the map
* @return A [Sequence] of [Segment]s based on the given [simulationRunsWrappers]
*/
fun loadSegments(
simulationRunsWrappers: List<CarlaSimulationRunsWrapper>,
useEveryVehicleAsEgo: Boolean = false,
minSegmentTickCount: Int = 10
minSegmentTickCount: Int = 10,
orderFilesBySeed: Boolean = false

Check warning on line 51 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L50-L51

Added lines #L50 - L51 were not covered by tests
): Sequence<Segment> {
// Load Blocks and save in SimulationRunsWrapper
simulationRunsWrappers.forEach { it.blocks = loadBlocks(it.mapDataFile).toList() }

var simulationRunsWrapperList = simulationRunsWrappers

Check warning on line 53 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L53

Added line #L53 was not covered by tests
// Check that every SimulationRunsWrapper has dynamic data files loaded
simulationRunsWrappers.forEach {
simulationRunsWrapperList.forEach {

Check warning on line 55 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L55

Added line #L55 was not covered by tests
check(it.dynamicDataFiles.any()) {
"The SimulationRunsWrapper with map data file '${it.mapDataFile}' has no dynamic files. Dynamic file " +
"paths: ${it.dynamicDataFiles.map { dynamicDataFilePath -> dynamicDataFilePath.toUri() }}"
}
}

// If orderFilesBySeed is set, order the dynamic data files by seed instead of grouped by map name
if (orderFilesBySeed) {
// Create a single CarlaSimulationRunsWrapper for each dynamic file
var sortedSimulationRunsWrapperList =
simulationRunsWrapperList.flatMap { wrapper ->
wrapper.dynamicDataFiles.map {
CarlaSimulationRunsWrapper(wrapper.mapDataFile, listOf(it))

Check warning on line 68 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L65-L68

Added lines #L65 - L68 were not covered by tests
}
}
// Sort the list by the seed name in the dynamic file name
sortedSimulationRunsWrapperList =
sortedSimulationRunsWrapperList.sortedBy {

Check warning on line 73 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L72-L73

Added lines #L72 - L73 were not covered by tests
getSeed(it.dynamicDataFiles.first().fileName.toString())
}
// Set simulationRunsWrapperList to sorted list to proceed with the sorted list
simulationRunsWrapperList = sortedSimulationRunsWrapperList

Check warning on line 77 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L77

Added line #L77 was not covered by tests
}

// Load Blocks and save in SimulationRunsWrapper
simulationRunsWrapperList.forEach { it.blocks = loadBlocks(it.mapDataFile).toList() }

Check warning on line 81 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L81

Added line #L81 was not covered by tests

/** Holds the [ArrayDeque] of [CarlaSimulationRunsWrapper] from the parameters */
val simulationRunsWrappersDeque = ArrayDeque(simulationRunsWrappers)
val simulationRunsWrappersDeque = ArrayDeque(simulationRunsWrapperList)

Check warning on line 84 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L84

Added line #L84 was not covered by tests
/** Holds the [Segment]s that still need to be calculated for the [Sequence] of [Segment]s */
val segmentBuffer = ArrayDeque<Segment>()

Expand Down Expand Up @@ -98,50 +127,99 @@ fun loadSegments(
* [dynamicDataFile] path. [useEveryVehicleAsEgo] lets you decide whether to use every vehicle to be
* used as the ego vehicle. This will multiply the size of the resulting sequence of [Segment]s by
* the number of vehicles.
*
* @param mapDataFile The [Path] to map data file containing all static information
* @param dynamicDataFile The [Path] to the data file which contains the timed state data for the
* simulation
* @param useEveryVehicleAsEgo Whether the [Segment]s should be enriched by considering every
* vehicle as the "ego" vehicle
* @param minSegmentTickCount The amount of ticks there should be at minimum to generate a [Segment]
* @param orderFilesBySeed Whether the dynamic data files should be sorted by their seeds instead of
* the map
* @return A [Sequence] of [Segment]s based on the given [mapDataFile] and [dynamicDataFile]
*/
fun loadSegments(
mapDataFile: Path,
dynamicDataFile: Path,
useEveryVehicleAsEgo: Boolean = false
useEveryVehicleAsEgo: Boolean = false,
minSegmentTickCount: Int = 10,
orderFilesBySeed: Boolean = false

Check warning on line 146 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L144-L146

Added lines #L144 - L146 were not covered by tests
): Sequence<Segment> {
// Call actual implementation of loadSegments with correct data structure
return loadSegments(
listOf(CarlaSimulationRunsWrapper(mapDataFile, listOf(dynamicDataFile))),
useEveryVehicleAsEgo)
useEveryVehicleAsEgo,
minSegmentTickCount,
orderFilesBySeed)

Check warning on line 153 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L151-L153

Added lines #L151 - L153 were not covered by tests
}

/**
* Load [Segment]s for one specific map. The map data comes from the file [mapDataFile] and the
* dynamic data from multiple files [dynamicDataFiles].
*
* @param mapDataFile The [Path] to map data file containing all static information
* @param dynamicDataFiles A [List] of [Path]s to the data files which contain the timed state data
* for the simulation
* @param useEveryVehicleAsEgo Whether the [Segment]s should be enriched by considering every
* vehicle as the "ego" vehicle
* @param minSegmentTickCount The amount of ticks there should be at minimum to generate a [Segment]
* @param orderFilesBySeed Whether the dynamic data files should be sorted by their seeds instead of
* the map
* @return A [Sequence] of [Segment]s based on the given [mapDataFile] and [dynamicDataFiles]
*/
fun loadSegments(
mapDataFile: Path,
dynamicDataFiles: List<Path>,
useEveryVehicleAsEgo: Boolean = false
useEveryVehicleAsEgo: Boolean = false,
minSegmentTickCount: Int = 10,
orderFilesBySeed: Boolean = false

Check warning on line 175 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L173-L175

Added lines #L173 - L175 were not covered by tests
): Sequence<Segment> {
// Call actual implementation of loadSegments with correct data structure
return loadSegments(
listOf(CarlaSimulationRunsWrapper(mapDataFile, dynamicDataFiles)), useEveryVehicleAsEgo)
listOf(CarlaSimulationRunsWrapper(mapDataFile, dynamicDataFiles)),
useEveryVehicleAsEgo,
minSegmentTickCount,
orderFilesBySeed)

Check warning on line 182 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L179-L182

Added lines #L179 - L182 were not covered by tests
}

/**
* Load [Segment]s based on a [Map]. The [Map] needs to have the following structure. Map<[Path],
* List[Path]> with the semantics: Map<MapDataPath, List<DynamicDataPath>>. As each dynamic data is
* linked to a map data, they are linked in the [Map].
*
* @param mapToDynamicDataFiles Maps the [Path] of the static file to a [List] of [Path]s of dynamic
* files related to the static file
* @param useEveryVehicleAsEgo Whether the [Segment]s should be enriched by considering every
* vehicle as the "ego" vehicle
* @param minSegmentTickCount The amount of ticks there should be at minimum to generate a [Segment]
* @param orderFilesBySeed Whether the dynamic data files should be sorted by their seeds instead of
* the map
* @return A [Sequence] of [Segment]s based on the given [mapDataFile] and [dynamicDataFiles]
*/
fun loadSegments(
mapToDynamicDataFiles: Map<Path, List<Path>>,
useEveryVehicleAsEgo: Boolean = false
useEveryVehicleAsEgo: Boolean = false,
minSegmentTickCount: Int = 10,
orderFilesBySeed: Boolean = false

Check warning on line 203 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L201-L203

Added lines #L201 - L203 were not covered by tests
): Sequence<Segment> {
/** Holds the [List] of [CarlaSimulationRunsWrapper]s */
val listOfCarlaSimulationRunsWrappers =
mapToDynamicDataFiles.map { CarlaSimulationRunsWrapper(it.key, it.value) }

// Call actual implementation of loadSegments with correct data structure
return loadSegments(listOfCarlaSimulationRunsWrappers, useEveryVehicleAsEgo)
return loadSegments(
listOfCarlaSimulationRunsWrappers,
useEveryVehicleAsEgo,
minSegmentTickCount,
orderFilesBySeed)

Check warning on line 214 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/CarlaDataFileHandling.kt#L210-L214

Added lines #L210 - L214 were not covered by tests
}

/** Return a [Sequence] of [Block]s from a given [mapDataFile] path. */
/**
* Return a [Sequence] of [Block]s from a given [mapDataFile] path.
*
* @param mapDataFile The [Path] which points to the file that contains the static map data
* @return The loaded [Block]s as a [Sequence] based on the given [mapDataFile] [Path]
*/
fun loadBlocks(mapDataFile: Path): Sequence<Block> {
/** Holds the decoded list of [JsonBlock]s from the specified [mapDataFile] */
val jsonBlocks = getJsonContentOfPath<List<JsonBlock>>(mapDataFile)
Expand All @@ -153,6 +231,9 @@ fun loadBlocks(mapDataFile: Path): Sequence<Block> {
* Returns the parsed Json content for the given [inputFilePath]. Currently supported file
* extensions: ".json", ".zip". The generic parameter [T] specifies the class to which the content
* should be parsed to
*
* @param inputFilePath The [Path] from which the JSON content should be loaded from
* @return The parsed JSON content from the given [Path]
*/
inline fun <reified T> getJsonContentOfPath(inputFilePath: Path): T {
// Create JsonBuilder with correct settings
Expand Down Expand Up @@ -196,6 +277,10 @@ inline fun <reified T> getJsonContentOfPath(inputFilePath: Path): T {
* [CarlaSimulationRunsWrapper] contains a [Path] to the [mapDataFile] and a list of [Path]s for the
* [dynamicDataFiles]. It also holds properties for calculated [Block]s and the [Path]s as an
* [ArrayDeque] in [dynamicDataFilesArrayDeque]
*
* @param mapDataFile The [Path] to map data file containing all static information
* @param dynamicDataFiles A [List] of [Path]s to the data files which contain the timed state data
* for the simulation
*/
data class CarlaSimulationRunsWrapper(val mapDataFile: Path, val dynamicDataFiles: List<Path>) {
/** Holds a [List] of [Block]s. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ fun getMapName(fileName: String): String {
error("Unknown filename format")
}

/**
* Returns the seed value for the given [fileName].
*
* @param fileName The filename from which the seed value should be calculated from
* @throws IllegalStateException When the [fileName] does not include "dynamic_data"
*/
fun getSeed(fileName: String): Int {
if (fileName.isEmpty()) {
return 0

Check warning on line 43 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/RunSegmentation.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/RunSegmentation.kt#L43

Added line #L43 was not covered by tests
}
if (fileName.contains("static_data")) {
error("Cannot get seed name for map data! Analyzed file: $fileName")

Check warning on line 46 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/RunSegmentation.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/RunSegmentation.kt#L46

Added line #L46 was not covered by tests
}
if (fileName.contains("dynamic_data")) {
return fileName.split("dynamic_data_")[1].split("_seed")[1].split(".")[0].toInt()

Check warning on line 49 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/RunSegmentation.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/RunSegmentation.kt#L49

Added line #L49 was not covered by tests
}
error("Unknown filename format")

Check warning on line 51 in stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/RunSegmentation.kt

View check run for this annotation

Codecov / codecov/patch

stars-importer-carla/src/main/kotlin/tools/aqua/stars/importer/carla/RunSegmentation.kt#L51

Added line #L51 was not covered by tests
}

fun getLaneProgressionForVehicle(
blocks: List<Block>,
jsonSimulationRun: List<JsonTickData>,
Expand Down

0 comments on commit 520bb1e

Please sign in to comment.