00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <cstdlib>
00021 #include <cerrno>
00022 #include <cstring>
00023 #include <stack>
00024 #include <algorithm>
00025 #include <climits>
00026 #include <iomanip>
00027 #include <map>
00028 #include <tr1/memory>
00029 #include <cvd/image_io.h>
00030 #include <cvd/image_convert.h>
00031 #include <cvd/morphology.h>
00032 #include <cvd/connected_components.h>
00033 #include <cvd/draw.h>
00034 #include <cvd/vector_image_ref.h>
00035
00036 #include <cvd/random.h>
00037 #include <cvd/timer.h>
00038 #include <gvars3/instances.h>
00039 #include <gvars3/GUI_readline.h>
00040 #include <gvars3/GStringUtil.h>
00041
00042 #include <TooN/functions/derivatives.h>
00043 #include <TooN/determinant.h>
00044 #include <TooN/SymEigen.h>
00045 #include <TooN/optimization/conjugate_gradient.h>
00046
00047 #include "conjugate_gradient_only.h"
00048 #include "forward_algorithm.h"
00049 #include "numerical_derivatives.h"
00050 #include "storm.h"
00051 #include "storm_imagery.h"
00052 #include "debug.h"
00053 #include "sampled_multispot.h"
00054 #include "mt19937.h"
00055 #include "utility.h"
00056 #include "multispot5.h"
00057
00058 using namespace std;
00059 using namespace CVD;
00060 using namespace GVars3;
00061 using namespace TooN;
00062 using namespace std::tr1;
00063
00064
00065
00066
00067
00068 FitSpotsGraphics::~FitSpotsGraphics(){}
00069
00070
00071
00072 class NullGraphics: public FitSpotsGraphics
00073 {
00074 public:
00075 virtual void init(CVD::ImageRef){}
00076 virtual void draw_krap(const std::vector<TooN::Vector<4> >&, const CVD::Image<CVD::byte>&, const BBox&, int, TooN::Vector<4>){}
00077 virtual void swap(){}
00078 virtual void draw_pixels(const std::vector<CVD::ImageRef>&, float, float, float, float){}
00079 virtual void draw_bbox(const BBox&){}
00080 virtual void glDrawCross(const TooN::Vector<2>&, int){}
00081 virtual ~NullGraphics(){}
00082 };
00083
00084
00085
00086 auto_ptr<FitSpotsGraphics> null_graphics()
00087 {
00088 return auto_ptr<FitSpotsGraphics>(new NullGraphics);
00089 }
00090
00091
00092
00093
00094
00095
00096 Vector<> spots_to_Vector(const vector<Vector<4> >& s)
00097 {
00098 Vector<> r(s.size()*4);
00099 for(unsigned int i=0; i < s.size(); i++)
00100 {
00101 r[i*4+0] = s[i][0];
00102 r[i*4+1] = s[i][1];
00103 r[i*4+2] = s[i][2];
00104 r[i*4+3] = s[i][3];
00105 }
00106 return r;
00107 }
00108
00109
00110
00111
00112
00113 vector<Vector<4> > spots_to_vector(const Vector<>& s)
00114 {
00115 vector<Vector<4> > r(s.size()/4);
00116 for(unsigned int i=0; i < r.size(); i++)
00117 r[i] = s.slice<Dynamic, 4>(i*4, 4);
00118 return r;
00119 }
00120
00121
00122
00123 Image<byte> scale_to_bytes(const Image<float>& im, float lo, float hi)
00124 {
00125 Image<byte> out(im.size());
00126 for(int r=0; r < out.size().y-0; r++)
00127 for(int c=0; c < out.size().x-0; c++)
00128 out[r][c] = (int)floor((im[r][c]-lo)*255/(hi-lo));
00129 return out;
00130 }
00131
00132
00133
00134 Image<byte> scale_to_bytes(const Image<float>& im)
00135 {
00136 float lo = *min_element(im.begin(), im.end());
00137 float hi = *max_element(im.begin(), im.end());
00138 Image<byte> out(im.size());
00139 for(int r=0; r < out.size().y-0; r++)
00140 for(int c=0; c < out.size().x-0; c++)
00141 out[r][c] = (int)floor((im[r][c]-lo)*255/(hi-lo));
00142
00143 return out;
00144 }
00145
00146
00147
00148 Image<float> average_image(const vector<Image<float> >& ims)
00149 {
00150 assert_same_size(ims);
00151 Image<float> r(ims[0].size(), 0);
00152
00153 for(unsigned int i=0; i < ims.size(); i++)
00154 transform(r.begin(), r.end(), ims[i].begin(), r.begin(), plus<float>());
00155
00156 transform(r.begin(), r.end(), r.begin(), bind2nd(multiplies<float>(), 1./ims.size()));
00157 return r;
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 class DataForMCMC
00174 {
00175 protected:
00176 const vector<ImageRef>& pixels;
00177 const vector<vector<double> >& pixel_intensities;
00178 const double mu_brightness, sigma_brightness, mu_blur, sigma_blur;
00179 const double variance;
00180 const int samples, sample_iterations;
00181 const Matrix<3> A;
00182 const Vector<3> pi;
00183 mutable MT19937& rng;
00184 public:
00185
00186 MT19937& get_rng()const
00187 {
00188 return rng;
00189 }
00190
00191 public:
00192 DataForMCMC(const vector<ImageRef>& pixels_,
00193 const vector<vector<double> >& pixel_intensities_,
00194 double mu_brightness_,
00195 double sigma_brightness_,
00196 double mu_blur_,
00197 double sigma_blur_,
00198 double variance_,
00199 int samples_,
00200 int sample_iterations_,
00201 Matrix<3> A_,
00202 Vector<3> pi_,
00203 MT19937& rng_)
00204 :pixels(pixels_),
00205 pixel_intensities(pixel_intensities_),
00206 mu_brightness(mu_brightness_),
00207 sigma_brightness(sigma_brightness_),
00208 mu_blur(mu_blur_),
00209 sigma_blur(sigma_blur_),
00210 variance(variance_),
00211 samples(samples_),
00212 sample_iterations(sample_iterations_),
00213 A(A_),
00214 pi(pi_),
00215 rng(rng_)
00216 {}
00217 };
00218
00219
00220
00221
00222
00223 class Kahan{
00224 private:
00225 double y;
00226 double c;
00227 double t;
00228 public:
00229 double sum;
00230
00231 Kahan()
00232 :c(0),sum(0)
00233 {}
00234
00235
00236
00237 void add(double i)
00238 {
00239
00240 y = i;
00241 y-= c;
00242
00243
00244 t = sum;
00245 t += y;
00246
00247
00248
00249 c = t;
00250 c -= sum;
00251 c -= y;
00252 sum = t;
00253 }
00254 };
00255
00256
00257
00258 class NegativeFreeEnergy: public DataForMCMC
00259 {
00260 public:
00261
00262
00263 NegativeFreeEnergy(const DataForMCMC& d)
00264 :DataForMCMC(d)
00265 {
00266 }
00267
00268
00269
00270
00271
00272
00273 double variance_from_sample(double sample, double samples, double base_sigma, double scale_pow) const
00274 {
00275 double scale = pow(1.25, sample * 1. / samples * scale_pow);
00276 double sigma = base_sigma * scale;
00277 double new_variance = sq(sigma);
00278
00279 return new_variance;
00280 }
00281
00282
00283
00284
00285
00286
00287 double compute_with_mask(const Vector<>& spots, const vector<vector<int> >& spot_pixels) const
00288 {
00289
00290
00291
00292 double base_sigma = sqrt(variance);
00293 double scale_pow = 100.0;
00294
00295 const unsigned int nspots = spots.size()/4;
00296 const unsigned int nframes = pixel_intensities.size();
00297 const unsigned int npixels = pixels.size();
00298 assert(spots.size() %4 == 0);
00299 assert(spot_pixels.size() == nspots);
00300 assert_same_size(spot_pixels);
00301
00302
00303 vector<vector<double> > spot_intensity;
00304 for(unsigned int i=0; i < nspots; i++)
00305 spot_intensity.push_back(compute_spot_intensity(pixels, spots.slice<Dynamic,4>(i*4,4)));
00306
00307 GibbsSampler2 sampler(pixel_intensities, spot_intensity, spots_to_vector(spots), spot_pixels, A, pi, variance, sample_iterations);
00308
00309 double sum = 0;
00310 Kahan ksum;
00311 for(int sample=0; sample < samples; sample++)
00312 {
00313
00314 double var1 = variance_from_sample(sample-2, samples, base_sigma, scale_pow);
00315 double var2 = variance_from_sample(sample-1, samples, base_sigma, scale_pow);
00316 double var3 = variance_from_sample(sample+1, samples, base_sigma, scale_pow);
00317 double var4 = variance_from_sample(sample+2, samples, base_sigma, scale_pow);
00318
00319
00320 sampler.set_variance(var2);
00321 sampler.next(DataForMCMC::get_rng());
00322
00323
00324 double err_sum=0;
00325 for(unsigned int frame=0; frame < nframes; frame++)
00326 for(unsigned int pixel=0; pixel < npixels; pixel++)
00327 err_sum -= sq(pixel_intensities[frame][pixel] - sampler.sample_intensities()[frame][pixel]);
00328
00329
00330
00331 double e1 = err_sum / (2*var1) - npixels*nframes*::log(2*M_PI*var1)/2;
00332 double e2 = err_sum / (2*var2) - npixels*nframes*::log(2*M_PI*var2)/2;
00333 double e3 = err_sum / (2*var3) - npixels*nframes*::log(2*M_PI*var3)/2;
00334 double e4 = err_sum / (2*var4) - npixels*nframes*::log(2*M_PI*var4)/2;
00335 sum += (-e1 + 8*e2 - 8*e3 + e4)/12;
00336
00337 ksum.add(-e1/12);
00338 ksum.add(8*e2/12);
00339 ksum.add(-8*e3/12);
00340 ksum.add(e4/12);
00341 }
00342
00343 double log_final = (log(variance_from_sample(samples, samples, base_sigma, scale_pow)*2*M_PI)/2) * npixels * nframes;
00344
00345 double priors=0;
00346 for(unsigned int i=0; i < nspots; i++)
00347 {
00348 priors += log_log_normal(spots[i*4+0], mu_brightness, sigma_brightness);
00349 priors += log_log_normal(spots[i*4+1], mu_blur, sigma_blur);
00350 }
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 sampler.set_variance(variance);
00363 return -(sum+priors - log_final);
00364 }
00365
00366
00367
00368
00369
00370 double operator()(const Vector<>& spots) const
00371 {
00372 double base_sigma = sqrt(variance);
00373 double scale_pow = 100.0;
00374
00375 const unsigned int nspots = spots.size()/4;
00376 const unsigned int nframes = pixel_intensities.size();
00377 const unsigned int npixels = pixels.size();
00378 assert(spots.size() %4 == 0);
00379
00380
00381 vector<vector<double> > spot_intensity;
00382 for(unsigned int i=0; i < nspots; i++)
00383 spot_intensity.push_back(compute_spot_intensity(pixels, spots.slice<Dynamic,4>(i*4,4)));
00384
00385 GibbsSampler sampler(pixel_intensities, spot_intensity, spots_to_vector(spots), A, pi, variance, sample_iterations);
00386
00387 double sum = 0;
00388 Kahan ksum;
00389 for(int sample=0; sample < samples; sample++)
00390 {
00391
00392 double var1 = variance_from_sample(sample-2, samples, base_sigma, scale_pow);
00393 double var2 = variance_from_sample(sample-1, samples, base_sigma, scale_pow);
00394 double var3 = variance_from_sample(sample+1, samples, base_sigma, scale_pow);
00395 double var4 = variance_from_sample(sample+2, samples, base_sigma, scale_pow);
00396
00397
00398 sampler.set_variance(var2);
00399 sampler.next(DataForMCMC::get_rng());
00400
00401
00402 double err_sum=0;
00403 for(unsigned int frame=0; frame < nframes; frame++)
00404 for(unsigned int pixel=0; pixel < npixels; pixel++)
00405 err_sum -= sq(pixel_intensities[frame][pixel] - sampler.sample_intensities()[frame][pixel]);
00406
00407
00408
00409 double e1 = err_sum / (2*var1) - npixels*nframes*::log(2*M_PI*var1)/2;
00410 double e2 = err_sum / (2*var2) - npixels*nframes*::log(2*M_PI*var2)/2;
00411 double e3 = err_sum / (2*var3) - npixels*nframes*::log(2*M_PI*var3)/2;
00412 double e4 = err_sum / (2*var4) - npixels*nframes*::log(2*M_PI*var4)/2;
00413 sum += (-e1 + 8*e2 - 8*e3 + e4)/12;
00414
00415 ksum.add(-e1/12);
00416 ksum.add(8*e2/12);
00417 ksum.add(-8*e3/12);
00418 ksum.add(e4/12);
00419 }
00420
00421 double log_final = (log(variance_from_sample(samples, samples, base_sigma, scale_pow)*2*M_PI)/2) * npixels * nframes;
00422
00423 double priors=0;
00424 for(unsigned int i=0; i < nspots; i++)
00425 {
00426 priors += log_log_normal(spots[i*4+0], mu_brightness, sigma_brightness);
00427 priors += log_log_normal(spots[i*4+1], mu_blur, sigma_blur);
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440 sampler.set_variance(variance);
00441 return -(sum+priors - log_final);
00442 }
00443 };
00444
00445
00446
00447
00448
00449
00450
00451 template<class Cmp, int First> struct IndexLexicographicPosition{
00452 const vector<Vector<4> >& spots;
00453
00454
00455 IndexLexicographicPosition(const vector<Vector<4> >& s)
00456 :spots(s)
00457 {}
00458
00459
00460 static const int Second = First==2?3:2;
00461
00462
00463 bool operator()(int a, int b)
00464 {
00465 Cmp cmp;
00466
00467 if(cmp(spots[a][First], spots[b][First]))
00468 return true;
00469 else if(spots[a][First] == spots[b][First])
00470 return cmp(spots[a][Second], spots[b][Second]);
00471 else
00472 return false;
00473 }
00474 };
00475
00476
00477
00478
00479
00480
00481
00482 struct SampledBackgroundData
00483 {
00484 const vector<vector<vector<double> > >& sample_intensities_without_spot;
00485 const vector<vector<double> >& pixel_intensities;
00486 const vector<ImageRef> pixels;
00487
00488 double mu_brightness, sigma_brightness, mu_blur, sigma_blur;
00489 const Matrix<3> A;
00490 const Vector<3> pi;
00491 double variance;
00492
00493 const vector<int> O;
00494
00495 SampledBackgroundData(
00496 const vector<vector<vector<double> > >& sample_intensities_without_spot_,
00497 const vector<vector<double> >& pixel_intensities_,
00498 const vector<ImageRef> pixels_,
00499 double mu_brightness_,
00500 double sigma_brightness_,
00501 double mu_blur_,
00502 double sigma_blur_,
00503 const Matrix<3> A_,
00504 const Vector<3> pi_,
00505 double variance_)
00506 :sample_intensities_without_spot(sample_intensities_without_spot_),
00507 pixel_intensities(pixel_intensities_),
00508 pixels(pixels_),
00509 mu_brightness(mu_brightness_),
00510 sigma_brightness(sigma_brightness_),
00511 mu_blur(mu_blur_),
00512 sigma_blur(sigma_blur_),
00513 A(A_),
00514 pi(pi_),
00515 variance(variance_),
00516 O(sequence(pixel_intensities.size()))
00517 {
00518 }
00519 };
00520
00521
00522
00523 struct SpotProbabilityWithSampledBackgroundFAKE: public SampledBackgroundData
00524 {
00525 SpotProbabilityWithSampledBackgroundFAKE(const SampledBackgroundData& d)
00526 :SampledBackgroundData(d)
00527 {
00528 }
00529
00530
00531 double operator()(const Vector<4>& spot) const
00532 {
00533 vector<double> spot_intensities = compute_spot_intensity(pixels, spot);
00534
00535 double sum_log_prob = 0;
00536
00537 for(unsigned int s=0; s < sample_intensities_without_spot.size(); s++)
00538 {
00539 SpotWithBackground B(sample_intensities_without_spot[s], spot_intensities, pixel_intensities, variance);
00540 double log_prob = forward_algorithm(A, pi, B, O);
00541
00542 double logprior = log_log_normal(spot[0], mu_brightness, sigma_brightness) +
00543 log_log_normal(spot[1], mu_blur, sigma_blur);
00544
00545
00546
00547 sum_log_prob += log_prob + logprior;
00548 }
00549
00550 double average_log_prob = sum_log_prob / sample_intensities_without_spot.size();
00551
00552
00553
00554 if(spot[0] < 0 || spot[1] < 0)
00555 return 1e100;
00556
00557 return average_log_prob;
00558 }
00559 };
00560
00561
00562
00563
00564
00565 struct SpotNegProbabilityDiffWithSampledBackground: public SampledBackgroundData
00566 {
00567
00568 SpotNegProbabilityDiffWithSampledBackground(const SampledBackgroundData& d)
00569 :SampledBackgroundData(d)
00570 {
00571 }
00572
00573
00574
00575 Vector<4> operator()(const Vector<4>& spot) const
00576 {
00577 if(spot[0] <= 0 || spot[1] <= 0)
00578 return Ones * std::numeric_limits<double>::quiet_NaN();
00579
00580 vector<pair<double, Vector<4> > > spot_intensities = compute_spot_intensity_derivatives(pixels, spot);
00581
00582 Vector<4> sum_diff_log = Zeros;
00583
00584 for(unsigned int s=0; s < sample_intensities_without_spot.size(); s++)
00585 {
00586 SpotWithBackground B(sample_intensities_without_spot[s], spot_intensities, pixel_intensities, variance);
00587
00588 pair<double, Vector<4> > r = forward_algorithm_deriv(A, pi, B, O);
00589
00590 sum_diff_log += r.second;
00591 }
00592
00593 Vector<4> diff_log = sum_diff_log / sample_intensities_without_spot.size();
00594
00595
00596 Vector<4> logprior_deriv = makeVector(diff_log_log_normal(spot[0], mu_brightness, sigma_brightness),
00597 diff_log_log_normal(spot[1], mu_blur, sigma_blur), 0, 0);
00598
00599 return -(diff_log + logprior_deriv);
00600 }
00601 };
00602
00603
00604
00605
00606 class FreeEnergyHessian: public DataForMCMC
00607 {
00608 public:
00609
00610
00611
00612 FreeEnergyHessian(const DataForMCMC& d)
00613 :DataForMCMC(d)
00614 {
00615 }
00616
00617
00618
00619
00620 Matrix<4> hessian(const vector<Vector<4> >& spots, int spot) const
00621 {
00622 cout << "----Computing pure MCMC hessian\n";
00623 const unsigned int nspots = spots.size();
00624 const unsigned int nframes = pixel_intensities.size();
00625 const unsigned int npixels = pixels.size();
00626 cout << spot << " " << nspots << " " << nframes << " " << npixels << endl;
00627
00628 vector<vector<double> > spot_intensity;
00629 for(unsigned int i=0; i < nspots; i++)
00630 spot_intensity.push_back(compute_spot_intensity(pixels, spots[i]));
00631
00632 vector<tuple<double, Vector<4>, Matrix<4> > > spot_hess_etc = compute_spot_intensity_hessian(pixels, spots[spot]);
00633
00634 GibbsSampler sampler(pixel_intensities, spot_intensity, spots, A, pi, variance, sample_iterations);
00635
00636
00637 Matrix<4> sum_hess1 = Zeros(spots.size());
00638 Matrix<4> sum_hess2 = Zeros(spots.size());
00639 Vector<4> sum_deriv = Zeros(spots.size());
00640
00641 for(int sample=0; sample < samples; sample++)
00642 {
00643 sampler.next(rng);
00644
00645
00646
00647 Matrix<4> hess = Zeros(spots.size());
00648 Vector<4> deriv = Zeros(spots.size());
00649 for(unsigned int frame=0; frame < nframes; frame++)
00650 {
00651 for(unsigned int pixel=0; pixel < npixels; pixel++)
00652 {
00653 double e = pixel_intensities[frame][pixel] - sampler.sample_intensities()[frame][pixel];
00654
00655 if(sampler.sample()[spot][frame] == 0)
00656 {
00657 hess += e * get<2>(spot_hess_etc[pixel]) - get<1>(spot_hess_etc[pixel]).as_col() * get<1>(spot_hess_etc[pixel]).as_row();
00658 deriv += e * get<1>(spot_hess_etc[pixel]);
00659
00660 }
00661 }
00662 }
00663
00664 hess[0][0] += hess_log_log_normal(spots[spot][0], mu_brightness, sigma_brightness);
00665 hess[1][1] += hess_log_log_normal(spots[spot][1], mu_blur, sigma_blur);
00666 sum_hess1 += hess;
00667
00668 deriv[0] += diff_log_log_normal(spots[spot][0], mu_brightness, sigma_brightness);
00669 deriv[1] += diff_log_log_normal(spots[spot][1], mu_blur, sigma_blur);
00670
00671 sum_hess2 += deriv.as_col() * deriv.as_row();
00672
00673 sum_deriv = sum_deriv + deriv;
00674 }
00675
00676 sum_hess1 /= (samples * variance);
00677 sum_hess2 /= (samples * variance);
00678 sum_deriv /= (samples * variance);
00679
00680
00681 cout << sum_hess1 << endl;
00682 cout << sum_hess2 << endl;
00683 cout << sum_deriv.as_col() * sum_deriv.as_row() << endl;
00684
00685 cout << "......." << sum_deriv << endl;
00686
00687
00688
00689
00690
00691
00692
00693 DiagonalMatrix<4> hess_prior = Zeros(spots.size());
00694
00695 cout << "sum of parts = \n" << sum_hess1 + sum_hess2 - sum_deriv.as_col() * sum_deriv.as_row() << endl;
00696
00697
00698
00699 cout << "++++Done Computing pure MCMC hessian\n";
00700 return sum_hess1 + sum_hess2 - sum_deriv.as_col() * sum_deriv.as_row();
00701 }
00702 };
00703
00704
00705 struct LessSecond
00706 {
00707
00708
00709
00710 template<class A, class B> bool operator()(const pair<A,B>& a, const pair<A,B>& b) const
00711 {
00712 return a.second < b.second;
00713 }
00714 };
00715
00716
00717 struct NthDeriv{
00718
00719 const SpotNegProbabilityDiffWithSampledBackground& compute_deriv;
00720 int i;
00721
00722 NthDeriv(const SpotNegProbabilityDiffWithSampledBackground& c, int ii)
00723 :compute_deriv(c),i(ii)
00724 {}
00725
00726 double operator()(const Vector<4>& f) const
00727 {
00728 return compute_deriv(f)[i];
00729 }
00730 };
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740 Matrix<4> sampled_background_spot_hessian_ffbs(const Vector<4>& spot, const SampledBackgroundData& d, int bs_iterations, MT19937& rng)
00741 {
00742 vector<tuple<double, Vector<4>, Matrix<4> > > spot_hess_etc = compute_spot_intensity_hessian(d.pixels, spot);
00743 vector<double> spot_intensities = compute_spot_intensity(d.pixels, spot);
00744
00745 Matrix<4> sum_hess_log = Zeros;
00746 Matrix<4> sum_diff2_log = Zeros;
00747
00748 vector<State> current_sample;
00749
00750 const unsigned int nframes = d.pixel_intensities.size();
00751 const unsigned int npixels = d.pixels.size();
00752
00753 Matrix<4> sum_hess = Zeros;
00754 Vector<4> sum_deriv = Zeros;
00755
00756 vector<pair<Matrix<4>, Vector<4> > > hess_and_deriv_part(nframes);
00757
00758 for(unsigned int s=0; s < d.sample_intensities_without_spot.size(); s++)
00759 {
00760 SpotWithBackground B(d.sample_intensities_without_spot[s], spot_intensities, d.pixel_intensities, d.variance);
00761
00762
00763
00764 for(unsigned int frame=0; frame < nframes; frame++)
00765 {
00766 Matrix<4> hess = Zeros;
00767 Vector<4> deriv = Zeros;
00768
00769 for(unsigned int pixel=0; pixel < npixels; pixel++)
00770 {
00771 double e = d.pixel_intensities[frame][pixel] - (d.sample_intensities_without_spot[s][frame][pixel] + spot_intensities[pixel]);
00772
00773 hess += e * get<2>(spot_hess_etc[pixel]) - get<1>(spot_hess_etc[pixel]).as_col() * get<1>(spot_hess_etc[pixel]).as_row();
00774 deriv += e * get<1>(spot_hess_etc[pixel]);
00775 }
00776 hess_and_deriv_part[frame] = make_pair(hess, deriv);
00777 }
00778
00779
00780 std::vector<array<double, 3> > delta = forward_algorithm_delta(d.A, d.pi, B, d.O);
00781
00782 for(int i=0; i < bs_iterations; i++)
00783 {
00784 current_sample = backward_sampling<3,State>(d.A, delta, rng);
00785
00786 Matrix<4> hess = Zeros;
00787 Vector<4> deriv = Zeros;
00788 for(unsigned int frame=0; frame < nframes; frame++)
00789 if(current_sample[frame] == 0)
00790 {
00791 hess += hess_and_deriv_part[frame].first;
00792 deriv += hess_and_deriv_part[frame].second;
00793 }
00794
00795 sum_hess += hess + deriv.as_col() * deriv.as_row();
00796 sum_deriv += deriv;
00797 }
00798 }
00799
00800 sum_hess /= (bs_iterations * d.sample_intensities_without_spot.size() * d.variance);
00801 sum_deriv /= (bs_iterations * d.sample_intensities_without_spot.size() * d.variance);
00802
00803 sum_hess -= sum_deriv.as_col() * sum_deriv.as_row();
00804
00805 sum_hess[0][0] += hess_log_log_normal(spot[0], d.mu_brightness, d.sigma_brightness);
00806 sum_hess[1][1] += hess_log_log_normal(spot[1], d.mu_blur, d.sigma_blur);
00807 sum_deriv[0] += diff_log_log_normal(spot[0], d.mu_brightness, d.sigma_brightness);
00808 sum_deriv[1] += diff_log_log_normal(spot[1], d.mu_blur, d.sigma_blur);
00809
00810
00811
00812
00813 return sum_hess;
00814 }
00815
00816
00817 Matrix<4> sampled_background_spot_hessian2(const Vector<4>& spot, const SampledBackgroundData& d)
00818 {
00819 vector<tuple<double, Vector<4>, Matrix<4> > > spot_intensities = compute_spot_intensity_hessian(d.pixels, spot);
00820
00821 Matrix<4> sum_hess_log = Zeros;
00822 Matrix<4> sum_diff2_log = Zeros;
00823
00824 for(unsigned int s=0; s < d.sample_intensities_without_spot.size(); s++)
00825 {
00826 SpotWithBackground B(d.sample_intensities_without_spot[s], spot_intensities, d.pixel_intensities, d.variance);
00827
00828 double prob;
00829 Vector<4> diff;
00830 Matrix<4> hess;
00831
00832 tie(prob, diff, hess) = forward_algorithm_hessian(d.A, d.pi, B, d.O);
00833
00834 sum_hess_log += hess;
00835
00836 diff += makeVector(diff_log_log_normal(spot[0], d.mu_brightness, d.sigma_brightness), diff_log_log_normal(spot[1], d.mu_blur, d.sigma_blur), 0, 0);
00837 sum_diff2_log += diff.as_col() * diff.as_row();
00838 }
00839
00840 Matrix<4> hess_log = sum_hess_log / d.sample_intensities_without_spot.size();
00841 Matrix<4> diff2_log = sum_diff2_log / d.sample_intensities_without_spot.size();
00842
00843
00844
00845 hess_log[0][0] += hess_log_log_normal(spot[0], d.mu_brightness, d.sigma_brightness);
00846 hess_log[1][1] += hess_log_log_normal(spot[1], d.mu_blur, d.sigma_blur);
00847
00848 return hess_log + diff2_log;
00849 }
00850
00851
00852 Matrix<4> sampled_background_spot_hessian_FAKE(const Vector<4>& spot, const SampledBackgroundData& d)
00853 {
00854 vector<tuple<double, Vector<4>, Matrix<4> > > spot_intensities = compute_spot_intensity_hessian(d.pixels, spot);
00855
00856 Matrix<4> sum_hess_log = Zeros;
00857
00858 for(unsigned int s=0; s < d.sample_intensities_without_spot.size(); s++)
00859 {
00860 SpotWithBackground B(d.sample_intensities_without_spot[s], spot_intensities, d.pixel_intensities, d.variance);
00861
00862 double prob;
00863 Vector<4> diff;
00864 Matrix<4> hess;
00865
00866 tie(prob, diff, hess) = forward_algorithm_hessian(d.A, d.pi, B, d.O);
00867
00868 sum_hess_log += hess;
00869 }
00870
00871 Matrix<4> hess_log = sum_hess_log / d.sample_intensities_without_spot.size();
00872
00873
00874
00875 hess_log[0][0] += hess_log_log_normal(spot[0], d.mu_brightness, d.sigma_brightness);
00876 hess_log[1][1] += hess_log_log_normal(spot[1], d.mu_blur, d.sigma_blur);
00877
00878 return hess_log;
00879 }
00880
00881
00882
00883
00884 void get_spot_pixels(const vector<ImageRef>& pixels, const Vector<4>& spot, vector<int>& out)
00885 {
00886
00887
00888 vector<ImageRef> pix = getDisc(spot[1]*6 + 1);
00889 out.resize(0);
00890 ImageRef offset = ir_rounded(spot.slice<2,2>());
00891 for(unsigned int j=0; j < pix.size(); j++)
00892 {
00893 int pos = lower_bound(pixels.begin(), pixels.end(), pix[j] + offset) - pixels.begin();
00894 if(pos != (int)pixels.size() && pixels[pos] == pix[j] + offset)
00895 out.push_back(pos);
00896 }
00897
00898 if(out.size() == 0)
00899 {
00900 cout << "********************************\n";
00901 cout << "********************************\n";
00902 cout << "********************************\n";
00903 cout << "********************************\n";
00904 cout << "********************************\n";
00905 cout << "Oe noes!11one\n";
00906 cout << pix.size() << endl;
00907 }
00908 }
00909
00910
00911
00912
00913 vector<string> split(const string& line)
00914 {
00915 vector<string> v;
00916 istringstream i(line);
00917 string s;
00918
00919 while(!i.eof())
00920 {
00921 i >> s;
00922 if(i.fail())
00923 break;
00924 v.push_back(s);
00925 }
00926 return v;
00927 }
00928
00929
00930
00931
00932
00933 template<class C> inline string xtoa(const C& x)
00934 {
00935 ostringstream os;
00936 os << x;
00937 return os.str();
00938 }
00939
00940
00941
00942
00943
00944
00945 template<class C> inline C atox(const string& s, const string& msg)
00946 {
00947 C c;
00948 istringstream i(s);
00949 i >> c;
00950 if(i.fail())
00951 throw LogFileParseError("Error parsing " + msg + ". Text is `" + s + "'.");
00952 return c;
00953 }
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984 StateParameters parse_log_file(istream& in)
00985 {
00986
00987 string line;
00988
00989
00990 string rngline, passline, iterationline;
00991 bool state_ok=0;
00992
00993
00994 string new_rngline, new_passline, new_iterationline;
00995 bool new_rngline_ok=0, new_passline_ok=0, new_iterationline_ok=0;
00996
00997 unsigned int lineno=0;
00998 bool doing_gvars = 0;
00999
01000 vector<ImageRef> pixels;
01001
01002 while(!in.eof())
01003 {
01004 getline(in, line);
01005 if(in.fail())
01006 break;
01007
01008 lineno++;
01009
01010 if(line == "ENDGVARLIST")
01011 {
01012 if(!doing_gvars)
01013 throw LogFileParseError("Spurious end of GVars");
01014 doing_gvars = 0;
01015 }
01016 else if(doing_gvars)
01017 {
01018 GUI.ParseLine(line);
01019 }
01020 else if(line == "BEGINGVARLIST")
01021 {
01022 doing_gvars = 1;
01023 }
01024 if(line.substr(0, 11) == "Iteration: ")
01025 {
01026 new_iterationline = line;
01027 new_iterationline_ok = true;
01028 }
01029 else if(line.substr(0, 4) == "PASS")
01030 {
01031 new_passline = line;
01032 if(new_passline_ok)
01033 throw LogFileParseError("Duplicate PASS on line " + xtoa(lineno));
01034 new_passline_ok = true;
01035 }
01036 else if(line.substr(0, 8) == "MT19937 ")
01037 {
01038 new_rngline = line;
01039 if(new_rngline_ok)
01040 throw LogFileParseError("Duplicate MT19937 on line " + xtoa(lineno));
01041
01042 new_rngline_ok = true;
01043
01044 }
01045 else if(line == "ENDCHECKPOINT")
01046 {
01047 if(new_passline_ok && new_rngline_ok && new_iterationline_ok)
01048 {
01049 iterationline = new_iterationline;
01050 rngline = new_rngline;
01051 passline = new_passline;
01052 }
01053 else
01054 throw LogFileParseError("Reached checkpoint with missing data: "
01055 "it=" + xtoa(new_iterationline_ok) +
01056 " pa=" + xtoa(new_passline_ok) +
01057 " rg=" + xtoa(new_rngline_ok) + " on line " + xtoa(lineno));
01058
01059
01060
01061 new_rngline_ok = 0;
01062 new_passline_ok = 0;
01063
01064 state_ok = true;
01065 }
01066 else if(line.substr(0, 7) == "PIXELS ")
01067 {
01068 vector<string> l = split(line);
01069 if( (l.size() - 1)%2 == 0)
01070 {
01071 int n = (l.size()-1)/2;
01072 pixels.resize(n);
01073 for(int i=0; i < n; i++)
01074 {
01075 pixels[i].x = atox<int>(l[i*2+1+0], "pixels");
01076 pixels[i].y = atox<int>(l[i*2+1+1], "pixels");
01077 }
01078 }
01079 else
01080 throw LogFileParseError("Bad PIXELS line");
01081 }
01082 }
01083
01084 if(!state_ok)
01085 throw LogFileParseError("No state found");
01086
01087 if(pixels.size() == 0)
01088 throw LogFileParseError("No pixels, or pixels is empty");
01089
01090
01091 StateParameters p;
01092 vector<string> l;
01093
01094
01095 l = split(iterationline);
01096 p.iteration = atox<int>(l[1], "iteration");
01097
01098
01099 p.rng =shared_ptr<MT19937>(new MT19937);
01100 {
01101 istringstream rng_s(rngline);
01102 try{
01103 p.rng->read(rng_s);
01104 }
01105 catch(MT19937::ParseError p)
01106 {
01107 throw LogFileParseError("Error parsing MT19937");
01108 }
01109 }
01110
01111
01112 l = split(passline);
01113 if( (l.size() - 1 ) % 4 == 0)
01114 {
01115 p.pass = atox<int>(l[0].substr(4), "pass");
01116
01117 for(unsigned int i=0; i < (l.size()-1)/4; i++)
01118 {
01119 cout << l[i*4+1+0] << endl;
01120 cout << l[i*4+1+1] << endl;
01121 cout << l[i*4+1+2] << endl;
01122 cout << l[i*4+1+3] << endl;
01123 p.spots.push_back(makeVector(
01124 atox<double>(l[i*4+1+0], "spot"),
01125 atox<double>(l[i*4+1+1], "spot"),
01126 atox<double>(l[i*4+1+2], "spot"),
01127 atox<double>(l[i*4+1+3], "spot")));
01128 }
01129
01130 }
01131 else
01132 throw LogFileParseError("Wrong number of elements in PASS line");
01133
01134
01135 p.pixels = pixels;
01136
01137 return p;
01138 }
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148 StateParameters generate_state_parameters_ye_olde(const BasicImage<double>& log_ratios, const vector<Image<float> >& ims, vector<ImageRef> pixels){
01149 sort(pixels.begin(), pixels.end());
01150
01151 const double variance = 1;
01152
01153
01154
01155 const double intensity_mu = GV3::get<double>("intensity.rel_mu", 0., -1) + log(sqrt(variance));
01156 const double intensity_sigma = GV3::get<double>("intensity.rel_sigma", 0., -1);
01157 const double blur_mu = GV3::get<double>("blur.mu", 0., -1);
01158 const double blur_sigma = GV3::get<double>("blur.sigma", 0., -1);
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169 double threshold = GV3::get<double>("threshold", 0, -1);
01170 const double post_threshold = GV3::get<double>("post_dilate.threshold", -1, 1);
01171 if(post_threshold != -1)
01172 threshold = post_threshold;
01173
01174
01175
01176 const double post_dilate_radius = GV3::get<double>("post_dilate.radius", 0, -1);
01177 if(post_dilate_radius != 0)
01178 {
01179 Image<byte> pix(ims[0].size());
01180 pix.fill(0);
01181
01182 for(unsigned int i=0; i < pixels.size(); i++)
01183 pix[pixels[i]] = 255;
01184
01185 Image<byte> dilated = morphology(pix, getDisc(post_dilate_radius), Morphology::BinaryDilate<byte>());
01186
01187 pixels.clear();
01188
01189 ImageRef p(0,0);
01190 do
01191 if(dilated[p])
01192 pixels.push_back(p);
01193 while(p.next(dilated.size()));
01194 }
01195
01196
01197 assert_same_size(ims);
01198 if(log_ratios.size() != ims[0].size())
01199 {
01200 cerr << "Bad log ratios size\n";
01201 exit(1);
01202 }
01203
01204 vector<Vector<4> > spots;
01205
01206
01207 if(GV3::get<bool>("spots.auto_initialise", 1, 1))
01208 {
01209
01210 vector<ImageRef> disc = getDisc(GV3::get<double>("spot_spread", 3.1, 1));
01211
01212
01213 map<ImageRef, double> valid_pixels;
01214 for(unsigned int i=0; i < pixels.size(); i++)
01215 if(log_ratios[pixels[i]] > threshold)
01216 valid_pixels.insert(make_pair(pixels[i], log_ratios[pixels[i]]));
01217
01218
01219
01220 ImageRef neighbours[8] = {
01221 ImageRef(-1, -1),
01222 ImageRef( 0, -1),
01223 ImageRef( 1, -1),
01224
01225 ImageRef(-1, 0),
01226 ImageRef( 1, 0),
01227
01228 ImageRef(-1, 1),
01229 ImageRef( 0, 1),
01230 ImageRef( 1, 1),
01231 };
01232 for(unsigned int i=0; i < pixels.size(); i++)
01233 {
01234 if(!(log_ratios[pixels[i]] > threshold))
01235 goto not_a_maximum;
01236
01237 for(int j=0; j < 8; j++)
01238 if(!log_ratios.in_image(pixels[i] + neighbours[j]) || ! (log_ratios[pixels[i]] > log_ratios[pixels[i] + neighbours[j]]))
01239 goto not_a_maximum;
01240
01241 spots.push_back(makeVector(log_normal_mode(intensity_mu, intensity_sigma), log_normal_mode(blur_mu, blur_sigma), pixels[i].x, pixels[i].y));
01242
01243
01244 for(unsigned int j=0; j < disc.size(); j++)
01245 valid_pixels.erase(pixels[i] + disc[j]);
01246
01247 not_a_maximum:;
01248 }
01249
01250 for(unsigned int i=0; i < spots.size(); i++)
01251 cout << spots[i] << endl;
01252
01253
01254 while(!valid_pixels.empty())
01255 {
01256 ImageRef p = max_element(valid_pixels.begin(), valid_pixels.end(), LessSecond())->first;
01257 spots.push_back(makeVector(log_normal_mode(intensity_mu, intensity_sigma), log_normal_mode(blur_mu, blur_sigma), p.x, p.y));
01258
01259 for(unsigned int j=0; j < disc.size(); j++)
01260 valid_pixels.erase(p + disc[j]);
01261 }
01262
01263
01264
01265 double extra_r = GV3::get<double>("extra_spots", 0, 1);
01266 vector<ImageRef> extra = getDisc(extra_r);
01267 vector<Vector<4> > more_spots;
01268 for(unsigned int i=0; i < extra.size(); i++)
01269 if(extra[i] != ImageRef_zero)
01270 for(unsigned int j=0; j < spots.size(); j++)
01271 more_spots.push_back(spots[j] + makeVector(0, 0, extra[i].x, extra[i].y) / (2*extra_r+1));
01272
01273 copy(more_spots.begin(), more_spots.end(), back_inserter(spots));
01274 }
01275 else
01276 {
01277 Vector<> loaded_spots = GV3::get<Vector<> >("spots.manual_spots", "", -1);
01278
01279 if(loaded_spots.size()%4 != 0)
01280 {
01281 cerr << "Loaded spot size is not a multiple of 4\n";
01282 exit(1);
01283 }
01284
01285 else
01286 spots = spots_to_vector(loaded_spots);
01287 }
01288
01289
01290 shared_ptr<MT19937> rng(new MT19937);
01291 rng->simple_seed(GV3::get<int>("seed", 0, 1));
01292
01293
01294 int start_iteration=0;
01295 int start_pass=0;
01296 if(GV3::get<bool>("checkpoint", 0, 1))
01297 {
01298 string rng_state = GV3::get<string>("checkpoint.rng.state", "", -1);
01299 istringstream rs(rng_state);
01300 rng->read(rs);
01301 start_iteration=GV3::get<int>("checkpoint.iteration", 0, -1);
01302 start_pass=GV3::get<int>("checkpoint.pass", 0, -1);
01303 }
01304
01305 StateParameters p;
01306 p.spots = spots;
01307 p.rng = rng;
01308 p.pass = start_pass;
01309 p.iteration = start_iteration;
01310 p.pixels = pixels;
01311
01312 return p;
01313 }
01314
01315
01316
01317 set<ImageRef> dilate_mask(const vector<ImageRef>& v, double r)
01318 {
01319 vector<ImageRef> m = getDisc(r);
01320
01321 set<ImageRef> ret;
01322
01323 for(unsigned int i=0; i < v.size(); i++)
01324 for(unsigned int j=0; j < m.size(); j++)
01325 ret.insert(v[i] + m[j]);
01326
01327 return ret;
01328 }
01329
01330
01331
01332 double brightness_motion_limit(double mu, double sigma, bool not_one)
01333 {
01334 if(not_one)
01335 return log_normal_std(mu, sigma);
01336 else
01337 return 1;
01338 }
01339
01340
01341
01342
01343
01344
01345
01346 class FitSpots
01347 {
01348 const vector<Image<float> >& ims;
01349 FitSpotsGraphics& graphics;
01350 const vector<ImageRef> pixels;
01351
01352
01353 vector<Vector<4> > spots;
01354
01355
01356 const int start_iteration;
01357 int start_pass;
01358
01359 MT19937& rng;
01360
01361 const double variance;
01362 const double intensity_mu;
01363 const double intensity_sigma;
01364 const double blur_mu;
01365 const double blur_sigma;
01366
01367
01368
01369
01370
01371
01372
01373
01374 const double area_extra_radius;
01375 set<ImageRef> allowed_area;
01376 const int use_position_prior;
01377 const double position_prior;
01378
01379
01380 const double max_motion;
01381 const int sample_iterations;
01382
01383
01384
01385
01386 const int main_cg_max_iterations;
01387 const int main_samples;
01388 const int main_passes;
01389 const int outer_loop_iterations;
01390
01391
01392 const int add_remove_tries;
01393 const int add_remove_opt_samples;
01394 const int add_remove_opt_retries;
01395 const int add_remove_opt_hess_inner_samples;
01396 const int h_outer_samples;
01397 const int h_inner_samples;
01398 const int tsamples;
01399
01400
01401 const Image<float> ave;
01402
01403
01404
01405 ofstream& save_spots;
01406
01407
01408 double time_gibbs;
01409 double time_cg;
01410
01411
01412
01413
01414
01415
01416
01417 const bool scale_brightness_limit;
01418 const Vector<4> limit;
01419
01420 const Matrix<3> A;
01421 const Vector<3> pi;
01422
01423
01424 vector<vector<double> > pixel_intensities;
01425
01426 DataForMCMC data_for_t_mcmc;
01427 DataForMCMC data_for_h_mcmc;
01428
01429 int iteration;
01430
01431 public:
01432
01433 FitSpots(const vector<Image<float> >& ims_, FitSpotsGraphics& graphics_, StateParameters& params, ofstream& save_spots_)
01434 :ims(ims_), graphics(graphics_),
01435
01436
01437 pixels(params.pixels),
01438 spots(params.spots),
01439 start_iteration(params.iteration),
01440 start_pass(params.pass),
01441 rng(*params.rng),
01442
01443
01444
01445
01446 variance(1),
01447
01448
01449
01450 intensity_mu(GV3::get<double>("intensity.rel_mu", 0., -1) + log(sqrt(variance))),
01451 intensity_sigma(GV3::get<double>("intensity.rel_sigma", 0., -1)),
01452 blur_mu(GV3::get<double>("blur.mu", 0., -1)),
01453 blur_sigma(GV3::get<double>("blur.sigma", 0., -1)),
01454
01455
01456 area_extra_radius(GV3::get<double>("position.extra_radius", 0., -1)),
01457 allowed_area(dilate_mask(pixels, area_extra_radius)),
01458 use_position_prior(GV3::get<bool>("position.use_prior", true, -1)),
01459 position_prior(1.0 / allowed_area.size()),
01460
01461
01462 max_motion(GV3::get<double>("cg.max_motion", 0., -1)),
01463 sample_iterations(GV3::get<int>("gibbs.mixing_iterations", 0, -1)),
01464
01465
01466
01467
01468 main_cg_max_iterations(GV3::get<double>("main.cg.max_iterations", 0., -1)),
01469 main_samples(GV3::get<int>("main.gibbs.samples", 0, -1)),
01470 main_passes(GV3::get<int>("main.passes", 0, -1)),
01471 outer_loop_iterations(GV3::get<int>("main.total_iterations", 100000000, 1)),
01472
01473
01474 add_remove_tries(GV3::get<int>("add_remove.tries", 0, -1)),
01475 add_remove_opt_samples(GV3::get<int>("add_remove.optimizer.samples", 0, -1)),
01476 add_remove_opt_retries(GV3::get<int>("add_remove.optimizer.attempts", 0, -1)),
01477 add_remove_opt_hess_inner_samples(GV3::get<int>("add_remove.optimizer.hessian_inner_samples", 0, -1)),
01478 h_outer_samples(GV3::get<int>("add_remove.hessian.outer_samples", 0, -1)),
01479 h_inner_samples(GV3::get<int>("add_remove.hessian.inner_samples", 0, -1)),
01480 tsamples(GV3::get<int>("add_remove.thermo.samples", 0, -1)),
01481
01482 ave(average_image(ims_)),
01483
01484 save_spots(save_spots_),
01485
01486 time_gibbs(0),
01487 time_cg(0),
01488
01489 scale_brightness_limit(GV3::get<bool>("max_motion.use_brightness_std", 0, -1)),
01490 limit(makeVector(brightness_motion_limit(intensity_mu, intensity_sigma, scale_brightness_limit), 1, 1, 1)*max_motion),
01491
01492 A(GV3::get<Matrix<3> >("A", Zeros, 1)),
01493 pi(GV3::get<Vector<3> >("pi", Zeros, 1)),
01494
01495
01496 data_for_t_mcmc(pixels, pixel_intensities, intensity_mu, intensity_sigma, blur_mu, blur_sigma, variance, tsamples, sample_iterations, A, pi, rng),
01497 data_for_h_mcmc(pixels, pixel_intensities, intensity_mu, intensity_sigma, blur_mu, blur_sigma, variance, h_outer_samples, sample_iterations, A, pi, rng)
01498
01499 {
01500 assert_same_size(ims);
01501
01502
01503 pixel_intensities.resize(ims.size(), vector<double>(pixels.size()));
01504 for(unsigned int frame=0; frame < ims.size(); frame++)
01505 for(unsigned int p=0; p < pixels.size(); p++)
01506 pixel_intensities[frame][p] = ims[frame][pixels[p]];
01507
01508 }
01509
01510
01511 void optimize_each_spot_in_turn_for_several_passes()
01512 {
01513
01514 vector<vector<double> > spot_intensities;
01515 for(unsigned int i=0; i < spots.size(); i++)
01516 spot_intensities.push_back(compute_spot_intensity(pixels, spots[i]));
01517
01518
01519 vector<vector<int> > spot_pixels;
01520 spot_pixels.resize(spots.size());
01521 for(unsigned int s=0; s < spots.size(); s++)
01522 get_spot_pixels(pixels, spots[s], spot_pixels[s]);
01523
01524
01525
01526
01527 for(int pass=start_pass; pass < main_passes; pass++)
01528 {
01529 save_spots << "Pass: " << pass << endl;
01530 rng.write(save_spots);
01531 save_spots << endl;
01532
01533 start_pass=0;
01534 save_spots << "PASS" << pass << ": " << setprecision(20) << scientific << spots_to_Vector(spots) << endl;
01535 save_spots << "ENDCHECKPOINT" << endl << flush;
01536
01537
01538
01539
01540
01541
01542
01543 vector<int> index = sequence(spots.size());
01544
01545 int passs = pass + iteration;
01546
01547 if(passs%4 == 0)
01548 sort(index.begin(), index.end(), IndexLexicographicPosition<less<double>, 2>(spots));
01549 else if(passs%4==1)
01550 sort(index.begin(), index.end(), IndexLexicographicPosition<greater<double>, 2>(spots));
01551 else if(passs%4==1)
01552 sort(index.begin(), index.end(), IndexLexicographicPosition<less<double>, 3>(spots));
01553 else
01554 sort(index.begin(), index.end(), IndexLexicographicPosition<greater<double>, 3>(spots));
01555
01556
01557 {
01558 vector<Vector<4> > tmp_spot(index.size());
01559 vector<vector<double> > tmp_spot_intensities(index.size());
01560 vector<vector<int> > tmp_spot_pixels(index.size());
01561 for(unsigned int i=0; i < index.size(); i++)
01562 {
01563 tmp_spot[i] = spots[index[i]];
01564 swap(tmp_spot_intensities[i], spot_intensities[index[i]]);
01565 swap(tmp_spot_pixels[i], spot_pixels[i]);
01566 }
01567
01568 swap(tmp_spot, spots);
01569 swap(tmp_spot_intensities, spot_intensities);
01570 swap(tmp_spot_pixels, spot_pixels);
01571 }
01572
01573
01574 for(int s=0; s < (int)spots.size(); s++)
01575 {
01576 timer.reset();
01577
01578 vector<vector<vector<State> > > sample_list;
01579 vector<vector<vector<double> > > sample_intensities;
01580
01581 GibbsSampler2 sampler(pixel_intensities, spot_intensities, spots, spot_pixels, A, pi, variance, sample_iterations);
01582 for(int i=0; i < main_samples; i++)
01583 {
01584 sampler.next(rng);
01585 sample_list.push_back(sampler.sample());
01586 sample_intensities.push_back(sampler.sample_intensities());
01587 }
01588
01589
01590 for(unsigned int i=0; i < sample_list.size(); i++)
01591 remove_spot(sample_intensities[i], spot_intensities[s], sample_list[i][s]);
01592
01593
01594 time_gibbs += timer.reset();
01595
01596
01597 SampledBackgroundData data(sample_intensities, pixel_intensities, pixels,
01598 intensity_mu, intensity_sigma, blur_mu, blur_sigma,
01599 A, pi, variance);
01600
01601
01602 SpotNegProbabilityDiffWithSampledBackground compute_deriv(data);
01603
01604
01605 graphics.draw_pixels(pixels, 0, 0, 1, 1);
01606 graphics.draw_krap(spots, scale_to_bytes(ave), boundingbox(pixels), s);
01607 graphics.swap();
01608
01609
01610
01611
01612 ConjugateGradientOnly<4> cg(spots[s], compute_deriv, limit);
01613
01614
01615 cg.max_iterations = main_cg_max_iterations;
01616
01617
01618 #if 0
01619 cout << setprecision(10);
01620 cout << spots_to_Vector(spots) << endl;
01621 Matrix<4> hess, hess_errors;
01622 cout << "Hello, my name is Inigo Montoya\n";
01623
01624
01625
01626
01627
01628
01629
01630
01631
01632 Matrix<4> rhess = -sampled_background_spot_hessian(cg.x, data);
01633 cout << "Hess:\n" << rhess << endl;
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643
01644
01645 cout << "Deriv:" << compute_deriv(cg.x) << endl;
01646
01647
01648 FreeEnergyHessian hesscomputer(data_for_h_mcmc);
01649
01650 Matrix<4> nhess = hesscomputer.hessian(spots, 0);
01651 cout << "NHess:\n" << nhess << endl;
01652
01653 cout << "Turbo-N Hess:\n" << sampled_background_spot_hessian_ffbs(cg.x, data, 10000) << endl;
01654
01655 cout << "TI energy: " << NegativeFreeEnergy(data_for_t_mcmc)(spots_to_Vector(spots)) << endl;
01656 cout << "FA energy: " << SpotProbabilityWithSampledBackgroundFAKE(data)(cg.x) << endl;
01657
01658
01659
01660
01661 exit(0);
01662 #endif
01663
01664 while(cg.iterate(compute_deriv))
01665 {
01666 graphics.draw_krap(spots, scale_to_bytes(ave), boundingbox(pixels), s, cg.x);
01667 graphics.draw_pixels(pixels, 0, 0, 1, .2);
01668 graphics.swap();
01669
01670 }
01671
01672
01673 spots[s] = cg.x;
01674
01675
01676 graphics.draw_krap(spots, scale_to_bytes(ave), boundingbox(pixels), -1);
01677 graphics.swap();
01678
01679
01680 spot_intensities[s] = compute_spot_intensity(pixels, spots[s]);
01681
01682
01683 get_spot_pixels(pixels, spots[s], spot_pixels[s]);
01684
01685
01686
01687
01688 ImageRef quantized_spot_position = ir_rounded(spots[s].slice<2,2>());
01689 bool zero_prior = use_position_prior && (allowed_area.count(quantized_spot_position)==0);
01690
01691
01692 if(spot_pixels[s].empty() || zero_prior)
01693 {
01694
01695 cout << " Erasing ejected spot: " << spot_pixels[s].empty() << " " << zero_prior << endl;
01696 cout << spots[s] << endl;
01697
01698
01699 spot_intensities.erase(spot_intensities.begin() + s);
01700 spot_pixels.erase(spot_pixels.begin() + s);
01701 spots.erase(spots.begin() + s);
01702 s--;
01703
01704 }
01705
01706
01707 time_cg += timer.reset();
01708
01709
01710
01711 }
01712 }
01713 }
01714
01715
01716 void try_modifying_model()
01717 {
01718
01719 for(int i=0; i < add_remove_tries; i++)
01720 {
01721 cout << endl << endl << "Modifying the model" << endl << "======================\n";
01722 cout << "Hello\n";
01723 bool add_spot = (rng() > 0.5) || (spots.size() == 1);
01724 cout << "World\n";
01725
01726 vector<Vector<4> > model_1, model_2;
01727
01728
01729 int original;
01730
01731 if(add_spot)
01732 {
01733 model_1 = spots;
01734 model_2 = model_1;
01735
01736
01737 int r;
01738 do
01739 {
01740 r = (int)(rng() * pixels.size());
01741
01742 }
01743 while(0);
01744
01745
01746
01747
01748
01749
01750 model_2.push_back(makeVector(log_normal_mode(intensity_mu, intensity_sigma),
01751 log_normal_mode(blur_mu, blur_sigma),
01752 pixels[r].x + rng()-.5, pixels[r].y + rng() - .5));
01753 cout << "Adding a spot\n";
01754
01755 original = 1;
01756 }
01757 else
01758 {
01759
01760 int a_random_spot = static_cast<int>(rng() * spots.size());
01761 model_1 = spots;
01762 swap(model_1[model_1.size()-1], model_1[a_random_spot]);
01763
01764 model_2 = model_1;
01765
01766 model_1.pop_back();
01767 cout << "Removing a spot\n";
01768 original = 2;
01769 }
01770
01771
01772 const int spot = model_2.size() - 1;
01773
01774 cout << "Original model: " << original << endl;
01775
01776
01777
01778
01779 vector<vector<double> > model2_spot_intensities;
01780 for(unsigned int i=0; i < model_2.size(); i++)
01781 model2_spot_intensities.push_back(compute_spot_intensity(pixels, model_2[i]));
01782
01783
01784 vector<vector<int> > model2_spot_pixels(model_2.size());
01785 for(unsigned int s=0; s < model_2.size(); s++)
01786 get_spot_pixels(pixels, model_2[s], model2_spot_pixels[s]);
01787
01788
01789 {
01790 cout << "Optimizing spot for model selection\n";
01791
01792
01793
01794 vector<vector<vector<State> > > sample_list;
01795 vector<vector<vector<double> > > sample_intensities;
01796
01797 GibbsSampler2 sampler(pixel_intensities, model2_spot_intensities, model_2, model2_spot_pixels, A, pi, variance, sample_iterations);
01798 for(int i=0; i < add_remove_opt_samples; i++)
01799 {
01800 sampler.next(rng);
01801 sample_list.push_back(sampler.sample());
01802 sample_intensities.push_back(sampler.sample_intensities());
01803 }
01804
01805
01806 for(unsigned int i=0; i < sample_list.size(); i++)
01807 remove_spot(sample_intensities[i], model2_spot_intensities[spot], sample_list[i][spot]);
01808
01809
01810 SampledBackgroundData data(sample_intensities, pixel_intensities, pixels,
01811 intensity_mu, intensity_sigma, blur_mu, blur_sigma,
01812 A, pi, variance);
01813
01814
01815 SpotNegProbabilityDiffWithSampledBackground compute_deriv(data);
01816
01817 graphics.draw_krap(model_2, scale_to_bytes(ave), boundingbox(pixels), spot);
01818 graphics.swap();
01819
01820
01821
01822
01823 ConjugateGradientOnly<4> cg(model_2[spot], compute_deriv, limit);
01824
01825
01826 for(int attempt=0; attempt < add_remove_opt_retries; attempt++)
01827 {
01828 cout << "Attempt " << attempt << " of " << add_remove_opt_retries << endl;
01829
01830
01831 while(cg.iterate(compute_deriv))
01832 {
01833 graphics.draw_krap(model_2, scale_to_bytes(ave), boundingbox(pixels), spot, cg.x);
01834 graphics.swap();
01835 }
01836
01837
01838
01839
01840
01841
01842 if(attempt < add_remove_opt_retries - 1)
01843 {
01844 Matrix<4> hessian = sampled_background_spot_hessian_ffbs(cg.x, data, add_remove_opt_hess_inner_samples, rng);
01845 SymEigen<4> hess_decomp(hessian);
01846
01847
01848
01849 cout << "Eigenvalues are: " << hess_decomp.get_evalues() << endl;
01850
01851 if(hess_decomp.is_negdef())
01852 break;
01853 else
01854 {
01855
01856 cg.init(cg.x + 0.1 * hess_decomp.get_evectors()[3], (hess_decomp.get_evectors()[3]));
01857
01858
01859 cout << "Grad = " << compute_deriv(cg.x) << endl;
01860 for(int i=0; i < 4; i++)
01861 {
01862 cout << "Direction: " << i << endl;
01863 cout << unit(compute_deriv(cg.x + 0.1*hess_decomp.get_evectors()[i])) * hess_decomp.get_evectors()[i] << endl;
01864 }
01865
01866 for(int i=0; i < 4; i++)
01867 {
01868 cout << "Direction: " << i << endl;
01869 Vector<4> d = Zeros;
01870 d[i] = 1;
01871 cout << unit(compute_deriv(cg.x + d)) * hess_decomp.get_evectors()[i] << endl;
01872 }
01873 }
01874 }
01875 }
01876
01877
01878
01879 model_2[spot] = cg.x;
01880
01881 graphics.draw_krap(model_2, scale_to_bytes(ave), boundingbox(pixels), -1);
01882 graphics.swap();
01883
01884
01885
01886 model2_spot_intensities[spot] = compute_spot_intensity(pixels, model_2[spot]);
01887 get_spot_pixels(pixels, model_2[spot], model2_spot_pixels[spot]);
01888
01889 cout << "Done optimizing for model selection\n";
01890 }
01891
01892
01893
01894 int keep=original;
01895
01896
01897 bool zero_prior = use_position_prior && (allowed_area.count(ir_rounded(model_2[spot].slice<2,2>()))==0);
01898
01899 if(zero_prior)
01900 {
01901
01902 keep = 1;
01903 }
01904 else
01905 {
01906
01907
01908
01909
01910 double position_log_prior_model2_minus_model1;
01911 if(use_position_prior)
01912 position_log_prior_model2_minus_model1 = (model_2.size() - model_1.size()) * ln(position_prior);
01913 else
01914 position_log_prior_model2_minus_model1 = 0;
01915
01916
01917
01918
01919
01920
01921 Matrix<4> hess;
01922
01923
01924
01925
01926 {
01927
01928 vector<vector<vector<State> > > sample_list;
01929 vector<vector<vector<double> > > sample_intensities;
01930
01931 GibbsSampler sampler(pixel_intensities, model2_spot_intensities, model_2, A, pi, variance, sample_iterations);
01932 for(int i=0; i < h_outer_samples; i++)
01933 {
01934 sampler.next(rng);
01935 sample_list.push_back(sampler.sample());
01936 sample_intensities.push_back(sampler.sample_intensities());
01937 }
01938
01939
01940 for(unsigned int i=0; i < sample_list.size(); i++)
01941 remove_spot(sample_intensities[i], model2_spot_intensities[spot], sample_list[i][spot]);
01942
01943
01944 SampledBackgroundData data(sample_intensities, pixel_intensities, pixels,
01945 intensity_mu, intensity_sigma, blur_mu, blur_sigma,
01946 A, pi, variance);
01947
01948 hess = sampled_background_spot_hessian_ffbs(model_2[spot], data, h_inner_samples, rng);
01949 }
01950
01951
01952 double det = determinant(-hess / (M_PI*2));
01953 SymEigen<4> hess_decomp(-hess);
01954 cout << "Hessien Eigenvalues are: " << hess_decomp.get_evalues() << endl;
01955 const double smallest_evalue = 1e-6;
01956
01957
01958
01959 if(hess_decomp.get_evalues()[0] > smallest_evalue)
01960 {
01961
01962
01963 cout << "Model 2:\n";
01964
01965 double model_2_energy = -NegativeFreeEnergy(data_for_t_mcmc).compute_with_mask(spots_to_Vector(model_2), model2_spot_pixels);
01966 cout << "Energy: " << model_2_energy << endl;
01967
01968
01969 double model_2_occam = -0.5 * log(det);
01970 double model_2_prob = model_2_energy + model_2_occam + position_log_prior_model2_minus_model1;
01971
01972 cout << "Occam: " << model_2_occam << endl;
01973 cout << "Position: " << position_log_prior_model2_minus_model1 << endl;
01974 cout << "Prob: " << model_2_prob << endl;
01975
01976
01977
01978
01979
01980
01981
01982 model2_spot_pixels.pop_back();
01983 double model_1_prob = -NegativeFreeEnergy(data_for_t_mcmc).compute_with_mask(spots_to_Vector(model_1), model2_spot_pixels);
01984 cout << "Prob: " << model_1_prob << endl;
01985
01986
01987 if(model_2_prob > model_1_prob)
01988 keep=2;
01989 else
01990 keep=1;
01991
01992 cout << "Models evaluated\n";
01993 }
01994 else
01995 {
01996 cout << "Determinant has bad eigenvalues!\n";
01997 keep = original;
01998 cout << hess_decomp.get_evalues() << endl;
01999 }
02000 }
02001
02002 if(keep == 2)
02003 {
02004 spots = model_2;
02005 cout << "Keeping model 2\n";
02006
02007 }
02008 else
02009 {
02010 spots = model_1;
02011 cout << "Keeping model 1\n";
02012 }
02013
02014 if(original != keep)
02015 {
02016 cout << "Model changed!\n";
02017
02018 }
02019 }
02020 }
02021
02022
02023 void run()
02024 {
02025 graphics.init(ims[0].size());
02026 save_spots << "LOGVERSION 2" << endl;
02027 save_spots << "PIXELS";
02028 for(unsigned int i=0; i < pixels.size(); i++)
02029 save_spots << " " << pixels[i].x << " " << pixels[i].y;
02030 save_spots << endl;
02031
02032 save_spots << "BEGINGVARLIST" << endl;
02033 GV3::print_var_list(save_spots, "", 1);
02034 save_spots << "ENDGVARLIST" << endl;
02035
02036
02037
02038 cout << "Limit vector: " << limit << endl;
02039
02040 for(iteration=start_iteration; iteration < outer_loop_iterations ;iteration++)
02041 {
02042 save_spots << "Iteration: " << iteration << endl;
02043 save_spots << "MAIN: " << setprecision(20) << scientific << spots_to_Vector(spots) << endl;
02044
02045 cout << endl << endl << "----------------------" << endl << "Optimizing:\n";
02046 cout << spots.size() << endl;
02047
02048
02049 optimize_each_spot_in_turn_for_several_passes();
02050
02051
02052 try_modifying_model();
02053 }
02054 save_spots << "FINAL: " << setprecision(15) << scientific << spots_to_Vector(spots) << endl;
02055 }
02056
02057 };
02058
02059
02060
02061
02062
02063
02064
02065
02066
02067
02068 void fit_spots_historic(const BasicImage<double>& log_ratios, const vector<Image<float> >& ims, vector<ImageRef> pixels, FitSpotsGraphics& graphics)
02069 {
02070 sort(pixels.begin(), pixels.end());
02071
02072
02073 const double post_dilate_radius = GV3::get<double>("post_dilate.radius", 0, -1);
02074
02075 if(post_dilate_radius != 0)
02076 {
02077 Image<byte> pix(ims[0].size());
02078 pix.fill(0);
02079
02080 for(unsigned int i=0; i < pixels.size(); i++)
02081 pix[pixels[i]] = 255;
02082
02083 Image<byte> dilated = morphology(pix, getDisc(post_dilate_radius), Morphology::BinaryDilate<byte>());
02084
02085 pixels.clear();
02086
02087 ImageRef p(0,0);
02088 do
02089 if(dilated[p])
02090 pixels.push_back(p);
02091 while(p.next(dilated.size()));
02092 }
02093
02094
02095 const double variance = 1;
02096
02097
02098
02099 const double intensity_mu = GV3::get<double>("intensity.rel_mu", 0., -1) + log(sqrt(variance));
02100 const double intensity_sigma = GV3::get<double>("intensity.rel_sigma", 0., -1);
02101 const double blur_mu = GV3::get<double>("blur.mu", 0., -1);
02102 const double blur_sigma = GV3::get<double>("blur.sigma", 0., -1);
02103
02104 double threshold = GV3::get<double>("threshold", 0, -1);
02105
02106
02107 const double post_threshold = GV3::get<double>("post_dilate.threshold", -1, 1);
02108 if(post_threshold != -1)
02109 threshold = post_threshold;
02110
02111
02112 const double max_motion = GV3::get<double>("cg.max_motion", 0., -1);
02113 const int sample_iterations = GV3::get<int>("gibbs.mixing_iterations", 0, -1);
02114
02115
02116
02117
02118 const int main_cg_max_iterations = GV3::get<double>("main.cg.max_iterations", 0., -1);
02119 const int main_samples = GV3::get<int>("main.gibbs.samples", 0, -1);
02120 const int main_passes = GV3::get<int>("main.passes", 0, -1);
02121 const int outer_loop_iterations = GV3::get<int>("main.total_iterations", 100000000, 1);
02122
02123
02124 const int add_remove_tries = GV3::get<int>("add_remove.tries", 0, -1);
02125 const int add_remove_opt_samples = GV3::get<int>("add_remove.optimizer.samples", 0, -1);
02126 const int add_remove_opt_retries = GV3::get<int>("add_remove.optimizer.attempts", 0, -1);
02127 const int add_remove_opt_hess_inner_samples = GV3::get<int>("add_remove.optimizer.hessian_inner_samples", 0, -1);
02128 const int h_outer_samples = GV3::get<int>("add_remove.hessian.outer_samples", 0, -1);
02129 const int h_inner_samples = GV3::get<int>("add_remove.hessian.inner_samples", 0, -1);
02130 const int tsamples = GV3::get<int>("add_remove.thermo.samples", 0, -1);
02131
02132 ofstream save_spots;
02133 save_spots.open(GV3::get<string>("save_spots", "", -1).c_str());
02134 int err = errno;
02135
02136 if(!save_spots.good())
02137 {
02138 cerr << "***********************************************************\n";
02139 cerr << "WARNING: failed to open " << GV3::get<string>("save_spots") << ": " <<strerror(err) << endl;
02140 cerr << "***********************************************************\n";
02141 return;
02142 }
02143
02144 const Matrix<3> A = GV3::get<Matrix<3> >("A", Zeros, 1);
02145 const Vector<3> pi = GV3::get<Vector<3> >("pi", Zeros, 1);
02146
02147
02148
02149
02150
02151
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161 graphics.init(log_ratios.size());
02162
02163 assert_same_size(ims);
02164 if(log_ratios.size() != ims[0].size())
02165 {
02166 cerr << "Bad log ratios size\n";
02167 exit(1);
02168 }
02169
02170 const Image<float> ave = average_image(ims);
02171
02172 vector<Vector<4> > spots;
02173
02174 if(GV3::get<bool>("spots.auto_initialise", 1, 1))
02175 {
02176 vector<ImageRef> disc = getDisc(GV3::get<double>("spot_spread", 3.1, 1));
02177
02178
02179 map<ImageRef, double> valid_pixels;
02180 for(unsigned int i=0; i < pixels.size(); i++)
02181 if(log_ratios[pixels[i]] > threshold)
02182 valid_pixels.insert(make_pair(pixels[i], log_ratios[pixels[i]]));
02183
02184
02185
02186 ImageRef neighbours[8] = {
02187 ImageRef(-1, -1),
02188 ImageRef( 0, -1),
02189 ImageRef( 1, -1),
02190
02191 ImageRef(-1, 0),
02192 ImageRef( 1, 0),
02193
02194 ImageRef(-1, 1),
02195 ImageRef( 0, 1),
02196 ImageRef( 1, 1),
02197 };
02198 for(unsigned int i=0; i < pixels.size(); i++)
02199 {
02200 if(!(log_ratios[pixels[i]] > threshold))
02201 goto not_a_maximum;
02202
02203 for(int j=0; j < 8; j++)
02204 if(!log_ratios.in_image(pixels[i] + neighbours[j]) || ! (log_ratios[pixels[i]] > log_ratios[pixels[i] + neighbours[j]]))
02205 goto not_a_maximum;
02206
02207 spots.push_back(makeVector(log_normal_mode(intensity_mu, intensity_sigma), log_normal_mode(blur_mu, blur_sigma), pixels[i].x, pixels[i].y));
02208
02209
02210 for(unsigned int j=0; j < disc.size(); j++)
02211 valid_pixels.erase(pixels[i] + disc[j]);
02212
02213 not_a_maximum:;
02214 }
02215
02216
02217
02218
02219
02220
02221
02222
02223
02224
02225
02226 for(unsigned int i=0; i < spots.size(); i++)
02227 {
02228 cout << spots[i] << endl;
02229
02230 }
02231
02232
02233
02234
02235
02236
02237
02238
02239
02240
02241
02242 while(!valid_pixels.empty())
02243 {
02244 ImageRef p = max_element(valid_pixels.begin(), valid_pixels.end(), LessSecond())->first;
02245 spots.push_back(makeVector(log_normal_mode(intensity_mu, intensity_sigma), log_normal_mode(blur_mu, blur_sigma), p.x, p.y));
02246
02247 for(unsigned int j=0; j < disc.size(); j++)
02248 valid_pixels.erase(p + disc[j]);
02249
02250
02251
02252
02253
02254
02255
02256
02257
02258
02259
02260
02261
02262
02263
02264
02265
02266 }
02267
02268
02269
02270
02271
02272
02273 double extra_r = GV3::get<double>("extra_spots", 0, 1);
02274 vector<ImageRef> extra = getDisc(extra_r);
02275 vector<Vector<4> > more_spots;
02276 for(unsigned int i=0; i < extra.size(); i++)
02277 if(extra[i] != ImageRef_zero)
02278 for(unsigned int j=0; j < spots.size(); j++)
02279 more_spots.push_back(spots[j] + makeVector(0, 0, extra[i].x, extra[i].y) / (2*extra_r+1));
02280
02281 copy(more_spots.begin(), more_spots.end(), back_inserter(spots));
02282
02283 }
02284 else
02285 {
02286 Vector<> loaded_spots = GV3::get<Vector<> >("spots.manual_spots", "", -1);
02287
02288 if(loaded_spots.size()%4 != 0)
02289 {
02290 cerr << "Loaded spot size is not a multiple of 4\n";
02291 exit(1);
02292 }
02293
02294 else
02295 spots = spots_to_vector(loaded_spots);
02296
02297
02298
02299
02300
02301
02302
02303
02304
02305
02306
02307
02308
02309
02310
02311
02312
02313
02314 }
02315
02316
02317
02318
02319 vector<vector<double> > pixel_intensities(ims.size(), vector<double>(pixels.size()));
02320 for(unsigned int frame=0; frame < ims.size(); frame++)
02321 for(unsigned int p=0; p < pixels.size(); p++)
02322 pixel_intensities[frame][p] = ims[frame][pixels[p]];
02323
02324
02325
02326 MT19937 rng;
02327 rng.simple_seed(GV3::get<int>("seed", 0, 1));
02328
02329 int start_iteration=0;
02330 int start_pass=0;
02331 if(GV3::get<bool>("checkpoint", 0, 1))
02332 {
02333 string rng_state = GV3::get<string>("checkpoint.rng.state", "", -1);
02334 istringstream rs(rng_state);
02335 rng.read(rs);
02336 start_iteration=GV3::get<int>("checkpoint.iteration", 0, -1);
02337 start_pass=GV3::get<int>("checkpoint.pass", 0, -1);
02338 }
02339
02340 DataForMCMC data_for_t_mcmc(pixels, pixel_intensities, intensity_mu, intensity_sigma, blur_mu, blur_sigma, variance, tsamples, sample_iterations, A, pi, rng);
02341 DataForMCMC data_for_h_mcmc(pixels, pixel_intensities, intensity_mu, intensity_sigma, blur_mu, blur_sigma, variance, h_outer_samples, sample_iterations, A, pi, rng);
02342
02343
02344
02345
02346 double time_gibbs=0;
02347 double time_cg = 0;
02348
02349 const Vector<4> limit = Ones * max_motion;
02350
02351 for(int iteration=start_iteration; iteration < outer_loop_iterations ;iteration++)
02352 {
02353 save_spots << "Iteration: " << iteration << endl;
02354 save_spots << "MAIN: " << setprecision(20) << scientific << spots_to_Vector(spots) << endl;
02355
02356 cout << endl << endl << "----------------------" << endl << "Optimizing:\n";
02357 cout << spots.size() << endl;
02358
02359 {
02360
02361 vector<vector<double> > spot_intensities;
02362 for(unsigned int i=0; i < spots.size(); i++)
02363 spot_intensities.push_back(compute_spot_intensity(pixels, spots[i]));
02364
02365
02366 vector<vector<int> > spot_pixels;
02367 spot_pixels.resize(spots.size());
02368 for(unsigned int s=0; s < spots.size(); s++)
02369 get_spot_pixels(pixels, spots[s], spot_pixels[s]);
02370
02371
02372
02373
02374 for(int pass=start_pass; pass < main_passes; pass++)
02375 {
02376 save_spots << "Pass: " << pass << endl;
02377 rng.write(save_spots);
02378 save_spots << endl;
02379
02380 start_pass=0;
02381 save_spots << "PASS" << pass << ": " << setprecision(20) << scientific << spots_to_Vector(spots) << endl;
02382 save_spots << "ENDCHECKPOINT" << endl << flush;
02383
02384
02385
02386
02387
02388
02389
02390 vector<int> index = sequence(spots.size());
02391
02392 int passs = pass + iteration;
02393
02394 if(passs%4 == 0)
02395 sort(index.begin(), index.end(), IndexLexicographicPosition<less<double>, 2>(spots));
02396 else if(passs%4==1)
02397 sort(index.begin(), index.end(), IndexLexicographicPosition<greater<double>, 2>(spots));
02398 else if(passs%4==1)
02399 sort(index.begin(), index.end(), IndexLexicographicPosition<less<double>, 3>(spots));
02400 else
02401 sort(index.begin(), index.end(), IndexLexicographicPosition<greater<double>, 3>(spots));
02402
02403
02404 {
02405 vector<Vector<4> > tmp_spot(index.size());
02406 vector<vector<double> > tmp_spot_intensities(index.size());
02407 vector<vector<int> > tmp_spot_pixels(index.size());
02408 for(unsigned int i=0; i < index.size(); i++)
02409 {
02410 tmp_spot[i] = spots[index[i]];
02411 swap(tmp_spot_intensities[i], spot_intensities[index[i]]);
02412 swap(tmp_spot_pixels[i], spot_pixels[i]);
02413 }
02414
02415 swap(tmp_spot, spots);
02416 swap(tmp_spot_intensities, spot_intensities);
02417 swap(tmp_spot_pixels, spot_pixels);
02418 }
02419
02420
02421 for(int s=0; s < (int)spots.size(); s++)
02422 {
02423 timer.reset();
02424
02425 vector<vector<vector<State> > > sample_list;
02426 vector<vector<vector<double> > > sample_intensities;
02427
02428 GibbsSampler2 sampler(pixel_intensities, spot_intensities, spots, spot_pixels, A, pi, variance, sample_iterations);
02429 for(int i=0; i < main_samples; i++)
02430 {
02431 sampler.next(rng);
02432 sample_list.push_back(sampler.sample());
02433 sample_intensities.push_back(sampler.sample_intensities());
02434 }
02435
02436
02437 for(unsigned int i=0; i < sample_list.size(); i++)
02438 remove_spot(sample_intensities[i], spot_intensities[s], sample_list[i][s]);
02439
02440
02441 time_gibbs += timer.reset();
02442
02443
02444 SampledBackgroundData data(sample_intensities, pixel_intensities, pixels,
02445 intensity_mu, intensity_sigma, blur_mu, blur_sigma,
02446 A, pi, variance);
02447
02448
02449 SpotNegProbabilityDiffWithSampledBackground compute_deriv(data);
02450
02451
02452 graphics.draw_pixels(pixels, 0, 0, 1, 1);
02453 graphics.draw_krap(spots, scale_to_bytes(ave), boundingbox(pixels), s);
02454 graphics.swap();
02455
02456
02457
02458
02459 ConjugateGradientOnly<4> cg(spots[s], compute_deriv, limit);
02460
02461
02462 cg.max_iterations = main_cg_max_iterations;
02463
02464
02465 #if 0
02466 cout << setprecision(10);
02467 cout << spots_to_Vector(spots) << endl;
02468 Matrix<4> hess, hess_errors;
02469 cout << "Hello, my name is Inigo Montoya\n";
02470
02471
02472
02473
02474
02475
02476
02477
02478
02479 Matrix<4> rhess = -sampled_background_spot_hessian(cg.x, data);
02480 cout << "Hess:\n" << rhess << endl;
02481
02482
02483
02484
02485
02486
02487
02488
02489
02490
02491
02492 cout << "Deriv:" << compute_deriv(cg.x) << endl;
02493
02494
02495 FreeEnergyHessian hesscomputer(data_for_h_mcmc);
02496
02497 Matrix<4> nhess = hesscomputer.hessian(spots, 0);
02498 cout << "NHess:\n" << nhess << endl;
02499
02500 cout << "Turbo-N Hess:\n" << sampled_background_spot_hessian_ffbs(cg.x, data, 10000) << endl;
02501
02502 cout << "TI energy: " << NegativeFreeEnergy(data_for_t_mcmc)(spots_to_Vector(spots)) << endl;
02503 cout << "FA energy: " << SpotProbabilityWithSampledBackgroundFAKE(data)(cg.x) << endl;
02504
02505
02506
02507
02508 exit(0);
02509 #endif
02510
02511 while(cg.iterate(compute_deriv))
02512 {
02513 graphics.draw_krap(spots, scale_to_bytes(ave), boundingbox(pixels), s, cg.x);
02514 graphics.draw_pixels(pixels, 0, 0, 1, .2);
02515 graphics.swap();
02516
02517 }
02518
02519
02520 spots[s] = cg.x;
02521
02522
02523 graphics.draw_krap(spots, scale_to_bytes(ave), boundingbox(pixels), -1);
02524 graphics.swap();
02525
02526
02527 spot_intensities[s] = compute_spot_intensity(pixels, spots[s]);
02528
02529
02530 get_spot_pixels(pixels, spots[s], spot_pixels[s]);
02531
02532 if(spot_pixels[s].empty())
02533 {
02534
02535 cout << " Erasing ejected spot\n";
02536 cout << spots[s] << endl;
02537
02538
02539 spot_intensities.erase(spot_intensities.begin() + s);
02540 spot_pixels.erase(spot_pixels.begin() + s);
02541 spots.erase(spots.begin() + s);
02542 s--;
02543
02544 }
02545
02546
02547 time_cg += timer.reset();
02548
02549
02550
02551 }
02552 }
02553 }
02554
02555
02556
02557 for(int i=0; i < add_remove_tries; i++)
02558 {
02559 cout << endl << endl << "Modifying the model" << endl << "======================\n";
02560 cout << "Hello\n";
02561 bool add_spot = (rng() > 0.5) || (spots.size() == 1);
02562 cout << "World\n";
02563
02564 vector<Vector<4> > model_1, model_2;
02565
02566
02567 int original;
02568
02569 if(add_spot)
02570 {
02571 model_1 = spots;
02572 model_2 = model_1;
02573
02574
02575 int r;
02576 do
02577 {
02578 r = (int)(rng() * pixels.size());
02579 cout << r << " " << log_ratios[pixels[r]] << " " << pixels[r] << " " << threshold << endl;
02580 }
02581 while(log_ratios[pixels[r]] < threshold);
02582
02583
02584 model_2.push_back(makeVector(log_normal_mode(intensity_mu, intensity_sigma),
02585 log_normal_mode(blur_mu, blur_sigma),
02586 pixels[r].x + rng()-.5, pixels[r].y + rng() - .5));
02587 cout << "Adding a spot\n";
02588
02589 original = 1;
02590 }
02591 else
02592 {
02593
02594 int a_random_spot = static_cast<int>(rng() * spots.size());
02595 model_1 = spots;
02596 swap(model_1[model_1.size()-1], model_1[a_random_spot]);
02597
02598 model_2 = model_1;
02599
02600 model_1.pop_back();
02601 cout << "Removing a spot\n";
02602 original = 2;
02603 }
02604
02605
02606 int spot = model_2.size() - 1;
02607
02608 cout << "Original model: " << original << endl;
02609
02610
02611
02612
02613 vector<vector<double> > model2_spot_intensities;
02614 for(unsigned int i=0; i < model_2.size(); i++)
02615 model2_spot_intensities.push_back(compute_spot_intensity(pixels, model_2[i]));
02616
02617
02618 vector<vector<int> > model2_spot_pixels(model_2.size());
02619 for(unsigned int s=0; s < model_2.size(); s++)
02620 get_spot_pixels(pixels, model_2[s], model2_spot_pixels[s]);
02621
02622
02623 {
02624 cout << "Optimizing spot for model selection\n";
02625
02626
02627
02628 vector<vector<vector<State> > > sample_list;
02629 vector<vector<vector<double> > > sample_intensities;
02630
02631 GibbsSampler2 sampler(pixel_intensities, model2_spot_intensities, model_2, model2_spot_pixels, A, pi, variance, sample_iterations);
02632 for(int i=0; i < add_remove_opt_samples; i++)
02633 {
02634 sampler.next(rng);
02635 sample_list.push_back(sampler.sample());
02636 sample_intensities.push_back(sampler.sample_intensities());
02637 }
02638
02639
02640 for(unsigned int i=0; i < sample_list.size(); i++)
02641 remove_spot(sample_intensities[i], model2_spot_intensities[spot], sample_list[i][spot]);
02642
02643
02644 SampledBackgroundData data(sample_intensities, pixel_intensities, pixels,
02645 intensity_mu, intensity_sigma, blur_mu, blur_sigma,
02646 A, pi, variance);
02647
02648
02649 SpotNegProbabilityDiffWithSampledBackground compute_deriv(data);
02650
02651 graphics.draw_krap(model_2, scale_to_bytes(ave), boundingbox(pixels), spot);
02652 graphics.swap();
02653
02654
02655
02656
02657 ConjugateGradientOnly<4> cg(model_2[spot], compute_deriv, limit);
02658
02659
02660 for(int attempt=0; attempt < add_remove_opt_retries; attempt++)
02661 {
02662 cout << "Attempt " << attempt << " of " << add_remove_opt_retries << endl;
02663
02664
02665 while(cg.iterate(compute_deriv))
02666 {
02667 graphics.draw_krap(model_2, scale_to_bytes(ave), boundingbox(pixels), spot, cg.x);
02668 graphics.swap();
02669 }
02670
02671
02672
02673
02674 if(attempt < add_remove_tries - 1)
02675 {
02676 Matrix<4> hessian = sampled_background_spot_hessian_ffbs(cg.x, data, add_remove_opt_hess_inner_samples, rng);
02677 SymEigen<4> hess_decomp(hessian);
02678
02679
02680
02681 cout << "Eigenvalues are: " << hess_decomp.get_evalues() << endl;
02682
02683 if(hess_decomp.is_negdef())
02684 break;
02685 else
02686 {
02687
02688 cg.init(cg.x + 0.1 * hess_decomp.get_evectors()[3], (hess_decomp.get_evectors()[3]));
02689
02690
02691 cout << "Grad = " << compute_deriv(cg.x) << endl;
02692 for(int i=0; i < 4; i++)
02693 {
02694 cout << "Direction: " << i << endl;
02695 cout << unit(compute_deriv(cg.x + 0.1*hess_decomp.get_evectors()[i])) * hess_decomp.get_evectors()[i] << endl;
02696 }
02697
02698 for(int i=0; i < 4; i++)
02699 {
02700 cout << "Direction: " << i << endl;
02701 Vector<4> d = Zeros;
02702 d[i] = 1;
02703 cout << unit(compute_deriv(cg.x + d)) * hess_decomp.get_evectors()[i] << endl;
02704 }
02705 }
02706 }
02707 }
02708
02709
02710
02711 model_2[spot] = cg.x;
02712
02713 graphics.draw_krap(model_2, scale_to_bytes(ave), boundingbox(pixels), -1);
02714 graphics.swap();
02715
02716
02717
02718 model2_spot_intensities[spot] = compute_spot_intensity(pixels, model_2[spot]);
02719 get_spot_pixels(pixels, model_2[spot], model2_spot_pixels[spot]);
02720
02721 cout << "Done optimizing for model selection\n";
02722 }
02723
02724
02725
02726 int keep=original;
02727
02728
02729
02730
02731
02732 Matrix<4> hess;
02733
02734
02735
02736
02737 {
02738
02739 vector<vector<vector<State> > > sample_list;
02740 vector<vector<vector<double> > > sample_intensities;
02741
02742 GibbsSampler sampler(pixel_intensities, model2_spot_intensities, model_2, A, pi, variance, sample_iterations);
02743 for(int i=0; i < h_outer_samples; i++)
02744 {
02745 sampler.next(rng);
02746 sample_list.push_back(sampler.sample());
02747 sample_intensities.push_back(sampler.sample_intensities());
02748 }
02749
02750
02751 for(unsigned int i=0; i < sample_list.size(); i++)
02752 remove_spot(sample_intensities[i], model2_spot_intensities[spot], sample_list[i][spot]);
02753
02754
02755 SampledBackgroundData data(sample_intensities, pixel_intensities, pixels,
02756 intensity_mu, intensity_sigma, blur_mu, blur_sigma,
02757 A, pi, variance);
02758
02759 hess = sampled_background_spot_hessian_ffbs(model_2[spot], data, h_inner_samples, rng);
02760 }
02761
02762
02763 double det = determinant(-hess / (M_PI*2));
02764 SymEigen<4> hess_decomp(-hess);
02765 cout << "Hessien Eigenvalues are: " << hess_decomp.get_evalues() << endl;
02766 const double smallest_evalue = 1e-6;
02767
02768
02769
02770 if(hess_decomp.get_evalues()[0] > smallest_evalue)
02771 {
02772
02773
02774 cout << "Model 2:\n";
02775
02776 double model_2_energy = -NegativeFreeEnergy(data_for_t_mcmc).compute_with_mask(spots_to_Vector(model_2), model2_spot_pixels);
02777 cout << "Energy: " << model_2_energy << endl;
02778
02779
02780 double model_2_occam = -0.5 * log(det);
02781 double model_2_prob = model_2_energy + model_2_occam;
02782
02783 cout << "Occam: " << model_2_occam << endl;
02784 cout << "Prob: " << model_2_prob << endl;
02785
02786
02787
02788
02789
02790
02791
02792 model2_spot_pixels.pop_back();
02793 double model_1_prob = -NegativeFreeEnergy(data_for_t_mcmc).compute_with_mask(spots_to_Vector(model_1), model2_spot_pixels);
02794 cout << "Prob: " << model_1_prob << endl;
02795
02796
02797 if(model_2_prob > model_1_prob)
02798 keep=2;
02799 else
02800 keep=1;
02801
02802 cout << "Models evaluated\n";
02803 }
02804 else
02805 {
02806 cout << "Determinant has bad eigenvalues!\n";
02807 keep = original;
02808 cout << hess_decomp.get_evalues() << endl;
02809 }
02810
02811 if(keep == 2)
02812 {
02813 spots = model_2;
02814 cout << "Keeping model 2\n";
02815
02816 }
02817 else
02818 {
02819 spots = model_1;
02820 cout << "Keeping model 1\n";
02821 }
02822
02823 if(original != keep)
02824 {
02825 cout << "Model changed!\n";
02826
02827 }
02828 }
02829 }
02830 save_spots << "FINAL: " << setprecision(15) << scientific << spots_to_Vector(spots) << endl;
02831 }
02832
02833
02834
02835
02836 void fit_spots_new(const vector<Image<float> >& ims, StateParameters& p, ofstream& save_spots, FitSpotsGraphics& gr)
02837 {
02838 FitSpots fit(ims, gr, p, save_spots);
02839 fit.run();
02840 }