Commit 7f1dc4d6e4558e584f8d349071b6494412ad2fbb

Authored by Christoph Deil
1 parent 8d3246b3

Some Python code cleanup

.gitignore
... ... @@ -6,6 +6,7 @@ pyext/build/
6 6 .libs
7 7 .deps
8 8 test/*.fits
  9 +examples/python/*.fits
9 10 inst/cta/test/*.xml
10 11 inst/cta/test/*.fits
11 12 inst/cta/test/*.fits.gz
... ...
examples/python/example_model_xml_io.py
... ... @@ -18,13 +18,14 @@
18 18 # You should have received a copy of the GNU General Public License
19 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20 # =====================================================================
21   -from gammalib import *
  21 +import gammalib
  22 +from gammalib import GEnergy
22 23  
23 24 #
24 25 # Set the location / centre of the source model to
25 26 # Right Ascension = 42.0 deg and Declination = 43 deg
26 27 # ===================================================
27   -centre = GSkyDir()
  28 +centre = gammalib.GSkyDir()
28 29 centre.radec_deg(42.0, 43.0)
29 30  
30 31 #
... ... @@ -32,9 +33,9 @@ centre.radec_deg(42.0, 43.0)
32 33 # Normalization: 1.0e-7 ph/cm2/s/MeV @ 100 MeV
33 34 # Spectral index: -2.1
34 35 # ===================================================
35   -point_spatial = GModelSpatialPointSource(centre)
36   -point_spectrum = GModelSpectralPlaw(1.0e-7, -2.1, GEnergy(100.0, "MeV"))
37   -point = GModelSky(point_spatial, point_spectrum)
  36 +point_spatial = gammalib.GModelSpatialPointSource(centre)
  37 +point_spectrum = gammalib.GModelSpectralPlaw(1.0e-7, -2.1, GEnergy(100.0, "MeV"))
  38 +point = gammalib.GModelSky(point_spatial, point_spectrum)
38 39 point.name('My point source')
39 40  
40 41 #
... ... @@ -43,9 +44,9 @@ point.name(&#39;My point source&#39;)
43 44 # Normalization: 4.2e-7 ph/cm2/s/MeV @ 100 MeV
44 45 # Spectral index: -2.4
45 46 # ===================================================
46   -gauss_spatial = GModelSpatialRadialGauss(centre, 3.0)
47   -gauss_spectrum = GModelSpectralPlaw(4.2e-7, -2.4, GEnergy(100.0, "MeV"))
48   -gauss = GModelSky(gauss_spatial, gauss_spectrum)
  47 +gauss_spatial = gammalib.GModelSpatialRadialGauss(centre, 3.0)
  48 +gauss_spectrum = gammalib.GModelSpectralPlaw(4.2e-7, -2.4, GEnergy(100.0, "MeV"))
  49 +gauss = gammalib.GModelSky(gauss_spatial, gauss_spectrum)
49 50 gauss.name('My Gaussian source')
50 51  
51 52 #
... ... @@ -53,7 +54,7 @@ gauss.name(&#39;My Gaussian source&#39;)
53 54 # models to the model container. Print the content of the
54 55 # container
55 56 # =========================================================
56   -models = GModels()
  57 +models = gammalib.GModels()
57 58 models.append(point)
58 59 models.append(gauss)
59 60 print(models)
... ... @@ -70,6 +71,6 @@ del models
70 71 # Now read the models back from the XML file into the container.
71 72 # Print again the content of the container.
72 73 # ==============================================================
73   -models2 = GModels(filename)
  74 +models2 = gammalib.GModels(filename)
74 75 print("")
75 76 print(models2)
... ...
examples/python/example_radial_models.py
... ... @@ -18,7 +18,7 @@
18 18 # You should have received a copy of the GNU General Public License
19 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20 # =====================================================================
21   -from gammalib import *
  21 +import gammalib
22 22  
23 23  
24 24 # ============================ #
... ... @@ -30,11 +30,11 @@ def test_radial_model(name, model, clobber=True):
30 30 """
31 31 # Make a 2D FITS image (cartesian projection, celestial coordinates)
32 32 ra, dec, binsz, npix = 0, 0, 0.01, 300
33   - image = GSkymap("CAR", "CEL", ra, dec, -binsz, binsz, npix, npix, 1)
  33 + image = gammalib.GSkymap("CAR", "CEL", ra, dec, -binsz, binsz, npix, npix, 1)
34 34  
35 35 # Fill the image
36   - energy = GEnergy()
37   - time = GTime()
  36 + energy = gammalib.GEnergy()
  37 + time = gammalib.GTime()
38 38 for inx in range(image.npix()):
39 39 dir = image.inx2dir(inx)
40 40 theta = centre.dist(dir)
... ... @@ -62,10 +62,15 @@ if __name__ == &#39;__main__&#39;:
62 62 Test radial model integration.
63 63 """
64 64 # Set model location / centre
65   - centre = GSkyDir()
  65 + centre = gammalib.GSkyDir()
66 66 centre.radec_deg(0.3, 0.1)
67 67  
68 68 # Test models
69   - test_radial_model(name='gauss', model=GModelSpatialRadialGauss(centre, 0.3))
70   - test_radial_model(name='disk', model=GModelSpatialRadialDisk(centre, 0.8))
71   - test_radial_model(name='shell', model=GModelSpatialRadialShell(centre, 0.5, 0.1))
  69 + model = gammalib.GModelSpatialRadialGauss(centre, 0.3)
  70 + test_radial_model(name='gauss', model=model)
  71 +
  72 + model = gammalib.GModelSpatialRadialDisk(centre, 0.8)
  73 + test_radial_model(name='disk', model=model)
  74 +
  75 + model = gammalib.GModelSpatialRadialShell(centre, 0.5, 0.1)
  76 + test_radial_model(name='shell', model=model)
... ...
test/test_GApplication.py
... ... @@ -17,10 +17,7 @@
17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18 #
19 19 # ==========================================================================
20   -from gammalib import *
21   -from math import *
22   -import os
23   -import sys
  20 +from gammalib import GPythonTestSuite, GLog, GApplicationPars
24 21  
25 22  
26 23 # ========================================== #
... ...
test/test_GFits.py
... ... @@ -17,16 +17,15 @@
17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18 #
19 19 # ==========================================================================
20   -from gammalib import *
21   -from math import *
  20 +import math
22 21 import os
23 22 import sys
24   -
  23 +import gammalib
25 24  
26 25 # =================================== #
27 26 # Test class for GammaLib FITS module #
28 27 # =================================== #
29   -class Test(GPythonTestSuite):
  28 +class Test(gammalib.GPythonTestSuite):
30 29 """
31 30 Test class for GammaLib FITS module.
32 31 """
... ... @@ -35,7 +34,7 @@ class Test(GPythonTestSuite):
35 34 Constructor.
36 35 """
37 36 # Call base class constructor
38   - GPythonTestSuite.__init__(self)
  37 + gammalib.GPythonTestSuite.__init__(self)
39 38  
40 39 # Return
41 40 return
... ... @@ -69,21 +68,21 @@ class Test(GPythonTestSuite):
69 68 pass
70 69  
71 70 # Create FITS file
72   - fits = GFits(file1, True)
  71 + fits = gammalib.GFits(file1, True)
73 72 sys.stdout.write(".")
74 73  
75 74 # Create images
76 75 nx = 10
77 76 ny = 10
78   - img1 = GFitsImageByte(nx, ny)
79   - img2 = GFitsImageDouble(nx, ny)
80   - img3 = GFitsImageFloat(nx, ny)
81   - img4 = GFitsImageLong(nx, ny)
82   - img5 = GFitsImageLongLong(nx, ny)
83   - img6 = GFitsImageSByte(nx, ny)
84   - img7 = GFitsImageShort(nx, ny)
85   - img8 = GFitsImageULong(nx, ny)
86   - img9 = GFitsImageUShort(nx, ny)
  77 + img1 = gammalib.GFitsImageByte(nx, ny)
  78 + img2 = gammalib.GFitsImageDouble(nx, ny)
  79 + img3 = gammalib.GFitsImageFloat(nx, ny)
  80 + img4 = gammalib.GFitsImageLong(nx, ny)
  81 + img5 = gammalib.GFitsImageLongLong(nx, ny)
  82 + img6 = gammalib.GFitsImageSByte(nx, ny)
  83 + img7 = gammalib.GFitsImageShort(nx, ny)
  84 + img8 = gammalib.GFitsImageULong(nx, ny)
  85 + img9 = gammalib.GFitsImageUShort(nx, ny)
87 86 for x in range(nx):
88 87 for y in range(ny):
89 88 img1[x, y] = x + y * nx
... ... @@ -127,17 +126,17 @@ class Test(GPythonTestSuite):
127 126  
128 127 # Create table columns
129 128 nrows = 10
130   - col1 = GFitsTableBitCol("BIT", nrows)
131   - col2 = GFitsTableBoolCol("BOOLEAN", nrows)
132   - col3 = GFitsTableByteCol("BYTE", nrows)
133   - col4 = GFitsTableDoubleCol("DOUBLE", nrows)
134   - col5 = GFitsTableFloatCol("FLOAT", nrows)
135   - col6 = GFitsTableLongCol("LONG", nrows)
136   - col7 = GFitsTableLongLongCol("LONGLONG", nrows)
137   - col8 = GFitsTableShortCol("SHORT", nrows)
138   - col9 = GFitsTableStringCol("STRING", nrows, 20)
139   - col10 = GFitsTableULongCol("ULONG", nrows)
140   - col11 = GFitsTableUShortCol("USHORT", nrows)
  129 + col1 = gammalib.GFitsTableBitCol("BIT", nrows)
  130 + col2 = gammalib.GFitsTableBoolCol("BOOLEAN", nrows)
  131 + col3 = gammalib.GFitsTableByteCol("BYTE", nrows)
  132 + col4 = gammalib.GFitsTableDoubleCol("DOUBLE", nrows)
  133 + col5 = gammalib.GFitsTableFloatCol("FLOAT", nrows)
  134 + col6 = gammalib.GFitsTableLongCol("LONG", nrows)
  135 + col7 = gammalib.GFitsTableLongLongCol("LONGLONG", nrows)
  136 + col8 = gammalib.GFitsTableShortCol("SHORT", nrows)
  137 + col9 = gammalib.GFitsTableStringCol("STRING", nrows, 20)
  138 + col10 = gammalib.GFitsTableULongCol("ULONG", nrows)
  139 + col11 = gammalib.GFitsTableUShortCol("USHORT", nrows)
141 140 for i in range(nrows):
142 141 col1[i] = i % 2
143 142 col2[i] = i % 2
... ... @@ -153,7 +152,7 @@ class Test(GPythonTestSuite):
153 152 sys.stdout.write(".")
154 153  
155 154 # Set ASCII table
156   - tbl_ascii = GFitsAsciiTable()
  155 + tbl_ascii = gammalib.GFitsAsciiTable()
157 156 # tbl_ascii.append(col1) # Need to implement ?/!
158 157 # tbl_ascii.append(col2) # Need to implement ?/!
159 158 tbl_ascii.append(col3)
... ... @@ -170,7 +169,7 @@ class Test(GPythonTestSuite):
170 169 sys.stdout.write(".")
171 170  
172 171 # Set binary table
173   - tbl_bin = GFitsBinTable()
  172 + tbl_bin = gammalib.GFitsBinTable()
174 173 tbl_bin.append(col1)
175 174 tbl_bin.append(col2)
176 175 tbl_bin.append(col3)
... ... @@ -196,7 +195,7 @@ class Test(GPythonTestSuite):
196 195 sys.stdout.write(".")
197 196  
198 197 # Re-open FITS file
199   - fits = GFits(file1)
  198 + fits = gammalib.GFits(file1)
200 199 sys.stdout.write(".")
201 200  
202 201 # Get double precision image, take square root of pixel and save in
... ... @@ -204,7 +203,7 @@ class Test(GPythonTestSuite):
204 203 img_double = fits.image("Double")
205 204 for x in range(nx):
206 205 for y in range(ny):
207   - img_double[x, y] = sqrt(img_double[x, y])
  206 + img_double[x, y] = math.sqrt(img_double[x, y])
208 207 sys.stdout.write(".")
209 208  
210 209 # Save into another FITS file
... ...
test/test_column_insert_remove.py
1 1 #! /usr/bin/env python
2 2  
3   -from gammalib import *
4   -
  3 +import gammalib
5 4  
6 5 def test_one(a):
7 6 a[0] = 1
8 7 a[1] = 2
9 8 a[2] = 3
10   - print a
  9 + print(a)
11 10 return a
12 11  
13 12  
... ... @@ -18,18 +17,18 @@ def test_two(a):
18 17 a.remove(1, 2)
19 18 a.remove(1, 1)
20 19 a.remove(0, 1)
21   - print a
  20 + print(a)
22 21 return a
23 22  
24 23  
25 24 def test_three(a):
26 25 a.insert(0, 10)
27   - print a
  26 + print(a)
28 27 a[7] = 99
29 28 a.remove(8, 2)
30 29 a[0] = 99
31 30 a.remove(1, 5)
32   - print a
  31 + print(a)
33 32 return a
34 33  
35 34  
... ... @@ -38,7 +37,7 @@ def set(a):
38 37 a[1] = 65536
39 38 a[2] = True
40 39 # a[3] = "Hallo"
41   - print a[1]
  40 + print(a[1])
42 41  
43 42  
44 43 #==========================#
... ... @@ -49,17 +48,17 @@ if __name__ == &#39;__main__&#39;:
49 48 Perform testing.
50 49 """
51 50 # Perform tests
52   - # a = GFitsTableBitCol("Test",4)
53   - # a = GFitsTableBoolCol("Test",4)
54   - # a = GFitsTableByteCol("Test",4)
55   - # a = GFitsTableDoubleCol("Test",4)
56   - # a = GFitsTableFloatCol("Test",4)
57   - # a = GFitsTableLongCol("Test",4)
58   - # a = GFitsTableLongLongCol("Test",4)
59   - # a = GFitsTableShortCol("Test",4)
60   - # a = GFitsTableStringCol("Test",4,10)
61   - # a = GFitsTableULongCol("Test",4)
62   - a = GFitsTableUShortCol("Test", 4)
  51 + # a = gammalib.GFitsTableBitCol("Test",4)
  52 + # a = gammalib.GFitsTableBoolCol("Test",4)
  53 + # a = gammalib.GFitsTableByteCol("Test",4)
  54 + # a = gammalib.GFitsTableDoubleCol("Test",4)
  55 + # a = gammalib.GFitsTableFloatCol("Test",4)
  56 + # a = gammalib.GFitsTableLongCol("Test",4)
  57 + # a = gammalib.GFitsTableLongLongCol("Test",4)
  58 + # a = gammalib.GFitsTableShortCol("Test",4)
  59 + # a = gammalib.GFitsTableStringCol("Test",4,10)
  60 + # a = gammalib.GFitsTableULongCol("Test",4)
  61 + a = gammalib.GFitsTableUShortCol("Test", 4)
63 62 a = test_one(a)
64 63 a = test_two(a)
65 64 a = test_three(a)
... ...
test/test_diffuse_cube.py
... ... @@ -22,9 +22,8 @@
22 22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 23 #
24 24 # ==========================================================================
25   -from gammalib import *
26   -from math import *
27   -
  25 +import math
  26 +from gammalib import GEnergy, GTime, GRan, GModels, GEbounds, GSkyDir
28 27  
29 28 # ======================== #
30 29 # Simulate CTA observation #
... ... @@ -94,7 +93,7 @@ def show_photons(photons, xmlname, e_min, e_max, area, duration, dir, radius, eb
94 93 counts[index] = counts[index] + 1.0
95 94  
96 95 # Create error bars
97   - error = [sqrt(c) for c in counts]
  96 + error = [math.sqrt(c) for c in counts]
98 97  
99 98 # Get model values
100 99 models = GModels(xmlname)
... ...
test/test_file_function.py
... ... @@ -18,9 +18,8 @@
18 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19 #
20 20 # ==========================================================================
21   -from gammalib import *
22 21 import math
23   -
  22 +from gammalib import GEnergy, GModelSpectralFunc, GRan, GModelSpectralPlaw
24 23  
25 24 # ================================= #
26 25 # Generate file function from model #
... ... @@ -74,7 +73,7 @@ def compare_photon_flux(model, file_function, emin=8.7, emax=1123.0, steps=13):
74 73 flux_model = model.flux(e_min, e_max)
75 74 flux_ff = file_function.flux(e_min, e_max)
76 75 if (abs(flux_model - flux_ff) > eps):
77   - print e_min, e_max, flux_model, flux_ff, flux_model - flux_ff
  76 + print(e_min, e_max, flux_model, flux_ff, flux_model - flux_ff)
78 77 n_violate += 1
79 78  
80 79 # Ramp up emax from above first node
... ... @@ -87,7 +86,7 @@ def compare_photon_flux(model, file_function, emin=8.7, emax=1123.0, steps=13):
87 86 flux_model = model.flux(e_min, e_max)
88 87 flux_ff = file_function.flux(e_min, e_max)
89 88 if (abs(flux_model - flux_ff) > eps):
90   - print e_min, e_max, flux_model, flux_ff, flux_model - flux_ff
  89 + print(e_min, e_max, flux_model, flux_ff, flux_model - flux_ff)
91 90 n_violate += 1
92 91  
93 92 # Ramp down emin from above last node
... ... @@ -99,7 +98,7 @@ def compare_photon_flux(model, file_function, emin=8.7, emax=1123.0, steps=13):
99 98 flux_model = model.flux(e_min, e_max)
100 99 flux_ff = file_function.flux(e_min, e_max)
101 100 if (abs(flux_model - flux_ff) > eps):
102   - print e_min, e_max, flux_model, flux_ff, flux_model - flux_ff
  101 + print(e_min, e_max, flux_model, flux_ff, flux_model - flux_ff)
103 102 n_violate += 1
104 103  
105 104 # Ramp down emin from below last node
... ... @@ -112,14 +111,14 @@ def compare_photon_flux(model, file_function, emin=8.7, emax=1123.0, steps=13):
112 111 flux_model = model.flux(e_min, e_max)
113 112 flux_ff = file_function.flux(e_min, e_max)
114 113 if (abs(flux_model - flux_ff) > eps):
115   - print e_min, e_max, flux_model, flux_ff, flux_model - flux_ff
  114 + print(e_min, e_max, flux_model, flux_ff, flux_model - flux_ff)
116 115 n_violate += 1
117 116  
118 117 # Print summary
119 118 if n_violate == 0:
120   - print "compare_photon_flux: All tests satisfied accuracy."
  119 + print("compare_photon_flux: All tests satisfied accuracy.")
121 120 else:
122   - print "compare_photon_flux: " + str(n_violate) + " tests exceeded accuracy."
  121 + print("compare_photon_flux: " + str(n_violate) + " tests exceeded accuracy.")
123 122  
124 123 # Return
125 124 return
... ... @@ -149,7 +148,7 @@ def compare_energy_flux(model, file_function, emin=8.7, emax=1123.0, steps=13):
149 148 flux_model = model.eflux(e_min, e_max)
150 149 flux_ff = file_function.eflux(e_min, e_max)
151 150 if (abs(flux_model - flux_ff) > eps):
152   - print e_min, e_max, flux_model, flux_ff, flux_model - flux_ff
  151 + print(e_min, e_max, flux_model, flux_ff, flux_model - flux_ff)
153 152 n_violate += 1
154 153  
155 154 # Ramp up emax from above first node
... ... @@ -162,7 +161,7 @@ def compare_energy_flux(model, file_function, emin=8.7, emax=1123.0, steps=13):
162 161 flux_model = model.eflux(e_min, e_max)
163 162 flux_ff = file_function.eflux(e_min, e_max)
164 163 if (abs(flux_model - flux_ff) > eps):
165   - print e_min, e_max, flux_model, flux_ff, flux_model - flux_ff
  164 + print(e_min, e_max, flux_model, flux_ff, flux_model - flux_ff)
166 165 n_violate += 1
167 166  
168 167 # Ramp down emin from above last node
... ... @@ -174,7 +173,7 @@ def compare_energy_flux(model, file_function, emin=8.7, emax=1123.0, steps=13):
174 173 flux_model = model.eflux(e_min, e_max)
175 174 flux_ff = file_function.eflux(e_min, e_max)
176 175 if (abs(flux_model - flux_ff) > eps):
177   - print e_min, e_max, flux_model, flux_ff, flux_model - flux_ff
  176 + print(e_min, e_max, flux_model, flux_ff, flux_model - flux_ff)
178 177 n_violate += 1
179 178  
180 179 # Ramp down emin from below last node
... ... @@ -187,14 +186,14 @@ def compare_energy_flux(model, file_function, emin=8.7, emax=1123.0, steps=13):
187 186 flux_model = model.eflux(e_min, e_max)
188 187 flux_ff = file_function.eflux(e_min, e_max)
189 188 if (abs(flux_model - flux_ff) > eps):
190   - print e_min, e_max, flux_model, flux_ff, flux_model - flux_ff
  189 + print(e_min, e_max, flux_model, flux_ff, flux_model - flux_ff)
191 190 n_violate += 1
192 191  
193 192 # Print summary
194 193 if n_violate == 0:
195   - print "compare_energy_flux: All tests satisfied accuracy."
  194 + print("compare_energy_flux: All tests satisfied accuracy.")
196 195 else:
197   - print "compare_energy_flux: " + str(n_violate) + " tests exceeded accuracy."
  196 + print("compare_energy_flux: " + str(n_violate) + " tests exceeded accuracy.")
198 197  
199 198 # Return
200 199 return
... ... @@ -218,7 +217,7 @@ def test_mc(file_function, trials=10):
218 217  
219 218 # Loop over trials
220 219 for i in range(trials):
221   - print file_function.mc(e_min, e_max, ran)
  220 + print(file_function.mc(e_min, e_max, ran))
222 221  
223 222 # Return
224 223 return
... ...