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

# 

# LSST Data Management System 

# 

# Copyright 2008-2017 AURA/LSST. 

# 

# This product includes software developed by the 

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

# 

# 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 LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <https://www.lsstcorp.org/LegalNotices/>. 

# 

"""Test cases for lsst.cp.pipe.FindDefectsTask.""" 

 

import unittest 

import numpy as np 

import copy 

 

import lsst.utils 

import lsst.utils.tests 

 

import lsst.cp.pipe as cpPipe 

from lsst.cp.pipe.utils import countMaskedPixels 

from lsst.ip.isr import isrMock 

from lsst.geom import Box2I, Point2I, Extent2I 

import lsst.meas.algorithms as measAlg 

 

 

class FindDefectsTaskTestCase(lsst.utils.tests.TestCase): 

"""A test case for the defect finding task.""" 

 

def setUp(self): 

self.defaultConfig = cpPipe.defects.FindDefectsTask.ConfigClass() 

 

for config in [self.defaultConfig.isrForDarks, self.defaultConfig.isrForFlats]: 

config.doCrosstalk = False 

config.doAddDistortionModel = False 

config.doUseOpticsTransmission = False 

config.doUseFilterTransmission = False 

config.doUseSensorTransmission = False 

config.doUseAtmosphereTransmission = False 

config.doAttachTransmissionCurve = False 

 

self.flatMean = 2000 

self.darkMean = 1 

self.readNoiseAdu = 10 

self.nSigmaBright = 8 

self.nSigmaDark = 8 

 

mockImageConfig = isrMock.IsrMock.ConfigClass() 

 

# flatDrop is not really relevant as we replace the data 

# but good to note it in case we change how this image is made 

mockImageConfig.flatDrop = 0.99999 

mockImageConfig.isTrimmed = True 

 

self.flatExp = isrMock.FlatMock(config=mockImageConfig).run() 

(shapeY, shapeX) = self.flatExp.getDimensions() 

 

# x, y, size tuples 

# always put edge defects at the start and change the value of nEdge 

 

self.brightDefects = [(0, 15, 3, 3), (100, 123, 1, 1)] 

 

self.darkDefects = [(5, 0, 1, 1), (7, 62, 2, 2)] 

 

nEdge = 1 # NOTE: update if more edge defects are included 

self.noEdges = slice(nEdge, None) 

self.onlyEdges = slice(0, nEdge) 

 

self.darkBBoxes = [Box2I(Point2I(x, y), Extent2I(sx, sy)) for (x, y, sx, sy) in self.darkDefects] 

self.brightBBoxes = [Box2I(Point2I(x, y), Extent2I(sx, sy)) for (x, y, sx, sy) in self.brightDefects] 

 

flatWidth = np.sqrt(self.flatMean) + self.readNoiseAdu 

darkWidth = self.readNoiseAdu 

self.rng = np.random.RandomState(0) 

flatData = self.rng.normal(self.flatMean, flatWidth, (shapeX, shapeY)) 

darkData = self.rng.normal(self.darkMean, darkWidth, (shapeX, shapeY)) 

 

# NOTE: darks and flats have same defects applied deliberately to both 

for defect in self.brightDefects: 

y, x, sy, sx = defect 

# are these actually the numbers we want? 

flatData[x:x+sx, y:y+sy] += self.nSigmaBright * flatWidth 

darkData[x:x+sx, y:y+sy] += self.nSigmaBright * darkWidth 

 

for defect in self.darkDefects: 

y, x, sy, sx = defect 

# are these actually the numbers we want? 

flatData[x:x+sx, y:y+sy] -= self.nSigmaDark * flatWidth 

darkData[x:x+sx, y:y+sy] -= self.nSigmaDark * darkWidth 

 

self.darkExp = self.flatExp.clone() 

self.spareImage = self.flatExp.clone() # for testing edge bits and misc 

 

self.flatExp.image.array[:] = flatData 

self.darkExp.image.array[:] = darkData 

 

self.defaultTask = cpPipe.defects.FindDefectsTask(config=self.defaultConfig) 

 

self.allDefectsList = measAlg.Defects() 

 

self.brightDefectsList = measAlg.Defects() 

for d in self.brightBBoxes: 

self.brightDefectsList.append(d) 

self.allDefectsList.append(d) 

 

self.darkDefectsList = measAlg.Defects() 

for d in self.darkBBoxes: 

self.darkDefectsList.append(d) 

self.allDefectsList.append(d) 

 

def check_maskBlocks(self, inputDefects, expectedDefects): 

"""A helper function for the tests of maskBlocksIfIntermitentBadPixelsInColumn. 

""" 

config = copy.copy(self.defaultConfig) 

config.badOnAndOffPixelColumnThreshold = 10 

config.goodPixelColumnGapThreshold = 5 

config.nPixBorderUpDown = 0 

config.nPixBorderLeftRight = 0 

 

task = cpPipe.defects.FindDefectsTask(config=config) 

 

defectsWithColumns = task.maskBlocksIfIntermitentBadPixelsInColumn(inputDefects) 

 

boxesMeasured = [] 

for defect in defectsWithColumns: 

boxesMeasured.append(defect.getBBox()) 

 

for boxInput in expectedDefects: 

self.assertIn(boxInput, boxesMeasured) 

 

# Check that the code did not mask anything extra by 

# looking in both the input list and "expanded-column" list. 

unionInputExpectedBoxes = [] 

for defect in inputDefects: 

unionInputExpectedBoxes.append(defect.getBBox()) 

for defect in expectedDefects: 

unionInputExpectedBoxes.append(defect) 

 

# Check that code doesn't mask more than it is supposed to. 

for boxMeas in boxesMeasured: 

self.assertIn(boxMeas, unionInputExpectedBoxes) 

 

def test_maskBlocks_full_column(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Tests that a contigous bad column does not get split by the code. 

 

The mock flat has a size of 200X204 pixels. This column has a maximum length of 50 

pixels, otherwise there would be a split along the mock amp boundary. 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

defects = self.allDefectsList 

defects.append(Box2I(corner=Point2I(15, 1), dimensions=Extent2I(1, 50))) 

expectedDefects = [Box2I(corner=Point2I(15, 1), dimensions=Extent2I(1, 50))] 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_long_column(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Tests that a contigous bad column with Npix >= badOnAndOffPixelColumnThreshold (10) 

does not get split by the code. 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(20, 1), dimensions=Extent2I(1, 25))] 

defects = self.allDefectsList 

defects.append(Box2I(corner=Point2I(20, 1), dimensions=Extent2I(1, 25))) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_short_column(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Tests that a contigous bad column Npix < badOnAndOffPixelColumnThreshold (10) 

does not get split by the code. 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(25, 1), dimensions=Extent2I(1, 8))] 

defects = self.allDefectsList 

defects.append(Box2I(corner=Point2I(25, 1), dimensions=Extent2I(1, 8))) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_discontigous_to_single_block(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in a column where Npix >= badOnAndOffPixelColumnThreshold (10) 

and gaps of good pixels < goodPixelColumnGapThreshold (5). Under these conditions, the whole 

block of bad pixels (including good gaps) should be masked. 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(30, 1), dimensions=Extent2I(1, 48))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(30, 1), dimensions=Extent2I(1, 2)), 

Box2I(corner=Point2I(30, 5), dimensions=Extent2I(1, 3)), 

Box2I(corner=Point2I(30, 11), dimensions=Extent2I(1, 5)), 

Box2I(corner=Point2I(30, 19), dimensions=Extent2I(1, 5)), 

Box2I(corner=Point2I(30, 27), dimensions=Extent2I(1, 4)), 

Box2I(corner=Point2I(30, 34), dimensions=Extent2I(1, 15))] 

for badBox in badPixels: 

defects.append(badBox) 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_discontigous_less_than_thresholds(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in a column where Npix < badOnAndOffPixelColumnThreshold (10) 

and gaps of good pixels < goodPixelColumnGapThreshold (5). Under these conditions, 

the expected defect boxes should be the same as the input boxes. 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(35, 1), dimensions=Extent2I(1, 2)), 

Box2I(corner=Point2I(35, 5), dimensions=Extent2I(1, 3)), 

Box2I(corner=Point2I(35, 11), dimensions=Extent2I(1, 2))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(35, 1), dimensions=Extent2I(1, 2)), 

