casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MatrixCleaner.h
Go to the documentation of this file.
1 //# Cleaner.h: this defines Cleaner a class for doing convolution
2 //# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003
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: aips2-request@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 //#
27 //# $Id: LatticeCleaner.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $
28 
29 #ifndef SYNTHESIS_MATRIXCLEANER_H
30 #define SYNTHESIS_MATRIXCLEANER_H
31 
32 //# Includes
33 #include <casa/aips.h>
34 #include <casa/Quanta/Quantum.h>
36 #include <casa/Arrays/IPosition.h>
37 #include <casa/Arrays/Vector.h>
38 #include <casa/Containers/Block.h>
40 
41 namespace casacore{
42 
43 template <class T> class Matrix;
44 }
45 
46 namespace casa { //# NAMESPACE CASA - BEGIN
47 
48 //# Forward Declarations
49 class MatrixNACleaner;
50 // <summary>A copy of casacore::LatticeCleaner but just using 2-D matrices</summary>
51 // <synopsis> It is a mimic of the casacore::LatticeCleaner class but avoid a lot of
52 // of the lattice to array and back copies and uses openmp in the obvious places
53 // </synopsis>
54 
55 // <summary>A class for doing multi-dimensional cleaning</summary>
56 
57 // <use visibility=export>
58 
59 // <reviewed reviewer="" date="yyyy/mm/dd" tests="tLatticeCleaner">
60 // </reviewed>
61 
62 // <prerequisite>
63 // <li> The mathematical concept of deconvolution
64 // </prerequisite>
65 //
66 // <etymology>
67 
68 // The MatrixCleaner class will deconvolve 2-D arrays of floats.
69 
70 // </etymology>
71 //
72 // <synopsis>
73 // This class will perform various types of Clean deconvolution
74 // on Lattices.
75 //
76 // </synopsis>
77 //
78 // <example>
79 // <srcblock>
80 // </srcblock>
81 // </example>
82 //
83 // <motivation>
84 // </motivation>
85 //
86 // <thrown>
87 // <li> casacore::AipsError: if psf has more dimensions than the model.
88 // </thrown>
89 //
90 // <todo asof="yyyy/mm/dd">
91 // </todo>
92 
94 {
95 public:
96 
97  // Create a cleaner : default constructor
98  MatrixCleaner();
99 
100  // Create a cleaner for a specific dirty image and PSF
102 
103  // The copy constructor uses reference semantics
104  MatrixCleaner(const MatrixCleaner& other);
105 
106  // The assignment operator also uses reference semantics
107  MatrixCleaner & operator=(const MatrixCleaner & other);
108 
109  // The destructor does nothing special.
110  ~MatrixCleaner();
111 
112  //just define the scales...nothing else is done
113  //the user will need to call setPsf+makePsfScales+setDirty+makeDirtyScales
114  //to be in a good state to clean.
116 
117  //Set the dirty image without calculating convolutions..
118  //can be done by calling makeDirtyScales or setscales if one want to redo the
119  //psfscales too.
120  void setDirty(const casacore::Matrix<casacore::Float>& dirty);
121  //Calculate the convolutions for the dirt
122  //Obviously the
123  void makeDirtyScales();
124  // Update the dirty image only (equiv of setDirty + makeDirtyScales)
125  void update(const casacore::Matrix<casacore::Float> & dirty);
126 
127  //change the psf
128  //don't forget to redo the setscales or run makePsfScales,
129  //followed by makeDirtyScales
130  void setPsf(const casacore::Matrix<casacore::Float>& psf);
131  //calculate the convolutions of the psf
132  void makePsfScales();
133 
134  // Set a number of scale sizes. The units of the scale are pixels.
135  // The 2 functions below assume you have the dirty image and the psf set
136  // the convolutions are calculated automatically and the masks ones too
137  // if it is set ....kept so as to be compatible function wise with LatticeCleaner
138  casacore::Bool setscales(const casacore::Int nscales, const casacore::Float scaleInc=1.0);
139 
140  // Set a specific set of scales
142 
143 
144 
145  // Set up control parameters
146  // cleanType - type of the cleaning algorithm to use (HOGBOM, MULTISCALE)
147  // niter - number of iterations
148  // gain - loop gain used in cleaning (a fraction of the maximum
149  // subtracted at every iteration)
150  // aThreshold - absolute threshold to stop iterations
151  // fThreshold - fractional threshold (i.e. given w.r.t. maximum residual)
152  // to stop iterations. This parameter is specified as
153  // casacore::Quantity so it can be given in per cents.
154  // choose - unused at the moment, specify false. Original meaning is
155  // to allow interactive decision on whether to continue iterations.
156  // This method always returns true.
158  const casacore::Float gain, const casacore::Quantity& aThreshold,
159  const casacore::Quantity& fThreshold);
160 
161  // This version of the method disables stopping on fractional threshold
163  const casacore::Float gain, const casacore::Quantity& threshold);
164 
165  // return how many iterations we did do
168 
169  // what iteration number to start on
170  void startingIteration(const casacore::Int starting = 0) {itsStartingIter = starting; }
171 
172  //Total flux accumulated so far
174 
175 
176  // Clean an image.
177  //return value gives you a hint of what's happening
178  // 1 = converged
179  // 0 = not converged but behaving normally
180  // -1 = not converged and stopped on cleaning consecutive smallest scale
181  // -2 = not converged and either large scale hit negative or diverging
182  // -3 = clean is diverging rather than converging
184 
185  // Set the mask
186  // mask - input mask lattice
187  // maskThreshold - if positive, the value is treated as a threshold value to determine
188  // whether a pixel is good (mask value is greater than the threshold) or has to be
189  // masked (mask value is below the threshold). Negative threshold switches mask clipping
190  // off. The mask value is used to weight the flux during cleaning. This mode is used
191  // to implement cleaning based on the signal-to-noise as opposed to the standard cleaning
192  // based on the flux. The default threshold value is 0.9, which ensures the behavior of the
193  // code is exactly the same as before this parameter has been introduced.
194  void setMask(casacore::Matrix<casacore::Float> & mask, const casacore::Float& maskThreshold = 0.9);
195  // Call the function below if the psf is changed ..no need to setMask again
197 
198  // remove the mask;
199  // useful when keeping object and sending a new dirty image to clean
200  // one can set another mask then
201  void unsetMask();
202 
203  // Tell the algorithm to NOT clean just the inner quarter
204  // (This is useful when multiscale clean is being used
205  // inside a major cycle for MF or WF algorithms)
206  // if true, the full image deconvolution will be attempted
208 
209  // Consider the case of a point source:
210  // the flux on all scales is the same, and the first scale will be chosen.
211  // Now, consider the case of a point source with a *little* bit of extended structure:
212  // thats right, the largest scale will be chosen. In this case, we should provide some
213  // bias towards the small scales, or against the large scales. We do this in
214  // an ad hoc manner, multiplying the maxima found at each scale by
215  // 1.0 - itsSmallScaleBias * itsScaleSizes(scale)/itsScaleSizes(nScalesToClean-1);
216  // Typical bias values range from 0.2 to 1.0.
218 
219  // During early iterations of a cycled casacore::MS Clean in mosaicing, it common
220  // to come across an ocsilatory pattern going between positive and
221  // negative in the large scale. If this is set, we stop at the first
222  // negative in the largest scale.
224 
225  // Some algorithms require that the cycles be terminated when the image
226  // is dominated by point sources; if we get nStopPointMode of the
227  // smallest scale components in a row, we terminate the cycles
228  void stopPointMode(casacore::Int nStopPointMode) {itsStopPointMode = nStopPointMode; }
229 
230  // After completion of cycle, querry this to find out if we stopped because
231  // of stopPointMode
233 
234  // speedup() will speed the clean iteration by raising the
235  // threshold. This may be required if the threshold is
236  // accidentally set too low (ie, lower than can be achieved
237  // given errors in the approximate PSF).
238  //
239  // threshold(iteration) = threshold(0)
240  // * ( exp( (iteration - startingiteration)/Ndouble )/ 2.718 )
241  // If speedup() is NOT invoked, no effect on threshold
242  void speedup(const casacore::Float Ndouble);
243 
244  // Look at what WE think the residuals look like
245  // Assumes the first scale is zero-sized
247  //slightly better approximation of the residual: it convolves the given model
248  //with the psf and remove it from the dirty image put in setdirty
250  // Method to return threshold, including any speedup factors
251  casacore::Float threshold() const;
252 
253  // Method to return the strength optimum achieved at the last clean iteration
254  // The output of this method makes sense only if it is called after clean
256 
257  // Helper function to optimize adding
258  //static void addTo(casacore::Matrix<casacore::Float>& to, const casacore::Matrix<casacore::Float> & add);
259 
260 protected:
261  friend class MatrixNACleaner;
262  // Make sure that the peak of the Psf is within the image
264 
265  // Make an array of the specified scale
266  void makeScale(casacore::Matrix<casacore::Float>& scale, const casacore::Float& scaleSize);
267 
268  // Make Spheroidal function for scale images
270 
271  // Find the Peak of the matrix
273  casacore::Float& maxAbs, casacore::IPosition& posMax);
274 
275  // This is made static since findMaxAbs is static(!).
276  // Why is findMaxAbs static???
277  // --SB
279  casacore::Float& maxAbs, casacore::IPosition& posMax,
280  const casacore::Int& supportSize=100);
281 
282  casacore::Int findBeamPatch(const casacore::Float maxScaleSize, const casacore::Int& nx, const casacore::Int& ny,
283  const casacore::Float psfBeam=4.0, const casacore::Float nBeams=20.0);
284  // Find the Peak of the lattice, applying a mask
286  casacore::Float& maxAbs, casacore::IPosition& posMax);
287 
288  // Helper function to reduce the box sizes until the have the same
289  // size keeping the centers intact
292 
293 
296  casacore::Int itsMaxNiter; // maximum possible number of iterations
306 private:
307 
308  //# The following functions are used in various places in the code and are
309  //# documented in the .cc file. Static functions are used when the functions
310  //# do not modify the object state. They ensure that implicit assumptions
311  //# about the current state and implicit side-effects are not possible
312  //# because all information must be supplied in the input arguments
313 
314 
317 
319 
323 
324 
325  casacore::Int itsIteration; // what iteration did we get to?
326  casacore::Int itsStartingIter; // what iteration did we get to?
328 
331 
332 
335 
336  // casacore::Memory to be allocated per TempLattice
338 
339  // Let the user choose whether to stop
341 
342  // Threshold speedup factors:
343  casacore::Bool itsDoSpeedup; // if false, threshold does not change with iteration
345 
346  //# Stop now?
347  //#// casacore::Bool stopnow(); Removed on 8-Apr-2004 by GvD
348 
349  // Calculate index into PsfConvScales
350  casacore::Int index(const casacore::Int scale, const casacore::Int otherscale);
351 
354 
355 
356 
362 
363  // threshold for masks. If negative, mask values are used as weights and no pixels are
364  // discarded (although effectively they would be discarded if the mask value is 0.)
365 
368 
369 };
370 
371 } //# NAMESPACE CASA - END
372 
373 #endif
~MatrixCleaner()
The destructor does nothing special.
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
casacore::Int index(const casacore::Int scale, const casacore::Int otherscale)
Calculate index into PsfConvScales.
int Int
Definition: aipstype.h:50
void setSmallScaleBias(const casacore::Float x=0.5)
Consider the case of a point source: the flux on all scales is the same, and the first scale will be ...
casacore::Matrix< casacore::Float > residual()
Look at what WE think the residuals look like Assumes the first scale is zero-sized.
casacore::Float threshold() const
Method to return threshold, including any speedup factors.
casacore::Quantum< casacore::Double > itsThreshold
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
static casacore::Bool findPSFMaxAbs(const casacore::Matrix< casacore::Float > &lattice, casacore::Float &maxAbs, casacore::IPosition &posMax, const casacore::Int &supportSize=100)
This is made static since findMaxAbs is static(!).
casacore::Int itsIteration
casacore::Int itsStopPointMode
casacore::Int numberIterations() const
casacore::Float spheroidal(casacore::Float nu)
Make Spheroidal function for scale images.
casacore::Float itsMaximumResidual
casacore::Float itsMaskThreshold
void update(const casacore::Matrix< casacore::Float > &dirty)
Update the dirty image only (equiv of setDirty + makeDirtyScales)
void startingIteration(const casacore::Int starting=0)
what iteration number to start on
casacore::Int iteration() const
return how many iterations we did do
casacore::Bool setscales(const casacore::Int nscales, const casacore::Float scaleInc=1.0)
Set a number of scale sizes.
casacore::Bool noClean_p
casacore::Quantum< casacore::Double > itsFracThreshold
casacore::Float totalFlux() const
Total flux accumulated so far.
void setPsf(const casacore::Matrix< casacore::Float > &psf)
change the psf don&#39;t forget to redo the setscales or run makePsfScales, followed by makeDirtyScales ...
casacore::CountedPtr< casacore::Matrix< casacore::Float > > itsMask
MatrixCleaner()
Create a cleaner : default constructor.
casacore::IPosition itsPositionPeakPsf
casacore::Vector< casacore::Float > itsScaleSizes
casacore::Bool setcontrol(casacore::CleanEnums::CleanType cleanType, const casacore::Int niter, const casacore::Float gain, const casacore::Quantity &aThreshold, const casacore::Quantity &fThreshold)
Set up control parameters cleanType - type of the cleaning algorithm to use (HOGBOM, MULTISCALE) niter - number of iterations gain - loop gain used in cleaning (a fraction of the maximum subtracted at every iteration) aThreshold - absolute threshold to stop iterations fThreshold - fractional threshold (i.e.
casacore::Bool itsScalesValid
casacore::Float itsNDouble
casacore::Float strengthOptimum() const
Method to return the strength optimum achieved at the last clean iteration The output of this method ...
Referenced counted pointer for constant data.
Definition: VisModelData.h:42
static casacore::Bool findMaxAbs(const casacore::Matrix< casacore::Float > &lattice, casacore::Float &maxAbs, casacore::IPosition &posMax)
Find the Peak of the matrix.
void makeDirtyScales()
Calculate the convolutions for the dirt Obviously the.
casacore::Vector< casacore::Float > itsTotalFluxScale
void unsetMask()
remove the mask; useful when keeping object and sending a new dirty image to clean one can set anothe...
casacore::Bool itsStopAtLargeScaleNegative
double Double
Definition: aipstype.h:55
casacore::Double itsMemoryMB
casacore::Memory to be allocated per TempLattice
casacore::Block< casacore::Matrix< casacore::Complex > > itsScaleXfrs
void ignoreCenterBox(casacore::Bool huh)
Tell the algorithm to NOT clean just the inner quarter (This is useful when multiscale clean is being...
casacore::Block< casacore::Matrix< casacore::Float > > itsPsfConvScales
casacore::Int findBeamPatch(const casacore::Float maxScaleSize, const casacore::Int &nx, const casacore::Int &ny, const casacore::Float psfBeam=4.0, const casacore::Float nBeams=20.0)
casacore::Bool itsDidStopPointMode
casacore::Bool itsJustStarting
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
static void makeBoxesSameSize(casacore::IPosition &blc1, casacore::IPosition &trc1, casacore::IPosition &blc2, casacore::IPosition &trc2)
Helper function to reduce the box sizes until the have the same size keeping the centers intact...
casacore::Float itsSmallScaleBias
casacore::Float itsTotalFlux
casacore::Bool validatePsf(const casacore::Matrix< casacore::Float > &psf)
Make sure that the peak of the Psf is within the image.
float Float
Definition: aipstype.h:54
void setDirty(const casacore::Matrix< casacore::Float > &dirty)
Set the dirty image without calculating convolutions.
casacore::Bool makeScaleMasks()
Call the function below if the psf is changed..no need to setMask again.
simple 1-D array
casacore::Block< casacore::Matrix< casacore::Float > > itsScales
A copy of casacore::LatticeCleaner but just using 2-D matrices.
Definition: MatrixCleaner.h:93
casacore::Float itsStrengthOptimum
A simple deconvolver that masks by memory of previous peaks.
void speedup(const casacore::Float Ndouble)
speedup() will speed the clean iteration by raising the threshold.
MatrixCleaner & operator=(const MatrixCleaner &other)
The assignment operator also uses reference semantics.
casacore::Int itsMaxNiter
casacore::IPosition psfShape_p
threshold for masks.
casacore::Int itsNscales
casacore::Int clean(casacore::Matrix< casacore::Float > &model, casacore::Bool doPlotProgress=false)
Clean an image.
casacore::Bool queryStopPointMode() const
After completion of cycle, querry this to find out if we stopped because of stopPointMode.
casacore::Bool destroyMasks()
casacore::Bool destroyScales()
casacore::Bool itsIgnoreCenterBox
void setMask(casacore::Matrix< casacore::Float > &mask, const casacore::Float &maskThreshold=0.9)
Set the mask mask - input mask lattice maskThreshold - if positive, the value is treated as a thresho...
casacore::CleanEnums::CleanType itsCleanType
void stopPointMode(casacore::Int nStopPointMode)
Some algorithms require that the cycles be terminated when the image is dominated by point sources; i...
void stopAtLargeScaleNegative()
During early iterations of a cycled casacore::MS Clean in mosaicing, it common to come across an ocsi...
casacore::Int itsStartingIter
casacore::Float itsGain
void makeScale(casacore::Matrix< casacore::Float > &scale, const casacore::Float &scaleSize)
Make an array of the specified scale.
void defineScales(const casacore::Vector< casacore::Float > &scales)
just define the scales...nothing else is done the user will need to call setPsf+makePsfScales+setDirt...
casacore::Bool itsDoSpeedup
Threshold speedup factors:
void makePsfScales()
calculate the convolutions of the psf
casacore::Block< casacore::Matrix< casacore::Float > > itsDirtyConvScales
casacore::Bool findMaxAbsMask(const casacore::Matrix< casacore::Float > &lattice, const casacore::Matrix< casacore::Float > &mask, casacore::Float &maxAbs, casacore::IPosition &posMax)
Find the Peak of the lattice, applying a mask.
casacore::Bool itsChoose
Let the user choose whether to stop.
casacore::CountedPtr< casacore::Matrix< casacore::Float > > itsDirty
casacore::Block< casacore::Matrix< casacore::Float > > itsScaleMasks
casacore::CountedPtr< casacore::Matrix< casacore::Complex > > itsXfr
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42