/
InOut.cpp
136 lines (119 loc) · 4.14 KB
/
InOut.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "stdafx.h"
#include "InOut.h"
void Error(const TStr& FuncName, const TStr& ErrorMsg){
printf("Error in %s: %s\n", FuncName.CStr(), ErrorMsg.CStr());
system("pause");
exit(1);
}
void ReadParameters(TStr settingsFN, vector<TStr>& out){
ifstream f;
f.open(settingsFN.CStr());
if (!f.is_open())
Error("ReadParameters", "File with settings (parameters.txt by default) is not found");
string insteadOfName = "1 ";
for (int i = 0; i < NFUNC; i++){
string s;
getline(f,s);
if ( s != FUNCNAMES[i])
Error("ReadParameters", "Wrong syntax in file of settings");
bool isComment = true;
while (isComment){
getline(f,s);
if (s.find_first_of("//")!=0)
isComment = false;
}
if (s == "default")
s = insteadOfName;
else s = insteadOfName + s;
TStr ts(s.c_str());
out.push_back(ts);
}
f.close();
}
void GetNEFromAccDistr(const TFltPrV& deg, int& nodes, int& edges){
double nodesD = deg[0].Val2.Val, edgesD = 0;
for (int i = 0; i < deg.Len(); i++){
if (i == deg.Len()-1)
edgesD += deg[i].Val1.Val * deg[i].Val2.Val;
else edgesD += deg[i].Val1.Val * (deg[i].Val2.Val - deg[i+1].Val2.Val);
}
nodes = static_cast<int>(nodesD);
edges = static_cast<int>(edgesD);
// edges /= 2; as Deg = inDeg + outDeg
}
void SaveDegree(const TFltPrV& deg, const TStr& n, bool isIn, bool isCum){
TFltPrV d(deg);
d.Sort();
int nodes, edges;
GetNEFromAccDistr(d, nodes, edges);
if (isCum){
d = TGUtil::GetCCdf(d);
}
TSnap::PlotDegDistr(d, nodes, edges, n, n, isCum, false, isIn);
}
// plot all points without binning
void PlotPoints(const TFltPrV& in, const TFltPrV& out, const TStr& name, const TStr& Plt){
if (Plt == "noncum" || Plt == "all"){
SaveDegree(in, name, true, false);
SaveDegree(out, name, false, false);
}
if (Plt == "cum" || Plt == "all"){
SaveDegree(in, name, true, true);
SaveDegree(out, name, false, true);
}
}
void PlotDegrees(const vector <TStr>& Parameters, const TFltPrV& In, const TFltPrV& Out, const TStr& Type){
const TStr& Name = Parameters[NAME];
const TStr& Plt = Parameters[PLT];
PlotPoints(In, Out, Type + Name, Plt);
}
void PrintMtx(const TKronMtx& FitMtxM, ofstream& TFile){
TFile << "Initiator matrix: ";
size_t Dim = FitMtxM.GetDim();
for (size_t i = 0; i < Dim; ++i)
for (size_t j = 0; j < Dim; ++j){
TFile << FitMtxM.At(i,j);
if (!(i == Dim-1 && j == Dim-1))
TFile << ";";
}
TFile << endl;
}
void GetParameters(const vector<TStr>& CommandLineArgs, vector<TStr>& Parameters){
Env = TEnv(CommandLineArgs[KRONTEST], TNotify::NullNotify);
// to plot
const TStr Plt = Env.GetIfArgPrefixStr("-plttype:", "model+kron", "Plotting of big model and/or its Kronecker product (model, kron, model+kron)");
// type of plots
const TStr PltType = Env.GetIfArgPrefixStr("-plt:", "all", "Type of plots (cum, noncum, all)");
for (size_t i = 0; i < CommandLineArgs.size(); i++)
Parameters.push_back(CommandLineArgs[i]);
Parameters.push_back(Plt); Parameters.push_back(PltType);
}
void ReadPNGraphFromFile(const TStr args, PNGraph& G){
Try
Env = TEnv(args, TNotify::StdNotify);
const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "../as20graph.txt", "Input graph file (single directed edge per line)");
// load graph
G = TSnap::LoadEdgeList<PNGraph>(InFNm, 0, 1);
Catch
}
ofstream OpenFile(const TStr& fileName)
{
Try
ofstream f(fileName.CStr());
if (f.is_open())
return f;
IAssert(1);
Catch
}
void GetMtxFromSepLine(const TStr& line, const TStr& separator, TFltV& matrix){
TStrV strVals;
line.SplitOnAllAnyCh(separator, strVals);
for (int i = 0; i < strVals.Len(); i++)
matrix.Add(strVals[i].GetFlt());
}
void ReadMtx(const TStr& Mtx, const TInt& MtxSize, TKronMtx& FitMtx){
TFltV matrix;
GetMtxFromSepLine(Mtx, ";", matrix);
FitMtx.GenMtx(matrix.Len() / MtxSize);
FitMtx.SetMtx(matrix);
}