Package stsci :: Package ndimage :: Module _ni_support
[hide private]
[frames] | no frames]

Source Code for Module stsci.ndimage._ni_support

 1  # Copyright (C) 2003-2005 Peter J. Verveer 
 2  # 
 3  # Redistribution and use in source and binary forms, with or without 
 4  # modification, are permitted provided that the following conditions 
 5  # are met: 
 6  # 
 7  # 1. Redistributions of source code must retain the above copyright 
 8  #    notice, this list of conditions and the following disclaimer. 
 9  # 
10  # 2. Redistributions in binary form must reproduce the above 
11  #    copyright notice, this list of conditions and the following 
12  #    disclaimer in the documentation and/or other materials provided 
13  #    with the distribution. 
14  # 
15  # 3. The name of the author may not be used to endorse or promote 
16  #    products derived from this software without specific prior 
17  #    written permission. 
18  # 
19  # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 
20  # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
21  # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22  # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 
23  # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
24  # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
25  # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26  # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
27  # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
28  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
29  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
30   
31  from __future__ import division 
32  import types 
33  import numpy 
34   
35 -def _extend_mode_to_code(mode):
36 """Convert an extension mode to the corresponding integer code. 37 """ 38 if mode == 'nearest': 39 return 0 40 elif mode == 'wrap': 41 return 1 42 elif mode == 'reflect': 43 return 2 44 elif mode == 'mirror': 45 return 3 46 elif mode == 'constant': 47 return 4 48 else: 49 raise RuntimeError('boundary mode not supported')
50
51 -def _normalize_sequence(input, rank, array_type=None):
52 """If input is a scalar, create a sequence of length equal to the 53 rank by duplicating the input. If input is a sequence, 54 check if its length is equal to the length of array. 55 """ 56 if (isinstance(input, (types.IntType, types.LongType, 57 types.FloatType))): 58 normalized = [input] * rank 59 else: 60 normalized = list(input) 61 if len(normalized) != rank: 62 err = "sequence argument must have length equal to input rank" 63 raise RuntimeError(err) 64 return normalized
65
66 -def _get_output(output, input, shape=None):
67 if shape is None: 68 shape = input.shape 69 if output is None: 70 output = numpy.zeros(shape, dtype = input.dtype.name) 71 return_value = output 72 elif type(output) in [type(types.TypeType), type(numpy.zeros((4,)).dtype)]: 73 output = numpy.zeros(shape, dtype = output) 74 return_value = output 75 elif type(output) is types.StringType: 76 output = numpy.typeDict[output] 77 output = numpy.zeros(shape, dtype = output) 78 return_value = output 79 else: 80 if output.shape != shape: 81 raise RuntimeError("output shape not correct") 82 return_value = None 83 return output, return_value
84
85 -def _check_axis(axis, rank):
86 if axis < 0: 87 axis += rank 88 if axis < 0 or axis >= rank: 89 raise ValueError('invalid axis') 90 return axis
91