SNAP Library 3.0, User Reference  2016-07-20 17:56:49
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rolx.cpp File Reference
#include "stdafx.h"
#include "Snap.h"
#include "rolx.h"

Go to the source code of this file.

Functions

void PrintFeatures (const TIntFtrH &Features)
 Prints all nodes' feature. More...
 
TIntFtrH CreateEmptyFeatures (const PUNGraph Graph)
 Creates an empty node-feature mapping of all nodes in the given graph. More...
 
TIntFtrH CreateEmptyFeatures (const TIntFtrH &Features)
 Creates an empty node-feature mapping of all nodes from an existing mapping. More...
 
int GetNumFeatures (const TIntFtrH &Features)
 Gets number of features from the node-feature mapping. More...
 
TFtr GetNthFeature (const TIntFtrH &Features, const int N)
 Gets the n-th feature of all nodes. More...
 
TIntFtrH ExtractFeatures (const PUNGraph Graph)
 Performs feature extraction, the first step of RolX. More...
 
void AddNeighborhoodFeatures (const PUNGraph Graph, TIntFtrH &Features)
 Adds neighborhood features (local + egonet) to the node-feature mapping. More...
 
void AddRecursiveFeatures (const PUNGraph Graph, TIntFtrH &Features)
 Adds recursive features to the node-feature mapping. More...
 
void AddLocalFeatures (const PUNGraph Graph, TIntFtrH &Features)
 Adds local features to the node-feature mapping. More...
 
void AddEgonetFeatures (const PUNGraph Graph, TIntFtrH &Features)
 Adds egonet features to the node-feature mapping. More...
 
TIntFtrH GenerateRecursiveFeatures (const PUNGraph Graph, const TIntFtrH &CurrFeatures)
 Generates recursive features out of current features. More...
 
TIntFtrH PruneRecursiveFeatures (const PUNGraph Graph, const TIntFtrH &Features, const TIntFtrH &NewFeatures, const int SimilarityThreshold)
 Prunes recursive features. More...
 
void AppendFeatures (TIntFtrH &DstFeatures, const TIntFtrH &SrcFeatures, const int ColIdx)
 Appends all src features to dst features. More...
 
TIntFtrH CalcVerticalLogBinning (const TIntFtrH &Features, const float BinFraction)
 Calculates vertical logarithmic binning features from the given features. More...
 
PUNGraph BuildFeatureGraph (const TIntFtrH &LogBinFeatures, const int SimilarityThreshold)
 Builds s-friend graph given similarity threshold. More...
 
TIntFtrH SummarizeConnectedComponents (const PUNGraph FeatureGraph, const TIntFtrH &Features, const TIntFtrH &NewFeatures)
 Summarizes s-friend graph and return retained features. More...
 
TVec< TIntGetNIdSorted (const TIntFtrH &Features, const int Idx)
 Sorts the Idx-th feature, return the list of corresponding node ID. More...
 
void AssignBinValue (const TVec< TInt > &SortedNId, const float BinFraction, TIntFtrH &LogBinFeatures)
 Assigns logarithmic binning value to features. More...
 
bool IsSimilarFeature (const TFtr &F1, const TFtr &F2, const int SimilarityThreshold)
 Whether the two features are similar, given similarity threshold. More...
 
TFltVV ConvertFeatureToMatrix (const TIntFtrH &Features, const TIntIntH &NodeIdMtxIdxH)
 Converts node-feature mapping to matrix. (i, j): i-th node, j-th feature. More...
 
void PrintMatrix (const TFltVV &Matrix)
 Prints feature matrix to stdout. More...
 
TFltVV CreateRandMatrix (const int XDim, const int YDim)
 Creates a random matrix with specified dimension. More...
 
bool FltIsZero (const TFlt Number)
 Whether the float is zero. More...
 
void CalcNonNegativeFactorization (const TFltVV &V, const int NumRoles, TFltVV &W, TFltVV &H, const double Threshold)
 Performs non-negative factorization V = WH. 2nd dim of W == number of roles. More...
 
TFlt CalcDescriptionLength (const TFltVV &V, const TFltVV &G, const TFltVV &F)
 Calculates the description length L = M + E. More...
 
TIntIntH CreateNodeIdMtxIdxHash (const TIntFtrH &Features)
 Creates the mapping of <node ID, matrix index>. More...
 
int GetMtxIdx (const TInt NodeId, const TIntIntH &NodeIdMtxIdxH)
 Gets matrix index of the node ID. More...
 
int GetNodeId (const TInt MtxId, const TIntIntH &NodeIdMtxIdxH)
 
TIntIntH FindRoles (const TFltVV &G, const TIntIntH &NodeIdMtxIdxH)
 Gets matrix index of the node ID. More...
 
void PlotRoles (const PUNGraph Graph, const TIntIntH &Roles)
 Plots found roles on a picture (.png). More...
 
