File: iraf_frame.py

package info (click to toggle)
python-scipy 0.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 32,016 kB
  • ctags: 46,675
  • sloc: cpp: 124,854; ansic: 110,614; python: 108,664; fortran: 76,260; objc: 424; makefile: 384; sh: 10
file content (196 lines) | stat: -rw-r--r-- 6,752 bytes parent folder | download
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import numpy as num

"""This module defines the function frame() which creates
a framed copy of an input array with the boundary pixels
defined according to the IRAF boundary modes: 'nearest',
'reflect', 'wrap', and 'constant.'
"""

def frame_nearest(a, shape, cval=None):

    """frame_nearest creates an oversized copy of 'a' with new 'shape'
    and the contents of 'a' in the center.  The boundary pixels are
    copied from the nearest edge pixel in 'a'.

    >>> a = num.arange(16)
    >>> a.shape=(4,4)
    >>> frame_nearest(a, (8,8))
    array([[ 0,  0,  0,  1,  2,  3,  3,  3],
           [ 0,  0,  0,  1,  2,  3,  3,  3],
           [ 0,  0,  0,  1,  2,  3,  3,  3],
           [ 4,  4,  4,  5,  6,  7,  7,  7],
           [ 8,  8,  8,  9, 10, 11, 11, 11],
           [12, 12, 12, 13, 14, 15, 15, 15],
           [12, 12, 12, 13, 14, 15, 15, 15],
           [12, 12, 12, 13, 14, 15, 15, 15]])
           
    """
    
    b = num.zeros(shape, dtype=a.dtype)
    delta = (num.array(b.shape) - num.array(a.shape))
    dy = delta[0] // 2
    dx = delta[1] // 2
    my = a.shape[0] + dy
    mx = a.shape[1] + dx
    
    b[dy:my, dx:mx] = a                  # center
    b[:dy,dx:mx]  = a[0:1,:]               # top
    b[my:,dx:mx]  = a[-1:,:]              # bottom
    b[dy:my, :dx] = a[:, 0:1]              # left
    b[dy:my, mx:] = a[:, -1:]             # right
    b[:dy, :dx]   = a[0,0]               # topleft
    b[:dy, mx:]   = a[0,-1]              # topright
    b[my:, :dx]   = a[-1, 0]             # bottomleft
    b[my:, mx:]   = a[-1, -1]            # bottomright
    
    return b

def frame_reflect(a, shape, cval=None):

    """frame_reflect creates an oversized copy of 'a' with new 'shape'
    and the contents of 'a' in the center.  The boundary pixels are
    reflected from the nearest edge pixels in 'a'.

    >>> a = num.arange(16)
    >>> a.shape = (4,4)
    >>> frame_reflect(a, (8,8))
    array([[ 5,  4,  4,  5,  6,  7,  7,  6],
           [ 1,  0,  0,  1,  2,  3,  3,  2],
           [ 1,  0,  0,  1,  2,  3,  3,  2],
           [ 5,  4,  4,  5,  6,  7,  7,  6],
           [ 9,  8,  8,  9, 10, 11, 11, 10],
           [13, 12, 12, 13, 14, 15, 15, 14],
           [13, 12, 12, 13, 14, 15, 15, 14],
           [ 9,  8,  8,  9, 10, 11, 11, 10]])
    """
    
    b = num.zeros(shape, dtype=a.dtype)
    delta = (num.array(b.shape) - num.array(a.shape))
    dy = delta[0] // 2
    dx = delta[1] // 2
    my = a.shape[0] + dy
    mx = a.shape[1] + dx
    sy = delta[0] - dy
    sx = delta[1] - dx
    
    b[dy:my, dx:mx] = a                            # center
    b[:dy,dx:mx]  = a[:dy,:][::-1,:]               # top
    b[my:,dx:mx]  = a[-sy:,:][::-1,:]              # bottom
    b[dy:my,:dx]  = a[:,:dx][:,::-1]               # left
    b[dy:my,mx:]  = a[:,-sx:][:,::-1]              # right
    b[:dy,:dx]    = a[:dy,:dx][::-1,::-1]          # topleft
    b[:dy,mx:]    = a[:dy,-sx:][::-1,::-1]         # topright
    b[my:,:dx]    = a[-sy:,:dx][::-1,::-1]         # bottomleft
    b[my:,mx:]    = a[-sy:,-sx:][::-1,::-1]        # bottomright
    return b

