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

import lsst.geom 

from .image import LOCAL, PARENT, ImageOrigin 

 

__all__ = ["supportSlicing"] 

 

 

def _getBBoxFromSliceTuple(img, imageSlice): 

"""Given a slice specification return the proper Box2I and origin 

This is the worker routine behind __getitem__ and __setitem__ 

 

The imageSlice may be: 

lsst.geom.Box2I 

slice, slice 

: 

Only the first one or two parts of the slice are recognised (no stride), a single int is 

interpreted as n:n+1, and negative indices are interpreted relative to the end of the array, 

so supported slices include: 

2 

-1 

1:10 

:-2 

: (equivalent to ... (python's Ellipsis) which is also supported) 

 

E.g. 

im[-1, :] 

im[..., 18] 

im[4, 10] 

im[-3:, -2:] 

im[-2, -2] 

im[1:4, 6:10] 

im[:] 

 

You may also add an extra argument, afwImage.PARENT or afwImage.LOCAL. The default is LOCAL 

as before, but if you specify PARENT the bounding box is interpreted in PARENT coordinates 

(this includes slices; e.g. 

im[-3:, -2:, afwImage.PARENT] 

still means the last few rows and columns, and 

im[1001:1004, 2006:2010, afwImage.PARENT] 

with xy0 = (1000, 2000) refers to the same pixels as 

im[1:4, 6:10, afwImage.LOCAL] 

) 

""" 

origin = LOCAL # this sets the default value 

 

try: 

_origin = imageSlice[-1] 

except TypeError: 

_origin = None 

 

if isinstance(_origin, ImageOrigin): 

origin = _origin 

imageSlice = imageSlice[0] if len(imageSlice) <= 2 else imageSlice[:-1] 

 

if isinstance(imageSlice, lsst.geom.Box2I): 

return imageSlice, origin 

 

if isinstance(imageSlice, slice) and imageSlice.start is None and imageSlice.stop is None: 

imageSlice = (Ellipsis, Ellipsis,) 

 

if not (isinstance(imageSlice, tuple) and len(imageSlice) == 2 and 

sum([isinstance(_, (slice, type(Ellipsis), int)) for _ in imageSlice]) == 2): 

raise IndexError( 

"Images may only be indexed as a 2-D slice not %s" % imageSlice) 

 

# Because we're going to use slice.indices(...) to construct our ranges, and 

# python doesn't understand PARENT coordinate systems, we need 

# to convert slices specified in PARENT coords to LOCAL 

 

imageSlice, _imageSlice = [], imageSlice 

for s, wh, z0 in zip(_imageSlice, img.getDimensions(), img.getXY0()): 

if origin == LOCAL: 

z0 = 0 # ignore image's xy0 

 

if isinstance(s, slice): 

if z0 != 0: 

start = s.start if s.start is None or s.start < 0 else s.start - z0 

stop = s.stop if s.stop is None or s.stop < 0 else s.stop - z0 

s = slice(start, stop, s.step) 

elif isinstance(s, int): 

if s < 0: 

s += z0 + wh 

s = slice(s - z0, s - z0 + 1) 

else: 

s = slice(0, wh) 

 

imageSlice.append(s) 

 

x, y = [_.indices(wh) for _, wh in zip(imageSlice, img.getDimensions())] 

return lsst.geom.Box2I(lsst.geom.Point2I(x[0], y[0]), lsst.geom.Point2I(x[1] - 1, y[1] - 1)), LOCAL 

 

 

def supportSlicing(cls): 

"""Support image slicing 

""" 

 

def _checkOrigin(origin): 

97 ↛ 98line 97 didn't jump to line 98, because the condition on line 97 was never true if origin not in (LOCAL, PARENT): 

raise RuntimeError("keyword origin must be afwImage.ORIGIN or afwImage.PARENT, not %s" % origin) 

 

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): 

bbox, origin = _getBBoxFromSliceTuple(self, imageSlice) 

 

_checkOrigin(origin) 

return self.Factory(self, bbox, origin) 

cls.__getitem__ = __getitem__ 

 

def __setitem__(self, imageSlice, rhs): 

bbox, origin = _getBBoxFromSliceTuple(self, imageSlice) 

 

_checkOrigin(origin) 

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

lhs = self.Factory(self, bbox, origin=origin) 

lhs.set(rhs) 

cls.__setitem__ = __setitem__ 

 

def __float__(self): 

"""Convert a 1x1 image to a floating scalar""" 

if self.getDimensions() != lsst.geom.Extent2I(1, 1): 

raise TypeError( 

"Only single-pixel images may be converted to python scalars") 

 

try: 

return float(self.get(0, 0)) 

135 ↛ 136line 135 didn't jump to line 136, because the exception caught by line 135 didn't happen except AttributeError: 

raise TypeError( 

"Unable to extract a single pixel from a {}".format(type(self).__name__)) 

except TypeError: 

raise TypeError( 

"Unable to convert a {} pixel to a scalar".format(type(self).__name__)) 

cls.__float__ = __float__ 

 

def __int__(self): 

"""Convert a 1x1 image to a integral scalar""" 

return int(float(self)) 

cls.__int__ = __int__