void PrintRoles (const TIntIntH &Roles)
 Prints found roles on stdout. More...
 
void FPrintMatrix (const TFltVV &Matrix, const TStr &Path)
 Prints feature matrix to file. More...
 
void FPrintRoles (const TIntIntH &Roles, const TStr &Path)
 Prints found roles to file. More...
 

Function Documentation

void AddEgonetFeatures ( const PUNGraph  Graph,
TIntFtrH Features 
)

Adds egonet features to the node-feature mapping.

Definition at line 87 of file rolx.cpp.

87  {
88  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
89  int NId = NI.GetId();
90  int ArndEdges;
91  PUNGraph Egonet = TSnap::GetEgonet(Graph, NId, ArndEdges);
92  Features.GetDat(NId).Add(Egonet->GetEdges());
93  Features.GetDat(NId).Add(ArndEdges);
94  }
95 }
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:82
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
PUNGraph GetEgonet(const PUNGraph &Graph, const int CtrNId, int &ArndEdges)
Returns the egonet of node CtrNId as center in undirected graph Graph. And returns number of edges ar...
Definition: subgraph.cpp:82
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
void AddLocalFeatures ( const PUNGraph  Graph,
TIntFtrH Features 
)

Adds local features to the node-feature mapping.

Definition at line 81 of file rolx.cpp.

81  {
82  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
83  Features.GetDat(TInt(NI.GetId())).Add(NI.GetInDeg());
84  }
85 }
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
Definition: dt.h:1044
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
void AddNeighborhoodFeatures ( const PUNGraph  Graph,
TIntFtrH Features 
)

Adds neighborhood features (local + egonet) to the node-feature mapping.

Definition at line 57 of file rolx.cpp.

57  {
58  AddLocalFeatures(Graph, Features);
59  printf("finish local features\n");
60  AddEgonetFeatures(Graph, Features);
61  printf("finish egonet features\n");
62 }
void AddLocalFeatures(const PUNGraph Graph, TIntFtrH &Features)
Adds local features to the node-feature mapping.
Definition: rolx.cpp:81
void AddEgonetFeatures(const PUNGraph Graph, TIntFtrH &Features)
Adds egonet features to the node-feature mapping.
Definition: rolx.cpp:87
void AddRecursiveFeatures ( const PUNGraph  Graph,
TIntFtrH Features 
)

Adds recursive features to the node-feature mapping.

Definition at line 64 of file rolx.cpp.

64  {
65  int SimilarityThreshold = 0;
66  TIntFtrH RetainedFeatures = Features;
67  while (true) {
68  TIntFtrH NewFeatures = GenerateRecursiveFeatures(Graph, RetainedFeatures);
69  RetainedFeatures = PruneRecursiveFeatures(Graph, Features, NewFeatures,
70  SimilarityThreshold);
71  if (0 == GetNumFeatures(RetainedFeatures)) {
72  break;
73  }
74  AppendFeatures(Features, RetainedFeatures);
75  ++SimilarityThreshold;
76  printf("recursion %d: ", SimilarityThreshold);
77  printf("current feature number %d\n", GetNumFeatures(Features));
78  }
79 }
int GetNumFeatures(const TIntFtrH &Features)
Gets number of features from the node-feature mapping.
Definition: rolx.cpp:35
TIntFtrH PruneRecursiveFeatures(const PUNGraph Graph, const TIntFtrH &Features, const TIntFtrH &NewFeatures, const int SimilarityThreshold)
Prunes recursive features.
Definition: rolx.cpp:119
TIntFtrH GenerateRecursiveFeatures(const PUNGraph Graph, const TIntFtrH &CurrFeatures)
Generates recursive features out of current features.
Definition: rolx.cpp:97
Definition: hash.h:88
void AppendFeatures(TIntFtrH &DstFeatures, const TIntFtrH &SrcFeatures, const int ColIdx)
Appends all src features to dst features.
Definition: rolx.cpp:131
void AppendFeatures ( TIntFtrH DstFeatures,
const TIntFtrH SrcFeatures,
const int  ColIdx 
)

Appends all src features to dst features.

Definition at line 131 of file rolx.cpp.

132  {
133  for (TIntFtrH::TIter HI = SrcFeatures.BegI();
134  HI < SrcFeatures.EndI();
135  HI++) {
136  const TFtr& Feature = HI.GetDat();
137  if (ColIdx >= 0) {
138  DstFeatures.GetDat(HI.GetKey()).Add(Feature[ColIdx]);
139  } else {
140  for (int i = 0; i < Feature.Len(); ++i) {
141  DstFeatures.GetDat(HI.GetKey()).Add(Feature[i]);
142  }
143  }
144  }
145 }
TIter BegI() const
Definition: hash.h:171
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TIter EndI() const
Definition: hash.h:176
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
Definition: ds.h:807
void AssignBinValue ( const TVec< TInt > &  SortedNId,
const float  BinFraction,
TIntFtrH LogBinFeatures 
)

Assigns logarithmic binning value to features.

Definition at line 212 of file rolx.cpp.

213  {
214  int NumNodes = LogBinFeatures.Len();
215  int NumAssigned = 0;
216  int BinValue = 0;
217  while (NumAssigned < NumNodes) {
218  int NumToAssign = ceil(BinFraction * (NumNodes - NumAssigned));
219  for (int i = NumAssigned; i < NumAssigned + NumToAssign; ++i) {
220  int NId = SortedNId[i];
221  LogBinFeatures.GetDat(NId).Add(BinValue);
222  }
223  NumAssigned += NumToAssign;
224  ++BinValue;
225  }
226 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
int Len() const
Definition: hash.h:186
PUNGraph BuildFeatureGraph ( const TIntFtrH LogBinFeatures,
const int  SimilarityThreshold 
)

Builds s-friend graph given similarity threshold.

Definition at line 158 of file rolx.cpp.

159  {
160  PUNGraph FeatureGraph = PUNGraph::New();
161  const int NumFeatures = GetNumFeatures(LogBinFeatures);
162  for (int i = 0; i < NumFeatures; ++i) {
163  FeatureGraph->AddNode(i);
164  }
165  for (int i = 0; i < NumFeatures; ++i) {
166  TFtr IthFeature = GetNthFeature(LogBinFeatures, i);
167  for (int j = i + 1; j < NumFeatures; ++j) {
168  TFtr JthFeature = GetNthFeature(LogBinFeatures, j);
169  if (IsSimilarFeature(IthFeature, JthFeature, SimilarityThreshold) &&
170  !FeatureGraph->IsEdge(i, j)) {
171  FeatureGraph->AddEdge(i, j);
172  }
173  }
174  }
175  return FeatureGraph;
176 }
int GetNumFeatures(const TIntFtrH &Features)
Gets number of features from the node-feature mapping.
Definition: rolx.cpp:35
static TPt New()
Definition: bd.h:479
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:8
TFtr GetNthFeature(const TIntFtrH &Features, const int N)
Gets the n-th feature of all nodes.
Definition: rolx.cpp:39
bool IsSimilarFeature(const TFtr &F1, const TFtr &F2, const int SimilarityThreshold)
Whether the two features are similar, given similarity threshold.
Definition: rolx.cpp:228
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the graph.
Definition: graph.cpp:92
bool IsEdge(const int &SrcNId, const int &DstNId) const
Tests whether an edge between node IDs SrcNId and DstNId exists in the graph.
Definition: graph.cpp:137
TFlt CalcDescriptionLength ( const TFltVV V,
const TFltVV G,
const TFltVV F 
)

Calculates the description length L = M + E.

Definition at line 352 of file rolx.cpp.

353  {
354  int B = 64;
355  int M = B * V.GetYDim() * (V.GetXDim() + F.GetYDim());
356  TFlt E = 0;
357  TFltVV GF(G.GetXDim(), F.GetYDim());
358  TLinAlg::Multiply(G, F, GF);
359  for (int i = 0; i < V.GetXDim(); ++i) {
360  for (int j = 0; j < V.GetYDim(); ++j) {
361  TFlt ValueV = V(i, j);
362  TFlt ValueGF = GF(i, j);
363  if (FltIsZero(ValueV)) {
364  E += ValueGF;
365  } else if (!FltIsZero(ValueGF)) {
366  E += ValueV * TMath::Log(ValueV / ValueGF) - ValueV + ValueGF;
367  }
368  }
369  }
370  return M + E;
371 }
int GetXDim() const
Definition: ds.h:2184
Definition: dt.h:1293
Definition: ds.h:2157
bool FltIsZero(const TFlt Number)
Whether the float is zero.
Definition: rolx.cpp:282
static void Multiply(const TFltVV &A, const TFltV &x, TFltV &y)
Definition: linalg.cpp:428
int GetYDim() const
Definition: ds.h:2185
static double Log(const double &Val)
Definition: xmath.h:14
void CalcNonNegativeFactorization ( const TFltVV V,
const int  NumRoles,
TFltVV W,
TFltVV H,
const double  Threshold 
)

Performs non-negative factorization V = WH. 2nd dim of W == number of roles.

Definition at line 286 of file rolx.cpp.

287  {
288  double Cost = 100, NewCost = 0;
289  int NumNodes = V.GetXDim();
290  int NumFeatures = V.GetYDim();
291  W = CreateRandMatrix(NumNodes, NumRoles);
292  H = CreateRandMatrix(NumRoles, NumFeatures);
293  TFltVV NewW(NumNodes, NumRoles);
294  TFltVV NewH(NumRoles, NumFeatures);
295  TFltVV Product(NumNodes, NumFeatures);
296  TFltV Sum(NumRoles);
297  TFltVV *PW = &W, *PH = &H, *PNewW = &NewW, *PNewH = &NewH, *Tmp;
298  //int IterNum = 1;
299  while (TFlt::Abs((NewCost - Cost)/Cost) > Threshold) {
300  TLinAlg::Multiply(*PW, *PH, Product);
301  //converge condition
302  Cost = NewCost;
303  NewCost = 0;
304  for (int i = 0; i < NumNodes; i++) {
305  for (int j = 0; j < NumFeatures; j++) {
306  NewCost += V(i, j) * TMath::Log(Product(i, j)) - Product(i, j);
307  }
308  }
309  // update W
310  for (int i = 0; i < NumNodes; i++) {
311  for (int a = 0; a < NumRoles; a++) {
312  double SumU = 0;
313  for (int u = 0; u < NumFeatures; ++u) {
314  if (!FltIsZero(Product(i, u))) {
315  SumU += V(i, u) / Product(i, u) * PH->At(a, u);
316  }
317  }
318  PNewW->At(i, a) = PW->At(i, a) * SumU;
319  }
320  }
321  for (int i = 0; i < NumRoles; i++) {
322  Sum[i] = 0;
323  }
324  for (int i = 0; i < NumNodes; i++) {
325  for (int j = 0; j < NumRoles; j++) {
326  Sum[j] += PNewW->At(i, j);
327  }
328  }
329  for (int i = 0; i < NumNodes; i++) {
330  for (int j = 0; j < NumRoles; j++) {
331  PNewW->At(i, j) /= Sum[j];
332  }
333  }
334  // update H
335  for (int a = 0; a < NumRoles; a++) {
336  for (int u = 0; u < NumFeatures; u++) {
337  double SumI = 0;
338  for (int i = 0; i < NumNodes; ++i) {
339  if (!FltIsZero(Product(i, u))) {
340  SumI += PW->At(i, a) * V(i, u) / Product(i, u);
341  }
342  }
343  PNewH->At(a, u) = PH->At(a, u) * SumI;
344  }
345  }
346  //printf("iteration %d, cost is %f\n", IterNum++, NewCost);
347  Tmp = PW; PW = PNewW; PNewW = Tmp;
348  Tmp = PH; PH = PNewH; PNewH = Tmp;
349  }
350 }
int GetXDim() const
Definition: ds.h:2184
Definition: ds.h:2157
bool FltIsZero(const TFlt Number)
Whether the float is zero.
Definition: rolx.cpp:282
static void Multiply(const TFltVV &A, const TFltV &x, TFltV &y)
Definition: linalg.cpp:428
static double Abs(const double &Flt)
Definition: dt.h:1337
TFltVV CreateRandMatrix(const int XDim, const int YDim)
Creates a random matrix with specified dimension.
Definition: rolx.cpp:270
int GetYDim() const
Definition: ds.h:2185
static double Log(const double &Val)
Definition: xmath.h:14
const TVal & At(const int &X, const int &Y) const
Definition: ds.h:2190
TIntFtrH CalcVerticalLogBinning ( const TIntFtrH Features,
const float  BinFraction 
)

Calculates vertical logarithmic binning features from the given features.

Definition at line 147 of file rolx.cpp.

148  {
149  const int NumFeatures = GetNumFeatures(Features);
150  TIntFtrH LogBinFeatures = CreateEmptyFeatures(Features);
151  for (int i = 0; i < NumFeatures; ++i) {
152  TVec<TInt> SortedNId = GetNIdSorted(Features, i);
153  AssignBinValue(SortedNId, BinFraction, LogBinFeatures);
154  }
155  return LogBinFeatures;
156 }
int GetNumFeatures(const TIntFtrH &Features)
Gets number of features from the node-feature mapping.
Definition: rolx.cpp:35
TIntFtrH CreateEmptyFeatures(const PUNGraph Graph)
Creates an empty node-feature mapping of all nodes in the given graph.
Definition: rolx.cpp:19
void AssignBinValue(const TVec< TInt > &SortedNId, const float BinFraction, TIntFtrH &LogBinFeatures)
Assigns logarithmic binning value to features.
Definition: rolx.cpp:212
Definition: hash.h:88
TVec< TInt > GetNIdSorted(const TIntFtrH &Features, const int Idx)
Sorts the Idx-th feature, return the list of corresponding node ID.
Definition: rolx.cpp:199
TFltVV ConvertFeatureToMatrix ( const TIntFtrH Features,
const TIntIntH NodeIdMtxIdxH 
)

Converts node-feature mapping to matrix. (i, j): i-th node, j-th feature.

Definition at line 239 of file rolx.cpp.

240  {
241  const int NumNodes = Features.Len();
242  const int NumFeatures = GetNumFeatures(Features);
243  TFltVV FeaturesMtx(NumNodes, NumFeatures);
244  for (TIntFtrH::TIter HI = Features.BegI(); HI < Features.EndI(); HI++) {
245  int i = GetMtxIdx(HI.GetKey(), NodeIdMtxIdxH);
246  for (int j = 0; j < NumFeatures; ++j) {
247  FeaturesMtx(i, j) = HI.GetDat()[j];
248  }
249  }
250  return FeaturesMtx;
251 }
int GetNumFeatures(const TIntFtrH &Features)
Gets number of features from the node-feature mapping.
Definition: rolx.cpp:35
TIter BegI() const
Definition: hash.h:171
TIter EndI() const
Definition: hash.h:176
int GetMtxIdx(const TInt NodeId, const TIntIntH &NodeIdMtxIdxH)
Gets matrix index of the node ID.
Definition: rolx.cpp:383
Definition: ds.h:2157
int Len() const
Definition: hash.h:186
TIntFtrH CreateEmptyFeatures ( const PUNGraph  Graph)

Creates an empty node-feature mapping of all nodes in the given graph.

Definition at line 19 of file rolx.cpp.

19  {
20  TIntFtrH EmptyFeatures;
21  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
22  EmptyFeatures.AddDat(TInt(NI.GetId()), TFtr());
23  }
24  return EmptyFeatures;
25 }
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
Definition: dt.h:1044
Definition: hash.h:88
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
TVec< TFlt > TFtr
Feature of a node.
Definition: rolx.h:6
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
TIntFtrH CreateEmptyFeatures ( const TIntFtrH Features)

