def load_nx_graph(): global node_attributes global edge_attributes graph = nx.Graph() print(graphiti.get_node_ids()) for id in graphiti.get_node_ids(): graph.add_node(id) graph.node[id]['label'] = graphiti.get_node_label(id) for a in node_attributes: attribute = graphiti.get_node_attribute(id, a['name']) if not(attribute is None): value = str(attribute) if a['type'] == "vec2" or a['type'] == "vec3": value = filter(lambda x: not (x in "[,]"), value) graph.node[id][a['name']] = value for id in graphiti.get_edge_ids(): node1 = graphiti.get_edge_node1(id) node2 = graphiti.get_edge_node2(id) graph.add_edge(node1, node2) return graph
def on_started(): #og.register_script("#console", 'dashboard.shell.execute') if len(sys.argv) == 3: if sys.argv[2].endswith(".json"): std.load_json(sys.argv[2]) else: print("Unrecognized format <'" + sys.argv[2] + "'> !") global nodes for nid in og.get_node_ids(): label = og.get_node_label(nid) nodes[label] = nid response = og.request(3, { "function" : "add" }) global timeseries timeseries = response['id']
def save_json(filename): graph = {} graph["meta"] = dict() graph["nodes"] = list() graph["edges"] = list() global node_attributes global edge_attributes print("Saving graph into '" + filename + "' ...") for id in graphiti.get_node_ids(): node = dict() node["id"] = id node["label"] = graphiti.get_node_label(id) for attribute in node_attributes: name = attribute['name'] value = graphiti.get_node_attribute(id, name) if value is None: continue node[name] = value graph["nodes"].append(node) for id in graphiti.get_link_ids(): edge = dict() edge["id"] = id edge["src"] = graphiti.get_link_node1(id) edge["dst"] = graphiti.get_link_node2(id) for attribute in edge_attributes: name = attribute['name'] value = graphiti.get_link_attribute(id, name) if value is None: continue edge[name] = value graph["edges"].append(edge) with open(filename, 'w') as outfile: json.dump(graph, outfile, indent=True, sort_keys=True) print("Done.")
def regex_map(expression, attribute, node_flag, edge_flag, f, translate = True): if translate: expression = fnmatch.translate(expression) r = re.compile(expression) if r is None: print("Invalid expression : <" + expression + "> !") return if node_flag: for nid in graphiti.get_node_ids(): value = None if attribute == "label": value = graphiti.get_node_label(nid) elif attribute == "mark": value = graphiti.get_node_mark(nid) elif attribute == "weight": value = graphiti.get_node_weight(nid) else: value = graphiti.get_node_attribute(nid, attribute) if value is None: f("node", nid, None) else: f("node", nid, r.match(str(value))) if edge_flag: for eid in graphiti.get_edge_ids(): value = None if attribute == "node1": value = graphiti.get_edge_node1(eid) elif attribute == "node2": value = graphiti.get_edge_node2(eid) else: value = graphiti.get_edge_attribute(eid, attribute) if value is None: f("edge", eid, None) else: value_str = str(value) f("edge", eid, r.match(value_str))
def save_json(filename): graph = {} graph["meta"] = dict() graph["nodes"] = list() graph["edges"] = list() global node_attributes global edge_attributes for id in graphiti.get_node_ids(): node = dict() node["id"] = id node["label"] = graphiti.get_node_label(id) for attribute in node_attributes: name = attribute['name'] value = graphiti.get_node_attribute(id, name) if value is None: continue node[name] = value graph["nodes"].append(node) for id in graphiti.get_edge_ids(): edge = dict() edge["id"] = id edge["src"] = graphiti.get_edge_node1(id) edge["dst"] = graphiti.get_edge_node2(id) for attribute in edge_attributes: name = attribute['name'] value = graphiti.get_edge_attribute(id, name) if value is None: continue edge[name] = value graph["edges"].append(edge) with open(filename, 'w') as outfile: json.dump(graph, outfile, indent=True, sort_keys=True)