Box2I(corner=Point2I(35, 5), dimensions=Extent2I(1, 3)), 

Box2I(corner=Point2I(35, 11), dimensions=Extent2I(1, 2))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_more_than_thresholds(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in a column where Npix < badOnAndOffPixelColumnThreshold (10) 

and gaps of good pixels < goodPixelColumnGapThreshold (5). 

Npix=34 (> 10) bad pixels total, 1 "good" gap with 13 pixels big enough 

(13 >= 5 good pixels, from y=6 (1+5) to y=19). 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(40, 1), dimensions=Extent2I(1, 7)), 

Box2I(corner=Point2I(40, 19), dimensions=Extent2I(1, 30))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(40, 1), dimensions=Extent2I(1, 2)), 

Box2I(corner=Point2I(40, 5), dimensions=Extent2I(1, 3)), 

Box2I(corner=Point2I(40, 19), dimensions=Extent2I(1, 5)), 

Box2I(corner=Point2I(40, 27), dimensions=Extent2I(1, 4)), 

Box2I(corner=Point2I(40, 34), dimensions=Extent2I(1, 15))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_not_enough_bad_pixels_in_column(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in a column where Npix < badOnAndOffPixelColumnThreshold (10) and 

and gaps of good pixels > goodPixelColumnGapThreshold (5). Since Npix < 

badOnAndOffPixelColumnThreshold, then it doesn't matter that the number of good pixels in gap > 

goodPixelColumnGapThreshold. 5<10 bad pixels total, 1 "good" gap big enough 

(29>=5 good pixels, from y =12 (10+2) to y=30) 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(45, 10), dimensions=Extent2I(1, 2)), 

Box2I(corner=Point2I(45, 30), dimensions=Extent2I(1, 3))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(45, 10), dimensions=Extent2I(1, 2)), 

Box2I(corner=Point2I(45, 30), dimensions=Extent2I(1, 3))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_every_other_pixel_bad_greater_than_threshold(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in a column where Npix > badOnAndOffPixelColumnThreshold (10) 

and every other pixel is bad. 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(50, 10), dimensions=Extent2I(1, 31))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(50, 10), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 12), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 14), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 16), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 18), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 20), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 22), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 24), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 26), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 28), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 30), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 32), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 34), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 36), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 38), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(50, 40), dimensions=Extent2I(1, 1))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_every_other_pixel_bad_less_than_threshold(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in a column where Npix > badOnAndOffPixelColumnThreshold (10) 

and every other pixel is bad. 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(55, 20), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(55, 22), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(55, 24), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(55, 26), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(55, 28), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(55, 30), dimensions=Extent2I(1, 1))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(55, 20), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(55, 22), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(55, 24), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(55, 26), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(55, 28), dimensions=Extent2I(1, 1)), 

Box2I(corner=Point2I(55, 30), dimensions=Extent2I(1, 1))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_blobs_one_side_good_less_than_threshold(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in column with "blobs" of "m" bad pixels to one side, 

m > badOnAndOffPixelColumnThreshold (10), number of good pixel in gaps between blobs < 

goodPixelColumnGapThreshold (5). 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(60, 1), dimensions=Extent2I(1, 29)), 

Box2I(corner=Point2I(61, 2), dimensions=Extent2I(1, 12)), 

Box2I(corner=Point2I(62, 2), dimensions=Extent2I(1, 12))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(60, 1), dimensions=Extent2I(1, 18)), 

Box2I(corner=Point2I(60, 20), dimensions=Extent2I(1, 10)), 

Box2I(corner=Point2I(61, 2), dimensions=Extent2I(2, 2)), 

Box2I(corner=Point2I(61, 6), dimensions=Extent2I(2, 8))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_blobs_other_side_good_less_than_threshold(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in column with "blobs" of "m" bad pixels to the other side, 

m > badOnAndOffPixelColumnThreshold (10), number of good pixel in gaps between blobs < 

goodPixelColumnGapThreshold (5). 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(70, 1), dimensions=Extent2I(1, 29)), 

Box2I(corner=Point2I(68, 2), dimensions=Extent2I(1, 12)), 

Box2I(corner=Point2I(69, 2), dimensions=Extent2I(1, 12))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(70, 1), dimensions=Extent2I(1, 18)), 