Creates an empty node-feature mapping of all nodes from an existing mapping.

Definition at line 27 of file rolx.cpp.

27  {
28  TIntFtrH EmptyFeatures;
29  for (TIntFtrH::TIter HI = Features.BegI(); HI < Features.EndI(); HI++) {
30  EmptyFeatures.AddDat(HI.GetKey(), TFtr());
31  }
32  return EmptyFeatures;
33 }
TIter BegI() const
Definition: hash.h:171
TIter EndI() const
Definition: hash.h:176
Definition: hash.h:88
TVec< TFlt > TFtr
Feature of a node.
Definition: rolx.h:6
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
TIntIntH CreateNodeIdMtxIdxHash ( const TIntFtrH Features)

Creates the mapping of <node ID, matrix index>.

Definition at line 373 of file rolx.cpp.

373  {
374  TIntIntH H;
375  TInt Idx = 0;
376  for (TIntFtrH::TIter HI = Features.BegI(); HI < Features.EndI(); HI++) {
377  H.AddDat(HI.GetKey(), Idx);
378  Idx++;
379  }
380  return H;
381 }
TIter BegI() const
Definition: hash.h:171
TIter EndI() const
Definition: hash.h:176
Definition: dt.h:1044
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
TFltVV CreateRandMatrix ( const int  XDim,
const int  YDim 
)

