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

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

# 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__ = ["MultibandPixel", "MultibandImage", "MultibandMask", "MultibandMaskedImage"] 

 

import numpy as np 

 

from lsst.geom import Point2I, Box2I, Extent2I 

from . import Image, ImageF, Mask, MaskPixel, PARENT, LOCAL 

from ..maskedImage import MaskedImage, MaskedImageF 

from ..slicing import imageIndicesToNumpy 

from ...multiband import MultibandBase 

 

 

class MultibandPixel(MultibandBase): 

"""Multiband Pixel class 

 

This represent acts as a container for a single pixel 

(scalar) in multiple bands. 

 

Parameters 

---------- 

singles: `sequence` 

Either a list of single band objects or an array of values. 

filters: `list` 

List of filter names. If `singles` is an `OrderedDict` or 

a `MultibandPixel` then this argument is ignored, 

otherwise it is required. 

position: `Point2I` 

Location of the pixel in the parent image. 

Unlike other objects that inherit from `MultibandBase`, 

`MultibandPixel` objects don't have a full `Box2I` 

bounding box, since they only contain a single pixel, 

so the bounding box cannot be inherited from the 

list of `singles`. 

""" 

def __init__(self, filters, singles, position): 

56 ↛ 57line 56 didn't jump to line 57, because the condition on line 56 was never true if any([arg is None for arg in [singles, filters, position]]): 

err = "Expected an array of `singles`, a list of `filters, and a `bbox`" 

raise NotImplementedError(err) 

 

# Make sure that singles is an array 

singles = np.array(singles, copy=False) 

 

super().__init__(filters, singles, bbox=Box2I(position, Extent2I(1, 1))) 

# In this case we want self.singles to be an array 

self._singles = singles 

 

# Make sure that the bounding box has been setup properly 

assert self.getBBox().getDimensions() == Extent2I(1, 1) 

 

def _getArray(self): 

"""Data cube array in multiple bands 

 

Since `self._singles` is just a 1D array, 

`array` just returns `self._singles`. 

""" 

return self.singles 

 

def _setArray(self, value): 

assert value.shape == self.array.shape 

self._singles[:] = value 

 

array = property(_getArray, _setArray) 

 

def clone(self, deep=True): 

"""Make a copy of the current instance 

 

`MultibandPixel.singles` is an array, 

so this just makes a copy of the array 

(as opposed to a view of the parent array). 

""" 

if deep: 

singles = np.copy(self.singles) 

position = self.getBBox().getMin() 

return MultibandPixel(filters=self.filters, singles=singles, position=position) 

 

result = MultibandPixel(filters=self.filters, singles=self.singles, position=self.getBBox().getMin()) 

result._bbox = self.getBBox() 

return result 

 

def __getitem__(self, indices): 

"""Get a slice of the underlying array 

 

Since a `MultibandPixel` is a scalar in the 

spatial dimensions, it can only be indexed with 

a filter name, number, or slice. 

""" 

107 ↛ 108line 107 didn't jump to line 108, because the condition on line 107 was never true if isinstance(indices, tuple): 

err = ("Too many indices: " 

"`MultibandPixel has no spatial dimensions and " 

"only accepts a filterIndex") 

raise IndexError(err) 

# Make the index into a valid numpy index or slice 

filters, filterIndex = self._filterNamesToIndex(indices) 

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

# The user only requested a pixel in a single band, so return it 

return self.array[filterIndex[0]] 

 

result = self.clone(False) 

result._filters = filters 

result._singles = self._singles[filterIndex] 

# No need to update the bounding box, since pixels can only be sliced in the filter dimension 

return result 

 

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

pass 

 

 

class MultibandImageBase(MultibandBase): 