Box2I(corner=Point2I(70, 20), dimensions=Extent2I(1, 10)), 

Box2I(corner=Point2I(68, 2), dimensions=Extent2I(2, 2)), 

Box2I(corner=Point2I(68, 6), dimensions=Extent2I(2, 8))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_blob_both_sides_good_less_than_threshold(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in column with "blobs" of "m" bad pixels to both sides, 

m > badOnAndOffPixelColumnThreshold (10), number of good pixel in gaps between blobs < 

goodPixelColumnGapThreshold (5). 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(75, 1), dimensions=Extent2I(1, 29)), 

Box2I(corner=Point2I(73, 2), dimensions=Extent2I(1, 12)), 

Box2I(corner=Point2I(74, 2), dimensions=Extent2I(1, 12)), 

Box2I(corner=Point2I(76, 2), dimensions=Extent2I(1, 12)), 

Box2I(corner=Point2I(77, 2), dimensions=Extent2I(1, 12))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(75, 1), dimensions=Extent2I(1, 18)), 

Box2I(corner=Point2I(75, 20), dimensions=Extent2I(1, 10)), 

Box2I(corner=Point2I(73, 2), dimensions=Extent2I(2, 2)), 

Box2I(corner=Point2I(73, 6), dimensions=Extent2I(2, 8)), 

Box2I(corner=Point2I(76, 2), dimensions=Extent2I(2, 2)), 

Box2I(corner=Point2I(76, 6), dimensions=Extent2I(2, 8))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_blob_one_side_good_greater_than_threshold(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in column with "blobs" of "m" bad pixels to one side, 

m > badOnAndOffPixelColumnThreshold (10), number of good pixel in gaps between blobs > 

goodPixelColumnGapThreshold (5). 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(80, 1), dimensions=Extent2I(1, 29)), 

Box2I(corner=Point2I(81, 2), dimensions=Extent2I(1, 2)), 

Box2I(corner=Point2I(81, 8), dimensions=Extent2I(1, 8))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(80, 1), dimensions=Extent2I(1, 18)), 

Box2I(corner=Point2I(80, 20), dimensions=Extent2I(1, 10)), 

Box2I(corner=Point2I(81, 2), dimensions=Extent2I(2, 2)), 

Box2I(corner=Point2I(81, 8), dimensions=Extent2I(2, 8))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_other_side_good_greater_than_threshold(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in column with "blobs" of "m" bad pixels to the other side, 

m > badOnAndOffPixelColumnThreshold (10), number of good pixel in gaps between blobs > 

goodPixelColumnGapThreshold (5). 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(87, 1), dimensions=Extent2I(1, 29)), 

Box2I(corner=Point2I(85, 2), dimensions=Extent2I(1, 2)), 

Box2I(corner=Point2I(85, 8), dimensions=Extent2I(1, 8))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(87, 1), dimensions=Extent2I(1, 18)), 

Box2I(corner=Point2I(87, 20), dimensions=Extent2I(1, 10)), 

Box2I(corner=Point2I(85, 2), dimensions=Extent2I(2, 2)), 

Box2I(corner=Point2I(85, 8), dimensions=Extent2I(2, 8))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_maskBlocks_both_sides_good_greater_than_threshold(self): 

"""A test for maskBlocksIfIntermitentBadPixelsInColumn. 

Npix discontiguous bad pixels in column with "blobs" of "m" bad pixels to both sides, 

m > badOnAndOffPixelColumnThreshold (10), number of good pixel in gaps between blobs > 

goodPixelColumnGapThreshold (5). 

 

Plots can be found in DM-19903 on Jira. 

""" 

 

expectedDefects = [Box2I(corner=Point2I(93, 1), dimensions=Extent2I(1, 34)), 

Box2I(corner=Point2I(91, 2), dimensions=Extent2I(1, 7)), 

Box2I(corner=Point2I(91, 18), dimensions=Extent2I(1, 9)), 

Box2I(corner=Point2I(92, 2), dimensions=Extent2I(1, 7)), 

Box2I(corner=Point2I(92, 18), dimensions=Extent2I(1, 9)), 

Box2I(corner=Point2I(94, 2), dimensions=Extent2I(1, 7)), 

Box2I(corner=Point2I(94, 18), dimensions=Extent2I(1, 9)), 

Box2I(corner=Point2I(95, 2), dimensions=Extent2I(1, 7)), 

Box2I(corner=Point2I(95, 18), dimensions=Extent2I(1, 9))] 

