comlixfit.py 12.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
#! /usr/bin/env python
# ==========================================================================
# Perform SRCLIX model fitting of COMPTEL observations
#
# Copyright (C) 2021 Juergen Knoedlseder
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# ==========================================================================
import os
import sys
import gammalib
import ctools


# =============== #
# comlixfit class #
# =============== #
class comlixfit(ctools.cslikelihood):
    """
    Perform SRCLIX model fitting of COMPTEL observations
    """
    # Constructor
    def __init__(self, *argv):
        """
        Constructor
        """
        # Initialise application by calling the base class constructor
        self._init_cslikelihood(self.__class__.__name__, ctools.__version__, argv)

        # Initialise members
        self._fixed_pars = []

        # Return
        return

    # Private methods
    def _get_parameters(self):
        """
        Get parameters from parfile
        """
        # Set observation if not done before
        if self.obs().is_empty():
            self.obs().load(self['inobs'].filename())

        # Set models if we have none
        if self.obs().models().is_empty():
            self.obs().models(self['inmodel'].filename())

        # Query parameters
        self['max_iter'].integer()
        self['like_accuracy'].real()
        self['fix_spat_for_ts'].boolean()
        self['fix_ext_for_initial'].boolean()

        # Get parameters
        bkgmethod = self['bkgmethod'].string()
        nrunav    = self['nrunav'].integer()
        navgr     = self['navgr'].integer()
        nincl     = self['nincl'].integer()
        nexcl     = self['nexcl'].integer()
        phi_first = self['phi_first'].integer()
        phi_last  = self['phi_last'].integer()

        # Check for incorrect parameters
        if nexcl < 0 or nexcl >= nincl:
            msg = 'Incorrect value %d for nexcl (bins to exclude).' % nexcl
            raise RuntimeError(msg)
        if nexcl != 0 and 2*int(nexcl/2) == nexcl :
            msg = 'nexcl=%d (bins to exclude) should be zero or odd number.' % nexcl
            raise RuntimeError(msg)
        if nincl < 3 or 2*int(nincl/2) == nincl:
            msg = 'nincl=%d (bins to include) should be odd and >= 3.' % nincl
            raise RuntimeError(msg)
        if navgr < 1 or 2*int(navgr/2) == navgr :
            msg = 'navgr=%d should be odd and >= 1.' % navgr
            raise RuntimeError(msg)

        # Query ahead output model filename
        if self._read_ahead():
            self['suffix'].string()
            self['outfolder'].string()
            self['outobs'].filename()
            self['outmodel'].filename()

        # Set Phibar layers for fitting
        for obs in self.obs():
            if obs.classname() == 'GCOMObservation':
                if phi_first >= 0:
                    obs.phi_first(phi_first)
                if phi_last >= 0:
                    obs.phi_last(phi_last)

        #  Write input parameters into logger
        self._log_parameters(gammalib.TERSE)

        # Set optimizer logger
        if self._logExplicit():
            self.opt().logger(self._log)

        # Return
        return

    def _update_obs(self):
        """
        Update background model in observation container

        The method updated the background model in the observation container
        by taking into account the current source models in the BGDLIXA
        model generation algorithm.
        """
        # Get task parameters
        bkgmethod = self['bkgmethod'].string()
        nrunav    = self['nrunav'].integer()
        navgr     = self['navgr'].integer()
        nincl     = self['nincl'].integer()
        nexcl     = self['nexcl'].integer()

        # Extract source models from observation model container
        models = gammalib.GModels()
        for model in self.obs().models():
            if model.classname() == 'GModelSky' or model.classname() == 'GCOMModelDRM':
                models.append(model)

        # Loop over all observations
        for obs in self.obs():

            # Skip non-COMPTEL observations
            if obs.classname() != 'GCOMObservation':
                continue

            # Compute DRM
            drm = obs.drm(models)

            # Compute background model
            obs.compute_drb(bkgmethod, drm, nrunav, navgr, nincl, nexcl)

            # Signal that DRB file was not yet saved
            obs.drbname('')

        # Return
        return

    def _final_model_fit(self):
        """
        Perform final model fit using ctlike
        """
        # Create instance of model fitting tool
        like = ctools.ctlike(self.obs())
        like['fix_spat_for_ts'] = self['fix_spat_for_ts'].boolean()

        # Run ctlike
        like.run()

        # Recover results
        self.opt(like.opt())
        self.obs(like.obs())

        # Return
        return

    def _fix_ext_pars(self):
        """
        Fix spatial extension parameters
        """
        # Initialise list of fixed parameters
        self._fixed_pars = []

        # Set list of spatial extension parameters
        pars = ['Radius', 'Width', 'Sigma', 'PA', 'MajorRadius', 'MinorRadius']

        # Loop over all sky models
        for model in self.obs().models():
            if model.classname() == 'GModelSky':

                # Loop over all parameters and fix all encountered spatial
                # extension parameters
                for par in pars:
                    if model.spatial().has_par(par):
                        if model[par].is_free():

                            # Fix parameter
                            model[par].fix()

                            # Bookkeeping fixed parameters
                            fixed = {'name': model.name(), 'par':  par}
                            self._fixed_pars.append(fixed)

                            # Log action
                            value = '%s[%s]' % (model.name(), par)
                            self._log_value(gammalib.EXPLICIT, 'Fixed parameter', value)

        # Return
        return

    def _free_fixed_ext_pars(self):
        """
        Free fixed spatial extension parameters
        """
        # Continue only if there are fixed spatial extension parameters
        if len(self._fixed_pars) > 0:

            # Loop over fixed parameters
            for fixed_par in self._fixed_pars:

                # Free parameter
                name = fixed_par['name']
                par  = fixed_par['par']
                self.obs().models()[name][par].free()

                # Log action
                value = '%s[%s]' % (name, par)
                self._log_value(gammalib.EXPLICIT, 'Free fixed parameter', value)

            # Clear list of fixed parameters
            self._fixed_pars = []

        # Return
        return

    # Public methods
    def run(self):
        """
        Run the script
        """
        # Switch screen logging on in debug mode
        if self._logDebug():
            self._log.cout(True)

        # Get parameters
        self._get_parameters()

        # Log header
        self._log_header1(gammalib.NORMAL, 'Input observations')

        # Log input observations
        self._log_string(gammalib.NORMAL, str(self.obs()))

        # Get parameters and initialise some variables
        niter = self['max_iter'].integer()
        eps   = self['like_accuracy'].real()
        delta = 0.0

        # Log header
        self._log_header1(gammalib.NORMAL,
                          'Iterative maximum likelihood model fitting')

        # Loop over iterations
        for iter in range(niter):

            # Log iteration header
            self._log_header2(gammalib.EXPLICIT, 'Iteration %d' % (iter+1))

            # Update observations
            self._update_obs()

            # Optionally fix or free fixed spatial extension parameters
            if self['fix_ext_for_initial'].boolean():
                if iter == 0:
                    self._fix_ext_pars()
                else:
                    self._free_fixed_ext_pars()

            # Fit model
            self.obs().optimize(self.opt())

            # Compute logL difference after first iteration
            if iter > 0:
                delta = logL - self.opt().value()

            # Store maximum likelihood value
            logL = self.opt().value()

            # Log maximum likelihood
            if iter == 0:
                result = '%.5f' % (logL)
            else:
                result = '%.5f (%.5f)' % (logL, delta)
            self._log_value(gammalib.NORMAL, 'logL after iteration %d' % (iter+1),
                            result)

            # Optionally log results
            self._log_string(gammalib.VERBOSE, str(self.obs().models()))

            # Check for convergence
            if iter > 0:
                if delta < eps:
                    break

        # Log header
        self._log_header1(gammalib.EXPLICIT,
                          'Final maximum likelihood model fitting')

        # Do final model fit
        self._final_model_fit()

        # Compute logL difference and store maximum likelihood value
        delta  = logL - self.opt().value()
        logL   = self.opt().value()

        # Log final maximum likelihood
        result = '%.5f (%.5f)' % (logL, delta)
        self._log_value(gammalib.NORMAL, 'logL after final iteration',
                        result)

        # Log results
        self._log_header1(gammalib.NORMAL,
                          'Maximum likelihood optimisation results')
        self._log_string(gammalib.NORMAL, str(self.opt()))
        self._log_string(gammalib.NORMAL, str(self.obs().models()))

        # Return
        return

    def save(self):
        """ 
        Save observation definition file
        """
        # Log header
        self._log_header1(gammalib.TERSE, 'Save observations')

        # Get output filenames
        outobs   = self['outobs'].filename()
        outmodel = self['outmodel'].filename()

        # If file exists and clobber flag is false then raise an exception
        if outobs.exists() and not self['clobber'].boolean():
            msg = ('Cannot save "'+outobs.url()+'": File already exists. '
                   'Use parameter clobber=yes to allow overwriting of files.')
            raise RuntimeError(msg)
        elif outmodel.exists() and not self['clobber'].boolean():
            msg = ('Cannot save "'+outmodel.url()+'": File already exists. '
                   'Use parameter clobber=yes to allow overwriting of files.')
            raise RuntimeError(msg)

        # Otherwise log filename and save file
        else:

            # Get DRB file suffix and set outfolder
            suffix    = self['suffix'].string()
            outfolder = self['outfolder'].string()

            # Create outfolder directory
            try:
                os.makedirs(gammalib.expand_env(outfolder))
            except OSError:
                pass

            # Loop over all observations
            for obs in self.obs():

                # Skip non-COMPTEL observations
                if obs.classname() != 'GCOMObservation':
                    continue

                # Store background filename
                drename = '%s/%s' % (outfolder, os.path.basename(obs.drename().url()))
                if suffix == '':
                    drbname = drename.replace('dre', 'drb')
                else:
                    drbname = drename.replace('dre', 'drb-%s' % suffix)
                obs.drbname(drbname)

                # Save DRB file
                obs.drb().save(drbname, self['clobber'].boolean())

                # Log saving
                self._log_value(gammalib.NORMAL, 'DRB file', drbname)

            # Log observation definition filename
            self._log_value(gammalib.NORMAL, 'Obs. definition XML file',
                                             outobs.url())

            # Save observations
            self.obs().save(outobs)

            # Log model definition filename
            self._log_value(gammalib.NORMAL, 'Model definition XML file',
                                             outmodel.url())

            # Save models
            self.obs().models().save(outmodel)

        # Return
        return


# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':

    # Create instance of application
    app = comlixfit(sys.argv)

    # Execute application
    app.execute()