Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import matplotlib.pyplot as plt
  4. import geopandas as gpd
  5. import pandas as pd
  6.  
  7. import geopandas as gpd
  8. import numpy as np
  9.  
  10. from fiona.crs import from_epsg
  11.  
  12. import psycopg2
  13.  
  14. plt.switch_backend('agg')
  15.  
  16. cen_con = psycopg2.connect(database = "census", user = "", password = "",
  17. host = "", port = 5432)
  18.  
  19. geo10 = gpd.read_postgis("SELECT geoid, geometry FROM PlotTract10();",
  20. geom_col = "geometry", con = cen_con, crs = from_epsg(2163))
  21.  
  22.  
  23. w2 = {0 : 1, 1 : 0.68, 2 : 0.22}
  24. def t_to_w2(t, scale = 1.0):
  25.  
  26. v = int(t / 10. / scale)
  27. if v > 2: return 0
  28.  
  29. return w2[v]
  30.  
  31. w3 = {1 : 0.962, 2 : 0.704, 3 : 0.377, 4 : 0.042}
  32. def t_to_w3(t, scale = 1.0):
  33.  
  34. v = int(t / 10. / scale) + 1
  35. if v > 6: return 0
  36. if v > 4: v = 4
  37.  
  38. return w3[v]
  39.  
  40.  
  41. def map_format(ax, on = False):
  42.  
  43. plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)
  44. plt.margins(0,0)
  45. ax.xaxis.set_major_locator(plt.NullLocator())
  46. ax.yaxis.set_major_locator(plt.NullLocator())
  47.  
  48. if not on:
  49. ax.set_axis_off()
  50. ax.set_axis_on()
  51. for a in ["bottom", "top", "right", "left"]:
  52. ax.spines[a].set_linewidth(0)
  53.  
  54. return ax
  55.  
  56.  
  57.  
  58. # Data directory.
  59.  
  60. data = "data/"
  61.  
  62.  
  63. # Load populations. Merge these together.
  64.  
  65. doc = pd.read_csv(data + "doc.csv", dtype = {"geoid" : int, "supply" : int, "region" : int})
  66. doc.rename(columns = {"region" : "destination_region"}, inplace = True)
  67.  
  68. pop = pd.read_csv(data + "pop.csv", dtype = {"geoid" : int, "pop" : int, "region" : int})
  69. pop.rename(columns = {"region" : "origin_region"}, inplace = True)
  70.  
  71. pop = pd.merge(doc, pop)
  72. pop.rename(columns = {"pop" : "D", "supply" : "S"}, inplace = True)
  73.  
  74.  
  75. # Create the times dataframe.
  76.  
  77. times = pd.read_csv(data + "times.csv", dtype = {'origin': int, 'destination': int, 'cost': float})
  78.  
  79.  
  80. # Ensure self-times of 0.
  81.  
  82. loc = list(pop[pop.D > 0].geoid.unique())
  83. self = pd.DataFrame({"origin" : loc, "destination" : loc, "cost" : [0] * len(loc)})
  84.  
  85. times = pd.concat([self, times], sort = False)
  86.  
  87.  
  88. # Merge the files
  89.  
  90. times = times.merge(pop[["geoid", "D", "origin_region"]] .rename(columns = {"geoid" : "origin"}), how = "left")
  91. times = times.merge(pop[["geoid", "S", "destination_region"]].rename(columns = {"geoid" : "destination"}), how = "left")
  92.  
  93. times["in_region"] = (times.origin_region == times.destination_region).astype(int)
  94. times.drop(["origin_region", "destination_region"], axis = 1, inplace = True)
  95.  
  96.  
  97. # Convert time costs to weights.
  98.  
  99. times["W2"] = times.cost.apply(t_to_w2)
  100. times["W3"] = times.cost.apply(t_to_w3)
  101.  
  102.  
  103. # Sum the weights and get the preferences; merge them.
  104.  
  105. W3sum = times[["origin", "W3"]].groupby("origin").sum().rename(columns = {"W3" : "W3sum"}).reset_index()
  106.  
  107. times = pd.merge(times, W3sum)
  108. times["G"] = times.W3 / times.W3sum
  109.  
  110.  
  111. # Get the total demand in an office location; merge it.
  112.  
  113. times["D2tot"] = times.W2 * times.D
  114. times["D3tot"] = times.W3 * times.D * times.G
  115.  
  116. demand_at_dest = times[["destination", "D2tot", "D3tot"]].groupby("destination").sum().reset_index()
  117. demand_at_dest = pd.merge(demand_at_dest, pop[["geoid", "S"]],
  118. left_on = "destination", right_on = "geoid", how = "left")
  119.  
  120. # Get the supply to demand ratio, at the location; merge it.
  121.  
  122. demand_at_dest["R2"] = demand_at_dest.S / demand_at_dest.D2tot
  123. demand_at_dest["R3"] = demand_at_dest.S / demand_at_dest.D3tot
  124. times = pd.merge(times, demand_at_dest[["destination", "R2", "R3"]])
  125.  
  126.  
  127. # Calculate the total supply per location.
  128.  
  129. times["fca2"] = times.W2 * times.R2
  130. times["fca2_ir"] = times.W2 * times.R2 * times.in_region
  131.  
  132. times["fca3"] = times.G * times.W3 * times.R3
  133. times["fca3_ir"] = times.G * times.W3 * times.R3 * times.in_region
  134.  
  135.  
  136. # Sum the supply to get the region's access.
  137.  
  138. fca = times[["origin", "fca2", "fca2_ir", "fca3", "fca3_ir"]].groupby("origin").sum().reset_index()
  139.  
  140. fca["in_region_2"] = fca["fca2_ir"] / fca["fca2"]
  141. fca.loc[fca["fca2"] == 0, "in_region_2"] = 0
  142.  
  143. fca["in_region_3"] = fca["fca3_ir"] / fca["fca3"]
  144. fca.loc[fca["fca3"] == 0, "in_region_3"] = 0
  145.  
  146. fca.drop(["fca2_ir", "fca3_ir"], axis = 1, inplace = True)
  147.  
  148. fca = pd.merge(fca, pop[["geoid", "D"]].copy().rename(columns = {"geoid" : "origin", "D" : "pop"}))
  149.  
  150.  
  151. # Make the normalized access
  152.  
  153. mean_access = (fca["fca2"] * fca["pop"]).sum() / fca["pop"].sum()
  154. fca["fca2_norm"] = fca["fca2"] / mean_access
  155.  
  156. mean_access = (fca["fca3"] * fca["pop"]).sum() / fca["pop"].sum()
  157. fca["fca3_norm"] = fca["fca3"] / mean_access
  158.  
  159.  
  160. # Save for posterity...
  161.  
  162. fca.to_csv("fca.csv", index = False)
  163.  
  164. geo_fca = pd.merge(geo10, fca, left_on = "geoid", right_on = "origin")
  165.  
  166. ax = geo_fca.plot(column = "fca2_norm", cmap = "coolwarm_r", vmin = 0, vmax = 2)
  167. map_format(ax)
  168. ax.figure.savefig("js_2sfca.png", dpi = 1200)
  169.  
  170.  
  171. ax = geo_fca.plot(column = "in_region_2", cmap = "coolwarm_r", legend = True, vmin = 0, vmax = 1)
  172.  
  173. map_format(ax)
  174. ax.figure.savefig("js_2sfca_ir.png", dpi = 1200)
  175.  
  176.  
  177. ax = geo_fca.plot(column = "fca3_norm", cmap = "coolwarm_r", vmin = 0, vmax = 2)
  178. map_format(ax)
  179. ax.figure.savefig("js_3sfca.png", dpi = 1200)
  180.  
  181.  
  182. ax = geo_fca.plot(column = "in_region_3", cmap = "coolwarm_r", legend = True, vmin = 0, vmax = 1)
  183.  
  184. map_format(ax)
  185. ax.figure.savefig("js_3sfca_ir.png", dpi = 1200)
  186.  
  187.  
  188.  
  189.  
  190. hp = times[(times.origin == 17031410900) & (times.cost < 60)]
  191. hp = pd.merge(geo10, hp, left_on = "geoid", right_on = "destination")
  192.  
  193. hpax = hp.plot(column = "W3", cmap = "viridis", legend = True)
  194. map_format(hpax)
  195. hpax.figure.savefig("hp_W3.pdf")
  196.  
  197. hpax = hp.plot(column = "G", cmap = "viridis", legend = True)
  198. map_format(hpax)
  199. hpax.figure.savefig("hp_G.pdf")
  200.  
  201. hpax = hp.plot(column = "S", cmap = "viridis", legend = True)
  202. map_format(hpax)
  203. hpax.figure.savefig("hp_S.pdf")
  204.  
  205. hpax = hp.plot(column = "R3", cmap = "viridis", legend = True)
  206. map_format(hpax)
  207. hpax.figure.savefig("hp_R3.pdf")
  208.  
  209. hpax = hp.plot(column = "fca3", cmap = "viridis", legend = True)
  210. map_format(hpax)
  211. hpax.figure.savefig("hp_fca3.pdf")
  212.  
  213.  
  214. uc = times[(times.destination == 17031836200) & (times.cost < 60)]
  215. uc = pd.merge(geo10, uc, left_on = "geoid", right_on = "origin")
  216.  
  217. ucax = uc.plot(column = "D", cmap = "viridis", legend = True)
  218. map_format(ucax)
  219. ucax.figure.savefig("uc_D.pdf")
  220.  
  221. ucax = uc.plot(column = "W3", cmap = "viridis", legend = True)
  222. map_format(ucax)
  223. ucax.figure.savefig("uc_W3.pdf")
  224.  
  225. ucax = uc.plot(column = "G", cmap = "viridis", legend = True)
  226. map_format(ucax)
  227. ucax.figure.savefig("uc_G.pdf")
  228.  
  229. ucax = uc.plot(column = "D3tot", cmap = "viridis", legend = True)
  230. map_format(ucax)
  231. ucax.figure.savefig("uc_D3tot.pdf")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement