示例#1
0
def speed_on_the_ground(graph: nx.DiGraph, filename: str):
    mpl.use("Agg")

    with commons.Section("Getting the background OSM map"):
        nodes = pd.DataFrame(data=nx.get_node_attributes(graph, name="pos"),
                             index=["lon", "lat"]).T
        extent = maps.ax4(nodes.lat, nodes.lon)
        osmap = maps.get_map_by_bbox(maps.ax2mb(*extent))

    with mpl.rc_context(PARAM['mpl_style']), commons.Axes() as ax1:
        velocity = pd.Series(nx.get_edge_attributes(
            graph, name="len")) / pd.Series(
                nx.get_edge_attributes(graph, name=PARAM['edge_time_attr']))
        cmap = LinearSegmentedColormap.from_list(
            name="noname", colors=["brown", "r", "orange", "g"])

        ax1.imshow(osmap, extent=extent, interpolation='quadric', zorder=-100)

        nx.draw(graph,
                ax=ax1,
                pos=nx.get_node_attributes(graph, name="pos"),
                edgelist=list(velocity.index),
                edge_color=list(velocity),
                edge_cmap=cmap,
                edge_vmin=0,
                edge_vmax=7,
                with_labels=False,
                arrows=False,
                node_size=0,
                alpha=0.8,
                width=0.3)

        fn = PARAM['out_images_path'] / commons.myname() / F"{filename}.png"
        ax1.figure.savefig(commons.makedirs(fn))
示例#2
0
def nx_draw_met_by_len(graph, edges_met=None, mpl_backend="Agg", printer=None):
    mpl.use(mpl_backend)

    with Section("Preparing to draw graph", out=printer):

        nodes = pd.DataFrame(data=nx.get_node_attributes(graph, name="loc"),
                             index=["lat", "lon"]).T
        edges_len = pd.Series(data=nx.get_edge_attributes(graph, name="len"),
                              name="len")

        if edges_met is not None:
            edges_met = pd.Series(name="met", data=edges_met)
        else:
            edges_met = pd.Series(name="met",
                                  data=nx.get_edge_attributes(graph,
                                                              name="met"))

        cmap = LinearSegmentedColormap.from_list(
            name="noname", colors=["g", "y", "r", "brown"])

    with Section("Getting the background OSM map", out=printer):
        extent = maps.ax4(nodes.lat, nodes.lon)
        osmap = maps.get_map_by_bbox(maps.ax2mb(*extent))

    with Section("Drawing image", out=printer):

        fig: plt.Figure
        ax1: plt.Axes
        (fig, ax1) = plt.subplots()

        # The background map
        ax1.imshow(osmap, extent=extent, interpolation='quadric', zorder=-100)

        ax1.axis("off")

        edge_colors = (edges_met / edges_len).clip(lower=0.9, upper=1.5)

        nx.draw(graph,
                ax=ax1,
                pos=nx.get_node_attributes(graph, name="pos"),
                edge_list=list(edge_colors.index),
                edge_color=list(edge_colors),
                edge_cmap=cmap,
                with_labels=False,
                arrows=False,
                node_size=0,
                alpha=1,
                width=0.4)

        ax1.set_xlim(extent[0:2])
        ax1.set_ylim(extent[2:4])

    try:
        # Note:
        # "yield (fig, ax1)" does not work with the "retry" context manager
        return iter([(fig, ax1)])
    finally:
        plt.close(fig)
示例#3
0
def compare_multiple_trajectories(table_name):
	mpl.use("Agg")

	# Number of trips to plot
	N = 10
	# Number of trajectories per trip
	M = 12

	graph = get_road_graph()
	nodes = pd.DataFrame(data=nx.get_node_attributes(graph, "loc"), index=["lat", "lon"]).T
	edges_len = nx.get_edge_attributes(graph, name="len")

	where = "('2016-05-02 08:00' <= pickup_datetime) and (pickup_datetime <= '2016-05-02 09:00')"
	trips = get_trip_data(table_name, graph, order="", where=where)

	trips = trips.sample(min(N, len(trips)), random_state=1)
	logger.debug(F"{len(trips)} trips")

	with Section("Getting the background OSM map", out=logger.debug):
		extent = maps.ax4(nodes.lat, nodes.lon)
		osmap = maps.get_map_by_bbox(maps.ax2mb(*extent))

	with plt.style.context({**PARAM['mpl_style'], 'font.size': 5}), Axes() as ax1:
		# The background map
		ax1.imshow(osmap, extent=extent, interpolation='quadric', zorder=-100)

		ax1.axis("off")

		ax1.set_xlim(extent[0:2])
		ax1.set_ylim(extent[2:4])

		for (__, trip) in trips.iterrows():
			with Section("Computing candidate trajectories", out=logger.debug):
				trajectories = pd.DataFrame(data={'path': [
					path
					for (__, path) in
					zip(range(M), nx.shortest_simple_paths(graph, source=trip.u, target=trip.v))
				]})
				trajectories['dist'] = [sum(edges_len[e] for e in pairwise(path)) for path in trajectories.path]
				trajectories = trajectories.sort_values(by='dist', ascending=False)

			marker = dict(markersize=2, markeredgewidth=0.2, markerfacecolor="None")
			ax1.plot(trip['pickup_longitude'], trip['pickup_latitude'], 'og', **marker)
			ax1.plot(trip['dropoff_longitude'], trip['dropoff_latitude'], 'xr', **marker)

			cmap = LinearSegmentedColormap.from_list(name="noname", colors=["g", "orange", "r", "brown"])
			colors = cmap(pd.Series(trajectories['dist'] / trip['distance']).rank(pct=True))

			for (c, path) in zip(colors, trajectories.path):
				(y, x) = nodes.loc[list(path)].values.T
				ax1.plot(x, y, c=c, alpha=0.5, lw=0.3)

			# Save to file
			fn = os.path.join(PARAM['out_images_path'], F"{myname()}/{table_name}.png")
			ax1.figure.savefig(makedirs(fn))
示例#4
0
def trip_trajectories_ingraph(table_name):
	mpl.use("Agg")

	# Max number of trajectories to plot
	N = 1000

	graph = get_road_graph()
	nodes = pd.DataFrame(data=nx.get_node_attributes(graph, "loc"), index=["lat", "lon"]).T

	trips = get_trip_data(table_name, graph)

	trips = trips.sample(min(N, len(trips)))
	logger.debug(F"{len(trips)} trips")

	logger.debug("Computing trajectories")
	trajectories = parallel_map(GraphPathDist(graph).path_only, zip(trips.u, trips.v))

	with Section("Getting the background OSM map", out=logger.debug):
		extent = maps.ax4(nodes.lat, nodes.lon)
		osmap = maps.get_map_by_bbox(maps.ax2mb(*extent))

	with plt.style.context({**PARAM['mpl_style'], 'font.size': 5}):
		with Axes() as ax1:
			# The background map
			ax1.imshow(osmap, extent=extent, interpolation='quadric', zorder=-100)

			ax1.axis("off")

			ax1.set_xlim(extent[0:2])
			ax1.set_ylim(extent[2:4])

			c = 'b'
			if ("green" in table_name): c = "green"
			if ("yello" in table_name): c = "orange"

			logger.debug("Plotting trajectories")
			for traj in trajectories:
				(y, x) = nodes.loc[list(traj)].values.T
				ax1.plot(x, y, c=c, alpha=0.1, lw=0.3)

			# Save to file
			fn = os.path.join(PARAM['out_images_path'], F"{myname()}/{table_name}.png")
			ax1.figure.savefig(makedirs(fn))

			# Meta info
			json.dump({'number_of_trajectories': len(trips)}, open((fn + ".txt"), 'w'))
示例#5
0
def trip_trajectories_velocity(table_name):
	mpl.use("Agg")

	# Max number of trajectories to use
	N = 10000

	graph = get_road_graph()
	nodes = pd.DataFrame(data=nx.get_node_attributes(graph, "loc"), index=["lat", "lon"]).T

	edge_name = pd.Series(nx.get_edge_attributes(graph, name="name"))

	where = "('2016-05-02 08:00' <= pickup_datetime) and (pickup_datetime <= '2016-05-02 09:00')"
	trips = get_trip_data(table_name, graph, order="", limit=N, where=where)

	trips['velocity'] = trips['distance'] / trips['duration/s']
	trips = trips.sort_values(by='velocity', ascending=True)

	logger.debug(F"{len(trips)} trips")

	with Section("Computing estimated trajectories", out=logger.debug):
		trips['traj'] = parallel_map(GraphPathDist(graph).path_only, zip(trips.u, trips.v))

	with Section("Getting the background OSM map", out=logger.debug):
		extent = maps.ax4(nodes.lat, nodes.lon)
		osmap = maps.get_map_by_bbox(maps.ax2mb(*extent))

	with Section("Computing edge velocities", out=logger.debug):
		edge_vel = defaultdict(list)
		for (traj, v) in zip(trips.traj, trips.velocity):
			for e in pairwise(traj):
				edge_vel[e].append(v)
		edge_vel = pd.Series({e: np.mean(v or np.nan) for (e, v) in edge_vel.items()}, index=graph.edges)
		edge_vel = edge_vel.dropna()

	with plt.style.context({**PARAM['mpl_style'], 'font.size': 5}), Axes() as ax1:
		# The background map
		ax1.imshow(osmap, extent=extent, interpolation='quadric', zorder=-100)

		ax1.axis("off")

		ax1.set_xlim(extent[0:2])
		ax1.set_ylim(extent[2:4])

		cmap_velocity = LinearSegmentedColormap.from_list(name="noname", colors=["brown", "r", "orange", "g"])

		# marker = dict(markersize=0.5, markeredgewidth=0.1, markerfacecolor="None")
		# ax1.plot(trips['pickup_longitude'], trips['pickup_latitude'], 'og', **marker)
		# ax1.plot(trips['dropoff_longitude'], trips['dropoff_latitude'], 'xr', **marker)

		# for e in edge_name[edge_name == "65th Street Transverse"].index:
		# 	print(e, edge_vel[e])

		edge_vel: pd.Series
		# edge_vel = edge_vel.rank(pct=True)
		edge_vel = edge_vel.clip(lower=2, upper=6).round()
		edge_vel = (edge_vel - edge_vel.min()) / (edge_vel.max() - edge_vel.min())
		edge_vel = edge_vel.apply(cmap_velocity)

		nx.draw_networkx_edges(
			graph.edge_subgraph(edge_vel.index),
			ax=ax1,
			pos=nx.get_node_attributes(graph, name="pos"),
			edge_list=list(edge_vel.index),
			edge_color=list(edge_vel),
			# edge_cmap=cmap_velocity,
			# vmin=0, vmax=1,
			with_labels=False, arrows=False, node_size=0, alpha=0.8, width=0.3,
		)

		# Save to file
		fn = os.path.join(PARAM['out_images_path'], F"{myname()}/{table_name}.png")
		ax1.figure.savefig(makedirs(fn))

		# Meta info
		json.dump({'number_of_trajectories': len(trips)}, open((fn + ".txt"), 'w'))