core.py 38.2 KB
Newer Older
Sebastian Heimann's avatar
Sebastian Heimann committed
1
2
3
4
5
6
7
8
9
10
import math
import os
import sys
import logging
import time
import copy
import shutil
import os.path as op
import numpy as num

11
12
13
from pyrocko.guts import (load, Object, String, Float, Int, Bool, List,
                          StringChoice)
from pyrocko import orthodrome as od, gf, trace, guts, util
14
from pyrocko import parimap, model, marker as pmarker
Sebastian Heimann's avatar
Sebastian Heimann committed
15

16
17
18
19
20
from .dataset import DatasetConfig
from .problems import ProblemConfig, Problem
from .targets import TargetAnalysisResult, TargetConfig
from .meta import (Path, HasPaths, expand_template, xjoin, GrondError,
                   Forbidden)
Sebastian Heimann's avatar
Sebastian Heimann committed
21
22
23
24
25

logger = logging.getLogger('grond.core')
guts_prefix = 'grond'


Sebastian Heimann's avatar
Sebastian Heimann committed
26
27
28
29
30
31
32
def mahalanobis_distance(xs, mx, cov):
    imask = num.diag(cov) != 0.
    icov = num.linalg.inv(cov[imask, :][:, imask])
    temp = xs[:, imask] - mx[imask]
    return num.sqrt(num.sum(temp * num.dot(icov, temp.T).T, axis=1))


Sebastian Heimann's avatar
Sebastian Heimann committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class DirectoryAlreadyExists(Exception):
    pass


class AnalyserConfig(Object):
    niter = Int.T(default=1000)


class SamplerDistributionChoice(StringChoice):
    choices = ['multivariate_normal', 'normal']


class SolverConfig(Object):
    niter_uniform = Int.T(default=1000)
Sebastian Heimann's avatar
Sebastian Heimann committed
47
    niter_transition = Int.T(default=0)
Sebastian Heimann's avatar
Sebastian Heimann committed
48
49
50
51
    niter_explorative = Int.T(default=10000)
    niter_non_explorative = Int.T(default=0)
    sampler_distribution = SamplerDistributionChoice.T(
        default='multivariate_normal')
52
    scatter_scale_transition = Float.T(default=2.0)
53
    scatter_scale = Float.T(default=1.0)
Sebastian Heimann's avatar
Sebastian Heimann committed
54
55
56
57

    def get_solver_kwargs(self):
        return dict(
            niter_uniform=self.niter_uniform,
Sebastian Heimann's avatar
Sebastian Heimann committed
58
            niter_transition=self.niter_transition,
Sebastian Heimann's avatar
Sebastian Heimann committed
59
60
            niter_explorative=self.niter_explorative,
            niter_non_explorative=self.niter_non_explorative,
61
            sampler_distribution=self.sampler_distribution,
62
            scatter_scale_transition=self.scatter_scale_transition,
63
            scatter_scale=self.scatter_scale)
Sebastian Heimann's avatar
Sebastian Heimann committed
64
65


Sebastian Heimann's avatar
Sebastian Heimann committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class EngineConfig(HasPaths):
    gf_stores_from_pyrocko_config = Bool.T(default=True)
    gf_store_superdirs = List.T(Path.T())
    gf_store_dirs = List.T(Path.T())

    def __init__(self, *args, **kwargs):
        HasPaths.__init__(self, *args, **kwargs)
        self._engine = None

    def get_engine(self):
        if self._engine is None:
            fp = self.expand_path
            self._engine = gf.LocalEngine(
                use_config=self.gf_stores_from_pyrocko_config,
                store_superdirs=fp(self.gf_store_superdirs),
                store_dirs=fp(self.gf_store_dirs))

        return self._engine


Sebastian Heimann's avatar
Sebastian Heimann committed
86
87
88
89
90
91
92
class Config(HasPaths):
    rundir_template = Path.T()
    dataset_config = DatasetConfig.T()
    target_configs = List.T(TargetConfig.T())
    problem_config = ProblemConfig.T()
    analyser_config = AnalyserConfig.T(default=AnalyserConfig.D())
    solver_config = SolverConfig.T(default=SolverConfig.D())
Sebastian Heimann's avatar
Sebastian Heimann committed
93
    engine_config = EngineConfig.T(default=EngineConfig.D())
Sebastian Heimann's avatar
Sebastian Heimann committed
94
95
96
97

    def __init__(self, *args, **kwargs):
        HasPaths.__init__(self, *args, **kwargs)

Sebastian Heimann's avatar
Sebastian Heimann committed
98
99
100
    def get_event_names(self):
        return self.dataset_config.get_event_names()

101
102
    def get_dataset(self, event_name):
        return self.dataset_config.get_dataset(event_name)
Sebastian Heimann's avatar
Sebastian Heimann committed
103
104

    def get_targets(self, event):
105
        ds = self.get_dataset(event.name)
Sebastian Heimann's avatar
Sebastian Heimann committed
106
107
108
109
110
111
112
113

        targets = []
        for igroup, target_config in enumerate(self.target_configs):
            targets.extend(target_config.get_targets(
                ds, event, 'group_%i' % igroup))

        return targets

