// parse 28/03/03 and 28-MAY-03 formats TSecTm TSecTm::GetDtTmFromDmyStr(const TStr& DmyStr){ int DmyStrLen=DmyStr.Len(); // day TChA ChA; int ChN=0; while ((ChN<DmyStrLen)&&(DmyStr[ChN]!='/')&&(DmyStr[ChN]!='-')){ ChA+=DmyStr[ChN]; ChN++;} TStr DayStr=ChA; // month ChA.Clr(); ChN++; while ((ChN<DmyStrLen)&&(DmyStr[ChN]!='/')&&(DmyStr[ChN]!='-')){ ChA+=DmyStr[ChN]; ChN++;} TStr MonthStr=ChA; // year ChA.Clr(); ChN++; while (ChN<DmyStrLen){ ChA+=DmyStr[ChN]; ChN++;} TStr YearStr=ChA; // transform to numbers int DayN=DayStr.GetInt(-1); int MonthN=MonthStr.GetInt(-1); int YearN=YearStr.GetInt(-1); if (MonthN == -1){ MonthN = TTmInfo::GetMonthN(MonthStr.ToCap()); } if ((DayN==-1)||(MonthN==-1)||(YearN==-1)){ return TSecTm(); } else { if (YearN<1000){ if (YearN<70){YearN+=2000;} else {YearN+=1900;}} // construct the date return GetDtTm(YearN, MonthN, DayN); } return TSecTm(); }
int TDateWnd::GetFtr(const TTm& Val) const { EAssert(InitP); const uint ValUnit = TSecTm(Val).GetInUnits(TmUnit); if (ValUnit < StartUnit) { return 0; } if (ValUnit > EndUnit) { return (int)(EndUnit - StartUnit); } return (int)(ValUnit - StartUnit); }
TSecTm::TSecTm(const PXmlTok& XmlTok) { const int Year = XmlTok->GetIntArgVal("Year"); const int Month = XmlTok->GetIntArgVal("Month"); const int Day = XmlTok->GetIntArgVal("Day"); const int Hour = XmlTok->GetIntArgVal("Hour"); const int Min = XmlTok->GetIntArgVal("Min"); const int Sec = XmlTok->GetIntArgVal("Sec"); AbsSecs = TSecTm(Year, Month, Day, Hour, Min, Sec).GetAbsSecs(); }
bool TDateWnd::Update(const TTm& Val) { // if first time, use it to initialize if (!InitP) { StartUnit = TSecTm(Val).GetInUnits(TmUnit); EndUnit = TSecTm(Val).GetInUnits(TmUnit); InitP = true; return true; } // check if we moved start or end boundary const uint ValUnit = TSecTm(Val).GetInUnits(TmUnit); if (StartUnit > ValUnit) { StartUnit = ValUnit; return true; } if (EndUnit < ValUnit) { EndUnit = ValUnit; return true; } // nope, we are fine return false; }
TFfGGen::TStopReason TFfGGen::GenGraph(const int& GraphNodes, PGStatVec& EvolStat, const bool& FloodStop) { int GrowthStatNodes = 100; Graph = PNGraph::New(); AddNodes(StartNodes); TStopReason SR = srUndef; while (Graph->GetNodes() < GraphNodes) { SR = AddNodes(GrowthStatNodes, FloodStop); if (SR != srOk) { return SR; } EvolStat->Add(Graph, TSecTm(Graph->GetNodes())); GrowthStatNodes = int(1.5*GrowthStatNodes); } return SR; }
TSecTm TSecTm::GetCurTm(){ const time_t TmSec = time(NULL); struct tm LocTm; uint AbsSec = TUInt::Mx; #if defined(GLib_MSN) localtime_s(&LocTm, &TmSec); #elif defined(GLib_BCB) LocTm = *localtime(&TmSec); #else LocTm = *localtime(&TmSec); #endif IAssert(TSecTm::GetTmSec(LocTm, AbsSec)); return TSecTm(AbsSec); }
// parse: // 10:16, 16 Sep 2004 // 10:20, 2004 Sep 16 // 2005-07-07 20:30:35 // 23:24:07, 2005-07-10 // 9 July 2005 14:38 // 21:16, July 9, 2005 // 06:02, 10 July 2005 bool TStrUtil::GetTmFromStr(const char* TmStr, TSecTm& Tm) { static TStrV MonthV1, MonthV2; if (MonthV1.Empty()) { TStr("january|february|march|april|may|june|july|august|september|october|november|december").SplitOnAllCh('|', MonthV1); TStr("jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec").SplitOnAllCh('|', MonthV2); } TChA Tmp(TmStr); Tmp.ToLc(); TVec<char *> WrdV; const char* End = Tmp.CStr()+Tmp.Len(); int Col = -1, Cols=0; for (char *b = Tmp.CStr(); b <End; ) { WrdV.Add(b); while (*b && ! (*b==' ' || *b=='-' || *b==':' || *b==',')) { b++; } if (*b==':') { if(Col==-1) { Col=WrdV.Len(); } Cols++; } *b=0; b++; while (*b && (*b==' ' || *b=='-' || *b==':' || *b==',')) { b++; } } if (Cols == 2) { if (Col+1 >= WrdV.Len()) { return false; } WrdV.Del(Col+1); } if (Col<1) { return false; } const int Hr = atoi(WrdV[Col-1]); const int Min = atoi(WrdV[Col]); WrdV.Del(Col); WrdV.Del(Col-1); if (WrdV.Len() != 3) { return false; } int y=0,m=1,d=2, Mon=-1; if (TCh::IsAlpha(WrdV[0][0])) { y=2; m=0; d=1; } else if (TCh::IsAlpha(WrdV[1][0])) { y=2; m=1; d=0; } else if (TCh::IsAlpha(WrdV[2][0])) { y=0; m=2; d=1; } else { y=0; m=1; d=2; Mon = atoi(WrdV[m]); } int Day = atoi(WrdV[d]); if (Mon <= 0) { Mon = MonthV1.SearchForw(WrdV[m])+1; } if (Mon <= 0) { Mon = MonthV2.SearchForw(WrdV[m])+1; } if (Mon == 0) { return false; } int Year = atoi(WrdV[y]); if (Day > Year) { ::Swap(Day, Year); } //printf("%d-%02d-%02d %02d:%02d\n", Year, Mon, Day, Hr, Min); Tm = TSecTm(Year, Mon, Day, Hr, Min, 0); return true; }
uint TSecTm::GetInUnits(const TTmUnit& TmUnit) const { static const int DayZero = TJulianDate::GetJulianDateN(1, 1, 1970); if (TmUnit == tmu1Sec) { return AbsSecs; } struct tm Time; IAssert(IsDef() && GetTmStruct(AbsSecs(), Time)); switch (TmUnit) { case tmu1Min : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, Time.tm_hour, Time.tm_min, 0).GetAbsSecs()/60; case tmu10Min : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, Time.tm_hour, 10*(Time.tm_min/10), 0).GetAbsSecs()/(10*60); case tmu15Min : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, Time.tm_hour, 15*(Time.tm_min/15), 0).GetAbsSecs()/(15*60); case tmu30Min : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, Time.tm_hour, 30*(Time.tm_min/30), 0).GetAbsSecs()/(30*60); case tmu1Hour : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, Time.tm_hour, 0, 0).GetAbsSecs()/3600; case tmu2Hour : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, 2*(Time.tm_hour/2), 0, 0).GetAbsSecs()/(2*3600); case tmu4Hour : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, 4*(Time.tm_hour/4), 0, 0).GetAbsSecs()/(4*3600); case tmu6Hour : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, 6*(Time.tm_hour/6), 0, 0).GetAbsSecs()/(6*3600); case tmu12Hour : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, 12*(Time.tm_hour/12), 0, 0).GetAbsSecs()/(12*3600); case tmuDay : return TJulianDate::GetJulianDateN(Time.tm_mday, Time.tm_mon+1, 1900+Time.tm_year) - DayZero; case tmuWeek : return (TJulianDate::GetJulianDateN(Time.tm_mday, Time.tm_mon+1, 1900+Time.tm_year)-DayZero)/7; case tmuMonth : return 12*(Time.tm_year-70)+Time.tm_mon+1; case tmuYear : return Time.tm_year+1900; default : Fail; } return TUInt::Mx; }
// Parse strings of the form 2006-08-28 14:11:16 or 14:11:16 08/28/2008 // Non-numeric characters act as separators (there can be many consecutive separating characters) // Variables give indexes of the date fields TSecTm TSecTm::GetDtTmFromStr(const TChA& YmdHmsPmStr, const int& YearId, const int& MonId, const int& DayId, const int& HourId, const int& MinId, const int& SecId) { TChA Tmp = YmdHmsPmStr; TVec<char *> FldV; // get the sequences of numbers for (char *c = (char *) Tmp.CStr(); *c; c++) { if (TCh::IsNum(*c)) { FldV.Add(c); while (TCh::IsNum(*c)) { c++; } c--; } else { *c = 0; } } const int Y = atoi(FldV[YearId]); const int M = atoi(FldV[MonId]); const int D = atoi(FldV[DayId]); const int H = atoi(FldV[HourId]); const int m = atoi(FldV[MinId]); const int S = atoi(FldV[SecId]); IAssert(Y>0 && M>0 && D>0 && M<13 && D<32); IAssert(H>=0 && H<24 && m>=0 && m<60 && S>=0 && S<60); return TSecTm(Y,M,D,H,m,S); }
int main(int argc, char* argv[]) { // create a graph and save it { PNGraph Graph = TNGraph::New(); for (int i = 0; i < 10; i++) { Graph->AddNode(i); } for (int i = 0; i < 10; i++) { Graph->AddEdge(i, TInt::Rnd.GetUniDevInt(10)); } TSnap::SaveEdgeList(Graph, "graph.txt", "Edge list format"); } // load a graph PNGraph Graph; Graph = TSnap::LoadEdgeList<PNGraph>("graph.txt", 0, 1); // traverse nodes for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) { printf("NodeId: %d, InDegree: %d, OutDegree: %d\n", NI.GetId(), NI.GetInDeg(), NI.GetOutDeg()); printf("OutNodes: "); for (int e = 0; e < NI.GetOutDeg(); e++) { printf(" %d", NI.GetOutNId(e)); } printf("\nInNodes: "); for (int e = 0; e < NI.GetInDeg(); e++) { printf(" %d", NI.GetInNId(e)); } printf("\n\n"); } // graph statistic TSnap::PrintInfo(Graph, "Graph info"); PNGraph MxWcc = TSnap::GetMxWcc(Graph); TSnap::PrintInfo(MxWcc, "Largest Weakly connected component"); // random graph PNGraph RndGraph = TSnap::GenRndGnm<PNGraph>(100, 1000); TGStat GraphStat(RndGraph, TSecTm(1), TGStat::AllStat(), "Gnm graph"); GraphStat.PlotAll("RndGraph", "Random graph on 1000 nodes"); // Forest Fire graph { TFfGGen ForestFire(false, 1, 0.35, 0.30, 1.0, 0.0, 0.0); ForestFire.GenGraph(100); PNGraph FfGraph = ForestFire.GetGraph(); } // network TPt<TNodeEDatNet<TStr, TStr> > Net = TNodeEDatNet<TStr, TStr>::New(); Net->AddNode(0, "zero"); Net->AddNode(1, "one"); Net->AddEdge(0, 1, "zero to one"); return 0; }
int main(int argc, char* argv[]) { TExeTm ExeTm; THash< TStr , CascadeElementV > quotesFiltered; double* vol_me; uint period = 9 * 3600; // 9 days because of NIFTY paper printf("((((( Starting The Filtering Cascades CODE )))))\n"); try { Env = TEnv(argc, argv, TNotify::StdNotify); Env.PrepArgs(TStr::Fmt("\nFiltering Memes Cascades. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm())); // ---== Loading Data ==--- TZipIn ZquotesIn("QuotesPreprocessedData_NIFTY.rar"); ///("/agbs/cluster/oaskaris/Data_Preparing_Codes/RESULTS/QuotesPreprocessedData_NIFTY.rar"); THash< TStr , CascadeElementV > quotes; quotes.Load(ZquotesIn); printf("Loaded QuotesPreprocessedData_NIFTY has instances: %d\n\n\n",quotes.Len()); // NIFTY Method for Filtering by Peaks uint begin = TSecTm(2008,7,31,0,0,0).GetAbsSecs(); uint end = TSecTm(2009,10,1,0,0,0).GetAbsSecs(); TSecTmV memesTimes; int bins = (end - begin) / period; for(int c=0;c<quotes.Len();c++) { memesTimes.Clr(); for(int i=0;i<quotes[c].Len();i++) { memesTimes.Add(quotes[c][i].time); } vol_me = Tools::calculateHistOfCascade(memesTimes,begin,period,false); // calculating mean and standard deviation double mean = 0; for(int i=0;i<bins;i++) { mean += vol_me[i]; } mean /= bins; double std = 0; for(int i=0;i<bins;i++) { std += pow(vol_me[i]-mean , 2); } std = sqrt(std / (bins-1)); // peak definition by NIFTY: a point is a peak if its volume in 9 days binning is 1 standard deviation higher than the average frequency double maxVolume = mean + std; int peakCnt = 0; for(int i=0;i<bins;i++) { if(vol_me[i] > maxVolume) { peakCnt++; } } // if there is more than 5 peaks ignore this quote, since it is not a meme if(peakCnt > 5) { delete[] vol_me; continue; } quotesFiltered.AddDat(quotes.GetKey(c),quotes[c]); delete[] vol_me; } TZipOut mout("QuotesPreprocessedData_NIFTY_FINALFILTERED.rar"); quotesFiltered.Save(mout); printf("Saved QuotesPreprocessedData_NIFTY_FINALFILTERED has instances: %d\n\n\n",quotesFiltered.Len()); printf("\nThe Meme Filter for plotting had been done successfully.\n"); } catch(exception& ex) { printf("\nError1 happened, it was: %s\n\n",ex.what()); } catch(TPt<TExcept>& ex) { printf("\nError2 happened: %s\n\n",ex[0].GetStr().CStr()); } printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr()); return 0; }
TSecTm TTm::GetSecTmFromDateTimeInt(const uint& DateTimeInt) { if (DateTimeInt == 0) { return TSecTm(); } return TSecTm(DateTimeInt); }
TSecTm::TSecTm(const TTm& Tm): AbsSecs( TSecTm(Tm.GetYear(), Tm.GetMonth(), Tm.GetDay(), Tm.GetHour(), Tm.GetMin(), Tm.GetSec()).GetAbsSecs()) { }
TSecTm TSecTm::LoadTxt(TILx& Lx){ return TSecTm(Lx.GetInt()); }
TSecTm TSecTm::GetDtTm(const int& YearN, const int& MonthN, const int& DayN){ uint AbsSecs; TSecTm::GetTmSec(YearN, MonthN, DayN, 0, 0, 0, AbsSecs); return TSecTm(AbsSecs); }
TSecTm::TSecTm(const TTm& Tm): AbsSecs( TSecTm(Tm.GetYear(), Tm.GetMonth(), Tm.GetDay(), Tm.GetHour(), Tm.GetMin(), int(TMath::Round(Tm.GetSec()*1000+Tm.GetMSec()))).GetAbsSecs()){}
uint TTm::GetDateTimeInt(const int& Year, const int& Month, const int& Day, const int& Hour, const int& Min, const int& Sec) { return TSecTm(Year, Month, Day, Hour, Min, Sec).GetAbsSecs(); }
TSecTm TSecTm::Round(const TTmUnit& TmUnit) const { if (TmUnit == tmu1Sec) { return *this; } struct tm Time; IAssert(IsDef() && GetTmStruct(AbsSecs(), Time)); switch (TmUnit) { case tmu1Min : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, Time.tm_hour, Time.tm_min, 0); case tmu10Min : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, Time.tm_hour, 10*(Time.tm_min/10), 0); case tmu15Min : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, Time.tm_hour, 15*(Time.tm_min/15), 0); case tmu30Min : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, Time.tm_hour, 30*(Time.tm_min/30), 0); case tmu1Hour : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, Time.tm_hour, 0, 0); case tmu2Hour : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, 2*(Time.tm_hour/2), 0, 0); case tmu4Hour : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, 4*(Time.tm_hour/4), 0, 0); case tmu6Hour : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, 6*(Time.tm_hour/6), 0, 0); case tmu12Hour : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, 12*(Time.tm_hour/12), 0, 0); case tmuDay : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, Time.tm_mday, 0, 0, 0); case tmuMonth : return TSecTm(Time.tm_year+1900, Time.tm_mon+1, 1, 0, 0, 0); case tmuYear : return TSecTm(Time.tm_year+1900, 1, 1, 0, 0, 0); case tmuWeek : { int dd=1, mm=1, yy=1; // week starts on Thursday, since 1.1.1970 is Thursday const int Day = TJulianDate::GetJulianDateN(Time.tm_mday, Time.tm_mon+1, 1900+Time.tm_year); TJulianDate::GetCalendarDate(3+7*(Day/7), dd, mm, yy); return TSecTm(yy, mm, dd, 0, 0, 0); } default : Fail; } return TSecTm(); }