def sequence_total_grid_mv_line_network(target_folder, infrastructure_graph): drafts_folder = make_folder(join(target_folder, 'drafts')) graph = infrastructure_graph if not graph.edges(): return {} # The network is empty and there is nothing to sequence node_table = get_table_from_graph( graph, ['longitude', 'latitude', 'population', 'peak_demand_in_kw']) node_table = node_table.rename(columns={'longitude': 'X', 'latitude': 'Y'}) node_table_path = join(drafts_folder, 'nodes-sequencer.csv') node_table.to_csv(node_table_path) edge_shapefile_path = join(drafts_folder, 'edges.shp') nwp = NetworkPlan.from_files(edge_shapefile_path, node_table_path, prioritize='population', proj='+proj=longlat +datum=WGS84 +no_defs') model = Sequencer(nwp, 'peak.demand.in.kw') model.sequence() order_series = model.output_frame['Sequence..Far.sighted.sequence'] for index, order in order_series.iteritems(): node_id = model.output_frame['Unnamed..0'][index] graph.node[node_id]['grid_mv_network_connection_order'] = order for node1_id, node2_id, edge_d in graph.cycle_edges(): node1_d = infrastructure_graph.node[node1_id] node2_d = infrastructure_graph.node[node2_id] edge_d['grid_mv_network_connection_order'] = min( node1_d.get('grid_mv_network_connection_order', float('inf')), node2_d.get('grid_mv_network_connection_order', float('inf'))) return {'infrastructure_graph': graph}
def test_sequencer_compare(): """ Test an old output to ensure we don't regress """ input_dir = "data/sumaila/input" csv_file = os.path.join(input_dir, "metrics-local.csv") shp_file = os.path.join(input_dir, "networks-proposed.shp") nwp = NetworkPlan.from_files(shp_file, csv_file, prioritize='Population') model = Sequencer(nwp, 'Demand...Projected.nodal.demand.per.year') model.sequence() expected_dir = "data/sumaila/expected_output" exp_csv_file = os.path.join(expected_dir, "sequenced-results.csv") exp_df = pd.read_csv(exp_csv_file) # exp_shp_file = os.path.join(expected_dir, "edges.shp") # expected_nwp = NetworkPlan(shp_file, exp_csv_file, prioritize='Population') # now compare results to expected #expected_net = expected_nwp.network compare_fields = ['Sequence..Vertex.id', 'Sequence..Far.sighted.sequence'] # exp_node_dict = expected_net.nodes(data=True) # exp_node_tups = [tuple(map(d.get, compare_fields)) for d in exp_node_dict] exp_node_tups = map(tuple, exp_df[compare_fields].values) seq_node_tups = map(tuple, model.output_frame[compare_fields].values) exp_node_tups = filter(lambda tup: tup[0] > 0, exp_node_tups) seq_node_tups = filter(lambda tup: tup[0] > 0, seq_node_tups) seq_node_tups = map(lambda tup: tuple(map(int, tup)), seq_node_tups) # seq_node_tups = [tuple(map(seq_node_dict[d].get, compare_fields)) for d in seq_node_dict] assert sorted(exp_node_tups, key=lambda tup: tup[0]) == \ sorted(seq_node_tups, key=lambda tup: tup[0]),\ "expected nodes do not match sequenced"
def sequence_total_grid_mv_line_network(target_folder, infrastructure_graph): drafts_folder = make_folder(join(target_folder, 'drafts')) graph = infrastructure_graph if not graph.edges(): return {} # The network is empty and there is nothing to sequence node_table = get_table_from_graph(graph, [ 'longitude', 'latitude', 'population', 'peak_demand_in_kw']) node_table = node_table.rename(columns={'longitude': 'X', 'latitude': 'Y'}) node_table_path = join(drafts_folder, 'nodes-sequencer.csv') node_table.to_csv(node_table_path) edge_shapefile_path = join(drafts_folder, 'edges.shp') nwp = NetworkPlan.from_files( edge_shapefile_path, node_table_path, prioritize='population', proj='+proj=longlat +datum=WGS84 +no_defs') model = Sequencer(nwp, 'peak.demand.in.kw') model.sequence() order_series = model.output_frame['Sequence..Far.sighted.sequence'] for index, order in order_series.iteritems(): node_id = model.output_frame['Unnamed..0'][index] graph.node[node_id]['grid_mv_network_connection_order'] = order for node1_id, node2_id, edge_d in graph.cycle_edges(): node1_d = infrastructure_graph.node[node1_id] node2_d = infrastructure_graph.node[node2_id] edge_d['grid_mv_network_connection_order'] = min( node1_d.get('grid_mv_network_connection_order', float('inf')), node2_d.get('grid_mv_network_connection_order', float('inf'))) return {'infrastructure_graph': graph}
def test_sequencer_follows_topology(): """Tests that the sequencer doesn't skip nodes in the network""" nwp = get_network_plan() model = Sequencer(nwp, 'Demand') results = model.sequence() fnodes = results['Sequence..Upstream.id'] node_seq_num = {node: seq_num for seq_num, node in results['Sequence..Vertex.id'].iteritems()} #For each from_node, assert that the sequencer has already pointed to it or its a root eq_(np.all([fnode in nwp.roots or node_seq_num[fnode] < seq_num for seq_num, fnode in fnodes.iteritems()]), True)
def test_sequencer_follows_topology(): """Tests that the sequencer doesn't skip nodes in the network""" nwp = get_network_plan() model = Sequencer(nwp, 'Demand') results = model.sequence() fnodes = results['Sequence..Upstream.id'] node_seq_num = { node: seq_num for seq_num, node in results['Sequence..Vertex.id'].iteritems() } #For each from_node, assert that the sequencer has already pointed to it or its a root eq_( np.all([ fnode in nwp.roots or node_seq_num[fnode] < seq_num for seq_num, fnode in fnodes.iteritems() ]), True)
def test_sequencer_with_fakes(): """ Make sure we work with fake nodes """ # for now, just make sure it runs without exceptions metrics, network, node_rank, edge_rank = gen_data_with_fakes() nwp = NetworkPlan(network, metrics, prioritize='Population', proj='wgs4') model = Sequencer(nwp, 'Demand...Projected.nodal.demand.per.year') results = model.sequence() node_ids = results['Sequence..Vertex.id'] sequence_ids = results['Sequence..Far.sighted.sequence'] actual_node_rank = dict(zip(node_ids, sequence_ids)) actual_edge_rank = {k: v['rank'] for k, v in model.networkplan.network.edge.iteritems()} assert node_rank == actual_node_rank,\ "Node sequencing is not what was expected" assert edge_rank == actual_edge_rank,\ "Edge sequencing is not what was expected"
def test_sequencer_with_fakes(): """ Make sure we work with fake nodes """ # for now, just make sure it runs without exceptions metrics, network, node_rank, edge_rank = gen_data_with_fakes() nwp = NetworkPlan(network, metrics, prioritize='Population', proj='wgs4') model = Sequencer(nwp, 'Demand...Projected.nodal.demand.per.year') results = model.sequence() node_ids = results['Sequence..Vertex.id'] sequence_ids = results['Sequence..Far.sighted.sequence'] actual_node_rank = dict(zip(node_ids, sequence_ids)) actual_edge_rank = { k: v['rank'] for k, v in model.networkplan.network.edge.iteritems() } assert node_rank == actual_node_rank,\ "Node sequencing is not what was expected" assert edge_rank == actual_edge_rank,\ "Edge sequencing is not what was expected"