Beispiel #1
0
def color_neighbors():
    graphiti.set_attribute("graphiti:space:linkmode", "string", "node_color")

    if graphiti.count_selected_nodes() == 0:
        print("Please select a node !")
        return
    
    selected = graphiti.get_selected_node(0)
    graph = std.load_nx_graph()
    neighbors = graph.neighbors(selected)
    graphiti.set_node_attribute(selected, "graphiti:space:color", "vec3", "0.0 1.0 1.0")
    for node in neighbors:
        graphiti.set_node_attribute(node, "graphiti:space:lod", "float", "1.0")
        graphiti.set_node_attribute(node, "graphiti:space:color", "vec3", "0.0 1.0 1.0")
Beispiel #2
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))
Beispiel #3
0
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")
Beispiel #4
0
def show_connected_components():

    graphiti.set_attribute("graphiti:space:linkmode", "string", "node_color")

    graph = std.load_nx_graph()
    cc = nx.connected_components(graph)

    for list in cc:
        r = random.random()
        g = random.random()
        b = random.random()
        color = str(r) + " " + str(g) + " " + str(b) 
        
        for node in list:
            graphiti.set_node_attribute(node, "graphiti:space:color", "vec3", color)
Beispiel #5
0
def show_low_degrees():
    graphiti.set_attribute("graphiti:space:linkmode", "string", "node_color")

    graph = std.load_nx_graph()
    max_degree = max(nx.degree(graph).values())
    for n in graph.nodes(data = True):
        deg = nx.degree(graph, n[0])
        tint = 0.3 + 0.9 * (1.0 - float(deg) / float(max_degree))

        color = graphiti.get_node_attribute(n[0], "graphiti:space:color")
        color[0] = tint * color[0]
        color[1] = tint * color[1]
        color[2] = tint * color[2]
        c = str(color[0]) + " " + str(color[1]) + " " + str(color[2])

        graphiti.set_node_attribute(n[0], "graphiti:space:color", "vec3", c)
