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
gstat.cpp
Go to the documentation of this file.
1 // Sigle Snapshot Graph Statistics
3 int TGStat::NDiamRuns = 10;
4 int TGStat::TakeSngVals = 100;
6 
7 bool TGStat::TCmpByVal::operator () (const TGStat& GS1, const TGStat& GS2) const {
8  IAssertR(GS1.HasVal(ValCmp) && GS2.HasVal(ValCmp), TStr::Fmt("CmpVal: %d (%s)",
9  int(ValCmp), TGStat::GetValStr(ValCmp).CStr()).CStr());
10  bool Res;
11  if (ValCmp == gsvTime) { Res = GS1.Time < GS2.Time; }
12  else { Res = GS1.GetVal(ValCmp) < GS2.GetVal(ValCmp); }
13  if (SortAsc) { return Res; }
14  else { return ! Res; }
15 }
16 
17 bool TGStat::TCmpByVal::operator () (const PGStat& GS1, const PGStat& GS2) const {
18  return operator()(*GS1, *GS2);
19 }
20 
21 TGStat::TGStat(const TSecTm& GraphTm, const TStr& GraphName) :
22  Time(GraphTm), GraphNm(GraphName), ValStatH(), DistrStatH() {
23 }
24 
25 TGStat::TGStat(const PNGraph& Graph, const TSecTm& GraphTm, TFSet StatFSet, const TStr& GraphName) {
26  TakeStat(Graph, GraphTm, StatFSet, GraphName);
27 }
28 
29 TGStat::TGStat(const PUNGraph& Graph, const TSecTm& GraphTm, TFSet StatFSet, const TStr& GraphName) {
30  TakeStat(Graph, GraphTm, StatFSet, GraphName);
31 }
32 
33 TGStat::TGStat(const PNEGraph& Graph, const TSecTm& GraphTm, TFSet StatFSet, const TStr& GraphName) {
34  TakeStat(Graph, GraphTm, StatFSet, GraphName);
35 }
36 TGStat::TGStat(const TGStat& GStat) : Time(GStat.Time), GraphNm(GStat.GraphNm),
37  ValStatH(GStat.ValStatH), DistrStatH(GStat.DistrStatH) {
38 }
39 
40 TGStat::TGStat(TSIn& SIn) : Time(SIn), GraphNm(SIn), ValStatH(SIn), DistrStatH(SIn) { }
41 
42 void TGStat::Save(TSOut& SOut) const {
43  Time.Save(SOut); GraphNm.Save(SOut);
44  ValStatH.Save(SOut); DistrStatH.Save(SOut);
45 }
46 
48  if (this != &GStat) {
49  Time = GStat.Time;
50  GraphNm = GStat.GraphNm;
51  ValStatH = GStat.ValStatH;
52  DistrStatH = GStat.DistrStatH;
53  }
54  return *this;
55 }
56 
57 bool TGStat::operator == (const TGStat& GStat) const {
58  return Time==GStat.Time && ValStatH==GStat.ValStatH && DistrStatH==GStat.DistrStatH;
59 }
60 
61 bool TGStat::operator < (const TGStat& GStat) const {
62  if (Time<GStat.Time) { return true; }
63  if (Time>GStat.Time) { return false; }
64  if (ValStatH.Empty() && ! GStat.ValStatH.Empty()) { return true; }
65  if (GStat.ValStatH.Empty()) { return false; }
66  for (int v = gsvTime; v < gsvMx; v++) {
67  if (! ValStatH.IsKey(v) && ! GStat.ValStatH.IsKey(v)) { continue; }
68  if (ValStatH.IsKey(v) && ! GStat.ValStatH.IsKey(v)) { return false; }
69  if (! ValStatH.IsKey(v)) { return true; }
70  if (ValStatH.GetDat(v) < GStat.ValStatH.GetDat(v)) { return true; }
71  }
72  return false;
73 }
74 
75 bool TGStat::HasVal(const TGStatVal& StatVal) const {
76  if (StatVal == gsvIndex) { return true; }
77  if (StatVal == gsvTime) { return Time.IsDef(); }
78  return ValStatH.IsKey(int(StatVal));
79 }
80 
81 double TGStat::GetVal(const TGStatVal& StatVal) const {
82  if (StatVal == gsvIndex) { return -1; }
83  if (StatVal == gsvTime) { return Time.GetAbsSecs(); }
84  if (! ValStatH.IsKey(int(StatVal))) { return -1.0; }
85  return ValStatH.GetDat(int(StatVal));
86 }
87 
88 void TGStat::SetVal(const TGStatVal& StatVal, const double& Val) {
89  ValStatH.AddDat(int(StatVal), Val);
90 }
91 
92 const TFltPrV& TGStat::GetDistr(const TGStatDistr& Distr) const {
93  if (! DistrStatH.IsKey(int(Distr))) { return EmptyV; }
94  return DistrStatH.GetDat(int(Distr));
95 }
96 
97 void TGStat::SetDistr(const TGStatDistr& Distr, const TFltPrV& FltPrV) {
98  DistrStatH.AddDat(Distr, FltPrV);
99 }
100 
101 void TGStat::GetDistr(const TGStatDistr& Distr, TFltPrV& FltPrV) const {
102  FltPrV = GetDistr(Distr);
103 }
104 
105 void TGStat::TakeStat(const PNGraph& Graph, const TSecTm& _Time, TFSet StatFSet, const TStr& GraphName) {
106  printf("\n===TakeStat: G(%u, %u) at %s\n", Graph->GetNodes(), Graph->GetEdges(), _Time.IsDef()?_Time.GetStr().CStr():"");
107  TExeTm ExeTm, FullTm;
108  Time = _Time;
109  GraphNm = GraphName;
110  if (StatFSet.In(gsvNone)) { return; }
111  TakeBasicStat(Graph, false);
112  TakeDiam(Graph, StatFSet, false);
113  if (StatFSet.In(gsdWcc) || StatFSet.In(gsdWccHops) || StatFSet.In(gsvFullDiam) || StatFSet.In(gsvEffWccDiam) || StatFSet.In(gsvWccNodes) || StatFSet.In(gsvWccSrcNodes) || StatFSet.In(gsvWccDstNodes) || StatFSet.In(gsvWccEdges) || StatFSet.In(gsvWccUniqEdges) || StatFSet.In(gsvWccBiDirEdges)) {
114  PNGraph WccGraph = TSnap::GetMxWcc(Graph);
115  TakeBasicStat(WccGraph, true);
116  TakeDiam(WccGraph, StatFSet, true);
117  SetVal(gsvWccSize, WccGraph->GetNodes()/double(Graph->GetNodes()));
118  }
119  // strongly connected component
120  TakeSccStat(Graph, StatFSet);
121  // strongly connected component
122  TakeBccStat(Graph, StatFSet);
123  // degrees
124  TakeDegDistr(Graph, StatFSet);
125  // components
126  TakeConnComp(Graph, StatFSet);
127  // spectral
128  TakeSpectral(Graph, StatFSet, -1);
129  // clustering coeffient
130  if (StatFSet.In(gsdClustCf) || StatFSet.In(gsvClustCf)) {
131  TakeClustCf(Graph); }
132  if (StatFSet.In(gsdTriadPart)) {
133  TakeTriadPart(Graph); }
134  printf("**[%s]\n", FullTm.GetTmStr());
135 }
136 
137 void TGStat::TakeStat(const PUNGraph& Graph, const TSecTm& _Time, TFSet StatFSet, const TStr& GraphName) {
138  printf("\n===TakeStat: UG(%u, %u) at %s\n", Graph->GetNodes(), Graph->GetEdges(), _Time.IsDef()?_Time.GetStr().CStr():"");
139  TExeTm ExeTm, FullTm;
140  Time = _Time;
141  GraphNm = GraphName;
142  if (StatFSet.In(gsvNone)) { return; }
143  TakeBasicStat(Graph, false);
144  TakeDiam(Graph, StatFSet, false);
145  if (StatFSet.In(gsdWcc) || StatFSet.In(gsdWccHops) || StatFSet.In(gsvFullDiam) || StatFSet.In(gsvEffWccDiam) || StatFSet.In(gsvWccNodes) || StatFSet.In(gsvWccSrcNodes) || StatFSet.In(gsvWccDstNodes) || StatFSet.In(gsvWccEdges) || StatFSet.In(gsvWccUniqEdges) || StatFSet.In(gsvWccBiDirEdges)) {
146  PUNGraph WccGraph = TSnap::GetMxWcc(Graph);
147  TakeBasicStat(WccGraph, true);
148  TakeDiam(WccGraph, StatFSet, true);
149  SetVal(gsvWccSize, WccGraph->GetNodes()/double(Graph->GetNodes()));
150  }
151  // strongly connected component
152  //TakeSccStat(Graph, StatFSet);
153  // strongly connected component
154  TakeBccStat(Graph, StatFSet);
155  // degrees
156  TakeDegDistr(Graph, StatFSet);
157  // components
158  TakeConnComp(Graph, StatFSet);
159  // spectral
160  //TakeSpectral(Graph, StatFSet, -1);
161  // clustering coeffient
162  if (StatFSet.In(gsdClustCf) || StatFSet.In(gsvClustCf)) {
163  TakeClustCf(Graph); }
164  if (StatFSet.In(gsdTriadPart)) {
165  TakeTriadPart(Graph); }
166  printf("**[%s]\n", FullTm.GetTmStr());
167 }
168 
169 void TGStat::TakeSpectral(const PNGraph& Graph, const int _TakeSngVals) {
170  TakeSpectral(Graph, TFSet() | gsdSngVal | gsdSngVec, _TakeSngVals);
171 }
172 
173 void TGStat::TakeSpectral(const PNGraph& Graph, TFSet StatFSet, int _TakeSngVals) {
174  TExeTm ExeTm;
175  if (_TakeSngVals == -1) { _TakeSngVals = TakeSngVals; }
176  // singular values, vectors
177  if (StatFSet.In(gsdSngVal)) {
178  printf("sing-vals...");
179  const int SngVals = TMath::Mn(_TakeSngVals, Graph->GetNodes()/2);
180  TFltV SngValV1;
181  TSnap::GetSngVals(Graph, SngVals, SngValV1);
182  SngValV1.Sort(false);
183  TFltPrV& SngValV = DistrStatH.AddDat(gsdSngVal);
184  SngValV.Gen(SngValV1.Len(), 0);
185  for (int i = 0; i < SngValV1.Len(); i++) {
186  SngValV.Add(TFltPr(i+1, SngValV1[i]));
187  }
188  printf("[%s] ", ExeTm.GetTmStr());
189  }
190  if (StatFSet.In(gsdSngVec)) {
191  printf("sing-vec...");
192  TFltV LeftV, RightV;
193  TSnap::GetSngVec(Graph, LeftV, RightV);
194  LeftV.Sort(false);
195  TFltPrV& SngVec = DistrStatH.AddDat(gsdSngVec);
196  SngVec.Gen(LeftV.Len(), 0);
197  for (int i = 0; i < TMath::Mn(Kilo(10), LeftV.Len()/2); i++) {
198  if (LeftV[i] > 0) { SngVec.Add(TFltPr(i+1, LeftV[i])); }
199  }
200  printf("[%s] ", ExeTm.GetTmStr());
201  }
202 }
203 
204 void TGStat::Plot(const TGStatDistr& Distr, const TStr& FNmPref, TStr Desc, bool PowerFit) const {
205  if (Desc.Empty()) Desc = FNmPref.GetUc();
206  if (! HasDistr(Distr) || Distr==gsdUndef || Distr==gsdMx) { return; }
207  TPlotInfo Info = GetPlotInfo(Distr);
208  TGnuPlot GnuPlot(Info.Val1+TStr(".")+FNmPref, TStr::Fmt("%s. G(%d, %d)", Desc.CStr(), GetNodes(),GetEdges()));
209  GnuPlot.SetXYLabel(Info.Val2, Info.Val3);
210  GnuPlot.SetScale(Info.Val4);
211  const int plotId = GnuPlot.AddPlot(GetDistr(Distr), gpwLinesPoints, "");
212  if (PowerFit) { GnuPlot.AddPwrFit(plotId, gpwLines); }
213  #ifdef GLib_MACOSX
214  GnuPlot.SaveEps();
215  #else
216  GnuPlot.SavePng();
217  #endif
218 }
219 
220 void TGStat::Plot(const TFSet& FSet, const TStr& FNmPref, const TStr& Desc, bool PowerFit) const {
221  for (int d = gsdUndef; d < gsdMx; d++) {
222  const TGStatDistr Distr = TGStatDistr(d);
223  if (! FSet.In(Distr)) { continue; }
224  Plot(Distr, FNmPref, Desc, PowerFit);
225  }
226 }
227 
228 void TGStat::PlotAll(const TStr& FNmPref, TStr Desc, bool PowerFit) const {
229  for (int d = gsdUndef; d < gsdMx; d++) {
230  const TGStatDistr Distr = TGStatDistr(d);
231  Plot(Distr, FNmPref, Desc, PowerFit);
232  }
233 }
234 
236  for (int val = gsvNone; val < gsvMx; val++) {
237  const TGStatVal Val = TGStatVal(val);
238  if (! HasVal(Val)) { continue; }
239  printf(" %s\t%g\n", GetValStr(Val).CStr(), GetVal(Val));
240  }
241 }
242 
243 void TGStat::AvgGStat(const PGStatVec& GStatVec, const bool& ClipAt1) {
244  AvgGStat(GStatVec->GetGStatV(), ClipAt1);
245 }
246 
247 void TGStat::AvgGStat(const TGStatV& GStatV, const bool& ClipAt1) {
248  if (GStatV.Empty()) return;
249  Time = GStatV[0]->Time;
250  GraphNm = GStatV[0]->GraphNm;
251  // values
252  for (int statVal = 0; statVal > gsvMx; statVal++) {
253  const TGStatVal GStatVal = TGStatVal(statVal);
254  TMom Mom;
255  for (int i = 0; i < GStatV.Len(); i++) {
256  if (GStatV[i]->HasVal(GStatVal)) {
257  Mom.Add(GStatV[i]->GetVal(GStatVal)); }
258  }
259  Mom.Def();
260  if (Mom.IsUsable()) {
261  IAssert(Mom.GetVals() == GStatV.Len()); // all must have the value
262  SetVal(GStatVal, Mom.GetMean());
263  }
264  }
265  // distributions
266  for (int distr = gsdUndef; distr < gsdMx; distr++) {
267  const TGStatDistr GStatDistr = TGStatDistr(distr);
268  THash<TFlt, TFlt> ValToSumH;
269  int DistrCnt = 0;
270  for (int i = 0; i < GStatV.Len(); i++) {
271  if (GStatV[i]->HasDistr(GStatDistr)) {
272  const TFltPrV& D = GStatV[i]->GetDistr(GStatDistr);
273  for (int d = 0; d < D.Len(); d++) {
274  ValToSumH.AddDat(D[d].Val1) += D[d].Val2; }
275  DistrCnt++;
276  }
277  }
278  IAssert(DistrCnt==0 || DistrCnt==GStatV.Len()); // all must have distribution
279  TFltPrV AvgStatV;
280  ValToSumH.GetKeyDatPrV(AvgStatV); AvgStatV.Sort();
281  for (int i = 0; i < AvgStatV.Len(); i++) {
282  AvgStatV[i].Val2 /= double(DistrCnt);
283  if (ClipAt1 && AvgStatV[i].Val2 < 1) { AvgStatV[i].Val2 = 1; }
284  }
285  SetDistr(GStatDistr, AvgStatV);
286  }
287 }
288 
290  switch (Distr) {
291  case gsdUndef : return TStr("Undef");
292  case gsdInDeg : return "InDeg";
293  case gsdOutDeg : return "OutDeg";
294  case gsdWcc : return "WccDist";
295  case gsdScc : return "SccDist";
296  case gsdHops : return "Hops";
297  case gsdWccHops : return "WccHops";
298  case gsdSngVal : return "SngVal";
299  case gsdSngVec : return "SngVec";
300  case gsdClustCf : return "ClustCf";
301  case gsdTriadPart : return "TriadPart";
302  case gsdMx: return TStr("Mx");
303  default: Fail; return TStr();
304  };
305 }
306 
308  static TIntStrH ValTyStrH;
309  if (ValTyStrH.Empty()) {
310  ValTyStrH.AddDat(gsvNone, "None");
311  ValTyStrH.AddDat(gsvIndex, "Index");
312  ValTyStrH.AddDat(gsvTime, "Time");
313  ValTyStrH.AddDat(gsvNodes, "Nodes");
314  ValTyStrH.AddDat(gsvZeroNodes, "ZeroNodes");
315  ValTyStrH.AddDat(gsvNonZNodes, "NonZNodes");
316  ValTyStrH.AddDat(gsvSrcNodes, "SrcNodes");
317  ValTyStrH.AddDat(gsvDstNodes, "DstNodes");
318  ValTyStrH.AddDat(gsvEdges, "Edges");
319  ValTyStrH.AddDat(gsvUniqEdges, "UniqEdges");
320  ValTyStrH.AddDat(gsvBiDirEdges, "BiDirEdges");
321  ValTyStrH.AddDat(gsvWccNodes, "WccNodes");
322  ValTyStrH.AddDat(gsvWccSrcNodes, "WccSrcNodes");
323  ValTyStrH.AddDat(gsvWccDstNodes, "WccDstNodes");
324  ValTyStrH.AddDat(gsvWccEdges, "WccEdges");
325  ValTyStrH.AddDat(gsvWccUniqEdges, "WccUniqEdges");
326  ValTyStrH.AddDat(gsvWccBiDirEdges, "WccBiDirEdges");
327  ValTyStrH.AddDat(gsvSccNodes, "SccNodes");
328  ValTyStrH.AddDat(gsvSccEdges, "SccEdges");
329  ValTyStrH.AddDat(gsvBccNodes, "BccNodes");
330  ValTyStrH.AddDat(gsvBccEdges, "BccEdges");
331  ValTyStrH.AddDat(gsvFullDiam, "FullDiam");
332  ValTyStrH.AddDat(gsvEffDiam, "EffDiam");
333  ValTyStrH.AddDat(gsvEffWccDiam, "EffWccDiam");
334  ValTyStrH.AddDat(gsvFullWccDiam, "FullWccDiam");
335  ValTyStrH.AddDat(gsvFullDiamDev, "FullDiamDev");
336  ValTyStrH.AddDat(gsvEffDiamDev, "EffDiamDev");
337  ValTyStrH.AddDat(gsvEffWccDiamDev, "EffWccDiamDev");
338  ValTyStrH.AddDat(gsvFullWccDiamDev, "FullWccDiamDev");
339  ValTyStrH.AddDat(gsvClustCf, "ClustCf");
340  ValTyStrH.AddDat(gsvOpenTriads, "OpenTr");
341  ValTyStrH.AddDat(gsvClosedTriads, "ClosedTr");
342  ValTyStrH.AddDat(gsvWccSize, "WccSize");
343  ValTyStrH.AddDat(gsvSccSize, "SccSize");
344  ValTyStrH.AddDat(gsvBccSize, "BccSize");
345  ValTyStrH.AddDat(gsvMx, "Mx");
346  }
347  IAssert(ValTyStrH.IsKey(int(Val)));
348  return ValTyStrH.GetDat(int(Val));
349 }
350 
352  //switch (Distr) {
353  //case gsdUndef : Fail; return TPlotInfo();
354  Fail;
355  return TPlotInfo();
356 }
357 
359  switch (Distr) {
360  case gsdUndef : Fail; return TPlotInfo();
361  case gsdInDeg : return TPlotInfo("inDeg", "In-degree, k", "Count", gpsLog10XY);
362  case gsdOutDeg : return TPlotInfo("outDeg", "Out-degree, k", "Count", gpsLog10XY);
363  case gsdWcc : return TPlotInfo("wcc", "WCC size", "Count", gpsLog10XY);
364  case gsdScc : return TPlotInfo("scc", "SCC size", "Count", gpsLog10XY);
365  case gsdHops : return TPlotInfo("hop", "Number of hops, h", "Reachable pairs of nodes inside h hops", gpsLog10Y);
366  case gsdWccHops : return TPlotInfo("wccHop", "Number of hops, h", "Reachable pairs of nodes inside h hops in WCC", gpsLog10Y);
367  case gsdSngVal : return TPlotInfo("sval", "Rank", "Singular value", gpsLog10XY);
368  case gsdSngVec : return TPlotInfo("svec", "Rank", "Left singular vector", gpsLog10XY);
369  case gsdClustCf : return TPlotInfo("ccf", "Degree, k", "Clustering coefficient, <C(k)>", gpsLog10XY);
370  case gsdTriadPart : return TPlotInfo("triad", "Number of triads adjacent to a node", "Number of such nodes", gpsLog10XY);
371  case gsdMx : Fail;
372  default: Fail; return TPlotInfo();
373  };
374 }
375 
377  return TFSet() | gsvNone;
378 }
379 
381  return TFSet();
382 }
383 
385  return TFSet() | gsdInDeg | gsdOutDeg;
386 }
387 
389  return TFSet() | gsdInDeg | gsdOutDeg | gsdWcc | gsdScc;
390 }
391 
393  return TFSet() | gsdHops | gsdWccHops;
394 }
395 
397  return TFSet() | gsdInDeg | gsdOutDeg | gsdWcc | gsdScc |
399 }
400 
402  return TFSet() | gsdInDeg | gsdOutDeg | gsdWcc | gsdScc
405 }
406 
408 // Graph Growth Statistics
410 
411 TGStatVec::TGStatVec(const TTmUnit& _TmUnit) : TmUnit(_TmUnit), StatFSet(), GStatV() {
413 }
414 
415 TGStatVec::TGStatVec(const TTmUnit& _TmUnit, const TFSet& TakeGrowthStat) :
416  TmUnit(_TmUnit), StatFSet(TakeGrowthStat), GStatV() {
417 }
418 
420  TmUnit(GStat.TmUnit), StatFSet(GStat.StatFSet), GStatV(GStat.GStatV) {
421 }
422 
423 TGStatVec::TGStatVec(TSIn& SIn) : TmUnit((TTmUnit) TInt(SIn).Val), StatFSet(SIn), GStatV(SIn) {
424 }
425 
427  return new TGStatVec(_TmUnit);
428 }
429 
430 PGStatVec TGStatVec::New(const TTmUnit& _TmUnit, const TFSet& TakeGrowthStat) {
431  return new TGStatVec(_TmUnit, TakeGrowthStat);
432 }
433 
434 void TGStatVec::Save(TSOut& SOut) const {
435  TInt(TmUnit).Save(SOut);
436  StatFSet.Save(SOut);
437  GStatV.Save(SOut);
438 }
439 
441  if (this != &GStat) {
442  TmUnit = GStat.TmUnit;
443  StatFSet = GStat.StatFSet;
444  GStatV = GStat.GStatV;
445  }
446  return *this;
447 }
448 
451  return GStatV.Last();
452 }
453 
454 PGStat TGStatVec::Add(const TSecTm& Time, const TStr& GraphNm) {
455  GStatV.Add(TGStat::New(Time, GraphNm));
456  return GStatV.Last();
457 }
458 
459 void TGStatVec::Add(const PNGraph& Graph, const TSecTm& Time, const TStr& GraphNm) {
460  if (Graph->GetNodes() < (int) TGStatVec::MinNodesEdges) {
461  printf(" ** TGStatVec::Add: graph too small (%d nodes).SKIP\n", Graph->GetNodes());
462  return;
463  }
464  Add(TGStat::New(Graph, Time, StatFSet, GraphNm));
465 }
466 
467 void TGStatVec::Add(const PUNGraph& Graph, const TSecTm& Time, const TStr& GraphNm) {
468  if (Graph->GetNodes() < (int) TGStatVec::MinNodesEdges) {
469  printf(" ** TGStatVec::Add: graph too small (%d nodes).SKIP\n", Graph->GetNodes());
470  return;
471  }
472  Add(TGStat::New(Graph, Time, StatFSet, GraphNm));
473 }
474 
475 void TGStatVec::Add(const PNEGraph& Graph, const TSecTm& Time, const TStr& GraphNm) {
476  if (Graph->GetNodes() < (int) TGStatVec::MinNodesEdges) {
477  printf(" ** TGStatVec::Add: graph too small (%d nodes).SKIP\n", Graph->GetNodes());
478  return;
479  }
480  Add(TGStat::New(Graph, Time, StatFSet, GraphNm));
481 }
482 
483 void TGStatVec::Sort(const TGStatVal& SortBy, const bool& Asc) {
484  GStatV.SortCmp(TGStat::TCmpByVal(SortBy, Asc));
485 }
486 
487 void TGStatVec::DelBefore(const TSecTm& Tm) {
488  TGStatV NewTickV;
489  for (int i = 0; i < Len(); i++) {
490  if (At(i)->Time >= Tm) { NewTickV.Add(At(i)); }
491  }
492  GStatV.Swap(NewTickV);
493 }
494 
495 void TGStatVec::DelAfter(const TSecTm& Tm) {
496  TGStatV NewTickV;
497  for (int i = 0; i < Len(); i++) {
498  if (At(i)->Time <= Tm) { NewTickV.Add(At(i)); }
499  }
500  GStatV.Swap(NewTickV);
501 }
502 
503 void TGStatVec::DelSmallNodes(const int& MinNodes) {
504  TGStatV NewTickV;
505  for (int i = 0; i < Len(); i++) {
506  if (At(i)->GetNodes() >= MinNodes) { NewTickV.Add(At(i)); }
507  }
508  GStatV.Swap(NewTickV);
509 }
510 
511 void TGStatVec::GetValV(const TGStatVal& XVal, const TGStatVal& YVal, TFltPrV& ValV) const {
512  ValV.Gen(Len(), 0);
513  double x;
514  for (int t = 0; t < Len(); t++) {
515  if (XVal == gsvIndex) { x = t+1; }
516  else if (XVal == gsvTime) { x = GetTime(t); }
517  else { x = At(t)->GetVal(XVal); }
518  ValV.Add(TFltPr(x, At(t)->GetVal(YVal)));
519  }
520  ValV.Sort(true); // sort by ascending x value
521 }
522 
523 PGStat TGStatVec::GetAvgGStat(const bool& ClipAt1) {
524  PGStat Stat = TGStat::New();
525  Stat->AvgGStat(GStatV, ClipAt1);
526  return Stat;
527 }
528 
529 void TGStatVec::Plot(const TGStatVal& XVal, const TGStatVal& YVal, const TStr& OutFNm, TStr& Desc, const TGpScaleTy& Scale,const bool& PowerFit) const {
530  if (! Last()->HasVal(XVal) || ! Last()->HasVal(YVal)) {
531  if (! Last()->HasVal(XVal)) { printf("** Does not have %s statistic\n", TGStat::GetValStr(XVal).CStr()); }
532  if (! Last()->HasVal(YVal)) { printf("** Does not have %s statistic\n", TGStat::GetValStr(YVal).CStr()); }
533  return;
534  }
535  if (Desc.Empty()) { Desc = OutFNm; }
536  TFltPrV ValV;
537  TGStatVec::GetValV(XVal, YVal, ValV);
538  TGnuPlot GP(TStr::Fmt("%s-%s.%s", TGStat::GetValStr(XVal).CStr(), TGStat::GetValStr(YVal).CStr(), OutFNm.CStr()),
539  TStr::Fmt("%s. %s vs. %s. G(%d,%d)", Desc.CStr(), TGStat::GetValStr(XVal).CStr(), TGStat::GetValStr(YVal).CStr(),
540  Last()->GetNodes(), Last()->GetEdges()));
541  GP.SetScale(Scale);
543  const int Id = GP.AddPlot(ValV, gpwLinesPoints);
544  if (PowerFit) { GP.AddPwrFit(Id); }
545  GP.SavePng();
546 }
547 
548 void TGStatVec::PlotAllVsX(const TGStatVal& XVal, const TStr& OutFNm, TStr Desc, const TGpScaleTy& Scale, const bool& PowerFit) const {
550  for (int stat = gsvNone; stat < gsvMx; stat++) {
551  const TGStatVal Stat = TGStatVal(stat);
552  if (SkipStat.In(Stat)) { continue; }
553  if (Last()->HasVal(Stat) && Last()->HasVal(XVal) && Stat!=XVal) {
554  Plot(XVal, Stat, OutFNm, Desc, Scale, PowerFit);
555  }
556  }
557 }
558 
559 void TGStatVec::ImposeDistr(const TGStatDistr& Distr, const TStr& FNmPref, TStr Desc, const bool& ExpBin,
560  const bool& PowerFit, const TGpSeriesTy& PlotWith, const TStr& Style) const {
561  if (Desc.Empty()) Desc = FNmPref.GetUc();
562  if (! At(0)->HasDistr(Distr) || Distr==gsdUndef || Distr==gsdMx) { return; }
563  TGStat::TPlotInfo Info = At(0)->GetPlotInfo(Distr);
564  TGnuPlot GnuPlot(Info.Val1+TStr(".")+FNmPref, TStr::Fmt("%s. G(%d, %d) --> G(%d, %d)", Desc.CStr(),
565  At(0)->GetNodes(), At(0)->GetEdges(), Last()->GetNodes(), Last()->GetEdges()));
566  GnuPlot.SetXYLabel(Info.Val2, Info.Val3);
567  GnuPlot.SetScale(Info.Val4);
568  int plotId;
569  for (int at = 0; at < Len(); at++) {
570  TStr Legend = At(at)->GetNm();
571  if (Legend.Empty()) { Legend = At(at)->GetTmStr(); }
572  if (! ExpBin) {
573  plotId = GnuPlot.AddPlot(At(at)->GetDistr(Distr), PlotWith, Legend, Style); }
574  else {
575  TFltPrV ExpBinV;
576  TGnuPlot::MakeExpBins(At(at)->GetDistr(Distr), ExpBinV, 2, 0);
577  plotId = GnuPlot.AddPlot(ExpBinV, PlotWith, Legend, Style);
578  }
579  if (PowerFit) { GnuPlot.AddPwrFit(plotId, gpwLines); }
580  }
581  GnuPlot.SavePng();
582 }
583 
584 void TGStatVec::SaveTxt(const TStr& FNmPref, const TStr& Desc) const {
585  FILE *F = fopen(TStr::Fmt("growth.%s.tab", FNmPref.CStr()).CStr(), "wt");
586  fprintf(F, "# %s\n", Desc.CStr());
587  fprintf(F, "# %s", TTmInfo::GetTmUnitStr(TmUnit).CStr());
588  TIntSet StatValSet;
589  for (int i = 0; i < Len(); i++) {
590  for (int v = gsvNone; v < gsvMx; v++) {
591  if (At(i)->HasVal(TGStatVal(v))) { StatValSet.AddKey(v); }
592  }
593  }
594  TIntV StatValV; StatValSet.GetKeyV(StatValV); StatValV.Sort();
595  for (int sv = 0; sv < StatValV.Len(); sv++) {
596  fprintf(F, "\t%s", TGStat::GetValStr(TGStatVal(StatValV[sv].Val)).CStr()); }
597  fprintf(F, "Time\n");
598  for (int i = 0; i < Len(); i++) {
599  const TGStat& G = *At(i);
600  for (int sv = 0; sv < StatValV.Len(); sv++) {
601  fprintf(F, "%g\t", G.GetVal(TGStatVal(StatValV[sv].Val))); }
602  fprintf(F, "%s\n", G.GetTmStr().CStr());
603  }
604  fclose(F);
605 }
#define IAssert(Cond)
Definition: bd.h:262
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
bool HasVal(const TGStatVal &Stat) const
Definition: gstat.h:200
PGStat GetAvgGStat(const bool &ClipAt1=false)
Definition: gstat.cpp:523
static const TFltPrV EmptyV
Definition: gstat.h:52
PGStat Add()
Definition: gstat.cpp:449
#define IAssertR(Cond, Reason)
Definition: bd.h:265
static TStr GetValStr(const TGStatVal &Val)
Definition: gstat.cpp:307
Definition: tm.h:355
void ImposeDistr(const TGStatDistr &Distr, const TStr &FNmPref, TStr Desc=TStr(), const bool &ExpBin=false, const bool &PowerFit=false, const TGpSeriesTy &PlotWith=gpwLinesPoints, const TStr &Style="") const
Definition: gstat.cpp:559
#define Kilo(n)
Definition: gbase.h:3
TStr GraphNm
Definition: gstat.h:56
static void MakeExpBins(const TFltPrV &XYValV, TFltPrV &ExpXYValV, const double &BinFactor=2, const double &MinYVal=1)
Definition: gnuplot.cpp:614
bool IsDef() const
Definition: tm.h:123
void SavePng(const int &SizeX=1000, const int &SizeY=800, const TStr &Comment=TStr())
Definition: gnuplot.h:120
PGraph GetMxWcc(const PGraph &Graph)
Returns a graph representing the largest weakly connected component on an input Graph.
Definition: cncom.h:452
void Save(TSOut &SOut) const
Definition: dt.h:1060
int GetVals() const
Definition: xmath.h:220
unsigned int uint
Definition: bd.h:11
static TFSet AllStat()
Definition: gstat.cpp:401
void TakeSpectral(const PNGraph &Graph, const int _TakeSngVals=-1)
Definition: gstat.cpp:169
void Save(TSOut &SOut) const
Definition: gstat.cpp:434
#define Fail
Definition: bd.h:238
bool operator==(const TGStat &GStat) const
Definition: gstat.cpp:57
TStr GetUc() const
Definition: dt.h:493
Definition: gstat.h:28
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
static TFSet NoDiamStat()
Definition: gstat.cpp:388
enum TGStatDistr_ TGStatDistr
TVal2 Val2
Definition: ds.h:221
Definition: bits.h:119
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:82
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void TakeConnComp(const PGraph &Graph)
Definition: gstat.h:392
void Save(TSOut &SOut) const
Definition: hash.h:141
bool Empty() const
Definition: hash.h:185
void PlotAllVsX(const TGStatVal &XVal, const TStr &OutFNm, TStr Desc=TStr(), const TGpScaleTy &Scale=gpsAuto, const bool &PowerFit=false) const
Definition: gstat.cpp:548
static TFSet NoDistrStat()
Definition: gstat.cpp:392
void GetKeyV(TVec< TKey > &KeyV) const
Definition: shash.h:1347
TTmUnit TmUnit
Definition: gstat.h:160
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:438
void AvgGStat(const PGStatVec &GStatVec, const bool &ClipAt1=false)
Definition: gstat.cpp:243
TVal3 Val3
Definition: ds.h:222
void SetXYLabel(const TStr &XLabel, const TStr &YLabel)
Definition: gnuplot.h:73
TGStatVec & operator=(const TGStatVec &GStat)
Definition: gstat.cpp:440
TVec< TFltPr > TFltPrV
Definition: ds.h:1539
TSecTm Time
Definition: gstat.h:55
static TStr GetDistrStr(const TGStatDistr &Distr)
Definition: gstat.cpp:289
Definition: xmath.h:129
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
bool HasDistr(const TGStatDistr &Distr) const
Definition: gstat.h:102
TGStatV GStatV
Definition: gstat.h:162
void Sort(const TGStatVal &SortBy=gsvNodes, const bool &Asc=true)
Definition: gstat.cpp:483
bool HasDistr(const TGStatDistr &Stat) const
Definition: gstat.h:201
TVal1 Val1
Definition: ds.h:220
int GetNodes() const
Definition: gstat.h:107
int AddPwrFit(const int &PlotId, const TGpSeriesTy &SeriesTy=gpwLines, const TStr &Style=TStr())
Definition: gnuplot.cpp:370
void DumpValStat()
Definition: gstat.cpp:235
int Len() const
Definition: gstat.h:183
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:168
bool IsUsable() const
Definition: xmath.h:225
Definition: fl.h:58
void Save(TSOut &SOut) const
Definition: ds.h:903
void Save(TSOut &SOut) const
Definition: tm.h:103
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
Definition: gstat.h:29
enum TGStatVal_ TGStatVal
void SaveTxt(const TStr &FNmPref, const TStr &Desc) const
Definition: gstat.cpp:584
Definition: gstat.h:28
bool operator()(const TGStat &GS1, const TGStat &GS2) const
Definition: gstat.cpp:7
Graph Statistics Sequence.
Definition: gstat.h:155
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
Definition: ds.h:1047
int GetTime(const int &ValN) const
Definition: gstat.h:189
const char * GetTmStr() const
Definition: tm.h:370
static TPlotInfo GetPlotInfo(const TGStatVal &Val)
Definition: gstat.cpp:351
Definition: gstat.h:28
void GetSngVec(const PNGraph &Graph, TFltV &LeftSV, TFltV &RightSV)
Computes the leading left and right singular vector of the adjacency matrix representing a directed G...
Definition: gsvd.cpp:225
void Save(TSOut &SOut) const
Definition: bits.h:135
void TakeStat(const PNGraph &Graph, const TSecTm &Time, TFSet StatFSet, const TStr &GraphName)
Definition: gstat.cpp:105
Definition: gstat.h:16
Definition: gstat.h:16
void TakeTriadPart(const PGraph &Graph)
Definition: gstat.h:460
void Add(const TFlt &Val, const TFlt &Wgt=1)
Definition: xmath.h:217
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1254
THash< TInt, TFltPrV > DistrStatH
Definition: gstat.h:58
Statistics of a Graph Snapshot.
Definition: gstat.h:36
void DelBefore(const TSecTm &Tm)
Definition: gstat.cpp:487
void Save(TSOut &SOut, const bool &IsSmall=false) const
Definition: dt.h:440
void TakeClustCf(const PGraph &Graph, const int &SampleNodes=-1)
Definition: gstat.h:447
Definition: gstat.h:17
static PGStat New(const TSecTm &Time=TSecTm(), const TStr &GraphName=TStr())
Definition: gstat.h:69
TIntFltH ValStatH
Definition: gstat.h:57
TFSet StatFSet
Definition: gstat.h:161
uint GetAbsSecs() const
Definition: tm.h:150
Definition: gstat.h:16
void DelSmallNodes(const int &MinNodes)
Definition: gstat.cpp:503
bool HasVal(const TGStatVal &StatVal) const
Definition: gstat.cpp:75
void TakeBccStat(const PGraph &Graph, TFSet StatFSet)
Definition: gstat.h:434
TVal4 Val4
Definition: ds.h:223
int AddKey(const TKey &Key)
Definition: shash.h:1254
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:551
int GetEdges() const
Definition: gstat.h:108
TGStatVec(const TTmUnit &_TmUnit=tmu1Sec)
Definition: gstat.cpp:411
bool SortAsc
Definition: gstat.h:45
static uint MinNodesEdges
Definition: gstat.h:157
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
static TFSet NoSvdStat()
Definition: gstat.cpp:396
void SaveEps(const int &FontSz=30, const TStr &Comment=TStr())
Definition: gnuplot.h:123
void SetVal(const TGStatVal &StatVal, const double &Val)
Definition: gstat.cpp:88
Definition: fl.h:128
void Save(TSOut &SOut) const
Definition: gstat.cpp:42
static int NDiamRuns
Definition: gstat.h:38
Definition: dt.h:1044
void GetSngVals(const PNGraph &Graph, const int &SngVals, TFltV &SngValV)
Computes largest SngVals singular values of the adjacency matrix representing a directed Graph...
Definition: gsvd.cpp:175
static TFSet NoStat()
Definition: gstat.cpp:376
TStr GetTmStr() const
Definition: gstat.h:91
Definition: tm.h:81
PGStat At(const int &ValN) const
Definition: gstat.h:186
Definition: gstat.h:28
void PlotAll(const TStr &FNmPref, TStr Desc=TStr(), bool PowerFit=false) const
Definition: gstat.cpp:228
PGStat Last() const
Definition: gstat.h:187
static TStr GetTmUnitStr(const TTmUnit &TmUnit)
Definition: tm.cpp:108
void SetScale(const TGpScaleTy &GpScaleTy)
Definition: gnuplot.h:78
double GetMean() const
Definition: xmath.h:240
Definition: dt.h:412
Definition: ds.h:218
TStr GetStr(const TLoc &Loc=lUs) const
Definition: tm.cpp:457
bool Empty() const
Definition: dt.h:488
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TQuad< TStr, TStr, TStr, TGpScaleTy > TPlotInfo
Definition: gstat.h:40
Definition: hash.h:88
void DelAfter(const TSecTm &Tm)
Definition: gstat.cpp:495
void GetValV(const TGStatVal &XVal, const TGStatVal &YVal, TFltPrV &ValV) const
Definition: gstat.cpp:511
void GetKeyDatPrV(TVec< TPair< TKey, TDat > > &KeyDatPrV) const
Definition: hash.h:458
void Plot(const TGStatDistr &Distr, const TStr &FNmPref, TStr Desc=TStr(), bool PowerFit=false) const
Definition: gstat.cpp:204
TGStat & operator=(const TGStat &GStat)
Definition: gstat.cpp:47
int AddPlot(const TIntV &YValV, const TGpSeriesTy &SeriesTy=gpwLinesPoints, const TStr &Label=TStr(), const TStr &Style=TStr())
Definition: gnuplot.cpp:186
Definition: bd.h:196
static int TakeSngVals
Definition: gstat.h:39
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
TGStat(const TSecTm &GraphTm=TSecTm(), const TStr &GraphName=TStr())
Definition: gstat.cpp:21
void TakeSccStat(const PGraph &Graph, TFSet StatFSet)
Definition: gstat.h:421
double GetVal(const TGStatVal &StatVal) const
Definition: gstat.cpp:81
Definition: gstat.h:23
Definition: gstat.h:16
static TFSet BasicStat()
Definition: gstat.cpp:380
void SortCmp(const TCmp &Cmp)
Sorts the elements of the vector using the comparator Cmp.
Definition: ds.h:739
char * CStr()
Definition: dt.h:476
TTmUnit
Definition: tm.h:11
bool IsKey(const TKey &Key) const
Definition: hash.h:216
static PGStatVec New(const TTmUnit &_TmUnit=tmu1Sec)
Definition: gstat.cpp:426
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
static TFSet DegDStat()
Definition: gstat.cpp:384
void SetDistr(const TGStatDistr &Distr, const TFltPrV &FltPrV)
Definition: gstat.cpp:97
TGpScaleTy
Definition: gnuplot.h:6
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void Plot(const TGStatVal &XVal, const TGStatVal &YVal, const TStr &OutFNm, TStr &Desc, const TGpScaleTy &Scale=gpsAuto, const bool &PowerFit=false) const
Definition: gstat.cpp:529
TGpSeriesTy
Definition: gnuplot.h:11
bool operator<(const TGStat &GStat) const
Definition: gstat.cpp:61
const TFltPrV & GetDistr(const TGStatDistr &Distr) const
Definition: gstat.cpp:92
void Def()
Definition: xmath.cpp:339
TGStatVal ValCmp
Definition: gstat.h:44
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:429
Definition: gstat.h:31
void TakeDegDistr(const PGraph &Graph)
Definition: gstat.h:301
void TakeBasicStat(const PGraph &Graph, const bool &IsMxWcc=false)
Definition: gstat.h:257
void TakeDiam(const PGraph &Graph, const bool &IsMxWcc=false)
Definition: gstat.h:326
bool In(const int &FlagN) const
Definition: bits.h:156