Commit b6640c4fa29999918c7e1e0508227273e6aed5d0

Authored by Michael Mayer
1 parent 907508c6

Added code snippets to retrieve analysis results in python

doc/source/user_manual/getting_started/python.rst
... ... @@ -354,6 +354,109 @@ The ``obsutils.sim()`` function then simulates the event data, the
354 354 functions will be added in the future.
355 355  
356 356  
  357 +Access common analysis results
  358 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  359 +Here are some examples how to access your analysis results in python. In the following it is
  360 +assumed that you have a ctlike object called 'like' which is setup and runs:
  361 +
  362 +.. code-block:: python
  363 +
  364 + import gammalib
  365 + import ctools
  366 + like = ctools.ctlike(sim.obs())
  367 + like.run()
  368 + ...
  369 +
  370 +* Best-fit parameters:
  371 +
  372 + The following command shows how to access the fit parameters and errors:
  373 +
  374 + .. code-block:: python
  375 +
  376 + obs = like.obs() # Get observations object
  377 + obs.models()["Crab"]["Prefactor"].value() # This returns the actual fit value
  378 + obs.models()["Crab"]["Prefactor"].error() # This returns the actual fit error
  379 + ...
  380 +
  381 +* Open XML model file from python and print models on screen:
  382 +
  383 + .. code-block:: python
  384 +
  385 + models = gammalib.GModels("$CTOOLS/share/models/crab.xml")
  386 + print models
  387 + ...
  388 +
  389 +* Likelihood value
  390 +
  391 + .. code-block:: python
  392 +
  393 + like.opt().value() # Returns likelihood value
  394 + ...
  395 +
  396 +* Covariance Matrix
  397 +
  398 + .. code-block:: python
  399 +
  400 + covariance = like.opt().curvature().invert() # Return GMatrix object
  401 + print covariance
  402 + ...
  403 +
  404 +Modify and work on XML models
  405 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  406 +
  407 +* Fix a certain parameter
  408 +
  409 + .. code-block:: python
  410 +
  411 + models["Crab"]["Prefactor"].fix()
  412 + ...
  413 +
  414 +* Release a certain parameter
  415 +
  416 + .. code-block:: python
  417 +
  418 + models["Crab"]["Prefactor"].free()
  419 + ...
  420 +
  421 +* Check if parameter is free
  422 +
  423 + .. code-block:: python
  424 +
  425 + models["Crab"]["Prefactor"].is_free()
  426 + ...
  427 +
  428 +* Set parameter value
  429 +
  430 + .. code-block:: python
  431 +
  432 + models["Crab"]["Index"].value(-2.5)
  433 + ...
  434 +
  435 +* Set parameter boundaries
  436 +
  437 + .. code-block:: python
  438 +
  439 + models["Crab"]["Index"].min(-5.0)
  440 + models["Crab"]["Index"].max(-1.0)
  441 + ...
  442 +
  443 +* Loop over models and parameters (e.g. fix all parameters)
  444 +
  445 + .. code-block:: python
  446 +
  447 + for model in models: # Loop over models
  448 + for par in model: # Loop over parameters
  449 + par.fix() # fix parameter
  450 + ...
  451 +
  452 +* Save modified XML model
  453 +
  454 + .. code-block:: python
  455 +
  456 + models.save("myXMLmodel.xml")
  457 + ...
  458 +
  459 +
357 460 Beyond the first steps
358 461 ~~~~~~~~~~~~~~~~~~~~~~
359 462  
... ...
scripts/csspec.py
... ... @@ -294,6 +294,8 @@ class csspec(ctools.cscript):
294 294 TSvalues = gammalib.GFitsTableDoubleCol("TS", self.m_ebounds.size())
295 295 ulim_values = gammalib.GFitsTableDoubleCol("UpperLimit", self.m_ebounds.size())
296 296 ulim_values.unit("erg/cm2/s")
  297 + Npred_values = gammalib.GFitsTableDoubleCol("Npred", self.m_ebounds.size())
  298 +
297 299  
298 300 # Loop over energy bins
299 301 for i in range(self.m_ebounds.size()):
... ... @@ -459,6 +461,7 @@ class csspec(ctools.cscript):
459 461 flux_err[i] = 0.0
460 462 TSvalues[i] = 0.0
461 463 ulim_values[i] = 0.0
  464 + Npred_values[i] = 0.0
462 465 continue
463 466  
464 467 # Get results
... ... @@ -491,6 +494,11 @@ class csspec(ctools.cscript):
491 494 TS = -1.0
492 495 if self.m_calc_ts:
493 496 TS = source.ts()
  497 +
  498 + # Compute Npred value
  499 + Npred = 0.0
  500 + for observation in like.obs():
  501 + Npred += observation.npred(source)
494 502  
495 503 # Get differential flux
496 504 fitted_flux = source.spectral().eval(elogmean,gammalib.GTime())
... ... @@ -503,6 +511,9 @@ class csspec(ctools.cscript):
503 511 # Set values for storage
504 512 TSvalues[i] = TS
505 513  
  514 + # Set npred values
  515 + Npred_values[i] = Npred
  516 +
506 517 # Convert fluxes to nuFnu
507 518 flux[i] = fitted_flux * elogmean2 * gammalib.MeV2erg
508 519 flux_err[i] = e_flux * elogmean2 * gammalib.MeV2erg
... ... @@ -528,6 +539,7 @@ class csspec(ctools.cscript):
528 539 table.append(flux_err)
529 540 table.append(TSvalues)
530 541 table.append(ulim_values)
  542 + table.append(Npred_values)
531 543  
532 544 # Create the FITS file now
533 545 self.fits = gammalib.GFits()
... ...