114
115
116
117
118
119
120
121
    def setup_modelling_environment(self, problem):
        problem.set_engine(self.engine_config.get_engine())
        ds = self.get_dataset(problem.base_source.name)
        synt = ds.synthetic_test
        if synt:
            synt.set_problem(problem)
            problem.base_source = problem.unpack(synt.get_x())

Sebastian Heimann's avatar
Sebastian Heimann committed
122
123
    def get_problem(self, event):
        targets = self.get_targets(event)
Sebastian Heimann's avatar
Sebastian Heimann committed
124
        problem = self.problem_config.get_problem(event, targets)
125
        self.setup_modelling_environment(problem)
Sebastian Heimann's avatar
Sebastian Heimann committed
126
        return problem
Sebastian Heimann's avatar
Sebastian Heimann committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143


def sarr(a):
    return ' '.join('%15g' % x for x in a)


def load_problem_info_and_data(dirname, subset=None):
    problem = load_problem_info(dirname)
    xs, misfits = load_problem_data(xjoin(dirname, subset), problem)
    return problem, xs, misfits


def load_problem_info(dirname):
    fn = op.join(dirname, 'problem.yaml')
    return guts.load(filename=fn)


144
def load_problem_data(dirname, problem, skip_models=0):
145
    fn = op.join(dirname, 'models')
Sebastian Heimann's avatar
Sebastian Heimann committed
146
147
    with open(fn, 'r') as f:
        nmodels = os.fstat(f.fileno()).st_size / (problem.nparameters * 8)
148
149
        nmodels -= skip_models
        f.seek(skip_models * problem.nparameters * 8)
Sebastian Heimann's avatar
Sebastian Heimann committed
150
151
        data = num.fromfile(
            f, dtype='<f8',
152
153
            count=nmodels * problem.nparameters)\
            .astype(num.float)
Sebastian Heimann's avatar
Sebastian Heimann committed
154

155
    nmodels = data.size/problem.nparameters - skip_models
Sebastian Heimann's avatar
Sebastian Heimann committed
156
157
158
159
    xs = data.reshape((nmodels, problem.nparameters))

    fn = op.join(dirname, 'misfits')
    with open(fn, 'r') as f:
160
        f.seek(skip_models * problem.ntargets * 2 * 8)
Sebastian Heimann's avatar
Sebastian Heimann committed
161
        data = num.fromfile(
162
163
            f, dtype='<f8', count=nmodels*problem.ntargets*2)\
            .astype(num.float)
Sebastian Heimann's avatar
Sebastian Heimann committed
164
165
166
167
168
169
170
171
172
173
174
175

    data = data.reshape((nmodels, problem.ntargets*2))

    combi = num.empty_like(data)
    combi[:, 0::2] = data[:, :problem.ntargets]
    combi[:, 1::2] = data[:, problem.ntargets:]

    misfits = combi.reshape((nmodels, problem.ntargets, 2))

    return xs, misfits


Sebastian Heimann's avatar
Sebastian Heimann committed
176
177
178
179
def get_mean_x(xs):
    return num.mean(xs, axis=0)


180
181
182
183
184
def get_mean_x_and_gm(problem, xs, misfits):
    gms = problem.global_misfits(misfits)
    return num.mean(xs, axis=0), num.mean(gms)


Sebastian Heimann's avatar
Sebastian Heimann committed
185
186
187
188
189
190
def get_best_x(problem, xs, misfits):
    gms = problem.global_misfits(misfits)
    ibest = num.argmin(gms)
    return xs[ibest, :]


191
192
193
194
195
196
def get_best_x_and_gm(problem, xs, misfits):
    gms = problem.global_misfits(misfits)
    ibest = num.argmin(gms)
    return xs[ibest, :], gms[ibest]


Sebastian Heimann's avatar
Sebastian Heimann committed
197
198
199
200
201
202
203
204
205
206
207
208
def get_mean_source(problem, xs):
    x_mean = get_mean_x(xs)
    source = problem.unpack(x_mean)
    return source


def get_best_source(problem, xs, misfits):
    x_best = get_best_x(problem, xs, misfits)
    source = problem.unpack(x_best)
    return source


Sebastian Heimann's avatar
Sebastian Heimann committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
def mean_latlondist(lats, lons):
    if len(lats) == 0:
        return 0., 0., 1000.
    else:
        ns, es = od.latlon_to_ne_numpy(lats[0], lons[0], lats, lons)
        n, e = num.mean(ns), num.mean(es)
        dists = num.sqrt((ns-n)**2 + (es-e)**2)
        lat, lon = od.ne_to_latlon(lats[0], lons[0], n, e)
        return float(lat), float(lon), float(num.max(dists))


def stations_mean_latlondist(stations):
    lats = num.array([s.lat for s in stations])
    lons = num.array([s.lon for s in stations])
    return mean_latlondist(lats, lons)


def read_config(path):
    config = load(filename=path)
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
228
229
230
    if not isinstance(config, Config):
        raise GrondError('invalid Grond configuration in file "%s"' % path)

Sebastian Heimann's avatar
Sebastian Heimann committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    config.set_basepath(op.dirname(path) or '.')
    return config


