Spaces:
Sleeping
Sleeping
nastasiasnk
commited on
Commit
•
f83432c
1
Parent(s):
3b7be5c
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ def test(input_json):
|
|
13 |
except json.JSONDecodeError:
|
14 |
inputs = json.loads(input_json.replace("'", '"'))
|
15 |
|
16 |
-
# Accessing the
|
17 |
ids_index = inputs['input']['ids_list']
|
18 |
weightsNames = inputs['input']["weights_names"]
|
19 |
|
@@ -27,11 +27,12 @@ def test(input_json):
|
|
27 |
threshold = inputs['input']["threshold"]
|
28 |
threshold = float(threshold)
|
29 |
|
30 |
-
|
31 |
df_matrix = pd.DataFrame(matrix).T
|
32 |
df_weights = pd.DataFrame(weights).T
|
33 |
df_matrix = df_matrix.round(0).astype(int)
|
34 |
df_weights = df_weights.round(0).astype(int)
|
|
|
|
|
35 |
|
36 |
def computeAccessibility (DistanceMatrix,weightsNames, destinationWeights=None,alpha = 0.0038, threshold = 600):
|
37 |
|
@@ -46,32 +47,125 @@ def test(input_json):
|
|
46 |
for columnName in weightsNames:
|
47 |
subdomainsAccessibility[columnName] = (decay_factors * 1).sum(axis=1)
|
48 |
|
49 |
-
#subdomainsAccessibility.drop(columns='commercial', inplace=True)
|
50 |
-
|
51 |
return subdomainsAccessibility
|
52 |
|
53 |
subdomainsAccessibility = computeAccessibility(df_matrix,weightsNames,df_weights,alpha,threshold)
|
54 |
-
|
55 |
-
#
|
56 |
subdomainsAccessibility_dictionary = subdomainsAccessibility.to_dict('index')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
#
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
# Prepare the output
|
65 |
output = {
|
66 |
"subdomainsAccessibility_dictionary": subdomainsAccessibility_dictionary
|
67 |
}
|
68 |
|
69 |
-
"""
|
70 |
-
for key, subdict in output['subdomainsAccessibility_dictionary'].items():
|
71 |
-
ordered = OrderedDict(sorted(subdict.items(), key=lambda x: int(x[0])))
|
72 |
-
output['subdomainsAccessibility_dictionary'][key] = ordered
|
73 |
|
74 |
-
"""
|
75 |
|
76 |
return json.dumps(output)
|
77 |
|
|
|
13 |
except json.JSONDecodeError:
|
14 |
inputs = json.loads(input_json.replace("'", '"'))
|
15 |
|
16 |
+
# Accessing the lists
|
17 |
ids_index = inputs['input']['ids_list']
|
18 |
weightsNames = inputs['input']["weights_names"]
|
19 |
|
|
|
27 |
threshold = inputs['input']["threshold"]
|
28 |
threshold = float(threshold)
|
29 |
|
|
|
30 |
df_matrix = pd.DataFrame(matrix).T
|
31 |
df_weights = pd.DataFrame(weights).T
|
32 |
df_matrix = df_matrix.round(0).astype(int)
|
33 |
df_weights = df_weights.round(0).astype(int)
|
34 |
+
|
35 |
+
|
36 |
|
37 |
def computeAccessibility (DistanceMatrix,weightsNames, destinationWeights=None,alpha = 0.0038, threshold = 600):
|
38 |
|
|
|
47 |
for columnName in weightsNames:
|
48 |
subdomainsAccessibility[columnName] = (decay_factors * 1).sum(axis=1)
|
49 |
|
|
|
|
|
50 |
return subdomainsAccessibility
|
51 |
|
52 |
subdomainsAccessibility = computeAccessibility(df_matrix,weightsNames,df_weights,alpha,threshold)
|
53 |
+
|
54 |
+
# make a dictionary to output in grasshopper / etc
|
55 |
subdomainsAccessibility_dictionary = subdomainsAccessibility.to_dict('index')
|
56 |
+
|
57 |
+
|
58 |
+
def remap(value, B_min, B_max, C_min, C_max):
|
59 |
+
return C_min + (((value - B_min) / (B_max - B_min))* (C_max - C_min))
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
def accessibilityToLivability (DistanceMatrix,subdomainsAccessibility, SubdomainAttributeDict):
|
64 |
+
|
65 |
+
"""
|
66 |
+
Converts accessibility measures into livability scores for various urban subdomains
|
67 |
+
using a specified scaling mechanism based on predefined thresholds and maximum points.
|
68 |
+
|
69 |
+
This function takes a DataFrame of total accessibility per subdomain and remaps these values
|
70 |
+
into livability scores based on thresholds and maximum scores provided in a dictionary.
|
71 |
+
The output DataFrame retains the original order of indices from a reference distance matrix.
|
72 |
+
New columns for combined values such as 'social infrastructure' and 'transportation' are added,
|
73 |
+
aggregating scores from relevant subdomains.
|
74 |
+
|
75 |
+
Parameters:
|
76 |
+
- DistanceMatrix (pd.DataFrame): DataFrame used to maintain the order of indices.
|
77 |
+
- totalAccessibility (pd.DataFrame): DataFrame containing accessibility scores for various subdomains.
|
78 |
+
- SubdomainAttributeDict (dict): Dictionary where each key is a subdomain and each value is a list
|
79 |
+
where the first element is the minimum threshold for good accessibility, and the second element is
|
80 |
+
the maximum livability score for that threshold.
|
81 |
+
|
82 |
+
Returns:
|
83 |
+
- pd.DataFrame: A new DataFrame with the same indices as DistanceMatrix and columns corresponding to
|
84 |
+
totalAccessibility, enhanced with additional columns for combined livability metrics.
|
85 |
+
|
86 |
+
The function processes each subdomain defined in SubdomainAttributeDict. If the accessibility in a
|
87 |
+
subdomain exceeds the threshold, the maximum livability score is assigned. Otherwise, a livability
|
88 |
+
score is calculated based on linear interpolation between 0 and the threshold. Combined metrics
|
89 |
+
for broader categories like 'social infrastructure' are computed by summing up relevant subdomain
|
90 |
+
scores.
|
91 |
+
|
92 |
+
Example:
|
93 |
+
--------
|
94 |
+
# Define the DistanceMatrix and totalAccessibility with example data
|
95 |
+
DistanceMatrix = pd.DataFrame(index=[0, 1, 2])
|
96 |
+
totalAccessibility = pd.DataFrame({'jobs': [100, 150, 200], 'health': [80, 90, 95]}, index=[0, 1, 2])
|
97 |
+
SubdomainAttributeDict = {'jobs': [100, 50], 'health': [80, 40]}
|
98 |
+
|
99 |
+
# Call the function
|
100 |
+
livability_scores = accessibilityToLivability(DistanceMatrix, totalAccessibility, SubdomainAttributeDict)
|
101 |
+
print(livability_scores)
|
102 |
+
|
103 |
+
Notes:
|
104 |
+
------
|
105 |
+
- The function assumes all columns in totalAccessibility are represented in SubdomainAttributeDict unless
|
106 |
+
explicitly handled otherwise within the function.
|
107 |
+
"""
|
108 |
+
|
109 |
+
livability = pd.DataFrame(index=DistanceMatrix.index, columns=subdomainsAccessibility.columns)
|
110 |
+
# livability["Workplaces"] = 0
|
111 |
+
livability.fillna(0, inplace=True)
|
112 |
|
113 |
+
|
114 |
+
# find a set of unique domains, to which subdomains are aggregated
|
115 |
+
|
116 |
+
temp = []
|
117 |
+
|
118 |
+
for key, values in SubdomainAttributeDict.items():
|
119 |
+
domain = SubdomainAttributeDict[key]['domain']
|
120 |
+
for item in domain:
|
121 |
+
if ',' in item:
|
122 |
+
domain_list = item.split(',')
|
123 |
+
SubdomainAttributeDict[key]['domain'] = domain_list
|
124 |
+
for domain in domain_list:
|
125 |
+
temp.append(domain)
|
126 |
+
else:
|
127 |
+
if item != 0:
|
128 |
+
temp.append(item)
|
129 |
+
|
130 |
+
domainsUnique = list(set(temp))
|
131 |
+
|
132 |
+
for domain in domainsUnique:
|
133 |
+
livability[domain] = 0
|
134 |
|
135 |
+
|
136 |
+
# remap accessibility to livability points
|
137 |
+
|
138 |
+
for key, values in SubdomainAttributeDict.items():
|
139 |
+
threshold = float(SubdomainAttributeDict[key]['thresholds'])
|
140 |
+
max_livability = float(SubdomainAttributeDict[key]['max_points'])
|
141 |
+
domain = SubdomainAttributeDict[key]['domain']
|
142 |
+
sqm_per_employee = str(SubdomainAttributeDict[key]['sqmPerEmpl'])
|
143 |
+
|
144 |
+
if key in subdomainsAccessibility.columns:
|
145 |
+
livability_score = remap(subdomainsAccessibility[key], 0, threshold, 0, max_livability)
|
146 |
+
livability.loc[subdomainsAccessibility[key] >= threshold, key] = max_livability
|
147 |
+
livability.loc[subdomainsAccessibility[key] < threshold, key] = livability_score
|
148 |
+
if any(domain):
|
149 |
+
for item in domain:
|
150 |
+
livability.loc[subdomainsAccessibility[key] >= threshold, domain] += max_livability
|
151 |
+
livability.loc[subdomainsAccessibility[key] < threshold, domain] += livability_score
|
152 |
+
|
153 |
+
return livability
|
154 |
+
|
155 |
|
156 |
+
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
|
161 |
+
|
162 |
+
|
163 |
# Prepare the output
|
164 |
output = {
|
165 |
"subdomainsAccessibility_dictionary": subdomainsAccessibility_dictionary
|
166 |
}
|
167 |
|
|
|
|
|
|
|
|
|
168 |
|
|
|
169 |
|
170 |
return json.dumps(output)
|
171 |
|