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