Skip to content

Commit

Permalink
Integrated the accessibility module
Browse files Browse the repository at this point in the history
  • Loading branch information
doorleyr committed Mar 4, 2020
1 parent 06d1168 commit a28a58d
Show file tree
Hide file tree
Showing 13 changed files with 50,801 additions and 79 deletions.
461 changes: 461 additions & 0 deletions accessibility_module.py

Large diffs are not rendered by default.

91 changes: 66 additions & 25 deletions baseline_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
OSM_CONFIG_FILE_PATH='./osm_amenities.json'
GROUPS_FILE_PATH='./amenity_groups.csv'
TABLE_CONFIG_FILE_PATH='./tables/corktown/table_configs.json'
SIM_AREA_GEOM_PATH='./tables/corktown/table_area.geojson'
SIM_AREA_GEOM_PATH='./tables/corktown/geometry/table_area.geojson'
SIM_ZONES_PATH='./tables/corktown/sim_zones.json'
WAC_PATH='./tables/corktown/mi_wac_S000_JT00_2017.csv.gz'

Expand All @@ -38,7 +38,7 @@
# =============================================================================
host='https://cityio.media.mit.edu/'

table_configs=json.load(open(TABLE_CONFIG_FILE_PATH))[table_name]
table_configs=json.load(open(TABLE_CONFIG_FILE_PATH))
osm_amenities=json.load(open(OSM_CONFIG_FILE_PATH))
amenity_groups=pd.read_csv(GROUPS_FILE_PATH)
amenity_scores=amenity_groups.copy()
Expand Down Expand Up @@ -74,7 +74,7 @@
# Get OSM amenities and census data
# =============================================================================

amenities=get_osm_amenies(bounds_all, amenity_types)
amenities=get_osm_amenies(bounds_all, amenity_types, wgs, projection)

# get the census data
# living, working, job types, housing types
Expand Down Expand Up @@ -124,7 +124,7 @@
amenity_scores['quota']=total_people*amenity_scores['quota_per_k_people']/1000

amenity_scores['num_present']=amenity_scores.apply(lambda row:
amenities[row['sub_sub_cat']], axis=1)
amenities[row['sub_sub_cat']]['count'], axis=1)

# individual density scores
amenity_scores['score']=amenity_scores.apply(lambda row: min(1,row['num_present']/row['quota']), axis=1)
Expand Down Expand Up @@ -162,42 +162,83 @@
income_level_counts=wac_data_full_table[income_level_cols].values
income_level_diversity=shannon_equitability_score(income_level_counts)

indicators=[{'name': 'Residential Density','category': 'Density', 'value': residential_density_score},
density_indicators={'Residential Density': residential_density_score,
'Employment Density': employment_density_score,
'3rd Places Day Density': cat_scores.loc['3rd places Day', 'score'],
'3rd Places Night Density': cat_scores.loc['3rd places Night', 'score'],
'Educational Inst Density': cat_scores.loc['Educational', 'score'],
'Cultural Inst Density': sub_cat_scores.loc['Culture', 'score']}

diversity_indicators={'Residential/Employment Ratio': resi_employ_ratio,
'Educational Inst Diversity': sub_cat_diversity.loc['Educational', 'diversity_score'],
'Cultural Inst Diversity': sub_cat_diversity.loc['Culture', 'diversity_score'],
'Educational Inst Diversity': sub_cat_diversity.loc['Educational', 'diversity_score'],
'3rd Places Diversity': cat_diversity.loc['3rd places Day', 'diversity_score'],
'Job Type Diversity':job_type_diversity,
'Income Level Diversity': income_level_diversity}

proximity_indicators={'Proximity to {}'.format(prox_ind): 0.5 for prox_ind in table_configs['scalers']}

mobility_indicators={'Mobility Efficiency': 0.5,
'Mobility Embodied Energy': 0.5}

buildings_indicators={'Buildings Efficiency': 0.5,
'Buildings Embodied Energy': 0.5}


indicators={'density': density_indicators,
'diversity': diversity_indicators,
'proximity': proximity_indicators,
'buildings': buildings_indicators,
'mobility': mobility_indicators}
# =============================================================================
# Save results
# =============================================================================

amenity_scores.to_csv('tables/corktown/amenity_scores.csv', index=False)

json.dump(indicators, open(BASELINE_INDICATORS_PATH, 'w'))

host='https://cityio.media.mit.edu/'
cityIO_output_path=host+'api/table/update/'+table_name

#r = requests.post(cityIO_output_path+'/density_indicators', data = json.dumps(density_indicators))
#print(r)
#r = requests.post(cityIO_output_path+'/diversity_indicators', data = json.dumps(diversity_indicators))
#print(r)
#r = requests.post(cityIO_output_path+'/proximity_indicators', data = json.dumps(proximity_indicators))
#print(r)
#r = requests.post(cityIO_output_path+'/mobility_indicators', data = json.dumps(mobility_indicators))
#print(r)


indicators=[
{'name': 'Residential Density','category': 'Density', 'value': residential_density_score},
{'name': 'Employment Density','category': 'Density', 'value': employment_density_score},
{'name': '3rd Places Day Density','category': 'Density', 'value': cat_scores.loc['3rd places Day', 'score']},
{'name': '3rd Places Night Density','category': 'Density', 'value': cat_scores.loc['3rd places Night', 'score']},
{'name': 'Educational Inst Density','category': 'Density', 'value': cat_scores.loc['Educational', 'score']},
{'name': 'Cultural Inst Density','category': 'Density', 'value': sub_cat_scores.loc['Culture', 'score']},
{'name': 'Cultural Inst Diversity','category': 'Diversity', 'value': sub_cat_diversity.loc['Culture', 'diversity_score']},
{'name': 'Residential/Employment Ratio','category': 'Diversity', 'value': resi_employ_ratio},
{'name': 'Educational Inst Diversity','category': 'Diversity', 'value': sub_cat_diversity.loc['Educational', 'diversity_score']},
{'name': '3rd Places Diversity','category': 'Diversity', 'value': cat_diversity.loc['3rd places Day', 'diversity_score']},
{'name': 'Job Type Diversity','category': 'Diversity', 'value': job_type_diversity},
{'name': 'Income Level Diversity','category': 'Diversity', 'value': income_level_diversity}]
# {'name': 'Cultural Inst Diversity','category': 'Diversity', 'value': sub_cat_diversity.loc['Culture', 'diversity_score']},
# {'name': 'Residential/Employment Ratio','category': 'Diversity', 'value': resi_employ_ratio},
# {'name': 'Educational Inst Diversity','category': 'Diversity', 'value': sub_cat_diversity.loc['Educational', 'diversity_score']},
# {'name': '3rd Places Diversity','category': 'Diversity', 'value': cat_diversity.loc['3rd places Day', 'diversity_score']},
# {'name': 'Job Type Diversity','category': 'Diversity', 'value': job_type_diversity},
# {'name': 'Income Level Diversity','category': 'Diversity', 'value': income_level_diversity}
]


