From 89b603e00c9f040c64c2a8557bff951d6c9480aa Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Fri, 18 Nov 2016 22:43:55 -0800 Subject: [PATCH] Now working without fancy.py. --- abp/__init__.py | 1 + abp/nxgraphstate.py | 9 ++------- abp/vizclient.py | 35 +++++++++++++++++++++++++++++++++++ examples/visualization/new.py | 8 ++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 abp/vizclient.py create mode 100644 examples/visualization/new.py diff --git a/abp/__init__.py b/abp/__init__.py index 48827db..93593a4 100644 --- a/abp/__init__.py +++ b/abp/__init__.py @@ -1,6 +1,7 @@ # Alias some stuff to make imports cleaner from abp.graphstate import GraphState from abp.nxgraphstate import NXGraphState +from abp.vizclient import VizClient from abp.qi import CircuitModel DETERMINISTIC = False diff --git a/abp/nxgraphstate.py b/abp/nxgraphstate.py index 253fd30..2c8d957 100644 --- a/abp/nxgraphstate.py +++ b/abp/nxgraphstate.py @@ -1,6 +1,8 @@ import networkx as nx +import numpy as np import graphstate import clifford +import util class NXGraphState(graphstate.GraphState, nx.Graph): """ This is GraphState with NetworkX-like abilities """ @@ -15,10 +17,3 @@ class NXGraphState(graphstate.GraphState, nx.Graph): for key, (x, y, z) in pos.items(): self.node[key]["position"] = util.xyz(x, y, z) - def add_vops(self): - """ Automatically add vops if they're not present """ - for key in self.node: - if not "vop" in self.node[key]: - self.node[key]["vop"] = clifford.identity - - diff --git a/abp/vizclient.py b/abp/vizclient.py new file mode 100644 index 0000000..0a89c11 --- /dev/null +++ b/abp/vizclient.py @@ -0,0 +1,35 @@ +import time, atexit, json +import networkx as nx +import numpy as np +import websocket +from socket import error as socket_error +import clifford +import util +import nxgraphstate + +class VizClient(object): + def __init__(self, uri = "ws://localhost:5000"): + self.ws = websocket.create_connection(uri, timeout=0.1) + atexit.register(self.shutdown) + + def shutdown(self): + """ Close the connection to the websocket """ + self.ws.close() + + def update(self, graph, delay = 0.5): + """ Call this function when you are ready to send data to the browser """ + g = nxgraphstate.NXGraphState(graph) + + # Automatically perform layout if position is not provided + if not all(("position" in node) for node in g.node.values()): + g.layout() + + # Send data to browser and rate-limit + try: + self.ws.send(json.dumps(g.to_json(stringify=True))) + self.ws.recv() + except websocket._exceptions.WebSocketTimeoutException: + print "Timed out ... you might be pushing a bit hard" + time.sleep(delay) + + diff --git a/examples/visualization/new.py b/examples/visualization/new.py new file mode 100644 index 0000000..5e4fa72 --- /dev/null +++ b/examples/visualization/new.py @@ -0,0 +1,8 @@ +from abp import GraphState, VizClient +from abp.util import xyz + +v = VizClient() +g = GraphState(5) +for i in range(5): + g.node[i]["position"] = xyz(i, 0, 0) +v.update(g)