-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·124 lines (98 loc) · 2.95 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
import argparse
import cProfile
import sys
import pdb
import pstats
from common import configvar, locs
import city_result
parser = argparse.ArgumentParser()
parser.add_argument(
'--pdb', action='store_true', help='drop into a debugger upon exception'
)
parser.add_argument(
'--profile', action='store_true', help='print out performance profile after run'
)
subparsers = parser.add_subparsers()
ALL_MODULES = [
'housing',
'housing_manual',
'climate',
'climate_change',
'walkability',
'bikeability',
'transit',
]
@configvar(return_doc=True, type=lambda x: x)
def factor_modules():
return f"""
Which factors to use in the analysis.
The value is a list of names of Python modules.
Example config:
- housing
- climate
The full list of supported modules is: {ALL_MODULES}.
"""
def compute_results(locs=locs.__dict__.values(), impute=False):
"""
Compute CityResult with the values for each given city * module.
Results are returned sorted from lowest to highest value.
"""
modules = [__import__(name) for name in factor_modules()]
city_results = []
for loc in locs:
result = city_result.CityResult(loc, modules)
result.compute()
city_results.append(result)
if impute:
city_result.impute_missing_values_with_mean(city_results)
city_results.sort()
return city_results
def compute_results_pd(*args, **kwargs):
"""
Similar to compute_results but return results as a Pandas DataFrame.
"""
return city_result.to_pandas(compute_results(*args, **kwargs))
def value_summary(args):
if args.cities == []:
# Hardcoded list of cities that we fully support
cities = locs.__dict__.values()
else:
cities = args.cities
city_results = compute_results(cities, impute=args.impute)
print('All numbers annual benefit (higher is better). Best city first')
for result in reversed(city_results):
result.print()
subparser = subparsers.add_parser(
'summary',
description='Print a summary of the costs of living in the given cities',
)
subparser.set_defaults(func=value_summary)
subparser.add_argument('cities', nargs='*')
subparser.add_argument(
'--impute',
action='store_true',
help='Impute missing/unknown data using the mean of other cities',
)
def main(args):
args = parser.parse_args()
if hasattr(args, 'func'):
args.func(args)
else:
parser.print_help()
sys.exit(1)
if __name__ == '__main__' and not hasattr(sys, 'ps1'):
args = parser.parse_args()
try:
if args.profile:
with cProfile.Profile() as pr:
main(args)
p = pstats.Stats(pr)
p.sort_stats(pstats.SortKey.CUMULATIVE).print_stats(50)
else:
main(args)
except: # noqa: E722
if args.pdb:
pdb.post_mortem()
else:
raise