atra package¶
OI-Analytics utility package for Argentina project
Subpackages¶
- atra.analysis package
- Submodules
- atra.analysis.adaptation_analysis module
- atra.analysis.collect_network_hazard_scenarios_national module
- atra.analysis.economic_failure_combine_national module
- atra.analysis.failure_estimation module
- atra.analysis.failure_estimation_bridges module
- atra.analysis.failure_estimation_dnv_flooded_roads module
- atra.analysis.flow_mapping module
- atra.analysis.hazards_network_intersections_results_collect module
- atra.analysis.hazards_networks_intersections module
- atra.analysis.multi_modal_failure_estimation module
- atra.analysis.risk_calculations module
- atra.mrio package
- atra.plot package
- Submodules
- atra.plot.adaptation_sensitvity module
- atra.plot.admin_map module
- atra.plot.air_network_flows module
- atra.plot.ba_hazard_maps module
- atra.plot.bridge_failures module
- atra.plot.bridge_risks module
- atra.plot.bridge_risks_combined module
- atra.plot.bridges module
- atra.plot.bridges_adaptation module
- atra.plot.change_plots module
- atra.plot.climate_change_plots module
- atra.plot.cost_vs_projects module
- atra.plot.hazard_maps module
- atra.plot.national_hazard_exposure_plots module
- atra.plot.national_rail_risks module
- atra.plot.national_rail_risks_combined module
- atra.plot.national_roads_risks module
- atra.plot.national_roads_risks_combined module
- atra.plot.network_air module
- atra.plot.network_rail module
- atra.plot.network_road module
- atra.plot.network_road_rural module
- atra.plot.network_water module
- atra.plot.od_commodities_charts module
- atra.plot.od_commodities_maps module
- atra.plot.plot_ranges module
- atra.plot.population_maps module
- atra.plot.rail_failures_multi_modal module
- atra.plot.rail_network_failures module
- atra.plot.rail_network_flows_max_scales module
- atra.plot.rail_routes module
- atra.plot.risk_changes module
- atra.plot.risk_comparison module
- atra.plot.road_dnv_estimates module
- atra.plot.road_network_failures module
- atra.plot.road_network_flows_max_scales module
- atra.plot.road_tmda_flows_correlations module
- atra.plot.roads_adaptation module
- atra.plot.water_network_flows_max_scales module
- atra.preprocess package
- Submodules
- atra.preprocess.combine_roads module
- atra.preprocess.convert_hazard_data module
- atra.preprocess.multi_modal_network_creation module
- atra.preprocess.network_air module
- atra.preprocess.network_road_topology module
- atra.preprocess.od_combine module
- atra.preprocess.port_od_flows module
- atra.preprocess.rail_od_flows module
- atra.preprocess.road_bridge_matches module
- atra.preprocess.road_network_creation module
- atra.preprocess.road_od_flows module
- atra.preprocess.scrape_wfs module
- atra.stats package
Submodules¶
atra.adaptation_options module¶
Estimate costs and benefits under fixed parameters varying the
cost components, durations of disruptions, GDP growth rates
-
calc_benefits_and_bcr
(x, discount_rates, discount_growth_rates, duration_max=10, min_loss=True, mode='road')[source]¶ Estimate the total cost and benefits for a road segment. This function is used within a pandas apply
Parameters: - x – a row from the road segment dataframe that we are considering
- param_values – numpy array with a set of parameter combinations
- mnt_dis_cost – adaptation costs for a district road in the mountains
- mnt_nat_cost – adaptation costs for a national road in the mountains
- cst_dis_cost – adaptation costs for a district road on flat terrain
- cst_nat_cost – adaptation costs for a national road on flat terrain
- pavement – set of paving combinations. This corresponds with the cost table and the param_values
- mnt_main_cost – maintenance costs for roads in the mountains
- cst_main_cost – maintenance costs for roads on flat terrain
- discount_rates – discount rates to be used for the costs
- discount_growth_rates – discount rates to be used for the losses
- rehab_costs – rehabilitation costs after a disaster
- min_main_dr – discount rates for 4-year periodic maintenance
- max_main_dr – discount rates for 8-year periodic maintenance
- min_exp (bool, optional) – Specify whether we want to use the minimum or maximum exposure length. The default value is set to True
- national (bool, optional) – Specify whether we are looking at national roads. The default value is set to False
- min_loss (bool, optional) – Specify whether we want to use the minimum or maximum economic losses. The default value is set to True
Returns: - uncer_output (list) – outcomes for the initial adaptation costs of this road segment
- tot_uncer_output (list) – outcomes for the total adaptation costs of this road segment
- rel_share (list) – relative share of each factor in the initial adaptation cost of this road segment
- tot_rel_share (list) – relative share of each factor in the total adaptation cost of this road segment
- bc_ratio (list) – benefit cost ratios for this road segment
-
calc_costs
(x, cst_2L_asphalt, cst_2L_concrete, cst_4L_concrete, cst_rehab, cst_routine, cst_periodic, discount_rates, min_main_dr, max_main_dr, mode='road')[source]¶ Estimate the total cost and benefits for a road segment. This function is used within a pandas apply
Parameters: - x – a row from the road segment dataframe that we are considering
- param_values – numpy array with a set of parameter combinations
- mnt_dis_cost – adaptation costs for a district road in the mountains
- mnt_nat_cost – adaptation costs for a national road in the mountains
- cst_dis_cost – adaptation costs for a district road on flat terrain
- cst_nat_cost – adaptation costs for a national road on flat terrain
- pavement – set of paving combinations. This corresponds with the cost table and the param_values
- mnt_main_cost – maintenance costs for roads in the mountains
- cst_main_cost – maintenance costs for roads on flat terrain
- discount_rates – discount rates to be used for the costs
- discount_growth_rates – discount rates to be used for the losses
- rehab_costs – rehabilitation costs after a disaster
- min_main_dr – discount rates for 4-year periodic maintenance
- max_main_dr – discount rates for 8-year periodic maintenance
- min_exp (bool, optional) – Specify whether we want to use the minimum or maximum exposure length. The default value is set to True
- national (bool, optional) – Specify whether we are looking at national roads. The default value is set to False
- min_loss (bool, optional) – Specify whether we want to use the minimum or maximum economic losses. The default value is set to True
Returns: - uncer_output (list) – outcomes for the initial adaptation costs of this road segment
- tot_uncer_output (list) – outcomes for the total adaptation costs of this road segment
- rel_share (list) – relative share of each factor in the initial adaptation cost of this road segment
- tot_rel_share (list) – relative share of each factor in the total adaptation cost of this road segment
- bc_ratio (list) – benefit cost ratios for this road segment
-
calculate_discounting_arrays
(discount_rate=12, growth_rate=2.7, start_year=2016, end_year=2050, min_period=4, max_period=8)[source]¶ Set discount rates for yearly and period maintenance costs
Parameters: - discount_rate – yearly discount rate
- growth_rate – yearly growth rate
Returns: - discount_rate_norm – discount rates to be used for the costs
- discount_rate_growth – discount rates to be used for the losses
- min_main_dr – discount rates for 4-year periodic maintenance
- max_main_dr – discount rates for 8-year periodic maintenance
atra.network module¶
Network representation and utilities
-
class
Network
(nodes=None, edges=None)[source]¶ Bases:
object
A Network is composed of nodes (points in space) and edges (lines)
Parameters: - nodes (geopandas.geodataframe.GeoDataFrame, optional) –
- edges (geopandas.geodataframe.GeoDataFrame, optional) –
-
nodes
¶ Type: geopandas.geodataframe.GeoDataFrame
-
edges
¶ Type: geopandas.geodataframe.GeoDataFrame
-
add_ids
(network, id_col='id', edge_prefix='edge', node_prefix='node', update=False)[source]¶ Add an id column with ascending ids
-
concat_dedup
(dfs)[source]¶ Concatenate a list of GeoDataFrames, dropping duplicate geometries - note: repeatedly drops indexes for deduplication to work
-
d_within
(geom, gdf, distance)[source]¶ Find the subset of a GeoDataFrame within some distance of a shapely geometry
-
intersects
(geom, gdf, tolerance=1e-09)[source]¶ Find the subset of a GeoDataFrame intersecting with a shapely geometry
-
link_nodes_to_edges_within
(network, distance, condition=None, tolerance=1e-09)[source]¶ Link nodes to all edges within some distance
-
link_nodes_to_nearest_edge
(network, condition=None)[source]¶ Link nodes to all edges within some distance
-
matching_gdf_from_geoms
(gdf, geoms)[source]¶ Create a geometry-only GeoDataFrame with column name to match an existing GeoDataFrame
-
nearest_vertex_idx_on_line
(point, line)[source]¶ Return the index of nearest vertex to a point on a line
-
round_geometries
(network, precision=3)[source]¶ Round coordinates of all node points and vertices of edge linestrings to some precision
-
snap_line
(line, points, tolerance=1e-09)[source]¶ Snap a line to points within tolerance, inserting vertices as necessary
-
split_edges_at_nodes
(network, tolerance=1e-09)[source]¶ Split network edges where they intersect node geometries
atra.transport_flow_and_failure_functions module¶
Functions used in the provincial and national-scale network failure analysis
-
combine_hazards_and_network_attributes_and_impacts
(hazard_dataframe, network_dataframe, network_id_column)[source]¶
-
edge_failure_sampling
(failure_scenarios, edge_column)[source]¶ Criteria for selecting failure samples
Parameters: - - Pandas DataFrame of failure scenarios (failure_scenarios) –
- - String name of column to select failed edge ID's (edge_column) –
Returns: Return type: edge_failure_samples - List of lists of failed edge sets
-
igraph_scenario_edge_failures_new
(network_df_in, edge_failure_set, flow_dataframe, edge_flow_path_indexes, path_criteria, tons_criteria, cost_criteria, time_criteria, transport_mode, new_path=True)[source]¶ Estimate network impacts of each failures When the tariff costs of each path are fixed by vehicle weight
Parameters: - - Pandas DataFrame of network (network_df_in) –
- - List of string edge ID's (edge_failure_set) –
- - Pandas DataFrame of list of edge paths (flow_dataframe) –
- - String name of column of edge paths in flow dataframe (path_criteria) –
- - String name of column of path tons in flow dataframe (tons_criteria) –
- - String name of column of path costs in flow dataframe (cost_criteria) –
- - String name of column of path travel time in flow dataframe (time_criteria) –
Returns: edge_failure_dictionary – With attributes edge_id - String name or list of failed edges origin - String node ID of Origin of disrupted OD flow destination - String node ID of Destination of disrupted OD flow no_access - Boolean 1 (no reroutng) or 0 (rerouting) new_cost - Float value of estimated cost of OD journey after disruption new_distance - Float value of estimated distance of OD journey after disruption new_path - List of string edge ID’s of estimated new route of OD journey after disruption new_time - Float value of estimated time of OD journey after disruption
Return type:
-
merge_failure_results
(flow_df_select, failure_df, id_col, tons_col, dist_col, time_col, cost_col)[source]¶ Merge failure results with flow results
Parameters: - flow_df_select (pandas.DataFrame) – edge flow values
- failure_df (pandas.DataFrame) – edge failure values
- tons_col (str) – name of column of tonnages in flow dataframe
- dist_col (str) – name of column of distance in flow dataframe
- time_col (str) – name of column of time in flow dataframe
- cost_col (str) – name of column of cost in flow dataframe
- vehicle_col (str) – name of column of vehicle counts in flow dataframe
- changing_tonnages (bool) –
Returns: flow_df_select – Of edge flow and failure values merged
Return type:
-
network_failure_assembly_shapefiles
(edge_failure_dataframe, gdf_edges, save_edges=True, shape_output_path='')[source]¶ Write results to Shapefiles
Outputs gdf_edges - a Shapefile with results of edge failure dataframe
Parameters:
-
network_od_path_estimations
(graph, source, target, cost_criteria, time_criteria)[source]¶ Estimate the paths, distances, times, and costs for given OD pair
Parameters: - graph – igraph network structure
- source – String/Float/Integer name of Origin node ID
- source – String/Float/Integer name of Destination node ID
- tonnage (float) – value of tonnage
- vehicle_weight (float) – unit weight of vehicle
- cost_criteria (str) – name of generalised cost criteria to be used: min_gcost or max_gcost
- time_criteria (str) – name of time criteria to be used: min_time or max_time
- fixed_cost (bool) –
Returns: - edge_path_list (list[list]) – nested lists of Strings/Floats/Integers of edge ID’s in routes
- path_dist_list (list[float]) – estimated distances of routes
- path_time_list (list[float]) – estimated times of routes
- path_gcost_list (list[float]) – estimated generalised costs of routes
-
rearrange_minmax_values
(edge_failure_dataframe)[source]¶ Write results to Shapefiles
Parameters: edge_failure_dataframe (pandas.DataFrame) – with min-max columns Returns: edge_failure_dataframe – With columns where min < max Return type: pandas.DataFrame
-
spatial_scenario_selection
(network_shapefile, polygon_dataframe, hazard_dictionary, data_dictionary, network_id_column, network_type='nodes')[source]¶ Intersect network edges/nodes and boundary Polygons to collect boundary and hazard attributes
- Parameters
- network_shapefile - Shapefile of edge LineStrings or node Points
- polygon_shapefile - Shapefile of boundary Polygons
- hazard_dictionary - Dictionary of hazard attributes
- data_dictionary - Dictionary of network-hazard-boundary intersection attributes
- network_type - String value -‘edges’ or ‘nodes’ - Default = ‘nodes’
- name_province - String name of province if needed - Default = ‘’
- Outputs
- data_dictionary - Dictionary of network-hazard-boundary intersection attributes:
- edge_id/node_id - String name of intersecting edge ID or node ID
- length - Float length of intersection of edge LineString and hazard Polygon: Only for edges
- province_id - String/Integer ID of Province
- province_name - String name of Province in English
- district_id - String/Integer ID of District
- district_name - String name of District in English
- commune_id - String/Integer ID of Commune
- commune_name - String name of Commune in English
- hazard_attributes - Dictionary of all attributes from hazard dictionary
-
write_flow_paths_to_network_files
(save_paths_df, min_industry_columns, max_industry_columns, gdf_edges, save_csv=True, save_shapes=True, shape_output_path='', csv_output_path='')[source]¶ Write results to Shapefiles
Outputs
gdf_edges
- a shapefile with minimum and maximum tonnage flows of all commodities/industries for each edge of network.Parameters: - save_paths_df – Pandas DataFrame of OD flow paths and their tonnages
- industry_columns – List of string names of all OD commodities/industries indentified
- min_max_exist – List of string names of commodity/industry columns for which min-max tonnage column names already exist
- gdf_edges – GeoDataFrame of network edge set
- save_csv – Boolean condition to tell code to save created edge csv file
- save_shapes – Boolean condition to tell code to save created edge shapefile
- shape_output_path – Path where the output shapefile will be stored
- csv_output_path – Path where the output csv file will be stored
atra.utils module¶
Shared plotting functions
-
class
Style
¶ Bases:
tuple
Style(color, zindex, label): class to hold an element’s styles
Used to generate legend entries, apply uniform style to groups of map elements (See network_map.py for example.)
-
color
¶ Alias for field number 0
-
label
¶ Alias for field number 2
-
zindex
¶ Alias for field number 1
-
-
assign_value_in_area_proportions_within_common_region
(poly_1_gpd, poly_2_gpd, poly_attribute, common_region_id)[source]¶
-
count_points_in_polygon
(x, points_sindex)[source]¶ Count points in a polygon
Parameters: - x – row of dataframe
- points_sindex – spatial index of dataframe with points in the region to consider
Returns: Return type: Number of points in polygon
-
extract_value_from_gdf
(row, gdf_sindex, gdf, column_name)[source]¶ - Inputs are:
- row – row of dataframe gdf_sindex – spatial index of dataframe of which we want to extract the value gdf – GeoDataFrame of which we want to extract the value column_name – column that contains the value we want to extract
- Outputs are:
- extracted value from other gdf
-
gdf_clip
(shape_in, clip_geom)[source]¶ - Inputs are:
- shape_in – path string to shapefile to be clipped
- Outputs are:
- province_geom – shapely geometry of province for what we do the calculation
-
gdf_geom_clip
(gdf_in, clip_geom)[source]¶ Filter a dataframe to contain only features within a clipping geometry
Parameters: - gdf_in – geopandas dataframe to be clipped in
- province_geom – shapely geometry of province for what we do the calculation
Returns: Return type: filtered dataframe
-
generate_weight_bins
(weights, n_steps=9, width_step=0.01, interpolation='linear')[source]¶ Given a list of weight values, generate <n_steps> bins with a width value to use for plotting e.g. weighted network flow maps.
-
get_axes
(extent=(-74.04, -52.9, -20.29, -57.38), epsg=None)[source]¶ Get map axes
Default to Argentina extent // Lambert Conformal projection
-
get_nearest_node
(x, sindex_input_nodes, input_nodes, id_column)[source]¶ Get nearest node in a dataframe
Parameters: - x – row of dataframe
- sindex_nodes – spatial index of dataframe of nodes in the network
- nodes – dataframe of nodes in the network
- id_column – name of column of id of closest node
Returns: Return type: Nearest node to geometry of row
-
line_length
(line, ellipsoid='WGS-84')[source]¶ Length of a line in meters, given in geographic coordinates.
Parameters: - line – a shapely LineString object with WGS-84 coordinates.
- ellipsoid – string name of an ellipsoid that geopy understands (see http://geopy.readthedocs.io/en/latest/#module-geopy.distance).
Returns: Length of line in kilometers.
-
plot_basemap
(ax, data_path, focus='ARG', neighbours=('CHL', 'BOL', 'PRY', 'BRA', 'URY'), country_border='white', plot_regions=True)[source]¶ Plot countries and regions background
-
plot_basemap_labels
(ax, data_path, labels=None, include_regions=False, include_zorder=2)[source]¶ Plot countries and regions background
-
scale_bar
(ax, length=100, location=(0.5, 0.05), linewidth=3)[source]¶ Draw a scale bar
Adapted from https://stackoverflow.com/questions/32333870/how-can-i-show-a-km-ruler-on-a-cartopy-matplotlib-plot/35705477#35705477
Parameters:
-
transform_geo_file
(source_file, sink_file, sink_schema, transform_record)[source]¶ Transform a fiona-readable file
Parameters:
-
voronoi_finite_polygons_2d
(vor, radius=None)[source]¶ Reconstruct infinite voronoi regions in a 2D diagram to finite regions.
Source: https://stackoverflow.com/questions/36063533/clipping-a-voronoi-diagram-python
Parameters: - vor (Voronoi) – Input diagram
- radius (float, optional) – Distance to ‘points at infinity’
Returns: - regions (list of tuples) – Indices of vertices in each revised Voronoi regions.
- vertices (list of tuples) – Coordinates for revised Voronoi vertices. Same as coordinates of input vertices, with ‘points at infinity’ appended to the end