"""Multiband Image class 

 

This class acts as a container for multiple `afw.Image` objects. 

All images must be contained in the same bounding box, 

and have the same data type. 

The data is stored in a 3D array (filters, y, x), and the single 

band `Image` instances have an internal array that points to the 

3D multiband array, so that the single band objects and multiband 

array are always in agreement. 

 

Parameters 

---------- 

filters: `list` 

List of filter names. 

array: 3D numpy array 

Array (filters, y, x) of multiband data. 

If this is used to initialize a `MultibandImage`, 

either `bbox` or `singles` is also required. 

singleType: `type` 

Type of the single band object (eg. `Image`, `Mask`) to 

convert the array into a tuple of single band objects 

that point to the image array. 

bbox: `Box2I` 

Location of the array in a larger single band image. 

If `bbox` is `None` then the bounding box is initialized 

at the origin. 

""" 

def __init__(self, filters, array, singleType, bbox=None): 

# Create the single band objects from the array 

158 ↛ 159line 158 didn't jump to line 159, because the condition on line 158 was never true if len(array) != len(filters): 

raise ValueError("`array` and `filters` must have the same length") 

self._array = array 

self._filters = tuple(filters) 

if bbox is None: 

bbox = Box2I(Point2I(0, 0), Extent2I(array.shape[2], array.shape[1])) 

self._bbox = bbox 

 

xy0 = self.getXY0() 

dtype = array.dtype 

self._singles = tuple([singleType(array=array[n], xy0=xy0, dtype=dtype) for n in range(len(array))]) 

 

# Make sure that all of the parameters have been setup appropriately 

assert isinstance(self._bbox, Box2I) 

assert len(self.singles) == len(self.filters) 

 

def _getArray(self): 

"""Data cube array in multiple bands 

 

Returns 

------- 

self._array: array 

The resulting 3D data cube with shape (filters, y, x). 

""" 

return self._array 

 

def _setArray(self, value): 

"""Set the values of the array""" 

self.array[:] = value 

 

array = property(_getArray, _setArray) 

 

def clone(self, deep=True): 

"""Copy the current object 

 

Parameters 

---------- 

deep: `bool` 

Whether or not to make a deep copy 

""" 

if deep: 

array = np.copy(self.array) 

bbox = Box2I(self.getBBox()) 

else: 

array = self.array 

bbox = self.getBBox() 

result = type(self)(self.filters, array, bbox) 

return result 

 

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

"""Slice the current object and return the result 

 

See `MultibandBase._slice` for a list of the parameters. 

""" 

if len(indices) > 0: 

if len(indices) == 1: 

indices = indices[0] 

allSlices = [filterIndex, slice(None), slice(None)] 

sy, sx, bbox = imageIndicesToNumpy(indices, self.getBBox) 

217 ↛ 219line 217 didn't jump to line 219, because the condition on line 217 was never false if sy is not None: 

allSlices[-2] = sy 

219 ↛ 221line 219 didn't jump to line 221, because the condition on line 219 was never false if sx is not None: 

allSlices[-1] = sx 

array = self._array[allSlices] 

 

# Return a scalar or MultibandPixel 

# if the image indices are integers 

if bbox is None: 

226 ↛ 227line 226 didn't jump to line 227, because the condition on line 226 was never true if not isinstance(filterIndex, slice) and len(filterIndex) == 1: 

return array[0] 

result = MultibandPixel( 

singles=array, 

filters=filters, 

position=Point2I(sx + self.x0, sy + self.y0) 

) 

return result 

else: 

array = self._array[filterIndex] 

bbox = self.getBBox() 

result = type(self)(filters, array, bbox) 

 

# Check that the image and array shapes agree 

imageShape = ( 

len(result.filters), 

result.getBBox().getHeight(), 

result.getBBox().getWidth() 

) 

assert result.array.shape == imageShape 

 

return result 

 

def __setitem__(self, args, value): 

"""Set a subset of the MultibandImage 

""" 

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 len(indices) > 1: 

sy, sx, bbox = imageIndicesToNumpy(indices[1:], self.getBBox) 

else: 

sy = sx = slice(None) 

264 ↛ 265line 264 didn't jump to line 265, because the condition on line 264 was never true if hasattr(value, "array"): 

self._array[filterIndex, sy, sx] = value.array 

else: 

self._array[filterIndex, sy, sx] = value 

 

def getBBox(self, origin=PARENT): 

"""Bounding box 

""" 

