-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.cpp
More file actions
357 lines (291 loc) · 11.3 KB
/
util.cpp
File metadata and controls
357 lines (291 loc) · 11.3 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
#include "util.h"
using namespace std;
typedef vector<string> stringvec;
typedef unsigned char uchar;
using namespace std;
int ancho = 0;
int alto = 0;
void listarDirectorio(const string& directorio, stringvec& v)
{
string nomArch;
DIR* dirp = opendir(directorio.c_str());
struct dirent * dp;
if (dirp == NULL) {
throw runtime_error("no se encontro directorio " + directorio + "!");
}
while ((dp = readdir(dirp)) != NULL) {
string nomArch = dp->d_name;
if (nomArch.compare(".") != 0 && nomArch.compare("..") != 0)
v.push_back(directorio + "/" + nomArch);
}
closedir(dirp);
}
vector<double> datosAVector(uchar &datos, int tamano) {
vector<double> ret (0);
for (size_t i = 0; i < tamano; ++i) {
double bt = static_cast<double>(*((unsigned char *)&datos + i));
ret.push_back(bt);
}
return ret;
}
void vectorADatos(vector<double> vec, int cantidadCopiada, uchar* datos) {
size_t i = 0;
for (vector<double>::iterator it = vec.begin() ; it != vec.end(); ++it) {
double byte = *it;
*(datos + cantidadCopiada + i) = static_cast<unsigned char>(byte);
i++;
}
}
void cargarDataSet(stringvec listaImagenes, int tamanoDeReferencia, vector<vector<double>> *dataSet) {
int ancho = 0, alto = 0;
uchar* datos = NULL;
PPM_LOADER_PIXEL_TYPE pt = PPM_LOADER_PIXEL_TYPE_INVALID;
uint i = 0;
for (stringvec::iterator it = listaImagenes.begin() ; it != listaImagenes.end(); ++it) {
string nombreArchivo = *it;
bool ret = LoadPPMFile(&datos, &ancho, &alto, &pt, nombreArchivo.c_str());
if (!ret || ancho == 0|| alto == 0|| pt!=PPM_LOADER_PIXEL_TYPE_GRAY_8B) {
throw runtime_error("no se puede cargar el archivo " + nombreArchivo + "!");
}
if (tamanoDeReferencia != ancho*alto)
throw runtime_error("La imagen " + nombreArchivo + " difiere en tamano o profundidad de color y canales.");
vector<double> vec = datosAVector(*datos, tamanoDeReferencia);
(*dataSet).push_back(vec);
}
}
void listarImagenes(string pathDataSet, stringvec& listadoImagenes) {
stringvec listadoDirectorios;
listarDirectorio(pathDataSet, listadoDirectorios);
for (vector<string>::iterator it = listadoDirectorios.begin() ; it != listadoDirectorios.end(); ++it) {
string path = *it;
listarDirectorio(path, listadoImagenes);
}
}
int getTamanoImagenes(string pathImagen, int *ancho, int *alto) {
uchar *datos;
PPM_LOADER_PIXEL_TYPE pt = PPM_LOADER_PIXEL_TYPE_INVALID;
bool ret = LoadPPMFile(&datos, ancho, alto, &pt, pathImagen.c_str());
if (!ret || ancho == 0|| alto == 0|| pt!=PPM_LOADER_PIXEL_TYPE_GRAY_8B) {
throw runtime_error("no se puede cargar el archivo" + pathImagen + "!");
}
return (*ancho)*(*alto);
}
void convertirMatrizAImagen(string pathImagen, int cantidadDeImagenes, vector<vector<double>>* dataSet) {
uchar* data = new uchar[cantidadDeImagenes*ancho*alto];
int copiado = 0;
for (int i=0; i<cantidadDeImagenes; i++) {
vectorADatos((*dataSet)[i], copiado, data);
copiado += ancho*alto;
}
SavePPMFile(pathImagen.c_str(), data, ancho, alto*cantidadDeImagenes, PPM_LOADER_PIXEL_TYPE_GRAY_8B, "salida junta");
}
const vector<string> explode(const string& s, const char& c)
{
string buff{""};
vector<string> v;
for(auto n:s)
{
if(n != c) buff+=n; else
if(n == c && buff != "") { v.push_back(buff); buff = ""; }
}
if(buff != "") v.push_back(buff);
return v;
}
void getEtiquetas(stringvec *listaDirectorios, vector<uint>* etiquetas) {
uint i = 0;
for (vector<string>::iterator it = listaDirectorios->begin() ; it != listaDirectorios->end(); ++it) {
i++;
string path = *it;
stringvec listadoImagenes;
listarDirectorio(path, listadoImagenes);
for (vector<string>::iterator it = listadoImagenes.begin() ; it != listadoImagenes.end(); ++it) {
(*etiquetas).push_back(i);
}
}
}
void cargarDataSetEnMatriz(string pathAlDataSet, vector<vector<double>>* dataSet, vector<uint>* labelsX) {
stringvec listaImagenes;
stringvec listaDirectorios;
listarDirectorio(pathAlDataSet, listaDirectorios);
listarImagenes(pathAlDataSet, listaImagenes);
//copy(listaImagenes.begin(), listaImagenes.end(), ostream_iterator<string>(cout, "\n"));
getEtiquetas(&listaDirectorios, labelsX);
/*string s = (etiquetas->find(44))->second;
cout << s << endl;*/
int tamanoDeReferencia = getTamanoImagenes(listaImagenes[0], &ancho, &alto);
//cout << tamanoDeReferencia << endl;
cargarDataSet(listaImagenes, tamanoDeReferencia, dataSet);
//codigo que escribe 20 de las imagenes cargagas en el vector para corroborar que las imagenes se
// pasaron correctamente a la matriz
string pathImagen = "./nomsal.pgm";
}
//------------------------ Parseo de la entrada -------------------------------//
bool contiene(char *argv[], const string *cadena) {
string param1 = argv[1], param2 = argv[3], param3 = argv[5], param4 = argv[5];
return param1.compare(*cadena) || param2.compare(*cadena) || param3.compare(*cadena) || param4.compare(*cadena);
}
string obtener(char *argv[], const string *cadena) {
string ret;
string param1 = argv[1], param2 = argv[3], param3 = argv[5], param4 = argv[7];
if (param1.compare(*cadena) == 0) ret = argv[2];
if (param2.compare(*cadena) == 0) ret = argv[4];
if (param3.compare(*cadena) == 0) ret = argv[6];
if (param4.compare(*cadena) == 0) ret = argv[8];
return ret;
}
bool obtenerParametros(int argc, char * argv[], string *metodo, string *trainSet, string *testSet, string *classif) {
bool ret = false;
const string param1 = "-m", param2 = "-i", param3 = "-q", param4 = "-o";
if (argc == 9 && contiene(argv, ¶m1) && contiene(argv, ¶m2) && contiene(argv, ¶m3) && contiene(argv, ¶m4)) {
*metodo = obtener(argv, ¶m1);
*trainSet = obtener(argv, ¶m2);
*testSet = obtener(argv, ¶m3);
*classif = obtener(argv, ¶m4);
ret = (metodo != NULL && trainSet != NULL && testSet != NULL && classif != NULL);
}
return ret;
}
/*
* nombreArchivo: el nombre del test, pero sin el '.in' ni el '.out' el metodo se va a encargar de cargar los datos de ambos.
* dataSet: la matriz que contendra las imagenes de entrada.
* labels: los labels de las imagenes cargadas en dataSet.
* autovalores: los 15 autovalores de mayor magnitud de la matriz de covarianza Mx (v1, v2, ..., v15) ordenados decrecientemente.
* */
void cargarTest(string nombreArchivo, vector<vector<double>> *dataSet, vector<uint> *labels, vector<double> *autovalores) {
fstream entrada(nombreArchivo + ".in", ios_base::in);
vector<string>* listaImagenes = new vector<string>(0);
string lectura;
bool path = true;
while(entrada >> lectura) {
lectura = explode(lectura, ',').at(0);
if (path) {
listaImagenes->push_back("./" + lectura);
path = false;
} else {
labels->push_back(stoi(lectura));
path = true;
}
}
entrada.close();
int tamanoDeReferencia = getTamanoImagenes(listaImagenes->at(0), &ancho, &alto);
cargarDataSet(*listaImagenes, tamanoDeReferencia, dataSet);
fstream salida(nombreArchivo + ".expected", ios_base::in);
while(salida >> lectura) {
autovalores->push_back(stod(lectura));
}
salida.close();
}
void cargarTest_aleat(vector<vector<double>> *dataSet, bool repeticion){
vector<string> listaImagenes(200);
string lectura;
unsigned short int i = 0;
while(i < 100){
listaImagenes[i] = "./randn/" + to_string(i+1) + ".pgm";
++i;
}
while(i < 200){
if(repeticion && (i%20 == 0 || i%20 == 1))
listaImagenes[i] = "./rands/" + to_string(1) + ".pgm";
else
listaImagenes[i] = "./rands/" + to_string((i-100)+1) + ".pgm";
++i;
}
int tamanoDeReferencia = getTamanoImagenes(listaImagenes.at(0), &ancho, &alto);
cargarDataSet(listaImagenes, tamanoDeReferencia, dataSet);
}
/*
* nombreArchivo: el nombre del archivo
* dataSet: la matriz que contendra las imagenes de entrada.
* labels: los labels de las imagenes cargadas en dataSet.
* */
void cargarSet(string nombreArchivo, vector<vector<double>> *dataSet, vector<uint> *labels) {
fstream entrada(nombreArchivo, ios_base::in);
vector<string>* listaImagenes = new vector<string>(0);
string lectura;
bool path = true;
while(entrada >> lectura) {
lectura = explode(lectura, ',').at(0);
if (path) {
listaImagenes->push_back("./" + lectura);
path = false;
} else {
labels->push_back(stoi(lectura));
path = true;
}
}
entrada.close();
int tamanoDeReferencia = getTamanoImagenes(listaImagenes->at(0), &ancho, &alto);
cargarDataSet(*listaImagenes, tamanoDeReferencia, dataSet);
}
/*
* nombreArchivo: el nombre del archivo
* dataSet: la matriz que contendra las imagenes de entrada.
* labels: los labels de las imagenes cargadas en dataSet.
* */
void cargarSet(string nombreArchivo, vector<vector<double>> *dataSet) {
fstream entrada(nombreArchivo, ios_base::in);
vector<string>* listaImagenes = new vector<string>(0);
string lectura;
while(entrada >> lectura) {
listaImagenes->push_back("./" + lectura);
}
entrada.close();
int tamanoDeReferencia = getTamanoImagenes(listaImagenes->at(0), &ancho, &alto);
cargarDataSet(*listaImagenes, tamanoDeReferencia, dataSet);
}
void guardarClasificacion(string nombreArchivo, vector<uint> &clasificacion) {
ofstream salida(nombreArchivo, ios_base::out);
for (vector<uint>::iterator it = clasificacion.begin() ; it != clasificacion.end(); ++it) {
uint clase = *it;
string clase_str = "";
clase_str += to_string(*it) + "\t";
//cout << fila_str << endl;
salida << clase_str << endl;
}
salida.close();
}
bool existeArchivo(const string& nombreArchivo) {
struct stat buf;
bool ret = false;
if (stat(nombreArchivo.c_str(), &buf) != -1) {
ret = true;
}
return ret;
}
bool is_file_exist(const char *fileName) {
std::ifstream infile(fileName);
return infile.good();
}
void crearCarpetaSiNoExiste(string path) {
if (!is_file_exist(path.c_str())) {
mkdir(path.c_str(), S_IRWXU);
}
}
void crearUltimaCarpetaSiNoExiste(string nombreArchivo) {
vector<string> v{explode(nombreArchivo, '/')};
string carpeta = "";
for (int i=0; i<v.size()-1; i++)
carpeta += v[i] + "/";
crearCarpetaSiNoExiste(carpeta);
}
/*
* Esta funcion es para que si el archivo ya existe lo abra y devuelve el flujo para agregarle datos
* y si no existe lo crea y devuelve el flujo agregarle datos.
*/
ofstream getFlujo(const string& nombreArchivo) {
crearUltimaCarpetaSiNoExiste(nombreArchivo);
if (existeArchivo(nombreArchivo)) {
ofstream ret(nombreArchivo, ios_base::app);
return ret;
} else {
ofstream ret2(nombreArchivo, ios_base::out);
return ret2;
}
}
string int2stringConCantidadDigitos(int cantidadDigitos, int i){
stringstream flujo;
flujo.fill ('0');
flujo.width (cantidadDigitos);
flujo << i;
return(flujo.str());
}