-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAdditionalMethods.cpp
More file actions
206 lines (168 loc) · 5.39 KB
/
AdditionalMethods.cpp
File metadata and controls
206 lines (168 loc) · 5.39 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
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <time.h>
#include "AdditionalMethods.h"
#include "ObjectMatrix.h"
#include "DataObject.h"
#include "alglib/dataanalysis.h"
///////////////////////////////////////////////////////////
// Projection.cpp
// Implementation of the Class Projection
// Created on: 24-Spa-2013 12:20:33
// Original author: Mindaugas
///////////////////////////////////////////////////////////
/*! \file AdditionalMethods
\brief A class of static methods for data conversion and static members.
*/
const char* AdditionalMethods::alphanum= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char *AdditionalMethods::tempFileSavePath = "";
std::string AdditionalMethods::tempPath = "";
FILE *AdditionalMethods::distFile = NULL;
//int AdditionalMethods::distFileCreated = 0;
time_t AdditionalMethods::startTime = 0;
AdditionalMethods::AdditionalMethods()
{
}
AdditionalMethods::~AdditionalMethods()
{
}
/**
* Method generates random file name and adds to the prefix
*/
std::string AdditionalMethods::generateFileName()
{
time_t t;
time(&t);
srand((unsigned int)(t+rand()));
std::string catString, path;
path.assign(AdditionalMethods::inputDataFile);
std::string::size_type tt = path.find_last_of("/");
path = path.substr(0,tt) + "/";
AdditionalMethods::tempPath.assign(path);
catString.assign(AdditionalMethods::tempPath);
int i;
int qty = 20;
/* char PID[5];
snprintf(PID, sizeof(PID), "%d", AdditionalMethods::PID);*/
for ( i = 0; i < qty; ++i)
catString += (AdditionalMethods::alphanum[rand() % (strlen(AdditionalMethods::alphanum) - 1)]);
///
/*catString +="_";
catString +=PID;*/
///
catString +=".bin";
return catString;
}
double** AdditionalMethods::ObjectMatrixToDouble(ObjectMatrix matrix)
{
int numOfObjects = matrix.getObjectCount();
int numOfFeatures = matrix.getObjectAt(0).getFeatureCount();
double **matrixToReturn;
matrixToReturn = Array2D(numOfObjects, numOfFeatures);
for (int i = 0; i < numOfObjects; i++)
for (int j = 0; j < numOfFeatures; j++)
matrixToReturn[i][j] = matrix.getObjectAt(i).getFeatureAt(j);
return matrixToReturn;
}
ObjectMatrix AdditionalMethods::DoubleToObjectMatrix(double** matrix, int rows, int cols)
{
std::vector<double> v;
v.reserve(cols);
ObjectMatrix toReturn(rows);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
v.push_back(matrix[i][j]);
toReturn.addObject(DataObject(v));
v.clear();
}
return toReturn;
}
double** AdditionalMethods::Array2D(int rows, int cols)
{
double *data = (double *)malloc(rows*cols*sizeof(double));
double **array= (double **)malloc(rows*sizeof(double*));
for (int i=0; i<rows; i++)
array[i] = &(data[cols*i]);
return array;
}
alglib::real_1d_array AdditionalMethods::ObjectMatrixTo1DArray(ObjectMatrix matrix)
{
alglib::real_1d_array toReturn;
int m = matrix.getObjectCount();
int n = matrix.getObjectAt(0).getFeatureCount();
toReturn.setlength(m * n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
toReturn(n * i + j) = matrix.getObjectAt(i).getFeatureAt(j);
return toReturn;
}
alglib::real_1d_array AdditionalMethods::DataObjectTo1DArray(DataObject dataObject)
{
alglib::real_1d_array toReturn;
// int m = matrix.getObjectCount();
int n = dataObject.getFeatureCount();
toReturn.setlength(n);
// for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
toReturn(j) = dataObject.getFeatureAt(j);
return toReturn;
}
ObjectMatrix AdditionalMethods::alglib1DArrayToObjectMatrix(alglib::real_1d_array array, int featureCount)
{
int m = array.length() / featureCount;
ObjectMatrix toReturn(m);
std::vector<double> dataObjectFeatures;
dataObjectFeatures.reserve(featureCount);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < featureCount; j++)
dataObjectFeatures.push_back(array(featureCount * i + j));
toReturn.addObject(DataObject(dataObjectFeatures));
dataObjectFeatures.clear();
}
return toReturn;
}
DataObject AdditionalMethods::alglib1DArrayToDataObject(alglib::real_1d_array array)
{
int m = array.length();// / featureCount;
DataObject toReturn;
std::vector<double> dataObjectFeatures;
dataObjectFeatures.reserve(m);
/* for (int i = 0; i < m; i++)
{*/
for (int j = 0; j < m; j++)
dataObjectFeatures.push_back(array(j));
/*toReturn.addObject(DataObject(dataObjectFeatures));
dataObjectFeatures.clear();*/
toReturn = DataObject(dataObjectFeatures);
//}
return toReturn;
}
std::vector<std::string> AdditionalMethods::split(const std::string &s, char delim, std::vector<std::string> &elems)
{
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
std::vector<std::string> AdditionalMethods::split(const std::string& s, char delim)
{
std::vector<std::string> elems;
AdditionalMethods::split(s, delim, elems);
return elems;
}
/**
* Method that deletes single file
*/
int AdditionalMethods::deleteFile()
{
fclose(AdditionalMethods::distFile);
if (std::remove(AdditionalMethods::tempFileSavePath) != 0)
std::cout << "Error deleting file";
return 0;
}