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

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

import logging 

import math 

from collections import defaultdict 

 

import numpy as num 

 

from matplotlib import pyplot as plt 

from matplotlib import cm, patches 

 

from pyrocko import plot, gf, trace 

from pyrocko.plot import mpl_init, mpl_color 

from pyrocko.guts import Tuple, Float, Int, String 

 

from grond import core, meta 

from .target import WaveformMisfitResult, WaveformMisfitTarget 

from ..plot import StationDistributionPlot 

 

from grond.plot.config import PlotConfig 

from grond.plot.common import light 

from grond.plot.collection import PlotItem 

 

guts_prefix = 'grond' 

km = 1e3 

d2r = math.pi / 180. 

 

logger = logging.getLogger('targets.waveform.plot') 

 

 

def make_norm_trace(a, b, exponent): 

tmin = max(a.tmin, b.tmin) 

tmax = min(a.tmax, b.tmax) 

c = a.chop(tmin, tmax, inplace=False) 

bc = b.chop(tmin, tmax, inplace=False) 

c.set_ydata(num.abs(c.get_ydata() - bc.get_ydata())**exponent) 

return c 

 

 

def amp_spec_max(spec_trs, key): 

amaxs = {} 

for spec_tr in spec_trs: 

amax = num.max(num.abs(spec_tr.ydata)) 

k = key(spec_tr) 

if k not in amaxs: 

amaxs[k] = amax 

else: 

amaxs[k] = max(amaxs[k], amax) 

 

return amaxs 

 

 

def plot_trace(axes, tr, **kwargs): 

return axes.plot(tr.get_xdata(), tr.get_ydata(), **kwargs) 

 

 

def plot_taper(axes, t, taper, **kwargs): 

y = num.ones(t.size) * 0.9 

taper(y, t[0], t[1] - t[0]) 

y2 = num.concatenate((y, -y[::-1])) 

t2 = num.concatenate((t, t[::-1])) 

axes.fill(t2, y2, **kwargs) 

 

 

def plot_dtrace(axes, tr, space, mi, ma, **kwargs): 

t = tr.get_xdata() 

y = tr.get_ydata() 

y2 = (num.concatenate((y, num.zeros(y.size))) - mi) / \ 

(ma - mi) * space - (1.0 + space) 

t2 = num.concatenate((t, t[::-1])) 

axes.fill( 

t2, y2, 

clip_on=False, 

**kwargs) 

 

 

def plot_spectrum( 

axes, spec_syn, spec_obs, fmin, fmax, space, mi, ma, 

syn_color='red', obs_color='black', 

syn_lw=1.5, obs_lw=1.0, color_vline='gray', fontsize=9.): 

 

fpad = (fmax - fmin) / 6. 

 

for spec, color, lw in [ 

(spec_syn, syn_color, syn_lw), 

(spec_obs, obs_color, obs_lw)]: 

 

f = spec.get_xdata() 

mask = num.logical_and(fmin - fpad <= f, f <= fmax + fpad) 

 

f = f[mask] 

y = num.abs(spec.get_ydata())[mask] 

 

y2 = (num.concatenate((y, num.zeros(y.size))) - mi) / \ 

(ma - mi) * space - (1.0 + space) 

f2 = num.concatenate((f, f[::-1])) 

axes2 = axes.twiny() 

axes2.set_axis_off() 

 

axes2.set_xlim(fmin - fpad * 5, fmax + fpad * 5) 

 

axes2.plot(f2, y2, clip_on=False, color=color, lw=lw) 

axes2.fill(f2, y2, alpha=0.1, clip_on=False, color=color) 

 

axes2.plot([fmin, fmin], [-1.0 - space, -1.0], color=color_vline) 

axes2.plot([fmax, fmax], [-1.0 - space, -1.0], color=color_vline) 

 

for (text, fx, ha) in [ 

('%.3g Hz' % fmin, fmin, 'right'), 

('%.3g Hz' % fmax, fmax, 'left')]: 

 

axes2.annotate( 

text, 

xy=(fx, -1.0), 

xycoords='data', 

xytext=( 

fontsize * 0.4 * [-1, 1][ha == 'left'], 

-fontsize * 0.2), 

textcoords='offset points', 

ha=ha, 

va='top', 

color=color_vline, 

fontsize=fontsize) 

 

 

def plot_dtrace_vline(axes, t, space, **kwargs): 

axes.plot([t, t], [-1.0 - space, -1.0], **kwargs) 

 

 

def layout(source, targets, nxmax, nymax): 

if nxmax == 1 and nymax == 1: 

nx = len(targets) 

ny = 1 

nxx = 1 

nyy = 1 

frame_to_target = {} 

for ix, target in enumerate(sorted(targets, key=lambda t: t.path)): 

frame_to_target[0, ix] = target 

 

return frame_to_target, nx, ny, nxx, nyy 

 

nframes = len(targets) 

 

nx = int(math.ceil(math.sqrt(nframes))) 

ny = (nframes - 1) // nx + 1 

 

nxx = (nx - 1) // nxmax + 1 

nyy = (ny - 1) // nymax + 1 

 

# nz = nxx * nyy 

 

xs = num.arange(nx) // ((max(2, nx) - 1.0) / 2.) 

ys = num.arange(ny) // ((max(2, ny) - 1.0) / 2.) 

 

xs -= num.mean(xs) 

ys -= num.mean(ys) 

 

fxs = num.tile(xs, ny) 

