Commit eac5f7f40e7250ce4d643fa23e92ea816ed60f6e
1 parent
d7df3c0e
Adapt to new model interface.
Showing
6 changed files
with
78 additions
and
32 deletions
inst/cta/test/test_gauss.py
| @@ -35,11 +35,13 @@ def gauss(dir, sigma): | @@ -35,11 +35,13 @@ def gauss(dir, sigma): | ||
| 35 | # Compute derivative | 35 | # Compute derivative |
| 36 | dh = 0.0001 | 36 | dh = 0.0001 |
| 37 | s = GSkyDir() | 37 | s = GSkyDir() |
| 38 | + e = GEnergy() | ||
| 39 | + t = GTime() | ||
| 38 | f1 = GModelSpatialRadialGauss(s, sigma) | 40 | f1 = GModelSpatialRadialGauss(s, sigma) |
| 39 | f2 = GModelSpatialRadialGauss(s, sigma + dh) | 41 | f2 = GModelSpatialRadialGauss(s, sigma + dh) |
| 40 | - v1 = f1.eval_gradients(dir) | 42 | + v1 = f1.eval_gradients(GPhoton(dir, e, t)) |
| 41 | g1 = f1[2].gradient() | 43 | g1 = f1[2].gradient() |
| 42 | - v2 = f2.eval_gradients(dir) | 44 | + v2 = f2.eval_gradients(GPhoton(dir, e, t)) |
| 43 | g2 = f2[2].gradient() | 45 | g2 = f2[2].gradient() |
| 44 | g = (v2 - v1) / dh | 46 | g = (v2 - v1) / dh |
| 45 | 47 | ||
| @@ -79,10 +81,12 @@ def show_gaussian(sigma): | @@ -79,10 +81,12 @@ def show_gaussian(sigma): | ||
| 79 | f_expected = [] | 81 | f_expected = [] |
| 80 | sigma_rad = sigma * (pi / 180.0) | 82 | sigma_rad = sigma * (pi / 180.0) |
| 81 | norm = 1.0 / (2.0 * pi * sigma_rad * sigma_rad) | 83 | norm = 1.0 / (2.0 * pi * sigma_rad * sigma_rad) |
| 84 | + eng = GEnergy() | ||
| 85 | + time = GTime() | ||
| 82 | for t in theta: | 86 | for t in theta: |
| 83 | s = GSkyDir() | 87 | s = GSkyDir() |
| 84 | s.radec_deg(0.0, t) | 88 | s.radec_deg(0.0, t) |
| 85 | - f = gauss.eval(s) | 89 | + f = gauss.eval(GPhoton(s, eng, time)) |
| 86 | e = norm * exp(-0.5 * t * t / sigma / sigma) | 90 | e = norm * exp(-0.5 * t * t / sigma / sigma) |
| 87 | f_gauss.append(f) | 91 | f_gauss.append(f) |
| 88 | f_expected.append(e) | 92 | f_expected.append(e) |
inst/cta/test/test_model.py
| @@ -36,7 +36,7 @@ def ptsrc_model(ra=0.0, dec=0.0): | @@ -36,7 +36,7 @@ def ptsrc_model(ra=0.0, dec=0.0): | ||
| 36 | spatial = GModelSpatialPointSource(pos) | 36 | spatial = GModelSpatialPointSource(pos) |
| 37 | 37 | ||
| 38 | # Set spectral model | 38 | # Set spectral model |
| 39 | - spectral = GModelSpectralPlaw(1.0, -2.0, 100.0) | 39 | + spectral = GModelSpectralPlaw(1.0, -2.0, GEnergy(100.0, "MeV")) |
| 40 | 40 | ||
| 41 | # Set sky model | 41 | # Set sky model |
| 42 | model = GModelSky(spatial, spectral) | 42 | model = GModelSky(spatial, spectral) |
| @@ -60,7 +60,7 @@ def shell_model(ra=0.3, dec=0.3, radius=0.3, width=0.1): | @@ -60,7 +60,7 @@ def shell_model(ra=0.3, dec=0.3, radius=0.3, width=0.1): | ||
| 60 | radial = GModelSpatialRadialShell(center, radius, width, False) | 60 | radial = GModelSpatialRadialShell(center, radius, width, False) |
| 61 | 61 | ||
| 62 | # Set spectral model | 62 | # Set spectral model |
| 63 | - spectral = GModelSpectralPlaw(1.0, -2.0, 100.0) | 63 | + spectral = GModelSpectralPlaw(1.0, -2.0, GEnergy(100.0, "MeV")) |
| 64 | 64 | ||
| 65 | # Set sky model | 65 | # Set sky model |
| 66 | model = GModelSky(radial, spectral) | 66 | model = GModelSky(radial, spectral) |
| @@ -84,7 +84,7 @@ def disk_model(ra=359.6, dec=-0.2, radius=0.4): | @@ -84,7 +84,7 @@ def disk_model(ra=359.6, dec=-0.2, radius=0.4): | ||
| 84 | radial = GModelSpatialRadialDisk(center, radius) | 84 | radial = GModelSpatialRadialDisk(center, radius) |
| 85 | 85 | ||
| 86 | # Set spectral model | 86 | # Set spectral model |
| 87 | - spectral = GModelSpectralPlaw(1.0, -2.0, 100.0) | 87 | + spectral = GModelSpectralPlaw(1.0, -2.0, GEnergy(100.0, "MeV")) |
| 88 | 88 | ||
| 89 | # Set sky model | 89 | # Set sky model |
| 90 | model = GModelSky(radial, spectral) | 90 | model = GModelSky(radial, spectral) |
| @@ -108,7 +108,7 @@ def gauss_model(ra=359.6, dec=+0.1, sigma=0.2): | @@ -108,7 +108,7 @@ def gauss_model(ra=359.6, dec=+0.1, sigma=0.2): | ||
| 108 | radial = GModelSpatialRadialGauss(center, sigma) | 108 | radial = GModelSpatialRadialGauss(center, sigma) |
| 109 | 109 | ||
| 110 | # Set spectral model | 110 | # Set spectral model |
| 111 | - spectral = GModelSpectralPlaw(1.0, -2.0, 100.0) | 111 | + spectral = GModelSpectralPlaw(1.0, -2.0, GEnergy(100.0, "MeV")) |
| 112 | 112 | ||
| 113 | # Set sky model | 113 | # Set sky model |
| 114 | model = GModelSky(radial, spectral) | 114 | model = GModelSky(radial, spectral) |
| @@ -128,7 +128,7 @@ def diffuse_model(file="../../../test/data/cena_lobes_parkes.fits"): | @@ -128,7 +128,7 @@ def diffuse_model(file="../../../test/data/cena_lobes_parkes.fits"): | ||
| 128 | spatial = GModelSpatialDiffuseMap(file) | 128 | spatial = GModelSpatialDiffuseMap(file) |
| 129 | 129 | ||
| 130 | # Set spectral model | 130 | # Set spectral model |
| 131 | - spectral = GModelSpectralPlaw(1.0, -2.0, 100.0) | 131 | + spectral = GModelSpectralPlaw(1.0, -2.0, GEnergy(100.0, "MeV")) |
| 132 | 132 | ||
| 133 | # Set sky model | 133 | # Set sky model |
| 134 | model = GModelSky(spatial, spectral) | 134 | model = GModelSky(spatial, spectral) |
inst/mwl/test/test_python.py
| @@ -93,7 +93,7 @@ def fit_spectrum(filename, xmlname): | @@ -93,7 +93,7 @@ def fit_spectrum(filename, xmlname): | ||
| 93 | yval = 0.0 | 93 | yval = 0.0 |
| 94 | for model in obs.models(): | 94 | for model in obs.models(): |
| 95 | if model.type() == "PointSource": | 95 | if model.type() == "PointSource": |
| 96 | - yval += model.value(d, e, t) * energy * energy * 1.6021765e-6 | 96 | + yval += model.value(GPhoton(d, e, t)) * energy * energy * 1.6021765e-6 |
| 97 | y.append(yval) | 97 | y.append(yval) |
| 98 | plt.plot(x, y) | 98 | plt.plot(x, y) |
| 99 | 99 |
test/example_model_xml_io.py
| 1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
| 2 | -# ========================================================================== | ||
| 3 | -# This script builds a set of models, saves the models into an XML file and | ||
| 4 | -# loads the models back into memory. | ||
| 5 | -# ========================================================================== | ||
| 6 | - | ||
| 7 | -"""IO example: GModels <--> XML file""" | ||
| 8 | - | 2 | +# ===================================================================== |
| 3 | +# This script builds a set of models, saves the models into an XML file | ||
| 4 | +# and loads the models back into memory. | ||
| 5 | +# | ||
| 6 | +# Copyright (C) 2011-2013 Juergen Knoedlseder | ||
| 7 | +# | ||
| 8 | +# This program is free software: you can redistribute it and/or modify | ||
| 9 | +# it under the terms of the GNU General Public License as published by | ||
| 10 | +# the Free Software Foundation, either version 3 of the License, or | ||
| 11 | +# (at your option) any later version. | ||
| 12 | +# | ||
| 13 | +# This program is distributed in the hope that it will be useful, | ||
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | +# GNU General Public License for more details. | ||
| 17 | +# | ||
| 18 | +# You should have received a copy of the GNU General Public License | ||
| 19 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 20 | +# ===================================================================== | ||
| 9 | from gammalib import * | 21 | from gammalib import * |
| 10 | 22 | ||
| 11 | # | 23 | # |
| @@ -21,7 +33,7 @@ center.radec_deg(42.0, 43.0) | @@ -21,7 +33,7 @@ center.radec_deg(42.0, 43.0) | ||
| 21 | # Spectral index: -2.1 | 33 | # Spectral index: -2.1 |
| 22 | # =================================================== | 34 | # =================================================== |
| 23 | point_spatial = GModelSpatialPointSource(center) | 35 | point_spatial = GModelSpatialPointSource(center) |
| 24 | -point_spectrum = GModelSpectralPlaw(1.0e-7, -2.1, 100.0) | 36 | +point_spectrum = GModelSpectralPlaw(1.0e-7, -2.1, GEnergy(100.0, "MeV")) |
| 25 | point = GModelSky(point_spatial, point_spectrum) | 37 | point = GModelSky(point_spatial, point_spectrum) |
| 26 | point.name('My point source') | 38 | point.name('My point source') |
| 27 | 39 | ||
| @@ -32,7 +44,7 @@ point.name('My point source') | @@ -32,7 +44,7 @@ point.name('My point source') | ||
| 32 | # Spectral index: -2.4 | 44 | # Spectral index: -2.4 |
| 33 | # =================================================== | 45 | # =================================================== |
| 34 | gauss_spatial = GModelSpatialRadialGauss(center, 3.0) | 46 | gauss_spatial = GModelSpatialRadialGauss(center, 3.0) |
| 35 | -gauss_spectrum = GModelSpectralPlaw(4.2e-7, -2.4, 100.0) | 47 | +gauss_spectrum = GModelSpectralPlaw(4.2e-7, -2.4, GEnergy(100.0, "MeV")) |
| 36 | gauss = GModelSky(gauss_spatial, gauss_spectrum) | 48 | gauss = GModelSky(gauss_spatial, gauss_spectrum) |
| 37 | gauss.name('My Gaussian source') | 49 | gauss.name('My Gaussian source') |
| 38 | 50 |
test/example_radial_models.py
| 1 | #! /usr/bin/env python | 1 | #! /usr/bin/env python |
| 2 | -# ========================================================================== | ||
| 3 | -# This script makes an image from a radial model and verifies that the model | ||
| 4 | -# is correctly normalized. | ||
| 5 | -# ========================================================================== | 2 | +# ===================================================================== |
| 3 | +# This script makes an image from a radial model and verifies that the | ||
| 4 | +# model is correctly normalized. | ||
| 5 | +# | ||
| 6 | +# Copyright (C) 2011-2013 Juergen Knoedlseder | ||
| 7 | +# | ||
| 8 | +# This program is free software: you can redistribute it and/or modify | ||
| 9 | +# it under the terms of the GNU General Public License as published by | ||
| 10 | +# the Free Software Foundation, either version 3 of the License, or | ||
| 11 | +# (at your option) any later version. | ||
| 12 | +# | ||
| 13 | +# This program is distributed in the hope that it will be useful, | ||
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | +# GNU General Public License for more details. | ||
| 17 | +# | ||
| 18 | +# You should have received a copy of the GNU General Public License | ||
| 19 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 20 | +# ===================================================================== | ||
| 6 | from gammalib import * | 21 | from gammalib import * |
| 7 | 22 | ||
| 8 | 23 | ||
| @@ -10,30 +25,34 @@ from gammalib import * | @@ -10,30 +25,34 @@ from gammalib import * | ||
| 10 | # Radial model testing routine # | 25 | # Radial model testing routine # |
| 11 | # ============================ # | 26 | # ============================ # |
| 12 | def test_radial_model(name, model, clobber=True): | 27 | def test_radial_model(name, model, clobber=True): |
| 13 | - """Make an image of a spatial model | ||
| 14 | - and check that it integrates to 1""" | 28 | + """ |
| 29 | + Make an image of a spatial model and check that it integrates to 1. | ||
| 30 | + """ | ||
| 15 | # Make a 2D FITS image (cartesian projection, celestial coordinates) | 31 | # Make a 2D FITS image (cartesian projection, celestial coordinates) |
| 16 | ra, dec, binsz, npix = 0, 0, 0.01, 300 | 32 | ra, dec, binsz, npix = 0, 0, 0.01, 300 |
| 17 | image = GSkymap("CAR", "CEL", ra, dec, -binsz, binsz, npix, npix, 1) | 33 | image = GSkymap("CAR", "CEL", ra, dec, -binsz, binsz, npix, npix, 1) |
| 18 | 34 | ||
| 19 | # Fill the image | 35 | # Fill the image |
| 36 | + energy = GEnergy() | ||
| 37 | + time = GTime() | ||
| 20 | for pix in range(image.npix()): | 38 | for pix in range(image.npix()): |
| 21 | - dir = image.pix2dir(pix) | ||
| 22 | - theta = center.dist(dir) | ||
| 23 | - image[pix] = model.eval(theta) | 39 | + dir = image.pix2dir(pix) |
| 40 | + theta = center.dist(dir) | ||
| 41 | + image[pix] = model.eval(theta, energy, time) | ||
| 24 | 42 | ||
| 25 | # Write it to file | 43 | # Write it to file |
| 26 | filename = name + '.fits' | 44 | filename = name + '.fits' |
| 27 | - print 'Writing', filename | 45 | + print('Writing', filename) |
| 28 | image.save(filename, clobber) | 46 | image.save(filename, clobber) |
| 29 | 47 | ||
| 30 | # Check that it integrates to 1 | 48 | # Check that it integrates to 1 |
| 31 | integral = 0.0 | 49 | integral = 0.0 |
| 32 | for pix in range(image.npix()): | 50 | for pix in range(image.npix()): |
| 33 | integral += image.omega(pix) * image[pix] | 51 | integral += image.omega(pix) * image[pix] |
| 34 | - print 'integral = %g (should be 1)' % integral | ||
| 35 | - print 'integral error = %g (should be 0)' % (integral - 1) | ||
| 36 | 52 | + print('integral = %g (should be 1)' % integral) | |
| 53 | + print('integral error = %g (should be 0)' % (integral - 1)) | ||
| 54 | + print('') | ||
| 55 | + | ||
| 37 | 56 | ||
| 38 | # ================ # | 57 | # ================ # |
| 39 | # Main entry point # | 58 | # Main entry point # |
testall.sh
| @@ -2,7 +2,7 @@ | @@ -2,7 +2,7 @@ | ||
| 2 | # | 2 | # |
| 3 | # Run all test and example scripts that come with the package | 3 | # Run all test and example scripts that come with the package |
| 4 | # | 4 | # |
| 5 | -# Copyright (C) 2011 Juergen Knoedlseder | 5 | +# Copyright (C) 2011-2013 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 |
| @@ -20,6 +20,17 @@ | @@ -20,6 +20,17 @@ | ||
| 20 | base=$PWD | 20 | base=$PWD |
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | +# examples | ||
| 24 | +# ======== | ||
| 25 | +echo | ||
| 26 | +echo "=====> examples" | ||
| 27 | +cd examples/python | ||
| 28 | +./models_howto.py | ||
| 29 | +./xml_howto.py | ||
| 30 | +./xml_html_create.py | ||
| 31 | +cd $base | ||
| 32 | + | ||
| 33 | + | ||
| 23 | # test | 34 | # test |
| 24 | # ==== | 35 | # ==== |
| 25 | echo | 36 | echo |