|
SNAP Library , User Reference
2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
#include <ncp.h>
Public Member Functions | |
| TNcpGraphsBase (const TStr &FNmWc) | |
| TNcpGraphsBase (TSIn &SIn) | |
| void | Save (TSOut &SOut) const |
| void | Impose (const TStr &OutFNm, const int &TopN, const bool &Smooth) |
| double | GetXAtMinY (const TFltPrV &Ncp, const int &NNodes) |
| TFltPr | GetXYAtMinY (const TFltPrV &Ncp, const int &NNodes) |
| void | PlotNcpMin (const TStr &OutFNm, const bool &VsGraphN=false) |
| void | SaveTxtNcpMin (const TStr &OutFNm, const bool &VsGraphN=false) |
| void | PlotRewNcpMin (const TStr &OutFNm, const bool &VsGraphN=false) |
| void | PlotBestWhisker (const TStr &OutFNm, const bool &VsGraphN=false) |
| void | PlotRewBestWhisker (const TStr &OutFNm, const bool &VsGraphN=false) |
| void | PlotAvgNcp (const TStr &OutFNm, const TVec< TFltPrV > &NcpVec, const int &MinSz, const double &MaxMinY) |
| void | SaveTxt (const TStr &OutFNm) |
Static Public Member Functions | |
| static void | PlotDataset (const TStr &InFNmWc, const TStr &OutFNm, const bool &ImposeNcp=false, const bool &VsGraphN=false) |
Private Attributes | |
| TStrV | GNmV |
| TFltV | ParamValV |
| TIntPrV | GSizeV |
| TFltPrV | WhiskerV |
| TFltPrV | RewWhiskerV |
| TVec< TFltPrV > | NcpV |
| TVec< TFltPrV > | RewNcpV |
| TVec< TFltPrV > | WhiskNcpV |
Local-Spectral-Clustering for a set of graphs (loads ncp-*.tab files)
| TNcpGraphsBase::TNcpGraphsBase | ( | const TStr & | FNmWc | ) |
Definition at line 1064 of file ncp.cpp.
{
TStr FNm;
for (TFFile FFile(FNmWc); FFile.Next(FNm); ) {
TSsParser Ss(FNm, ssfTabSep, true, false);
int TrueNcpId=-1, WhiskId=-1, RewBestWhiskId=-1, RewId=-1, BestWhiskId=-1;
while (Ss.Next()) {
for (int f = 0; f < Ss.GetFlds(); f++) {
// load ForestFire parameter (fwd burn prob)
if (strstr(Ss[f], "FWD:")) {
TStr S(Ss[f]); const int x = S.SearchStr("FWD:");
ParamValV.Add(S.GetSubStr(x+4, S.SearchCh(' ', x+1)-1).GetFlt());
}
// extract column names
if (strstr(Ss[f], "ORIGINAL MIN")!=NULL) {
GNmV.Add(TStr::Fmt("%s %s", FNm.GetSubStr(FNm.SearchCh('.')+1, FNm.SearchChBack('.')-1).CStr(), strchr(Ss[f], '(')));
int Nodes=0,Edges=0; sscanf(strchr(Ss[f], '(')+1, "%d,%d)", &Nodes, &Edges);
GSizeV.Add(TIntPr(Nodes, Edges));
printf("%s: %d %d\n", GNmV.Last().CStr(), Nodes, Edges);
TrueNcpId=f;
}
if (strstr(Ss[f], "ORIGINAL whisker")!=NULL || strstr(Ss[f], "TRUE whisker")!=NULL) { WhiskId=f; }
if (strstr(Ss[f], "ORIGINAL Best whisker")!=NULL || strstr(Ss[f], "TRUE Best whisker")!=NULL) { BestWhiskId=f; }
if (strstr(Ss[f], "REWIRED MIN")!=NULL || strstr(Ss[f], "RAND MIN")!=NULL) { RewId=f; }
if (strstr(Ss[f], "REWIRED Best whisker")!=NULL || strstr(Ss[f], "RAND Best whisker")!=NULL) { RewBestWhiskId=f; }
}
if (TrueNcpId!=-1 || WhiskId!=-1) { break; }
}
if (TrueNcpId < 0) { printf("%s\n", FNm.GetFMid().CStr()); break; }
if (BestWhiskId < 0) { WhiskerV.Add(TFltPr(1,1)); }
if (RewBestWhiskId < 0) { RewWhiskerV.Add(TFltPr(1,1)); }
NcpV.Add(); WhiskNcpV.Add(); RewNcpV.Add();
TFltPrV& Ncp = NcpV.Last();
TFltPrV& WhiskNcp = WhiskNcpV.Last();
TFltPrV& RewNcp = RewNcpV.Last();
bool Once=false, Once2=false;
while (Ss.Next()) {
if (TrueNcpId < Ss.GetFlds()&& Ss.IsFlt(TrueNcpId)) { Ncp.Add(TFltPr(Ss.GetFlt(TrueNcpId-1), Ss.GetFlt(TrueNcpId))); }
if (WhiskId>=0 && WhiskId < Ss.GetFlds() && Ss.IsFlt(WhiskId)) { WhiskNcp.Add(TFltPr(Ss.GetFlt(WhiskId-1), Ss.GetFlt(WhiskId))); }
if (RewId >=0 && RewId < Ss.GetFlds()&& Ss.IsFlt(RewId)) { RewNcp.Add(TFltPr(Ss.GetFlt(RewId-1), Ss.GetFlt(RewId))); }
if (BestWhiskId>=0 && BestWhiskId < Ss.GetFlds() && ! Once) { Once=true;
int W2=BestWhiskId-1; while (W2 > 0 && Ss.GetFlt(W2)!=(double)int(Ss.GetFlt(W2))) { W2--; }
WhiskerV.Add(TFltPr(Ss.GetFlt(W2), Ss.GetFlt(BestWhiskId))); }
if (RewBestWhiskId>=0 && RewBestWhiskId < Ss.GetFlds() && ! Once2) { Once2=true;
int W2=RewBestWhiskId-1; while (W2 > 0 && Ss.GetFlt(W2)!=(double)int(Ss.GetFlt(W2))) { W2--; }
RewWhiskerV.Add(TFltPr(Ss.GetFlt(W2), Ss.GetFlt(RewBestWhiskId))); }
}
printf(" ncp:%d whisk:%d rew:%d\n", NcpV.Last().Len(), WhiskNcpV.Last().Len(), RewNcpV.Last().Len());
}
IAssert(NcpV.Len() == GSizeV.Len());
}
| TNcpGraphsBase::TNcpGraphsBase | ( | TSIn & | SIn | ) |
| double TNcpGraphsBase::GetXAtMinY | ( | const TFltPrV & | Ncp, |
| const int & | NNodes | ||
| ) |
Definition at line 1133 of file ncp.cpp.
{
double MinX1=1, MinY1=1;
for (int k = 0; k < Ncp.Len(); k++) {
if (Ncp[k].Val2<MinY1) { MinX1=Ncp[k].Val1; MinY1=Ncp[k].Val2; } }
return MinX1<1 ? 1 : MinX1;
//if (NNodes < 1000) return MinX1;
// smooth
/*const int WndSize = 50;
double MinX=1, MinY=1;
TFltPrV Ncp2V;
for (int k = 0; k < Ncp.Len(); k++) {
int WndSz = k > WndSize ? WndSize : k;
double SmoothVal=0.0, SmoothCnt=0;
for (int i = -WndSz; i <= WndSz; i++) {
if (k+i > -1 && k+i < Ncp.Len()) { SmoothCnt+=pow(1.1, -abs(i));
SmoothVal+=pow(1.1, -abs(i)) * Ncp[k+i].Val2; }
}
SmoothVal = SmoothVal/SmoothCnt;
Ncp2V.Add(TFltPr(Ncp[k].Val1, SmoothVal));
if (SmoothVal<MinY) { MinX=Ncp[k].Val1; MinY=SmoothVal; }
}
static int cnt = 0;
if (Ncp2V.Len() > 10 && cnt < 10) {
TGnuPlot GP(TStr::Fmt("test-%03d", ++cnt));
GP.SetScale(gpsLog10XY);
GP.AddPlot(Ncp, gpwLines, "true");
GP.AddPlot(Ncp2V, gpwLines, "smooth");
GP.SavePng();
}
if (MinX < 1) { return 1; } else if (MinX > 1000) { return 1000; }
return MinX;*/
}
| TFltPr TNcpGraphsBase::GetXYAtMinY | ( | const TFltPrV & | Ncp, |
| const int & | NNodes | ||
| ) |
| void TNcpGraphsBase::Impose | ( | const TStr & | OutFNm, |
| const int & | TopN, | ||
| const bool & | Smooth | ||
| ) |
| void TNcpGraphsBase::PlotAvgNcp | ( | const TStr & | OutFNm, |
| const TVec< TFltPrV > & | NcpVec, | ||
| const int & | MinSz, | ||
| const double & | MaxMinY | ||
| ) |
Definition at line 1247 of file ncp.cpp.
{
THash<TFlt, TMom> MomH;
int Cnt=0;
for (int i = 0; i < NcpVec.Len(); i++) {
if (GSizeV[i].Val1 < MinSz) { continue; }
const TFltPrV& Ncp = NcpVec[i];
double MinX=1, MinY=1;
for (int k = 0; k < Ncp.Len(); k++){
if (Ncp[k].Val2<MinY) { MinX=Ncp[k].Val1; MinY=Ncp[k].Val2; } }
if (MinY > MaxMinY) { continue; } Cnt++;
//const double Coef = (1-0.0001)/(1.0-MinY);
for (int k = 0; k < Ncp.Len(); k++){
//MomH.AddDat(TMath::Round(exp(TMath::Round(log(Ncp[k].Val1()), 2)),2)).Add(0.0001+(Ncp[k].Val2-MinY)*Coef);
MomH.AddDat(TMath::Round(exp(TMath::Round(log(Ncp[k].Val1()), 1)),0)).Add(Ncp[k].Val2);
}
}
TGnuPlot::PlotValMomH(MomH, OutFNm, "", "size of the cluster, k", "phi(k)", gpsLog, gpwLines, true, true,true,true);
printf(" minSz: %d, miny %g\t%d\n", MinSz, MaxMinY, Cnt);
}
| void TNcpGraphsBase::PlotBestWhisker | ( | const TStr & | OutFNm, |
| const bool & | VsGraphN = false |
||
| ) |
Definition at line 1217 of file ncp.cpp.
{
TFltPrV GSzMinK, GSzMinY;
for (int i = 0; i < GSizeV.Len(); i++) {
if (WhiskerV[i].Val1()>0) {
const double X = VsGraphN ? (!ParamValV.Empty()?ParamValV[i]():i+1) : GSizeV[i].Val1();
GSzMinK.Add(TFltPr(X, WhiskerV[i].Val1()));
GSzMinY.Add(TFltPr(X, WhiskerV[i].Val2()));
}
}
GSzMinK.Sort(); GSzMinY.Sort();
const TStr XLabel = VsGraphN ? (!ParamValV.Empty()?"parameter value":"network number") : "network size";
TGnuPlot::PlotValV(GSzMinK, TStr("bestW-")+OutFNm, "Network", XLabel, "size of best whisker", VsGraphN?gpsLog10Y:gpsLog, false, gpwLinesPoints);
TGnuPlot::PlotValV(GSzMinY, TStr("condAtBestW-")+OutFNm, "Network", XLabel, "conductance of best whisker", VsGraphN?gpsLog10Y:gpsLog, false, gpwLinesPoints);
}
| void TNcpGraphsBase::PlotDataset | ( | const TStr & | InFNmWc, |
| const TStr & | OutFNm, | ||
| const bool & | ImposeNcp = false, |
||
| const bool & | VsGraphN = false |
||
| ) | [static] |
Definition at line 1281 of file ncp.cpp.
{
TNcpGraphsBase NcpBs(InFNmWc);
//NcpBs.Save(TFOut(OutFNm+".NcpBase"));
//TNcpGraphsBase NcpBs(TFIn(OutFNm+".NcpBase"));
if (ImposeNcp) {
NcpBs.Impose(OutFNm+"5R", 5, false); NcpBs.Impose(OutFNm+"5S", 5, true);
NcpBs.Impose(OutFNm+"R", 10, false); NcpBs.Impose(OutFNm+"S", 10, true);
}
NcpBs.PlotNcpMin(OutFNm, VsGraphN);
//NcpBs.SaveTxtNcpMin(OutFNm, VsGraphN);
NcpBs.PlotRewNcpMin(OutFNm, VsGraphN);
NcpBs.PlotBestWhisker(OutFNm, VsGraphN);
NcpBs.PlotRewBestWhisker(OutFNm, VsGraphN);
//NcpBs.PlotAvgNcp(OutFNm+"AvgNcp", NcpBs.NcpV, 1, 1);
//NcpBs.PlotAvgNcp(OutFNm+"AvgRewNcp", NcpBs.RewNcpV, 1, 1);
/*NcpBs.PlotAvgNcp(OutFNm+"AvgNcp2", NcpBs.NcpV, 100, 0.1);
NcpBs.PlotAvgNcp(OutFNm+"AvgNcp3", NcpBs.NcpV, 100, 0.01);
NcpBs.PlotAvgNcp(OutFNm+"AvgNcp4", NcpBs.NcpV, 100, 0.001);
NcpBs.PlotAvgNcp(OutFNm+"AvgNcp5", NcpBs.NcpV, 100, 0.0001);
NcpBs.PlotAvgNcp(OutFNm+"RewNcp1", NcpBs.RewNcpV, 1000, 1);
NcpBs.PlotAvgNcp(OutFNm+"RewNcp2", NcpBs.RewNcpV, 100, 0.1);
NcpBs.PlotAvgNcp(OutFNm+"RewNcp3", NcpBs.RewNcpV, 100, 0.01);
NcpBs.PlotAvgNcp(OutFNm+"RewNcp4", NcpBs.RewNcpV, 100, 0.001);
NcpBs.PlotAvgNcp(OutFNm+"RewNcp5", NcpBs.RewNcpV, 100, 0.0001);
NcpBs.PlotAvgNcp(OutFNm+"WhiskNcp1", NcpBs.WhiskNcpV, 1000, 1);
NcpBs.PlotAvgNcp(OutFNm+"WhiskNcp2", NcpBs.WhiskNcpV, 100, 0.1);
NcpBs.PlotAvgNcp(OutFNm+"WhiskNcp3", NcpBs.WhiskNcpV, 100, 0.01);
NcpBs.PlotAvgNcp(OutFNm+"WhiskNcp4", NcpBs.WhiskNcpV, 100, 0.001);
NcpBs.PlotAvgNcp(OutFNm+"WhiskNcp5", NcpBs.WhiskNcpV, 100, 0.0001);
NcpBs.PlotAvgNcp(OutFNm+"WhiskNcp6", NcpBs.WhiskNcpV, 100, 0.00004);
NcpBs.PlotAvgNcp(OutFNm+"WhiskNcp7", NcpBs.WhiskNcpV, 100, 0.00005);*/
//NcpBs.SaveTxt(OutFNm+"bestK.txt");
}
| void TNcpGraphsBase::PlotNcpMin | ( | const TStr & | OutFNm, |
| const bool & | VsGraphN = false |
||
| ) |
Definition at line 1173 of file ncp.cpp.
{
TFltPrV GSzMinK, GSzMinY;
for (int i = 0; i < NcpV.Len(); i++) {
const TFltPr XYAtMinY = GetXYAtMinY(NcpV[i], GSizeV[i].Val1);
const double X = VsGraphN ? (!ParamValV.Empty()?ParamValV[i]():i+1) : GSizeV[i].Val1();
GSzMinK.Add(TFltPr(X, XYAtMinY.Val1));
GSzMinY.Add(TFltPr(X, XYAtMinY.Val2));
}
GSzMinK.Sort(); GSzMinY.Sort();
const TStr XLabel = VsGraphN ? (!ParamValV.Empty()?"parameter value":"network number") : "network size";
TGnuPlot::PlotValV(GSzMinK, TStr("bestK-")+OutFNm, "Network", XLabel, "size of best cluster", VsGraphN?gpsLog10Y:gpsLog, false, gpwLinesPoints);
TGnuPlot::PlotValV(GSzMinY, TStr("condAtBestK-")+OutFNm, "Network", XLabel, "conductance of best cluster", VsGraphN?gpsLog10Y:gpsLog, false, gpwLinesPoints);
}
| void TNcpGraphsBase::PlotRewBestWhisker | ( | const TStr & | OutFNm, |
| const bool & | VsGraphN = false |
||
| ) |
Definition at line 1232 of file ncp.cpp.
{
TFltPrV GSzMinK, GSzMinY;
for (int i = 0; i < GSizeV.Len(); i++) {
if (WhiskerV[i].Val1()>0) {
const double X = VsGraphN ? (!ParamValV.Empty()?ParamValV[i]():i+1) : GSizeV[i].Val1();
GSzMinK.Add(TFltPr(X, RewWhiskerV[i].Val1()));
GSzMinY.Add(TFltPr(X, RewWhiskerV[i].Val2()));
}
}
GSzMinK.Sort(); GSzMinY.Sort();
const TStr XLabel = VsGraphN ? (!ParamValV.Empty()?"parameter value":"network number") : "network size";
TGnuPlot::PlotValV(GSzMinK, TStr("bestWR-")+OutFNm, "Rewired network", XLabel, "size of best rewired whisker", VsGraphN?gpsLog10Y:gpsLog, false, gpwLinesPoints);
TGnuPlot::PlotValV(GSzMinY, TStr("condAtBestWR-")+OutFNm, "Rewired network", XLabel, "conductance of best rewired whisker", VsGraphN?gpsLog10Y:gpsLog, false, gpwLinesPoints);
}
| void TNcpGraphsBase::PlotRewNcpMin | ( | const TStr & | OutFNm, |
| const bool & | VsGraphN = false |
||
| ) |
Definition at line 1203 of file ncp.cpp.
{
TFltPrV GSzMinK, GSzMinY;
for (int i = 0; i < NcpV.Len(); i++) {
const TFltPr XYAtMinY = GetXYAtMinY(RewNcpV[i], GSizeV[i].Val1);
const double X = VsGraphN ? (!ParamValV.Empty()?ParamValV[i]():i+1) : GSizeV[i].Val1();
GSzMinK.Add(TFltPr(X, XYAtMinY.Val1));
GSzMinY.Add(TFltPr(X, XYAtMinY.Val2));
}
GSzMinK.Sort(); GSzMinY.Sort();
const TStr XLabel = VsGraphN ? (!ParamValV.Empty()?"parameter value":"network number") : "network size";
TGnuPlot::PlotValV(GSzMinK, TStr("bestR-")+OutFNm, "Rewired network", XLabel, "size of best cluster", VsGraphN?gpsLog10Y:gpsLog, false, gpwLinesPoints);
TGnuPlot::PlotValV(GSzMinY, TStr("condAtBestR-")+OutFNm, "Rewired network", XLabel, "conductance of best cluster", VsGraphN?gpsLog10Y:gpsLog, false, gpwLinesPoints);
}
| void TNcpGraphsBase::Save | ( | TSOut & | SOut | ) | const |
| void TNcpGraphsBase::SaveTxt | ( | const TStr & | OutFNm | ) |
Definition at line 1267 of file ncp.cpp.
{
FILE *F=fopen(OutFNm.CStr(), "wt");
fprintf(F, "#Nodes\tEdges\tBestK\tPhi(BestK)\tMaxWhiskN\tPhi(MaxWhisk)\tGraph\n");
for (int i = 0; i < NcpV.Len(); i++) {
const TFltPrV& Ncp = NcpV[i];
double MinX=1, MinY=1;
for (int k = 0; k < Ncp.Len(); k++){
if (Ncp[k].Val2<MinY) { MinX=Ncp[k].Val1; MinY=Ncp[k].Val2; } }
fprintf(F, "%d\t%d\t%d\t%f\t%d\t%f\t%s\n", GSizeV[i].Val1(), GSizeV[i].Val2(),
int(MinX), MinY, int(WhiskerV[i].Val1), WhiskerV[i].Val2(), GNmV[i].CStr());
}
fclose(F);
}
| void TNcpGraphsBase::SaveTxtNcpMin | ( | const TStr & | OutFNm, |
| const bool & | VsGraphN = false |
||
| ) |
Definition at line 1187 of file ncp.cpp.
{
TVec<TQuad<TInt, TInt, TFlt, TStr> > GSzMinK;
for (int i = 0; i < NcpV.Len(); i++) {
const TFltPr XYAtMinY = GetXYAtMinY(NcpV[i], GSizeV[i].Val1);
const double X = VsGraphN ? (!ParamValV.Empty()?ParamValV[i]():i+1) : GSizeV[i].Val1();
GSzMinK.Add(TQuad<TInt, TInt, TFlt, TStr>((int)X, (int)XYAtMinY.Val1(), XYAtMinY.Val2, GNmV[i]));
}
GSzMinK.Sort();
FILE *F = fopen(TStr::Fmt("bestK-%s.txt", OutFNm.CStr()).CStr(), "wt");
fprintf(F, "#nodes\tbestK\tcondAtBestK\tgraph name\n");
for (int i = 0; i < GSzMinK.Len(); i++) {
fprintf(F, "%d\t%d\t%f\t%s\n", GSzMinK[i].Val1(), GSzMinK[i].Val2(), GSzMinK[i].Val3(), GSzMinK[i].Val4.CStr());
}
fclose(F);
}
TStrV TNcpGraphsBase::GNmV [private] |
TIntPrV TNcpGraphsBase::GSizeV [private] |
TVec<TFltPrV> TNcpGraphsBase::NcpV [private] |
TFltV TNcpGraphsBase::ParamValV [private] |
TVec<TFltPrV> TNcpGraphsBase::RewNcpV [private] |
TFltPrV TNcpGraphsBase::RewWhiskerV [private] |
TFltPrV TNcpGraphsBase::WhiskerV [private] |
TVec<TFltPrV> TNcpGraphsBase::WhiskNcpV [private] |