-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandling.cpp
200 lines (159 loc) · 5.99 KB
/
FileHandling.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
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
///-------------------------------------------------------------------------------------------------
// file: FileHandling.cpp
//
// summary: Implements the file handling class
///-------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "FileHandling.h"
FileHandling::FileHandling() {}
// Input: Contents of input.csv -- Opens the file and reads the values into matrix values
void FileHandling::openFile(ClassData &data)
{
using namespace std;
ifstream myFile;
string line;
myFile.open(data.fileName); // Open the file
if (myFile.is_open()) { /* Valid File: Begin reading data form the file */
vector<std::string> columns;
while (getline(myFile, line)) { /* While the file has more data */
istringstream cell(line);
string distance;
while (getline(cell, distance, ',')) {
columns.push_back(distance);
}
data.values.push_back(columns); // Store data
columns.clear();
}
}
myFile.close();
}
// Input: Contents of input.csv -- Splits the values into x and y coords
void FileHandling::sortGraph(ClassData &data)
{
std::vector<float> xdatatemp;
std::vector<float> ydatatemp;
float xCoord = 0;
float yCoord = 0;
data.getLabels();
for (int i = 1; i < (data.values.size()); i++) // OLD: for (int i = 1; i < (data.values.size() - 1); i++)
{ // Columns
int nodeClass =
stoi(data.values[i][(data.values[0].size() - 1)]); // Get the class of the node
if (nodeClass > data.numOfClasses) // Get the highest class number
data.numOfClasses = nodeClass;
data.classNum.push_back(nodeClass); // Add to vector of class numbers
for (int j = 1; j < (data.values[i].size() - 1); j++) // Rows
{
if (xdatatemp.size() <= ydatatemp.size()) // Get X-coords
{
xCoord = stof(data.values[i][j]);
xdatatemp.push_back(xCoord);
if (xCoord > data.xmax)
data.xmax = xCoord;
}
else
{ // Get y coords
yCoord = stof(data.values[i][j]);
ydatatemp.push_back(yCoord);
if (yCoord > data.ymax)
data.ymax = yCoord;
}
}
if (xdatatemp.size() != ydatatemp.size()) // Duplicate last pair if odd # of columns
{
ydatatemp.push_back(xCoord);
if (yCoord > data.ymax) // Get y-max
data.ymax = yCoord;
}
data.xdata.push_back(xdatatemp);
data.dataTransparency.push_back(255);
data.originalXData.push_back(xdatatemp); // Add line plot coords
data.ydata.push_back(ydatatemp);
data.originalYData.push_back(ydatatemp);
xdatatemp.clear(); // Clear for the next plot line
ydatatemp.clear();
}
}
void FileHandling::normalizeData(ClassData &data)
{
// Y Normalization
std::vector<float> minYcol;
std::vector<float> maxYcol;
std::vector<float> temp;
std::vector<std::vector<float> > convertedValues; // NEW
float min = 0;
float max = 0;
for (int j = 0; j < data.ydata[0].size(); j++) { // Gets the min and max of every column
min = data.ydata[0][j];
for (int i = 0; i < data.ydata.size(); i++) {
if (data.ydata[i][j] > max) {
max = data.ydata[i][j];
}
if (data.ydata[i][j] < min) {
min = data.ydata[i][j];
}
}
if (min == max) // This is used when entire column is of same values, the normalized value is 0.5 (chosen arbitrarily)
min = 0;
minYcol.push_back(min);
maxYcol.push_back(max);
max = 0;
}
for (int i = 0; i < data.ydata.size(); i++) { // Normalize the data from 0 - 1
for (int j = 0; j < data.ydata[0].size(); j++) {
float original = data.ydata[i][j];
float currentMin = minYcol[j];
float currentMax = maxYcol[j];
float converted = (original - currentMin) / (currentMax - currentMin);
temp.push_back(converted);
}
convertedValues.push_back(temp);
temp.clear();
}
data.ydata.clear();
data.ydata = convertedValues; // Fill ydata coordinates with normalized data
data.originalYData = convertedValues;
convertedValues.clear();
//data.xmax = data.ydata[0].size(); // Change xMax and yMax to normalized data
data.ymax = 1;
// X Normalization after this point
std::vector<float> minXcol;
std::vector<float> maxXcol;
temp.clear();
convertedValues.clear();
min = 0;
max = 0;
for (int j = 0; j < data.xdata[0].size(); j++) { // Gets the min and max of every column
min = data.xdata[0][j];
for (int i = 0; i < data.xdata.size(); i++) {
if (data.xdata[i][j] > max) {
max = data.xdata[i][j];
}
if (data.xdata[i][j] < min) {
min = data.xdata[i][j];
}
}
if (min == max) //This is used when entire column is of same values, the normalized value is 0.5 (chosen arbitrarily)
min =0;
minXcol.push_back(min);
maxXcol.push_back(max);
max = 0;
}
for (int i = 0; i < data.xdata.size(); i++) { // Normalize the data from 0 - 1
for (int j = 0; j < data.xdata[0].size(); j++) {
float original = data.xdata[i][j];
float currentMin = minXcol[j];
float currentMax = maxXcol[j];
float converted = (original - currentMin) / (currentMax - currentMin);
temp.push_back(converted);
}
convertedValues.push_back(temp);
temp.clear();
}
data.xdata.clear();
data.xdata = convertedValues; // Fill ydata coordinates with normalized data
data.originalXData = convertedValues;
convertedValues.clear();
//data.xmax = data.ydata[0].size(); // Change xMax and yMax to normalized data
data.xmax = 1;
}