LCOV - code coverage report
Current view: top level - synthesis/ImagerObjects - SDAlgorithmMEM.cc (source / functions) Hit Total Coverage
Test: casa_coverage.info Lines: 66 80 82.5 %
Date: 2023-10-25 08:47:59 Functions: 6 6 100.0 %

          Line data    Source code
       1             : //# SDAlgorithmMEM.cc: Implementation of SDAlgorithmMEM classes
       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             : //# $Id$
      27             : 
      28             : #include <casacore/casa/Arrays/ArrayMath.h>
      29             : #include <casacore/casa/OS/HostInfo.h>
      30             : 
      31             : #include <components/ComponentModels/SkyComponent.h>
      32             : #include <components/ComponentModels/ComponentList.h>
      33             : #include <casacore/images/Images/TempImage.h>
      34             : #include <casacore/images/Images/SubImage.h>
      35             : #include <casacore/images/Regions/ImageRegion.h>
      36             : #include <casacore/casa/OS/File.h>
      37             : #include <casacore/lattices/LEL/LatticeExpr.h>
      38             : #include <casacore/lattices/Lattices/TiledLineStepper.h>
      39             : #include <casacore/lattices/Lattices/LatticeStepper.h>
      40             : #include <casacore/lattices/Lattices/LatticeIterator.h>
      41             : #include <synthesis/TransformMachines/StokesImageUtil.h>
      42             : #include <casacore/coordinates/Coordinates/StokesCoordinate.h>
      43             : #include <casacore/casa/Exceptions/Error.h>
      44             : #include <casacore/casa/BasicSL/String.h>
      45             : #include <casacore/casa/Utilities/Assert.h>
      46             : #include <casacore/casa/OS/Directory.h>
      47             : #include <casacore/tables/Tables/TableLock.h>
      48             : 
      49             : #include<synthesis/ImagerObjects/SIMinorCycleController.h>
      50             : 
      51             : #include <sstream>
      52             : 
      53             : #include <casacore/casa/Logging/LogMessage.h>
      54             : #include <casacore/casa/Logging/LogIO.h>
      55             : #include <casacore/casa/Logging/LogSink.h>
      56             : 
      57             : #include <casacore/casa/System/Choice.h>
      58             : #include <msvis/MSVis/StokesVector.h>
      59             : 
      60             : #include <synthesis/ImagerObjects/SDAlgorithmMEM.h>
      61             : #include <synthesis/MeasurementEquations/CEMemModel.h>
      62             : #include <synthesis/MeasurementEquations/LatConvEquation.h>
      63             : #include <synthesis/MeasurementEquations/IPLatConvEquation.h>
      64             : 
      65             : 
      66             : using namespace casacore;
      67             : namespace casa { //# NAMESPACE CASA - BEGIN
      68             : 
      69           1 :   SDAlgorithmMEM::SDAlgorithmMEM(String entropy):
      70           1 :     SDAlgorithmBase()
      71             :  {
      72           3 :    LogIO os( LogOrigin("SDAlgorithmMEM","Constructor",WHERE) );
      73           1 :    itsAlgorithmName=String("Mem");
      74           1 :    itsMatDeltaModel.resize();
      75             : 
      76           1 :     if(entropy=="entropy") {
      77             :       //      os << "Deconvolving image using maximum entropy algorithm" << LogIO::POST;
      78           1 :       itsEnt = new EntropyI;
      79             :     }
      80           0 :     else if (entropy=="emptiness") {
      81           0 :       itsEnt = new EntropyEmptiness;
      82             :     }
      83             :     else {
      84             :       // Put check in Deconvolver parameter object.
      85           0 :       os << " Known MEM entropies: entropy | emptiness " << LogIO::POST;
      86           0 :       os << LogIO::SEVERE << "Unknown MEM entropy: " << entropy << LogIO::POST;
      87             :       //      return false;
      88             :     }
      89             :    
      90           1 :  }
      91             : 
      92           2 :   SDAlgorithmMEM::~SDAlgorithmMEM()
      93             :  {
      94             :    
      95           2 :  }
      96             : 
      97             :   //  void SDAlgorithmMEM::initializeDeconvolver( Float &peakresidual, Float &modelflux )
      98           2 :   void SDAlgorithmMEM::initializeDeconvolver()
      99             :   {
     100           6 :     LogIO os( LogOrigin("SDAlgorithmMEM","initializeDeconvolver",WHERE) );
     101             : 
     102           2 :     itsImages->residual()->get( itsMatResidual, true );
     103           2 :     itsImages->model()->get( itsMatModel, true );
     104           2 :     itsImages->psf()->get( itsMatPsf, true );
     105           2 :     itsImages->mask()->get( itsMatMask, true );
     106             : 
     107             : 
     108             :     //    cout << "initDecon : " << itsImages->residual()->shape() << " : " << itsMatResidual.shape() 
     109             :     //   << itsImages->model()->shape() << " : " << itsMatModel.shape() 
     110             :     //   << itsImages->psf()->shape() << " : " << itsMatPsf.shape() 
     111             :     //   << endl;
     112             : 
     113             :     /*
     114             :     findMaxAbs( itsMatResidual, itsPeakResidual, itsMaxPos );
     115             :     itsModelFlux = sum( itsMatModel );
     116             : 
     117             :     peakresidual = itsPeakResidual;
     118             :     modelflux = itsModelFlux;
     119             :     */
     120             : 
     121             :     // Initialize the Delta Image model. Resize if needed.
     122           2 :     if ( itsMatDeltaModel.shape().nelements() != itsMatModel.shape().nelements() )
     123           1 :       { itsMatDeltaModel.resize ( itsMatModel.shape() ); }
     124             : 
     125             : 
     126           2 :   }
     127             : 
     128             :   // Code obtained from Deconvolver.cc
     129           2 :   void SDAlgorithmMEM::takeOneStep( Float /*loopgain*/, 
     130             :                                             Int cycleNiter, 
     131             :                                             Float cycleThreshold, 
     132             :                                             Float &peakresidual, 
     133             :                                             Float &modelflux, 
     134             :                                             Int &iterdone)
     135             :   {
     136           6 :     LogIO os( LogOrigin("SDAlgorithmMEM","takeOneStep",WHERE) );
     137             :     // tmp
     138           2 :     itsImages->residual()->get( itsMatResidual, true );
     139           2 :     itsImages->mask()->get( itsMatMask, true );
     140           2 :     findMaxAbsMask( itsMatResidual, itsMatMask, itsPeakResidual, itsMaxPos );
     141           2 :     cout << "Peak Res at start of step : " << itsPeakResidual << endl;
     142             :     // tmp
     143             : 
     144             : 
     145             :     // Store current model in this matrix.
     146           2 :     itsImages->model()->get( itsMatDeltaModel, true );
     147           2 :     itsMatModel.assign( itsMatDeltaModel ); // This should make an explicit copy
     148           2 :     cout << "Flux at start of step : " << sum(itsMatModel) << endl;
     149             : 
     150             :     // Set model to zero
     151             :     
     152           4 :     LatticeLocker lockmod(*(itsImages->model()), FileLocker::Write);
     153           2 :     itsImages->model()->set( 0.0 );
     154             :     
     155             :     // Add to construction params
     156           2 :     Float targetFlux=1.0;
     157           2 :     Bool constrainTargetFlux=false;
     158           2 :     Bool initializeModel=true; // Use incremental model ?
     159           2 :     Bool imagePlane=true; // Use full image plane. Otherwise, use inner quarter.
     160             :     
     161           2 :     CEMemModel memer( *itsEnt, *(itsImages->model()), cycleNiter, cycleThreshold,
     162             :                       targetFlux, constrainTargetFlux, 
     163           6 :                       initializeModel, imagePlane);
     164             :     
     165           2 :     if (!initializeModel) {
     166           0 :       Record info=itsImages->model()->miscInfo();
     167             :       try {
     168           0 :         Float alpha = 0.0;
     169           0 :         Float beta = 0.0;
     170           0 :         info.get("ALPHA", alpha);
     171           0 :         memer.setAlpha(alpha);
     172           0 :         info.get("BETA", beta);
     173           0 :         memer.setBeta(beta); 
     174           0 :       } catch  (AipsError x) {
     175             :         // could not get Alpha and Beta for initialization
     176             :         // continue
     177             :         os << "Could not retrieve Alpha and Beta from previously initialized model" 
     178           0 :            << LogIO::POST;
     179             :       } 
     180             :     } 
     181             : 
     182             :     /// Set the Prior    
     183             :     ///    if(prior != 0){
     184             :     ///      memer.setPrior(priorSub);
     185             :     ///    }
     186             : 
     187           2 :     memer.setMask(*(itsImages->mask()));
     188             : 
     189           4 :     CountedPtr<ResidualEquation<Lattice<Float> > > residEqn;
     190             : 
     191           2 :     if (imagePlane) {
     192           2 :       residEqn = new IPLatConvEquation (*(itsImages->psf()), *(itsImages->residual()));
     193             :     } else {
     194           0 :       residEqn = new LatConvEquation (*(itsImages->psf()), *(itsImages->residual()));
     195             :     }    
     196             :     
     197             :     //Bool result=
     198           2 :     memer.solve(*residEqn);
     199             : 
     200           4 :     Record info=itsImages->model()->miscInfo();
     201           2 :     info.define("ALPHA", memer.getAlpha());
     202           2 :     info.define("BETA",  memer.getBeta());
     203           2 :     itsImages->model()->setMiscInfo(info);
     204             : 
     205           2 :     iterdone = memer.numberIterations();
     206             : 
     207           4 :     LatticeExprNode maxres( max( memer.getResidual() ) );
     208           2 :     cout << "MAX RES at end : " << maxres.getFloat() << endl;
     209             : 
     210             :     // Retrieve residual before major cycle
     211           2 :     itsImages->residual()->copyData( memer.getResidual() );
     212             : 
     213             :     // Add delta model to old model
     214             :     //Bool ret2 = 
     215           2 :     itsImages->model()->get( itsMatDeltaModel, true );
     216           2 :     itsMatModel += itsMatDeltaModel;
     217             : 
     218             :     //---------------------------------
     219             : 
     220             :     //  Find Peak Residual
     221           2 :     itsImages->residual()->get( itsMatResidual, true );
     222           2 :     itsImages->mask()->get( itsMatMask, true );
     223           2 :     findMaxAbsMask( itsMatResidual, itsMatMask, itsPeakResidual, itsMaxPos );
     224           2 :     peakresidual = itsPeakResidual;
     225             : 
     226             :     // Find Total Model flux
     227           2 :     modelflux = sum( itsMatModel ); // Performance hog ?
     228           2 :     (itsImages->model())->put( itsMatModel );
     229             : 
     230           2 :     cout << "peakres : " << peakresidual << "    model : " << modelflux << endl;
     231             : 
     232           2 :   }         
     233             : 
     234           2 :   void SDAlgorithmMEM::finalizeDeconvolver()
     235             :   {
     236           4 :     LatticeLocker lock1(*(itsImages->residual()), FileLocker::Write);
     237           2 :     LatticeLocker lock2(*(itsImages->model()), FileLocker::Write);
     238           2 :     (itsImages->residual())->put( itsMatResidual );
     239           2 :     (itsImages->model())->put( itsMatModel );
     240           2 :   }
     241             : 
     242             : 
     243             : } //# NAMESPACE CASA - END
     244             : 

Generated by: LCOV version 1.16