-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvOutput.cpp
More file actions
215 lines (193 loc) · 7.68 KB
/
Copy pathCsvOutput.cpp
File metadata and controls
215 lines (193 loc) · 7.68 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Copyright 2014 Wouter Spekkink
Authors : Wouter Spekkink <wouter.spekkink@gmail.com>
Website : http://www.wouterspekkink.org
This file is part of Dynamic Networks.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
Copyright 2014 Wouter Spekkink. All rights reserved.
Dynamic Networks is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Dynamic Networks is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CsvOutput.h"
#include <fstream>
#include <QFileDialog>
bool CsvOutput(MatrixMulti *matrix, std::string filename, std::string sep)
{
std::ofstream fileOut(filename.c_str());
if(!fileOut.is_open()) {
return false;
}
MatrixMulti *outputMatrix = matrix; //Pointer to the matrix to be written
std::vector<std::vector <short> > data = outputMatrix->GetPartData();
std::vector<std::string> rowNames = outputMatrix->GetPartRows();
fileOut << sep;
std::vector<std::string>::iterator it;
for(it = rowNames.begin(); it != rowNames.end(); it++) {
fileOut << *it << sep;
}
fileOut << '\n';
for(std::vector<std::string>::size_type i = 0; i != rowNames.size(); i++) {
fileOut << rowNames[i] << sep;
std::vector<short> currentRow = data[i];
std::vector<short>::iterator it2;
for(it2 = currentRow.begin(); it2 != currentRow.end(); it2++) {
fileOut << *it2 << sep;
}
fileOut << '\n';
}
fileOut.close();
return true;
}
bool CsvOutput(MatCollection *collection, std::string filename, std::string sep)
{
std::ofstream fileOut(filename.c_str());
if(!fileOut.is_open()) {
return false;
}
std::vector<MatrixMulti> outputCol = collection->GetCollection();
std::vector<MatrixMulti>::iterator collit;
for(collit = outputCol.begin(); collit != outputCol.end(); collit++) {
MatrixMulti currentMat = *collit;
std::vector<std::vector <short> > data = currentMat.GetPartData();
std::vector<std::string> rowNames = currentMat.GetPartRows();
fileOut << sep;
std::vector<std::string>::iterator it;
for(it = rowNames.begin(); it != rowNames.end(); it++) {
fileOut << *it << sep;
}
fileOut << '\n';
for(std::vector<std::string>::size_type i = 0; i != rowNames.size(); i++) {
fileOut << rowNames[i] << sep;
std::vector<short> currentRow = data[i];
std::vector<short>::iterator it2;
for(it2 = currentRow.begin(); it2 != currentRow.end(); it2++) {
fileOut << *it2 << sep;
}
fileOut << '\n';
}
}
fileOut.close();
return true;
}
bool CsvOutput(EdgeFinder *edges, std::string networkType, std::string filename, std::string sep) {
std::ofstream fileOut(filename.c_str());
if(!fileOut.is_open()) {
return false;
}
std::vector<std::vector <int> > myEdges = edges->GetEdges();
std::vector<std::string> edgeNames = edges->GetNames();
std::vector<std::string> edgeIntervals = edges->GetIntervals();
std::vector<std::string> attributeIntervals = edges->GetAttributeInterval();
std::vector<std::string> colNames = edges->GetHeader();
// Below part write the header
if(networkType == "Whole Network - Dynamic" || networkType == "Ego Network - Dynamic") {
fileOut << "Source" << sep << "Target" << sep << "Type" << sep << "Weight" << sep << "Time Interval";
fileOut << '\n';
} else if(networkType == "Partial Network - Static" || networkType == "Ego Network - Static") {
fileOut << "Source" << sep << "Target" << sep << "Type" << sep << "Weight" << sep << "Appearance";
fileOut << '\n';
}
std::vector<std::vector <int> >::iterator it;
for(it = myEdges.begin(); it != myEdges.end(); it++) {
// Writing the edges below
std::vector<int> currentEntry = *it;
if(!currentEntry.empty()) {
if(networkType == "Whole Network - Dynamic" || networkType == "Ego Network - Dynamic") {
fileOut << edgeNames[currentEntry[0]] << sep << edgeNames[currentEntry[1]] << sep << "Undirected" << sep;
fileOut << "<";
for(unsigned int i = 2; i != currentEntry.size() - 2; i++) {
if(i % 2 == 0) {
fileOut << attributeIntervals[currentEntry[i]];
} else {
fileOut << currentEntry[i] << "],";
}
}
fileOut << attributeIntervals[currentEntry[currentEntry.size() - 2]];
fileOut << currentEntry[currentEntry.size() - 1] << "]>" << sep;
}
else if(networkType == "Partial Network - Static" || networkType == "Ego Network - Static") {
fileOut << edgeNames[currentEntry[0]] << sep << edgeNames[currentEntry[1]] << sep << "Undirected" << sep << currentEntry[2] << sep;
}
if(networkType == "Whole Network - Dynamic" || networkType == "Ego Network - Dynamic") {
fileOut << "<";
for(unsigned int i = 2; i != currentEntry.size() - 2; i++) {
if(i % 2 == 0) {
fileOut << edgeIntervals[currentEntry[i]] << ",";
}
}
fileOut << edgeIntervals[currentEntry[currentEntry.size() - 2]] << ">";
fileOut << '\n';
} else if(networkType == "Partial Network - Static" || networkType == "Ego Network - Static") {
for(unsigned int i = 3; i != currentEntry.size() - 1; i++) {
fileOut << colNames[currentEntry[i]] << " - ";
}
fileOut << colNames[currentEntry[currentEntry.size()- 1]];
fileOut << '\n';
}
}
}
fileOut.close();
return true;
}
bool CsvOutput(MetricsFinder *metrics, std::string filename, std::string sep, std::string metric)
{
// The implementation should depend on the type of metric to be written
if(metric == "Involvement" || metric == "Degree") {
// Let's first initialize some variables.
std::vector<int> data = metrics->GetDataOne();
std::vector<std::string> names = metrics->GetNames();
std::vector<std::string> intervals = metrics->GetIntervals();
std::string agent = metrics->GetEgo();
std::ofstream fileOut(filename.c_str());
if(!fileOut.is_open()) {
return false;
}
if(metric == "Involvement") {
fileOut << "Involvement data for " << agent << ":\n";
fileOut << "Time Step" << sep << "Interval" << sep << "Involvement\n";
}
if(metric == "Degree") {
fileOut << "Degree data for " << agent << ":\n";
fileOut << "Time Step" << sep << "Interval" << sep << "Degree\n";
}
for(std::vector<int>::size_type i = 0; i != data.size(); i++) {
fileOut << "t" << i + 1 << sep;
fileOut << intervals[i] << sep;
fileOut << data[i] << "\n";
}
fileOut.close();
}
return true;
}
bool CsvOutput(TwoModeEdges *edges, std::string filename, std::string sep)
{
std::ofstream fileOut(filename.c_str());
if(!fileOut.is_open()) {
return false;
}
std::vector<std::vector <short> > myEdges = edges->GetEdges();
std::vector<std::string> edgeNames = edges->GetNames();
std::vector<std::string> edgeIntervals = edges->GetIntervals();
std::vector<std::string> colNames = edges->GetHeader();
int weight = 1;
fileOut << "Source" << sep << "Target" << sep << "Type" << sep << "Weight" << sep << "Time Interval\n";
std::vector<std::vector <short> >::iterator it;
for(it = myEdges.begin(); it != myEdges.end(); it++) {
std::vector<short> currentEntry = *it;
if(!currentEntry.empty()) {
fileOut << edgeNames[currentEntry[1]] << sep << colNames[currentEntry[0]] << sep << "Undirected" << sep << weight << sep;
fileOut << edgeIntervals[currentEntry[0]] ;
fileOut << "\n";
}
}
fileOut.close();
return true;
}