1 import numpy as num
2
3 """This module defines the function frame() which creates
4 a framed copy of an input array with the boundary pixels
5 defined according to the IRAF boundary modes: 'nearest',
6 'reflect', 'wrap', and 'constant.'
7 """
8
10
11 """frame_nearest creates an oversized copy of 'a' with new 'shape'
12 and the contents of 'a' in the center. The boundary pixels are
13 copied from the nearest edge pixel in 'a'.
14
15 >>> a = num.arange(16)
16 >>> a.shape=(4,4)
17 >>> frame_nearest(a, (8,8))
18 array([[ 0, 0, 0, 1, 2, 3, 3, 3],
19 [ 0, 0, 0, 1, 2, 3, 3, 3],
20 [ 0, 0, 0, 1, 2, 3, 3, 3],
21 [ 4, 4, 4, 5, 6, 7, 7, 7],
22 [ 8, 8, 8, 9, 10, 11, 11, 11],
23 [12, 12, 12, 13, 14, 15, 15, 15],
24 [12, 12, 12, 13, 14, 15, 15, 15],
25 [12, 12, 12, 13, 14, 15, 15, 15]])
26
27 """
28
29 b = num.zeros(shape, dtype=a.dtype)
30 delta = (num.array(b.shape) - num.array(a.shape))
31 dy = delta[0] // 2
32 dx = delta[1] // 2
33 my = a.shape[0] + dy
34 mx = a.shape[1] + dx
35
36 b[dy:my, dx:mx] = a
37 b[:dy,dx:mx] = a[0:1,:]
38 b[my:,dx:mx] = a[-1:,:]
39 b[dy:my, :dx] = a[:, 0:1]
40 b[dy:my, mx:] = a[:, -1:]
41 b[:dy, :dx] = a[0,0]
42 b[:dy, mx:] = a[0,-1]
43 b[my:, :dx] = a[-1, 0]
44 b[my:, mx:] = a[-1, -1]
45
46 return b
47
49
50 """frame_reflect creates an oversized copy of 'a' with new 'shape'
51 and the contents of 'a' in the center. The boundary pixels are
52 reflected from the nearest edge pixels in 'a'.
53
54 >>> a = num.arange(16)
55 >>> a.shape = (4,4)
56 >>> frame_reflect(a, (8,8))
57 array([[ 5, 4, 4, 5, 6, 7, 7, 6],
58 [ 1, 0, 0, 1, 2, 3, 3, 2],
59 [ 1, 0, 0, 1, 2, 3, 3, 2],
60 [ 5, 4, 4, 5, 6, 7, 7, 6],
61 [ 9, 8, 8, 9, 10, 11, 11, 10],
62 [13, 12, 12, 13, 14, 15, 15, 14],
63 [13, 12, 12, 13, 14, 15, 15, 14],
64 [ 9, 8, 8, 9, 10, 11, 11, 10]])
65 """
66
67 b = num.zeros(shape, dtype=a.dtype)
68 delta = (num.array(b.shape) - num.array(a.shape))
69 dy = delta[0] // 2
70 dx = delta[1] // 2
71 my = a.shape[0] + dy
72 mx = a.shape[1] + dx
73 sy = delta[0] - dy
74 sx = delta[1] - dx
75
76 b[dy:my, dx:mx] = a
77 b[:dy,dx:mx] = a[:dy,:][::-1,:]
78 b[my:,dx:mx] = a[-sy:,:][::-1,:]
79 b[dy:my,:dx] = a[:,:dx][:,::-1]
80 b[dy:my,mx:] = a[:,-sx:][:,::-1]
81 b[:dy,:dx] = a[:dy,:dx][::-1,::-1]
82 b[:dy,mx:] = a[:dy,-sx:][::-1,::-1]
83 b[my:,:dx] = a[-sy:,:dx][::-1,::-1]
84 b[my:,mx:] = a[-sy:,-sx:][::-1,::-1]
85 return b
86
88 """frame_wrap creates an oversized copy of 'a' with new 'shape'
89 and the contents of 'a' in the center. The boundary pixels are
90 wrapped around to the opposite edge pixels in 'a'.
91
92 >>> a = num.arange(16)
93 >>> a.shape=(4,4)
94 >>> frame_wrap(a, (8,8))
95 array([[10, 11, 8, 9, 10, 11, 8, 9],
96 [14, 15, 12, 13, 14, 15, 12, 13],
97 [ 2, 3, 0, 1, 2, 3, 0, 1],
98 [ 6, 7, 4, 5, 6, 7, 4, 5],
99 [10, 11, 8, 9, 10, 11, 8, 9],
100 [14, 15, 12, 13, 14, 15, 12, 13],
101 [ 2, 3, 0, 1, 2, 3, 0, 1],
102 [ 6, 7, 4, 5, 6, 7, 4, 5]])
103
104 """
105
106 b = num.zeros(shape, dtype=a.dtype)
107 delta = (num.array(b.shape) - num.array(a.shape))
108 dy = delta[0] // 2
109 dx = delta[1] // 2
110 my = a.shape[0] + dy
111 mx = a.shape[1] + dx
112 sy = delta[0] - dy
113 sx = delta[1] - dx
114
115 b[dy:my, dx:mx] = a
116 b[:dy,dx:mx] = a[-dy:,:]
117 b[my:,dx:mx] = a[:sy,:]
118 b[dy:my,:dx] = a[:,-dx:]
119 b[dy:my,mx:] = a[:, :sx]
120 b[:dy,:dx] = a[-dy:,-dx:]
121 b[:dy,mx:] = a[-dy:,:sx ]
122 b[my:,:dx] = a[:sy, -dx:]
123 b[my:,mx:] = a[:sy, :sx]
124 return b
125
127 """frame_nearest creates an oversized copy of 'a' with new 'shape'
128 and the contents of 'a' in the center. The boundary pixels are
129 copied from the nearest edge pixel in 'a'.
130
131 >>> a = num.arange(16)
132 >>> a.shape=(4,4)
133 >>> frame_constant(a, (8,8), cval=42)
134 array([[42, 42, 42, 42, 42, 42, 42, 42],
135 [42, 42, 42, 42, 42, 42, 42, 42],
136 [42, 42, 0, 1, 2, 3, 42, 42],
137 [42, 42, 4, 5, 6, 7, 42, 42],
138 [42, 42, 8, 9, 10, 11, 42, 42],
139 [42, 42, 12, 13, 14, 15, 42, 42],
140 [42, 42, 42, 42, 42, 42, 42, 42],
141 [42, 42, 42, 42, 42, 42, 42, 42]])
142
143 """
144
145 b = num.zeros(shape, dtype=a.dtype)
146 delta = (num.array(b.shape) - num.array(a.shape))
147 dy = delta[0] // 2
148 dx = delta[1] // 2
149 my = a.shape[0] + dy
150 mx = a.shape[1] + dx
151
152 b[dy:my, dx:mx] = a
153 b[:dy,dx:mx] = cval
154 b[my:,dx:mx] = cval
155 b[dy:my, :dx] = cval
156 b[dy:my, mx:] = cval
157 b[:dy, :dx] = cval
158 b[:dy, mx:] = cval
159 b[my:, :dx] = cval
160 b[my:, mx:] = cval
161 return b
162
163 _frame_dispatch = { "nearest": frame_nearest,
164 "reflect": frame_reflect,
165 "wrap": frame_wrap,
166 "constant" : frame_constant }
167
168 -def frame(a, shape, mode="nearest", cval=0.0):
169
170 """frame creates an oversized copy of 'a' with new 'shape', with
171 extra pixels being supplied according to IRAF boundary mode,
172 'mode'. """
173
174 try:
175 f = _frame_dispatch[mode]
176 except KeyError:
177 raise ValueError('invalid IRAF boundary mode: "%s"' % mode)
178
179 return f(a, shape, cval)
180
182
183 """unframe extracts the center slice of framed array 'a' which had
184 'shape' prior to framing."""
185
186 delta = num.array(a.shape) - num.array(shape)
187 dy = delta[0]//2
188 dx = delta[1]//2
189 my = shape[0] + dy
190 mx = shape[1] + dx
191 return a[dy:my, dx:mx]
192
196