if origin == PARENT: 

return self._bbox 

274 ↛ 276line 274 didn't jump to line 276, because the condition on line 274 was never false elif origin == LOCAL: 

return Box2I(Point2I(0, 0), self._bbox.getDimensions()) 

raise ValueError("Unrecognized origin, expected either PARENT or LOCAL") 

 

 

def makeImageFromSingles(cls, filters, singles): 

"""Construct a MultibandImage from a collection of single band images 

 

Parameters 

---------- 

filters: `list` 

List of filter names. 

singles: `list` 

A list of single band objects. 

If `array` is not `None`, then `singles` is ignored 

""" 

array = np.array([image.array for image in singles], dtype=singles[0].array.dtype) 

291 ↛ 292line 291 didn't jump to line 292, because the condition on line 291 was never true if not np.all([image.getBBox() == singles[0].getBBox() for image in singles[1:]]): 

raise ValueError("Single band images did not all have the same bounding box") 

bbox = singles[0].getBBox() 

return cls(filters, array, bbox) 

 

 

def makeImageFromKwargs(cls, filters, filterKwargs, singleType=ImageF, **kwargs): 

"""Build a MultibandImage from a set of keyword arguments 

 

Parameters 

---------- 

filters: `list` 

List of filter names. 

singleType: class 

Class of the single band objects. 

This is ignored unless `singles` and `array` 

are both `None`, in which case it is required. 

filterKwargs: `dict` 

Keyword arguments to initialize a new instance of an inherited class 

that are different for each filter. 

The keys are the names of the arguments and the values 

should also be dictionaries, with filter names as keys 

and the value of the argument for a given filter as values. 

kwargs: `dict` 

Keyword arguments to initialize a new instance of an 

inherited class that are the same in all bands. 

""" 

# Attempt to load a set of images 

singles = [] 

for f in filters: 

if filterKwargs is not None: 

for key, value in filterKwargs: 

kwargs[key] = value[f] 

singles.append(singleType(**kwargs)) 

return cls.makeImageFromSingles(filters, singles) 

 

 

class MultibandImage(MultibandImageBase): 

"""Multiband Image class 

 

See `MultibandImageBase` for a description of the parameters. 

""" 

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

super().__init__(filters, array, Image, bbox) 

 

@staticmethod 

def fromImages(filters, singles): 

"""Construct a MultibandImage from a collection of single band images 

 

see `fromSingles` for a description of parameters 

""" 

return makeImageFromSingles(MultibandImage, filters, singles) 

 

@staticmethod 

def fromKwargs(filters, filterKwargs, singleType=ImageF, **kwargs): 

"""Build a MultibandImage from a set of keyword arguments 

 

see `makeImageFromKwargs` for a description of parameters 

""" 

return makeImageFromKwargs(MultibandImage, filters, filterKwargs, singleType, **kwargs) 

 

 

class MultibandMask(MultibandImageBase): 

"""Multiband Mask class 

 

See `MultibandImageBase` for a description of the parameters. 

""" 

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

super().__init__(filters, array, Mask, bbox) 

# Set Mask specific properties 

self._refMask = self._singles[0] 

refMask = self._refMask 

assert np.all([refMask.getMaskPlaneDict() == m.getMaskPlaneDict() for m in self.singles]) 

assert np.all([refMask.getNumPlanesMax() == m.getNumPlanesMax() for m in self.singles]) 

assert np.all([refMask.getNumPlanesUsed() == m.getNumPlanesUsed() for m in self.singles]) 

 

@staticmethod 

def fromMasks(filters, singles): 

"""Construct a MultibandImage from a collection of single band images 

 

see `fromSingles` for a description of parameters 

""" 

return makeImageFromSingles(MultibandMask, filters, singles) 

 

@staticmethod 

def fromKwargs(filters, filterKwargs, singleType=ImageF, **kwargs): 

"""Build a MultibandImage from a set of keyword arguments 

 

see `makeImageFromKwargs` for a description of parameters 

""" 

return makeImageFromKwargs(MultibandMask, filters, filterKwargs, singleType, **kwargs) 

 

