network_wrangler.TransitNetwork

class network_wrangler.TransitNetwork(feed=None, config=None, shapes_foreign_key=None, stops_foreign_key=None, id_scalar=None)[source]

Bases: object

Representation of a Transit Network.

Typical usage example:

import network_wrangler as wr
stpaul = r'/home/jovyan/work/example/stpaul'
tc=wr.TransitNetwork.read(path=stpaul)
feed

Partridge feed mapping dataframes.

Type:

DotDict

config

Partridge config

Type:

nx.DiGraph

road_net

Associated roadway network object.

Type:

RoadwayNetwork

graph

Graph for associated roadway network object.

Type:

nx.MultiDiGraph

feed_path

Where the feed was read in from.

Type:

str

validated_frequencies

The frequencies have been validated.

Type:

bool

validated_road_network_consistency

The network has been validated against the road network.

shapes_foreign_key

foreign key between shapes dataframe and roadway network nodes.

Type:

str

stops_foreign_key

foreign key between stops dataframe and roadway network nodes.

Type:

str

id_scalar

scalar value added to create new stop and shape IDs when necessary.

Type:

int

REQUIRED_FILES

list of files that the transit network requires.

Type:

list[str]

__init__(feed=None, config=None, shapes_foreign_key=None, stops_foreign_key=None, id_scalar=None)[source]

Constructor

Todo

Make graph a reference to associated RoadwayNetwork’s graph, not its own thing.

Methods

__init__([feed, config, shapes_foreign_key, ...])

Constructor

apply(project_card_dictionary)

Wrapper method to apply a project to a transit network.

apply_python_calculation(pycode[, in_place])

Changes roadway network object by executing pycode.

apply_transit_feature_change(trip_ids, ...)

Changes the transit attributes for the selected features based on the project card information passed

apply_transit_managed_lane(trip_ids, ...[, ...])

check_network_connectivity(shapes_foreign_key)

check if new shapes contain any links that are not in the roadway network

empty()

Create an empty transit network instance using the default config.

read(feed_path[, shapes_foreign_key, ...])

Read GTFS feed from folder and TransitNetwork object.

route_between_nodes(graph, A, B)

find complete path when the new shape has connectivity issue

route_ids_in_routestxt(feed)

Wherever route_id occurs, make sure it is in routes.txt

select_transit_features(selection)

combines multiple selections

select_transit_features_by_nodes(node_ids[, ...])

Selects transit features that use any one of a list of node_ids

set_roadnet(road_net[, graph_shapes, ...])

rtype:

None

shape_ids_in_shapestxt(feed)

Wherever shape_id occurs, make sure it is in shapes.txt

stop_ids_in_stopstxt(feed)

Wherever stop_id occurs, make sure it is in stops.txt

transit_net_to_gdf(transit)

Returns a geodataframe given a TransitNetwork or a valid Shapes DataFrame.

trip_ids_in_tripstxt(feed)

Wherever trip_id occurs, make sure it is in trips.txt

validate_feed(feed, config)

Since Partridge lazily loads the df, load each file to make sure it actually works.

validate_frequencies()

Validates that there are no transit trips in the feed with zero frequencies.

validate_network_keys(feed)

Validates foreign keys are present in all connecting feed files.

validate_road_network_consistencies()

Validates transit network against the road network for both stops and shapes.

validate_transit_shapes()

Validates that all transit shapes are part of the roadway network.

validate_transit_stops()

Validates that all transit stops are part of the roadway network.

write([path, filename])

Writes a network in the transit network standard

Attributes

REQUIRED_FILES

apply(project_card_dictionary)[source]

Wrapper method to apply a project to a transit network.

Parameters:

project_card_dictionary – dict a dictionary of the project card object

apply_python_calculation(pycode, in_place=True)[source]

Changes roadway network object by executing pycode.

Parameters:
  • pycode – python code which changes values in the roadway network object

  • in_place – update self or return a new roadway network object

apply_transit_feature_change(trip_ids, properties, in_place=True)[source]

Changes the transit attributes for the selected features based on the project card information passed

Parameters:
  • trip_ids – pd.Series all trip_ids to apply change to

  • properties – list of dictionaries transit properties to change

  • in_place – bool whether to apply changes in place or return a new network

Returns:

None

apply_transit_managed_lane(trip_ids, node_ids, scalar, in_place=True)[source]
check_network_connectivity(shapes_foreign_key)[source]

check if new shapes contain any links that are not in the roadway network

