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

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

# This file is part of ap_association. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://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 <https://www.gnu.org/licenses/>. 

 

from astropy.stats import median_absolute_deviation 

import numpy as np 

from scipy.optimize import lsq_linear 

from scipy.stats import skew 

 

import lsst.geom as geom 

import lsst.pex.config as pexConfig 

 

from .diaCalculation import ( 

DiaObjectCalculationPluginConfig, 

DiaObjectCalculationPlugin) 

from lsst.meas.base.pluginRegistry import register 

 

__all__ = ("MeanDiaPositionConfig", "MeanDiaPosition", 

"WeightedMeanDiaPsFluxConfig", "WeightedMeanDiaPsFlux", 

"PercentileDiaPsFlux", "PercentileDiaPsFluxConfig", 

"SigmaDiaPsFlux", "SigmaDiaPsFluxConfig", 

"Chi2DiaPsFlux", "Chi2DiaPsFluxConfig", 

"MadDiaPsFlux", "MadDiaPsFluxConfig", 

"SkewDiaPsFlux", "SkewDiaPsFluxConfig", 

"MinMaxDiaPsFlux", "MinMaxDiaPsFluxConfig", 

"MaxSlopeDiaPsFlux", "MaxSlopeDiaPsFluxConfig", 

"ErrMeanDiaPsFlux", "ErrMeanDiaPsFluxConfig", 

"LinearFitDiaPsFlux", "LinearFitDiaPsFluxConfig", 

"StetsonJDiaPsFlux", "StetsonJDiaPsFluxConfig", 

"WeightedMeanDiaTotFlux", "WeightedMeanDiaTotFluxConfig", 

"SigmaDiaTotFlux", "SigmaDiaTotFluxConfig") 

 

 

class MeanDiaPositionConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_meanPosition") 

class MeanDiaPosition(DiaObjectCalculationPlugin): 

"""Compute the mean position of a DiaObject given a set of DiaSources. 

""" 

 

ConfigClass = MeanDiaPositionConfig 

outputCols = ["ra", "decl"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, diaObject, diaSources, **kwargs): 

"""Compute the mean ra/dec position of the diaObject given the 

diaSource locations. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

Catalog of DiaSources summarized by this DiaObject. 

""" 

aveCoord = geom.averageSpherePoint( 

list(geom.SpherePoint(src["ra"], src["decl"], geom.degrees) 

for idx, src in diaSources.iterrows())) 

if not (np.isfinite(aveCoord.getRa().asDegrees()) and 

np.isfinite(aveCoord.getDec().asDegrees())): 

self.fail(diaObject, self.outputCols) 

else: 

diaObject["ra"] = aveCoord.getRa().asDegrees() 

diaObject["decl"] = aveCoord.getDec().asDegrees() 

 

 

class WeightedMeanDiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_meanFlux") 

class WeightedMeanDiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute the weighted mean and mean error on the point source fluxes 

of the DiaSource measured on the difference image. 

 

Additionally store number of usable data points. 

""" 

 

ConfigClass = WeightedMeanDiaPsFluxConfig 

outputCols = ["PSFluxMean", "PSFluxMeanErr", "PSFluxNdata"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the weighted mean and mean error of the point source flux. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 0: 

tot_weight = np.nansum(1 / filterDiaFluxes["psFluxErr"] ** 2) 

fluxMean = np.nansum(filterDiaFluxes["psFlux"] / 

filterDiaFluxes["psFluxErr"] ** 2) 

fluxMean /= tot_weight 

fluxMeanErr = np.sqrt(1 / tot_weight) 

nFluxData = np.sum(np.isfinite(filterDiaFluxes["psFlux"])) 

else: 

fluxMean = np.nan 

fluxMeanErr = np.nan 

nFluxData = 0 

if np.isfinite(fluxMean) and np.isfinite(fluxMeanErr): 

diaObject["{}PSFluxMean".format(filterName)] = fluxMean 

diaObject["{}PSFluxMeanErr".format(filterName)] = fluxMeanErr 

diaObject["{}PSFluxNdata".format(filterName)] = nFluxData 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

def fail(self, diaObject, columns, error=None): 

"""Set diaObject position values to nan. 

 

Since we set an explicit value instead of nan for all, we override 

the fail method. 

