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 seed_circle_layout(): radius = 100.0 ids = graphiti.get_node_ids() zeros = [] for id in ids: attribute = graphiti.get_node_attribute(id, "depth") if attribute == None: continue else: if attribute == 0: zeros.append(id) count = 0 if len(zeros) == 1: radius = 0.0 for id in zeros: angle = 2.0 * math.pi * float(count) / float(len(zeros)) x = radius * math.cos(angle) y = 0.0 z = radius * math.sin(angle) graphiti.set_node_attribute(id, "graphiti:space:position", "vec3", str(x) + " " + str(y) + " " + str(z)) graphiti.set_node_attribute(id, "graphiti:space:locked", "bool", "True") graphiti.set_node_attribute(id, "graphiti:space:activity", "float", "2.0") graphiti.set_node_attribute(id, "og:space:mark", "int", "2") count += 1
def detect_spn(): degree_map = calculate_degree_map() source_map = dict() for eid in graphiti.get_link_ids(): src = graphiti.get_link_node1(eid) if src not in degree_map: continue if degree_map[src]["in"] == 0 and degree_map[src]["out"] >= 0: dst = graphiti.get_link_node2(eid) if src not in source_map: source_map[src] = [(dst, eid)] elif dst not in source_map[src]: source_map[src].append((dst, eid)) for nid in graphiti.get_node_ids(): graphiti.set_node_attribute(nid, "graphiti:space:lod", "float", "0.0") for eid in graphiti.get_link_ids(): graphiti.set_link_attribute(eid, "graphiti:space:lod", "float", "0.0") for source in source_map.keys(): graphiti.set_node_attribute(source, "graphiti:space:lod", "float", "1.0") for successor in source_map[source]: graphiti.set_node_attribute(successor[0], "graphiti:space:lod", "float", "1.0") graphiti.set_link_attribute(successor[1], "graphiti:space:lod", "float", "1.0") print("SPN detection results :") print(source_map)
def globe_layout(): ids = graphiti.get_node_ids() for id in ids: geo = graphiti.get_node_attribute(id, "world:geolocation") if geo is not None: pos = globe_coordinates(geo[0], geo[1]) graphiti.set_node_attribute(id, "graphiti:space:position", "vec3", std.vec3_to_str(pos)) graphiti.set_node_attribute(id, "graphiti:space:locked", "bool", "true")
def attribute_to_lod(attribute, fun = lambda x : x): min = float("inf") max = float("-inf") min_value = None max_value = None n_values = dict() for nid in graphiti.get_node_ids(): value = graphiti.get_node_attribute(nid, attribute) if value is None: graphiti.set_node_attribute(nid, "graphiti:space:lod", "float", "-1000") continue fun_value = fun(value) if fun_value is None: graphiti.set_node_attribute(nid, "graphiti:space:lod", "float", "-1000") continue fvalue = float(fun_value) if fvalue > max: max = fvalue max_value = value if fvalue < min: min = fvalue min_value = value n_values[nid] = fvalue e_values = dict() for eid in graphiti.get_link_ids(): value = graphiti.get_link_attribute(eid, attribute) if value is None: graphiti.set_link_attribute(eid, "graphiti:space:lod", "float", "-1000") continue fun_value = fun(value) if fun_value is None: graphiti.set_link_attribute(eid, "graphiti:space:lod", "float", "-1000") continue fvalue = float(fun_value) if fvalue > max: max = fvalue max_value = value if fvalue < min: min = fvalue min_value = value e_values[eid] = fvalue for nid in n_values.keys(): svalue = (n_values[nid] - min) / (max - min) print(attribute + " : " + str(n_values[nid]) + " --> lod : " + str(svalue)) graphiti.set_node_attribute(nid, "graphiti:space:lod", "float", str(svalue)) for eid in e_values.keys(): svalue = (e_values[eid] - min) / (max - min) print(attribute + " : " + str(e_values[eid]) + " --> lod : " + str(svalue)) graphiti.set_link_attribute(eid, "graphiti:space:lod", "float", str(svalue)) print("LOD range : " + str(min_value) + " to " + str(max_value))
def randomize_timeline(): ids = graphiti.get_node_ids() time = 0 for nid in ids: time += 500 graphiti.send_command(time, "graph:set_node_attribute", { "id" : int(random.choice(ids)), "type" : "vec4", "name" : "graphiti:space:color", "value" : "1.0 0.0 0.0 1.0" })
def color_diff(): global colors for n in graphiti.get_node_ids(): s = graphiti.get_node_attribute(n, 'diffstatus') graphiti.set_node_attribute(n, "graphiti:space:color", "vec3", colors[s]) for e in graphiti.get_edge_ids(): s = graphiti.get_edge_attribute(e, 'diffstatus') try: graphiti.set_edge_attribute(e, "graphiti:space:color", "vec3", colors[s]) except KeyError: print("edge: {}".format(e)) print("diffstatus: {}".format(s)) sys.exit(-1)
def color_diff(): global colors for n in graphiti.get_node_ids(): s = graphiti.get_node_attribute(n, 'diffstatus') graphiti.set_node_attribute(n, "graphiti:space:color", "vec3", colors[s]) for e in graphiti.get_link_ids(): s = graphiti.get_link_attribute(e, 'diffstatus') try: graphiti.set_link_attribute(e, "graphiti:space:color", "vec3", colors[s]) except KeyError: print("link: {}".format(e)) print("diffstatus: {}".format(s)) sys.exit(-1)
def sphere_layout(radius): ids = graphiti.get_node_ids() for nid in ids: r1 = random.random() * 2 * math.pi r2 = random.random() * 2 * math.pi r3 = 0.9 + 0.1 * random.random() pos = [ radius * r3 * math.sin(r1) * math.cos(r2), radius * r3 * math.cos(r1), radius * r3 * math.sin(r1) * math.sin(r2) ] graphiti.set_node_attribute(nid, "graphiti:space:position", "vec3", std.vec3_to_str(pos))
def color_nodes_by_infected(): graphiti.set_attribute("graphiti:space:linkmode", "string", "node_color") ids = graphiti.get_node_ids() for id in ids: score = graphiti.get_node_attribute(id, "sgraph:infected") if score is None: graphiti.set_node_attribute(id, "graphiti:space:color", "vec4", "0.5 0.5 0.5 0.5") elif score < 0: graphiti.set_node_attribute(id, "graphiti:space:color", "vec3", "1.0 0.0 0.0") elif score > 0: graphiti.set_node_attribute(id, "graphiti:space:color", "vec3", "0.0 1.0 0.0") else: graphiti.set_node_attribute(id, "graphiti:space:color", "vec3", "1.0 1.0 1.0")
def color_nodes_by_dga_score(): graphiti.set_attribute("graphiti:space:linkmode", "string", "node_color") ids = graphiti.get_node_ids() for id in ids: score = graphiti.get_node_attribute(id, "sgraph:dga:score") if score is None: graphiti.set_node_attribute(id, "graphiti:space:color", "vec4", "0.5 0.5 0.5 0.5") continue else: # DGA score is between [0 : not DGA, 100 : DGA] sub = score / 100; rgb = [1.0, 1.0 - sub, 1.0 - sub, 1.0] graphiti.set_node_attribute(id, "graphiti:space:color", "vec4", std.vec4_to_str(rgb))
def multiply_colors(v, node_flag, edge_flag): def f(c, v): return [c[0] * v[0], c[1] * v[1], c[2] * v[2], c[3] * v[3]] if node_flag: for id in graphiti.get_node_ids(): c = graphiti.get_node_attribute(id, "graphiti:space:color") graphiti.set_node_attribute(id, "graphiti:space:color", "vec4", std.vec4_to_str(f(c, v))) if edge_flag: for id in graphiti.get_link_ids(): c = graphiti.get_link_attribute(id, "graphiti:space:color1") graphiti.set_link_attribute(id, "graphiti:space:color1", "vec4", std.vec4_to_str(f(c, v))) c = graphiti.get_link_attribute(id, "graphiti:space:color2") graphiti.set_link_attribute(id, "graphiti:space:color2", "vec4", std.vec4_to_str(f(c, v)))
def cube_layout(): ids = graphiti.get_node_ids() size = int(len(ids) ** (1.0 / 3.0)) count = 0 for nid in ids: pos = [ count % size, count / (size * size), (count % (size * size)) / size ] for i in range(len(pos)): pos[i] = 5 * (pos[i] - size / 2) graphiti.set_node_attribute(nid, "graphiti:space:position", "vec3", std.vec3_to_str(pos)) count += 1
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)
def color_by_node_degree(): graphiti.set_attribute("graphiti:space:linkmode", "string", "node_color") print("Building node degree table ...") edges = graphiti.get_link_ids() degree_table = dict() for eid in edges: nid1 = graphiti.get_link_node1(eid) nid2 = graphiti.get_link_node2(eid) if nid1 not in degree_table: degree_table[nid1] = { "in" : 0, "out" : 0 } if nid2 not in degree_table: degree_table[nid2] = { "in" : 0, "out" : 0 } degree_table[nid1]["out"] += 1 degree_table[nid2]["in"] += 1 print("Randomizing color map ...") m = dict() m["isolated"] = [0.95, 0.98, 0.36, 1.0] m["leaf"] = [0.06, 0.94, 0.61, 1.0] m["source"] = [0.91, 0.18, 0.17, 1.0] m["sink"] = [0.03, 0.65, 0.94, 1.0] m["other"] = [0.77, 0.78, 0.75, 1.0] print(m) print("Coloring ...") for nid in graphiti.get_node_ids(): if nid not in degree_table: t = "isolated" else: deg = degree_table[nid] if deg["in"] == 0 and deg["out"] == 1: t = "leaf" elif deg["in"] == 0 and deg["out"] > 1: t = "source" elif deg["in"] > 0 and deg["out"] == 0: t = "sink" else: t = "other" graphiti.set_node_attribute(nid, "graphiti:space:color", "vec4", std.vec4_to_str(m[t]))
def color_nodes_by_type(): graphiti.set_attribute("graphiti:space:linkmode", "string", "edge_color") ids = graphiti.get_node_ids() colors = dict() for id in ids: type = graphiti.get_node_attribute(id, "type") if type == None: print("Node " + str(id) + " has no type attribute !") color = None if type in colors: color = colors[type] else: r = random.random() g = random.random() b = random.random() color = str(r) + " " + str(g) + " " + str(b) colors[type] = color graphiti.set_node_attribute(id, "graphiti:space:color", "vec3", color)
def set_lod(value): for id in graphiti.get_node_ids(): graphiti.set_node_attribute(id, "graphiti:space:lod", "float", str(value)) for id in graphiti.get_link_ids(): graphiti.set_link_attribute(id, "graphiti:space:lod", "float", str(value))
def reset_icons(): for nid in graphiti.get_node_ids(): graphiti.set_node_attribute(nid, "graphiti:space:icon", "string", "shapes/disk")
def attribute_to_icon(attribute, fun = lambda a : "shapes/disk"): for nid in graphiti.get_node_ids(): value = graphiti.get_node_attribute(nid, attribute) graphiti.set_node_attribute(nid, "graphiti:space:icon", "string", fun(value))
def randomize_lod(): for id in graphiti.get_node_ids(): graphiti.set_node_attribute(id, "og:space:lod", "float", str(random.random())) for id in graphiti.get_link_ids(): graphiti.set_link_attribute(id, "og:space:lod", "float", str(random.random()))
def clear_graph(): for id in graphiti.get_node_ids(): graphiti.remove_node(id)
def randomize_node_activity(): for id in graphiti.get_node_ids(): if random.uniform(0.0, 1.0) > 0.10: continue activity = random.uniform(0.0, 5.0) graphiti.set_node_attribute(id, "graphiti:space:activity", "float", str(activity))
def clear_colors(): graphiti.set_attribute("graphiti:space:linkmode", "string", "node_color") for n in graphiti.get_node_ids(): graphiti.set_node_attribute(n, "graphiti:space:color", "vec4", "1.0 1.0 1.0 1.0")
def randomize_node_icons(): icons = glob.glob("./Resources/Countries/*.png") print(icons) for id in graphiti.get_node_ids(): icon = "countries/" + random.choice(icons).split("/")[-1][:2].lower() graphiti.set_node_attribute(id, "graphiti:space:icon", "string", icon)
def randomize_node_size(): for id in graphiti.get_node_ids(): activity = random.uniform(0.0, 5.0) graphiti.set_node_attribute(id, "graphiti:space:size", "float", str(activity))
def clear_icons(): for n in graphiti.get_node_ids(): graphiti.set_node_attribute(n, "graphiti:space:icon", "string", "shapes/disk")