def frame_wrap(a, shape, cval=None):
    """frame_wrap creates an oversized copy of 'a' with new 'shape'
    and the contents of 'a' in the center.  The boundary pixels are
    wrapped around to the opposite edge pixels in 'a'.

    >>> a = num.arange(16)
    >>> a.shape=(4,4)
    >>> frame_wrap(a, (8,8))
    array([[10, 11,  8,  9, 10, 11,  8,  9],
           [14, 15, 12, 13, 14, 15, 12, 13],
           [ 2,  3,  0,  1,  2,  3,  0,  1],
           [ 6,  7,  4,  5,  6,  7,  4,  5],
           [10, 11,  8,  9, 10, 11,  8,  9],
           [14, 15, 12, 13, 14, 15, 12, 13],
           [ 2,  3,  0,  1,  2,  3,  0,  1],
           [ 6,  7,  4,  5,  6,  7,  4,  5]])

    """
    
    b = num.zeros(shape, dtype=a.dtype)
    delta = (num.array(b.shape) - num.array(a.shape))
    dy = delta[0] // 2
    dx = delta[1] // 2
    my = a.shape[0] + dy
    mx = a.shape[1] + dx
    sy = delta[0] - dy
    sx = delta[1] - dx
    
    b[dy:my, dx:mx] = a                  # center
    b[:dy,dx:mx]  = a[-dy:,:]            # top
    b[my:,dx:mx]  = a[:sy,:]             # bottom
    b[dy:my,:dx]  = a[:,-dx:]            # left
    b[dy:my,mx:]  = a[:, :sx]            # right
    b[:dy,:dx]    = a[-dy:,-dx:]         # topleft
    b[:dy,mx:]    = a[-dy:,:sx ]         # topright
    b[my:,:dx]    = a[:sy, -dx:]         # bottomleft
    b[my:,mx:]    = a[:sy, :sx]          # bottomright
    return b

def frame_constant(a, shape, cval=0):
    """frame_nearest creates an oversized copy of 'a' with new 'shape'
    and the contents of 'a' in the center.  The boundary pixels are
    copied from the nearest edge pixel in 'a'.

    >>> a = num.arange(16)
    >>> a.shape=(4,4)
    >>> frame_constant(a, (8,8), cval=42)
    array([[42, 42, 42, 42, 42, 42, 42, 42],
           [42, 42, 42, 42, 42, 42, 42, 42],
           [42, 42,  0,  1,  2,  3, 42, 42],
           [42, 42,  4,  5,  6,  7, 42, 42],
           [42, 42,  8,  9, 10, 11, 42, 42],
           [42, 42, 12, 13, 14, 15, 42, 42],
           [42, 42, 42, 42, 42, 42, 42, 42],
           [42, 42, 42, 42, 42, 42, 42, 42]])

    """
    
    b = num.zeros(shape, dtype=a.dtype)
    delta = (num.array(b.shape) - num.array(a.shape))
    dy = delta[0] // 2
    dx = delta[1] // 2
    my = a.shape[0] + dy
    mx = a.shape[1] + dx
    
    b[dy:my, dx:mx] = a              # center
    b[:dy,dx:mx]  = cval             # top
    b[my:,dx:mx]  = cval             # bottom
    b[dy:my, :dx] = cval             # left
    b[dy:my, mx:] = cval             # right
    b[:dy, :dx]   = cval             # topleft
    b[:dy, mx:]   = cval             # topright
    b[my:, :dx]   = cval             # bottomleft
    b[my:, mx:]   = cval             # bottomright
    return b

_frame_dispatch = { "nearest": frame_nearest,
                    "reflect": frame_reflect,
                    "wrap": frame_wrap,
                    "constant" : frame_constant }
             
def frame(a, shape, mode="nearest", cval=0.0):
    
    """frame creates an oversized copy of 'a' with new 'shape', with
    extra pixels being supplied according to IRAF boundary mode,
    'mode'.  """

    try:
        f = _frame_dispatch[mode]
    except KeyError:
        raise ValueError('invalid IRAF boundary mode: "%s"' % mode)
    
    return f(a, shape, cval)

def unframe(a, shape):

    """unframe extracts the center slice of framed array 'a' which had
    'shape' prior to framing."""

    delta = num.array(a.shape) - num.array(shape)
    dy = delta[0]//2
    dx = delta[1]//2
    my = shape[0] + dy
    mx = shape[1] + dx
    return a[dy:my, dx:mx]

def test():
    import doctest, iraf_frame
    return doctest.testmod(iraf_frame)