casacore
Loading...
Searching...
No Matches
LatticeHistograms.h
Go to the documentation of this file.
1//# LatticeHistograms.h: generate histograms from a lattice
2//# Copyright (C) 1996,1997,1999,2000,2001
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef LATTICES_LATTICEHISTOGRAMS_H
27#define LATTICES_LATTICEHISTOGRAMS_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Arrays/ArrayFwd.h>
33#include <casacore/lattices/LatticeMath/TiledCollapser.h>
34#include <casacore/lattices/LatticeMath/LatticeStatistics.h>
35#include <casacore/lattices/LatticeMath/LatticeProgress.h>
36#include <casacore/casa/Logging/LogIO.h>
37#include <casacore/casa/System/PGPlotter.h>
38#include <casacore/scimath/Mathematics/NumericTraits.h>
39#include <casacore/casa/iosfwd.h>
40
41namespace casacore { //# NAMESPACE CASACORE - BEGIN
42
43//# Forward Declarations
44template <class T> class MaskedLattice;
45template <class T> class TempLattice;
46class IPosition;
47class PGPlotter;
48
49// <summary>
50// Displays histograms of regions from a lattice.
51// </summary>
52
53// <use visibility=export>
54
55// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
56// </reviewed>
57
58// <prerequisite>
59// <li> <linkto class=MaskedLattice>MaskedLattice</linkto>
60// </prerequisite>
61
62// <etymology>
63// This is a class designed to display histograms from MaskedLattices
64// </etymology>
65
66// <synopsis>
67// This class enable you to display and/or retrieve histograms evaluated over
68// specified regions from a MaskedLattice. The dimension of the region is arbitrary, but
69// the size of each dimension is always the size of the corresponding lattice axis.
70// The histograms are displayed as a function of location of the axes not
71// used to evaluate the histograms over. The axes which you evaluate the histograms
72// over are called the cursor axes, the others are called the display axes.
73//
74// For example, consider a lattice cube (call the axes xyz or [0,1,2]). You could
75// display histograms from xy planes (cursor axes [0,1]) as a function of z (display
76// axes [2]). Or you could retrieve histograms from the z axis (cursor axes [2])
77// for each [x,y] location (display axes [0,1]).
78//
79// This class generates a "storage lattice" into which it writes the histograms.
80// It is from this storage lattice that the plotting and retrieval
81// arrays are drawn. The storage lattice is either in core or on disk
82// depending upon its size (if > 10% of memory given by .aipsrc system.resources.memory
83// then it goes into a disk-based PagedArray). If on disk, the
84// storage lattice is deleted when the <src>LatticeHistograms</src>
85// object destructs.
86//
87//
88// <note role=tip>
89// Note that for complex lattices, real and imaginary are treated independently.
90// They are binned and plotted separately.
91// </note>
92//
93// <note role=tip>
94// If you ignore return error statuses from the functions that set the
95// state of the class, the internal status of the class is set to bad.
96// This means it will just keep on returning error conditions until you
97// explicitly recover the situation. A message describing the last
98// error condition can be recovered with function errorMessage.
99
100// </note>
101// </synopsis>
102
103// <example>
104// <srcBlock>
106//
107// PagedImage<Float> inImage(inName);
108//
110//
111// LogOrigin or("myClass", "myFunction(...)", WHERE);
112// LogIO os(or);
113// ImageHistograms<Float> histo(inImage, os);
114//
116//
117// Vector<Int> cursorAxes(2)
118// cursorAxes(0) = 1;
119// cursorAxes(1) = 2;
120// if (!histo.setAxes(cursorAxes)) return 1;
121//
123//
124// if (!histo.setList(True)) return 1;
125// String device = "/xs";
126// Vector<Int> nxy(2);
127// nxy(0) = 3;
128// nxy(1) = 3;
129// if (!histo.setPlotting(device, nxy)) return 1;
130//
132//
133// if (!histo.display ()) return 1;
134//
136//
137// Array<Float> values, counts;
138// if (!histo.getHistograms(values, counts)) return 1;
139//
140// </srcBlock>
141// In this example, a <src>PagedImage</src> is constructed. We set the cursor axes
142// to be the y and z axes so we make a histogram of each yz plane as a function
143// of x location on the PGPLOT device "/xs" with 9 subplots per page.
144// After the plotting we also retrieve the histograms into an array.
145// </example>
146
147// <motivation>
148// The generation of histograms from an image is a basic and necessary capability.
149// </motivation>
150//
151// <todo asof="2000/04/04">
152// <li> Make ascii listing of histograms as well as plots if desired
153// </todo>
154//
155
156
157template <class T> class LatticeHistograms
158{
159public:
160
161// Constructor takes the MaskedLattice and a <src>LogIO</src> object for logging.
162// You can also specify whether you want to see progress meters or not.
163// You can force the storage lattice to be disk based, otherwise
164// the decision for core or disk is taken for you.
166 LogIO& os,
167 Bool showProgress=True,
168 Bool forceDisk=False);
169
170// Constructor takes the MaskedLattice only. In the absence of a logger you get no messages.
171// This includes error messages and potential listing of statistics.
172// You can specify whether you want to see progress meters or not.
173// You can force the storage lattice to be disk based, otherwise
174// the decision for core or disk is taken for you.
176 Bool showProgress=True,
177 Bool forceDisk=False);
178
179// Copy constructor (copy semantics)
181
182// Destructor
184
185// Assignment operator (copy semantics)
187
188// Set the cursor axes (0 relative). A return value of <src>False</src>
189// indicates you have asked for an invalid axis or that the internal
190// status of the class is bad. The default state of the class is to set
191// the cursor axes to all axes in the lattice.
192 Bool setAxes (const Vector<Int>& cursorAxes);
193
194// Set the number of bins for the histogram. Note that the bin width is
195// worked out for each histogram separately from the data minimum and maximum.
196// The default state of the class is to set 25 bins. A return value of <src>False</src>
197// indicates you gave a non-positive bin width or that the internal status of the
198// class is bad.
199 Bool setNBins (const uInt& nBins);
200
201// Specify a pixel intensity range for which all pixels in that range are
202// included. A vector of length 1 for <src>include</src> means that the
203// range will be set to <src>-abs(include(0))</src> to <src>abs(include(0))</src>.
204// A return value of <src>False</src> indicates that the internal
205// status of the class is bad. If you don't call this function, the default
206// state of the class is to include all pixels.
208
209// Specify that a Gaussian overlay should be plotted on the histogram. This
210// Gaussian has the same mean and standard deviation as the data that were
211// binned, and the same integral as the histogram. A return value of <src>False</src>
212// indicates that the internal status of the class is bad. The default state of
213// the class is to not draw a Gaussian overlay.
214 Bool setGaussian (const Bool& doGauss);
215
216// Specify the form of the histogram. It can be plotted linearly or
217// logarithmically, and cumulatively or non-cumulatively. A return value
218// of <src>False</src> indicates that the internal status of the class is bad.
219// The default state of the class is to draw the histograms linearly and
220// non-cumulatively.
221 Bool setForm (const Bool& doLog, const Bool& doCumu);
222
223// This function allows you to control whether some statistics of the
224// data that contributed to the histogram are written to the output
225// stream. A return value of <src>False</src> indicates that the internal
226// status of the class is bad. The default state of the class is to not
227// list statistics.
228 Bool setStatsList(const Bool& doList);
229
230// This function sets the name of the PGPLOT plotting device and the number of
231// subplots in x and y per page. If you set <src>plotter</src> but offer
232// a zero length array for <src>nxy</src> then <src>nxy</src> is set
233// to [1,1]. A return value of <src>False</src> indicates invalid
234// plotting arguments or that the internal status of the class is bad. If you
235// don't call this function, the default state of the class is to not set
236// a plotting device.
238 const Vector<Int>& nxy);
239
240// Display the histograms by plotting them. A return value of <src>False</src>
241// indicates an invalid plotting device, or that the internal status of the class is bad.
242// If you don't call this function you won't see any histograms.
244
245// CLose the plotter
247
248// Return the display axes
250
251// This function retrieves the histograms into <src>Array</src>. The shape of the first
252// dimension of this array is the number of bins. The rest of the shape of the
253// array is the shape of the display axes (e.g. if the shape of the lattice is
254// [nx,ny,nz] and you ask for histograms of the y axis the shape of the returned
255// array would be [nbins,nx,nz]. The histograms are retrieved in the form
256// specified by the <src>setForm</src> function. The arrays are resized internally.
257// A return value of <src>False</src> indicates that the internal status of the class is bad.
259
260 // in this version, the set of stats for each histogram is also returned. The
261 // stats array has the shape of the display axes.
262 Bool getHistograms (Array<T>& values, Array<T>& counts, Array<Vector<T> >& stats);
263
264
265// This function retrieves the histogram at the specified location
266// into <src>Vectors</src>. The histogram is retrieved in the form
267// specified by the <src>setForm</src> function. The vectors are resized
268// internally. If <src>posInLattice=True</src> then the location is a
269// location in the input lattice. Any positions on the display axes
270// are ignored. Otherwise, you should just give locations for
271// the display axes only. A return value of <src>False</src> indicates that
272// the internal status of the class is bad.
274 Vector<T>& counts,
275 const IPosition& pos,
276 const Bool posInLattice=False);
277
278// Reset argument error condition. If you specify invalid arguments to
279// one of the above <src>set</src> functions, an internal flag will be set which will
280// prevent the work functions from doing anything (should you have chosen
281// to ignore the Boolean return values of the <src>set</src> functions).
282// This function allows you to reset that internal state to good.
284
285// Recover last error message
286 String errorMessage() const {return error_p;};
287
288// Set a MaskedLattice. A return value of <src>False</src> indicates the
289// lattice had an invalid type or that the internal status of the class is bad.
291
292// These things are protected only so that they are available to ImageHistograms
293// which inherits from LatticeHistograms
294
295protected:
300
301// Given a location in the histogram storage lattice, convert those locations on the
302// non-histogram axis (the first one) relative to the parent or current lattice
303 IPosition locHistInLattice (const IPosition& histPosition,
304 Bool relativeToParent=True) const;
305
306private:
307
308// A useful typedef
310
322
323
324// Convert a <tt>T</tt> to a <tt>Float</tt> for plotting
325 static Float convertT (const T value) {return Float(std::real(value));};
326
327// Convert a <tt>Float</tt> (from plotting) to a <tt>T</tt>
328 static T convertF (const Float value) {return T(value);};
329
330// Display histograms as a function of display axis
332
333// Display one histogram
334 Bool displayOneHistogram (const T&linearSum,
335 const T&linearYMax,
336 const IPosition& histPos,
337 const Vector<T> &stats,
338 const Vector<T>& values,
339 const Vector<T>& counts,
340 PGPlotter& plotter);
341
342
343// Fish out and convert to the appropriate form one histogram from the
344// storage lattice
345 void extractOneHistogram (T& linearSum,
346 T& linearYMax,
347 Vector<T>& values,
348 Vector<T>& counts,
349 const Vector<T>& stats,
350 const Vector<T>& intCounts);
351
352// Iterate through the lattice and generate the histogram accumulation lattice
354
355// Get the statistics from the statistics object for the current
356// location of either the input lattice, or the histogram storage lattice
358 const IPosition &pos) const;
359
360// List statistics
361 void listStatistics(LogIO& os, const Vector<T>& stats, T binWidth);
362
363
364// Fill histograms storage lattice
366
367// Create and fill statistics object
369
370// Check/set include pixel range
372 Bool& noInclude,
373 const Vector<T>& include,
374 ostream& os);
375
376// Set stream attributes
377 void setStream (ostream& os, Int oPrec);
378
379// Make a string with pixel coordinates of display axes. This function
380// is over-ridden by ImageHistograms which inherits from LatticeHistograms.
381 virtual String writeCoordinates(const IPosition& histPos) const;
382
383// Write values of display axes on plots
385 PGPlotter& plotter,
386 Float nchar) const;
387};
388
389
390
391// <summary> Generate histograms, tile by tile, from a masked lattice </summary>
392//
393// <use visibility=export>
394//
395// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
396// </reviewed>
397//
398// <prerequisite>
399// <li> <linkto class=LatticeApply>LatticeApply</linkto>
400// <li> <linkto class=TiledCollapser>TiledCollapser</linkto>
401// </prerequisite>
402//
403// <etymology>
404// This class is used by <src>LatticeHistograms</src> to generate
405// histograms from an input <src>MaskedLattice</src>.
406// The input lattice is iterated through in tile-sized chunks
407// and fed to an object of this class.
408// </etymology>
409//
410// <synopsis>
411// <src>HistTiledCollapser</src> is derived from <src>TiledCollapser</src> which
412// is a base class used to define methods. Objects of this base class are
413// used by <src>LatticeApply</src> functions. In this particular case,
414// we are interested in <src>LatticeApply::tiledApply</src>. This function iterates
415// through a <src>MaskedLattice</src> and allows you to collapse one or more
416// axes, computing some values from it, and placing those values into
417// an output <src>MaskedLattice</src>. It iterates through the input
418// lattice in optimal tile-sized chunks. <src>LatticeHistograms</src>
419// uses a <src>HistTiledCollapser</src> object which it gives to
420// <src>LatticeApply::tiledApply</src> for digestion. After it has
421// done its work, <src>LatticeHistograms</src> then accesses the output
422// <src>Lattice</src> that it made.
423// </synopsis>
424//
425// <example>
426// <srcblock>
428//
429// HistTiledCollapser<T> collapser(pStats, nBins_p);
430//
433//
434// Int newOutAxis = 0;
435//
438//
439// LatticeApply<T>::tiledApply(outLattice, inLattice,
440// collapser, collapseAxes,
441// newOutAxis);
442//
443// </srcblock>
444// In this example, a collapser is made and passed to LatticeApply.
445// Afterwards, the output Lattice is available for use.
446// The Lattices must all be the correct shapes on input to tiledApply
447// </example>
448//
449// <motivation>
450// The LatticeApply classes enable the ugly details of optimal
451// Lattice iteration to be hidden from the user.
452// </motivation>
453//
454// <todo asof="1998/05/10">
455// <li>
456// </todo>
457
458template <class T>
460{
461
462public:
463// Constructor
465
467
468// Initialize process, making some checks
469 virtual void init (uInt nOutPixelsPerCollapse);
470
471// Initialize the accumulator
472 virtual void initAccumulator (uInt64 n1, uInt64 n3);
473
474// Process the data in the current chunk.
475 virtual void process (
476 uInt accumIndex1,
477 uInt accumIndex3,
478 const T* inData,
479 const Bool* inMask,
480 uInt inDataIncr,
481 uInt inMaskIncr,
482 uInt nrval,
483 const IPosition& startPos,
484 const IPosition& shape
485 );
486
487// End the accumulation process and return the result arrays
488 virtual void endAccumulator(Array<T>& result,
489 Array<Bool>& resultMask,
490 const IPosition& shape);
491
492// Can handle null mask
493 virtual Bool canHandleNullMask() const {return True;};
494
495private:
501};
502
503
504
505} //# NAMESPACE CASACORE - END
506
507#ifndef CASACORE_NO_AUTO_TEMPLATES
508#include <casacore/lattices/LatticeMath/LatticeHistograms.tcc>
509#endif //# CASACORE_NO_AUTO_TEMPLATES
510#endif
simple 1-D array
Definition Block.h:198
Generate histograms, tile by tile, from a masked lattice.
LatticeStatistics< T > * pStats_p
virtual void process(uInt accumIndex1, uInt accumIndex3, const T *inData, const Bool *inMask, uInt inDataIncr, uInt inMaskIncr, uInt nrval, const IPosition &startPos, const IPosition &shape)
Process the data in the current chunk.
virtual Bool canHandleNullMask() const
Can handle null mask.
HistTiledCollapser(LatticeStatistics< T > *pStats, uInt nBins)
Constructor.
virtual void init(uInt nOutPixelsPerCollapse)
Initialize process, making some checks.
virtual void initAccumulator(uInt64 n1, uInt64 n3)
Initialize the accumulator.
virtual void endAccumulator(Array< T > &result, Array< Bool > &resultMask, const IPosition &shape)
End the accumulation process and return the result arrays
Bool setNBins(const uInt &nBins)
Set the number of bins for the histogram.
LatticeStatistics< T > * pStats_p
Bool setInclude(Vector< T > &range, Bool &noInclude, const Vector< T > &include, ostream &os)
Check/set include pixel range.
void setStream(ostream &os, Int oPrec)
Set stream attributes.
LatticeHistograms< T > & operator=(const LatticeHistograms< T > &other)
Assignment operator (copy semantics)
Bool writeDispAxesValues(const String &coords, PGPlotter &plotter, Float nchar) const
Write values of display axes on plots.
Bool 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 one histogram.
virtual String writeCoordinates(const IPosition &histPos) const
Make a string with pixel coordinates of display axes.
Bool displayHistograms()
Display histograms as a function of display axis.
Bool setAxes(const Vector< Int > &cursorAxes)
Set the cursor axes (0 relative).
Bool setStatsList(const Bool &doList)
This function allows you to control whether some statistics of the data that contributed to the histo...
Bool makeStatistics()
Create and fill statistics object.
void getStatistics(Vector< T > &stats, const IPosition &pos) const
Get the statistics from the statistics object for the current location of either the input lattice,...
void makeHistograms()
Fill histograms storage lattice.
virtual ~LatticeHistograms()
Destructor.
String errorMessage() const
Recover last error message.
LatticeHistograms(const MaskedLattice< T > &lattice, Bool showProgress=True, Bool forceDisk=False)
Constructor takes the MaskedLattice only.
LatticeHistograms(const MaskedLattice< T > &lattice, LogIO &os, Bool showProgress=True, Bool forceDisk=False)
Constructor takes the MaskedLattice and a LogIO object for logging.
void closePlotting()
CLose the plotter.
void resetError()
Reset argument error condition.
Bool setForm(const Bool &doLog, const Bool &doCumu)
Specify the form of the histogram.
Bool generateStorageLattice()
Iterate through the lattice and generate the histogram accumulation lattice.
Bool setGaussian(const Bool &doGauss)
Specify that a Gaussian overlay should be plotted on the histogram.
NumericTraits< T >::PrecisionType AccumType
A useful typedef.
IPosition locHistInLattice(const IPosition &histPosition, Bool relativeToParent=True) const
Given a location in the histogram storage lattice, convert those locations on the non-histogram axis ...
static T convertF(const Float value)
Convert a Float (from plotting) to a T
void listStatistics(LogIO &os, const Vector< T > &stats, T binWidth)
List statistics.
Bool setNewLattice(const MaskedLattice< T > &lattice)
Set a MaskedLattice.
const MaskedLattice< T > * pInLattice_p
Bool getHistograms(Array< T > &values, Array< T > &counts)
This function retrieves the histograms into Array.
Bool display()
Display the histograms by plotting them.
LogIO os_p
These things are protected only so that they are available to ImageHistograms which inherits from Lat...
static Float convertT(const T value)
Convert a T to a Float for plotting.
Bool setPlotting(PGPlotter &plotter, const Vector< Int > &nxy)
This function sets the name of the PGPLOT plotting device and the number of subplots in x and y per p...
Bool setIncludeRange(const Vector< T > &include)
Specify a pixel intensity range for which all pixels in that range are included.
Bool getHistogram(Vector< T > &values, Vector< T > &counts, const IPosition &pos, const Bool posInLattice=False)
This function retrieves the histogram at the specified location into Vectors.
Vector< Int > displayAxes() const
Return the display axes.
TempLattice< T > * pStoreLattice_p
void extractOneHistogram(T &linearSum, T &linearYMax, Vector< T > &values, Vector< T > &counts, const Vector< T > &stats, const Vector< T > &intCounts)
Fish out and convert to the appropriate form one histogram from the storage lattice.
Bool getHistograms(Array< T > &values, Array< T > &counts, Array< Vector< T > > &stats)
in this version, the set of stats for each histogram is also returned.
LatticeHistograms(const LatticeHistograms< T > &other)
Copy constructor (copy semantics)
Char PrecisionType
Higher precision type (Float->Double)
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape.
Definition ExprNode.h:1991
float Float
Definition aipstype.h:52
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
const Bool True
Definition aipstype.h:41
unsigned long long uInt64
Definition aipsxtype.h:37