From ffa2f7f8daeefb5ef0c3d159201b79b9d3890cc4 Mon Sep 17 00:00:00 2001 From: rakow Date: Mon, 25 Mar 2024 17:04:16 +0100 Subject: [PATCH] write trip analysis --- .../analysis/population/TripAnalysis.java | 5 ++-- .../population/TripByGroupAnalysis.java | 9 +++---- .../simwrapper/dashboard/TripDashboard.java | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/contribs/application/src/main/java/org/matsim/application/analysis/population/TripAnalysis.java b/contribs/application/src/main/java/org/matsim/application/analysis/population/TripAnalysis.java index 997869cae1d..e7f7879587e 100644 --- a/contribs/application/src/main/java/org/matsim/application/analysis/population/TripAnalysis.java +++ b/contribs/application/src/main/java/org/matsim/application/analysis/population/TripAnalysis.java @@ -38,7 +38,8 @@ @CommandLine.Command(name = "trips", description = "Calculates various trip related metrics.") @CommandSpec( requires = {"trips.csv", "persons.csv"}, - produces = {"mode_share.csv", "mode_share_per_dist.csv", "mode_users.csv", "trip_stats.csv", "population_trip_stats.csv", "trip_purposes_by_hour.csv"} + produces = {"mode_share.csv", "mode_share_per_dist.csv", "mode_users.csv", "trip_stats.csv", + "mode_share_per_%s.csv", "population_trip_stats.csv", "trip_purposes_by_hour.csv"} ) public class TripAnalysis implements MATSimAppCommand { @@ -189,7 +190,7 @@ public Integer call() throws Exception { writeModeShare(joined, labels); if (groups != null) { - groups.analyzeModeShare(joined, labels); + groups.analyzeModeShare(joined, labels, (g) -> output.getPath("mode_share_per_%s.csv", g)); } writePopulationStats(persons, joined); diff --git a/contribs/application/src/main/java/org/matsim/application/analysis/population/TripByGroupAnalysis.java b/contribs/application/src/main/java/org/matsim/application/analysis/population/TripByGroupAnalysis.java index 91e80dbaca2..b4073266def 100644 --- a/contribs/application/src/main/java/org/matsim/application/analysis/population/TripByGroupAnalysis.java +++ b/contribs/application/src/main/java/org/matsim/application/analysis/population/TripByGroupAnalysis.java @@ -11,7 +11,9 @@ import java.io.BufferedReader; import java.io.IOException; +import java.nio.file.Path; import java.util.*; +import java.util.function.Function; import static tech.tablesaw.aggregate.AggregateFunctions.count; @@ -82,7 +84,7 @@ final class TripByGroupAnalysis { } } - void analyzeModeShare(Table trips, List dists) { + void analyzeModeShare(Table trips, List dists, Function output) { for (Group group : groups) { @@ -114,9 +116,8 @@ void analyzeModeShare(Table trips, List dists) { .toArray(String[]::new) ); - // TODO: write trip analysis, obtain output path from TripAnalysis -// aggr.write().csv(output.getPath("mode_share_per_dist.csv").toFile()); - + String name = String.join("_", group.columns); + aggr.write().csv(output.apply(name).toFile()); } } diff --git a/contribs/simwrapper/src/main/java/org/matsim/simwrapper/dashboard/TripDashboard.java b/contribs/simwrapper/src/main/java/org/matsim/simwrapper/dashboard/TripDashboard.java index 30c4a140e6b..ee4e1812106 100644 --- a/contribs/simwrapper/src/main/java/org/matsim/simwrapper/dashboard/TripDashboard.java +++ b/contribs/simwrapper/src/main/java/org/matsim/simwrapper/dashboard/TripDashboard.java @@ -26,6 +26,8 @@ public class TripDashboard implements Dashboard { @Nullable private final String modeUsersRefCsv; + private String groupedRefData; + private String[] args; /** @@ -51,6 +53,14 @@ public TripDashboard(@Nullable String modeShareRefCsv, @Nullable String modeShar args = new String[0]; } + /** + * Set grouped reference data. Will enable additional tab with analysis for subgroups of the population. + */ + public TripDashboard withGroupedRefData(String groupedRefData) { + this.groupedRefData = groupedRefData; + return this; + } + /** * Set argument that will be passed to the analysis script. See {@link TripAnalysis}. */ @@ -65,6 +75,15 @@ public void configure(Header header, Layout layout) { header.title = "Trips"; header.description = "General information about modal share and trip distributions."; + String[] args = new String[this.groupedRefData == null ? this.args.length: this.args.length + 2]; + System.arraycopy(this.args, 0, args, 0, this.args.length); + + // Add ref data to the argument if set + if (groupedRefData != null) { + args[this.args.length] = "--input-ref-data"; + args[this.args.length + 1] = groupedRefData; + } + Layout.Row first = layout.row("first"); first.el(Plotly.class, (viz, data) -> { viz.title = "Modal split"; @@ -226,5 +245,10 @@ public void configure(Header header, Layout layout) { }); + if (groupedRefData != null) { + + // TODO create the additional tab + + } } }