44 int* hsum =
new int[bins*p->
channels()];
47 int* histo = p->
histogram(bins, scale, offset);
50 for ( i=0; i<bins; i++ )
if ( max < histo[i] ) max = histo[i];
52 for ( i=0; i<bins*p->
channels(); i++ ) hsum[i] = histo[i];
53 for ( i=bins-1; i>0; i-- )
55 hsum[c*bins+i-1] += hsum[c*bins+i];
66 cout <<
"Histogram" << endl;
68 cout <<
"Bin\tLevel\tNumber\tSum\tVolume\tInformation" << endl;
71 for ( c=0; c<p->
channels(); c++ ) cout <<
"\tChannel" << c+1;
102 title =
"Cumulative mass";
119 for ( i=0; i<bins; i++ ) {
120 (*plot)[i] = p->
minimum() + i/scale;
121 for ( c=0, j=i, k=bins+i; c<p->
channels(); c++, j+=bins, k+=bins ) (*plot)[k] = histo[j];
126 for ( i=0, k=2*bins; i<bins; i++, k++ ) {
127 (*plot)[k] = hsum[i] * unit_mass;
130 Hi = (-histo[i]*1.0/hsum[0]) * log(histo[i]*1.0/hsum[0]) / log(2.0);
132 cout << i+1 <<
tab << setw(12) << (*plot)[i] <<
tab << histo[i] <<
tab << hsum[i] <<
tab <<
133 p->
sampling().volume()*hsum[i] <<
tab << Hi << endl;
137 cout <<
"Voxels & number in histogram: " << datasize <<
" "
138 << hsum[0] <<
" (" << hsum[0]*100.0/datasize <<
" %)" << endl;
139 cout <<
"Voxel volume: " << p->
sampling().volume() <<
" A/voxel" << endl;
140 cout <<
"Informational entropy and rate: " << H <<
" " << log(1.0*bins)/log(2.0) - H << endl << endl;
143 for ( i=0; i<bins; i++ ) {
144 cout << i+1 <<
tab << (*plot)[i];
145 for ( c=0, j=0; c<p->
channels(); c++, j+=bins ) cout <<
tab << histo[j];
169 int bins(10000), pbins(101);
170 unsigned long i, j, k, m, hsum;
172 double invsize = 100.0/datasize;
173 double scale, offset, fraction, d;
175 int* histo = p->
histogram(bins, scale, offset);
177 double* perc =
new double[pbins];
182 unsigned long ncol(3);
207 cout <<
"%\tBin\tValue" << endl;
208 cout << 0 <<
tab << 0 <<
tab << perc[0] << endl;
210 (*plot)[2*pbins] = perc[0];
211 for ( i=j=1, hsum = histo[0]; i<bins; hsum += histo[i], i++ ) {
212 fraction = hsum*invsize;
213 for ( d = (fraction>j)? 1.0/(fraction - j): 1; fraction > j; j++ ) {
214 perc[j] = p->
minimum() + (i - d*(fraction - j))/scale;
215 cout << j <<
tab << i <<
tab << perc[j] << endl;
217 (*plot)[pbins+j] = i;
218 (*plot)[2*pbins+j] = perc[j];
221 cout << 100 <<
tab << i <<
tab << perc[100] << endl;
223 (*plot)[pbins+100] = i;
224 (*plot)[2*pbins+100] = perc[100];
246 unsigned long i, j, k, m, nh(bins), ncol(n+2);
247 unsigned long datasize = p->
data_size(), thresh((100*datasize)/bins);
248 double v, max(0), avg[n],
std[n], w[n];
251 double scale, offset;
252 int* histo = p->
histogram(bins, scale, offset);
256 for ( j=0; j<n; j++ ) avg[j] = std[j] = w[j] = 0;
257 for ( i=0; i<datasize; i++ ) {
258 avg[sel[i]] += (*p)[i];
259 std[sel[i]] += (*p)[i]*(*p)[i];
262 for ( j=0; j<n; j++ ) {
265 std[j] -= avg[j]*avg[j];
266 if ( std[j] > 0 ) std[j] = sqrt(std[j]);
276 for ( i=nh=0; i<bins; i++ ) {
277 if ( histo[i] < thresh ) {
285 if ( nh < 20 && nh < bins/2 )
286 cerr <<
"Warning: Too few bins selected for fitting! (" << nh <<
"/" << bins <<
")" << endl;
291 for ( i=j=0; i<bins; i++ )
if ( sel[i] ) {
292 simp->
x[j] = p->
minimum() + i/scale;
293 simp->
fx[j++] = histo[i];
294 if ( max < histo[i] ) max = histo[i];
297 for ( i=j=0; j<n; j++, i++ ) {
298 simp->
param[i] = max;
299 simp->
lolimit[i] = datasize/bins;
303 simp->
param[i] = avg[j];
305 simp->
lolimit[i] = avg[j] - 5*std[j];
306 simp->
hilimit[i] = avg[j] + 5*std[j];
315 simp->
param[i] = std[j];
327 float* gauss =
new float[3*n];
328 for ( j=0; j<3*n; j++ ) gauss[j] = simp->
param[j];
333 cout <<
"Gaussian fit over " << nh <<
" bins:" << endl;
334 cout <<
"Amp\tOffset\tSigma\tSIP" << endl;
335 for ( j=0; j<n; j++ )
336 cout << gauss[3*j] <<
tab << gauss[3*j+1] <<
tab << gauss[3*j+2] <<
tab << bins*gauss[3*j]/datasize << endl;
337 cout <<
"Sampling-independent RMSD: " << bins*R/datasize << endl << endl;
354 for ( i=1; i<ncol; i++ ) {
360 for ( i=2; i<ncol; i++ )
372 for ( j=0; j<n; j++ ){
373 txt =
Bstring(j+1,
"Gaussian %d:") +
Bstring(gauss[3*j],
" a=%g") +
382 for ( i=0, j=bins; i<bins; i++, j++ ) {
383 (*plot)[i] = p->
minimum() + i/scale;
384 (*plot)[j] = histo[i];
385 for ( k=0, m=i+2*bins; k<3*n; k+=3, m+=bins ) {
386 x = ((*plot)[i] - gauss[k+1])/gauss[k+2];
387 (*plot)[m] = gauss[k]*exp(-0.5*x*x);
416 unsigned long i, j, m, n, ni, datasize = p->
data_size();
417 double max = 0, sum = 0;
420 double scale, offset;
421 int* histo = p->
histogram(bins, scale, offset);
424 for ( i=0, j=0, n=0, ni=0; i<bins; i++ ) {
425 if ( histo[i] > 0 ) {
433 cout <<
"Positive bins = " << n <<
" Interval size = " << ni*1.0/n << endl;
436 if ( bins < n ) bins = n;
437 else bins = (bins*n)/ni;
439 histo = p->
histogram(bins, scale, offset);
442 cout <<
"Bins reset to " << bins <<
", scale = " << scale <<
", offset = " << offset << endl << endl;
446 for ( i=0; i<bins; i++ ) simp->
x[i] = p->
minimum() + i/scale;
447 for ( i=1; i<bins-1; i++ ) {
448 if ( histo[i] < 1 ) simp->
fx[i] = log(0.5);
449 else simp->
fx[i] = log(1.0*histo[i]);
451 if ( max < simp->fx[i] ) max = simp->
fx[i];
453 simp->
fx[0] = simp->
fx[1];
454 simp->
fx[bins-1] = log(0.5);
456 simp->
param[0] = log(datasize*1.0);
457 simp->
lolimit[0] = log(datasize/2.0);
458 simp->
hilimit[0] = log(datasize+1.0);
465 if ( simp->
param[2] < 0.001 ) simp->
param[2] = 0.001;
473 float* poisson =
new float[3];
474 poisson[0] = exp(simp->
param[0]);
475 poisson[1] = simp->
param[1];
476 poisson[2] = simp->
param[2];
481 cout <<
"Poisson function fit over " << bins <<
" bins:" << endl;
482 cout <<
"Lambda: " << poisson[2] << endl;
484 cout <<
"Included voxels: " << poisson[0] <<
" (" << 100*poisson[0]/datasize <<
"%)" << endl;
485 cout <<
"Sampling-independent RMSD: " << bins*R/datasize << endl << endl;
493 unsigned long ncol(3);
522 for ( i=0, j=bins, m=2*bins; i<bins; i++, j++, m++ ) {
524 (*plot)[j] = histo[i];
525 (*plot)[m] = poisson[0]*pow(poisson[2],(
float)i)*exp(-poisson[2])/
factorial(i);
526 if ( !isfinite((*plot)[m]) ) (*plot)[m] = 0;
540 for ( i=0; i<simp->
npoint; i++ ) {
541 for ( j=k=0, df=0; j<simp->
constant[0]; j++, k+=3 ) {
542 x = (simp->
x[i] - simp->
param[k+1])/simp->
param[k+2];
543 df += simp->
param[k]*exp(-0.5*x*x);
558 float* df =
new float[simp->
npoint];
562 for ( i=0, n=0; i<simp->
npoint; i++ ) {
563 if ( simp->
fx[i] >= 0 ) {
564 x = simp->
x[i] + simp->
param[1];
565 df[i] = fabs(simp->
fx[i] - simp->
param[0] - x*log(simp->
param[2]) +
566 simp->
param[2] + lgamma(x+1.0L));
567 if ( !isfinite(df[i]) ) {
568 error_show(
"Error in img_fit_poisson_to_his_R", __FILE__, __LINE__);
569 cerr <<
"i=" << i <<
"\tfx[i]=" << simp->
fx[i] <<
"\tamp=" <<
571 <<
"\tlambda=" << simp->
param[2] << endl;
578 qsort((
void *) df, simp->
npoint,
sizeof(
float),
585 for ( i=0, R=0; i<j; i++ ) if ( df[i] > -1 ) {
590 if ( n ) R = sqrt(R/n);
void change_type(char letter)
Get the data type indicated by a single letter code.
Header file for utility functions for use in qsort calls.
Bsimplex * init_simplex(int nvar, int nparam, int nconstant, int npoint)
Initializes a Bsimplex structure.
Header file for image utilities.
Header file for general utilities.
double img_fit_poisson_to_his_R(Bsimplex *simp)
int kill_simplex(Bsimplex *simp)
Frees all memory associated with a Bsimplex structure.
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
double average(size_t cc, double xf, double yf, double zf, size_t nn, double iscale)
Averages in the xy plane when scaling is less than 1.
General image parameter class.
double factorial(int n)
Calculates the factorial of n.
Bplot * img_percentiles(Bimage *p)
Calculates the percentiles from the histogram of an image.
double img_fit_gauss_to_his_R(Bsimplex *simp)
Bplot * img_histogram(Bimage *p, long bins)
Calculates the histogram of an image.
double standard_deviation()
void add_text(Bstring &s)
unsigned char * data_pointer()
int QsortSmallToLargeFloat(const void *x, const void *y)
Utility function for sorting floating point values in qsort.
Nelder and Mead downhill simplex method for generalized parameter fitting.
int * k_means(long n, float *data, int k)
Generate clusters using a K-means algorithm.
Bplot * img_fit_poisson_to_histogram(Bimage *p, long bins)
Fits a poisson function to a histogram of an image.
Header file for clustering functions.
double simplex(Bsimplex *simp, int maxcycles, double tolerance, double(funk)(Bsimplex *))
Header file for 2D and 3D image I/O.
Bplot * img_fit_gauss_to_histogram(Bimage *p, long bins, long n)
Fits a gaussian function to a histogram of an image.
int * histogram(size_t bins, double &scale, double &offset)
Calculates the histogram of an image.
ostream & tab(ostream &out)
Header file for general utilities.