Commit cf33f7280e1d2310d2d509673862fe6156dc51ea

Authored by Jürgen Knödlseder
2 parents 370ee4fa 10b8ae6a

Merge branch '1501-add-documentation-for-python-interface' into integration

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
... ... @@ -281,19 +281,21 @@ class csspec(ctools.cscript):
281 281 table.card("TELESCOP", "CTA", "Name of Telescope")
282 282  
283 283 # Create FITS table columns
284   - energy = gammalib.GFitsTableDoubleCol("Energy", self.m_ebounds.size())
  284 + energy = gammalib.GFitsTableDoubleCol("Energy", self.m_ebounds.size())
285 285 energy.unit("TeV")
286   - energy_low = gammalib.GFitsTableDoubleCol("ed_Energy", self.m_ebounds.size())
  286 + energy_low = gammalib.GFitsTableDoubleCol("ed_Energy", self.m_ebounds.size())
287 287 energy_low.unit("TeV")
288 288 energy_high = gammalib.GFitsTableDoubleCol("eu_Energy", self.m_ebounds.size())
289 289 energy_high.unit("TeV")
290   - flux = gammalib.GFitsTableDoubleCol("Flux", self.m_ebounds.size())
  290 + flux = gammalib.GFitsTableDoubleCol("Flux", self.m_ebounds.size())
291 291 flux.unit("erg/cm2/s")
292   - flux_err = gammalib.GFitsTableDoubleCol("e_Flux", self.m_ebounds.size())
  292 + flux_err = gammalib.GFitsTableDoubleCol("e_Flux", self.m_ebounds.size())
293 293 flux_err.unit("erg/cm2/s")
294   - TSvalues = gammalib.GFitsTableDoubleCol("TS", self.m_ebounds.size())
295   - ulim_values = gammalib.GFitsTableDoubleCol("UpperLimit", self.m_ebounds.size())
  294 + TSvalues = gammalib.GFitsTableDoubleCol("TS", self.m_ebounds.size())
  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()):
... ... @@ -455,10 +457,11 @@ class csspec(ctools.cscript):
455 457 self.log("No event in this bin. Bin is skipped\n")
456 458  
457 459 # Set all values to 0
458   - flux[i] = 0.0
459   - flux_err[i] = 0.0
460   - TSvalues[i] = 0.0
461   - ulim_values[i] = 0.0
  460 + flux[i] = 0.0
  461 + flux_err[i] = 0.0
  462 + TSvalues[i] = 0.0
  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
... ... @@ -520,7 +531,7 @@ class csspec(ctools.cscript):
520 531 self.log(", UL = "+str(ulim_values[i])+" [erg/cm2/s]")
521 532 self.log("\n")
522 533  
523   - # Append filles columns to fits table
  534 + # Append filled columns to fits table
524 535 table.append(energy)
525 536 table.append(energy_low)
526 537 table.append(energy_high)
... ... @@ -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()
... ...