def getMaskPlane(self, key): 

"""Get the bit number of a mask in the `MaskPlaneDict` 

 

Each `key` in the mask plane has an associated bit value 

in the mask. This method returns the bit number of the 

`key` in the `MaskPlaneDict`. 

This is in contrast to `getPlaneBitMask`, which returns the 

value of the bit number. 

 

For example, if `getMaskPlane` returns `8`, then `getPlaneBitMask` 

returns `256`. 

 

Parameters 

---------- 

key: `str` 

Name of the key in the `MaskPlaneDict` 

 

Returns 

------- 

bit: `int` 

Bit number for mask `key` 

""" 

return self._refMask.getMaskPlaneDict()[key] 

 

def getPlaneBitMask(self, names): 

"""Get the bit number of a mask in the `MaskPlaneDict` 

 

Each `key` in the mask plane has an associated bit value 

in the mask. This method returns the bit number of the 

`key` in the `MaskPlaneDict`. 

This is in contrast to `getPlaneBitMask`, which returns the 

value of the bit number. 

 

For example, if `getMaskPlane` returns `8`, then `getPlaneBitMask` 

returns `256`. 

 

Parameters 

---------- 

names: `str` or list of `str` 

Name of the key in the `MaskPlaneDict` or a list of keys. 

If multiple keys are used, the value returned is the integer 

value of the number with all of the bit values in `names`. 

 

For example if `MaskPlaneDict("CR")=3` and 

`MaskPlaneDict("NO_DATA)=8`, then 

`getPlaneBitMask(("CR", "NO_DATA"))=264` 

 

Returns 

------- 

bit value: `int` 

Bit value for all of the combined bits described by `names`. 

""" 

return self._refMask.getPlaneBitMask(names) 

 

def getNumPlanesMax(self): 

"""Maximum number of mask planes available 

 

This is required to be the same for all of the single 

band `Mask` objects. 

""" 

return self._refMask.getNumPlanesMax() 

 

def getNumPlanesUsed(self): 

"""Number of mask planes used 

 

This is required to be the same for all of the single 

band `Mask` objects. 

""" 

return self._refMask.getNumPlanesUsed() 

 

def getMaskPlaneDict(self): 

"""Dictionary of Mask Plane bit values 

""" 

return self._refMask.getMaskPlaneDict() 

 

@staticmethod 

def clearMaskPlaneDict(): 

"""Reset the mask plane dictionary 

""" 

Mask[MaskPixel].clearMaskPlaneDict() 

 

@staticmethod 

def addMaskPlane(name): 

"""Add a mask to the mask plane 

 

Parameters 

---------- 

name: `str` 

Name of the new mask plane 

 

Returns 

------- 

index: `int` 

Bit value of the mask in the mask plane. 

""" 

idx = Mask[MaskPixel].addMaskPlane(name) 

return idx 

 

@staticmethod 

def removeMaskPlane(name): 

"""Remove a mask from the mask plane 

 

Parameters 

---------- 

name: `str` 

Name of the mask plane to remove 

""" 

Mask[MaskPixel].removeMaskPlaneDict() 

 

def clearAllMaskPlanes(self): 

"""Clear all the pixels 

""" 

self._refMask.clearAllMaskPlanes() 

 

def _getOtherMasks(self, others): 

"""Check if two masks can be combined 

 

This method checks that `self` and `others` 

have the same number of bands, or if 

others is a single value, creates a list 

to use for updating all of the `singles`. 

""" 

505 ↛ 511line 505 didn't jump to line 511, because the condition on line 505 was never false if isinstance(others, MultibandMask): 

506 ↛ 507line 506 didn't jump to line 507, because the condition on line 506 was never true if len(self.singles) != len(others.singles) or self.filters != others.filters: 

msg = "Both `MultibandMask` objects must have the same number of bands to combine" 

raise ValueError(msg) 

result = [s for s in others.singles] 

else: 

result = [others]*len(self.singles) 

return result 

 

def __ior__(self, others): 

_others = self._getOtherMasks(others) 

