def __init__(self, fileName1, fileName2, overlap_filename, examples,
                 directed):
        self.rootDir = self.getPairsDir(fileName1) + "pairs"
        self.fileName1 = fileName1
        self.fileName2 = fileName2

        #print os.path.abspath(overlap_filename)
        self.df_overlap = pd.read_csv(os.path.abspath(overlap_filename),
                                      index_col=None,
                                      header=None)
        self.df_overlap.columns = ['node_id', 'degree']

        self.examples = examples
        self.directed = directed

        self.G1 = self.getGraph(fileName1)
        print "[Load] G1."
        self.G2 = self.getGraph(fileName2)
        print "[Load] G2."

        self.ndd1HG1 = snap.TIntStrH()
        self.ndd2HG1 = snap.TIntStrH()

        self.ndd1HG2 = snap.TIntStrH()
        self.ndd2HG2 = snap.TIntStrH()

        self.graph1Name = self.getGraphName(fileName1)
        self.graph2Name = self.getGraphName(fileName2)
Exemplo n.º 2
0
def snap_load(filename="", src_path="", directed=False):
    graph_extension = ".graph"
    label_extension = ".label"
    if filename == "":
        graph_extension = ""

    if os.path.isfile(src_path + filename + graph_extension):
        g_path = src_path + filename + graph_extension
        FIn = snap.TFIn(g_path)
        if directed:
            G = snap.TNGraph.Load(FIn)
        else:
            G = snap.TUNGraph.Load(FIn)
    elif os.path.isfile(src_path + filename + ".gml") or os.path.isfile(src_path + filename + ".gpickle"):
        graph_converter.gml_gpickle_to_snap_graph(filename, src_path)
        FIn = snap.TFIn(src_path + filename + graph_extension)
        if directed:
            G = snap.TNGraph.Load(FIn)
        else:
            G = snap.TUNGraph.Load(FIn)
    if os.path.isfile(src_path + filename + label_extension):
        FIn = snap.TFIn(src_path + filename + label_extension)
        labels = snap.TIntStrH()
        labels.Load(FIn)
    else:
        labels = snap.TIntStrH()

    if labels:
        return G, labels
    else:
        return G
def colorifyNetworkSnap(pajekInput, pajekOutput, myDatabase=''):
    """
    This functions takes in a pajek file and output a pajek file. It assign a color label for each node
    """

    #initialize an graph
    G = snap.LoadPajek_PUNGraph(pajekInput)

    #annotating netowrk nodes with raga labels and edges with distances
    cmd1 = "select raagaId from file where id = (select file_id from pattern where id =%d)"
    NIdColorH = snap.TIntStrH()
    NIdLabelH = snap.TIntStrH()

    try:
        con = psy.connect(database=myDatabase, user=myUser)
        cur = con.cursor()
        print "Successfully connected to the server"

        for NI in G.Nodes():
            nid = NI.GetId()
            cur.execute(cmd1 % (nid))
            ragaId = cur.fetchone()[0]
            print ragaId
            NIdLabelH[nid] = str(ragaId)
            NIdColorH[nid] = colors[str(ragaId)]

    except psy.DatabaseError, e:
        print 'Error %s' % e
        if con:
            con.rollback()
            con.close()
        sys.exit(1)
Exemplo n.º 4
0
    def __init__(self, graph, layout, printLabel=True):

        # Degree of Edge: Role, Color, Weight, iterator
        self.groups = {
            0: [
                ['ClaimID', 'yellow', 0.4, 0],
            ],
            1: [
                ['Doctor', 'red', 0.3, 0],
                ['Body Shop', 'yellow', 0.3, 0],
                ['Towing company', 'orange', 0.3, 0],
                ['Inspector', 'blue', 0.3, 0],
                ['Lawyer', 'purple', 0.2, 0],
            ],
            2: [
                ['Claimant', 'green', 0.7, 0],
            ]
        }

        self.G = graph
        self.layout = layout
        self.printLabel = printLabel
        self.labels = snap.TIntStrH()
        self.NIdColorH = snap.TIntStrH()
        self.nameList = {}  # nameList is only been assigned once