""" 

for colName in columns: 

if colName.endswith("Ndata"): 

diaObject[colName] = 0 

else: 

diaObject[colName] = np.nan 

 

 

class PercentileDiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

percentiles = pexConfig.ListField( 

dtype=int, 

default=[5, 25, 50, 75, 95], 

doc="Percentiles to calculate to compute values for. Should be " 

"integer values." 

) 

 

 

@register("ap_percentileFlux") 

class PercentileDiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute percentiles of diaSource fluxes. 

""" 

 

ConfigClass = PercentileDiaPsFluxConfig 

# Output columns are created upon instantiation of the class. 

outputCols = [] 

 

def __init__(self, config, name, metadata, **kwargs): 

DiaObjectCalculationPlugin.__init__(self, 

config, 

name, 

metadata, 

**kwargs) 

self.outputCols = ["PSFluxPercentile{:02d}".format(percent) 

for percent in self.config.percentiles] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the percentile fluxes of the point source flux. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 0: 

pTiles = np.nanpercentile(filterDiaFluxes["psFlux"], 

self.config.percentiles) 

for pTile, tilePercent in zip(pTiles, self.config.percentiles): 

diaObject[ 

"{}PSFluxPercentile{:02d}".format(filterName, 

tilePercent)] = pTile 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

 

class SigmaDiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_sigmaFlux") 

class SigmaDiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute scatter of diaSource fluxes. 

""" 

 

ConfigClass = SigmaDiaPsFluxConfig 

# Output columns are created upon instantiation of the class. 

outputCols = ["PSFluxSigma"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the sigma fluxes of the point source flux. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 1: 

diaObject["{}PSFluxSigma".format(filterName)] = np.nanstd( 

filterDiaFluxes["psFlux"]) 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

 

class Chi2DiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_chi2Flux") 

class Chi2DiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute chi2 of diaSource fluxes. 

""" 

 

ConfigClass = Chi2DiaPsFluxConfig 

 

# Required input Cols 

inputCols = ["PSFluxMean"] 

# Output columns are created upon instantiation of the class. 

outputCols = ["PSFluxChi2"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.FLUX_MOMENTS_CALCULATED 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the chi2 of the point source fluxes. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 0: 

delta = (filterDiaFluxes["psFlux"] - 

diaObject["{}PSFluxMean".format(filterName)]) 

diaObject["{}PSFluxChi2".format(filterName)] = np.nansum( 

(delta / filterDiaFluxes["psFluxErr"]) ** 2) 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

 

class MadDiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_madFlux") 

class MadDiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute median absolute deviation of diaSource fluxes. 

""" 

 

ConfigClass = MadDiaPsFluxConfig 

 

# Required input Cols 

# Output columns are created upon instantiation of the class. 

outputCols = ["PSFluxMAD"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the median absolute deviation of the point source fluxes. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 0: 

diaObject["{}PSFluxMAD".format(filterName)] = ( 

median_absolute_deviation(filterDiaFluxes["psFlux"], 

ignore_nan=True) 

) 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

 

class SkewDiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_skewFlux") 

class SkewDiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute the skew of diaSource fluxes. 

""" 

 

ConfigClass = SkewDiaPsFluxConfig 

 

# Required input Cols 

# Output columns are created upon instantiation of the class. 

outputCols = ["PSFluxSkew"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the skew of the point source fluxes. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 0: 

fluxes = filterDiaFluxes["psFlux"] 

diaObject["{}PSFluxSkew".format(filterName)] = ( 

skew(fluxes[~np.isnan(fluxes)]) 

) 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

 

class MinMaxDiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_minMaxFlux") 

class MinMaxDiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute min/max of diaSource fluxes. 

""" 

 

ConfigClass = MinMaxDiaPsFluxConfig 

 

# Required input Cols 

# Output columns are created upon instantiation of the class. 

outputCols = ["PSFluxMin", "PSFluxMax"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute min/max of the point source fluxes. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 0: 

fluxes = filterDiaFluxes["psFlux"] 

diaObject["{}PSFluxMin".format(filterName)] = np.min(fluxes) 

diaObject["{}PSFluxMax".format(filterName)] = np.max(fluxes) 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

 

class MaxSlopeDiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_maxSlopeFlux") 

class MaxSlopeDiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute the maximum ratio time ordered deltaFlux / deltaTime. 

""" 

 

ConfigClass = MinMaxDiaPsFluxConfig 

 

# Required input Cols 

# Output columns are created upon instantiation of the class. 

outputCols = ["PSFluxMaxSlope"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the maximum ratio time ordered deltaFlux / deltaTime. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 1: 

tmpDiaSources = filterDiaFluxes[~np.isnan(filterDiaFluxes["psFlux"])] 

fluxes = tmpDiaSources["psFlux"].to_numpy() 

times = tmpDiaSources["midPointTai"].to_numpy() 

diaObject["{}PSFluxMaxSlope".format(filterName)] = np.max( 

(fluxes[1:] - fluxes[:-1]) / (times[1:] - times[:-1])) 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

 

class ErrMeanDiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_meanErrFlux") 

class ErrMeanDiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute the mean of the dia source errors. 

""" 

 

ConfigClass = ErrMeanDiaPsFluxConfig 

 

# Required input Cols 

# Output columns are created upon instantiation of the class. 

outputCols = ["PSFluxErrMean"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the mean of the dia source errors. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 0: 

diaObject["{}PSFluxErrMean".format(filterName)] = np.nanmean( 

filterDiaFluxes["psFluxErr"]) 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

 

class LinearFitDiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_linearFit") 

class LinearFitDiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute fit a linear model to flux vs time. 

""" 

 

ConfigClass = LinearFitDiaPsFluxConfig 

 

# Required input Cols 

# Output columns are created upon instantiation of the class. 

outputCols = ["PSFluxLinearSlope", "PSFluxLinearIntercept"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute fit a linear model to flux vs time. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 1: 

tmpDiaSources = filterDiaFluxes[ 

~np.logical_or(np.isnan(filterDiaFluxes["psFlux"]), 

np.isnan(filterDiaFluxes["psFluxErr"]))] 

fluxes = tmpDiaSources["psFlux"].to_numpy() 

errors = tmpDiaSources["psFluxErr"].to_numpy() 

times = tmpDiaSources["midPointTai"].to_numpy() 

A = np.array([times / errors, 1 / errors]).transpose() 

m, b = lsq_linear(A, fluxes / errors).x 

diaObject["{}PSFluxLinearSlope".format(filterName)] = m 

diaObject["{}PSFluxLinearIntercept".format(filterName)] = b 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

 

class StetsonJDiaPsFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_stetsonJ") 

class StetsonJDiaPsFlux(DiaObjectCalculationPlugin): 

"""Compute the StetsonJ statistic on the DIA point source fluxes. 

""" 

 

ConfigClass = LinearFitDiaPsFluxConfig 

 

# Required input Cols 

inputCols = ["PSFluxMean"] 

# Output columns are created upon instantiation of the class. 

outputCols = ["PSFluxStetsonJ"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.FLUX_MOMENTS_CALCULATED 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the StetsonJ statistic on the DIA point source fluxes. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 1: 

tmpDiaSources = filterDiaFluxes[ 

~np.logical_or(np.isnan(filterDiaFluxes["psFlux"]), 

np.isnan(filterDiaFluxes["psFluxErr"]))] 

fluxes = tmpDiaSources["psFlux"].to_numpy() 

errors = tmpDiaSources["psFluxErr"].to_numpy() 

 

diaObject["{}PSFluxStetsonJ".format(filterName)] = self._stetson_J( 

fluxes, 

errors, 

diaObject["{}PSFluxMean".format(filterName)]) 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

def _stetson_J(self, fluxes, errors, mean=None): 

"""Compute the single band stetsonJ statistic. 

 

Parameters 

---------- 

fluxes : `numpy.ndarray` (N,) 

Calibrated lightcurve flux values. 

errors : `numpy.ndarray` (N,) 

Errors on the calibrated lightcurve fluxes. 

mean : `float` 

Starting mean from previous plugin. 

 

Returns 

------- 

stetsonJ : `float` 

stetsonJ statistic for the input fluxes and errors. 

 

References 

---------- 

.. [1] Stetson, P. B., "On the Automatic Determination of Light-Curve 

Parameters for Cepheid Variables", PASP, 108, 851S, 1996 

""" 

n_points = len(fluxes) 

flux_mean = self._stetson_mean(fluxes, errors, mean) 

delta_val = ( 

np.sqrt(n_points / (n_points - 1)) * (fluxes - flux_mean) / errors) 

p_k = delta_val ** 2 - 1 

 

return np.mean(np.sign(p_k) * np.sqrt(np.fabs(p_k))) 

 

def _stetson_mean(self, 

values, 

errors, 

mean=None, 

alpha=2., 

beta=2., 

n_iter=20, 

tol=1e-6): 

"""Compute the stetson mean of the fluxes which down-weights outliers. 

 

Weighted biased on an error weighted difference scaled by a constant 

(1/``a``) and raised to the power beta. Higher betas more harshly 

penalize outliers and ``a`` sets the number of sigma where a weighted 

difference of 1 occurs. 

 

Parameters 

---------- 

values : `numpy.dnarray`, (N,) 

Input values to compute the mean of. 

errors : `numpy.ndarray`, (N,) 

Errors on the input values. 

mean : `float` 

Starting mean value or None. 

alpha : `float` 

Scalar down-weighting of the fractional difference. lower->more 

clipping. (Default value is 2.) 

beta : `float` 

Power law slope of the used to down-weight outliers. higher->more 

clipping. (Default value is 2.) 

n_iter : `int` 

Number of iterations of clipping. 

tol : `float` 

Fractional and absolute tolerance goal on the change in the mean 

before exiting early. (Default value is 1e-6) 

 

Returns 

------- 

mean : `float` 

Weighted stetson mean result. 

 

References 

---------- 

.. [1] Stetson, P. B., "On the Automatic Determination of Light-Curve 

Parameters for Cepheid Variables", PASP, 108, 851S, 1996 

""" 

n_points = len(values) 

n_factor = np.sqrt(n_points / (n_points - 1)) 

inv_var = 1 / errors ** 2 

 

if mean is None: 

mean = np.average(values, weights=inv_var) 

for iter_idx in range(n_iter): 

chi = np.fabs(n_factor * (values - mean) / errors) 

tmp_mean = np.average( 

values, 

weights=inv_var / (1 + (chi / alpha) ** beta)) 

diff = np.fabs(tmp_mean - mean) 

mean = tmp_mean 

if diff / mean < tol and diff < tol: 

break 

return mean 

 

 

class WeightedMeanDiaTotFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_meanTotFlux") 

class WeightedMeanDiaTotFlux(DiaObjectCalculationPlugin): 

"""Compute the weighted mean and mean error on the point source fluxes 

forced photometered at the DiaSource location in the calibrated image. 

 

Additionally store number of usable data points. 

""" 

 

ConfigClass = WeightedMeanDiaPsFluxConfig 

outputCols = ["TOTFluxMean", "TOTFluxMeanErr"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the weighted mean and mean error of the point source flux. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 0: 

tot_weight = np.nansum(1 / filterDiaFluxes["totFluxErr"] ** 2) 

fluxMean = np.nansum(filterDiaFluxes["totFlux"] / 

filterDiaFluxes["totFluxErr"] ** 2) 

fluxMean /= tot_weight 

fluxMeanErr = np.sqrt(1 / tot_weight) 

else: 

fluxMean = np.nan 

fluxMeanErr = np.nan 

if np.isfinite(fluxMean) and np.isfinite(fluxMeanErr): 

diaObject["{}TOTFluxMean".format(filterName)] = fluxMean 

diaObject["{}TOTFluxMeanErr".format(filterName)] = fluxMeanErr 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols]) 

 

 

class SigmaDiaTotFluxConfig(DiaObjectCalculationPluginConfig): 

pass 

 

 

@register("ap_sigmaTotFlux") 

class SigmaDiaTotFlux(DiaObjectCalculationPlugin): 

"""Compute scatter of diaSource fluxes. 

""" 

 

ConfigClass = SigmaDiaPsFluxConfig 

# Output columns are created upon instantiation of the class. 

outputCols = ["TOTFluxSigma"] 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.DEFAULT_CATALOGCALCULATION 

 

def calculate(self, 

diaObject, 

diaSources, 

filterDiaFluxes, 

filterName, 

**kwargs): 

"""Compute the sigma fluxes of the point source flux measured on the 

calibrated image. 

 

Parameters 

---------- 

diaObject : `dict` 

Summary object to store values in. 

diaSources : `pandas.DataFrame` 

DataFrame representing all diaSources associated with this 

diaObject. 

filterDiaFluxes : `pandas.DataFrame` 

DataFrame representing diaSources associated with this 

diaObject that are observed in the band pass ``filterName``. 

filterName : `str` 

Simple, string name of the filter for the flux being calculated. 

""" 

if len(filterDiaFluxes) > 1: 

diaObject["{}TOTFluxSigma".format(filterName)] = np.nanstd( 

filterDiaFluxes["totFlux"]) 

else: 

self.fail(diaObject, 

["{}{}".format(filterName, colName) 

for colName in self.outputCols])