Skip to content

Commit

Permalink
Merge pull request #3648 from LBNL-UCB-STI/alex-vv/3239-improve-walk-…
Browse files Browse the repository at this point in the history
…skim-generation

Do not generate duplicate values for WALK skims
  • Loading branch information
nikolayilyin authored Oct 31, 2022
2 parents 3e901aa + 4b7c752 commit 8885190
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
16 changes: 10 additions & 6 deletions src/main/scala/beam/router/skim/ActivitySimSkimmer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,18 @@ class ActivitySimSkimmer @Inject() (matsimServices: MatsimServices, beamScenario
destination: GeoUnit,
pathType: ActivitySimPathType
): Option[ExcerptData] = {
val individualSkims = timeBin.hours.flatMap { hour =>
getCurrentSkimValue(ActivitySimSkimmerKey(hour, pathType, origin.id, destination.id))
.map(_.asInstanceOf[ActivitySimSkimmerInternal])
}
if (individualSkims.isEmpty) {
if (pathType == ActivitySimPathType.WALK && timeBin != ActivitySimTimeBin.EARLY_AM) {
None
} else {
Some(weightedData(timeBin.toString, origin.id, destination.id, pathType, individualSkims))
val individualSkims = timeBin.hours.flatMap { hour =>
getCurrentSkimValue(ActivitySimSkimmerKey(hour, pathType, origin.id, destination.id))
.map(_.asInstanceOf[ActivitySimSkimmerInternal])
}
if (individualSkims.isEmpty) {
None
} else {
Some(weightedData(timeBin.toString, origin.id, destination.id, pathType, individualSkims))
}
}
}

Expand Down
37 changes: 22 additions & 15 deletions src/main/scala/scripts/BackgroundSkimsCreatorApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,28 @@ import java.nio.file.Path
import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal

/*
Example of parameters usage:
--configPath test/input/beamville/beam.conf
--input test/input/beamville/input.csv
--output test/input/beamville/output.csv
--linkstatsPath test/input/beamville/linkstats.csv
--ODSkimsPath test/input/beamville/odskims.csv
--parallelism 2
Note that all csv files can automatically use gzip compression if specified with `csv.gz` extension
for example "--input test/input/beamville/input.csv.gz"
Run with gradle:
./gradlew execute -PmainClass=scripts.BackgroundSkimsCreatorApp -PappArgs=["'--configPath', 'test/input/beamville/beam-with-fullActivitySimBackgroundSkims.conf', '--output', 'output.csv', '--input', 'input.csv', '--ODSkimsPath', 'ODSkimsBeamville.csv', '--linkstatsPath', '0.linkstats.csv'"]
*/
/**
* This app generates full OD skims
*
* See more details at https://github.com/LBNL-UCB-STI/beam/issues/3193
*
* Example of parameters usage:
* --configPath - path to a config (test/input/beamville/beam.conf)
* --input - (optional) input csv file which contains origin, destination, and mode (test/input/beamville/input.csv)
* --output - the resulting file path (test/input/beamville/output.csv)
* --linkstatsPath - (optional) path to linkstats file (test/input/beamville/linkstats.csv)
* --ODSkimsPath - (optional) path to existing skims file (test/input/beamville/odskims.csv)
* --parallelism 2
*
* If `ODSkimsPath` is provided and contains non-zero values for desired OD pair with desired mode then skims are returned from that file,
* otherwise they will be calculated.
*
* Note that all csv files can automatically use gzip compression if specified with `csv.gz` extension
* for example "--input test/input/beamville/input.csv.gz"
*
* Run with gradle:
* ./gradlew execute -PmainClass=scripts.BackgroundSkimsCreatorApp -PappArgs=["'--configPath', 'test/input/beamville/beam-with-fullActivitySimBackgroundSkims.conf', '--output', 'output.csv', '--input', 'input.csv', '--ODSkimsPath', 'ODSkimsBeamville.csv', '--linkstatsPath', '0.linkstats.csv'"]
*/
object BackgroundSkimsCreatorApp extends App with BeamHelper {

case class InputParameters(
Expand Down
16 changes: 12 additions & 4 deletions src/test/scala/scripts/BackgroundSkimsCreatorAppSpec.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scripts

import akka.actor.ActorSystem
import beam.router.skim.ActivitySimPathType
import beam.router.skim.ActivitySimSkimmer.ExcerptData
import beam.sim.config.{BeamConfig, MatSimBeamConfigBuilder}
import beam.sim.{BeamHelper, BeamServices}
Expand Down Expand Up @@ -55,16 +56,23 @@ class BackgroundSkimsCreatorAppSpec
"run with parameters" in {
whenReady(BackgroundSkimsCreatorApp.runWithServices(beamServices, params)) { _ =>
val csv = GenericCsvReader.readAs[ExcerptData](outputPath.toString, toCsvSkimRow, _ => true)._1.toVector
csv.size shouldBe 15
csv.count(_.weightedTotalTime > 10) shouldBe 10
csv.size shouldBe 11
csv.count(_.weightedTotalTime > 10) shouldBe 6
}
}

"generate all skims if input is not set" in {
whenReady(BackgroundSkimsCreatorApp.runWithServices(beamServices, params.copy(input = None))) { _ =>
val csv = GenericCsvReader.readAs[ExcerptData](outputPath.toString, toCsvSkimRow, _ => true)._1.toVector
csv.size shouldBe 105
csv.count(_.weightedTotalTime > 10) shouldBe 65
csv.size shouldBe 73
csv.count(_.weightedTotalTime > 10) shouldBe 33
}
}

"do not generate duplicate values for WALK skims" in {
whenReady(BackgroundSkimsCreatorApp.runWithServices(beamServices, params.copy(input = None))) { _ =>
val csv = GenericCsvReader.readAs[ExcerptData](outputPath.toString, toCsvSkimRow, _ => true)._1.toVector
csv.filter(_.pathType == ActivitySimPathType.WALK).map(_.timePeriodString).distinct shouldBe Vector("EA")
}
}
}
Expand Down

0 comments on commit 8885190

Please sign in to comment.