Alternaria
fit cylinders and ellipsoids to fungus
|
00001 /* 00002 * This work is licensed under a Creative Commons 00003 * Attribution-Noncommercial-Share Alike 3.0 United States License. 00004 * 00005 * http://creativecommons.org/licenses/by-nc-sa/3.0/us/ 00006 * 00007 * You are free: 00008 * 00009 * to Share - to copy, distribute, display, and perform the work 00010 * to Remix - to make derivative works 00011 * 00012 * Under the following conditions: 00013 * 00014 * Attribution. You must attribute the work in the manner specified by the 00015 * author or licensor (but not in any way that suggests that they endorse you 00016 * or your use of the work). 00017 * 00018 * Noncommercial. You may not use this work for commercial purposes. 00019 * 00020 * Share Alike. If you alter, transform, or build upon this work, you may 00021 * distribute the resulting work only under the same or similar license to 00022 * this one. 00023 * 00024 * For any reuse or distribution, you must make clear to others the license 00025 * terms of this work. The best way to do this is by including this header. 00026 * 00027 * Any of the above conditions can be waived if you get permission from the 00028 * copyright holder. 00029 * 00030 * Apart from the remix rights granted under this license, nothing in this 00031 * license impairs or restricts the author's moral rights. 00032 */ 00033 00034 00046 #include <config.h> 00047 00048 #include <cstdlib> 00049 #include <cmath> 00050 #include <cassert> 00051 #include <iostream> 00052 #include <iomanip> 00053 00054 #include <inttypes.h> 00055 00056 #include <jwsc/base/limits.h> 00057 #include <jwsc/math/constants.h> 00058 #include <jwsc/prob/pdf.h> 00059 #include <jwsc/prob/pmf.h> 00060 00061 #include <jwsc++/base/exception.h> 00062 00063 #include "sampler.h" 00064 #include "structure.h" 00065 #include "hypha.h" 00066 #include "spore.h" 00067 #include "alternaria_model.h" 00068 #include "alternaria_moves.h" 00069 00070 00071 using std::isinf; 00072 using std::isnan; 00073 using namespace jwsc; 00074 using jwscxx::base::Exception; 00075 using jwscxx::base::Arg_error; 00076 00077 00083 bool Apical_hypha_birth_death_move::run_1(Sampler_move_parameters* params) 00084 throw (Exception) 00085 { 00086 params->init_proposals(); 00087 00088 Structure* parent; 00089 Apical_hypha* hypha; 00090 const Apical_hypha_density* density; 00091 00092 density = params->alternaria_proposal->get_apical_hypha_d(); 00093 00094 try 00095 { 00096 size_t level = params->alternaria_proposal->get_uniform_random_level(); 00097 parent = params->alternaria_proposal->get_random_terminal(level); 00098 00099 try 00100 { 00101 hypha = density->sample(parent); 00102 params->alternaria_proposal->add_apical_hypha(hypha); 00103 } 00104 catch (Exception e) 00105 { 00106 return false; 00107 } 00108 } 00109 catch (Exception e) 00110 { 00111 try 00112 { 00113 hypha = density->sample(params->alternaria->get_theta(), 00114 params->alternaria->get_psi(), 00115 params->alternaria->get_base_level()); 00116 params->alternaria_proposal->add_apical_hypha(hypha); 00117 } 00118 catch (Exception ee) 00119 { 00120 return false; 00121 } 00122 } 00123 00124 if (params->alternaria_proposal->update_scene(params->psf_proposal, 00125 params->imaging_proposal)) 00126 { 00127 return false; 00128 } 00129 00130 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 00131 params->psf_proposal, params->imaging_proposal, params->data, 00132 params->data_avg); 00133 00134 double hypha_log_prob = hypha->get_log_prob(); 00135 00136 double birth_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 00137 : log(JWSC_MIN_LOG_ARG); 00138 00139 double death_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 00140 : log(JWSC_MIN_LOG_ARG); 00141 00142 double log_alpha = params->ll_proposal - params->ll; 00143 log_alpha += params->alternaria_proposal->get_log_prob(); 00144 log_alpha -= params->alternaria->get_log_prob(); 00145 log_alpha -= hypha_log_prob; 00146 log_alpha -= birth_move_log_prob; 00147 log_alpha += death_move_log_prob; 00148 00149 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 00150 assert(!isinf(log_alpha) && !isnan(log_alpha)); 00151 #endif 00152 00153 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 00154 00155 if (log_u <= log_alpha) 00156 { 00157 return true; 00158 } 00159 return false; 00160 } 00161 00162 00168 bool Apical_hypha_birth_death_move::run_2(Sampler_move_parameters* params) 00169 throw (Exception) 00170 { 00171 params->init_proposals(); 00172 00173 Apical_hypha* hypha; 00174 00175 try 00176 { 00177 size_t level = params->alternaria_proposal->get_uniform_random_level(); 00178 hypha = params->alternaria_proposal 00179 ->get_random_terminal_apical_hypha(level); 00180 00181 // Check that hypha doesn't have any laterals. 00182 if (hypha->get_laterals().size() != 0) 00183 { 00184 return false; 00185 } 00186 00187 // TEMPORARY Check that this is not the root. 00188 if (!(hypha->get_parent())) 00189 { 00190 return false; 00191 } 00192 } 00193 catch (Exception e) 00194 { 00195 return false; 00196 } 00197 00198 double hypha_log_prob = hypha->get_log_prob(); 00199 00200 try 00201 { 00202 params->alternaria_proposal->remove_apical_hypha(hypha); 00203 } 00204 catch (Arg_error e) 00205 { 00206 return false; 00207 } 00208 00209 if (params->alternaria_proposal->update_scene(params->psf_proposal, 00210 params->imaging_proposal)) 00211 { 00212 return false; 00213 } 00214 00215 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 00216 params->psf_proposal, params->imaging_proposal, params->data, 00217 params->data_avg); 00218 00219 double birth_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 00220 : log(JWSC_MIN_LOG_ARG); 00221 00222 double death_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 00223 : log(JWSC_MIN_LOG_ARG); 00224 00225 double log_alpha = params->ll_proposal - params->ll; 00226 log_alpha += params->alternaria_proposal->get_log_prob(); 00227 log_alpha -= params->alternaria->get_log_prob(); 00228 log_alpha += hypha_log_prob; 00229 log_alpha += birth_move_log_prob; 00230 log_alpha -= death_move_log_prob; 00231 00232 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 00233 assert(!isinf(log_alpha) && !isnan(log_alpha)); 00234 #endif 00235 00236 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 00237 00238 if (log_u <= log_alpha) 00239 { 00240 return true; 00241 } 00242 return false; 00243 } 00244 00245 00246 00247 00256 bool Spore_birth_death_move::run_1(Sampler_move_parameters* params) 00257 throw (Exception) 00258 { 00259 params->init_proposals(); 00260 00261 Structure* parent; 00262 Spore* spore; 00263 const Spore_density* density; 00264 00265 density = params->alternaria_proposal->get_spore_d(); 00266 00267 try 00268 { 00269 size_t level = params->alternaria_proposal->get_uniform_random_level(); 00270 if (level == 0) 00271 { 00272 return false; 00273 } 00274 00275 parent = params->alternaria_proposal->get_random_terminal(level); 00276 00277 try 00278 { 00279 spore = density->sample(parent); 00280 params->alternaria_proposal->add_spore(spore); 00281 } 00282 catch (Exception e) 00283 { 00284 return false; 00285 } 00286 } 00287 catch (Exception e) 00288 { 00289 try 00290 { 00291 spore = density->sample(params->alternaria->get_theta(), 00292 params->alternaria->get_psi(), 00293 params->alternaria->get_base_level()); 00294 params->alternaria_proposal->add_spore(spore); 00295 } 00296 catch (Exception ee) 00297 { 00298 return false; 00299 } 00300 } 00301 00302 if (params->alternaria_proposal->update_scene(params->psf_proposal, 00303 params->imaging_proposal)) 00304 { 00305 return false; 00306 } 00307 00308 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 00309 params->psf_proposal, params->imaging_proposal, params->data, 00310 params->data_avg); 00311 00312 double spore_log_prob = spore->get_log_prob(); 00313 00314 double birth_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 00315 : log(JWSC_MIN_LOG_ARG); 00316 00317 double death_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 00318 : log(JWSC_MIN_LOG_ARG); 00319 00320 double log_alpha = params->ll_proposal - params->ll; 00321 log_alpha += params->alternaria_proposal->get_log_prob(); 00322 log_alpha -= params->alternaria->get_log_prob(); 00323 log_alpha -= spore_log_prob; 00324 log_alpha -= birth_move_log_prob; 00325 log_alpha += death_move_log_prob; 00326 00327 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 00328 assert(!isinf(log_alpha) && !isnan(log_alpha)); 00329 #endif 00330 00331 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 00332 00333 if (log_u <= log_alpha) 00334 { 00335 return true; 00336 } 00337 return false; 00338 } 00339 00340 00346 bool Spore_birth_death_move::run_2(Sampler_move_parameters* params) 00347 throw (Exception) 00348 { 00349 params->init_proposals(); 00350 00351 Spore* spore; 00352 00353 try 00354 { 00355 size_t level = params->alternaria_proposal->get_uniform_random_level(); 00356 spore = params->alternaria_proposal->get_random_terminal_spore(level); 00357 00358 // Check that spore doesn't have any laterals. 00359 if (spore->get_laterals().size() != 0) 00360 { 00361 return false; 00362 } 00363 00364 // TEMPORARY Check that this is not the root. 00365 if (!(spore->get_parent())) 00366 { 00367 return false; 00368 } 00369 } 00370 catch (Exception e) 00371 { 00372 return false; 00373 } 00374 00375 double spore_log_prob = spore->get_log_prob(); 00376 00377 try 00378 { 00379 params->alternaria_proposal->remove_spore(spore); 00380 } 00381 catch (Arg_error e) 00382 { 00383 return false; 00384 } 00385 00386 if (params->alternaria_proposal->update_scene(params->psf_proposal, 00387 params->imaging_proposal)) 00388 { 00389 return false; 00390 } 00391 00392 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 00393 params->psf_proposal, params->imaging_proposal, params->data, 00394 params->data_avg); 00395 00396 double birth_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 00397 : log(JWSC_MIN_LOG_ARG); 00398 00399 double death_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 00400 : log(JWSC_MIN_LOG_ARG); 00401 00402 double log_alpha = params->ll_proposal - params->ll; 00403 log_alpha += params->alternaria_proposal->get_log_prob(); 00404 log_alpha -= params->alternaria->get_log_prob(); 00405 log_alpha += spore_log_prob; 00406 log_alpha += birth_move_log_prob; 00407 log_alpha -= death_move_log_prob; 00408 00409 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 00410 assert(!isinf(log_alpha) && !isnan(log_alpha)); 00411 #endif 00412 00413 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 00414 00415 if (log_u <= log_alpha) 00416 { 00417 return true; 00418 } 00419 return false; 00420 } 00421 00439 bool Spore_transition_apical_move::run_1(Sampler_move_parameters* params) 00440 throw (Exception) 00441 { 00442 params->init_proposals(); 00443 00444 Apical_hypha* hypha; 00445 Spore* spore = 0; 00446 const Spore_density* density; 00447 double apical_to_spore_proposal_log_prob; 00448 double spore_to_apical_proposal_log_prob; 00449 00450 density = params->alternaria_proposal->get_spore_d(); 00451 00452 try 00453 { 00454 size_t level = params->alternaria_proposal->get_uniform_random_level(); 00455 if (level == 0) 00456 { 00457 return false; 00458 } 00459 00460 hypha = params->alternaria_proposal->get_random_apical_hypha(level); 00461 spore_to_apical_proposal_log_prob = hypha->get_log_prob(); 00462 00463 spore = new Spore(*hypha, density->sample_width(), density); 00464 } 00465 catch (Exception e) 00466 { 00467 return false; 00468 } 00469 00470 apical_to_spore_proposal_log_prob = spore->get_log_prob(); 00471 00472 try 00473 { 00474 params->alternaria_proposal->replace_apical_hypha_with_spore(hypha, 00475 spore); 00476 } 00477 catch (Exception e) 00478 { 00479 // We are going to try and handle this now in the sampler by catching 00480 // the exception and rejecting the proposal. 00481 throw e; 00482 } 00483 00484 if (params->alternaria_proposal->update_scene(params->psf_proposal, 00485 params->imaging_proposal)) 00486 { 00487 return false; 00488 } 00489 00490 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 00491 params->psf_proposal, params->imaging_proposal, params->data, 00492 params->data_avg); 00493 00494 double apical_to_spore_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) 00495 ? log(prob_1) 00496 : log(JWSC_MIN_LOG_ARG); 00497 00498 double spore_to_apical_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) 00499 ? log(prob_2) 00500 : log(JWSC_MIN_LOG_ARG); 00501 00502 double log_alpha = params->ll_proposal - params->ll; 00503 log_alpha += params->alternaria_proposal->get_log_prob(); 00504 log_alpha -= params->alternaria->get_log_prob(); 00505 log_alpha -= apical_to_spore_proposal_log_prob; 00506 log_alpha += spore_to_apical_proposal_log_prob; 00507 log_alpha -= apical_to_spore_move_log_prob; 00508 log_alpha += spore_to_apical_move_log_prob; 00509 00510 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 00511 assert(!isinf(log_alpha) && !isnan(log_alpha)); 00512 #endif 00513 00514 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 00515 00516 if (log_u <= log_alpha) 00517 { 00518 return true; 00519 } 00520 return false; 00521 } 00522 00523 00529 bool Spore_transition_apical_move::run_2(Sampler_move_parameters* params) 00530 throw (Exception) 00531 { 00532 params->init_proposals(); 00533 00534 Apical_hypha* hypha = 0; 00535 Spore* spore; 00536 const Apical_hypha_density* density; 00537 double spore_to_apical_proposal_log_prob; 00538 double apical_to_spore_proposal_log_prob; 00539 00540 density = params->alternaria_proposal->get_apical_hypha_d(); 00541 00542 try 00543 { 00544 size_t level = params->alternaria_proposal->get_uniform_random_level(); 00545 spore = params->alternaria_proposal->get_random_spore(level); 00546 apical_to_spore_proposal_log_prob = spore->get_log_prob(); 00547 00548 hypha = new Apical_hypha(*spore, density->sample_width(), density); 00549 } 00550 catch (Exception e) 00551 { 00552 return false; 00553 } 00554 00555 spore_to_apical_proposal_log_prob = hypha->get_log_prob(); 00556 00557 try 00558 { 00559 params->alternaria_proposal->replace_spore_with_apical_hypha(spore, 00560 hypha); 00561 } 00562 catch (Exception e) 00563 { 00564 // We are going to try and handle this now in the sampler by catching 00565 // the exception and rejecting the proposal. 00566 throw e; 00567 } 00568 00569 if (params->alternaria_proposal->update_scene(params->psf_proposal, 00570 params->imaging_proposal)) 00571 { 00572 return false; 00573 } 00574 00575 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 00576 params->psf_proposal, params->imaging_proposal, params->data, 00577 params->data_avg); 00578 00579 double apical_to_spore_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) 00580 ? log(prob_1) 00581 : log(JWSC_MIN_LOG_ARG); 00582 00583 double spore_to_apical_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) 00584 ? log(prob_2) 00585 : log(JWSC_MIN_LOG_ARG); 00586 00587 double log_alpha = params->ll_proposal - params->ll; 00588 log_alpha += params->alternaria_proposal->get_log_prob(); 00589 log_alpha -= params->alternaria->get_log_prob(); 00590 log_alpha += apical_to_spore_proposal_log_prob; 00591 log_alpha -= spore_to_apical_proposal_log_prob; 00592 log_alpha += apical_to_spore_move_log_prob; 00593 log_alpha -= spore_to_apical_move_log_prob; 00594 00595 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 00596 assert(!isinf(log_alpha) && !isnan(log_alpha)); 00597 #endif 00598 00599 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 00600 00601 if (log_u <= log_alpha) 00602 { 00603 return true; 00604 } 00605 return false; 00606 } 00607 00624 bool Spore_split_merge_2_apical_move::run_1(Sampler_move_parameters* params) 00625 throw (Exception) 00626 { 00627 params->init_proposals(); 00628 00629 Spore* spore; 00630 00631 Apical_hypha* split_1 = 0; 00632 double split_1_log_prob; 00633 00634 Apical_hypha* split_2 = 0; 00635 double split_2_log_prob; 00636 00637 Spore* merge = 0; 00638 double merge_log_prob; 00639 00640 try 00641 { 00642 size_t level = params->alternaria_proposal->get_uniform_random_level(); 00643 spore = params->alternaria_proposal->get_random_spore(level); 00644 00645 const Apical_hypha_density* hypha_d = params->alternaria_proposal 00646 ->get_apical_hypha_d(); 00647 const Spore_density* spore_d = params->alternaria_proposal 00648 ->get_spore_d(); 00649 00650 propose_1st_split(&split_1, &split_1_log_prob, hypha_d, spore); 00651 propose_2nd_split(&split_2, &split_2_log_prob, split_1); 00652 00653 propose_merge(&merge, &merge_log_prob, spore->get_parent(), spore_d, 00654 split_1, split_2); 00655 00656 params->alternaria_proposal->split_spore_into_2_apical(spore, 00657 split_1, split_2); 00658 } 00659 catch (Exception e) 00660 { 00661 delete merge; 00662 delete split_1; 00663 delete split_2; 00664 return false; 00665 } 00666 00667 delete merge; 00668 delete split_1; 00669 delete split_2; 00670 00671 if (params->alternaria_proposal->update_scene(params->psf_proposal, 00672 params->imaging_proposal)) 00673 { 00674 return false; 00675 } 00676 00677 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 00678 params->psf_proposal, params->imaging_proposal, params->data, 00679 params->data_avg); 00680 00681 double split_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 00682 : log(JWSC_MIN_LOG_ARG); 00683 00684 double merge_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 00685 : log(JWSC_MIN_LOG_ARG); 00686 00687 double log_alpha = params->ll_proposal - params->ll; 00688 log_alpha += params->alternaria_proposal->get_log_prob(); 00689 log_alpha -= params->alternaria->get_log_prob(); 00690 log_alpha += merge_log_prob; 00691 log_alpha -= split_1_log_prob; 00692 log_alpha -= split_2_log_prob; 00693 log_alpha -= split_move_log_prob; 00694 log_alpha += merge_move_log_prob; 00695 00696 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 00697 assert(!isinf(log_alpha) && !isnan(log_alpha)); 00698 #endif 00699 00700 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 00701 00702 if (log_u <= log_alpha) 00703 { 00704 return true; 00705 } 00706 return false; 00707 } 00708 00709 00715 bool Spore_split_merge_2_apical_move::run_2(Sampler_move_parameters* params) 00716 throw (Exception) 00717 { 00718 params->init_proposals(); 00719 00720 Apical_hypha* hypha_1; 00721 Apical_hypha* hypha_2; 00722 00723 Spore* merge = 0; 00724 double merge_log_prob; 00725 00726 Apical_hypha* split_1 = 0; 00727 double split_1_log_prob; 00728 00729 Apical_hypha* split_2 = 0; 00730 double split_2_log_prob; 00731 00732 try 00733 { 00734 size_t level = params->alternaria_proposal->get_uniform_random_level(); 00735 hypha_1 = params->alternaria_proposal->get_random_apical_hypha(level); 00736 if (!(hypha_2 = dynamic_cast<Apical_hypha*>(hypha_1->get_apical()))) 00737 { 00738 return false; 00739 } 00740 00741 const Apical_hypha_density* hypha_d = params->alternaria_proposal 00742 ->get_apical_hypha_d(); 00743 const Spore_density* spore_d = params->alternaria_proposal 00744 ->get_spore_d(); 00745 00746 propose_merge(&merge, &merge_log_prob, hypha_1->get_parent(), spore_d, 00747 hypha_1, hypha_2); 00748 00749 propose_1st_split(&split_1, &split_1_log_prob, hypha_d, merge); 00750 propose_2nd_split(&split_2, &split_2_log_prob, split_1); 00751 00752 params->alternaria_proposal->merge_2_apical_with_spore(hypha_1, merge); 00753 } 00754 catch (Exception e) 00755 { 00756 delete split_1; 00757 delete split_2; 00758 delete merge; 00759 return false; 00760 } 00761 00762 delete split_1; 00763 delete split_2; 00764 delete merge; 00765 00766 if (params->alternaria_proposal->update_scene(params->psf_proposal, 00767 params->imaging_proposal)) 00768 { 00769 return false; 00770 } 00771 00772 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 00773 params->psf_proposal, params->imaging_proposal, params->data, 00774 params->data_avg); 00775 00776 double split_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 00777 : log(JWSC_MIN_LOG_ARG); 00778 00779 double merge_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 00780 : log(JWSC_MIN_LOG_ARG); 00781 00782 double log_alpha = params->ll_proposal - params->ll; 00783 log_alpha += params->alternaria_proposal->get_log_prob(); 00784 log_alpha -= params->alternaria->get_log_prob(); 00785 log_alpha += split_1_log_prob; 00786 log_alpha += split_2_log_prob; 00787 log_alpha -= merge_log_prob; 00788 log_alpha += split_move_log_prob; 00789 log_alpha -= merge_move_log_prob; 00790 00791 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 00792 assert(!isinf(log_alpha) && !isnan(log_alpha)); 00793 #endif 00794 00795 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 00796 00797 if (log_u <= log_alpha) 00798 { 00799 return true; 00800 } 00801 return false; 00802 } 00803 00804 00812 void Spore_split_merge_2_apical_move::propose_1st_split 00813 ( 00814 Apical_hypha** proposal_out, 00815 double* log_prob_out, 00816 const Apical_hypha_density* density, 00817 const Spore* spore 00818 ) 00819 throw (Arg_error) 00820 { 00821 Apical_hypha* proposal; 00822 double log_prob; 00823 00824 assert(*proposal_out == 0); 00825 00826 *proposal_out = density->sample(); 00827 proposal = *proposal_out; 00828 log_prob = proposal->get_log_prob(); 00829 00830 float mu, sigma; 00831 float min, max; 00832 double p_1, p_2; 00833 double delta; 00834 00835 const Structure* parent = spore->get_parent(); 00836 00837 // Set a parent-dependant width. 00838 if (parent && (dynamic_cast<const Apical_hypha*>(parent) || 00839 dynamic_cast<const Lateral_hypha*>(parent))) 00840 { 00841 float width = proposal->get_width(); 00842 00843 // Take out the width log prob. 00844 mu = density->get_width().get_mu(); 00845 sigma = density->get_width().get_sigma(); 00846 min = density->get_width().get_min(); 00847 max = density->get_width().get_max(); 00848 delta = (max - min) / 1000; 00849 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 00850 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 00851 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 00852 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 00853 00854 float dwidth = density->sample_dwidth(); 00855 proposal->set_width(parent->get_width() + dwidth); 00856 00857 // Put in the differential width log prob. 00858 mu = density->get_dwidth().get_mu(); 00859 sigma = density->get_dwidth().get_sigma(); 00860 min = density->get_dwidth().get_min(); 00861 max = density->get_dwidth().get_max(); 00862 delta = (max - min) / 1000; 00863 p_1 = integrate_gaussian_pdf_d(mu, sigma, dwidth, dwidth+delta); 00864 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 00865 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 00866 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 00867 } 00868 00869 float theta = proposal->get_theta(); 00870 00871 // Take out the theta log prob. 00872 mu = density->get_theta().get_mu(); 00873 sigma = density->get_theta().get_sigma(); 00874 min = density->get_theta().get_min(); 00875 max = density->get_theta().get_max(); 00876 delta = (max - min) / 1000; 00877 p_1 = integrate_gaussian_pdf_d(mu, sigma, theta, theta+delta); 00878 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 00879 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 00880 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 00881 00882 // Re-sample a theta close to the original. 00883 mu = spore->get_theta(); 00884 sigma = density->get_theta().get_sigma(); 00885 min = density->get_theta().get_min(); 00886 max = density->get_theta().get_max(); 00887 theta = sample_gaussian_pdf_d(mu, sigma, min, max); 00888 proposal->set_theta(theta); 00889 00890 // Put in the re-sampled theta log prob. 00891 mu = density->get_theta().get_mu(); 00892 sigma = density->get_theta().get_sigma(); 00893 min = density->get_theta().get_min(); 00894 max = density->get_theta().get_max(); 00895 delta = (max - min) / 1000; 00896 p_1 = integrate_gaussian_pdf_d(mu, sigma, theta, theta+delta); 00897 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 00898 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 00899 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 00900 00901 float psi; 00902 00903 // Take out the psi log prob. 00904 min = density->get_psi().get_min(); 00905 max = density->get_psi().get_max(); 00906 p_1 = max - min; 00907 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 00908 00909 // Re-sample a psi close to the original (using a Gaussian). 00910 mu = spore->get_psi(); 00911 sigma = density->get_theta().get_sigma(); // Not a bug. 00912 min = density->get_psi().get_min(); 00913 max = density->get_psi().get_max(); 00914 psi = sample_gaussian_pdf_d(mu, sigma, min, max); 00915 proposal->set_psi(psi); 00916 00917 // Put in the re-sampled psi log prob. 00918 delta = (max - min) / 1000; 00919 p_1 = integrate_gaussian_pdf_d(mu, sigma, psi, psi+delta); 00920 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 00921 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 00922 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 00923 00924 float opacity = proposal->get_opacity(); 00925 00926 // Take out the opacity log prob. 00927 mu = density->get_opacity().get_mu(); 00928 sigma = density->get_opacity().get_sigma(); 00929 min = density->get_opacity().get_min(); 00930 max = density->get_opacity().get_max(); 00931 delta = (max - min) / 1000; 00932 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 00933 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 00934 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 00935 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 00936 00937 // Re-sample a opacity close to the original. 00938 mu = spore->get_opacity(); 00939 sigma = density->get_opacity().get_sigma(); 00940 min = density->get_opacity().get_min(); 00941 max = density->get_opacity().get_max(); 00942 opacity = sample_gaussian_pdf_d(mu, sigma, min, max); 00943 proposal->set_opacity(opacity); 00944 00945 // Put in the re-sampled opacity log prob. 00946 mu = density->get_opacity().get_mu(); 00947 sigma = density->get_opacity().get_sigma(); 00948 min = density->get_opacity().get_min(); 00949 max = density->get_opacity().get_max(); 00950 delta = (max - min) / 1000; 00951 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 00952 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 00953 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 00954 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 00955 00956 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 00957 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 00958 #endif 00959 00960 *log_prob_out = log_prob; 00961 } 00962 00963 00970 void Spore_split_merge_2_apical_move::propose_2nd_split 00971 ( 00972 Apical_hypha** proposal_out, 00973 double* log_prob_out, 00974 const Apical_hypha* split_1 00975 ) 00976 throw (Arg_error) 00977 { 00978 Apical_hypha* proposal; 00979 double log_prob; 00980 00981 assert(*proposal_out == 0); 00982 00983 const Apical_hypha_density* density = split_1->get_density(); 00984 00985 *proposal_out = density->sample(); 00986 proposal = *proposal_out; 00987 log_prob = proposal->get_log_prob(); 00988 00989 float mu, sigma; 00990 float min, max; 00991 double p_1, p_2; 00992 double delta; 00993 float width = proposal->get_width(); 00994 00995 // Take out the width log prob. 00996 mu = density->get_width().get_mu(); 00997 sigma = density->get_width().get_sigma(); 00998 min = density->get_width().get_min(); 00999 max = density->get_width().get_max(); 01000 delta = (max - min) / 1000; 01001 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 01002 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01003 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01004 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01005 01006 float dwidth = density->sample_dwidth(); 01007 proposal->set_width(split_1->get_width() + dwidth); 01008 01009 // Put in the differential width log prob. 01010 mu = density->get_dwidth().get_mu(); 01011 sigma = density->get_dwidth().get_sigma(); 01012 min = density->get_dwidth().get_min(); 01013 max = density->get_dwidth().get_max(); 01014 delta = (max - min) / 1000; 01015 p_1 = integrate_gaussian_pdf_d(mu, sigma, dwidth, dwidth+delta); 01016 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01017 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01018 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01019 01020 float opacity = proposal->get_opacity(); 01021 01022 // Take out the opacity log prob. 01023 mu = density->get_opacity().get_mu(); 01024 sigma = density->get_opacity().get_sigma(); 01025 min = density->get_opacity().get_min(); 01026 max = density->get_opacity().get_max(); 01027 delta = (max - min) / 1000; 01028 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 01029 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01030 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01031 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01032 01033 // Re-sample a opacity close to the original. 01034 mu = split_1->get_opacity(); 01035 sigma = density->get_opacity().get_sigma(); 01036 min = density->get_opacity().get_min(); 01037 max = density->get_opacity().get_max(); 01038 opacity = sample_gaussian_pdf_d(mu, sigma, min, max); 01039 proposal->set_opacity(opacity); 01040 01041 // Put in the re-sampled opacity log prob. 01042 mu = density->get_opacity().get_mu(); 01043 sigma = density->get_opacity().get_sigma(); 01044 min = density->get_opacity().get_min(); 01045 max = density->get_opacity().get_max(); 01046 delta = (max - min) / 1000; 01047 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 01048 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01049 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01050 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01051 01052 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 01053 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 01054 #endif 01055 01056 *log_prob_out = log_prob; 01057 } 01058 01059 01071 void Spore_split_merge_2_apical_move::propose_merge 01072 ( 01073 Spore** proposal_out, 01074 double* log_prob_out, 01075 const Structure* parent, 01076 const Spore_density* density, 01077 const Apical_hypha* hypha_1, 01078 const Apical_hypha* hypha_2 01079 ) 01080 throw (Arg_error) 01081 { 01082 Spore* proposal; 01083 double log_prob; 01084 01085 assert(*proposal_out == 0); 01086 01087 *proposal_out = density->sample(); 01088 proposal = *proposal_out; 01089 log_prob = proposal->get_log_prob(); 01090 01091 float mu, sigma; 01092 float min, max; 01093 double p_1, p_2; 01094 double delta; 01095 01096 float opacity = proposal->get_opacity(); 01097 01098 // Take out the opacity log prob. 01099 mu = density->get_opacity().get_mu(); 01100 sigma = density->get_opacity().get_sigma(); 01101 min = density->get_opacity().get_min(); 01102 max = density->get_opacity().get_max(); 01103 delta = (max - min) / 1000; 01104 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 01105 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01106 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01107 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01108 01109 // Re-sample a opacity close to the original. 01110 opacity = 0.5f*hypha_1->get_opacity() + 0.5f*hypha_2->get_opacity(); 01111 sigma = density->get_opacity().get_sigma(); 01112 min = -6.0f*sigma; 01113 max = 6.0f*sigma; 01114 opacity += sample_gaussian_pdf_d(0, sigma, min, max); 01115 proposal->set_opacity(opacity); 01116 01117 // Put in the re-sampled opacity log prob. 01118 mu = density->get_opacity().get_mu(); 01119 sigma = density->get_opacity().get_sigma(); 01120 min = density->get_opacity().get_min(); 01121 max = density->get_opacity().get_max(); 01122 delta = (max - min) / 1000; 01123 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 01124 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01125 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01126 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01127 01128 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 01129 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 01130 #endif 01131 01132 *proposal_out = proposal; 01133 *log_prob_out = log_prob; 01134 } 01135 01153 bool Spore_split_merge_spore_move::run_1 01154 ( 01155 Sampler_move_parameters* params 01156 ) 01157 throw (Exception) 01158 { 01159 params->init_proposals(); 01160 01161 Spore* spore; 01162 01163 Spore* split_1 = 0; 01164 double split_1_log_prob; 01165 01166 Spore* split_2 = 0; 01167 double split_2_log_prob; 01168 01169 Spore* merge = 0; 01170 double merge_log_prob; 01171 01172 try 01173 { 01174 size_t level = params->alternaria_proposal->get_uniform_random_level(); 01175 spore = params->alternaria_proposal->get_random_spore(level); 01176 01177 propose_1st_split(&split_1, &split_1_log_prob, spore); 01178 propose_2nd_split(&split_2, &split_2_log_prob, split_1); 01179 01180 propose_merge(&merge, &merge_log_prob, spore->get_parent(), split_1, 01181 split_2); 01182 01183 params->alternaria_proposal->split_spore_into_spore(spore, 01184 split_1, split_2); 01185 } 01186 catch (Exception e) 01187 { 01188 delete merge; 01189 delete split_1; 01190 delete split_2; 01191 return false; 01192 } 01193 01194 delete merge; 01195 delete split_1; 01196 delete split_2; 01197 01198 if (params->alternaria_proposal->update_scene(params->psf_proposal, 01199 params->imaging_proposal)) 01200 { 01201 return false; 01202 } 01203 01204 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 01205 params->psf_proposal, params->imaging_proposal, params->data, 01206 params->data_avg); 01207 01208 double split_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 01209 : log(JWSC_MIN_LOG_ARG); 01210 01211 double merge_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 01212 : log(JWSC_MIN_LOG_ARG); 01213 01214 double log_alpha = params->ll_proposal - params->ll; 01215 log_alpha += params->alternaria_proposal->get_log_prob(); 01216 log_alpha -= params->alternaria->get_log_prob(); 01217 log_alpha += merge_log_prob; 01218 log_alpha -= split_1_log_prob; 01219 log_alpha -= split_2_log_prob; 01220 log_alpha -= split_move_log_prob; 01221 log_alpha += merge_move_log_prob; 01222 01223 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 01224 assert(!isinf(log_alpha) && !isnan(log_alpha)); 01225 #endif 01226 01227 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 01228 01229 if (log_u <= log_alpha) 01230 { 01231 return true; 01232 } 01233 return false; 01234 } 01235 01236 01242 bool Spore_split_merge_spore_move::run_2 01243 ( 01244 Sampler_move_parameters* params 01245 ) 01246 throw (Exception) 01247 { 01248 params->init_proposals(); 01249 01250 Spore* spore; 01251 Apical_structure* apical; 01252 01253 Spore* merge = 0; 01254 double merge_log_prob; 01255 01256 Spore* split_1 = 0; 01257 double split_1_log_prob; 01258 01259 Spore* split_2 = 0; 01260 double split_2_log_prob; 01261 01262 try 01263 { 01264 size_t level = params->alternaria_proposal->get_uniform_random_level(); 01265 spore = params->alternaria_proposal->get_random_spore(level); 01266 apical = spore->get_apical(); 01267 01268 if (!apical || !dynamic_cast<Spore*>(apical)) 01269 { 01270 return false; 01271 } 01272 01273 propose_merge(&merge, &merge_log_prob, spore->get_parent(), spore, 01274 (Spore*)apical); 01275 01276 propose_1st_split(&split_1, &split_1_log_prob, merge); 01277 propose_2nd_split(&split_2, &split_2_log_prob, split_1); 01278 01279 params->alternaria_proposal->merge_spore_with_spore(spore, 01280 merge); 01281 } 01282 catch (Exception e) 01283 { 01284 delete split_1; 01285 delete split_2; 01286 delete merge; 01287 return false; 01288 } 01289 01290 delete split_1; 01291 delete split_2; 01292 delete merge; 01293 01294 if (params->alternaria_proposal->update_scene(params->psf_proposal, 01295 params->imaging_proposal)) 01296 { 01297 return false; 01298 } 01299 01300 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 01301 params->psf_proposal, params->imaging_proposal, params->data, 01302 params->data_avg); 01303 01304 double split_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 01305 : log(JWSC_MIN_LOG_ARG); 01306 01307 double merge_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 01308 : log(JWSC_MIN_LOG_ARG); 01309 01310 double log_alpha = params->ll_proposal - params->ll; 01311 log_alpha += params->alternaria_proposal->get_log_prob(); 01312 log_alpha -= params->alternaria->get_log_prob(); 01313 log_alpha += split_1_log_prob; 01314 log_alpha += split_2_log_prob; 01315 log_alpha -= merge_log_prob; 01316 log_alpha += split_move_log_prob; 01317 log_alpha -= merge_move_log_prob; 01318 01319 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 01320 assert(!isinf(log_alpha) && !isnan(log_alpha)); 01321 #endif 01322 01323 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 01324 01325 if (log_u <= log_alpha) 01326 { 01327 return true; 01328 } 01329 return false; 01330 } 01331 01332 01339 void Spore_split_merge_spore_move::propose_1st_split 01340 ( 01341 Spore** proposal_out, 01342 double* log_prob_out, 01343 const Spore* spore 01344 ) 01345 throw (Arg_error) 01346 { 01347 Spore* proposal; 01348 double log_prob; 01349 01350 assert(*proposal_out == 0); 01351 01352 const Spore_density* density = spore->get_density(); 01353 01354 *proposal_out = density->sample(); 01355 proposal = *proposal_out; 01356 log_prob = proposal->get_log_prob(); 01357 01358 float mu, sigma; 01359 float min, max; 01360 double p_1, p_2; 01361 double delta; 01362 01363 float width = proposal->get_width(); 01364 01365 // Take out the width log prob. 01366 mu = density->get_width().get_mu(); 01367 sigma = density->get_width().get_sigma(); 01368 min = density->get_width().get_min(); 01369 max = density->get_width().get_max(); 01370 delta = (max - min) / 1000; 01371 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 01372 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01373 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01374 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01375 01376 // Re-sample a width close to the original. 01377 width = 0.65f*spore->get_width(); 01378 sigma = density->get_width().get_sigma(); 01379 min = -6.0f*sigma; 01380 max = 6.0f*sigma; 01381 width += sample_gaussian_pdf_d(0, sigma, min, max); 01382 proposal->set_width(width); 01383 01384 // Put in the re-sampled width log prob. 01385 mu = density->get_width().get_mu(); 01386 sigma = density->get_width().get_sigma(); 01387 min = density->get_width().get_min(); 01388 max = density->get_width().get_max(); 01389 delta = (max - min) / 1000; 01390 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 01391 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01392 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01393 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01394 01395 float theta = proposal->get_theta(); 01396 01397 // Take out the theta log prob. 01398 mu = density->get_theta().get_mu(); 01399 sigma = density->get_theta().get_sigma(); 01400 min = density->get_theta().get_min(); 01401 max = density->get_theta().get_max(); 01402 delta = (max - min) / 1000; 01403 p_1 = integrate_gaussian_pdf_d(mu, sigma, theta, theta+delta); 01404 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01405 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01406 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01407 01408 // Re-sample a theta close to the original. 01409 mu = spore->get_theta(); 01410 sigma = density->get_theta().get_sigma(); 01411 min = density->get_theta().get_min(); 01412 max = density->get_theta().get_max(); 01413 theta = sample_gaussian_pdf_d(mu, sigma, min, max); 01414 proposal->set_theta(theta); 01415 01416 // Put in the re-sampled theta log prob. 01417 mu = density->get_theta().get_mu(); 01418 sigma = density->get_theta().get_sigma(); 01419 min = density->get_theta().get_min(); 01420 max = density->get_theta().get_max(); 01421 delta = (max - min) / 1000; 01422 p_1 = integrate_gaussian_pdf_d(mu, sigma, theta, theta+delta); 01423 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01424 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01425 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01426 01427 float psi; 01428 01429 // Take out the psi log prob. 01430 min = density->get_psi().get_min(); 01431 max = density->get_psi().get_max(); 01432 p_1 = max - min; 01433 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01434 01435 // Re-sample a psi close to the original (using a Gaussian). 01436 mu = spore->get_psi(); 01437 sigma = density->get_theta().get_sigma(); // Not a bug. 01438 min = density->get_psi().get_min(); 01439 max = density->get_psi().get_max(); 01440 psi = sample_gaussian_pdf_d(mu, sigma, min, max); 01441 proposal->set_psi(psi); 01442 01443 // Put in the re-sampled psi log prob. 01444 delta = (max - min) / 1000; 01445 p_1 = integrate_gaussian_pdf_d(mu, sigma, psi, psi+delta); 01446 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01447 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01448 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01449 01450 float opacity = proposal->get_opacity(); 01451 01452 // Take out the opacity log prob. 01453 mu = density->get_opacity().get_mu(); 01454 sigma = density->get_opacity().get_sigma(); 01455 min = density->get_opacity().get_min(); 01456 max = density->get_opacity().get_max(); 01457 delta = (max - min) / 1000; 01458 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 01459 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01460 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01461 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01462 01463 // Re-sample a opacity close to the original. 01464 mu = spore->get_opacity(); 01465 sigma = density->get_opacity().get_sigma(); 01466 min = density->get_opacity().get_min(); 01467 max = density->get_opacity().get_max(); 01468 opacity = sample_gaussian_pdf_d(mu, sigma, min, max); 01469 proposal->set_opacity(opacity); 01470 01471 // Put in the re-sampled opacity log prob. 01472 mu = density->get_opacity().get_mu(); 01473 sigma = density->get_opacity().get_sigma(); 01474 min = density->get_opacity().get_min(); 01475 max = density->get_opacity().get_max(); 01476 delta = (max - min) / 1000; 01477 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 01478 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01479 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01480 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01481 01482 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 01483 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 01484 #endif 01485 01486 *log_prob_out = log_prob; 01487 } 01488 01489 01496 void Spore_split_merge_spore_move::propose_2nd_split 01497 ( 01498 Spore** proposal_out, 01499 double* log_prob_out, 01500 const Spore* split_1 01501 ) 01502 throw (Arg_error) 01503 { 01504 Spore* proposal; 01505 double log_prob; 01506 01507 assert(*proposal_out == 0); 01508 01509 const Spore_density* density = split_1->get_density(); 01510 01511 *proposal_out = density->sample(); 01512 proposal = *proposal_out; 01513 log_prob = proposal->get_log_prob(); 01514 01515 float mu, sigma; 01516 float min, max; 01517 double p_1, p_2; 01518 double delta; 01519 01520 float opacity = proposal->get_opacity(); 01521 01522 // Take out the opacity log prob. 01523 mu = density->get_opacity().get_mu(); 01524 sigma = density->get_opacity().get_sigma(); 01525 min = density->get_opacity().get_min(); 01526 max = density->get_opacity().get_max(); 01527 delta = (max - min) / 1000; 01528 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 01529 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01530 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01531 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01532 01533 // Re-sample a opacity close to the original. 01534 mu = split_1->get_opacity(); 01535 sigma = density->get_opacity().get_sigma(); 01536 min = density->get_opacity().get_min(); 01537 max = density->get_opacity().get_max(); 01538 opacity = sample_gaussian_pdf_d(mu, sigma, min, max); 01539 proposal->set_opacity(opacity); 01540 01541 // Put in the re-sampled opacity log prob. 01542 mu = density->get_opacity().get_mu(); 01543 sigma = density->get_opacity().get_sigma(); 01544 min = density->get_opacity().get_min(); 01545 max = density->get_opacity().get_max(); 01546 delta = (max - min) / 1000; 01547 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 01548 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01549 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01550 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01551 01552 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 01553 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 01554 #endif 01555 01556 *log_prob_out = log_prob; 01557 } 01558 01559 01570 void Spore_split_merge_spore_move::propose_merge 01571 ( 01572 Spore** proposal_out, 01573 double* log_prob_out, 01574 const Structure*parent, 01575 const Spore* spore_1, 01576 const Spore* spore_2 01577 ) 01578 throw (Arg_error) 01579 { 01580 Spore* proposal; 01581 double log_prob; 01582 01583 assert(*proposal_out == 0); 01584 01585 const Spore_density* density = spore_1->get_density(); 01586 01587 *proposal_out = density->sample(); 01588 proposal = *proposal_out; 01589 log_prob = proposal->get_log_prob(); 01590 01591 float mu, sigma; 01592 float min, max; 01593 double p_1, p_2; 01594 double delta; 01595 01596 float width = proposal->get_width(); 01597 01598 // Take out the width log prob. 01599 mu = density->get_width().get_mu(); 01600 sigma = density->get_width().get_sigma(); 01601 min = density->get_width().get_min(); 01602 max = density->get_width().get_max(); 01603 delta = (max - min) / 1000; 01604 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 01605 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01606 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01607 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01608 01609 // Re-sample a width close to the original. 01610 width = 0.65f*spore_1->get_width() + 0.65f*spore_2->get_width(); 01611 sigma = density->get_width().get_sigma(); 01612 min = -6.0f*sigma; 01613 max = 6.0f*sigma; 01614 width += sample_gaussian_pdf_d(0, sigma, min, max); 01615 proposal->set_width(width); 01616 01617 // Put in the re-sampled width log prob. 01618 mu = density->get_width().get_mu(); 01619 sigma = density->get_width().get_sigma(); 01620 min = density->get_width().get_min(); 01621 max = density->get_width().get_max(); 01622 delta = (max - min) / 1000; 01623 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 01624 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01625 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01626 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01627 01628 float opacity = proposal->get_opacity(); 01629 01630 // Take out the opacity log prob. 01631 mu = density->get_opacity().get_mu(); 01632 sigma = density->get_opacity().get_sigma(); 01633 min = density->get_opacity().get_min(); 01634 max = density->get_opacity().get_max(); 01635 delta = (max - min) / 1000; 01636 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 01637 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01638 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01639 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01640 01641 // Re-sample a opacity close to the original. 01642 opacity = 0.5f*spore_1->get_opacity() + 0.5f*spore_2->get_opacity(); 01643 sigma = density->get_opacity().get_sigma(); 01644 min = -6.0f*sigma; 01645 max = 6.0f*sigma; 01646 opacity += sample_gaussian_pdf_d(0, sigma, min, max); 01647 proposal->set_opacity(opacity); 01648 01649 // Put in the re-sampled opacity log prob. 01650 mu = density->get_opacity().get_mu(); 01651 sigma = density->get_opacity().get_sigma(); 01652 min = density->get_opacity().get_min(); 01653 max = density->get_opacity().get_max(); 01654 delta = (max - min) / 1000; 01655 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 01656 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 01657 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 01658 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 01659 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 01660 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 01661 #endif 01662 01663 *proposal_out = proposal; 01664 *log_prob_out = log_prob; 01665 } 01666 01680 bool Lateral_hypha_birth_death_move::run_1(Sampler_move_parameters* params) 01681 throw (Exception) 01682 { 01683 params->init_proposals(); 01684 01685 Apical_structure* parent; 01686 Lateral_hypha* hypha; 01687 const Lateral_hypha_density* density; 01688 01689 try 01690 { 01691 size_t level = params->alternaria_proposal->get_uniform_random_level(); 01692 //parent = params->alternaria_proposal->get_random_apical_hypha(level); 01693 parent = dynamic_cast<Apical_structure*>( 01694 params->alternaria_proposal->get_random_structure(level)); 01695 01696 if (!parent) 01697 { 01698 return false; 01699 } 01700 01701 // HACK Do not allow terminal parents. 01702 if (!parent->get_apical()) 01703 { 01704 return false; 01705 } 01706 01707 density = (level == 0) 01708 ? params->alternaria_proposal->get_lateral_hypha_1_d() 01709 : params->alternaria_proposal->get_lateral_hypha_n_d(); 01710 01711 hypha = density->sample(parent); 01712 params->alternaria_proposal->add_lateral_hypha(hypha); 01713 } 01714 catch (Exception e) 01715 { 01716 return false; 01717 } 01718 01719 if (params->alternaria_proposal->update_scene(params->psf_proposal, 01720 params->imaging_proposal)) 01721 { 01722 return false; 01723 } 01724 01725 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 01726 params->psf_proposal, params->imaging_proposal, params->data, 01727 params->data_avg); 01728 01729 double hypha_log_prob = hypha->get_log_prob(); 01730 01731 double birth_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 01732 : log(JWSC_MIN_LOG_ARG); 01733 01734 double death_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 01735 : log(JWSC_MIN_LOG_ARG); 01736 01737 double log_alpha = params->ll_proposal - params->ll; 01738 log_alpha += params->alternaria_proposal->get_log_prob(); 01739 log_alpha -= params->alternaria->get_log_prob(); 01740 log_alpha -= hypha_log_prob; 01741 log_alpha -= birth_move_log_prob; 01742 log_alpha += death_move_log_prob; 01743 01744 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 01745 assert(!isinf(log_alpha) && !isnan(log_alpha)); 01746 #endif 01747 01748 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 01749 01750 if (log_u <= log_alpha) 01751 { 01752 return true; 01753 } 01754 return false; 01755 } 01756 01757 01763 bool Lateral_hypha_birth_death_move::run_2(Sampler_move_parameters* params) 01764 throw (Exception) 01765 { 01766 params->init_proposals(); 01767 01768 Lateral_hypha* hypha; 01769 01770 try 01771 { 01772 size_t level = params->alternaria_proposal->get_uniform_random_level(); 01773 hypha = params->alternaria_proposal 01774 ->get_random_terminal_lateral_hypha(level); 01775 01776 // Check that hypha doesn't have any laterals. 01777 if (hypha->get_laterals().size() != 0) 01778 { 01779 return false; 01780 } 01781 } 01782 catch (Exception e) 01783 { 01784 return false; 01785 } 01786 01787 double hypha_log_prob = hypha->get_log_prob(); 01788 01789 try 01790 { 01791 params->alternaria_proposal->remove_lateral_hypha(hypha); 01792 } 01793 catch (Arg_error e) 01794 { 01795 return false; 01796 } 01797 01798 if (params->alternaria_proposal->update_scene(params->psf_proposal, 01799 params->imaging_proposal)) 01800 { 01801 return false; 01802 } 01803 01804 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 01805 params->psf_proposal, params->imaging_proposal, params->data, 01806 params->data_avg); 01807 01808 double birth_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 01809 : log(JWSC_MIN_LOG_ARG); 01810 01811 double death_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 01812 : log(JWSC_MIN_LOG_ARG); 01813 01814 double log_alpha = params->ll_proposal - params->ll; 01815 log_alpha += params->alternaria_proposal->get_log_prob(); 01816 log_alpha -= params->alternaria->get_log_prob(); 01817 log_alpha += hypha_log_prob; 01818 log_alpha += birth_move_log_prob; 01819 log_alpha -= death_move_log_prob; 01820 01821 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 01822 assert(!isinf(log_alpha) && !isnan(log_alpha)); 01823 #endif 01824 01825 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 01826 01827 if (log_u <= log_alpha) 01828 { 01829 return true; 01830 } 01831 return false; 01832 } 01833 01834 01840 bool Apical_hypha_split_merge_apical_move::run_1 01841 ( 01842 Sampler_move_parameters* params 01843 ) 01844 throw (Exception) 01845 { 01846 params->init_proposals(); 01847 01848 Apical_hypha* hypha; 01849 01850 Apical_hypha* split_1 = 0; 01851 double split_1_log_prob; 01852 01853 Apical_hypha* split_2 = 0; 01854 double split_2_log_prob; 01855 01856 Apical_hypha* merge = 0; 01857 double merge_log_prob; 01858 01859 try 01860 { 01861 size_t level = params->alternaria_proposal->get_uniform_random_level(); 01862 hypha = params->alternaria_proposal->get_random_apical_hypha(level); 01863 01864 propose_1st_split(&split_1, &split_1_log_prob, hypha); 01865 propose_2nd_split(&split_2, &split_2_log_prob, split_1); 01866 01867 propose_merge(&merge, &merge_log_prob, hypha->get_parent(), split_1, 01868 split_2); 01869 01870 params->alternaria_proposal->split_apical_hypha_into_apical(hypha, 01871 split_1, split_2); 01872 } 01873 catch (Exception e) 01874 { 01875 delete merge; 01876 delete split_1; 01877 delete split_2; 01878 return false; 01879 } 01880 01881 delete merge; 01882 delete split_1; 01883 delete split_2; 01884 01885 if (params->alternaria_proposal->update_scene(params->psf_proposal, 01886 params->imaging_proposal)) 01887 { 01888 return false; 01889 } 01890 01891 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 01892 params->psf_proposal, params->imaging_proposal, params->data, 01893 params->data_avg); 01894 01895 double split_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 01896 : log(JWSC_MIN_LOG_ARG); 01897 01898 double merge_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 01899 : log(JWSC_MIN_LOG_ARG); 01900 01901 double log_alpha = params->ll_proposal - params->ll; 01902 log_alpha += params->alternaria_proposal->get_log_prob(); 01903 log_alpha -= params->alternaria->get_log_prob(); 01904 log_alpha += merge_log_prob; 01905 log_alpha -= split_1_log_prob; 01906 log_alpha -= split_2_log_prob; 01907 log_alpha -= split_move_log_prob; 01908 log_alpha += merge_move_log_prob; 01909 01910 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 01911 assert(!isinf(log_alpha) && !isnan(log_alpha)); 01912 #endif 01913 01914 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 01915 01916 if (log_u <= log_alpha) 01917 { 01918 return true; 01919 } 01920 return false; 01921 } 01922 01923 01929 bool Apical_hypha_split_merge_apical_move::run_2 01930 ( 01931 Sampler_move_parameters* params 01932 ) 01933 throw (Exception) 01934 { 01935 params->init_proposals(); 01936 01937 Apical_hypha* hypha; 01938 Apical_structure* apical; 01939 01940 Apical_hypha* merge = 0; 01941 double merge_log_prob; 01942 01943 Apical_hypha* split_1 = 0; 01944 double split_1_log_prob; 01945 01946 Apical_hypha* split_2 = 0; 01947 double split_2_log_prob; 01948 01949 try 01950 { 01951 size_t level = params->alternaria_proposal->get_uniform_random_level(); 01952 hypha = params->alternaria_proposal->get_random_apical_hypha(level); 01953 apical = hypha->get_apical(); 01954 01955 if (!apical || !dynamic_cast<Apical_hypha*>(apical)) 01956 { 01957 return false; 01958 } 01959 01960 propose_merge(&merge, &merge_log_prob, hypha->get_parent(), hypha, 01961 (Apical_hypha*)apical); 01962 01963 propose_1st_split(&split_1, &split_1_log_prob, merge); 01964 propose_2nd_split(&split_2, &split_2_log_prob, split_1); 01965 01966 params->alternaria_proposal->merge_apical_hypha_with_apical(hypha, 01967 merge); 01968 } 01969 catch (Exception e) 01970 { 01971 delete split_1; 01972 delete split_2; 01973 delete merge; 01974 return false; 01975 } 01976 01977 delete split_1; 01978 delete split_2; 01979 delete merge; 01980 01981 if (params->alternaria_proposal->update_scene(params->psf_proposal, 01982 params->imaging_proposal)) 01983 { 01984 return false; 01985 } 01986 01987 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 01988 params->psf_proposal, params->imaging_proposal, params->data, 01989 params->data_avg); 01990 01991 double split_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 01992 : log(JWSC_MIN_LOG_ARG); 01993 01994 double merge_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 01995 : log(JWSC_MIN_LOG_ARG); 01996 01997 double log_alpha = params->ll_proposal - params->ll; 01998 log_alpha += params->alternaria_proposal->get_log_prob(); 01999 log_alpha -= params->alternaria->get_log_prob(); 02000 log_alpha += split_1_log_prob; 02001 log_alpha += split_2_log_prob; 02002 log_alpha -= merge_log_prob; 02003 log_alpha += split_move_log_prob; 02004 log_alpha -= merge_move_log_prob; 02005 02006 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 02007 assert(!isinf(log_alpha) && !isnan(log_alpha)); 02008 #endif 02009 02010 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 02011 02012 if (log_u <= log_alpha) 02013 { 02014 return true; 02015 } 02016 return false; 02017 } 02018 02019 02026 void Apical_hypha_split_merge_apical_move::propose_1st_split 02027 ( 02028 Apical_hypha** proposal_out, 02029 double* log_prob_out, 02030 const Apical_hypha* hypha 02031 ) 02032 throw (Arg_error) 02033 { 02034 Apical_hypha* proposal; 02035 double log_prob; 02036 02037 assert(*proposal_out == 0); 02038 02039 const Apical_hypha_density* density = hypha->get_density(); 02040 02041 *proposal_out = density->sample(); 02042 proposal = *proposal_out; 02043 log_prob = proposal->get_log_prob(); 02044 02045 float mu, sigma; 02046 float min, max; 02047 double p_1, p_2; 02048 double delta; 02049 02050 const Structure* parent = hypha->get_parent(); 02051 02052 // Set a parent-dependant width. 02053 if (parent && (dynamic_cast<const Apical_hypha*>(parent) || 02054 dynamic_cast<const Lateral_hypha*>(parent))) 02055 { 02056 float width = proposal->get_width(); 02057 02058 // Take out the width log prob. 02059 mu = density->get_width().get_mu(); 02060 sigma = density->get_width().get_sigma(); 02061 min = density->get_width().get_min(); 02062 max = density->get_width().get_max(); 02063 delta = (max - min) / 1000; 02064 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 02065 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02066 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02067 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02068 02069 float dwidth = density->sample_dwidth(); 02070 proposal->set_width(parent->get_width() + dwidth); 02071 02072 // Put in the differential width log prob. 02073 mu = density->get_dwidth().get_mu(); 02074 sigma = density->get_dwidth().get_sigma(); 02075 min = density->get_dwidth().get_min(); 02076 max = density->get_dwidth().get_max(); 02077 delta = (max - min) / 1000; 02078 p_1 = integrate_gaussian_pdf_d(mu, sigma, dwidth, dwidth+delta); 02079 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02080 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02081 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02082 } 02083 02084 float theta = proposal->get_theta(); 02085 02086 // Take out the theta log prob. 02087 mu = density->get_theta().get_mu(); 02088 sigma = density->get_theta().get_sigma(); 02089 min = density->get_theta().get_min(); 02090 max = density->get_theta().get_max(); 02091 delta = (max - min) / 1000; 02092 p_1 = integrate_gaussian_pdf_d(mu, sigma, theta, theta+delta); 02093 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02094 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02095 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02096 02097 // Re-sample a theta close to the original. 02098 mu = hypha->get_theta(); 02099 sigma = density->get_theta().get_sigma(); 02100 min = density->get_theta().get_min(); 02101 max = density->get_theta().get_max(); 02102 theta = sample_gaussian_pdf_d(mu, sigma, min, max); 02103 proposal->set_theta(theta); 02104 02105 // Put in the re-sampled theta log prob. 02106 mu = density->get_theta().get_mu(); 02107 sigma = density->get_theta().get_sigma(); 02108 min = density->get_theta().get_min(); 02109 max = density->get_theta().get_max(); 02110 delta = (max - min) / 1000; 02111 p_1 = integrate_gaussian_pdf_d(mu, sigma, theta, theta+delta); 02112 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02113 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02114 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02115 02116 float psi; 02117 02118 // Take out the psi log prob. 02119 min = density->get_psi().get_min(); 02120 max = density->get_psi().get_max(); 02121 p_1 = max - min; 02122 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02123 02124 // Re-sample a psi close to the original (using a Gaussian). 02125 mu = hypha->get_psi(); 02126 sigma = density->get_theta().get_sigma(); // Not a bug. 02127 min = density->get_psi().get_min(); 02128 max = density->get_psi().get_max(); 02129 psi = sample_gaussian_pdf_d(mu, sigma, min, max); 02130 proposal->set_psi(psi); 02131 02132 // Put in the re-sampled psi log prob. 02133 delta = (max - min) / 1000; 02134 p_1 = integrate_gaussian_pdf_d(mu, sigma, psi, psi+delta); 02135 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02136 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02137 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02138 02139 float opacity = proposal->get_opacity(); 02140 02141 // Take out the opacity log prob. 02142 mu = density->get_opacity().get_mu(); 02143 sigma = density->get_opacity().get_sigma(); 02144 min = density->get_opacity().get_min(); 02145 max = density->get_opacity().get_max(); 02146 delta = (max - min) / 1000; 02147 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 02148 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02149 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02150 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02151 02152 // Re-sample a opacity close to the original. 02153 mu = hypha->get_opacity(); 02154 sigma = density->get_opacity().get_sigma(); 02155 min = density->get_opacity().get_min(); 02156 max = density->get_opacity().get_max(); 02157 opacity = sample_gaussian_pdf_d(mu, sigma, min, max); 02158 proposal->set_opacity(opacity); 02159 02160 // Put in the re-sampled opacity log prob. 02161 mu = density->get_opacity().get_mu(); 02162 sigma = density->get_opacity().get_sigma(); 02163 min = density->get_opacity().get_min(); 02164 max = density->get_opacity().get_max(); 02165 delta = (max - min) / 1000; 02166 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 02167 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02168 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02169 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02170 02171 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 02172 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 02173 #endif 02174 02175 *log_prob_out = log_prob; 02176 } 02177 02178 02185 void Apical_hypha_split_merge_apical_move::propose_2nd_split 02186 ( 02187 Apical_hypha** proposal_out, 02188 double* log_prob_out, 02189 const Apical_hypha* split_1 02190 ) 02191 throw (Arg_error) 02192 { 02193 Apical_hypha* proposal; 02194 double log_prob; 02195 02196 assert(*proposal_out == 0); 02197 02198 const Apical_hypha_density* density = split_1->get_density(); 02199 02200 *proposal_out = density->sample(); 02201 proposal = *proposal_out; 02202 log_prob = proposal->get_log_prob(); 02203 02204 float mu, sigma; 02205 float min, max; 02206 double p_1, p_2; 02207 double delta; 02208 float width = proposal->get_width(); 02209 02210 // Take out the width log prob. 02211 mu = density->get_width().get_mu(); 02212 sigma = density->get_width().get_sigma(); 02213 min = density->get_width().get_min(); 02214 max = density->get_width().get_max(); 02215 delta = (max - min) / 1000; 02216 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 02217 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02218 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02219 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02220 02221 float dwidth = density->sample_dwidth(); 02222 proposal->set_width(split_1->get_width() + dwidth); 02223 02224 // Put in the differential width log prob. 02225 mu = density->get_dwidth().get_mu(); 02226 sigma = density->get_dwidth().get_sigma(); 02227 min = density->get_dwidth().get_min(); 02228 max = density->get_dwidth().get_max(); 02229 delta = (max - min) / 1000; 02230 p_1 = integrate_gaussian_pdf_d(mu, sigma, dwidth, dwidth+delta); 02231 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02232 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02233 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02234 02235 float opacity = proposal->get_opacity(); 02236 02237 // Take out the opacity log prob. 02238 mu = density->get_opacity().get_mu(); 02239 sigma = density->get_opacity().get_sigma(); 02240 min = density->get_opacity().get_min(); 02241 max = density->get_opacity().get_max(); 02242 delta = (max - min) / 1000; 02243 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 02244 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02245 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02246 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02247 02248 // Re-sample a opacity close to the original. 02249 mu = split_1->get_opacity(); 02250 sigma = density->get_opacity().get_sigma(); 02251 min = density->get_opacity().get_min(); 02252 max = density->get_opacity().get_max(); 02253 opacity = sample_gaussian_pdf_d(mu, sigma, min, max); 02254 proposal->set_opacity(opacity); 02255 02256 // Put in the re-sampled opacity log prob. 02257 mu = density->get_opacity().get_mu(); 02258 sigma = density->get_opacity().get_sigma(); 02259 min = density->get_opacity().get_min(); 02260 max = density->get_opacity().get_max(); 02261 delta = (max - min) / 1000; 02262 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 02263 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02264 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02265 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02266 02267 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 02268 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 02269 #endif 02270 02271 *log_prob_out = log_prob; 02272 } 02273 02274 02285 void Apical_hypha_split_merge_apical_move::propose_merge 02286 ( 02287 Apical_hypha** proposal_out, 02288 double* log_prob_out, 02289 const Structure* parent, 02290 const Apical_hypha* hypha_1, 02291 const Apical_hypha* hypha_2 02292 ) 02293 throw (Arg_error) 02294 { 02295 Apical_hypha* proposal; 02296 double log_prob; 02297 02298 assert(*proposal_out == 0); 02299 02300 const Apical_hypha_density* density = hypha_1->get_density(); 02301 02302 *proposal_out = density->sample(); 02303 proposal = *proposal_out; 02304 log_prob = proposal->get_log_prob(); 02305 02306 float mu, sigma; 02307 float min, max; 02308 double p_1, p_2; 02309 double delta; 02310 02311 // Set a parent-dependant width. 02312 if (parent && (dynamic_cast<const Apical_hypha*>(parent) || 02313 dynamic_cast<const Lateral_hypha*>(parent))) 02314 { 02315 float width = proposal->get_width(); 02316 02317 // Take out the width log prob. 02318 mu = density->get_width().get_mu(); 02319 sigma = density->get_width().get_sigma(); 02320 min = density->get_width().get_min(); 02321 max = density->get_width().get_max(); 02322 delta = (max - min) / 1000; 02323 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 02324 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02325 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02326 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02327 02328 float dwidth = density->sample_dwidth(); 02329 proposal->set_width(parent->get_width() + dwidth); 02330 02331 // Put in the differential width log prob. 02332 mu = density->get_dwidth().get_mu(); 02333 sigma = density->get_dwidth().get_sigma(); 02334 min = density->get_dwidth().get_min(); 02335 max = density->get_dwidth().get_max(); 02336 delta = (max - min) / 1000; 02337 p_1 = integrate_gaussian_pdf_d(mu, sigma, dwidth, dwidth+delta); 02338 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02339 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02340 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02341 } 02342 02343 float opacity = proposal->get_opacity(); 02344 02345 // Take out the opacity log prob. 02346 mu = density->get_opacity().get_mu(); 02347 sigma = density->get_opacity().get_sigma(); 02348 min = density->get_opacity().get_min(); 02349 max = density->get_opacity().get_max(); 02350 delta = (max - min) / 1000; 02351 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 02352 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02353 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02354 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02355 02356 // Re-sample a opacity close to the original. 02357 opacity = 0.5f*hypha_1->get_opacity() + 0.5f*hypha_2->get_opacity(); 02358 sigma = density->get_opacity().get_sigma(); 02359 min = -6.0f*sigma; 02360 max = 6.0f*sigma; 02361 opacity += sample_gaussian_pdf_d(0, sigma, min, max); 02362 proposal->set_opacity(opacity); 02363 02364 // Put in the re-sampled opacity log prob. 02365 mu = density->get_opacity().get_mu(); 02366 sigma = density->get_opacity().get_sigma(); 02367 min = density->get_opacity().get_min(); 02368 max = density->get_opacity().get_max(); 02369 delta = (max - min) / 1000; 02370 p_1 = integrate_gaussian_pdf_d(mu, sigma, opacity, opacity+delta); 02371 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02372 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02373 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02374 02375 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 02376 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 02377 #endif 02378 02379 *proposal_out = proposal; 02380 *log_prob_out = log_prob; 02381 } 02382 02383 02389 bool Apical_hypha_split_merge_lateral_move::run_1 02390 ( 02391 Sampler_move_parameters* params 02392 ) 02393 throw (Exception) 02394 { 02395 params->init_proposals(); 02396 02397 Apical_hypha* hypha; 02398 02399 Apical_hypha* split_1 = 0; 02400 double split_1_log_prob; 02401 02402 Lateral_hypha* split_2 = 0; 02403 double split_2_log_prob; 02404 02405 Apical_hypha* merge = 0; 02406 double merge_log_prob; 02407 02408 const Lateral_hypha_density* lateral_d; 02409 02410 // This move sucks! 02411 return false; 02412 02413 try 02414 { 02415 size_t level = params->alternaria_proposal->get_uniform_random_level(); 02416 hypha = params->alternaria_proposal->get_random_apical_hypha(level); 02417 02418 // HACK Do not split a terminal. 02419 if (!hypha->get_apical()) 02420 { 02421 return false; 02422 } 02423 02424 lateral_d = (level == 0) 02425 ? params->alternaria_proposal->get_lateral_hypha_1_d() 02426 : params->alternaria_proposal->get_lateral_hypha_n_d(); 02427 02428 propose_1st_split(&split_1, &split_1_log_prob, hypha); 02429 propose_2nd_split(&split_2, &split_2_log_prob, split_1, lateral_d); 02430 02431 propose_merge(&merge, &merge_log_prob, hypha->get_parent(), split_1, 02432 split_2); 02433 02434 params->alternaria_proposal->split_apical_hypha_into_lateral(hypha, 02435 split_1, split_2); 02436 } 02437 catch (Exception e) 02438 { 02439 delete merge; 02440 delete split_1; 02441 delete split_2; 02442 return false; 02443 } 02444 02445 delete split_1; 02446 delete split_2; 02447 delete merge; 02448 02449 if (params->alternaria_proposal->update_scene(params->psf_proposal, 02450 params->imaging_proposal)) 02451 { 02452 return false; 02453 } 02454 02455 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 02456 params->psf_proposal, params->imaging_proposal, params->data, 02457 params->data_avg); 02458 02459 double split_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 02460 : log(JWSC_MIN_LOG_ARG); 02461 02462 double merge_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 02463 : log(JWSC_MIN_LOG_ARG); 02464 02465 double log_alpha = params->ll_proposal - params->ll; 02466 log_alpha += params->alternaria_proposal->get_log_prob(); 02467 log_alpha -= params->alternaria->get_log_prob(); 02468 log_alpha += merge_log_prob; 02469 log_alpha -= split_1_log_prob; 02470 log_alpha -= split_2_log_prob; 02471 log_alpha -= split_move_log_prob; 02472 log_alpha += merge_move_log_prob; 02473 02474 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 02475 assert(!isinf(log_alpha) && !isnan(log_alpha)); 02476 #endif 02477 02478 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 02479 02480 if (log_u <= log_alpha) 02481 { 02482 return true; 02483 } 02484 return false; 02485 } 02486 02487 02493 bool Apical_hypha_split_merge_lateral_move::run_2 02494 ( 02495 Sampler_move_parameters* params 02496 ) 02497 throw (Exception) 02498 { 02499 params->init_proposals(); 02500 02501 Apical_hypha* hypha; 02502 Lateral_structure* lateral; 02503 02504 Apical_hypha* merge = 0; 02505 double merge_log_prob; 02506 02507 Apical_hypha* split_1 = 0; 02508 double split_1_log_prob; 02509 02510 Lateral_hypha* split_2 = 0; 02511 double split_2_log_prob; 02512 02513 const Lateral_hypha_density* lateral_d; 02514 02515 try 02516 { 02517 size_t level = params->alternaria_proposal->get_uniform_random_level(); 02518 hypha = params->alternaria_proposal 02519 ->get_random_terminal_apical_hypha(level); 02520 02521 lateral = hypha->get_random_lateral(); 02522 02523 if (!dynamic_cast<Lateral_hypha*>(lateral)) 02524 { 02525 return false; 02526 } 02527 02528 propose_merge(&merge, &merge_log_prob, hypha->get_parent(), hypha, 02529 (Lateral_hypha*)lateral); 02530 02531 lateral_d = (level == 0) 02532 ? params->alternaria_proposal->get_lateral_hypha_1_d() 02533 : params->alternaria_proposal->get_lateral_hypha_n_d(); 02534 02535 propose_1st_split(&split_1, &split_1_log_prob, merge); 02536 propose_2nd_split(&split_2, &split_2_log_prob, split_1, lateral_d); 02537 02538 params->alternaria_proposal->merge_apical_hypha_with_lateral(hypha, 02539 (Lateral_hypha*)lateral, merge); 02540 } 02541 catch (Exception e) 02542 { 02543 delete split_1; 02544 delete split_2; 02545 delete merge; 02546 return false; 02547 } 02548 02549 delete split_1; 02550 delete split_2; 02551 delete merge; 02552 02553 if (params->alternaria_proposal->update_scene(params->psf_proposal, 02554 params->imaging_proposal)) 02555 { 02556 return false; 02557 } 02558 02559 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 02560 params->psf_proposal, params->imaging_proposal, params->data, 02561 params->data_avg); 02562 02563 double split_move_log_prob = (prob_1 >= JWSC_MIN_LOG_ARG) ? log(prob_1) 02564 : log(JWSC_MIN_LOG_ARG); 02565 02566 double merge_move_log_prob = (prob_2 >= JWSC_MIN_LOG_ARG) ? log(prob_2) 02567 : log(JWSC_MIN_LOG_ARG); 02568 02569 double log_alpha = params->ll_proposal - params->ll; 02570 log_alpha += params->alternaria_proposal->get_log_prob(); 02571 log_alpha -= params->alternaria->get_log_prob(); 02572 log_alpha += split_1_log_prob; 02573 log_alpha += split_2_log_prob; 02574 log_alpha -= merge_log_prob; 02575 log_alpha += split_move_log_prob; 02576 log_alpha -= merge_move_log_prob; 02577 02578 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 02579 assert(!isinf(log_alpha) && !isnan(log_alpha)); 02580 #endif 02581 02582 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 02583 02584 if (log_u <= log_alpha) 02585 { 02586 return true; 02587 } 02588 return false; 02589 } 02590 02591 02598 void Apical_hypha_split_merge_lateral_move::propose_1st_split 02599 ( 02600 Apical_hypha** proposal_out, 02601 double* log_prob_out, 02602 const Apical_hypha* hypha 02603 ) 02604 throw (Arg_error) 02605 { 02606 Apical_hypha* proposal; 02607 double log_prob; 02608 02609 assert(*proposal_out == 0); 02610 02611 const Apical_hypha_density* density = hypha->get_density(); 02612 02613 *proposal_out = density->sample(); 02614 proposal = *proposal_out; 02615 log_prob = proposal->get_log_prob(); 02616 02617 float mu, sigma; 02618 float min, max; 02619 double p_1, p_2; 02620 double delta; 02621 02622 const Structure* parent = hypha->get_parent(); 02623 02624 // Set a parent-dependant width. 02625 if (parent && (dynamic_cast<const Apical_hypha*>(parent) || 02626 dynamic_cast<const Lateral_hypha*>(parent))) 02627 { 02628 float width = proposal->get_width(); 02629 02630 // Take out the width log prob. 02631 mu = density->get_width().get_mu(); 02632 sigma = density->get_width().get_sigma(); 02633 min = density->get_width().get_min(); 02634 max = density->get_width().get_max(); 02635 delta = (max - min) / 1000; 02636 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 02637 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02638 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02639 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02640 02641 float dwidth = density->sample_dwidth(); 02642 proposal->set_width(parent->get_width() + dwidth); 02643 02644 // Put in the differential width log prob. 02645 mu = density->get_dwidth().get_mu(); 02646 sigma = density->get_dwidth().get_sigma(); 02647 min = density->get_dwidth().get_min(); 02648 max = density->get_dwidth().get_max(); 02649 delta = (max - min) / 1000; 02650 p_1 = integrate_gaussian_pdf_d(mu, sigma, dwidth, dwidth+delta); 02651 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02652 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02653 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02654 } 02655 02656 float theta = proposal->get_theta(); 02657 02658 // Take out the theta log prob. 02659 mu = density->get_theta().get_mu(); 02660 sigma = density->get_theta().get_sigma(); 02661 min = density->get_theta().get_min(); 02662 max = density->get_theta().get_max(); 02663 delta = (max - min) / 1000; 02664 p_1 = integrate_gaussian_pdf_d(mu, sigma, theta, theta+delta); 02665 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02666 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02667 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02668 02669 // Re-sample a theta close to the original. 02670 mu = hypha->get_theta(); 02671 sigma = density->get_theta().get_sigma(); 02672 min = density->get_theta().get_min(); 02673 max = density->get_theta().get_max(); 02674 theta = sample_gaussian_pdf_d(mu, sigma, min, max); 02675 proposal->set_theta(theta); 02676 02677 // Put in the re-sampled theta log prob. 02678 mu = density->get_theta().get_mu(); 02679 sigma = density->get_theta().get_sigma(); 02680 min = density->get_theta().get_min(); 02681 max = density->get_theta().get_max(); 02682 delta = (max - min) / 1000; 02683 p_1 = integrate_gaussian_pdf_d(mu, sigma, theta, theta+delta); 02684 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02685 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02686 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02687 02688 float psi; 02689 02690 // Take out the psi log prob. 02691 min = density->get_psi().get_min(); 02692 max = density->get_psi().get_max(); 02693 p_1 = max - min; 02694 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02695 02696 // Re-sample a psi close to the original (using a Gaussian). 02697 mu = hypha->get_psi(); 02698 sigma = density->get_theta().get_sigma(); // Not a bug. 02699 min = density->get_psi().get_min(); 02700 max = density->get_psi().get_max(); 02701 psi = sample_gaussian_pdf_d(mu, sigma, min, max); 02702 proposal->set_psi(psi); 02703 02704 // Put in the re-sampled psi log prob. 02705 delta = (max - min) / 1000; 02706 p_1 = integrate_gaussian_pdf_d(mu, sigma, psi, psi+delta); 02707 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02708 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02709 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02710 02711 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 02712 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 02713 #endif 02714 02715 *log_prob_out = log_prob; 02716 } 02717 02718 02725 void Apical_hypha_split_merge_lateral_move::propose_2nd_split 02726 ( 02727 Lateral_hypha** proposal_out, 02728 double* log_prob_out, 02729 const Apical_hypha* split_1, 02730 const Lateral_hypha_density* density 02731 ) 02732 { 02733 assert(*proposal_out == 0); 02734 02735 Lateral_hypha* proposal = density->sample(); 02736 double log_prob = proposal->get_log_prob(); 02737 02738 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 02739 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 02740 #endif 02741 02742 *proposal_out = proposal; 02743 *log_prob_out = log_prob; 02744 } 02745 02746 02757 void Apical_hypha_split_merge_lateral_move::propose_merge 02758 ( 02759 Apical_hypha** proposal_out, 02760 double* log_prob_out, 02761 const Structure* parent, 02762 const Apical_hypha* hypha_1, 02763 const Lateral_hypha* hypha_2 02764 ) 02765 throw (Arg_error) 02766 { 02767 Apical_hypha* proposal; 02768 double log_prob; 02769 02770 assert(*proposal_out == 0); 02771 02772 const Apical_hypha_density* density = hypha_1->get_density(); 02773 02774 *proposal_out = density->sample(); 02775 proposal = *proposal_out; 02776 log_prob = proposal->get_log_prob(); 02777 02778 // Set a parent-dependant width. 02779 if (parent && (dynamic_cast<const Apical_hypha*>(parent) || 02780 dynamic_cast<const Lateral_hypha*>(parent))) 02781 { 02782 float mu, sigma; 02783 float min, max; 02784 double p_1, p_2; 02785 double delta; 02786 float width = proposal->get_width(); 02787 02788 // Take out the width log prob. 02789 mu = density->get_width().get_mu(); 02790 sigma = density->get_width().get_sigma(); 02791 min = density->get_width().get_min(); 02792 max = density->get_width().get_max(); 02793 delta = (max - min) / 1000; 02794 p_1 = integrate_gaussian_pdf_d(mu, sigma, width, width+delta); 02795 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02796 log_prob -= log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02797 log_prob += log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02798 02799 float dwidth = density->sample_dwidth(); 02800 proposal->set_width(parent->get_width() + dwidth); 02801 02802 // Put in the differential width log prob. 02803 mu = density->get_dwidth().get_mu(); 02804 sigma = density->get_dwidth().get_sigma(); 02805 min = density->get_dwidth().get_min(); 02806 max = density->get_dwidth().get_max(); 02807 delta = (max - min) / 1000; 02808 p_1 = integrate_gaussian_pdf_d(mu, sigma, dwidth, dwidth+delta); 02809 p_2 = integrate_gaussian_pdf_d(mu, sigma, min, max); 02810 log_prob += log((p_1 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_1); 02811 log_prob -= log((p_2 < JWSC_MIN_LOG_ARG) ? JWSC_MIN_LOG_ARG : p_2); 02812 } 02813 02814 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 02815 assert(!std::isinf(log_prob) && !std::isnan(log_prob)); 02816 #endif 02817 02818 *log_prob_out = log_prob; 02819 } 02820 02821 02827 bool Structure_resize_move::run(Sampler_move_parameters* params) 02828 throw (Exception) 02829 { 02830 params->init_proposals(); 02831 02832 Structure* s; 02833 Structure* parent; 02834 02835 try 02836 { 02837 size_t level = params->alternaria_proposal->get_uniform_random_level(); 02838 s = params->alternaria_proposal->get_random_structure(level); 02839 parent = s->get_parent(); 02840 } 02841 catch (Exception e) 02842 { 02843 return false; 02844 } 02845 02846 Structure_density* density = s->get_density()->clone(); 02847 02848 float mu = s->get_length(); 02849 float sigma = density->get_length().get_sigma(); 02850 float min = density->get_length().get_min(); 02851 float max = density->get_length().get_max(); 02852 density->set_length(mu, sigma, min, max); 02853 02854 float width; 02855 02856 if (parent && (dynamic_cast<Apical_hypha*>(parent) || 02857 dynamic_cast<Lateral_hypha*>(parent)) && 02858 dynamic_cast<Apical_hypha_density*>(density)) 02859 { 02860 Apical_hypha_density* hypha_density = (Apical_hypha_density*)density; 02861 02862 width = parent->get_width() + hypha_density->sample_dwidth(); 02863 } 02864 else 02865 { 02866 mu = s->get_width(); 02867 sigma = density->get_width().get_sigma(); 02868 min = density->get_width().get_min(); 02869 max = density->get_width().get_max(); 02870 density->set_width(mu, sigma, min, max); 02871 02872 width = density->sample_width(); 02873 } 02874 02875 try 02876 { 02877 s->set_size(density->sample_length(), width); 02878 } 02879 catch (Arg_error e) 02880 { 02881 delete density; 02882 return false; 02883 } 02884 02885 delete density; 02886 02887 if (params->alternaria_proposal->update_scene(params->psf_proposal, 02888 params->imaging_proposal)) 02889 { 02890 return false; 02891 } 02892 02893 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 02894 params->psf_proposal, params->imaging_proposal, params->data, 02895 params->data_avg); 02896 02897 double log_alpha = params->ll_proposal - params->ll; 02898 log_alpha += params->alternaria_proposal->get_log_prob(); 02899 log_alpha -= params->alternaria->get_log_prob(); 02900 02901 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 02902 assert(!isinf(log_alpha) && !isnan(log_alpha)); 02903 #endif 02904 02905 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 02906 02907 if (log_u <= log_alpha) 02908 { 02909 return true; 02910 } 02911 return false; 02912 } 02913 02914 02920 const std::ostringstream& Structure_resize_move::get_info 02921 ( 02922 Sampler_move_parameters* params 02923 ) 02924 { 02925 info.str(""); 02926 return info; 02927 } 02928 02929 02936 const std::ostringstream& Structure_resize_move::get_proposal_info 02937 ( 02938 Sampler_move_parameters* params 02939 ) 02940 { 02941 proposal_info.str(""); 02942 return proposal_info; 02943 } 02944 02945 02951 bool Structure_rotate_move::run(Sampler_move_parameters* params) 02952 throw (Exception) 02953 { 02954 params->init_proposals(); 02955 02956 Structure* s; 02957 02958 try 02959 { 02960 size_t level = params->alternaria_proposal->get_uniform_random_level(); 02961 s = params->alternaria_proposal->get_random_structure(level); 02962 } 02963 catch (Exception e) 02964 { 02965 return false; 02966 } 02967 02968 Structure_density* density = s->get_density()->clone(); 02969 02970 float mu = s->get_theta(); 02971 float sigma = density->get_theta().get_sigma(); 02972 float min = density->get_theta().get_min(); 02973 float max = density->get_theta().get_max(); 02974 density->set_theta(mu, sigma, min, max); 02975 float theta = density->sample_theta(); 02976 02977 /* 02978 mu = s->get_psi(); 02979 sigma = density->get_theta().get_sigma(); // Not a bug, but dumb. 02980 min = density->get_psi().get_min(); 02981 max = density->get_psi().get_max(); 02982 float psi = sample_gaussian_pdf_d(mu, sigma, min, max); 02983 */ 02984 mu = 0; 02985 sigma = density->get_theta().get_sigma(); // Not a bug, but dumb. 02986 min = -6.0f*sigma; 02987 max = 6.0f*sigma; 02988 float psi = s->get_psi() + sample_gaussian_pdf_d(mu, sigma, min, max); 02989 02990 min = density->get_psi().get_min(); 02991 max = density->get_psi().get_max(); 02992 02993 if (psi > max && (psi - JWSC_2_PI) >= min) 02994 { 02995 psi -= JWSC_2_PI; 02996 } 02997 else if (psi < min && (psi + JWSC_2_PI) <= max) 02998 { 02999 psi += JWSC_2_PI; 03000 } 03001 03002 try 03003 { 03004 s->set_orientation(theta, psi); 03005 } 03006 catch (Arg_error e) 03007 { 03008 delete density; 03009 return false; 03010 } 03011 03012 delete density; 03013 03014 if (params->alternaria_proposal->update_scene(params->psf_proposal, 03015 params->imaging_proposal)) 03016 { 03017 return false; 03018 } 03019 03020 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 03021 params->psf_proposal, params->imaging_proposal, params->data, 03022 params->data_avg); 03023 03024 double log_alpha = params->ll_proposal - params->ll; 03025 log_alpha += params->alternaria_proposal->get_log_prob(); 03026 log_alpha -= params->alternaria->get_log_prob(); 03027 03028 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 03029 assert(!isinf(log_alpha) && !isnan(log_alpha)); 03030 #endif 03031 03032 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 03033 03034 if (log_u <= log_alpha) 03035 { 03036 return true; 03037 } 03038 return false; 03039 } 03040 03041 03047 const std::ostringstream& Structure_rotate_move::get_info 03048 ( 03049 Sampler_move_parameters* params 03050 ) 03051 { 03052 info.str(""); 03053 return info; 03054 } 03055 03056 03062 const std::ostringstream& Structure_rotate_move::get_proposal_info 03063 ( 03064 Sampler_move_parameters* params 03065 ) 03066 { 03067 proposal_info.str(""); 03068 return proposal_info; 03069 } 03070 03071 03077 bool Structure_opacity_move::run(Sampler_move_parameters* params) 03078 throw (Exception) 03079 { 03080 params->init_proposals(); 03081 03082 Structure* s; 03083 03084 try 03085 { 03086 size_t level = params->alternaria_proposal->get_uniform_random_level(); 03087 s = params->alternaria_proposal->get_random_structure(level); 03088 } 03089 catch (Exception e) 03090 { 03091 return false; 03092 } 03093 03094 Structure_density* density = s->get_density()->clone(); 03095 03096 float mu = s->get_opacity(); 03097 float sigma = density->get_opacity().get_sigma(); 03098 float min = density->get_opacity().get_min(); 03099 float max = density->get_opacity().get_max(); 03100 density->set_opacity(mu, sigma, min, max); 03101 03102 s->set_opacity(density->sample_opacity()); 03103 03104 delete density; 03105 03106 if (params->alternaria_proposal->update_scene(params->psf_proposal, 03107 params->imaging_proposal)) 03108 { 03109 return false; 03110 } 03111 03112 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 03113 params->psf_proposal, params->imaging_proposal, params->data, 03114 params->data_avg); 03115 03116 double log_alpha = params->ll_proposal - params->ll; 03117 log_alpha += params->alternaria_proposal->get_log_prob(); 03118 log_alpha -= params->alternaria->get_log_prob(); 03119 03120 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 03121 assert(!isinf(log_alpha) && !isnan(log_alpha)); 03122 #endif 03123 03124 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 03125 03126 if (log_u <= log_alpha) 03127 { 03128 return true; 03129 } 03130 return false; 03131 } 03132 03133 03139 const std::ostringstream& Structure_opacity_move::get_info 03140 ( 03141 Sampler_move_parameters* params 03142 ) 03143 { 03144 info.str(""); 03145 return info; 03146 } 03147 03148 03154 const std::ostringstream& Structure_opacity_move::get_proposal_info 03155 ( 03156 Sampler_move_parameters* params 03157 ) 03158 { 03159 proposal_info.str(""); 03160 return proposal_info; 03161 } 03162 03163 03169 bool Apical_structure_position_move::run(Sampler_move_parameters* params) 03170 throw (Exception) 03171 { 03172 params->init_proposals(); 03173 03174 Apical_structure* s; 03175 03176 try 03177 { 03178 size_t level = params->alternaria_proposal->get_uniform_random_level(); 03179 s = dynamic_cast<Apical_structure*>( 03180 params->alternaria_proposal->get_random_structure(level)); 03181 03182 if (!s) 03183 { 03184 return false; 03185 } 03186 } 03187 catch (Exception e) 03188 { 03189 return false; 03190 } 03191 03192 const Vector_f* centroid = s->get_centroid(); 03193 float x = centroid->elts[0] + sample_gaussian_pdf_d(0, 1, -10, 10); 03194 float y = centroid->elts[1] + sample_gaussian_pdf_d(0, 1, -10, 10); 03195 float z = centroid->elts[2] + sample_gaussian_pdf_d(0, 1, -10, 10); 03196 03197 try 03198 { 03199 s->set_position(x, y, z); 03200 } 03201 catch (Arg_error e) 03202 { 03203 return false; 03204 } 03205 03206 if (params->alternaria_proposal->update_scene(params->psf_proposal, 03207 params->imaging_proposal)) 03208 { 03209 return false; 03210 } 03211 03212 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 03213 params->psf_proposal, params->imaging_proposal, params->data, 03214 params->data_avg); 03215 03216 double log_alpha = params->ll_proposal - params->ll; 03217 log_alpha += params->alternaria_proposal->get_log_prob(); 03218 log_alpha -= params->alternaria->get_log_prob(); 03219 03220 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 03221 assert(!isinf(log_alpha) && !isnan(log_alpha)); 03222 #endif 03223 03224 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 03225 03226 if (log_u <= log_alpha) 03227 { 03228 return true; 03229 } 03230 return false; 03231 } 03232 03233 03239 const std::ostringstream& Apical_structure_position_move::get_info 03240 ( 03241 Sampler_move_parameters* params 03242 ) 03243 { 03244 info.str(""); 03245 return info; 03246 } 03247 03248 03254 const std::ostringstream& Apical_structure_position_move::get_proposal_info 03255 ( 03256 Sampler_move_parameters* params 03257 ) 03258 { 03259 proposal_info.str(""); 03260 return proposal_info; 03261 } 03262 03263 03269 bool Lateral_structure_lat_dist_move::run(Sampler_move_parameters* params) 03270 throw (Exception) 03271 { 03272 params->init_proposals(); 03273 03274 Lateral_structure* lat; 03275 03276 try 03277 { 03278 size_t level = params->alternaria_proposal->get_uniform_random_level(); 03279 lat = params->alternaria_proposal->get_random_lateral_hypha(level); 03280 } 03281 catch (Exception e) 03282 { 03283 return false; 03284 } 03285 03286 Structure* parent = 0; 03287 Apical_structure* apical = 0; 03288 03289 if (lat->get_parent()) 03290 { 03291 parent = lat->get_parent()->get_parent(); 03292 apical = lat->get_parent()->get_apical(); 03293 } 03294 03295 Lateral_structure_density* density = lat->get_density()->clone(); 03296 03297 try 03298 { 03299 float mu = lat->get_lat_dist(); 03300 float sigma = density->get_lat_dist().get_sigma(); 03301 float min = density->get_lat_dist().get_min(); 03302 float max = density->get_lat_dist().get_max(); 03303 float lat_dist; 03304 03305 if (dynamic_cast<Apical_structure*>(lat->get_parent()) 03306 && parent && apical) 03307 { 03308 float t_mu = mu - min; 03309 float t_max = 2.0f*(max - min); 03310 float t_min = -max + min; 03311 03312 lat_dist = sample_gaussian_pdf_d(t_mu, sigma, t_min, t_max); 03313 03314 if (lat_dist < 0) 03315 { 03316 lat->set_parent(parent, -lat_dist + min); 03317 } 03318 else if (lat_dist > (max - min)) 03319 { 03320 lat->set_parent(apical, (lat_dist - (max - min)) + min); 03321 } 03322 else 03323 { 03324 lat->set_lat_dist(lat_dist + min); 03325 } 03326 } 03327 else if (dynamic_cast<Apical_structure*>(lat->get_parent()) 03328 && parent && !apical) 03329 { 03330 float t_mu = mu - min; 03331 float t_max = max - min; 03332 float t_min = -max + min; 03333 03334 lat_dist = sample_gaussian_pdf_d(t_mu, sigma, t_min, t_max); 03335 03336 if (lat_dist < 0) 03337 { 03338 lat->set_parent(parent, -lat_dist + min); 03339 } 03340 else 03341 { 03342 lat->set_lat_dist(lat_dist + min); 03343 } 03344 } 03345 else if ((dynamic_cast<Lateral_structure*>(lat->get_parent()) || 03346 !parent) && apical) 03347 { 03348 float t_mu = mu - min; 03349 float t_max = 2.0f*(max - min); 03350 float t_min = 0; 03351 03352 lat_dist = sample_gaussian_pdf_d(t_mu, sigma, t_min, t_max); 03353 03354 if (lat_dist > (max - min)) 03355 { 03356 lat->set_parent(apical, (lat_dist - (max - min)) + min); 03357 } 03358 else 03359 { 03360 lat->set_lat_dist(lat_dist + min); 03361 } 03362 } 03363 else if ((dynamic_cast<Lateral_structure*>(lat->get_parent()) || 03364 !parent) && !apical) 03365 { 03366 density->set_lat_dist(mu, sigma, min, max); 03367 lat->set_lat_dist(density->sample_lat_dist()); 03368 } 03369 else 03370 { 03371 std::cerr << "BUG: Lateral_structure_lat_dist_move::run: invalid case\n"; 03372 assert(0); 03373 } 03374 } 03375 catch (Arg_error e) 03376 { 03377 delete density; 03378 return false; 03379 } 03380 03381 delete density; 03382 03383 if (params->alternaria_proposal->update_scene(params->psf_proposal, 03384 params->imaging_proposal)) 03385 { 03386 return false; 03387 } 03388 03389 params->ll_proposal = params->alternaria_proposal->calc_log_likelihood( 03390 params->psf_proposal, params->imaging_proposal, params->data, 03391 params->data_avg); 03392 03393 double log_alpha = params->ll_proposal - params->ll; 03394 log_alpha += params->alternaria_proposal->get_log_prob(); 03395 log_alpha -= params->alternaria->get_log_prob(); 03396 03397 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN 03398 assert(!isinf(log_alpha) && !isnan(log_alpha)); 03399 #endif 03400 03401 double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0)); 03402 03403 if (log_u <= log_alpha) 03404 { 03405 return true; 03406 } 03407 return false; 03408 } 03409 03410 03416 const std::ostringstream& Lateral_structure_lat_dist_move::get_info 03417 ( 03418 Sampler_move_parameters* params 03419 ) 03420 { 03421 info.str(""); 03422 return info; 03423 } 03424 03425 03431 const std::ostringstream& Lateral_structure_lat_dist_move::get_proposal_info 03432 ( 03433 Sampler_move_parameters* params 03434 ) 03435 { 03436 proposal_info.str(""); 03437 return proposal_info; 03438 }