-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataio.cpp
More file actions
512 lines (467 loc) · 14.8 KB
/
Copy pathdataio.cpp
File metadata and controls
512 lines (467 loc) · 14.8 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
* --------------------------------------------------------------------------- *
* CAMPAIGN *
* --------------------------------------------------------------------------- *
* This is part of the CAMPAIGN data clustering library originating from *
* Simbios, the NIH National Center for Physics-Based Simulation of Biological *
* Structures at Stanford, funded under the NIH Roadmap for Medical Research, *
* grant U54 GM072970 (See https://simtk.org), and the FEATURE Project at *
* Stanford, funded under the NIH grant LM05652 *
* (See http://feature.stanford.edu/index.php). *
* *
* Portions copyright (c) 2010 Stanford University, Authors, and Contributors. *
* Authors: Kai J. Kohlhoff *
* Contributors: Marc Sosnick, William Hsu *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* *
* This program 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 Lesser General Public *
* License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* --------------------------------------------------------------------------- *
*/
/* $Id$ */
/**
* \file dataio.cpp
* \brief File and stream I/O routines
*
* Implements a number of I/O routines for files and streams.
*
* \author Author: Kai J. Kohlhoff, Contributors: Marc Sosnick, William Hsu
* \date 2/2/2010
* \version 1.0
**/
#include <iostream>
#include <fstream>
#include <cfloat>
#include <string>
#include <sstream>
#include <assert.h>
#include "dataio.h"
#include "defaults.h"
using namespace std;
class Internal
{
private:
int N; /** < Number of data points */
int K; /** < Number of cluster centers */
int D; /** < Number of dimensions */
int dataSize; /** < Number of elements in data set */
bool deviceCheck; /** < Determines if device check should be performed */
bool printTime; /** < Determines if time of execution should be reported */
float* data; /** < Storage location for clustering input data */
const char* fileName; /** < Name of data file */
const char* execName; /** < Name of executable */
const char* methodType; /** < Type of method, e.g. type of clustering algorithm */
public:
/**
* Constructor
*/
Internal()
{
N = K = D = dataSize = 0;
deviceCheck = true;
printTime = true;
data = NULL;
}
/**
* Destructor
*/
~ Internal()
{
delete data;
}
/**
* \brief Get number of data points
* \return Number of data points
*/
int getNumElements() { return N; };
/**
* \brief Get number of clusters
* \return Number of clusters
*/
int getNumClusters() { return K; };
/**
* \brief Get number of dimensions
* \return Number of dimensions
*/
int getDimensions() { return D; };
/**
* \brief Get name of executable
* \return Name of executable
*/
const char* getExecName()
{
return execName;
}
/**
* \brief Get name of data file
* \return Name of data file
*/
const char* getFileName()
{
return fileName;
}
/**
* \brief Get type of method
* \return Type of method
*/
const char* getMethodType()
{
return methodType;
}
/**
* \brief Get number of elements in data file
* \return Number of elements in data file
*/
int getDataSize()
{
return dataSize;
}
/**
* \brief Set name of executable
*/
void setExecName(const char* en)
{
execName = en;
}
/**
* \brief Set name of data file
*/
void setFileName(const char* fn)
{
fileName = fn;
}
/**
* \brief Set type of method
*/
void setMethodType(const char* mt)
{
methodType = mt;
}
/**
* \brief Set number of data elements
*/
void setDataSize(int numData)
{
dataSize = numData;
}
/**
* \brief Returns pointer to current data, or NULL if none has been read
* \return An array containing the data
*/
float* getData()
{
return data;
}
/**
* \brief Print parameters
*/
void printParams()
{
std::cout << "Using " << N << " data points, " << K << " clusters, " << D << " dimension(s)" << std::endl;
}
istream& getline(istream& stream, string& str)
{
char ch;
str.clear();
while (stream.get(ch) && ch != '\n')
str.push_back(ch);
return stream;
}
/**
* \brief Read data from file
* \param fileName Name of data file
* \return An array containing the data
*/
float* readParsFile(const char* fileName)
{
string line;
ifstream infile;
float pars[3];
infile.open(fileName, ios::in);
if (!infile.is_open())
{
std::cout << "Error in readParsFile(): Unable to find or open file \"" << fileName << "\"." << std::endl;
exit(1);
}
assert(!infile.fail());
try
{
// read parameters (number of data points, clusters, and dimensions)
for (int i = 0; i < 3; i++)
{
getline(infile, line);
if (infile.eof()) throw 111;
istringstream buffer(line.c_str());
if (!(buffer >> pars[i])) throw 112;
}
if (N == 0) N = (int) pars[0]; // if N not otherwise initialized
if (K == 0) K = (int) pars[1]; // if K not otherwise initialized
if (D == 0) D = (int) pars[2]; // if D not otherwise initialized
}
catch (int e)
{
std::cout << "Error in dataIO::readParsFile(): ";
if (e == 111) std::cout << "reached end of file \"" << fileName << "\" prematurely" << std::endl;
else if (e == 112) std::cout << "can only read floating point numbers" << std::endl;
else std::cout << "reading file content failed" << std::endl;
std::cout << " Please check parameters and file format" << std::endl;
return NULL;
}
infile.close();
assert(!infile.fail());
return data;
}
/**
* \brief Read data from file
* \param fileName Name of data file
* \return An array containing the data
*/
float* readFile(const char* fileName)
{
string line;
ifstream infile;
float pars[3];
int numData;
infile.open(fileName, ios::in);
if (!infile.is_open())
{
std::cout << "Error in readFile(): Unable to find or open file \"" << fileName << "\"." << std::endl;
exit(1);
}
assert(!infile.fail());
try
{
// read parameters (number of data points, clusters, and dimensions)
for (int i = 0; i < 3; i++)
{
getline(infile, line);
if (infile.eof()) throw 42;
istringstream buffer(line.c_str());
if (!(buffer >> pars[i])) throw 1337;
}
if (N == 0) N = (int) pars[0]; // if N not otherwise initialized
if (K == 0) K = (int) pars[1]; // if K not otherwise initialized
if (D == 0) D = (int) pars[2]; // if D not otherwise initialized
// read the actual data
if ((numData = dataSize) == 0)
{
printParams();
numData = N * D;
}
std::cout << "Reading " << numData << " floats" << std::endl;
data = (float*) malloc(sizeof(float) * numData);
memset(data, 0, sizeof(float) * numData);
for (int i = 0; i < numData; i++)
{
getline(infile, line);
if (infile.eof()) throw 42;
istringstream buffer(line.c_str());
if (!(buffer >> data[i])) throw 1337;
}
}
catch (int e)
{
std::cout << "Error in dataIO::readFile(): ";
if (e == 42) std::cout << "reached end of file \"" << fileName << "\" prematurely" << std::endl;
else if (e == 1337) std::cout << "can only read floating point numbers" << std::endl;
else std::cout << "reading file content failed" << std::endl;
std::cout << " Please check parameters and file format" << std::endl;
return NULL;
}
infile.close();
assert(!infile.fail());
return data;
}
/**
* \brief Read data from stream, used internally by readStdIn()
* \param numbers Number of floats to be read
* \return An array containing the data
*/
float* readStdIn(int numbers)
{
data = (float*) malloc(sizeof(float) * numbers);
memset(data, 0, sizeof(float) * numbers);
for (int i = 0; i < numbers; i++)
{
cin >> data[i];
}
return data;
}
/**
* \brief Read data from stdin
* \return An array containing the data
*/
float* readStdIn()
{
int numData = 0;
std::cout << "No file name provided, reading data from stdin" << std::endl;
std::cout << "Please input number of data points N:" << std::endl;
cin >> N;
std::cout << "Please input number of cluster centers K:" << std::endl;
cin >> K;
std::cout << "Please input number of dimensions D:" << std::endl;
cin >> D;
if ((numData = dataSize) == 0)
{
printParams();
numData = N * D;
}
std::cout << "Reading " << numData << " floats" << std::endl;
readStdIn(numData);
return data;
}
}; // End of Internal
/**
* Constructor
*/
DataIO::DataIO()
{
ip = new Internal;
}
/**
* Destructor
*/
DataIO::~DataIO()
{
delete ip;
}
/**
* \brief Read data from file or stdin.
* \param fileName Name of data file, if empty quotes ("") read from stdin
* \return An array containing the data
*/
float* DataIO::readData(const char* fileName)
{
float* data;
if (fileName == nullptr) data = ip->readStdIn();
else data = ip->readFile(fileName);
return data;
}
/**
* \brief Returns pointer to current data, or NULL if none has been read
* \return An array containing the data
*/
float* DataIO::getData()
{
return ip->getData();
}
/**
* \brief Get name of data file
* \return Name of data file
*/
const char* DataIO::getFileName()
{
return ip->getFileName();
}
/**
* \brief Get number of data points
* \return Number of data points
*/
int DataIO::getNumElements() { return ip->getNumElements(); }
/**
* \brief Get number of clusters
* \return Number of clusters
*/
int DataIO::getNumClusters() { return ip->getNumClusters(); }
/**
* \brief Get number of dimensions
* \return Number of dimensions
*/
int DataIO::getDimensions() { return ip->getDimensions(); }
/**
* \brief Get number of elements in data file
* \return Number of elements in data file
*/
int DataIO::getDataSize() { return ip->getDataSize(); }
/**
* \brief Set number of elements in data file. Overrides default behavio
* \param numData Number of elements in data file
*/
void DataIO::setDataSize(int numData)
{
ip->setDataSize(numData);
}
/**
* \brief Prints list of clusters
*/
void DataIO::printClusters(int numData, int numClust, int numDim, float *data, float *ctr, int *assign)
{
std::cout << "Data clusters:" << std::endl;
// Data must have at least one dimension
if (numDim < 1)
{
std::cout << "Error in printClusters(). Dimension D has to be larger than 0" << std::endl;
return;
}
// if data points in one dimension (scalar values)
else if (numDim == 1)
{
for (int k = 0; k < numClust; ++k)
{
std::cout << "Cluster " << k << " (";
int count = 0;
for (int n = 0; n < numData; ++n)
{
if (assign[n] == k) { std::cout << data[n] << ", "; count++; }
}
// remove last two characters to avoid trailing comma
if (count > 0) std::cout << "\b\b";
if (ctr != NULL) std::cout << ") ctr " << ctr[k] << std::endl;
else std::cout << ")" << std::endl;
}
}
// if vector length > 1
else
{
// for each cluster center
for (int i = 0; i < numClust; i++)
{
std::cout << "Cluster " << i << " (";
int count = 0;
// for each data point
for (int j = 0; j < numData; j++)
{
// if element assigned to current cluster center then print out element
if (assign[j] == i)
{
// print out vectors
std::cout << "{";
// Hsu 9/29/11 for (int cnt = 0; cnt < numDim; cnt++) std::cout << data[numDim * j + cnt] << ((cnt < numDim-1) ? ", " : "");
for (int cnt = 0; cnt < numDim; cnt++) std::cout << data[numData * cnt + j] << ((cnt < numDim-1) ? ", " : ""); // Hsu 9/29/11 new
std::cout << "}, ";
count++;
}
}
// remove last two characters to avoid trailing comma
if (count > 0) std::cout << "\b\b";
if (ctr != NULL)
{
// print cluster center
std::cout << ") ctr {";
for (int cnt = 0; cnt < numDim; cnt++) std::cout << ctr[numDim * i + cnt] << ", ";
std::cout << "\b\b}" << std::endl;;
}
else std::cout << ")" << std::endl;
}
}
}
/**
* Main, intended for unit testing
*/
/*
int main()
{
DataIO* fio = new DataIO;
fio->readData("../../data/testInput.dat");
return 0;
}
*/