-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun_sequencer.py
executable file
·88 lines (72 loc) · 2.62 KB
/
run_sequencer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
"""
Run sequencer
"""
import argparse
import json
import os
import sys
import logging
import sequencer
from sequencer import NetworkPlan, Sequencer
logger = logging.getLogger('sequencer')
logger.info(
"sequencer %s (Python %s)" %
(sequencer.__version__,
'.'.join(map(str, sys.version_info[:3]))))
def load_arguments(value_by_key):
"""
Merge arguments from command line with args from json config
shameless copy from infrastructure-planning
"""
configuration_path = value_by_key.pop('configuration_path')
input_path = value_by_key.pop('input_path')
if configuration_path:
if input_path and not os.path.isabs(configuration_path):
configuration_path = os.path.join(input_path, configuration_path)
g = json.load(open(configuration_path))
else:
g = {}
# Command-line arguments override configuration arguments
for k, v in value_by_key.items():
if v is None:
continue
g[k] = v
# Resolve input paths
if input_path:
for k, v in g.items():
if k.endswith('_path') and \
not k.startswith('output') and \
v and not os.path.isabs(v):
g[k] = os.path.join(input_path, v)
return g
parser = argparse.ArgumentParser(
description="Run sequencer on networkplanner output"
" (metrics csv and network shp)")
parser.add_argument("--configuration_path", "-c",
help="json configuration file")
parser.add_argument("--input_path", "-i",
help="directory which all input paths are relative to")
parser.add_argument("--metrics_path", "-m",
help="metrics csv filename")
parser.add_argument("--network_path", "-n",
help="network shp filename")
parser.add_argument("--demand_field", "-d",
default="Demand...Projected.nodal.demand.per.year",
help="field name in metrics data representing demand")
parser.add_argument("--prioritize_field", "-p",
default="Population",
help="field name in metrics data to select root nodes by")
parser.add_argument("--output_path", "-o",
default=".",
help="directory where all output files will be written")
args = parser.parse_args()
params = load_arguments(args.__dict__)
if 'metrics_path' not in params:
raise Exception("metrics_path parameter is required")
if 'network_path' not in params:
raise Exception("network_path parameter is required")
nwp = NetworkPlan.from_files(params['network_path'], params['metrics_path'], prioritize=params['prioritize_field'])
sequencer = Sequencer(nwp, params['demand_field'])
results = sequencer.sequence()
sequencer.output(params['output_path'])