Alternaria
fit cylinders and ellipsoids to fungus
alternaria_model.h
Go to the documentation of this file.
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 #ifndef ALTERNARIA_MODEL_H
00047 #define ALTERNARIA_MODEL_H
00048 
00049 
00050 #include <config.h>
00051 
00052 #include <cassert>
00053 #include <iostream>
00054 #include <list>
00055 #include <vector>
00056 
00057 #include <inttypes.h>
00058 
00059 #include <jwsc/config.h>
00060 #include <jwsc/matblock/matblock.h>
00061 
00062 #include <jwsc++/base/exception.h>
00063 
00064 #include "density.h"
00065 #include "printable.h"
00066 #include "structure.h"
00067 #include "spore.h"
00068 #include "hypha.h"
00069 #include "psf_model.h"
00070 #include "imaging_model.h"
00071 
00072 
00078 class Alternaria_density
00079 {
00080     public:
00081 
00086         Alternaria_density() throw (jwscxx::base::Arg_error);
00087 
00088 
00090         virtual ~Alternaria_density() { }
00091 
00092 
00094         float get_num_levels_p() const { return num_levels_p; }
00095 
00096 
00098         float get_spore_p() const { return spore_p; }
00099 
00100 
00102         float get_apical_hypha_p() const { return apical_hypha_p; }
00103 
00104 
00109         float get_lateral_hypha_1_p() const { return lateral_hypha_1_p; }
00110 
00111 
00116         float get_lateral_hypha_n_p() const { return lateral_hypha_n_p; }
00117 
00118 
00120         const Density::Uniform_params& get_c_1() const { return c_1; }
00121 
00122 
00124         void set_num_levels_p(float p) throw (jwscxx::base::Arg_error);
00125 
00126 
00128         void set_structure_p
00129         (
00130             float spore_p, 
00131             float apical_hypha_p, 
00132             float lateral_hypha_1_p,
00133             float lateral_hypha_n_p
00134         ) 
00135         throw (jwscxx::base::Arg_error);
00136 
00137 
00139         void set_c_1(float min, float max) throw (jwscxx::base::Arg_error);
00140 
00141 
00143         float sample_c_1() const;
00144 
00145 
00147         virtual class Alternaria* sample
00148         (
00149             size_t                       N,
00150             float                        centroid_x, 
00151             float                        centroid_y, 
00152             float                        centroid_z,
00153             float                        theta,
00154             float                        psi,
00155             size_t                       base_level,
00156             const Spore_density*         spore_d,
00157             const Apical_hypha_density*  apical_hypha_d,
00158             const Lateral_hypha_density* lateral_hypha_1_d,
00159             const Lateral_hypha_density* lateral_hypha_n_d
00160         ) 
00161         const throw (jwscxx::base::Arg_error);
00162 
00163 
00164     protected:
00165 
00167         void generate_structure
00168         (
00169             Alternaria* alternaria, 
00170             size_t      N, 
00171             float       centroid_x, 
00172             float       centroid_y, 
00173             float       centroid_z
00174         ) 
00175         const throw (jwscxx::base::Arg_error);
00176 
00177 
00178     protected:
00179 
00184         float num_levels_p;
00185 
00187         float spore_p;
00188 
00190         float apical_hypha_p;
00191 
00193         float lateral_hypha_1_p;
00194 
00196         float lateral_hypha_n_p;
00197 
00199         Density::Uniform_params  c_1;
00200 };
00201 
00202 
00208 class Alternaria : public Printable
00209 {
00210     public:
00211 
00213         Alternaria
00214         (
00215             float                        c_1,
00216             float                        theta,
00217             float                        psi,
00218             size_t                       base_level,
00219             const Alternaria_density*    alt_d,
00220             const Spore_density*         spore_d,
00221             const Apical_hypha_density*  apical_hypha_d,
00222             const Lateral_hypha_density* lateral_hypha_1_d,
00223             const Lateral_hypha_density* lateral_hypha_n_d
00224         )
00225         throw (jwscxx::base::Arg_error);
00226 
00227 
00229         Alternaria(const Alternaria& alt);
00230 
00231 
00233         Alternaria
00234         (
00235             const char*                  fname,
00236             const Alternaria_density*    alt_d,
00237             const Spore_density*         spore_d,
00238             const Apical_hypha_density*  apical_hypha_d,
00239             const Lateral_hypha_density* lateral_hypha_1_d,
00240             const Lateral_hypha_density* lateral_hypha_n_d
00241         )
00242         throw (jwscxx::base::IO_error, jwscxx::base::Arg_error);
00243 
00244 
00246         virtual ~Alternaria();
00247 
00248 
00250         Alternaria* clone() const;
00251 
00252 
00254         virtual Alternaria& operator= (const Alternaria& alternaria);
00255 
00256 
00258         Structure* get_root() const { return root; }
00259 
00260 
00262         const float get_theta() const { return theta; }
00263 
00264 
00266         const float get_psi() const { return psi; }
00267 
00268 
00270         const size_t get_base_level() const { return base_level; }
00271 
00272 
00274         const Alternaria_density* get_alt_d() const { return alt_d; }
00275 
00276 
00278         const Spore_density* get_spore_d() const { return spore_d; }
00279 
00280 
00282         const Apical_hypha_density* get_apical_hypha_d() const 
00283         { return apical_hypha_d; }
00284 
00285 
00287         const Lateral_hypha_density* get_lateral_hypha_1_d() const 
00288         { return lateral_hypha_1_d; }
00289 
00290 
00292         const Lateral_hypha_density* get_lateral_hypha_n_d() const 
00293         { return lateral_hypha_n_d; }
00294 
00295 
00297         double get_log_prob() const;
00298 
00299 
00301         float get_c_1() const { return c_1; }
00302 
00303 
00305         void set_root(Structure* root) throw (jwscxx::base::Arg_error);
00306 
00307 
00309         void set_c_1(float c_1) throw (jwscxx::base::Arg_error);
00310 
00311 
00313         virtual void print(std::ostream& out=std::cout) const;
00314 
00315 
00317         virtual void print(const char* fname) const 
00318             throw (jwscxx::base::IO_error);
00319 
00320 
00322         size_t get_num_levels() const { return level_structs.size(); }
00323 
00324 
00326         size_t get_num_structures() const;
00327 
00328 
00330         size_t get_num_apical_hypha() const;
00331 
00332 
00334         size_t get_num_lateral_hypha() const;
00335 
00336 
00338         size_t get_num_spores() const;
00339 
00340 
00342         size_t get_uniform_random_level() const 
00343             throw (jwscxx::base::Result_error);
00344 
00345 
00347         size_t get_geometric_random_level() const 
00348             throw (jwscxx::base::Result_error);
00349 
00350 
00355         Structure* get_random_structure(size_t level) const 
00356             throw (jwscxx::base::Arg_error, jwscxx::base::Result_error);
00357 
00358 
00363         Lateral_hypha* get_random_lateral_hypha(size_t level) const
00364             throw (jwscxx::base::Arg_error, jwscxx::base::Result_error);
00365 
00366 
00371         Apical_hypha* get_random_apical_hypha(size_t level) const
00372             throw (jwscxx::base::Arg_error, jwscxx::base::Result_error);
00373 
00374 
00378         Spore* get_random_spore(size_t level) const
00379             throw (jwscxx::base::Arg_error, jwscxx::base::Result_error);
00380 
00381 
00386         Structure* get_random_terminal(size_t level) const
00387             throw (jwscxx::base::Arg_error, jwscxx::base::Result_error);
00388 
00389 
00394         Apical_hypha* get_random_terminal_apical_hypha(size_t level) const
00395             throw (jwscxx::base::Arg_error, jwscxx::base::Result_error);
00396 
00397 
00402         Spore* get_random_terminal_spore(size_t level) const
00403             throw (jwscxx::base::Arg_error, jwscxx::base::Result_error);
00404 
00405 
00410         Lateral_hypha* get_random_terminal_lateral_hypha(size_t level) const
00411             throw (jwscxx::base::Arg_error, jwscxx::base::Result_error);
00412 
00413 
00415         void add_lateral_hypha(Lateral_hypha* h) 
00416             throw (jwscxx::base::Arg_error);
00417 
00418 
00420         void remove_lateral_hypha(Lateral_hypha* h)
00421             throw (jwscxx::base::Arg_error);
00422 
00423 
00425         void add_apical_hypha(Apical_hypha* h) throw (jwscxx::base::Arg_error);
00426 
00427 
00429         void remove_apical_hypha(Apical_hypha* h) 
00430             throw (jwscxx::base::Arg_error);
00431 
00432 
00434         void merge_2_apical_with_spore
00435         (
00436             Apical_hypha* hypha_1,
00437             const Spore*  rvals
00438         ) 
00439         throw (jwscxx::base::Arg_error);
00440 
00441 
00446         void split_apical_hypha_into_apical
00447         (
00448             Apical_hypha*       h,
00449             const Apical_hypha* rvals_1,
00450             const Apical_hypha* rvals_2
00451         ) 
00452         throw (jwscxx::base::Arg_error);
00453 
00454 
00459         void merge_apical_hypha_with_apical
00460         (
00461             Apical_hypha*       h, 
00462             const Apical_hypha* rvals
00463         ) 
00464         throw (jwscxx::base::Arg_error);
00465 
00466 
00470         void split_apical_hypha_into_lateral
00471         (
00472             Apical_hypha*        h,
00473             const Apical_hypha*  rvals_1,
00474             const Lateral_hypha* rvals_2
00475         ) 
00476         throw (jwscxx::base::Arg_error);
00477 
00478 
00483         void merge_apical_hypha_with_lateral
00484         (
00485             Apical_hypha*       h, 
00486             Lateral_hypha*      lat,
00487             const Apical_hypha* rvals
00488         ) 
00489         throw (jwscxx::base::Arg_error);
00490 
00491 
00493         void replace_apical_hypha_with_spore
00494         (
00495             Apical_hypha* hypha,
00496             Spore*        spore
00497         )
00498         throw (jwscxx::base::Arg_error);
00499 
00500 
00502         void split_spore_into_2_apical
00503         (
00504             Spore*              spore,
00505             const Apical_hypha* rvals_1,
00506             const Apical_hypha* rvals_2
00507         ) 
00508         throw (jwscxx::base::Arg_error);
00509 
00510 
00512         void add_spore(Spore* s) throw (jwscxx::base::Arg_error);
00513 
00514 
00516         void remove_spore(Spore* s) throw (jwscxx::base::Arg_error);
00517 
00518 
00520         void replace_spore_with_apical_hypha
00521         (
00522             Spore*        spore,
00523             Apical_hypha* hypha
00524         )
00525         throw (jwscxx::base::Arg_error);
00526 
00527 
00531         void split_spore_into_spore
00532         (
00533             Spore*       s,
00534             const Spore* rvals_1,
00535             const Spore* rvals_2
00536         ) 
00537         throw (jwscxx::base::Arg_error);
00538 
00539 
00543         void merge_spore_with_spore
00544         (
00545             Spore*       s, 
00546             const Spore* rvals
00547         ) 
00548         throw (jwscxx::base::Arg_error);
00549 
00550 
00551     protected:
00552 
00554         void read(std::istream& in) 
00555             throw (jwscxx::base::IO_error, jwscxx::base::Arg_error);
00556 
00558         void build_levels(Structure* s);
00559 
00561         void check_levels();
00562 
00567         void check_c_1() const throw (jwscxx::base::Arg_error);
00568 
00570         void ensure_level_space(size_t level);
00571 
00573         void recursively_update_level(Structure* s, size_t old_level)
00574             throw (jwscxx::base::Arg_error);
00575 
00576 
00577     protected:
00578 
00583         Structure* root;
00584 
00586         float theta;
00587 
00589         float psi;
00590 
00592         size_t base_level;
00593 
00595         const Alternaria_density* alt_d;
00596 
00598         const Spore_density* spore_d;
00599 
00601         const Apical_hypha_density* apical_hypha_d;
00602 
00607         const Lateral_hypha_density* lateral_hypha_1_d;
00608 
00613         const Lateral_hypha_density* lateral_hypha_n_d;
00614 
00616         std::vector< std::vector<Structure*>* > level_structs;
00617 
00619         std::vector< std::vector<Apical_hypha*>* > level_apical_hypha;
00620 
00622         std::vector< std::vector<Lateral_hypha*>* > level_lateral_hypha;
00623 
00625         std::vector< std::vector<Spore*>* > level_spores;
00626 
00628         std::vector< std::vector<Structure*>* > level_terminals;
00629 
00631         double log_prob;
00632 
00650         float c_1;
00651 };
00652 
00653 
00659 #if defined ALTERNARIA_HAVE_FFTW3F
00660 class Alternaria_model_density : public Alternaria_density
00661 {
00662     public:
00663 
00665         virtual ~Alternaria_model_density() { }
00666 
00667 
00669         class Alternaria_model* sample
00670         (
00671             size_t                       N,
00672             float                        centroid_x, 
00673             float                        centroid_y, 
00674             float                        centroid_z,
00675             float                        theta,
00676             float                        psi,
00677             size_t                       base_level,
00678             const Spore_density*         spore_d,
00679             const Apical_hypha_density*  apical_hypha_d,
00680             const Lateral_hypha_density* lateral_hypha_1_d,
00681             const Lateral_hypha_density* lateral_hypha_n_d,
00682             const PSF_model*             psf,
00683             const Imaging_model*         imaging
00684         ) 
00685         const throw (jwscxx::base::Arg_error);
00686 };
00687 #endif
00688 
00689 
00695 #if defined ALTERNARIA_HAVE_FFTW3F
00696 class Alternaria_model : public Alternaria
00697 {
00698     public:
00699 
00701         Alternaria_model
00702         (
00703             float                        c_1,
00704             float                        theta,
00705             float                        psi,
00706             size_t                       base_level,
00707             const Alternaria_density*    alt_d,
00708             const Spore_density*         spore_d,
00709             const Apical_hypha_density*  apical_hypha_d,
00710             const Lateral_hypha_density* lateral_hypha_1_d,
00711             const Lateral_hypha_density* lateral_hypha_n_d,
00712             const PSF_model*             psf,
00713             const Imaging_model*         imaging
00714         )
00715         throw (jwscxx::base::Arg_error);
00716 
00717 
00719         Alternaria_model(const Alternaria_model& model);
00720 
00721 
00723         Alternaria_model
00724         (
00725             const Alternaria&    alt,
00726             const PSF_model*     psf,
00727             const Imaging_model* imaging
00728         );
00729 
00730 
00735         Alternaria_model
00736         (
00737             const char*                  fname,
00738             const Alternaria_density*    alt_d,
00739             const Spore_density*         spore_d,
00740             const Apical_hypha_density*  apical_hypha_d,
00741             const Lateral_hypha_density* lateral_hypha_1_d,
00742             const Lateral_hypha_density* lateral_hypha_n_d,
00743             const PSF_model*             psf,
00744             const Imaging_model*         imaging
00745         )
00746         throw (jwscxx::base::IO_error, jwscxx::base::Arg_error);
00747 
00748 
00750         virtual ~Alternaria_model();
00751 
00752 
00754         Alternaria_model* clone() const;
00755 
00756 
00758         virtual Alternaria_model& operator= (const Alternaria_model&);
00759 
00760 
00765         double calc_log_likelihood
00766         (
00767             const PSF_model*        psf,
00768             const Imaging_model*    imaging,
00769             const jwsc::Matblock_f* data,
00770             float                   data_avg
00771         ) 
00772         const throw (jwscxx::base::Arg_error);
00773 
00774 
00776         bool update_scene(const PSF_model* psf, const Imaging_model* imaging);
00777 
00778 
00783         void write_scene(const char* fname_fmt, const PSF_model* psf)
00784             const throw (jwscxx::base::IO_error);
00785 
00786 
00787     protected:
00788 
00795         jwsc::Matblock_f* scene;
00796 
00798         jwsc::Matblock_cf* s_1;
00799 
00801         jwsc::Matblock_cf* s_2;
00802 
00804         jwsc::Matblock_f* s_3;
00805 
00807         jwsc::Matblock_cf* s_4;
00808 };
00809 #endif
00810 
00811 
00812 #endif