def SaveState(ds): fname = sw.GetStateName() Start = Snap.TInt(ds["start"]) Dist = Snap.TInt(ds["dist"]) Visited = ds["visit"] FOut = Snap.TFOut(Snap.TStr(fname)) Start.Save(FOut) Dist.Save(FOut) Visited.Save(FOut) FOut.Flush()
def SaveState(sw, ds): fname = sw.GetStateName() Start = Snap.TInt(ds["start"]) Dist = Snap.TInt(ds["dist"]) Visited = ds["visit"] FOut = Snap.TFOut(Snap.TStr(fname)) sw.cum_timer.cum_start("disk") Start.Save(FOut) Dist.Save(FOut) Visited.Save(FOut) FOut.Flush() sw.cum_timer.cum_stop("disk")
def LoadState(): fname = sw.GetStateName() if not os.path.exists(fname): return None FIn = Snap.TFIn(Snap.TStr(fname)) Start = Snap.TInt(FIn) Dist = Snap.TInt(FIn) Visited = Snap.TIntH(FIn) ds = {} ds["start"] = Start.Val ds["dist"] = Dist.Val ds["visit"] = Visited return ds
def LoadState(sw): fname = sw.GetStateName() if not os.path.exists(fname): return None sw.cum_timer.cum_start("disk") FIn = Snap.TFIn(Snap.TStr(fname)) Start = Snap.TInt(FIn) Dist = Snap.TInt(FIn) Visited = Snap.TIntV(FIn) sw.cum_timer.cum_stop("disk") ds = {} ds["start"] = Start.Val ds["dist"] = Dist.Val ds["visit"] = Visited return ds
def SaveState(ds): fname = sw.GetStateName() First = Snap.TInt(ds["first"]) Range = Snap.TInt(ds["range"]) Count = Snap.TInt(ds["count"]) Dist = Snap.TInt(ds["dist"]) Start = Snap.TInt(ds["start"]) Visited = ds["visit"] FOut = Snap.TFOut(Snap.TStr(fname)) First.Save(FOut) Range.Save(FOut) Count.Save(FOut) Dist.Save(FOut) Start.Save(FOut) Visited.Save(FOut) FOut.Flush()
def LoadState(): fname = sw.GetStateName() if not os.path.exists(fname): return None FIn = Snap.TFIn(Snap.TStr(fname)) First = Snap.TInt(FIn) Range = Snap.TInt(FIn) Count = Snap.TInt(FIn) Dist = Snap.TInt(FIn) Start = Snap.TInt(FIn) Visited = Snap.TIntV(FIn) ds = {} ds["first"] = First.Val ds["range"] = Range.Val ds["count"] = Count.Val ds["dist"] = Dist.Val ds["start"] = Start.Val ds["visit"] = Visited return ds
def test(): status = False try: import snap version = snap.Version i = snap.TInt(5) if i == 5: status = True except: pass if status: print("SUCCESS, your version of Snap.py is %s" % (version)) else: print("*** ERROR, no working Snap.py was found on your computer")
status = False try: import snap version = snap.Version i = snap.TInt(5) if i == 5: status = True except: pass if status: print "SUCCESS, your version of Snap.py is %s" % (version) else: print "*** ERROR, no working Snap.py was found on your computer"
import snap as Snap h = Snap.TIntIntVH() print(h.Len()) for i in range(0, 10): k = h.AddKey(Snap.TInt(i)) v = h.GetDat(Snap.TInt(i)) for j in range(0, i + 3): v.Add(j) print(i, k) print(h.Len()) print("-----------") for i in range(0, 10): j = h.GetKeyId(Snap.TInt(i)) print(j) v = h.GetDat(Snap.TInt(i)) #print(type(j), type(v)) print(v.Len()) print()
import snap h = snap.TIntIntVH() print h.Len() for i in range(0, 10): k = h.AddKey(snap.TInt(i)) v = h.GetDat(snap.TInt(i)) for j in range(0, i + 3): v.Add(j) print i, k print h.Len() print "-----------" for i in range(0, 10): j = h.GetKeyId(snap.TInt(i)) print j v = h.GetDat(snap.TInt(i)) #print type(j), type(v) print v.Len() print
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()