def test_lots_of_nodes(self): graph = Graph() actions = ["RenderAction", "ComprenderAction"] scenefiles = ["MayaFile", "NukeFile", "HoudiniFile"] nodes = {} nodes["scenes"] = [] nodes["actions"] = [] numNodes = 2000 for x in range(0, numNodes): r = random() if nodes["scenes"] and r > 0.5: scene = choice(nodes["scenes"]) actiontype = choice(scene.knownActions()) node = scene.createAction(actiontype, "bar") nodes["actions"].append(node) else: node = graph.createNode(choice(scenefiles), "foo") nodes["scenes"].append(node) self.assertEqual( len(nodes["actions"]) + len(nodes["scenes"]), numNodes) self.assertEqual(len(graph.nodes), numNodes) for nodename in graph.nodes: self.assertEqual(nodename, graph.nodes[nodename].name) json1 = graph.serialize() self.assertTrue(json.dumps(json1)) revivedGraph = Graph.deserialize(json1) self.assertEqual(graph, revivedGraph)
def test_create_graph(self): graph = Graph() scenefile = graph.createNode("MayaFile", "lighting") action = graph.createNode("RenderAction", "dragon") data = graph.createNode("Data", "dragon") self.assertEqual(len(graph.nodes), 3)
def _onClearButtonClicked(self): """ Clean the scene. Delete all modules and connections. """ self.graph = Graph() self.scene.clearDockPanel() self.scene.clear() self.scene.modules = {} self.statusBar.showMessage(QString("Clear..."), 2000)
def test_node_names(self): graph = Graph() scenefile = graph.createNode("MayaFile", "lighting") action = scenefile.createAction("RenderAction", "dragon") data = action.createData("RenderData") self.assertEqual(scenefile.name, "MayaFile") self.assertEqual(action.name, "RenderAction") self.assertEqual(data.name, "RenderData")
def _onClearButtonClicked(self): """ Clean the scene. Delete all modules and connections. """ self.graph =Graph() self.scene.clearDockPanel() self.scene.clear() self.scene.modules = {} self.statusBar.showMessage(QString("Clear..."), 2000)
def importShots(self): tempshots = [ "fx010", "fx020", "fx030", "fx040", "fx100", "fx120", "vg040", "vg045", "vg080" ] for shotname in tempshots: shot = Shot(shotname) shot.graph = Graph() self.shotBrowser.addShot(shot)
def __init__(self, iface): QDialog.__init__(self, iface.mainWindow()) self.iface = iface self.setupUi(self) self.setWindowTitle(QString("Workflow Builder v0.01alpha")) self.resize(800, 400) self.scene = DiagramScene(self) self.graphicsView.setScene(self.scene) self.graph = Graph() # SIGNALS and SLOTS QObject.connect(self.executeButton, SIGNAL("clicked()"), self._onExecuteButtonClicked) QObject.connect(self.saveButton, SIGNAL("clicked()"), self._onSaveButtonClicked) QObject.connect(self.cancelButton, SIGNAL("clicked()"), self.reject) QObject.connect(self.clearButton, SIGNAL("clicked()"), self._onClearButtonClicked) QObject.connect(self.graph, SIGNAL("graph"), self.errorMessage)
def setShot(self, shot): logger.debug("Setting shot: " + shot.name) self.shot = shot self.scene().clear() if not self.shot.graph: logger.debug("No graph for shot yet. Creating one") self.shot.graph = Graph() self.shot.graph.addGraphChangedCallback(self.graphChanged) self.graphChanged()
def __init__(self, parent, name): super(TemplateItem, self).__init__(parent) self.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled | Qt.ItemIsEditable) self.setForeground(0, QBrush(QColor("black"))) self.setBackground(0, QBrush(QColor("grey"))) self.shot = Shot(name) self.shot.graph = Graph() self.setText(0, self.shot.name)
def __init__(self, parent = None): super(DiagramScene, self).__init__(parent) self.line = 0 self.textItem = 0 self.myItemColor = Qt.white self.myTextColor = Qt.black self.myLineColor = Qt.black # from VisTrails self.modules = {} self.connections = {} self.justClick = False self.graph = Graph()
def createGraph(self): graph = Graph() actions = ["RenderAction", "ComprenderAction"] scenefiles = ["MayaFile", "NukeFile", "HoudiniFile"] nodes = {} nodes["scenes"] = [] nodes["actions"] = [] numNodes = 100 for x in range(0, numNodes): r = random() if nodes["scenes"] and r > 0.5: scene = choice(nodes["scenes"]) actiontype = choice(scene.knownActions()) node = scene.createAction(actiontype, "bar") nodes["actions"].append(node) else: node = graph.createNode(choice(scenefiles), "foo") nodes["scenes"].append(node) return graph
def add(self, ax, A, ay, B, f): # Adjency Matrix: x, y y = B.x G = copy.deepcopy(A) G.permute(f) x = G.x # coefficients: ax, ay # Links adjX = G.adj adjY = B.adj nY = B.n_nodes new = {} fullset = set(x.keys()).union(set(y.keys())) for i in range(nY): if ((i, i) in x and (i, i) in y): new[i, i] = self.summ(ax, x[i, i], ay, y[i, i]) elif ((i, i) in x and not (i, i) in y): new[i, i] = self.summ(ax, x[i, i], ay, None) elif ((not (i, i) in x) and (i, i) in y): new[i, i] = self.summ(ax, None, ay, y[i, i]) # degree=self.X.degree(i) linked_nodes = [] if (i in adjX and i in adjY): linked_nodes = set(adjX[i]).union(set(adjY[i])) else: if (i in adjX and not i in adjY): linked_nodes = set(adjX[i]) if (i in adjY and not i in adjX): linked_nodes = set(adjY[i]) # for j in range(degree): for j in linked_nodes: if ((not (i, j) in y) and (not (i, j) in x)): continue elif ((i, j) in y and (i, j) in x): new[i, j] = self.summ(ax, x[i, j], ay, y[i, j]) elif (not (i, j) in y): # new[fi,fj]=self.summ(ax,x[i,j0],ay,[0]*len(x[i,j0])) new[i, j] = self.summ(ax, x[i, j], ay, None) # if(not x.has_key((i,j0))): elif (not (i, j) in x): # new[fi,fj]=self.summ(ax,[0]*len(x[i,j0]),ay,y[fi,fj]) new[i, j] = self.summ(ax, None, ay, y[i, j]) newG = Graph(x=new, s=None, adj=None) return newG
def __init__(self, iface ): QDialog.__init__(self, iface.mainWindow()) self.iface = iface self.setupUi(self) self.setWindowTitle(QString("Workflow Builder v0.01alpha")) self.resize(800, 400) self.scene = DiagramScene(self) self.graphicsView.setScene(self.scene) self.graph = Graph() # SIGNALS and SLOTS QObject.connect(self.executeButton, SIGNAL("clicked()"), self._onExecuteButtonClicked) QObject.connect(self.saveButton, SIGNAL("clicked()"), self._onSaveButtonClicked) QObject.connect(self.cancelButton, SIGNAL("clicked()"), self.reject) QObject.connect(self.clearButton, SIGNAL("clicked()"), self._onClearButtonClicked) QObject.connect(self.graph, SIGNAL("graph"), self.errorMessage)
def add(self, ax, ay): self.alignedSource() # Trasforming in two matrix x = self.aX.matrix() y = self.aY.matrix() adjX = self.aX.adj adjY = self.aY.adj # Nodes nX = self.aX.n_nodes new = {} # new set of nodes fullset = set(x.keys()).union(set(y.keys())) for i in range(nX): # node in both networks if ((i, i) in x and (i, i) in y): new[i, i] = self.summ(ax, x[i, i], ay, y[i, i]) # node in one of the two elif ((i, i) in x and not (i, i) in y): new[i, i] = self.summ(ax, x[i, i], ay, None) elif ((not (i, i) in x) and (i, i) in y): new[i, i] = self.summ(ax, None, ay, y[i, i]) linked_nodes = [] if (i in adjX and i in adjY): linked_nodes = set(adjX[i]).union(set(adjY[i])) else: if (i in adjX and not i in adjY): linked_nodes = set(adjX[i]) if (i in adjY and not i in adjX): linked_nodes = set(adjY[i]) for j in linked_nodes: # edge doesn't exist in both networks (impossible) if ((not (i, j) in y) and (not (i, j) in x)): continue # edge exists in both networks elif ((i, j) in y and (i, j) in x): new[i, j] = self.summ(ax, x[i, j], ay, y[i, j]) # edge exists in one of the two elif (not (i, j) in y): new[i, j] = self.summ(ax, x[i, j], ay, None) elif (not (i, j) in x): new[i, j] = self.summ(ax, None, ay, y[i, j]) newG = Graph(x=new, s=self.Y.s, adj=None) return newG
def give_me_a_network(self, geo, n_a, e_a, s=None): ind = [re.findall(r'-?\d+\.?\d*', k) for k in geo.axes[0]] x_g = {} for i in range(len(ind)): if (len(ind[i]) > 2 and int(ind[i][0]) == int(ind[i][1]) and not (int(ind[i][0]), int(ind[i][1])) in x_g): x_g[int(ind[i][0]), int(ind[i][1])] = [ geo.loc[geo.axes[0][i + j]] for j in range(n_a) ] elif (len(ind[i]) > 2 and int(ind[i][0]) != int(ind[i][1]) and not (int(ind[i][0]), int(ind[i][1])) in x_g): x_g[int(ind[i][0]), int(ind[i][1])] = [ geo.loc[geo.axes[0][i + j]] for j in range(e_a) ] elif (len(ind[i]) == 2 and not (int(ind[i][0]), int(ind[i][1])) in x_g): x_g[int(ind[i][0]), int(ind[i][1])] = [geo.loc[geo.axes[0][i]]] geo_net = Graph(x=x_g, adj=None, s=s) return geo_net
def give_me_a_network(self, geo, n_a, e_a): # create the name of the nodes and edges ind = [re.findall(r'-?\d+\.?\d*', k) for k in geo.axes[0]] x_g = {} # loop to fill the x dictionary to create a graph for i in range(len(ind)): if (len(ind[i]) > 2 and int(ind[i][0]) == int(ind[i][1]) and not (int(ind[i][0]), int(ind[i][1])) in x_g): x_g[int(ind[i][0]), int(ind[i][1])] = [ geo.loc[geo.axes[0][i + j]] for j in range(n_a) ] elif (len(ind[i]) > 2 and int(ind[i][0]) != int(ind[i][1]) and not (int(ind[i][0]), int(ind[i][1])) in x_g): x_g[int(ind[i][0]), int(ind[i][1])] = [ geo.loc[geo.axes[0][i + j]] for j in range(e_a) ] elif (len(ind[i]) == 2 and not (int(ind[i][0]), int(ind[i][1])) in x_g): x_g[int(ind[i][0]), int(ind[i][1])] = [geo.loc[geo.axes[0][i]]] geo_net = Graph(x=x_g, adj=None, s=None) return geo_net
x2 = {} x2[0, 0] = [1] x2[1, 1] = [1] x2[2, 2] = [1] x2[3, 3] = [1] x2[4, 4] = [1] x2[5, 5] = [1] x2[0, 1] = [1] x2[1, 0] = [1] x2[1, 2] = [1] x2[2, 1] = [1] x2[3, 4] = [1] x2[4, 3] = [1] # Create Graph set: G = GraphSet(graph_type='directed') G.add(Graph(x=x1, s=[1, 2], adj=None)) G.add(Graph(x=x2, s=[2, 3], adj=None)) # Compute a distance with euclidean distance without matching the graphs match = ID(hamming()) match.dis(G.X[0], G.X[1]) # 2) GRAPHS with Euclidean scalar and vector attributes on both nodes and edges # Define the graphs: x1 = {} x1[0, 0] = [0.813, 0.630] x1[1, 1] = [1.606, 2.488] x1[2, 2] = [2.300, 0.710] x1[3, 3] = [0.950, 1.616] x1[4, 4] = [2.046, 1.560] x1[5, 5] = [2.959, 2.387]
def make_graph_from_nmap_parser(parser): """ """ hosts = parser.get_root().search_children('host', deep=True) graph = Graph() nodes = list() index = 1 # setting initial reference host nodes.append(NetNode(0)) node = nodes[-1] node.set_info({'ip':'127.0.0.1/8', 'hostname':'localhost'}) node.set_draw_info({'color':(0,0,0), 'radius':NONE_RADIUS}) # for each host in hosts just mount the graph for host in hosts: trace = host.search_children('trace', True, True) # if host has traceroute information mount graph if trace != None: prev_node = nodes[0] hops = trace.search_children('hop') ttls = [int(hop.get_attr('ttl')) for hop in hops] # getting nodes of host by ttl for ttl in range(1, max(ttls) + 1): if ttl in ttls: hop = trace.query_children('hop', 'ttl', ttl, True) for node in nodes: if hop.get_attr('ipaddr') == node.get_info('ip'): break else: nodes.append(NetNode(index)) node = nodes[-1] index += 1 node.set_draw_info({'valid':True}) node.set_info({'ip':hop.get_attr('ipaddr')}) node.set_draw_info({'color':(1,1,1), 'radius':NONE_RADIUS}) if hop.get_attr('host') != None: node.set_info({'hostname':hop.get_attr('host')}) rtt = hop.get_attr('rtt') if rtt != '--': graph.set_connection(node, prev_node, float(rtt)) else: graph.set_connection(node, prev_node) else: nodes.append(NetNode(index)) node = nodes[-1] index += 1 node.set_draw_info({'valid':False}) node.set_info({'ip':None, 'hostname':None}) node.set_draw_info({'color':(1,1,1), 'radius':NONE_RADIUS}) graph.set_connection(node, prev_node) prev_node = node # for each full scanned host for host in hosts: ip = host.query_children('address', 'addrtype', 'ipv4', True) if ip == None: ip = host.query_children('address', 'addrtype', 'ipv6', True) for node in nodes: if ip.get_attr('addr') == node.get_info('ip'): break else: nodes.append(NetNode(index)) node = nodes[-1] index += 1 node.set_draw_info({'no_route':True}) graph.set_connection(node, nodes[0]) node.set_draw_info({'valid':True}) node.set_info({'scanned':True}) set_node_info(node, host) graph.set_nodes(nodes) graph.set_main_node_by_id(0) return graph
import os import sys from core import Graph from pprint import pprint # refer to https://github.com/cedricleroy/pyungo if __name__ == '__main__': graph = Graph() @graph.register(inputs=['a', 'b'], outputs=['c']) def f_my_function(a, b): return a + b #@graph.register(inputs=['d', 'a', {'x':100}, {'y':10}], outputs=['e']) def f_my_function3(d, a, x, y): #def f_my_function3(*args, **kwargs): #print args, kwargs #y = kwargs['y'] print(d, a, y) return d - a + x graph.add_node(f_my_function3, inputs=['d', 'a', {'x':100}, {'y':10}], outputs=['e']) @graph.register(inputs=['c'], outputs=['d']) def f_my_function2(*args): print 'args:', (args) c = args[0] return c / 10. res = graph.calculate(data={'a': 2, 'b': 3})
import os import sys sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),"..")) from core import Specie,Graph from itertools import permutations graph=Graph("../graph.json") for i in range(graph.n): start=graph.nodes[i] graph.startindex=i initial=list(set(range(graph.n))-{graph.startindex}) gloabl_minimum=max(Specie(graph,list(sol)) for sol in permutations(initial)) print(f"global minimum for starting and ending in {start:15}: cost={gloabl_minimum.cost} path={gloabl_minimum.genes}")
import os import sys sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) from core import Pool, Graph, Specie import numpy as np from itertools import permutations np.random.seed(42) graph = Graph("../graph.json") pool = Pool(graph, 10, .65, .01) print("initial pop") for specie, p in zip(pool.species, pool.ps): print(specie.genes, specie.cost, round(p * 100, 2)) initial = list(set(range(graph.n)) - {graph.startindex}) gloabl_minimum = max(Specie(graph, list(sol)) for sol in permutations(initial)) print("global minimum", gloabl_minimum.cost) while pool.best.cost != gloabl_minimum.cost: print(pool.gen, pool.is_saturated()) pool.step() print(f"took {pool.gen} generations to reach gloabl minimum cost")
def get_global_minimum(graph): initial = list(set(range(graph.n)) - {graph.startindex}) ans = max((Specie(graph, list(sol))) for sol in permutations(initial)) print("best paths:") for sol in permutations(initial): specie = Specie(graph, list(sol)) if specie.cost == ans.cost: specie.print() return ans args = get_args() np.random.seed(args.seed) graph = Graph("graph.json") pool = Pool(graph, args.population_size, args.crossover_rate, args.mutation_rate) global_minimum = get_global_minimum(graph) best_costs = [pool.best.cost] while pool.best.cost != global_minimum.cost: pool.step() best_costs.append(pool.best.cost) print("path found:") pool.best.print() plt.plot(range(0, len(best_costs)), best_costs) plt.xlabel("Generation") plt.ylabel("Best Cost") plt.title(
class WorkflowBuilder(QDialog, Ui_workflowBuilder): ''' Dialog where you can create you own Module and save it. ''' def __init__(self, iface): QDialog.__init__(self, iface.mainWindow()) self.iface = iface self.setupUi(self) self.setWindowTitle(QString("Workflow Builder v0.01alpha")) self.resize(800, 400) self.scene = DiagramScene(self) self.graphicsView.setScene(self.scene) self.graph = Graph() # SIGNALS and SLOTS QObject.connect(self.executeButton, SIGNAL("clicked()"), self._onExecuteButtonClicked) QObject.connect(self.saveButton, SIGNAL("clicked()"), self._onSaveButtonClicked) QObject.connect(self.cancelButton, SIGNAL("clicked()"), self.reject) QObject.connect(self.clearButton, SIGNAL("clicked()"), self._onClearButtonClicked) QObject.connect(self.graph, SIGNAL("graph"), self.errorMessage) def errorMessage(self, err): if err is "set": self.statusBar.showMessage( QString("You should set selected modules."), 5000) self.scene.clearSelection() self.scene.clearDockPanel() for key in self.graph._invalidSubGraph._invalidInputs.keys(): invalidMod = self.graph._invalidSubGraph._invalidInputs[key] invalidPort = key gInvalidMod = self.scene.findGraphicsModule(invalidMod) if gInvalidMod: gInvalidMod.setSelected(True) elif err is "loop": self.statusBar.showMessage(QString("Graph contains loop(s)."), 5000) else: self.statusBar.showMessage(QString(err), 5000) def createGraph(self): """ From modules and connections in scene we will create Graph and sort Modules to Subgraphs. """ modules = self.graph.modules.values() cons = self.graph.connections.values() def findConnections(mod): ''' Get Modules connected with 'mod' Module and are still avaliable from 'modules' (list of Modules getting at the begging from scene). mod: Module ''' mods = [] for con in cons: if con.sModule == mod: mods.append(con.dModule) elif con.dModule == mod: mods.append(con.sModule) mods = filter(lambda m: True if m in modules else False, list(set(mods))) # while mods: tmp = mods.pop() if tmp in modules: modules.pop(modules.index(tmp)) findConnections(tmp) sGraph.addModule(tmp) # filling Graph self.graph.subgraphs = {} while modules: sGraph = SubGraph() self.graph.addSubGraph(sGraph) mod = modules.pop() findConnections(mod) sGraph.addModule(mod) sGraphCon = filter(lambda c: c.sModule in sGraph.getModules(), cons) sGraph.setConnections(sGraphCon) def _onExecuteButtonClicked(self): """ Create Graph, check if everything is set. Execute it. """ self.statusBar.showMessage(QString("Executing..."), 200000) self.createGraph() if not self.graph.findLoop(): if self.graph.executeGraph(): self.statusBar.showMessage(QString("Executed successfully."), 2000) else: #self.statusBar.showMessage(QString("Executed not successfully."), 2000) pass #self.statusBar.clearMessage() def _onSaveButtonClicked(self): """ Save Graph/Workflow as new Module in Processing Framework to (re)use it. """ self.statusBar.showMessage(QString("Saving..."), 2000) self.createGraph() if not self.graph.findLoop(): if self.graph: #if self.graph.path: # self.graph.save() # reloadPlugin('workflow_builder') # reloadPlugin('processingmanager') #else: svDialog = SaveDialog(self.graph, self) svDialog.show() def _onClearButtonClicked(self): """ Clean the scene. Delete all modules and connections. """ self.graph = Graph() self.scene.clearDockPanel() self.scene.clear() self.scene.modules = {} self.statusBar.showMessage(QString("Clear..."), 2000)
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) from random import shuffle from core import Graph def test_path(path, expected=True): print("path:") graph.print_path(path) try: ans = graph.tsp_weight(path) except Exception as e: if expected: raise e ans = "failed" print("cost:", ans) graph = Graph("../graph.json") print(graph.get_names_pretty()) print(graph.edges) print("-" * 48) path = list(range(1, graph.n)) test_path(path) for _ in range(20): shuffle(path) test_path(path) test_path([0] * graph.n, False) test_path([2, 3, 4, 5, 0, 1])
hidden_neurons=hidden_neurons, output_neurons=output_neurons) X_train, X_validate, Y_train, Y_validate = nn.split_data(X, Y, 0.3) loss_train, acc_train, loss_validate, acc_validate =\ nn.train( X_train = X_train, Y_train = Y_train, X_validate = X_validate, Y_validate = Y_validate) graph = Graph(file="nn1", amount_data=amount_data, hidden_neurons=hidden_neurons, output_neurons=output_neurons, W1=nn.W1, W2=nn.W2, b1=nn.b1, b2=nn.b2) plt.figure() plt.plot(loss_train, label="train") plt.plot(loss_validate, label="validate") plt.xlabel('epochs') plt.ylabel(r'J($\theta$)') plt.legend() plt.figure() plt.plot(acc_train, label="train") plt.plot(acc_validate, label="validate") plt.yticks(np.arange(0.0, 1.1, 0.1))
from core import Graph,Pool import numpy as np from args import get_args from tqdm import tqdm import pickle import os args=get_args(p=1000,m=0.01) np.random.seed(args.seed) graph=Graph("states.json","Mato Grosso do Sul") initial=None if os.path.isfile("best_extra.p"): with open("best_extra.p","rb") as f: initial=pickle.load(f) pool=Pool( graph, args.population_size, args.crossover_rate, args.mutation_rate, initial ) last_best=None try: progress=tqdm(unit=" gen") while True: progress.update()
def read_from_tgf(self,filename): fh = open(filename,"r") for l in fh: n=0 #process(l) g = l.split() if not g: continue #g=l.split() else: if(g[0]=='GRAPH_TYPE' and g[1]=='undirected'): g_type="undirected" continue elif(g[0]=='GRAPH_TYPE' and g[1]=='directed'): g_type="directed" continue if(g[0]=='NODE_ATTR'): n_attr=len(g)-1 print(n_attr) continue if(g[0]=='EDGE_ATTR'): e_attr=len(g)-1 print(e_attr) continue if(g[0]=='GRAPH'): x={} continue if(g[0]=='NODES'): block='n' continue if(g[0]=='EDGES'): block='e' continue if(g[0]=='FEATURES'): s=g[1] if(g[0]=='#'): self.add(Graph(x=x,adj=None,s=s)) del x,block continue else: try: if(isinstance(int(g[0]),int)): if(block=='n'): x[int(g[0]),int(g[0])]=list(map(float,g[1:n_attr+1])) continue if(block=='e' and g_type=='undirected' and e_attr==1): x[int(g[0]),int(g[1])]=[int(g[2])] x[int(g[1]),int(g[0])]=[int(g[2])] continue if(block=='e' and g_type=='undirected' and e_attr>1): x[int(g[0]),int(g[1])]=list(map(float,g[2:e_attr+1])) x[int(g[1]),int(g[0])]=list(map(float,g[2:e_attr+1])) continue if(block=='e' and g_type=='directed'): x[int(g[0]),int(g[1])]=list(map(float,g[2:e_attr+1])) continue except: continue
def read_from_text(self, filename): remove = string.punctuation remove.replace(".", " ") fh = open(filename, "r") for l in fh: n = 0 # process(l) g = l.split() if not g: continue # g=l.split() else: if (g[0] == 'GraphSet'): print('Start Parsing') continue if (g[0] == 'GRAPH_TYPE'): graph_type = g[1] self.g_type = graph_type continue if (g[0] == 'LABELS'): type_y = g[1] continue if (g[0] == 'FEATURES'): type_y = g[1] continue if (g[0] == 'NODE_ATTR'): n_attr = len(g) - 1 continue if (g[0] == 'EDGE_ATTR'): e_attr = len(g) - 1 continue if (g[0] == 'Graph'): if (int(g[1]) == 0): x = {} # first graph need to be estimated s = [d.translate({ord('['): None}).translate({ord(','): None}).translate({ord(']'): None}) for d in g[4:len(g)]] continue elif (int(g[1]) > 0): # estimate self.add(Graph(x=x, adj=adj, s=s)) x = {} s=[d.translate({ord('['): None}).translate({ord(','): None}).translate({ord(']'): None}) for d in g[4:len(g)]] continue if (g[0] == 'Attributes'): block = 'attr' continue if (g[0] == 'Adjency'): adj = {} block = 'adj' continue elif (isinstance(int(g[0]), int)): if (block == 'attr'): if (int(g[0]) == int(g[1]) and n_attr > 1): x[int(g[0]), int(g[1])] = list(map(float, g[2:n_attr + 2])) continue if (int(g[0]) == int(g[1]) and n_attr == 1): x[int(g[0]), int(g[1])] = [float(g[2])] if (int(g[0]) != int(g[1]) and e_attr > 1): x[int(g[0]), int(g[1])] = list(map(float, g[2:e_attr + 2])) if (graph_type == 'undirected'): x[int(g[1]), int(g[0])] = x[int(g[0]), int(g[1])] continue if (int(g[0]) != int(g[1]) and e_attr == 1): x[int(g[0]), int(g[1])] = [float(g[2])] if (graph_type == 'undirected'): x[int(g[1]), int(g[0])] = x[int(g[0]), int(g[1])] continue continue if (block == 'adj'): adj[int(g[0])] = list(map(int, g[1:len(g)])) continue else: continue self.add(Graph(x=x, adj=adj, s=s)) print("End Parsing")
class WorkflowBuilder(QDialog, Ui_workflowBuilder): ''' Dialog where you can create you own Module and save it. ''' def __init__(self, iface ): QDialog.__init__(self, iface.mainWindow()) self.iface = iface self.setupUi(self) self.setWindowTitle(QString("Workflow Builder v0.01alpha")) self.resize(800, 400) self.scene = DiagramScene(self) self.graphicsView.setScene(self.scene) self.graph = Graph() # SIGNALS and SLOTS QObject.connect(self.executeButton, SIGNAL("clicked()"), self._onExecuteButtonClicked) QObject.connect(self.saveButton, SIGNAL("clicked()"), self._onSaveButtonClicked) QObject.connect(self.cancelButton, SIGNAL("clicked()"), self.reject) QObject.connect(self.clearButton, SIGNAL("clicked()"), self._onClearButtonClicked) QObject.connect(self.graph, SIGNAL("graph"), self.errorMessage) def errorMessage(self, err): if err is "set": self.statusBar.showMessage(QString("You should set selected modules."), 5000) self.scene.clearSelection() self.scene.clearDockPanel() for key in self.graph._invalidSubGraph._invalidInputs.keys(): invalidMod = self.graph._invalidSubGraph._invalidInputs[key] invalidPort = key gInvalidMod = self.scene.findGraphicsModule(invalidMod) if gInvalidMod: gInvalidMod.setSelected(True) elif err is "loop": self.statusBar.showMessage(QString("Graph contains loop(s)."), 5000) else: self.statusBar.showMessage(QString(err), 5000) def createGraph(self): """ From modules and connections in scene we will create Graph and sort Modules to Subgraphs. """ modules = self.graph.modules.values() cons = self.graph.connections.values() def findConnections(mod): ''' Get Modules connected with 'mod' Module and are still avaliable from 'modules' (list of Modules getting at the begging from scene). mod: Module ''' mods = [] for con in cons: if con.sModule == mod: mods.append(con.dModule) elif con.dModule == mod: mods.append(con.sModule) mods = filter(lambda m: True if m in modules else False, list(set(mods))) # while mods: tmp = mods.pop() if tmp in modules: modules.pop(modules.index(tmp)) findConnections(tmp) sGraph.addModule(tmp) # filling Graph self.graph.subgraphs = {} while modules: sGraph = SubGraph() self.graph.addSubGraph(sGraph) mod = modules.pop() findConnections(mod) sGraph.addModule(mod) sGraphCon = filter(lambda c: c.sModule in sGraph.getModules() , cons) sGraph.setConnections(sGraphCon) def _onExecuteButtonClicked(self): """ Create Graph, check if everything is set. Execute it. """ self.statusBar.showMessage(QString("Executing..."), 200000) self.createGraph() if not self.graph.findLoop(): if self.graph.executeGraph(): self.statusBar.showMessage(QString("Executed successfully."), 2000) else: #self.statusBar.showMessage(QString("Executed not successfully."), 2000) pass #self.statusBar.clearMessage() def _onSaveButtonClicked(self): """ Save Graph/Workflow as new Module in Processing Framework to (re)use it. """ self.statusBar.showMessage(QString("Saving..."), 2000) self.createGraph() if not self.graph.findLoop(): if self.graph: #if self.graph.path: # self.graph.save() # reloadPlugin('workflow_builder') # reloadPlugin('processingmanager') #else: svDialog = SaveDialog(self.graph, self) svDialog.show() def _onClearButtonClicked(self): """ Clean the scene. Delete all modules and connections. """ self.graph =Graph() self.scene.clearDockPanel() self.scene.clear() self.scene.modules = {} self.statusBar.showMessage(QString("Clear..."), 2000)