quetzal.model.preparationmodel module

class quetzal.model.preparationmodel.PreparationModel(*args, **kwargs)[source]

Bases: quetzal.model.model.Model, quetzal.model.cubemodel.cubeModel

preparation_cast_network(nearest_method='nodes', weight='length', penalty_factor=1, speed=3, replace_nodes=False, dumb_cast=False, **nc_kwargs)[source]

Finds a path for the transport lines in the actual road network, to know on which roads a bus line is going, because the public transport links are defined between two stops, without road information. It will evaluate the best combination of nodes in the road network between the two stops. The evaluation is done with the distance. The results will be found as a list of road_links for each public transport link. If the transport network has modes on dedicated infrastructure (train, metro…), create two submodels and use a dumbcast on the dedicated infrastructure modes.

Requires
  • self.nodes

  • self.links

  • self.road_nodes

  • self.road_links

Parameters
  • nearest_method (['nodes'|'links'], optional, default 'nodes') –

    Options are:

    ’nodes’ –(default) looks for the actual nearest node in road_nodes. ‘links’ –looks for the nearest link to a stop in road_links and links the stop to its end_node (b)

  • weight (str, optional, default 'length') – Column of road_links containing road_links length

  • penalty_factor (int, optional, default 1) – Multiplicative penality of weight

  • speed (int, optional, default 3) – Walk speed

  • replace_nodes (bool, optional, default False) – If True replaces nodes by road_nodes. If False, model will use road_to_transit ntlegs

  • dumb_cast (bool, optional, default False) – If True, the network is casted on himself (cast links on links and not on road_links). It will still return the closest road_node for the stops.

  • nodes_checkpoints – mandatory transit nodes

Builds

self.links – add columns road_a, road_b, road_node_list, road_link_list, road_length

preparation_clusterize_nodes(n_clusters=None, adaptive_clustering=False, **kwargs)[source]
Create nodes clusters to optimize computation time.

It will agregate nodes based on their relative distance to build “stop areas”

Requires

self.nodes

Parameters
  • n_clusters (int, optional, default None) – Number of nodes clusters

  • adaptive_clustering (bool, optional, default False) – If True, will define itself the number of clusters. If False n_clusters must be defined

Builds
  • self.links – contain recomputed links with the clusterized nodes

  • self.nodes – contain the clusterized nodes

  • self.disaggregated_nodes – contain the former nodes

preparation_clusterize_zones(max_zones=500, cluster_column=None, is_od_stack=False, **kwargs)[source]

Clusterize zones to optimize computation time.

Requires
  • self.zones

  • self.volumes

Parameters
  • max_zones (int, optional, default 500) – _description_, by

  • cluster_column (string, optional, default None) – cluster column in self.zones if clusters are already defined

  • is_od_stack (bool, optional, default False) – If True, requires table od_stack

Builds
  • self.zones

  • self.volumes

  • self.cluster_series

preparation_drop_redundant_footpaths(access_time='time', log=False)[source]

Reduce number of footpaths to optimize computation by performing a shortest path algorithm in the graph made of footpaths, road_links and road_to_transit.

Requires
  • self.road_links

  • self.road_to_transit

  • self.footpaths

Parameters
  • access_time (str, optional, default 'time') – Time column in road_to_transit and footpaths

  • log (bool, optional, default False) – If true, returns the old and new numbers of footpaths

Builds

self.footpaths – update footpaths dataframe

preparation_drop_redundant_road_to_transit(access_time='time', log=False)[source]

Reduce number of road_to_transit connectors to optimize computation by performing a shortest path algorithm in the graph made of road_links and road_to_transit.

Requires
  • self.road_links

  • self.road_to_transit

Parameters
  • access_time (str, optional, default 'time') – Time column in road_to_transit

  • log (bool, optional, default False) – If true, returns the old and new numbers of road_to_transit

Builds

self.road_to_transit – update road_to_transit dataframe

preparation_drop_redundant_zone_to_road(access_time='time', log=False)[source]

Reduce number of zone_to_road connectors to optimize computation by performing a shortest path algorithm in the graph made of road_links and zone_to_road.

Requires
  • self.road_links

  • self.zone_to_road

Parameters
  • access_time (str, optional, default 'time') – Time column in zone_to_road

  • log (bool, optional, default False) – If true, returns the old and new numbers of zone_to_road

Builds

self.zone_to_road – update zone_to_road dataframe

preparation_drop_redundant_zone_to_transit()[source]

Keeps only relevant zone_to_transit connectors : verifies if the nodes linked to the zones exist in the self.links, and drops redundants

Requires
  • self.links

  • self.zone_to_transit

  • self.zones

Builds

self.zone_to_transit – update zone_to_transit dataframe

preparation_footpaths(road=False, speed=3, max_length=None, n_clusters=None, **kwargs)[source]

Create the footpaths : pedestrian links between stations (nodes), that will allow transfers between stations.

Requires

self.nodes

