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 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_link_ids(): node1 = graphiti.get_link_node1(id) node2 = graphiti.get_link_node2(id) graph.add_edge(node1, node2) return graph
def calculate_degree_map(): degrees = dict() for eid in graphiti.get_link_ids(): bi = False e_type = graphiti.get_link_attribute(eid, "type") if e_type is not None and "<->" in e_type: bi = True nid1 = graphiti.get_link_node1(eid) nid2 = graphiti.get_link_node2(eid) if nid1 not in degrees: degrees[nid1] = { "in" : 0, "out" : 0 } if nid2 not in degrees: degrees[nid2] = { "in" : 0, "out" : 0 } if bi: degrees[nid1]["in"] += 1 degrees[nid1]["out"] += 1 degrees[nid2]["in"] += 1 degrees[nid2]["out"] += 1 else: degrees[nid1]["out"] += 1 degrees[nid2]["in"] += 1 return degrees
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 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 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 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_link_ids(): value = None if attribute == "node1": value = graphiti.get_link_node1(eid) elif attribute == "node2": value = graphiti.get_link_node2(eid) else: value = graphiti.get_link_attribute(eid, attribute) if value is None: f("edge", eid, None) else: value_str = str(value) f("edge", eid, r.match(value_str))
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_edges_by_type(): graphiti.set_attribute("graphiti:space:linkmode", "string", "edge_color") ids = graphiti.get_link_ids() colors = dict() for id in ids: type = graphiti.get_link_attribute(id, "type") if type == None: print("Edge " + 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_link_attribute(id, "graphiti:space:color", "vec3", color)
def randomize_edge_width(): for id in graphiti.get_link_ids(): width = random.uniform(0.0, 5.0) graphiti.set_link_attribute(id, "graphiti:space:width", "float", str(width))
def randomize_edge_activity(): for id in graphiti.get_link_ids(): activity = random.uniform(0.0, 2.0) graphiti.set_link_attribute(id, "graphiti:space:activity", "float", str(activity))
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 show_edge_direction(): for id in graphiti.get_link_ids(): graphiti.set_link_attribute(id, "og:space:icon", "string", "styles/triangles")
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 randomize_edge_icons(): icons = glob.glob("./Resources/SpaceView/EdgeStyles/*.png") print(icons) for id in graphiti.get_link_ids(): icon = "styles/" + random.choice(icons).split("/")[-1][:-4].lower() graphiti.set_link_attribute(id, "graphiti:space:icon", "string", icon)