|
SNAP Library, User Reference
2012-10-15 15:06:59
SNAP, a general purpose network analysis and graph mining library
|
#include <timenet.h>
Inherits TNodeNet< TSecTm >.
Classes | |
| class | TTmBucket |
Public Types | |
| typedef TNodeNet< TSecTm > | TNet |
| typedef TPt< TNodeNet< TSecTm > > | PNet |
| typedef TVec< TTmBucket > | TTmBucketV |
Public Member Functions | |
| TTimeNet () | |
| TTimeNet (const int &Nodes, const int &Edges) | |
| TTimeNet (const TTimeNet &TimeNet) | |
| TTimeNet (TSIn &SIn) | |
| void | Save (TSOut &SOut) const |
| Saves the network to a (binary) stream SOut. | |
| TTimeNet & | operator= (const TTimeNet &TimeNet) |
| PTimeNet | GetSubGraph (const TIntV &NIdV) const |
| PTimeNENet | GetTimeNENet () const |
| void | GetNIdByTm (TIntV &NIdV) const |
| void | GetTmBuckets (const TTmUnit &GroupBy, TTmBucketV &TmBucketV) const |
| void | GetNodeBuckets (const int NodesPerBucket, TTimeNet::TTmBucketV &TmBucketV) const |
| PGStatVec | TimeGrowth (const TTmUnit &TmUnit, const TFSet &TakeStat, const TSecTm &StartTm) const |
| void | PlotEffDiam (const TStr &FNmPref, const TStr &Desc, const TTmUnit &GroupBy, const TSecTm &StartTm, const int &NDiamRuns=10, const bool &OnlyWcc=false, const bool &AlsoRewire=false) const |
| void | PlotMissingPast (const TStr &FNmPref, const TStr &Desc, const TTmUnit &GroupBy, const TSecTm &DelPreTmEdges, const TSecTm &PostTmDiam) const |
| void | PlotCCfOverTm (const TStr &FNmPref, TStr Desc, const TTmUnit &TmUnit, const int &NodesBucket=-1) const |
| void | PlotMedianDegOverTm (const TStr &FNmPref, const TTmUnit &TmUnit, const int &NodesPerBucket=-1) const |
Static Public Member Functions | |
| static PTimeNet | New () |
| Static constructor that returns a pointer to the network. Call: TPt <TNodeNet<TNodeData> > Net = TNodeNet<TNodeData>::New(). | |
| static PTimeNet | New (const int &Nodes, const int &Edges) |
| static PTimeNet | Load (TSIn &SIn) |
| Static constructor that loads the network from a stream SIn and returns a pointer to it. | |
| static PTimeNet | LoadBipartite (const TStr &InFNm) |
| static PTimeNet | LoadArxiv (const TStr &PaperFNm, const TStr &CiteFNm) |
| static PTimeNet | LoadPatents (const TStr &PatentFNm, const TStr &CiteFNm) |
| static PTimeNet | LoadAmazon (const TStr &StlFNm) |
Friends | |
| class | TPt< TTimeNet > |
| typedef TPt<TNodeNet<TSecTm> > TTimeNet::PNet |
Reimplemented from TNodeNet< TSecTm >.
| typedef TNodeNet<TSecTm> TTimeNet::TNet |
Reimplemented from TNodeNet< TSecTm >.
| typedef TVec<TTmBucket> TTimeNet::TTmBucketV |
| TTimeNet::TTimeNet | ( | ) | [inline] |
| TTimeNet::TTimeNet | ( | const int & | Nodes, |
| const int & | Edges | ||
| ) | [inline] |
| TTimeNet::TTimeNet | ( | const TTimeNet & | TimeNet | ) | [inline] |
| TTimeNet::TTimeNet | ( | TSIn & | SIn | ) | [inline] |
| void TTimeNet::GetNIdByTm | ( | TIntV & | NIdV | ) | const |
Definition at line 48 of file timenet.cpp.
{
TVec<TKeyDat<TSecTm, TInt> > TmToNIdV(GetNodes(), 0);
for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) {
TmToNIdV.Add(TKeyDat<TSecTm, TInt>(NodeI.GetDat(), NodeI.GetId())); }
TmToNIdV.Sort();
NIdV.Gen(GetNodes(), 0);
for (int i = 0; i < TmToNIdV.Len(); i++) {
NIdV.Add(TmToNIdV[i].Dat); }
}
| void TTimeNet::GetNodeBuckets | ( | const int | NodesPerBucket, |
| TTimeNet::TTmBucketV & | TmBucketV | ||
| ) | const |
Definition at line 77 of file timenet.cpp.
{
TIntV NIdV;
GetNIdByTm(NIdV);
TmBucketV.Gen(NIdV.Len() / NodesPerBucket + 1, 0);
for (int i = 0; i < NIdV.Len(); i++) {
const int b = i/NodesPerBucket;
if (TmBucketV.Len() <= b) { TmBucketV.Add(TTimeNet::TTmBucket(TSecTm(b))); }
TmBucketV[b].NIdV.Add(NIdV[i]);
}
}
| PTimeNet TTimeNet::GetSubGraph | ( | const TIntV & | NIdV | ) | const |
Definition at line 10 of file timenet.cpp.
{
PTimeNet NewNetPt = TTimeNet::New();
TTimeNet& NewNet = *NewNetPt;
NewNet.Reserve(NIdV.Len(), -1);
int node, edge;
TNodeI NI;
for (node = 0; node < NIdV.Len(); node++) {
NewNet.AddNode(NIdV[node], GetNDat(NIdV[node])); // also copy the node data
}
for (node = 0; node < NIdV.Len(); node++) {
NI = GetNI(NIdV[node]);
const int SrcNId = NI.GetId();
for (edge = 0; edge < NI.GetOutDeg(); edge++) {
const int OutNId = NI.GetOutNId(edge);
if (NewNet.IsNode(OutNId)) {
NewNet.AddEdge(SrcNId, OutNId); }
}
}
NewNet.Defrag();
return NewNetPt;
}
| PTimeNENet TTimeNet::GetTimeNENet | ( | ) | const |
Definition at line 32 of file timenet.cpp.
{
TIntV NIdV; GetNIdByTm(NIdV);
PTimeNENet OutNet = TTimeNENet::New(GetNodes(), GetEdges());
for (int i = 0; i < NIdV.Len(); i++) {
const int Src = NIdV[i];
const TTimeNet::TNodeI NI = GetNI(Src);
const TSecTm SrcTm = NI.GetDat();
if (! OutNet->IsNode(Src)) { OutNet->AddNode(Src, SrcTm); }
for (int e = 0; e < NI.GetOutDeg(); e++) {
if (! OutNet->IsNode(NI.GetOutNId(e))) { OutNet->AddNode(NI.GetOutNId(e), SrcTm); }
OutNet->AddEdge(Src, NI.GetOutNId(e), -1, SrcTm);
}
}
return OutNet;
}
| void TTimeNet::GetTmBuckets | ( | const TTmUnit & | GroupBy, |
| TTmBucketV & | TmBucketV | ||
| ) | const |
Definition at line 59 of file timenet.cpp.
{
THash<TInt, TIntV> TmIdToNIdVH;
for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) {
const int TmId = NodeI().Round(TmUnit);
if (! TmIdToNIdVH.IsKey(TmId)) TmIdToNIdVH.AddKey(TmId);
TmIdToNIdVH.GetDat(TmId).Add(NodeI.GetId());
}
TVec<TPair<TInt, TIntV> > TmIdNIdVV;
TmIdToNIdVH.GetKeyDatPrV(TmIdNIdVV);
TmIdNIdVV.Sort();
TmBucketV.Gen(TmIdNIdVV.Len());
for (int i = 0; i < TmIdNIdVV.Len(); i++) {
TTmBucket& Bucket = TmBucketV[i];
Bucket.BegTm = TmIdNIdVV[i].Val1;
Bucket.NIdV = TmIdNIdVV[i].Val2;
}
}
| static PTimeNet TTimeNet::Load | ( | TSIn & | SIn | ) | [inline, static] |
Static constructor that loads the network from a stream SIn and returns a pointer to it.
Reimplemented from TNodeNet< TSecTm >.
Definition at line 41 of file timenet.h.
{ return new TTimeNet(SIn); }
| PTimeNet TTimeNet::LoadAmazon | ( | const TStr & | StlFNm | ) | [static] |
Definition at line 561 of file timenet.cpp.
{
PTimeNet TimeNetPt = TTimeNet::New();
TTimeNet& TimeNet = *TimeNetPt;
TimeNet.Reserve(3953993, -1);
printf("Amazon Share-the-Love...\n");
char line [2024], MonthStr[4];
int NLines=0;
TStrV ColV;
FILE *F = fopen(StlFNm.CStr(), "rt");
while (! feof(F)) {
memset(line, 0, 2024);
fgets(line, 2024, F);
if (strlen(line) == 0) break;
TStr(line).SplitOnAllCh(',', ColV);
const int SrcNId = ColV[0].GetInt();
const int DstNId = ColV[1].GetInt();
// time data
TStr TmStr = ColV[2]; // time-format: 29JAN02:21:55:23
int Year = TmStr.GetSubStr(5, 6).GetInt();
if (Year < 10) { Year += 2000; } else { Year += 1900; }
MonthStr[0]=toupper(TmStr[2]); MonthStr[1]=tolower(TmStr[3]);
MonthStr[2]=tolower(TmStr[4]); MonthStr[3]=0;
const int Month = TTmInfo::GetMonthN(MonthStr, lUs);
const int Day = TmStr.GetSubStr(0, 1).GetInt();
const int Hour = TmStr.GetSubStr(8, 9).GetInt();
const int Min = TmStr.GetSubStr(11, 12).GetInt();
const int Sec = TmStr.GetSubStr(14, 15).GetInt();
// add nodes and links
if (! TimeNet.IsNode(SrcNId)) { TimeNet.AddNode(SrcNId, TSecTm(Year, Month, Day, Hour, Min, Sec)); }
if (! TimeNet.IsNode(DstNId)) { TimeNet.AddNode(DstNId, TSecTm(Year, Month, Day, Hour, Min, Sec)); }
if (! TimeNet.IsEdge(SrcNId, DstNId)) { TimeNet.AddEdge(SrcNId, DstNId); }
if (++NLines % 100000 == 0) printf("\r %dk", NLines/1000);
}
fclose(F);
printf("\r %d lines read\n", NLines);
printf("Graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
TimeNet.Defrag(true);
return TimeNetPt;
}
| PTimeNet TTimeNet::LoadArxiv | ( | const TStr & | PaperFNm, |
| const TStr & | CiteFNm | ||
| ) | [static] |
Definition at line 381 of file timenet.cpp.
{
TExeTm ExeTm;
PTimeNet TimeNetPt = TTimeNet::New();
TTimeNet& TimeNet = *TimeNetPt;
printf("Arxiv citation graph (paper publication year)...\n");
// load time data (file hep-ph-slacdates)
char Line [1024];
FILE *PprF = fopen(PaperFNm.CStr(), "rt");
TStr StrId, StrTime;
TStrV StrV, StrTimeV;
int N = 0, DuplicateNode = 0;
while (! feof(PprF)) {
Line[0] = 0;
fgets(Line, 1024, PprF);
if (strlen(Line) == 0 || Line[0] == '#') continue;
Line[strlen(Line)-1] = 0; // delete trailing '\n'
TStr(Line).SplitOnWs(StrV); IAssert(StrV.Len() == 2);
StrId = StrV[0]; StrTime = StrV[1]; IAssert(!StrId.Empty() && !StrTime.Empty());
StrTime.SplitOnAllCh('-', StrTimeV); IAssert(StrTimeV.Len() == 3);
const int NodeId = StrId.GetInt();
if (! TimeNet.IsNode(NodeId)) {
const int Year = StrTimeV[0].GetInt();
const int Month = StrTimeV[1].GetInt();
const int Day = StrTimeV[2].GetInt();
TimeNet.AddNode(NodeId, TSecTm(Year, Month, Day));
} else { DuplicateNode++; }
if (++N % 10000 == 0) printf("\r %dk", N/1000);
}
printf("\r %d nodes read. %d duplicate nodes. %s\n", N, DuplicateNode, ExeTm.GetTmStr());
fclose(PprF);
// load citations (file hep-ph-citations)
int NewSrcIds=0, NewDstIds=0, DupLinks=0, NewCits=0;
FILE *CiteF = fopen(CiteFNm.CStr(), "rt");
N = 0; ExeTm.Tick();
printf("Loading Arxiv citations...\n");
TIntPrV EdgeV;
THash<TInt, TSecTm> NIdToTimeH;
while (! feof(CiteF)) {
Line[0] = 0;
fgets(Line, 1024, CiteF);
if (strlen(Line) == 0 || Line[0] == '#') continue;
Line[strlen(Line)-1] = 0; // delete trailing '\n'
TStr(Line).SplitOnWs(StrV); IAssert(StrV.Len() == 2);
const int SrcNId = StrV[0].GetInt();
const int DstNId = StrV[1].GetInt();
EdgeV.Add(TIntPr(SrcNId, DstNId));
// infer time of destination node -- earliest paper that cites the node (paper)
if (! TimeNet.IsNode(DstNId) && TimeNet.IsNode(SrcNId)) {
const TSecTm& SrcTm = TimeNet.GetNDat(SrcNId);
if (! NIdToTimeH.IsKey(DstNId)) {
NIdToTimeH.AddDat(DstNId, SrcTm);
NewDstIds++;
}
else if (NIdToTimeH.GetDat(DstNId) < SrcTm) {
NIdToTimeH.GetDat(DstNId) = SrcTm; }
}
if (++N % 10000 == 0) printf("\r %dk", N/1000);
}
fclose(CiteF);
// add infeered time nodes (nodes which are cited by papers with known time)
for (int i = 0; i < NIdToTimeH.Len(); i++) {
TimeNet.AddNode(NIdToTimeH.GetKey(i), NIdToTimeH[i]);
}
// add links
for (int i = 0; i < EdgeV.Len(); i++) {
const int SrcNId = EdgeV[i].Val1;
const int DstNId = EdgeV[i].Val2;
if (TimeNet.IsNode(SrcNId) && TimeNet.IsNode(DstNId)) {
if (! TimeNet.IsEdge(SrcNId, DstNId)) { TimeNet.AddEdge(SrcNId, DstNId); }
else { DupLinks++; }
} else {
if (! TimeNet.IsNode(SrcNId)) {
NewSrcIds++;
if (! TimeNet.IsNode(DstNId)) { NewCits++; }
}
}
}
printf("\r %d citations read. %s\n", N, ExeTm.GetTmStr());
printf("Graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
printf("Removing 0-degree nodes: %d nodes\n", TSnap::CntDegNodes(TimeNetPt, 0));
TIntV RmNIdV;
for (TTimeNet::TNodeI ni = TimeNet.BegNI(); ni < TimeNet.EndNI(); ni++) {
if (ni.GetDeg() == 0) { RmNIdV.Add(ni.GetId()); }
}
for (int i = 0; i < RmNIdV.Len(); i++) {
TimeNet.DelNode(RmNIdV[i]);
}
TimeNet.Defrag(true);
printf("\nFinal graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
printf(" Duplicate citations : %d\n", DupLinks);
printf(" Nodes without time which are cited : %d (add them to graph, use time of the earliest source node)\n", NewDstIds);
printf(" Citations between unknown time nodes : %d\n", NewCits);
printf(" Nodes without time which make citations: %d (do not add them into the graph)\n", NewSrcIds);
return TimeNetPt;
}
| PTimeNet TTimeNet::LoadBipartite | ( | const TStr & | InFNm | ) | [static] |
Definition at line 344 of file timenet.cpp.
{
PTimeNet TimeNetPt = TTimeNet::New();
TTimeNet& TimeNet = *TimeNetPt;
PSs Ss = TSs::LoadTxt(ssfTabSep, InFNm.CStr());
TIntH Set1IdH; // paper ids
TStrV StrTimeV;
for (int y = 0; y < Ss->GetYLen(); y++) {
if (Ss->At(0, y)[0] == '#') continue; // skip comments
if (Ss->GetXLen(y) < 3) continue; // there must be at least one author
const int& SrcId = Ss->At(0, y).GetInt();
IAssert(! Set1IdH.IsKey(SrcId));
IAssert(! TimeNet.IsNode(SrcId));
Set1IdH.AddKey(SrcId);
Ss->At(1, y).SplitOnAllCh('-', StrTimeV);
const int Year = StrTimeV[0].GetInt();
const int Month = StrTimeV[1].GetInt();
const int Day = StrTimeV[2].GetInt();
const TSecTm NodeTm(Year, Month, Day);
TimeNet.AddNode(SrcId, NodeTm);
for (int dst = 2; dst < Ss->GetXLen(y); dst++) {
const int DstId = Ss->At(dst, y).GetInt();
IAssert(! Set1IdH.IsKey(DstId));
if (! TimeNet.IsNode(DstId)) { TimeNet.AddNode(DstId, NodeTm); }
else { TimeNet.GetNDat(DstId) = TMath::Mn(NodeTm, TimeNet.GetNDat(DstId)); }
if (! TimeNet.IsEdge(SrcId, DstId)) { TimeNet.AddEdge(SrcId, DstId); }
}
}
TimeNet.Defrag();
printf("Bipartate graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
printf(" Bipartate sets: %d nodes --> %d nodes\n", TSnap::CntInDegNodes(TimeNetPt, 0),
TSnap::CntOutDegNodes(TimeNetPt, 0));
return TimeNetPt;
}
| PTimeNet TTimeNet::LoadPatents | ( | const TStr & | PatentFNm, |
| const TStr & | CiteFNm | ||
| ) | [static] |
Definition at line 478 of file timenet.cpp.
{
int N = 0;
TExeTm ExeTm;
PTimeNet TimeNetPt = TTimeNet::New();
TTimeNet& TimeNet = *TimeNetPt;
TimeNet.Reserve(4000000, 160000000);
printf("parsing patent data (patent grant year)...\n");
// load time data (file pat63_99.txt)
const int& PatIdCol = 0;
const int& GYearCol = 1;
TStrV ColV;
char Line [1024];
FILE *PatF = fopen(PatentFNm.CStr(), "rt");
fgets(Line, 1024, PatF); // skip 1st line
while (! feof(PatF)) {
Line[0] = 0;
fgets(Line, 1024, PatF);
if (strlen(Line) == 0) break;
TStr(Line).SplitOnAllCh(',', ColV, false);
IAssert(ColV.Len() == 23);
const int PatentId = ColV[PatIdCol].GetInt();
const int GrantYear = ColV[GYearCol].GetInt();
IAssert(! TimeNet.IsNode(PatentId));
TimeNet.AddNode(PatentId, TSecTm(GrantYear)); // pretend year is a second
if (++N % 100000 == 0) printf("\r %dk", N/1000);
}
printf("\r %d patents read. %s\n", N, ExeTm.GetTmStr());
fclose(PatF);
// load citations (file cite75_99.txt)
printf("\nLoading patent citations...\n");
int NewSrcIds=0, NewDstIds=0, DupLinks=0, NewCits=0;
N = 0; ExeTm.Tick();
TStr SrcId, DstId;
FILE *CiteF = fopen(CiteFNm.CStr(), "rt");
fgets(Line, 1024, CiteF); // skip 1st line
while (! feof(CiteF)) {
Line[0] = 0;
fgets(Line, 1024, CiteF);
if (strlen(Line) == 0) break;
Line[strlen(Line)-1] = 0; // delete trailing '\n'
TStr(Line).SplitOnCh(SrcId, ',', DstId);
const int SrcNId = SrcId.GetInt();
const int DstNId = DstId.GetInt();
if (! TimeNet.IsNode(SrcNId) && ! TimeNet.IsNode(DstNId)) {
//TimeNet.AddNode(SrcNId, TSecTm(1, 1, 1)); NewSrcIds++;
//TimeNet.AddNode(DstNId, TSecTm(1, 1, 1)); NewDstIds++;
NewCits++;
continue;
}
else if (TimeNet.IsNode(SrcNId) && ! TimeNet.IsNode(DstNId)) {
TimeNet.AddNode(DstNId, TimeNet.GetNDat(SrcNId)); NewDstIds++;
}
else if (! TimeNet.IsNode(SrcNId) && TimeNet.IsNode(DstNId)) {
TimeNet.AddNode(SrcNId, TimeNet.GetNDat(DstNId)); NewSrcIds++;
}
if (! TimeNet.IsEdge(SrcNId, DstNId)) {
TimeNet.AddEdge(SrcNId, DstNId);
} else { DupLinks++; }
if (++N % 100000 == 0) printf("\r %dk", N/1000);
}
fclose(CiteF);
printf("\r %d citations read. %s\n\n", N, ExeTm.GetTmStr());
printf("Graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
printf("Removing 0-degree nodes: %d nodes\n", TSnap::CntDegNodes(TimeNetPt, 0));
TIntV RmNIdV;
for (TTimeNet::TNodeI ni = TimeNet.BegNI(); ni < TimeNet.EndNI(); ni++) {
if (ni.GetDeg() == 0) { RmNIdV.Add(ni.GetId()); }
}
for (int i = 0; i < RmNIdV.Len(); i++) {
TimeNet.DelNode(RmNIdV[i]);
}
TimeNet.Defrag(true);
printf("\nFinal graph: nodes: %d edges: %d\n", TimeNet.GetNodes(), TimeNet.GetEdges());
printf(" Duplicate citations : %d\n", DupLinks);
printf(" Citations between unknown time nodes : %d\n", NewCits);
printf(" Nodes without time which make citations: %d\n", NewSrcIds);
printf(" Nodes without time which are cited : %d\n", NewDstIds);
return TimeNetPt;
}
| static PTimeNet TTimeNet::New | ( | ) | [inline, static] |
Static constructor that returns a pointer to the network. Call: TPt <TNodeNet<TNodeData> > Net = TNodeNet<TNodeData>::New().
Reimplemented from TNodeNet< TSecTm >.
Definition at line 39 of file timenet.h.
{ return new TTimeNet(); }
| static PTimeNet TTimeNet::New | ( | const int & | Nodes, |
| const int & | Edges | ||
| ) | [inline, static] |
Definition at line 3 of file timenet.cpp.
{
if (this != &TimeNet) {
TNet::operator=(TimeNet);
}
return *this;
}
| void TTimeNet::PlotCCfOverTm | ( | const TStr & | FNmPref, |
| TStr | Desc, | ||
| const TTmUnit & | TmUnit, | ||
| const int & | NodesBucket = -1 |
||
| ) | const |
Definition at line 253 of file timenet.cpp.
{
if (Desc.Empty()) { Desc = FNmPref; }
TTimeNet::TTmBucketV TmBucketV;
TStr XLbl;
if (TmUnit == tmuNodes) {
XLbl = "Number of nodes (time)";
IAssert(NodesBucket > 0);
GetNodeBuckets(NodesBucket, TmBucketV); }
else {
XLbl = TStr::Fmt("Time (%s)", TTmInfo::GetTmUnitStr(TmUnit).CStr());
GetTmBuckets(TmUnit, TmBucketV);
}
TIntV NodeIdV;
TFltPrV DegToCCfV, CcfV, OpClV, OpV;
TVec<TTuple<TFlt, 4> > OpenClsV;
TTuple<TFlt, 4> Tuple;
TExeTm ExeTm;
int XVal = 0;
printf("Clustering coefficient over time:\n %d edges, %d edges per bucket, %d buckets \n", GetEdges(), 100000, TmBucketV.Len());
PUNGraph UNGraph = TSnap::ConvertGraph<PUNGraph>(PTimeNet((TTimeNet*)this));
for (int t = 0; t < TmBucketV.Len(); t++) {
printf("\r %d/%d: ", t+1, TmBucketV.Len());
NodeIdV.AddV(TmBucketV[t].NIdV); // edges up to time T
int Open=0, Close=0;
const PUNGraph Graph = TSnap::GetSubGraph(UNGraph, NodeIdV);
const double CCf = TSnap::GetClustCf(Graph, DegToCCfV, Open, Close);
if (TmUnit == tmuNodes) { XVal = Graph->GetNodes(); }
else { XVal = TmBucketV[t].BegTm.GetInUnits(TmUnit); }
CcfV.Add(TFltPr(XVal, CCf));
OpClV.Add(TFltPr(XVal, Open+Close==0?0:Close/(Open+Close)));
OpV.Add(TFltPr(XVal, Open==0?0:Close/Open));
Tuple[0]=Graph->GetNodes();
Tuple[1]=Graph->GetEdges();
Tuple[2]=Close; Tuple[3]=Open;
OpenClsV.Add(Tuple);
printf(" %s", ExeTm.GetStr());
TGnuPlot::PlotValV(DegToCCfV, TStr::Fmt("ccfAt%02dtm.%s", t+1, FNmPref.CStr()),
TStr::Fmt("%s. At time %d. Clustering Coefficient. G(%d,%d)", Desc.CStr(), t+1, Graph->GetNodes(), Graph->GetEdges()),
"Degree", "Clustering coefficient", gpsLog10XY, false);
}
TGnuPlot::PlotValV(CcfV, "ccfOverTm."+FNmPref, Desc+". Average Clustering Coefficient", XLbl, "Average clustering coefficient", gpsAuto, false);
TGnuPlot::PlotValV(OpClV, "ClsOpnTr1."+FNmPref, Desc+". Close/(Open+Closed) triads", XLbl, "Close / (Open+Closed) triads", gpsAuto, false);
TGnuPlot::PlotValV(OpV, "ClsOpnTr2."+FNmPref, Desc+". Close/Open triads", XLbl, "Close / Open triads", gpsAuto, false);
TGnuPlot::SaveTs(OpenClsV, "ClsOpnTr."+FNmPref+".tab", TStr::Fmt("#%s\n#Nodes\tEdges\tClosed\tOpenTriads", Desc.CStr()));
printf("\n");
}
| void TTimeNet::PlotEffDiam | ( | const TStr & | FNmPref, |
| const TStr & | Desc, | ||
| const TTmUnit & | GroupBy, | ||
| const TSecTm & | StartTm, | ||
| const int & | NDiamRuns = 10, |
||
| const bool & | OnlyWcc = false, |
||
| const bool & | AlsoRewire = false |
||
| ) | const |
Definition at line 106 of file timenet.cpp.
{
const TStr WccStr = OnlyWcc ? "WCC " : TStr::GetNullStr();
TTmBucketV TmBucketV;
GetTmBuckets(TmUnit, TmBucketV);
TIntV NodeIdV;
TExeTm ExeTm, Run1Tm;
TFltTrV TmDiamV, NdsDiamV;
TFltTrV RwTmDiamV, RwNdsDiamV;
for (int t = 0; t < TmBucketV.Len(); t++) {
NodeIdV.AddV(TmBucketV[t].NIdV); // nodes up to time T
printf("\n*** %d/%d] at %s (%d nodes)\n", t+1, TmBucketV.Len(),
TmBucketV[t].BegTm.GetStr(TmUnit).CStr(), NodeIdV.Len()); ExeTm.Tick();
if (TmBucketV[t].BegTm < StartTm) continue;
//PUNGraph PreGraph = GetSubUNGraph(NodeIdV, true);
PUNGraph PreGraph = TSnap::ConvertSubGraph<PUNGraph>(PTimeNet((TTimeNet*)this), NodeIdV);
{ TMom Mom;
for (int r = 0; r < NDiamRuns; r++) {
printf("%d...", r+1); Run1Tm.Tick();
const double EffDiam = TSnap::GetAnfEffDiam(OnlyWcc ? TSnap::GetMxWcc(PreGraph) : PreGraph);
Mom.Add(EffDiam); printf("[%s]\r", Run1Tm.GetTmStr());
}
Mom.Def();
TmDiamV.Add(TFltTr((int)TmBucketV[t].BegTm.GetInUnits(TmUnit), Mom.GetMean(), Mom.GetSDev()));
NdsDiamV.Add(TFltTr(PreGraph->GetNodes(), Mom.GetMean(), Mom.GetSDev()));
NdsDiamV.Sort();
printf(" [%s] \n", ExeTm.GetTmStr()); }
if (AlsoRewire) {
//PUNGraph RwGraph = TGGen::GenRndDegSeqS(PreGraph, 50, TInt::Rnd); // edge switching model
TIntV DegSeqV(PreGraph->GetNodes(), 0);
for (TUNGraph::TNodeI NI = PreGraph->BegNI(); NI < PreGraph->EndNI(); NI++) { DegSeqV.Add(NI.GetDeg()); }
PUNGraph RwGraph = TSnap::GenConfModel(DegSeqV, TInt::Rnd);
printf("Configuration model: (%d, %d) --> (%d, %d)\n", PreGraph->GetNodes(), PreGraph->GetEdges(), RwGraph->GetNodes(), RwGraph->GetEdges());
TMom Mom;
for (int r = 0; r < NDiamRuns; r++) {
printf(" diam run %d...", r+1); Run1Tm.Tick();
const double EffDiam = TSnap::GetAnfEffDiam(OnlyWcc ? TSnap::GetMxWcc(PreGraph):PreGraph);
Mom.Add(EffDiam); printf(" current run [%s]\n", Run1Tm.GetTmStr());
}
Mom.Def();
RwTmDiamV.Add(TFltTr((int)TmBucketV[t].BegTm.GetInUnits(TmUnit), Mom.GetMean(), Mom.GetSDev()));
RwNdsDiamV.Add(TFltTr(PreGraph->GetNodes(), Mom.GetMean(), Mom.GetSDev()));
RwNdsDiamV.Sort();
printf("done with diameter. Total time [%s] \n", ExeTm.GetTmStr());
}
// plot
{ TGnuPlot GnuPlot("diamEff-T."+FNmPref, TStr::Fmt("%s. G(%d, %d)", Desc.CStr(), GetNodes(), GetEdges()));
GnuPlot.SetXYLabel(TStr::Fmt("TIME [%s]", TTmInfo::GetTmUnitStr(TmUnit).CStr()), WccStr+"Effective Diameter");
GnuPlot.AddErrBar(TmDiamV, "True", "");
if (! RwTmDiamV.Empty()) { GnuPlot.AddErrBar(RwTmDiamV, "Rewired", "");}
GnuPlot.SavePng(); }
{ TGnuPlot GnuPlot("diamEff-N."+FNmPref, TStr::Fmt("%s. G(%d, %d)", Desc.CStr(), GetNodes(), GetEdges()));
GnuPlot.SetXYLabel("NODES", WccStr+"Effective Diameter");
GnuPlot.AddErrBar(NdsDiamV, "True", "");
if (! RwNdsDiamV.Empty()) { GnuPlot.AddErrBar(RwNdsDiamV, "Rewired", "");}
GnuPlot.SavePng(); }
}
}
| void TTimeNet::PlotMedianDegOverTm | ( | const TStr & | FNmPref, |
| const TTmUnit & | TmUnit, | ||
| const int & | NodesPerBucket = -1 |
||
| ) | const |
Definition at line 300 of file timenet.cpp.
{
TTimeNet::TTmBucketV TmBucketV;
TStr XLbl;
if (TmUnit == tmuNodes) {
XLbl = "Number of nodes (time)"; IAssert(NodesPerBucket > 0);
GetNodeBuckets(NodesPerBucket, TmBucketV); }
else {
XLbl = TStr::Fmt("Time (%s)", TTmInfo::GetTmUnitStr(TmUnit).CStr());
GetTmBuckets(TmUnit, TmBucketV); }
printf("\n\n%s\nMedian degree over time:\n %d edges, %d edges per bucket, %d buckets \n", FNmPref.CStr(), GetEdges(), NodesPerBucket, TmBucketV.Len());
TFltPrV MedDegV, MedInDegV, MedOutDegV;
TIntV NodeIdV;
int XVal;
PUNGraph UNGraph = TSnap::ConvertGraph<PUNGraph>(PTimeNet((TTimeNet*)this));
PNGraph NGraph = TSnap::ConvertGraph<PNGraph>(PTimeNet((TTimeNet*)this));
FILE *F = fopen(("gStat-"+FNmPref+".tab").CStr(), "wt");
fprintf(F, "UndirNodes\tUndirEdges\tUndirNonZNodes\tMedianDeg\tMeanDeg\tDirNodes\tDirEdges\tDirNonzNodes\tMedInDeg\tMedOutDeg\tMeanInDeg\tMeanOutDeg\n");
for (int t = 0; t < TmBucketV.Len(); t++) {
printf("\r %d/%d: ", t+1, TmBucketV.Len());
NodeIdV.AddV(TmBucketV[t].NIdV); // edges up to time T
if (TmUnit == tmuNodes) { XVal = NodeIdV.Len(); }
else { XVal = TmBucketV[t].BegTm.GetInUnits(TmUnit); }
// un graph
{ const PUNGraph Graph = TSnap::GetSubGraph(UNGraph, NodeIdV); TMom Mom;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { if (NI.GetOutDeg()>0) { Mom.Add(NI.GetOutDeg());} }
Mom.Def(); MedDegV.Add(TFltPr(XVal, Mom.GetMedian()));
fprintf(F, "%d\t%d\t%d\t%f\t%f", Graph->GetNodes(), Graph->GetEdges(), TSnap::CntNonZNodes(Graph), (float)Mom.GetMedian(), (float)Mom.GetMean()); }
// directed graph
{ const PNGraph Graph = TSnap::GetSubGraph<PNGraph>(NGraph, NodeIdV); TMom MomOut, MomIn;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
if (NI.GetOutDeg()>0) { MomOut.Add(NI.GetOutDeg()); }
if (NI.GetInDeg()>0) { MomIn.Add(NI.GetInDeg()); } }
MomOut.Def(); MedOutDegV.Add(TFltPr(XVal, MomOut.GetMedian()));
MomIn.Def(); MedInDegV.Add(TFltPr(XVal, MomIn.GetMedian()));
fprintf(F, "\t%d\t%d\t%d\t%f\t%f\t%f\t%f\n", Graph->GetNodes(), Graph->GetEdges(), (int)TSnap::CntNonZNodes(Graph), (float)MomIn.GetMedian(), (float)MomOut.GetMedian(), (float)MomIn.GetMean(), (float)MomOut.GetMean()); }
}
fclose(F);
TGnuPlot::PlotValV(MedDegV, "medDeg."+FNmPref, FNmPref+" Median degree", TTmInfo::GetTmUnitStr(TmUnit), "Median degree");
TGnuPlot::PlotValV(MedOutDegV, "medOutDeg."+FNmPref, FNmPref+" Median OUT degree", TTmInfo::GetTmUnitStr(TmUnit), "Median OUT degree");
TGnuPlot::PlotValV(MedInDegV, "medInDeg."+FNmPref, FNmPref+" Median IN degree", TTmInfo::GetTmUnitStr(TmUnit), "Median IN degree");
}
| void TTimeNet::PlotMissingPast | ( | const TStr & | FNmPref, |
| const TStr & | Desc, | ||
| const TTmUnit & | GroupBy, | ||
| const TSecTm & | DelPreTmEdges, | ||
| const TSecTm & | PostTmDiam | ||
| ) | const |
Definition at line 169 of file timenet.cpp.
{
printf("\nGrowth over time: degree distribution, Growth Power Law, Diameter.\n %s group by %s.\n",
FNmPref.CStr(), TTmInfo::GetTmUnitStr(TmUnit).CStr());
printf(" Delete out-edges of pre time %s nodes.\n Take subgraph of post year %s subgraph.\n\n",
DelPreTmEdges.GetStr().CStr(), PostTmDiam.GetStr().CStr());
const int NDiamRuns = 10;
const int NSamples = 100;
//PUNGraph FullGraph = GetUNGraph();
PUNGraph FullGraph = TSnap::ConvertGraph<PUNGraph>(PTimeNet((TTimeNet*)this));
// delete past
if (DelPreTmEdges.IsDef()) {
int NDelNodes = 0, NDelEdges = 0;
printf("Deleting pre-%s node out-links\n", DelPreTmEdges.GetStr().CStr());
for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) {
if (NodeI() < DelPreTmEdges) {
const int NodeId = NodeI.GetId();
for (int edge = 0; edge < NodeI.GetOutDeg(); edge++) {
FullGraph->DelEdge(NodeId, NodeI.GetOutNId(edge)); }
NDelEdges += NodeI.GetOutDeg(); NDelNodes++;
}
}
printf(" Deleted %d nodes out-edges (%d edges total).\n", NDelNodes, NDelEdges);
FullGraph->Defrag(true);
}
PGStatVec GrowthStat = TGStatVec::New(TmUnit);
TFltV PreDiamSDev, PreEffDiamSDev, WccDiamSDev, WccEffDiamSDev;
TIntV NodeIdV;
TExeTm ExeTm;
TTmBucketV TmBucketV;
GetTmBuckets(TmUnit, TmBucketV);
for (int t = 0; t < TmBucketV.Len(); t++) {
printf("\nGraph: %s (%d / %d) [%s]\n", TmBucketV[t].BegTm.GetTmStr().CStr(),
t+1, TmBucketV.Len(), TExeTm::GetCurTm());
// up-to-year subgraph
NodeIdV.AddV(TmBucketV[t].NIdV); // nodes up to time T
if (TmBucketV[t].BegTm < PostTmDiam) { continue; }
const PUNGraph PreGraph = TSnap::GetSubGraph(FullGraph, NodeIdV, true);
const PUNGraph WccGraph = TSnap::GetMxWcc(PreGraph);
TIntV PostYearNIdV, WccPostYearNIdV;
for (TUNGraph::TNodeI NI = PreGraph->BegNI(); NI < PreGraph->EndNI(); NI++) {
if (GetNDat(NI.GetId()) >= PostTmDiam) {
PostYearNIdV.Add(NI.GetId());
if (WccGraph->IsNode(NI.GetId())) { WccPostYearNIdV.Add(NI.GetId()); }
}
}
TMom PreDiamMom, PreEffDiamMom, WccDiamMom, WccEffDiamMom;
// diameter of PostYearDiam subgraph using whole graph (all edges)
int FullDiam; double EffDiam;
for (int r = 0; r < NDiamRuns; r++) {
if (! PostYearNIdV.Empty()) {
TSnap::GetBfsEffDiam(PreGraph, NSamples, PostYearNIdV, false, EffDiam, FullDiam);
PreDiamMom.Add(FullDiam); PreEffDiamMom.Add(EffDiam);
}
if (! WccPostYearNIdV.Empty()) {
TSnap::GetBfsEffDiam(WccGraph, NSamples, WccPostYearNIdV, false, EffDiam, FullDiam);
WccDiamMom.Add(FullDiam); WccEffDiamMom.Add(EffDiam);
}
printf(" diam: %d [%s] \r", r+1, ExeTm.GetTmStr()); ExeTm.Tick();
}
PreDiamMom.Def(); PreEffDiamMom.Def();
WccDiamMom.Def(); WccEffDiamMom.Def();
// save stat
PGStat GraphStatPt = GrowthStat->Add(TmBucketV[t].BegTm);
TGStat& GS = *GraphStatPt;
GS.TakeBasicStat(PreGraph, false);
GS.TakeBasicStat(WccGraph, true);
GS.SetVal(gsvFullDiam, PreDiamMom.GetMean()); // mean
GS.SetVal(gsvEffDiam, PreEffDiamMom.GetMean());
GS.SetVal(gsvFullWccDiam, WccDiamMom.GetMean());
GS.SetVal(gsvEffWccDiam, WccEffDiamMom.GetMean());
GS.SetVal(gsvFullDiamDev, PreDiamMom.GetSDev()); // variance
GS.SetVal(gsvEffDiamDev, PreEffDiamMom.GetSDev());
GS.SetVal(gsvFullWccDiamDev, WccDiamMom.GetSDev());
GS.SetVal(gsvEffWccDiamDev, WccEffDiamMom.GetSDev());
{ TFOut FOut("growth."+FNmPref+".gStatVec"); GrowthStat->Save(FOut); }
GrowthStat->SaveTxt(FNmPref, TStr::Fmt("%s. MISSING PAST DIAMETER\nDelPreEdges\t%s\nPostYearDiam\t%s\n",
Desc.CStr(), DelPreTmEdges.GetStr().CStr(), PostTmDiam.GetStr().CStr()));
}
// diameter plots
//GrowthStat->PlotDiam(FNmPref, Desc + TStr::Fmt(" MISSING PAST. DelPre:%d PostYear:%d.",
// DelPreEdges, PostYearDiam));*/
}
| void TTimeNet::Save | ( | TSOut & | SOut | ) | const [inline, virtual] |
Saves the network to a (binary) stream SOut.
Reimplemented from TNodeNet< TSecTm >.
Definition at line 38 of file timenet.h.
{ TNet::Save(SOut); }
| PGStatVec TTimeNet::TimeGrowth | ( | const TTmUnit & | TmUnit, |
| const TFSet & | TakeStat, | ||
| const TSecTm & | StartTm | ||
| ) | const |
Definition at line 88 of file timenet.cpp.
{
PGStatVec GrowthStat = new TGStatVec(TmUnit, TakeStat);
TTmBucketV TmBucketV;
GetTmBuckets(TmUnit, TmBucketV);
TIntV NodeIdV;
TExeTm ExeTm;
for (int t = 0; t < TmBucketV.Len(); t++) {
NodeIdV.AddV(TmBucketV[t].NIdV); // nodes up to time T
printf("\n=== %d/%d] %s (%d nodes)\n", t+1, TmBucketV.Len(),
TmBucketV[t].BegTm.GetStr().CStr(), NodeIdV.Len()); ExeTm.Tick();
if (TmBucketV[t].BegTm < StartTm) continue;
//PNGraph PreGraph = GetSubGraph(NodeIdV, true); // renumber nodes
PNGraph PreGraph = TSnap::ConvertSubGraph<PNGraph>(PTimeNet((TTimeNet*)this), NodeIdV); // don't renumber nodes
GrowthStat->Add(PreGraph, TmBucketV[t].BegTm);
}
return GrowthStat;
}