Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
adonath committed Jan 29, 2015
1 parent d7206c0 commit 2bab6fb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 55 deletions.
2 changes: 1 addition & 1 deletion gammapy/astro/population/simulate.py
Expand Up @@ -12,7 +12,7 @@
from ...utils import coordinates as astrometry
from ...utils.const import d_sun_to_galactic_center
from ...utils.distributions import draw, pdf
from gammapy.morphology.models import morph_types
from ...morphology.models import morph_types
from ...catalog.utils import as_quantity
from ..source import SNR, SNRTrueloveMcKee, PWN, Pulsar
from ..population import Exponential, FaucherSpiral, RMIN, RMAX, ZMIN, ZMAX, radial_distributions
Expand Down
89 changes: 45 additions & 44 deletions gammapy/detect/test_statistics.py
Expand Up @@ -41,11 +41,11 @@ class TSMapResult(Bunch):
Attributes
----------
ts : ndarray
ts : `~numpy.ndarray`
Estimated TS map
amplitude : ndarray
amplitude : `~numpy.ndarray`
Estimated best fit flux amplitude map
niter : ndarray
niter : `~numpy.ndarray`
Number of iterations map
runtime : float
Time needed to compute TS map.
Expand Down Expand Up @@ -78,7 +78,7 @@ def write(self, filename, header, overwrite=False):
hdu_list = fits.HDUList()
if 'MORPH' not in header and hasattr(self, 'morphology'):
header['MORPH'] = self.morphology, 'Source morphology assumption.'
if not type(self.scale) == float:
if not np.isscalar(self.scale):
header['EXTNAME'] = 'scale'
header['HDUNAME'] = 'scale'
header['SCALE'] = 'max', 'Source morphology scale parameter.'
Expand All @@ -100,11 +100,11 @@ def f_cash_root(x, counts, background, model):
----------
x : float
Model amplitude.
counts : array
counts : `~numpy.ndarray`
Count map slice, where model is defined.
background : array
background : `~numpy.ndarray`
Background map slice, where model is defined.
model : array
model : `~numpy.ndarray`
Source template (multiplied with exposure).
"""
return (model * (counts / (x * FLUX_FACTOR * model + background) - 1)).sum()
Expand All @@ -118,11 +118,11 @@ def f_cash(x, counts, background, model):
----------
x : float
Model amplitude.
counts : array
counts : `~numpy.ndarray`
Count map slice, where model is defined.
background : array
background : `~numpy.ndarray`
Background map slice, where model is defined.
model : array
model : `~numpy.ndarray`
Source template (multiplied with exposure).
"""
with np.errstate(invalid='ignore', divide='ignore'):
Expand All @@ -142,12 +142,11 @@ def compute_ts_map_multiscale(maps, psf_parameters, scales=[0], downsample='auto
Parameters
----------
maps : `astropy.io.fits.HDUList`
HDU list containing the data. The list must contain the following HDU
extensions:
* 'On' -- Counts image
* 'Background' -- Background image
* 'Diffuse' -- Diffuse model image
* 'ExpGammaMap' -- Exposure image
HDU list containing the data. The list must contain the following HDU extensions:
* 'On', Counts image
* 'Background', Background image
* 'Diffuse', Diffuse model image
* 'ExpGammaMap', Exposure image
psf_parameters : dict
Dict defining the multi gauss PSF parameters.
See `~gammapy.irf.multi_gauss_psf` for details.
Expand Down Expand Up @@ -195,10 +194,10 @@ def compute_ts_map_multiscale(maps, psf_parameters, scales=[0], downsample='auto
maps_ = {}
for map_, func in zip(maps, funcs):
if downsampled:
maps_[map_.name] = downsample_2N(map_.data, factor, func,
shape=shape_2N(shape))
maps_[map_.name.lower()] = downsample_2N(map_.data, factor, func,
shape=shape_2N(shape))
else:
maps_[map_.name] = map_.data
maps_[map_.name.lower()] = map_.data

# Set up PSF and source kernel
kernel = multi_gauss_psf_kernel(psf_parameters, BINSZ=BINSZ,
Expand All @@ -215,15 +214,17 @@ def compute_ts_map_multiscale(maps, psf_parameters, scales=[0], downsample='auto
x_size = _round_up_to_odd_integer(2 * sigma * (1 + width)
+ kernel.shape[0] / 2)
source_kernel = Model2DKernel(model, x_size=x_size, mode='oversample')
else:
raise ValueError('Unknown morphology model.')
kernel = convolve(source_kernel, kernel)
kernel.normalize()

# Compute TS map
if residual:
background = (maps_['Background'] + maps_['DIFFUSE'] + maps_['OnModel'])
background = (maps_['background'] + maps_['diffuse'] + maps_['OnModel'])
else:
background = maps_['Background'] + maps_['DIFFUSE']
ts_results = compute_ts_map(maps_['On'], background, maps_['ExpGammaMap'],
background = maps_['background'] + maps_['diffuse']
ts_results = compute_ts_map(maps_['on'], background, maps_['expgammamap'],
kernel, *args, **kwargs)
logging.info('TS map computation took {0:.1f} s \n'.format(ts_results.runtime))
ts_results['scale'] = scale
Expand Down Expand Up @@ -280,11 +281,11 @@ def compute_ts_map(counts, background, exposure, kernel, mask=None, flux=None,
Parameters
----------
counts : array
counts : `~numpy.ndarray`
Count map
background : array
background : `~numpy.ndarray`
Background map
exposure : array
exposure : `~numpy.ndarray`
Exposure map
kernel : `astropy.convolution.Kernel2D`
Source model kernel.
Expand Down Expand Up @@ -379,15 +380,15 @@ def _ts_value(position, counts, exposure, background, kernel, flux,
----------
position : tuple (i, j)
Pixel position.
counts : array
counts : `~numpy.ndarray`
Count map.
background : array
background : `~numpy.ndarray`
Background map.
exposure : array
exposure : `~numpy.ndarray`
Exposure map.
kernel : `astropy.convolution.Kernel2D`
Source model kernel.
flux : array
flux : `~numpy.ndarray`
Flux map. The flux value at the given pixel position is used as
starting value for the minimization.
Expand Down Expand Up @@ -441,11 +442,11 @@ def _amplitude_bounds(counts, background, model):
Parameters
----------
counts : array
counts : `~numpy.ndarray`
Count map.
background : array
background : `~numpy.ndarray`
Background map.
model : array
model : `~numpy.ndarray`
Source template (multiplied with exposure).
"""
# Check that counts slice contains at least one count
Expand All @@ -465,11 +466,11 @@ def _root_amplitude(counts, background, model, flux):
Parameters
----------
counts : array
counts : `~numpy.ndarray`
Slice of count map.
background : array
background : `~numpy.ndarray`
Slice of background map.
model : array
model : `~numpy.ndarray`
Model template to fit.
flux : float
Starting value for the fit.
Expand Down Expand Up @@ -499,11 +500,11 @@ def _root_amplitude_brentq(counts, background, model):
Parameters
----------
counts : array
counts : `~numpy.ndarray`
Slice of count map.
background : array
background : `~numpy.ndarray`
Slice of background map.
model : array
model : `~numpy.ndarray`
Model template to fit.
Returns
Expand Down Expand Up @@ -538,11 +539,11 @@ def _fit_amplitude_scipy(counts, background, model, optimizer='Brent'):
Parameters
----------
counts : array
counts : `~numpy.ndarray`
Slice of count map.
background : array
background : `~numpy.ndarray`
Slice of background map.
model : array
model : `~numpy.ndarray`
Model template to fit.
flux : float
Starting value for the fit.
Expand Down Expand Up @@ -572,11 +573,11 @@ def _fit_amplitude_minuit(counts, background, model, flux):
Parameters
----------
counts : array
counts : `~numpy.ndarray`
Slice of count map.
background : array
background : `~numpy.ndarray`
Slice of background map.
model : array
model : `~numpy.ndarray`
Model template to fit.
flux : float
Starting value for the fit.
Expand Down
2 changes: 1 addition & 1 deletion gammapy/morphology/__init__.py
Expand Up @@ -6,6 +6,6 @@
from .gauss import *
from .model import *
from .overlap import *
from gammapy.morphology.models import *
from .models import *
from .theta import *
from .utils import *
17 changes: 9 additions & 8 deletions gammapy/scripts/ts_image.py
@@ -1,9 +1,10 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from ..utils.scripts import get_parser
import os

from ..utils.scripts import get_parser
__all__ = ['ts_image']


def main(args=None):
Expand All @@ -15,27 +16,27 @@ def main(args=None):
parser.add_argument('--psf', type=str, default='psf.json',
help='JSON file containing PSF information. ')
parser.add_argument('--morphology', type=str, default='Gaussian2D',
help="Which source morphology to use for TS calculation.\n"
help="Which source morphology to use for TS calculation."
"Either 'Gaussian2D' or 'Shell2D'.")
parser.add_argument('--width', type=float, default=None,
help="Width of the shell, measured as fraction of the"
" inner radius.\n")
" inner radius.")
parser.add_argument('--scales', type=float, default=[0], nargs='+',
help='List of scales to compute TS maps for in deg.')
parser.add_argument('--downsample', type=str, default='auto',
help="Downsample factor of the data to obtain optimal"
" performance.\n"
"Must be power of 2. Can be 'auto' to choose the downsample \n"
" performance."
"Must be power of 2. Can be 'auto' to choose the downsample"
"factor automatically depending on the scale.")
parser.add_argument('--residual', action='store_true',
help="Whether to compute a residual TS image. If a residual \n"
"TS image is computed an excess model has to be provided \n"
help="Whether to compute a residual TS image. If a residual"
"TS image is computed an excess model has to be provided"
"using the '--model' parameter.")
parser.add_argument('--model', type=str,
help='Input excess model FITS file name')
parser.add_argument('--threshold', type=float, default=None,
help="Minimal required initial (before fitting) TS value,"
" that the \nfit is done at all.")
" that the fit is done at all.")
parser.add_argument('--overwrite', action='store_true',
help='Overwrite output files.')
args = parser.parse_args()
Expand Down

0 comments on commit 2bab6fb

Please sign in to comment.