def analyse(problem, niter=1000, show_progress=False):
    if niter == 0:
        return

    wtargets = []
    for target in problem.targets:
        wtarget = copy.copy(target)
        wtarget.flip_norm = True
        wtarget.weight = 1.0
        wtargets.append(wtarget)

246
    groups, ngroups = problem.get_group_mask()
247

Sebastian Heimann's avatar
Sebastian Heimann committed
248
249
250
251
252
253
    wproblem = problem.copy()
    wproblem.targets = wtargets

    xbounds = num.array(wproblem.bounds(), dtype=num.float)
    npar = xbounds.shape[0]

254
    mss = num.zeros((niter, wproblem.ntargets))
Sebastian Heimann's avatar
Sebastian Heimann committed
255
256
257
258
259
    rstate = num.random.RandomState(123)

    if show_progress:
        pbar = util.progressbar('analysing problem', niter)

260
    isbad_mask = None
Sebastian Heimann's avatar
Sebastian Heimann committed
261
    for iiter in xrange(niter):
262
        break
Sebastian Heimann's avatar
Sebastian Heimann committed
263
264
265
266
267
268
269
270
271
272
273
274
275
        while True:
            x = []
            for i in xrange(npar):
                v = rstate.uniform(xbounds[i, 0], xbounds[i, 1])
                x.append(v)

            try:
                x = wproblem.preconstrain(x)
                break

            except Forbidden:
                pass

276
277
278
279
280
281
        if isbad_mask is not None and num.any(isbad_mask):
            isok_mask = num.logical_not(isbad_mask)
        else:
            isok_mask = None

        _, ms = wproblem.evaluate(x, mask=isok_mask)
Sebastian Heimann's avatar
Sebastian Heimann committed
282
283
        mss[iiter, :] = ms

284
285
        isbad_mask = num.isnan(ms)

Sebastian Heimann's avatar
Sebastian Heimann committed
286
287
288
289
290
291
        if show_progress:
            pbar.update(iiter)

    if show_progress:
        pbar.finish()

292
293
294
    # mean_ms = num.mean(mss, axis=0)
    # weights = 1.0 / mean_ms
    weights = num.ones(wproblem.ntargets)
295
296
297
298
    for igroup in xrange(ngroups):
        weights[groups == igroup] /= (
            num.nansum(weights[groups == igroup]) /
            num.nansum(num.isfinite(weights[groups == igroup])))
Sebastian Heimann's avatar
Sebastian Heimann committed
299
300
301
302
303
304
305
306
307
308
309
310

    for weight, target in zip(weights, problem.targets):
        target.analysis_result = TargetAnalysisResult(
            balancing_weight=float(weight))


def solve(problem,
          rundir=None,
          niter_uniform=1000,
          niter_transition=1000,
          niter_explorative=10000,
          niter_non_explorative=0,
311
          scatter_scale_transition=2.0,
312
          scatter_scale=1.0,
Sebastian Heimann's avatar
Sebastian Heimann committed
313
314
315
316
317
318
319
320
321
322
323
          xs_inject=None,
          sampler_distribution='multivariate_normal',
          status=()):

    xbounds = num.array(problem.bounds(), dtype=num.float)
    npar = xbounds.shape[0]

    nlinks_cap = 8 * npar + 1
    chains_m = num.zeros((1 + problem.nbootstrap, nlinks_cap), num.float)
    chains_i = num.zeros((1 + problem.nbootstrap, nlinks_cap), num.int)
    nlinks = 0
Sebastian Heimann's avatar
Sebastian Heimann committed
324
    mbx = None
Sebastian Heimann's avatar
Sebastian Heimann committed
325
326
327
328
329
330

    if xs_inject is not None and xs_inject.size != 0:
        niter_inject = xs_inject.shape[0]
    else:
        niter_inject = 0

331
    niter = niter_inject + niter_uniform + niter_transition + \
332
        niter_explorative + niter_non_explorative
Sebastian Heimann's avatar
Sebastian Heimann committed
333
334

    iiter = 0
Sebastian Heimann's avatar
Sebastian Heimann committed
335
336
    sbx = None
    mxs = None
Sebastian Heimann's avatar
Sebastian Heimann committed
337
338
339
340
    covs = None
    xhist = num.zeros((niter, npar))
    isbad_mask = None
    accept_sum = num.zeros(1 + problem.nbootstrap, dtype=num.int)
341
    accept_hist = num.zeros(niter, dtype=num.int)
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
342
    pnames = [p.name for p in problem.parameters]
Sebastian Heimann's avatar
Sebastian Heimann committed
343
344
345

    while iiter < niter:

346
347
348
349
350
351
352
353
354
355
356
357
        if iiter < niter_inject:
            phase = 'inject'
        elif iiter < niter_inject + niter_uniform:
            phase = 'uniform'
        elif iiter < niter_inject + niter_uniform + niter_transition:
            phase = 'transition'
        elif iiter < niter_inject + niter_uniform + niter_transition + \
                niter_explorative:
            phase = 'explorative'
        else:
            phase = 'non_explorative'

358
        factor = 0.0
359
        if phase == 'transition':
360
361
362
363
364
365
366
367
368
369
            T = float(niter_transition)
            A = scatter_scale_transition
            B = scatter_scale
            tau = T/(math.log(A) - math.log(B))
            t0 = math.log(A) * T / (math.log(A) - math.log(B))
            t = float(iiter - niter_uniform - niter_inject)
            factor = num.exp(-(t-t0) / tau)

        elif phase in ('explorative', 'non_explorative'):
            factor = scatter_scale
Sebastian Heimann's avatar
Sebastian Heimann committed
370
371
372
373

        ntries_preconstrain = 0
        ntries_sample = 0

374
        if phase == 'inject':
Sebastian Heimann's avatar
Sebastian Heimann committed
375
376
377
378
379
            x = xs_inject[iiter, :]
        else:
            while True:
                ntries_preconstrain += 1

380
                if mbx is None or phase == 'uniform':
381
                    x = problem.random_uniform(xbounds)
Sebastian Heimann's avatar
Sebastian Heimann committed
382
383
384
385
                else:
                    # jchoice = num.random.randint(0, 1 + problem.nbootstrap)
                    jchoice = num.argmin(accept_sum)

386
                    if phase in ('transition', 'explorative'):
Sebastian Heimann's avatar
Sebastian Heimann committed
387
388
389
                        ichoice = num.random.randint(0, nlinks)
                        xb = xhist[chains_i[jchoice, ichoice]]
                    else:
Sebastian Heimann's avatar
Sebastian Heimann committed
390
                        xb = mxs[jchoice]
Sebastian Heimann's avatar
Sebastian Heimann committed
391
392
393
394

                    if sampler_distribution == 'multivariate_normal':
                        ntries_sample = 0

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
395
396
                        ntry = 0
                        ok_mask_sum = num.zeros(npar, dtype=num.int)
Sebastian Heimann's avatar
Sebastian Heimann committed
397
398
399
                        while True:
                            ntries_sample += 1
                            vs = num.random.multivariate_normal(
400
                                xb, factor**2 * covs[jchoice])
Sebastian Heimann's avatar
Sebastian Heimann committed
401

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
402
403
404
405
                            ok_mask = num.logical_and(
                                xbounds[:, 0] <= vs, vs <= xbounds[:, 1])

                            if num.all(ok_mask):
Sebastian Heimann's avatar
Sebastian Heimann committed
406
407
                                break

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
408
409
410
411
412
413
414
415
416
417
418
419
                            ok_mask_sum += ok_mask

                            if ntry > 1000:
                                raise GrondError(
                                    'failed to produce a suitable candidate '
                                    'sample from multivariate normal '
                                    'distribution, (%s)' %
                                    ', '.join('%s:%i' % xx for xx in
                                              zip(pnames, ok_mask_sum)))

                            ntry += 1

Sebastian Heimann's avatar
Sebastian Heimann committed
420
421
422
                        x = vs.tolist()

                    if sampler_distribution == 'normal':
423
                        x = []
Sebastian Heimann's avatar
Sebastian Heimann committed
424
                        for i in xrange(npar):
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
425
                            ntry = 0
Sebastian Heimann's avatar
Sebastian Heimann committed
426
                            while True:
427
428
                                if sbx[i] > 0.:
                                    v = num.random.normal(
429
                                        xb[i], factor*sbx[i])
430
431
432
                                else:
                                    v = xb[i]

Sebastian Heimann's avatar
Sebastian Heimann committed
433
434
435
                                if xbounds[i, 0] <= v and v <= xbounds[i, 1]:
                                    break

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
436
437
438
439
440
441
442
443
                                if ntry > 1000:
                                    raise GrondError(
                                        'failed to produce a suitable '
                                        'candidate sample from normal '
                                        'distribution')

                                ntry += 1

Sebastian Heimann's avatar
Sebastian Heimann committed
444
445
446
447
448
449
450
451
452
                            x.append(v)

                try:
                    x = problem.preconstrain(x)
                    break

                except Forbidden:
                    pass

453
454
455
456
457
458
        if isbad_mask is not None and num.any(isbad_mask):
            isok_mask = num.logical_not(isbad_mask)
        else:
            isok_mask = None

        ms, ns = problem.evaluate(x, mask=isok_mask)
Sebastian Heimann's avatar
Sebastian Heimann committed
459
460
461
462
463
464

        isbad_mask_new = num.isnan(ms)
        if isbad_mask is not None and num.any(isbad_mask != isbad_mask_new):
            logger.error(
                'skipping problem %s: inconsistency in data availability' %
                problem.name)
Sebastian Heimann's avatar
Sebastian Heimann committed
465

Sebastian Heimann's avatar
flake8    
Sebastian Heimann committed
466
467
468
            for target, isbad_new, isbad in zip(
                    problem.targets, isbad_mask_new, isbad_mask):

Sebastian Heimann's avatar
Sebastian Heimann committed
469
470
471
472
                if isbad_new != isbad:
                    logger.error('%s, %s -> %s' % (
                        target.string_id(), isbad, isbad_new))

