1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 from __future__ import division
32 import types
33 import numpy
34
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
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
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
86 if axis < 0:
87 axis += rank
88 if axis < 0 or axis >= rank:
89 raise ValueError('invalid axis')
90 return axis
91