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

Allow xml write/read of additional attributes #118

Merged
merged 39 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
f90faab
add todos
KasiaKoz Jun 1, 2022
2d65b08
refactor additional attrib check method
KasiaKoz Jun 1, 2022
ea45c28
extract/isolate additional attrib save method
KasiaKoz Jun 1, 2022
4750aa0
extract/isolate general attrib save method
KasiaKoz Jun 1, 2022
0ab7118
refactor/generalise general attrib save method
KasiaKoz Jun 1, 2022
ac696c1
allow saving additional attribs for stops
KasiaKoz Jun 1, 2022
17e9c0f
allow saving additional attribs for routes
KasiaKoz Jun 1, 2022
beebfa0
allow saving additional attribs for services
KasiaKoz Jun 1, 2022
96514ad
allow saving additional attribs for schedule
KasiaKoz Jun 1, 2022
d1a7d38
(add missing file) allow saving additional attribs for schedule
KasiaKoz Jun 1, 2022
21ce8f4
Merge branch 'master' into arbitrary-attribs
KasiaKoz Jun 1, 2022
297d504
general functions to prepare and check for optional and required xml …
KasiaKoz Jun 1, 2022
89c8cd0
fix var import oopsie
KasiaKoz Jun 1, 2022
d7d230c
add saving arbitrary additional attributes for network nodes
KasiaKoz Jun 1, 2022
207c84a
tidy up consolidating stop projections
KasiaKoz Jun 6, 2022
29543e5
refactor inputs_handler/outputs_handler
KasiaKoz Jun 6, 2022
6c93c02
refactor test file names inputs_handler/outputs_handler
KasiaKoz Jun 6, 2022
d0b1037
add read additional node attributes from xml
KasiaKoz Jun 6, 2022
ca0dff4
refactor and add additional attrib read placeholders for schedule elems
KasiaKoz Jun 6, 2022
1bfac75
add additional attrib read for stops
KasiaKoz Jun 6, 2022
48c00d7
add additional attrib read for routes
KasiaKoz Jun 6, 2022
4ee1f0e
add additional attrib read for services
KasiaKoz Jun 6, 2022
e4efffc
add additional attrib read for schedule
KasiaKoz Jun 6, 2022
c1919cb
extend additional attrib read for global network attribs
KasiaKoz Jun 6, 2022
ed24443
lint cleanup
KasiaKoz Jun 7, 2022
d1502eb
fix notebook import
KasiaKoz Jun 7, 2022
4c1f39a
more notebook fixes
KasiaKoz Jun 7, 2022
864532a
fix up network level attributes, add read
KasiaKoz Jun 7, 2022
6fcf03b
change to nested dict support for stop/route/service attribute setting
KasiaKoz Jun 7, 2022
c1fd312
tidy up todo
KasiaKoz Jun 7, 2022
7ccf1f7
add attributes as an always present attribute
KasiaKoz Jun 8, 2022
3355426
fix disappearing attribute values post save
KasiaKoz Jun 8, 2022
d7f3974
change simplified tag
KasiaKoz Jun 8, 2022
65def8c
possible fix for pt attributes being read as None
KasiaKoz Jun 17, 2022
29a5bbe
Magical java typing for additional attributes (#124)
KasiaKoz Jun 28, 2022
e68ba63
Merge branch 'master' into arbitrary-attribs
KasiaKoz Jun 28, 2022
e574b60
address PR comments
KasiaKoz Jun 30, 2022
83ee9c6
Merge branch 'master' into arbitrary-attribs
KasiaKoz Jul 1, 2022
e7d979f
Multimodal access egress script (#119)
KasiaKoz Jul 14, 2022
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
360 changes: 180 additions & 180 deletions example_data/example_google_speed_data/api_requests_viz.geojson

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"26997928": ["1", "2", "3", "4"], "546461337": ["998", "999"]}
{"26997928.0": ["1", "2", "3", "4"], "546461337.0": ["998", "999"]}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
osm_id,osm_ref,osm_name
26997928,A400,Charing Cross Road
546461337,A3211,Byward Street
26997928.0,A400,Charing Cross Road
546461337.0,A3211,Byward Street
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
,osm_id,osm_ref,osm_name,network_id,vehicle_type,toll_amount,start_time,end_time
0,26997928,A400,Charing Cross Road,True,type2,2.9,00:00,23:59
1,546461337,A3211,Byward Street,True,type2,2.9,00:00,23:59
0,26997928.0,A400,Charing Cross Road,True,type2,2.9,00:00,23:59
1,546461337.0,A3211,Byward Street,True,type2,2.9,00:00,23:59
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" ?>
<!DOCTYPE roadpricing SYSTEM "http://www.matsim.org/files/dtd/roadpricing_v1.dtd">
<roadpricing type="cordon" name="cordon-toll">
<description>A simple cordon toll scheme</description>
<roadpricing type="link" name="simple-toll">
<description>A simple toll scheme</description>
<links>
<!-- === A3211 === -->
<link id="998">
Expand Down
2 changes: 1 addition & 1 deletion genet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from genet.core import Network # noqa: F401
from genet.schedule_elements import Schedule, Service, Route, Stop # noqa: F401
from genet.use.road_pricing import Toll # noqa: F401
from genet.inputs_handler.read import * # noqa: F401,F403
from genet.input.read import * # noqa: F401,F403
from genet.auxiliary_files import AuxiliaryFile # noqa: F401
from genet.max_stable_set import MaxStableSet # noqa: F401
from genet.exceptions import ScheduleElementGraphSchemaError, RouteInitialisationError, \
Expand Down
2 changes: 1 addition & 1 deletion genet/configs/OSM/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ MODES:

DEFAULT_OSM_TAG_VALUE:
# GeNet will first look at OSM tags to infer the matsim values the link should have. For the types and values head over to
# outputs_handler/matsim_xml_values.py.
# output/matsim_xml_values.py.
car: secondary
bus: secondary
rail: railway
Expand Down
2 changes: 1 addition & 1 deletion genet/configs/OSM/slim_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ MODES:

DEFAULT_OSM_TAG_VALUE:
# GeNet will first look at OSM tags to infer the matsim values the link should have. For the types and values head over to
# outputs_handler/matsim_xml_values.py.
# output/matsim_xml_values.py.
car: secondary
bus: secondary
rail: railway
Expand Down
42 changes: 34 additions & 8 deletions genet/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import genet.modify.change_log as change_log
import genet.modify.graph as modify_graph
import genet.modify.schedule as modify_schedule
import genet.outputs_handler.geojson as geojson
import genet.outputs_handler.matsim_xml_writer as matsim_xml_writer
import genet.outputs_handler.sanitiser as sanitiser
import genet.output.geojson as geojson
import genet.output.matsim_xml_writer as matsim_xml_writer
import genet.output.sanitiser as sanitiser
import genet.schedule_elements as schedule_elements
import genet.utils.dict_support as dict_support
import genet.utils.elevation as elevation
Expand All @@ -37,16 +37,19 @@


class Network:
def __init__(self, epsg):
def __init__(self, epsg, **kwargs):
self.epsg = epsg
self.transformer = Transformer.from_crs(epsg, 'epsg:4326', always_xy=True)
self.graph = nx.MultiDiGraph(name='Network graph', crs=self.epsg, simplified=False)
self.graph = nx.MultiDiGraph(name='Network graph', crs=epsg)
self.attributes = {'crs': epsg}
self.schedule = schedule_elements.Schedule(epsg)
self.change_log = change_log.ChangeLog()
self.auxiliary_files = {'node': {}, 'link': {}}
# link_id_mapping maps between (usually string literal) index per edge to the from and to nodes that are
# connected by the edge
self.link_id_mapping = {}
if kwargs:
self.add_additional_attributes(kwargs)

def __repr__(self):
return f"<{self.__class__.__name__} instance at {id(self)}: with \ngraph: {nx.info(self.graph)} and " \
Expand All @@ -55,9 +58,26 @@ def __repr__(self):
def __str__(self):
return self.info()

def add_additional_attributes(self, attribs: dict):
"""
adds attributes defined by keys of the attribs dictionary with values of the corresponding values
:param attribs: the additional attributes {attribute_name: attribute_value}
:return:
"""
for k, v in attribs.items():
if k not in self.__dict__:
setattr(self, k, v)
else:
logging.warning(
f"{self.__class__.__name__} already has an additional attribute: {k}. "
"Consider overwritting it instead.")

def has_attrib(self, attrib_name):
return attrib_name in self.__dict__

def add(self, other):
"""
This let's you add on `other` genet.Network to the network this method is called on.
This lets you add on `other` genet.Network to the network this method is called on.
This is deliberately not a magic function to discourage `new_network = network_1 + network_2` (and memory
goes out the window)
:param other:
Expand Down Expand Up @@ -243,10 +263,16 @@ def simplify(self, no_processes=1, keep_loops=False):
v not in useless_self_loops}

# mark graph as having been simplified
self.graph.graph["simplified"] = True
self._mark_as_simplified()

def _mark_as_simplified(self):
self.attributes['simplified'] = True

def is_simplified(self):
return self.graph.graph["simplified"]
if 'simplified' in self.attributes:
# range of values for backwards compatibility
return self.attributes['simplified'] in {'true', 'True', True}
return False

def node_attribute_summary(self, data=False):
"""
Expand Down
7 changes: 7 additions & 0 deletions genet/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ class PartialMaxStableSetProblem(Exception):
Raised when the maximum stable set to snap PT to the network is partial - some stops found nothing to snap to
"""
pass


class MalformedAdditionalAttributeError(Exception):
"""
Raised when additional attributes can not be saved to MATSim network
"""
pass
File renamed without changes.
File renamed without changes.
Loading