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 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 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 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]))