Beispiel #6
0
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]))
Beispiel #7
0
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)
Beispiel #8
0
def color_nodes_by_nominal_attribute(attribute_name):

    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, attribute_name)
        if type == None:
            print("Node " + str(id) + " has no <" + attribute_name + "> 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)
Beispiel #9
0
def conic_layout():
    graph = std.load_nx_graph()

    graphiti.set_attribute("graphiti:space:linkmode", "string", "node_color")

    sorted_degrees = sorted(nx.degree(graph).values())
    max_degree = sorted_degrees[-1]

    degree_nodes = {}
    for n in graph.nodes(data = True):
        degree = nx.degree(graph, n[0])
        if degree in degree_nodes:
            degree_nodes[degree].append(n[0])
        else:
            degree_nodes[degree] = [n[0]]

    max_radius = 30.0
    max_height = 20
    for n in graph.nodes(data = True):
        degree = nx.degree(graph, n[0])

        nodes = degree_nodes[degree]

        radius = 1.0 + max_radius * float(1.0 - float(degree) / float(max_degree))
        alpha = 2.0 * math.pi * random.random() #float(nodes.index(n[0])) / float(len(nodes)) 
        # 3D
        # beta = 2.0 * math.pi * random.random() #float(nodes.index(n[0])) / float(len(nodes)) 

        x = radius * math.cos(alpha)
        y = max_height * float(degree) / float(max_degree)
        z = radius * math.sin(alpha)
        # 3D
        # x = radius * math.sin(alpha) * math.cos(beta)
        # y = radius * math.sin(alpha) * math.sin(beta)
        # z = radius * math.cos(alpha)

        graphiti.set_node_attribute(n[0], "graphiti:space:position", "vec3", str(x) + " " + str(y) + " " + str(z))
Beispiel #10
0
def on_idle():

    global radius
    global radius_speed

    global alpha
    global alpha_speed

    global beta
    global beta_speed

    hand_position = None
    hand_direction = None
    hand_velocity = None
    box = None
    confidence = None
    grab = None

    mutex.acquire()
    try:
        if "position" in leap_context:
            hand_position = leap_context["position"]
        if "direction" in leap_context:
            hand_direction = leap_context["direction"]
        if "velocity" in leap_context:
            hand_velocity = leap_context["velocity"]
        if "box" in leap_context:
            box = leap_context["box"]
        if "confidence" in leap_context:
            confidence = leap_context["confidence"]
        if "grab" in leap_context:
            grab = leap_context["grab"]
    finally:
        mutex.release()

    camera_position = graphiti.get_attribute("og:space:camera:position")

    cam_pos = Leap.Vector()
    cam_pos.x = camera_position[0]
    cam_pos.y = camera_position[1]
    cam_pos.z = camera_position[2]

    alpha_acceleration = 0.0
    beta_acceleration = 0.0
    radius_acceleration = 0.0
    action = ""
    x_action = ""
    y_action = ""
    z_action = ""
    if not (hand_position is None or hand_direction is None or hand_velocity is None or box is None or confidence < 0.1):

        hand_position = box.normalize_point(hand_position)
        hand_velocity = box.normalize_point(hand_velocity)

        hand_pos = Leap.Vector()
        hand_pos.x = 2.0 * hand_position.z - 1.0
        hand_pos.y = -(2.0 * hand_position.y - 1.0)
        hand_pos.z = 2.0 * hand_position.x - 1.0

        if hand_pos.z > 0.0:
            z_action = "Right "
            z_sign = 1.0
        elif hand_pos.z < -0.0:
            z_action = "Left "
            z_sign = -1.0
        else:
            z_sign = 0.0

        if hand_pos.y > 0.0:
            y_action = "Down "
            y_sign = 1.0
        elif hand_pos.y < -0.0:
            y_sign = -1.0
            y_action = "Up "
        else:
            y_sign = 0.0

        if hand_pos.x > 0.0:
            x_action = " Back"
            x_sign = 1.0
        elif hand_pos.x < -0.0:
            x_action = " Front"
            x_sign = -1.0
        else:
            x_sign = 0.0

        threshold = 0.45
        if abs(hand_pos.z) >= threshold:
            action += z_action
            hand_pos.z = (1.0 / threshold) * (hand_pos.z - z_sign * threshold)
            alpha_acceleration = z_sign * (abs(hand_pos.z * hand_pos.z)) / 300

        threshold = 0.3
        if abs(hand_pos.y) >= threshold:
            action += y_action
            hand_pos.y = (1.0 / threshold) * (hand_pos.y - y_sign * threshold)
            beta_acceleration = -y_sign * 0.1

        if abs(hand_pos.x) >= threshold:
            action += x_action
            hand_pos.x = (1.0 / threshold) * (hand_pos.x - x_sign * threshold)
            radius_acceleration = x_sign *  0.1

        if len(action) == 0:
            action += "Neutral"

    if grab == 1.0:
        alpha_speed = 0.0
        beta_speed = 0.0
        radius_speed = 0.0
        alpha_acceleration = 0.0
        beta_acceleration = 0.0
        radius_acceleration = 0.0
        graphiti.set_attribute("og:space:title", "string", "Freeze")
        return

    alpha_speed += alpha_acceleration
    alpha += alpha_speed

    beta_speed += beta_acceleration
    beta += beta_speed 

    radius_speed += radius_acceleration
    radius += radius_speed

    max_beta = 100.0
    min_beta = - 100.0
    if beta > max_beta:
        beta = max_beta
    if beta < min_beta:
        beta = min_beta

    max_radius = 200
    min_radius = 10
    if radius > max_radius:
        radius = max_radius
    if radius < min_radius:
        radius = min_radius

    cam_pos.x = radius * math.cos(alpha)
    cam_pos.y = beta
    cam_pos.z = radius * math.sin(alpha)

    graphiti.set_attribute("og:space:camera:position", "vec3", "{0} {1} {2}".format(cam_pos.x, cam_pos.y, cam_pos.z))
    graphiti.set_attribute("og:space:title", "string", action)
    graphiti.set_attribute("og:space:camera:target", "vec3", "0.0 {0} 0.0".format(cam_pos.y))

    alpha_speed = alpha_speed * 0.8
    beta_speed = beta_speed * 0.8
    radius_speed = radius_speed * 0.8
Beispiel #11
0
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")
Beispiel #12
0
def show_debug():
    flag = graphiti.get_attribute("og:space:debug")
    graphiti.set_attribute("og:space:debug", "bool", str(not flag))
Beispiel #13
0
def load_json(json_filename):
    nodes = {}
    edges = {}
    global node_attributes
    global edge_attributes

    print ("Loading \"" + json_filename + "\" ...")

    import json
    with open(json_filename, "r") as infile:
        data = json.load(infile)

    print("Loading meta information ...")
    # TODO : Find a more generic way of doing this
    if "meta" in data:
        if "title" in data["meta"].keys():
            if data["meta"]["title"] is not None:
                graphiti.set_attribute("raindance:space:title", "string", data["meta"]["title"])

    if "attributes" in data:
        for key in data["attributes"].keys():
            if key in reserved_attributes:
                continue
            att_info = get_attribute_info(data["attributes"][key])
            if att_info is None:
                print("Error: Couldn't parse key '" + key + "' with value '" + str(data["attributes"][key]) + "'!")
                continue
            graphiti.set_attribute(key, att_info[0], att_info[1])

    print(". Loading nodes ...")
    for n in data["nodes"]:
        n = prepare_node(n)

        label = ""
        if "label" in n:
            label = n["label"].encode("utf-8")
        
        nid = graphiti.add_node(label)
        nodes[n["id"]] = nid

        for key in n.keys():
            if key in reserved_attributes:
                continue
            att_info = get_attribute_info(n[key])
            if att_info is None:
                print("Error: Couldn't parse key '" + key + "' with value '" + str(n[key]) + "'!")
                continue
            graphiti.set_node_attribute(nid, key, att_info[0], att_info[1])

    print(". Loading edges ...")
    for e in data["edges"]:
        e = prepare_edge(e)

        if "src" in e:
            eid = graphiti.add_edge(nodes[e["src"]], nodes[e["dst"]])
        else:
            eid = graphiti.add_edge(nodes[e['source']], nodes[e['target']])
        edges[e["id"]] = eid

        for key in e.keys():
            if key in reserved_attributes:
                continue
            att_info = get_attribute_info(e[key])
            if att_info is None:
                print("Error: Couldn't parse key '" + key + "' with value '" + str(e[key]) + "'!")
                continue
            graphiti.set_edge_attribute(eid, key, att_info[0], att_info[1])

    if "timeline" in data:
        print(". Loading timeline ...")
        for c in data["timeline"]:
            # TODO : Get rid of this translation phase when possible.
            if c[1].startswith("graph:"):
                if c[1] in ["graph:remove_node", "graph:set_node_attribute"]:
                    c[2]["id"] = nodes[c[2]["id"]]
                elif c[1] in ["graph:remove_edge", "graph:set_edge_attribute"]:
                    c[2]["id"] = edges[c[2]["id"]]
                elif c[1] in ["graph:add_edge"]:
                    c[2]["src"] = nodes[c[2]["src"]]
                    c[2]["dst"] = nodes[c[2]["dst"]]
            graphiti.send_command(c[0], c[1], c[2])

    print("Done.")
Beispiel #14
0
def on_idle():

    global radius
    global radius_speed

    global alpha
    global alpha_speed

    global beta
    global beta_speed

    hand_position = None
    hand_direction = None
    hand_velocity = None
    box = None
    confidence = None
    grab = None

    mutex.acquire()
    try:
        if "position" in leap_context:
            hand_position = leap_context["position"]
        if "direction" in leap_context:
            hand_direction = leap_context["direction"]
        if "velocity" in leap_context:
            hand_velocity = leap_context["velocity"]
        if "box" in leap_context:
            box = leap_context["box"]
        if "confidence" in leap_context:
            confidence = leap_context["confidence"]
        if "grab" in leap_context:
            grab = leap_context["grab"]
    finally:
        mutex.release()

    camera_position = graphiti.get_attribute("og:space:camera:position")

    cam_pos = Leap.Vector()
    cam_pos.x = camera_position[0]
    cam_pos.y = camera_position[1]
    cam_pos.z = camera_position[2]

    alpha_acceleration = 0.0
    beta_acceleration = 0.0
    radius_acceleration = 0.0
    action = ""
    x_action = ""
    y_action = ""
    z_action = ""
    if not (hand_position is None or hand_direction is None
            or hand_velocity is None or box is None or confidence < 0.1):

        hand_position = box.normalize_point(hand_position)
        hand_velocity = box.normalize_point(hand_velocity)

        hand_pos = Leap.Vector()
        hand_pos.x = 2.0 * hand_position.z - 1.0
        hand_pos.y = -(2.0 * hand_position.y - 1.0)
        hand_pos.z = 2.0 * hand_position.x - 1.0

        if hand_pos.z > 0.0:
            z_action = "Right "
            z_sign = 1.0
        elif hand_pos.z < -0.0:
            z_action = "Left "
            z_sign = -1.0
        else:
            z_sign = 0.0

        if hand_pos.y > 0.0:
            y_action = "Down "
            y_sign = 1.0
        elif hand_pos.y < -0.0:
            y_sign = -1.0
            y_action = "Up "
        else:
            y_sign = 0.0

        if hand_pos.x > 0.0:
            x_action = " Back"
            x_sign = 1.0
        elif hand_pos.x < -0.0:
            x_action = " Front"
            x_sign = -1.0
        else:
            x_sign = 0.0

        threshold = 0.45
        if abs(hand_pos.z) >= threshold:
            action += z_action
            hand_pos.z = (1.0 / threshold) * (hand_pos.z - z_sign * threshold)
            alpha_acceleration = z_sign * (abs(hand_pos.z * hand_pos.z)) / 300

        threshold = 0.3
        if abs(hand_pos.y) >= threshold:
            action += y_action
            hand_pos.y = (1.0 / threshold) * (hand_pos.y - y_sign * threshold)
            beta_acceleration = -y_sign * 0.1

        if abs(hand_pos.x) >= threshold:
            action += x_action
            hand_pos.x = (1.0 / threshold) * (hand_pos.x - x_sign * threshold)
            radius_acceleration = x_sign * 0.1

        if len(action) == 0:
            action += "Neutral"

    if grab == 1.0:
        alpha_speed = 0.0
        beta_speed = 0.0
        radius_speed = 0.0
        alpha_acceleration = 0.0
        beta_acceleration = 0.0
        radius_acceleration = 0.0
        graphiti.set_attribute("og:space:title", "string", "Freeze")
        return

    alpha_speed += alpha_acceleration
    alpha += alpha_speed

    beta_speed += beta_acceleration
    beta += beta_speed

    radius_speed += radius_acceleration
    radius += radius_speed

    max_beta = 100.0
    min_beta = -100.0
    if beta > max_beta:
        beta = max_beta
    if beta < min_beta:
        beta = min_beta

    max_radius = 200
    min_radius = 10
    if radius > max_radius:
        radius = max_radius
    if radius < min_radius:
        radius = min_radius

    cam_pos.x = radius * math.cos(alpha)
    cam_pos.y = beta
    cam_pos.z = radius * math.sin(alpha)

    graphiti.set_attribute(
        "og:space:camera:position", "vec3",
        "{0} {1} {2}".format(cam_pos.x, cam_pos.y, cam_pos.z))
    graphiti.set_attribute("og:space:title", "string", action)
    graphiti.set_attribute("og:space:camera:target", "vec3",
                           "0.0 {0} 0.0".format(cam_pos.y))

    alpha_speed = alpha_speed * 0.8
    beta_speed = beta_speed * 0.8
    radius_speed = radius_speed * 0.8