{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using ctools from Python" ] }, { "cell_type": "raw", "metadata": { "jupyter.source_hidden": true }, "source": [ "
\"Download
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook you will learn how to use the ctools and cscripts from Python instead of typing the commands in the console.\n", "\n", "ctools provides two Python modules that allow using all tools and scripts as Python classes. To use ctools from Python you have to import the `ctools` and `cscripts` modules into Python. You should also import the `gammalib` module, as ctools without GammaLib is generally not very useful.\n", "\n", "__Warning:__ Always import `gammalib` before you import `ctools` and `cscripts`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import gammalib\n", "import ctools\n", "import cscripts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simulating events\n", "\n", "As an example we will simulate an observation of an hour of the Crab nebula." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "sim = ctools.ctobssim()\n", "sim['inmodel'] = '${CTOOLS}/share/models/crab.xml'\n", "sim['outevents'] = 'events.fits'\n", "sim['caldb'] = 'prod2'\n", "sim['irf'] = 'South_0.5h'\n", "sim['ra'] = 83.5\n", "sim['dec'] = 22.8\n", "sim['rad'] = 5.0\n", "sim['tmin'] = '2020-01-01T00:00:00'\n", "sim['tmax'] = '2020-01-01T01:00:00'\n", "sim['emin'] = 0.03\n", "sim['emax'] = 150.0\n", "sim.execute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first line generates an instance of the ctobssim tool as a Python class. User parameters are then set using the `[ ]` operator. After setting all parameters the `execute()` method is called to execute the `ctobssim` tool. On output the `events.fits` FITS file is created. Until now everything is analogous to running the tool from the command line, but in Python you can easily combine the different blocks into more complex workflows.\n", "\n", "Remember that you can consult the manual of each tool to find out how it works and to discover all the input parameters that you can set." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\r\n", "ctobssim\r\n", "========\r\n", "\r\n", "Simulate event list(s).\r\n", "\r\n", "\r\n", "Synopsis\r\n", "--------\r\n", "\r\n", "This tool simulates event list(s) using the instrument characteristics \r\n", "specified by the instrument response function(s) and an input model. The \r\n", "simulation includes photon events from astrophysical sources and background\r\n", "events from an instrumental background model.\r\n", "\r\n", "By default, ctobssim creates a single event list. ctobssim queries a pointing\r\n", "direction, the radius of the simulation region, a time interval, an energy\r\n", "interval, an instrumental response function, and an input model. ctobssim uses\r\n", "a numerical random number generator for the simulations with a seed value\r\n", "provided by the hidden seed parameter. Changing this parameter for\r\n", "subsequent runs will lead to different event samples.\r\n", "\r\n", "ctobssim performs a safety check on the maximum photon rate for all model\r\n", "components to avoid that the tool locks up and requests huge memory \r\n", "resources, which may happen if a mistake was made in setting up the input \r\n", "model (for example if an error in the flux units is made). The maximum allowed\r\n", "photon rate is controlled by the hidden maxrate parameter, which by default \r\n", "is set to 1e6.\r\n", "\r\n", "ctobssim can also generate multiple event lists if an observation definition \r\n", "file is specified on input using the hidden inobs parameter. In that \r\n", "case, simulation information will be gathered from the file, and for each \r\n", "observation an event list will be created.\r\n", "\r\n", "For each event file, the simulation parameters will be written as data\r\n", "selection keywords to the FITS header. These keywords are mandatory for any\r\n", "unbinned maximum likelihood analysis of the event data.\r\n", "\r\n", "\r\n", "General parameters\r\n", "------------------\r\n", "\r\n", "inobs [file]\r\n", " Input event list or observation definition XML file. If provided (i.e. the\r\n", " parameter is not blank or NONE), the pointing definition and eventually the\r\n", " response information will be extracted from the input file for event\r\n", " simulation.\r\n", "\r\n", "inmodel [file]\r\n", " Input model XML file.\r\n", "\r\n", "caldb [string]\r\n", " Calibration database.\r\n", "\r\n", "irf [string]\r\n", " Instrumental response function.\r\n", "\r\n", "(edisp = no) [boolean]\r\n", " Apply energy dispersion?\r\n", "\r\n", "outevents [file]\r\n", " Output event list or observation definition XML file.\r\n", "\r\n", "(prefix = sim_events_) [string]\r\n", " Prefix for event list in observation definition XML file.\r\n", "\r\n", "(startindex = 1) [integer]\r\n", " Start index of event list in observation definition XML file.\r\n", "\r\n", "(seed = 1) [integer]\r\n", " Integer seed value to be used for Monte Carlo simulations. Keep this \r\n", " parameter at the same value for repeatable simulations, or increment \r\n", " this value for subsequent runs if non-repeatable simulations are\r\n", " required.\r\n", "\r\n", "ra [real]\r\n", " Right Ascension of CTA pointing (J2000, in degrees).\r\n", "\r\n", "dec [real]\r\n", " Declination of CTA pointing (J2000, in degrees).\r\n", "\r\n", "rad [real]\r\n", " Radius of CTA field of view (simulation cone radius) (in degrees).\r\n", "\r\n", "tmin [time]\r\n", " Start time (UTC string, JD, MJD or MET in seconds).\r\n", "\r\n", "tmax [time]\r\n", " Stop time (UTC string, JD, MJD or MET in seconds).\r\n", "\r\n", "(mjdref = 51544.5) [real]\r\n", " Reference Modified Julian Day (MJD) for simulated events. The times in\r\n", " seconds for each event are counted from this reference time on.\r\n", "\r\n", "emin [real]\r\n", " Lower energy limit of simulated events (in TeV).\r\n", "\r\n", "emax [real]\r\n", " Upper energy limit of simulated events (in TeV).\r\n", "\r\n", "(deadc = 0.95) [real]\r\n", " Average deadtime correction factor.\r\n", "\r\n", "(maxrate = 1.0e6) [real]\r\n", " Maximum photon rate for source models. Source models that exceed this\r\n", " maximum photon rate will lead to an exception as very likely the\r\n", " specified model normalisation is too large (probably due to the\r\n", " a misinterpretation of units). Note that ctools specifies intensity\r\n", " units per MeV.\r\n", "\r\n", "\r\n", "Standard parameters\r\n", "-------------------\r\n", "\r\n", "(nthreads = 0) [integer]\r\n", " Number of parallel processes (0=use all available CPUs).\r\n", "\r\n", "(publish = no) [boolean]\r\n", " Specifies whether the event list(s) should be published on VO Hub.\r\n", "\r\n", "(chatter = 2) [integer]\r\n", " Verbosity of the executable:\r\n", " chatter = 0: no information will be logged\r\n", "\r\n", " chatter = 1: only errors will be logged\r\n", "\r\n", " chatter = 2: errors and actions will be logged\r\n", "\r\n", " chatter = 3: report about the task execution\r\n", "\r\n", " chatter = 4: detailed report about the task execution\r\n", "\r\n", "(clobber = yes) [boolean]\r\n", " Specifies whether existing files should be overwritten.\r\n", "\r\n", "(debug = no) [boolean]\r\n", " Enables debug mode. In debug mode the executable will dump any log file output to the console.\r\n", "\r\n", "(mode = ql) [string]\r\n", " Mode of automatic parameters (default is ql, i.e. \"query and learn\").\r\n", "\r\n", "(logfile = ctobssim.log) [string]\r\n", " Name of log file.\r\n", "\r\n", "\r\n", "Related tools or scripts\r\n", "------------------------\r\n", "\r\n", "csobsdef\r\n" ] } ], "source": [ "!ctobssim --help" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In a Jupyter notebook a code line starting with `!` is executed in the shell, so you can do the operation above just from the command line.\n", "\n", "One of the advantages of using ctools from Python is that you can run a tool using" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "sim.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main difference to the `execute()` method is that the `run()` method will not write the output (i.e., the simulated event list) to disk. Why is this useful? Well, after having typed `sim.run()` the `ctobssim` class still exists as an object in memory, including all the simulated events. You can always save to disk later using the `save()` method.\n", "\n", "The `ctobssim` class has an `obs()` method that returns an observation container that holds the simulated CTA observation with its associated events. To visualise this container, type:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "=== GObservations ===\n", " Number of observations ....: 1\n", " Number of models ..........: 2\n", " Number of observed events .: 204568\n", " Number of predicted events : 0\n" ] } ], "source": [ "print(sim.obs())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is one CTA observation in the container and to visualise that observation type:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "=== GCTAObservation ===\n", " Name ......................: \n", " Identifier ................: 000001\n", " Instrument ................: CTA\n", " Event file ................: events.fits\n", " Event type ................: EventList\n", " Statistic .................: cstat\n", " Ontime ....................: 3599.99999976158 s\n", " Livetime ..................: 3527.99999976635 s\n", " Deadtime correction .......: 0.98\n", " User energy range .........: undefined\n", "=== GCTAPointing ===\n", " Pointing direction ........: (RA,Dec)=(83.5,22.8)\n", "=== GCTAResponseIrf ===\n", " Caldb mission .............: cta\n", " Caldb instrument ..........: prod2\n", " Response name .............: South_0.5h\n", " Energy dispersion .........: Not used\n", " Safe energy range .........: undefined\n", "=== GCTAEventList ===\n", " Number of events ..........: 204568 (disposed in \"events.fits\")\n", " Time interval .............: 58849.0008007407 - 58849.0424674074 days\n", " Energy interval ...........: 0.03 - 150 TeV\n", " Region of interest ........: RA=83.5, DEC=22.8 [0,0] Radius=5 deg\n", "=== GSkyRegions ===\n", " Number of regions .........: 0\n" ] } ], "source": [ "print(sim.obs()[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The observation contains a CTA event list that is implement by the GammaLib class `GCTAEventList`. You can access the event list using the `events()` method. To visualise the individual events you can iterate over the events using a for loop. This will show the simulated celestial coordinates `(RA, DEC)`, the coordinate in the camera system `[DETX, DETY]`, the energies and the terrestrial times (TT) of all events. Let's peek at the first events of the list:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dir=RA=83.7821807861328, DEC=22.0288944244385 [-0.0134540141482956,0.00456559645656115] Energy=36.9588024914265 GeV Time=315532804.286922 s (TT)\n" ] } ], "source": [ "events = sim.obs()[0].events()\n", "for event in events[:1]:\n", " print(event)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use this feature to inspect some of the event properties, for example look at their energy spectrum. For this we will use the Python packages matplotlib. If you do not have matplotlib you can use another plotting package of your choice or skip this step." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAERCAYAAAB4jRxOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGP1JREFUeJzt3XuwpHV95/H3Z2TBoFGjm0ACMuAapEK56yULXnbLVoyg\n7ooxbgnE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0682nhluBhwF/ApDkYuD9TR6r+X7z+u2TTirJBfRWPq2sDHokfZ8MJHCE\nL0mt4QhfklrCgi9JLWHBl6SWsOBLUktY8CWpJf4/YSiW8RnJDJkAAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "#this will visualize plots inline\n", "\n", "ax = plt.subplot()\n", "ax.set_yscale('log')\n", "ax.set_xlabel('Log10(Energy [TeV])')\n", "\n", "energies = []\n", "for event in events:\n", " energies.append(event.energy().log10TeV())\n", " \n", "n, bins, patches = plt.hist(energies)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fitting a model to the observations\n", "\n", "We can use the observation in memory to directly run a likelihood fit." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "like = ctools.ctlike(sim.obs())\n", "like.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is very compact. Where do we define the model fit to the data? Where are the user parameters? `ctlike` doesn’t in fact need any parameters as all the relevant information is already contained in the observation container produced by the ctobssim class. Indeed, you constructed the ctlike instance by using the ctobssim observation container as constructor argument.\n", "\n", "An observation container, implemented by the `GObservations` class of GammaLib, is the fundamental brick of any ctools analysis. An observation container can hold more than events, e.g., in this case it also holds the model that was used to generate the events.\n", "\n", "Many tools and scripts handle observation containers, and accept them upon construction and return them after running the tool via the `obs()` method. Passing observation containers between ctools classes is a very convenient and powerful way of building in-memory analysis pipelines. However, this implies that you need some computing ressources when dealing with large observation containers (for example if you want to analyse a few 100 hours of data at once). Also, if the script crashes the information is lost.\n", "\n", "To check how the fit went you can inspect the optimiser used by `ctlike` by typing:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "=== GOptimizerLM ===\n", " Optimized function value ..: 766517.033\n", " Absolute precision ........: 0.005\n", " Acceptable value decrease .: 2\n", " Optimization status .......: converged\n", " Number of parameters ......: 10\n", " Number of free parameters .: 4\n", " Number of iterations ......: 2\n", " Lambda ....................: 1e-05\n" ] } ], "source": [ "print(like.opt())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You see that the fit converged after 2 iterations. Out of 10 parameters in the model 4 have been fitted (the others were kept fixed). To inspect the fit results you can print the model container that can be access using the `models()` method of the observation container:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "=== GModels ===\n", " Number of models ..........: 2\n", " Number of parameters ......: 10\n", "=== GModelSky ===\n", " Name ......................: Crab\n", " Instruments ...............: all\n", " Observation identifiers ...: all\n", " Model type ................: PointSource\n", " Model components ..........: \"PointSource\" * \"PowerLaw\" * \"Constant\"\n", " Number of parameters ......: 6\n", " Number of spatial par's ...: 2\n", " RA .......................: 83.6331 [-360,360] deg (fixed,scale=1)\n", " DEC ......................: 22.0145 [-90,90] deg (fixed,scale=1)\n", " Number of spectral par's ..: 3\n", " Prefactor ................: 5.71641983396841e-16 +/- 6.16044728001869e-18 [1e-23,1e-13] ph/cm2/s/MeV (free,scale=1e-16,gradient)\n", " Index ....................: -2.47207578184696 +/- 0.00792255637026554 [-0,-5] (free,scale=-1,gradient)\n", " PivotEnergy ..............: 300000 [10000,1000000000] MeV (fixed,scale=1000000,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n", " Number of scale par's .....: 0\n", "=== GCTAModelIrfBackground ===\n", " Name ......................: CTABackgroundModel\n", " Instruments ...............: CTA\n", " Observation identifiers ...: all\n", " Model type ................: \"PowerLaw\" * \"Constant\"\n", " Number of parameters ......: 4\n", " Number of spectral par's ..: 3\n", " Prefactor ................: 0.999547413672001 +/- 0.00722086618569711 [0.001,1000] ph/cm2/s/MeV (free,scale=1,gradient)\n", " Index ....................: -0.000149454940530402 +/- 0.00255125105744512 [-5,5] (free,scale=1,gradient)\n", " PivotEnergy ..............: 1000000 [10000,1000000000] MeV (fixed,scale=1000000,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n" ] } ], "source": [ "print(like.obs().models())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, in this way we can fetch the minimum of the optimized function (the negative of the natural logarithm of the likelihood) to compare different model hypotheses." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "766517.033411\n" ] } ], "source": [ "like1 = like.opt().value()\n", "print(like1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose you want to repeat the fit by optimising also the position of the point source. This is easy from Python, as you can modify the model and fit interactively. Type the following:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "=== GModels ===\n", " Number of models ..........: 2\n", " Number of parameters ......: 10\n", "=== GModelSky ===\n", " Name ......................: Crab\n", " Instruments ...............: all\n", " Observation identifiers ...: all\n", " Model type ................: PointSource\n", " Model components ..........: \"PointSource\" * \"PowerLaw\" * \"Constant\"\n", " Number of parameters ......: 6\n", " Number of spatial par's ...: 2\n", " RA .......................: 83.6321513539243 +/- 0.000642709906923956 [-360,360] deg (free,scale=1)\n", " DEC ......................: 22.015365038759 +/- 0.000605599773766818 [-90,90] deg (free,scale=1)\n", " Number of spectral par's ..: 3\n", " Prefactor ................: 5.71414829301837e-16 +/- 6.15917760942954e-18 [1e-23,1e-13] ph/cm2/s/MeV (free,scale=1e-16,gradient)\n", " Index ....................: -2.47211138483201 +/- 0.0079230679574118 [-0,-5] (free,scale=-1,gradient)\n", " PivotEnergy ..............: 300000 [10000,1000000000] MeV (fixed,scale=1000000,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n", " Number of scale par's .....: 0\n", "=== GCTAModelIrfBackground ===\n", " Name ......................: CTABackgroundModel\n", " Instruments ...............: CTA\n", " Observation identifiers ...: all\n", " Model type ................: \"PowerLaw\" * \"Constant\"\n", " Number of parameters ......: 4\n", " Number of spectral par's ..: 3\n", " Prefactor ................: 0.999543391635431 +/- 0.0072207997895289 [0.001,1000] ph/cm2/s/MeV (free,scale=1,gradient)\n", " Index ....................: -0.000148647508899144 +/- 0.0025512389825989 [-5,5] (free,scale=1,gradient)\n", " PivotEnergy ..............: 1000000 [10000,1000000000] MeV (fixed,scale=1000000,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n" ] } ], "source": [ "like.obs().models()['Crab']['RA'].free()\n", "like.obs().models()['Crab']['DEC'].free()\n", "like.run()\n", "print(like.obs().models())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can quantify the improvement of the model by comparing the new value of the optimized function with the previous one." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.21736054635\n" ] } ], "source": [ "like2 = like.opt().value()\n", "ts = -2.0 * (like2 - like1)\n", "print(ts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The test statistic (TS) is expected to be distributed as a $\\chi^2_n$ with a number of degrees of freedom $n$ equal to the additional number of degrees of freedom of the (second) more complex model with respect to the (first) more parsimonious one, for our case two degrees of freedom. The chance probability that the likelihood improved that much because of pure statistical fluctuations is the integral from TS to infinity of the chi square with two degrees of freedom. In this case the improvement is negligible (i.e., the chance probability is very high), as expected since in the model the source is already at the true position." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generating a log file\n", "\n", "By default, tools and scripts run from Python will not generate a log file. The reason for this is that Python scripts are often used to build ctools analysis pipelines and workflows, and one generally does not want that such a script pollutes the workspace with log files. You can however instruct a ctool or cscript to generate a log file as follows:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": true }, "outputs": [], "source": [ "like.logFileOpen()\n", "like.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This creates a log file named ``ctlike.log`` in your local work space that you can visualise using any editor." ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 2 }