Sebastian Heimann's avatar
Sebastian Heimann committed
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
            return

        isbad_mask = isbad_mask_new

        if num.all(isbad_mask):
            logger.error(
                'skipping problem %s: all target misfit values are NaN' %
                problem.name)
            return

        if rundir:
            problem.dump_problem_data(rundir, x, ms, ns)

        m = problem.global_misfit(ms, ns)
        ms = problem.bootstrap_misfit(ms, ns)

        chains_m[0, nlinks] = m
        chains_m[1:, nlinks] = ms
        chains_i[:, nlinks] = iiter

        nlinks += 1

        for ichain in xrange(chains_m.shape[0]):
            isort = num.argsort(chains_m[ichain, :nlinks])
            chains_m[ichain, :nlinks] = chains_m[ichain, isort]
            chains_i[ichain, :nlinks] = chains_i[ichain, isort]

        if nlinks == nlinks_cap:
            accept = (chains_i[:, nlinks_cap-1] != iiter).astype(num.int)
            nlinks -= 1
        else:
            accept = num.ones(1 + problem.nbootstrap, dtype=num.int)

        accept_sum += accept
507
        accept_hist[iiter] = num.sum(accept)
Sebastian Heimann's avatar
Sebastian Heimann committed
508
509
510
511
512
513
514
515
516
517
518
519
520

        lines = []
        if 'state' in status:
            lines.append('%i' % iiter)
            lines.append(''.join('-X'[int(acc)] for acc in accept))

        xhist[iiter, :] = x

        bxs = xhist[chains_i[:, :nlinks].ravel(), :]
        gxs = xhist[chains_i[0, :nlinks], :]
        gms = chains_m[0, :nlinks]

        if nlinks > (nlinks_cap-1)/2:
Sebastian Heimann's avatar
Sebastian Heimann committed
521
522
523
524
525
526
527
528
529
530
531
            # mean and std of all bootstrap ensembles together
            mbx = num.mean(bxs, axis=0)
            sbx = num.std(bxs, axis=0)

            # mean and std of global configuration
            mgx = num.mean(gxs, axis=0)
            sgx = num.std(gxs, axis=0)

            # best in global configuration
            bgx = xhist[chains_i[0, 0], :]

Sebastian Heimann's avatar
Sebastian Heimann committed
532
            covs = []
Sebastian Heimann's avatar
Sebastian Heimann committed
533
            mxs = []
Sebastian Heimann's avatar
Sebastian Heimann committed
534
535
            for i in xrange(1 + problem.nbootstrap):
                xs = xhist[chains_i[i, :nlinks], :]
Sebastian Heimann's avatar
Sebastian Heimann committed
536
537
538
539
540
                mx = num.mean(xs, axis=0)
                cov = num.cov(xs.T)

                mxs.append(mx)
                covs.append(cov)
Sebastian Heimann's avatar
Sebastian Heimann committed
541
542
543
544
545
546
547

            if 'state' in status:
                lines.append(
                    '%-15s %15s %15s %15s %15s %15s' %
                    ('parameter', 'B mean', 'B std', 'G mean', 'G std',
                     'G best'))

Sebastian Heimann's avatar
Sebastian Heimann committed
548
                for (pname, mbv, sbv, mgv, sgv, bgv) in zip(
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
549
                        pnames, mbx, sbx, mgx, sgx, bgx):
Sebastian Heimann's avatar
Sebastian Heimann committed
550
551
552

                    lines.append(
                        '%-15s %15.4g %15.4g %15.4g %15.4g %15.4g' %
Sebastian Heimann's avatar
Sebastian Heimann committed
553
                        (pname, mbv, sbv, mgv, sgv, bgv))
Sebastian Heimann's avatar
Sebastian Heimann committed
554
555
556
557
558
559
560
561

                lines.append('%-15s %15s %15s %15.4g %15.4g %15.4g' % (
                    'misfit', '', '',
                    num.mean(gms), num.std(gms), num.min(gms)))

        if 'state' in status:
            lines.append(
                '%-15s %15i %-15s %15i %15i' % (
562
                    'iteration', iiter+1, '(%s, %g)' % (phase, factor),
Sebastian Heimann's avatar
Sebastian Heimann committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
                    ntries_sample, ntries_preconstrain))

        if 'matrix' in status:
            matrix = (chains_i[:, :30] % 94 + 32).T
            for row in matrix[::-1]:
                lines.append(''.join(chr(xxx) for xxx in row))

        if status:
            lines[0:0] = ['\033[2J']
            lines.append('')
            print '\n'.join(lines)

        iiter += 1


Sebastian Heimann's avatar
Sebastian Heimann committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
def bootstrap_outliers(problem, misfits, std_factor=1.0):
    '''
    Identify bootstrap configurations performing bad in global configuration
    '''

    gms = problem.global_misfits(misfits)

    ibests = []
    for ibootstrap in xrange(problem.nbootstrap):
        bms = problem.bootstrap_misfits(misfits, ibootstrap)
        ibests.append(num.argmin(bms))

    m = num.median(gms[ibests])
    s = num.std(gms[ibests])

    return num.where(gms > m+s)[0]


Sebastian Heimann's avatar
Sebastian Heimann committed
596
597
598
599
def forward(rundir_or_config_path, event_names):

    if not event_names:
        return
Sebastian Heimann's avatar
Sebastian Heimann committed
600

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
601
    if os.path.isdir(rundir_or_config_path):
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
602
603
604
605
        rundir = rundir_or_config_path
        config = guts.load(
            filename=op.join(rundir, 'config.yaml'))

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
606
        config.set_basepath(rundir)
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
607
608
        problem, xs, misfits = load_problem_info_and_data(
            rundir, subset='harvest')