for s, o in zip(self.singles, _others): 

s |= o 

return self 

 

def __iand__(self, others): 

_others = self._getOtherMasks(others) 

for s, o in zip(self.singles, _others): 

s &= o 

return self 

 

def __ixor__(self, others): 

_others = self._getOtherMasks(others) 

for s, o in zip(self.singles, _others): 

s ^= o 

return self 

 

 

class MultibandTripleBase(MultibandBase): 

"""MultibandTripleBase class 

 

This is a base class inherited by multiband classes 

with `image`, `mask`, and `variance` objects, 

such as `MultibandMaskedImage` and `MultibandExposure`. 

 

Parameters 

---------- 

filters: `list` 

List of filter names. If `singles` is an `OrderedDict` 

then this argument is ignored, otherwise it is required. 

image: `list` or `MultibandImage` 

List of `Image` objects that represent the image in each band or 

a `MultibandImage`. 

Ignored if `singles` is not `None`. 

mask: `list` or `MultibandMask` 

List of `Mask` objects that represent the mask in each bandor 

a `MultibandMask`. 

Ignored if `singles` is not `None`. 

variance: `list` or `MultibandImage` 

List of `Image` objects that represent the variance in each bandor 

a `MultibandImage`. 

Ignored if `singles` is not `None`. 

""" 

def __init__(self, filters, image, mask, variance): 

self._filters = tuple(filters) 

# Convert single band images into multiband images 

if not isinstance(image, MultibandBase): 

image = MultibandImage.fromImages(filters, image) 

563 ↛ 564line 563 didn't jump to line 564, because the condition on line 563 was never true if mask is not None: 

mask = MultibandMask.fromMasks(filters, mask) 

565 ↛ 566line 565 didn't jump to line 566, because the condition on line 565 was never true if variance is not None: 

variance = MultibandImage.fromImages(filters, variance) 

self._image = image 

self._mask = mask 

self._variance = variance 

 

self._singles = self._buildSingles(self._image, self._mask, self._variance) 

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

 

def setXY0(self, xy0): 

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

This is different than `MultibandBase.setXY0` 

because the multiband `image`, `mask`, and `variance` objects 

must all have their bounding boxes updated. 

Parameters 

---------- 

xy0: `Point2I` 

New minimum bounds of the bounding box 

""" 

super().setXY0(xy0) 

self.image.setXY0(xy0) 

586 ↛ 588line 586 didn't jump to line 588, because the condition on line 586 was never false if self.mask is not None: 

self.mask.setXY0(xy0) 

588 ↛ exitline 588 didn't return from function 'setXY0', because the condition on line 588 was never false if self.variance is not None: 

self.variance.setXY0(xy0) 

 

def shiftedTo(self, xy0): 

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

 

This is different than `MultibandBase.shiftedTo` 

because the multiband `image`, `mask`, and `variance` objects 

must all have their bounding boxes updated. 

 

Parameters 

---------- 

xy0: `Point2I` 

New minimum bounds of the bounding box 

 

Returns 

------- 

result: `MultibandBase` 

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

""" 

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

result = self.clone(False) 

result._image = result.image.shiftedTo(xy0) 

if self.mask is not None: 

result._mask = result.mask.shiftedTo(xy0) 

if self.variance is not None: 

result._variance = result.variance.shiftedTo(xy0) 

result._bbox = result.image.getBBox() 

return result 

 

def clone(self, deep=True): 

"""Make a copy of the current instance 

""" 

image = self.image.clone(deep) 

622 ↛ 625line 622 didn't jump to line 625, because the condition on line 622 was never false if self.mask is not None: 

mask = self.mask.clone(deep) 

else: 

mask = None 

626 ↛ 629line 626 didn't jump to line 629, because the condition on line 626 was never false if self.variance is not None: 

variance = self.variance.clone(deep) 

else: 

variance = None 

return type(self)(self.filters, image, mask, variance) 

 

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

"""Slice the current object and return the result 

 

See `Multiband._slice` for a list of the parameters. 

""" 

image = self.image._slice(filters, filterIndex, indices) 

