def getLblGraph(self, fileName): context = snap.TTableContext() schema = snap.Schema() schema.Add(snap.TStrTAttrPr("srcLabel", snap.atStr)) schema.Add(snap.TStrTAttrPr("srcId", snap.atInt)) schema.Add(snap.TStrTAttrPr("dstLabel", snap.atStr)) schema.Add(snap.TStrTAttrPr("dstId", snap.atInt)) table = snap.TTable.LoadSS(schema, fileName, context, " ", snap.TBool(False)) #print table edgeattrv = snap.TStrV() edgeattrv.Add("srcLabel") edgeattrv.Add("dstLabel") # edgeattrv.Add("edgeattr2") srcnodeattrv = snap.TStrV() # srcnodeattrv.Add("srcLabel") dstnodeattrv = snap.TStrV() # srcnodeattrv.Add("dstLabel") # net will be an object of type snap.PNEANet return snap.ToNetwork(snap.PNEANet, table, "srcId", "dstId", srcnodeattrv, dstnodeattrv, edgeattrv, snap.aaFirst)
def parseGraph(filename="./GG-NE/test.tsv"): edgefilename = filename # A file containing the graph, where each row contains an edge # and each edge is represented with the source and dest node ids, # the edge attributes, and the source and destination node attributes # separated by a tab. context = snap.TTableContext( ) # When loading strings from different files, it is important to use the same context # so that SNAP knows that the same string has been seen before in another table. schema = snap.Schema() schema.Add(snap.TStrTAttrPr("srcID", snap.atInt)) schema.Add(snap.TStrTAttrPr("dstID", snap.atInt)) schema.Add(snap.TStrTAttrPr("weight", snap.atFlt)) table = snap.TTable.LoadSS(schema, edgefilename, context, "\t", snap.TBool(False)) # In this example, we add both edge attributes to the network, # but only one src node attribute, and no dst node attributes. edgeattrv = snap.TStrV() edgeattrv.Add("weight") srcnodeattrv = snap.TStrV() dstnodeattrv = snap.TStrV() # net will be an object of type snap.PNEANet G = snap.ToNetwork(snap.PNEANet, table, "srcID", "dstID", srcnodeattrv, dstnodeattrv, edgeattrv, snap.aaFirst) labels = pd.read_table("SS-Butterfly_labels.tsv") G.AddIntAttrN("label") for index, row in labels.iterrows(): G.AddIntAttrDatN(row["# Node_ID"], row["Species"], "label") return G
def get_commits_graph(path): context = snap.TTableContext() e_schema = snap.Schema() e_schema.Add(snap.TStrTAttrPr("source", snap.atStr)) e_schema.Add(snap.TStrTAttrPr("target", snap.atStr)) e_schema.Add(snap.TStrTAttrPr("weight", snap.atStr)) n_schema = snap.Schema() n_schema.Add(snap.TStrTAttrPr("id", snap.atStr)) n_schema.Add(snap.TStrTAttrPr("username", snap.atStr)) n_schema.Add(snap.TStrTAttrPr("size", snap.atStr)) edgetable = snap.TTable.LoadSS(e_schema, path + '{}_edges.csv'.format(pname), context, ",", snap.TBool(True)) nodetable = snap.TTable.LoadSS(n_schema, path + '{}_nodes.csv'.format(pname), context, ",", snap.TBool(True)) edgeattrv = snap.TStrV() nodeattrv = snap.TStrV() net = snap.ToNetwork(snap.PNEANet, edgetable, "source", "target", edgeattrv, nodetable, "id", nodeattrv, snap.aaFirst) snap.DelSelfEdges(net) snap.SaveEdgeList(net, 'temp/commits_temp_edgelist.csv') Data = open('temp/commits_temp_edgelist.csv', 'r') Graphtype = nx.Graph() G = nx.parse_edgelist(Data, delimiter='\t', create_using=Graphtype, nodetype=int, data=(('weight', float),), comments='#') return G
def walkEdgeAttributes(self, EId): EIdAttrName = snap.TStrV() self.G.AttrNameEI(EId, EIdAttrName) AttrLen = EIdAttrName.Len() EIdAttrValue = snap.TStrV() self.G.AttrValueEI(EId, EIdAttrValue) AttrLen = EIdAttrValue.Len() for i in range(AttrLen): print "Vertical Edge: %i, Attr: %s, Val: %s" % ( EId, EIdAttrName.GetI(i)(), EIdAttrValue.GetI(i)())
def walkNodeAttributes(self, NId): NIdAttrName = snap.TStrV() self.G.AttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() NIdAttrValue = snap.TStrV() self.G.AttrValueNI(NId, NIdAttrValue) AttrLen = NIdAttrValue.Len() for i in range(AttrLen): print "Vertical Node: %i, Attr: %s, Val: %s" % ( NId, NIdAttrName.GetI(i)(), NIdAttrValue.GetI(i)())
def __init__(self, fileName): self.fileName = fileName self.rootDir = self.getRootDir() self.attrVal1 = attrVal1 self.attrVal2 = attrVal2 self.graphName = self.getGraphName() self.lblV = snap.TStrV() self.lblV.Add(attrVal1) self.lblV.Add(attrVal2) self.lblNH = snap.TStrIntH() # Node count with attached label self.lblEH = snap.TIntIntH() # Edge count with attached src dst labels #self.G = self.getGraph(snap.PUNGraph) self.G = self.getLblGraph() self.saveGraph() ufileName = os.path.abspath(self.rootDir + "/" + self.graphName + ".txt") self.snapG = snap.LoadEdgeList(snap.PUNGraph, ufileName) self.nxG = nx.read_edgelist(ufileName) self.gNAH = self.getNodeIdLabel(self.G)
def getLabelVector(self): lblV = snap.TStrV() for x in xrange(0, self.mVals): lbl = str(bin(2 ** x)[2:].zfill(self.mVals)) self.lblNH[lbl] = 0 self.lblNH.GetKeyV(lblV) return lblV
def main(args): if len(args) < 3: print(get_usage()) sys.exit(1) votes = sys.argv[1] outFile = sys.argv[2] t = testutils.Timer(ENABLE_TIMER) context = snap.TTableContext() VoteS = snap.Schema() VoteS.Add(snap.TStrTAttrPr("UserId", snap.atInt)) VoteS.Add(snap.TStrTAttrPr("AdminId", snap.atInt)) TVotes = snap.TTable.LoadSS("WikiVotes", VoteS, votes, context, '\t', snap.TBool(False)) t.show("load Votes", TVotes) GroupBy = snap.TStrV() GroupBy.Add("UserId") JointTable = TVotes.SelfSimJoinPerGroup(GroupBy, "AdminId", DISTANCE_ATTRIBUTE, snap.Jaccard, 0.5) t.show("SimJoinPerGroup complete", JointTable) JointTable.SelectAtomic("WikiVotes_1.UserId", "WikiVotes_2.UserId", snap.NEQ) t.show("Select complete", JointTable) testutils.dump(JointTable, 20) JointTable.SaveSS(outFile)
def walkNodeAttributes(self, NId): Val = "" NIdAttrName = snap.TStrV() self.NG.AttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() NIdAttrValue = snap.TStrV() self.NG.AttrValueNI(NId, NIdAttrValue) AttrLen = NIdAttrValue.Len() for i in range(AttrLen): Val = NIdAttrValue.GetI(i)() break; return Val
def load_mode_to_graph(mode, filename, Graph, context): modeId = mode + 'Id' schema = snap.Schema() schema.Add(snap.TStrTAttrPr(modeId, snap.atStr)) schema.Add(snap.TStrTAttrPr("datasetId", snap.atStr)) modenet = snap.TTable.LoadSS(schema, filename, context, "\t", snap.TBool(False)) snap.LoadModeNetToNet(Graph, mode, modenet, modeId, snap.TStrV())
def main(args): if len(args) < 3: print(get_usage()) sys.exit(1) yelp = sys.argv[1] outFile = sys.argv[2] t = testutils.Timer(ENABLE_TIMER) context = snap.TTableContext() YelpS = snap.Schema() YelpS.Add(snap.TStrTAttrPr("Name", snap.atStr)) YelpS.Add(snap.TStrTAttrPr("City", snap.atStr)) YelpS.Add(snap.TStrTAttrPr("State", snap.atStr)) YelpS.Add(snap.TStrTAttrPr("Latitude", snap.atFlt)) YelpS.Add(snap.TStrTAttrPr("Longitude", snap.atFlt)) TYelp = snap.TTable.LoadSS("Yelp", YelpS, yelp, context, '\t', snap.TBool(True)); t.show("load Yelp", TYelp) Cols = snap.TStrV() Cols.Add("Latitude") Cols.Add("Longitude") # Get all business within 5 kilometers of each other JointTable = TYelp.SelfSimJoin(Cols, DISTANCE_ATTRIBUTE, snap.Haversine, 2) t.show("SimJoin complete", JointTable) ProjectionV = snap.TStrV() ProjectionV.Add("Yelp_1.Name") ProjectionV.Add("Yelp_1.City") ProjectionV.Add("Yelp_1.State") ProjectionV.Add("Yelp_2.Name") ProjectionV.Add("Yelp_2.City") ProjectionV.Add("Yelp_2.State") ProjectionV.Add(DISTANCE_ATTRIBUTE) JointTable.ProjectInPlace(ProjectionV) t.show("Project complete") testutils.dump(JointTable, 100); JointTable.SaveSS(outFile)
def getNodeFeatures(graph, node): # The snap vectors that will house the raw data intVals = snap.TIntV() intNames = snap.TStrV() fltVals = snap.TFltV() fltNames = snap.TStrV() strVals = snap.TStrV() strNames = snap.TStrV() # Load the raw data from the node graph.IntAttrValueNI(node, intVals) graph.IntAttrNameNI(node, intNames) graph.FltAttrValueNI(node, fltVals) graph.FltAttrNameNI(node, fltNames) graph.StrAttrValueNI(node, strVals) graph.StrAttrNameNI(node, strNames) # Put the data into a dictionary to be returned return _aggregate_vecs(intNames, intVals, fltNames, fltVals, strNames, strVals)
def load_chchse(path): #load table context = snap.TTableContext() schema = snap.Schema() schema.Add(snap.TStrTAttrPr("STITCH 1", snap.atStr)) schema.Add(snap.TStrTAttrPr("STITCH 2", snap.atStr)) schema.Add(snap.TStrTAttrPr("Polypharmacy Side Effect", snap.atStr)) schema.Add(snap.TStrTAttrPr("Side Effect Name", snap.atStr)) table = snap.TTable.LoadSS(schema, path, context, ",", snap.TBool(True)) #reformat CIDs and seIDs as strings raw_cid1s = snap.TStrV() cid1s = snap.TIntV() table.ReadStrCol("STITCH 1", raw_cid1s) for raw_cid in raw_cid1s: cid = format_cid(raw_cid) cid1s.Add(cid) table.StoreIntCol("cid1", cid1s) raw_cid2s = snap.TStrV() cid2s = snap.TIntV() table.ReadStrCol("STITCH 2", raw_cid2s) for raw_cid in raw_cid2s: cid = format_cid(raw_cid) cid2s.Add(cid) table.StoreIntCol("cid2", cid2s) #save table as binary #cache_path = "../cache/ChChSe-decagon_table.tsv" #table.Save(snap.TFOut(cache_path)) #TEST: checks the number of side effect types seVec = snap.TStrV() table.ReadStrCol("Side Effect Name", seVec) print len(set(list(seVec))) return table
def load_crossnet_to_graph(context, edgeId, srcName, dstName, filepath, Graph, prefix="miner"): srcId = srcName + "SrcId" dstId = dstName + "DstId" schema = snap.Schema() schema.Add(snap.TStrTAttrPr(edgeId, snap.atStr)) schema.Add(snap.TStrTAttrPr("datasetId", snap.atStr)) schema.Add(snap.TStrTAttrPr(srcId, snap.atStr)) schema.Add(snap.TStrTAttrPr(dstId, snap.atStr)) crossnet = snap.TTable.LoadSS(schema, filepath, context, DELIMITER, snap.TBool(False)) crossName = prefix + "-" + dstName + "-" + srcName Graph.AddCrossNet(srcName, dstName, crossName, False) snap.LoadCrossNetToNet(Graph, srcName, dstName, crossName, crossnet, srcId, dstId, snap.TStrV())
def ttableToTmmnet(): # load table t0 = t() context = snap.TTableContext() schema = snap.Schema() schema.Add(snap.TStrTAttrPr("srcID", snap.atInt)) schema.Add(snap.TStrTAttrPr("dstID", snap.atInt)) edge_table = snap.TTable.LoadSS(schema, NW.small, context, "\t", snap.TBool(False)) t1 = reportTime(t0, "TTABLE") # convert table to TMMNet mmnet = snap.TMMNet.New() edgeattrv = snap.TStrV() edgeattrv.Add("edgeattr1") CrossG = snap.LoadCrossNetToNet(mmnet, "Mode1", "Mode2", "Cross1", edge_table, "srcID", "dstID", edgeattrv) reportTime(t1, "convert TTABLE to CrossNet")
friends_table.Save(FOut) FOut.Flush() enrollments_table.Save(FOut) FOut.Flush() advisors_table.Save(FOut) FOut.Flush() prereqs_table.Save(FOut) FOut.Flush() instructors_table.Save(FOut) FOut.Flush() context.Save(FOut) FOut.Flush() #Create TMMNet mmnet = snap.TMMNet.New() nodeattrv = snap.TStrV() snap.LoadModeNetToNet(mmnet, "students", advisors_table, "StudentID", nodeattrv) snap.LoadModeNetToNet(mmnet, "courses", instructors_table, "CourseID", nodeattrv) snap.LoadModeNetToNet(mmnet, "profs", advisors_table, "ProfID", nodeattrv) edgeattrv = snap.TStrV() snap.LoadCrossNetToNet(mmnet, "students", "students", "friends", friends_table, "Student1ID", "Student2ID", edgeattrv) snap.LoadCrossNetToNet(mmnet, "students", "profs", "advisors", advisors_table, "StudentID", "ProfID", edgeattrv) snap.LoadCrossNetToNet(mmnet, "students", "courses", "enrollments", enrollments_table, "StudentID", "CourseID", edgeattrv) snap.LoadCrossNetToNet(mmnet, "courses", "courses", "prereqs", prereqs_table, "CourseID", "PrereqCourseID", edgeattrv) snap.LoadCrossNetToNet(mmnet, "courses", "profs", "instructors", instructors_table, "CourseID", "ProfID", edgeattrv) # Convert to TNEANet crossnetids = snap.TIntV() crossnetids.Add(mmnet.GetCrossId("friends"))
def main(args): if len(args) < 3: print(get_usage()) sys.exit(1) root = sys.argv[1] mid_date = sys.argv[2] mid_ticks = utils.date_to_ticks(mid_date) file_cache = { TCOLLAB: None, TPULL: None, TREPO: None, TFOLLOW: None, TWATCH: None, TFORK: None } for file in os.listdir(root): if file.endswith(".tsv"): file_cache[file] = os.path.join(root, file) print file_cache[file] for key, val in file_cache.iteritems(): if val == None: print("One of the required files not found.") print(get_usage()) sys.exit(1) t = testutils.Timer(ENABLE_TIMER) context = snap.TTableContext() S1 = snap.Schema() S1.Add(snap.TStrTAttrPr("userid1", snap.atStr)) S1.Add(snap.TStrTAttrPr("userid2", snap.atStr)) S1.Add(snap.TStrTAttrPr("created_at", snap.atInt)) Tfollow = snap.TTable.LoadSS("Tfollow", S1, file_cache[TFOLLOW], context, '\t', snap.TBool(False)) t.show("load follow") S2 = snap.Schema() S2.Add(snap.TStrTAttrPr("userid", snap.atStr)) S2.Add(snap.TStrTAttrPr("owner", snap.atStr)) S2.Add(snap.TStrTAttrPr("name", snap.atStr)) S2.Add(snap.TStrTAttrPr("created_at", snap.atInt)) Tcollab = snap.TTable.LoadSS("Tcollab", S2, file_cache[TCOLLAB], context, '\t', snap.TBool(False)) t.show("load collab") S3 = snap.Schema() S3.Add(snap.TStrTAttrPr("userid", snap.atStr)) S3.Add(snap.TStrTAttrPr("owner", snap.atStr)) S3.Add(snap.TStrTAttrPr("name", snap.atStr)) S3.Add(snap.TStrTAttrPr("pullid", snap.atInt)) S3.Add(snap.TStrTAttrPr("status", snap.atStr)) S3.Add(snap.TStrTAttrPr("created_at", snap.atInt)) Tpull = snap.TTable.LoadSS("Tpull", S3, file_cache[TPULL], context, '\t', snap.TBool(False)) t.show("load pull") # If (u,v) collaborated on the same repository - determined by the owner, name pair, # are added as collaborators. #TODO Better column renaming V = snap.TStrV() V.Add("created_at") Tcollab.Order(V, "", snap.TBool(False), snap.TBool(True)) V.Clr() V.Add("owner") V.Add("name") V.Add("userid") Tcollab.Group(V, "UserRepoId") V.Clr() V.Add("UserRepoId") Tcollab.Unique(V) Tcollab_merge = Tcollab.SelfJoin("owner") Tcollab_merge.SelectAtomic("Tcollab_1.name", "Tcollab_2.name", snap.EQ) Tcollab_merge.SelectAtomic("Tcollab_1.userid", "Tcollab_2.userid", snap.NEQ) # BUGBUG - Commenting this line will mean created_at is not present in Tcollab_merge. # However, the ProjectInPlace will not complain and silently exclude created_at from the # result. This leads to the Index:-1 error in SelectAtomicIntConst on created_at later in the code. Tcollab_merge.ColMin("Tcollab_1.created_at", "Tcollab_2.created_at", "created_at") V = snap.TStrV() V.Add("Tcollab_1.userid") V.Add("Tcollab_2.userid") V.Add("created_at") Tcollab_merge.ProjectInPlace(V) Tcollab_merge.Rename("Tcollab_1.userid", "userid1") Tcollab_merge.Rename("Tcollab_2.userid", "userid2") t.show("merge collab", Tcollab_merge) #testutils.dump(Tcollab_merge, 50) # If (u,v) worked on the same pull request on the same repository, they are added # as (soft) collaborators. V = snap.TStrV() V.Add("created_at") Tpull.Order(V, "", snap.TBool(False), snap.TBool(True)) V.Clr() V.Add("owner") V.Add("name") V.Add("userid") Tpull.Group(V, "UserRepoId") V.Clr() V.Add("UserRepoId") Tpull.Unique(V) Tpull_merge = Tpull.SelfJoin("owner") Tpull_merge.SelectAtomic("Tpull_1.name", "Tpull_2.name", snap.EQ) Tpull_merge.SelectAtomic("Tpull_1.pullid", "Tpull_2.pullid", snap.EQ) Tpull_merge.SelectAtomic("Tpull_1.userid", "Tpull_2.userid", snap.NEQ) Tpull_merge.ColMin("Tpull_1.created_at", "Tpull_2.created_at", "created_at") V = snap.TStrV() V.Add("Tpull_1.userid") V.Add("Tpull_2.userid") V.Add("created_at") Tpull_merge.ProjectInPlace(V) Tpull_merge.Rename("Tpull_1.userid", "userid1") Tpull_merge.Rename("Tpull_2.userid", "userid2") t.show("merge pull", Tpull_merge) # BUGBUG: UnionAll is returning unexpected result at this point #Tmerge = Tcollab_merge.UnionAll(Tpull_merge, "Tmerge") Tmerge = Tpull_merge # Select the base and delta tables from the merged table. Tbase = snap.TTable.New(Tmerge, "Base") Tdelta = snap.TTable.New(Tmerge, "Delta") Tbase.SelectAtomicIntConst("created_at", mid_ticks, snap.LTE) Tdelta.SelectAtomicIntConst("created_at", mid_ticks, snap.GTE) #TODO: Union Tbase with collab and pull to include (userid, owner) edge t.show("collab union") # Convert base table to base graph Gbase = snap.ToNetwork(snap.PNEANet, Tbase, "userid1", "userid2", snap.aaFirst) Gdelta = snap.ToNetwork(snap.PNEANet, Tdelta, "userid1", "userid2", snap.aaFirst) t.show("base graph", Gbase) t.show("delta graph", Gdelta) NITERS = 20 total_preck = 0 print("Userid\tPrec@%d\tAverage Index" % (N_TOP_RECOS)) # Random walk with restarts # BUGBUG: Returns the same id everytime # userid = Gbase.GetRndNId() for i in range(NITERS): # Randomly choose a starting node userid = random.choice([node.GetId() for node in Gbase.Nodes()]) user = Gbase.GetNI(userid) # Perform random walk with restarts on base graph HT = snap.TIntFltH() snap.GetRndWalkRestart_PNEANet(Gbase, ALPHA, userid, HT) HT.SortByDat(False) j = 0 cnt = 0 preck = 0 average_index = -1 # Calculate precision while cnt < N_TOP_RECOS and j < HT.Len(): recoid = HT.GetKey(j) pagerank = HT.GetDat(recoid) #print recoid, pagerank if recoid != userid: # If the edge is not in base graph but is present in delta graph, we made an accurate prediction. if not Gbase.IsEdge(userid, recoid) and Gdelta.IsNode( userid) and Gdelta.IsNode(recoid) and (Gdelta.IsEdge( userid, recoid) or Gdelta.IsEdge(recoid, userid)): preck += 1 cnt += 1 j += 1 # Calculate average index try: node = Gdelta.GetNI(userid) edges = [nid for nid in node.GetOutEdges() ] + [nid for nid in node.GetInEdges()] #print edges #print([HT.GetKeyId(nid) for nid in edges]) index = 0 for nid in edges: index += HT.GetKeyId(nid) average_index = index / len(edges) except: # Node not present in delta graph implies no new edges formed pass total_preck += preck print("%d\t%d\t%f" % (userid, preck, average_index)) #rank = snap.TTable.New("Rank", HT, "User", PAGE_RANK_ATTRIBUTE, context, snap.TBool(True)) print("Average Precision@%d = %f" % (N_TOP_RECOS, total_preck / float(NITERS)))
# Select # >>> tags.select('Tags = "python"') tags.SelectAtomicStrConst("Tag", "python", snap.EQ) t.show("select", tags) # Join # >>> questions = posts.join(tags) questions = posts.Join("PostId", tags, "PostId") t.show("join", questions) testutils.dump(questions, 1) # Project # >>> questions.project(['PostId', 'UserId', 'AcceptedAnswerId'], in_place = True) V = snap.TStrV() V.Add("t1.PostId") V.Add("t1.UserId") V.Add("t1.AcceptedAnswerId") questions.ProjectInPlace(V) t.show("copy & project", questions) # Rename # >>> questions.rename('UserId', 'Asker') questions.Rename("t1.UserId", "Asker") t.show("rename", questions) # Project # >>> posts.project(['PostId',UserId'], in_place = True) V = snap.TStrV() V.Add("PostId")
print time.ctime() # In[5]: print time.ctime(), "Creating table after join ..." PaperPRankJoinT = papers.Join("PaperID", PRankT, "PaperID") t.show("paperprankjoint", PaperPRankJoinT) r.show("__PaperPRankJoinT__") print time.ctime() # In[6]: print time.ctime() orderby = snap.TStrV() orderby.Add("PageRankScore") PaperPRankJoinT.Order(orderby, "", snap.TBool(False), snap.TBool(False)) print time.ctime() # In[14]: print time.ctime() InDegH = snap.TIntH() i = 0 for item in InDegV: i += 1 if i % 1000000 == 0: print i InDegH.AddDat(item.GetVal1(), item.GetVal2())
def main(args): if len(args) < 1: print("python github-join.py <path_to_tsv_file>") sys.exit(1) filename = args[0] t = testutils.Timer(ENABLE_TIMER) context = snap.TTableContext() S = snap.Schema() S.Add(snap.TStrTAttrPr("userid", snap.atStr)) S.Add(snap.TStrTAttrPr("owner", snap.atStr)) S.Add(snap.TStrTAttrPr("name", snap.atStr)) S.Add(snap.TStrTAttrPr("pullid", snap.atInt)) S.Add(snap.TStrTAttrPr("status", snap.atStr)) S.Add(snap.TStrTAttrPr("created_at", snap.atInt)) Tpull = snap.TTable.LoadSS("Tpull", S, filename, context, '\t', snap.TBool(False)) t.show("load pull") V = snap.TStrV() V.Add("created_at") Tpull.Order(V, "", snap.TBool(False), snap.TBool(True)) V.Clr() V.Add("owner") V.Add("name") V.Add("userid") Tpull.Group(V, "TagId") V.Clr() V.Add("TagId") Tpull.Unique(V) t.show("Unique", Tpull) Tpull_merge = Tpull.SelfJoin("owner") t.show("Merge", Tpull_merge) # Things work fine till this point Tpull_merge.SelectAtomic("Tpull_1.name", "Tpull_2.name", snap.EQ) Tpull_merge.SelectAtomic("Tpull_1.pullid", "Tpull_2.pullid", snap.EQ) Tpull_merge.SelectAtomic("Tpull_1.userid", "Tpull_2.userid", snap.NEQ) Tpull_merge.ColMin("Tpull_1.created_at", "Tpull_2.created_at", "created_at") V = snap.TStrV() V.Add("Tpull_1.userid") V.Add("Tpull_2.userid") V.Add("created_at") Tpull_merge.ProjectInPlace(V) Tpull_merge.Rename("Tpull_1.userid", "userid1") Tpull_merge.Rename("Tpull_2.userid", "userid2") # Copy the Tpull_merge to form two graphs - base and delta. Select all rows in base for created_at < x and all dates in delta for created_at > x Tbase = snap.TTable.New(Tpull_merge, "Base") Tdelta = snap.TTable.New(Tpull_merge, "Delta") #Tbase.SelectAtomicIntConst("created_at", x, snap.LTE) #Tdelta.SelectAtomicIntConst("created_at", x, snap.GTE) G = snap.ToNetwork(snap.PNEANet, Tbase, "userid1", "userid2", snap.aaFirst) t.show("graph", G)
PaperAA_AAJoinT = PaperAA_AJoinT.Join("AuthorID", AuthT, "AuthorID") print time.ctime() # In[85]: print time.ctime(), "Joining big table with PageRank and InDeg tables ..." PaperAA_PRJoinT = PaperAA_AAJoinT.Join("PaperID-1", PRankT, "PaperID") print time.ctime() PaperAA_PRIDJoinT = InDegT.Join("PaperID-1", PaperAA_PRJoinT, "PaperID") print time.ctime() # In[86]: # Now, we aggregate along different columns. print time.ctime(), "Aggregating Affl PageRank ..." GroupBy = snap.TStrV() GroupBy.Add("AfflID-1") PaperAA_PRIDJoinT.Aggregate(GroupBy, snap.aaSum, "PageRankScore", "PRScoreAggAffl", snap.TBool(False)) print time.ctime() print time.ctime(), "Aggregating Affl InDeg ..." GroupBy = snap.TStrV() GroupBy.Add("AfflID-1") PaperAA_PRIDJoinT.Aggregate(GroupBy, snap.aaSum, "InDeg", "InDegAggAffl", snap.TBool(False)) print time.ctime() print time.ctime(), "Aggregating Auth PageRank" GroupBy = snap.TStrV() GroupBy.Add("AuthorID-1")
nodeschema = snap.Schema() nodeschema.Add(snap.TStrTAttrPr("nodeID", snap.atStr)) nodeschema.Add(snap.TStrTAttrPr("name", snap.atStr)) nodeschema.Add(snap.TStrTAttrPr("movies", snap.atStr)) nodeschema.Add(snap.TStrTAttrPr("main_genre", snap.atStr)) nodeschema.Add(snap.TStrTAttrPr("genres", snap.atStr)) edge_table = snap.TTable.LoadSS(edgeschema, edgefilename, context, "\t", snap.TBool(False)) print "edge_rows", edge_table.GetNumValidRows() node_table = snap.TTable.LoadSS(nodeschema, nodefilename, context, "\t", snap.TBool(False)) print "node_rows", node_table.GetNumValidRows() srcattrv = snap.TStrV() srcattrv.Add("edgeattr1") dstattrv = snap.TStrV() dstattrv.Add("edgeattr1") edgeattrv = snap.TStrV() edgeattrv.Add("edgeattr1") nodeattrv = snap.TStrV() nodeattrv.Add("name") net1 = snap.ToNetwork(snap.PNEANet, edge_table, "srcID", "dstID", srcattrv, dstattrv, edgeattrv, snap.aaFirst) print "nodes1", net1.GetNodes() print "edges1", net1.GetEdges()
schema = snap.Schema() schema.Add(snap.TStrTAttrPr("srcID", snap.atStr)) schema.Add(snap.TStrTAttrPr("dstID", snap.atStr)) schema.Add(snap.TStrTAttrPr("timestamp", snap.atInt)) # schema.Add(snap.TStrTAttrPr("edgeattr2", snap.atStr)) # schema.Add(snap.TStrTAttrPr("srcnodeattr1", snap.atStr)) # schema.Add(snap.TStrTAttrPr("srcnodeattr2", snap.atStr)) # schema.Add(snap.TStrTAttrPr("dstnodeattr1", snap.atStr)) # schema.Add(snap.TStrTAttrPr("dstnodeattr2", snap.atStr)) table = snap.TTable.LoadSS(schema, edgefilename, context, " ", snap.TBool(False)) # In this example, we add both edge attributes to the network, # but only one src node attribute, and no dst node attributes. edgeattrv = snap.TStrV() edgeattrv.Add("timestamp") # edgeattrv.Add("edgeattr2") srcnodeattrv = snap.TStrV() # srcnodeattrv.Add("srcnodeattr1") dstnodeattrv = snap.TStrV() # net will be an object of type snap.PNEANet G = snap.ToNetwork(snap.PNEANet, table, "srcID", "dstID", srcnodeattrv, dstnodeattrv, edgeattrv, snap.aaFirst) graph = MG.MultiGraph() graph.setGraph(G)
def ManipulateAttributesIter(): ''' Test node, edge attribute functionality using iterators ''' NNodes = 1000 NEdges = 1000 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 print("Added nodes") # create attributes and fill all nodes #attr1 = TStr("str") #attr2 = TStr("int") #attr3 = TStr("float") #attr4 = TStr("default") attr1 = "STR" attr2 = "INT" attr3 = "FLOAT" attr4 = "DEFAULT" # Test column int iterator for node 3, 50, 700, 900 # Check if we can set defaults to 0 fordata. Graph.AddIntAttrN(attr2, 0) NI3 = Graph.GetNI(3) NI50 = Graph.GetNI(50) NI700 = Graph.GetNI(700) NI900 = Graph.GetNI(900) Graph.AddIntAttrDatN(NI3, 3*2, attr2) Graph.AddIntAttrDatN(NI50, 50*2, attr2) Graph.AddIntAttrDatN(NI700, 700*2, attr2) Graph.AddIntAttrDatN(NI900, 900*2, attr2) print("Added attributes") NodeId = 0 NI = Graph.BegNAIntI(attr2) while NI < Graph.EndNAIntI(attr2): if NI.GetDat() != 0: print("Attribute1: %s, Node: %i, Val: %d" % (attr2, NodeId, NI.GetDat())) #print("Attribute: %s, Node: %i, Val: %d" % (attr2(), NodeId, NI.GetDat())) NodeId += 1 NI.Next() # Test column flt iterator for node 3, 50, 700, 900 NI5 = Graph.GetNI(5) NI50 = Graph.GetNI(50) NI300 = Graph.GetNI(300) NI653 = Graph.GetNI(653) Graph.AddFltAttrDatN(NI5, 3.41, attr3) Graph.AddFltAttrDatN(NI50, 2.718, attr3) Graph.AddFltAttrDatN(NI300, 150.0, attr3) Graph.AddFltAttrDatN(NI653, 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("Attribute2: %s, Node: %i, Val: %f" % (attr3, NodeId, NI.GetDat())) #print("Attribute: %s, Node: %i, Val: %f" % (attr3(), NodeId, NI.GetDat())) NodeId += 1 NI.Next() # Test column str iterator for node 3, 50, 700, 900 NI10 = Graph.GetNI(10) NI20 = Graph.GetNI(20) NI400 = Graph.GetNI(400) #Graph.AddStrAttrDatN(10, TStr("abc"), attr1) #Graph.AddStrAttrDatN(20, TStr("def"), attr1) #Graph.AddStrAttrDatN(400, TStr("ghi"), attr1) Graph.AddStrAttrDatN(NI10, "abc", attr1) Graph.AddStrAttrDatN(NI20, "def", attr1) Graph.AddStrAttrDatN(NI400, "ghi", attr1) # this does not show since ""=null #Graph.AddStrAttrDatN(455, TStr(""), attr1) # TODO Graph.AddStrAttrDatN(455, "", attr1) NodeId = 0 NI = Graph.BegNAStrI(attr1) NodeId = 0 while NI < Graph.EndNAStrI(attr1): if NI.GetDat() != snap.TStr.GetNullStr(): print("Attribute3: %s, Node: %i, Val: %s" % (attr1, NodeId, NI.GetDat())) #print("Attribute: %s, Node: %i, Val: %s" % (attr1(), NodeId, NI.GetDat())) NodeId += 1 NI.Next() # Test column iterator over many types (must skip default/deleted attr) NId = 55 NI55 = Graph.GetNI(55) NI80 = Graph.GetNI(80) #Graph.AddStrAttrDatN(NId, TStr("aaa"), attr1) Graph.AddStrAttrDatN(NI55, "aaa", attr1) Graph.AddIntAttrDatN(NI55, 3*2, attr2) Graph.AddFltAttrDatN(NI55, 3.41, attr3) #Graph.AddStrAttrDatN(80, TStr("dont appear"), attr4) # should not show up Graph.AddStrAttrDatN(NI80, "dont appear", attr4) # should not show up attr1idx = Graph.GetAttrIndN(attr1) attr2idx = Graph.GetAttrIndN(attr2) attr3idx = Graph.GetAttrIndN(attr3) attr4idx = Graph.GetAttrIndN(attr4) print("Node attribute indexes: %s %d, %s %d, %s %d, %s %d" % ( attr1, attr1idx, attr2, attr2idx, attr3, attr3idx, attr4, attr4idx)) NI = Graph.GetNI(NId) print("NI attributes: %i, %s %d %.2f" % ( NI.GetId(), Graph.GetStrAttrDatN(NI, attr1), Graph.GetIntAttrDatN(NI, attr2), Graph.GetFltAttrDatN(NI, attr3))) print("ind attributes: %i, %s %d %.2f" % ( NI.GetId(), Graph.GetStrAttrIndDatN(NI, attr1idx), Graph.GetIntAttrIndDatN(NI, attr2idx), Graph.GetFltAttrIndDatN(NI, attr3idx))) NIdAttrName = snap.TStrV() NIdAttrValue = snap.TStrV() NIdIntAttrValue = snap.TIntV() NIdFltAttrValue = snap.TFltV() NIdStrAttrValue = snap.TStrV() Graph.AttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node1: %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) NIdAttrName = snap.TStrV() Graph.IntAttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node11 (int): %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) NIdAttrName = snap.TStrV() Graph.FltAttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node12 (flt): %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) NIdAttrName = snap.TStrV() Graph.StrAttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node13 (str): %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) Graph.IntAttrValueNI(NId, NIdIntAttrValue) AttrLen = NIdIntAttrValue.Len() for i in range(AttrLen): print("Vertical Node14 (int): %i, Attr_Val: %d" % (NId, NIdIntAttrValue.GetI(i)())) Graph.FltAttrValueNI(NId, NIdFltAttrValue) AttrLen = NIdFltAttrValue.Len() for i in range(AttrLen): print("Vertical Node15 (flt): %i, Attr_Val: %.2f" % (NId, NIdFltAttrValue.GetI(i)())) Graph.StrAttrValueNI(NId, NIdStrAttrValue) AttrLen = NIdStrAttrValue.Len() for i in range(AttrLen): print("Vertical Node16 (str): %i, Attr_Val: %s" % (NId, NIdStrAttrValue.GetI(i)())) Graph.DelAttrDatN(NId, attr2) Graph.AttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node2 (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 Node3 (no str) : %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) Graph.AttrValueNI(NId, NIdAttrValue) AttrLen = NIdAttrValue.Len() for i in range(AttrLen): print("Vertical Node4 (no str) : %i, Attr_Val: %s" % (NId, NIdAttrValue.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)) if total/NNodes != 70: print("*** Error3") # Test column iterator for edge EI3 = Graph.GetEI(3) EI55 = Graph.GetEI(55) EI705 = Graph.GetEI(705) EI905 = Graph.GetEI(905) Graph.AddIntAttrDatE(EI3, 3*2, attr2) Graph.AddIntAttrDatE(EI55, 55*2, attr2) Graph.AddIntAttrDatE(EI705, 705*2, attr2) Graph.AddIntAttrDatE(EI905, 905*2, attr2) EdgeId = 0 EI = Graph.BegEAIntI(attr2) while EI < Graph.EndEAIntI(attr2): if EI.GetDat() != snap.TInt.Mn: print("E Attribute1: %s, Edge: %i, Val: %i" % ( attr2, EdgeId, EI.GetDat())) #% (attr2(), EdgeId, EI.GetDat()) EdgeId += 1 EI.Next() # Test column flt iterator for edge Graph.AddFltAttrE(attr3, 0.00) EI5 = Graph.GetEI(5) EI50 = Graph.GetEI(50) EI300 = Graph.GetEI(300) EI653 = Graph.GetEI(653) Graph.AddFltAttrDatE(EI5, 4.41, attr3) Graph.AddFltAttrDatE(EI50, 3.718, attr3) Graph.AddFltAttrDatE(EI300, 151.0, attr3) Graph.AddFltAttrDatE(EI653, 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 Attribute2: %s, Edge: %i, Val: %f" % ( attr3, EdgeId, EI.GetDat())) #(attr3(), EdgeId, EI.GetDat()) EdgeId += 1 EI.Next() # Test column str iterator for edge #Graph.AddStrAttrDatE(10, TStr("abc"), attr1) #Graph.AddStrAttrDatE(20, TStr("def"), attr1) #Graph.AddStrAttrDatE(400, TStr("ghi"), attr1) EI10 = Graph.GetEI(10) EI20 = Graph.GetEI(20) EI400 = Graph.GetEI(400) Graph.AddStrAttrDatE(EI10, "abc", attr1) Graph.AddStrAttrDatE(EI20, "def", attr1) Graph.AddStrAttrDatE(EI400, "ghi", attr1) # this does not show since ""=null #Graph.AddStrAttrDatE(455, TStr(""), attr1) # TODO Graph.AddStrAttrDatE(455, "", attr1) EdgeId = 0 EI = Graph.BegEAStrI(attr1) while EI < Graph.EndEAStrI(attr1): if EI.GetDat() != snap.TStr.GetNullStr(): print("E Attribute3: %s, Edge: %i, Val: %s" % ( attr1, EdgeId, EI.GetDat())) #(attr1(), EdgeId, EI.GetDat()) EdgeId += 1 EI.Next() # Test column iterator over many types (must skip default/deleted attr) EId = 55 EI55 = Graph.GetEI(55) EI80 = Graph.GetEI(80) #Graph.AddStrAttrDatE(EId, TStr("aaa"), attr1) Graph.AddStrAttrDatE(EI55, "aaa", attr1) Graph.AddIntAttrDatE(EI55, 3*2, attr2) Graph.AddFltAttrDatE(EI55, 3.41, attr3) #Graph.AddStrAttrDatE(80, TStr("dont appear"), attr4) # should not show up Graph.AddStrAttrDatE(EI80, "dont appear", attr4) # should not show up attr1idx = Graph.GetAttrIndE(attr1) attr2idx = Graph.GetAttrIndE(attr2) attr3idx = Graph.GetAttrIndE(attr3) attr4idx = Graph.GetAttrIndE(attr4) print("Edge attribute indexes: %s %d, %s %d, %s %d, %s %d" % ( attr1, attr1idx, attr2, attr2idx, attr3, attr3idx, attr4, attr4idx)) EI = Graph.GetEI(EId) print("EI attributes: %i, %s %d %.2f" % ( EI.GetId(), Graph.GetStrAttrDatE(EI, attr1), Graph.GetIntAttrDatE(EI, attr2), Graph.GetFltAttrDatE(EI, attr3))) print("ind attributes: %i, %s %d %.2f" % ( EI.GetId(), Graph.GetStrAttrIndDatE(EI, attr1idx), Graph.GetIntAttrIndDatE(EI, attr2idx), Graph.GetFltAttrIndDatE(EI, attr3idx))) EIdAttrName = snap.TStrV() EIdAttrValue = snap.TStrV() Graph.AttrNameEI(EId, EIdAttrName) AttrLen = EIdAttrName.Len() for i in range(AttrLen): print("Vertical Edge1: %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 Edge2 (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 Edge3 (no str) : %i, Attr: %s" % (EId, EIdAttrName.GetI(i)())) Graph.AttrValueEI(EId, EIdAttrValue) AttrLen = EIdAttrValue.Len() for i in range(AttrLen): print("Vertical Edge4 (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)) if total/NNodes != 70: print("*** Error4") Graph.Clr()
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()
context = snap.TTableContext() # Construct the graph logging.info('Building Multi-Modal Network') Graph = snap.TMMNet.New() # Loading Modes try: chemical_mode_file = config.get('Modes', 'Chemical') cmschema = snap.Schema() cmschema.Add(snap.TStrTAttrPr("ChemicalId", snap.atStr)) cmschema.Add(snap.TStrTAttrPr("datasetId", snap.atStr)) chemical_mode = snap.TTable.LoadSS(cmschema, chemical_mode_file, context, "\t", snap.TBool(False)) logging.info('Done loading Chemical Mode') snap.LoadModeNetToNet(Graph, "Chemical", chemical_mode, "ChemicalId", snap.TStrV()) except ConfigParser.NoOptionError: logging.info('Skipping Chemical Mode') try: function_mode_file = config.get('Modes', 'Function') fmschema = snap.Schema() fmschema.Add(snap.TStrTAttrPr("FunctionId", snap.atStr)) fmschema.Add(snap.TStrTAttrPr("datasetId", snap.atStr)) function_mode = snap.TTable.LoadSS(fmschema, function_mode_file, context, "\t", snap.TBool(False)) logging.info('Done loading Function Mode') snap.LoadModeNetToNet(Graph, "Function", function_mode, "FunctionId", snap.TStrV()) except ConfigParser.NoOptionError: logging.info('Skipping Function Mode')
useredges.to_csv('temp/mergededges.csv', index=None) # Build graph from temp files using SNAP library context = snap.TTableContext() e_schema = snap.Schema() e_schema.Add(snap.TStrTAttrPr("source", snap.atStr)) e_schema.Add(snap.TStrTAttrPr("target", snap.atStr)) n_schema = snap.Schema() n_schema.Add(snap.TStrTAttrPr("username", snap.atStr)) edgetable = snap.TTable.LoadSS(e_schema, 'temp/mergededges.csv', context, ",", snap.TBool(True)) nodetable = snap.TTable.LoadSS(n_schema, 'temp/mergednodes.csv', context, ",", snap.TBool(True)) edgeattrv = snap.TStrV() nodeattrv = snap.TStrV() nodeattrv.Add("username") net = snap.ToNetwork(snap.PNEANet, edgetable, "source", "target", edgeattrv, nodetable, "username", nodeattrv, snap.aaFirst) # Need to remove self-edges to compute rich club coefficient snap.DelSelfEdges(net) # Store the results name = str(pid) + '_usergraph' snap.SaveEdgeListNet(net, outpath + name + '.csv', 'Network of issues, PR and commits') generateTables(outpath, name, net)