# =============================================================================
# Random values
# =============================================================================
import random
for prox_ind in ['Employment','Education', 'Housing', '3rd Places', 'Parks', 'Healthcare']:
for prox_ind in table_configs['scalers']:
indicators.append({'name': 'Proximity to {}'.format(prox_ind),
'category': 'Proximity', 'value': random.random()})
for energy_ind in ['Buildings','Mobility']:
for energy_ind in ['Mobility']:
indicators.append({'name': '{} Energy Efficiency'.format(energy_ind),
'category': 'Energy', 'value': random.random()})
indicators.append({'name': '{} Embodied Energy'.format(energy_ind),
'category': 'Energy', 'value': random.random()})
# =============================================================================
# Save results
# =============================================================================
amenity_scores.to_csv('tables/corktown/amenity_scores.csv', index=False)


host='https://cityio.media.mit.edu/'
cityIO_output_path=host+'api/table/update/'+table_name

r = requests.post(cityIO_output_path+'/indicators', data = json.dumps(indicators))
print(r)

json.dump(indicators, open(BASELINE_INDICATORS_PATH, 'w'))
print(r)
82 changes: 62 additions & 20 deletions indicators_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,29 @@ def initialise():
Steps that only need to be performed once when the model starts running
"""
print('Initialising')
global geogrid, amenity_scores, basic_stats, baseline_indicators
global geogrid, amenity_scores, basic_stats, baseline_indicators, indicators
with urllib.request.urlopen(cityIO_get_url+'/GEOGRID') as url:
#get the GEOGRID from cityI/O
geogrid=json.loads(url.read().decode())
amenity_scores=pd.read_csv(AMENITY_SCORES_PATH)
baseline_indicators=json.load(open(BASELINE_INDICATORS_PATH))
indicators=baseline_indicators.copy()
basic_stats=json.load(open(BASIC_STATS_PATH))



def perform_updates(output_name, geogrid_data):
def perform_grid_updates(geogrid_data):
"""
Steps that take place every time a change is detected in the
city_io grid data
"""
print('Performing updates')

global indicators
residents=basic_stats['residents']
employees=basic_stats['employees']
indicators=baseline_indicators.copy()

# density_indicators=indicators['density']
# diversity_indicators=indicators['diversity']

for cell in geogrid_data:
if 'Residential' in cell['name']:
Expand All @@ -67,9 +70,27 @@ def perform_updates(output_name, geogrid_data):
ind['value']=employment_density_score
elif ind['name']=='Residential/Employment Ratio':
ind['value']=min(residents, employees)/max(residents, employees)
r = requests.post(cityIO_post_url+'/'+output_name, data = json.dumps(indicators))
print(r)

r = requests.post(cityIO_post_url+'/indicators', data = json.dumps(indicators))
print('Density Indicators: {}'.format(r))

# density_indicators['Residential Density']=residential_density_score
# density_indicators['Employment Density']=employment_density_score
# diversity_indicators['Residential/Employment Ratio']=min(residents, employees)/max(residents, employees)

# r = requests.post(cityIO_post_url+'/indicators2/density', data = json.dumps(density_indicators))
# print('Density Indicators: {}'.format(r))
# r = requests.post(cityIO_post_url+'/indicators2/diversity', data = json.dumps(diversity_indicators))
# print('DiversityIndicators: {}'.format(r))

def perform_access_updates(ind_access):
global indicators
for poi in ind_access:
for ind in indicators:
if ind['name']=='Proximity to {}'.format(poi):
ind['value']=min(1, ind_access[poi])
r = requests.post(cityIO_post_url+'/indicators', data = json.dumps(indicators))
print('Accessibility: {}'.format(r))

# INPUTS
AMENITY_SCORES_PATH='tables/{}/amenity_scores.csv'.format(table_name)
Expand All @@ -83,7 +104,7 @@ def perform_updates(output_name, geogrid_data):
cityIO_post_url=host+'api/table/update/'+table_name


SLEEP_TIME=0.1 # seconds to sleep between checkinh cityI/O
SLEEP_TIME=0.1 # seconds to sleep between checking cityI/O

PEOPLE_PER_RESI_BLD=200
PEOPLE_PER_OFFICE_BLD=200
Expand All @@ -94,24 +115,45 @@ def perform_updates(output_name, geogrid_data):
# =============================================================================
# Update Loop
# =============================================================================
lastId=0
last_grid_hash_Id=0
last_access_hash_id=0
while True:
#check if grid data changed
sleep(SLEEP_TIME)
# =============================================================================
# check if grid data changed and perform updates if so
# =============================================================================
try:
with urllib.request.urlopen(cityIO_get_url+'/meta/hashes/GEOGRIDDATA') as url:
hash_id=json.loads(url.read().decode())
with urllib.request.urlopen(cityIO_get_url+'/meta/hashes') as url:
hashes=json.loads(url.read().decode())
grid_hash_id=hashes['GEOGRIDDATA']
access_hash_id=hashes['ind_access']
except:
print('Cant access cityIO GEOGRIDDATA hash')
hash_id=1
if hash_id==lastId:
sleep(SLEEP_TIME)
print('Cant access cityIO hashes')
grid_hash_id=1
access_hash_id=1
sleep(1)
if grid_hash_id==last_grid_hash_Id:
pass
else:
try:
with urllib.request.urlopen(cityIO_get_url+'/GEOGRIDDATA') as url:
geogrid_data=json.loads(url.read().decode())
perform_updates('indicators', geogrid_data)
lastId=hash_id
perform_grid_updates(geogrid_data)
last_grid_hash_Id=grid_hash_id
except:
print('Got city_IO GEOGRIDDATA hash but couldnt get data')
sleep(SLEEP_TIME)

print('Cant access GEOGRID data')
sleep(1)
# =============================================================================
# check if accessibility data changed and perform updates if so
# =============================================================================
if access_hash_id==last_access_hash_id:
pass
else:
try:
with urllib.request.urlopen(cityIO_get_url+'/ind_access') as url:
ind_access_data=json.loads(url.read().decode())
perform_access_updates(ind_access_data)
last_access_hash_id=access_hash_id
except:
print('Cant access ind_access data')
sleep(1)
23 changes: 16 additions & 7 deletions osm_amenities.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"amenity=bar",
"amenity=pub",
"amenity=biergarten",
"amenity=nightclub"
"amenity=nightclub"
],
"groceries": [
"shop=convenience",
Expand Down Expand Up @@ -56,14 +56,14 @@
],
"hotels": [
"amenity=hotel",
"tourism=hotel"
"tourism=hotel"
],
"libraries": [
"amenity=library"
],
"museums": [
"tourism=museum",
"museum=*"
"museum=*"
],
"cinema": [
"amenity=cinema"
Expand All @@ -72,7 +72,7 @@
"shop=books"
],
"art-gallery": [
"tourism=gallery"
"tourism=gallery"
],
"schools": [
"amenity=school",
Expand All @@ -83,18 +83,27 @@
],
"kindergarten": [
"amenity=kindergarten",
"amenity=preschool"
"amenity=preschool"
],
"further_education": [
"amenity=college"
],
"education": [
"amenity=university",
"amenity=kindergarten",
"amenity=preschool",
"amenity=college"
],
"sports_facilities": [
"sport=*",
"leisure=stadium"
]
,
],
"sports_shop": [
"shop=sports"
],
"parks": [
"leisure=park",
"amenity=park"
]
}
}
Loading

0 comments on commit a28a58d

Please sign in to comment.