parameter_list.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "parameter_list.h"
  5. using namespace std;
  6. ParameterList::ParameterList() :
  7. verbose(string(max_verbosity_states,'0')), file(""), sequence_start(0), output(""), F(0), Jets(0),
  8. file_gt(""), center(Point(-1,-1)), extent(Point(-1,-1)),
  9. bf_weights(vector<double>(0)), exps(1), current_exp(0) {
  10. }
  11. ParameterList::ParameterList(string filename) :
  12. verbose(string(max_verbosity_states,'0')), file(""), sequence_start(0), output(""), F(0), Jets(0),
  13. file_gt(""), center(Point(-1,-1)), extent(Point(-1,-1)),
  14. bf_weights(vector<double>(0)), exps(1), current_exp(0)
  15. {
  16. read(filename);
  17. }
  18. ParameterList::ParameterList(ParameterList& params) :
  19. verbose(params.verbose), file(params.file), file_list(params.file_list), id_list(params.id_list), name_list(params.name_list), category_list(params.category_list), sequence_start(params.sequence_start), sequence_start_list(params.sequence_start_list),
  20. jet_estimation(params.jet_estimation), jet_weight(params.jet_weight), jet_fps(params.jet_fps), jet_S(params.jet_S), output(params.output), F(params.F), Jets(params.Jets),
  21. file_gt(params.file_gt), file_gt_list(params.file_gt_list), occlusions_list(params.occlusions_list), pixel(params.pixel), set(params.set), center(params.center), extent(params.extent),
  22. bf_weights(params.bf_weights),
  23. insert_order_it(params.insert_order_it), params(params.params), paramslist(params.paramslist), paramslist_iterator(params.paramslist_iterator),
  24. exps(params.exps), current_exp(params.current_exp)
  25. {
  26. }
  27. void ParameterList::read(string filename) {
  28. // read in config from file
  29. FILE* f = fopen(filename.c_str(), "rb");
  30. if (f == NULL)
  31. perror("Error opening file");
  32. else
  33. {
  34. char buffer[200];
  35. while (!feof(f))
  36. {
  37. if(fgets(buffer, 200, f) == NULL)
  38. break;
  39. char* name = strtok(buffer, "\n"); // get rid of '\n' at the end
  40. name = strtok(name, "\t"); // first token
  41. if (name != NULL && name[0] != '#') {
  42. char* value = strtok(NULL, "\t"); // next token
  43. if(value != 0) {
  44. // get sequence file name
  45. if(strcmp(name,"id") == 0) {
  46. id_list.push_back(atoi(value));
  47. continue;
  48. }
  49. // get sequence file name
  50. if(strcmp(name,"name") == 0) {
  51. name_list.push_back(value);
  52. continue;
  53. }
  54. // get sequence file name
  55. if(strcmp(name,"category") == 0) {
  56. category_list.push_back(value);
  57. continue;
  58. }
  59. // get sequence file name
  60. if(strcmp(name,"file") == 0) {
  61. file = value;
  62. file_list.push_back(value);
  63. continue;
  64. }
  65. // get sequence groundtruth file name
  66. if(strcmp(name,"file_gt") == 0) {
  67. file_gt = value;
  68. file_gt_list.push_back(value);
  69. continue;
  70. }
  71. // get sequence groundtruth file name
  72. if(strcmp(name,"occlusions") == 0) {
  73. occlusions_list.push_back(value);
  74. continue;
  75. }
  76. // get output path for results
  77. if(strcmp(name,"output") == 0) {
  78. output = value;
  79. continue;
  80. }
  81. // get first frame number
  82. if(strcmp(name,"start") == 0) {
  83. sequence_start = atoi(value);
  84. sequence_start_list.push_back(sequence_start);
  85. continue;
  86. }
  87. // get jet estimation path
  88. if(strcmp(name,"jet_estimation") == 0) {
  89. jet_estimation.push_back(value);
  90. }
  91. // get jet weight path
  92. if(strcmp(name,"jet_weight") == 0) {
  93. jet_weight.push_back(atof(value));
  94. }
  95. // get jet estimation path
  96. if(strcmp(name,"jet_fps") == 0) {
  97. jet_fps.push_back(atoi(value));
  98. }
  99. // get jet estimation path
  100. if(strcmp(name,"jet_S") == 0) {
  101. jet_S.push_back(atoi(value));
  102. }
  103. // get sequence length
  104. if(strcmp(name,"F") == 0) {
  105. F = atoi(value);
  106. pixel = vector<vector<Point2f> >(F, vector<Point2f>(0));
  107. set = vector<int>(F, 0);
  108. continue;
  109. }
  110. // get sequence length
  111. if(strcmp(name,"Jets") == 0) {
  112. Jets = atoi(value);
  113. continue;
  114. }
  115. // get selected pixels
  116. if(strcmp(name,"pixel") == 0) {
  117. int i = atoi(value); // get number of img
  118. //i -= sequence_start;
  119. i -= 1;
  120. if(i < 0 || i >= (int) F)
  121. continue;
  122. char* coord = strtok(NULL, "\t"); // next token
  123. while(coord != 0) {
  124. char* x = strsep(&coord, ","); // split x and y
  125. char* y = strsep(&coord, ","); // next token
  126. if(x != 0 && y != 0) {
  127. pixel[i].push_back(Point2f(atof(x),atof(y)));
  128. set[i]++;
  129. }
  130. coord = strtok(NULL, "\t"); // next token
  131. }
  132. continue;
  133. }
  134. // get center for cropping
  135. if(strcmp(name,"center") == 0) {
  136. char* x = strtok(value, ","); // split x and y
  137. char* y = strtok(NULL, ","); // next token
  138. if(x != 0 && y != 0)
  139. center = Point(atof(x),atof(y));
  140. continue;
  141. }
  142. // get frame size for cropping
  143. if(strcmp(name,"extent") == 0) {
  144. char* x = strtok(value, ","); // split x and y
  145. char* y = strtok(NULL, ","); // next token
  146. if(x != 0 && y != 0)
  147. extent = Point(atof(x),atof(y));
  148. continue;
  149. }
  150. // get bilateral filter weights
  151. if(strcmp(name,"bf_weight") == 0) {
  152. int i = atoi(value); // get number of weight
  153. if(i > 0) i--;
  154. char* weight = strtok(NULL, "\t"); // next token
  155. if(weight != 0) {
  156. bf_weights.resize(i+1, 0);
  157. bf_weights[i] = atof(weight);
  158. }
  159. continue;
  160. }
  161. if(strcmp(name,"verbose") == 0) {
  162. verbose = value;
  163. // do not continue for backward compatibility
  164. }
  165. // get any other parameter
  166. string n = name;
  167. insert(n, parse(value), true);
  168. } else {
  169. if(name[0] != '\0' && name[0] != '#')
  170. cerr << "Value to parameter '" << name << "' is missing!" << endl;
  171. }
  172. }
  173. }
  174. fclose (f);
  175. }
  176. for(uint32_t i = id_list.size(); i < name_list.size(); i++)
  177. id_list.push_back(i);
  178. }
  179. void ParameterList::insert(string param, string val, bool overwrite) {
  180. vector<string> vals;
  181. vals.push_back(val);
  182. insert(param, vals, overwrite);
  183. }
  184. void ParameterList::insert(string param, vector<string> vals, bool overwrite) {
  185. map<string, uint32_t>::iterator it = params.find(param);
  186. if(it != params.end()) {
  187. // parameter already exists
  188. uint32_t idx = it->second;
  189. // remove old size of parameterlist from exps
  190. exps /= paramslist[idx].size();
  191. // overwrite parameter list
  192. if(overwrite)
  193. paramslist[idx] = vals;
  194. else
  195. paramslist[idx].insert(paramslist[idx].end(), vals.begin(), vals.end());
  196. // add new size of parameterlist from exps
  197. exps *= paramslist[idx].size();
  198. } else {
  199. // parameter does not exist
  200. uint32_t idx = paramslist.size();
  201. params.insert(pair<string,uint32_t>(param, idx)); // add parameter
  202. insert_order_it.push_back(params.find(param)); // store iterator
  203. paramslist.push_back(vals); // store parameter values
  204. paramslist_iterator.push_back(0); // selected value
  205. // add parameterlist to exps
  206. exps *= paramslist[idx].size();
  207. }
  208. }
  209. uint32_t ParameterList::experiment(){
  210. return current_exp;
  211. }
  212. uint32_t ParameterList::experiments() {
  213. return exps;
  214. }
  215. string ParameterList::experimentName() {
  216. stringstream os;
  217. bool empty = true;
  218. // iterate over all parameters
  219. for(uint32_t i = 0; i < insert_order_it.size(); i++) {
  220. string name = insert_order_it[i]->first;
  221. uint32_t idx = insert_order_it[i]->second;
  222. uint32_t iter = paramslist_iterator[idx];
  223. // skip parameters with only one value
  224. if(paramslist[idx].size() == 1)
  225. continue;
  226. if(!empty) os << "_";
  227. os << name << "_" << paramslist[idx][iter];
  228. empty = false;
  229. }
  230. return os.str();
  231. }
  232. string ParameterList::currentParametersName() {
  233. stringstream os;
  234. // iterate over all parameters
  235. for(uint32_t i = 0; i < insert_order_it.size(); i++) {
  236. string name = insert_order_it[i]->first;
  237. uint32_t idx = insert_order_it[i]->second;
  238. // skip parameters with only one value
  239. if(paramslist[idx].size() == 1)
  240. continue;
  241. os << name << "\t";
  242. }
  243. return os.str();
  244. }
  245. string ParameterList::currentParametersTabs() {
  246. stringstream os;
  247. // iterate over all parameters
  248. for(uint32_t i = 0; i < insert_order_it.size(); i++) {
  249. uint32_t idx = insert_order_it[i]->second;
  250. // skip parameters with only one value
  251. if(paramslist[idx].size() == 1)
  252. continue;
  253. os << "\t";
  254. }
  255. return os.str();
  256. }
  257. string ParameterList::currentParametersValue() {
  258. stringstream os;
  259. // iterate over all parameters
  260. for(uint32_t i = 0; i < insert_order_it.size(); i++) {
  261. string name = insert_order_it[i]->first;
  262. uint32_t idx = insert_order_it[i]->second;
  263. uint32_t iter = paramslist_iterator[idx];
  264. // skip parameters with only one value
  265. if(paramslist[idx].size() == 1)
  266. continue;
  267. os << paramslist[idx][iter] << "\t";
  268. }
  269. return os.str();
  270. }
  271. void ParameterList::reset() {
  272. for(uint32_t i = 0; i < paramslist.size(); i++)
  273. paramslist_iterator[i] = 0;
  274. current_exp = 0;
  275. }
  276. bool ParameterList::hasNextExp() {
  277. return current_exp < (exps - 1);
  278. }
  279. bool ParameterList::nextExp() {
  280. if(!hasNextExp())
  281. return false;
  282. // increment iterator of each list
  283. for(uint32_t i = 0; i < paramslist.size(); i++) {
  284. // skip parameters with only one value
  285. if(paramslist[i].size() == 1)
  286. continue;
  287. paramslist_iterator[i]++;
  288. // decide whether to increment
  289. if(paramslist_iterator[i] == paramslist[i].size())
  290. paramslist_iterator[i] = 0;
  291. else
  292. break;
  293. }
  294. current_exp++;
  295. return true;
  296. }
  297. ostream& operator<<(ostream& os, const ParameterList& params) {
  298. os << params.cfgString();
  299. return os;
  300. }
  301. string ParameterList::cfgString(bool all_exps) const {
  302. stringstream os;
  303. // compute the maximal length of a parameter name to align all with tabs
  304. int max_length = 0;
  305. for(uint32_t i = 0; i < insert_order_it.size(); i++) {
  306. string name = insert_order_it[i]->first;
  307. max_length = max(max_length, (int) name.length());
  308. }
  309. int tabs = ceil(max_length/8) + 1;
  310. for(uint32_t f = 0; f < id_list.size(); f++)
  311. os << "id" << string(tabs,'\t') << id_list[f] << "\n";
  312. for(uint32_t f = 0; f < name_list.size(); f++)
  313. os << "name" << string(tabs,'\t') << name_list[f] << "\n";
  314. for(uint32_t f = 0; f < file_list.size(); f++)
  315. os << "file" << string(tabs,'\t') << file_list[f] << "\n";
  316. if(file_list.size() == 0)
  317. os << "file" << string(tabs,'\t') << file << endl;
  318. for(uint32_t f = 0; f < category_list.size(); f++)
  319. os << "category" << string(tabs,'\t') << category_list[f] << "\n";
  320. for(uint32_t f = 0; f < file_gt_list.size(); f++)
  321. os << "file_gt" << string(tabs,'\t') << file_gt_list[f] << "\n";
  322. if(file_gt_list.size() == 0)
  323. os << "file_gt" << string(tabs,'\t') << file_gt << endl;
  324. for(uint32_t f = 0; f < occlusions_list.size(); f++)
  325. os << "occlusions" << string(tabs,'\t') << occlusions_list[f] << "\n";
  326. if(!output.empty())
  327. os << "output" << string(tabs,'\t') << output << "\n" << "\n";
  328. for(uint32_t f = 0; f < sequence_start_list.size(); f++)
  329. os << "start" << string(tabs,'\t') << sequence_start_list[f] << "\n";
  330. if(sequence_start_list.size() == 0)
  331. os << "start" << string(tabs,'\t') << sequence_start << endl;
  332. for(uint32_t f = 0; f < jet_estimation.size(); f++)
  333. os << "jet_estimation" << string(tabs,'\t') << jet_estimation[f] << "\n";
  334. for(uint32_t f = 0; f < jet_weight.size(); f++)
  335. os << "jet_weight" << string(tabs,'\t') << jet_weight[f] << "\n";
  336. for(uint32_t f = 0; f < jet_fps.size(); f++)
  337. os << "jet_fps" << string(tabs,'\t') << jet_fps[f] << "\n";
  338. for(uint32_t f = 0; f < jet_S.size(); f++)
  339. os << "jet_S" << string(tabs,'\t') << jet_S[f] << "\n";
  340. os << "F" << string(tabs,'\t') << F << "\n";
  341. os << "Jets" << string(tabs,'\t') << Jets << "\n";
  342. for(uint32_t i = 0; i < F; i++) {
  343. if(i < set.size() && set[i] > 0) {
  344. os << "pixels " << i << "\t";
  345. for(int p = 0 ; p < set[i]; p++)
  346. os << pixel[i][p] << "\t";
  347. os << "\n";
  348. }
  349. }
  350. if(extent.x > 0 || extent.y > 0)
  351. os << "extent" << string(tabs,'\t') << extent << "\n";
  352. if(center.x > 0 || center.y > 0)
  353. os << "center" << string(tabs,'\t') << center << "\n";
  354. os << "\n";
  355. // iterate over all other parameters
  356. for(uint32_t i = 0; i < insert_order_it.size(); i++) {
  357. string name = insert_order_it[i]->first;
  358. uint32_t idx = insert_order_it[i]->second;
  359. uint32_t iter = paramslist_iterator[idx];
  360. // print parameter name and add tabs
  361. os << name << string(1,'\t');
  362. if(!all_exps || paramslist[idx].size() == 1)
  363. os << paramslist[idx][iter];
  364. // print parameter list
  365. if(paramslist[idx].size() > 1) {
  366. if(!all_exps) os << "\t # in ";
  367. os << "(";
  368. for(uint32_t i = 0; i < paramslist[idx].size(); i++) {
  369. if(i > 0) os << ",";
  370. os << paramslist[idx][i];
  371. }
  372. os << ")";
  373. }
  374. os << "\n";
  375. }
  376. if(bf_weights.size() > 0) {
  377. os << "bf_weights\t";
  378. for(uint32_t i = 0; i < bf_weights.size(); i++) {
  379. os << bf_weights[i] << "\t";
  380. }
  381. os << "\n";
  382. }
  383. return os.str();
  384. }
  385. vector<string> ParameterList::parse(char* value) {
  386. vector<string> val_list;
  387. // check if list of paramter values specified
  388. if(value[0] == '(') {
  389. // copy for strok
  390. char* cpy = new char[strlen(value)+1];
  391. strcpy(cpy,value);
  392. char* list = strtok(cpy, "("); // get rid of '('
  393. list = strtok(list, ")"); // get rid of ')'
  394. list = strtok(list, ","); // iterate over list
  395. while(list != NULL) {
  396. val_list.push_back(list);
  397. list = strtok(NULL, ","); // iterate over set
  398. }
  399. // delete copy
  400. delete[] cpy;
  401. } else {
  402. val_list.push_back(value);
  403. }
  404. return val_list;
  405. }
  406. void ParameterList::print() {
  407. int max_length = 0;
  408. for(uint32_t i = 0; i < insert_order_it.size(); i++) {
  409. string name = insert_order_it[i]->first;
  410. max_length = max(max_length, (int) name.length());
  411. }
  412. int tabs = ceil(max_length/8) + 1;
  413. if(current_exp == 0) {
  414. for(uint32_t f = 0; f < id_list.size(); f++)
  415. cout << "id" << string(tabs,'\t') << id_list[f] << endl;
  416. for(uint32_t f = 0; f < name_list.size(); f++)
  417. cout << "name" << string(tabs,'\t') << name_list[f] << endl;
  418. for(uint32_t f = 0; f < file_list.size(); f++)
  419. cout << "file" << string(tabs,'\t') << file_list[f] << endl;
  420. if(file_list.size() == 0)
  421. cout << "file" << string(tabs,'\t') << file << endl;
  422. for(uint32_t f = 0; f < category_list.size(); f++)
  423. cout << "category" << string(tabs,'\t') << category_list[f] << endl;
  424. for(uint32_t f = 0; f < file_gt_list.size(); f++)
  425. cout << "file_gt" << string(tabs,'\t') << file_gt_list[f] << endl;
  426. if(file_gt_list.size() == 0)
  427. cout << "file_gt" << string(tabs,'\t') << file_gt << endl;
  428. for(uint32_t f = 0; f < occlusions_list.size(); f++)
  429. cout << "occlusions" << string(tabs,'\t') << occlusions_list[f] << endl;
  430. cout << "output" << string(tabs,'\t') << output << endl << endl;
  431. for(uint32_t f = 0; f < sequence_start_list.size(); f++)
  432. cout << "start" << string(tabs,'\t') << sequence_start_list[f] << endl;
  433. if(sequence_start_list.size() == 0)
  434. cout << "start" << string(tabs,'\t') << sequence_start << endl;
  435. for(uint32_t f = 0; f < jet_estimation.size(); f++)
  436. cout << "jet_estimation" << string(tabs,'\t') << jet_estimation[f] << "\n";
  437. for(uint32_t f = 0; f < jet_weight.size(); f++)
  438. cout << "jet_weight" << string(tabs,'\t') << jet_weight[f] << "\n";
  439. for(uint32_t f = 0; f < jet_fps.size(); f++)
  440. cout << "jet_fps" << string(tabs,'\t') << jet_fps[f] << "\n";
  441. for(uint32_t f = 0; f < jet_S.size(); f++)
  442. cout << "jet_S" << string(tabs,'\t') << jet_S[f] << "\n";
  443. cout << "F" << string(tabs,'\t') << F << endl;
  444. cout << "Jets" << string(tabs,'\t') << Jets << endl;
  445. for(uint32_t i = 0; i < F; i++) {
  446. if(i < set.size() && set[i] > 0) {
  447. cout << "pixels " << i << string(ceil(max_length/8),'\t');
  448. for(int p = 0 ; p < set[i]; p++)
  449. cout << pixel[i][p] << "\t";
  450. cout << endl;
  451. }
  452. }
  453. if(extent.x > 0 || extent.y > 0)
  454. cout << "extent" << string(tabs,'\t') << extent << endl;
  455. if(center.x > 0 || center.y > 0)
  456. cout << "center" << string(tabs,'\t') << center << endl;
  457. }
  458. cout << endl;
  459. if(exps > 1)
  460. cout << "--------------------- Experiment " << (current_exp + 1) << " of " << exps << " ---------------------" << endl;
  461. cout << "Parameters set to:" << endl;
  462. for(uint32_t i = 0; i < insert_order_it.size(); i++) {
  463. string name = insert_order_it[i]->first;
  464. uint32_t idx = insert_order_it[i]->second;
  465. uint32_t iter = paramslist_iterator[idx];
  466. // print parameter name and add tabs
  467. int tabs = ceil(max_length/8) - floor(name.length()/8) + 1;
  468. cout << "\t" << name << string(tabs,'\t') << paramslist[idx][iter];
  469. // print parameter list
  470. if(paramslist[idx].size() > 1) {
  471. cout << "\t in (";
  472. for(uint32_t i = 0; i < paramslist[idx].size(); i++) {
  473. if(i > 0)
  474. cout << ",";
  475. cout << paramslist[idx][i];
  476. }
  477. cout << ")";
  478. }
  479. cout << endl;
  480. }
  481. if(bf_weights.size() > 0) {
  482. cout << "\tbf_weights" << string(ceil(max_length/8),'\t');
  483. for(uint32_t i = 0; i < bf_weights.size(); i++) {
  484. cout << bf_weights[i] << "\t";
  485. }
  486. cout << endl;
  487. }
  488. }
  489. bool ParameterList::exists(string param) {
  490. return params.find(param) != params.end();
  491. }
  492. void ParameterList::setParameter(string param, string value) {
  493. if(!exists(param)) {
  494. vector<string> vals;
  495. vals.push_back(value);
  496. insert(param,vals);
  497. }
  498. uint32_t idx = params[param];
  499. uint32_t it = paramslist_iterator[idx];
  500. paramslist[idx][it] = value;
  501. }
  502. string ParameterList::parameter(const char* param) {
  503. if(!exists(param)) {
  504. cerr << "Error: Parameter " << param << " does not exist!" << endl;
  505. return "";
  506. }
  507. uint32_t idx = params[param];
  508. uint32_t it = paramslist_iterator[idx];
  509. return paramslist[idx][it];
  510. }
  511. template<>
  512. string ParameterList::parameter<string>(string param, string def) {
  513. if(!exists(param)) {
  514. return def;
  515. }
  516. uint32_t idx = params[param];
  517. uint32_t it = paramslist_iterator[idx];
  518. return paramslist[idx][it];
  519. }
  520. template<>
  521. int ParameterList::parameter<int>(string param, string def) {
  522. if(!exists(param)) {
  523. if(!def.empty()) return atoi(def.c_str());
  524. cerr << "Error: Parameter " << param << " does not exist!" << endl;
  525. return 0;
  526. }
  527. uint32_t idx = params[param];
  528. uint32_t it = paramslist_iterator[idx];
  529. return atoi(paramslist[idx][it].c_str());
  530. }
  531. template<>
  532. double ParameterList::parameter<double>(string param, string def) {
  533. if(!exists(param)) {
  534. if(!def.empty()) return atof(def.c_str());
  535. cerr << "Error: Parameter " << param << " does not exist!" << endl;
  536. return 0;
  537. }
  538. uint32_t idx = params[param];
  539. uint32_t it = paramslist_iterator[idx];
  540. return atof(paramslist[idx][it].c_str());
  541. }
  542. template<>
  543. float ParameterList::parameter<float>(string param, string def) {
  544. if(!exists(param)) {
  545. if(!def.empty()) return atof(def.c_str());
  546. cerr << "Error: Parameter " << param << " does not exist!" << endl;
  547. return 0;
  548. }
  549. uint32_t idx = params[param];
  550. uint32_t it = paramslist_iterator[idx];
  551. return atof(paramslist[idx][it].c_str());
  552. }
  553. template<>
  554. bool ParameterList::parameter<bool>(string param, string def) {
  555. if(!exists(param)) {
  556. if(!def.empty()) return (def == "0") ? false : true;
  557. cerr << "Error: Parameter " << param << " does not exist!" << endl;
  558. return false;
  559. }
  560. uint32_t idx = params[param];
  561. uint32_t it = paramslist_iterator[idx];
  562. return (paramslist[idx][it] == "0") ? false : true;
  563. }
  564. template<>
  565. vector<int> ParameterList::splitParameter<int>(string param, string def) {
  566. char* split_str = NULL;
  567. if(!exists(param)) {
  568. if(def.length() > 0) {
  569. split_str = new char[def.length() + 1];
  570. strcpy(split_str, def.c_str());
  571. }
  572. } else {
  573. uint32_t idx = params[param];
  574. uint32_t it = paramslist_iterator[idx];
  575. if(paramslist[idx][it].length() > 0) {
  576. split_str = new char[paramslist[idx][it].length() + 1];
  577. strcpy(split_str, paramslist[idx][it].c_str());
  578. }
  579. }
  580. vector<int> output;
  581. if(split_str != NULL) {
  582. char* part = strtok(split_str, ","); // next token
  583. while (part != NULL) {
  584. output.push_back(atoi(part));
  585. part = strtok(NULL, ","); // next token
  586. }
  587. delete[] split_str;
  588. }
  589. return output;
  590. }
  591. template<>
  592. vector<float> ParameterList::splitParameter<float>(string param, string def) {
  593. char* split_str = NULL;
  594. if(!exists(param)) {
  595. if(def.length() > 0) {
  596. split_str = new char[def.length() + 1];
  597. strcpy(split_str, def.c_str());
  598. }
  599. } else {
  600. uint32_t idx = params[param];
  601. uint32_t it = paramslist_iterator[idx];
  602. if(paramslist[idx][it].length() > 0) {
  603. split_str = new char[paramslist[idx][it].length() + 1];
  604. strcpy(split_str, paramslist[idx][it].c_str());
  605. }
  606. }
  607. vector<float> output;
  608. if(split_str != NULL) {
  609. char* part = strtok(split_str, ","); // next token
  610. while (part != NULL) {
  611. output.push_back(atof(part));
  612. part = strtok(NULL, ","); // next token
  613. }
  614. delete[] split_str;
  615. }
  616. return output;
  617. }
  618. template<>
  619. int ParameterList::maximum(string param){
  620. if(!exists(param))
  621. throw std::logic_error( "Parameter used to obtain minimum does not exist!" );
  622. uint32_t idx = params[param];
  623. int maxi = 0;
  624. for(uint32_t it = 0; it < paramslist[idx].size(); it++)
  625. maxi = std::max(maxi, atoi(paramslist[idx][it].c_str()));
  626. return maxi;
  627. }
  628. template<>
  629. int ParameterList::minimum(string param){
  630. if(!exists(param))
  631. throw std::logic_error( "Parameter used to obtain minimum does not exist!" );
  632. uint32_t idx = params[param];
  633. int mini = 0;
  634. for(uint32_t it = 0; it < paramslist[idx].size(); it++)
  635. mini = std::min(mini, atoi(paramslist[idx][it].c_str()));
  636. return mini;
  637. }