37 vector<long> histo(bins*c, 0);
44 if ( datatype <
Float ) {
45 scale = 1/(ceil((max - min + 1)/bins));
48 scale = (bins - 1)*1.0/(max - min);
49 offset = 0.5 - scale*min;
53 cout <<
"DEBUG Bimage::histogram: range=" << min <<
"-"
54 << max <<
" bins=" << bins <<
" scale=" << scale
55 <<
" offset=" << offset << endl;
58 for ( cc=0; cc<c; cc++, i++ ) {
59 j = (long) (scale*(*
this)[i] + offset);
60 if ( j >= 0 && j < bins ) histo[cc*bins+j]++;
81 long i, j, k, cc, npage, ncol;
83 double bin_half = (max - min)/(4*bins);
85 vector<long> hsum(bins*c, 0);
88 vector<long> histo =
histogram(bins, scale, offset);
91 for ( i=0; i<bins; i++ )
if ( hmax < histo[i] ) hmax = histo[i];
93 for ( i=0; i<bins*c; i++ ) hsum[i] = histo[i];
94 for ( i=bins-1; i>0; i-- )
95 for ( cc=0; cc<c; cc++ )
96 hsum[cc*bins+i-1] += hsum[cc*bins+i];
107 cout <<
"Histogram" << endl;
109 cout <<
"Bin\tLevel\tNumber\tSum\tVolume\tInformation" << endl;
111 cout <<
"Bin\tLevel";
112 for ( cc=0; cc<c; cc++ ) cout <<
"\tChannel" << cc+1;
126 for ( i=1; i<=c; i++ ) {
143 title =
"Cumulative mass";
160 for ( i=0; i<bins; i++ ) {
161 (*plot)[i] = min + i/scale;
162 for ( cc=0, j=i, k=bins+i; cc<c; cc++, j+=bins, k+=bins ) (*
plot)[k] = histo[j];
167 for ( i=0, k=2*bins; i<bins; i++, k++ ) {
168 (*plot)[k] = hsum[i] * unit_mass;
171 Hi = (-histo[i]*1.0/hsum[0]) * log(histo[i]*1.0/hsum[0]) / log(2.0);
173 cout << i+1 <<
tab << setw(12) << (*plot)[i] <<
tab << histo[i] <<
tab << hsum[i] <<
tab <<
178 cout <<
"Voxels & number in histogram: " << datasize <<
" "
179 << hsum[0] <<
" (" << hsum[0]*100.0/datasize <<
" %)" << endl;
180 cout <<
"Voxel volume: " <<
sampling(0).volume() <<
" A/voxel" << endl;
181 cout <<
"Informational entropy and rate: " << H <<
" " << log(1.0*bins)/log(2.0) - H << endl << endl;
184 for ( i=0; i<bins; i++ ) {
185 cout << i+1 <<
tab << (*plot)[i];
186 for ( cc=0, j=0; cc<c; cc++, j+=bins ) cout <<
tab << histo[j];
209 long i, j, k, bins(1000), imax(0), imax2(0);
210 double scale, offset, hmax(0), hmax2(0);
212 if ( datatype <
Float ) bins = max - min + 1;
214 vector<long> h =
histogram(bins, scale, offset);
217 for ( i=0, imax=0; i<bins; ++i )
if ( hmax < h[i] ) {
224 cerr <<
"Warning: First bin in histogram is already the highest!" << endl;
229 for ( i=imax; i && h[i] >= hmax2; --i ) ;
232 for ( ; i && h[i] < hmax2; --i ) ;
235 for ( ; i && h[i] > hmax/2; --i )
if ( hmax2 < h[i] ) {
243 cerr <<
"Warning: First bin in histogram is already the highest!" << endl;
244 cerr <<
"scale = " << scale <<
" offset = " << offset << endl;
245 cerr <<
"imax = " << imax <<
" imax2 = " << imax2 << endl;
249 long np(bins/imax+1), ninc(0), thr(datasize/1e4);
250 double stot(0), inc(0), d;
251 double amp[np], s[np], sv[np], sv2[np];
253 for ( j=0; j<np; j++ ) s[j] = sv[j] = sv2[j] = amp[j] = 0;
255 for ( i=0, j=0; i<bins && j<np; i++ ) {
261 if ( amp[j] < h[i] ) amp[j] = h[i];
265 for ( j=0; j<np; j++ ) stot += s[j];
268 cout <<
"Peak\t%\tAmp\tAvg\tStDev" << endl;
269 for ( j=k=0; j<np; j++ )
if ( s[j] ) {
271 sv2[j] = sv2[j]/s[j] - sv[j]*sv[j];
272 if ( j && amp[j] > thr ) {
277 cout << setprecision(4) << j <<
tab << 100*s[j]/stot <<
tab <<
278 amp[j] <<
tab << sv[j] <<
tab << sqrt(sv2[j]) << endl;
283 cout <<
"Counts scale: " << inc <<
" (" << ninc <<
")" << endl;
285 if ( datatype <
Float ) scale = inc;
287 for ( i=0; i<datasize; i++ )
set(i, floor((*
this)[i]/scale + 0.5));
310 for ( j=1, i=2; j<np; j++, i++ ) {
318 for ( i=0; i<bins; i++ ) {
320 (*plot)[i+bins] = h[i];
321 for ( j=1, k=2*bins; j<np; j++, k+=bins )
324 (*plot)[i+k] = amp[j] * exp(-0.5*d*d/sv2[j]);
346 long bins(10000), pbins(101);
348 double invsize(100.0/datasize);
349 double scale, offset, fraction, d;
351 vector<long> histo =
histogram(bins, scale, offset);
353 vector<double> perc(pbins);
383 cout <<
"%\tBin\tValue" << endl;
384 cout << 0 <<
tab << 0 <<
tab << perc[0] << endl;
386 (*plot)[2*pbins] = perc[0];
387 for ( i=j=1, hsum = histo[0]; i<bins; hsum += histo[i], i++ ) {
388 fraction = hsum*invsize;
389 for ( d = (fraction>j)? 1.0/(fraction - j): 1; fraction > j; j++ ) {
390 perc[j] = min + (i - d*(fraction - j))/scale;
392 cout << j <<
tab << i <<
tab << perc[j] << endl;
394 (*plot)[pbins+j] = i;
395 (*plot)[2*pbins+j] = perc[j];
399 cout << 100 <<
tab << i <<
tab << perc[100] << endl;
401 (*plot)[pbins+100] = i;
402 (*plot)[2*pbins+100] = perc[100];
426 long i, j, bins(256);
427 if ( (max - min)/std < 25 ) bins = 10*(max - min)/std;
428 if ( datatype <=
SCharacter ) bins = (long) (max - min + 1.1);
430 long ihi, imin, nmin, imax(bins/2), nmax(0), isig(0);
431 double g, sigma(8.0/(bins*bins));
432 double scale, offset;
434 vector<long> hist =
histogram(bins, scale, offset);
437 cout <<
"Estimating extreme thresholds from the histogram:" << endl;
438 cout <<
"Bins: " << bins << endl;
441 imax = (avg - min)*scale;
444 for ( i=imax/2, j=(bins-imax)/2; i<j; i++ ) {
445 if ( nmax < hist[i] ) {
451 while ( imax+isig < bins - 1 && imax-isig > 0 &&
452 hist[imax] < 2*hist[imax+isig] && hist[imax] < 2*hist[imax-isig] )
456 cout <<
"DEBUG Bimage::histogram_thresholds: Gaussian parameters:" << endl;
457 cout <<
"DEBUG Bimage::histogram_thresholds: max=" << nmax <<
" imax=" << imax <<
" sigma=" << isig << endl;
460 if ( isig > 0 ) sigma = -0.5/(isig*isig);
462 for ( i=0; i<imax/2; i++ ) {
463 g = nmax*exp(sigma*(imax - i)*(imax - i));
464 if ( hist[i] > 20*g ) ihi = i;
468 cout <<
"DEBUG Bimage::histogram_thresholds: highest peak in first quarter = " << ihi << endl;
472 for ( i=ihi; i<imax/2; i++ ) {
473 if ( nmin > hist[i] ) {
480 for ( i=bins-1, j=(bins-imax)/2; i>j; i-- ) {
481 g = nmax*exp(sigma*(imax - i)*(imax - i));
482 if ( hist[i] > 20*g ) ihi = i;
486 cout <<
"DEBUG Bimage::histogram_thresholds: highest peak in last quarter = " << ihi << endl;
490 for ( i=(bins-imax)/2; i<ihi; i++ ) {
491 if ( nmin >= hist[i] ) {
499 if ( imin >= imax ) {
500 cerr <<
"Error in Bimage::histogram_thresholds: extremes not found!" << endl;
504 tmin = imin/scale + min;
505 tmax = imax/scale + min;
508 cout <<
"DEBUG Bimage::histogram_thresholds: imin=" << imin <<
" imax=" << imax <<
" tmin=" << tmin <<
" tmax=" << tmax << endl;
511 cout <<
"Thresholds: " << tmin <<
tab << tmax << endl << endl;
525 double scale, offset;
526 vector<long> h =
histogram(bins, scale, offset);
528 long i, total(x*y*z);
529 double sum(0), sumB(0), wB(0), wF(0), mB, mF, mx(0), bt(0), t1(0), t2(0);
531 for ( i = 1; i < bins; ++i )
534 for ( i = 0; i < bins; ++i ) {
543 mF = (
sum - sumB) / wF;
544 bt = wB * wF * (mB - mF) * (mB - mF);
556 return ( t1 + t2 ) / (2 * scale) + min;
566 for ( i=0; i<simp.
points(); i++ ) {
567 for ( j=k=0, df=0; j<simp.
constant(0); j++, k+=3 ) {
595 long thresh((100*datasize)/bins);
596 double hmax(0), havg[ngauss], hstd[ngauss], w[ngauss];
598 double scale, offset;
599 vector<long> histo =
histogram(bins, scale, offset);
603 for ( j=0; j<ngauss; j++ ) havg[j] = hstd[j] = w[j] = 0;
604 for ( i=0; i<datasize; i++ ) {
605 havg[sel[i]] += (*this)[i];
606 hstd[sel[i]] += (*this)[i]*(*this)[i];
609 for ( j=0; j<ngauss; j++ ) {
612 hstd[j] -= havg[j]*havg[j];
613 if ( hstd[j] > 0 ) hstd[j] = sqrt(hstd[j]);
619 sel =
new long[bins];
622 for ( i=nh=0; i<bins; i++ ) {
623 if ( histo[i] < thresh ) {
631 if ( nh < 20 && nh < bins/2 )
632 cerr <<
"Warning: Too few bins selected for fitting! (" << nh <<
"/" << bins <<
")" << endl;
635 vector<double> xx(bins), fx(nh);
636 for ( i=j=0; i<bins; i++ )
if ( sel[i] ) {
637 xx[j] = min + i/scale;
639 if ( hmax < histo[i] ) hmax = histo[i];
645 Bsimplex simp(1, 3*ngauss, 1, nh, xx, fx);
648 for ( i=j=0; j<ngauss; j++, i++ ) {
650 simp.
limits(i, datasize/bins, 5*hmax);
655 simp.
limits(i, havg[j] - 5*hstd[j], havg[j] + 5*hstd[j]);
665 simp.
limits(i, hstd[j]/5, 5*hstd[j]);
668 simp.
limits(i, 1/scale, 0.5*(max - min)/n);
675 vector<double> gauss;
676 for ( j=0; j<3*ngauss; j++ ) gauss.push_back(simp.
parameter(j));
679 cout <<
"Gaussian fit over " << nh <<
" bins:" << endl;
680 cout <<
"Amp\tOffset\tSigma\tSIP" << endl;
681 for ( j=0; j<ngauss; j++ )
682 cout << gauss[3*j] <<
tab << gauss[3*j+1] <<
tab << gauss[3*j+2] <<
tab << bins*gauss[3*j]/datasize << endl;
683 cout <<
"Sampling-independent RMSD: " << bins*R/datasize << endl << endl;
704 long i, j, k, m, ncol(ngauss+2);
705 double v, bin_half = (max - min)/(4*bins);
709 double scale, offset;
710 vector<long> histo =
histogram(bins, scale, offset);
716 Bstring title(
"Gaussian fit"), txt;
725 for ( i=1; i<ncol; i++ ) {
731 for ( i=2; i<ncol; i++ )
741 for ( j=0; j<ngauss; j++ ){
742 txt =
Bstring(j+1,
"Gaussian %d:") +
Bstring(gauss[3*j],
" a=%g") +
749 for ( i=0, j=bins; i<bins; i++, j++ ) {
750 (*plot)[i] = min + i/scale;
751 (*plot)[j] = histo[i];
752 for ( k=0, m=i+2*bins; k<3*ngauss; k+=3, m+=bins ) {
753 v = ((*plot)[i] - gauss[k+1])/gauss[k+2];
754 (*plot)[m] = gauss[k]*exp(-0.5*v*v);
812 double fact(1), pl(1), df, R(0);
818 for ( i=0; i<simp.
points(); i++ ) {
823 df = f[i] - pl*ep/fact;
827 if ( i ) R = sqrt(R/i);
981 if ( bins > avg + 5*std ) bins = avg + 5*std;
982 if ( bins > 150 ) bins = 150;
983 if ( bins < 5 ) bins = 5;
985 long i, j, m, imax(0);
986 double hmax(0), scale(bins*1.0/(avg+5*std));
988 vector<double> b(bins, 0);
989 vector<double> h(bins, 0);
991 for ( i=0; i<datasize; ++i ) {
992 j = (long) (scale*(*
this)[i] + 0.5);
993 if ( j >= 0 && j < bins ) h[j]+=1;
996 for ( i=0; i<bins; ++i ) {
1004 if ( imax < 1 ) imax = 1;
1008 cout <<
"scale=" << scale <<
" imax=" << imax << endl;
1010 Bsimplex simp(1, 2, 0, bins, b, h);
1013 simp.
limits(0, hmax, 20*hmax);
1016 if ( avg < imax ) simp.
parameter(1, avg);
1018 simp.
limits(1, avg/2, 2*imax);
1023 cout <<
"Poisson function fit over " << bins <<
" bins:" << endl;
1024 cout <<
"Amplitude: " << simp.
parameter(0) << endl;
1025 cout <<
"Lambda: " << simp.
parameter(1) << endl;
1026 cout <<
"Sampling-independent RMSD: " << bins*R/datasize << endl << endl;
1035 Bstring title(
"Poisson fit"), txt;
1067 for ( i=0, j=bins, m=2*bins; i<bins; i++, j++, m++ ) {