ThreeB 1.1
|
Classes | |
struct | ConjugateGradientOnly< Size, Precision > |
Class for performing optimization with Conjugate Gradient, where only the derivatives are available. More... | |
class | NullGraphics |
Graphics class which does absoloutely nothing. More... | |
class | DataForMCMC |
Closure hoding the data required do use GibbsSampler2 See FitSpots for naming of variables. More... | |
struct | SampledBackgroundData |
Closure holding image data generated using samples drawn from the model. More... | |
struct | SpotNegProbabilityDiffWithSampledBackground |
Compute the derivative of the negative log probability with respect to the parameters of one spot, given some samples of the other spots. More... | |
class | FreeEnergyHessian |
Class for computing the Hessian of the negative free energy. More... | |
class | FitSpots |
Mega class which actually does the meat of the spot fitting. More... | |
class | FitSpotsGraphics |
Graphics class for FittingSpots. More... | |
class | UserInterfaceCallback |
Callback class used by FitSpots to provide enough hooks for a user interface. More... | |
class | SampledMultispot::GibbsSampler |
Draw samples from the spot states given the spots positions and some data. More... | |
class | SampledMultispot::GibbsSampler2 |
Gibbs sampling class which masks spots to reduce computation. More... | |
Functions | |
auto_ptr< UserInterfaceCallback > | null_ui () |
auto_ptr< FitSpotsGraphics > | null_graphics () |
void | get_spot_pixels (const vector< ImageRef > &pixels, const Vector< 4 > &spot, vector< int > &out) |
StateParameters | parse_log_file (istream &in) |
double | brightness_motion_limit (double mu, double sigma, bool not_one) |
void | fit_spots_new (const vector< Image< float > > &ims, StateParameters &p, ofstream &save_spots, FitSpotsGraphics &gr) |
void | fit_spots_new (const vector< Image< float > > &ims, StateParameters &p, ofstream &save_spots, FitSpotsGraphics &gr, UserInterfaceCallback &ui) |
template<class B > | |
double | spot_shape_s (const TooN::Vector< 2 > &x, const TooN::Vector< 4, double, B > &phi) |
template<class B > | |
std::pair< double, TooN::Vector< 4 > > | spot_shape_diff_position (const TooN::Vector< 2 > &x, const TooN::Vector< 4, double, B > &phi) |
template<class B > | |
std::tr1::tuple< double, TooN::Vector < 4 >, TooN::Matrix< 4 > > | spot_shape_hess_position (const TooN::Vector< 2 > &x, const TooN::Vector< 4, double, B > &phi) |
template<class B > | |
std::tr1::tuple< double, TooN::Vector < 2 >, TooN::Matrix< 2 > > | spot_shape_hess (const TooN::Vector< 2 > &x, const TooN::Vector< 4, double, B > &phi) |
template<class B > | |
std::pair< double, TooN::Vector< 2 > > | spot_shape_diff (const TooN::Vector< 2 > &x, const TooN::Vector< 4, double, B > &phi) |
template<class B > | |
double | spot_shape (const TooN::Vector< 2 > &x, const TooN::Vector< 4, double, B > &phi) |
template<class Base > | |
std::tr1::tuple< double, TooN::Vector < 2 >, TooN::Matrix< 2 > > | log_probability_spot_hess (const CVD::SubImage< float > &im, double variance, const TooN::Vector< 4, double, Base > &spot_parameters) |
template<class Base > | |
std::pair< double, TooN::Vector< 2 > > | log_probability_spot_diff (const CVD::SubImage< float > &im, double variance, const TooN::Vector< 4, double, Base > &spot_parameters) |
template<class Base > | |
double | log_probability_spot (const CVD::SubImage< float > &im, double variance, const TooN::Vector< 4, double, Base > &spot_parameters) |
double | log_normal_std (double mu, double sigma) |
double | log_normal_mode (double mu, double sigma) |
double | log_log_normal (double x, double mu, double sigma) |
double | diff_log_log_normal (double x, double mu, double sigma) |
double | hess_log_log_normal (double x, double mu, double sigma) |
auto_ptr<UserInterfaceCallback> null_ui | ( | ) |
Factory function to generate an instance of NullGraphics.
Definition at line 62 of file multispot5.cc.
Referenced by fit_spots_new().
{ return auto_ptr<UserInterfaceCallback>(new NullUICallback); }
auto_ptr<FitSpotsGraphics> null_graphics | ( | ) |
Factory function to generate an instance of NullGraphics.
Definition at line 89 of file multispot5.cc.
Referenced by Java_ThreeBRunner_call(), and mmain().
{ return auto_ptr<FitSpotsGraphics>(new NullGraphics); }
void get_spot_pixels | ( | const vector< ImageRef > & | pixels, |
const Vector< 4 > & | spot, | ||
vector< int > & | out | ||
) |
Which pixels belong to a given spot? Find the indices of those pixels.
Definition at line 886 of file multispot5.cc.
Referenced by FitSpots::optimize_each_spot_in_turn_for_several_passes(), and FitSpots::try_modifying_model().
{ //Go out to three sigma vector<ImageRef> pix = getDisc(spot[1]*6 + 1); out.resize(0); ImageRef offset = ir_rounded(spot.slice<2,2>()); for(unsigned int j=0; j < pix.size(); j++) { int pos = lower_bound(pixels.begin(), pixels.end(), pix[j] + offset) - pixels.begin(); if(pos != (int)pixels.size() && pixels[pos] == pix[j] + offset) out.push_back(pos); } if(out.size() == 0) { cout << "********************************\n"; cout << "********************************\n"; cout << "********************************\n"; cout << "********************************\n"; cout << "********************************\n"; cout << "Oe noes!11one\n"; cout << pix.size() << endl; } }
StateParameters parse_log_file | ( | istream & | in | ) |
Parser for multispot 5 log files.
Log files are mostly line oriented and contain various records
The main records are:
Iteraton: #ITNUM MAIN: <list of spot parameters>
Pass: #PASSNUM MT19937 <random number generator state> PASS#PASSNUM: <list of spot parameters> ENDCHECKPOINT
Note that MAIN is redundant since it will be the same as the following PASS 1 (or the first pass computed if restoring from a checkpoint).
Data should only be considered valid after ENDCHECKPOINT has been read
Iteration is written once per iteration, not once per pass. (FIXME)
Which moron invented this file format???
Note that the file format hasn't beren fixed, so that the output can easily be compared to the output of the historic version which is known to be good.
in | Stream to parse file from |
Definition at line 986 of file multispot5.cc.
References StateParameters::iteration, StateParameters::pass, StateParameters::pixels, StateParameters::rng, split(), StateParameters::spots, and xtoa().
Referenced by mmain().
{ //A line read from the file string line; //State lines known to be OK string rngline, passline, iterationline; bool state_ok=0; //State lines read in, with flags of goodness string new_rngline, new_passline, new_iterationline; bool new_rngline_ok=0, new_passline_ok=0, new_iterationline_ok=0; unsigned int lineno=0; bool doing_gvars = 0; vector<ImageRef> pixels; while(!in.eof()) { getline(in, line); if(in.fail()) break; lineno++; if(line == "ENDGVARLIST") { if(!doing_gvars) throw LogFileParseError("Spurious end of GVars"); doing_gvars = 0; } else if(doing_gvars) { GUI.ParseLine(line); } else if(line == "BEGINGVARLIST") { doing_gvars = 1; } if(line.substr(0, 11) == "Iteration: ") { new_iterationline = line; new_iterationline_ok = true; } else if(line.substr(0, 4) == "PASS") { new_passline = line; if(new_passline_ok) throw LogFileParseError("Duplicate PASS on line " + xtoa(lineno)); new_passline_ok = true; } else if(line.substr(0, 8) == "MT19937 ") { new_rngline = line; if(new_rngline_ok) throw LogFileParseError("Duplicate MT19937 on line " + xtoa(lineno)); new_rngline_ok = true; } else if(line == "ENDCHECKPOINT") { if(new_passline_ok && new_rngline_ok && new_iterationline_ok) { iterationline = new_iterationline; rngline = new_rngline; passline = new_passline; } else throw LogFileParseError("Reached checkpoint with missing data: " "it=" + xtoa(new_iterationline_ok) + " pa=" + xtoa(new_passline_ok) + " rg=" + xtoa(new_rngline_ok) + " on line " + xtoa(lineno)); //Don't reset iteration since it only appears once for each entire //set of passes. new_rngline_ok = 0; new_passline_ok = 0; state_ok = true; } else if(line.substr(0, 7) == "PIXELS ") { vector<string> l = split(line); if( (l.size() - 1)%2 == 0) { int n = (l.size()-1)/2; pixels.resize(n); for(int i=0; i < n; i++) { pixels[i].x = atox<int>(l[i*2+1+0], "pixels"); pixels[i].y = atox<int>(l[i*2+1+1], "pixels"); } } else throw LogFileParseError("Bad PIXELS line"); } } if(!state_ok) throw LogFileParseError("No state found"); if(pixels.size() == 0) throw LogFileParseError("No pixels, or pixels is empty"); //Now parse the lines StateParameters p; vector<string> l; //Parse the iterations l = split(iterationline); p.iteration = atox<int>(l[1], "iteration"); //Parse the random number generator p.rng =shared_ptr<MT19937>(new MT19937); { istringstream rng_s(rngline); try{ p.rng->read(rng_s); } catch(MT19937::ParseError p) { throw LogFileParseError("Error parsing MT19937"); } } //Parse PASS and the listing of spots l = split(passline); if( (l.size() - 1 ) % 4 == 0) { p.pass = atox<int>(l[0].substr(4), "pass"); for(unsigned int i=0; i < (l.size()-1)/4; i++) { cout << l[i*4+1+0] << endl; cout << l[i*4+1+1] << endl; cout << l[i*4+1+2] << endl; cout << l[i*4+1+3] << endl; p.spots.push_back(makeVector( atox<double>(l[i*4+1+0], "spot"), atox<double>(l[i*4+1+1], "spot"), atox<double>(l[i*4+1+2], "spot"), atox<double>(l[i*4+1+3], "spot"))); } } else throw LogFileParseError("Wrong number of elements in PASS line"); //Set up the pixels (oh god the pixels) p.pixels = pixels; return p; }
double brightness_motion_limit | ( | double | mu, |
double | sigma, | ||
bool | not_one | ||
) |
How far should steps in brightness be limited to?
Definition at line 1334 of file multispot5.cc.
References log_normal_std().
{ if(not_one) return log_normal_std(mu, sigma); else return 1; }
void fit_spots_new | ( | const vector< Image< float > > & | ims, |
StateParameters & | p, | ||
ofstream & | save_spots, | ||
FitSpotsGraphics & | gr | ||
) |
Wrapper function for using FitSpots.
Definition at line 2081 of file multispot5.cc.
References null_ui(), and FitSpots::run().
Referenced by mmain().
void fit_spots_new | ( | const vector< Image< float > > & | ims, |
StateParameters & | p, | ||
ofstream & | save_spots, | ||
FitSpotsGraphics & | gr, | ||
UserInterfaceCallback & | ui | ||
) |
Wrapper function for using FitSpots.
Definition at line 2090 of file multispot5.cc.
References FitSpots::run().
{ try{ FitSpots fit(ims, gr, ui, p, save_spots); fit.run(); } catch(UserInterfaceCallback::UserIssuedStop) { } }
double spot_shape_s | ( | const TooN::Vector< 2 > & | x, |
const TooN::Vector< 4, double, B > & | phi | ||
) |
See spot_shape()
x | |
phi |
Definition at line 17 of file storm.h.
Referenced by spot_shape(), spot_shape_diff(), spot_shape_diff_position(), spot_shape_hess(), and spot_shape_hess_position().
{
return -norm_sq(x - phi.template slice<2,2>()) / (2*phi[1]*phi[1]);
}
std::pair<double, TooN::Vector<4> > spot_shape_diff_position | ( | const TooN::Vector< 2 > & | x, |
const TooN::Vector< 4, double, B > & | phi | ||
) |
Compute the spot shape and its derivative with respect to posision.
See also spot_shape()
x | |
phi |
Definition at line 27 of file storm.h.
References spot_shape_s(), and sq().
{ using namespace TooN; double s = spot_shape_s(x, phi); double r_2_pi = sqrt(2*M_PI); double prob = exp(s) * phi[0]/(phi[1]*r_2_pi); Vector<4> deriv = (exp(s) / (phi[1]*r_2_pi)) * makeVector(1, -phi[0] * (1 + 2*s)/phi[1], (x[0] - phi[2])*(phi[0]/sq(phi[1])), (x[1] - phi[3])*(phi[0]/sq(phi[1]))); return std::make_pair(prob, deriv); }
std::tr1::tuple<double, TooN::Vector<4>, TooN::Matrix<4> > spot_shape_hess_position | ( | const TooN::Vector< 2 > & | x, |
const TooN::Vector< 4, double, B > & | phi | ||
) |
Compute the spot shape and its Hessian with respect to posision.
See also spot_shape()
x | |
phi |
Definition at line 49 of file storm.h.
References spot_shape_s(), and sq().
{ using namespace TooN; using namespace std::tr1; double s = spot_shape_s(x, phi); double r_2_pi = sqrt(2*M_PI); double es = exp(s); double prob = es * phi[0]/(phi[1]*r_2_pi); Vector<4> deriv = (es / (phi[1]*r_2_pi)) * makeVector(1, -phi[0] * (1 + 2*s)/phi[1], (x[0] - phi[2])*(phi[0]/sq(phi[1])), (x[1] - phi[3])*(phi[0]/sq(phi[1]))); Matrix<4> hess; hess[0][0] = 0; hess[0][1] = -es*(1+2*s) / (phi[1] * phi[1] * r_2_pi); hess[1][0] = hess[0][1]; hess[0][2] = es * (x[0] - phi[2]) / (pow(phi[1], 3)*r_2_pi); hess[2][0] = es * (x[0] - phi[2]) / (pow(phi[1], 3)*r_2_pi); hess[0][3] = es * (x[1] - phi[3]) / (pow(phi[1], 3)*r_2_pi); hess[3][0] = es * (x[1] - phi[3]) / (pow(phi[1], 3)*r_2_pi); hess[1][1] = 2*phi[0]*es*(1 + 5*s + 2*s*s) / ( pow(phi[1], 3) * r_2_pi); hess[1][2] = -phi[0] * es * (3 + 2*s) * (x[0] - phi[2]) / (pow(phi[1], 4) * r_2_pi); hess[1][3] = -phi[0] * es * (3 + 2*s) * (x[1] - phi[3]) / (pow(phi[1], 4) * r_2_pi); hess[2][1] = hess[1][2]; hess[3][1] = hess[1][3]; hess[2][2] = phi[0] * es * (sq(x[0] - phi[2]) - sq(phi[1])) / (r_2_pi * pow(phi[1], 5)); hess[3][3] = phi[0] * es * (sq(x[1] - phi[3]) - sq(phi[1])) / (r_2_pi * pow(phi[1], 5)); hess[2][3] = phi[0] * es * (x[0] - phi[2])*(x[1] - phi[3]) / (r_2_pi * pow(phi[1], 5)); hess[3][2] = hess[2][3]; return make_tuple(prob, deriv, hess); }
std::tr1::tuple<double, TooN::Vector<2>, TooN::Matrix<2> > spot_shape_hess | ( | const TooN::Vector< 2 > & | x, |
const TooN::Vector< 4, double, B > & | phi | ||
) |
Value of the spot, given the parameters and input location.
The spot is described by the following formula:
where
This describes a generic blobby spot function of a variable size. The light output can be tuned by varying , and the level of blur can be changed independently by varying . The derivative is:
And the hessian is:
x | |
phi |
Definition at line 124 of file storm.h.
References spot_shape_s().
Referenced by log_probability_spot_hess().
{ double s = spot_shape_s(x, phi); double r_2_pi = sqrt(2*M_PI); double prob = exp(s) * phi[0]/(phi[1]*r_2_pi); TooN::Vector<2> deriv = (exp(s) / (phi[1]*r_2_pi)) * TooN::makeVector(1, -phi[0] * (1 + 2*s)/phi[1]); TooN::Matrix<2> hess; hess[0][0] = 0; hess[0][1] = -exp(s)*(1+2*s) / (phi[1] * phi[1] * r_2_pi); hess[1][0] = hess[0][1]; hess[1][1] = 2*phi[0]*exp(s)*(1 + 5*s + 2*s*s) / ( pow(phi[1], 3) * r_2_pi); return std::tr1::make_tuple(prob, deriv, hess); }
std::pair<double, TooN::Vector<2> > spot_shape_diff | ( | const TooN::Vector< 2 > & | x, |
const TooN::Vector< 4, double, B > & | phi | ||
) |
x | |
phi |
Definition at line 146 of file storm.h.
References spot_shape_s().
Referenced by log_probability_spot_diff().
{ double s = spot_shape_s(x, phi); double r_2_pi = sqrt(2*M_PI); double prob = exp(s) * phi[0]/(phi[1]*r_2_pi); TooN::Vector<2> deriv = (exp(s) / (phi[1]*r_2_pi)) * TooN::makeVector(1, -phi[0] * (1 + 2*s)/phi[1]); return std::make_pair(prob, deriv); }
double spot_shape | ( | const TooN::Vector< 2 > & | x, |
const TooN::Vector< 4, double, B > & | phi | ||
) |
x | |
phi |
Definition at line 162 of file storm.h.
References spot_shape_s().
Referenced by log_probability_spot().
{ double s = spot_shape_s(x, phi); double r_2_pi = sqrt(2*M_PI); // FIXME FIXME FIXME and don't forget to fix the HESSIAN AND DERIVATIVE // Should be: 1/(2 pi s^2) for two dimensions // vvvvvvvvvvvvv http://lol.i.trollyou.com/ double prob = exp(s) * phi[0]/(phi[1]*r_2_pi); return prob; }
std::tr1::tuple<double, TooN::Vector<2>, TooN::Matrix<2> > log_probability_spot_hess | ( | const CVD::SubImage< float > & | im, |
double | variance, | ||
const TooN::Vector< 4, double, Base > & | spot_parameters | ||
) |
Find the log probability of an image patch, assuming zero base-line mean and the given variance.
This function makes use of the spot shape. It is assumed that the centre pixel of the image is at 0,0. Since the noise is Gaussian:
where I is the image, and N is the number of pixels. See also log_probability_no_spot and (spot_shape). The derivatives are:
im | Image |
variance | |
spot_parameters |
Definition at line 217 of file storm.h.
References spot_shape_hess(), and sq().
{ using namespace TooN; using namespace std::tr1; //-1 because if the image is 3x3, ie 0,1,2 then 1,1 is the centre. //If it is 2x2, ie 0,1 then .5,.5 is the centre Vector<2> centre = makeVector((im.size().x-1) / 2.0, (im.size().y-1) / 2.0); double logprob_part=0; Vector<2> diff = Zeros; Matrix<2> hess = Zeros; for(int y=0; y < im.size().y; y++) for(int x=0; x < im.size().x; x++) { Vector<2> d = TooN::makeVector(x, y) - centre; double mu; Vector<2> diff_mu; Matrix<2> hess_mu; tie(mu, diff_mu, hess_mu) = spot_shape_hess(d, spot_parameters); double e = im[y][x] - mu; logprob_part += -sq(e); diff += diff_mu * e; hess += e * hess_mu - diff_mu.as_col() * diff_mu.as_row(); } return make_tuple( logprob_part / (2*variance) - im.size().area() * log(2*M_PI*variance)/2, diff / variance, hess / variance); }
std::pair<double, TooN::Vector<2> > log_probability_spot_diff | ( | const CVD::SubImage< float > & | im, |
double | variance, | ||
const TooN::Vector< 4, double, Base > & | spot_parameters | ||
) |
See log_probability_spot_hess.
im | Image |
variance | |
spot_parameters |
Definition at line 257 of file storm.h.
References spot_shape_diff(), and sq().
{ using namespace TooN; using namespace std::tr1; using namespace std; //-1 because if the image is 3x3, ie 0,1,2 then 1,1 is the centre. //If it is 2x2, ie 0,1 then .5,.5 is the centre Vector<2> centre = makeVector((im.size().x-1) / 2.0, (im.size().y-1) / 2.0); double logprob_part=0; Vector<2> diff = Zeros; for(int y=0; y < im.size().y; y++) for(int x=0; x < im.size().x; x++) { Vector<2> d = makeVector(x, y) - centre; double mu; Vector<2> diff_mu; tie(mu, diff_mu) = spot_shape_diff(d, spot_parameters); double e = im[y][x] - mu; logprob_part += -sq(e); diff += diff_mu * e; } return make_pair(logprob_part / (2*variance) - im.size().area() * log(2*M_PI*variance)/2, diff / variance); }
double log_probability_spot | ( | const CVD::SubImage< float > & | im, |
double | variance, | ||
const TooN::Vector< 4, double, Base > & | spot_parameters | ||
) |
See log_probability_spot_hess.
im | Image |
variance | |
spot_parameters |
Definition at line 292 of file storm.h.
References spot_shape(), and sq().
{ //-1 because if the image is 3x3, ie 0,1,2 then 1,1 is the centre. //If it is 2x2, ie 0,1 then .5,.5 is the centre TooN::Vector<2> centre = TooN::makeVector((im.size().x-1) / 2.0, (im.size().y-1) / 2.0); double logprob_part=0; for(int y=0; y < im.size().y; y++) for(int x=0; x < im.size().x; x++) { TooN::Vector<2> d = TooN::makeVector(x, y) - centre; double mu = spot_shape(d, spot_parameters); double e = im[y][x] - mu; logprob_part += -sq(e); } return logprob_part / (2*variance) - im.size().area() * log(2*M_PI*variance)/2; }
double log_normal_std | ( | double | mu, |
double | sigma | ||
) | [inline] |
double log_normal_mode | ( | double | mu, |
double | sigma | ||
) | [inline] |
Compute the mode of a log-normal distribution.
See log_normal().
sigma | |
mu |
Definition at line 340 of file storm.h.
Referenced by generate_state_parameters_ye_olde(), and FitSpots::try_modifying_model().
{
return exp(mu - sigma * sigma);
}
double log_log_normal | ( | double | x, |
double | mu, | ||
double | sigma | ||
) | [inline] |
Log-normal distribution.
This is given by:
x | x |
mu | |
sigma |
Definition at line 355 of file storm.h.
Referenced by NegativeFreeEnergy::compute_with_mask(), and NegativeFreeEnergy::operator()().
double diff_log_log_normal | ( | double | x, |
double | mu, | ||
double | sigma | ||
) | [inline] |
Derivative of the log of the log-normal distribution:
.
x | x |
mu | |
sigma |
Definition at line 369 of file storm.h.
Referenced by FreeEnergyHessian::hessian(), SpotNegProbabilityDiffWithSampledBackground::operator()(), sampled_background_spot_hessian2(), and sampled_background_spot_hessian_ffbs().
double hess_log_log_normal | ( | double | x, |
double | mu, | ||
double | sigma | ||
) | [inline] |
Second derivative of the log of the log-normal distribution:
.
x | x |
mu | |
sigma |
Definition at line 384 of file storm.h.
Referenced by FreeEnergyHessian::hessian(), sampled_background_spot_hessian2(), sampled_background_spot_hessian_FAKE(), and sampled_background_spot_hessian_ffbs().