casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ClarkCleanModel.h
Go to the documentation of this file.
00001 //# ClarkCleanModel.h: this defines ClarkCleanModel
00002 //# Copyright (C) 1996,1997,1998,1999,2000,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id$
00028 
00029 #ifndef SYNTHESIS_CLARKCLEANMODEL_H
00030 #define SYNTHESIS_CLARKCLEANMODEL_H
00031 
00032 #include <casa/aips.h>
00033 #include <casa/Arrays/Matrix.h>
00034 #include <casa/Arrays/Vector.h>
00035 #include <casa/Arrays/Array.h>
00036 #include <synthesis/MeasurementEquations/ArrayModel.h>
00037 #include <synthesis/MeasurementEquations/Iterate.h>
00038 //#include <synthesis/MeasurementEquations/ResidualEquation.h>
00039 #include <synthesis/MeasurementEquations/ConvolutionEquation.h>
00040 #include <casa/Logging/LogIO.h>
00041 
00042 namespace casa { //# NAMESPACE CASA - BEGIN
00043 
00044 class ClarkCleanProgress;
00045 
00046 // <summary>
00047 // A Class for performing the Clark Clean Algorithm on Arrays
00048 // </summary>
00049 
00050 // <use visibility=export>
00051 
00052 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00053 // </reviewed>
00054 
00055 // <prerequisite> 
00056 // <li> ResidualEquation/ConvolutionEquation 
00057 // <li> LinearModel/LinearEquation Paradigm 
00058 // </prerequisite>
00059 //
00060 // <etymology>
00061 // This class is called ClarkCleanModel because thats the algorithm it uses
00062 // deconvolve the model. 
00063 // </etymology>
00064 //
00065 // <synopsis>
00066 // This class is used to perform the Clark Clean Algorithm on an
00067 // Array. Only the deconvolved model of the sky are directly stored by this
00068 // class. The point spread function (psf) and convolved (dirty) image are
00069 // stored in a companion class which is must be derived from
00070 // ResidualEquation. 
00071 // 
00072 // The cleaning works like this. The user constructs a ClarkCleanModel by
00073 // specifying an initial model of the sky. This can by be
00074 // one,two,three... dimensional depending on the dimension of the psf (see
00075 // below). The user then constructs a class which implements the forward
00076 // equation between the model and the dirty image. Typically this will be
00077 // the ConvolutionEquation class, although any class which has a
00078 // ResidualEquation interface will be work (but perhaps very slowly, as the
00079 // ConvolutionEquation class has member functions optimised for cleaning)
00080 //
00081 // The user then calls the solve() function (with the appropriate equation
00082 // class as an arguement), and this class will perform the Clark clean.
00083 // The various clean parameters are set (prior to calling solve) using the
00084 // functions derived from the Iterate class, in particular setGain(),
00085 // setNumberIterations() & setThreshold() (to set a flux limit). 
00086 // 
00087 // The solve() function does not return either the deconvolved model or the
00088 // residuals. The solved model can be obtained using the getModel() function
00089 // (derived from ArrayModel()) and the residual can be obtained using the
00090 // residual() member function of the Convolution/Residual Equation Class.
00091 // 
00092 // The size and shape of the model used in this class MUST be the same as
00093 // the convolved data (Dirty Image), stored in the companion
00094 // ResidualEquation Class. However the model (and convolved data) can have
00095 // more dimensions than the psf, as well as a different size (either larger
00096 // or smaller). When the dimensionality is different the cleaning is done
00097 // independendtly in each "plane" of the model. (Note this has not
00098 // been implemented yet but is relatively simple to do if necessary). 
00099 //
00100 // This multi-dimensionalty is exploited when cleaning arrays of
00101 // StokesVectors. Here the Array of StokesVectors is decomposed into a stack
00102 // of 4 Floating point arrays and the cleaning is done on all the the arrays
00103 // simultaneosly. The criterion for choosing the brightest pixel has been
00104 // generalised by using the "length" of the Stokesvector in 4 dimensional
00105 // space. 
00106 //
00107 // A companion class to this one is MaskedClarkCleanModel. This provides
00108 // the same functionality but is used with MaskedArrays which indicate which
00109 // regions of the model to search for clean components. 
00110 //
00111 // </synopsis>
00112 //
00113 // <example>
00114 // <srcblock>
00115 // Matrix<Float> psf(12,12), dirty(10,10), initialModel(10,10);
00116 // ...put appropriate values into psf, dirty, & initialModel....
00117 // ClarkCleanModel<Float> deconvolvedModel(initialModel); 
00118 // ConvolutionEquation convEqn(psf, dirty);
00119 // deconvolvedModel.setGain(0.2); 
00120 // deconvolvedModel.setNumberIterations(1000);
00121 // Bool convWorked = deconvolvedModel.solve(convEqn);
00122 // Array<Float> finalModel, residuals;
00123 // if (convWorked){
00124 //   finalModel = deconvolvedModel.getModel();
00125 //   ConvEqn.residual(deconvolvedModel, finalResidual);
00126 // }
00127 // </srcblock> 
00128 // </example>
00129 //
00130 // <motivation>
00131 // This class is needed to deconvolve images.
00132 // </motivation>
00133 //
00134 // <templating arg=T>
00135 // I have tested this class with Arrays of
00136 //    <li> Float
00137 //    <li> StokesVector
00138 // </templating>
00139 //
00140 // <todo asof="1996/05/02">
00141 //   <li> Make changes so that multidimensions work as advertised
00142 //   <li> compare timing with other clean implementations (ie, Mark's
00143 //   CleanTools, SDE, AIPS & miriad) 
00144 // </todo>
00145 
00146 class ClarkCleanModel: 
00147   public ArrayModel<Float>,
00148   public Iterate
00149 {
00150 public:
00151   // The default constructor does nothing more than initialise a zero length
00152   // array to hold the deconvolved model. If this constructor is used then 
00153   // the actual model must be set using the setModel() function of the
00154   // ArrayModel class.
00155   ClarkCleanModel();
00156   // Construct the ClarkCleanModel object and initialise the model.
00157   ClarkCleanModel(Array<Float> & model);
00158   // Construct the ClarkCleanModel object and initialise the model ans mask
00159   ClarkCleanModel(Array<Float> & model, Array<Float> & mask);
00160 
00161   void getMask(Array<Float>& mask) const;
00162   void setMask(const Array<Float>& mask);
00163   void setMask(Array<Float> & mask);
00164 
00165   void getModel(Array<Float>& model) const;
00166   void setModel(const Array<Float>& model);
00167   void setModel(Array<Float> & model);
00168 
00169   // Set/get the progress display 
00170   // <group>
00171   virtual void setProgress(ClarkCleanProgress& ccp) { itsProgressPtr = &ccp; }
00172   virtual ClarkCleanProgress& getProgress() { return *itsProgressPtr; }
00173   // </group>
00174 
00175   // Using a Clark clean deconvolution proceedure solve for an improved
00176   // estimate of the deconvolved object. The convolution/residual equation
00177   // contains the psf and dirty image. When called with a ResidualEquation
00178   // arguement a quite general interface is used that is slow. The
00179   // convolution equation contains functions that speed things up. The
00180   // functions return False if the deconvolution could not be done.
00181   // <group>
00182   Bool solve(ConvolutionEquation & eqn);
00183   Bool singleSolve(ConvolutionEquation & eqn, Array<Float>& residual);
00184   // </group>
00185 
00186   // These functions set various "knobs" that the user can tweak and are
00187   // specific to the Clark clean algorithm. The more generic parameters
00188   // ie. clean gain, and maximum residual fluxlimit, are set using functions
00189   // in the Iterate base class. The get functions return the value that was
00190   // actually used after the cleaning was done.
00191   // <group>
00192   // set the size of the PSF used in the minor iterations. If not set it
00193   // defaults to the largest useful Psf (ie. min(modelSize*2, psfSize))
00194   virtual void setPsfPatchSize(const IPosition & psfPatchSize); 
00195   virtual IPosition getPsfPatchSize(); 
00196   // Set the size of the histogram used to determine how many pixels are
00197   // "active" in a minor iteration. Default value is 1000 is OK for
00198   // everything except very small cleans.
00199   virtual void setHistLength(const uInt HistBins ); 
00200   virtual uInt getHistLength(); 
00201   // Set the maximum number of minor iterations to perform for each major
00202   // cycle. 
00203   virtual void setMaxNumberMinorIterations(const uInt maxNumMinorIterations); 
00204   virtual uInt getMaxNumberMinorIterations();
00205   // Set and get the initial number of iterations
00206   virtual void setInitialNumberIterations(const uInt initialNumberIterations); 
00207   virtual uInt getInitialNumberIterations();
00208   // Set the maximum number of major cycles to perform
00209   virtual void setMaxNumberMajorCycles(const uInt maxNumMajorCycles); 
00210   virtual uInt getMaxNumberMajorCycles();
00211   // Set the maximum number of active pixels to use in the minor
00212   // iterations. The specified number can be exceeded if the topmost bin of
00213   // the histogram contains more pixels than specified here. The default is
00214   // 10,000 which is suitable for images of 512by512 pixels. Reduce this for
00215   // smaller images and increase it for larger ones. 
00216   virtual void setMaxNumPix(const uInt maxNumPix ); 
00217   virtual uInt getMaxNumPix(); 
00218   // Set the maximum exterior psf value. This is used to determine when to
00219   // stop the minor itartions. This is normally determined from the psf and
00220   // the number set here is only used if this cannot be determined. The
00221   // default is zero.
00222   virtual void setMaxExtPsf(const Float maxExtPsf ); 
00223   virtual Float getMaxExtPsf(); 
00224   // An exponent on the F(m,n) factor (see Clark[1980]) which influences how
00225   // quickly active pixels are treated as unreliable. Larger values mean
00226   // more major iterations. The default is zero. I have no experience on
00227   // when to use this factor.
00228   virtual void setSpeedup(const Float speedup ); 
00229   virtual Float getSpeedup(); 
00230   // The structure of various AIPS++ algorithms creates major cycles around
00231   // the Clark Clean (or other deconvolution algrithms.  The cycleSpeedup
00232   // parameter causes the threshold to edge up as
00233   // thresh = thresh_0 * 2^( iter/cycleSpeedup ); 
00234   // ignored if cycleSpeedup <= 0.
00235   virtual void setCycleSpeedup(const Float speedup ); 
00236   virtual Float getCycleSpeedup(); 
00237   // We are overwriting Iterate's threshold() method to put out speedup in it
00238   virtual const Float threshold();
00239   // The user can be asked whether to stop after every minor cycle
00240   virtual void setChoose(const Bool askForChoice);
00241   virtual Bool getChoose();
00242   // </group>
00243 
00244 private:
00245 // Do all the minor iterations for one major cycle. Cleaning stops
00246 // when the flux or iteration limit is reached.
00247   void doMinorIterations(Array<Float> & model, 
00248                          Matrix<Float> & pixelValue, 
00249                          const Matrix<Int> & pixelPos, 
00250                          const Int numPix,
00251                          Matrix<Float> & psfPatch,
00252                          Float fluxLimit, 
00253                          uInt & numberIterations, 
00254                          Float Fmn, 
00255                          const uInt totalIterations,
00256                          Float& totalflux);
00257 // Find all the pixels in the residual that are greater than fluxlimit, and
00258 // store the values in the pixelsValue Matrix, and their positions in the
00259 // pixelPos Matrix. Increases the size of the output matrices as
00260 // necessary, but does not decrease them. So the actual number of "active"
00261 // pixels is returned. This will always be less than (or equal to) the matrix
00262 // size.
00263   Int cacheActivePixels(Matrix<Float> & pixVal, Matrix<Int> & pixPos, 
00264                          const Array<Float> & data, const Float fluxLimit);
00265 // make histogram of absolute values in array
00266   void absHistogram(Vector<Int> & hist, Float & minVal, 
00267                     Float & maxVal, const Array<Float> & data);
00268 // Determine the flux limit if we only select the maxNumPix biggest
00269 // residuals. Flux limit is not exact due to quantising by the histogram
00270   Float biggestResiduals(Float & maxRes, const uInt maxNumPix, 
00271                          const Float fluxLimit, const Array<Float> & residual);
00272 // Work out the size of the Psf patch to use. 
00273   Float getPsfPatch(Array<Float>& psfPatch, ConvolutionEquation& eqn);
00274 // The maximum residual is the absolute maximum.
00275   Float maxResidual(const Array<Float> & residual);
00276   void maxVect(Vector<Float> & maxVal, Float & absVal, Int & offset,
00277                const Matrix<Float> & pixVal, const Int numPix);
00278   void subtractComponent(Matrix<Float> & pixVal, const Matrix<Int> & pixPos,
00279                          const Int numPix, const Vector<Float> & maxVal,
00280                          const Vector<Int> & maxPos, const Matrix<Float> & psf);
00281   Float absMaxBeyondDist(const IPosition &maxDist, const IPosition &centre,
00282                          const Array<Float> &array);
00283   Bool stopnow();
00284 
00285   uInt theHistBins;
00286   Float theMaxExtPsf;
00287   uInt theMaxNumberMinorIterations;
00288   uInt theInitialNumberIterations;
00289   Int theMaxNumberMajorCycles;
00290   uInt theMaxNumPix;
00291   IPosition thePsfPatchSize;
00292   Float theSpeedup;
00293   Float theCycleSpeedup;
00294   Bool theChoose;
00295   Array<Float> theMask;
00296   LogIO theLog;
00297   // There are too many iterations counters.
00298   // This one is required for theCycleSpeedup and threshold(),
00299   // and just keeps track of the total iterations done by THIS
00300   // ClarkCleanModel
00301   Int theIterCounter; 
00302   ClarkCleanProgress* itsProgressPtr;
00303   Bool itsJustStarting;
00304 };
00305 
00306 
00307 } //# NAMESPACE CASA - END
00308 
00309 #endif