PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/plastimatch/segment/autolabel_parms.cxx

https://gitlab.com/plastimatch/plastimatch
C++ | 378 lines | 344 code | 19 blank | 15 comment | 267 complexity | 5e8595a3b9f13fba800946f0376446a0 MD5 | raw file
  1. /* -----------------------------------------------------------------------
  2. See COPYRIGHT.TXT and LICENSE.TXT for copyright and license information
  3. ----------------------------------------------------------------------- */
  4. #include "plmsegment_config.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <math.h>
  9. #include <fstream>
  10. #include <iostream>
  11. #include <sstream>
  12. #include "autolabel_feature.h"
  13. #include "file_util.h"
  14. #include "parameter_parser.h"
  15. #include "autolabel_parms.h"
  16. #include "plm_math.h"
  17. #include "print_and_exit.h"
  18. #include "string_util.h"
  19. class Autolabel_parms_parser : public Parameter_parser
  20. {
  21. public:
  22. Autolabel_parms *mp;
  23. public:
  24. Autolabel_parms_parser (Autolabel_parms *mp)
  25. {
  26. this->mp = mp;
  27. }
  28. public:
  29. virtual Plm_return_code begin_section (
  30. const std::string& section)
  31. {
  32. if (section == "PREALIGN" || section == "PREALIGNMENT") {
  33. this->enable_key_regularization (true);
  34. return PLM_SUCCESS;
  35. }
  36. if (section == "ATLAS-SELECTION") {
  37. this->enable_key_regularization (true);
  38. return PLM_SUCCESS;
  39. }
  40. if (section == "TRAINING") {
  41. this->enable_key_regularization (true);
  42. return PLM_SUCCESS;
  43. }
  44. if (section == "REGISTRATION") {
  45. this->enable_key_regularization (true);
  46. return PLM_SUCCESS;
  47. }
  48. if (section == "STRUCTURES") {
  49. this->enable_key_regularization (false);
  50. return PLM_SUCCESS;
  51. }
  52. if (section == "LABELING") {
  53. this->enable_key_regularization (true);
  54. return PLM_SUCCESS;
  55. }
  56. if (section == "OPTIMIZATION_RESULT") {
  57. this->enable_key_regularization (true);
  58. return PLM_SUCCESS;
  59. }
  60. /* else, unknown section */
  61. return PLM_ERROR;
  62. }
  63. virtual Plm_return_code end_section (
  64. const std::string& section)
  65. {
  66. return PLM_SUCCESS;
  67. }
  68. virtual Plm_return_code set_key_value (
  69. const std::string& section,
  70. const std::string& key,
  71. const std::string& index,
  72. const std::string& val)
  73. {
  74. return this->mp->set_key_value (section, key, index, val);
  75. }
  76. };
  77. class Autolabel_parms_private
  78. {
  79. public:
  80. std::list<Autolabel_feature*> feature_list;
  81. public:
  82. Autolabel_parms_private () {}
  83. ~Autolabel_parms_private () {
  84. delete_all_features ();
  85. }
  86. void delete_all_features () {
  87. std::list<Autolabel_feature*>::iterator it;
  88. for (it = feature_list.begin(); it != feature_list.end(); it++) {
  89. delete *it;
  90. }
  91. feature_list.clear ();
  92. }
  93. };
  94. Autolabel_parms::Autolabel_parms ()
  95. {
  96. this->enforce_anatomic_constraints = false;
  97. this->d_ptr = new Autolabel_parms_private ();
  98. }
  99. Autolabel_parms::~Autolabel_parms ()
  100. {
  101. delete this->d_ptr;
  102. }
  103. Plm_return_code
  104. Autolabel_parms::set_key_value (
  105. const std::string& section,
  106. const std::string& key,
  107. const std::string& index,
  108. const std::string& val
  109. )
  110. {
  111. #if defined (commentout)
  112. /* [PREALIGNMENT] */
  113. if (section == "PREALIGN" || section == "PREALIGNMENT") {
  114. if (key == "mode") {
  115. if (val == "DISABLED" || val == "disabled"
  116. || val == "Disabled" || val == "0")
  117. {
  118. this->prealign_mode = "disabled";
  119. }
  120. else if (val == "DEFAULT" || val == "default" || val == "Default") {
  121. this->prealign_mode = "default";
  122. }
  123. else if (val == "CUSTOM" || val == "custom" || val == "Custom") {
  124. this->prealign_mode = "custom";
  125. }
  126. }
  127. else if (key == "reference") {
  128. this->prealign_reference = val;
  129. }
  130. else if (key == "spacing") {
  131. this->prealign_spacing = val;
  132. }
  133. else if (key == "registration_config") {
  134. this->prealign_registration_config = val;
  135. }
  136. else {
  137. goto error_exit;
  138. }
  139. }
  140. /* [ATLAS-SELECTION] */
  141. if (section == "ATLAS-SELECTION") {
  142. if (key == "enable_atlas_selection") {
  143. if (val == "True" || val == "true" || val == "1") {
  144. this->enable_atlas_selection = true;
  145. }
  146. else {
  147. this->enable_atlas_selection = false;
  148. }
  149. }
  150. else if (key == "atlas_selection_criteria") {
  151. if (val == "nmi" || val == "NMI") {
  152. this->atlas_selection_criteria="nmi";
  153. }
  154. else if (val == "nmi-post" || val == "NMI-POST") {
  155. this->atlas_selection_criteria="nmi-post";
  156. }
  157. else if (val == "nmi-ratio" || val == "NMI-RATIO") {
  158. this->atlas_selection_criteria="nmi-ratio";
  159. }
  160. else if (val == "mse" || val == "MSE") {
  161. this->atlas_selection_criteria="mse";
  162. }
  163. else if (val == "mse-post" || val == "MSE-POST") {
  164. this->atlas_selection_criteria="mse-post";
  165. }
  166. else if (val == "mse-ratio" || val == "MSE-RATIO") {
  167. this->atlas_selection_criteria="mse-ratio";
  168. }
  169. else if (val == "random" || val == "RANDOM") {
  170. this->atlas_selection_criteria="random";
  171. }
  172. else if (val == "precomputed" || val == "PRECOMPUTED") {
  173. this->atlas_selection_criteria="precomputed";
  174. }
  175. }
  176. else if (key == "similarity_percent_threshold") {
  177. sscanf (val.c_str(), "%g", &this->similarity_percent_threshold);
  178. }
  179. else if (key == "atlases_from_ranking") {
  180. sscanf (val.c_str(), "%d", &this->atlases_from_ranking);
  181. }
  182. else if (key == "mi_histogram_bins") {
  183. sscanf (val.c_str(), "%d", &this->mi_histogram_bins);
  184. }
  185. else if (key == "percentage_nmi_random_sample") {
  186. sscanf (val.c_str(), "%g", &this->percentage_nmi_random_sample);
  187. }
  188. else if (key == "roi_mask_fn" || key == "roi_mask") {
  189. this->roi_mask_fn = val;
  190. }
  191. else if (key == "selection_reg_parms") {
  192. this->selection_reg_parms_fn = val;
  193. }
  194. else if (key == "lower_mi_value_subject") {
  195. sscanf (val.c_str(), "%d", &this->lower_mi_value_sub);
  196. this->lower_mi_value_sub_defined = true;
  197. }
  198. else if (key == "upper_mi_value_subject") {
  199. sscanf (val.c_str(), "%d", &this->upper_mi_value_sub);
  200. this->upper_mi_value_sub_defined = true;
  201. }
  202. else if (key == "lower_mi_value_atlas") {
  203. sscanf (val.c_str(), "%d", &this->lower_mi_value_atl);
  204. this->lower_mi_value_atl_defined = true;
  205. }
  206. else if (key == "upper_mi_value_atlas") {
  207. sscanf (val.c_str(), "%d", &this->upper_mi_value_atl);
  208. this->upper_mi_value_atl_defined = true;
  209. }
  210. else if (key == "min_random_atlases") {
  211. sscanf (val.c_str(), "%d", &this->min_random_atlases);
  212. }
  213. else if (key == "max_random_atlases") {
  214. sscanf (val.c_str(), "%d", &this->max_random_atlases);
  215. }
  216. else if (key == "precomputed_ranking") {
  217. this->precomputed_ranking_fn = val;
  218. }
  219. else {
  220. goto error_exit;
  221. }
  222. }
  223. /* [TRAINING] */
  224. if (section == "TRAINING") {
  225. if (key == "atlas_dir") {
  226. this->atlas_dir = val;
  227. }
  228. else if (key == "fusion_criteria") {
  229. if (val == "gaussian" || val == "GAUSSIAN" || val == "Gaussian") {
  230. this->fusion_criteria = "gaussian";
  231. }
  232. else if (val == "staple" || val == "STAPLE" || val == "Staple") {
  233. this->fusion_criteria = "staple";
  234. }
  235. else if (val == "gaussian,staple" || val == "GAUSSIAN,STAPLE" || val == "Gaussian,Staple" ||
  236. val == "staple,gaussian" || val == "STAPLE,GAUSSIAN" || val == "Staple,Gaussian") {
  237. this->fusion_criteria = "gaussian_and_staple";
  238. }
  239. }
  240. else if (key == "distance_map_algorithm") {
  241. this->distance_map_algorithm = val;
  242. }
  243. else if (key == "minimum_similarity") {
  244. this->minsim_values = val;
  245. }
  246. else if (key == "rho_values") {
  247. this->rho_values = val;
  248. }
  249. else if (key == "sigma_values") {
  250. this->sigma_values = val;
  251. }
  252. else if (key == "threshold_values") {
  253. this->threshold_values = val;
  254. }
  255. else if (key == "confidence_weight") {
  256. this->confidence_weight = val;
  257. }
  258. else if (key == "training_dir") {
  259. this->training_dir = val;
  260. }
  261. else if (key == "write_distance_map_files") {
  262. if (val == "0") {
  263. this->write_distance_map_files = false;
  264. }
  265. }
  266. else if (key == "write_thresholded_files") {
  267. if (val == "0") {
  268. this->write_thresholded_files = false;
  269. }
  270. }
  271. else if (key == "write_weight_files") {
  272. if (val == "0") {
  273. this->write_weight_files = false;
  274. }
  275. }
  276. else if (key == "write_warped_images") {
  277. if (val == "0") {
  278. this->write_warped_images = false;
  279. }
  280. }
  281. else if (key == "write_warped_structures") {
  282. if (val == "0") {
  283. this->write_warped_structures = false;
  284. }
  285. }
  286. else {
  287. goto error_exit;
  288. }
  289. }
  290. /* [REGISTRATION] */
  291. if (section == "REGISTRATION") {
  292. if (key == "registration_config") {
  293. this->registration_config = val;
  294. }
  295. else {
  296. goto error_exit;
  297. }
  298. }
  299. /* [STRUCTURES] */
  300. if (section == "STRUCTURES") {
  301. /* Add key to list of structures */
  302. this->structure_map[key] = key;
  303. if (val != "") {
  304. /* Key/value pair, so add for renaming */
  305. this->structure_map[val] = key;
  306. }
  307. /* There is no filtering of structures values */
  308. }
  309. /* [LABELING] */
  310. if (section == "LABELING") {
  311. if (key == "input") {
  312. this->labeling_input_fn = val;
  313. }
  314. else if (key == "output") {
  315. this->labeling_output_fn = val;
  316. }
  317. else {
  318. goto error_exit;
  319. }
  320. }
  321. /* [OPTIMIZATION-RESULT] */
  322. if (section == "OPTIMIZATION_RESULT") {
  323. if (key == "registration") {
  324. this->optimization_result_reg = val;
  325. }
  326. else if (key == "gaussian_weighting_voting_rho") {
  327. sscanf (val.c_str(), "%g", &this->optimization_result_seg_rho);
  328. }
  329. else if (key == "gaussian_weighting_voting_sigma") {
  330. sscanf (val.c_str(), "%g", &this->optimization_result_seg_sigma);
  331. }
  332. else if (key == "gaussian_weighting_voting_minsim") {
  333. sscanf (val.c_str(), "%g", &this->optimization_result_seg_minsim);
  334. }
  335. else if (key == "optimization_result_confidence_weight") {
  336. sscanf (val.c_str(), "%g", &this->optimization_result_confidence_weight);
  337. }
  338. else if (key == "gaussian_weighting_voting_thresh") {
  339. this->optimization_result_seg_thresh = val;
  340. }
  341. else {
  342. goto error_exit;
  343. }
  344. }
  345. return 0;
  346. error_exit:
  347. print_and_exit ("Unknown (sec,key,val) combination: (%s,%s,%s)\n",
  348. section.c_str(), key.c_str(), val.c_str());
  349. return -1;
  350. #endif
  351. return PLM_SUCCESS;
  352. }
  353. void
  354. Autolabel_parms::parse_command_file ()
  355. {
  356. Autolabel_parms_parser mpp (this);
  357. /* Parse the main config file */
  358. mpp.parse_config_file (this->cmd_file_fn);
  359. }