-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAccFromODMatrix_Walk.py
185 lines (129 loc) · 5.34 KB
/
AccFromODMatrix_Walk.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
"""
Create accessibility from OD matrix
Parameters set for pedestrian accessibility
version: 2.0
date: Jan 2020
@author: dvale
"""
# Libraries used
import pandas as pd
import numpy
import os
# Option to make sure that we don't get an alert
# that we are trying to create a number out of a string
pd.options.mode.chained_assignment = None
# Working directory #
print ('Working directory is: ' + os.getcwd()) # get directory
print ('')
##### SET PARAMETERS ######################################
### A) Files to be read ############################
### 1) OD Matrix (with 3 columns: Origin, Destination, TravelCost (can be time, cost, GTC, etc))
OD_Matrix = 'OD_Matrix_Walk.csv' # in this example TravelCost = Walking Distance
### 2) Table with data for opportunities (jobs, hospitals, retail, population, etc)
OppData = 'DestinationData.csv'
### 2.1) Identify Name of column with ID for each destination
DestinationIDColumn = 'Destination_Code'
# Identify Name of column with relevant destination data (eg jobs, hospitals, retail, population)
# This should be integer or float, not a string
DataColumn = 'Residents'
### B) Output file #################################
# 2) Define Output file name (CSV)
OutputFile = 'AccResults_Walk.csv'
### C) Parameters for impedance functions ##########
# 1) Exponential
# Formula: e ^ (beta * Cij)
# define beta
beta = -0.003 # the equivalent to 200 m = 0.500
# 2) Cumulative opportunities
# Formula: 1 if Cij <= MaxValue ; 0 if Cij > MaxValue
# define threshold
MaxCost = 600 # 600 meters
# 3) Gaussian
# Formula: e ^(-1 *((Cij^2)/v))
# define v
v = 57708 # the equivalent to 200 meters = 0.500
# 4) cumulative-Gaussian
# Formula: 1 if Cij <= Acceptable
# e ^(-1 *((Cij^2)/v)) if Cij > Acceptable
# define a
a = 400 # acceptable = 400 meters
# define v
v = 57708 # the equivalent to (a + 200 meters) = 0.500
##### END OF PARAMETERS ###################################
###########################################################
###### CALCULATIONS START HERE ############################
##### 1) Read OD Matrix ###################################
print ('Reading OD matrix...')
# Read OD matrix
df = pd.read_csv(OD_Matrix , index_col=False)
# If Matrix has more than 3 columns, only the first 3 will be used
df = df.iloc[:,0:3]
# We assume that the OD matrix is in fomat Origin, Destination, ODCost (time, money, CO2, etc)
df.columns = ['Origin', 'Destination', 'ODCost']
# Force string on destination field
df['Destination']=df['Destination'].astype(str)
# Force float on ODCost
df['ODCost']=df['ODCost'].astype(float)
print ('OD matrix read!')
print ('It has ' + str(len(df.index)) + ' rows' )
print ('')
##### 2) Calculate impedance function #####################
print ('Calculating impedance function...')
###### 2a) Exponential
# calculate exponential function
df['fCij_exp'] = numpy.exp(beta * df['ODCost']).astype(float)
###### 2b) Cumulative opportunities
# calculate rectangular function
df['fCij_cum'] = 0
df['fCij_cum'].loc[df['ODCost'] <= MaxCost] = 1
###### 2c) Gaussian
#calculate function
df['fCij_gau'] = numpy.exp(-1 * ((df['ODCost']**2) / v)).astype(float)
###### 2d) Cumulative-Gaussian
#calculate function
df['fCij_cumgau'] = 0 #create column
df['fCij_cumgau'].loc[df['ODCost'] > a] = numpy.exp(-1 * (((df['ODCost']-a)**2) / v)).astype(float)
df['fCij_cumgau'].loc[df['ODCost'] <= a] = 1
print ('Impedance functions calculated!')
##### 3) Read Destination Data ############################
print ('')
print ('Reading destination data...')
#### Read table with data for destinations (opportunities)
df_opportunities = pd.read_csv(OppData, index_col=False)
# Rename column and force string
df_opportunities['Destination'] = df_opportunities[DestinationIDColumn].astype(str)
# force float on data column
df_opportunities['OppData'] = df_opportunities[DataColumn].astype(float)
print ('Destination data read!')
print ('It has ' + str(len(df_opportunities.index)) + ' rows' )
##### 4) Join dataframes and calculate accessibility ######
print ('')
print ('Merging tables and calculating accessibility (this might take some time)...')
# merge tables (Join based on destination field)
df = df.merge(df_opportunities, on='Destination')
## Calculate Ojf(Cij)
# exponential
df['Opp_exp'] = df['fCij_exp'] * df[DataColumn]
# cumulative
df['Opp_cum'] = df['fCij_cum'] * df[DataColumn]
# gaussian
df['Opp_gau'] = df['fCij_gau'] * df[DataColumn]
# cumulative-gaussian
df['Opp_cumgau'] = df['fCij_cumgau'] * df[DataColumn]
# Sum opportunities by origin
# sum (Ojf(Cij))
df_Results = df.groupby('Origin')[['Opp_exp', 'Opp_cum', 'Opp_gau', 'Opp_cumgau']].sum()
# Rename accessibility fields
df_Results = df_Results.rename(columns={'Opp_exp': 'Acc_exponential', 'Opp_cum': 'Acc_cumulative', 'Opp_gau': 'Acc_Gaussian', 'Opp_cumgau': 'Acc_CumGaussian'})
print ('Accessibility values calculated!')
print ('Final table has ' + str(len(df_Results.index)) + ' rows' )
##### 5) Save results to csv file #########################
print ('')
print ('Saving output to csv')
# Write csv file
df_Results.to_csv(OutputFile)
print ('')
print('Results saved at ' + os.getcwd())
print('Filename: ' + OutputFile)
##################################################