48 Histogram::Histogram(
double minimum,
double maximum,
int nbins) {
49 SetValidRange(minimum, maximum);
50 SetBinRange(minimum, maximum);
76 Histogram::Histogram(
Cube &cube,
int statsBand,
Progress *progress,
77 double startSample,
double startLine,
78 double endSample,
double endLine,
79 int bins,
bool addCubeData) {
80 InitializeFromCube(cube, statsBand, progress, bins, startSample, startLine,
84 Brick cubeDataBrick((
int)(endSample - startSample + 1),
88 int startBand = statsBand;
89 int endBand = statsBand;
96 if (progress != NULL) {
97 progress->
SetText(
"Gathering histogram");
99 (
int)(endLine - startLine + 1) * (
int)(endBand - startBand + 1));
103 for (
int band = startBand; band <= endBand; band++) {
104 for (
int line = (
int)startLine; line <= endLine; line++) {
105 cubeDataBrick.SetBasePosition(qRound(startSample), line, band);
106 cube.
read(cubeDataBrick);
107 AddData(cubeDataBrick.DoubleBuffer(), cubeDataBrick.size());
108 if (progress != NULL) {
129 string msg =
"The number of Histogram Bins must be greater than 0";
135 rangesFromNet(net,statFunc);
138 addMeasureDataFromNet(net,statFunc);
153 if (binWidth <= 0 ) {
154 string msg =
"The width of Histogram Bins must be greater than 0";
159 rangesFromNet(net,statFunc);
165 SetBinRange(binWidth*(floor(this->
ValidMinimum()/binWidth)+0.5),
167 SetValidRange(binWidth*floor(this->
ValidMinimum()/binWidth),
172 int nBins = int ( ceil(domain/binWidth) );
176 addMeasureDataFromNet(net,statFunc);
189 for (
int i=0;i<nObjPts;i++) {
191 if (point->IsIgnored())
continue;
194 int nObs = point->GetNumMeasures();
195 for (
int j=0;j<nObs;j++) {
197 if (measure->IsIgnored())
continue;
198 this->AddData((measure->*statFunc)());
218 for (
int i=0;i<nObjPts;i++) {
220 if (point->IsIgnored())
continue;
223 int nObs = point->GetNumMeasures();
224 for (
int j=0;j<nObs;j++) {
226 if (measure->IsIgnored())
continue;
228 temp = (measure->*statFunc)();
229 if (temp > max) max = temp;
230 if (temp < min) min = temp;
236 string msg =
"The net file appears to have 1 or fewer measures with residual data, "
237 "thus no histogram for this net file can be created;";
242 SetValidRange(min, max);
243 SetBinRange(min, max);
247 void Histogram::InitializeFromCube(
Cube &cube,
int statsBand,
248 Progress *progress,
int nbins,
double startSample,
double startLine,
249 double endSample,
double endLine) {
251 if ((statsBand < 0) || (statsBand > cube.
bandCount())) {
252 string msg =
"Cannot gather histogram for band [" +
IString(statsBand) +
259 double minDnValue =
Null;
260 double maxDnValue =
Null;
271 else if (cube.
pixelType() == SignedWord) {
286 IString msg =
"Unsupported pixel type";
287 throw IException(IException::Programmer, msg,
_FILEINFO_);
290 if (startSample ==
Null)
293 if (endSample ==
Null)
296 if (startLine ==
Null)
303 if (minDnValue ==
Null || maxDnValue ==
Null) {
305 Brick cubeDataBrick((
int)(endSample - startSample + 1),
312 int startBand = statsBand;
313 int endBand = statsBand;
315 if (statsBand == 0) {
320 if (progress != NULL) {
321 progress->
SetText(
"Computing min/max for histogram");
323 (
int)(endLine - startLine + 1) * (
int)(endBand - startBand + 1));
327 for (
int band = startBand; band <= endBand; band++) {
328 for (
int line = (
int)startLine; line <= endLine; line++) {
329 cubeDataBrick.SetBasePosition(qRound(startSample), line, band);
330 cube.
read(cubeDataBrick);
331 stats.AddData(cubeDataBrick.DoubleBuffer(), cubeDataBrick.size());
332 if (progress != NULL) {
338 if (stats.ValidPixels() == 0) {
343 minDnValue = stats.Minimum();
344 maxDnValue = stats.Maximum();
349 SetBinRange(minDnValue, maxDnValue);
355 Histogram::~Histogram() {
358 void Histogram::SetBinRange(
double binStart,
double binEnd) {
359 if (binEnd < binStart) {
360 string msg =
"The binning range start [" +
IString(binStart) +
361 " must be less than the end [" +
IString(binEnd) +
".";
365 p_binRangeStart = binStart;
366 p_binRangeEnd = binEnd;
371 void Histogram::Reset() {
373 for (
int i = 0; i < (int)p_bins.size(); i++) {
380 void Histogram::SetBins(
const int nbins) {
381 p_bins.resize(nbins);
393 void Histogram::AddData(
const double *data,
394 const unsigned int count) {
395 Statistics::AddData(data, count);
397 int nbins = p_bins.size();
399 for (
unsigned int i = 0; i < count; i++) {
401 if (BinRangeStart() == BinRangeEnd()) {
405 index = (int) floor((
double)(nbins - 1) / (BinRangeEnd() - BinRangeStart()) *
406 (data[i] - BinRangeStart()) + 0.5);
408 if (index < 0) index = 0;
409 if (index >= nbins) index = nbins - 1;
423 void Histogram::AddData(
const double data) {
424 Statistics::AddData(data);
426 int nbins = p_bins.size();
429 if (BinRangeStart() == BinRangeEnd()) {
433 index = (int) floor((
double)(nbins - 1) / (BinRangeEnd() - BinRangeStart()) *
434 (data - BinRangeStart()) + 0.5);
436 if (index < 0) index = 0;
437 if (index >= nbins) index = nbins - 1;
452 void Histogram::RemoveData(
const double *data,
453 const unsigned int count) {
454 Statistics::RemoveData(data, count);
456 int nbins = p_bins.size();
458 for (
unsigned int i = 0; i < count; i++) {
460 if (BinRangeStart() == BinRangeEnd()) {
464 index = (int) floor((
double)(nbins - 1) / (BinRangeEnd() - BinRangeStart()) *
465 (data[i] - BinRangeStart()) + 0.5);
467 if (index < 0) index = 0;
468 if (index >= nbins) index = nbins - 1;
479 double Histogram::Median()
const {
480 return Percent(50.0);
488 double Histogram::Mode()
const {
490 for (
int i = 0; i < (int)p_bins.size(); i++) {
491 if (p_bins[i] > p_bins[mode]) mode = i;
494 if (p_bins[mode] < 1)
return NULL8;
496 return BinMiddle(mode);
510 double Histogram::Percent(
double percent)
const {
511 if ((percent < 0.0) || (percent > 100.0)) {
512 string m =
"Argument percent outside of the range 0 to 100 in";
513 m +=
" [Histogram::Percent]";
517 if (ValidPixels() < 1)
return NULL8;
519 BigInt currentPixels = 0;
520 double currentPercent;
522 for (
int i = 0; i < (int)p_bins.size(); i++) {
523 currentPixels += p_bins[i];
524 currentPercent = (double) currentPixels / (
double) ValidPixels() * 100.0;
525 if (currentPercent >= percent) {
530 return BinMiddle((
int)p_bins.size() - 1);
541 double Histogram::Skew()
const {
542 if (ValidPixels() < 1)
return NULL8;
543 double sdev = StandardDeviation();
544 if (sdev == 0.0)
return 0.0;
545 return 3.0 * (
Average() - Median()) / sdev;
556 BigInt Histogram::BinCount(
const int index)
const {
557 if ((index < 0) || (index >= (
int)p_bins.size())) {
562 return p_bins[index];
577 void Histogram::BinRange(
const int index,
578 double &low,
double &high)
const {
579 if ((index < 0) || (index >= (
int)p_bins.size())) {
584 double binSize = (BinRangeEnd() - BinRangeStart()) / (
double)(p_bins.size() - 1);
585 low = BinRangeStart() - binSize / 2.0 + binSize * (double) index;
586 high = low + binSize;
598 double Histogram::BinMiddle(
const int index)
const {
599 if ((index < 0) || (index >= (
int)p_bins.size())) {
605 BinRange(index, low, high);
606 return (low + high) / 2.0;
617 double Histogram::BinSize()
const {
619 BinRange(0, low, high);
629 int Histogram::Bins()
const {
630 return (
int)p_bins.size();
639 BigInt Histogram::MaxBinCount()
const {
641 BigInt maxBinCount = 0;
642 for (
int i = 0; i < (int)p_bins.size(); i++) {
643 if (p_bins[i] > maxBinCount) maxBinCount = p_bins[i];