Commit c4bc62ff836737b19b80f089abf65d7f73d0693e

Authored by Jürgen Knödlseder
1 parent 0a9552e7

Allow using spectral models without a 'Prefactor' parameter in cssens and cstsdist (#1889)

ChangeLog
1 -2019-07-23 1 +2019-10-31
2 2
3 * Version 1.7.0 released 3 * Version 1.7.0 released
4 ======================== 4 ========================
5 5
6 Remove response cache before source testing in cttsmap (#2973) 6 Remove response cache before source testing in cttsmap (#2973)
7 Enabling running of example scripts from objects in memory (#2925) 7 Enabling running of example scripts from objects in memory (#2925)
  8 + Allow using spectral models without "Prefactor" parameter in cssens
  9 + and cstsdist (#1889)
8 10
9 11
10 2019-07-15 12 2019-07-15
1 New Features and Important Changes in ctools 1.7.0 1 New Features and Important Changes in ctools 1.7.0
2 2
3 -23 July 2019 3 +31 October 2019
4 4
5 5
6 Introduction 6 Introduction
@@ -254,7 +254,8 @@ None @@ -254,7 +254,8 @@ None
254 254
255 cssens - Computes sensitivity curve 255 cssens - Computes sensitivity curve
256 ----------------------------------- 256 -----------------------------------
257 -None 257 +Allow using spectral models without "Prefactor" parameter for sensitivity
  258 +computation (#1889).
258 259
259 260
260 csspec - Generates Spectral Energy Distribution 261 csspec - Generates Spectral Energy Distribution
@@ -269,7 +270,8 @@ None @@ -269,7 +270,8 @@ None
269 270
270 cstsdist - Computes Test Statistic distribution 271 cstsdist - Computes Test Statistic distribution
271 ----------------------------------------------- 272 -----------------------------------------------
272 -None 273 +Allow using spectral models without "Prefactor" parameter for computation of
  274 +TS distribution (#1889).
273 275
274 276
275 cstsmapmerge - Merge Test Statistic maps 277 cstsmapmerge - Merge Test Statistic maps
@@ -304,7 +306,9 @@ None @@ -304,7 +306,9 @@ None
304 306
305 modutils - Model utilities 307 modutils - Model utilities
306 -------------------------- 308 --------------------------
307 -None 309 +Allow using spectral models without "Prefactor" parameter as test source in
  310 +test_source() function. Add normalisation_parameter() function to retrieve
  311 +spectral normalisation parameter from any model (#1889).
308 312
309 313
310 mputils - Multiprocessing utilities 314 mputils - Multiprocessing utilities
cscripts/cssens.py
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 # ========================================================================== 2 # ==========================================================================
3 # Computes the array sensitivity using the Test Statistic for a test source 3 # Computes the array sensitivity using the Test Statistic for a test source
4 # 4 #
5 -# Copyright (C) 2011-2018 Juergen Knoedlseder 5 +# Copyright (C) 2011-2019 Juergen Knoedlseder
6 # 6 #
7 # This program is free software: you can redistribute it and/or modify 7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by 8 # it under the terms of the GNU General Public License as published by
@@ -377,13 +377,14 @@ class cssens(ctools.csobservation): @@ -377,13 +377,14 @@ class cssens(ctools.csobservation):
377 # Write header for iteration into logger 377 # Write header for iteration into logger
378 self._log_header2(gammalib.EXPLICIT, 'Iteration '+str(iterations)) 378 self._log_header2(gammalib.EXPLICIT, 'Iteration '+str(iterations))
379 379
380 - # Create a copy of the test models, set the prefactor of the test  
381 - # source in the models, and append the models to the observation.  
382 - # "crab_prefactor" is the Prefactor that corresponds to a flux of  
383 - # 1 Crab. 380 + # Create a copy of the test models, set the normalisation parameter
  381 + # of the test source in the models, and append the models to the
  382 + # observation. "crab_prefactor" is the prefactor that corresponds
  383 + # to a flux of 1 Crab.
384 models = test_model.copy() 384 models = test_model.copy()
385 - crab_prefactor = models[self._srcname]['Prefactor'].value() * crab_unit  
386 - models[self._srcname]['Prefactor'].value(crab_prefactor * test_crab_flux) 385 + prefactor = modutils.normalisation_parameter(models[self._srcname])
  386 + crab_prefactor = prefactor.value() * crab_unit
  387 + prefactor.value(crab_prefactor * test_crab_flux)
387 self.obs().models(models) 388 self.obs().models(models)
388 389
389 # Simulate events for the models. "sim" holds an observation 390 # Simulate events for the models. "sim" holds an observation
@@ -422,7 +423,8 @@ class cssens(ctools.csobservation): @@ -422,7 +423,8 @@ class cssens(ctools.csobservation):
422 ts = source.ts() 423 ts = source.ts()
423 424
424 # Get fitted Crab, photon and energy fluxes 425 # Get fitted Crab, photon and energy fluxes
425 - crab_flux = source['Prefactor'].value() / crab_prefactor 426 + prefactor = modutils.normalisation_parameter(source)
  427 + crab_flux = prefactor.value() / crab_prefactor
426 photon_flux = source.spectral().flux(emin, emax) 428 photon_flux = source.spectral().flux(emin, emax)
427 energy_flux = source.spectral().eflux(emin, emax) 429 energy_flux = source.spectral().eflux(emin, emax)
428 430
@@ -679,7 +681,7 @@ class cssens(ctools.csobservation): @@ -679,7 +681,7 @@ class cssens(ctools.csobservation):
679 681
680 # Set test source model for this observation 682 # Set test source model for this observation
681 self._models = modutils.test_source(self.obs().models(), self._srcname, 683 self._models = modutils.test_source(self.obs().models(), self._srcname,
682 - ra=self._ra, dec=self._dec) 684 + ra=self._ra, dec=self._dec)
683 685
684 # Write observation into logger 686 # Write observation into logger
685 self._log_observations(gammalib.NORMAL, self.obs(), 'Input observation') 687 self._log_observations(gammalib.NORMAL, self.obs(), 'Input observation')
cscripts/cstsdist.py
@@ -232,9 +232,10 @@ class cstsdist(ctools.csobservation): @@ -232,9 +232,10 @@ class cstsdist(ctools.csobservation):
232 for par in model: 232 for par in model:
233 self._log_string(gammalib.EXPLICIT, str(par)) 233 self._log_string(gammalib.EXPLICIT, str(par))
234 elif self._logNormal(): 234 elif self._logNormal():
235 - name = 'Trial %d' % seed  
236 - value = 'TS=%.3f Prefactor=%e +/- %e' % \  
237 - (ts, model['Prefactor'].value(), model['Prefactor'].error()) 235 + prefactor = modutils.normalisation_parameter(model)
  236 + name = 'Trial %d' % seed
  237 + value = 'TS=%.3f %s=%e +/- %e' % \
  238 + (ts, prefactor.name(), prefactor.value(), prefactor.error())
238 self._log_value(gammalib.TERSE, name, value) 239 self._log_value(gammalib.TERSE, name, value)
239 240
240 # Initialise results 241 # Initialise results
cscripts/modutils.py
1 # ========================================================================== 1 # ==========================================================================
2 # Utility functions for model handling 2 # Utility functions for model handling
3 # 3 #
4 -# Copyright (C) 2016-2017 Juergen Knoedlseder 4 +# Copyright (C) 2016-2019 Juergen Knoedlseder
5 # 5 #
6 # This program is free software: you can redistribute it and/or modify 6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by 7 # it under the terms of the GNU General Public License as published by
@@ -61,10 +61,20 @@ def test_source(models, srcname, ra=None, dec=None, fitspat=False, @@ -61,10 +61,20 @@ def test_source(models, srcname, ra=None, dec=None, fitspat=False,
61 model = outmodels[srcname] 61 model = outmodels[srcname]
62 model.tscalc(True) 62 model.tscalc(True)
63 63
64 - # If source model has no "Prefactor" parameter then raise an exception  
65 - if not model.has_par('Prefactor'):  
66 - msg = ('Model "%s" has no parameter "Prefactor". Only spectral '  
67 - 'models with a "Prefactor" parameter are supported.' % 64 + # Get spectral model component
  65 + spectral = model.spectral()
  66 +
  67 + # If source model has no normalisation parameter then raise an exception
  68 + if not (spectral.has_par('Normalization') or
  69 + spectral.has_par('Value') or
  70 + spectral.has_par('Prefactor') or
  71 + spectral.has_par('Integral') or
  72 + spectral.has_par('PhotonFlux') or
  73 + spectral.has_par('EnergyFlux')):
  74 + msg = ('Model "%s" has no flux normalisation parameter. Only spectral '
  75 + 'models with a flux normalisation parameter (one of '
  76 + '"Normalization", "Value", "Prefactor", "Integral", '
  77 + '"PhotonFlux", and "EnergyFlux") are supported.' %
68 srcname) 78 srcname)
69 raise RuntimeError(msg) 79 raise RuntimeError(msg)
70 80
@@ -100,6 +110,51 @@ def test_source(models, srcname, ra=None, dec=None, fitspat=False, @@ -100,6 +110,51 @@ def test_source(models, srcname, ra=None, dec=None, fitspat=False,
100 return outmodels 110 return outmodels
101 111
102 112
  113 +# ============================================= #
  114 +# Return spectral model normalisation parameter #
  115 +# ============================================= #
  116 +def normalisation_parameter(model):
  117 + """
  118 + Return spectral model normalisation parameter
  119 +
  120 + Parameters
  121 + ----------
  122 + model : `~gammalib.GModel`
  123 + Input model
  124 +
  125 + Returns
  126 + -------
  127 + par : `~gammalib.GModelPar`
  128 + Spectral model normalisation parameter
  129 + """
  130 + # Initialise normalisation parameter as None
  131 + par = None
  132 +
  133 + # Set list of possible normalisation parameters
  134 + norm_pars = ['Normalization', 'Value', 'Prefactor', 'Integral',
  135 + 'PhotonFlux', 'EnergyFlux']
  136 +
  137 + # Get spectral model component
  138 + spectral = model.spectral()
  139 +
  140 + # Find normalisation parameter
  141 + for norm_par in norm_pars:
  142 + if spectral.has_par(norm_par):
  143 + par = spectral[norm_par]
  144 +
  145 + # If no normalisation parameter then raise an exception
  146 + if par == None:
  147 + msg = ('Model "%s" has no flux normalisation parameter. Only spectral '
  148 + 'models with a flux normalisation parameter (one of '
  149 + '"Normalization", "Value", "Prefactor", "Integral", '
  150 + '"PhotonFlux", and "EnergyFlux") are supported.' %
  151 + model.name())
  152 + raise RuntimeError(msg)
  153 +
  154 + # Return normalisation parameter
  155 + return par
  156 +
  157 +
103 # ================================= # 158 # ================================= #
104 # Returns attributes for ds9 string # 159 # Returns attributes for ds9 string #
105 # ================================= # 160 # ================================= #
doc/source/admin/release_history/1.7.rst
@@ -26,6 +26,8 @@ Improvements @@ -26,6 +26,8 @@ Improvements
26 Remove response cache before source testing in cttsmap 26 Remove response cache before source testing in cttsmap
27 * [`2925 <https://cta-redmine.irap.omp.eu/issues/2925>`_] - 27 * [`2925 <https://cta-redmine.irap.omp.eu/issues/2925>`_] -
28 Enabling running of example scripts from objects in memory 28 Enabling running of example scripts from objects in memory
  29 +* [`1889 <https://cta-redmine.irap.omp.eu/issues/1889>`_] -
  30 + Allow using spectral models without ``Prefactor`` parameter in :ref:`cssens` and :ref:`cstsdist`
29 31
30 32
31 New features 33 New features