Sebastian Heimann's avatar
Sebastian Heimann committed
609

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
610
611
612
        gms = problem.global_misfits(misfits)
        ibest = num.argmin(gms)
        xbest = xs[ibest, :]
Sebastian Heimann's avatar
Sebastian Heimann committed
613

614
        ds = config.get_dataset(problem.base_source.name)
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
615
        problem.set_engine(config.engine_config.get_engine())
Sebastian Heimann's avatar
Sebastian Heimann committed
616

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
617
618
619
620
621
622
623
624
625
        for target in problem.targets:
            target.set_dataset(ds)

        payload = [(problem, xbest)]

    else:
        config = read_config(rundir_or_config_path)

        payload = []
626
627
628
        for event_name in event_names:
            ds = config.get_dataset(event_name)
            event = ds.get_event()
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
629
630
631
632
            problem = config.get_problem(event)
            xref = problem.preconstrain(
                problem.pack(problem.base_source))
            payload.append((problem, xref))
Sebastian Heimann's avatar
Sebastian Heimann committed
633
634

    all_trs = []
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
635
636
637
    events = []
    for (problem, x) in payload:
        ds.empty_cache()
638
        ms, ns, results = problem.evaluate(x, result_mode='full')
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
639
640
641

        event = problem.unpack(x).pyrocko_event()
        events.append(event)
Sebastian Heimann's avatar
Sebastian Heimann committed
642
643

        for result in results:
644
            if not isinstance(result, gf.SeismosizerError):
Sebastian Heimann's avatar
Sebastian Heimann committed
645
646
647
648
649
                result.filtered_obs.set_codes(location='ob')
                result.filtered_syn.set_codes(location='sy')
                all_trs.append(result.filtered_obs)
                all_trs.append(result.filtered_syn)

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
650
651
    markers = []
    for ev in events:
652
        markers.append(pmarker.EventMarker(ev))
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
653
654

    trace.snuffle(all_trs, markers=markers, stations=ds.get_stations())
Sebastian Heimann's avatar
Sebastian Heimann committed
655
656


Sebastian Heimann's avatar
Sebastian Heimann committed
657
def harvest(rundir, problem=None, nbest=10, force=False, weed=0):
Sebastian Heimann's avatar
Sebastian Heimann committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672

    if problem is None:
        problem, xs, misfits = load_problem_info_and_data(rundir)
    else:
        xs, misfits = load_problem_data(rundir, problem)

    dumpdir = op.join(rundir, 'harvest')
    if op.exists(dumpdir):
        if force:
            shutil.rmtree(dumpdir)
        else:
            raise DirectoryAlreadyExists(dumpdir)

    util.ensuredir(dumpdir)

Sebastian Heimann's avatar
Sebastian Heimann committed
673
674
    ibests_list = []
    ibests = []
Sebastian Heimann's avatar
Sebastian Heimann committed
675
676
677
    gms = problem.global_misfits(misfits)
    isort = num.argsort(gms)

Sebastian Heimann's avatar
Sebastian Heimann committed
678
679
    ibests_list.append(isort[:nbest])

680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
    if weed != 3:
        for ibootstrap in xrange(problem.nbootstrap):
            bms = problem.bootstrap_misfits(misfits, ibootstrap)
            isort = num.argsort(bms)
            ibests_list.append(isort[:nbest])
            ibests.append(isort[0])

        if weed:
            mean_gm_best = num.median(gms[ibests])
            std_gm_best = num.std(gms[ibests])
            ibad = set()

            for ibootstrap, ibest in enumerate(ibests):
                if gms[ibest] > mean_gm_best + std_gm_best:
                    ibad.add(ibootstrap)

            ibests_list = [
                ibests_ for (ibootstrap, ibests_) in enumerate(ibests_list)
                if ibootstrap not in ibad]
Sebastian Heimann's avatar
Sebastian Heimann committed
699

700
    ibests = num.concatenate(ibests_list)
Sebastian Heimann's avatar
Sebastian Heimann committed
701
702
703
704
705
706
707
708

    if weed == 2:
        ibests = ibests[gms[ibests] < mean_gm_best]

    for i in ibests:
        x = xs[i]
        ms = misfits[i, :, 0]
        ns = misfits[i, :, 1]
Sebastian Heimann's avatar
Sebastian Heimann committed
709
710
711
        problem.dump_problem_data(dumpdir, x, ms, ns)


Sebastian Heimann's avatar
Sebastian Heimann committed
712
713
714
715
def get_event_names(config):
    return config.get_event_names()


Sebastian Heimann's avatar
Sebastian Heimann committed
716
def check_problem(problem):
Sebastian Heimann's avatar
Sebastian Heimann committed
717
718
719
720
    if len(problem.targets) == 0:
        raise GrondError('no targets available')


721
722
723
724
725
def check(
        config,
        event_names=None,
        target_string_ids=None,
        show_plot=False,
726
        show_waveforms=False,
727
728
        n_random_synthetics=10):

729
730
731
    if show_plot:
        from matplotlib import pyplot as plt
        from grond.plot import colors
