/usr/include/casacore/lattices/LatticeMath/LatticeHistograms.tcc is in casacore-dev 2.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 | //# LatticeHistograms.cc: generate histograms from a Lattice
//# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: aips2-request@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
//#
//# $Id: LatticeHistograms.tcc 21563 2015-02-16 07:05:15Z gervandiepen $
#ifndef LATTICES_LATTICEHISTOGRAMS_TCC
#define LATTICES_LATTICEHISTOGRAMS_TCC
#include <casacore/lattices/LatticeMath/LatticeHistograms.h>
#include <casacore/lattices/LatticeMath/LatticeHistSpecialize.h>
#include <casacore/lattices/LatticeMath/LatticeHistProgress.h>
#include <casacore/lattices/LatticeMath/LattStatsSpecialize.h>
#include <casacore/casa/aips.h>
#include <casacore/casa/Arrays/Array.h>
#include <casacore/casa/Arrays/ArrayMath.h>
#include <casacore/casa/Arrays/ArrayLogical.h>
#include <casacore/casa/Arrays/VectorIter.h>
#include <casacore/lattices/Lattices/MaskedLattice.h>
#include <casacore/lattices/LatticeMath/LatticeApply.h>
#include <casacore/lattices/Lattices/LatticeIterator.h>
#include <casacore/lattices/Lattices/LatticeStepper.h>
#include <casacore/lattices/LRegions/LatticeRegion.h>
#include <casacore/lattices/Lattices/TempLattice.h>
#include <casacore/lattices/Lattices/SubLattice.h>
#include <casacore/lattices/LatticeMath/LatticeStatsBase.h>
#include <casacore/casa/Logging/LogIO.h>
#include <casacore/casa/BasicMath/Math.h>
#include <casacore/casa/Quanta/QMath.h>
#include <casacore/tables/Tables/Table.h>
#include <casacore/casa/OS/HostInfo.h>
#include <casacore/casa/System/PGPlotter.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/Utilities/DataType.h>
#include <casacore/casa/Utilities/ValType.h>
#include <casacore/casa/BasicSL/String.h>
#include <casacore/casa/iomanip.h>
#include <casacore/casa/stdlib.h>
#include <casacore/casa/sstream.h>
namespace casacore { //# NAMESPACE CASACORE - BEGIN
// Public functions
template <class T>
LatticeHistograms<T>::LatticeHistograms (const MaskedLattice<T>& lattice,
LogIO &os, Bool showProgress,
Bool forceDisk)
: os_p(os),
goodParameterStatus_p(True),
error_p(""),
pInLattice_p(0),
pStoreLattice_p(0),
pStats_p(0),
binAll_p(True),
needStorageLattice_p(True),
doCumu_p(False),
doGauss_p(False),
doList_p(False),
doLog_p(False),
haveLogger_p(True),
showProgress_p(showProgress),
forceDisk_p(forceDisk),
nBins_p(25)
//
// Constructor.
//
{
nxy_p.resize(0);
range_p.resize(0);
blcParent_p.resize(0);
if (setNewLattice(lattice)) {
// Cursor axes defaults to all
Vector<Int> cursorAxes;
goodParameterStatus_p = setAxes(cursorAxes);
} else {
os_p << error_p << LogIO::EXCEPTION;
}
}
template <class T>
LatticeHistograms<T>::LatticeHistograms (const MaskedLattice<T>& lattice,
Bool showProgress,
Bool forceDisk)
:
goodParameterStatus_p(True),
error_p(""),
pInLattice_p(0),
pStoreLattice_p(0),
pStats_p(0),
binAll_p(True),
needStorageLattice_p(True),
doCumu_p(False),
doGauss_p(False),
doList_p(False),
doLog_p(False),
haveLogger_p(False),
showProgress_p(showProgress),
forceDisk_p(forceDisk),
nBins_p(25)
//
// Constructor.
//
{
nxy_p.resize(0);
range_p.resize(0);
blcParent_p.resize(0);
if (setNewLattice(lattice)) {
// Cursor axes defaults to all
Vector<Int> cursorAxes;
goodParameterStatus_p = setAxes(cursorAxes);
} else {
os_p << error_p << LogIO::EXCEPTION;
}
}
template <class T>
LatticeHistograms<T>::LatticeHistograms(const LatticeHistograms<T> &other)
: pInLattice_p(0),
pStoreLattice_p(0),
pStats_p(0)
//
// Copy constructor. Storage lattice not copied.
//
{
operator=(other);
}
template <class T>
LatticeHistograms<T> &LatticeHistograms<T>::operator=(const LatticeHistograms<T> &other)
//
// Assignment operator. Storage lattices not copied.
//
{
if (this != &other) {
// Deal with pointer
if (pInLattice_p!=0) delete pInLattice_p;
pInLattice_p = other.pInLattice_p->cloneML();
// Delete storage and statistics objects.
if (pStoreLattice_p != 0) {
delete pStoreLattice_p;
pStoreLattice_p = 0;
}
//
if (pStats_p != 0) {
delete pStats_p;
pStats_p = 0;
}
needStorageLattice_p = True;
// Do the rest
os_p = other.os_p;
binAll_p = other.binAll_p;
goodParameterStatus_p = other.goodParameterStatus_p;
doCumu_p = other.doCumu_p;
doGauss_p = other.doGauss_p;
doList_p = other.doList_p;
doLog_p = other.doLog_p;
haveLogger_p = other.haveLogger_p;
showProgress_p = other.showProgress_p;
nBins_p = other.nBins_p;
cursorAxes_p = other.cursorAxes_p;
displayAxes_p = other.displayAxes_p;
plotter_p = other.plotter_p;
nxy_p = other.nxy_p;
range_p = other.range_p;
blcParent_p = other.blcParent_p;
forceDisk_p = other.forceDisk_p;
error_p = other.error_p;
}
return *this;
}
template <class T>
LatticeHistograms<T>::~LatticeHistograms()
//
// Destructor.
//
{
delete pInLattice_p;
pInLattice_p = 0;
if (pStoreLattice_p != 0) {
delete pStoreLattice_p;
pStoreLattice_p = 0;
}
if (pStats_p != 0) {
delete pStats_p;
pStats_p = 0;
}
}
template <class T>
Bool LatticeHistograms<T>::setAxes (const Vector<Int>& axes)
//
// This function sets the cursor axes and the display axes
//
{
if (!goodParameterStatus_p) {
return False;
}
// Save current cursor axes
Vector<Int> saveAxes(cursorAxes_p.copy());
// Set cursor arrays (can't assign to potentially zero length array)
cursorAxes_p.resize(0);
cursorAxes_p = axes;
if (cursorAxes_p.nelements() == 0) {
// User didn't give any axes. Set them to all.
cursorAxes_p.resize(pInLattice_p->ndim());
for (uInt i=0; i<pInLattice_p->ndim(); i++) cursorAxes_p(i) = i;
} else {
for (uInt i=0; i<cursorAxes_p.nelements(); i++) {
if (cursorAxes_p(i) < 0 || cursorAxes_p(i) > Int(pInLattice_p->ndim()-1)) {
error_p = "Invalid cursor axes";
return False;
}
}
}
// Set the display axes
displayAxes_p.resize(0);
displayAxes_p = IPosition::otherAxes(pInLattice_p->ndim(),
cursorAxes_p).asVector();
// Signal that we have changed the axes and need new accumulation lattices
if (saveAxes.nelements() != cursorAxes_p.nelements() ||
!allEQ(saveAxes, cursorAxes_p)) needStorageLattice_p = True;
return True;
}
template <class T>
Bool LatticeHistograms<T>::setNBins (const uInt& nBins)
//
// Set the number of bins
//
{
if (!goodParameterStatus_p) {
return False;
}
// Save number of bins
const uInt saveNBins = nBins_p;
if (nBins < 1) {
error_p = "Invalid number of bins";
goodParameterStatus_p = False;
return False;
} else {
nBins_p = nBins;
}
// Signal that we need a new accumulation lattice
if (saveNBins != nBins_p) needStorageLattice_p = True;
return True;
}
template <class T>
Bool LatticeHistograms<T>::setIncludeRange(const Vector<T>& include)
//
// Assign the desired inclusion range
//
{
if (!goodParameterStatus_p) {
return False;
}
// Save current ranges
Vector<T> saveRange(range_p.copy());
// CHeck
Bool noInclude;
ostringstream os;
if (!setInclude(range_p, noInclude, include, os)) {
error_p = "Invalid pixel inclusion range";
goodParameterStatus_p = False;
return False;
}
binAll_p = noInclude;
// Signal that we need new accumulation lattices
if (saveRange.nelements() != range_p.nelements() ||
!allEQ(saveRange, range_p)) needStorageLattice_p = True;
return True;
}
template <class T>
Bool LatticeHistograms<T>::setGaussian (const Bool& doGauss)
//
// Specify whether there should be a Gaussian overlay or not
//
{
if (!goodParameterStatus_p) {
return False;
}
doGauss_p = doGauss;
return True;
}
template <class T>
Bool LatticeHistograms<T>::setForm (const Bool& doLog, const Bool& doCumu)
//
// Specify whether the form of the histogram should be linear/log
// or cumulative or not.
//
{
if (!goodParameterStatus_p) {
return False;
}
doLog_p = doLog;
doCumu_p = doCumu;
return True;
}
template <class T>
Bool LatticeHistograms<T>::setStatsList (const Bool& doList)
//
// See if user wants to list statistics as well
//
{
if (!goodParameterStatus_p) {
return False;
}
doList_p = doList;
return True;
}
template <class T>
Bool LatticeHistograms<T>::setPlotting(PGPlotter& plotter,
const Vector<Int>& nxy)
//
// Assign the desired PGPLOT device name and number
// of subplots
//
{
if (!goodParameterStatus_p) {
return False;
}
// Is new plotter attached ?
if (!plotter.isAttached()) {
error_p = "Input plotter is not attached";
goodParameterStatus_p = False;
return False;
}
// Don't reattach to the same plotter. The assignment will
// close the previous device
if (plotter_p.isAttached()) {
if (plotter_p.qid() != plotter.qid()) plotter_p = plotter;
} else {
plotter_p = plotter;
}
// Plotting device and subplots. nxy_p is set to [1,1] if zero length
nxy_p.resize(0);
nxy_p = nxy;
ostringstream os;
if (!LatticeStatsBase::setNxy(nxy_p, os)) {
error_p = "Invalid number of subplots";
goodParameterStatus_p = False;
return False;
}
return True;
}
template <class T>
Bool LatticeHistograms<T>::setNewLattice(const MaskedLattice<T>& lattice)
//
// Assign pointer to lattice
//
{
if (!goodParameterStatus_p) {
return False;
}
T* dummy = 0;
DataType latticeType = whatType(dummy);
if (latticeType !=TpFloat && latticeType != TpComplex) {
ostringstream oss;
oss << "Lattices of type " << latticeType << " are not currently supported" << endl;
error_p = String(oss);
goodParameterStatus_p = False;
pInLattice_p = 0;
return False;
}
// Clone pointer
if (pInLattice_p!=0) delete pInLattice_p;
pInLattice_p = lattice.cloneML();
// This is the location of the input SubLattice in
// the parent Lattice
blcParent_p = pInLattice_p->region().slicer().start();
// Signal that we have changed the lattice and need a new accumulation
// lattice
needStorageLattice_p = True;
return True;
}
template <class T>
void LatticeHistograms<T>::closePlotting()
{
if (plotter_p.isAttached()) plotter_p.detach();
}
template <class T>
Bool LatticeHistograms<T>::display()
//
// This function displays (plotting and listing) the requested
// histograms as a function of the display axes
//
{
if (!goodParameterStatus_p) {
return False;
}
// Generate storage lattices if required
if (needStorageLattice_p) {
if (!generateStorageLattice()) return False;
}
// Display histograms
displayHistograms ();
return True;
}
template <class T>
Bool LatticeHistograms<T>::getHistograms(
Array<T>& values, Array<T>& counts
) {
Array<Vector<T> > stats;
return getHistograms(values, counts, stats);
}
template <class T>
Bool LatticeHistograms<T>::getHistograms(
Array<T>& values, Array<T>& counts, Array<Vector<T> >& stats
) {
if (!goodParameterStatus_p) {
return False;
}
// Generate storage lattices if required
if (needStorageLattice_p) {
if (!generateStorageLattice()) return False;
}
// Set up iterator to work through histogram storage lattice line by line
// Use the LatticeStepper (default) which will guarantee the access pattern.
// There will be no overhang (as tile shape for first axis is length of axis)
IPosition cursorShape(pStoreLattice_p->ndim(),1);
cursorShape(0) = pStoreLattice_p->shape()(0);
IPosition vectorAxis(1,0);
// Make the stepper explicitly so we can specify the cursorAxes
// and then vectorCursor will cope with an axis of length 1
// (it is possible the user could ask for a histogram with one bin !)
LatticeStepper histStepper(
pStoreLattice_p->shape(), cursorShape,
vectorAxis, IPosition::makeAxisPath(pStoreLattice_p->ndim())
);
RO_LatticeIterator<T> histIterator(*pStoreLattice_p, histStepper);
// Resize output arrays and setup vector iterators
IPosition shape = pStoreLattice_p->shape();
counts.resize(shape);
values.resize(shape);
static const IPosition removeAxis(1, 0);
stats.resize(
shape.size() == 1 ? IPosition(1,1) : shape.removeAxes(removeAxis)
);
VectorIterator<T> valuesIterator(values);
VectorIterator<T> countsIterator(counts);
Vector<T> stat;
T linearSum, linearYMax;
// Iterate through histogram storage lattice
for (
histIterator.reset(),valuesIterator.origin(),countsIterator.origin();
! histIterator.atEnd();
histIterator++,valuesIterator.next(),countsIterator.next()
) {
// Find statistics from the data that made this histogram
IPosition pos = histIterator.position();
getStatistics (stat, pos);
stats(pos.size() == 1 ? IPosition(1, 0) : pos.removeAxes(removeAxis)).assign(stat);
// Extract the histogram in the appropriate form
extractOneHistogram(
linearSum, linearYMax, valuesIterator.vector(),
countsIterator.vector(), stat,
histIterator.vectorCursor()
);
}
return True;
}
template <class T>
Bool LatticeHistograms<T>::getHistogram (Vector<T>& values,
Vector<T>& counts,
const IPosition& pos,
const Bool posInLattice)
//
// Retrieve histogram values and counts from specified
// location into vectors
//
// Inputs:
// posInLattice If true the location is given as lattice coordinates
// The non-display axis values will be ignored.
// Otherwise the position should be for the
// display axes only.
//
{
if (!goodParameterStatus_p) {
return False;
}
// Make sure we have a correctly size position
if (posInLattice) {
if (pos.nelements() != pInLattice_p->ndim()) {
error_p = "Incorrectly sized position given";
values.resize(0);
counts.resize(0);
return False;
}
} else {
if (pos.nelements() != displayAxes_p.nelements()) {
error_p = "Incorrectly sized position given";
values.resize(0);
counts.resize(0);
return False;
}
}
// Generate storage lattices if required
if (needStorageLattice_p) {
if (!generateStorageLattice()) return False;
}
// Set position for getting slice from storage lattice
const uInt nDim = displayAxes_p.nelements();
IPosition histPos(nDim+1,0);
if (posInLattice) {
// Discard non display axes
for (uInt i=0; i<nDim; i++) {
histPos(i+1) = pos(displayAxes_p(i));
}
} else {
// Use position as is
for (uInt i=0; i<nDim; i++) {
histPos(i+1) = pos(i);
}
}
// Get histogram slice of integer counts (i.e. linear,
// not cumulative or logarithmic etc)
IPosition sliceShape(nDim+1,1);
sliceShape(0) = nBins_p;
Array<T> intCounts;
pStoreLattice_p->getSlice(intCounts, histPos, sliceShape,
IPosition(nDim+1,1), False);
// Copy integer counts to a Vector
Vector<T> intCountsV(nBins_p);
histPos = 0;
for (uInt i=0; i<nBins_p; i++) {
histPos(0) = i;
intCountsV(i) = intCounts(histPos);
}
// Get statistics slice.
Vector<AccumType> statsA;
Vector<T> statsT;
pStats_p->getStats(statsA, pos, posInLattice);
statsT.resize(statsA.nelements());
convertArray (statsT, statsA);
// Convert to desired form and make values vector too
counts.resize(nBins_p);
values.resize(nBins_p);
T linearSum, linearYMax;
extractOneHistogram (linearSum, linearYMax, values,
counts, statsT, intCountsV);
return True;
}
// Private functions
template <class T>
Bool LatticeHistograms<T>::displayHistograms ()
//
// Display the histograms as a function of the display axes
//
{
// Set up for plotting
if (plotter_p.isAttached()) {
plotter_p.subp(nxy_p(0), nxy_p(1));
plotter_p.ask(True);
plotter_p.sch(1.2);
plotter_p.svp(0.1,0.9,0.1,0.9);
} else {
error_p = "Plotter is not attached";
return False;
}
// Set up iterator to work through histogram storage lattice line by line.
// We don't use the TiledLineStepper to guarentee the access pattern is
// row based rather than tile based. There will be no overhang because
// the tile shape for the histogram axis is the size of the histogram
IPosition cursorShape(pStoreLattice_p->ndim(),1);
cursorShape(0) = pStoreLattice_p->shape()(0);
IPosition vectorAxis(1);
vectorAxis(0) = 0;
// Make the stepper explicitly so we can specify the cursorAxes
// and then vectorCursor will cope with an axis of length 1
// (it is possible the user could ask for a histogram with one bin !)
LatticeStepper histStepper(pStoreLattice_p->shape(), cursorShape,
vectorAxis, IPosition::makeAxisPath(pStoreLattice_p->ndim()));
RO_LatticeIterator<T> histIterator(*pStoreLattice_p, histStepper);
// Histogram vectors and other bits and pieces
Vector<T> counts(pStoreLattice_p->shape()(0));
Vector<T> values(pStoreLattice_p->shape()(0));
Vector<T> stats;
T linearSum, linearYMax;
IPosition latticePos(pInLattice_p->ndim(),0);
// Iterate through histogram storage lattice
for (histIterator.reset(); !histIterator.atEnd(); histIterator++) {
// Find statistics from the data that made this histogram
getStatistics (stats, histIterator.position());
// Extract histogram in the form requested for plotting
extractOneHistogram (linearSum, linearYMax, values, counts, stats,
histIterator.vectorCursor());
// Display the histogram
if (!displayOneHistogram (linearSum, linearYMax,
histIterator.position(),
stats, values, counts,
plotter_p)) return False;
}
return True;
}
template <class T>
Bool LatticeHistograms<T>::displayOneHistogram (const T& linearSum,
const T& linearYMax,
const IPosition& histPos,
const Vector<T>& stats,
const Vector<T>& values,
const Vector<T>& counts,
PGPlotter& plotter)
//
// Display the histogram and optionally the equivalent Gaussian
//
// Inputs
// histPos location in histogram storage lattice of start of
// this histogram. Remember that the first axis
// of the storage lattice has the counts.
//
{
// Are we going to see the Gaussian ?
Bool doGauss2 = False;
if (doGauss_p && stats(LatticeStatsBase::SIGMA)>0) doGauss2 = True;
// Set binwidth
const T binWidth = LatticeHistSpecialize::setBinWidth(stats(LatticeStatsBase::MIN),
stats(LatticeStatsBase::MAX),
nBins_p);
// Do plots
LatticeHistSpecialize::plot(plotter, doGauss_p, doCumu_p, doLog_p,
linearSum, linearYMax, binWidth, values,
counts, stats, 0, 1, True);
// Write values of the display axes on the plot
T* dummy = 0;
DataType type = whatType(dummy);
Float nchar = 0.5;
if (type==TpComplex) nchar = 1.5;
String coords = writeCoordinates(histPos);
if (!writeDispAxesValues (coords, plotter, nchar)) return False;
if (haveLogger_p && doList_p) {
// List pixel coordinates of display axes for this histogram
// Write statistics to a LogIO object
os_p << coords << endl;
listStatistics(os_p, stats, binWidth);
}
return True;
}
template <class T>
void LatticeHistograms<T>::extractOneHistogram (T& linearSum,
T& linearYMax,
Vector<T>& values,
Vector<T>& counts,
const Vector<T>& stats,
const Vector<T>& intCounts)
//
// Extract this histogram, convert to the appropriate form
// and return the values and counts
//
{
// FIsh out min and max
Vector<T> range(2);
range(0) = stats(LatticeStatsBase::MIN);
range(1) = stats(LatticeStatsBase::MAX);
// Set bin width
const uInt nBins = nBins_p;
const T binWidth = LatticeHistSpecialize::setBinWidth(range(0), range(1), nBins);
// Copy histogram counts into output T array and generate
// values (abcissa) array
T xx = range(0) + binWidth/2.0;
linearYMax = -1.0;
linearSum = 0.0;
for (uInt i=0; i<intCounts.nelements(); i++) {
values(i) = xx;
counts(i) = intCounts(i);
xx += binWidth;
linearYMax = LattStatsSpecialize::max(linearYMax,counts(i));
linearSum += counts(i);
}
linearSum = LatticeHistSpecialize::mul(linearSum, binWidth);
// Make histogram cumulative if desired
if (doCumu_p) LatticeHistSpecialize::makeCumulative (counts, linearYMax, nBins, 1.0);
// Make histogram logarithmic if desired
if (doLog_p) LatticeHistSpecialize::makeLogarithmic (counts, linearYMax, nBins);
}
template <class T>
Bool LatticeHistograms<T>::generateStorageLattice()
//
// Generate the histogram, and statistics storage lattices.
//
{
// Set the display axes vector if needed
if (displayAxes_p.nelements()==0) {
displayAxes_p.resize(0);
displayAxes_p = IPosition::otherAxes(pInLattice_p->ndim(),
cursorAxes_p).asVector();
}
// Make the statistics object
if (!makeStatistics()) return False;
// Fill the histogram storage lattice
makeHistograms();
needStorageLattice_p = False;
return True;
}
template <class T>
void LatticeHistograms<T>::getStatistics (Vector<T> &stats,
const IPosition& histPos) const
//
// Extract statistics slice for the given position in the
// histogram storage lattice.
//
// Input:
// histPos The location in the histogram storage lattice
// Outputs
// stats The statistics for this chunk.
{
// Discard the histogram axis location
uInt n = displayAxes_p.nelements();
IPosition pos;
if (n > 0) {
pos.resize(n);
for (uInt i=0; i<n; i++) {
pos(i) = histPos(i+1);
}
}
// Get the statistics
Vector<AccumType> statsA;
pStats_p->getStats(statsA, pos, False);
stats.resize(statsA.nelements());
convertArray (stats, statsA);
}
template <class T>
void LatticeHistograms<T>::listStatistics(LogIO& os,
const Vector<T>& stats,
T binWidth)
{
// Have to convert LogIO object to ostream before can apply
// the manipulators
const Int oPrec = 6;
setStream(os.output(), oPrec);
ostringstream os0, os1, os2, os3, os4, os5, os6, os7;
setStream(os0, oPrec); setStream(os1, oPrec); setStream(os2, oPrec);
setStream(os3, oPrec); setStream(os4, oPrec); setStream(os5, oPrec);
setStream(os6, oPrec); setStream(os7, oPrec);
//
T* dummy = 0;
DataType type = whatType(dummy);
Int oWidth;
if (type==TpFloat) {
oWidth = 15; //
} else if (type==TpComplex) {
oWidth = 33; // (x, y)
}
//
os << "No. binned = ";
os.output() << setw(oWidth) << Int64(std::real(stats(LatticeStatsBase::NPTS))+0.1) << endl;
os << "Sum = ";
os0 << stats(LatticeStatsBase::SUM);
os.output() << setw(oWidth) << String(os0) << " Mean = ";
os1 << stats(LatticeStatsBase::MEAN);
os.output() << setw(oWidth) << String(os1) << endl;
//
os << "Variance = ";
os2 << stats(LatticeStatsBase::VARIANCE);
os.output() << setw(oWidth) << String(os2);
//
if (stats(LatticeStatsBase::VARIANCE)> 0.0) {
os << " Sigma = ";
os3 << stats(LatticeStatsBase::SIGMA);
os.output() << setw(oWidth) << String(os3) << endl;
} else {
os << endl;
}
os << "Rms = ";
os4 << stats(LatticeStatsBase::RMS);
os.output() << setw(oWidth) << String(os4) << endl;
os << endl;
os << "Bin width = ";
os5 << binWidth;
os.output() << setw(oWidth) << String(os5) << endl;
os << "Min binned = ";
os6 << stats(LatticeStatsBase::MIN);
os.output() << setw(oWidth) << String(os6) << " Max binned = ";
os7 << stats(LatticeStatsBase::MAX);
os.output() << setw(oWidth) << String(os7) << endl << endl << endl;
os.post();
}
template <class T>
IPosition LatticeHistograms<T>::locHistInLattice(const IPosition& storagePosition,
Bool relativeToParent) const
//
// Given a location in the histogram storage lattice, convert those locations on
// the non-histogram axis (the histogram axis is the first one) to locations
// in the original parent lattice. Optionally account for the location of the
// subLattice in the parent lattice
//
{
IPosition pos(storagePosition);
for (uInt j=1; j<pos.nelements(); j++) {
if (relativeToParent) {
pos(j) = storagePosition(j) + blcParent_p(displayAxes_p(j-1));
} else {
pos(j) = storagePosition(j);
}
}
return pos;
}
template <class T>
Bool LatticeHistograms<T>::makeStatistics()
{
// Create LatticeStatistics object. Show progress meter.
if (pStats_p != 0) delete pStats_p;
pStats_p = new LatticeStatistics<T>(*pInLattice_p, os_p, showProgress_p, forceDisk_p);
// Set state. Make sure that the min/max is set to the
// user's include range if there is one. LatticeHistograms
// only allows an inclusion range, and range_p is already
// filled with it.
Vector<T> exclude;
if (!pStats_p->setInExCludeRange(range_p, exclude, True)) return False;
if (!pStats_p->setAxes(cursorAxes_p)) return False;
// We get an arbitary statistics slice here so as to
// activate the statistics object and make it a bit
// more obvious to the user the order in which things are done.
Vector<AccumType> stats;
IPosition pos(displayAxes_p.nelements(),0);
if (!pStats_p->getStats(stats, pos, False)) return False;
return True;
}
template <class T>
void LatticeHistograms<T>::makeHistograms()
{
if (haveLogger_p) {
os_p << LogIO::DEBUG1 << "Creating new histogram storage lattice" << LogIO::POST;
}
// Set storage lattice shape. The first axis is the histogram axis
IPosition storeLatticeShape;
LatticeStatsBase::setStorageImageShape(storeLatticeShape, False, Int(nBins_p),
displayAxes_p, pInLattice_p->shape());
// Set the storage lattice tile shape to the tile shape of the
// axes of the parent lattice from which it is created.
// For the histogram axis, set the tile shape to the number of bins
// (which probably won't be too big, but could be !)
IPosition tileShape(storeLatticeShape.nelements(),1);
for (uInt i=1; i<tileShape.nelements(); i++) {
tileShape(i) = pInLattice_p->niceCursorShape()(displayAxes_p(i-1));
}
tileShape(0) = storeLatticeShape(0);
// Delete old histogram storage lattice
if (pStoreLattice_p != 0) delete pStoreLattice_p;
// Create storage lattice
uInt memory = HostInfo::memoryTotal()/1024;
Double useMemory = Double(memory)/10.0;
if (forceDisk_p) useMemory = 0.0;
pStoreLattice_p = new TempLattice<T>(TiledShape(storeLatticeShape,
tileShape), useMemory);
// Create collapser for LatticeApply
HistTiledCollapser<T> collapser(pStats_p, nBins_p);
LatticeHistProgress* pProgressMeter = 0;
if (showProgress_p) pProgressMeter = new LatticeHistProgress();
// This is the first output axis (there is only one in IH) getting
// collapsed values
Int newOutAxis = 0;
// Iterate through lattice and create histograms
// Output has to be a MaskedLattice, so make a writable SubLattice.
SubLattice<T> outLatt (*pStoreLattice_p, True);
LatticeApply<T,T>::tiledApply(outLatt, *pInLattice_p,
collapser, IPosition(cursorAxes_p),
newOutAxis, pProgressMeter);
if (pProgressMeter != 0) {
delete pProgressMeter;
pProgressMeter = 0;
}
}
template <class T>
Bool LatticeHistograms<T>::setInclude(Vector<T>& range,
Bool& noInclude,
const Vector<T>& include,
ostream& os)
//
// Take the user's data inclusion range
//
// Inputs:
// include Include range given by user. Zero length indicates
// no include range
// os Output stream for reporting
// Outputs:
// noInclude If True user did not give an include range
// range A pixel value selection range. Will be resized to
// zero length if both noInclude and noExclude are True
// Bool True if successfull, will fail if user tries to give too
// many values for includeB or excludeB, or tries to give
// values for both
{
noInclude = True;
range.resize(0);
if (include.nelements() == 0) {
;
} else if (include.nelements() == 1) {
range.resize(2);
range(0) = -abs(include(0));
range(1) = abs(include(0));
noInclude = False;
} else if (include.nelements() == 2) {
range.resize(2);
range(0) = min(include(0),include(1));
range(1) = max(include(0),include(1));
noInclude = False;
} else {
os << endl << "Too many elements for argument include" << endl;
return False;
}
return True;
}
template <class T>
String LatticeHistograms<T>::writeCoordinates(const IPosition& histPos) const
//
// Write pixel coordinates relative to parent lattice
//
{
ostringstream oss;
const Int nDisplayAxes = displayAxes_p.nelements();
if (nDisplayAxes > 0) {
for (Int j=0; j<nDisplayAxes; j++) {
oss << "Axis " << displayAxes_p(j) + 1 << "="
<< locHistInLattice(histPos,True)(j+1)+1;
if (j < nDisplayAxes-1) oss << ", ";
}
}
return String(oss);
}
template <class T>
Bool LatticeHistograms<T>::writeDispAxesValues (const String& coords,
PGPlotter& plotter,
Float nchar) const
{
// Fill the string stream with the name and value of each display axis
const Int nDisplayAxes = displayAxes_p.nelements();
if (nDisplayAxes > 0) {
// Write on plot
Vector<Float> box(8);
box = plotter.qtxt (0.0, 0.0, 0.0, 0.0, "X");
Float dx = box(3) - box(0);
const char* tLabel = coords.chars();
box = plotter.qtxt (0.0, 0.0, 0.0, 0.0, tLabel);
Float dy = box(5) - box(4);
Vector<Float> win = plotter.qwin();
Float mx = win(0) + dx;
Float my = win(3) + nchar*dy;
//
Int tbg = plotter.qtbg();
plotter.stbg(0);
plotter.ptxt (mx, my, 0.0, 0.0, tLabel);
plotter.stbg(tbg);
}
return True;
}
template <class T>
void LatticeHistograms<T>::setStream (ostream& os, Int oPrec)
{
os.fill(' ');
os.precision(oPrec);
os.setf(ios::scientific, ios::floatfield);
os.setf(ios::left, ios::adjustfield);
}
// HistTiledCollapser
template <class T>
HistTiledCollapser<T>::HistTiledCollapser(LatticeStatistics<T>* pStats, uInt nBins)
: pStats_p(pStats),
nBins_p(nBins)
{;}
template <class T>
HistTiledCollapser<T>::~HistTiledCollapser<T>() {}
template <class T>
void HistTiledCollapser<T>::init (uInt nOutPixelsPerCollapse)
{
AlwaysAssert (nOutPixelsPerCollapse == nBins_p, AipsError);
}
template <class T>
void HistTiledCollapser<T>::initAccumulator (uInt64 n1, uInt64 n3)
//
// pHist_p contains the histograms for each chunk
// It is T not uInt so we can handle Complex types
{
pHist_p = new Block<T>(nBins_p*n1*n3);
pHist_p->set(0);
//
n1_p = n1;
n3_p = n3;
}
template <class T>
void HistTiledCollapser<T>::process (
uInt index1, uInt index3, const T* pInData,
const Bool* pInMask, uInt dataIncr, uInt maskIncr,
uInt nrval, const IPosition& startPos, const IPosition&
) {
//
// Process the data in the current chunk. Everything in this
// chunk belongs in one output location in the accumulation
// lattices
// Fish out the min and max for this chunk of the data
// from the statistics object
typedef typename NumericTraits<T>::PrecisionType AccumType;
Vector<AccumType> stats;
pStats_p->getStats(stats, startPos, True);
ThrowIf(
stats.empty(),
"Failed to compute statistics, if you set a range you have likely excluded all valid pixels"
);
// Assignment from AccumType to T ok (e.g. Double to FLoat)
Vector<T> clip(2);
clip(0) = stats(LatticeStatsBase::MIN);
clip(1) = stats(LatticeStatsBase::MAX);
// Set histogram bin width
const T binWidth = LatticeHistSpecialize::setBinWidth(clip(0), clip(1), nBins_p);
// Fill histograms.
uInt offset = (nBins_p*index1) + (nBins_p*n1_p*index3);
LatticeHistSpecialize::process(
pInData, pInMask, pHist_p, clip,
binWidth, offset, nrval,
nBins_p, dataIncr, maskIncr
);
}
template <class T>
void HistTiledCollapser<T>::endAccumulator(Array<T>& result,
Array<Bool>& resultMask,
const IPosition& shape)
{
// Reshape arrays. The mask is always true. Any locations
// in the storage lattice for which there were no valid points
// will have the NPTS field set to zero. That is what
// we use to effectively mask it.
resultMask.resize(shape);
resultMask.set(True);
result.resize(shape);
//
Bool deleteRes;
T* res = result.getStorage (deleteRes);
T* resptr = res;
const T* histPtr = pHist_p->storage();
// The histogram storage lattice has the logical shape
// [nBins, n1, n3].
for (uInt k=0; k<nBins_p*n1_p*n3_p; k++) {
*resptr++ = *histPtr++;
}
result.putStorage (res, deleteRes);
delete pHist_p;
}
} //# NAMESPACE CASACORE - END
#endif
|