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

from __future__ import print_function 

from builtins import object 

import os 

import numpy as np 

import numpy.ma as ma 

import matplotlib.pyplot as plt 

from collections import OrderedDict 

 

import lsst.sims.maf.db as db 

import lsst.sims.maf.utils as utils 

from lsst.sims.maf.plots import PlotHandler 

import lsst.sims.maf.maps as maps 

from lsst.sims.maf.stackers import BaseDitherStacker 

from .metricBundle import MetricBundle, createEmptyMetricBundle 

import warnings 

 

__all__ = ['makeBundlesDictFromList', 'MetricBundleGroup'] 

 

 

def makeBundlesDictFromList(bundleList): 

"""Utility to convert a list of MetricBundles into a dictionary, keyed by the fileRoot names. 

 

Raises an exception if the fileroot duplicates another metricBundle. 

(Note this should alert to potential cases of filename duplication). 

 

Parameters 

---------- 

bundleList : List[MetricBundle] 

""" 

bDict = {} 

for b in bundleList: 

32 ↛ 33line 32 didn't jump to line 33, because the condition on line 32 was never true if b.fileRoot in bDict: 

raise NameError('More than one metricBundle is using the same fileroot, %s' % (b.fileRoot)) 

bDict[b.fileRoot] = b 

return bDict 

 

 

class MetricBundleGroup(object): 

"""The MetricBundleGroup exists to calculate the metric values for a group of 

MetricBundles. 

 

The MetricBundleGroup will query data from a single database table (for multiple 

constraints), use that data to calculate metric values for multiple slicers, 

and calculate summary statistics and generate plots for all metrics included in 

the dictionary passed to the MetricBundleGroup. 

 

We calculate the metric values here, rather than in the individual MetricBundles, 

because it is much more efficient to step through a slicer once (and calculate all 

the relevant metric values at each point) than it is to repeat this process multiple times. 

 

The MetricBundleGroup also determines how to efficiently group the MetricBundles 

to reduce the number of sql queries of the database, grabbing larger chunks of data at once. 

 

Parameters 

---------- 

bundleDict : dict[MetricBundle] 

Individual MetricBundles should be placed into a dictionary, and then passed to 

the MetricBundleGroup. The dictionary keys can then be used to identify MetricBundles 

if needed -- and to identify new MetricBundles which could be created if 'reduce' 

functions are run on a particular MetricBundle. 

A bundleDict can be conveniently created from a list of MetricBundles using 

makeBundlesDictFromList 

dbObj : Database 

The database object (typically an :class:`OpsimDatabase`) connected to the data to be used to 

calculate metrics. 

Advanced use: It is possible to set this to None, in which case data should be passed 

directly to the runCurrent method (and runAll should not be used). 

outDir : Optional[str] 

Directory to save the metric results. Default is the current directory. 

resultsDb : Optional[ResultsDb] 

A results database. If not specified, one will be created in the outDir. 

This database saves information about the metrics calculated, including their summary statistics. 

verbose : Optional[bool] 

Flag to turn on/off verbose feedback. 

saveEarly : Optional[bool] 

If True, metric values will be saved immediately after they are first calculated (to prevent 

data loss) as well as after summary statistics are calculated. 

If False, metric values will only be saved after summary statistics are calculated. 

dbTable : Optional[str] 

The name of the table in the dbObj to query for data. 

""" 

def __init__(self, bundleDict, dbObj, outDir='.', resultsDb=None, verbose=True, 

saveEarly=True, dbTable=None): 

"""Set up the MetricBundleGroup. 

""" 

# Print occasional messages to screen. 

self.verbose = verbose 

# Save metric results as soon as possible (in case of crash). 

self.saveEarly = saveEarly 

# Check for output directory, create it if needed. 

self.outDir = outDir 

92 ↛ 93line 92 didn't jump to line 93, because the condition on line 92 was never true if not os.path.isdir(self.outDir): 

os.makedirs(self.outDir) 

 

# Do some type checking on the MetricBundle dictionary. 

96 ↛ 97line 96 didn't jump to line 97, because the condition on line 96 was never true if not isinstance(bundleDict, dict): 

raise ValueError('bundleDict should be a dictionary containing MetricBundle objects.') 

for b in bundleDict.values(): 

99 ↛ 100line 99 didn't jump to line 100, because the condition on line 99 was never true if not isinstance(b, MetricBundle): 

raise ValueError('bundleDict should contain only MetricBundle objects.') 

# Identify the series of constraints. 

self.constraints = list(set([b.constraint for b in bundleDict.values()])) 

# Set the bundleDict (all bundles, with all constraints) 

self.bundleDict = bundleDict 

 

# Check the dbObj. 

if not isinstance(dbObj, db.Database): 

warnings.warn('Warning: dbObj should be an instantiated Database (or child) object.') 

self.dbObj = dbObj 

# Set the table we're going to be querying. 

self.dbTable = dbTable 

if self.dbTable is None and self.dbObj is not None: 

self.dbTable = self.dbObj.defaultTable 

 

# Check the resultsDb (optional). 

if resultsDb is not None: 

117 ↛ 118line 117 didn't jump to line 118, because the condition on line 117 was never true if not isinstance(resultsDb, db.ResultsDb): 

raise ValueError('resultsDb should be an ResultsDb object') 

self.resultsDb = resultsDb 

 

# Dict to keep track of what's been run: 

self.hasRun = {} 

for bk in bundleDict: 

self.hasRun[bk] = False 

 

def _checkCompatible(self, metricBundle1, metricBundle2): 

"""Check if two MetricBundles are "compatible". 

Compatible indicates that the sql constraints, the slicers, and the maps are the same, and 

that the stackers do not interfere with each other 

(i.e. are not trying to set the same column in different ways). 

Returns True if the MetricBundles are compatible, False if not. 

 

Parameters 

---------- 

metricBundle1 : MetricBundle 

metricBundle2 : MetricBundle 

 

Returns 

------- 

bool 

""" 

142 ↛ 143line 142 didn't jump to line 143, because the condition on line 142 was never true if metricBundle1.constraint != metricBundle2.constraint: 

return False 

144 ↛ 145line 144 didn't jump to line 145, because the condition on line 144 was never true if metricBundle1.slicer != metricBundle2.slicer: 

return False 

146 ↛ 147line 146 didn't jump to line 147, because the condition on line 146 was never true if metricBundle1.mapsList.sort() != metricBundle2.mapsList.sort(): 

return False 

148 ↛ 149line 148 didn't jump to line 149, because the loop on line 148 never started for stacker in metricBundle1.stackerList: 

for stacker2 in metricBundle2.stackerList: 

# If the stackers have different names, that's OK, and if they are identical, that's ok. 

if (stacker.__class__.__name__ == stacker2.__class__.__name__) & (stacker != stacker2): 

return False 

# But if we got this far, everything matches. 

return True 

 

def _findCompatibleLists(self): 

"""Find sets of compatible metricBundles from the currentBundleDict. 

""" 

# CompatibleLists stores a list of lists; 

# each (nested) list contains the bundleDict _keys_ of a compatible set of metricBundles. 

# 

compatibleLists = [] 

for k, b in self.currentBundleDict.items(): 

foundCompatible = False 

for compatibleList in compatibleLists: 

comparisonMetricBundleKey = compatibleList[0] 

compatible = self._checkCompatible(self.bundleDict[comparisonMetricBundleKey], b) 

168 ↛ 165line 168 didn't jump to line 165, because the condition on line 168 was never false if compatible: 

# Must compare all metricBundles in each subset (if they are a potential match), 

# as the stackers could be different (and one could be incompatible, 

# not necessarily the first) 

172 ↛ 173line 172 didn't jump to line 173, because the loop on line 172 never started for comparisonMetricBundleKey in compatibleList[1:]: 

compatible = self._checkCompatible(self.bundleDict[comparisonMetricBundleKey], b) 

if not compatible: 

# If we find one which is not compatible, stop and go on to the 

# next subset list. 

break 

# Otherwise, we reached the end of the subset and they were all compatible. 

foundCompatible = True 

compatibleList.append(k) 

if not foundCompatible: 

# Didn't find a pre-existing compatible set; make a new one. 

compatibleLists.append([k, ]) 

self.compatibleLists = compatibleLists 

 

def runAll(self, clearMemory=False, plotNow=False, plotKwargs=None): 

"""Runs all the metricBundles in the metricBundleGroup, over all constraints. 

 

Calculates metric values, then runs reduce functions and summary statistics for 

all MetricBundles. 

 

Parameters 

---------- 

clearMemory : Optional[bool] 

If True, deletes metric values from memory after running each constraint group. 

plotNow : Optional[bool] 

If True, plots the metric values immediately after calculation. 

plotKwargs : Optional[kwargs] 

kwargs to pass to plotCurrent. 

""" 

for constraint in self.constraints: 

# Set the 'currentBundleDict' which is a dictionary of the metricBundles which match this 

# constraint. 

self.setCurrent(constraint) 

self.runCurrent(constraint, clearMemory=clearMemory, 

plotNow=plotNow, plotKwargs=plotKwargs) 

 

def setCurrent(self, constraint): 

"""Utility to set the currentBundleDict (i.e. a set of metricBundles with the same SQL constraint). 

 

Parameters 

---------- 

constraint : str 

The subset of MetricBundles with metricBundle.constraint == constraint will be 

included in a subset identified as the currentBundleDict. 

These are the active metrics to be calculated and plotted, etc. 

""" 

218 ↛ 219line 218 didn't jump to line 219, because the condition on line 218 was never true if constraint is None: 

constraint = '' 

self.currentBundleDict = {} 

for k, b in self.bundleDict.items(): 

222 ↛ 221line 222 didn't jump to line 221, because the condition on line 222 was never false if b.constraint == constraint: 

self.currentBundleDict[k] = b 

 

def runCurrent(self, constraint, simData=None, clearMemory=False, plotNow=False, plotKwargs=None): 

"""Run all the metricBundles which match this constraint in the metricBundleGroup. 

 

Calculates the metric values, then runs reduce functions and summary statistics for 

metrics in the current set only (see self.setCurrent). 

 

Parameters 

---------- 

constraint : str 

constraint to use to set the currently active metrics 

simData : Optional[numpy.ndarray] 

If simData is not None, then this numpy structured array is used instead of querying 

data from the dbObj. 

clearMemory : Optional[bool] 

If True, metric values are deleted from memory after they are calculated (and saved to disk). 

plotNow : Optional[bool] 

Plot immediately after calculating metric values (instead of the usual procedure, which 

is to plot after metric values are calculated for all constraints). 

plotKwargs : Optional[kwargs] 

Plotting kwargs to pass to plotCurrent. 

""" 

# Build list of all the columns needed from the database. 

self.dbCols = [] 

for b in self.currentBundleDict.values(): 

self.dbCols.extend(b.dbCols) 

self.dbCols = list(set(self.dbCols)) 

 

# Can pass simData directly (if had other method for getting data) 

if simData is not None: 

self.simData = simData 

 

else: 

self.simData = None 

# Query for the data. 

try: 

self.getData(constraint) 

except UserWarning: 

warnings.warn('No data matching constraint %s' % constraint) 

metricsSkipped = [] 

for b in self.currentBundleDict.values(): 

metricsSkipped.append("%s : %s : %s" % (b.metric.name, b.metadata, b.slicer.slicerName)) 

warnings.warn(' This means skipping metrics %s' % metricsSkipped) 

return 

except ValueError: 

warnings.warn('One or more of the columns requested from the database was not available.' + 

' Skipping constraint %s' % constraint) 

metricsSkipped = [] 

for b in self.currentBundleDict.values(): 

metricsSkipped.append("%s : %s : %s" % (b.metric.name, b.metadata, b.slicer.slicerName)) 

warnings.warn(' This means skipping metrics %s' % metricsSkipped) 

return 

 

# Find compatible subsets of the MetricBundle dictionary, 

# which can be run/metrics calculated/ together. 

self._findCompatibleLists() 

 

for compatibleList in self.compatibleLists: 

282 ↛ 284line 282 didn't jump to line 284, because the condition on line 282 was never false if self.verbose: 

print('Running: ', compatibleList) 

self._runCompatible(compatibleList) 

285 ↛ 287line 285 didn't jump to line 287, because the condition on line 285 was never false if self.verbose: 

print('Completed metric generation.') 

for key in compatibleList: 

self.hasRun[key] = True 

# Run the reduce methods. 

290 ↛ 292line 290 didn't jump to line 292, because the condition on line 290 was never false if self.verbose: 

print('Running reduce methods.') 

self.reduceCurrent() 

# Run the summary statistics. 

294 ↛ 296line 294 didn't jump to line 296, because the condition on line 294 was never false if self.verbose: 

print('Running summary statistics.') 

self.summaryCurrent() 

297 ↛ 299line 297 didn't jump to line 299, because the condition on line 297 was never false if self.verbose: 

print('Completed.') 

299 ↛ 300line 299 didn't jump to line 300, because the condition on line 299 was never true if plotNow: 

if plotKwargs is None: 

self.plotCurrent() 

else: 

self.plotCurrent(**plotKwargs) 

# Optionally: clear results from memory. 

305 ↛ 306line 305 didn't jump to line 306, because the condition on line 305 was never true if clearMemory: 

for b in self.currentBundleDict.values(): 

b.metricValues = None 

if self.verbose: 

print('Deleted metricValues from memory.') 

 

def getData(self, constraint): 

"""Query the data from the database. 

 

The currently bundleDict should generally be set before calling getData (using setCurrent). 

 

Parameters 

---------- 

constraint : str 

The constraint for the currently active set of MetricBundles. 

""" 

321 ↛ 329line 321 didn't jump to line 329, because the condition on line 321 was never false if self.verbose: 

322 ↛ 323line 322 didn't jump to line 323, because the condition on line 322 was never true if constraint == '': 

print("Querying database %s with no constraint for columns %s." % 

(self.dbTable, self.dbCols)) 

else: 

print("Querying database %s with constraint %s for columns %s" % 

(self.dbTable, constraint, self.dbCols)) 

# Note that we do NOT run the stackers at this point (this must be done in each 'compatible' group). 

self.simData = utils.getSimData(self.dbObj, constraint, self.dbCols, 

groupBy='default', tableName=self.dbTable) 

 

332 ↛ 336line 332 didn't jump to line 336, because the condition on line 332 was never false if self.verbose: 

print("Found %i visits" % (self.simData.size)) 

 

# Query for the fieldData if we need it for the opsimFieldSlicer. 

needFields = [b.slicer.needsFields for b in self.currentBundleDict.values()] 

337 ↛ 338line 337 didn't jump to line 338, because the condition on line 337 was never true if True in needFields: 

self.fieldData = utils.getFieldData(self.dbObj, constraint) 

else: 

self.fieldData = None 

 

def _runCompatible(self, compatibleList): 

"""Runs a set of 'compatible' metricbundles in the MetricBundleGroup dictionary, 

identified by 'compatibleList' keys. 

 

A compatible list of MetricBundles is a subset of the currentBundleDict. 

The currentBundleDict == set of MetricBundles with the same constraint. 

The compatibleBundles == set of MetricBundles with the same constraint, the same 

slicer, the same maps applied to the slicer, and stackers which do not clobber each other's data. 

 

This is where the work of calculating the metric values is done. 

""" 

 

354 ↛ 355line 354 didn't jump to line 355, because the condition on line 354 was never true if len(self.simData) == 0: 

return 

 

# Grab a dictionary representation of this subset of the dictionary, for easier iteration. 

bDict = {key: self.currentBundleDict.get(key) for key in compatibleList} 

 

# Find the unique stackers and maps. These are already "compatible" (as id'd by compatibleList). 

uniqStackers = [] 

allStackers = [] 

uniqMaps = [] 

allMaps = [] 

for b in bDict.values(): 

allStackers += b.stackerList 

allMaps += b.mapsList 

for s in allStackers: 

369 ↛ 368line 369 didn't jump to line 368, because the condition on line 369 was never false if s not in uniqStackers: 

uniqStackers.append(s) 

for m in allMaps: 

372 ↛ 371line 372 didn't jump to line 371, because the condition on line 372 was never false if m not in uniqMaps: 

uniqMaps.append(m) 

 

# Run stackers. 

# Run dither stackers first. (this is a bit of a hack -- we should probably figure out 

# proper hierarchy and DAG so that stackers run in the order they need to. This will catch 90%). 

ditherStackers = [] 

for s in uniqStackers: 

if isinstance(s, BaseDitherStacker): 

ditherStackers.append(s) 

for stacker in ditherStackers: 

self.simData = stacker.run(self.simData, override=True) 

uniqStackers.remove(stacker) 

for stacker in uniqStackers: 

# Note that stackers will clobber previously existing rows with the same name. 

self.simData = stacker.run(self.simData, override=True) 

 

# Pull out one of the slicers to use as our 'slicer'. 

# This will be forced back into all of the metricBundles at the end (so that they track 

# the same metadata such as the slicePoints, in case the same actual object wasn't used). 

slicer = list(bDict.values())[0].slicer 

if (slicer.slicerName == 'OpsimFieldSlicer'): 

slicer.setupSlicer(self.simData, self.fieldData, maps=uniqMaps) 

else: 

slicer.setupSlicer(self.simData, maps=uniqMaps) 

# Copy the slicer (after setup) back into the individual metricBundles. 

398 ↛ 403line 398 didn't jump to line 403, because the condition on line 398 was never false if slicer.slicerName != 'HealpixSlicer' or slicer.slicerName != 'UniSlicer': 

for b in bDict.values(): 

b.slicer = slicer 

 

# Set up (masked) arrays to store metric data in each metricBundle. 

for b in bDict.values(): 

b._setupMetricValues() 

 

# Set up an ordered dictionary to be the cache if needed: 

# (Currently using OrderedDict, it might be faster to use 2 regular Dicts instead) 

if slicer.cacheSize > 0: 

cacheDict = OrderedDict() 

cache = True 

else: 

cache = False 

# Run through all slicepoints and calculate metrics. 

for i, slice_i in enumerate(slicer): 

slicedata = self.simData[slice_i['idxs']] 

if len(slicedata) == 0: 

# No data at this slicepoint. Mask data values. 

for b in bDict.values(): 

b.metricValues.mask[i] = True 

else: 

# There is data! Should we use our data cache? 

if cache: 

# Make the data idxs hashable. 

cacheKey = frozenset(slice_i['idxs']) 

# If key exists, set flag to use it, otherwise add it 

426 ↛ 427line 426 didn't jump to line 427, because the condition on line 426 was never true if cacheKey in cacheDict: 

useCache = True 

cacheVal = cacheDict[cacheKey] 

# Move this value to the end of the OrderedDict 

del cacheDict[cacheKey] 

cacheDict[cacheKey] = cacheVal 

else: 

cacheDict[cacheKey] = i 

useCache = False 

for b in bDict.values(): 

436 ↛ 437line 436 didn't jump to line 437, because the condition on line 436 was never true if useCache: 

b.metricValues.data[i] = b.metricValues.data[cacheDict[cacheKey]] 

else: 

b.metricValues.data[i] = b.metric.run(slicedata, slicePoint=slice_i['slicePoint']) 

# If we are above the cache size, drop the oldest element from the cache dict. 

441 ↛ 442line 441 didn't jump to line 442, because the condition on line 441 was never true if len(cacheDict) > slicer.cacheSize: 

del cacheDict[list(cacheDict.keys())[0]] 

 

# Not using memoize, just calculate things normally 

else: 

for b in bDict.values(): 

b.metricValues.data[i] = b.metric.run(slicedata, slicePoint=slice_i['slicePoint']) 

# Mask data where metrics could not be computed (according to metric bad value). 

for b in bDict.values(): 

450 ↛ 451line 450 didn't jump to line 451, because the condition on line 450 was never true if b.metricValues.dtype.name == 'object': 

for ind, val in enumerate(b.metricValues.data): 

if val is b.metric.badval: 

b.metricValues.mask[ind] = True 

else: 

# For some reason, this doesn't work for dtype=object arrays. 

b.metricValues.mask = np.where(b.metricValues.data == b.metric.badval, 

True, b.metricValues.mask) 

 

# Save data to disk as we go, although this won't keep summary values, etc. (just failsafe). 

if self.saveEarly: 

for b in bDict.values(): 

b.write(outDir=self.outDir, resultsDb=self.resultsDb) 

 

def reduceAll(self, updateSummaries=True): 

"""Run the reduce methods for all metrics in bundleDict. 

 

Running this method, for all MetricBundles at once, assumes that clearMemory was False. 

 

Parameters 

---------- 

updateSummaries : Optional[bool] 

If True, summary metrics are removed from the top-level (non-reduced) 

MetricBundle. Usually this should be True, as summary metrics are generally 

intended to run on the simpler data produced by reduce metrics. 

""" 

for constraint in self.constraints: 

self.setCurrent(constraint) 

self.reduceCurrent(updateSummaries=updateSummaries) 

 

def reduceCurrent(self, updateSummaries=True): 

"""Run all reduce functions for the metricbundle in the currently active set of MetricBundles. 

 

Parameters 

---------- 

updateSummaries : Optional[bool] 

If True, summary metrics are removed from the top-level (non-reduced) 

MetricBundle. Usually this should be True, as summary metrics are generally 

intended to run on the simpler data produced by reduce metrics. 

""" 

# Create a temporary dictionary to hold the reduced metricbundles. 

reduceBundleDict = {} 

for b in self.currentBundleDict.values(): 

# If there are no reduce functions associated with the metric, skip this metricBundle. 

494 ↛ 496line 494 didn't jump to line 496, because the condition on line 494 was never true if len(b.metric.reduceFuncs) > 0: 

# Apply reduce functions, creating a new metricBundle in the process (new metric values). 

for reduceFunc in b.metric.reduceFuncs.values(): 

newmetricbundle = b.reduceMetric(reduceFunc) 

# Add the new metricBundle to our metricBundleGroup dictionary. 

name = newmetricbundle.metric.name 

if name in self.bundleDict: 

name = newmetricbundle.fileRoot 

reduceBundleDict[name] = newmetricbundle 

if self.saveEarly: 

newmetricbundle.write(outDir=self.outDir, resultsDb=self.resultsDb) 

# Remove summaryMetrics from top level metricbundle if desired. 

if updateSummaries: 

b.summaryMetrics = [] 

# Add the new metricBundles to the MetricBundleGroup dictionary. 

self.bundleDict.update(reduceBundleDict) 

# And add to to the currentBundleDict too, so we run as part of 'summaryCurrent'. 

self.currentBundleDict.update(reduceBundleDict) 

 

def summaryAll(self): 

"""Run the summary statistics for all metrics in bundleDict. 

 

Calculating all summary statistics, for all MetricBundles, at this 

point assumes that clearMemory was False. 

""" 

for constraint in self.constraints: 

self.setCurrent(constraint) 

self.summaryCurrent() 

 

def summaryCurrent(self): 

"""Run summary statistics on all the metricBundles in the currently active set of MetricBundles. 

""" 

for b in self.currentBundleDict.values(): 

b.computeSummaryStats(self.resultsDb) 

 

def plotAll(self, savefig=True, outfileSuffix=None, figformat='pdf', dpi=600, thumbnail=True, 

closefigs=True): 

"""Generate all the plots for all the metricBundles in bundleDict. 

 

Generating all ploots, for all MetricBundles, at this point, assumes that 

clearMemory was False. 

 

Parameters 

---------- 

savefig : Optional[bool] 

If True, save figures to disk, to self.outDir directory. 

outfileSuffix : Optional[str] 

Append outfileSuffix to the end of every plot file generated. Useful for generating 

sequential series of images for movies. 

figformat : Optional[str] 

Matplotlib figure format to use to save to disk. Default pdf. 

dpi : Optional[str] 

DPI for matplotlib figure. Default 600. 

thumbnail : Optional[bool] 

If True, save a small thumbnail jpg version of the output file to disk as well. 

This is useful for showMaf web pages. Default True. 

closefigs : Optional[bool] 

Close the matplotlib figures after they are saved to disk. If many figures are 

generated, closing the figures saves significant memory. Default True. 

""" 

for constraint in self.constraints: 

555 ↛ 558line 555 didn't jump to line 558, because the condition on line 555 was never false if self.verbose: 

print('Plotting figures with "%s" constraint now.' % (constraint)) 

 

self.setCurrent(constraint) 

self.plotCurrent(savefig=savefig, outfileSuffix=outfileSuffix, figformat=figformat, dpi=dpi, 

thumbnail=thumbnail, closefigs=closefigs) 

 

def plotCurrent(self, savefig=True, outfileSuffix=None, figformat='pdf', dpi=600, thumbnail=True, 

closefigs=True): 

"""Generate the plots for the currently active set of MetricBundles. 

 

Parameters 

---------- 

savefig : Optional[bool] 

If True, save figures to disk, to self.outDir directory. 

outfileSuffix : Optional[str] 

Append outfileSuffix to the end of every plot file generated. Useful for generating 

sequential series of images for movies. 

figformat : Optional[str] 

Matplotlib figure format to use to save to disk. Default pdf. 

dpi : Optional[str] 

DPI for matplotlib figure. Default 600. 

thumbnail : Optional[bool] 

If True, save a small thumbnail jpg version of the output file to disk as well. 

This is useful for showMaf web pages. Default True. 

closefigs : Optional[bool] 

Close the matplotlib figures after they are saved to disk. If many figures are 

generated, closing the figures saves significant memory. Default True. 

""" 

plotHandler = PlotHandler(outDir=self.outDir, resultsDb=self.resultsDb, 

savefig=savefig, figformat=figformat, dpi=dpi, thumbnail=thumbnail) 

 

for b in self.currentBundleDict.values(): 

try: 

b.plot(plotHandler=plotHandler, outfileSuffix=outfileSuffix, savefig=savefig) 

except ValueError as ve: 

message = 'Plotting failed for metricBundle %s.' % (b.fileRoot) 

message += ' Error message: %s' % (ve) 

warnings.warn(message) 

594 ↛ 587line 594 didn't jump to line 587, because the condition on line 594 was never false if closefigs: 

plt.close('all') 

596 ↛ exitline 596 didn't return from function 'plotCurrent', because the condition on line 596 was never false if self.verbose: 

print('Plotting complete.') 

 

def writeAll(self): 

"""Save all the MetricBundles to disk. 

 

Saving all MetricBundles to disk at this point assumes that clearMemory was False. 

""" 

for constraint in self.constraints: 

self.setCurrent(constraint) 

self.writeCurrent() 

 

def writeCurrent(self): 

"""Save all the MetricBundles in the currently active set to disk. 

""" 

611 ↛ 616line 611 didn't jump to line 616, because the condition on line 611 was never false if self.verbose: 

612 ↛ 615line 612 didn't jump to line 615, because the condition on line 612 was never false if self.saveEarly: 

print('Re-saving metric bundles.') 

else: 

print('Saving metric bundles.') 

for b in self.currentBundleDict.values(): 

b.write(outDir=self.outDir, resultsDb=self.resultsDb) 

 

def readAll(self): 

"""Attempt to read all MetricBundles from disk. 

 

You must set the metrics/slicer/constraint/runName for a metricBundle appropriately; 

then this method will search for files in the location self.outDir/metricBundle.fileRoot. 

Reads all the files associated with all metricbundles in self.bundleDict. 

""" 

reduceBundleDict = {} 

removeBundles = [] 

for b in self.bundleDict: 

bundle = self.bundleDict[b] 

filename = os.path.join(self.outDir, bundle.fileRoot + '.npz') 

try: 

# Create a temporary metricBundle to read the data into. 

# (we don't use b directly, as this overrides plotDict/etc). 

tmpBundle = createEmptyMetricBundle() 

tmpBundle.read(filename) 

# Copy the tmpBundle metricValues into bundle. 

bundle.metricValues = tmpBundle.metricValues 

# And copy the slicer into b, to get slicePoints. 

bundle.slicer = tmpBundle.slicer 

if self.verbose: 

print('Read %s from disk.' % (bundle.fileRoot)) 

except IOError: 

warnings.warn('Warning: file %s not found, bundle not restored.' % filename) 

removeBundles.append(b) 

 

# Look to see if this is a complex metric, with associated 'reduce' functions, 

# and read those in too. 

if len(bundle.metric.reduceFuncs) > 0: 

origMetricName = bundle.metric.name 

for reduceFunc in bundle.metric.reduceFuncs.values(): 

reduceName = origMetricName + '_' + reduceFunc.__name__.replace('reduce', '') 

# Borrow the fileRoot in b (we'll reset it appropriately afterwards). 

bundle.metric.name = reduceName 

bundle._buildFileRoot() 

filename = os.path.join(self.outDir, bundle.fileRoot + '.npz') 

tmpBundle = createEmptyMetricBundle() 

try: 

tmpBundle.read(filename) 

# This won't necessarily recreate the plotDict and displayDict exactly 

# as they would have been made if you calculated the reduce metric from scratch. 

# Perhaps update these metric reduce dictionaries after reading them in? 

newmetricBundle = MetricBundle(metric=bundle.metric, slicer=bundle.slicer, 

constraint=bundle.constraint, 

stackerList=bundle.stackerList, runName=bundle.runName, 

metadata=bundle.metadata, 

plotDict=bundle.plotDict, 

displayDict=bundle.displayDict, 

summaryMetrics=bundle.summaryMetrics, 

mapsList=bundle.mapsList, 

fileRoot=bundle.fileRoot, plotFuncs=bundle.plotFuncs) 

newmetricBundle.metric.name = reduceName 

newmetricBundle.metricValues = ma.copy(tmpBundle.metricValues) 

# Add the new metricBundle to our metricBundleGroup dictionary. 

name = newmetricBundle.metric.name 

if name in self.bundleDict: 

name = newmetricBundle.fileRoot 

reduceBundleDict[name] = newmetricBundle 

if self.verbose: 

print('Read %s from disk.' % (newmetricBundle.fileRoot)) 

except IOError: 

warnings.warn('Warning: file %s not found, bundle not restored ("reduce" metric).' 

% filename) 

 

# Remove summaryMetrics from top level metricbundle. 

bundle.summaryMetrics = [] 

# Update parent MetricBundle name. 

bundle.metric.name = origMetricName 

bundle._buildFileRoot() 

 

# Add the reduce bundles into the bundleDict. 

self.bundleDict.update(reduceBundleDict) 

# And remove the bundles which were not found on disk, so we don't try to make (blank) plots. 

for b in removeBundles: 

del self.bundleDict[b]