def main(): if len(argv) < 5: stderr.write( f"USAGE: {argv[0]} [path to road network GEOJSON file] [path to CSV emissions directory] " f"[path to CSV building counts directory] [path to output directory]" ) exit(1) with open(argv[1], 'r', encoding='utf-8') as file: network = RoadNetwork(file) for hour in range(3, 25): with open(osp.join(argv[2], f'2017-07-04_{hour-1:02d}_energy.csv'), 'r', encoding='utf-8') as file: emissions = EmissionsSnapshot.load(file) hm = comp_all(network, emissions) with open(osp.join(argv[3], f'{hour:02d}_counts.csv'), 'r', encoding='utf-8') as file: reader = csv.reader(file) next(reader) buildings = [] for row in reader: bldg = Building.parse_row(row) buildings.append(bldg) with open(osp.join(argv[4], f'building_em_density_{hour:02d}.csv'), 'w', encoding='utf-8', newline='') as file: writer = csv.writer(file) writer.writerow([ 'BUILDING', 'BUILDING_X', 'BUILDING_Y', 'BUILDING_AREA', 'EMISSIONS_TOTAL', 'MAPPED_VEHICLE_COUNT', 'EMISSIONS_CONCENTRATION' ]) for bldg in buildings: sw_corner = utm_to_bm((bldg.west, bldg.south)) ne_corner = utm_to_bm((bldg.east, bldg.north)) x_min, x_max, y_min, y_max = (floor(sw_corner[0]), floor(ne_corner[0]), floor(ne_corner[1]), floor(sw_corner[1])) em_total = 0 for i, j in itprod(range(y_min, y_max + 1), range(x_min, x_max + 1)): if i < 0 or BM_ROWS <= i or j < 0 or BM_COLS <= j: continue em_total += hm[0][i][j] writer.writerow([ bldg.building_id, bldg.location[0], bldg.location[1], bldg.area, em_total, bldg.count, em_total / bldg.area ])
def main(): if len(argv) < 4: stderr.write( "USAGE: " + argv[0] + " [path to road network GEOJSON file] [path to emissions CSV snapshot file] " + "[path to output directory]\n") exit(1) with open(argv[1], 'r', encoding='utf-8') as file: network = RoadNetwork(file) for day, hour in itprod([4, 6], range(10, 11)): with open(osp.join(argv[2], f'2017-07-{day:02d}_{hour:02d}_energy.csv'), 'r', encoding='utf-8') as file: emissions = EmissionsSnapshot.load(file) fig: Figure = plt.figure(figsize=(8.5, 8)) ax = fig.add_subplot() # ax.set_xlabel("UTM Position (m)") # ax.set_ylabel("UTM Position (m)") ax.set_xticks([]) ax.set_yticks([]) ax.set_xlim(X_MIN, X_MAX) ax.set_ylim(Y_MIN, Y_MAX) plot_roads(ax, network) hmap, link_cells, max_value = comp_all(network, emissions) em_norm = Normalize(0, max_value, clip=True) # print(f'07/{day:02d} at {hour:02d} max value = {max_value:03.3f} MMBtu') n_items = 3 ax.imshow(hmap, zorder=1, cmap=HEAT_CM_2_ALPHA, extent=(X_MIN, X_MAX, Y_MIN, Y_MAX)) # ax.set_title(f"Emissions Heat Map in Downtown Chicago,\n07/{day:02d}/2017 at {hour:02d}:00", pad=10.0, # fontdict={ # 'fontsize': 22 # }) cbar = fig.colorbar(cm.ScalarMappable(norm=em_norm, cmap=HEAT_CM_2), label="Emissions Quantity (MMBtu)") fig.tight_layout() plt.savefig( osp.join(argv[3], '2017-07-{:02d}_{:02d}_heatmap.png'.format(day, hour))) plt.close(fig)
def main(): if len(sys.argv) < 3: sys.stderr.write( "USAGE: " + sys.argv[0] + " [path to road network GeoJSON file] [path to snapshot data] [path to link volume data]\n" ) sys.exit(1) # Set up figure and axes: fig = plt.figure() ax = fig.add_subplot(111) with open(sys.argv[1], "r", encoding="utf-8") as f: network = RoadNetwork(f) # Load traces and count # of frames per link: with open(sys.argv[2], "r", encoding="utf-8") as f: snapshot = Snapshot.load(f, ordered=False) link_counts = {} for road in network.links: link_counts[road.id] = set() for frame in snapshot.iter_time(): link_counts[frame.link].add(frame.vid) for road in network.links: link_counts[road.id] = len(link_counts[road.id]) # Load link volume snapshot data: with open(sys.argv[3], "r", encoding="utf-8") as f: vols = linkvolio.link_volumes(f) xs = [] ys = [] for road in network.links: if road.id in link_counts and road.id in vols: ys.append(link_counts[road.id]) xs.append(vols[road.id].link_vol) ax.plot(xs, ys, '.') ax.set_ylabel("Vehicles on Link (Snapshot Data)") ax.set_xlabel("Vehicles on Link (Link Volume Data)") ax.set_title("Link Volume Correlation") plt.show()
def main(): if len(sys.argv) < 3: sys.stderr.write( "USAGE: " + sys.argv[0] + " [path to road network GeoJSON file] [path to snapshot data] [path to emissions snapshot]\n" ) sys.exit(1) # Set up figure and axes: fig = plt.figure() ax = fig.add_subplot(111) with open(sys.argv[1], "r", encoding="utf-8") as f: network = RoadNetwork(f) # Load traces and count # of frames per link: with open(sys.argv[2], "r", encoding="utf-8") as f: snapshot = Snapshot.load(f, ordered=False) link_counts = {} for road in network.links: link_counts[road.id] = set() for frame in snapshot.iter_time(): link_counts[frame.link].add(frame.vid) for road in network.links: link_counts[road.id] = len(link_counts[road.id]) # Load emissions snapshot data: with open(sys.argv[3], "r", encoding="utf-8") as f: snapshot = EmissionsSnapshot.load(f) xs = [] ys = [] for road in network.links: if road.id in link_counts and road.id in snapshot.data: xs.append(link_counts[road.id]) ys.append(snapshot.data[road.id].quantity) ax.plot(xs, ys, '.') ax.set_title("Recorded Snapshot Vehicle Counts vs. Emissions Quantities") ax.set_xlabel("Derived Link Volume (vehicles)") ax.set_ylabel("Emissions Quantity (MMBtu)") plt.show()
def main(): if len(sys.argv) < 3: sys.stderr.write( "USAGE: " + sys.argv[0] + " [path to road network GeoJSON file] [path to snapshot data]\n") sys.exit(1) # Set up figure and axes: fig = plt.figure() ax = fig.add_subplot(aspect="equal") with open(sys.argv[1], "r", encoding="utf-8") as f: network = RoadNetwork(f) plot_roads(ax, network, False, False) # Load and render traces: with open(sys.argv[2], "r", encoding="utf-8") as f: snapshot = Snapshot.load(f, ordered=False) rng = default_rng(SEED) i = 0 for trace in snapshot.iter_traces(): if rng.random() >= SAMPLE_PROPORTION: continue line = plot_trace(trace, rng.random(3)) if line is None: continue ax.add_line(line) i += 1 if i % 10 == 0: sys.stderr.write("Plotted {} traces...\n".format(i)) sys.stderr.flush() ax.autoscale_view() ax.set_xlabel("Position (m)") ax.set_ylabel("Position (m)") ax.set_title("Vehicle Traces") plt.show()
def main(): if len(argv) < 5: stderr.write( "USAGE: " + argv[0] + "[path to road network GEOJSON file] [path to CSV mappings directory] [path to CSV " "emissions directory] [output directory]\n ") exit(1) with open(argv[1], 'r', encoding='utf-8') as file: network = RoadNetwork(file) err = 0 outside = 0 total = 0 for hour in range(3, 25): with open(osp.join(argv[2], f'{hour:02d}_mappings.csv'), 'r', encoding='utf-8') as file: vm = VehicleMappings.load(file) with open(osp.join(argv[3], f'2017-07-04_{hour-1:02d}_energy.csv'), 'r', encoding='utf-8') as file: em = EmissionsSnapshot.load(file) hm, link_cells, max_value = comp_all(network, em) for i in vm.data: cells = link_cells[vm.data[i].link_id] vx, vy, dist = _get_nearest_cell(vm.data[i].vehicle_loc, cells) total += 1 if vm.data[i].vehicle_loc[1] < Y_MIN or Y_MAX < vm.data[ i].vehicle_loc[1]: outside += 1 err += 1 elif dist > DIST_THRESHOLD: err += 1 print( f"{err:05d} erroneous entries out of {total:05d} = {100 * err / total:2.3f}%" ) print( f"{outside:05d} entries with vehicle outside of y-bounds out of {total:05d} = {100 * outside / total:2.3f}%" )
def main(): if len(sys.argv) < 2: sys.stderr.write("USAGE: " + sys.argv[0] + " [path to road network GeoJSON file]\n") sys.exit(1) # Set up figure and axes: fig = plt.figure() ax = fig.add_subplot(aspect="equal") with open(sys.argv[1], "r", encoding="utf-8") as f: network = RoadNetwork(f) plot_roads(ax, network) ax.autoscale_view() ax.set_xlabel("Position (m)") ax.set_ylabel("Position (m)") ax.set_title("Road Network") plt.show()
def main(): if len(sys.argv) < 3: sys.stderr.write( "USAGE: " + sys.argv[0] + " [path to road network GeoJSON file] [path to link volume data] [path to emissions snapshot]\n" ) sys.exit(1) # Set up figure and axes: fig = plt.figure() ax = fig.add_subplot(111) with open(sys.argv[1], "r", encoding="utf-8") as f: network = RoadNetwork(f) # Load traces and count # of frames per link: with open(sys.argv[2], "r", encoding="utf-8") as f: volumes = linkvolio.link_volumes(f) # Load emissions snapshot data: with open(sys.argv[3], "r", encoding="utf-8") as f: snapshot = EmissionsSnapshot.load(f) xs = [] ys = [] for road in network.links: if road.id in volumes and road.id in snapshot.data: xs.append(volumes[road.id].link_vol) ys.append(snapshot.data[road.id].quantity) ax.plot(xs, ys, '.') ax.set_title("Recorded Link Volume vs. Emissions Quantity") ax.set_xlabel("Link Volume (vehicles / hour)") ax.set_ylabel("Emissions Quantity (MMBtu)") plt.show()
def main(): if len(argv) < 3: stderr.write( f"USAGE: {argv[0]} [path to road network] [path to emissions directory]\n" ) exit(1) with open(argv[1], 'r', encoding='utf-8') as file: network = RoadNetwork(file) result = [{}] * 24 for hour in range(0, 24): maps = {} for day in range(4, 11): with open(osp.join(argv[2], f'2017-07-{day:02d}_{hour:02d}_energy.csv'), 'r', encoding='utf-8') as file: emissions = EmissionsSnapshot.load(file) maps[day] = heatmap.comp_values(network, emissions)[0] for prev in range(4, day): result[hour][(prev, day)] = heatmap.comp_diff(maps[prev], maps[day]) print( f"Computed difference for hour {hour:02d}, days {prev:02d} and {day:02d}" ) with open('data/heatmap_diffs.csv', 'w', newline='', encoding='utf-8') as csv_file: csv_writer = CSVWriter(csv_file) csv_writer.writerow(["Day 1", "Day 2", "Hour", "Difference"]) for hour, diffs in enumerate(result): for day1, day2 in diffs.keys(): csv_writer.writerow([day1, day2, hour, diffs[(day1, day2)]])
def main(): if len(sys.argv) < 3: sys.stderr.write( "USAGE: " + sys.argv[0] + " [path to road network GeoJSON file] [path to snapshot data]\n") sys.exit(1) # Set up figure and axes: fig = plt.figure() ax = fig.add_subplot(121, aspect="equal") # Load traces and count # of frames per link: with open(sys.argv[2], "r", encoding="utf-8") as f: snapshot = Snapshot.load(f, ordered=False) link_counts = Counter(frame.link for frame in snapshot.iter_time()) # print("most common links: ", link_counts.most_common(10)) with open(sys.argv[1], "r", encoding="utf-8") as f: network = RoadNetwork(f) counts = np.zeros(len(network.links)) for road in network.links: counts[road.id] = link_counts.get(road.id, 0) # Filter outliers in count data: q1 = np.percentile(counts, 25) q3 = np.percentile(counts, 75) iqr = q3 - q1 f1 = q1 - (1.5 * iqr) f2 = q3 + (1.5 * iqr) plotted = {} for road in network.links: v = counts[road.id] if v < f1 or v > f2: continue plotted[road.id] = v # Plot roads: cmap = plt.get_cmap("viridis") nm = Normalize() nm.autoscale([v for v in plotted.values()]) for road in network.links: if road.id in plotted: color = cmap(nm(plotted[road.id])) else: color = (0, 0, 0, 0) ax.add_line(plot_road(road, color)) ax.autoscale_view() ax.set_xlabel("Position (m)") ax.set_ylabel("Position (m)") ax.set_title("Link Density") print("nonzero links: ", np.count_nonzero(counts)) ax2 = fig.add_subplot(122) ax2.hist(counts, bins="auto") ax2.axvline(f1, linestyle='--', color='k') ax2.axvline(f2, linestyle='--', color='k') ax2.axvline(q1, linestyle='--', color='r') ax2.axvline(q3, linestyle='--', color='r') ax2.set_xlabel("Density") ax2.set_ylabel("Frequency") ax2.set_title("Densities") plt.show()
def main(): if len(argv) < 4: stderr.write( f"USAGE: {argv[0]} [path to road network GEOJSON file] [path to simulation snapshot CSV directory] " f"[output directory for new dataset] [generate reports = true | false]" ) exit(1) gen_reports = argv[4] == 'true' with open(argv[1], 'r', encoding='utf-8') as file: network = RoadNetwork(file) err_x, err_y, outside_x, outside_y, err_total, total = 0, 0, 0, 0, 0, 0 methods = {} for hour in range(0, 25): if gen_reports: file = open(osp.join(argv[3], f'report_Snapshot_{hour*10**6:d}.csv'), 'w', encoding='utf-8', newline='') cr_writer = csv.writer(file) cr_writer.writerow(['VEHICLE', 'TIME', 'LINK', 'A_X', 'A_Y', 'B_X', 'B_Y', 'OLD_X_COORD', 'OLD_Y_COORD', 'X_METHOD', 'Y_METHOD', 'NEW_X_COORD', 'NEW_Y_COORD', 'DIFF_X', 'DIFF_Y']) else: cr_writer = None with open(osp.join(argv[2], f'Snapshot_{hour*10**6:d}.csv'), 'r', encoding='utf-8') as file: ss = Snapshot.load(file) total += len(ss.frames) for frame in ss.frames: start = network.links[frame.link].points[0] end = network.links[frame.link].points[1] if start[0] < end[0]: a = start b = end else: a = end b = start err_coords = 0 if frame.x < X_MIN or X_MAX < frame.x: outside_x += 1 err_x += 1 err_coords += 1 elif frame.x < a[0] - DX_THRESHOLD or b[0] + DX_THRESHOLD < frame.x: err_x += 1 err_coords += 1 if frame.y < Y_MIN or Y_MAX < frame.y: outside_y += 1 err_y += 1 err_coords += 1 elif frame.y < min(a[1], b[1]) - DY_THRESHOLD or max(a[1], b[1]) + DY_THRESHOLD < frame.y: err_y += 1 err_coords += 1 if err_coords > 0: err_total += 1 old_x, old_y = frame.x, frame.y frame.x, frame.y, x_meth, y_meth = comp_xy(frame.x, a, b) if (x_meth, y_meth) not in methods: methods[(x_meth, y_meth)] = 1 else: methods[(x_meth, y_meth)] += 1 if gen_reports: cr_writer.writerow([frame.vid, frame.timestamp(), frame.link, a[0], a[1], b[0], b[1], old_x, old_y, X_METHODS[x_meth], Y_METHODS[y_meth], frame.x, frame.y, frame.x - old_x, frame.y - old_y]) print(f"{100 * (hour + 1)/25:3.2f}% of data processed") with open(osp.join(argv[3], f'Snapshot_{hour*10**6:d}.csv'), 'w', encoding='utf-8', newline='') as file: ss.write(file) print("Error rates prior to reinterpretation:") print(f"{err_x:05d} erroneous x-coordinates out of {total:05d} total entries = {err_x * 100 / total:3.3f}%") print(f"{outside_x:05d} of these were outside the map, rate of occurrence = {outside_x * 100 / total:3.3f}%") print(f"{err_y:05d} erroneous y-coordinates out of {total:05d} total entries = {err_y * 100 / total:3.3f}%") print(f"{outside_y:05d} of these were outside the map, rate of occurrence = {outside_y * 100 / total:3.3f}%") print(f"{err_total:05d} erroneous entries in total, rate of occurrence = {err_total *100 / total:3.3f}%") for xym in methods: print(f"{methods[xym]:05d} entries were corrected as follows: method for x-coord was \"{X_METHODS[xym[0]]}\", " f"method for y-coord was \"{Y_METHODS[xym[1]]}\"")
def main(): if len(sys.argv) < 4: sys.stderr.write( "USAGE: " + sys.argv[0] + " [path to simplified building data] [path to mappings] [road network]\n" ) sys.exit(1) buildings = load_buildings(sys.argv[1]) with open(sys.argv[3], 'r', encoding='utf-8') as file: network = RoadNetwork(file) with open(sys.argv[2], "r", encoding="utf-8") as f: reader = csv.reader(f) next(reader) mappings = {} for row in reader: vehicle = int(row[0]) bldg = int(row[4]) mapped_count = int(row[8]) buildings[bldg]["count"] = mapped_count mappings[vehicle] = { "building": bldg, "x": float(row[2]), "y": float(row[3]), "distance": float(row[7]) } counts = np.fromiter(filter(lambda x: x > 0, map(lambda b: b["count"], buildings.values())), float) if LOG_SCALING: counts = np.log(counts) cq1, cq3, cf1, cf2 = fences(counts) norm = Normalize(0, cf2, clip=True) # Set up figure and axes: fig = plt.figure() ax = fig.add_subplot(aspect="equal") plot_roads(ax, network, color_roads=False, plot_nodes=False, alpha=0.70, default_road_color='#FFFFFF') for bldg in buildings.values(): edge_alpha = 1.0 alpha = 0.8 if bldg["count"] <= 0: bldg["color"] = (0, 0, 0, 0) edge_alpha = 0.5 else: if LOG_SCALING: count = np.log(bldg["count"]) else: count = bldg["count"] bldg["color"] = HEAT_CM_1(norm(count), alpha=alpha) patch = bldg["poly"] patch.set_edgecolor((0, 0, 0, edge_alpha)) patch.set_facecolor(bldg["color"]) patch.set_fill(True) ax.add_patch(patch) vids = np.fromiter(mappings.keys(), int, len(mappings)) n = min(len(mappings), 2500) rng = default_rng(SEED) sample = rng.choice(vids, size=n, replace=False) for vid in sample: frame = mappings[vid] bldg = buildings[frame["building"]] xs = [frame["x"], bldg["center"][0]] ys = [frame["y"], bldg["center"][1]] ax.add_line(Line2D(xs, ys, linestyle="-", marker='.', markevery=[0], linewidth=1, color=(0, 0, 0, 0.5))) ax.autoscale_view() ax.set_facecolor((0.50, 0.50, 0.50, 1.0)) ax.set_xlabel("Position (m)") ax.set_ylabel("Position (m)") ax.set_title("Vehicle Mapping Density, 10 AM") fig.colorbar(cm.ScalarMappable(norm=norm, cmap=HEAT_CM_1)) plt.show()
def main(): if len(argv) < 6: stderr.write( f"USAGE: {argv[0]} [path to road network GEOJSON file] [path to simplified building data] " f"[path to mappings file] [path to emissions file] [output path]") exit(1) with open(argv[1], 'r', encoding='utf-8') as file: network = RoadNetwork(file) buildings = load_buildings(argv[2], z_order=Z_ORDER_BUILDING) with open(argv[3], 'r', encoding='utf-8') as file: reader = csv.reader(file) next(reader) mappings = {} for row in reader: vehicle = int(row[0]) bldg = int(row[4]) mapped_count = int(row[8]) buildings[bldg]["count"] = mapped_count mappings[vehicle] = { "building": bldg, "x": float(row[2]), "y": float(row[3]), "distance": float(row[7]) } counts = np.fromiter( filter(lambda x: x > 0, map(lambda b: b["count"], buildings.values())), float) if LOG_SCALING: counts = np.log(counts) cq1, cq3, cf1, cf2 = fences(counts) bldg_norm = Normalize(0, cf2, clip=True) # Set up figure and axes: fig = plt.figure() ax: Axes = fig.add_subplot() plot_roads(ax, network, color_roads=False, plot_nodes=False, z_order=Z_ORDER_ROAD, alpha=0.25, default_road_color='#FFFFFF') for bldg in buildings.values(): edge_alpha = 0.75 alpha = 0.5 if bldg["count"] <= 0: bldg["color"] = (0, 0, 0, 0) edge_alpha = 0.5 else: if LOG_SCALING: count = np.log(bldg["count"]) else: count = bldg["count"] bldg["color"] = HEAT_CM_1(bldg_norm(count)) patch = bldg["poly"] patch.set_edgecolor((0, 0, 0, edge_alpha)) patch.set_facecolor(bldg["color"]) patch.set_fill(True) ax.add_patch(patch) vids = np.fromiter(mappings.keys(), int, len(mappings)) n = min(len(mappings), 2500) rng = default_rng(SEED) sample = rng.choice(vids, size=n, replace=False) with open(argv[4], 'r', encoding='utf-8') as file: emissions = EmissionsSnapshot.load(file) hmap, link_cells, max_value = comp_all(network, emissions) em_norm = Normalize(0, max_value, clip=True) ax.imshow(hmap, cmap=HEAT_CM_2_ALPHA, zorder=Z_ORDER_HEAT, extent=(X_MIN, X_MAX, Y_MIN, Y_MAX)) ax.set_xlim(X_MIN, X_MAX) ax.set_ylim(Y_MIN, Y_MAX) ax.set_xticks([]) ax.set_yticks([]) fig.colorbar(cm.ScalarMappable(norm=bldg_norm, cmap=HEAT_CM_1), label="Mapped Vehicle Count") fig.colorbar(cm.ScalarMappable(norm=em_norm, cmap=HEAT_CM_2), label="Emissions Quantity (MMBtu)") plt.savefig(argv[5]) plt.show()