PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/read_datasets.h

https://github.com/lionelc/ANN-TrCL
C Header | 804 lines | 706 code | 94 blank | 4 comment | 190 complexity | 67f59a649d759f2b82e85d1e8972e317 MD5 | raw file
  1. //read hurricane-related data into neural network
  2. //written by Lei Jiang, Center for Computation and Technology, Louisiana State University
  3. //the code is now for testbed only. The robustness of functionality is not guaranteed. The original author is glad to know any major modification of key functionality
  4. //by the user of codes.
  5. #define LEARNING_FILE_PATH_RBF ".\\learning_error_archive\\learning_error_RBF.txt"
  6. #define VALIDATION_OUT_FILE_PATH_RBF ".\\learning_error_archive\\validation_error_RBF.txt"
  7. #define LEARNING_FILE_PATH ".\\learning_error_archive\\learning_error.txt"
  8. #define VALIDATION_OUT_FILE_PATH1 ".\\learning_error_archive\\tf_validation_error_counter.txt"
  9. #define WEIGHT_I_H_FILE ".\\learning_error_archive\\weight_i_h_counter.txt"
  10. #define WEIGHT_H_O_FILE ".\\learning_error_archive\\weight_h_o_counter.txt"
  11. #define WEIGHT_I_H_FILE_RBF ".\\learning_error_archive\\weight_i_h_rbf_counter.txt"
  12. #define WEIGHT_H_O_FILE_RBF ".\\learning_error_archive\\weight_h_o_rbf_counter.txt"
  13. #define TRAINING_FILE_PATH_TEST ".\\learning_error_archive\\test.txt"
  14. #define VALIDATION_FILE_PATH_TEST ".\\learning_error_archive\\test.txt"
  15. #define LEARNING_CURVE_PATH ".\\learning_error_archive\\learning_curve_counter.txt"
  16. void read_output_file(char* in_filename, int start, int end, double* obs, double* pred)
  17. {
  18. int i=0, j=0;
  19. int count =0, timecount=0; //count the num of edges
  20. int firstmark =0;
  21. char tempstr[MAX_LINE_LENGTH];
  22. char tempchar[LINE_THRESHOLD];
  23. for(i=0;i< MAX_LINE_LENGTH;i++)
  24. tempstr[i] = '\0';
  25. bool startmark = false;
  26. int startcount =0;
  27. FILE *file;
  28. file = fopen (in_filename, "r");
  29. if (!file)
  30. {
  31. printf ("Error opening %s\n", in_filename);
  32. exit(0);
  33. }
  34. while(fgets(tempstr,MAX_LINE_LENGTH,file))
  35. {
  36. i = 0;
  37. firstmark = 0;
  38. for(j=0; j< LINE_THRESHOLD; j++)
  39. tempchar[j] = '\0';
  40. if(tempstr[0] == '"' || tempstr[0] == '#')
  41. continue;
  42. if(!startmark)
  43. {
  44. if(startcount< start-1)
  45. {
  46. startcount++;
  47. continue;
  48. }
  49. else
  50. startmark = true;
  51. }
  52. while(tempstr[i] != ' ' && tempstr[i] != 9)
  53. {
  54. tempchar[firstmark] = tempstr[i];
  55. i++;
  56. firstmark++;
  57. }
  58. //added May 21
  59. timecount = atoi(tempchar);
  60. for(j=0; j< firstmark; j++)
  61. tempchar[j] = '\0';
  62. firstmark = 0;
  63. while(tempstr[i] == ' ' || tempstr[i] == 9)
  64. {
  65. i++;
  66. }
  67. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  68. {
  69. tempchar[firstmark] = tempstr[i];
  70. i++;
  71. firstmark++;
  72. }
  73. obs[count] = atof(tempchar);
  74. for(j=0; j< firstmark; j++)
  75. tempchar[j] = '\0';
  76. firstmark = 0;
  77. while(tempstr[i] == ' ' || tempstr[i] == 9)
  78. {
  79. i++;
  80. }
  81. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  82. {
  83. tempchar[firstmark] = tempstr[i];
  84. i++;
  85. firstmark++;
  86. }
  87. pred[count] = atof(tempchar);
  88. count++;
  89. if(count>= end-start+1)
  90. break;
  91. }
  92. fclose(file);
  93. return;
  94. }
  95. void read_max_wave_file(char* in_filename, int start, int end)
  96. {
  97. int i=0, j=0;
  98. int count =0; //count the num of edges
  99. int firstmark =0;
  100. char tempstr[MAX_LINE_LENGTH];
  101. char tempchar[LINE_THRESHOLD];
  102. for(i=0;i< MAX_LINE_LENGTH;i++)
  103. tempstr[i] = '\0';
  104. bool startmark = false;
  105. int startcount =0;
  106. FILE *file;
  107. file = fopen (in_filename, "r");
  108. if (!file)
  109. {
  110. printf ("Error opening %s\n", in_filename);
  111. exit(0);
  112. }
  113. while(fgets(tempstr,MAX_LINE_LENGTH,file))
  114. {
  115. i = 0;
  116. firstmark = 0;
  117. for(j=0; j< LINE_THRESHOLD; j++)
  118. tempchar[j] = '\0';
  119. if(tempstr[0] == '"' || tempstr[0] == '#')
  120. continue;
  121. if(!startmark)
  122. {
  123. if(startcount< start-1)
  124. {
  125. startcount++;
  126. continue;
  127. }
  128. else
  129. startmark = true;
  130. }
  131. while(tempstr[i] != ' ' && tempstr[i] != 9)
  132. {
  133. tempchar[firstmark] = tempstr[i];
  134. i++;
  135. firstmark++;
  136. }
  137. omega_c[count] = atof(tempchar);
  138. for(j=0; j< firstmark; j++)
  139. tempchar[j] = '\0';
  140. firstmark = 0;
  141. while(tempstr[i] == ' ' || tempstr[i] == 9)
  142. {
  143. i++;
  144. }
  145. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  146. {
  147. tempchar[firstmark] = tempstr[i];
  148. i++;
  149. firstmark++;
  150. }
  151. size_c[count] = atof(tempchar);
  152. for(j=0; j< firstmark; j++)
  153. tempchar[j] = '\0';
  154. firstmark = 0;
  155. while(tempstr[i] == ' ' || tempstr[i] == 9)
  156. {
  157. i++;
  158. }
  159. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  160. {
  161. tempchar[firstmark] = tempstr[i];
  162. i++;
  163. firstmark++;
  164. }
  165. radius_c[count] = atof(tempchar);
  166. for(j=0; j< firstmark; j++)
  167. tempchar[j] = '\0';
  168. firstmark = 0;
  169. while(tempstr[i] == ' ' || tempstr[i] == 9)
  170. {
  171. i++;
  172. }
  173. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  174. {
  175. tempchar[firstmark] = tempstr[i];
  176. i++;
  177. firstmark++;
  178. }
  179. maxw_c[count] = atof(tempchar);
  180. count++;
  181. if(count>= end-start+1)
  182. break;
  183. }
  184. fclose(file);
  185. return;
  186. }
  187. void read_cos_file(char* in_filename, int start, int end)
  188. {
  189. int i=0, j=0;
  190. int count =0; //count the num of edges
  191. int firstmark =0;
  192. char tempstr[MAX_LINE_LENGTH];
  193. char tempchar[LINE_THRESHOLD];
  194. for(i=0;i< MAX_LINE_LENGTH;i++)
  195. tempstr[i] = '\0';
  196. bool startmark = false;
  197. int startcount =0;
  198. FILE *file;
  199. file = fopen (in_filename, "r");
  200. if (!file)
  201. {
  202. printf ("Error opening %s\n", in_filename);
  203. exit(0);
  204. }
  205. while(fgets(tempstr,MAX_LINE_LENGTH,file))
  206. {
  207. i = 0;
  208. firstmark = 0;
  209. for(j=0; j< LINE_THRESHOLD; j++)
  210. tempchar[j] = '\0';
  211. if(tempstr[0] == '"' || tempstr[0] == '#')
  212. continue;
  213. if(!startmark)
  214. {
  215. if(startcount< start-1)
  216. {
  217. startcount++;
  218. continue;
  219. }
  220. else
  221. startmark = true;
  222. }
  223. while(tempstr[i] != ' ' && tempstr[i] != 9)
  224. {
  225. tempchar[firstmark] = tempstr[i];
  226. i++;
  227. firstmark++;
  228. }
  229. toy_input[count] = atof(tempchar);
  230. for(j=0; j< firstmark; j++)
  231. tempchar[j] = '\0';
  232. firstmark = 0;
  233. while(tempstr[i] == ' ' || tempstr[i] == 9)
  234. {
  235. i++;
  236. }
  237. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  238. {
  239. tempchar[firstmark] = tempstr[i];
  240. i++;
  241. firstmark++;
  242. }
  243. toy_output[count] = atof(tempchar);
  244. count++;
  245. if(count>= end-start+1)
  246. break;
  247. }
  248. fclose(file);
  249. return;
  250. }
  251. void read_unary_file(char* in_filename, int start, int end)
  252. {
  253. int i=0, j=0;
  254. int count =0; //count the num of edges
  255. int firstmark =0;
  256. char tempstr[MAX_LINE_LENGTH];
  257. char tempchar[LINE_THRESHOLD];
  258. for(i=0;i< MAX_LINE_LENGTH;i++)
  259. tempstr[i] = '\0';
  260. bool startmark = false;
  261. int startcount =0;
  262. FILE *file;
  263. file = fopen (in_filename, "r");
  264. if (!file)
  265. {
  266. printf ("Error opening %s\n", in_filename);
  267. exit(0);
  268. }
  269. while(fgets(tempstr,MAX_LINE_LENGTH,file))
  270. {
  271. i = 0;
  272. firstmark = 0;
  273. for(j=0; j< LINE_THRESHOLD; j++)
  274. tempchar[j] = '\0';
  275. if(tempstr[0] == '"' || tempstr[0] == '#')
  276. continue;
  277. if(!startmark)
  278. {
  279. if(startcount< start-1)
  280. {
  281. startcount++;
  282. continue;
  283. }
  284. else
  285. startmark = true;
  286. }
  287. while(tempstr[i] == ' ' || tempstr[i] == 9)
  288. {
  289. i++;
  290. }
  291. while(tempstr[i] != ' ' && tempstr[i] != 9)
  292. {
  293. tempchar[firstmark] = tempstr[i];
  294. i++;
  295. firstmark++;
  296. }
  297. omega_c[count] = atof(tempchar);
  298. for(j=0; j< firstmark; j++)
  299. tempchar[j] = '\0';
  300. firstmark = 0;
  301. while(tempstr[i] == ' ' || tempstr[i] == 9)
  302. {
  303. i++;
  304. }
  305. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  306. {
  307. tempchar[firstmark] = tempstr[i];
  308. i++;
  309. firstmark++;
  310. }
  311. radius_c[count] = atof(tempchar);
  312. for(j=0; j< firstmark; j++)
  313. tempchar[j] = '\0';
  314. firstmark = 0;
  315. while(tempstr[i] == ' ' || tempstr[i] == 9)
  316. {
  317. i++;
  318. }
  319. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  320. {
  321. tempchar[firstmark] = tempstr[i];
  322. i++;
  323. firstmark++;
  324. }
  325. size_c[count] = atof(tempchar);
  326. for(j=0; j< firstmark; j++)
  327. tempchar[j] = '\0';
  328. firstmark = 0;
  329. while(tempstr[i] == ' ' || tempstr[i] == 9)
  330. {
  331. i++;
  332. }
  333. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  334. {
  335. tempchar[firstmark] = tempstr[i];
  336. i++;
  337. firstmark++;
  338. }
  339. max_wave_sim[count][0] = atof(tempchar);
  340. for(j=0; j< firstmark; j++)
  341. tempchar[j] = '\0';
  342. firstmark = 0;
  343. while(tempstr[i] == ' ' || tempstr[i] == 9)
  344. {
  345. i++;
  346. }
  347. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  348. {
  349. tempchar[firstmark] = tempstr[i];
  350. i++;
  351. firstmark++;
  352. }
  353. max_wave_sim[count][1] = atof(tempchar);
  354. for(j=0; j< firstmark; j++)
  355. tempchar[j] = '\0';
  356. firstmark = 0;
  357. while(tempstr[i] == ' ' || tempstr[i] == 9)
  358. {
  359. i++;
  360. }
  361. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  362. {
  363. tempchar[firstmark] = tempstr[i];
  364. i++;
  365. firstmark++;
  366. }
  367. max_wave_sim[count][2] = atof(tempchar);
  368. for(j=0; j< firstmark; j++)
  369. tempchar[j] = '\0';
  370. firstmark = 0;
  371. while(tempstr[i] == ' ' || tempstr[i] == 9)
  372. {
  373. i++;
  374. }
  375. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10)
  376. {
  377. tempchar[firstmark] = tempstr[i];
  378. i++;
  379. firstmark++;
  380. }
  381. max_wave_sim[count][3] = atof(tempchar);
  382. count++;
  383. if(count>= end-start+1)
  384. break;
  385. }
  386. fclose(file);
  387. return;
  388. }
  389. void read_letter_file(char* in_filename, int start, int end)
  390. {
  391. int i=0, j=0;
  392. int count =0; //count the num of edges
  393. int firstmark =0;
  394. char tempstr[MAX_LINE_LENGTH];
  395. char tempchar[LINE_THRESHOLD];
  396. for(i=0;i< MAX_LINE_LENGTH;i++)
  397. tempstr[i] = '\0';
  398. bool startmark = false;
  399. int startcount =0;
  400. FILE *file;
  401. file = fopen (in_filename, "r");
  402. if (!file)
  403. {
  404. printf ("Error opening %s\n", in_filename);
  405. exit(0);
  406. }
  407. while(fgets(tempstr,MAX_LINE_LENGTH,file))
  408. {
  409. i = 0;
  410. firstmark = 0;
  411. for(j=0; j< LINE_THRESHOLD; j++)
  412. tempchar[j] = '\0';
  413. if(tempstr[0] == '"' || tempstr[0] == '#')
  414. continue;
  415. if(!startmark)
  416. {
  417. if(startcount< start-1)
  418. {
  419. startcount++;
  420. continue;
  421. }
  422. else
  423. startmark = true;
  424. }
  425. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i]!=',')
  426. {
  427. tempchar[firstmark] = tempstr[i];
  428. i++;
  429. firstmark++;
  430. }
  431. letter_class[count] = (int)tempchar[0]-65;
  432. for(int s=0; s<= 15; s++)
  433. {
  434. for(j=0; j< firstmark; j++)
  435. tempchar[j] = '\0';
  436. firstmark = 0;
  437. while(tempstr[i] == ' ' || tempstr[i] == 9 ||tempstr[i]==',')
  438. {
  439. i++;
  440. }
  441. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10 && tempstr[i]!=',')
  442. {
  443. tempchar[firstmark] = tempstr[i];
  444. i++;
  445. firstmark++;
  446. }
  447. input_letter[count][s] = atoi(tempchar);
  448. }
  449. count++;
  450. if(count>= end-start+1)
  451. break;
  452. }
  453. fclose(file);
  454. return;
  455. }
  456. void read_mushroom_file(char* in_filename, int start, int end)
  457. {
  458. int i=0, j=0;
  459. int count =0; //count the num of edges
  460. int firstmark =0;
  461. char tempstr[MAX_LINE_LENGTH];
  462. char tempchar[LINE_THRESHOLD];
  463. for(i=0;i< MAX_LINE_LENGTH;i++)
  464. tempstr[i] = '\0';
  465. bool startmark = false;
  466. int startcount =0;
  467. FILE *file;
  468. file = fopen (in_filename, "r");
  469. if (!file)
  470. {
  471. printf ("Error opening %s\n", in_filename);
  472. exit(0);
  473. }
  474. while(fgets(tempstr,MAX_LINE_LENGTH,file))
  475. {
  476. i = 0;
  477. firstmark = 0;
  478. for(j=0; j< LINE_THRESHOLD; j++)
  479. tempchar[j] = '\0';
  480. if(tempstr[0] == '"' || tempstr[0] == '#')
  481. continue;
  482. if(!startmark)
  483. {
  484. if(startcount< start-1)
  485. {
  486. startcount++;
  487. continue;
  488. }
  489. else
  490. startmark = true;
  491. }
  492. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i]!=',')
  493. {
  494. tempchar[firstmark] = tempstr[i];
  495. i++;
  496. firstmark++;
  497. }
  498. if(tempchar[0] == 'p')
  499. mushroom_class[count] = 0;
  500. else if (tempchar[0] == 'e')
  501. mushroom_class[count] = 1;
  502. for(int s=0; s< 10; s++)
  503. {
  504. for(j=0; j< firstmark; j++)
  505. tempchar[j] = '\0';
  506. firstmark = 0;
  507. while(tempstr[i] == ' ' || tempstr[i] == 9 ||tempstr[i]==',')
  508. {
  509. i++;
  510. }
  511. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10 && tempstr[i]!=',')
  512. {
  513. tempchar[firstmark] = tempstr[i];
  514. i++;
  515. firstmark++;
  516. }
  517. input_mushroom[count][s] = (int)tempchar[0]-97;
  518. }
  519. for(j=0; j< firstmark; j++)
  520. tempchar[j] = '\0';
  521. firstmark = 0;
  522. while(tempstr[i] == ' ' || tempstr[i] == 9 ||tempstr[i]==',')
  523. {
  524. i++;
  525. }
  526. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10 && tempstr[i]!=',')
  527. {
  528. tempchar[firstmark] = tempstr[i];
  529. i++;
  530. firstmark++;
  531. }
  532. for(int s=10; s< 21; s++)
  533. {
  534. for(j=0; j< firstmark; j++)
  535. tempchar[j] = '\0';
  536. firstmark = 0;
  537. while(tempstr[i] == ' ' || tempstr[i] == 9 ||tempstr[i]==',')
  538. {
  539. i++;
  540. }
  541. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10 && tempstr[i]!=',')
  542. {
  543. tempchar[firstmark] = tempstr[i];
  544. i++;
  545. firstmark++;
  546. }
  547. input_mushroom[count][s] = (int)tempchar[0]-97;
  548. }
  549. count++;
  550. if(count>= end-start+1)
  551. break;
  552. }
  553. fclose(file);
  554. return;
  555. }
  556. void read_aus_file(char* in_filename, int start, int end)
  557. {
  558. int i=0, j=0, temp;
  559. int count =0; //count the num of edges
  560. int firstmark =0;
  561. char tempstr[MAX_LINE_LENGTH];
  562. char tempchar[LINE_THRESHOLD];
  563. for(i=0;i< MAX_LINE_LENGTH;i++)
  564. tempstr[i] = '\0';
  565. bool startmark = false;
  566. int startcount =0;
  567. FILE *file;
  568. file = fopen (in_filename, "r");
  569. if (!file)
  570. {
  571. printf ("Error opening %s\n", in_filename);
  572. exit(0);
  573. }
  574. while(fgets(tempstr,MAX_LINE_LENGTH,file))
  575. {
  576. i = 0; temp=0;
  577. firstmark = 0;
  578. for(j=0; j< LINE_THRESHOLD; j++)
  579. tempchar[j] = '\0';
  580. if(tempstr[0] == '"' || tempstr[0] == '#')
  581. continue;
  582. if(!startmark)
  583. {
  584. if(startcount< start-1)
  585. {
  586. startcount++;
  587. continue;
  588. }
  589. else
  590. startmark = true;
  591. }
  592. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i]!=',')
  593. {
  594. tempchar[firstmark] = tempstr[i];
  595. i++;
  596. firstmark++;
  597. }
  598. if(tempchar[0] == '-')
  599. aus_class[count] = 0;
  600. else if (tempchar[0] == '+')
  601. aus_class[count] = 1;
  602. for(int s=0; s<14 ;s++)
  603. input_aus[count][s] = 0.0;
  604. for(int s=0; s< 14; s++)
  605. {
  606. if(tempstr[i] == '\0' || tempstr[i] == 9 || tempstr[i]==10)
  607. break;
  608. for(j=0; j< firstmark; j++)
  609. tempchar[j] = '\0';
  610. firstmark = 0;
  611. while(tempstr[i] == ' '|| tempstr[i] == 9 ||tempstr[i]==',')
  612. {
  613. i++;
  614. }
  615. while(tempstr[i] != ' ' && tempstr[i]!=':' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10 )
  616. {
  617. tempchar[firstmark] = tempstr[i];
  618. i++;
  619. firstmark++;
  620. }
  621. temp = atoi(tempchar);
  622. if(temp <1)
  623. break;
  624. for(j=0; j< firstmark; j++)
  625. tempchar[j] = '\0';
  626. firstmark=0;
  627. i++; //for the colon
  628. while(tempstr[i] != ' ' && tempstr[i] != 9 && tempstr[i] != '\0' && tempstr[i] != 10 && tempstr[i]!=',' && tempstr[i]!=':')
  629. {
  630. tempchar[firstmark] = tempstr[i];
  631. i++;
  632. firstmark++;
  633. }
  634. input_aus[count][temp-1] = atof(tempchar);
  635. if(temp == 14)
  636. break;
  637. }
  638. count++;
  639. if(count>= end-start+1)
  640. break;
  641. }
  642. fclose(file);
  643. return;
  644. }