Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace tab-separated output with CSV #2872

Merged
merged 7 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 + ".stopwatch.txt");
filesToCheckFor.add(outputPath + scenarioName + ".traveldistancestats.txt");
filesToCheckFor.add(outputPath + scenarioName + ".scorestats.csv");
filesToCheckFor.add(outputPath + scenarioName + ".stopwatch.csv");
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 @@ -83,9 +83,9 @@ public final void testDefaultPControler() {
filesToCheckFor.add(utils.getOutputDirectory() + "0.actsFromParatransitUsers.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "pOperatorLogger.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "pStats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.csv");
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 @@ -145,9 +145,9 @@ public final void testSubsidyPControler() {
filesToCheckFor.add(utils.getOutputDirectory() + "0.actsFromParatransitUsers.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "pOperatorLogger.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "pStats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.csv");
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 @@ -84,9 +84,9 @@ public final void testSubsidyPControler() {
filesToCheckFor.add(utils.getOutputDirectory() + "0.actsFromParatransitUsers.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "pOperatorLogger.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "pStats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "traveldistancestats.txt");
filesToCheckFor.add(utils.getOutputDirectory() + "scorestats.csv");
filesToCheckFor.add(utils.getOutputDirectory() + "stopwatch.csv");
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 @@ -65,19 +65,19 @@ public void configure(Header header, Layout layout) {
}).el(PieChart.class, (viz, data) -> {
viz.title = "Mode Share";
viz.description = "at final Iteration";
viz.dataset = data.output("*.modestats.txt");
viz.ignoreColumns = List.of("Iteration");
viz.dataset = data.output("*.modestats.csv");
viz.ignoreColumns = List.of("iteration");
viz.useLastRow = true;
});


layout.row("second").el(Line.class, (viz, data) -> {

viz.title = "Score";
viz.dataset = data.output("*.scorestats.txt");
viz.dataset = data.output("*.scorestats.csv");
viz.description = "per Iteration";
viz.x = "ITERATION";
viz.columns = List.of("avg. EXECUTED", "avg. WORST", "avg. BEST");
viz.columns = List.of("avg_executed", "avg_worst", "avg_best");
viz.xAxisName = "Iteration";
viz.yAxisName = "Score";

Expand All @@ -87,7 +87,7 @@ public void configure(Header header, Layout layout) {
.el(Area.class, (viz, data) -> {
viz.title = "Mode Share Progression";
viz.description = "per Iteration";
viz.dataset = data.output("*.modestats.txt");
viz.dataset = data.output("*.modestats.csv");
viz.x = "Iteration";
viz.xAxisName = "Iteration";
viz.yAxisName = "Share";
Expand Down
93 changes: 40 additions & 53 deletions matsim/src/main/java/org/matsim/analysis/IterationStopWatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ public final class IterationStopWatch {
/** data structures to identify nested operations */
private Stack<String> currentMeasuredOperations;
private Map<String, List<String>> currentIterationChildren;
private Map<Integer, Map<String, List<String>>> children;
private final Map<Integer, Map<String, List<String>>> children;

/** Creates a new IterationStopWatch. */
public IterationStopWatch() {
this.iterations = new LinkedHashMap<Integer, Map<String, Long>>();
this.identifiers = new LinkedList<String>();
this.operations = new LinkedList<String>();
this.iterations = new LinkedHashMap<>();
this.identifiers = new LinkedList<>();
this.operations = new LinkedList<>();
this.currentIterationValues = null;
this.children = new LinkedHashMap<Integer, Map<String, List<String>>>();
this.children = new LinkedHashMap<>();
}

/**
Expand All @@ -109,18 +109,16 @@ public void reset() {
* Sets the current iteration, so that the times measured using {@link #beginOperation(String)},
* {@link #endOperation(String)} and {@link #timestamp(String)} are assigned to the correct iteration for
* the analysis.
*
* @param iteration
*/
public void beginIteration(final int iteration) {
this.iteration = Integer.valueOf(iteration);
this.iteration = iteration;
if (this.iterations.get(this.iteration) == null) {
this.currentIterationValues = new HashMap<String, Long>();
this.currentIterationValues = new HashMap<>();
this.iterations.put(this.iteration, this.currentIterationValues);
this.nextIdentifierPosition = 0;
this.nextOperationPosition = 0;
this.currentMeasuredOperations = new Stack<String>();
this.currentIterationChildren = new HashMap<String, List<String>>();
this.currentMeasuredOperations = new Stack<>();
this.currentIterationChildren = new HashMap<>();
this.children.put(this.iteration, this.currentIterationChildren);
}
this.beginOperation(OPERATION_ITERATION);
Expand All @@ -139,16 +137,16 @@ public void beginOperation(final String identifier) {

String ident = "BEGIN " + identifier;
ensureIdentifier(ident);
this.currentIterationValues.put(ident, Long.valueOf(System.currentTimeMillis()));
this.currentIterationChildren.put(identifier, new ArrayList<String>());
this.currentIterationValues.put(ident, System.currentTimeMillis());

this.currentIterationChildren.put(identifier, new ArrayList<>());

// check whether this operation has a parent operation
if (this.currentMeasuredOperations.size() > 0) {
if (!this.currentMeasuredOperations.isEmpty()) {
String parent = this.currentMeasuredOperations.peek();
this.currentIterationChildren.get(parent).add(identifier);
}

// add ident to stack
this.currentMeasuredOperations.push(identifier);
}
Expand All @@ -163,9 +161,9 @@ public void endOperation(final String identifier) {
String ident = "END " + identifier;
ensureIdentifier(ident);
ensureOperation(identifier);
this.currentIterationValues.put(ident, Long.valueOf(System.currentTimeMillis()));
this.currentIterationValues.put(ident, System.currentTimeMillis());


this.currentMeasuredOperations.pop();
}

Expand All @@ -180,31 +178,30 @@ public void endIteration() {
*/
public void timestamp(final String identifier) {
ensureIdentifier(identifier);
this.currentIterationValues.put(identifier, Long.valueOf(System.currentTimeMillis()));
this.currentIterationValues.put(identifier, System.currentTimeMillis());
}

/**
* Writes the gathered data tab-separated into a text file.
* Writes the gathered data into a file.
*
* @param filename The name of a file where to write the gathered data.
* @param delimiter The delimiter to be used as field separator.
*/
public void writeTextFile(final String filename) {

try {
BufferedWriter writer = IOUtils.getBufferedWriter(filename + ".txt");
public void writeSeparatedFile(final String filename, final String delimiter) {
try (BufferedWriter writer = IOUtils.getBufferedWriter(filename)) {

// print header
writer.write("Iteration");
writer.write("iteration");
for (String identifier : this.identifiers) {
writer.write('\t');
writer.write(delimiter);
writer.write(identifier);
}
writer.write('\t');
writer.write(delimiter);
for (String identifier : this.operations) {
writer.write('\t');
writer.write(delimiter);
writer.write(identifier);
}
writer.write("\n");
writer.write('\n');

// print data
for (Map.Entry<Integer, Map<String, Long>> entry : this.iterations.entrySet()) {
Expand All @@ -215,18 +212,18 @@ public void writeTextFile(final String filename) {
// identifiers
for (String identifier : this.identifiers) {
Long time = data.get(identifier);
writer.write('\t');
writer.write(delimiter);
writer.write(formatMilliTime(time));
}
// blank separator
writer.write('\t');
writer.write(delimiter);
// durations of operations
for (String identifier: this.operations) {
Long startTime = data.get("BEGIN " + identifier);
Long endTime = data.get("END " + identifier);
writer.write('\t');
writer.write(delimiter);
if (startTime != null && endTime != null) {
double diff = (endTime.longValue() - startTime.longValue()) / 1000.0;
double diff = (endTime - startTime) / 1000.0;
writer.write(Time.writeTime(diff));
}
}
Expand All @@ -235,7 +232,6 @@ public void writeTextFile(final String filename) {
writer.write("\n");
}
writer.flush();
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -249,13 +245,13 @@ public void writeTextFile(final String filename) {
public void writeGraphFile(String filename) {

int iterations = this.iterations.entrySet().size();
Map<String, double[]> arrayMap = new HashMap<String, double[]>();
Map<String, double[]> arrayMap = new HashMap<>();
for (String identifier : this.operations) arrayMap.put(identifier, new double[iterations]);

int iter = 0;
for(Entry<Integer, Map<String, Long>> entry : this.iterations.entrySet()) {
Map<String, Long> data = entry.getValue();

// children map of current iteration
Map<String, List<String>> childrenMap = this.children.get(entry.getKey());

Expand All @@ -264,18 +260,18 @@ public void writeGraphFile(String filename) {
Long startTime = data.get("BEGIN " + identifier);
Long endTime = data.get("END " + identifier);
if (startTime != null && endTime != null) {
double diff = (endTime.longValue() - startTime.longValue());
double diff = (endTime - startTime);

/*
* If the operation has children, subtract their durations since they are
* also included in the plot. Otherwise their duration would be counted twice.
* also included in the plot. Otherwise, their duration would be counted twice.
*/
for (String child : childrenMap.get(identifier)) {
Long childStartTime = data.get("BEGIN " + child);
Long childEndTime = data.get("END " + child);
diff -= (childEndTime.longValue() - childStartTime.longValue());
diff -= (childEndTime - childStartTime);
}

arrayMap.get(identifier)[iter] = diff / 1000.0;
} else arrayMap.get(identifier)[iter] = 0.0;
}
Expand Down Expand Up @@ -304,17 +300,12 @@ public void writeGraphFile(String filename) {
chart.getChart().getCategoryPlot().getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);

double[] iterationData = null;
// double[] sumData = new double[iterations];
for (String operation : this.operations) {
double[] data = arrayMap.get(operation);
if (operation.equals(OPERATION_ITERATION)) {
iterationData = data;
continue;
} else {
chart.addSeries(operation, data);
// for (int i = 0; i < iterations; i++) {
// sumData[i] += data[i];
// }
}
}
if (iterationData != null) {
Expand All @@ -330,8 +321,6 @@ public void writeGraphFile(String filename) {
* Make sure the given identifier exists in our collection. If it is missing, insert it at the correct
* place. "Correct" means that it tries to insert this identifier right after the last-requested identifier.
* This should help to write the gathered data out in a natural way.
*
* @param identifier
*/
private void ensureIdentifier(final String identifier) {
int pos = this.identifiers.indexOf(identifier);
Expand All @@ -345,8 +334,6 @@ private void ensureIdentifier(final String identifier) {

/**
* Does the same as {@link #ensureIdentifier(String)}, but for operation-identifiers.
*
* @param identifier
*/
private void ensureOperation(final String identifier) {
int pos = this.operations.indexOf(identifier);
Expand All @@ -359,7 +346,7 @@ private void ensureOperation(final String identifier) {
}

/**
* Formats the time given in milliseconds (e.g. returned by {@link java.lang.System#currentTimeMillis()}
* Formats the time given in milliseconds (e.g. returned by {@link java.lang.System#currentTimeMillis()})
* nicely for output.
*
* @param millis A time value in milliseconds
Expand All @@ -369,7 +356,7 @@ private String formatMilliTime(final Long millis) {
if (millis == null) {
return "";
}
return this.formatter.format(new Date(millis.longValue()));
return this.formatter.format(new Date(millis));
}

}
Loading
Loading