Skip to content

Instantly share code, notes, and snippets.

@cdeil
Created February 11, 2014 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdeil/8936875 to your computer and use it in GitHub Desktop.
Save cdeil/8936875 to your computer and use it in GitHub Desktop.
_________________________________________________________________________ test_image _________________________________________________________________________
@pytest.mark.skipif('not HAS_SCIPY')
def test_image():
# Test dataset parameters
x_size_kernel, y_size_kernel = (11, 11)
x_size_image, y_size_image = (31, 31)
total_excess = 100
total_background = 1000
ones = np.ones((x_size_image, y_size_image))
# Create test dataset
kernel = Gaussian2DKernel(width=3, x_size=x_size_kernel, y_size=y_size_kernel).array
excess = total_excess * Gaussian2DKernel(width=3, x_size=x_size_image, y_size=y_size_image).array
background = total_background * ones / ones.sum()
counts = excess + background
#np.random.seed(0)
#counts = np.random.poisson(counts)
images = dict(counts=counts, background=background)
> probability = matched_filter.probability_image(images, kernel)
gammapy/detect/tests/test_matched_filter.py:58:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
images = {'background': array([[ 1.04058273, 1.04058273, 1.04058273, 1.04058273, 1.04058273,
...273, 1.04058273, 1.0...273, 1.04058273, 1.04058273,
...275, 1.04058273, 1.04058273, 1.04058273, 1.04058273,
1.04058273]])}
kernel = array([[ 0.00109952, 0.00181281, 0.00267451, 0.00353086, 0.00417122,
...952, 0.00417122, 0.00353086, 0.00267451, 0.00181281,
0.00109952]])
def probability_image(images, kernel):
"""Compute matched-filter p-value image.
Parameters
----------
images : dict of arrays
Keys: 'counts', 'background'
kernel : array_like
Returns
-------
probability : array
"""
out = np.zeros_like(images['counts'], dtype='float64')
> process_image_pixels(images, kernel, out, probability_center)
gammapy/detect/matched_filter.py:74:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
images = {'background': array([[ 1.04058273, 1.04058273, 1.04058273, 1.04058273, 1.04058273,
...273, 1.04058273, 1.0...273, 1.04058273, 1.04058273,
...275, 1.04058273, 1.04058273, 1.04058273, 1.04058273,
1.04058273]])}
kernel = array([[ 0.00109952, 0.00181281, 0.00267451, 0.00353086, 0.00417122,
...952, 0.00417122, 0.00353086, 0.00267451, 0.00181281,
0.00109952]])
out = array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
... 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0.]])
pixel_function = <function probability_center at 0x110ae7848>
def process_image_pixels(images, kernel, out, pixel_function):
"""Process images for a given kernel and per-pixel function.
This is a helper function for the following common task:
For a given set of same-shaped images and a smaller-shaped kernel,
process each image pixel by moving the kernel at that position,
cut out kernel-shaped parts from the images and call a function
to compute output values for that position.
This function loops over image pixels and takes care of bounding
box computations, including image boundary handling.
Parameters
----------
images : dict of arrays
Images needed to compute out
kernel : array (shape must be odd-valued)
kernel shape must be odd-valued
out : single array or dict of arrays
These arrays must have been pre-created by the caller
pixel_function : function to process a part of the images
Examples
--------
As an example, here is how to implement convolution as a special
case of process_image_pixels with one input and output image::
def convolve(image, kernel):
'''Convolve image with kernel'''
from gammapy.image.utils import process_image_pixels
images = dict(image=np.asanyarray(image))
kernel = np.asanyarray(kernel)
out = dict(image=np.empty_like(image))
def convolve_function(images, kernel):
value = np.sum(images['image'] * kernel)
return dict(image=value)
process_image_pixels(images, kernel, out, convolve_function)
return out['image']
* TODO: add different options to treat the edges
* TODO: implement multiprocessing version
* TODO: this function is similar to view_as_windows in scikit-image:
http://scikit-image.org/docs/dev/api/skimage.util.html#view-as-windows
Is this function needed or can everything be done with view_as_windows?
"""
if isinstance(out, dict):
n0, n1 = out.values()[0].shape
else:
n0, n1 = out.shape
# Check kernel shape
k0, k1 = kernel.shape
if (k0 % 2 == 0) or (k1 % 2 == 0):
raise ValueError('Kernel shape must have odd dimensions')
k0, k1 = (k0 - 1) / 2, (k1 - 1) / 2
# Loop over all pixels
for i0 in range(0, n0):
for i1 in range(0, n1):
# Compute low and high extension
# (# pixels, not counting central pixel)
i0_lo = min(k0, i0)
i1_lo = min(k1, i1)
i0_hi = min(k0, n0 - i0 - 1)
i1_hi = min(k1, n1 - i1 - 1)
# Cut out relevant parts of the image arrays
# This creates views, i.e. is fast and memory efficient
image_parts = dict()
for name, image in images.items():
# hi + 1 because with Python slicing the hi edge is not included
part = image[i0 - i0_lo: i0 + i0_hi + 1,
> i1 - i1_lo: i1 + i1_hi + 1]
E IndexError: invalid slice
gammapy/image/utils.py:322: IndexError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment