Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

from lsst.geom import Point2I, Box2I 

from .image import LOCAL, PARENT, ImageOrigin 

 

__all__ = ["supportSlicing"] 

 

 

def splitSliceArgs(sliceArgs): 

"""Separate the actual slice from origin arguments to __getitem__ or 

__setitem__, using PARENT for the origin if it is not provided. 

 

Parameter 

--------- 

sliceArgs : `tuple`, `Box2I`, or `Point2I` 

The first argument passed to an image-like object's 

``__getitem__`` or ``__setitem__``. 

 

Returns 

------- 

sliceArgs : `tuple`, `Box2I`, or `Point2I` 

The original sliceArgs passed in, with any ImageOrigin argument 

stripped off. 

origin : `ImageOrigin` 

Enum value that sets whether or not to consider xy0 in positions. 

 

See interpretSliceArgs for more information. 

 

Intended primarily for internal use by `supportSlicing()`. 

""" 

defaultOrigin = PARENT 

try: 

if isinstance(sliceArgs[-1], ImageOrigin): 

# Args are already a tuple that includes the origin. 

if len(sliceArgs) == 2: 

return sliceArgs[0], sliceArgs[-1] 

else: 

return sliceArgs[:-1], sliceArgs[-1] 

else: 

# Args are a tuple that does not include the origin; add it to make origin explicit. 

return sliceArgs, defaultOrigin 

except TypeError: # Arg is a scalar; return it along with the default origin. 

return sliceArgs, defaultOrigin 

 

 

def handleNegativeIndex(index, size, origin, default): 

"""Handle negative indices passed to image accessors. 

 

When negative indices are used in LOCAL coordinates, we interpret them as 

relative to the upper bounds of the array, as in regular negative indexing 

in Python. 

 

Using negative indices in PARENT coordinates is not allowed unless passed 

via a `Point2I` or `Box2I`; the potential for confusion between actual 

negative indices (when ``xy0 < 0``) and offsets relative to the upper 

bounds of the array is too great. 

 

Parameters 

---------- 

index : `int` or `None` 

1-d pixel index to interpret, as given by a caller to an image-like 

object's ``__getitem__`` or ``__setitem__``. 

size : `int` 

Size of the image in the dimension corresponding to ``index``. 

origin : `ImageOrigin` 

Enum value that sets whether or not to consider xy0 in positions. 

default : `int` 

Index to return if `index` is None. 

 

Returns 

------- 

index : `int` 

If ``origin==PARENT``, either the given ``index`` or ``default``. 

If ``origin==LOCAL``, an equivalent index guaranteed to be nonnegative. 

 

Intended primarily for internal use by `supportSlicing()`. 

""" 

if index is None: 

assert default is not None 

return default 

if index < 0: 

80 ↛ 83line 80 didn't jump to line 83, because the condition on line 80 was never false if origin == LOCAL: 

index = size + index 

else: 

raise IndexError("Negative indices are not permitted with the PARENT origin. " 

"Use LOCAL to use negative to index relative to the end, " 

"and Point2I or Box2I indexing to access negative pixels " 

"in PARENT coordinates.") 

return index 

 

 

def makePointFromIndices(x, y, origin, bbox): 

"""Create a Point2I from an x, y pair, correctly handling negative indices. 

 

Parameters 

---------- 

x : `int` 

Column index. 

y : `int` 

Row index. 

origin : `ImageOrigin` 

Enum value that sets whether or not to consider xy0 in positions. 

bbox : `Box2I` 

Bounding box of the image being indexed, obtained using 

``getBBox(origin)``. 

 

Returns 

------- 

point : `Point2I` 

A point corresponding to the given indices. 

 

Intended primarily for internal use by `supportSlicing()`. 

""" 

return Point2I( 

handleNegativeIndex(x, bbox.getWidth(), origin, default=None), 

handleNegativeIndex(y, bbox.getHeight(), origin, default=None) 

) 

 

 

def makeBoxFromSlices(x, y, origin, bbox): 