Sebastian Heimann's avatar
Sebastian Heimann committed
732

733
    markers = []
734
735
736
    for ievent, event_name in enumerate(event_names):
        ds = config.get_dataset(event_name)
        event = ds.get_event()
737
        trs_all = []
Sebastian Heimann's avatar
Sebastian Heimann committed
738
739
        try:
            problem = config.get_problem(event)
740

Sebastian Heimann's avatar
Sebastian Heimann committed
741
742
743
744
            _, ngroups = problem.get_group_mask()
            logger.info('number of target supergroups: %i' % ngroups)
            logger.info('number of targets (total): %i' % len(problem.targets))

745
746
747
            if target_string_ids:
                problem.targets = [
                    target for target in problem.targets
Sebastian Heimann's avatar
Sebastian Heimann committed
748
749
                    if util.match_nslc(target_string_ids, target.string_id())]

750
751
            logger.info(
                'number of targets (selected): %i' % len(problem.targets))
752

Sebastian Heimann's avatar
Sebastian Heimann committed
753
754
755
756
757
            check_problem(problem)

            xbounds = num.array(problem.bounds(), dtype=num.float)

            results_list = []
758

759
            sources = []
760
761
            if n_random_synthetics == 0:
                x = problem.pack(problem.base_source)
762
                sources.append(problem.base_source)
763
                ms, ns, results = problem.evaluate(x, result_mode='full')
Sebastian Heimann's avatar
Sebastian Heimann committed
764
765
                results_list.append(results)

766
767
768
            else:
                for i in xrange(n_random_synthetics):
                    x = problem.random_uniform(xbounds)
769
                    sources.append(problem.unpack(x))
770
771
772
                    ms, ns, results = problem.evaluate(x, result_mode='full')
                    results_list.append(results)

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
            if show_waveforms:
                engine = config.engine_config.get_engine()
                times = []
                tdata = []
                for target in problem.targets:
                    tobs_shift_group = []
                    tcuts = []
                    for source in sources:
                        tmin_fit, tmax_fit, tfade, tfade_taper = \
                            target.get_taper_params(engine, source)

                        times.extend((tmin_fit-tfade*2., tmax_fit+tfade*2.))

                        tobs, tsyn = target.get_pick_shift(engine, source)
                        if None not in (tobs, tsyn):
                            tobs_shift = tobs - tsyn
                        else:
                            tobs_shift = 0.0

                        tcuts.append(target.get_cutout_timespan(
                            tmin_fit+tobs_shift, tmax_fit+tobs_shift, tfade))

                        tobs_shift_group.append(tobs_shift)

                    tcuts = num.array(tcuts, dtype=num.float)

                    tdata.append((
                        tfade,
                        num.mean(tobs_shift_group),
                        (num.min(tcuts[:, 0]), num.max(tcuts[:, 1]))))

                tmin = min(times)
                tmax = max(times)

                tmax += (tmax-tmin)*2

                for (tfade, tobs_shift, tcut), target in zip(
                        tdata, problem.targets):

                    store = engine.get_store(target.store_id)

                    deltat = store.config.deltat

                    freqlimits = list(target.get_freqlimits())
                    freqlimits[2] = 0.45/deltat
                    freqlimits[3] = 0.5/deltat
                    freqlimits = tuple(freqlimits)

                    trs_projected, trs_restituted, trs_raw = \
                        ds.get_waveform(
                            target.codes,
                            tmin=tmin+tobs_shift,
                            tmax=tmax+tobs_shift,
                            tfade=tfade,
                            freqlimits=freqlimits,
                            deltat=deltat,
                            backazimuth=target.get_backazimuth_for_waveform(),
                            debug=True)

                    trs_projected = copy.deepcopy(trs_projected)
                    trs_restituted = copy.deepcopy(trs_restituted)
                    trs_raw = copy.deepcopy(trs_raw)

                    for trx in trs_projected + trs_restituted + trs_raw:
                        trx.shift(-tobs_shift)
                        trx.set_codes(
                            network='',
                            station=target.string_id(),
                            location='')

                    for trx in trs_projected:
                        trx.set_codes(location=trx.location + '2_proj')

                    for trx in trs_restituted:
                        trx.set_codes(location=trx.location + '1_rest')

                    for trx in trs_raw:
                        trx.set_codes(location=trx.location + '0_raw')

                    trs_all.extend(trs_projected)
                    trs_all.extend(trs_restituted)
                    trs_all.extend(trs_raw)

                    for source in sources:
                        tmin_fit, tmax_fit, tfade, tfade_taper = \
                            target.get_taper_params(engine, source)

                        markers.append(pmarker.Marker(
                            nslc_ids=[('', target.string_id(), '*', '*')],
                            tmin=tmin_fit, tmax=tmax_fit))

                    markers.append(pmarker.Marker(
                        nslc_ids=[('', target.string_id(), '*', '*')],
                        tmin=tcut[0]-tobs_shift, tmax=tcut[1]-tobs_shift,
                        kind=1))