Creates a random matrix with specified dimension.

Definition at line 270 of file rolx.cpp.

270  {
271  int Seed = 13;
272  TFltVV Matrix(XDim, YDim);
273  for (int i = 0; i < XDim; ++i) {
274  for (int j = 0; j < YDim; ++j) {
275  Matrix(i, j) = (double)Seed / 10007;
276  Seed = (Seed * 1871) % 10007;
277  }
278  }
279  return Matrix;
280 }
Definition: ds.h:2157
TIntFtrH ExtractFeatures ( const PUNGraph  Graph)

Performs feature extraction, the first step of RolX.

Definition at line 48 of file rolx.cpp.

48  {
49  TIntFtrH Features = CreateEmptyFeatures(Graph);
50  AddNeighborhoodFeatures(Graph, Features);
51  printf("finish neighborhood features\n");
52  AddRecursiveFeatures(Graph, Features);
53  printf("finish recursive features\n");
54  return Features;
55 }
TIntFtrH CreateEmptyFeatures(const PUNGraph Graph)
Creates an empty node-feature mapping of all nodes in the given graph.
Definition: rolx.cpp:19
void AddRecursiveFeatures(const PUNGraph Graph, TIntFtrH &Features)
Adds recursive features to the node-feature mapping.
Definition: rolx.cpp:64
void AddNeighborhoodFeatures(const PUNGraph Graph, TIntFtrH &Features)
Adds neighborhood features (local + egonet) to the node-feature mapping.
Definition: rolx.cpp:57
Definition: hash.h:88
TIntIntH FindRoles ( const TFltVV G,
const TIntIntH NodeIdMtxIdxH 
)

Gets matrix index of the node ID.

Definition at line 398 of file rolx.cpp.

