Alternaria
fit cylinders and ellipsoids to fungus
sampler.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 
00047 #ifndef SAMPLER_H
00048 #define SAMPLER_H
00049 
00050 
00051 #include <config.h>
00052 
00053 #include <iostream>
00054 #include <sstream>
00055 #include <list>
00056 
00057 #include <inttypes.h>
00058 
00059 #include <jwsc/matblock/matblock.h>
00060 
00061 #include <jwsc++/base/exception.h>
00062 
00063 #include "imaging_model.h"
00064 #include "psf_model.h"
00065 #include "alternaria_model.h"
00066 
00067 
00073 class Sampler_move_parameters
00074 {
00075     public:
00076 
00078         Sampler_move_parameters
00079         (
00080             Alternaria_model*       alternaria,
00081             PSF_model*              psf,
00082             Imaging_model*          imaging,
00083             const jwsc::Matblock_f* data
00084         )
00085         throw (jwscxx::base::Arg_error);
00086 
00087 
00089         virtual ~Sampler_move_parameters();
00090 
00091 
00096         void init_proposals();
00097 
00098 
00100         Alternaria_model* alternaria;
00101 
00103         PSF_model* psf;
00104 
00106         Imaging_model* imaging;
00107 
00109         double ll;
00110 
00112         Alternaria_model* alternaria_proposal;
00113 
00115         PSF_model* psf_proposal;
00116 
00118         Imaging_model* imaging_proposal;
00119 
00121         double ll_proposal;
00122 
00124         const jwsc::Matblock_f* data;
00125 
00127         float data_avg;
00128 };
00129 
00130 
00136 class Sampler_move
00137 {
00138     public:
00139 
00141         virtual ~Sampler_move() { }
00142 
00143 
00145         virtual bool run(Sampler_move_parameters* params)
00146             throw (jwscxx::base::Exception) = 0;
00147 
00148 
00150         const char* get_name() const { return name; }
00151 
00152 
00157         virtual const std::ostringstream& get_info
00158         (
00159             Sampler_move_parameters* params
00160         ) = 0;
00161 
00162 
00167         virtual const std::ostringstream& get_proposal_info
00168         (
00169             Sampler_move_parameters* params
00170         ) = 0;
00171 
00172 
00174         virtual double get_prob() const = 0;
00175 
00176 
00178         virtual void set_prob(double prob) throw (jwscxx::base::Arg_error) = 0;
00179 
00180 
00181     protected:
00182 
00184         const char* name;
00185 
00187         std::ostringstream info;
00188 
00190         std::ostringstream proposal_info;
00191 };
00192 
00193 
00199 class Sampler_diffusion_move : public Sampler_move
00200 {
00201     public:
00202 
00204         Sampler_diffusion_move(const char* name, double prob) 
00205             throw (jwscxx::base::Arg_error);
00206 
00207 
00209         virtual ~Sampler_diffusion_move() { }
00210 
00211 
00213         double get_prob() const { return prob; }
00214 
00215 
00217         void set_prob(double prob) throw (jwscxx::base::Arg_error);
00218 
00219 
00220 
00221     protected:
00222 
00224         uint32_t num_accepted;
00225 
00227         uint32_t num_attempts;
00228 
00230         double prob;
00231 };
00232 
00233 
00239 class Sampler_jump_move : public Sampler_move
00240 {
00241     public:
00242 
00244         Sampler_jump_move
00245         (
00246             const char* name_1, 
00247             const char* name_2,
00248             double      prob_1, 
00249             double      prob_2
00250         )
00251         throw (jwscxx::base::Arg_error);
00252 
00253 
00255         virtual ~Sampler_jump_move() { }
00256 
00257 
00262         bool run(Sampler_move_parameters* params) 
00263             throw (jwscxx::base::Exception);
00264 
00265 
00270         virtual bool run_1(Sampler_move_parameters* params)
00271             throw (jwscxx::base::Exception) = 0;
00272 
00273 
00278         virtual bool run_2(Sampler_move_parameters* params)
00279             throw (jwscxx::base::Exception) = 0;
00280 
00281 
00286         virtual const std::ostringstream& get_info
00287         (
00288             Sampler_move_parameters* params
00289         );
00290 
00291 
00296         virtual const std::ostringstream& get_proposal_info
00297         (
00298             Sampler_move_parameters* params
00299         );
00300 
00301 
00303         double get_prob() const { return prob_1+prob_2; }
00304 
00305 
00307         void set_prob(double prob) throw (jwscxx::base::Arg_error);
00308 
00309 
00311         double get_prob_1() const { return prob_1; }
00312 
00313 
00315         const char* get_name_1() const { return name_1; }
00316 
00317 
00319         void set_prob_1(double prob_1) throw (jwscxx::base::Arg_error);
00320 
00321 
00323         double get_prob_2() const { return prob_2; }
00324 
00325 
00327         const char* get_name_2() const { return name_2; }
00328 
00329 
00331         void set_prob_2(double prob_2) throw (jwscxx::base::Arg_error);
00332 
00333 
00334     protected:
00335 
00337         uint32_t num_accepted_1;
00338 
00340         uint32_t num_attempts_1;
00341 
00343         double prob_1;
00344 
00346         const char* name_1;
00347 
00349         uint32_t num_accepted_2;
00350 
00352         uint32_t num_attempts_2;
00353 
00355         double prob_2;
00356 
00358         const char* name_2;
00359 };
00360 
00361 
00368 class Sampler
00369 {
00370     public:
00371 
00373         virtual ~Sampler();
00374 
00375 
00377         void add_move(Sampler_move* move);
00378 
00379 
00381         void normalize_move_probs() throw (jwscxx::base::Arg_error);
00382 
00383 
00385         void run
00386         (
00387             Alternaria_model**       best_alt,
00388             PSF_model**              best_psf,
00389             Imaging_model**          best_imaging,
00390             Sampler_move_parameters* params,
00391             uint32_t                 iterations,
00392             const char*              move_fname,
00393             const char*              best_fname,
00394             const char*              alt_pro_fmt
00395         )
00396         throw (jwscxx::base::Exception);
00397 
00398 
00400         void run
00401         (
00402             Alternaria_model**       best_alt,
00403             PSF_model**              best_psf,
00404             Imaging_model**          best_imaging,
00405             Sampler_move_parameters* params,
00406             uint32_t                 iterations,
00407             std::ostream&            move_out,
00408             std::ostream&            best_out,
00409             const char*              alt_pro_fmt
00410         )
00411         throw (jwscxx::base::Exception);
00412 
00413 
00414     protected:
00415 
00417         std::list<Sampler_move*> moves;
00418 };
00419 
00420 
00421 #endif