"""Transform a tuple of slice objects into a Box2I, correctly handling negative indices. 

 

Parameters 

---------- 

x : `slice` 

Column index range. Step must be None. 

y : `slice` 

Row index range. Step must be None. 

origin : `ImageOrigin` 

Enum value that sets whether or not to consider xy0 in positions. 

bbox : `Box2I` 

Bounding box of the image being indexed, obtained using 

``getBBox(origin)``. 

 

Returns 

------- 

bbox : `Box2I` 

A point corresponding to the given indices. 

 

Intended primarily for internal use by `supportSlicing()`. 

""" 

140 ↛ 141line 140 didn't jump to line 141, because the condition on line 140 was never true if x.step is not None or y.step is not None: 

raise ValueError("Slices with steps are not supported in image indexing.") 

begin = Point2I( 

handleNegativeIndex(x.start, bbox.getWidth(), origin, default=bbox.getBeginX()), 

handleNegativeIndex(y.start, bbox.getHeight(), origin, default=bbox.getBeginY()) 

) 

end = Point2I( 

handleNegativeIndex(x.stop, bbox.getWidth(), origin, default=bbox.getEndX()), 

handleNegativeIndex(y.stop, bbox.getHeight(), origin, default=bbox.getEndY()) 

) 

return Box2I(begin, end - begin) 

 

 

def interpretSliceArgs(sliceArgs, bboxGetter): 

"""Transform arguments to __getitem__ or __setitem__ to a standard form. 

 

Parameters 

---------- 

sliceArgs : `tuple`, `Box2I`, or `Point2I` 

Slice arguments passed directly to `__getitem__` or `__setitem__`. 

bboxGetter : callable 

Callable that accepts an ImageOrigin enum value and returns the 

appropriate image bounding box. Usually the bound getBBox method 

of an Image, Mask, or MaskedImage object. 

 

Returns 

------- 

box : `Box2I` or `None` 

A box to use to create a subimage, or None if the slice refers to a 

scalar. 

index: `tuple` or `None` 

An ``(x, y)`` tuple of integers, or None if the slice refers to a 

box. 

origin : `ImageOrigin` 

Enum indicating whether to account for xy0. 

 

Intended primarily for internal use by `supportSlicing()`. 

""" 

slices, origin = splitSliceArgs(sliceArgs) 

if isinstance(slices, Point2I): 

return None, slices, origin 

elif isinstance(slices, Box2I): 

return slices, None, origin 

elif isinstance(slices, slice): 

184 ↛ 185line 184 didn't jump to line 185, because the condition on line 184 was never true if slices.start is not None or slices.stop is not None or slices.step is not None: 

raise TypeError("Single-dimension slices must not have bounds.") 

x = slices 

y = slices 

origin = LOCAL # doesn't matter, as the slices cover the full image 

else: 

x, y = slices 

if isinstance(x, slice): 

192 ↛ 194line 192 didn't jump to line 194, because the condition on line 192 was never false if isinstance(y, slice): 

return makeBoxFromSlices(x, y, origin=origin, bbox=bboxGetter(origin)), None, origin 

raise TypeError("Mixed indices of the form (slice, int) are not supported for images.") 

else: 

196 ↛ 197line 196 didn't jump to line 197, because the condition on line 196 was never true if isinstance(y, slice): 

raise TypeError("Mixed indices of the form (int, slice) are not supported for images.") 

return None, makePointFromIndices(x, y, origin=origin, bbox=bboxGetter(origin)), origin 

 

 

def supportSlicing(cls): 

"""Support image slicing 

""" 

 

def Factory(self, *args, **kwargs): 

"""Return an object of this type 

""" 

return cls(*args, **kwargs) 

cls.Factory = Factory 

 

def clone(self): 

"""Return a deep copy of self""" 

return cls(self, True) 

cls.clone = clone 

 

def __getitem__(self, imageSlice): 

box, index, origin = interpretSliceArgs(imageSlice, self.getBBox) 

if box is not None: 

return self.subset(box, origin=origin) 

return self._get(index, origin=origin) 

cls.__getitem__ = __getitem__ 

 

def __setitem__(self, imageSlice, rhs): 

box, index, origin = interpretSliceArgs(imageSlice, self.getBBox) 

if box is not None: 

if self.assign(rhs, box, origin) is NotImplemented: 

lhs = self.subset(box, origin=origin) 

lhs.set(rhs) 

else: 

self._set(index, origin=origin, value=rhs) 

cls.__setitem__ = __setitem__