Ejemplo n.º 1
0
def DefaultConstructor():
    '''
    Test the default constructor
    '''

    Graph = snap.TNEANet()
    PrintGStats("DefaultConstructor:Graph", Graph)
Ejemplo n.º 2
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
Ejemplo n.º 3
0
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)
    readLoop(graph)


graph = sn.TNEANet()
#graph.AddFltAttrN("attr", 1.)
for i in range(1000):
    graph.AddNode(i)
repeatChangeAttr(graph)
Ejemplo n.º 4
0
 def __init__(self):
     self.g = snap.TNEANet()
Ejemplo n.º 5
0
 def loadBinaryGraph(self, fileName):
     FIn = snap.TFIn(fileName)
     self.G = snap.TNEANet(FIn)
Ejemplo n.º 6
0
 def getEmptyGraph(self):
     return snap.TNEANet()
Ejemplo n.º 7
0
def ManipulateNodesEdges():
    '''
    Test node, edge creation
    '''

    NNodes = 10000
    NEdges = 100000
    FName = "test.graph"

    Graph = snap.TNEANet.New()
    t = Graph.Empty()

    # create the nodes
    for i in range(0, NNodes):
        Graph.AddNode(i)

    t = Graph.Empty()
    n = Graph.GetNodes()
    
    # create random edges
    NCount = NEdges
    while NCount > 0:
        x = int(random.random() * NNodes)
        y = int(random.random() * NNodes)
        # skip the loops in this test
        if x != y  and  not Graph.IsEdge(x,y):
            n = Graph.AddEdge(x, y)
            NCount -= 1

    PrintGStats("ManipulateNodesEdges:Graph1", Graph)

    # get all the nodes
    NCount = 0
    NI = Graph.BegNI()
    while NI < Graph.EndNI():
        NCount += 1
        NI.Next()

    # get all the edges for all the nodes
    ECount1 = 0
    NI = Graph.BegNI()
    while NI < Graph.EndNI():
        ECount1 += NI.GetOutDeg()
        NI.Next()

    ECount1 = ECount1 / 2

    # get all the edges directly
    ECount2 = 0
    EI = Graph.BegEI()
    while EI < Graph.EndEI():
        ECount2 += 1
        EI.Next()

    print("graph ManipulateNodesEdges:Graph2, nodes %d, edges1 %d, edges2 %d"\
        % (NCount, ECount1, ECount2))

    # assignment
    Graph1 = Graph
    PrintGStats("ManipulateNodesEdges:Graph3", Graph1)

    # save the graph
    print("graph type = ", type(Graph))
    #FOut = TFOut(TStr(FName))
    FOut = snap.TFOut(FName)
    Graph.Save(FOut)
    FOut.Flush()

    # load the graph
    #FIn = TFIn(TStr(FName))
    FIn = snap.TFIn(FName)
    Graph2 = snap.TNEANet(FIn)
    PrintGStats("ManipulateNodesEdges:Graph4" , Graph2)

    # remove all the nodes and edges
    for i in range(0, NNodes):
        n = Graph.GetRndNId()
        Graph.DelNode(n)

    PrintGStats("ManipulateNodesEdges:Graph5" , Graph)
    
    Graph1.Clr()
    PrintGStats("ManipulateNodesEdges:Graph6" , Graph1)