defects = self.allDefectsList 

badPixels = [Box2I(corner=Point2I(93, 1), dimensions=Extent2I(1, 12)), 

Box2I(corner=Point2I(93, 15), dimensions=Extent2I(1, 20)), 

Box2I(corner=Point2I(91, 2), dimensions=Extent2I(2, 2)), 

Box2I(corner=Point2I(91, 7), dimensions=Extent2I(2, 2)), 

Box2I(corner=Point2I(94, 2), dimensions=Extent2I(2, 2)), 

Box2I(corner=Point2I(94, 7), dimensions=Extent2I(2, 2)), 

Box2I(corner=Point2I(91, 18), dimensions=Extent2I(2, 3)), 

Box2I(corner=Point2I(91, 24), dimensions=Extent2I(2, 3)), 

Box2I(corner=Point2I(94, 18), dimensions=Extent2I(2, 3)), 

Box2I(corner=Point2I(94, 24), dimensions=Extent2I(2, 3))] 

for badBox in badPixels: 

defects.append(badBox) 

 

self.check_maskBlocks(defects, expectedDefects) 

 

def test_defectFindingAllSensor(self): 

config = copy.copy(self.defaultConfig) 

config.nPixBorderLeftRight = 0 

config.nPixBorderUpDown = 0 

 

task = cpPipe.defects.FindDefectsTask(config=config) 

 

defects = task.findHotAndColdPixels(self.flatExp, 'flat') 

 

allBBoxes = self.darkBBoxes + self.brightBBoxes 

 

boxesMeasured = [] 

for defect in defects: 

boxesMeasured.append(defect.getBBox()) 

 

for expectedBBox in allBBoxes: 

self.assertIn(expectedBBox, boxesMeasured) 

 

def test_defectFindingEdgeIgnore(self): 

config = copy.copy(self.defaultConfig) 

config.nPixBorderUpDown = 0 

task = cpPipe.defects.FindDefectsTask(config=config) 

defects = task.findHotAndColdPixels(self.flatExp, 'flat') 

 

shouldBeFound = self.darkBBoxes[self.noEdges] + self.brightBBoxes[self.noEdges] 

 

boxesMeasured = [] 

for defect in defects: 

boxesMeasured.append(defect.getBBox()) 

 

for expectedBBox in shouldBeFound: 

self.assertIn(expectedBBox, boxesMeasured) 

 

shouldBeMissed = self.darkBBoxes[self.onlyEdges] + self.brightBBoxes[self.onlyEdges] 

for boxMissed in shouldBeMissed: 

self.assertNotIn(boxMissed, boxesMeasured) 

 

def test_postProcessDefectSets(self): 

"""Tests the way in which the defect sets merge. 

 

There is potential for logic errors in their combination 

so several combinations of defects and combination methods 

are tested here.""" 

defects = self.defaultTask.findHotAndColdPixels(self.flatExp, 'flat') 

 

# defect list has length one 

merged = self.defaultTask._postProcessDefectSets([defects], self.flatExp.getDimensions(), 'FRACTION') 

self.assertEqual(defects, merged) 

 

# should always be true regardless of config 

# defect list now has length 2 

merged = self.defaultTask._postProcessDefectSets([defects, defects], self.flatExp.getDimensions(), 

'FRACTION') 

self.assertEqual(defects, merged) 

 

# now start manipulating defect lists 

config = copy.copy(self.defaultConfig) 

config.combinationMode = 'FRACTION' 

config.combinationFraction = 0.85 

task = cpPipe.defects.FindDefectsTask(config=config) 

merged = task._postProcessDefectSets([defects, defects], self.flatExp.getDimensions(), 'FRACTION') 

 

defectList = [defects]*10 # 10 identical defect sets 

# remove one defect from one of them, should still be over threshold 

defectList[7] = defectList[7][:-1] 

merged = task._postProcessDefectSets(defectList, self.flatExp.getDimensions(), 'FRACTION') 

self.assertEqual(defects, merged) 

 

