Skip to content

Commit

Permalink
Fix so that travel distance is closed (#2145)
Browse files Browse the repository at this point in the history
* Fix so that travel distance is closed

* Full fix

* fix order of clean up

* Remove python from tests

* Fmt

* fmt
  • Loading branch information
JustinPihony authored Sep 6, 2019
1 parent 2c71576 commit 72024fb
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/main/resources/beam-template.conf
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ beam.outputs.generalizedLinkStats.endTime = 32400
beam.outputs.defaultWriteInterval = 1
beam.outputs.writePlansInterval = "int | 0"
beam.outputs.writeEventsInterval = "int | 1"
beam.outputs.writeAnalysis = "boolean | true"
beam.outputs.writeLinkTraversalInterval = "int | 0"
beam.physsim.writeEventsInterval = "int | 0"
beam.physsim.writePlansInterval = "int | 0"
Expand Down
22 changes: 12 additions & 10 deletions src/main/scala/beam/sim/BeamSim.scala
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,18 @@ class BeamSim @Inject()(
}

private def writeEventsAnalysisUsing(event: IterationEndsEvent) = {
val writeEventsInterval = beamServices.beamConfig.beam.outputs.writeEventsInterval
val writeEventAnalysisInThisIteration = writeEventsInterval > 0 && event.getIteration % writeEventsInterval == 0
if (writeEventAnalysisInThisIteration) {
val currentEventsFilePath =
event.getServices.getControlerIO.getIterationFilename(event.getServices.getIterationNumber, "events.csv")
val pythonProcess = beam.analysis.AnalysisProcessor.firePythonScriptAsync(
"src/main/python/events_analysis/analyze_events.py",
if ((new File(currentEventsFilePath)).exists) currentEventsFilePath else currentEventsFilePath + ".gz"
)
runningPythonScripts += pythonProcess
if (beamServices.beamConfig.beam.outputs.writeAnalysis) {
val writeEventsInterval = beamServices.beamConfig.beam.outputs.writeEventsInterval
val writeEventAnalysisInThisIteration = writeEventsInterval > 0 && event.getIteration % writeEventsInterval == 0
if (writeEventAnalysisInThisIteration) {
val currentEventsFilePath =
event.getServices.getControlerIO.getIterationFilename(event.getServices.getIterationNumber, "events.csv")
val pythonProcess = beam.analysis.AnalysisProcessor.firePythonScriptAsync(
"src/main/python/events_analysis/analyze_events.py",
if ((new File(currentEventsFilePath)).exists) currentEventsFilePath else currentEventsFilePath + ".gz"
)
runningPythonScripts += pythonProcess
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/beam/sim/config/BeamConfig.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// generated by tscfg 0.9.4 on Wed Sep 04 16:07:43 PDT 2019
// generated by tscfg 0.9.4 on Fri Sep 06 01:02:36 EDT 2019
// source: src/main/resources/beam-template.conf

package beam.sim.config

Expand Down Expand Up @@ -1942,6 +1943,7 @@ object BeamConfig {
generalizedLinkStats: BeamConfig.Beam.Outputs.GeneralizedLinkStats,
generalizedLinkStatsInterval: scala.Int,
stats: BeamConfig.Beam.Outputs.Stats,
writeAnalysis: scala.Boolean,
writeEventsInterval: scala.Int,
writeGraphs: scala.Boolean,
writeLinkTraversalInterval: scala.Int,
Expand Down Expand Up @@ -2001,8 +2003,7 @@ object BeamConfig {
"addTimestampToOutputDirectory"
),
baseOutputDirectory =
if (c.hasPathOrNull("baseOutputDirectory")) c.getString("baseOutputDirectory")
else "/Users/critter/Documents/beam/beam-output/",
if (c.hasPathOrNull("baseOutputDirectory")) c.getString("baseOutputDirectory") else "output",
defaultWriteInterval = if (c.hasPathOrNull("defaultWriteInterval")) c.getInt("defaultWriteInterval") else 1,
displayPerformanceTimings = c.hasPathOrNull("displayPerformanceTimings") && c.getBoolean(
"displayPerformanceTimings"
Expand All @@ -2021,6 +2022,7 @@ object BeamConfig {
if (c.hasPathOrNull("stats")) c.getConfig("stats")
else com.typesafe.config.ConfigFactory.parseString("stats{}")
),
writeAnalysis = !c.hasPathOrNull("writeAnalysis") || c.getBoolean("writeAnalysis"),
writeEventsInterval = if (c.hasPathOrNull("writeEventsInterval")) c.getInt("writeEventsInterval") else 1,
writeGraphs = !c.hasPathOrNull("writeGraphs") || c.getBoolean("writeGraphs"),
writeLinkTraversalInterval =
Expand Down
7 changes: 6 additions & 1 deletion src/test/scala/beam/agentsim/agents/GenericEventsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import beam.sim.config.{BeamConfig, MatSimBeamConfigBuilder}
import beam.sim.population.DefaultPopulationAdjustment
import beam.sim.{BeamHelper, BeamScenario, BeamServices}
import beam.utils.FileUtils
import com.google.inject
import org.matsim.api.core.v01.Scenario
import org.matsim.core.api.experimental.events.EventsManager
import org.matsim.core.events.handler.BasicEventHandler
Expand All @@ -16,6 +17,7 @@ trait GenericEventsSpec extends WordSpecLike with IntegrationSpecCommon with Bea
protected var beamServices: BeamServices = _
protected var eventManager: EventsManager = _
protected var scenario: Scenario = _
private var injector: inject.Injector = _

override def beforeAll(): Unit = {

Expand All @@ -29,7 +31,7 @@ trait GenericEventsSpec extends WordSpecLike with IntegrationSpecCommon with Bea
val scenario = ScenarioUtils.loadScenario(matsimConfig).asInstanceOf[MutableScenario]
scenario.setNetwork(beamScenario.network)

val injector = org.matsim.core.controler.Injector.createInjector(
injector = org.matsim.core.controler.Injector.createInjector(
matsimConfig,
module(baseConfig, beamConfig, scenario, beamScenario)
)
Expand All @@ -43,6 +45,9 @@ trait GenericEventsSpec extends WordSpecLike with IntegrationSpecCommon with Bea
}

override protected def afterAll(): Unit = {
val travelDistanceStats = injector.getInstance(classOf[org.matsim.analysis.TravelDistanceStats])
if (travelDistanceStats != null)
travelDistanceStats.close()
scenario = null
eventManager = null
beamServices = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import com.typesafe.config.Config
import org.matsim.core.utils.misc.Time
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{Matchers, WordSpecLike}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

import scala.collection.JavaConverters._
import scala.util.Random

class RideHailSurgePricingManagerSpec extends WordSpecLike with Matchers with MockitoSugar with BeamHelper {
class RideHailSurgePricingManagerSpec
extends WordSpecLike
with Matchers
with MockitoSugar
with BeamHelper
with BeforeAndAfterAll {

val testConfigFileName = "test/input/beamville/beam.conf"
val config: Config = testConfig(testConfigFileName).resolve()
Expand All @@ -22,6 +27,11 @@ class RideHailSurgePricingManagerSpec extends WordSpecLike with Matchers with Mo
lazy val injector = buildInjector(config, beamExecConfig.beamConfig, scenario, beamScenario)
lazy val beamServices = buildBeamServices(injector, scenario)

override def afterAll(): Unit = {
injector.getInstance(classOf[org.matsim.analysis.TravelDistanceStats]).close()
super.afterAll()
}

"RideHailSurgePricingManager" must {
"be correctly initialized" in {
val surgePricingManager = new RideHailSurgePricingManager(beamServices)
Expand Down
10 changes: 9 additions & 1 deletion src/test/scala/beam/integration/EventsFileSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import beam.sim.BeamHelper
import beam.sim.config.BeamExecutionConfig
import beam.utils.EventReader._
import com.typesafe.config.{Config, ConfigValueFactory}
import com.google.inject
import org.matsim.api.core.v01.Id
import org.matsim.api.core.v01.population.{Activity, Leg, Person}
import org.matsim.core.config.ConfigUtils
Expand All @@ -31,6 +32,7 @@ class EventsFileSpec extends FlatSpec with BeforeAndAfterAll with Matchers with

private var scenario: MutableScenario = _
private var personHouseholds: Map[Id[Person], Household] = _
private var injector: inject.Injector = _

override protected def beforeAll(): Unit = {
val beamExecutionConfig: BeamExecutionConfig = setupBeamWithConfig(config)
Expand All @@ -40,7 +42,7 @@ class EventsFileSpec extends FlatSpec with BeforeAndAfterAll with Matchers with
beamExecutionConfig.matsimConfig
)
scenario = scenarioBuilt
val injector = buildInjector(config, beamExecutionConfig.beamConfig, scenario, beamScenario)
injector = buildInjector(config, beamExecutionConfig.beamConfig, scenario, beamScenario)
val services = buildBeamServices(injector, scenario)

runBeam(services, scenario, beamScenario, scenario.getConfig.controler().getOutputDirectory)
Expand All @@ -51,6 +53,12 @@ class EventsFileSpec extends FlatSpec with BeforeAndAfterAll with Matchers with
.toMap
}

override def afterAll(): Unit = {
val travelDistanceStats = injector.getInstance(classOf[org.matsim.analysis.TravelDistanceStats])
if (travelDistanceStats != null)
travelDistanceStats.close()
}

it should "contain the same bus trips entries" in {
tripsFromEvents("BUS-DEFAULT") should contain theSameElementsAs
tripsFromGtfs(new File("test/input/beamville/r5/bus-freq/trips.txt"))
Expand Down
17 changes: 14 additions & 3 deletions src/test/scala/beam/sflight/CaccSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ import beam.tags.{ExcludeRegular, Periodic}
import beam.utils.FileUtils
import beam.utils.TestConfigUtils.testConfig
import com.typesafe.config.ConfigFactory
import com.google.inject
import org.matsim.core.config.Config
import org.matsim.core.controler.OutputDirectoryHierarchy
import org.matsim.core.scenario.{MutableScenario, ScenarioUtils}
import org.scalatest.{BeforeAndAfterAllConfigMap, Matchers, WordSpecLike}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

import scala.io.Source

class CaccSpec extends WordSpecLike with Matchers with BeamHelper with BeforeAndAfterAllConfigMap {
class CaccSpec extends WordSpecLike with Matchers with BeamHelper with BeforeAndAfterAll {

private var injector: inject.Injector = _

override def afterAll(): Unit = {
val travelDistanceStats = injector.getInstance(classOf[org.matsim.analysis.TravelDistanceStats])
if (travelDistanceStats != null)
travelDistanceStats.close()
super.afterAll()
}

private def runSimulationAndReturnAvgCarTravelTimes(caccEnabled: Boolean, iterationNumber: Int): Double = {
val config = ConfigFactory
Expand All @@ -41,7 +51,8 @@ class CaccSpec extends WordSpecLike with Matchers with BeamHelper with BeforeAnd
val scenario = ScenarioUtils.loadScenario(matsimConfig).asInstanceOf[MutableScenario]
scenario.setNetwork(beamScenario.network)

val services = buildBeamServices(buildInjector(config, beamConfig, scenario, beamScenario), scenario)
injector = buildInjector(config, beamConfig, scenario, beamScenario)
val services = buildBeamServices(injector, scenario)
DefaultPopulationAdjustment(services).update(scenario)

val controller = services.controler
Expand Down
6 changes: 4 additions & 2 deletions src/test/scala/beam/sflight/UrbanSimRunSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import beam.sim.config.{BeamConfig, MatSimBeamConfigBuilder}
import beam.sim.{BeamHelper, BeamServices}
import beam.utils.FileUtils
import beam.utils.TestConfigUtils.testConfig
import com.google.inject
import com.typesafe.config.ConfigValueFactory
import org.matsim.core.scenario.{MutableScenario, ScenarioUtils}
import org.scalatest.{BeforeAndAfterAllConfigMap, ConfigMap, Matchers, WordSpecLike}
Expand Down Expand Up @@ -65,7 +64,7 @@ class UrbanSimRunSpec extends WordSpecLike with Matchers with BeamHelper with Be
listOfPrivateVehicleTypes should not contain ("Car-rh-only")
listOfPrivateVehicleTypes should have size 4

val injector: inject.Injector = buildInjector(conf, beamConfig, scenario, beamScenario)
val injector = buildInjector(conf, beamConfig, scenario, beamScenario)
val services = injector.getInstance(classOf[BeamServices])

val output = scenario.getConfig.controler().getOutputDirectory
Expand All @@ -92,6 +91,9 @@ class UrbanSimRunSpec extends WordSpecLike with Matchers with BeamHelper with Be
.foreach(
itr => exactly(1, itr.list) should endWith(".events.csv").or(endWith(".events.csv.gz"))
)
val travelDistanceStats = injector.getInstance(classOf[org.matsim.analysis.TravelDistanceStats])
if (travelDistanceStats != null)
travelDistanceStats.close()
}
}
}
3 changes: 3 additions & 0 deletions src/test/scala/beam/utils/SimRunnerForTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ trait SimRunnerForTest extends BeamHelper with BeforeAndAfterAll { this: Suite =
}

override protected def afterAll(): Unit = {
val travelDistanceStats = injector.getInstance(classOf[org.matsim.analysis.TravelDistanceStats])
if (travelDistanceStats != null)
travelDistanceStats.close()
beamScenario = null
scenario = null
injector = null
Expand Down
1 change: 1 addition & 0 deletions test/input/beamville/beam.conf
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ beam.outputs.writePlansInterval = ${beam.outputs.defaultWriteInterval}
beam.outputs.writeEventsInterval = ${beam.outputs.defaultWriteInterval}
beam.physsim.writeEventsInterval = ${beam.outputs.defaultWriteInterval}
beam.physsim.writePlansInterval = ${beam.outputs.defaultWriteInterval}
beam.outputs.writeAnalysis = false
beam.outputs.writeLinkTraversalInterval = 0
beam.physsim.linkStatsWriteInterval = 0

Expand Down
1 change: 1 addition & 0 deletions test/input/sf-light/sf-light-0.5k.conf
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ beam.outputs.addTimestampToOutputDirectory = true
beam.outputs.writePlansInterval = 1
beam.outputs.writeEventsInterval = 1
beam.outputs.writeLinkTraversalInterval = 0
beam.outputs.writeAnalysis = false

# The remaining params customize how events are written to output files
beam.outputs.events.fileOutputFormats = "csv.gz,xml.gz,parquet" # valid options: xml(.gz) , csv(.gz), none - DEFAULT: csv.gz
Expand Down
1 change: 1 addition & 0 deletions test/input/sf-light/urbansim-1k.conf
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ beam.outputs.writePlansInterval = 1
beam.outputs.writeEventsInterval = 1
beam.outputs.writeLinkTraversalInterval = 0
beam.outputs.writeSkimsInterval = 1
beam.outputs.writeAnalysis = false

# The remaining params customize how events are written to output files
beam.outputs.events.fileOutputFormats = "csv.gz" # valid options: xml(.gz) , csv(.gz), none - DEFAULT: csv.gz
Expand Down

0 comments on commit 72024fb

Please sign in to comment.