SNAP Library 6.0, User Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
conv.cpp
Go to the documentation of this file.
1 // Conversion Functions
3 namespace TSnap {
4 
5 
6 int LoadModeNetToNet(PMMNet Graph, const TStr& Name, PTable Table, const TStr& NCol,
7  TStrV& NodeAttrV) {
8  Graph->AddModeNet(Name);
9  TModeNet& Net = Graph->GetModeNetByName(Name);
10  return LoadMode(Net, Table, NCol, NodeAttrV);
11 }
12 
13 
14 int LoadMode(TModeNet& Graph, PTable Table, const TStr& NCol,
15  TStrV& NodeAttrV) {
16 
17  const TAttrType NodeType = Table->GetColType(NCol);
18  const TInt NColIdx = Table->GetColIdx(NCol);
19 
20  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
21  if ((Table->Next)[CurrRowIdx] == Table->Invalid) {
22  continue;
23  }
24 
25  // add src and dst nodes to graph if they are not seen earlier
26  TInt NVal;
27  if (NodeType == atFlt) {
28  return -1;
29  } else if (NodeType == atInt || NodeType == atStr) {
30  if (NodeType == atInt) {
31  NVal = (Table->IntCols)[NColIdx][CurrRowIdx];
32  } else {
33  NVal = (Table->StrColMaps)[NColIdx][CurrRowIdx];
34  if (strlen(Table->GetContextKey(NVal)) == 0) { continue; } //illegal value
35  }
36  if (!Graph.IsNode(NVal)) {Graph.AddNode(NVal); }
37  }
38 
39  // Aggregate edge attributes and add to graph
40  for (TInt i = 0; i < NodeAttrV.Len(); i++) {
41  TStr ColName = NodeAttrV[i];
42  TAttrType T = Table->GetColType(ColName);
43  TInt Index = Table->GetColIdx(ColName);
44  switch (T) {
45  case atInt:
46  Graph.AddIntAttrDatN(NVal, Table->IntCols[Index][CurrRowIdx], ColName);
47  break;
48  case atFlt:
49  Graph.AddFltAttrDatN(NVal, Table->FltCols[Index][CurrRowIdx], ColName);
50  break;
51  case atStr:
52  Graph.AddStrAttrDatN(NVal, Table->GetStrValIdx(Index, CurrRowIdx), ColName);
53  break;
54  }
55  }
56  }
57  return 1;
58 }
59 
60 int LoadCrossNetToNet(PMMNet Graph, const TStr& Mode1, const TStr& Mode2, const TStr& CrossName,
61  PTable Table, const TStr& SrcCol, const TStr& DstCol, TStrV& EdgeAttrV)
62 {
63  Graph->AddCrossNet(Mode1, Mode2, CrossName);
64  TCrossNet& Net = Graph->GetCrossNetByName(CrossName);
65  return LoadCrossNet(Net, Table, SrcCol, DstCol, EdgeAttrV);
66 }
67 
68 
69 int LoadCrossNet(TCrossNet& Graph, PTable Table, const TStr& SrcCol, const TStr& DstCol,
70  TStrV& EdgeAttrV)
71 {
72 
73  const TAttrType NodeType = Table->GetColType(SrcCol);
74  Assert(NodeType == Table->GetColType(DstCol));
75  const TInt SrcColIdx = Table->GetColIdx(SrcCol);
76  const TInt DstColIdx = Table->GetColIdx(DstCol);
77 
78  // node values - i.e. the unique values of src/dst col
79  //THashSet<TInt> IntNodeVals; // for both int and string node attr types.
80  THash<TFlt, TInt> FltNodeVals;
81 
82  // make single pass over all rows in the table
83  for (int CurrRowIdx = 0; CurrRowIdx < (Table->Next).Len(); CurrRowIdx++) {
84  if ((Table->Next)[CurrRowIdx] == Table->Invalid) {
85  continue;
86  }
87 
88  // add src and dst nodes to graph if they are not seen earlier
89  TInt SVal, DVal;
90  if (NodeType == atFlt) {
91  return -1;
92  } else if (NodeType == atInt || NodeType == atStr) {
93  if (NodeType == atInt) {
94  SVal = (Table->IntCols)[SrcColIdx][CurrRowIdx];
95  DVal = (Table->IntCols)[DstColIdx][CurrRowIdx];
96  } else {
97  SVal = (Table->StrColMaps)[SrcColIdx][CurrRowIdx];
98  if (strlen(Table->GetContextKey(SVal)) == 0) { continue; } //illegal value
99  DVal = (Table->StrColMaps)[DstColIdx][CurrRowIdx];
100  if (strlen(Table->GetContextKey(DVal)) == 0) { continue; } //illegal value
101  }
102  }
103 
104  // add edge and edge attributes
105  if (Graph.AddEdge(SVal, DVal, CurrRowIdx) == -1) { return -1; }
106 
107  // Aggregate edge attributes and add to graph
108  for (TInt i = 0; i < EdgeAttrV.Len(); i++) {
109  TStr ColName = EdgeAttrV[i];
110  TAttrType T = Table->GetColType(ColName);
111  TInt Index = Table->GetColIdx(ColName);
112  switch (T) {
113  case atInt:
114  Graph.AddIntAttrDatE(CurrRowIdx, Table->IntCols[Index][CurrRowIdx], ColName);
115  break;
116  case atFlt:
117  Graph.AddFltAttrDatE(CurrRowIdx, Table->FltCols[Index][CurrRowIdx], ColName);
118  break;
119  case atStr:
120  Graph.AddStrAttrDatE(CurrRowIdx, Table->GetStrValIdx(Index, CurrRowIdx), ColName);
121  break;
122  }
123  }
124  }
125  return 1;
126 }
127 
128 }; //namespace TSnap
Main namespace for all the Snap global entities.
Definition: alg.h:1
int AddFltAttrDatE(const TCrossEdgeI &EdgeI, const TFlt &value, const TStr &attr)
Attribute based add function for attr to Flt value.
Definition: mmnet.h:410
int AddStrAttrDatE(const TCrossEdgeI &EdgeI, const TStr &value, const TStr &attr)
Attribute based add function for attr to Str value.
Definition: mmnet.h:407
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
int LoadModeNetToNet(PMMNet Graph, const TStr &Name, PTable Table, const TStr &NCol, TStrV &NodeAttrV)
Loads a mode, with name Name, into the PMMNet from the TTable. NCol specifies the node id column and ...
Definition: conv.cpp:6
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.cpp:535
Definition: gbase.h:23
int AddFltAttrDatN(const TNodeI &NodeI, const TFlt &value, const TStr &attr)
Attribute based add function for attr to Flt value.
Definition: network.h:2868
int AddIntAttrDatE(const TCrossEdgeI &EdgeI, const TInt &value, const TStr &attr)
Attribute based add function for attr to Int value.
Definition: mmnet.h:404
int LoadCrossNetToNet(PMMNet Graph, const TStr &Mode1, const TStr &Mode2, const TStr &CrossName, PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &EdgeAttrV)
Loads a crossnet from Mode1 to Mode2, with name CrossName, from the provided TTable. EdgeAttrV specifies edge attributes.
Definition: conv.cpp:60
int AddStrAttrDatN(const TNodeI &NodeI, const TStr &value, const TStr &attr)
Attribute based add function for attr to Str value.
Definition: network.h:2865
int LoadCrossNet(TCrossNet &Graph, PTable Table, const TStr &SrcCol, const TStr &DstCol, TStrV &EdgeAttrV)
Loads the edges from the TTable and EdgeAttrV specifies columns containing edge attributes.
Definition: conv.cpp:69
#define Assert(Cond)
Definition: bd.h:251
int LoadMode(TModeNet &Graph, PTable Table, const TStr &NCol, TStrV &NodeAttrV)
Loads the nodes specified in column NCol from the TTable with the attributes specified in NodeAttrV...
Definition: conv.cpp:14
The nodes of one particular mode in a TMMNet, and their neighbor vectors as TIntV attributes...
Definition: mmnet.h:23
Definition: dt.h:1137
int AddEdge(const int &sourceNId, const int &destNId, int EId=-1)
Adds an edge to the CrossNet; Mode1 NId should be the sourceNId always, regardless of whether edge is...
Definition: mmnet.cpp:233
Definition: dt.h:412
Definition: hash.h:97
Definition: gbase.h:23
Definition: bd.h:196
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:2385
Definition: gbase.h:23
int AddIntAttrDatN(const TNodeI &NodeI, const TInt &value, const TStr &attr)
Attribute based add function for attr to Int value.
Definition: network.h:2862
Implements a single CrossNet consisting of edges between two TModeNets (could be the same TModeNet) ...
Definition: mmnet.h:133