SNAP Library 2.2, User Reference  2014-03-11 19:15:55
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TCodaAnalyzer Class Reference

#include <agmdirected.h>

List of all members.

Public Member Functions

 TCodaAnalyzer ()
 TCodaAnalyzer (TCoda &Coda, const double MemThres=-1.0)
void GetAllCmtyVV (TVec< TIntV > &CmtyVV, const int MinSz)
double GetFrac2Mode (const double Thres2Mode=0.2, const int MinSzEach=2)
void Summary (const int TopK=10, const double Thres2Mode=0.2)
int GetNumComs ()
void GetCmtyVAll (TIntV &CmtyVAll, const int CID)
 save bipartite community affiliation into gexf file
PNGraph Net2ModeCommunities (const double MaxJac, const double JacEdge, const bool GetWcc=true)
void Dump2ModeCommunities (const TStr &OutFNm, const double MaxJac, const TIntStrH &NIDNameH)
void Draw2ModeCommunity (const int CID, const TStr &OutFNm, const TIntStrH &NIDNameH, const THash< TInt, TIntTr > &NIDColorH)

Public Attributes

PNGraph G
TVec< TIntFltHInCmtyValHV
TVec< TIntFltHOutCmtyValHV
TVec< TIntFltHInOutCmtyValHV

Detailed Description

Definition at line 198 of file agmdirected.h.


Constructor & Destructor Documentation

Definition at line 204 of file agmdirected.h.

{ G = TNGraph::New(); }
TCodaAnalyzer::TCodaAnalyzer ( TCoda Coda,
const double  MemThres = -1.0 
) [inline]

Definition at line 205 of file agmdirected.h.

                                                           {
    G = Coda.GetGraphRawNID();
    printf("graph copied (%d nodes %d edges)\n", G->GetNodes(), G->GetEdges());
    TIntV CIdV;
    Coda.GetTopCIDs(CIdV, Coda.GetNumComs());
    double Delta = MemThres == -1.0 ? sqrt(Coda.PNoCom): MemThres;
    for (int c = 0; c < CIdV.Len(); c++) {
      int CID = CIdV[c];
      TIntFltH InMemH, OutMemH, InOutMemH;
      Coda.GetNIDValH(InOutMemH, OutMemH, InMemH, CID, Delta);
      InCmtyValHV.Add(InMemH);
      OutCmtyValHV.Add(OutMemH);
      InOutCmtyValHV.Add(InOutMemH);
    }
    printf("Communities copied (%d communities)\n", InCmtyValHV.Len());
  }

Member Function Documentation

void TCodaAnalyzer::Draw2ModeCommunity ( const int  CID,
const TStr OutFNm,
const TIntStrH NIDNameH,
const THash< TInt, TIntTr > &  NIDColorH 
) [inline]

Plot bipartite graph for the 2-mode community

