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

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

# This file is part of afw. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

 

__all__ = ["MultibandBase"] 

 

from abc import ABC, abstractmethod 

 

from lsst.geom import Box2I 

 

 

class MultibandBase(ABC): 

"""Base class for multiband objects 

 

The LSST stack has a number of image-like classes that have 

data in multiple bands that are stored as separate objects. 

Analyzing the data can be easier using a Multiband object that 

wraps the underlying data as a single data cube that can be sliced and 

updated as a single object. 

 

`MultibandBase` is designed to contain the most important universal 

methods for initializing, slicing, and extracting common parameters 

(such as the bounding box or XY0 position) to all of the single band classes, 

as long as derived classes either call the base class `__init__` 

or set the `_filters`, `_singles`, and `_bbox`. 

 

Parameters 

---------- 

filters: `list` 

List of filter names. 

singles: `list` 

List of single band objects 

bbox: `Box2I` 

By default `MultibandBase` uses `singles[0].getBBox()` to set 

the bounding box of the multiband 

""" 

def __init__(self, filters, singles, bbox=None): 

self._filters = tuple([f for f in filters]) 

self._singles = tuple(singles) 

 

if bbox is None: 

self._bbox = self._singles[0].getBBox() 

if not all([s.getBBox() == self.getBBox() for s in self.singles]): 

bboxes = [s.getBBox() == self.getBBox() for s in self.singles] 

err = "`singles` are required to have the same bounding box, received {0}" 

raise ValueError(err.format(bboxes)) 

else: 

self._bbox = bbox 

 

@abstractmethod 

def clone(self, deep=True): 

"""Copy the current object 

 

This must be overloaded in a subclass of `MultibandBase` 

 

Parameters 

---------- 

deep: `bool` 

Whether or not to make a deep copy 

 

Returns 

------- 

result: `MultibandBase` 

copy of the instance that inherits from `MultibandBase` 

""" 

pass 

 

@property 

def filters(self): 

"""List of filter names for the single band objects 

""" 

return self._filters 

 

@property 

def singles(self): 

"""List of single band objects 

""" 

return self._singles 

 

def getBBox(self): 

"""Bounding box 

""" 

return self._bbox 

 

def getXY0(self): 

"""Minimum coordinate in the bounding box 

""" 

return self.getBBox().getMin() 

 

@property 

def x0(self): 

"""X0 

 

X component of XY0 `Point2I.getX()` 

""" 

return self.getBBox().getMinX() 

 

@property 

def y0(self): 

"""Y0 

 

Y component of XY0 `Point2I.getY()` 

""" 

return self.getBBox().getMinY() 

 

@property 

def origin(self): 

"""Minimum (y,x) position 

 

This is the position of `self.getBBox().getMin()`, 

but available as a tuple for numpy array indexing. 

""" 

return (self.y0, self.x0) 

 

@property 

def width(self): 

"""Width of the images 

""" 

return self.getBBox().getWidth() 

 

@property 

def height(self): 

"""Height of the images 

""" 

return self.getBBox().getHeight() 

 

def __len__(self): 

return len(self.filters) 

 

def __getitem__(self, args): 

"""Get a slice of the underlying array 

 

If only a single filter is specified, 

return the single band object sliced 

appropriately. 

""" 

if not isinstance(args, tuple): 

indices = (args,) 

else: 

indices = args 

 

# Return the single band object if the first 

# index is not a list or slice. 

filters, filterIndex = self._filterNamesToIndex(indices[0]) 

if not isinstance(filterIndex, slice) and len(filterIndex) == 1: 

if len(indices) > 2: 

return self.singles[filterIndex[0]][indices[1:]] 

elif len(indices) == 2: 

return self.singles[filterIndex[0]][indices[1]] 

else: 

return self.singles[filterIndex[0]] 

 

return self._slice(filters=filters, filterIndex=filterIndex, indices=indices[1:]) 

 

def __iter__(self): 

self._filterIndex = 0 

return self 

 

def __next__(self): 

if self._filterIndex < len(self.filters): 

result = self.singles[self._filterIndex] 

self._filterIndex += 1 

