Skip to content

Commit

Permalink
Make the refactored KPI gerenator pass the integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
mfitz committed Jan 25, 2024
1 parent 1e86754 commit 7f8f98b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class MatsimKpiGenerator implements Runnable {
private Path matsimOutputDirectory;

@Option(names = "-o", description = "Sets the output directory. Defaults to stdout", required = true)
private String outputDir;
private Path outputDir;

public static void main(String[] args) {
int exitCode = new CommandLine(new MatsimKpiGenerator()).execute(args);
Expand All @@ -55,6 +55,7 @@ public void run() {
String outputEventsFile = String.format("%s/output_events.xml.gz", matsimOutputDirectory);
System.out.printf("Streaming MATSim events from %s%n", outputEventsFile);
new MatsimEventsReader(eventsManager).readFile(outputEventsFile);

Map<String, AtomicInteger> eventsSeen = matsimLinkLogHandler.getEventCounts();
Integer eventCount = eventsSeen.values()
.stream()
Expand All @@ -69,8 +70,6 @@ public void run() {
// swallow
}

// get the network link info from the matsim outputs and pass it into the calculator

kpiCalculator.writeCongestionKpi(Path.of(format("%s/congestion-kpi.csv", outputDir)));
kpiCalculator.writeCongestionKpi(outputDir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.network.Network;
import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.LongColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;
import tech.tablesaw.api.*;

import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -17,6 +14,8 @@
import java.util.Map;
import java.util.stream.LongStream;

import static tech.tablesaw.aggregate.AggregateFunctions.mean;

public class TablesawKpiCalculator implements KpiCalculator {

private static final Logger LOGGER = LogManager.getLogger(TablesawKpiCalculator.class);
Expand Down Expand Up @@ -55,6 +54,7 @@ public class TablesawKpiCalculator implements KpiCalculator {

public TablesawKpiCalculator(Network network) {
this.network = network;
createNetworkLinkTables(network);
}

@Override
Expand Down Expand Up @@ -94,8 +94,62 @@ public void recordVehicleMode(String vehicleId, String mode) {
}

@Override
public void writeCongestionKpi(Path directory) {
System.out.printf("Writing Congestion KPIs to %s", directory);
public void writeCongestionKpi(Path outputDirectory) {
System.out.printf("Writing Congestion KPIs to %s", outputDirectory);
System.out.println("Computing - Congestion KPI");
Table linkLog = getLinkLog();

// compute travel time on links
linkLog.addColumns(
linkLog.doubleColumn("endTime")
.subtract(linkLog.doubleColumn("startTime"))
.setName("travelTime")
);

// compute free flow time on links (length / freespeed)
networkLinks.addColumns(
networkLinks.doubleColumn("length")
.divide(networkLinks.doubleColumn("freespeed"))
.setName("freeFlowTime")
);

// add freeflow time to link log
linkLog =
linkLog
.joinOn("linkID")
.inner(networkLinks.selectColumns("linkID", "freeFlowTime"));

// compute delay ratio
linkLog.addColumns(
linkLog.doubleColumn("travelTime")
.divide(linkLog.doubleColumn("freeFlowTime"))
.setName("delayRatio")
);

// put in hour bins
IntColumn hour = IntColumn.create("hour");
linkLog.doubleColumn("endTime")
.forEach(time -> hour.append(
(int) Math.floor(time / (60 * 60))
));
linkLog.addColumns(hour);

// intermediate output data
Table intermediate =
linkLog
.summarize("delayRatio", mean)
.by("linkID", "mode", "hour");
intermediate.write().csv(String.format("%s/congestion.csv", outputDirectory));

// kpi output
Table kpi =
linkLog
.where(linkLog.intColumn("hour").isGreaterThanOrEqualTo(7)
.and(linkLog.intColumn("hour").isLessThanOrEqualTo(9)))
.summarize("delayRatio", mean)
.by("mode")
.setName("Congestion KPI");
kpi.write().csv(String.format("%s/kpi.csv", outputDirectory));
}

private void newLinkLogEntry(String vehicleID, String linkID, String mode, double startTime) {
Expand Down Expand Up @@ -205,4 +259,11 @@ private void createNetworkLinkTables(Network network) {
StringColumn.create("mode", modesColumn)
);
}

private void writeIntermediateData(String outputDir) {
getLinkLog().write().csv(String.format("%s/linkLog.csv", outputDir));
getVehicleOccupancy().write().csv(String.format("%s/vehicleOccupancy.csv", outputDir));
networkLinks.write().csv(String.format("%s/networkLinks.csv", outputDir));
networkLinkModes.write().csv(String.format("%s/networkLinkModes.csv", outputDir));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ public void testApp() throws Exception {
);

assertThat(exitCode).isEqualTo(0).as("App return code should be zero");
// String[] outputFilesList = appOutputDir.getRoot().list();
// assertThat(outputFilesList).hasSize(6).as("Check number of output files created");
// assertThat(outputFilesList).contains("kpi.csv").as("Check KPI CSV file exists");
// File expectedKpiFile = new File(String.format("%s/expected-kpi.csv", testDataDirRoot));
// assertThat(new File(String.format("%s/kpi.csv", appOutputDir.getRoot())))
// .hasSameTextualContentAs(expectedKpiFile)
// .as("Check calculated KPI data");
File expectedKpiFile = new File(String.format("%s/expected-kpi.csv", testDataDirRoot));
assertThat(new File(String.format("%s/kpi.csv", appOutputDir.getRoot())))
.hasSameTextualContentAs(expectedKpiFile)
.as("Check calculated KPI data");
}
}

0 comments on commit 7f8f98b

Please sign in to comment.