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

1from lsst.afw.image.imageSlice import ImageSliceF, ImageSliceD 

2 

3__all__ = ("disableImageArithmetic", "disableMaskArithmetic") 

4 

5 

6def wrapNotImplemented(cls, attr): 

7 """Wrap a method providing a helpful error message about image arithmetic 

8 

9 Parameters 

10 ---------- 

11 cls : `type` 

12 Class in which the method is to be defined. 

13 attr : `str` 

14 Name of the method. 

15 

16 Returns 

17 ------- 

18 method : callable 

19 Wrapped method. 

20 """ 

21 existing = getattr(cls, attr, None) 

22 

23 def notImplemented(self, other): 

24 """Provide a helpful error message about image arithmetic 

25 

26 Unless we're operating on an ImageSlice, in which case it might be 

27 defined. 

28 

29 Parameters 

30 ---------- 

31 self : subclass of `lsst.afw.image.ImageBase` 

32 Image someone's attempting to do arithmetic with. 

33 other : anything 

34 The operand of the arithmetic operation. 

35 """ 

36 if existing is not None and isinstance(other, (ImageSliceF, ImageSliceD)): 

37 return existing(self, other) 

38 raise NotImplementedError("This arithmetic operation is not implemented, in order to prevent the " 

39 "accidental proliferation of temporaries. Please use the in-place " 

40 "arithmetic operations (e.g., += instead of +) or operate on the " 

41 "underlying arrays.") 

42 return notImplemented 

43 

44 

45def disableImageArithmetic(cls): 

46 """Add helpful error messages about image arithmetic""" 

47 for attr in ("__add__", "__sub__", "__mul__", "__truediv__", 

48 "__radd__", "__rsub__", "__rmul__", "__rtruediv__"): 

49 setattr(cls, attr, wrapNotImplemented(cls, attr)) 

50 

51 

52def disableMaskArithmetic(cls): 

53 """Add helpful error messages about mask arithmetic""" 

54 for attr in ("__or__", "__and__", "__xor__", 

55 "__ror__", "__rand__", "__rxor__"): 

56 setattr(cls, attr, wrapNotImplemented(cls, attr))