398  {
399  TIntIntH Roles;
400  for (int i = 0; i < G.GetXDim(); i++) {
401  int Role = -1;
402  TFlt Max = TFlt::Mn;
403  for (int j = 0; j < G.GetYDim(); j ++) {
404  if (G(i, j) > Max) {
405  Max = G(i, j);
406  Role = j;
407  }
408  }
409  int NodeId = GetNodeId(i, NodeIdMtxIdxH);
410  Roles.AddDat(NodeId, Role);
411  }
412  return Roles;
413 }
int GetXDim() const
Definition: ds.h:2184
Definition: dt.h:1293
int GetNodeId(const TInt MtxId, const TIntIntH &NodeIdMtxIdxH)
Definition: rolx.cpp:387
int GetYDim() const
Definition: ds.h:2185
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
static const double Mn
Definition: dt.h:1297
bool FltIsZero ( const TFlt  Number)

Whether the float is zero.

Definition at line 282 of file rolx.cpp.

282  {
283  return TFlt::Abs(Number) < TFlt::Eps;
284 }
static double Abs(const double &Flt)
Definition: dt.h:1337
static const double Eps
Definition: dt.h:1301
void FPrintMatrix ( const TFltVV Matrix,
const TStr Path 
)

Prints feature matrix to file.

Definition at line 434 of file rolx.cpp.

434  {
435  FILE *Fp;
436  Fp = fopen(Path.CStr(), "w");
437  int XDim = Matrix.GetXDim();
438  int YDim = Matrix.GetYDim();
439  for (int i = 0; i < XDim; ++i) {
440  for (int j = 0; j < YDim; ++j) {
441  if (j != 0) {
442  fprintf(Fp, " ");
443  }
444  fprintf(Fp, "%f", Matrix(i, j)());
445  }
446  fprintf(Fp, "\n");
447  }
448  fclose(Fp);
449 }
int GetXDim() const
Definition: ds.h:2184
int GetYDim() const
Definition: ds.h:2185
char * CStr()
Definition: dt.h:476
void FPrintRoles ( const TIntIntH Roles,
const TStr Path 
)

Prints found roles to file.

Definition at line 451 of file rolx.cpp.

451  {
452  FILE *Fp;
453  Fp = fopen(Path.CStr(), "w");
454  fprintf(Fp, "--roles (node ID role ID)--\n\n");
455  for (TIntIntH::TIter HI = Roles.BegI(); HI < Roles.EndI(); HI++) {
456  fprintf(Fp, "%d\t%d\n", HI.GetKey()(), HI.GetDat()());
457  }
458  fclose(Fp);
459 }
TIter BegI() const
Definition: hash.h:171
TIter EndI() const
Definition: hash.h:176
char * CStr()
Definition: dt.h:476
TIntFtrH GenerateRecursiveFeatures ( const PUNGraph  Graph,
const TIntFtrH CurrFeatures 
)

Generates recursive features out of current features.

Definition at line 97 of file rolx.cpp.

98  {
99  const int NumCurrFeatures = GetNumFeatures(CurrFeatures);
100  if (0 == NumCurrFeatures) {
101  return CurrFeatures;
102  }
103  TIntFtrH NewFeatures = CreateEmptyFeatures(CurrFeatures);
104  for (int i = 0; i < NumCurrFeatures; ++i) {
105  for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
106  float Sum = 0;
107  for (int j = 0; j < NI.GetInDeg(); ++j) {
108  int NbrNId = NI.GetInNId(j);
109  Sum += CurrFeatures.GetDat(NbrNId)[i]();
110  }
111  NewFeatures.GetDat(NI.GetId()).Add(Sum);
112  NewFeatures.GetDat(NI.GetId()).Add(0 == NI.GetInDeg()?
113  0 : (float(Sum) / NI.GetInDeg()));
114  }
115  }
116  return NewFeatures;
117 }
int GetNumFeatures(const TIntFtrH &Features)
Gets number of features from the node-feature mapping.
Definition: rolx.cpp:35
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:64
TIntFtrH CreateEmptyFeatures(const PUNGraph Graph)
Creates an empty node-feature mapping of all nodes in the given graph.
Definition: rolx.cpp:19
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
Definition: hash.h:88
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: graph.h:211
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: graph.h:209
int GetMtxIdx ( const TInt  NodeId,
const TIntIntH NodeIdMtxIdxH 
)

Gets matrix index of the node ID.

Definition at line 383 of file rolx.cpp.

383  {
384  return NodeIdMtxIdxH.GetDat(NodeId)();
385 }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TVec<TInt> GetNIdSorted ( const TIntFtrH Features,
const int  Idx 
)

Sorts the Idx-th feature, return the list of corresponding node ID.

Definition at line 199 of file rolx.cpp.