def q2_3():
    print("============")
    g = load_graph_weights()
    v_mat = cal_initial_feature(g)
    for k in range(2):
        v_mat = aggregrate(v_mat, g)
    v_9 = v_mat[9]

    cos_sim = [(i, cal_cos_sim(v_9, v)) for i, v in enumerate(v_mat)]
    cos_sim.sort(key=lambda y: y[1], reverse=True)
    cos_sims = [u[1] for u in cos_sim]
    plt.hist(cos_sims, bins=20)
    plt.title("distribution of cosine similarity")
    plt.show()

    node_1, sim_1 = find_node(0, 0.05, cos_sim)
    subg_1 = get_2_nd_subgraph(node_1, g)
    subg_1_h = snap.TIntStrH()
    subg_1_h[node_1] = "blue"
    subg_1 = snap.ConvertSubGraph(snap.PUNGraph, g, subg_1)
    snap.DrawGViz(subg_1, snap.gvlNeato, "subgraph_1.png",
                  "subgraph 1 sim: {0}".format(round(sim_1,
                                                     2)), True, subg_1_h)

    node_2 = 9  #find_node(0.4, 0.45, cos_sim)
    subg_2 = get_2_nd_subgraph(node_2, g)
    subg_2_h = snap.TIntStrH()
    subg_2_h[node_2] = "blue"
    subg_2 = snap.ConvertSubGraph(snap.PUNGraph, g, subg_2)
    snap.DrawGViz(subg_2, snap.gvlNeato, "subgraph_center_9.png",
                  "subgraph_center_9", True, subg_2_h)

    node_3, sim_3 = find_node(0.6, 0.65, cos_sim)
    subg_3 = get_2_nd_subgraph(node_3, g)
    subg_3_h = snap.TIntStrH()
    subg_3_h[node_3] = "blue"
    subg_3 = snap.ConvertSubGraph(snap.PUNGraph, g, subg_3)
    snap.DrawGViz(subg_3, snap.gvlNeato, "subgraph_3.png",
                  "subgraph 3 sim: {0}".format(round(sim_3,
                                                     2)), True, subg_3_h)

    node_4, sim_4 = find_node(0.9, 0.95, cos_sim)
    subg_4 = get_2_nd_subgraph(node_4, g)
    subg_4_h = snap.TIntStrH()
    subg_4_h[node_4] = "blue"
    subg_4 = snap.ConvertSubGraph(snap.PUNGraph, g, subg_4)
    snap.DrawGViz(subg_4, snap.gvlNeato, "subgraph_4.png",
                  "subgraph 4 sim: {0}".format(round(sim_4,
                                                     2)), True, subg_4_h)

    print("============")
 def genNDDH(self, G):
     ndd1H = snap.TIntStrH()
     ndd2H = snap.TIntStrH()
     n = G.GetNodes()
     print "# of nodes: %d" % n
     i = 0
     for node in G.Nodes():
         nddV = self.get1HNDD(G, node)
         ndd1H[node.GetId()] = self.asString(nddV)
         nddV = self.get2HNDD(G, node)
         ndd2H[node.GetId()] = self.asString(nddV)
         i += 1
         progress = float(i) / n
         print "[progress] %f\n" % progress
     return ndd1H, ndd2H
Exemplo n.º 7
0
    def createVectLabel(self, attrbToLabel, vertice, xmlFileName):

        root = self.startRootNetwork(xmlFileName)
        temp = vertice.split('-')
        dictLabel = snap.TIntStrH()
        for metaNetwork in root.getchildren():
            for nodesType in metaNetwork.getchildren():
                for nodeclass in nodesType.getchildren():

                    nodeclassType = nodeclass.get("type")

                    if (nodeclassType != temp[0]):
                        continue

                    for node in nodeclass.getchildren():

                        verticeName = node.get("id")

                        if attrbToLabel == "mainId":
                            dictLabel[int(verticeName)] = verticeName
                            continue

                        for nodeProperties in node.getchildren():
                            for property in nodeProperties.getchildren():

                                # To include only attributes that the were selected in the var self.attribSelecteds
                                name = property.get('name')
                                value = property.get('value')

                                if name == attrbToLabel:
                                    dictLabel[int(verticeName)] = value

        return dictLabel
