Skip to content

Commit

Permalink
Table name as parameter. Scenarios from cityIO rather than text files…
Browse files Browse the repository at this point in the history
…. New corktown_dev table.
  • Loading branch information
doorleyr committed Jul 16, 2020
1 parent c96b950 commit 17383e3
Show file tree
Hide file tree
Showing 15 changed files with 136,982 additions and 42 deletions.
60 changes: 32 additions & 28 deletions corktown_offline_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
@author: doorleyr
"""

from toolbox import Indicator, CompositeIndicator
from proximity_indicator import ProxIndicator
from innovation_indicator import InnoIndicator
from economic_indicator import EconomicIndicator
Expand All @@ -15,10 +14,12 @@

import json
import pandas as pd
import urllib



folder='../Scenarios/24_Jun_20/'
table_name='corktown'
host='https://cityio.media.mit.edu/'
cityIO_get_url=host+'api/table/'+table_name

# =============================================================================
# Initialise Indicators
Expand All @@ -34,15 +35,16 @@
# Load contextual data
# =============================================================================

geogrid=json.load(open('{}corktown_geogrid.geojson'.format(folder)))
with urllib.request.urlopen(cityIO_get_url+'/GEOGRID') as url:
geogrid=json.loads(url.read().decode())
cell_area=geogrid['properties']['header']['cellSize']**2

updatable=[((feat['properties']['interactive'])or (feat['properties']['static_new'])
) for feat in geogrid['features']]

types=json.load(open('{}corktown_types.json'.format(folder)))
types=geogrid['properties']['types']

static_types=json.load(open('{}corktown_static_types.json'.format(folder)))
static_types=geogrid['properties']['static_types']

types.update(static_types)

Expand All @@ -55,12 +57,21 @@
ind.geogrid_header=geogrid['properties']['header']

# =============================================================================
# Load the saved land use scenarios
# Load the saved land use scenarios from city_IO
# =============================================================================
geogrid_data_base=json.load(open('{}ford_base.json'.format(folder)))
geogrid_data_campus=json.load(open('{}ford_campus.json'.format(folder)))
geogrid_data_housing=json.load(open('{}ford_housing.json'.format(folder)))
geogrid_data_inno_com=json.load(open('{}ford_inno_com.json'.format(folder)))
# Scenarios are saved as scenrio0, scenario1 etc. on cityIO
# scenario name is contained in the 'info' field
# try 0 to N and save all the results
# Additional scenarios can be created and saved to cityIO using the CityScope interactive front-end
all_scenarios={}
for i in range(10):
try:
with urllib.request.urlopen(cityIO_get_url+'/scenarios'+str(i)) as url:
geogriddata=json.loads(url.read().decode())
all_scenarios[geogriddata['info']['name']]=geogriddata['GEOGRIDDATA']
except:
pass
print('Downloaded {} land use scenarios'.format(len(all_scenarios)))

# =============================================================================
# Define some functions to calculate indicators and land use stats for each scenario
Expand Down Expand Up @@ -118,29 +129,26 @@ def create_scenario_row(all_ind, stats, scenario_name):
if not ind['units']==None:
raw_name+= ' ['+ind['units']+']'
all_cols[raw_name]=ind['raw_value']
for type_name in stats:
all_cols[type_name + ' sqm']=stats[type_name]['sqm']
all_cols[type_name + ' capacity']=stats[type_name]['capacity']
return all_cols

# =============================================================================
# Calculate indicators and land use stats for each scenario
# =============================================================================

base_indicators=get_all_indicators(geogrid_data_base)
base_stats=get_type_stats(geogrid_data_base, reporting_types, updatable,cell_area)
base_indicators=get_all_indicators(all_scenarios['Baseline'])
base_stats=get_type_stats(all_scenarios['Baseline'], reporting_types, updatable,cell_area)

campus_indicators=get_all_indicators(geogrid_data_campus)
campus_stats=get_type_stats(geogrid_data_campus, reporting_types, updatable,cell_area)
campus_indicators=get_all_indicators(all_scenarios['Campus_Only'])
campus_stats=get_type_stats(all_scenarios['Campus_Only'], reporting_types, updatable,cell_area)

campus_mobility_indicators=get_all_indicators(geogrid_data_campus)
campus_mobility_indicators=get_all_indicators(all_scenarios['Campus_Only'])
campus_mobility_stats=campus_stats

housing_indicators=get_all_indicators(geogrid_data_housing)
housing_stats=get_type_stats(geogrid_data_housing, reporting_types, updatable,cell_area)
housing_indicators=get_all_indicators(all_scenarios['Campus_Housing'])
housing_stats=get_type_stats(all_scenarios['Campus_Housing'], reporting_types, updatable,cell_area)

inno_com_indicators=get_all_indicators(geogrid_data_inno_com)
inno_com_stats=get_type_stats(geogrid_data_inno_com, reporting_types, updatable,cell_area)
inno_com_indicators=get_all_indicators(all_scenarios['Innovation_Community'])
inno_com_stats=get_type_stats(all_scenarios['Innovation_Community'], reporting_types, updatable,cell_area)

all_scenarios=[]
all_scenarios.append(create_scenario_row(base_indicators, base_stats, scenario_name='BAU'))
Expand Down Expand Up @@ -175,10 +183,6 @@ def create_scenario_row(all_ind, stats, scenario_name):
col for col in all_scenarios[0] if 'raw' in col]+ [
comp_ind for comp_ind in aggregation]

for type_name in reporting_types:
col_order.append(type_name + ' sqm')
col_order.append(type_name + ' capacity')

output=output[col_order]

output.to_csv('{}scenario_outputs.csv'.format(folder))
output.to_csv('scenario_outputs.csv')
26 changes: 13 additions & 13 deletions listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from proximity_indicator import ProxIndicator
from innovation_indicator import InnoIndicator
from mobility_indicator import MobilityIndicator
from aggregate_indicator import AggregateIndicator
from economic_indicator import EconomicIndicator
from buildings_indicator import BuildingsIndicator
from diversity_indicator import DiversityIndicator
Expand All @@ -12,23 +11,23 @@

from statistics import mean

def main(host_mode='remote'):
reference=json.load(open('./tables/corktown/reference.json'))
def main(host_mode='remote', table_name='corktown_dev'):
reference=json.load(open('./tables/{}/reference.json'.format(table_name)))
if host_mode=='local':
host = 'http://127.0.0.1:5000/'
else:
host = 'https://cityio.media.mit.edu/'
# Individual Indicators
I = InnoIndicator()
P = ProxIndicator(name='proximity', host=host, indicator_type_in='numeric', table_name='corktown')
P_hm = ProxIndicator(name='proximity_heatmap', host=host, indicator_type_in='heatmap', table_name='corktown')
M = MobilityIndicator(name='mobility', table_name='corktown')
B= BuildingsIndicator(name='buildings', host=host,table_name='corktown')
D= DiversityIndicator(name='diversity', table_name='corktown')
P = ProxIndicator(name='proximity', host=host, indicator_type_in='numeric', table_name=table_name)
P_hm = ProxIndicator(name='proximity_heatmap', host=host, indicator_type_in='heatmap', table_name=table_name)
M = MobilityIndicator(name='mobility', table_name=table_name)
B= BuildingsIndicator(name='buildings', host=host,table_name=table_name)
D= DiversityIndicator(name='diversity', table_name=table_name)

# 2nd order individual indicators
E = EconomicIndicator(name='Economic',
table_name='corktown')
table_name=table_name)

for indicator in [
I,
Expand All @@ -37,7 +36,7 @@ def main(host_mode='remote'):
D]:
indicator.viz_type='bar'

H = Handler('corktown', quietly=False, host_mode=host_mode, reference=reference)
H = Handler(table_name, quietly=False, host_mode=host_mode, reference=reference)

H.add_indicators([
I,
Expand Down Expand Up @@ -93,7 +92,8 @@ def main(host_mode='remote'):

if __name__ == '__main__':
if len(sys.argv)>1:
host_mode=sys.argv[1]
main(host_mode=host_mode)
table_name=sys.argv[1]
else:
main()
table_name='corktown_dev'
print('Running for table named {} on city_IO'.format(table_name))
main(table_name=table_name)
Binary file added tables/corktown_dev/fitted_co2_model.p
Binary file not shown.
Binary file added tables/corktown_dev/fitted_pa_model.p
Binary file not shown.
Loading

0 comments on commit 17383e3

Please sign in to comment.