199  {
201  for (TIntFtrH::TIter HI = Features.BegI(); HI < Features.EndI(); HI++) {
202  F.AddDat(HI.GetKey(), HI.GetDat()[Idx]);
203  }
204  F.SortByDat();
205  TVec<TInt> SortedNId;
206  for (THash<TInt, TFlt>::TIter HI = F.BegI(); HI < F.EndI(); HI++) {
207  SortedNId.Add(HI.GetKey());
208  }
209  return SortedNId;
210 }
TIter BegI() const
Definition: hash.h:171
TIter EndI() const
Definition: hash.h:176
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void SortByDat(const bool &Asc=true)
Definition: hash.h:250
int GetNodeId ( const TInt  MtxId,
const TIntIntH NodeIdMtxIdxH 
)

Definition at line 387 of file rolx.cpp.

387  {
388  for (TIntIntH::TIter HI = NodeIdMtxIdxH.BegI();
389  HI < NodeIdMtxIdxH.EndI();
390  HI++) {
391  if (HI.GetDat() == MtxId) {
392  return HI.GetKey()();
393  }
394  }
395  return -1;
396 }
TIter BegI() const
Definition: hash.h:171
TIter EndI() const
Definition: hash.h:176
TFtr GetNthFeature ( const TIntFtrH Features,
const int  N 
)

Gets the n-th feature of all nodes.

Definition at line 39 of file rolx.cpp.

39  {
40  TFtr NthFeature;
41  IAssert(0 <= N && N < GetNumFeatures(Features));
42  for (TIntFtrH::TIter HI = Features.BegI(); HI < Features.EndI(); HI++) {
43  NthFeature.Add(HI.GetDat()[N]);
44  }
45  return NthFeature;
46 }
#define IAssert(Cond)
Definition: bd.h:262
int GetNumFeatures(const TIntFtrH &Features)
Gets number of features from the node-feature mapping.
Definition: rolx.cpp:35
TIter BegI() const
Definition: hash.h:171
TIter EndI() const
Definition: hash.h:176
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
int GetNumFeatures ( const TIntFtrH Features)

Gets number of features from the node-feature mapping.

Definition at line 35 of file rolx.cpp.

35  {
36  return Features.BegI().GetDat().Len();
37 }
TIter BegI() const
Definition: hash.h:171
const TDat & GetDat() const
Definition: hash.h:72
bool IsSimilarFeature ( const TFtr F1,
const TFtr F2,
const int  SimilarityThreshold 
)

Whether the two features are similar, given similarity threshold.

Definition at line 228 of file rolx.cpp.