Exemplo n.º 8
0
    def __init__(self, filename, mVals, pVal, tau):
        self.fileName = fileName
        self.mVals = mVals
        self.pVal = pVal
        self.pVals = []
        self.pVals.append(pVal)
        self.pVals.append(1 - pVal)
        self.tau = tau
        self.nLH = snap.TIntStrH()
        self.lblNH = snap.TStrIntH()  # Node count with attached label
        self.lblEH = snap.TIntIntH()  # Edge count with attached src dst labels

        self.RH = snap.TIntFltPrH()
        self.BH = snap.TIntFltPrH()

        self.cRV = snap.TIntV()
        self.cBV = snap.TIntV()

        self.G = self.getGraph(snap.PUNGraph)
        self.NG = snap.TNEANet()
        self.graphName = self.getGraphName()
        self.rootDir = self.getParentDir(self.fileName)
        self.absrootDir = os.path.abspath(self.rootDir)

        self.cR_count = 0
        self.cB_count = 0

        self.RH_count = 0
        self.BH_count = 0
Exemplo n.º 9
0
def create_rnd_trees(size, number, filename, dst_path, labeled=False, seed=1):
    random.seed(seed)
    for i in range(number):
        G = nx.random_tree(size, seed + i)
        GSnap = snap.TUNGraph()

        if labeled:
            labels = snap.TIntStrH()
        for node in G.nodes(data=True):
            id = node[0]
            if labeled:
                node_label = node[1]['predicate']
                labels[int(id)] = str(node_label)
            GSnap.AddNode(int(id))

        for edge in G.edges():
            GSnap.AddEdge(int(edge[0]), int(edge[1]))

        FOut = snap.TFOut(dst_path + filename + "_" +
                          str(i).zfill(math.ceil(math.log10(number + 1))) +
                          ".graph")
        GSnap.Save(FOut)
        FOut.Flush()

        if labeled:
            FOut = snap.TFOut(dst_path + filename + "_" +
                              str(i).zfill(math.ceil(math.log10(number + 1))) +
                              ".labels")
            labels.Save(FOut)
            FOut.Flush()
Exemplo n.º 10
0
def gml_gpickle_to_snap_graph(filename, src_path, dst_path=""):
    if dst_path == "":
        dst_path = src_path

    try:
        G = nx.read_gpickle(src_path + filename + ".gpickle")
    except:
        G = nx.read_gml(src_path + filename + ".gml")

    GSnap = snap.TUNGraph()

    labels = snap.TIntStrH()
    for node in G.nodes(data=True):
        id = node[0]
        node_label = node[1]['predicate']
        labels[int(id)] = str(node_label)
        GSnap.AddNode(int(id))

    for edge in G.edges():
        GSnap.AddEdge(int(edge[0]), int(edge[1]))

    FOut = snap.TFOut(dst_path + filename + ".graph")
    GSnap.Save(FOut)

    FOut = snap.TFOut(dst_path + filename + ".labels")
    labels.Save(FOut)
    FOut.Flush()
Exemplo n.º 11
0
    def drawGraph(self,labelDict,theGraph,graphName):

        labels = snap.TIntStrH()
        for NI in theGraph.Nodes():
            thekey=self.nid2id[NI.GetId()]
            labels[NI.GetId()] = str(labelDict[thekey])
        snap.DrawGViz(theGraph, snap.gvlSfdp, graphName, " ", labels)
Exemplo n.º 12
0
def build_legend(filename):
  legend = snap.TIntStrH()
  with open(filename) as f:
    for line in f:
      tokens = line.split()
      if not tokens[0].isdigit():  continue   # Ignore the first line
      key = int(tokens[0])
      legend[key] = ' '.join(tokens[1:])
  return legend
Exemplo n.º 13
0
def main():
    graphs, names = load_networks(IN_FOLDER)
    for g, n in zip(graphs, names):
        NIdColorH = snap.TIntStrH()
        if g.GetNodes() < 1000:
            for i in g.Nodes():
                NIdColorH[i.GetId()] = "red"
            snap.DrawGViz(g, snap.gvlCirco, OUT_FOLDER + n + ".png", n, False,
                          NIdColorH)