Return type:

Series

static empty()[source]

Create an empty transit network instance using the default config. :rtype: TransitNetwork

Todo

fill out this method

static read(feed_path, shapes_foreign_key='shape_model_node_id', stops_foreign_key='model_node_id', id_scalar=100000000)[source]

Read GTFS feed from folder and TransitNetwork object.

Return type:

TransitNetwork

Parameters:
  • feed_path – where to read transit network files from.

  • shapes_foreign_key – foreign key between shapes dataframe and roadway network nodes. Will default to SHAPES_FOREIGN_KEY if not provided.

  • stops_foreign_key – foreign key between stops dataframe and roadway network nodes. Will defaul to STOPS_FOREIGN_KEY if not provided.

  • id_scalar – scalar value added to create new stop and shape IDs when necessary. Will default to ID_SCALAR if not provided.

Returns: a TransitNetwork object.

static route_between_nodes(graph, A, B)[source]

find complete path when the new shape has connectivity issue

Return type:

list

static route_ids_in_routestxt(feed)[source]

Wherever route_id occurs, make sure it is in routes.txt

Return type:

Bool

Parameters:

feed – partridge feed object

Returns:

Boolean indicating if feed is okay.

select_transit_features(selection)[source]

combines multiple selections

Return type:

Series

Parameters:

selection – selection dictionary

Returns: trip identifiers : list of GTFS trip IDs in the selection

select_transit_features_by_nodes(node_ids, require_all=False)[source]

Selects transit features that use any one of a list of node_ids

Return type:

Series

Parameters:
  • node_ids – list (generally coming from nx.shortest_path)

  • require_all – bool if True, the returned trip_ids must traverse all of the nodes (default = False)

Returns:

trip identifiers list of GTFS trip IDs in the selection

set_roadnet(road_net, graph_shapes=False, graph_stops=False, validate_consistency=True)[source]
Return type:

None

static shape_ids_in_shapestxt(feed)[source]

Wherever shape_id occurs, make sure it is in shapes.txt

Return type:

Bool

Parameters:

feed – partridge feed object

Returns:

Boolean indicating if feed is okay.

static stop_ids_in_stopstxt(feed)[source]

Wherever stop_id occurs, make sure it is in stops.txt

Return type:

Bool

Parameters:

feed – partridge feed object

Returns:

Boolean indicating if feed is okay.

static transit_net_to_gdf(transit)[source]

Returns a geodataframe given a TransitNetwork or a valid Shapes DataFrame.

Parameters:

transit – either a TransitNetwork or a Shapes GeoDataFrame

Todo

Make more sophisticated.

static trip_ids_in_tripstxt(feed)[source]

Wherever trip_id occurs, make sure it is in trips.txt

Return type:

Bool

Parameters:

feed – partridge feed object

Returns:

Boolean indicating if feed is okay.

static validate_feed(feed, config)[source]

Since Partridge lazily loads the df, load each file to make sure it actually works.

Partridge uses a DiGraph from the networkx library to represent the relationships between GTFS files. Each file is a ‘node’, and the relationship between files are ‘edges’.

Return type:

bool

Parameters:
  • feed – partridge feed

  • config – partridge config

validate_frequencies()[source]

Validates that there are no transit trips in the feed with zero frequencies.

Changes state of self.validated_frequencies boolean based on outcome.

Return type:

bool

Returns:

boolean indicating if valid or not.

static validate_network_keys(feed)[source]

Validates foreign keys are present in all connecting feed files.

Return type:

Bool

Parameters:

feed – partridge feed object

Returns:

Boolean indicating if feed is okay.

validate_road_network_consistencies()[source]

Validates transit network against the road network for both stops and shapes.

Return type:

bool

Returns:

boolean indicating if valid or not.

validate_transit_shapes()[source]

Validates that all transit shapes are part of the roadway network.

Return type:

bool

Returns:

Boolean indicating if valid or not.

validate_transit_stops()[source]

Validates that all transit stops are part of the roadway network.

Return type:

bool

Returns:

Boolean indicating if valid or not.

write(path='.', filename=None)[source]

Writes a network in the transit network standard

Return type:

None

Parameters:
  • path – the path were the output will be saved

  • filename – the name prefix of the transit files that will be generated

REQUIRED_FILES = ['agency.txt', 'frequencies.txt', 'routes.txt', 'shapes.txt', 'stop_times.txt', 'stops.txt', 'trips.txt']