Skip to content

Commit

Permalink
separate TAZ and MAZ network
Browse files Browse the repository at this point in the history
  • Loading branch information
i-am-sijia committed Jun 4, 2024
1 parent 9380449 commit eb07858
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 32 deletions.
2 changes: 1 addition & 1 deletion tm2py/components/demand/commercial.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def emmebank(self):
This should really be in the controller?
Or part of network.skims?
"""
self._emmebank = self.controller.emme_manager.highway_emmebank
self._emmebank = self.controller.emme_manager.highway_taz_emmebank
return self._emmebank

@property
Expand Down
2 changes: 1 addition & 1 deletion tm2py/components/demand/demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(self, controller: RunController):
# @LogStartEnd("prepare highway demand")
def run(self):
"""Open combined demand OMX files from demand models and prepare for assignment."""
self._emmebank_path = self.get_abs_path(self.config.emme.highway_database_path)
self._emmebank_path = self.get_abs_path(self.config.emme.highway_taz_database_path)
self._emmebank = self.controller.emme_manager.emmebank(self._emmebank_path)
self._create_zero_matrix()
for time in self.time_period_names():
Expand Down
4 changes: 2 additions & 2 deletions tm2py/components/demand/prepare_demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def validate_inputs(self):
@property
def highway_emmebank(self):
if self._highway_emmebank == None:
self._highway_emmebank = self.controller.emme_manager.highway_emmebank
self._highway_emmebank = self.controller.emme_manager.highway_taz_emmebank
self._emmebank = self._highway_emmebank
return self._highway_emmebank

Expand Down Expand Up @@ -584,7 +584,7 @@ def num_internal_zones(self):
@property
def num_total_zones(self):
self._emmebank_path = self.controller.get_abs_path(
self.controller.config.emme.highway_database_path
self.controller.config.emme.highway_taz_database_path
)
self._emmebank = self.controller.emme_manager.emmebank(self._emmebank_path)
time_period = self.controller.config.time_periods[0].name
Expand Down
33 changes: 23 additions & 10 deletions tm2py/components/network/create_tod_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def run(self):
# emme_app = self._emme_manager.project(project_path)
# self._emme_manager.init_modeller(emme_app)
with self._setup():
self._create_highway_scenarios()
self._create_highway_scenarios(zone_flag="taz")
self._create_highway_scenarios(zone_flag="maz")
self._create_transit_scenarios()

@_context
Expand Down Expand Up @@ -90,20 +91,32 @@ def _project_coordinates(self, ref_scenario):
emme_app.project.save()

@LogStartEnd("Create highway time of day scenarios.")
def _create_highway_scenarios(self):
emmebank = self.controller.emme_manager.highway_emmebank.emmebank
def _create_highway_scenarios(self, zone_flag = "taz"):
if zone_flag == "taz":
emmebank = self.controller.emme_manager.highway_taz_emmebank.emmebank
else:
emmebank = self.controller.emme_manager.highway_maz_emmebank.emmebank
ref_scenario = emmebank.scenario(
self.controller.config.emme.all_day_scenario_id
)
self._ref_auto_network = ref_scenario.get_network()
n_time_periods = len(self.controller.config.time_periods)
self.controller.emme_manager.highway_emmebank.change_dimensions(
{
"scenarios": 1 + n_time_periods,
"full_matrices": 9999,
"extra_attribute_values": 60000000,
}
)
if zone_flag == "taz":
self.controller.emme_manager.highway_taz_emmebank.change_dimensions(
{
"scenarios": 1 + n_time_periods,
"full_matrices": 9999,
"extra_attribute_values": 60000000,
}
)
else:
self.controller.emme_manager.highway_maz_emmebank.change_dimensions(
{
"scenarios": 1 + n_time_periods,
"full_matrices": 9999,
"extra_attribute_values": 60000000,
}
)
# create VDFs & set cross-reference function parameters
emmebank.extra_function_parameters.el1 = "@free_flow_time"
emmebank.extra_function_parameters.el2 = "@capacity"
Expand Down
2 changes: 1 addition & 1 deletion tm2py/components/network/highway/drive_access_skims.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def process_stops(stops):

def _get_drive_costs(self, period: TimePeriodConfig) -> pd.DataFrame:
"""Load the drive costs from OMX matrix files, return as pandas dataframe."""
emmebank = self.controller.emme_manager.highway_emmebank.emmebank
emmebank = self.controller.emme_manager.highway_taz_emmebank.emmebank
scenario = emmebank.scenario(period.emme_scenario_id)
zone_numbers = scenario.zone_numbers
network = self.controller.emme_manager.get_network(
Expand Down
78 changes: 75 additions & 3 deletions tm2py/components/network/highway/highway_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ def __init__(self, controller: RunController):
self._class_config = None
self._scenario = None
self._highway_emmebank = None
self._highway_maz_scenarios = None
self._highway_maz_emmebank = None

@property
def highway_emmebank(self):
if not self._highway_emmebank:
self._highway_emmebank = self.controller.emme_manager.highway_emmebank
self._highway_emmebank = self.controller.emme_manager.highway_taz_emmebank
return self._highway_emmebank

@property
Expand All @@ -122,6 +124,20 @@ def class_config(self):
self._class_config = {c.name: c for c in self.config.classes}

return self._class_config

@property
def highway_maz_emmebank(self):
if not self._highway_maz_emmebank:
self._highway_maz_emmebank = self.controller.emme_manager.highway_maz_emmebank
return self._highway_maz_emmebank

@property
def highway_maz_scenarios(self):
if self._highway_maz_scenarios is None:
self._highway_maz_scenarios = {
tp: self.highway_maz_emmebank.scenario(tp) for tp in self.time_period_names
}
return self._highway_maz_scenarios

def validate_inputs(self):
"""Validate inputs files are correct, raise if an error is found."""
Expand All @@ -144,7 +160,7 @@ def run(self):
AssignmentClass(c, time, iteration) for c in self.config.classes
]
if iteration > 0:
self._copy_maz_flow(scenario)
self._copy_maz_flow(scenario, time)
else:
self._reset_background_traffic(scenario)
self._create_skim_matrices(scenario, assign_classes)
Expand Down Expand Up @@ -232,16 +248,72 @@ def _setup(self, scenario: EmmeScenario, time_period: str):
self._matrix_cache.clear()
self._matrix_cache = None
self._skim_matrices = []

def _get_maz_links(
self,
time_period: str,
):
"""Create dictionary of link ids mapped to maz network.
Args:
time_period (str): time period abbreviation
"""
_highway_maz_scenario = self.highway_maz_scenarios[time_period]
if not _highway_maz_scenario.has_traffic_results:
return {}
_highway_maz_net = _highway_maz_scenario.get_partial_network(
["LINK"], include_attributes=False
)

highway_attributes = {
"LINK": ["#link_id", "@maz_flow"]
}

def _copy_maz_flow(self, scenario: EmmeScenario):
self.emme_manager.copy_attribute_values(
_highway_maz_scenario, _highway_maz_net, highway_attributes
)

# TODO can we just get the link attributes as a DataFrame and merge them?
maz_link_dict = {
maz_link["#link_id"]: maz_link for maz_link in _highway_maz_net.links()
}

return maz_link_dict

def _copy_maz_flow(self, scenario: EmmeScenario, time_period: str):
"""Copy maz_flow from MAZ demand assignment to ul1 for background traffic.
Args:
scenario: Emme scenario object
time_period (str): time period abbreviation
"""

_highway_net = scenario.get_partial_network(
["LINK"], include_attributes=False
)
highway_attributes = {
"LINK": ["#link_id", "@maz_flow"]
}
self.emme_manager.copy_attribute_values(
scenario, _highway_net, highway_attributes
)

_maz_link_dict = self._get_maz_links(time_period)

self.logger.log(
"Copy @maz_flow to ul1 for background traffic", indent=True, level="DETAIL"
)
for link in _highway_net.links():
if link["#link_id"] in _maz_link_dict.keys():
link["@maz_flow"] = _maz_link_dict[link["#link_id"]]["@maz_flow"]

_update_attributes = {
"LINK": ["@maz_flow"],
}
self.emme_manager.copy_attribute_values(
_highway_net, scenario, _update_attributes
)

net_calc = NetworkCalculator(self.controller, scenario)
net_calc("ul1", "@maz_flow")

Expand Down
11 changes: 8 additions & 3 deletions tm2py/components/network/highway/highway_maz.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(self, controller: RunController):
@property
def highway_emmebank(self):
if self._highway_emmebank is None:
self._highway_emmebank = self.controller.emme_manager.highway_emmebank
self._highway_emmebank = self.controller.emme_manager.highway_maz_emmebank
return self._highway_emmebank

@property
Expand All @@ -122,6 +122,11 @@ def run(self):
county_groups[group.number] = group.counties
for time in self.time_period_names:
self._scenario = self.highway_emmebank.scenario(time)
if self.controller.iteration == 0:
create_attribute = self.controller.emme_manager.tool(
"inro.emme.data.extra_attribute.create_extra_attribute"
)
create_attribute("LINK","@maz_flow","Assigned MAZ-to-MAZ flow",overwrite=True,scenario=self._scenario)
with self._setup(time):
self._prepare_network()
for i, names in county_groups.items():
Expand Down Expand Up @@ -518,7 +523,7 @@ def _assign_flow_text(
)

self.controller.emme_manager.copy_attribute_values(
self._network, self._scenario, {"LINK": ["temp_flow"]}, {"LINK": ["data1"]}
self._network, self._scenario, {"LINK": ["temp_flow"]}, {"LINK": ["@maz_flow"]}
)

def _load_text_format_paths(
Expand Down Expand Up @@ -689,7 +694,7 @@ def __init__(self, controller: RunController):
@property
def highway_emmebank(self):
if self._highway_emmebank is None:
self._highway_emmebank = self.controller.emme_manager.highway_emmebank
self._highway_emmebank = self.controller.emme_manager.highway_maz_emmebank
return self._highway_emmebank

@property
Expand Down
2 changes: 1 addition & 1 deletion tm2py/components/network/highway/highway_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def run(self):
@property
def highway_emmebank(self):
if not self._highway_emmebank:
self._highway_emmebank = self.controller.emme_manager.highway_emmebank
self._highway_emmebank = self.controller.emme_manager.highway_taz_emmebank
return self._highway_emmebank

@property
Expand Down
2 changes: 1 addition & 1 deletion tm2py/components/network/transit/transit_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def transit_emmebank(self):
@property
def highway_emmebank(self):
if not self._highway_emmebank:
self._highway_emmebank = self.controller.emme_manager.highway_emmebank
self._highway_emmebank = self.controller.emme_manager.highway_taz_emmebank
return self._highway_emmebank

@property
Expand Down
6 changes: 4 additions & 2 deletions tm2py/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,8 @@ class EmmeConfig(ConfigItem):
all_day_scenario_id: scenario ID to use for all day
(initial imported) scenario with all time period data
project_path: relative path from run_dir to Emme desktop project (.emp)
highway_database_path: relative path to highway Emmebank
highway_taz_database_path: relative path to TAZ highway Emmebank
highway_maz_database_path: relative path to MAZ highway Emmebank
active_north_database_path: relative paths to active mode Emmebank for north bay
active_south_database_path: relative paths to active mode Emmebank for south bay
transit_database_path: relative path to transit Emmebank
Expand All @@ -1274,7 +1275,8 @@ class EmmeConfig(ConfigItem):

all_day_scenario_id: int
project_path: pathlib.Path
highway_database_path: pathlib.Path
highway_taz_database_path: pathlib.Path
highway_maz_database_path: pathlib.Path
active_north_database_path: pathlib.Path
active_south_database_path: pathlib.Path
transit_database_path: pathlib.Path
Expand Down
24 changes: 17 additions & 7 deletions tm2py/emme/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ def __init__(self, controller, emme_config: "EmmeConfig"):
self.project_path = self.controller.get_abs_path(self.config.project_path)

# see if works without os.path.normcase(os.path.realpath(project_path))
self.highway_database_path = self.controller.get_abs_path(
self.config.highway_database_path
self.highway_taz_database_path = self.controller.get_abs_path(
self.config.highway_taz_database_path
)
self.highway_maz_database_path = self.controller.get_abs_path(
self.config.highway_maz_database_path
)
self.transit_database_path = self.controller.get_abs_path(
self.config.transit_database_path
Expand All @@ -163,7 +166,8 @@ def __init__(self, controller, emme_config: "EmmeConfig"):
self._project = None
self._modeller = None

self._highway_emmebank = None
self._highway_taz_emmebank = None
self._highway_maz_emmebank = None
self._transit_emmebank = None
self._active_north_emmebank = None
self._active_south_emmebank = None
Expand Down Expand Up @@ -221,10 +225,16 @@ def modeller(self) -> EmmeModeller:
return self._modeller

@property
def highway_emmebank(self) -> EmmeBank:
if self._highway_emmebank is None:
self._highway_emmebank = EmmeBank(self, self.highway_database_path)
return self._highway_emmebank
def highway_taz_emmebank(self) -> EmmeBank:
if self._highway_taz_emmebank is None:
self._highway_taz_emmebank = EmmeBank(self, self.highway_taz_database_path)
return self._highway_taz_emmebank

@property
def highway_maz_emmebank(self) -> EmmeBank:
if self._highway_maz_emmebank is None:
self._highway_maz_emmebank = EmmeBank(self, self.highway_maz_database_path)
return self._highway_maz_emmebank

@property
def transit_emmebank(self) -> EmmeBank:
Expand Down

0 comments on commit eb07858

Please sign in to comment.