Demands

OD demand distributions for simulation and empirical data.

class micropurc.demands.ODDistribution(origins, dests, weights)[source]

Bases: object

Distribution over origin-destination pairs.

Variables:
  • origins (numpy.ndarray) – Origin node indices (internal), shape (M,).

  • dests (numpy.ndarray) – Destination node indices (internal), shape (M,).

  • weights (numpy.ndarray) – Sampling probabilities, shape (M,), normalized on construction, so a caller may pass raw counts.

Parameters:
origins: ndarray
dests: ndarray
weights: ndarray
property size: int

Number of OD pairs in the distribution.

sample(rng, n_trips)[source]

Draw n_trips OD pairs with replacement, proportional to the weights.

Returns the sampled origins and destinations, each of shape (n_trips,).

Parameters:
Return type:

tuple[ndarray, ndarray]

micropurc.demands.od_from_csv(network, trips_csv, *, origin_col='origin', dest_col='destination', weight_col='sampling_weight', use_node_index=True)[source]

Build an ODDistribution from a CSV file.

Parameters:
  • network (Network) – Network for mapping raw node IDs.

  • trips_csv (str) – Path to CSV with OD demands.

  • origin_col (str) – Column name for origin node IDs.

  • dest_col (str) – Column name for destination node IDs.

  • weight_col (str | None) – Column holding the weights. When it is None or absent from the file, the OD pairs are weighted equally.

  • use_node_index (bool) – Map the raw IDs through network.node_index. With this off, or with no index on the network, the IDs are cast to int and used as internal indices.

Returns:

The origin-destination distribution parsed from the file.

Raises:

ValueError – If the origin or destination column is absent.

Return type:

ODDistribution

micropurc.demands.od_uniform_all_pairs(network, *, allow_self_trips=False)[source]

Uniform distribution over all OD pairs.

Parameters:
  • network (Network) – Network whose nodes define the OD universe.

  • allow_self_trips (bool) – Keep the pairs whose origin equals their destination.

Returns:

The uniform distribution over the retained OD pairs.

Return type:

ODDistribution