Exemplo n.º 14
0
def DrawGraph(lower, upper, cos_sim, g, filename, title='', color='blue'):
    """
    output the graph
    """
    sub = GetSubgraph(FindNode(lower, upper, cos_sim), g)
    whole_graph = snap.TIntStrH()
    whole_graph[sub[0]] = color
    sub = snap.ConvertSubGraph(snap.PUNGraph, g, sub)
    snap.DrawGViz(sub, snap.gvlNeato, filename, title, True, whole_graph)
Exemplo n.º 15
0
def plot_subgraph(graph, nodes, output, title):
    node_ids_vector = snap.TIntV()
    labels = snap.TIntStrH()
    for pair in nodes:
        if pair[0] not in node_ids_vector:
            node_ids_vector.Add(pair[0])
            pkg = graph.GetStrAttrDatN(pair[0], "pkg")
            labels[pair[0]] = ellipsize_text(pkg, 30)
    subgraph = snap.GetSubGraph(graph, node_ids_vector)
    snap.DrawGViz(subgraph, snap.gvlNeato, output, title, labels)
Exemplo n.º 16
0
 def save_graph(self):
     labels = snap.TIntStrH()
     filled_nodes = self.assignment.keys()
     # assert(len(filled_nodes) > 0)
     for i in range(self.graph.GetNodes()):
         if i in filled_nodes:
             labels[i] = str(i) + ": " + self.assignment[i]
         else:
             labels[i] = str(i) + ": "
     snap.DrawGViz(self.graph, snap.gvlDot, "assignment.png", " ", labels)
Exemplo n.º 17
0
def GraphVisualization(G1):
    labels = snap.TIntStrH()
    # page_name = snap.TIntStrH()
    # page_type = snap.TIntStrH()

    for NI in G1.Nodes():
        labels[NI.GetId()] = str(NI.GetId())
        # page_name[NI.GetId()] = str(targets['page_name'][NI.GetId()])
        # page_type[NI.GetId()] = str(targets['page_type'][NI.GetId()])

    snap.DrawGViz(G1, snap.gvlDot, "output.png", " ", labels)
Exemplo n.º 18
0
def build_legends(filename, valuefn, delim=None):
  legend     = snap.TIntStrH()
  full_lines = { }
  with open(filename) as f:
    for line in f:
      tokens = line.split(delim)
      if not tokens[0].isdigit():  continue  # Ignore the first line
      key = int(tokens[0])
      legend[key]     = valuefn(tokens)
      full_lines[key] = tokens[1:]
  return (legend, full_lines)
Exemplo n.º 19
0
def buildNetwork(devices):
    vector = {}
    home_network = snap.TUNGraph.New()
    labels = snap.TIntStrH()
    dev_ind = 0
    for device in devices:
        dev_ind+=1
        home_network.AddNode(dev_ind)
        if dev_ind!=1:
            home_network.AddEdge(dev_ind, 1)
        labels.AddDat(dev_ind, str(device))
        vector = buildVector(device,vector)
    return home_network, labels, vector