638 ↛ 641line 638 didn't jump to line 641, because the condition on line 638 was never false if self.mask is not None: 

mask = self._mask._slice(filters, filterIndex, indices) 

else: 

mask = None 

642 ↛ 645line 642 didn't jump to line 645, because the condition on line 642 was never false if self.variance is not None: 

variance = self._variance._slice(filters, filterIndex, indices) 

else: 

variance = None 

 

# If only a single pixel is selected, return the tuple of MultibandPixels 

if isinstance(image, MultibandPixel): 

649 ↛ 651line 649 didn't jump to line 651, because the condition on line 649 was never false if mask is not None: 

assert isinstance(mask, MultibandPixel) 

651 ↛ 653line 651 didn't jump to line 653, because the condition on line 651 was never false if variance is not None: 

assert isinstance(variance, MultibandPixel) 

return (image, mask, variance) 

 

result = type(self)(filters=filters, image=image, mask=mask, variance=variance) 

assert all([r.getBBox() == result._bbox for r in [result._mask, result._variance]]) 

return result 

 

def _verifyUpdate(self, image=None, mask=None, variance=None): 

"""Check that the new image, mask, or variance is valid 

 

This basically means checking that the update to the 

property matches the current bounding box and inherits 

from the `MultibandBase` class. 

""" 

for prop in [image, mask, variance]: 

if prop is not None: 

if prop.getBBox() != self.getBBox(): 

raise ValueError("Bounding box does not match the current class") 

if not isinstance(prop, MultibandBase): 

err = "image, mask, and variance should all inherit from the MultibandBase class" 

raise ValueError(err) 

 

@property 

def image(self): 

"""The image of the MultibandMaskedImage""" 

return self._image 

 

@property 

def mask(self): 

"""The mask of the MultibandMaskedImage""" 

return self._mask 

 

@property 

def variance(self): 

"""The variance of the MultibandMaskedImage""" 

return self._variance 

 

def getBBox(self, origin=PARENT): 

"""Bounding box 

""" 

692 ↛ 694line 692 didn't jump to line 694, because the condition on line 692 was never false if origin == PARENT: 

return self._bbox 

elif origin == LOCAL: 

return Box2I(Point2I(0, 0), self._bbox.getDimensions()) 

raise ValueError("Unrecognized origin, expected either PARENT or LOCAL") 

 

 

def tripleFromSingles(cls, filters, singles): 

"""Construct a MultibandTriple from a collection of single band objects 

 

Parameters 

---------- 

filters: `list` 

List of filter names. 

singles: `list` 

A list of single band objects. 

If `array` is not `None`, then `singles` is ignored 

""" 

710 ↛ 711line 710 didn't jump to line 711, because the condition on line 710 was never true if not np.all([single.getBBox() == singles[0].getBBox() for single in singles[1:]]): 

raise ValueError("Single band images did not all have the same bounding box") 

image = MultibandImage.fromImages(filters, [s.image for s in singles]) 

mask = MultibandMask.fromMasks(filters, [s.mask for s in singles]) 

variance = MultibandImage.fromImages(filters, [s.variance for s in singles]) 

return cls(filters, image, mask, variance) 

 

 

def tripleFromArrays(cls, filters, image, mask, variance, bbox=None): 

"""Construct a MultibandTriple from a set of arrays 

 

Parameters 

---------- 

filters: `list` 

List of filter names. 

image: array 

Array of image values 

mask: array 

Array of mask values 

variance: array 

Array of variance values 

bbox: `Box2I` 

Location of the array in a larger single band image. 

This argument is ignored if `singles` is not `None`. 

""" 

735 ↛ 736line 735 didn't jump to line 736, because the condition on line 735 was never true if bbox is None: 

bbox = Box2I(Point2I(0, 0), Extent2I(image.shape[1], image.shape[0])) 

mImage = MultibandImage(filters, image, bbox) 

738 ↛ 739line 738 didn't jump to line 739, because the condition on line 738 was never true if mask is not None: 

mMask = MultibandMask(filters, mask, bbox) 

else: 

