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