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// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Arrays/ArrayFwd.h>
32#include <casacore/lattices/LatticeMath/TiledCollapser.h>
33#include <casacore/lattices/LatticeMath/LatticeStatistics.h>
34#include <casacore/lattices/LatticeMath/LatticeProgress.h>
35#include <casacore/casa/Logging/LogIO.h>
36#include <casacore/casa/System/PGPlotter.h>
37#include <casacore/scimath/Mathematics/NumericTraits.h>
38#include <casacore/casa/iosfwd.h>
39
40namespace casacore { // # NAMESPACE CASACORE - BEGIN
41
42// # Forward Declarations
43template <class T>
44class MaskedLattice;
45template <class T>
46class TempLattice;
47class IPosition;
48class PGPlotter;
49
50// <summary>
51// Displays histograms of regions from a lattice.
52// </summary>
53
54// <use visibility=export>
55
56// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
57// </reviewed>
58
59// <prerequisite>
60// <li> <linkto class=MaskedLattice>MaskedLattice</linkto>
61// </prerequisite>
62
63// <etymology>
64// This is a class designed to display histograms from MaskedLattices
65// </etymology>
66
67// <synopsis>
68// This class enable you to display and/or retrieve histograms evaluated over
69// specified regions from a MaskedLattice. The dimension of the region is arbitrary, but
70// the size of each dimension is always the size of the corresponding lattice axis.
71// The histograms are displayed as a function of location of the axes not
72// used to evaluate the histograms over. The axes which you evaluate the histograms
73// over are called the cursor axes, the others are called the display axes.
74//
75// For example, consider a lattice cube (call the axes xyz or [0,1,2]). You could
76// display histograms from xy planes (cursor axes [0,1]) as a function of z (display
77// axes [2]). Or you could retrieve histograms from the z axis (cursor axes [2])
78// for each [x,y] location (display axes [0,1]).
79//
80// This class generates a "storage lattice" into which it writes the histograms.
81// It is from this storage lattice that the plotting and retrieval
82// arrays are drawn. The storage lattice is either in core or on disk
83// depending upon its size (if > 10% of memory given by .aipsrc system.resources.memory
84// then it goes into a disk-based PagedArray). If on disk, the
85// storage lattice is deleted when the <src>LatticeHistograms</src>
86// object destructs.
87//
88//
89// <note role=tip>
90// Note that for complex lattices, real and imaginary are treated independently.
91// They are binned and plotted separately.
92// </note>
93//
94// <note role=tip>
95// If you ignore return error statuses from the functions that set the
96// state of the class, the internal status of the class is set to bad.
97// This means it will just keep on returning error conditions until you
98// explicitly recover the situation. A message describing the last
99// error condition can be recovered with function errorMessage.
100
101// </note>
102// </synopsis>
103
104// <example>
105// <srcBlock>
107//
108// PagedImage<Float> inImage(inName);
109//
111//
112// LogOrigin or("myClass", "myFunction(...)", WHERE);
113// LogIO os(or);
114// ImageHistograms<Float> histo(inImage, os);
115//
117//
118// Vector<Int> cursorAxes(2)
119// cursorAxes(0) = 1;
120// cursorAxes(1) = 2;
121// if (!histo.setAxes(cursorAxes)) return 1;
122//
124//
125// if (!histo.setList(True)) return 1;
126// String device = "/xs";
127// Vector<Int> nxy(2);
128// nxy(0) = 3;
129// nxy(1) = 3;
130// if (!histo.setPlotting(device, nxy)) return 1;
131//
133//
134// if (!histo.display ()) return 1;
135//
137//
138// Array<Float> values, counts;
139// if (!histo.getHistograms(values, counts)) return 1;
140//
141// </srcBlock>
142// In this example, a <src>PagedImage</src> is constructed. We set the cursor axes
143// to be the y and z axes so we make a histogram of each yz plane as a function
144// of x location on the PGPLOT device "/xs" with 9 subplots per page.
145// After the plotting we also retrieve the histograms into an array.
146// </example>
147
148// <motivation>
149// The generation of histograms from an image is a basic and necessary capability.
150// </motivation>
151//
152// <todo asof="2000/04/04">
153// <li> Make ascii listing of histograms as well as plots if desired
154// </todo>
155//
156
157template <class T>
159 public:
160 // Constructor takes the MaskedLattice and a <src>LogIO</src> object for logging.
161 // You can also specify whether you want to see progress meters or not.
162 // You can force the storage lattice to be disk based, otherwise
163 // the decision for core or disk is taken for you.
164 LatticeHistograms(const MaskedLattice<T>& lattice, LogIO& os, Bool showProgress = True,
165 Bool forceDisk = False);
166
167 // Constructor takes the MaskedLattice only. In the absence of a logger you get no messages.
168 // This includes error messages and potential listing of statistics.
169 // You can specify whether you want to see progress meters or not.
170 // You can force the storage lattice to be disk based, otherwise
171 // the decision for core or disk is taken for you.
172 LatticeHistograms(const MaskedLattice<T>& lattice, Bool showProgress = True,
173 Bool forceDisk = False);
174
175 // Copy constructor (copy semantics)
177
178 // Destructor
180
181 // Assignment operator (copy semantics)
183
184 // Set the cursor axes (0 relative). A return value of <src>False</src>
185 // indicates you have asked for an invalid axis or that the internal
186 // status of the class is bad. The default state of the class is to set
187 // the cursor axes to all axes in the lattice.
188 Bool setAxes(const Vector<Int>& cursorAxes);
189
190 // Set the number of bins for the histogram. Note that the bin width is
191 // worked out for each histogram separately from the data minimum and maximum.
192 // The default state of the class is to set 25 bins. A return value of <src>False</src>
193 // indicates you gave a non-positive bin width or that the internal status of the
194 // class is bad.
195 Bool setNBins(const uInt& nBins);
196
197 // Specify a pixel intensity range for which all pixels in that range are
198 // included. A vector of length 1 for <src>include</src> means that the
199 // range will be set to <src>-abs(include(0))</src> to <src>abs(include(0))</src>.
200 // A return value of <src>False</src> indicates that the internal
201 // status of the class is bad. If you don't call this function, the default
202 // state of the class is to include all pixels.
204
205 // Specify that a Gaussian overlay should be plotted on the histogram. This
206 // Gaussian has the same mean and standard deviation as the data that were
207 // binned, and the same integral as the histogram. A return value of <src>False</src>
208 // indicates that the internal status of the class is bad. The default state of
209 // the class is to not draw a Gaussian overlay.
210 Bool setGaussian(const Bool& doGauss);
211
212 // Specify the form of the histogram. It can be plotted linearly or
213 // logarithmically, and cumulatively or non-cumulatively. A return value
214 // of <src>False</src> indicates that the internal status of the class is bad.
215 // The default state of the class is to draw the histograms linearly and
216 // non-cumulatively.
217 Bool setForm(const Bool& doLog, const Bool& doCumu);
218
219 // This function allows you to control whether some statistics of the
220 // data that contributed to the histogram are written to the output
221 // stream. A return value of <src>False</src> indicates that the internal
222 // status of the class is bad. The default state of the class is to not
223 // list statistics.
224 Bool setStatsList(const Bool& doList);
225
226 // This function sets the name of the PGPLOT plotting device and the number of
227 // subplots in x and y per page. If you set <src>plotter</src> but offer
228 // a zero length array for <src>nxy</src> then <src>nxy</src> is set
229 // to [1,1]. A return value of <src>False</src> indicates invalid
230 // plotting arguments or that the internal status of the class is bad. If you
231 // don't call this function, the default state of the class is to not set
232 // a plotting device.
233 Bool setPlotting(PGPlotter& plotter, const Vector<Int>& nxy);
234
235 // Display the histograms by plotting them. A return value of <src>False</src>
236 // indicates an invalid plotting device, or that the internal status of the class is bad.
237 // If you don't call this function you won't see any histograms.
239
240 // CLose the plotter
242
243 // Return the display axes
245
246 // This function retrieves the histograms into <src>Array</src>. The shape of the first
247 // dimension of this array is the number of bins. The rest of the shape of the
248 // array is the shape of the display axes (e.g. if the shape of the lattice is
249 // [nx,ny,nz] and you ask for histograms of the y axis the shape of the returned
250 // array would be [nbins,nx,nz]. The histograms are retrieved in the form
251 // specified by the <src>setForm</src> function. The arrays are resized internally.
252 // A return value of <src>False</src> indicates that the internal status of the class is bad.
254
255 // in this version, the set of stats for each histogram is also returned. The
256 // stats array has the shape of the display axes.
257 Bool getHistograms(Array<T>& values, Array<T>& counts, Array<Vector<T>>& stats);
258
259 // This function retrieves the histogram at the specified location
260 // into <src>Vectors</src>. The histogram is retrieved in the form
261 // specified by the <src>setForm</src> function. The vectors are resized
262 // internally. If <src>posInLattice=True</src> then the location is a
263 // location in the input lattice. Any positions on the display axes
264 // are ignored. Otherwise, you should just give locations for
265 // the display axes only. A return value of <src>False</src> indicates that
266 // the internal status of the class is bad.
267 Bool getHistogram(Vector<T>& values, Vector<T>& counts, const IPosition& pos,
268 const Bool posInLattice = False);
269
270 // Reset argument error condition. If you specify invalid arguments to
271 // one of the above <src>set</src> functions, an internal flag will be set which will
272 // prevent the work functions from doing anything (should you have chosen
273 // to ignore the Boolean return values of the <src>set</src> functions).
274 // This function allows you to reset that internal state to good.
276
277 // Recover last error message
278 String errorMessage() const { return error_p; };
279
280 // Set a MaskedLattice. A return value of <src>False</src> indicates the
281 // lattice had an invalid type or that the internal status of the class is bad.
283
284 // These things are protected only so that they are available to ImageHistograms
285 // which inherits from LatticeHistograms
286
287 protected:
292
293 // Given a location in the histogram storage lattice, convert those locations on the
294 // non-histogram axis (the first one) relative to the parent or current lattice
295 IPosition locHistInLattice(const IPosition& histPosition, Bool relativeToParent = True) const;
296
297 private:
298 // A useful typedef
300
312
313 // Convert a <tt>T</tt> to a <tt>Float</tt> for plotting
314 static Float convertT(const T value) { return Float(std::real(value)); };
315
316 // Convert a <tt>Float</tt> (from plotting) to a <tt>T</tt>
317 static T convertF(const Float value) { return T(value); };
318
319 // Display histograms as a function of display axis
321
322 // Display one histogram
323 Bool displayOneHistogram(const T& linearSum, const T& linearYMax, const IPosition& histPos,
324 const Vector<T>& stats, const Vector<T>& values, const Vector<T>& counts,
325 PGPlotter& plotter);
326
327 // Fish out and convert to the appropriate form one histogram from the
328 // storage lattice
329 void extractOneHistogram(T& linearSum, T& linearYMax, Vector<T>& values, Vector<T>& counts,
330 const Vector<T>& stats, const Vector<T>& intCounts);
331
332 // Iterate through the lattice and generate the histogram accumulation lattice
334
335 // Get the statistics from the statistics object for the current
336 // location of either the input lattice, or the histogram storage lattice
337 void getStatistics(Vector<T>& stats, const IPosition& pos) const;
338
339 // List statistics
340 void listStatistics(LogIO& os, const Vector<T>& stats, T binWidth);
341
342 // Fill histograms storage lattice
344
345 // Create and fill statistics object
347
348 // Check/set include pixel range
349 Bool setInclude(Vector<T>& range, Bool& noInclude, const Vector<T>& include, std::ostream& os);
350
351 // Set stream attributes
352 void setStream(std::ostream& os, Int oPrec);
353
354 // Make a string with pixel coordinates of display axes. This function
355 // is over-ridden by ImageHistograms which inherits from LatticeHistograms.
356 virtual String writeCoordinates(const IPosition& histPos) const;
357
358 // Write values of display axes on plots
359 Bool writeDispAxesValues(const String& coords, PGPlotter& plotter, Float nchar) const;
360};
361
362// <summary> Generate histograms, tile by tile, from a masked lattice </summary>
363//
364// <use visibility=export>
365//
366// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
367// </reviewed>
368//
369// <prerequisite>
370// <li> <linkto class=LatticeApply>LatticeApply</linkto>
371// <li> <linkto class=TiledCollapser>TiledCollapser</linkto>
372// </prerequisite>
373//
374// <etymology>
375// This class is used by <src>LatticeHistograms</src> to generate
376// histograms from an input <src>MaskedLattice</src>.
377// The input lattice is iterated through in tile-sized chunks
378// and fed to an object of this class.
379// </etymology>
380//
381// <synopsis>
382// <src>HistTiledCollapser</src> is derived from <src>TiledCollapser</src> which
383// is a base class used to define methods. Objects of this base class are
384// used by <src>LatticeApply</src> functions. In this particular case,
385// we are interested in <src>LatticeApply::tiledApply</src>. This function iterates
386// through a <src>MaskedLattice</src> and allows you to collapse one or more
387// axes, computing some values from it, and placing those values into
388// an output <src>MaskedLattice</src>. It iterates through the input
389// lattice in optimal tile-sized chunks. <src>LatticeHistograms</src>
390// uses a <src>HistTiledCollapser</src> object which it gives to
391// <src>LatticeApply::tiledApply</src> for digestion. After it has
392// done its work, <src>LatticeHistograms</src> then accesses the output
393// <src>Lattice</src> that it made.
394// </synopsis>
395//
396// <example>
397// <srcblock>
399//
400// HistTiledCollapser<T> collapser(pStats, nBins_p);
401//
404//
405// Int newOutAxis = 0;
406//
409//
410// LatticeApply<T>::tiledApply(outLattice, inLattice,
411// collapser, collapseAxes,
412// newOutAxis);
413//
414// </srcblock>
415// In this example, a collapser is made and passed to LatticeApply.
416// Afterwards, the output Lattice is available for use.
417// The Lattices must all be the correct shapes on input to tiledApply
418// </example>
419//
420// <motivation>
421// The LatticeApply classes enable the ugly details of optimal
422// Lattice iteration to be hidden from the user.
423// </motivation>
424//
425// <todo asof="1998/05/10">
426// <li>
427// </todo>
428
429template <class T>
430class HistTiledCollapser : public TiledCollapser<T, T> {
431 public:
432 // Constructor
434
436
437 // Initialize process, making some checks
438 virtual void init(uInt nOutPixelsPerCollapse);
439
440 // Initialize the accumulator
441 virtual void initAccumulator(uInt64 n1, uInt64 n3);
442
443 // Process the data in the current chunk.
444 virtual void process(uInt accumIndex1, uInt accumIndex3, const T* inData, const Bool* inMask,
445 uInt inDataIncr, uInt inMaskIncr, uInt nrval, const IPosition& startPos,
446 const IPosition& shape);
447
448 // End the accumulation process and return the result arrays
449 virtual void endAccumulator(Array<T>& result, Array<Bool>& resultMask, const IPosition& shape);
450
451 // Can handle null mask
452 virtual Bool canHandleNullMask() const { return True; };
453
454 private:
460};
461
462} // namespace casacore
463
464#ifndef CASACORE_NO_AUTO_TEMPLATES
465#include <casacore/lattices/LatticeMath/LatticeHistograms.tcc>
466#endif // # CASACORE_NO_AUTO_TEMPLATES
467#endif
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
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).
void setStream(std::ostream &os, Int oPrec)
Set stream attributes.
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.
Bool setInclude(Vector< T > &range, Bool &noInclude, const Vector< T > &include, std::ostream &os)
Check/set include pixel range.
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:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
IPosition shape(const RecordFieldId &) const
Get the actual shape of this field.
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
const Bool True
Definition aipstype.h:41
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
unsigned long long uInt64
Definition aipsxtype.h:37