def __init__(self): self.plot_object = PlotBot() self.b_conflicts = self.plot_object.b_conflicts self.labels = {} self.root = "" self.graph = None
class PlotNetwork: def __init__(self): self.plot_object = PlotBot() self.b_conflicts = self.plot_object.b_conflicts self.labels = {} self.root = "" self.graph = None def generate_network(self, max_nodes=None): """Generates a network from the b_conflicts in the plot_object, with nodes as conflicts and edges as suggested linked conflicts as per the Plotto text. User can set the max_nodes argument if they wish to produce a sub_graph of max_nodes nodes :param max_nodes: set the limit to the number of nodes generated in the network. :return: Returns a networkx DiGraph G """ self.graph = nx.DiGraph() first = True i = 0 for conflict in self.b_conflicts: if max_nodes is not None and nx.number_of_nodes(self.graph) >= max_nodes: break curr = self.b_conflicts[conflict] self.labels[curr.label] = curr.label if first: self.root = curr.label first = False self.graph.add_node(curr.label, object_data=curr) for sequel in self.plot_object.get_links(self.b_conflicts[conflict], "sequels"): self.graph.add_edge(curr.label, sequel.label) self.labels[sequel.label] = sequel.label for prequel in self.plot_object.get_links(self.b_conflicts[conflict], "prequels"): self.graph.add_edge(prequel.label, curr.label) self.labels[prequel.label] = prequel.label i += 1 return self.graph def draw_circular_graph(self): """ Draws a circular graph from the previously computed graph :return: None """ pos = graphviz_layout(self.graph, prog="twopi", args="") plt.figure(figsize=(8, 8)) nx.draw(self.graph, pos, node_size=20, alpha=0.5, node_color="blue", with_labels=False) nx.draw_networkx_labels(self.graph, pos, self.labels, font_size=6) plt.axis("equal") plt.savefig("circular_tree.png") plt.show() def draw_node_color_map(self): """ Draws a node color map based on node degree from the previously computed graph. :return: None """ pos = nx.spring_layout(self.graph, iterations=2000) nx.draw(self.graph, pos, node_color=range(nx.number_of_nodes(self.graph)), node_size=100, cmap=plt.cm.Blues) plt.savefig("../../College/FYP/FinalReport/images/node_colormap.png") # save as png plt.show() # display def draw_lanl_graph(self): """ Draws a lanl graph from the previously computed graph. :return: None """ lanl_graph = sorted(nx.connected_component_subgraphs(self.graph), key=len, reverse=True)[0] plt.figure(figsize=(8, 8)) # use graphviz to find radial layout pos = graphviz_layout(self.graph, prog="twopi", root=self.root) # draw nodes, coloring by rtt ping time nx.draw(self.graph, pos, with_labels=False, alpha=0.5, node_size=15) # adjust the plot limits xmax = 1.02 * max(xx for xx, yy in pos.values()) ymax = 1.02 * max(yy for xx, yy in pos.values()) plt.xlim(0, xmax) plt.ylim(0, ymax) # plt.show() plt.savefig("lanl_routes.png")
from flask_video_streaming.camera_pi import Camera from flask import Flask, render_template, Response from gpiozero import Button from time import sleep from threading import Thread from sketch import to_sketch from plotbot import PlotBot bot = PlotBot() take_picture = False drawing = False drawing_thread = None frame = None def cam2bot(p): """convert camera resolution 720*480 to bot coordinate""" return (p[1] / 480 * 55 + 25, p[0] / 480 * 55 - 50) def _stop_drawing(): global drawing bot.relax() drawing = False Camera().get_frame() def draw(): global drawing, frame lines = to_sketch(frame) for line in lines:
from flask import Flask, render_template, send_from_directory from flask_socketio import SocketIO, emit from plotbot import PlotBot from time import sleep, time from threading import Thread from gpiozero import Button app = Flask(__name__) socketio = SocketIO(app, async_mode='threading') bot = PlotBot() user_id = None timestamp = None background_thread = None background_run = True lines = [] def reset(): global user_id, lines user_id = None lines = [] socketio.emit('reset') notify() button = Button(18, pull_up=True, hold_time=2) button.when_held = reset def notify(broadcast=True): global user_id
from plotbot import PlotBot from time import sleep def drange(start, stop, step): r = start while True: yield r r += step if start < stop and r > stop or start > stop and r < stop: break b = PlotBot() def rect(x1, y1, x2, y2): for x in drange(x1, x2, 0.1): b.goto([x, y1]) sleep(.0005) for y in drange(y1, y2, 0.1): b.goto([x2, y]) sleep(.0005) for x in drange(x2, x1, -0.1): b.goto([x, y2]) sleep(.0005) for y in drange(y2, y1, -0.1): b.goto([x1, y]) sleep(.0005)