Parameters
  • road (bool, optional, default False) – If True, compute walk_time on road_links based on parameter speed

  • speed (int, optional, default 3) – Speed of walk on footpaths. Smaller than real walk speed because the footpaths do not follow roads

  • max_length (int, optional, default None) – Maximal length of footpaths

  • n_clusters (int, optional, default None) – Number of nodes clusters : create nodes clusters to optimize computation time. It will agregate nodes based on their relative distance to build “stop areas”

Builds
  • self.footpaths

  • self.road_links – add columns walk_time if road=True

preparation_logit(mode=1, pt_mode=1, pt_path=1, segments=[], time=- 1, price=- 1, transfers=- 1, time_shift=None, route_types=None)[source]

Builds the necessary tables to perform analysis_mode_utility and step_logit. They contain the parameters of the nested logit. For the neste logit we should have 1 >= mode >= pt_mode >= pt_path > 0. If the three of them are equal to 1 the nested logit will be equivalent to a flat logit.

Does not require specific attributes in self.

Parameters
  • mode (int, optional, default 1) – phi parameter used in the logit choice between modes

  • pt_mode (int, optional, default 1) – phi parameter used in the logit choice between pt modes

  • pt_path (int, optional, default 1) – phi parameter used in the logit choice between pt paths

  • segments (list, optional, default []) – Demand segments we want to use in the logit

  • time (int, optional, default -1) – number of utility units by seconds

  • price (int, optional, default -1) – number of utility units by currency unit

  • transfers (int, optional, default -1) – number of utility units by transfer

  • time_shift (int, optional, default None) – Used with timetable (time expanded) models. Number of utility units by time_shift

Builds
  • self.mode_utility – Modal constants, per mode and per segment

  • self.mode_nests – Structure of the nested logit per segment

  • self.logit_scales – Scales of the nested logit per segment (parameters phi)

  • self.utility_values – Utility values of time, price, transfers and time_shift per segment

preparation_map_matching(routing=True, n_neighbors_centroid=100, n_neighbors=10, distance_max=5000, by='trip_id', sequence='link_sequence')[source]

Mapmatch each trip_id in self.links to the road_network (self.road_links)

Parameters
  • routing (bool, optional) – _description_, by default True

  • n_neighbors_centroid (int, optional) – _description_, by default 100

  • n_neighbors (int, optional) – _description_, by default 10

  • distance_max (int, optional) – _description_, by default 5000

  • by (str, optional) – _description_, by default ‘trip_id’

  • sequence (str, optional) – _description_, by default ‘link_sequence’

preparation_map_tracks(agg={'speed': <function PreparationModel.<lambda>>}, buffer=50, smoothing_span=100, *args, **kwargs)[source]

Grand mystère

Requires

self.nodes

Parameters
  • agg (dict, optional) – _description_, by default {‘speed’: lambda s: s.mean() * 3.6}

  • buffer (int, optional) – _description_, by default 50

  • smoothing_span (int, optional) – _description_, by default 100

preparation_ntlegs(short_leg_speed=2, long_leg_speed=10, threshold=1000, n_ntlegs=5, max_ntleg_length=5000, zone_to_transit=True, zone_to_road=False, prefix=False)[source]

Builds the centroids and the non-transit links/legs (ntlegs), also known as connectors. Pameters short_leg_speed and long_leg_speed allow to model diferent types of access to the network (PT/private): for short connectors, the short_leg_speed is used - it represents a walk speed. For long connectors, which will occur for large zones at the edge of the study area, we may want to consider that the access to the network is made by car/taxi, and hence at a larger speed, the long_leg_speed. Function integrates a curve to smoothly go from short_leg_speed to long_leg_speed (can be understood as probability to access by foot or car).

Requires
  • self.nodes

  • self.zones

Parameters
  • short_leg_speed (int, optional, default 2) – Speed of the short legs, in km/h

  • long_leg_speed (int, optional, default 10) – Speed of the short legs, in km/h

  • threshold (int, optional, default 1000) – Threshold for the definition of the short and long legs

  • n_ntlegs (int, optional, default 5) – Number of ntlegs to create per zone (and per type)

  • max_ntleg_length (int, optional, default 5000) – maximal length of the ntlegs, in m

  • zone_to_transit (bool, optional, default True) – True to create links between zones and transit stops (nodes)

  • zone_to_road (bool, optional, default False) – True to create links between zones and road_nodes, and between road_nodes and nodes

  • prefix (bool, optional, default False) – If True, add prefixes to the index of the ntlegs ztt_ (zone_to_transit), ztr_ (zone_to_road), rtt_ (road_to_transit)

Builds
  • self.centroids

  • self.zone_to_transit

  • self.zone_to_road

  • self.road_to_transit

Examples

::
sm.step_ntlegs(

n_ntlegs=5, walk_speed=2, short_leg_speed=3, long_leg_speed=15, threshold=500, max_ntleg_length=5000

)

quetzal.model.preparationmodel.read_hdf(filepath)[source]
quetzal.model.preparationmodel.read_json(folder, **kwargs)[source]