Sebastian Heimann's avatar
Sebastian Heimann committed
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
            if show_plot:
                for itarget, target in enumerate(problem.targets):
                    yabsmaxs = []
                    for results in results_list:
                        result = results[itarget]
                        if not isinstance(result, gf.SeismosizerError):
                            yabsmaxs.append(
                                num.max(num.abs(
                                    result.filtered_obs.get_ydata())))

                    if yabsmaxs:
                        yabsmax = max(yabsmaxs) or 1.0
                    else:
                        yabsmax = None

                    fig = None
                    ii = 0
                    for results in results_list:
                        result = results[itarget]
                        if not isinstance(result, gf.SeismosizerError):
                            if fig is None:
                                fig = plt.figure()
                                axes = fig.add_subplot(1, 1, 1)
                                axes.set_ylim(0., 4.)
                                axes.set_title('%s' % target.string_id())

                            xdata = result.filtered_obs.get_xdata()
                            ydata = result.filtered_obs.get_ydata() / yabsmax
                            axes.plot(xdata, ydata*0.5 + 3.5, color='black')

                            color = colors[ii % len(colors)]

                            xdata = result.filtered_syn.get_xdata()
                            ydata = result.filtered_syn.get_ydata()
                            ydata = ydata / (num.max(num.abs(ydata)) or 1.0)

                            axes.plot(xdata, ydata*0.5 + 2.5, color=color)

                            xdata = result.processed_syn.get_xdata()
                            ydata = result.processed_syn.get_ydata()
                            ydata = ydata / (num.max(num.abs(ydata)) or 1.0)

                            axes.plot(xdata, ydata*0.5 + 1.5, color=color)
                            if result.tsyn_pick:
                                axes.axvline(
                                    result.tsyn_pick,
                                    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, y2 * 0.5 + 0.5, color='gray')
                            ii += 1
                        else:
                            logger.info(str(result))

                    if fig:
                        plt.show()
Sebastian Heimann's avatar
Sebastian Heimann committed
932

Sebastian Heimann's avatar
Sebastian Heimann committed
933
934
935
936
937
938
939
940
941
942
943
944
945
            else:
                for itarget, target in enumerate(problem.targets):

                    nok = 0
                    for results in results_list:
                        result = results[itarget]
                        if not isinstance(result, gf.SeismosizerError):
                            nok += 1

                    if nok == 0:
                        sok = 'not used'
                    elif nok == len(results_list):
                        sok = 'ok'
946
                    else:
Sebastian Heimann's avatar
Sebastian Heimann committed
947
                        sok = 'not used (%i/%i ok)' % (nok, len(results_list))
Sebastian Heimann's avatar
Sebastian Heimann committed
948

Sebastian Heimann's avatar
Sebastian Heimann committed
949
950
                    logger.info('%-40s %s' % (
                        (target.string_id() + ':', sok)))
Sebastian Heimann's avatar
Sebastian Heimann committed
951
952
953
954
955
956
957

        except GrondError, e:
            logger.error('event %i, %s: %s' % (
                ievent,
                event.name or util.time_to_str(event.time),
                str(e)))

958
959
960
961
    if show_waveforms:
        trace.snuffle(trs_all, stations=ds.get_stations(), markers=markers)


Sebastian Heimann's avatar
Sebastian Heimann committed
962
963
g_state = {}

Sebastian Heimann's avatar
Sebastian Heimann committed
964

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
965
def go(config, event_names=None, force=False, nparallel=1, status=('state',)):
Sebastian Heimann's avatar
Sebastian Heimann committed
966
967

    status = tuple(status)
Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
968
    g_data = (config, force, status, nparallel, event_names)
Sebastian Heimann's avatar
Sebastian Heimann committed
969
    g_state[id(g_data)] = g_data
Sebastian Heimann's avatar
Sebastian Heimann committed
970

971
972
    nevents = len(event_names)

973
    from .baraddur import BaraddurProcess
Marius Isken's avatar
Marius Isken committed
974
    baraddur = BaraddurProcess(project_dir=op.abspath(op.curdir))
975
976
    baraddur.start()

Sebastian Heimann's avatar
Sebastian Heimann committed
977
    for x in parimap.parimap(
978
            process_event,
Sebastian Heimann's avatar
Sebastian Heimann committed
979
980
981
            xrange(nevents),
            [id(g_data)] * nevents,
            nprocs=nparallel):
Sebastian Heimann's avatar
Sebastian Heimann committed
982

Sebastian Heimann's avatar
Sebastian Heimann committed
983
        pass
984
985
986
    logger.info('Grond finished processing %d events. '
                'Ctrl+C to kill Barad-dur')
    baraddur.join()
Sebastian Heimann's avatar
Sebastian Heimann committed
987
988


Sebastian Heimann's avatar
Sebastian Heimann committed
989
990
def process_event(ievent, g_data_id):

Sebastian Heimann's avatar
wip    
Sebastian Heimann committed
991
    config, force, status, nparallel, event_names = g_state[g_data_id]
Sebastian Heimann's avatar
Sebastian Heimann committed
992
993
994
995

    if nparallel > 1:
        status = ()

996
    event_name = event_names[ievent]
Sebastian Heimann's avatar
Sebastian Heimann committed
997

998
    ds = config.get_dataset(event_name)
Sebastian Heimann's avatar
Sebastian Heimann committed
999

1000
    nevents = len(event_names)