Ejemplo n.º 8
0
def ManipulateNodeEdgeAttributes():
    '''
      Test node attribute functionality
    '''

    NNodes = 1000
    NEdges = 1000

    Graph = snap.TNEANet()
    t = Graph.Empty()

    # create the nodes
    for i in range(0, NNodes):
        Graph.AddNode(i)

    t = Graph.Empty()
    n = Graph.GetNodes()

    # create random edges
    NCount = NEdges
    while NCount > 0:
        x = int(random.random() * NNodes)
        y = int(random.random() * NNodes)
        # skip the loops in this test
        if x != y and not Graph.IsEdge(x, y):
            n = Graph.AddEdge(x, y)
        NCount -= 1

    print "Added nodes"

    # create attributes and fill all nodes
    attr1 = snap.TStr("str")
    attr2 = snap.TStr("int")
    attr3 = snap.TStr("float")
    attr4 = snap.TStr("default")

    # Test verticaliterator for node 3, 50, 700, 900
    # Check if we can set defaults to 0 fordata.
    Graph.AddIntAttrN(attr2, 0)
    Graph.AddIntAttrDatN(3, 3 * 2, attr2)
    Graph.AddIntAttrDatN(50, 50 * 2, attr2)
    Graph.AddIntAttrDatN(700, 700 * 2, attr2)
    Graph.AddIntAttrDatN(900, 900 * 2, attr2)

    print "Added attributes"

    NodeId = 0
    NI = Graph.BegNAIntI(attr2)
    while NI < Graph.EndNAIntI(attr2):
        if NI.GetDat() != 0:
            print "Attribute: %s, Node: %i, Val: %d" % (attr2(), NodeId,
                                                        NI.GetDat())
        NodeId += 1
        NI.Next()

    # Test vertical flt iterator for node 3, 50, 700, 900
    Graph.AddFltAttrDatN(5, 3.41, attr3)
    Graph.AddFltAttrDatN(50, 2.718, attr3)
    Graph.AddFltAttrDatN(300, 150.0, attr3)

    Graph.AddFltAttrDatN(653, 653, attr3)
    NodeId = 0
    NCount = 0
    NI = Graph.BegNI()
    while NI < Graph.EndNI():
        NCount += 1
        NI.Next()

    NI = Graph.BegNAFltI(attr3)
    NodeId = 0
    while NI < Graph.EndNAFltI(attr3):
        if NI.GetDat() != snap.TFlt.Mn:
            print "Attribute: %s, Node: %i, Val: %f" % (attr3(), NodeId,
                                                        NI.GetDat())
        NodeId += 1
        NI.Next()

    # Test vertical str iterator for node 3, 50, 700, 900
    Graph.AddStrAttrDatN(10, snap.TStr("abc"), attr1)
    Graph.AddStrAttrDatN(20, snap.TStr("def"), attr1)
    Graph.AddStrAttrDatN(400, snap.TStr("ghi"), attr1)
    # this does not show since ""=null
    Graph.AddStrAttrDatN(455, snap.TStr(""), attr1)
    NodeId = 0

    NI = Graph.BegNAStrI(attr1)
    NodeId = 0
    while NI < Graph.EndNAStrI(attr1):
        if NI.GetDat() != snap.TStr.GetNullStr():
            print "Attribute: %s, Node: %i, Val: %s" % (attr1(), NodeId,
                                                        NI.GetDat())
        NodeId += 1
        NI.Next()

    # Test vertical iterator over many types (must skip default/deleted attr)
    NId = 55
    Graph.AddStrAttrDatN(NId, snap.TStr("aaa"), attr1)
    Graph.AddIntAttrDatN(NId, 3 * 2, attr2)
    Graph.AddFltAttrDatN(NId, 3.41, attr3)
    Graph.AddStrAttrDatN(80, snap.TStr("dont appear"),
                         attr4)  # should not show up
    NIdAttrName = snap.TStrV()
    Graph.AttrNameNI(NId, NIdAttrName)
    AttrLen = NIdAttrName.Len()
    for i in range(AttrLen):
        print "Vertical Node: %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())

    Graph.DelAttrDatN(NId, attr2)
    Graph.AttrNameNI(NId, NIdAttrName)
    AttrLen = NIdAttrName.Len()
    for i in range(AttrLen):
        print "Vertical Node (no int) : %i, Attr: %s" % (NId,
                                                         NIdAttrName.GetI(i)())

    Graph.AddIntAttrDatN(NId, 3 * 2, attr2)
    Graph.DelAttrN(attr1)
    Graph.AttrNameNI(NId, NIdAttrName)
    AttrLen = NIdAttrName.Len()
    for i in range(AttrLen):
        print "Vertical Node (no str) : %i, Attr: %s" % (NId,
                                                         NIdAttrName.GetI(i)())

    NIdAttrValue = snap.TStrV()
    Graph.AttrValueNI(NId, NIdAttrValue)
    AttrLen = NIdAttrValue.Len()
    for i in range(AttrLen):
        print "Vertical Node (no str) : %i, Attr_Val: %s" % (
            NId, NIdAttrName.GetI(i)())

    for i in range(NNodes):
        Graph.AddIntAttrDatN(i, 70, attr2)

    total = 0
    NI = Graph.BegNAIntI(attr2)
    while NI < Graph.EndNAIntI(attr2):
        total += NI.GetDat()
        NI.Next()

    print "Average: %i (should be 70)" % (total / NNodes)

    # Test verticaliterator for edge
    Graph.AddIntAttrDatE(3, 3 * 2, attr2)
    Graph.AddIntAttrDatE(55, 55 * 2, attr2)
    Graph.AddIntAttrDatE(705, 705 * 2, attr2)
    Graph.AddIntAttrDatE(905, 905 * 2, attr2)
    EdgeId = 0
    EI = Graph.BegEAIntI(attr2)
    while EI < Graph.EndEAIntI(attr2):
        if EI.GetDat() != snap.TInt.Mn:
            print "E Attribute: %s, Edge: %i, Val: %i" \
                  % (attr2(), EdgeId, EI.GetDat())
        EdgeId += 1
        EI.Next()

    # Test vertical flt iterator for edge
    Graph.AddFltAttrE(attr3, 0.00)
    Graph.AddFltAttrDatE(5, 4.41, attr3)
    Graph.AddFltAttrDatE(50, 3.718, attr3)
    Graph.AddFltAttrDatE(300, 151.0, attr3)
    Graph.AddFltAttrDatE(653, 654, attr3)
    EdgeId = 0
    EI = Graph.BegEAFltI(attr3)
    while EI < Graph.EndEAFltI(attr3):
        # Check if defaults are set to 0.
        if EI.GetDat() != 0:
            print "E Attribute: %s, Edge: %i, Val: %f" % \
                  (attr3(), EdgeId, EI.GetDat())
        EdgeId += 1
        EI.Next()

    # Test vertical str iterator for edge
    Graph.AddStrAttrDatE(10, snap.TStr("abc"), attr1)
    Graph.AddStrAttrDatE(20, snap.TStr("def"), attr1)
    Graph.AddStrAttrDatE(400, snap.TStr("ghi"), attr1)
    # this does not show since ""=null
    Graph.AddStrAttrDatE(455, snap.TStr(""), attr1)
    EdgeId = 0
    EI = Graph.BegEAStrI(attr1)
    while EI < Graph.EndEAStrI(attr1):
        if EI.GetDat() != snap.TStr.GetNullStr():
            print "E Attribute: %s, Edge: %i, Val: %s" % \
                  (attr1(), EdgeId, EI.GetDat())
        EdgeId += 1
        EI.Next()

    # Test vertical iterator over many types (must skip default/deleted attr)
    EId = 55
    Graph.AddStrAttrDatE(EId, snap.TStr("aaa"), attr1)
    Graph.AddIntAttrDatE(EId, 3 * 2, attr2)
    Graph.AddFltAttrDatE(EId, 3.41, attr3)
    Graph.AddStrAttrDatE(80, snap.TStr("dont appear"),
                         attr4)  # should not show up
    EIdAttrName = snap.TStrV()
    #  Graph.AttrNameEI(EId, EIdAttrName)
    AttrLen = EIdAttrName.Len()
    for i in range(AttrLen):
        print "Vertical Edge: %i, Attr: %s" % (EId, EIdAttrName.GetI(i))

    Graph.DelAttrDatE(EId, attr2)
    #  Graph.AttrNameEI(EId, EIdAttrName)
    AttrLen = EIdAttrName.Len()
    for i in range(AttrLen):
        print "Vertical Edge (no int) : %i, Attr: %s" % (EId,
                                                         EIdAttrName.GetI(i))

    Graph.AddIntAttrDatE(EId, 3 * 2, attr2)
    Graph.DelAttrE(attr1)
    #  Graph.AttrNameEI(EId, EIdAttrName)
    AttrLen = EIdAttrName.Len()
    for i in range(AttrLen):
        print "Vertical Edge (no str) : %i, Attr: %s" % (EId,
                                                         EIdAttrName.GetI(i)())

    EIdAttrValue = snap.TStrV()
    Graph.AttrValueEI(snap.TInt(EId), EIdAttrValue)
    AttrLen = EIdAttrValue.Len()
    for i in range(AttrLen):
        print "Vertical Edge (no str) : %i, Attr_Val: %s" % (
            EId, EIdAttrValue.GetI(i)())

    for i in range(NEdges):
        Graph.AddIntAttrDatE(i, 70, attr2)

    total = 0
    EI = Graph.BegNAIntI(attr2)
    while EI < Graph.EndNAIntI(attr2):
        total += EI.GetDat()
        EI.Next()

    print "Average: %i (should be 70)" % (total / NEdges)

    Graph.Clr()