Exemplo n.º 20
0
    def betweenCentral(self,date):

            temp = self.network
            temp.AddFltAttrN("bcentrality")

            # Colour Hash Table
            NIdColorH = snap.TIntStrH()
            purple = 0
            blue = 0
            green = 0
            yellow = 0
            red = 0
            # For every node
            for NI in temp.Nodes():

                # Get ITs betweenes centrality
                CloseCentr = snap.GetClosenessCentr(temp, NI.GetId())
                temp.AddFltAttrDatN(NI.GetId(),CloseCentr,"bcentrality")

                # Determine colour
                if CloseCentr <0.2:
                    NIdColorH[NI.GetId()] = "purple"
                    purple+=1

                elif CloseCentr <0.4:
                    NIdColorH[NI.GetId()] = "blue"
                    blue+=1

                elif CloseCentr <0.6:
                    NIdColorH[NI.GetId()] = "green"
                    green+=1

                elif CloseCentr <0.8:
                    NIdColorH[NI.GetId()] = "yellow"
                    yellow+=1

                else:
                    NIdColorH[NI.GetId()] = "red"
                    red+=1

            print"Purple:\t", purple
            print"Blue:\t" , blue
            print"Green:\t", green
            print"Yellow:", yellow
            print"Red:\t" ,red

            # Draw graph
            #snap.DrawGViz(temp, snap.gvlSfdp, "BetweenesCentrality.png", date, True, NIdColorH)


            """
def main():
    cosine_dists = get_cosine_dist()

    Graph = snap.TUNGraph.New()
    name_to_id = add_nodes(Graph)

    for pair in cosine_dists:
        if cosine_dists[pair] > 0.5:
            Graph.AddEdge(name_to_id[pair[0]], name_to_id[pair[1]])

    labels = snap.TIntStrH()
    for name in name_to_id:
        labels[name_to_id[name]] = name
    snap.DrawGViz(Graph, snap.gvlDot, OUTPUT_GRAPH, " ", labels)
Exemplo n.º 22
0
def PlotSubGraph(G,NodeID,path="./graph/"):
    Node = G.GetNI(NodeID)
    NIdV = snap.TIntV()         #subgraph nodes ID
    NIdV.Add(NodeID)
    Deg = Node.GetDeg()
    NidName = snap.TIntStrH()
    NidName[NodeID] = str(NodeID)
    for i in range(Deg):
        NbrID = Node.GetNbrNId(i)
        NIdV.Add(NbrID)
        NidName[NbrID] = str(NbrID)
        SubGraph = snap.GetSubGraph(G, NIdV)
    snap.DrawGViz(SubGraph,snap.gvlDot,path+"subgraph"+str(NodeID)+".png","SubGraph of "+str(NodeID),NidName)    
    return 0
Exemplo n.º 23
0
def visualize_graph(file_egdes, file_img, list_comunity):

    graph = snap.LoadEdgeList(snap.PUNGraph, file_egdes, 0, 1, '\t')
    NIdColorH = snap.TIntStrH()
    colors = ["red", "yellow", "brown", "blue", "green", "pink", "indigo", "antiquewhite","chocolate", "purple", "sienna4", "powderblue", "violet" ]
    num_color = len(colors)
    for i, comunity in enumerate(list_comunity):
        if len(comunity) ==1:
            NIdColorH[comunity[0]]= "white" 
        else:
            index = i%num_color
            for node in comunity:
                NIdColorH[node] = colors[index]

    snap.DrawGViz(graph, snap.gvlNeato, file_img, "graph top 200 nodes", True, NIdColorH)
Exemplo n.º 24
0
    def getNodeIdLabel(self, G):
        gLH = snap.TIntStrH()
        for EI in G.Edges():
            srcId = EI.GetSrcNId()
            dstId = EI.GetDstNId()

            EI = G.GetEI(srcId, dstId)

            srcAttr = "srcLabel"
            srcAttrVal = G.GetStrAttrDatE(EI, srcAttr)

            dstAttr = "dstLabel"
            dstAttrVal = G.GetStrAttrDatE(EI, dstAttr)

            gLH[srcId] = srcAttrVal
            gLH[dstId] = dstAttrVal
        return gLH
Exemplo n.º 25
0
def create_graph_from_likes(filename):
    myfile = open(filename, "r")
    all_data = json.load(myfile)

    # loading part of the graph
    count = 0
    data = {}
    for post, likes in all_data.items():
        if count == 100: break
        count += 1
        data[post] = likes

    graph = snap.TUNGraph.New()
    names = snap.TIntStrH()

    posts_ints = {}
    users_ints = {}
    p = 0
    k = 3000
    for post, likes in data.items():
        posts_ints[post] = p
        graph.AddNode(p)
        for like in likes:
            # user_int_id = -1
            if like['id'] in users_ints.keys():
                user_int_id = users_ints[like['id']]
                names[user_int_id] = like['name'].encode("utf8")
            else:
                users_ints[like['id']] = k
                user_int_id = k
                graph.AddNode(user_int_id)
                k += 1
            graph.AddEdge(p, user_int_id)
        p += 1
    print "G1: Nodes %d, Edges %d" % (graph.GetNodes(), graph.GetEdges())

    return graph
Exemplo n.º 26
0
for item in b:
	if item not in proxy:
		proxy[item]=count
		rev_proxy[count]=item
		count=count+1
	if b[item] not in proxy:
		proxy[b[item]]=count
		rev_proxy[count]=b[item]
		count=count+1

for item in proxy:
	n1.AddNode(proxy[item])

for item in b:
	if b[item] != 0:
		n1.AddEdge(proxy[b[item]],proxy[item])
		
NIdColorH = snap.TIntStrH()
OutDegV = snap.TIntPrV()
snap.GetNodeOutDegV(n1, OutDegV)

for item in OutDegV:
	if item.GetVal2()>=3:
		NIdColorH[item.GetVal1()]="green"
		print rev_proxy[item.GetVal1()], item.GetVal2()
	if item.GetVal2()==0:
		NIdColorH[item.GetVal1()]="black"

snap.DrawGViz(n1, snap.gvlDot, "graph3.png", "graph 1",False,NIdColorH)		
snap.DrawGViz(n1, snap.gvlNeato, "graph1.png", "graph 1",False,NIdColorH)
snap.DrawGViz(n1, snap.gvlCirco, "graph2.png", "graph 2",False,NIdColorH)
Exemplo n.º 27
0
print("Iter < Hash.EndI", Iter < Hash.EndI())
#while Iter < Hash.EndI():
while not Iter.IsEnd():
    Key = Iter.GetKey()
    Value = Iter.GetDat()
    print(Key, Value)

    Iter.Next()

for item in Hash:
    print(item, Hash[item])

print("---------- 2 ---------")

h = snap.TIntStrH()

h[5] = "five"
h[3] = "thre"
h[9] = "nine"
h[6] = "six"
h[1] = "one"

print(h.Len())

print("h[3] =", h[3])

h[3] = "three"

print("h[3] =", h[3])
Exemplo n.º 28
0
def runSIR(G, init_adopter, p, tl, initial_list=None):
    number_node = G.GetNodes()
    number_edge = G.GetEdges()
    if initial_list is None:
        initial_list = random.sample(range(number_node), init_adopter)

    # data to return
    infectious_num = []
    removed_num = []
    final_states = []

    # 0: Susceptible, 1:Infectious, 2: Removed
    labels = snap.TIntStrH()
    for NI in G.Nodes():
        nid = NI.GetId()
        state = 0
        if nid in initial_list:
            G.AddIntAttrDatN(nid, 1, "state")
            state = 1
        else:
            G.AddIntAttrDatN(nid, 0, "state")
            state = 0
        G.AddIntAttrDatN(nid, 0, "step")

    number_R = number_node - len(initial_list)
    number_echo = 0
    while number_R != number_node:
        for NI in G.Nodes():
            nid = NI.GetId()
            ival = G.GetIntAttrDatN(nid, "state")
            # if it's infectious, it will infect its neighbors.
            if ival == 1:
                for nid1 in NI.GetOutEdges():
                    att_value = G.GetIntAttrDatN(nid1, "state")

                    # with probability of p=0.2, a susceptible node will be infected.
                    if (np.random.choice(np.arange(0, 2), p=[p, 1 - p])
                            == 0) and (att_value == 0):
                        # The state 3 is temporary in order to avoid repeated infection.
                        G.AddIntAttrDatN(nid1, 3, "state")

                # update the step of infection.
                step_this = G.GetIntAttrDatN(nid, "step")
                step_this += 1
                G.AddIntAttrDatN(nid, step_this, "step")
                if step_this == tl:
                    G.AddIntAttrDatN(nid, 2, "state")

        # Count the number of susceptible or removed nodes.
        number_R_temp = 0
        removed = 0
        for NI in G.Nodes():
            nid = NI.GetId()
            ival = G.GetIntAttrDatN(nid, "state")
            if ival == 3:  # The nodes should be updated as infectious now.
                G.AddIntAttrDatN(nid, 1, "state")
                ival = 1

            if ival == 0 or ival == 2:
                number_R_temp += 1
            if ival == 2:
                removed += 1
        number_R = number_R_temp
        number_echo += 1
        infectious_num.append(number_node - number_R)
        removed_num.append(removed)
        #print(number_R)

    for NI in G.Nodes():
        nid = NI.GetId()
        ival = G.GetIntAttrDatN(nid, "state")
        step_number = G.GetIntAttrDatN(nid, "step")
        final_states.append((nid, ival, step_number))
        # print(nid, ival, step_number)

    print("One round of simulation completes.")
    return infectious_num, removed_num, final_states
Exemplo n.º 29
0
edge = []  #initializes edge
for jsonindex in range(len(jsons)):
    DstNId = userdict[destnodes[jsonindex]]  #integer source node id
    file = jsons[jsonindex]
    weights = jsons[jsonindex].values()
    rowindex = 0
    for key in file:
        short = re.search('user=(.+?)&hl', key).group(1)
        SrcNId = userdict[short]  #this should be the contents of the json
        edge.append([SrcNId, DstNId, weights[rowindex], sizeindex
                     ])  #source node, destination node, edge weight, edge id
        rowindex = rowindex + 1  #index for each keys in json
        sizeindex = sizeindex + 1  #row index of edge matrix

labels = snap.TIntStrH()
for index in range(sizeindex):
    #labels[index]=nodedict[edge[index][0]]
    labels.AddDat(index, nodedict[edge[index][0]])

#Creates network object
G1 = snap.TNEANet.New()

#Adds Nodes
for node in nodedict:
    G1.AddNode(node)

#Adds Weight Attribute
G1.AddIntAttrE('Weight')

#Adds Edges
Exemplo n.º 30
0
def hops_estimation_recursive_snap(G,
                                   H,
                                   G_labels=snap.TIntStrH(),
                                   H_labels=snap.TIntStrH(),
                                   k=0,
                                   runtime=0,
                                   labeled=True,
                                   detailed_estimation=False):
    """

    :param G: graph
    :param H: pattern graph
    :param G_labels: vector of graph labels
    :param H_labels: vector of pattern labels
    :param k: number of iterations of hops for one iteration
    :param runtime: runtime of hops for one estimation
    :param labeled: determines if graph and pattern are labeled
    :param detailed_estimation: if output should be detailed
    :return: in case of detailed estimation return estimation count and single iteration steps, else only return estimation count
    """
    estimation_values = []

    if labeled:
        pattern_root_node, root_node_predicate_name = get_pattern_root_node_snap(
            H_labels, label_frequency_histogram_snap(G_labels))

        possible_root_nodes = [
            x.GetId() for x in G.Nodes()
            if G_labels[x.GetId()] == root_node_predicate_name
        ]
    else:
        pattern_root_node = H.GetRndNId()
        possible_root_nodes = [x.GetId() for x in G.Nodes()]

    if runtime != 0:
        t_end = time.time() + runtime
        while time.time() < t_end:
            v = possible_root_nodes[random.randint(
                0,
                len(possible_root_nodes) - 1)]

            c, phi, phi_inv = find_tree_embeddings_snap(
                pattern_root_node, v, {pattern_root_node: v},
                {v: pattern_root_node}, H, H_labels, G, G_labels, labeled)
            c *= len(possible_root_nodes)
            estimation_values.append(c)
    else:
        for i in range(0, k):
            # sample root node and sample first image of u
            v = possible_root_nodes[random.randint(
                0,
                len(possible_root_nodes) - 1)]

            c, phi, phi_inv = find_tree_embeddings_snap(
                pattern_root_node, v, {pattern_root_node: v},
                {v: pattern_root_node}, H, H_labels, G, G_labels, labeled)
            c *= len(possible_root_nodes)
            estimation_values.append(c)
    if len(estimation_values) == 0:
        return 0, [0]
    if detailed_estimation:
        return sum(estimation_values) / len(
            estimation_values), estimation_values
    else:
        return sum(estimation_values) / len(estimation_values)