Skip to content

Commit

Permalink
replace traveldistancestats.txt with traveldistancestats.csv
Browse files Browse the repository at this point in the history
  • Loading branch information
mrieser committed Oct 13, 2023
1 parent 0691ad9 commit 5899b40
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public final void testPControler() {
filesToCheckFor.add(outputPath + scenarioName + ".0.actsFromParatransitUsers.txt");
filesToCheckFor.add(outputPath + scenarioName + ".pOperatorLogger.txt");
filesToCheckFor.add(outputPath + scenarioName + ".pStats.txt");
filesToCheckFor.add(outputPath + scenarioName + ".scorestats.txt");
filesToCheckFor.add(outputPath + scenarioName + ".scorestats.csv");
filesToCheckFor.add(outputPath + scenarioName + ".stopwatch.csv");
filesToCheckFor.add(outputPath + scenarioName + ".traveldistancestats.txt");
filesToCheckFor.add(outputPath + scenarioName + ".traveldistancestats.csv");
filesToCheckFor.add(outputPath + scenarioName + ".pStat_light.gexf.gz");
filesToCheckFor.add(outputPath + scenarioName + ".pStat.gexf.gz");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public final void testDefaultPControler() {
filesToCheckFor.add(utils.getOutputDirectory() + "pStats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "pStat_light.gexf.gz");
filesToCheckFor.add(utils.getOutputDirectory() + "pStat.gexf.gz");

Expand Down Expand Up @@ -147,7 +147,7 @@ public final void testSubsidyPControler() {
filesToCheckFor.add(utils.getOutputDirectory() + "pStats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "pStat_light.gexf.gz");
filesToCheckFor.add(utils.getOutputDirectory() + "pStat.gexf.gz");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public final void testSubsidyPControler() {
filesToCheckFor.add(utils.getOutputDirectory() + "pStats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "pStat_light.gexf.gz");
filesToCheckFor.add(utils.getOutputDirectory() + "pStat.gexf.gz");

Expand Down
31 changes: 18 additions & 13 deletions matsim/src/main/java/org/matsim/analysis/TravelDistanceStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.matsim.api.core.v01.population.Plan;
import org.matsim.core.config.Config;
import org.matsim.core.config.groups.ControllerConfigGroup;
import org.matsim.core.config.groups.GlobalConfigGroup;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.router.TripStructureUtils;
import org.matsim.core.router.TripStructureUtils.Trip;
Expand Down Expand Up @@ -59,36 +60,40 @@
public class TravelDistanceStats {

private final ControllerConfigGroup controllerConfigGroup;
final private BufferedWriter out;
final private String legStatsPngName;
final private String tripStatsPngName;
private final BufferedWriter out;
private final String legStatsPngName;
private final String tripStatsPngName;
private final String delimiter;

private double[] legStatsHistory = null;
private double[] tripStatsHistory = null;

private final static Logger log = LogManager.getLogger(TravelDistanceStats.class);

@Inject
TravelDistanceStats(ControllerConfigGroup controllerConfigGroup, OutputDirectoryHierarchy controlerIO) {
TravelDistanceStats(ControllerConfigGroup controllerConfigGroup, OutputDirectoryHierarchy controlerIO, GlobalConfigGroup globalConfig) {
this(controllerConfigGroup, controlerIO.getOutputFilename("traveldistancestats"),
controlerIO.getOutputFilename("traveldistancestats") + "legs",
controlerIO.getOutputFilename("traveldistancestats") + "trips", controllerConfigGroup.isCreateGraphs());
controlerIO.getOutputFilename("traveldistancestats") + "trips",
controllerConfigGroup.isCreateGraphs(),
globalConfig.getDefaultDelimiter());
}

/**
* @param filename including the path, excluding the file type extension
* @param createPNG true if in every iteration, the distance statistics should be visualized in a graph and written to disk.
* @throws UncheckedIOException
*/
public TravelDistanceStats(final Config config, final String filename, final boolean createPNG) throws UncheckedIOException {
this(config.controller(), filename, filename + "legs", filename + "trips", createPNG);
public TravelDistanceStats(final Config config, final String filename, final boolean createPNG, final String delimiter) throws UncheckedIOException {
this(config.controller(), filename, filename + "legs", filename + "trips", createPNG, delimiter);
}

private TravelDistanceStats(ControllerConfigGroup controllerConfigGroup, String travelDistanceStatsFileName,
String legStatsPngName, String tripStatsPngName, boolean createPNG) {
private TravelDistanceStats(ControllerConfigGroup controllerConfigGroup, String travelDistanceStatsFileName,
String legStatsPngName, String tripStatsPngName, boolean createPNG, String delimiter) {
this.controllerConfigGroup = controllerConfigGroup;
this.legStatsPngName = legStatsPngName;
this.tripStatsPngName = tripStatsPngName;
this.delimiter = delimiter;
if (createPNG) {
int iterations = controllerConfigGroup.getLastIteration() - controllerConfigGroup.getFirstIteration();
if (iterations > 5000) {
Expand All @@ -97,13 +102,13 @@ private TravelDistanceStats(ControllerConfigGroup controllerConfigGroup, String
this.legStatsHistory = new double[iterations+1];
this.tripStatsHistory = new double[iterations+1];
}
if (travelDistanceStatsFileName.toLowerCase(Locale.ROOT).endsWith(".txt")) {
if (travelDistanceStatsFileName.toLowerCase(Locale.ROOT).endsWith(".csv")) {
this.out = IOUtils.getBufferedWriter(travelDistanceStatsFileName);
} else {
this.out = IOUtils.getBufferedWriter(travelDistanceStatsFileName + ".txt");
this.out = IOUtils.getBufferedWriter(travelDistanceStatsFileName + ".csv");
}
try {
this.out.write("ITERATION\tavg. Average Leg distance\tavg. Average Trip distance\n");
this.out.write("ITERATION" + this.delimiter + "avg. Average Leg distance" + this.delimiter + "avg. Average Trip distance\n");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down Expand Up @@ -149,7 +154,7 @@ public void addIteration(int iteration, IdMap<Person, Plan> map) {
log.info("(and teleported legs whose route contains a distance.)");// TODO: still valid?

try {
this.out.write(iteration + "\t" + legStats.getAverage() + "\t" + tripStats.getAverage() + "\t" + "\n");
this.out.write(iteration + this.delimiter + legStats.getAverage() + this.delimiter + tripStats.getAverage() + this.delimiter + "\n");
this.out.flush();
} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.matsim.api.core.v01.population.Route;
import org.matsim.core.config.groups.ControllerConfigGroup;
import org.matsim.core.config.groups.ControllerConfigGroup.CompressionType;
import org.matsim.core.config.groups.GlobalConfigGroup;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting;
import org.matsim.core.population.PopulationUtils;
Expand All @@ -35,9 +36,9 @@
*/
public class TravelDistanceStatsTest {

HashMap<String, Integer> person3modes = new HashMap<String, Integer>();
HashMap<String, Integer> person1modes = new HashMap<String, Integer>();
HashMap<String, Integer> person2modes = new HashMap<String, Integer>();
HashMap<String, Integer> person3modes = new HashMap<>();
HashMap<String, Integer> person1modes = new HashMap<>();
HashMap<String, Integer> person2modes = new HashMap<>();
private int avglegdis;
private int avgtripdis;
private Double person1legsum;
Expand Down Expand Up @@ -342,7 +343,7 @@ private void performTest(IdMap<Person, Plan> map, String outputDirectory) {
controllerConfigGroup.setCreateGraphs(true);
controllerConfigGroup.setFirstIteration(0);
controllerConfigGroup.setLastIteration(10);
TravelDistanceStats travelDistanceStats = new TravelDistanceStats(controllerConfigGroup, controlerIO);
TravelDistanceStats travelDistanceStats = new TravelDistanceStats(controllerConfigGroup, controlerIO, new GlobalConfigGroup());
travelDistanceStats.addIteration(0, map);
readAndValidateValues(0, person1legsum + person2legsum + person3legsum, 12,
person1TotalNumberOfLegs + person2TotalNumberOfLegs + person3TotalNumberOfLegs);
Expand All @@ -357,22 +358,23 @@ private void performTest(IdMap<Person, Plan> map, String outputDirectory) {

private void readAndValidateValues(int itr, Double legSum, int totalTrip, long totalLeg) {

String file = utils.getOutputDirectory() + "/TravelDistanceStat" + "/traveldistancestats.txt";
String file = utils.getOutputDirectory() + "/TravelDistanceStat" + "/traveldistancestats.csv";
BufferedReader br;
String line;
try {
br = new BufferedReader(new FileReader(file));
String firstRow = br.readLine();
String[] columnNames = firstRow.split(" ");
String delimiter = new GlobalConfigGroup().getDefaultDelimiter();
String[] columnNames = firstRow.split(delimiter);
decideColumns(columnNames);
int iteration = 0;
while ((line = br.readLine()) != null) {
if (iteration == itr) {
String[] column = line.split(" ");
String[] column = line.split(delimiter);
// checking if column number in greater than 0, because 0th column is always
// 'Iteration' and we don't need that --> see decideColumns() method
Double avgLegvalue = (avglegdis > 0) ? Double.valueOf(column[avglegdis]) : 0;
Double avgTripvalue = (avgtripdis > 0) ? Double.valueOf(column[avgtripdis]) : 0;
double avgLegvalue = (avglegdis > 0) ? Double.parseDouble(column[avglegdis]) : 0;
double avgTripvalue = (avgtripdis > 0) ? Double.parseDouble(column[avgtripdis]) : 0;

Assert.assertEquals("avg. Average Trip distance does not match", (legSum / totalTrip), avgTripvalue,
0);
Expand All @@ -384,7 +386,6 @@ private void readAndValidateValues(int itr, Double legSum, int totalTrip, long t
iteration++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Expand Down

0 comments on commit 5899b40

Please sign in to comment.