# remove another and should be under threshold 

defectList[3] = defectList[3][:-1] 

merged = task._postProcessDefectSets(defectList, self.flatExp.getDimensions(), 'FRACTION') 

self.assertNotEqual(defects, merged) 

 

# now test the AND and OR modes 

defectList = [defects]*10 # 10 identical defect sets 

merged = task._postProcessDefectSets(defectList, self.flatExp.getDimensions(), 'AND') 

self.assertEqual(defects, merged) 

 

defectList[7] = defectList[7][:-1] 

merged = task._postProcessDefectSets(defectList, self.flatExp.getDimensions(), 'AND') 

self.assertNotEqual(defects, merged) 

 

merged = task._postProcessDefectSets(defectList, self.flatExp.getDimensions(), 'OR') 

self.assertEqual(defects, merged) 

 

def test_pixelCounting(self): 

"""Test that the number of defective pixels identified is as expected.""" 

config = copy.copy(self.defaultConfig) 

config.nPixBorderUpDown = 0 

config.nPixBorderLeftRight = 0 

task = cpPipe.defects.FindDefectsTask(config=config) 

defects = task.findHotAndColdPixels(self.flatExp, 'flat') 

 

defectArea = 0 

for defect in defects: 

defectArea += defect.getBBox().getArea() 

 

# The columnar code will cover blocks of a column 

# with on-and-off pixels, thus creating more bad pixels 

# that what initially placed in self.brightDefects and self.darkDefects. 

# Thus, defectArea should be >= crossCheck. 

crossCheck = 0 

for x, y, sx, sy in self.brightDefects: 

crossCheck += sx*sy 

for x, y, sx, sy in self.darkDefects: 

crossCheck += sx*sy 

 

# Test the result of _nPixFromDefects() 

# via two different ways of calculating area. 

self.assertEqual(defectArea, task._nPixFromDefects(defects)) 

# defectArea should be >= crossCheck 

self.assertGreaterEqual(defectArea, crossCheck) 

 

def test_getNumGoodPixels(self): 

"""Test the the number of pixels in the image not masked is as expected.""" 

testImage = self.flatExp.clone() 

mi = testImage.maskedImage 

 

imageSize = testImage.getBBox().getArea() 

nGood = self.defaultTask._getNumGoodPixels(mi) 

 

self.assertEqual(imageSize, nGood) 

 

NODATABIT = mi.mask.getPlaneBitMask("NO_DATA") 

 

noDataBox = Box2I(Point2I(31, 49), Extent2I(3, 6)) 

testImage.mask[noDataBox] |= NODATABIT 

 

self.assertEqual(imageSize - noDataBox.getArea(), self.defaultTask._getNumGoodPixels(mi)) 

# check for misfire; we're setting NO_DATA here, not BAD 

self.assertEqual(imageSize, self.defaultTask._getNumGoodPixels(mi, 'BAD')) 

 

testImage.mask[noDataBox] ^= NODATABIT # XOR to reset what we did 

self.assertEqual(imageSize, nGood) 

 

BADBIT = mi.mask.getPlaneBitMask("BAD") 

badBox = Box2I(Point2I(85, 98), Extent2I(4, 7)) 

testImage.mask[badBox] |= BADBIT 

 

self.assertEqual(imageSize - badBox.getArea(), self.defaultTask._getNumGoodPixels(mi, 'BAD')) 

 

def test_edgeMasking(self): 

"""Check that the right number of edge pixels are masked by _setEdgeBits()""" 

testImage = self.flatExp.clone() 

mi = testImage.maskedImage 

 

self.assertEqual(countMaskedPixels(mi, 'EDGE'), 0) 

self.defaultTask._setEdgeBits(mi) 

 

hEdge = self.defaultConfig.nPixBorderLeftRight 

vEdge = self.defaultConfig.nPixBorderUpDown 

xSize, ySize = mi.getDimensions() 

 

nEdge = xSize*vEdge*2 + ySize*hEdge*2 - hEdge*vEdge*4 

 

self.assertEqual(countMaskedPixels(mi, 'EDGE'), nEdge) 

 

 

class TestMemory(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

656 ↛ 657line 656 didn't jump to line 657, because the condition on line 656 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()