else: 

raise StopIteration 

return result 

 

def _filterNamesToIndex(self, filterIndex): 

"""Convert a list of filter names to an index or a slice 

 

Parameters 

---------- 

filterIndex: iterable or `object` 

Index to specify a filter or list of filters, 

usually a string or enum. 

For example `filterIndex` can be 

`"R"` or `["R", "G", "B"]` or `[Filter.R, Filter.G, Filter.B]`, 

if `Filter` is an enum. 

 

Returns 

------- 

filterNames: `list` 

Names of the filters in the slice 

filterIndex: `slice` or `list` of `int` 

Index of each filter in `filterNames` in 

`self.filters`. 

""" 

if isinstance(filterIndex, slice): 

if filterIndex.start is not None: 

start = self.filters.index(filterIndex.start) 

else: 

start = None 

if filterIndex.stop is not None: 

stop = self.filters.index(filterIndex.stop) 

else: 

stop = None 

filterIndices = slice(start, stop, filterIndex.step) 

filterNames = self.filters[filterIndices] 

else: 

if isinstance(filterIndex, str): 

filterNames = [filterIndex] 

filterIndices = [self.filters.index(filterIndex)] 

else: 

try: 

# Check to see if the filterIndex is an iterable 

filterNames = [f for f in filterIndex] 

except TypeError: 

filterNames = [filterIndex] 

filterIndices = [self.filters.index(f) for f in filterNames] 

return tuple(filterNames), filterIndices 

 

def setXY0(self, xy0): 

"""Shift the bounding box but keep the same Extent 

 

Parameters 

---------- 

xy0: `Point2I` 

New minimum bounds of the bounding box 

""" 

self._bbox = Box2I(xy0, self._bbox.getDimensions()) 

for singleObj in self.singles: 

singleObj.setXY0(xy0) 

 

def shiftedTo(self, xy0): 

"""Shift the bounding box but keep the same Extent 

 

This method is broken until DM-10781 is completed. 

 

Parameters 

---------- 

xy0: `Point2I` 

New minimum bounds of the bounding box 

 

Returns 

------- 

result: `MultibandBase` 

A copy of the object, shifted to `xy0`. 

""" 

raise NotImplementedError("shiftedBy not implemented until DM-10781") 

result = self.clone(False) 

result._bbox = Box2I(xy0, result._bbox.getDimensions()) 

for singleObj in result.singles: 

singleObj.setXY0(xy0) 

return result 

 

def shiftedBy(self, offset): 

"""Shift a bounding box by an offset, but keep the same Extent 

 

This method is broken until DM-10781 is completed. 

 

Parameters 

---------- 

offset: `Extent2I` 

Amount to shift the bounding box in x and y. 

 

Returns 

------- 

result: `MultibandBase` 

A copy of the object, shifted by `offset` 

""" 

raise NotImplementedError("shiftedBy not implemented until DM-10781") 

xy0 = self._bbox.getMin() + offset 

return self.shiftedTo(xy0) 

 

@abstractmethod 

def _slice(self, filters, filterIndex, indices): 

"""Slice the current object and return the result 

 

Different inherited classes will handling slicing differently, 

so this method must be overloaded in inherited classes. 

 

Parameters 

---------- 

filters: `list` of `str` 

List of filter names for the slice. This is a subset of the 

filters in the parent multiband object 

filterIndex: `list` of `int` or `slice` 

Index along the filter dimension 

indices: `tuple` of remaining indices 

`MultibandBase.__getitem__` separates the first (filter) 

index from the remaining indices, so `indices` is a tuple 

of all of the indices that come after `filter` in the 

`args` passed to `MultibandBase.__getitem__`. 

 

Returns 

------- 

result: `object` 

Sliced version of the current object, which could be the 

same class or a different class depending on the 

slice being made. 

""" 

pass 

 

def __repr__(self): 

result = "<{0}, filters={1}, bbox={2}>".format( 

self.__class__.__name__, self.filters, self.getBBox().__repr__()) 

return result 

 

def __str__(self): 

if hasattr(self, "array"): 

return str(self.array) 

return self.__repr__()