229  {
230  IAssert(F1.Len() == F2.Len());
231  for (int i = 0; i < F1.Len(); ++i) {
232  if (TFlt::Abs(F1[i] - F2[i]) > SimilarityThreshold) {
233  return false;
234  }
235  }
236  return true;
237 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
static double Abs(const double &Flt)
Definition: dt.h:1337
void PlotRoles ( const PUNGraph  Graph,
const TIntIntH Roles 
)

Plots found roles on a picture (.png).

Definition at line 415 of file rolx.cpp.

415  {
416  TStr RoleToColor[10] = { "white", "black", "red", "green", "blue",
417  "yellow", "gold", "cyan", "magenta", "brown" };
418  TIntStrH Color;
419  for (TIntIntH::TIter HI = Roles.BegI(); HI < Roles.EndI(); HI++) {
420  Color.AddDat(HI.GetKey(), RoleToColor[HI.GetDat()].CStr());
421  }
422  TSnap::DrawGViz(Graph, gvlDot, "gviz_plot.png", "Dot", 1, Color);
423 }
void DrawGViz(const PGraph &Graph, const TGVizLayout &Layout, const TStr &PltFNm, const TStr &Desc=TStr(), const bool &NodeLabels=false, const TIntStrH &NIdColorH=TIntStrH())
Draws a given Graph using a selected GraphViz Layout engine with nodes colored.
Definition: gviz.h:34
TIter BegI() const
Definition: hash.h:171
TIter EndI() const
Definition: hash.h:176
Definition: gviz.h:3
Definition: dt.h:412
char * CStr()
Definition: dt.h:476
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void PrintFeatures ( const TIntFtrH Features)

Prints all nodes' feature.

Definition at line 5 of file rolx.cpp.

5  {
6  for (TIntFtrH::TIter HI = Features.BegI(); HI < Features.EndI(); HI++) {
7  printf("%d: [", HI.GetKey()());
8  const TFtr& Feature = HI.GetDat();
9  for (int i = 0; i < Feature.Len(); ++i) {
10  if (i > 0) {
11  printf(",");
12  }
13  printf("%f", Feature[i]());
14  }
15  printf("]\n");
16  }
17 }
TIter BegI() const
Definition: hash.h:171
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
TIter EndI() const
Definition: hash.h:176
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
Definition: ds.h:807
void PrintMatrix ( const TFltVV Matrix)

Prints feature matrix to stdout.

Definition at line 253 of file rolx.cpp.

253  {
254  int XDim = Matrix.GetXDim();
255  int YDim = Matrix.GetYDim();
256  printf("[");
257  for (int i = 0; i < XDim; ++i) {
258  printf("[");
259  for (int j = 0; j < YDim; ++j) {
260  if (j != 0) {
261  printf(" ");
262  }
263  printf("%f", Matrix(i, j)());
264  }
265  printf("]\n");
266  }
267  printf("]\n");
268 }
int GetXDim() const
Definition: ds.h:2184
int GetYDim() const
Definition: ds.h:2185
void PrintRoles ( const TIntIntH Roles)

Prints found roles on stdout.

Definition at line 425 of file rolx.cpp.

425  {
426  printf("--roles (node ID: role ID)--\n");
427  printf("{\n");
428  for (TIntIntH::TIter HI = Roles.BegI(); HI < Roles.EndI(); HI++) {
429  printf("(%d: %d)\n", HI.GetKey()(), HI.GetDat()());
430  }
431  printf("}\n");
432 }
TIter BegI() const
Definition: hash.h:171
TIter EndI() const
Definition: hash.h:176
TIntFtrH PruneRecursiveFeatures ( const PUNGraph  Graph,
const TIntFtrH Features,
const TIntFtrH NewFeatures,
const int  SimilarityThreshold 
)

Prunes recursive features.

Definition at line 119 of file rolx.cpp.

120  {
121  TIntFtrH AllFeatures = CreateEmptyFeatures(Features);
122  AppendFeatures(AllFeatures, Features);
123  AppendFeatures(AllFeatures, NewFeatures);
124  const float BinFraction = 0.5;
125  TIntFtrH LogBinFeatures = CalcVerticalLogBinning(AllFeatures, BinFraction);
126  PUNGraph FeatureGraph = BuildFeatureGraph(LogBinFeatures,
127  SimilarityThreshold);
128  return SummarizeConnectedComponents(FeatureGraph, Features, NewFeatures);
129 }
TIntFtrH SummarizeConnectedComponents(const PUNGraph FeatureGraph, const TIntFtrH &Features, const TIntFtrH &NewFeatures)
Summarizes s-friend graph and return retained features.
Definition: rolx.cpp:178
TIntFtrH CreateEmptyFeatures(const PUNGraph Graph)
Creates an empty node-feature mapping of all nodes in the given graph.
Definition: rolx.cpp:19
Definition: hash.h:88
void AppendFeatures(TIntFtrH &DstFeatures, const TIntFtrH &SrcFeatures, const int ColIdx)
Appends all src features to dst features.
Definition: rolx.cpp:131
PUNGraph BuildFeatureGraph(const TIntFtrH &LogBinFeatures, const int SimilarityThreshold)
Builds s-friend graph given similarity threshold.
Definition: rolx.cpp:158
TIntFtrH CalcVerticalLogBinning(const TIntFtrH &Features, const float BinFraction)
Calculates vertical logarithmic binning features from the given features.
Definition: rolx.cpp:147
TIntFtrH SummarizeConnectedComponents ( const PUNGraph  FeatureGraph,
const TIntFtrH Features,
const TIntFtrH NewFeatures 
)

Summarizes s-friend graph and return retained features.

Definition at line 178 of file rolx.cpp.

179  {
180  TCnComV Wcc;
181  TSnap::GetWccs(FeatureGraph, Wcc);
182  TVec<TInt> RetainedIdx;
183  for (int i = 0; i < Wcc.Len(); ++i) {
184  RetainedIdx.Add(Wcc[i][0]);
185  }
186  RetainedIdx.Sort();
187 
188  TIntFtrH RetainedFeatures = CreateEmptyFeatures(Features);
189  const int StartIdxNewFeatures = GetNumFeatures(Features);
190  for (int i = 0; i < RetainedIdx.Len(); ++i) {
191  const int IdxNewFeatures = RetainedIdx[i] - StartIdxNewFeatures;
192  if (IdxNewFeatures >= 0) {
193  AppendFeatures(RetainedFeatures, NewFeatures, IdxNewFeatures);
194  }
195  }
196  return RetainedFeatures;
197 }
int GetNumFeatures(const TIntFtrH &Features)
Gets number of features from the node-feature mapping.
Definition: rolx.cpp:35
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
TIntFtrH CreateEmptyFeatures(const PUNGraph Graph)
Creates an empty node-feature mapping of all nodes in the given graph.
Definition: rolx.cpp:19
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
Definition: hash.h:88
void AppendFeatures(TIntFtrH &DstFeatures, const TIntFtrH &SrcFeatures, const int ColIdx)
Appends all src features to dst features.
Definition: rolx.cpp:131
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
void GetWccs(const PGraph &Graph, TCnComV &CnComV)
Returns all weakly connected components in a Graph.
Definition: cncom.h:376