lsst.afw  13.0-91-g2a3e2ee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
slicing.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division, print_function
2 
3 import lsst.afw.geom as afwGeom
4 from .image import LOCAL
5 
6 __all__ = ["supportSlicing"]
7 
8 
9 def _getBBoxFromSliceTuple(img, imageSlice):
10  """Given a slice specification return the proper Box2I
11  This is the worker routine behind __getitem__ and __setitem__
12 
13  The imageSlice may be:
14  lsst.afw.geom.Box2I
15  slice, slice
16  :
17  Only the first one or two parts of the slice are recognised (no stride), a single int is
18  interpreted as n:n+1, and negative indices are interpreted relative to the end of the array,
19  so supported slices include:
20  2
21  -1
22  1:10
23  :-2
24  : (equivalent to ... (python's Ellipsis) which is also supported)
25 
26  E.g.
27  im[-1, :]
28  im[..., 18]
29  im[4, 10]
30  im[-3:, -2:]
31  im[-2, -2]
32  im[1:4, 6:10]
33  im[:]
34  """
35  if isinstance(imageSlice, afwGeom.Box2I):
36  return imageSlice
37 
38  if isinstance(imageSlice, slice) and imageSlice.start is None and imageSlice.stop is None:
39  imageSlice = (Ellipsis, Ellipsis,)
40 
41  if not (isinstance(imageSlice, tuple) and len(imageSlice) == 2 and
42  sum([isinstance(_, (slice, type(Ellipsis), int)) for _ in imageSlice]) == 2):
43  raise IndexError(
44  "Images may only be indexed as a 2-D slice not %s" % imageSlice)
45 
46  imageSlice, _imageSlice = [], imageSlice
47  for s, wh in zip(_imageSlice, img.getDimensions()):
48  if isinstance(s, slice):
49  pass
50  elif isinstance(s, int):
51  if s < 0:
52  s += wh
53  s = slice(s, s + 1)
54  else:
55  s = slice(0, wh)
56 
57  imageSlice.append(s)
58 
59  x, y = [_.indices(wh) for _, wh in zip(imageSlice, img.getDimensions())]
60  return afwGeom.Box2I(afwGeom.Point2I(x[0], y[0]), afwGeom.Point2I(x[1] - 1, y[1] - 1))
61 
62 
63 def supportSlicing(cls):
64  """Support image slicing
65  """
66 
67  def Factory(self, *args):
68  """Return an object of this type
69  """
70  return cls(*args)
71  cls.Factory = Factory
72 
73  def clone(self):
74  """Return a deep copy of self"""
75  return cls(self, True)
76  cls.clone = clone
77 
78  def __getitem__(self, imageSlice):
79  return self.Factory(self, _getBBoxFromSliceTuple(self, imageSlice), LOCAL)
80  cls.__getitem__ = __getitem__
81 
82  def __setitem__(self, imageSlice, rhs):
83  bbox = _getBBoxFromSliceTuple(self, imageSlice)
84 
85  if self.assign(rhs, bbox, LOCAL) is NotImplemented:
86  lhs = self.Factory(self, bbox, LOCAL)
87  lhs.set(rhs)
88  cls.__setitem__ = __setitem__
89 
90  def __float__(self):
91  """Convert a 1x1 image to a floating scalar"""
92  if self.getDimensions() != afwGeom.Extent2I(1, 1):
93  raise TypeError(
94  "Only single-pixel images may be converted to python scalars")
95 
96  try:
97  return float(self.get(0, 0))
98  except AttributeError:
99  raise TypeError(
100  "Unable to extract a single pixel from a {}".format(type(self).__name__))
101  except TypeError:
102  raise TypeError(
103  "Unable to convert a {} pixel to a scalar".format(type(self).__name__))
104  cls.__float__ = __float__
105 
106  def __int__(self):
107  """Convert a 1x1 image to a integral scalar"""
108  return int(float(self))
109  cls.__int__ = __int__
An integer coordinate rectangle.
Definition: Box.h:55