fys = num.repeat(ys, nx) 

 

data = [] 

 

for target in targets: 

azi = source.azibazi_to(target)[0] 

dist = source.distance_to(target) 

x = dist * num.sin(num.deg2rad(azi)) 

y = dist * num.cos(num.deg2rad(azi)) 

data.append((x, y, dist)) 

 

gxs, gys, dists = num.array(data, dtype=num.float).T 

 

iorder = num.argsort(dists) 

 

gxs = gxs[iorder] 

gys = gys[iorder] 

targets_sorted = [targets[ii] for ii in iorder] 

 

gxs -= num.mean(gxs) 

gys -= num.mean(gys) 

 

gmax = max(num.max(num.abs(gys)), num.max(num.abs(gxs))) 

if gmax == 0.: 

gmax = 1. 

 

gxs /= gmax 

gys /= gmax 

 

dists = num.sqrt( 

(fxs[num.newaxis, :] - gxs[:, num.newaxis])**2 + 

(fys[num.newaxis, :] - gys[:, num.newaxis])**2) 

 

distmax = num.max(dists) 

 

availmask = num.ones(dists.shape[1], dtype=num.bool) 

frame_to_target = {} 

for itarget, target in enumerate(targets_sorted): 

iframe = num.argmin( 

num.where(availmask, dists[itarget], distmax + 1.)) 

availmask[iframe] = False 

iy, ix = num.unravel_index(iframe, (ny, nx)) 

frame_to_target[iy, ix] = target 

 

return frame_to_target, nx, ny, nxx, nyy 

 

 

class CheckWaveformsPlot(PlotConfig): 

''' Plot for checking the waveforms fit with a number of synthetics ''' 

name = 'check_waveform' 

 

size_cm = Tuple.T( 

2, Float.T(), 

default=(9., 7.5), 

help='width and length of the figure in cm') 

n_random_synthetics = Int.T( 

default=10, 

help='Number of Synthetics to generate') 

 

def make(self, environ): 

cm = environ.get_plot_collection_manager() 

mpl_init(fontsize=self.font_size) 

 

environ.setup_modelling() 

 

problem = environ.get_problem() 

results_list = [] 

sources = [] 

if self.n_random_synthetics == 0: 

x = problem.preconstrain(problem.get_reference_model()) 

sources.append(problem.base_source) 

results = problem.evaluate(x) 

results_list.append(results) 

 

else: 

for _ in range(self.n_random_synthetics): 

x = problem.get_random_model() 

sources.append(problem.get_source(x)) 

results = problem.evaluate(x) 

results_list.append(results) 

 

cm.create_group_mpl(self, self.draw_figures( 

sources, problem.targets, results_list), 

title=u'Waveform Check', 

section='checks', 

feather_icon='activity', 

description=u''' 

Plot to judge waveform time window settings and source model parameter ranges. 

 

For each waveform target, observed and synthetic waveforms are shown. For the 

latter, models are randomly drawn from the configured parameter search space. 

 

The top panel shows the observed waveform; filtered (faint gray), and filtered 

and tapered (black). The colored outline around the observed trace shows the 

taper position for each drawn model in a different color. The middle panel 

shows the filtered synthetic waveforms of the drawn models and the bottom plot 

shows the corresponding filtered and tapered synthetic waveforms. The colors of 

taper and synthetic traces are consistent for each random model. The given time 

is relative to the reference event origin time. 

''') 

 

def draw_figures(self, sources, targets, results_list): 

results_list = list(zip(*results_list)) 

for itarget, target, results in zip( 

range(len(targets)), targets, results_list): 

 

if isinstance(target, WaveformMisfitTarget) and results: 

item = PlotItem(name='t%i' % itarget) 

item.attributes['targets'] = [target.string_id()] 

fig = self.draw_figure(sources, target, results) 

if fig is not None: 

yield item, fig 

 

def draw_figure(self, sources, target, results): 

t0_mean = num.mean([s.time for s in sources]) 

 

# distances = [ 

# s.distance_to(target) for s in sources] 

 

# distance_min = num.min(distances) 

# distance_max = num.max(distances) 

 

yabsmaxs = [] 

 

for result in results: 

if isinstance(result, WaveformMisfitResult): 

yabsmaxs.append( 

num.max(num.abs( 

result.filtered_obs.get_ydata()))) 

 

if yabsmaxs: 

yabsmax = max(yabsmaxs) or 1.0 

else: 

yabsmax = None 

 

fontsize = self.font_size 

 

fig = plt.figure(figsize=self.size_inch) 

 

labelpos = plot.mpl_margins( 

fig, nw=1, nh=1, w=1., h=5., 

units=fontsize) 

 

axes = fig.add_subplot(1, 1, 1) 

 

labelpos(axes, 2.5, 2.0) 

axes.set_frame_on(False) 

axes.set_ylim(1., 4.) 

axes.get_yaxis().set_visible(False) 

axes.set_title('%s' % target.string_id()) 

axes.set_xlabel('Time [s]') 

ii = 0 

 

for source, result in zip(sources, results): 

if not isinstance(result, WaveformMisfitResult): 

continue 

 

if result.tobs_shift != 0.0: 

t0 = result.tsyn_pick 

else: 

t0 = t0_mean 

 

t = result.filtered_obs.get_xdata() 

ydata = result.filtered_obs.get_ydata() / yabsmax 

axes.plot( 

t-t0, ydata*0.40 + 3.5, color='black', lw=1.0) 

 

color = plot.mpl_graph_color(ii) 

 

t = result.filtered_syn.get_xdata() 

ydata = result.filtered_syn.get_ydata() 

ydata = ydata / (num.max(num.abs(ydata)) or 1.0) 

 

axes.plot(t-t0, ydata*0.47 + 2.5, color=color, alpha=0.5, lw=1.0) 

 

t = result.processed_syn.get_xdata() 

ydata = result.processed_syn.get_ydata() 

ydata = ydata / (num.max(num.abs(ydata)) or 1.0) 

 

axes.plot(t-t0, ydata*0.47 + 1.5, color=color, alpha=0.5, lw=1.0) 

if result.tobs_shift != 0.0: 

axes.axvline( 

result.tsyn_pick - t0, 

color=(0.7, 0.7, 0.7), 

zorder=2) 

 

t = result.processed_syn.get_xdata() 

taper = result.taper 

 

y = num.ones(t.size) * 0.9 

taper(y, t[0], t[1] - t[0]) 

y2 = num.concatenate((y, -y[::-1])) 

t2 = num.concatenate((t, t[::-1])) 

axes.plot(t2-t0, y2 * 0.47 + 3.5, color=color, alpha=0.2, lw=1.0) 

ii += 1 

 

return fig 

 

 

class FitsWaveformEnsemblePlot(PlotConfig): 

''' Plot showing all waveform fits for the ensemble of solutions''' 

 

name = 'fits_waveform_ensemble' 

size_cm = Tuple.T( 

2, Float.T(), 

default=(9., 5.), 

help='width and length of the figure in cm') 

nx = Int.T( 

default=1, 

help='horizontal number of subplots on every page') 

ny = Int.T( 

default=1, 

help='vertical number of subplots on every page') 

misfit_cutoff = Float.T( 

optional=True, 

help='Plot fits for models up to this misfit value') 

color_parameter = String.T( 

default='misfit', 

help='Choice of value to color, options: dist and misfit') 

font_size = Float.T( 

default=8, 

help='Font Size of all fonts, except title') 

font_size_title = Float.T( 

default=10, 

help='Font size of title') 

 

def make(self, environ): 

cm = environ.get_plot_collection_manager() 

mpl_init(fontsize=self.font_size) 

environ.setup_modelling() 

ds = environ.get_dataset() 

history = environ.get_history(subset='harvest') 

optimiser = environ.get_optimiser() 

 

cm.create_group_mpl( 

self, 

self.draw_figures(ds, history, optimiser), 

title=u'Waveform fits for the ensemble', 

section='fits', 

feather_icon='activity', 

description=u''' 

Plot showing waveform (attribute) fits for the ensemble of solutions. 

 

Waveform fits for every nth model in the ensemble of bootstrap solutions. 

Depending on the target configuration different types of comparisons are 

possible: (i) time domain waveform differences, (ii) amplitude spectra, (iii) 

envelopes, (iv) cross correlation functions. Each waveform plot gives a number 

of details: 

 

1) Target information (left side, from top to bottom) gives station name with 

component, distance to source, azimuth of station with respect to source, 

target weight, target misfit and starting time of the waveform relative to the 

origin time. 

 

2) The background gray area shows the applied taper function. 

 

3) The waveforms shown are: the restituted and filtered observed trace without 

tapering (light grey) and the same trace with tapering and processing (dark 

gray), the synthetic trace (light red) and the filtered, tapered and (if 

enabled) shifted and processed synthetic trace (colored). The colors of the 

synthetic traces indicate how well the corresponding models fit in the global 

weighting scheme (when all bootstrap weights are equal), from better fit (red) 

to worse fit (blue). The amplitudes of the traces are scaled according to the 

target weight (small weight, small amplitude) and normed relative to the 

maximum amplitude of the targets of the corresponding normalisation family. 

 

4) The bottom panel shows, depending on the type of comparison, sample-wise 

residuals for time domain comparisons (red filled), spectra of observed and 

synthetic traces for amplitude spectrum comparisons, or cross correlation 

traces.''') 

 

def draw_figures(self, ds, history, optimiser): 

 

color_parameter = self.color_parameter 

misfit_cutoff = self.misfit_cutoff 

fontsize = self.font_size 

fontsize_title = self.font_size_title 

 

nxmax = self.nx 

nymax = self.ny 

 

problem = history.problem 

 

for target in problem.targets: 

target.set_dataset(ds) 

 

target_index = {} 

i = 0 

for target in problem.targets: 

target_index[target] = i, i+target.nmisfits 

i += target.nmisfits 

 

gms = history.get_sorted_primary_misfits()[::-1] 

models = history.get_sorted_primary_models()[::-1] 

 

if misfit_cutoff is not None: 

ibest = gms < misfit_cutoff 

gms = gms[ibest] 

models = models[ibest] 

 

gms = gms[::10] 

models = models[::10] 

 

nmodels = models.shape[0] 

if color_parameter == 'dist': 

mx = num.mean(models, axis=0) 

cov = num.cov(models.T) 

mdists = core.mahalanobis_distance(models, mx, cov) 

icolor = meta.ordersort(mdists) 

 

elif color_parameter == 'misfit': 

iorder = num.arange(nmodels) 

icolor = iorder 

 

elif color_parameter in problem.parameter_names: 

ind = problem.name_to_index(color_parameter) 

icolor = problem.extract(models, ind) 

 

