casacore
Loading...
Searching...
No Matches
LatticeApply.h
Go to the documentation of this file.
1// # LatticeApply.h: Optimally iterate through a Lattice and apply provided function object
2// # Copyright (C) 1997,1998,1999
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_LATTICEAPPLY_H
27#define LATTICES_LATTICEAPPLY_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Containers/Block.h>
32#include <casacore/scimath/Mathematics/NumericTraits.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # Forward Declarations
37template <class T, class U>
38class TiledCollapser;
39template <class T, class U>
40class LineCollapser;
41template <class T>
42class Lattice;
43template <class T>
44class MaskedLattice;
45class LatticeProgress;
46class IPosition;
47class LatticeRegion;
48
49// <summary>
50// Optimally iterate through a Lattice and apply provided function object
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=Lattice>MaskedLattice</linkto>
60// <li> <linkto class=LineCollapser>LineCollapser</linkto>
61// <li> <linkto class=TiledCollapser>TiledCollapser</linkto>
62// </prerequisite>
63
64// <synopsis>
65// This function iterates through a Lattice and applies a user given
66// function object to chunks along the specified axes. Usually the
67// function collapses the chunk to 1 or a few values (e.g. get min/max).
68// The result of the function is written into the output Lattice(s) at the
69// location of the collapsed chunk. The output lattice(s) must be supplied
70// with the correct shape. E.g. when a lattice with shape [nx,ny,nz] is
71// collapsed by calculating the mean of each y-line, the output lattice
72// has to have shape [nx,nz]. It is also possible to have output shape
73// [nx,1,nz], [1,nx,nz], [nx,nz,1] or even e.g. [nx,1,1,1,nz].
74// <p>
75// By specifying a region it is possible to apply the function object
76// to a subset of the lattice. Of course, the shape of the output lattice(s)
77// have to match the shape of the region.
78// <p>
79// The iteration is done in an optimal way. To keep memory usage down,
80// it caches as few tiles as possible.
81// There are 2 ways to iterate.
82// <ol>
83// <li> For some applications an entire line is needed. An example is
84// the calculation of the moment. The functions <src>lineApply</src>
85// and <src>lineMultiApply</src> can be used for that purpose.
86// Internally they use the
87// <linkto class=TiledLineStepper>TiledLineStepper</linkto>
88// navigator, so only a few tiles are kept in the cache.
89// <br> One can also think of applications where an entire plane (or cube)
90// is needed. This is not supported, but can be implemented when needed.
91// <li> Other applications do not care how the data are traversed,
92// making it possible to iterate tile by tile (which is optimal).
93// An example is the calculation of the minimum, maximum, mean of
94// a line, plane, etc..
95// For this purpose the function <src>tiledApply</src> can be used.
96// This function is faster and uses less memory than <src>lineApply</src>,
97// so whenever possible this one should be used. Another advantage of
98// this function is that it is possible to operate per line, plane, etc.
99// or even for the entire lattice.
100// </ol>
101// The user has to supply a function object derived from the abstract base
102// class <linkto class=LineCollapser>LineCollapser</linkto> or
103// <linkto class=TiledCollapser>TiledCollapser</linkto>, resp..
104// The <src>process</src> function in these classes has to process
105// the chunk of data passed in. The <src>nstepsDone</src> function
106// in these classes can be used to monitor the progress.
107// <p>
108// The class is Doubly templated. Ths first template type
109// is for the data type you are processing. The second type is
110// for what type you want the results of the processing assigned to.
111// For example, if you are computing sums of squares for statistical
112// purposes, you might use higher precision (Float->Double) for this.
113// No check is made that the template types are self-consistent.
114// </synopsis>
115
116// <example>
117// Collapse each line in the y-direction using my collapser function object.
118// <srcblock>
119// MyLineCollapser collapser;
120// PagedArray<Float> latticeIn("lattice.file");
121// IPosition shape = latticeIn.shape();
122// shape(1) = 1;
123// ArrayLattice<Double> latticeOut(shape);
124// LatticeApply<Float,Double>::lineApply (latticeOut, latticeIn, collapser, 1);
125// </srcblock>
126// </example>
127
128// <motivation>
129// This class makes it possible that a user can apply functions to
130// a lattice in an optimal way, without having to know all the details
131// of iterating through a lattice.
132// </motivation>
133
134// # <todo asof="1997/08/01">
135// # <li>
136// # </todo>
137
138template <class T, class U = T>
140 public:
141 // This function iterates line by line through an input lattice and applies
142 // a user supplied function object to each line along the specified axis.
143 // The scalar result of the function object is written into the output
144 // lattice at the location of the collapsed line. The output lattice must
145 // be supplied with the correct shape (the shape of the supplied region).
146 // The default region is the entire input lattice.
147 // <group>
148 static void lineApply(MaskedLattice<U>& latticeOut, const MaskedLattice<T>& latticeIn,
149 LineCollapser<T, U>& collapser, uInt collapseAxis,
150 LatticeProgress* tellProgress = 0);
151 static void lineApply(MaskedLattice<U>& latticeOut, const MaskedLattice<T>& latticeIn,
152 const LatticeRegion& region, LineCollapser<T, U>& collapser,
153 uInt collapseAxis, LatticeProgress* tellProgress = 0);
154 // </group>
155
156 // This function iterates line by line through an input lattice and applies
157 // a user supplied function object to each line along the specified axis.
158 // The vector result of the function object is written into the output
159 // lattices at the location of the collapsed line (1 value per lattice).
160 // The output lattices must be supplied with the correct shape (the shape
161 // of the supplied region).
162 // The default region is the entire input lattice.
163 // <group>
164 static void lineMultiApply(Block<MaskedLattice<U>*>& latticeOut,
165 const MaskedLattice<T>& latticeIn, LineCollapser<T, U>& collapser,
166 uInt collapseAxis, LatticeProgress* tellProgress = 0);
167
168 static void lineMultiApply(Block<MaskedLattice<U>*>& latticeOut,
169 const MaskedLattice<T>& latticeIn, const LatticeRegion& region,
170 LineCollapser<T, U>& collapser, uInt collapseAxis,
171 LatticeProgress* tellProgress = 0);
172 // </group>
173
174 // This function iterates tile by tile through an input lattice and applies
175 // a user supplied function object to each chunk along the specified axes.
176 // A chunk can be a line, plane, etc. which is determined by the argument
177 // <src>collapseAxes</src>. E.g. IPosition(2,1,2) means planes along
178 // axes 1 and 2 (thus y,z planes).
179 // The result of the function object is written into the output
180 // lattice at the location of the collapsed chunk. The output lattice must
181 // be supplied with the correct shape (the shape of the supplied region
182 // plus the number of values resulting from the collapse).
183 // The default region is the entire input lattice.
184 // <group>
185 static void tiledApply(MaskedLattice<U>& latticeOut, const MaskedLattice<T>& latticeIn,
186 TiledCollapser<T, U>& collapser, const IPosition& collapseAxes,
187 Int newOutAxis = -1, LatticeProgress* tellProgress = 0);
188 static void tiledApply(MaskedLattice<U>& latticeOut, const MaskedLattice<T>& latticeIn,
189 const LatticeRegion& region, TiledCollapser<T, U>& collapser,
190 const IPosition& collapseAxes, Int newOutAxis = -1,
191 LatticeProgress* tellProgress = 0);
192 // </group>
193
194 // This function iterates tile by tile through an input lattice and applies
195 // a user supplied function object to each chunk along the specified axes.
196 // A chunk can be a line, plane, etc. which is determined by the argument
197 // <src>collapseAxes</src>. E.g. IPosition(2,1,2) means planes along
198 // axes 1 and 2 (thus y,z planes).
199 // The result of the function object is written into the output
200 // lattices at the location of the collapsed chunk. The output lattices must
201 // be supplied with the correct shape (the shape of the supplied region).
202 // The default region is the entire input lattice.
203 // <note role=warning>
204 // These functions are only declared, but not implemented yet.
205 // Thus they cannot be used yet.
206 // </note>
207 // <group>
208 static void tiledMultiApply(Block<MaskedLattice<U>*>& latticeOut,
209 const MaskedLattice<T>& latticeIn, TiledCollapser<T, U>& collapser,
210 const IPosition& collapseAxes, LatticeProgress* tellProgress = 0);
211 static void tiledMultiApply(Block<MaskedLattice<U>*>& latticeOut,
212 const MaskedLattice<T>& latticeIn, const LatticeRegion& region,
213 TiledCollapser<T, U>& collapser, const IPosition& collapseAxes,
214 LatticeProgress* tellProgress = 0);
215 // </group>
216
217 private:
218 // Do some checks on the given arguments.
219 // It returns an IPosition with the same length as shapeOut.
220 // It contains a mapping of output to input axes. A value of -1
221 // indicates that the axis is new (to contain the collapse result).
222 // <br>Argument newOutAxis tells the output axis to store the results.
223 // -1 means that the function has to find it out itself; it takes the
224 // first axis with a length mismatching the corresponding input axis.
225 static IPosition prepare(const IPosition& shapeIn, const IPosition& shapeOut,
226 const IPosition& collapseAxes, Int newOutAxis);
227
228 static IPosition _chunkShape(uInt axis, const MaskedLattice<T>& latticeIn);
229};
230
231} // namespace casacore
232
233#ifndef CASACORE_NO_AUTO_TEMPLATES
234#include <casacore/lattices/LatticeMath/LatticeApply.tcc>
235#endif // # CASACORE_NO_AUTO_TEMPLATES
236#endif
static void tiledApply(MaskedLattice< U > &latticeOut, const MaskedLattice< T > &latticeIn, TiledCollapser< T, U > &collapser, const IPosition &collapseAxes, Int newOutAxis=-1, LatticeProgress *tellProgress=0)
This function iterates tile by tile through an input lattice and applies a user supplied function obj...
static void lineMultiApply(Block< MaskedLattice< U > * > &latticeOut, const MaskedLattice< T > &latticeIn, LineCollapser< T, U > &collapser, uInt collapseAxis, LatticeProgress *tellProgress=0)
This function iterates line by line through an input lattice and applies a user supplied function obj...
static void lineApply(MaskedLattice< U > &latticeOut, const MaskedLattice< T > &latticeIn, const LatticeRegion &region, LineCollapser< T, U > &collapser, uInt collapseAxis, LatticeProgress *tellProgress=0)
static void tiledMultiApply(Block< MaskedLattice< U > * > &latticeOut, const MaskedLattice< T > &latticeIn, TiledCollapser< T, U > &collapser, const IPosition &collapseAxes, LatticeProgress *tellProgress=0)
This function iterates tile by tile through an input lattice and applies a user supplied function obj...
static void lineApply(MaskedLattice< U > &latticeOut, const MaskedLattice< T > &latticeIn, LineCollapser< T, U > &collapser, uInt collapseAxis, LatticeProgress *tellProgress=0)
This function iterates line by line through an input lattice and applies a user supplied function obj...
static void lineMultiApply(Block< MaskedLattice< U > * > &latticeOut, const MaskedLattice< T > &latticeIn, const LatticeRegion &region, LineCollapser< T, U > &collapser, uInt collapseAxis, LatticeProgress *tellProgress=0)
static IPosition prepare(const IPosition &shapeIn, const IPosition &shapeOut, const IPosition &collapseAxes, Int newOutAxis)
Do some checks on the given arguments.
static IPosition _chunkShape(uInt axis, const MaskedLattice< T > &latticeIn)
static void tiledApply(MaskedLattice< U > &latticeOut, const MaskedLattice< T > &latticeIn, const LatticeRegion &region, TiledCollapser< T, U > &collapser, const IPosition &collapseAxes, Int newOutAxis=-1, LatticeProgress *tellProgress=0)
static void tiledMultiApply(Block< MaskedLattice< U > * > &latticeOut, const MaskedLattice< T > &latticeIn, const LatticeRegion &region, TiledCollapser< T, U > &collapser, const IPosition &collapseAxes, LatticeProgress *tellProgress=0)
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
int Int
Definition aipstype.h:48