Definition at line 355 of file agmdirected.h.

                                                                                                                             {
    TIntV CmtyVIn, CmtyVOut, CmtyVAll;
    InCmtyValHV[CID].GetKeyV(CmtyVIn);
    OutCmtyValHV[CID].GetKeyV(CmtyVOut);
    GetCmtyVAll(CmtyVAll, CID);

    //adjust for the nodes who belong to both cmtyvin and cmtyvout
    for (int u = 0; u < InOutCmtyValHV[CID].Len(); u++) {
      int UID = InOutCmtyValHV[CID].GetKey(u);
      if (CmtyVIn.Len() >= CmtyVOut.Len()) {
        CmtyVIn.DelIfIn(UID); 
      } else {
        CmtyVOut.DelIfIn(UID); 
      }
    }

    PNGraph SG = TSnap::GetSubGraph(G, CmtyVAll);
    if (CmtyVAll.Len() == 0) { return; }
    double OXMin = 0.1, YMin = 0.1, OXMax = 2500.00, YMax = 1000.0, IXMin = 0.1, IXMax = 2500.00;
    double OStep = (OXMax - OXMin) / (double) CmtyVOut.Len(), IStep = (IXMax - IXMin) / (double) CmtyVIn.Len();

    FILE* F = fopen(OutFNm.CStr(), "wt");
    fprintf(F, "<?xml version='1.0' encoding='UTF-8'?>\n");
    fprintf(F, "<gexf xmlns='http://www.gexf.net/1.2draft' xmlns:viz='http://www.gexf.net/1.1draft/viz' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd' version='1.2'>\n");
    fprintf(F, "\t<graph mode='static' defaultedgetype='directed'>\n");
    fprintf(F, "\t\t<nodes>\n");
    for (int c = 0; c < CmtyVOut.Len(); c++) {
      int NID = CmtyVOut[c];
      double XPos = c * OStep + OXMin;
      TStr Label = NIDNameH.IsKey(NID)? NIDNameH.GetDat(NID): "";
      Label.ChangeChAll('<', ' ');
      Label.ChangeChAll('>', ' ');
      Label.ChangeChAll('&', ' ');
      Label.ChangeChAll('\'', ' ');
      TIntTr Color = NIDColorH.IsKey(NID)? NIDColorH.GetDat(NID) : TIntTr(120, 120, 120);
      fprintf(F, "\t\t\t<node id='%d' label='%s'>\n", NID, Label.CStr());
      fprintf(F, "\t\t\t\t<viz:color r='%d' g='%d' b='%d'/>\n", Color.Val1.Val, Color.Val2.Val, Color.Val3.Val);
      fprintf(F, "\t\t\t\t<viz:size value='4.0'/>\n");
      fprintf(F, "\t\t\t\t<viz:shape value='square'/>\n");
      fprintf(F, "\t\t\t\t<viz:position x='%f' y='%f' z='0.0'/>\n", XPos, YMax); 
      fprintf(F, "\t\t\t</node>\n");
    }

    for (int u = 0; u < CmtyVIn.Len(); u++) {
      int NID = CmtyVIn[u];
      TStr Label = NIDNameH.IsKey(NID)? NIDNameH.GetDat(NID): "";
      Label.ChangeChAll('<', ' ');
      Label.ChangeChAll('>', ' ');
      Label.ChangeChAll('&', ' ');
      Label.ChangeChAll('\'', ' ');
      double XPos = IXMin + u * IStep;
      TIntTr Color = NIDColorH.IsKey(NID)? NIDColorH.GetDat(NID) : TIntTr(120, 120, 120);
      double Alpha = 1.0;
      fprintf(F, "\t\t\t<node id='%d' label='%s'>\n", NID, Label.CStr());
      fprintf(F, "\t\t\t\t<viz:color r='%d' g='%d' b='%d' a='%.1f'/>\n", Color.Val1.Val, Color.Val2.Val, Color.Val3.Val, Alpha);
      fprintf(F, "\t\t\t\t<viz:size value='4.0'/>\n");
      fprintf(F, "\t\t\t\t<viz:shape value='square'/>\n");
      fprintf(F, "\t\t\t\t<viz:position x='%f' y='%f' z='0.0'/>\n", XPos, YMin); 
      fprintf(F, "\t\t\t</node>\n");
    }
    fprintf(F, "\t\t</nodes>\n");
    //plot edges
    int EID = 0;
    fprintf(F, "\t\t<edges>\n");
    for (TNGraph::TNodeI NI = SG->BegNI(); NI < SG->EndNI(); NI++) {
      if (NI.GetOutDeg() == 0 && NI.GetInDeg() == 0  ) { continue; }
      for (int e = 0; e < NI.GetOutDeg(); e++) {
        fprintf(F, "\t\t\t<edge id='%d' source='%d' target='%d'/>\n", EID++, NI.GetId(), NI.GetOutNId(e));
      }
    }
    fprintf(F, "\t\t</edges>\n");
    fprintf(F, "\t</graph>\n");
    fprintf(F, "</gexf>\n");
    fclose(F);
  }
void TCodaAnalyzer::Dump2ModeCommunities ( const TStr OutFNm,
const double  MaxJac,
const TIntStrH NIDNameH 
) [inline]

Definition at line 317 of file agmdirected.h.

                                                                                               {
    FILE* F = fopen(OutFNm.CStr(), "wt");
    for (int c = 0; c < InCmtyValHV.Len(); c++) {
      double Jacc = (double) InOutCmtyValHV[c].Len() / (double) (InCmtyValHV[c].Len() + OutCmtyValHV[c].Len() - InOutCmtyValHV[c].Len());
      if (Jacc > MaxJac) { continue; }
      TIntV CmtyVIn, CmtyVOut, CmtyVAll;
      InCmtyValHV[c].GetKeyV(CmtyVIn);
      OutCmtyValHV[c].GetKeyV(CmtyVOut);
      GetCmtyVAll(CmtyVAll, c);
      //adjust for the nodes who belong to both cmtyvin and cmtyvout
      for (int u = 0; u < InOutCmtyValHV[c].Len(); u++) {
        int UID = InOutCmtyValHV[c].GetKey(u);
        if (CmtyVIn.Len() >= CmtyVOut.Len()) {
          CmtyVIn.DelIfIn(UID); 
        } else {
          CmtyVOut.DelIfIn(UID); 
        }
      }
      if (CmtyVAll.Len() == 0) { continue; }
      fprintf(F, "Com %d\n", c);
      for (int u = 0; u < CmtyVOut.Len(); u++) {
        int NID = CmtyVOut[u];
        TStr Label = NIDNameH.IsKey(NID)? NIDNameH.GetDat(NID): TStr::Fmt("Concept %d", NID);
        fprintf(F, "%s:%f\n", Label.CStr(), OutCmtyValHV[c].GetDat(NID).Val);
      }
      fprintf(F, "||==>||\n");
      for (int u = 0; u < CmtyVIn.Len(); u++) {
        int NID = CmtyVIn[u];
        TStr Label = NIDNameH.IsKey(NID)? NIDNameH.GetDat(NID): TStr::Fmt("Concept %d", NID);
        fprintf(F, "%s:%f\n", Label.CStr(), InCmtyValHV[c].GetDat(NID).Val);
      }
      fprintf(F, "\n");
    }
    fclose(F);
  }
void TCodaAnalyzer::GetAllCmtyVV ( TVec< TIntV > &  CmtyVV,
const int  MinSz 
) [inline]

Definition at line 221 of file agmdirected.h.

                                                          {
    for (int c = 0; c < InCmtyValHV.Len(); c++) {
      TIntV CmtyVIn, CmtyVOut, CmtyVInOut;
      if (InCmtyValHV[c].Len() < MinSz || OutCmtyValHV[c].Len() < MinSz) { continue; }
      InOutCmtyValHV[c].GetKeyV(CmtyVInOut);
      InCmtyValHV[c].GetKeyV(CmtyVIn);
      OutCmtyValHV[c].GetKeyV(CmtyVOut);
      CmtyVV.Add(CmtyVInOut);
      CmtyVV.Add(CmtyVOut);
      CmtyVV.Add(CmtyVIn);
    }
  }
void TCodaAnalyzer::GetCmtyVAll ( TIntV CmtyVAll,
const int  CID 
) [inline]

save bipartite community affiliation into gexf file

Definition at line 263 of file agmdirected.h.

                                                   {
    TIntV CmtyVIn, CmtyVOut;
    InCmtyValHV[CID].GetKeyV(CmtyVIn);
    OutCmtyValHV[CID].GetKeyV(CmtyVOut);
    CmtyVIn.Sort();
    CmtyVOut.Sort();
    CmtyVAll.Gen(CmtyVIn.Len() + CmtyVOut.Len(), 0);
    CmtyVIn.Union(CmtyVOut, CmtyVAll);
  }
double TCodaAnalyzer::GetFrac2Mode ( const double  Thres2Mode = 0.2,
const int  MinSzEach = 2 
) [inline]

Definition at line 234 of file agmdirected.h.

                                                                              {
    int Cnt2Mode = 0;
    int CntAll = 0;
    for (int c = 0; c < InCmtyValHV.Len(); c++) {
      double Jacc = (double) InOutCmtyValHV[c].Len() / (double) (InCmtyValHV[c].Len() + OutCmtyValHV[c].Len() - InOutCmtyValHV[c].Len());
      if (InCmtyValHV[c].Len() < MinSzEach || OutCmtyValHV[c].Len() < MinSzEach) { continue; }
      if (Jacc <= Thres2Mode) { Cnt2Mode++; }
      CntAll++;
    }
    return (double) Cnt2Mode / (double) CntAll;
  }