target_to_results = defaultdict(list) 

all_syn_trs = [] 

 

dtraces = [] 

for imodel in range(nmodels): 

model = models[imodel, :] 

 

source = problem.get_source(model) 

results = problem.evaluate(model) 

 

dtraces.append([]) 

 

for target, result in zip(problem.targets, results): 

w = target.get_combined_weight() 

 

if isinstance(result, gf.SeismosizerError) or \ 

not isinstance(target, WaveformMisfitTarget) or \ 

not num.all(num.isfinite(w)): 

 

dtraces[-1].extend([None] * target.nmisfits) 

continue 

 

itarget, itarget_end = target_index[target] 

assert itarget_end == itarget + 1 

 

if target.misfit_config.domain == 'cc_max_norm': 

tref = ( 

result.filtered_obs.tmin + result.filtered_obs.tmax) \ 

* 0.5 

 

for tr_filt, tr_proc, tshift in ( 

(result.filtered_obs, 

result.processed_obs, 

0.), 

(result.filtered_syn, 

result.processed_syn, 

result.tshift)): 

 

norm = num.sum(num.abs(tr_proc.ydata)) \ 

/ tr_proc.data_len() 

tr_filt.ydata /= norm 

tr_proc.ydata /= norm 

 

tr_filt.shift(tshift) 

tr_proc.shift(tshift) 

 

ctr = result.cc 

ctr.shift(tref) 

 

dtrace = ctr 

 

else: 

for tr in ( 

result.filtered_obs, 

result.filtered_syn, 

result.processed_obs, 

result.processed_syn): 

 

tr.ydata *= w 

 

if result.tshift is not None and result.tshift != 0.0: 

# result.filtered_syn.shift(result.tshift) 

result.processed_syn.shift(result.tshift) 

 

dtrace = make_norm_trace( 

result.processed_syn, result.processed_obs, 

problem.norm_exponent) 

 

target_to_results[target].append(result) 

 

dtrace.meta = dict( 

normalisation_family=target.normalisation_family, 

path=target.path) 

 

dtraces[-1].append(dtrace) 

 

result.processed_syn.meta = dict( 

normalisation_family=target.normalisation_family, 

path=target.path) 

 

all_syn_trs.append(result.processed_syn) 

 

if not all_syn_trs: 

logger.warn('No traces to show!') 

return 

 

def skey(tr): 

return tr.meta['normalisation_family'], tr.meta['path'] 

 

trace_minmaxs = trace.minmax(all_syn_trs, skey) 

 

dtraces_all = [] 

for dtraces_group in dtraces: 

dtraces_all.extend(dtraces_group) 

 

dminmaxs = trace.minmax([ 

dtrace_ for dtrace_ in dtraces_all if dtrace_ is not None], skey) 

 

for tr in dtraces_all: 

if tr: 

dmin, dmax = dminmaxs[skey(tr)] 

tr.ydata /= max(abs(dmin), abs(dmax)) 

 

cg_to_targets = meta.gather( 

problem.waveform_targets, 

lambda t: (t.path, t.codes[3]), 

filter=lambda t: t in target_to_results) 

 

cgs = sorted(cg_to_targets.keys()) 

 

from matplotlib import colors 

cmap = cm.ScalarMappable( 

norm=colors.Normalize(vmin=num.min(icolor), vmax=num.max(icolor)), 

cmap=plt.get_cmap('coolwarm')) 

 

imodel_to_color = [] 

for imodel in range(nmodels): 

imodel_to_color.append(cmap.to_rgba(icolor[imodel])) 

 

for cg in cgs: 

targets = cg_to_targets[cg] 

 

frame_to_target, nx, ny, nxx, nyy = layout( 

source, targets, nxmax, nymax) 

 

figures = {} 

for iy in range(ny): 

for ix in range(nx): 

if (iy, ix) not in frame_to_target: 

continue 

 

ixx = ix // nxmax 

iyy = iy // nymax 

if (iyy, ixx) not in figures: 

title = '_'.join(x for x in cg if x) 

item = PlotItem( 

name='fig_%s_%i_%i' % (title, ixx, iyy)) 

item.attributes['targets'] = [] 

figures[iyy, ixx] = ( 

item, plt.figure(figsize=self.size_inch)) 

 

figures[iyy, ixx][1].subplots_adjust( 

left=0.03, 

right=1.0 - 0.03, 

bottom=0.03, 

top=1.0 - 0.06, 

wspace=0.2, 

hspace=0.2) 

 

item, fig = figures[iyy, ixx] 

 

target = frame_to_target[iy, ix] 

 

item.attributes['targets'].append(target.string_id()) 

 

amin, amax = trace_minmaxs[ 

target.normalisation_family, target.path] 

absmax = max(abs(amin), abs(amax)) 

 

ny_this = nymax # min(ny, nymax) 

nx_this = nxmax # min(nx, nxmax) 

i_this = (iy % ny_this) * nx_this + (ix % nx_this) + 1 

 

axes2 = fig.add_subplot(ny_this, nx_this, i_this) 

 

space = 0.5 

space_factor = 1.0 + space 

axes2.set_axis_off() 

axes2.set_ylim(-1.05 * space_factor, 1.05) 

 

axes = axes2.twinx() 

axes.set_axis_off() 

 

if target.misfit_config.domain == 'cc_max_norm': 

axes.set_ylim(-10. * space_factor, 10.) 

else: 

axes.set_ylim(-absmax*1.33 * space_factor, absmax*1.33) 

 

itarget, itarget_end = target_index[target] 

assert itarget_end == itarget + 1 

 

for imodel, result in enumerate(target_to_results[target]): 

 

syn_color = imodel_to_color[imodel] 

 

dtrace = dtraces[imodel][itarget] 

 

tap_color_annot = (0.35, 0.35, 0.25) 

tap_color_edge = (0.85, 0.85, 0.80) 

tap_color_fill = (0.95, 0.95, 0.90) 

 

plot_taper( 

axes2, 

result.processed_obs.get_xdata(), 

result.taper, 

fc=tap_color_fill, ec=tap_color_edge, alpha=0.2) 

 

obs_color = mpl_color('aluminium5') 

obs_color_light = light(obs_color, 0.5) 

 

plot_dtrace( 

axes2, dtrace, space, 0., 1., 

fc='none', 

ec=syn_color) 

 

# plot_trace( 

# axes, result.filtered_syn, 

# color=syn_color_light, lw=1.0) 

 

if imodel == 0: 

plot_trace( 

axes, result.filtered_obs, 

color=obs_color_light, lw=0.75) 

 

plot_trace( 

axes, result.processed_syn, 

color=syn_color, lw=1.0, alpha=0.3) 

 

plot_trace( 

axes, result.processed_obs, 

color=obs_color, lw=0.75, alpha=0.3) 

 

if imodel != 0: 

continue 

xdata = result.filtered_obs.get_xdata() 

axes.set_xlim(xdata[0], xdata[-1]) 

 

tmarks = [ 

result.processed_obs.tmin, 

result.processed_obs.tmax] 

 

for tmark in tmarks: 

axes2.plot( 

[tmark, tmark], [-0.9, 0.1], 

color=tap_color_annot) 

 

dur = tmarks[1] - tmarks[0] 

for tmark, text, ha in [ 

(tmarks[0], 

'$\\,$ ' + meta.str_duration( 

tmarks[0] - source.time), 

'left'), 

(tmarks[1], 

'$\\Delta$ ' + meta.str_duration( 

dur), 

'right')]: 

 

axes2.annotate( 

text, 

xy=(tmark, -0.9), 

xycoords='data', 

xytext=( 

fontsize*0.4 * [-1, 1][ha == 'left'], 

fontsize*0.2), 

textcoords='offset points', 

ha=ha, 

va='bottom', 

color=tap_color_annot, 

fontsize=fontsize) 

 

axes2.set_xlim( 

tmarks[0] - dur*0.1, tmarks[1] + dur*0.1) 

 

scale_string = None 

 

if target.misfit_config.domain == 'cc_max_norm': 

scale_string = 'Syn/obs scales differ!' 

 

infos = [] 

if scale_string: 

infos.append(scale_string) 

 

if self.nx == 1 and self.ny == 1: 

infos.append(target.string_id()) 

else: 

infos.append('.'.join(x for x in target.codes if x)) 

dist = source.distance_to(target) 

azi = source.azibazi_to(target)[0] 

infos.append(meta.str_dist(dist)) 

infos.append(u'%.0f\u00B0' % azi) 

axes2.annotate( 

'\n'.join(infos), 

xy=(0., 1.), 

xycoords='axes fraction', 

xytext=(2., 2.), 

textcoords='offset points', 

ha='left', 

va='top', 

fontsize=fontsize, 

fontstyle='normal') 

 

if (self.nx == 1 and self.ny == 1): 

yield item, fig 

del figures[iyy, ixx] 

 

if not (self.nx == 1 and self.ny == 1): 

for (iyy, ixx), (_, fig) in figures.items(): 

title = '.'.join(x for x in cg if x) 

if len(figures) > 1: 

title += ' (%i/%i, %i/%i)' % (iyy+1, nyy, ixx+1, nxx) 

 

fig.suptitle(title, fontsize=fontsize_title) 

 

for item, fig in figures.values(): 

yield item, fig 

 

 

class FitsWaveformPlot(PlotConfig): 

''' Plot showing the waveform fits for the best model ''' 

name = 'fits_waveform' 

size_cm = Tuple.T( 

2, Float.T(), 

default=(9., 5.), 

help='width and length of the figure in cm') 

nx = Int.T( 

default=1, 

help='horizontal number of subplots on every page') 

ny = Int.T( 

default=1, 

help='vertical number of subplots on every page') 

font_size = Float.T( 

default=8, 

help='Font Size of all fonts, except title') 

font_size_title = Float.T( 

default=10, 

help='Font Size of title') 

 

def make(self, environ): 

cm = environ.get_plot_collection_manager() 

mpl_init(fontsize=self.font_size) 

environ.setup_modelling() 

ds = environ.get_dataset() 

optimiser = environ.get_optimiser() 

 

environ.setup_modelling() 

 

history = environ.get_history(subset='harvest') 

cm.create_group_mpl( 

self, 

self.draw_figures(ds, history, optimiser), 

title=u'Waveform fits for best model', 

section='fits', 

feather_icon='activity', 

description=u''' 

Plot showing observed and synthetic waveform (attributes) for the best fitting 

model. 

 

Best model's waveform fits for all targets. Depending on the target 

configurations different types of comparisons are possible: (i) time domain 

waveform differences, (ii) amplitude spectra, (iii) envelopes, (iv) cross 

correlation functions. Each waveform plot gives a number of details: 

 

1) Target information (left side, from top to bottom) gives station name with 

component, distance to source, azimuth of station with respect to source, 

target weight, target misfit and starting time of the waveform relative to the 

origin time. 

 

2) The background gray area shows the applied taper function. 

 

3) The waveforms shown are: the restituted and filtered observed trace without 

tapering (light grey) and the same trace with tapering and processing (dark 

gray), the synthetic trace (light red) and the filtered, tapered and (if 

enabled) shifted and processed synthetic target trace (red). The traces are 

scaled according to the target weight (small weight, small amplitude) and 

normed relative to the maximum amplitude of the targets of the corresponding 

normalisation family. 

 

4) The bottom panel shows, depending on the type of comparison, sample-wise 

residuals for time domain comparisons (red filled), spectra of observed and 

synthetic traces for amplitude spectrum comparisons, or cross correlation 

traces. 

 

5) Colored boxes on the upper right show the relative weight of the target 

within the entire dataset of the optimisation (top box, orange) and the 

relative misfit contribution to the global misfit of the optimisation (bottom 

box, red). 

''') 

 

def draw_figures(self, ds, history, optimiser): 

 

fontsize = self.font_size 

fontsize_title = self.font_size_title 

 

nxmax = self.nx 

nymax = self.ny 

 

problem = history.problem 

 

for target in problem.targets: 

target.set_dataset(ds) 

 

target_index = {} 

i = 0 

for target in problem.targets: 

target_index[target] = i, i+target.nmisfits 

i += target.nmisfits 

 

xbest = history.get_best_model() 

misfits = history.misfits[history.get_sorted_misfits_idx(chain=0), ...] 

 

ws = problem.get_target_weights() 

 

gcms = problem.combine_misfits( 

misfits[:1, :, :], 

extra_correlated_weights=optimiser.get_correlated_weights(problem), 

get_contributions=True)[0, :] 

 

w_max = num.nanmax(ws) 

gcm_max = num.nanmax(gcms) 

 

source = problem.get_source(xbest) 

 

target_to_result = {} 

all_syn_trs = [] 

all_syn_specs = [] 

results = problem.evaluate(xbest) 

 

dtraces = [] 

for target, result in zip(problem.targets, results): 

if not isinstance(result, WaveformMisfitResult): 

dtraces.extend([None] * target.nmisfits) 

continue 

 

itarget, itarget_end = target_index[target] 

assert itarget_end == itarget + 1 

 

w = target.get_combined_weight() 

 

if target.misfit_config.domain == 'cc_max_norm': 

tref = ( 

result.filtered_obs.tmin + result.filtered_obs.tmax) * 0.5 

for tr_filt, tr_proc, tshift in ( 

(result.filtered_obs, 

result.processed_obs, 

0.), 

(result.filtered_syn, 

result.processed_syn, 

result.tshift)): 

 

norm = num.sum(num.abs(tr_proc.ydata)) / tr_proc.data_len() 

tr_filt.ydata /= norm 

tr_proc.ydata /= norm 

 

tr_filt.shift(tshift) 

tr_proc.shift(tshift) 

 

ctr = result.cc 

ctr.shift(tref) 

 

dtrace = ctr 

 

else: 

for tr in ( 

result.filtered_obs, 

result.filtered_syn, 

result.processed_obs, 

result.processed_syn): 

 

tr.ydata *= w 

 

for spec in ( 

result.spectrum_obs, 

result.spectrum_syn): 

 

if spec is not None: 

spec.ydata *= w 

 

if result.tshift is not None and result.tshift != 0.0: 

# result.filtered_syn.shift(result.tshift) 

result.processed_syn.shift(result.tshift) 

 

dtrace = make_norm_trace( 

result.processed_syn, result.processed_obs, 

problem.norm_exponent) 

 

target_to_result[target] = result 

 

dtrace.meta = dict( 

normalisation_family=target.normalisation_family, 

path=target.path) 

dtraces.append(dtrace) 

 

result.processed_syn.meta = dict( 

normalisation_family=target.normalisation_family, 

path=target.path) 

 

all_syn_trs.append(result.processed_syn) 

 

if result.spectrum_syn: 

result.spectrum_syn.meta = dict( 

normalisation_family=target.normalisation_family, 

path=target.path) 

 

all_syn_specs.append(result.spectrum_syn) 

 

if not all_syn_trs: 

logger.warn('No traces to show!') 

return 

 

def skey(tr): 

return tr.meta['normalisation_family'], tr.meta['path'] 

 

trace_minmaxs = trace.minmax(all_syn_trs, skey) 

 

amp_spec_maxs = amp_spec_max(all_syn_specs, skey) 

 

dminmaxs = trace.minmax([x for x in dtraces if x is not None], skey) 

 

for tr in dtraces: 

if tr: 

dmin, dmax = dminmaxs[skey(tr)] 

tr.ydata /= max(abs(dmin), abs(dmax)) 

 

cg_to_targets = meta.gather( 

problem.waveform_targets, 

lambda t: (t.path, t.codes[3]), 

filter=lambda t: t in target_to_result) 

 

cgs = sorted(cg_to_targets.keys()) 

 

for cg in cgs: 

targets = cg_to_targets[cg] 

 

frame_to_target, nx, ny, nxx, nyy = layout( 

source, targets, nxmax, nymax) 

 

figures = {} 

for iy in range(ny): 

for ix in range(nx): 

if (iy, ix) not in frame_to_target: 

continue 

 

ixx = ix // nxmax 

iyy = iy // nymax 

if (iyy, ixx) not in figures: 

title = '_'.join(x for x in cg if x) 

item = PlotItem( 

name='fig_%s_%i_%i' % (title, ixx, iyy)) 

item.attributes['targets'] = [] 

figures[iyy, ixx] = ( 

item, plt.figure(figsize=self.size_inch)) 

 

figures[iyy, ixx][1].subplots_adjust( 

left=0.03, 

right=1.0 - 0.03, 

bottom=0.03, 

top=1.0 - 0.06, 

wspace=0.2, 

hspace=0.2) 

 

item, fig = figures[iyy, ixx] 

 

target = frame_to_target[iy, ix] 

 

item.attributes['targets'].append(target.string_id()) 

 

amin, amax = trace_minmaxs[ 

target.normalisation_family, target.path] 

absmax = max(abs(amin), abs(amax)) 

 

ny_this = nymax # min(ny, nymax) 

nx_this = nxmax # min(nx, nxmax) 

i_this = (iy % ny_this) * nx_this + (ix % nx_this) + 1 

 

axes2 = fig.add_subplot(ny_this, nx_this, i_this) 

 

space = 0.5 

space_factor = 1.0 + space 

axes2.set_axis_off() 

axes2.set_ylim(-1.05 * space_factor, 1.05) 

 

axes = axes2.twinx() 

axes.set_axis_off() 

 

if target.misfit_config.domain == 'cc_max_norm': 

axes.set_ylim(-10. * space_factor, 10.) 

else: 

axes.set_ylim( 

-absmax * 1.33 * space_factor, absmax * 1.33) 

 

itarget, itarget_end = target_index[target] 

assert itarget_end == itarget + 1 

 

result = target_to_result[target] 

 

dtrace = dtraces[itarget] 

 

tap_color_annot = (0.35, 0.35, 0.25) 

tap_color_edge = (0.85, 0.85, 0.80) 

tap_color_fill = (0.95, 0.95, 0.90) 

 

plot_taper( 

axes2, result.processed_obs.get_xdata(), result.taper, 

fc=tap_color_fill, ec=tap_color_edge) 

 

obs_color = mpl_color('aluminium5') 

obs_color_light = light(obs_color, 0.5) 

 

syn_color = mpl_color('scarletred2') 

syn_color_light = light(syn_color, 0.5) 

 

misfit_color = mpl_color('scarletred2') 

weight_color = mpl_color('chocolate2') 

 

cc_color = mpl_color('aluminium5') 

 

if target.misfit_config.domain == 'cc_max_norm': 

tref = (result.filtered_obs.tmin + 

result.filtered_obs.tmax) * 0.5 

 

plot_dtrace( 

axes2, dtrace, space, -1., 1., 

fc=light(cc_color, 0.5), 

ec=cc_color) 

 

plot_dtrace_vline( 

axes2, tref, space, color=tap_color_annot) 

 

elif target.misfit_config.domain == 'frequency_domain': 

 

asmax = amp_spec_maxs[ 

target.normalisation_family, target.path] 

fmin, fmax = \ 

target.misfit_config.get_full_frequency_range() 

 

plot_spectrum( 

axes2, 

result.spectrum_syn, 

result.spectrum_obs, 

fmin, fmax, 

space, 0., asmax, 

syn_color=syn_color, 

obs_color=obs_color, 

syn_lw=1.0, 

obs_lw=0.75, 

color_vline=tap_color_annot, 

fontsize=fontsize) 

 

else: 

plot_dtrace( 

axes2, dtrace, space, 0., 1., 

fc=light(misfit_color, 0.3), 

ec=misfit_color) 

 

plot_trace( 

axes, result.filtered_syn, 

color=syn_color_light, lw=1.0) 

 

plot_trace( 

axes, result.filtered_obs, 

color=obs_color_light, lw=0.75) 

 

plot_trace( 

axes, result.processed_syn, 

color=syn_color, lw=1.0) 

 

plot_trace( 

axes, result.processed_obs, 

color=obs_color, lw=0.75) 

 

# xdata = result.filtered_obs.get_xdata() 

 

tmarks = [ 

result.processed_obs.tmin, 

result.processed_obs.tmax] 

 

for tmark in tmarks: 

axes2.plot( 

[tmark, tmark], [-0.9, 0.1], color=tap_color_annot) 

 

dur = tmarks[1] - tmarks[0] 

for tmark, text, ha in [ 

(tmarks[0], 

'$\\,$ ' + meta.str_duration( 

tmarks[0] - source.time), 

'left'), 

(tmarks[1], 

'$\\Delta$ ' + meta.str_duration(dur), 

'right')]: 

 

axes2.annotate( 

text, 

xy=(tmark, -0.9), 

xycoords='data', 

xytext=( 

fontsize * 0.4 * [-1, 1][ha == 'left'], 

fontsize * 0.2), 

textcoords='offset points', 

ha=ha, 

va='bottom', 

color=tap_color_annot, 

fontsize=fontsize) 

 

axes2.set_xlim(tmarks[0] - dur*0.1, tmarks[1] + dur*0.1) 

 

rel_w = ws[itarget] / w_max 

rel_c = gcms[itarget] / gcm_max 

 

sw = 0.25 

sh = 0.1 

ph = 0.01 

 

for (ih, rw, facecolor, edgecolor) in [ 

(0, rel_w, light(weight_color, 0.5), 

weight_color), 

(1, rel_c, light(misfit_color, 0.5), 

misfit_color)]: 

 

bar = patches.Rectangle( 

(1.0 - rw * sw, 1.0 - (ih + 1) * sh + ph), 

rw * sw, 

sh - 2 * ph, 

facecolor=facecolor, edgecolor=edgecolor, 

zorder=10, 

transform=axes.transAxes, clip_on=False) 

 

axes.add_patch(bar) 

 

scale_string = None 

 

if target.misfit_config.domain == 'cc_max_norm': 

scale_string = 'Syn/obs scales differ!' 

 

infos = [] 

if scale_string: 

infos.append(scale_string) 

 

if self.nx == 1 and self.ny == 1: 

infos.append(target.string_id()) 

else: 

infos.append('.'.join(x for x in target.codes if x)) 

 

dist = source.distance_to(target) 

azi = source.azibazi_to(target)[0] 

infos.append(meta.str_dist(dist)) 

infos.append('%.0f\u00B0' % azi) 

infos.append('%.3g' % ws[itarget]) 

infos.append('%.3g' % gcms[itarget]) 

axes2.annotate( 

'\n'.join(infos), 

xy=(0., 1.), 

xycoords='axes fraction', 

xytext=(2., 2.), 

textcoords='offset points', 

ha='left', 

va='top', 

fontsize=fontsize, 

fontstyle='normal') 

 

if (self.nx == 1 and self.ny == 1): 

yield item, fig 

del figures[iyy, ixx] 

 

if not (self.nx == 1 and self.ny == 1): 

for (iyy, ixx), (_, fig) in figures.items(): 

title = '.'.join(x for x in cg if x) 

if len(figures) > 1: 

title += ' (%i/%i, %i/%i)' % ( 

iyy + 1, nyy, ixx + 1, nxx) 

 

fig.suptitle(title, fontsize=fontsize_title) 

 

for item, fig in figures.values(): 

yield item, fig 

 

 

class WaveformStationDistribution(StationDistributionPlot): 

''' Plot showing all waveform fits for the ensemble of solutions''' 

 

name = 'seismic_stations' 

 

def make(self, environ): 

from grond.problems.base import ProblemDataNotAvailable 

from grond.environment import NoRundirAvailable 

 

cm = environ.get_plot_collection_manager() 

mpl_init(fontsize=self.font_size) 

 

problem = environ.get_problem() 

dataset = environ.get_dataset() 

try: 

history = environ.get_history(subset='harvest') 

except (NoRundirAvailable, ProblemDataNotAvailable): 

history = None 

 

cm.create_group_mpl( 

self, 

self.draw_figures(problem, dataset, history), 

title=u'Seismic station locations', 

section='checks', 

feather_icon='target', 

description=u''' 

Plot showing seismic station locations and attributes. 

 

Station locations in dependence of distance and azimuth are shown. The center 

of the plot corresponds to the origin of the search space, not to the optimised 

location of the source. 

''') 

 

def draw_figures(self, problem, dataset, history): 

 

target_index = {} 

i = 0 

for target in problem.targets: 

target_index[target] = i, i+target.nmisfits 

i += target.nmisfits 

 

ws = problem.get_target_weights() 

 

if history: 

misfits = history.misfits[history.get_sorted_misfits_idx(), ...] 

gcms = problem.combine_misfits( 

misfits[:1, :, :], get_contributions=True)[0, :] 

 

event = problem.base_source 

 

cg_to_targets = meta.gather( 

problem.waveform_targets, 

lambda t: (t.path, t.codes[3])) 

 

cgs = sorted(cg_to_targets.keys()) 

 

for cg in cgs: 

cg_str = '.'.join(cg) 

 

targets = cg_to_targets[cg] 

if len(targets) == 0: 

continue 

 

assert all(target_index[target][0] == target_index[target][1] - 1 

for target in targets) 

 

itargets = num.array( 

[target_index[target][0] for target in targets]) 

 

labels = ['.'.join(x for x in t.codes[:3] if x) for t in targets] 

 

azimuths = num.array([event.azibazi_to(t)[0] for t in targets]) 

distances = num.array([t.distance_to(event) for t in targets]) 

 

item = PlotItem( 

name='seismic_stations_weights_%s' % cg_str, 

title=u'Station weights (%s)' % cg_str, 

description=u'\n\nMarkers are scaled according to the ' 

u'weighting factor of the corresponding target\'s ' 

u'contribution in the misfit function.') 

fig, ax, legend = self.plot_station_distribution( 

azimuths, distances, ws[itargets], labels) 

legend.set_title( 

'Weight', 

prop=dict(size=self.font_size)) 

 

yield (item, fig) 

 

if history: 

item = PlotItem( 

name='seismic_stations_contributions_%s' % cg_str, 

title=u'Station misfit contributions (%s)' % cg_str, 

description=u'\n\nMarkers are scaled according to their ' 

u'misfit contribution for the globally best ' 

u'source model.') 

fig, ax, legend = self.plot_station_distribution( 

azimuths, distances, gcms[itargets], labels) 

legend.set_title( 

'Contribution', 

prop=dict(size=self.font_size)) 

 

yield (item, fig) 

 

 

def get_plot_classes(): 

return [ 

WaveformStationDistribution, 

CheckWaveformsPlot, 

FitsWaveformPlot, 

FitsWaveformEnsemblePlot 

]