mMask = None 

742 ↛ 743line 742 didn't jump to line 743, because the condition on line 742 was never true if variance is not None: 

mVariance = MultibandImage(filters, variance, bbox) 

else: 

mVariance = None 

return cls(filters, mImage, mMask, mVariance) 

 

 

def makeTripleFromKwargs(cls, filters, filterKwargs, singleType, **kwargs): 

"""Build a MultibandImage from a set of keyword arguments 

 

Parameters 

---------- 

filters: `list` 

List of filter names. 

singleType: `class` 

Class of the single band objects. 

This is ignored unless `singles` and `array` 

are both `None`, in which case it is required. 

filterKwargs: `dict` 

Keyword arguments to initialize a new instance of an inherited class 

that are different for each filter. 

The keys are the names of the arguments and the values 

should also be dictionaries, with filter names as keys 

and the value of the argument for a given filter as values. 

kwargs: `dict` 

Keyword arguments to initialize a new instance of an inherited 

class that are the same in all bands. 

""" 

# Attempt to load a set of images 

singles = [] 

for f in filters: 

if filterKwargs is not None: 

for key, value in filterKwargs: 

kwargs[key] = value[f] 

singles.append(singleType(**kwargs)) 

return tripleFromSingles(cls, filters, singles) 

 

 

class MultibandMaskedImage(MultibandTripleBase): 

"""MultibandMaskedImage class 

 

This class acts as a container for multiple `afw.MaskedImage` objects. 

All masked images must have the same bounding box, and the associated 

images must all have the same data type. 

The `image`, `mask`, and `variance` are all stored separately into 

a `MultibandImage`, `MultibandMask`, and `MultibandImage` respectively, 

which each have their own internal 3D arrays (filter, y, x). 

 

See `MultibandTripleBase` for parameter definitions. 

""" 

def __init__(self, filters, image=None, mask=None, variance=None): 

super().__init__(filters, image, mask, variance) 

 

@staticmethod 

def fromImages(filters, singles): 

"""Construct a MultibandImage from a collection of single band images 

 

see `tripleFromImages` for a description of parameters 

""" 

return tripleFromSingles(MultibandMaskedImage, filters, singles) 

 

@staticmethod 

def fromArrays(filters, image, mask, variance, bbox=None): 

"""Construct a MultibandMaskedImage from a collection of arrays 

 

see `tripleFromArrays` for a description of parameters 

""" 

return tripleFromArrays(MultibandMaskedImage, filters, image, mask, variance, bbox) 

 

@staticmethod 

def fromKwargs(filters, filterKwargs, singleType=MaskedImageF, **kwargs): 

"""Build a MultibandImage from a set of keyword arguments 

 

see `makeTripleFromKwargs` for a description of parameters 

""" 

return makeTripleFromKwargs(MultibandMaskedImage, filters, filterKwargs, singleType, **kwargs) 

 

def _buildSingles(self, image=None, mask=None, variance=None): 

"""Make a new list of single band objects 

 

Parameters 

---------- 

image: `MultibandImage` 

`MultibandImage` object that represent the image in each band. 

mask: `MultibandMask` 

`MultibandMask` object that represent the mask in each band. 

variance: `MultibandImage` 

`MultibandImage` object that represent the variance in each band. 

 

Returns 

------- 

singles: `tuple` 

Tuple of `MaskedImage` objects for each band, 

where the `image`, `mask`, and `variance` of each `single` 

point to the multiband objects. 

""" 

singles = [] 

839 ↛ 840line 839 didn't jump to line 840, because the condition on line 839 was never true if image is None: 

image = self.image 

if mask is None: 

mask = self.mask 

if variance is None: 

variance = self.variance 

 

dtype = image.array.dtype 

singles = [] 

for f in self.filters: 

_image = image[f] 

if mask is not None: 

_mask = mask[f] 

else: 

_mask = None 

if variance is not None: 

_variance = variance[f] 

else: 

_variance = None 

singles.append(MaskedImage(image=_image, mask=_mask, variance=_variance, dtype=dtype)) 

return tuple(singles)