int TCodaAnalyzer::GetNumComs ( ) [inline]

Definition at line 260 of file agmdirected.h.

{ return InCmtyValHV.Len(); }
PNGraph TCodaAnalyzer::Net2ModeCommunities ( const double  MaxJac,
const double  JacEdge,
const bool  GetWcc = true 
) [inline]

Definition at line 273 of file agmdirected.h.

                                                                                                   {
    //if In(A) is similar to Out(B), create an edge A->B between 2 communities A, B
    int Coms = InCmtyValHV.Len();
    PNGraph ComG = TNGraph::New(Coms, -1);
    for (int c = 0; c < InCmtyValHV.Len(); c++) {
      double Jacc = (double) InOutCmtyValHV[c].Len() / (double) (InCmtyValHV[c].Len() + OutCmtyValHV[c].Len() - InOutCmtyValHV[c].Len());
      if (Jacc > MaxJac) { continue; }
      ComG->AddNode(c);
    }
    TVec<TIntSet> CmtySVIn, CmtySVOut;
    for (int c = 0; c < Coms; c++) {
      TIntV CmtyVIn, CmtyVOut;
      InCmtyValHV[c].GetKeyV(CmtyVIn);
      OutCmtyValHV[c].GetKeyV(CmtyVOut);
      TIntSet CmtySIn(CmtyVIn), CmtySOut(CmtyVOut);
      CmtySVIn.Add(CmtySIn);
      CmtySVOut.Add(CmtySOut);
    }
    for (int c1 = 0; c1 < Coms; c1++) {
      if (! ComG->IsNode(c1)) { continue; }
      for (int c2 = 0; c2 < Coms; c2++) {
        if (! ComG->IsNode(c2)) { continue; }
        int IntC1C2 = TAGMUtil::Intersection(CmtySVIn[c1], CmtySVOut[c2]);
        double Jac = (double) IntC1C2 / (CmtySVIn[c1].Len() + CmtySVOut[c2].Len() - IntC1C2);
        if (Jac >= JacEdge) {
          ComG->AddEdge(c1, c2);
        }
      }
    }
    //PNGraph Wcc = TSnap::GetMxWcc(ComG);
    TIntV NIDV;
    ComG->GetNIdV(NIDV);
    for (int u = 0; u < NIDV.Len(); u++) {
      int NID = NIDV[u];
      TNGraph::TNodeI NI = ComG->GetNI(NID);
      if (NI.GetDeg() == 0) { ComG->DelNode(NID); }
      if (NI.GetInDeg() == 1 && NI.GetOutDeg() == 1 && NI.GetOutNId(0) == NID) { ComG->DelNode(NID); }
    }
    printf("Community graph made (Jaccard similarity for edges: %f, %d nodes, %d edges)\n", JacEdge, ComG->GetNodes(), ComG->GetEdges());
    return ComG;
  }
void TCodaAnalyzer::Summary ( const int  TopK = 10,
const double  Thres2Mode = 0.2 
) [inline]

Definition at line 246 of file agmdirected.h.

                                                                   {
    int Cnt2Mode = 0;
    double SumJacc = 0.0;
    for (int c = 0; c < InCmtyValHV.Len(); c++) {
      double Jacc = (double) InOutCmtyValHV[c].Len() / (double) (InCmtyValHV[c].Len() + OutCmtyValHV[c].Len() - InOutCmtyValHV[c].Len());
      if (Jacc <= Thres2Mode) { Cnt2Mode++; }
      SumJacc += Jacc;
      if (c < TopK) {
        printf("Cmty %d: InOut: %d, In:%d, Out:%d, Jacc;%.3f\n", c, InCmtyValHV[c].Len(), InCmtyValHV[c].Len(), OutCmtyValHV[c].Len(), Jacc);
      }
    }
    double AvgJacc = SumJacc / (double) InCmtyValHV.Len();
    printf("Average jaccard similarity = %.3f. (%d / %d communities are 2-mode)\n", AvgJacc, Cnt2Mode, InCmtyValHV.Len());
  }

Member Data Documentation

Definition at line 200 of file agmdirected.h.


The documentation for this class was generated from the following file: