예제 #1
0
def get_graph():
    response_builder = ResponseBuilder()
    nodes = response_builder.return_node_list(d_social_network_graph)
    edges = response_builder.return_edge_list(d_social_network_graph)

    response = dict()
    response["nodes"] = nodes
    response['edges'] = edges

    return jsonify(response)
예제 #2
0
 def Switch(self, ps):
     #Objekt vom ResponseBuilder erstellen
     rb = ResponseBuilder()
     #Auf verwendete Methode testen und entsprechende ResponseBuilder-Methode aufrufen
     if ps.Method == "GET":
         response = rb.Getresponse(ps)
     elif ps.Method == "PUT":
         response = rb.Putresponse(ps)
     else:
         response = rb.Errorresponse(ps)
     #Vom ResponseBuilder erstellte Response an MainDatei weitergeben
     return response
예제 #3
0
def get_school_communities():
    multi_school_community_graph = louvain.find_partition(d_social_network_graph, louvain.CPMVertexPartition,
                                       resolution_parameter=0.0005)
    for idx, community in enumerate(multi_school_community_graph):
        for node in community:
            v = d_social_network_graph.vs[node]
            v["groupId"] = idx

    response_builder = ResponseBuilder()
    nodes = response_builder.return_node_list(d_social_network_graph)
    edges = response_builder.return_edge_list(d_social_network_graph)

    response = dict()
    response["nodes"] = nodes
    response['edges'] = edges

    return jsonify(response)
예제 #4
0
def get_interest_based_communities():

    target_school = request.args.get('school')
    interests = request.args.get('interests').split(',')

    # Level 1 - School Community Detection
    multi_school_community_graph = louvain.find_partition(d_social_network_graph, louvain.CPMVertexPartition,
                                                        resolution_parameter=0.0005)
    school_community_id_map = {}
    for community_id,community in enumerate(multi_school_community_graph):
        min_check = len(community)*0.1
        if min_check >= 1:
            for i,node in enumerate(community):
                school_name = d_social_network_graph.vs[node]['school']
                if school_name in school_community_id_map:
                    school_community_id_map[school_name] += ","+str(community_id)
                else:
                    school_community_id_map[school_name] = str(community_id)
                i += 1
                if i > min_check:
                    break

    for school, community_id in school_community_id_map.iteritems():
        community_ids = community_id.split(",")
        school_community_id_map[school] = max(set(community_ids), key=community_ids.count)

    target_community_id = school_community_id_map.get(target_school)
    school_nodes = multi_school_community_graph[int(target_community_id)]

    # Level 2 Interest-based Community Detection
    school_community_graph = Graph()
    school_community_graph.to_directed()

    interest_based_community_graph = Graph()
    interest_based_community_graph.to_directed()

    updated_school_nodes = []

    for node in school_nodes:
        school_community_graph.add_vertex(d_social_network_graph.vs[node].index)
        updated_school_nodes.append(d_social_network_graph.vs[node])

    updated_school_node_ids=[]

    for i,v in enumerate(school_community_graph.vs):
        v["id"] = updated_school_nodes[i]["id"]
        v["label"] = updated_school_nodes[i]["label"]
        v["groupId"] = -1
        v['interestedNode']=False
        v['influentialNode']=False
        v["interest"] = updated_school_nodes[i]["interest"]
        updated_school_node_ids.append(updated_school_nodes[i]["id"])

    for e in d_social_network_graph.es:
        src = e.source
        dest = e.target
        if d_social_network_graph.vs[src]['id'] in updated_school_node_ids \
                and d_social_network_graph.vs[dest]['id'] in updated_school_node_ids:

            id_src = d_social_network_graph.vs[src]['id']
            id_dest = d_social_network_graph.vs[dest]['id']
            sg_src = school_community_graph.vs.find(id=id_src).index
            sg_dest = school_community_graph.vs.find(id=id_dest).index
            school_community_graph.add_edge(sg_src, sg_dest)

    interested_nodes = []

    for v in school_community_graph.vs:
        is_interested = False
        for interest in interests:
            if interest in v["interest"]:
                is_interested = True
            else:
                is_interested = False
                break
        if is_interested:
            v["interestedNode"] = True
            interested_nodes.append(v)


    # Compute shortest paths among sub-community nodes usinf Dijkstra's shortest path algorithm
    influenceFactorAdjGrid = d_social_network_graph.shortest_paths_dijkstra(interested_nodes, interested_nodes, None, OUT)
    interest_based_community_graph = Graph.Weighted_Adjacency(influenceFactorAdjGrid, ADJ_DIRECTED, "weight", False)
    interest_based_community_graph.simplify(combine_edges='sum')

    i = 0
    for v in interest_based_community_graph.vs:
        node = interested_nodes[i]
        v["id"] = node["id"]
        i += 1

    influentials_object = get_influential_node(interest_based_community_graph, school_community_graph)
    if len(influentials_object) > 0:
        for node_id in influentials_object[len(influentials_object)-1][2]:
            node_id["influentialNode"] = True

    response_builder = ResponseBuilder()
    nodes = response_builder.return_node_list(school_community_graph)
    edges = response_builder.return_edge_list(school_community_graph)

    # Json object for coverage graph
    nodes_coverage = []
    for item in influentials_object:
        temp_json = dict()
        temp_json["no_of_influencers"] = item[0]
        temp_json["coverage"] = item[1]
        temp_json["node_info"] = []
        for node in item[2]:
            curr_node = dict()
            curr_node["id"] = node["id"]
            curr_node["interest"] = node["interest"]
            curr_node["influentialNode"] = node["influentialNode"]
            curr_node["groupId"] = node["groupId"]
            curr_node["influence"] = node["influence"]
            curr_node["interestedNode"] = node["interestedNode"]
            curr_node["name"] = node["name"]
            temp_json["node_info"].append(curr_node)
        nodes_coverage.append(temp_json)

    response = dict()
    response["nodes"] = nodes
    response['edges'] = edges
    response['coverageObject'] = nodes_coverage

    return jsonify(response)