Skip to content

Commit

Permalink
Added warnings for normalization and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adonath committed Sep 11, 2013
1 parent 57d25af commit b830674
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
13 changes: 9 additions & 4 deletions astropy/nddata/convolution/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
Currently only symmetric 2D kernels are supported.
"""
import copy
import warnings

import numpy as np
import copy

from .utils import (discretize_model, add_kernel_arrays_1D,
add_kernel_arrays_2D, NormalizationWarning)
add_kernel_arrays_2D)


class Kernel(object):
Expand All @@ -33,8 +35,11 @@ class Kernel(object):
def __init__(self, array):
self._array = array
self._normalization = 1. / self._array.sum()
if np.abs(self._normalization) > self._array.size * self._normalization:
raise NormalizationWarning("Normalization factor of kernel is" +
# The value of 100 is kind of arbitrary
# there are kernel sum to zero and the user should
# be warned in this case
if np.abs(self._normalization) > 100:
warnings.warn("Normalization factor of kernel is" +
"exceptionally large.")

@property
Expand Down
17 changes: 11 additions & 6 deletions astropy/nddata/convolution/kernels.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from __future__ import division
import warnings

import numpy as np

from .core import Kernel1D, Kernel2D, Kernel
from .utils import KernelSizeError

from ...modeling.functional_models import *
from ...modeling.core import Parametric1DModel, Parametric2DModel


__all__ = sorted(['Gaussian1DKernel', 'Gaussian2DKernel', 'CustomKernel',
'Box1DKernel', 'Box2DKernel', 'Tophat2DKernel',
'Trapezoid1DKernel', 'MexicanHat1DKernel',
'MexicanHat2DKernel', 'AiryDisk2DKernel',
'Model1DKernel', 'Model2DKernel'])
'Model1DKernel', 'Model2DKernel',
'TrapezoidDisk2DKernel'])


class Gaussian1DKernel(Kernel1D):
Expand Down Expand Up @@ -212,7 +213,7 @@ class TrapezoidDisk2DKernel(Kernel2D):
_weighted = True

def __init__(self, width, slope=1., **kwargs):
self._model = TrapezoidDisk2DModel(1, 0, width, slope)
self._model = TrapezoidDisk2DModel(1, 0, 0, width, slope)
self._default_size = width + 2. / slope
super(TrapezoidDisk2DKernel, self).__init__(**kwargs)
self._truncation = 0
Expand Down Expand Up @@ -242,7 +243,9 @@ class MexicanHat1DKernel(Kernel1D):
def __init__(self, width, **kwargs):
self._default_size = 8 * width
self._model = MexicanHat1DModel(1, 0, width)
super(MexicanHat1DKernel, self).__init__(**kwargs)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
super(MexicanHat1DKernel, self).__init__(**kwargs)
self._truncation = np.abs(self._array.sum() / self._array.size)
self._normalization = 0

Expand Down Expand Up @@ -274,7 +277,9 @@ class MexicanHat2DKernel(Kernel2D):
def __init__(self, width, **kwargs):
self._default_size = 8 * width
self._model = MexicanHat2DModel(1, 0, 0, width)
super(MexicanHat2DKernel, self).__init__(**kwargs)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
super(MexicanHat2DKernel, self).__init__(**kwargs)
self._truncation = np.abs(self._array.sum() / self._array.size)
self._normalization = 0

Expand Down
15 changes: 8 additions & 7 deletions astropy/nddata/convolution/tests/test_convolve_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@


widths = [3, 5, 7, 9]
kernel_types = [GaussianKernel, BoxKernel, Tophat2DKernel, MexicanHat1DKernel]
kernel_types = [Gaussian1DKernel, Gaussian2DKernel,
Box1DKernel, Box2DKernel,
Trapezoid1DKernel, TrapezoidDisk2DKernel,
MexicanHat1DKernel, Tophat2DKernel]


class Test2DConvolutions(object):
Expand Down Expand Up @@ -80,15 +83,13 @@ def test_smallkernel_vs_makekernel(self, width):
Compares a small kernel to something produced by makekernel
"""
box = BoxKernel(width)
kernel1 = np.ones([width, width])
kernel2 = np.outer(box.array, box.array)
kernel1 = np.ones([width, width]) / width ** 2
kernel2 = Box2DKernel(width)

x = np.zeros(kernel2.shape)
xslice = [slice(sh // 2, sh // 2) for sh in kernel2.shape]
x[xslice] = 1.0
x[kernel2.center] = 1.0

c2 = convolve_fft(x, kernel2, boundary='fill')
c2 = convolve_fft(x, kernel2.array, boundary='fill')
c1 = convolve_fft(x, kernel1, boundary='fill')

assert_almost_equal(c1, c2, decimal=12)
14 changes: 2 additions & 12 deletions astropy/nddata/convolution/utils.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
from __future__ import division

import numpy as np

from ...modeling.core import Parametric1DModel, Parametric2DModel


class NormalizationWarning(Warning):
"""
Called when normalization of kernels is strange.
"""
def __init__(self, message):
self._message = message

def __str__(self):
return self._message


class DiscretizationError(Exception):
"""
Called when discretization of models goes wrong.
Expand Down Expand Up @@ -235,4 +225,4 @@ def discretize_integrate_2D(model, range_, mode='analytical'):
if getattr(model, "integral", False):
raise NotImplementedError("Currently not supported.")
else:
raise NotImplementedError("Currently not supported.")
raise NotImplementedError("Currently not supported.")

0 comments on commit b830674

Please sign in to comment.