ImageFitter.h

Go to the documentation of this file.
00001 //# Copyright (C) 1998,1999,2000,2001,2003
00002 //# Associated Universities, Inc. Washington DC, USA.
00003 //#
00004 //# This program is free software; you can redistribute it and/or modify it
00005 //# under the terms of the GNU General Public License as published by the Free
00006 //# Software Foundation; either version 2 of the License, or (at your option)
00007 //# any later version.
00008 //#
00009 //# This program is distributed in the hope that it will be useful, but WITHOUT
00010 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00012 //# more details.
00013 //#
00014 //# You should have received a copy of the GNU General Public License along
00015 //# with this program; if not, write to the Free Software Foundation, Inc.,
00016 //# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00017 //#
00018 //# Correspondence concerning AIPS++ should be addressed as follows:
00019 //#        Internet email: aips2-request@nrao.edu.
00020 //#        Postal address: AIPS++ Project Office
00021 //#                        National Radio Astronomy Observatory
00022 //#                        520 Edgemont Road
00023 //#                        Charlottesville, VA 22903-2475 USA
00024 //#
00025 
00026 #ifndef IMAGES_IMAGEFITTER_H
00027 #define IMAGES_IMAGEFITTER_H
00028 
00029 #include <imageanalysis/ImageAnalysis/ImageTask.h>
00030 
00031 #include <components/ComponentModels/ComponentList.h>
00032 #include <lattices/LatticeMath/Fit2D.h>
00033 
00034 #include <imageanalysis/IO/ImageFitterResults.h>
00035 
00036 namespace casa {
00037 
00038 class ImageFitter : public ImageTask<Float> {
00039     // <summary>
00040     // Top level interface to ImageAnalysis::fitsky to handle inputs, bookkeeping etc and
00041     // ultimately call fitsky to do fitting
00042     // </summary>
00043 
00044     // <reviewed reviewer="" date="" tests="" demos="">
00045     // </reviewed>
00046 
00047     // <prerequisite>
00048     // </prerequisite>
00049 
00050     // <etymology>
00051     // Fits components to sources in images (ImageSourceComponentFitter was deemed to be to long
00052     // of a name)
00053     // </etymology>
00054 
00055     // <synopsis>
00056     // ImageFitter is the top level interface for fitting image source components. It handles most
00057     // of the inputs, bookkeeping etc. It can be instantiated and its one public method, fit,
00058     // run from either a C++ app or python.
00059     // </synopsis>
00060 
00061     // <example>
00062     // <srcblock>
00063     // ImageFitter fitter(...)
00064     // fitter.fit()
00065     // </srcblock>
00066     // </example>
00067 
00068 public:
00069 
00070     ImageFitter() = delete;
00071 
00072     // constructor appropriate for API calls.
00073     // Parameters:
00074     // <ul>
00075     // <li>imagename - the name of the input image in which to fit the models</li>
00076     // <li>box - A 2-D rectangular box in which to use pixels for the fitting, eg box=100,120,200,230
00077     // In cases where both box and region are specified, box, not region, is used.</li>
00078     // <li>region - Named region to use for fitting</li>
00079     // <li>regionPtr - A pointer to a region. Note there are unfortunately several different types of
00080     // region records throughout CASA. In this case, it must be a Record produced by creating a
00081     // region via a RegionManager method.
00082     // <li>chanInp - Zero-based channel number on which to do the fit. Only a single channel can be
00083     // specified.</li>
00084     // <li>stokes - Stokes plane on which to do the fit. Only a single Stokes parameter can be
00085     // specified.</li>
00086     // <li> maskInp - Mask (as LEL) to use as a way to specify which pixels to use </li>
00087     // <li> includepix - Pixel value range to include in the fit. includepix and excludepix
00088     // cannot be specified simultaneously. </li>
00089     // <li> excludepix - Pixel value range to exclude from fit</li>
00090     // <li> residualInp - Name of residual image to save. Blank means do not save residual image</li>
00091     // <li> modelInp - Name of the model image to save. Blank means do not save model image</li>
00092 
00093     // use these constructors when you already have a pointer to a valid ImageInterface object
00094 
00095 
00096     ImageFitter(
00097         const SPCIIF image, const String& region,
00098         const Record *const &regionRec,
00099         const String& box="",
00100         const String& chanInp="", const String& stokes="",
00101         const String& maskInp="",
00102         const String& estiamtesFilename="",
00103         const String& newEstimatesInp="", const String& compListName=""
00104     );
00105 
00106     ImageFitter(const ImageFitter& other) = delete;
00107 
00108     // destructor
00109     ~ImageFitter();
00110 
00111     // Do the fit. If componentList is specified, store the fitted components in
00112     // that object. The first list in the returned pair represents the convolved components.
00113     // The second list represents the deconvolved components. If the image has no beam,
00114     // the two lists will be the same.
00115     std::pair<ComponentList, ComponentList> fit();
00116 
00117     void setWriteControl(ImageFitterResults::CompListWriteControl x) { _writeControl = x; }
00118 
00119     inline String getClass() const {return _class;}
00120 
00121     // Did the fit converge for the specified channel?
00122     // Throw AipsError if the fit has not yet been done.
00123     // <src>plane</src> is relative to the first plane in the image chosen to be fit.
00124     Bool converged(uInt plane) const;
00125 
00126     // Did the fit converge?
00127     // Throw AipsError if the fit has not yet been done.
00128     // <src>plane</src> is relative to the first plane in the image chosen to be fit.
00129     Vector<Bool> converged() const;
00130 
00131     // set the zero level estimate. Implies fitting of zero level should be done. Must be
00132     // called before fit() to have an effect.
00133     void setZeroLevelEstimate(Double estimate, Bool isFixed);
00134 
00135     // Unset zero level (resets to zero). Implies fitting of zero level should not be done.
00136     // Call prior to fit().
00137     void unsetZeroLevelEstimate();
00138 
00139     // get the fitted result and error. Throws
00140     // an exception if the zero level was not fit for.
00141     void getZeroLevelSolution(vector<Double>& solution, vector<Double>& error);
00142 
00143     // set rms level for calculating uncertainties. If not positive, an exception is thrown.
00144     void setRMS(const Quantity& rms);
00145 
00146     void setIncludePixelRange(const std::pair<Float, Float>& r) {
00147         _includePixelRange.reset(new std::pair<Float, Float>(r));
00148     }
00149 
00150     void setExcludePixelRange(const std::pair<Float, Float>& r) {
00151         _excludePixelRange.reset(new std::pair<Float, Float>(r));
00152     }
00153 
00154     // set the output model image name
00155     void setModel(const String& m) { _model = m; }
00156 
00157     // set the output residual image name
00158     void setResidual(const String& r) { _residual = r; }
00159 
00160     // set noise correlation beam FWHM
00161     void setNoiseFWHM(const Quantity& q);
00162 
00163     // in pixel widths
00164     void setNoiseFWHM(Double d);
00165 
00166     // clear noise FWHM, if the image has no beam, use the uncorrelated noise equations.
00167     // If the image has a beam(s) use the correlated noise equations with theta_N =
00168     // the geometric mean of the beam major and minor axes.
00169     void clearNoiseFWHM();
00170 
00171     // The Record holding all the output info
00172     Record getOutputRecord() const {return _output; }
00173 
00174     // Set The summary text file name.
00175     void setSummaryFile(const String& f) { _summary = f; }
00176 
00177 protected:
00178 
00179     virtual Bool _hasLogfileSupport() const { return True; }
00180 
00181     virtual inline Bool _supportsMultipleRegions() const {return True;}
00182 
00183 private:
00184 
00185     using Angular2DGaussian = GaussianBeam;
00186 
00187     String _regionString;
00188     String _residual{}, _model{}, _estimatesString{}, _summary{};
00189     String _newEstimatesFileName, _compListName, _bUnit;
00190     SHARED_PTR<std::pair<Float, Float> > _includePixelRange{}, _excludePixelRange{};
00191     ComponentList _estimates{}, _curConvolvedList, _curDeconvolvedList;
00192     Vector<String> _fixed{}, _deconvolvedMessages;
00193     Bool _fitDone{False}, _noBeam{False}, _doZeroLevel{}, _zeroLevelIsFixed{},
00194         _correlatedNoise, _useBeamForNoise;
00195     Vector<Bool> _fitConverged{};
00196     Vector<Quantity> _peakIntensities{}, _peakIntensityErrors, _fluxDensityErrors,
00197         _fluxDensities, _majorAxes, _majorAxisErrors, _minorAxes, _minorAxisErrors,
00198         _positionAngles, _positionAngleErrors;
00199     vector<Quantity> _allConvolvedPeakIntensities{}, _allConvolvedPeakIntensityErrors{}, _allSums{},
00200         _allFluxDensities{}, _allFluxDensityErrors{};
00201     vector<GaussianBeam> _allBeams;
00202     vector<Double> _allBeamsPix, _allBeamsSter;
00203     vector<uInt> _allChanNums;
00204     vector<Bool> _isPoint;
00205     Record _residStats, inputStats, _output;
00206     Double _rms = -1;
00207     String _kludgedStokes;
00208     ImageFitterResults::CompListWriteControl _writeControl = ImageFitterResults::NO_WRITE;
00209     Vector<uInt> _chanVec;
00210     uInt _curChan;
00211     Double _zeroLevelOffsetEstimate = 0;
00212     vector<Double> _zeroLevelOffsetSolution, _zeroLevelOffsetError;
00213     Int _stokesPixNumber = -1, _chanPixNumber = -1;
00214     ImageFitterResults _results;
00215     std::unique_ptr<Quantity> _noiseFWHM{};
00216     Quantity _pixWidth{0, "arcsec"};
00217 
00218     const static String _class;
00219 
00220     void _fitLoop(
00221         Bool& anyConverged, ComponentList& convolvedList,
00222         ComponentList& deconvolvedList, SPIIF templateImage,
00223         SPIIF residualImage, SPIIF modelImage,
00224         /*LCMask& completePixelMask, */ String& resultsString
00225     );
00226 
00227     vector<OutputDestinationChecker::OutputStruct> _getOutputStruct();
00228 
00229     vector<Coordinate::Type> _getNecessaryCoordinates() const;
00230 
00231     CasacRegionManager::StokesControl _getStokesControl() const;
00232 
00233     void _finishConstruction(const String& estimatesFilename);
00234 
00235     //String _resultsHeader() const;
00236 
00237     // summarize the results in a nicely formatted string
00238     String _resultsToString(uInt nPixels) const;
00239 
00240     //summarize the size details in a nicely formatted string
00241     String _sizeToString(const uInt compNumber) const;
00242 
00243     String _spectrumToString(uInt compNumber) const;
00244 
00245     void _setDeconvolvedSizes();
00246 
00247     void _getStandardDeviations(Double& inputStdDev, Double& residStdDev) const;
00248 
00249     void _getRMSs(Double& inputRMS, Double& residRMS) const;
00250 
00251     Double _getStatistic(const String& type, const uInt index, const Record& stats) const;
00252 
00253     String _statisticsToString() const;
00254 
00255     SPIIF _createImageTemplate() const;
00256 
00257     void _writeCompList(ComponentList& list) const;
00258 
00259     void _setIncludeExclude(
00260         Fit2D& fitter
00261     ) const;
00262 
00263     void _fitsky(
00264         Fit2D& fitter, Array<Float>& pixels,
00265         Array<Bool>& pixelMask, Bool& converged,
00266         Double& zeroLevelOffsetSolution,
00267         Double& zeroLevelOffsetError,
00268         std::pair<Int, Int>& pixelOffsets,
00269         const Vector<String>& models, Bool fitIt,
00270         Bool deconvolveIt,
00271         Double zeroLevelEstimate
00272     );
00273 
00274     Vector<Double> _singleParameterEstimate(
00275         Fit2D& fitter, Fit2D::Types model,
00276         const MaskedArray<Float>& pixels, Float minVal,
00277         Float maxVal, const IPosition& minPos, const IPosition& maxPos
00278     ) const;
00279 
00280     ComponentType::Shape _convertModelType(Fit2D::Types typeIn) const;
00281 
00282     void _fitskyExtractBeam(
00283         Vector<Double>& parameters, const ImageInfo& imageInfo,
00284         const Bool xIsLong, const CoordinateSystem& cSys
00285     ) const;
00286 
00287     void _encodeSkyComponentError(
00288         SkyComponent& sky, Double facToJy, const CoordinateSystem& csys,
00289         const Vector<Double>& parameters, const Vector<Double>& errors,
00290         Stokes::StokesTypes stokes, Bool xIsLong
00291     ) const;
00292 
00293     void _doConverged(
00294         ComponentList& convolvedList, ComponentList& deconvolvedList,
00295         Double& zeroLevelOffsetEstimate, std::pair<Int, Int>& pixelOffsets,
00296         SPIIF& residualImage, SPIIF& modelImage,
00297         SHARED_PTR<TempImage<Float> >& tImage,
00298         SHARED_PTR<ArrayLattice<Bool> >& initMask,
00299         Double zeroLevelOffsetSolution, Double zeroLevelOffsetError,
00300         Bool hasSpectralAxis, Int spectralAxisNumber, Bool outputImages, const IPosition& planeShape,
00301         const Array<Float>& pixels, const Array<Bool>& pixelMask, const Fit2D& fitter,
00302         SPIIF templateImage
00303     );
00304 
00305     Quantity _pixelWidth();
00306 
00307     void _calculateErrors();
00308 
00309     Double _getRMS() const;
00310 
00311     Double _correlatedOverallSNR(
00312         uInt comp, Double a, Double b, Double signalToNoise
00313     ) const;
00314 
00315     GaussianBeam _getCurrentBeam() const;
00316 
00317     void _createOutputRecord(const ComponentList& convolved, const ComponentList& decon);
00318 
00319     void _setSum(const SkyComponent& comp, const SubImage<Float>& im, uInt compNum);
00320 
00321     void _setBeam(GaussianBeam& beam, uInt ngauss);
00322 };
00323 }
00324 
00325 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1